]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/gofrontend/expressions.cc
PR translation/52264
[thirdparty/gcc.git] / gcc / go / gofrontend / expressions.cc
CommitLineData
e440a328 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 <gmp.h>
10
11#ifndef ENABLE_BUILD_WITH_CXX
12extern "C"
13{
14#endif
15
16#include "toplev.h"
17#include "intl.h"
18#include "tree.h"
19#include "gimple.h"
20#include "tree-iterator.h"
21#include "convert.h"
22#include "real.h"
23#include "realmpfr.h"
e440a328 24
25#ifndef ENABLE_BUILD_WITH_CXX
26}
27#endif
28
29#include "go-c.h"
30#include "gogo.h"
31#include "types.h"
32#include "export.h"
33#include "import.h"
34#include "statements.h"
35#include "lex.h"
a9182619 36#include "runtime.h"
6e193e6f 37#include "backend.h"
e440a328 38#include "expressions.h"
d751bb78 39#include "ast-dump.h"
e440a328 40
41// Class Expression.
42
43Expression::Expression(Expression_classification classification,
b13c66cd 44 Location location)
e440a328 45 : classification_(classification), location_(location)
46{
47}
48
49Expression::~Expression()
50{
51}
52
53// If this expression has a constant integer value, return it.
54
55bool
56Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
57 Type** ptype) const
58{
59 *ptype = NULL;
60 return this->do_integer_constant_value(iota_is_constant, val, ptype);
61}
62
63// If this expression has a constant floating point value, return it.
64
65bool
66Expression::float_constant_value(mpfr_t val, Type** ptype) const
67{
68 *ptype = NULL;
69 if (this->do_float_constant_value(val, ptype))
70 return true;
71 mpz_t ival;
72 mpz_init(ival);
73 Type* t;
74 bool ret;
75 if (!this->do_integer_constant_value(false, ival, &t))
76 ret = false;
77 else
78 {
79 mpfr_set_z(val, ival, GMP_RNDN);
80 ret = true;
81 }
82 mpz_clear(ival);
83 return ret;
84}
85
86// If this expression has a constant complex value, return it.
87
88bool
89Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
90 Type** ptype) const
91{
92 *ptype = NULL;
93 if (this->do_complex_constant_value(real, imag, ptype))
94 return true;
95 Type *t;
96 if (this->float_constant_value(real, &t))
97 {
98 mpfr_set_ui(imag, 0, GMP_RNDN);
99 return true;
100 }
101 return false;
102}
103
104// Traverse the expressions.
105
106int
107Expression::traverse(Expression** pexpr, Traverse* traverse)
108{
109 Expression* expr = *pexpr;
110 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
111 {
112 int t = traverse->expression(pexpr);
113 if (t == TRAVERSE_EXIT)
114 return TRAVERSE_EXIT;
115 else if (t == TRAVERSE_SKIP_COMPONENTS)
116 return TRAVERSE_CONTINUE;
117 }
118 return expr->do_traverse(traverse);
119}
120
121// Traverse subexpressions of this expression.
122
123int
124Expression::traverse_subexpressions(Traverse* traverse)
125{
126 return this->do_traverse(traverse);
127}
128
129// Default implementation for do_traverse for child classes.
130
131int
132Expression::do_traverse(Traverse*)
133{
134 return TRAVERSE_CONTINUE;
135}
136
137// This virtual function is called by the parser if the value of this
a7549a6a 138// expression is being discarded. By default, we give an error.
139// Expressions with side effects override.
e440a328 140
141void
142Expression::do_discarding_value()
143{
a7549a6a 144 this->unused_value_error();
e440a328 145}
146
147// This virtual function is called to export expressions. This will
148// only be used by expressions which may be constant.
149
150void
151Expression::do_export(Export*) const
152{
c3e6f413 153 go_unreachable();
e440a328 154}
155
a7549a6a 156// Give an error saying that the value of the expression is not used.
e440a328 157
158void
a7549a6a 159Expression::unused_value_error()
e440a328 160{
a7549a6a 161 error_at(this->location(), "value computed is not used");
e440a328 162}
163
164// Note that this expression is an error. This is called by children
165// when they discover an error.
166
167void
168Expression::set_is_error()
169{
170 this->classification_ = EXPRESSION_ERROR;
171}
172
173// For children to call to report an error conveniently.
174
175void
176Expression::report_error(const char* msg)
177{
178 error_at(this->location_, "%s", msg);
179 this->set_is_error();
180}
181
182// Set types of variables and constants. This is implemented by the
183// child class.
184
185void
186Expression::determine_type(const Type_context* context)
187{
188 this->do_determine_type(context);
189}
190
191// Set types when there is no context.
192
193void
194Expression::determine_type_no_context()
195{
196 Type_context context;
197 this->do_determine_type(&context);
198}
199
200// Return a tree handling any conversions which must be done during
201// assignment.
202
203tree
204Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
205 Type* rhs_type, tree rhs_tree,
b13c66cd 206 Location location)
e440a328 207{
208 if (lhs_type == rhs_type)
209 return rhs_tree;
210
5c13bd80 211 if (lhs_type->is_error() || rhs_type->is_error())
e440a328 212 return error_mark_node;
213
e440a328 214 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
215 return error_mark_node;
216
217 Gogo* gogo = context->gogo();
218
9f0e0513 219 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 220 if (lhs_type_tree == error_mark_node)
221 return error_mark_node;
222
223 if (lhs_type->interface_type() != NULL)
224 {
225 if (rhs_type->interface_type() == NULL)
226 return Expression::convert_type_to_interface(context, lhs_type,
227 rhs_type, rhs_tree,
228 location);
229 else
230 return Expression::convert_interface_to_interface(context, lhs_type,
231 rhs_type, rhs_tree,
232 false, location);
233 }
234 else if (rhs_type->interface_type() != NULL)
235 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
236 rhs_tree, location);
411eb89e 237 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
e440a328 238 {
239 // Assigning nil to an open array.
c484d925 240 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
e440a328 241
242 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
243
244 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
245 tree field = TYPE_FIELDS(lhs_type_tree);
c484d925 246 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 247 "__values") == 0);
248 elt->index = field;
249 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
250
251 elt = VEC_quick_push(constructor_elt, init, NULL);
252 field = DECL_CHAIN(field);
c484d925 253 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 254 "__count") == 0);
255 elt->index = field;
256 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
257
258 elt = VEC_quick_push(constructor_elt, init, NULL);
259 field = DECL_CHAIN(field);
c484d925 260 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 261 "__capacity") == 0);
262 elt->index = field;
263 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
264
265 tree val = build_constructor(lhs_type_tree, init);
266 TREE_CONSTANT(val) = 1;
267
268 return val;
269 }
270 else if (rhs_type->is_nil_type())
271 {
272 // The left hand side should be a pointer type at the tree
273 // level.
c484d925 274 go_assert(POINTER_TYPE_P(lhs_type_tree));
e440a328 275 return fold_convert(lhs_type_tree, null_pointer_node);
276 }
277 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
278 {
279 // No conversion is needed.
280 return rhs_tree;
281 }
282 else if (POINTER_TYPE_P(lhs_type_tree)
283 || INTEGRAL_TYPE_P(lhs_type_tree)
284 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
285 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
b13c66cd 286 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
e440a328 287 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
288 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
289 {
290 // This conversion must be permitted by Go, or we wouldn't have
291 // gotten here.
c484d925 292 go_assert(int_size_in_bytes(lhs_type_tree)
e440a328 293 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
b13c66cd 294 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
295 lhs_type_tree, rhs_tree);
e440a328 296 }
297 else
298 {
c484d925 299 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
e440a328 300 return rhs_tree;
301 }
302}
303
304// Return a tree for a conversion from a non-interface type to an
305// interface type.
306
307tree
308Expression::convert_type_to_interface(Translate_context* context,
309 Type* lhs_type, Type* rhs_type,
b13c66cd 310 tree rhs_tree, Location location)
e440a328 311{
312 Gogo* gogo = context->gogo();
313 Interface_type* lhs_interface_type = lhs_type->interface_type();
314 bool lhs_is_empty = lhs_interface_type->is_empty();
315
316 // Since RHS_TYPE is a static type, we can create the interface
317 // method table at compile time.
318
319 // When setting an interface to nil, we just set both fields to
320 // NULL.
321 if (rhs_type->is_nil_type())
63697958 322 {
323 Btype* lhs_btype = lhs_type->get_backend(gogo);
324 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
325 }
e440a328 326
327 // This should have been checked already.
c484d925 328 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
e440a328 329
9f0e0513 330 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 331 if (lhs_type_tree == error_mark_node)
332 return error_mark_node;
333
334 // An interface is a tuple. If LHS_TYPE is an empty interface type,
335 // then the first field is the type descriptor for RHS_TYPE.
336 // Otherwise it is the interface method table for RHS_TYPE.
337 tree first_field_value;
338 if (lhs_is_empty)
a1d23b41 339 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
e440a328 340 else
341 {
342 // Build the interface method table for this interface and this
343 // object type: a list of function pointers for each interface
344 // method.
345 Named_type* rhs_named_type = rhs_type->named_type();
346 bool is_pointer = false;
347 if (rhs_named_type == NULL)
348 {
349 rhs_named_type = rhs_type->deref()->named_type();
350 is_pointer = true;
351 }
352 tree method_table;
353 if (rhs_named_type == NULL)
354 method_table = null_pointer_node;
355 else
356 method_table =
357 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
358 is_pointer);
b13c66cd 359 first_field_value = fold_convert_loc(location.gcc_location(),
360 const_ptr_type_node, method_table);
e440a328 361 }
84b7d3c6 362 if (first_field_value == error_mark_node)
363 return error_mark_node;
e440a328 364
365 // Start building a constructor for the value we will return.
366
367 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
368
369 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
370 tree field = TYPE_FIELDS(lhs_type_tree);
c484d925 371 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 372 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
373 elt->index = field;
b13c66cd 374 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
375 first_field_value);
e440a328 376
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
c484d925 379 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
e440a328 380 elt->index = field;
381
382 if (rhs_type->points_to() != NULL)
383 {
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
388 }
389
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
392
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
394
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
b13c66cd 396 space = fold_convert_loc(location.gcc_location(),
397 build_pointer_type(TREE_TYPE(rhs_tree)), space);
e440a328 398 space = save_expr(space);
399
b13c66cd 400 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
e440a328 401 TREE_THIS_NOTRAP(ref) = 1;
b13c66cd 402 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
403 void_type_node, ref, rhs_tree);
e440a328 404
b13c66cd 405 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
406 space);
e440a328 407
408 return build2(COMPOUND_EXPR, lhs_type_tree, set,
409 build_constructor(lhs_type_tree, init));
410}
411
412// Return a tree for the type descriptor of RHS_TREE, which has
413// interface type RHS_TYPE. If RHS_TREE is nil the result will be
414// NULL.
415
416tree
417Expression::get_interface_type_descriptor(Translate_context*,
418 Type* rhs_type, tree rhs_tree,
b13c66cd 419 Location location)
e440a328 420{
421 tree rhs_type_tree = TREE_TYPE(rhs_tree);
c484d925 422 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
e440a328 423 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
424 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
425 NULL_TREE);
426 if (rhs_type->interface_type()->is_empty())
427 {
c484d925 428 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
e440a328 429 "__type_descriptor") == 0);
430 return v;
431 }
432
c484d925 433 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
e440a328 434 == 0);
c484d925 435 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
e440a328 436 v = save_expr(v);
b13c66cd 437 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
c484d925 438 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
e440a328 439 tree f = TYPE_FIELDS(TREE_TYPE(v1));
c484d925 440 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
e440a328 441 == 0);
442 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
443
b13c66cd 444 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
445 v, fold_convert_loc(location.gcc_location(),
446 TREE_TYPE(v),
447 null_pointer_node));
448 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
449 null_pointer_node);
450 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
e440a328 451 eq, n, v1);
452}
453
454// Return a tree for the conversion of an interface type to an
455// interface type.
456
457tree
458Expression::convert_interface_to_interface(Translate_context* context,
459 Type *lhs_type, Type *rhs_type,
460 tree rhs_tree, bool for_type_guard,
b13c66cd 461 Location location)
e440a328 462{
463 Gogo* gogo = context->gogo();
464 Interface_type* lhs_interface_type = lhs_type->interface_type();
465 bool lhs_is_empty = lhs_interface_type->is_empty();
466
9f0e0513 467 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 468 if (lhs_type_tree == error_mark_node)
469 return error_mark_node;
470
471 // In the general case this requires runtime examination of the type
472 // method table to match it up with the interface methods.
473
474 // FIXME: If all of the methods in the right hand side interface
475 // also appear in the left hand side interface, then we don't need
476 // to do a runtime check, although we still need to build a new
477 // method table.
478
479 // Get the type descriptor for the right hand side. This will be
480 // NULL for a nil interface.
481
482 if (!DECL_P(rhs_tree))
483 rhs_tree = save_expr(rhs_tree);
484
485 tree rhs_type_descriptor =
486 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
487 location);
488
489 // The result is going to be a two element constructor.
490
491 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
492
493 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
494 tree field = TYPE_FIELDS(lhs_type_tree);
495 elt->index = field;
496
497 if (for_type_guard)
498 {
499 // A type assertion fails when converting a nil interface.
a1d23b41 500 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
501 location);
e440a328 502 static tree assert_interface_decl;
503 tree call = Gogo::call_builtin(&assert_interface_decl,
504 location,
505 "__go_assert_interface",
506 2,
507 ptr_type_node,
508 TREE_TYPE(lhs_type_descriptor),
509 lhs_type_descriptor,
510 TREE_TYPE(rhs_type_descriptor),
511 rhs_type_descriptor);
5fb82b5e 512 if (call == error_mark_node)
513 return error_mark_node;
e440a328 514 // This will panic if the interface conversion fails.
515 TREE_NOTHROW(assert_interface_decl) = 0;
b13c66cd 516 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
517 call);
e440a328 518 }
519 else if (lhs_is_empty)
520 {
521 // A convertion to an empty interface always succeeds, and the
522 // first field is just the type descriptor of the object.
c484d925 523 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 524 "__type_descriptor") == 0);
c484d925 525 go_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
e440a328 526 elt->value = rhs_type_descriptor;
527 }
528 else
529 {
530 // A conversion to a non-empty interface may fail, but unlike a
531 // type assertion converting nil will always succeed.
c484d925 532 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
e440a328 533 == 0);
a1d23b41 534 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
535 location);
e440a328 536 static tree convert_interface_decl;
537 tree call = Gogo::call_builtin(&convert_interface_decl,
538 location,
539 "__go_convert_interface",
540 2,
541 ptr_type_node,
542 TREE_TYPE(lhs_type_descriptor),
543 lhs_type_descriptor,
544 TREE_TYPE(rhs_type_descriptor),
545 rhs_type_descriptor);
5fb82b5e 546 if (call == error_mark_node)
547 return error_mark_node;
e440a328 548 // This will panic if the interface conversion fails.
549 TREE_NOTHROW(convert_interface_decl) = 0;
b13c66cd 550 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
551 call);
e440a328 552 }
553
554 // The second field is simply the object pointer.
555
556 elt = VEC_quick_push(constructor_elt, init, NULL);
557 field = DECL_CHAIN(field);
c484d925 558 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
e440a328 559 elt->index = field;
560
561 tree rhs_type_tree = TREE_TYPE(rhs_tree);
c484d925 562 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
e440a328 563 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
c484d925 564 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
e440a328 565 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
566 NULL_TREE);
567
568 return build_constructor(lhs_type_tree, init);
569}
570
571// Return a tree for the conversion of an interface type to a
572// non-interface type.
573
574tree
575Expression::convert_interface_to_type(Translate_context* context,
576 Type *lhs_type, Type* rhs_type,
b13c66cd 577 tree rhs_tree, Location location)
e440a328 578{
579 Gogo* gogo = context->gogo();
580 tree rhs_type_tree = TREE_TYPE(rhs_tree);
581
9f0e0513 582 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 583 if (lhs_type_tree == error_mark_node)
584 return error_mark_node;
585
586 // Call a function to check that the type is valid. The function
587 // will panic with an appropriate runtime type error if the type is
588 // not valid.
589
a1d23b41 590 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
e440a328 591
592 if (!DECL_P(rhs_tree))
593 rhs_tree = save_expr(rhs_tree);
594
595 tree rhs_type_descriptor =
596 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
597 location);
598
a1d23b41 599 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
600 location);
e440a328 601
602 static tree check_interface_type_decl;
603 tree call = Gogo::call_builtin(&check_interface_type_decl,
604 location,
605 "__go_check_interface_type",
606 3,
607 void_type_node,
608 TREE_TYPE(lhs_type_descriptor),
609 lhs_type_descriptor,
610 TREE_TYPE(rhs_type_descriptor),
611 rhs_type_descriptor,
612 TREE_TYPE(rhs_inter_descriptor),
613 rhs_inter_descriptor);
5fb82b5e 614 if (call == error_mark_node)
615 return error_mark_node;
e440a328 616 // This call will panic if the conversion is invalid.
617 TREE_NOTHROW(check_interface_type_decl) = 0;
618
619 // If the call succeeds, pull out the value.
c484d925 620 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
e440a328 621 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
c484d925 622 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
e440a328 623 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
624 NULL_TREE);
625
626 // If the value is a pointer, then it is the value we want.
627 // Otherwise it points to the value.
628 if (lhs_type->points_to() == NULL)
629 {
b13c66cd 630 val = fold_convert_loc(location.gcc_location(),
631 build_pointer_type(lhs_type_tree), val);
632 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
e440a328 633 }
634
635 return build2(COMPOUND_EXPR, lhs_type_tree, call,
b13c66cd 636 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
e440a328 637}
638
639// Convert an expression to a tree. This is implemented by the child
640// class. Not that it is not in general safe to call this multiple
641// times for a single expression, but that we don't catch such errors.
642
643tree
644Expression::get_tree(Translate_context* context)
645{
646 // The child may have marked this expression as having an error.
647 if (this->classification_ == EXPRESSION_ERROR)
648 return error_mark_node;
649
650 return this->do_get_tree(context);
651}
652
653// Return a tree for VAL in TYPE.
654
655tree
656Expression::integer_constant_tree(mpz_t val, tree type)
657{
658 if (type == error_mark_node)
659 return error_mark_node;
660 else if (TREE_CODE(type) == INTEGER_TYPE)
661 return double_int_to_tree(type,
662 mpz_get_double_int(type, val, true));
663 else if (TREE_CODE(type) == REAL_TYPE)
664 {
665 mpfr_t fval;
666 mpfr_init_set_z(fval, val, GMP_RNDN);
667 tree ret = Expression::float_constant_tree(fval, type);
668 mpfr_clear(fval);
669 return ret;
670 }
671 else if (TREE_CODE(type) == COMPLEX_TYPE)
672 {
673 mpfr_t fval;
674 mpfr_init_set_z(fval, val, GMP_RNDN);
675 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
676 mpfr_clear(fval);
677 tree imag = build_real_from_int_cst(TREE_TYPE(type),
678 integer_zero_node);
679 return build_complex(type, real, imag);
680 }
681 else
c3e6f413 682 go_unreachable();
e440a328 683}
684
685// Return a tree for VAL in TYPE.
686
687tree
688Expression::float_constant_tree(mpfr_t val, tree type)
689{
690 if (type == error_mark_node)
691 return error_mark_node;
692 else if (TREE_CODE(type) == INTEGER_TYPE)
693 {
694 mpz_t ival;
695 mpz_init(ival);
696 mpfr_get_z(ival, val, GMP_RNDN);
697 tree ret = Expression::integer_constant_tree(ival, type);
698 mpz_clear(ival);
699 return ret;
700 }
701 else if (TREE_CODE(type) == REAL_TYPE)
702 {
703 REAL_VALUE_TYPE r1;
704 real_from_mpfr(&r1, val, type, GMP_RNDN);
705 REAL_VALUE_TYPE r2;
706 real_convert(&r2, TYPE_MODE(type), &r1);
707 return build_real(type, r2);
708 }
709 else if (TREE_CODE(type) == COMPLEX_TYPE)
710 {
711 REAL_VALUE_TYPE r1;
712 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
713 REAL_VALUE_TYPE r2;
714 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
715 tree imag = build_real_from_int_cst(TREE_TYPE(type),
716 integer_zero_node);
717 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
718 }
719 else
c3e6f413 720 go_unreachable();
e440a328 721}
722
723// Return a tree for REAL/IMAG in TYPE.
724
725tree
726Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
727{
f690b0bb 728 if (type == error_mark_node)
729 return error_mark_node;
730 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
731 return Expression::float_constant_tree(real, type);
732 else if (TREE_CODE(type) == COMPLEX_TYPE)
e440a328 733 {
734 REAL_VALUE_TYPE r1;
735 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
736 REAL_VALUE_TYPE r2;
737 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
738
739 REAL_VALUE_TYPE r3;
740 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
741 REAL_VALUE_TYPE r4;
742 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
743
744 return build_complex(type, build_real(TREE_TYPE(type), r2),
745 build_real(TREE_TYPE(type), r4));
746 }
747 else
c3e6f413 748 go_unreachable();
e440a328 749}
750
751// Return a tree which evaluates to true if VAL, of arbitrary integer
752// type, is negative or is more than the maximum value of BOUND_TYPE.
753// If SOFAR is not NULL, it is or'red into the result. The return
754// value may be NULL if SOFAR is NULL.
755
756tree
757Expression::check_bounds(tree val, tree bound_type, tree sofar,
b13c66cd 758 Location loc)
e440a328 759{
760 tree val_type = TREE_TYPE(val);
761 tree ret = NULL_TREE;
762
763 if (!TYPE_UNSIGNED(val_type))
764 {
b13c66cd 765 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
e440a328 766 build_int_cst(val_type, 0));
767 if (ret == boolean_false_node)
768 ret = NULL_TREE;
769 }
770
c3068ac0 771 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
772 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
773 go_assert(val_type_size != -1 && bound_type_size != -1);
774 if (val_type_size > bound_type_size
775 || (val_type_size == bound_type_size
776 && TYPE_UNSIGNED(val_type)
777 && !TYPE_UNSIGNED(bound_type)))
e440a328 778 {
779 tree max = TYPE_MAX_VALUE(bound_type);
b13c66cd 780 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
781 val, fold_convert_loc(loc.gcc_location(),
782 val_type, max));
e440a328 783 if (big == boolean_false_node)
784 ;
785 else if (ret == NULL_TREE)
786 ret = big;
787 else
b13c66cd 788 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
789 boolean_type_node, ret, big);
e440a328 790 }
791
792 if (ret == NULL_TREE)
793 return sofar;
794 else if (sofar == NULL_TREE)
795 return ret;
796 else
b13c66cd 797 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
e440a328 798 sofar, ret);
799}
800
d751bb78 801void
802Expression::dump_expression(Ast_dump_context* ast_dump_context) const
803{
804 this->do_dump_expression(ast_dump_context);
805}
806
e440a328 807// Error expressions. This are used to avoid cascading errors.
808
809class Error_expression : public Expression
810{
811 public:
b13c66cd 812 Error_expression(Location location)
e440a328 813 : Expression(EXPRESSION_ERROR, location)
814 { }
815
816 protected:
817 bool
818 do_is_constant() const
819 { return true; }
820
821 bool
822 do_integer_constant_value(bool, mpz_t val, Type**) const
823 {
824 mpz_set_ui(val, 0);
825 return true;
826 }
827
828 bool
829 do_float_constant_value(mpfr_t val, Type**) const
830 {
831 mpfr_set_ui(val, 0, GMP_RNDN);
832 return true;
833 }
834
835 bool
836 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
837 {
838 mpfr_set_ui(real, 0, GMP_RNDN);
839 mpfr_set_ui(imag, 0, GMP_RNDN);
840 return true;
841 }
842
843 void
844 do_discarding_value()
845 { }
846
847 Type*
848 do_type()
849 { return Type::make_error_type(); }
850
851 void
852 do_determine_type(const Type_context*)
853 { }
854
855 Expression*
856 do_copy()
857 { return this; }
858
859 bool
860 do_is_addressable() const
861 { return true; }
862
863 tree
864 do_get_tree(Translate_context*)
865 { return error_mark_node; }
d751bb78 866
867 void
868 do_dump_expression(Ast_dump_context*) const;
e440a328 869};
870
d751bb78 871// Dump the ast representation for an error expression to a dump context.
872
873void
874Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
875{
876 ast_dump_context->ostream() << "_Error_" ;
877}
878
e440a328 879Expression*
b13c66cd 880Expression::make_error(Location location)
e440a328 881{
882 return new Error_expression(location);
883}
884
885// An expression which is really a type. This is used during parsing.
886// It is an error if these survive after lowering.
887
888class
889Type_expression : public Expression
890{
891 public:
b13c66cd 892 Type_expression(Type* type, Location location)
e440a328 893 : Expression(EXPRESSION_TYPE, location),
894 type_(type)
895 { }
896
897 protected:
898 int
899 do_traverse(Traverse* traverse)
900 { return Type::traverse(this->type_, traverse); }
901
902 Type*
903 do_type()
904 { return this->type_; }
905
906 void
907 do_determine_type(const Type_context*)
908 { }
909
910 void
911 do_check_types(Gogo*)
912 { this->report_error(_("invalid use of type")); }
913
914 Expression*
915 do_copy()
916 { return this; }
917
918 tree
919 do_get_tree(Translate_context*)
c3e6f413 920 { go_unreachable(); }
e440a328 921
d751bb78 922 void do_dump_expression(Ast_dump_context*) const;
923
e440a328 924 private:
925 // The type which we are representing as an expression.
926 Type* type_;
927};
928
d751bb78 929void
930Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
931{
932 ast_dump_context->dump_type(this->type_);
933}
934
e440a328 935Expression*
b13c66cd 936Expression::make_type(Type* type, Location location)
e440a328 937{
938 return new Type_expression(type, location);
939}
940
e03bdf36 941// Class Parser_expression.
942
943Type*
944Parser_expression::do_type()
945{
946 // We should never really ask for the type of a Parser_expression.
947 // However, it can happen, at least when we have an invalid const
948 // whose initializer refers to the const itself. In that case we
949 // may ask for the type when lowering the const itself.
c484d925 950 go_assert(saw_errors());
e03bdf36 951 return Type::make_error_type();
952}
953
e440a328 954// Class Var_expression.
955
956// Lower a variable expression. Here we just make sure that the
957// initialization expression of the variable has been lowered. This
958// ensures that we will be able to determine the type of the variable
959// if necessary.
960
961Expression*
ceeb4318 962Var_expression::do_lower(Gogo* gogo, Named_object* function,
963 Statement_inserter* inserter, int)
e440a328 964{
965 if (this->variable_->is_variable())
966 {
967 Variable* var = this->variable_->var_value();
968 // This is either a local variable or a global variable. A
969 // reference to a variable which is local to an enclosing
970 // function will be a reference to a field in a closure.
971 if (var->is_global())
ceeb4318 972 {
973 function = NULL;
974 inserter = NULL;
975 }
976 var->lower_init_expression(gogo, function, inserter);
e440a328 977 }
978 return this;
979}
980
e440a328 981// Return the type of a reference to a variable.
982
983Type*
984Var_expression::do_type()
985{
986 if (this->variable_->is_variable())
987 return this->variable_->var_value()->type();
988 else if (this->variable_->is_result_variable())
989 return this->variable_->result_var_value()->type();
990 else
c3e6f413 991 go_unreachable();
e440a328 992}
993
0ab09e06 994// Determine the type of a reference to a variable.
995
996void
997Var_expression::do_determine_type(const Type_context*)
998{
999 if (this->variable_->is_variable())
1000 this->variable_->var_value()->determine_type();
1001}
1002
e440a328 1003// Something takes the address of this variable. This means that we
1004// may want to move the variable onto the heap.
1005
1006void
1007Var_expression::do_address_taken(bool escapes)
1008{
1009 if (!escapes)
f325319b 1010 {
1011 if (this->variable_->is_variable())
1012 this->variable_->var_value()->set_non_escaping_address_taken();
1013 else if (this->variable_->is_result_variable())
1014 this->variable_->result_var_value()->set_non_escaping_address_taken();
1015 else
1016 go_unreachable();
1017 }
e440a328 1018 else
f325319b 1019 {
1020 if (this->variable_->is_variable())
1021 this->variable_->var_value()->set_address_taken();
1022 else if (this->variable_->is_result_variable())
1023 this->variable_->result_var_value()->set_address_taken();
1024 else
1025 go_unreachable();
1026 }
e440a328 1027}
1028
1029// Get the tree for a reference to a variable.
1030
1031tree
1032Var_expression::do_get_tree(Translate_context* context)
1033{
fe2f84cf 1034 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1035 context->function());
1036 tree ret = var_to_tree(bvar);
1037 if (ret == error_mark_node)
1038 return error_mark_node;
1039 bool is_in_heap;
1040 if (this->variable_->is_variable())
1041 is_in_heap = this->variable_->var_value()->is_in_heap();
1042 else if (this->variable_->is_result_variable())
1043 is_in_heap = this->variable_->result_var_value()->is_in_heap();
1044 else
c3e6f413 1045 go_unreachable();
fe2f84cf 1046 if (is_in_heap)
1047 {
b13c66cd 1048 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
fe2f84cf 1049 TREE_THIS_NOTRAP(ret) = 1;
1050 }
1051 return ret;
e440a328 1052}
1053
d751bb78 1054// Ast dump for variable expression.
1055
1056void
1057Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1058{
1059 ast_dump_context->ostream() << this->variable_->name() ;
1060}
1061
e440a328 1062// Make a reference to a variable in an expression.
1063
1064Expression*
b13c66cd 1065Expression::make_var_reference(Named_object* var, Location location)
e440a328 1066{
1067 if (var->is_sink())
1068 return Expression::make_sink(location);
1069
1070 // FIXME: Creating a new object for each reference to a variable is
1071 // wasteful.
1072 return new Var_expression(var, location);
1073}
1074
1075// Class Temporary_reference_expression.
1076
1077// The type.
1078
1079Type*
1080Temporary_reference_expression::do_type()
1081{
1082 return this->statement_->type();
1083}
1084
1085// Called if something takes the address of this temporary variable.
1086// We never have to move temporary variables to the heap, but we do
1087// need to know that they must live in the stack rather than in a
1088// register.
1089
1090void
1091Temporary_reference_expression::do_address_taken(bool)
1092{
1093 this->statement_->set_is_address_taken();
1094}
1095
1096// Get a tree referring to the variable.
1097
1098tree
eefc1ed3 1099Temporary_reference_expression::do_get_tree(Translate_context* context)
e440a328 1100{
eefc1ed3 1101 Bvariable* bvar = this->statement_->get_backend_variable(context);
1102
1103 // The gcc backend can't represent the same set of recursive types
1104 // that the Go frontend can. In some cases this means that a
1105 // temporary variable won't have the right backend type. Correct
1106 // that here by adding a type cast. We need to use base() to push
1107 // the circularity down one level.
1108 tree ret = var_to_tree(bvar);
ceeb4318 1109 if (!this->is_lvalue_
1110 && POINTER_TYPE_P(TREE_TYPE(ret))
1111 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
eefc1ed3 1112 {
9f0e0513 1113 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1114 tree type_tree = type_to_tree(type_btype);
b13c66cd 1115 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
eefc1ed3 1116 }
1117 return ret;
e440a328 1118}
1119
d751bb78 1120// Ast dump for temporary reference.
1121
1122void
1123Temporary_reference_expression::do_dump_expression(
1124 Ast_dump_context* ast_dump_context) const
1125{
1126 ast_dump_context->dump_temp_variable_name(this->statement_);
1127}
1128
e440a328 1129// Make a reference to a temporary variable.
1130
ceeb4318 1131Temporary_reference_expression*
e440a328 1132Expression::make_temporary_reference(Temporary_statement* statement,
b13c66cd 1133 Location location)
e440a328 1134{
1135 return new Temporary_reference_expression(statement, location);
1136}
1137
e9d3367e 1138// Class Set_and_use_temporary_expression.
1139
1140// Return the type.
1141
1142Type*
1143Set_and_use_temporary_expression::do_type()
1144{
1145 return this->statement_->type();
1146}
1147
1148// Take the address.
1149
1150void
1151Set_and_use_temporary_expression::do_address_taken(bool)
1152{
1153 this->statement_->set_is_address_taken();
1154}
1155
1156// Return the backend representation.
1157
1158tree
1159Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1160{
1161 Bvariable* bvar = this->statement_->get_backend_variable(context);
1162 tree var_tree = var_to_tree(bvar);
1163 tree expr_tree = this->expr_->get_tree(context);
1164 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1165 return error_mark_node;
1166 Location loc = this->location();
1167 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1168 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1169 var_tree, expr_tree),
1170 var_tree);
1171}
1172
1173// Dump.
1174
1175void
1176Set_and_use_temporary_expression::do_dump_expression(
1177 Ast_dump_context* ast_dump_context) const
1178{
1179 ast_dump_context->ostream() << '(';
1180 ast_dump_context->dump_temp_variable_name(this->statement_);
1181 ast_dump_context->ostream() << " = ";
1182 this->expr_->dump_expression(ast_dump_context);
1183 ast_dump_context->ostream() << ')';
1184}
1185
1186// Make a set-and-use temporary.
1187
1188Set_and_use_temporary_expression*
1189Expression::make_set_and_use_temporary(Temporary_statement* statement,
1190 Expression* expr, Location location)
1191{
1192 return new Set_and_use_temporary_expression(statement, expr, location);
1193}
1194
e440a328 1195// A sink expression--a use of the blank identifier _.
1196
1197class Sink_expression : public Expression
1198{
1199 public:
b13c66cd 1200 Sink_expression(Location location)
e440a328 1201 : Expression(EXPRESSION_SINK, location),
1202 type_(NULL), var_(NULL_TREE)
1203 { }
1204
1205 protected:
1206 void
1207 do_discarding_value()
1208 { }
1209
1210 Type*
1211 do_type();
1212
1213 void
1214 do_determine_type(const Type_context*);
1215
1216 Expression*
1217 do_copy()
1218 { return new Sink_expression(this->location()); }
1219
1220 tree
1221 do_get_tree(Translate_context*);
1222
d751bb78 1223 void
1224 do_dump_expression(Ast_dump_context*) const;
1225
e440a328 1226 private:
1227 // The type of this sink variable.
1228 Type* type_;
1229 // The temporary variable we generate.
1230 tree var_;
1231};
1232
1233// Return the type of a sink expression.
1234
1235Type*
1236Sink_expression::do_type()
1237{
1238 if (this->type_ == NULL)
1239 return Type::make_sink_type();
1240 return this->type_;
1241}
1242
1243// Determine the type of a sink expression.
1244
1245void
1246Sink_expression::do_determine_type(const Type_context* context)
1247{
1248 if (context->type != NULL)
1249 this->type_ = context->type;
1250}
1251
1252// Return a temporary variable for a sink expression. This will
1253// presumably be a write-only variable which the middle-end will drop.
1254
1255tree
1256Sink_expression::do_get_tree(Translate_context* context)
1257{
1258 if (this->var_ == NULL_TREE)
1259 {
c484d925 1260 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
9f0e0513 1261 Btype* bt = this->type_->get_backend(context->gogo());
1262 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
e440a328 1263 }
1264 return this->var_;
1265}
1266
d751bb78 1267// Ast dump for sink expression.
1268
1269void
1270Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1271{
1272 ast_dump_context->ostream() << "_" ;
1273}
1274
e440a328 1275// Make a sink expression.
1276
1277Expression*
b13c66cd 1278Expression::make_sink(Location location)
e440a328 1279{
1280 return new Sink_expression(location);
1281}
1282
1283// Class Func_expression.
1284
1285// FIXME: Can a function expression appear in a constant expression?
1286// The value is unchanging. Initializing a constant to the address of
1287// a function seems like it could work, though there might be little
1288// point to it.
1289
e440a328 1290// Traversal.
1291
1292int
1293Func_expression::do_traverse(Traverse* traverse)
1294{
1295 return (this->closure_ == NULL
1296 ? TRAVERSE_CONTINUE
1297 : Expression::traverse(&this->closure_, traverse));
1298}
1299
1300// Return the type of a function expression.
1301
1302Type*
1303Func_expression::do_type()
1304{
1305 if (this->function_->is_function())
1306 return this->function_->func_value()->type();
1307 else if (this->function_->is_function_declaration())
1308 return this->function_->func_declaration_value()->type();
1309 else
c3e6f413 1310 go_unreachable();
e440a328 1311}
1312
1313// Get the tree for a function expression without evaluating the
1314// closure.
1315
1316tree
1317Func_expression::get_tree_without_closure(Gogo* gogo)
1318{
1319 Function_type* fntype;
1320 if (this->function_->is_function())
1321 fntype = this->function_->func_value()->type();
1322 else if (this->function_->is_function_declaration())
1323 fntype = this->function_->func_declaration_value()->type();
1324 else
c3e6f413 1325 go_unreachable();
e440a328 1326
1327 // Builtin functions are handled specially by Call_expression. We
1328 // can't take their address.
1329 if (fntype->is_builtin())
1330 {
cb0e02f3 1331 error_at(this->location(),
1332 "invalid use of special builtin function %qs; must be called",
e440a328 1333 this->function_->name().c_str());
1334 return error_mark_node;
1335 }
1336
1337 Named_object* no = this->function_;
9d6f3721 1338
1339 tree id = no->get_id(gogo);
1340 if (id == error_mark_node)
1341 return error_mark_node;
1342
e440a328 1343 tree fndecl;
1344 if (no->is_function())
1345 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1346 else if (no->is_function_declaration())
1347 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1348 else
c3e6f413 1349 go_unreachable();
e440a328 1350
9d6f3721 1351 if (fndecl == error_mark_node)
1352 return error_mark_node;
1353
b13c66cd 1354 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
e440a328 1355}
1356
1357// Get the tree for a function expression. This is used when we take
1358// the address of a function rather than simply calling it. If the
1359// function has a closure, we must use a trampoline.
1360
1361tree
1362Func_expression::do_get_tree(Translate_context* context)
1363{
1364 Gogo* gogo = context->gogo();
1365
1366 tree fnaddr = this->get_tree_without_closure(gogo);
1367 if (fnaddr == error_mark_node)
1368 return error_mark_node;
1369
c484d925 1370 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
e440a328 1371 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1372 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1373
1374 // For a normal non-nested function call, that is all we have to do.
1375 if (!this->function_->is_function()
1376 || this->function_->func_value()->enclosing() == NULL)
1377 {
c484d925 1378 go_assert(this->closure_ == NULL);
e440a328 1379 return fnaddr;
1380 }
1381
1382 // For a nested function call, we have to always allocate a
1383 // trampoline. If we don't always allocate, then closures will not
1384 // be reliably distinct.
1385 Expression* closure = this->closure_;
1386 tree closure_tree;
1387 if (closure == NULL)
1388 closure_tree = null_pointer_node;
1389 else
1390 {
1391 // Get the value of the closure. This will be a pointer to
1392 // space allocated on the heap.
1393 closure_tree = closure->get_tree(context);
1394 if (closure_tree == error_mark_node)
1395 return error_mark_node;
c484d925 1396 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
e440a328 1397 }
1398
1399 // Now we need to build some code on the heap. This code will load
1400 // the static chain pointer with the closure and then jump to the
1401 // body of the function. The normal gcc approach is to build the
1402 // code on the stack. Unfortunately we can not do that, as Go
1403 // permits us to return the function pointer.
1404
1405 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1406}
1407
d751bb78 1408// Ast dump for function.
1409
1410void
1411Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1412{
8b1c301d 1413 ast_dump_context->ostream() << this->function_->name();
1414 if (this->closure_ != NULL)
1415 {
1416 ast_dump_context->ostream() << " {closure = ";
1417 this->closure_->dump_expression(ast_dump_context);
1418 ast_dump_context->ostream() << "}";
1419 }
d751bb78 1420}
1421
e440a328 1422// Make a reference to a function in an expression.
1423
1424Expression*
1425Expression::make_func_reference(Named_object* function, Expression* closure,
b13c66cd 1426 Location location)
e440a328 1427{
1428 return new Func_expression(function, closure, location);
1429}
1430
1431// Class Unknown_expression.
1432
1433// Return the name of an unknown expression.
1434
1435const std::string&
1436Unknown_expression::name() const
1437{
1438 return this->named_object_->name();
1439}
1440
1441// Lower a reference to an unknown name.
1442
1443Expression*
ceeb4318 1444Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 1445{
b13c66cd 1446 Location location = this->location();
e440a328 1447 Named_object* no = this->named_object_;
deded542 1448 Named_object* real;
1449 if (!no->is_unknown())
1450 real = no;
1451 else
e440a328 1452 {
deded542 1453 real = no->unknown_value()->real_named_object();
1454 if (real == NULL)
1455 {
1456 if (this->is_composite_literal_key_)
1457 return this;
acf8e158 1458 if (!this->no_error_message_)
1459 error_at(location, "reference to undefined name %qs",
1460 this->named_object_->message_name().c_str());
deded542 1461 return Expression::make_error(location);
1462 }
e440a328 1463 }
1464 switch (real->classification())
1465 {
1466 case Named_object::NAMED_OBJECT_CONST:
1467 return Expression::make_const_reference(real, location);
1468 case Named_object::NAMED_OBJECT_TYPE:
1469 return Expression::make_type(real->type_value(), location);
1470 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1471 if (this->is_composite_literal_key_)
1472 return this;
acf8e158 1473 if (!this->no_error_message_)
1474 error_at(location, "reference to undefined type %qs",
1475 real->message_name().c_str());
e440a328 1476 return Expression::make_error(location);
1477 case Named_object::NAMED_OBJECT_VAR:
7d834090 1478 real->var_value()->set_is_used();
e440a328 1479 return Expression::make_var_reference(real, location);
1480 case Named_object::NAMED_OBJECT_FUNC:
1481 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1482 return Expression::make_func_reference(real, NULL, location);
1483 case Named_object::NAMED_OBJECT_PACKAGE:
1484 if (this->is_composite_literal_key_)
1485 return this;
acf8e158 1486 if (!this->no_error_message_)
1487 error_at(location, "unexpected reference to package");
e440a328 1488 return Expression::make_error(location);
1489 default:
c3e6f413 1490 go_unreachable();
e440a328 1491 }
1492}
1493
d751bb78 1494// Dump the ast representation for an unknown expression to a dump context.
1495
1496void
1497Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1498{
1499 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1500 << ")";
d751bb78 1501}
1502
e440a328 1503// Make a reference to an unknown name.
1504
acf8e158 1505Unknown_expression*
b13c66cd 1506Expression::make_unknown_reference(Named_object* no, Location location)
e440a328 1507{
e440a328 1508 return new Unknown_expression(no, location);
1509}
1510
1511// A boolean expression.
1512
1513class Boolean_expression : public Expression
1514{
1515 public:
b13c66cd 1516 Boolean_expression(bool val, Location location)
e440a328 1517 : Expression(EXPRESSION_BOOLEAN, location),
1518 val_(val), type_(NULL)
1519 { }
1520
1521 static Expression*
1522 do_import(Import*);
1523
1524 protected:
1525 bool
1526 do_is_constant() const
1527 { return true; }
1528
1529 Type*
1530 do_type();
1531
1532 void
1533 do_determine_type(const Type_context*);
1534
1535 Expression*
1536 do_copy()
1537 { return this; }
1538
1539 tree
1540 do_get_tree(Translate_context*)
1541 { return this->val_ ? boolean_true_node : boolean_false_node; }
1542
1543 void
1544 do_export(Export* exp) const
1545 { exp->write_c_string(this->val_ ? "true" : "false"); }
1546
d751bb78 1547 void
1548 do_dump_expression(Ast_dump_context* ast_dump_context) const
1549 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1550
e440a328 1551 private:
1552 // The constant.
1553 bool val_;
1554 // The type as determined by context.
1555 Type* type_;
1556};
1557
1558// Get the type.
1559
1560Type*
1561Boolean_expression::do_type()
1562{
1563 if (this->type_ == NULL)
1564 this->type_ = Type::make_boolean_type();
1565 return this->type_;
1566}
1567
1568// Set the type from the context.
1569
1570void
1571Boolean_expression::do_determine_type(const Type_context* context)
1572{
1573 if (this->type_ != NULL && !this->type_->is_abstract())
1574 ;
1575 else if (context->type != NULL && context->type->is_boolean_type())
1576 this->type_ = context->type;
1577 else if (!context->may_be_abstract)
1578 this->type_ = Type::lookup_bool_type();
1579}
1580
1581// Import a boolean constant.
1582
1583Expression*
1584Boolean_expression::do_import(Import* imp)
1585{
1586 if (imp->peek_char() == 't')
1587 {
1588 imp->require_c_string("true");
1589 return Expression::make_boolean(true, imp->location());
1590 }
1591 else
1592 {
1593 imp->require_c_string("false");
1594 return Expression::make_boolean(false, imp->location());
1595 }
1596}
1597
1598// Make a boolean expression.
1599
1600Expression*
b13c66cd 1601Expression::make_boolean(bool val, Location location)
e440a328 1602{
1603 return new Boolean_expression(val, location);
1604}
1605
1606// Class String_expression.
1607
1608// Get the type.
1609
1610Type*
1611String_expression::do_type()
1612{
1613 if (this->type_ == NULL)
1614 this->type_ = Type::make_string_type();
1615 return this->type_;
1616}
1617
1618// Set the type from the context.
1619
1620void
1621String_expression::do_determine_type(const Type_context* context)
1622{
1623 if (this->type_ != NULL && !this->type_->is_abstract())
1624 ;
1625 else if (context->type != NULL && context->type->is_string_type())
1626 this->type_ = context->type;
1627 else if (!context->may_be_abstract)
1628 this->type_ = Type::lookup_string_type();
1629}
1630
1631// Build a string constant.
1632
1633tree
1634String_expression::do_get_tree(Translate_context* context)
1635{
1636 return context->gogo()->go_string_constant_tree(this->val_);
1637}
1638
8b1c301d 1639 // Write string literal to string dump.
e440a328 1640
1641void
8b1c301d 1642String_expression::export_string(String_dump* exp,
1643 const String_expression* str)
e440a328 1644{
1645 std::string s;
8b1c301d 1646 s.reserve(str->val_.length() * 4 + 2);
e440a328 1647 s += '"';
8b1c301d 1648 for (std::string::const_iterator p = str->val_.begin();
1649 p != str->val_.end();
e440a328 1650 ++p)
1651 {
1652 if (*p == '\\' || *p == '"')
1653 {
1654 s += '\\';
1655 s += *p;
1656 }
1657 else if (*p >= 0x20 && *p < 0x7f)
1658 s += *p;
1659 else if (*p == '\n')
1660 s += "\\n";
1661 else if (*p == '\t')
1662 s += "\\t";
1663 else
1664 {
1665 s += "\\x";
1666 unsigned char c = *p;
1667 unsigned int dig = c >> 4;
1668 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1669 dig = c & 0xf;
1670 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1671 }
1672 }
1673 s += '"';
1674 exp->write_string(s);
1675}
1676
8b1c301d 1677// Export a string expression.
1678
1679void
1680String_expression::do_export(Export* exp) const
1681{
1682 String_expression::export_string(exp, this);
1683}
1684
e440a328 1685// Import a string expression.
1686
1687Expression*
1688String_expression::do_import(Import* imp)
1689{
1690 imp->require_c_string("\"");
1691 std::string val;
1692 while (true)
1693 {
1694 int c = imp->get_char();
1695 if (c == '"' || c == -1)
1696 break;
1697 if (c != '\\')
1698 val += static_cast<char>(c);
1699 else
1700 {
1701 c = imp->get_char();
1702 if (c == '\\' || c == '"')
1703 val += static_cast<char>(c);
1704 else if (c == 'n')
1705 val += '\n';
1706 else if (c == 't')
1707 val += '\t';
1708 else if (c == 'x')
1709 {
1710 c = imp->get_char();
1711 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1712 c = imp->get_char();
1713 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1714 char v = (vh << 4) | vl;
1715 val += v;
1716 }
1717 else
1718 {
1719 error_at(imp->location(), "bad string constant");
1720 return Expression::make_error(imp->location());
1721 }
1722 }
1723 }
1724 return Expression::make_string(val, imp->location());
1725}
1726
d751bb78 1727// Ast dump for string expression.
1728
1729void
1730String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1731{
8b1c301d 1732 String_expression::export_string(ast_dump_context, this);
d751bb78 1733}
1734
e440a328 1735// Make a string expression.
1736
1737Expression*
b13c66cd 1738Expression::make_string(const std::string& val, Location location)
e440a328 1739{
1740 return new String_expression(val, location);
1741}
1742
1743// Make an integer expression.
1744
1745class Integer_expression : public Expression
1746{
1747 public:
5d4b8566 1748 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1749 Location location)
e440a328 1750 : Expression(EXPRESSION_INTEGER, location),
5d4b8566 1751 type_(type), is_character_constant_(is_character_constant)
e440a328 1752 { mpz_init_set(this->val_, *val); }
1753
1754 static Expression*
1755 do_import(Import*);
1756
1757 // Return whether VAL fits in the type.
1758 static bool
b13c66cd 1759 check_constant(mpz_t val, Type*, Location);
e440a328 1760
8b1c301d 1761 // Write VAL to string dump.
e440a328 1762 static void
8b1c301d 1763 export_integer(String_dump* exp, const mpz_t val);
e440a328 1764
d751bb78 1765 // Write VAL to dump context.
1766 static void
1767 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1768
e440a328 1769 protected:
1770 bool
1771 do_is_constant() const
1772 { return true; }
1773
1774 bool
1775 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1776
1777 Type*
1778 do_type();
1779
1780 void
1781 do_determine_type(const Type_context* context);
1782
1783 void
1784 do_check_types(Gogo*);
1785
1786 tree
1787 do_get_tree(Translate_context*);
1788
1789 Expression*
1790 do_copy()
5d4b8566 1791 {
1792 if (this->is_character_constant_)
1793 return Expression::make_character(&this->val_, this->type_,
1794 this->location());
1795 else
1796 return Expression::make_integer(&this->val_, this->type_,
1797 this->location());
1798 }
e440a328 1799
1800 void
1801 do_export(Export*) const;
1802
d751bb78 1803 void
1804 do_dump_expression(Ast_dump_context*) const;
1805
e440a328 1806 private:
1807 // The integer value.
1808 mpz_t val_;
1809 // The type so far.
1810 Type* type_;
5d4b8566 1811 // Whether this is a character constant.
1812 bool is_character_constant_;
e440a328 1813};
1814
1815// Return an integer constant value.
1816
1817bool
1818Integer_expression::do_integer_constant_value(bool, mpz_t val,
1819 Type** ptype) const
1820{
1821 if (this->type_ != NULL)
1822 *ptype = this->type_;
1823 mpz_set(val, this->val_);
1824 return true;
1825}
1826
1827// Return the current type. If we haven't set the type yet, we return
1828// an abstract integer type.
1829
1830Type*
1831Integer_expression::do_type()
1832{
1833 if (this->type_ == NULL)
5d4b8566 1834 {
1835 if (this->is_character_constant_)
1836 this->type_ = Type::make_abstract_character_type();
1837 else
1838 this->type_ = Type::make_abstract_integer_type();
1839 }
e440a328 1840 return this->type_;
1841}
1842
1843// Set the type of the integer value. Here we may switch from an
1844// abstract type to a real type.
1845
1846void
1847Integer_expression::do_determine_type(const Type_context* context)
1848{
1849 if (this->type_ != NULL && !this->type_->is_abstract())
1850 ;
1851 else if (context->type != NULL
1852 && (context->type->integer_type() != NULL
1853 || context->type->float_type() != NULL
1854 || context->type->complex_type() != NULL))
1855 this->type_ = context->type;
1856 else if (!context->may_be_abstract)
5d4b8566 1857 {
1858 if (this->is_character_constant_)
1859 this->type_ = Type::lookup_integer_type("int32");
1860 else
1861 this->type_ = Type::lookup_integer_type("int");
1862 }
e440a328 1863}
1864
1865// Return true if the integer VAL fits in the range of the type TYPE.
1866// Otherwise give an error and return false. TYPE may be NULL.
1867
1868bool
1869Integer_expression::check_constant(mpz_t val, Type* type,
b13c66cd 1870 Location location)
e440a328 1871{
1872 if (type == NULL)
1873 return true;
1874 Integer_type* itype = type->integer_type();
1875 if (itype == NULL || itype->is_abstract())
1876 return true;
1877
1878 int bits = mpz_sizeinbase(val, 2);
1879
1880 if (itype->is_unsigned())
1881 {
1882 // For an unsigned type we can only accept a nonnegative number,
1883 // and we must be able to represent at least BITS.
1884 if (mpz_sgn(val) >= 0
1885 && bits <= itype->bits())
1886 return true;
1887 }
1888 else
1889 {
1890 // For a signed type we need an extra bit to indicate the sign.
1891 // We have to handle the most negative integer specially.
1892 if (bits + 1 <= itype->bits()
1893 || (bits <= itype->bits()
1894 && mpz_sgn(val) < 0
1895 && (mpz_scan1(val, 0)
1896 == static_cast<unsigned long>(itype->bits() - 1))
1897 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1898 return true;
1899 }
1900
1901 error_at(location, "integer constant overflow");
1902 return false;
1903}
1904
1905// Check the type of an integer constant.
1906
1907void
1908Integer_expression::do_check_types(Gogo*)
1909{
1910 if (this->type_ == NULL)
1911 return;
1912 if (!Integer_expression::check_constant(this->val_, this->type_,
1913 this->location()))
1914 this->set_is_error();
1915}
1916
1917// Get a tree for an integer constant.
1918
1919tree
1920Integer_expression::do_get_tree(Translate_context* context)
1921{
1922 Gogo* gogo = context->gogo();
1923 tree type;
1924 if (this->type_ != NULL && !this->type_->is_abstract())
9f0e0513 1925 type = type_to_tree(this->type_->get_backend(gogo));
e440a328 1926 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1927 {
1928 // We are converting to an abstract floating point type.
9f0e0513 1929 Type* ftype = Type::lookup_float_type("float64");
1930 type = type_to_tree(ftype->get_backend(gogo));
e440a328 1931 }
1932 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1933 {
1934 // We are converting to an abstract complex type.
9f0e0513 1935 Type* ctype = Type::lookup_complex_type("complex128");
1936 type = type_to_tree(ctype->get_backend(gogo));
e440a328 1937 }
1938 else
1939 {
1940 // If we still have an abstract type here, then this is being
1941 // used in a constant expression which didn't get reduced for
1942 // some reason. Use a type which will fit the value. We use <,
1943 // not <=, because we need an extra bit for the sign bit.
1944 int bits = mpz_sizeinbase(this->val_, 2);
1945 if (bits < INT_TYPE_SIZE)
9f0e0513 1946 {
1947 Type* t = Type::lookup_integer_type("int");
1948 type = type_to_tree(t->get_backend(gogo));
1949 }
e440a328 1950 else if (bits < 64)
9f0e0513 1951 {
1952 Type* t = Type::lookup_integer_type("int64");
1953 type = type_to_tree(t->get_backend(gogo));
1954 }
e440a328 1955 else
1956 type = long_long_integer_type_node;
1957 }
1958 return Expression::integer_constant_tree(this->val_, type);
1959}
1960
1961// Write VAL to export data.
1962
1963void
8b1c301d 1964Integer_expression::export_integer(String_dump* exp, const mpz_t val)
e440a328 1965{
1966 char* s = mpz_get_str(NULL, 10, val);
1967 exp->write_c_string(s);
1968 free(s);
1969}
1970
1971// Export an integer in a constant expression.
1972
1973void
1974Integer_expression::do_export(Export* exp) const
1975{
1976 Integer_expression::export_integer(exp, this->val_);
5d4b8566 1977 if (this->is_character_constant_)
1978 exp->write_c_string("'");
e440a328 1979 // A trailing space lets us reliably identify the end of the number.
1980 exp->write_c_string(" ");
1981}
1982
1983// Import an integer, floating point, or complex value. This handles
1984// all these types because they all start with digits.
1985
1986Expression*
1987Integer_expression::do_import(Import* imp)
1988{
1989 std::string num = imp->read_identifier();
1990 imp->require_c_string(" ");
1991 if (!num.empty() && num[num.length() - 1] == 'i')
1992 {
1993 mpfr_t real;
1994 size_t plus_pos = num.find('+', 1);
1995 size_t minus_pos = num.find('-', 1);
1996 size_t pos;
1997 if (plus_pos == std::string::npos)
1998 pos = minus_pos;
1999 else if (minus_pos == std::string::npos)
2000 pos = plus_pos;
2001 else
2002 {
2003 error_at(imp->location(), "bad number in import data: %qs",
2004 num.c_str());
2005 return Expression::make_error(imp->location());
2006 }
2007 if (pos == std::string::npos)
2008 mpfr_set_ui(real, 0, GMP_RNDN);
2009 else
2010 {
2011 std::string real_str = num.substr(0, pos);
2012 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
2013 {
2014 error_at(imp->location(), "bad number in import data: %qs",
2015 real_str.c_str());
2016 return Expression::make_error(imp->location());
2017 }
2018 }
2019
2020 std::string imag_str;
2021 if (pos == std::string::npos)
2022 imag_str = num;
2023 else
2024 imag_str = num.substr(pos);
2025 imag_str = imag_str.substr(0, imag_str.size() - 1);
2026 mpfr_t imag;
2027 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2028 {
2029 error_at(imp->location(), "bad number in import data: %qs",
2030 imag_str.c_str());
2031 return Expression::make_error(imp->location());
2032 }
2033 Expression* ret = Expression::make_complex(&real, &imag, NULL,
2034 imp->location());
2035 mpfr_clear(real);
2036 mpfr_clear(imag);
2037 return ret;
2038 }
2039 else if (num.find('.') == std::string::npos
2040 && num.find('E') == std::string::npos)
2041 {
5d4b8566 2042 bool is_character_constant = (!num.empty()
2043 && num[num.length() - 1] == '\'');
2044 if (is_character_constant)
2045 num = num.substr(0, num.length() - 1);
e440a328 2046 mpz_t val;
2047 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2048 {
2049 error_at(imp->location(), "bad number in import data: %qs",
2050 num.c_str());
2051 return Expression::make_error(imp->location());
2052 }
5d4b8566 2053 Expression* ret;
2054 if (is_character_constant)
2055 ret = Expression::make_character(&val, NULL, imp->location());
2056 else
2057 ret = Expression::make_integer(&val, NULL, imp->location());
e440a328 2058 mpz_clear(val);
2059 return ret;
2060 }
2061 else
2062 {
2063 mpfr_t val;
2064 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2065 {
2066 error_at(imp->location(), "bad number in import data: %qs",
2067 num.c_str());
2068 return Expression::make_error(imp->location());
2069 }
2070 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2071 mpfr_clear(val);
2072 return ret;
2073 }
2074}
d751bb78 2075// Ast dump for integer expression.
2076
2077void
2078Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2079{
5d4b8566 2080 if (this->is_character_constant_)
2081 ast_dump_context->ostream() << '\'';
8b1c301d 2082 Integer_expression::export_integer(ast_dump_context, this->val_);
5d4b8566 2083 if (this->is_character_constant_)
2084 ast_dump_context->ostream() << '\'';
d751bb78 2085}
2086
e440a328 2087// Build a new integer value.
2088
2089Expression*
5d4b8566 2090Expression::make_integer(const mpz_t* val, Type* type, Location location)
2091{
2092 return new Integer_expression(val, type, false, location);
2093}
2094
2095// Build a new character constant value.
2096
2097Expression*
2098Expression::make_character(const mpz_t* val, Type* type, Location location)
e440a328 2099{
5d4b8566 2100 return new Integer_expression(val, type, true, location);
e440a328 2101}
2102
2103// Floats.
2104
2105class Float_expression : public Expression
2106{
2107 public:
b13c66cd 2108 Float_expression(const mpfr_t* val, Type* type, Location location)
e440a328 2109 : Expression(EXPRESSION_FLOAT, location),
2110 type_(type)
2111 {
2112 mpfr_init_set(this->val_, *val, GMP_RNDN);
2113 }
2114
2115 // Constrain VAL to fit into TYPE.
2116 static void
2117 constrain_float(mpfr_t val, Type* type);
2118
2119 // Return whether VAL fits in the type.
2120 static bool
b13c66cd 2121 check_constant(mpfr_t val, Type*, Location);
e440a328 2122
2123 // Write VAL to export data.
2124 static void
8b1c301d 2125 export_float(String_dump* exp, const mpfr_t val);
2126
d751bb78 2127 // Write VAL to dump file.
2128 static void
2129 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
e440a328 2130
2131 protected:
2132 bool
2133 do_is_constant() const
2134 { return true; }
2135
2136 bool
2137 do_float_constant_value(mpfr_t val, Type**) const;
2138
2139 Type*
2140 do_type();
2141
2142 void
2143 do_determine_type(const Type_context*);
2144
2145 void
2146 do_check_types(Gogo*);
2147
2148 Expression*
2149 do_copy()
2150 { return Expression::make_float(&this->val_, this->type_,
2151 this->location()); }
2152
2153 tree
2154 do_get_tree(Translate_context*);
2155
2156 void
2157 do_export(Export*) const;
2158
d751bb78 2159 void
2160 do_dump_expression(Ast_dump_context*) const;
2161
e440a328 2162 private:
2163 // The floating point value.
2164 mpfr_t val_;
2165 // The type so far.
2166 Type* type_;
2167};
2168
2169// Constrain VAL to fit into TYPE.
2170
2171void
2172Float_expression::constrain_float(mpfr_t val, Type* type)
2173{
2174 Float_type* ftype = type->float_type();
2175 if (ftype != NULL && !ftype->is_abstract())
2f50f88a 2176 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
e440a328 2177}
2178
2179// Return a floating point constant value.
2180
2181bool
2182Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2183{
2184 if (this->type_ != NULL)
2185 *ptype = this->type_;
2186 mpfr_set(val, this->val_, GMP_RNDN);
2187 return true;
2188}
2189
2190// Return the current type. If we haven't set the type yet, we return
2191// an abstract float type.
2192
2193Type*
2194Float_expression::do_type()
2195{
2196 if (this->type_ == NULL)
2197 this->type_ = Type::make_abstract_float_type();
2198 return this->type_;
2199}
2200
2201// Set the type of the float value. Here we may switch from an
2202// abstract type to a real type.
2203
2204void
2205Float_expression::do_determine_type(const Type_context* context)
2206{
2207 if (this->type_ != NULL && !this->type_->is_abstract())
2208 ;
2209 else if (context->type != NULL
2210 && (context->type->integer_type() != NULL
2211 || context->type->float_type() != NULL
2212 || context->type->complex_type() != NULL))
2213 this->type_ = context->type;
2214 else if (!context->may_be_abstract)
48080209 2215 this->type_ = Type::lookup_float_type("float64");
e440a328 2216}
2217
2218// Return true if the floating point value VAL fits in the range of
2219// the type TYPE. Otherwise give an error and return false. TYPE may
2220// be NULL.
2221
2222bool
2223Float_expression::check_constant(mpfr_t val, Type* type,
b13c66cd 2224 Location location)
e440a328 2225{
2226 if (type == NULL)
2227 return true;
2228 Float_type* ftype = type->float_type();
2229 if (ftype == NULL || ftype->is_abstract())
2230 return true;
2231
2232 // A NaN or Infinity always fits in the range of the type.
2233 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2234 return true;
2235
2236 mp_exp_t exp = mpfr_get_exp(val);
2237 mp_exp_t max_exp;
2238 switch (ftype->bits())
2239 {
2240 case 32:
2241 max_exp = 128;
2242 break;
2243 case 64:
2244 max_exp = 1024;
2245 break;
2246 default:
c3e6f413 2247 go_unreachable();
e440a328 2248 }
2249 if (exp > max_exp)
2250 {
2251 error_at(location, "floating point constant overflow");
2252 return false;
2253 }
2254 return true;
2255}
2256
2257// Check the type of a float value.
2258
2259void
2260Float_expression::do_check_types(Gogo*)
2261{
2262 if (this->type_ == NULL)
2263 return;
2264
2265 if (!Float_expression::check_constant(this->val_, this->type_,
2266 this->location()))
2267 this->set_is_error();
2268
2269 Integer_type* integer_type = this->type_->integer_type();
2270 if (integer_type != NULL)
2271 {
2272 if (!mpfr_integer_p(this->val_))
2273 this->report_error(_("floating point constant truncated to integer"));
2274 else
2275 {
c484d925 2276 go_assert(!integer_type->is_abstract());
e440a328 2277 mpz_t ival;
2278 mpz_init(ival);
2279 mpfr_get_z(ival, this->val_, GMP_RNDN);
2280 Integer_expression::check_constant(ival, integer_type,
2281 this->location());
2282 mpz_clear(ival);
2283 }
2284 }
2285}
2286
2287// Get a tree for a float constant.
2288
2289tree
2290Float_expression::do_get_tree(Translate_context* context)
2291{
2292 Gogo* gogo = context->gogo();
2293 tree type;
2294 if (this->type_ != NULL && !this->type_->is_abstract())
9f0e0513 2295 type = type_to_tree(this->type_->get_backend(gogo));
e440a328 2296 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2297 {
2298 // We have an abstract integer type. We just hope for the best.
9f0e0513 2299 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
e440a328 2300 }
2301 else
2302 {
2303 // If we still have an abstract type here, then this is being
2304 // used in a constant expression which didn't get reduced. We
2305 // just use float64 and hope for the best.
9f0e0513 2306 Type* ft = Type::lookup_float_type("float64");
2307 type = type_to_tree(ft->get_backend(gogo));
e440a328 2308 }
2309 return Expression::float_constant_tree(this->val_, type);
2310}
2311
8b1c301d 2312// Write a floating point number to a string dump.
e440a328 2313
2314void
8b1c301d 2315Float_expression::export_float(String_dump *exp, const mpfr_t val)
e440a328 2316{
2317 mp_exp_t exponent;
2318 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2319 if (*s == '-')
2320 exp->write_c_string("-");
2321 exp->write_c_string("0.");
2322 exp->write_c_string(*s == '-' ? s + 1 : s);
2323 mpfr_free_str(s);
2324 char buf[30];
2325 snprintf(buf, sizeof buf, "E%ld", exponent);
2326 exp->write_c_string(buf);
2327}
2328
2329// Export a floating point number in a constant expression.
2330
2331void
2332Float_expression::do_export(Export* exp) const
2333{
2334 Float_expression::export_float(exp, this->val_);
2335 // A trailing space lets us reliably identify the end of the number.
2336 exp->write_c_string(" ");
2337}
2338
d751bb78 2339// Dump a floating point number to the dump file.
2340
2341void
2342Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2343{
8b1c301d 2344 Float_expression::export_float(ast_dump_context, this->val_);
d751bb78 2345}
2346
e440a328 2347// Make a float expression.
2348
2349Expression*
b13c66cd 2350Expression::make_float(const mpfr_t* val, Type* type, Location location)
e440a328 2351{
2352 return new Float_expression(val, type, location);
2353}
2354
2355// Complex numbers.
2356
2357class Complex_expression : public Expression
2358{
2359 public:
2360 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
b13c66cd 2361 Location location)
e440a328 2362 : Expression(EXPRESSION_COMPLEX, location),
2363 type_(type)
2364 {
2365 mpfr_init_set(this->real_, *real, GMP_RNDN);
2366 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2367 }
2368
2369 // Constrain REAL/IMAG to fit into TYPE.
2370 static void
2371 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2372
2373 // Return whether REAL/IMAG fits in the type.
2374 static bool
b13c66cd 2375 check_constant(mpfr_t real, mpfr_t imag, Type*, Location);
e440a328 2376
8b1c301d 2377 // Write REAL/IMAG to string dump.
e440a328 2378 static void
8b1c301d 2379 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
e440a328 2380
d751bb78 2381 // Write REAL/IMAG to dump context.
2382 static void
2383 dump_complex(Ast_dump_context* ast_dump_context,
2384 const mpfr_t real, const mpfr_t val);
2385
e440a328 2386 protected:
2387 bool
2388 do_is_constant() const
2389 { return true; }
2390
2391 bool
2392 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2393
2394 Type*
2395 do_type();
2396
2397 void
2398 do_determine_type(const Type_context*);
2399
2400 void
2401 do_check_types(Gogo*);
2402
2403 Expression*
2404 do_copy()
2405 {
2406 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2407 this->location());
2408 }
2409
2410 tree
2411 do_get_tree(Translate_context*);
2412
2413 void
2414 do_export(Export*) const;
2415
d751bb78 2416 void
2417 do_dump_expression(Ast_dump_context*) const;
2418
e440a328 2419 private:
2420 // The real part.
2421 mpfr_t real_;
2422 // The imaginary part;
2423 mpfr_t imag_;
2424 // The type if known.
2425 Type* type_;
2426};
2427
2428// Constrain REAL/IMAG to fit into TYPE.
2429
2430void
2431Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2432{
2433 Complex_type* ctype = type->complex_type();
2434 if (ctype != NULL && !ctype->is_abstract())
2435 {
2f50f88a 2436 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2437 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
e440a328 2438 }
2439}
2440
2441// Return a complex constant value.
2442
2443bool
2444Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2445 Type** ptype) const
2446{
2447 if (this->type_ != NULL)
2448 *ptype = this->type_;
2449 mpfr_set(real, this->real_, GMP_RNDN);
2450 mpfr_set(imag, this->imag_, GMP_RNDN);
2451 return true;
2452}
2453
2454// Return the current type. If we haven't set the type yet, we return
2455// an abstract complex type.
2456
2457Type*
2458Complex_expression::do_type()
2459{
2460 if (this->type_ == NULL)
2461 this->type_ = Type::make_abstract_complex_type();
2462 return this->type_;
2463}
2464
2465// Set the type of the complex value. Here we may switch from an
2466// abstract type to a real type.
2467
2468void
2469Complex_expression::do_determine_type(const Type_context* context)
2470{
2471 if (this->type_ != NULL && !this->type_->is_abstract())
2472 ;
2473 else if (context->type != NULL
2474 && context->type->complex_type() != NULL)
2475 this->type_ = context->type;
2476 else if (!context->may_be_abstract)
48080209 2477 this->type_ = Type::lookup_complex_type("complex128");
e440a328 2478}
2479
2480// Return true if the complex value REAL/IMAG fits in the range of the
2481// type TYPE. Otherwise give an error and return false. TYPE may be
2482// NULL.
2483
2484bool
2485Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
b13c66cd 2486 Location location)
e440a328 2487{
2488 if (type == NULL)
2489 return true;
2490 Complex_type* ctype = type->complex_type();
2491 if (ctype == NULL || ctype->is_abstract())
2492 return true;
2493
2494 mp_exp_t max_exp;
2495 switch (ctype->bits())
2496 {
2497 case 64:
2498 max_exp = 128;
2499 break;
2500 case 128:
2501 max_exp = 1024;
2502 break;
2503 default:
c3e6f413 2504 go_unreachable();
e440a328 2505 }
2506
2507 // A NaN or Infinity always fits in the range of the type.
2508 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2509 {
2510 if (mpfr_get_exp(real) > max_exp)
2511 {
2512 error_at(location, "complex real part constant overflow");
2513 return false;
2514 }
2515 }
2516
2517 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2518 {
2519 if (mpfr_get_exp(imag) > max_exp)
2520 {
2521 error_at(location, "complex imaginary part constant overflow");
2522 return false;
2523 }
2524 }
2525
2526 return true;
2527}
2528
2529// Check the type of a complex value.
2530
2531void
2532Complex_expression::do_check_types(Gogo*)
2533{
2534 if (this->type_ == NULL)
2535 return;
2536
2537 if (!Complex_expression::check_constant(this->real_, this->imag_,
2538 this->type_, this->location()))
2539 this->set_is_error();
2540}
2541
2542// Get a tree for a complex constant.
2543
2544tree
2545Complex_expression::do_get_tree(Translate_context* context)
2546{
2547 Gogo* gogo = context->gogo();
2548 tree type;
2549 if (this->type_ != NULL && !this->type_->is_abstract())
9f0e0513 2550 type = type_to_tree(this->type_->get_backend(gogo));
e440a328 2551 else
2552 {
2553 // If we still have an abstract type here, this this is being
2554 // used in a constant expression which didn't get reduced. We
2555 // just use complex128 and hope for the best.
9f0e0513 2556 Type* ct = Type::lookup_complex_type("complex128");
2557 type = type_to_tree(ct->get_backend(gogo));
e440a328 2558 }
2559 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2560}
2561
2562// Write REAL/IMAG to export data.
2563
2564void
8b1c301d 2565Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
e440a328 2566 const mpfr_t imag)
2567{
2568 if (!mpfr_zero_p(real))
2569 {
2570 Float_expression::export_float(exp, real);
2571 if (mpfr_sgn(imag) > 0)
2572 exp->write_c_string("+");
2573 }
2574 Float_expression::export_float(exp, imag);
2575 exp->write_c_string("i");
2576}
2577
2578// Export a complex number in a constant expression.
2579
2580void
2581Complex_expression::do_export(Export* exp) const
2582{
2583 Complex_expression::export_complex(exp, this->real_, this->imag_);
2584 // A trailing space lets us reliably identify the end of the number.
2585 exp->write_c_string(" ");
2586}
2587
d751bb78 2588// Dump a complex expression to the dump file.
2589
2590void
2591Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2592{
8b1c301d 2593 Complex_expression::export_complex(ast_dump_context,
d751bb78 2594 this->real_,
2595 this->imag_);
2596}
2597
e440a328 2598// Make a complex expression.
2599
2600Expression*
2601Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
b13c66cd 2602 Location location)
e440a328 2603{
2604 return new Complex_expression(real, imag, type, location);
2605}
2606
d5b605df 2607// Find a named object in an expression.
2608
2609class Find_named_object : public Traverse
2610{
2611 public:
2612 Find_named_object(Named_object* no)
2613 : Traverse(traverse_expressions),
2614 no_(no), found_(false)
2615 { }
2616
2617 // Whether we found the object.
2618 bool
2619 found() const
2620 { return this->found_; }
2621
2622 protected:
2623 int
2624 expression(Expression**);
2625
2626 private:
2627 // The object we are looking for.
2628 Named_object* no_;
2629 // Whether we found it.
2630 bool found_;
2631};
2632
e440a328 2633// A reference to a const in an expression.
2634
2635class Const_expression : public Expression
2636{
2637 public:
b13c66cd 2638 Const_expression(Named_object* constant, Location location)
e440a328 2639 : Expression(EXPRESSION_CONST_REFERENCE, location),
13e818f5 2640 constant_(constant), type_(NULL), seen_(false)
e440a328 2641 { }
2642
d5b605df 2643 Named_object*
2644 named_object()
2645 { return this->constant_; }
2646
a7f064d5 2647 // Check that the initializer does not refer to the constant itself.
2648 void
2649 check_for_init_loop();
2650
e440a328 2651 protected:
ba4aedd4 2652 int
2653 do_traverse(Traverse*);
2654
e440a328 2655 Expression*
ceeb4318 2656 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 2657
2658 bool
2659 do_is_constant() const
2660 { return true; }
2661
2662 bool
2663 do_integer_constant_value(bool, mpz_t val, Type**) const;
2664
2665 bool
2666 do_float_constant_value(mpfr_t val, Type**) const;
2667
2668 bool
2669 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2670
2671 bool
2672 do_string_constant_value(std::string* val) const
2673 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2674
2675 Type*
2676 do_type();
2677
2678 // The type of a const is set by the declaration, not the use.
2679 void
2680 do_determine_type(const Type_context*);
2681
2682 void
2683 do_check_types(Gogo*);
2684
2685 Expression*
2686 do_copy()
2687 { return this; }
2688
2689 tree
2690 do_get_tree(Translate_context* context);
2691
2692 // When exporting a reference to a const as part of a const
2693 // expression, we export the value. We ignore the fact that it has
2694 // a name.
2695 void
2696 do_export(Export* exp) const
2697 { this->constant_->const_value()->expr()->export_expression(exp); }
2698
d751bb78 2699 void
2700 do_dump_expression(Ast_dump_context*) const;
2701
e440a328 2702 private:
2703 // The constant.
2704 Named_object* constant_;
2705 // The type of this reference. This is used if the constant has an
2706 // abstract type.
2707 Type* type_;
13e818f5 2708 // Used to prevent infinite recursion when a constant incorrectly
2709 // refers to itself.
2710 mutable bool seen_;
e440a328 2711};
2712
ba4aedd4 2713// Traversal.
2714
2715int
2716Const_expression::do_traverse(Traverse* traverse)
2717{
2718 if (this->type_ != NULL)
2719 return Type::traverse(this->type_, traverse);
2720 return TRAVERSE_CONTINUE;
2721}
2722
e440a328 2723// Lower a constant expression. This is where we convert the
2724// predeclared constant iota into an integer value.
2725
2726Expression*
ceeb4318 2727Const_expression::do_lower(Gogo* gogo, Named_object*,
2728 Statement_inserter*, int iota_value)
e440a328 2729{
2730 if (this->constant_->const_value()->expr()->classification()
2731 == EXPRESSION_IOTA)
2732 {
2733 if (iota_value == -1)
2734 {
2735 error_at(this->location(),
2736 "iota is only defined in const declarations");
2737 iota_value = 0;
2738 }
2739 mpz_t val;
2740 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2741 Expression* ret = Expression::make_integer(&val, NULL,
2742 this->location());
2743 mpz_clear(val);
2744 return ret;
2745 }
2746
2747 // Make sure that the constant itself has been lowered.
2748 gogo->lower_constant(this->constant_);
2749
2750 return this;
2751}
2752
2753// Return an integer constant value.
2754
2755bool
2756Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2757 Type** ptype) const
2758{
13e818f5 2759 if (this->seen_)
2760 return false;
2761
e440a328 2762 Type* ctype;
2763 if (this->type_ != NULL)
2764 ctype = this->type_;
2765 else
2766 ctype = this->constant_->const_value()->type();
2767 if (ctype != NULL && ctype->integer_type() == NULL)
2768 return false;
2769
2770 Expression* e = this->constant_->const_value()->expr();
13e818f5 2771
2772 this->seen_ = true;
2773
e440a328 2774 Type* t;
2775 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2776
13e818f5 2777 this->seen_ = false;
2778
e440a328 2779 if (r
2780 && ctype != NULL
2781 && !Integer_expression::check_constant(val, ctype, this->location()))
2782 return false;
2783
2784 *ptype = ctype != NULL ? ctype : t;
2785 return r;
2786}
2787
2788// Return a floating point constant value.
2789
2790bool
2791Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2792{
13e818f5 2793 if (this->seen_)
2794 return false;
2795
e440a328 2796 Type* ctype;
2797 if (this->type_ != NULL)
2798 ctype = this->type_;
2799 else
2800 ctype = this->constant_->const_value()->type();
2801 if (ctype != NULL && ctype->float_type() == NULL)
2802 return false;
2803
13e818f5 2804 this->seen_ = true;
2805
e440a328 2806 Type* t;
2807 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2808 &t);
13e818f5 2809
2810 this->seen_ = false;
2811
e440a328 2812 if (r && ctype != NULL)
2813 {
2814 if (!Float_expression::check_constant(val, ctype, this->location()))
2815 return false;
2816 Float_expression::constrain_float(val, ctype);
2817 }
2818 *ptype = ctype != NULL ? ctype : t;
2819 return r;
2820}
2821
2822// Return a complex constant value.
2823
2824bool
2825Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2826 Type **ptype) const
2827{
13e818f5 2828 if (this->seen_)
2829 return false;
2830
e440a328 2831 Type* ctype;
2832 if (this->type_ != NULL)
2833 ctype = this->type_;
2834 else
2835 ctype = this->constant_->const_value()->type();
2836 if (ctype != NULL && ctype->complex_type() == NULL)
2837 return false;
2838
13e818f5 2839 this->seen_ = true;
2840
e440a328 2841 Type *t;
2842 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2843 imag,
2844 &t);
13e818f5 2845
2846 this->seen_ = false;
2847
e440a328 2848 if (r && ctype != NULL)
2849 {
2850 if (!Complex_expression::check_constant(real, imag, ctype,
2851 this->location()))
2852 return false;
2853 Complex_expression::constrain_complex(real, imag, ctype);
2854 }
2855 *ptype = ctype != NULL ? ctype : t;
2856 return r;
2857}
2858
2859// Return the type of the const reference.
2860
2861Type*
2862Const_expression::do_type()
2863{
2864 if (this->type_ != NULL)
2865 return this->type_;
13e818f5 2866
2f78f012 2867 Named_constant* nc = this->constant_->const_value();
2868
2869 if (this->seen_ || nc->lowering())
13e818f5 2870 {
2871 this->report_error(_("constant refers to itself"));
2872 this->type_ = Type::make_error_type();
2873 return this->type_;
2874 }
2875
2876 this->seen_ = true;
2877
e440a328 2878 Type* ret = nc->type();
13e818f5 2879
e440a328 2880 if (ret != NULL)
13e818f5 2881 {
2882 this->seen_ = false;
2883 return ret;
2884 }
2885
e440a328 2886 // During parsing, a named constant may have a NULL type, but we
2887 // must not return a NULL type here.
13e818f5 2888 ret = nc->expr()->type();
2889
2890 this->seen_ = false;
2891
2892 return ret;
e440a328 2893}
2894
2895// Set the type of the const reference.
2896
2897void
2898Const_expression::do_determine_type(const Type_context* context)
2899{
2900 Type* ctype = this->constant_->const_value()->type();
2901 Type* cetype = (ctype != NULL
2902 ? ctype
2903 : this->constant_->const_value()->expr()->type());
2904 if (ctype != NULL && !ctype->is_abstract())
2905 ;
2906 else if (context->type != NULL
2907 && (context->type->integer_type() != NULL
2908 || context->type->float_type() != NULL
2909 || context->type->complex_type() != NULL)
2910 && (cetype->integer_type() != NULL
2911 || cetype->float_type() != NULL
2912 || cetype->complex_type() != NULL))
2913 this->type_ = context->type;
2914 else if (context->type != NULL
2915 && context->type->is_string_type()
2916 && cetype->is_string_type())
2917 this->type_ = context->type;
2918 else if (context->type != NULL
2919 && context->type->is_boolean_type()
2920 && cetype->is_boolean_type())
2921 this->type_ = context->type;
2922 else if (!context->may_be_abstract)
2923 {
2924 if (cetype->is_abstract())
2925 cetype = cetype->make_non_abstract_type();
2926 this->type_ = cetype;
2927 }
2928}
2929
a7f064d5 2930// Check for a loop in which the initializer of a constant refers to
2931// the constant itself.
e440a328 2932
2933void
a7f064d5 2934Const_expression::check_for_init_loop()
e440a328 2935{
5c13bd80 2936 if (this->type_ != NULL && this->type_->is_error())
d5b605df 2937 return;
2938
a7f064d5 2939 if (this->seen_)
2940 {
2941 this->report_error(_("constant refers to itself"));
2942 this->type_ = Type::make_error_type();
2943 return;
2944 }
2945
d5b605df 2946 Expression* init = this->constant_->const_value()->expr();
2947 Find_named_object find_named_object(this->constant_);
a7f064d5 2948
2949 this->seen_ = true;
d5b605df 2950 Expression::traverse(&init, &find_named_object);
a7f064d5 2951 this->seen_ = false;
2952
d5b605df 2953 if (find_named_object.found())
2954 {
5c13bd80 2955 if (this->type_ == NULL || !this->type_->is_error())
a7f064d5 2956 {
2957 this->report_error(_("constant refers to itself"));
2958 this->type_ = Type::make_error_type();
2959 }
d5b605df 2960 return;
2961 }
a7f064d5 2962}
2963
2964// Check types of a const reference.
2965
2966void
2967Const_expression::do_check_types(Gogo*)
2968{
5c13bd80 2969 if (this->type_ != NULL && this->type_->is_error())
a7f064d5 2970 return;
2971
2972 this->check_for_init_loop();
d5b605df 2973
e440a328 2974 if (this->type_ == NULL || this->type_->is_abstract())
2975 return;
2976
2977 // Check for integer overflow.
2978 if (this->type_->integer_type() != NULL)
2979 {
2980 mpz_t ival;
2981 mpz_init(ival);
2982 Type* dummy;
2983 if (!this->integer_constant_value(true, ival, &dummy))
2984 {
2985 mpfr_t fval;
2986 mpfr_init(fval);
2987 Expression* cexpr = this->constant_->const_value()->expr();
2988 if (cexpr->float_constant_value(fval, &dummy))
2989 {
2990 if (!mpfr_integer_p(fval))
2991 this->report_error(_("floating point constant "
2992 "truncated to integer"));
2993 else
2994 {
2995 mpfr_get_z(ival, fval, GMP_RNDN);
2996 Integer_expression::check_constant(ival, this->type_,
2997 this->location());
2998 }
2999 }
3000 mpfr_clear(fval);
3001 }
3002 mpz_clear(ival);
3003 }
3004}
3005
3006// Return a tree for the const reference.
3007
3008tree
3009Const_expression::do_get_tree(Translate_context* context)
3010{
3011 Gogo* gogo = context->gogo();
3012 tree type_tree;
3013 if (this->type_ == NULL)
3014 type_tree = NULL_TREE;
3015 else
3016 {
9f0e0513 3017 type_tree = type_to_tree(this->type_->get_backend(gogo));
e440a328 3018 if (type_tree == error_mark_node)
3019 return error_mark_node;
3020 }
3021
3022 // If the type has been set for this expression, but the underlying
3023 // object is an abstract int or float, we try to get the abstract
3024 // value. Otherwise we may lose something in the conversion.
3025 if (this->type_ != NULL
a68492b4 3026 && (this->constant_->const_value()->type() == NULL
3027 || this->constant_->const_value()->type()->is_abstract()))
e440a328 3028 {
3029 Expression* expr = this->constant_->const_value()->expr();
3030 mpz_t ival;
3031 mpz_init(ival);
3032 Type* t;
3033 if (expr->integer_constant_value(true, ival, &t))
3034 {
3035 tree ret = Expression::integer_constant_tree(ival, type_tree);
3036 mpz_clear(ival);
3037 return ret;
3038 }
3039 mpz_clear(ival);
3040
3041 mpfr_t fval;
3042 mpfr_init(fval);
3043 if (expr->float_constant_value(fval, &t))
3044 {
3045 tree ret = Expression::float_constant_tree(fval, type_tree);
3046 mpfr_clear(fval);
3047 return ret;
3048 }
3049
3050 mpfr_t imag;
3051 mpfr_init(imag);
3052 if (expr->complex_constant_value(fval, imag, &t))
3053 {
3054 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
3055 mpfr_clear(fval);
3056 mpfr_clear(imag);
3057 return ret;
3058 }
3059 mpfr_clear(imag);
3060 mpfr_clear(fval);
3061 }
3062
3063 tree const_tree = this->constant_->get_tree(gogo, context->function());
3064 if (this->type_ == NULL
3065 || const_tree == error_mark_node
3066 || TREE_TYPE(const_tree) == error_mark_node)
3067 return const_tree;
3068
3069 tree ret;
3070 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
3071 ret = fold_convert(type_tree, const_tree);
3072 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
3073 ret = fold(convert_to_integer(type_tree, const_tree));
3074 else if (TREE_CODE(type_tree) == REAL_TYPE)
3075 ret = fold(convert_to_real(type_tree, const_tree));
3076 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
3077 ret = fold(convert_to_complex(type_tree, const_tree));
3078 else
c3e6f413 3079 go_unreachable();
e440a328 3080 return ret;
3081}
3082
d751bb78 3083// Dump ast representation for constant expression.
3084
3085void
3086Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
3087{
3088 ast_dump_context->ostream() << this->constant_->name();
3089}
3090
e440a328 3091// Make a reference to a constant in an expression.
3092
3093Expression*
3094Expression::make_const_reference(Named_object* constant,
b13c66cd 3095 Location location)
e440a328 3096{
3097 return new Const_expression(constant, location);
3098}
3099
d5b605df 3100// Find a named object in an expression.
3101
3102int
3103Find_named_object::expression(Expression** pexpr)
3104{
3105 switch ((*pexpr)->classification())
3106 {
3107 case Expression::EXPRESSION_CONST_REFERENCE:
a7f064d5 3108 {
3109 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3110 if (ce->named_object() == this->no_)
3111 break;
3112
3113 // We need to check a constant initializer explicitly, as
3114 // loops here will not be caught by the loop checking for
3115 // variable initializers.
3116 ce->check_for_init_loop();
3117
3118 return TRAVERSE_CONTINUE;
3119 }
3120
d5b605df 3121 case Expression::EXPRESSION_VAR_REFERENCE:
3122 if ((*pexpr)->var_expression()->named_object() == this->no_)
3123 break;
3124 return TRAVERSE_CONTINUE;
3125 case Expression::EXPRESSION_FUNC_REFERENCE:
3126 if ((*pexpr)->func_expression()->named_object() == this->no_)
3127 break;
3128 return TRAVERSE_CONTINUE;
3129 default:
3130 return TRAVERSE_CONTINUE;
3131 }
3132 this->found_ = true;
3133 return TRAVERSE_EXIT;
3134}
3135
e440a328 3136// The nil value.
3137
3138class Nil_expression : public Expression
3139{
3140 public:
b13c66cd 3141 Nil_expression(Location location)
e440a328 3142 : Expression(EXPRESSION_NIL, location)
3143 { }
3144
3145 static Expression*
3146 do_import(Import*);
3147
3148 protected:
3149 bool
3150 do_is_constant() const
3151 { return true; }
3152
3153 Type*
3154 do_type()
3155 { return Type::make_nil_type(); }
3156
3157 void
3158 do_determine_type(const Type_context*)
3159 { }
3160
3161 Expression*
3162 do_copy()
3163 { return this; }
3164
3165 tree
3166 do_get_tree(Translate_context*)
3167 { return null_pointer_node; }
3168
3169 void
3170 do_export(Export* exp) const
3171 { exp->write_c_string("nil"); }
d751bb78 3172
3173 void
3174 do_dump_expression(Ast_dump_context* ast_dump_context) const
3175 { ast_dump_context->ostream() << "nil"; }
e440a328 3176};
3177
3178// Import a nil expression.
3179
3180Expression*
3181Nil_expression::do_import(Import* imp)
3182{
3183 imp->require_c_string("nil");
3184 return Expression::make_nil(imp->location());
3185}
3186
3187// Make a nil expression.
3188
3189Expression*
b13c66cd 3190Expression::make_nil(Location location)
e440a328 3191{
3192 return new Nil_expression(location);
3193}
3194
3195// The value of the predeclared constant iota. This is little more
3196// than a marker. This will be lowered to an integer in
3197// Const_expression::do_lower, which is where we know the value that
3198// it should have.
3199
3200class Iota_expression : public Parser_expression
3201{
3202 public:
b13c66cd 3203 Iota_expression(Location location)
e440a328 3204 : Parser_expression(EXPRESSION_IOTA, location)
3205 { }
3206
3207 protected:
3208 Expression*
ceeb4318 3209 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
c3e6f413 3210 { go_unreachable(); }
e440a328 3211
3212 // There should only ever be one of these.
3213 Expression*
3214 do_copy()
c3e6f413 3215 { go_unreachable(); }
d751bb78 3216
3217 void
3218 do_dump_expression(Ast_dump_context* ast_dump_context) const
3219 { ast_dump_context->ostream() << "iota"; }
e440a328 3220};
3221
3222// Make an iota expression. This is only called for one case: the
3223// value of the predeclared constant iota.
3224
3225Expression*
3226Expression::make_iota()
3227{
b13c66cd 3228 static Iota_expression iota_expression(Linemap::unknown_location());
e440a328 3229 return &iota_expression;
3230}
3231
3232// A type conversion expression.
3233
3234class Type_conversion_expression : public Expression
3235{
3236 public:
3237 Type_conversion_expression(Type* type, Expression* expr,
b13c66cd 3238 Location location)
e440a328 3239 : Expression(EXPRESSION_CONVERSION, location),
3240 type_(type), expr_(expr), may_convert_function_types_(false)
3241 { }
3242
3243 // Return the type to which we are converting.
3244 Type*
3245 type() const
3246 { return this->type_; }
3247
3248 // Return the expression which we are converting.
3249 Expression*
3250 expr() const
3251 { return this->expr_; }
3252
3253 // Permit converting from one function type to another. This is
3254 // used internally for method expressions.
3255 void
3256 set_may_convert_function_types()
3257 {
3258 this->may_convert_function_types_ = true;
3259 }
3260
3261 // Import a type conversion expression.
3262 static Expression*
3263 do_import(Import*);
3264
3265 protected:
3266 int
3267 do_traverse(Traverse* traverse);
3268
3269 Expression*
ceeb4318 3270 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 3271
3272 bool
3273 do_is_constant() const
3274 { return this->expr_->is_constant(); }
3275
3276 bool
3277 do_integer_constant_value(bool, mpz_t, Type**) const;
3278
3279 bool
3280 do_float_constant_value(mpfr_t, Type**) const;
3281
3282 bool
3283 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3284
3285 bool
3286 do_string_constant_value(std::string*) const;
3287
3288 Type*
3289 do_type()
3290 { return this->type_; }
3291
3292 void
3293 do_determine_type(const Type_context*)
3294 {
3295 Type_context subcontext(this->type_, false);
3296 this->expr_->determine_type(&subcontext);
3297 }
3298
3299 void
3300 do_check_types(Gogo*);
3301
3302 Expression*
3303 do_copy()
3304 {
3305 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3306 this->location());
3307 }
3308
3309 tree
3310 do_get_tree(Translate_context* context);
3311
3312 void
3313 do_export(Export*) const;
3314
d751bb78 3315 void
3316 do_dump_expression(Ast_dump_context*) const;
3317
e440a328 3318 private:
3319 // The type to convert to.
3320 Type* type_;
3321 // The expression to convert.
3322 Expression* expr_;
3323 // True if this is permitted to convert function types. This is
3324 // used internally for method expressions.
3325 bool may_convert_function_types_;
3326};
3327
3328// Traversal.
3329
3330int
3331Type_conversion_expression::do_traverse(Traverse* traverse)
3332{
3333 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3334 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3335 return TRAVERSE_EXIT;
3336 return TRAVERSE_CONTINUE;
3337}
3338
3339// Convert to a constant at lowering time.
3340
3341Expression*
ceeb4318 3342Type_conversion_expression::do_lower(Gogo*, Named_object*,
3343 Statement_inserter*, int)
e440a328 3344{
3345 Type* type = this->type_;
3346 Expression* val = this->expr_;
b13c66cd 3347 Location location = this->location();
e440a328 3348
3349 if (type->integer_type() != NULL)
3350 {
3351 mpz_t ival;
3352 mpz_init(ival);
3353 Type* dummy;
3354 if (val->integer_constant_value(false, ival, &dummy))
3355 {
3356 if (!Integer_expression::check_constant(ival, type, location))
3357 mpz_set_ui(ival, 0);
3358 Expression* ret = Expression::make_integer(&ival, type, location);
3359 mpz_clear(ival);
3360 return ret;
3361 }
3362
3363 mpfr_t fval;
3364 mpfr_init(fval);
3365 if (val->float_constant_value(fval, &dummy))
3366 {
3367 if (!mpfr_integer_p(fval))
3368 {
3369 error_at(location,
3370 "floating point constant truncated to integer");
3371 return Expression::make_error(location);
3372 }
3373 mpfr_get_z(ival, fval, GMP_RNDN);
3374 if (!Integer_expression::check_constant(ival, type, location))
3375 mpz_set_ui(ival, 0);
3376 Expression* ret = Expression::make_integer(&ival, type, location);
3377 mpfr_clear(fval);
3378 mpz_clear(ival);
3379 return ret;
3380 }
3381 mpfr_clear(fval);
3382 mpz_clear(ival);
3383 }
3384
3385 if (type->float_type() != NULL)
3386 {
3387 mpfr_t fval;
3388 mpfr_init(fval);
3389 Type* dummy;
3390 if (val->float_constant_value(fval, &dummy))
3391 {
3392 if (!Float_expression::check_constant(fval, type, location))
3393 mpfr_set_ui(fval, 0, GMP_RNDN);
3394 Float_expression::constrain_float(fval, type);
3395 Expression *ret = Expression::make_float(&fval, type, location);
3396 mpfr_clear(fval);
3397 return ret;
3398 }
3399 mpfr_clear(fval);
3400 }
3401
3402 if (type->complex_type() != NULL)
3403 {
3404 mpfr_t real;
3405 mpfr_t imag;
3406 mpfr_init(real);
3407 mpfr_init(imag);
3408 Type* dummy;
3409 if (val->complex_constant_value(real, imag, &dummy))
3410 {
3411 if (!Complex_expression::check_constant(real, imag, type, location))
3412 {
3413 mpfr_set_ui(real, 0, GMP_RNDN);
3414 mpfr_set_ui(imag, 0, GMP_RNDN);
3415 }
3416 Complex_expression::constrain_complex(real, imag, type);
3417 Expression* ret = Expression::make_complex(&real, &imag, type,
3418 location);
3419 mpfr_clear(real);
3420 mpfr_clear(imag);
3421 return ret;
3422 }
3423 mpfr_clear(real);
3424 mpfr_clear(imag);
3425 }
3426
55072f2b 3427 if (type->is_slice_type())
e440a328 3428 {
3429 Type* element_type = type->array_type()->element_type()->forwarded();
60963afd 3430 bool is_byte = (element_type->integer_type() != NULL
3431 && element_type->integer_type()->is_byte());
3432 bool is_rune = (element_type->integer_type() != NULL
3433 && element_type->integer_type()->is_rune());
3434 if (is_byte || is_rune)
e440a328 3435 {
3436 std::string s;
3437 if (val->string_constant_value(&s))
3438 {
3439 Expression_list* vals = new Expression_list();
3440 if (is_byte)
3441 {
3442 for (std::string::const_iterator p = s.begin();
3443 p != s.end();
3444 p++)
3445 {
3446 mpz_t val;
3447 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3448 Expression* v = Expression::make_integer(&val,
3449 element_type,
3450 location);
3451 vals->push_back(v);
3452 mpz_clear(val);
3453 }
3454 }
3455 else
3456 {
3457 const char *p = s.data();
3458 const char *pend = s.data() + s.length();
3459 while (p < pend)
3460 {
3461 unsigned int c;
3462 int adv = Lex::fetch_char(p, &c);
3463 if (adv == 0)
3464 {
3465 warning_at(this->location(), 0,
3466 "invalid UTF-8 encoding");
3467 adv = 1;
3468 }
3469 p += adv;
3470 mpz_t val;
3471 mpz_init_set_ui(val, c);
3472 Expression* v = Expression::make_integer(&val,
3473 element_type,
3474 location);
3475 vals->push_back(v);
3476 mpz_clear(val);
3477 }
3478 }
3479
3480 return Expression::make_slice_composite_literal(type, vals,
3481 location);
3482 }
3483 }
3484 }
3485
3486 return this;
3487}
3488
3489// Return the constant integer value if there is one.
3490
3491bool
3492Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3493 mpz_t val,
3494 Type** ptype) const
3495{
3496 if (this->type_->integer_type() == NULL)
3497 return false;
3498
3499 mpz_t ival;
3500 mpz_init(ival);
3501 Type* dummy;
3502 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3503 {
3504 if (!Integer_expression::check_constant(ival, this->type_,
3505 this->location()))
3506 {
3507 mpz_clear(ival);
3508 return false;
3509 }
3510 mpz_set(val, ival);
3511 mpz_clear(ival);
3512 *ptype = this->type_;
3513 return true;
3514 }
3515 mpz_clear(ival);
3516
3517 mpfr_t fval;
3518 mpfr_init(fval);
3519 if (this->expr_->float_constant_value(fval, &dummy))
3520 {
3521 mpfr_get_z(val, fval, GMP_RNDN);
3522 mpfr_clear(fval);
3523 if (!Integer_expression::check_constant(val, this->type_,
3524 this->location()))
3525 return false;
3526 *ptype = this->type_;
3527 return true;
3528 }
3529 mpfr_clear(fval);
3530
3531 return false;
3532}
3533
3534// Return the constant floating point value if there is one.
3535
3536bool
3537Type_conversion_expression::do_float_constant_value(mpfr_t val,
3538 Type** ptype) const
3539{
3540 if (this->type_->float_type() == NULL)
3541 return false;
3542
3543 mpfr_t fval;
3544 mpfr_init(fval);
3545 Type* dummy;
3546 if (this->expr_->float_constant_value(fval, &dummy))
3547 {
3548 if (!Float_expression::check_constant(fval, this->type_,
3549 this->location()))
3550 {
3551 mpfr_clear(fval);
3552 return false;
3553 }
3554 mpfr_set(val, fval, GMP_RNDN);
3555 mpfr_clear(fval);
3556 Float_expression::constrain_float(val, this->type_);
3557 *ptype = this->type_;
3558 return true;
3559 }
3560 mpfr_clear(fval);
3561
3562 return false;
3563}
3564
3565// Return the constant complex value if there is one.
3566
3567bool
3568Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3569 mpfr_t imag,
3570 Type **ptype) const
3571{
3572 if (this->type_->complex_type() == NULL)
3573 return false;
3574
3575 mpfr_t rval;
3576 mpfr_t ival;
3577 mpfr_init(rval);
3578 mpfr_init(ival);
3579 Type* dummy;
3580 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3581 {
3582 if (!Complex_expression::check_constant(rval, ival, this->type_,
3583 this->location()))
3584 {
3585 mpfr_clear(rval);
3586 mpfr_clear(ival);
3587 return false;
3588 }
3589 mpfr_set(real, rval, GMP_RNDN);
3590 mpfr_set(imag, ival, GMP_RNDN);
3591 mpfr_clear(rval);
3592 mpfr_clear(ival);
3593 Complex_expression::constrain_complex(real, imag, this->type_);
3594 *ptype = this->type_;
3595 return true;
3596 }
3597 mpfr_clear(rval);
3598 mpfr_clear(ival);
3599
3600 return false;
3601}
3602
3603// Return the constant string value if there is one.
3604
3605bool
3606Type_conversion_expression::do_string_constant_value(std::string* val) const
3607{
3608 if (this->type_->is_string_type()
3609 && this->expr_->type()->integer_type() != NULL)
3610 {
3611 mpz_t ival;
3612 mpz_init(ival);
3613 Type* dummy;
3614 if (this->expr_->integer_constant_value(false, ival, &dummy))
3615 {
3616 unsigned long ulval = mpz_get_ui(ival);
3617 if (mpz_cmp_ui(ival, ulval) == 0)
3618 {
3619 Lex::append_char(ulval, true, val, this->location());
3620 mpz_clear(ival);
3621 return true;
3622 }
3623 }
3624 mpz_clear(ival);
3625 }
3626
3627 // FIXME: Could handle conversion from const []int here.
3628
3629 return false;
3630}
3631
3632// Check that types are convertible.
3633
3634void
3635Type_conversion_expression::do_check_types(Gogo*)
3636{
3637 Type* type = this->type_;
3638 Type* expr_type = this->expr_->type();
3639 std::string reason;
3640
5c13bd80 3641 if (type->is_error() || expr_type->is_error())
842f6425 3642 {
842f6425 3643 this->set_is_error();
3644 return;
3645 }
3646
e440a328 3647 if (this->may_convert_function_types_
3648 && type->function_type() != NULL
3649 && expr_type->function_type() != NULL)
3650 return;
3651
3652 if (Type::are_convertible(type, expr_type, &reason))
3653 return;
3654
3655 error_at(this->location(), "%s", reason.c_str());
3656 this->set_is_error();
3657}
3658
3659// Get a tree for a type conversion.
3660
3661tree
3662Type_conversion_expression::do_get_tree(Translate_context* context)
3663{
3664 Gogo* gogo = context->gogo();
9f0e0513 3665 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
e440a328 3666 tree expr_tree = this->expr_->get_tree(context);
3667
3668 if (type_tree == error_mark_node
3669 || expr_tree == error_mark_node
3670 || TREE_TYPE(expr_tree) == error_mark_node)
3671 return error_mark_node;
3672
3673 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3674 return fold_convert(type_tree, expr_tree);
3675
3676 Type* type = this->type_;
3677 Type* expr_type = this->expr_->type();
3678 tree ret;
3679 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3680 ret = Expression::convert_for_assignment(context, type, expr_type,
3681 expr_tree, this->location());
3682 else if (type->integer_type() != NULL)
3683 {
3684 if (expr_type->integer_type() != NULL
3685 || expr_type->float_type() != NULL
3686 || expr_type->is_unsafe_pointer_type())
3687 ret = fold(convert_to_integer(type_tree, expr_tree));
3688 else
c3e6f413 3689 go_unreachable();
e440a328 3690 }
3691 else if (type->float_type() != NULL)
3692 {
3693 if (expr_type->integer_type() != NULL
3694 || expr_type->float_type() != NULL)
3695 ret = fold(convert_to_real(type_tree, expr_tree));
3696 else
c3e6f413 3697 go_unreachable();
e440a328 3698 }
3699 else if (type->complex_type() != NULL)
3700 {
3701 if (expr_type->complex_type() != NULL)
3702 ret = fold(convert_to_complex(type_tree, expr_tree));
3703 else
c3e6f413 3704 go_unreachable();
e440a328 3705 }
3706 else if (type->is_string_type()
3707 && expr_type->integer_type() != NULL)
3708 {
3709 expr_tree = fold_convert(integer_type_node, expr_tree);
3710 if (host_integerp(expr_tree, 0))
3711 {
3712 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3713 std::string s;
3714 Lex::append_char(intval, true, &s, this->location());
3715 Expression* se = Expression::make_string(s, this->location());
3716 return se->get_tree(context);
3717 }
3718
3719 static tree int_to_string_fndecl;
3720 ret = Gogo::call_builtin(&int_to_string_fndecl,
3721 this->location(),
3722 "__go_int_to_string",
3723 1,
3724 type_tree,
3725 integer_type_node,
3726 fold_convert(integer_type_node, expr_tree));
3727 }
55072f2b 3728 else if (type->is_string_type() && expr_type->is_slice_type())
e440a328 3729 {
e440a328 3730 if (!DECL_P(expr_tree))
3731 expr_tree = save_expr(expr_tree);
55072f2b 3732 Array_type* a = expr_type->array_type();
e440a328 3733 Type* e = a->element_type()->forwarded();
c484d925 3734 go_assert(e->integer_type() != NULL);
e440a328 3735 tree valptr = fold_convert(const_ptr_type_node,
3736 a->value_pointer_tree(gogo, expr_tree));
3737 tree len = a->length_tree(gogo, expr_tree);
b13c66cd 3738 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3739 len);
60963afd 3740 if (e->integer_type()->is_byte())
e440a328 3741 {
3742 static tree byte_array_to_string_fndecl;
3743 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3744 this->location(),
3745 "__go_byte_array_to_string",
3746 2,
3747 type_tree,
3748 const_ptr_type_node,
3749 valptr,
9581e91d 3750 integer_type_node,
e440a328 3751 len);
3752 }
3753 else
3754 {
60963afd 3755 go_assert(e->integer_type()->is_rune());
e440a328 3756 static tree int_array_to_string_fndecl;
3757 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3758 this->location(),
3759 "__go_int_array_to_string",
3760 2,
3761 type_tree,
3762 const_ptr_type_node,
3763 valptr,
9581e91d 3764 integer_type_node,
e440a328 3765 len);
3766 }
3767 }
411eb89e 3768 else if (type->is_slice_type() && expr_type->is_string_type())
e440a328 3769 {
3770 Type* e = type->array_type()->element_type()->forwarded();
c484d925 3771 go_assert(e->integer_type() != NULL);
60963afd 3772 if (e->integer_type()->is_byte())
e440a328 3773 {
ef43e66c 3774 tree string_to_byte_array_fndecl = NULL_TREE;
e440a328 3775 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3776 this->location(),
3777 "__go_string_to_byte_array",
3778 1,
3779 type_tree,
3780 TREE_TYPE(expr_tree),
3781 expr_tree);
3782 }
3783 else
3784 {
60963afd 3785 go_assert(e->integer_type()->is_rune());
ef43e66c 3786 tree string_to_int_array_fndecl = NULL_TREE;
e440a328 3787 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3788 this->location(),
3789 "__go_string_to_int_array",
3790 1,
3791 type_tree,
3792 TREE_TYPE(expr_tree),
3793 expr_tree);
3794 }
3795 }
3796 else if ((type->is_unsafe_pointer_type()
3797 && expr_type->points_to() != NULL)
3798 || (expr_type->is_unsafe_pointer_type()
3799 && type->points_to() != NULL))
3800 ret = fold_convert(type_tree, expr_tree);
3801 else if (type->is_unsafe_pointer_type()
3802 && expr_type->integer_type() != NULL)
3803 ret = convert_to_pointer(type_tree, expr_tree);
3804 else if (this->may_convert_function_types_
3805 && type->function_type() != NULL
3806 && expr_type->function_type() != NULL)
b13c66cd 3807 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3808 expr_tree);
e440a328 3809 else
3810 ret = Expression::convert_for_assignment(context, type, expr_type,
3811 expr_tree, this->location());
3812
3813 return ret;
3814}
3815
3816// Output a type conversion in a constant expression.
3817
3818void
3819Type_conversion_expression::do_export(Export* exp) const
3820{
3821 exp->write_c_string("convert(");
3822 exp->write_type(this->type_);
3823 exp->write_c_string(", ");
3824 this->expr_->export_expression(exp);
3825 exp->write_c_string(")");
3826}
3827
3828// Import a type conversion or a struct construction.
3829
3830Expression*
3831Type_conversion_expression::do_import(Import* imp)
3832{
3833 imp->require_c_string("convert(");
3834 Type* type = imp->read_type();
3835 imp->require_c_string(", ");
3836 Expression* val = Expression::import_expression(imp);
3837 imp->require_c_string(")");
3838 return Expression::make_cast(type, val, imp->location());
3839}
3840
d751bb78 3841// Dump ast representation for a type conversion expression.
3842
3843void
3844Type_conversion_expression::do_dump_expression(
3845 Ast_dump_context* ast_dump_context) const
3846{
3847 ast_dump_context->dump_type(this->type_);
3848 ast_dump_context->ostream() << "(";
3849 ast_dump_context->dump_expression(this->expr_);
3850 ast_dump_context->ostream() << ") ";
3851}
3852
e440a328 3853// Make a type cast expression.
3854
3855Expression*
b13c66cd 3856Expression::make_cast(Type* type, Expression* val, Location location)
e440a328 3857{
3858 if (type->is_error_type() || val->is_error_expression())
3859 return Expression::make_error(location);
3860 return new Type_conversion_expression(type, val, location);
3861}
3862
9581e91d 3863// An unsafe type conversion, used to pass values to builtin functions.
3864
3865class Unsafe_type_conversion_expression : public Expression
3866{
3867 public:
3868 Unsafe_type_conversion_expression(Type* type, Expression* expr,
b13c66cd 3869 Location location)
9581e91d 3870 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3871 type_(type), expr_(expr)
3872 { }
3873
3874 protected:
3875 int
3876 do_traverse(Traverse* traverse);
3877
3878 Type*
3879 do_type()
3880 { return this->type_; }
3881
3882 void
3883 do_determine_type(const Type_context*)
a9182619 3884 { this->expr_->determine_type_no_context(); }
9581e91d 3885
3886 Expression*
3887 do_copy()
3888 {
3889 return new Unsafe_type_conversion_expression(this->type_,
3890 this->expr_->copy(),
3891 this->location());
3892 }
3893
3894 tree
3895 do_get_tree(Translate_context*);
3896
d751bb78 3897 void
3898 do_dump_expression(Ast_dump_context*) const;
3899
9581e91d 3900 private:
3901 // The type to convert to.
3902 Type* type_;
3903 // The expression to convert.
3904 Expression* expr_;
3905};
3906
3907// Traversal.
3908
3909int
3910Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3911{
3912 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3913 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3914 return TRAVERSE_EXIT;
3915 return TRAVERSE_CONTINUE;
3916}
3917
3918// Convert to backend representation.
3919
3920tree
3921Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3922{
3923 // We are only called for a limited number of cases.
3924
3925 Type* t = this->type_;
3926 Type* et = this->expr_->type();
3927
9f0e0513 3928 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
9581e91d 3929 tree expr_tree = this->expr_->get_tree(context);
3930 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3931 return error_mark_node;
3932
b13c66cd 3933 Location loc = this->location();
9581e91d 3934
3935 bool use_view_convert = false;
411eb89e 3936 if (t->is_slice_type())
9581e91d 3937 {
411eb89e 3938 go_assert(et->is_slice_type());
9581e91d 3939 use_view_convert = true;
3940 }
3941 else if (t->map_type() != NULL)
c484d925 3942 go_assert(et->map_type() != NULL);
9581e91d 3943 else if (t->channel_type() != NULL)
c484d925 3944 go_assert(et->channel_type() != NULL);
9581e91d 3945 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
c484d925 3946 go_assert((et->points_to() != NULL
de0e0814 3947 && et->points_to()->channel_type() != NULL)
3948 || et->is_nil_type());
09ea332d 3949 else if (t->points_to() != NULL)
c484d925 3950 go_assert(et->points_to() != NULL || et->is_nil_type());
9581e91d 3951 else if (et->is_unsafe_pointer_type())
c484d925 3952 go_assert(t->points_to() != NULL);
9581e91d 3953 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3954 {
c484d925 3955 go_assert(et->interface_type() != NULL
9581e91d 3956 && !et->interface_type()->is_empty());
3957 use_view_convert = true;
3958 }
3959 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3960 {
c484d925 3961 go_assert(et->interface_type() != NULL
9581e91d 3962 && et->interface_type()->is_empty());
3963 use_view_convert = true;
3964 }
588e3cf9 3965 else if (t->integer_type() != NULL)
3966 {
c484d925 3967 go_assert(et->is_boolean_type()
588e3cf9 3968 || et->integer_type() != NULL
3969 || et->function_type() != NULL
3970 || et->points_to() != NULL
3971 || et->map_type() != NULL
3972 || et->channel_type() != NULL);
3973 return convert_to_integer(type_tree, expr_tree);
3974 }
9581e91d 3975 else
c3e6f413 3976 go_unreachable();
9581e91d 3977
3978 if (use_view_convert)
b13c66cd 3979 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3980 expr_tree);
9581e91d 3981 else
b13c66cd 3982 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
9581e91d 3983}
3984
d751bb78 3985// Dump ast representation for an unsafe type conversion expression.
3986
3987void
3988Unsafe_type_conversion_expression::do_dump_expression(
3989 Ast_dump_context* ast_dump_context) const
3990{
3991 ast_dump_context->dump_type(this->type_);
3992 ast_dump_context->ostream() << "(";
3993 ast_dump_context->dump_expression(this->expr_);
3994 ast_dump_context->ostream() << ") ";
3995}
3996
9581e91d 3997// Make an unsafe type conversion expression.
3998
3999Expression*
4000Expression::make_unsafe_cast(Type* type, Expression* expr,
b13c66cd 4001 Location location)
9581e91d 4002{
4003 return new Unsafe_type_conversion_expression(type, expr, location);
4004}
4005
e440a328 4006// Unary expressions.
4007
4008class Unary_expression : public Expression
4009{
4010 public:
b13c66cd 4011 Unary_expression(Operator op, Expression* expr, Location location)
e440a328 4012 : Expression(EXPRESSION_UNARY, location),
09ea332d 4013 op_(op), escapes_(true), create_temp_(false), expr_(expr)
e440a328 4014 { }
4015
4016 // Return the operator.
4017 Operator
4018 op() const
4019 { return this->op_; }
4020
4021 // Return the operand.
4022 Expression*
4023 operand() const
4024 { return this->expr_; }
4025
4026 // Record that an address expression does not escape.
4027 void
4028 set_does_not_escape()
4029 {
c484d925 4030 go_assert(this->op_ == OPERATOR_AND);
e440a328 4031 this->escapes_ = false;
4032 }
4033
09ea332d 4034 // Record that this is an address expression which should create a
4035 // temporary variable if necessary. This is used for method calls.
4036 void
4037 set_create_temp()
4038 {
4039 go_assert(this->op_ == OPERATOR_AND);
4040 this->create_temp_ = true;
4041 }
4042
e440a328 4043 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4044 // could be done, false if not.
4045 static bool
4046 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
b13c66cd 4047 Location);
e440a328 4048
4049 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4050 // could be done, false if not.
4051 static bool
4052 eval_float(Operator op, mpfr_t uval, mpfr_t val);
4053
4054 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
4055 // true if this could be done, false if not.
4056 static bool
4057 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
4058 mpfr_t imag);
4059
4060 static Expression*
4061 do_import(Import*);
4062
4063 protected:
4064 int
4065 do_traverse(Traverse* traverse)
4066 { return Expression::traverse(&this->expr_, traverse); }
4067
4068 Expression*
ceeb4318 4069 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 4070
4071 bool
4072 do_is_constant() const;
4073
4074 bool
4075 do_integer_constant_value(bool, mpz_t, Type**) const;
4076
4077 bool
4078 do_float_constant_value(mpfr_t, Type**) const;
4079
4080 bool
4081 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
4082
4083 Type*
4084 do_type();
4085
4086 void
4087 do_determine_type(const Type_context*);
4088
4089 void
4090 do_check_types(Gogo*);
4091
4092 Expression*
4093 do_copy()
4094 {
4095 return Expression::make_unary(this->op_, this->expr_->copy(),
4096 this->location());
4097 }
4098
baef9f7a 4099 bool
4100 do_must_eval_subexpressions_in_order(int*) const
4101 { return this->op_ == OPERATOR_MULT; }
4102
e440a328 4103 bool
4104 do_is_addressable() const
4105 { return this->op_ == OPERATOR_MULT; }
4106
4107 tree
4108 do_get_tree(Translate_context*);
4109
4110 void
4111 do_export(Export*) const;
4112
d751bb78 4113 void
4114 do_dump_expression(Ast_dump_context*) const;
4115
e440a328 4116 private:
4117 // The unary operator to apply.
4118 Operator op_;
4119 // Normally true. False if this is an address expression which does
4120 // not escape the current function.
4121 bool escapes_;
09ea332d 4122 // True if this is an address expression which should create a
4123 // temporary variable if necessary.
4124 bool create_temp_;
e440a328 4125 // The operand.
4126 Expression* expr_;
4127};
4128
4129// If we are taking the address of a composite literal, and the
4130// contents are not constant, then we want to make a heap composite
4131// instead.
4132
4133Expression*
ceeb4318 4134Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 4135{
b13c66cd 4136 Location loc = this->location();
e440a328 4137 Operator op = this->op_;
4138 Expression* expr = this->expr_;
4139
4140 if (op == OPERATOR_MULT && expr->is_type_expression())
4141 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4142
4143 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4144 // moving x to the heap. FIXME: Is it worth doing a real escape
4145 // analysis here? This case is found in math/unsafe.go and is
4146 // therefore worth special casing.
4147 if (op == OPERATOR_MULT)
4148 {
4149 Expression* e = expr;
4150 while (e->classification() == EXPRESSION_CONVERSION)
4151 {
4152 Type_conversion_expression* te
4153 = static_cast<Type_conversion_expression*>(e);
4154 e = te->expr();
4155 }
4156
4157 if (e->classification() == EXPRESSION_UNARY)
4158 {
4159 Unary_expression* ue = static_cast<Unary_expression*>(e);
4160 if (ue->op_ == OPERATOR_AND)
4161 {
4162 if (e == expr)
4163 {
4164 // *&x == x.
4165 return ue->expr_;
4166 }
4167 ue->set_does_not_escape();
4168 }
4169 }
4170 }
4171
55661ce9 4172 // Catching an invalid indirection of unsafe.Pointer here avoid
4173 // having to deal with TYPE_VOID in other places.
4174 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4175 {
4176 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4177 return Expression::make_error(this->location());
4178 }
4179
e440a328 4180 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4181 || op == OPERATOR_NOT || op == OPERATOR_XOR)
4182 {
4183 Expression* ret = NULL;
4184
4185 mpz_t eval;
4186 mpz_init(eval);
4187 Type* etype;
4188 if (expr->integer_constant_value(false, eval, &etype))
4189 {
4190 mpz_t val;
4191 mpz_init(val);
4192 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4193 ret = Expression::make_integer(&val, etype, loc);
4194 mpz_clear(val);
4195 }
4196 mpz_clear(eval);
4197 if (ret != NULL)
4198 return ret;
4199
4200 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4201 {
4202 mpfr_t fval;
4203 mpfr_init(fval);
4204 Type* ftype;
4205 if (expr->float_constant_value(fval, &ftype))
4206 {
4207 mpfr_t val;
4208 mpfr_init(val);
4209 if (Unary_expression::eval_float(op, fval, val))
4210 ret = Expression::make_float(&val, ftype, loc);
4211 mpfr_clear(val);
4212 }
4213 if (ret != NULL)
4214 {
4215 mpfr_clear(fval);
4216 return ret;
4217 }
4218
4219 mpfr_t ival;
4220 mpfr_init(ival);
4221 if (expr->complex_constant_value(fval, ival, &ftype))
4222 {
4223 mpfr_t real;
4224 mpfr_t imag;
4225 mpfr_init(real);
4226 mpfr_init(imag);
4227 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
4228 ret = Expression::make_complex(&real, &imag, ftype, loc);
4229 mpfr_clear(real);
4230 mpfr_clear(imag);
4231 }
4232 mpfr_clear(ival);
4233 mpfr_clear(fval);
4234 if (ret != NULL)
4235 return ret;
4236 }
4237 }
4238
4239 return this;
4240}
4241
4242// Return whether a unary expression is a constant.
4243
4244bool
4245Unary_expression::do_is_constant() const
4246{
4247 if (this->op_ == OPERATOR_MULT)
4248 {
4249 // Indirecting through a pointer is only constant if the object
4250 // to which the expression points is constant, but we currently
4251 // have no way to determine that.
4252 return false;
4253 }
4254 else if (this->op_ == OPERATOR_AND)
4255 {
4256 // Taking the address of a variable is constant if it is a
4257 // global variable, not constant otherwise. In other cases
4258 // taking the address is probably not a constant.
4259 Var_expression* ve = this->expr_->var_expression();
4260 if (ve != NULL)
4261 {
4262 Named_object* no = ve->named_object();
4263 return no->is_variable() && no->var_value()->is_global();
4264 }
4265 return false;
4266 }
4267 else
4268 return this->expr_->is_constant();
4269}
4270
4271// Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
4272// UVAL, if known; it may be NULL. Return true if this could be done,
4273// false if not.
4274
4275bool
4276Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
b13c66cd 4277 Location location)
e440a328 4278{
4279 switch (op)
4280 {
4281 case OPERATOR_PLUS:
4282 mpz_set(val, uval);
4283 return true;
4284 case OPERATOR_MINUS:
4285 mpz_neg(val, uval);
4286 return Integer_expression::check_constant(val, utype, location);
4287 case OPERATOR_NOT:
4288 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4289 return true;
4290 case OPERATOR_XOR:
4291 if (utype == NULL
4292 || utype->integer_type() == NULL
4293 || utype->integer_type()->is_abstract())
4294 mpz_com(val, uval);
4295 else
4296 {
4297 // The number of HOST_WIDE_INTs that it takes to represent
4298 // UVAL.
4299 size_t count = ((mpz_sizeinbase(uval, 2)
4300 + HOST_BITS_PER_WIDE_INT
4301 - 1)
4302 / HOST_BITS_PER_WIDE_INT);
4303
4304 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4305 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4306
4307 size_t ecount;
4308 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
c484d925 4309 go_assert(ecount <= count);
e440a328 4310
4311 // Trim down to the number of words required by the type.
4312 size_t obits = utype->integer_type()->bits();
4313 if (!utype->integer_type()->is_unsigned())
4314 ++obits;
4315 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4316 / HOST_BITS_PER_WIDE_INT);
c484d925 4317 go_assert(ocount <= count);
e440a328 4318
4319 for (size_t i = 0; i < ocount; ++i)
4320 phwi[i] = ~phwi[i];
4321
4322 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4323 if (clearbits != 0)
4324 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4325 >> clearbits);
4326
4327 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4328
4329 delete[] phwi;
4330 }
4331 return Integer_expression::check_constant(val, utype, location);
4332 case OPERATOR_AND:
4333 case OPERATOR_MULT:
4334 return false;
4335 default:
c3e6f413 4336 go_unreachable();
e440a328 4337 }
4338}
4339
4340// Apply unary opcode OP to UVAL, setting VAL. Return true if this
4341// could be done, false if not.
4342
4343bool
4344Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
4345{
4346 switch (op)
4347 {
4348 case OPERATOR_PLUS:
4349 mpfr_set(val, uval, GMP_RNDN);
4350 return true;
4351 case OPERATOR_MINUS:
4352 mpfr_neg(val, uval, GMP_RNDN);
4353 return true;
4354 case OPERATOR_NOT:
4355 case OPERATOR_XOR:
4356 case OPERATOR_AND:
4357 case OPERATOR_MULT:
4358 return false;
4359 default:
c3e6f413 4360 go_unreachable();
e440a328 4361 }
4362}
4363
4364// Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4365// if this could be done, false if not.
4366
4367bool
4368Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4369 mpfr_t real, mpfr_t imag)
4370{
4371 switch (op)
4372 {
4373 case OPERATOR_PLUS:
4374 mpfr_set(real, rval, GMP_RNDN);
4375 mpfr_set(imag, ival, GMP_RNDN);
4376 return true;
4377 case OPERATOR_MINUS:
4378 mpfr_neg(real, rval, GMP_RNDN);
4379 mpfr_neg(imag, ival, GMP_RNDN);
4380 return true;
4381 case OPERATOR_NOT:
4382 case OPERATOR_XOR:
4383 case OPERATOR_AND:
4384 case OPERATOR_MULT:
4385 return false;
4386 default:
c3e6f413 4387 go_unreachable();
e440a328 4388 }
4389}
4390
4391// Return the integral constant value of a unary expression, if it has one.
4392
4393bool
4394Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4395 Type** ptype) const
4396{
4397 mpz_t uval;
4398 mpz_init(uval);
4399 bool ret;
4400 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4401 ret = false;
4402 else
4403 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4404 this->location());
4405 mpz_clear(uval);
4406 return ret;
4407}
4408
4409// Return the floating point constant value of a unary expression, if
4410// it has one.
4411
4412bool
4413Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4414{
4415 mpfr_t uval;
4416 mpfr_init(uval);
4417 bool ret;
4418 if (!this->expr_->float_constant_value(uval, ptype))
4419 ret = false;
4420 else
4421 ret = Unary_expression::eval_float(this->op_, uval, val);
4422 mpfr_clear(uval);
4423 return ret;
4424}
4425
4426// Return the complex constant value of a unary expression, if it has
4427// one.
4428
4429bool
4430Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4431 Type** ptype) const
4432{
4433 mpfr_t rval;
4434 mpfr_t ival;
4435 mpfr_init(rval);
4436 mpfr_init(ival);
4437 bool ret;
4438 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4439 ret = false;
4440 else
4441 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4442 mpfr_clear(rval);
4443 mpfr_clear(ival);
4444 return ret;
4445}
4446
4447// Return the type of a unary expression.
4448
4449Type*
4450Unary_expression::do_type()
4451{
4452 switch (this->op_)
4453 {
4454 case OPERATOR_PLUS:
4455 case OPERATOR_MINUS:
4456 case OPERATOR_NOT:
4457 case OPERATOR_XOR:
4458 return this->expr_->type();
4459
4460 case OPERATOR_AND:
4461 return Type::make_pointer_type(this->expr_->type());
4462
4463 case OPERATOR_MULT:
4464 {
4465 Type* subtype = this->expr_->type();
4466 Type* points_to = subtype->points_to();
4467 if (points_to == NULL)
4468 return Type::make_error_type();
4469 return points_to;
4470 }
4471
4472 default:
c3e6f413 4473 go_unreachable();
e440a328 4474 }
4475}
4476
4477// Determine abstract types for a unary expression.
4478
4479void
4480Unary_expression::do_determine_type(const Type_context* context)
4481{
4482 switch (this->op_)
4483 {
4484 case OPERATOR_PLUS:
4485 case OPERATOR_MINUS:
4486 case OPERATOR_NOT:
4487 case OPERATOR_XOR:
4488 this->expr_->determine_type(context);
4489 break;
4490
4491 case OPERATOR_AND:
4492 // Taking the address of something.
4493 {
4494 Type* subtype = (context->type == NULL
4495 ? NULL
4496 : context->type->points_to());
4497 Type_context subcontext(subtype, false);
4498 this->expr_->determine_type(&subcontext);
4499 }
4500 break;
4501
4502 case OPERATOR_MULT:
4503 // Indirecting through a pointer.
4504 {
4505 Type* subtype = (context->type == NULL
4506 ? NULL
4507 : Type::make_pointer_type(context->type));
4508 Type_context subcontext(subtype, false);
4509 this->expr_->determine_type(&subcontext);
4510 }
4511 break;
4512
4513 default:
c3e6f413 4514 go_unreachable();
e440a328 4515 }
4516}
4517
4518// Check types for a unary expression.
4519
4520void
4521Unary_expression::do_check_types(Gogo*)
4522{
9fe897ef 4523 Type* type = this->expr_->type();
5c13bd80 4524 if (type->is_error())
9fe897ef 4525 {
4526 this->set_is_error();
4527 return;
4528 }
4529
e440a328 4530 switch (this->op_)
4531 {
4532 case OPERATOR_PLUS:
4533 case OPERATOR_MINUS:
9fe897ef 4534 if (type->integer_type() == NULL
4535 && type->float_type() == NULL
4536 && type->complex_type() == NULL)
4537 this->report_error(_("expected numeric type"));
e440a328 4538 break;
4539
4540 case OPERATOR_NOT:
4541 case OPERATOR_XOR:
9fe897ef 4542 if (type->integer_type() == NULL
4543 && !type->is_boolean_type())
4544 this->report_error(_("expected integer or boolean type"));
e440a328 4545 break;
4546
4547 case OPERATOR_AND:
4548 if (!this->expr_->is_addressable())
09ea332d 4549 {
4550 if (!this->create_temp_)
4551 this->report_error(_("invalid operand for unary %<&%>"));
4552 }
e440a328 4553 else
4554 this->expr_->address_taken(this->escapes_);
4555 break;
4556
4557 case OPERATOR_MULT:
4558 // Indirecting through a pointer.
9fe897ef 4559 if (type->points_to() == NULL)
4560 this->report_error(_("expected pointer"));
e440a328 4561 break;
4562
4563 default:
c3e6f413 4564 go_unreachable();
e440a328 4565 }
4566}
4567
4568// Get a tree for a unary expression.
4569
4570tree
4571Unary_expression::do_get_tree(Translate_context* context)
4572{
e9d3367e 4573 Location loc = this->location();
4574
4575 // Taking the address of a set-and-use-temporary expression requires
4576 // setting the temporary and then taking the address.
4577 if (this->op_ == OPERATOR_AND)
4578 {
4579 Set_and_use_temporary_expression* sut =
4580 this->expr_->set_and_use_temporary_expression();
4581 if (sut != NULL)
4582 {
4583 Temporary_statement* temp = sut->temporary();
4584 Bvariable* bvar = temp->get_backend_variable(context);
4585 tree var_tree = var_to_tree(bvar);
4586 Expression* val = sut->expression();
4587 tree val_tree = val->get_tree(context);
4588 if (var_tree == error_mark_node || val_tree == error_mark_node)
4589 return error_mark_node;
4590 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
4591 var_tree);
4592 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4593 TREE_TYPE(addr_tree),
4594 build2_loc(sut->location().gcc_location(),
4595 MODIFY_EXPR, void_type_node,
4596 var_tree, val_tree),
4597 addr_tree);
4598 }
4599 }
4600
e440a328 4601 tree expr = this->expr_->get_tree(context);
4602 if (expr == error_mark_node)
4603 return error_mark_node;
4604
e440a328 4605 switch (this->op_)
4606 {
4607 case OPERATOR_PLUS:
4608 return expr;
4609
4610 case OPERATOR_MINUS:
4611 {
4612 tree type = TREE_TYPE(expr);
4613 tree compute_type = excess_precision_type(type);
4614 if (compute_type != NULL_TREE)
4615 expr = ::convert(compute_type, expr);
b13c66cd 4616 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
e440a328 4617 (compute_type != NULL_TREE
4618 ? compute_type
4619 : type),
4620 expr);
4621 if (compute_type != NULL_TREE)
4622 ret = ::convert(type, ret);
4623 return ret;
4624 }
4625
4626 case OPERATOR_NOT:
4627 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
b13c66cd 4628 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
4629 TREE_TYPE(expr), expr);
e440a328 4630 else
b13c66cd 4631 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
4632 expr, build_int_cst(TREE_TYPE(expr), 0));
e440a328 4633
4634 case OPERATOR_XOR:
b13c66cd 4635 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4636 expr);
e440a328 4637
4638 case OPERATOR_AND:
09ea332d 4639 if (!this->create_temp_)
4640 {
4641 // We should not see a non-constant constructor here; cases
4642 // where we would see one should have been moved onto the
4643 // heap at parse time. Taking the address of a nonconstant
4644 // constructor will not do what the programmer expects.
4645 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4646 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4647 }
e440a328 4648
4649 // Build a decl for a constant constructor.
4650 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4651 {
b13c66cd 4652 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
e440a328 4653 create_tmp_var_name("C"), TREE_TYPE(expr));
4654 DECL_EXTERNAL(decl) = 0;
4655 TREE_PUBLIC(decl) = 0;
4656 TREE_READONLY(decl) = 1;
4657 TREE_CONSTANT(decl) = 1;
4658 TREE_STATIC(decl) = 1;
4659 TREE_ADDRESSABLE(decl) = 1;
4660 DECL_ARTIFICIAL(decl) = 1;
4661 DECL_INITIAL(decl) = expr;
4662 rest_of_decl_compilation(decl, 1, 0);
4663 expr = decl;
4664 }
4665
09ea332d 4666 if (this->create_temp_
4667 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4668 && !DECL_P(expr)
4669 && TREE_CODE(expr) != INDIRECT_REF
4670 && TREE_CODE(expr) != COMPONENT_REF)
4671 {
4672 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4673 DECL_IGNORED_P(tmp) = 1;
4674 DECL_INITIAL(tmp) = expr;
4675 TREE_ADDRESSABLE(tmp) = 1;
b13c66cd 4676 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
09ea332d 4677 build_pointer_type(TREE_TYPE(expr)),
b13c66cd 4678 build1_loc(loc.gcc_location(), DECL_EXPR,
4679 void_type_node, tmp),
4680 build_fold_addr_expr_loc(loc.gcc_location(), tmp));
09ea332d 4681 }
4682
b13c66cd 4683 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
e440a328 4684
4685 case OPERATOR_MULT:
4686 {
c484d925 4687 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
e440a328 4688
4689 // If we are dereferencing the pointer to a large struct, we
4690 // need to check for nil. We don't bother to check for small
4691 // structs because we expect the system to crash on a nil
4692 // pointer dereference.
4693 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4694 if (s == -1 || s >= 4096)
4695 {
4696 if (!DECL_P(expr))
4697 expr = save_expr(expr);
b13c66cd 4698 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4699 boolean_type_node,
e440a328 4700 expr,
4701 fold_convert(TREE_TYPE(expr),
4702 null_pointer_node));
4703 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4704 loc);
b13c66cd 4705 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4706 TREE_TYPE(expr), build3(COND_EXPR,
4707 void_type_node,
4708 compare, crash,
4709 NULL_TREE),
e440a328 4710 expr);
4711 }
4712
4713 // If the type of EXPR is a recursive pointer type, then we
4714 // need to insert a cast before indirecting.
4715 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4716 {
4717 Type* pt = this->expr_->type()->points_to();
9f0e0513 4718 tree ind = type_to_tree(pt->get_backend(context->gogo()));
b13c66cd 4719 expr = fold_convert_loc(loc.gcc_location(),
4720 build_pointer_type(ind), expr);
e440a328 4721 }
4722
b13c66cd 4723 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
e440a328 4724 }
4725
4726 default:
c3e6f413 4727 go_unreachable();
e440a328 4728 }
4729}
4730
4731// Export a unary expression.
4732
4733void
4734Unary_expression::do_export(Export* exp) const
4735{
4736 switch (this->op_)
4737 {
4738 case OPERATOR_PLUS:
4739 exp->write_c_string("+ ");
4740 break;
4741 case OPERATOR_MINUS:
4742 exp->write_c_string("- ");
4743 break;
4744 case OPERATOR_NOT:
4745 exp->write_c_string("! ");
4746 break;
4747 case OPERATOR_XOR:
4748 exp->write_c_string("^ ");
4749 break;
4750 case OPERATOR_AND:
4751 case OPERATOR_MULT:
4752 default:
c3e6f413 4753 go_unreachable();
e440a328 4754 }
4755 this->expr_->export_expression(exp);
4756}
4757
4758// Import a unary expression.
4759
4760Expression*
4761Unary_expression::do_import(Import* imp)
4762{
4763 Operator op;
4764 switch (imp->get_char())
4765 {
4766 case '+':
4767 op = OPERATOR_PLUS;
4768 break;
4769 case '-':
4770 op = OPERATOR_MINUS;
4771 break;
4772 case '!':
4773 op = OPERATOR_NOT;
4774 break;
4775 case '^':
4776 op = OPERATOR_XOR;
4777 break;
4778 default:
c3e6f413 4779 go_unreachable();
e440a328 4780 }
4781 imp->require_c_string(" ");
4782 Expression* expr = Expression::import_expression(imp);
4783 return Expression::make_unary(op, expr, imp->location());
4784}
4785
d751bb78 4786// Dump ast representation of an unary expression.
4787
4788void
4789Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4790{
4791 ast_dump_context->dump_operator(this->op_);
4792 ast_dump_context->ostream() << "(";
4793 ast_dump_context->dump_expression(this->expr_);
4794 ast_dump_context->ostream() << ") ";
4795}
4796
e440a328 4797// Make a unary expression.
4798
4799Expression*
b13c66cd 4800Expression::make_unary(Operator op, Expression* expr, Location location)
e440a328 4801{
4802 return new Unary_expression(op, expr, location);
4803}
4804
4805// If this is an indirection through a pointer, return the expression
4806// being pointed through. Otherwise return this.
4807
4808Expression*
4809Expression::deref()
4810{
4811 if (this->classification_ == EXPRESSION_UNARY)
4812 {
4813 Unary_expression* ue = static_cast<Unary_expression*>(this);
4814 if (ue->op() == OPERATOR_MULT)
4815 return ue->operand();
4816 }
4817 return this;
4818}
4819
4820// Class Binary_expression.
4821
4822// Traversal.
4823
4824int
4825Binary_expression::do_traverse(Traverse* traverse)
4826{
4827 int t = Expression::traverse(&this->left_, traverse);
4828 if (t == TRAVERSE_EXIT)
4829 return TRAVERSE_EXIT;
4830 return Expression::traverse(&this->right_, traverse);
4831}
4832
4833// Compare integer constants according to OP.
4834
4835bool
4836Binary_expression::compare_integer(Operator op, mpz_t left_val,
4837 mpz_t right_val)
4838{
4839 int i = mpz_cmp(left_val, right_val);
4840 switch (op)
4841 {
4842 case OPERATOR_EQEQ:
4843 return i == 0;
4844 case OPERATOR_NOTEQ:
4845 return i != 0;
4846 case OPERATOR_LT:
4847 return i < 0;
4848 case OPERATOR_LE:
4849 return i <= 0;
4850 case OPERATOR_GT:
4851 return i > 0;
4852 case OPERATOR_GE:
4853 return i >= 0;
4854 default:
c3e6f413 4855 go_unreachable();
e440a328 4856 }
4857}
4858
4859// Compare floating point constants according to OP.
4860
4861bool
4862Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4863 mpfr_t right_val)
4864{
4865 int i;
4866 if (type == NULL)
4867 i = mpfr_cmp(left_val, right_val);
4868 else
4869 {
4870 mpfr_t lv;
4871 mpfr_init_set(lv, left_val, GMP_RNDN);
4872 mpfr_t rv;
4873 mpfr_init_set(rv, right_val, GMP_RNDN);
4874 Float_expression::constrain_float(lv, type);
4875 Float_expression::constrain_float(rv, type);
4876 i = mpfr_cmp(lv, rv);
4877 mpfr_clear(lv);
4878 mpfr_clear(rv);
4879 }
4880 switch (op)
4881 {
4882 case OPERATOR_EQEQ:
4883 return i == 0;
4884 case OPERATOR_NOTEQ:
4885 return i != 0;
4886 case OPERATOR_LT:
4887 return i < 0;
4888 case OPERATOR_LE:
4889 return i <= 0;
4890 case OPERATOR_GT:
4891 return i > 0;
4892 case OPERATOR_GE:
4893 return i >= 0;
4894 default:
c3e6f413 4895 go_unreachable();
e440a328 4896 }
4897}
4898
4899// Compare complex constants according to OP. Complex numbers may
4900// only be compared for equality.
4901
4902bool
4903Binary_expression::compare_complex(Operator op, Type* type,
4904 mpfr_t left_real, mpfr_t left_imag,
4905 mpfr_t right_real, mpfr_t right_imag)
4906{
4907 bool is_equal;
4908 if (type == NULL)
4909 is_equal = (mpfr_cmp(left_real, right_real) == 0
4910 && mpfr_cmp(left_imag, right_imag) == 0);
4911 else
4912 {
4913 mpfr_t lr;
4914 mpfr_t li;
4915 mpfr_init_set(lr, left_real, GMP_RNDN);
4916 mpfr_init_set(li, left_imag, GMP_RNDN);
4917 mpfr_t rr;
4918 mpfr_t ri;
4919 mpfr_init_set(rr, right_real, GMP_RNDN);
4920 mpfr_init_set(ri, right_imag, GMP_RNDN);
4921 Complex_expression::constrain_complex(lr, li, type);
4922 Complex_expression::constrain_complex(rr, ri, type);
4923 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4924 mpfr_clear(lr);
4925 mpfr_clear(li);
4926 mpfr_clear(rr);
4927 mpfr_clear(ri);
4928 }
4929 switch (op)
4930 {
4931 case OPERATOR_EQEQ:
4932 return is_equal;
4933 case OPERATOR_NOTEQ:
4934 return !is_equal;
4935 default:
c3e6f413 4936 go_unreachable();
e440a328 4937 }
4938}
4939
4940// Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4941// LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4942// RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4943// this could be done, false if not.
4944
4945bool
4946Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4947 Type* right_type, mpz_t right_val,
b13c66cd 4948 Location location, mpz_t val)
e440a328 4949{
4950 bool is_shift_op = false;
4951 switch (op)
4952 {
4953 case OPERATOR_OROR:
4954 case OPERATOR_ANDAND:
4955 case OPERATOR_EQEQ:
4956 case OPERATOR_NOTEQ:
4957 case OPERATOR_LT:
4958 case OPERATOR_LE:
4959 case OPERATOR_GT:
4960 case OPERATOR_GE:
4961 // These return boolean values. We should probably handle them
4962 // anyhow in case a type conversion is used on the result.
4963 return false;
4964 case OPERATOR_PLUS:
4965 mpz_add(val, left_val, right_val);
4966 break;
4967 case OPERATOR_MINUS:
4968 mpz_sub(val, left_val, right_val);
4969 break;
4970 case OPERATOR_OR:
4971 mpz_ior(val, left_val, right_val);
4972 break;
4973 case OPERATOR_XOR:
4974 mpz_xor(val, left_val, right_val);
4975 break;
4976 case OPERATOR_MULT:
4977 mpz_mul(val, left_val, right_val);
4978 break;
4979 case OPERATOR_DIV:
4980 if (mpz_sgn(right_val) != 0)
4981 mpz_tdiv_q(val, left_val, right_val);
4982 else
4983 {
4984 error_at(location, "division by zero");
4985 mpz_set_ui(val, 0);
4986 return true;
4987 }
4988 break;
4989 case OPERATOR_MOD:
4990 if (mpz_sgn(right_val) != 0)
4991 mpz_tdiv_r(val, left_val, right_val);
4992 else
4993 {
4994 error_at(location, "division by zero");
4995 mpz_set_ui(val, 0);
4996 return true;
4997 }
4998 break;
4999 case OPERATOR_LSHIFT:
5000 {
5001 unsigned long shift = mpz_get_ui(right_val);
a28c1598 5002 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
e440a328 5003 {
5004 error_at(location, "shift count overflow");
5005 mpz_set_ui(val, 0);
5006 return true;
5007 }
5008 mpz_mul_2exp(val, left_val, shift);
5009 is_shift_op = true;
5010 break;
5011 }
5012 break;
5013 case OPERATOR_RSHIFT:
5014 {
5015 unsigned long shift = mpz_get_ui(right_val);
5016 if (mpz_cmp_ui(right_val, shift) != 0)
5017 {
5018 error_at(location, "shift count overflow");
5019 mpz_set_ui(val, 0);
5020 return true;
5021 }
5022 if (mpz_cmp_ui(left_val, 0) >= 0)
5023 mpz_tdiv_q_2exp(val, left_val, shift);
5024 else
5025 mpz_fdiv_q_2exp(val, left_val, shift);
5026 is_shift_op = true;
5027 break;
5028 }
5029 break;
5030 case OPERATOR_AND:
5031 mpz_and(val, left_val, right_val);
5032 break;
5033 case OPERATOR_BITCLEAR:
5034 {
5035 mpz_t tval;
5036 mpz_init(tval);
5037 mpz_com(tval, right_val);
5038 mpz_and(val, left_val, tval);
5039 mpz_clear(tval);
5040 }
5041 break;
5042 default:
c3e6f413 5043 go_unreachable();
e440a328 5044 }
5045
5046 Type* type = left_type;
5047 if (!is_shift_op)
5048 {
5049 if (type == NULL)
5050 type = right_type;
5051 else if (type != right_type && right_type != NULL)
5052 {
5053 if (type->is_abstract())
5054 type = right_type;
5055 else if (!right_type->is_abstract())
5056 {
5057 // This look like a type error which should be diagnosed
5058 // elsewhere. Don't do anything here, to avoid an
5059 // unhelpful chain of error messages.
5060 return true;
5061 }
5062 }
5063 }
5064
5065 if (type != NULL && !type->is_abstract())
5066 {
5067 // We have to check the operands too, as we have implicitly
5068 // coerced them to TYPE.
5069 if ((type != left_type
5070 && !Integer_expression::check_constant(left_val, type, location))
5071 || (!is_shift_op
5072 && type != right_type
5073 && !Integer_expression::check_constant(right_val, type,
5074 location))
5075 || !Integer_expression::check_constant(val, type, location))
5076 mpz_set_ui(val, 0);
5077 }
5078
5079 return true;
5080}
5081
5082// Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
5083// Return true if this could be done, false if not.
5084
5085bool
5086Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
5087 Type* right_type, mpfr_t right_val,
b13c66cd 5088 mpfr_t val, Location location)
e440a328 5089{
5090 switch (op)
5091 {
5092 case OPERATOR_OROR:
5093 case OPERATOR_ANDAND:
5094 case OPERATOR_EQEQ:
5095 case OPERATOR_NOTEQ:
5096 case OPERATOR_LT:
5097 case OPERATOR_LE:
5098 case OPERATOR_GT:
5099 case OPERATOR_GE:
5100 // These return boolean values. We should probably handle them
5101 // anyhow in case a type conversion is used on the result.
5102 return false;
5103 case OPERATOR_PLUS:
5104 mpfr_add(val, left_val, right_val, GMP_RNDN);
5105 break;
5106 case OPERATOR_MINUS:
5107 mpfr_sub(val, left_val, right_val, GMP_RNDN);
5108 break;
5109 case OPERATOR_OR:
5110 case OPERATOR_XOR:
5111 case OPERATOR_AND:
5112 case OPERATOR_BITCLEAR:
5113 return false;
5114 case OPERATOR_MULT:
5115 mpfr_mul(val, left_val, right_val, GMP_RNDN);
5116 break;
5117 case OPERATOR_DIV:
5118 if (mpfr_zero_p(right_val))
5119 error_at(location, "division by zero");
5120 mpfr_div(val, left_val, right_val, GMP_RNDN);
5121 break;
5122 case OPERATOR_MOD:
5123 return false;
5124 case OPERATOR_LSHIFT:
5125 case OPERATOR_RSHIFT:
5126 return false;
5127 default:
c3e6f413 5128 go_unreachable();
e440a328 5129 }
5130
5131 Type* type = left_type;
5132 if (type == NULL)
5133 type = right_type;
5134 else if (type != right_type && right_type != NULL)
5135 {
5136 if (type->is_abstract())
5137 type = right_type;
5138 else if (!right_type->is_abstract())
5139 {
5140 // This looks like a type error which should be diagnosed
5141 // elsewhere. Don't do anything here, to avoid an unhelpful
5142 // chain of error messages.
5143 return true;
5144 }
5145 }
5146
5147 if (type != NULL && !type->is_abstract())
5148 {
5149 if ((type != left_type
5150 && !Float_expression::check_constant(left_val, type, location))
5151 || (type != right_type
5152 && !Float_expression::check_constant(right_val, type,
5153 location))
5154 || !Float_expression::check_constant(val, type, location))
5155 mpfr_set_ui(val, 0, GMP_RNDN);
5156 }
5157
5158 return true;
5159}
5160
5161// Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
5162// RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
5163// could be done, false if not.
5164
5165bool
5166Binary_expression::eval_complex(Operator op, Type* left_type,
5167 mpfr_t left_real, mpfr_t left_imag,
5168 Type *right_type,
5169 mpfr_t right_real, mpfr_t right_imag,
5170 mpfr_t real, mpfr_t imag,
b13c66cd 5171 Location location)
e440a328 5172{
5173 switch (op)
5174 {
5175 case OPERATOR_OROR:
5176 case OPERATOR_ANDAND:
5177 case OPERATOR_EQEQ:
5178 case OPERATOR_NOTEQ:
5179 case OPERATOR_LT:
5180 case OPERATOR_LE:
5181 case OPERATOR_GT:
5182 case OPERATOR_GE:
5183 // These return boolean values and must be handled differently.
5184 return false;
5185 case OPERATOR_PLUS:
5186 mpfr_add(real, left_real, right_real, GMP_RNDN);
5187 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
5188 break;
5189 case OPERATOR_MINUS:
5190 mpfr_sub(real, left_real, right_real, GMP_RNDN);
5191 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
5192 break;
5193 case OPERATOR_OR:
5194 case OPERATOR_XOR:
5195 case OPERATOR_AND:
5196 case OPERATOR_BITCLEAR:
5197 return false;
5198 case OPERATOR_MULT:
5199 {
5200 // You might think that multiplying two complex numbers would
5201 // be simple, and you would be right, until you start to think
5202 // about getting the right answer for infinity. If one
5203 // operand here is infinity and the other is anything other
5204 // than zero or NaN, then we are going to wind up subtracting
5205 // two infinity values. That will give us a NaN, but the
5206 // correct answer is infinity.
5207
5208 mpfr_t lrrr;
5209 mpfr_init(lrrr);
5210 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
5211
5212 mpfr_t lrri;
5213 mpfr_init(lrri);
5214 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
5215
5216 mpfr_t lirr;
5217 mpfr_init(lirr);
5218 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
5219
5220 mpfr_t liri;
5221 mpfr_init(liri);
5222 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
5223
5224 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5225 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5226
5227 // If we get NaN on both sides, check whether it should really
5228 // be infinity. The rule is that if either side of the
5229 // complex number is infinity, then the whole value is
5230 // infinity, even if the other side is NaN. So the only case
5231 // we have to fix is the one in which both sides are NaN.
5232 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5233 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5234 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5235 {
5236 bool is_infinity = false;
5237
5238 mpfr_t lr;
5239 mpfr_t li;
5240 mpfr_init_set(lr, left_real, GMP_RNDN);
5241 mpfr_init_set(li, left_imag, GMP_RNDN);
5242
5243 mpfr_t rr;
5244 mpfr_t ri;
5245 mpfr_init_set(rr, right_real, GMP_RNDN);
5246 mpfr_init_set(ri, right_imag, GMP_RNDN);
5247
5248 // If the left side is infinity, then the result is
5249 // infinity.
5250 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
5251 {
5252 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
5253 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5254 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
5255 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5256 if (mpfr_nan_p(rr))
5257 {
5258 mpfr_set_ui(rr, 0, GMP_RNDN);
5259 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5260 }
5261 if (mpfr_nan_p(ri))
5262 {
5263 mpfr_set_ui(ri, 0, GMP_RNDN);
5264 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5265 }
5266 is_infinity = true;
5267 }
5268
5269 // If the right side is infinity, then the result is
5270 // infinity.
5271 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
5272 {
5273 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5274 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5275 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5276 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5277 if (mpfr_nan_p(lr))
5278 {
5279 mpfr_set_ui(lr, 0, GMP_RNDN);
5280 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5281 }
5282 if (mpfr_nan_p(li))
5283 {
5284 mpfr_set_ui(li, 0, GMP_RNDN);
5285 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5286 }
5287 is_infinity = true;
5288 }
5289
5290 // If we got an overflow in the intermediate computations,
5291 // then the result is infinity.
5292 if (!is_infinity
5293 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
5294 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
5295 {
5296 if (mpfr_nan_p(lr))
5297 {
5298 mpfr_set_ui(lr, 0, GMP_RNDN);
5299 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5300 }
5301 if (mpfr_nan_p(li))
5302 {
5303 mpfr_set_ui(li, 0, GMP_RNDN);
5304 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5305 }
5306 if (mpfr_nan_p(rr))
5307 {
5308 mpfr_set_ui(rr, 0, GMP_RNDN);
5309 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5310 }
5311 if (mpfr_nan_p(ri))
5312 {
5313 mpfr_set_ui(ri, 0, GMP_RNDN);
5314 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5315 }
5316 is_infinity = true;
5317 }
5318
5319 if (is_infinity)
5320 {
5321 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
5322 mpfr_mul(lrri, lr, ri, GMP_RNDN);
5323 mpfr_mul(lirr, li, rr, GMP_RNDN);
5324 mpfr_mul(liri, li, ri, GMP_RNDN);
5325 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5326 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5327 mpfr_set_inf(real, mpfr_sgn(real));
5328 mpfr_set_inf(imag, mpfr_sgn(imag));
5329 }
5330
5331 mpfr_clear(lr);
5332 mpfr_clear(li);
5333 mpfr_clear(rr);
5334 mpfr_clear(ri);
5335 }
5336
5337 mpfr_clear(lrrr);
5338 mpfr_clear(lrri);
5339 mpfr_clear(lirr);
5340 mpfr_clear(liri);
5341 }
5342 break;
5343 case OPERATOR_DIV:
5344 {
5345 // For complex division we want to avoid having an
5346 // intermediate overflow turn the whole result in a NaN. We
5347 // scale the values to try to avoid this.
5348
5349 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
5350 error_at(location, "division by zero");
5351
5352 mpfr_t rra;
5353 mpfr_t ria;
5354 mpfr_init(rra);
5355 mpfr_init(ria);
5356 mpfr_abs(rra, right_real, GMP_RNDN);
5357 mpfr_abs(ria, right_imag, GMP_RNDN);
5358 mpfr_t t;
5359 mpfr_init(t);
5360 mpfr_max(t, rra, ria, GMP_RNDN);
5361
5362 mpfr_t rr;
5363 mpfr_t ri;
5364 mpfr_init_set(rr, right_real, GMP_RNDN);
5365 mpfr_init_set(ri, right_imag, GMP_RNDN);
5366 long ilogbw = 0;
5367 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
5368 {
5369 ilogbw = mpfr_get_exp(t);
5370 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
5371 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
5372 }
5373
5374 mpfr_t denom;
5375 mpfr_init(denom);
5376 mpfr_mul(denom, rr, rr, GMP_RNDN);
5377 mpfr_mul(t, ri, ri, GMP_RNDN);
5378 mpfr_add(denom, denom, t, GMP_RNDN);
5379
5380 mpfr_mul(real, left_real, rr, GMP_RNDN);
5381 mpfr_mul(t, left_imag, ri, GMP_RNDN);
5382 mpfr_add(real, real, t, GMP_RNDN);
5383 mpfr_div(real, real, denom, GMP_RNDN);
5384 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
5385
5386 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
5387 mpfr_mul(t, left_real, ri, GMP_RNDN);
5388 mpfr_sub(imag, imag, t, GMP_RNDN);
5389 mpfr_div(imag, imag, denom, GMP_RNDN);
5390 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
5391
5392 // If we wind up with NaN on both sides, check whether we
5393 // should really have infinity. The rule is that if either
5394 // side of the complex number is infinity, then the whole
5395 // value is infinity, even if the other side is NaN. So the
5396 // only case we have to fix is the one in which both sides are
5397 // NaN.
5398 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5399 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5400 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5401 {
5402 if (mpfr_zero_p(denom))
5403 {
5404 mpfr_set_inf(real, mpfr_sgn(rr));
5405 mpfr_mul(real, real, left_real, GMP_RNDN);
5406 mpfr_set_inf(imag, mpfr_sgn(rr));
5407 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
5408 }
5409 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
5410 && mpfr_number_p(rr) && mpfr_number_p(ri))
5411 {
5412 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5413 mpfr_copysign(t, t, left_real, GMP_RNDN);
5414
5415 mpfr_t t2;
5416 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5417 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5418
5419 mpfr_t t3;
5420 mpfr_init(t3);
5421 mpfr_mul(t3, t, rr, GMP_RNDN);
5422
5423 mpfr_t t4;
5424 mpfr_init(t4);
5425 mpfr_mul(t4, t2, ri, GMP_RNDN);
5426
5427 mpfr_add(t3, t3, t4, GMP_RNDN);
5428 mpfr_set_inf(real, mpfr_sgn(t3));
5429
5430 mpfr_mul(t3, t2, rr, GMP_RNDN);
5431 mpfr_mul(t4, t, ri, GMP_RNDN);
5432 mpfr_sub(t3, t3, t4, GMP_RNDN);
5433 mpfr_set_inf(imag, mpfr_sgn(t3));
5434
5435 mpfr_clear(t2);
5436 mpfr_clear(t3);
5437 mpfr_clear(t4);
5438 }
5439 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5440 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5441 {
5442 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5443 mpfr_copysign(t, t, rr, GMP_RNDN);
5444
5445 mpfr_t t2;
5446 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5447 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5448
5449 mpfr_t t3;
5450 mpfr_init(t3);
5451 mpfr_mul(t3, left_real, t, GMP_RNDN);
5452
5453 mpfr_t t4;
5454 mpfr_init(t4);
5455 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5456
5457 mpfr_add(t3, t3, t4, GMP_RNDN);
5458 mpfr_set_ui(real, 0, GMP_RNDN);
5459 mpfr_mul(real, real, t3, GMP_RNDN);
5460
5461 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5462 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5463 mpfr_sub(t3, t3, t4, GMP_RNDN);
5464 mpfr_set_ui(imag, 0, GMP_RNDN);
5465 mpfr_mul(imag, imag, t3, GMP_RNDN);
5466
5467 mpfr_clear(t2);
5468 mpfr_clear(t3);
5469 mpfr_clear(t4);
5470 }
5471 }
5472
5473 mpfr_clear(denom);
5474 mpfr_clear(rr);
5475 mpfr_clear(ri);
5476 mpfr_clear(t);
5477 mpfr_clear(rra);
5478 mpfr_clear(ria);
5479 }
5480 break;
5481 case OPERATOR_MOD:
5482 return false;
5483 case OPERATOR_LSHIFT:
5484 case OPERATOR_RSHIFT:
5485 return false;
5486 default:
c3e6f413 5487 go_unreachable();
e440a328 5488 }
5489
5490 Type* type = left_type;
5491 if (type == NULL)
5492 type = right_type;
5493 else if (type != right_type && right_type != NULL)
5494 {
5495 if (type->is_abstract())
5496 type = right_type;
5497 else if (!right_type->is_abstract())
5498 {
5499 // This looks like a type error which should be diagnosed
5500 // elsewhere. Don't do anything here, to avoid an unhelpful
5501 // chain of error messages.
5502 return true;
5503 }
5504 }
5505
5506 if (type != NULL && !type->is_abstract())
5507 {
5508 if ((type != left_type
5509 && !Complex_expression::check_constant(left_real, left_imag,
5510 type, location))
5511 || (type != right_type
5512 && !Complex_expression::check_constant(right_real, right_imag,
5513 type, location))
5514 || !Complex_expression::check_constant(real, imag, type,
5515 location))
5516 {
5517 mpfr_set_ui(real, 0, GMP_RNDN);
5518 mpfr_set_ui(imag, 0, GMP_RNDN);
5519 }
5520 }
5521
5522 return true;
5523}
5524
5525// Lower a binary expression. We have to evaluate constant
5526// expressions now, in order to implement Go's unlimited precision
5527// constants.
5528
5529Expression*
e9d3367e 5530Binary_expression::do_lower(Gogo* gogo, Named_object*,
5531 Statement_inserter* inserter, int)
e440a328 5532{
b13c66cd 5533 Location location = this->location();
e440a328 5534 Operator op = this->op_;
5535 Expression* left = this->left_;
5536 Expression* right = this->right_;
5537
5538 const bool is_comparison = (op == OPERATOR_EQEQ
5539 || op == OPERATOR_NOTEQ
5540 || op == OPERATOR_LT
5541 || op == OPERATOR_LE
5542 || op == OPERATOR_GT
5543 || op == OPERATOR_GE);
5544
5545 // Integer constant expressions.
5546 {
5547 mpz_t left_val;
5548 mpz_init(left_val);
5549 Type* left_type;
5550 mpz_t right_val;
5551 mpz_init(right_val);
5552 Type* right_type;
5553 if (left->integer_constant_value(false, left_val, &left_type)
5554 && right->integer_constant_value(false, right_val, &right_type))
5555 {
5556 Expression* ret = NULL;
5557 if (left_type != right_type
5558 && left_type != NULL
cfdd67bc 5559 && !left_type->is_abstract()
e440a328 5560 && right_type != NULL
cfdd67bc 5561 && !right_type->is_abstract()
e440a328 5562 && left_type->base() != right_type->base()
5563 && op != OPERATOR_LSHIFT
5564 && op != OPERATOR_RSHIFT)
5565 {
5566 // May be a type error--let it be diagnosed later.
5567 }
5568 else if (is_comparison)
5569 {
5570 bool b = Binary_expression::compare_integer(op, left_val,
5571 right_val);
5572 ret = Expression::make_cast(Type::lookup_bool_type(),
5573 Expression::make_boolean(b, location),
5574 location);
5575 }
5576 else
5577 {
5578 mpz_t val;
5579 mpz_init(val);
5580
5581 if (Binary_expression::eval_integer(op, left_type, left_val,
5582 right_type, right_val,
5583 location, val))
5584 {
c484d925 5585 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
e440a328 5586 Type* type;
5587 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5588 type = left_type;
5589 else if (left_type == NULL)
5590 type = right_type;
5591 else if (right_type == NULL)
5592 type = left_type;
5593 else if (!left_type->is_abstract()
5594 && left_type->named_type() != NULL)
5595 type = left_type;
5596 else if (!right_type->is_abstract()
5597 && right_type->named_type() != NULL)
5598 type = right_type;
5599 else if (!left_type->is_abstract())
5600 type = left_type;
5601 else if (!right_type->is_abstract())
5602 type = right_type;
5603 else if (left_type->float_type() != NULL)
5604 type = left_type;
5605 else if (right_type->float_type() != NULL)
5606 type = right_type;
5607 else if (left_type->complex_type() != NULL)
5608 type = left_type;
5609 else if (right_type->complex_type() != NULL)
5610 type = right_type;
5611 else
5612 type = left_type;
cfdd67bc 5613
5614 bool is_character = false;
5615 if (type == NULL)
5616 {
5617 Type* t = this->left_->type();
5618 if (t->integer_type() != NULL
5619 && t->integer_type()->is_rune())
5620 is_character = true;
5621 else if (op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT)
5622 {
5623 t = this->right_->type();
5624 if (t->integer_type() != NULL
5625 && t->integer_type()->is_rune())
5626 is_character = true;
5627 }
5628 }
5629
5630 if (is_character)
5631 ret = Expression::make_character(&val, type, location);
5632 else
5633 ret = Expression::make_integer(&val, type, location);
e440a328 5634 }
5635
5636 mpz_clear(val);
5637 }
5638
5639 if (ret != NULL)
5640 {
5641 mpz_clear(right_val);
5642 mpz_clear(left_val);
5643 return ret;
5644 }
5645 }
5646 mpz_clear(right_val);
5647 mpz_clear(left_val);
5648 }
5649
5650 // Floating point constant expressions.
5651 {
5652 mpfr_t left_val;
5653 mpfr_init(left_val);
5654 Type* left_type;
5655 mpfr_t right_val;
5656 mpfr_init(right_val);
5657 Type* right_type;
5658 if (left->float_constant_value(left_val, &left_type)
5659 && right->float_constant_value(right_val, &right_type))
5660 {
5661 Expression* ret = NULL;
5662 if (left_type != right_type
5663 && left_type != NULL
5664 && right_type != NULL
5665 && left_type->base() != right_type->base()
5666 && op != OPERATOR_LSHIFT
5667 && op != OPERATOR_RSHIFT)
5668 {
5669 // May be a type error--let it be diagnosed later.
5670 }
5671 else if (is_comparison)
5672 {
5673 bool b = Binary_expression::compare_float(op,
5674 (left_type != NULL
5675 ? left_type
5676 : right_type),
5677 left_val, right_val);
5678 ret = Expression::make_boolean(b, location);
5679 }
5680 else
5681 {
5682 mpfr_t val;
5683 mpfr_init(val);
5684
5685 if (Binary_expression::eval_float(op, left_type, left_val,
5686 right_type, right_val, val,
5687 location))
5688 {
c484d925 5689 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
e440a328 5690 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5691 Type* type;
5692 if (left_type == NULL)
5693 type = right_type;
5694 else if (right_type == NULL)
5695 type = left_type;
5696 else if (!left_type->is_abstract()
5697 && left_type->named_type() != NULL)
5698 type = left_type;
5699 else if (!right_type->is_abstract()
5700 && right_type->named_type() != NULL)
5701 type = right_type;
5702 else if (!left_type->is_abstract())
5703 type = left_type;
5704 else if (!right_type->is_abstract())
5705 type = right_type;
5706 else if (left_type->float_type() != NULL)
5707 type = left_type;
5708 else if (right_type->float_type() != NULL)
5709 type = right_type;
5710 else
5711 type = left_type;
5712 ret = Expression::make_float(&val, type, location);
5713 }
5714
5715 mpfr_clear(val);
5716 }
5717
5718 if (ret != NULL)
5719 {
5720 mpfr_clear(right_val);
5721 mpfr_clear(left_val);
5722 return ret;
5723 }
5724 }
5725 mpfr_clear(right_val);
5726 mpfr_clear(left_val);
5727 }
5728
5729 // Complex constant expressions.
5730 {
5731 mpfr_t left_real;
5732 mpfr_t left_imag;
5733 mpfr_init(left_real);
5734 mpfr_init(left_imag);
5735 Type* left_type;
5736
5737 mpfr_t right_real;
5738 mpfr_t right_imag;
5739 mpfr_init(right_real);
5740 mpfr_init(right_imag);
5741 Type* right_type;
5742
5743 if (left->complex_constant_value(left_real, left_imag, &left_type)
5744 && right->complex_constant_value(right_real, right_imag, &right_type))
5745 {
5746 Expression* ret = NULL;
5747 if (left_type != right_type
5748 && left_type != NULL
5749 && right_type != NULL
5750 && left_type->base() != right_type->base())
5751 {
5752 // May be a type error--let it be diagnosed later.
5753 }
3b59603e 5754 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
e440a328 5755 {
5756 bool b = Binary_expression::compare_complex(op,
5757 (left_type != NULL
5758 ? left_type
5759 : right_type),
5760 left_real,
5761 left_imag,
5762 right_real,
5763 right_imag);
5764 ret = Expression::make_boolean(b, location);
5765 }
5766 else
5767 {
5768 mpfr_t real;
5769 mpfr_t imag;
5770 mpfr_init(real);
5771 mpfr_init(imag);
5772
5773 if (Binary_expression::eval_complex(op, left_type,
5774 left_real, left_imag,
5775 right_type,
5776 right_real, right_imag,
5777 real, imag,
5778 location))
5779 {
c484d925 5780 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
e440a328 5781 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5782 Type* type;
5783 if (left_type == NULL)
5784 type = right_type;
5785 else if (right_type == NULL)
5786 type = left_type;
5787 else if (!left_type->is_abstract()
5788 && left_type->named_type() != NULL)
5789 type = left_type;
5790 else if (!right_type->is_abstract()
5791 && right_type->named_type() != NULL)
5792 type = right_type;
5793 else if (!left_type->is_abstract())
5794 type = left_type;
5795 else if (!right_type->is_abstract())
5796 type = right_type;
5797 else if (left_type->complex_type() != NULL)
5798 type = left_type;
5799 else if (right_type->complex_type() != NULL)
5800 type = right_type;
5801 else
5802 type = left_type;
5803 ret = Expression::make_complex(&real, &imag, type,
5804 location);
5805 }
5806 mpfr_clear(real);
5807 mpfr_clear(imag);
5808 }
5809
5810 if (ret != NULL)
5811 {
5812 mpfr_clear(left_real);
5813 mpfr_clear(left_imag);
5814 mpfr_clear(right_real);
5815 mpfr_clear(right_imag);
5816 return ret;
5817 }
5818 }
5819
5820 mpfr_clear(left_real);
5821 mpfr_clear(left_imag);
5822 mpfr_clear(right_real);
5823 mpfr_clear(right_imag);
5824 }
5825
5826 // String constant expressions.
5827 if (op == OPERATOR_PLUS
5828 && left->type()->is_string_type()
5829 && right->type()->is_string_type())
5830 {
5831 std::string left_string;
5832 std::string right_string;
5833 if (left->string_constant_value(&left_string)
5834 && right->string_constant_value(&right_string))
5835 return Expression::make_string(left_string + right_string, location);
5836 }
5837
b40dc774 5838 // Special case for shift of a floating point constant.
5839 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5840 {
5841 mpfr_t left_val;
5842 mpfr_init(left_val);
5843 Type* left_type;
5844 mpz_t right_val;
5845 mpz_init(right_val);
5846 Type* right_type;
5847 if (left->float_constant_value(left_val, &left_type)
5848 && right->integer_constant_value(false, right_val, &right_type)
5849 && mpfr_integer_p(left_val)
5850 && (left_type == NULL
5851 || left_type->is_abstract()
5852 || left_type->integer_type() != NULL))
5853 {
5854 mpz_t left_int;
5855 mpz_init(left_int);
5856 mpfr_get_z(left_int, left_val, GMP_RNDN);
5857
5858 mpz_t val;
5859 mpz_init(val);
5860
5861 Expression* ret = NULL;
5862 if (Binary_expression::eval_integer(op, left_type, left_int,
5863 right_type, right_val,
5864 location, val))
5865 ret = Expression::make_integer(&val, left_type, location);
5866
5867 mpz_clear(left_int);
5868 mpz_clear(val);
5869
5870 if (ret != NULL)
5871 {
5872 mpfr_clear(left_val);
5873 mpz_clear(right_val);
5874 return ret;
5875 }
5876 }
5877
5878 mpfr_clear(left_val);
5879 mpz_clear(right_val);
5880 }
5881
e9d3367e 5882 // Lower struct and array comparisons.
5883 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5884 {
5885 if (left->type()->struct_type() != NULL)
5886 return this->lower_struct_comparison(gogo, inserter);
5887 else if (left->type()->array_type() != NULL
5888 && !left->type()->is_slice_type())
5889 return this->lower_array_comparison(gogo, inserter);
5890 }
5891
e440a328 5892 return this;
5893}
5894
e9d3367e 5895// Lower a struct comparison.
5896
5897Expression*
5898Binary_expression::lower_struct_comparison(Gogo* gogo,
5899 Statement_inserter* inserter)
5900{
5901 Struct_type* st = this->left_->type()->struct_type();
5902 Struct_type* st2 = this->right_->type()->struct_type();
5903 if (st2 == NULL)
5904 return this;
5905 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5906 return this;
5907 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5908 this->right_->type(), NULL))
5909 return this;
5910
5911 // See if we can compare using memcmp. As a heuristic, we use
5912 // memcmp rather than field references and comparisons if there are
5913 // more than two fields.
113ef6a5 5914 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
e9d3367e 5915 return this->lower_compare_to_memcmp(gogo, inserter);
5916
5917 Location loc = this->location();
5918
5919 Expression* left = this->left_;
5920 Temporary_statement* left_temp = NULL;
5921 if (left->var_expression() == NULL
5922 && left->temporary_reference_expression() == NULL)
5923 {
5924 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5925 inserter->insert(left_temp);
5926 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5927 }
5928
5929 Expression* right = this->right_;
5930 Temporary_statement* right_temp = NULL;
5931 if (right->var_expression() == NULL
5932 && right->temporary_reference_expression() == NULL)
5933 {
5934 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5935 inserter->insert(right_temp);
5936 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5937 }
5938
5939 Expression* ret = Expression::make_boolean(true, loc);
5940 const Struct_field_list* fields = st->fields();
5941 unsigned int field_index = 0;
5942 for (Struct_field_list::const_iterator pf = fields->begin();
5943 pf != fields->end();
5944 ++pf, ++field_index)
5945 {
5946 if (field_index > 0)
5947 {
5948 if (left_temp == NULL)
5949 left = left->copy();
5950 else
5951 left = Expression::make_temporary_reference(left_temp, loc);
5952 if (right_temp == NULL)
5953 right = right->copy();
5954 else
5955 right = Expression::make_temporary_reference(right_temp, loc);
5956 }
5957 Expression* f1 = Expression::make_field_reference(left, field_index,
5958 loc);
5959 Expression* f2 = Expression::make_field_reference(right, field_index,
5960 loc);
5961 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5962 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5963 }
5964
5965 if (this->op_ == OPERATOR_NOTEQ)
5966 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5967
5968 return ret;
5969}
5970
5971// Lower an array comparison.
5972
5973Expression*
5974Binary_expression::lower_array_comparison(Gogo* gogo,
5975 Statement_inserter* inserter)
5976{
5977 Array_type* at = this->left_->type()->array_type();
5978 Array_type* at2 = this->right_->type()->array_type();
5979 if (at2 == NULL)
5980 return this;
5981 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5982 return this;
5983 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5984 this->right_->type(), NULL))
5985 return this;
5986
5987 // Call memcmp directly if possible. This may let the middle-end
5988 // optimize the call.
113ef6a5 5989 if (at->compare_is_identity(gogo))
e9d3367e 5990 return this->lower_compare_to_memcmp(gogo, inserter);
5991
5992 // Call the array comparison function.
5993 Named_object* hash_fn;
5994 Named_object* equal_fn;
5995 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5996 &hash_fn, &equal_fn);
5997
5998 Location loc = this->location();
5999
6000 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
6001
6002 Expression_list* args = new Expression_list();
6003 args->push_back(this->operand_address(inserter, this->left_));
6004 args->push_back(this->operand_address(inserter, this->right_));
6005 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
6006
6007 Expression* ret = Expression::make_call(func, args, false, loc);
6008
6009 if (this->op_ == OPERATOR_NOTEQ)
6010 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
6011
6012 return ret;
6013}
6014
6015// Lower a struct or array comparison to a call to memcmp.
6016
6017Expression*
6018Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
6019{
6020 Location loc = this->location();
6021
6022 Expression* a1 = this->operand_address(inserter, this->left_);
6023 Expression* a2 = this->operand_address(inserter, this->right_);
6024 Expression* len = Expression::make_type_info(this->left_->type(),
6025 TYPE_INFO_SIZE);
6026
6027 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
6028
6029 mpz_t zval;
6030 mpz_init_set_ui(zval, 0);
6031 Expression* zero = Expression::make_integer(&zval, NULL, loc);
6032 mpz_clear(zval);
6033
6034 return Expression::make_binary(this->op_, call, zero, loc);
6035}
6036
6037// Return the address of EXPR, cast to unsafe.Pointer.
6038
6039Expression*
6040Binary_expression::operand_address(Statement_inserter* inserter,
6041 Expression* expr)
6042{
6043 Location loc = this->location();
6044
6045 if (!expr->is_addressable())
6046 {
6047 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
6048 loc);
6049 inserter->insert(temp);
6050 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
6051 }
6052 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
6053 static_cast<Unary_expression*>(expr)->set_does_not_escape();
6054 Type* void_type = Type::make_void_type();
6055 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
6056 return Expression::make_cast(unsafe_pointer_type, expr, loc);
6057}
6058
e440a328 6059// Return the integer constant value, if it has one.
6060
6061bool
6062Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
6063 Type** ptype) const
6064{
6065 mpz_t left_val;
6066 mpz_init(left_val);
6067 Type* left_type;
6068 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
6069 &left_type))
6070 {
6071 mpz_clear(left_val);
6072 return false;
6073 }
6074
6075 mpz_t right_val;
6076 mpz_init(right_val);
6077 Type* right_type;
6078 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
6079 &right_type))
6080 {
6081 mpz_clear(right_val);
6082 mpz_clear(left_val);
6083 return false;
6084 }
6085
6086 bool ret;
6087 if (left_type != right_type
6088 && left_type != NULL
6089 && right_type != NULL
6090 && left_type->base() != right_type->base()
6091 && this->op_ != OPERATOR_RSHIFT
6092 && this->op_ != OPERATOR_LSHIFT)
6093 ret = false;
6094 else
6095 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
6096 right_type, right_val,
6097 this->location(), val);
6098
6099 mpz_clear(right_val);
6100 mpz_clear(left_val);
6101
6102 if (ret)
6103 *ptype = left_type;
6104
6105 return ret;
6106}
6107
6108// Return the floating point constant value, if it has one.
6109
6110bool
6111Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
6112{
6113 mpfr_t left_val;
6114 mpfr_init(left_val);
6115 Type* left_type;
6116 if (!this->left_->float_constant_value(left_val, &left_type))
6117 {
6118 mpfr_clear(left_val);
6119 return false;
6120 }
6121
6122 mpfr_t right_val;
6123 mpfr_init(right_val);
6124 Type* right_type;
6125 if (!this->right_->float_constant_value(right_val, &right_type))
6126 {
6127 mpfr_clear(right_val);
6128 mpfr_clear(left_val);
6129 return false;
6130 }
6131
6132 bool ret;
6133 if (left_type != right_type
6134 && left_type != NULL
6135 && right_type != NULL
6136 && left_type->base() != right_type->base())
6137 ret = false;
6138 else
6139 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
6140 right_type, right_val,
6141 val, this->location());
6142
6143 mpfr_clear(left_val);
6144 mpfr_clear(right_val);
6145
6146 if (ret)
6147 *ptype = left_type;
6148
6149 return ret;
6150}
6151
6152// Return the complex constant value, if it has one.
6153
6154bool
6155Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
6156 Type** ptype) const
6157{
6158 mpfr_t left_real;
6159 mpfr_t left_imag;
6160 mpfr_init(left_real);
6161 mpfr_init(left_imag);
6162 Type* left_type;
6163 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
6164 {
6165 mpfr_clear(left_real);
6166 mpfr_clear(left_imag);
6167 return false;
6168 }
6169
6170 mpfr_t right_real;
6171 mpfr_t right_imag;
6172 mpfr_init(right_real);
6173 mpfr_init(right_imag);
6174 Type* right_type;
6175 if (!this->right_->complex_constant_value(right_real, right_imag,
6176 &right_type))
6177 {
6178 mpfr_clear(left_real);
6179 mpfr_clear(left_imag);
6180 mpfr_clear(right_real);
6181 mpfr_clear(right_imag);
6182 return false;
6183 }
6184
6185 bool ret;
6186 if (left_type != right_type
6187 && left_type != NULL
6188 && right_type != NULL
6189 && left_type->base() != right_type->base())
6190 ret = false;
6191 else
6192 ret = Binary_expression::eval_complex(this->op_, left_type,
6193 left_real, left_imag,
6194 right_type,
6195 right_real, right_imag,
6196 real, imag,
6197 this->location());
6198 mpfr_clear(left_real);
6199 mpfr_clear(left_imag);
6200 mpfr_clear(right_real);
6201 mpfr_clear(right_imag);
6202
6203 if (ret)
6204 *ptype = left_type;
6205
6206 return ret;
6207}
6208
6209// Note that the value is being discarded.
6210
6211void
6212Binary_expression::do_discarding_value()
6213{
6214 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
6215 this->right_->discarding_value();
6216 else
a7549a6a 6217 this->unused_value_error();
e440a328 6218}
6219
6220// Get type.
6221
6222Type*
6223Binary_expression::do_type()
6224{
5f5fea79 6225 if (this->classification() == EXPRESSION_ERROR)
6226 return Type::make_error_type();
6227
e440a328 6228 switch (this->op_)
6229 {
6230 case OPERATOR_OROR:
6231 case OPERATOR_ANDAND:
6232 case OPERATOR_EQEQ:
6233 case OPERATOR_NOTEQ:
6234 case OPERATOR_LT:
6235 case OPERATOR_LE:
6236 case OPERATOR_GT:
6237 case OPERATOR_GE:
6238 return Type::lookup_bool_type();
6239
6240 case OPERATOR_PLUS:
6241 case OPERATOR_MINUS:
6242 case OPERATOR_OR:
6243 case OPERATOR_XOR:
6244 case OPERATOR_MULT:
6245 case OPERATOR_DIV:
6246 case OPERATOR_MOD:
6247 case OPERATOR_AND:
6248 case OPERATOR_BITCLEAR:
6249 {
6250 Type* left_type = this->left_->type();
6251 Type* right_type = this->right_->type();
5c13bd80 6252 if (left_type->is_error())
a5fe8571 6253 return left_type;
5c13bd80 6254 else if (right_type->is_error())
a5fe8571 6255 return right_type;
5f5fea79 6256 else if (!Type::are_compatible_for_binop(left_type, right_type))
6257 {
6258 this->report_error(_("incompatible types in binary expression"));
6259 return Type::make_error_type();
6260 }
a5fe8571 6261 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
e440a328 6262 return left_type;
6263 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
6264 return right_type;
6265 else if (!left_type->is_abstract())
6266 return left_type;
6267 else if (!right_type->is_abstract())
6268 return right_type;
6269 else if (left_type->complex_type() != NULL)
6270 return left_type;
6271 else if (right_type->complex_type() != NULL)
6272 return right_type;
6273 else if (left_type->float_type() != NULL)
6274 return left_type;
6275 else if (right_type->float_type() != NULL)
6276 return right_type;
cfdd67bc 6277 else if (left_type->integer_type() != NULL
6278 && left_type->integer_type()->is_rune())
6279 return left_type;
6280 else if (right_type->integer_type() != NULL
6281 && right_type->integer_type()->is_rune())
6282 return right_type;
e440a328 6283 else
6284 return left_type;
6285 }
6286
6287 case OPERATOR_LSHIFT:
6288 case OPERATOR_RSHIFT:
6289 return this->left_->type();
6290
6291 default:
c3e6f413 6292 go_unreachable();
e440a328 6293 }
6294}
6295
6296// Set type for a binary expression.
6297
6298void
6299Binary_expression::do_determine_type(const Type_context* context)
6300{
6301 Type* tleft = this->left_->type();
6302 Type* tright = this->right_->type();
6303
6304 // Both sides should have the same type, except for the shift
6305 // operations. For a comparison, we should ignore the incoming
6306 // type.
6307
6308 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
6309 || this->op_ == OPERATOR_RSHIFT);
6310
6311 bool is_comparison = (this->op_ == OPERATOR_EQEQ
6312 || this->op_ == OPERATOR_NOTEQ
6313 || this->op_ == OPERATOR_LT
6314 || this->op_ == OPERATOR_LE
6315 || this->op_ == OPERATOR_GT
6316 || this->op_ == OPERATOR_GE);
6317
6318 Type_context subcontext(*context);
6319
6320 if (is_comparison)
6321 {
6322 // In a comparison, the context does not determine the types of
6323 // the operands.
6324 subcontext.type = NULL;
6325 }
6326
6327 // Set the context for the left hand operand.
6328 if (is_shift_op)
6329 {
b40dc774 6330 // The right hand operand of a shift plays no role in
6331 // determining the type of the left hand operand.
e440a328 6332 }
6333 else if (!tleft->is_abstract())
6334 subcontext.type = tleft;
6335 else if (!tright->is_abstract())
6336 subcontext.type = tright;
6337 else if (subcontext.type == NULL)
6338 {
6339 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
6340 || (tleft->float_type() != NULL && tright->float_type() != NULL)
6341 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
6342 {
6343 // Both sides have an abstract integer, abstract float, or
6344 // abstract complex type. Just let CONTEXT determine
6345 // whether they may remain abstract or not.
6346 }
6347 else if (tleft->complex_type() != NULL)
6348 subcontext.type = tleft;
6349 else if (tright->complex_type() != NULL)
6350 subcontext.type = tright;
6351 else if (tleft->float_type() != NULL)
6352 subcontext.type = tleft;
6353 else if (tright->float_type() != NULL)
6354 subcontext.type = tright;
6355 else
6356 subcontext.type = tleft;
f58a23ae 6357
6358 if (subcontext.type != NULL && !context->may_be_abstract)
6359 subcontext.type = subcontext.type->make_non_abstract_type();
e440a328 6360 }
6361
6362 this->left_->determine_type(&subcontext);
6363
e440a328 6364 if (is_shift_op)
6365 {
b40dc774 6366 // We may have inherited an unusable type for the shift operand.
6367 // Give a useful error if that happened.
6368 if (tleft->is_abstract()
6369 && subcontext.type != NULL
6370 && (this->left_->type()->integer_type() == NULL
6371 || (subcontext.type->integer_type() == NULL
6372 && subcontext.type->float_type() == NULL
6373 && subcontext.type->complex_type() == NULL)))
6374 this->report_error(("invalid context-determined non-integer type "
6375 "for shift operand"));
6376
6377 // The context for the right hand operand is the same as for the
6378 // left hand operand, except for a shift operator.
e440a328 6379 subcontext.type = Type::lookup_integer_type("uint");
6380 subcontext.may_be_abstract = false;
6381 }
6382
6383 this->right_->determine_type(&subcontext);
6384}
6385
6386// Report an error if the binary operator OP does not support TYPE.
be8b5eee 6387// OTYPE is the type of the other operand. Return whether the
6388// operation is OK. This should not be used for shift.
e440a328 6389
6390bool
be8b5eee 6391Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
b13c66cd 6392 Location location)
e440a328 6393{
6394 switch (op)
6395 {
6396 case OPERATOR_OROR:
6397 case OPERATOR_ANDAND:
6398 if (!type->is_boolean_type())
6399 {
6400 error_at(location, "expected boolean type");
6401 return false;
6402 }
6403 break;
6404
6405 case OPERATOR_EQEQ:
6406 case OPERATOR_NOTEQ:
e9d3367e 6407 {
6408 std::string reason;
6409 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
6410 {
6411 error_at(location, "%s", reason.c_str());
6412 return false;
6413 }
6414 }
e440a328 6415 break;
6416
6417 case OPERATOR_LT:
6418 case OPERATOR_LE:
6419 case OPERATOR_GT:
6420 case OPERATOR_GE:
e9d3367e 6421 {
6422 std::string reason;
6423 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
6424 {
6425 error_at(location, "%s", reason.c_str());
6426 return false;
6427 }
6428 }
e440a328 6429 break;
6430
6431 case OPERATOR_PLUS:
6432 case OPERATOR_PLUSEQ:
6433 if (type->integer_type() == NULL
6434 && type->float_type() == NULL
6435 && type->complex_type() == NULL
6436 && !type->is_string_type())
6437 {
6438 error_at(location,
6439 "expected integer, floating, complex, or string type");
6440 return false;
6441 }
6442 break;
6443
6444 case OPERATOR_MINUS:
6445 case OPERATOR_MINUSEQ:
6446 case OPERATOR_MULT:
6447 case OPERATOR_MULTEQ:
6448 case OPERATOR_DIV:
6449 case OPERATOR_DIVEQ:
6450 if (type->integer_type() == NULL
6451 && type->float_type() == NULL
6452 && type->complex_type() == NULL)
6453 {
6454 error_at(location, "expected integer, floating, or complex type");
6455 return false;
6456 }
6457 break;
6458
6459 case OPERATOR_MOD:
6460 case OPERATOR_MODEQ:
6461 case OPERATOR_OR:
6462 case OPERATOR_OREQ:
6463 case OPERATOR_AND:
6464 case OPERATOR_ANDEQ:
6465 case OPERATOR_XOR:
6466 case OPERATOR_XOREQ:
6467 case OPERATOR_BITCLEAR:
6468 case OPERATOR_BITCLEAREQ:
6469 if (type->integer_type() == NULL)
6470 {
6471 error_at(location, "expected integer type");
6472 return false;
6473 }
6474 break;
6475
6476 default:
c3e6f413 6477 go_unreachable();
e440a328 6478 }
6479
6480 return true;
6481}
6482
6483// Check types.
6484
6485void
6486Binary_expression::do_check_types(Gogo*)
6487{
5f5fea79 6488 if (this->classification() == EXPRESSION_ERROR)
6489 return;
6490
e440a328 6491 Type* left_type = this->left_->type();
6492 Type* right_type = this->right_->type();
5c13bd80 6493 if (left_type->is_error() || right_type->is_error())
9fe897ef 6494 {
6495 this->set_is_error();
6496 return;
6497 }
e440a328 6498
6499 if (this->op_ == OPERATOR_EQEQ
6500 || this->op_ == OPERATOR_NOTEQ
6501 || this->op_ == OPERATOR_LT
6502 || this->op_ == OPERATOR_LE
6503 || this->op_ == OPERATOR_GT
6504 || this->op_ == OPERATOR_GE)
6505 {
6506 if (!Type::are_assignable(left_type, right_type, NULL)
6507 && !Type::are_assignable(right_type, left_type, NULL))
6508 {
6509 this->report_error(_("incompatible types in binary expression"));
6510 return;
6511 }
6512 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 6513 right_type,
e440a328 6514 this->location())
6515 || !Binary_expression::check_operator_type(this->op_, right_type,
be8b5eee 6516 left_type,
e440a328 6517 this->location()))
6518 {
6519 this->set_is_error();
6520 return;
6521 }
6522 }
6523 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
6524 {
6525 if (!Type::are_compatible_for_binop(left_type, right_type))
6526 {
6527 this->report_error(_("incompatible types in binary expression"));
6528 return;
6529 }
6530 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 6531 right_type,
e440a328 6532 this->location()))
6533 {
6534 this->set_is_error();
6535 return;
6536 }
6537 }
6538 else
6539 {
6540 if (left_type->integer_type() == NULL)
6541 this->report_error(_("shift of non-integer operand"));
6542
6543 if (!right_type->is_abstract()
6544 && (right_type->integer_type() == NULL
6545 || !right_type->integer_type()->is_unsigned()))
6546 this->report_error(_("shift count not unsigned integer"));
6547 else
6548 {
6549 mpz_t val;
6550 mpz_init(val);
6551 Type* type;
6552 if (this->right_->integer_constant_value(true, val, &type))
6553 {
6554 if (mpz_sgn(val) < 0)
a4eba91b 6555 {
6556 this->report_error(_("negative shift count"));
6557 mpz_set_ui(val, 0);
b13c66cd 6558 Location rloc = this->right_->location();
a4eba91b 6559 this->right_ = Expression::make_integer(&val, right_type,
6560 rloc);
6561 }
e440a328 6562 }
6563 mpz_clear(val);
6564 }
6565 }
6566}
6567
6568// Get a tree for a binary expression.
6569
6570tree
6571Binary_expression::do_get_tree(Translate_context* context)
6572{
6573 tree left = this->left_->get_tree(context);
6574 tree right = this->right_->get_tree(context);
6575
6576 if (left == error_mark_node || right == error_mark_node)
6577 return error_mark_node;
6578
6579 enum tree_code code;
6580 bool use_left_type = true;
6581 bool is_shift_op = false;
6582 switch (this->op_)
6583 {
6584 case OPERATOR_EQEQ:
6585 case OPERATOR_NOTEQ:
6586 case OPERATOR_LT:
6587 case OPERATOR_LE:
6588 case OPERATOR_GT:
6589 case OPERATOR_GE:
6590 return Expression::comparison_tree(context, this->op_,
6591 this->left_->type(), left,
6592 this->right_->type(), right,
6593 this->location());
6594
6595 case OPERATOR_OROR:
6596 code = TRUTH_ORIF_EXPR;
6597 use_left_type = false;
6598 break;
6599 case OPERATOR_ANDAND:
6600 code = TRUTH_ANDIF_EXPR;
6601 use_left_type = false;
6602 break;
6603 case OPERATOR_PLUS:
6604 code = PLUS_EXPR;
6605 break;
6606 case OPERATOR_MINUS:
6607 code = MINUS_EXPR;
6608 break;
6609 case OPERATOR_OR:
6610 code = BIT_IOR_EXPR;
6611 break;
6612 case OPERATOR_XOR:
6613 code = BIT_XOR_EXPR;
6614 break;
6615 case OPERATOR_MULT:
6616 code = MULT_EXPR;
6617 break;
6618 case OPERATOR_DIV:
6619 {
6620 Type *t = this->left_->type();
6621 if (t->float_type() != NULL || t->complex_type() != NULL)
6622 code = RDIV_EXPR;
6623 else
6624 code = TRUNC_DIV_EXPR;
6625 }
6626 break;
6627 case OPERATOR_MOD:
6628 code = TRUNC_MOD_EXPR;
6629 break;
6630 case OPERATOR_LSHIFT:
6631 code = LSHIFT_EXPR;
6632 is_shift_op = true;
6633 break;
6634 case OPERATOR_RSHIFT:
6635 code = RSHIFT_EXPR;
6636 is_shift_op = true;
6637 break;
6638 case OPERATOR_AND:
6639 code = BIT_AND_EXPR;
6640 break;
6641 case OPERATOR_BITCLEAR:
6642 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
6643 code = BIT_AND_EXPR;
6644 break;
6645 default:
c3e6f413 6646 go_unreachable();
e440a328 6647 }
6648
6649 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
6650
6651 if (this->left_->type()->is_string_type())
6652 {
c484d925 6653 go_assert(this->op_ == OPERATOR_PLUS);
9f0e0513 6654 Type* st = Type::make_string_type();
6655 tree string_type = type_to_tree(st->get_backend(context->gogo()));
e440a328 6656 static tree string_plus_decl;
6657 return Gogo::call_builtin(&string_plus_decl,
6658 this->location(),
6659 "__go_string_plus",
6660 2,
6661 string_type,
6662 string_type,
6663 left,
6664 string_type,
6665 right);
6666 }
6667
6668 tree compute_type = excess_precision_type(type);
6669 if (compute_type != NULL_TREE)
6670 {
6671 left = ::convert(compute_type, left);
6672 right = ::convert(compute_type, right);
6673 }
6674
6675 tree eval_saved = NULL_TREE;
6676 if (is_shift_op)
6677 {
e440a328 6678 // Make sure the values are evaluated.
a7a70f31 6679 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6680 {
6681 left = save_expr(left);
6682 eval_saved = left;
6683 }
6684 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6685 {
6686 right = save_expr(right);
6687 if (eval_saved == NULL_TREE)
6688 eval_saved = right;
6689 else
b13c66cd 6690 eval_saved = fold_build2_loc(this->location().gcc_location(),
6691 COMPOUND_EXPR,
a7a70f31 6692 void_type_node, eval_saved, right);
6693 }
e440a328 6694 }
6695
b13c66cd 6696 tree ret = fold_build2_loc(this->location().gcc_location(),
e440a328 6697 code,
6698 compute_type != NULL_TREE ? compute_type : type,
6699 left, right);
6700
6701 if (compute_type != NULL_TREE)
6702 ret = ::convert(type, ret);
6703
6704 // In Go, a shift larger than the size of the type is well-defined.
6705 // This is not true in GENERIC, so we need to insert a conditional.
6706 if (is_shift_op)
6707 {
c484d925 6708 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6709 go_assert(this->left_->type()->integer_type() != NULL);
e440a328 6710 int bits = TYPE_PRECISION(TREE_TYPE(left));
6711
6712 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6713 build_int_cst_type(TREE_TYPE(right), bits));
6714
b13c66cd 6715 tree overflow_result = fold_convert_loc(this->location().gcc_location(),
e440a328 6716 TREE_TYPE(left),
6717 integer_zero_node);
6718 if (this->op_ == OPERATOR_RSHIFT
6719 && !this->left_->type()->integer_type()->is_unsigned())
6720 {
b13c66cd 6721 tree neg =
6722 fold_build2_loc(this->location().gcc_location(), LT_EXPR,
6723 boolean_type_node, left,
6724 fold_convert_loc(this->location().gcc_location(),
6725 TREE_TYPE(left),
6726 integer_zero_node));
6727 tree neg_one =
6728 fold_build2_loc(this->location().gcc_location(),
6729 MINUS_EXPR, TREE_TYPE(left),
6730 fold_convert_loc(this->location().gcc_location(),
6731 TREE_TYPE(left),
6732 integer_zero_node),
6733 fold_convert_loc(this->location().gcc_location(),
6734 TREE_TYPE(left),
6735 integer_one_node));
6736 overflow_result =
6737 fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6738 TREE_TYPE(left), neg, neg_one,
6739 overflow_result);
6740 }
6741
6742 ret = fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6743 TREE_TYPE(left), compare, ret, overflow_result);
e440a328 6744
a7a70f31 6745 if (eval_saved != NULL_TREE)
b13c66cd 6746 ret = fold_build2_loc(this->location().gcc_location(), COMPOUND_EXPR,
a7a70f31 6747 TREE_TYPE(ret), eval_saved, ret);
e440a328 6748 }
6749
6750 return ret;
6751}
6752
6753// Export a binary expression.
6754
6755void
6756Binary_expression::do_export(Export* exp) const
6757{
6758 exp->write_c_string("(");
6759 this->left_->export_expression(exp);
6760 switch (this->op_)
6761 {
6762 case OPERATOR_OROR:
6763 exp->write_c_string(" || ");
6764 break;
6765 case OPERATOR_ANDAND:
6766 exp->write_c_string(" && ");
6767 break;
6768 case OPERATOR_EQEQ:
6769 exp->write_c_string(" == ");
6770 break;
6771 case OPERATOR_NOTEQ:
6772 exp->write_c_string(" != ");
6773 break;
6774 case OPERATOR_LT:
6775 exp->write_c_string(" < ");
6776 break;
6777 case OPERATOR_LE:
6778 exp->write_c_string(" <= ");
6779 break;
6780 case OPERATOR_GT:
6781 exp->write_c_string(" > ");
6782 break;
6783 case OPERATOR_GE:
6784 exp->write_c_string(" >= ");
6785 break;
6786 case OPERATOR_PLUS:
6787 exp->write_c_string(" + ");
6788 break;
6789 case OPERATOR_MINUS:
6790 exp->write_c_string(" - ");
6791 break;
6792 case OPERATOR_OR:
6793 exp->write_c_string(" | ");
6794 break;
6795 case OPERATOR_XOR:
6796 exp->write_c_string(" ^ ");
6797 break;
6798 case OPERATOR_MULT:
6799 exp->write_c_string(" * ");
6800 break;
6801 case OPERATOR_DIV:
6802 exp->write_c_string(" / ");
6803 break;
6804 case OPERATOR_MOD:
6805 exp->write_c_string(" % ");
6806 break;
6807 case OPERATOR_LSHIFT:
6808 exp->write_c_string(" << ");
6809 break;
6810 case OPERATOR_RSHIFT:
6811 exp->write_c_string(" >> ");
6812 break;
6813 case OPERATOR_AND:
6814 exp->write_c_string(" & ");
6815 break;
6816 case OPERATOR_BITCLEAR:
6817 exp->write_c_string(" &^ ");
6818 break;
6819 default:
c3e6f413 6820 go_unreachable();
e440a328 6821 }
6822 this->right_->export_expression(exp);
6823 exp->write_c_string(")");
6824}
6825
6826// Import a binary expression.
6827
6828Expression*
6829Binary_expression::do_import(Import* imp)
6830{
6831 imp->require_c_string("(");
6832
6833 Expression* left = Expression::import_expression(imp);
6834
6835 Operator op;
6836 if (imp->match_c_string(" || "))
6837 {
6838 op = OPERATOR_OROR;
6839 imp->advance(4);
6840 }
6841 else if (imp->match_c_string(" && "))
6842 {
6843 op = OPERATOR_ANDAND;
6844 imp->advance(4);
6845 }
6846 else if (imp->match_c_string(" == "))
6847 {
6848 op = OPERATOR_EQEQ;
6849 imp->advance(4);
6850 }
6851 else if (imp->match_c_string(" != "))
6852 {
6853 op = OPERATOR_NOTEQ;
6854 imp->advance(4);
6855 }
6856 else if (imp->match_c_string(" < "))
6857 {
6858 op = OPERATOR_LT;
6859 imp->advance(3);
6860 }
6861 else if (imp->match_c_string(" <= "))
6862 {
6863 op = OPERATOR_LE;
6864 imp->advance(4);
6865 }
6866 else if (imp->match_c_string(" > "))
6867 {
6868 op = OPERATOR_GT;
6869 imp->advance(3);
6870 }
6871 else if (imp->match_c_string(" >= "))
6872 {
6873 op = OPERATOR_GE;
6874 imp->advance(4);
6875 }
6876 else if (imp->match_c_string(" + "))
6877 {
6878 op = OPERATOR_PLUS;
6879 imp->advance(3);
6880 }
6881 else if (imp->match_c_string(" - "))
6882 {
6883 op = OPERATOR_MINUS;
6884 imp->advance(3);
6885 }
6886 else if (imp->match_c_string(" | "))
6887 {
6888 op = OPERATOR_OR;
6889 imp->advance(3);
6890 }
6891 else if (imp->match_c_string(" ^ "))
6892 {
6893 op = OPERATOR_XOR;
6894 imp->advance(3);
6895 }
6896 else if (imp->match_c_string(" * "))
6897 {
6898 op = OPERATOR_MULT;
6899 imp->advance(3);
6900 }
6901 else if (imp->match_c_string(" / "))
6902 {
6903 op = OPERATOR_DIV;
6904 imp->advance(3);
6905 }
6906 else if (imp->match_c_string(" % "))
6907 {
6908 op = OPERATOR_MOD;
6909 imp->advance(3);
6910 }
6911 else if (imp->match_c_string(" << "))
6912 {
6913 op = OPERATOR_LSHIFT;
6914 imp->advance(4);
6915 }
6916 else if (imp->match_c_string(" >> "))
6917 {
6918 op = OPERATOR_RSHIFT;
6919 imp->advance(4);
6920 }
6921 else if (imp->match_c_string(" & "))
6922 {
6923 op = OPERATOR_AND;
6924 imp->advance(3);
6925 }
6926 else if (imp->match_c_string(" &^ "))
6927 {
6928 op = OPERATOR_BITCLEAR;
6929 imp->advance(4);
6930 }
6931 else
6932 {
6933 error_at(imp->location(), "unrecognized binary operator");
6934 return Expression::make_error(imp->location());
6935 }
6936
6937 Expression* right = Expression::import_expression(imp);
6938
6939 imp->require_c_string(")");
6940
6941 return Expression::make_binary(op, left, right, imp->location());
6942}
6943
d751bb78 6944// Dump ast representation of a binary expression.
6945
6946void
6947Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6948{
6949 ast_dump_context->ostream() << "(";
6950 ast_dump_context->dump_expression(this->left_);
6951 ast_dump_context->ostream() << " ";
6952 ast_dump_context->dump_operator(this->op_);
6953 ast_dump_context->ostream() << " ";
6954 ast_dump_context->dump_expression(this->right_);
6955 ast_dump_context->ostream() << ") ";
6956}
6957
e440a328 6958// Make a binary expression.
6959
6960Expression*
6961Expression::make_binary(Operator op, Expression* left, Expression* right,
b13c66cd 6962 Location location)
e440a328 6963{
6964 return new Binary_expression(op, left, right, location);
6965}
6966
6967// Implement a comparison.
6968
6969tree
6970Expression::comparison_tree(Translate_context* context, Operator op,
6971 Type* left_type, tree left_tree,
6972 Type* right_type, tree right_tree,
b13c66cd 6973 Location location)
e440a328 6974{
6975 enum tree_code code;
6976 switch (op)
6977 {
6978 case OPERATOR_EQEQ:
6979 code = EQ_EXPR;
6980 break;
6981 case OPERATOR_NOTEQ:
6982 code = NE_EXPR;
6983 break;
6984 case OPERATOR_LT:
6985 code = LT_EXPR;
6986 break;
6987 case OPERATOR_LE:
6988 code = LE_EXPR;
6989 break;
6990 case OPERATOR_GT:
6991 code = GT_EXPR;
6992 break;
6993 case OPERATOR_GE:
6994 code = GE_EXPR;
6995 break;
6996 default:
c3e6f413 6997 go_unreachable();
e440a328 6998 }
6999
15c67ee2 7000 if (left_type->is_string_type() && right_type->is_string_type())
e440a328 7001 {
9f0e0513 7002 Type* st = Type::make_string_type();
7003 tree string_type = type_to_tree(st->get_backend(context->gogo()));
e440a328 7004 static tree string_compare_decl;
7005 left_tree = Gogo::call_builtin(&string_compare_decl,
7006 location,
7007 "__go_strcmp",
7008 2,
7009 integer_type_node,
7010 string_type,
7011 left_tree,
7012 string_type,
7013 right_tree);
7014 right_tree = build_int_cst_type(integer_type_node, 0);
7015 }
15c67ee2 7016 else if ((left_type->interface_type() != NULL
7017 && right_type->interface_type() == NULL
7018 && !right_type->is_nil_type())
7019 || (left_type->interface_type() == NULL
7020 && !left_type->is_nil_type()
7021 && right_type->interface_type() != NULL))
e440a328 7022 {
7023 // Comparing an interface value to a non-interface value.
7024 if (left_type->interface_type() == NULL)
7025 {
7026 std::swap(left_type, right_type);
7027 std::swap(left_tree, right_tree);
7028 }
7029
7030 // The right operand is not an interface. We need to take its
7031 // address if it is not a pointer.
7032 tree make_tmp;
7033 tree arg;
7034 if (right_type->points_to() != NULL)
7035 {
7036 make_tmp = NULL_TREE;
7037 arg = right_tree;
7038 }
7039 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
7040 {
7041 make_tmp = NULL_TREE;
b13c66cd 7042 arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree);
e440a328 7043 if (DECL_P(right_tree))
7044 TREE_ADDRESSABLE(right_tree) = 1;
7045 }
7046 else
7047 {
7048 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
7049 get_name(right_tree));
7050 DECL_IGNORED_P(tmp) = 0;
7051 DECL_INITIAL(tmp) = right_tree;
7052 TREE_ADDRESSABLE(tmp) = 1;
7053 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
b13c66cd 7054 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
7055 arg = build_fold_addr_expr_loc(location.gcc_location(), tmp);
e440a328 7056 }
b13c66cd 7057 arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg);
e440a328 7058
a1d23b41 7059 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
7060 location);
e440a328 7061
7062 if (left_type->interface_type()->is_empty())
7063 {
7064 static tree empty_interface_value_compare_decl;
7065 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
7066 location,
7067 "__go_empty_interface_value_compare",
7068 3,
7069 integer_type_node,
7070 TREE_TYPE(left_tree),
7071 left_tree,
7072 TREE_TYPE(descriptor),
7073 descriptor,
7074 ptr_type_node,
7075 arg);
5fb82b5e 7076 if (left_tree == error_mark_node)
7077 return error_mark_node;
e440a328 7078 // This can panic if the type is not comparable.
7079 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
7080 }
7081 else
7082 {
7083 static tree interface_value_compare_decl;
7084 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
7085 location,
7086 "__go_interface_value_compare",
7087 3,
7088 integer_type_node,
7089 TREE_TYPE(left_tree),
7090 left_tree,
7091 TREE_TYPE(descriptor),
7092 descriptor,
7093 ptr_type_node,
7094 arg);
5fb82b5e 7095 if (left_tree == error_mark_node)
7096 return error_mark_node;
e440a328 7097 // This can panic if the type is not comparable.
7098 TREE_NOTHROW(interface_value_compare_decl) = 0;
7099 }
7100 right_tree = build_int_cst_type(integer_type_node, 0);
7101
7102 if (make_tmp != NULL_TREE)
7103 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
7104 left_tree);
7105 }
7106 else if (left_type->interface_type() != NULL
7107 && right_type->interface_type() != NULL)
7108 {
739bad04 7109 if (left_type->interface_type()->is_empty()
7110 && right_type->interface_type()->is_empty())
e440a328 7111 {
e440a328 7112 static tree empty_interface_compare_decl;
7113 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
7114 location,
7115 "__go_empty_interface_compare",
7116 2,
7117 integer_type_node,
7118 TREE_TYPE(left_tree),
7119 left_tree,
7120 TREE_TYPE(right_tree),
7121 right_tree);
5fb82b5e 7122 if (left_tree == error_mark_node)
7123 return error_mark_node;
e440a328 7124 // This can panic if the type is uncomparable.
7125 TREE_NOTHROW(empty_interface_compare_decl) = 0;
7126 }
739bad04 7127 else if (!left_type->interface_type()->is_empty()
7128 && !right_type->interface_type()->is_empty())
e440a328 7129 {
e440a328 7130 static tree interface_compare_decl;
7131 left_tree = Gogo::call_builtin(&interface_compare_decl,
7132 location,
7133 "__go_interface_compare",
7134 2,
7135 integer_type_node,
7136 TREE_TYPE(left_tree),
7137 left_tree,
7138 TREE_TYPE(right_tree),
7139 right_tree);
5fb82b5e 7140 if (left_tree == error_mark_node)
7141 return error_mark_node;
e440a328 7142 // This can panic if the type is uncomparable.
7143 TREE_NOTHROW(interface_compare_decl) = 0;
7144 }
739bad04 7145 else
7146 {
7147 if (left_type->interface_type()->is_empty())
7148 {
c484d925 7149 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
739bad04 7150 std::swap(left_type, right_type);
7151 std::swap(left_tree, right_tree);
7152 }
c484d925 7153 go_assert(!left_type->interface_type()->is_empty());
7154 go_assert(right_type->interface_type()->is_empty());
739bad04 7155 static tree interface_empty_compare_decl;
7156 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
7157 location,
7158 "__go_interface_empty_compare",
7159 2,
7160 integer_type_node,
7161 TREE_TYPE(left_tree),
7162 left_tree,
7163 TREE_TYPE(right_tree),
7164 right_tree);
7165 if (left_tree == error_mark_node)
7166 return error_mark_node;
7167 // This can panic if the type is uncomparable.
7168 TREE_NOTHROW(interface_empty_compare_decl) = 0;
7169 }
7170
e440a328 7171 right_tree = build_int_cst_type(integer_type_node, 0);
7172 }
7173
7174 if (left_type->is_nil_type()
7175 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
7176 {
7177 std::swap(left_type, right_type);
7178 std::swap(left_tree, right_tree);
7179 }
7180
7181 if (right_type->is_nil_type())
7182 {
7183 if (left_type->array_type() != NULL
7184 && left_type->array_type()->length() == NULL)
7185 {
7186 Array_type* at = left_type->array_type();
7187 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
7188 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7189 }
7190 else if (left_type->interface_type() != NULL)
7191 {
7192 // An interface is nil if the first field is nil.
7193 tree left_type_tree = TREE_TYPE(left_tree);
c484d925 7194 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
e440a328 7195 tree field = TYPE_FIELDS(left_type_tree);
7196 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
7197 field, NULL_TREE);
7198 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7199 }
7200 else
7201 {
c484d925 7202 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
e440a328 7203 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7204 }
7205 }
7206
d8ccb1e3 7207 if (left_tree == error_mark_node || right_tree == error_mark_node)
7208 return error_mark_node;
7209
e440a328 7210 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
7211 if (CAN_HAVE_LOCATION_P(ret))
b13c66cd 7212 SET_EXPR_LOCATION(ret, location.gcc_location());
e440a328 7213 return ret;
7214}
7215
7216// Class Bound_method_expression.
7217
7218// Traversal.
7219
7220int
7221Bound_method_expression::do_traverse(Traverse* traverse)
7222{
e0659c9e 7223 return Expression::traverse(&this->expr_, traverse);
e440a328 7224}
7225
7226// Return the type of a bound method expression. The type of this
7227// object is really the type of the method with no receiver. We
7228// should be able to get away with just returning the type of the
7229// method.
7230
7231Type*
7232Bound_method_expression::do_type()
7233{
e0659c9e 7234 if (this->method_->is_function())
7235 return this->method_->func_value()->type();
7236 else if (this->method_->is_function_declaration())
7237 return this->method_->func_declaration_value()->type();
7238 else
7239 return Type::make_error_type();
e440a328 7240}
7241
7242// Determine the types of a method expression.
7243
7244void
7245Bound_method_expression::do_determine_type(const Type_context*)
7246{
e0659c9e 7247 Function_type* fntype = this->type()->function_type();
e440a328 7248 if (fntype == NULL || !fntype->is_method())
7249 this->expr_->determine_type_no_context();
7250 else
7251 {
7252 Type_context subcontext(fntype->receiver()->type(), false);
7253 this->expr_->determine_type(&subcontext);
7254 }
7255}
7256
7257// Check the types of a method expression.
7258
7259void
7260Bound_method_expression::do_check_types(Gogo*)
7261{
e0659c9e 7262 if (!this->method_->is_function()
7263 && !this->method_->is_function_declaration())
e440a328 7264 this->report_error(_("object is not a method"));
7265 else
7266 {
e0659c9e 7267 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
e440a328 7268 Type* etype = (this->expr_type_ != NULL
7269 ? this->expr_type_
7270 : this->expr_->type());
7271 etype = etype->deref();
07ba8be5 7272 if (!Type::are_identical(rtype, etype, true, NULL))
e440a328 7273 this->report_error(_("method type does not match object type"));
7274 }
7275}
7276
7277// Get the tree for a method expression. There is no standard tree
7278// representation for this. The only places it may currently be used
7279// are in a Call_expression or a Go_statement, which will take it
7280// apart directly. So this has nothing to do at present.
7281
7282tree
7283Bound_method_expression::do_get_tree(Translate_context*)
7284{
d40405e2 7285 error_at(this->location(), "reference to method other than calling it");
7286 return error_mark_node;
e440a328 7287}
7288
d751bb78 7289// Dump ast representation of a bound method expression.
7290
7291void
7292Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
7293 const
7294{
7295 if (this->expr_type_ != NULL)
7296 ast_dump_context->ostream() << "(";
7297 ast_dump_context->dump_expression(this->expr_);
7298 if (this->expr_type_ != NULL)
7299 {
7300 ast_dump_context->ostream() << ":";
7301 ast_dump_context->dump_type(this->expr_type_);
7302 ast_dump_context->ostream() << ")";
7303 }
7304
e0659c9e 7305 ast_dump_context->ostream() << "." << this->method_->name();
d751bb78 7306}
7307
e440a328 7308// Make a method expression.
7309
7310Bound_method_expression*
e0659c9e 7311Expression::make_bound_method(Expression* expr, Named_object* method,
b13c66cd 7312 Location location)
e440a328 7313{
7314 return new Bound_method_expression(expr, method, location);
7315}
7316
7317// Class Builtin_call_expression. This is used for a call to a
7318// builtin function.
7319
7320class Builtin_call_expression : public Call_expression
7321{
7322 public:
7323 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
b13c66cd 7324 bool is_varargs, Location location);
e440a328 7325
7326 protected:
7327 // This overrides Call_expression::do_lower.
7328 Expression*
ceeb4318 7329 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 7330
7331 bool
7332 do_is_constant() const;
7333
7334 bool
7335 do_integer_constant_value(bool, mpz_t, Type**) const;
7336
7337 bool
7338 do_float_constant_value(mpfr_t, Type**) const;
7339
7340 bool
7341 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
7342
a7549a6a 7343 void
7344 do_discarding_value();
7345
e440a328 7346 Type*
7347 do_type();
7348
7349 void
7350 do_determine_type(const Type_context*);
7351
7352 void
7353 do_check_types(Gogo*);
7354
7355 Expression*
7356 do_copy()
7357 {
7358 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
7359 this->args()->copy(),
7360 this->is_varargs(),
7361 this->location());
7362 }
7363
7364 tree
7365 do_get_tree(Translate_context*);
7366
7367 void
7368 do_export(Export*) const;
7369
7370 virtual bool
7371 do_is_recover_call() const;
7372
7373 virtual void
7374 do_set_recover_arg(Expression*);
7375
7376 private:
7377 // The builtin functions.
7378 enum Builtin_function_code
7379 {
7380 BUILTIN_INVALID,
7381
7382 // Predeclared builtin functions.
7383 BUILTIN_APPEND,
7384 BUILTIN_CAP,
7385 BUILTIN_CLOSE,
48080209 7386 BUILTIN_COMPLEX,
e440a328 7387 BUILTIN_COPY,
1cce762f 7388 BUILTIN_DELETE,
e440a328 7389 BUILTIN_IMAG,
7390 BUILTIN_LEN,
7391 BUILTIN_MAKE,
7392 BUILTIN_NEW,
7393 BUILTIN_PANIC,
7394 BUILTIN_PRINT,
7395 BUILTIN_PRINTLN,
7396 BUILTIN_REAL,
7397 BUILTIN_RECOVER,
7398
7399 // Builtin functions from the unsafe package.
7400 BUILTIN_ALIGNOF,
7401 BUILTIN_OFFSETOF,
7402 BUILTIN_SIZEOF
7403 };
7404
7405 Expression*
7406 one_arg() const;
7407
7408 bool
7409 check_one_arg();
7410
7411 static Type*
7412 real_imag_type(Type*);
7413
7414 static Type*
48080209 7415 complex_type(Type*);
e440a328 7416
a9182619 7417 Expression*
7418 lower_make();
7419
7420 bool
7421 check_int_value(Expression*);
7422
e440a328 7423 // A pointer back to the general IR structure. This avoids a global
7424 // variable, or passing it around everywhere.
7425 Gogo* gogo_;
7426 // The builtin function being called.
7427 Builtin_function_code code_;
0f914071 7428 // Used to stop endless loops when the length of an array uses len
7429 // or cap of the array itself.
7430 mutable bool seen_;
e440a328 7431};
7432
7433Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7434 Expression* fn,
7435 Expression_list* args,
7436 bool is_varargs,
b13c66cd 7437 Location location)
e440a328 7438 : Call_expression(fn, args, is_varargs, location),
0f914071 7439 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
e440a328 7440{
7441 Func_expression* fnexp = this->fn()->func_expression();
c484d925 7442 go_assert(fnexp != NULL);
e440a328 7443 const std::string& name(fnexp->named_object()->name());
7444 if (name == "append")
7445 this->code_ = BUILTIN_APPEND;
7446 else if (name == "cap")
7447 this->code_ = BUILTIN_CAP;
7448 else if (name == "close")
7449 this->code_ = BUILTIN_CLOSE;
48080209 7450 else if (name == "complex")
7451 this->code_ = BUILTIN_COMPLEX;
e440a328 7452 else if (name == "copy")
7453 this->code_ = BUILTIN_COPY;
1cce762f 7454 else if (name == "delete")
7455 this->code_ = BUILTIN_DELETE;
e440a328 7456 else if (name == "imag")
7457 this->code_ = BUILTIN_IMAG;
7458 else if (name == "len")
7459 this->code_ = BUILTIN_LEN;
7460 else if (name == "make")
7461 this->code_ = BUILTIN_MAKE;
7462 else if (name == "new")
7463 this->code_ = BUILTIN_NEW;
7464 else if (name == "panic")
7465 this->code_ = BUILTIN_PANIC;
7466 else if (name == "print")
7467 this->code_ = BUILTIN_PRINT;
7468 else if (name == "println")
7469 this->code_ = BUILTIN_PRINTLN;
7470 else if (name == "real")
7471 this->code_ = BUILTIN_REAL;
7472 else if (name == "recover")
7473 this->code_ = BUILTIN_RECOVER;
7474 else if (name == "Alignof")
7475 this->code_ = BUILTIN_ALIGNOF;
7476 else if (name == "Offsetof")
7477 this->code_ = BUILTIN_OFFSETOF;
7478 else if (name == "Sizeof")
7479 this->code_ = BUILTIN_SIZEOF;
7480 else
c3e6f413 7481 go_unreachable();
e440a328 7482}
7483
7484// Return whether this is a call to recover. This is a virtual
7485// function called from the parent class.
7486
7487bool
7488Builtin_call_expression::do_is_recover_call() const
7489{
7490 if (this->classification() == EXPRESSION_ERROR)
7491 return false;
7492 return this->code_ == BUILTIN_RECOVER;
7493}
7494
7495// Set the argument for a call to recover.
7496
7497void
7498Builtin_call_expression::do_set_recover_arg(Expression* arg)
7499{
7500 const Expression_list* args = this->args();
c484d925 7501 go_assert(args == NULL || args->empty());
e440a328 7502 Expression_list* new_args = new Expression_list();
7503 new_args->push_back(arg);
7504 this->set_args(new_args);
7505}
7506
7507// A traversal class which looks for a call expression.
7508
7509class Find_call_expression : public Traverse
7510{
7511 public:
7512 Find_call_expression()
7513 : Traverse(traverse_expressions),
7514 found_(false)
7515 { }
7516
7517 int
7518 expression(Expression**);
7519
7520 bool
7521 found()
7522 { return this->found_; }
7523
7524 private:
7525 bool found_;
7526};
7527
7528int
7529Find_call_expression::expression(Expression** pexpr)
7530{
7531 if ((*pexpr)->call_expression() != NULL)
7532 {
7533 this->found_ = true;
7534 return TRAVERSE_EXIT;
7535 }
7536 return TRAVERSE_CONTINUE;
7537}
7538
7539// Lower a builtin call expression. This turns new and make into
7540// specific expressions. We also convert to a constant if we can.
7541
7542Expression*
ceeb4318 7543Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
7544 Statement_inserter* inserter, int)
e440a328 7545{
a9182619 7546 if (this->classification() == EXPRESSION_ERROR)
7547 return this;
7548
b13c66cd 7549 Location loc = this->location();
1cce762f 7550
a8725655 7551 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7552 {
7553 this->report_error(_("invalid use of %<...%> with builtin function"));
1cce762f 7554 return Expression::make_error(loc);
a8725655 7555 }
7556
1cce762f 7557 if (this->is_constant())
e440a328 7558 {
7559 // We can only lower len and cap if there are no function calls
7560 // in the arguments. Otherwise we have to make the call.
7561 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
7562 {
7563 Expression* arg = this->one_arg();
7564 if (!arg->is_constant())
7565 {
7566 Find_call_expression find_call;
7567 Expression::traverse(&arg, &find_call);
7568 if (find_call.found())
7569 return this;
7570 }
7571 }
7572
7573 mpz_t ival;
7574 mpz_init(ival);
7575 Type* type;
7576 if (this->integer_constant_value(true, ival, &type))
7577 {
1cce762f 7578 Expression* ret = Expression::make_integer(&ival, type, loc);
e440a328 7579 mpz_clear(ival);
7580 return ret;
7581 }
7582 mpz_clear(ival);
7583
7584 mpfr_t rval;
7585 mpfr_init(rval);
7586 if (this->float_constant_value(rval, &type))
7587 {
1cce762f 7588 Expression* ret = Expression::make_float(&rval, type, loc);
e440a328 7589 mpfr_clear(rval);
7590 return ret;
7591 }
7592
7593 mpfr_t imag;
7594 mpfr_init(imag);
7595 if (this->complex_constant_value(rval, imag, &type))
7596 {
1cce762f 7597 Expression* ret = Expression::make_complex(&rval, &imag, type, loc);
e440a328 7598 mpfr_clear(rval);
7599 mpfr_clear(imag);
7600 return ret;
7601 }
7602 mpfr_clear(rval);
7603 mpfr_clear(imag);
7604 }
1cce762f 7605
7606 switch (this->code_)
e440a328 7607 {
1cce762f 7608 default:
7609 break;
7610
7611 case BUILTIN_NEW:
7612 {
7613 const Expression_list* args = this->args();
7614 if (args == NULL || args->size() < 1)
7615 this->report_error(_("not enough arguments"));
7616 else if (args->size() > 1)
7617 this->report_error(_("too many arguments"));
7618 else
7619 {
7620 Expression* arg = args->front();
7621 if (!arg->is_type_expression())
7622 {
7623 error_at(arg->location(), "expected type");
7624 this->set_is_error();
7625 }
7626 else
7627 return Expression::make_allocation(arg->type(), loc);
7628 }
7629 }
7630 break;
7631
7632 case BUILTIN_MAKE:
7633 return this->lower_make();
7634
7635 case BUILTIN_RECOVER:
e440a328 7636 if (function != NULL)
7637 function->func_value()->set_calls_recover();
7638 else
7639 {
7640 // Calling recover outside of a function always returns the
7641 // nil empty interface.
823c7e3d 7642 Type* eface = Type::make_empty_interface_type(loc);
1cce762f 7643 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
e440a328 7644 }
1cce762f 7645 break;
7646
7647 case BUILTIN_APPEND:
7648 {
7649 // Lower the varargs.
7650 const Expression_list* args = this->args();
7651 if (args == NULL || args->empty())
e440a328 7652 return this;
1cce762f 7653 Type* slice_type = args->front()->type();
7654 if (!slice_type->is_slice_type())
7655 {
7656 error_at(args->front()->location(), "argument 1 must be a slice");
7657 this->set_is_error();
7658 return this;
7659 }
19fd40c3 7660 Type* element_type = slice_type->array_type()->element_type();
7661 this->lower_varargs(gogo, function, inserter,
7662 Type::make_array_type(element_type, NULL),
7663 2);
1cce762f 7664 }
7665 break;
7666
7667 case BUILTIN_DELETE:
7668 {
7669 // Lower to a runtime function call.
7670 const Expression_list* args = this->args();
7671 if (args == NULL || args->size() < 2)
7672 this->report_error(_("not enough arguments"));
7673 else if (args->size() > 2)
7674 this->report_error(_("too many arguments"));
7675 else if (args->front()->type()->map_type() == NULL)
7676 this->report_error(_("argument 1 must be a map"));
7677 else
7678 {
7679 // Since this function returns no value it must appear in
7680 // a statement by itself, so we don't have to worry about
7681 // order of evaluation of values around it. Evaluate the
7682 // map first to get order of evaluation right.
7683 Map_type* mt = args->front()->type()->map_type();
7684 Temporary_statement* map_temp =
7685 Statement::make_temporary(mt, args->front(), loc);
7686 inserter->insert(map_temp);
7687
7688 Temporary_statement* key_temp =
7689 Statement::make_temporary(mt->key_type(), args->back(), loc);
7690 inserter->insert(key_temp);
7691
7692 Expression* e1 = Expression::make_temporary_reference(map_temp,
7693 loc);
7694 Expression* e2 = Expression::make_temporary_reference(key_temp,
7695 loc);
7696 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
7697 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
7698 2, e1, e2);
7699 }
7700 }
7701 break;
e440a328 7702 }
7703
7704 return this;
7705}
7706
a9182619 7707// Lower a make expression.
7708
7709Expression*
7710Builtin_call_expression::lower_make()
7711{
b13c66cd 7712 Location loc = this->location();
a9182619 7713
7714 const Expression_list* args = this->args();
7715 if (args == NULL || args->size() < 1)
7716 {
7717 this->report_error(_("not enough arguments"));
7718 return Expression::make_error(this->location());
7719 }
7720
7721 Expression_list::const_iterator parg = args->begin();
7722
7723 Expression* first_arg = *parg;
7724 if (!first_arg->is_type_expression())
7725 {
7726 error_at(first_arg->location(), "expected type");
7727 this->set_is_error();
7728 return Expression::make_error(this->location());
7729 }
7730 Type* type = first_arg->type();
7731
7732 bool is_slice = false;
7733 bool is_map = false;
7734 bool is_chan = false;
411eb89e 7735 if (type->is_slice_type())
a9182619 7736 is_slice = true;
7737 else if (type->map_type() != NULL)
7738 is_map = true;
7739 else if (type->channel_type() != NULL)
7740 is_chan = true;
7741 else
7742 {
7743 this->report_error(_("invalid type for make function"));
7744 return Expression::make_error(this->location());
7745 }
7746
ac84c822 7747 bool have_big_args = false;
7748 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7749 int uintptr_bits = uintptr_type->integer_type()->bits();
7750
a9182619 7751 ++parg;
7752 Expression* len_arg;
7753 if (parg == args->end())
7754 {
7755 if (is_slice)
7756 {
7757 this->report_error(_("length required when allocating a slice"));
7758 return Expression::make_error(this->location());
7759 }
7760
7761 mpz_t zval;
7762 mpz_init_set_ui(zval, 0);
7763 len_arg = Expression::make_integer(&zval, NULL, loc);
7764 mpz_clear(zval);
7765 }
7766 else
7767 {
7768 len_arg = *parg;
7769 if (!this->check_int_value(len_arg))
7770 {
7771 this->report_error(_("bad size for make"));
7772 return Expression::make_error(this->location());
7773 }
ac84c822 7774 if (len_arg->type()->integer_type() != NULL
7775 && len_arg->type()->integer_type()->bits() > uintptr_bits)
7776 have_big_args = true;
a9182619 7777 ++parg;
7778 }
7779
7780 Expression* cap_arg = NULL;
7781 if (is_slice && parg != args->end())
7782 {
7783 cap_arg = *parg;
7784 if (!this->check_int_value(cap_arg))
7785 {
7786 this->report_error(_("bad capacity when making slice"));
7787 return Expression::make_error(this->location());
7788 }
ac84c822 7789 if (cap_arg->type()->integer_type() != NULL
7790 && cap_arg->type()->integer_type()->bits() > uintptr_bits)
7791 have_big_args = true;
a9182619 7792 ++parg;
7793 }
7794
7795 if (parg != args->end())
7796 {
7797 this->report_error(_("too many arguments to make"));
7798 return Expression::make_error(this->location());
7799 }
7800
b13c66cd 7801 Location type_loc = first_arg->location();
a9182619 7802 Expression* type_arg;
7803 if (is_slice || is_chan)
7804 type_arg = Expression::make_type_descriptor(type, type_loc);
7805 else if (is_map)
7806 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7807 else
7808 go_unreachable();
7809
7810 Expression* call;
7811 if (is_slice)
7812 {
7813 if (cap_arg == NULL)
ac84c822 7814 call = Runtime::make_call((have_big_args
7815 ? Runtime::MAKESLICE1BIG
7816 : Runtime::MAKESLICE1),
7817 loc, 2, type_arg, len_arg);
a9182619 7818 else
ac84c822 7819 call = Runtime::make_call((have_big_args
7820 ? Runtime::MAKESLICE2BIG
7821 : Runtime::MAKESLICE2),
7822 loc, 3, type_arg, len_arg, cap_arg);
a9182619 7823 }
7824 else if (is_map)
ac84c822 7825 call = Runtime::make_call((have_big_args
7826 ? Runtime::MAKEMAPBIG
7827 : Runtime::MAKEMAP),
7828 loc, 2, type_arg, len_arg);
a9182619 7829 else if (is_chan)
ac84c822 7830 call = Runtime::make_call((have_big_args
7831 ? Runtime::MAKECHANBIG
7832 : Runtime::MAKECHAN),
7833 loc, 2, type_arg, len_arg);
a9182619 7834 else
7835 go_unreachable();
7836
7837 return Expression::make_unsafe_cast(type, call, loc);
7838}
7839
7840// Return whether an expression has an integer value. Report an error
7841// if not. This is used when handling calls to the predeclared make
7842// function.
7843
7844bool
7845Builtin_call_expression::check_int_value(Expression* e)
7846{
7847 if (e->type()->integer_type() != NULL)
7848 return true;
7849
7850 // Check for a floating point constant with integer value.
7851 mpfr_t fval;
7852 mpfr_init(fval);
7853
7854 Type* dummy;
7855 if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval))
7856 {
7857 mpz_t ival;
7858 mpz_init(ival);
7859
7860 bool ok = false;
7861
7862 mpfr_clear_overflow();
7863 mpfr_clear_erangeflag();
7864 mpfr_get_z(ival, fval, GMP_RNDN);
7865 if (!mpfr_overflow_p()
7866 && !mpfr_erangeflag_p()
7867 && mpz_sgn(ival) >= 0)
7868 {
7869 Named_type* ntype = Type::lookup_integer_type("int");
7870 Integer_type* inttype = ntype->integer_type();
7871 mpz_t max;
7872 mpz_init_set_ui(max, 1);
7873 mpz_mul_2exp(max, max, inttype->bits() - 1);
7874 ok = mpz_cmp(ival, max) < 0;
7875 mpz_clear(max);
7876 }
7877 mpz_clear(ival);
7878
7879 if (ok)
7880 {
7881 mpfr_clear(fval);
7882 return true;
7883 }
7884 }
7885
7886 mpfr_clear(fval);
7887
7888 return false;
7889}
7890
e440a328 7891// Return the type of the real or imag functions, given the type of
7892// the argument. We need to map complex to float, complex64 to
7893// float32, and complex128 to float64, so it has to be done by name.
7894// This returns NULL if it can't figure out the type.
7895
7896Type*
7897Builtin_call_expression::real_imag_type(Type* arg_type)
7898{
7899 if (arg_type == NULL || arg_type->is_abstract())
7900 return NULL;
7901 Named_type* nt = arg_type->named_type();
7902 if (nt == NULL)
7903 return NULL;
7904 while (nt->real_type()->named_type() != NULL)
7905 nt = nt->real_type()->named_type();
48080209 7906 if (nt->name() == "complex64")
e440a328 7907 return Type::lookup_float_type("float32");
7908 else if (nt->name() == "complex128")
7909 return Type::lookup_float_type("float64");
7910 else
7911 return NULL;
7912}
7913
48080209 7914// Return the type of the complex function, given the type of one of the
e440a328 7915// argments. Like real_imag_type, we have to map by name.
7916
7917Type*
48080209 7918Builtin_call_expression::complex_type(Type* arg_type)
e440a328 7919{
7920 if (arg_type == NULL || arg_type->is_abstract())
7921 return NULL;
7922 Named_type* nt = arg_type->named_type();
7923 if (nt == NULL)
7924 return NULL;
7925 while (nt->real_type()->named_type() != NULL)
7926 nt = nt->real_type()->named_type();
48080209 7927 if (nt->name() == "float32")
e440a328 7928 return Type::lookup_complex_type("complex64");
7929 else if (nt->name() == "float64")
7930 return Type::lookup_complex_type("complex128");
7931 else
7932 return NULL;
7933}
7934
7935// Return a single argument, or NULL if there isn't one.
7936
7937Expression*
7938Builtin_call_expression::one_arg() const
7939{
7940 const Expression_list* args = this->args();
7941 if (args->size() != 1)
7942 return NULL;
7943 return args->front();
7944}
7945
7946// Return whether this is constant: len of a string, or len or cap of
7947// a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7948
7949bool
7950Builtin_call_expression::do_is_constant() const
7951{
7952 switch (this->code_)
7953 {
7954 case BUILTIN_LEN:
7955 case BUILTIN_CAP:
7956 {
0f914071 7957 if (this->seen_)
7958 return false;
7959
e440a328 7960 Expression* arg = this->one_arg();
7961 if (arg == NULL)
7962 return false;
7963 Type* arg_type = arg->type();
7964
7965 if (arg_type->points_to() != NULL
7966 && arg_type->points_to()->array_type() != NULL
411eb89e 7967 && !arg_type->points_to()->is_slice_type())
e440a328 7968 arg_type = arg_type->points_to();
7969
7970 if (arg_type->array_type() != NULL
7971 && arg_type->array_type()->length() != NULL)
0f914071 7972 return true;
e440a328 7973
7974 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
0f914071 7975 {
7976 this->seen_ = true;
7977 bool ret = arg->is_constant();
7978 this->seen_ = false;
7979 return ret;
7980 }
e440a328 7981 }
7982 break;
7983
7984 case BUILTIN_SIZEOF:
7985 case BUILTIN_ALIGNOF:
7986 return this->one_arg() != NULL;
7987
7988 case BUILTIN_OFFSETOF:
7989 {
7990 Expression* arg = this->one_arg();
7991 if (arg == NULL)
7992 return false;
7993 return arg->field_reference_expression() != NULL;
7994 }
7995
48080209 7996 case BUILTIN_COMPLEX:
e440a328 7997 {
7998 const Expression_list* args = this->args();
7999 if (args != NULL && args->size() == 2)
8000 return args->front()->is_constant() && args->back()->is_constant();
8001 }
8002 break;
8003
8004 case BUILTIN_REAL:
8005 case BUILTIN_IMAG:
8006 {
8007 Expression* arg = this->one_arg();
8008 return arg != NULL && arg->is_constant();
8009 }
8010
8011 default:
8012 break;
8013 }
8014
8015 return false;
8016}
8017
8018// Return an integer constant value if possible.
8019
8020bool
8021Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
8022 mpz_t val,
8023 Type** ptype) const
8024{
8025 if (this->code_ == BUILTIN_LEN
8026 || this->code_ == BUILTIN_CAP)
8027 {
8028 Expression* arg = this->one_arg();
8029 if (arg == NULL)
8030 return false;
8031 Type* arg_type = arg->type();
8032
8033 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
8034 {
8035 std::string sval;
8036 if (arg->string_constant_value(&sval))
8037 {
8038 mpz_set_ui(val, sval.length());
8039 *ptype = Type::lookup_integer_type("int");
8040 return true;
8041 }
8042 }
8043
8044 if (arg_type->points_to() != NULL
8045 && arg_type->points_to()->array_type() != NULL
411eb89e 8046 && !arg_type->points_to()->is_slice_type())
e440a328 8047 arg_type = arg_type->points_to();
8048
8049 if (arg_type->array_type() != NULL
8050 && arg_type->array_type()->length() != NULL)
8051 {
0f914071 8052 if (this->seen_)
8053 return false;
e440a328 8054 Expression* e = arg_type->array_type()->length();
0f914071 8055 this->seen_ = true;
8056 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
8057 this->seen_ = false;
8058 if (r)
e440a328 8059 {
8060 *ptype = Type::lookup_integer_type("int");
8061 return true;
8062 }
8063 }
8064 }
8065 else if (this->code_ == BUILTIN_SIZEOF
8066 || this->code_ == BUILTIN_ALIGNOF)
8067 {
8068 Expression* arg = this->one_arg();
8069 if (arg == NULL)
8070 return false;
8071 Type* arg_type = arg->type();
5c13bd80 8072 if (arg_type->is_error())
e440a328 8073 return false;
8074 if (arg_type->is_abstract())
8075 return false;
9aa9e2df 8076 if (arg_type->named_type() != NULL)
8077 arg_type->named_type()->convert(this->gogo_);
927a01eb 8078
8079 unsigned int ret;
e440a328 8080 if (this->code_ == BUILTIN_SIZEOF)
8081 {
927a01eb 8082 if (!arg_type->backend_type_size(this->gogo_, &ret))
e440a328 8083 return false;
8084 }
8085 else if (this->code_ == BUILTIN_ALIGNOF)
8086 {
637bd3af 8087 if (arg->field_reference_expression() == NULL)
927a01eb 8088 {
8089 if (!arg_type->backend_type_align(this->gogo_, &ret))
8090 return false;
8091 }
637bd3af 8092 else
e440a328 8093 {
8094 // Calling unsafe.Alignof(s.f) returns the alignment of
8095 // the type of f when it is used as a field in a struct.
927a01eb 8096 if (!arg_type->backend_type_field_align(this->gogo_, &ret))
8097 return false;
e440a328 8098 }
e440a328 8099 }
8100 else
c3e6f413 8101 go_unreachable();
927a01eb 8102
8103 mpz_set_ui(val, ret);
e440a328 8104 *ptype = NULL;
8105 return true;
8106 }
8107 else if (this->code_ == BUILTIN_OFFSETOF)
8108 {
8109 Expression* arg = this->one_arg();
8110 if (arg == NULL)
8111 return false;
8112 Field_reference_expression* farg = arg->field_reference_expression();
8113 if (farg == NULL)
8114 return false;
8115 Expression* struct_expr = farg->expr();
8116 Type* st = struct_expr->type();
8117 if (st->struct_type() == NULL)
8118 return false;
9aa9e2df 8119 if (st->named_type() != NULL)
8120 st->named_type()->convert(this->gogo_);
927a01eb 8121 unsigned int offset;
8122 if (!st->struct_type()->backend_field_offset(this->gogo_,
8123 farg->field_index(),
8124 &offset))
e440a328 8125 return false;
927a01eb 8126 mpz_set_ui(val, offset);
e440a328 8127 return true;
8128 }
8129 return false;
8130}
8131
8132// Return a floating point constant value if possible.
8133
8134bool
8135Builtin_call_expression::do_float_constant_value(mpfr_t val,
8136 Type** ptype) const
8137{
8138 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
8139 {
8140 Expression* arg = this->one_arg();
8141 if (arg == NULL)
8142 return false;
8143
8144 mpfr_t real;
8145 mpfr_t imag;
8146 mpfr_init(real);
8147 mpfr_init(imag);
8148
8149 bool ret = false;
8150 Type* type;
8151 if (arg->complex_constant_value(real, imag, &type))
8152 {
8153 if (this->code_ == BUILTIN_REAL)
8154 mpfr_set(val, real, GMP_RNDN);
8155 else
8156 mpfr_set(val, imag, GMP_RNDN);
8157 *ptype = Builtin_call_expression::real_imag_type(type);
8158 ret = true;
8159 }
8160
8161 mpfr_clear(real);
8162 mpfr_clear(imag);
8163 return ret;
8164 }
8165
8166 return false;
8167}
8168
8169// Return a complex constant value if possible.
8170
8171bool
8172Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
8173 Type** ptype) const
8174{
48080209 8175 if (this->code_ == BUILTIN_COMPLEX)
e440a328 8176 {
8177 const Expression_list* args = this->args();
8178 if (args == NULL || args->size() != 2)
8179 return false;
8180
8181 mpfr_t r;
8182 mpfr_init(r);
8183 Type* rtype;
8184 if (!args->front()->float_constant_value(r, &rtype))
8185 {
8186 mpfr_clear(r);
8187 return false;
8188 }
8189
8190 mpfr_t i;
8191 mpfr_init(i);
8192
8193 bool ret = false;
8194 Type* itype;
8195 if (args->back()->float_constant_value(i, &itype)
07ba8be5 8196 && Type::are_identical(rtype, itype, false, NULL))
e440a328 8197 {
8198 mpfr_set(real, r, GMP_RNDN);
8199 mpfr_set(imag, i, GMP_RNDN);
48080209 8200 *ptype = Builtin_call_expression::complex_type(rtype);
e440a328 8201 ret = true;
8202 }
8203
8204 mpfr_clear(r);
8205 mpfr_clear(i);
8206
8207 return ret;
8208 }
8209
8210 return false;
8211}
8212
a7549a6a 8213// Give an error if we are discarding the value of an expression which
8214// should not normally be discarded. We don't give an error for
8215// discarding the value of an ordinary function call, but we do for
8216// builtin functions, purely for consistency with the gc compiler.
8217
8218void
8219Builtin_call_expression::do_discarding_value()
8220{
8221 switch (this->code_)
8222 {
8223 case BUILTIN_INVALID:
8224 default:
8225 go_unreachable();
8226
8227 case BUILTIN_APPEND:
8228 case BUILTIN_CAP:
8229 case BUILTIN_COMPLEX:
8230 case BUILTIN_IMAG:
8231 case BUILTIN_LEN:
8232 case BUILTIN_MAKE:
8233 case BUILTIN_NEW:
8234 case BUILTIN_REAL:
8235 case BUILTIN_ALIGNOF:
8236 case BUILTIN_OFFSETOF:
8237 case BUILTIN_SIZEOF:
8238 this->unused_value_error();
8239 break;
8240
8241 case BUILTIN_CLOSE:
8242 case BUILTIN_COPY:
1cce762f 8243 case BUILTIN_DELETE:
a7549a6a 8244 case BUILTIN_PANIC:
8245 case BUILTIN_PRINT:
8246 case BUILTIN_PRINTLN:
8247 case BUILTIN_RECOVER:
8248 break;
8249 }
8250}
8251
e440a328 8252// Return the type.
8253
8254Type*
8255Builtin_call_expression::do_type()
8256{
8257 switch (this->code_)
8258 {
8259 case BUILTIN_INVALID:
8260 default:
c3e6f413 8261 go_unreachable();
e440a328 8262
8263 case BUILTIN_NEW:
8264 case BUILTIN_MAKE:
8265 {
8266 const Expression_list* args = this->args();
8267 if (args == NULL || args->empty())
8268 return Type::make_error_type();
8269 return Type::make_pointer_type(args->front()->type());
8270 }
8271
8272 case BUILTIN_CAP:
8273 case BUILTIN_COPY:
8274 case BUILTIN_LEN:
8275 case BUILTIN_ALIGNOF:
8276 case BUILTIN_OFFSETOF:
8277 case BUILTIN_SIZEOF:
8278 return Type::lookup_integer_type("int");
8279
8280 case BUILTIN_CLOSE:
1cce762f 8281 case BUILTIN_DELETE:
e440a328 8282 case BUILTIN_PANIC:
8283 case BUILTIN_PRINT:
8284 case BUILTIN_PRINTLN:
8285 return Type::make_void_type();
8286
e440a328 8287 case BUILTIN_RECOVER:
823c7e3d 8288 return Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 8289
8290 case BUILTIN_APPEND:
8291 {
8292 const Expression_list* args = this->args();
8293 if (args == NULL || args->empty())
8294 return Type::make_error_type();
8295 return args->front()->type();
8296 }
8297
8298 case BUILTIN_REAL:
8299 case BUILTIN_IMAG:
8300 {
8301 Expression* arg = this->one_arg();
8302 if (arg == NULL)
8303 return Type::make_error_type();
8304 Type* t = arg->type();
8305 if (t->is_abstract())
8306 t = t->make_non_abstract_type();
8307 t = Builtin_call_expression::real_imag_type(t);
8308 if (t == NULL)
8309 t = Type::make_error_type();
8310 return t;
8311 }
8312
48080209 8313 case BUILTIN_COMPLEX:
e440a328 8314 {
8315 const Expression_list* args = this->args();
8316 if (args == NULL || args->size() != 2)
8317 return Type::make_error_type();
8318 Type* t = args->front()->type();
8319 if (t->is_abstract())
8320 {
8321 t = args->back()->type();
8322 if (t->is_abstract())
8323 t = t->make_non_abstract_type();
8324 }
48080209 8325 t = Builtin_call_expression::complex_type(t);
e440a328 8326 if (t == NULL)
8327 t = Type::make_error_type();
8328 return t;
8329 }
8330 }
8331}
8332
8333// Determine the type.
8334
8335void
8336Builtin_call_expression::do_determine_type(const Type_context* context)
8337{
fb94b0ca 8338 if (!this->determining_types())
8339 return;
8340
e440a328 8341 this->fn()->determine_type_no_context();
8342
8343 const Expression_list* args = this->args();
8344
8345 bool is_print;
8346 Type* arg_type = NULL;
8347 switch (this->code_)
8348 {
8349 case BUILTIN_PRINT:
8350 case BUILTIN_PRINTLN:
8351 // Do not force a large integer constant to "int".
8352 is_print = true;
8353 break;
8354
8355 case BUILTIN_REAL:
8356 case BUILTIN_IMAG:
48080209 8357 arg_type = Builtin_call_expression::complex_type(context->type);
e440a328 8358 is_print = false;
8359 break;
8360
48080209 8361 case BUILTIN_COMPLEX:
e440a328 8362 {
48080209 8363 // For the complex function the type of one operand can
e440a328 8364 // determine the type of the other, as in a binary expression.
8365 arg_type = Builtin_call_expression::real_imag_type(context->type);
8366 if (args != NULL && args->size() == 2)
8367 {
8368 Type* t1 = args->front()->type();
8369 Type* t2 = args->front()->type();
8370 if (!t1->is_abstract())
8371 arg_type = t1;
8372 else if (!t2->is_abstract())
8373 arg_type = t2;
8374 }
8375 is_print = false;
8376 }
8377 break;
8378
8379 default:
8380 is_print = false;
8381 break;
8382 }
8383
8384 if (args != NULL)
8385 {
8386 for (Expression_list::const_iterator pa = args->begin();
8387 pa != args->end();
8388 ++pa)
8389 {
8390 Type_context subcontext;
8391 subcontext.type = arg_type;
8392
8393 if (is_print)
8394 {
8395 // We want to print large constants, we so can't just
8396 // use the appropriate nonabstract type. Use uint64 for
8397 // an integer if we know it is nonnegative, otherwise
8398 // use int64 for a integer, otherwise use float64 for a
8399 // float or complex128 for a complex.
8400 Type* want_type = NULL;
8401 Type* atype = (*pa)->type();
8402 if (atype->is_abstract())
8403 {
8404 if (atype->integer_type() != NULL)
8405 {
8406 mpz_t val;
8407 mpz_init(val);
8408 Type* dummy;
8409 if (this->integer_constant_value(true, val, &dummy)
8410 && mpz_sgn(val) >= 0)
8411 want_type = Type::lookup_integer_type("uint64");
8412 else
8413 want_type = Type::lookup_integer_type("int64");
8414 mpz_clear(val);
8415 }
8416 else if (atype->float_type() != NULL)
8417 want_type = Type::lookup_float_type("float64");
8418 else if (atype->complex_type() != NULL)
8419 want_type = Type::lookup_complex_type("complex128");
8420 else if (atype->is_abstract_string_type())
8421 want_type = Type::lookup_string_type();
8422 else if (atype->is_abstract_boolean_type())
8423 want_type = Type::lookup_bool_type();
8424 else
c3e6f413 8425 go_unreachable();
e440a328 8426 subcontext.type = want_type;
8427 }
8428 }
8429
8430 (*pa)->determine_type(&subcontext);
8431 }
8432 }
8433}
8434
8435// If there is exactly one argument, return true. Otherwise give an
8436// error message and return false.
8437
8438bool
8439Builtin_call_expression::check_one_arg()
8440{
8441 const Expression_list* args = this->args();
8442 if (args == NULL || args->size() < 1)
8443 {
8444 this->report_error(_("not enough arguments"));
8445 return false;
8446 }
8447 else if (args->size() > 1)
8448 {
8449 this->report_error(_("too many arguments"));
8450 return false;
8451 }
8452 if (args->front()->is_error_expression()
5c13bd80 8453 || args->front()->type()->is_error())
e440a328 8454 {
8455 this->set_is_error();
8456 return false;
8457 }
8458 return true;
8459}
8460
8461// Check argument types for a builtin function.
8462
8463void
8464Builtin_call_expression::do_check_types(Gogo*)
8465{
8466 switch (this->code_)
8467 {
8468 case BUILTIN_INVALID:
8469 case BUILTIN_NEW:
8470 case BUILTIN_MAKE:
8471 return;
8472
8473 case BUILTIN_LEN:
8474 case BUILTIN_CAP:
8475 {
8476 // The single argument may be either a string or an array or a
8477 // map or a channel, or a pointer to a closed array.
8478 if (this->check_one_arg())
8479 {
8480 Type* arg_type = this->one_arg()->type();
8481 if (arg_type->points_to() != NULL
8482 && arg_type->points_to()->array_type() != NULL
411eb89e 8483 && !arg_type->points_to()->is_slice_type())
e440a328 8484 arg_type = arg_type->points_to();
8485 if (this->code_ == BUILTIN_CAP)
8486 {
5c13bd80 8487 if (!arg_type->is_error()
e440a328 8488 && arg_type->array_type() == NULL
8489 && arg_type->channel_type() == NULL)
8490 this->report_error(_("argument must be array or slice "
8491 "or channel"));
8492 }
8493 else
8494 {
5c13bd80 8495 if (!arg_type->is_error()
e440a328 8496 && !arg_type->is_string_type()
8497 && arg_type->array_type() == NULL
8498 && arg_type->map_type() == NULL
8499 && arg_type->channel_type() == NULL)
8500 this->report_error(_("argument must be string or "
8501 "array or slice or map or channel"));
8502 }
8503 }
8504 }
8505 break;
8506
8507 case BUILTIN_PRINT:
8508 case BUILTIN_PRINTLN:
8509 {
8510 const Expression_list* args = this->args();
8511 if (args == NULL)
8512 {
8513 if (this->code_ == BUILTIN_PRINT)
8514 warning_at(this->location(), 0,
8515 "no arguments for builtin function %<%s%>",
8516 (this->code_ == BUILTIN_PRINT
8517 ? "print"
8518 : "println"));
8519 }
8520 else
8521 {
8522 for (Expression_list::const_iterator p = args->begin();
8523 p != args->end();
8524 ++p)
8525 {
8526 Type* type = (*p)->type();
5c13bd80 8527 if (type->is_error()
e440a328 8528 || type->is_string_type()
8529 || type->integer_type() != NULL
8530 || type->float_type() != NULL
8531 || type->complex_type() != NULL
8532 || type->is_boolean_type()
8533 || type->points_to() != NULL
8534 || type->interface_type() != NULL
8535 || type->channel_type() != NULL
8536 || type->map_type() != NULL
8537 || type->function_type() != NULL
411eb89e 8538 || type->is_slice_type())
e440a328 8539 ;
acf8e158 8540 else if ((*p)->is_type_expression())
8541 {
8542 // If this is a type expression it's going to give
8543 // an error anyhow, so we don't need one here.
8544 }
e440a328 8545 else
8546 this->report_error(_("unsupported argument type to "
8547 "builtin function"));
8548 }
8549 }
8550 }
8551 break;
8552
8553 case BUILTIN_CLOSE:
e440a328 8554 if (this->check_one_arg())
8555 {
8556 if (this->one_arg()->type()->channel_type() == NULL)
8557 this->report_error(_("argument must be channel"));
5202d986 8558 else if (!this->one_arg()->type()->channel_type()->may_send())
8559 this->report_error(_("cannot close receive-only channel"));
e440a328 8560 }
8561 break;
8562
8563 case BUILTIN_PANIC:
8564 case BUILTIN_SIZEOF:
8565 case BUILTIN_ALIGNOF:
8566 this->check_one_arg();
8567 break;
8568
8569 case BUILTIN_RECOVER:
8570 if (this->args() != NULL && !this->args()->empty())
8571 this->report_error(_("too many arguments"));
8572 break;
8573
8574 case BUILTIN_OFFSETOF:
8575 if (this->check_one_arg())
8576 {
8577 Expression* arg = this->one_arg();
8578 if (arg->field_reference_expression() == NULL)
8579 this->report_error(_("argument must be a field reference"));
8580 }
8581 break;
8582
8583 case BUILTIN_COPY:
8584 {
8585 const Expression_list* args = this->args();
8586 if (args == NULL || args->size() < 2)
8587 {
8588 this->report_error(_("not enough arguments"));
8589 break;
8590 }
8591 else if (args->size() > 2)
8592 {
8593 this->report_error(_("too many arguments"));
8594 break;
8595 }
8596 Type* arg1_type = args->front()->type();
8597 Type* arg2_type = args->back()->type();
5c13bd80 8598 if (arg1_type->is_error() || arg2_type->is_error())
e440a328 8599 break;
8600
8601 Type* e1;
411eb89e 8602 if (arg1_type->is_slice_type())
e440a328 8603 e1 = arg1_type->array_type()->element_type();
8604 else
8605 {
8606 this->report_error(_("left argument must be a slice"));
8607 break;
8608 }
8609
411eb89e 8610 if (arg2_type->is_slice_type())
60963afd 8611 {
8612 Type* e2 = arg2_type->array_type()->element_type();
8613 if (!Type::are_identical(e1, e2, true, NULL))
8614 this->report_error(_("element types must be the same"));
8615 }
e440a328 8616 else if (arg2_type->is_string_type())
e440a328 8617 {
60963afd 8618 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
8619 this->report_error(_("first argument must be []byte"));
e440a328 8620 }
60963afd 8621 else
8622 this->report_error(_("second argument must be slice or string"));
e440a328 8623 }
8624 break;
8625
8626 case BUILTIN_APPEND:
8627 {
8628 const Expression_list* args = this->args();
b0d311a1 8629 if (args == NULL || args->size() < 2)
e440a328 8630 {
8631 this->report_error(_("not enough arguments"));
8632 break;
8633 }
0b7755ec 8634 if (args->size() > 2)
8635 {
8636 this->report_error(_("too many arguments"));
8637 break;
8638 }
4fd4fcf4 8639
8640 // The language permits appending a string to a []byte, as a
8641 // special case.
8642 if (args->back()->type()->is_string_type())
8643 {
8644 const Array_type* at = args->front()->type()->array_type();
8645 const Type* e = at->element_type()->forwarded();
60963afd 8646 if (e->integer_type() != NULL && e->integer_type()->is_byte())
4fd4fcf4 8647 break;
8648 }
8649
19fd40c3 8650 // The language says that the second argument must be
8651 // assignable to a slice of the element type of the first
8652 // argument. We already know the first argument is a slice
8653 // type.
8654 Array_type* at = args->front()->type()->array_type();
8655 Type* arg2_type = Type::make_array_type(at->element_type(), NULL);
e440a328 8656 std::string reason;
19fd40c3 8657 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason))
e440a328 8658 {
8659 if (reason.empty())
19fd40c3 8660 this->report_error(_("argument 2 has invalid type"));
e440a328 8661 else
8662 {
19fd40c3 8663 error_at(this->location(), "argument 2 has invalid type (%s)",
e440a328 8664 reason.c_str());
8665 this->set_is_error();
8666 }
8667 }
8668 break;
8669 }
8670
8671 case BUILTIN_REAL:
8672 case BUILTIN_IMAG:
8673 if (this->check_one_arg())
8674 {
8675 if (this->one_arg()->type()->complex_type() == NULL)
8676 this->report_error(_("argument must have complex type"));
8677 }
8678 break;
8679
48080209 8680 case BUILTIN_COMPLEX:
e440a328 8681 {
8682 const Expression_list* args = this->args();
8683 if (args == NULL || args->size() < 2)
8684 this->report_error(_("not enough arguments"));
8685 else if (args->size() > 2)
8686 this->report_error(_("too many arguments"));
8687 else if (args->front()->is_error_expression()
5c13bd80 8688 || args->front()->type()->is_error()
e440a328 8689 || args->back()->is_error_expression()
5c13bd80 8690 || args->back()->type()->is_error())
e440a328 8691 this->set_is_error();
8692 else if (!Type::are_identical(args->front()->type(),
07ba8be5 8693 args->back()->type(), true, NULL))
48080209 8694 this->report_error(_("complex arguments must have identical types"));
e440a328 8695 else if (args->front()->type()->float_type() == NULL)
48080209 8696 this->report_error(_("complex arguments must have "
e440a328 8697 "floating-point type"));
8698 }
8699 break;
8700
8701 default:
c3e6f413 8702 go_unreachable();
e440a328 8703 }
8704}
8705
8706// Return the tree for a builtin function.
8707
8708tree
8709Builtin_call_expression::do_get_tree(Translate_context* context)
8710{
8711 Gogo* gogo = context->gogo();
b13c66cd 8712 Location location = this->location();
e440a328 8713 switch (this->code_)
8714 {
8715 case BUILTIN_INVALID:
8716 case BUILTIN_NEW:
8717 case BUILTIN_MAKE:
c3e6f413 8718 go_unreachable();
e440a328 8719
8720 case BUILTIN_LEN:
8721 case BUILTIN_CAP:
8722 {
8723 const Expression_list* args = this->args();
c484d925 8724 go_assert(args != NULL && args->size() == 1);
e440a328 8725 Expression* arg = *args->begin();
8726 Type* arg_type = arg->type();
0f914071 8727
8728 if (this->seen_)
8729 {
c484d925 8730 go_assert(saw_errors());
0f914071 8731 return error_mark_node;
8732 }
8733 this->seen_ = true;
8734
e440a328 8735 tree arg_tree = arg->get_tree(context);
0f914071 8736
8737 this->seen_ = false;
8738
e440a328 8739 if (arg_tree == error_mark_node)
8740 return error_mark_node;
8741
8742 if (arg_type->points_to() != NULL)
8743 {
8744 arg_type = arg_type->points_to();
c484d925 8745 go_assert(arg_type->array_type() != NULL
411eb89e 8746 && !arg_type->is_slice_type());
c484d925 8747 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
e440a328 8748 arg_tree = build_fold_indirect_ref(arg_tree);
8749 }
8750
8751 tree val_tree;
8752 if (this->code_ == BUILTIN_LEN)
8753 {
8754 if (arg_type->is_string_type())
8755 val_tree = String_type::length_tree(gogo, arg_tree);
8756 else if (arg_type->array_type() != NULL)
0f914071 8757 {
8758 if (this->seen_)
8759 {
c484d925 8760 go_assert(saw_errors());
0f914071 8761 return error_mark_node;
8762 }
8763 this->seen_ = true;
8764 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
8765 this->seen_ = false;
8766 }
e440a328 8767 else if (arg_type->map_type() != NULL)
8768 {
9f0e0513 8769 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
e440a328 8770 static tree map_len_fndecl;
8771 val_tree = Gogo::call_builtin(&map_len_fndecl,
8772 location,
8773 "__go_map_len",
8774 1,
9581e91d 8775 integer_type_node,
9f0e0513 8776 arg_type_tree,
e440a328 8777 arg_tree);
8778 }
8779 else if (arg_type->channel_type() != NULL)
8780 {
9f0e0513 8781 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
e440a328 8782 static tree chan_len_fndecl;
8783 val_tree = Gogo::call_builtin(&chan_len_fndecl,
8784 location,
8785 "__go_chan_len",
8786 1,
9581e91d 8787 integer_type_node,
9f0e0513 8788 arg_type_tree,
e440a328 8789 arg_tree);
8790 }
8791 else
c3e6f413 8792 go_unreachable();
e440a328 8793 }
8794 else
8795 {
8796 if (arg_type->array_type() != NULL)
0f914071 8797 {
8798 if (this->seen_)
8799 {
c484d925 8800 go_assert(saw_errors());
0f914071 8801 return error_mark_node;
8802 }
8803 this->seen_ = true;
8804 val_tree = arg_type->array_type()->capacity_tree(gogo,
8805 arg_tree);
8806 this->seen_ = false;
8807 }
e440a328 8808 else if (arg_type->channel_type() != NULL)
8809 {
9f0e0513 8810 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
e440a328 8811 static tree chan_cap_fndecl;
8812 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
8813 location,
8814 "__go_chan_cap",
8815 1,
9581e91d 8816 integer_type_node,
9f0e0513 8817 arg_type_tree,
e440a328 8818 arg_tree);
8819 }
8820 else
c3e6f413 8821 go_unreachable();
e440a328 8822 }
8823
d8ccb1e3 8824 if (val_tree == error_mark_node)
8825 return error_mark_node;
8826
9f0e0513 8827 Type* int_type = Type::lookup_integer_type("int");
8828 tree type_tree = type_to_tree(int_type->get_backend(gogo));
e440a328 8829 if (type_tree == TREE_TYPE(val_tree))
8830 return val_tree;
8831 else
8832 return fold(convert_to_integer(type_tree, val_tree));
8833 }
8834
8835 case BUILTIN_PRINT:
8836 case BUILTIN_PRINTLN:
8837 {
8838 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
8839 tree stmt_list = NULL_TREE;
8840
8841 const Expression_list* call_args = this->args();
8842 if (call_args != NULL)
8843 {
8844 for (Expression_list::const_iterator p = call_args->begin();
8845 p != call_args->end();
8846 ++p)
8847 {
8848 if (is_ln && p != call_args->begin())
8849 {
8850 static tree print_space_fndecl;
8851 tree call = Gogo::call_builtin(&print_space_fndecl,
8852 location,
8853 "__go_print_space",
8854 0,
8855 void_type_node);
5fb82b5e 8856 if (call == error_mark_node)
8857 return error_mark_node;
e440a328 8858 append_to_statement_list(call, &stmt_list);
8859 }
8860
8861 Type* type = (*p)->type();
8862
8863 tree arg = (*p)->get_tree(context);
8864 if (arg == error_mark_node)
8865 return error_mark_node;
8866
8867 tree* pfndecl;
8868 const char* fnname;
8869 if (type->is_string_type())
8870 {
8871 static tree print_string_fndecl;
8872 pfndecl = &print_string_fndecl;
8873 fnname = "__go_print_string";
8874 }
8875 else if (type->integer_type() != NULL
8876 && type->integer_type()->is_unsigned())
8877 {
8878 static tree print_uint64_fndecl;
8879 pfndecl = &print_uint64_fndecl;
8880 fnname = "__go_print_uint64";
8881 Type* itype = Type::lookup_integer_type("uint64");
9f0e0513 8882 Btype* bitype = itype->get_backend(gogo);
b13c66cd 8883 arg = fold_convert_loc(location.gcc_location(),
8884 type_to_tree(bitype), arg);
e440a328 8885 }
8886 else if (type->integer_type() != NULL)
8887 {
8888 static tree print_int64_fndecl;
8889 pfndecl = &print_int64_fndecl;
8890 fnname = "__go_print_int64";
8891 Type* itype = Type::lookup_integer_type("int64");
9f0e0513 8892 Btype* bitype = itype->get_backend(gogo);
b13c66cd 8893 arg = fold_convert_loc(location.gcc_location(),
8894 type_to_tree(bitype), arg);
e440a328 8895 }
8896 else if (type->float_type() != NULL)
8897 {
8898 static tree print_double_fndecl;
8899 pfndecl = &print_double_fndecl;
8900 fnname = "__go_print_double";
b13c66cd 8901 arg = fold_convert_loc(location.gcc_location(),
8902 double_type_node, arg);
e440a328 8903 }
8904 else if (type->complex_type() != NULL)
8905 {
8906 static tree print_complex_fndecl;
8907 pfndecl = &print_complex_fndecl;
8908 fnname = "__go_print_complex";
b13c66cd 8909 arg = fold_convert_loc(location.gcc_location(),
8910 complex_double_type_node, arg);
e440a328 8911 }
8912 else if (type->is_boolean_type())
8913 {
8914 static tree print_bool_fndecl;
8915 pfndecl = &print_bool_fndecl;
8916 fnname = "__go_print_bool";
8917 }
8918 else if (type->points_to() != NULL
8919 || type->channel_type() != NULL
8920 || type->map_type() != NULL
8921 || type->function_type() != NULL)
8922 {
8923 static tree print_pointer_fndecl;
8924 pfndecl = &print_pointer_fndecl;
8925 fnname = "__go_print_pointer";
b13c66cd 8926 arg = fold_convert_loc(location.gcc_location(),
8927 ptr_type_node, arg);
e440a328 8928 }
8929 else if (type->interface_type() != NULL)
8930 {
8931 if (type->interface_type()->is_empty())
8932 {
8933 static tree print_empty_interface_fndecl;
8934 pfndecl = &print_empty_interface_fndecl;
8935 fnname = "__go_print_empty_interface";
8936 }
8937 else
8938 {
8939 static tree print_interface_fndecl;
8940 pfndecl = &print_interface_fndecl;
8941 fnname = "__go_print_interface";
8942 }
8943 }
411eb89e 8944 else if (type->is_slice_type())
e440a328 8945 {
8946 static tree print_slice_fndecl;
8947 pfndecl = &print_slice_fndecl;
8948 fnname = "__go_print_slice";
8949 }
8950 else
c3e6f413 8951 go_unreachable();
e440a328 8952
8953 tree call = Gogo::call_builtin(pfndecl,
8954 location,
8955 fnname,
8956 1,
8957 void_type_node,
8958 TREE_TYPE(arg),
8959 arg);
5fb82b5e 8960 if (call == error_mark_node)
8961 return error_mark_node;
8962 append_to_statement_list(call, &stmt_list);
e440a328 8963 }
8964 }
8965
8966 if (is_ln)
8967 {
8968 static tree print_nl_fndecl;
8969 tree call = Gogo::call_builtin(&print_nl_fndecl,
8970 location,
8971 "__go_print_nl",
8972 0,
8973 void_type_node);
5fb82b5e 8974 if (call == error_mark_node)
8975 return error_mark_node;
e440a328 8976 append_to_statement_list(call, &stmt_list);
8977 }
8978
8979 return stmt_list;
8980 }
8981
8982 case BUILTIN_PANIC:
8983 {
8984 const Expression_list* args = this->args();
c484d925 8985 go_assert(args != NULL && args->size() == 1);
e440a328 8986 Expression* arg = args->front();
8987 tree arg_tree = arg->get_tree(context);
8988 if (arg_tree == error_mark_node)
8989 return error_mark_node;
b13c66cd 8990 Type *empty =
823c7e3d 8991 Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 8992 arg_tree = Expression::convert_for_assignment(context, empty,
8993 arg->type(),
8994 arg_tree, location);
8995 static tree panic_fndecl;
8996 tree call = Gogo::call_builtin(&panic_fndecl,
8997 location,
8998 "__go_panic",
8999 1,
9000 void_type_node,
9001 TREE_TYPE(arg_tree),
9002 arg_tree);
5fb82b5e 9003 if (call == error_mark_node)
9004 return error_mark_node;
e440a328 9005 // This function will throw an exception.
9006 TREE_NOTHROW(panic_fndecl) = 0;
9007 // This function will not return.
9008 TREE_THIS_VOLATILE(panic_fndecl) = 1;
9009 return call;
9010 }
9011
9012 case BUILTIN_RECOVER:
9013 {
9014 // The argument is set when building recover thunks. It's a
9015 // boolean value which is true if we can recover a value now.
9016 const Expression_list* args = this->args();
c484d925 9017 go_assert(args != NULL && args->size() == 1);
e440a328 9018 Expression* arg = args->front();
9019 tree arg_tree = arg->get_tree(context);
9020 if (arg_tree == error_mark_node)
9021 return error_mark_node;
9022
b13c66cd 9023 Type *empty =
823c7e3d 9024 Type::make_empty_interface_type(Linemap::predeclared_location());
9f0e0513 9025 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
e440a328 9026
9027 Type* nil_type = Type::make_nil_type();
9028 Expression* nil = Expression::make_nil(location);
9029 tree nil_tree = nil->get_tree(context);
9030 tree empty_nil_tree = Expression::convert_for_assignment(context,
9031 empty,
9032 nil_type,
9033 nil_tree,
9034 location);
9035
9036 // We need to handle a deferred call to recover specially,
9037 // because it changes whether it can recover a panic or not.
9038 // See test7 in test/recover1.go.
9039 tree call;
9040 if (this->is_deferred())
9041 {
9042 static tree deferred_recover_fndecl;
9043 call = Gogo::call_builtin(&deferred_recover_fndecl,
9044 location,
9045 "__go_deferred_recover",
9046 0,
9047 empty_tree);
9048 }
9049 else
9050 {
9051 static tree recover_fndecl;
9052 call = Gogo::call_builtin(&recover_fndecl,
9053 location,
9054 "__go_recover",
9055 0,
9056 empty_tree);
9057 }
5fb82b5e 9058 if (call == error_mark_node)
9059 return error_mark_node;
b13c66cd 9060 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
9061 arg_tree, call, empty_nil_tree);
e440a328 9062 }
9063
9064 case BUILTIN_CLOSE:
e440a328 9065 {
9066 const Expression_list* args = this->args();
c484d925 9067 go_assert(args != NULL && args->size() == 1);
e440a328 9068 Expression* arg = args->front();
9069 tree arg_tree = arg->get_tree(context);
9070 if (arg_tree == error_mark_node)
9071 return error_mark_node;
0dc2f918 9072 static tree close_fndecl;
9073 return Gogo::call_builtin(&close_fndecl,
9074 location,
9075 "__go_builtin_close",
9076 1,
9077 void_type_node,
9078 TREE_TYPE(arg_tree),
9079 arg_tree);
e440a328 9080 }
9081
9082 case BUILTIN_SIZEOF:
9083 case BUILTIN_OFFSETOF:
9084 case BUILTIN_ALIGNOF:
9085 {
9086 mpz_t val;
9087 mpz_init(val);
9088 Type* dummy;
9089 bool b = this->integer_constant_value(true, val, &dummy);
7f1d9abd 9090 if (!b)
9091 {
c484d925 9092 go_assert(saw_errors());
7f1d9abd 9093 return error_mark_node;
9094 }
9f0e0513 9095 Type* int_type = Type::lookup_integer_type("int");
9096 tree type = type_to_tree(int_type->get_backend(gogo));
e440a328 9097 tree ret = Expression::integer_constant_tree(val, type);
9098 mpz_clear(val);
9099 return ret;
9100 }
9101
9102 case BUILTIN_COPY:
9103 {
9104 const Expression_list* args = this->args();
c484d925 9105 go_assert(args != NULL && args->size() == 2);
e440a328 9106 Expression* arg1 = args->front();
9107 Expression* arg2 = args->back();
9108
9109 tree arg1_tree = arg1->get_tree(context);
9110 tree arg2_tree = arg2->get_tree(context);
9111 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
9112 return error_mark_node;
9113
9114 Type* arg1_type = arg1->type();
9115 Array_type* at = arg1_type->array_type();
9116 arg1_tree = save_expr(arg1_tree);
9117 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
9118 tree arg1_len = at->length_tree(gogo, arg1_tree);
d8ccb1e3 9119 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
9120 return error_mark_node;
e440a328 9121
9122 Type* arg2_type = arg2->type();
9123 tree arg2_val;
9124 tree arg2_len;
411eb89e 9125 if (arg2_type->is_slice_type())
e440a328 9126 {
9127 at = arg2_type->array_type();
9128 arg2_tree = save_expr(arg2_tree);
9129 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
9130 arg2_len = at->length_tree(gogo, arg2_tree);
9131 }
9132 else
9133 {
9134 arg2_tree = save_expr(arg2_tree);
9135 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
9136 arg2_len = String_type::length_tree(gogo, arg2_tree);
9137 }
d8ccb1e3 9138 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
9139 return error_mark_node;
e440a328 9140
9141 arg1_len = save_expr(arg1_len);
9142 arg2_len = save_expr(arg2_len);
b13c66cd 9143 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
9144 TREE_TYPE(arg1_len),
9145 fold_build2_loc(location.gcc_location(),
9146 LT_EXPR, boolean_type_node,
e440a328 9147 arg1_len, arg2_len),
9148 arg1_len, arg2_len);
9149 len = save_expr(len);
9150
9151 Type* element_type = at->element_type();
9f0e0513 9152 Btype* element_btype = element_type->get_backend(gogo);
9153 tree element_type_tree = type_to_tree(element_btype);
d8ccb1e3 9154 if (element_type_tree == error_mark_node)
9155 return error_mark_node;
e440a328 9156 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
b13c66cd 9157 tree bytecount = fold_convert_loc(location.gcc_location(),
9158 TREE_TYPE(element_size), len);
9159 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
e440a328 9160 TREE_TYPE(element_size),
9161 bytecount, element_size);
b13c66cd 9162 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
9163 bytecount);
e440a328 9164
b13c66cd 9165 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9166 arg1_val);
9167 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9168 arg2_val);
3991cb03 9169
9170 static tree copy_fndecl;
9171 tree call = Gogo::call_builtin(&copy_fndecl,
9172 location,
9173 "__go_copy",
9174 3,
9175 void_type_node,
9176 ptr_type_node,
9177 arg1_val,
9178 ptr_type_node,
9179 arg2_val,
9180 size_type_node,
9181 bytecount);
9182 if (call == error_mark_node)
9183 return error_mark_node;
e440a328 9184
b13c66cd 9185 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
9186 TREE_TYPE(len), call, len);
e440a328 9187 }
9188
9189 case BUILTIN_APPEND:
9190 {
9191 const Expression_list* args = this->args();
c484d925 9192 go_assert(args != NULL && args->size() == 2);
e440a328 9193 Expression* arg1 = args->front();
9194 Expression* arg2 = args->back();
9195
9196 tree arg1_tree = arg1->get_tree(context);
9197 tree arg2_tree = arg2->get_tree(context);
9198 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
9199 return error_mark_node;
9200
9d44fbe3 9201 Array_type* at = arg1->type()->array_type();
4fd4fcf4 9202 Type* element_type = at->element_type()->forwarded();
9d44fbe3 9203
4fd4fcf4 9204 tree arg2_val;
9205 tree arg2_len;
9206 tree element_size;
9207 if (arg2->type()->is_string_type()
60963afd 9208 && element_type->integer_type() != NULL
9209 && element_type->integer_type()->is_byte())
4fd4fcf4 9210 {
9211 arg2_tree = save_expr(arg2_tree);
9212 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
9213 arg2_len = String_type::length_tree(gogo, arg2_tree);
9214 element_size = size_int(1);
9215 }
9216 else
9217 {
9218 arg2_tree = Expression::convert_for_assignment(context, at,
9219 arg2->type(),
9220 arg2_tree,
9221 location);
9222 if (arg2_tree == error_mark_node)
9223 return error_mark_node;
9224
9225 arg2_tree = save_expr(arg2_tree);
9226
9227 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
9228 arg2_len = at->length_tree(gogo, arg2_tree);
9229
9230 Btype* element_btype = element_type->get_backend(gogo);
9231 tree element_type_tree = type_to_tree(element_btype);
9232 if (element_type_tree == error_mark_node)
9233 return error_mark_node;
9234 element_size = TYPE_SIZE_UNIT(element_type_tree);
9235 }
ed64c8e5 9236
b13c66cd 9237 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9238 arg2_val);
9239 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
9240 arg2_len);
9241 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
3991cb03 9242 element_size);
e440a328 9243
4fd4fcf4 9244 if (arg2_val == error_mark_node
9245 || arg2_len == error_mark_node
9246 || element_size == error_mark_node)
9247 return error_mark_node;
9248
e440a328 9249 // We rebuild the decl each time since the slice types may
9250 // change.
9251 tree append_fndecl = NULL_TREE;
9252 return Gogo::call_builtin(&append_fndecl,
9253 location,
9254 "__go_append",
3991cb03 9255 4,
e440a328 9256 TREE_TYPE(arg1_tree),
e440a328 9257 TREE_TYPE(arg1_tree),
9258 arg1_tree,
3991cb03 9259 ptr_type_node,
9260 arg2_val,
9261 size_type_node,
9262 arg2_len,
9263 size_type_node,
9264 element_size);
e440a328 9265 }
9266
9267 case BUILTIN_REAL:
9268 case BUILTIN_IMAG:
9269 {
9270 const Expression_list* args = this->args();
c484d925 9271 go_assert(args != NULL && args->size() == 1);
e440a328 9272 Expression* arg = args->front();
9273 tree arg_tree = arg->get_tree(context);
9274 if (arg_tree == error_mark_node)
9275 return error_mark_node;
c484d925 9276 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
e440a328 9277 if (this->code_ == BUILTIN_REAL)
b13c66cd 9278 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
e440a328 9279 TREE_TYPE(TREE_TYPE(arg_tree)),
9280 arg_tree);
9281 else
b13c66cd 9282 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
e440a328 9283 TREE_TYPE(TREE_TYPE(arg_tree)),
9284 arg_tree);
9285 }
9286
48080209 9287 case BUILTIN_COMPLEX:
e440a328 9288 {
9289 const Expression_list* args = this->args();
c484d925 9290 go_assert(args != NULL && args->size() == 2);
e440a328 9291 tree r = args->front()->get_tree(context);
9292 tree i = args->back()->get_tree(context);
9293 if (r == error_mark_node || i == error_mark_node)
9294 return error_mark_node;
c484d925 9295 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
e440a328 9296 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
c484d925 9297 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
b13c66cd 9298 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
e440a328 9299 build_complex_type(TREE_TYPE(r)),
9300 r, i);
9301 }
9302
9303 default:
c3e6f413 9304 go_unreachable();
e440a328 9305 }
9306}
9307
9308// We have to support exporting a builtin call expression, because
9309// code can set a constant to the result of a builtin expression.
9310
9311void
9312Builtin_call_expression::do_export(Export* exp) const
9313{
9314 bool ok = false;
9315
9316 mpz_t val;
9317 mpz_init(val);
9318 Type* dummy;
9319 if (this->integer_constant_value(true, val, &dummy))
9320 {
9321 Integer_expression::export_integer(exp, val);
9322 ok = true;
9323 }
9324 mpz_clear(val);
9325
9326 if (!ok)
9327 {
9328 mpfr_t fval;
9329 mpfr_init(fval);
9330 if (this->float_constant_value(fval, &dummy))
9331 {
9332 Float_expression::export_float(exp, fval);
9333 ok = true;
9334 }
9335 mpfr_clear(fval);
9336 }
9337
9338 if (!ok)
9339 {
9340 mpfr_t real;
9341 mpfr_t imag;
9342 mpfr_init(real);
9343 mpfr_init(imag);
9344 if (this->complex_constant_value(real, imag, &dummy))
9345 {
9346 Complex_expression::export_complex(exp, real, imag);
9347 ok = true;
9348 }
9349 mpfr_clear(real);
9350 mpfr_clear(imag);
9351 }
9352
9353 if (!ok)
9354 {
9355 error_at(this->location(), "value is not constant");
9356 return;
9357 }
9358
9359 // A trailing space lets us reliably identify the end of the number.
9360 exp->write_c_string(" ");
9361}
9362
9363// Class Call_expression.
9364
9365// Traversal.
9366
9367int
9368Call_expression::do_traverse(Traverse* traverse)
9369{
9370 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9371 return TRAVERSE_EXIT;
9372 if (this->args_ != NULL)
9373 {
9374 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9375 return TRAVERSE_EXIT;
9376 }
9377 return TRAVERSE_CONTINUE;
9378}
9379
9380// Lower a call statement.
9381
9382Expression*
ceeb4318 9383Call_expression::do_lower(Gogo* gogo, Named_object* function,
9384 Statement_inserter* inserter, int)
e440a328 9385{
b13c66cd 9386 Location loc = this->location();
09ea332d 9387
ceeb4318 9388 // A type cast can look like a function call.
e440a328 9389 if (this->fn_->is_type_expression()
9390 && this->args_ != NULL
9391 && this->args_->size() == 1)
9392 return Expression::make_cast(this->fn_->type(), this->args_->front(),
09ea332d 9393 loc);
e440a328 9394
9395 // Recognize a call to a builtin function.
9396 Func_expression* fne = this->fn_->func_expression();
9397 if (fne != NULL
9398 && fne->named_object()->is_function_declaration()
9399 && fne->named_object()->func_declaration_value()->type()->is_builtin())
9400 return new Builtin_call_expression(gogo, this->fn_, this->args_,
09ea332d 9401 this->is_varargs_, loc);
e440a328 9402
9403 // Handle an argument which is a call to a function which returns
9404 // multiple results.
9405 if (this->args_ != NULL
9406 && this->args_->size() == 1
9407 && this->args_->front()->call_expression() != NULL
9408 && this->fn_->type()->function_type() != NULL)
9409 {
9410 Function_type* fntype = this->fn_->type()->function_type();
9411 size_t rc = this->args_->front()->call_expression()->result_count();
9412 if (rc > 1
9413 && fntype->parameters() != NULL
9414 && (fntype->parameters()->size() == rc
9415 || (fntype->is_varargs()
9416 && fntype->parameters()->size() - 1 <= rc)))
9417 {
9418 Call_expression* call = this->args_->front()->call_expression();
9419 Expression_list* args = new Expression_list;
9420 for (size_t i = 0; i < rc; ++i)
9421 args->push_back(Expression::make_call_result(call, i));
9422 // We can't return a new call expression here, because this
42535814 9423 // one may be referenced by Call_result expressions. We
9424 // also can't delete the old arguments, because we may still
9425 // traverse them somewhere up the call stack. FIXME.
e440a328 9426 this->args_ = args;
9427 }
9428 }
9429
ceeb4318 9430 // If this call returns multiple results, create a temporary
9431 // variable for each result.
9432 size_t rc = this->result_count();
9433 if (rc > 1 && this->results_ == NULL)
9434 {
9435 std::vector<Temporary_statement*>* temps =
9436 new std::vector<Temporary_statement*>;
9437 temps->reserve(rc);
9438 const Typed_identifier_list* results =
9439 this->fn_->type()->function_type()->results();
9440 for (Typed_identifier_list::const_iterator p = results->begin();
9441 p != results->end();
9442 ++p)
9443 {
9444 Temporary_statement* temp = Statement::make_temporary(p->type(),
09ea332d 9445 NULL, loc);
ceeb4318 9446 inserter->insert(temp);
9447 temps->push_back(temp);
9448 }
9449 this->results_ = temps;
9450 }
9451
e440a328 9452 // Handle a call to a varargs function by packaging up the extra
9453 // parameters.
9454 if (this->fn_->type()->function_type() != NULL
9455 && this->fn_->type()->function_type()->is_varargs())
9456 {
9457 Function_type* fntype = this->fn_->type()->function_type();
9458 const Typed_identifier_list* parameters = fntype->parameters();
c484d925 9459 go_assert(parameters != NULL && !parameters->empty());
e440a328 9460 Type* varargs_type = parameters->back().type();
09ea332d 9461 this->lower_varargs(gogo, function, inserter, varargs_type,
9462 parameters->size());
9463 }
9464
9465 // If this is call to a method, call the method directly passing the
9466 // object as the first parameter.
9467 Bound_method_expression* bme = this->fn_->bound_method_expression();
9468 if (bme != NULL)
9469 {
9470 Named_object* method = bme->method();
9471 Expression* first_arg = bme->first_argument();
9472
9473 // We always pass a pointer when calling a method.
9474 if (first_arg->type()->points_to() == NULL
9475 && !first_arg->type()->is_error())
9476 {
9477 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9478 // We may need to create a temporary variable so that we can
9479 // take the address. We can't do that here because it will
9480 // mess up the order of evaluation.
9481 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9482 ue->set_create_temp();
9483 }
9484
9485 // If we are calling a method which was inherited from an
9486 // embedded struct, and the method did not get a stub, then the
9487 // first type may be wrong.
9488 Type* fatype = bme->first_argument_type();
9489 if (fatype != NULL)
9490 {
9491 if (fatype->points_to() == NULL)
9492 fatype = Type::make_pointer_type(fatype);
9493 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9494 }
9495
9496 Expression_list* new_args = new Expression_list();
9497 new_args->push_back(first_arg);
9498 if (this->args_ != NULL)
9499 {
9500 for (Expression_list::const_iterator p = this->args_->begin();
9501 p != this->args_->end();
9502 ++p)
9503 new_args->push_back(*p);
9504 }
9505
9506 // We have to change in place because this structure may be
9507 // referenced by Call_result_expressions. We can't delete the
9508 // old arguments, because we may be traversing them up in some
9509 // caller. FIXME.
9510 this->args_ = new_args;
9511 this->fn_ = Expression::make_func_reference(method, NULL,
9512 bme->location());
e440a328 9513 }
9514
9515 return this;
9516}
9517
9518// Lower a call to a varargs function. FUNCTION is the function in
9519// which the call occurs--it's not the function we are calling.
9520// VARARGS_TYPE is the type of the varargs parameter, a slice type.
9521// PARAM_COUNT is the number of parameters of the function we are
9522// calling; the last of these parameters will be the varargs
9523// parameter.
9524
09ea332d 9525void
e440a328 9526Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
ceeb4318 9527 Statement_inserter* inserter,
e440a328 9528 Type* varargs_type, size_t param_count)
9529{
9530 if (this->varargs_are_lowered_)
09ea332d 9531 return;
e440a328 9532
b13c66cd 9533 Location loc = this->location();
e440a328 9534
c484d925 9535 go_assert(param_count > 0);
411eb89e 9536 go_assert(varargs_type->is_slice_type());
e440a328 9537
9538 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9539 if (arg_count < param_count - 1)
9540 {
9541 // Not enough arguments; will be caught in check_types.
09ea332d 9542 return;
e440a328 9543 }
9544
9545 Expression_list* old_args = this->args_;
9546 Expression_list* new_args = new Expression_list();
9547 bool push_empty_arg = false;
9548 if (old_args == NULL || old_args->empty())
9549 {
c484d925 9550 go_assert(param_count == 1);
e440a328 9551 push_empty_arg = true;
9552 }
9553 else
9554 {
9555 Expression_list::const_iterator pa;
9556 int i = 1;
9557 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9558 {
9559 if (static_cast<size_t>(i) == param_count)
9560 break;
9561 new_args->push_back(*pa);
9562 }
9563
9564 // We have reached the varargs parameter.
9565
9566 bool issued_error = false;
9567 if (pa == old_args->end())
9568 push_empty_arg = true;
9569 else if (pa + 1 == old_args->end() && this->is_varargs_)
9570 new_args->push_back(*pa);
9571 else if (this->is_varargs_)
9572 {
9573 this->report_error(_("too many arguments"));
09ea332d 9574 return;
e440a328 9575 }
e440a328 9576 else
9577 {
9578 Type* element_type = varargs_type->array_type()->element_type();
9579 Expression_list* vals = new Expression_list;
9580 for (; pa != old_args->end(); ++pa, ++i)
9581 {
9582 // Check types here so that we get a better message.
9583 Type* patype = (*pa)->type();
b13c66cd 9584 Location paloc = (*pa)->location();
e440a328 9585 if (!this->check_argument_type(i, element_type, patype,
9586 paloc, issued_error))
9587 continue;
9588 vals->push_back(*pa);
9589 }
9590 Expression* val =
9591 Expression::make_slice_composite_literal(varargs_type, vals, loc);
09ea332d 9592 gogo->lower_expression(function, inserter, &val);
e440a328 9593 new_args->push_back(val);
9594 }
9595 }
9596
9597 if (push_empty_arg)
9598 new_args->push_back(Expression::make_nil(loc));
9599
9600 // We can't return a new call expression here, because this one may
6d4c2432 9601 // be referenced by Call_result expressions. FIXME. We can't
9602 // delete OLD_ARGS because we may have both a Call_expression and a
9603 // Builtin_call_expression which refer to them. FIXME.
e440a328 9604 this->args_ = new_args;
9605 this->varargs_are_lowered_ = true;
e440a328 9606}
9607
ceeb4318 9608// Get the function type. This can return NULL in error cases.
e440a328 9609
9610Function_type*
9611Call_expression::get_function_type() const
9612{
9613 return this->fn_->type()->function_type();
9614}
9615
9616// Return the number of values which this call will return.
9617
9618size_t
9619Call_expression::result_count() const
9620{
9621 const Function_type* fntype = this->get_function_type();
9622 if (fntype == NULL)
9623 return 0;
9624 if (fntype->results() == NULL)
9625 return 0;
9626 return fntype->results()->size();
9627}
9628
ceeb4318 9629// Return the temporary which holds a result.
9630
9631Temporary_statement*
9632Call_expression::result(size_t i) const
9633{
9634 go_assert(this->results_ != NULL
9635 && this->results_->size() > i);
9636 return (*this->results_)[i];
9637}
9638
e440a328 9639// Return whether this is a call to the predeclared function recover.
9640
9641bool
9642Call_expression::is_recover_call() const
9643{
9644 return this->do_is_recover_call();
9645}
9646
9647// Set the argument to the recover function.
9648
9649void
9650Call_expression::set_recover_arg(Expression* arg)
9651{
9652 this->do_set_recover_arg(arg);
9653}
9654
9655// Virtual functions also implemented by Builtin_call_expression.
9656
9657bool
9658Call_expression::do_is_recover_call() const
9659{
9660 return false;
9661}
9662
9663void
9664Call_expression::do_set_recover_arg(Expression*)
9665{
c3e6f413 9666 go_unreachable();
e440a328 9667}
9668
ceeb4318 9669// We have found an error with this call expression; return true if
9670// we should report it.
9671
9672bool
9673Call_expression::issue_error()
9674{
9675 if (this->issued_error_)
9676 return false;
9677 else
9678 {
9679 this->issued_error_ = true;
9680 return true;
9681 }
9682}
9683
e440a328 9684// Get the type.
9685
9686Type*
9687Call_expression::do_type()
9688{
9689 if (this->type_ != NULL)
9690 return this->type_;
9691
9692 Type* ret;
9693 Function_type* fntype = this->get_function_type();
9694 if (fntype == NULL)
9695 return Type::make_error_type();
9696
9697 const Typed_identifier_list* results = fntype->results();
9698 if (results == NULL)
9699 ret = Type::make_void_type();
9700 else if (results->size() == 1)
9701 ret = results->begin()->type();
9702 else
9703 ret = Type::make_call_multiple_result_type(this);
9704
9705 this->type_ = ret;
9706
9707 return this->type_;
9708}
9709
9710// Determine types for a call expression. We can use the function
9711// parameter types to set the types of the arguments.
9712
9713void
9714Call_expression::do_determine_type(const Type_context*)
9715{
fb94b0ca 9716 if (!this->determining_types())
9717 return;
9718
e440a328 9719 this->fn_->determine_type_no_context();
9720 Function_type* fntype = this->get_function_type();
9721 const Typed_identifier_list* parameters = NULL;
9722 if (fntype != NULL)
9723 parameters = fntype->parameters();
9724 if (this->args_ != NULL)
9725 {
9726 Typed_identifier_list::const_iterator pt;
9727 if (parameters != NULL)
9728 pt = parameters->begin();
09ea332d 9729 bool first = true;
e440a328 9730 for (Expression_list::const_iterator pa = this->args_->begin();
9731 pa != this->args_->end();
9732 ++pa)
9733 {
09ea332d 9734 if (first)
9735 {
9736 first = false;
9737 // If this is a method, the first argument is the
9738 // receiver.
9739 if (fntype != NULL && fntype->is_method())
9740 {
9741 Type* rtype = fntype->receiver()->type();
9742 // The receiver is always passed as a pointer.
9743 if (rtype->points_to() == NULL)
9744 rtype = Type::make_pointer_type(rtype);
9745 Type_context subcontext(rtype, false);
9746 (*pa)->determine_type(&subcontext);
9747 continue;
9748 }
9749 }
9750
e440a328 9751 if (parameters != NULL && pt != parameters->end())
9752 {
9753 Type_context subcontext(pt->type(), false);
9754 (*pa)->determine_type(&subcontext);
9755 ++pt;
9756 }
9757 else
9758 (*pa)->determine_type_no_context();
9759 }
9760 }
9761}
9762
fb94b0ca 9763// Called when determining types for a Call_expression. Return true
9764// if we should go ahead, false if they have already been determined.
9765
9766bool
9767Call_expression::determining_types()
9768{
9769 if (this->types_are_determined_)
9770 return false;
9771 else
9772 {
9773 this->types_are_determined_ = true;
9774 return true;
9775 }
9776}
9777
e440a328 9778// Check types for parameter I.
9779
9780bool
9781Call_expression::check_argument_type(int i, const Type* parameter_type,
9782 const Type* argument_type,
b13c66cd 9783 Location argument_location,
e440a328 9784 bool issued_error)
9785{
9786 std::string reason;
053ee6ca 9787 bool ok;
9788 if (this->are_hidden_fields_ok_)
9789 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
9790 &reason);
9791 else
9792 ok = Type::are_assignable(parameter_type, argument_type, &reason);
9793 if (!ok)
e440a328 9794 {
9795 if (!issued_error)
9796 {
9797 if (reason.empty())
9798 error_at(argument_location, "argument %d has incompatible type", i);
9799 else
9800 error_at(argument_location,
9801 "argument %d has incompatible type (%s)",
9802 i, reason.c_str());
9803 }
9804 this->set_is_error();
9805 return false;
9806 }
9807 return true;
9808}
9809
9810// Check types.
9811
9812void
9813Call_expression::do_check_types(Gogo*)
9814{
9815 Function_type* fntype = this->get_function_type();
9816 if (fntype == NULL)
9817 {
5c13bd80 9818 if (!this->fn_->type()->is_error())
e440a328 9819 this->report_error(_("expected function"));
9820 return;
9821 }
9822
09ea332d 9823 bool is_method = fntype->is_method();
9824 if (is_method)
e440a328 9825 {
09ea332d 9826 go_assert(this->args_ != NULL && !this->args_->empty());
9827 Type* rtype = fntype->receiver()->type();
9828 Expression* first_arg = this->args_->front();
9829 // The language permits copying hidden fields for a method
9830 // receiver. We dereference the values since receivers are
9831 // always passed as pointers.
9832 std::string reason;
9833 if (!Type::are_assignable_hidden_ok(rtype->deref(),
9834 first_arg->type()->deref(),
9835 &reason))
e440a328 9836 {
09ea332d 9837 if (reason.empty())
9838 this->report_error(_("incompatible type for receiver"));
9839 else
e440a328 9840 {
09ea332d 9841 error_at(this->location(),
9842 "incompatible type for receiver (%s)",
9843 reason.c_str());
9844 this->set_is_error();
e440a328 9845 }
9846 }
9847 }
9848
9849 // Note that varargs was handled by the lower_varargs() method, so
9850 // we don't have to worry about it here.
9851
9852 const Typed_identifier_list* parameters = fntype->parameters();
9853 if (this->args_ == NULL)
9854 {
9855 if (parameters != NULL && !parameters->empty())
9856 this->report_error(_("not enough arguments"));
9857 }
9858 else if (parameters == NULL)
09ea332d 9859 {
9860 if (!is_method || this->args_->size() > 1)
9861 this->report_error(_("too many arguments"));
9862 }
e440a328 9863 else
9864 {
9865 int i = 0;
09ea332d 9866 Expression_list::const_iterator pa = this->args_->begin();
9867 if (is_method)
9868 ++pa;
9869 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9870 pt != parameters->end();
9871 ++pt, ++pa, ++i)
e440a328 9872 {
09ea332d 9873 if (pa == this->args_->end())
e440a328 9874 {
09ea332d 9875 this->report_error(_("not enough arguments"));
e440a328 9876 return;
9877 }
9878 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9879 (*pa)->location(), false);
9880 }
09ea332d 9881 if (pa != this->args_->end())
9882 this->report_error(_("too many arguments"));
e440a328 9883 }
9884}
9885
9886// Return whether we have to use a temporary variable to ensure that
9887// we evaluate this call expression in order. If the call returns no
ceeb4318 9888// results then it will inevitably be executed last.
e440a328 9889
9890bool
9891Call_expression::do_must_eval_in_order() const
9892{
ceeb4318 9893 return this->result_count() > 0;
e440a328 9894}
9895
e440a328 9896// Get the function and the first argument to use when calling an
9897// interface method.
9898
9899tree
9900Call_expression::interface_method_function(
9901 Translate_context* context,
9902 Interface_field_reference_expression* interface_method,
9903 tree* first_arg_ptr)
9904{
9905 tree expr = interface_method->expr()->get_tree(context);
9906 if (expr == error_mark_node)
9907 return error_mark_node;
9908 expr = save_expr(expr);
9909 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9910 if (first_arg == error_mark_node)
9911 return error_mark_node;
9912 *first_arg_ptr = first_arg;
9913 return interface_method->get_function_tree(context, expr);
9914}
9915
9916// Build the call expression.
9917
9918tree
9919Call_expression::do_get_tree(Translate_context* context)
9920{
9921 if (this->tree_ != NULL_TREE)
9922 return this->tree_;
9923
9924 Function_type* fntype = this->get_function_type();
9925 if (fntype == NULL)
9926 return error_mark_node;
9927
9928 if (this->fn_->is_error_expression())
9929 return error_mark_node;
9930
9931 Gogo* gogo = context->gogo();
b13c66cd 9932 Location location = this->location();
e440a328 9933
9934 Func_expression* func = this->fn_->func_expression();
e440a328 9935 Interface_field_reference_expression* interface_method =
9936 this->fn_->interface_field_reference_expression();
9937 const bool has_closure = func != NULL && func->closure() != NULL;
09ea332d 9938 const bool is_interface_method = interface_method != NULL;
e440a328 9939
9940 int nargs;
9941 tree* args;
9942 if (this->args_ == NULL || this->args_->empty())
9943 {
09ea332d 9944 nargs = is_interface_method ? 1 : 0;
e440a328 9945 args = nargs == 0 ? NULL : new tree[nargs];
9946 }
09ea332d 9947 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9948 {
9949 // Passing a receiver parameter.
9950 go_assert(!is_interface_method
9951 && fntype->is_method()
9952 && this->args_->size() == 1);
9953 nargs = 1;
9954 args = new tree[nargs];
9955 args[0] = this->args_->front()->get_tree(context);
9956 }
e440a328 9957 else
9958 {
9959 const Typed_identifier_list* params = fntype->parameters();
e440a328 9960
9961 nargs = this->args_->size();
09ea332d 9962 int i = is_interface_method ? 1 : 0;
e440a328 9963 nargs += i;
9964 args = new tree[nargs];
9965
9966 Typed_identifier_list::const_iterator pp = params->begin();
09ea332d 9967 Expression_list::const_iterator pe = this->args_->begin();
9968 if (!is_interface_method && fntype->is_method())
9969 {
9970 args[i] = (*pe)->get_tree(context);
9971 ++pe;
9972 ++i;
9973 }
9974 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
e440a328 9975 {
c484d925 9976 go_assert(pp != params->end());
e440a328 9977 tree arg_val = (*pe)->get_tree(context);
9978 args[i] = Expression::convert_for_assignment(context,
9979 pp->type(),
9980 (*pe)->type(),
9981 arg_val,
9982 location);
9983 if (args[i] == error_mark_node)
cf609de4 9984 {
9985 delete[] args;
9986 return error_mark_node;
9987 }
e440a328 9988 }
c484d925 9989 go_assert(pp == params->end());
9990 go_assert(i == nargs);
e440a328 9991 }
9992
9f0e0513 9993 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
e440a328 9994 if (rettype == error_mark_node)
cf609de4 9995 {
9996 delete[] args;
9997 return error_mark_node;
9998 }
e440a328 9999
10000 tree fn;
10001 if (has_closure)
10002 fn = func->get_tree_without_closure(gogo);
09ea332d 10003 else if (!is_interface_method)
e440a328 10004 fn = this->fn_->get_tree(context);
e440a328 10005 else
09ea332d 10006 fn = this->interface_method_function(context, interface_method, &args[0]);
e440a328 10007
10008 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
cf609de4 10009 {
10010 delete[] args;
10011 return error_mark_node;
10012 }
e440a328 10013
e440a328 10014 tree fndecl = fn;
10015 if (TREE_CODE(fndecl) == ADDR_EXPR)
10016 fndecl = TREE_OPERAND(fndecl, 0);
9aa9e2df 10017
10018 // Add a type cast in case the type of the function is a recursive
10019 // type which refers to itself.
10020 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
10021 {
9f0e0513 10022 tree fnt = type_to_tree(fntype->get_backend(gogo));
9aa9e2df 10023 if (fnt == error_mark_node)
10024 return error_mark_node;
b13c66cd 10025 fn = fold_convert_loc(location.gcc_location(), fnt, fn);
9aa9e2df 10026 }
10027
10028 // This is to support builtin math functions when using 80387 math.
e440a328 10029 tree excess_type = NULL_TREE;
68e1881d 10030 if (optimize
10031 && TREE_CODE(fndecl) == FUNCTION_DECL
e440a328 10032 && DECL_IS_BUILTIN(fndecl)
10033 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
10034 && nargs > 0
10035 && ((SCALAR_FLOAT_TYPE_P(rettype)
10036 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
10037 || (COMPLEX_FLOAT_TYPE_P(rettype)
10038 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
10039 {
10040 excess_type = excess_precision_type(TREE_TYPE(args[0]));
10041 if (excess_type != NULL_TREE)
10042 {
10043 tree excess_fndecl = mathfn_built_in(excess_type,
10044 DECL_FUNCTION_CODE(fndecl));
10045 if (excess_fndecl == NULL_TREE)
10046 excess_type = NULL_TREE;
10047 else
10048 {
b13c66cd 10049 fn = build_fold_addr_expr_loc(location.gcc_location(),
10050 excess_fndecl);
e440a328 10051 for (int i = 0; i < nargs; ++i)
26ae0101 10052 {
10053 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
10054 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
10055 args[i] = ::convert(excess_type, args[i]);
10056 }
e440a328 10057 }
10058 }
10059 }
10060
10061 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
10062 fn, nargs, args);
10063 delete[] args;
10064
b13c66cd 10065 SET_EXPR_LOCATION(ret, location.gcc_location());
e440a328 10066
10067 if (has_closure)
10068 {
10069 tree closure_tree = func->closure()->get_tree(context);
10070 if (closure_tree != error_mark_node)
10071 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
10072 }
10073
10074 // If this is a recursive function type which returns itself, as in
10075 // type F func() F
10076 // we have used ptr_type_node for the return type. Add a cast here
10077 // to the correct type.
10078 if (TREE_TYPE(ret) == ptr_type_node)
10079 {
9f0e0513 10080 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
b13c66cd 10081 ret = fold_convert_loc(location.gcc_location(), t, ret);
e440a328 10082 }
10083
10084 if (excess_type != NULL_TREE)
10085 {
10086 // Calling convert here can undo our excess precision change.
10087 // That may or may not be a bug in convert_to_real.
10088 ret = build1(NOP_EXPR, rettype, ret);
10089 }
10090
ceeb4318 10091 if (this->results_ != NULL)
10092 ret = this->set_results(context, ret);
e440a328 10093
10094 this->tree_ = ret;
10095
10096 return ret;
10097}
10098
ceeb4318 10099// Set the result variables if this call returns multiple results.
10100
10101tree
10102Call_expression::set_results(Translate_context* context, tree call_tree)
10103{
10104 tree stmt_list = NULL_TREE;
10105
10106 call_tree = save_expr(call_tree);
10107
10108 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
10109 {
10110 go_assert(saw_errors());
10111 return call_tree;
10112 }
10113
b13c66cd 10114 Location loc = this->location();
ceeb4318 10115 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
10116 size_t rc = this->result_count();
10117 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
10118 {
10119 go_assert(field != NULL_TREE);
10120
10121 Temporary_statement* temp = this->result(i);
10122 Temporary_reference_expression* ref =
10123 Expression::make_temporary_reference(temp, loc);
10124 ref->set_is_lvalue();
10125 tree temp_tree = ref->get_tree(context);
10126 if (temp_tree == error_mark_node)
10127 continue;
10128
b13c66cd 10129 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
10130 TREE_TYPE(field), call_tree, field, NULL_TREE);
10131 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
10132 void_type_node, temp_tree, val_tree);
ceeb4318 10133
10134 append_to_statement_list(set_tree, &stmt_list);
10135 }
10136 go_assert(field == NULL_TREE);
10137
10138 return save_expr(stmt_list);
10139}
10140
d751bb78 10141// Dump ast representation for a call expressin.
10142
10143void
10144Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
10145{
10146 this->fn_->dump_expression(ast_dump_context);
10147 ast_dump_context->ostream() << "(";
10148 if (args_ != NULL)
10149 ast_dump_context->dump_expression_list(this->args_);
10150
10151 ast_dump_context->ostream() << ") ";
10152}
10153
e440a328 10154// Make a call expression.
10155
10156Call_expression*
10157Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
b13c66cd 10158 Location location)
e440a328 10159{
10160 return new Call_expression(fn, args, is_varargs, location);
10161}
10162
10163// A single result from a call which returns multiple results.
10164
10165class Call_result_expression : public Expression
10166{
10167 public:
10168 Call_result_expression(Call_expression* call, unsigned int index)
10169 : Expression(EXPRESSION_CALL_RESULT, call->location()),
10170 call_(call), index_(index)
10171 { }
10172
10173 protected:
10174 int
10175 do_traverse(Traverse*);
10176
10177 Type*
10178 do_type();
10179
10180 void
10181 do_determine_type(const Type_context*);
10182
10183 void
10184 do_check_types(Gogo*);
10185
10186 Expression*
10187 do_copy()
10188 {
10189 return new Call_result_expression(this->call_->call_expression(),
10190 this->index_);
10191 }
10192
10193 bool
10194 do_must_eval_in_order() const
10195 { return true; }
10196
10197 tree
10198 do_get_tree(Translate_context*);
10199
d751bb78 10200 void
10201 do_dump_expression(Ast_dump_context*) const;
10202
e440a328 10203 private:
10204 // The underlying call expression.
10205 Expression* call_;
10206 // Which result we want.
10207 unsigned int index_;
10208};
10209
10210// Traverse a call result.
10211
10212int
10213Call_result_expression::do_traverse(Traverse* traverse)
10214{
10215 if (traverse->remember_expression(this->call_))
10216 {
10217 // We have already traversed the call expression.
10218 return TRAVERSE_CONTINUE;
10219 }
10220 return Expression::traverse(&this->call_, traverse);
10221}
10222
10223// Get the type.
10224
10225Type*
10226Call_result_expression::do_type()
10227{
425dd051 10228 if (this->classification() == EXPRESSION_ERROR)
10229 return Type::make_error_type();
10230
e440a328 10231 // THIS->CALL_ can be replaced with a temporary reference due to
10232 // Call_expression::do_must_eval_in_order when there is an error.
10233 Call_expression* ce = this->call_->call_expression();
10234 if (ce == NULL)
5e85f268 10235 {
10236 this->set_is_error();
10237 return Type::make_error_type();
10238 }
e440a328 10239 Function_type* fntype = ce->get_function_type();
10240 if (fntype == NULL)
5e85f268 10241 {
e37658e2 10242 if (ce->issue_error())
99b3f06f 10243 {
10244 if (!ce->fn()->type()->is_error())
10245 this->report_error(_("expected function"));
10246 }
5e85f268 10247 this->set_is_error();
10248 return Type::make_error_type();
10249 }
e440a328 10250 const Typed_identifier_list* results = fntype->results();
ceeb4318 10251 if (results == NULL || results->size() < 2)
7b8d861f 10252 {
ceeb4318 10253 if (ce->issue_error())
10254 this->report_error(_("number of results does not match "
10255 "number of values"));
7b8d861f 10256 return Type::make_error_type();
10257 }
e440a328 10258 Typed_identifier_list::const_iterator pr = results->begin();
10259 for (unsigned int i = 0; i < this->index_; ++i)
10260 {
10261 if (pr == results->end())
425dd051 10262 break;
e440a328 10263 ++pr;
10264 }
10265 if (pr == results->end())
425dd051 10266 {
ceeb4318 10267 if (ce->issue_error())
10268 this->report_error(_("number of results does not match "
10269 "number of values"));
425dd051 10270 return Type::make_error_type();
10271 }
e440a328 10272 return pr->type();
10273}
10274
425dd051 10275// Check the type. Just make sure that we trigger the warning in
10276// do_type.
e440a328 10277
10278void
10279Call_result_expression::do_check_types(Gogo*)
10280{
425dd051 10281 this->type();
e440a328 10282}
10283
10284// Determine the type. We have nothing to do here, but the 0 result
10285// needs to pass down to the caller.
10286
10287void
10288Call_result_expression::do_determine_type(const Type_context*)
10289{
fb94b0ca 10290 this->call_->determine_type_no_context();
e440a328 10291}
10292
ceeb4318 10293// Return the tree. We just refer to the temporary set by the call
10294// expression. We don't do this at lowering time because it makes it
10295// hard to evaluate the call at the right time.
e440a328 10296
10297tree
10298Call_result_expression::do_get_tree(Translate_context* context)
10299{
ceeb4318 10300 Call_expression* ce = this->call_->call_expression();
10301 go_assert(ce != NULL);
10302 Temporary_statement* ts = ce->result(this->index_);
10303 Expression* ref = Expression::make_temporary_reference(ts, this->location());
10304 return ref->get_tree(context);
e440a328 10305}
10306
d751bb78 10307// Dump ast representation for a call result expression.
10308
10309void
10310Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10311 const
10312{
10313 // FIXME: Wouldn't it be better if the call is assigned to a temporary
10314 // (struct) and the fields are referenced instead.
10315 ast_dump_context->ostream() << this->index_ << "@(";
10316 ast_dump_context->dump_expression(this->call_);
10317 ast_dump_context->ostream() << ")";
10318}
10319
e440a328 10320// Make a reference to a single result of a call which returns
10321// multiple results.
10322
10323Expression*
10324Expression::make_call_result(Call_expression* call, unsigned int index)
10325{
10326 return new Call_result_expression(call, index);
10327}
10328
10329// Class Index_expression.
10330
10331// Traversal.
10332
10333int
10334Index_expression::do_traverse(Traverse* traverse)
10335{
10336 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
10337 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
10338 || (this->end_ != NULL
10339 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
10340 return TRAVERSE_EXIT;
10341 return TRAVERSE_CONTINUE;
10342}
10343
10344// Lower an index expression. This converts the generic index
10345// expression into an array index, a string index, or a map index.
10346
10347Expression*
ceeb4318 10348Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 10349{
b13c66cd 10350 Location location = this->location();
e440a328 10351 Expression* left = this->left_;
10352 Expression* start = this->start_;
10353 Expression* end = this->end_;
10354
10355 Type* type = left->type();
5c13bd80 10356 if (type->is_error())
e440a328 10357 return Expression::make_error(location);
b0cf7ddd 10358 else if (left->is_type_expression())
10359 {
10360 error_at(location, "attempt to index type expression");
10361 return Expression::make_error(location);
10362 }
e440a328 10363 else if (type->array_type() != NULL)
10364 return Expression::make_array_index(left, start, end, location);
10365 else if (type->points_to() != NULL
10366 && type->points_to()->array_type() != NULL
411eb89e 10367 && !type->points_to()->is_slice_type())
e440a328 10368 {
10369 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
10370 location);
10371 return Expression::make_array_index(deref, start, end, location);
10372 }
10373 else if (type->is_string_type())
10374 return Expression::make_string_index(left, start, end, location);
10375 else if (type->map_type() != NULL)
10376 {
10377 if (end != NULL)
10378 {
10379 error_at(location, "invalid slice of map");
10380 return Expression::make_error(location);
10381 }
6d4c2432 10382 Map_index_expression* ret = Expression::make_map_index(left, start,
10383 location);
e440a328 10384 if (this->is_lvalue_)
10385 ret->set_is_lvalue();
10386 return ret;
10387 }
10388 else
10389 {
10390 error_at(location,
10391 "attempt to index object which is not array, string, or map");
10392 return Expression::make_error(location);
10393 }
10394}
10395
d751bb78 10396// Write an indexed expression (expr[expr:expr] or expr[expr]) to a
10397// dump context
10398
10399void
10400Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10401 const Expression* expr,
10402 const Expression* start,
10403 const Expression* end)
10404{
10405 expr->dump_expression(ast_dump_context);
10406 ast_dump_context->ostream() << "[";
10407 start->dump_expression(ast_dump_context);
10408 if (end != NULL)
10409 {
10410 ast_dump_context->ostream() << ":";
10411 end->dump_expression(ast_dump_context);
10412 }
10413 ast_dump_context->ostream() << "]";
10414}
10415
10416// Dump ast representation for an index expression.
10417
10418void
10419Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10420 const
10421{
10422 Index_expression::dump_index_expression(ast_dump_context, this->left_,
10423 this->start_, this->end_);
10424}
10425
e440a328 10426// Make an index expression.
10427
10428Expression*
10429Expression::make_index(Expression* left, Expression* start, Expression* end,
b13c66cd 10430 Location location)
e440a328 10431{
10432 return new Index_expression(left, start, end, location);
10433}
10434
10435// An array index. This is used for both indexing and slicing.
10436
10437class Array_index_expression : public Expression
10438{
10439 public:
10440 Array_index_expression(Expression* array, Expression* start,
b13c66cd 10441 Expression* end, Location location)
e440a328 10442 : Expression(EXPRESSION_ARRAY_INDEX, location),
10443 array_(array), start_(start), end_(end), type_(NULL)
10444 { }
10445
10446 protected:
10447 int
10448 do_traverse(Traverse*);
10449
10450 Type*
10451 do_type();
10452
10453 void
10454 do_determine_type(const Type_context*);
10455
10456 void
10457 do_check_types(Gogo*);
10458
10459 Expression*
10460 do_copy()
10461 {
10462 return Expression::make_array_index(this->array_->copy(),
10463 this->start_->copy(),
10464 (this->end_ == NULL
10465 ? NULL
10466 : this->end_->copy()),
10467 this->location());
10468 }
10469
baef9f7a 10470 bool
10471 do_must_eval_subexpressions_in_order(int* skip) const
10472 {
10473 *skip = 1;
10474 return true;
10475 }
10476
e440a328 10477 bool
10478 do_is_addressable() const;
10479
10480 void
10481 do_address_taken(bool escapes)
10482 { this->array_->address_taken(escapes); }
10483
10484 tree
10485 do_get_tree(Translate_context*);
10486
d751bb78 10487 void
10488 do_dump_expression(Ast_dump_context*) const;
10489
e440a328 10490 private:
10491 // The array we are getting a value from.
10492 Expression* array_;
10493 // The start or only index.
10494 Expression* start_;
10495 // The end index of a slice. This may be NULL for a simple array
10496 // index, or it may be a nil expression for the length of the array.
10497 Expression* end_;
10498 // The type of the expression.
10499 Type* type_;
10500};
10501
10502// Array index traversal.
10503
10504int
10505Array_index_expression::do_traverse(Traverse* traverse)
10506{
10507 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10508 return TRAVERSE_EXIT;
10509 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10510 return TRAVERSE_EXIT;
10511 if (this->end_ != NULL)
10512 {
10513 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10514 return TRAVERSE_EXIT;
10515 }
10516 return TRAVERSE_CONTINUE;
10517}
10518
10519// Return the type of an array index.
10520
10521Type*
10522Array_index_expression::do_type()
10523{
10524 if (this->type_ == NULL)
10525 {
10526 Array_type* type = this->array_->type()->array_type();
10527 if (type == NULL)
10528 this->type_ = Type::make_error_type();
10529 else if (this->end_ == NULL)
10530 this->type_ = type->element_type();
411eb89e 10531 else if (type->is_slice_type())
e440a328 10532 {
10533 // A slice of a slice has the same type as the original
10534 // slice.
10535 this->type_ = this->array_->type()->deref();
10536 }
10537 else
10538 {
10539 // A slice of an array is a slice.
10540 this->type_ = Type::make_array_type(type->element_type(), NULL);
10541 }
10542 }
10543 return this->type_;
10544}
10545
10546// Set the type of an array index.
10547
10548void
10549Array_index_expression::do_determine_type(const Type_context*)
10550{
10551 this->array_->determine_type_no_context();
7917ad68 10552 this->start_->determine_type_no_context();
e440a328 10553 if (this->end_ != NULL)
7917ad68 10554 this->end_->determine_type_no_context();
e440a328 10555}
10556
10557// Check types of an array index.
10558
10559void
10560Array_index_expression::do_check_types(Gogo*)
10561{
10562 if (this->start_->type()->integer_type() == NULL)
10563 this->report_error(_("index must be integer"));
10564 if (this->end_ != NULL
10565 && this->end_->type()->integer_type() == NULL
99b3f06f 10566 && !this->end_->type()->is_error()
10567 && !this->end_->is_nil_expression()
10568 && !this->end_->is_error_expression())
e440a328 10569 this->report_error(_("slice end must be integer"));
10570
10571 Array_type* array_type = this->array_->type()->array_type();
f9c68f17 10572 if (array_type == NULL)
10573 {
c484d925 10574 go_assert(this->array_->type()->is_error());
f9c68f17 10575 return;
10576 }
e440a328 10577
10578 unsigned int int_bits =
10579 Type::lookup_integer_type("int")->integer_type()->bits();
10580
10581 Type* dummy;
10582 mpz_t lval;
10583 mpz_init(lval);
10584 bool lval_valid = (array_type->length() != NULL
10585 && array_type->length()->integer_constant_value(true,
10586 lval,
10587 &dummy));
10588 mpz_t ival;
10589 mpz_init(ival);
10590 if (this->start_->integer_constant_value(true, ival, &dummy))
10591 {
10592 if (mpz_sgn(ival) < 0
10593 || mpz_sizeinbase(ival, 2) >= int_bits
10594 || (lval_valid
10595 && (this->end_ == NULL
10596 ? mpz_cmp(ival, lval) >= 0
10597 : mpz_cmp(ival, lval) > 0)))
10598 {
10599 error_at(this->start_->location(), "array index out of bounds");
10600 this->set_is_error();
10601 }
10602 }
10603 if (this->end_ != NULL && !this->end_->is_nil_expression())
10604 {
10605 if (this->end_->integer_constant_value(true, ival, &dummy))
10606 {
10607 if (mpz_sgn(ival) < 0
10608 || mpz_sizeinbase(ival, 2) >= int_bits
10609 || (lval_valid && mpz_cmp(ival, lval) > 0))
10610 {
10611 error_at(this->end_->location(), "array index out of bounds");
10612 this->set_is_error();
10613 }
10614 }
10615 }
10616 mpz_clear(ival);
10617 mpz_clear(lval);
10618
10619 // A slice of an array requires an addressable array. A slice of a
10620 // slice is always possible.
411eb89e 10621 if (this->end_ != NULL && !array_type->is_slice_type())
88ec30c8 10622 {
10623 if (!this->array_->is_addressable())
8da39c3b 10624 this->report_error(_("slice of unaddressable value"));
88ec30c8 10625 else
10626 this->array_->address_taken(true);
10627 }
e440a328 10628}
10629
10630// Return whether this expression is addressable.
10631
10632bool
10633Array_index_expression::do_is_addressable() const
10634{
10635 // A slice expression is not addressable.
10636 if (this->end_ != NULL)
10637 return false;
10638
10639 // An index into a slice is addressable.
411eb89e 10640 if (this->array_->type()->is_slice_type())
e440a328 10641 return true;
10642
10643 // An index into an array is addressable if the array is
10644 // addressable.
10645 return this->array_->is_addressable();
10646}
10647
10648// Get a tree for an array index.
10649
10650tree
10651Array_index_expression::do_get_tree(Translate_context* context)
10652{
10653 Gogo* gogo = context->gogo();
b13c66cd 10654 Location loc = this->location();
e440a328 10655
10656 Array_type* array_type = this->array_->type()->array_type();
d8cd8e2d 10657 if (array_type == NULL)
10658 {
c484d925 10659 go_assert(this->array_->type()->is_error());
d8cd8e2d 10660 return error_mark_node;
10661 }
e440a328 10662
9f0e0513 10663 tree type_tree = type_to_tree(array_type->get_backend(gogo));
c65212a0 10664 if (type_tree == error_mark_node)
10665 return error_mark_node;
e440a328 10666
10667 tree array_tree = this->array_->get_tree(context);
10668 if (array_tree == error_mark_node)
10669 return error_mark_node;
10670
10671 if (array_type->length() == NULL && !DECL_P(array_tree))
10672 array_tree = save_expr(array_tree);
a04bfdfc 10673
10674 tree length_tree = NULL_TREE;
10675 if (this->end_ == NULL || this->end_->is_nil_expression())
10676 {
10677 length_tree = array_type->length_tree(gogo, array_tree);
10678 if (length_tree == error_mark_node)
10679 return error_mark_node;
10680 length_tree = save_expr(length_tree);
10681 }
10682
10683 tree capacity_tree = NULL_TREE;
10684 if (this->end_ != NULL)
10685 {
10686 capacity_tree = array_type->capacity_tree(gogo, array_tree);
10687 if (capacity_tree == error_mark_node)
10688 return error_mark_node;
10689 capacity_tree = save_expr(capacity_tree);
10690 }
10691
10692 tree length_type = (length_tree != NULL_TREE
10693 ? TREE_TYPE(length_tree)
10694 : TREE_TYPE(capacity_tree));
e440a328 10695
10696 tree bad_index = boolean_false_node;
10697
10698 tree start_tree = this->start_->get_tree(context);
10699 if (start_tree == error_mark_node)
10700 return error_mark_node;
10701 if (!DECL_P(start_tree))
10702 start_tree = save_expr(start_tree);
10703 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10704 start_tree = convert_to_integer(length_type, start_tree);
10705
10706 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10707 loc);
10708
b13c66cd 10709 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10710 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10711 boolean_type_node, bad_index,
10712 fold_build2_loc(loc.gcc_location(),
e440a328 10713 (this->end_ == NULL
10714 ? GE_EXPR
10715 : GT_EXPR),
10716 boolean_type_node, start_tree,
a04bfdfc 10717 (this->end_ == NULL
10718 ? length_tree
10719 : capacity_tree)));
e440a328 10720
10721 int code = (array_type->length() != NULL
10722 ? (this->end_ == NULL
10723 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10724 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10725 : (this->end_ == NULL
10726 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10727 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
10728 tree crash = Gogo::runtime_error(code, loc);
10729
10730 if (this->end_ == NULL)
10731 {
10732 // Simple array indexing. This has to return an l-value, so
10733 // wrap the index check into START_TREE.
10734 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
10735 build3(COND_EXPR, void_type_node,
10736 bad_index, crash, NULL_TREE),
10737 start_tree);
b13c66cd 10738 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
e440a328 10739
10740 if (array_type->length() != NULL)
10741 {
10742 // Fixed array.
10743 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
10744 start_tree, NULL_TREE, NULL_TREE);
10745 }
10746 else
10747 {
10748 // Open array.
10749 tree values = array_type->value_pointer_tree(gogo, array_tree);
9f0e0513 10750 Type* element_type = array_type->element_type();
10751 Btype* belement_type = element_type->get_backend(gogo);
10752 tree element_type_tree = type_to_tree(belement_type);
c65212a0 10753 if (element_type_tree == error_mark_node)
10754 return error_mark_node;
e440a328 10755 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
b13c66cd 10756 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
e440a328 10757 start_tree, element_size);
b13c66cd 10758 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
e440a328 10759 TREE_TYPE(values), values, offset);
10760 return build_fold_indirect_ref(ptr);
10761 }
10762 }
10763
10764 // Array slice.
10765
e440a328 10766 tree end_tree;
10767 if (this->end_->is_nil_expression())
10768 end_tree = length_tree;
10769 else
10770 {
10771 end_tree = this->end_->get_tree(context);
10772 if (end_tree == error_mark_node)
10773 return error_mark_node;
10774 if (!DECL_P(end_tree))
10775 end_tree = save_expr(end_tree);
10776 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10777 end_tree = convert_to_integer(length_type, end_tree);
10778
10779 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
10780 loc);
10781
b13c66cd 10782 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
e440a328 10783
b13c66cd 10784 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10785 boolean_type_node,
10786 fold_build2_loc(loc.gcc_location(),
10787 LT_EXPR, boolean_type_node,
e440a328 10788 end_tree, start_tree),
b13c66cd 10789 fold_build2_loc(loc.gcc_location(),
10790 GT_EXPR, boolean_type_node,
e440a328 10791 end_tree, capacity_tree));
b13c66cd 10792 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10793 boolean_type_node, bad_index, bad_end);
e440a328 10794 }
10795
9f0e0513 10796 Type* element_type = array_type->element_type();
10797 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
c65212a0 10798 if (element_type_tree == error_mark_node)
10799 return error_mark_node;
e440a328 10800 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10801
b13c66cd 10802 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10803 fold_convert_loc(loc.gcc_location(), sizetype,
10804 start_tree),
e440a328 10805 element_size);
10806
10807 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
c65212a0 10808 if (value_pointer == error_mark_node)
10809 return error_mark_node;
e440a328 10810
b13c66cd 10811 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
e440a328 10812 TREE_TYPE(value_pointer),
10813 value_pointer, offset);
10814
b13c66cd 10815 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10816 length_type, end_tree, start_tree);
e440a328 10817
b13c66cd 10818 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10819 length_type, capacity_tree,
10820 start_tree);
e440a328 10821
9f0e0513 10822 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
c484d925 10823 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
e440a328 10824
10825 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10826
10827 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10828 tree field = TYPE_FIELDS(struct_tree);
c484d925 10829 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
e440a328 10830 elt->index = field;
10831 elt->value = value_pointer;
10832
10833 elt = VEC_quick_push(constructor_elt, init, NULL);
10834 field = DECL_CHAIN(field);
c484d925 10835 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
e440a328 10836 elt->index = field;
b13c66cd 10837 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10838 result_length_tree);
e440a328 10839
10840 elt = VEC_quick_push(constructor_elt, init, NULL);
10841 field = DECL_CHAIN(field);
c484d925 10842 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
e440a328 10843 elt->index = field;
b13c66cd 10844 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10845 result_capacity_tree);
e440a328 10846
10847 tree constructor = build_constructor(struct_tree, init);
10848
10849 if (TREE_CONSTANT(value_pointer)
10850 && TREE_CONSTANT(result_length_tree)
10851 && TREE_CONSTANT(result_capacity_tree))
10852 TREE_CONSTANT(constructor) = 1;
10853
b13c66cd 10854 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
10855 TREE_TYPE(constructor),
e440a328 10856 build3(COND_EXPR, void_type_node,
10857 bad_index, crash, NULL_TREE),
10858 constructor);
10859}
10860
d751bb78 10861// Dump ast representation for an array index expression.
10862
10863void
10864Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10865 const
10866{
10867 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10868 this->start_, this->end_);
10869}
10870
e440a328 10871// Make an array index expression. END may be NULL.
10872
10873Expression*
10874Expression::make_array_index(Expression* array, Expression* start,
b13c66cd 10875 Expression* end, Location location)
e440a328 10876{
e440a328 10877 return new Array_index_expression(array, start, end, location);
10878}
10879
10880// A string index. This is used for both indexing and slicing.
10881
10882class String_index_expression : public Expression
10883{
10884 public:
10885 String_index_expression(Expression* string, Expression* start,
b13c66cd 10886 Expression* end, Location location)
e440a328 10887 : Expression(EXPRESSION_STRING_INDEX, location),
10888 string_(string), start_(start), end_(end)
10889 { }
10890
10891 protected:
10892 int
10893 do_traverse(Traverse*);
10894
10895 Type*
10896 do_type();
10897
10898 void
10899 do_determine_type(const Type_context*);
10900
10901 void
10902 do_check_types(Gogo*);
10903
10904 Expression*
10905 do_copy()
10906 {
10907 return Expression::make_string_index(this->string_->copy(),
10908 this->start_->copy(),
10909 (this->end_ == NULL
10910 ? NULL
10911 : this->end_->copy()),
10912 this->location());
10913 }
10914
baef9f7a 10915 bool
10916 do_must_eval_subexpressions_in_order(int* skip) const
10917 {
10918 *skip = 1;
10919 return true;
10920 }
10921
e440a328 10922 tree
10923 do_get_tree(Translate_context*);
10924
d751bb78 10925 void
10926 do_dump_expression(Ast_dump_context*) const;
10927
e440a328 10928 private:
10929 // The string we are getting a value from.
10930 Expression* string_;
10931 // The start or only index.
10932 Expression* start_;
10933 // The end index of a slice. This may be NULL for a single index,
10934 // or it may be a nil expression for the length of the string.
10935 Expression* end_;
10936};
10937
10938// String index traversal.
10939
10940int
10941String_index_expression::do_traverse(Traverse* traverse)
10942{
10943 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10944 return TRAVERSE_EXIT;
10945 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10946 return TRAVERSE_EXIT;
10947 if (this->end_ != NULL)
10948 {
10949 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10950 return TRAVERSE_EXIT;
10951 }
10952 return TRAVERSE_CONTINUE;
10953}
10954
10955// Return the type of a string index.
10956
10957Type*
10958String_index_expression::do_type()
10959{
10960 if (this->end_ == NULL)
10961 return Type::lookup_integer_type("uint8");
10962 else
7672d35f 10963 return this->string_->type();
e440a328 10964}
10965
10966// Determine the type of a string index.
10967
10968void
10969String_index_expression::do_determine_type(const Type_context*)
10970{
10971 this->string_->determine_type_no_context();
93000773 10972 this->start_->determine_type_no_context();
e440a328 10973 if (this->end_ != NULL)
93000773 10974 this->end_->determine_type_no_context();
e440a328 10975}
10976
10977// Check types of a string index.
10978
10979void
10980String_index_expression::do_check_types(Gogo*)
10981{
10982 if (this->start_->type()->integer_type() == NULL)
10983 this->report_error(_("index must be integer"));
10984 if (this->end_ != NULL
10985 && this->end_->type()->integer_type() == NULL
10986 && !this->end_->is_nil_expression())
10987 this->report_error(_("slice end must be integer"));
10988
10989 std::string sval;
10990 bool sval_valid = this->string_->string_constant_value(&sval);
10991
10992 mpz_t ival;
10993 mpz_init(ival);
10994 Type* dummy;
10995 if (this->start_->integer_constant_value(true, ival, &dummy))
10996 {
10997 if (mpz_sgn(ival) < 0
10998 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10999 {
11000 error_at(this->start_->location(), "string index out of bounds");
11001 this->set_is_error();
11002 }
11003 }
11004 if (this->end_ != NULL && !this->end_->is_nil_expression())
11005 {
11006 if (this->end_->integer_constant_value(true, ival, &dummy))
11007 {
11008 if (mpz_sgn(ival) < 0
11009 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
11010 {
11011 error_at(this->end_->location(), "string index out of bounds");
11012 this->set_is_error();
11013 }
11014 }
11015 }
11016 mpz_clear(ival);
11017}
11018
11019// Get a tree for a string index.
11020
11021tree
11022String_index_expression::do_get_tree(Translate_context* context)
11023{
b13c66cd 11024 Location loc = this->location();
e440a328 11025
11026 tree string_tree = this->string_->get_tree(context);
11027 if (string_tree == error_mark_node)
11028 return error_mark_node;
11029
11030 if (this->string_->type()->points_to() != NULL)
11031 string_tree = build_fold_indirect_ref(string_tree);
11032 if (!DECL_P(string_tree))
11033 string_tree = save_expr(string_tree);
11034 tree string_type = TREE_TYPE(string_tree);
11035
11036 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
11037 length_tree = save_expr(length_tree);
11038 tree length_type = TREE_TYPE(length_tree);
11039
11040 tree bad_index = boolean_false_node;
11041
11042 tree start_tree = this->start_->get_tree(context);
11043 if (start_tree == error_mark_node)
11044 return error_mark_node;
11045 if (!DECL_P(start_tree))
11046 start_tree = save_expr(start_tree);
11047 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
11048 start_tree = convert_to_integer(length_type, start_tree);
11049
11050 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
11051 loc);
11052
b13c66cd 11053 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
e440a328 11054
11055 int code = (this->end_ == NULL
11056 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
11057 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
11058 tree crash = Gogo::runtime_error(code, loc);
11059
11060 if (this->end_ == NULL)
11061 {
b13c66cd 11062 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
11063 boolean_type_node, bad_index,
11064 fold_build2_loc(loc.gcc_location(), GE_EXPR,
e440a328 11065 boolean_type_node,
11066 start_tree, length_tree));
11067
11068 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
b13c66cd 11069 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
11070 TREE_TYPE(bytes_tree),
e440a328 11071 bytes_tree,
b13c66cd 11072 fold_convert_loc(loc.gcc_location(), sizetype,
11073 start_tree));
11074 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
e440a328 11075
11076 return build2(COMPOUND_EXPR, TREE_TYPE(index),
11077 build3(COND_EXPR, void_type_node,
11078 bad_index, crash, NULL_TREE),
11079 index);
11080 }
11081 else
11082 {
11083 tree end_tree;
11084 if (this->end_->is_nil_expression())
11085 end_tree = build_int_cst(length_type, -1);
11086 else
11087 {
11088 end_tree = this->end_->get_tree(context);
11089 if (end_tree == error_mark_node)
11090 return error_mark_node;
11091 if (!DECL_P(end_tree))
11092 end_tree = save_expr(end_tree);
11093 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
11094 end_tree = convert_to_integer(length_type, end_tree);
11095
11096 bad_index = Expression::check_bounds(end_tree, length_type,
11097 bad_index, loc);
11098
b13c66cd 11099 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
11100 end_tree);
e440a328 11101 }
11102
11103 static tree strslice_fndecl;
11104 tree ret = Gogo::call_builtin(&strslice_fndecl,
11105 loc,
11106 "__go_string_slice",
11107 3,
11108 string_type,
11109 string_type,
11110 string_tree,
11111 length_type,
11112 start_tree,
11113 length_type,
11114 end_tree);
5fb82b5e 11115 if (ret == error_mark_node)
11116 return error_mark_node;
e440a328 11117 // This will panic if the bounds are out of range for the
11118 // string.
11119 TREE_NOTHROW(strslice_fndecl) = 0;
11120
11121 if (bad_index == boolean_false_node)
11122 return ret;
11123 else
11124 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
11125 build3(COND_EXPR, void_type_node,
11126 bad_index, crash, NULL_TREE),
11127 ret);
11128 }
11129}
11130
d751bb78 11131// Dump ast representation for a string index expression.
11132
11133void
11134String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11135 const
11136{
11137 Index_expression::dump_index_expression(ast_dump_context, this->string_,
11138 this->start_, this->end_);
11139}
11140
e440a328 11141// Make a string index expression. END may be NULL.
11142
11143Expression*
11144Expression::make_string_index(Expression* string, Expression* start,
b13c66cd 11145 Expression* end, Location location)
e440a328 11146{
11147 return new String_index_expression(string, start, end, location);
11148}
11149
11150// Class Map_index.
11151
11152// Get the type of the map.
11153
11154Map_type*
11155Map_index_expression::get_map_type() const
11156{
11157 Map_type* mt = this->map_->type()->deref()->map_type();
c7524fae 11158 if (mt == NULL)
c484d925 11159 go_assert(saw_errors());
e440a328 11160 return mt;
11161}
11162
11163// Map index traversal.
11164
11165int
11166Map_index_expression::do_traverse(Traverse* traverse)
11167{
11168 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
11169 return TRAVERSE_EXIT;
11170 return Expression::traverse(&this->index_, traverse);
11171}
11172
11173// Return the type of a map index.
11174
11175Type*
11176Map_index_expression::do_type()
11177{
c7524fae 11178 Map_type* mt = this->get_map_type();
11179 if (mt == NULL)
11180 return Type::make_error_type();
11181 Type* type = mt->val_type();
e440a328 11182 // If this map index is in a tuple assignment, we actually return a
11183 // pointer to the value type. Tuple_map_assignment_statement is
11184 // responsible for handling this correctly. We need to get the type
11185 // right in case this gets assigned to a temporary variable.
11186 if (this->is_in_tuple_assignment_)
11187 type = Type::make_pointer_type(type);
11188 return type;
11189}
11190
11191// Fix the type of a map index.
11192
11193void
11194Map_index_expression::do_determine_type(const Type_context*)
11195{
11196 this->map_->determine_type_no_context();
c7524fae 11197 Map_type* mt = this->get_map_type();
11198 Type* key_type = mt == NULL ? NULL : mt->key_type();
11199 Type_context subcontext(key_type, false);
e440a328 11200 this->index_->determine_type(&subcontext);
11201}
11202
11203// Check types of a map index.
11204
11205void
11206Map_index_expression::do_check_types(Gogo*)
11207{
11208 std::string reason;
c7524fae 11209 Map_type* mt = this->get_map_type();
11210 if (mt == NULL)
11211 return;
11212 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
e440a328 11213 {
11214 if (reason.empty())
11215 this->report_error(_("incompatible type for map index"));
11216 else
11217 {
11218 error_at(this->location(), "incompatible type for map index (%s)",
11219 reason.c_str());
11220 this->set_is_error();
11221 }
11222 }
11223}
11224
11225// Get a tree for a map index.
11226
11227tree
11228Map_index_expression::do_get_tree(Translate_context* context)
11229{
11230 Map_type* type = this->get_map_type();
c7524fae 11231 if (type == NULL)
11232 return error_mark_node;
e440a328 11233
11234 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
11235 if (valptr == error_mark_node)
11236 return error_mark_node;
11237 valptr = save_expr(valptr);
11238
11239 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
11240
11241 if (this->is_lvalue_)
11242 return build_fold_indirect_ref(valptr);
11243 else if (this->is_in_tuple_assignment_)
11244 {
11245 // Tuple_map_assignment_statement is responsible for using this
11246 // appropriately.
11247 return valptr;
11248 }
11249 else
11250 {
63697958 11251 Gogo* gogo = context->gogo();
11252 Btype* val_btype = type->val_type()->get_backend(gogo);
11253 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
e440a328 11254 return fold_build3(COND_EXPR, val_type_tree,
11255 fold_build2(EQ_EXPR, boolean_type_node, valptr,
11256 fold_convert(TREE_TYPE(valptr),
11257 null_pointer_node)),
63697958 11258 expr_to_tree(val_zero),
e440a328 11259 build_fold_indirect_ref(valptr));
11260 }
11261}
11262
11263// Get a tree for the map index. This returns a tree which evaluates
11264// to a pointer to a value. The pointer will be NULL if the key is
11265// not in the map.
11266
11267tree
11268Map_index_expression::get_value_pointer(Translate_context* context,
11269 bool insert)
11270{
11271 Map_type* type = this->get_map_type();
c7524fae 11272 if (type == NULL)
11273 return error_mark_node;
e440a328 11274
11275 tree map_tree = this->map_->get_tree(context);
11276 tree index_tree = this->index_->get_tree(context);
11277 index_tree = Expression::convert_for_assignment(context, type->key_type(),
11278 this->index_->type(),
11279 index_tree,
11280 this->location());
11281 if (map_tree == error_mark_node || index_tree == error_mark_node)
11282 return error_mark_node;
11283
11284 if (this->map_->type()->points_to() != NULL)
11285 map_tree = build_fold_indirect_ref(map_tree);
11286
11287 // We need to pass in a pointer to the key, so stuff it into a
11288 // variable.
746d2e73 11289 tree tmp;
11290 tree make_tmp;
11291 if (current_function_decl != NULL)
11292 {
11293 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
11294 DECL_IGNORED_P(tmp) = 0;
11295 DECL_INITIAL(tmp) = index_tree;
11296 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
11297 TREE_ADDRESSABLE(tmp) = 1;
11298 }
11299 else
11300 {
b13c66cd 11301 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
11302 create_tmp_var_name("M"),
746d2e73 11303 TREE_TYPE(index_tree));
11304 DECL_EXTERNAL(tmp) = 0;
11305 TREE_PUBLIC(tmp) = 0;
11306 TREE_STATIC(tmp) = 1;
11307 DECL_ARTIFICIAL(tmp) = 1;
11308 if (!TREE_CONSTANT(index_tree))
b13c66cd 11309 make_tmp = fold_build2_loc(this->location().gcc_location(),
11310 INIT_EXPR, void_type_node,
746d2e73 11311 tmp, index_tree);
11312 else
11313 {
11314 TREE_READONLY(tmp) = 1;
11315 TREE_CONSTANT(tmp) = 1;
11316 DECL_INITIAL(tmp) = index_tree;
11317 make_tmp = NULL_TREE;
11318 }
11319 rest_of_decl_compilation(tmp, 1, 0);
11320 }
b13c66cd 11321 tree tmpref =
11322 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
11323 build_fold_addr_expr_loc(this->location().gcc_location(),
11324 tmp));
e440a328 11325
11326 static tree map_index_fndecl;
11327 tree call = Gogo::call_builtin(&map_index_fndecl,
11328 this->location(),
11329 "__go_map_index",
11330 3,
11331 const_ptr_type_node,
11332 TREE_TYPE(map_tree),
11333 map_tree,
11334 const_ptr_type_node,
11335 tmpref,
11336 boolean_type_node,
11337 (insert
11338 ? boolean_true_node
11339 : boolean_false_node));
5fb82b5e 11340 if (call == error_mark_node)
11341 return error_mark_node;
e440a328 11342 // This can panic on a map of interface type if the interface holds
11343 // an uncomparable or unhashable type.
11344 TREE_NOTHROW(map_index_fndecl) = 0;
11345
9f0e0513 11346 Type* val_type = type->val_type();
11347 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
e440a328 11348 if (val_type_tree == error_mark_node)
11349 return error_mark_node;
11350 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
11351
b13c66cd 11352 tree ret = fold_convert_loc(this->location().gcc_location(),
11353 ptr_val_type_tree, call);
746d2e73 11354 if (make_tmp != NULL_TREE)
11355 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
11356 return ret;
e440a328 11357}
11358
d751bb78 11359// Dump ast representation for a map index expression
11360
11361void
11362Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11363 const
11364{
11365 Index_expression::dump_index_expression(ast_dump_context,
11366 this->map_, this->index_, NULL);
11367}
11368
e440a328 11369// Make a map index expression.
11370
11371Map_index_expression*
11372Expression::make_map_index(Expression* map, Expression* index,
b13c66cd 11373 Location location)
e440a328 11374{
11375 return new Map_index_expression(map, index, location);
11376}
11377
11378// Class Field_reference_expression.
11379
11380// Return the type of a field reference.
11381
11382Type*
11383Field_reference_expression::do_type()
11384{
b0e628fb 11385 Type* type = this->expr_->type();
5c13bd80 11386 if (type->is_error())
b0e628fb 11387 return type;
11388 Struct_type* struct_type = type->struct_type();
c484d925 11389 go_assert(struct_type != NULL);
e440a328 11390 return struct_type->field(this->field_index_)->type();
11391}
11392
11393// Check the types for a field reference.
11394
11395void
11396Field_reference_expression::do_check_types(Gogo*)
11397{
b0e628fb 11398 Type* type = this->expr_->type();
5c13bd80 11399 if (type->is_error())
b0e628fb 11400 return;
11401 Struct_type* struct_type = type->struct_type();
c484d925 11402 go_assert(struct_type != NULL);
11403 go_assert(struct_type->field(this->field_index_) != NULL);
e440a328 11404}
11405
11406// Get a tree for a field reference.
11407
11408tree
11409Field_reference_expression::do_get_tree(Translate_context* context)
11410{
11411 tree struct_tree = this->expr_->get_tree(context);
11412 if (struct_tree == error_mark_node
11413 || TREE_TYPE(struct_tree) == error_mark_node)
11414 return error_mark_node;
c484d925 11415 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
e440a328 11416 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
b1d655d5 11417 if (field == NULL_TREE)
11418 {
11419 // This can happen for a type which refers to itself indirectly
11420 // and then turns out to be erroneous.
c484d925 11421 go_assert(saw_errors());
b1d655d5 11422 return error_mark_node;
11423 }
e440a328 11424 for (unsigned int i = this->field_index_; i > 0; --i)
11425 {
11426 field = DECL_CHAIN(field);
c484d925 11427 go_assert(field != NULL_TREE);
e440a328 11428 }
c35179ff 11429 if (TREE_TYPE(field) == error_mark_node)
11430 return error_mark_node;
e440a328 11431 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
11432 NULL_TREE);
11433}
11434
d751bb78 11435// Dump ast representation for a field reference expression.
11436
11437void
11438Field_reference_expression::do_dump_expression(
11439 Ast_dump_context* ast_dump_context) const
11440{
11441 this->expr_->dump_expression(ast_dump_context);
11442 ast_dump_context->ostream() << "." << this->field_index_;
11443}
11444
e440a328 11445// Make a reference to a qualified identifier in an expression.
11446
11447Field_reference_expression*
11448Expression::make_field_reference(Expression* expr, unsigned int field_index,
b13c66cd 11449 Location location)
e440a328 11450{
11451 return new Field_reference_expression(expr, field_index, location);
11452}
11453
11454// Class Interface_field_reference_expression.
11455
11456// Return a tree for the pointer to the function to call.
11457
11458tree
11459Interface_field_reference_expression::get_function_tree(Translate_context*,
11460 tree expr)
11461{
11462 if (this->expr_->type()->points_to() != NULL)
11463 expr = build_fold_indirect_ref(expr);
11464
11465 tree expr_type = TREE_TYPE(expr);
c484d925 11466 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
e440a328 11467
11468 tree field = TYPE_FIELDS(expr_type);
c484d925 11469 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
e440a328 11470
11471 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
c484d925 11472 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
e440a328 11473
11474 table = build_fold_indirect_ref(table);
c484d925 11475 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
e440a328 11476
11477 std::string name = Gogo::unpack_hidden_name(this->name_);
11478 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
11479 field != NULL_TREE;
11480 field = DECL_CHAIN(field))
11481 {
11482 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
11483 break;
11484 }
c484d925 11485 go_assert(field != NULL_TREE);
e440a328 11486
11487 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
11488}
11489
11490// Return a tree for the first argument to pass to the interface
11491// function.
11492
11493tree
11494Interface_field_reference_expression::get_underlying_object_tree(
11495 Translate_context*,
11496 tree expr)
11497{
11498 if (this->expr_->type()->points_to() != NULL)
11499 expr = build_fold_indirect_ref(expr);
11500
11501 tree expr_type = TREE_TYPE(expr);
c484d925 11502 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
e440a328 11503
11504 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
c484d925 11505 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
e440a328 11506
11507 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11508}
11509
11510// Traversal.
11511
11512int
11513Interface_field_reference_expression::do_traverse(Traverse* traverse)
11514{
11515 return Expression::traverse(&this->expr_, traverse);
11516}
11517
11518// Return the type of an interface field reference.
11519
11520Type*
11521Interface_field_reference_expression::do_type()
11522{
11523 Type* expr_type = this->expr_->type();
11524
11525 Type* points_to = expr_type->points_to();
11526 if (points_to != NULL)
11527 expr_type = points_to;
11528
11529 Interface_type* interface_type = expr_type->interface_type();
11530 if (interface_type == NULL)
11531 return Type::make_error_type();
11532
11533 const Typed_identifier* method = interface_type->find_method(this->name_);
11534 if (method == NULL)
11535 return Type::make_error_type();
11536
11537 return method->type();
11538}
11539
11540// Determine types.
11541
11542void
11543Interface_field_reference_expression::do_determine_type(const Type_context*)
11544{
11545 this->expr_->determine_type_no_context();
11546}
11547
11548// Check the types for an interface field reference.
11549
11550void
11551Interface_field_reference_expression::do_check_types(Gogo*)
11552{
11553 Type* type = this->expr_->type();
11554
11555 Type* points_to = type->points_to();
11556 if (points_to != NULL)
11557 type = points_to;
11558
11559 Interface_type* interface_type = type->interface_type();
11560 if (interface_type == NULL)
5c491127 11561 {
11562 if (!type->is_error_type())
11563 this->report_error(_("expected interface or pointer to interface"));
11564 }
e440a328 11565 else
11566 {
11567 const Typed_identifier* method =
11568 interface_type->find_method(this->name_);
11569 if (method == NULL)
11570 {
11571 error_at(this->location(), "method %qs not in interface",
11572 Gogo::message_name(this->name_).c_str());
11573 this->set_is_error();
11574 }
11575 }
11576}
11577
11578// Get a tree for a reference to a field in an interface. There is no
11579// standard tree type representation for this: it's a function
11580// attached to its first argument, like a Bound_method_expression.
11581// The only places it may currently be used are in a Call_expression
11582// or a Go_statement, which will take it apart directly. So this has
11583// nothing to do at present.
11584
11585tree
11586Interface_field_reference_expression::do_get_tree(Translate_context*)
11587{
c3e6f413 11588 go_unreachable();
e440a328 11589}
11590
d751bb78 11591// Dump ast representation for an interface field reference.
11592
11593void
11594Interface_field_reference_expression::do_dump_expression(
11595 Ast_dump_context* ast_dump_context) const
11596{
11597 this->expr_->dump_expression(ast_dump_context);
11598 ast_dump_context->ostream() << "." << this->name_;
11599}
11600
e440a328 11601// Make a reference to a field in an interface.
11602
11603Expression*
11604Expression::make_interface_field_reference(Expression* expr,
11605 const std::string& field,
b13c66cd 11606 Location location)
e440a328 11607{
11608 return new Interface_field_reference_expression(expr, field, location);
11609}
11610
11611// A general selector. This is a Parser_expression for LEFT.NAME. It
11612// is lowered after we know the type of the left hand side.
11613
11614class Selector_expression : public Parser_expression
11615{
11616 public:
11617 Selector_expression(Expression* left, const std::string& name,
b13c66cd 11618 Location location)
e440a328 11619 : Parser_expression(EXPRESSION_SELECTOR, location),
11620 left_(left), name_(name)
11621 { }
11622
11623 protected:
11624 int
11625 do_traverse(Traverse* traverse)
11626 { return Expression::traverse(&this->left_, traverse); }
11627
11628 Expression*
ceeb4318 11629 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 11630
11631 Expression*
11632 do_copy()
11633 {
11634 return new Selector_expression(this->left_->copy(), this->name_,
11635 this->location());
11636 }
11637
d751bb78 11638 void
11639 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11640
e440a328 11641 private:
11642 Expression*
11643 lower_method_expression(Gogo*);
11644
11645 // The expression on the left hand side.
11646 Expression* left_;
11647 // The name on the right hand side.
11648 std::string name_;
11649};
11650
11651// Lower a selector expression once we know the real type of the left
11652// hand side.
11653
11654Expression*
ceeb4318 11655Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11656 int)
e440a328 11657{
11658 Expression* left = this->left_;
11659 if (left->is_type_expression())
11660 return this->lower_method_expression(gogo);
11661 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11662 this->location());
11663}
11664
11665// Lower a method expression T.M or (*T).M. We turn this into a
11666// function literal.
11667
11668Expression*
11669Selector_expression::lower_method_expression(Gogo* gogo)
11670{
b13c66cd 11671 Location location = this->location();
e440a328 11672 Type* type = this->left_->type();
11673 const std::string& name(this->name_);
11674
11675 bool is_pointer;
11676 if (type->points_to() == NULL)
11677 is_pointer = false;
11678 else
11679 {
11680 is_pointer = true;
11681 type = type->points_to();
11682 }
11683 Named_type* nt = type->named_type();
11684 if (nt == NULL)
11685 {
11686 error_at(location,
11687 ("method expression requires named type or "
11688 "pointer to named type"));
11689 return Expression::make_error(location);
11690 }
11691
11692 bool is_ambiguous;
11693 Method* method = nt->method_function(name, &is_ambiguous);
ab1468c3 11694 const Typed_identifier* imethod = NULL;
dcc8506b 11695 if (method == NULL && !is_pointer)
ab1468c3 11696 {
11697 Interface_type* it = nt->interface_type();
11698 if (it != NULL)
11699 imethod = it->find_method(name);
11700 }
11701
11702 if (method == NULL && imethod == NULL)
e440a328 11703 {
11704 if (!is_ambiguous)
dcc8506b 11705 error_at(location, "type %<%s%s%> has no method %<%s%>",
11706 is_pointer ? "*" : "",
e440a328 11707 nt->message_name().c_str(),
11708 Gogo::message_name(name).c_str());
11709 else
dcc8506b 11710 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
e440a328 11711 Gogo::message_name(name).c_str(),
dcc8506b 11712 is_pointer ? "*" : "",
e440a328 11713 nt->message_name().c_str());
11714 return Expression::make_error(location);
11715 }
11716
ab1468c3 11717 if (method != NULL && !is_pointer && !method->is_value_method())
e440a328 11718 {
11719 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
11720 nt->message_name().c_str(),
11721 Gogo::message_name(name).c_str());
11722 return Expression::make_error(location);
11723 }
11724
11725 // Build a new function type in which the receiver becomes the first
11726 // argument.
ab1468c3 11727 Function_type* method_type;
11728 if (method != NULL)
11729 {
11730 method_type = method->type();
c484d925 11731 go_assert(method_type->is_method());
ab1468c3 11732 }
11733 else
11734 {
11735 method_type = imethod->type()->function_type();
c484d925 11736 go_assert(method_type != NULL && !method_type->is_method());
ab1468c3 11737 }
e440a328 11738
11739 const char* const receiver_name = "$this";
11740 Typed_identifier_list* parameters = new Typed_identifier_list();
11741 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
11742 location));
11743
11744 const Typed_identifier_list* method_parameters = method_type->parameters();
11745 if (method_parameters != NULL)
11746 {
f470da59 11747 int i = 0;
e440a328 11748 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11749 p != method_parameters->end();
f470da59 11750 ++p, ++i)
11751 {
11752 if (!p->name().empty() && p->name() != Import::import_marker)
11753 parameters->push_back(*p);
11754 else
11755 {
11756 char buf[20];
11757 snprintf(buf, sizeof buf, "$param%d", i);
11758 parameters->push_back(Typed_identifier(buf, p->type(),
11759 p->location()));
11760 }
11761 }
e440a328 11762 }
11763
11764 const Typed_identifier_list* method_results = method_type->results();
11765 Typed_identifier_list* results;
11766 if (method_results == NULL)
11767 results = NULL;
11768 else
11769 {
11770 results = new Typed_identifier_list();
11771 for (Typed_identifier_list::const_iterator p = method_results->begin();
11772 p != method_results->end();
11773 ++p)
11774 results->push_back(*p);
11775 }
11776
11777 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
11778 location);
11779 if (method_type->is_varargs())
11780 fntype->set_is_varargs();
11781
11782 // We generate methods which always takes a pointer to the receiver
11783 // as their first argument. If this is for a pointer type, we can
11784 // simply reuse the existing function. We use an internal hack to
11785 // get the right type.
11786
ab1468c3 11787 if (method != NULL && is_pointer)
e440a328 11788 {
11789 Named_object* mno = (method->needs_stub_method()
11790 ? method->stub_object()
11791 : method->named_object());
11792 Expression* f = Expression::make_func_reference(mno, NULL, location);
11793 f = Expression::make_cast(fntype, f, location);
11794 Type_conversion_expression* tce =
11795 static_cast<Type_conversion_expression*>(f);
11796 tce->set_may_convert_function_types();
11797 return f;
11798 }
11799
11800 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
11801 location);
11802
11803 Named_object* vno = gogo->lookup(receiver_name, NULL);
c484d925 11804 go_assert(vno != NULL);
e440a328 11805 Expression* ve = Expression::make_var_reference(vno, location);
ab1468c3 11806 Expression* bm;
11807 if (method != NULL)
11808 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
11809 else
11810 bm = Expression::make_interface_field_reference(ve, name, location);
f690b0bb 11811
11812 // Even though we found the method above, if it has an error type we
11813 // may see an error here.
11814 if (bm->is_error_expression())
463fe805 11815 {
11816 gogo->finish_function(location);
11817 return bm;
11818 }
e440a328 11819
11820 Expression_list* args;
f470da59 11821 if (parameters->size() <= 1)
e440a328 11822 args = NULL;
11823 else
11824 {
11825 args = new Expression_list();
f470da59 11826 Typed_identifier_list::const_iterator p = parameters->begin();
11827 ++p;
11828 for (; p != parameters->end(); ++p)
e440a328 11829 {
11830 vno = gogo->lookup(p->name(), NULL);
c484d925 11831 go_assert(vno != NULL);
e440a328 11832 args->push_back(Expression::make_var_reference(vno, location));
11833 }
11834 }
11835
ceeb4318 11836 gogo->start_block(location);
11837
e440a328 11838 Call_expression* call = Expression::make_call(bm, args,
11839 method_type->is_varargs(),
11840 location);
11841
11842 size_t count = call->result_count();
11843 Statement* s;
11844 if (count == 0)
a7549a6a 11845 s = Statement::make_statement(call, true);
e440a328 11846 else
11847 {
11848 Expression_list* retvals = new Expression_list();
11849 if (count <= 1)
11850 retvals->push_back(call);
11851 else
11852 {
11853 for (size_t i = 0; i < count; ++i)
11854 retvals->push_back(Expression::make_call_result(call, i));
11855 }
be2fc38d 11856 s = Statement::make_return_statement(retvals, location);
e440a328 11857 }
11858 gogo->add_statement(s);
11859
ceeb4318 11860 Block* b = gogo->finish_block(location);
11861
11862 gogo->add_block(b, location);
11863
11864 // Lower the call in case there are multiple results.
11865 gogo->lower_block(no, b);
11866
e440a328 11867 gogo->finish_function(location);
11868
11869 return Expression::make_func_reference(no, NULL, location);
11870}
11871
d751bb78 11872// Dump the ast for a selector expression.
11873
11874void
11875Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11876 const
11877{
11878 ast_dump_context->dump_expression(this->left_);
11879 ast_dump_context->ostream() << ".";
11880 ast_dump_context->ostream() << this->name_;
11881}
11882
e440a328 11883// Make a selector expression.
11884
11885Expression*
11886Expression::make_selector(Expression* left, const std::string& name,
b13c66cd 11887 Location location)
e440a328 11888{
11889 return new Selector_expression(left, name, location);
11890}
11891
11892// Implement the builtin function new.
11893
11894class Allocation_expression : public Expression
11895{
11896 public:
b13c66cd 11897 Allocation_expression(Type* type, Location location)
e440a328 11898 : Expression(EXPRESSION_ALLOCATION, location),
11899 type_(type)
11900 { }
11901
11902 protected:
11903 int
11904 do_traverse(Traverse* traverse)
11905 { return Type::traverse(this->type_, traverse); }
11906
11907 Type*
11908 do_type()
11909 { return Type::make_pointer_type(this->type_); }
11910
11911 void
11912 do_determine_type(const Type_context*)
11913 { }
11914
e440a328 11915 Expression*
11916 do_copy()
11917 { return new Allocation_expression(this->type_, this->location()); }
11918
11919 tree
11920 do_get_tree(Translate_context*);
11921
d751bb78 11922 void
11923 do_dump_expression(Ast_dump_context*) const;
11924
e440a328 11925 private:
11926 // The type we are allocating.
11927 Type* type_;
11928};
11929
e440a328 11930// Return a tree for an allocation expression.
11931
11932tree
11933Allocation_expression::do_get_tree(Translate_context* context)
11934{
9f0e0513 11935 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
19824ddb 11936 if (type_tree == error_mark_node)
11937 return error_mark_node;
e440a328 11938 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11939 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11940 this->location());
19824ddb 11941 if (space == error_mark_node)
11942 return error_mark_node;
e440a328 11943 return fold_convert(build_pointer_type(type_tree), space);
11944}
11945
d751bb78 11946// Dump ast representation for an allocation expression.
11947
11948void
11949Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11950 const
11951{
11952 ast_dump_context->ostream() << "new(";
11953 ast_dump_context->dump_type(this->type_);
11954 ast_dump_context->ostream() << ")";
11955}
11956
e440a328 11957// Make an allocation expression.
11958
11959Expression*
b13c66cd 11960Expression::make_allocation(Type* type, Location location)
e440a328 11961{
11962 return new Allocation_expression(type, location);
11963}
11964
e440a328 11965// Construct a struct.
11966
11967class Struct_construction_expression : public Expression
11968{
11969 public:
11970 Struct_construction_expression(Type* type, Expression_list* vals,
b13c66cd 11971 Location location)
e440a328 11972 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
11973 type_(type), vals_(vals)
11974 { }
11975
11976 // Return whether this is a constant initializer.
11977 bool
11978 is_constant_struct() const;
11979
11980 protected:
11981 int
11982 do_traverse(Traverse* traverse);
11983
11984 Type*
11985 do_type()
11986 { return this->type_; }
11987
11988 void
11989 do_determine_type(const Type_context*);
11990
11991 void
11992 do_check_types(Gogo*);
11993
11994 Expression*
11995 do_copy()
11996 {
11997 return new Struct_construction_expression(this->type_, this->vals_->copy(),
11998 this->location());
11999 }
12000
e440a328 12001 tree
12002 do_get_tree(Translate_context*);
12003
12004 void
12005 do_export(Export*) const;
12006
d751bb78 12007 void
12008 do_dump_expression(Ast_dump_context*) const;
12009
e440a328 12010 private:
12011 // The type of the struct to construct.
12012 Type* type_;
12013 // The list of values, in order of the fields in the struct. A NULL
12014 // entry means that the field should be zero-initialized.
12015 Expression_list* vals_;
12016};
12017
12018// Traversal.
12019
12020int
12021Struct_construction_expression::do_traverse(Traverse* traverse)
12022{
12023 if (this->vals_ != NULL
12024 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12025 return TRAVERSE_EXIT;
12026 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12027 return TRAVERSE_EXIT;
12028 return TRAVERSE_CONTINUE;
12029}
12030
12031// Return whether this is a constant initializer.
12032
12033bool
12034Struct_construction_expression::is_constant_struct() const
12035{
12036 if (this->vals_ == NULL)
12037 return true;
12038 for (Expression_list::const_iterator pv = this->vals_->begin();
12039 pv != this->vals_->end();
12040 ++pv)
12041 {
12042 if (*pv != NULL
12043 && !(*pv)->is_constant()
12044 && (!(*pv)->is_composite_literal()
12045 || (*pv)->is_nonconstant_composite_literal()))
12046 return false;
12047 }
12048
12049 const Struct_field_list* fields = this->type_->struct_type()->fields();
12050 for (Struct_field_list::const_iterator pf = fields->begin();
12051 pf != fields->end();
12052 ++pf)
12053 {
12054 // There are no constant constructors for interfaces.
12055 if (pf->type()->interface_type() != NULL)
12056 return false;
12057 }
12058
12059 return true;
12060}
12061
12062// Final type determination.
12063
12064void
12065Struct_construction_expression::do_determine_type(const Type_context*)
12066{
12067 if (this->vals_ == NULL)
12068 return;
12069 const Struct_field_list* fields = this->type_->struct_type()->fields();
12070 Expression_list::const_iterator pv = this->vals_->begin();
12071 for (Struct_field_list::const_iterator pf = fields->begin();
12072 pf != fields->end();
12073 ++pf, ++pv)
12074 {
12075 if (pv == this->vals_->end())
12076 return;
12077 if (*pv != NULL)
12078 {
12079 Type_context subcontext(pf->type(), false);
12080 (*pv)->determine_type(&subcontext);
12081 }
12082 }
a6cb4c0e 12083 // Extra values are an error we will report elsewhere; we still want
12084 // to determine the type to avoid knockon errors.
12085 for (; pv != this->vals_->end(); ++pv)
12086 (*pv)->determine_type_no_context();
e440a328 12087}
12088
12089// Check types.
12090
12091void
12092Struct_construction_expression::do_check_types(Gogo*)
12093{
12094 if (this->vals_ == NULL)
12095 return;
12096
12097 Struct_type* st = this->type_->struct_type();
12098 if (this->vals_->size() > st->field_count())
12099 {
12100 this->report_error(_("too many expressions for struct"));
12101 return;
12102 }
12103
12104 const Struct_field_list* fields = st->fields();
12105 Expression_list::const_iterator pv = this->vals_->begin();
12106 int i = 0;
12107 for (Struct_field_list::const_iterator pf = fields->begin();
12108 pf != fields->end();
12109 ++pf, ++pv, ++i)
12110 {
12111 if (pv == this->vals_->end())
12112 {
12113 this->report_error(_("too few expressions for struct"));
12114 break;
12115 }
12116
12117 if (*pv == NULL)
12118 continue;
12119
12120 std::string reason;
12121 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
12122 {
12123 if (reason.empty())
12124 error_at((*pv)->location(),
12125 "incompatible type for field %d in struct construction",
12126 i + 1);
12127 else
12128 error_at((*pv)->location(),
12129 ("incompatible type for field %d in "
12130 "struct construction (%s)"),
12131 i + 1, reason.c_str());
12132 this->set_is_error();
12133 }
12134 }
c484d925 12135 go_assert(pv == this->vals_->end());
e440a328 12136}
12137
12138// Return a tree for constructing a struct.
12139
12140tree
12141Struct_construction_expression::do_get_tree(Translate_context* context)
12142{
12143 Gogo* gogo = context->gogo();
12144
12145 if (this->vals_ == NULL)
63697958 12146 {
12147 Btype* btype = this->type_->get_backend(gogo);
12148 return expr_to_tree(gogo->backend()->zero_expression(btype));
12149 }
e440a328 12150
9f0e0513 12151 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
e440a328 12152 if (type_tree == error_mark_node)
12153 return error_mark_node;
c484d925 12154 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
e440a328 12155
12156 bool is_constant = true;
12157 const Struct_field_list* fields = this->type_->struct_type()->fields();
12158 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
12159 fields->size());
12160 Struct_field_list::const_iterator pf = fields->begin();
12161 Expression_list::const_iterator pv = this->vals_->begin();
12162 for (tree field = TYPE_FIELDS(type_tree);
12163 field != NULL_TREE;
12164 field = DECL_CHAIN(field), ++pf)
12165 {
c484d925 12166 go_assert(pf != fields->end());
e440a328 12167
63697958 12168 Btype* fbtype = pf->type()->get_backend(gogo);
12169
e440a328 12170 tree val;
12171 if (pv == this->vals_->end())
63697958 12172 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
e440a328 12173 else if (*pv == NULL)
12174 {
63697958 12175 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
e440a328 12176 ++pv;
12177 }
12178 else
12179 {
12180 val = Expression::convert_for_assignment(context, pf->type(),
12181 (*pv)->type(),
12182 (*pv)->get_tree(context),
12183 this->location());
12184 ++pv;
12185 }
12186
12187 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
12188 return error_mark_node;
12189
12190 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
12191 elt->index = field;
12192 elt->value = val;
12193 if (!TREE_CONSTANT(val))
12194 is_constant = false;
12195 }
c484d925 12196 go_assert(pf == fields->end());
e440a328 12197
12198 tree ret = build_constructor(type_tree, elts);
12199 if (is_constant)
12200 TREE_CONSTANT(ret) = 1;
12201 return ret;
12202}
12203
12204// Export a struct construction.
12205
12206void
12207Struct_construction_expression::do_export(Export* exp) const
12208{
12209 exp->write_c_string("convert(");
12210 exp->write_type(this->type_);
12211 for (Expression_list::const_iterator pv = this->vals_->begin();
12212 pv != this->vals_->end();
12213 ++pv)
12214 {
12215 exp->write_c_string(", ");
12216 if (*pv != NULL)
12217 (*pv)->export_expression(exp);
12218 }
12219 exp->write_c_string(")");
12220}
12221
d751bb78 12222// Dump ast representation of a struct construction expression.
12223
12224void
12225Struct_construction_expression::do_dump_expression(
12226 Ast_dump_context* ast_dump_context) const
12227{
d751bb78 12228 ast_dump_context->dump_type(this->type_);
12229 ast_dump_context->ostream() << "{";
12230 ast_dump_context->dump_expression_list(this->vals_);
12231 ast_dump_context->ostream() << "}";
12232}
12233
e440a328 12234// Make a struct composite literal. This used by the thunk code.
12235
12236Expression*
12237Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
b13c66cd 12238 Location location)
e440a328 12239{
c484d925 12240 go_assert(type->struct_type() != NULL);
e440a328 12241 return new Struct_construction_expression(type, vals, location);
12242}
12243
12244// Construct an array. This class is not used directly; instead we
12245// use the child classes, Fixed_array_construction_expression and
12246// Open_array_construction_expression.
12247
12248class Array_construction_expression : public Expression
12249{
12250 protected:
12251 Array_construction_expression(Expression_classification classification,
12252 Type* type, Expression_list* vals,
b13c66cd 12253 Location location)
e440a328 12254 : Expression(classification, location),
12255 type_(type), vals_(vals)
12256 { }
12257
12258 public:
12259 // Return whether this is a constant initializer.
12260 bool
12261 is_constant_array() const;
12262
12263 // Return the number of elements.
12264 size_t
12265 element_count() const
12266 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
12267
12268protected:
12269 int
12270 do_traverse(Traverse* traverse);
12271
12272 Type*
12273 do_type()
12274 { return this->type_; }
12275
12276 void
12277 do_determine_type(const Type_context*);
12278
12279 void
12280 do_check_types(Gogo*);
12281
e440a328 12282 void
12283 do_export(Export*) const;
12284
12285 // The list of values.
12286 Expression_list*
12287 vals()
12288 { return this->vals_; }
12289
12290 // Get a constructor tree for the array values.
12291 tree
12292 get_constructor_tree(Translate_context* context, tree type_tree);
12293
d751bb78 12294 void
12295 do_dump_expression(Ast_dump_context*) const;
12296
e440a328 12297 private:
12298 // The type of the array to construct.
12299 Type* type_;
12300 // The list of values.
12301 Expression_list* vals_;
12302};
12303
12304// Traversal.
12305
12306int
12307Array_construction_expression::do_traverse(Traverse* traverse)
12308{
12309 if (this->vals_ != NULL
12310 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12311 return TRAVERSE_EXIT;
12312 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12313 return TRAVERSE_EXIT;
12314 return TRAVERSE_CONTINUE;
12315}
12316
12317// Return whether this is a constant initializer.
12318
12319bool
12320Array_construction_expression::is_constant_array() const
12321{
12322 if (this->vals_ == NULL)
12323 return true;
12324
12325 // There are no constant constructors for interfaces.
12326 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12327 return false;
12328
12329 for (Expression_list::const_iterator pv = this->vals_->begin();
12330 pv != this->vals_->end();
12331 ++pv)
12332 {
12333 if (*pv != NULL
12334 && !(*pv)->is_constant()
12335 && (!(*pv)->is_composite_literal()
12336 || (*pv)->is_nonconstant_composite_literal()))
12337 return false;
12338 }
12339 return true;
12340}
12341
12342// Final type determination.
12343
12344void
12345Array_construction_expression::do_determine_type(const Type_context*)
12346{
12347 if (this->vals_ == NULL)
12348 return;
12349 Type_context subcontext(this->type_->array_type()->element_type(), false);
12350 for (Expression_list::const_iterator pv = this->vals_->begin();
12351 pv != this->vals_->end();
12352 ++pv)
12353 {
12354 if (*pv != NULL)
12355 (*pv)->determine_type(&subcontext);
12356 }
12357}
12358
12359// Check types.
12360
12361void
12362Array_construction_expression::do_check_types(Gogo*)
12363{
12364 if (this->vals_ == NULL)
12365 return;
12366
12367 Array_type* at = this->type_->array_type();
12368 int i = 0;
12369 Type* element_type = at->element_type();
12370 for (Expression_list::const_iterator pv = this->vals_->begin();
12371 pv != this->vals_->end();
12372 ++pv, ++i)
12373 {
12374 if (*pv != NULL
12375 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12376 {
12377 error_at((*pv)->location(),
12378 "incompatible type for element %d in composite literal",
12379 i + 1);
12380 this->set_is_error();
12381 }
12382 }
12383
12384 Expression* length = at->length();
09add252 12385 if (length != NULL && !length->is_error_expression())
e440a328 12386 {
12387 mpz_t val;
12388 mpz_init(val);
12389 Type* type;
12390 if (at->length()->integer_constant_value(true, val, &type))
12391 {
12392 if (this->vals_->size() > mpz_get_ui(val))
12393 this->report_error(_("too many elements in composite literal"));
12394 }
12395 mpz_clear(val);
12396 }
12397}
12398
12399// Get a constructor tree for the array values.
12400
12401tree
12402Array_construction_expression::get_constructor_tree(Translate_context* context,
12403 tree type_tree)
12404{
12405 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12406 (this->vals_ == NULL
12407 ? 0
12408 : this->vals_->size()));
12409 Type* element_type = this->type_->array_type()->element_type();
12410 bool is_constant = true;
12411 if (this->vals_ != NULL)
12412 {
12413 size_t i = 0;
12414 for (Expression_list::const_iterator pv = this->vals_->begin();
12415 pv != this->vals_->end();
12416 ++pv, ++i)
12417 {
12418 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
12419 elt->index = size_int(i);
12420 if (*pv == NULL)
63697958 12421 {
12422 Gogo* gogo = context->gogo();
12423 Btype* ebtype = element_type->get_backend(gogo);
12424 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
12425 elt->value = expr_to_tree(zv);
12426 }
e440a328 12427 else
12428 {
12429 tree value_tree = (*pv)->get_tree(context);
12430 elt->value = Expression::convert_for_assignment(context,
12431 element_type,
12432 (*pv)->type(),
12433 value_tree,
12434 this->location());
12435 }
12436 if (elt->value == error_mark_node)
12437 return error_mark_node;
12438 if (!TREE_CONSTANT(elt->value))
12439 is_constant = false;
12440 }
12441 }
12442
12443 tree ret = build_constructor(type_tree, values);
12444 if (is_constant)
12445 TREE_CONSTANT(ret) = 1;
12446 return ret;
12447}
12448
12449// Export an array construction.
12450
12451void
12452Array_construction_expression::do_export(Export* exp) const
12453{
12454 exp->write_c_string("convert(");
12455 exp->write_type(this->type_);
12456 if (this->vals_ != NULL)
12457 {
12458 for (Expression_list::const_iterator pv = this->vals_->begin();
12459 pv != this->vals_->end();
12460 ++pv)
12461 {
12462 exp->write_c_string(", ");
12463 if (*pv != NULL)
12464 (*pv)->export_expression(exp);
12465 }
12466 }
12467 exp->write_c_string(")");
12468}
12469
d751bb78 12470// Dump ast representation of an array construction expressin.
12471
12472void
12473Array_construction_expression::do_dump_expression(
12474 Ast_dump_context* ast_dump_context) const
12475{
8b1c301d 12476 Expression* length = this->type_->array_type() != NULL ?
12477 this->type_->array_type()->length() : NULL;
12478
12479 ast_dump_context->ostream() << "[" ;
12480 if (length != NULL)
12481 {
12482 ast_dump_context->dump_expression(length);
12483 }
12484 ast_dump_context->ostream() << "]" ;
d751bb78 12485 ast_dump_context->dump_type(this->type_);
12486 ast_dump_context->ostream() << "{" ;
12487 ast_dump_context->dump_expression_list(this->vals_);
12488 ast_dump_context->ostream() << "}" ;
12489
12490}
12491
e440a328 12492// Construct a fixed array.
12493
12494class Fixed_array_construction_expression :
12495 public Array_construction_expression
12496{
12497 public:
12498 Fixed_array_construction_expression(Type* type, Expression_list* vals,
b13c66cd 12499 Location location)
e440a328 12500 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
12501 type, vals, location)
12502 {
c484d925 12503 go_assert(type->array_type() != NULL
e440a328 12504 && type->array_type()->length() != NULL);
12505 }
12506
12507 protected:
12508 Expression*
12509 do_copy()
12510 {
12511 return new Fixed_array_construction_expression(this->type(),
12512 (this->vals() == NULL
12513 ? NULL
12514 : this->vals()->copy()),
12515 this->location());
12516 }
12517
12518 tree
12519 do_get_tree(Translate_context*);
8b1c301d 12520
12521 void
12522 do_dump_expression(Ast_dump_context*);
e440a328 12523};
12524
12525// Return a tree for constructing a fixed array.
12526
12527tree
12528Fixed_array_construction_expression::do_get_tree(Translate_context* context)
12529{
9f0e0513 12530 Type* type = this->type();
12531 Btype* btype = type->get_backend(context->gogo());
12532 return this->get_constructor_tree(context, type_to_tree(btype));
e440a328 12533}
12534
8b1c301d 12535// Dump ast representation of an array construction expressin.
12536
12537void
12538Fixed_array_construction_expression::do_dump_expression(
12539 Ast_dump_context* ast_dump_context)
12540{
12541
12542 ast_dump_context->ostream() << "[";
12543 ast_dump_context->dump_expression (this->type()->array_type()->length());
12544 ast_dump_context->ostream() << "]";
12545 ast_dump_context->dump_type(this->type());
12546 ast_dump_context->ostream() << "{";
12547 ast_dump_context->dump_expression_list(this->vals());
12548 ast_dump_context->ostream() << "}";
12549
12550}
e440a328 12551// Construct an open array.
12552
12553class Open_array_construction_expression : public Array_construction_expression
12554{
12555 public:
12556 Open_array_construction_expression(Type* type, Expression_list* vals,
b13c66cd 12557 Location location)
e440a328 12558 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
12559 type, vals, location)
12560 {
c484d925 12561 go_assert(type->array_type() != NULL
e440a328 12562 && type->array_type()->length() == NULL);
12563 }
12564
12565 protected:
12566 // Note that taking the address of an open array literal is invalid.
12567
12568 Expression*
12569 do_copy()
12570 {
12571 return new Open_array_construction_expression(this->type(),
12572 (this->vals() == NULL
12573 ? NULL
12574 : this->vals()->copy()),
12575 this->location());
12576 }
12577
12578 tree
12579 do_get_tree(Translate_context*);
12580};
12581
12582// Return a tree for constructing an open array.
12583
12584tree
12585Open_array_construction_expression::do_get_tree(Translate_context* context)
12586{
f9c68f17 12587 Array_type* array_type = this->type()->array_type();
12588 if (array_type == NULL)
12589 {
c484d925 12590 go_assert(this->type()->is_error());
f9c68f17 12591 return error_mark_node;
12592 }
12593
12594 Type* element_type = array_type->element_type();
9f0e0513 12595 Btype* belement_type = element_type->get_backend(context->gogo());
12596 tree element_type_tree = type_to_tree(belement_type);
3d60812e 12597 if (element_type_tree == error_mark_node)
12598 return error_mark_node;
12599
e440a328 12600 tree values;
12601 tree length_tree;
12602 if (this->vals() == NULL || this->vals()->empty())
12603 {
12604 // We need to create a unique value.
12605 tree max = size_int(0);
12606 tree constructor_type = build_array_type(element_type_tree,
12607 build_index_type(max));
12608 if (constructor_type == error_mark_node)
12609 return error_mark_node;
12610 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
12611 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
12612 elt->index = size_int(0);
63697958 12613 Gogo* gogo = context->gogo();
12614 Btype* btype = element_type->get_backend(gogo);
12615 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
e440a328 12616 values = build_constructor(constructor_type, vec);
12617 if (TREE_CONSTANT(elt->value))
12618 TREE_CONSTANT(values) = 1;
12619 length_tree = size_int(0);
12620 }
12621 else
12622 {
12623 tree max = size_int(this->vals()->size() - 1);
12624 tree constructor_type = build_array_type(element_type_tree,
12625 build_index_type(max));
12626 if (constructor_type == error_mark_node)
12627 return error_mark_node;
12628 values = this->get_constructor_tree(context, constructor_type);
12629 length_tree = size_int(this->vals()->size());
12630 }
12631
12632 if (values == error_mark_node)
12633 return error_mark_node;
12634
12635 bool is_constant_initializer = TREE_CONSTANT(values);
d8829beb 12636
12637 // We have to copy the initial values into heap memory if we are in
12638 // a function or if the values are not constants. We also have to
12639 // copy them if they may contain pointers in a non-constant context,
12640 // as otherwise the garbage collector won't see them.
12641 bool copy_to_heap = (context->function() != NULL
12642 || !is_constant_initializer
12643 || (element_type->has_pointer()
12644 && !context->is_const()));
e440a328 12645
12646 if (is_constant_initializer)
12647 {
b13c66cd 12648 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
e440a328 12649 create_tmp_var_name("C"), TREE_TYPE(values));
12650 DECL_EXTERNAL(tmp) = 0;
12651 TREE_PUBLIC(tmp) = 0;
12652 TREE_STATIC(tmp) = 1;
12653 DECL_ARTIFICIAL(tmp) = 1;
d8829beb 12654 if (copy_to_heap)
e440a328 12655 {
d8829beb 12656 // If we are not copying the value to the heap, we will only
12657 // initialize the value once, so we can use this directly
12658 // rather than copying it. In that case we can't make it
12659 // read-only, because the program is permitted to change it.
e440a328 12660 TREE_READONLY(tmp) = 1;
12661 TREE_CONSTANT(tmp) = 1;
12662 }
12663 DECL_INITIAL(tmp) = values;
12664 rest_of_decl_compilation(tmp, 1, 0);
12665 values = tmp;
12666 }
12667
12668 tree space;
12669 tree set;
d8829beb 12670 if (!copy_to_heap)
e440a328 12671 {
d8829beb 12672 // the initializer will only run once.
e440a328 12673 space = build_fold_addr_expr(values);
12674 set = NULL_TREE;
12675 }
12676 else
12677 {
12678 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
12679 space = context->gogo()->allocate_memory(element_type, memsize,
12680 this->location());
12681 space = save_expr(space);
12682
12683 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
b13c66cd 12684 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
12685 s);
e440a328 12686 TREE_THIS_NOTRAP(ref) = 1;
12687 set = build2(MODIFY_EXPR, void_type_node, ref, values);
12688 }
12689
12690 // Build a constructor for the open array.
12691
9f0e0513 12692 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
3d60812e 12693 if (type_tree == error_mark_node)
12694 return error_mark_node;
c484d925 12695 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
e440a328 12696
12697 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
12698
12699 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
12700 tree field = TYPE_FIELDS(type_tree);
c484d925 12701 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
e440a328 12702 elt->index = field;
12703 elt->value = fold_convert(TREE_TYPE(field), space);
12704
12705 elt = VEC_quick_push(constructor_elt, init, NULL);
12706 field = DECL_CHAIN(field);
c484d925 12707 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
e440a328 12708 elt->index = field;
12709 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12710
12711 elt = VEC_quick_push(constructor_elt, init, NULL);
12712 field = DECL_CHAIN(field);
c484d925 12713 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
e440a328 12714 elt->index = field;
12715 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12716
12717 tree constructor = build_constructor(type_tree, init);
3d60812e 12718 if (constructor == error_mark_node)
12719 return error_mark_node;
d8829beb 12720 if (!copy_to_heap)
e440a328 12721 TREE_CONSTANT(constructor) = 1;
12722
12723 if (set == NULL_TREE)
12724 return constructor;
12725 else
12726 return build2(COMPOUND_EXPR, type_tree, set, constructor);
12727}
12728
12729// Make a slice composite literal. This is used by the type
12730// descriptor code.
12731
12732Expression*
12733Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
b13c66cd 12734 Location location)
e440a328 12735{
411eb89e 12736 go_assert(type->is_slice_type());
e440a328 12737 return new Open_array_construction_expression(type, vals, location);
12738}
12739
12740// Construct a map.
12741
12742class Map_construction_expression : public Expression
12743{
12744 public:
12745 Map_construction_expression(Type* type, Expression_list* vals,
b13c66cd 12746 Location location)
e440a328 12747 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
12748 type_(type), vals_(vals)
c484d925 12749 { go_assert(vals == NULL || vals->size() % 2 == 0); }
e440a328 12750
12751 protected:
12752 int
12753 do_traverse(Traverse* traverse);
12754
12755 Type*
12756 do_type()
12757 { return this->type_; }
12758
12759 void
12760 do_determine_type(const Type_context*);
12761
12762 void
12763 do_check_types(Gogo*);
12764
12765 Expression*
12766 do_copy()
12767 {
12768 return new Map_construction_expression(this->type_, this->vals_->copy(),
12769 this->location());
12770 }
12771
12772 tree
12773 do_get_tree(Translate_context*);
12774
12775 void
12776 do_export(Export*) const;
12777
d751bb78 12778 void
12779 do_dump_expression(Ast_dump_context*) const;
12780
e440a328 12781 private:
12782 // The type of the map to construct.
12783 Type* type_;
12784 // The list of values.
12785 Expression_list* vals_;
12786};
12787
12788// Traversal.
12789
12790int
12791Map_construction_expression::do_traverse(Traverse* traverse)
12792{
12793 if (this->vals_ != NULL
12794 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12795 return TRAVERSE_EXIT;
12796 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12797 return TRAVERSE_EXIT;
12798 return TRAVERSE_CONTINUE;
12799}
12800
12801// Final type determination.
12802
12803void
12804Map_construction_expression::do_determine_type(const Type_context*)
12805{
12806 if (this->vals_ == NULL)
12807 return;
12808
12809 Map_type* mt = this->type_->map_type();
12810 Type_context key_context(mt->key_type(), false);
12811 Type_context val_context(mt->val_type(), false);
12812 for (Expression_list::const_iterator pv = this->vals_->begin();
12813 pv != this->vals_->end();
12814 ++pv)
12815 {
12816 (*pv)->determine_type(&key_context);
12817 ++pv;
12818 (*pv)->determine_type(&val_context);
12819 }
12820}
12821
12822// Check types.
12823
12824void
12825Map_construction_expression::do_check_types(Gogo*)
12826{
12827 if (this->vals_ == NULL)
12828 return;
12829
12830 Map_type* mt = this->type_->map_type();
12831 int i = 0;
12832 Type* key_type = mt->key_type();
12833 Type* val_type = mt->val_type();
12834 for (Expression_list::const_iterator pv = this->vals_->begin();
12835 pv != this->vals_->end();
12836 ++pv, ++i)
12837 {
12838 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12839 {
12840 error_at((*pv)->location(),
12841 "incompatible type for element %d key in map construction",
12842 i + 1);
12843 this->set_is_error();
12844 }
12845 ++pv;
12846 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12847 {
12848 error_at((*pv)->location(),
12849 ("incompatible type for element %d value "
12850 "in map construction"),
12851 i + 1);
12852 this->set_is_error();
12853 }
12854 }
12855}
12856
12857// Return a tree for constructing a map.
12858
12859tree
12860Map_construction_expression::do_get_tree(Translate_context* context)
12861{
12862 Gogo* gogo = context->gogo();
b13c66cd 12863 Location loc = this->location();
e440a328 12864
12865 Map_type* mt = this->type_->map_type();
12866
12867 // Build a struct to hold the key and value.
12868 tree struct_type = make_node(RECORD_TYPE);
12869
12870 Type* key_type = mt->key_type();
12871 tree id = get_identifier("__key");
9f0e0513 12872 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
5845bde6 12873 if (key_type_tree == error_mark_node)
12874 return error_mark_node;
b13c66cd 12875 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12876 key_type_tree);
e440a328 12877 DECL_CONTEXT(key_field) = struct_type;
12878 TYPE_FIELDS(struct_type) = key_field;
12879
12880 Type* val_type = mt->val_type();
12881 id = get_identifier("__val");
9f0e0513 12882 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
5845bde6 12883 if (val_type_tree == error_mark_node)
12884 return error_mark_node;
b13c66cd 12885 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12886 val_type_tree);
e440a328 12887 DECL_CONTEXT(val_field) = struct_type;
12888 DECL_CHAIN(key_field) = val_field;
12889
12890 layout_type(struct_type);
12891
12892 bool is_constant = true;
12893 size_t i = 0;
12894 tree valaddr;
12895 tree make_tmp;
12896
12897 if (this->vals_ == NULL || this->vals_->empty())
12898 {
12899 valaddr = null_pointer_node;
12900 make_tmp = NULL_TREE;
12901 }
12902 else
12903 {
12904 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12905 this->vals_->size() / 2);
12906
12907 for (Expression_list::const_iterator pv = this->vals_->begin();
12908 pv != this->vals_->end();
12909 ++pv, ++i)
12910 {
12911 bool one_is_constant = true;
12912
12913 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12914
12915 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12916 elt->index = key_field;
12917 tree val_tree = (*pv)->get_tree(context);
12918 elt->value = Expression::convert_for_assignment(context, key_type,
12919 (*pv)->type(),
12920 val_tree, loc);
12921 if (elt->value == error_mark_node)
12922 return error_mark_node;
12923 if (!TREE_CONSTANT(elt->value))
12924 one_is_constant = false;
12925
12926 ++pv;
12927
12928 elt = VEC_quick_push(constructor_elt, one, NULL);
12929 elt->index = val_field;
12930 val_tree = (*pv)->get_tree(context);
12931 elt->value = Expression::convert_for_assignment(context, val_type,
12932 (*pv)->type(),
12933 val_tree, loc);
12934 if (elt->value == error_mark_node)
12935 return error_mark_node;
12936 if (!TREE_CONSTANT(elt->value))
12937 one_is_constant = false;
12938
12939 elt = VEC_quick_push(constructor_elt, values, NULL);
12940 elt->index = size_int(i);
12941 elt->value = build_constructor(struct_type, one);
12942 if (one_is_constant)
12943 TREE_CONSTANT(elt->value) = 1;
12944 else
12945 is_constant = false;
12946 }
12947
12948 tree index_type = build_index_type(size_int(i - 1));
12949 tree array_type = build_array_type(struct_type, index_type);
12950 tree init = build_constructor(array_type, values);
12951 if (is_constant)
12952 TREE_CONSTANT(init) = 1;
12953 tree tmp;
12954 if (current_function_decl != NULL)
12955 {
12956 tmp = create_tmp_var(array_type, get_name(array_type));
12957 DECL_INITIAL(tmp) = init;
b13c66cd 12958 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
12959 void_type_node, tmp);
e440a328 12960 TREE_ADDRESSABLE(tmp) = 1;
12961 }
12962 else
12963 {
b13c66cd 12964 tmp = build_decl(loc.gcc_location(), VAR_DECL,
12965 create_tmp_var_name("M"), array_type);
e440a328 12966 DECL_EXTERNAL(tmp) = 0;
12967 TREE_PUBLIC(tmp) = 0;
12968 TREE_STATIC(tmp) = 1;
12969 DECL_ARTIFICIAL(tmp) = 1;
12970 if (!TREE_CONSTANT(init))
b13c66cd 12971 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
12972 void_type_node, tmp, init);
e440a328 12973 else
12974 {
12975 TREE_READONLY(tmp) = 1;
12976 TREE_CONSTANT(tmp) = 1;
12977 DECL_INITIAL(tmp) = init;
12978 make_tmp = NULL_TREE;
12979 }
12980 rest_of_decl_compilation(tmp, 1, 0);
12981 }
12982
12983 valaddr = build_fold_addr_expr(tmp);
12984 }
12985
2b5f213d 12986 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
e440a328 12987
9f0e0513 12988 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
5845bde6 12989 if (type_tree == error_mark_node)
12990 return error_mark_node;
e440a328 12991
12992 static tree construct_map_fndecl;
12993 tree call = Gogo::call_builtin(&construct_map_fndecl,
12994 loc,
12995 "__go_construct_map",
12996 6,
12997 type_tree,
12998 TREE_TYPE(descriptor),
12999 descriptor,
13000 sizetype,
13001 size_int(i),
13002 sizetype,
13003 TYPE_SIZE_UNIT(struct_type),
13004 sizetype,
13005 byte_position(val_field),
13006 sizetype,
13007 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
13008 const_ptr_type_node,
13009 fold_convert(const_ptr_type_node, valaddr));
5fb82b5e 13010 if (call == error_mark_node)
13011 return error_mark_node;
e440a328 13012
13013 tree ret;
13014 if (make_tmp == NULL)
13015 ret = call;
13016 else
b13c66cd 13017 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
13018 make_tmp, call);
e440a328 13019 return ret;
13020}
13021
13022// Export an array construction.
13023
13024void
13025Map_construction_expression::do_export(Export* exp) const
13026{
13027 exp->write_c_string("convert(");
13028 exp->write_type(this->type_);
13029 for (Expression_list::const_iterator pv = this->vals_->begin();
13030 pv != this->vals_->end();
13031 ++pv)
13032 {
13033 exp->write_c_string(", ");
13034 (*pv)->export_expression(exp);
13035 }
13036 exp->write_c_string(")");
13037}
13038
d751bb78 13039// Dump ast representation for a map construction expression.
13040
13041void
13042Map_construction_expression::do_dump_expression(
13043 Ast_dump_context* ast_dump_context) const
13044{
d751bb78 13045 ast_dump_context->ostream() << "{" ;
8b1c301d 13046 ast_dump_context->dump_expression_list(this->vals_, true);
d751bb78 13047 ast_dump_context->ostream() << "}";
13048}
13049
e440a328 13050// A general composite literal. This is lowered to a type specific
13051// version.
13052
13053class Composite_literal_expression : public Parser_expression
13054{
13055 public:
13056 Composite_literal_expression(Type* type, int depth, bool has_keys,
b13c66cd 13057 Expression_list* vals, Location location)
e440a328 13058 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
13059 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
13060 { }
13061
13062 protected:
13063 int
13064 do_traverse(Traverse* traverse);
13065
13066 Expression*
ceeb4318 13067 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 13068
13069 Expression*
13070 do_copy()
13071 {
13072 return new Composite_literal_expression(this->type_, this->depth_,
13073 this->has_keys_,
13074 (this->vals_ == NULL
13075 ? NULL
13076 : this->vals_->copy()),
13077 this->location());
13078 }
13079
d751bb78 13080 void
13081 do_dump_expression(Ast_dump_context*) const;
13082
e440a328 13083 private:
13084 Expression*
81c4b26b 13085 lower_struct(Gogo*, Type*);
e440a328 13086
13087 Expression*
113ef6a5 13088 lower_array(Type*);
e440a328 13089
13090 Expression*
113ef6a5 13091 make_array(Type*, Expression_list*);
e440a328 13092
13093 Expression*
ceeb4318 13094 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
e440a328 13095
13096 // The type of the composite literal.
13097 Type* type_;
13098 // The depth within a list of composite literals within a composite
13099 // literal, when the type is omitted.
13100 int depth_;
13101 // The values to put in the composite literal.
13102 Expression_list* vals_;
13103 // If this is true, then VALS_ is a list of pairs: a key and a
13104 // value. In an array initializer, a missing key will be NULL.
13105 bool has_keys_;
13106};
13107
13108// Traversal.
13109
13110int
13111Composite_literal_expression::do_traverse(Traverse* traverse)
13112{
13113 if (this->vals_ != NULL
13114 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
13115 return TRAVERSE_EXIT;
13116 return Type::traverse(this->type_, traverse);
13117}
13118
13119// Lower a generic composite literal into a specific version based on
13120// the type.
13121
13122Expression*
ceeb4318 13123Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
13124 Statement_inserter* inserter, int)
e440a328 13125{
13126 Type* type = this->type_;
13127
13128 for (int depth = this->depth_; depth > 0; --depth)
13129 {
13130 if (type->array_type() != NULL)
13131 type = type->array_type()->element_type();
13132 else if (type->map_type() != NULL)
13133 type = type->map_type()->val_type();
13134 else
13135 {
5c13bd80 13136 if (!type->is_error())
e440a328 13137 error_at(this->location(),
13138 ("may only omit types within composite literals "
13139 "of slice, array, or map type"));
13140 return Expression::make_error(this->location());
13141 }
13142 }
13143
e00772b3 13144 Type *pt = type->points_to();
13145 bool is_pointer = false;
13146 if (pt != NULL)
13147 {
13148 is_pointer = true;
13149 type = pt;
13150 }
13151
13152 Expression* ret;
5c13bd80 13153 if (type->is_error())
e440a328 13154 return Expression::make_error(this->location());
13155 else if (type->struct_type() != NULL)
e00772b3 13156 ret = this->lower_struct(gogo, type);
e440a328 13157 else if (type->array_type() != NULL)
113ef6a5 13158 ret = this->lower_array(type);
e440a328 13159 else if (type->map_type() != NULL)
e00772b3 13160 ret = this->lower_map(gogo, function, inserter, type);
e440a328 13161 else
13162 {
13163 error_at(this->location(),
13164 ("expected struct, slice, array, or map type "
13165 "for composite literal"));
13166 return Expression::make_error(this->location());
13167 }
e00772b3 13168
13169 if (is_pointer)
13170 ret = Expression::make_heap_composite(ret, this->location());
13171
13172 return ret;
e440a328 13173}
13174
13175// Lower a struct composite literal.
13176
13177Expression*
81c4b26b 13178Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
e440a328 13179{
b13c66cd 13180 Location location = this->location();
e440a328 13181 Struct_type* st = type->struct_type();
13182 if (this->vals_ == NULL || !this->has_keys_)
07daa4e7 13183 {
e6013c28 13184 if (this->vals_ != NULL
13185 && !this->vals_->empty()
13186 && type->named_type() != NULL
13187 && type->named_type()->named_object()->package() != NULL)
13188 {
13189 for (Struct_field_list::const_iterator pf = st->fields()->begin();
13190 pf != st->fields()->end();
13191 ++pf)
07daa4e7 13192 {
e6013c28 13193 if (Gogo::is_hidden_name(pf->field_name()))
07daa4e7 13194 error_at(this->location(),
e6013c28 13195 "assignment of unexported field %qs in %qs literal",
13196 Gogo::message_name(pf->field_name()).c_str(),
13197 type->named_type()->message_name().c_str());
07daa4e7 13198 }
13199 }
13200
13201 return new Struct_construction_expression(type, this->vals_, location);
13202 }
e440a328 13203
13204 size_t field_count = st->field_count();
13205 std::vector<Expression*> vals(field_count);
13206 Expression_list::const_iterator p = this->vals_->begin();
13207 while (p != this->vals_->end())
13208 {
13209 Expression* name_expr = *p;
13210
13211 ++p;
c484d925 13212 go_assert(p != this->vals_->end());
e440a328 13213 Expression* val = *p;
13214
13215 ++p;
13216
13217 if (name_expr == NULL)
13218 {
13219 error_at(val->location(), "mixture of field and value initializers");
13220 return Expression::make_error(location);
13221 }
13222
13223 bool bad_key = false;
13224 std::string name;
81c4b26b 13225 const Named_object* no = NULL;
e440a328 13226 switch (name_expr->classification())
13227 {
13228 case EXPRESSION_UNKNOWN_REFERENCE:
13229 name = name_expr->unknown_expression()->name();
13230 break;
13231
13232 case EXPRESSION_CONST_REFERENCE:
81c4b26b 13233 no = static_cast<Const_expression*>(name_expr)->named_object();
e440a328 13234 break;
13235
13236 case EXPRESSION_TYPE:
13237 {
13238 Type* t = name_expr->type();
13239 Named_type* nt = t->named_type();
13240 if (nt == NULL)
13241 bad_key = true;
13242 else
81c4b26b 13243 no = nt->named_object();
e440a328 13244 }
13245 break;
13246
13247 case EXPRESSION_VAR_REFERENCE:
81c4b26b 13248 no = name_expr->var_expression()->named_object();
e440a328 13249 break;
13250
13251 case EXPRESSION_FUNC_REFERENCE:
81c4b26b 13252 no = name_expr->func_expression()->named_object();
e440a328 13253 break;
13254
13255 case EXPRESSION_UNARY:
13256 // If there is a local variable around with the same name as
13257 // the field, and this occurs in the closure, then the
13258 // parser may turn the field reference into an indirection
13259 // through the closure. FIXME: This is a mess.
13260 {
13261 bad_key = true;
13262 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
13263 if (ue->op() == OPERATOR_MULT)
13264 {
13265 Field_reference_expression* fre =
13266 ue->operand()->field_reference_expression();
13267 if (fre != NULL)
13268 {
13269 Struct_type* st =
13270 fre->expr()->type()->deref()->struct_type();
13271 if (st != NULL)
13272 {
13273 const Struct_field* sf = st->field(fre->field_index());
13274 name = sf->field_name();
2d29d278 13275
13276 // See below. FIXME.
13277 if (!Gogo::is_hidden_name(name)
13278 && name[0] >= 'a'
13279 && name[0] <= 'z')
13280 {
13281 if (gogo->lookup_global(name.c_str()) != NULL)
13282 name = gogo->pack_hidden_name(name, false);
13283 }
13284
e440a328 13285 char buf[20];
13286 snprintf(buf, sizeof buf, "%u", fre->field_index());
13287 size_t buflen = strlen(buf);
13288 if (name.compare(name.length() - buflen, buflen, buf)
13289 == 0)
13290 {
13291 name = name.substr(0, name.length() - buflen);
13292 bad_key = false;
13293 }
13294 }
13295 }
13296 }
13297 }
13298 break;
13299
13300 default:
13301 bad_key = true;
13302 break;
13303 }
13304 if (bad_key)
13305 {
13306 error_at(name_expr->location(), "expected struct field name");
13307 return Expression::make_error(location);
13308 }
13309
81c4b26b 13310 if (no != NULL)
13311 {
13312 name = no->name();
13313
13314 // A predefined name won't be packed. If it starts with a
13315 // lower case letter we need to check for that case, because
2d29d278 13316 // the field name will be packed. FIXME.
81c4b26b 13317 if (!Gogo::is_hidden_name(name)
13318 && name[0] >= 'a'
13319 && name[0] <= 'z')
13320 {
13321 Named_object* gno = gogo->lookup_global(name.c_str());
13322 if (gno == no)
13323 name = gogo->pack_hidden_name(name, false);
13324 }
13325 }
13326
e440a328 13327 unsigned int index;
13328 const Struct_field* sf = st->find_local_field(name, &index);
13329 if (sf == NULL)
13330 {
13331 error_at(name_expr->location(), "unknown field %qs in %qs",
13332 Gogo::message_name(name).c_str(),
13333 (type->named_type() != NULL
13334 ? type->named_type()->message_name().c_str()
13335 : "unnamed struct"));
13336 return Expression::make_error(location);
13337 }
13338 if (vals[index] != NULL)
13339 {
13340 error_at(name_expr->location(),
13341 "duplicate value for field %qs in %qs",
13342 Gogo::message_name(name).c_str(),
13343 (type->named_type() != NULL
13344 ? type->named_type()->message_name().c_str()
13345 : "unnamed struct"));
13346 return Expression::make_error(location);
13347 }
13348
07daa4e7 13349 if (type->named_type() != NULL
13350 && type->named_type()->named_object()->package() != NULL
13351 && Gogo::is_hidden_name(sf->field_name()))
13352 error_at(name_expr->location(),
13353 "assignment of unexported field %qs in %qs literal",
13354 Gogo::message_name(sf->field_name()).c_str(),
13355 type->named_type()->message_name().c_str());
07daa4e7 13356
e440a328 13357 vals[index] = val;
13358 }
13359
13360 Expression_list* list = new Expression_list;
13361 list->reserve(field_count);
13362 for (size_t i = 0; i < field_count; ++i)
13363 list->push_back(vals[i]);
13364
13365 return new Struct_construction_expression(type, list, location);
13366}
13367
13368// Lower an array composite literal.
13369
13370Expression*
113ef6a5 13371Composite_literal_expression::lower_array(Type* type)
e440a328 13372{
b13c66cd 13373 Location location = this->location();
e440a328 13374 if (this->vals_ == NULL || !this->has_keys_)
113ef6a5 13375 return this->make_array(type, this->vals_);
e440a328 13376
13377 std::vector<Expression*> vals;
13378 vals.reserve(this->vals_->size());
13379 unsigned long index = 0;
13380 Expression_list::const_iterator p = this->vals_->begin();
13381 while (p != this->vals_->end())
13382 {
13383 Expression* index_expr = *p;
13384
13385 ++p;
c484d925 13386 go_assert(p != this->vals_->end());
e440a328 13387 Expression* val = *p;
13388
13389 ++p;
13390
13391 if (index_expr != NULL)
13392 {
13393 mpz_t ival;
13394 mpz_init(ival);
6f6d9955 13395
e440a328 13396 Type* dummy;
13397 if (!index_expr->integer_constant_value(true, ival, &dummy))
13398 {
13399 mpz_clear(ival);
13400 error_at(index_expr->location(),
13401 "index expression is not integer constant");
13402 return Expression::make_error(location);
13403 }
6f6d9955 13404
e440a328 13405 if (mpz_sgn(ival) < 0)
13406 {
13407 mpz_clear(ival);
13408 error_at(index_expr->location(), "index expression is negative");
13409 return Expression::make_error(location);
13410 }
6f6d9955 13411
e440a328 13412 index = mpz_get_ui(ival);
13413 if (mpz_cmp_ui(ival, index) != 0)
13414 {
13415 mpz_clear(ival);
13416 error_at(index_expr->location(), "index value overflow");
13417 return Expression::make_error(location);
13418 }
6f6d9955 13419
13420 Named_type* ntype = Type::lookup_integer_type("int");
13421 Integer_type* inttype = ntype->integer_type();
13422 mpz_t max;
13423 mpz_init_set_ui(max, 1);
13424 mpz_mul_2exp(max, max, inttype->bits() - 1);
13425 bool ok = mpz_cmp(ival, max) < 0;
13426 mpz_clear(max);
13427 if (!ok)
13428 {
13429 mpz_clear(ival);
13430 error_at(index_expr->location(), "index value overflow");
13431 return Expression::make_error(location);
13432 }
13433
e440a328 13434 mpz_clear(ival);
6f6d9955 13435
13436 // FIXME: Our representation isn't very good; this avoids
13437 // thrashing.
13438 if (index > 0x1000000)
13439 {
13440 error_at(index_expr->location(), "index too large for compiler");
13441 return Expression::make_error(location);
13442 }
e440a328 13443 }
13444
13445 if (index == vals.size())
13446 vals.push_back(val);
13447 else
13448 {
13449 if (index > vals.size())
13450 {
13451 vals.reserve(index + 32);
13452 vals.resize(index + 1, static_cast<Expression*>(NULL));
13453 }
13454 if (vals[index] != NULL)
13455 {
13456 error_at((index_expr != NULL
13457 ? index_expr->location()
13458 : val->location()),
13459 "duplicate value for index %lu",
13460 index);
13461 return Expression::make_error(location);
13462 }
13463 vals[index] = val;
13464 }
13465
13466 ++index;
13467 }
13468
13469 size_t size = vals.size();
13470 Expression_list* list = new Expression_list;
13471 list->reserve(size);
13472 for (size_t i = 0; i < size; ++i)
13473 list->push_back(vals[i]);
13474
113ef6a5 13475 return this->make_array(type, list);
e440a328 13476}
13477
13478// Actually build the array composite literal. This handles
13479// [...]{...}.
13480
13481Expression*
113ef6a5 13482Composite_literal_expression::make_array(Type* type, Expression_list* vals)
e440a328 13483{
b13c66cd 13484 Location location = this->location();
e440a328 13485 Array_type* at = type->array_type();
13486 if (at->length() != NULL && at->length()->is_nil_expression())
13487 {
13488 size_t size = vals == NULL ? 0 : vals->size();
13489 mpz_t vlen;
13490 mpz_init_set_ui(vlen, size);
13491 Expression* elen = Expression::make_integer(&vlen, NULL, location);
13492 mpz_clear(vlen);
13493 at = Type::make_array_type(at->element_type(), elen);
13494 type = at;
13495 }
13496 if (at->length() != NULL)
13497 return new Fixed_array_construction_expression(type, vals, location);
13498 else
13499 return new Open_array_construction_expression(type, vals, location);
13500}
13501
13502// Lower a map composite literal.
13503
13504Expression*
a287720d 13505Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
ceeb4318 13506 Statement_inserter* inserter,
a287720d 13507 Type* type)
e440a328 13508{
b13c66cd 13509 Location location = this->location();
e440a328 13510 if (this->vals_ != NULL)
13511 {
13512 if (!this->has_keys_)
13513 {
13514 error_at(location, "map composite literal must have keys");
13515 return Expression::make_error(location);
13516 }
13517
a287720d 13518 for (Expression_list::iterator p = this->vals_->begin();
e440a328 13519 p != this->vals_->end();
13520 p += 2)
13521 {
13522 if (*p == NULL)
13523 {
13524 ++p;
13525 error_at((*p)->location(),
13526 "map composite literal must have keys for every value");
13527 return Expression::make_error(location);
13528 }
a287720d 13529 // Make sure we have lowered the key; it may not have been
13530 // lowered in order to handle keys for struct composite
13531 // literals. Lower it now to get the right error message.
13532 if ((*p)->unknown_expression() != NULL)
13533 {
13534 (*p)->unknown_expression()->clear_is_composite_literal_key();
ceeb4318 13535 gogo->lower_expression(function, inserter, &*p);
c484d925 13536 go_assert((*p)->is_error_expression());
a287720d 13537 return Expression::make_error(location);
13538 }
e440a328 13539 }
13540 }
13541
13542 return new Map_construction_expression(type, this->vals_, location);
13543}
13544
d751bb78 13545// Dump ast representation for a composite literal expression.
13546
13547void
13548Composite_literal_expression::do_dump_expression(
13549 Ast_dump_context* ast_dump_context) const
13550{
8b1c301d 13551 ast_dump_context->ostream() << "composite(";
d751bb78 13552 ast_dump_context->dump_type(this->type_);
13553 ast_dump_context->ostream() << ", {";
8b1c301d 13554 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
d751bb78 13555 ast_dump_context->ostream() << "})";
13556}
13557
e440a328 13558// Make a composite literal expression.
13559
13560Expression*
13561Expression::make_composite_literal(Type* type, int depth, bool has_keys,
13562 Expression_list* vals,
b13c66cd 13563 Location location)
e440a328 13564{
13565 return new Composite_literal_expression(type, depth, has_keys, vals,
13566 location);
13567}
13568
13569// Return whether this expression is a composite literal.
13570
13571bool
13572Expression::is_composite_literal() const
13573{
13574 switch (this->classification_)
13575 {
13576 case EXPRESSION_COMPOSITE_LITERAL:
13577 case EXPRESSION_STRUCT_CONSTRUCTION:
13578 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13579 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13580 case EXPRESSION_MAP_CONSTRUCTION:
13581 return true;
13582 default:
13583 return false;
13584 }
13585}
13586
13587// Return whether this expression is a composite literal which is not
13588// constant.
13589
13590bool
13591Expression::is_nonconstant_composite_literal() const
13592{
13593 switch (this->classification_)
13594 {
13595 case EXPRESSION_STRUCT_CONSTRUCTION:
13596 {
13597 const Struct_construction_expression *psce =
13598 static_cast<const Struct_construction_expression*>(this);
13599 return !psce->is_constant_struct();
13600 }
13601 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13602 {
13603 const Fixed_array_construction_expression *pace =
13604 static_cast<const Fixed_array_construction_expression*>(this);
13605 return !pace->is_constant_array();
13606 }
13607 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13608 {
13609 const Open_array_construction_expression *pace =
13610 static_cast<const Open_array_construction_expression*>(this);
13611 return !pace->is_constant_array();
13612 }
13613 case EXPRESSION_MAP_CONSTRUCTION:
13614 return true;
13615 default:
13616 return false;
13617 }
13618}
13619
13620// Return true if this is a reference to a local variable.
13621
13622bool
13623Expression::is_local_variable() const
13624{
13625 const Var_expression* ve = this->var_expression();
13626 if (ve == NULL)
13627 return false;
13628 const Named_object* no = ve->named_object();
13629 return (no->is_result_variable()
13630 || (no->is_variable() && !no->var_value()->is_global()));
13631}
13632
13633// Class Type_guard_expression.
13634
13635// Traversal.
13636
13637int
13638Type_guard_expression::do_traverse(Traverse* traverse)
13639{
13640 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
13641 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13642 return TRAVERSE_EXIT;
13643 return TRAVERSE_CONTINUE;
13644}
13645
13646// Check types of a type guard expression. The expression must have
13647// an interface type, but the actual type conversion is checked at run
13648// time.
13649
13650void
13651Type_guard_expression::do_check_types(Gogo*)
13652{
13653 // 6g permits using a type guard with unsafe.pointer; we are
13654 // compatible.
13655 Type* expr_type = this->expr_->type();
13656 if (expr_type->is_unsafe_pointer_type())
13657 {
13658 if (this->type_->points_to() == NULL
13659 && (this->type_->integer_type() == NULL
13660 || (this->type_->forwarded()
13661 != Type::lookup_integer_type("uintptr"))))
13662 this->report_error(_("invalid unsafe.Pointer conversion"));
13663 }
13664 else if (this->type_->is_unsafe_pointer_type())
13665 {
13666 if (expr_type->points_to() == NULL
13667 && (expr_type->integer_type() == NULL
13668 || (expr_type->forwarded()
13669 != Type::lookup_integer_type("uintptr"))))
13670 this->report_error(_("invalid unsafe.Pointer conversion"));
13671 }
13672 else if (expr_type->interface_type() == NULL)
f725ade8 13673 {
5c13bd80 13674 if (!expr_type->is_error() && !this->type_->is_error())
f725ade8 13675 this->report_error(_("type assertion only valid for interface types"));
13676 this->set_is_error();
13677 }
e440a328 13678 else if (this->type_->interface_type() == NULL)
13679 {
13680 std::string reason;
13681 if (!expr_type->interface_type()->implements_interface(this->type_,
13682 &reason))
13683 {
5c13bd80 13684 if (!this->type_->is_error())
e440a328 13685 {
f725ade8 13686 if (reason.empty())
13687 this->report_error(_("impossible type assertion: "
13688 "type does not implement interface"));
13689 else
13690 error_at(this->location(),
13691 ("impossible type assertion: "
13692 "type does not implement interface (%s)"),
13693 reason.c_str());
e440a328 13694 }
f725ade8 13695 this->set_is_error();
e440a328 13696 }
13697 }
13698}
13699
13700// Return a tree for a type guard expression.
13701
13702tree
13703Type_guard_expression::do_get_tree(Translate_context* context)
13704{
13705 Gogo* gogo = context->gogo();
13706 tree expr_tree = this->expr_->get_tree(context);
13707 if (expr_tree == error_mark_node)
13708 return error_mark_node;
13709 Type* expr_type = this->expr_->type();
13710 if ((this->type_->is_unsafe_pointer_type()
13711 && (expr_type->points_to() != NULL
13712 || expr_type->integer_type() != NULL))
13713 || (expr_type->is_unsafe_pointer_type()
13714 && this->type_->points_to() != NULL))
9f0e0513 13715 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
13716 expr_tree);
e440a328 13717 else if (expr_type->is_unsafe_pointer_type()
13718 && this->type_->integer_type() != NULL)
9f0e0513 13719 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
13720 expr_tree);
e440a328 13721 else if (this->type_->interface_type() != NULL)
13722 return Expression::convert_interface_to_interface(context, this->type_,
13723 this->expr_->type(),
13724 expr_tree, true,
13725 this->location());
13726 else
13727 return Expression::convert_for_assignment(context, this->type_,
13728 this->expr_->type(), expr_tree,
13729 this->location());
13730}
13731
d751bb78 13732// Dump ast representation for a type guard expression.
13733
13734void
13735Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
13736 const
13737{
13738 this->expr_->dump_expression(ast_dump_context);
13739 ast_dump_context->ostream() << ".";
13740 ast_dump_context->dump_type(this->type_);
13741}
13742
e440a328 13743// Make a type guard expression.
13744
13745Expression*
13746Expression::make_type_guard(Expression* expr, Type* type,
b13c66cd 13747 Location location)
e440a328 13748{
13749 return new Type_guard_expression(expr, type, location);
13750}
13751
13752// Class Heap_composite_expression.
13753
13754// When you take the address of a composite literal, it is allocated
13755// on the heap. This class implements that.
13756
13757class Heap_composite_expression : public Expression
13758{
13759 public:
b13c66cd 13760 Heap_composite_expression(Expression* expr, Location location)
e440a328 13761 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13762 expr_(expr)
13763 { }
13764
13765 protected:
13766 int
13767 do_traverse(Traverse* traverse)
13768 { return Expression::traverse(&this->expr_, traverse); }
13769
13770 Type*
13771 do_type()
13772 { return Type::make_pointer_type(this->expr_->type()); }
13773
13774 void
13775 do_determine_type(const Type_context*)
13776 { this->expr_->determine_type_no_context(); }
13777
13778 Expression*
13779 do_copy()
13780 {
13781 return Expression::make_heap_composite(this->expr_->copy(),
13782 this->location());
13783 }
13784
13785 tree
13786 do_get_tree(Translate_context*);
13787
13788 // We only export global objects, and the parser does not generate
13789 // this in global scope.
13790 void
13791 do_export(Export*) const
c3e6f413 13792 { go_unreachable(); }
e440a328 13793
d751bb78 13794 void
13795 do_dump_expression(Ast_dump_context*) const;
13796
e440a328 13797 private:
13798 // The composite literal which is being put on the heap.
13799 Expression* expr_;
13800};
13801
13802// Return a tree which allocates a composite literal on the heap.
13803
13804tree
13805Heap_composite_expression::do_get_tree(Translate_context* context)
13806{
13807 tree expr_tree = this->expr_->get_tree(context);
13808 if (expr_tree == error_mark_node)
13809 return error_mark_node;
13810 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
c484d925 13811 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
e440a328 13812 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13813 expr_size, this->location());
13814 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13815 space = save_expr(space);
b13c66cd 13816 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13817 space);
e440a328 13818 TREE_THIS_NOTRAP(ref) = 1;
13819 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13820 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13821 space);
b13c66cd 13822 SET_EXPR_LOCATION(ret, this->location().gcc_location());
e440a328 13823 return ret;
13824}
13825
d751bb78 13826// Dump ast representation for a heap composite expression.
13827
13828void
13829Heap_composite_expression::do_dump_expression(
13830 Ast_dump_context* ast_dump_context) const
13831{
13832 ast_dump_context->ostream() << "&(";
13833 ast_dump_context->dump_expression(this->expr_);
13834 ast_dump_context->ostream() << ")";
13835}
13836
e440a328 13837// Allocate a composite literal on the heap.
13838
13839Expression*
b13c66cd 13840Expression::make_heap_composite(Expression* expr, Location location)
e440a328 13841{
13842 return new Heap_composite_expression(expr, location);
13843}
13844
13845// Class Receive_expression.
13846
13847// Return the type of a receive expression.
13848
13849Type*
13850Receive_expression::do_type()
13851{
13852 Channel_type* channel_type = this->channel_->type()->channel_type();
13853 if (channel_type == NULL)
13854 return Type::make_error_type();
13855 return channel_type->element_type();
13856}
13857
13858// Check types for a receive expression.
13859
13860void
13861Receive_expression::do_check_types(Gogo*)
13862{
13863 Type* type = this->channel_->type();
5c13bd80 13864 if (type->is_error())
e440a328 13865 {
13866 this->set_is_error();
13867 return;
13868 }
13869 if (type->channel_type() == NULL)
13870 {
13871 this->report_error(_("expected channel"));
13872 return;
13873 }
13874 if (!type->channel_type()->may_receive())
13875 {
13876 this->report_error(_("invalid receive on send-only channel"));
13877 return;
13878 }
13879}
13880
13881// Get a tree for a receive expression.
13882
13883tree
13884Receive_expression::do_get_tree(Translate_context* context)
13885{
f24f10bb 13886 Location loc = this->location();
13887
e440a328 13888 Channel_type* channel_type = this->channel_->type()->channel_type();
5b8368f4 13889 if (channel_type == NULL)
13890 {
c484d925 13891 go_assert(this->channel_->type()->is_error());
5b8368f4 13892 return error_mark_node;
13893 }
f24f10bb 13894
13895 Expression* td = Expression::make_type_descriptor(channel_type, loc);
13896 tree td_tree = td->get_tree(context);
13897
e440a328 13898 Type* element_type = channel_type->element_type();
9f0e0513 13899 Btype* element_type_btype = element_type->get_backend(context->gogo());
13900 tree element_type_tree = type_to_tree(element_type_btype);
e440a328 13901
13902 tree channel = this->channel_->get_tree(context);
13903 if (element_type_tree == error_mark_node || channel == error_mark_node)
13904 return error_mark_node;
13905
f24f10bb 13906 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
e440a328 13907}
13908
d751bb78 13909// Dump ast representation for a receive expression.
13910
13911void
13912Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13913{
13914 ast_dump_context->ostream() << " <- " ;
13915 ast_dump_context->dump_expression(channel_);
13916}
13917
e440a328 13918// Make a receive expression.
13919
13920Receive_expression*
b13c66cd 13921Expression::make_receive(Expression* channel, Location location)
e440a328 13922{
13923 return new Receive_expression(channel, location);
13924}
13925
e440a328 13926// An expression which evaluates to a pointer to the type descriptor
13927// of a type.
13928
13929class Type_descriptor_expression : public Expression
13930{
13931 public:
b13c66cd 13932 Type_descriptor_expression(Type* type, Location location)
e440a328 13933 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13934 type_(type)
13935 { }
13936
13937 protected:
13938 Type*
13939 do_type()
13940 { return Type::make_type_descriptor_ptr_type(); }
13941
13942 void
13943 do_determine_type(const Type_context*)
13944 { }
13945
13946 Expression*
13947 do_copy()
13948 { return this; }
13949
13950 tree
13951 do_get_tree(Translate_context* context)
a1d23b41 13952 {
13953 return this->type_->type_descriptor_pointer(context->gogo(),
13954 this->location());
13955 }
e440a328 13956
d751bb78 13957 void
13958 do_dump_expression(Ast_dump_context*) const;
13959
e440a328 13960 private:
13961 // The type for which this is the descriptor.
13962 Type* type_;
13963};
13964
d751bb78 13965// Dump ast representation for a type descriptor expression.
13966
13967void
13968Type_descriptor_expression::do_dump_expression(
13969 Ast_dump_context* ast_dump_context) const
13970{
13971 ast_dump_context->dump_type(this->type_);
13972}
13973
e440a328 13974// Make a type descriptor expression.
13975
13976Expression*
b13c66cd 13977Expression::make_type_descriptor(Type* type, Location location)
e440a328 13978{
13979 return new Type_descriptor_expression(type, location);
13980}
13981
13982// An expression which evaluates to some characteristic of a type.
13983// This is only used to initialize fields of a type descriptor. Using
13984// a new expression class is slightly inefficient but gives us a good
13985// separation between the frontend and the middle-end with regard to
13986// how types are laid out.
13987
13988class Type_info_expression : public Expression
13989{
13990 public:
13991 Type_info_expression(Type* type, Type_info type_info)
b13c66cd 13992 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
e440a328 13993 type_(type), type_info_(type_info)
13994 { }
13995
13996 protected:
13997 Type*
13998 do_type();
13999
14000 void
14001 do_determine_type(const Type_context*)
14002 { }
14003
14004 Expression*
14005 do_copy()
14006 { return this; }
14007
14008 tree
14009 do_get_tree(Translate_context* context);
14010
d751bb78 14011 void
14012 do_dump_expression(Ast_dump_context*) const;
14013
e440a328 14014 private:
14015 // The type for which we are getting information.
14016 Type* type_;
14017 // What information we want.
14018 Type_info type_info_;
14019};
14020
14021// The type is chosen to match what the type descriptor struct
14022// expects.
14023
14024Type*
14025Type_info_expression::do_type()
14026{
14027 switch (this->type_info_)
14028 {
14029 case TYPE_INFO_SIZE:
14030 return Type::lookup_integer_type("uintptr");
14031 case TYPE_INFO_ALIGNMENT:
14032 case TYPE_INFO_FIELD_ALIGNMENT:
14033 return Type::lookup_integer_type("uint8");
14034 default:
c3e6f413 14035 go_unreachable();
e440a328 14036 }
14037}
14038
14039// Return type information in GENERIC.
14040
14041tree
14042Type_info_expression::do_get_tree(Translate_context* context)
14043{
927a01eb 14044 Btype* btype = this->type_->get_backend(context->gogo());
14045 Gogo* gogo = context->gogo();
14046 size_t val;
14047 switch (this->type_info_)
e440a328 14048 {
927a01eb 14049 case TYPE_INFO_SIZE:
14050 val = gogo->backend()->type_size(btype);
14051 break;
14052 case TYPE_INFO_ALIGNMENT:
14053 val = gogo->backend()->type_alignment(btype);
14054 break;
14055 case TYPE_INFO_FIELD_ALIGNMENT:
14056 val = gogo->backend()->type_field_alignment(btype);
14057 break;
14058 default:
14059 go_unreachable();
e440a328 14060 }
927a01eb 14061 tree val_type_tree = type_to_tree(this->type()->get_backend(gogo));
14062 go_assert(val_type_tree != error_mark_node);
14063 return build_int_cstu(val_type_tree, val);
e440a328 14064}
14065
d751bb78 14066// Dump ast representation for a type info expression.
14067
14068void
14069Type_info_expression::do_dump_expression(
14070 Ast_dump_context* ast_dump_context) const
14071{
14072 ast_dump_context->ostream() << "typeinfo(";
14073 ast_dump_context->dump_type(this->type_);
14074 ast_dump_context->ostream() << ",";
14075 ast_dump_context->ostream() <<
14076 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
14077 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
14078 : this->type_info_ == TYPE_INFO_SIZE ? "size "
14079 : "unknown");
14080 ast_dump_context->ostream() << ")";
14081}
14082
e440a328 14083// Make a type info expression.
14084
14085Expression*
14086Expression::make_type_info(Type* type, Type_info type_info)
14087{
14088 return new Type_info_expression(type, type_info);
14089}
14090
14091// An expression which evaluates to the offset of a field within a
14092// struct. This, like Type_info_expression, q.v., is only used to
14093// initialize fields of a type descriptor.
14094
14095class Struct_field_offset_expression : public Expression
14096{
14097 public:
14098 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
b13c66cd 14099 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
14100 Linemap::predeclared_location()),
e440a328 14101 type_(type), field_(field)
14102 { }
14103
14104 protected:
14105 Type*
14106 do_type()
14107 { return Type::lookup_integer_type("uintptr"); }
14108
14109 void
14110 do_determine_type(const Type_context*)
14111 { }
14112
14113 Expression*
14114 do_copy()
14115 { return this; }
14116
14117 tree
14118 do_get_tree(Translate_context* context);
14119
d751bb78 14120 void
14121 do_dump_expression(Ast_dump_context*) const;
14122
e440a328 14123 private:
14124 // The type of the struct.
14125 Struct_type* type_;
14126 // The field.
14127 const Struct_field* field_;
14128};
14129
14130// Return a struct field offset in GENERIC.
14131
14132tree
14133Struct_field_offset_expression::do_get_tree(Translate_context* context)
14134{
9f0e0513 14135 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
e440a328 14136 if (type_tree == error_mark_node)
14137 return error_mark_node;
14138
9f0e0513 14139 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
c484d925 14140 go_assert(val_type_tree != error_mark_node);
e440a328 14141
14142 const Struct_field_list* fields = this->type_->fields();
14143 tree struct_field_tree = TYPE_FIELDS(type_tree);
14144 Struct_field_list::const_iterator p;
14145 for (p = fields->begin();
14146 p != fields->end();
14147 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
14148 {
c484d925 14149 go_assert(struct_field_tree != NULL_TREE);
e440a328 14150 if (&*p == this->field_)
14151 break;
14152 }
c484d925 14153 go_assert(&*p == this->field_);
e440a328 14154
14155 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
14156 byte_position(struct_field_tree));
14157}
14158
d751bb78 14159// Dump ast representation for a struct field offset expression.
14160
14161void
14162Struct_field_offset_expression::do_dump_expression(
14163 Ast_dump_context* ast_dump_context) const
14164{
14165 ast_dump_context->ostream() << "unsafe.Offsetof(";
2d29d278 14166 ast_dump_context->dump_type(this->type_);
14167 ast_dump_context->ostream() << '.';
14168 ast_dump_context->ostream() <<
14169 Gogo::message_name(this->field_->field_name());
d751bb78 14170 ast_dump_context->ostream() << ")";
14171}
14172
e440a328 14173// Make an expression for a struct field offset.
14174
14175Expression*
14176Expression::make_struct_field_offset(Struct_type* type,
14177 const Struct_field* field)
14178{
14179 return new Struct_field_offset_expression(type, field);
14180}
14181
a9182619 14182// An expression which evaluates to a pointer to the map descriptor of
14183// a map type.
14184
14185class Map_descriptor_expression : public Expression
14186{
14187 public:
b13c66cd 14188 Map_descriptor_expression(Map_type* type, Location location)
a9182619 14189 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
14190 type_(type)
14191 { }
14192
14193 protected:
14194 Type*
14195 do_type()
14196 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14197
14198 void
14199 do_determine_type(const Type_context*)
14200 { }
14201
14202 Expression*
14203 do_copy()
14204 { return this; }
14205
14206 tree
14207 do_get_tree(Translate_context* context)
14208 {
14209 return this->type_->map_descriptor_pointer(context->gogo(),
14210 this->location());
14211 }
14212
d751bb78 14213 void
14214 do_dump_expression(Ast_dump_context*) const;
14215
a9182619 14216 private:
14217 // The type for which this is the descriptor.
14218 Map_type* type_;
14219};
14220
d751bb78 14221// Dump ast representation for a map descriptor expression.
14222
14223void
14224Map_descriptor_expression::do_dump_expression(
14225 Ast_dump_context* ast_dump_context) const
14226{
14227 ast_dump_context->ostream() << "map_descriptor(";
14228 ast_dump_context->dump_type(this->type_);
14229 ast_dump_context->ostream() << ")";
14230}
14231
a9182619 14232// Make a map descriptor expression.
14233
14234Expression*
b13c66cd 14235Expression::make_map_descriptor(Map_type* type, Location location)
a9182619 14236{
14237 return new Map_descriptor_expression(type, location);
14238}
14239
e440a328 14240// An expression which evaluates to the address of an unnamed label.
14241
14242class Label_addr_expression : public Expression
14243{
14244 public:
b13c66cd 14245 Label_addr_expression(Label* label, Location location)
e440a328 14246 : Expression(EXPRESSION_LABEL_ADDR, location),
14247 label_(label)
14248 { }
14249
14250 protected:
14251 Type*
14252 do_type()
14253 { return Type::make_pointer_type(Type::make_void_type()); }
14254
14255 void
14256 do_determine_type(const Type_context*)
14257 { }
14258
14259 Expression*
14260 do_copy()
14261 { return new Label_addr_expression(this->label_, this->location()); }
14262
14263 tree
6e193e6f 14264 do_get_tree(Translate_context* context)
14265 {
e8816003 14266 return expr_to_tree(this->label_->get_addr(context, this->location()));
6e193e6f 14267 }
e440a328 14268
d751bb78 14269 void
14270 do_dump_expression(Ast_dump_context* ast_dump_context) const
14271 { ast_dump_context->ostream() << this->label_->name(); }
14272
e440a328 14273 private:
14274 // The label whose address we are taking.
14275 Label* label_;
14276};
14277
14278// Make an expression for the address of an unnamed label.
14279
14280Expression*
b13c66cd 14281Expression::make_label_addr(Label* label, Location location)
e440a328 14282{
14283 return new Label_addr_expression(label, location);
14284}
14285
14286// Import an expression. This comes at the end in order to see the
14287// various class definitions.
14288
14289Expression*
14290Expression::import_expression(Import* imp)
14291{
14292 int c = imp->peek_char();
14293 if (imp->match_c_string("- ")
14294 || imp->match_c_string("! ")
14295 || imp->match_c_string("^ "))
14296 return Unary_expression::do_import(imp);
14297 else if (c == '(')
14298 return Binary_expression::do_import(imp);
14299 else if (imp->match_c_string("true")
14300 || imp->match_c_string("false"))
14301 return Boolean_expression::do_import(imp);
14302 else if (c == '"')
14303 return String_expression::do_import(imp);
14304 else if (c == '-' || (c >= '0' && c <= '9'))
14305 {
14306 // This handles integers, floats and complex constants.
14307 return Integer_expression::do_import(imp);
14308 }
14309 else if (imp->match_c_string("nil"))
14310 return Nil_expression::do_import(imp);
14311 else if (imp->match_c_string("convert"))
14312 return Type_conversion_expression::do_import(imp);
14313 else
14314 {
14315 error_at(imp->location(), "import error: expected expression");
14316 return Expression::make_error(imp->location());
14317 }
14318}
14319
14320// Class Expression_list.
14321
14322// Traverse the list.
14323
14324int
14325Expression_list::traverse(Traverse* traverse)
14326{
14327 for (Expression_list::iterator p = this->begin();
14328 p != this->end();
14329 ++p)
14330 {
14331 if (*p != NULL)
14332 {
14333 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
14334 return TRAVERSE_EXIT;
14335 }
14336 }
14337 return TRAVERSE_CONTINUE;
14338}
14339
14340// Copy the list.
14341
14342Expression_list*
14343Expression_list::copy()
14344{
14345 Expression_list* ret = new Expression_list();
14346 for (Expression_list::iterator p = this->begin();
14347 p != this->end();
14348 ++p)
14349 {
14350 if (*p == NULL)
14351 ret->push_back(NULL);
14352 else
14353 ret->push_back((*p)->copy());
14354 }
14355 return ret;
14356}
14357
14358// Return whether an expression list has an error expression.
14359
14360bool
14361Expression_list::contains_error() const
14362{
14363 for (Expression_list::const_iterator p = this->begin();
14364 p != this->end();
14365 ++p)
14366 if (*p != NULL && (*p)->is_error_expression())
14367 return true;
14368 return false;
14369}