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