]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/gofrontend/expressions.cc
2012-05-30 John David Anglin <dave.anglin@nrc-cnrc.gc.ca>
[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
ffe743ca 9#include <algorithm>
10
e440a328 11#include <gmp.h>
12
13#ifndef ENABLE_BUILD_WITH_CXX
14extern "C"
15{
16#endif
17
18#include "toplev.h"
19#include "intl.h"
20#include "tree.h"
21#include "gimple.h"
22#include "tree-iterator.h"
23#include "convert.h"
24#include "real.h"
25#include "realmpfr.h"
e440a328 26
27#ifndef ENABLE_BUILD_WITH_CXX
28}
29#endif
30
31#include "go-c.h"
32#include "gogo.h"
33#include "types.h"
34#include "export.h"
35#include "import.h"
36#include "statements.h"
37#include "lex.h"
a9182619 38#include "runtime.h"
6e193e6f 39#include "backend.h"
e440a328 40#include "expressions.h"
d751bb78 41#include "ast-dump.h"
e440a328 42
43// Class Expression.
44
45Expression::Expression(Expression_classification classification,
b13c66cd 46 Location location)
e440a328 47 : classification_(classification), location_(location)
48{
49}
50
51Expression::~Expression()
52{
53}
54
e440a328 55// Traverse the expressions.
56
57int
58Expression::traverse(Expression** pexpr, Traverse* traverse)
59{
60 Expression* expr = *pexpr;
61 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
62 {
63 int t = traverse->expression(pexpr);
64 if (t == TRAVERSE_EXIT)
65 return TRAVERSE_EXIT;
66 else if (t == TRAVERSE_SKIP_COMPONENTS)
67 return TRAVERSE_CONTINUE;
68 }
69 return expr->do_traverse(traverse);
70}
71
72// Traverse subexpressions of this expression.
73
74int
75Expression::traverse_subexpressions(Traverse* traverse)
76{
77 return this->do_traverse(traverse);
78}
79
80// Default implementation for do_traverse for child classes.
81
82int
83Expression::do_traverse(Traverse*)
84{
85 return TRAVERSE_CONTINUE;
86}
87
88// This virtual function is called by the parser if the value of this
a7549a6a 89// expression is being discarded. By default, we give an error.
90// Expressions with side effects override.
e440a328 91
92void
93Expression::do_discarding_value()
94{
a7549a6a 95 this->unused_value_error();
e440a328 96}
97
98// This virtual function is called to export expressions. This will
99// only be used by expressions which may be constant.
100
101void
102Expression::do_export(Export*) const
103{
c3e6f413 104 go_unreachable();
e440a328 105}
106
a7549a6a 107// Give an error saying that the value of the expression is not used.
e440a328 108
109void
a7549a6a 110Expression::unused_value_error()
e440a328 111{
a7549a6a 112 error_at(this->location(), "value computed is not used");
e440a328 113}
114
115// Note that this expression is an error. This is called by children
116// when they discover an error.
117
118void
119Expression::set_is_error()
120{
121 this->classification_ = EXPRESSION_ERROR;
122}
123
124// For children to call to report an error conveniently.
125
126void
127Expression::report_error(const char* msg)
128{
129 error_at(this->location_, "%s", msg);
130 this->set_is_error();
131}
132
133// Set types of variables and constants. This is implemented by the
134// child class.
135
136void
137Expression::determine_type(const Type_context* context)
138{
139 this->do_determine_type(context);
140}
141
142// Set types when there is no context.
143
144void
145Expression::determine_type_no_context()
146{
147 Type_context context;
148 this->do_determine_type(&context);
149}
150
151// Return a tree handling any conversions which must be done during
152// assignment.
153
154tree
155Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
156 Type* rhs_type, tree rhs_tree,
b13c66cd 157 Location location)
e440a328 158{
5c13bd80 159 if (lhs_type->is_error() || rhs_type->is_error())
e440a328 160 return error_mark_node;
161
e440a328 162 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
163 return error_mark_node;
164
165 Gogo* gogo = context->gogo();
166
9f0e0513 167 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 168 if (lhs_type_tree == error_mark_node)
169 return error_mark_node;
170
bb92f513 171 if (lhs_type != rhs_type && lhs_type->interface_type() != NULL)
e440a328 172 {
173 if (rhs_type->interface_type() == NULL)
174 return Expression::convert_type_to_interface(context, lhs_type,
175 rhs_type, rhs_tree,
176 location);
177 else
178 return Expression::convert_interface_to_interface(context, lhs_type,
179 rhs_type, rhs_tree,
180 false, location);
181 }
bb92f513 182 else if (lhs_type != rhs_type && rhs_type->interface_type() != NULL)
e440a328 183 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
184 rhs_tree, location);
411eb89e 185 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
e440a328 186 {
187 // Assigning nil to an open array.
c484d925 188 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
e440a328 189
190 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
191
192 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
193 tree field = TYPE_FIELDS(lhs_type_tree);
c484d925 194 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 195 "__values") == 0);
196 elt->index = field;
197 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
198
199 elt = VEC_quick_push(constructor_elt, init, NULL);
200 field = DECL_CHAIN(field);
c484d925 201 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 202 "__count") == 0);
203 elt->index = field;
204 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
205
206 elt = VEC_quick_push(constructor_elt, init, NULL);
207 field = DECL_CHAIN(field);
c484d925 208 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 209 "__capacity") == 0);
210 elt->index = field;
211 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
212
213 tree val = build_constructor(lhs_type_tree, init);
214 TREE_CONSTANT(val) = 1;
215
216 return val;
217 }
218 else if (rhs_type->is_nil_type())
219 {
220 // The left hand side should be a pointer type at the tree
221 // level.
c484d925 222 go_assert(POINTER_TYPE_P(lhs_type_tree));
e440a328 223 return fold_convert(lhs_type_tree, null_pointer_node);
224 }
225 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
226 {
227 // No conversion is needed.
228 return rhs_tree;
229 }
230 else if (POINTER_TYPE_P(lhs_type_tree)
231 || INTEGRAL_TYPE_P(lhs_type_tree)
232 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
233 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
b13c66cd 234 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
3e785901 235 else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE
236 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
237 || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE
238 && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE))
e440a328 239 {
bb92f513 240 // Avoid confusion from zero sized variables which may be
241 // represented as non-zero-sized.
242 if (int_size_in_bytes(lhs_type_tree) == 0
243 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
244 return rhs_tree;
245
e440a328 246 // This conversion must be permitted by Go, or we wouldn't have
247 // gotten here.
c484d925 248 go_assert(int_size_in_bytes(lhs_type_tree)
bb92f513 249 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
b13c66cd 250 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
251 lhs_type_tree, rhs_tree);
e440a328 252 }
253 else
254 {
c484d925 255 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
e440a328 256 return rhs_tree;
257 }
258}
259
260// Return a tree for a conversion from a non-interface type to an
261// interface type.
262
263tree
264Expression::convert_type_to_interface(Translate_context* context,
265 Type* lhs_type, Type* rhs_type,
b13c66cd 266 tree rhs_tree, Location location)
e440a328 267{
268 Gogo* gogo = context->gogo();
269 Interface_type* lhs_interface_type = lhs_type->interface_type();
270 bool lhs_is_empty = lhs_interface_type->is_empty();
271
272 // Since RHS_TYPE is a static type, we can create the interface
273 // method table at compile time.
274
275 // When setting an interface to nil, we just set both fields to
276 // NULL.
277 if (rhs_type->is_nil_type())
63697958 278 {
279 Btype* lhs_btype = lhs_type->get_backend(gogo);
280 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
281 }
e440a328 282
283 // This should have been checked already.
c484d925 284 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
e440a328 285
9f0e0513 286 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 287 if (lhs_type_tree == error_mark_node)
288 return error_mark_node;
289
290 // An interface is a tuple. If LHS_TYPE is an empty interface type,
291 // then the first field is the type descriptor for RHS_TYPE.
292 // Otherwise it is the interface method table for RHS_TYPE.
293 tree first_field_value;
294 if (lhs_is_empty)
a1d23b41 295 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
e440a328 296 else
297 {
298 // Build the interface method table for this interface and this
299 // object type: a list of function pointers for each interface
300 // method.
301 Named_type* rhs_named_type = rhs_type->named_type();
302 bool is_pointer = false;
303 if (rhs_named_type == NULL)
304 {
305 rhs_named_type = rhs_type->deref()->named_type();
306 is_pointer = true;
307 }
308 tree method_table;
309 if (rhs_named_type == NULL)
310 method_table = null_pointer_node;
311 else
312 method_table =
313 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
314 is_pointer);
b13c66cd 315 first_field_value = fold_convert_loc(location.gcc_location(),
316 const_ptr_type_node, method_table);
e440a328 317 }
84b7d3c6 318 if (first_field_value == error_mark_node)
319 return error_mark_node;
e440a328 320
321 // Start building a constructor for the value we will return.
322
323 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
324
325 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
326 tree field = TYPE_FIELDS(lhs_type_tree);
c484d925 327 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 328 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
329 elt->index = field;
b13c66cd 330 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
331 first_field_value);
e440a328 332
333 elt = VEC_quick_push(constructor_elt, init, NULL);
334 field = DECL_CHAIN(field);
c484d925 335 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
e440a328 336 elt->index = field;
337
338 if (rhs_type->points_to() != NULL)
339 {
340 // We are assigning a pointer to the interface; the interface
341 // holds the pointer itself.
342 elt->value = rhs_tree;
343 return build_constructor(lhs_type_tree, init);
344 }
345
346 // We are assigning a non-pointer value to the interface; the
347 // interface gets a copy of the value in the heap.
348
349 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
350
351 tree space = gogo->allocate_memory(rhs_type, object_size, location);
b13c66cd 352 space = fold_convert_loc(location.gcc_location(),
353 build_pointer_type(TREE_TYPE(rhs_tree)), space);
e440a328 354 space = save_expr(space);
355
b13c66cd 356 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
e440a328 357 TREE_THIS_NOTRAP(ref) = 1;
b13c66cd 358 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
359 void_type_node, ref, rhs_tree);
e440a328 360
b13c66cd 361 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
362 space);
e440a328 363
364 return build2(COMPOUND_EXPR, lhs_type_tree, set,
365 build_constructor(lhs_type_tree, init));
366}
367
368// Return a tree for the type descriptor of RHS_TREE, which has
369// interface type RHS_TYPE. If RHS_TREE is nil the result will be
370// NULL.
371
372tree
373Expression::get_interface_type_descriptor(Translate_context*,
374 Type* rhs_type, tree rhs_tree,
b13c66cd 375 Location location)
e440a328 376{
377 tree rhs_type_tree = TREE_TYPE(rhs_tree);
c484d925 378 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
e440a328 379 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
380 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
381 NULL_TREE);
382 if (rhs_type->interface_type()->is_empty())
383 {
c484d925 384 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
e440a328 385 "__type_descriptor") == 0);
386 return v;
387 }
388
c484d925 389 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
e440a328 390 == 0);
c484d925 391 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
e440a328 392 v = save_expr(v);
b13c66cd 393 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
c484d925 394 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
e440a328 395 tree f = TYPE_FIELDS(TREE_TYPE(v1));
c484d925 396 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
e440a328 397 == 0);
398 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
399
b13c66cd 400 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
401 v, fold_convert_loc(location.gcc_location(),
402 TREE_TYPE(v),
403 null_pointer_node));
404 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
405 null_pointer_node);
406 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
e440a328 407 eq, n, v1);
408}
409
410// Return a tree for the conversion of an interface type to an
411// interface type.
412
413tree
414Expression::convert_interface_to_interface(Translate_context* context,
415 Type *lhs_type, Type *rhs_type,
416 tree rhs_tree, bool for_type_guard,
b13c66cd 417 Location location)
e440a328 418{
419 Gogo* gogo = context->gogo();
420 Interface_type* lhs_interface_type = lhs_type->interface_type();
421 bool lhs_is_empty = lhs_interface_type->is_empty();
422
9f0e0513 423 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 424 if (lhs_type_tree == error_mark_node)
425 return error_mark_node;
426
427 // In the general case this requires runtime examination of the type
428 // method table to match it up with the interface methods.
429
430 // FIXME: If all of the methods in the right hand side interface
431 // also appear in the left hand side interface, then we don't need
432 // to do a runtime check, although we still need to build a new
433 // method table.
434
435 // Get the type descriptor for the right hand side. This will be
436 // NULL for a nil interface.
437
438 if (!DECL_P(rhs_tree))
439 rhs_tree = save_expr(rhs_tree);
440
441 tree rhs_type_descriptor =
442 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
443 location);
444
445 // The result is going to be a two element constructor.
446
447 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
448
449 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
450 tree field = TYPE_FIELDS(lhs_type_tree);
451 elt->index = field;
452
453 if (for_type_guard)
454 {
455 // A type assertion fails when converting a nil interface.
a1d23b41 456 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
457 location);
e440a328 458 static tree assert_interface_decl;
459 tree call = Gogo::call_builtin(&assert_interface_decl,
460 location,
461 "__go_assert_interface",
462 2,
463 ptr_type_node,
464 TREE_TYPE(lhs_type_descriptor),
465 lhs_type_descriptor,
466 TREE_TYPE(rhs_type_descriptor),
467 rhs_type_descriptor);
5fb82b5e 468 if (call == error_mark_node)
469 return error_mark_node;
e440a328 470 // This will panic if the interface conversion fails.
471 TREE_NOTHROW(assert_interface_decl) = 0;
b13c66cd 472 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
473 call);
e440a328 474 }
475 else if (lhs_is_empty)
476 {
477 // A convertion to an empty interface always succeeds, and the
478 // first field is just the type descriptor of the object.
c484d925 479 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
e440a328 480 "__type_descriptor") == 0);
7172c949 481 elt->value = fold_convert_loc(location.gcc_location(),
482 TREE_TYPE(field), rhs_type_descriptor);
e440a328 483 }
484 else
485 {
486 // A conversion to a non-empty interface may fail, but unlike a
487 // type assertion converting nil will always succeed.
c484d925 488 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
e440a328 489 == 0);
a1d23b41 490 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
491 location);
e440a328 492 static tree convert_interface_decl;
493 tree call = Gogo::call_builtin(&convert_interface_decl,
494 location,
495 "__go_convert_interface",
496 2,
497 ptr_type_node,
498 TREE_TYPE(lhs_type_descriptor),
499 lhs_type_descriptor,
500 TREE_TYPE(rhs_type_descriptor),
501 rhs_type_descriptor);
5fb82b5e 502 if (call == error_mark_node)
503 return error_mark_node;
e440a328 504 // This will panic if the interface conversion fails.
505 TREE_NOTHROW(convert_interface_decl) = 0;
b13c66cd 506 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
507 call);
e440a328 508 }
509
510 // The second field is simply the object pointer.
511
512 elt = VEC_quick_push(constructor_elt, init, NULL);
513 field = DECL_CHAIN(field);
c484d925 514 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
e440a328 515 elt->index = field;
516
517 tree rhs_type_tree = TREE_TYPE(rhs_tree);
c484d925 518 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
e440a328 519 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
c484d925 520 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
e440a328 521 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
522 NULL_TREE);
523
524 return build_constructor(lhs_type_tree, init);
525}
526
527// Return a tree for the conversion of an interface type to a
528// non-interface type.
529
530tree
531Expression::convert_interface_to_type(Translate_context* context,
532 Type *lhs_type, Type* rhs_type,
b13c66cd 533 tree rhs_tree, Location location)
e440a328 534{
535 Gogo* gogo = context->gogo();
536 tree rhs_type_tree = TREE_TYPE(rhs_tree);
537
9f0e0513 538 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
e440a328 539 if (lhs_type_tree == error_mark_node)
540 return error_mark_node;
541
542 // Call a function to check that the type is valid. The function
543 // will panic with an appropriate runtime type error if the type is
544 // not valid.
545
a1d23b41 546 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
e440a328 547
548 if (!DECL_P(rhs_tree))
549 rhs_tree = save_expr(rhs_tree);
550
551 tree rhs_type_descriptor =
552 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
553 location);
554
a1d23b41 555 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
556 location);
e440a328 557
558 static tree check_interface_type_decl;
559 tree call = Gogo::call_builtin(&check_interface_type_decl,
560 location,
561 "__go_check_interface_type",
562 3,
563 void_type_node,
564 TREE_TYPE(lhs_type_descriptor),
565 lhs_type_descriptor,
566 TREE_TYPE(rhs_type_descriptor),
567 rhs_type_descriptor,
568 TREE_TYPE(rhs_inter_descriptor),
569 rhs_inter_descriptor);
5fb82b5e 570 if (call == error_mark_node)
571 return error_mark_node;
e440a328 572 // This call will panic if the conversion is invalid.
573 TREE_NOTHROW(check_interface_type_decl) = 0;
574
575 // If the call succeeds, pull out the value.
c484d925 576 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
e440a328 577 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
c484d925 578 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
e440a328 579 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
580 NULL_TREE);
581
582 // If the value is a pointer, then it is the value we want.
583 // Otherwise it points to the value.
584 if (lhs_type->points_to() == NULL)
585 {
b13c66cd 586 val = fold_convert_loc(location.gcc_location(),
587 build_pointer_type(lhs_type_tree), val);
588 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
e440a328 589 }
590
591 return build2(COMPOUND_EXPR, lhs_type_tree, call,
b13c66cd 592 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
e440a328 593}
594
595// Convert an expression to a tree. This is implemented by the child
596// class. Not that it is not in general safe to call this multiple
597// times for a single expression, but that we don't catch such errors.
598
599tree
600Expression::get_tree(Translate_context* context)
601{
602 // The child may have marked this expression as having an error.
603 if (this->classification_ == EXPRESSION_ERROR)
604 return error_mark_node;
605
606 return this->do_get_tree(context);
607}
608
609// Return a tree for VAL in TYPE.
610
611tree
612Expression::integer_constant_tree(mpz_t val, tree type)
613{
614 if (type == error_mark_node)
615 return error_mark_node;
616 else if (TREE_CODE(type) == INTEGER_TYPE)
617 return double_int_to_tree(type,
618 mpz_get_double_int(type, val, true));
619 else if (TREE_CODE(type) == REAL_TYPE)
620 {
621 mpfr_t fval;
622 mpfr_init_set_z(fval, val, GMP_RNDN);
623 tree ret = Expression::float_constant_tree(fval, type);
624 mpfr_clear(fval);
625 return ret;
626 }
627 else if (TREE_CODE(type) == COMPLEX_TYPE)
628 {
629 mpfr_t fval;
630 mpfr_init_set_z(fval, val, GMP_RNDN);
631 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
632 mpfr_clear(fval);
633 tree imag = build_real_from_int_cst(TREE_TYPE(type),
634 integer_zero_node);
635 return build_complex(type, real, imag);
636 }
637 else
c3e6f413 638 go_unreachable();
e440a328 639}
640
641// Return a tree for VAL in TYPE.
642
643tree
644Expression::float_constant_tree(mpfr_t val, tree type)
645{
646 if (type == error_mark_node)
647 return error_mark_node;
648 else if (TREE_CODE(type) == INTEGER_TYPE)
649 {
650 mpz_t ival;
651 mpz_init(ival);
652 mpfr_get_z(ival, val, GMP_RNDN);
653 tree ret = Expression::integer_constant_tree(ival, type);
654 mpz_clear(ival);
655 return ret;
656 }
657 else if (TREE_CODE(type) == REAL_TYPE)
658 {
659 REAL_VALUE_TYPE r1;
660 real_from_mpfr(&r1, val, type, GMP_RNDN);
661 REAL_VALUE_TYPE r2;
662 real_convert(&r2, TYPE_MODE(type), &r1);
663 return build_real(type, r2);
664 }
665 else if (TREE_CODE(type) == COMPLEX_TYPE)
666 {
667 REAL_VALUE_TYPE r1;
668 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
669 REAL_VALUE_TYPE r2;
670 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
671 tree imag = build_real_from_int_cst(TREE_TYPE(type),
672 integer_zero_node);
673 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
674 }
675 else
c3e6f413 676 go_unreachable();
e440a328 677}
678
679// Return a tree for REAL/IMAG in TYPE.
680
681tree
682Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
683{
f690b0bb 684 if (type == error_mark_node)
685 return error_mark_node;
686 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
687 return Expression::float_constant_tree(real, type);
688 else if (TREE_CODE(type) == COMPLEX_TYPE)
e440a328 689 {
690 REAL_VALUE_TYPE r1;
691 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
692 REAL_VALUE_TYPE r2;
693 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
694
695 REAL_VALUE_TYPE r3;
696 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
697 REAL_VALUE_TYPE r4;
698 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
699
700 return build_complex(type, build_real(TREE_TYPE(type), r2),
701 build_real(TREE_TYPE(type), r4));
702 }
703 else
c3e6f413 704 go_unreachable();
e440a328 705}
706
707// Return a tree which evaluates to true if VAL, of arbitrary integer
708// type, is negative or is more than the maximum value of BOUND_TYPE.
709// If SOFAR is not NULL, it is or'red into the result. The return
710// value may be NULL if SOFAR is NULL.
711
712tree
713Expression::check_bounds(tree val, tree bound_type, tree sofar,
b13c66cd 714 Location loc)
e440a328 715{
716 tree val_type = TREE_TYPE(val);
717 tree ret = NULL_TREE;
718
719 if (!TYPE_UNSIGNED(val_type))
720 {
b13c66cd 721 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
e440a328 722 build_int_cst(val_type, 0));
723 if (ret == boolean_false_node)
724 ret = NULL_TREE;
725 }
726
c3068ac0 727 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
728 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
729 go_assert(val_type_size != -1 && bound_type_size != -1);
730 if (val_type_size > bound_type_size
731 || (val_type_size == bound_type_size
732 && TYPE_UNSIGNED(val_type)
733 && !TYPE_UNSIGNED(bound_type)))
e440a328 734 {
735 tree max = TYPE_MAX_VALUE(bound_type);
b13c66cd 736 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
737 val, fold_convert_loc(loc.gcc_location(),
738 val_type, max));
e440a328 739 if (big == boolean_false_node)
740 ;
741 else if (ret == NULL_TREE)
742 ret = big;
743 else
b13c66cd 744 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
745 boolean_type_node, ret, big);
e440a328 746 }
747
748 if (ret == NULL_TREE)
749 return sofar;
750 else if (sofar == NULL_TREE)
751 return ret;
752 else
b13c66cd 753 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
e440a328 754 sofar, ret);
755}
756
d751bb78 757void
758Expression::dump_expression(Ast_dump_context* ast_dump_context) const
759{
760 this->do_dump_expression(ast_dump_context);
761}
762
e440a328 763// Error expressions. This are used to avoid cascading errors.
764
765class Error_expression : public Expression
766{
767 public:
b13c66cd 768 Error_expression(Location location)
e440a328 769 : Expression(EXPRESSION_ERROR, location)
770 { }
771
772 protected:
773 bool
774 do_is_constant() const
775 { return true; }
776
777 bool
0c77715b 778 do_numeric_constant_value(Numeric_constant* nc) const
e440a328 779 {
0c77715b 780 nc->set_unsigned_long(NULL, 0);
e440a328 781 return true;
782 }
783
784 void
785 do_discarding_value()
786 { }
787
788 Type*
789 do_type()
790 { return Type::make_error_type(); }
791
792 void
793 do_determine_type(const Type_context*)
794 { }
795
796 Expression*
797 do_copy()
798 { return this; }
799
800 bool
801 do_is_addressable() const
802 { return true; }
803
804 tree
805 do_get_tree(Translate_context*)
806 { return error_mark_node; }
d751bb78 807
808 void
809 do_dump_expression(Ast_dump_context*) const;
e440a328 810};
811
d751bb78 812// Dump the ast representation for an error expression to a dump context.
813
814void
815Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
816{
817 ast_dump_context->ostream() << "_Error_" ;
818}
819
e440a328 820Expression*
b13c66cd 821Expression::make_error(Location location)
e440a328 822{
823 return new Error_expression(location);
824}
825
826// An expression which is really a type. This is used during parsing.
827// It is an error if these survive after lowering.
828
829class
830Type_expression : public Expression
831{
832 public:
b13c66cd 833 Type_expression(Type* type, Location location)
e440a328 834 : Expression(EXPRESSION_TYPE, location),
835 type_(type)
836 { }
837
838 protected:
839 int
840 do_traverse(Traverse* traverse)
841 { return Type::traverse(this->type_, traverse); }
842
843 Type*
844 do_type()
845 { return this->type_; }
846
847 void
848 do_determine_type(const Type_context*)
849 { }
850
851 void
852 do_check_types(Gogo*)
853 { this->report_error(_("invalid use of type")); }
854
855 Expression*
856 do_copy()
857 { return this; }
858
859 tree
860 do_get_tree(Translate_context*)
c3e6f413 861 { go_unreachable(); }
e440a328 862
d751bb78 863 void do_dump_expression(Ast_dump_context*) const;
864
e440a328 865 private:
866 // The type which we are representing as an expression.
867 Type* type_;
868};
869
d751bb78 870void
871Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
872{
873 ast_dump_context->dump_type(this->type_);
874}
875
e440a328 876Expression*
b13c66cd 877Expression::make_type(Type* type, Location location)
e440a328 878{
879 return new Type_expression(type, location);
880}
881
e03bdf36 882// Class Parser_expression.
883
884Type*
885Parser_expression::do_type()
886{
887 // We should never really ask for the type of a Parser_expression.
888 // However, it can happen, at least when we have an invalid const
889 // whose initializer refers to the const itself. In that case we
890 // may ask for the type when lowering the const itself.
c484d925 891 go_assert(saw_errors());
e03bdf36 892 return Type::make_error_type();
893}
894
e440a328 895// Class Var_expression.
896
897// Lower a variable expression. Here we just make sure that the
898// initialization expression of the variable has been lowered. This
899// ensures that we will be able to determine the type of the variable
900// if necessary.
901
902Expression*
ceeb4318 903Var_expression::do_lower(Gogo* gogo, Named_object* function,
904 Statement_inserter* inserter, int)
e440a328 905{
906 if (this->variable_->is_variable())
907 {
908 Variable* var = this->variable_->var_value();
909 // This is either a local variable or a global variable. A
910 // reference to a variable which is local to an enclosing
911 // function will be a reference to a field in a closure.
912 if (var->is_global())
ceeb4318 913 {
914 function = NULL;
915 inserter = NULL;
916 }
917 var->lower_init_expression(gogo, function, inserter);
e440a328 918 }
919 return this;
920}
921
e440a328 922// Return the type of a reference to a variable.
923
924Type*
925Var_expression::do_type()
926{
927 if (this->variable_->is_variable())
928 return this->variable_->var_value()->type();
929 else if (this->variable_->is_result_variable())
930 return this->variable_->result_var_value()->type();
931 else
c3e6f413 932 go_unreachable();
e440a328 933}
934
0ab09e06 935// Determine the type of a reference to a variable.
936
937void
938Var_expression::do_determine_type(const Type_context*)
939{
940 if (this->variable_->is_variable())
941 this->variable_->var_value()->determine_type();
942}
943
e440a328 944// Something takes the address of this variable. This means that we
945// may want to move the variable onto the heap.
946
947void
948Var_expression::do_address_taken(bool escapes)
949{
950 if (!escapes)
f325319b 951 {
952 if (this->variable_->is_variable())
953 this->variable_->var_value()->set_non_escaping_address_taken();
954 else if (this->variable_->is_result_variable())
955 this->variable_->result_var_value()->set_non_escaping_address_taken();
956 else
957 go_unreachable();
958 }
e440a328 959 else
f325319b 960 {
961 if (this->variable_->is_variable())
962 this->variable_->var_value()->set_address_taken();
963 else if (this->variable_->is_result_variable())
964 this->variable_->result_var_value()->set_address_taken();
965 else
966 go_unreachable();
967 }
e440a328 968}
969
970// Get the tree for a reference to a variable.
971
972tree
973Var_expression::do_get_tree(Translate_context* context)
974{
fe2f84cf 975 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
976 context->function());
977 tree ret = var_to_tree(bvar);
978 if (ret == error_mark_node)
979 return error_mark_node;
980 bool is_in_heap;
981 if (this->variable_->is_variable())
982 is_in_heap = this->variable_->var_value()->is_in_heap();
983 else if (this->variable_->is_result_variable())
984 is_in_heap = this->variable_->result_var_value()->is_in_heap();
985 else
c3e6f413 986 go_unreachable();
fe2f84cf 987 if (is_in_heap)
988 {
b13c66cd 989 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
fe2f84cf 990 TREE_THIS_NOTRAP(ret) = 1;
991 }
992 return ret;
e440a328 993}
994
d751bb78 995// Ast dump for variable expression.
996
997void
998Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
999{
1000 ast_dump_context->ostream() << this->variable_->name() ;
1001}
1002
e440a328 1003// Make a reference to a variable in an expression.
1004
1005Expression*
b13c66cd 1006Expression::make_var_reference(Named_object* var, Location location)
e440a328 1007{
1008 if (var->is_sink())
1009 return Expression::make_sink(location);
1010
1011 // FIXME: Creating a new object for each reference to a variable is
1012 // wasteful.
1013 return new Var_expression(var, location);
1014}
1015
1016// Class Temporary_reference_expression.
1017
1018// The type.
1019
1020Type*
1021Temporary_reference_expression::do_type()
1022{
1023 return this->statement_->type();
1024}
1025
1026// Called if something takes the address of this temporary variable.
1027// We never have to move temporary variables to the heap, but we do
1028// need to know that they must live in the stack rather than in a
1029// register.
1030
1031void
1032Temporary_reference_expression::do_address_taken(bool)
1033{
1034 this->statement_->set_is_address_taken();
1035}
1036
1037// Get a tree referring to the variable.
1038
1039tree
eefc1ed3 1040Temporary_reference_expression::do_get_tree(Translate_context* context)
e440a328 1041{
eefc1ed3 1042 Bvariable* bvar = this->statement_->get_backend_variable(context);
1043
1044 // The gcc backend can't represent the same set of recursive types
1045 // that the Go frontend can. In some cases this means that a
1046 // temporary variable won't have the right backend type. Correct
1047 // that here by adding a type cast. We need to use base() to push
1048 // the circularity down one level.
1049 tree ret = var_to_tree(bvar);
ceeb4318 1050 if (!this->is_lvalue_
1051 && POINTER_TYPE_P(TREE_TYPE(ret))
1052 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
eefc1ed3 1053 {
9f0e0513 1054 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1055 tree type_tree = type_to_tree(type_btype);
b13c66cd 1056 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
eefc1ed3 1057 }
1058 return ret;
e440a328 1059}
1060
d751bb78 1061// Ast dump for temporary reference.
1062
1063void
1064Temporary_reference_expression::do_dump_expression(
1065 Ast_dump_context* ast_dump_context) const
1066{
1067 ast_dump_context->dump_temp_variable_name(this->statement_);
1068}
1069
e440a328 1070// Make a reference to a temporary variable.
1071
ceeb4318 1072Temporary_reference_expression*
e440a328 1073Expression::make_temporary_reference(Temporary_statement* statement,
b13c66cd 1074 Location location)
e440a328 1075{
1076 return new Temporary_reference_expression(statement, location);
1077}
1078
e9d3367e 1079// Class Set_and_use_temporary_expression.
1080
1081// Return the type.
1082
1083Type*
1084Set_and_use_temporary_expression::do_type()
1085{
1086 return this->statement_->type();
1087}
1088
1089// Take the address.
1090
1091void
1092Set_and_use_temporary_expression::do_address_taken(bool)
1093{
1094 this->statement_->set_is_address_taken();
1095}
1096
1097// Return the backend representation.
1098
1099tree
1100Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1101{
1102 Bvariable* bvar = this->statement_->get_backend_variable(context);
1103 tree var_tree = var_to_tree(bvar);
1104 tree expr_tree = this->expr_->get_tree(context);
1105 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1106 return error_mark_node;
1107 Location loc = this->location();
1108 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1109 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1110 var_tree, expr_tree),
1111 var_tree);
1112}
1113
1114// Dump.
1115
1116void
1117Set_and_use_temporary_expression::do_dump_expression(
1118 Ast_dump_context* ast_dump_context) const
1119{
1120 ast_dump_context->ostream() << '(';
1121 ast_dump_context->dump_temp_variable_name(this->statement_);
1122 ast_dump_context->ostream() << " = ";
1123 this->expr_->dump_expression(ast_dump_context);
1124 ast_dump_context->ostream() << ')';
1125}
1126
1127// Make a set-and-use temporary.
1128
1129Set_and_use_temporary_expression*
1130Expression::make_set_and_use_temporary(Temporary_statement* statement,
1131 Expression* expr, Location location)
1132{
1133 return new Set_and_use_temporary_expression(statement, expr, location);
1134}
1135
e440a328 1136// A sink expression--a use of the blank identifier _.
1137
1138class Sink_expression : public Expression
1139{
1140 public:
b13c66cd 1141 Sink_expression(Location location)
e440a328 1142 : Expression(EXPRESSION_SINK, location),
1143 type_(NULL), var_(NULL_TREE)
1144 { }
1145
1146 protected:
1147 void
1148 do_discarding_value()
1149 { }
1150
1151 Type*
1152 do_type();
1153
1154 void
1155 do_determine_type(const Type_context*);
1156
1157 Expression*
1158 do_copy()
1159 { return new Sink_expression(this->location()); }
1160
1161 tree
1162 do_get_tree(Translate_context*);
1163
d751bb78 1164 void
1165 do_dump_expression(Ast_dump_context*) const;
1166
e440a328 1167 private:
1168 // The type of this sink variable.
1169 Type* type_;
1170 // The temporary variable we generate.
1171 tree var_;
1172};
1173
1174// Return the type of a sink expression.
1175
1176Type*
1177Sink_expression::do_type()
1178{
1179 if (this->type_ == NULL)
1180 return Type::make_sink_type();
1181 return this->type_;
1182}
1183
1184// Determine the type of a sink expression.
1185
1186void
1187Sink_expression::do_determine_type(const Type_context* context)
1188{
1189 if (context->type != NULL)
1190 this->type_ = context->type;
1191}
1192
1193// Return a temporary variable for a sink expression. This will
1194// presumably be a write-only variable which the middle-end will drop.
1195
1196tree
1197Sink_expression::do_get_tree(Translate_context* context)
1198{
1199 if (this->var_ == NULL_TREE)
1200 {
c484d925 1201 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
9f0e0513 1202 Btype* bt = this->type_->get_backend(context->gogo());
1203 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
e440a328 1204 }
1205 return this->var_;
1206}
1207
d751bb78 1208// Ast dump for sink expression.
1209
1210void
1211Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1212{
1213 ast_dump_context->ostream() << "_" ;
1214}
1215
e440a328 1216// Make a sink expression.
1217
1218Expression*
b13c66cd 1219Expression::make_sink(Location location)
e440a328 1220{
1221 return new Sink_expression(location);
1222}
1223
1224// Class Func_expression.
1225
1226// FIXME: Can a function expression appear in a constant expression?
1227// The value is unchanging. Initializing a constant to the address of
1228// a function seems like it could work, though there might be little
1229// point to it.
1230
e440a328 1231// Traversal.
1232
1233int
1234Func_expression::do_traverse(Traverse* traverse)
1235{
1236 return (this->closure_ == NULL
1237 ? TRAVERSE_CONTINUE
1238 : Expression::traverse(&this->closure_, traverse));
1239}
1240
1241// Return the type of a function expression.
1242
1243Type*
1244Func_expression::do_type()
1245{
1246 if (this->function_->is_function())
1247 return this->function_->func_value()->type();
1248 else if (this->function_->is_function_declaration())
1249 return this->function_->func_declaration_value()->type();
1250 else
c3e6f413 1251 go_unreachable();
e440a328 1252}
1253
1254// Get the tree for a function expression without evaluating the
1255// closure.
1256
1257tree
1258Func_expression::get_tree_without_closure(Gogo* gogo)
1259{
1260 Function_type* fntype;
1261 if (this->function_->is_function())
1262 fntype = this->function_->func_value()->type();
1263 else if (this->function_->is_function_declaration())
1264 fntype = this->function_->func_declaration_value()->type();
1265 else
c3e6f413 1266 go_unreachable();
e440a328 1267
1268 // Builtin functions are handled specially by Call_expression. We
1269 // can't take their address.
1270 if (fntype->is_builtin())
1271 {
cb0e02f3 1272 error_at(this->location(),
1273 "invalid use of special builtin function %qs; must be called",
e440a328 1274 this->function_->name().c_str());
1275 return error_mark_node;
1276 }
1277
1278 Named_object* no = this->function_;
9d6f3721 1279
1280 tree id = no->get_id(gogo);
1281 if (id == error_mark_node)
1282 return error_mark_node;
1283
e440a328 1284 tree fndecl;
1285 if (no->is_function())
1286 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1287 else if (no->is_function_declaration())
1288 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1289 else
c3e6f413 1290 go_unreachable();
e440a328 1291
9d6f3721 1292 if (fndecl == error_mark_node)
1293 return error_mark_node;
1294
b13c66cd 1295 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
e440a328 1296}
1297
1298// Get the tree for a function expression. This is used when we take
1299// the address of a function rather than simply calling it. If the
1300// function has a closure, we must use a trampoline.
1301
1302tree
1303Func_expression::do_get_tree(Translate_context* context)
1304{
1305 Gogo* gogo = context->gogo();
1306
1307 tree fnaddr = this->get_tree_without_closure(gogo);
1308 if (fnaddr == error_mark_node)
1309 return error_mark_node;
1310
c484d925 1311 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
e440a328 1312 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1313 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1314
2010c17a 1315 // If there is no closure, that is all have to do.
1316 if (this->closure_ == NULL)
1317 return fnaddr;
e440a328 1318
2010c17a 1319 go_assert(this->function_->func_value()->enclosing() != NULL);
1320
1321 // Get the value of the closure. This will be a pointer to space
1322 // allocated on the heap.
1323 tree closure_tree = this->closure_->get_tree(context);
1324 if (closure_tree == error_mark_node)
1325 return error_mark_node;
1326 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
e440a328 1327
1328 // Now we need to build some code on the heap. This code will load
1329 // the static chain pointer with the closure and then jump to the
1330 // body of the function. The normal gcc approach is to build the
1331 // code on the stack. Unfortunately we can not do that, as Go
1332 // permits us to return the function pointer.
1333
1334 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1335}
1336
d751bb78 1337// Ast dump for function.
1338
1339void
1340Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1341{
8b1c301d 1342 ast_dump_context->ostream() << this->function_->name();
1343 if (this->closure_ != NULL)
1344 {
1345 ast_dump_context->ostream() << " {closure = ";
1346 this->closure_->dump_expression(ast_dump_context);
1347 ast_dump_context->ostream() << "}";
1348 }
d751bb78 1349}
1350
e440a328 1351// Make a reference to a function in an expression.
1352
1353Expression*
1354Expression::make_func_reference(Named_object* function, Expression* closure,
b13c66cd 1355 Location location)
e440a328 1356{
1357 return new Func_expression(function, closure, location);
1358}
1359
1360// Class Unknown_expression.
1361
1362// Return the name of an unknown expression.
1363
1364const std::string&
1365Unknown_expression::name() const
1366{
1367 return this->named_object_->name();
1368}
1369
1370// Lower a reference to an unknown name.
1371
1372Expression*
ceeb4318 1373Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 1374{
b13c66cd 1375 Location location = this->location();
e440a328 1376 Named_object* no = this->named_object_;
deded542 1377 Named_object* real;
1378 if (!no->is_unknown())
1379 real = no;
1380 else
e440a328 1381 {
deded542 1382 real = no->unknown_value()->real_named_object();
1383 if (real == NULL)
1384 {
1385 if (this->is_composite_literal_key_)
1386 return this;
acf8e158 1387 if (!this->no_error_message_)
1388 error_at(location, "reference to undefined name %qs",
1389 this->named_object_->message_name().c_str());
deded542 1390 return Expression::make_error(location);
1391 }
e440a328 1392 }
1393 switch (real->classification())
1394 {
1395 case Named_object::NAMED_OBJECT_CONST:
1396 return Expression::make_const_reference(real, location);
1397 case Named_object::NAMED_OBJECT_TYPE:
1398 return Expression::make_type(real->type_value(), location);
1399 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1400 if (this->is_composite_literal_key_)
1401 return this;
acf8e158 1402 if (!this->no_error_message_)
1403 error_at(location, "reference to undefined type %qs",
1404 real->message_name().c_str());
e440a328 1405 return Expression::make_error(location);
1406 case Named_object::NAMED_OBJECT_VAR:
7d834090 1407 real->var_value()->set_is_used();
e440a328 1408 return Expression::make_var_reference(real, location);
1409 case Named_object::NAMED_OBJECT_FUNC:
1410 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1411 return Expression::make_func_reference(real, NULL, location);
1412 case Named_object::NAMED_OBJECT_PACKAGE:
1413 if (this->is_composite_literal_key_)
1414 return this;
acf8e158 1415 if (!this->no_error_message_)
1416 error_at(location, "unexpected reference to package");
e440a328 1417 return Expression::make_error(location);
1418 default:
c3e6f413 1419 go_unreachable();
e440a328 1420 }
1421}
1422
d751bb78 1423// Dump the ast representation for an unknown expression to a dump context.
1424
1425void
1426Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1427{
1428 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1429 << ")";
d751bb78 1430}
1431
e440a328 1432// Make a reference to an unknown name.
1433
acf8e158 1434Unknown_expression*
b13c66cd 1435Expression::make_unknown_reference(Named_object* no, Location location)
e440a328 1436{
e440a328 1437 return new Unknown_expression(no, location);
1438}
1439
1440// A boolean expression.
1441
1442class Boolean_expression : public Expression
1443{
1444 public:
b13c66cd 1445 Boolean_expression(bool val, Location location)
e440a328 1446 : Expression(EXPRESSION_BOOLEAN, location),
1447 val_(val), type_(NULL)
1448 { }
1449
1450 static Expression*
1451 do_import(Import*);
1452
1453 protected:
1454 bool
1455 do_is_constant() const
1456 { return true; }
1457
1458 Type*
1459 do_type();
1460
1461 void
1462 do_determine_type(const Type_context*);
1463
1464 Expression*
1465 do_copy()
1466 { return this; }
1467
1468 tree
1469 do_get_tree(Translate_context*)
1470 { return this->val_ ? boolean_true_node : boolean_false_node; }
1471
1472 void
1473 do_export(Export* exp) const
1474 { exp->write_c_string(this->val_ ? "true" : "false"); }
1475
d751bb78 1476 void
1477 do_dump_expression(Ast_dump_context* ast_dump_context) const
1478 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1479
e440a328 1480 private:
1481 // The constant.
1482 bool val_;
1483 // The type as determined by context.
1484 Type* type_;
1485};
1486
1487// Get the type.
1488
1489Type*
1490Boolean_expression::do_type()
1491{
1492 if (this->type_ == NULL)
1493 this->type_ = Type::make_boolean_type();
1494 return this->type_;
1495}
1496
1497// Set the type from the context.
1498
1499void
1500Boolean_expression::do_determine_type(const Type_context* context)
1501{
1502 if (this->type_ != NULL && !this->type_->is_abstract())
1503 ;
1504 else if (context->type != NULL && context->type->is_boolean_type())
1505 this->type_ = context->type;
1506 else if (!context->may_be_abstract)
1507 this->type_ = Type::lookup_bool_type();
1508}
1509
1510// Import a boolean constant.
1511
1512Expression*
1513Boolean_expression::do_import(Import* imp)
1514{
1515 if (imp->peek_char() == 't')
1516 {
1517 imp->require_c_string("true");
1518 return Expression::make_boolean(true, imp->location());
1519 }
1520 else
1521 {
1522 imp->require_c_string("false");
1523 return Expression::make_boolean(false, imp->location());
1524 }
1525}
1526
1527// Make a boolean expression.
1528
1529Expression*
b13c66cd 1530Expression::make_boolean(bool val, Location location)
e440a328 1531{
1532 return new Boolean_expression(val, location);
1533}
1534
1535// Class String_expression.
1536
1537// Get the type.
1538
1539Type*
1540String_expression::do_type()
1541{
1542 if (this->type_ == NULL)
1543 this->type_ = Type::make_string_type();
1544 return this->type_;
1545}
1546
1547// Set the type from the context.
1548
1549void
1550String_expression::do_determine_type(const Type_context* context)
1551{
1552 if (this->type_ != NULL && !this->type_->is_abstract())
1553 ;
1554 else if (context->type != NULL && context->type->is_string_type())
1555 this->type_ = context->type;
1556 else if (!context->may_be_abstract)
1557 this->type_ = Type::lookup_string_type();
1558}
1559
1560// Build a string constant.
1561
1562tree
1563String_expression::do_get_tree(Translate_context* context)
1564{
1565 return context->gogo()->go_string_constant_tree(this->val_);
1566}
1567
8b1c301d 1568 // Write string literal to string dump.
e440a328 1569
1570void
8b1c301d 1571String_expression::export_string(String_dump* exp,
1572 const String_expression* str)
e440a328 1573{
1574 std::string s;
8b1c301d 1575 s.reserve(str->val_.length() * 4 + 2);
e440a328 1576 s += '"';
8b1c301d 1577 for (std::string::const_iterator p = str->val_.begin();
1578 p != str->val_.end();
e440a328 1579 ++p)
1580 {
1581 if (*p == '\\' || *p == '"')
1582 {
1583 s += '\\';
1584 s += *p;
1585 }
1586 else if (*p >= 0x20 && *p < 0x7f)
1587 s += *p;
1588 else if (*p == '\n')
1589 s += "\\n";
1590 else if (*p == '\t')
1591 s += "\\t";
1592 else
1593 {
1594 s += "\\x";
1595 unsigned char c = *p;
1596 unsigned int dig = c >> 4;
1597 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1598 dig = c & 0xf;
1599 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1600 }
1601 }
1602 s += '"';
1603 exp->write_string(s);
1604}
1605
8b1c301d 1606// Export a string expression.
1607
1608void
1609String_expression::do_export(Export* exp) const
1610{
1611 String_expression::export_string(exp, this);
1612}
1613
e440a328 1614// Import a string expression.
1615
1616Expression*
1617String_expression::do_import(Import* imp)
1618{
1619 imp->require_c_string("\"");
1620 std::string val;
1621 while (true)
1622 {
1623 int c = imp->get_char();
1624 if (c == '"' || c == -1)
1625 break;
1626 if (c != '\\')
1627 val += static_cast<char>(c);
1628 else
1629 {
1630 c = imp->get_char();
1631 if (c == '\\' || c == '"')
1632 val += static_cast<char>(c);
1633 else if (c == 'n')
1634 val += '\n';
1635 else if (c == 't')
1636 val += '\t';
1637 else if (c == 'x')
1638 {
1639 c = imp->get_char();
1640 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1641 c = imp->get_char();
1642 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1643 char v = (vh << 4) | vl;
1644 val += v;
1645 }
1646 else
1647 {
1648 error_at(imp->location(), "bad string constant");
1649 return Expression::make_error(imp->location());
1650 }
1651 }
1652 }
1653 return Expression::make_string(val, imp->location());
1654}
1655
d751bb78 1656// Ast dump for string expression.
1657
1658void
1659String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1660{
8b1c301d 1661 String_expression::export_string(ast_dump_context, this);
d751bb78 1662}
1663
e440a328 1664// Make a string expression.
1665
1666Expression*
b13c66cd 1667Expression::make_string(const std::string& val, Location location)
e440a328 1668{
1669 return new String_expression(val, location);
1670}
1671
1672// Make an integer expression.
1673
1674class Integer_expression : public Expression
1675{
1676 public:
5d4b8566 1677 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1678 Location location)
e440a328 1679 : Expression(EXPRESSION_INTEGER, location),
5d4b8566 1680 type_(type), is_character_constant_(is_character_constant)
e440a328 1681 { mpz_init_set(this->val_, *val); }
1682
1683 static Expression*
1684 do_import(Import*);
1685
8b1c301d 1686 // Write VAL to string dump.
e440a328 1687 static void
8b1c301d 1688 export_integer(String_dump* exp, const mpz_t val);
e440a328 1689
d751bb78 1690 // Write VAL to dump context.
1691 static void
1692 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1693
e440a328 1694 protected:
1695 bool
1696 do_is_constant() const
1697 { return true; }
1698
1699 bool
0c77715b 1700 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 1701
1702 Type*
1703 do_type();
1704
1705 void
1706 do_determine_type(const Type_context* context);
1707
1708 void
1709 do_check_types(Gogo*);
1710
1711 tree
1712 do_get_tree(Translate_context*);
1713
1714 Expression*
1715 do_copy()
5d4b8566 1716 {
1717 if (this->is_character_constant_)
1718 return Expression::make_character(&this->val_, this->type_,
1719 this->location());
1720 else
1721 return Expression::make_integer(&this->val_, this->type_,
1722 this->location());
1723 }
e440a328 1724
1725 void
1726 do_export(Export*) const;
1727
d751bb78 1728 void
1729 do_dump_expression(Ast_dump_context*) const;
1730
e440a328 1731 private:
1732 // The integer value.
1733 mpz_t val_;
1734 // The type so far.
1735 Type* type_;
5d4b8566 1736 // Whether this is a character constant.
1737 bool is_character_constant_;
e440a328 1738};
1739
0c77715b 1740// Return a numeric constant for this expression. We have to mark
1741// this as a character when appropriate.
e440a328 1742
1743bool
0c77715b 1744Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 1745{
0c77715b 1746 if (this->is_character_constant_)
1747 nc->set_rune(this->type_, this->val_);
1748 else
1749 nc->set_int(this->type_, this->val_);
e440a328 1750 return true;
1751}
1752
1753// Return the current type. If we haven't set the type yet, we return
1754// an abstract integer type.
1755
1756Type*
1757Integer_expression::do_type()
1758{
1759 if (this->type_ == NULL)
5d4b8566 1760 {
1761 if (this->is_character_constant_)
1762 this->type_ = Type::make_abstract_character_type();
1763 else
1764 this->type_ = Type::make_abstract_integer_type();
1765 }
e440a328 1766 return this->type_;
1767}
1768
1769// Set the type of the integer value. Here we may switch from an
1770// abstract type to a real type.
1771
1772void
1773Integer_expression::do_determine_type(const Type_context* context)
1774{
1775 if (this->type_ != NULL && !this->type_->is_abstract())
1776 ;
0c77715b 1777 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 1778 this->type_ = context->type;
1779 else if (!context->may_be_abstract)
5d4b8566 1780 {
1781 if (this->is_character_constant_)
1782 this->type_ = Type::lookup_integer_type("int32");
1783 else
1784 this->type_ = Type::lookup_integer_type("int");
1785 }
e440a328 1786}
1787
e440a328 1788// Check the type of an integer constant.
1789
1790void
1791Integer_expression::do_check_types(Gogo*)
1792{
0c77715b 1793 Type* type = this->type_;
1794 if (type == NULL)
e440a328 1795 return;
0c77715b 1796 Numeric_constant nc;
1797 if (this->is_character_constant_)
1798 nc.set_rune(NULL, this->val_);
1799 else
1800 nc.set_int(NULL, this->val_);
1801 if (!nc.set_type(type, true, this->location()))
e440a328 1802 this->set_is_error();
1803}
1804
1805// Get a tree for an integer constant.
1806
1807tree
1808Integer_expression::do_get_tree(Translate_context* context)
1809{
1810 Gogo* gogo = context->gogo();
1811 tree type;
1812 if (this->type_ != NULL && !this->type_->is_abstract())
9f0e0513 1813 type = type_to_tree(this->type_->get_backend(gogo));
e440a328 1814 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1815 {
1816 // We are converting to an abstract floating point type.
9f0e0513 1817 Type* ftype = Type::lookup_float_type("float64");
1818 type = type_to_tree(ftype->get_backend(gogo));
e440a328 1819 }
1820 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1821 {
1822 // We are converting to an abstract complex type.
9f0e0513 1823 Type* ctype = Type::lookup_complex_type("complex128");
1824 type = type_to_tree(ctype->get_backend(gogo));
e440a328 1825 }
1826 else
1827 {
1828 // If we still have an abstract type here, then this is being
1829 // used in a constant expression which didn't get reduced for
1830 // some reason. Use a type which will fit the value. We use <,
1831 // not <=, because we need an extra bit for the sign bit.
1832 int bits = mpz_sizeinbase(this->val_, 2);
1833 if (bits < INT_TYPE_SIZE)
9f0e0513 1834 {
1835 Type* t = Type::lookup_integer_type("int");
1836 type = type_to_tree(t->get_backend(gogo));
1837 }
e440a328 1838 else if (bits < 64)
9f0e0513 1839 {
1840 Type* t = Type::lookup_integer_type("int64");
1841 type = type_to_tree(t->get_backend(gogo));
1842 }
e440a328 1843 else
1844 type = long_long_integer_type_node;
1845 }
1846 return Expression::integer_constant_tree(this->val_, type);
1847}
1848
1849// Write VAL to export data.
1850
1851void
8b1c301d 1852Integer_expression::export_integer(String_dump* exp, const mpz_t val)
e440a328 1853{
1854 char* s = mpz_get_str(NULL, 10, val);
1855 exp->write_c_string(s);
1856 free(s);
1857}
1858
1859// Export an integer in a constant expression.
1860
1861void
1862Integer_expression::do_export(Export* exp) const
1863{
1864 Integer_expression::export_integer(exp, this->val_);
5d4b8566 1865 if (this->is_character_constant_)
1866 exp->write_c_string("'");
e440a328 1867 // A trailing space lets us reliably identify the end of the number.
1868 exp->write_c_string(" ");
1869}
1870
1871// Import an integer, floating point, or complex value. This handles
1872// all these types because they all start with digits.
1873
1874Expression*
1875Integer_expression::do_import(Import* imp)
1876{
1877 std::string num = imp->read_identifier();
1878 imp->require_c_string(" ");
1879 if (!num.empty() && num[num.length() - 1] == 'i')
1880 {
1881 mpfr_t real;
1882 size_t plus_pos = num.find('+', 1);
1883 size_t minus_pos = num.find('-', 1);
1884 size_t pos;
1885 if (plus_pos == std::string::npos)
1886 pos = minus_pos;
1887 else if (minus_pos == std::string::npos)
1888 pos = plus_pos;
1889 else
1890 {
1891 error_at(imp->location(), "bad number in import data: %qs",
1892 num.c_str());
1893 return Expression::make_error(imp->location());
1894 }
1895 if (pos == std::string::npos)
1896 mpfr_set_ui(real, 0, GMP_RNDN);
1897 else
1898 {
1899 std::string real_str = num.substr(0, pos);
1900 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1901 {
1902 error_at(imp->location(), "bad number in import data: %qs",
1903 real_str.c_str());
1904 return Expression::make_error(imp->location());
1905 }
1906 }
1907
1908 std::string imag_str;
1909 if (pos == std::string::npos)
1910 imag_str = num;
1911 else
1912 imag_str = num.substr(pos);
1913 imag_str = imag_str.substr(0, imag_str.size() - 1);
1914 mpfr_t imag;
1915 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1916 {
1917 error_at(imp->location(), "bad number in import data: %qs",
1918 imag_str.c_str());
1919 return Expression::make_error(imp->location());
1920 }
1921 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1922 imp->location());
1923 mpfr_clear(real);
1924 mpfr_clear(imag);
1925 return ret;
1926 }
1927 else if (num.find('.') == std::string::npos
1928 && num.find('E') == std::string::npos)
1929 {
5d4b8566 1930 bool is_character_constant = (!num.empty()
1931 && num[num.length() - 1] == '\'');
1932 if (is_character_constant)
1933 num = num.substr(0, num.length() - 1);
e440a328 1934 mpz_t val;
1935 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1936 {
1937 error_at(imp->location(), "bad number in import data: %qs",
1938 num.c_str());
1939 return Expression::make_error(imp->location());
1940 }
5d4b8566 1941 Expression* ret;
1942 if (is_character_constant)
1943 ret = Expression::make_character(&val, NULL, imp->location());
1944 else
1945 ret = Expression::make_integer(&val, NULL, imp->location());
e440a328 1946 mpz_clear(val);
1947 return ret;
1948 }
1949 else
1950 {
1951 mpfr_t val;
1952 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1953 {
1954 error_at(imp->location(), "bad number in import data: %qs",
1955 num.c_str());
1956 return Expression::make_error(imp->location());
1957 }
1958 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1959 mpfr_clear(val);
1960 return ret;
1961 }
1962}
d751bb78 1963// Ast dump for integer expression.
1964
1965void
1966Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1967{
5d4b8566 1968 if (this->is_character_constant_)
1969 ast_dump_context->ostream() << '\'';
8b1c301d 1970 Integer_expression::export_integer(ast_dump_context, this->val_);
5d4b8566 1971 if (this->is_character_constant_)
1972 ast_dump_context->ostream() << '\'';
d751bb78 1973}
1974
e440a328 1975// Build a new integer value.
1976
1977Expression*
5d4b8566 1978Expression::make_integer(const mpz_t* val, Type* type, Location location)
1979{
1980 return new Integer_expression(val, type, false, location);
1981}
1982
1983// Build a new character constant value.
1984
1985Expression*
1986Expression::make_character(const mpz_t* val, Type* type, Location location)
e440a328 1987{
5d4b8566 1988 return new Integer_expression(val, type, true, location);
e440a328 1989}
1990
1991// Floats.
1992
1993class Float_expression : public Expression
1994{
1995 public:
b13c66cd 1996 Float_expression(const mpfr_t* val, Type* type, Location location)
e440a328 1997 : Expression(EXPRESSION_FLOAT, location),
1998 type_(type)
1999 {
2000 mpfr_init_set(this->val_, *val, GMP_RNDN);
2001 }
2002
e440a328 2003 // Write VAL to export data.
2004 static void
8b1c301d 2005 export_float(String_dump* exp, const mpfr_t val);
2006
d751bb78 2007 // Write VAL to dump file.
2008 static void
2009 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
e440a328 2010
2011 protected:
2012 bool
2013 do_is_constant() const
2014 { return true; }
2015
2016 bool
0c77715b 2017 do_numeric_constant_value(Numeric_constant* nc) const
2018 {
2019 nc->set_float(this->type_, this->val_);
2020 return true;
2021 }
e440a328 2022
2023 Type*
2024 do_type();
2025
2026 void
2027 do_determine_type(const Type_context*);
2028
2029 void
2030 do_check_types(Gogo*);
2031
2032 Expression*
2033 do_copy()
2034 { return Expression::make_float(&this->val_, this->type_,
2035 this->location()); }
2036
2037 tree
2038 do_get_tree(Translate_context*);
2039
2040 void
2041 do_export(Export*) const;
2042
d751bb78 2043 void
2044 do_dump_expression(Ast_dump_context*) const;
2045
e440a328 2046 private:
2047 // The floating point value.
2048 mpfr_t val_;
2049 // The type so far.
2050 Type* type_;
2051};
2052
e440a328 2053// Return the current type. If we haven't set the type yet, we return
2054// an abstract float type.
2055
2056Type*
2057Float_expression::do_type()
2058{
2059 if (this->type_ == NULL)
2060 this->type_ = Type::make_abstract_float_type();
2061 return this->type_;
2062}
2063
2064// Set the type of the float value. Here we may switch from an
2065// abstract type to a real type.
2066
2067void
2068Float_expression::do_determine_type(const Type_context* context)
2069{
2070 if (this->type_ != NULL && !this->type_->is_abstract())
2071 ;
2072 else if (context->type != NULL
2073 && (context->type->integer_type() != NULL
2074 || context->type->float_type() != NULL
2075 || context->type->complex_type() != NULL))
2076 this->type_ = context->type;
2077 else if (!context->may_be_abstract)
48080209 2078 this->type_ = Type::lookup_float_type("float64");
e440a328 2079}
2080
e440a328 2081// Check the type of a float value.
2082
2083void
2084Float_expression::do_check_types(Gogo*)
2085{
0c77715b 2086 Type* type = this->type_;
2087 if (type == NULL)
e440a328 2088 return;
0c77715b 2089 Numeric_constant nc;
2090 nc.set_float(NULL, this->val_);
2091 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2092 this->set_is_error();
e440a328 2093}
2094
2095// Get a tree for a float constant.
2096
2097tree
2098Float_expression::do_get_tree(Translate_context* context)
2099{
2100 Gogo* gogo = context->gogo();
2101 tree type;
2102 if (this->type_ != NULL && !this->type_->is_abstract())
9f0e0513 2103 type = type_to_tree(this->type_->get_backend(gogo));
e440a328 2104 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2105 {
2106 // We have an abstract integer type. We just hope for the best.
9f0e0513 2107 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
e440a328 2108 }
2109 else
2110 {
2111 // If we still have an abstract type here, then this is being
2112 // used in a constant expression which didn't get reduced. We
2113 // just use float64 and hope for the best.
9f0e0513 2114 Type* ft = Type::lookup_float_type("float64");
2115 type = type_to_tree(ft->get_backend(gogo));
e440a328 2116 }
2117 return Expression::float_constant_tree(this->val_, type);
2118}
2119
8b1c301d 2120// Write a floating point number to a string dump.
e440a328 2121
2122void
8b1c301d 2123Float_expression::export_float(String_dump *exp, const mpfr_t val)
e440a328 2124{
2125 mp_exp_t exponent;
2126 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2127 if (*s == '-')
2128 exp->write_c_string("-");
2129 exp->write_c_string("0.");
2130 exp->write_c_string(*s == '-' ? s + 1 : s);
2131 mpfr_free_str(s);
2132 char buf[30];
2133 snprintf(buf, sizeof buf, "E%ld", exponent);
2134 exp->write_c_string(buf);
2135}
2136
2137// Export a floating point number in a constant expression.
2138
2139void
2140Float_expression::do_export(Export* exp) const
2141{
2142 Float_expression::export_float(exp, this->val_);
2143 // A trailing space lets us reliably identify the end of the number.
2144 exp->write_c_string(" ");
2145}
2146
d751bb78 2147// Dump a floating point number to the dump file.
2148
2149void
2150Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2151{
8b1c301d 2152 Float_expression::export_float(ast_dump_context, this->val_);
d751bb78 2153}
2154
e440a328 2155// Make a float expression.
2156
2157Expression*
b13c66cd 2158Expression::make_float(const mpfr_t* val, Type* type, Location location)
e440a328 2159{
2160 return new Float_expression(val, type, location);
2161}
2162
2163// Complex numbers.
2164
2165class Complex_expression : public Expression
2166{
2167 public:
2168 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
b13c66cd 2169 Location location)
e440a328 2170 : Expression(EXPRESSION_COMPLEX, location),
2171 type_(type)
2172 {
2173 mpfr_init_set(this->real_, *real, GMP_RNDN);
2174 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2175 }
2176
8b1c301d 2177 // Write REAL/IMAG to string dump.
e440a328 2178 static void
8b1c301d 2179 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
e440a328 2180
d751bb78 2181 // Write REAL/IMAG to dump context.
2182 static void
2183 dump_complex(Ast_dump_context* ast_dump_context,
2184 const mpfr_t real, const mpfr_t val);
2185
e440a328 2186 protected:
2187 bool
2188 do_is_constant() const
2189 { return true; }
2190
2191 bool
0c77715b 2192 do_numeric_constant_value(Numeric_constant* nc) const
2193 {
2194 nc->set_complex(this->type_, this->real_, this->imag_);
2195 return true;
2196 }
e440a328 2197
2198 Type*
2199 do_type();
2200
2201 void
2202 do_determine_type(const Type_context*);
2203
2204 void
2205 do_check_types(Gogo*);
2206
2207 Expression*
2208 do_copy()
2209 {
2210 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2211 this->location());
2212 }
2213
2214 tree
2215 do_get_tree(Translate_context*);
2216
2217 void
2218 do_export(Export*) const;
2219
d751bb78 2220 void
2221 do_dump_expression(Ast_dump_context*) const;
2222
e440a328 2223 private:
2224 // The real part.
2225 mpfr_t real_;
2226 // The imaginary part;
2227 mpfr_t imag_;
2228 // The type if known.
2229 Type* type_;
2230};
2231
e440a328 2232// Return the current type. If we haven't set the type yet, we return
2233// an abstract complex type.
2234
2235Type*
2236Complex_expression::do_type()
2237{
2238 if (this->type_ == NULL)
2239 this->type_ = Type::make_abstract_complex_type();
2240 return this->type_;
2241}
2242
2243// Set the type of the complex value. Here we may switch from an
2244// abstract type to a real type.
2245
2246void
2247Complex_expression::do_determine_type(const Type_context* context)
2248{
2249 if (this->type_ != NULL && !this->type_->is_abstract())
2250 ;
2251 else if (context->type != NULL
2252 && context->type->complex_type() != NULL)
2253 this->type_ = context->type;
2254 else if (!context->may_be_abstract)
48080209 2255 this->type_ = Type::lookup_complex_type("complex128");
e440a328 2256}
2257
e440a328 2258// Check the type of a complex value.
2259
2260void
2261Complex_expression::do_check_types(Gogo*)
2262{
0c77715b 2263 Type* type = this->type_;
2264 if (type == NULL)
e440a328 2265 return;
0c77715b 2266 Numeric_constant nc;
2267 nc.set_complex(NULL, this->real_, this->imag_);
2268 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2269 this->set_is_error();
2270}
2271
2272// Get a tree for a complex constant.
2273
2274tree
2275Complex_expression::do_get_tree(Translate_context* context)
2276{
2277 Gogo* gogo = context->gogo();
2278 tree type;
2279 if (this->type_ != NULL && !this->type_->is_abstract())
9f0e0513 2280 type = type_to_tree(this->type_->get_backend(gogo));
e440a328 2281 else
2282 {
2283 // If we still have an abstract type here, this this is being
2284 // used in a constant expression which didn't get reduced. We
2285 // just use complex128 and hope for the best.
9f0e0513 2286 Type* ct = Type::lookup_complex_type("complex128");
2287 type = type_to_tree(ct->get_backend(gogo));
e440a328 2288 }
2289 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2290}
2291
2292// Write REAL/IMAG to export data.
2293
2294void
8b1c301d 2295Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
e440a328 2296 const mpfr_t imag)
2297{
2298 if (!mpfr_zero_p(real))
2299 {
2300 Float_expression::export_float(exp, real);
2301 if (mpfr_sgn(imag) > 0)
2302 exp->write_c_string("+");
2303 }
2304 Float_expression::export_float(exp, imag);
2305 exp->write_c_string("i");
2306}
2307
2308// Export a complex number in a constant expression.
2309
2310void
2311Complex_expression::do_export(Export* exp) const
2312{
2313 Complex_expression::export_complex(exp, this->real_, this->imag_);
2314 // A trailing space lets us reliably identify the end of the number.
2315 exp->write_c_string(" ");
2316}
2317
d751bb78 2318// Dump a complex expression to the dump file.
2319
2320void
2321Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2322{
8b1c301d 2323 Complex_expression::export_complex(ast_dump_context,
d751bb78 2324 this->real_,
2325 this->imag_);
2326}
2327
e440a328 2328// Make a complex expression.
2329
2330Expression*
2331Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
b13c66cd 2332 Location location)
e440a328 2333{
2334 return new Complex_expression(real, imag, type, location);
2335}
2336
d5b605df 2337// Find a named object in an expression.
2338
2339class Find_named_object : public Traverse
2340{
2341 public:
2342 Find_named_object(Named_object* no)
2343 : Traverse(traverse_expressions),
2344 no_(no), found_(false)
2345 { }
2346
2347 // Whether we found the object.
2348 bool
2349 found() const
2350 { return this->found_; }
2351
2352 protected:
2353 int
2354 expression(Expression**);
2355
2356 private:
2357 // The object we are looking for.
2358 Named_object* no_;
2359 // Whether we found it.
2360 bool found_;
2361};
2362
e440a328 2363// A reference to a const in an expression.
2364
2365class Const_expression : public Expression
2366{
2367 public:
b13c66cd 2368 Const_expression(Named_object* constant, Location location)
e440a328 2369 : Expression(EXPRESSION_CONST_REFERENCE, location),
13e818f5 2370 constant_(constant), type_(NULL), seen_(false)
e440a328 2371 { }
2372
d5b605df 2373 Named_object*
2374 named_object()
2375 { return this->constant_; }
2376
a7f064d5 2377 // Check that the initializer does not refer to the constant itself.
2378 void
2379 check_for_init_loop();
2380
e440a328 2381 protected:
ba4aedd4 2382 int
2383 do_traverse(Traverse*);
2384
e440a328 2385 Expression*
ceeb4318 2386 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 2387
2388 bool
2389 do_is_constant() const
2390 { return true; }
2391
2392 bool
0c77715b 2393 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 2394
2395 bool
af6b489a 2396 do_string_constant_value(std::string* val) const;
e440a328 2397
2398 Type*
2399 do_type();
2400
2401 // The type of a const is set by the declaration, not the use.
2402 void
2403 do_determine_type(const Type_context*);
2404
2405 void
2406 do_check_types(Gogo*);
2407
2408 Expression*
2409 do_copy()
2410 { return this; }
2411
2412 tree
2413 do_get_tree(Translate_context* context);
2414
2415 // When exporting a reference to a const as part of a const
2416 // expression, we export the value. We ignore the fact that it has
2417 // a name.
2418 void
2419 do_export(Export* exp) const
2420 { this->constant_->const_value()->expr()->export_expression(exp); }
2421
d751bb78 2422 void
2423 do_dump_expression(Ast_dump_context*) const;
2424
e440a328 2425 private:
2426 // The constant.
2427 Named_object* constant_;
2428 // The type of this reference. This is used if the constant has an
2429 // abstract type.
2430 Type* type_;
13e818f5 2431 // Used to prevent infinite recursion when a constant incorrectly
2432 // refers to itself.
2433 mutable bool seen_;
e440a328 2434};
2435
ba4aedd4 2436// Traversal.
2437
2438int
2439Const_expression::do_traverse(Traverse* traverse)
2440{
2441 if (this->type_ != NULL)
2442 return Type::traverse(this->type_, traverse);
2443 return TRAVERSE_CONTINUE;
2444}
2445
e440a328 2446// Lower a constant expression. This is where we convert the
2447// predeclared constant iota into an integer value.
2448
2449Expression*
ceeb4318 2450Const_expression::do_lower(Gogo* gogo, Named_object*,
2451 Statement_inserter*, int iota_value)
e440a328 2452{
2453 if (this->constant_->const_value()->expr()->classification()
2454 == EXPRESSION_IOTA)
2455 {
2456 if (iota_value == -1)
2457 {
2458 error_at(this->location(),
2459 "iota is only defined in const declarations");
2460 iota_value = 0;
2461 }
2462 mpz_t val;
2463 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2464 Expression* ret = Expression::make_integer(&val, NULL,
2465 this->location());
2466 mpz_clear(val);
2467 return ret;
2468 }
2469
2470 // Make sure that the constant itself has been lowered.
2471 gogo->lower_constant(this->constant_);
2472
2473 return this;
2474}
2475
0c77715b 2476// Return a numeric constant value.
e440a328 2477
2478bool
0c77715b 2479Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 2480{
13e818f5 2481 if (this->seen_)
2482 return false;
2483
e440a328 2484 Expression* e = this->constant_->const_value()->expr();
0c77715b 2485
13e818f5 2486 this->seen_ = true;
2487
0c77715b 2488 bool r = e->numeric_constant_value(nc);
e440a328 2489
13e818f5 2490 this->seen_ = false;
2491
e440a328 2492 Type* ctype;
2493 if (this->type_ != NULL)
2494 ctype = this->type_;
2495 else
2496 ctype = this->constant_->const_value()->type();
e440a328 2497 if (r && ctype != NULL)
2498 {
0c77715b 2499 if (!nc->set_type(ctype, false, this->location()))
e440a328 2500 return false;
e440a328 2501 }
e440a328 2502
e440a328 2503 return r;
2504}
2505
af6b489a 2506bool
2507Const_expression::do_string_constant_value(std::string* val) const
2508{
2509 if (this->seen_)
2510 return false;
2511
2512 Expression* e = this->constant_->const_value()->expr();
2513
2514 this->seen_ = true;
2515 bool ok = e->string_constant_value(val);
2516 this->seen_ = false;
2517
2518 return ok;
2519}
2520
e440a328 2521// Return the type of the const reference.
2522
2523Type*
2524Const_expression::do_type()
2525{
2526 if (this->type_ != NULL)
2527 return this->type_;
13e818f5 2528
2f78f012 2529 Named_constant* nc = this->constant_->const_value();
2530
2531 if (this->seen_ || nc->lowering())
13e818f5 2532 {
2533 this->report_error(_("constant refers to itself"));
2534 this->type_ = Type::make_error_type();
2535 return this->type_;
2536 }
2537
2538 this->seen_ = true;
2539
e440a328 2540 Type* ret = nc->type();
13e818f5 2541
e440a328 2542 if (ret != NULL)
13e818f5 2543 {
2544 this->seen_ = false;
2545 return ret;
2546 }
2547
e440a328 2548 // During parsing, a named constant may have a NULL type, but we
2549 // must not return a NULL type here.
13e818f5 2550 ret = nc->expr()->type();
2551
2552 this->seen_ = false;
2553
2554 return ret;
e440a328 2555}
2556
2557// Set the type of the const reference.
2558
2559void
2560Const_expression::do_determine_type(const Type_context* context)
2561{
2562 Type* ctype = this->constant_->const_value()->type();
2563 Type* cetype = (ctype != NULL
2564 ? ctype
2565 : this->constant_->const_value()->expr()->type());
2566 if (ctype != NULL && !ctype->is_abstract())
2567 ;
2568 else if (context->type != NULL
0c77715b 2569 && context->type->is_numeric_type()
2570 && cetype->is_numeric_type())
e440a328 2571 this->type_ = context->type;
2572 else if (context->type != NULL
2573 && context->type->is_string_type()
2574 && cetype->is_string_type())
2575 this->type_ = context->type;
2576 else if (context->type != NULL
2577 && context->type->is_boolean_type()
2578 && cetype->is_boolean_type())
2579 this->type_ = context->type;
2580 else if (!context->may_be_abstract)
2581 {
2582 if (cetype->is_abstract())
2583 cetype = cetype->make_non_abstract_type();
2584 this->type_ = cetype;
2585 }
2586}
2587
a7f064d5 2588// Check for a loop in which the initializer of a constant refers to
2589// the constant itself.
e440a328 2590
2591void
a7f064d5 2592Const_expression::check_for_init_loop()
e440a328 2593{
5c13bd80 2594 if (this->type_ != NULL && this->type_->is_error())
d5b605df 2595 return;
2596
a7f064d5 2597 if (this->seen_)
2598 {
2599 this->report_error(_("constant refers to itself"));
2600 this->type_ = Type::make_error_type();
2601 return;
2602 }
2603
d5b605df 2604 Expression* init = this->constant_->const_value()->expr();
2605 Find_named_object find_named_object(this->constant_);
a7f064d5 2606
2607 this->seen_ = true;
d5b605df 2608 Expression::traverse(&init, &find_named_object);
a7f064d5 2609 this->seen_ = false;
2610
d5b605df 2611 if (find_named_object.found())
2612 {
5c13bd80 2613 if (this->type_ == NULL || !this->type_->is_error())
a7f064d5 2614 {
2615 this->report_error(_("constant refers to itself"));
2616 this->type_ = Type::make_error_type();
2617 }
d5b605df 2618 return;
2619 }
a7f064d5 2620}
2621
2622// Check types of a const reference.
2623
2624void
2625Const_expression::do_check_types(Gogo*)
2626{
5c13bd80 2627 if (this->type_ != NULL && this->type_->is_error())
a7f064d5 2628 return;
2629
2630 this->check_for_init_loop();
d5b605df 2631
0c77715b 2632 // Check that numeric constant fits in type.
2633 if (this->type_ != NULL && this->type_->is_numeric_type())
e440a328 2634 {
0c77715b 2635 Numeric_constant nc;
2636 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
e440a328 2637 {
0c77715b 2638 if (!nc.set_type(this->type_, true, this->location()))
2639 this->set_is_error();
e440a328 2640 }
e440a328 2641 }
2642}
2643
2644// Return a tree for the const reference.
2645
2646tree
2647Const_expression::do_get_tree(Translate_context* context)
2648{
2649 Gogo* gogo = context->gogo();
2650 tree type_tree;
2651 if (this->type_ == NULL)
2652 type_tree = NULL_TREE;
2653 else
2654 {
9f0e0513 2655 type_tree = type_to_tree(this->type_->get_backend(gogo));
e440a328 2656 if (type_tree == error_mark_node)
2657 return error_mark_node;
2658 }
2659
2660 // If the type has been set for this expression, but the underlying
2661 // object is an abstract int or float, we try to get the abstract
2662 // value. Otherwise we may lose something in the conversion.
2663 if (this->type_ != NULL
0c77715b 2664 && this->type_->is_numeric_type()
a68492b4 2665 && (this->constant_->const_value()->type() == NULL
2666 || this->constant_->const_value()->type()->is_abstract()))
e440a328 2667 {
2668 Expression* expr = this->constant_->const_value()->expr();
0c77715b 2669 Numeric_constant nc;
2670 if (expr->numeric_constant_value(&nc)
2671 && nc.set_type(this->type_, false, this->location()))
e440a328 2672 {
0c77715b 2673 Expression* e = nc.expression(this->location());
2674 return e->get_tree(context);
e440a328 2675 }
e440a328 2676 }
2677
2678 tree const_tree = this->constant_->get_tree(gogo, context->function());
2679 if (this->type_ == NULL
2680 || const_tree == error_mark_node
2681 || TREE_TYPE(const_tree) == error_mark_node)
2682 return const_tree;
2683
2684 tree ret;
2685 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2686 ret = fold_convert(type_tree, const_tree);
2687 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2688 ret = fold(convert_to_integer(type_tree, const_tree));
2689 else if (TREE_CODE(type_tree) == REAL_TYPE)
2690 ret = fold(convert_to_real(type_tree, const_tree));
2691 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2692 ret = fold(convert_to_complex(type_tree, const_tree));
2693 else
c3e6f413 2694 go_unreachable();
e440a328 2695 return ret;
2696}
2697
d751bb78 2698// Dump ast representation for constant expression.
2699
2700void
2701Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2702{
2703 ast_dump_context->ostream() << this->constant_->name();
2704}
2705
e440a328 2706// Make a reference to a constant in an expression.
2707
2708Expression*
2709Expression::make_const_reference(Named_object* constant,
b13c66cd 2710 Location location)
e440a328 2711{
2712 return new Const_expression(constant, location);
2713}
2714
d5b605df 2715// Find a named object in an expression.
2716
2717int
2718Find_named_object::expression(Expression** pexpr)
2719{
2720 switch ((*pexpr)->classification())
2721 {
2722 case Expression::EXPRESSION_CONST_REFERENCE:
a7f064d5 2723 {
2724 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2725 if (ce->named_object() == this->no_)
2726 break;
2727
2728 // We need to check a constant initializer explicitly, as
2729 // loops here will not be caught by the loop checking for
2730 // variable initializers.
2731 ce->check_for_init_loop();
2732
2733 return TRAVERSE_CONTINUE;
2734 }
2735
d5b605df 2736 case Expression::EXPRESSION_VAR_REFERENCE:
2737 if ((*pexpr)->var_expression()->named_object() == this->no_)
2738 break;
2739 return TRAVERSE_CONTINUE;
2740 case Expression::EXPRESSION_FUNC_REFERENCE:
2741 if ((*pexpr)->func_expression()->named_object() == this->no_)
2742 break;
2743 return TRAVERSE_CONTINUE;
2744 default:
2745 return TRAVERSE_CONTINUE;
2746 }
2747 this->found_ = true;
2748 return TRAVERSE_EXIT;
2749}
2750
e440a328 2751// The nil value.
2752
2753class Nil_expression : public Expression
2754{
2755 public:
b13c66cd 2756 Nil_expression(Location location)
e440a328 2757 : Expression(EXPRESSION_NIL, location)
2758 { }
2759
2760 static Expression*
2761 do_import(Import*);
2762
2763 protected:
2764 bool
2765 do_is_constant() const
2766 { return true; }
2767
2768 Type*
2769 do_type()
2770 { return Type::make_nil_type(); }
2771
2772 void
2773 do_determine_type(const Type_context*)
2774 { }
2775
2776 Expression*
2777 do_copy()
2778 { return this; }
2779
2780 tree
2781 do_get_tree(Translate_context*)
2782 { return null_pointer_node; }
2783
2784 void
2785 do_export(Export* exp) const
2786 { exp->write_c_string("nil"); }
d751bb78 2787
2788 void
2789 do_dump_expression(Ast_dump_context* ast_dump_context) const
2790 { ast_dump_context->ostream() << "nil"; }
e440a328 2791};
2792
2793// Import a nil expression.
2794
2795Expression*
2796Nil_expression::do_import(Import* imp)
2797{
2798 imp->require_c_string("nil");
2799 return Expression::make_nil(imp->location());
2800}
2801
2802// Make a nil expression.
2803
2804Expression*
b13c66cd 2805Expression::make_nil(Location location)
e440a328 2806{
2807 return new Nil_expression(location);
2808}
2809
2810// The value of the predeclared constant iota. This is little more
2811// than a marker. This will be lowered to an integer in
2812// Const_expression::do_lower, which is where we know the value that
2813// it should have.
2814
2815class Iota_expression : public Parser_expression
2816{
2817 public:
b13c66cd 2818 Iota_expression(Location location)
e440a328 2819 : Parser_expression(EXPRESSION_IOTA, location)
2820 { }
2821
2822 protected:
2823 Expression*
ceeb4318 2824 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
c3e6f413 2825 { go_unreachable(); }
e440a328 2826
2827 // There should only ever be one of these.
2828 Expression*
2829 do_copy()
c3e6f413 2830 { go_unreachable(); }
d751bb78 2831
2832 void
2833 do_dump_expression(Ast_dump_context* ast_dump_context) const
2834 { ast_dump_context->ostream() << "iota"; }
e440a328 2835};
2836
2837// Make an iota expression. This is only called for one case: the
2838// value of the predeclared constant iota.
2839
2840Expression*
2841Expression::make_iota()
2842{
b13c66cd 2843 static Iota_expression iota_expression(Linemap::unknown_location());
e440a328 2844 return &iota_expression;
2845}
2846
2847// A type conversion expression.
2848
2849class Type_conversion_expression : public Expression
2850{
2851 public:
2852 Type_conversion_expression(Type* type, Expression* expr,
b13c66cd 2853 Location location)
e440a328 2854 : Expression(EXPRESSION_CONVERSION, location),
2855 type_(type), expr_(expr), may_convert_function_types_(false)
2856 { }
2857
2858 // Return the type to which we are converting.
2859 Type*
2860 type() const
2861 { return this->type_; }
2862
2863 // Return the expression which we are converting.
2864 Expression*
2865 expr() const
2866 { return this->expr_; }
2867
2868 // Permit converting from one function type to another. This is
2869 // used internally for method expressions.
2870 void
2871 set_may_convert_function_types()
2872 {
2873 this->may_convert_function_types_ = true;
2874 }
2875
2876 // Import a type conversion expression.
2877 static Expression*
2878 do_import(Import*);
2879
2880 protected:
2881 int
2882 do_traverse(Traverse* traverse);
2883
2884 Expression*
ceeb4318 2885 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 2886
2887 bool
2888 do_is_constant() const
2889 { return this->expr_->is_constant(); }
2890
2891 bool
0c77715b 2892 do_numeric_constant_value(Numeric_constant*) const;
e440a328 2893
2894 bool
2895 do_string_constant_value(std::string*) const;
2896
2897 Type*
2898 do_type()
2899 { return this->type_; }
2900
2901 void
2902 do_determine_type(const Type_context*)
2903 {
2904 Type_context subcontext(this->type_, false);
2905 this->expr_->determine_type(&subcontext);
2906 }
2907
2908 void
2909 do_check_types(Gogo*);
2910
2911 Expression*
2912 do_copy()
2913 {
2914 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2915 this->location());
2916 }
2917
2918 tree
2919 do_get_tree(Translate_context* context);
2920
2921 void
2922 do_export(Export*) const;
2923
d751bb78 2924 void
2925 do_dump_expression(Ast_dump_context*) const;
2926
e440a328 2927 private:
2928 // The type to convert to.
2929 Type* type_;
2930 // The expression to convert.
2931 Expression* expr_;
2932 // True if this is permitted to convert function types. This is
2933 // used internally for method expressions.
2934 bool may_convert_function_types_;
2935};
2936
2937// Traversal.
2938
2939int
2940Type_conversion_expression::do_traverse(Traverse* traverse)
2941{
2942 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2943 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2944 return TRAVERSE_EXIT;
2945 return TRAVERSE_CONTINUE;
2946}
2947
2948// Convert to a constant at lowering time.
2949
2950Expression*
ceeb4318 2951Type_conversion_expression::do_lower(Gogo*, Named_object*,
2952 Statement_inserter*, int)
e440a328 2953{
2954 Type* type = this->type_;
2955 Expression* val = this->expr_;
b13c66cd 2956 Location location = this->location();
e440a328 2957
0c77715b 2958 if (type->is_numeric_type())
e440a328 2959 {
0c77715b 2960 Numeric_constant nc;
2961 if (val->numeric_constant_value(&nc))
e440a328 2962 {
0c77715b 2963 if (!nc.set_type(type, true, location))
2964 return Expression::make_error(location);
2965 return nc.expression(location);
e440a328 2966 }
e440a328 2967 }
2968
55072f2b 2969 if (type->is_slice_type())
e440a328 2970 {
2971 Type* element_type = type->array_type()->element_type()->forwarded();
60963afd 2972 bool is_byte = (element_type->integer_type() != NULL
2973 && element_type->integer_type()->is_byte());
2974 bool is_rune = (element_type->integer_type() != NULL
2975 && element_type->integer_type()->is_rune());
2976 if (is_byte || is_rune)
e440a328 2977 {
2978 std::string s;
2979 if (val->string_constant_value(&s))
2980 {
2981 Expression_list* vals = new Expression_list();
2982 if (is_byte)
2983 {
2984 for (std::string::const_iterator p = s.begin();
2985 p != s.end();
2986 p++)
2987 {
2988 mpz_t val;
2989 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2990 Expression* v = Expression::make_integer(&val,
2991 element_type,
2992 location);
2993 vals->push_back(v);
2994 mpz_clear(val);
2995 }
2996 }
2997 else
2998 {
2999 const char *p = s.data();
3000 const char *pend = s.data() + s.length();
3001 while (p < pend)
3002 {
3003 unsigned int c;
3004 int adv = Lex::fetch_char(p, &c);
3005 if (adv == 0)
3006 {
3007 warning_at(this->location(), 0,
3008 "invalid UTF-8 encoding");
3009 adv = 1;
3010 }
3011 p += adv;
3012 mpz_t val;
3013 mpz_init_set_ui(val, c);
3014 Expression* v = Expression::make_integer(&val,
3015 element_type,
3016 location);
3017 vals->push_back(v);
3018 mpz_clear(val);
3019 }
3020 }
3021
3022 return Expression::make_slice_composite_literal(type, vals,
3023 location);
3024 }
3025 }
3026 }
3027
3028 return this;
3029}
3030
0c77715b 3031// Return the constant numeric value if there is one.
e440a328 3032
3033bool
0c77715b 3034Type_conversion_expression::do_numeric_constant_value(
3035 Numeric_constant* nc) const
e440a328 3036{
0c77715b 3037 if (!this->type_->is_numeric_type())
e440a328 3038 return false;
0c77715b 3039 if (!this->expr_->numeric_constant_value(nc))
e440a328 3040 return false;
0c77715b 3041 return nc->set_type(this->type_, false, this->location());
e440a328 3042}
3043
3044// Return the constant string value if there is one.
3045
3046bool
3047Type_conversion_expression::do_string_constant_value(std::string* val) const
3048{
3049 if (this->type_->is_string_type()
3050 && this->expr_->type()->integer_type() != NULL)
3051 {
0c77715b 3052 Numeric_constant nc;
3053 if (this->expr_->numeric_constant_value(&nc))
e440a328 3054 {
0c77715b 3055 unsigned long ival;
3056 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
e440a328 3057 {
0c77715b 3058 val->clear();
3059 Lex::append_char(ival, true, val, this->location());
e440a328 3060 return true;
3061 }
3062 }
e440a328 3063 }
3064
3065 // FIXME: Could handle conversion from const []int here.
3066
3067 return false;
3068}
3069
3070// Check that types are convertible.
3071
3072void
3073Type_conversion_expression::do_check_types(Gogo*)
3074{
3075 Type* type = this->type_;
3076 Type* expr_type = this->expr_->type();
3077 std::string reason;
3078
5c13bd80 3079 if (type->is_error() || expr_type->is_error())
842f6425 3080 {
842f6425 3081 this->set_is_error();
3082 return;
3083 }
3084
e440a328 3085 if (this->may_convert_function_types_
3086 && type->function_type() != NULL
3087 && expr_type->function_type() != NULL)
3088 return;
3089
3090 if (Type::are_convertible(type, expr_type, &reason))
3091 return;
3092
3093 error_at(this->location(), "%s", reason.c_str());
3094 this->set_is_error();
3095}
3096
3097// Get a tree for a type conversion.
3098
3099tree
3100Type_conversion_expression::do_get_tree(Translate_context* context)
3101{
3102 Gogo* gogo = context->gogo();
9f0e0513 3103 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
e440a328 3104 tree expr_tree = this->expr_->get_tree(context);
3105
3106 if (type_tree == error_mark_node
3107 || expr_tree == error_mark_node
3108 || TREE_TYPE(expr_tree) == error_mark_node)
3109 return error_mark_node;
3110
3111 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3112 return fold_convert(type_tree, expr_tree);
3113
3114 Type* type = this->type_;
3115 Type* expr_type = this->expr_->type();
3116 tree ret;
3117 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3118 ret = Expression::convert_for_assignment(context, type, expr_type,
3119 expr_tree, this->location());
3120 else if (type->integer_type() != NULL)
3121 {
3122 if (expr_type->integer_type() != NULL
3123 || expr_type->float_type() != NULL
3124 || expr_type->is_unsafe_pointer_type())
3125 ret = fold(convert_to_integer(type_tree, expr_tree));
3126 else
c3e6f413 3127 go_unreachable();
e440a328 3128 }
3129 else if (type->float_type() != NULL)
3130 {
3131 if (expr_type->integer_type() != NULL
3132 || expr_type->float_type() != NULL)
3133 ret = fold(convert_to_real(type_tree, expr_tree));
3134 else
c3e6f413 3135 go_unreachable();
e440a328 3136 }
3137 else if (type->complex_type() != NULL)
3138 {
3139 if (expr_type->complex_type() != NULL)
3140 ret = fold(convert_to_complex(type_tree, expr_tree));
3141 else
c3e6f413 3142 go_unreachable();
e440a328 3143 }
3144 else if (type->is_string_type()
3145 && expr_type->integer_type() != NULL)
3146 {
3147 expr_tree = fold_convert(integer_type_node, expr_tree);
3148 if (host_integerp(expr_tree, 0))
3149 {
3150 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3151 std::string s;
3152 Lex::append_char(intval, true, &s, this->location());
3153 Expression* se = Expression::make_string(s, this->location());
3154 return se->get_tree(context);
3155 }
3156
3157 static tree int_to_string_fndecl;
3158 ret = Gogo::call_builtin(&int_to_string_fndecl,
3159 this->location(),
3160 "__go_int_to_string",
3161 1,
3162 type_tree,
3163 integer_type_node,
3164 fold_convert(integer_type_node, expr_tree));
3165 }
55072f2b 3166 else if (type->is_string_type() && expr_type->is_slice_type())
e440a328 3167 {
e440a328 3168 if (!DECL_P(expr_tree))
3169 expr_tree = save_expr(expr_tree);
55072f2b 3170 Array_type* a = expr_type->array_type();
e440a328 3171 Type* e = a->element_type()->forwarded();
c484d925 3172 go_assert(e->integer_type() != NULL);
e440a328 3173 tree valptr = fold_convert(const_ptr_type_node,
3174 a->value_pointer_tree(gogo, expr_tree));
3175 tree len = a->length_tree(gogo, expr_tree);
b13c66cd 3176 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3177 len);
60963afd 3178 if (e->integer_type()->is_byte())
e440a328 3179 {
3180 static tree byte_array_to_string_fndecl;
3181 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3182 this->location(),
3183 "__go_byte_array_to_string",
3184 2,
3185 type_tree,
3186 const_ptr_type_node,
3187 valptr,
9581e91d 3188 integer_type_node,
e440a328 3189 len);
3190 }
3191 else
3192 {
60963afd 3193 go_assert(e->integer_type()->is_rune());
e440a328 3194 static tree int_array_to_string_fndecl;
3195 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3196 this->location(),
3197 "__go_int_array_to_string",
3198 2,
3199 type_tree,
3200 const_ptr_type_node,
3201 valptr,
9581e91d 3202 integer_type_node,
e440a328 3203 len);
3204 }
3205 }
411eb89e 3206 else if (type->is_slice_type() && expr_type->is_string_type())
e440a328 3207 {
3208 Type* e = type->array_type()->element_type()->forwarded();
c484d925 3209 go_assert(e->integer_type() != NULL);
60963afd 3210 if (e->integer_type()->is_byte())
e440a328 3211 {
ef43e66c 3212 tree string_to_byte_array_fndecl = NULL_TREE;
e440a328 3213 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3214 this->location(),
3215 "__go_string_to_byte_array",
3216 1,
3217 type_tree,
3218 TREE_TYPE(expr_tree),
3219 expr_tree);
3220 }
3221 else
3222 {
60963afd 3223 go_assert(e->integer_type()->is_rune());
ef43e66c 3224 tree string_to_int_array_fndecl = NULL_TREE;
e440a328 3225 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3226 this->location(),
3227 "__go_string_to_int_array",
3228 1,
3229 type_tree,
3230 TREE_TYPE(expr_tree),
3231 expr_tree);
3232 }
3233 }
3234 else if ((type->is_unsafe_pointer_type()
3235 && expr_type->points_to() != NULL)
3236 || (expr_type->is_unsafe_pointer_type()
3237 && type->points_to() != NULL))
3238 ret = fold_convert(type_tree, expr_tree);
3239 else if (type->is_unsafe_pointer_type()
3240 && expr_type->integer_type() != NULL)
3241 ret = convert_to_pointer(type_tree, expr_tree);
3242 else if (this->may_convert_function_types_
3243 && type->function_type() != NULL
3244 && expr_type->function_type() != NULL)
b13c66cd 3245 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3246 expr_tree);
e440a328 3247 else
3248 ret = Expression::convert_for_assignment(context, type, expr_type,
3249 expr_tree, this->location());
3250
3251 return ret;
3252}
3253
3254// Output a type conversion in a constant expression.
3255
3256void
3257Type_conversion_expression::do_export(Export* exp) const
3258{
3259 exp->write_c_string("convert(");
3260 exp->write_type(this->type_);
3261 exp->write_c_string(", ");
3262 this->expr_->export_expression(exp);
3263 exp->write_c_string(")");
3264}
3265
3266// Import a type conversion or a struct construction.
3267
3268Expression*
3269Type_conversion_expression::do_import(Import* imp)
3270{
3271 imp->require_c_string("convert(");
3272 Type* type = imp->read_type();
3273 imp->require_c_string(", ");
3274 Expression* val = Expression::import_expression(imp);
3275 imp->require_c_string(")");
3276 return Expression::make_cast(type, val, imp->location());
3277}
3278
d751bb78 3279// Dump ast representation for a type conversion expression.
3280
3281void
3282Type_conversion_expression::do_dump_expression(
3283 Ast_dump_context* ast_dump_context) const
3284{
3285 ast_dump_context->dump_type(this->type_);
3286 ast_dump_context->ostream() << "(";
3287 ast_dump_context->dump_expression(this->expr_);
3288 ast_dump_context->ostream() << ") ";
3289}
3290
e440a328 3291// Make a type cast expression.
3292
3293Expression*
b13c66cd 3294Expression::make_cast(Type* type, Expression* val, Location location)
e440a328 3295{
3296 if (type->is_error_type() || val->is_error_expression())
3297 return Expression::make_error(location);
3298 return new Type_conversion_expression(type, val, location);
3299}
3300
9581e91d 3301// An unsafe type conversion, used to pass values to builtin functions.
3302
3303class Unsafe_type_conversion_expression : public Expression
3304{
3305 public:
3306 Unsafe_type_conversion_expression(Type* type, Expression* expr,
b13c66cd 3307 Location location)
9581e91d 3308 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3309 type_(type), expr_(expr)
3310 { }
3311
3312 protected:
3313 int
3314 do_traverse(Traverse* traverse);
3315
3316 Type*
3317 do_type()
3318 { return this->type_; }
3319
3320 void
3321 do_determine_type(const Type_context*)
a9182619 3322 { this->expr_->determine_type_no_context(); }
9581e91d 3323
3324 Expression*
3325 do_copy()
3326 {
3327 return new Unsafe_type_conversion_expression(this->type_,
3328 this->expr_->copy(),
3329 this->location());
3330 }
3331
3332 tree
3333 do_get_tree(Translate_context*);
3334
d751bb78 3335 void
3336 do_dump_expression(Ast_dump_context*) const;
3337
9581e91d 3338 private:
3339 // The type to convert to.
3340 Type* type_;
3341 // The expression to convert.
3342 Expression* expr_;
3343};
3344
3345// Traversal.
3346
3347int
3348Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3349{
3350 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3351 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3352 return TRAVERSE_EXIT;
3353 return TRAVERSE_CONTINUE;
3354}
3355
3356// Convert to backend representation.
3357
3358tree
3359Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3360{
3361 // We are only called for a limited number of cases.
3362
3363 Type* t = this->type_;
3364 Type* et = this->expr_->type();
3365
9f0e0513 3366 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
9581e91d 3367 tree expr_tree = this->expr_->get_tree(context);
3368 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3369 return error_mark_node;
3370
b13c66cd 3371 Location loc = this->location();
9581e91d 3372
3373 bool use_view_convert = false;
411eb89e 3374 if (t->is_slice_type())
9581e91d 3375 {
411eb89e 3376 go_assert(et->is_slice_type());
9581e91d 3377 use_view_convert = true;
3378 }
3379 else if (t->map_type() != NULL)
c484d925 3380 go_assert(et->map_type() != NULL);
9581e91d 3381 else if (t->channel_type() != NULL)
c484d925 3382 go_assert(et->channel_type() != NULL);
09ea332d 3383 else if (t->points_to() != NULL)
c484d925 3384 go_assert(et->points_to() != NULL || et->is_nil_type());
9581e91d 3385 else if (et->is_unsafe_pointer_type())
c484d925 3386 go_assert(t->points_to() != NULL);
9581e91d 3387 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3388 {
c484d925 3389 go_assert(et->interface_type() != NULL
9581e91d 3390 && !et->interface_type()->is_empty());
3391 use_view_convert = true;
3392 }
3393 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3394 {
c484d925 3395 go_assert(et->interface_type() != NULL
9581e91d 3396 && et->interface_type()->is_empty());
3397 use_view_convert = true;
3398 }
588e3cf9 3399 else if (t->integer_type() != NULL)
3400 {
c484d925 3401 go_assert(et->is_boolean_type()
588e3cf9 3402 || et->integer_type() != NULL
3403 || et->function_type() != NULL
3404 || et->points_to() != NULL
3405 || et->map_type() != NULL
3406 || et->channel_type() != NULL);
3407 return convert_to_integer(type_tree, expr_tree);
3408 }
9581e91d 3409 else
c3e6f413 3410 go_unreachable();
9581e91d 3411
3412 if (use_view_convert)
b13c66cd 3413 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3414 expr_tree);
9581e91d 3415 else
b13c66cd 3416 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
9581e91d 3417}
3418
d751bb78 3419// Dump ast representation for an unsafe type conversion expression.
3420
3421void
3422Unsafe_type_conversion_expression::do_dump_expression(
3423 Ast_dump_context* ast_dump_context) const
3424{
3425 ast_dump_context->dump_type(this->type_);
3426 ast_dump_context->ostream() << "(";
3427 ast_dump_context->dump_expression(this->expr_);
3428 ast_dump_context->ostream() << ") ";
3429}
3430
9581e91d 3431// Make an unsafe type conversion expression.
3432
3433Expression*
3434Expression::make_unsafe_cast(Type* type, Expression* expr,
b13c66cd 3435 Location location)
9581e91d 3436{
3437 return new Unsafe_type_conversion_expression(type, expr, location);
3438}
3439
e440a328 3440// Unary expressions.
3441
3442class Unary_expression : public Expression
3443{
3444 public:
b13c66cd 3445 Unary_expression(Operator op, Expression* expr, Location location)
e440a328 3446 : Expression(EXPRESSION_UNARY, location),
09ea332d 3447 op_(op), escapes_(true), create_temp_(false), expr_(expr)
e440a328 3448 { }
3449
3450 // Return the operator.
3451 Operator
3452 op() const
3453 { return this->op_; }
3454
3455 // Return the operand.
3456 Expression*
3457 operand() const
3458 { return this->expr_; }
3459
3460 // Record that an address expression does not escape.
3461 void
3462 set_does_not_escape()
3463 {
c484d925 3464 go_assert(this->op_ == OPERATOR_AND);
e440a328 3465 this->escapes_ = false;
3466 }
3467
09ea332d 3468 // Record that this is an address expression which should create a
3469 // temporary variable if necessary. This is used for method calls.
3470 void
3471 set_create_temp()
3472 {
3473 go_assert(this->op_ == OPERATOR_AND);
3474 this->create_temp_ = true;
3475 }
3476
0c77715b 3477 // Apply unary opcode OP to UNC, setting NC. Return true if this
3478 // could be done, false if not. Issue errors for overflow.
e440a328 3479 static bool
0c77715b 3480 eval_constant(Operator op, const Numeric_constant* unc,
3481 Location, Numeric_constant* nc);
e440a328 3482
3483 static Expression*
3484 do_import(Import*);
3485
3486 protected:
3487 int
3488 do_traverse(Traverse* traverse)
3489 { return Expression::traverse(&this->expr_, traverse); }
3490
3491 Expression*
ceeb4318 3492 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 3493
3494 bool
3495 do_is_constant() const;
3496
3497 bool
0c77715b 3498 do_numeric_constant_value(Numeric_constant*) const;
e440a328 3499
3500 Type*
3501 do_type();
3502
3503 void
3504 do_determine_type(const Type_context*);
3505
3506 void
3507 do_check_types(Gogo*);
3508
3509 Expression*
3510 do_copy()
3511 {
3512 return Expression::make_unary(this->op_, this->expr_->copy(),
3513 this->location());
3514 }
3515
baef9f7a 3516 bool
3517 do_must_eval_subexpressions_in_order(int*) const
3518 { return this->op_ == OPERATOR_MULT; }
3519
e440a328 3520 bool
3521 do_is_addressable() const
3522 { return this->op_ == OPERATOR_MULT; }
3523
3524 tree
3525 do_get_tree(Translate_context*);
3526
3527 void
3528 do_export(Export*) const;
3529
d751bb78 3530 void
3531 do_dump_expression(Ast_dump_context*) const;
3532
e440a328 3533 private:
3534 // The unary operator to apply.
3535 Operator op_;
3536 // Normally true. False if this is an address expression which does
3537 // not escape the current function.
3538 bool escapes_;
09ea332d 3539 // True if this is an address expression which should create a
3540 // temporary variable if necessary.
3541 bool create_temp_;
e440a328 3542 // The operand.
3543 Expression* expr_;
3544};
3545
3546// If we are taking the address of a composite literal, and the
3547// contents are not constant, then we want to make a heap composite
3548// instead.
3549
3550Expression*
ceeb4318 3551Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 3552{
b13c66cd 3553 Location loc = this->location();
e440a328 3554 Operator op = this->op_;
3555 Expression* expr = this->expr_;
3556
3557 if (op == OPERATOR_MULT && expr->is_type_expression())
3558 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3559
3560 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3561 // moving x to the heap. FIXME: Is it worth doing a real escape
3562 // analysis here? This case is found in math/unsafe.go and is
3563 // therefore worth special casing.
3564 if (op == OPERATOR_MULT)
3565 {
3566 Expression* e = expr;
3567 while (e->classification() == EXPRESSION_CONVERSION)
3568 {
3569 Type_conversion_expression* te
3570 = static_cast<Type_conversion_expression*>(e);
3571 e = te->expr();
3572 }
3573
3574 if (e->classification() == EXPRESSION_UNARY)
3575 {
3576 Unary_expression* ue = static_cast<Unary_expression*>(e);
3577 if (ue->op_ == OPERATOR_AND)
3578 {
3579 if (e == expr)
3580 {
3581 // *&x == x.
3582 return ue->expr_;
3583 }
3584 ue->set_does_not_escape();
3585 }
3586 }
3587 }
3588
55661ce9 3589 // Catching an invalid indirection of unsafe.Pointer here avoid
3590 // having to deal with TYPE_VOID in other places.
3591 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3592 {
3593 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3594 return Expression::make_error(this->location());
3595 }
3596
59a401fe 3597 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
e440a328 3598 {
0c77715b 3599 Numeric_constant nc;
3600 if (expr->numeric_constant_value(&nc))
e440a328 3601 {
0c77715b 3602 Numeric_constant result;
3603 if (Unary_expression::eval_constant(op, &nc, loc, &result))
3604 return result.expression(loc);
e440a328 3605 }
3606 }
3607
3608 return this;
3609}
3610
3611// Return whether a unary expression is a constant.
3612
3613bool
3614Unary_expression::do_is_constant() const
3615{
3616 if (this->op_ == OPERATOR_MULT)
3617 {
3618 // Indirecting through a pointer is only constant if the object
3619 // to which the expression points is constant, but we currently
3620 // have no way to determine that.
3621 return false;
3622 }
3623 else if (this->op_ == OPERATOR_AND)
3624 {
3625 // Taking the address of a variable is constant if it is a
3626 // global variable, not constant otherwise. In other cases
3627 // taking the address is probably not a constant.
3628 Var_expression* ve = this->expr_->var_expression();
3629 if (ve != NULL)
3630 {
3631 Named_object* no = ve->named_object();
3632 return no->is_variable() && no->var_value()->is_global();
3633 }
3634 return false;
3635 }
3636 else
3637 return this->expr_->is_constant();
3638}
3639
0c77715b 3640// Apply unary opcode OP to UNC, setting NC. Return true if this
3641// could be done, false if not. Issue errors for overflow.
e440a328 3642
3643bool
0c77715b 3644Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
3645 Location location, Numeric_constant* nc)
e440a328 3646{
3647 switch (op)
3648 {
3649 case OPERATOR_PLUS:
0c77715b 3650 *nc = *unc;
e440a328 3651 return true;
0c77715b 3652
e440a328 3653 case OPERATOR_MINUS:
0c77715b 3654 if (unc->is_int() || unc->is_rune())
3655 break;
3656 else if (unc->is_float())
3657 {
3658 mpfr_t uval;
3659 unc->get_float(&uval);
3660 mpfr_t val;
3661 mpfr_init(val);
3662 mpfr_neg(val, uval, GMP_RNDN);
3663 nc->set_float(unc->type(), val);
3664 mpfr_clear(uval);
3665 mpfr_clear(val);
3666 return true;
3667 }
3668 else if (unc->is_complex())
3669 {
3670 mpfr_t ureal, uimag;
3671 unc->get_complex(&ureal, &uimag);
3672 mpfr_t real, imag;
3673 mpfr_init(real);
3674 mpfr_init(imag);
3675 mpfr_neg(real, ureal, GMP_RNDN);
3676 mpfr_neg(imag, uimag, GMP_RNDN);
3677 nc->set_complex(unc->type(), real, imag);
3678 mpfr_clear(ureal);
3679 mpfr_clear(uimag);
3680 mpfr_clear(real);
3681 mpfr_clear(imag);
3682 return true;
3683 }
e440a328 3684 else
0c77715b 3685 go_unreachable();
e440a328 3686
0c77715b 3687 case OPERATOR_XOR:
3688 break;
68448d53 3689
59a401fe 3690 case OPERATOR_NOT:
e440a328 3691 case OPERATOR_AND:
3692 case OPERATOR_MULT:
3693 return false;
0c77715b 3694
e440a328 3695 default:
c3e6f413 3696 go_unreachable();
e440a328 3697 }
e440a328 3698
0c77715b 3699 if (!unc->is_int() && !unc->is_rune())
3700 return false;
3701
3702 mpz_t uval;
8387e1df 3703 if (unc->is_rune())
3704 unc->get_rune(&uval);
3705 else
3706 unc->get_int(&uval);
0c77715b 3707 mpz_t val;
3708 mpz_init(val);
e440a328 3709
e440a328 3710 switch (op)
3711 {
e440a328 3712 case OPERATOR_MINUS:
0c77715b 3713 mpz_neg(val, uval);
3714 break;
3715
e440a328 3716 case OPERATOR_NOT:
0c77715b 3717 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3718 break;
3719
e440a328 3720 case OPERATOR_XOR:
0c77715b 3721 {
3722 Type* utype = unc->type();
3723 if (utype->integer_type() == NULL
3724 || utype->integer_type()->is_abstract())
3725 mpz_com(val, uval);
3726 else
3727 {
3728 // The number of HOST_WIDE_INTs that it takes to represent
3729 // UVAL.
3730 size_t count = ((mpz_sizeinbase(uval, 2)
3731 + HOST_BITS_PER_WIDE_INT
3732 - 1)
3733 / HOST_BITS_PER_WIDE_INT);
e440a328 3734
0c77715b 3735 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3736 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3737
3738 size_t obits = utype->integer_type()->bits();
3739
3740 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
3741 {
3742 mpz_t adj;
3743 mpz_init_set_ui(adj, 1);
3744 mpz_mul_2exp(adj, adj, obits);
3745 mpz_add(uval, uval, adj);
3746 mpz_clear(adj);
3747 }
3748
3749 size_t ecount;
3750 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3751 go_assert(ecount <= count);
3752
3753 // Trim down to the number of words required by the type.
3754 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3755 / HOST_BITS_PER_WIDE_INT);
3756 go_assert(ocount <= count);
3757
3758 for (size_t i = 0; i < ocount; ++i)
3759 phwi[i] = ~phwi[i];
3760
3761 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3762 if (clearbits != 0)
3763 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3764 >> clearbits);
3765
3766 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3767
3768 if (!utype->integer_type()->is_unsigned()
3769 && mpz_tstbit(val, obits - 1))
3770 {
3771 mpz_t adj;
3772 mpz_init_set_ui(adj, 1);
3773 mpz_mul_2exp(adj, adj, obits);
3774 mpz_sub(val, val, adj);
3775 mpz_clear(adj);
3776 }
3777
3778 delete[] phwi;
3779 }
3780 }
3781 break;
e440a328 3782
e440a328 3783 default:
c3e6f413 3784 go_unreachable();
e440a328 3785 }
e440a328 3786
0c77715b 3787 if (unc->is_rune())
3788 nc->set_rune(NULL, val);
e440a328 3789 else
0c77715b 3790 nc->set_int(NULL, val);
e440a328 3791
0c77715b 3792 mpz_clear(uval);
3793 mpz_clear(val);
e440a328 3794
0c77715b 3795 return nc->set_type(unc->type(), true, location);
e440a328 3796}
3797
0c77715b 3798// Return the integral constant value of a unary expression, if it has one.
e440a328 3799
3800bool
0c77715b 3801Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 3802{
0c77715b 3803 Numeric_constant unc;
3804 if (!this->expr_->numeric_constant_value(&unc))
3805 return false;
3806 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
3807 nc);
e440a328 3808}
3809
3810// Return the type of a unary expression.
3811
3812Type*
3813Unary_expression::do_type()
3814{
3815 switch (this->op_)
3816 {
3817 case OPERATOR_PLUS:
3818 case OPERATOR_MINUS:
3819 case OPERATOR_NOT:
3820 case OPERATOR_XOR:
3821 return this->expr_->type();
3822
3823 case OPERATOR_AND:
3824 return Type::make_pointer_type(this->expr_->type());
3825
3826 case OPERATOR_MULT:
3827 {
3828 Type* subtype = this->expr_->type();
3829 Type* points_to = subtype->points_to();
3830 if (points_to == NULL)
3831 return Type::make_error_type();
3832 return points_to;
3833 }
3834
3835 default:
c3e6f413 3836 go_unreachable();
e440a328 3837 }
3838}
3839
3840// Determine abstract types for a unary expression.
3841
3842void
3843Unary_expression::do_determine_type(const Type_context* context)
3844{
3845 switch (this->op_)
3846 {
3847 case OPERATOR_PLUS:
3848 case OPERATOR_MINUS:
3849 case OPERATOR_NOT:
3850 case OPERATOR_XOR:
3851 this->expr_->determine_type(context);
3852 break;
3853
3854 case OPERATOR_AND:
3855 // Taking the address of something.
3856 {
3857 Type* subtype = (context->type == NULL
3858 ? NULL
3859 : context->type->points_to());
3860 Type_context subcontext(subtype, false);
3861 this->expr_->determine_type(&subcontext);
3862 }
3863 break;
3864
3865 case OPERATOR_MULT:
3866 // Indirecting through a pointer.
3867 {
3868 Type* subtype = (context->type == NULL
3869 ? NULL
3870 : Type::make_pointer_type(context->type));
3871 Type_context subcontext(subtype, false);
3872 this->expr_->determine_type(&subcontext);
3873 }
3874 break;
3875
3876 default:
c3e6f413 3877 go_unreachable();
e440a328 3878 }
3879}
3880
3881// Check types for a unary expression.
3882
3883void
3884Unary_expression::do_check_types(Gogo*)
3885{
9fe897ef 3886 Type* type = this->expr_->type();
5c13bd80 3887 if (type->is_error())
9fe897ef 3888 {
3889 this->set_is_error();
3890 return;
3891 }
3892
e440a328 3893 switch (this->op_)
3894 {
3895 case OPERATOR_PLUS:
3896 case OPERATOR_MINUS:
9fe897ef 3897 if (type->integer_type() == NULL
3898 && type->float_type() == NULL
3899 && type->complex_type() == NULL)
3900 this->report_error(_("expected numeric type"));
e440a328 3901 break;
3902
3903 case OPERATOR_NOT:
59a401fe 3904 if (!type->is_boolean_type())
3905 this->report_error(_("expected boolean type"));
3906 break;
3907
e440a328 3908 case OPERATOR_XOR:
9fe897ef 3909 if (type->integer_type() == NULL
3910 && !type->is_boolean_type())
3911 this->report_error(_("expected integer or boolean type"));
e440a328 3912 break;
3913
3914 case OPERATOR_AND:
3915 if (!this->expr_->is_addressable())
09ea332d 3916 {
3917 if (!this->create_temp_)
3918 this->report_error(_("invalid operand for unary %<&%>"));
3919 }
e440a328 3920 else
3921 this->expr_->address_taken(this->escapes_);
3922 break;
3923
3924 case OPERATOR_MULT:
3925 // Indirecting through a pointer.
9fe897ef 3926 if (type->points_to() == NULL)
3927 this->report_error(_("expected pointer"));
e440a328 3928 break;
3929
3930 default:
c3e6f413 3931 go_unreachable();
e440a328 3932 }
3933}
3934
3935// Get a tree for a unary expression.
3936
3937tree
3938Unary_expression::do_get_tree(Translate_context* context)
3939{
e9d3367e 3940 Location loc = this->location();
3941
3942 // Taking the address of a set-and-use-temporary expression requires
3943 // setting the temporary and then taking the address.
3944 if (this->op_ == OPERATOR_AND)
3945 {
3946 Set_and_use_temporary_expression* sut =
3947 this->expr_->set_and_use_temporary_expression();
3948 if (sut != NULL)
3949 {
3950 Temporary_statement* temp = sut->temporary();
3951 Bvariable* bvar = temp->get_backend_variable(context);
3952 tree var_tree = var_to_tree(bvar);
3953 Expression* val = sut->expression();
3954 tree val_tree = val->get_tree(context);
3955 if (var_tree == error_mark_node || val_tree == error_mark_node)
3956 return error_mark_node;
3957 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
3958 var_tree);
3959 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
3960 TREE_TYPE(addr_tree),
3961 build2_loc(sut->location().gcc_location(),
3962 MODIFY_EXPR, void_type_node,
3963 var_tree, val_tree),
3964 addr_tree);
3965 }
3966 }
3967
e440a328 3968 tree expr = this->expr_->get_tree(context);
3969 if (expr == error_mark_node)
3970 return error_mark_node;
3971
e440a328 3972 switch (this->op_)
3973 {
3974 case OPERATOR_PLUS:
3975 return expr;
3976
3977 case OPERATOR_MINUS:
3978 {
3979 tree type = TREE_TYPE(expr);
3980 tree compute_type = excess_precision_type(type);
3981 if (compute_type != NULL_TREE)
3982 expr = ::convert(compute_type, expr);
b13c66cd 3983 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
e440a328 3984 (compute_type != NULL_TREE
3985 ? compute_type
3986 : type),
3987 expr);
3988 if (compute_type != NULL_TREE)
3989 ret = ::convert(type, ret);
3990 return ret;
3991 }
3992
3993 case OPERATOR_NOT:
3994 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
b13c66cd 3995 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
3996 TREE_TYPE(expr), expr);
e440a328 3997 else
b13c66cd 3998 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
3999 expr, build_int_cst(TREE_TYPE(expr), 0));
e440a328 4000
4001 case OPERATOR_XOR:
b13c66cd 4002 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4003 expr);
e440a328 4004
4005 case OPERATOR_AND:
09ea332d 4006 if (!this->create_temp_)
4007 {
4008 // We should not see a non-constant constructor here; cases
4009 // where we would see one should have been moved onto the
4010 // heap at parse time. Taking the address of a nonconstant
4011 // constructor will not do what the programmer expects.
4012 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4013 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4014 }
e440a328 4015
4016 // Build a decl for a constant constructor.
4017 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4018 {
b13c66cd 4019 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
e440a328 4020 create_tmp_var_name("C"), TREE_TYPE(expr));
4021 DECL_EXTERNAL(decl) = 0;
4022 TREE_PUBLIC(decl) = 0;
4023 TREE_READONLY(decl) = 1;
4024 TREE_CONSTANT(decl) = 1;
4025 TREE_STATIC(decl) = 1;
4026 TREE_ADDRESSABLE(decl) = 1;
4027 DECL_ARTIFICIAL(decl) = 1;
4028 DECL_INITIAL(decl) = expr;
4029 rest_of_decl_compilation(decl, 1, 0);
4030 expr = decl;
4031 }
4032
09ea332d 4033 if (this->create_temp_
4034 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
dd28fd36 4035 && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr))
09ea332d 4036 && TREE_CODE(expr) != INDIRECT_REF
4037 && TREE_CODE(expr) != COMPONENT_REF)
4038 {
fc81003d 4039 if (current_function_decl != NULL)
4040 {
4041 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4042 DECL_IGNORED_P(tmp) = 1;
4043 DECL_INITIAL(tmp) = expr;
4044 TREE_ADDRESSABLE(tmp) = 1;
4045 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4046 build_pointer_type(TREE_TYPE(expr)),
4047 build1_loc(loc.gcc_location(), DECL_EXPR,
4048 void_type_node, tmp),
4049 build_fold_addr_expr_loc(loc.gcc_location(),
4050 tmp));
4051 }
4052 else
4053 {
4054 tree tmp = build_decl(loc.gcc_location(), VAR_DECL,
4055 create_tmp_var_name("A"), TREE_TYPE(expr));
4056 DECL_EXTERNAL(tmp) = 0;
4057 TREE_PUBLIC(tmp) = 0;
4058 TREE_STATIC(tmp) = 1;
4059 DECL_ARTIFICIAL(tmp) = 1;
4060 TREE_ADDRESSABLE(tmp) = 1;
4061 tree make_tmp;
4062 if (!TREE_CONSTANT(expr))
4063 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
4064 void_type_node, tmp, expr);
4065 else
4066 {
4067 TREE_READONLY(tmp) = 1;
4068 TREE_CONSTANT(tmp) = 1;
4069 DECL_INITIAL(tmp) = expr;
4070 make_tmp = NULL_TREE;
4071 }
4072 rest_of_decl_compilation(tmp, 1, 0);
4073 tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp);
4074 if (make_tmp == NULL_TREE)
4075 return addr;
4076 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4077 TREE_TYPE(addr), make_tmp, addr);
4078 }
09ea332d 4079 }
4080
b13c66cd 4081 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
e440a328 4082
4083 case OPERATOR_MULT:
4084 {
c484d925 4085 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
e440a328 4086
4087 // If we are dereferencing the pointer to a large struct, we
4088 // need to check for nil. We don't bother to check for small
4089 // structs because we expect the system to crash on a nil
4090 // pointer dereference.
19b4f09b 4091 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr));
4092 if (!VOID_TYPE_P(target_type_tree))
e440a328 4093 {
19b4f09b 4094 HOST_WIDE_INT s = int_size_in_bytes(target_type_tree);
4095 if (s == -1 || s >= 4096)
4096 {
4097 if (!DECL_P(expr))
4098 expr = save_expr(expr);
4099 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4100 boolean_type_node,
4101 expr,
4102 fold_convert(TREE_TYPE(expr),
4103 null_pointer_node));
4104 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4105 loc);
4106 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4107 TREE_TYPE(expr), build3(COND_EXPR,
4108 void_type_node,
4109 compare, crash,
4110 NULL_TREE),
4111 expr);
4112 }
e440a328 4113 }
4114
4115 // If the type of EXPR is a recursive pointer type, then we
4116 // need to insert a cast before indirecting.
19b4f09b 4117 if (VOID_TYPE_P(target_type_tree))
e440a328 4118 {
4119 Type* pt = this->expr_->type()->points_to();
9f0e0513 4120 tree ind = type_to_tree(pt->get_backend(context->gogo()));
b13c66cd 4121 expr = fold_convert_loc(loc.gcc_location(),
4122 build_pointer_type(ind), expr);
e440a328 4123 }
4124
b13c66cd 4125 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
e440a328 4126 }
4127
4128 default:
c3e6f413 4129 go_unreachable();
e440a328 4130 }
4131}
4132
4133// Export a unary expression.
4134
4135void
4136Unary_expression::do_export(Export* exp) const
4137{
4138 switch (this->op_)
4139 {
4140 case OPERATOR_PLUS:
4141 exp->write_c_string("+ ");
4142 break;
4143 case OPERATOR_MINUS:
4144 exp->write_c_string("- ");
4145 break;
4146 case OPERATOR_NOT:
4147 exp->write_c_string("! ");
4148 break;
4149 case OPERATOR_XOR:
4150 exp->write_c_string("^ ");
4151 break;
4152 case OPERATOR_AND:
4153 case OPERATOR_MULT:
4154 default:
c3e6f413 4155 go_unreachable();
e440a328 4156 }
4157 this->expr_->export_expression(exp);
4158}
4159
4160// Import a unary expression.
4161
4162Expression*
4163Unary_expression::do_import(Import* imp)
4164{
4165 Operator op;
4166 switch (imp->get_char())
4167 {
4168 case '+':
4169 op = OPERATOR_PLUS;
4170 break;
4171 case '-':
4172 op = OPERATOR_MINUS;
4173 break;
4174 case '!':
4175 op = OPERATOR_NOT;
4176 break;
4177 case '^':
4178 op = OPERATOR_XOR;
4179 break;
4180 default:
c3e6f413 4181 go_unreachable();
e440a328 4182 }
4183 imp->require_c_string(" ");
4184 Expression* expr = Expression::import_expression(imp);
4185 return Expression::make_unary(op, expr, imp->location());
4186}
4187
d751bb78 4188// Dump ast representation of an unary expression.
4189
4190void
4191Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4192{
4193 ast_dump_context->dump_operator(this->op_);
4194 ast_dump_context->ostream() << "(";
4195 ast_dump_context->dump_expression(this->expr_);
4196 ast_dump_context->ostream() << ") ";
4197}
4198
e440a328 4199// Make a unary expression.
4200
4201Expression*
b13c66cd 4202Expression::make_unary(Operator op, Expression* expr, Location location)
e440a328 4203{
4204 return new Unary_expression(op, expr, location);
4205}
4206
4207// If this is an indirection through a pointer, return the expression
4208// being pointed through. Otherwise return this.
4209
4210Expression*
4211Expression::deref()
4212{
4213 if (this->classification_ == EXPRESSION_UNARY)
4214 {
4215 Unary_expression* ue = static_cast<Unary_expression*>(this);
4216 if (ue->op() == OPERATOR_MULT)
4217 return ue->operand();
4218 }
4219 return this;
4220}
4221
4222// Class Binary_expression.
4223
4224// Traversal.
4225
4226int
4227Binary_expression::do_traverse(Traverse* traverse)
4228{
4229 int t = Expression::traverse(&this->left_, traverse);
4230 if (t == TRAVERSE_EXIT)
4231 return TRAVERSE_EXIT;
4232 return Expression::traverse(&this->right_, traverse);
4233}
4234
0c77715b 4235// Return the type to use for a binary operation on operands of
4236// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4237// such may be NULL or abstract.
4238
4239bool
4240Binary_expression::operation_type(Operator op, Type* left_type,
4241 Type* right_type, Type** result_type)
4242{
4243 if (left_type != right_type
4244 && !left_type->is_abstract()
4245 && !right_type->is_abstract()
4246 && left_type->base() != right_type->base()
4247 && op != OPERATOR_LSHIFT
4248 && op != OPERATOR_RSHIFT)
4249 {
4250 // May be a type error--let it be diagnosed elsewhere.
4251 return false;
4252 }
4253
4254 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4255 {
4256 if (left_type->integer_type() != NULL)
4257 *result_type = left_type;
4258 else
4259 *result_type = Type::make_abstract_integer_type();
4260 }
4261 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
4262 *result_type = left_type;
4263 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
4264 *result_type = right_type;
4265 else if (!left_type->is_abstract())
4266 *result_type = left_type;
4267 else if (!right_type->is_abstract())
4268 *result_type = right_type;
4269 else if (left_type->complex_type() != NULL)
4270 *result_type = left_type;
4271 else if (right_type->complex_type() != NULL)
4272 *result_type = right_type;
4273 else if (left_type->float_type() != NULL)
4274 *result_type = left_type;
4275 else if (right_type->float_type() != NULL)
4276 *result_type = right_type;
4277 else if (left_type->integer_type() != NULL
4278 && left_type->integer_type()->is_rune())
4279 *result_type = left_type;
4280 else if (right_type->integer_type() != NULL
4281 && right_type->integer_type()->is_rune())
4282 *result_type = right_type;
4283 else
4284 *result_type = left_type;
4285
4286 return true;
4287}
4288
4289// Convert an integer comparison code and an operator to a boolean
4290// value.
e440a328 4291
4292bool
0c77715b 4293Binary_expression::cmp_to_bool(Operator op, int cmp)
e440a328 4294{
e440a328 4295 switch (op)
4296 {
4297 case OPERATOR_EQEQ:
0c77715b 4298 return cmp == 0;
4299 break;
e440a328 4300 case OPERATOR_NOTEQ:
0c77715b 4301 return cmp != 0;
4302 break;
e440a328 4303 case OPERATOR_LT:
0c77715b 4304 return cmp < 0;
4305 break;
e440a328 4306 case OPERATOR_LE:
0c77715b 4307 return cmp <= 0;
e440a328 4308 case OPERATOR_GT:
0c77715b 4309 return cmp > 0;
e440a328 4310 case OPERATOR_GE:
0c77715b 4311 return cmp >= 0;
e440a328 4312 default:
c3e6f413 4313 go_unreachable();
e440a328 4314 }
4315}
4316
0c77715b 4317// Compare constants according to OP.
e440a328 4318
4319bool
0c77715b 4320Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
4321 Numeric_constant* right_nc,
4322 Location location, bool* result)
e440a328 4323{
0c77715b 4324 Type* left_type = left_nc->type();
4325 Type* right_type = right_nc->type();
4326
4327 Type* type;
4328 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4329 return false;
4330
4331 // When comparing an untyped operand to a typed operand, we are
4332 // effectively coercing the untyped operand to the other operand's
4333 // type, so make sure that is valid.
4334 if (!left_nc->set_type(type, true, location)
4335 || !right_nc->set_type(type, true, location))
4336 return false;
4337
4338 bool ret;
4339 int cmp;
4340 if (type->complex_type() != NULL)
4341 {
4342 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
4343 return false;
4344 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
4345 }
4346 else if (type->float_type() != NULL)
4347 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
e440a328 4348 else
0c77715b 4349 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
4350
4351 if (ret)
4352 *result = Binary_expression::cmp_to_bool(op, cmp);
4353
4354 return ret;
4355}
4356
4357// Compare integer constants.
4358
4359bool
4360Binary_expression::compare_integer(const Numeric_constant* left_nc,
4361 const Numeric_constant* right_nc,
4362 int* cmp)
4363{
4364 mpz_t left_val;
4365 if (!left_nc->to_int(&left_val))
4366 return false;
4367 mpz_t right_val;
4368 if (!right_nc->to_int(&right_val))
e440a328 4369 {
0c77715b 4370 mpz_clear(left_val);
4371 return false;
e440a328 4372 }
0c77715b 4373
4374 *cmp = mpz_cmp(left_val, right_val);
4375
4376 mpz_clear(left_val);
4377 mpz_clear(right_val);
4378
4379 return true;
4380}
4381
4382// Compare floating point constants.
4383
4384bool
4385Binary_expression::compare_float(const Numeric_constant* left_nc,
4386 const Numeric_constant* right_nc,
4387 int* cmp)
4388{
4389 mpfr_t left_val;
4390 if (!left_nc->to_float(&left_val))
4391 return false;
4392 mpfr_t right_val;
4393 if (!right_nc->to_float(&right_val))
e440a328 4394 {
0c77715b 4395 mpfr_clear(left_val);
4396 return false;
4397 }
4398
4399 // We already coerced both operands to the same type. If that type
4400 // is not an abstract type, we need to round the values accordingly.
4401 Type* type = left_nc->type();
4402 if (!type->is_abstract() && type->float_type() != NULL)
4403 {
4404 int bits = type->float_type()->bits();
4405 mpfr_prec_round(left_val, bits, GMP_RNDN);
4406 mpfr_prec_round(right_val, bits, GMP_RNDN);
e440a328 4407 }
0c77715b 4408
4409 *cmp = mpfr_cmp(left_val, right_val);
4410
4411 mpfr_clear(left_val);
4412 mpfr_clear(right_val);
4413
4414 return true;
e440a328 4415}
4416
0c77715b 4417// Compare complex constants. Complex numbers may only be compared
4418// for equality.
e440a328 4419
4420bool
0c77715b 4421Binary_expression::compare_complex(const Numeric_constant* left_nc,
4422 const Numeric_constant* right_nc,
4423 int* cmp)
e440a328 4424{
0c77715b 4425 mpfr_t left_real, left_imag;
4426 if (!left_nc->to_complex(&left_real, &left_imag))
4427 return false;
4428 mpfr_t right_real, right_imag;
4429 if (!right_nc->to_complex(&right_real, &right_imag))
e440a328 4430 {
0c77715b 4431 mpfr_clear(left_real);
4432 mpfr_clear(left_imag);
4433 return false;
e440a328 4434 }
0c77715b 4435
4436 // We already coerced both operands to the same type. If that type
4437 // is not an abstract type, we need to round the values accordingly.
4438 Type* type = left_nc->type();
4439 if (!type->is_abstract() && type->complex_type() != NULL)
e440a328 4440 {
0c77715b 4441 int bits = type->complex_type()->bits();
4442 mpfr_prec_round(left_real, bits / 2, GMP_RNDN);
4443 mpfr_prec_round(left_imag, bits / 2, GMP_RNDN);
4444 mpfr_prec_round(right_real, bits / 2, GMP_RNDN);
4445 mpfr_prec_round(right_imag, bits / 2, GMP_RNDN);
e440a328 4446 }
0c77715b 4447
4448 *cmp = (mpfr_cmp(left_real, right_real) != 0
4449 || mpfr_cmp(left_imag, right_imag) != 0);
4450
4451 mpfr_clear(left_real);
4452 mpfr_clear(left_imag);
4453 mpfr_clear(right_real);
4454 mpfr_clear(right_imag);
4455
4456 return true;
e440a328 4457}
4458
0c77715b 4459// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4460// true if this could be done, false if not. Issue errors at LOCATION
4461// as appropriate.
e440a328 4462
4463bool
0c77715b 4464Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
4465 Numeric_constant* right_nc,
4466 Location location, Numeric_constant* nc)
e440a328 4467{
e440a328 4468 switch (op)
4469 {
4470 case OPERATOR_OROR:
4471 case OPERATOR_ANDAND:
4472 case OPERATOR_EQEQ:
4473 case OPERATOR_NOTEQ:
4474 case OPERATOR_LT:
4475 case OPERATOR_LE:
4476 case OPERATOR_GT:
4477 case OPERATOR_GE:
0c77715b 4478 // These return boolean values and as such must be handled
4479 // elsewhere.
4480 go_unreachable();
4481 default:
4482 break;
4483 }
4484
4485 Type* left_type = left_nc->type();
4486 Type* right_type = right_nc->type();
4487
4488 Type* type;
4489 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4490 return false;
4491
4492 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
4493
4494 // When combining an untyped operand with a typed operand, we are
4495 // effectively coercing the untyped operand to the other operand's
4496 // type, so make sure that is valid.
4497 if (!left_nc->set_type(type, true, location))
4498 return false;
4499 if (!is_shift && !right_nc->set_type(type, true, location))
4500 return false;
4501
4502 bool r;
4503 if (type->complex_type() != NULL)
4504 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc);
4505 else if (type->float_type() != NULL)
4506 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc);
4507 else
4508 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
4509
4510 if (r)
4511 r = nc->set_type(type, true, location);
4512
4513 return r;
4514}
4515
4516// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4517// integer operations. Return true if this could be done, false if
4518// not.
4519
4520bool
4521Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
4522 const Numeric_constant* right_nc,
4523 Location location, Numeric_constant* nc)
4524{
4525 mpz_t left_val;
4526 if (!left_nc->to_int(&left_val))
4527 return false;
4528 mpz_t right_val;
4529 if (!right_nc->to_int(&right_val))
4530 {
4531 mpz_clear(left_val);
e440a328 4532 return false;
0c77715b 4533 }
4534
4535 mpz_t val;
4536 mpz_init(val);
4537
4538 switch (op)
4539 {
e440a328 4540 case OPERATOR_PLUS:
4541 mpz_add(val, left_val, right_val);
4542 break;
4543 case OPERATOR_MINUS:
4544 mpz_sub(val, left_val, right_val);
4545 break;
4546 case OPERATOR_OR:
4547 mpz_ior(val, left_val, right_val);
4548 break;
4549 case OPERATOR_XOR:
4550 mpz_xor(val, left_val, right_val);
4551 break;
4552 case OPERATOR_MULT:
4553 mpz_mul(val, left_val, right_val);
4554 break;
4555 case OPERATOR_DIV:
4556 if (mpz_sgn(right_val) != 0)
4557 mpz_tdiv_q(val, left_val, right_val);
4558 else
4559 {
4560 error_at(location, "division by zero");
4561 mpz_set_ui(val, 0);
e440a328 4562 }
4563 break;
4564 case OPERATOR_MOD:
4565 if (mpz_sgn(right_val) != 0)
4566 mpz_tdiv_r(val, left_val, right_val);
4567 else
4568 {
4569 error_at(location, "division by zero");
4570 mpz_set_ui(val, 0);
e440a328 4571 }
4572 break;
4573 case OPERATOR_LSHIFT:
4574 {
4575 unsigned long shift = mpz_get_ui(right_val);
0c77715b 4576 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
4577 mpz_mul_2exp(val, left_val, shift);
4578 else
e440a328 4579 {
4580 error_at(location, "shift count overflow");
4581 mpz_set_ui(val, 0);
e440a328 4582 }
e440a328 4583 break;
4584 }
4585 break;
4586 case OPERATOR_RSHIFT:
4587 {
4588 unsigned long shift = mpz_get_ui(right_val);
4589 if (mpz_cmp_ui(right_val, shift) != 0)
4590 {
4591 error_at(location, "shift count overflow");
4592 mpz_set_ui(val, 0);
e440a328 4593 }
e440a328 4594 else
0c77715b 4595 {
4596 if (mpz_cmp_ui(left_val, 0) >= 0)
4597 mpz_tdiv_q_2exp(val, left_val, shift);
4598 else
4599 mpz_fdiv_q_2exp(val, left_val, shift);
4600 }
e440a328 4601 break;
4602 }
4603 break;
4604 case OPERATOR_AND:
4605 mpz_and(val, left_val, right_val);
4606 break;
4607 case OPERATOR_BITCLEAR:
4608 {
4609 mpz_t tval;
4610 mpz_init(tval);
4611 mpz_com(tval, right_val);
4612 mpz_and(val, left_val, tval);
4613 mpz_clear(tval);
4614 }
4615 break;
4616 default:
c3e6f413 4617 go_unreachable();
e440a328 4618 }
4619
0c77715b 4620 mpz_clear(left_val);
4621 mpz_clear(right_val);
e440a328 4622
0c77715b 4623 if (left_nc->is_rune()
4624 || (op != OPERATOR_LSHIFT
4625 && op != OPERATOR_RSHIFT
4626 && right_nc->is_rune()))
4627 nc->set_rune(NULL, val);
4628 else
4629 nc->set_int(NULL, val);
4630
4631 mpz_clear(val);
e440a328 4632
4633 return true;
4634}
4635
0c77715b 4636// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4637// floating point operations. Return true if this could be done,
4638// false if not.
e440a328 4639
4640bool
0c77715b 4641Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
4642 const Numeric_constant* right_nc,
4643 Location location, Numeric_constant* nc)
e440a328 4644{
0c77715b 4645 mpfr_t left_val;
4646 if (!left_nc->to_float(&left_val))
4647 return false;
4648 mpfr_t right_val;
4649 if (!right_nc->to_float(&right_val))
e440a328 4650 {
0c77715b 4651 mpfr_clear(left_val);
e440a328 4652 return false;
0c77715b 4653 }
4654
4655 mpfr_t val;
4656 mpfr_init(val);
4657
4658 bool ret = true;
4659 switch (op)
4660 {
e440a328 4661 case OPERATOR_PLUS:
4662 mpfr_add(val, left_val, right_val, GMP_RNDN);
4663 break;
4664 case OPERATOR_MINUS:
4665 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4666 break;
4667 case OPERATOR_OR:
4668 case OPERATOR_XOR:
4669 case OPERATOR_AND:
4670 case OPERATOR_BITCLEAR:
0c77715b 4671 case OPERATOR_MOD:
4672 case OPERATOR_LSHIFT:
4673 case OPERATOR_RSHIFT:
4674 mpfr_set_ui(val, 0, GMP_RNDN);
4675 ret = false;
4676 break;
e440a328 4677 case OPERATOR_MULT:
4678 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4679 break;
4680 case OPERATOR_DIV:
0c77715b 4681 if (!mpfr_zero_p(right_val))
4682 mpfr_div(val, left_val, right_val, GMP_RNDN);
4683 else
4684 {
4685 error_at(location, "division by zero");
4686 mpfr_set_ui(val, 0, GMP_RNDN);
4687 }
e440a328 4688 break;
e440a328 4689 default:
c3e6f413 4690 go_unreachable();
e440a328 4691 }
4692
0c77715b 4693 mpfr_clear(left_val);
4694 mpfr_clear(right_val);
e440a328 4695
0c77715b 4696 nc->set_float(NULL, val);
4697 mpfr_clear(val);
e440a328 4698
0c77715b 4699 return ret;
e440a328 4700}
4701
0c77715b 4702// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4703// complex operations. Return true if this could be done, false if
4704// not.
e440a328 4705
4706bool
0c77715b 4707Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
4708 const Numeric_constant* right_nc,
4709 Location location, Numeric_constant* nc)
e440a328 4710{
0c77715b 4711 mpfr_t left_real, left_imag;
4712 if (!left_nc->to_complex(&left_real, &left_imag))
4713 return false;
4714 mpfr_t right_real, right_imag;
4715 if (!right_nc->to_complex(&right_real, &right_imag))
e440a328 4716 {
0c77715b 4717 mpfr_clear(left_real);
4718 mpfr_clear(left_imag);
e440a328 4719 return false;
0c77715b 4720 }
4721
4722 mpfr_t real, imag;
4723 mpfr_init(real);
4724 mpfr_init(imag);
4725
4726 bool ret = true;
4727 switch (op)
4728 {
e440a328 4729 case OPERATOR_PLUS:
4730 mpfr_add(real, left_real, right_real, GMP_RNDN);
4731 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4732 break;
4733 case OPERATOR_MINUS:
4734 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4735 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4736 break;
4737 case OPERATOR_OR:
4738 case OPERATOR_XOR:
4739 case OPERATOR_AND:
4740 case OPERATOR_BITCLEAR:
0c77715b 4741 case OPERATOR_MOD:
4742 case OPERATOR_LSHIFT:
4743 case OPERATOR_RSHIFT:
4744 mpfr_set_ui(real, 0, GMP_RNDN);
4745 mpfr_set_ui(imag, 0, GMP_RNDN);
4746 ret = false;
4747 break;
e440a328 4748 case OPERATOR_MULT:
4749 {
4750 // You might think that multiplying two complex numbers would
4751 // be simple, and you would be right, until you start to think
4752 // about getting the right answer for infinity. If one
4753 // operand here is infinity and the other is anything other
4754 // than zero or NaN, then we are going to wind up subtracting
4755 // two infinity values. That will give us a NaN, but the
4756 // correct answer is infinity.
4757
4758 mpfr_t lrrr;
4759 mpfr_init(lrrr);
4760 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4761
4762 mpfr_t lrri;
4763 mpfr_init(lrri);
4764 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4765
4766 mpfr_t lirr;
4767 mpfr_init(lirr);
4768 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4769
4770 mpfr_t liri;
4771 mpfr_init(liri);
4772 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4773
4774 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4775 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4776
4777 // If we get NaN on both sides, check whether it should really
4778 // be infinity. The rule is that if either side of the
4779 // complex number is infinity, then the whole value is
4780 // infinity, even if the other side is NaN. So the only case
4781 // we have to fix is the one in which both sides are NaN.
4782 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4783 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4784 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4785 {
4786 bool is_infinity = false;
4787
4788 mpfr_t lr;
4789 mpfr_t li;
4790 mpfr_init_set(lr, left_real, GMP_RNDN);
4791 mpfr_init_set(li, left_imag, GMP_RNDN);
4792
4793 mpfr_t rr;
4794 mpfr_t ri;
4795 mpfr_init_set(rr, right_real, GMP_RNDN);
4796 mpfr_init_set(ri, right_imag, GMP_RNDN);
4797
4798 // If the left side is infinity, then the result is
4799 // infinity.
4800 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4801 {
4802 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4803 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4804 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4805 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4806 if (mpfr_nan_p(rr))
4807 {
4808 mpfr_set_ui(rr, 0, GMP_RNDN);
4809 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4810 }
4811 if (mpfr_nan_p(ri))
4812 {
4813 mpfr_set_ui(ri, 0, GMP_RNDN);
4814 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4815 }
4816 is_infinity = true;
4817 }
4818
4819 // If the right side is infinity, then the result is
4820 // infinity.
4821 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4822 {
4823 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4824 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4825 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4826 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4827 if (mpfr_nan_p(lr))
4828 {
4829 mpfr_set_ui(lr, 0, GMP_RNDN);
4830 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4831 }
4832 if (mpfr_nan_p(li))
4833 {
4834 mpfr_set_ui(li, 0, GMP_RNDN);
4835 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4836 }
4837 is_infinity = true;
4838 }
4839
4840 // If we got an overflow in the intermediate computations,
4841 // then the result is infinity.
4842 if (!is_infinity
4843 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4844 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4845 {
4846 if (mpfr_nan_p(lr))
4847 {
4848 mpfr_set_ui(lr, 0, GMP_RNDN);
4849 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4850 }
4851 if (mpfr_nan_p(li))
4852 {
4853 mpfr_set_ui(li, 0, GMP_RNDN);
4854 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4855 }
4856 if (mpfr_nan_p(rr))
4857 {
4858 mpfr_set_ui(rr, 0, GMP_RNDN);
4859 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4860 }
4861 if (mpfr_nan_p(ri))
4862 {
4863 mpfr_set_ui(ri, 0, GMP_RNDN);
4864 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4865 }
4866 is_infinity = true;
4867 }
4868
4869 if (is_infinity)
4870 {
4871 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4872 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4873 mpfr_mul(lirr, li, rr, GMP_RNDN);
4874 mpfr_mul(liri, li, ri, GMP_RNDN);
4875 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4876 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4877 mpfr_set_inf(real, mpfr_sgn(real));
4878 mpfr_set_inf(imag, mpfr_sgn(imag));
4879 }
4880
4881 mpfr_clear(lr);
4882 mpfr_clear(li);
4883 mpfr_clear(rr);
4884 mpfr_clear(ri);
4885 }
4886
4887 mpfr_clear(lrrr);
4888 mpfr_clear(lrri);
4889 mpfr_clear(lirr);
4890 mpfr_clear(liri);
4891 }
4892 break;
4893 case OPERATOR_DIV:
4894 {
4895 // For complex division we want to avoid having an
4896 // intermediate overflow turn the whole result in a NaN. We
4897 // scale the values to try to avoid this.
4898
4899 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
0c77715b 4900 {
4901 error_at(location, "division by zero");
4902 mpfr_set_ui(real, 0, GMP_RNDN);
4903 mpfr_set_ui(imag, 0, GMP_RNDN);
4904 break;
4905 }
e440a328 4906
4907 mpfr_t rra;
4908 mpfr_t ria;
4909 mpfr_init(rra);
4910 mpfr_init(ria);
4911 mpfr_abs(rra, right_real, GMP_RNDN);
4912 mpfr_abs(ria, right_imag, GMP_RNDN);
4913 mpfr_t t;
4914 mpfr_init(t);
4915 mpfr_max(t, rra, ria, GMP_RNDN);
4916
4917 mpfr_t rr;
4918 mpfr_t ri;
4919 mpfr_init_set(rr, right_real, GMP_RNDN);
4920 mpfr_init_set(ri, right_imag, GMP_RNDN);
4921 long ilogbw = 0;
4922 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4923 {
4924 ilogbw = mpfr_get_exp(t);
4925 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4926 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4927 }
4928
4929 mpfr_t denom;
4930 mpfr_init(denom);
4931 mpfr_mul(denom, rr, rr, GMP_RNDN);
4932 mpfr_mul(t, ri, ri, GMP_RNDN);
4933 mpfr_add(denom, denom, t, GMP_RNDN);
4934
4935 mpfr_mul(real, left_real, rr, GMP_RNDN);
4936 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4937 mpfr_add(real, real, t, GMP_RNDN);
4938 mpfr_div(real, real, denom, GMP_RNDN);
4939 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4940
4941 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4942 mpfr_mul(t, left_real, ri, GMP_RNDN);
4943 mpfr_sub(imag, imag, t, GMP_RNDN);
4944 mpfr_div(imag, imag, denom, GMP_RNDN);
4945 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4946
4947 // If we wind up with NaN on both sides, check whether we
4948 // should really have infinity. The rule is that if either
4949 // side of the complex number is infinity, then the whole
4950 // value is infinity, even if the other side is NaN. So the
4951 // only case we have to fix is the one in which both sides are
4952 // NaN.
4953 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4954 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4955 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4956 {
4957 if (mpfr_zero_p(denom))
4958 {
4959 mpfr_set_inf(real, mpfr_sgn(rr));
4960 mpfr_mul(real, real, left_real, GMP_RNDN);
4961 mpfr_set_inf(imag, mpfr_sgn(rr));
4962 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4963 }
4964 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4965 && mpfr_number_p(rr) && mpfr_number_p(ri))
4966 {
4967 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4968 mpfr_copysign(t, t, left_real, GMP_RNDN);
4969
4970 mpfr_t t2;
4971 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4972 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4973
4974 mpfr_t t3;
4975 mpfr_init(t3);
4976 mpfr_mul(t3, t, rr, GMP_RNDN);
4977
4978 mpfr_t t4;
4979 mpfr_init(t4);
4980 mpfr_mul(t4, t2, ri, GMP_RNDN);
4981
4982 mpfr_add(t3, t3, t4, GMP_RNDN);
4983 mpfr_set_inf(real, mpfr_sgn(t3));
4984
4985 mpfr_mul(t3, t2, rr, GMP_RNDN);
4986 mpfr_mul(t4, t, ri, GMP_RNDN);
4987 mpfr_sub(t3, t3, t4, GMP_RNDN);
4988 mpfr_set_inf(imag, mpfr_sgn(t3));
4989
4990 mpfr_clear(t2);
4991 mpfr_clear(t3);
4992 mpfr_clear(t4);
4993 }
4994 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4995 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4996 {
4997 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4998 mpfr_copysign(t, t, rr, GMP_RNDN);
4999
5000 mpfr_t t2;
5001 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5002 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5003
5004 mpfr_t t3;
5005 mpfr_init(t3);
5006 mpfr_mul(t3, left_real, t, GMP_RNDN);
5007
5008 mpfr_t t4;
5009 mpfr_init(t4);
5010 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5011
5012 mpfr_add(t3, t3, t4, GMP_RNDN);
5013 mpfr_set_ui(real, 0, GMP_RNDN);
5014 mpfr_mul(real, real, t3, GMP_RNDN);
5015
5016 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5017 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5018 mpfr_sub(t3, t3, t4, GMP_RNDN);
5019 mpfr_set_ui(imag, 0, GMP_RNDN);
5020 mpfr_mul(imag, imag, t3, GMP_RNDN);
5021
5022 mpfr_clear(t2);
5023 mpfr_clear(t3);
5024 mpfr_clear(t4);
5025 }
5026 }
5027
5028 mpfr_clear(denom);
5029 mpfr_clear(rr);
5030 mpfr_clear(ri);
5031 mpfr_clear(t);
5032 mpfr_clear(rra);
5033 mpfr_clear(ria);
5034 }
5035 break;
e440a328 5036 default:
c3e6f413 5037 go_unreachable();
e440a328 5038 }
5039
0c77715b 5040 mpfr_clear(left_real);
5041 mpfr_clear(left_imag);
5042 mpfr_clear(right_real);
5043 mpfr_clear(right_imag);
e440a328 5044
0c77715b 5045 nc->set_complex(NULL, real, imag);
5046 mpfr_clear(real);
5047 mpfr_clear(imag);
e440a328 5048
0c77715b 5049 return ret;
e440a328 5050}
5051
5052// Lower a binary expression. We have to evaluate constant
5053// expressions now, in order to implement Go's unlimited precision
5054// constants.
5055
5056Expression*
e9d3367e 5057Binary_expression::do_lower(Gogo* gogo, Named_object*,
5058 Statement_inserter* inserter, int)
e440a328 5059{
b13c66cd 5060 Location location = this->location();
e440a328 5061 Operator op = this->op_;
5062 Expression* left = this->left_;
5063 Expression* right = this->right_;
5064
5065 const bool is_comparison = (op == OPERATOR_EQEQ
5066 || op == OPERATOR_NOTEQ
5067 || op == OPERATOR_LT
5068 || op == OPERATOR_LE
5069 || op == OPERATOR_GT
5070 || op == OPERATOR_GE);
5071
0c77715b 5072 // Numeric constant expressions.
e440a328 5073 {
0c77715b 5074 Numeric_constant left_nc;
5075 Numeric_constant right_nc;
5076 if (left->numeric_constant_value(&left_nc)
5077 && right->numeric_constant_value(&right_nc))
e440a328 5078 {
0c77715b 5079 if (is_comparison)
e440a328 5080 {
0c77715b 5081 bool result;
5082 if (!Binary_expression::compare_constant(op, &left_nc,
5083 &right_nc, location,
5084 &result))
5085 return this;
5086 return Expression::make_cast(Type::lookup_bool_type(),
5087 Expression::make_boolean(result,
5088 location),
5089 location);
e440a328 5090 }
5091 else
5092 {
0c77715b 5093 Numeric_constant nc;
5094 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
5095 location, &nc))
5096 return this;
5097 return nc.expression(location);
e440a328 5098 }
5099 }
e440a328 5100 }
5101
5102 // String constant expressions.
315fa98d 5103 if (left->type()->is_string_type() && right->type()->is_string_type())
e440a328 5104 {
5105 std::string left_string;
5106 std::string right_string;
5107 if (left->string_constant_value(&left_string)
5108 && right->string_constant_value(&right_string))
315fa98d 5109 {
5110 if (op == OPERATOR_PLUS)
5111 return Expression::make_string(left_string + right_string,
5112 location);
5113 else if (is_comparison)
5114 {
5115 int cmp = left_string.compare(right_string);
0c77715b 5116 bool r = Binary_expression::cmp_to_bool(op, cmp);
5117 return Expression::make_cast(Type::lookup_bool_type(),
5118 Expression::make_boolean(r,
5119 location),
5120 location);
b40dc774 5121 }
5122 }
b40dc774 5123 }
5124
e9d3367e 5125 // Lower struct and array comparisons.
5126 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5127 {
5128 if (left->type()->struct_type() != NULL)
5129 return this->lower_struct_comparison(gogo, inserter);
5130 else if (left->type()->array_type() != NULL
5131 && !left->type()->is_slice_type())
5132 return this->lower_array_comparison(gogo, inserter);
5133 }
5134
e440a328 5135 return this;
5136}
5137
e9d3367e 5138// Lower a struct comparison.
5139
5140Expression*
5141Binary_expression::lower_struct_comparison(Gogo* gogo,
5142 Statement_inserter* inserter)
5143{
5144 Struct_type* st = this->left_->type()->struct_type();
5145 Struct_type* st2 = this->right_->type()->struct_type();
5146 if (st2 == NULL)
5147 return this;
5148 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5149 return this;
5150 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5151 this->right_->type(), NULL))
5152 return this;
5153
5154 // See if we can compare using memcmp. As a heuristic, we use
5155 // memcmp rather than field references and comparisons if there are
5156 // more than two fields.
113ef6a5 5157 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
e9d3367e 5158 return this->lower_compare_to_memcmp(gogo, inserter);
5159
5160 Location loc = this->location();
5161
5162 Expression* left = this->left_;
5163 Temporary_statement* left_temp = NULL;
5164 if (left->var_expression() == NULL
5165 && left->temporary_reference_expression() == NULL)
5166 {
5167 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5168 inserter->insert(left_temp);
5169 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5170 }
5171
5172 Expression* right = this->right_;
5173 Temporary_statement* right_temp = NULL;
5174 if (right->var_expression() == NULL
5175 && right->temporary_reference_expression() == NULL)
5176 {
5177 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5178 inserter->insert(right_temp);
5179 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5180 }
5181
5182 Expression* ret = Expression::make_boolean(true, loc);
5183 const Struct_field_list* fields = st->fields();
5184 unsigned int field_index = 0;
5185 for (Struct_field_list::const_iterator pf = fields->begin();
5186 pf != fields->end();
5187 ++pf, ++field_index)
5188 {
5189 if (field_index > 0)
5190 {
5191 if (left_temp == NULL)
5192 left = left->copy();
5193 else
5194 left = Expression::make_temporary_reference(left_temp, loc);
5195 if (right_temp == NULL)
5196 right = right->copy();
5197 else
5198 right = Expression::make_temporary_reference(right_temp, loc);
5199 }
5200 Expression* f1 = Expression::make_field_reference(left, field_index,
5201 loc);
5202 Expression* f2 = Expression::make_field_reference(right, field_index,
5203 loc);
5204 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5205 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5206 }
5207
5208 if (this->op_ == OPERATOR_NOTEQ)
5209 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5210
5211 return ret;
5212}
5213
5214// Lower an array comparison.
5215
5216Expression*
5217Binary_expression::lower_array_comparison(Gogo* gogo,
5218 Statement_inserter* inserter)
5219{
5220 Array_type* at = this->left_->type()->array_type();
5221 Array_type* at2 = this->right_->type()->array_type();
5222 if (at2 == NULL)
5223 return this;
5224 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5225 return this;
5226 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5227 this->right_->type(), NULL))
5228 return this;
5229
5230 // Call memcmp directly if possible. This may let the middle-end
5231 // optimize the call.
113ef6a5 5232 if (at->compare_is_identity(gogo))
e9d3367e 5233 return this->lower_compare_to_memcmp(gogo, inserter);
5234
5235 // Call the array comparison function.
5236 Named_object* hash_fn;
5237 Named_object* equal_fn;
5238 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5239 &hash_fn, &equal_fn);
5240
5241 Location loc = this->location();
5242
5243 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5244
5245 Expression_list* args = new Expression_list();
5246 args->push_back(this->operand_address(inserter, this->left_));
5247 args->push_back(this->operand_address(inserter, this->right_));
5248 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
5249
5250 Expression* ret = Expression::make_call(func, args, false, loc);
5251
5252 if (this->op_ == OPERATOR_NOTEQ)
5253 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5254
5255 return ret;
5256}
5257
5258// Lower a struct or array comparison to a call to memcmp.
5259
5260Expression*
5261Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5262{
5263 Location loc = this->location();
5264
5265 Expression* a1 = this->operand_address(inserter, this->left_);
5266 Expression* a2 = this->operand_address(inserter, this->right_);
5267 Expression* len = Expression::make_type_info(this->left_->type(),
5268 TYPE_INFO_SIZE);
5269
5270 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
5271
5272 mpz_t zval;
5273 mpz_init_set_ui(zval, 0);
5274 Expression* zero = Expression::make_integer(&zval, NULL, loc);
5275 mpz_clear(zval);
5276
5277 return Expression::make_binary(this->op_, call, zero, loc);
5278}
5279
5280// Return the address of EXPR, cast to unsafe.Pointer.
5281
5282Expression*
5283Binary_expression::operand_address(Statement_inserter* inserter,
5284 Expression* expr)
5285{
5286 Location loc = this->location();
5287
5288 if (!expr->is_addressable())
5289 {
5290 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5291 loc);
5292 inserter->insert(temp);
5293 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5294 }
5295 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5296 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5297 Type* void_type = Type::make_void_type();
5298 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5299 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5300}
5301
0c77715b 5302// Return the numeric constant value, if it has one.
e440a328 5303
5304bool
0c77715b 5305Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 5306{
0c77715b 5307 Operator op = this->op_;
e440a328 5308
0c77715b 5309 if (op == OPERATOR_EQEQ
5310 || op == OPERATOR_NOTEQ
5311 || op == OPERATOR_LT
5312 || op == OPERATOR_LE
5313 || op == OPERATOR_GT
5314 || op == OPERATOR_GE)
5315 return false;
e440a328 5316
0c77715b 5317 Numeric_constant left_nc;
5318 if (!this->left_->numeric_constant_value(&left_nc))
5319 return false;
5320 Numeric_constant right_nc;
5321 if (!this->right_->numeric_constant_value(&right_nc))
5322 return false;
e440a328 5323
0c77715b 5324 return Binary_expression::eval_constant(op, &left_nc, &right_nc,
5325 this->location(), nc);
e440a328 5326}
5327
5328// Note that the value is being discarded.
5329
5330void
5331Binary_expression::do_discarding_value()
5332{
5333 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5334 this->right_->discarding_value();
5335 else
a7549a6a 5336 this->unused_value_error();
e440a328 5337}
5338
5339// Get type.
5340
5341Type*
5342Binary_expression::do_type()
5343{
5f5fea79 5344 if (this->classification() == EXPRESSION_ERROR)
5345 return Type::make_error_type();
5346
e440a328 5347 switch (this->op_)
5348 {
5349 case OPERATOR_OROR:
5350 case OPERATOR_ANDAND:
5351 case OPERATOR_EQEQ:
5352 case OPERATOR_NOTEQ:
5353 case OPERATOR_LT:
5354 case OPERATOR_LE:
5355 case OPERATOR_GT:
5356 case OPERATOR_GE:
5357 return Type::lookup_bool_type();
5358
5359 case OPERATOR_PLUS:
5360 case OPERATOR_MINUS:
5361 case OPERATOR_OR:
5362 case OPERATOR_XOR:
5363 case OPERATOR_MULT:
5364 case OPERATOR_DIV:
5365 case OPERATOR_MOD:
5366 case OPERATOR_AND:
5367 case OPERATOR_BITCLEAR:
5368 {
0c77715b 5369 Type* type;
5370 if (!Binary_expression::operation_type(this->op_,
5371 this->left_->type(),
5372 this->right_->type(),
5373 &type))
5374 return Type::make_error_type();
5375 return type;
e440a328 5376 }
5377
5378 case OPERATOR_LSHIFT:
5379 case OPERATOR_RSHIFT:
5380 return this->left_->type();
5381
5382 default:
c3e6f413 5383 go_unreachable();
e440a328 5384 }
5385}
5386
5387// Set type for a binary expression.
5388
5389void
5390Binary_expression::do_determine_type(const Type_context* context)
5391{
5392 Type* tleft = this->left_->type();
5393 Type* tright = this->right_->type();
5394
5395 // Both sides should have the same type, except for the shift
5396 // operations. For a comparison, we should ignore the incoming
5397 // type.
5398
5399 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5400 || this->op_ == OPERATOR_RSHIFT);
5401
5402 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5403 || this->op_ == OPERATOR_NOTEQ
5404 || this->op_ == OPERATOR_LT
5405 || this->op_ == OPERATOR_LE
5406 || this->op_ == OPERATOR_GT
5407 || this->op_ == OPERATOR_GE);
5408
5409 Type_context subcontext(*context);
5410
5411 if (is_comparison)
5412 {
5413 // In a comparison, the context does not determine the types of
5414 // the operands.
5415 subcontext.type = NULL;
5416 }
5417
5418 // Set the context for the left hand operand.
5419 if (is_shift_op)
5420 {
b40dc774 5421 // The right hand operand of a shift plays no role in
5422 // determining the type of the left hand operand.
e440a328 5423 }
5424 else if (!tleft->is_abstract())
5425 subcontext.type = tleft;
5426 else if (!tright->is_abstract())
5427 subcontext.type = tright;
5428 else if (subcontext.type == NULL)
5429 {
5430 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5431 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5432 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5433 {
5434 // Both sides have an abstract integer, abstract float, or
5435 // abstract complex type. Just let CONTEXT determine
5436 // whether they may remain abstract or not.
5437 }
5438 else if (tleft->complex_type() != NULL)
5439 subcontext.type = tleft;
5440 else if (tright->complex_type() != NULL)
5441 subcontext.type = tright;
5442 else if (tleft->float_type() != NULL)
5443 subcontext.type = tleft;
5444 else if (tright->float_type() != NULL)
5445 subcontext.type = tright;
5446 else
5447 subcontext.type = tleft;
f58a23ae 5448
5449 if (subcontext.type != NULL && !context->may_be_abstract)
5450 subcontext.type = subcontext.type->make_non_abstract_type();
e440a328 5451 }
5452
5453 this->left_->determine_type(&subcontext);
5454
e440a328 5455 if (is_shift_op)
5456 {
b40dc774 5457 // We may have inherited an unusable type for the shift operand.
5458 // Give a useful error if that happened.
5459 if (tleft->is_abstract()
5460 && subcontext.type != NULL
5461 && (this->left_->type()->integer_type() == NULL
5462 || (subcontext.type->integer_type() == NULL
5463 && subcontext.type->float_type() == NULL
5464 && subcontext.type->complex_type() == NULL)))
5465 this->report_error(("invalid context-determined non-integer type "
5466 "for shift operand"));
5467
5468 // The context for the right hand operand is the same as for the
5469 // left hand operand, except for a shift operator.
e440a328 5470 subcontext.type = Type::lookup_integer_type("uint");
5471 subcontext.may_be_abstract = false;
5472 }
5473
5474 this->right_->determine_type(&subcontext);
5475}
5476
5477// Report an error if the binary operator OP does not support TYPE.
be8b5eee 5478// OTYPE is the type of the other operand. Return whether the
5479// operation is OK. This should not be used for shift.
e440a328 5480
5481bool
be8b5eee 5482Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
b13c66cd 5483 Location location)
e440a328 5484{
5485 switch (op)
5486 {
5487 case OPERATOR_OROR:
5488 case OPERATOR_ANDAND:
5489 if (!type->is_boolean_type())
5490 {
5491 error_at(location, "expected boolean type");
5492 return false;
5493 }
5494 break;
5495
5496 case OPERATOR_EQEQ:
5497 case OPERATOR_NOTEQ:
e9d3367e 5498 {
5499 std::string reason;
5500 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
5501 {
5502 error_at(location, "%s", reason.c_str());
5503 return false;
5504 }
5505 }
e440a328 5506 break;
5507
5508 case OPERATOR_LT:
5509 case OPERATOR_LE:
5510 case OPERATOR_GT:
5511 case OPERATOR_GE:
e9d3367e 5512 {
5513 std::string reason;
5514 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
5515 {
5516 error_at(location, "%s", reason.c_str());
5517 return false;
5518 }
5519 }
e440a328 5520 break;
5521
5522 case OPERATOR_PLUS:
5523 case OPERATOR_PLUSEQ:
5524 if (type->integer_type() == NULL
5525 && type->float_type() == NULL
5526 && type->complex_type() == NULL
5527 && !type->is_string_type())
5528 {
5529 error_at(location,
5530 "expected integer, floating, complex, or string type");
5531 return false;
5532 }
5533 break;
5534
5535 case OPERATOR_MINUS:
5536 case OPERATOR_MINUSEQ:
5537 case OPERATOR_MULT:
5538 case OPERATOR_MULTEQ:
5539 case OPERATOR_DIV:
5540 case OPERATOR_DIVEQ:
5541 if (type->integer_type() == NULL
5542 && type->float_type() == NULL
5543 && type->complex_type() == NULL)
5544 {
5545 error_at(location, "expected integer, floating, or complex type");
5546 return false;
5547 }
5548 break;
5549
5550 case OPERATOR_MOD:
5551 case OPERATOR_MODEQ:
5552 case OPERATOR_OR:
5553 case OPERATOR_OREQ:
5554 case OPERATOR_AND:
5555 case OPERATOR_ANDEQ:
5556 case OPERATOR_XOR:
5557 case OPERATOR_XOREQ:
5558 case OPERATOR_BITCLEAR:
5559 case OPERATOR_BITCLEAREQ:
5560 if (type->integer_type() == NULL)
5561 {
5562 error_at(location, "expected integer type");
5563 return false;
5564 }
5565 break;
5566
5567 default:
c3e6f413 5568 go_unreachable();
e440a328 5569 }
5570
5571 return true;
5572}
5573
5574// Check types.
5575
5576void
5577Binary_expression::do_check_types(Gogo*)
5578{
5f5fea79 5579 if (this->classification() == EXPRESSION_ERROR)
5580 return;
5581
e440a328 5582 Type* left_type = this->left_->type();
5583 Type* right_type = this->right_->type();
5c13bd80 5584 if (left_type->is_error() || right_type->is_error())
9fe897ef 5585 {
5586 this->set_is_error();
5587 return;
5588 }
e440a328 5589
5590 if (this->op_ == OPERATOR_EQEQ
5591 || this->op_ == OPERATOR_NOTEQ
5592 || this->op_ == OPERATOR_LT
5593 || this->op_ == OPERATOR_LE
5594 || this->op_ == OPERATOR_GT
5595 || this->op_ == OPERATOR_GE)
5596 {
5597 if (!Type::are_assignable(left_type, right_type, NULL)
5598 && !Type::are_assignable(right_type, left_type, NULL))
5599 {
5600 this->report_error(_("incompatible types in binary expression"));
5601 return;
5602 }
5603 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5604 right_type,
e440a328 5605 this->location())
5606 || !Binary_expression::check_operator_type(this->op_, right_type,
be8b5eee 5607 left_type,
e440a328 5608 this->location()))
5609 {
5610 this->set_is_error();
5611 return;
5612 }
5613 }
5614 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5615 {
5616 if (!Type::are_compatible_for_binop(left_type, right_type))
5617 {
5618 this->report_error(_("incompatible types in binary expression"));
5619 return;
5620 }
5621 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5622 right_type,
e440a328 5623 this->location()))
5624 {
5625 this->set_is_error();
5626 return;
5627 }
5628 }
5629 else
5630 {
5631 if (left_type->integer_type() == NULL)
5632 this->report_error(_("shift of non-integer operand"));
5633
5634 if (!right_type->is_abstract()
5635 && (right_type->integer_type() == NULL
5636 || !right_type->integer_type()->is_unsigned()))
5637 this->report_error(_("shift count not unsigned integer"));
5638 else
5639 {
0c77715b 5640 Numeric_constant nc;
5641 if (this->right_->numeric_constant_value(&nc))
e440a328 5642 {
0c77715b 5643 mpz_t val;
5644 if (!nc.to_int(&val))
5645 this->report_error(_("shift count not unsigned integer"));
5646 else
a4eba91b 5647 {
0c77715b 5648 if (mpz_sgn(val) < 0)
5649 {
5650 this->report_error(_("negative shift count"));
5651 mpz_set_ui(val, 0);
5652 Location rloc = this->right_->location();
5653 this->right_ = Expression::make_integer(&val, right_type,
5654 rloc);
5655 }
5656 mpz_clear(val);
a4eba91b 5657 }
e440a328 5658 }
e440a328 5659 }
5660 }
5661}
5662
5663// Get a tree for a binary expression.
5664
5665tree
5666Binary_expression::do_get_tree(Translate_context* context)
5667{
5668 tree left = this->left_->get_tree(context);
5669 tree right = this->right_->get_tree(context);
5670
5671 if (left == error_mark_node || right == error_mark_node)
5672 return error_mark_node;
5673
5674 enum tree_code code;
5675 bool use_left_type = true;
5676 bool is_shift_op = false;
29a2d1d8 5677 bool is_idiv_op = false;
e440a328 5678 switch (this->op_)
5679 {
5680 case OPERATOR_EQEQ:
5681 case OPERATOR_NOTEQ:
5682 case OPERATOR_LT:
5683 case OPERATOR_LE:
5684 case OPERATOR_GT:
5685 case OPERATOR_GE:
5686 return Expression::comparison_tree(context, this->op_,
5687 this->left_->type(), left,
5688 this->right_->type(), right,
5689 this->location());
5690
5691 case OPERATOR_OROR:
5692 code = TRUTH_ORIF_EXPR;
5693 use_left_type = false;
5694 break;
5695 case OPERATOR_ANDAND:
5696 code = TRUTH_ANDIF_EXPR;
5697 use_left_type = false;
5698 break;
5699 case OPERATOR_PLUS:
5700 code = PLUS_EXPR;
5701 break;
5702 case OPERATOR_MINUS:
5703 code = MINUS_EXPR;
5704 break;
5705 case OPERATOR_OR:
5706 code = BIT_IOR_EXPR;
5707 break;
5708 case OPERATOR_XOR:
5709 code = BIT_XOR_EXPR;
5710 break;
5711 case OPERATOR_MULT:
5712 code = MULT_EXPR;
5713 break;
5714 case OPERATOR_DIV:
5715 {
5716 Type *t = this->left_->type();
5717 if (t->float_type() != NULL || t->complex_type() != NULL)
5718 code = RDIV_EXPR;
5719 else
29a2d1d8 5720 {
5721 code = TRUNC_DIV_EXPR;
5722 is_idiv_op = true;
5723 }
e440a328 5724 }
5725 break;
5726 case OPERATOR_MOD:
5727 code = TRUNC_MOD_EXPR;
29a2d1d8 5728 is_idiv_op = true;
e440a328 5729 break;
5730 case OPERATOR_LSHIFT:
5731 code = LSHIFT_EXPR;
5732 is_shift_op = true;
5733 break;
5734 case OPERATOR_RSHIFT:
5735 code = RSHIFT_EXPR;
5736 is_shift_op = true;
5737 break;
5738 case OPERATOR_AND:
5739 code = BIT_AND_EXPR;
5740 break;
5741 case OPERATOR_BITCLEAR:
5742 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5743 code = BIT_AND_EXPR;
5744 break;
5745 default:
c3e6f413 5746 go_unreachable();
e440a328 5747 }
5748
29a2d1d8 5749 location_t gccloc = this->location().gcc_location();
e440a328 5750 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5751
5752 if (this->left_->type()->is_string_type())
5753 {
c484d925 5754 go_assert(this->op_ == OPERATOR_PLUS);
9f0e0513 5755 Type* st = Type::make_string_type();
5756 tree string_type = type_to_tree(st->get_backend(context->gogo()));
e440a328 5757 static tree string_plus_decl;
5758 return Gogo::call_builtin(&string_plus_decl,
5759 this->location(),
5760 "__go_string_plus",
5761 2,
5762 string_type,
5763 string_type,
5764 left,
5765 string_type,
5766 right);
5767 }
5768
5769 tree compute_type = excess_precision_type(type);
5770 if (compute_type != NULL_TREE)
5771 {
5772 left = ::convert(compute_type, left);
5773 right = ::convert(compute_type, right);
5774 }
5775
5776 tree eval_saved = NULL_TREE;
29a2d1d8 5777 if (is_shift_op
5778 || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow)))
e440a328 5779 {
e440a328 5780 // Make sure the values are evaluated.
29a2d1d8 5781 if (!DECL_P(left))
a7a70f31 5782 {
5783 left = save_expr(left);
5784 eval_saved = left;
5785 }
29a2d1d8 5786 if (!DECL_P(right))
a7a70f31 5787 {
5788 right = save_expr(right);
5789 if (eval_saved == NULL_TREE)
5790 eval_saved = right;
5791 else
29a2d1d8 5792 eval_saved = fold_build2_loc(gccloc, COMPOUND_EXPR,
a7a70f31 5793 void_type_node, eval_saved, right);
5794 }
e440a328 5795 }
5796
29a2d1d8 5797 tree ret = fold_build2_loc(gccloc, code,
e440a328 5798 compute_type != NULL_TREE ? compute_type : type,
5799 left, right);
5800
5801 if (compute_type != NULL_TREE)
5802 ret = ::convert(type, ret);
5803
5804 // In Go, a shift larger than the size of the type is well-defined.
5805 // This is not true in GENERIC, so we need to insert a conditional.
5806 if (is_shift_op)
5807 {
c484d925 5808 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5809 go_assert(this->left_->type()->integer_type() != NULL);
e440a328 5810 int bits = TYPE_PRECISION(TREE_TYPE(left));
5811
5812 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5813 build_int_cst_type(TREE_TYPE(right), bits));
5814
29a2d1d8 5815 tree overflow_result = fold_convert_loc(gccloc, TREE_TYPE(left),
e440a328 5816 integer_zero_node);
5817 if (this->op_ == OPERATOR_RSHIFT
5818 && !this->left_->type()->integer_type()->is_unsigned())
5819 {
b13c66cd 5820 tree neg =
29a2d1d8 5821 fold_build2_loc(gccloc, LT_EXPR, boolean_type_node,
5822 left,
5823 fold_convert_loc(gccloc, TREE_TYPE(left),
b13c66cd 5824 integer_zero_node));
5825 tree neg_one =
29a2d1d8 5826 fold_build2_loc(gccloc, MINUS_EXPR, TREE_TYPE(left),
5827 fold_convert_loc(gccloc, TREE_TYPE(left),
b13c66cd 5828 integer_zero_node),
29a2d1d8 5829 fold_convert_loc(gccloc, TREE_TYPE(left),
b13c66cd 5830 integer_one_node));
5831 overflow_result =
29a2d1d8 5832 fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left),
5833 neg, neg_one, overflow_result);
5834 }
5835
5836 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left),
5837 compare, ret, overflow_result);
5838
5839 if (eval_saved != NULL_TREE)
5840 ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
5841 eval_saved, ret);
5842 }
5843
5844 // Add checks for division by zero and division overflow as needed.
5845 if (is_idiv_op)
5846 {
5847 if (go_check_divide_zero)
5848 {
5849 // right == 0
5850 tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
5851 right,
5852 fold_convert_loc(gccloc,
5853 TREE_TYPE(right),
5854 integer_zero_node));
5855
5856 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0
5857 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO;
5858 tree panic = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
5859 Gogo::runtime_error(errcode,
5860 this->location()),
5861 fold_convert_loc(gccloc, TREE_TYPE(ret),
5862 integer_zero_node));
5863
5864 // right == 0 ? (__go_runtime_error(...), 0) : ret
5865 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5866 check, panic, ret);
b13c66cd 5867 }
5868
29a2d1d8 5869 if (go_check_divide_overflow)
5870 {
5871 // right == -1
5872 // FIXME: It would be nice to say that this test is expected
5873 // to return false.
5874 tree m1 = integer_minus_one_node;
5875 tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
5876 right,
5877 fold_convert_loc(gccloc,
5878 TREE_TYPE(right),
5879 m1));
5880
5881 tree overflow;
5882 if (TYPE_UNSIGNED(TREE_TYPE(ret)))
5883 {
5884 // An unsigned -1 is the largest possible number, so
5885 // dividing is always 1 or 0.
5886 tree cmp = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
5887 left, right);
5888 if (this->op_ == OPERATOR_DIV)
5889 overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5890 cmp,
5891 fold_convert_loc(gccloc,
5892 TREE_TYPE(ret),
5893 integer_one_node),
5894 fold_convert_loc(gccloc,
5895 TREE_TYPE(ret),
5896 integer_zero_node));
5897 else
5898 overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5899 cmp,
5900 fold_convert_loc(gccloc,
5901 TREE_TYPE(ret),
5902 integer_zero_node),
5903 left);
5904 }
5905 else
5906 {
5907 // Computing left / -1 is the same as computing - left,
5908 // which does not overflow since Go sets -fwrapv.
5909 if (this->op_ == OPERATOR_DIV)
5910 overflow = fold_build1_loc(gccloc, NEGATE_EXPR, TREE_TYPE(left),
5911 left);
5912 else
5913 overflow = integer_zero_node;
5914 }
5915 overflow = fold_convert_loc(gccloc, TREE_TYPE(ret), overflow);
5916
5917 // right == -1 ? - left : ret
5918 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5919 check, overflow, ret);
5920 }
e440a328 5921
a7a70f31 5922 if (eval_saved != NULL_TREE)
29a2d1d8 5923 ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
5924 eval_saved, ret);
e440a328 5925 }
5926
5927 return ret;
5928}
5929
5930// Export a binary expression.
5931
5932void
5933Binary_expression::do_export(Export* exp) const
5934{
5935 exp->write_c_string("(");
5936 this->left_->export_expression(exp);
5937 switch (this->op_)
5938 {
5939 case OPERATOR_OROR:
5940 exp->write_c_string(" || ");
5941 break;
5942 case OPERATOR_ANDAND:
5943 exp->write_c_string(" && ");
5944 break;
5945 case OPERATOR_EQEQ:
5946 exp->write_c_string(" == ");
5947 break;
5948 case OPERATOR_NOTEQ:
5949 exp->write_c_string(" != ");
5950 break;
5951 case OPERATOR_LT:
5952 exp->write_c_string(" < ");
5953 break;
5954 case OPERATOR_LE:
5955 exp->write_c_string(" <= ");
5956 break;
5957 case OPERATOR_GT:
5958 exp->write_c_string(" > ");
5959 break;
5960 case OPERATOR_GE:
5961 exp->write_c_string(" >= ");
5962 break;
5963 case OPERATOR_PLUS:
5964 exp->write_c_string(" + ");
5965 break;
5966 case OPERATOR_MINUS:
5967 exp->write_c_string(" - ");
5968 break;
5969 case OPERATOR_OR:
5970 exp->write_c_string(" | ");
5971 break;
5972 case OPERATOR_XOR:
5973 exp->write_c_string(" ^ ");
5974 break;
5975 case OPERATOR_MULT:
5976 exp->write_c_string(" * ");
5977 break;
5978 case OPERATOR_DIV:
5979 exp->write_c_string(" / ");
5980 break;
5981 case OPERATOR_MOD:
5982 exp->write_c_string(" % ");
5983 break;
5984 case OPERATOR_LSHIFT:
5985 exp->write_c_string(" << ");
5986 break;
5987 case OPERATOR_RSHIFT:
5988 exp->write_c_string(" >> ");
5989 break;
5990 case OPERATOR_AND:
5991 exp->write_c_string(" & ");
5992 break;
5993 case OPERATOR_BITCLEAR:
5994 exp->write_c_string(" &^ ");
5995 break;
5996 default:
c3e6f413 5997 go_unreachable();
e440a328 5998 }
5999 this->right_->export_expression(exp);
6000 exp->write_c_string(")");
6001}
6002
6003// Import a binary expression.
6004
6005Expression*
6006Binary_expression::do_import(Import* imp)
6007{
6008 imp->require_c_string("(");
6009
6010 Expression* left = Expression::import_expression(imp);
6011
6012 Operator op;
6013 if (imp->match_c_string(" || "))
6014 {
6015 op = OPERATOR_OROR;
6016 imp->advance(4);
6017 }
6018 else if (imp->match_c_string(" && "))
6019 {
6020 op = OPERATOR_ANDAND;
6021 imp->advance(4);
6022 }
6023 else if (imp->match_c_string(" == "))
6024 {
6025 op = OPERATOR_EQEQ;
6026 imp->advance(4);
6027 }
6028 else if (imp->match_c_string(" != "))
6029 {
6030 op = OPERATOR_NOTEQ;
6031 imp->advance(4);
6032 }
6033 else if (imp->match_c_string(" < "))
6034 {
6035 op = OPERATOR_LT;
6036 imp->advance(3);
6037 }
6038 else if (imp->match_c_string(" <= "))
6039 {
6040 op = OPERATOR_LE;
6041 imp->advance(4);
6042 }
6043 else if (imp->match_c_string(" > "))
6044 {
6045 op = OPERATOR_GT;
6046 imp->advance(3);
6047 }
6048 else if (imp->match_c_string(" >= "))
6049 {
6050 op = OPERATOR_GE;
6051 imp->advance(4);
6052 }
6053 else if (imp->match_c_string(" + "))
6054 {
6055 op = OPERATOR_PLUS;
6056 imp->advance(3);
6057 }
6058 else if (imp->match_c_string(" - "))
6059 {
6060 op = OPERATOR_MINUS;
6061 imp->advance(3);
6062 }
6063 else if (imp->match_c_string(" | "))
6064 {
6065 op = OPERATOR_OR;
6066 imp->advance(3);
6067 }
6068 else if (imp->match_c_string(" ^ "))
6069 {
6070 op = OPERATOR_XOR;
6071 imp->advance(3);
6072 }
6073 else if (imp->match_c_string(" * "))
6074 {
6075 op = OPERATOR_MULT;
6076 imp->advance(3);
6077 }
6078 else if (imp->match_c_string(" / "))
6079 {
6080 op = OPERATOR_DIV;
6081 imp->advance(3);
6082 }
6083 else if (imp->match_c_string(" % "))
6084 {
6085 op = OPERATOR_MOD;
6086 imp->advance(3);
6087 }
6088 else if (imp->match_c_string(" << "))
6089 {
6090 op = OPERATOR_LSHIFT;
6091 imp->advance(4);
6092 }
6093 else if (imp->match_c_string(" >> "))
6094 {
6095 op = OPERATOR_RSHIFT;
6096 imp->advance(4);
6097 }
6098 else if (imp->match_c_string(" & "))
6099 {
6100 op = OPERATOR_AND;
6101 imp->advance(3);
6102 }
6103 else if (imp->match_c_string(" &^ "))
6104 {
6105 op = OPERATOR_BITCLEAR;
6106 imp->advance(4);
6107 }
6108 else
6109 {
6110 error_at(imp->location(), "unrecognized binary operator");
6111 return Expression::make_error(imp->location());
6112 }
6113
6114 Expression* right = Expression::import_expression(imp);
6115
6116 imp->require_c_string(")");
6117
6118 return Expression::make_binary(op, left, right, imp->location());
6119}
6120
d751bb78 6121// Dump ast representation of a binary expression.
6122
6123void
6124Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6125{
6126 ast_dump_context->ostream() << "(";
6127 ast_dump_context->dump_expression(this->left_);
6128 ast_dump_context->ostream() << " ";
6129 ast_dump_context->dump_operator(this->op_);
6130 ast_dump_context->ostream() << " ";
6131 ast_dump_context->dump_expression(this->right_);
6132 ast_dump_context->ostream() << ") ";
6133}
6134
e440a328 6135// Make a binary expression.
6136
6137Expression*
6138Expression::make_binary(Operator op, Expression* left, Expression* right,
b13c66cd 6139 Location location)
e440a328 6140{
6141 return new Binary_expression(op, left, right, location);
6142}
6143
6144// Implement a comparison.
6145
6146tree
6147Expression::comparison_tree(Translate_context* context, Operator op,
6148 Type* left_type, tree left_tree,
6149 Type* right_type, tree right_tree,
b13c66cd 6150 Location location)
e440a328 6151{
6152 enum tree_code code;
6153 switch (op)
6154 {
6155 case OPERATOR_EQEQ:
6156 code = EQ_EXPR;
6157 break;
6158 case OPERATOR_NOTEQ:
6159 code = NE_EXPR;
6160 break;
6161 case OPERATOR_LT:
6162 code = LT_EXPR;
6163 break;
6164 case OPERATOR_LE:
6165 code = LE_EXPR;
6166 break;
6167 case OPERATOR_GT:
6168 code = GT_EXPR;
6169 break;
6170 case OPERATOR_GE:
6171 code = GE_EXPR;
6172 break;
6173 default:
c3e6f413 6174 go_unreachable();
e440a328 6175 }
6176
15c67ee2 6177 if (left_type->is_string_type() && right_type->is_string_type())
e440a328 6178 {
9f0e0513 6179 Type* st = Type::make_string_type();
6180 tree string_type = type_to_tree(st->get_backend(context->gogo()));
e440a328 6181 static tree string_compare_decl;
6182 left_tree = Gogo::call_builtin(&string_compare_decl,
6183 location,
6184 "__go_strcmp",
6185 2,
6186 integer_type_node,
6187 string_type,
6188 left_tree,
6189 string_type,
6190 right_tree);
6191 right_tree = build_int_cst_type(integer_type_node, 0);
6192 }
15c67ee2 6193 else if ((left_type->interface_type() != NULL
6194 && right_type->interface_type() == NULL
6195 && !right_type->is_nil_type())
6196 || (left_type->interface_type() == NULL
6197 && !left_type->is_nil_type()
6198 && right_type->interface_type() != NULL))
e440a328 6199 {
6200 // Comparing an interface value to a non-interface value.
6201 if (left_type->interface_type() == NULL)
6202 {
6203 std::swap(left_type, right_type);
6204 std::swap(left_tree, right_tree);
6205 }
6206
6207 // The right operand is not an interface. We need to take its
6208 // address if it is not a pointer.
6209 tree make_tmp;
6210 tree arg;
6211 if (right_type->points_to() != NULL)
6212 {
6213 make_tmp = NULL_TREE;
6214 arg = right_tree;
6215 }
dd28fd36 6216 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree))
6217 || (TREE_CODE(right_tree) != CONST_DECL
6218 && DECL_P(right_tree)))
e440a328 6219 {
6220 make_tmp = NULL_TREE;
b13c66cd 6221 arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree);
e440a328 6222 if (DECL_P(right_tree))
6223 TREE_ADDRESSABLE(right_tree) = 1;
6224 }
6225 else
6226 {
6227 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6228 get_name(right_tree));
6229 DECL_IGNORED_P(tmp) = 0;
6230 DECL_INITIAL(tmp) = right_tree;
6231 TREE_ADDRESSABLE(tmp) = 1;
6232 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
b13c66cd 6233 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
6234 arg = build_fold_addr_expr_loc(location.gcc_location(), tmp);
e440a328 6235 }
b13c66cd 6236 arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg);
e440a328 6237
a1d23b41 6238 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
6239 location);
e440a328 6240
6241 if (left_type->interface_type()->is_empty())
6242 {
6243 static tree empty_interface_value_compare_decl;
6244 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6245 location,
6246 "__go_empty_interface_value_compare",
6247 3,
6248 integer_type_node,
6249 TREE_TYPE(left_tree),
6250 left_tree,
6251 TREE_TYPE(descriptor),
6252 descriptor,
6253 ptr_type_node,
6254 arg);
5fb82b5e 6255 if (left_tree == error_mark_node)
6256 return error_mark_node;
e440a328 6257 // This can panic if the type is not comparable.
6258 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6259 }
6260 else
6261 {
6262 static tree interface_value_compare_decl;
6263 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6264 location,
6265 "__go_interface_value_compare",
6266 3,
6267 integer_type_node,
6268 TREE_TYPE(left_tree),
6269 left_tree,
6270 TREE_TYPE(descriptor),
6271 descriptor,
6272 ptr_type_node,
6273 arg);
5fb82b5e 6274 if (left_tree == error_mark_node)
6275 return error_mark_node;
e440a328 6276 // This can panic if the type is not comparable.
6277 TREE_NOTHROW(interface_value_compare_decl) = 0;
6278 }
6279 right_tree = build_int_cst_type(integer_type_node, 0);
6280
6281 if (make_tmp != NULL_TREE)
6282 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6283 left_tree);
6284 }
6285 else if (left_type->interface_type() != NULL
6286 && right_type->interface_type() != NULL)
6287 {
739bad04 6288 if (left_type->interface_type()->is_empty()
6289 && right_type->interface_type()->is_empty())
e440a328 6290 {
e440a328 6291 static tree empty_interface_compare_decl;
6292 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6293 location,
6294 "__go_empty_interface_compare",
6295 2,
6296 integer_type_node,
6297 TREE_TYPE(left_tree),
6298 left_tree,
6299 TREE_TYPE(right_tree),
6300 right_tree);
5fb82b5e 6301 if (left_tree == error_mark_node)
6302 return error_mark_node;
e440a328 6303 // This can panic if the type is uncomparable.
6304 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6305 }
739bad04 6306 else if (!left_type->interface_type()->is_empty()
6307 && !right_type->interface_type()->is_empty())
e440a328 6308 {
e440a328 6309 static tree interface_compare_decl;
6310 left_tree = Gogo::call_builtin(&interface_compare_decl,
6311 location,
6312 "__go_interface_compare",
6313 2,
6314 integer_type_node,
6315 TREE_TYPE(left_tree),
6316 left_tree,
6317 TREE_TYPE(right_tree),
6318 right_tree);
5fb82b5e 6319 if (left_tree == error_mark_node)
6320 return error_mark_node;
e440a328 6321 // This can panic if the type is uncomparable.
6322 TREE_NOTHROW(interface_compare_decl) = 0;
6323 }
739bad04 6324 else
6325 {
6326 if (left_type->interface_type()->is_empty())
6327 {
c484d925 6328 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
739bad04 6329 std::swap(left_type, right_type);
6330 std::swap(left_tree, right_tree);
6331 }
c484d925 6332 go_assert(!left_type->interface_type()->is_empty());
6333 go_assert(right_type->interface_type()->is_empty());
739bad04 6334 static tree interface_empty_compare_decl;
6335 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6336 location,
6337 "__go_interface_empty_compare",
6338 2,
6339 integer_type_node,
6340 TREE_TYPE(left_tree),
6341 left_tree,
6342 TREE_TYPE(right_tree),
6343 right_tree);
6344 if (left_tree == error_mark_node)
6345 return error_mark_node;
6346 // This can panic if the type is uncomparable.
6347 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6348 }
6349
e440a328 6350 right_tree = build_int_cst_type(integer_type_node, 0);
6351 }
6352
6353 if (left_type->is_nil_type()
6354 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6355 {
6356 std::swap(left_type, right_type);
6357 std::swap(left_tree, right_tree);
6358 }
6359
6360 if (right_type->is_nil_type())
6361 {
6362 if (left_type->array_type() != NULL
6363 && left_type->array_type()->length() == NULL)
6364 {
6365 Array_type* at = left_type->array_type();
6366 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6367 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6368 }
6369 else if (left_type->interface_type() != NULL)
6370 {
6371 // An interface is nil if the first field is nil.
6372 tree left_type_tree = TREE_TYPE(left_tree);
c484d925 6373 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
e440a328 6374 tree field = TYPE_FIELDS(left_type_tree);
6375 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6376 field, NULL_TREE);
6377 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6378 }
6379 else
6380 {
c484d925 6381 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
e440a328 6382 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6383 }
6384 }
6385
d8ccb1e3 6386 if (left_tree == error_mark_node || right_tree == error_mark_node)
6387 return error_mark_node;
6388
e440a328 6389 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6390 if (CAN_HAVE_LOCATION_P(ret))
b13c66cd 6391 SET_EXPR_LOCATION(ret, location.gcc_location());
e440a328 6392 return ret;
6393}
6394
6395// Class Bound_method_expression.
6396
6397// Traversal.
6398
6399int
6400Bound_method_expression::do_traverse(Traverse* traverse)
6401{
e0659c9e 6402 return Expression::traverse(&this->expr_, traverse);
e440a328 6403}
6404
6405// Return the type of a bound method expression. The type of this
6406// object is really the type of the method with no receiver. We
6407// should be able to get away with just returning the type of the
6408// method.
6409
6410Type*
6411Bound_method_expression::do_type()
6412{
e0659c9e 6413 if (this->method_->is_function())
6414 return this->method_->func_value()->type();
6415 else if (this->method_->is_function_declaration())
6416 return this->method_->func_declaration_value()->type();
6417 else
6418 return Type::make_error_type();
e440a328 6419}
6420
6421// Determine the types of a method expression.
6422
6423void
6424Bound_method_expression::do_determine_type(const Type_context*)
6425{
e0659c9e 6426 Function_type* fntype = this->type()->function_type();
e440a328 6427 if (fntype == NULL || !fntype->is_method())
6428 this->expr_->determine_type_no_context();
6429 else
6430 {
6431 Type_context subcontext(fntype->receiver()->type(), false);
6432 this->expr_->determine_type(&subcontext);
6433 }
6434}
6435
6436// Check the types of a method expression.
6437
6438void
6439Bound_method_expression::do_check_types(Gogo*)
6440{
e0659c9e 6441 if (!this->method_->is_function()
6442 && !this->method_->is_function_declaration())
e440a328 6443 this->report_error(_("object is not a method"));
6444 else
6445 {
e0659c9e 6446 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
e440a328 6447 Type* etype = (this->expr_type_ != NULL
6448 ? this->expr_type_
6449 : this->expr_->type());
6450 etype = etype->deref();
07ba8be5 6451 if (!Type::are_identical(rtype, etype, true, NULL))
e440a328 6452 this->report_error(_("method type does not match object type"));
6453 }
6454}
6455
6456// Get the tree for a method expression. There is no standard tree
6457// representation for this. The only places it may currently be used
6458// are in a Call_expression or a Go_statement, which will take it
6459// apart directly. So this has nothing to do at present.
6460
6461tree
6462Bound_method_expression::do_get_tree(Translate_context*)
6463{
d40405e2 6464 error_at(this->location(), "reference to method other than calling it");
6465 return error_mark_node;
e440a328 6466}
6467
d751bb78 6468// Dump ast representation of a bound method expression.
6469
6470void
6471Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
6472 const
6473{
6474 if (this->expr_type_ != NULL)
6475 ast_dump_context->ostream() << "(";
6476 ast_dump_context->dump_expression(this->expr_);
6477 if (this->expr_type_ != NULL)
6478 {
6479 ast_dump_context->ostream() << ":";
6480 ast_dump_context->dump_type(this->expr_type_);
6481 ast_dump_context->ostream() << ")";
6482 }
6483
e0659c9e 6484 ast_dump_context->ostream() << "." << this->method_->name();
d751bb78 6485}
6486
e440a328 6487// Make a method expression.
6488
6489Bound_method_expression*
e0659c9e 6490Expression::make_bound_method(Expression* expr, Named_object* method,
b13c66cd 6491 Location location)
e440a328 6492{
6493 return new Bound_method_expression(expr, method, location);
6494}
6495
6496// Class Builtin_call_expression. This is used for a call to a
6497// builtin function.
6498
6499class Builtin_call_expression : public Call_expression
6500{
6501 public:
6502 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
b13c66cd 6503 bool is_varargs, Location location);
e440a328 6504
6505 protected:
6506 // This overrides Call_expression::do_lower.
6507 Expression*
ceeb4318 6508 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 6509
6510 bool
6511 do_is_constant() const;
6512
6513 bool
0c77715b 6514 do_numeric_constant_value(Numeric_constant*) const;
e440a328 6515
a7549a6a 6516 void
6517 do_discarding_value();
6518
e440a328 6519 Type*
6520 do_type();
6521
6522 void
6523 do_determine_type(const Type_context*);
6524
6525 void
6526 do_check_types(Gogo*);
6527
6528 Expression*
6529 do_copy()
6530 {
6531 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6532 this->args()->copy(),
6533 this->is_varargs(),
6534 this->location());
6535 }
6536
6537 tree
6538 do_get_tree(Translate_context*);
6539
6540 void
6541 do_export(Export*) const;
6542
6543 virtual bool
6544 do_is_recover_call() const;
6545
6546 virtual void
6547 do_set_recover_arg(Expression*);
6548
6549 private:
6550 // The builtin functions.
6551 enum Builtin_function_code
6552 {
6553 BUILTIN_INVALID,
6554
6555 // Predeclared builtin functions.
6556 BUILTIN_APPEND,
6557 BUILTIN_CAP,
6558 BUILTIN_CLOSE,
48080209 6559 BUILTIN_COMPLEX,
e440a328 6560 BUILTIN_COPY,
1cce762f 6561 BUILTIN_DELETE,
e440a328 6562 BUILTIN_IMAG,
6563 BUILTIN_LEN,
6564 BUILTIN_MAKE,
6565 BUILTIN_NEW,
6566 BUILTIN_PANIC,
6567 BUILTIN_PRINT,
6568 BUILTIN_PRINTLN,
6569 BUILTIN_REAL,
6570 BUILTIN_RECOVER,
6571
6572 // Builtin functions from the unsafe package.
6573 BUILTIN_ALIGNOF,
6574 BUILTIN_OFFSETOF,
6575 BUILTIN_SIZEOF
6576 };
6577
6578 Expression*
6579 one_arg() const;
6580
6581 bool
6582 check_one_arg();
6583
6584 static Type*
6585 real_imag_type(Type*);
6586
6587 static Type*
48080209 6588 complex_type(Type*);
e440a328 6589
a9182619 6590 Expression*
6591 lower_make();
6592
6593 bool
6594 check_int_value(Expression*);
6595
e440a328 6596 // A pointer back to the general IR structure. This avoids a global
6597 // variable, or passing it around everywhere.
6598 Gogo* gogo_;
6599 // The builtin function being called.
6600 Builtin_function_code code_;
0f914071 6601 // Used to stop endless loops when the length of an array uses len
6602 // or cap of the array itself.
6603 mutable bool seen_;
e440a328 6604};
6605
6606Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6607 Expression* fn,
6608 Expression_list* args,
6609 bool is_varargs,
b13c66cd 6610 Location location)
e440a328 6611 : Call_expression(fn, args, is_varargs, location),
0f914071 6612 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
e440a328 6613{
6614 Func_expression* fnexp = this->fn()->func_expression();
c484d925 6615 go_assert(fnexp != NULL);
e440a328 6616 const std::string& name(fnexp->named_object()->name());
6617 if (name == "append")
6618 this->code_ = BUILTIN_APPEND;
6619 else if (name == "cap")
6620 this->code_ = BUILTIN_CAP;
6621 else if (name == "close")
6622 this->code_ = BUILTIN_CLOSE;
48080209 6623 else if (name == "complex")
6624 this->code_ = BUILTIN_COMPLEX;
e440a328 6625 else if (name == "copy")
6626 this->code_ = BUILTIN_COPY;
1cce762f 6627 else if (name == "delete")
6628 this->code_ = BUILTIN_DELETE;
e440a328 6629 else if (name == "imag")
6630 this->code_ = BUILTIN_IMAG;
6631 else if (name == "len")
6632 this->code_ = BUILTIN_LEN;
6633 else if (name == "make")
6634 this->code_ = BUILTIN_MAKE;
6635 else if (name == "new")
6636 this->code_ = BUILTIN_NEW;
6637 else if (name == "panic")
6638 this->code_ = BUILTIN_PANIC;
6639 else if (name == "print")
6640 this->code_ = BUILTIN_PRINT;
6641 else if (name == "println")
6642 this->code_ = BUILTIN_PRINTLN;
6643 else if (name == "real")
6644 this->code_ = BUILTIN_REAL;
6645 else if (name == "recover")
6646 this->code_ = BUILTIN_RECOVER;
6647 else if (name == "Alignof")
6648 this->code_ = BUILTIN_ALIGNOF;
6649 else if (name == "Offsetof")
6650 this->code_ = BUILTIN_OFFSETOF;
6651 else if (name == "Sizeof")
6652 this->code_ = BUILTIN_SIZEOF;
6653 else
c3e6f413 6654 go_unreachable();
e440a328 6655}
6656
6657// Return whether this is a call to recover. This is a virtual
6658// function called from the parent class.
6659
6660bool
6661Builtin_call_expression::do_is_recover_call() const
6662{
6663 if (this->classification() == EXPRESSION_ERROR)
6664 return false;
6665 return this->code_ == BUILTIN_RECOVER;
6666}
6667
6668// Set the argument for a call to recover.
6669
6670void
6671Builtin_call_expression::do_set_recover_arg(Expression* arg)
6672{
6673 const Expression_list* args = this->args();
c484d925 6674 go_assert(args == NULL || args->empty());
e440a328 6675 Expression_list* new_args = new Expression_list();
6676 new_args->push_back(arg);
6677 this->set_args(new_args);
6678}
6679
6680// A traversal class which looks for a call expression.
6681
6682class Find_call_expression : public Traverse
6683{
6684 public:
6685 Find_call_expression()
6686 : Traverse(traverse_expressions),
6687 found_(false)
6688 { }
6689
6690 int
6691 expression(Expression**);
6692
6693 bool
6694 found()
6695 { return this->found_; }
6696
6697 private:
6698 bool found_;
6699};
6700
6701int
6702Find_call_expression::expression(Expression** pexpr)
6703{
6704 if ((*pexpr)->call_expression() != NULL)
6705 {
6706 this->found_ = true;
6707 return TRAVERSE_EXIT;
6708 }
6709 return TRAVERSE_CONTINUE;
6710}
6711
6712// Lower a builtin call expression. This turns new and make into
6713// specific expressions. We also convert to a constant if we can.
6714
6715Expression*
ceeb4318 6716Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
6717 Statement_inserter* inserter, int)
e440a328 6718{
a9182619 6719 if (this->classification() == EXPRESSION_ERROR)
6720 return this;
6721
b13c66cd 6722 Location loc = this->location();
1cce762f 6723
a8725655 6724 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6725 {
6726 this->report_error(_("invalid use of %<...%> with builtin function"));
1cce762f 6727 return Expression::make_error(loc);
a8725655 6728 }
6729
1cce762f 6730 if (this->is_constant())
e440a328 6731 {
6732 // We can only lower len and cap if there are no function calls
6733 // in the arguments. Otherwise we have to make the call.
6734 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6735 {
6736 Expression* arg = this->one_arg();
aa615cb3 6737 if (arg != NULL && !arg->is_constant())
e440a328 6738 {
6739 Find_call_expression find_call;
6740 Expression::traverse(&arg, &find_call);
6741 if (find_call.found())
6742 return this;
6743 }
6744 }
6745
0c77715b 6746 Numeric_constant nc;
6747 if (this->numeric_constant_value(&nc))
6748 return nc.expression(loc);
e440a328 6749 }
1cce762f 6750
6751 switch (this->code_)
e440a328 6752 {
1cce762f 6753 default:
6754 break;
6755
6756 case BUILTIN_NEW:
6757 {
6758 const Expression_list* args = this->args();
6759 if (args == NULL || args->size() < 1)
6760 this->report_error(_("not enough arguments"));
6761 else if (args->size() > 1)
6762 this->report_error(_("too many arguments"));
6763 else
6764 {
6765 Expression* arg = args->front();
6766 if (!arg->is_type_expression())
6767 {
6768 error_at(arg->location(), "expected type");
6769 this->set_is_error();
6770 }
6771 else
6772 return Expression::make_allocation(arg->type(), loc);
6773 }
6774 }
6775 break;
6776
6777 case BUILTIN_MAKE:
6778 return this->lower_make();
6779
6780 case BUILTIN_RECOVER:
e440a328 6781 if (function != NULL)
6782 function->func_value()->set_calls_recover();
6783 else
6784 {
6785 // Calling recover outside of a function always returns the
6786 // nil empty interface.
823c7e3d 6787 Type* eface = Type::make_empty_interface_type(loc);
1cce762f 6788 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
e440a328 6789 }
1cce762f 6790 break;
6791
6792 case BUILTIN_APPEND:
6793 {
6794 // Lower the varargs.
6795 const Expression_list* args = this->args();
6796 if (args == NULL || args->empty())
e440a328 6797 return this;
1cce762f 6798 Type* slice_type = args->front()->type();
6799 if (!slice_type->is_slice_type())
6800 {
6801 error_at(args->front()->location(), "argument 1 must be a slice");
6802 this->set_is_error();
6803 return this;
6804 }
19fd40c3 6805 Type* element_type = slice_type->array_type()->element_type();
6806 this->lower_varargs(gogo, function, inserter,
6807 Type::make_array_type(element_type, NULL),
6808 2);
1cce762f 6809 }
6810 break;
6811
6812 case BUILTIN_DELETE:
6813 {
6814 // Lower to a runtime function call.
6815 const Expression_list* args = this->args();
6816 if (args == NULL || args->size() < 2)
6817 this->report_error(_("not enough arguments"));
6818 else if (args->size() > 2)
6819 this->report_error(_("too many arguments"));
6820 else if (args->front()->type()->map_type() == NULL)
6821 this->report_error(_("argument 1 must be a map"));
6822 else
6823 {
6824 // Since this function returns no value it must appear in
6825 // a statement by itself, so we don't have to worry about
6826 // order of evaluation of values around it. Evaluate the
6827 // map first to get order of evaluation right.
6828 Map_type* mt = args->front()->type()->map_type();
6829 Temporary_statement* map_temp =
6830 Statement::make_temporary(mt, args->front(), loc);
6831 inserter->insert(map_temp);
6832
6833 Temporary_statement* key_temp =
6834 Statement::make_temporary(mt->key_type(), args->back(), loc);
6835 inserter->insert(key_temp);
6836
6837 Expression* e1 = Expression::make_temporary_reference(map_temp,
6838 loc);
6839 Expression* e2 = Expression::make_temporary_reference(key_temp,
6840 loc);
6841 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
6842 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
6843 2, e1, e2);
6844 }
6845 }
6846 break;
e440a328 6847 }
6848
6849 return this;
6850}
6851
a9182619 6852// Lower a make expression.
6853
6854Expression*
6855Builtin_call_expression::lower_make()
6856{
b13c66cd 6857 Location loc = this->location();
a9182619 6858
6859 const Expression_list* args = this->args();
6860 if (args == NULL || args->size() < 1)
6861 {
6862 this->report_error(_("not enough arguments"));
6863 return Expression::make_error(this->location());
6864 }
6865
6866 Expression_list::const_iterator parg = args->begin();
6867
6868 Expression* first_arg = *parg;
6869 if (!first_arg->is_type_expression())
6870 {
6871 error_at(first_arg->location(), "expected type");
6872 this->set_is_error();
6873 return Expression::make_error(this->location());
6874 }
6875 Type* type = first_arg->type();
6876
6877 bool is_slice = false;
6878 bool is_map = false;
6879 bool is_chan = false;
411eb89e 6880 if (type->is_slice_type())
a9182619 6881 is_slice = true;
6882 else if (type->map_type() != NULL)
6883 is_map = true;
6884 else if (type->channel_type() != NULL)
6885 is_chan = true;
6886 else
6887 {
6888 this->report_error(_("invalid type for make function"));
6889 return Expression::make_error(this->location());
6890 }
6891
ac84c822 6892 bool have_big_args = false;
6893 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6894 int uintptr_bits = uintptr_type->integer_type()->bits();
6895
a9182619 6896 ++parg;
6897 Expression* len_arg;
6898 if (parg == args->end())
6899 {
6900 if (is_slice)
6901 {
6902 this->report_error(_("length required when allocating a slice"));
6903 return Expression::make_error(this->location());
6904 }
6905
6906 mpz_t zval;
6907 mpz_init_set_ui(zval, 0);
6908 len_arg = Expression::make_integer(&zval, NULL, loc);
6909 mpz_clear(zval);
6910 }
6911 else
6912 {
6913 len_arg = *parg;
6914 if (!this->check_int_value(len_arg))
6915 {
6916 this->report_error(_("bad size for make"));
6917 return Expression::make_error(this->location());
6918 }
ac84c822 6919 if (len_arg->type()->integer_type() != NULL
6920 && len_arg->type()->integer_type()->bits() > uintptr_bits)
6921 have_big_args = true;
a9182619 6922 ++parg;
6923 }
6924
6925 Expression* cap_arg = NULL;
6926 if (is_slice && parg != args->end())
6927 {
6928 cap_arg = *parg;
6929 if (!this->check_int_value(cap_arg))
6930 {
6931 this->report_error(_("bad capacity when making slice"));
6932 return Expression::make_error(this->location());
6933 }
ac84c822 6934 if (cap_arg->type()->integer_type() != NULL
6935 && cap_arg->type()->integer_type()->bits() > uintptr_bits)
6936 have_big_args = true;
a9182619 6937 ++parg;
6938 }
6939
6940 if (parg != args->end())
6941 {
6942 this->report_error(_("too many arguments to make"));
6943 return Expression::make_error(this->location());
6944 }
6945
b13c66cd 6946 Location type_loc = first_arg->location();
a9182619 6947 Expression* type_arg;
6948 if (is_slice || is_chan)
6949 type_arg = Expression::make_type_descriptor(type, type_loc);
6950 else if (is_map)
6951 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
6952 else
6953 go_unreachable();
6954
6955 Expression* call;
6956 if (is_slice)
6957 {
6958 if (cap_arg == NULL)
ac84c822 6959 call = Runtime::make_call((have_big_args
6960 ? Runtime::MAKESLICE1BIG
6961 : Runtime::MAKESLICE1),
6962 loc, 2, type_arg, len_arg);
a9182619 6963 else
ac84c822 6964 call = Runtime::make_call((have_big_args
6965 ? Runtime::MAKESLICE2BIG
6966 : Runtime::MAKESLICE2),
6967 loc, 3, type_arg, len_arg, cap_arg);
a9182619 6968 }
6969 else if (is_map)
ac84c822 6970 call = Runtime::make_call((have_big_args
6971 ? Runtime::MAKEMAPBIG
6972 : Runtime::MAKEMAP),
6973 loc, 2, type_arg, len_arg);
a9182619 6974 else if (is_chan)
ac84c822 6975 call = Runtime::make_call((have_big_args
6976 ? Runtime::MAKECHANBIG
6977 : Runtime::MAKECHAN),
6978 loc, 2, type_arg, len_arg);
a9182619 6979 else
6980 go_unreachable();
6981
6982 return Expression::make_unsafe_cast(type, call, loc);
6983}
6984
6985// Return whether an expression has an integer value. Report an error
6986// if not. This is used when handling calls to the predeclared make
6987// function.
6988
6989bool
6990Builtin_call_expression::check_int_value(Expression* e)
6991{
6992 if (e->type()->integer_type() != NULL)
6993 return true;
6994
6995 // Check for a floating point constant with integer value.
0c77715b 6996 Numeric_constant nc;
6997 mpz_t ival;
6998 if (e->numeric_constant_value(&nc) && nc.to_int(&ival))
a9182619 6999 {
a9182619 7000 mpz_clear(ival);
0c77715b 7001 return true;
a9182619 7002 }
7003
a9182619 7004 return false;
7005}
7006
e440a328 7007// Return the type of the real or imag functions, given the type of
7008// the argument. We need to map complex to float, complex64 to
7009// float32, and complex128 to float64, so it has to be done by name.
7010// This returns NULL if it can't figure out the type.
7011
7012Type*
7013Builtin_call_expression::real_imag_type(Type* arg_type)
7014{
7015 if (arg_type == NULL || arg_type->is_abstract())
7016 return NULL;
7017 Named_type* nt = arg_type->named_type();
7018 if (nt == NULL)
7019 return NULL;
7020 while (nt->real_type()->named_type() != NULL)
7021 nt = nt->real_type()->named_type();
48080209 7022 if (nt->name() == "complex64")
e440a328 7023 return Type::lookup_float_type("float32");
7024 else if (nt->name() == "complex128")
7025 return Type::lookup_float_type("float64");
7026 else
7027 return NULL;
7028}
7029
48080209 7030// Return the type of the complex function, given the type of one of the
e440a328 7031// argments. Like real_imag_type, we have to map by name.
7032
7033Type*
48080209 7034Builtin_call_expression::complex_type(Type* arg_type)
e440a328 7035{
7036 if (arg_type == NULL || arg_type->is_abstract())
7037 return NULL;
7038 Named_type* nt = arg_type->named_type();
7039 if (nt == NULL)
7040 return NULL;
7041 while (nt->real_type()->named_type() != NULL)
7042 nt = nt->real_type()->named_type();
48080209 7043 if (nt->name() == "float32")
e440a328 7044 return Type::lookup_complex_type("complex64");
7045 else if (nt->name() == "float64")
7046 return Type::lookup_complex_type("complex128");
7047 else
7048 return NULL;
7049}
7050
7051// Return a single argument, or NULL if there isn't one.
7052
7053Expression*
7054Builtin_call_expression::one_arg() const
7055{
7056 const Expression_list* args = this->args();
aa615cb3 7057 if (args == NULL || args->size() != 1)
e440a328 7058 return NULL;
7059 return args->front();
7060}
7061
7062// Return whether this is constant: len of a string, or len or cap of
7063// a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7064
7065bool
7066Builtin_call_expression::do_is_constant() const
7067{
7068 switch (this->code_)
7069 {
7070 case BUILTIN_LEN:
7071 case BUILTIN_CAP:
7072 {
0f914071 7073 if (this->seen_)
7074 return false;
7075
e440a328 7076 Expression* arg = this->one_arg();
7077 if (arg == NULL)
7078 return false;
7079 Type* arg_type = arg->type();
7080
7081 if (arg_type->points_to() != NULL
7082 && arg_type->points_to()->array_type() != NULL
411eb89e 7083 && !arg_type->points_to()->is_slice_type())
e440a328 7084 arg_type = arg_type->points_to();
7085
7086 if (arg_type->array_type() != NULL
7087 && arg_type->array_type()->length() != NULL)
0f914071 7088 return true;
e440a328 7089
7090 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
0f914071 7091 {
7092 this->seen_ = true;
7093 bool ret = arg->is_constant();
7094 this->seen_ = false;
7095 return ret;
7096 }
e440a328 7097 }
7098 break;
7099
7100 case BUILTIN_SIZEOF:
7101 case BUILTIN_ALIGNOF:
7102 return this->one_arg() != NULL;
7103
7104 case BUILTIN_OFFSETOF:
7105 {
7106 Expression* arg = this->one_arg();
7107 if (arg == NULL)
7108 return false;
7109 return arg->field_reference_expression() != NULL;
7110 }
7111
48080209 7112 case BUILTIN_COMPLEX:
e440a328 7113 {
7114 const Expression_list* args = this->args();
7115 if (args != NULL && args->size() == 2)
7116 return args->front()->is_constant() && args->back()->is_constant();
7117 }
7118 break;
7119
7120 case BUILTIN_REAL:
7121 case BUILTIN_IMAG:
7122 {
7123 Expression* arg = this->one_arg();
7124 return arg != NULL && arg->is_constant();
7125 }
7126
7127 default:
7128 break;
7129 }
7130
7131 return false;
7132}
7133
0c77715b 7134// Return a numeric constant if possible.
e440a328 7135
7136bool
0c77715b 7137Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 7138{
7139 if (this->code_ == BUILTIN_LEN
7140 || this->code_ == BUILTIN_CAP)
7141 {
7142 Expression* arg = this->one_arg();
7143 if (arg == NULL)
7144 return false;
7145 Type* arg_type = arg->type();
7146
7147 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7148 {
7149 std::string sval;
7150 if (arg->string_constant_value(&sval))
7151 {
0c77715b 7152 nc->set_unsigned_long(Type::lookup_integer_type("int"),
7153 sval.length());
e440a328 7154 return true;
7155 }
7156 }
7157
7158 if (arg_type->points_to() != NULL
7159 && arg_type->points_to()->array_type() != NULL
411eb89e 7160 && !arg_type->points_to()->is_slice_type())
e440a328 7161 arg_type = arg_type->points_to();
7162
7163 if (arg_type->array_type() != NULL
7164 && arg_type->array_type()->length() != NULL)
7165 {
0f914071 7166 if (this->seen_)
7167 return false;
e440a328 7168 Expression* e = arg_type->array_type()->length();
0f914071 7169 this->seen_ = true;
0c77715b 7170 bool r = e->numeric_constant_value(nc);
0f914071 7171 this->seen_ = false;
7172 if (r)
e440a328 7173 {
0c77715b 7174 if (!nc->set_type(Type::lookup_integer_type("int"), false,
7175 this->location()))
7176 r = false;
e440a328 7177 }
0c77715b 7178 return r;
e440a328 7179 }
7180 }
7181 else if (this->code_ == BUILTIN_SIZEOF
7182 || this->code_ == BUILTIN_ALIGNOF)
7183 {
7184 Expression* arg = this->one_arg();
7185 if (arg == NULL)
7186 return false;
7187 Type* arg_type = arg->type();
5c13bd80 7188 if (arg_type->is_error())
e440a328 7189 return false;
7190 if (arg_type->is_abstract())
7191 return false;
9aa9e2df 7192 if (arg_type->named_type() != NULL)
7193 arg_type->named_type()->convert(this->gogo_);
927a01eb 7194
7195 unsigned int ret;
e440a328 7196 if (this->code_ == BUILTIN_SIZEOF)
7197 {
927a01eb 7198 if (!arg_type->backend_type_size(this->gogo_, &ret))
e440a328 7199 return false;
7200 }
7201 else if (this->code_ == BUILTIN_ALIGNOF)
7202 {
637bd3af 7203 if (arg->field_reference_expression() == NULL)
927a01eb 7204 {
7205 if (!arg_type->backend_type_align(this->gogo_, &ret))
7206 return false;
7207 }
637bd3af 7208 else
e440a328 7209 {
7210 // Calling unsafe.Alignof(s.f) returns the alignment of
7211 // the type of f when it is used as a field in a struct.
927a01eb 7212 if (!arg_type->backend_type_field_align(this->gogo_, &ret))
7213 return false;
e440a328 7214 }
e440a328 7215 }
7216 else
c3e6f413 7217 go_unreachable();
927a01eb 7218
7ba86326 7219 nc->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7220 static_cast<unsigned long>(ret));
e440a328 7221 return true;
7222 }
7223 else if (this->code_ == BUILTIN_OFFSETOF)
7224 {
7225 Expression* arg = this->one_arg();
7226 if (arg == NULL)
7227 return false;
7228 Field_reference_expression* farg = arg->field_reference_expression();
7229 if (farg == NULL)
7230 return false;
7231 Expression* struct_expr = farg->expr();
7232 Type* st = struct_expr->type();
7233 if (st->struct_type() == NULL)
7234 return false;
9aa9e2df 7235 if (st->named_type() != NULL)
7236 st->named_type()->convert(this->gogo_);
927a01eb 7237 unsigned int offset;
7238 if (!st->struct_type()->backend_field_offset(this->gogo_,
7239 farg->field_index(),
7240 &offset))
e440a328 7241 return false;
7ba86326 7242 nc->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7243 static_cast<unsigned long>(offset));
e440a328 7244 return true;
7245 }
0c77715b 7246 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
e440a328 7247 {
7248 Expression* arg = this->one_arg();
7249 if (arg == NULL)
7250 return false;
7251
0c77715b 7252 Numeric_constant argnc;
7253 if (!arg->numeric_constant_value(&argnc))
7254 return false;
7255
e440a328 7256 mpfr_t real;
7257 mpfr_t imag;
0c77715b 7258 if (!argnc.to_complex(&real, &imag))
7259 return false;
e440a328 7260
0c77715b 7261 Type* type = Builtin_call_expression::real_imag_type(argnc.type());
7262 if (this->code_ == BUILTIN_REAL)
7263 nc->set_float(type, real);
7264 else
7265 nc->set_float(type, imag);
7266 return true;
e440a328 7267 }
0c77715b 7268 else if (this->code_ == BUILTIN_COMPLEX)
e440a328 7269 {
7270 const Expression_list* args = this->args();
7271 if (args == NULL || args->size() != 2)
7272 return false;
7273
0c77715b 7274 Numeric_constant rnc;
7275 if (!args->front()->numeric_constant_value(&rnc))
7276 return false;
7277 Numeric_constant inc;
7278 if (!args->back()->numeric_constant_value(&inc))
7279 return false;
7280
7281 if (rnc.type() != NULL
7282 && !rnc.type()->is_abstract()
7283 && inc.type() != NULL
7284 && !inc.type()->is_abstract()
7285 && !Type::are_identical(rnc.type(), inc.type(), false, NULL))
7286 return false;
7287
e440a328 7288 mpfr_t r;
0c77715b 7289 if (!rnc.to_float(&r))
7290 return false;
7291 mpfr_t i;
7292 if (!inc.to_float(&i))
e440a328 7293 {
7294 mpfr_clear(r);
7295 return false;
7296 }
7297
0c77715b 7298 Type* arg_type = rnc.type();
7299 if (arg_type == NULL || arg_type->is_abstract())
7300 arg_type = inc.type();
e440a328 7301
0c77715b 7302 Type* type = Builtin_call_expression::complex_type(arg_type);
7303 nc->set_complex(type, r, i);
e440a328 7304
7305 mpfr_clear(r);
7306 mpfr_clear(i);
7307
0c77715b 7308 return true;
e440a328 7309 }
7310
7311 return false;
7312}
7313
a7549a6a 7314// Give an error if we are discarding the value of an expression which
7315// should not normally be discarded. We don't give an error for
7316// discarding the value of an ordinary function call, but we do for
7317// builtin functions, purely for consistency with the gc compiler.
7318
7319void
7320Builtin_call_expression::do_discarding_value()
7321{
7322 switch (this->code_)
7323 {
7324 case BUILTIN_INVALID:
7325 default:
7326 go_unreachable();
7327
7328 case BUILTIN_APPEND:
7329 case BUILTIN_CAP:
7330 case BUILTIN_COMPLEX:
7331 case BUILTIN_IMAG:
7332 case BUILTIN_LEN:
7333 case BUILTIN_MAKE:
7334 case BUILTIN_NEW:
7335 case BUILTIN_REAL:
7336 case BUILTIN_ALIGNOF:
7337 case BUILTIN_OFFSETOF:
7338 case BUILTIN_SIZEOF:
7339 this->unused_value_error();
7340 break;
7341
7342 case BUILTIN_CLOSE:
7343 case BUILTIN_COPY:
1cce762f 7344 case BUILTIN_DELETE:
a7549a6a 7345 case BUILTIN_PANIC:
7346 case BUILTIN_PRINT:
7347 case BUILTIN_PRINTLN:
7348 case BUILTIN_RECOVER:
7349 break;
7350 }
7351}
7352
e440a328 7353// Return the type.
7354
7355Type*
7356Builtin_call_expression::do_type()
7357{
7358 switch (this->code_)
7359 {
7360 case BUILTIN_INVALID:
7361 default:
c3e6f413 7362 go_unreachable();
e440a328 7363
7364 case BUILTIN_NEW:
7365 case BUILTIN_MAKE:
7366 {
7367 const Expression_list* args = this->args();
7368 if (args == NULL || args->empty())
7369 return Type::make_error_type();
7370 return Type::make_pointer_type(args->front()->type());
7371 }
7372
7373 case BUILTIN_CAP:
7374 case BUILTIN_COPY:
7375 case BUILTIN_LEN:
7ba86326 7376 return Type::lookup_integer_type("int");
7377
e440a328 7378 case BUILTIN_ALIGNOF:
7379 case BUILTIN_OFFSETOF:
7380 case BUILTIN_SIZEOF:
7ba86326 7381 return Type::lookup_integer_type("uintptr");
e440a328 7382
7383 case BUILTIN_CLOSE:
1cce762f 7384 case BUILTIN_DELETE:
e440a328 7385 case BUILTIN_PANIC:
7386 case BUILTIN_PRINT:
7387 case BUILTIN_PRINTLN:
7388 return Type::make_void_type();
7389
e440a328 7390 case BUILTIN_RECOVER:
823c7e3d 7391 return Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 7392
7393 case BUILTIN_APPEND:
7394 {
7395 const Expression_list* args = this->args();
7396 if (args == NULL || args->empty())
7397 return Type::make_error_type();
7398 return args->front()->type();
7399 }
7400
7401 case BUILTIN_REAL:
7402 case BUILTIN_IMAG:
7403 {
7404 Expression* arg = this->one_arg();
7405 if (arg == NULL)
7406 return Type::make_error_type();
7407 Type* t = arg->type();
7408 if (t->is_abstract())
7409 t = t->make_non_abstract_type();
7410 t = Builtin_call_expression::real_imag_type(t);
7411 if (t == NULL)
7412 t = Type::make_error_type();
7413 return t;
7414 }
7415
48080209 7416 case BUILTIN_COMPLEX:
e440a328 7417 {
7418 const Expression_list* args = this->args();
7419 if (args == NULL || args->size() != 2)
7420 return Type::make_error_type();
7421 Type* t = args->front()->type();
7422 if (t->is_abstract())
7423 {
7424 t = args->back()->type();
7425 if (t->is_abstract())
7426 t = t->make_non_abstract_type();
7427 }
48080209 7428 t = Builtin_call_expression::complex_type(t);
e440a328 7429 if (t == NULL)
7430 t = Type::make_error_type();
7431 return t;
7432 }
7433 }
7434}
7435
7436// Determine the type.
7437
7438void
7439Builtin_call_expression::do_determine_type(const Type_context* context)
7440{
fb94b0ca 7441 if (!this->determining_types())
7442 return;
7443
e440a328 7444 this->fn()->determine_type_no_context();
7445
7446 const Expression_list* args = this->args();
7447
7448 bool is_print;
7449 Type* arg_type = NULL;
7450 switch (this->code_)
7451 {
7452 case BUILTIN_PRINT:
7453 case BUILTIN_PRINTLN:
7454 // Do not force a large integer constant to "int".
7455 is_print = true;
7456 break;
7457
7458 case BUILTIN_REAL:
7459 case BUILTIN_IMAG:
48080209 7460 arg_type = Builtin_call_expression::complex_type(context->type);
e440a328 7461 is_print = false;
7462 break;
7463
48080209 7464 case BUILTIN_COMPLEX:
e440a328 7465 {
48080209 7466 // For the complex function the type of one operand can
e440a328 7467 // determine the type of the other, as in a binary expression.
7468 arg_type = Builtin_call_expression::real_imag_type(context->type);
7469 if (args != NULL && args->size() == 2)
7470 {
7471 Type* t1 = args->front()->type();
7472 Type* t2 = args->front()->type();
7473 if (!t1->is_abstract())
7474 arg_type = t1;
7475 else if (!t2->is_abstract())
7476 arg_type = t2;
7477 }
7478 is_print = false;
7479 }
7480 break;
7481
7482 default:
7483 is_print = false;
7484 break;
7485 }
7486
7487 if (args != NULL)
7488 {
7489 for (Expression_list::const_iterator pa = args->begin();
7490 pa != args->end();
7491 ++pa)
7492 {
7493 Type_context subcontext;
7494 subcontext.type = arg_type;
7495
7496 if (is_print)
7497 {
7498 // We want to print large constants, we so can't just
7499 // use the appropriate nonabstract type. Use uint64 for
7500 // an integer if we know it is nonnegative, otherwise
7501 // use int64 for a integer, otherwise use float64 for a
7502 // float or complex128 for a complex.
7503 Type* want_type = NULL;
7504 Type* atype = (*pa)->type();
7505 if (atype->is_abstract())
7506 {
7507 if (atype->integer_type() != NULL)
7508 {
0c77715b 7509 Numeric_constant nc;
7510 if (this->numeric_constant_value(&nc))
7511 {
7512 mpz_t val;
7513 if (nc.to_int(&val))
7514 {
7515 if (mpz_sgn(val) >= 0)
7516 want_type = Type::lookup_integer_type("uint64");
7517 mpz_clear(val);
7518 }
7519 }
7520 if (want_type == NULL)
e440a328 7521 want_type = Type::lookup_integer_type("int64");
e440a328 7522 }
7523 else if (atype->float_type() != NULL)
7524 want_type = Type::lookup_float_type("float64");
7525 else if (atype->complex_type() != NULL)
7526 want_type = Type::lookup_complex_type("complex128");
7527 else if (atype->is_abstract_string_type())
7528 want_type = Type::lookup_string_type();
7529 else if (atype->is_abstract_boolean_type())
7530 want_type = Type::lookup_bool_type();
7531 else
c3e6f413 7532 go_unreachable();
e440a328 7533 subcontext.type = want_type;
7534 }
7535 }
7536
7537 (*pa)->determine_type(&subcontext);
7538 }
7539 }
7540}
7541
7542// If there is exactly one argument, return true. Otherwise give an
7543// error message and return false.
7544
7545bool
7546Builtin_call_expression::check_one_arg()
7547{
7548 const Expression_list* args = this->args();
7549 if (args == NULL || args->size() < 1)
7550 {
7551 this->report_error(_("not enough arguments"));
7552 return false;
7553 }
7554 else if (args->size() > 1)
7555 {
7556 this->report_error(_("too many arguments"));
7557 return false;
7558 }
7559 if (args->front()->is_error_expression()
5c13bd80 7560 || args->front()->type()->is_error())
e440a328 7561 {
7562 this->set_is_error();
7563 return false;
7564 }
7565 return true;
7566}
7567
7568// Check argument types for a builtin function.
7569
7570void
7571Builtin_call_expression::do_check_types(Gogo*)
7572{
375646ea 7573 if (this->is_error_expression())
7574 return;
e440a328 7575 switch (this->code_)
7576 {
7577 case BUILTIN_INVALID:
7578 case BUILTIN_NEW:
7579 case BUILTIN_MAKE:
cd238b8d 7580 case BUILTIN_DELETE:
e440a328 7581 return;
7582
7583 case BUILTIN_LEN:
7584 case BUILTIN_CAP:
7585 {
7586 // The single argument may be either a string or an array or a
7587 // map or a channel, or a pointer to a closed array.
7588 if (this->check_one_arg())
7589 {
7590 Type* arg_type = this->one_arg()->type();
7591 if (arg_type->points_to() != NULL
7592 && arg_type->points_to()->array_type() != NULL
411eb89e 7593 && !arg_type->points_to()->is_slice_type())
e440a328 7594 arg_type = arg_type->points_to();
7595 if (this->code_ == BUILTIN_CAP)
7596 {
5c13bd80 7597 if (!arg_type->is_error()
e440a328 7598 && arg_type->array_type() == NULL
7599 && arg_type->channel_type() == NULL)
7600 this->report_error(_("argument must be array or slice "
7601 "or channel"));
7602 }
7603 else
7604 {
5c13bd80 7605 if (!arg_type->is_error()
e440a328 7606 && !arg_type->is_string_type()
7607 && arg_type->array_type() == NULL
7608 && arg_type->map_type() == NULL
7609 && arg_type->channel_type() == NULL)
7610 this->report_error(_("argument must be string or "
7611 "array or slice or map or channel"));
7612 }
7613 }
7614 }
7615 break;
7616
7617 case BUILTIN_PRINT:
7618 case BUILTIN_PRINTLN:
7619 {
7620 const Expression_list* args = this->args();
7621 if (args == NULL)
7622 {
7623 if (this->code_ == BUILTIN_PRINT)
7624 warning_at(this->location(), 0,
7625 "no arguments for builtin function %<%s%>",
7626 (this->code_ == BUILTIN_PRINT
7627 ? "print"
7628 : "println"));
7629 }
7630 else
7631 {
7632 for (Expression_list::const_iterator p = args->begin();
7633 p != args->end();
7634 ++p)
7635 {
7636 Type* type = (*p)->type();
5c13bd80 7637 if (type->is_error()
e440a328 7638 || type->is_string_type()
7639 || type->integer_type() != NULL
7640 || type->float_type() != NULL
7641 || type->complex_type() != NULL
7642 || type->is_boolean_type()
7643 || type->points_to() != NULL
7644 || type->interface_type() != NULL
7645 || type->channel_type() != NULL
7646 || type->map_type() != NULL
7647 || type->function_type() != NULL
411eb89e 7648 || type->is_slice_type())
e440a328 7649 ;
acf8e158 7650 else if ((*p)->is_type_expression())
7651 {
7652 // If this is a type expression it's going to give
7653 // an error anyhow, so we don't need one here.
7654 }
e440a328 7655 else
7656 this->report_error(_("unsupported argument type to "
7657 "builtin function"));
7658 }
7659 }
7660 }
7661 break;
7662
7663 case BUILTIN_CLOSE:
e440a328 7664 if (this->check_one_arg())
7665 {
7666 if (this->one_arg()->type()->channel_type() == NULL)
7667 this->report_error(_("argument must be channel"));
5202d986 7668 else if (!this->one_arg()->type()->channel_type()->may_send())
7669 this->report_error(_("cannot close receive-only channel"));
e440a328 7670 }
7671 break;
7672
7673 case BUILTIN_PANIC:
7674 case BUILTIN_SIZEOF:
7675 case BUILTIN_ALIGNOF:
7676 this->check_one_arg();
7677 break;
7678
7679 case BUILTIN_RECOVER:
7680 if (this->args() != NULL && !this->args()->empty())
7681 this->report_error(_("too many arguments"));
7682 break;
7683
7684 case BUILTIN_OFFSETOF:
7685 if (this->check_one_arg())
7686 {
7687 Expression* arg = this->one_arg();
7688 if (arg->field_reference_expression() == NULL)
7689 this->report_error(_("argument must be a field reference"));
7690 }
7691 break;
7692
7693 case BUILTIN_COPY:
7694 {
7695 const Expression_list* args = this->args();
7696 if (args == NULL || args->size() < 2)
7697 {
7698 this->report_error(_("not enough arguments"));
7699 break;
7700 }
7701 else if (args->size() > 2)
7702 {
7703 this->report_error(_("too many arguments"));
7704 break;
7705 }
7706 Type* arg1_type = args->front()->type();
7707 Type* arg2_type = args->back()->type();
5c13bd80 7708 if (arg1_type->is_error() || arg2_type->is_error())
e440a328 7709 break;
7710
7711 Type* e1;
411eb89e 7712 if (arg1_type->is_slice_type())
e440a328 7713 e1 = arg1_type->array_type()->element_type();
7714 else
7715 {
7716 this->report_error(_("left argument must be a slice"));
7717 break;
7718 }
7719
411eb89e 7720 if (arg2_type->is_slice_type())
60963afd 7721 {
7722 Type* e2 = arg2_type->array_type()->element_type();
7723 if (!Type::are_identical(e1, e2, true, NULL))
7724 this->report_error(_("element types must be the same"));
7725 }
e440a328 7726 else if (arg2_type->is_string_type())
e440a328 7727 {
60963afd 7728 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
7729 this->report_error(_("first argument must be []byte"));
e440a328 7730 }
60963afd 7731 else
7732 this->report_error(_("second argument must be slice or string"));
e440a328 7733 }
7734 break;
7735
7736 case BUILTIN_APPEND:
7737 {
7738 const Expression_list* args = this->args();
b0d311a1 7739 if (args == NULL || args->size() < 2)
e440a328 7740 {
7741 this->report_error(_("not enough arguments"));
7742 break;
7743 }
0b7755ec 7744 if (args->size() > 2)
7745 {
7746 this->report_error(_("too many arguments"));
7747 break;
7748 }
cd238b8d 7749 if (args->front()->type()->is_error()
7750 || args->back()->type()->is_error())
7751 break;
7752
7753 Array_type* at = args->front()->type()->array_type();
7754 Type* e = at->element_type();
4fd4fcf4 7755
7756 // The language permits appending a string to a []byte, as a
7757 // special case.
7758 if (args->back()->type()->is_string_type())
7759 {
60963afd 7760 if (e->integer_type() != NULL && e->integer_type()->is_byte())
4fd4fcf4 7761 break;
7762 }
7763
19fd40c3 7764 // The language says that the second argument must be
7765 // assignable to a slice of the element type of the first
7766 // argument. We already know the first argument is a slice
7767 // type.
cd238b8d 7768 Type* arg2_type = Type::make_array_type(e, NULL);
e440a328 7769 std::string reason;
19fd40c3 7770 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason))
e440a328 7771 {
7772 if (reason.empty())
19fd40c3 7773 this->report_error(_("argument 2 has invalid type"));
e440a328 7774 else
7775 {
19fd40c3 7776 error_at(this->location(), "argument 2 has invalid type (%s)",
e440a328 7777 reason.c_str());
7778 this->set_is_error();
7779 }
7780 }
7781 break;
7782 }
7783
7784 case BUILTIN_REAL:
7785 case BUILTIN_IMAG:
7786 if (this->check_one_arg())
7787 {
7788 if (this->one_arg()->type()->complex_type() == NULL)
7789 this->report_error(_("argument must have complex type"));
7790 }
7791 break;
7792
48080209 7793 case BUILTIN_COMPLEX:
e440a328 7794 {
7795 const Expression_list* args = this->args();
7796 if (args == NULL || args->size() < 2)
7797 this->report_error(_("not enough arguments"));
7798 else if (args->size() > 2)
7799 this->report_error(_("too many arguments"));
7800 else if (args->front()->is_error_expression()
5c13bd80 7801 || args->front()->type()->is_error()
e440a328 7802 || args->back()->is_error_expression()
5c13bd80 7803 || args->back()->type()->is_error())
e440a328 7804 this->set_is_error();
7805 else if (!Type::are_identical(args->front()->type(),
07ba8be5 7806 args->back()->type(), true, NULL))
48080209 7807 this->report_error(_("complex arguments must have identical types"));
e440a328 7808 else if (args->front()->type()->float_type() == NULL)
48080209 7809 this->report_error(_("complex arguments must have "
e440a328 7810 "floating-point type"));
7811 }
7812 break;
7813
7814 default:
c3e6f413 7815 go_unreachable();
e440a328 7816 }
7817}
7818
7819// Return the tree for a builtin function.
7820
7821tree
7822Builtin_call_expression::do_get_tree(Translate_context* context)
7823{
7824 Gogo* gogo = context->gogo();
b13c66cd 7825 Location location = this->location();
e440a328 7826 switch (this->code_)
7827 {
7828 case BUILTIN_INVALID:
7829 case BUILTIN_NEW:
7830 case BUILTIN_MAKE:
c3e6f413 7831 go_unreachable();
e440a328 7832
7833 case BUILTIN_LEN:
7834 case BUILTIN_CAP:
7835 {
7836 const Expression_list* args = this->args();
c484d925 7837 go_assert(args != NULL && args->size() == 1);
e440a328 7838 Expression* arg = *args->begin();
7839 Type* arg_type = arg->type();
0f914071 7840
7841 if (this->seen_)
7842 {
c484d925 7843 go_assert(saw_errors());
0f914071 7844 return error_mark_node;
7845 }
7846 this->seen_ = true;
7847
e440a328 7848 tree arg_tree = arg->get_tree(context);
0f914071 7849
7850 this->seen_ = false;
7851
e440a328 7852 if (arg_tree == error_mark_node)
7853 return error_mark_node;
7854
7855 if (arg_type->points_to() != NULL)
7856 {
7857 arg_type = arg_type->points_to();
c484d925 7858 go_assert(arg_type->array_type() != NULL
411eb89e 7859 && !arg_type->is_slice_type());
c484d925 7860 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
e440a328 7861 arg_tree = build_fold_indirect_ref(arg_tree);
7862 }
7863
7864 tree val_tree;
7865 if (this->code_ == BUILTIN_LEN)
7866 {
7867 if (arg_type->is_string_type())
7868 val_tree = String_type::length_tree(gogo, arg_tree);
7869 else if (arg_type->array_type() != NULL)
0f914071 7870 {
7871 if (this->seen_)
7872 {
c484d925 7873 go_assert(saw_errors());
0f914071 7874 return error_mark_node;
7875 }
7876 this->seen_ = true;
7877 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7878 this->seen_ = false;
7879 }
e440a328 7880 else if (arg_type->map_type() != NULL)
7881 {
9f0e0513 7882 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
e440a328 7883 static tree map_len_fndecl;
7884 val_tree = Gogo::call_builtin(&map_len_fndecl,
7885 location,
7886 "__go_map_len",
7887 1,
9581e91d 7888 integer_type_node,
9f0e0513 7889 arg_type_tree,
e440a328 7890 arg_tree);
7891 }
7892 else if (arg_type->channel_type() != NULL)
7893 {
9f0e0513 7894 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
e440a328 7895 static tree chan_len_fndecl;
7896 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7897 location,
7898 "__go_chan_len",
7899 1,
9581e91d 7900 integer_type_node,
9f0e0513 7901 arg_type_tree,
e440a328 7902 arg_tree);
7903 }
7904 else
c3e6f413 7905 go_unreachable();
e440a328 7906 }
7907 else
7908 {
7909 if (arg_type->array_type() != NULL)
0f914071 7910 {
7911 if (this->seen_)
7912 {
c484d925 7913 go_assert(saw_errors());
0f914071 7914 return error_mark_node;
7915 }
7916 this->seen_ = true;
7917 val_tree = arg_type->array_type()->capacity_tree(gogo,
7918 arg_tree);
7919 this->seen_ = false;
7920 }
e440a328 7921 else if (arg_type->channel_type() != NULL)
7922 {
9f0e0513 7923 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
e440a328 7924 static tree chan_cap_fndecl;
7925 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7926 location,
7927 "__go_chan_cap",
7928 1,
9581e91d 7929 integer_type_node,
9f0e0513 7930 arg_type_tree,
e440a328 7931 arg_tree);
7932 }
7933 else
c3e6f413 7934 go_unreachable();
e440a328 7935 }
7936
d8ccb1e3 7937 if (val_tree == error_mark_node)
7938 return error_mark_node;
7939
9f0e0513 7940 Type* int_type = Type::lookup_integer_type("int");
7941 tree type_tree = type_to_tree(int_type->get_backend(gogo));
e440a328 7942 if (type_tree == TREE_TYPE(val_tree))
7943 return val_tree;
7944 else
7945 return fold(convert_to_integer(type_tree, val_tree));
7946 }
7947
7948 case BUILTIN_PRINT:
7949 case BUILTIN_PRINTLN:
7950 {
7951 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7952 tree stmt_list = NULL_TREE;
7953
7954 const Expression_list* call_args = this->args();
7955 if (call_args != NULL)
7956 {
7957 for (Expression_list::const_iterator p = call_args->begin();
7958 p != call_args->end();
7959 ++p)
7960 {
7961 if (is_ln && p != call_args->begin())
7962 {
7963 static tree print_space_fndecl;
7964 tree call = Gogo::call_builtin(&print_space_fndecl,
7965 location,
7966 "__go_print_space",
7967 0,
7968 void_type_node);
5fb82b5e 7969 if (call == error_mark_node)
7970 return error_mark_node;
e440a328 7971 append_to_statement_list(call, &stmt_list);
7972 }
7973
7974 Type* type = (*p)->type();
7975
7976 tree arg = (*p)->get_tree(context);
7977 if (arg == error_mark_node)
7978 return error_mark_node;
7979
7980 tree* pfndecl;
7981 const char* fnname;
7982 if (type->is_string_type())
7983 {
7984 static tree print_string_fndecl;
7985 pfndecl = &print_string_fndecl;
7986 fnname = "__go_print_string";
7987 }
7988 else if (type->integer_type() != NULL
7989 && type->integer_type()->is_unsigned())
7990 {
7991 static tree print_uint64_fndecl;
7992 pfndecl = &print_uint64_fndecl;
7993 fnname = "__go_print_uint64";
7994 Type* itype = Type::lookup_integer_type("uint64");
9f0e0513 7995 Btype* bitype = itype->get_backend(gogo);
b13c66cd 7996 arg = fold_convert_loc(location.gcc_location(),
7997 type_to_tree(bitype), arg);
e440a328 7998 }
7999 else if (type->integer_type() != NULL)
8000 {
8001 static tree print_int64_fndecl;
8002 pfndecl = &print_int64_fndecl;
8003 fnname = "__go_print_int64";
8004 Type* itype = Type::lookup_integer_type("int64");
9f0e0513 8005 Btype* bitype = itype->get_backend(gogo);
b13c66cd 8006 arg = fold_convert_loc(location.gcc_location(),
8007 type_to_tree(bitype), arg);
e440a328 8008 }
8009 else if (type->float_type() != NULL)
8010 {
8011 static tree print_double_fndecl;
8012 pfndecl = &print_double_fndecl;
8013 fnname = "__go_print_double";
b13c66cd 8014 arg = fold_convert_loc(location.gcc_location(),
8015 double_type_node, arg);
e440a328 8016 }
8017 else if (type->complex_type() != NULL)
8018 {
8019 static tree print_complex_fndecl;
8020 pfndecl = &print_complex_fndecl;
8021 fnname = "__go_print_complex";
b13c66cd 8022 arg = fold_convert_loc(location.gcc_location(),
8023 complex_double_type_node, arg);
e440a328 8024 }
8025 else if (type->is_boolean_type())
8026 {
8027 static tree print_bool_fndecl;
8028 pfndecl = &print_bool_fndecl;
8029 fnname = "__go_print_bool";
8030 }
8031 else if (type->points_to() != NULL
8032 || type->channel_type() != NULL
8033 || type->map_type() != NULL
8034 || type->function_type() != NULL)
8035 {
8036 static tree print_pointer_fndecl;
8037 pfndecl = &print_pointer_fndecl;
8038 fnname = "__go_print_pointer";
b13c66cd 8039 arg = fold_convert_loc(location.gcc_location(),
8040 ptr_type_node, arg);
e440a328 8041 }
8042 else if (type->interface_type() != NULL)
8043 {
8044 if (type->interface_type()->is_empty())
8045 {
8046 static tree print_empty_interface_fndecl;
8047 pfndecl = &print_empty_interface_fndecl;
8048 fnname = "__go_print_empty_interface";
8049 }
8050 else
8051 {
8052 static tree print_interface_fndecl;
8053 pfndecl = &print_interface_fndecl;
8054 fnname = "__go_print_interface";
8055 }
8056 }
411eb89e 8057 else if (type->is_slice_type())
e440a328 8058 {
8059 static tree print_slice_fndecl;
8060 pfndecl = &print_slice_fndecl;
8061 fnname = "__go_print_slice";
8062 }
8063 else
cd238b8d 8064 {
8065 go_assert(saw_errors());
8066 return error_mark_node;
8067 }
e440a328 8068
8069 tree call = Gogo::call_builtin(pfndecl,
8070 location,
8071 fnname,
8072 1,
8073 void_type_node,
8074 TREE_TYPE(arg),
8075 arg);
5fb82b5e 8076 if (call == error_mark_node)
8077 return error_mark_node;
8078 append_to_statement_list(call, &stmt_list);
e440a328 8079 }
8080 }
8081
8082 if (is_ln)
8083 {
8084 static tree print_nl_fndecl;
8085 tree call = Gogo::call_builtin(&print_nl_fndecl,
8086 location,
8087 "__go_print_nl",
8088 0,
8089 void_type_node);
5fb82b5e 8090 if (call == error_mark_node)
8091 return error_mark_node;
e440a328 8092 append_to_statement_list(call, &stmt_list);
8093 }
8094
8095 return stmt_list;
8096 }
8097
8098 case BUILTIN_PANIC:
8099 {
8100 const Expression_list* args = this->args();
c484d925 8101 go_assert(args != NULL && args->size() == 1);
e440a328 8102 Expression* arg = args->front();
8103 tree arg_tree = arg->get_tree(context);
8104 if (arg_tree == error_mark_node)
8105 return error_mark_node;
b13c66cd 8106 Type *empty =
823c7e3d 8107 Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 8108 arg_tree = Expression::convert_for_assignment(context, empty,
8109 arg->type(),
8110 arg_tree, location);
8111 static tree panic_fndecl;
8112 tree call = Gogo::call_builtin(&panic_fndecl,
8113 location,
8114 "__go_panic",
8115 1,
8116 void_type_node,
8117 TREE_TYPE(arg_tree),
8118 arg_tree);
5fb82b5e 8119 if (call == error_mark_node)
8120 return error_mark_node;
e440a328 8121 // This function will throw an exception.
8122 TREE_NOTHROW(panic_fndecl) = 0;
8123 // This function will not return.
8124 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8125 return call;
8126 }
8127
8128 case BUILTIN_RECOVER:
8129 {
8130 // The argument is set when building recover thunks. It's a
8131 // boolean value which is true if we can recover a value now.
8132 const Expression_list* args = this->args();
c484d925 8133 go_assert(args != NULL && args->size() == 1);
e440a328 8134 Expression* arg = args->front();
8135 tree arg_tree = arg->get_tree(context);
8136 if (arg_tree == error_mark_node)
8137 return error_mark_node;
8138
b13c66cd 8139 Type *empty =
823c7e3d 8140 Type::make_empty_interface_type(Linemap::predeclared_location());
9f0e0513 8141 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
e440a328 8142
8143 Type* nil_type = Type::make_nil_type();
8144 Expression* nil = Expression::make_nil(location);
8145 tree nil_tree = nil->get_tree(context);
8146 tree empty_nil_tree = Expression::convert_for_assignment(context,
8147 empty,
8148 nil_type,
8149 nil_tree,
8150 location);
8151
8152 // We need to handle a deferred call to recover specially,
8153 // because it changes whether it can recover a panic or not.
8154 // See test7 in test/recover1.go.
8155 tree call;
8156 if (this->is_deferred())
8157 {
8158 static tree deferred_recover_fndecl;
8159 call = Gogo::call_builtin(&deferred_recover_fndecl,
8160 location,
8161 "__go_deferred_recover",
8162 0,
8163 empty_tree);
8164 }
8165 else
8166 {
8167 static tree recover_fndecl;
8168 call = Gogo::call_builtin(&recover_fndecl,
8169 location,
8170 "__go_recover",
8171 0,
8172 empty_tree);
8173 }
5fb82b5e 8174 if (call == error_mark_node)
8175 return error_mark_node;
b13c66cd 8176 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
8177 arg_tree, call, empty_nil_tree);
e440a328 8178 }
8179
8180 case BUILTIN_CLOSE:
e440a328 8181 {
8182 const Expression_list* args = this->args();
c484d925 8183 go_assert(args != NULL && args->size() == 1);
e440a328 8184 Expression* arg = args->front();
8185 tree arg_tree = arg->get_tree(context);
8186 if (arg_tree == error_mark_node)
8187 return error_mark_node;
0dc2f918 8188 static tree close_fndecl;
8189 return Gogo::call_builtin(&close_fndecl,
8190 location,
8191 "__go_builtin_close",
8192 1,
8193 void_type_node,
8194 TREE_TYPE(arg_tree),
8195 arg_tree);
e440a328 8196 }
8197
8198 case BUILTIN_SIZEOF:
8199 case BUILTIN_OFFSETOF:
8200 case BUILTIN_ALIGNOF:
8201 {
0c77715b 8202 Numeric_constant nc;
8203 unsigned long val;
8204 if (!this->numeric_constant_value(&nc)
8205 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
7f1d9abd 8206 {
c484d925 8207 go_assert(saw_errors());
7f1d9abd 8208 return error_mark_node;
8209 }
7ba86326 8210 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8211 tree type = type_to_tree(uintptr_type->get_backend(gogo));
0c77715b 8212 return build_int_cst(type, val);
e440a328 8213 }
8214
8215 case BUILTIN_COPY:
8216 {
8217 const Expression_list* args = this->args();
c484d925 8218 go_assert(args != NULL && args->size() == 2);
e440a328 8219 Expression* arg1 = args->front();
8220 Expression* arg2 = args->back();
8221
8222 tree arg1_tree = arg1->get_tree(context);
8223 tree arg2_tree = arg2->get_tree(context);
8224 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8225 return error_mark_node;
8226
8227 Type* arg1_type = arg1->type();
8228 Array_type* at = arg1_type->array_type();
8229 arg1_tree = save_expr(arg1_tree);
8230 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8231 tree arg1_len = at->length_tree(gogo, arg1_tree);
d8ccb1e3 8232 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8233 return error_mark_node;
e440a328 8234
8235 Type* arg2_type = arg2->type();
8236 tree arg2_val;
8237 tree arg2_len;
411eb89e 8238 if (arg2_type->is_slice_type())
e440a328 8239 {
8240 at = arg2_type->array_type();
8241 arg2_tree = save_expr(arg2_tree);
8242 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8243 arg2_len = at->length_tree(gogo, arg2_tree);
8244 }
8245 else
8246 {
8247 arg2_tree = save_expr(arg2_tree);
8248 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8249 arg2_len = String_type::length_tree(gogo, arg2_tree);
8250 }
d8ccb1e3 8251 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8252 return error_mark_node;
e440a328 8253
8254 arg1_len = save_expr(arg1_len);
8255 arg2_len = save_expr(arg2_len);
b13c66cd 8256 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
8257 TREE_TYPE(arg1_len),
8258 fold_build2_loc(location.gcc_location(),
8259 LT_EXPR, boolean_type_node,
e440a328 8260 arg1_len, arg2_len),
8261 arg1_len, arg2_len);
8262 len = save_expr(len);
8263
8264 Type* element_type = at->element_type();
9f0e0513 8265 Btype* element_btype = element_type->get_backend(gogo);
8266 tree element_type_tree = type_to_tree(element_btype);
d8ccb1e3 8267 if (element_type_tree == error_mark_node)
8268 return error_mark_node;
e440a328 8269 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
b13c66cd 8270 tree bytecount = fold_convert_loc(location.gcc_location(),
8271 TREE_TYPE(element_size), len);
8272 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
e440a328 8273 TREE_TYPE(element_size),
8274 bytecount, element_size);
b13c66cd 8275 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
8276 bytecount);
e440a328 8277
b13c66cd 8278 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8279 arg1_val);
8280 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8281 arg2_val);
3991cb03 8282
8283 static tree copy_fndecl;
8284 tree call = Gogo::call_builtin(&copy_fndecl,
8285 location,
8286 "__go_copy",
8287 3,
8288 void_type_node,
8289 ptr_type_node,
8290 arg1_val,
8291 ptr_type_node,
8292 arg2_val,
8293 size_type_node,
8294 bytecount);
8295 if (call == error_mark_node)
8296 return error_mark_node;
e440a328 8297
b13c66cd 8298 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
8299 TREE_TYPE(len), call, len);
e440a328 8300 }
8301
8302 case BUILTIN_APPEND:
8303 {
8304 const Expression_list* args = this->args();
c484d925 8305 go_assert(args != NULL && args->size() == 2);
e440a328 8306 Expression* arg1 = args->front();
8307 Expression* arg2 = args->back();
8308
8309 tree arg1_tree = arg1->get_tree(context);
8310 tree arg2_tree = arg2->get_tree(context);
8311 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8312 return error_mark_node;
8313
9d44fbe3 8314 Array_type* at = arg1->type()->array_type();
4fd4fcf4 8315 Type* element_type = at->element_type()->forwarded();
9d44fbe3 8316
4fd4fcf4 8317 tree arg2_val;
8318 tree arg2_len;
8319 tree element_size;
8320 if (arg2->type()->is_string_type()
60963afd 8321 && element_type->integer_type() != NULL
8322 && element_type->integer_type()->is_byte())
4fd4fcf4 8323 {
8324 arg2_tree = save_expr(arg2_tree);
8325 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8326 arg2_len = String_type::length_tree(gogo, arg2_tree);
8327 element_size = size_int(1);
8328 }
8329 else
8330 {
8331 arg2_tree = Expression::convert_for_assignment(context, at,
8332 arg2->type(),
8333 arg2_tree,
8334 location);
8335 if (arg2_tree == error_mark_node)
8336 return error_mark_node;
8337
8338 arg2_tree = save_expr(arg2_tree);
8339
8340 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8341 arg2_len = at->length_tree(gogo, arg2_tree);
8342
8343 Btype* element_btype = element_type->get_backend(gogo);
8344 tree element_type_tree = type_to_tree(element_btype);
8345 if (element_type_tree == error_mark_node)
8346 return error_mark_node;
8347 element_size = TYPE_SIZE_UNIT(element_type_tree);
8348 }
ed64c8e5 8349
b13c66cd 8350 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8351 arg2_val);
8352 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
8353 arg2_len);
8354 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
3991cb03 8355 element_size);
e440a328 8356
4fd4fcf4 8357 if (arg2_val == error_mark_node
8358 || arg2_len == error_mark_node
8359 || element_size == error_mark_node)
8360 return error_mark_node;
8361
e440a328 8362 // We rebuild the decl each time since the slice types may
8363 // change.
8364 tree append_fndecl = NULL_TREE;
8365 return Gogo::call_builtin(&append_fndecl,
8366 location,
8367 "__go_append",
3991cb03 8368 4,
e440a328 8369 TREE_TYPE(arg1_tree),
e440a328 8370 TREE_TYPE(arg1_tree),
8371 arg1_tree,
3991cb03 8372 ptr_type_node,
8373 arg2_val,
8374 size_type_node,
8375 arg2_len,
8376 size_type_node,
8377 element_size);
e440a328 8378 }
8379
8380 case BUILTIN_REAL:
8381 case BUILTIN_IMAG:
8382 {
8383 const Expression_list* args = this->args();
c484d925 8384 go_assert(args != NULL && args->size() == 1);
e440a328 8385 Expression* arg = args->front();
8386 tree arg_tree = arg->get_tree(context);
8387 if (arg_tree == error_mark_node)
8388 return error_mark_node;
c484d925 8389 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
e440a328 8390 if (this->code_ == BUILTIN_REAL)
b13c66cd 8391 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
e440a328 8392 TREE_TYPE(TREE_TYPE(arg_tree)),
8393 arg_tree);
8394 else
b13c66cd 8395 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
e440a328 8396 TREE_TYPE(TREE_TYPE(arg_tree)),
8397 arg_tree);
8398 }
8399
48080209 8400 case BUILTIN_COMPLEX:
e440a328 8401 {
8402 const Expression_list* args = this->args();
c484d925 8403 go_assert(args != NULL && args->size() == 2);
e440a328 8404 tree r = args->front()->get_tree(context);
8405 tree i = args->back()->get_tree(context);
8406 if (r == error_mark_node || i == error_mark_node)
8407 return error_mark_node;
c484d925 8408 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
e440a328 8409 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
c484d925 8410 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
b13c66cd 8411 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
e440a328 8412 build_complex_type(TREE_TYPE(r)),
8413 r, i);
8414 }
8415
8416 default:
c3e6f413 8417 go_unreachable();
e440a328 8418 }
8419}
8420
8421// We have to support exporting a builtin call expression, because
8422// code can set a constant to the result of a builtin expression.
8423
8424void
8425Builtin_call_expression::do_export(Export* exp) const
8426{
0c77715b 8427 Numeric_constant nc;
8428 if (!this->numeric_constant_value(&nc))
8429 {
8430 error_at(this->location(), "value is not constant");
8431 return;
8432 }
e440a328 8433
0c77715b 8434 if (nc.is_int())
e440a328 8435 {
0c77715b 8436 mpz_t val;
8437 nc.get_int(&val);
e440a328 8438 Integer_expression::export_integer(exp, val);
0c77715b 8439 mpz_clear(val);
e440a328 8440 }
0c77715b 8441 else if (nc.is_float())
e440a328 8442 {
8443 mpfr_t fval;
0c77715b 8444 nc.get_float(&fval);
8445 Float_expression::export_float(exp, fval);
e440a328 8446 mpfr_clear(fval);
8447 }
0c77715b 8448 else if (nc.is_complex())
e440a328 8449 {
8450 mpfr_t real;
8451 mpfr_t imag;
0c77715b 8452 Complex_expression::export_complex(exp, real, imag);
e440a328 8453 mpfr_clear(real);
8454 mpfr_clear(imag);
8455 }
0c77715b 8456 else
8457 go_unreachable();
e440a328 8458
8459 // A trailing space lets us reliably identify the end of the number.
8460 exp->write_c_string(" ");
8461}
8462
8463// Class Call_expression.
8464
8465// Traversal.
8466
8467int
8468Call_expression::do_traverse(Traverse* traverse)
8469{
8470 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8471 return TRAVERSE_EXIT;
8472 if (this->args_ != NULL)
8473 {
8474 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8475 return TRAVERSE_EXIT;
8476 }
8477 return TRAVERSE_CONTINUE;
8478}
8479
8480// Lower a call statement.
8481
8482Expression*
ceeb4318 8483Call_expression::do_lower(Gogo* gogo, Named_object* function,
8484 Statement_inserter* inserter, int)
e440a328 8485{
b13c66cd 8486 Location loc = this->location();
09ea332d 8487
ceeb4318 8488 // A type cast can look like a function call.
e440a328 8489 if (this->fn_->is_type_expression()
8490 && this->args_ != NULL
8491 && this->args_->size() == 1)
8492 return Expression::make_cast(this->fn_->type(), this->args_->front(),
09ea332d 8493 loc);
e440a328 8494
8495 // Recognize a call to a builtin function.
8496 Func_expression* fne = this->fn_->func_expression();
8497 if (fne != NULL
8498 && fne->named_object()->is_function_declaration()
8499 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8500 return new Builtin_call_expression(gogo, this->fn_, this->args_,
09ea332d 8501 this->is_varargs_, loc);
e440a328 8502
8503 // Handle an argument which is a call to a function which returns
8504 // multiple results.
8505 if (this->args_ != NULL
8506 && this->args_->size() == 1
8507 && this->args_->front()->call_expression() != NULL
8508 && this->fn_->type()->function_type() != NULL)
8509 {
8510 Function_type* fntype = this->fn_->type()->function_type();
8511 size_t rc = this->args_->front()->call_expression()->result_count();
8512 if (rc > 1
8513 && fntype->parameters() != NULL
8514 && (fntype->parameters()->size() == rc
8515 || (fntype->is_varargs()
8516 && fntype->parameters()->size() - 1 <= rc)))
8517 {
8518 Call_expression* call = this->args_->front()->call_expression();
8519 Expression_list* args = new Expression_list;
8520 for (size_t i = 0; i < rc; ++i)
8521 args->push_back(Expression::make_call_result(call, i));
8522 // We can't return a new call expression here, because this
42535814 8523 // one may be referenced by Call_result expressions. We
8524 // also can't delete the old arguments, because we may still
8525 // traverse them somewhere up the call stack. FIXME.
e440a328 8526 this->args_ = args;
8527 }
8528 }
8529
ceeb4318 8530 // If this call returns multiple results, create a temporary
8531 // variable for each result.
8532 size_t rc = this->result_count();
8533 if (rc > 1 && this->results_ == NULL)
8534 {
8535 std::vector<Temporary_statement*>* temps =
8536 new std::vector<Temporary_statement*>;
8537 temps->reserve(rc);
8538 const Typed_identifier_list* results =
8539 this->fn_->type()->function_type()->results();
8540 for (Typed_identifier_list::const_iterator p = results->begin();
8541 p != results->end();
8542 ++p)
8543 {
8544 Temporary_statement* temp = Statement::make_temporary(p->type(),
09ea332d 8545 NULL, loc);
ceeb4318 8546 inserter->insert(temp);
8547 temps->push_back(temp);
8548 }
8549 this->results_ = temps;
8550 }
8551
e440a328 8552 // Handle a call to a varargs function by packaging up the extra
8553 // parameters.
8554 if (this->fn_->type()->function_type() != NULL
8555 && this->fn_->type()->function_type()->is_varargs())
8556 {
8557 Function_type* fntype = this->fn_->type()->function_type();
8558 const Typed_identifier_list* parameters = fntype->parameters();
c484d925 8559 go_assert(parameters != NULL && !parameters->empty());
e440a328 8560 Type* varargs_type = parameters->back().type();
09ea332d 8561 this->lower_varargs(gogo, function, inserter, varargs_type,
8562 parameters->size());
8563 }
8564
8565 // If this is call to a method, call the method directly passing the
8566 // object as the first parameter.
8567 Bound_method_expression* bme = this->fn_->bound_method_expression();
8568 if (bme != NULL)
8569 {
8570 Named_object* method = bme->method();
8571 Expression* first_arg = bme->first_argument();
8572
8573 // We always pass a pointer when calling a method.
8574 if (first_arg->type()->points_to() == NULL
8575 && !first_arg->type()->is_error())
8576 {
8577 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
8578 // We may need to create a temporary variable so that we can
8579 // take the address. We can't do that here because it will
8580 // mess up the order of evaluation.
8581 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
8582 ue->set_create_temp();
8583 }
8584
8585 // If we are calling a method which was inherited from an
8586 // embedded struct, and the method did not get a stub, then the
8587 // first type may be wrong.
8588 Type* fatype = bme->first_argument_type();
8589 if (fatype != NULL)
8590 {
8591 if (fatype->points_to() == NULL)
8592 fatype = Type::make_pointer_type(fatype);
8593 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
8594 }
8595
8596 Expression_list* new_args = new Expression_list();
8597 new_args->push_back(first_arg);
8598 if (this->args_ != NULL)
8599 {
8600 for (Expression_list::const_iterator p = this->args_->begin();
8601 p != this->args_->end();
8602 ++p)
8603 new_args->push_back(*p);
8604 }
8605
8606 // We have to change in place because this structure may be
8607 // referenced by Call_result_expressions. We can't delete the
8608 // old arguments, because we may be traversing them up in some
8609 // caller. FIXME.
8610 this->args_ = new_args;
8611 this->fn_ = Expression::make_func_reference(method, NULL,
8612 bme->location());
e440a328 8613 }
8614
8615 return this;
8616}
8617
8618// Lower a call to a varargs function. FUNCTION is the function in
8619// which the call occurs--it's not the function we are calling.
8620// VARARGS_TYPE is the type of the varargs parameter, a slice type.
8621// PARAM_COUNT is the number of parameters of the function we are
8622// calling; the last of these parameters will be the varargs
8623// parameter.
8624
09ea332d 8625void
e440a328 8626Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
ceeb4318 8627 Statement_inserter* inserter,
e440a328 8628 Type* varargs_type, size_t param_count)
8629{
8630 if (this->varargs_are_lowered_)
09ea332d 8631 return;
e440a328 8632
b13c66cd 8633 Location loc = this->location();
e440a328 8634
c484d925 8635 go_assert(param_count > 0);
411eb89e 8636 go_assert(varargs_type->is_slice_type());
e440a328 8637
8638 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8639 if (arg_count < param_count - 1)
8640 {
8641 // Not enough arguments; will be caught in check_types.
09ea332d 8642 return;
e440a328 8643 }
8644
8645 Expression_list* old_args = this->args_;
8646 Expression_list* new_args = new Expression_list();
8647 bool push_empty_arg = false;
8648 if (old_args == NULL || old_args->empty())
8649 {
c484d925 8650 go_assert(param_count == 1);
e440a328 8651 push_empty_arg = true;
8652 }
8653 else
8654 {
8655 Expression_list::const_iterator pa;
8656 int i = 1;
8657 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8658 {
8659 if (static_cast<size_t>(i) == param_count)
8660 break;
8661 new_args->push_back(*pa);
8662 }
8663
8664 // We have reached the varargs parameter.
8665
8666 bool issued_error = false;
8667 if (pa == old_args->end())
8668 push_empty_arg = true;
8669 else if (pa + 1 == old_args->end() && this->is_varargs_)
8670 new_args->push_back(*pa);
8671 else if (this->is_varargs_)
8672 {
a6645f74 8673 if ((*pa)->type()->is_slice_type())
8674 this->report_error(_("too many arguments"));
8675 else
8676 {
8677 error_at(this->location(),
8678 _("invalid use of %<...%> with non-slice"));
8679 this->set_is_error();
8680 }
09ea332d 8681 return;
e440a328 8682 }
e440a328 8683 else
8684 {
8685 Type* element_type = varargs_type->array_type()->element_type();
8686 Expression_list* vals = new Expression_list;
8687 for (; pa != old_args->end(); ++pa, ++i)
8688 {
8689 // Check types here so that we get a better message.
8690 Type* patype = (*pa)->type();
b13c66cd 8691 Location paloc = (*pa)->location();
e440a328 8692 if (!this->check_argument_type(i, element_type, patype,
8693 paloc, issued_error))
8694 continue;
8695 vals->push_back(*pa);
8696 }
8697 Expression* val =
8698 Expression::make_slice_composite_literal(varargs_type, vals, loc);
09ea332d 8699 gogo->lower_expression(function, inserter, &val);
e440a328 8700 new_args->push_back(val);
8701 }
8702 }
8703
8704 if (push_empty_arg)
8705 new_args->push_back(Expression::make_nil(loc));
8706
8707 // We can't return a new call expression here, because this one may
6d4c2432 8708 // be referenced by Call_result expressions. FIXME. We can't
8709 // delete OLD_ARGS because we may have both a Call_expression and a
8710 // Builtin_call_expression which refer to them. FIXME.
e440a328 8711 this->args_ = new_args;
8712 this->varargs_are_lowered_ = true;
e440a328 8713}
8714
ceeb4318 8715// Get the function type. This can return NULL in error cases.
e440a328 8716
8717Function_type*
8718Call_expression::get_function_type() const
8719{
8720 return this->fn_->type()->function_type();
8721}
8722
8723// Return the number of values which this call will return.
8724
8725size_t
8726Call_expression::result_count() const
8727{
8728 const Function_type* fntype = this->get_function_type();
8729 if (fntype == NULL)
8730 return 0;
8731 if (fntype->results() == NULL)
8732 return 0;
8733 return fntype->results()->size();
8734}
8735
ceeb4318 8736// Return the temporary which holds a result.
8737
8738Temporary_statement*
8739Call_expression::result(size_t i) const
8740{
cd238b8d 8741 if (this->results_ == NULL || this->results_->size() <= i)
8742 {
8743 go_assert(saw_errors());
8744 return NULL;
8745 }
ceeb4318 8746 return (*this->results_)[i];
8747}
8748
e440a328 8749// Return whether this is a call to the predeclared function recover.
8750
8751bool
8752Call_expression::is_recover_call() const
8753{
8754 return this->do_is_recover_call();
8755}
8756
8757// Set the argument to the recover function.
8758
8759void
8760Call_expression::set_recover_arg(Expression* arg)
8761{
8762 this->do_set_recover_arg(arg);
8763}
8764
8765// Virtual functions also implemented by Builtin_call_expression.
8766
8767bool
8768Call_expression::do_is_recover_call() const
8769{
8770 return false;
8771}
8772
8773void
8774Call_expression::do_set_recover_arg(Expression*)
8775{
c3e6f413 8776 go_unreachable();
e440a328 8777}
8778
ceeb4318 8779// We have found an error with this call expression; return true if
8780// we should report it.
8781
8782bool
8783Call_expression::issue_error()
8784{
8785 if (this->issued_error_)
8786 return false;
8787 else
8788 {
8789 this->issued_error_ = true;
8790 return true;
8791 }
8792}
8793
e440a328 8794// Get the type.
8795
8796Type*
8797Call_expression::do_type()
8798{
8799 if (this->type_ != NULL)
8800 return this->type_;
8801
8802 Type* ret;
8803 Function_type* fntype = this->get_function_type();
8804 if (fntype == NULL)
8805 return Type::make_error_type();
8806
8807 const Typed_identifier_list* results = fntype->results();
8808 if (results == NULL)
8809 ret = Type::make_void_type();
8810 else if (results->size() == 1)
8811 ret = results->begin()->type();
8812 else
8813 ret = Type::make_call_multiple_result_type(this);
8814
8815 this->type_ = ret;
8816
8817 return this->type_;
8818}
8819
8820// Determine types for a call expression. We can use the function
8821// parameter types to set the types of the arguments.
8822
8823void
8824Call_expression::do_determine_type(const Type_context*)
8825{
fb94b0ca 8826 if (!this->determining_types())
8827 return;
8828
e440a328 8829 this->fn_->determine_type_no_context();
8830 Function_type* fntype = this->get_function_type();
8831 const Typed_identifier_list* parameters = NULL;
8832 if (fntype != NULL)
8833 parameters = fntype->parameters();
8834 if (this->args_ != NULL)
8835 {
8836 Typed_identifier_list::const_iterator pt;
8837 if (parameters != NULL)
8838 pt = parameters->begin();
09ea332d 8839 bool first = true;
e440a328 8840 for (Expression_list::const_iterator pa = this->args_->begin();
8841 pa != this->args_->end();
8842 ++pa)
8843 {
09ea332d 8844 if (first)
8845 {
8846 first = false;
8847 // If this is a method, the first argument is the
8848 // receiver.
8849 if (fntype != NULL && fntype->is_method())
8850 {
8851 Type* rtype = fntype->receiver()->type();
8852 // The receiver is always passed as a pointer.
8853 if (rtype->points_to() == NULL)
8854 rtype = Type::make_pointer_type(rtype);
8855 Type_context subcontext(rtype, false);
8856 (*pa)->determine_type(&subcontext);
8857 continue;
8858 }
8859 }
8860
e440a328 8861 if (parameters != NULL && pt != parameters->end())
8862 {
8863 Type_context subcontext(pt->type(), false);
8864 (*pa)->determine_type(&subcontext);
8865 ++pt;
8866 }
8867 else
8868 (*pa)->determine_type_no_context();
8869 }
8870 }
8871}
8872
fb94b0ca 8873// Called when determining types for a Call_expression. Return true
8874// if we should go ahead, false if they have already been determined.
8875
8876bool
8877Call_expression::determining_types()
8878{
8879 if (this->types_are_determined_)
8880 return false;
8881 else
8882 {
8883 this->types_are_determined_ = true;
8884 return true;
8885 }
8886}
8887
e440a328 8888// Check types for parameter I.
8889
8890bool
8891Call_expression::check_argument_type(int i, const Type* parameter_type,
8892 const Type* argument_type,
b13c66cd 8893 Location argument_location,
e440a328 8894 bool issued_error)
8895{
8896 std::string reason;
053ee6ca 8897 bool ok;
8898 if (this->are_hidden_fields_ok_)
8899 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
8900 &reason);
8901 else
8902 ok = Type::are_assignable(parameter_type, argument_type, &reason);
8903 if (!ok)
e440a328 8904 {
8905 if (!issued_error)
8906 {
8907 if (reason.empty())
8908 error_at(argument_location, "argument %d has incompatible type", i);
8909 else
8910 error_at(argument_location,
8911 "argument %d has incompatible type (%s)",
8912 i, reason.c_str());
8913 }
8914 this->set_is_error();
8915 return false;
8916 }
8917 return true;
8918}
8919
8920// Check types.
8921
8922void
8923Call_expression::do_check_types(Gogo*)
8924{
a6645f74 8925 if (this->classification() == EXPRESSION_ERROR)
8926 return;
8927
e440a328 8928 Function_type* fntype = this->get_function_type();
8929 if (fntype == NULL)
8930 {
5c13bd80 8931 if (!this->fn_->type()->is_error())
e440a328 8932 this->report_error(_("expected function"));
8933 return;
8934 }
8935
09ea332d 8936 bool is_method = fntype->is_method();
8937 if (is_method)
e440a328 8938 {
09ea332d 8939 go_assert(this->args_ != NULL && !this->args_->empty());
8940 Type* rtype = fntype->receiver()->type();
8941 Expression* first_arg = this->args_->front();
8942 // The language permits copying hidden fields for a method
8943 // receiver. We dereference the values since receivers are
8944 // always passed as pointers.
8945 std::string reason;
8946 if (!Type::are_assignable_hidden_ok(rtype->deref(),
8947 first_arg->type()->deref(),
8948 &reason))
e440a328 8949 {
09ea332d 8950 if (reason.empty())
8951 this->report_error(_("incompatible type for receiver"));
8952 else
e440a328 8953 {
09ea332d 8954 error_at(this->location(),
8955 "incompatible type for receiver (%s)",
8956 reason.c_str());
8957 this->set_is_error();
e440a328 8958 }
8959 }
8960 }
8961
8962 // Note that varargs was handled by the lower_varargs() method, so
a6645f74 8963 // we don't have to worry about it here unless something is wrong.
8964 if (this->is_varargs_ && !this->varargs_are_lowered_)
8965 {
8966 if (!fntype->is_varargs())
8967 {
8968 error_at(this->location(),
8969 _("invalid use of %<...%> calling non-variadic function"));
8970 this->set_is_error();
8971 return;
8972 }
8973 }
e440a328 8974
8975 const Typed_identifier_list* parameters = fntype->parameters();
8976 if (this->args_ == NULL)
8977 {
8978 if (parameters != NULL && !parameters->empty())
8979 this->report_error(_("not enough arguments"));
8980 }
8981 else if (parameters == NULL)
09ea332d 8982 {
8983 if (!is_method || this->args_->size() > 1)
8984 this->report_error(_("too many arguments"));
8985 }
e440a328 8986 else
8987 {
8988 int i = 0;
09ea332d 8989 Expression_list::const_iterator pa = this->args_->begin();
8990 if (is_method)
8991 ++pa;
8992 for (Typed_identifier_list::const_iterator pt = parameters->begin();
8993 pt != parameters->end();
8994 ++pt, ++pa, ++i)
e440a328 8995 {
09ea332d 8996 if (pa == this->args_->end())
e440a328 8997 {
09ea332d 8998 this->report_error(_("not enough arguments"));
e440a328 8999 return;
9000 }
9001 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9002 (*pa)->location(), false);
9003 }
09ea332d 9004 if (pa != this->args_->end())
9005 this->report_error(_("too many arguments"));
e440a328 9006 }
9007}
9008
9009// Return whether we have to use a temporary variable to ensure that
9010// we evaluate this call expression in order. If the call returns no
ceeb4318 9011// results then it will inevitably be executed last.
e440a328 9012
9013bool
9014Call_expression::do_must_eval_in_order() const
9015{
ceeb4318 9016 return this->result_count() > 0;
e440a328 9017}
9018
e440a328 9019// Get the function and the first argument to use when calling an
9020// interface method.
9021
9022tree
9023Call_expression::interface_method_function(
9024 Translate_context* context,
9025 Interface_field_reference_expression* interface_method,
9026 tree* first_arg_ptr)
9027{
9028 tree expr = interface_method->expr()->get_tree(context);
9029 if (expr == error_mark_node)
9030 return error_mark_node;
9031 expr = save_expr(expr);
9032 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9033 if (first_arg == error_mark_node)
9034 return error_mark_node;
9035 *first_arg_ptr = first_arg;
9036 return interface_method->get_function_tree(context, expr);
9037}
9038
9039// Build the call expression.
9040
9041tree
9042Call_expression::do_get_tree(Translate_context* context)
9043{
9044 if (this->tree_ != NULL_TREE)
9045 return this->tree_;
9046
9047 Function_type* fntype = this->get_function_type();
9048 if (fntype == NULL)
9049 return error_mark_node;
9050
9051 if (this->fn_->is_error_expression())
9052 return error_mark_node;
9053
9054 Gogo* gogo = context->gogo();
b13c66cd 9055 Location location = this->location();
e440a328 9056
9057 Func_expression* func = this->fn_->func_expression();
e440a328 9058 Interface_field_reference_expression* interface_method =
9059 this->fn_->interface_field_reference_expression();
9060 const bool has_closure = func != NULL && func->closure() != NULL;
09ea332d 9061 const bool is_interface_method = interface_method != NULL;
e440a328 9062
9063 int nargs;
9064 tree* args;
9065 if (this->args_ == NULL || this->args_->empty())
9066 {
09ea332d 9067 nargs = is_interface_method ? 1 : 0;
e440a328 9068 args = nargs == 0 ? NULL : new tree[nargs];
9069 }
09ea332d 9070 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9071 {
9072 // Passing a receiver parameter.
9073 go_assert(!is_interface_method
9074 && fntype->is_method()
9075 && this->args_->size() == 1);
9076 nargs = 1;
9077 args = new tree[nargs];
9078 args[0] = this->args_->front()->get_tree(context);
9079 }
e440a328 9080 else
9081 {
9082 const Typed_identifier_list* params = fntype->parameters();
e440a328 9083
9084 nargs = this->args_->size();
09ea332d 9085 int i = is_interface_method ? 1 : 0;
e440a328 9086 nargs += i;
9087 args = new tree[nargs];
9088
9089 Typed_identifier_list::const_iterator pp = params->begin();
09ea332d 9090 Expression_list::const_iterator pe = this->args_->begin();
9091 if (!is_interface_method && fntype->is_method())
9092 {
9093 args[i] = (*pe)->get_tree(context);
9094 ++pe;
9095 ++i;
9096 }
9097 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
e440a328 9098 {
c484d925 9099 go_assert(pp != params->end());
e440a328 9100 tree arg_val = (*pe)->get_tree(context);
9101 args[i] = Expression::convert_for_assignment(context,
9102 pp->type(),
9103 (*pe)->type(),
9104 arg_val,
9105 location);
9106 if (args[i] == error_mark_node)
cf609de4 9107 {
9108 delete[] args;
9109 return error_mark_node;
9110 }
e440a328 9111 }
c484d925 9112 go_assert(pp == params->end());
9113 go_assert(i == nargs);
e440a328 9114 }
9115
9f0e0513 9116 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
e440a328 9117 if (rettype == error_mark_node)
cf609de4 9118 {
9119 delete[] args;
9120 return error_mark_node;
9121 }
e440a328 9122
9123 tree fn;
9124 if (has_closure)
9125 fn = func->get_tree_without_closure(gogo);
09ea332d 9126 else if (!is_interface_method)
e440a328 9127 fn = this->fn_->get_tree(context);
e440a328 9128 else
09ea332d 9129 fn = this->interface_method_function(context, interface_method, &args[0]);
e440a328 9130
9131 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
cf609de4 9132 {
9133 delete[] args;
9134 return error_mark_node;
9135 }
e440a328 9136
e440a328 9137 tree fndecl = fn;
9138 if (TREE_CODE(fndecl) == ADDR_EXPR)
9139 fndecl = TREE_OPERAND(fndecl, 0);
9aa9e2df 9140
9141 // Add a type cast in case the type of the function is a recursive
9142 // type which refers to itself.
9143 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
9144 {
9f0e0513 9145 tree fnt = type_to_tree(fntype->get_backend(gogo));
9aa9e2df 9146 if (fnt == error_mark_node)
9147 return error_mark_node;
b13c66cd 9148 fn = fold_convert_loc(location.gcc_location(), fnt, fn);
9aa9e2df 9149 }
9150
9151 // This is to support builtin math functions when using 80387 math.
e440a328 9152 tree excess_type = NULL_TREE;
68e1881d 9153 if (optimize
9154 && TREE_CODE(fndecl) == FUNCTION_DECL
e440a328 9155 && DECL_IS_BUILTIN(fndecl)
9156 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
9157 && nargs > 0
9158 && ((SCALAR_FLOAT_TYPE_P(rettype)
9159 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
9160 || (COMPLEX_FLOAT_TYPE_P(rettype)
9161 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
9162 {
9163 excess_type = excess_precision_type(TREE_TYPE(args[0]));
9164 if (excess_type != NULL_TREE)
9165 {
9166 tree excess_fndecl = mathfn_built_in(excess_type,
9167 DECL_FUNCTION_CODE(fndecl));
9168 if (excess_fndecl == NULL_TREE)
9169 excess_type = NULL_TREE;
9170 else
9171 {
b13c66cd 9172 fn = build_fold_addr_expr_loc(location.gcc_location(),
9173 excess_fndecl);
e440a328 9174 for (int i = 0; i < nargs; ++i)
26ae0101 9175 {
9176 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
9177 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
9178 args[i] = ::convert(excess_type, args[i]);
9179 }
e440a328 9180 }
9181 }
9182 }
9183
9184 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9185 fn, nargs, args);
9186 delete[] args;
9187
b13c66cd 9188 SET_EXPR_LOCATION(ret, location.gcc_location());
e440a328 9189
9190 if (has_closure)
9191 {
9192 tree closure_tree = func->closure()->get_tree(context);
9193 if (closure_tree != error_mark_node)
9194 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
9195 }
9196
9197 // If this is a recursive function type which returns itself, as in
9198 // type F func() F
9199 // we have used ptr_type_node for the return type. Add a cast here
9200 // to the correct type.
9201 if (TREE_TYPE(ret) == ptr_type_node)
9202 {
9f0e0513 9203 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
b13c66cd 9204 ret = fold_convert_loc(location.gcc_location(), t, ret);
e440a328 9205 }
9206
9207 if (excess_type != NULL_TREE)
9208 {
9209 // Calling convert here can undo our excess precision change.
9210 // That may or may not be a bug in convert_to_real.
9211 ret = build1(NOP_EXPR, rettype, ret);
9212 }
9213
ceeb4318 9214 if (this->results_ != NULL)
9215 ret = this->set_results(context, ret);
e440a328 9216
9217 this->tree_ = ret;
9218
9219 return ret;
9220}
9221
ceeb4318 9222// Set the result variables if this call returns multiple results.
9223
9224tree
9225Call_expression::set_results(Translate_context* context, tree call_tree)
9226{
9227 tree stmt_list = NULL_TREE;
9228
9229 call_tree = save_expr(call_tree);
9230
9231 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9232 {
9233 go_assert(saw_errors());
9234 return call_tree;
9235 }
9236
b13c66cd 9237 Location loc = this->location();
ceeb4318 9238 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9239 size_t rc = this->result_count();
9240 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
9241 {
9242 go_assert(field != NULL_TREE);
9243
9244 Temporary_statement* temp = this->result(i);
cd238b8d 9245 if (temp == NULL)
9246 {
9247 go_assert(saw_errors());
9248 return error_mark_node;
9249 }
ceeb4318 9250 Temporary_reference_expression* ref =
9251 Expression::make_temporary_reference(temp, loc);
9252 ref->set_is_lvalue();
9253 tree temp_tree = ref->get_tree(context);
9254 if (temp_tree == error_mark_node)
423d1705 9255 return error_mark_node;
ceeb4318 9256
b13c66cd 9257 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
9258 TREE_TYPE(field), call_tree, field, NULL_TREE);
9259 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
9260 void_type_node, temp_tree, val_tree);
ceeb4318 9261
9262 append_to_statement_list(set_tree, &stmt_list);
9263 }
9264 go_assert(field == NULL_TREE);
9265
9266 return save_expr(stmt_list);
9267}
9268
d751bb78 9269// Dump ast representation for a call expressin.
9270
9271void
9272Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
9273{
9274 this->fn_->dump_expression(ast_dump_context);
9275 ast_dump_context->ostream() << "(";
9276 if (args_ != NULL)
9277 ast_dump_context->dump_expression_list(this->args_);
9278
9279 ast_dump_context->ostream() << ") ";
9280}
9281
e440a328 9282// Make a call expression.
9283
9284Call_expression*
9285Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
b13c66cd 9286 Location location)
e440a328 9287{
9288 return new Call_expression(fn, args, is_varargs, location);
9289}
9290
9291// A single result from a call which returns multiple results.
9292
9293class Call_result_expression : public Expression
9294{
9295 public:
9296 Call_result_expression(Call_expression* call, unsigned int index)
9297 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9298 call_(call), index_(index)
9299 { }
9300
9301 protected:
9302 int
9303 do_traverse(Traverse*);
9304
9305 Type*
9306 do_type();
9307
9308 void
9309 do_determine_type(const Type_context*);
9310
9311 void
9312 do_check_types(Gogo*);
9313
9314 Expression*
9315 do_copy()
9316 {
9317 return new Call_result_expression(this->call_->call_expression(),
9318 this->index_);
9319 }
9320
9321 bool
9322 do_must_eval_in_order() const
9323 { return true; }
9324
9325 tree
9326 do_get_tree(Translate_context*);
9327
d751bb78 9328 void
9329 do_dump_expression(Ast_dump_context*) const;
9330
e440a328 9331 private:
9332 // The underlying call expression.
9333 Expression* call_;
9334 // Which result we want.
9335 unsigned int index_;
9336};
9337
9338// Traverse a call result.
9339
9340int
9341Call_result_expression::do_traverse(Traverse* traverse)
9342{
9343 if (traverse->remember_expression(this->call_))
9344 {
9345 // We have already traversed the call expression.
9346 return TRAVERSE_CONTINUE;
9347 }
9348 return Expression::traverse(&this->call_, traverse);
9349}
9350
9351// Get the type.
9352
9353Type*
9354Call_result_expression::do_type()
9355{
425dd051 9356 if (this->classification() == EXPRESSION_ERROR)
9357 return Type::make_error_type();
9358
e440a328 9359 // THIS->CALL_ can be replaced with a temporary reference due to
9360 // Call_expression::do_must_eval_in_order when there is an error.
9361 Call_expression* ce = this->call_->call_expression();
9362 if (ce == NULL)
5e85f268 9363 {
9364 this->set_is_error();
9365 return Type::make_error_type();
9366 }
e440a328 9367 Function_type* fntype = ce->get_function_type();
9368 if (fntype == NULL)
5e85f268 9369 {
e37658e2 9370 if (ce->issue_error())
99b3f06f 9371 {
9372 if (!ce->fn()->type()->is_error())
9373 this->report_error(_("expected function"));
9374 }
5e85f268 9375 this->set_is_error();
9376 return Type::make_error_type();
9377 }
e440a328 9378 const Typed_identifier_list* results = fntype->results();
ceeb4318 9379 if (results == NULL || results->size() < 2)
7b8d861f 9380 {
ceeb4318 9381 if (ce->issue_error())
9382 this->report_error(_("number of results does not match "
9383 "number of values"));
7b8d861f 9384 return Type::make_error_type();
9385 }
e440a328 9386 Typed_identifier_list::const_iterator pr = results->begin();
9387 for (unsigned int i = 0; i < this->index_; ++i)
9388 {
9389 if (pr == results->end())
425dd051 9390 break;
e440a328 9391 ++pr;
9392 }
9393 if (pr == results->end())
425dd051 9394 {
ceeb4318 9395 if (ce->issue_error())
9396 this->report_error(_("number of results does not match "
9397 "number of values"));
425dd051 9398 return Type::make_error_type();
9399 }
e440a328 9400 return pr->type();
9401}
9402
425dd051 9403// Check the type. Just make sure that we trigger the warning in
9404// do_type.
e440a328 9405
9406void
9407Call_result_expression::do_check_types(Gogo*)
9408{
425dd051 9409 this->type();
e440a328 9410}
9411
9412// Determine the type. We have nothing to do here, but the 0 result
9413// needs to pass down to the caller.
9414
9415void
9416Call_result_expression::do_determine_type(const Type_context*)
9417{
fb94b0ca 9418 this->call_->determine_type_no_context();
e440a328 9419}
9420
ceeb4318 9421// Return the tree. We just refer to the temporary set by the call
9422// expression. We don't do this at lowering time because it makes it
9423// hard to evaluate the call at the right time.
e440a328 9424
9425tree
9426Call_result_expression::do_get_tree(Translate_context* context)
9427{
ceeb4318 9428 Call_expression* ce = this->call_->call_expression();
cd238b8d 9429 if (ce == NULL)
9430 {
9431 go_assert(this->call_->is_error_expression());
9432 return error_mark_node;
9433 }
ceeb4318 9434 Temporary_statement* ts = ce->result(this->index_);
cd238b8d 9435 if (ts == NULL)
9436 {
9437 go_assert(saw_errors());
9438 return error_mark_node;
9439 }
ceeb4318 9440 Expression* ref = Expression::make_temporary_reference(ts, this->location());
9441 return ref->get_tree(context);
e440a328 9442}
9443
d751bb78 9444// Dump ast representation for a call result expression.
9445
9446void
9447Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9448 const
9449{
9450 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9451 // (struct) and the fields are referenced instead.
9452 ast_dump_context->ostream() << this->index_ << "@(";
9453 ast_dump_context->dump_expression(this->call_);
9454 ast_dump_context->ostream() << ")";
9455}
9456
e440a328 9457// Make a reference to a single result of a call which returns
9458// multiple results.
9459
9460Expression*
9461Expression::make_call_result(Call_expression* call, unsigned int index)
9462{
9463 return new Call_result_expression(call, index);
9464}
9465
9466// Class Index_expression.
9467
9468// Traversal.
9469
9470int
9471Index_expression::do_traverse(Traverse* traverse)
9472{
9473 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9474 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9475 || (this->end_ != NULL
9476 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9477 return TRAVERSE_EXIT;
9478 return TRAVERSE_CONTINUE;
9479}
9480
9481// Lower an index expression. This converts the generic index
9482// expression into an array index, a string index, or a map index.
9483
9484Expression*
ceeb4318 9485Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 9486{
b13c66cd 9487 Location location = this->location();
e440a328 9488 Expression* left = this->left_;
9489 Expression* start = this->start_;
9490 Expression* end = this->end_;
9491
9492 Type* type = left->type();
5c13bd80 9493 if (type->is_error())
e440a328 9494 return Expression::make_error(location);
b0cf7ddd 9495 else if (left->is_type_expression())
9496 {
9497 error_at(location, "attempt to index type expression");
9498 return Expression::make_error(location);
9499 }
e440a328 9500 else if (type->array_type() != NULL)
9501 return Expression::make_array_index(left, start, end, location);
9502 else if (type->points_to() != NULL
9503 && type->points_to()->array_type() != NULL
411eb89e 9504 && !type->points_to()->is_slice_type())
e440a328 9505 {
9506 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9507 location);
9508 return Expression::make_array_index(deref, start, end, location);
9509 }
9510 else if (type->is_string_type())
9511 return Expression::make_string_index(left, start, end, location);
9512 else if (type->map_type() != NULL)
9513 {
9514 if (end != NULL)
9515 {
9516 error_at(location, "invalid slice of map");
9517 return Expression::make_error(location);
9518 }
6d4c2432 9519 Map_index_expression* ret = Expression::make_map_index(left, start,
9520 location);
e440a328 9521 if (this->is_lvalue_)
9522 ret->set_is_lvalue();
9523 return ret;
9524 }
9525 else
9526 {
9527 error_at(location,
9528 "attempt to index object which is not array, string, or map");
9529 return Expression::make_error(location);
9530 }
9531}
9532
d751bb78 9533// Write an indexed expression (expr[expr:expr] or expr[expr]) to a
9534// dump context
9535
9536void
9537Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
9538 const Expression* expr,
9539 const Expression* start,
9540 const Expression* end)
9541{
9542 expr->dump_expression(ast_dump_context);
9543 ast_dump_context->ostream() << "[";
9544 start->dump_expression(ast_dump_context);
9545 if (end != NULL)
9546 {
9547 ast_dump_context->ostream() << ":";
9548 end->dump_expression(ast_dump_context);
9549 }
9550 ast_dump_context->ostream() << "]";
9551}
9552
9553// Dump ast representation for an index expression.
9554
9555void
9556Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9557 const
9558{
9559 Index_expression::dump_index_expression(ast_dump_context, this->left_,
9560 this->start_, this->end_);
9561}
9562
e440a328 9563// Make an index expression.
9564
9565Expression*
9566Expression::make_index(Expression* left, Expression* start, Expression* end,
b13c66cd 9567 Location location)
e440a328 9568{
9569 return new Index_expression(left, start, end, location);
9570}
9571
9572// An array index. This is used for both indexing and slicing.
9573
9574class Array_index_expression : public Expression
9575{
9576 public:
9577 Array_index_expression(Expression* array, Expression* start,
b13c66cd 9578 Expression* end, Location location)
e440a328 9579 : Expression(EXPRESSION_ARRAY_INDEX, location),
9580 array_(array), start_(start), end_(end), type_(NULL)
9581 { }
9582
9583 protected:
9584 int
9585 do_traverse(Traverse*);
9586
9587 Type*
9588 do_type();
9589
9590 void
9591 do_determine_type(const Type_context*);
9592
9593 void
9594 do_check_types(Gogo*);
9595
9596 Expression*
9597 do_copy()
9598 {
9599 return Expression::make_array_index(this->array_->copy(),
9600 this->start_->copy(),
9601 (this->end_ == NULL
9602 ? NULL
9603 : this->end_->copy()),
9604 this->location());
9605 }
9606
baef9f7a 9607 bool
9608 do_must_eval_subexpressions_in_order(int* skip) const
9609 {
9610 *skip = 1;
9611 return true;
9612 }
9613
e440a328 9614 bool
9615 do_is_addressable() const;
9616
9617 void
9618 do_address_taken(bool escapes)
9619 { this->array_->address_taken(escapes); }
9620
9621 tree
9622 do_get_tree(Translate_context*);
9623
d751bb78 9624 void
9625 do_dump_expression(Ast_dump_context*) const;
9626
e440a328 9627 private:
9628 // The array we are getting a value from.
9629 Expression* array_;
9630 // The start or only index.
9631 Expression* start_;
9632 // The end index of a slice. This may be NULL for a simple array
9633 // index, or it may be a nil expression for the length of the array.
9634 Expression* end_;
9635 // The type of the expression.
9636 Type* type_;
9637};
9638
9639// Array index traversal.
9640
9641int
9642Array_index_expression::do_traverse(Traverse* traverse)
9643{
9644 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9645 return TRAVERSE_EXIT;
9646 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9647 return TRAVERSE_EXIT;
9648 if (this->end_ != NULL)
9649 {
9650 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9651 return TRAVERSE_EXIT;
9652 }
9653 return TRAVERSE_CONTINUE;
9654}
9655
9656// Return the type of an array index.
9657
9658Type*
9659Array_index_expression::do_type()
9660{
9661 if (this->type_ == NULL)
9662 {
9663 Array_type* type = this->array_->type()->array_type();
9664 if (type == NULL)
9665 this->type_ = Type::make_error_type();
9666 else if (this->end_ == NULL)
9667 this->type_ = type->element_type();
411eb89e 9668 else if (type->is_slice_type())
e440a328 9669 {
9670 // A slice of a slice has the same type as the original
9671 // slice.
9672 this->type_ = this->array_->type()->deref();
9673 }
9674 else
9675 {
9676 // A slice of an array is a slice.
9677 this->type_ = Type::make_array_type(type->element_type(), NULL);
9678 }
9679 }
9680 return this->type_;
9681}
9682
9683// Set the type of an array index.
9684
9685void
9686Array_index_expression::do_determine_type(const Type_context*)
9687{
9688 this->array_->determine_type_no_context();
7917ad68 9689 this->start_->determine_type_no_context();
e440a328 9690 if (this->end_ != NULL)
7917ad68 9691 this->end_->determine_type_no_context();
e440a328 9692}
9693
9694// Check types of an array index.
9695
9696void
9697Array_index_expression::do_check_types(Gogo*)
9698{
9699 if (this->start_->type()->integer_type() == NULL)
9700 this->report_error(_("index must be integer"));
9701 if (this->end_ != NULL
9702 && this->end_->type()->integer_type() == NULL
99b3f06f 9703 && !this->end_->type()->is_error()
9704 && !this->end_->is_nil_expression()
9705 && !this->end_->is_error_expression())
e440a328 9706 this->report_error(_("slice end must be integer"));
9707
9708 Array_type* array_type = this->array_->type()->array_type();
f9c68f17 9709 if (array_type == NULL)
9710 {
c484d925 9711 go_assert(this->array_->type()->is_error());
f9c68f17 9712 return;
9713 }
e440a328 9714
9715 unsigned int int_bits =
9716 Type::lookup_integer_type("int")->integer_type()->bits();
9717
0c77715b 9718 Numeric_constant lvalnc;
e440a328 9719 mpz_t lval;
e440a328 9720 bool lval_valid = (array_type->length() != NULL
0c77715b 9721 && array_type->length()->numeric_constant_value(&lvalnc)
9722 && lvalnc.to_int(&lval));
9723 Numeric_constant inc;
e440a328 9724 mpz_t ival;
0c77715b 9725 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 9726 {
9727 if (mpz_sgn(ival) < 0
9728 || mpz_sizeinbase(ival, 2) >= int_bits
9729 || (lval_valid
9730 && (this->end_ == NULL
9731 ? mpz_cmp(ival, lval) >= 0
9732 : mpz_cmp(ival, lval) > 0)))
9733 {
9734 error_at(this->start_->location(), "array index out of bounds");
9735 this->set_is_error();
9736 }
0c77715b 9737 mpz_clear(ival);
e440a328 9738 }
9739 if (this->end_ != NULL && !this->end_->is_nil_expression())
9740 {
0c77715b 9741 Numeric_constant enc;
9742 mpz_t eval;
9743 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 9744 {
0c77715b 9745 if (mpz_sgn(eval) < 0
9746 || mpz_sizeinbase(eval, 2) >= int_bits
9747 || (lval_valid && mpz_cmp(eval, lval) > 0))
e440a328 9748 {
9749 error_at(this->end_->location(), "array index out of bounds");
9750 this->set_is_error();
9751 }
0c77715b 9752 mpz_clear(eval);
e440a328 9753 }
9754 }
0c77715b 9755 if (lval_valid)
9756 mpz_clear(lval);
e440a328 9757
9758 // A slice of an array requires an addressable array. A slice of a
9759 // slice is always possible.
411eb89e 9760 if (this->end_ != NULL && !array_type->is_slice_type())
88ec30c8 9761 {
9762 if (!this->array_->is_addressable())
8da39c3b 9763 this->report_error(_("slice of unaddressable value"));
88ec30c8 9764 else
9765 this->array_->address_taken(true);
9766 }
e440a328 9767}
9768
9769// Return whether this expression is addressable.
9770
9771bool
9772Array_index_expression::do_is_addressable() const
9773{
9774 // A slice expression is not addressable.
9775 if (this->end_ != NULL)
9776 return false;
9777
9778 // An index into a slice is addressable.
411eb89e 9779 if (this->array_->type()->is_slice_type())
e440a328 9780 return true;
9781
9782 // An index into an array is addressable if the array is
9783 // addressable.
9784 return this->array_->is_addressable();
9785}
9786
9787// Get a tree for an array index.
9788
9789tree
9790Array_index_expression::do_get_tree(Translate_context* context)
9791{
9792 Gogo* gogo = context->gogo();
b13c66cd 9793 Location loc = this->location();
e440a328 9794
9795 Array_type* array_type = this->array_->type()->array_type();
d8cd8e2d 9796 if (array_type == NULL)
9797 {
c484d925 9798 go_assert(this->array_->type()->is_error());
d8cd8e2d 9799 return error_mark_node;
9800 }
e440a328 9801
9f0e0513 9802 tree type_tree = type_to_tree(array_type->get_backend(gogo));
c65212a0 9803 if (type_tree == error_mark_node)
9804 return error_mark_node;
e440a328 9805
9806 tree array_tree = this->array_->get_tree(context);
9807 if (array_tree == error_mark_node)
9808 return error_mark_node;
9809
9810 if (array_type->length() == NULL && !DECL_P(array_tree))
9811 array_tree = save_expr(array_tree);
a04bfdfc 9812
9813 tree length_tree = NULL_TREE;
9814 if (this->end_ == NULL || this->end_->is_nil_expression())
9815 {
9816 length_tree = array_type->length_tree(gogo, array_tree);
9817 if (length_tree == error_mark_node)
9818 return error_mark_node;
9819 length_tree = save_expr(length_tree);
9820 }
9821
9822 tree capacity_tree = NULL_TREE;
9823 if (this->end_ != NULL)
9824 {
9825 capacity_tree = array_type->capacity_tree(gogo, array_tree);
9826 if (capacity_tree == error_mark_node)
9827 return error_mark_node;
9828 capacity_tree = save_expr(capacity_tree);
9829 }
9830
9831 tree length_type = (length_tree != NULL_TREE
9832 ? TREE_TYPE(length_tree)
9833 : TREE_TYPE(capacity_tree));
e440a328 9834
9835 tree bad_index = boolean_false_node;
9836
9837 tree start_tree = this->start_->get_tree(context);
9838 if (start_tree == error_mark_node)
9839 return error_mark_node;
9840 if (!DECL_P(start_tree))
9841 start_tree = save_expr(start_tree);
9842 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9843 start_tree = convert_to_integer(length_type, start_tree);
9844
9845 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9846 loc);
9847
b13c66cd 9848 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
9849 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
9850 boolean_type_node, bad_index,
9851 fold_build2_loc(loc.gcc_location(),
e440a328 9852 (this->end_ == NULL
9853 ? GE_EXPR
9854 : GT_EXPR),
9855 boolean_type_node, start_tree,
a04bfdfc 9856 (this->end_ == NULL
9857 ? length_tree
9858 : capacity_tree)));
e440a328 9859
9860 int code = (array_type->length() != NULL
9861 ? (this->end_ == NULL
9862 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9863 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9864 : (this->end_ == NULL
9865 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9866 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9867 tree crash = Gogo::runtime_error(code, loc);
9868
9869 if (this->end_ == NULL)
9870 {
9871 // Simple array indexing. This has to return an l-value, so
9872 // wrap the index check into START_TREE.
9873 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9874 build3(COND_EXPR, void_type_node,
9875 bad_index, crash, NULL_TREE),
9876 start_tree);
b13c66cd 9877 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
e440a328 9878
9879 if (array_type->length() != NULL)
9880 {
9881 // Fixed array.
9882 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9883 start_tree, NULL_TREE, NULL_TREE);
9884 }
9885 else
9886 {
9887 // Open array.
9888 tree values = array_type->value_pointer_tree(gogo, array_tree);
9f0e0513 9889 Type* element_type = array_type->element_type();
9890 Btype* belement_type = element_type->get_backend(gogo);
9891 tree element_type_tree = type_to_tree(belement_type);
c65212a0 9892 if (element_type_tree == error_mark_node)
9893 return error_mark_node;
e440a328 9894 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
b13c66cd 9895 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
e440a328 9896 start_tree, element_size);
b13c66cd 9897 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
e440a328 9898 TREE_TYPE(values), values, offset);
9899 return build_fold_indirect_ref(ptr);
9900 }
9901 }
9902
9903 // Array slice.
9904
e440a328 9905 tree end_tree;
9906 if (this->end_->is_nil_expression())
9907 end_tree = length_tree;
9908 else
9909 {
9910 end_tree = this->end_->get_tree(context);
9911 if (end_tree == error_mark_node)
9912 return error_mark_node;
9913 if (!DECL_P(end_tree))
9914 end_tree = save_expr(end_tree);
9915 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9916 end_tree = convert_to_integer(length_type, end_tree);
9917
9918 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9919 loc);
9920
b13c66cd 9921 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
e440a328 9922
b13c66cd 9923 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
9924 boolean_type_node,
9925 fold_build2_loc(loc.gcc_location(),
9926 LT_EXPR, boolean_type_node,
e440a328 9927 end_tree, start_tree),
b13c66cd 9928 fold_build2_loc(loc.gcc_location(),
9929 GT_EXPR, boolean_type_node,
e440a328 9930 end_tree, capacity_tree));
b13c66cd 9931 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
9932 boolean_type_node, bad_index, bad_end);
e440a328 9933 }
9934
9f0e0513 9935 Type* element_type = array_type->element_type();
9936 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
c65212a0 9937 if (element_type_tree == error_mark_node)
9938 return error_mark_node;
e440a328 9939 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9940
b13c66cd 9941 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
9942 fold_convert_loc(loc.gcc_location(), sizetype,
9943 start_tree),
e440a328 9944 element_size);
9945
9946 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
c65212a0 9947 if (value_pointer == error_mark_node)
9948 return error_mark_node;
e440a328 9949
b13c66cd 9950 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
e440a328 9951 TREE_TYPE(value_pointer),
9952 value_pointer, offset);
9953
b13c66cd 9954 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
9955 length_type, end_tree, start_tree);
e440a328 9956
b13c66cd 9957 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
9958 length_type, capacity_tree,
9959 start_tree);
e440a328 9960
9f0e0513 9961 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
c484d925 9962 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
e440a328 9963
9964 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9965
9966 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9967 tree field = TYPE_FIELDS(struct_tree);
c484d925 9968 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
e440a328 9969 elt->index = field;
9970 elt->value = value_pointer;
9971
9972 elt = VEC_quick_push(constructor_elt, init, NULL);
9973 field = DECL_CHAIN(field);
c484d925 9974 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
e440a328 9975 elt->index = field;
b13c66cd 9976 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
9977 result_length_tree);
e440a328 9978
9979 elt = VEC_quick_push(constructor_elt, init, NULL);
9980 field = DECL_CHAIN(field);
c484d925 9981 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
e440a328 9982 elt->index = field;
b13c66cd 9983 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
9984 result_capacity_tree);
e440a328 9985
9986 tree constructor = build_constructor(struct_tree, init);
9987
9988 if (TREE_CONSTANT(value_pointer)
9989 && TREE_CONSTANT(result_length_tree)
9990 && TREE_CONSTANT(result_capacity_tree))
9991 TREE_CONSTANT(constructor) = 1;
9992
b13c66cd 9993 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
9994 TREE_TYPE(constructor),
e440a328 9995 build3(COND_EXPR, void_type_node,
9996 bad_index, crash, NULL_TREE),
9997 constructor);
9998}
9999
d751bb78 10000// Dump ast representation for an array index expression.
10001
10002void
10003Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10004 const
10005{
10006 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10007 this->start_, this->end_);
10008}
10009
e440a328 10010// Make an array index expression. END may be NULL.
10011
10012Expression*
10013Expression::make_array_index(Expression* array, Expression* start,
b13c66cd 10014 Expression* end, Location location)
e440a328 10015{
e440a328 10016 return new Array_index_expression(array, start, end, location);
10017}
10018
10019// A string index. This is used for both indexing and slicing.
10020
10021class String_index_expression : public Expression
10022{
10023 public:
10024 String_index_expression(Expression* string, Expression* start,
b13c66cd 10025 Expression* end, Location location)
e440a328 10026 : Expression(EXPRESSION_STRING_INDEX, location),
10027 string_(string), start_(start), end_(end)
10028 { }
10029
10030 protected:
10031 int
10032 do_traverse(Traverse*);
10033
10034 Type*
10035 do_type();
10036
10037 void
10038 do_determine_type(const Type_context*);
10039
10040 void
10041 do_check_types(Gogo*);
10042
10043 Expression*
10044 do_copy()
10045 {
10046 return Expression::make_string_index(this->string_->copy(),
10047 this->start_->copy(),
10048 (this->end_ == NULL
10049 ? NULL
10050 : this->end_->copy()),
10051 this->location());
10052 }
10053
baef9f7a 10054 bool
10055 do_must_eval_subexpressions_in_order(int* skip) const
10056 {
10057 *skip = 1;
10058 return true;
10059 }
10060
e440a328 10061 tree
10062 do_get_tree(Translate_context*);
10063
d751bb78 10064 void
10065 do_dump_expression(Ast_dump_context*) const;
10066
e440a328 10067 private:
10068 // The string we are getting a value from.
10069 Expression* string_;
10070 // The start or only index.
10071 Expression* start_;
10072 // The end index of a slice. This may be NULL for a single index,
10073 // or it may be a nil expression for the length of the string.
10074 Expression* end_;
10075};
10076
10077// String index traversal.
10078
10079int
10080String_index_expression::do_traverse(Traverse* traverse)
10081{
10082 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10083 return TRAVERSE_EXIT;
10084 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10085 return TRAVERSE_EXIT;
10086 if (this->end_ != NULL)
10087 {
10088 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10089 return TRAVERSE_EXIT;
10090 }
10091 return TRAVERSE_CONTINUE;
10092}
10093
10094// Return the type of a string index.
10095
10096Type*
10097String_index_expression::do_type()
10098{
10099 if (this->end_ == NULL)
10100 return Type::lookup_integer_type("uint8");
10101 else
7672d35f 10102 return this->string_->type();
e440a328 10103}
10104
10105// Determine the type of a string index.
10106
10107void
10108String_index_expression::do_determine_type(const Type_context*)
10109{
10110 this->string_->determine_type_no_context();
93000773 10111 this->start_->determine_type_no_context();
e440a328 10112 if (this->end_ != NULL)
93000773 10113 this->end_->determine_type_no_context();
e440a328 10114}
10115
10116// Check types of a string index.
10117
10118void
10119String_index_expression::do_check_types(Gogo*)
10120{
10121 if (this->start_->type()->integer_type() == NULL)
10122 this->report_error(_("index must be integer"));
10123 if (this->end_ != NULL
10124 && this->end_->type()->integer_type() == NULL
10125 && !this->end_->is_nil_expression())
10126 this->report_error(_("slice end must be integer"));
10127
10128 std::string sval;
10129 bool sval_valid = this->string_->string_constant_value(&sval);
10130
0c77715b 10131 Numeric_constant inc;
e440a328 10132 mpz_t ival;
0c77715b 10133 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 10134 {
10135 if (mpz_sgn(ival) < 0
10136 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10137 {
10138 error_at(this->start_->location(), "string index out of bounds");
10139 this->set_is_error();
10140 }
0c77715b 10141 mpz_clear(ival);
e440a328 10142 }
10143 if (this->end_ != NULL && !this->end_->is_nil_expression())
10144 {
0c77715b 10145 Numeric_constant enc;
10146 mpz_t eval;
10147 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 10148 {
0c77715b 10149 if (mpz_sgn(eval) < 0
10150 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
e440a328 10151 {
10152 error_at(this->end_->location(), "string index out of bounds");
10153 this->set_is_error();
10154 }
0c77715b 10155 mpz_clear(eval);
e440a328 10156 }
10157 }
e440a328 10158}
10159
10160// Get a tree for a string index.
10161
10162tree
10163String_index_expression::do_get_tree(Translate_context* context)
10164{
b13c66cd 10165 Location loc = this->location();
e440a328 10166
10167 tree string_tree = this->string_->get_tree(context);
10168 if (string_tree == error_mark_node)
10169 return error_mark_node;
10170
10171 if (this->string_->type()->points_to() != NULL)
10172 string_tree = build_fold_indirect_ref(string_tree);
10173 if (!DECL_P(string_tree))
10174 string_tree = save_expr(string_tree);
10175 tree string_type = TREE_TYPE(string_tree);
10176
10177 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
10178 length_tree = save_expr(length_tree);
10179 tree length_type = TREE_TYPE(length_tree);
10180
10181 tree bad_index = boolean_false_node;
10182
10183 tree start_tree = this->start_->get_tree(context);
10184 if (start_tree == error_mark_node)
10185 return error_mark_node;
10186 if (!DECL_P(start_tree))
10187 start_tree = save_expr(start_tree);
10188 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10189 start_tree = convert_to_integer(length_type, start_tree);
10190
10191 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10192 loc);
10193
b13c66cd 10194 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
e440a328 10195
10196 int code = (this->end_ == NULL
10197 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10198 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
10199 tree crash = Gogo::runtime_error(code, loc);
10200
10201 if (this->end_ == NULL)
10202 {
b13c66cd 10203 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10204 boolean_type_node, bad_index,
10205 fold_build2_loc(loc.gcc_location(), GE_EXPR,
e440a328 10206 boolean_type_node,
10207 start_tree, length_tree));
10208
10209 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
b13c66cd 10210 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10211 TREE_TYPE(bytes_tree),
e440a328 10212 bytes_tree,
b13c66cd 10213 fold_convert_loc(loc.gcc_location(), sizetype,
10214 start_tree));
10215 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
e440a328 10216
10217 return build2(COMPOUND_EXPR, TREE_TYPE(index),
10218 build3(COND_EXPR, void_type_node,
10219 bad_index, crash, NULL_TREE),
10220 index);
10221 }
10222 else
10223 {
10224 tree end_tree;
10225 if (this->end_->is_nil_expression())
10226 end_tree = build_int_cst(length_type, -1);
10227 else
10228 {
10229 end_tree = this->end_->get_tree(context);
10230 if (end_tree == error_mark_node)
10231 return error_mark_node;
10232 if (!DECL_P(end_tree))
10233 end_tree = save_expr(end_tree);
10234 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10235 end_tree = convert_to_integer(length_type, end_tree);
10236
10237 bad_index = Expression::check_bounds(end_tree, length_type,
10238 bad_index, loc);
10239
b13c66cd 10240 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
10241 end_tree);
e440a328 10242 }
10243
10244 static tree strslice_fndecl;
10245 tree ret = Gogo::call_builtin(&strslice_fndecl,
10246 loc,
10247 "__go_string_slice",
10248 3,
10249 string_type,
10250 string_type,
10251 string_tree,
10252 length_type,
10253 start_tree,
10254 length_type,
10255 end_tree);
5fb82b5e 10256 if (ret == error_mark_node)
10257 return error_mark_node;
e440a328 10258 // This will panic if the bounds are out of range for the
10259 // string.
10260 TREE_NOTHROW(strslice_fndecl) = 0;
10261
10262 if (bad_index == boolean_false_node)
10263 return ret;
10264 else
10265 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
10266 build3(COND_EXPR, void_type_node,
10267 bad_index, crash, NULL_TREE),
10268 ret);
10269 }
10270}
10271
d751bb78 10272// Dump ast representation for a string index expression.
10273
10274void
10275String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10276 const
10277{
10278 Index_expression::dump_index_expression(ast_dump_context, this->string_,
10279 this->start_, this->end_);
10280}
10281
e440a328 10282// Make a string index expression. END may be NULL.
10283
10284Expression*
10285Expression::make_string_index(Expression* string, Expression* start,
b13c66cd 10286 Expression* end, Location location)
e440a328 10287{
10288 return new String_index_expression(string, start, end, location);
10289}
10290
10291// Class Map_index.
10292
10293// Get the type of the map.
10294
10295Map_type*
10296Map_index_expression::get_map_type() const
10297{
10298 Map_type* mt = this->map_->type()->deref()->map_type();
c7524fae 10299 if (mt == NULL)
c484d925 10300 go_assert(saw_errors());
e440a328 10301 return mt;
10302}
10303
10304// Map index traversal.
10305
10306int
10307Map_index_expression::do_traverse(Traverse* traverse)
10308{
10309 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
10310 return TRAVERSE_EXIT;
10311 return Expression::traverse(&this->index_, traverse);
10312}
10313
10314// Return the type of a map index.
10315
10316Type*
10317Map_index_expression::do_type()
10318{
c7524fae 10319 Map_type* mt = this->get_map_type();
10320 if (mt == NULL)
10321 return Type::make_error_type();
10322 Type* type = mt->val_type();
e440a328 10323 // If this map index is in a tuple assignment, we actually return a
10324 // pointer to the value type. Tuple_map_assignment_statement is
10325 // responsible for handling this correctly. We need to get the type
10326 // right in case this gets assigned to a temporary variable.
10327 if (this->is_in_tuple_assignment_)
10328 type = Type::make_pointer_type(type);
10329 return type;
10330}
10331
10332// Fix the type of a map index.
10333
10334void
10335Map_index_expression::do_determine_type(const Type_context*)
10336{
10337 this->map_->determine_type_no_context();
c7524fae 10338 Map_type* mt = this->get_map_type();
10339 Type* key_type = mt == NULL ? NULL : mt->key_type();
10340 Type_context subcontext(key_type, false);
e440a328 10341 this->index_->determine_type(&subcontext);
10342}
10343
10344// Check types of a map index.
10345
10346void
10347Map_index_expression::do_check_types(Gogo*)
10348{
10349 std::string reason;
c7524fae 10350 Map_type* mt = this->get_map_type();
10351 if (mt == NULL)
10352 return;
10353 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
e440a328 10354 {
10355 if (reason.empty())
10356 this->report_error(_("incompatible type for map index"));
10357 else
10358 {
10359 error_at(this->location(), "incompatible type for map index (%s)",
10360 reason.c_str());
10361 this->set_is_error();
10362 }
10363 }
10364}
10365
10366// Get a tree for a map index.
10367
10368tree
10369Map_index_expression::do_get_tree(Translate_context* context)
10370{
10371 Map_type* type = this->get_map_type();
c7524fae 10372 if (type == NULL)
10373 return error_mark_node;
e440a328 10374
10375 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
10376 if (valptr == error_mark_node)
10377 return error_mark_node;
10378 valptr = save_expr(valptr);
10379
10380 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10381
10382 if (this->is_lvalue_)
10383 return build_fold_indirect_ref(valptr);
10384 else if (this->is_in_tuple_assignment_)
10385 {
10386 // Tuple_map_assignment_statement is responsible for using this
10387 // appropriately.
10388 return valptr;
10389 }
10390 else
10391 {
63697958 10392 Gogo* gogo = context->gogo();
10393 Btype* val_btype = type->val_type()->get_backend(gogo);
10394 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
e440a328 10395 return fold_build3(COND_EXPR, val_type_tree,
10396 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10397 fold_convert(TREE_TYPE(valptr),
10398 null_pointer_node)),
63697958 10399 expr_to_tree(val_zero),
e440a328 10400 build_fold_indirect_ref(valptr));
10401 }
10402}
10403
10404// Get a tree for the map index. This returns a tree which evaluates
10405// to a pointer to a value. The pointer will be NULL if the key is
10406// not in the map.
10407
10408tree
10409Map_index_expression::get_value_pointer(Translate_context* context,
10410 bool insert)
10411{
10412 Map_type* type = this->get_map_type();
c7524fae 10413 if (type == NULL)
10414 return error_mark_node;
e440a328 10415
10416 tree map_tree = this->map_->get_tree(context);
10417 tree index_tree = this->index_->get_tree(context);
10418 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10419 this->index_->type(),
10420 index_tree,
10421 this->location());
10422 if (map_tree == error_mark_node || index_tree == error_mark_node)
10423 return error_mark_node;
10424
10425 if (this->map_->type()->points_to() != NULL)
10426 map_tree = build_fold_indirect_ref(map_tree);
10427
10428 // We need to pass in a pointer to the key, so stuff it into a
10429 // variable.
746d2e73 10430 tree tmp;
10431 tree make_tmp;
10432 if (current_function_decl != NULL)
10433 {
10434 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10435 DECL_IGNORED_P(tmp) = 0;
10436 DECL_INITIAL(tmp) = index_tree;
10437 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10438 TREE_ADDRESSABLE(tmp) = 1;
10439 }
10440 else
10441 {
b13c66cd 10442 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
10443 create_tmp_var_name("M"),
746d2e73 10444 TREE_TYPE(index_tree));
10445 DECL_EXTERNAL(tmp) = 0;
10446 TREE_PUBLIC(tmp) = 0;
10447 TREE_STATIC(tmp) = 1;
10448 DECL_ARTIFICIAL(tmp) = 1;
10449 if (!TREE_CONSTANT(index_tree))
b13c66cd 10450 make_tmp = fold_build2_loc(this->location().gcc_location(),
10451 INIT_EXPR, void_type_node,
746d2e73 10452 tmp, index_tree);
10453 else
10454 {
10455 TREE_READONLY(tmp) = 1;
10456 TREE_CONSTANT(tmp) = 1;
10457 DECL_INITIAL(tmp) = index_tree;
10458 make_tmp = NULL_TREE;
10459 }
10460 rest_of_decl_compilation(tmp, 1, 0);
10461 }
b13c66cd 10462 tree tmpref =
10463 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
10464 build_fold_addr_expr_loc(this->location().gcc_location(),
10465 tmp));
e440a328 10466
10467 static tree map_index_fndecl;
10468 tree call = Gogo::call_builtin(&map_index_fndecl,
10469 this->location(),
10470 "__go_map_index",
10471 3,
10472 const_ptr_type_node,
10473 TREE_TYPE(map_tree),
10474 map_tree,
10475 const_ptr_type_node,
10476 tmpref,
10477 boolean_type_node,
10478 (insert
10479 ? boolean_true_node
10480 : boolean_false_node));
5fb82b5e 10481 if (call == error_mark_node)
10482 return error_mark_node;
e440a328 10483 // This can panic on a map of interface type if the interface holds
10484 // an uncomparable or unhashable type.
10485 TREE_NOTHROW(map_index_fndecl) = 0;
10486
9f0e0513 10487 Type* val_type = type->val_type();
10488 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
e440a328 10489 if (val_type_tree == error_mark_node)
10490 return error_mark_node;
10491 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10492
b13c66cd 10493 tree ret = fold_convert_loc(this->location().gcc_location(),
10494 ptr_val_type_tree, call);
746d2e73 10495 if (make_tmp != NULL_TREE)
10496 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10497 return ret;
e440a328 10498}
10499
d751bb78 10500// Dump ast representation for a map index expression
10501
10502void
10503Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10504 const
10505{
10506 Index_expression::dump_index_expression(ast_dump_context,
10507 this->map_, this->index_, NULL);
10508}
10509
e440a328 10510// Make a map index expression.
10511
10512Map_index_expression*
10513Expression::make_map_index(Expression* map, Expression* index,
b13c66cd 10514 Location location)
e440a328 10515{
10516 return new Map_index_expression(map, index, location);
10517}
10518
10519// Class Field_reference_expression.
10520
10521// Return the type of a field reference.
10522
10523Type*
10524Field_reference_expression::do_type()
10525{
b0e628fb 10526 Type* type = this->expr_->type();
5c13bd80 10527 if (type->is_error())
b0e628fb 10528 return type;
10529 Struct_type* struct_type = type->struct_type();
c484d925 10530 go_assert(struct_type != NULL);
e440a328 10531 return struct_type->field(this->field_index_)->type();
10532}
10533
10534// Check the types for a field reference.
10535
10536void
10537Field_reference_expression::do_check_types(Gogo*)
10538{
b0e628fb 10539 Type* type = this->expr_->type();
5c13bd80 10540 if (type->is_error())
b0e628fb 10541 return;
10542 Struct_type* struct_type = type->struct_type();
c484d925 10543 go_assert(struct_type != NULL);
10544 go_assert(struct_type->field(this->field_index_) != NULL);
e440a328 10545}
10546
10547// Get a tree for a field reference.
10548
10549tree
10550Field_reference_expression::do_get_tree(Translate_context* context)
10551{
10552 tree struct_tree = this->expr_->get_tree(context);
10553 if (struct_tree == error_mark_node
10554 || TREE_TYPE(struct_tree) == error_mark_node)
10555 return error_mark_node;
c484d925 10556 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
e440a328 10557 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
b1d655d5 10558 if (field == NULL_TREE)
10559 {
10560 // This can happen for a type which refers to itself indirectly
10561 // and then turns out to be erroneous.
c484d925 10562 go_assert(saw_errors());
b1d655d5 10563 return error_mark_node;
10564 }
e440a328 10565 for (unsigned int i = this->field_index_; i > 0; --i)
10566 {
10567 field = DECL_CHAIN(field);
c484d925 10568 go_assert(field != NULL_TREE);
e440a328 10569 }
c35179ff 10570 if (TREE_TYPE(field) == error_mark_node)
10571 return error_mark_node;
e440a328 10572 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10573 NULL_TREE);
10574}
10575
d751bb78 10576// Dump ast representation for a field reference expression.
10577
10578void
10579Field_reference_expression::do_dump_expression(
10580 Ast_dump_context* ast_dump_context) const
10581{
10582 this->expr_->dump_expression(ast_dump_context);
10583 ast_dump_context->ostream() << "." << this->field_index_;
10584}
10585
e440a328 10586// Make a reference to a qualified identifier in an expression.
10587
10588Field_reference_expression*
10589Expression::make_field_reference(Expression* expr, unsigned int field_index,
b13c66cd 10590 Location location)
e440a328 10591{
10592 return new Field_reference_expression(expr, field_index, location);
10593}
10594
10595// Class Interface_field_reference_expression.
10596
10597// Return a tree for the pointer to the function to call.
10598
10599tree
10600Interface_field_reference_expression::get_function_tree(Translate_context*,
10601 tree expr)
10602{
10603 if (this->expr_->type()->points_to() != NULL)
10604 expr = build_fold_indirect_ref(expr);
10605
10606 tree expr_type = TREE_TYPE(expr);
c484d925 10607 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
e440a328 10608
10609 tree field = TYPE_FIELDS(expr_type);
c484d925 10610 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
e440a328 10611
10612 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
c484d925 10613 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
e440a328 10614
10615 table = build_fold_indirect_ref(table);
c484d925 10616 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
e440a328 10617
10618 std::string name = Gogo::unpack_hidden_name(this->name_);
10619 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10620 field != NULL_TREE;
10621 field = DECL_CHAIN(field))
10622 {
10623 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10624 break;
10625 }
c484d925 10626 go_assert(field != NULL_TREE);
e440a328 10627
10628 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10629}
10630
10631// Return a tree for the first argument to pass to the interface
10632// function.
10633
10634tree
10635Interface_field_reference_expression::get_underlying_object_tree(
10636 Translate_context*,
10637 tree expr)
10638{
10639 if (this->expr_->type()->points_to() != NULL)
10640 expr = build_fold_indirect_ref(expr);
10641
10642 tree expr_type = TREE_TYPE(expr);
c484d925 10643 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
e440a328 10644
10645 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
c484d925 10646 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
e440a328 10647
10648 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10649}
10650
10651// Traversal.
10652
10653int
10654Interface_field_reference_expression::do_traverse(Traverse* traverse)
10655{
10656 return Expression::traverse(&this->expr_, traverse);
10657}
10658
10659// Return the type of an interface field reference.
10660
10661Type*
10662Interface_field_reference_expression::do_type()
10663{
10664 Type* expr_type = this->expr_->type();
10665
10666 Type* points_to = expr_type->points_to();
10667 if (points_to != NULL)
10668 expr_type = points_to;
10669
10670 Interface_type* interface_type = expr_type->interface_type();
10671 if (interface_type == NULL)
10672 return Type::make_error_type();
10673
10674 const Typed_identifier* method = interface_type->find_method(this->name_);
10675 if (method == NULL)
10676 return Type::make_error_type();
10677
10678 return method->type();
10679}
10680
10681// Determine types.
10682
10683void
10684Interface_field_reference_expression::do_determine_type(const Type_context*)
10685{
10686 this->expr_->determine_type_no_context();
10687}
10688
10689// Check the types for an interface field reference.
10690
10691void
10692Interface_field_reference_expression::do_check_types(Gogo*)
10693{
10694 Type* type = this->expr_->type();
10695
10696 Type* points_to = type->points_to();
10697 if (points_to != NULL)
10698 type = points_to;
10699
10700 Interface_type* interface_type = type->interface_type();
10701 if (interface_type == NULL)
5c491127 10702 {
10703 if (!type->is_error_type())
10704 this->report_error(_("expected interface or pointer to interface"));
10705 }
e440a328 10706 else
10707 {
10708 const Typed_identifier* method =
10709 interface_type->find_method(this->name_);
10710 if (method == NULL)
10711 {
10712 error_at(this->location(), "method %qs not in interface",
10713 Gogo::message_name(this->name_).c_str());
10714 this->set_is_error();
10715 }
10716 }
10717}
10718
10719// Get a tree for a reference to a field in an interface. There is no
10720// standard tree type representation for this: it's a function
10721// attached to its first argument, like a Bound_method_expression.
10722// The only places it may currently be used are in a Call_expression
10723// or a Go_statement, which will take it apart directly. So this has
10724// nothing to do at present.
10725
10726tree
10727Interface_field_reference_expression::do_get_tree(Translate_context*)
10728{
11bbe026 10729 error_at(this->location(), "reference to method other than calling it");
10730 return error_mark_node;
e440a328 10731}
10732
d751bb78 10733// Dump ast representation for an interface field reference.
10734
10735void
10736Interface_field_reference_expression::do_dump_expression(
10737 Ast_dump_context* ast_dump_context) const
10738{
10739 this->expr_->dump_expression(ast_dump_context);
10740 ast_dump_context->ostream() << "." << this->name_;
10741}
10742
e440a328 10743// Make a reference to a field in an interface.
10744
10745Expression*
10746Expression::make_interface_field_reference(Expression* expr,
10747 const std::string& field,
b13c66cd 10748 Location location)
e440a328 10749{
10750 return new Interface_field_reference_expression(expr, field, location);
10751}
10752
10753// A general selector. This is a Parser_expression for LEFT.NAME. It
10754// is lowered after we know the type of the left hand side.
10755
10756class Selector_expression : public Parser_expression
10757{
10758 public:
10759 Selector_expression(Expression* left, const std::string& name,
b13c66cd 10760 Location location)
e440a328 10761 : Parser_expression(EXPRESSION_SELECTOR, location),
10762 left_(left), name_(name)
10763 { }
10764
10765 protected:
10766 int
10767 do_traverse(Traverse* traverse)
10768 { return Expression::traverse(&this->left_, traverse); }
10769
10770 Expression*
ceeb4318 10771 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 10772
10773 Expression*
10774 do_copy()
10775 {
10776 return new Selector_expression(this->left_->copy(), this->name_,
10777 this->location());
10778 }
10779
d751bb78 10780 void
10781 do_dump_expression(Ast_dump_context* ast_dump_context) const;
10782
e440a328 10783 private:
10784 Expression*
10785 lower_method_expression(Gogo*);
10786
10787 // The expression on the left hand side.
10788 Expression* left_;
10789 // The name on the right hand side.
10790 std::string name_;
10791};
10792
10793// Lower a selector expression once we know the real type of the left
10794// hand side.
10795
10796Expression*
ceeb4318 10797Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
10798 int)
e440a328 10799{
10800 Expression* left = this->left_;
10801 if (left->is_type_expression())
10802 return this->lower_method_expression(gogo);
10803 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10804 this->location());
10805}
10806
10807// Lower a method expression T.M or (*T).M. We turn this into a
10808// function literal.
10809
10810Expression*
10811Selector_expression::lower_method_expression(Gogo* gogo)
10812{
b13c66cd 10813 Location location = this->location();
e440a328 10814 Type* type = this->left_->type();
10815 const std::string& name(this->name_);
10816
10817 bool is_pointer;
10818 if (type->points_to() == NULL)
10819 is_pointer = false;
10820 else
10821 {
10822 is_pointer = true;
10823 type = type->points_to();
10824 }
10825 Named_type* nt = type->named_type();
10826 if (nt == NULL)
10827 {
10828 error_at(location,
10829 ("method expression requires named type or "
10830 "pointer to named type"));
10831 return Expression::make_error(location);
10832 }
10833
10834 bool is_ambiguous;
10835 Method* method = nt->method_function(name, &is_ambiguous);
ab1468c3 10836 const Typed_identifier* imethod = NULL;
dcc8506b 10837 if (method == NULL && !is_pointer)
ab1468c3 10838 {
10839 Interface_type* it = nt->interface_type();
10840 if (it != NULL)
10841 imethod = it->find_method(name);
10842 }
10843
10844 if (method == NULL && imethod == NULL)
e440a328 10845 {
10846 if (!is_ambiguous)
dcc8506b 10847 error_at(location, "type %<%s%s%> has no method %<%s%>",
10848 is_pointer ? "*" : "",
e440a328 10849 nt->message_name().c_str(),
10850 Gogo::message_name(name).c_str());
10851 else
dcc8506b 10852 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
e440a328 10853 Gogo::message_name(name).c_str(),
dcc8506b 10854 is_pointer ? "*" : "",
e440a328 10855 nt->message_name().c_str());
10856 return Expression::make_error(location);
10857 }
10858
ab1468c3 10859 if (method != NULL && !is_pointer && !method->is_value_method())
e440a328 10860 {
10861 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10862 nt->message_name().c_str(),
10863 Gogo::message_name(name).c_str());
10864 return Expression::make_error(location);
10865 }
10866
10867 // Build a new function type in which the receiver becomes the first
10868 // argument.
ab1468c3 10869 Function_type* method_type;
10870 if (method != NULL)
10871 {
10872 method_type = method->type();
c484d925 10873 go_assert(method_type->is_method());
ab1468c3 10874 }
10875 else
10876 {
10877 method_type = imethod->type()->function_type();
c484d925 10878 go_assert(method_type != NULL && !method_type->is_method());
ab1468c3 10879 }
e440a328 10880
10881 const char* const receiver_name = "$this";
10882 Typed_identifier_list* parameters = new Typed_identifier_list();
10883 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10884 location));
10885
10886 const Typed_identifier_list* method_parameters = method_type->parameters();
10887 if (method_parameters != NULL)
10888 {
f470da59 10889 int i = 0;
e440a328 10890 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10891 p != method_parameters->end();
f470da59 10892 ++p, ++i)
10893 {
68883531 10894 if (!p->name().empty())
f470da59 10895 parameters->push_back(*p);
10896 else
10897 {
10898 char buf[20];
10899 snprintf(buf, sizeof buf, "$param%d", i);
10900 parameters->push_back(Typed_identifier(buf, p->type(),
10901 p->location()));
10902 }
10903 }
e440a328 10904 }
10905
10906 const Typed_identifier_list* method_results = method_type->results();
10907 Typed_identifier_list* results;
10908 if (method_results == NULL)
10909 results = NULL;
10910 else
10911 {
10912 results = new Typed_identifier_list();
10913 for (Typed_identifier_list::const_iterator p = method_results->begin();
10914 p != method_results->end();
10915 ++p)
10916 results->push_back(*p);
10917 }
10918
10919 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10920 location);
10921 if (method_type->is_varargs())
10922 fntype->set_is_varargs();
10923
10924 // We generate methods which always takes a pointer to the receiver
10925 // as their first argument. If this is for a pointer type, we can
10926 // simply reuse the existing function. We use an internal hack to
10927 // get the right type.
10928
ab1468c3 10929 if (method != NULL && is_pointer)
e440a328 10930 {
10931 Named_object* mno = (method->needs_stub_method()
10932 ? method->stub_object()
10933 : method->named_object());
10934 Expression* f = Expression::make_func_reference(mno, NULL, location);
10935 f = Expression::make_cast(fntype, f, location);
10936 Type_conversion_expression* tce =
10937 static_cast<Type_conversion_expression*>(f);
10938 tce->set_may_convert_function_types();
10939 return f;
10940 }
10941
10942 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10943 location);
10944
10945 Named_object* vno = gogo->lookup(receiver_name, NULL);
c484d925 10946 go_assert(vno != NULL);
e440a328 10947 Expression* ve = Expression::make_var_reference(vno, location);
ab1468c3 10948 Expression* bm;
10949 if (method != NULL)
10950 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10951 else
10952 bm = Expression::make_interface_field_reference(ve, name, location);
f690b0bb 10953
10954 // Even though we found the method above, if it has an error type we
10955 // may see an error here.
10956 if (bm->is_error_expression())
463fe805 10957 {
10958 gogo->finish_function(location);
10959 return bm;
10960 }
e440a328 10961
10962 Expression_list* args;
f470da59 10963 if (parameters->size() <= 1)
e440a328 10964 args = NULL;
10965 else
10966 {
10967 args = new Expression_list();
f470da59 10968 Typed_identifier_list::const_iterator p = parameters->begin();
10969 ++p;
10970 for (; p != parameters->end(); ++p)
e440a328 10971 {
10972 vno = gogo->lookup(p->name(), NULL);
c484d925 10973 go_assert(vno != NULL);
e440a328 10974 args->push_back(Expression::make_var_reference(vno, location));
10975 }
10976 }
10977
ceeb4318 10978 gogo->start_block(location);
10979
e440a328 10980 Call_expression* call = Expression::make_call(bm, args,
10981 method_type->is_varargs(),
10982 location);
10983
10984 size_t count = call->result_count();
10985 Statement* s;
10986 if (count == 0)
a7549a6a 10987 s = Statement::make_statement(call, true);
e440a328 10988 else
10989 {
10990 Expression_list* retvals = new Expression_list();
10991 if (count <= 1)
10992 retvals->push_back(call);
10993 else
10994 {
10995 for (size_t i = 0; i < count; ++i)
10996 retvals->push_back(Expression::make_call_result(call, i));
10997 }
be2fc38d 10998 s = Statement::make_return_statement(retvals, location);
e440a328 10999 }
11000 gogo->add_statement(s);
11001
ceeb4318 11002 Block* b = gogo->finish_block(location);
11003
11004 gogo->add_block(b, location);
11005
11006 // Lower the call in case there are multiple results.
11007 gogo->lower_block(no, b);
11008
e440a328 11009 gogo->finish_function(location);
11010
11011 return Expression::make_func_reference(no, NULL, location);
11012}
11013
d751bb78 11014// Dump the ast for a selector expression.
11015
11016void
11017Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11018 const
11019{
11020 ast_dump_context->dump_expression(this->left_);
11021 ast_dump_context->ostream() << ".";
11022 ast_dump_context->ostream() << this->name_;
11023}
11024
e440a328 11025// Make a selector expression.
11026
11027Expression*
11028Expression::make_selector(Expression* left, const std::string& name,
b13c66cd 11029 Location location)
e440a328 11030{
11031 return new Selector_expression(left, name, location);
11032}
11033
11034// Implement the builtin function new.
11035
11036class Allocation_expression : public Expression
11037{
11038 public:
b13c66cd 11039 Allocation_expression(Type* type, Location location)
e440a328 11040 : Expression(EXPRESSION_ALLOCATION, location),
11041 type_(type)
11042 { }
11043
11044 protected:
11045 int
11046 do_traverse(Traverse* traverse)
11047 { return Type::traverse(this->type_, traverse); }
11048
11049 Type*
11050 do_type()
11051 { return Type::make_pointer_type(this->type_); }
11052
11053 void
11054 do_determine_type(const Type_context*)
11055 { }
11056
e440a328 11057 Expression*
11058 do_copy()
11059 { return new Allocation_expression(this->type_, this->location()); }
11060
11061 tree
11062 do_get_tree(Translate_context*);
11063
d751bb78 11064 void
11065 do_dump_expression(Ast_dump_context*) const;
11066
e440a328 11067 private:
11068 // The type we are allocating.
11069 Type* type_;
11070};
11071
e440a328 11072// Return a tree for an allocation expression.
11073
11074tree
11075Allocation_expression::do_get_tree(Translate_context* context)
11076{
9f0e0513 11077 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
19824ddb 11078 if (type_tree == error_mark_node)
11079 return error_mark_node;
e440a328 11080 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11081 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11082 this->location());
19824ddb 11083 if (space == error_mark_node)
11084 return error_mark_node;
e440a328 11085 return fold_convert(build_pointer_type(type_tree), space);
11086}
11087
d751bb78 11088// Dump ast representation for an allocation expression.
11089
11090void
11091Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11092 const
11093{
11094 ast_dump_context->ostream() << "new(";
11095 ast_dump_context->dump_type(this->type_);
11096 ast_dump_context->ostream() << ")";
11097}
11098
e440a328 11099// Make an allocation expression.
11100
11101Expression*
b13c66cd 11102Expression::make_allocation(Type* type, Location location)
e440a328 11103{
11104 return new Allocation_expression(type, location);
11105}
11106
e440a328 11107// Construct a struct.
11108
11109class Struct_construction_expression : public Expression
11110{
11111 public:
11112 Struct_construction_expression(Type* type, Expression_list* vals,
b13c66cd 11113 Location location)
e440a328 11114 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
0c4f5a19 11115 type_(type), vals_(vals), traverse_order_(NULL)
e440a328 11116 { }
11117
0c4f5a19 11118 // Set the traversal order, used to ensure that we implement the
11119 // order of evaluation rules. Takes ownership of the argument.
11120 void
11121 set_traverse_order(std::vector<int>* traverse_order)
11122 { this->traverse_order_ = traverse_order; }
11123
e440a328 11124 // Return whether this is a constant initializer.
11125 bool
11126 is_constant_struct() const;
11127
11128 protected:
11129 int
11130 do_traverse(Traverse* traverse);
11131
11132 Type*
11133 do_type()
11134 { return this->type_; }
11135
11136 void
11137 do_determine_type(const Type_context*);
11138
11139 void
11140 do_check_types(Gogo*);
11141
11142 Expression*
11143 do_copy()
11144 {
0c4f5a19 11145 Struct_construction_expression* ret =
11146 new Struct_construction_expression(this->type_, this->vals_->copy(),
11147 this->location());
11148 if (this->traverse_order_ != NULL)
11149 ret->set_traverse_order(this->traverse_order_);
11150 return ret;
e440a328 11151 }
11152
e440a328 11153 tree
11154 do_get_tree(Translate_context*);
11155
11156 void
11157 do_export(Export*) const;
11158
d751bb78 11159 void
11160 do_dump_expression(Ast_dump_context*) const;
11161
e440a328 11162 private:
11163 // The type of the struct to construct.
11164 Type* type_;
11165 // The list of values, in order of the fields in the struct. A NULL
11166 // entry means that the field should be zero-initialized.
11167 Expression_list* vals_;
0c4f5a19 11168 // If not NULL, the order in which to traverse vals_. This is used
11169 // so that we implement the order of evaluation rules correctly.
11170 std::vector<int>* traverse_order_;
e440a328 11171};
11172
11173// Traversal.
11174
11175int
11176Struct_construction_expression::do_traverse(Traverse* traverse)
11177{
0c4f5a19 11178 if (this->vals_ != NULL)
11179 {
11180 if (this->traverse_order_ == NULL)
11181 {
11182 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11183 return TRAVERSE_EXIT;
11184 }
11185 else
11186 {
11187 for (std::vector<int>::const_iterator p =
11188 this->traverse_order_->begin();
11189 p != this->traverse_order_->end();
11190 ++p)
11191 {
11192 if (Expression::traverse(&this->vals_->at(*p), traverse)
11193 == TRAVERSE_EXIT)
11194 return TRAVERSE_EXIT;
11195 }
11196 }
11197 }
e440a328 11198 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11199 return TRAVERSE_EXIT;
11200 return TRAVERSE_CONTINUE;
11201}
11202
11203// Return whether this is a constant initializer.
11204
11205bool
11206Struct_construction_expression::is_constant_struct() const
11207{
11208 if (this->vals_ == NULL)
11209 return true;
11210 for (Expression_list::const_iterator pv = this->vals_->begin();
11211 pv != this->vals_->end();
11212 ++pv)
11213 {
11214 if (*pv != NULL
11215 && !(*pv)->is_constant()
11216 && (!(*pv)->is_composite_literal()
11217 || (*pv)->is_nonconstant_composite_literal()))
11218 return false;
11219 }
11220
11221 const Struct_field_list* fields = this->type_->struct_type()->fields();
11222 for (Struct_field_list::const_iterator pf = fields->begin();
11223 pf != fields->end();
11224 ++pf)
11225 {
11226 // There are no constant constructors for interfaces.
11227 if (pf->type()->interface_type() != NULL)
11228 return false;
11229 }
11230
11231 return true;
11232}
11233
11234// Final type determination.
11235
11236void
11237Struct_construction_expression::do_determine_type(const Type_context*)
11238{
11239 if (this->vals_ == NULL)
11240 return;
11241 const Struct_field_list* fields = this->type_->struct_type()->fields();
11242 Expression_list::const_iterator pv = this->vals_->begin();
11243 for (Struct_field_list::const_iterator pf = fields->begin();
11244 pf != fields->end();
11245 ++pf, ++pv)
11246 {
11247 if (pv == this->vals_->end())
11248 return;
11249 if (*pv != NULL)
11250 {
11251 Type_context subcontext(pf->type(), false);
11252 (*pv)->determine_type(&subcontext);
11253 }
11254 }
a6cb4c0e 11255 // Extra values are an error we will report elsewhere; we still want
11256 // to determine the type to avoid knockon errors.
11257 for (; pv != this->vals_->end(); ++pv)
11258 (*pv)->determine_type_no_context();
e440a328 11259}
11260
11261// Check types.
11262
11263void
11264Struct_construction_expression::do_check_types(Gogo*)
11265{
11266 if (this->vals_ == NULL)
11267 return;
11268
11269 Struct_type* st = this->type_->struct_type();
11270 if (this->vals_->size() > st->field_count())
11271 {
11272 this->report_error(_("too many expressions for struct"));
11273 return;
11274 }
11275
11276 const Struct_field_list* fields = st->fields();
11277 Expression_list::const_iterator pv = this->vals_->begin();
11278 int i = 0;
11279 for (Struct_field_list::const_iterator pf = fields->begin();
11280 pf != fields->end();
11281 ++pf, ++pv, ++i)
11282 {
11283 if (pv == this->vals_->end())
11284 {
11285 this->report_error(_("too few expressions for struct"));
11286 break;
11287 }
11288
11289 if (*pv == NULL)
11290 continue;
11291
11292 std::string reason;
11293 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
11294 {
11295 if (reason.empty())
11296 error_at((*pv)->location(),
11297 "incompatible type for field %d in struct construction",
11298 i + 1);
11299 else
11300 error_at((*pv)->location(),
11301 ("incompatible type for field %d in "
11302 "struct construction (%s)"),
11303 i + 1, reason.c_str());
11304 this->set_is_error();
11305 }
11306 }
c484d925 11307 go_assert(pv == this->vals_->end());
e440a328 11308}
11309
11310// Return a tree for constructing a struct.
11311
11312tree
11313Struct_construction_expression::do_get_tree(Translate_context* context)
11314{
11315 Gogo* gogo = context->gogo();
11316
11317 if (this->vals_ == NULL)
63697958 11318 {
11319 Btype* btype = this->type_->get_backend(gogo);
11320 return expr_to_tree(gogo->backend()->zero_expression(btype));
11321 }
e440a328 11322
9f0e0513 11323 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
e440a328 11324 if (type_tree == error_mark_node)
11325 return error_mark_node;
c484d925 11326 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
e440a328 11327
11328 bool is_constant = true;
11329 const Struct_field_list* fields = this->type_->struct_type()->fields();
11330 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
11331 fields->size());
11332 Struct_field_list::const_iterator pf = fields->begin();
11333 Expression_list::const_iterator pv = this->vals_->begin();
11334 for (tree field = TYPE_FIELDS(type_tree);
11335 field != NULL_TREE;
11336 field = DECL_CHAIN(field), ++pf)
11337 {
c484d925 11338 go_assert(pf != fields->end());
e440a328 11339
63697958 11340 Btype* fbtype = pf->type()->get_backend(gogo);
11341
e440a328 11342 tree val;
11343 if (pv == this->vals_->end())
63697958 11344 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
e440a328 11345 else if (*pv == NULL)
11346 {
63697958 11347 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
e440a328 11348 ++pv;
11349 }
11350 else
11351 {
11352 val = Expression::convert_for_assignment(context, pf->type(),
11353 (*pv)->type(),
11354 (*pv)->get_tree(context),
11355 this->location());
11356 ++pv;
11357 }
11358
11359 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
11360 return error_mark_node;
11361
11362 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
11363 elt->index = field;
11364 elt->value = val;
11365 if (!TREE_CONSTANT(val))
11366 is_constant = false;
11367 }
c484d925 11368 go_assert(pf == fields->end());
e440a328 11369
11370 tree ret = build_constructor(type_tree, elts);
11371 if (is_constant)
11372 TREE_CONSTANT(ret) = 1;
11373 return ret;
11374}
11375
11376// Export a struct construction.
11377
11378void
11379Struct_construction_expression::do_export(Export* exp) const
11380{
11381 exp->write_c_string("convert(");
11382 exp->write_type(this->type_);
11383 for (Expression_list::const_iterator pv = this->vals_->begin();
11384 pv != this->vals_->end();
11385 ++pv)
11386 {
11387 exp->write_c_string(", ");
11388 if (*pv != NULL)
11389 (*pv)->export_expression(exp);
11390 }
11391 exp->write_c_string(")");
11392}
11393
d751bb78 11394// Dump ast representation of a struct construction expression.
11395
11396void
11397Struct_construction_expression::do_dump_expression(
11398 Ast_dump_context* ast_dump_context) const
11399{
d751bb78 11400 ast_dump_context->dump_type(this->type_);
11401 ast_dump_context->ostream() << "{";
11402 ast_dump_context->dump_expression_list(this->vals_);
11403 ast_dump_context->ostream() << "}";
11404}
11405
e440a328 11406// Make a struct composite literal. This used by the thunk code.
11407
11408Expression*
11409Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
b13c66cd 11410 Location location)
e440a328 11411{
c484d925 11412 go_assert(type->struct_type() != NULL);
e440a328 11413 return new Struct_construction_expression(type, vals, location);
11414}
11415
11416// Construct an array. This class is not used directly; instead we
11417// use the child classes, Fixed_array_construction_expression and
11418// Open_array_construction_expression.
11419
11420class Array_construction_expression : public Expression
11421{
11422 protected:
11423 Array_construction_expression(Expression_classification classification,
ffe743ca 11424 Type* type,
11425 const std::vector<unsigned long>* indexes,
11426 Expression_list* vals, Location location)
e440a328 11427 : Expression(classification, location),
ffe743ca 11428 type_(type), indexes_(indexes), vals_(vals)
11429 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
e440a328 11430
11431 public:
11432 // Return whether this is a constant initializer.
11433 bool
11434 is_constant_array() const;
11435
11436 // Return the number of elements.
11437 size_t
11438 element_count() const
11439 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11440
11441protected:
11442 int
11443 do_traverse(Traverse* traverse);
11444
11445 Type*
11446 do_type()
11447 { return this->type_; }
11448
11449 void
11450 do_determine_type(const Type_context*);
11451
11452 void
11453 do_check_types(Gogo*);
11454
e440a328 11455 void
11456 do_export(Export*) const;
11457
ffe743ca 11458 // The indexes.
11459 const std::vector<unsigned long>*
11460 indexes()
11461 { return this->indexes_; }
11462
e440a328 11463 // The list of values.
11464 Expression_list*
11465 vals()
11466 { return this->vals_; }
11467
11468 // Get a constructor tree for the array values.
11469 tree
11470 get_constructor_tree(Translate_context* context, tree type_tree);
11471
d751bb78 11472 void
11473 do_dump_expression(Ast_dump_context*) const;
11474
e440a328 11475 private:
11476 // The type of the array to construct.
11477 Type* type_;
ffe743ca 11478 // The list of indexes into the array, one for each value. This may
11479 // be NULL, in which case the indexes start at zero and increment.
11480 const std::vector<unsigned long>* indexes_;
11481 // The list of values. This may be NULL if there are no values.
e440a328 11482 Expression_list* vals_;
11483};
11484
11485// Traversal.
11486
11487int
11488Array_construction_expression::do_traverse(Traverse* traverse)
11489{
11490 if (this->vals_ != NULL
11491 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11492 return TRAVERSE_EXIT;
11493 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11494 return TRAVERSE_EXIT;
11495 return TRAVERSE_CONTINUE;
11496}
11497
11498// Return whether this is a constant initializer.
11499
11500bool
11501Array_construction_expression::is_constant_array() const
11502{
11503 if (this->vals_ == NULL)
11504 return true;
11505
11506 // There are no constant constructors for interfaces.
11507 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11508 return false;
11509
11510 for (Expression_list::const_iterator pv = this->vals_->begin();
11511 pv != this->vals_->end();
11512 ++pv)
11513 {
11514 if (*pv != NULL
11515 && !(*pv)->is_constant()
11516 && (!(*pv)->is_composite_literal()
11517 || (*pv)->is_nonconstant_composite_literal()))
11518 return false;
11519 }
11520 return true;
11521}
11522
11523// Final type determination.
11524
11525void
11526Array_construction_expression::do_determine_type(const Type_context*)
11527{
11528 if (this->vals_ == NULL)
11529 return;
11530 Type_context subcontext(this->type_->array_type()->element_type(), false);
11531 for (Expression_list::const_iterator pv = this->vals_->begin();
11532 pv != this->vals_->end();
11533 ++pv)
11534 {
11535 if (*pv != NULL)
11536 (*pv)->determine_type(&subcontext);
11537 }
11538}
11539
11540// Check types.
11541
11542void
11543Array_construction_expression::do_check_types(Gogo*)
11544{
11545 if (this->vals_ == NULL)
11546 return;
11547
11548 Array_type* at = this->type_->array_type();
11549 int i = 0;
11550 Type* element_type = at->element_type();
11551 for (Expression_list::const_iterator pv = this->vals_->begin();
11552 pv != this->vals_->end();
11553 ++pv, ++i)
11554 {
11555 if (*pv != NULL
11556 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11557 {
11558 error_at((*pv)->location(),
11559 "incompatible type for element %d in composite literal",
11560 i + 1);
11561 this->set_is_error();
11562 }
11563 }
e440a328 11564}
11565
11566// Get a constructor tree for the array values.
11567
11568tree
11569Array_construction_expression::get_constructor_tree(Translate_context* context,
11570 tree type_tree)
11571{
11572 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11573 (this->vals_ == NULL
11574 ? 0
11575 : this->vals_->size()));
11576 Type* element_type = this->type_->array_type()->element_type();
11577 bool is_constant = true;
11578 if (this->vals_ != NULL)
11579 {
11580 size_t i = 0;
ffe743ca 11581 std::vector<unsigned long>::const_iterator pi;
11582 if (this->indexes_ != NULL)
11583 pi = this->indexes_->begin();
e440a328 11584 for (Expression_list::const_iterator pv = this->vals_->begin();
11585 pv != this->vals_->end();
11586 ++pv, ++i)
11587 {
ffe743ca 11588 if (this->indexes_ != NULL)
11589 go_assert(pi != this->indexes_->end());
e440a328 11590 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
ffe743ca 11591
11592 if (this->indexes_ == NULL)
11593 elt->index = size_int(i);
11594 else
11595 elt->index = size_int(*pi);
11596
e440a328 11597 if (*pv == NULL)
63697958 11598 {
11599 Gogo* gogo = context->gogo();
11600 Btype* ebtype = element_type->get_backend(gogo);
11601 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
11602 elt->value = expr_to_tree(zv);
11603 }
e440a328 11604 else
11605 {
11606 tree value_tree = (*pv)->get_tree(context);
11607 elt->value = Expression::convert_for_assignment(context,
11608 element_type,
11609 (*pv)->type(),
11610 value_tree,
11611 this->location());
11612 }
11613 if (elt->value == error_mark_node)
11614 return error_mark_node;
11615 if (!TREE_CONSTANT(elt->value))
11616 is_constant = false;
ffe743ca 11617 if (this->indexes_ != NULL)
11618 ++pi;
e440a328 11619 }
ffe743ca 11620 if (this->indexes_ != NULL)
11621 go_assert(pi == this->indexes_->end());
e440a328 11622 }
11623
11624 tree ret = build_constructor(type_tree, values);
11625 if (is_constant)
11626 TREE_CONSTANT(ret) = 1;
11627 return ret;
11628}
11629
11630// Export an array construction.
11631
11632void
11633Array_construction_expression::do_export(Export* exp) const
11634{
11635 exp->write_c_string("convert(");
11636 exp->write_type(this->type_);
11637 if (this->vals_ != NULL)
11638 {
ffe743ca 11639 std::vector<unsigned long>::const_iterator pi;
11640 if (this->indexes_ != NULL)
11641 pi = this->indexes_->begin();
e440a328 11642 for (Expression_list::const_iterator pv = this->vals_->begin();
11643 pv != this->vals_->end();
11644 ++pv)
11645 {
11646 exp->write_c_string(", ");
ffe743ca 11647
11648 if (this->indexes_ != NULL)
11649 {
11650 char buf[100];
11651 snprintf(buf, sizeof buf, "%lu", *pi);
11652 exp->write_c_string(buf);
11653 exp->write_c_string(":");
11654 }
11655
e440a328 11656 if (*pv != NULL)
11657 (*pv)->export_expression(exp);
ffe743ca 11658
11659 if (this->indexes_ != NULL)
11660 ++pi;
e440a328 11661 }
11662 }
11663 exp->write_c_string(")");
11664}
11665
d751bb78 11666// Dump ast representation of an array construction expressin.
11667
11668void
11669Array_construction_expression::do_dump_expression(
11670 Ast_dump_context* ast_dump_context) const
11671{
ffe743ca 11672 Expression* length = this->type_->array_type()->length();
8b1c301d 11673
11674 ast_dump_context->ostream() << "[" ;
11675 if (length != NULL)
11676 {
11677 ast_dump_context->dump_expression(length);
11678 }
11679 ast_dump_context->ostream() << "]" ;
d751bb78 11680 ast_dump_context->dump_type(this->type_);
11681 ast_dump_context->ostream() << "{" ;
ffe743ca 11682 if (this->indexes_ == NULL)
11683 ast_dump_context->dump_expression_list(this->vals_);
11684 else
11685 {
11686 Expression_list::const_iterator pv = this->vals_->begin();
11687 for (std::vector<unsigned long>::const_iterator pi =
11688 this->indexes_->begin();
11689 pi != this->indexes_->end();
11690 ++pi, ++pv)
11691 {
11692 if (pi != this->indexes_->begin())
11693 ast_dump_context->ostream() << ", ";
11694 ast_dump_context->ostream() << *pi << ':';
11695 ast_dump_context->dump_expression(*pv);
11696 }
11697 }
d751bb78 11698 ast_dump_context->ostream() << "}" ;
11699
11700}
11701
e440a328 11702// Construct a fixed array.
11703
11704class Fixed_array_construction_expression :
11705 public Array_construction_expression
11706{
11707 public:
ffe743ca 11708 Fixed_array_construction_expression(Type* type,
11709 const std::vector<unsigned long>* indexes,
11710 Expression_list* vals, Location location)
e440a328 11711 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
ffe743ca 11712 type, indexes, vals, location)
11713 { go_assert(type->array_type() != NULL && !type->is_slice_type()); }
e440a328 11714
11715 protected:
11716 Expression*
11717 do_copy()
11718 {
11719 return new Fixed_array_construction_expression(this->type(),
ffe743ca 11720 this->indexes(),
e440a328 11721 (this->vals() == NULL
11722 ? NULL
11723 : this->vals()->copy()),
11724 this->location());
11725 }
11726
11727 tree
11728 do_get_tree(Translate_context*);
11729};
11730
11731// Return a tree for constructing a fixed array.
11732
11733tree
11734Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11735{
9f0e0513 11736 Type* type = this->type();
11737 Btype* btype = type->get_backend(context->gogo());
11738 return this->get_constructor_tree(context, type_to_tree(btype));
e440a328 11739}
11740
11741// Construct an open array.
11742
11743class Open_array_construction_expression : public Array_construction_expression
11744{
11745 public:
ffe743ca 11746 Open_array_construction_expression(Type* type,
11747 const std::vector<unsigned long>* indexes,
11748 Expression_list* vals, Location location)
e440a328 11749 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
ffe743ca 11750 type, indexes, vals, location)
11751 { go_assert(type->is_slice_type()); }
e440a328 11752
11753 protected:
11754 // Note that taking the address of an open array literal is invalid.
11755
11756 Expression*
11757 do_copy()
11758 {
11759 return new Open_array_construction_expression(this->type(),
ffe743ca 11760 this->indexes(),
e440a328 11761 (this->vals() == NULL
11762 ? NULL
11763 : this->vals()->copy()),
11764 this->location());
11765 }
11766
11767 tree
11768 do_get_tree(Translate_context*);
11769};
11770
11771// Return a tree for constructing an open array.
11772
11773tree
11774Open_array_construction_expression::do_get_tree(Translate_context* context)
11775{
f9c68f17 11776 Array_type* array_type = this->type()->array_type();
11777 if (array_type == NULL)
11778 {
c484d925 11779 go_assert(this->type()->is_error());
f9c68f17 11780 return error_mark_node;
11781 }
11782
11783 Type* element_type = array_type->element_type();
9f0e0513 11784 Btype* belement_type = element_type->get_backend(context->gogo());
11785 tree element_type_tree = type_to_tree(belement_type);
3d60812e 11786 if (element_type_tree == error_mark_node)
11787 return error_mark_node;
11788
e440a328 11789 tree values;
11790 tree length_tree;
11791 if (this->vals() == NULL || this->vals()->empty())
11792 {
11793 // We need to create a unique value.
11794 tree max = size_int(0);
11795 tree constructor_type = build_array_type(element_type_tree,
11796 build_index_type(max));
11797 if (constructor_type == error_mark_node)
11798 return error_mark_node;
11799 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11800 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11801 elt->index = size_int(0);
63697958 11802 Gogo* gogo = context->gogo();
11803 Btype* btype = element_type->get_backend(gogo);
11804 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
e440a328 11805 values = build_constructor(constructor_type, vec);
11806 if (TREE_CONSTANT(elt->value))
11807 TREE_CONSTANT(values) = 1;
11808 length_tree = size_int(0);
11809 }
11810 else
11811 {
ffe743ca 11812 unsigned long max_index;
11813 if (this->indexes() == NULL)
11814 max_index = this->vals()->size() - 1;
11815 else
00773463 11816 max_index = this->indexes()->back();
ffe743ca 11817 tree max_tree = size_int(max_index);
e440a328 11818 tree constructor_type = build_array_type(element_type_tree,
ffe743ca 11819 build_index_type(max_tree));
e440a328 11820 if (constructor_type == error_mark_node)
11821 return error_mark_node;
11822 values = this->get_constructor_tree(context, constructor_type);
ffe743ca 11823 length_tree = size_int(max_index + 1);
e440a328 11824 }
11825
11826 if (values == error_mark_node)
11827 return error_mark_node;
11828
11829 bool is_constant_initializer = TREE_CONSTANT(values);
d8829beb 11830
11831 // We have to copy the initial values into heap memory if we are in
11832 // a function or if the values are not constants. We also have to
11833 // copy them if they may contain pointers in a non-constant context,
11834 // as otherwise the garbage collector won't see them.
11835 bool copy_to_heap = (context->function() != NULL
11836 || !is_constant_initializer
11837 || (element_type->has_pointer()
11838 && !context->is_const()));
e440a328 11839
11840 if (is_constant_initializer)
11841 {
b13c66cd 11842 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
e440a328 11843 create_tmp_var_name("C"), TREE_TYPE(values));
11844 DECL_EXTERNAL(tmp) = 0;
11845 TREE_PUBLIC(tmp) = 0;
11846 TREE_STATIC(tmp) = 1;
11847 DECL_ARTIFICIAL(tmp) = 1;
d8829beb 11848 if (copy_to_heap)
e440a328 11849 {
d8829beb 11850 // If we are not copying the value to the heap, we will only
11851 // initialize the value once, so we can use this directly
11852 // rather than copying it. In that case we can't make it
11853 // read-only, because the program is permitted to change it.
e440a328 11854 TREE_READONLY(tmp) = 1;
11855 TREE_CONSTANT(tmp) = 1;
11856 }
11857 DECL_INITIAL(tmp) = values;
11858 rest_of_decl_compilation(tmp, 1, 0);
11859 values = tmp;
11860 }
11861
11862 tree space;
11863 tree set;
d8829beb 11864 if (!copy_to_heap)
e440a328 11865 {
d8829beb 11866 // the initializer will only run once.
e440a328 11867 space = build_fold_addr_expr(values);
11868 set = NULL_TREE;
11869 }
11870 else
11871 {
11872 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11873 space = context->gogo()->allocate_memory(element_type, memsize,
11874 this->location());
11875 space = save_expr(space);
11876
11877 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
b13c66cd 11878 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
11879 s);
e440a328 11880 TREE_THIS_NOTRAP(ref) = 1;
11881 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11882 }
11883
11884 // Build a constructor for the open array.
11885
9f0e0513 11886 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
3d60812e 11887 if (type_tree == error_mark_node)
11888 return error_mark_node;
c484d925 11889 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
e440a328 11890
11891 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11892
11893 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11894 tree field = TYPE_FIELDS(type_tree);
c484d925 11895 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
e440a328 11896 elt->index = field;
11897 elt->value = fold_convert(TREE_TYPE(field), space);
11898
11899 elt = VEC_quick_push(constructor_elt, init, NULL);
11900 field = DECL_CHAIN(field);
c484d925 11901 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
e440a328 11902 elt->index = field;
11903 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11904
11905 elt = VEC_quick_push(constructor_elt, init, NULL);
11906 field = DECL_CHAIN(field);
c484d925 11907 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
e440a328 11908 elt->index = field;
11909 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11910
11911 tree constructor = build_constructor(type_tree, init);
3d60812e 11912 if (constructor == error_mark_node)
11913 return error_mark_node;
d8829beb 11914 if (!copy_to_heap)
e440a328 11915 TREE_CONSTANT(constructor) = 1;
11916
11917 if (set == NULL_TREE)
11918 return constructor;
11919 else
11920 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11921}
11922
11923// Make a slice composite literal. This is used by the type
11924// descriptor code.
11925
11926Expression*
11927Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
b13c66cd 11928 Location location)
e440a328 11929{
411eb89e 11930 go_assert(type->is_slice_type());
ffe743ca 11931 return new Open_array_construction_expression(type, NULL, vals, location);
e440a328 11932}
11933
11934// Construct a map.
11935
11936class Map_construction_expression : public Expression
11937{
11938 public:
11939 Map_construction_expression(Type* type, Expression_list* vals,
b13c66cd 11940 Location location)
e440a328 11941 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11942 type_(type), vals_(vals)
c484d925 11943 { go_assert(vals == NULL || vals->size() % 2 == 0); }
e440a328 11944
11945 protected:
11946 int
11947 do_traverse(Traverse* traverse);
11948
11949 Type*
11950 do_type()
11951 { return this->type_; }
11952
11953 void
11954 do_determine_type(const Type_context*);
11955
11956 void
11957 do_check_types(Gogo*);
11958
11959 Expression*
11960 do_copy()
11961 {
11962 return new Map_construction_expression(this->type_, this->vals_->copy(),
11963 this->location());
11964 }
11965
11966 tree
11967 do_get_tree(Translate_context*);
11968
11969 void
11970 do_export(Export*) const;
11971
d751bb78 11972 void
11973 do_dump_expression(Ast_dump_context*) const;
11974
e440a328 11975 private:
11976 // The type of the map to construct.
11977 Type* type_;
11978 // The list of values.
11979 Expression_list* vals_;
11980};
11981
11982// Traversal.
11983
11984int
11985Map_construction_expression::do_traverse(Traverse* traverse)
11986{
11987 if (this->vals_ != NULL
11988 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11989 return TRAVERSE_EXIT;
11990 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11991 return TRAVERSE_EXIT;
11992 return TRAVERSE_CONTINUE;
11993}
11994
11995// Final type determination.
11996
11997void
11998Map_construction_expression::do_determine_type(const Type_context*)
11999{
12000 if (this->vals_ == NULL)
12001 return;
12002
12003 Map_type* mt = this->type_->map_type();
12004 Type_context key_context(mt->key_type(), false);
12005 Type_context val_context(mt->val_type(), false);
12006 for (Expression_list::const_iterator pv = this->vals_->begin();
12007 pv != this->vals_->end();
12008 ++pv)
12009 {
12010 (*pv)->determine_type(&key_context);
12011 ++pv;
12012 (*pv)->determine_type(&val_context);
12013 }
12014}
12015
12016// Check types.
12017
12018void
12019Map_construction_expression::do_check_types(Gogo*)
12020{
12021 if (this->vals_ == NULL)
12022 return;
12023
12024 Map_type* mt = this->type_->map_type();
12025 int i = 0;
12026 Type* key_type = mt->key_type();
12027 Type* val_type = mt->val_type();
12028 for (Expression_list::const_iterator pv = this->vals_->begin();
12029 pv != this->vals_->end();
12030 ++pv, ++i)
12031 {
12032 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12033 {
12034 error_at((*pv)->location(),
12035 "incompatible type for element %d key in map construction",
12036 i + 1);
12037 this->set_is_error();
12038 }
12039 ++pv;
12040 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12041 {
12042 error_at((*pv)->location(),
12043 ("incompatible type for element %d value "
12044 "in map construction"),
12045 i + 1);
12046 this->set_is_error();
12047 }
12048 }
12049}
12050
12051// Return a tree for constructing a map.
12052
12053tree
12054Map_construction_expression::do_get_tree(Translate_context* context)
12055{
12056 Gogo* gogo = context->gogo();
b13c66cd 12057 Location loc = this->location();
e440a328 12058
12059 Map_type* mt = this->type_->map_type();
12060
12061 // Build a struct to hold the key and value.
12062 tree struct_type = make_node(RECORD_TYPE);
12063
12064 Type* key_type = mt->key_type();
12065 tree id = get_identifier("__key");
9f0e0513 12066 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
5845bde6 12067 if (key_type_tree == error_mark_node)
12068 return error_mark_node;
b13c66cd 12069 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12070 key_type_tree);
e440a328 12071 DECL_CONTEXT(key_field) = struct_type;
12072 TYPE_FIELDS(struct_type) = key_field;
12073
12074 Type* val_type = mt->val_type();
12075 id = get_identifier("__val");
9f0e0513 12076 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
5845bde6 12077 if (val_type_tree == error_mark_node)
12078 return error_mark_node;
b13c66cd 12079 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12080 val_type_tree);
e440a328 12081 DECL_CONTEXT(val_field) = struct_type;
12082 DECL_CHAIN(key_field) = val_field;
12083
12084 layout_type(struct_type);
12085
12086 bool is_constant = true;
12087 size_t i = 0;
12088 tree valaddr;
12089 tree make_tmp;
12090
12091 if (this->vals_ == NULL || this->vals_->empty())
12092 {
12093 valaddr = null_pointer_node;
12094 make_tmp = NULL_TREE;
12095 }
12096 else
12097 {
12098 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12099 this->vals_->size() / 2);
12100
12101 for (Expression_list::const_iterator pv = this->vals_->begin();
12102 pv != this->vals_->end();
12103 ++pv, ++i)
12104 {
12105 bool one_is_constant = true;
12106
12107 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12108
12109 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12110 elt->index = key_field;
12111 tree val_tree = (*pv)->get_tree(context);
12112 elt->value = Expression::convert_for_assignment(context, key_type,
12113 (*pv)->type(),
12114 val_tree, loc);
12115 if (elt->value == error_mark_node)
12116 return error_mark_node;
12117 if (!TREE_CONSTANT(elt->value))
12118 one_is_constant = false;
12119
12120 ++pv;
12121
12122 elt = VEC_quick_push(constructor_elt, one, NULL);
12123 elt->index = val_field;
12124 val_tree = (*pv)->get_tree(context);
12125 elt->value = Expression::convert_for_assignment(context, val_type,
12126 (*pv)->type(),
12127 val_tree, loc);
12128 if (elt->value == error_mark_node)
12129 return error_mark_node;
12130 if (!TREE_CONSTANT(elt->value))
12131 one_is_constant = false;
12132
12133 elt = VEC_quick_push(constructor_elt, values, NULL);
12134 elt->index = size_int(i);
12135 elt->value = build_constructor(struct_type, one);
12136 if (one_is_constant)
12137 TREE_CONSTANT(elt->value) = 1;
12138 else
12139 is_constant = false;
12140 }
12141
12142 tree index_type = build_index_type(size_int(i - 1));
12143 tree array_type = build_array_type(struct_type, index_type);
12144 tree init = build_constructor(array_type, values);
12145 if (is_constant)
12146 TREE_CONSTANT(init) = 1;
12147 tree tmp;
12148 if (current_function_decl != NULL)
12149 {
12150 tmp = create_tmp_var(array_type, get_name(array_type));
12151 DECL_INITIAL(tmp) = init;
b13c66cd 12152 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
12153 void_type_node, tmp);
e440a328 12154 TREE_ADDRESSABLE(tmp) = 1;
12155 }
12156 else
12157 {
b13c66cd 12158 tmp = build_decl(loc.gcc_location(), VAR_DECL,
12159 create_tmp_var_name("M"), array_type);
e440a328 12160 DECL_EXTERNAL(tmp) = 0;
12161 TREE_PUBLIC(tmp) = 0;
12162 TREE_STATIC(tmp) = 1;
12163 DECL_ARTIFICIAL(tmp) = 1;
12164 if (!TREE_CONSTANT(init))
b13c66cd 12165 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
12166 void_type_node, tmp, init);
e440a328 12167 else
12168 {
12169 TREE_READONLY(tmp) = 1;
12170 TREE_CONSTANT(tmp) = 1;
12171 DECL_INITIAL(tmp) = init;
12172 make_tmp = NULL_TREE;
12173 }
12174 rest_of_decl_compilation(tmp, 1, 0);
12175 }
12176
12177 valaddr = build_fold_addr_expr(tmp);
12178 }
12179
2b5f213d 12180 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
e440a328 12181
9f0e0513 12182 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
5845bde6 12183 if (type_tree == error_mark_node)
12184 return error_mark_node;
e440a328 12185
12186 static tree construct_map_fndecl;
12187 tree call = Gogo::call_builtin(&construct_map_fndecl,
12188 loc,
12189 "__go_construct_map",
12190 6,
12191 type_tree,
12192 TREE_TYPE(descriptor),
12193 descriptor,
12194 sizetype,
12195 size_int(i),
12196 sizetype,
12197 TYPE_SIZE_UNIT(struct_type),
12198 sizetype,
12199 byte_position(val_field),
12200 sizetype,
12201 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
12202 const_ptr_type_node,
12203 fold_convert(const_ptr_type_node, valaddr));
5fb82b5e 12204 if (call == error_mark_node)
12205 return error_mark_node;
e440a328 12206
12207 tree ret;
12208 if (make_tmp == NULL)
12209 ret = call;
12210 else
b13c66cd 12211 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
12212 make_tmp, call);
e440a328 12213 return ret;
12214}
12215
12216// Export an array construction.
12217
12218void
12219Map_construction_expression::do_export(Export* exp) const
12220{
12221 exp->write_c_string("convert(");
12222 exp->write_type(this->type_);
12223 for (Expression_list::const_iterator pv = this->vals_->begin();
12224 pv != this->vals_->end();
12225 ++pv)
12226 {
12227 exp->write_c_string(", ");
12228 (*pv)->export_expression(exp);
12229 }
12230 exp->write_c_string(")");
12231}
12232
d751bb78 12233// Dump ast representation for a map construction expression.
12234
12235void
12236Map_construction_expression::do_dump_expression(
12237 Ast_dump_context* ast_dump_context) const
12238{
d751bb78 12239 ast_dump_context->ostream() << "{" ;
8b1c301d 12240 ast_dump_context->dump_expression_list(this->vals_, true);
d751bb78 12241 ast_dump_context->ostream() << "}";
12242}
12243
e440a328 12244// A general composite literal. This is lowered to a type specific
12245// version.
12246
12247class Composite_literal_expression : public Parser_expression
12248{
12249 public:
12250 Composite_literal_expression(Type* type, int depth, bool has_keys,
b13c66cd 12251 Expression_list* vals, Location location)
e440a328 12252 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
12253 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
12254 { }
12255
12256 protected:
12257 int
12258 do_traverse(Traverse* traverse);
12259
12260 Expression*
ceeb4318 12261 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 12262
12263 Expression*
12264 do_copy()
12265 {
12266 return new Composite_literal_expression(this->type_, this->depth_,
12267 this->has_keys_,
12268 (this->vals_ == NULL
12269 ? NULL
12270 : this->vals_->copy()),
12271 this->location());
12272 }
12273
d751bb78 12274 void
12275 do_dump_expression(Ast_dump_context*) const;
12276
e440a328 12277 private:
12278 Expression*
81c4b26b 12279 lower_struct(Gogo*, Type*);
e440a328 12280
12281 Expression*
113ef6a5 12282 lower_array(Type*);
e440a328 12283
12284 Expression*
ffe743ca 12285 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
e440a328 12286
12287 Expression*
ceeb4318 12288 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
e440a328 12289
12290 // The type of the composite literal.
12291 Type* type_;
12292 // The depth within a list of composite literals within a composite
12293 // literal, when the type is omitted.
12294 int depth_;
12295 // The values to put in the composite literal.
12296 Expression_list* vals_;
12297 // If this is true, then VALS_ is a list of pairs: a key and a
12298 // value. In an array initializer, a missing key will be NULL.
12299 bool has_keys_;
12300};
12301
12302// Traversal.
12303
12304int
12305Composite_literal_expression::do_traverse(Traverse* traverse)
12306{
12307 if (this->vals_ != NULL
12308 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12309 return TRAVERSE_EXIT;
12310 return Type::traverse(this->type_, traverse);
12311}
12312
12313// Lower a generic composite literal into a specific version based on
12314// the type.
12315
12316Expression*
ceeb4318 12317Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
12318 Statement_inserter* inserter, int)
e440a328 12319{
12320 Type* type = this->type_;
12321
12322 for (int depth = this->depth_; depth > 0; --depth)
12323 {
12324 if (type->array_type() != NULL)
12325 type = type->array_type()->element_type();
12326 else if (type->map_type() != NULL)
12327 type = type->map_type()->val_type();
12328 else
12329 {
5c13bd80 12330 if (!type->is_error())
e440a328 12331 error_at(this->location(),
12332 ("may only omit types within composite literals "
12333 "of slice, array, or map type"));
12334 return Expression::make_error(this->location());
12335 }
12336 }
12337
e00772b3 12338 Type *pt = type->points_to();
12339 bool is_pointer = false;
12340 if (pt != NULL)
12341 {
12342 is_pointer = true;
12343 type = pt;
12344 }
12345
12346 Expression* ret;
5c13bd80 12347 if (type->is_error())
e440a328 12348 return Expression::make_error(this->location());
12349 else if (type->struct_type() != NULL)
e00772b3 12350 ret = this->lower_struct(gogo, type);
e440a328 12351 else if (type->array_type() != NULL)
113ef6a5 12352 ret = this->lower_array(type);
e440a328 12353 else if (type->map_type() != NULL)
e00772b3 12354 ret = this->lower_map(gogo, function, inserter, type);
e440a328 12355 else
12356 {
12357 error_at(this->location(),
12358 ("expected struct, slice, array, or map type "
12359 "for composite literal"));
12360 return Expression::make_error(this->location());
12361 }
e00772b3 12362
12363 if (is_pointer)
12364 ret = Expression::make_heap_composite(ret, this->location());
12365
12366 return ret;
e440a328 12367}
12368
12369// Lower a struct composite literal.
12370
12371Expression*
81c4b26b 12372Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
e440a328 12373{
b13c66cd 12374 Location location = this->location();
e440a328 12375 Struct_type* st = type->struct_type();
12376 if (this->vals_ == NULL || !this->has_keys_)
07daa4e7 12377 {
e6013c28 12378 if (this->vals_ != NULL
12379 && !this->vals_->empty()
12380 && type->named_type() != NULL
12381 && type->named_type()->named_object()->package() != NULL)
12382 {
12383 for (Struct_field_list::const_iterator pf = st->fields()->begin();
12384 pf != st->fields()->end();
12385 ++pf)
07daa4e7 12386 {
e6013c28 12387 if (Gogo::is_hidden_name(pf->field_name()))
07daa4e7 12388 error_at(this->location(),
e6013c28 12389 "assignment of unexported field %qs in %qs literal",
12390 Gogo::message_name(pf->field_name()).c_str(),
12391 type->named_type()->message_name().c_str());
07daa4e7 12392 }
12393 }
12394
12395 return new Struct_construction_expression(type, this->vals_, location);
12396 }
e440a328 12397
12398 size_t field_count = st->field_count();
12399 std::vector<Expression*> vals(field_count);
0c4f5a19 12400 std::vector<int>* traverse_order = new(std::vector<int>);
e440a328 12401 Expression_list::const_iterator p = this->vals_->begin();
12402 while (p != this->vals_->end())
12403 {
12404 Expression* name_expr = *p;
12405
12406 ++p;
c484d925 12407 go_assert(p != this->vals_->end());
e440a328 12408 Expression* val = *p;
12409
12410 ++p;
12411
12412 if (name_expr == NULL)
12413 {
12414 error_at(val->location(), "mixture of field and value initializers");
12415 return Expression::make_error(location);
12416 }
12417
12418 bool bad_key = false;
12419 std::string name;
81c4b26b 12420 const Named_object* no = NULL;
e440a328 12421 switch (name_expr->classification())
12422 {
12423 case EXPRESSION_UNKNOWN_REFERENCE:
12424 name = name_expr->unknown_expression()->name();
12425 break;
12426
12427 case EXPRESSION_CONST_REFERENCE:
81c4b26b 12428 no = static_cast<Const_expression*>(name_expr)->named_object();
e440a328 12429 break;
12430
12431 case EXPRESSION_TYPE:
12432 {
12433 Type* t = name_expr->type();
12434 Named_type* nt = t->named_type();
12435 if (nt == NULL)
12436 bad_key = true;
12437 else
81c4b26b 12438 no = nt->named_object();
e440a328 12439 }
12440 break;
12441
12442 case EXPRESSION_VAR_REFERENCE:
81c4b26b 12443 no = name_expr->var_expression()->named_object();
e440a328 12444 break;
12445
12446 case EXPRESSION_FUNC_REFERENCE:
81c4b26b 12447 no = name_expr->func_expression()->named_object();
e440a328 12448 break;
12449
12450 case EXPRESSION_UNARY:
12451 // If there is a local variable around with the same name as
12452 // the field, and this occurs in the closure, then the
12453 // parser may turn the field reference into an indirection
12454 // through the closure. FIXME: This is a mess.
12455 {
12456 bad_key = true;
12457 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
12458 if (ue->op() == OPERATOR_MULT)
12459 {
12460 Field_reference_expression* fre =
12461 ue->operand()->field_reference_expression();
12462 if (fre != NULL)
12463 {
12464 Struct_type* st =
12465 fre->expr()->type()->deref()->struct_type();
12466 if (st != NULL)
12467 {
12468 const Struct_field* sf = st->field(fre->field_index());
12469 name = sf->field_name();
2d29d278 12470
12471 // See below. FIXME.
12472 if (!Gogo::is_hidden_name(name)
12473 && name[0] >= 'a'
12474 && name[0] <= 'z')
12475 {
12476 if (gogo->lookup_global(name.c_str()) != NULL)
12477 name = gogo->pack_hidden_name(name, false);
12478 }
12479
e440a328 12480 char buf[20];
12481 snprintf(buf, sizeof buf, "%u", fre->field_index());
12482 size_t buflen = strlen(buf);
12483 if (name.compare(name.length() - buflen, buflen, buf)
12484 == 0)
12485 {
12486 name = name.substr(0, name.length() - buflen);
12487 bad_key = false;
12488 }
12489 }
12490 }
12491 }
12492 }
12493 break;
12494
12495 default:
12496 bad_key = true;
12497 break;
12498 }
12499 if (bad_key)
12500 {
12501 error_at(name_expr->location(), "expected struct field name");
12502 return Expression::make_error(location);
12503 }
12504
81c4b26b 12505 if (no != NULL)
12506 {
12507 name = no->name();
12508
12509 // A predefined name won't be packed. If it starts with a
12510 // lower case letter we need to check for that case, because
2d29d278 12511 // the field name will be packed. FIXME.
81c4b26b 12512 if (!Gogo::is_hidden_name(name)
12513 && name[0] >= 'a'
12514 && name[0] <= 'z')
12515 {
12516 Named_object* gno = gogo->lookup_global(name.c_str());
12517 if (gno == no)
12518 name = gogo->pack_hidden_name(name, false);
12519 }
12520 }
12521
e440a328 12522 unsigned int index;
12523 const Struct_field* sf = st->find_local_field(name, &index);
12524 if (sf == NULL)
12525 {
12526 error_at(name_expr->location(), "unknown field %qs in %qs",
12527 Gogo::message_name(name).c_str(),
12528 (type->named_type() != NULL
12529 ? type->named_type()->message_name().c_str()
12530 : "unnamed struct"));
12531 return Expression::make_error(location);
12532 }
12533 if (vals[index] != NULL)
12534 {
12535 error_at(name_expr->location(),
12536 "duplicate value for field %qs in %qs",
12537 Gogo::message_name(name).c_str(),
12538 (type->named_type() != NULL
12539 ? type->named_type()->message_name().c_str()
12540 : "unnamed struct"));
12541 return Expression::make_error(location);
12542 }
12543
07daa4e7 12544 if (type->named_type() != NULL
12545 && type->named_type()->named_object()->package() != NULL
12546 && Gogo::is_hidden_name(sf->field_name()))
12547 error_at(name_expr->location(),
12548 "assignment of unexported field %qs in %qs literal",
12549 Gogo::message_name(sf->field_name()).c_str(),
12550 type->named_type()->message_name().c_str());
07daa4e7 12551
e440a328 12552 vals[index] = val;
0c4f5a19 12553 traverse_order->push_back(index);
e440a328 12554 }
12555
12556 Expression_list* list = new Expression_list;
12557 list->reserve(field_count);
12558 for (size_t i = 0; i < field_count; ++i)
12559 list->push_back(vals[i]);
12560
0c4f5a19 12561 Struct_construction_expression* ret =
12562 new Struct_construction_expression(type, list, location);
12563 ret->set_traverse_order(traverse_order);
12564 return ret;
e440a328 12565}
12566
00773463 12567// Used to sort an index/value array.
12568
12569class Index_value_compare
12570{
12571 public:
12572 bool
12573 operator()(const std::pair<unsigned long, Expression*>& a,
12574 const std::pair<unsigned long, Expression*>& b)
12575 { return a.first < b.first; }
12576};
12577
e440a328 12578// Lower an array composite literal.
12579
12580Expression*
113ef6a5 12581Composite_literal_expression::lower_array(Type* type)
e440a328 12582{
b13c66cd 12583 Location location = this->location();
e440a328 12584 if (this->vals_ == NULL || !this->has_keys_)
ffe743ca 12585 return this->make_array(type, NULL, this->vals_);
e440a328 12586
ffe743ca 12587 std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
12588 indexes->reserve(this->vals_->size());
00773463 12589 bool indexes_out_of_order = false;
ffe743ca 12590 Expression_list* vals = new Expression_list();
12591 vals->reserve(this->vals_->size());
e440a328 12592 unsigned long index = 0;
12593 Expression_list::const_iterator p = this->vals_->begin();
12594 while (p != this->vals_->end())
12595 {
12596 Expression* index_expr = *p;
12597
12598 ++p;
c484d925 12599 go_assert(p != this->vals_->end());
e440a328 12600 Expression* val = *p;
12601
12602 ++p;
12603
ffe743ca 12604 if (index_expr == NULL)
12605 {
12606 if (!indexes->empty())
12607 indexes->push_back(index);
12608 }
12609 else
e440a328 12610 {
ffe743ca 12611 if (indexes->empty() && !vals->empty())
12612 {
12613 for (size_t i = 0; i < vals->size(); ++i)
12614 indexes->push_back(i);
12615 }
12616
0c77715b 12617 Numeric_constant nc;
12618 if (!index_expr->numeric_constant_value(&nc))
e440a328 12619 {
e440a328 12620 error_at(index_expr->location(),
12621 "index expression is not integer constant");
12622 return Expression::make_error(location);
12623 }
6f6d9955 12624
0c77715b 12625 switch (nc.to_unsigned_long(&index))
e440a328 12626 {
0c77715b 12627 case Numeric_constant::NC_UL_VALID:
12628 break;
12629 case Numeric_constant::NC_UL_NOTINT:
12630 error_at(index_expr->location(),
12631 "index expression is not integer constant");
12632 return Expression::make_error(location);
12633 case Numeric_constant::NC_UL_NEGATIVE:
e440a328 12634 error_at(index_expr->location(), "index expression is negative");
12635 return Expression::make_error(location);
0c77715b 12636 case Numeric_constant::NC_UL_BIG:
e440a328 12637 error_at(index_expr->location(), "index value overflow");
12638 return Expression::make_error(location);
0c77715b 12639 default:
12640 go_unreachable();
e440a328 12641 }
6f6d9955 12642
12643 Named_type* ntype = Type::lookup_integer_type("int");
12644 Integer_type* inttype = ntype->integer_type();
0c77715b 12645 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8)
12646 && index >> (inttype->bits() - 1) != 0)
6f6d9955 12647 {
6f6d9955 12648 error_at(index_expr->location(), "index value overflow");
12649 return Expression::make_error(location);
12650 }
12651
ffe743ca 12652 if (std::find(indexes->begin(), indexes->end(), index)
12653 != indexes->end())
e440a328 12654 {
ffe743ca 12655 error_at(index_expr->location(), "duplicate value for index %lu",
e440a328 12656 index);
12657 return Expression::make_error(location);
12658 }
ffe743ca 12659
00773463 12660 if (!indexes->empty() && index < indexes->back())
12661 indexes_out_of_order = true;
12662
ffe743ca 12663 indexes->push_back(index);
e440a328 12664 }
12665
ffe743ca 12666 vals->push_back(val);
12667
e440a328 12668 ++index;
12669 }
12670
ffe743ca 12671 if (indexes->empty())
12672 {
12673 delete indexes;
12674 indexes = NULL;
12675 }
e440a328 12676
00773463 12677 if (indexes_out_of_order)
12678 {
12679 typedef std::vector<std::pair<unsigned long, Expression*> > V;
12680
12681 V v;
12682 v.reserve(indexes->size());
12683 std::vector<unsigned long>::const_iterator pi = indexes->begin();
12684 for (Expression_list::const_iterator pe = vals->begin();
12685 pe != vals->end();
12686 ++pe, ++pi)
12687 v.push_back(std::make_pair(*pi, *pe));
12688
12689 std::sort(v.begin(), v.end(), Index_value_compare());
12690
12691 delete indexes;
12692 delete vals;
12693 indexes = new std::vector<unsigned long>();
12694 indexes->reserve(v.size());
12695 vals = new Expression_list();
12696 vals->reserve(v.size());
12697
12698 for (V::const_iterator p = v.begin(); p != v.end(); ++p)
12699 {
12700 indexes->push_back(p->first);
12701 vals->push_back(p->second);
12702 }
12703 }
12704
ffe743ca 12705 return this->make_array(type, indexes, vals);
e440a328 12706}
12707
12708// Actually build the array composite literal. This handles
12709// [...]{...}.
12710
12711Expression*
ffe743ca 12712Composite_literal_expression::make_array(
12713 Type* type,
12714 const std::vector<unsigned long>* indexes,
12715 Expression_list* vals)
e440a328 12716{
b13c66cd 12717 Location location = this->location();
e440a328 12718 Array_type* at = type->array_type();
ffe743ca 12719
e440a328 12720 if (at->length() != NULL && at->length()->is_nil_expression())
12721 {
ffe743ca 12722 size_t size;
12723 if (vals == NULL)
12724 size = 0;
00773463 12725 else if (indexes != NULL)
12726 size = indexes->back() + 1;
12727 else
ffe743ca 12728 {
12729 size = vals->size();
12730 Integer_type* it = Type::lookup_integer_type("int")->integer_type();
12731 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8)
12732 && size >> (it->bits() - 1) != 0)
12733 {
12734 error_at(location, "too many elements in composite literal");
12735 return Expression::make_error(location);
12736 }
12737 }
ffe743ca 12738
e440a328 12739 mpz_t vlen;
12740 mpz_init_set_ui(vlen, size);
12741 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12742 mpz_clear(vlen);
12743 at = Type::make_array_type(at->element_type(), elen);
12744 type = at;
12745 }
ffe743ca 12746 else if (at->length() != NULL
12747 && !at->length()->is_error_expression()
12748 && this->vals_ != NULL)
12749 {
12750 Numeric_constant nc;
12751 unsigned long val;
12752 if (at->length()->numeric_constant_value(&nc)
12753 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
12754 {
12755 if (indexes == NULL)
12756 {
12757 if (this->vals_->size() > val)
12758 {
12759 error_at(location, "too many elements in composite literal");
12760 return Expression::make_error(location);
12761 }
12762 }
12763 else
12764 {
00773463 12765 unsigned long max = indexes->back();
ffe743ca 12766 if (max >= val)
12767 {
12768 error_at(location,
12769 ("some element keys in composite literal "
12770 "are out of range"));
12771 return Expression::make_error(location);
12772 }
12773 }
12774 }
12775 }
12776
e440a328 12777 if (at->length() != NULL)
ffe743ca 12778 return new Fixed_array_construction_expression(type, indexes, vals,
12779 location);
e440a328 12780 else
ffe743ca 12781 return new Open_array_construction_expression(type, indexes, vals,
12782 location);
e440a328 12783}
12784
12785// Lower a map composite literal.
12786
12787Expression*
a287720d 12788Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
ceeb4318 12789 Statement_inserter* inserter,
a287720d 12790 Type* type)
e440a328 12791{
b13c66cd 12792 Location location = this->location();
e440a328 12793 if (this->vals_ != NULL)
12794 {
12795 if (!this->has_keys_)
12796 {
12797 error_at(location, "map composite literal must have keys");
12798 return Expression::make_error(location);
12799 }
12800
a287720d 12801 for (Expression_list::iterator p = this->vals_->begin();
e440a328 12802 p != this->vals_->end();
12803 p += 2)
12804 {
12805 if (*p == NULL)
12806 {
12807 ++p;
12808 error_at((*p)->location(),
12809 "map composite literal must have keys for every value");
12810 return Expression::make_error(location);
12811 }
a287720d 12812 // Make sure we have lowered the key; it may not have been
12813 // lowered in order to handle keys for struct composite
12814 // literals. Lower it now to get the right error message.
12815 if ((*p)->unknown_expression() != NULL)
12816 {
12817 (*p)->unknown_expression()->clear_is_composite_literal_key();
ceeb4318 12818 gogo->lower_expression(function, inserter, &*p);
c484d925 12819 go_assert((*p)->is_error_expression());
a287720d 12820 return Expression::make_error(location);
12821 }
e440a328 12822 }
12823 }
12824
12825 return new Map_construction_expression(type, this->vals_, location);
12826}
12827
d751bb78 12828// Dump ast representation for a composite literal expression.
12829
12830void
12831Composite_literal_expression::do_dump_expression(
12832 Ast_dump_context* ast_dump_context) const
12833{
8b1c301d 12834 ast_dump_context->ostream() << "composite(";
d751bb78 12835 ast_dump_context->dump_type(this->type_);
12836 ast_dump_context->ostream() << ", {";
8b1c301d 12837 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
d751bb78 12838 ast_dump_context->ostream() << "})";
12839}
12840
e440a328 12841// Make a composite literal expression.
12842
12843Expression*
12844Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12845 Expression_list* vals,
b13c66cd 12846 Location location)
e440a328 12847{
12848 return new Composite_literal_expression(type, depth, has_keys, vals,
12849 location);
12850}
12851
12852// Return whether this expression is a composite literal.
12853
12854bool
12855Expression::is_composite_literal() const
12856{
12857 switch (this->classification_)
12858 {
12859 case EXPRESSION_COMPOSITE_LITERAL:
12860 case EXPRESSION_STRUCT_CONSTRUCTION:
12861 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12862 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12863 case EXPRESSION_MAP_CONSTRUCTION:
12864 return true;
12865 default:
12866 return false;
12867 }
12868}
12869
12870// Return whether this expression is a composite literal which is not
12871// constant.
12872
12873bool
12874Expression::is_nonconstant_composite_literal() const
12875{
12876 switch (this->classification_)
12877 {
12878 case EXPRESSION_STRUCT_CONSTRUCTION:
12879 {
12880 const Struct_construction_expression *psce =
12881 static_cast<const Struct_construction_expression*>(this);
12882 return !psce->is_constant_struct();
12883 }
12884 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12885 {
12886 const Fixed_array_construction_expression *pace =
12887 static_cast<const Fixed_array_construction_expression*>(this);
12888 return !pace->is_constant_array();
12889 }
12890 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12891 {
12892 const Open_array_construction_expression *pace =
12893 static_cast<const Open_array_construction_expression*>(this);
12894 return !pace->is_constant_array();
12895 }
12896 case EXPRESSION_MAP_CONSTRUCTION:
12897 return true;
12898 default:
12899 return false;
12900 }
12901}
12902
12903// Return true if this is a reference to a local variable.
12904
12905bool
12906Expression::is_local_variable() const
12907{
12908 const Var_expression* ve = this->var_expression();
12909 if (ve == NULL)
12910 return false;
12911 const Named_object* no = ve->named_object();
12912 return (no->is_result_variable()
12913 || (no->is_variable() && !no->var_value()->is_global()));
12914}
12915
12916// Class Type_guard_expression.
12917
12918// Traversal.
12919
12920int
12921Type_guard_expression::do_traverse(Traverse* traverse)
12922{
12923 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12924 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12925 return TRAVERSE_EXIT;
12926 return TRAVERSE_CONTINUE;
12927}
12928
12929// Check types of a type guard expression. The expression must have
12930// an interface type, but the actual type conversion is checked at run
12931// time.
12932
12933void
12934Type_guard_expression::do_check_types(Gogo*)
12935{
12936 // 6g permits using a type guard with unsafe.pointer; we are
12937 // compatible.
12938 Type* expr_type = this->expr_->type();
12939 if (expr_type->is_unsafe_pointer_type())
12940 {
12941 if (this->type_->points_to() == NULL
12942 && (this->type_->integer_type() == NULL
12943 || (this->type_->forwarded()
12944 != Type::lookup_integer_type("uintptr"))))
12945 this->report_error(_("invalid unsafe.Pointer conversion"));
12946 }
12947 else if (this->type_->is_unsafe_pointer_type())
12948 {
12949 if (expr_type->points_to() == NULL
12950 && (expr_type->integer_type() == NULL
12951 || (expr_type->forwarded()
12952 != Type::lookup_integer_type("uintptr"))))
12953 this->report_error(_("invalid unsafe.Pointer conversion"));
12954 }
12955 else if (expr_type->interface_type() == NULL)
f725ade8 12956 {
5c13bd80 12957 if (!expr_type->is_error() && !this->type_->is_error())
f725ade8 12958 this->report_error(_("type assertion only valid for interface types"));
12959 this->set_is_error();
12960 }
e440a328 12961 else if (this->type_->interface_type() == NULL)
12962 {
12963 std::string reason;
12964 if (!expr_type->interface_type()->implements_interface(this->type_,
12965 &reason))
12966 {
5c13bd80 12967 if (!this->type_->is_error())
e440a328 12968 {
f725ade8 12969 if (reason.empty())
12970 this->report_error(_("impossible type assertion: "
12971 "type does not implement interface"));
12972 else
12973 error_at(this->location(),
12974 ("impossible type assertion: "
12975 "type does not implement interface (%s)"),
12976 reason.c_str());
e440a328 12977 }
f725ade8 12978 this->set_is_error();
e440a328 12979 }
12980 }
12981}
12982
12983// Return a tree for a type guard expression.
12984
12985tree
12986Type_guard_expression::do_get_tree(Translate_context* context)
12987{
12988 Gogo* gogo = context->gogo();
12989 tree expr_tree = this->expr_->get_tree(context);
12990 if (expr_tree == error_mark_node)
12991 return error_mark_node;
12992 Type* expr_type = this->expr_->type();
12993 if ((this->type_->is_unsafe_pointer_type()
12994 && (expr_type->points_to() != NULL
12995 || expr_type->integer_type() != NULL))
12996 || (expr_type->is_unsafe_pointer_type()
12997 && this->type_->points_to() != NULL))
9f0e0513 12998 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
12999 expr_tree);
e440a328 13000 else if (expr_type->is_unsafe_pointer_type()
13001 && this->type_->integer_type() != NULL)
9f0e0513 13002 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
13003 expr_tree);
e440a328 13004 else if (this->type_->interface_type() != NULL)
13005 return Expression::convert_interface_to_interface(context, this->type_,
13006 this->expr_->type(),
13007 expr_tree, true,
13008 this->location());
13009 else
13010 return Expression::convert_for_assignment(context, this->type_,
13011 this->expr_->type(), expr_tree,
13012 this->location());
13013}
13014
d751bb78 13015// Dump ast representation for a type guard expression.
13016
13017void
13018Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
13019 const
13020{
13021 this->expr_->dump_expression(ast_dump_context);
13022 ast_dump_context->ostream() << ".";
13023 ast_dump_context->dump_type(this->type_);
13024}
13025
e440a328 13026// Make a type guard expression.
13027
13028Expression*
13029Expression::make_type_guard(Expression* expr, Type* type,
b13c66cd 13030 Location location)
e440a328 13031{
13032 return new Type_guard_expression(expr, type, location);
13033}
13034
13035// Class Heap_composite_expression.
13036
13037// When you take the address of a composite literal, it is allocated
13038// on the heap. This class implements that.
13039
13040class Heap_composite_expression : public Expression
13041{
13042 public:
b13c66cd 13043 Heap_composite_expression(Expression* expr, Location location)
e440a328 13044 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13045 expr_(expr)
13046 { }
13047
13048 protected:
13049 int
13050 do_traverse(Traverse* traverse)
13051 { return Expression::traverse(&this->expr_, traverse); }
13052
13053 Type*
13054 do_type()
13055 { return Type::make_pointer_type(this->expr_->type()); }
13056
13057 void
13058 do_determine_type(const Type_context*)
13059 { this->expr_->determine_type_no_context(); }
13060
13061 Expression*
13062 do_copy()
13063 {
13064 return Expression::make_heap_composite(this->expr_->copy(),
13065 this->location());
13066 }
13067
13068 tree
13069 do_get_tree(Translate_context*);
13070
13071 // We only export global objects, and the parser does not generate
13072 // this in global scope.
13073 void
13074 do_export(Export*) const
c3e6f413 13075 { go_unreachable(); }
e440a328 13076
d751bb78 13077 void
13078 do_dump_expression(Ast_dump_context*) const;
13079
e440a328 13080 private:
13081 // The composite literal which is being put on the heap.
13082 Expression* expr_;
13083};
13084
13085// Return a tree which allocates a composite literal on the heap.
13086
13087tree
13088Heap_composite_expression::do_get_tree(Translate_context* context)
13089{
13090 tree expr_tree = this->expr_->get_tree(context);
6d3ed74c 13091 if (expr_tree == error_mark_node || TREE_TYPE(expr_tree) == error_mark_node)
e440a328 13092 return error_mark_node;
13093 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
c484d925 13094 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
e440a328 13095 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13096 expr_size, this->location());
13097 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13098 space = save_expr(space);
b13c66cd 13099 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13100 space);
e440a328 13101 TREE_THIS_NOTRAP(ref) = 1;
13102 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13103 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13104 space);
b13c66cd 13105 SET_EXPR_LOCATION(ret, this->location().gcc_location());
e440a328 13106 return ret;
13107}
13108
d751bb78 13109// Dump ast representation for a heap composite expression.
13110
13111void
13112Heap_composite_expression::do_dump_expression(
13113 Ast_dump_context* ast_dump_context) const
13114{
13115 ast_dump_context->ostream() << "&(";
13116 ast_dump_context->dump_expression(this->expr_);
13117 ast_dump_context->ostream() << ")";
13118}
13119
e440a328 13120// Allocate a composite literal on the heap.
13121
13122Expression*
b13c66cd 13123Expression::make_heap_composite(Expression* expr, Location location)
e440a328 13124{
13125 return new Heap_composite_expression(expr, location);
13126}
13127
13128// Class Receive_expression.
13129
13130// Return the type of a receive expression.
13131
13132Type*
13133Receive_expression::do_type()
13134{
13135 Channel_type* channel_type = this->channel_->type()->channel_type();
13136 if (channel_type == NULL)
13137 return Type::make_error_type();
13138 return channel_type->element_type();
13139}
13140
13141// Check types for a receive expression.
13142
13143void
13144Receive_expression::do_check_types(Gogo*)
13145{
13146 Type* type = this->channel_->type();
5c13bd80 13147 if (type->is_error())
e440a328 13148 {
13149 this->set_is_error();
13150 return;
13151 }
13152 if (type->channel_type() == NULL)
13153 {
13154 this->report_error(_("expected channel"));
13155 return;
13156 }
13157 if (!type->channel_type()->may_receive())
13158 {
13159 this->report_error(_("invalid receive on send-only channel"));
13160 return;
13161 }
13162}
13163
13164// Get a tree for a receive expression.
13165
13166tree
13167Receive_expression::do_get_tree(Translate_context* context)
13168{
f24f10bb 13169 Location loc = this->location();
13170
e440a328 13171 Channel_type* channel_type = this->channel_->type()->channel_type();
5b8368f4 13172 if (channel_type == NULL)
13173 {
c484d925 13174 go_assert(this->channel_->type()->is_error());
5b8368f4 13175 return error_mark_node;
13176 }
f24f10bb 13177
13178 Expression* td = Expression::make_type_descriptor(channel_type, loc);
13179 tree td_tree = td->get_tree(context);
13180
e440a328 13181 Type* element_type = channel_type->element_type();
9f0e0513 13182 Btype* element_type_btype = element_type->get_backend(context->gogo());
13183 tree element_type_tree = type_to_tree(element_type_btype);
e440a328 13184
13185 tree channel = this->channel_->get_tree(context);
13186 if (element_type_tree == error_mark_node || channel == error_mark_node)
13187 return error_mark_node;
13188
f24f10bb 13189 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
e440a328 13190}
13191
d751bb78 13192// Dump ast representation for a receive expression.
13193
13194void
13195Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13196{
13197 ast_dump_context->ostream() << " <- " ;
13198 ast_dump_context->dump_expression(channel_);
13199}
13200
e440a328 13201// Make a receive expression.
13202
13203Receive_expression*
b13c66cd 13204Expression::make_receive(Expression* channel, Location location)
e440a328 13205{
13206 return new Receive_expression(channel, location);
13207}
13208
e440a328 13209// An expression which evaluates to a pointer to the type descriptor
13210// of a type.
13211
13212class Type_descriptor_expression : public Expression
13213{
13214 public:
b13c66cd 13215 Type_descriptor_expression(Type* type, Location location)
e440a328 13216 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13217 type_(type)
13218 { }
13219
13220 protected:
13221 Type*
13222 do_type()
13223 { return Type::make_type_descriptor_ptr_type(); }
13224
13225 void
13226 do_determine_type(const Type_context*)
13227 { }
13228
13229 Expression*
13230 do_copy()
13231 { return this; }
13232
13233 tree
13234 do_get_tree(Translate_context* context)
a1d23b41 13235 {
13236 return this->type_->type_descriptor_pointer(context->gogo(),
13237 this->location());
13238 }
e440a328 13239
d751bb78 13240 void
13241 do_dump_expression(Ast_dump_context*) const;
13242
e440a328 13243 private:
13244 // The type for which this is the descriptor.
13245 Type* type_;
13246};
13247
d751bb78 13248// Dump ast representation for a type descriptor expression.
13249
13250void
13251Type_descriptor_expression::do_dump_expression(
13252 Ast_dump_context* ast_dump_context) const
13253{
13254 ast_dump_context->dump_type(this->type_);
13255}
13256
e440a328 13257// Make a type descriptor expression.
13258
13259Expression*
b13c66cd 13260Expression::make_type_descriptor(Type* type, Location location)
e440a328 13261{
13262 return new Type_descriptor_expression(type, location);
13263}
13264
13265// An expression which evaluates to some characteristic of a type.
13266// This is only used to initialize fields of a type descriptor. Using
13267// a new expression class is slightly inefficient but gives us a good
13268// separation between the frontend and the middle-end with regard to
13269// how types are laid out.
13270
13271class Type_info_expression : public Expression
13272{
13273 public:
13274 Type_info_expression(Type* type, Type_info type_info)
b13c66cd 13275 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
e440a328 13276 type_(type), type_info_(type_info)
13277 { }
13278
13279 protected:
13280 Type*
13281 do_type();
13282
13283 void
13284 do_determine_type(const Type_context*)
13285 { }
13286
13287 Expression*
13288 do_copy()
13289 { return this; }
13290
13291 tree
13292 do_get_tree(Translate_context* context);
13293
d751bb78 13294 void
13295 do_dump_expression(Ast_dump_context*) const;
13296
e440a328 13297 private:
13298 // The type for which we are getting information.
13299 Type* type_;
13300 // What information we want.
13301 Type_info type_info_;
13302};
13303
13304// The type is chosen to match what the type descriptor struct
13305// expects.
13306
13307Type*
13308Type_info_expression::do_type()
13309{
13310 switch (this->type_info_)
13311 {
13312 case TYPE_INFO_SIZE:
13313 return Type::lookup_integer_type("uintptr");
13314 case TYPE_INFO_ALIGNMENT:
13315 case TYPE_INFO_FIELD_ALIGNMENT:
13316 return Type::lookup_integer_type("uint8");
13317 default:
c3e6f413 13318 go_unreachable();
e440a328 13319 }
13320}
13321
13322// Return type information in GENERIC.
13323
13324tree
13325Type_info_expression::do_get_tree(Translate_context* context)
13326{
927a01eb 13327 Btype* btype = this->type_->get_backend(context->gogo());
13328 Gogo* gogo = context->gogo();
13329 size_t val;
13330 switch (this->type_info_)
e440a328 13331 {
927a01eb 13332 case TYPE_INFO_SIZE:
13333 val = gogo->backend()->type_size(btype);
13334 break;
13335 case TYPE_INFO_ALIGNMENT:
13336 val = gogo->backend()->type_alignment(btype);
13337 break;
13338 case TYPE_INFO_FIELD_ALIGNMENT:
13339 val = gogo->backend()->type_field_alignment(btype);
13340 break;
13341 default:
13342 go_unreachable();
e440a328 13343 }
927a01eb 13344 tree val_type_tree = type_to_tree(this->type()->get_backend(gogo));
13345 go_assert(val_type_tree != error_mark_node);
13346 return build_int_cstu(val_type_tree, val);
e440a328 13347}
13348
d751bb78 13349// Dump ast representation for a type info expression.
13350
13351void
13352Type_info_expression::do_dump_expression(
13353 Ast_dump_context* ast_dump_context) const
13354{
13355 ast_dump_context->ostream() << "typeinfo(";
13356 ast_dump_context->dump_type(this->type_);
13357 ast_dump_context->ostream() << ",";
13358 ast_dump_context->ostream() <<
13359 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
13360 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
13361 : this->type_info_ == TYPE_INFO_SIZE ? "size "
13362 : "unknown");
13363 ast_dump_context->ostream() << ")";
13364}
13365
e440a328 13366// Make a type info expression.
13367
13368Expression*
13369Expression::make_type_info(Type* type, Type_info type_info)
13370{
13371 return new Type_info_expression(type, type_info);
13372}
13373
13374// An expression which evaluates to the offset of a field within a
13375// struct. This, like Type_info_expression, q.v., is only used to
13376// initialize fields of a type descriptor.
13377
13378class Struct_field_offset_expression : public Expression
13379{
13380 public:
13381 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
b13c66cd 13382 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
13383 Linemap::predeclared_location()),
e440a328 13384 type_(type), field_(field)
13385 { }
13386
13387 protected:
13388 Type*
13389 do_type()
13390 { return Type::lookup_integer_type("uintptr"); }
13391
13392 void
13393 do_determine_type(const Type_context*)
13394 { }
13395
13396 Expression*
13397 do_copy()
13398 { return this; }
13399
13400 tree
13401 do_get_tree(Translate_context* context);
13402
d751bb78 13403 void
13404 do_dump_expression(Ast_dump_context*) const;
13405
e440a328 13406 private:
13407 // The type of the struct.
13408 Struct_type* type_;
13409 // The field.
13410 const Struct_field* field_;
13411};
13412
13413// Return a struct field offset in GENERIC.
13414
13415tree
13416Struct_field_offset_expression::do_get_tree(Translate_context* context)
13417{
9f0e0513 13418 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
e440a328 13419 if (type_tree == error_mark_node)
13420 return error_mark_node;
13421
9f0e0513 13422 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
c484d925 13423 go_assert(val_type_tree != error_mark_node);
e440a328 13424
13425 const Struct_field_list* fields = this->type_->fields();
13426 tree struct_field_tree = TYPE_FIELDS(type_tree);
13427 Struct_field_list::const_iterator p;
13428 for (p = fields->begin();
13429 p != fields->end();
13430 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
13431 {
c484d925 13432 go_assert(struct_field_tree != NULL_TREE);
e440a328 13433 if (&*p == this->field_)
13434 break;
13435 }
c484d925 13436 go_assert(&*p == this->field_);
e440a328 13437
13438 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
13439 byte_position(struct_field_tree));
13440}
13441
d751bb78 13442// Dump ast representation for a struct field offset expression.
13443
13444void
13445Struct_field_offset_expression::do_dump_expression(
13446 Ast_dump_context* ast_dump_context) const
13447{
13448 ast_dump_context->ostream() << "unsafe.Offsetof(";
2d29d278 13449 ast_dump_context->dump_type(this->type_);
13450 ast_dump_context->ostream() << '.';
13451 ast_dump_context->ostream() <<
13452 Gogo::message_name(this->field_->field_name());
d751bb78 13453 ast_dump_context->ostream() << ")";
13454}
13455
e440a328 13456// Make an expression for a struct field offset.
13457
13458Expression*
13459Expression::make_struct_field_offset(Struct_type* type,
13460 const Struct_field* field)
13461{
13462 return new Struct_field_offset_expression(type, field);
13463}
13464
a9182619 13465// An expression which evaluates to a pointer to the map descriptor of
13466// a map type.
13467
13468class Map_descriptor_expression : public Expression
13469{
13470 public:
b13c66cd 13471 Map_descriptor_expression(Map_type* type, Location location)
a9182619 13472 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
13473 type_(type)
13474 { }
13475
13476 protected:
13477 Type*
13478 do_type()
13479 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
13480
13481 void
13482 do_determine_type(const Type_context*)
13483 { }
13484
13485 Expression*
13486 do_copy()
13487 { return this; }
13488
13489 tree
13490 do_get_tree(Translate_context* context)
13491 {
13492 return this->type_->map_descriptor_pointer(context->gogo(),
13493 this->location());
13494 }
13495
d751bb78 13496 void
13497 do_dump_expression(Ast_dump_context*) const;
13498
a9182619 13499 private:
13500 // The type for which this is the descriptor.
13501 Map_type* type_;
13502};
13503
d751bb78 13504// Dump ast representation for a map descriptor expression.
13505
13506void
13507Map_descriptor_expression::do_dump_expression(
13508 Ast_dump_context* ast_dump_context) const
13509{
13510 ast_dump_context->ostream() << "map_descriptor(";
13511 ast_dump_context->dump_type(this->type_);
13512 ast_dump_context->ostream() << ")";
13513}
13514
a9182619 13515// Make a map descriptor expression.
13516
13517Expression*
b13c66cd 13518Expression::make_map_descriptor(Map_type* type, Location location)
a9182619 13519{
13520 return new Map_descriptor_expression(type, location);
13521}
13522
e440a328 13523// An expression which evaluates to the address of an unnamed label.
13524
13525class Label_addr_expression : public Expression
13526{
13527 public:
b13c66cd 13528 Label_addr_expression(Label* label, Location location)
e440a328 13529 : Expression(EXPRESSION_LABEL_ADDR, location),
13530 label_(label)
13531 { }
13532
13533 protected:
13534 Type*
13535 do_type()
13536 { return Type::make_pointer_type(Type::make_void_type()); }
13537
13538 void
13539 do_determine_type(const Type_context*)
13540 { }
13541
13542 Expression*
13543 do_copy()
13544 { return new Label_addr_expression(this->label_, this->location()); }
13545
13546 tree
6e193e6f 13547 do_get_tree(Translate_context* context)
13548 {
e8816003 13549 return expr_to_tree(this->label_->get_addr(context, this->location()));
6e193e6f 13550 }
e440a328 13551
d751bb78 13552 void
13553 do_dump_expression(Ast_dump_context* ast_dump_context) const
13554 { ast_dump_context->ostream() << this->label_->name(); }
13555
e440a328 13556 private:
13557 // The label whose address we are taking.
13558 Label* label_;
13559};
13560
13561// Make an expression for the address of an unnamed label.
13562
13563Expression*
b13c66cd 13564Expression::make_label_addr(Label* label, Location location)
e440a328 13565{
13566 return new Label_addr_expression(label, location);
13567}
13568
13569// Import an expression. This comes at the end in order to see the
13570// various class definitions.
13571
13572Expression*
13573Expression::import_expression(Import* imp)
13574{
13575 int c = imp->peek_char();
13576 if (imp->match_c_string("- ")
13577 || imp->match_c_string("! ")
13578 || imp->match_c_string("^ "))
13579 return Unary_expression::do_import(imp);
13580 else if (c == '(')
13581 return Binary_expression::do_import(imp);
13582 else if (imp->match_c_string("true")
13583 || imp->match_c_string("false"))
13584 return Boolean_expression::do_import(imp);
13585 else if (c == '"')
13586 return String_expression::do_import(imp);
13587 else if (c == '-' || (c >= '0' && c <= '9'))
13588 {
13589 // This handles integers, floats and complex constants.
13590 return Integer_expression::do_import(imp);
13591 }
13592 else if (imp->match_c_string("nil"))
13593 return Nil_expression::do_import(imp);
13594 else if (imp->match_c_string("convert"))
13595 return Type_conversion_expression::do_import(imp);
13596 else
13597 {
13598 error_at(imp->location(), "import error: expected expression");
13599 return Expression::make_error(imp->location());
13600 }
13601}
13602
13603// Class Expression_list.
13604
13605// Traverse the list.
13606
13607int
13608Expression_list::traverse(Traverse* traverse)
13609{
13610 for (Expression_list::iterator p = this->begin();
13611 p != this->end();
13612 ++p)
13613 {
13614 if (*p != NULL)
13615 {
13616 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13617 return TRAVERSE_EXIT;
13618 }
13619 }
13620 return TRAVERSE_CONTINUE;
13621}
13622
13623// Copy the list.
13624
13625Expression_list*
13626Expression_list::copy()
13627{
13628 Expression_list* ret = new Expression_list();
13629 for (Expression_list::iterator p = this->begin();
13630 p != this->end();
13631 ++p)
13632 {
13633 if (*p == NULL)
13634 ret->push_back(NULL);
13635 else
13636 ret->push_back((*p)->copy());
13637 }
13638 return ret;
13639}
13640
13641// Return whether an expression list has an error expression.
13642
13643bool
13644Expression_list::contains_error() const
13645{
13646 for (Expression_list::const_iterator p = this->begin();
13647 p != this->end();
13648 ++p)
13649 if (*p != NULL && (*p)->is_error_expression())
13650 return true;
13651 return false;
13652}
0c77715b 13653
13654// Class Numeric_constant.
13655
13656// Destructor.
13657
13658Numeric_constant::~Numeric_constant()
13659{
13660 this->clear();
13661}
13662
13663// Copy constructor.
13664
13665Numeric_constant::Numeric_constant(const Numeric_constant& a)
13666 : classification_(a.classification_), type_(a.type_)
13667{
13668 switch (a.classification_)
13669 {
13670 case NC_INVALID:
13671 break;
13672 case NC_INT:
13673 case NC_RUNE:
13674 mpz_init_set(this->u_.int_val, a.u_.int_val);
13675 break;
13676 case NC_FLOAT:
13677 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
13678 break;
13679 case NC_COMPLEX:
13680 mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real,
13681 GMP_RNDN);
13682 mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag,
13683 GMP_RNDN);
13684 break;
13685 default:
13686 go_unreachable();
13687 }
13688}
13689
13690// Assignment operator.
13691
13692Numeric_constant&
13693Numeric_constant::operator=(const Numeric_constant& a)
13694{
13695 this->clear();
13696 this->classification_ = a.classification_;
13697 this->type_ = a.type_;
13698 switch (a.classification_)
13699 {
13700 case NC_INVALID:
13701 break;
13702 case NC_INT:
13703 case NC_RUNE:
13704 mpz_init_set(this->u_.int_val, a.u_.int_val);
13705 break;
13706 case NC_FLOAT:
13707 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
13708 break;
13709 case NC_COMPLEX:
13710 mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real,
13711 GMP_RNDN);
13712 mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag,
13713 GMP_RNDN);
13714 break;
13715 default:
13716 go_unreachable();
13717 }
13718 return *this;
13719}
13720
13721// Clear the contents.
13722
13723void
13724Numeric_constant::clear()
13725{
13726 switch (this->classification_)
13727 {
13728 case NC_INVALID:
13729 break;
13730 case NC_INT:
13731 case NC_RUNE:
13732 mpz_clear(this->u_.int_val);
13733 break;
13734 case NC_FLOAT:
13735 mpfr_clear(this->u_.float_val);
13736 break;
13737 case NC_COMPLEX:
13738 mpfr_clear(this->u_.complex_val.real);
13739 mpfr_clear(this->u_.complex_val.imag);
13740 break;
13741 default:
13742 go_unreachable();
13743 }
13744 this->classification_ = NC_INVALID;
13745}
13746
13747// Set to an unsigned long value.
13748
13749void
13750Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
13751{
13752 this->clear();
13753 this->classification_ = NC_INT;
13754 this->type_ = type;
13755 mpz_init_set_ui(this->u_.int_val, val);
13756}
13757
13758// Set to an integer value.
13759
13760void
13761Numeric_constant::set_int(Type* type, const mpz_t val)
13762{
13763 this->clear();
13764 this->classification_ = NC_INT;
13765 this->type_ = type;
13766 mpz_init_set(this->u_.int_val, val);
13767}
13768
13769// Set to a rune value.
13770
13771void
13772Numeric_constant::set_rune(Type* type, const mpz_t val)
13773{
13774 this->clear();
13775 this->classification_ = NC_RUNE;
13776 this->type_ = type;
13777 mpz_init_set(this->u_.int_val, val);
13778}
13779
13780// Set to a floating point value.
13781
13782void
13783Numeric_constant::set_float(Type* type, const mpfr_t val)
13784{
13785 this->clear();
13786 this->classification_ = NC_FLOAT;
13787 this->type_ = type;
833b523c 13788 // Numeric constants do not have negative zero values, so remove
13789 // them here. They also don't have infinity or NaN values, but we
13790 // should never see them here.
13791 if (mpfr_zero_p(val))
13792 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN);
13793 else
13794 mpfr_init_set(this->u_.float_val, val, GMP_RNDN);
0c77715b 13795}
13796
13797// Set to a complex value.
13798
13799void
13800Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag)
13801{
13802 this->clear();
13803 this->classification_ = NC_COMPLEX;
13804 this->type_ = type;
13805 mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN);
13806 mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN);
13807}
13808
13809// Get an int value.
13810
13811void
13812Numeric_constant::get_int(mpz_t* val) const
13813{
13814 go_assert(this->is_int());
13815 mpz_init_set(*val, this->u_.int_val);
13816}
13817
13818// Get a rune value.
13819
13820void
13821Numeric_constant::get_rune(mpz_t* val) const
13822{
13823 go_assert(this->is_rune());
13824 mpz_init_set(*val, this->u_.int_val);
13825}
13826
13827// Get a floating point value.
13828
13829void
13830Numeric_constant::get_float(mpfr_t* val) const
13831{
13832 go_assert(this->is_float());
13833 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
13834}
13835
13836// Get a complex value.
13837
13838void
13839Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const
13840{
13841 go_assert(this->is_complex());
13842 mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN);
13843 mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN);
13844}
13845
13846// Express value as unsigned long if possible.
13847
13848Numeric_constant::To_unsigned_long
13849Numeric_constant::to_unsigned_long(unsigned long* val) const
13850{
13851 switch (this->classification_)
13852 {
13853 case NC_INT:
13854 case NC_RUNE:
13855 return this->mpz_to_unsigned_long(this->u_.int_val, val);
13856 case NC_FLOAT:
13857 return this->mpfr_to_unsigned_long(this->u_.float_val, val);
13858 case NC_COMPLEX:
13859 if (!mpfr_zero_p(this->u_.complex_val.imag))
13860 return NC_UL_NOTINT;
13861 return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val);
13862 default:
13863 go_unreachable();
13864 }
13865}
13866
13867// Express integer value as unsigned long if possible.
13868
13869Numeric_constant::To_unsigned_long
13870Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
13871 unsigned long *val) const
13872{
13873 if (mpz_sgn(ival) < 0)
13874 return NC_UL_NEGATIVE;
13875 unsigned long ui = mpz_get_ui(ival);
13876 if (mpz_cmp_ui(ival, ui) != 0)
13877 return NC_UL_BIG;
13878 *val = ui;
13879 return NC_UL_VALID;
13880}
13881
13882// Express floating point value as unsigned long if possible.
13883
13884Numeric_constant::To_unsigned_long
13885Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
13886 unsigned long *val) const
13887{
13888 if (!mpfr_integer_p(fval))
13889 return NC_UL_NOTINT;
13890 mpz_t ival;
13891 mpz_init(ival);
13892 mpfr_get_z(ival, fval, GMP_RNDN);
13893 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
13894 mpz_clear(ival);
13895 return ret;
13896}
13897
13898// Convert value to integer if possible.
13899
13900bool
13901Numeric_constant::to_int(mpz_t* val) const
13902{
13903 switch (this->classification_)
13904 {
13905 case NC_INT:
13906 case NC_RUNE:
13907 mpz_init_set(*val, this->u_.int_val);
13908 return true;
13909 case NC_FLOAT:
13910 if (!mpfr_integer_p(this->u_.float_val))
13911 return false;
13912 mpz_init(*val);
13913 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN);
13914 return true;
13915 case NC_COMPLEX:
13916 if (!mpfr_zero_p(this->u_.complex_val.imag)
13917 || !mpfr_integer_p(this->u_.complex_val.real))
13918 return false;
13919 mpz_init(*val);
13920 mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN);
13921 return true;
13922 default:
13923 go_unreachable();
13924 }
13925}
13926
13927// Convert value to floating point if possible.
13928
13929bool
13930Numeric_constant::to_float(mpfr_t* val) const
13931{
13932 switch (this->classification_)
13933 {
13934 case NC_INT:
13935 case NC_RUNE:
13936 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN);
13937 return true;
13938 case NC_FLOAT:
13939 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
13940 return true;
13941 case NC_COMPLEX:
13942 if (!mpfr_zero_p(this->u_.complex_val.imag))
13943 return false;
13944 mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN);
13945 return true;
13946 default:
13947 go_unreachable();
13948 }
13949}
13950
13951// Convert value to complex.
13952
13953bool
13954Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const
13955{
13956 switch (this->classification_)
13957 {
13958 case NC_INT:
13959 case NC_RUNE:
13960 mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN);
13961 mpfr_init_set_ui(*vi, 0, GMP_RNDN);
13962 return true;
13963 case NC_FLOAT:
13964 mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN);
13965 mpfr_init_set_ui(*vi, 0, GMP_RNDN);
13966 return true;
13967 case NC_COMPLEX:
13968 mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN);
13969 mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN);
13970 return true;
13971 default:
13972 go_unreachable();
13973 }
13974}
13975
13976// Get the type.
13977
13978Type*
13979Numeric_constant::type() const
13980{
13981 if (this->type_ != NULL)
13982 return this->type_;
13983 switch (this->classification_)
13984 {
13985 case NC_INT:
13986 return Type::make_abstract_integer_type();
13987 case NC_RUNE:
13988 return Type::make_abstract_character_type();
13989 case NC_FLOAT:
13990 return Type::make_abstract_float_type();
13991 case NC_COMPLEX:
13992 return Type::make_abstract_complex_type();
13993 default:
13994 go_unreachable();
13995 }
13996}
13997
13998// If the constant can be expressed in TYPE, then set the type of the
13999// constant to TYPE and return true. Otherwise return false, and, if
14000// ISSUE_ERROR is true, report an appropriate error message.
14001
14002bool
14003Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
14004{
14005 bool ret;
14006 if (type == NULL)
14007 ret = true;
14008 else if (type->integer_type() != NULL)
14009 ret = this->check_int_type(type->integer_type(), issue_error, loc);
14010 else if (type->float_type() != NULL)
14011 ret = this->check_float_type(type->float_type(), issue_error, loc);
14012 else if (type->complex_type() != NULL)
14013 ret = this->check_complex_type(type->complex_type(), issue_error, loc);
14014 else
14015 go_unreachable();
14016 if (ret)
14017 this->type_ = type;
14018 return ret;
14019}
14020
14021// Check whether the constant can be expressed in an integer type.
14022
14023bool
14024Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
14025 Location location) const
14026{
14027 mpz_t val;
14028 switch (this->classification_)
14029 {
14030 case NC_INT:
14031 case NC_RUNE:
14032 mpz_init_set(val, this->u_.int_val);
14033 break;
14034
14035 case NC_FLOAT:
14036 if (!mpfr_integer_p(this->u_.float_val))
14037 {
14038 if (issue_error)
14039 error_at(location, "floating point constant truncated to integer");
14040 return false;
14041 }
14042 mpz_init(val);
14043 mpfr_get_z(val, this->u_.float_val, GMP_RNDN);
14044 break;
14045
14046 case NC_COMPLEX:
14047 if (!mpfr_integer_p(this->u_.complex_val.real)
14048 || !mpfr_zero_p(this->u_.complex_val.imag))
14049 {
14050 if (issue_error)
14051 error_at(location, "complex constant truncated to integer");
14052 return false;
14053 }
14054 mpz_init(val);
14055 mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN);
14056 break;
14057
14058 default:
14059 go_unreachable();
14060 }
14061
14062 bool ret;
14063 if (type->is_abstract())
14064 ret = true;
14065 else
14066 {
14067 int bits = mpz_sizeinbase(val, 2);
14068 if (type->is_unsigned())
14069 {
14070 // For an unsigned type we can only accept a nonnegative
14071 // number, and we must be able to represents at least BITS.
14072 ret = mpz_sgn(val) >= 0 && bits <= type->bits();
14073 }
14074 else
14075 {
14076 // For a signed type we need an extra bit to indicate the
14077 // sign. We have to handle the most negative integer
14078 // specially.
14079 ret = (bits + 1 <= type->bits()
14080 || (bits <= type->bits()
14081 && mpz_sgn(val) < 0
14082 && (mpz_scan1(val, 0)
14083 == static_cast<unsigned long>(type->bits() - 1))
14084 && mpz_scan0(val, type->bits()) == ULONG_MAX));
14085 }
14086 }
14087
14088 if (!ret && issue_error)
14089 error_at(location, "integer constant overflow");
14090
14091 return ret;
14092}
14093
14094// Check whether the constant can be expressed in a floating point
14095// type.
14096
14097bool
14098Numeric_constant::check_float_type(Float_type* type, bool issue_error,
14099 Location location) const
14100{
14101 mpfr_t val;
14102 switch (this->classification_)
14103 {
14104 case NC_INT:
14105 case NC_RUNE:
14106 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN);
14107 break;
14108
14109 case NC_FLOAT:
14110 mpfr_init_set(val, this->u_.float_val, GMP_RNDN);
14111 break;
14112
14113 case NC_COMPLEX:
14114 if (!mpfr_zero_p(this->u_.complex_val.imag))
14115 {
14116 if (issue_error)
14117 error_at(location, "complex constant truncated to float");
14118 return false;
14119 }
14120 mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN);
14121 break;
14122
14123 default:
14124 go_unreachable();
14125 }
14126
14127 bool ret;
14128 if (type->is_abstract())
14129 ret = true;
14130 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
14131 {
14132 // A NaN or Infinity always fits in the range of the type.
14133 ret = true;
14134 }
14135 else
14136 {
14137 mp_exp_t exp = mpfr_get_exp(val);
14138 mp_exp_t max_exp;
14139 switch (type->bits())
14140 {
14141 case 32:
14142 max_exp = 128;
14143 break;
14144 case 64:
14145 max_exp = 1024;
14146 break;
14147 default:
14148 go_unreachable();
14149 }
14150
14151 ret = exp <= max_exp;
14152 }
14153
14154 mpfr_clear(val);
14155
14156 if (!ret && issue_error)
14157 error_at(location, "floating point constant overflow");
14158
14159 return ret;
14160}
14161
14162// Check whether the constant can be expressed in a complex type.
14163
14164bool
14165Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
14166 Location location) const
14167{
14168 if (type->is_abstract())
14169 return true;
14170
14171 mp_exp_t max_exp;
14172 switch (type->bits())
14173 {
14174 case 64:
14175 max_exp = 128;
14176 break;
14177 case 128:
14178 max_exp = 1024;
14179 break;
14180 default:
14181 go_unreachable();
14182 }
14183
14184 mpfr_t real;
14185 switch (this->classification_)
14186 {
14187 case NC_INT:
14188 case NC_RUNE:
14189 mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN);
14190 break;
14191
14192 case NC_FLOAT:
14193 mpfr_init_set(real, this->u_.float_val, GMP_RNDN);
14194 break;
14195
14196 case NC_COMPLEX:
14197 if (!mpfr_nan_p(this->u_.complex_val.imag)
14198 && !mpfr_inf_p(this->u_.complex_val.imag)
14199 && !mpfr_zero_p(this->u_.complex_val.imag))
14200 {
14201 if (mpfr_get_exp(this->u_.complex_val.imag) > max_exp)
14202 {
14203 if (issue_error)
14204 error_at(location, "complex imaginary part overflow");
14205 return false;
14206 }
14207 }
14208 mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN);
14209 break;
14210
14211 default:
14212 go_unreachable();
14213 }
14214
14215 bool ret;
14216 if (mpfr_nan_p(real) || mpfr_inf_p(real) || mpfr_zero_p(real))
14217 ret = true;
14218 else
14219 ret = mpfr_get_exp(real) <= max_exp;
14220
14221 mpfr_clear(real);
14222
14223 if (!ret && issue_error)
14224 error_at(location, "complex real part overflow");
14225
14226 return ret;
14227}
14228
14229// Return an Expression for this value.
14230
14231Expression*
14232Numeric_constant::expression(Location loc) const
14233{
14234 switch (this->classification_)
14235 {
14236 case NC_INT:
14237 return Expression::make_integer(&this->u_.int_val, this->type_, loc);
14238 case NC_RUNE:
14239 return Expression::make_character(&this->u_.int_val, this->type_, loc);
14240 case NC_FLOAT:
14241 return Expression::make_float(&this->u_.float_val, this->type_, loc);
14242 case NC_COMPLEX:
14243 return Expression::make_complex(&this->u_.complex_val.real,
14244 &this->u_.complex_val.imag,
14245 this->type_, loc);
14246 default:
14247 go_unreachable();
14248 }
14249}