]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/gofrontend/expressions.cc
2018-01-08 Steven G. Kargl <kargl@gcc.gnu.org>
[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 "go-c.h"
12#include "gogo.h"
631d5788 13#include "go-diagnostics.h"
438b4bec 14#include "go-encode-id.h"
e440a328 15#include "types.h"
16#include "export.h"
17#include "import.h"
18#include "statements.h"
19#include "lex.h"
a9182619 20#include "runtime.h"
6e193e6f 21#include "backend.h"
e440a328 22#include "expressions.h"
d751bb78 23#include "ast-dump.h"
e440a328 24
25// Class Expression.
26
27Expression::Expression(Expression_classification classification,
b13c66cd 28 Location location)
e440a328 29 : classification_(classification), location_(location)
30{
31}
32
33Expression::~Expression()
34{
35}
36
e440a328 37// Traverse the expressions.
38
39int
40Expression::traverse(Expression** pexpr, Traverse* traverse)
41{
42 Expression* expr = *pexpr;
43 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
44 {
45 int t = traverse->expression(pexpr);
46 if (t == TRAVERSE_EXIT)
47 return TRAVERSE_EXIT;
48 else if (t == TRAVERSE_SKIP_COMPONENTS)
49 return TRAVERSE_CONTINUE;
50 }
51 return expr->do_traverse(traverse);
52}
53
54// Traverse subexpressions of this expression.
55
56int
57Expression::traverse_subexpressions(Traverse* traverse)
58{
59 return this->do_traverse(traverse);
60}
61
62// Default implementation for do_traverse for child classes.
63
64int
65Expression::do_traverse(Traverse*)
66{
67 return TRAVERSE_CONTINUE;
68}
69
70// This virtual function is called by the parser if the value of this
a7549a6a 71// expression is being discarded. By default, we give an error.
72// Expressions with side effects override.
e440a328 73
4f2138d7 74bool
e440a328 75Expression::do_discarding_value()
76{
a7549a6a 77 this->unused_value_error();
4f2138d7 78 return false;
e440a328 79}
80
81// This virtual function is called to export expressions. This will
82// only be used by expressions which may be constant.
83
84void
85Expression::do_export(Export*) const
86{
c3e6f413 87 go_unreachable();
e440a328 88}
89
a7549a6a 90// Give an error saying that the value of the expression is not used.
e440a328 91
92void
a7549a6a 93Expression::unused_value_error()
e440a328 94{
4f2138d7 95 this->report_error(_("value computed is not used"));
e440a328 96}
97
98// Note that this expression is an error. This is called by children
99// when they discover an error.
100
101void
102Expression::set_is_error()
103{
104 this->classification_ = EXPRESSION_ERROR;
105}
106
107// For children to call to report an error conveniently.
108
109void
110Expression::report_error(const char* msg)
111{
631d5788 112 go_error_at(this->location_, "%s", msg);
e440a328 113 this->set_is_error();
114}
115
116// Set types of variables and constants. This is implemented by the
117// child class.
118
119void
120Expression::determine_type(const Type_context* context)
121{
122 this->do_determine_type(context);
123}
124
125// Set types when there is no context.
126
127void
128Expression::determine_type_no_context()
129{
130 Type_context context;
131 this->do_determine_type(&context);
132}
133
2c809f8f 134// Return an expression handling any conversions which must be done during
e440a328 135// assignment.
136
2c809f8f 137Expression*
b4a33049 138Expression::convert_for_assignment(Gogo*, Type* lhs_type,
2c809f8f 139 Expression* rhs, Location location)
e440a328 140{
2c809f8f 141 Type* rhs_type = rhs->type();
142 if (lhs_type->is_error()
143 || rhs_type->is_error()
144 || rhs->is_error_expression())
145 return Expression::make_error(location);
e440a328 146
22984631 147 bool are_identical = Type::are_identical(lhs_type, rhs_type, false, NULL);
148 if (!are_identical && lhs_type->interface_type() != NULL)
e440a328 149 {
150 if (rhs_type->interface_type() == NULL)
2c809f8f 151 return Expression::convert_type_to_interface(lhs_type, rhs, location);
e440a328 152 else
2c809f8f 153 return Expression::convert_interface_to_interface(lhs_type, rhs, false,
154 location);
e440a328 155 }
22984631 156 else if (!are_identical && rhs_type->interface_type() != NULL)
2c809f8f 157 return Expression::convert_interface_to_type(lhs_type, rhs, location);
411eb89e 158 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
e440a328 159 {
2c809f8f 160 // Assigning nil to a slice.
2c809f8f 161 Expression* nil = Expression::make_nil(location);
e67508fa 162 Expression* zero = Expression::make_integer_ul(0, NULL, location);
2c809f8f 163 return Expression::make_slice_value(lhs_type, nil, zero, zero, location);
e440a328 164 }
165 else if (rhs_type->is_nil_type())
2c809f8f 166 return Expression::make_nil(location);
22984631 167 else if (are_identical)
e440a328 168 {
22984631 169 if (lhs_type->forwarded() != rhs_type->forwarded())
170 {
171 // Different but identical types require an explicit
172 // conversion. This happens with type aliases.
173 return Expression::make_cast(lhs_type, rhs, location);
174 }
175
e440a328 176 // No conversion is needed.
2c809f8f 177 return rhs;
178 }
179 else if (lhs_type->points_to() != NULL)
180 return Expression::make_unsafe_cast(lhs_type, rhs, location);
181 else if (lhs_type->is_numeric_type())
182 return Expression::make_cast(lhs_type, rhs, location);
183 else if ((lhs_type->struct_type() != NULL
184 && rhs_type->struct_type() != NULL)
185 || (lhs_type->array_type() != NULL
186 && rhs_type->array_type() != NULL))
e440a328 187 {
188 // This conversion must be permitted by Go, or we wouldn't have
189 // gotten here.
2c809f8f 190 return Expression::make_unsafe_cast(lhs_type, rhs, location);
e440a328 191 }
192 else
2c809f8f 193 return rhs;
e440a328 194}
195
2c809f8f 196// Return an expression for a conversion from a non-interface type to an
e440a328 197// interface type.
198
2c809f8f 199Expression*
200Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs,
201 Location location)
e440a328 202{
e440a328 203 Interface_type* lhs_interface_type = lhs_type->interface_type();
204 bool lhs_is_empty = lhs_interface_type->is_empty();
205
206 // Since RHS_TYPE is a static type, we can create the interface
207 // method table at compile time.
208
209 // When setting an interface to nil, we just set both fields to
210 // NULL.
2c809f8f 211 Type* rhs_type = rhs->type();
e440a328 212 if (rhs_type->is_nil_type())
63697958 213 {
2c809f8f 214 Expression* nil = Expression::make_nil(location);
215 return Expression::make_interface_value(lhs_type, nil, nil, location);
63697958 216 }
e440a328 217
218 // This should have been checked already.
2b8629dd 219 if (!lhs_interface_type->implements_interface(rhs_type, NULL))
220 {
221 go_assert(saw_errors());
222 return Expression::make_error(location);
223 }
e440a328 224
e440a328 225 // An interface is a tuple. If LHS_TYPE is an empty interface type,
226 // then the first field is the type descriptor for RHS_TYPE.
227 // Otherwise it is the interface method table for RHS_TYPE.
2c809f8f 228 Expression* first_field;
e440a328 229 if (lhs_is_empty)
2c809f8f 230 first_field = Expression::make_type_descriptor(rhs_type, location);
e440a328 231 else
232 {
233 // Build the interface method table for this interface and this
234 // object type: a list of function pointers for each interface
235 // method.
236 Named_type* rhs_named_type = rhs_type->named_type();
c0cab2ec 237 Struct_type* rhs_struct_type = rhs_type->struct_type();
e440a328 238 bool is_pointer = false;
c0cab2ec 239 if (rhs_named_type == NULL && rhs_struct_type == NULL)
e440a328 240 {
241 rhs_named_type = rhs_type->deref()->named_type();
c0cab2ec 242 rhs_struct_type = rhs_type->deref()->struct_type();
e440a328 243 is_pointer = true;
244 }
c0cab2ec 245 if (rhs_named_type != NULL)
2c809f8f 246 first_field =
247 rhs_named_type->interface_method_table(lhs_interface_type,
248 is_pointer);
c0cab2ec 249 else if (rhs_struct_type != NULL)
2c809f8f 250 first_field =
251 rhs_struct_type->interface_method_table(lhs_interface_type,
252 is_pointer);
c0cab2ec 253 else
2c809f8f 254 first_field = Expression::make_nil(location);
e440a328 255 }
e440a328 256
2c809f8f 257 Expression* obj;
e440a328 258 if (rhs_type->points_to() != NULL)
259 {
2c809f8f 260 // We are assigning a pointer to the interface; the interface
e440a328 261 // holds the pointer itself.
2c809f8f 262 obj = rhs;
263 }
264 else
265 {
266 // We are assigning a non-pointer value to the interface; the
45ff893b 267 // interface gets a copy of the value in the heap if it escapes.
268 // TODO(cmang): Associate escape state state of RHS with newly
269 // created OBJ.
2c809f8f 270 obj = Expression::make_heap_expression(rhs, location);
e440a328 271 }
272
2c809f8f 273 return Expression::make_interface_value(lhs_type, first_field, obj, location);
274}
e440a328 275
2c809f8f 276// Return an expression for the type descriptor of RHS.
e440a328 277
2c809f8f 278Expression*
279Expression::get_interface_type_descriptor(Expression* rhs)
280{
281 go_assert(rhs->type()->interface_type() != NULL);
282 Location location = rhs->location();
e440a328 283
2c809f8f 284 // The type descriptor is the first field of an empty interface.
285 if (rhs->type()->interface_type()->is_empty())
286 return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR,
287 location);
288
289 Expression* mtable =
290 Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location);
e440a328 291
2c809f8f 292 Expression* descriptor =
f614ea8b 293 Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, location);
2c809f8f 294 descriptor = Expression::make_field_reference(descriptor, 0, location);
295 Expression* nil = Expression::make_nil(location);
e440a328 296
2c809f8f 297 Expression* eq =
298 Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location);
299 return Expression::make_conditional(eq, nil, descriptor, location);
e440a328 300}
301
2c809f8f 302// Return an expression for the conversion of an interface type to an
e440a328 303// interface type.
304
2c809f8f 305Expression*
306Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs,
307 bool for_type_guard,
308 Location location)
e440a328 309{
8ba8cc87 310 if (Type::are_identical(lhs_type, rhs->type(), false, NULL))
311 return rhs;
312
e440a328 313 Interface_type* lhs_interface_type = lhs_type->interface_type();
314 bool lhs_is_empty = lhs_interface_type->is_empty();
315
e440a328 316 // In the general case this requires runtime examination of the type
317 // method table to match it up with the interface methods.
318
319 // FIXME: If all of the methods in the right hand side interface
320 // also appear in the left hand side interface, then we don't need
321 // to do a runtime check, although we still need to build a new
322 // method table.
323
8ba8cc87 324 // We are going to evaluate RHS multiple times.
325 go_assert(rhs->is_variable());
326
e440a328 327 // Get the type descriptor for the right hand side. This will be
328 // NULL for a nil interface.
2c809f8f 329 Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs);
330 Expression* lhs_type_expr =
331 Expression::make_type_descriptor(lhs_type, location);
e440a328 332
2c809f8f 333 Expression* first_field;
e440a328 334 if (for_type_guard)
335 {
336 // A type assertion fails when converting a nil interface.
6098d6cb 337 first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2,
338 lhs_type_expr, rhs_type_expr);
e440a328 339 }
340 else if (lhs_is_empty)
341 {
2c809f8f 342 // A conversion to an empty interface always succeeds, and the
e440a328 343 // first field is just the type descriptor of the object.
2c809f8f 344 first_field = rhs_type_expr;
e440a328 345 }
346 else
347 {
348 // A conversion to a non-empty interface may fail, but unlike a
349 // type assertion converting nil will always succeed.
6098d6cb 350 first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2,
351 lhs_type_expr, rhs_type_expr);
e440a328 352 }
353
354 // The second field is simply the object pointer.
2c809f8f 355 Expression* obj =
356 Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location);
357 return Expression::make_interface_value(lhs_type, first_field, obj, location);
e440a328 358}
359
2c809f8f 360// Return an expression for the conversion of an interface type to a
e440a328 361// non-interface type.
362
2c809f8f 363Expression*
364Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs,
365 Location location)
e440a328 366{
8ba8cc87 367 // We are going to evaluate RHS multiple times.
368 go_assert(rhs->is_variable());
369
e440a328 370 // Call a function to check that the type is valid. The function
371 // will panic with an appropriate runtime type error if the type is
372 // not valid.
2c809f8f 373 Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type,
374 location);
375 Expression* rhs_descriptor =
376 Expression::get_interface_type_descriptor(rhs);
377
378 Type* rhs_type = rhs->type();
379 Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type,
380 location);
381
6098d6cb 382 Expression* check_iface = Runtime::make_call(Runtime::ASSERTI2T,
2c809f8f 383 location, 3, lhs_type_expr,
384 rhs_descriptor, rhs_inter_expr);
e440a328 385
386 // If the call succeeds, pull out the value.
2c809f8f 387 Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT,
388 location);
e440a328 389
390 // If the value is a pointer, then it is the value we want.
391 // Otherwise it points to the value.
392 if (lhs_type->points_to() == NULL)
393 {
2c809f8f 394 obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj,
395 location);
f614ea8b 396 obj = Expression::make_dereference(obj, NIL_CHECK_NOT_NEEDED,
397 location);
e440a328 398 }
2c809f8f 399 return Expression::make_compound(check_iface, obj, location);
e440a328 400}
401
ea664253 402// Convert an expression to its backend representation. This is implemented by
403// the child class. Not that it is not in general safe to call this multiple
e440a328 404// times for a single expression, but that we don't catch such errors.
405
ea664253 406Bexpression*
407Expression::get_backend(Translate_context* context)
e440a328 408{
409 // The child may have marked this expression as having an error.
410 if (this->classification_ == EXPRESSION_ERROR)
ea664253 411 return context->backend()->error_expression();
e440a328 412
ea664253 413 return this->do_get_backend(context);
e440a328 414}
415
48c2a53a 416// Return a backend expression for VAL.
417Bexpression*
418Expression::backend_numeric_constant_expression(Translate_context* context,
419 Numeric_constant* val)
e440a328 420{
48c2a53a 421 Gogo* gogo = context->gogo();
422 Type* type = val->type();
423 if (type == NULL)
424 return gogo->backend()->error_expression();
e440a328 425
48c2a53a 426 Btype* btype = type->get_backend(gogo);
427 Bexpression* ret;
428 if (type->integer_type() != NULL)
e440a328 429 {
430 mpz_t ival;
48c2a53a 431 if (!val->to_int(&ival))
432 {
433 go_assert(saw_errors());
434 return gogo->backend()->error_expression();
435 }
436 ret = gogo->backend()->integer_constant_expression(btype, ival);
e440a328 437 mpz_clear(ival);
e440a328 438 }
48c2a53a 439 else if (type->float_type() != NULL)
e440a328 440 {
48c2a53a 441 mpfr_t fval;
442 if (!val->to_float(&fval))
443 {
444 go_assert(saw_errors());
445 return gogo->backend()->error_expression();
446 }
447 ret = gogo->backend()->float_constant_expression(btype, fval);
448 mpfr_clear(fval);
e440a328 449 }
48c2a53a 450 else if (type->complex_type() != NULL)
e440a328 451 {
fcbea5e4 452 mpc_t cval;
453 if (!val->to_complex(&cval))
48c2a53a 454 {
455 go_assert(saw_errors());
456 return gogo->backend()->error_expression();
457 }
fcbea5e4 458 ret = gogo->backend()->complex_constant_expression(btype, cval);
459 mpc_clear(cval);
e440a328 460 }
461 else
c3e6f413 462 go_unreachable();
e440a328 463
48c2a53a 464 return ret;
e440a328 465}
466
2c809f8f 467// Return an expression which evaluates to true if VAL, of arbitrary integer
468// type, is negative or is more than the maximum value of the Go type "int".
e440a328 469
2c809f8f 470Expression*
471Expression::check_bounds(Expression* val, Location loc)
e440a328 472{
2c809f8f 473 Type* val_type = val->type();
474 Type* bound_type = Type::lookup_integer_type("int");
475
476 int val_type_size;
477 bool val_is_unsigned = false;
478 if (val_type->integer_type() != NULL)
479 {
480 val_type_size = val_type->integer_type()->bits();
481 val_is_unsigned = val_type->integer_type()->is_unsigned();
482 }
483 else
484 {
485 if (!val_type->is_numeric_type()
486 || !Type::are_convertible(bound_type, val_type, NULL))
487 {
488 go_assert(saw_errors());
489 return Expression::make_boolean(true, loc);
490 }
e440a328 491
2c809f8f 492 if (val_type->complex_type() != NULL)
493 val_type_size = val_type->complex_type()->bits();
494 else
495 val_type_size = val_type->float_type()->bits();
496 }
497
498 Expression* negative_index = Expression::make_boolean(false, loc);
499 Expression* index_overflows = Expression::make_boolean(false, loc);
500 if (!val_is_unsigned)
e440a328 501 {
e67508fa 502 Expression* zero = Expression::make_integer_ul(0, val_type, loc);
2c809f8f 503 negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc);
e440a328 504 }
505
2c809f8f 506 int bound_type_size = bound_type->integer_type()->bits();
c3068ac0 507 if (val_type_size > bound_type_size
508 || (val_type_size == bound_type_size
2c809f8f 509 && val_is_unsigned))
510 {
511 mpz_t one;
512 mpz_init_set_ui(one, 1UL);
513
514 // maxval = 2^(bound_type_size - 1) - 1
515 mpz_t maxval;
516 mpz_init(maxval);
517 mpz_mul_2exp(maxval, one, bound_type_size - 1);
518 mpz_sub_ui(maxval, maxval, 1);
e67508fa 519 Expression* max = Expression::make_integer_z(&maxval, val_type, loc);
2c809f8f 520 mpz_clear(one);
521 mpz_clear(maxval);
522
523 index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc);
e440a328 524 }
525
2c809f8f 526 return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows,
527 loc);
e440a328 528}
529
d751bb78 530void
531Expression::dump_expression(Ast_dump_context* ast_dump_context) const
532{
533 this->do_dump_expression(ast_dump_context);
534}
535
e440a328 536// Error expressions. This are used to avoid cascading errors.
537
538class Error_expression : public Expression
539{
540 public:
b13c66cd 541 Error_expression(Location location)
e440a328 542 : Expression(EXPRESSION_ERROR, location)
543 { }
544
545 protected:
546 bool
547 do_is_constant() const
548 { return true; }
549
550 bool
0c77715b 551 do_numeric_constant_value(Numeric_constant* nc) const
e440a328 552 {
0c77715b 553 nc->set_unsigned_long(NULL, 0);
e440a328 554 return true;
555 }
556
4f2138d7 557 bool
e440a328 558 do_discarding_value()
4f2138d7 559 { return true; }
e440a328 560
561 Type*
562 do_type()
563 { return Type::make_error_type(); }
564
565 void
566 do_determine_type(const Type_context*)
567 { }
568
569 Expression*
570 do_copy()
571 { return this; }
572
573 bool
574 do_is_addressable() const
575 { return true; }
576
ea664253 577 Bexpression*
578 do_get_backend(Translate_context* context)
579 { return context->backend()->error_expression(); }
d751bb78 580
581 void
582 do_dump_expression(Ast_dump_context*) const;
e440a328 583};
584
d751bb78 585// Dump the ast representation for an error expression to a dump context.
586
587void
588Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
589{
590 ast_dump_context->ostream() << "_Error_" ;
591}
592
e440a328 593Expression*
b13c66cd 594Expression::make_error(Location location)
e440a328 595{
596 return new Error_expression(location);
597}
598
599// An expression which is really a type. This is used during parsing.
600// It is an error if these survive after lowering.
601
602class
603Type_expression : public Expression
604{
605 public:
b13c66cd 606 Type_expression(Type* type, Location location)
e440a328 607 : Expression(EXPRESSION_TYPE, location),
608 type_(type)
609 { }
610
611 protected:
612 int
613 do_traverse(Traverse* traverse)
614 { return Type::traverse(this->type_, traverse); }
615
616 Type*
617 do_type()
618 { return this->type_; }
619
620 void
621 do_determine_type(const Type_context*)
622 { }
623
624 void
625 do_check_types(Gogo*)
626 { this->report_error(_("invalid use of type")); }
627
628 Expression*
629 do_copy()
630 { return this; }
631
ea664253 632 Bexpression*
633 do_get_backend(Translate_context*)
c3e6f413 634 { go_unreachable(); }
e440a328 635
d751bb78 636 void do_dump_expression(Ast_dump_context*) const;
637
e440a328 638 private:
639 // The type which we are representing as an expression.
640 Type* type_;
641};
642
d751bb78 643void
644Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
645{
646 ast_dump_context->dump_type(this->type_);
647}
648
e440a328 649Expression*
b13c66cd 650Expression::make_type(Type* type, Location location)
e440a328 651{
652 return new Type_expression(type, location);
653}
654
e03bdf36 655// Class Parser_expression.
656
657Type*
658Parser_expression::do_type()
659{
660 // We should never really ask for the type of a Parser_expression.
661 // However, it can happen, at least when we have an invalid const
662 // whose initializer refers to the const itself. In that case we
663 // may ask for the type when lowering the const itself.
c484d925 664 go_assert(saw_errors());
e03bdf36 665 return Type::make_error_type();
666}
667
e440a328 668// Class Var_expression.
669
670// Lower a variable expression. Here we just make sure that the
671// initialization expression of the variable has been lowered. This
672// ensures that we will be able to determine the type of the variable
673// if necessary.
674
675Expression*
ceeb4318 676Var_expression::do_lower(Gogo* gogo, Named_object* function,
677 Statement_inserter* inserter, int)
e440a328 678{
679 if (this->variable_->is_variable())
680 {
681 Variable* var = this->variable_->var_value();
682 // This is either a local variable or a global variable. A
683 // reference to a variable which is local to an enclosing
684 // function will be a reference to a field in a closure.
685 if (var->is_global())
ceeb4318 686 {
687 function = NULL;
688 inserter = NULL;
689 }
690 var->lower_init_expression(gogo, function, inserter);
e440a328 691 }
692 return this;
693}
694
e440a328 695// Return the type of a reference to a variable.
696
697Type*
698Var_expression::do_type()
699{
700 if (this->variable_->is_variable())
701 return this->variable_->var_value()->type();
702 else if (this->variable_->is_result_variable())
703 return this->variable_->result_var_value()->type();
704 else
c3e6f413 705 go_unreachable();
e440a328 706}
707
0ab09e06 708// Determine the type of a reference to a variable.
709
710void
711Var_expression::do_determine_type(const Type_context*)
712{
713 if (this->variable_->is_variable())
714 this->variable_->var_value()->determine_type();
715}
716
e440a328 717// Something takes the address of this variable. This means that we
718// may want to move the variable onto the heap.
719
720void
721Var_expression::do_address_taken(bool escapes)
722{
723 if (!escapes)
f325319b 724 {
725 if (this->variable_->is_variable())
726 this->variable_->var_value()->set_non_escaping_address_taken();
727 else if (this->variable_->is_result_variable())
728 this->variable_->result_var_value()->set_non_escaping_address_taken();
729 else
730 go_unreachable();
731 }
e440a328 732 else
f325319b 733 {
734 if (this->variable_->is_variable())
735 this->variable_->var_value()->set_address_taken();
736 else if (this->variable_->is_result_variable())
737 this->variable_->result_var_value()->set_address_taken();
738 else
739 go_unreachable();
740 }
45ff893b 741
742 if (this->variable_->is_variable()
743 && this->variable_->var_value()->is_in_heap())
744 {
745 Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP);
746 Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP);
747 }
e440a328 748}
749
ea664253 750// Get the backend representation for a reference to a variable.
e440a328 751
ea664253 752Bexpression*
753Var_expression::do_get_backend(Translate_context* context)
e440a328 754{
fe2f84cf 755 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
756 context->function());
fe2f84cf 757 bool is_in_heap;
c6777780 758 Location loc = this->location();
9b27b43c 759 Btype* btype;
760 Gogo* gogo = context->gogo();
fe2f84cf 761 if (this->variable_->is_variable())
9b27b43c 762 {
763 is_in_heap = this->variable_->var_value()->is_in_heap();
764 btype = this->variable_->var_value()->type()->get_backend(gogo);
765 }
fe2f84cf 766 else if (this->variable_->is_result_variable())
9b27b43c 767 {
768 is_in_heap = this->variable_->result_var_value()->is_in_heap();
769 btype = this->variable_->result_var_value()->type()->get_backend(gogo);
770 }
fe2f84cf 771 else
c3e6f413 772 go_unreachable();
c6777780 773
d4e6573e 774 Bexpression* ret =
7af8e400 775 context->backend()->var_expression(bvar, loc);
fe2f84cf 776 if (is_in_heap)
9b27b43c 777 ret = context->backend()->indirect_expression(btype, ret, true, loc);
ea664253 778 return ret;
e440a328 779}
780
d751bb78 781// Ast dump for variable expression.
782
783void
784Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
785{
786 ast_dump_context->ostream() << this->variable_->name() ;
787}
788
e440a328 789// Make a reference to a variable in an expression.
790
791Expression*
b13c66cd 792Expression::make_var_reference(Named_object* var, Location location)
e440a328 793{
794 if (var->is_sink())
795 return Expression::make_sink(location);
796
797 // FIXME: Creating a new object for each reference to a variable is
798 // wasteful.
799 return new Var_expression(var, location);
800}
801
b0c09712 802// Class Enclosed_var_expression.
803
804int
805Enclosed_var_expression::do_traverse(Traverse*)
806{
807 return TRAVERSE_CONTINUE;
808}
809
810// Lower the reference to the enclosed variable.
811
812Expression*
813Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function,
814 Statement_inserter* inserter, int)
815{
816 gogo->lower_expression(function, inserter, &this->reference_);
817 return this;
818}
819
820// Flatten the reference to the enclosed variable.
821
822Expression*
823Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function,
824 Statement_inserter* inserter)
825{
826 gogo->flatten_expression(function, inserter, &this->reference_);
827 return this;
828}
829
830void
831Enclosed_var_expression::do_address_taken(bool escapes)
832{
833 if (!escapes)
834 {
835 if (this->variable_->is_variable())
836 this->variable_->var_value()->set_non_escaping_address_taken();
837 else if (this->variable_->is_result_variable())
838 this->variable_->result_var_value()->set_non_escaping_address_taken();
839 else
840 go_unreachable();
841 }
842 else
843 {
844 if (this->variable_->is_variable())
845 this->variable_->var_value()->set_address_taken();
846 else if (this->variable_->is_result_variable())
847 this->variable_->result_var_value()->set_address_taken();
848 else
849 go_unreachable();
850 }
45ff893b 851
852 if (this->variable_->is_variable()
853 && this->variable_->var_value()->is_in_heap())
854 Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP);
b0c09712 855}
856
857// Ast dump for enclosed variable expression.
858
859void
860Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const
861{
862 adc->ostream() << this->variable_->name();
863}
864
865// Make a reference to a variable within an enclosing function.
866
867Expression*
868Expression::make_enclosing_var_reference(Expression* reference,
869 Named_object* var, Location location)
870{
871 return new Enclosed_var_expression(reference, var, location);
872}
873
e440a328 874// Class Temporary_reference_expression.
875
876// The type.
877
878Type*
879Temporary_reference_expression::do_type()
880{
881 return this->statement_->type();
882}
883
884// Called if something takes the address of this temporary variable.
885// We never have to move temporary variables to the heap, but we do
886// need to know that they must live in the stack rather than in a
887// register.
888
889void
890Temporary_reference_expression::do_address_taken(bool)
891{
892 this->statement_->set_is_address_taken();
893}
894
ea664253 895// Get a backend expression referring to the variable.
e440a328 896
ea664253 897Bexpression*
898Temporary_reference_expression::do_get_backend(Translate_context* context)
e440a328 899{
cd440cff 900 Gogo* gogo = context->gogo();
eefc1ed3 901 Bvariable* bvar = this->statement_->get_backend_variable(context);
7af8e400 902 Bexpression* ret = gogo->backend()->var_expression(bvar, this->location());
eefc1ed3 903
cd440cff 904 // The backend can't always represent the same set of recursive types
eefc1ed3 905 // that the Go frontend can. In some cases this means that a
906 // temporary variable won't have the right backend type. Correct
907 // that here by adding a type cast. We need to use base() to push
908 // the circularity down one level.
cd440cff 909 Type* stype = this->statement_->type();
ceeb4318 910 if (!this->is_lvalue_
03118c21 911 && stype->points_to() != NULL
912 && stype->points_to()->is_void_type())
eefc1ed3 913 {
cd440cff 914 Btype* btype = this->type()->base()->get_backend(gogo);
915 ret = gogo->backend()->convert_expression(btype, ret, this->location());
eefc1ed3 916 }
ea664253 917 return ret;
e440a328 918}
919
d751bb78 920// Ast dump for temporary reference.
921
922void
923Temporary_reference_expression::do_dump_expression(
924 Ast_dump_context* ast_dump_context) const
925{
926 ast_dump_context->dump_temp_variable_name(this->statement_);
927}
928
e440a328 929// Make a reference to a temporary variable.
930
ceeb4318 931Temporary_reference_expression*
e440a328 932Expression::make_temporary_reference(Temporary_statement* statement,
b13c66cd 933 Location location)
e440a328 934{
935 return new Temporary_reference_expression(statement, location);
936}
937
e9d3367e 938// Class Set_and_use_temporary_expression.
939
940// Return the type.
941
942Type*
943Set_and_use_temporary_expression::do_type()
944{
945 return this->statement_->type();
946}
947
0afbb937 948// Determine the type of the expression.
949
950void
951Set_and_use_temporary_expression::do_determine_type(
952 const Type_context* context)
953{
954 this->expr_->determine_type(context);
955}
956
e9d3367e 957// Take the address.
958
959void
960Set_and_use_temporary_expression::do_address_taken(bool)
961{
962 this->statement_->set_is_address_taken();
963}
964
965// Return the backend representation.
966
ea664253 967Bexpression*
968Set_and_use_temporary_expression::do_get_backend(Translate_context* context)
e9d3367e 969{
e9d3367e 970 Location loc = this->location();
a302c105 971 Gogo* gogo = context->gogo();
972 Bvariable* bvar = this->statement_->get_backend_variable(context);
7af8e400 973 Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, loc);
a302c105 974
0ab48656 975 Named_object* fn = context->function();
976 go_assert(fn != NULL);
977 Bfunction* bfn = fn->func_value()->get_or_make_decl(gogo, fn);
ea664253 978 Bexpression* bexpr = this->expr_->get_backend(context);
0ab48656 979 Bstatement* set = gogo->backend()->assignment_statement(bfn, lvar_ref,
980 bexpr, loc);
7af8e400 981 Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc);
a302c105 982 Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc);
ea664253 983 return ret;
e9d3367e 984}
985
986// Dump.
987
988void
989Set_and_use_temporary_expression::do_dump_expression(
990 Ast_dump_context* ast_dump_context) const
991{
992 ast_dump_context->ostream() << '(';
993 ast_dump_context->dump_temp_variable_name(this->statement_);
994 ast_dump_context->ostream() << " = ";
995 this->expr_->dump_expression(ast_dump_context);
996 ast_dump_context->ostream() << ')';
997}
998
999// Make a set-and-use temporary.
1000
1001Set_and_use_temporary_expression*
1002Expression::make_set_and_use_temporary(Temporary_statement* statement,
1003 Expression* expr, Location location)
1004{
1005 return new Set_and_use_temporary_expression(statement, expr, location);
1006}
1007
e440a328 1008// A sink expression--a use of the blank identifier _.
1009
1010class Sink_expression : public Expression
1011{
1012 public:
b13c66cd 1013 Sink_expression(Location location)
e440a328 1014 : Expression(EXPRESSION_SINK, location),
aa93217a 1015 type_(NULL), bvar_(NULL)
e440a328 1016 { }
1017
1018 protected:
4f2138d7 1019 bool
e440a328 1020 do_discarding_value()
4f2138d7 1021 { return true; }
e440a328 1022
1023 Type*
1024 do_type();
1025
1026 void
1027 do_determine_type(const Type_context*);
1028
1029 Expression*
1030 do_copy()
1031 { return new Sink_expression(this->location()); }
1032
ea664253 1033 Bexpression*
1034 do_get_backend(Translate_context*);
e440a328 1035
d751bb78 1036 void
1037 do_dump_expression(Ast_dump_context*) const;
1038
e440a328 1039 private:
1040 // The type of this sink variable.
1041 Type* type_;
1042 // The temporary variable we generate.
aa93217a 1043 Bvariable* bvar_;
e440a328 1044};
1045
1046// Return the type of a sink expression.
1047
1048Type*
1049Sink_expression::do_type()
1050{
1051 if (this->type_ == NULL)
1052 return Type::make_sink_type();
1053 return this->type_;
1054}
1055
1056// Determine the type of a sink expression.
1057
1058void
1059Sink_expression::do_determine_type(const Type_context* context)
1060{
1061 if (context->type != NULL)
1062 this->type_ = context->type;
1063}
1064
1065// Return a temporary variable for a sink expression. This will
1066// presumably be a write-only variable which the middle-end will drop.
1067
ea664253 1068Bexpression*
1069Sink_expression::do_get_backend(Translate_context* context)
e440a328 1070{
aa93217a 1071 Location loc = this->location();
1072 Gogo* gogo = context->gogo();
1073 if (this->bvar_ == NULL)
e440a328 1074 {
c484d925 1075 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
aa93217a 1076 Named_object* fn = context->function();
1077 go_assert(fn != NULL);
1078 Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn);
9f0e0513 1079 Btype* bt = this->type_->get_backend(context->gogo());
aa93217a 1080 Bstatement* decl;
1081 this->bvar_ =
1082 gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL,
1083 false, loc, &decl);
d4e6573e 1084 Bexpression* var_ref =
7af8e400 1085 gogo->backend()->var_expression(this->bvar_, loc);
aa93217a 1086 var_ref = gogo->backend()->compound_expression(decl, var_ref, loc);
ea664253 1087 return var_ref;
e440a328 1088 }
7af8e400 1089 return gogo->backend()->var_expression(this->bvar_, loc);
e440a328 1090}
1091
d751bb78 1092// Ast dump for sink expression.
1093
1094void
1095Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1096{
1097 ast_dump_context->ostream() << "_" ;
1098}
1099
e440a328 1100// Make a sink expression.
1101
1102Expression*
b13c66cd 1103Expression::make_sink(Location location)
e440a328 1104{
1105 return new Sink_expression(location);
1106}
1107
1108// Class Func_expression.
1109
1110// FIXME: Can a function expression appear in a constant expression?
1111// The value is unchanging. Initializing a constant to the address of
1112// a function seems like it could work, though there might be little
1113// point to it.
1114
e440a328 1115// Traversal.
1116
1117int
1118Func_expression::do_traverse(Traverse* traverse)
1119{
1120 return (this->closure_ == NULL
1121 ? TRAVERSE_CONTINUE
1122 : Expression::traverse(&this->closure_, traverse));
1123}
1124
1125// Return the type of a function expression.
1126
1127Type*
1128Func_expression::do_type()
1129{
1130 if (this->function_->is_function())
1131 return this->function_->func_value()->type();
1132 else if (this->function_->is_function_declaration())
1133 return this->function_->func_declaration_value()->type();
1134 else
c3e6f413 1135 go_unreachable();
e440a328 1136}
1137
ea664253 1138// Get the backend representation for the code of a function expression.
e440a328 1139
97267c39 1140Bexpression*
8381eda7 1141Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc)
e440a328 1142{
1143 Function_type* fntype;
8381eda7 1144 if (no->is_function())
1145 fntype = no->func_value()->type();
1146 else if (no->is_function_declaration())
1147 fntype = no->func_declaration_value()->type();
e440a328 1148 else
c3e6f413 1149 go_unreachable();
e440a328 1150
1151 // Builtin functions are handled specially by Call_expression. We
1152 // can't take their address.
1153 if (fntype->is_builtin())
1154 {
631d5788 1155 go_error_at(loc,
1156 "invalid use of special builtin function %qs; must be called",
1157 no->message_name().c_str());
97267c39 1158 return gogo->backend()->error_expression();
e440a328 1159 }
1160
97267c39 1161 Bfunction* fndecl;
e440a328 1162 if (no->is_function())
cf3cae55 1163 fndecl = no->func_value()->get_or_make_decl(gogo, no);
e440a328 1164 else if (no->is_function_declaration())
cf3cae55 1165 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no);
e440a328 1166 else
c3e6f413 1167 go_unreachable();
e440a328 1168
97267c39 1169 return gogo->backend()->function_code_expression(fndecl, loc);
e440a328 1170}
1171
ea664253 1172// Get the backend representation for a function expression. This is used when
1173// we take the address of a function rather than simply calling it. A func
8381eda7 1174// value is represented as a pointer to a block of memory. The first
1175// word of that memory is a pointer to the function code. The
1176// remaining parts of that memory are the addresses of variables that
1177// the function closes over.
e440a328 1178
ea664253 1179Bexpression*
1180Func_expression::do_get_backend(Translate_context* context)
e440a328 1181{
8381eda7 1182 // If there is no closure, just use the function descriptor.
2010c17a 1183 if (this->closure_ == NULL)
8381eda7 1184 {
1185 Gogo* gogo = context->gogo();
1186 Named_object* no = this->function_;
1187 Expression* descriptor;
1188 if (no->is_function())
1189 descriptor = no->func_value()->descriptor(gogo, no);
1190 else if (no->is_function_declaration())
1191 {
1192 if (no->func_declaration_value()->type()->is_builtin())
1193 {
631d5788 1194 go_error_at(this->location(),
1195 ("invalid use of special builtin function %qs; "
1196 "must be called"),
1197 no->message_name().c_str());
ea664253 1198 return gogo->backend()->error_expression();
8381eda7 1199 }
1200 descriptor = no->func_declaration_value()->descriptor(gogo, no);
1201 }
1202 else
1203 go_unreachable();
2010c17a 1204
ea664253 1205 Bexpression* bdesc = descriptor->get_backend(context);
1206 return gogo->backend()->address_expression(bdesc, this->location());
8381eda7 1207 }
e440a328 1208
8381eda7 1209 go_assert(this->function_->func_value()->enclosing() != NULL);
e440a328 1210
8381eda7 1211 // If there is a closure, then the closure is itself the function
1212 // expression. It is a pointer to a struct whose first field points
1213 // to the function code and whose remaining fields are the addresses
1214 // of the closed-over variables.
76818e19 1215 Bexpression *bexpr = this->closure_->get_backend(context);
1216
1217 // Introduce a backend type conversion, to account for any differences
1218 // between the argument type (function descriptor, struct with a
1219 // single field) and the closure (struct with multiple fields).
1220 Gogo* gogo = context->gogo();
1221 Btype *btype = this->type()->get_backend(gogo);
1222 return gogo->backend()->convert_expression(btype, bexpr, this->location());
e440a328 1223}
1224
d751bb78 1225// Ast dump for function.
1226
1227void
1228Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1229{
8b1c301d 1230 ast_dump_context->ostream() << this->function_->name();
1231 if (this->closure_ != NULL)
1232 {
1233 ast_dump_context->ostream() << " {closure = ";
1234 this->closure_->dump_expression(ast_dump_context);
1235 ast_dump_context->ostream() << "}";
1236 }
d751bb78 1237}
1238
e440a328 1239// Make a reference to a function in an expression.
1240
1241Expression*
1242Expression::make_func_reference(Named_object* function, Expression* closure,
b13c66cd 1243 Location location)
e440a328 1244{
b1d7ecfa 1245 Func_expression* fe = new Func_expression(function, closure, location);
1246
1247 // Detect references to builtin functions and set the runtime code if
1248 // appropriate.
1249 if (function->is_function_declaration())
1250 fe->set_runtime_code(Runtime::name_to_code(function->name()));
1251 return fe;
e440a328 1252}
1253
c6837989 1254// Class Func_descriptor_expression.
8381eda7 1255
c6837989 1256// Constructor.
8381eda7 1257
c6837989 1258Func_descriptor_expression::Func_descriptor_expression(Named_object* fn)
1259 : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()),
f8bdf81a 1260 fn_(fn), dvar_(NULL)
c6837989 1261{
1262 go_assert(!fn->is_function() || !fn->func_value()->needs_closure());
1263}
8381eda7 1264
c6837989 1265// Traversal.
8381eda7 1266
c6837989 1267int
1268Func_descriptor_expression::do_traverse(Traverse*)
1269{
1270 return TRAVERSE_CONTINUE;
1271}
8381eda7 1272
1273// All function descriptors have the same type.
1274
1275Type* Func_descriptor_expression::descriptor_type;
1276
1277void
1278Func_descriptor_expression::make_func_descriptor_type()
1279{
1280 if (Func_descriptor_expression::descriptor_type != NULL)
1281 return;
1282 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1283 Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type);
1284 Func_descriptor_expression::descriptor_type =
1285 Type::make_builtin_named_type("functionDescriptor", struct_type);
1286}
1287
1288Type*
1289Func_descriptor_expression::do_type()
1290{
1291 Func_descriptor_expression::make_func_descriptor_type();
1292 return Func_descriptor_expression::descriptor_type;
1293}
1294
ea664253 1295// The backend representation for a function descriptor.
8381eda7 1296
ea664253 1297Bexpression*
1298Func_descriptor_expression::do_get_backend(Translate_context* context)
8381eda7 1299{
8381eda7 1300 Named_object* no = this->fn_;
1301 Location loc = no->location();
ea664253 1302 if (this->dvar_ != NULL)
7af8e400 1303 return context->backend()->var_expression(this->dvar_, loc);
8381eda7 1304
ea664253 1305 Gogo* gogo = context->gogo();
19272321 1306 std::string var_name(gogo->function_descriptor_name(no));
09e57698 1307 bool is_descriptor = false;
1308 if (no->is_function_declaration()
1309 && !no->func_declaration_value()->asm_name().empty()
1310 && Linemap::is_predeclared_location(no->location()))
19272321 1311 is_descriptor = true;
8381eda7 1312
1313 Btype* btype = this->type()->get_backend(gogo);
1314
1315 Bvariable* bvar;
438b4bec 1316 std::string asm_name(go_selectively_encode_id(var_name));
09e57698 1317 if (no->package() != NULL || is_descriptor)
438b4bec 1318 bvar = context->backend()->immutable_struct_reference(var_name, asm_name,
1319 btype, loc);
8381eda7 1320 else
1321 {
1322 Location bloc = Linemap::predeclared_location();
1323 bool is_hidden = ((no->is_function()
1324 && no->func_value()->enclosing() != NULL)
1325 || Gogo::is_thunk(no));
438b4bec 1326 bvar = context->backend()->immutable_struct(var_name, asm_name,
1327 is_hidden, false,
8381eda7 1328 btype, bloc);
1329 Expression_list* vals = new Expression_list();
f8bdf81a 1330 vals->push_back(Expression::make_func_code_reference(this->fn_, bloc));
8381eda7 1331 Expression* init =
1332 Expression::make_struct_composite_literal(this->type(), vals, bloc);
1333 Translate_context bcontext(gogo, NULL, NULL, NULL);
1334 bcontext.set_is_const();
ea664253 1335 Bexpression* binit = init->get_backend(&bcontext);
8381eda7 1336 context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden,
1337 false, btype, bloc, binit);
1338 }
1339
1340 this->dvar_ = bvar;
7af8e400 1341 return gogo->backend()->var_expression(bvar, loc);
8381eda7 1342}
1343
c6837989 1344// Print a function descriptor expression.
1345
1346void
1347Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const
1348{
1349 context->ostream() << "[descriptor " << this->fn_->name() << "]";
1350}
1351
8381eda7 1352// Make a function descriptor expression.
1353
c6837989 1354Func_descriptor_expression*
1355Expression::make_func_descriptor(Named_object* fn)
8381eda7 1356{
c6837989 1357 return new Func_descriptor_expression(fn);
8381eda7 1358}
1359
1360// Make the function descriptor type, so that it can be converted.
1361
1362void
1363Expression::make_func_descriptor_type()
1364{
1365 Func_descriptor_expression::make_func_descriptor_type();
1366}
1367
1368// A reference to just the code of a function.
1369
1370class Func_code_reference_expression : public Expression
1371{
1372 public:
1373 Func_code_reference_expression(Named_object* function, Location location)
1374 : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location),
1375 function_(function)
1376 { }
1377
1378 protected:
1379 int
1380 do_traverse(Traverse*)
1381 { return TRAVERSE_CONTINUE; }
1382
f9ca30f9 1383 bool
3ae06f68 1384 do_is_static_initializer() const
f9ca30f9 1385 { return true; }
1386
8381eda7 1387 Type*
1388 do_type()
1389 { return Type::make_pointer_type(Type::make_void_type()); }
1390
1391 void
1392 do_determine_type(const Type_context*)
1393 { }
1394
1395 Expression*
1396 do_copy()
1397 {
1398 return Expression::make_func_code_reference(this->function_,
1399 this->location());
1400 }
1401
ea664253 1402 Bexpression*
1403 do_get_backend(Translate_context*);
8381eda7 1404
1405 void
1406 do_dump_expression(Ast_dump_context* context) const
1407 { context->ostream() << "[raw " << this->function_->name() << "]" ; }
1408
1409 private:
1410 // The function.
1411 Named_object* function_;
1412};
1413
ea664253 1414// Get the backend representation for a reference to function code.
8381eda7 1415
ea664253 1416Bexpression*
1417Func_code_reference_expression::do_get_backend(Translate_context* context)
8381eda7 1418{
ea664253 1419 return Func_expression::get_code_pointer(context->gogo(), this->function_,
1420 this->location());
8381eda7 1421}
1422
1423// Make a reference to the code of a function.
1424
1425Expression*
1426Expression::make_func_code_reference(Named_object* function, Location location)
1427{
1428 return new Func_code_reference_expression(function, location);
1429}
1430
e440a328 1431// Class Unknown_expression.
1432
1433// Return the name of an unknown expression.
1434
1435const std::string&
1436Unknown_expression::name() const
1437{
1438 return this->named_object_->name();
1439}
1440
1441// Lower a reference to an unknown name.
1442
1443Expression*
ceeb4318 1444Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 1445{
b13c66cd 1446 Location location = this->location();
e440a328 1447 Named_object* no = this->named_object_;
deded542 1448 Named_object* real;
1449 if (!no->is_unknown())
1450 real = no;
1451 else
e440a328 1452 {
deded542 1453 real = no->unknown_value()->real_named_object();
1454 if (real == NULL)
1455 {
1456 if (this->is_composite_literal_key_)
1457 return this;
acf8e158 1458 if (!this->no_error_message_)
631d5788 1459 go_error_at(location, "reference to undefined name %qs",
1460 this->named_object_->message_name().c_str());
deded542 1461 return Expression::make_error(location);
1462 }
e440a328 1463 }
1464 switch (real->classification())
1465 {
1466 case Named_object::NAMED_OBJECT_CONST:
1467 return Expression::make_const_reference(real, location);
1468 case Named_object::NAMED_OBJECT_TYPE:
1469 return Expression::make_type(real->type_value(), location);
1470 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1471 if (this->is_composite_literal_key_)
1472 return this;
acf8e158 1473 if (!this->no_error_message_)
631d5788 1474 go_error_at(location, "reference to undefined type %qs",
1475 real->message_name().c_str());
e440a328 1476 return Expression::make_error(location);
1477 case Named_object::NAMED_OBJECT_VAR:
7d834090 1478 real->var_value()->set_is_used();
e440a328 1479 return Expression::make_var_reference(real, location);
1480 case Named_object::NAMED_OBJECT_FUNC:
1481 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1482 return Expression::make_func_reference(real, NULL, location);
1483 case Named_object::NAMED_OBJECT_PACKAGE:
1484 if (this->is_composite_literal_key_)
1485 return this;
acf8e158 1486 if (!this->no_error_message_)
631d5788 1487 go_error_at(location, "unexpected reference to package");
e440a328 1488 return Expression::make_error(location);
1489 default:
c3e6f413 1490 go_unreachable();
e440a328 1491 }
1492}
1493
d751bb78 1494// Dump the ast representation for an unknown expression to a dump context.
1495
1496void
1497Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1498{
1499 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1500 << ")";
d751bb78 1501}
1502
e440a328 1503// Make a reference to an unknown name.
1504
acf8e158 1505Unknown_expression*
b13c66cd 1506Expression::make_unknown_reference(Named_object* no, Location location)
e440a328 1507{
e440a328 1508 return new Unknown_expression(no, location);
1509}
1510
1511// A boolean expression.
1512
1513class Boolean_expression : public Expression
1514{
1515 public:
b13c66cd 1516 Boolean_expression(bool val, Location location)
e440a328 1517 : Expression(EXPRESSION_BOOLEAN, location),
1518 val_(val), type_(NULL)
1519 { }
1520
1521 static Expression*
1522 do_import(Import*);
1523
1524 protected:
1525 bool
1526 do_is_constant() const
1527 { return true; }
1528
0e168074 1529 bool
3ae06f68 1530 do_is_static_initializer() const
0e168074 1531 { return true; }
1532
e440a328 1533 Type*
1534 do_type();
1535
1536 void
1537 do_determine_type(const Type_context*);
1538
1539 Expression*
1540 do_copy()
1541 { return this; }
1542
ea664253 1543 Bexpression*
1544 do_get_backend(Translate_context* context)
1545 { return context->backend()->boolean_constant_expression(this->val_); }
e440a328 1546
1547 void
1548 do_export(Export* exp) const
1549 { exp->write_c_string(this->val_ ? "true" : "false"); }
1550
d751bb78 1551 void
1552 do_dump_expression(Ast_dump_context* ast_dump_context) const
1553 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1554
e440a328 1555 private:
1556 // The constant.
1557 bool val_;
1558 // The type as determined by context.
1559 Type* type_;
1560};
1561
1562// Get the type.
1563
1564Type*
1565Boolean_expression::do_type()
1566{
1567 if (this->type_ == NULL)
1568 this->type_ = Type::make_boolean_type();
1569 return this->type_;
1570}
1571
1572// Set the type from the context.
1573
1574void
1575Boolean_expression::do_determine_type(const Type_context* context)
1576{
1577 if (this->type_ != NULL && !this->type_->is_abstract())
1578 ;
1579 else if (context->type != NULL && context->type->is_boolean_type())
1580 this->type_ = context->type;
1581 else if (!context->may_be_abstract)
1582 this->type_ = Type::lookup_bool_type();
1583}
1584
1585// Import a boolean constant.
1586
1587Expression*
1588Boolean_expression::do_import(Import* imp)
1589{
1590 if (imp->peek_char() == 't')
1591 {
1592 imp->require_c_string("true");
1593 return Expression::make_boolean(true, imp->location());
1594 }
1595 else
1596 {
1597 imp->require_c_string("false");
1598 return Expression::make_boolean(false, imp->location());
1599 }
1600}
1601
1602// Make a boolean expression.
1603
1604Expression*
b13c66cd 1605Expression::make_boolean(bool val, Location location)
e440a328 1606{
1607 return new Boolean_expression(val, location);
1608}
1609
1610// Class String_expression.
1611
1612// Get the type.
1613
1614Type*
1615String_expression::do_type()
1616{
1617 if (this->type_ == NULL)
1618 this->type_ = Type::make_string_type();
1619 return this->type_;
1620}
1621
1622// Set the type from the context.
1623
1624void
1625String_expression::do_determine_type(const Type_context* context)
1626{
1627 if (this->type_ != NULL && !this->type_->is_abstract())
1628 ;
1629 else if (context->type != NULL && context->type->is_string_type())
1630 this->type_ = context->type;
1631 else if (!context->may_be_abstract)
1632 this->type_ = Type::lookup_string_type();
1633}
1634
1635// Build a string constant.
1636
ea664253 1637Bexpression*
1638String_expression::do_get_backend(Translate_context* context)
e440a328 1639{
2c809f8f 1640 Gogo* gogo = context->gogo();
1641 Btype* btype = Type::make_string_type()->get_backend(gogo);
1642
1643 Location loc = this->location();
1644 std::vector<Bexpression*> init(2);
1645 Bexpression* str_cst =
1646 gogo->backend()->string_constant_expression(this->val_);
1647 init[0] = gogo->backend()->address_expression(str_cst, loc);
1648
1649 Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo);
1650 mpz_t lenval;
1651 mpz_init_set_ui(lenval, this->val_.length());
1652 init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval);
1653 mpz_clear(lenval);
1654
ea664253 1655 return gogo->backend()->constructor_expression(btype, init, loc);
e440a328 1656}
1657
8b1c301d 1658 // Write string literal to string dump.
e440a328 1659
1660void
8b1c301d 1661String_expression::export_string(String_dump* exp,
1662 const String_expression* str)
e440a328 1663{
1664 std::string s;
8b1c301d 1665 s.reserve(str->val_.length() * 4 + 2);
e440a328 1666 s += '"';
8b1c301d 1667 for (std::string::const_iterator p = str->val_.begin();
1668 p != str->val_.end();
e440a328 1669 ++p)
1670 {
1671 if (*p == '\\' || *p == '"')
1672 {
1673 s += '\\';
1674 s += *p;
1675 }
1676 else if (*p >= 0x20 && *p < 0x7f)
1677 s += *p;
1678 else if (*p == '\n')
1679 s += "\\n";
1680 else if (*p == '\t')
1681 s += "\\t";
1682 else
1683 {
1684 s += "\\x";
1685 unsigned char c = *p;
1686 unsigned int dig = c >> 4;
1687 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1688 dig = c & 0xf;
1689 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1690 }
1691 }
1692 s += '"';
1693 exp->write_string(s);
1694}
1695
8b1c301d 1696// Export a string expression.
1697
1698void
1699String_expression::do_export(Export* exp) const
1700{
1701 String_expression::export_string(exp, this);
1702}
1703
e440a328 1704// Import a string expression.
1705
1706Expression*
1707String_expression::do_import(Import* imp)
1708{
1709 imp->require_c_string("\"");
1710 std::string val;
1711 while (true)
1712 {
1713 int c = imp->get_char();
1714 if (c == '"' || c == -1)
1715 break;
1716 if (c != '\\')
1717 val += static_cast<char>(c);
1718 else
1719 {
1720 c = imp->get_char();
1721 if (c == '\\' || c == '"')
1722 val += static_cast<char>(c);
1723 else if (c == 'n')
1724 val += '\n';
1725 else if (c == 't')
1726 val += '\t';
1727 else if (c == 'x')
1728 {
1729 c = imp->get_char();
1730 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1731 c = imp->get_char();
1732 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1733 char v = (vh << 4) | vl;
1734 val += v;
1735 }
1736 else
1737 {
631d5788 1738 go_error_at(imp->location(), "bad string constant");
e440a328 1739 return Expression::make_error(imp->location());
1740 }
1741 }
1742 }
1743 return Expression::make_string(val, imp->location());
1744}
1745
d751bb78 1746// Ast dump for string expression.
1747
1748void
1749String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1750{
8b1c301d 1751 String_expression::export_string(ast_dump_context, this);
d751bb78 1752}
1753
e440a328 1754// Make a string expression.
1755
1756Expression*
b13c66cd 1757Expression::make_string(const std::string& val, Location location)
e440a328 1758{
1759 return new String_expression(val, location);
1760}
1761
2c809f8f 1762// An expression that evaluates to some characteristic of a string.
1763// This is used when indexing, bound-checking, or nil checking a string.
1764
1765class String_info_expression : public Expression
1766{
1767 public:
1768 String_info_expression(Expression* string, String_info string_info,
1769 Location location)
1770 : Expression(EXPRESSION_STRING_INFO, location),
1771 string_(string), string_info_(string_info)
1772 { }
1773
1774 protected:
1775 Type*
1776 do_type();
1777
1778 void
1779 do_determine_type(const Type_context*)
1780 { go_unreachable(); }
1781
1782 Expression*
1783 do_copy()
1784 {
1785 return new String_info_expression(this->string_->copy(), this->string_info_,
1786 this->location());
1787 }
1788
ea664253 1789 Bexpression*
1790 do_get_backend(Translate_context* context);
2c809f8f 1791
1792 void
1793 do_dump_expression(Ast_dump_context*) const;
1794
1795 void
1796 do_issue_nil_check()
1797 { this->string_->issue_nil_check(); }
1798
1799 private:
1800 // The string for which we are getting information.
1801 Expression* string_;
1802 // What information we want.
1803 String_info string_info_;
1804};
1805
1806// Return the type of the string info.
1807
1808Type*
1809String_info_expression::do_type()
1810{
1811 switch (this->string_info_)
1812 {
1813 case STRING_INFO_DATA:
1814 {
1815 Type* byte_type = Type::lookup_integer_type("uint8");
1816 return Type::make_pointer_type(byte_type);
1817 }
1818 case STRING_INFO_LENGTH:
1819 return Type::lookup_integer_type("int");
1820 default:
1821 go_unreachable();
1822 }
1823}
1824
1825// Return string information in GENERIC.
1826
ea664253 1827Bexpression*
1828String_info_expression::do_get_backend(Translate_context* context)
2c809f8f 1829{
1830 Gogo* gogo = context->gogo();
1831
ea664253 1832 Bexpression* bstring = this->string_->get_backend(context);
2c809f8f 1833 switch (this->string_info_)
1834 {
1835 case STRING_INFO_DATA:
1836 case STRING_INFO_LENGTH:
ea664253 1837 return gogo->backend()->struct_field_expression(bstring,
1838 this->string_info_,
1839 this->location());
2c809f8f 1840 break;
1841 default:
1842 go_unreachable();
1843 }
2c809f8f 1844}
1845
1846// Dump ast representation for a type info expression.
1847
1848void
1849String_info_expression::do_dump_expression(
1850 Ast_dump_context* ast_dump_context) const
1851{
1852 ast_dump_context->ostream() << "stringinfo(";
1853 this->string_->dump_expression(ast_dump_context);
1854 ast_dump_context->ostream() << ",";
1855 ast_dump_context->ostream() <<
1856 (this->string_info_ == STRING_INFO_DATA ? "data"
1857 : this->string_info_ == STRING_INFO_LENGTH ? "length"
1858 : "unknown");
1859 ast_dump_context->ostream() << ")";
1860}
1861
1862// Make a string info expression.
1863
1864Expression*
1865Expression::make_string_info(Expression* string, String_info string_info,
1866 Location location)
1867{
1868 return new String_info_expression(string, string_info, location);
1869}
1870
e440a328 1871// Make an integer expression.
1872
1873class Integer_expression : public Expression
1874{
1875 public:
5d4b8566 1876 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1877 Location location)
e440a328 1878 : Expression(EXPRESSION_INTEGER, location),
5d4b8566 1879 type_(type), is_character_constant_(is_character_constant)
e440a328 1880 { mpz_init_set(this->val_, *val); }
1881
1882 static Expression*
1883 do_import(Import*);
1884
8b1c301d 1885 // Write VAL to string dump.
e440a328 1886 static void
8b1c301d 1887 export_integer(String_dump* exp, const mpz_t val);
e440a328 1888
d751bb78 1889 // Write VAL to dump context.
1890 static void
1891 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1892
e440a328 1893 protected:
1894 bool
1895 do_is_constant() const
1896 { return true; }
1897
0e168074 1898 bool
3ae06f68 1899 do_is_static_initializer() const
0e168074 1900 { return true; }
1901
e440a328 1902 bool
0c77715b 1903 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 1904
1905 Type*
1906 do_type();
1907
1908 void
1909 do_determine_type(const Type_context* context);
1910
1911 void
1912 do_check_types(Gogo*);
1913
ea664253 1914 Bexpression*
1915 do_get_backend(Translate_context*);
e440a328 1916
1917 Expression*
1918 do_copy()
5d4b8566 1919 {
1920 if (this->is_character_constant_)
1921 return Expression::make_character(&this->val_, this->type_,
1922 this->location());
1923 else
e67508fa 1924 return Expression::make_integer_z(&this->val_, this->type_,
1925 this->location());
5d4b8566 1926 }
e440a328 1927
1928 void
1929 do_export(Export*) const;
1930
d751bb78 1931 void
1932 do_dump_expression(Ast_dump_context*) const;
1933
e440a328 1934 private:
1935 // The integer value.
1936 mpz_t val_;
1937 // The type so far.
1938 Type* type_;
5d4b8566 1939 // Whether this is a character constant.
1940 bool is_character_constant_;
e440a328 1941};
1942
0c77715b 1943// Return a numeric constant for this expression. We have to mark
1944// this as a character when appropriate.
e440a328 1945
1946bool
0c77715b 1947Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 1948{
0c77715b 1949 if (this->is_character_constant_)
1950 nc->set_rune(this->type_, this->val_);
1951 else
1952 nc->set_int(this->type_, this->val_);
e440a328 1953 return true;
1954}
1955
1956// Return the current type. If we haven't set the type yet, we return
1957// an abstract integer type.
1958
1959Type*
1960Integer_expression::do_type()
1961{
1962 if (this->type_ == NULL)
5d4b8566 1963 {
1964 if (this->is_character_constant_)
1965 this->type_ = Type::make_abstract_character_type();
1966 else
1967 this->type_ = Type::make_abstract_integer_type();
1968 }
e440a328 1969 return this->type_;
1970}
1971
1972// Set the type of the integer value. Here we may switch from an
1973// abstract type to a real type.
1974
1975void
1976Integer_expression::do_determine_type(const Type_context* context)
1977{
1978 if (this->type_ != NULL && !this->type_->is_abstract())
1979 ;
0c77715b 1980 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 1981 this->type_ = context->type;
1982 else if (!context->may_be_abstract)
5d4b8566 1983 {
1984 if (this->is_character_constant_)
1985 this->type_ = Type::lookup_integer_type("int32");
1986 else
1987 this->type_ = Type::lookup_integer_type("int");
1988 }
e440a328 1989}
1990
e440a328 1991// Check the type of an integer constant.
1992
1993void
1994Integer_expression::do_check_types(Gogo*)
1995{
0c77715b 1996 Type* type = this->type_;
1997 if (type == NULL)
e440a328 1998 return;
0c77715b 1999 Numeric_constant nc;
2000 if (this->is_character_constant_)
2001 nc.set_rune(NULL, this->val_);
2002 else
2003 nc.set_int(NULL, this->val_);
2004 if (!nc.set_type(type, true, this->location()))
e440a328 2005 this->set_is_error();
2006}
2007
ea664253 2008// Get the backend representation for an integer constant.
e440a328 2009
ea664253 2010Bexpression*
2011Integer_expression::do_get_backend(Translate_context* context)
e440a328 2012{
12373dd5 2013 if (this->is_error_expression()
2014 || (this->type_ != NULL && this->type_->is_error_type()))
2015 {
2016 go_assert(saw_errors());
2017 return context->gogo()->backend()->error_expression();
2018 }
2019
48c2a53a 2020 Type* resolved_type = NULL;
e440a328 2021 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2022 resolved_type = this->type_;
e440a328 2023 else if (this->type_ != NULL && this->type_->float_type() != NULL)
2024 {
2025 // We are converting to an abstract floating point type.
48c2a53a 2026 resolved_type = Type::lookup_float_type("float64");
e440a328 2027 }
2028 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
2029 {
2030 // We are converting to an abstract complex type.
48c2a53a 2031 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2032 }
2033 else
2034 {
2035 // If we still have an abstract type here, then this is being
2036 // used in a constant expression which didn't get reduced for
2037 // some reason. Use a type which will fit the value. We use <,
2038 // not <=, because we need an extra bit for the sign bit.
2039 int bits = mpz_sizeinbase(this->val_, 2);
1b1f2abf 2040 Type* int_type = Type::lookup_integer_type("int");
2041 if (bits < int_type->integer_type()->bits())
48c2a53a 2042 resolved_type = int_type;
e440a328 2043 else if (bits < 64)
48c2a53a 2044 resolved_type = Type::lookup_integer_type("int64");
e440a328 2045 else
48c2a53a 2046 {
2047 if (!saw_errors())
631d5788 2048 go_error_at(this->location(),
2049 "unknown type for large integer constant");
ea664253 2050 return context->gogo()->backend()->error_expression();
48c2a53a 2051 }
e440a328 2052 }
48c2a53a 2053 Numeric_constant nc;
2054 nc.set_int(resolved_type, this->val_);
ea664253 2055 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2056}
2057
2058// Write VAL to export data.
2059
2060void
8b1c301d 2061Integer_expression::export_integer(String_dump* exp, const mpz_t val)
e440a328 2062{
2063 char* s = mpz_get_str(NULL, 10, val);
2064 exp->write_c_string(s);
2065 free(s);
2066}
2067
2068// Export an integer in a constant expression.
2069
2070void
2071Integer_expression::do_export(Export* exp) const
2072{
2073 Integer_expression::export_integer(exp, this->val_);
5d4b8566 2074 if (this->is_character_constant_)
2075 exp->write_c_string("'");
e440a328 2076 // A trailing space lets us reliably identify the end of the number.
2077 exp->write_c_string(" ");
2078}
2079
2080// Import an integer, floating point, or complex value. This handles
2081// all these types because they all start with digits.
2082
2083Expression*
2084Integer_expression::do_import(Import* imp)
2085{
2086 std::string num = imp->read_identifier();
2087 imp->require_c_string(" ");
2088 if (!num.empty() && num[num.length() - 1] == 'i')
2089 {
2090 mpfr_t real;
2091 size_t plus_pos = num.find('+', 1);
2092 size_t minus_pos = num.find('-', 1);
2093 size_t pos;
2094 if (plus_pos == std::string::npos)
2095 pos = minus_pos;
2096 else if (minus_pos == std::string::npos)
2097 pos = plus_pos;
2098 else
2099 {
631d5788 2100 go_error_at(imp->location(), "bad number in import data: %qs",
2101 num.c_str());
e440a328 2102 return Expression::make_error(imp->location());
2103 }
2104 if (pos == std::string::npos)
2105 mpfr_set_ui(real, 0, GMP_RNDN);
2106 else
2107 {
2108 std::string real_str = num.substr(0, pos);
2109 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
2110 {
631d5788 2111 go_error_at(imp->location(), "bad number in import data: %qs",
2112 real_str.c_str());
e440a328 2113 return Expression::make_error(imp->location());
2114 }
2115 }
2116
2117 std::string imag_str;
2118 if (pos == std::string::npos)
2119 imag_str = num;
2120 else
2121 imag_str = num.substr(pos);
2122 imag_str = imag_str.substr(0, imag_str.size() - 1);
2123 mpfr_t imag;
2124 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2125 {
631d5788 2126 go_error_at(imp->location(), "bad number in import data: %qs",
2127 imag_str.c_str());
e440a328 2128 return Expression::make_error(imp->location());
2129 }
fcbea5e4 2130 mpc_t cval;
2131 mpc_init2(cval, mpc_precision);
2132 mpc_set_fr_fr(cval, real, imag, MPC_RNDNN);
e440a328 2133 mpfr_clear(real);
2134 mpfr_clear(imag);
fcbea5e4 2135 Expression* ret = Expression::make_complex(&cval, NULL, imp->location());
2136 mpc_clear(cval);
e440a328 2137 return ret;
2138 }
2139 else if (num.find('.') == std::string::npos
2140 && num.find('E') == std::string::npos)
2141 {
5d4b8566 2142 bool is_character_constant = (!num.empty()
2143 && num[num.length() - 1] == '\'');
2144 if (is_character_constant)
2145 num = num.substr(0, num.length() - 1);
e440a328 2146 mpz_t val;
2147 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2148 {
631d5788 2149 go_error_at(imp->location(), "bad number in import data: %qs",
2150 num.c_str());
e440a328 2151 return Expression::make_error(imp->location());
2152 }
5d4b8566 2153 Expression* ret;
2154 if (is_character_constant)
2155 ret = Expression::make_character(&val, NULL, imp->location());
2156 else
e67508fa 2157 ret = Expression::make_integer_z(&val, NULL, imp->location());
e440a328 2158 mpz_clear(val);
2159 return ret;
2160 }
2161 else
2162 {
2163 mpfr_t val;
2164 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2165 {
631d5788 2166 go_error_at(imp->location(), "bad number in import data: %qs",
2167 num.c_str());
e440a328 2168 return Expression::make_error(imp->location());
2169 }
2170 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2171 mpfr_clear(val);
2172 return ret;
2173 }
2174}
d751bb78 2175// Ast dump for integer expression.
2176
2177void
2178Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2179{
5d4b8566 2180 if (this->is_character_constant_)
2181 ast_dump_context->ostream() << '\'';
8b1c301d 2182 Integer_expression::export_integer(ast_dump_context, this->val_);
5d4b8566 2183 if (this->is_character_constant_)
2184 ast_dump_context->ostream() << '\'';
d751bb78 2185}
2186
e67508fa 2187// Build a new integer value from a multi-precision integer.
e440a328 2188
2189Expression*
e67508fa 2190Expression::make_integer_z(const mpz_t* val, Type* type, Location location)
5d4b8566 2191{
2192 return new Integer_expression(val, type, false, location);
2193}
2194
e67508fa 2195// Build a new integer value from an unsigned long.
2196
2197Expression*
2198Expression::make_integer_ul(unsigned long val, Type *type, Location location)
2199{
2200 mpz_t zval;
2201 mpz_init_set_ui(zval, val);
2202 Expression* ret = Expression::make_integer_z(&zval, type, location);
2203 mpz_clear(zval);
2204 return ret;
2205}
2206
2207// Build a new integer value from a signed long.
2208
2209Expression*
2210Expression::make_integer_sl(long val, Type *type, Location location)
2211{
2212 mpz_t zval;
2213 mpz_init_set_si(zval, val);
2214 Expression* ret = Expression::make_integer_z(&zval, type, location);
2215 mpz_clear(zval);
2216 return ret;
2217}
2218
3f378015 2219// Store an int64_t in an uninitialized mpz_t.
2220
2221static void
2222set_mpz_from_int64(mpz_t* zval, int64_t val)
2223{
2224 if (val >= 0)
2225 {
2226 unsigned long ul = static_cast<unsigned long>(val);
2227 if (static_cast<int64_t>(ul) == val)
2228 {
2229 mpz_init_set_ui(*zval, ul);
2230 return;
2231 }
2232 }
2233 uint64_t uv;
2234 if (val >= 0)
2235 uv = static_cast<uint64_t>(val);
2236 else
2237 uv = static_cast<uint64_t>(- val);
2238 unsigned long ul = uv & 0xffffffffUL;
2239 mpz_init_set_ui(*zval, ul);
2240 mpz_t hval;
2241 mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32));
2242 mpz_mul_2exp(hval, hval, 32);
2243 mpz_add(*zval, *zval, hval);
2244 mpz_clear(hval);
2245 if (val < 0)
2246 mpz_neg(*zval, *zval);
2247}
2248
2249// Build a new integer value from an int64_t.
2250
2251Expression*
2252Expression::make_integer_int64(int64_t val, Type* type, Location location)
2253{
2254 mpz_t zval;
2255 set_mpz_from_int64(&zval, val);
2256 Expression* ret = Expression::make_integer_z(&zval, type, location);
2257 mpz_clear(zval);
2258 return ret;
2259}
2260
5d4b8566 2261// Build a new character constant value.
2262
2263Expression*
2264Expression::make_character(const mpz_t* val, Type* type, Location location)
e440a328 2265{
5d4b8566 2266 return new Integer_expression(val, type, true, location);
e440a328 2267}
2268
2269// Floats.
2270
2271class Float_expression : public Expression
2272{
2273 public:
b13c66cd 2274 Float_expression(const mpfr_t* val, Type* type, Location location)
e440a328 2275 : Expression(EXPRESSION_FLOAT, location),
2276 type_(type)
2277 {
2278 mpfr_init_set(this->val_, *val, GMP_RNDN);
2279 }
2280
e440a328 2281 // Write VAL to export data.
2282 static void
8b1c301d 2283 export_float(String_dump* exp, const mpfr_t val);
2284
d751bb78 2285 // Write VAL to dump file.
2286 static void
2287 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
e440a328 2288
2289 protected:
2290 bool
2291 do_is_constant() const
2292 { return true; }
2293
0e168074 2294 bool
3ae06f68 2295 do_is_static_initializer() const
0e168074 2296 { return true; }
2297
e440a328 2298 bool
0c77715b 2299 do_numeric_constant_value(Numeric_constant* nc) const
2300 {
2301 nc->set_float(this->type_, this->val_);
2302 return true;
2303 }
e440a328 2304
2305 Type*
2306 do_type();
2307
2308 void
2309 do_determine_type(const Type_context*);
2310
2311 void
2312 do_check_types(Gogo*);
2313
2314 Expression*
2315 do_copy()
2316 { return Expression::make_float(&this->val_, this->type_,
2317 this->location()); }
2318
ea664253 2319 Bexpression*
2320 do_get_backend(Translate_context*);
e440a328 2321
2322 void
2323 do_export(Export*) const;
2324
d751bb78 2325 void
2326 do_dump_expression(Ast_dump_context*) const;
2327
e440a328 2328 private:
2329 // The floating point value.
2330 mpfr_t val_;
2331 // The type so far.
2332 Type* type_;
2333};
2334
e440a328 2335// Return the current type. If we haven't set the type yet, we return
2336// an abstract float type.
2337
2338Type*
2339Float_expression::do_type()
2340{
2341 if (this->type_ == NULL)
2342 this->type_ = Type::make_abstract_float_type();
2343 return this->type_;
2344}
2345
2346// Set the type of the float value. Here we may switch from an
2347// abstract type to a real type.
2348
2349void
2350Float_expression::do_determine_type(const Type_context* context)
2351{
2352 if (this->type_ != NULL && !this->type_->is_abstract())
2353 ;
2354 else if (context->type != NULL
2355 && (context->type->integer_type() != NULL
2356 || context->type->float_type() != NULL
2357 || context->type->complex_type() != NULL))
2358 this->type_ = context->type;
2359 else if (!context->may_be_abstract)
48080209 2360 this->type_ = Type::lookup_float_type("float64");
e440a328 2361}
2362
e440a328 2363// Check the type of a float value.
2364
2365void
2366Float_expression::do_check_types(Gogo*)
2367{
0c77715b 2368 Type* type = this->type_;
2369 if (type == NULL)
e440a328 2370 return;
0c77715b 2371 Numeric_constant nc;
2372 nc.set_float(NULL, this->val_);
2373 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2374 this->set_is_error();
e440a328 2375}
2376
ea664253 2377// Get the backend representation for a float constant.
e440a328 2378
ea664253 2379Bexpression*
2380Float_expression::do_get_backend(Translate_context* context)
e440a328 2381{
12373dd5 2382 if (this->is_error_expression()
2383 || (this->type_ != NULL && this->type_->is_error_type()))
2384 {
2385 go_assert(saw_errors());
2386 return context->gogo()->backend()->error_expression();
2387 }
2388
48c2a53a 2389 Type* resolved_type;
e440a328 2390 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2391 resolved_type = this->type_;
e440a328 2392 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2393 {
2394 // We have an abstract integer type. We just hope for the best.
48c2a53a 2395 resolved_type = Type::lookup_integer_type("int");
2396 }
2397 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
2398 {
2399 // We are converting to an abstract complex type.
2400 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2401 }
2402 else
2403 {
2404 // If we still have an abstract type here, then this is being
2405 // used in a constant expression which didn't get reduced. We
2406 // just use float64 and hope for the best.
48c2a53a 2407 resolved_type = Type::lookup_float_type("float64");
e440a328 2408 }
48c2a53a 2409
2410 Numeric_constant nc;
2411 nc.set_float(resolved_type, this->val_);
ea664253 2412 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2413}
2414
8b1c301d 2415// Write a floating point number to a string dump.
e440a328 2416
2417void
8b1c301d 2418Float_expression::export_float(String_dump *exp, const mpfr_t val)
e440a328 2419{
2420 mp_exp_t exponent;
2421 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2422 if (*s == '-')
2423 exp->write_c_string("-");
2424 exp->write_c_string("0.");
2425 exp->write_c_string(*s == '-' ? s + 1 : s);
2426 mpfr_free_str(s);
2427 char buf[30];
2428 snprintf(buf, sizeof buf, "E%ld", exponent);
2429 exp->write_c_string(buf);
2430}
2431
2432// Export a floating point number in a constant expression.
2433
2434void
2435Float_expression::do_export(Export* exp) const
2436{
2437 Float_expression::export_float(exp, this->val_);
2438 // A trailing space lets us reliably identify the end of the number.
2439 exp->write_c_string(" ");
2440}
2441
d751bb78 2442// Dump a floating point number to the dump file.
2443
2444void
2445Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2446{
8b1c301d 2447 Float_expression::export_float(ast_dump_context, this->val_);
d751bb78 2448}
2449
e440a328 2450// Make a float expression.
2451
2452Expression*
b13c66cd 2453Expression::make_float(const mpfr_t* val, Type* type, Location location)
e440a328 2454{
2455 return new Float_expression(val, type, location);
2456}
2457
2458// Complex numbers.
2459
2460class Complex_expression : public Expression
2461{
2462 public:
fcbea5e4 2463 Complex_expression(const mpc_t* val, Type* type, Location location)
e440a328 2464 : Expression(EXPRESSION_COMPLEX, location),
2465 type_(type)
2466 {
fcbea5e4 2467 mpc_init2(this->val_, mpc_precision);
2468 mpc_set(this->val_, *val, MPC_RNDNN);
e440a328 2469 }
2470
fcbea5e4 2471 // Write VAL to string dump.
e440a328 2472 static void
fcbea5e4 2473 export_complex(String_dump* exp, const mpc_t val);
e440a328 2474
d751bb78 2475 // Write REAL/IMAG to dump context.
2476 static void
fcbea5e4 2477 dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val);
d751bb78 2478
e440a328 2479 protected:
2480 bool
2481 do_is_constant() const
2482 { return true; }
2483
0e168074 2484 bool
3ae06f68 2485 do_is_static_initializer() const
0e168074 2486 { return true; }
2487
e440a328 2488 bool
0c77715b 2489 do_numeric_constant_value(Numeric_constant* nc) const
2490 {
fcbea5e4 2491 nc->set_complex(this->type_, this->val_);
0c77715b 2492 return true;
2493 }
e440a328 2494
2495 Type*
2496 do_type();
2497
2498 void
2499 do_determine_type(const Type_context*);
2500
2501 void
2502 do_check_types(Gogo*);
2503
2504 Expression*
2505 do_copy()
2506 {
fcbea5e4 2507 return Expression::make_complex(&this->val_, this->type_,
e440a328 2508 this->location());
2509 }
2510
ea664253 2511 Bexpression*
2512 do_get_backend(Translate_context*);
e440a328 2513
2514 void
2515 do_export(Export*) const;
2516
d751bb78 2517 void
2518 do_dump_expression(Ast_dump_context*) const;
abd26de0 2519
e440a328 2520 private:
fcbea5e4 2521 // The complex value.
2522 mpc_t val_;
e440a328 2523 // The type if known.
2524 Type* type_;
2525};
2526
e440a328 2527// Return the current type. If we haven't set the type yet, we return
2528// an abstract complex type.
2529
2530Type*
2531Complex_expression::do_type()
2532{
2533 if (this->type_ == NULL)
2534 this->type_ = Type::make_abstract_complex_type();
2535 return this->type_;
2536}
2537
2538// Set the type of the complex value. Here we may switch from an
2539// abstract type to a real type.
2540
2541void
2542Complex_expression::do_determine_type(const Type_context* context)
2543{
2544 if (this->type_ != NULL && !this->type_->is_abstract())
2545 ;
abd26de0 2546 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 2547 this->type_ = context->type;
2548 else if (!context->may_be_abstract)
48080209 2549 this->type_ = Type::lookup_complex_type("complex128");
e440a328 2550}
2551
e440a328 2552// Check the type of a complex value.
2553
2554void
2555Complex_expression::do_check_types(Gogo*)
2556{
0c77715b 2557 Type* type = this->type_;
2558 if (type == NULL)
e440a328 2559 return;
0c77715b 2560 Numeric_constant nc;
fcbea5e4 2561 nc.set_complex(NULL, this->val_);
0c77715b 2562 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2563 this->set_is_error();
2564}
2565
ea664253 2566// Get the backend representation for a complex constant.
e440a328 2567
ea664253 2568Bexpression*
2569Complex_expression::do_get_backend(Translate_context* context)
e440a328 2570{
12373dd5 2571 if (this->is_error_expression()
2572 || (this->type_ != NULL && this->type_->is_error_type()))
2573 {
2574 go_assert(saw_errors());
2575 return context->gogo()->backend()->error_expression();
2576 }
2577
48c2a53a 2578 Type* resolved_type;
e440a328 2579 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2580 resolved_type = this->type_;
2581 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2582 {
2583 // We are converting to an abstract integer type.
2584 resolved_type = Type::lookup_integer_type("int");
2585 }
2586 else if (this->type_ != NULL && this->type_->float_type() != NULL)
2587 {
2588 // We are converting to an abstract float type.
2589 resolved_type = Type::lookup_float_type("float64");
2590 }
e440a328 2591 else
2592 {
47ae02b7 2593 // If we still have an abstract type here, this is being
e440a328 2594 // used in a constant expression which didn't get reduced. We
2595 // just use complex128 and hope for the best.
48c2a53a 2596 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2597 }
48c2a53a 2598
2599 Numeric_constant nc;
fcbea5e4 2600 nc.set_complex(resolved_type, this->val_);
ea664253 2601 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2602}
2603
2604// Write REAL/IMAG to export data.
2605
2606void
fcbea5e4 2607Complex_expression::export_complex(String_dump* exp, const mpc_t val)
e440a328 2608{
fcbea5e4 2609 if (!mpfr_zero_p(mpc_realref(val)))
e440a328 2610 {
fcbea5e4 2611 Float_expression::export_float(exp, mpc_realref(val));
d1db782d 2612 if (mpfr_sgn(mpc_imagref(val)) >= 0)
e440a328 2613 exp->write_c_string("+");
2614 }
fcbea5e4 2615 Float_expression::export_float(exp, mpc_imagref(val));
e440a328 2616 exp->write_c_string("i");
2617}
2618
2619// Export a complex number in a constant expression.
2620
2621void
2622Complex_expression::do_export(Export* exp) const
2623{
fcbea5e4 2624 Complex_expression::export_complex(exp, this->val_);
e440a328 2625 // A trailing space lets us reliably identify the end of the number.
2626 exp->write_c_string(" ");
2627}
2628
d751bb78 2629// Dump a complex expression to the dump file.
2630
2631void
2632Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2633{
fcbea5e4 2634 Complex_expression::export_complex(ast_dump_context, this->val_);
d751bb78 2635}
2636
e440a328 2637// Make a complex expression.
2638
2639Expression*
fcbea5e4 2640Expression::make_complex(const mpc_t* val, Type* type, Location location)
e440a328 2641{
fcbea5e4 2642 return new Complex_expression(val, type, location);
e440a328 2643}
2644
d5b605df 2645// Find a named object in an expression.
2646
2647class Find_named_object : public Traverse
2648{
2649 public:
2650 Find_named_object(Named_object* no)
2651 : Traverse(traverse_expressions),
2652 no_(no), found_(false)
2653 { }
2654
2655 // Whether we found the object.
2656 bool
2657 found() const
2658 { return this->found_; }
2659
2660 protected:
2661 int
2662 expression(Expression**);
2663
2664 private:
2665 // The object we are looking for.
2666 Named_object* no_;
2667 // Whether we found it.
2668 bool found_;
2669};
2670
e440a328 2671// A reference to a const in an expression.
2672
2673class Const_expression : public Expression
2674{
2675 public:
b13c66cd 2676 Const_expression(Named_object* constant, Location location)
e440a328 2677 : Expression(EXPRESSION_CONST_REFERENCE, location),
13e818f5 2678 constant_(constant), type_(NULL), seen_(false)
e440a328 2679 { }
2680
d5b605df 2681 Named_object*
2682 named_object()
2683 { return this->constant_; }
2684
a7f064d5 2685 // Check that the initializer does not refer to the constant itself.
2686 void
2687 check_for_init_loop();
2688
e440a328 2689 protected:
ba4aedd4 2690 int
2691 do_traverse(Traverse*);
2692
e440a328 2693 Expression*
ceeb4318 2694 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 2695
2696 bool
2697 do_is_constant() const
2698 { return true; }
2699
0e168074 2700 bool
3ae06f68 2701 do_is_static_initializer() const
0e168074 2702 { return true; }
2703
e440a328 2704 bool
0c77715b 2705 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 2706
2707 bool
af6b489a 2708 do_string_constant_value(std::string* val) const;
e440a328 2709
2710 Type*
2711 do_type();
2712
2713 // The type of a const is set by the declaration, not the use.
2714 void
2715 do_determine_type(const Type_context*);
2716
2717 void
2718 do_check_types(Gogo*);
2719
2720 Expression*
2721 do_copy()
2722 { return this; }
2723
ea664253 2724 Bexpression*
2725 do_get_backend(Translate_context* context);
e440a328 2726
2727 // When exporting a reference to a const as part of a const
2728 // expression, we export the value. We ignore the fact that it has
2729 // a name.
2730 void
2731 do_export(Export* exp) const
2732 { this->constant_->const_value()->expr()->export_expression(exp); }
2733
d751bb78 2734 void
2735 do_dump_expression(Ast_dump_context*) const;
2736
e440a328 2737 private:
2738 // The constant.
2739 Named_object* constant_;
2740 // The type of this reference. This is used if the constant has an
2741 // abstract type.
2742 Type* type_;
13e818f5 2743 // Used to prevent infinite recursion when a constant incorrectly
2744 // refers to itself.
2745 mutable bool seen_;
e440a328 2746};
2747
ba4aedd4 2748// Traversal.
2749
2750int
2751Const_expression::do_traverse(Traverse* traverse)
2752{
2753 if (this->type_ != NULL)
2754 return Type::traverse(this->type_, traverse);
2755 return TRAVERSE_CONTINUE;
2756}
2757
e440a328 2758// Lower a constant expression. This is where we convert the
2759// predeclared constant iota into an integer value.
2760
2761Expression*
ceeb4318 2762Const_expression::do_lower(Gogo* gogo, Named_object*,
2763 Statement_inserter*, int iota_value)
e440a328 2764{
2765 if (this->constant_->const_value()->expr()->classification()
2766 == EXPRESSION_IOTA)
2767 {
2768 if (iota_value == -1)
2769 {
631d5788 2770 go_error_at(this->location(),
2771 "iota is only defined in const declarations");
e440a328 2772 iota_value = 0;
2773 }
e67508fa 2774 return Expression::make_integer_ul(iota_value, NULL, this->location());
e440a328 2775 }
2776
2777 // Make sure that the constant itself has been lowered.
2778 gogo->lower_constant(this->constant_);
2779
2780 return this;
2781}
2782
0c77715b 2783// Return a numeric constant value.
e440a328 2784
2785bool
0c77715b 2786Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 2787{
13e818f5 2788 if (this->seen_)
2789 return false;
2790
e440a328 2791 Expression* e = this->constant_->const_value()->expr();
0c77715b 2792
13e818f5 2793 this->seen_ = true;
2794
0c77715b 2795 bool r = e->numeric_constant_value(nc);
e440a328 2796
13e818f5 2797 this->seen_ = false;
2798
e440a328 2799 Type* ctype;
2800 if (this->type_ != NULL)
2801 ctype = this->type_;
2802 else
2803 ctype = this->constant_->const_value()->type();
e440a328 2804 if (r && ctype != NULL)
2805 {
0c77715b 2806 if (!nc->set_type(ctype, false, this->location()))
e440a328 2807 return false;
e440a328 2808 }
e440a328 2809
e440a328 2810 return r;
2811}
2812
af6b489a 2813bool
2814Const_expression::do_string_constant_value(std::string* val) const
2815{
2816 if (this->seen_)
2817 return false;
2818
2819 Expression* e = this->constant_->const_value()->expr();
2820
2821 this->seen_ = true;
2822 bool ok = e->string_constant_value(val);
2823 this->seen_ = false;
2824
2825 return ok;
2826}
2827
e440a328 2828// Return the type of the const reference.
2829
2830Type*
2831Const_expression::do_type()
2832{
2833 if (this->type_ != NULL)
2834 return this->type_;
13e818f5 2835
2f78f012 2836 Named_constant* nc = this->constant_->const_value();
2837
2838 if (this->seen_ || nc->lowering())
13e818f5 2839 {
2840 this->report_error(_("constant refers to itself"));
2841 this->type_ = Type::make_error_type();
2842 return this->type_;
2843 }
2844
2845 this->seen_ = true;
2846
e440a328 2847 Type* ret = nc->type();
13e818f5 2848
e440a328 2849 if (ret != NULL)
13e818f5 2850 {
2851 this->seen_ = false;
2852 return ret;
2853 }
2854
e440a328 2855 // During parsing, a named constant may have a NULL type, but we
2856 // must not return a NULL type here.
13e818f5 2857 ret = nc->expr()->type();
2858
2859 this->seen_ = false;
2860
2861 return ret;
e440a328 2862}
2863
2864// Set the type of the const reference.
2865
2866void
2867Const_expression::do_determine_type(const Type_context* context)
2868{
2869 Type* ctype = this->constant_->const_value()->type();
2870 Type* cetype = (ctype != NULL
2871 ? ctype
2872 : this->constant_->const_value()->expr()->type());
2873 if (ctype != NULL && !ctype->is_abstract())
2874 ;
2875 else if (context->type != NULL
0c77715b 2876 && context->type->is_numeric_type()
2877 && cetype->is_numeric_type())
e440a328 2878 this->type_ = context->type;
2879 else if (context->type != NULL
2880 && context->type->is_string_type()
2881 && cetype->is_string_type())
2882 this->type_ = context->type;
2883 else if (context->type != NULL
2884 && context->type->is_boolean_type()
2885 && cetype->is_boolean_type())
2886 this->type_ = context->type;
2887 else if (!context->may_be_abstract)
2888 {
2889 if (cetype->is_abstract())
2890 cetype = cetype->make_non_abstract_type();
2891 this->type_ = cetype;
2892 }
2893}
2894
a7f064d5 2895// Check for a loop in which the initializer of a constant refers to
2896// the constant itself.
e440a328 2897
2898void
a7f064d5 2899Const_expression::check_for_init_loop()
e440a328 2900{
5c13bd80 2901 if (this->type_ != NULL && this->type_->is_error())
d5b605df 2902 return;
2903
a7f064d5 2904 if (this->seen_)
2905 {
2906 this->report_error(_("constant refers to itself"));
2907 this->type_ = Type::make_error_type();
2908 return;
2909 }
2910
d5b605df 2911 Expression* init = this->constant_->const_value()->expr();
2912 Find_named_object find_named_object(this->constant_);
a7f064d5 2913
2914 this->seen_ = true;
d5b605df 2915 Expression::traverse(&init, &find_named_object);
a7f064d5 2916 this->seen_ = false;
2917
d5b605df 2918 if (find_named_object.found())
2919 {
5c13bd80 2920 if (this->type_ == NULL || !this->type_->is_error())
a7f064d5 2921 {
2922 this->report_error(_("constant refers to itself"));
2923 this->type_ = Type::make_error_type();
2924 }
d5b605df 2925 return;
2926 }
a7f064d5 2927}
2928
2929// Check types of a const reference.
2930
2931void
2932Const_expression::do_check_types(Gogo*)
2933{
5c13bd80 2934 if (this->type_ != NULL && this->type_->is_error())
a7f064d5 2935 return;
2936
2937 this->check_for_init_loop();
d5b605df 2938
0c77715b 2939 // Check that numeric constant fits in type.
2940 if (this->type_ != NULL && this->type_->is_numeric_type())
e440a328 2941 {
0c77715b 2942 Numeric_constant nc;
2943 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
e440a328 2944 {
0c77715b 2945 if (!nc.set_type(this->type_, true, this->location()))
2946 this->set_is_error();
e440a328 2947 }
e440a328 2948 }
2949}
2950
ea664253 2951// Return the backend representation for a const reference.
e440a328 2952
ea664253 2953Bexpression*
2954Const_expression::do_get_backend(Translate_context* context)
e440a328 2955{
12373dd5 2956 if (this->is_error_expression()
2957 || (this->type_ != NULL && this->type_->is_error()))
2958 {
2959 go_assert(saw_errors());
2960 return context->backend()->error_expression();
2961 }
e440a328 2962
2963 // If the type has been set for this expression, but the underlying
2964 // object is an abstract int or float, we try to get the abstract
2965 // value. Otherwise we may lose something in the conversion.
f2de4532 2966 Expression* expr = this->constant_->const_value()->expr();
e440a328 2967 if (this->type_ != NULL
0c77715b 2968 && this->type_->is_numeric_type()
a68492b4 2969 && (this->constant_->const_value()->type() == NULL
2970 || this->constant_->const_value()->type()->is_abstract()))
e440a328 2971 {
0c77715b 2972 Numeric_constant nc;
2973 if (expr->numeric_constant_value(&nc)
2974 && nc.set_type(this->type_, false, this->location()))
e440a328 2975 {
0c77715b 2976 Expression* e = nc.expression(this->location());
ea664253 2977 return e->get_backend(context);
e440a328 2978 }
e440a328 2979 }
2980
2c809f8f 2981 if (this->type_ != NULL)
f2de4532 2982 expr = Expression::make_cast(this->type_, expr, this->location());
ea664253 2983 return expr->get_backend(context);
e440a328 2984}
2985
d751bb78 2986// Dump ast representation for constant expression.
2987
2988void
2989Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2990{
2991 ast_dump_context->ostream() << this->constant_->name();
2992}
2993
e440a328 2994// Make a reference to a constant in an expression.
2995
2996Expression*
2997Expression::make_const_reference(Named_object* constant,
b13c66cd 2998 Location location)
e440a328 2999{
3000 return new Const_expression(constant, location);
3001}
3002
d5b605df 3003// Find a named object in an expression.
3004
3005int
3006Find_named_object::expression(Expression** pexpr)
3007{
3008 switch ((*pexpr)->classification())
3009 {
3010 case Expression::EXPRESSION_CONST_REFERENCE:
a7f064d5 3011 {
3012 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3013 if (ce->named_object() == this->no_)
3014 break;
3015
3016 // We need to check a constant initializer explicitly, as
3017 // loops here will not be caught by the loop checking for
3018 // variable initializers.
3019 ce->check_for_init_loop();
3020
3021 return TRAVERSE_CONTINUE;
3022 }
3023
d5b605df 3024 case Expression::EXPRESSION_VAR_REFERENCE:
3025 if ((*pexpr)->var_expression()->named_object() == this->no_)
3026 break;
3027 return TRAVERSE_CONTINUE;
3028 case Expression::EXPRESSION_FUNC_REFERENCE:
3029 if ((*pexpr)->func_expression()->named_object() == this->no_)
3030 break;
3031 return TRAVERSE_CONTINUE;
3032 default:
3033 return TRAVERSE_CONTINUE;
3034 }
3035 this->found_ = true;
3036 return TRAVERSE_EXIT;
3037}
3038
e440a328 3039// The nil value.
3040
3041class Nil_expression : public Expression
3042{
3043 public:
b13c66cd 3044 Nil_expression(Location location)
e440a328 3045 : Expression(EXPRESSION_NIL, location)
3046 { }
3047
3048 static Expression*
3049 do_import(Import*);
3050
3051 protected:
3052 bool
3053 do_is_constant() const
3054 { return true; }
3055
f9ca30f9 3056 bool
3ae06f68 3057 do_is_static_initializer() const
f9ca30f9 3058 { return true; }
3059
e440a328 3060 Type*
3061 do_type()
3062 { return Type::make_nil_type(); }
3063
3064 void
3065 do_determine_type(const Type_context*)
3066 { }
3067
3068 Expression*
3069 do_copy()
3070 { return this; }
3071
ea664253 3072 Bexpression*
3073 do_get_backend(Translate_context* context)
3074 { return context->backend()->nil_pointer_expression(); }
e440a328 3075
3076 void
3077 do_export(Export* exp) const
3078 { exp->write_c_string("nil"); }
d751bb78 3079
3080 void
3081 do_dump_expression(Ast_dump_context* ast_dump_context) const
3082 { ast_dump_context->ostream() << "nil"; }
e440a328 3083};
3084
3085// Import a nil expression.
3086
3087Expression*
3088Nil_expression::do_import(Import* imp)
3089{
3090 imp->require_c_string("nil");
3091 return Expression::make_nil(imp->location());
3092}
3093
3094// Make a nil expression.
3095
3096Expression*
b13c66cd 3097Expression::make_nil(Location location)
e440a328 3098{
3099 return new Nil_expression(location);
3100}
3101
3102// The value of the predeclared constant iota. This is little more
3103// than a marker. This will be lowered to an integer in
3104// Const_expression::do_lower, which is where we know the value that
3105// it should have.
3106
3107class Iota_expression : public Parser_expression
3108{
3109 public:
b13c66cd 3110 Iota_expression(Location location)
e440a328 3111 : Parser_expression(EXPRESSION_IOTA, location)
3112 { }
3113
3114 protected:
3115 Expression*
ceeb4318 3116 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
c3e6f413 3117 { go_unreachable(); }
e440a328 3118
3119 // There should only ever be one of these.
3120 Expression*
3121 do_copy()
c3e6f413 3122 { go_unreachable(); }
d751bb78 3123
3124 void
3125 do_dump_expression(Ast_dump_context* ast_dump_context) const
3126 { ast_dump_context->ostream() << "iota"; }
e440a328 3127};
3128
3129// Make an iota expression. This is only called for one case: the
3130// value of the predeclared constant iota.
3131
3132Expression*
3133Expression::make_iota()
3134{
b13c66cd 3135 static Iota_expression iota_expression(Linemap::unknown_location());
e440a328 3136 return &iota_expression;
3137}
3138
da244e59 3139// Class Type_conversion_expression.
e440a328 3140
3141// Traversal.
3142
3143int
3144Type_conversion_expression::do_traverse(Traverse* traverse)
3145{
3146 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3147 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3148 return TRAVERSE_EXIT;
3149 return TRAVERSE_CONTINUE;
3150}
3151
3152// Convert to a constant at lowering time.
3153
3154Expression*
ceeb4318 3155Type_conversion_expression::do_lower(Gogo*, Named_object*,
3156 Statement_inserter*, int)
e440a328 3157{
3158 Type* type = this->type_;
3159 Expression* val = this->expr_;
b13c66cd 3160 Location location = this->location();
e440a328 3161
0c77715b 3162 if (type->is_numeric_type())
e440a328 3163 {
0c77715b 3164 Numeric_constant nc;
3165 if (val->numeric_constant_value(&nc))
e440a328 3166 {
0c77715b 3167 if (!nc.set_type(type, true, location))
3168 return Expression::make_error(location);
3169 return nc.expression(location);
e440a328 3170 }
e440a328 3171 }
3172
d7739c9a 3173 // According to the language specification on string conversions
3174 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
3175 // When converting an integer into a string, the string will be a UTF-8
3176 // representation of the integer and integers "outside the range of valid
3177 // Unicode code points are converted to '\uFFFD'."
3178 if (type->is_string_type())
3179 {
3180 Numeric_constant nc;
3181 if (val->numeric_constant_value(&nc) && nc.is_int())
3182 {
3183 // An integer value doesn't fit in the Unicode code point range if it
3184 // overflows the Go "int" type or is negative.
3185 unsigned long ul;
3186 if (!nc.set_type(Type::lookup_integer_type("int"), false, location)
3187 || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE)
3188 return Expression::make_string("\ufffd", location);
3189 }
3190 }
3191
55072f2b 3192 if (type->is_slice_type())
e440a328 3193 {
3194 Type* element_type = type->array_type()->element_type()->forwarded();
60963afd 3195 bool is_byte = (element_type->integer_type() != NULL
3196 && element_type->integer_type()->is_byte());
3197 bool is_rune = (element_type->integer_type() != NULL
3198 && element_type->integer_type()->is_rune());
3199 if (is_byte || is_rune)
e440a328 3200 {
3201 std::string s;
3202 if (val->string_constant_value(&s))
3203 {
3204 Expression_list* vals = new Expression_list();
3205 if (is_byte)
3206 {
3207 for (std::string::const_iterator p = s.begin();
3208 p != s.end();
3209 p++)
3210 {
e67508fa 3211 unsigned char c = static_cast<unsigned char>(*p);
3212 vals->push_back(Expression::make_integer_ul(c,
3213 element_type,
3214 location));
e440a328 3215 }
3216 }
3217 else
3218 {
3219 const char *p = s.data();
3220 const char *pend = s.data() + s.length();
3221 while (p < pend)
3222 {
3223 unsigned int c;
3224 int adv = Lex::fetch_char(p, &c);
3225 if (adv == 0)
3226 {
631d5788 3227 go_warning_at(this->location(), 0,
e440a328 3228 "invalid UTF-8 encoding");
3229 adv = 1;
3230 }
3231 p += adv;
e67508fa 3232 vals->push_back(Expression::make_integer_ul(c,
3233 element_type,
3234 location));
e440a328 3235 }
3236 }
3237
3238 return Expression::make_slice_composite_literal(type, vals,
3239 location);
3240 }
3241 }
3242 }
3243
3244 return this;
3245}
3246
35a54f17 3247// Flatten a type conversion by using a temporary variable for the slice
3248// in slice to string conversions.
3249
3250Expression*
3251Type_conversion_expression::do_flatten(Gogo*, Named_object*,
3252 Statement_inserter* inserter)
3253{
5bf8be8b 3254 if (this->type()->is_error_type() || this->expr_->is_error_expression())
3255 {
3256 go_assert(saw_errors());
3257 return Expression::make_error(this->location());
3258 }
3259
2c809f8f 3260 if (((this->type()->is_string_type()
3261 && this->expr_->type()->is_slice_type())
8ba8cc87 3262 || this->expr_->type()->interface_type() != NULL)
35a54f17 3263 && !this->expr_->is_variable())
3264 {
3265 Temporary_statement* temp =
3266 Statement::make_temporary(NULL, this->expr_, this->location());
3267 inserter->insert(temp);
3268 this->expr_ = Expression::make_temporary_reference(temp, this->location());
3269 }
3270 return this;
3271}
3272
1ca01a59 3273// Return whether a type conversion is a constant.
3274
3275bool
3276Type_conversion_expression::do_is_constant() const
3277{
3278 if (!this->expr_->is_constant())
3279 return false;
3280
3281 // A conversion to a type that may not be used as a constant is not
3282 // a constant. For example, []byte(nil).
3283 Type* type = this->type_;
3284 if (type->integer_type() == NULL
3285 && type->float_type() == NULL
3286 && type->complex_type() == NULL
3287 && !type->is_boolean_type()
3288 && !type->is_string_type())
3289 return false;
3290
3291 return true;
3292}
3293
3ae06f68 3294// Return whether a type conversion can be used in a constant
3295// initializer.
0e168074 3296
3297bool
3ae06f68 3298Type_conversion_expression::do_is_static_initializer() const
0e168074 3299{
3300 Type* type = this->type_;
3301 Type* expr_type = this->expr_->type();
3302
3303 if (type->interface_type() != NULL
3304 || expr_type->interface_type() != NULL)
3305 return false;
3306
3ae06f68 3307 if (!this->expr_->is_static_initializer())
0e168074 3308 return false;
3309
3310 if (Type::are_identical(type, expr_type, false, NULL))
3311 return true;
3312
03118c21 3313 if (type->is_string_type() && expr_type->is_string_type())
3314 return true;
3315
3316 if ((type->is_numeric_type()
3317 || type->is_boolean_type()
3318 || type->points_to() != NULL)
3319 && (expr_type->is_numeric_type()
3320 || expr_type->is_boolean_type()
3321 || expr_type->points_to() != NULL))
3322 return true;
3323
3324 return false;
0e168074 3325}
3326
0c77715b 3327// Return the constant numeric value if there is one.
e440a328 3328
3329bool
0c77715b 3330Type_conversion_expression::do_numeric_constant_value(
3331 Numeric_constant* nc) const
e440a328 3332{
0c77715b 3333 if (!this->type_->is_numeric_type())
e440a328 3334 return false;
0c77715b 3335 if (!this->expr_->numeric_constant_value(nc))
e440a328 3336 return false;
0c77715b 3337 return nc->set_type(this->type_, false, this->location());
e440a328 3338}
3339
3340// Return the constant string value if there is one.
3341
3342bool
3343Type_conversion_expression::do_string_constant_value(std::string* val) const
3344{
3345 if (this->type_->is_string_type()
3346 && this->expr_->type()->integer_type() != NULL)
3347 {
0c77715b 3348 Numeric_constant nc;
3349 if (this->expr_->numeric_constant_value(&nc))
e440a328 3350 {
0c77715b 3351 unsigned long ival;
3352 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
e440a328 3353 {
0c77715b 3354 val->clear();
3355 Lex::append_char(ival, true, val, this->location());
e440a328 3356 return true;
3357 }
3358 }
e440a328 3359 }
3360
3361 // FIXME: Could handle conversion from const []int here.
3362
3363 return false;
3364}
3365
da244e59 3366// Determine the resulting type of the conversion.
3367
3368void
3369Type_conversion_expression::do_determine_type(const Type_context*)
3370{
3371 Type_context subcontext(this->type_, false);
3372 this->expr_->determine_type(&subcontext);
3373}
3374
e440a328 3375// Check that types are convertible.
3376
3377void
3378Type_conversion_expression::do_check_types(Gogo*)
3379{
3380 Type* type = this->type_;
3381 Type* expr_type = this->expr_->type();
3382 std::string reason;
3383
5c13bd80 3384 if (type->is_error() || expr_type->is_error())
842f6425 3385 {
842f6425 3386 this->set_is_error();
3387 return;
3388 }
3389
e440a328 3390 if (this->may_convert_function_types_
3391 && type->function_type() != NULL
3392 && expr_type->function_type() != NULL)
3393 return;
3394
3395 if (Type::are_convertible(type, expr_type, &reason))
3396 return;
3397
631d5788 3398 go_error_at(this->location(), "%s", reason.c_str());
e440a328 3399 this->set_is_error();
3400}
3401
ea664253 3402// Get the backend representation for a type conversion.
e440a328 3403
ea664253 3404Bexpression*
3405Type_conversion_expression::do_get_backend(Translate_context* context)
e440a328 3406{
e440a328 3407 Type* type = this->type_;
3408 Type* expr_type = this->expr_->type();
2c809f8f 3409
3410 Gogo* gogo = context->gogo();
3411 Btype* btype = type->get_backend(gogo);
2c809f8f 3412 Location loc = this->location();
3413
3414 if (Type::are_identical(type, expr_type, false, NULL))
859cdc93 3415 {
3416 Bexpression* bexpr = this->expr_->get_backend(context);
3417 return gogo->backend()->convert_expression(btype, bexpr, loc);
3418 }
2c809f8f 3419 else if (type->interface_type() != NULL
3420 || expr_type->interface_type() != NULL)
e440a328 3421 {
2c809f8f 3422 Expression* conversion =
3423 Expression::convert_for_assignment(gogo, type, this->expr_,
3424 this->location());
ea664253 3425 return conversion->get_backend(context);
e440a328 3426 }
3427 else if (type->is_string_type()
3428 && expr_type->integer_type() != NULL)
3429 {
2c809f8f 3430 mpz_t intval;
3431 Numeric_constant nc;
3432 if (this->expr_->numeric_constant_value(&nc)
3433 && nc.to_int(&intval)
3434 && mpz_fits_ushort_p(intval))
e440a328 3435 {
e440a328 3436 std::string s;
2c809f8f 3437 Lex::append_char(mpz_get_ui(intval), true, &s, loc);
3438 mpz_clear(intval);
3439 Expression* se = Expression::make_string(s, loc);
ea664253 3440 return se->get_backend(context);
e440a328 3441 }
3442
f16ab008 3443 Expression* i2s_expr =
736a16ba 3444 Runtime::make_call(Runtime::INTSTRING, loc, 2,
3445 Expression::make_nil(loc), this->expr_);
ea664253 3446 return Expression::make_cast(type, i2s_expr, loc)->get_backend(context);
e440a328 3447 }
55072f2b 3448 else if (type->is_string_type() && expr_type->is_slice_type())
e440a328 3449 {
55072f2b 3450 Array_type* a = expr_type->array_type();
e440a328 3451 Type* e = a->element_type()->forwarded();
c484d925 3452 go_assert(e->integer_type() != NULL);
35a54f17 3453 go_assert(this->expr_->is_variable());
3454
3455 Runtime::Function code;
60963afd 3456 if (e->integer_type()->is_byte())
736a16ba 3457 code = Runtime::SLICEBYTETOSTRING;
e440a328 3458 else
35a54f17 3459 {
3460 go_assert(e->integer_type()->is_rune());
736a16ba 3461 code = Runtime::SLICERUNETOSTRING;
35a54f17 3462 }
736a16ba 3463 return Runtime::make_call(code, loc, 2, Expression::make_nil(loc),
3464 this->expr_)->get_backend(context);
e440a328 3465 }
411eb89e 3466 else if (type->is_slice_type() && expr_type->is_string_type())
e440a328 3467 {
3468 Type* e = type->array_type()->element_type()->forwarded();
c484d925 3469 go_assert(e->integer_type() != NULL);
6c252e42 3470
2c809f8f 3471 Runtime::Function code;
60963afd 3472 if (e->integer_type()->is_byte())
736a16ba 3473 code = Runtime::STRINGTOSLICEBYTE;
e440a328 3474 else
3475 {
60963afd 3476 go_assert(e->integer_type()->is_rune());
736a16ba 3477 code = Runtime::STRINGTOSLICERUNE;
e440a328 3478 }
736a16ba 3479 Expression* s2a = Runtime::make_call(code, loc, 2,
3480 Expression::make_nil(loc),
3481 this->expr_);
ea664253 3482 return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context);
2c809f8f 3483 }
3484 else if (type->is_numeric_type())
3485 {
3486 go_assert(Type::are_convertible(type, expr_type, NULL));
859cdc93 3487 Bexpression* bexpr = this->expr_->get_backend(context);
ea664253 3488 return gogo->backend()->convert_expression(btype, bexpr, loc);
e440a328 3489 }
3490 else if ((type->is_unsafe_pointer_type()
2c809f8f 3491 && (expr_type->points_to() != NULL
3492 || expr_type->integer_type()))
3493 || (expr_type->is_unsafe_pointer_type()
3494 && type->points_to() != NULL)
3495 || (this->may_convert_function_types_
3496 && type->function_type() != NULL
3497 && expr_type->function_type() != NULL))
859cdc93 3498 {
3499 Bexpression* bexpr = this->expr_->get_backend(context);
3500 return gogo->backend()->convert_expression(btype, bexpr, loc);
3501 }
e440a328 3502 else
2c809f8f 3503 {
3504 Expression* conversion =
3505 Expression::convert_for_assignment(gogo, type, this->expr_, loc);
ea664253 3506 return conversion->get_backend(context);
2c809f8f 3507 }
e440a328 3508}
3509
3510// Output a type conversion in a constant expression.
3511
3512void
3513Type_conversion_expression::do_export(Export* exp) const
3514{
3515 exp->write_c_string("convert(");
3516 exp->write_type(this->type_);
3517 exp->write_c_string(", ");
3518 this->expr_->export_expression(exp);
3519 exp->write_c_string(")");
3520}
3521
3522// Import a type conversion or a struct construction.
3523
3524Expression*
3525Type_conversion_expression::do_import(Import* imp)
3526{
3527 imp->require_c_string("convert(");
3528 Type* type = imp->read_type();
3529 imp->require_c_string(", ");
3530 Expression* val = Expression::import_expression(imp);
3531 imp->require_c_string(")");
3532 return Expression::make_cast(type, val, imp->location());
3533}
3534
d751bb78 3535// Dump ast representation for a type conversion expression.
3536
3537void
3538Type_conversion_expression::do_dump_expression(
3539 Ast_dump_context* ast_dump_context) const
3540{
3541 ast_dump_context->dump_type(this->type_);
3542 ast_dump_context->ostream() << "(";
3543 ast_dump_context->dump_expression(this->expr_);
3544 ast_dump_context->ostream() << ") ";
3545}
3546
e440a328 3547// Make a type cast expression.
3548
3549Expression*
b13c66cd 3550Expression::make_cast(Type* type, Expression* val, Location location)
e440a328 3551{
3552 if (type->is_error_type() || val->is_error_expression())
3553 return Expression::make_error(location);
3554 return new Type_conversion_expression(type, val, location);
3555}
3556
98f62f7a 3557// Class Unsafe_type_conversion_expression.
9581e91d 3558
3559// Traversal.
3560
3561int
3562Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3563{
3564 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3565 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3566 return TRAVERSE_EXIT;
3567 return TRAVERSE_CONTINUE;
3568}
3569
3ae06f68 3570// Return whether an unsafe type conversion can be used as a constant
3571// initializer.
aa5ae575 3572
3573bool
3ae06f68 3574Unsafe_type_conversion_expression::do_is_static_initializer() const
aa5ae575 3575{
3576 Type* type = this->type_;
3577 Type* expr_type = this->expr_->type();
3578
3579 if (type->interface_type() != NULL
3580 || expr_type->interface_type() != NULL)
3581 return false;
3582
3ae06f68 3583 if (!this->expr_->is_static_initializer())
aa5ae575 3584 return false;
3585
3586 if (Type::are_convertible(type, expr_type, NULL))
3587 return true;
3588
03118c21 3589 if (type->is_string_type() && expr_type->is_string_type())
3590 return true;
3591
3592 if ((type->is_numeric_type()
3593 || type->is_boolean_type()
3594 || type->points_to() != NULL)
3595 && (expr_type->is_numeric_type()
3596 || expr_type->is_boolean_type()
3597 || expr_type->points_to() != NULL))
3598 return true;
3599
3600 return false;
aa5ae575 3601}
3602
9581e91d 3603// Convert to backend representation.
3604
ea664253 3605Bexpression*
3606Unsafe_type_conversion_expression::do_get_backend(Translate_context* context)
9581e91d 3607{
3608 // We are only called for a limited number of cases.
3609
3610 Type* t = this->type_;
3611 Type* et = this->expr_->type();
5c4802f1 3612
3613 if (t->is_error_type()
3614 || this->expr_->is_error_expression()
3615 || et->is_error_type())
3616 {
3617 go_assert(saw_errors());
3618 return context->backend()->error_expression();
3619 }
3620
2c809f8f 3621 if (t->array_type() != NULL)
3622 go_assert(et->array_type() != NULL
3623 && t->is_slice_type() == et->is_slice_type());
3624 else if (t->struct_type() != NULL)
9581e91d 3625 {
2c809f8f 3626 if (t->named_type() != NULL
3627 && et->named_type() != NULL
3628 && !Type::are_convertible(t, et, NULL))
3629 {
3630 go_assert(saw_errors());
ea664253 3631 return context->backend()->error_expression();
2c809f8f 3632 }
3633
3634 go_assert(et->struct_type() != NULL
3635 && Type::are_convertible(t, et, NULL));
3636 }
3637 else if (t->map_type() != NULL)
c484d925 3638 go_assert(et->map_type() != NULL);
9581e91d 3639 else if (t->channel_type() != NULL)
c484d925 3640 go_assert(et->channel_type() != NULL);
09ea332d 3641 else if (t->points_to() != NULL)
2c809f8f 3642 go_assert(et->points_to() != NULL
3643 || et->channel_type() != NULL
3644 || et->map_type() != NULL
3645 || et->function_type() != NULL
132ed071 3646 || et->integer_type() != NULL
2c809f8f 3647 || et->is_nil_type());
9581e91d 3648 else if (et->is_unsafe_pointer_type())
c484d925 3649 go_assert(t->points_to() != NULL);
2c809f8f 3650 else if (t->interface_type() != NULL)
9581e91d 3651 {
2c809f8f 3652 bool empty_iface = t->interface_type()->is_empty();
c484d925 3653 go_assert(et->interface_type() != NULL
2c809f8f 3654 && et->interface_type()->is_empty() == empty_iface);
9581e91d 3655 }
588e3cf9 3656 else if (t->integer_type() != NULL)
2c809f8f 3657 go_assert(et->is_boolean_type()
3658 || et->integer_type() != NULL
3659 || et->function_type() != NULL
3660 || et->points_to() != NULL
3661 || et->map_type() != NULL
8ba8cc87 3662 || et->channel_type() != NULL
3663 || et->is_nil_type());
cd39797e 3664 else if (t->function_type() != NULL)
3665 go_assert(et->points_to() != NULL);
9581e91d 3666 else
c3e6f413 3667 go_unreachable();
9581e91d 3668
2c809f8f 3669 Gogo* gogo = context->gogo();
3670 Btype* btype = t->get_backend(gogo);
ea664253 3671 Bexpression* bexpr = this->expr_->get_backend(context);
2c809f8f 3672 Location loc = this->location();
ea664253 3673 return gogo->backend()->convert_expression(btype, bexpr, loc);
9581e91d 3674}
3675
d751bb78 3676// Dump ast representation for an unsafe type conversion expression.
3677
3678void
3679Unsafe_type_conversion_expression::do_dump_expression(
3680 Ast_dump_context* ast_dump_context) const
3681{
3682 ast_dump_context->dump_type(this->type_);
3683 ast_dump_context->ostream() << "(";
3684 ast_dump_context->dump_expression(this->expr_);
3685 ast_dump_context->ostream() << ") ";
3686}
3687
9581e91d 3688// Make an unsafe type conversion expression.
3689
3690Expression*
3691Expression::make_unsafe_cast(Type* type, Expression* expr,
b13c66cd 3692 Location location)
9581e91d 3693{
3694 return new Unsafe_type_conversion_expression(type, expr, location);
3695}
3696
76f85fd6 3697// Class Unary_expression.
e440a328 3698
03118c21 3699// Call the address_taken method of the operand if needed. This is
3700// called after escape analysis but before inserting write barriers.
3701
3702void
3703Unary_expression::check_operand_address_taken(Gogo* gogo)
3704{
3705 if (this->op_ != OPERATOR_AND)
3706 return;
3707
3708 // If this->escapes_ is false at this point, then it was set to
3709 // false by an explicit call to set_does_not_escape, and the value
3710 // does not escape. If this->escapes_ is true, we may be able to
3711 // set it to false if taking the address of a variable that does not
3712 // escape.
3713 Node* n = Node::make_node(this);
3714 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
3715 this->escapes_ = false;
3716
3717 // When compiling the runtime, the address operator does not cause
3718 // local variables to escape. When escape analysis becomes the
3719 // default, this should be changed to make it an error if we have an
3720 // address operator that escapes.
3721 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
3722 this->escapes_ = false;
3723
3724 Named_object* var = NULL;
3725 if (this->expr_->var_expression() != NULL)
3726 var = this->expr_->var_expression()->named_object();
3727 else if (this->expr_->enclosed_var_expression() != NULL)
3728 var = this->expr_->enclosed_var_expression()->variable();
3729
3730 if (this->escapes_ && var != NULL)
3731 {
3732 if (var->is_variable())
3733 this->escapes_ = var->var_value()->escapes();
3734 if (var->is_result_variable())
3735 this->escapes_ = var->result_var_value()->escapes();
3736 }
3737
3738 this->expr_->address_taken(this->escapes_);
3739}
3740
e440a328 3741// If we are taking the address of a composite literal, and the
2c809f8f 3742// contents are not constant, then we want to make a heap expression
e440a328 3743// instead.
3744
3745Expression*
ceeb4318 3746Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 3747{
b13c66cd 3748 Location loc = this->location();
e440a328 3749 Operator op = this->op_;
3750 Expression* expr = this->expr_;
3751
3752 if (op == OPERATOR_MULT && expr->is_type_expression())
3753 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3754
3755 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3756 // moving x to the heap. FIXME: Is it worth doing a real escape
3757 // analysis here? This case is found in math/unsafe.go and is
3758 // therefore worth special casing.
3759 if (op == OPERATOR_MULT)
3760 {
3761 Expression* e = expr;
3762 while (e->classification() == EXPRESSION_CONVERSION)
3763 {
3764 Type_conversion_expression* te
3765 = static_cast<Type_conversion_expression*>(e);
3766 e = te->expr();
3767 }
3768
3769 if (e->classification() == EXPRESSION_UNARY)
3770 {
3771 Unary_expression* ue = static_cast<Unary_expression*>(e);
3772 if (ue->op_ == OPERATOR_AND)
3773 {
3774 if (e == expr)
3775 {
3776 // *&x == x.
f4dea966 3777 if (!ue->expr_->is_addressable() && !ue->create_temp_)
3778 {
631d5788 3779 go_error_at(ue->location(),
3780 "invalid operand for unary %<&%>");
f4dea966 3781 this->set_is_error();
3782 }
e440a328 3783 return ue->expr_;
3784 }
3785 ue->set_does_not_escape();
3786 }
3787 }
3788 }
3789
55661ce9 3790 // Catching an invalid indirection of unsafe.Pointer here avoid
3791 // having to deal with TYPE_VOID in other places.
3792 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3793 {
631d5788 3794 go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
55661ce9 3795 return Expression::make_error(this->location());
3796 }
3797
d9f3743a 3798 // Check for an invalid pointer dereference. We need to do this
3799 // here because Unary_expression::do_type will return an error type
3800 // in this case. That can cause code to appear erroneous, and
3801 // therefore disappear at lowering time, without any error message.
3802 if (op == OPERATOR_MULT && expr->type()->points_to() == NULL)
3803 {
3804 this->report_error(_("expected pointer"));
3805 return Expression::make_error(this->location());
3806 }
3807
59a401fe 3808 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
e440a328 3809 {
0c77715b 3810 Numeric_constant nc;
3811 if (expr->numeric_constant_value(&nc))
e440a328 3812 {
0c77715b 3813 Numeric_constant result;
af7a5274 3814 bool issued_error;
3815 if (Unary_expression::eval_constant(op, &nc, loc, &result,
3816 &issued_error))
0c77715b 3817 return result.expression(loc);
af7a5274 3818 else if (issued_error)
3819 return Expression::make_error(this->location());
e440a328 3820 }
3821 }
3822
3823 return this;
3824}
3825
f9ca30f9 3826// Flatten expression if a nil check must be performed and create temporary
3827// variables if necessary.
3828
3829Expression*
3830Unary_expression::do_flatten(Gogo* gogo, Named_object*,
3831 Statement_inserter* inserter)
3832{
5bf8be8b 3833 if (this->is_error_expression()
3834 || this->expr_->is_error_expression()
3835 || this->expr_->type()->is_error_type())
3836 {
3837 go_assert(saw_errors());
3838 return Expression::make_error(this->location());
3839 }
f4dea966 3840
f9ca30f9 3841 Location location = this->location();
3842 if (this->op_ == OPERATOR_MULT
3843 && !this->expr_->is_variable())
3844 {
3845 go_assert(this->expr_->type()->points_to() != NULL);
f614ea8b 3846 switch (this->requires_nil_check(gogo))
f9ca30f9 3847 {
f614ea8b 3848 case NIL_CHECK_ERROR_ENCOUNTERED:
2a305b85 3849 {
3850 go_assert(saw_errors());
3851 return Expression::make_error(this->location());
3852 }
f614ea8b 3853 case NIL_CHECK_NOT_NEEDED:
3854 break;
3855 case NIL_CHECK_NEEDED:
3856 this->create_temp_ = true;
3857 break;
3858 case NIL_CHECK_DEFAULT:
3859 go_unreachable();
f9ca30f9 3860 }
3861 }
3862
3863 if (this->create_temp_ && !this->expr_->is_variable())
3864 {
3865 Temporary_statement* temp =
3866 Statement::make_temporary(NULL, this->expr_, location);
3867 inserter->insert(temp);
3868 this->expr_ = Expression::make_temporary_reference(temp, location);
3869 }
3870
3871 return this;
3872}
3873
e440a328 3874// Return whether a unary expression is a constant.
3875
3876bool
3877Unary_expression::do_is_constant() const
3878{
3879 if (this->op_ == OPERATOR_MULT)
3880 {
3881 // Indirecting through a pointer is only constant if the object
3882 // to which the expression points is constant, but we currently
3883 // have no way to determine that.
3884 return false;
3885 }
3886 else if (this->op_ == OPERATOR_AND)
3887 {
3888 // Taking the address of a variable is constant if it is a
f9ca30f9 3889 // global variable, not constant otherwise. In other cases taking the
3890 // address is probably not a constant.
e440a328 3891 Var_expression* ve = this->expr_->var_expression();
3892 if (ve != NULL)
3893 {
3894 Named_object* no = ve->named_object();
3895 return no->is_variable() && no->var_value()->is_global();
3896 }
3897 return false;
3898 }
3899 else
3900 return this->expr_->is_constant();
3901}
3902
3ae06f68 3903// Return whether a unary expression can be used as a constant
3904// initializer.
3905
3906bool
3907Unary_expression::do_is_static_initializer() const
3908{
3909 if (this->op_ == OPERATOR_MULT)
3910 return false;
3911 else if (this->op_ == OPERATOR_AND)
de048538 3912 return Unary_expression::base_is_static_initializer(this->expr_);
3913 else
3914 return this->expr_->is_static_initializer();
3915}
3ae06f68 3916
de048538 3917// Return whether the address of EXPR can be used as a static
3918// initializer.
3ae06f68 3919
de048538 3920bool
3921Unary_expression::base_is_static_initializer(Expression* expr)
3922{
3923 // The address of a field reference can be a static initializer if
3924 // the base can be a static initializer.
3925 Field_reference_expression* fre = expr->field_reference_expression();
3926 if (fre != NULL)
3927 return Unary_expression::base_is_static_initializer(fre->expr());
3928
3929 // The address of an index expression can be a static initializer if
3930 // the base can be a static initializer and the index is constant.
3931 Array_index_expression* aind = expr->array_index_expression();
3932 if (aind != NULL)
3933 return (aind->end() == NULL
3934 && aind->start()->is_constant()
3935 && Unary_expression::base_is_static_initializer(aind->array()));
3936
3937 // The address of a global variable can be a static initializer.
3938 Var_expression* ve = expr->var_expression();
3939 if (ve != NULL)
3940 {
3941 Named_object* no = ve->named_object();
3942 return no->is_variable() && no->var_value()->is_global();
3943 }
3944
3945 // The address of a composite literal can be used as a static
3946 // initializer if the composite literal is itself usable as a
3947 // static initializer.
3948 if (expr->is_composite_literal() && expr->is_static_initializer())
3949 return true;
3ae06f68 3950
de048538 3951 // The address of a string constant can be used as a static
3952 // initializer. This can not be written in Go itself but this is
3953 // used when building a type descriptor.
3954 if (expr->string_expression() != NULL)
3955 return true;
3956
3957 return false;
3ae06f68 3958}
3959
f614ea8b 3960// Return whether this dereference expression requires an explicit nil
3961// check. If we are dereferencing the pointer to a large struct
3962// (greater than the specified size threshold), we need to check for
3963// nil. We don't bother to check for small structs because we expect
3964// the system to crash on a nil pointer dereference. However, if we
3965// know the address of this expression is being taken, we must always
3966// check for nil.
3967Unary_expression::Nil_check_classification
3968Unary_expression::requires_nil_check(Gogo* gogo)
3969{
3970 go_assert(this->op_ == OPERATOR_MULT);
3971 go_assert(this->expr_->type()->points_to() != NULL);
3972
3973 if (this->issue_nil_check_ == NIL_CHECK_NEEDED)
3974 return NIL_CHECK_NEEDED;
3975 else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED)
3976 return NIL_CHECK_NOT_NEEDED;
3977
3978 Type* ptype = this->expr_->type()->points_to();
3979 int64_t type_size = -1;
3980 if (!ptype->is_void_type())
3981 {
3982 bool ok = ptype->backend_type_size(gogo, &type_size);
3983 if (!ok)
3984 return NIL_CHECK_ERROR_ENCOUNTERED;
3985 }
3986
3987 int64_t size_cutoff = gogo->nil_check_size_threshold();
3988 if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff))
3989 this->issue_nil_check_ = NIL_CHECK_NEEDED;
3990 else
3991 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
3992 return this->issue_nil_check_;
3993}
3994
0c77715b 3995// Apply unary opcode OP to UNC, setting NC. Return true if this
af7a5274 3996// could be done, false if not. On overflow, issues an error and sets
3997// *ISSUED_ERROR.
e440a328 3998
3999bool
0c77715b 4000Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
af7a5274 4001 Location location, Numeric_constant* nc,
4002 bool* issued_error)
e440a328 4003{
af7a5274 4004 *issued_error = false;
e440a328 4005 switch (op)
4006 {
4007 case OPERATOR_PLUS:
0c77715b 4008 *nc = *unc;
e440a328 4009 return true;
0c77715b 4010
e440a328 4011 case OPERATOR_MINUS:
0c77715b 4012 if (unc->is_int() || unc->is_rune())
4013 break;
4014 else if (unc->is_float())
4015 {
4016 mpfr_t uval;
4017 unc->get_float(&uval);
4018 mpfr_t val;
4019 mpfr_init(val);
4020 mpfr_neg(val, uval, GMP_RNDN);
4021 nc->set_float(unc->type(), val);
4022 mpfr_clear(uval);
4023 mpfr_clear(val);
4024 return true;
4025 }
4026 else if (unc->is_complex())
4027 {
fcbea5e4 4028 mpc_t uval;
4029 unc->get_complex(&uval);
4030 mpc_t val;
4031 mpc_init2(val, mpc_precision);
4032 mpc_neg(val, uval, MPC_RNDNN);
4033 nc->set_complex(unc->type(), val);
4034 mpc_clear(uval);
4035 mpc_clear(val);
0c77715b 4036 return true;
4037 }
e440a328 4038 else
0c77715b 4039 go_unreachable();
e440a328 4040
0c77715b 4041 case OPERATOR_XOR:
4042 break;
68448d53 4043
59a401fe 4044 case OPERATOR_NOT:
e440a328 4045 case OPERATOR_AND:
4046 case OPERATOR_MULT:
4047 return false;
0c77715b 4048
e440a328 4049 default:
c3e6f413 4050 go_unreachable();
e440a328 4051 }
e440a328 4052
0c77715b 4053 if (!unc->is_int() && !unc->is_rune())
4054 return false;
4055
4056 mpz_t uval;
8387e1df 4057 if (unc->is_rune())
4058 unc->get_rune(&uval);
4059 else
4060 unc->get_int(&uval);
0c77715b 4061 mpz_t val;
4062 mpz_init(val);
e440a328 4063
e440a328 4064 switch (op)
4065 {
e440a328 4066 case OPERATOR_MINUS:
0c77715b 4067 mpz_neg(val, uval);
4068 break;
4069
e440a328 4070 case OPERATOR_NOT:
0c77715b 4071 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4072 break;
4073
e440a328 4074 case OPERATOR_XOR:
0c77715b 4075 {
4076 Type* utype = unc->type();
4077 if (utype->integer_type() == NULL
4078 || utype->integer_type()->is_abstract())
4079 mpz_com(val, uval);
4080 else
4081 {
4082 // The number of HOST_WIDE_INTs that it takes to represent
4083 // UVAL.
4084 size_t count = ((mpz_sizeinbase(uval, 2)
4085 + HOST_BITS_PER_WIDE_INT
4086 - 1)
4087 / HOST_BITS_PER_WIDE_INT);
e440a328 4088
0c77715b 4089 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4090 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4091
4092 size_t obits = utype->integer_type()->bits();
4093
4094 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
4095 {
4096 mpz_t adj;
4097 mpz_init_set_ui(adj, 1);
4098 mpz_mul_2exp(adj, adj, obits);
4099 mpz_add(uval, uval, adj);
4100 mpz_clear(adj);
4101 }
4102
4103 size_t ecount;
4104 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
4105 go_assert(ecount <= count);
4106
4107 // Trim down to the number of words required by the type.
4108 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4109 / HOST_BITS_PER_WIDE_INT);
4110 go_assert(ocount <= count);
4111
4112 for (size_t i = 0; i < ocount; ++i)
4113 phwi[i] = ~phwi[i];
4114
4115 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4116 if (clearbits != 0)
4117 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4118 >> clearbits);
4119
4120 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4121
4122 if (!utype->integer_type()->is_unsigned()
4123 && mpz_tstbit(val, obits - 1))
4124 {
4125 mpz_t adj;
4126 mpz_init_set_ui(adj, 1);
4127 mpz_mul_2exp(adj, adj, obits);
4128 mpz_sub(val, val, adj);
4129 mpz_clear(adj);
4130 }
4131
4132 delete[] phwi;
4133 }
4134 }
4135 break;
e440a328 4136
e440a328 4137 default:
c3e6f413 4138 go_unreachable();
e440a328 4139 }
e440a328 4140
0c77715b 4141 if (unc->is_rune())
4142 nc->set_rune(NULL, val);
e440a328 4143 else
0c77715b 4144 nc->set_int(NULL, val);
e440a328 4145
0c77715b 4146 mpz_clear(uval);
4147 mpz_clear(val);
e440a328 4148
af7a5274 4149 if (!nc->set_type(unc->type(), true, location))
4150 {
4151 *issued_error = true;
4152 return false;
4153 }
4154 return true;
e440a328 4155}
4156
0c77715b 4157// Return the integral constant value of a unary expression, if it has one.
e440a328 4158
4159bool
0c77715b 4160Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 4161{
0c77715b 4162 Numeric_constant unc;
4163 if (!this->expr_->numeric_constant_value(&unc))
4164 return false;
af7a5274 4165 bool issued_error;
0c77715b 4166 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
af7a5274 4167 nc, &issued_error);
e440a328 4168}
4169
4170// Return the type of a unary expression.
4171
4172Type*
4173Unary_expression::do_type()
4174{
4175 switch (this->op_)
4176 {
4177 case OPERATOR_PLUS:
4178 case OPERATOR_MINUS:
4179 case OPERATOR_NOT:
4180 case OPERATOR_XOR:
4181 return this->expr_->type();
4182
4183 case OPERATOR_AND:
4184 return Type::make_pointer_type(this->expr_->type());
4185
4186 case OPERATOR_MULT:
4187 {
4188 Type* subtype = this->expr_->type();
4189 Type* points_to = subtype->points_to();
4190 if (points_to == NULL)
4191 return Type::make_error_type();
4192 return points_to;
4193 }
4194
4195 default:
c3e6f413 4196 go_unreachable();
e440a328 4197 }
4198}
4199
4200// Determine abstract types for a unary expression.
4201
4202void
4203Unary_expression::do_determine_type(const Type_context* context)
4204{
4205 switch (this->op_)
4206 {
4207 case OPERATOR_PLUS:
4208 case OPERATOR_MINUS:
4209 case OPERATOR_NOT:
4210 case OPERATOR_XOR:
4211 this->expr_->determine_type(context);
4212 break;
4213
4214 case OPERATOR_AND:
4215 // Taking the address of something.
4216 {
4217 Type* subtype = (context->type == NULL
4218 ? NULL
4219 : context->type->points_to());
4220 Type_context subcontext(subtype, false);
4221 this->expr_->determine_type(&subcontext);
4222 }
4223 break;
4224
4225 case OPERATOR_MULT:
4226 // Indirecting through a pointer.
4227 {
4228 Type* subtype = (context->type == NULL
4229 ? NULL
4230 : Type::make_pointer_type(context->type));
4231 Type_context subcontext(subtype, false);
4232 this->expr_->determine_type(&subcontext);
4233 }
4234 break;
4235
4236 default:
c3e6f413 4237 go_unreachable();
e440a328 4238 }
4239}
4240
4241// Check types for a unary expression.
4242
4243void
4244Unary_expression::do_check_types(Gogo*)
4245{
9fe897ef 4246 Type* type = this->expr_->type();
5c13bd80 4247 if (type->is_error())
9fe897ef 4248 {
4249 this->set_is_error();
4250 return;
4251 }
4252
e440a328 4253 switch (this->op_)
4254 {
4255 case OPERATOR_PLUS:
4256 case OPERATOR_MINUS:
9fe897ef 4257 if (type->integer_type() == NULL
4258 && type->float_type() == NULL
4259 && type->complex_type() == NULL)
4260 this->report_error(_("expected numeric type"));
e440a328 4261 break;
4262
4263 case OPERATOR_NOT:
59a401fe 4264 if (!type->is_boolean_type())
4265 this->report_error(_("expected boolean type"));
4266 break;
4267
e440a328 4268 case OPERATOR_XOR:
b3b1474e 4269 if (type->integer_type() == NULL)
4270 this->report_error(_("expected integer"));
e440a328 4271 break;
4272
4273 case OPERATOR_AND:
4274 if (!this->expr_->is_addressable())
09ea332d 4275 {
4276 if (!this->create_temp_)
f4dea966 4277 {
631d5788 4278 go_error_at(this->location(), "invalid operand for unary %<&%>");
f4dea966 4279 this->set_is_error();
4280 }
09ea332d 4281 }
e440a328 4282 else
da244e59 4283 this->expr_->issue_nil_check();
e440a328 4284 break;
4285
4286 case OPERATOR_MULT:
4287 // Indirecting through a pointer.
9fe897ef 4288 if (type->points_to() == NULL)
4289 this->report_error(_("expected pointer"));
7661d702 4290 if (type->points_to()->is_error())
4291 this->set_is_error();
e440a328 4292 break;
4293
4294 default:
c3e6f413 4295 go_unreachable();
e440a328 4296 }
4297}
4298
ea664253 4299// Get the backend representation for a unary expression.
e440a328 4300
ea664253 4301Bexpression*
4302Unary_expression::do_get_backend(Translate_context* context)
e440a328 4303{
1b1f2abf 4304 Gogo* gogo = context->gogo();
e9d3367e 4305 Location loc = this->location();
4306
4307 // Taking the address of a set-and-use-temporary expression requires
4308 // setting the temporary and then taking the address.
4309 if (this->op_ == OPERATOR_AND)
4310 {
4311 Set_and_use_temporary_expression* sut =
4312 this->expr_->set_and_use_temporary_expression();
4313 if (sut != NULL)
4314 {
4315 Temporary_statement* temp = sut->temporary();
4316 Bvariable* bvar = temp->get_backend_variable(context);
d4e6573e 4317 Bexpression* bvar_expr =
7af8e400 4318 gogo->backend()->var_expression(bvar, loc);
ea664253 4319 Bexpression* bval = sut->expression()->get_backend(context);
f9ca30f9 4320
0ab48656 4321 Named_object* fn = context->function();
4322 go_assert(fn != NULL);
4323 Bfunction* bfn =
4324 fn->func_value()->get_or_make_decl(gogo, fn);
f9ca30f9 4325 Bstatement* bassign =
0ab48656 4326 gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc);
f9ca30f9 4327 Bexpression* bvar_addr =
4328 gogo->backend()->address_expression(bvar_expr, loc);
ea664253 4329 return gogo->backend()->compound_expression(bassign, bvar_addr, loc);
e9d3367e 4330 }
4331 }
4332
f9ca30f9 4333 Bexpression* ret;
ea664253 4334 Bexpression* bexpr = this->expr_->get_backend(context);
f9ca30f9 4335 Btype* btype = this->expr_->type()->get_backend(gogo);
e440a328 4336 switch (this->op_)
4337 {
4338 case OPERATOR_PLUS:
f9ca30f9 4339 ret = bexpr;
4340 break;
e440a328 4341
4342 case OPERATOR_MINUS:
f9ca30f9 4343 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
4344 ret = gogo->backend()->convert_expression(btype, ret, loc);
4345 break;
e440a328 4346
4347 case OPERATOR_NOT:
e440a328 4348 case OPERATOR_XOR:
f9ca30f9 4349 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
4350 break;
e440a328 4351
4352 case OPERATOR_AND:
09ea332d 4353 if (!this->create_temp_)
4354 {
4355 // We should not see a non-constant constructor here; cases
4356 // where we would see one should have been moved onto the
4357 // heap at parse time. Taking the address of a nonconstant
4358 // constructor will not do what the programmer expects.
f9ca30f9 4359
4360 go_assert(!this->expr_->is_composite_literal()
3ae06f68 4361 || this->expr_->is_static_initializer());
24060bf9 4362 if (this->expr_->classification() == EXPRESSION_UNARY)
4363 {
4364 Unary_expression* ue =
4365 static_cast<Unary_expression*>(this->expr_);
4366 go_assert(ue->op() != OPERATOR_AND);
4367 }
09ea332d 4368 }
e440a328 4369
f23d7786 4370 if (this->is_gc_root_ || this->is_slice_init_)
76f85fd6 4371 {
19272321 4372 std::string var_name;
f23d7786 4373 bool copy_to_heap = false;
4374 if (this->is_gc_root_)
4375 {
4376 // Build a decl for a GC root variable. GC roots are mutable, so
4377 // they cannot be represented as an immutable_struct in the
4378 // backend.
19272321 4379 var_name = gogo->gc_root_name();
f23d7786 4380 }
4381 else
4382 {
4383 // Build a decl for a slice value initializer. An immutable slice
4384 // value initializer may have to be copied to the heap if it
4385 // contains pointers in a non-constant context.
19272321 4386 var_name = gogo->initializer_name();
f23d7786 4387
4388 Array_type* at = this->expr_->type()->array_type();
4389 go_assert(at != NULL);
4390
4391 // If we are not copying the value to the heap, we will only
4392 // initialize the value once, so we can use this directly
4393 // rather than copying it. In that case we can't make it
4394 // read-only, because the program is permitted to change it.
3ae06f68 4395 copy_to_heap = context->function() != NULL;
f23d7786 4396 }
19272321 4397 std::string asm_name(go_selectively_encode_id(var_name));
f23d7786 4398 Bvariable* implicit =
19272321 4399 gogo->backend()->implicit_variable(var_name, asm_name,
438b4bec 4400 btype, true, copy_to_heap,
4401 false, 0);
19272321 4402 gogo->backend()->implicit_variable_set_init(implicit, var_name, btype,
aa5ae575 4403 true, copy_to_heap, false,
4404 bexpr);
7af8e400 4405 bexpr = gogo->backend()->var_expression(implicit, loc);
1b4fb1e0 4406
4407 // If we are not copying a slice initializer to the heap,
4408 // then it can be changed by the program, so if it can
4409 // contain pointers we must register it as a GC root.
4410 if (this->is_slice_init_
4411 && !copy_to_heap
4412 && this->expr_->type()->has_pointer())
4413 {
4414 Bexpression* root =
7af8e400 4415 gogo->backend()->var_expression(implicit, loc);
1b4fb1e0 4416 root = gogo->backend()->address_expression(root, loc);
4417 Type* type = Type::make_pointer_type(this->expr_->type());
4418 gogo->add_gc_root(Expression::make_backend(root, type, loc));
4419 }
76f85fd6 4420 }
4421 else if ((this->expr_->is_composite_literal()
3ae06f68 4422 || this->expr_->string_expression() != NULL)
4423 && this->expr_->is_static_initializer())
f9ca30f9 4424 {
19272321 4425 std::string var_name(gogo->initializer_name());
4426 std::string asm_name(go_selectively_encode_id(var_name));
f9ca30f9 4427 Bvariable* decl =
19272321 4428 gogo->backend()->immutable_struct(var_name, asm_name,
438b4bec 4429 true, false, btype, loc);
19272321 4430 gogo->backend()->immutable_struct_set_init(decl, var_name, true,
4431 false, btype, loc, bexpr);
7af8e400 4432 bexpr = gogo->backend()->var_expression(decl, loc);
f9ca30f9 4433 }
09ea332d 4434
f9ca30f9 4435 go_assert(!this->create_temp_ || this->expr_->is_variable());
4436 ret = gogo->backend()->address_expression(bexpr, loc);
4437 break;
e440a328 4438
4439 case OPERATOR_MULT:
4440 {
f9ca30f9 4441 go_assert(this->expr_->type()->points_to() != NULL);
e440a328 4442
f614ea8b 4443 bool known_valid = false;
f9ca30f9 4444 Type* ptype = this->expr_->type()->points_to();
4445 Btype* pbtype = ptype->get_backend(gogo);
f614ea8b 4446 switch (this->requires_nil_check(gogo))
4447 {
4448 case NIL_CHECK_NOT_NEEDED:
4449 break;
4450 case NIL_CHECK_ERROR_ENCOUNTERED:
2a305b85 4451 {
4452 go_assert(saw_errors());
4453 return gogo->backend()->error_expression();
4454 }
f614ea8b 4455 case NIL_CHECK_NEEDED:
4456 {
f9ca30f9 4457 go_assert(this->expr_->is_variable());
2dd89704 4458
4459 // If we're nil-checking the result of a set-and-use-temporary
4460 // expression, then pick out the target temp and use that
4461 // for the final result of the conditional.
4462 Bexpression* tbexpr = bexpr;
4463 Bexpression* ubexpr = bexpr;
4464 Set_and_use_temporary_expression* sut =
4465 this->expr_->set_and_use_temporary_expression();
4466 if (sut != NULL) {
4467 Temporary_statement* temp = sut->temporary();
4468 Bvariable* bvar = temp->get_backend_variable(context);
4469 ubexpr = gogo->backend()->var_expression(bvar, loc);
4470 }
ea664253 4471 Bexpression* nil =
f614ea8b 4472 Expression::make_nil(loc)->get_backend(context);
f9ca30f9 4473 Bexpression* compare =
2dd89704 4474 gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr,
f9ca30f9 4475 nil, loc);
f9ca30f9 4476 Bexpression* crash =
f614ea8b 4477 gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4478 loc)->get_backend(context);
93715b75 4479 Bfunction* bfn = context->function()->func_value()->get_decl();
4480 bexpr = gogo->backend()->conditional_expression(bfn, btype,
4481 compare,
2dd89704 4482 crash, ubexpr,
f9ca30f9 4483 loc);
f614ea8b 4484 known_valid = true;
4485 break;
4486 }
4487 case NIL_CHECK_DEFAULT:
4488 go_unreachable();
4489 }
4490 ret = gogo->backend()->indirect_expression(pbtype, bexpr,
4491 known_valid, loc);
e440a328 4492 }
f9ca30f9 4493 break;
e440a328 4494
4495 default:
c3e6f413 4496 go_unreachable();
e440a328 4497 }
f9ca30f9 4498
ea664253 4499 return ret;
e440a328 4500}
4501
4502// Export a unary expression.
4503
4504void
4505Unary_expression::do_export(Export* exp) const
4506{
4507 switch (this->op_)
4508 {
4509 case OPERATOR_PLUS:
4510 exp->write_c_string("+ ");
4511 break;
4512 case OPERATOR_MINUS:
4513 exp->write_c_string("- ");
4514 break;
4515 case OPERATOR_NOT:
4516 exp->write_c_string("! ");
4517 break;
4518 case OPERATOR_XOR:
4519 exp->write_c_string("^ ");
4520 break;
4521 case OPERATOR_AND:
4522 case OPERATOR_MULT:
4523 default:
c3e6f413 4524 go_unreachable();
e440a328 4525 }
4526 this->expr_->export_expression(exp);
4527}
4528
4529// Import a unary expression.
4530
4531Expression*
4532Unary_expression::do_import(Import* imp)
4533{
4534 Operator op;
4535 switch (imp->get_char())
4536 {
4537 case '+':
4538 op = OPERATOR_PLUS;
4539 break;
4540 case '-':
4541 op = OPERATOR_MINUS;
4542 break;
4543 case '!':
4544 op = OPERATOR_NOT;
4545 break;
4546 case '^':
4547 op = OPERATOR_XOR;
4548 break;
4549 default:
c3e6f413 4550 go_unreachable();
e440a328 4551 }
4552 imp->require_c_string(" ");
4553 Expression* expr = Expression::import_expression(imp);
4554 return Expression::make_unary(op, expr, imp->location());
4555}
4556
d751bb78 4557// Dump ast representation of an unary expression.
4558
4559void
4560Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4561{
4562 ast_dump_context->dump_operator(this->op_);
4563 ast_dump_context->ostream() << "(";
4564 ast_dump_context->dump_expression(this->expr_);
4565 ast_dump_context->ostream() << ") ";
4566}
4567
e440a328 4568// Make a unary expression.
4569
4570Expression*
b13c66cd 4571Expression::make_unary(Operator op, Expression* expr, Location location)
e440a328 4572{
4573 return new Unary_expression(op, expr, location);
4574}
4575
f614ea8b 4576Expression*
4577Expression::make_dereference(Expression* ptr,
4578 Nil_check_classification docheck,
4579 Location location)
4580{
4581 Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location);
4582 if (docheck == NIL_CHECK_NEEDED)
4583 deref->unary_expression()->set_requires_nil_check(true);
4584 else if (docheck == NIL_CHECK_NOT_NEEDED)
4585 deref->unary_expression()->set_requires_nil_check(false);
4586 return deref;
4587}
4588
e440a328 4589// If this is an indirection through a pointer, return the expression
4590// being pointed through. Otherwise return this.
4591
4592Expression*
4593Expression::deref()
4594{
4595 if (this->classification_ == EXPRESSION_UNARY)
4596 {
4597 Unary_expression* ue = static_cast<Unary_expression*>(this);
4598 if (ue->op() == OPERATOR_MULT)
4599 return ue->operand();
4600 }
4601 return this;
4602}
4603
4604// Class Binary_expression.
4605
4606// Traversal.
4607
4608int
4609Binary_expression::do_traverse(Traverse* traverse)
4610{
4611 int t = Expression::traverse(&this->left_, traverse);
4612 if (t == TRAVERSE_EXIT)
4613 return TRAVERSE_EXIT;
4614 return Expression::traverse(&this->right_, traverse);
4615}
4616
3ae06f68 4617// Return whether this expression may be used as a static initializer.
4618
4619bool
4620Binary_expression::do_is_static_initializer() const
4621{
4622 if (!this->left_->is_static_initializer()
4623 || !this->right_->is_static_initializer())
4624 return false;
4625
4626 // Addresses can be static initializers, but we can't implement
4627 // arbitray binary expressions of them.
4628 Unary_expression* lu = this->left_->unary_expression();
4629 Unary_expression* ru = this->right_->unary_expression();
4630 if (lu != NULL && lu->op() == OPERATOR_AND)
4631 {
4632 if (ru != NULL && ru->op() == OPERATOR_AND)
4633 return this->op_ == OPERATOR_MINUS;
4634 else
4635 return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS;
4636 }
4637 else if (ru != NULL && ru->op() == OPERATOR_AND)
4638 return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS;
4639
4640 // Other cases should resolve in the backend.
4641 return true;
4642}
4643
0c77715b 4644// Return the type to use for a binary operation on operands of
4645// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4646// such may be NULL or abstract.
4647
4648bool
4649Binary_expression::operation_type(Operator op, Type* left_type,
4650 Type* right_type, Type** result_type)
4651{
4652 if (left_type != right_type
4653 && !left_type->is_abstract()
4654 && !right_type->is_abstract()
4655 && left_type->base() != right_type->base()
4656 && op != OPERATOR_LSHIFT
4657 && op != OPERATOR_RSHIFT)
4658 {
4659 // May be a type error--let it be diagnosed elsewhere.
4660 return false;
4661 }
4662
4663 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4664 {
4665 if (left_type->integer_type() != NULL)
4666 *result_type = left_type;
4667 else
4668 *result_type = Type::make_abstract_integer_type();
4669 }
4670 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
4671 *result_type = left_type;
4672 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
4673 *result_type = right_type;
4674 else if (!left_type->is_abstract())
4675 *result_type = left_type;
4676 else if (!right_type->is_abstract())
4677 *result_type = right_type;
4678 else if (left_type->complex_type() != NULL)
4679 *result_type = left_type;
4680 else if (right_type->complex_type() != NULL)
4681 *result_type = right_type;
4682 else if (left_type->float_type() != NULL)
4683 *result_type = left_type;
4684 else if (right_type->float_type() != NULL)
4685 *result_type = right_type;
4686 else if (left_type->integer_type() != NULL
4687 && left_type->integer_type()->is_rune())
4688 *result_type = left_type;
4689 else if (right_type->integer_type() != NULL
4690 && right_type->integer_type()->is_rune())
4691 *result_type = right_type;
4692 else
4693 *result_type = left_type;
4694
4695 return true;
4696}
4697
4698// Convert an integer comparison code and an operator to a boolean
4699// value.
e440a328 4700
4701bool
0c77715b 4702Binary_expression::cmp_to_bool(Operator op, int cmp)
e440a328 4703{
e440a328 4704 switch (op)
4705 {
4706 case OPERATOR_EQEQ:
0c77715b 4707 return cmp == 0;
4708 break;
e440a328 4709 case OPERATOR_NOTEQ:
0c77715b 4710 return cmp != 0;
4711 break;
e440a328 4712 case OPERATOR_LT:
0c77715b 4713 return cmp < 0;
4714 break;
e440a328 4715 case OPERATOR_LE:
0c77715b 4716 return cmp <= 0;
e440a328 4717 case OPERATOR_GT:
0c77715b 4718 return cmp > 0;
e440a328 4719 case OPERATOR_GE:
0c77715b 4720 return cmp >= 0;
e440a328 4721 default:
c3e6f413 4722 go_unreachable();
e440a328 4723 }
4724}
4725
0c77715b 4726// Compare constants according to OP.
e440a328 4727
4728bool
0c77715b 4729Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
4730 Numeric_constant* right_nc,
4731 Location location, bool* result)
e440a328 4732{
0c77715b 4733 Type* left_type = left_nc->type();
4734 Type* right_type = right_nc->type();
4735
4736 Type* type;
4737 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4738 return false;
4739
4740 // When comparing an untyped operand to a typed operand, we are
4741 // effectively coercing the untyped operand to the other operand's
4742 // type, so make sure that is valid.
4743 if (!left_nc->set_type(type, true, location)
4744 || !right_nc->set_type(type, true, location))
4745 return false;
4746
4747 bool ret;
4748 int cmp;
4749 if (type->complex_type() != NULL)
4750 {
4751 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
4752 return false;
4753 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
4754 }
4755 else if (type->float_type() != NULL)
4756 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
e440a328 4757 else
0c77715b 4758 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
4759
4760 if (ret)
4761 *result = Binary_expression::cmp_to_bool(op, cmp);
4762
4763 return ret;
4764}
4765
4766// Compare integer constants.
4767
4768bool
4769Binary_expression::compare_integer(const Numeric_constant* left_nc,
4770 const Numeric_constant* right_nc,
4771 int* cmp)
4772{
4773 mpz_t left_val;
4774 if (!left_nc->to_int(&left_val))
4775 return false;
4776 mpz_t right_val;
4777 if (!right_nc->to_int(&right_val))
e440a328 4778 {
0c77715b 4779 mpz_clear(left_val);
4780 return false;
e440a328 4781 }
0c77715b 4782
4783 *cmp = mpz_cmp(left_val, right_val);
4784
4785 mpz_clear(left_val);
4786 mpz_clear(right_val);
4787
4788 return true;
4789}
4790
4791// Compare floating point constants.
4792
4793bool
4794Binary_expression::compare_float(const Numeric_constant* left_nc,
4795 const Numeric_constant* right_nc,
4796 int* cmp)
4797{
4798 mpfr_t left_val;
4799 if (!left_nc->to_float(&left_val))
4800 return false;
4801 mpfr_t right_val;
4802 if (!right_nc->to_float(&right_val))
e440a328 4803 {
0c77715b 4804 mpfr_clear(left_val);
4805 return false;
4806 }
4807
4808 // We already coerced both operands to the same type. If that type
4809 // is not an abstract type, we need to round the values accordingly.
4810 Type* type = left_nc->type();
4811 if (!type->is_abstract() && type->float_type() != NULL)
4812 {
4813 int bits = type->float_type()->bits();
4814 mpfr_prec_round(left_val, bits, GMP_RNDN);
4815 mpfr_prec_round(right_val, bits, GMP_RNDN);
e440a328 4816 }
0c77715b 4817
4818 *cmp = mpfr_cmp(left_val, right_val);
4819
4820 mpfr_clear(left_val);
4821 mpfr_clear(right_val);
4822
4823 return true;
e440a328 4824}
4825
0c77715b 4826// Compare complex constants. Complex numbers may only be compared
4827// for equality.
e440a328 4828
4829bool
0c77715b 4830Binary_expression::compare_complex(const Numeric_constant* left_nc,
4831 const Numeric_constant* right_nc,
4832 int* cmp)
e440a328 4833{
fcbea5e4 4834 mpc_t left_val;
4835 if (!left_nc->to_complex(&left_val))
0c77715b 4836 return false;
fcbea5e4 4837 mpc_t right_val;
4838 if (!right_nc->to_complex(&right_val))
e440a328 4839 {
fcbea5e4 4840 mpc_clear(left_val);
0c77715b 4841 return false;
e440a328 4842 }
0c77715b 4843
4844 // We already coerced both operands to the same type. If that type
4845 // is not an abstract type, we need to round the values accordingly.
4846 Type* type = left_nc->type();
4847 if (!type->is_abstract() && type->complex_type() != NULL)
e440a328 4848 {
0c77715b 4849 int bits = type->complex_type()->bits();
fcbea5e4 4850 mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN);
4851 mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN);
4852 mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN);
4853 mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN);
e440a328 4854 }
0c77715b 4855
fcbea5e4 4856 *cmp = mpc_cmp(left_val, right_val) != 0;
0c77715b 4857
fcbea5e4 4858 mpc_clear(left_val);
4859 mpc_clear(right_val);
0c77715b 4860
4861 return true;
e440a328 4862}
4863
0c77715b 4864// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4865// true if this could be done, false if not. Issue errors at LOCATION
af7a5274 4866// as appropriate, and sets *ISSUED_ERROR if it did.
e440a328 4867
4868bool
0c77715b 4869Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
4870 Numeric_constant* right_nc,
af7a5274 4871 Location location, Numeric_constant* nc,
4872 bool* issued_error)
e440a328 4873{
af7a5274 4874 *issued_error = false;
e440a328 4875 switch (op)
4876 {
4877 case OPERATOR_OROR:
4878 case OPERATOR_ANDAND:
4879 case OPERATOR_EQEQ:
4880 case OPERATOR_NOTEQ:
4881 case OPERATOR_LT:
4882 case OPERATOR_LE:
4883 case OPERATOR_GT:
4884 case OPERATOR_GE:
9767e2d3 4885 // These return boolean values, not numeric.
4886 return false;
0c77715b 4887 default:
4888 break;
4889 }
4890
4891 Type* left_type = left_nc->type();
4892 Type* right_type = right_nc->type();
4893
4894 Type* type;
4895 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4896 return false;
4897
4898 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
4899
4900 // When combining an untyped operand with a typed operand, we are
4901 // effectively coercing the untyped operand to the other operand's
4902 // type, so make sure that is valid.
4903 if (!left_nc->set_type(type, true, location))
4904 return false;
4905 if (!is_shift && !right_nc->set_type(type, true, location))
4906 return false;
85334a21 4907 if (is_shift
4908 && ((left_type->integer_type() == NULL
4909 && !left_type->is_abstract())
4910 || (right_type->integer_type() == NULL
4911 && !right_type->is_abstract())))
4912 return false;
0c77715b 4913
4914 bool r;
4915 if (type->complex_type() != NULL)
4916 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc);
4917 else if (type->float_type() != NULL)
4918 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc);
4919 else
4920 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
4921
4922 if (r)
af7a5274 4923 {
4924 r = nc->set_type(type, true, location);
4925 if (!r)
4926 *issued_error = true;
4927 }
0c77715b 4928
4929 return r;
4930}
4931
4932// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4933// integer operations. Return true if this could be done, false if
4934// not.
4935
4936bool
4937Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
4938 const Numeric_constant* right_nc,
4939 Location location, Numeric_constant* nc)
4940{
4941 mpz_t left_val;
4942 if (!left_nc->to_int(&left_val))
4943 return false;
4944 mpz_t right_val;
4945 if (!right_nc->to_int(&right_val))
4946 {
4947 mpz_clear(left_val);
e440a328 4948 return false;
0c77715b 4949 }
4950
4951 mpz_t val;
4952 mpz_init(val);
4953
4954 switch (op)
4955 {
e440a328 4956 case OPERATOR_PLUS:
4957 mpz_add(val, left_val, right_val);
2c809f8f 4958 if (mpz_sizeinbase(val, 2) > 0x100000)
4959 {
631d5788 4960 go_error_at(location, "constant addition overflow");
71a45216 4961 nc->set_invalid();
2c809f8f 4962 mpz_set_ui(val, 1);
4963 }
e440a328 4964 break;
4965 case OPERATOR_MINUS:
4966 mpz_sub(val, left_val, right_val);
2c809f8f 4967 if (mpz_sizeinbase(val, 2) > 0x100000)
4968 {
631d5788 4969 go_error_at(location, "constant subtraction overflow");
71a45216 4970 nc->set_invalid();
2c809f8f 4971 mpz_set_ui(val, 1);
4972 }
e440a328 4973 break;
4974 case OPERATOR_OR:
4975 mpz_ior(val, left_val, right_val);
4976 break;
4977 case OPERATOR_XOR:
4978 mpz_xor(val, left_val, right_val);
4979 break;
4980 case OPERATOR_MULT:
4981 mpz_mul(val, left_val, right_val);
2c809f8f 4982 if (mpz_sizeinbase(val, 2) > 0x100000)
4983 {
631d5788 4984 go_error_at(location, "constant multiplication overflow");
71a45216 4985 nc->set_invalid();
2c809f8f 4986 mpz_set_ui(val, 1);
4987 }
e440a328 4988 break;
4989 case OPERATOR_DIV:
4990 if (mpz_sgn(right_val) != 0)
4991 mpz_tdiv_q(val, left_val, right_val);
4992 else
4993 {
631d5788 4994 go_error_at(location, "division by zero");
71a45216 4995 nc->set_invalid();
e440a328 4996 mpz_set_ui(val, 0);
e440a328 4997 }
4998 break;
4999 case OPERATOR_MOD:
5000 if (mpz_sgn(right_val) != 0)
5001 mpz_tdiv_r(val, left_val, right_val);
5002 else
5003 {
631d5788 5004 go_error_at(location, "division by zero");
71a45216 5005 nc->set_invalid();
e440a328 5006 mpz_set_ui(val, 0);
e440a328 5007 }
5008 break;
5009 case OPERATOR_LSHIFT:
5010 {
5011 unsigned long shift = mpz_get_ui(right_val);
0c77715b 5012 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
5013 mpz_mul_2exp(val, left_val, shift);
5014 else
e440a328 5015 {
631d5788 5016 go_error_at(location, "shift count overflow");
71a45216 5017 nc->set_invalid();
2c809f8f 5018 mpz_set_ui(val, 1);
e440a328 5019 }
e440a328 5020 break;
5021 }
5022 break;
5023 case OPERATOR_RSHIFT:
5024 {
5025 unsigned long shift = mpz_get_ui(right_val);
5026 if (mpz_cmp_ui(right_val, shift) != 0)
5027 {
631d5788 5028 go_error_at(location, "shift count overflow");
71a45216 5029 nc->set_invalid();
2c809f8f 5030 mpz_set_ui(val, 1);
e440a328 5031 }
e440a328 5032 else
0c77715b 5033 {
5034 if (mpz_cmp_ui(left_val, 0) >= 0)
5035 mpz_tdiv_q_2exp(val, left_val, shift);
5036 else
5037 mpz_fdiv_q_2exp(val, left_val, shift);
5038 }
e440a328 5039 break;
5040 }
5041 break;
5042 case OPERATOR_AND:
5043 mpz_and(val, left_val, right_val);
5044 break;
5045 case OPERATOR_BITCLEAR:
5046 {
5047 mpz_t tval;
5048 mpz_init(tval);
5049 mpz_com(tval, right_val);
5050 mpz_and(val, left_val, tval);
5051 mpz_clear(tval);
5052 }
5053 break;
5054 default:
c3e6f413 5055 go_unreachable();
e440a328 5056 }
5057
0c77715b 5058 mpz_clear(left_val);
5059 mpz_clear(right_val);
e440a328 5060
0c77715b 5061 if (left_nc->is_rune()
5062 || (op != OPERATOR_LSHIFT
5063 && op != OPERATOR_RSHIFT
5064 && right_nc->is_rune()))
5065 nc->set_rune(NULL, val);
5066 else
5067 nc->set_int(NULL, val);
5068
5069 mpz_clear(val);
e440a328 5070
5071 return true;
5072}
5073
0c77715b 5074// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5075// floating point operations. Return true if this could be done,
5076// false if not.
e440a328 5077
5078bool
0c77715b 5079Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
5080 const Numeric_constant* right_nc,
5081 Location location, Numeric_constant* nc)
e440a328 5082{
0c77715b 5083 mpfr_t left_val;
5084 if (!left_nc->to_float(&left_val))
5085 return false;
5086 mpfr_t right_val;
5087 if (!right_nc->to_float(&right_val))
e440a328 5088 {
0c77715b 5089 mpfr_clear(left_val);
e440a328 5090 return false;
0c77715b 5091 }
5092
5093 mpfr_t val;
5094 mpfr_init(val);
5095
5096 bool ret = true;
5097 switch (op)
5098 {
e440a328 5099 case OPERATOR_PLUS:
5100 mpfr_add(val, left_val, right_val, GMP_RNDN);
5101 break;
5102 case OPERATOR_MINUS:
5103 mpfr_sub(val, left_val, right_val, GMP_RNDN);
5104 break;
5105 case OPERATOR_OR:
5106 case OPERATOR_XOR:
5107 case OPERATOR_AND:
5108 case OPERATOR_BITCLEAR:
0c77715b 5109 case OPERATOR_MOD:
5110 case OPERATOR_LSHIFT:
5111 case OPERATOR_RSHIFT:
5112 mpfr_set_ui(val, 0, GMP_RNDN);
5113 ret = false;
5114 break;
e440a328 5115 case OPERATOR_MULT:
5116 mpfr_mul(val, left_val, right_val, GMP_RNDN);
5117 break;
5118 case OPERATOR_DIV:
0c77715b 5119 if (!mpfr_zero_p(right_val))
5120 mpfr_div(val, left_val, right_val, GMP_RNDN);
5121 else
5122 {
631d5788 5123 go_error_at(location, "division by zero");
71a45216 5124 nc->set_invalid();
0c77715b 5125 mpfr_set_ui(val, 0, GMP_RNDN);
5126 }
e440a328 5127 break;
e440a328 5128 default:
c3e6f413 5129 go_unreachable();
e440a328 5130 }
5131
0c77715b 5132 mpfr_clear(left_val);
5133 mpfr_clear(right_val);
e440a328 5134
0c77715b 5135 nc->set_float(NULL, val);
5136 mpfr_clear(val);
e440a328 5137
0c77715b 5138 return ret;
e440a328 5139}
5140
0c77715b 5141// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5142// complex operations. Return true if this could be done, false if
5143// not.
e440a328 5144
5145bool
0c77715b 5146Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
5147 const Numeric_constant* right_nc,
5148 Location location, Numeric_constant* nc)
e440a328 5149{
fcbea5e4 5150 mpc_t left_val;
5151 if (!left_nc->to_complex(&left_val))
0c77715b 5152 return false;
fcbea5e4 5153 mpc_t right_val;
5154 if (!right_nc->to_complex(&right_val))
e440a328 5155 {
fcbea5e4 5156 mpc_clear(left_val);
e440a328 5157 return false;
0c77715b 5158 }
5159
fcbea5e4 5160 mpc_t val;
5161 mpc_init2(val, mpc_precision);
0c77715b 5162
5163 bool ret = true;
5164 switch (op)
5165 {
e440a328 5166 case OPERATOR_PLUS:
fcbea5e4 5167 mpc_add(val, left_val, right_val, MPC_RNDNN);
e440a328 5168 break;
5169 case OPERATOR_MINUS:
fcbea5e4 5170 mpc_sub(val, left_val, right_val, MPC_RNDNN);
e440a328 5171 break;
5172 case OPERATOR_OR:
5173 case OPERATOR_XOR:
5174 case OPERATOR_AND:
5175 case OPERATOR_BITCLEAR:
0c77715b 5176 case OPERATOR_MOD:
5177 case OPERATOR_LSHIFT:
5178 case OPERATOR_RSHIFT:
fcbea5e4 5179 mpc_set_ui(val, 0, MPC_RNDNN);
0c77715b 5180 ret = false;
5181 break;
e440a328 5182 case OPERATOR_MULT:
fcbea5e4 5183 mpc_mul(val, left_val, right_val, MPC_RNDNN);
e440a328 5184 break;
5185 case OPERATOR_DIV:
fcbea5e4 5186 if (mpc_cmp_si(right_val, 0) == 0)
5187 {
631d5788 5188 go_error_at(location, "division by zero");
71a45216 5189 nc->set_invalid();
fcbea5e4 5190 mpc_set_ui(val, 0, MPC_RNDNN);
5191 break;
5192 }
5193 mpc_div(val, left_val, right_val, MPC_RNDNN);
e440a328 5194 break;
e440a328 5195 default:
c3e6f413 5196 go_unreachable();
e440a328 5197 }
5198
fcbea5e4 5199 mpc_clear(left_val);
5200 mpc_clear(right_val);
e440a328 5201
fcbea5e4 5202 nc->set_complex(NULL, val);
5203 mpc_clear(val);
e440a328 5204
0c77715b 5205 return ret;
e440a328 5206}
5207
5208// Lower a binary expression. We have to evaluate constant
5209// expressions now, in order to implement Go's unlimited precision
5210// constants.
5211
5212Expression*
e9d3367e 5213Binary_expression::do_lower(Gogo* gogo, Named_object*,
5214 Statement_inserter* inserter, int)
e440a328 5215{
b13c66cd 5216 Location location = this->location();
e440a328 5217 Operator op = this->op_;
5218 Expression* left = this->left_;
5219 Expression* right = this->right_;
5220
5221 const bool is_comparison = (op == OPERATOR_EQEQ
5222 || op == OPERATOR_NOTEQ
5223 || op == OPERATOR_LT
5224 || op == OPERATOR_LE
5225 || op == OPERATOR_GT
5226 || op == OPERATOR_GE);
5227
0c77715b 5228 // Numeric constant expressions.
e440a328 5229 {
0c77715b 5230 Numeric_constant left_nc;
5231 Numeric_constant right_nc;
5232 if (left->numeric_constant_value(&left_nc)
5233 && right->numeric_constant_value(&right_nc))
e440a328 5234 {
0c77715b 5235 if (is_comparison)
e440a328 5236 {
0c77715b 5237 bool result;
5238 if (!Binary_expression::compare_constant(op, &left_nc,
5239 &right_nc, location,
5240 &result))
5241 return this;
e90c9dfc 5242 return Expression::make_cast(Type::make_boolean_type(),
0c77715b 5243 Expression::make_boolean(result,
5244 location),
5245 location);
e440a328 5246 }
5247 else
5248 {
0c77715b 5249 Numeric_constant nc;
af7a5274 5250 bool issued_error;
0c77715b 5251 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
af7a5274 5252 location, &nc,
5253 &issued_error))
5254 {
5255 if (issued_error)
5256 return Expression::make_error(location);
71a45216 5257 return this;
af7a5274 5258 }
0c77715b 5259 return nc.expression(location);
e440a328 5260 }
5261 }
e440a328 5262 }
5263
5264 // String constant expressions.
315fa98d 5265 if (left->type()->is_string_type() && right->type()->is_string_type())
e440a328 5266 {
5267 std::string left_string;
5268 std::string right_string;
5269 if (left->string_constant_value(&left_string)
5270 && right->string_constant_value(&right_string))
315fa98d 5271 {
5272 if (op == OPERATOR_PLUS)
5273 return Expression::make_string(left_string + right_string,
5274 location);
5275 else if (is_comparison)
5276 {
5277 int cmp = left_string.compare(right_string);
0c77715b 5278 bool r = Binary_expression::cmp_to_bool(op, cmp);
e90c9dfc 5279 return Expression::make_boolean(r, location);
b40dc774 5280 }
5281 }
b40dc774 5282 }
5283
ceeb12d7 5284 // Lower struct, array, and some interface comparisons.
e9d3367e 5285 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5286 {
b79832ca 5287 if (left->type()->struct_type() != NULL
5288 && right->type()->struct_type() != NULL)
e9d3367e 5289 return this->lower_struct_comparison(gogo, inserter);
5290 else if (left->type()->array_type() != NULL
b79832ca 5291 && !left->type()->is_slice_type()
5292 && right->type()->array_type() != NULL
5293 && !right->type()->is_slice_type())
e9d3367e 5294 return this->lower_array_comparison(gogo, inserter);
ceeb12d7 5295 else if ((left->type()->interface_type() != NULL
5296 && right->type()->interface_type() == NULL)
5297 || (left->type()->interface_type() == NULL
5298 && right->type()->interface_type() != NULL))
5299 return this->lower_interface_value_comparison(gogo, inserter);
e9d3367e 5300 }
5301
736a16ba 5302 // Lower string concatenation to String_concat_expression, so that
5303 // we can group sequences of string additions.
5304 if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS)
5305 {
5306 Expression_list* exprs;
5307 String_concat_expression* left_sce =
5308 this->left_->string_concat_expression();
5309 if (left_sce != NULL)
5310 exprs = left_sce->exprs();
5311 else
5312 {
5313 exprs = new Expression_list();
5314 exprs->push_back(this->left_);
5315 }
5316
5317 String_concat_expression* right_sce =
5318 this->right_->string_concat_expression();
5319 if (right_sce != NULL)
5320 exprs->append(right_sce->exprs());
5321 else
5322 exprs->push_back(this->right_);
5323
5324 return Expression::make_string_concat(exprs);
5325 }
5326
e440a328 5327 return this;
5328}
5329
e9d3367e 5330// Lower a struct comparison.
5331
5332Expression*
5333Binary_expression::lower_struct_comparison(Gogo* gogo,
5334 Statement_inserter* inserter)
5335{
5336 Struct_type* st = this->left_->type()->struct_type();
5337 Struct_type* st2 = this->right_->type()->struct_type();
5338 if (st2 == NULL)
5339 return this;
5340 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5341 return this;
5342 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5343 this->right_->type(), NULL))
5344 return this;
5345
5346 // See if we can compare using memcmp. As a heuristic, we use
5347 // memcmp rather than field references and comparisons if there are
5348 // more than two fields.
113ef6a5 5349 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
e9d3367e 5350 return this->lower_compare_to_memcmp(gogo, inserter);
5351
5352 Location loc = this->location();
5353
5354 Expression* left = this->left_;
5355 Temporary_statement* left_temp = NULL;
5356 if (left->var_expression() == NULL
5357 && left->temporary_reference_expression() == NULL)
5358 {
5359 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5360 inserter->insert(left_temp);
5361 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5362 }
5363
5364 Expression* right = this->right_;
5365 Temporary_statement* right_temp = NULL;
5366 if (right->var_expression() == NULL
5367 && right->temporary_reference_expression() == NULL)
5368 {
5369 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5370 inserter->insert(right_temp);
5371 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5372 }
5373
5374 Expression* ret = Expression::make_boolean(true, loc);
5375 const Struct_field_list* fields = st->fields();
5376 unsigned int field_index = 0;
5377 for (Struct_field_list::const_iterator pf = fields->begin();
5378 pf != fields->end();
5379 ++pf, ++field_index)
5380 {
f5165c05 5381 if (Gogo::is_sink_name(pf->field_name()))
5382 continue;
5383
e9d3367e 5384 if (field_index > 0)
5385 {
5386 if (left_temp == NULL)
5387 left = left->copy();
5388 else
5389 left = Expression::make_temporary_reference(left_temp, loc);
5390 if (right_temp == NULL)
5391 right = right->copy();
5392 else
5393 right = Expression::make_temporary_reference(right_temp, loc);
5394 }
5395 Expression* f1 = Expression::make_field_reference(left, field_index,
5396 loc);
5397 Expression* f2 = Expression::make_field_reference(right, field_index,
5398 loc);
5399 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5400 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5401 }
5402
5403 if (this->op_ == OPERATOR_NOTEQ)
5404 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5405
5406 return ret;
5407}
5408
5409// Lower an array comparison.
5410
5411Expression*
5412Binary_expression::lower_array_comparison(Gogo* gogo,
5413 Statement_inserter* inserter)
5414{
5415 Array_type* at = this->left_->type()->array_type();
5416 Array_type* at2 = this->right_->type()->array_type();
5417 if (at2 == NULL)
5418 return this;
5419 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5420 return this;
5421 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5422 this->right_->type(), NULL))
5423 return this;
5424
5425 // Call memcmp directly if possible. This may let the middle-end
5426 // optimize the call.
113ef6a5 5427 if (at->compare_is_identity(gogo))
e9d3367e 5428 return this->lower_compare_to_memcmp(gogo, inserter);
5429
5430 // Call the array comparison function.
5431 Named_object* hash_fn;
5432 Named_object* equal_fn;
5433 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5434 &hash_fn, &equal_fn);
5435
5436 Location loc = this->location();
5437
5438 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5439
5440 Expression_list* args = new Expression_list();
5441 args->push_back(this->operand_address(inserter, this->left_));
5442 args->push_back(this->operand_address(inserter, this->right_));
e9d3367e 5443
5444 Expression* ret = Expression::make_call(func, args, false, loc);
5445
5446 if (this->op_ == OPERATOR_NOTEQ)
5447 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5448
5449 return ret;
5450}
5451
ceeb12d7 5452// Lower an interface to value comparison.
5453
5454Expression*
5455Binary_expression::lower_interface_value_comparison(Gogo*,
5456 Statement_inserter* inserter)
5457{
5458 Type* left_type = this->left_->type();
5459 Type* right_type = this->right_->type();
5460 Interface_type* ift;
5461 if (left_type->interface_type() != NULL)
5462 {
5463 ift = left_type->interface_type();
5464 if (!ift->implements_interface(right_type, NULL))
5465 return this;
5466 }
5467 else
5468 {
5469 ift = right_type->interface_type();
5470 if (!ift->implements_interface(left_type, NULL))
5471 return this;
5472 }
5473 if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL))
5474 return this;
5475
5476 Location loc = this->location();
5477
5478 if (left_type->interface_type() == NULL
5479 && left_type->points_to() == NULL
5480 && !this->left_->is_addressable())
5481 {
5482 Temporary_statement* temp =
5483 Statement::make_temporary(left_type, NULL, loc);
5484 inserter->insert(temp);
5485 this->left_ =
5486 Expression::make_set_and_use_temporary(temp, this->left_, loc);
5487 }
5488
5489 if (right_type->interface_type() == NULL
5490 && right_type->points_to() == NULL
5491 && !this->right_->is_addressable())
5492 {
5493 Temporary_statement* temp =
5494 Statement::make_temporary(right_type, NULL, loc);
5495 inserter->insert(temp);
5496 this->right_ =
5497 Expression::make_set_and_use_temporary(temp, this->right_, loc);
5498 }
5499
5500 return this;
5501}
5502
e9d3367e 5503// Lower a struct or array comparison to a call to memcmp.
5504
5505Expression*
5506Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5507{
5508 Location loc = this->location();
5509
5510 Expression* a1 = this->operand_address(inserter, this->left_);
5511 Expression* a2 = this->operand_address(inserter, this->right_);
5512 Expression* len = Expression::make_type_info(this->left_->type(),
5513 TYPE_INFO_SIZE);
5514
5515 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
e67508fa 5516 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
e9d3367e 5517 return Expression::make_binary(this->op_, call, zero, loc);
5518}
5519
a32698ee 5520Expression*
5c3f3470 5521Binary_expression::do_flatten(Gogo* gogo, Named_object*,
a32698ee 5522 Statement_inserter* inserter)
5523{
5524 Location loc = this->location();
5bf8be8b 5525 if (this->left_->type()->is_error_type()
5526 || this->right_->type()->is_error_type()
5527 || this->left_->is_error_expression()
5528 || this->right_->is_error_expression())
5529 {
5530 go_assert(saw_errors());
5531 return Expression::make_error(loc);
5532 }
5533
a32698ee 5534 Temporary_statement* temp;
a32698ee 5535
5536 Type* left_type = this->left_->type();
5537 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5538 || this->op_ == OPERATOR_RSHIFT);
5539 bool is_idiv_op = ((this->op_ == OPERATOR_DIV &&
5540 left_type->integer_type() != NULL)
5541 || this->op_ == OPERATOR_MOD);
5542
a32698ee 5543 if (is_shift_op
5c3f3470 5544 || (is_idiv_op
5545 && (gogo->check_divide_by_zero() || gogo->check_divide_overflow())))
a32698ee 5546 {
545ab43b 5547 if (!this->left_->is_variable() && !this->left_->is_constant())
a32698ee 5548 {
5549 temp = Statement::make_temporary(NULL, this->left_, loc);
5550 inserter->insert(temp);
5551 this->left_ = Expression::make_temporary_reference(temp, loc);
5552 }
545ab43b 5553 if (!this->right_->is_variable() && !this->right_->is_constant())
a32698ee 5554 {
5555 temp =
5556 Statement::make_temporary(NULL, this->right_, loc);
5557 this->right_ = Expression::make_temporary_reference(temp, loc);
5558 inserter->insert(temp);
5559 }
5560 }
5561 return this;
5562}
5563
5564
e9d3367e 5565// Return the address of EXPR, cast to unsafe.Pointer.
5566
5567Expression*
5568Binary_expression::operand_address(Statement_inserter* inserter,
5569 Expression* expr)
5570{
5571 Location loc = this->location();
5572
5573 if (!expr->is_addressable())
5574 {
5575 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5576 loc);
5577 inserter->insert(temp);
5578 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5579 }
5580 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5581 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5582 Type* void_type = Type::make_void_type();
5583 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5584 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5585}
5586
0c77715b 5587// Return the numeric constant value, if it has one.
e440a328 5588
5589bool
0c77715b 5590Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 5591{
0c77715b 5592 Numeric_constant left_nc;
5593 if (!this->left_->numeric_constant_value(&left_nc))
5594 return false;
5595 Numeric_constant right_nc;
5596 if (!this->right_->numeric_constant_value(&right_nc))
5597 return false;
af7a5274 5598 bool issued_error;
9767e2d3 5599 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
af7a5274 5600 this->location(), nc, &issued_error);
e440a328 5601}
5602
5603// Note that the value is being discarded.
5604
4f2138d7 5605bool
e440a328 5606Binary_expression::do_discarding_value()
5607{
5608 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
4f2138d7 5609 return this->right_->discarding_value();
e440a328 5610 else
4f2138d7 5611 {
5612 this->unused_value_error();
5613 return false;
5614 }
e440a328 5615}
5616
5617// Get type.
5618
5619Type*
5620Binary_expression::do_type()
5621{
5f5fea79 5622 if (this->classification() == EXPRESSION_ERROR)
5623 return Type::make_error_type();
5624
e440a328 5625 switch (this->op_)
5626 {
e440a328 5627 case OPERATOR_EQEQ:
5628 case OPERATOR_NOTEQ:
5629 case OPERATOR_LT:
5630 case OPERATOR_LE:
5631 case OPERATOR_GT:
5632 case OPERATOR_GE:
e90c9dfc 5633 if (this->type_ == NULL)
5634 this->type_ = Type::make_boolean_type();
5635 return this->type_;
e440a328 5636
5637 case OPERATOR_PLUS:
5638 case OPERATOR_MINUS:
5639 case OPERATOR_OR:
5640 case OPERATOR_XOR:
5641 case OPERATOR_MULT:
5642 case OPERATOR_DIV:
5643 case OPERATOR_MOD:
5644 case OPERATOR_AND:
5645 case OPERATOR_BITCLEAR:
e90c9dfc 5646 case OPERATOR_OROR:
5647 case OPERATOR_ANDAND:
e440a328 5648 {
0c77715b 5649 Type* type;
5650 if (!Binary_expression::operation_type(this->op_,
5651 this->left_->type(),
5652 this->right_->type(),
5653 &type))
5654 return Type::make_error_type();
5655 return type;
e440a328 5656 }
5657
5658 case OPERATOR_LSHIFT:
5659 case OPERATOR_RSHIFT:
5660 return this->left_->type();
5661
5662 default:
c3e6f413 5663 go_unreachable();
e440a328 5664 }
5665}
5666
5667// Set type for a binary expression.
5668
5669void
5670Binary_expression::do_determine_type(const Type_context* context)
5671{
5672 Type* tleft = this->left_->type();
5673 Type* tright = this->right_->type();
5674
5675 // Both sides should have the same type, except for the shift
5676 // operations. For a comparison, we should ignore the incoming
5677 // type.
5678
5679 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5680 || this->op_ == OPERATOR_RSHIFT);
5681
5682 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5683 || this->op_ == OPERATOR_NOTEQ
5684 || this->op_ == OPERATOR_LT
5685 || this->op_ == OPERATOR_LE
5686 || this->op_ == OPERATOR_GT
5687 || this->op_ == OPERATOR_GE);
5688
c999c2a7 5689 // For constant expressions, the context of the result is not useful in
5690 // determining the types of the operands. It is only legal to use abstract
5691 // boolean, numeric, and string constants as operands where it is legal to
5692 // use non-abstract boolean, numeric, and string constants, respectively.
5693 // Any issues with the operation will be resolved in the check_types pass.
5694 bool is_constant_expr = (this->left_->is_constant()
5695 && this->right_->is_constant());
5696
e440a328 5697 Type_context subcontext(*context);
5698
9c4ff2ce 5699 if (is_constant_expr && !is_shift_op)
af7a5274 5700 {
5701 subcontext.type = NULL;
5702 subcontext.may_be_abstract = true;
5703 }
5704 else if (is_comparison)
e440a328 5705 {
5706 // In a comparison, the context does not determine the types of
5707 // the operands.
5708 subcontext.type = NULL;
5709 }
5710
5711 // Set the context for the left hand operand.
5712 if (is_shift_op)
5713 {
b40dc774 5714 // The right hand operand of a shift plays no role in
5715 // determining the type of the left hand operand.
e440a328 5716 }
5717 else if (!tleft->is_abstract())
5718 subcontext.type = tleft;
5719 else if (!tright->is_abstract())
5720 subcontext.type = tright;
5721 else if (subcontext.type == NULL)
5722 {
5723 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5724 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5725 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5726 {
5727 // Both sides have an abstract integer, abstract float, or
5728 // abstract complex type. Just let CONTEXT determine
5729 // whether they may remain abstract or not.
5730 }
5731 else if (tleft->complex_type() != NULL)
5732 subcontext.type = tleft;
5733 else if (tright->complex_type() != NULL)
5734 subcontext.type = tright;
5735 else if (tleft->float_type() != NULL)
5736 subcontext.type = tleft;
5737 else if (tright->float_type() != NULL)
5738 subcontext.type = tright;
5739 else
5740 subcontext.type = tleft;
f58a23ae 5741
5742 if (subcontext.type != NULL && !context->may_be_abstract)
5743 subcontext.type = subcontext.type->make_non_abstract_type();
e440a328 5744 }
5745
af7a5274 5746 this->left_->determine_type(&subcontext);
e440a328 5747
e440a328 5748 if (is_shift_op)
5749 {
b40dc774 5750 // We may have inherited an unusable type for the shift operand.
5751 // Give a useful error if that happened.
5752 if (tleft->is_abstract()
5753 && subcontext.type != NULL
8ab6effb 5754 && !subcontext.may_be_abstract
f6bc81e6 5755 && subcontext.type->interface_type() == NULL
8ab6effb 5756 && subcontext.type->integer_type() == NULL)
b40dc774 5757 this->report_error(("invalid context-determined non-integer type "
8ab6effb 5758 "for left operand of shift"));
b40dc774 5759
5760 // The context for the right hand operand is the same as for the
5761 // left hand operand, except for a shift operator.
e440a328 5762 subcontext.type = Type::lookup_integer_type("uint");
5763 subcontext.may_be_abstract = false;
5764 }
5765
af7a5274 5766 this->right_->determine_type(&subcontext);
e90c9dfc 5767
5768 if (is_comparison)
5769 {
5770 if (this->type_ != NULL && !this->type_->is_abstract())
5771 ;
5772 else if (context->type != NULL && context->type->is_boolean_type())
5773 this->type_ = context->type;
5774 else if (!context->may_be_abstract)
5775 this->type_ = Type::lookup_bool_type();
5776 }
e440a328 5777}
5778
5779// Report an error if the binary operator OP does not support TYPE.
be8b5eee 5780// OTYPE is the type of the other operand. Return whether the
5781// operation is OK. This should not be used for shift.
e440a328 5782
5783bool
be8b5eee 5784Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
b13c66cd 5785 Location location)
e440a328 5786{
5787 switch (op)
5788 {
5789 case OPERATOR_OROR:
5790 case OPERATOR_ANDAND:
c999c2a7 5791 if (!type->is_boolean_type()
5792 || !otype->is_boolean_type())
e440a328 5793 {
631d5788 5794 go_error_at(location, "expected boolean type");
e440a328 5795 return false;
5796 }
5797 break;
5798
5799 case OPERATOR_EQEQ:
5800 case OPERATOR_NOTEQ:
e9d3367e 5801 {
5802 std::string reason;
5803 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
5804 {
631d5788 5805 go_error_at(location, "%s", reason.c_str());
e9d3367e 5806 return false;
5807 }
5808 }
e440a328 5809 break;
5810
5811 case OPERATOR_LT:
5812 case OPERATOR_LE:
5813 case OPERATOR_GT:
5814 case OPERATOR_GE:
e9d3367e 5815 {
5816 std::string reason;
5817 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
5818 {
631d5788 5819 go_error_at(location, "%s", reason.c_str());
e9d3367e 5820 return false;
5821 }
5822 }
e440a328 5823 break;
5824
5825 case OPERATOR_PLUS:
5826 case OPERATOR_PLUSEQ:
c999c2a7 5827 if ((!type->is_numeric_type() && !type->is_string_type())
5828 || (!otype->is_numeric_type() && !otype->is_string_type()))
e440a328 5829 {
631d5788 5830 go_error_at(location,
e440a328 5831 "expected integer, floating, complex, or string type");
5832 return false;
5833 }
5834 break;
5835
5836 case OPERATOR_MINUS:
5837 case OPERATOR_MINUSEQ:
5838 case OPERATOR_MULT:
5839 case OPERATOR_MULTEQ:
5840 case OPERATOR_DIV:
5841 case OPERATOR_DIVEQ:
c999c2a7 5842 if (!type->is_numeric_type() || !otype->is_numeric_type())
e440a328 5843 {
631d5788 5844 go_error_at(location, "expected integer, floating, or complex type");
e440a328 5845 return false;
5846 }
5847 break;
5848
5849 case OPERATOR_MOD:
5850 case OPERATOR_MODEQ:
5851 case OPERATOR_OR:
5852 case OPERATOR_OREQ:
5853 case OPERATOR_AND:
5854 case OPERATOR_ANDEQ:
5855 case OPERATOR_XOR:
5856 case OPERATOR_XOREQ:
5857 case OPERATOR_BITCLEAR:
5858 case OPERATOR_BITCLEAREQ:
c999c2a7 5859 if (type->integer_type() == NULL || otype->integer_type() == NULL)
e440a328 5860 {
631d5788 5861 go_error_at(location, "expected integer type");
e440a328 5862 return false;
5863 }
5864 break;
5865
5866 default:
c3e6f413 5867 go_unreachable();
e440a328 5868 }
5869
5870 return true;
5871}
5872
5873// Check types.
5874
5875void
5876Binary_expression::do_check_types(Gogo*)
5877{
5f5fea79 5878 if (this->classification() == EXPRESSION_ERROR)
5879 return;
5880
e440a328 5881 Type* left_type = this->left_->type();
5882 Type* right_type = this->right_->type();
5c13bd80 5883 if (left_type->is_error() || right_type->is_error())
9fe897ef 5884 {
5885 this->set_is_error();
5886 return;
5887 }
e440a328 5888
5889 if (this->op_ == OPERATOR_EQEQ
5890 || this->op_ == OPERATOR_NOTEQ
5891 || this->op_ == OPERATOR_LT
5892 || this->op_ == OPERATOR_LE
5893 || this->op_ == OPERATOR_GT
5894 || this->op_ == OPERATOR_GE)
5895 {
907c5ecd 5896 if (left_type->is_nil_type() && right_type->is_nil_type())
5897 {
5898 this->report_error(_("invalid comparison of nil with nil"));
5899 return;
5900 }
e440a328 5901 if (!Type::are_assignable(left_type, right_type, NULL)
5902 && !Type::are_assignable(right_type, left_type, NULL))
5903 {
5904 this->report_error(_("incompatible types in binary expression"));
5905 return;
5906 }
5907 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5908 right_type,
e440a328 5909 this->location())
5910 || !Binary_expression::check_operator_type(this->op_, right_type,
be8b5eee 5911 left_type,
e440a328 5912 this->location()))
5913 {
5914 this->set_is_error();
5915 return;
5916 }
5917 }
5918 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5919 {
5920 if (!Type::are_compatible_for_binop(left_type, right_type))
5921 {
5922 this->report_error(_("incompatible types in binary expression"));
5923 return;
5924 }
5925 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5926 right_type,
e440a328 5927 this->location()))
5928 {
5929 this->set_is_error();
5930 return;
5931 }
5c65b19d 5932 if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD)
5933 {
5934 // Division by a zero integer constant is an error.
5935 Numeric_constant rconst;
5936 unsigned long rval;
5937 if (left_type->integer_type() != NULL
5938 && this->right_->numeric_constant_value(&rconst)
5939 && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID
5940 && rval == 0)
5941 {
5942 this->report_error(_("integer division by zero"));
5943 return;
5944 }
5945 }
e440a328 5946 }
5947 else
5948 {
5949 if (left_type->integer_type() == NULL)
5950 this->report_error(_("shift of non-integer operand"));
5951
6b5e0fac 5952 if (right_type->is_string_type())
5953 this->report_error(_("shift count not unsigned integer"));
5954 else if (!right_type->is_abstract()
e440a328 5955 && (right_type->integer_type() == NULL
5956 || !right_type->integer_type()->is_unsigned()))
5957 this->report_error(_("shift count not unsigned integer"));
5958 else
5959 {
0c77715b 5960 Numeric_constant nc;
5961 if (this->right_->numeric_constant_value(&nc))
e440a328 5962 {
0c77715b 5963 mpz_t val;
5964 if (!nc.to_int(&val))
5965 this->report_error(_("shift count not unsigned integer"));
5966 else
a4eba91b 5967 {
0c77715b 5968 if (mpz_sgn(val) < 0)
5969 {
5970 this->report_error(_("negative shift count"));
0c77715b 5971 Location rloc = this->right_->location();
e67508fa 5972 this->right_ = Expression::make_integer_ul(0, right_type,
5973 rloc);
0c77715b 5974 }
5975 mpz_clear(val);
a4eba91b 5976 }
e440a328 5977 }
e440a328 5978 }
5979 }
5980}
5981
ea664253 5982// Get the backend representation for a binary expression.
e440a328 5983
ea664253 5984Bexpression*
5985Binary_expression::do_get_backend(Translate_context* context)
e440a328 5986{
1b1f2abf 5987 Gogo* gogo = context->gogo();
a32698ee 5988 Location loc = this->location();
5989 Type* left_type = this->left_->type();
5990 Type* right_type = this->right_->type();
1b1f2abf 5991
e440a328 5992 bool use_left_type = true;
5993 bool is_shift_op = false;
29a2d1d8 5994 bool is_idiv_op = false;
e440a328 5995 switch (this->op_)
5996 {
5997 case OPERATOR_EQEQ:
5998 case OPERATOR_NOTEQ:
5999 case OPERATOR_LT:
6000 case OPERATOR_LE:
6001 case OPERATOR_GT:
6002 case OPERATOR_GE:
ea664253 6003 return Expression::comparison(context, this->type_, this->op_,
6004 this->left_, this->right_, loc);
e440a328 6005
6006 case OPERATOR_OROR:
e440a328 6007 case OPERATOR_ANDAND:
e440a328 6008 use_left_type = false;
6009 break;
6010 case OPERATOR_PLUS:
e440a328 6011 case OPERATOR_MINUS:
e440a328 6012 case OPERATOR_OR:
e440a328 6013 case OPERATOR_XOR:
e440a328 6014 case OPERATOR_MULT:
e440a328 6015 break;
6016 case OPERATOR_DIV:
a32698ee 6017 if (left_type->float_type() != NULL || left_type->complex_type() != NULL)
6018 break;
729f8831 6019 // Fall through.
e440a328 6020 case OPERATOR_MOD:
29a2d1d8 6021 is_idiv_op = true;
e440a328 6022 break;
6023 case OPERATOR_LSHIFT:
e440a328 6024 case OPERATOR_RSHIFT:
e440a328 6025 is_shift_op = true;
6026 break;
e440a328 6027 case OPERATOR_BITCLEAR:
a32698ee 6028 this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc);
6029 case OPERATOR_AND:
e440a328 6030 break;
6031 default:
c3e6f413 6032 go_unreachable();
e440a328 6033 }
6034
736a16ba 6035 // The only binary operation for string is +, and that should have
6036 // been converted to a String_concat_expression in do_lower.
6037 go_assert(!left_type->is_string_type());
a32698ee 6038
6039 // For complex division Go might want slightly different results than the
6040 // backend implementation provides, so we have our own runtime routine.
1850e20c 6041 if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL)
6042 {
a32698ee 6043 Runtime::Function complex_code;
1850e20c 6044 switch (this->left_->type()->complex_type()->bits())
6045 {
6046 case 64:
a32698ee 6047 complex_code = Runtime::COMPLEX64_DIV;
1850e20c 6048 break;
6049 case 128:
a32698ee 6050 complex_code = Runtime::COMPLEX128_DIV;
1850e20c 6051 break;
6052 default:
6053 go_unreachable();
6054 }
a32698ee 6055 Expression* complex_div =
6056 Runtime::make_call(complex_code, loc, 2, this->left_, this->right_);
ea664253 6057 return complex_div->get_backend(context);
1850e20c 6058 }
6059
ea664253 6060 Bexpression* left = this->left_->get_backend(context);
6061 Bexpression* right = this->right_->get_backend(context);
e440a328 6062
a32698ee 6063 Type* type = use_left_type ? left_type : right_type;
6064 Btype* btype = type->get_backend(gogo);
6065
6066 Bexpression* ret =
6067 gogo->backend()->binary_expression(this->op_, left, right, loc);
6068 ret = gogo->backend()->convert_expression(btype, ret, loc);
e440a328 6069
a32698ee 6070 // Initialize overflow constants.
6071 Bexpression* overflow;
6072 mpz_t zero;
6073 mpz_init_set_ui(zero, 0UL);
6074 mpz_t one;
6075 mpz_init_set_ui(one, 1UL);
6076 mpz_t neg_one;
6077 mpz_init_set_si(neg_one, -1);
e440a328 6078
a32698ee 6079 Btype* left_btype = left_type->get_backend(gogo);
6080 Btype* right_btype = right_type->get_backend(gogo);
e440a328 6081
6082 // In Go, a shift larger than the size of the type is well-defined.
a32698ee 6083 // This is not true in C, so we need to insert a conditional.
e440a328 6084 if (is_shift_op)
6085 {
a32698ee 6086 go_assert(left_type->integer_type() != NULL);
e440a328 6087
a32698ee 6088 int bits = left_type->integer_type()->bits();
a7c5b619 6089
6090 Numeric_constant nc;
6091 unsigned long ul;
6092 if (!this->right_->numeric_constant_value(&nc)
6093 || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID
6094 || ul >= static_cast<unsigned long>(bits))
e440a328 6095 {
a7c5b619 6096 mpz_t bitsval;
6097 mpz_init_set_ui(bitsval, bits);
6098 Bexpression* bits_expr =
6099 gogo->backend()->integer_constant_expression(right_btype, bitsval);
6100 Bexpression* compare =
6101 gogo->backend()->binary_expression(OPERATOR_LT,
6102 right, bits_expr, loc);
6103
6104 Bexpression* zero_expr =
6105 gogo->backend()->integer_constant_expression(left_btype, zero);
6106 overflow = zero_expr;
6107 Bfunction* bfn = context->function()->func_value()->get_decl();
6108 if (this->op_ == OPERATOR_RSHIFT
6109 && !left_type->integer_type()->is_unsigned())
6110 {
6111 Bexpression* neg_expr =
6112 gogo->backend()->binary_expression(OPERATOR_LT, left,
6113 zero_expr, loc);
6114 Bexpression* neg_one_expr =
6115 gogo->backend()->integer_constant_expression(left_btype,
6116 neg_one);
6117 overflow = gogo->backend()->conditional_expression(bfn,
6118 btype,
6119 neg_expr,
6120 neg_one_expr,
6121 zero_expr,
6122 loc);
6123 }
6124 ret = gogo->backend()->conditional_expression(bfn, btype, compare,
6125 ret, overflow, loc);
6126 mpz_clear(bitsval);
29a2d1d8 6127 }
29a2d1d8 6128 }
6129
6130 // Add checks for division by zero and division overflow as needed.
6131 if (is_idiv_op)
6132 {
5c3f3470 6133 if (gogo->check_divide_by_zero())
29a2d1d8 6134 {
6135 // right == 0
a32698ee 6136 Bexpression* zero_expr =
6137 gogo->backend()->integer_constant_expression(right_btype, zero);
6138 Bexpression* check =
6139 gogo->backend()->binary_expression(OPERATOR_EQEQ,
6140 right, zero_expr, loc);
29a2d1d8 6141
a32698ee 6142 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO)
29a2d1d8 6143 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO;
ea664253 6144 Bexpression* crash = gogo->runtime_error(errcode,
6145 loc)->get_backend(context);
29a2d1d8 6146
6147 // right == 0 ? (__go_runtime_error(...), 0) : ret
93715b75 6148 Bfunction* bfn = context->function()->func_value()->get_decl();
6149 ret = gogo->backend()->conditional_expression(bfn, btype,
6150 check, crash,
ea664253 6151 ret, loc);
b13c66cd 6152 }
6153
5c3f3470 6154 if (gogo->check_divide_overflow())
29a2d1d8 6155 {
6156 // right == -1
6157 // FIXME: It would be nice to say that this test is expected
6158 // to return false.
a32698ee 6159
6160 Bexpression* neg_one_expr =
6161 gogo->backend()->integer_constant_expression(right_btype, neg_one);
6162 Bexpression* check =
6163 gogo->backend()->binary_expression(OPERATOR_EQEQ,
6164 right, neg_one_expr, loc);
6165
6166 Bexpression* zero_expr =
6167 gogo->backend()->integer_constant_expression(btype, zero);
6168 Bexpression* one_expr =
6169 gogo->backend()->integer_constant_expression(btype, one);
93715b75 6170 Bfunction* bfn = context->function()->func_value()->get_decl();
a32698ee 6171
6172 if (type->integer_type()->is_unsigned())
29a2d1d8 6173 {
6174 // An unsigned -1 is the largest possible number, so
6175 // dividing is always 1 or 0.
a32698ee 6176
6177 Bexpression* cmp =
6178 gogo->backend()->binary_expression(OPERATOR_EQEQ,
6179 left, right, loc);
29a2d1d8 6180 if (this->op_ == OPERATOR_DIV)
a32698ee 6181 overflow =
93715b75 6182 gogo->backend()->conditional_expression(bfn, btype, cmp,
a32698ee 6183 one_expr, zero_expr,
6184 loc);
29a2d1d8 6185 else
a32698ee 6186 overflow =
93715b75 6187 gogo->backend()->conditional_expression(bfn, btype, cmp,
a32698ee 6188 zero_expr, left,
6189 loc);
29a2d1d8 6190 }
6191 else
6192 {
6193 // Computing left / -1 is the same as computing - left,
6194 // which does not overflow since Go sets -fwrapv.
6195 if (this->op_ == OPERATOR_DIV)
a32698ee 6196 {
6197 Expression* negate_expr =
6198 Expression::make_unary(OPERATOR_MINUS, this->left_, loc);
ea664253 6199 overflow = negate_expr->get_backend(context);
a32698ee 6200 }
29a2d1d8 6201 else
a32698ee 6202 overflow = zero_expr;
29a2d1d8 6203 }
a32698ee 6204 overflow = gogo->backend()->convert_expression(btype, overflow, loc);
29a2d1d8 6205
6206 // right == -1 ? - left : ret
93715b75 6207 ret = gogo->backend()->conditional_expression(bfn, btype,
6208 check, overflow,
a32698ee 6209 ret, loc);
29a2d1d8 6210 }
e440a328 6211 }
6212
a32698ee 6213 mpz_clear(zero);
6214 mpz_clear(one);
6215 mpz_clear(neg_one);
ea664253 6216 return ret;
e440a328 6217}
6218
6219// Export a binary expression.
6220
6221void
6222Binary_expression::do_export(Export* exp) const
6223{
6224 exp->write_c_string("(");
6225 this->left_->export_expression(exp);
6226 switch (this->op_)
6227 {
6228 case OPERATOR_OROR:
6229 exp->write_c_string(" || ");
6230 break;
6231 case OPERATOR_ANDAND:
6232 exp->write_c_string(" && ");
6233 break;
6234 case OPERATOR_EQEQ:
6235 exp->write_c_string(" == ");
6236 break;
6237 case OPERATOR_NOTEQ:
6238 exp->write_c_string(" != ");
6239 break;
6240 case OPERATOR_LT:
6241 exp->write_c_string(" < ");
6242 break;
6243 case OPERATOR_LE:
6244 exp->write_c_string(" <= ");
6245 break;
6246 case OPERATOR_GT:
6247 exp->write_c_string(" > ");
6248 break;
6249 case OPERATOR_GE:
6250 exp->write_c_string(" >= ");
6251 break;
6252 case OPERATOR_PLUS:
6253 exp->write_c_string(" + ");
6254 break;
6255 case OPERATOR_MINUS:
6256 exp->write_c_string(" - ");
6257 break;
6258 case OPERATOR_OR:
6259 exp->write_c_string(" | ");
6260 break;
6261 case OPERATOR_XOR:
6262 exp->write_c_string(" ^ ");
6263 break;
6264 case OPERATOR_MULT:
6265 exp->write_c_string(" * ");
6266 break;
6267 case OPERATOR_DIV:
6268 exp->write_c_string(" / ");
6269 break;
6270 case OPERATOR_MOD:
6271 exp->write_c_string(" % ");
6272 break;
6273 case OPERATOR_LSHIFT:
6274 exp->write_c_string(" << ");
6275 break;
6276 case OPERATOR_RSHIFT:
6277 exp->write_c_string(" >> ");
6278 break;
6279 case OPERATOR_AND:
6280 exp->write_c_string(" & ");
6281 break;
6282 case OPERATOR_BITCLEAR:
6283 exp->write_c_string(" &^ ");
6284 break;
6285 default:
c3e6f413 6286 go_unreachable();
e440a328 6287 }
6288 this->right_->export_expression(exp);
6289 exp->write_c_string(")");
6290}
6291
6292// Import a binary expression.
6293
6294Expression*
6295Binary_expression::do_import(Import* imp)
6296{
6297 imp->require_c_string("(");
6298
6299 Expression* left = Expression::import_expression(imp);
6300
6301 Operator op;
6302 if (imp->match_c_string(" || "))
6303 {
6304 op = OPERATOR_OROR;
6305 imp->advance(4);
6306 }
6307 else if (imp->match_c_string(" && "))
6308 {
6309 op = OPERATOR_ANDAND;
6310 imp->advance(4);
6311 }
6312 else if (imp->match_c_string(" == "))
6313 {
6314 op = OPERATOR_EQEQ;
6315 imp->advance(4);
6316 }
6317 else if (imp->match_c_string(" != "))
6318 {
6319 op = OPERATOR_NOTEQ;
6320 imp->advance(4);
6321 }
6322 else if (imp->match_c_string(" < "))
6323 {
6324 op = OPERATOR_LT;
6325 imp->advance(3);
6326 }
6327 else if (imp->match_c_string(" <= "))
6328 {
6329 op = OPERATOR_LE;
6330 imp->advance(4);
6331 }
6332 else if (imp->match_c_string(" > "))
6333 {
6334 op = OPERATOR_GT;
6335 imp->advance(3);
6336 }
6337 else if (imp->match_c_string(" >= "))
6338 {
6339 op = OPERATOR_GE;
6340 imp->advance(4);
6341 }
6342 else if (imp->match_c_string(" + "))
6343 {
6344 op = OPERATOR_PLUS;
6345 imp->advance(3);
6346 }
6347 else if (imp->match_c_string(" - "))
6348 {
6349 op = OPERATOR_MINUS;
6350 imp->advance(3);
6351 }
6352 else if (imp->match_c_string(" | "))
6353 {
6354 op = OPERATOR_OR;
6355 imp->advance(3);
6356 }
6357 else if (imp->match_c_string(" ^ "))
6358 {
6359 op = OPERATOR_XOR;
6360 imp->advance(3);
6361 }
6362 else if (imp->match_c_string(" * "))
6363 {
6364 op = OPERATOR_MULT;
6365 imp->advance(3);
6366 }
6367 else if (imp->match_c_string(" / "))
6368 {
6369 op = OPERATOR_DIV;
6370 imp->advance(3);
6371 }
6372 else if (imp->match_c_string(" % "))
6373 {
6374 op = OPERATOR_MOD;
6375 imp->advance(3);
6376 }
6377 else if (imp->match_c_string(" << "))
6378 {
6379 op = OPERATOR_LSHIFT;
6380 imp->advance(4);
6381 }
6382 else if (imp->match_c_string(" >> "))
6383 {
6384 op = OPERATOR_RSHIFT;
6385 imp->advance(4);
6386 }
6387 else if (imp->match_c_string(" & "))
6388 {
6389 op = OPERATOR_AND;
6390 imp->advance(3);
6391 }
6392 else if (imp->match_c_string(" &^ "))
6393 {
6394 op = OPERATOR_BITCLEAR;
6395 imp->advance(4);
6396 }
6397 else
6398 {
631d5788 6399 go_error_at(imp->location(), "unrecognized binary operator");
e440a328 6400 return Expression::make_error(imp->location());
6401 }
6402
6403 Expression* right = Expression::import_expression(imp);
6404
6405 imp->require_c_string(")");
6406
6407 return Expression::make_binary(op, left, right, imp->location());
6408}
6409
d751bb78 6410// Dump ast representation of a binary expression.
6411
6412void
6413Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6414{
6415 ast_dump_context->ostream() << "(";
6416 ast_dump_context->dump_expression(this->left_);
6417 ast_dump_context->ostream() << " ";
6418 ast_dump_context->dump_operator(this->op_);
6419 ast_dump_context->ostream() << " ";
6420 ast_dump_context->dump_expression(this->right_);
6421 ast_dump_context->ostream() << ") ";
6422}
6423
e440a328 6424// Make a binary expression.
6425
6426Expression*
6427Expression::make_binary(Operator op, Expression* left, Expression* right,
b13c66cd 6428 Location location)
e440a328 6429{
6430 return new Binary_expression(op, left, right, location);
6431}
6432
6433// Implement a comparison.
6434
a32698ee 6435Bexpression*
6436Expression::comparison(Translate_context* context, Type* result_type,
6437 Operator op, Expression* left, Expression* right,
6438 Location location)
e440a328 6439{
2387f644 6440 Type* left_type = left->type();
6441 Type* right_type = right->type();
ceeb12d7 6442
e67508fa 6443 Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
1b1f2abf 6444
15c67ee2 6445 if (left_type->is_string_type() && right_type->is_string_type())
e440a328 6446 {
6098d6cb 6447 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
6448 {
6449 left = Runtime::make_call(Runtime::EQSTRING, location, 2,
6450 left, right);
6451 right = Expression::make_boolean(true, location);
6452 }
6453 else
6454 {
6455 left = Runtime::make_call(Runtime::CMPSTRING, location, 2,
6456 left, right);
6457 right = zexpr;
6458 }
e440a328 6459 }
15c67ee2 6460 else if ((left_type->interface_type() != NULL
6461 && right_type->interface_type() == NULL
6462 && !right_type->is_nil_type())
6463 || (left_type->interface_type() == NULL
6464 && !left_type->is_nil_type()
6465 && right_type->interface_type() != NULL))
e440a328 6466 {
6467 // Comparing an interface value to a non-interface value.
6468 if (left_type->interface_type() == NULL)
6469 {
6470 std::swap(left_type, right_type);
2387f644 6471 std::swap(left, right);
e440a328 6472 }
6473
6474 // The right operand is not an interface. We need to take its
6475 // address if it is not a pointer.
ceeb12d7 6476 Expression* pointer_arg = NULL;
e440a328 6477 if (right_type->points_to() != NULL)
2387f644 6478 pointer_arg = right;
e440a328 6479 else
6480 {
2387f644 6481 go_assert(right->is_addressable());
6482 pointer_arg = Expression::make_unary(OPERATOR_AND, right,
ceeb12d7 6483 location);
e440a328 6484 }
e440a328 6485
2387f644 6486 Expression* descriptor =
6487 Expression::make_type_descriptor(right_type, location);
6488 left =
ceeb12d7 6489 Runtime::make_call((left_type->interface_type()->is_empty()
6098d6cb 6490 ? Runtime::EFACEVALEQ
6491 : Runtime::IFACEVALEQ),
2387f644 6492 location, 3, left, descriptor,
ceeb12d7 6493 pointer_arg);
6098d6cb 6494 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6495 right = Expression::make_boolean(true, location);
e440a328 6496 }
6497 else if (left_type->interface_type() != NULL
6498 && right_type->interface_type() != NULL)
6499 {
ceeb12d7 6500 Runtime::Function compare_function;
739bad04 6501 if (left_type->interface_type()->is_empty()
6502 && right_type->interface_type()->is_empty())
6098d6cb 6503 compare_function = Runtime::EFACEEQ;
739bad04 6504 else if (!left_type->interface_type()->is_empty()
6505 && !right_type->interface_type()->is_empty())
6098d6cb 6506 compare_function = Runtime::IFACEEQ;
739bad04 6507 else
6508 {
6509 if (left_type->interface_type()->is_empty())
6510 {
739bad04 6511 std::swap(left_type, right_type);
2387f644 6512 std::swap(left, right);
739bad04 6513 }
c484d925 6514 go_assert(!left_type->interface_type()->is_empty());
6515 go_assert(right_type->interface_type()->is_empty());
6098d6cb 6516 compare_function = Runtime::IFACEEFACEEQ;
739bad04 6517 }
6518
2387f644 6519 left = Runtime::make_call(compare_function, location, 2, left, right);
6098d6cb 6520 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6521 right = Expression::make_boolean(true, location);
e440a328 6522 }
6523
6524 if (left_type->is_nil_type()
6525 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6526 {
6527 std::swap(left_type, right_type);
2387f644 6528 std::swap(left, right);
e440a328 6529 }
6530
6531 if (right_type->is_nil_type())
6532 {
2387f644 6533 right = Expression::make_nil(location);
e440a328 6534 if (left_type->array_type() != NULL
6535 && left_type->array_type()->length() == NULL)
6536 {
6537 Array_type* at = left_type->array_type();
44dbe1d7 6538 bool is_lvalue = false;
6539 left = at->get_value_pointer(context->gogo(), left, is_lvalue);
e440a328 6540 }
6541 else if (left_type->interface_type() != NULL)
6542 {
6543 // An interface is nil if the first field is nil.
2387f644 6544 left = Expression::make_field_reference(left, 0, location);
e440a328 6545 }
6546 }
6547
ea664253 6548 Bexpression* left_bexpr = left->get_backend(context);
6549 Bexpression* right_bexpr = right->get_backend(context);
e90c9dfc 6550
a32698ee 6551 Gogo* gogo = context->gogo();
6552 Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr,
6553 right_bexpr, location);
6554 if (result_type != NULL)
6555 ret = gogo->backend()->convert_expression(result_type->get_backend(gogo),
6556 ret, location);
e440a328 6557 return ret;
6558}
6559
736a16ba 6560// Class String_concat_expression.
6561
6562bool
6563String_concat_expression::do_is_constant() const
6564{
6565 for (Expression_list::const_iterator pe = this->exprs_->begin();
6566 pe != this->exprs_->end();
6567 ++pe)
6568 {
6569 if (!(*pe)->is_constant())
6570 return false;
6571 }
6572 return true;
6573}
6574
6575bool
3ae06f68 6576String_concat_expression::do_is_static_initializer() const
736a16ba 6577{
6578 for (Expression_list::const_iterator pe = this->exprs_->begin();
6579 pe != this->exprs_->end();
6580 ++pe)
6581 {
3ae06f68 6582 if (!(*pe)->is_static_initializer())
736a16ba 6583 return false;
6584 }
6585 return true;
6586}
6587
6588Type*
6589String_concat_expression::do_type()
6590{
6591 Type* t = this->exprs_->front()->type();
6592 Expression_list::iterator pe = this->exprs_->begin();
6593 ++pe;
6594 for (; pe != this->exprs_->end(); ++pe)
6595 {
6596 Type* t1;
6597 if (!Binary_expression::operation_type(OPERATOR_PLUS, t,
6598 (*pe)->type(),
6599 &t1))
6600 return Type::make_error_type();
6601 t = t1;
6602 }
6603 return t;
6604}
6605
6606void
6607String_concat_expression::do_determine_type(const Type_context* context)
6608{
6609 Type_context subcontext(*context);
6610 for (Expression_list::iterator pe = this->exprs_->begin();
6611 pe != this->exprs_->end();
6612 ++pe)
6613 {
6614 Type* t = (*pe)->type();
6615 if (!t->is_abstract())
6616 {
6617 subcontext.type = t;
6618 break;
6619 }
6620 }
6621 if (subcontext.type == NULL)
6622 subcontext.type = this->exprs_->front()->type();
6623 for (Expression_list::iterator pe = this->exprs_->begin();
6624 pe != this->exprs_->end();
6625 ++pe)
6626 (*pe)->determine_type(&subcontext);
6627}
6628
6629void
6630String_concat_expression::do_check_types(Gogo*)
6631{
6632 if (this->is_error_expression())
6633 return;
6634 Type* t = this->exprs_->front()->type();
6635 if (t->is_error())
6636 {
6637 this->set_is_error();
6638 return;
6639 }
6640 Expression_list::iterator pe = this->exprs_->begin();
6641 ++pe;
6642 for (; pe != this->exprs_->end(); ++pe)
6643 {
6644 Type* t1 = (*pe)->type();
6645 if (!Type::are_compatible_for_binop(t, t1))
6646 {
6647 this->report_error("incompatible types in binary expression");
6648 return;
6649 }
6650 if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1,
6651 this->location()))
6652 {
6653 this->set_is_error();
6654 return;
6655 }
6656 }
6657}
6658
6659Expression*
6660String_concat_expression::do_flatten(Gogo*, Named_object*,
6661 Statement_inserter*)
6662{
6663 if (this->is_error_expression())
6664 return this;
6665 Location loc = this->location();
6666 Type* type = this->type();
6667 Expression* nil_arg = Expression::make_nil(loc);
6668 Expression* call;
6669 switch (this->exprs_->size())
6670 {
6671 case 0: case 1:
6672 go_unreachable();
6673
6674 case 2: case 3: case 4: case 5:
6675 {
6676 Expression* len = Expression::make_integer_ul(this->exprs_->size(),
6677 NULL, loc);
6678 Array_type* arg_type = Type::make_array_type(type, len);
6679 arg_type->set_is_array_incomparable();
6680 Expression* arg =
6681 Expression::make_array_composite_literal(arg_type, this->exprs_,
6682 loc);
6683 Runtime::Function code;
6684 switch (this->exprs_->size())
6685 {
6686 default:
6687 go_unreachable();
6688 case 2:
6689 code = Runtime::CONCATSTRING2;
6690 break;
6691 case 3:
6692 code = Runtime::CONCATSTRING3;
6693 break;
6694 case 4:
6695 code = Runtime::CONCATSTRING4;
6696 break;
6697 case 5:
6698 code = Runtime::CONCATSTRING5;
6699 break;
6700 }
6701 call = Runtime::make_call(code, loc, 2, nil_arg, arg);
6702 }
6703 break;
6704
6705 default:
6706 {
6707 Type* arg_type = Type::make_array_type(type, NULL);
6708 Slice_construction_expression* sce =
6709 Expression::make_slice_composite_literal(arg_type, this->exprs_,
6710 loc);
6711 sce->set_storage_does_not_escape();
6712 call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg,
6713 sce);
6714 }
6715 break;
6716 }
6717
6718 return Expression::make_cast(type, call, loc);
6719}
6720
6721void
6722String_concat_expression::do_dump_expression(
6723 Ast_dump_context* ast_dump_context) const
6724{
6725 ast_dump_context->ostream() << "concat(";
6726 ast_dump_context->dump_expression_list(this->exprs_, false);
6727 ast_dump_context->ostream() << ")";
6728}
6729
6730Expression*
6731Expression::make_string_concat(Expression_list* exprs)
6732{
6733 return new String_concat_expression(exprs);
6734}
6735
e440a328 6736// Class Bound_method_expression.
6737
6738// Traversal.
6739
6740int
6741Bound_method_expression::do_traverse(Traverse* traverse)
6742{
e0659c9e 6743 return Expression::traverse(&this->expr_, traverse);
e440a328 6744}
6745
6746// Return the type of a bound method expression. The type of this
0afbb937 6747// object is simply the type of the method with no receiver.
e440a328 6748
6749Type*
6750Bound_method_expression::do_type()
6751{
0afbb937 6752 Named_object* fn = this->method_->named_object();
6753 Function_type* fntype;
6754 if (fn->is_function())
6755 fntype = fn->func_value()->type();
6756 else if (fn->is_function_declaration())
6757 fntype = fn->func_declaration_value()->type();
e0659c9e 6758 else
6759 return Type::make_error_type();
0afbb937 6760 return fntype->copy_without_receiver();
e440a328 6761}
6762
6763// Determine the types of a method expression.
6764
6765void
6766Bound_method_expression::do_determine_type(const Type_context*)
6767{
0afbb937 6768 Named_object* fn = this->method_->named_object();
6769 Function_type* fntype;
6770 if (fn->is_function())
6771 fntype = fn->func_value()->type();
6772 else if (fn->is_function_declaration())
6773 fntype = fn->func_declaration_value()->type();
6774 else
6775 fntype = NULL;
e440a328 6776 if (fntype == NULL || !fntype->is_method())
6777 this->expr_->determine_type_no_context();
6778 else
6779 {
6780 Type_context subcontext(fntype->receiver()->type(), false);
6781 this->expr_->determine_type(&subcontext);
6782 }
6783}
6784
6785// Check the types of a method expression.
6786
6787void
6788Bound_method_expression::do_check_types(Gogo*)
6789{
0afbb937 6790 Named_object* fn = this->method_->named_object();
6791 if (!fn->is_function() && !fn->is_function_declaration())
6792 {
6793 this->report_error(_("object is not a method"));
6794 return;
6795 }
6796
6797 Function_type* fntype;
6798 if (fn->is_function())
6799 fntype = fn->func_value()->type();
6800 else if (fn->is_function_declaration())
6801 fntype = fn->func_declaration_value()->type();
e440a328 6802 else
0afbb937 6803 go_unreachable();
6804 Type* rtype = fntype->receiver()->type()->deref();
6805 Type* etype = (this->expr_type_ != NULL
6806 ? this->expr_type_
6807 : this->expr_->type());
6808 etype = etype->deref();
6809 if (!Type::are_identical(rtype, etype, true, NULL))
6810 this->report_error(_("method type does not match object type"));
6811}
6812
6813// If a bound method expression is not simply called, then it is
6814// represented as a closure. The closure will hold a single variable,
6815// the receiver to pass to the method. The function will be a simple
6816// thunk that pulls that value from the closure and calls the method
6817// with the remaining arguments.
6818//
6819// Because method values are not common, we don't build all thunks for
6820// every methods, but instead only build them as we need them. In
6821// particular, we even build them on demand for methods defined in
6822// other packages.
6823
6824Bound_method_expression::Method_value_thunks
6825 Bound_method_expression::method_value_thunks;
6826
6827// Find or create the thunk for METHOD.
6828
6829Named_object*
6830Bound_method_expression::create_thunk(Gogo* gogo, const Method* method,
6831 Named_object* fn)
6832{
6833 std::pair<Named_object*, Named_object*> val(fn, NULL);
6834 std::pair<Method_value_thunks::iterator, bool> ins =
6835 Bound_method_expression::method_value_thunks.insert(val);
6836 if (!ins.second)
6837 {
6838 // We have seen this method before.
6839 go_assert(ins.first->second != NULL);
6840 return ins.first->second;
6841 }
6842
6843 Location loc = fn->location();
6844
6845 Function_type* orig_fntype;
6846 if (fn->is_function())
6847 orig_fntype = fn->func_value()->type();
6848 else if (fn->is_function_declaration())
6849 orig_fntype = fn->func_declaration_value()->type();
6850 else
6851 orig_fntype = NULL;
6852
6853 if (orig_fntype == NULL || !orig_fntype->is_method())
e440a328 6854 {
0afbb937 6855 ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name());
6856 return ins.first->second;
e440a328 6857 }
0afbb937 6858
6859 Struct_field_list* sfl = new Struct_field_list();
f8bdf81a 6860 // The type here is wrong--it should be the C function type. But it
6861 // doesn't really matter.
0afbb937 6862 Type* vt = Type::make_pointer_type(Type::make_void_type());
6863 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
6864 sfl->push_back(Struct_field(Typed_identifier("val.1",
6865 orig_fntype->receiver()->type(),
6866 loc)));
6bf4793c 6867 Struct_type* st = Type::make_struct_type(sfl, loc);
6868 st->set_is_struct_incomparable();
6869 Type* closure_type = Type::make_pointer_type(st);
0afbb937 6870
f8bdf81a 6871 Function_type* new_fntype = orig_fntype->copy_with_names();
0afbb937 6872
da244e59 6873 std::string thunk_name = Gogo::thunk_name();
6874 Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
0afbb937 6875 false, loc);
6876
f8bdf81a 6877 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
6878 cvar->set_is_used();
1ecc6157 6879 cvar->set_is_closure();
da244e59 6880 Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
6881 NULL, cvar);
f8bdf81a 6882 new_no->func_value()->set_closure_var(cp);
0afbb937 6883
f8bdf81a 6884 gogo->start_block(loc);
0afbb937 6885
6886 // Field 0 of the closure is the function code pointer, field 1 is
6887 // the value on which to invoke the method.
6888 Expression* arg = Expression::make_var_reference(cp, loc);
f614ea8b 6889 arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc);
0afbb937 6890 arg = Expression::make_field_reference(arg, 1, loc);
6891
6892 Expression* bme = Expression::make_bound_method(arg, method, fn, loc);
6893
6894 const Typed_identifier_list* orig_params = orig_fntype->parameters();
6895 Expression_list* args;
6896 if (orig_params == NULL || orig_params->empty())
6897 args = NULL;
6898 else
6899 {
6900 const Typed_identifier_list* new_params = new_fntype->parameters();
6901 args = new Expression_list();
6902 for (Typed_identifier_list::const_iterator p = new_params->begin();
f8bdf81a 6903 p != new_params->end();
0afbb937 6904 ++p)
6905 {
6906 Named_object* p_no = gogo->lookup(p->name(), NULL);
6907 go_assert(p_no != NULL
6908 && p_no->is_variable()
6909 && p_no->var_value()->is_parameter());
6910 args->push_back(Expression::make_var_reference(p_no, loc));
6911 }
6912 }
6913
6914 Call_expression* call = Expression::make_call(bme, args,
6915 orig_fntype->is_varargs(),
6916 loc);
6917 call->set_varargs_are_lowered();
6918
6919 Statement* s = Statement::make_return_from_call(call, loc);
6920 gogo->add_statement(s);
6921 Block* b = gogo->finish_block(loc);
6922 gogo->add_block(b, loc);
6923 gogo->lower_block(new_no, b);
a32698ee 6924 gogo->flatten_block(new_no, b);
0afbb937 6925 gogo->finish_function(loc);
6926
6927 ins.first->second = new_no;
6928 return new_no;
6929}
6930
6931// Return an expression to check *REF for nil while dereferencing
6932// according to FIELD_INDEXES. Update *REF to build up the field
6933// reference. This is a static function so that we don't have to
6934// worry about declaring Field_indexes in expressions.h.
6935
6936static Expression*
6937bme_check_nil(const Method::Field_indexes* field_indexes, Location loc,
6938 Expression** ref)
6939{
6940 if (field_indexes == NULL)
6941 return Expression::make_boolean(false, loc);
6942 Expression* cond = bme_check_nil(field_indexes->next, loc, ref);
6943 Struct_type* stype = (*ref)->type()->deref()->struct_type();
6944 go_assert(stype != NULL
6945 && field_indexes->field_index < stype->field_count());
6946 if ((*ref)->type()->struct_type() == NULL)
6947 {
6948 go_assert((*ref)->type()->points_to() != NULL);
6949 Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref,
6950 Expression::make_nil(loc),
6951 loc);
6952 cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc);
f614ea8b 6953 *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT,
6954 loc);
0afbb937 6955 go_assert((*ref)->type()->struct_type() == stype);
6956 }
6957 *ref = Expression::make_field_reference(*ref, field_indexes->field_index,
6958 loc);
6959 return cond;
e440a328 6960}
6961
cd39797e 6962// Flatten a method value into a struct with nil checks. We can't do
6963// this in the lowering phase, because if the method value is called
6964// directly we don't need a thunk. That case will have been handled
6965// by Call_expression::do_lower, so if we get here then we do need a
6966// thunk.
e440a328 6967
cd39797e 6968Expression*
6969Bound_method_expression::do_flatten(Gogo* gogo, Named_object*,
6970 Statement_inserter* inserter)
e440a328 6971{
cd39797e 6972 Location loc = this->location();
6973
6974 Named_object* thunk = Bound_method_expression::create_thunk(gogo,
0afbb937 6975 this->method_,
6976 this->function_);
6977 if (thunk->is_erroneous())
6978 {
6979 go_assert(saw_errors());
cd39797e 6980 return Expression::make_error(loc);
0afbb937 6981 }
6982
cd39797e 6983 // Force the expression into a variable. This is only necessary if
6984 // we are going to do nil checks below, but it's easy enough to
6985 // always do it.
6986 Expression* expr = this->expr_;
6987 if (!expr->is_variable())
6988 {
6989 Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc);
6990 inserter->insert(etemp);
6991 expr = Expression::make_temporary_reference(etemp, loc);
6992 }
0afbb937 6993
6994 // If the method expects a value, and we have a pointer, we need to
6995 // dereference the pointer.
6996
6997 Named_object* fn = this->method_->named_object();
cd39797e 6998 Function_type *fntype;
0afbb937 6999 if (fn->is_function())
7000 fntype = fn->func_value()->type();
7001 else if (fn->is_function_declaration())
7002 fntype = fn->func_declaration_value()->type();
7003 else
7004 go_unreachable();
7005
cd39797e 7006 Expression* val = expr;
0afbb937 7007 if (fntype->receiver()->type()->points_to() == NULL
7008 && val->type()->points_to() != NULL)
f614ea8b 7009 val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc);
0afbb937 7010
7011 // Note that we are ignoring this->expr_type_ here. The thunk will
7012 // expect a closure whose second field has type this->expr_type_ (if
7013 // that is not NULL). We are going to pass it a closure whose
7014 // second field has type this->expr_->type(). Since
7015 // this->expr_type_ is only not-NULL for pointer types, we can get
7016 // away with this.
7017
7018 Struct_field_list* fields = new Struct_field_list();
7019 fields->push_back(Struct_field(Typed_identifier("fn.0",
7020 thunk->func_value()->type(),
7021 loc)));
7022 fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc)));
7023 Struct_type* st = Type::make_struct_type(fields, loc);
6bf4793c 7024 st->set_is_struct_incomparable();
0afbb937 7025
7026 Expression_list* vals = new Expression_list();
7027 vals->push_back(Expression::make_func_code_reference(thunk, loc));
7028 vals->push_back(val);
7029
7030 Expression* ret = Expression::make_struct_composite_literal(st, vals, loc);
0afbb937 7031
cd39797e 7032 if (!gogo->compiling_runtime() || gogo->package_name() != "runtime")
7033 ret = Expression::make_heap_expression(ret, loc);
7034 else
7035 {
7036 // When compiling the runtime, method closures do not escape.
7037 // When escape analysis becomes the default, and applies to
7038 // method closures, this should be changed to make it an error
7039 // if a method closure escapes.
7040 Temporary_statement* ctemp = Statement::make_temporary(st, ret, loc);
7041 inserter->insert(ctemp);
7042 ret = Expression::make_temporary_reference(ctemp, loc);
7043 ret = Expression::make_unary(OPERATOR_AND, ret, loc);
7044 ret->unary_expression()->set_does_not_escape();
7045 }
7046
7047 // If necessary, check whether the expression or any embedded
7048 // pointers are nil.
0afbb937 7049
df7ef1fd 7050 Expression* nil_check = NULL;
0afbb937 7051 if (this->method_->field_indexes() != NULL)
7052 {
0afbb937 7053 Expression* ref = expr;
7054 nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref);
7055 expr = ref;
7056 }
7057
7058 if (this->method_->is_value_method() && expr->type()->points_to() != NULL)
7059 {
7060 Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr,
7061 Expression::make_nil(loc),
7062 loc);
7063 if (nil_check == NULL)
7064 nil_check = n;
7065 else
7066 nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc);
7067 }
7068
7069 if (nil_check != NULL)
7070 {
cd39797e 7071 Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
7072 loc);
7073 // Fix the type of the conditional expression by pretending to
7074 // evaluate to RET either way through the conditional.
7075 crash = Expression::make_compound(crash, ret, loc);
7076 ret = Expression::make_conditional(nil_check, crash, ret, loc);
7077 }
7078
7079 // RET is a pointer to a struct, but we want a function type.
7080 ret = Expression::make_unsafe_cast(this->type(), ret, loc);
7081
7082 return ret;
e440a328 7083}
7084
d751bb78 7085// Dump ast representation of a bound method expression.
7086
7087void
7088Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
7089 const
7090{
7091 if (this->expr_type_ != NULL)
7092 ast_dump_context->ostream() << "(";
7093 ast_dump_context->dump_expression(this->expr_);
7094 if (this->expr_type_ != NULL)
7095 {
7096 ast_dump_context->ostream() << ":";
7097 ast_dump_context->dump_type(this->expr_type_);
7098 ast_dump_context->ostream() << ")";
7099 }
7100
0afbb937 7101 ast_dump_context->ostream() << "." << this->function_->name();
d751bb78 7102}
7103
e440a328 7104// Make a method expression.
7105
7106Bound_method_expression*
0afbb937 7107Expression::make_bound_method(Expression* expr, const Method* method,
7108 Named_object* function, Location location)
e440a328 7109{
0afbb937 7110 return new Bound_method_expression(expr, method, function, location);
e440a328 7111}
7112
7113// Class Builtin_call_expression. This is used for a call to a
7114// builtin function.
7115
e440a328 7116Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7117 Expression* fn,
7118 Expression_list* args,
7119 bool is_varargs,
b13c66cd 7120 Location location)
e440a328 7121 : Call_expression(fn, args, is_varargs, location),
6334270b 7122 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false),
7123 recover_arg_is_set_(false)
e440a328 7124{
7125 Func_expression* fnexp = this->fn()->func_expression();
79651b1f 7126 if (fnexp == NULL)
7127 {
7128 this->code_ = BUILTIN_INVALID;
7129 return;
7130 }
e440a328 7131 const std::string& name(fnexp->named_object()->name());
7132 if (name == "append")
7133 this->code_ = BUILTIN_APPEND;
7134 else if (name == "cap")
7135 this->code_ = BUILTIN_CAP;
7136 else if (name == "close")
7137 this->code_ = BUILTIN_CLOSE;
48080209 7138 else if (name == "complex")
7139 this->code_ = BUILTIN_COMPLEX;
e440a328 7140 else if (name == "copy")
7141 this->code_ = BUILTIN_COPY;
1cce762f 7142 else if (name == "delete")
7143 this->code_ = BUILTIN_DELETE;
e440a328 7144 else if (name == "imag")
7145 this->code_ = BUILTIN_IMAG;
7146 else if (name == "len")
7147 this->code_ = BUILTIN_LEN;
7148 else if (name == "make")
7149 this->code_ = BUILTIN_MAKE;
7150 else if (name == "new")
7151 this->code_ = BUILTIN_NEW;
7152 else if (name == "panic")
7153 this->code_ = BUILTIN_PANIC;
7154 else if (name == "print")
7155 this->code_ = BUILTIN_PRINT;
7156 else if (name == "println")
7157 this->code_ = BUILTIN_PRINTLN;
7158 else if (name == "real")
7159 this->code_ = BUILTIN_REAL;
7160 else if (name == "recover")
7161 this->code_ = BUILTIN_RECOVER;
7162 else if (name == "Alignof")
7163 this->code_ = BUILTIN_ALIGNOF;
7164 else if (name == "Offsetof")
7165 this->code_ = BUILTIN_OFFSETOF;
7166 else if (name == "Sizeof")
7167 this->code_ = BUILTIN_SIZEOF;
7168 else
c3e6f413 7169 go_unreachable();
e440a328 7170}
7171
7172// Return whether this is a call to recover. This is a virtual
7173// function called from the parent class.
7174
7175bool
7176Builtin_call_expression::do_is_recover_call() const
7177{
7178 if (this->classification() == EXPRESSION_ERROR)
7179 return false;
7180 return this->code_ == BUILTIN_RECOVER;
7181}
7182
7183// Set the argument for a call to recover.
7184
7185void
7186Builtin_call_expression::do_set_recover_arg(Expression* arg)
7187{
7188 const Expression_list* args = this->args();
c484d925 7189 go_assert(args == NULL || args->empty());
e440a328 7190 Expression_list* new_args = new Expression_list();
7191 new_args->push_back(arg);
7192 this->set_args(new_args);
6334270b 7193 this->recover_arg_is_set_ = true;
e440a328 7194}
7195
e440a328 7196// Lower a builtin call expression. This turns new and make into
7197// specific expressions. We also convert to a constant if we can.
7198
7199Expression*
321e5ad2 7200Builtin_call_expression::do_lower(Gogo*, Named_object* function,
ceeb4318 7201 Statement_inserter* inserter, int)
e440a328 7202{
79651b1f 7203 if (this->is_error_expression())
a9182619 7204 return this;
7205
b13c66cd 7206 Location loc = this->location();
1cce762f 7207
a8725655 7208 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7209 {
7210 this->report_error(_("invalid use of %<...%> with builtin function"));
1cce762f 7211 return Expression::make_error(loc);
a8725655 7212 }
7213
393ba00b 7214 if (this->code_ == BUILTIN_OFFSETOF)
7215 {
7216 Expression* arg = this->one_arg();
12e69faa 7217
7218 if (arg->bound_method_expression() != NULL
7219 || arg->interface_field_reference_expression() != NULL)
7220 {
7221 this->report_error(_("invalid use of method value as argument "
7222 "of Offsetof"));
7223 return this;
7224 }
7225
393ba00b 7226 Field_reference_expression* farg = arg->field_reference_expression();
7227 while (farg != NULL)
7228 {
7229 if (!farg->implicit())
7230 break;
7231 // When the selector refers to an embedded field,
7232 // it must not be reached through pointer indirections.
7233 if (farg->expr()->deref() != farg->expr())
7234 {
12e69faa 7235 this->report_error(_("argument of Offsetof implies "
7236 "indirection of an embedded field"));
393ba00b 7237 return this;
7238 }
7239 // Go up until we reach the original base.
7240 farg = farg->expr()->field_reference_expression();
7241 }
7242 }
7243
1cce762f 7244 if (this->is_constant())
e440a328 7245 {
0c77715b 7246 Numeric_constant nc;
7247 if (this->numeric_constant_value(&nc))
7248 return nc.expression(loc);
e440a328 7249 }
1cce762f 7250
7251 switch (this->code_)
e440a328 7252 {
1cce762f 7253 default:
7254 break;
7255
7256 case BUILTIN_NEW:
7257 {
7258 const Expression_list* args = this->args();
7259 if (args == NULL || args->size() < 1)
7260 this->report_error(_("not enough arguments"));
7261 else if (args->size() > 1)
7262 this->report_error(_("too many arguments"));
7263 else
7264 {
7265 Expression* arg = args->front();
7266 if (!arg->is_type_expression())
7267 {
631d5788 7268 go_error_at(arg->location(), "expected type");
1cce762f 7269 this->set_is_error();
7270 }
7271 else
7272 return Expression::make_allocation(arg->type(), loc);
7273 }
7274 }
7275 break;
7276
7277 case BUILTIN_MAKE:
321e5ad2 7278 return this->lower_make(inserter);
1cce762f 7279
7280 case BUILTIN_RECOVER:
e440a328 7281 if (function != NULL)
7282 function->func_value()->set_calls_recover();
7283 else
7284 {
7285 // Calling recover outside of a function always returns the
7286 // nil empty interface.
823c7e3d 7287 Type* eface = Type::make_empty_interface_type(loc);
1cce762f 7288 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
e440a328 7289 }
1cce762f 7290 break;
7291
1cce762f 7292 case BUILTIN_DELETE:
7293 {
7294 // Lower to a runtime function call.
7295 const Expression_list* args = this->args();
7296 if (args == NULL || args->size() < 2)
7297 this->report_error(_("not enough arguments"));
7298 else if (args->size() > 2)
7299 this->report_error(_("too many arguments"));
7300 else if (args->front()->type()->map_type() == NULL)
7301 this->report_error(_("argument 1 must be a map"));
7302 else
7303 {
7304 // Since this function returns no value it must appear in
7305 // a statement by itself, so we don't have to worry about
7306 // order of evaluation of values around it. Evaluate the
7307 // map first to get order of evaluation right.
7308 Map_type* mt = args->front()->type()->map_type();
7309 Temporary_statement* map_temp =
7310 Statement::make_temporary(mt, args->front(), loc);
7311 inserter->insert(map_temp);
7312
7313 Temporary_statement* key_temp =
7314 Statement::make_temporary(mt->key_type(), args->back(), loc);
7315 inserter->insert(key_temp);
7316
0d5530d9 7317 Expression* e1 = Expression::make_type_descriptor(mt, loc);
7318 Expression* e2 = Expression::make_temporary_reference(map_temp,
1cce762f 7319 loc);
0d5530d9 7320 Expression* e3 = Expression::make_temporary_reference(key_temp,
1cce762f 7321 loc);
0d5530d9 7322 e3 = Expression::make_unary(OPERATOR_AND, e3, loc);
1cce762f 7323 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
0d5530d9 7324 3, e1, e2, e3);
1cce762f 7325 }
7326 }
7327 break;
88b03a70 7328
7329 case BUILTIN_PRINT:
7330 case BUILTIN_PRINTLN:
7331 // Force all the arguments into temporary variables, so that we
7332 // don't try to evaluate something while holding the print lock.
7333 if (this->args() == NULL)
7334 break;
7335 for (Expression_list::iterator pa = this->args()->begin();
7336 pa != this->args()->end();
7337 ++pa)
7338 {
493ce3ee 7339 if (!(*pa)->is_variable() && !(*pa)->is_constant())
88b03a70 7340 {
7341 Temporary_statement* temp =
7342 Statement::make_temporary(NULL, *pa, loc);
7343 inserter->insert(temp);
7344 *pa = Expression::make_temporary_reference(temp, loc);
7345 }
7346 }
7347 break;
e440a328 7348 }
7349
7350 return this;
7351}
7352
35a54f17 7353// Flatten a builtin call expression. This turns the arguments of copy and
7354// append into temporary expressions.
7355
7356Expression*
321e5ad2 7357Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function,
35a54f17 7358 Statement_inserter* inserter)
7359{
16cb7fec 7360 Location loc = this->location();
7361
7362 switch (this->code_)
35a54f17 7363 {
16cb7fec 7364 default:
7365 break;
7366
7367 case BUILTIN_APPEND:
321e5ad2 7368 return this->flatten_append(gogo, function, inserter);
7369
16cb7fec 7370 case BUILTIN_COPY:
7371 {
7372 Type* at = this->args()->front()->type();
7373 for (Expression_list::iterator pa = this->args()->begin();
7374 pa != this->args()->end();
7375 ++pa)
7376 {
7377 if ((*pa)->is_nil_expression())
7378 {
7379 Expression* nil = Expression::make_nil(loc);
7380 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
7381 *pa = Expression::make_slice_value(at, nil, zero, zero, loc);
7382 }
7383 if (!(*pa)->is_variable())
7384 {
7385 Temporary_statement* temp =
7386 Statement::make_temporary(NULL, *pa, loc);
7387 inserter->insert(temp);
7388 *pa = Expression::make_temporary_reference(temp, loc);
7389 }
7390 }
7391 }
7392 break;
7393
7394 case BUILTIN_PANIC:
35a54f17 7395 for (Expression_list::iterator pa = this->args()->begin();
16cb7fec 7396 pa != this->args()->end();
7397 ++pa)
7398 {
7399 if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL)
55e8ba6a 7400 {
16cb7fec 7401 Temporary_statement* temp =
7402 Statement::make_temporary(NULL, *pa, loc);
7403 inserter->insert(temp);
7404 *pa = Expression::make_temporary_reference(temp, loc);
55e8ba6a 7405 }
16cb7fec 7406 }
7739537f 7407 break;
0d5530d9 7408
7409 case BUILTIN_LEN:
132ed071 7410 case BUILTIN_CAP:
321e5ad2 7411 {
7412 Expression_list::iterator pa = this->args()->begin();
7413 if (!(*pa)->is_variable()
7414 && ((*pa)->type()->map_type() != NULL
7415 || (*pa)->type()->channel_type() != NULL))
7416 {
7417 Temporary_statement* temp =
7418 Statement::make_temporary(NULL, *pa, loc);
7419 inserter->insert(temp);
7420 *pa = Expression::make_temporary_reference(temp, loc);
7421 }
7422 }
7423 break;
35a54f17 7424 }
16cb7fec 7425
35a54f17 7426 return this;
7427}
7428
a9182619 7429// Lower a make expression.
7430
7431Expression*
321e5ad2 7432Builtin_call_expression::lower_make(Statement_inserter* inserter)
a9182619 7433{
b13c66cd 7434 Location loc = this->location();
a9182619 7435
7436 const Expression_list* args = this->args();
7437 if (args == NULL || args->size() < 1)
7438 {
7439 this->report_error(_("not enough arguments"));
7440 return Expression::make_error(this->location());
7441 }
7442
7443 Expression_list::const_iterator parg = args->begin();
7444
7445 Expression* first_arg = *parg;
7446 if (!first_arg->is_type_expression())
7447 {
631d5788 7448 go_error_at(first_arg->location(), "expected type");
a9182619 7449 this->set_is_error();
7450 return Expression::make_error(this->location());
7451 }
7452 Type* type = first_arg->type();
7453
22deed0d 7454 if (!type->in_heap())
7455 go_error_at(first_arg->location(),
7456 "can't make slice of go:notinheap type");
7457
a9182619 7458 bool is_slice = false;
7459 bool is_map = false;
7460 bool is_chan = false;
411eb89e 7461 if (type->is_slice_type())
a9182619 7462 is_slice = true;
7463 else if (type->map_type() != NULL)
7464 is_map = true;
7465 else if (type->channel_type() != NULL)
7466 is_chan = true;
7467 else
7468 {
7469 this->report_error(_("invalid type for make function"));
7470 return Expression::make_error(this->location());
7471 }
7472
f6bc81e6 7473 Type_context int_context(Type::lookup_integer_type("int"), false);
7474
a9182619 7475 ++parg;
7476 Expression* len_arg;
ccea2b36 7477 bool len_small = false;
a9182619 7478 if (parg == args->end())
7479 {
7480 if (is_slice)
7481 {
7482 this->report_error(_("length required when allocating a slice"));
7483 return Expression::make_error(this->location());
7484 }
e67508fa 7485 len_arg = Expression::make_integer_ul(0, NULL, loc);
a9182619 7486 }
7487 else
7488 {
7489 len_arg = *parg;
f6bc81e6 7490 len_arg->determine_type(&int_context);
ccea2b36 7491 if (!this->check_int_value(len_arg, true, &len_small))
1ad00fd4 7492 return Expression::make_error(this->location());
a9182619 7493 ++parg;
7494 }
7495
7496 Expression* cap_arg = NULL;
ccea2b36 7497 bool cap_small = false;
a9182619 7498 if (is_slice && parg != args->end())
7499 {
7500 cap_arg = *parg;
f6bc81e6 7501 cap_arg->determine_type(&int_context);
ccea2b36 7502 if (!this->check_int_value(cap_arg, false, &cap_small))
1ad00fd4 7503 return Expression::make_error(this->location());
7504
7505 Numeric_constant nclen;
7506 Numeric_constant nccap;
7507 unsigned long vlen;
7508 unsigned long vcap;
7509 if (len_arg->numeric_constant_value(&nclen)
7510 && cap_arg->numeric_constant_value(&nccap)
7511 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
7512 && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID
7513 && vlen > vcap)
a9182619 7514 {
1ad00fd4 7515 this->report_error(_("len larger than cap"));
a9182619 7516 return Expression::make_error(this->location());
7517 }
1ad00fd4 7518
a9182619 7519 ++parg;
7520 }
7521
7522 if (parg != args->end())
7523 {
7524 this->report_error(_("too many arguments to make"));
7525 return Expression::make_error(this->location());
7526 }
7527
b13c66cd 7528 Location type_loc = first_arg->location();
a9182619 7529
7530 Expression* call;
7531 if (is_slice)
7532 {
321e5ad2 7533 Type* et = type->array_type()->element_type();
7534 Expression* type_arg = Expression::make_type_descriptor(et, type_loc);
a9182619 7535 if (cap_arg == NULL)
321e5ad2 7536 {
7537 Temporary_statement* temp = Statement::make_temporary(NULL,
7538 len_arg,
7539 loc);
7540 inserter->insert(temp);
7541 len_arg = Expression::make_temporary_reference(temp, loc);
7542 cap_arg = Expression::make_temporary_reference(temp, loc);
ccea2b36 7543 cap_small = len_small;
321e5ad2 7544 }
ccea2b36 7545
7546 Runtime::Function code = Runtime::MAKESLICE;
7547 if (!len_small || !cap_small)
7548 code = Runtime::MAKESLICE64;
7549 call = Runtime::make_call(code, loc, 3, type_arg, len_arg, cap_arg);
a9182619 7550 }
7551 else if (is_map)
321e5ad2 7552 {
7553 Expression* type_arg = Expression::make_type_descriptor(type, type_loc);
7554 call = Runtime::make_call(Runtime::MAKEMAP, loc, 4, type_arg, len_arg,
7555 Expression::make_nil(loc),
7556 Expression::make_nil(loc));
7557 }
a9182619 7558 else if (is_chan)
321e5ad2 7559 {
7560 Expression* type_arg = Expression::make_type_descriptor(type, type_loc);
7561 call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg);
7562 }
a9182619 7563 else
7564 go_unreachable();
7565
7566 return Expression::make_unsafe_cast(type, call, loc);
7567}
7568
321e5ad2 7569// Flatten a call to the predeclared append function. We do this in
7570// the flatten phase, not the lowering phase, so that we run after
7571// type checking and after order_evaluations.
7572
7573Expression*
7574Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function,
7575 Statement_inserter* inserter)
7576{
7577 if (this->is_error_expression())
7578 return this;
7579
7580 Location loc = this->location();
7581
7582 const Expression_list* args = this->args();
7583 go_assert(args != NULL && !args->empty());
7584
7585 Type* slice_type = args->front()->type();
7586 go_assert(slice_type->is_slice_type());
7587 Type* element_type = slice_type->array_type()->element_type();
7588
7589 if (args->size() == 1)
7590 {
7591 // append(s) evaluates to s.
7592 return args->front();
7593 }
7594
7595 Type* int_type = Type::lookup_integer_type("int");
7596 Type* uint_type = Type::lookup_integer_type("uint");
7597
7598 // Implementing
7599 // append(s1, s2...)
7600 // or
7601 // append(s1, a1, a2, a3, ...)
7602
7603 // s1tmp := s1
7604 Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(),
7605 loc);
7606 inserter->insert(s1tmp);
7607
7608 // l1tmp := len(s1tmp)
7609 Named_object* lenfn = gogo->lookup_global("len");
7610 Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc);
7611 Expression_list* call_args = new Expression_list();
7612 call_args->push_back(Expression::make_temporary_reference(s1tmp, loc));
7613 Expression* len = Expression::make_call(lenref, call_args, false, loc);
7614 gogo->lower_expression(function, inserter, &len);
7615 gogo->flatten_expression(function, inserter, &len);
7616 Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc);
7617 inserter->insert(l1tmp);
7618
7619 Temporary_statement* s2tmp = NULL;
7620 Temporary_statement* l2tmp = NULL;
7621 Expression_list* add = NULL;
7622 Expression* len2;
7623 if (this->is_varargs())
7624 {
7625 go_assert(args->size() == 2);
7626
7627 // s2tmp := s2
7628 s2tmp = Statement::make_temporary(NULL, args->back(), loc);
7629 inserter->insert(s2tmp);
7630
7631 // l2tmp := len(s2tmp)
7632 lenref = Expression::make_func_reference(lenfn, NULL, loc);
7633 call_args = new Expression_list();
7634 call_args->push_back(Expression::make_temporary_reference(s2tmp, loc));
7635 len = Expression::make_call(lenref, call_args, false, loc);
7636 gogo->lower_expression(function, inserter, &len);
7637 gogo->flatten_expression(function, inserter, &len);
7638 l2tmp = Statement::make_temporary(int_type, len, loc);
7639 inserter->insert(l2tmp);
7640
7641 // len2 = l2tmp
7642 len2 = Expression::make_temporary_reference(l2tmp, loc);
7643 }
7644 else
7645 {
7646 // We have to ensure that all the arguments are in variables
7647 // now, because otherwise if one of them is an index expression
7648 // into the current slice we could overwrite it before we fetch
7649 // it.
7650 add = new Expression_list();
7651 Expression_list::const_iterator pa = args->begin();
7652 for (++pa; pa != args->end(); ++pa)
7653 {
7654 if ((*pa)->is_variable())
7655 add->push_back(*pa);
7656 else
7657 {
7658 Temporary_statement* tmp = Statement::make_temporary(NULL, *pa,
7659 loc);
7660 inserter->insert(tmp);
7661 add->push_back(Expression::make_temporary_reference(tmp, loc));
7662 }
7663 }
7664
7665 // len2 = len(add)
7666 len2 = Expression::make_integer_ul(add->size(), int_type, loc);
7667 }
7668
7669 // ntmp := l1tmp + len2
7670 Expression* ref = Expression::make_temporary_reference(l1tmp, loc);
7671 Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc);
7672 gogo->lower_expression(function, inserter, &sum);
7673 gogo->flatten_expression(function, inserter, &sum);
7674 Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc);
7675 inserter->insert(ntmp);
7676
7677 // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ?
7678 // growslice(type, s1tmp, ntmp) :
7679 // s1tmp[:ntmp]
7680 // Using uint here means that if the computation of ntmp overflowed,
7681 // we will call growslice which will panic.
7682
7683 Expression* left = Expression::make_temporary_reference(ntmp, loc);
7684 left = Expression::make_cast(uint_type, left, loc);
7685
7686 Named_object* capfn = gogo->lookup_global("cap");
7687 Expression* capref = Expression::make_func_reference(capfn, NULL, loc);
7688 call_args = new Expression_list();
7689 call_args->push_back(Expression::make_temporary_reference(s1tmp, loc));
7690 Expression* right = Expression::make_call(capref, call_args, false, loc);
7691 right = Expression::make_cast(uint_type, right, loc);
7692
7693 Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc);
7694
7695 Expression* a1 = Expression::make_type_descriptor(element_type, loc);
7696 Expression* a2 = Expression::make_temporary_reference(s1tmp, loc);
7697 Expression* a3 = Expression::make_temporary_reference(ntmp, loc);
7698 Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3,
7699 a1, a2, a3);
7700 call = Expression::make_unsafe_cast(slice_type, call, loc);
7701
7702 ref = Expression::make_temporary_reference(s1tmp, loc);
7703 Expression* zero = Expression::make_integer_ul(0, int_type, loc);
7704 Expression* ref2 = Expression::make_temporary_reference(ntmp, loc);
7705 // FIXME: Mark this index as not requiring bounds checks.
7706 ref = Expression::make_index(ref, zero, ref2, NULL, loc);
7707
7708 Expression* rhs = Expression::make_conditional(cond, call, ref, loc);
7709
7710 gogo->lower_expression(function, inserter, &rhs);
7711 gogo->flatten_expression(function, inserter, &rhs);
7712
7713 Expression* lhs = Expression::make_temporary_reference(s1tmp, loc);
7714 Statement* assign = Statement::make_assignment(lhs, rhs, loc);
7715 inserter->insert(assign);
7716
7717 if (this->is_varargs())
7718 {
7719 // copy(s1tmp[l1tmp:], s2tmp)
7720 a1 = Expression::make_temporary_reference(s1tmp, loc);
7721 ref = Expression::make_temporary_reference(l1tmp, loc);
7722 Expression* nil = Expression::make_nil(loc);
7723 // FIXME: Mark this index as not requiring bounds checks.
7724 a1 = Expression::make_index(a1, ref, nil, NULL, loc);
7725
7726 a2 = Expression::make_temporary_reference(s2tmp, loc);
7727
7728 Named_object* copyfn = gogo->lookup_global("copy");
7729 Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc);
7730 call_args = new Expression_list();
7731 call_args->push_back(a1);
7732 call_args->push_back(a2);
7733 call = Expression::make_call(copyref, call_args, false, loc);
7734 gogo->lower_expression(function, inserter, &call);
7735 gogo->flatten_expression(function, inserter, &call);
7736 inserter->insert(Statement::make_statement(call, false));
7737 }
7738 else
7739 {
7740 // For each argument:
7741 // s1tmp[l1tmp+i] = a
7742 unsigned long i = 0;
7743 for (Expression_list::const_iterator pa = add->begin();
7744 pa != add->end();
7745 ++pa, ++i)
7746 {
7747 ref = Expression::make_temporary_reference(s1tmp, loc);
7748 ref2 = Expression::make_temporary_reference(l1tmp, loc);
7749 Expression* off = Expression::make_integer_ul(i, int_type, loc);
7750 ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc);
7751 // FIXME: Mark this index as not requiring bounds checks.
7752 lhs = Expression::make_index(ref, ref2, NULL, NULL, loc);
7753 gogo->lower_expression(function, inserter, &lhs);
7754 gogo->flatten_expression(function, inserter, &lhs);
03118c21 7755 // The flatten pass runs after the write barrier pass, so we
7756 // need to insert a write barrier here if necessary.
7757 if (!gogo->assign_needs_write_barrier(lhs))
7758 assign = Statement::make_assignment(lhs, *pa, loc);
7759 else
7760 {
7761 Function* f = function == NULL ? NULL : function->func_value();
7762 assign = gogo->assign_with_write_barrier(f, NULL, inserter,
7763 lhs, *pa, loc);
7764 }
321e5ad2 7765 inserter->insert(assign);
7766 }
7767 }
7768
7769 return Expression::make_temporary_reference(s1tmp, loc);
7770}
7771
a9182619 7772// Return whether an expression has an integer value. Report an error
7773// if not. This is used when handling calls to the predeclared make
ccea2b36 7774// function. Set *SMALL if the value is known to fit in type "int".
a9182619 7775
7776bool
ccea2b36 7777Builtin_call_expression::check_int_value(Expression* e, bool is_length,
7778 bool *small)
a9182619 7779{
ccea2b36 7780 *small = false;
7781
0c77715b 7782 Numeric_constant nc;
1ad00fd4 7783 if (e->numeric_constant_value(&nc))
a9182619 7784 {
1ad00fd4 7785 unsigned long v;
7786 switch (nc.to_unsigned_long(&v))
7787 {
7788 case Numeric_constant::NC_UL_VALID:
1b10c5e7 7789 break;
1ad00fd4 7790 case Numeric_constant::NC_UL_NOTINT:
631d5788 7791 go_error_at(e->location(), "non-integer %s argument to make",
7792 is_length ? "len" : "cap");
1ad00fd4 7793 return false;
7794 case Numeric_constant::NC_UL_NEGATIVE:
631d5788 7795 go_error_at(e->location(), "negative %s argument to make",
7796 is_length ? "len" : "cap");
1ad00fd4 7797 return false;
7798 case Numeric_constant::NC_UL_BIG:
7799 // We don't want to give a compile-time error for a 64-bit
7800 // value on a 32-bit target.
1b10c5e7 7801 break;
1ad00fd4 7802 }
1b10c5e7 7803
7804 mpz_t val;
7805 if (!nc.to_int(&val))
7806 go_unreachable();
7807 int bits = mpz_sizeinbase(val, 2);
7808 mpz_clear(val);
7809 Type* int_type = Type::lookup_integer_type("int");
7810 if (bits >= int_type->integer_type()->bits())
7811 {
631d5788 7812 go_error_at(e->location(), "%s argument too large for make",
7813 is_length ? "len" : "cap");
1b10c5e7 7814 return false;
7815 }
7816
ccea2b36 7817 *small = true;
1b10c5e7 7818 return true;
a9182619 7819 }
7820
1ad00fd4 7821 if (e->type()->integer_type() != NULL)
ccea2b36 7822 {
7823 int ebits = e->type()->integer_type()->bits();
7824 int intbits = Type::lookup_integer_type("int")->integer_type()->bits();
7825
7826 // We can treat ebits == intbits as small even for an unsigned
7827 // integer type, because we will convert the value to int and
7828 // then reject it in the runtime if it is negative.
7829 *small = ebits <= intbits;
7830
7831 return true;
7832 }
1ad00fd4 7833
631d5788 7834 go_error_at(e->location(), "non-integer %s argument to make",
7835 is_length ? "len" : "cap");
a9182619 7836 return false;
7837}
7838
e440a328 7839// Return the type of the real or imag functions, given the type of
fcbea5e4 7840// the argument. We need to map complex64 to float32 and complex128
7841// to float64, so it has to be done by name. This returns NULL if it
7842// can't figure out the type.
e440a328 7843
7844Type*
7845Builtin_call_expression::real_imag_type(Type* arg_type)
7846{
7847 if (arg_type == NULL || arg_type->is_abstract())
7848 return NULL;
7849 Named_type* nt = arg_type->named_type();
7850 if (nt == NULL)
7851 return NULL;
7852 while (nt->real_type()->named_type() != NULL)
7853 nt = nt->real_type()->named_type();
48080209 7854 if (nt->name() == "complex64")
e440a328 7855 return Type::lookup_float_type("float32");
7856 else if (nt->name() == "complex128")
7857 return Type::lookup_float_type("float64");
7858 else
7859 return NULL;
7860}
7861
48080209 7862// Return the type of the complex function, given the type of one of the
e440a328 7863// argments. Like real_imag_type, we have to map by name.
7864
7865Type*
48080209 7866Builtin_call_expression::complex_type(Type* arg_type)
e440a328 7867{
7868 if (arg_type == NULL || arg_type->is_abstract())
7869 return NULL;
7870 Named_type* nt = arg_type->named_type();
7871 if (nt == NULL)
7872 return NULL;
7873 while (nt->real_type()->named_type() != NULL)
7874 nt = nt->real_type()->named_type();
48080209 7875 if (nt->name() == "float32")
e440a328 7876 return Type::lookup_complex_type("complex64");
7877 else if (nt->name() == "float64")
7878 return Type::lookup_complex_type("complex128");
7879 else
7880 return NULL;
7881}
7882
7883// Return a single argument, or NULL if there isn't one.
7884
7885Expression*
7886Builtin_call_expression::one_arg() const
7887{
7888 const Expression_list* args = this->args();
aa615cb3 7889 if (args == NULL || args->size() != 1)
e440a328 7890 return NULL;
7891 return args->front();
7892}
7893
83921647 7894// A traversal class which looks for a call or receive expression.
7895
7896class Find_call_expression : public Traverse
7897{
7898 public:
7899 Find_call_expression()
7900 : Traverse(traverse_expressions),
7901 found_(false)
7902 { }
7903
7904 int
7905 expression(Expression**);
7906
7907 bool
7908 found()
7909 { return this->found_; }
7910
7911 private:
7912 bool found_;
7913};
7914
7915int
7916Find_call_expression::expression(Expression** pexpr)
7917{
7918 if ((*pexpr)->call_expression() != NULL
7919 || (*pexpr)->receive_expression() != NULL)
7920 {
7921 this->found_ = true;
7922 return TRAVERSE_EXIT;
7923 }
7924 return TRAVERSE_CONTINUE;
7925}
7926
7927// Return whether this is constant: len of a string constant, or len
7928// or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7929// unsafe.Alignof.
e440a328 7930
7931bool
7932Builtin_call_expression::do_is_constant() const
7933{
12e69faa 7934 if (this->is_error_expression())
7935 return true;
e440a328 7936 switch (this->code_)
7937 {
7938 case BUILTIN_LEN:
7939 case BUILTIN_CAP:
7940 {
0f914071 7941 if (this->seen_)
7942 return false;
7943
e440a328 7944 Expression* arg = this->one_arg();
7945 if (arg == NULL)
7946 return false;
7947 Type* arg_type = arg->type();
7948
7949 if (arg_type->points_to() != NULL
7950 && arg_type->points_to()->array_type() != NULL
411eb89e 7951 && !arg_type->points_to()->is_slice_type())
e440a328 7952 arg_type = arg_type->points_to();
7953
83921647 7954 // The len and cap functions are only constant if there are no
7955 // function calls or channel operations in the arguments.
7956 // Otherwise we have to make the call.
7957 if (!arg->is_constant())
7958 {
7959 Find_call_expression find_call;
7960 Expression::traverse(&arg, &find_call);
7961 if (find_call.found())
7962 return false;
7963 }
7964
e440a328 7965 if (arg_type->array_type() != NULL
7966 && arg_type->array_type()->length() != NULL)
0f914071 7967 return true;
e440a328 7968
7969 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
0f914071 7970 {
7971 this->seen_ = true;
7972 bool ret = arg->is_constant();
7973 this->seen_ = false;
7974 return ret;
7975 }
e440a328 7976 }
7977 break;
7978
7979 case BUILTIN_SIZEOF:
7980 case BUILTIN_ALIGNOF:
7981 return this->one_arg() != NULL;
7982
7983 case BUILTIN_OFFSETOF:
7984 {
7985 Expression* arg = this->one_arg();
7986 if (arg == NULL)
7987 return false;
7988 return arg->field_reference_expression() != NULL;
7989 }
7990
48080209 7991 case BUILTIN_COMPLEX:
e440a328 7992 {
7993 const Expression_list* args = this->args();
7994 if (args != NULL && args->size() == 2)
7995 return args->front()->is_constant() && args->back()->is_constant();
7996 }
7997 break;
7998
7999 case BUILTIN_REAL:
8000 case BUILTIN_IMAG:
8001 {
8002 Expression* arg = this->one_arg();
8003 return arg != NULL && arg->is_constant();
8004 }
8005
8006 default:
8007 break;
8008 }
8009
8010 return false;
8011}
8012
0c77715b 8013// Return a numeric constant if possible.
e440a328 8014
8015bool
0c77715b 8016Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 8017{
8018 if (this->code_ == BUILTIN_LEN
8019 || this->code_ == BUILTIN_CAP)
8020 {
8021 Expression* arg = this->one_arg();
8022 if (arg == NULL)
8023 return false;
8024 Type* arg_type = arg->type();
8025
8026 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
8027 {
8028 std::string sval;
8029 if (arg->string_constant_value(&sval))
8030 {
0c77715b 8031 nc->set_unsigned_long(Type::lookup_integer_type("int"),
8032 sval.length());
e440a328 8033 return true;
8034 }
8035 }
8036
8037 if (arg_type->points_to() != NULL
8038 && arg_type->points_to()->array_type() != NULL
411eb89e 8039 && !arg_type->points_to()->is_slice_type())
e440a328 8040 arg_type = arg_type->points_to();
8041
8042 if (arg_type->array_type() != NULL
8043 && arg_type->array_type()->length() != NULL)
8044 {
0f914071 8045 if (this->seen_)
8046 return false;
e440a328 8047 Expression* e = arg_type->array_type()->length();
0f914071 8048 this->seen_ = true;
0c77715b 8049 bool r = e->numeric_constant_value(nc);
0f914071 8050 this->seen_ = false;
8051 if (r)
e440a328 8052 {
0c77715b 8053 if (!nc->set_type(Type::lookup_integer_type("int"), false,
8054 this->location()))
8055 r = false;
e440a328 8056 }
0c77715b 8057 return r;
e440a328 8058 }
8059 }
8060 else if (this->code_ == BUILTIN_SIZEOF
8061 || this->code_ == BUILTIN_ALIGNOF)
8062 {
8063 Expression* arg = this->one_arg();
8064 if (arg == NULL)
8065 return false;
8066 Type* arg_type = arg->type();
5c13bd80 8067 if (arg_type->is_error())
e440a328 8068 return false;
8069 if (arg_type->is_abstract())
8070 return false;
2c809f8f 8071 if (this->seen_)
8072 return false;
927a01eb 8073
3f378015 8074 int64_t ret;
e440a328 8075 if (this->code_ == BUILTIN_SIZEOF)
8076 {
2c809f8f 8077 this->seen_ = true;
8078 bool ok = arg_type->backend_type_size(this->gogo_, &ret);
8079 this->seen_ = false;
8080 if (!ok)
e440a328 8081 return false;
8082 }
8083 else if (this->code_ == BUILTIN_ALIGNOF)
8084 {
2c809f8f 8085 bool ok;
8086 this->seen_ = true;
637bd3af 8087 if (arg->field_reference_expression() == NULL)
2c809f8f 8088 ok = arg_type->backend_type_align(this->gogo_, &ret);
637bd3af 8089 else
e440a328 8090 {
8091 // Calling unsafe.Alignof(s.f) returns the alignment of
8092 // the type of f when it is used as a field in a struct.
2c809f8f 8093 ok = arg_type->backend_type_field_align(this->gogo_, &ret);
e440a328 8094 }
2c809f8f 8095 this->seen_ = false;
8096 if (!ok)
8097 return false;
e440a328 8098 }
8099 else
c3e6f413 8100 go_unreachable();
927a01eb 8101
3f378015 8102 mpz_t zval;
8103 set_mpz_from_int64(&zval, ret);
8104 nc->set_int(Type::lookup_integer_type("uintptr"), zval);
8105 mpz_clear(zval);
e440a328 8106 return true;
8107 }
8108 else if (this->code_ == BUILTIN_OFFSETOF)
8109 {
8110 Expression* arg = this->one_arg();
8111 if (arg == NULL)
8112 return false;
8113 Field_reference_expression* farg = arg->field_reference_expression();
8114 if (farg == NULL)
8115 return false;
2c809f8f 8116 if (this->seen_)
8117 return false;
8118
3f378015 8119 int64_t total_offset = 0;
9a4bd570 8120 while (true)
8121 {
8122 Expression* struct_expr = farg->expr();
8123 Type* st = struct_expr->type();
8124 if (st->struct_type() == NULL)
8125 return false;
8126 if (st->named_type() != NULL)
8127 st->named_type()->convert(this->gogo_);
3f378015 8128 int64_t offset;
2c809f8f 8129 this->seen_ = true;
8130 bool ok = st->struct_type()->backend_field_offset(this->gogo_,
8131 farg->field_index(),
8132 &offset);
8133 this->seen_ = false;
8134 if (!ok)
8135 return false;
9a4bd570 8136 total_offset += offset;
8137 if (farg->implicit() && struct_expr->field_reference_expression() != NULL)
8138 {
8139 // Go up until we reach the original base.
8140 farg = struct_expr->field_reference_expression();
8141 continue;
8142 }
8143 break;
8144 }
3f378015 8145 mpz_t zval;
8146 set_mpz_from_int64(&zval, total_offset);
8147 nc->set_int(Type::lookup_integer_type("uintptr"), zval);
8148 mpz_clear(zval);
e440a328 8149 return true;
8150 }
0c77715b 8151 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
e440a328 8152 {
8153 Expression* arg = this->one_arg();
8154 if (arg == NULL)
8155 return false;
8156
0c77715b 8157 Numeric_constant argnc;
8158 if (!arg->numeric_constant_value(&argnc))
8159 return false;
8160
fcbea5e4 8161 mpc_t val;
8162 if (!argnc.to_complex(&val))
0c77715b 8163 return false;
e440a328 8164
0c77715b 8165 Type* type = Builtin_call_expression::real_imag_type(argnc.type());
8166 if (this->code_ == BUILTIN_REAL)
fcbea5e4 8167 nc->set_float(type, mpc_realref(val));
0c77715b 8168 else
fcbea5e4 8169 nc->set_float(type, mpc_imagref(val));
8170 mpc_clear(val);
0c77715b 8171 return true;
e440a328 8172 }
0c77715b 8173 else if (this->code_ == BUILTIN_COMPLEX)
e440a328 8174 {
8175 const Expression_list* args = this->args();
8176 if (args == NULL || args->size() != 2)
8177 return false;
8178
0c77715b 8179 Numeric_constant rnc;
8180 if (!args->front()->numeric_constant_value(&rnc))
8181 return false;
8182 Numeric_constant inc;
8183 if (!args->back()->numeric_constant_value(&inc))
8184 return false;
8185
8186 if (rnc.type() != NULL
8187 && !rnc.type()->is_abstract()
8188 && inc.type() != NULL
8189 && !inc.type()->is_abstract()
8190 && !Type::are_identical(rnc.type(), inc.type(), false, NULL))
8191 return false;
8192
e440a328 8193 mpfr_t r;
0c77715b 8194 if (!rnc.to_float(&r))
8195 return false;
8196 mpfr_t i;
8197 if (!inc.to_float(&i))
e440a328 8198 {
8199 mpfr_clear(r);
8200 return false;
8201 }
8202
0c77715b 8203 Type* arg_type = rnc.type();
8204 if (arg_type == NULL || arg_type->is_abstract())
8205 arg_type = inc.type();
e440a328 8206
fcbea5e4 8207 mpc_t val;
8208 mpc_init2(val, mpc_precision);
8209 mpc_set_fr_fr(val, r, i, MPC_RNDNN);
e440a328 8210 mpfr_clear(r);
8211 mpfr_clear(i);
8212
fcbea5e4 8213 Type* type = Builtin_call_expression::complex_type(arg_type);
8214 nc->set_complex(type, val);
8215
8216 mpc_clear(val);
8217
0c77715b 8218 return true;
e440a328 8219 }
8220
8221 return false;
8222}
8223
a7549a6a 8224// Give an error if we are discarding the value of an expression which
8225// should not normally be discarded. We don't give an error for
8226// discarding the value of an ordinary function call, but we do for
8227// builtin functions, purely for consistency with the gc compiler.
8228
4f2138d7 8229bool
a7549a6a 8230Builtin_call_expression::do_discarding_value()
8231{
8232 switch (this->code_)
8233 {
8234 case BUILTIN_INVALID:
8235 default:
8236 go_unreachable();
8237
8238 case BUILTIN_APPEND:
8239 case BUILTIN_CAP:
8240 case BUILTIN_COMPLEX:
8241 case BUILTIN_IMAG:
8242 case BUILTIN_LEN:
8243 case BUILTIN_MAKE:
8244 case BUILTIN_NEW:
8245 case BUILTIN_REAL:
8246 case BUILTIN_ALIGNOF:
8247 case BUILTIN_OFFSETOF:
8248 case BUILTIN_SIZEOF:
8249 this->unused_value_error();
4f2138d7 8250 return false;
a7549a6a 8251
8252 case BUILTIN_CLOSE:
8253 case BUILTIN_COPY:
1cce762f 8254 case BUILTIN_DELETE:
a7549a6a 8255 case BUILTIN_PANIC:
8256 case BUILTIN_PRINT:
8257 case BUILTIN_PRINTLN:
8258 case BUILTIN_RECOVER:
4f2138d7 8259 return true;
a7549a6a 8260 }
8261}
8262
e440a328 8263// Return the type.
8264
8265Type*
8266Builtin_call_expression::do_type()
8267{
79651b1f 8268 if (this->is_error_expression())
8269 return Type::make_error_type();
e440a328 8270 switch (this->code_)
8271 {
8272 case BUILTIN_INVALID:
8273 default:
79651b1f 8274 return Type::make_error_type();
e440a328 8275
8276 case BUILTIN_NEW:
8277 case BUILTIN_MAKE:
8278 {
8279 const Expression_list* args = this->args();
8280 if (args == NULL || args->empty())
8281 return Type::make_error_type();
8282 return Type::make_pointer_type(args->front()->type());
8283 }
8284
8285 case BUILTIN_CAP:
8286 case BUILTIN_COPY:
8287 case BUILTIN_LEN:
7ba86326 8288 return Type::lookup_integer_type("int");
8289
e440a328 8290 case BUILTIN_ALIGNOF:
8291 case BUILTIN_OFFSETOF:
8292 case BUILTIN_SIZEOF:
7ba86326 8293 return Type::lookup_integer_type("uintptr");
e440a328 8294
8295 case BUILTIN_CLOSE:
1cce762f 8296 case BUILTIN_DELETE:
e440a328 8297 case BUILTIN_PANIC:
8298 case BUILTIN_PRINT:
8299 case BUILTIN_PRINTLN:
8300 return Type::make_void_type();
8301
e440a328 8302 case BUILTIN_RECOVER:
823c7e3d 8303 return Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 8304
8305 case BUILTIN_APPEND:
8306 {
8307 const Expression_list* args = this->args();
8308 if (args == NULL || args->empty())
8309 return Type::make_error_type();
3ff4863b 8310 Type *ret = args->front()->type();
8311 if (!ret->is_slice_type())
8312 return Type::make_error_type();
8313 return ret;
e440a328 8314 }
8315
8316 case BUILTIN_REAL:
8317 case BUILTIN_IMAG:
8318 {
8319 Expression* arg = this->one_arg();
8320 if (arg == NULL)
8321 return Type::make_error_type();
8322 Type* t = arg->type();
8323 if (t->is_abstract())
8324 t = t->make_non_abstract_type();
8325 t = Builtin_call_expression::real_imag_type(t);
8326 if (t == NULL)
8327 t = Type::make_error_type();
8328 return t;
8329 }
8330
48080209 8331 case BUILTIN_COMPLEX:
e440a328 8332 {
8333 const Expression_list* args = this->args();
8334 if (args == NULL || args->size() != 2)
8335 return Type::make_error_type();
8336 Type* t = args->front()->type();
8337 if (t->is_abstract())
8338 {
8339 t = args->back()->type();
8340 if (t->is_abstract())
8341 t = t->make_non_abstract_type();
8342 }
48080209 8343 t = Builtin_call_expression::complex_type(t);
e440a328 8344 if (t == NULL)
8345 t = Type::make_error_type();
8346 return t;
8347 }
8348 }
8349}
8350
8351// Determine the type.
8352
8353void
8354Builtin_call_expression::do_determine_type(const Type_context* context)
8355{
fb94b0ca 8356 if (!this->determining_types())
8357 return;
8358
e440a328 8359 this->fn()->determine_type_no_context();
8360
8361 const Expression_list* args = this->args();
8362
8363 bool is_print;
8364 Type* arg_type = NULL;
321e5ad2 8365 Type* trailing_arg_types = NULL;
e440a328 8366 switch (this->code_)
8367 {
8368 case BUILTIN_PRINT:
8369 case BUILTIN_PRINTLN:
8370 // Do not force a large integer constant to "int".
8371 is_print = true;
8372 break;
8373
8374 case BUILTIN_REAL:
8375 case BUILTIN_IMAG:
48080209 8376 arg_type = Builtin_call_expression::complex_type(context->type);
f6bc81e6 8377 if (arg_type == NULL)
8378 arg_type = Type::lookup_complex_type("complex128");
e440a328 8379 is_print = false;
8380 break;
8381
48080209 8382 case BUILTIN_COMPLEX:
e440a328 8383 {
48080209 8384 // For the complex function the type of one operand can
e440a328 8385 // determine the type of the other, as in a binary expression.
8386 arg_type = Builtin_call_expression::real_imag_type(context->type);
f6bc81e6 8387 if (arg_type == NULL)
8388 arg_type = Type::lookup_float_type("float64");
e440a328 8389 if (args != NULL && args->size() == 2)
8390 {
8391 Type* t1 = args->front()->type();
c849bb59 8392 Type* t2 = args->back()->type();
e440a328 8393 if (!t1->is_abstract())
8394 arg_type = t1;
8395 else if (!t2->is_abstract())
8396 arg_type = t2;
8397 }
8398 is_print = false;
8399 }
8400 break;
8401
321e5ad2 8402 case BUILTIN_APPEND:
8403 if (!this->is_varargs()
8404 && args != NULL
8405 && !args->empty()
8406 && args->front()->type()->is_slice_type())
8407 trailing_arg_types =
8408 args->front()->type()->array_type()->element_type();
8409 is_print = false;
8410 break;
8411
e440a328 8412 default:
8413 is_print = false;
8414 break;
8415 }
8416
8417 if (args != NULL)
8418 {
8419 for (Expression_list::const_iterator pa = args->begin();
8420 pa != args->end();
8421 ++pa)
8422 {
8423 Type_context subcontext;
8424 subcontext.type = arg_type;
8425
8426 if (is_print)
8427 {
8428 // We want to print large constants, we so can't just
8429 // use the appropriate nonabstract type. Use uint64 for
8430 // an integer if we know it is nonnegative, otherwise
8431 // use int64 for a integer, otherwise use float64 for a
8432 // float or complex128 for a complex.
8433 Type* want_type = NULL;
8434 Type* atype = (*pa)->type();
8435 if (atype->is_abstract())
8436 {
8437 if (atype->integer_type() != NULL)
8438 {
0c77715b 8439 Numeric_constant nc;
8440 if (this->numeric_constant_value(&nc))
8441 {
8442 mpz_t val;
8443 if (nc.to_int(&val))
8444 {
8445 if (mpz_sgn(val) >= 0)
8446 want_type = Type::lookup_integer_type("uint64");
8447 mpz_clear(val);
8448 }
8449 }
8450 if (want_type == NULL)
e440a328 8451 want_type = Type::lookup_integer_type("int64");
e440a328 8452 }
8453 else if (atype->float_type() != NULL)
8454 want_type = Type::lookup_float_type("float64");
8455 else if (atype->complex_type() != NULL)
8456 want_type = Type::lookup_complex_type("complex128");
8457 else if (atype->is_abstract_string_type())
8458 want_type = Type::lookup_string_type();
8459 else if (atype->is_abstract_boolean_type())
8460 want_type = Type::lookup_bool_type();
8461 else
c3e6f413 8462 go_unreachable();
e440a328 8463 subcontext.type = want_type;
8464 }
8465 }
8466
8467 (*pa)->determine_type(&subcontext);
321e5ad2 8468
8469 if (trailing_arg_types != NULL)
8470 {
8471 arg_type = trailing_arg_types;
8472 trailing_arg_types = NULL;
8473 }
e440a328 8474 }
8475 }
8476}
8477
8478// If there is exactly one argument, return true. Otherwise give an
8479// error message and return false.
8480
8481bool
8482Builtin_call_expression::check_one_arg()
8483{
8484 const Expression_list* args = this->args();
8485 if (args == NULL || args->size() < 1)
8486 {
8487 this->report_error(_("not enough arguments"));
8488 return false;
8489 }
8490 else if (args->size() > 1)
8491 {
8492 this->report_error(_("too many arguments"));
8493 return false;
8494 }
8495 if (args->front()->is_error_expression()
5c13bd80 8496 || args->front()->type()->is_error())
e440a328 8497 {
8498 this->set_is_error();
8499 return false;
8500 }
8501 return true;
8502}
8503
8504// Check argument types for a builtin function.
8505
8506void
8507Builtin_call_expression::do_check_types(Gogo*)
8508{
375646ea 8509 if (this->is_error_expression())
8510 return;
e440a328 8511 switch (this->code_)
8512 {
8513 case BUILTIN_INVALID:
8514 case BUILTIN_NEW:
8515 case BUILTIN_MAKE:
cd238b8d 8516 case BUILTIN_DELETE:
e440a328 8517 return;
8518
8519 case BUILTIN_LEN:
8520 case BUILTIN_CAP:
8521 {
8522 // The single argument may be either a string or an array or a
8523 // map or a channel, or a pointer to a closed array.
8524 if (this->check_one_arg())
8525 {
8526 Type* arg_type = this->one_arg()->type();
8527 if (arg_type->points_to() != NULL
8528 && arg_type->points_to()->array_type() != NULL
411eb89e 8529 && !arg_type->points_to()->is_slice_type())
e440a328 8530 arg_type = arg_type->points_to();
8531 if (this->code_ == BUILTIN_CAP)
8532 {
5c13bd80 8533 if (!arg_type->is_error()
e440a328 8534 && arg_type->array_type() == NULL
8535 && arg_type->channel_type() == NULL)
8536 this->report_error(_("argument must be array or slice "
8537 "or channel"));
8538 }
8539 else
8540 {
5c13bd80 8541 if (!arg_type->is_error()
e440a328 8542 && !arg_type->is_string_type()
8543 && arg_type->array_type() == NULL
8544 && arg_type->map_type() == NULL
8545 && arg_type->channel_type() == NULL)
8546 this->report_error(_("argument must be string or "
8547 "array or slice or map or channel"));
8548 }
8549 }
8550 }
8551 break;
8552
8553 case BUILTIN_PRINT:
8554 case BUILTIN_PRINTLN:
8555 {
8556 const Expression_list* args = this->args();
8557 if (args == NULL)
8558 {
8559 if (this->code_ == BUILTIN_PRINT)
631d5788 8560 go_warning_at(this->location(), 0,
e440a328 8561 "no arguments for builtin function %<%s%>",
8562 (this->code_ == BUILTIN_PRINT
8563 ? "print"
8564 : "println"));
8565 }
8566 else
8567 {
8568 for (Expression_list::const_iterator p = args->begin();
8569 p != args->end();
8570 ++p)
8571 {
8572 Type* type = (*p)->type();
5c13bd80 8573 if (type->is_error()
e440a328 8574 || type->is_string_type()
8575 || type->integer_type() != NULL
8576 || type->float_type() != NULL
8577 || type->complex_type() != NULL
8578 || type->is_boolean_type()
8579 || type->points_to() != NULL
8580 || type->interface_type() != NULL
8581 || type->channel_type() != NULL
8582 || type->map_type() != NULL
8583 || type->function_type() != NULL
411eb89e 8584 || type->is_slice_type())
e440a328 8585 ;
acf8e158 8586 else if ((*p)->is_type_expression())
8587 {
8588 // If this is a type expression it's going to give
8589 // an error anyhow, so we don't need one here.
8590 }
e440a328 8591 else
8592 this->report_error(_("unsupported argument type to "
8593 "builtin function"));
8594 }
8595 }
8596 }
8597 break;
8598
8599 case BUILTIN_CLOSE:
e440a328 8600 if (this->check_one_arg())
8601 {
8602 if (this->one_arg()->type()->channel_type() == NULL)
8603 this->report_error(_("argument must be channel"));
5202d986 8604 else if (!this->one_arg()->type()->channel_type()->may_send())
8605 this->report_error(_("cannot close receive-only channel"));
e440a328 8606 }
8607 break;
8608
8609 case BUILTIN_PANIC:
8610 case BUILTIN_SIZEOF:
8611 case BUILTIN_ALIGNOF:
8612 this->check_one_arg();
8613 break;
8614
8615 case BUILTIN_RECOVER:
6334270b 8616 if (this->args() != NULL
8617 && !this->args()->empty()
8618 && !this->recover_arg_is_set_)
e440a328 8619 this->report_error(_("too many arguments"));
8620 break;
8621
8622 case BUILTIN_OFFSETOF:
8623 if (this->check_one_arg())
8624 {
8625 Expression* arg = this->one_arg();
8626 if (arg->field_reference_expression() == NULL)
8627 this->report_error(_("argument must be a field reference"));
8628 }
8629 break;
8630
8631 case BUILTIN_COPY:
8632 {
8633 const Expression_list* args = this->args();
8634 if (args == NULL || args->size() < 2)
8635 {
8636 this->report_error(_("not enough arguments"));
8637 break;
8638 }
8639 else if (args->size() > 2)
8640 {
8641 this->report_error(_("too many arguments"));
8642 break;
8643 }
8644 Type* arg1_type = args->front()->type();
8645 Type* arg2_type = args->back()->type();
5c13bd80 8646 if (arg1_type->is_error() || arg2_type->is_error())
6bebb39d 8647 {
8648 this->set_is_error();
8649 break;
8650 }
e440a328 8651
8652 Type* e1;
411eb89e 8653 if (arg1_type->is_slice_type())
e440a328 8654 e1 = arg1_type->array_type()->element_type();
8655 else
8656 {
8657 this->report_error(_("left argument must be a slice"));
8658 break;
8659 }
8660
411eb89e 8661 if (arg2_type->is_slice_type())
60963afd 8662 {
8663 Type* e2 = arg2_type->array_type()->element_type();
8664 if (!Type::are_identical(e1, e2, true, NULL))
8665 this->report_error(_("element types must be the same"));
8666 }
e440a328 8667 else if (arg2_type->is_string_type())
e440a328 8668 {
60963afd 8669 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
8670 this->report_error(_("first argument must be []byte"));
e440a328 8671 }
60963afd 8672 else
8673 this->report_error(_("second argument must be slice or string"));
e440a328 8674 }
8675 break;
8676
8677 case BUILTIN_APPEND:
8678 {
8679 const Expression_list* args = this->args();
321e5ad2 8680 if (args == NULL || args->empty())
e440a328 8681 {
8682 this->report_error(_("not enough arguments"));
8683 break;
8684 }
321e5ad2 8685
8686 Type* slice_type = args->front()->type();
8687 if (!slice_type->is_slice_type())
6bebb39d 8688 {
321e5ad2 8689 if (slice_type->is_error_type())
8690 break;
8691 if (slice_type->is_nil_type())
8692 go_error_at(args->front()->location(), "use of untyped nil");
8693 else
8694 go_error_at(args->front()->location(),
8695 "argument 1 must be a slice");
6bebb39d 8696 this->set_is_error();
8697 break;
8698 }
cd238b8d 8699
321e5ad2 8700 Type* element_type = slice_type->array_type()->element_type();
22deed0d 8701 if (!element_type->in_heap())
8702 go_error_at(args->front()->location(),
8703 "can't append to slice of go:notinheap type");
321e5ad2 8704 if (this->is_varargs())
4fd4fcf4 8705 {
321e5ad2 8706 if (!args->back()->type()->is_slice_type()
8707 && !args->back()->type()->is_string_type())
8708 {
8709 go_error_at(args->back()->location(),
8710 "invalid use of %<...%> with non-slice/non-string");
8711 this->set_is_error();
8712 break;
8713 }
4fd4fcf4 8714
321e5ad2 8715 if (args->size() < 2)
8716 {
8717 this->report_error(_("not enough arguments"));
8718 break;
8719 }
8720 if (args->size() > 2)
8721 {
8722 this->report_error(_("too many arguments"));
8723 break;
8724 }
8725
8726 if (args->back()->type()->is_string_type()
8727 && element_type->integer_type() != NULL
8728 && element_type->integer_type()->is_byte())
8729 {
8730 // Permit append(s1, s2...) when s1 is a slice of
8731 // bytes and s2 is a string type.
8732 }
e440a328 8733 else
8734 {
321e5ad2 8735 // We have to test for assignment compatibility to a
8736 // slice of the element type, which is not necessarily
8737 // the same as the type of the first argument: the
8738 // first argument might have a named type.
8739 Type* check_type = Type::make_array_type(element_type, NULL);
8740 std::string reason;
8741 if (!Type::are_assignable(check_type, args->back()->type(),
8742 &reason))
8743 {
8744 if (reason.empty())
8745 go_error_at(args->back()->location(),
8746 "argument 2 has invalid type");
8747 else
8748 go_error_at(args->back()->location(),
8749 "argument 2 has invalid type (%s)",
8750 reason.c_str());
8751 this->set_is_error();
8752 break;
8753 }
8754 }
8755 }
8756 else
8757 {
8758 Expression_list::const_iterator pa = args->begin();
8759 int i = 2;
8760 for (++pa; pa != args->end(); ++pa, ++i)
8761 {
8762 std::string reason;
8763 if (!Type::are_assignable(element_type, (*pa)->type(),
8764 &reason))
8765 {
8766 if (reason.empty())
8767 go_error_at((*pa)->location(),
8768 "argument %d has incompatible type", i);
8769 else
8770 go_error_at((*pa)->location(),
8771 "argument %d has incompatible type (%s)",
8772 i, reason.c_str());
8773 this->set_is_error();
8774 }
e440a328 8775 }
8776 }
e440a328 8777 }
321e5ad2 8778 break;
e440a328 8779
8780 case BUILTIN_REAL:
8781 case BUILTIN_IMAG:
8782 if (this->check_one_arg())
8783 {
8784 if (this->one_arg()->type()->complex_type() == NULL)
8785 this->report_error(_("argument must have complex type"));
8786 }
8787 break;
8788
48080209 8789 case BUILTIN_COMPLEX:
e440a328 8790 {
8791 const Expression_list* args = this->args();
8792 if (args == NULL || args->size() < 2)
8793 this->report_error(_("not enough arguments"));
8794 else if (args->size() > 2)
8795 this->report_error(_("too many arguments"));
8796 else if (args->front()->is_error_expression()
5c13bd80 8797 || args->front()->type()->is_error()
e440a328 8798 || args->back()->is_error_expression()
5c13bd80 8799 || args->back()->type()->is_error())
e440a328 8800 this->set_is_error();
8801 else if (!Type::are_identical(args->front()->type(),
07ba8be5 8802 args->back()->type(), true, NULL))
48080209 8803 this->report_error(_("complex arguments must have identical types"));
e440a328 8804 else if (args->front()->type()->float_type() == NULL)
48080209 8805 this->report_error(_("complex arguments must have "
e440a328 8806 "floating-point type"));
8807 }
8808 break;
8809
8810 default:
c3e6f413 8811 go_unreachable();
e440a328 8812 }
8813}
8814
72666aed 8815Expression*
8816Builtin_call_expression::do_copy()
8817{
8818 Call_expression* bce =
8819 new Builtin_call_expression(this->gogo_, this->fn()->copy(),
da244e59 8820 (this->args() == NULL
8821 ? NULL
8822 : this->args()->copy()),
72666aed 8823 this->is_varargs(),
8824 this->location());
8825
8826 if (this->varargs_are_lowered())
8827 bce->set_varargs_are_lowered();
8828 return bce;
8829}
8830
ea664253 8831// Return the backend representation for a builtin function.
e440a328 8832
ea664253 8833Bexpression*
8834Builtin_call_expression::do_get_backend(Translate_context* context)
e440a328 8835{
8836 Gogo* gogo = context->gogo();
b13c66cd 8837 Location location = this->location();
a0d8874e 8838
8839 if (this->is_erroneous_call())
8840 {
8841 go_assert(saw_errors());
8842 return gogo->backend()->error_expression();
8843 }
8844
e440a328 8845 switch (this->code_)
8846 {
8847 case BUILTIN_INVALID:
8848 case BUILTIN_NEW:
8849 case BUILTIN_MAKE:
c3e6f413 8850 go_unreachable();
e440a328 8851
8852 case BUILTIN_LEN:
8853 case BUILTIN_CAP:
8854 {
8855 const Expression_list* args = this->args();
c484d925 8856 go_assert(args != NULL && args->size() == 1);
2c809f8f 8857 Expression* arg = args->front();
e440a328 8858 Type* arg_type = arg->type();
0f914071 8859
8860 if (this->seen_)
8861 {
c484d925 8862 go_assert(saw_errors());
ea664253 8863 return context->backend()->error_expression();
0f914071 8864 }
8865 this->seen_ = true;
0f914071 8866 this->seen_ = false;
e440a328 8867 if (arg_type->points_to() != NULL)
8868 {
8869 arg_type = arg_type->points_to();
c484d925 8870 go_assert(arg_type->array_type() != NULL
411eb89e 8871 && !arg_type->is_slice_type());
f614ea8b 8872 arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT,
8873 location);
e440a328 8874 }
8875
1b1f2abf 8876 Type* int_type = Type::lookup_integer_type("int");
2c809f8f 8877 Expression* val;
e440a328 8878 if (this->code_ == BUILTIN_LEN)
8879 {
8880 if (arg_type->is_string_type())
2c809f8f 8881 val = Expression::make_string_info(arg, STRING_INFO_LENGTH,
8882 location);
e440a328 8883 else if (arg_type->array_type() != NULL)
0f914071 8884 {
8885 if (this->seen_)
8886 {
c484d925 8887 go_assert(saw_errors());
ea664253 8888 return context->backend()->error_expression();
0f914071 8889 }
8890 this->seen_ = true;
2c809f8f 8891 val = arg_type->array_type()->get_length(gogo, arg);
0f914071 8892 this->seen_ = false;
8893 }
0d5530d9 8894 else if (arg_type->map_type() != NULL
8895 || arg_type->channel_type() != NULL)
8896 {
8897 // The first field is the length. If the pointer is
8898 // nil, the length is zero.
8899 Type* pint_type = Type::make_pointer_type(int_type);
8900 arg = Expression::make_unsafe_cast(pint_type, arg, location);
8901 Expression* nil = Expression::make_nil(location);
8902 nil = Expression::make_cast(pint_type, nil, location);
8903 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ,
8904 arg, nil, location);
8905 Expression* zero = Expression::make_integer_ul(0, int_type,
8906 location);
f614ea8b 8907 Expression* indir =
8908 Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED,
8909 location);
0d5530d9 8910 val = Expression::make_conditional(cmp, zero, indir, location);
8911 }
e440a328 8912 else
c3e6f413 8913 go_unreachable();
e440a328 8914 }
8915 else
8916 {
8917 if (arg_type->array_type() != NULL)
0f914071 8918 {
8919 if (this->seen_)
8920 {
c484d925 8921 go_assert(saw_errors());
ea664253 8922 return context->backend()->error_expression();
0f914071 8923 }
8924 this->seen_ = true;
2c809f8f 8925 val = arg_type->array_type()->get_capacity(gogo, arg);
0f914071 8926 this->seen_ = false;
8927 }
e440a328 8928 else if (arg_type->channel_type() != NULL)
132ed071 8929 {
8930 // The second field is the capacity. If the pointer
8931 // is nil, the capacity is zero.
8932 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8933 Type* pint_type = Type::make_pointer_type(int_type);
8934 Expression* parg = Expression::make_unsafe_cast(uintptr_type,
8935 arg,
8936 location);
8937 int off = int_type->integer_type()->bits() / 8;
8938 Expression* eoff = Expression::make_integer_ul(off,
8939 uintptr_type,
8940 location);
8941 parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff,
8942 location);
8943 parg = Expression::make_unsafe_cast(pint_type, parg, location);
8944 Expression* nil = Expression::make_nil(location);
8945 nil = Expression::make_cast(pint_type, nil, location);
8946 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ,
8947 arg, nil, location);
8948 Expression* zero = Expression::make_integer_ul(0, int_type,
8949 location);
f614ea8b 8950 Expression* indir =
8951 Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED,
8952 location);
132ed071 8953 val = Expression::make_conditional(cmp, zero, indir, location);
8954 }
e440a328 8955 else
c3e6f413 8956 go_unreachable();
e440a328 8957 }
8958
2c809f8f 8959 return Expression::make_cast(int_type, val,
ea664253 8960 location)->get_backend(context);
e440a328 8961 }
8962
8963 case BUILTIN_PRINT:
8964 case BUILTIN_PRINTLN:
8965 {
8966 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
88b03a70 8967
8968 Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK,
8969 location, 0);
e440a328 8970
8971 const Expression_list* call_args = this->args();
8972 if (call_args != NULL)
8973 {
8974 for (Expression_list::const_iterator p = call_args->begin();
8975 p != call_args->end();
8976 ++p)
8977 {
8978 if (is_ln && p != call_args->begin())
8979 {
2c809f8f 8980 Expression* print_space =
88b03a70 8981 Runtime::make_call(Runtime::PRINTSP, location, 0);
e440a328 8982
2c809f8f 8983 print_stmts =
8984 Expression::make_compound(print_stmts, print_space,
8985 location);
8986 }
e440a328 8987
2c809f8f 8988 Expression* arg = *p;
8989 Type* type = arg->type();
8990 Runtime::Function code;
e440a328 8991 if (type->is_string_type())
88b03a70 8992 code = Runtime::PRINTSTRING;
e440a328 8993 else if (type->integer_type() != NULL
8994 && type->integer_type()->is_unsigned())
8995 {
e440a328 8996 Type* itype = Type::lookup_integer_type("uint64");
2c809f8f 8997 arg = Expression::make_cast(itype, arg, location);
88b03a70 8998 code = Runtime::PRINTUINT;
e440a328 8999 }
9000 else if (type->integer_type() != NULL)
9001 {
e440a328 9002 Type* itype = Type::lookup_integer_type("int64");
2c809f8f 9003 arg = Expression::make_cast(itype, arg, location);
88b03a70 9004 code = Runtime::PRINTINT;
e440a328 9005 }
9006 else if (type->float_type() != NULL)
9007 {
2c809f8f 9008 Type* dtype = Type::lookup_float_type("float64");
9009 arg = Expression::make_cast(dtype, arg, location);
88b03a70 9010 code = Runtime::PRINTFLOAT;
e440a328 9011 }
9012 else if (type->complex_type() != NULL)
9013 {
2c809f8f 9014 Type* ctype = Type::lookup_complex_type("complex128");
9015 arg = Expression::make_cast(ctype, arg, location);
88b03a70 9016 code = Runtime::PRINTCOMPLEX;
e440a328 9017 }
9018 else if (type->is_boolean_type())
88b03a70 9019 code = Runtime::PRINTBOOL;
e440a328 9020 else if (type->points_to() != NULL
9021 || type->channel_type() != NULL
9022 || type->map_type() != NULL
9023 || type->function_type() != NULL)
9024 {
2c809f8f 9025 arg = Expression::make_cast(type, arg, location);
88b03a70 9026 code = Runtime::PRINTPOINTER;
e440a328 9027 }
9028 else if (type->interface_type() != NULL)
9029 {
9030 if (type->interface_type()->is_empty())
88b03a70 9031 code = Runtime::PRINTEFACE;
e440a328 9032 else
88b03a70 9033 code = Runtime::PRINTIFACE;
e440a328 9034 }
411eb89e 9035 else if (type->is_slice_type())
88b03a70 9036 code = Runtime::PRINTSLICE;
e440a328 9037 else
cd238b8d 9038 {
9039 go_assert(saw_errors());
ea664253 9040 return context->backend()->error_expression();
cd238b8d 9041 }
e440a328 9042
2c809f8f 9043 Expression* call = Runtime::make_call(code, location, 1, arg);
88b03a70 9044 print_stmts = Expression::make_compound(print_stmts, call,
9045 location);
e440a328 9046 }
9047 }
9048
9049 if (is_ln)
9050 {
2c809f8f 9051 Expression* print_nl =
88b03a70 9052 Runtime::make_call(Runtime::PRINTNL, location, 0);
9053 print_stmts = Expression::make_compound(print_stmts, print_nl,
9054 location);
e440a328 9055 }
9056
88b03a70 9057 Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK,
9058 location, 0);
9059 print_stmts = Expression::make_compound(print_stmts, unlock, location);
32e3ff69 9060
ea664253 9061 return print_stmts->get_backend(context);
e440a328 9062 }
9063
9064 case BUILTIN_PANIC:
9065 {
9066 const Expression_list* args = this->args();
c484d925 9067 go_assert(args != NULL && args->size() == 1);
e440a328 9068 Expression* arg = args->front();
b13c66cd 9069 Type *empty =
823c7e3d 9070 Type::make_empty_interface_type(Linemap::predeclared_location());
2c809f8f 9071 arg = Expression::convert_for_assignment(gogo, empty, arg, location);
9072
9073 Expression* panic =
03ac9de4 9074 Runtime::make_call(Runtime::GOPANIC, location, 1, arg);
ea664253 9075 return panic->get_backend(context);
e440a328 9076 }
9077
9078 case BUILTIN_RECOVER:
9079 {
9080 // The argument is set when building recover thunks. It's a
9081 // boolean value which is true if we can recover a value now.
9082 const Expression_list* args = this->args();
c484d925 9083 go_assert(args != NULL && args->size() == 1);
e440a328 9084 Expression* arg = args->front();
b13c66cd 9085 Type *empty =
823c7e3d 9086 Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 9087
e440a328 9088 Expression* nil = Expression::make_nil(location);
2c809f8f 9089 nil = Expression::convert_for_assignment(gogo, empty, nil, location);
e440a328 9090
9091 // We need to handle a deferred call to recover specially,
9092 // because it changes whether it can recover a panic or not.
9093 // See test7 in test/recover1.go.
2c809f8f 9094 Expression* recover = Runtime::make_call((this->is_deferred()
03ac9de4 9095 ? Runtime::DEFERREDRECOVER
9096 : Runtime::GORECOVER),
2c809f8f 9097 location, 0);
9098 Expression* cond =
9099 Expression::make_conditional(arg, recover, nil, location);
ea664253 9100 return cond->get_backend(context);
e440a328 9101 }
9102
9103 case BUILTIN_CLOSE:
e440a328 9104 {
9105 const Expression_list* args = this->args();
c484d925 9106 go_assert(args != NULL && args->size() == 1);
e440a328 9107 Expression* arg = args->front();
2c809f8f 9108 Expression* close = Runtime::make_call(Runtime::CLOSE, location,
9109 1, arg);
ea664253 9110 return close->get_backend(context);
e440a328 9111 }
9112
9113 case BUILTIN_SIZEOF:
9114 case BUILTIN_OFFSETOF:
9115 case BUILTIN_ALIGNOF:
9116 {
0c77715b 9117 Numeric_constant nc;
9118 unsigned long val;
9119 if (!this->numeric_constant_value(&nc)
9120 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
7f1d9abd 9121 {
c484d925 9122 go_assert(saw_errors());
ea664253 9123 return context->backend()->error_expression();
7f1d9abd 9124 }
7ba86326 9125 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2c809f8f 9126 mpz_t ival;
9127 nc.get_int(&ival);
9128 Expression* int_cst =
e67508fa 9129 Expression::make_integer_z(&ival, uintptr_type, location);
2c809f8f 9130 mpz_clear(ival);
ea664253 9131 return int_cst->get_backend(context);
e440a328 9132 }
9133
9134 case BUILTIN_COPY:
9135 {
9136 const Expression_list* args = this->args();
c484d925 9137 go_assert(args != NULL && args->size() == 2);
e440a328 9138 Expression* arg1 = args->front();
9139 Expression* arg2 = args->back();
9140
e440a328 9141 Type* arg1_type = arg1->type();
9142 Array_type* at = arg1_type->array_type();
35a54f17 9143 go_assert(arg1->is_variable());
321e5ad2 9144
9145 Expression* call;
e440a328 9146
9147 Type* arg2_type = arg2->type();
2c809f8f 9148 go_assert(arg2->is_variable());
321e5ad2 9149 if (arg2_type->is_string_type())
9150 call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location,
9151 2, arg1, arg2);
e440a328 9152 else
9153 {
321e5ad2 9154 Type* et = at->element_type();
9155 if (et->has_pointer())
9156 {
9157 Expression* td = Expression::make_type_descriptor(et,
9158 location);
9159 call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location,
9160 3, td, arg1, arg2);
9161 }
9162 else
9163 {
9164 Expression* sz = Expression::make_type_info(et,
9165 TYPE_INFO_SIZE);
9166 call = Runtime::make_call(Runtime::SLICECOPY, location, 3,
9167 arg1, arg2, sz);
9168 }
e440a328 9169 }
2c809f8f 9170
321e5ad2 9171 return call->get_backend(context);
e440a328 9172 }
9173
9174 case BUILTIN_APPEND:
321e5ad2 9175 // Handled in Builtin_call_expression::flatten_append.
9176 go_unreachable();
e440a328 9177
9178 case BUILTIN_REAL:
9179 case BUILTIN_IMAG:
9180 {
9181 const Expression_list* args = this->args();
c484d925 9182 go_assert(args != NULL && args->size() == 1);
2c809f8f 9183
9184 Bexpression* ret;
ea664253 9185 Bexpression* bcomplex = args->front()->get_backend(context);
2c809f8f 9186 if (this->code_ == BUILTIN_REAL)
9187 ret = gogo->backend()->real_part_expression(bcomplex, location);
9188 else
9189 ret = gogo->backend()->imag_part_expression(bcomplex, location);
ea664253 9190 return ret;
e440a328 9191 }
9192
48080209 9193 case BUILTIN_COMPLEX:
e440a328 9194 {
9195 const Expression_list* args = this->args();
c484d925 9196 go_assert(args != NULL && args->size() == 2);
ea664253 9197 Bexpression* breal = args->front()->get_backend(context);
9198 Bexpression* bimag = args->back()->get_backend(context);
9199 return gogo->backend()->complex_expression(breal, bimag, location);
e440a328 9200 }
9201
9202 default:
c3e6f413 9203 go_unreachable();
e440a328 9204 }
9205}
9206
9207// We have to support exporting a builtin call expression, because
9208// code can set a constant to the result of a builtin expression.
9209
9210void
9211Builtin_call_expression::do_export(Export* exp) const
9212{
0c77715b 9213 Numeric_constant nc;
9214 if (!this->numeric_constant_value(&nc))
9215 {
631d5788 9216 go_error_at(this->location(), "value is not constant");
0c77715b 9217 return;
9218 }
e440a328 9219
0c77715b 9220 if (nc.is_int())
e440a328 9221 {
0c77715b 9222 mpz_t val;
9223 nc.get_int(&val);
e440a328 9224 Integer_expression::export_integer(exp, val);
0c77715b 9225 mpz_clear(val);
e440a328 9226 }
0c77715b 9227 else if (nc.is_float())
e440a328 9228 {
9229 mpfr_t fval;
0c77715b 9230 nc.get_float(&fval);
9231 Float_expression::export_float(exp, fval);
e440a328 9232 mpfr_clear(fval);
9233 }
0c77715b 9234 else if (nc.is_complex())
e440a328 9235 {
fcbea5e4 9236 mpc_t cval;
9237 nc.get_complex(&cval);
9238 Complex_expression::export_complex(exp, cval);
9239 mpc_clear(cval);
e440a328 9240 }
0c77715b 9241 else
9242 go_unreachable();
e440a328 9243
9244 // A trailing space lets us reliably identify the end of the number.
9245 exp->write_c_string(" ");
9246}
9247
9248// Class Call_expression.
9249
8381eda7 9250// A Go function can be viewed in a couple of different ways. The
9251// code of a Go function becomes a backend function with parameters
9252// whose types are simply the backend representation of the Go types.
9253// If there are multiple results, they are returned as a backend
9254// struct.
9255
9256// However, when Go code refers to a function other than simply
9257// calling it, the backend type of that function is actually a struct.
9258// The first field of the struct points to the Go function code
9259// (sometimes a wrapper as described below). The remaining fields
9260// hold addresses of closed-over variables. This struct is called a
9261// closure.
9262
9263// There are a few cases to consider.
9264
9265// A direct function call of a known function in package scope. In
9266// this case there are no closed-over variables, and we know the name
9267// of the function code. We can simply produce a backend call to the
9268// function directly, and not worry about the closure.
9269
9270// A direct function call of a known function literal. In this case
9271// we know the function code and we know the closure. We generate the
9272// function code such that it expects an additional final argument of
9273// the closure type. We pass the closure as the last argument, after
9274// the other arguments.
9275
9276// An indirect function call. In this case we have a closure. We
9277// load the pointer to the function code from the first field of the
9278// closure. We pass the address of the closure as the last argument.
9279
9280// A call to a method of an interface. Type methods are always at
9281// package scope, so we call the function directly, and don't worry
9282// about the closure.
9283
9284// This means that for a function at package scope we have two cases.
9285// One is the direct call, which has no closure. The other is the
9286// indirect call, which does have a closure. We can't simply ignore
9287// the closure, even though it is the last argument, because that will
9288// fail on targets where the function pops its arguments. So when
9289// generating a closure for a package-scope function we set the
9290// function code pointer in the closure to point to a wrapper
9291// function. This wrapper function accepts a final argument that
9292// points to the closure, ignores it, and calls the real function as a
9293// direct function call. This wrapper will normally be efficient, and
9294// can often simply be a tail call to the real function.
9295
9296// We don't use GCC's static chain pointer because 1) we don't need
9297// it; 2) GCC only permits using a static chain to call a known
9298// function, so we can't use it for an indirect call anyhow. Since we
9299// can't use it for an indirect call, we may as well not worry about
9300// using it for a direct call either.
9301
9302// We pass the closure last rather than first because it means that
9303// the function wrapper we put into a closure for a package-scope
9304// function can normally just be a tail call to the real function.
9305
9306// For method expressions we generate a wrapper that loads the
9307// receiver from the closure and then calls the method. This
9308// unfortunately forces reshuffling the arguments, since there is a
9309// new first argument, but we can't avoid reshuffling either for
9310// method expressions or for indirect calls of package-scope
9311// functions, and since the latter are more common we reshuffle for
9312// method expressions.
9313
9314// Note that the Go code retains the Go types. The extra final
9315// argument only appears when we convert to the backend
9316// representation.
9317
e440a328 9318// Traversal.
9319
9320int
9321Call_expression::do_traverse(Traverse* traverse)
9322{
0c0dacab 9323 // If we are calling a function in a different package that returns
9324 // an unnamed type, this may be the only chance we get to traverse
9325 // that type. We don't traverse this->type_ because it may be a
9326 // Call_multiple_result_type that will just lead back here.
9327 if (this->type_ != NULL && !this->type_->is_error_type())
9328 {
9329 Function_type *fntype = this->get_function_type();
9330 if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT)
9331 return TRAVERSE_EXIT;
9332 }
e440a328 9333 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9334 return TRAVERSE_EXIT;
9335 if (this->args_ != NULL)
9336 {
9337 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9338 return TRAVERSE_EXIT;
9339 }
9340 return TRAVERSE_CONTINUE;
9341}
9342
9343// Lower a call statement.
9344
9345Expression*
ceeb4318 9346Call_expression::do_lower(Gogo* gogo, Named_object* function,
9347 Statement_inserter* inserter, int)
e440a328 9348{
b13c66cd 9349 Location loc = this->location();
09ea332d 9350
ceeb4318 9351 // A type cast can look like a function call.
e440a328 9352 if (this->fn_->is_type_expression()
9353 && this->args_ != NULL
9354 && this->args_->size() == 1)
9355 return Expression::make_cast(this->fn_->type(), this->args_->front(),
09ea332d 9356 loc);
e440a328 9357
88f06749 9358 // Because do_type will return an error type and thus prevent future
9359 // errors, check for that case now to ensure that the error gets
9360 // reported.
37448b10 9361 Function_type* fntype = this->get_function_type();
9362 if (fntype == NULL)
88f06749 9363 {
9364 if (!this->fn_->type()->is_error())
9365 this->report_error(_("expected function"));
5f1045b5 9366 this->set_is_error();
9367 return this;
88f06749 9368 }
9369
e440a328 9370 // Handle an argument which is a call to a function which returns
9371 // multiple results.
9372 if (this->args_ != NULL
9373 && this->args_->size() == 1
37448b10 9374 && this->args_->front()->call_expression() != NULL)
e440a328 9375 {
e440a328 9376 size_t rc = this->args_->front()->call_expression()->result_count();
9377 if (rc > 1
37448b10 9378 && ((fntype->parameters() != NULL
9379 && (fntype->parameters()->size() == rc
9380 || (fntype->is_varargs()
9381 && fntype->parameters()->size() - 1 <= rc)))
9382 || fntype->is_builtin()))
e440a328 9383 {
9384 Call_expression* call = this->args_->front()->call_expression();
e90ecd2d 9385 call->set_is_multi_value_arg();
c33af8e4 9386 if (this->is_varargs_)
9387 {
9388 // It is not clear which result of a multiple result call
9389 // the ellipsis operator should be applied to. If we unpack the
9390 // the call into its individual results here, the ellipsis will be
9391 // applied to the last result.
631d5788 9392 go_error_at(call->location(),
9393 _("multiple-value argument in single-value context"));
c33af8e4 9394 return Expression::make_error(call->location());
9395 }
9396
e440a328 9397 Expression_list* args = new Expression_list;
9398 for (size_t i = 0; i < rc; ++i)
9399 args->push_back(Expression::make_call_result(call, i));
9400 // We can't return a new call expression here, because this
42535814 9401 // one may be referenced by Call_result expressions. We
9402 // also can't delete the old arguments, because we may still
9403 // traverse them somewhere up the call stack. FIXME.
e440a328 9404 this->args_ = args;
9405 }
9406 }
9407
37448b10 9408 // Recognize a call to a builtin function.
9409 if (fntype->is_builtin())
9410 return new Builtin_call_expression(gogo, this->fn_, this->args_,
9411 this->is_varargs_, loc);
9412
ceeb4318 9413 // If this call returns multiple results, create a temporary
5731103c 9414 // variable to hold them.
9415 if (this->result_count() > 1 && this->call_temp_ == NULL)
ceeb4318 9416 {
5731103c 9417 Struct_field_list* sfl = new Struct_field_list();
9418 Function_type* fntype = this->get_function_type();
37448b10 9419 const Typed_identifier_list* results = fntype->results();
5731103c 9420 Location loc = this->location();
9421
9422 int i = 0;
9423 char buf[20];
ceeb4318 9424 for (Typed_identifier_list::const_iterator p = results->begin();
5731103c 9425 p != results->end();
9426 ++p, ++i)
9427 {
9428 snprintf(buf, sizeof buf, "res%d", i);
9429 sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc)));
9430 }
9431
9432 Struct_type* st = Type::make_struct_type(sfl, loc);
9433 st->set_is_struct_incomparable();
9434 this->call_temp_ = Statement::make_temporary(st, NULL, loc);
9435 inserter->insert(this->call_temp_);
ceeb4318 9436 }
9437
e440a328 9438 // Handle a call to a varargs function by packaging up the extra
9439 // parameters.
37448b10 9440 if (fntype->is_varargs())
e440a328 9441 {
e440a328 9442 const Typed_identifier_list* parameters = fntype->parameters();
c484d925 9443 go_assert(parameters != NULL && !parameters->empty());
e440a328 9444 Type* varargs_type = parameters->back().type();
09ea332d 9445 this->lower_varargs(gogo, function, inserter, varargs_type,
0e9a2e72 9446 parameters->size(), SLICE_STORAGE_MAY_ESCAPE);
09ea332d 9447 }
9448
9449 // If this is call to a method, call the method directly passing the
9450 // object as the first parameter.
9451 Bound_method_expression* bme = this->fn_->bound_method_expression();
9452 if (bme != NULL)
9453 {
0afbb937 9454 Named_object* methodfn = bme->function();
09ea332d 9455 Expression* first_arg = bme->first_argument();
9456
9457 // We always pass a pointer when calling a method.
9458 if (first_arg->type()->points_to() == NULL
9459 && !first_arg->type()->is_error())
9460 {
9461 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9462 // We may need to create a temporary variable so that we can
9463 // take the address. We can't do that here because it will
9464 // mess up the order of evaluation.
9465 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9466 ue->set_create_temp();
9467 }
9468
9469 // If we are calling a method which was inherited from an
9470 // embedded struct, and the method did not get a stub, then the
9471 // first type may be wrong.
9472 Type* fatype = bme->first_argument_type();
9473 if (fatype != NULL)
9474 {
9475 if (fatype->points_to() == NULL)
9476 fatype = Type::make_pointer_type(fatype);
9477 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9478 }
9479
9480 Expression_list* new_args = new Expression_list();
9481 new_args->push_back(first_arg);
9482 if (this->args_ != NULL)
9483 {
9484 for (Expression_list::const_iterator p = this->args_->begin();
9485 p != this->args_->end();
9486 ++p)
9487 new_args->push_back(*p);
9488 }
9489
9490 // We have to change in place because this structure may be
9491 // referenced by Call_result_expressions. We can't delete the
9492 // old arguments, because we may be traversing them up in some
9493 // caller. FIXME.
9494 this->args_ = new_args;
0afbb937 9495 this->fn_ = Expression::make_func_reference(methodfn, NULL,
09ea332d 9496 bme->location());
e440a328 9497 }
9498
105f9a24 9499 // Handle a couple of special runtime functions. In the runtime
9500 // package, getcallerpc returns the PC of the caller, and
9501 // getcallersp returns the frame pointer of the caller. Implement
9502 // these by turning them into calls to GCC builtin functions. We
9503 // could implement them in normal code, but then we would have to
9504 // explicitly unwind the stack. These functions are intended to be
9505 // efficient. Note that this technique obviously only works for
9506 // direct calls, but that is the only way they are used. The actual
9507 // argument to these functions is always the address of a parameter;
9508 // we don't need that for the GCC builtin functions, so we just
9509 // ignore it.
9510 if (gogo->compiling_runtime()
9511 && this->args_ != NULL
9512 && this->args_->size() == 1
9513 && gogo->package_name() == "runtime")
9514 {
9515 Func_expression* fe = this->fn_->func_expression();
9516 if (fe != NULL
9517 && fe->named_object()->is_function_declaration()
9518 && fe->named_object()->package() == NULL)
9519 {
9520 std::string n = Gogo::unpack_hidden_name(fe->named_object()->name());
9521 if (n == "getcallerpc")
9522 {
9523 static Named_object* builtin_return_address;
9524 return this->lower_to_builtin(&builtin_return_address,
9525 "__builtin_return_address",
9526 0);
9527 }
9528 else if (n == "getcallersp")
9529 {
9530 static Named_object* builtin_frame_address;
9531 return this->lower_to_builtin(&builtin_frame_address,
9532 "__builtin_frame_address",
9533 1);
9534 }
9535 }
9536 }
9537
e440a328 9538 return this;
9539}
9540
9541// Lower a call to a varargs function. FUNCTION is the function in
9542// which the call occurs--it's not the function we are calling.
9543// VARARGS_TYPE is the type of the varargs parameter, a slice type.
9544// PARAM_COUNT is the number of parameters of the function we are
9545// calling; the last of these parameters will be the varargs
9546// parameter.
9547
09ea332d 9548void
e440a328 9549Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
ceeb4318 9550 Statement_inserter* inserter,
0e9a2e72 9551 Type* varargs_type, size_t param_count,
9552 Slice_storage_escape_disp escape_disp)
e440a328 9553{
03118c21 9554 // When compiling the runtime, varargs slices do not escape. When
9555 // escape analysis becomes the default, this should be changed to
9556 // make it an error if we have a varargs slice that escapes.
9557 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
9558 escape_disp = SLICE_STORAGE_DOES_NOT_ESCAPE;
9559
e440a328 9560 if (this->varargs_are_lowered_)
09ea332d 9561 return;
e440a328 9562
b13c66cd 9563 Location loc = this->location();
e440a328 9564
c484d925 9565 go_assert(param_count > 0);
411eb89e 9566 go_assert(varargs_type->is_slice_type());
e440a328 9567
9568 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9569 if (arg_count < param_count - 1)
9570 {
9571 // Not enough arguments; will be caught in check_types.
09ea332d 9572 return;
e440a328 9573 }
9574
9575 Expression_list* old_args = this->args_;
9576 Expression_list* new_args = new Expression_list();
9577 bool push_empty_arg = false;
9578 if (old_args == NULL || old_args->empty())
9579 {
c484d925 9580 go_assert(param_count == 1);
e440a328 9581 push_empty_arg = true;
9582 }
9583 else
9584 {
9585 Expression_list::const_iterator pa;
9586 int i = 1;
9587 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9588 {
9589 if (static_cast<size_t>(i) == param_count)
9590 break;
9591 new_args->push_back(*pa);
9592 }
9593
9594 // We have reached the varargs parameter.
9595
9596 bool issued_error = false;
9597 if (pa == old_args->end())
9598 push_empty_arg = true;
9599 else if (pa + 1 == old_args->end() && this->is_varargs_)
9600 new_args->push_back(*pa);
9601 else if (this->is_varargs_)
9602 {
a6645f74 9603 if ((*pa)->type()->is_slice_type())
9604 this->report_error(_("too many arguments"));
9605 else
9606 {
631d5788 9607 go_error_at(this->location(),
9608 _("invalid use of %<...%> with non-slice"));
a6645f74 9609 this->set_is_error();
9610 }
09ea332d 9611 return;
e440a328 9612 }
e440a328 9613 else
9614 {
9615 Type* element_type = varargs_type->array_type()->element_type();
9616 Expression_list* vals = new Expression_list;
9617 for (; pa != old_args->end(); ++pa, ++i)
9618 {
9619 // Check types here so that we get a better message.
9620 Type* patype = (*pa)->type();
b13c66cd 9621 Location paloc = (*pa)->location();
e440a328 9622 if (!this->check_argument_type(i, element_type, patype,
9623 paloc, issued_error))
9624 continue;
9625 vals->push_back(*pa);
9626 }
0e9a2e72 9627 Slice_construction_expression* sce =
e440a328 9628 Expression::make_slice_composite_literal(varargs_type, vals, loc);
0e9a2e72 9629 if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE)
9630 sce->set_storage_does_not_escape();
9631 Expression* val = sce;
09ea332d 9632 gogo->lower_expression(function, inserter, &val);
e440a328 9633 new_args->push_back(val);
9634 }
9635 }
9636
9637 if (push_empty_arg)
9638 new_args->push_back(Expression::make_nil(loc));
9639
9640 // We can't return a new call expression here, because this one may
6d4c2432 9641 // be referenced by Call_result expressions. FIXME. We can't
9642 // delete OLD_ARGS because we may have both a Call_expression and a
9643 // Builtin_call_expression which refer to them. FIXME.
e440a328 9644 this->args_ = new_args;
9645 this->varargs_are_lowered_ = true;
e440a328 9646}
9647
105f9a24 9648// Return a call to __builtin_return_address or __builtin_frame_address.
9649
9650Expression*
9651Call_expression::lower_to_builtin(Named_object** pno, const char* name,
9652 int arg)
9653{
9654 if (*pno == NULL)
9655 *pno = Gogo::declare_builtin_rf_address(name);
9656
9657 Location loc = this->location();
9658
9659 Expression* fn = Expression::make_func_reference(*pno, NULL, loc);
9660 Expression* a = Expression::make_integer_ul(arg, NULL, loc);
9661 Expression_list *args = new Expression_list();
9662 args->push_back(a);
9663 Expression* call = Expression::make_call(fn, args, false, loc);
9664
9665 // The builtin functions return void*, but the Go functions return uintptr.
9666 Type* uintptr_type = Type::lookup_integer_type("uintptr");
9667 return Expression::make_cast(uintptr_type, call, loc);
9668}
9669
2c809f8f 9670// Flatten a call with multiple results into a temporary.
9671
9672Expression*
b8e86a51 9673Call_expression::do_flatten(Gogo* gogo, Named_object*,
9674 Statement_inserter* inserter)
2c809f8f 9675{
5bf8be8b 9676 if (this->is_erroneous_call())
9677 {
9678 go_assert(saw_errors());
9679 return Expression::make_error(this->location());
9680 }
b8e86a51 9681
91c0fd76 9682 if (this->is_flattened_)
9683 return this;
9684 this->is_flattened_ = true;
9685
b8e86a51 9686 // Add temporary variables for all arguments that require type
9687 // conversion.
9688 Function_type* fntype = this->get_function_type();
9782d556 9689 if (fntype == NULL)
9690 {
9691 go_assert(saw_errors());
9692 return this;
9693 }
b8e86a51 9694 if (this->args_ != NULL && !this->args_->empty()
9695 && fntype->parameters() != NULL && !fntype->parameters()->empty())
9696 {
9697 bool is_interface_method =
9698 this->fn_->interface_field_reference_expression() != NULL;
9699
9700 Expression_list *args = new Expression_list();
9701 Typed_identifier_list::const_iterator pp = fntype->parameters()->begin();
9702 Expression_list::const_iterator pa = this->args_->begin();
9703 if (!is_interface_method && fntype->is_method())
9704 {
9705 // The receiver argument.
9706 args->push_back(*pa);
9707 ++pa;
9708 }
9709 for (; pa != this->args_->end(); ++pa, ++pp)
9710 {
9711 go_assert(pp != fntype->parameters()->end());
9712 if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL))
9713 args->push_back(*pa);
9714 else
9715 {
9716 Location loc = (*pa)->location();
8ba8cc87 9717 Expression* arg = *pa;
9718 if (!arg->is_variable())
9719 {
9720 Temporary_statement *temp =
9721 Statement::make_temporary(NULL, arg, loc);
9722 inserter->insert(temp);
9723 arg = Expression::make_temporary_reference(temp, loc);
9724 }
9725 arg = Expression::convert_for_assignment(gogo, pp->type(), arg,
9726 loc);
9727 args->push_back(arg);
b8e86a51 9728 }
9729 }
9730 delete this->args_;
9731 this->args_ = args;
9732 }
9733
2c809f8f 9734 return this;
9735}
9736
ceeb4318 9737// Get the function type. This can return NULL in error cases.
e440a328 9738
9739Function_type*
9740Call_expression::get_function_type() const
9741{
9742 return this->fn_->type()->function_type();
9743}
9744
9745// Return the number of values which this call will return.
9746
9747size_t
9748Call_expression::result_count() const
9749{
9750 const Function_type* fntype = this->get_function_type();
9751 if (fntype == NULL)
9752 return 0;
9753 if (fntype->results() == NULL)
9754 return 0;
9755 return fntype->results()->size();
9756}
9757
5731103c 9758// Return the temporary that holds the result for a call with multiple
9759// results.
ceeb4318 9760
9761Temporary_statement*
5731103c 9762Call_expression::results() const
ceeb4318 9763{
5731103c 9764 if (this->call_temp_ == NULL)
cd238b8d 9765 {
9766 go_assert(saw_errors());
9767 return NULL;
9768 }
5731103c 9769 return this->call_temp_;
ceeb4318 9770}
9771
1373401e 9772// Set the number of results expected from a call expression.
9773
9774void
9775Call_expression::set_expected_result_count(size_t count)
9776{
9777 go_assert(this->expected_result_count_ == 0);
9778 this->expected_result_count_ = count;
9779}
9780
e440a328 9781// Return whether this is a call to the predeclared function recover.
9782
9783bool
9784Call_expression::is_recover_call() const
9785{
9786 return this->do_is_recover_call();
9787}
9788
9789// Set the argument to the recover function.
9790
9791void
9792Call_expression::set_recover_arg(Expression* arg)
9793{
9794 this->do_set_recover_arg(arg);
9795}
9796
9797// Virtual functions also implemented by Builtin_call_expression.
9798
9799bool
9800Call_expression::do_is_recover_call() const
9801{
9802 return false;
9803}
9804
9805void
9806Call_expression::do_set_recover_arg(Expression*)
9807{
c3e6f413 9808 go_unreachable();
e440a328 9809}
9810
ceeb4318 9811// We have found an error with this call expression; return true if
9812// we should report it.
9813
9814bool
9815Call_expression::issue_error()
9816{
9817 if (this->issued_error_)
9818 return false;
9819 else
9820 {
9821 this->issued_error_ = true;
9822 return true;
9823 }
9824}
9825
5bf8be8b 9826// Whether or not this call contains errors, either in the call or the
9827// arguments to the call.
9828
9829bool
9830Call_expression::is_erroneous_call()
9831{
9832 if (this->is_error_expression() || this->fn()->is_error_expression())
9833 return true;
9834
9835 if (this->args() == NULL)
9836 return false;
9837 for (Expression_list::iterator pa = this->args()->begin();
9838 pa != this->args()->end();
9839 ++pa)
9840 {
9841 if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression())
9842 return true;
9843 }
9844 return false;
9845}
9846
e440a328 9847// Get the type.
9848
9849Type*
9850Call_expression::do_type()
9851{
9852 if (this->type_ != NULL)
9853 return this->type_;
9854
9855 Type* ret;
9856 Function_type* fntype = this->get_function_type();
9857 if (fntype == NULL)
9858 return Type::make_error_type();
9859
9860 const Typed_identifier_list* results = fntype->results();
9861 if (results == NULL)
9862 ret = Type::make_void_type();
9863 else if (results->size() == 1)
9864 ret = results->begin()->type();
9865 else
9866 ret = Type::make_call_multiple_result_type(this);
9867
9868 this->type_ = ret;
9869
9870 return this->type_;
9871}
9872
9873// Determine types for a call expression. We can use the function
9874// parameter types to set the types of the arguments.
9875
9876void
9877Call_expression::do_determine_type(const Type_context*)
9878{
fb94b0ca 9879 if (!this->determining_types())
9880 return;
9881
e440a328 9882 this->fn_->determine_type_no_context();
9883 Function_type* fntype = this->get_function_type();
9884 const Typed_identifier_list* parameters = NULL;
9885 if (fntype != NULL)
9886 parameters = fntype->parameters();
9887 if (this->args_ != NULL)
9888 {
9889 Typed_identifier_list::const_iterator pt;
9890 if (parameters != NULL)
9891 pt = parameters->begin();
09ea332d 9892 bool first = true;
e440a328 9893 for (Expression_list::const_iterator pa = this->args_->begin();
9894 pa != this->args_->end();
9895 ++pa)
9896 {
09ea332d 9897 if (first)
9898 {
9899 first = false;
9900 // If this is a method, the first argument is the
9901 // receiver.
9902 if (fntype != NULL && fntype->is_method())
9903 {
9904 Type* rtype = fntype->receiver()->type();
9905 // The receiver is always passed as a pointer.
9906 if (rtype->points_to() == NULL)
9907 rtype = Type::make_pointer_type(rtype);
9908 Type_context subcontext(rtype, false);
9909 (*pa)->determine_type(&subcontext);
9910 continue;
9911 }
9912 }
9913
e440a328 9914 if (parameters != NULL && pt != parameters->end())
9915 {
9916 Type_context subcontext(pt->type(), false);
9917 (*pa)->determine_type(&subcontext);
9918 ++pt;
9919 }
9920 else
9921 (*pa)->determine_type_no_context();
9922 }
9923 }
9924}
9925
fb94b0ca 9926// Called when determining types for a Call_expression. Return true
9927// if we should go ahead, false if they have already been determined.
9928
9929bool
9930Call_expression::determining_types()
9931{
9932 if (this->types_are_determined_)
9933 return false;
9934 else
9935 {
9936 this->types_are_determined_ = true;
9937 return true;
9938 }
9939}
9940
e440a328 9941// Check types for parameter I.
9942
9943bool
9944Call_expression::check_argument_type(int i, const Type* parameter_type,
9945 const Type* argument_type,
b13c66cd 9946 Location argument_location,
e440a328 9947 bool issued_error)
9948{
9949 std::string reason;
1eae365b 9950 if (!Type::are_assignable(parameter_type, argument_type, &reason))
e440a328 9951 {
9952 if (!issued_error)
9953 {
9954 if (reason.empty())
631d5788 9955 go_error_at(argument_location, "argument %d has incompatible type", i);
e440a328 9956 else
631d5788 9957 go_error_at(argument_location,
9958 "argument %d has incompatible type (%s)",
9959 i, reason.c_str());
e440a328 9960 }
9961 this->set_is_error();
9962 return false;
9963 }
9964 return true;
9965}
9966
9967// Check types.
9968
9969void
9970Call_expression::do_check_types(Gogo*)
9971{
a6645f74 9972 if (this->classification() == EXPRESSION_ERROR)
9973 return;
9974
e440a328 9975 Function_type* fntype = this->get_function_type();
9976 if (fntype == NULL)
9977 {
5c13bd80 9978 if (!this->fn_->type()->is_error())
e440a328 9979 this->report_error(_("expected function"));
9980 return;
9981 }
9982
1373401e 9983 if (this->expected_result_count_ != 0
9984 && this->expected_result_count_ != this->result_count())
9985 {
9986 if (this->issue_error())
9987 this->report_error(_("function result count mismatch"));
9988 this->set_is_error();
9989 return;
9990 }
9991
09ea332d 9992 bool is_method = fntype->is_method();
9993 if (is_method)
e440a328 9994 {
09ea332d 9995 go_assert(this->args_ != NULL && !this->args_->empty());
9996 Type* rtype = fntype->receiver()->type();
9997 Expression* first_arg = this->args_->front();
1eae365b 9998 // We dereference the values since receivers are always passed
9999 // as pointers.
09ea332d 10000 std::string reason;
1eae365b 10001 if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(),
10002 &reason))
e440a328 10003 {
09ea332d 10004 if (reason.empty())
10005 this->report_error(_("incompatible type for receiver"));
10006 else
e440a328 10007 {
631d5788 10008 go_error_at(this->location(),
10009 "incompatible type for receiver (%s)",
10010 reason.c_str());
09ea332d 10011 this->set_is_error();
e440a328 10012 }
10013 }
10014 }
10015
10016 // Note that varargs was handled by the lower_varargs() method, so
a6645f74 10017 // we don't have to worry about it here unless something is wrong.
10018 if (this->is_varargs_ && !this->varargs_are_lowered_)
10019 {
10020 if (!fntype->is_varargs())
10021 {
631d5788 10022 go_error_at(this->location(),
10023 _("invalid use of %<...%> calling non-variadic function"));
a6645f74 10024 this->set_is_error();
10025 return;
10026 }
10027 }
e440a328 10028
10029 const Typed_identifier_list* parameters = fntype->parameters();
10030 if (this->args_ == NULL)
10031 {
10032 if (parameters != NULL && !parameters->empty())
10033 this->report_error(_("not enough arguments"));
10034 }
10035 else if (parameters == NULL)
09ea332d 10036 {
10037 if (!is_method || this->args_->size() > 1)
10038 this->report_error(_("too many arguments"));
10039 }
1373401e 10040 else if (this->args_->size() == 1
10041 && this->args_->front()->call_expression() != NULL
10042 && this->args_->front()->call_expression()->result_count() > 1)
10043 {
10044 // This is F(G()) when G returns more than one result. If the
10045 // results can be matched to parameters, it would have been
10046 // lowered in do_lower. If we get here we know there is a
10047 // mismatch.
10048 if (this->args_->front()->call_expression()->result_count()
10049 < parameters->size())
10050 this->report_error(_("not enough arguments"));
10051 else
10052 this->report_error(_("too many arguments"));
10053 }
e440a328 10054 else
10055 {
10056 int i = 0;
09ea332d 10057 Expression_list::const_iterator pa = this->args_->begin();
10058 if (is_method)
10059 ++pa;
10060 for (Typed_identifier_list::const_iterator pt = parameters->begin();
10061 pt != parameters->end();
10062 ++pt, ++pa, ++i)
e440a328 10063 {
09ea332d 10064 if (pa == this->args_->end())
e440a328 10065 {
09ea332d 10066 this->report_error(_("not enough arguments"));
e440a328 10067 return;
10068 }
10069 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
10070 (*pa)->location(), false);
10071 }
09ea332d 10072 if (pa != this->args_->end())
10073 this->report_error(_("too many arguments"));
e440a328 10074 }
10075}
10076
72666aed 10077Expression*
10078Call_expression::do_copy()
10079{
10080 Call_expression* call =
10081 Expression::make_call(this->fn_->copy(),
10082 (this->args_ == NULL
10083 ? NULL
10084 : this->args_->copy()),
10085 this->is_varargs_, this->location());
10086
10087 if (this->varargs_are_lowered_)
10088 call->set_varargs_are_lowered();
10089 return call;
10090}
10091
e440a328 10092// Return whether we have to use a temporary variable to ensure that
10093// we evaluate this call expression in order. If the call returns no
ceeb4318 10094// results then it will inevitably be executed last.
e440a328 10095
10096bool
10097Call_expression::do_must_eval_in_order() const
10098{
ceeb4318 10099 return this->result_count() > 0;
e440a328 10100}
10101
e440a328 10102// Get the function and the first argument to use when calling an
10103// interface method.
10104
2387f644 10105Expression*
e440a328 10106Call_expression::interface_method_function(
e440a328 10107 Interface_field_reference_expression* interface_method,
db122cb9 10108 Expression** first_arg_ptr,
10109 Location location)
e440a328 10110{
db122cb9 10111 Expression* object = interface_method->get_underlying_object();
10112 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
10113 *first_arg_ptr =
10114 Expression::make_unsafe_cast(unsafe_ptr_type, object, location);
2387f644 10115 return interface_method->get_function();
e440a328 10116}
10117
10118// Build the call expression.
10119
ea664253 10120Bexpression*
10121Call_expression::do_get_backend(Translate_context* context)
e440a328 10122{
5731103c 10123 Location location = this->location();
10124
2c809f8f 10125 if (this->call_ != NULL)
5731103c 10126 {
10127 // If the call returns multiple results, make a new reference to
10128 // the temporary.
10129 if (this->call_temp_ != NULL)
10130 {
10131 Expression* ref =
10132 Expression::make_temporary_reference(this->call_temp_, location);
10133 return ref->get_backend(context);
10134 }
10135
10136 return this->call_;
10137 }
e440a328 10138
10139 Function_type* fntype = this->get_function_type();
10140 if (fntype == NULL)
ea664253 10141 return context->backend()->error_expression();
e440a328 10142
10143 if (this->fn_->is_error_expression())
ea664253 10144 return context->backend()->error_expression();
e440a328 10145
10146 Gogo* gogo = context->gogo();
e440a328 10147
10148 Func_expression* func = this->fn_->func_expression();
e440a328 10149 Interface_field_reference_expression* interface_method =
10150 this->fn_->interface_field_reference_expression();
10151 const bool has_closure = func != NULL && func->closure() != NULL;
09ea332d 10152 const bool is_interface_method = interface_method != NULL;
e440a328 10153
f8bdf81a 10154 bool has_closure_arg;
8381eda7 10155 if (has_closure)
f8bdf81a 10156 has_closure_arg = true;
8381eda7 10157 else if (func != NULL)
f8bdf81a 10158 has_closure_arg = false;
8381eda7 10159 else if (is_interface_method)
f8bdf81a 10160 has_closure_arg = false;
8381eda7 10161 else
f8bdf81a 10162 has_closure_arg = true;
8381eda7 10163
e440a328 10164 int nargs;
2c809f8f 10165 std::vector<Bexpression*> fn_args;
e440a328 10166 if (this->args_ == NULL || this->args_->empty())
10167 {
f8bdf81a 10168 nargs = is_interface_method ? 1 : 0;
2c809f8f 10169 if (nargs > 0)
10170 fn_args.resize(1);
e440a328 10171 }
09ea332d 10172 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
10173 {
10174 // Passing a receiver parameter.
10175 go_assert(!is_interface_method
10176 && fntype->is_method()
10177 && this->args_->size() == 1);
f8bdf81a 10178 nargs = 1;
2c809f8f 10179 fn_args.resize(1);
ea664253 10180 fn_args[0] = this->args_->front()->get_backend(context);
09ea332d 10181 }
e440a328 10182 else
10183 {
10184 const Typed_identifier_list* params = fntype->parameters();
e440a328 10185
10186 nargs = this->args_->size();
09ea332d 10187 int i = is_interface_method ? 1 : 0;
e440a328 10188 nargs += i;
2c809f8f 10189 fn_args.resize(nargs);
e440a328 10190
10191 Typed_identifier_list::const_iterator pp = params->begin();
09ea332d 10192 Expression_list::const_iterator pe = this->args_->begin();
10193 if (!is_interface_method && fntype->is_method())
10194 {
ea664253 10195 fn_args[i] = (*pe)->get_backend(context);
09ea332d 10196 ++pe;
10197 ++i;
10198 }
10199 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
e440a328 10200 {
c484d925 10201 go_assert(pp != params->end());
2c809f8f 10202 Expression* arg =
10203 Expression::convert_for_assignment(gogo, pp->type(), *pe,
10204 location);
ea664253 10205 fn_args[i] = arg->get_backend(context);
e440a328 10206 }
c484d925 10207 go_assert(pp == params->end());
f8bdf81a 10208 go_assert(i == nargs);
e440a328 10209 }
10210
2c809f8f 10211 Expression* fn;
10212 Expression* closure = NULL;
8381eda7 10213 if (func != NULL)
10214 {
10215 Named_object* no = func->named_object();
2c809f8f 10216 fn = Expression::make_func_code_reference(no, location);
10217 if (has_closure)
10218 closure = func->closure();
8381eda7 10219 }
09ea332d 10220 else if (!is_interface_method)
8381eda7 10221 {
2c809f8f 10222 closure = this->fn_;
10223
10224 // The backend representation of this function type is a pointer
10225 // to a struct whose first field is the actual function to call.
10226 Type* pfntype =
10227 Type::make_pointer_type(
10228 Type::make_pointer_type(Type::make_void_type()));
10229 fn = Expression::make_unsafe_cast(pfntype, this->fn_, location);
f614ea8b 10230 fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location);
2c809f8f 10231 }
e440a328 10232 else
cf609de4 10233 {
2387f644 10234 Expression* first_arg;
db122cb9 10235 fn = this->interface_method_function(interface_method, &first_arg,
10236 location);
ea664253 10237 fn_args[0] = first_arg->get_backend(context);
e440a328 10238 }
10239
1ecc6157 10240 Bexpression* bclosure = NULL;
10241 if (has_closure_arg)
10242 bclosure = closure->get_backend(context);
f8bdf81a 10243 else
1ecc6157 10244 go_assert(closure == NULL);
f8bdf81a 10245
ea664253 10246 Bexpression* bfn = fn->get_backend(context);
80d1e1a8 10247
10248 // When not calling a named function directly, use a type conversion
10249 // in case the type of the function is a recursive type which refers
10250 // to itself. We don't do this for an interface method because 1)
10251 // an interface method never refers to itself, so we always have a
10252 // function type here; 2) we pass an extra first argument to an
10253 // interface method, so fntype is not correct.
10254 if (func == NULL && !is_interface_method)
10255 {
10256 Btype* bft = fntype->get_backend_fntype(gogo);
10257 bfn = gogo->backend()->convert_expression(bft, bfn, location);
10258 }
10259
4ced7af9 10260 Bfunction* bfunction = NULL;
10261 if (context->function())
10262 bfunction = context->function()->func_value()->get_decl();
10263 Bexpression* call = gogo->backend()->call_expression(bfunction, bfn,
10264 fn_args, bclosure,
10265 location);
e440a328 10266
5731103c 10267 if (this->call_temp_ != NULL)
e440a328 10268 {
5731103c 10269 // This case occurs when the call returns multiple results.
e440a328 10270
5731103c 10271 Expression* ref = Expression::make_temporary_reference(this->call_temp_,
10272 location);
10273 Bexpression* bref = ref->get_backend(context);
10274 Bstatement* bassn = gogo->backend()->assignment_statement(bfunction,
10275 bref, call,
10276 location);
e440a328 10277
5731103c 10278 ref = Expression::make_temporary_reference(this->call_temp_, location);
10279 this->call_ = ref->get_backend(context);
10280
10281 return gogo->backend()->compound_expression(bassn, this->call_,
10282 location);
2c809f8f 10283 }
e440a328 10284
2c809f8f 10285 this->call_ = call;
ea664253 10286 return this->call_;
e440a328 10287}
10288
d751bb78 10289// Dump ast representation for a call expressin.
10290
10291void
10292Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
10293{
10294 this->fn_->dump_expression(ast_dump_context);
10295 ast_dump_context->ostream() << "(";
10296 if (args_ != NULL)
10297 ast_dump_context->dump_expression_list(this->args_);
10298
10299 ast_dump_context->ostream() << ") ";
10300}
10301
e440a328 10302// Make a call expression.
10303
10304Call_expression*
10305Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
b13c66cd 10306 Location location)
e440a328 10307{
10308 return new Call_expression(fn, args, is_varargs, location);
10309}
10310
da244e59 10311// Class Call_result_expression.
e440a328 10312
10313// Traverse a call result.
10314
10315int
10316Call_result_expression::do_traverse(Traverse* traverse)
10317{
10318 if (traverse->remember_expression(this->call_))
10319 {
10320 // We have already traversed the call expression.
10321 return TRAVERSE_CONTINUE;
10322 }
10323 return Expression::traverse(&this->call_, traverse);
10324}
10325
10326// Get the type.
10327
10328Type*
10329Call_result_expression::do_type()
10330{
425dd051 10331 if (this->classification() == EXPRESSION_ERROR)
10332 return Type::make_error_type();
10333
e440a328 10334 // THIS->CALL_ can be replaced with a temporary reference due to
10335 // Call_expression::do_must_eval_in_order when there is an error.
10336 Call_expression* ce = this->call_->call_expression();
10337 if (ce == NULL)
5e85f268 10338 {
10339 this->set_is_error();
10340 return Type::make_error_type();
10341 }
e440a328 10342 Function_type* fntype = ce->get_function_type();
10343 if (fntype == NULL)
5e85f268 10344 {
e37658e2 10345 if (ce->issue_error())
99b3f06f 10346 {
10347 if (!ce->fn()->type()->is_error())
10348 this->report_error(_("expected function"));
10349 }
5e85f268 10350 this->set_is_error();
10351 return Type::make_error_type();
10352 }
e440a328 10353 const Typed_identifier_list* results = fntype->results();
ceeb4318 10354 if (results == NULL || results->size() < 2)
7b8d861f 10355 {
ceeb4318 10356 if (ce->issue_error())
10357 this->report_error(_("number of results does not match "
10358 "number of values"));
7b8d861f 10359 return Type::make_error_type();
10360 }
e440a328 10361 Typed_identifier_list::const_iterator pr = results->begin();
10362 for (unsigned int i = 0; i < this->index_; ++i)
10363 {
10364 if (pr == results->end())
425dd051 10365 break;
e440a328 10366 ++pr;
10367 }
10368 if (pr == results->end())
425dd051 10369 {
ceeb4318 10370 if (ce->issue_error())
10371 this->report_error(_("number of results does not match "
10372 "number of values"));
425dd051 10373 return Type::make_error_type();
10374 }
e440a328 10375 return pr->type();
10376}
10377
425dd051 10378// Check the type. Just make sure that we trigger the warning in
10379// do_type.
e440a328 10380
10381void
10382Call_result_expression::do_check_types(Gogo*)
10383{
425dd051 10384 this->type();
e440a328 10385}
10386
10387// Determine the type. We have nothing to do here, but the 0 result
10388// needs to pass down to the caller.
10389
10390void
10391Call_result_expression::do_determine_type(const Type_context*)
10392{
fb94b0ca 10393 this->call_->determine_type_no_context();
e440a328 10394}
10395
ea664253 10396// Return the backend representation. We just refer to the temporary set by the
10397// call expression. We don't do this at lowering time because it makes it
ceeb4318 10398// hard to evaluate the call at the right time.
e440a328 10399
ea664253 10400Bexpression*
10401Call_result_expression::do_get_backend(Translate_context* context)
e440a328 10402{
ceeb4318 10403 Call_expression* ce = this->call_->call_expression();
cd238b8d 10404 if (ce == NULL)
10405 {
10406 go_assert(this->call_->is_error_expression());
ea664253 10407 return context->backend()->error_expression();
cd238b8d 10408 }
5731103c 10409 Temporary_statement* ts = ce->results();
cd238b8d 10410 if (ts == NULL)
10411 {
10412 go_assert(saw_errors());
ea664253 10413 return context->backend()->error_expression();
cd238b8d 10414 }
ceeb4318 10415 Expression* ref = Expression::make_temporary_reference(ts, this->location());
5731103c 10416 ref = Expression::make_field_reference(ref, this->index_, this->location());
ea664253 10417 return ref->get_backend(context);
e440a328 10418}
10419
d751bb78 10420// Dump ast representation for a call result expression.
10421
10422void
10423Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10424 const
10425{
10426 // FIXME: Wouldn't it be better if the call is assigned to a temporary
10427 // (struct) and the fields are referenced instead.
10428 ast_dump_context->ostream() << this->index_ << "@(";
10429 ast_dump_context->dump_expression(this->call_);
10430 ast_dump_context->ostream() << ")";
10431}
10432
e440a328 10433// Make a reference to a single result of a call which returns
10434// multiple results.
10435
10436Expression*
10437Expression::make_call_result(Call_expression* call, unsigned int index)
10438{
10439 return new Call_result_expression(call, index);
10440}
10441
10442// Class Index_expression.
10443
10444// Traversal.
10445
10446int
10447Index_expression::do_traverse(Traverse* traverse)
10448{
10449 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
10450 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
10451 || (this->end_ != NULL
acf2b673 10452 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10453 || (this->cap_ != NULL
10454 && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT))
e440a328 10455 return TRAVERSE_EXIT;
10456 return TRAVERSE_CONTINUE;
10457}
10458
10459// Lower an index expression. This converts the generic index
10460// expression into an array index, a string index, or a map index.
10461
10462Expression*
ceeb4318 10463Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 10464{
b13c66cd 10465 Location location = this->location();
e440a328 10466 Expression* left = this->left_;
10467 Expression* start = this->start_;
10468 Expression* end = this->end_;
acf2b673 10469 Expression* cap = this->cap_;
e440a328 10470
10471 Type* type = left->type();
5c13bd80 10472 if (type->is_error())
d9f3743a 10473 {
10474 go_assert(saw_errors());
10475 return Expression::make_error(location);
10476 }
b0cf7ddd 10477 else if (left->is_type_expression())
10478 {
631d5788 10479 go_error_at(location, "attempt to index type expression");
b0cf7ddd 10480 return Expression::make_error(location);
10481 }
e440a328 10482 else if (type->array_type() != NULL)
acf2b673 10483 return Expression::make_array_index(left, start, end, cap, location);
e440a328 10484 else if (type->points_to() != NULL
10485 && type->points_to()->array_type() != NULL
411eb89e 10486 && !type->points_to()->is_slice_type())
e440a328 10487 {
f614ea8b 10488 Expression* deref =
10489 Expression::make_dereference(left, NIL_CHECK_DEFAULT, location);
38092374 10490
10491 // For an ordinary index into the array, the pointer will be
10492 // dereferenced. For a slice it will not--the resulting slice
10493 // will simply reuse the pointer, which is incorrect if that
10494 // pointer is nil.
10495 if (end != NULL || cap != NULL)
10496 deref->issue_nil_check();
10497
acf2b673 10498 return Expression::make_array_index(deref, start, end, cap, location);
e440a328 10499 }
10500 else if (type->is_string_type())
acf2b673 10501 {
10502 if (cap != NULL)
10503 {
631d5788 10504 go_error_at(location, "invalid 3-index slice of string");
acf2b673 10505 return Expression::make_error(location);
10506 }
10507 return Expression::make_string_index(left, start, end, location);
10508 }
e440a328 10509 else if (type->map_type() != NULL)
10510 {
acf2b673 10511 if (end != NULL || cap != NULL)
e440a328 10512 {
631d5788 10513 go_error_at(location, "invalid slice of map");
e440a328 10514 return Expression::make_error(location);
10515 }
0d5530d9 10516 return Expression::make_map_index(left, start, location);
e440a328 10517 }
b1aba207 10518 else if (cap != NULL)
10519 {
10520 go_error_at(location,
10521 "invalid 3-index slice of object that is not a slice");
10522 return Expression::make_error(location);
10523 }
10524 else if (end != NULL)
10525 {
10526 go_error_at(location,
10527 ("attempt to slice object that is not "
10528 "array, slice, or string"));
10529 return Expression::make_error(location);
10530 }
e440a328 10531 else
10532 {
631d5788 10533 go_error_at(location,
b1aba207 10534 ("attempt to index object that is not "
10535 "array, slice, string, or map"));
e440a328 10536 return Expression::make_error(location);
10537 }
10538}
10539
acf2b673 10540// Write an indexed expression
10541// (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
d751bb78 10542
10543void
10544Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10545 const Expression* expr,
10546 const Expression* start,
acf2b673 10547 const Expression* end,
10548 const Expression* cap)
d751bb78 10549{
10550 expr->dump_expression(ast_dump_context);
10551 ast_dump_context->ostream() << "[";
10552 start->dump_expression(ast_dump_context);
10553 if (end != NULL)
10554 {
10555 ast_dump_context->ostream() << ":";
10556 end->dump_expression(ast_dump_context);
10557 }
acf2b673 10558 if (cap != NULL)
10559 {
10560 ast_dump_context->ostream() << ":";
10561 cap->dump_expression(ast_dump_context);
10562 }
d751bb78 10563 ast_dump_context->ostream() << "]";
10564}
10565
10566// Dump ast representation for an index expression.
10567
10568void
10569Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10570 const
10571{
10572 Index_expression::dump_index_expression(ast_dump_context, this->left_,
acf2b673 10573 this->start_, this->end_, this->cap_);
d751bb78 10574}
10575
e440a328 10576// Make an index expression.
10577
10578Expression*
10579Expression::make_index(Expression* left, Expression* start, Expression* end,
acf2b673 10580 Expression* cap, Location location)
e440a328 10581{
acf2b673 10582 return new Index_expression(left, start, end, cap, location);
e440a328 10583}
10584
da244e59 10585// Class Array_index_expression.
e440a328 10586
10587// Array index traversal.
10588
10589int
10590Array_index_expression::do_traverse(Traverse* traverse)
10591{
10592 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10593 return TRAVERSE_EXIT;
10594 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10595 return TRAVERSE_EXIT;
10596 if (this->end_ != NULL)
10597 {
10598 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10599 return TRAVERSE_EXIT;
10600 }
acf2b673 10601 if (this->cap_ != NULL)
10602 {
10603 if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
10604 return TRAVERSE_EXIT;
10605 }
e440a328 10606 return TRAVERSE_CONTINUE;
10607}
10608
10609// Return the type of an array index.
10610
10611Type*
10612Array_index_expression::do_type()
10613{
10614 if (this->type_ == NULL)
10615 {
10616 Array_type* type = this->array_->type()->array_type();
10617 if (type == NULL)
10618 this->type_ = Type::make_error_type();
10619 else if (this->end_ == NULL)
10620 this->type_ = type->element_type();
411eb89e 10621 else if (type->is_slice_type())
e440a328 10622 {
10623 // A slice of a slice has the same type as the original
10624 // slice.
10625 this->type_ = this->array_->type()->deref();
10626 }
10627 else
10628 {
10629 // A slice of an array is a slice.
10630 this->type_ = Type::make_array_type(type->element_type(), NULL);
10631 }
10632 }
10633 return this->type_;
10634}
10635
10636// Set the type of an array index.
10637
10638void
10639Array_index_expression::do_determine_type(const Type_context*)
10640{
10641 this->array_->determine_type_no_context();
f77aa642 10642
10643 Type_context index_context(Type::lookup_integer_type("int"), false);
10644 if (this->start_->is_constant())
10645 this->start_->determine_type(&index_context);
10646 else
10647 this->start_->determine_type_no_context();
e440a328 10648 if (this->end_ != NULL)
f77aa642 10649 {
10650 if (this->end_->is_constant())
10651 this->end_->determine_type(&index_context);
10652 else
10653 this->end_->determine_type_no_context();
10654 }
acf2b673 10655 if (this->cap_ != NULL)
f77aa642 10656 {
10657 if (this->cap_->is_constant())
10658 this->cap_->determine_type(&index_context);
10659 else
10660 this->cap_->determine_type_no_context();
10661 }
e440a328 10662}
10663
10664// Check types of an array index.
10665
10666void
d0a50ed8 10667Array_index_expression::do_check_types(Gogo* gogo)
e440a328 10668{
f6bc81e6 10669 Numeric_constant nc;
10670 unsigned long v;
10671 if (this->start_->type()->integer_type() == NULL
10672 && !this->start_->type()->is_error()
10673 && (!this->start_->numeric_constant_value(&nc)
10674 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 10675 this->report_error(_("index must be integer"));
10676 if (this->end_ != NULL
10677 && this->end_->type()->integer_type() == NULL
99b3f06f 10678 && !this->end_->type()->is_error()
10679 && !this->end_->is_nil_expression()
f6bc81e6 10680 && !this->end_->is_error_expression()
10681 && (!this->end_->numeric_constant_value(&nc)
10682 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 10683 this->report_error(_("slice end must be integer"));
acf2b673 10684 if (this->cap_ != NULL
10685 && this->cap_->type()->integer_type() == NULL
10686 && !this->cap_->type()->is_error()
10687 && !this->cap_->is_nil_expression()
10688 && !this->cap_->is_error_expression()
10689 && (!this->cap_->numeric_constant_value(&nc)
10690 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
10691 this->report_error(_("slice capacity must be integer"));
e440a328 10692
10693 Array_type* array_type = this->array_->type()->array_type();
f9c68f17 10694 if (array_type == NULL)
10695 {
c484d925 10696 go_assert(this->array_->type()->is_error());
f9c68f17 10697 return;
10698 }
e440a328 10699
10700 unsigned int int_bits =
10701 Type::lookup_integer_type("int")->integer_type()->bits();
10702
0c77715b 10703 Numeric_constant lvalnc;
e440a328 10704 mpz_t lval;
e440a328 10705 bool lval_valid = (array_type->length() != NULL
0c77715b 10706 && array_type->length()->numeric_constant_value(&lvalnc)
10707 && lvalnc.to_int(&lval));
10708 Numeric_constant inc;
e440a328 10709 mpz_t ival;
0bd5d859 10710 bool ival_valid = false;
0c77715b 10711 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 10712 {
0bd5d859 10713 ival_valid = true;
e440a328 10714 if (mpz_sgn(ival) < 0
10715 || mpz_sizeinbase(ival, 2) >= int_bits
10716 || (lval_valid
10717 && (this->end_ == NULL
10718 ? mpz_cmp(ival, lval) >= 0
10719 : mpz_cmp(ival, lval) > 0)))
10720 {
631d5788 10721 go_error_at(this->start_->location(), "array index out of bounds");
e440a328 10722 this->set_is_error();
10723 }
10724 }
10725 if (this->end_ != NULL && !this->end_->is_nil_expression())
10726 {
0c77715b 10727 Numeric_constant enc;
10728 mpz_t eval;
acf2b673 10729 bool eval_valid = false;
0c77715b 10730 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 10731 {
acf2b673 10732 eval_valid = true;
0c77715b 10733 if (mpz_sgn(eval) < 0
10734 || mpz_sizeinbase(eval, 2) >= int_bits
10735 || (lval_valid && mpz_cmp(eval, lval) > 0))
e440a328 10736 {
631d5788 10737 go_error_at(this->end_->location(), "array index out of bounds");
e440a328 10738 this->set_is_error();
10739 }
0bd5d859 10740 else if (ival_valid && mpz_cmp(ival, eval) > 0)
10741 this->report_error(_("inverted slice range"));
e440a328 10742 }
acf2b673 10743
10744 Numeric_constant cnc;
10745 mpz_t cval;
10746 if (this->cap_ != NULL
10747 && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval))
10748 {
10749 if (mpz_sgn(cval) < 0
10750 || mpz_sizeinbase(cval, 2) >= int_bits
10751 || (lval_valid && mpz_cmp(cval, lval) > 0))
10752 {
631d5788 10753 go_error_at(this->cap_->location(), "array index out of bounds");
acf2b673 10754 this->set_is_error();
10755 }
10756 else if (ival_valid && mpz_cmp(ival, cval) > 0)
10757 {
631d5788 10758 go_error_at(this->cap_->location(),
10759 "invalid slice index: capacity less than start");
acf2b673 10760 this->set_is_error();
10761 }
10762 else if (eval_valid && mpz_cmp(eval, cval) > 0)
10763 {
631d5788 10764 go_error_at(this->cap_->location(),
10765 "invalid slice index: capacity less than length");
acf2b673 10766 this->set_is_error();
10767 }
10768 mpz_clear(cval);
10769 }
10770
10771 if (eval_valid)
10772 mpz_clear(eval);
e440a328 10773 }
0bd5d859 10774 if (ival_valid)
10775 mpz_clear(ival);
0c77715b 10776 if (lval_valid)
10777 mpz_clear(lval);
e440a328 10778
10779 // A slice of an array requires an addressable array. A slice of a
10780 // slice is always possible.
411eb89e 10781 if (this->end_ != NULL && !array_type->is_slice_type())
88ec30c8 10782 {
10783 if (!this->array_->is_addressable())
8da39c3b 10784 this->report_error(_("slice of unaddressable value"));
88ec30c8 10785 else
d0a50ed8 10786 {
10787 bool escapes = true;
10788
10789 // When compiling the runtime, a slice operation does not
10790 // cause local variables to escape. When escape analysis
10791 // becomes the default, this should be changed to make it an
10792 // error if we have a slice operation that escapes.
10793 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
10794 escapes = false;
10795
10796 this->array_->address_taken(escapes);
10797 }
88ec30c8 10798 }
e440a328 10799}
10800
2c809f8f 10801// Flatten array indexing by using temporary variables for slices and indexes.
35a54f17 10802
10803Expression*
10804Array_index_expression::do_flatten(Gogo*, Named_object*,
10805 Statement_inserter* inserter)
10806{
10807 Location loc = this->location();
5bf8be8b 10808 Expression* array = this->array_;
10809 Expression* start = this->start_;
10810 Expression* end = this->end_;
10811 Expression* cap = this->cap_;
10812 if (array->is_error_expression()
10813 || array->type()->is_error_type()
10814 || start->is_error_expression()
10815 || start->type()->is_error_type()
10816 || (end != NULL
10817 && (end->is_error_expression() || end->type()->is_error_type()))
10818 || (cap != NULL
10819 && (cap->is_error_expression() || cap->type()->is_error_type())))
10820 {
10821 go_assert(saw_errors());
10822 return Expression::make_error(loc);
10823 }
10824
2c809f8f 10825 Temporary_statement* temp;
5bf8be8b 10826 if (array->type()->is_slice_type() && !array->is_variable())
35a54f17 10827 {
5bf8be8b 10828 temp = Statement::make_temporary(NULL, array, loc);
35a54f17 10829 inserter->insert(temp);
10830 this->array_ = Expression::make_temporary_reference(temp, loc);
10831 }
5bf8be8b 10832 if (!start->is_variable())
2c809f8f 10833 {
5bf8be8b 10834 temp = Statement::make_temporary(NULL, start, loc);
2c809f8f 10835 inserter->insert(temp);
10836 this->start_ = Expression::make_temporary_reference(temp, loc);
10837 }
5bf8be8b 10838 if (end != NULL
10839 && !end->is_nil_expression()
10840 && !end->is_variable())
2c809f8f 10841 {
5bf8be8b 10842 temp = Statement::make_temporary(NULL, end, loc);
2c809f8f 10843 inserter->insert(temp);
10844 this->end_ = Expression::make_temporary_reference(temp, loc);
10845 }
03118c21 10846 if (cap != NULL && !cap->is_variable())
2c809f8f 10847 {
5bf8be8b 10848 temp = Statement::make_temporary(NULL, cap, loc);
2c809f8f 10849 inserter->insert(temp);
10850 this->cap_ = Expression::make_temporary_reference(temp, loc);
10851 }
10852
35a54f17 10853 return this;
10854}
10855
e440a328 10856// Return whether this expression is addressable.
10857
10858bool
10859Array_index_expression::do_is_addressable() const
10860{
10861 // A slice expression is not addressable.
10862 if (this->end_ != NULL)
10863 return false;
10864
10865 // An index into a slice is addressable.
411eb89e 10866 if (this->array_->type()->is_slice_type())
e440a328 10867 return true;
10868
10869 // An index into an array is addressable if the array is
10870 // addressable.
10871 return this->array_->is_addressable();
10872}
10873
bf1323be 10874void
10875Array_index_expression::do_address_taken(bool escapes)
10876{
10877 // In &x[0], if x is a slice, then x's address is not taken.
10878 if (!this->array_->type()->is_slice_type())
10879 this->array_->address_taken(escapes);
10880}
10881
ea664253 10882// Get the backend representation for an array index.
e440a328 10883
ea664253 10884Bexpression*
10885Array_index_expression::do_get_backend(Translate_context* context)
e440a328 10886{
e440a328 10887 Array_type* array_type = this->array_->type()->array_type();
d8cd8e2d 10888 if (array_type == NULL)
10889 {
c484d925 10890 go_assert(this->array_->type()->is_error());
ea664253 10891 return context->backend()->error_expression();
d8cd8e2d 10892 }
35a54f17 10893 go_assert(!array_type->is_slice_type() || this->array_->is_variable());
e440a328 10894
2c809f8f 10895 Location loc = this->location();
10896 Gogo* gogo = context->gogo();
10897
6dfedc16 10898 Type* int_type = Type::lookup_integer_type("int");
10899 Btype* int_btype = int_type->get_backend(gogo);
e440a328 10900
2c809f8f 10901 // We need to convert the length and capacity to the Go "int" type here
10902 // because the length of a fixed-length array could be of type "uintptr"
10903 // and gimple disallows binary operations between "uintptr" and other
10904 // integer types. FIXME.
10905 Bexpression* length = NULL;
a04bfdfc 10906 if (this->end_ == NULL || this->end_->is_nil_expression())
10907 {
35a54f17 10908 Expression* len = array_type->get_length(gogo, this->array_);
ea664253 10909 length = len->get_backend(context);
2c809f8f 10910 length = gogo->backend()->convert_expression(int_btype, length, loc);
a04bfdfc 10911 }
10912
2c809f8f 10913 Bexpression* capacity = NULL;
a04bfdfc 10914 if (this->end_ != NULL)
10915 {
35a54f17 10916 Expression* cap = array_type->get_capacity(gogo, this->array_);
ea664253 10917 capacity = cap->get_backend(context);
2c809f8f 10918 capacity = gogo->backend()->convert_expression(int_btype, capacity, loc);
a04bfdfc 10919 }
10920
2c809f8f 10921 Bexpression* cap_arg = capacity;
acf2b673 10922 if (this->cap_ != NULL)
10923 {
ea664253 10924 cap_arg = this->cap_->get_backend(context);
2c809f8f 10925 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
acf2b673 10926 }
10927
2c809f8f 10928 if (length == NULL)
10929 length = cap_arg;
e440a328 10930
10931 int code = (array_type->length() != NULL
10932 ? (this->end_ == NULL
10933 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10934 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10935 : (this->end_ == NULL
10936 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10937 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
ea664253 10938 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context);
2c809f8f 10939
6dfedc16 10940 if (this->start_->type()->integer_type() == NULL
10941 && !Type::are_convertible(int_type, this->start_->type(), NULL))
10942 {
10943 go_assert(saw_errors());
10944 return context->backend()->error_expression();
10945 }
d9f3743a 10946
ea664253 10947 Bexpression* bad_index =
d9f3743a 10948 Expression::check_bounds(this->start_, loc)->get_backend(context);
2c809f8f 10949
ea664253 10950 Bexpression* start = this->start_->get_backend(context);
2c809f8f 10951 start = gogo->backend()->convert_expression(int_btype, start, loc);
10952 Bexpression* start_too_large =
10953 gogo->backend()->binary_expression((this->end_ == NULL
10954 ? OPERATOR_GE
10955 : OPERATOR_GT),
10956 start,
10957 (this->end_ == NULL
10958 ? length
10959 : capacity),
10960 loc);
10961 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large,
10962 bad_index, loc);
e440a328 10963
93715b75 10964 Bfunction* bfn = context->function()->func_value()->get_decl();
e440a328 10965 if (this->end_ == NULL)
10966 {
10967 // Simple array indexing. This has to return an l-value, so
2c809f8f 10968 // wrap the index check into START.
10969 start =
93715b75 10970 gogo->backend()->conditional_expression(bfn, int_btype, bad_index,
2c809f8f 10971 crash, start, loc);
e440a328 10972
2c809f8f 10973 Bexpression* ret;
e440a328 10974 if (array_type->length() != NULL)
10975 {
ea664253 10976 Bexpression* array = this->array_->get_backend(context);
2c809f8f 10977 ret = gogo->backend()->array_index_expression(array, start, loc);
e440a328 10978 }
10979 else
10980 {
2c809f8f 10981 // Slice.
10982 Expression* valptr =
44dbe1d7 10983 array_type->get_value_pointer(gogo, this->array_,
10984 this->is_lvalue_);
ea664253 10985 Bexpression* ptr = valptr->get_backend(context);
2c809f8f 10986 ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc);
9b27b43c 10987
10988 Type* ele_type = this->array_->type()->array_type()->element_type();
10989 Btype* ele_btype = ele_type->get_backend(gogo);
10990 ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc);
e440a328 10991 }
ea664253 10992 return ret;
e440a328 10993 }
10994
10995 // Array slice.
10996
acf2b673 10997 if (this->cap_ != NULL)
10998 {
2c809f8f 10999 Bexpression* bounds_bcheck =
ea664253 11000 Expression::check_bounds(this->cap_, loc)->get_backend(context);
2c809f8f 11001 bad_index =
11002 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck,
11003 bad_index, loc);
11004 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
11005
11006 Bexpression* cap_too_small =
11007 gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc);
11008 Bexpression* cap_too_large =
11009 gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc);
11010 Bexpression* bad_cap =
11011 gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small,
11012 cap_too_large, loc);
11013 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap,
11014 bad_index, loc);
11015 }
11016
11017 Bexpression* end;
e440a328 11018 if (this->end_->is_nil_expression())
2c809f8f 11019 end = length;
e440a328 11020 else
11021 {
2c809f8f 11022 Bexpression* bounds_bcheck =
ea664253 11023 Expression::check_bounds(this->end_, loc)->get_backend(context);
e440a328 11024
2c809f8f 11025 bad_index =
11026 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck,
11027 bad_index, loc);
e440a328 11028
ea664253 11029 end = this->end_->get_backend(context);
2c809f8f 11030 end = gogo->backend()->convert_expression(int_btype, end, loc);
11031 Bexpression* end_too_small =
11032 gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc);
11033 Bexpression* end_too_large =
11034 gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc);
11035 Bexpression* bad_end =
11036 gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small,
11037 end_too_large, loc);
11038 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end,
11039 bad_index, loc);
e440a328 11040 }
11041
2c809f8f 11042 Bexpression* result_length =
11043 gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc);
e440a328 11044
2c809f8f 11045 Bexpression* result_capacity =
11046 gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc);
e440a328 11047
03118c21 11048 // If the new capacity is zero, don't change val. Otherwise we can
11049 // get a pointer to the next object in memory, keeping it live
11050 // unnecessarily. When the capacity is zero, the actual pointer
11051 // value doesn't matter.
11052 Bexpression* zero =
11053 Expression::make_integer_ul(0, int_type, loc)->get_backend(context);
11054 Bexpression* cond =
11055 gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero,
11056 loc);
11057 Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype,
11058 cond, zero,
11059 start, loc);
44dbe1d7 11060 Expression* valptr = array_type->get_value_pointer(gogo, this->array_,
11061 this->is_lvalue_);
03118c21 11062 Bexpression* val = valptr->get_backend(context);
11063 val = gogo->backend()->pointer_offset_expression(val, offset, loc);
11064
2c809f8f 11065 Btype* struct_btype = this->type()->get_backend(gogo);
11066 std::vector<Bexpression*> init;
11067 init.push_back(val);
11068 init.push_back(result_length);
11069 init.push_back(result_capacity);
e440a328 11070
2c809f8f 11071 Bexpression* ctor =
11072 gogo->backend()->constructor_expression(struct_btype, init, loc);
93715b75 11073 return gogo->backend()->conditional_expression(bfn, struct_btype, bad_index,
ea664253 11074 crash, ctor, loc);
e440a328 11075}
11076
d751bb78 11077// Dump ast representation for an array index expression.
11078
11079void
11080Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11081 const
11082{
11083 Index_expression::dump_index_expression(ast_dump_context, this->array_,
acf2b673 11084 this->start_, this->end_, this->cap_);
d751bb78 11085}
11086
acf2b673 11087// Make an array index expression. END and CAP may be NULL.
e440a328 11088
11089Expression*
11090Expression::make_array_index(Expression* array, Expression* start,
acf2b673 11091 Expression* end, Expression* cap,
11092 Location location)
e440a328 11093{
acf2b673 11094 return new Array_index_expression(array, start, end, cap, location);
e440a328 11095}
11096
50075d74 11097// Class String_index_expression.
e440a328 11098
11099// String index traversal.
11100
11101int
11102String_index_expression::do_traverse(Traverse* traverse)
11103{
11104 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
11105 return TRAVERSE_EXIT;
11106 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
11107 return TRAVERSE_EXIT;
11108 if (this->end_ != NULL)
11109 {
11110 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
11111 return TRAVERSE_EXIT;
11112 }
11113 return TRAVERSE_CONTINUE;
11114}
11115
2c809f8f 11116Expression*
11117String_index_expression::do_flatten(Gogo*, Named_object*,
11118 Statement_inserter* inserter)
e440a328 11119{
2c809f8f 11120 Location loc = this->location();
5bf8be8b 11121 Expression* string = this->string_;
11122 Expression* start = this->start_;
11123 Expression* end = this->end_;
11124 if (string->is_error_expression()
11125 || string->type()->is_error_type()
11126 || start->is_error_expression()
11127 || start->type()->is_error_type()
11128 || (end != NULL
11129 && (end->is_error_expression() || end->type()->is_error_type())))
11130 {
11131 go_assert(saw_errors());
11132 return Expression::make_error(loc);
11133 }
11134
11135 Temporary_statement* temp;
2c809f8f 11136 if (!this->string_->is_variable())
11137 {
11138 temp = Statement::make_temporary(NULL, this->string_, loc);
11139 inserter->insert(temp);
11140 this->string_ = Expression::make_temporary_reference(temp, loc);
11141 }
11142 if (!this->start_->is_variable())
11143 {
11144 temp = Statement::make_temporary(NULL, this->start_, loc);
11145 inserter->insert(temp);
11146 this->start_ = Expression::make_temporary_reference(temp, loc);
11147 }
11148 if (this->end_ != NULL
11149 && !this->end_->is_nil_expression()
11150 && !this->end_->is_variable())
11151 {
11152 temp = Statement::make_temporary(NULL, this->end_, loc);
11153 inserter->insert(temp);
11154 this->end_ = Expression::make_temporary_reference(temp, loc);
11155 }
11156
11157 return this;
11158}
11159
11160// Return the type of a string index.
11161
11162Type*
11163String_index_expression::do_type()
11164{
11165 if (this->end_ == NULL)
11166 return Type::lookup_integer_type("uint8");
11167 else
11168 return this->string_->type();
11169}
11170
11171// Determine the type of a string index.
11172
11173void
11174String_index_expression::do_determine_type(const Type_context*)
11175{
11176 this->string_->determine_type_no_context();
f77aa642 11177
11178 Type_context index_context(Type::lookup_integer_type("int"), false);
11179 if (this->start_->is_constant())
11180 this->start_->determine_type(&index_context);
11181 else
11182 this->start_->determine_type_no_context();
e440a328 11183 if (this->end_ != NULL)
f77aa642 11184 {
11185 if (this->end_->is_constant())
11186 this->end_->determine_type(&index_context);
11187 else
11188 this->end_->determine_type_no_context();
11189 }
e440a328 11190}
11191
11192// Check types of a string index.
11193
11194void
11195String_index_expression::do_check_types(Gogo*)
11196{
acdc230d 11197 Numeric_constant nc;
11198 unsigned long v;
11199 if (this->start_->type()->integer_type() == NULL
11200 && !this->start_->type()->is_error()
11201 && (!this->start_->numeric_constant_value(&nc)
11202 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 11203 this->report_error(_("index must be integer"));
11204 if (this->end_ != NULL
11205 && this->end_->type()->integer_type() == NULL
acdc230d 11206 && !this->end_->type()->is_error()
11207 && !this->end_->is_nil_expression()
11208 && !this->end_->is_error_expression()
11209 && (!this->end_->numeric_constant_value(&nc)
11210 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 11211 this->report_error(_("slice end must be integer"));
11212
11213 std::string sval;
11214 bool sval_valid = this->string_->string_constant_value(&sval);
11215
0c77715b 11216 Numeric_constant inc;
e440a328 11217 mpz_t ival;
0bd5d859 11218 bool ival_valid = false;
0c77715b 11219 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 11220 {
0bd5d859 11221 ival_valid = true;
e440a328 11222 if (mpz_sgn(ival) < 0
b10f32fb 11223 || (sval_valid
11224 && (this->end_ == NULL
11225 ? mpz_cmp_ui(ival, sval.length()) >= 0
11226 : mpz_cmp_ui(ival, sval.length()) > 0)))
e440a328 11227 {
631d5788 11228 go_error_at(this->start_->location(), "string index out of bounds");
e440a328 11229 this->set_is_error();
11230 }
11231 }
11232 if (this->end_ != NULL && !this->end_->is_nil_expression())
11233 {
0c77715b 11234 Numeric_constant enc;
11235 mpz_t eval;
11236 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 11237 {
0c77715b 11238 if (mpz_sgn(eval) < 0
11239 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
e440a328 11240 {
631d5788 11241 go_error_at(this->end_->location(), "string index out of bounds");
e440a328 11242 this->set_is_error();
11243 }
0bd5d859 11244 else if (ival_valid && mpz_cmp(ival, eval) > 0)
11245 this->report_error(_("inverted slice range"));
0c77715b 11246 mpz_clear(eval);
e440a328 11247 }
11248 }
0bd5d859 11249 if (ival_valid)
11250 mpz_clear(ival);
e440a328 11251}
11252
ea664253 11253// Get the backend representation for a string index.
e440a328 11254
ea664253 11255Bexpression*
11256String_index_expression::do_get_backend(Translate_context* context)
e440a328 11257{
b13c66cd 11258 Location loc = this->location();
2c809f8f 11259 Expression* string_arg = this->string_;
11260 if (this->string_->type()->points_to() != NULL)
f614ea8b 11261 string_arg = Expression::make_dereference(this->string_,
11262 NIL_CHECK_NOT_NEEDED, loc);
e440a328 11263
2c809f8f 11264 Expression* bad_index = Expression::check_bounds(this->start_, loc);
e440a328 11265
2c809f8f 11266 int code = (this->end_ == NULL
11267 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
11268 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
e440a328 11269
2c809f8f 11270 Gogo* gogo = context->gogo();
ea664253 11271 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context);
1b1f2abf 11272
11273 Type* int_type = Type::lookup_integer_type("int");
e440a328 11274
2c809f8f 11275 // It is possible that an error occurred earlier because the start index
11276 // cannot be represented as an integer type. In this case, we shouldn't
11277 // try casting the starting index into an integer since
11278 // Type_conversion_expression will fail to get the backend representation.
11279 // FIXME.
11280 if (this->start_->type()->integer_type() == NULL
11281 && !Type::are_convertible(int_type, this->start_->type(), NULL))
11282 {
11283 go_assert(saw_errors());
ea664253 11284 return context->backend()->error_expression();
2c809f8f 11285 }
e440a328 11286
2c809f8f 11287 Expression* start = Expression::make_cast(int_type, this->start_, loc);
93715b75 11288 Bfunction* bfn = context->function()->func_value()->get_decl();
e440a328 11289
2c809f8f 11290 if (this->end_ == NULL)
11291 {
11292 Expression* length =
11293 Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc);
e440a328 11294
2c809f8f 11295 Expression* start_too_large =
11296 Expression::make_binary(OPERATOR_GE, start, length, loc);
11297 bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large,
11298 bad_index, loc);
11299 Expression* bytes =
11300 Expression::make_string_info(this->string_, STRING_INFO_DATA, loc);
e440a328 11301
ea664253 11302 Bexpression* bstart = start->get_backend(context);
11303 Bexpression* ptr = bytes->get_backend(context);
2c809f8f 11304 ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc);
9b27b43c 11305 Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo);
11306 Bexpression* index =
11307 gogo->backend()->indirect_expression(ubtype, ptr, true, loc);
e440a328 11308
2c809f8f 11309 Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo);
ea664253 11310 Bexpression* index_error = bad_index->get_backend(context);
93715b75 11311 return gogo->backend()->conditional_expression(bfn, byte_btype,
11312 index_error, crash,
11313 index, loc);
2c809f8f 11314 }
11315
11316 Expression* end = NULL;
11317 if (this->end_->is_nil_expression())
e67508fa 11318 end = Expression::make_integer_sl(-1, int_type, loc);
e440a328 11319 else
11320 {
2c809f8f 11321 Expression* bounds_check = Expression::check_bounds(this->end_, loc);
11322 bad_index =
11323 Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc);
11324 end = Expression::make_cast(int_type, this->end_, loc);
e440a328 11325 }
2c809f8f 11326
11327 Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3,
11328 string_arg, start, end);
ea664253 11329 Bexpression* bstrslice = strslice->get_backend(context);
2c809f8f 11330
11331 Btype* str_btype = strslice->type()->get_backend(gogo);
ea664253 11332 Bexpression* index_error = bad_index->get_backend(context);
93715b75 11333 return gogo->backend()->conditional_expression(bfn, str_btype, index_error,
ea664253 11334 crash, bstrslice, loc);
e440a328 11335}
11336
d751bb78 11337// Dump ast representation for a string index expression.
11338
11339void
11340String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11341 const
11342{
acf2b673 11343 Index_expression::dump_index_expression(ast_dump_context, this->string_,
11344 this->start_, this->end_, NULL);
d751bb78 11345}
11346
e440a328 11347// Make a string index expression. END may be NULL.
11348
11349Expression*
11350Expression::make_string_index(Expression* string, Expression* start,
b13c66cd 11351 Expression* end, Location location)
e440a328 11352{
11353 return new String_index_expression(string, start, end, location);
11354}
11355
11356// Class Map_index.
11357
11358// Get the type of the map.
11359
11360Map_type*
11361Map_index_expression::get_map_type() const
11362{
0d5530d9 11363 Map_type* mt = this->map_->type()->map_type();
c7524fae 11364 if (mt == NULL)
c484d925 11365 go_assert(saw_errors());
e440a328 11366 return mt;
11367}
11368
11369// Map index traversal.
11370
11371int
11372Map_index_expression::do_traverse(Traverse* traverse)
11373{
11374 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
11375 return TRAVERSE_EXIT;
11376 return Expression::traverse(&this->index_, traverse);
11377}
11378
2c809f8f 11379// We need to pass in a pointer to the key, so flatten the index into a
11380// temporary variable if it isn't already. The value pointer will be
11381// dereferenced and checked for nil, so flatten into a temporary to avoid
11382// recomputation.
11383
11384Expression*
91c0fd76 11385Map_index_expression::do_flatten(Gogo* gogo, Named_object*,
2c809f8f 11386 Statement_inserter* inserter)
11387{
91c0fd76 11388 Location loc = this->location();
2c809f8f 11389 Map_type* mt = this->get_map_type();
5bf8be8b 11390 if (this->index()->is_error_expression()
11391 || this->index()->type()->is_error_type()
11392 || mt->is_error_type())
11393 {
11394 go_assert(saw_errors());
11395 return Expression::make_error(loc);
11396 }
11397
91c0fd76 11398 if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL))
11399 {
11400 if (this->index_->type()->interface_type() != NULL
11401 && !this->index_->is_variable())
11402 {
11403 Temporary_statement* temp =
11404 Statement::make_temporary(NULL, this->index_, loc);
11405 inserter->insert(temp);
11406 this->index_ = Expression::make_temporary_reference(temp, loc);
11407 }
11408 this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(),
11409 this->index_, loc);
11410 }
2c809f8f 11411
11412 if (!this->index_->is_variable())
11413 {
11414 Temporary_statement* temp = Statement::make_temporary(NULL, this->index_,
91c0fd76 11415 loc);
2c809f8f 11416 inserter->insert(temp);
91c0fd76 11417 this->index_ = Expression::make_temporary_reference(temp, loc);
2c809f8f 11418 }
11419
11420 if (this->value_pointer_ == NULL)
0d5530d9 11421 this->get_value_pointer(gogo);
5bf8be8b 11422 if (this->value_pointer_->is_error_expression()
11423 || this->value_pointer_->type()->is_error_type())
11424 return Expression::make_error(loc);
2c809f8f 11425 if (!this->value_pointer_->is_variable())
11426 {
11427 Temporary_statement* temp =
91c0fd76 11428 Statement::make_temporary(NULL, this->value_pointer_, loc);
2c809f8f 11429 inserter->insert(temp);
91c0fd76 11430 this->value_pointer_ = Expression::make_temporary_reference(temp, loc);
2c809f8f 11431 }
11432
11433 return this;
11434}
11435
e440a328 11436// Return the type of a map index.
11437
11438Type*
11439Map_index_expression::do_type()
11440{
c7524fae 11441 Map_type* mt = this->get_map_type();
11442 if (mt == NULL)
11443 return Type::make_error_type();
0d5530d9 11444 return mt->val_type();
e440a328 11445}
11446
11447// Fix the type of a map index.
11448
11449void
11450Map_index_expression::do_determine_type(const Type_context*)
11451{
11452 this->map_->determine_type_no_context();
c7524fae 11453 Map_type* mt = this->get_map_type();
11454 Type* key_type = mt == NULL ? NULL : mt->key_type();
11455 Type_context subcontext(key_type, false);
e440a328 11456 this->index_->determine_type(&subcontext);
11457}
11458
11459// Check types of a map index.
11460
11461void
11462Map_index_expression::do_check_types(Gogo*)
11463{
11464 std::string reason;
c7524fae 11465 Map_type* mt = this->get_map_type();
11466 if (mt == NULL)
11467 return;
11468 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
e440a328 11469 {
11470 if (reason.empty())
11471 this->report_error(_("incompatible type for map index"));
11472 else
11473 {
631d5788 11474 go_error_at(this->location(), "incompatible type for map index (%s)",
11475 reason.c_str());
e440a328 11476 this->set_is_error();
11477 }
11478 }
11479}
11480
ea664253 11481// Get the backend representation for a map index.
e440a328 11482
ea664253 11483Bexpression*
11484Map_index_expression::do_get_backend(Translate_context* context)
e440a328 11485{
11486 Map_type* type = this->get_map_type();
c7524fae 11487 if (type == NULL)
2c809f8f 11488 {
11489 go_assert(saw_errors());
ea664253 11490 return context->backend()->error_expression();
2c809f8f 11491 }
e440a328 11492
2c809f8f 11493 go_assert(this->value_pointer_ != NULL
11494 && this->value_pointer_->is_variable());
e440a328 11495
f614ea8b 11496 Expression* val = Expression::make_dereference(this->value_pointer_,
11497 NIL_CHECK_NOT_NEEDED,
11498 this->location());
0d5530d9 11499 return val->get_backend(context);
e440a328 11500}
11501
0d5530d9 11502// Get an expression for the map index. This returns an expression
11503// that evaluates to a pointer to a value. If the key is not in the
11504// map, the pointer will point to a zero value.
e440a328 11505
2c809f8f 11506Expression*
0d5530d9 11507Map_index_expression::get_value_pointer(Gogo* gogo)
e440a328 11508{
2c809f8f 11509 if (this->value_pointer_ == NULL)
746d2e73 11510 {
2c809f8f 11511 Map_type* type = this->get_map_type();
11512 if (type == NULL)
746d2e73 11513 {
2c809f8f 11514 go_assert(saw_errors());
11515 return Expression::make_error(this->location());
746d2e73 11516 }
e440a328 11517
2c809f8f 11518 Location loc = this->location();
11519 Expression* map_ref = this->map_;
e440a328 11520
0d5530d9 11521 Expression* index_ptr = Expression::make_unary(OPERATOR_AND,
11522 this->index_,
2c809f8f 11523 loc);
0d5530d9 11524
11525 Expression* zero = type->fat_zero_value(gogo);
11526
11527 Expression* map_index;
11528
11529 if (zero == NULL)
11530 map_index =
11531 Runtime::make_call(Runtime::MAPACCESS1, loc, 3,
11532 Expression::make_type_descriptor(type, loc),
11533 map_ref, index_ptr);
11534 else
11535 map_index =
11536 Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4,
11537 Expression::make_type_descriptor(type, loc),
11538 map_ref, index_ptr, zero);
2c809f8f 11539
11540 Type* val_type = type->val_type();
11541 this->value_pointer_ =
11542 Expression::make_unsafe_cast(Type::make_pointer_type(val_type),
11543 map_index, this->location());
11544 }
0d5530d9 11545
2c809f8f 11546 return this->value_pointer_;
e440a328 11547}
11548
d751bb78 11549// Dump ast representation for a map index expression
11550
11551void
11552Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11553 const
11554{
acf2b673 11555 Index_expression::dump_index_expression(ast_dump_context, this->map_,
11556 this->index_, NULL, NULL);
d751bb78 11557}
11558
e440a328 11559// Make a map index expression.
11560
11561Map_index_expression*
11562Expression::make_map_index(Expression* map, Expression* index,
b13c66cd 11563 Location location)
e440a328 11564{
11565 return new Map_index_expression(map, index, location);
11566}
11567
11568// Class Field_reference_expression.
11569
149eabc5 11570// Lower a field reference expression. There is nothing to lower, but
11571// this is where we generate the tracking information for fields with
11572// the magic go:"track" tag.
11573
11574Expression*
11575Field_reference_expression::do_lower(Gogo* gogo, Named_object* function,
11576 Statement_inserter* inserter, int)
11577{
11578 Struct_type* struct_type = this->expr_->type()->struct_type();
11579 if (struct_type == NULL)
11580 {
11581 // Error will be reported elsewhere.
11582 return this;
11583 }
11584 const Struct_field* field = struct_type->field(this->field_index_);
11585 if (field == NULL)
11586 return this;
11587 if (!field->has_tag())
11588 return this;
11589 if (field->tag().find("go:\"track\"") == std::string::npos)
11590 return this;
11591
604e278d 11592 // References from functions generated by the compiler don't count.
c6292d1d 11593 if (function != NULL && function->func_value()->is_type_specific_function())
604e278d 11594 return this;
11595
149eabc5 11596 // We have found a reference to a tracked field. Build a call to
11597 // the runtime function __go_fieldtrack with a string that describes
11598 // the field. FIXME: We should only call this once per referenced
11599 // field per function, not once for each reference to the field.
11600
11601 if (this->called_fieldtrack_)
11602 return this;
11603 this->called_fieldtrack_ = true;
11604
11605 Location loc = this->location();
11606
11607 std::string s = "fieldtrack \"";
11608 Named_type* nt = this->expr_->type()->named_type();
11609 if (nt == NULL || nt->named_object()->package() == NULL)
11610 s.append(gogo->pkgpath());
11611 else
11612 s.append(nt->named_object()->package()->pkgpath());
11613 s.push_back('.');
11614 if (nt != NULL)
5c29ad36 11615 s.append(Gogo::unpack_hidden_name(nt->name()));
149eabc5 11616 s.push_back('.');
11617 s.append(field->field_name());
11618 s.push_back('"');
11619
11620 // We can't use a string here, because internally a string holds a
11621 // pointer to the actual bytes; when the linker garbage collects the
11622 // string, it won't garbage collect the bytes. So we use a
11623 // [...]byte.
11624
e67508fa 11625 Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc);
149eabc5 11626
11627 Type* byte_type = gogo->lookup_global("byte")->type_value();
6bf4793c 11628 Array_type* array_type = Type::make_array_type(byte_type, length_expr);
11629 array_type->set_is_array_incomparable();
149eabc5 11630
11631 Expression_list* bytes = new Expression_list();
11632 for (std::string::const_iterator p = s.begin(); p != s.end(); p++)
11633 {
e67508fa 11634 unsigned char c = static_cast<unsigned char>(*p);
11635 bytes->push_back(Expression::make_integer_ul(c, NULL, loc));
149eabc5 11636 }
11637
11638 Expression* e = Expression::make_composite_literal(array_type, 0, false,
62750cd5 11639 bytes, false, loc);
149eabc5 11640
11641 Variable* var = new Variable(array_type, e, true, false, false, loc);
11642
11643 static int count;
11644 char buf[50];
11645 snprintf(buf, sizeof buf, "fieldtrack.%d", count);
11646 ++count;
11647
11648 Named_object* no = gogo->add_variable(buf, var);
11649 e = Expression::make_var_reference(no, loc);
11650 e = Expression::make_unary(OPERATOR_AND, e, loc);
11651
11652 Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e);
604e278d 11653 gogo->lower_expression(function, inserter, &call);
149eabc5 11654 inserter->insert(Statement::make_statement(call, false));
11655
11656 // Put this function, and the global variable we just created, into
11657 // unique sections. This will permit the linker to garbage collect
11658 // them if they are not referenced. The effect is that the only
11659 // strings, indicating field references, that will wind up in the
11660 // executable will be those for functions that are actually needed.
66a6be58 11661 if (function != NULL)
11662 function->func_value()->set_in_unique_section();
149eabc5 11663 var->set_in_unique_section();
11664
11665 return this;
11666}
11667
e440a328 11668// Return the type of a field reference.
11669
11670Type*
11671Field_reference_expression::do_type()
11672{
b0e628fb 11673 Type* type = this->expr_->type();
5c13bd80 11674 if (type->is_error())
b0e628fb 11675 return type;
11676 Struct_type* struct_type = type->struct_type();
c484d925 11677 go_assert(struct_type != NULL);
e440a328 11678 return struct_type->field(this->field_index_)->type();
11679}
11680
11681// Check the types for a field reference.
11682
11683void
11684Field_reference_expression::do_check_types(Gogo*)
11685{
b0e628fb 11686 Type* type = this->expr_->type();
5c13bd80 11687 if (type->is_error())
b0e628fb 11688 return;
11689 Struct_type* struct_type = type->struct_type();
c484d925 11690 go_assert(struct_type != NULL);
11691 go_assert(struct_type->field(this->field_index_) != NULL);
e440a328 11692}
11693
ea664253 11694// Get the backend representation for a field reference.
e440a328 11695
ea664253 11696Bexpression*
11697Field_reference_expression::do_get_backend(Translate_context* context)
e440a328 11698{
ea664253 11699 Bexpression* bstruct = this->expr_->get_backend(context);
11700 return context->gogo()->backend()->struct_field_expression(bstruct,
11701 this->field_index_,
11702 this->location());
e440a328 11703}
11704
d751bb78 11705// Dump ast representation for a field reference expression.
11706
11707void
11708Field_reference_expression::do_dump_expression(
11709 Ast_dump_context* ast_dump_context) const
11710{
11711 this->expr_->dump_expression(ast_dump_context);
11712 ast_dump_context->ostream() << "." << this->field_index_;
11713}
11714
e440a328 11715// Make a reference to a qualified identifier in an expression.
11716
11717Field_reference_expression*
11718Expression::make_field_reference(Expression* expr, unsigned int field_index,
b13c66cd 11719 Location location)
e440a328 11720{
11721 return new Field_reference_expression(expr, field_index, location);
11722}
11723
11724// Class Interface_field_reference_expression.
11725
2387f644 11726// Return an expression for the pointer to the function to call.
e440a328 11727
2387f644 11728Expression*
11729Interface_field_reference_expression::get_function()
e440a328 11730{
2387f644 11731 Expression* ref = this->expr_;
11732 Location loc = this->location();
11733 if (ref->type()->points_to() != NULL)
f614ea8b 11734 ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc);
e440a328 11735
2387f644 11736 Expression* mtable =
11737 Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc);
11738 Struct_type* mtable_type = mtable->type()->points_to()->struct_type();
e440a328 11739
11740 std::string name = Gogo::unpack_hidden_name(this->name_);
2387f644 11741 unsigned int index;
11742 const Struct_field* field = mtable_type->find_local_field(name, &index);
11743 go_assert(field != NULL);
f614ea8b 11744
11745 mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc);
2387f644 11746 return Expression::make_field_reference(mtable, index, loc);
e440a328 11747}
11748
2387f644 11749// Return an expression for the first argument to pass to the interface
e440a328 11750// function.
11751
2387f644 11752Expression*
11753Interface_field_reference_expression::get_underlying_object()
e440a328 11754{
2387f644 11755 Expression* expr = this->expr_;
11756 if (expr->type()->points_to() != NULL)
f614ea8b 11757 expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT,
11758 this->location());
2387f644 11759 return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT,
11760 this->location());
e440a328 11761}
11762
11763// Traversal.
11764
11765int
11766Interface_field_reference_expression::do_traverse(Traverse* traverse)
11767{
11768 return Expression::traverse(&this->expr_, traverse);
11769}
11770
0afbb937 11771// Lower the expression. If this expression is not called, we need to
11772// evaluate the expression twice when converting to the backend
11773// interface. So introduce a temporary variable if necessary.
11774
11775Expression*
9782d556 11776Interface_field_reference_expression::do_flatten(Gogo*, Named_object*,
11777 Statement_inserter* inserter)
0afbb937 11778{
5bf8be8b 11779 if (this->expr_->is_error_expression()
11780 || this->expr_->type()->is_error_type())
11781 {
11782 go_assert(saw_errors());
11783 return Expression::make_error(this->location());
11784 }
11785
2387f644 11786 if (!this->expr_->is_variable())
0afbb937 11787 {
11788 Temporary_statement* temp =
11789 Statement::make_temporary(this->expr_->type(), NULL, this->location());
11790 inserter->insert(temp);
11791 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_,
11792 this->location());
11793 }
11794 return this;
11795}
11796
e440a328 11797// Return the type of an interface field reference.
11798
11799Type*
11800Interface_field_reference_expression::do_type()
11801{
11802 Type* expr_type = this->expr_->type();
11803
11804 Type* points_to = expr_type->points_to();
11805 if (points_to != NULL)
11806 expr_type = points_to;
11807
11808 Interface_type* interface_type = expr_type->interface_type();
11809 if (interface_type == NULL)
11810 return Type::make_error_type();
11811
11812 const Typed_identifier* method = interface_type->find_method(this->name_);
11813 if (method == NULL)
11814 return Type::make_error_type();
11815
11816 return method->type();
11817}
11818
11819// Determine types.
11820
11821void
11822Interface_field_reference_expression::do_determine_type(const Type_context*)
11823{
11824 this->expr_->determine_type_no_context();
11825}
11826
11827// Check the types for an interface field reference.
11828
11829void
11830Interface_field_reference_expression::do_check_types(Gogo*)
11831{
11832 Type* type = this->expr_->type();
11833
11834 Type* points_to = type->points_to();
11835 if (points_to != NULL)
11836 type = points_to;
11837
11838 Interface_type* interface_type = type->interface_type();
11839 if (interface_type == NULL)
5c491127 11840 {
11841 if (!type->is_error_type())
11842 this->report_error(_("expected interface or pointer to interface"));
11843 }
e440a328 11844 else
11845 {
11846 const Typed_identifier* method =
11847 interface_type->find_method(this->name_);
11848 if (method == NULL)
11849 {
631d5788 11850 go_error_at(this->location(), "method %qs not in interface",
11851 Gogo::message_name(this->name_).c_str());
e440a328 11852 this->set_is_error();
11853 }
11854 }
11855}
11856
0afbb937 11857// If an interface field reference is not simply called, then it is
11858// represented as a closure. The closure will hold a single variable,
11859// the value of the interface on which the method should be called.
11860// The function will be a simple thunk that pulls the value from the
11861// closure and calls the method with the remaining arguments.
11862
11863// Because method values are not common, we don't build all thunks for
11864// all possible interface methods, but instead only build them as we
11865// need them. In particular, we even build them on demand for
11866// interface methods defined in other packages.
11867
11868Interface_field_reference_expression::Interface_method_thunks
11869 Interface_field_reference_expression::interface_method_thunks;
11870
11871// Find or create the thunk to call method NAME on TYPE.
11872
11873Named_object*
11874Interface_field_reference_expression::create_thunk(Gogo* gogo,
11875 Interface_type* type,
11876 const std::string& name)
11877{
11878 std::pair<Interface_type*, Method_thunks*> val(type, NULL);
11879 std::pair<Interface_method_thunks::iterator, bool> ins =
11880 Interface_field_reference_expression::interface_method_thunks.insert(val);
11881 if (ins.second)
11882 {
11883 // This is the first time we have seen this interface.
11884 ins.first->second = new Method_thunks();
11885 }
11886
11887 for (Method_thunks::const_iterator p = ins.first->second->begin();
11888 p != ins.first->second->end();
11889 p++)
11890 if (p->first == name)
11891 return p->second;
11892
11893 Location loc = type->location();
11894
11895 const Typed_identifier* method_id = type->find_method(name);
11896 if (method_id == NULL)
11897 return Named_object::make_erroneous_name(Gogo::thunk_name());
11898
11899 Function_type* orig_fntype = method_id->type()->function_type();
11900 if (orig_fntype == NULL)
11901 return Named_object::make_erroneous_name(Gogo::thunk_name());
11902
11903 Struct_field_list* sfl = new Struct_field_list();
f8bdf81a 11904 // The type here is wrong--it should be the C function type. But it
11905 // doesn't really matter.
0afbb937 11906 Type* vt = Type::make_pointer_type(Type::make_void_type());
11907 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
11908 sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc)));
6bf4793c 11909 Struct_type* st = Type::make_struct_type(sfl, loc);
11910 st->set_is_struct_incomparable();
11911 Type* closure_type = Type::make_pointer_type(st);
0afbb937 11912
f8bdf81a 11913 Function_type* new_fntype = orig_fntype->copy_with_names();
0afbb937 11914
da244e59 11915 std::string thunk_name = Gogo::thunk_name();
11916 Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
0afbb937 11917 false, loc);
11918
f8bdf81a 11919 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
11920 cvar->set_is_used();
1ecc6157 11921 cvar->set_is_closure();
da244e59 11922 Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
11923 NULL, cvar);
f8bdf81a 11924 new_no->func_value()->set_closure_var(cp);
0afbb937 11925
f8bdf81a 11926 gogo->start_block(loc);
0afbb937 11927
11928 // Field 0 of the closure is the function code pointer, field 1 is
11929 // the value on which to invoke the method.
11930 Expression* arg = Expression::make_var_reference(cp, loc);
f614ea8b 11931 arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc);
0afbb937 11932 arg = Expression::make_field_reference(arg, 1, loc);
11933
11934 Expression *ifre = Expression::make_interface_field_reference(arg, name,
11935 loc);
11936
11937 const Typed_identifier_list* orig_params = orig_fntype->parameters();
11938 Expression_list* args;
11939 if (orig_params == NULL || orig_params->empty())
11940 args = NULL;
11941 else
11942 {
11943 const Typed_identifier_list* new_params = new_fntype->parameters();
11944 args = new Expression_list();
11945 for (Typed_identifier_list::const_iterator p = new_params->begin();
f8bdf81a 11946 p != new_params->end();
0afbb937 11947 ++p)
11948 {
11949 Named_object* p_no = gogo->lookup(p->name(), NULL);
11950 go_assert(p_no != NULL
11951 && p_no->is_variable()
11952 && p_no->var_value()->is_parameter());
11953 args->push_back(Expression::make_var_reference(p_no, loc));
11954 }
11955 }
11956
11957 Call_expression* call = Expression::make_call(ifre, args,
11958 orig_fntype->is_varargs(),
11959 loc);
11960 call->set_varargs_are_lowered();
11961
11962 Statement* s = Statement::make_return_from_call(call, loc);
11963 gogo->add_statement(s);
11964 Block* b = gogo->finish_block(loc);
11965 gogo->add_block(b, loc);
11966 gogo->lower_block(new_no, b);
a32698ee 11967 gogo->flatten_block(new_no, b);
0afbb937 11968 gogo->finish_function(loc);
11969
11970 ins.first->second->push_back(std::make_pair(name, new_no));
11971 return new_no;
11972}
11973
ea664253 11974// Get the backend representation for a method value.
e440a328 11975
ea664253 11976Bexpression*
11977Interface_field_reference_expression::do_get_backend(Translate_context* context)
e440a328 11978{
0afbb937 11979 Interface_type* type = this->expr_->type()->interface_type();
11980 if (type == NULL)
11981 {
11982 go_assert(saw_errors());
ea664253 11983 return context->backend()->error_expression();
0afbb937 11984 }
11985
11986 Named_object* thunk =
11987 Interface_field_reference_expression::create_thunk(context->gogo(),
11988 type, this->name_);
11989 if (thunk->is_erroneous())
11990 {
11991 go_assert(saw_errors());
ea664253 11992 return context->backend()->error_expression();
0afbb937 11993 }
11994
11995 // FIXME: We should lower this earlier, but we can't it lower it in
11996 // the lowering pass because at that point we don't know whether we
11997 // need to create the thunk or not. If the expression is called, we
11998 // don't need the thunk.
11999
12000 Location loc = this->location();
12001
12002 Struct_field_list* fields = new Struct_field_list();
12003 fields->push_back(Struct_field(Typed_identifier("fn.0",
12004 thunk->func_value()->type(),
12005 loc)));
12006 fields->push_back(Struct_field(Typed_identifier("val.1",
12007 this->expr_->type(),
12008 loc)));
12009 Struct_type* st = Type::make_struct_type(fields, loc);
6bf4793c 12010 st->set_is_struct_incomparable();
0afbb937 12011
12012 Expression_list* vals = new Expression_list();
12013 vals->push_back(Expression::make_func_code_reference(thunk, loc));
12014 vals->push_back(this->expr_);
12015
12016 Expression* expr = Expression::make_struct_composite_literal(st, vals, loc);
ea664253 12017 Bexpression* bclosure =
12018 Expression::make_heap_expression(expr, loc)->get_backend(context);
0afbb937 12019
4df0c2d4 12020 Gogo* gogo = context->gogo();
12021 Btype* btype = this->type()->get_backend(gogo);
12022 bclosure = gogo->backend()->convert_expression(btype, bclosure, loc);
12023
2387f644 12024 Expression* nil_check =
12025 Expression::make_binary(OPERATOR_EQEQ, this->expr_,
12026 Expression::make_nil(loc), loc);
ea664253 12027 Bexpression* bnil_check = nil_check->get_backend(context);
0afbb937 12028
ea664253 12029 Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
12030 loc)->get_backend(context);
2387f644 12031
93715b75 12032 Bfunction* bfn = context->function()->func_value()->get_decl();
2387f644 12033 Bexpression* bcond =
93715b75 12034 gogo->backend()->conditional_expression(bfn, NULL,
12035 bnil_check, bcrash, NULL, loc);
0ab48656 12036 Bfunction* bfunction = context->function()->func_value()->get_decl();
12037 Bstatement* cond_statement =
12038 gogo->backend()->expression_statement(bfunction, bcond);
ea664253 12039 return gogo->backend()->compound_expression(cond_statement, bclosure, loc);
e440a328 12040}
12041
d751bb78 12042// Dump ast representation for an interface field reference.
12043
12044void
12045Interface_field_reference_expression::do_dump_expression(
12046 Ast_dump_context* ast_dump_context) const
12047{
12048 this->expr_->dump_expression(ast_dump_context);
12049 ast_dump_context->ostream() << "." << this->name_;
12050}
12051
e440a328 12052// Make a reference to a field in an interface.
12053
12054Expression*
12055Expression::make_interface_field_reference(Expression* expr,
12056 const std::string& field,
b13c66cd 12057 Location location)
e440a328 12058{
12059 return new Interface_field_reference_expression(expr, field, location);
12060}
12061
12062// A general selector. This is a Parser_expression for LEFT.NAME. It
12063// is lowered after we know the type of the left hand side.
12064
12065class Selector_expression : public Parser_expression
12066{
12067 public:
12068 Selector_expression(Expression* left, const std::string& name,
b13c66cd 12069 Location location)
e440a328 12070 : Parser_expression(EXPRESSION_SELECTOR, location),
12071 left_(left), name_(name)
12072 { }
12073
12074 protected:
12075 int
12076 do_traverse(Traverse* traverse)
12077 { return Expression::traverse(&this->left_, traverse); }
12078
12079 Expression*
ceeb4318 12080 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 12081
12082 Expression*
12083 do_copy()
12084 {
12085 return new Selector_expression(this->left_->copy(), this->name_,
12086 this->location());
12087 }
12088
d751bb78 12089 void
12090 do_dump_expression(Ast_dump_context* ast_dump_context) const;
12091
e440a328 12092 private:
12093 Expression*
12094 lower_method_expression(Gogo*);
12095
12096 // The expression on the left hand side.
12097 Expression* left_;
12098 // The name on the right hand side.
12099 std::string name_;
12100};
12101
12102// Lower a selector expression once we know the real type of the left
12103// hand side.
12104
12105Expression*
ceeb4318 12106Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
12107 int)
e440a328 12108{
12109 Expression* left = this->left_;
12110 if (left->is_type_expression())
12111 return this->lower_method_expression(gogo);
12112 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
12113 this->location());
12114}
12115
12116// Lower a method expression T.M or (*T).M. We turn this into a
12117// function literal.
12118
12119Expression*
12120Selector_expression::lower_method_expression(Gogo* gogo)
12121{
b13c66cd 12122 Location location = this->location();
868b439e 12123 Type* left_type = this->left_->type();
12124 Type* type = left_type;
e440a328 12125 const std::string& name(this->name_);
12126
12127 bool is_pointer;
12128 if (type->points_to() == NULL)
12129 is_pointer = false;
12130 else
12131 {
12132 is_pointer = true;
12133 type = type->points_to();
12134 }
12135 Named_type* nt = type->named_type();
12136 if (nt == NULL)
12137 {
631d5788 12138 go_error_at(location,
12139 ("method expression requires named type or "
12140 "pointer to named type"));
e440a328 12141 return Expression::make_error(location);
12142 }
12143
12144 bool is_ambiguous;
12145 Method* method = nt->method_function(name, &is_ambiguous);
ab1468c3 12146 const Typed_identifier* imethod = NULL;
dcc8506b 12147 if (method == NULL && !is_pointer)
ab1468c3 12148 {
12149 Interface_type* it = nt->interface_type();
12150 if (it != NULL)
12151 imethod = it->find_method(name);
12152 }
12153
868b439e 12154 if ((method == NULL && imethod == NULL)
12155 || (left_type->named_type() != NULL && left_type->points_to() != NULL))
e440a328 12156 {
12157 if (!is_ambiguous)
631d5788 12158 go_error_at(location, "type %<%s%s%> has no method %<%s%>",
12159 is_pointer ? "*" : "",
12160 nt->message_name().c_str(),
12161 Gogo::message_name(name).c_str());
e440a328 12162 else
631d5788 12163 go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
12164 Gogo::message_name(name).c_str(),
12165 is_pointer ? "*" : "",
12166 nt->message_name().c_str());
e440a328 12167 return Expression::make_error(location);
12168 }
12169
ab1468c3 12170 if (method != NULL && !is_pointer && !method->is_value_method())
e440a328 12171 {
631d5788 12172 go_error_at(location, "method requires pointer (use %<(*%s).%s%>)",
12173 nt->message_name().c_str(),
12174 Gogo::message_name(name).c_str());
e440a328 12175 return Expression::make_error(location);
12176 }
12177
12178 // Build a new function type in which the receiver becomes the first
12179 // argument.
ab1468c3 12180 Function_type* method_type;
12181 if (method != NULL)
12182 {
12183 method_type = method->type();
c484d925 12184 go_assert(method_type->is_method());
ab1468c3 12185 }
12186 else
12187 {
12188 method_type = imethod->type()->function_type();
c484d925 12189 go_assert(method_type != NULL && !method_type->is_method());
ab1468c3 12190 }
e440a328 12191
12192 const char* const receiver_name = "$this";
12193 Typed_identifier_list* parameters = new Typed_identifier_list();
12194 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
12195 location));
12196
12197 const Typed_identifier_list* method_parameters = method_type->parameters();
12198 if (method_parameters != NULL)
12199 {
f470da59 12200 int i = 0;
e440a328 12201 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
12202 p != method_parameters->end();
f470da59 12203 ++p, ++i)
12204 {
68883531 12205 if (!p->name().empty())
f470da59 12206 parameters->push_back(*p);
12207 else
12208 {
12209 char buf[20];
12210 snprintf(buf, sizeof buf, "$param%d", i);
12211 parameters->push_back(Typed_identifier(buf, p->type(),
12212 p->location()));
12213 }
12214 }
e440a328 12215 }
12216
12217 const Typed_identifier_list* method_results = method_type->results();
12218 Typed_identifier_list* results;
12219 if (method_results == NULL)
12220 results = NULL;
12221 else
12222 {
12223 results = new Typed_identifier_list();
12224 for (Typed_identifier_list::const_iterator p = method_results->begin();
12225 p != method_results->end();
12226 ++p)
12227 results->push_back(*p);
12228 }
12229
12230 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
12231 location);
12232 if (method_type->is_varargs())
12233 fntype->set_is_varargs();
12234
12235 // We generate methods which always takes a pointer to the receiver
12236 // as their first argument. If this is for a pointer type, we can
12237 // simply reuse the existing function. We use an internal hack to
12238 // get the right type.
8381eda7 12239 // FIXME: This optimization is disabled because it doesn't yet work
12240 // with function descriptors when the method expression is not
12241 // directly called.
12242 if (method != NULL && is_pointer && false)
e440a328 12243 {
12244 Named_object* mno = (method->needs_stub_method()
12245 ? method->stub_object()
12246 : method->named_object());
12247 Expression* f = Expression::make_func_reference(mno, NULL, location);
12248 f = Expression::make_cast(fntype, f, location);
12249 Type_conversion_expression* tce =
12250 static_cast<Type_conversion_expression*>(f);
12251 tce->set_may_convert_function_types();
12252 return f;
12253 }
12254
12255 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
12256 location);
12257
12258 Named_object* vno = gogo->lookup(receiver_name, NULL);
c484d925 12259 go_assert(vno != NULL);
e440a328 12260 Expression* ve = Expression::make_var_reference(vno, location);
ab1468c3 12261 Expression* bm;
12262 if (method != NULL)
12263 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
12264 else
12265 bm = Expression::make_interface_field_reference(ve, name, location);
f690b0bb 12266
12267 // Even though we found the method above, if it has an error type we
12268 // may see an error here.
12269 if (bm->is_error_expression())
463fe805 12270 {
12271 gogo->finish_function(location);
12272 return bm;
12273 }
e440a328 12274
12275 Expression_list* args;
f470da59 12276 if (parameters->size() <= 1)
e440a328 12277 args = NULL;
12278 else
12279 {
12280 args = new Expression_list();
f470da59 12281 Typed_identifier_list::const_iterator p = parameters->begin();
12282 ++p;
12283 for (; p != parameters->end(); ++p)
e440a328 12284 {
12285 vno = gogo->lookup(p->name(), NULL);
c484d925 12286 go_assert(vno != NULL);
e440a328 12287 args->push_back(Expression::make_var_reference(vno, location));
12288 }
12289 }
12290
ceeb4318 12291 gogo->start_block(location);
12292
e440a328 12293 Call_expression* call = Expression::make_call(bm, args,
12294 method_type->is_varargs(),
12295 location);
12296
0afbb937 12297 Statement* s = Statement::make_return_from_call(call, location);
e440a328 12298 gogo->add_statement(s);
12299
ceeb4318 12300 Block* b = gogo->finish_block(location);
12301
12302 gogo->add_block(b, location);
12303
12304 // Lower the call in case there are multiple results.
12305 gogo->lower_block(no, b);
a32698ee 12306 gogo->flatten_block(no, b);
ceeb4318 12307
e440a328 12308 gogo->finish_function(location);
12309
12310 return Expression::make_func_reference(no, NULL, location);
12311}
12312
d751bb78 12313// Dump the ast for a selector expression.
12314
12315void
12316Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12317 const
12318{
12319 ast_dump_context->dump_expression(this->left_);
12320 ast_dump_context->ostream() << ".";
12321 ast_dump_context->ostream() << this->name_;
12322}
12323
e440a328 12324// Make a selector expression.
12325
12326Expression*
12327Expression::make_selector(Expression* left, const std::string& name,
b13c66cd 12328 Location location)
e440a328 12329{
12330 return new Selector_expression(left, name, location);
12331}
12332
da244e59 12333// Class Allocation_expression.
e440a328 12334
da244e59 12335int
12336Allocation_expression::do_traverse(Traverse* traverse)
e440a328 12337{
da244e59 12338 return Type::traverse(this->type_, traverse);
12339}
e440a328 12340
da244e59 12341Type*
12342Allocation_expression::do_type()
12343{
12344 return Type::make_pointer_type(this->type_);
12345}
e440a328 12346
22deed0d 12347void
12348Allocation_expression::do_check_types(Gogo*)
12349{
12350 if (!this->type_->in_heap())
12351 go_error_at(this->location(), "can't heap allocate go:notinheap type");
12352}
12353
da244e59 12354// Make a copy of an allocation expression.
e440a328 12355
da244e59 12356Expression*
12357Allocation_expression::do_copy()
12358{
12359 Allocation_expression* alloc =
12360 new Allocation_expression(this->type_, this->location());
12361 if (this->allocate_on_stack_)
12362 alloc->set_allocate_on_stack();
12363 return alloc;
12364}
e440a328 12365
ea664253 12366// Return the backend representation for an allocation expression.
e440a328 12367
ea664253 12368Bexpression*
12369Allocation_expression::do_get_backend(Translate_context* context)
e440a328 12370{
2c809f8f 12371 Gogo* gogo = context->gogo();
12372 Location loc = this->location();
da244e59 12373
45ff893b 12374 Node* n = Node::make_node(this);
12375 if (this->allocate_on_stack_
12376 || (n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
da244e59 12377 {
2a305b85 12378 int64_t size;
12379 bool ok = this->type_->backend_type_size(gogo, &size);
12380 if (!ok)
12381 {
12382 go_assert(saw_errors());
12383 return gogo->backend()->error_expression();
12384 }
d5d1c295 12385 return gogo->backend()->stack_allocation_expression(size, loc);
da244e59 12386 }
12387
2a305b85 12388 Btype* btype = this->type_->get_backend(gogo);
12389 Bexpression* space =
ea664253 12390 gogo->allocate_memory(this->type_, loc)->get_backend(context);
d5d1c295 12391 Btype* pbtype = gogo->backend()->pointer_type(btype);
ea664253 12392 return gogo->backend()->convert_expression(pbtype, space, loc);
e440a328 12393}
12394
d751bb78 12395// Dump ast representation for an allocation expression.
12396
12397void
12398Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12399 const
12400{
12401 ast_dump_context->ostream() << "new(";
12402 ast_dump_context->dump_type(this->type_);
12403 ast_dump_context->ostream() << ")";
12404}
12405
e440a328 12406// Make an allocation expression.
12407
12408Expression*
b13c66cd 12409Expression::make_allocation(Type* type, Location location)
e440a328 12410{
12411 return new Allocation_expression(type, location);
12412}
12413
e32de7ba 12414// Class Ordered_value_list.
e440a328 12415
12416int
e32de7ba 12417Ordered_value_list::traverse_vals(Traverse* traverse)
e440a328 12418{
0c4f5a19 12419 if (this->vals_ != NULL)
12420 {
12421 if (this->traverse_order_ == NULL)
12422 {
12423 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12424 return TRAVERSE_EXIT;
12425 }
12426 else
12427 {
e32de7ba 12428 for (std::vector<unsigned long>::const_iterator p =
12429 this->traverse_order_->begin();
0c4f5a19 12430 p != this->traverse_order_->end();
12431 ++p)
12432 {
12433 if (Expression::traverse(&this->vals_->at(*p), traverse)
12434 == TRAVERSE_EXIT)
12435 return TRAVERSE_EXIT;
12436 }
12437 }
12438 }
e32de7ba 12439 return TRAVERSE_CONTINUE;
12440}
12441
12442// Class Struct_construction_expression.
12443
12444// Traversal.
12445
12446int
12447Struct_construction_expression::do_traverse(Traverse* traverse)
12448{
12449 if (this->traverse_vals(traverse) == TRAVERSE_EXIT)
12450 return TRAVERSE_EXIT;
e440a328 12451 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12452 return TRAVERSE_EXIT;
12453 return TRAVERSE_CONTINUE;
12454}
12455
12456// Return whether this is a constant initializer.
12457
12458bool
12459Struct_construction_expression::is_constant_struct() const
12460{
e32de7ba 12461 if (this->vals() == NULL)
e440a328 12462 return true;
e32de7ba 12463 for (Expression_list::const_iterator pv = this->vals()->begin();
12464 pv != this->vals()->end();
e440a328 12465 ++pv)
12466 {
12467 if (*pv != NULL
12468 && !(*pv)->is_constant()
12469 && (!(*pv)->is_composite_literal()
12470 || (*pv)->is_nonconstant_composite_literal()))
12471 return false;
12472 }
12473
12474 const Struct_field_list* fields = this->type_->struct_type()->fields();
12475 for (Struct_field_list::const_iterator pf = fields->begin();
12476 pf != fields->end();
12477 ++pf)
12478 {
12479 // There are no constant constructors for interfaces.
12480 if (pf->type()->interface_type() != NULL)
12481 return false;
12482 }
12483
12484 return true;
12485}
12486
3ae06f68 12487// Return whether this struct can be used as a constant initializer.
f9ca30f9 12488
12489bool
3ae06f68 12490Struct_construction_expression::do_is_static_initializer() const
f9ca30f9 12491{
e32de7ba 12492 if (this->vals() == NULL)
f9ca30f9 12493 return true;
e32de7ba 12494 for (Expression_list::const_iterator pv = this->vals()->begin();
12495 pv != this->vals()->end();
f9ca30f9 12496 ++pv)
12497 {
3ae06f68 12498 if (*pv != NULL && !(*pv)->is_static_initializer())
f9ca30f9 12499 return false;
12500 }
de048538 12501
12502 const Struct_field_list* fields = this->type_->struct_type()->fields();
12503 for (Struct_field_list::const_iterator pf = fields->begin();
12504 pf != fields->end();
12505 ++pf)
12506 {
12507 // There are no constant constructors for interfaces.
12508 if (pf->type()->interface_type() != NULL)
12509 return false;
12510 }
12511
f9ca30f9 12512 return true;
12513}
12514
e440a328 12515// Final type determination.
12516
12517void
12518Struct_construction_expression::do_determine_type(const Type_context*)
12519{
e32de7ba 12520 if (this->vals() == NULL)
e440a328 12521 return;
12522 const Struct_field_list* fields = this->type_->struct_type()->fields();
e32de7ba 12523 Expression_list::const_iterator pv = this->vals()->begin();
e440a328 12524 for (Struct_field_list::const_iterator pf = fields->begin();
12525 pf != fields->end();
12526 ++pf, ++pv)
12527 {
e32de7ba 12528 if (pv == this->vals()->end())
e440a328 12529 return;
12530 if (*pv != NULL)
12531 {
12532 Type_context subcontext(pf->type(), false);
12533 (*pv)->determine_type(&subcontext);
12534 }
12535 }
a6cb4c0e 12536 // Extra values are an error we will report elsewhere; we still want
12537 // to determine the type to avoid knockon errors.
e32de7ba 12538 for (; pv != this->vals()->end(); ++pv)
a6cb4c0e 12539 (*pv)->determine_type_no_context();
e440a328 12540}
12541
12542// Check types.
12543
12544void
12545Struct_construction_expression::do_check_types(Gogo*)
12546{
e32de7ba 12547 if (this->vals() == NULL)
e440a328 12548 return;
12549
12550 Struct_type* st = this->type_->struct_type();
e32de7ba 12551 if (this->vals()->size() > st->field_count())
e440a328 12552 {
12553 this->report_error(_("too many expressions for struct"));
12554 return;
12555 }
12556
12557 const Struct_field_list* fields = st->fields();
e32de7ba 12558 Expression_list::const_iterator pv = this->vals()->begin();
e440a328 12559 int i = 0;
12560 for (Struct_field_list::const_iterator pf = fields->begin();
12561 pf != fields->end();
12562 ++pf, ++pv, ++i)
12563 {
e32de7ba 12564 if (pv == this->vals()->end())
e440a328 12565 {
12566 this->report_error(_("too few expressions for struct"));
12567 break;
12568 }
12569
12570 if (*pv == NULL)
12571 continue;
12572
12573 std::string reason;
12574 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
12575 {
12576 if (reason.empty())
631d5788 12577 go_error_at((*pv)->location(),
12578 "incompatible type for field %d in struct construction",
12579 i + 1);
e440a328 12580 else
631d5788 12581 go_error_at((*pv)->location(),
12582 ("incompatible type for field %d in "
12583 "struct construction (%s)"),
12584 i + 1, reason.c_str());
e440a328 12585 this->set_is_error();
12586 }
12587 }
e32de7ba 12588 go_assert(pv == this->vals()->end());
e440a328 12589}
12590
8ba8cc87 12591// Flatten a struct construction expression. Store the values into
12592// temporaries in case they need interface conversion.
12593
12594Expression*
12595Struct_construction_expression::do_flatten(Gogo*, Named_object*,
12596 Statement_inserter* inserter)
12597{
e32de7ba 12598 if (this->vals() == NULL)
8ba8cc87 12599 return this;
12600
12601 // If this is a constant struct, we don't need temporaries.
de048538 12602 if (this->is_constant_struct() || this->is_static_initializer())
8ba8cc87 12603 return this;
12604
12605 Location loc = this->location();
e32de7ba 12606 for (Expression_list::iterator pv = this->vals()->begin();
12607 pv != this->vals()->end();
8ba8cc87 12608 ++pv)
12609 {
12610 if (*pv != NULL)
12611 {
5bf8be8b 12612 if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type())
12613 {
12614 go_assert(saw_errors());
12615 return Expression::make_error(loc);
12616 }
8ba8cc87 12617 if (!(*pv)->is_variable())
12618 {
12619 Temporary_statement* temp =
12620 Statement::make_temporary(NULL, *pv, loc);
12621 inserter->insert(temp);
12622 *pv = Expression::make_temporary_reference(temp, loc);
12623 }
12624 }
12625 }
12626 return this;
12627}
12628
ea664253 12629// Return the backend representation for constructing a struct.
e440a328 12630
ea664253 12631Bexpression*
12632Struct_construction_expression::do_get_backend(Translate_context* context)
e440a328 12633{
12634 Gogo* gogo = context->gogo();
12635
2c809f8f 12636 Btype* btype = this->type_->get_backend(gogo);
e32de7ba 12637 if (this->vals() == NULL)
ea664253 12638 return gogo->backend()->zero_expression(btype);
e440a328 12639
e440a328 12640 const Struct_field_list* fields = this->type_->struct_type()->fields();
e32de7ba 12641 Expression_list::const_iterator pv = this->vals()->begin();
2c809f8f 12642 std::vector<Bexpression*> init;
12643 for (Struct_field_list::const_iterator pf = fields->begin();
12644 pf != fields->end();
12645 ++pf)
e440a328 12646 {
63697958 12647 Btype* fbtype = pf->type()->get_backend(gogo);
e32de7ba 12648 if (pv == this->vals()->end())
2c809f8f 12649 init.push_back(gogo->backend()->zero_expression(fbtype));
e440a328 12650 else if (*pv == NULL)
12651 {
2c809f8f 12652 init.push_back(gogo->backend()->zero_expression(fbtype));
e440a328 12653 ++pv;
12654 }
12655 else
12656 {
2c809f8f 12657 Expression* val =
12658 Expression::convert_for_assignment(gogo, pf->type(),
12659 *pv, this->location());
ea664253 12660 init.push_back(val->get_backend(context));
e440a328 12661 ++pv;
12662 }
e440a328 12663 }
ea664253 12664 return gogo->backend()->constructor_expression(btype, init, this->location());
e440a328 12665}
12666
12667// Export a struct construction.
12668
12669void
12670Struct_construction_expression::do_export(Export* exp) const
12671{
12672 exp->write_c_string("convert(");
12673 exp->write_type(this->type_);
e32de7ba 12674 for (Expression_list::const_iterator pv = this->vals()->begin();
12675 pv != this->vals()->end();
e440a328 12676 ++pv)
12677 {
12678 exp->write_c_string(", ");
12679 if (*pv != NULL)
12680 (*pv)->export_expression(exp);
12681 }
12682 exp->write_c_string(")");
12683}
12684
d751bb78 12685// Dump ast representation of a struct construction expression.
12686
12687void
12688Struct_construction_expression::do_dump_expression(
12689 Ast_dump_context* ast_dump_context) const
12690{
d751bb78 12691 ast_dump_context->dump_type(this->type_);
12692 ast_dump_context->ostream() << "{";
e32de7ba 12693 ast_dump_context->dump_expression_list(this->vals());
d751bb78 12694 ast_dump_context->ostream() << "}";
12695}
12696
e440a328 12697// Make a struct composite literal. This used by the thunk code.
12698
12699Expression*
12700Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
b13c66cd 12701 Location location)
e440a328 12702{
c484d925 12703 go_assert(type->struct_type() != NULL);
e440a328 12704 return new Struct_construction_expression(type, vals, location);
12705}
12706
da244e59 12707// Class Array_construction_expression.
e440a328 12708
12709// Traversal.
12710
12711int
12712Array_construction_expression::do_traverse(Traverse* traverse)
12713{
e32de7ba 12714 if (this->traverse_vals(traverse) == TRAVERSE_EXIT)
e440a328 12715 return TRAVERSE_EXIT;
12716 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12717 return TRAVERSE_EXIT;
12718 return TRAVERSE_CONTINUE;
12719}
12720
12721// Return whether this is a constant initializer.
12722
12723bool
12724Array_construction_expression::is_constant_array() const
12725{
e32de7ba 12726 if (this->vals() == NULL)
e440a328 12727 return true;
12728
12729 // There are no constant constructors for interfaces.
12730 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12731 return false;
12732
e32de7ba 12733 for (Expression_list::const_iterator pv = this->vals()->begin();
12734 pv != this->vals()->end();
e440a328 12735 ++pv)
12736 {
12737 if (*pv != NULL
12738 && !(*pv)->is_constant()
12739 && (!(*pv)->is_composite_literal()
12740 || (*pv)->is_nonconstant_composite_literal()))
12741 return false;
12742 }
12743 return true;
12744}
12745
3ae06f68 12746// Return whether this can be used a constant initializer.
f9ca30f9 12747
12748bool
3ae06f68 12749Array_construction_expression::do_is_static_initializer() const
f9ca30f9 12750{
e32de7ba 12751 if (this->vals() == NULL)
f9ca30f9 12752 return true;
de048538 12753
12754 // There are no constant constructors for interfaces.
12755 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12756 return false;
12757
e32de7ba 12758 for (Expression_list::const_iterator pv = this->vals()->begin();
12759 pv != this->vals()->end();
f9ca30f9 12760 ++pv)
12761 {
3ae06f68 12762 if (*pv != NULL && !(*pv)->is_static_initializer())
f9ca30f9 12763 return false;
12764 }
12765 return true;
12766}
12767
e440a328 12768// Final type determination.
12769
12770void
12771Array_construction_expression::do_determine_type(const Type_context*)
12772{
e32de7ba 12773 if (this->vals() == NULL)
e440a328 12774 return;
12775 Type_context subcontext(this->type_->array_type()->element_type(), false);
e32de7ba 12776 for (Expression_list::const_iterator pv = this->vals()->begin();
12777 pv != this->vals()->end();
e440a328 12778 ++pv)
12779 {
12780 if (*pv != NULL)
12781 (*pv)->determine_type(&subcontext);
12782 }
12783}
12784
12785// Check types.
12786
12787void
12788Array_construction_expression::do_check_types(Gogo*)
12789{
e32de7ba 12790 if (this->vals() == NULL)
e440a328 12791 return;
12792
12793 Array_type* at = this->type_->array_type();
12794 int i = 0;
12795 Type* element_type = at->element_type();
e32de7ba 12796 for (Expression_list::const_iterator pv = this->vals()->begin();
12797 pv != this->vals()->end();
e440a328 12798 ++pv, ++i)
12799 {
12800 if (*pv != NULL
12801 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12802 {
631d5788 12803 go_error_at((*pv)->location(),
12804 "incompatible type for element %d in composite literal",
12805 i + 1);
e440a328 12806 this->set_is_error();
12807 }
12808 }
e440a328 12809}
12810
8ba8cc87 12811// Flatten an array construction expression. Store the values into
12812// temporaries in case they need interface conversion.
12813
12814Expression*
12815Array_construction_expression::do_flatten(Gogo*, Named_object*,
12816 Statement_inserter* inserter)
12817{
e32de7ba 12818 if (this->vals() == NULL)
8ba8cc87 12819 return this;
12820
12821 // If this is a constant array, we don't need temporaries.
de048538 12822 if (this->is_constant_array() || this->is_static_initializer())
8ba8cc87 12823 return this;
12824
12825 Location loc = this->location();
e32de7ba 12826 for (Expression_list::iterator pv = this->vals()->begin();
12827 pv != this->vals()->end();
8ba8cc87 12828 ++pv)
12829 {
12830 if (*pv != NULL)
12831 {
5bf8be8b 12832 if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type())
12833 {
12834 go_assert(saw_errors());
12835 return Expression::make_error(loc);
12836 }
8ba8cc87 12837 if (!(*pv)->is_variable())
12838 {
12839 Temporary_statement* temp =
12840 Statement::make_temporary(NULL, *pv, loc);
12841 inserter->insert(temp);
12842 *pv = Expression::make_temporary_reference(temp, loc);
12843 }
12844 }
12845 }
12846 return this;
12847}
12848
2c809f8f 12849// Get a constructor expression for the array values.
e440a328 12850
2c809f8f 12851Bexpression*
12852Array_construction_expression::get_constructor(Translate_context* context,
12853 Btype* array_btype)
e440a328 12854{
e440a328 12855 Type* element_type = this->type_->array_type()->element_type();
2c809f8f 12856
12857 std::vector<unsigned long> indexes;
12858 std::vector<Bexpression*> vals;
12859 Gogo* gogo = context->gogo();
e32de7ba 12860 if (this->vals() != NULL)
e440a328 12861 {
12862 size_t i = 0;
ffe743ca 12863 std::vector<unsigned long>::const_iterator pi;
12864 if (this->indexes_ != NULL)
12865 pi = this->indexes_->begin();
e32de7ba 12866 for (Expression_list::const_iterator pv = this->vals()->begin();
12867 pv != this->vals()->end();
e440a328 12868 ++pv, ++i)
12869 {
ffe743ca 12870 if (this->indexes_ != NULL)
12871 go_assert(pi != this->indexes_->end());
ffe743ca 12872
12873 if (this->indexes_ == NULL)
2c809f8f 12874 indexes.push_back(i);
ffe743ca 12875 else
2c809f8f 12876 indexes.push_back(*pi);
e440a328 12877 if (*pv == NULL)
63697958 12878 {
63697958 12879 Btype* ebtype = element_type->get_backend(gogo);
12880 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
2c809f8f 12881 vals.push_back(zv);
63697958 12882 }
e440a328 12883 else
12884 {
2c809f8f 12885 Expression* val_expr =
12886 Expression::convert_for_assignment(gogo, element_type, *pv,
12887 this->location());
ea664253 12888 vals.push_back(val_expr->get_backend(context));
e440a328 12889 }
ffe743ca 12890 if (this->indexes_ != NULL)
12891 ++pi;
e440a328 12892 }
ffe743ca 12893 if (this->indexes_ != NULL)
12894 go_assert(pi == this->indexes_->end());
e440a328 12895 }
2c809f8f 12896 return gogo->backend()->array_constructor_expression(array_btype, indexes,
12897 vals, this->location());
e440a328 12898}
12899
12900// Export an array construction.
12901
12902void
12903Array_construction_expression::do_export(Export* exp) const
12904{
12905 exp->write_c_string("convert(");
12906 exp->write_type(this->type_);
e32de7ba 12907 if (this->vals() != NULL)
e440a328 12908 {
ffe743ca 12909 std::vector<unsigned long>::const_iterator pi;
12910 if (this->indexes_ != NULL)
12911 pi = this->indexes_->begin();
e32de7ba 12912 for (Expression_list::const_iterator pv = this->vals()->begin();
12913 pv != this->vals()->end();
e440a328 12914 ++pv)
12915 {
12916 exp->write_c_string(", ");
ffe743ca 12917
12918 if (this->indexes_ != NULL)
12919 {
12920 char buf[100];
12921 snprintf(buf, sizeof buf, "%lu", *pi);
12922 exp->write_c_string(buf);
12923 exp->write_c_string(":");
12924 }
12925
e440a328 12926 if (*pv != NULL)
12927 (*pv)->export_expression(exp);
ffe743ca 12928
12929 if (this->indexes_ != NULL)
12930 ++pi;
e440a328 12931 }
12932 }
12933 exp->write_c_string(")");
12934}
12935
0e9a2e72 12936// Dump ast representation of an array construction expression.
d751bb78 12937
12938void
12939Array_construction_expression::do_dump_expression(
12940 Ast_dump_context* ast_dump_context) const
12941{
ffe743ca 12942 Expression* length = this->type_->array_type()->length();
8b1c301d 12943
12944 ast_dump_context->ostream() << "[" ;
12945 if (length != NULL)
12946 {
12947 ast_dump_context->dump_expression(length);
12948 }
12949 ast_dump_context->ostream() << "]" ;
d751bb78 12950 ast_dump_context->dump_type(this->type_);
0e9a2e72 12951 this->dump_slice_storage_expression(ast_dump_context);
d751bb78 12952 ast_dump_context->ostream() << "{" ;
ffe743ca 12953 if (this->indexes_ == NULL)
e32de7ba 12954 ast_dump_context->dump_expression_list(this->vals());
ffe743ca 12955 else
12956 {
e32de7ba 12957 Expression_list::const_iterator pv = this->vals()->begin();
ffe743ca 12958 for (std::vector<unsigned long>::const_iterator pi =
12959 this->indexes_->begin();
12960 pi != this->indexes_->end();
12961 ++pi, ++pv)
12962 {
12963 if (pi != this->indexes_->begin())
12964 ast_dump_context->ostream() << ", ";
12965 ast_dump_context->ostream() << *pi << ':';
12966 ast_dump_context->dump_expression(*pv);
12967 }
12968 }
d751bb78 12969 ast_dump_context->ostream() << "}" ;
12970
12971}
12972
da244e59 12973// Class Fixed_array_construction_expression.
e440a328 12974
da244e59 12975Fixed_array_construction_expression::Fixed_array_construction_expression(
12976 Type* type, const std::vector<unsigned long>* indexes,
12977 Expression_list* vals, Location location)
12978 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
12979 type, indexes, vals, location)
12980{ go_assert(type->array_type() != NULL && !type->is_slice_type()); }
e440a328 12981
ea664253 12982// Return the backend representation for constructing a fixed array.
e440a328 12983
ea664253 12984Bexpression*
12985Fixed_array_construction_expression::do_get_backend(Translate_context* context)
e440a328 12986{
9f0e0513 12987 Type* type = this->type();
12988 Btype* btype = type->get_backend(context->gogo());
ea664253 12989 return this->get_constructor(context, btype);
e440a328 12990}
12991
76f85fd6 12992Expression*
12993Expression::make_array_composite_literal(Type* type, Expression_list* vals,
12994 Location location)
12995{
12996 go_assert(type->array_type() != NULL && !type->is_slice_type());
12997 return new Fixed_array_construction_expression(type, NULL, vals, location);
12998}
12999
da244e59 13000// Class Slice_construction_expression.
e440a328 13001
da244e59 13002Slice_construction_expression::Slice_construction_expression(
13003 Type* type, const std::vector<unsigned long>* indexes,
13004 Expression_list* vals, Location location)
13005 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION,
13006 type, indexes, vals, location),
0e9a2e72 13007 valtype_(NULL), array_val_(NULL), slice_storage_(NULL),
13008 storage_escapes_(true)
e440a328 13009{
da244e59 13010 go_assert(type->is_slice_type());
23d77f91 13011
da244e59 13012 unsigned long lenval;
13013 Expression* length;
13014 if (vals == NULL || vals->empty())
13015 lenval = 0;
13016 else
13017 {
13018 if (this->indexes() == NULL)
13019 lenval = vals->size();
13020 else
13021 lenval = indexes->back() + 1;
13022 }
13023 Type* int_type = Type::lookup_integer_type("int");
13024 length = Expression::make_integer_ul(lenval, int_type, location);
13025 Type* element_type = type->array_type()->element_type();
6bf4793c 13026 Array_type* array_type = Type::make_array_type(element_type, length);
13027 array_type->set_is_array_incomparable();
13028 this->valtype_ = array_type;
da244e59 13029}
e440a328 13030
23d77f91 13031// Traversal.
13032
13033int
13034Slice_construction_expression::do_traverse(Traverse* traverse)
13035{
13036 if (this->Array_construction_expression::do_traverse(traverse)
13037 == TRAVERSE_EXIT)
13038 return TRAVERSE_EXIT;
13039 if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT)
13040 return TRAVERSE_EXIT;
0e9a2e72 13041 if (this->array_val_ != NULL
13042 && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT)
13043 return TRAVERSE_EXIT;
13044 if (this->slice_storage_ != NULL
13045 && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT)
13046 return TRAVERSE_EXIT;
23d77f91 13047 return TRAVERSE_CONTINUE;
13048}
13049
0e9a2e72 13050// Helper routine to create fixed array value underlying the slice literal.
13051// May be called during flattening, or later during do_get_backend().
e440a328 13052
0e9a2e72 13053Expression*
13054Slice_construction_expression::create_array_val()
e440a328 13055{
f9c68f17 13056 Array_type* array_type = this->type()->array_type();
13057 if (array_type == NULL)
13058 {
c484d925 13059 go_assert(this->type()->is_error());
0e9a2e72 13060 return NULL;
f9c68f17 13061 }
13062
f23d7786 13063 Location loc = this->location();
23d77f91 13064 go_assert(this->valtype_ != NULL);
3d60812e 13065
f23d7786 13066 Expression_list* vals = this->vals();
0e9a2e72 13067 return new Fixed_array_construction_expression(
13068 this->valtype_, this->indexes(), vals, loc);
13069}
13070
13071// If we're previous established that the slice storage does not
13072// escape, then create a separate array temp val here for it. We
13073// need to do this as part of flattening so as to be able to insert
13074// the new temp statement.
13075
13076Expression*
13077Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no,
13078 Statement_inserter* inserter)
13079{
13080 if (this->type()->array_type() == NULL)
13081 return NULL;
13082
13083 // Base class flattening first
13084 this->Array_construction_expression::do_flatten(gogo, no, inserter);
13085
a1bbc2c3 13086 // Create a stack-allocated storage temp if storage won't escape
03118c21 13087 if (!this->storage_escapes_
13088 && this->slice_storage_ == NULL
13089 && this->element_count() > 0)
0e9a2e72 13090 {
13091 Location loc = this->location();
03118c21 13092 this->array_val_ = this->create_array_val();
0e9a2e72 13093 go_assert(this->array_val_);
13094 Temporary_statement* temp =
13095 Statement::make_temporary(this->valtype_, this->array_val_, loc);
13096 inserter->insert(temp);
13097 this->slice_storage_ = Expression::make_temporary_reference(temp, loc);
13098 }
13099 return this;
13100}
13101
13102// When dumping a slice construction expression that has an explicit
13103// storeage temp, emit the temp here (if we don't do this the storage
13104// temp appears unused in the AST dump).
13105
13106void
13107Slice_construction_expression::
13108dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const
13109{
13110 if (this->slice_storage_ == NULL)
13111 return;
13112 ast_dump_context->ostream() << "storage=" ;
13113 ast_dump_context->dump_expression(this->slice_storage_);
13114}
13115
13116// Return the backend representation for constructing a slice.
13117
13118Bexpression*
13119Slice_construction_expression::do_get_backend(Translate_context* context)
13120{
13121 if (this->array_val_ == NULL)
03118c21 13122 this->array_val_ = this->create_array_val();
0e9a2e72 13123 if (this->array_val_ == NULL)
13124 {
13125 go_assert(this->type()->is_error());
13126 return context->backend()->error_expression();
13127 }
13128
13129 Location loc = this->location();
e440a328 13130
3ae06f68 13131 bool is_static_initializer = this->array_val_->is_static_initializer();
d8829beb 13132
13133 // We have to copy the initial values into heap memory if we are in
3ae06f68 13134 // a function or if the values are not constants.
13135 bool copy_to_heap = context->function() != NULL || !is_static_initializer;
e440a328 13136
f23d7786 13137 Expression* space;
0e9a2e72 13138
13139 if (this->slice_storage_ != NULL)
13140 {
13141 go_assert(!this->storage_escapes_);
13142 space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc);
13143 }
13144 else if (!copy_to_heap)
e440a328 13145 {
f23d7786 13146 // The initializer will only run once.
0e9a2e72 13147 space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc);
f23d7786 13148 space->unary_expression()->set_is_slice_init();
e440a328 13149 }
13150 else
45ff893b 13151 {
0e9a2e72 13152 space = Expression::make_heap_expression(this->array_val_, loc);
45ff893b 13153 Node* n = Node::make_node(this);
13154 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
13155 {
13156 n = Node::make_node(space);
13157 n->set_encoding(Node::ESCAPE_NONE);
13158 }
13159 }
e440a328 13160
2c809f8f 13161 // Build a constructor for the slice.
f23d7786 13162 Expression* len = this->valtype_->array_type()->length();
13163 Expression* slice_val =
13164 Expression::make_slice_value(this->type(), space, len, len, loc);
ea664253 13165 return slice_val->get_backend(context);
e440a328 13166}
13167
13168// Make a slice composite literal. This is used by the type
13169// descriptor code.
13170
0e9a2e72 13171Slice_construction_expression*
e440a328 13172Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
b13c66cd 13173 Location location)
e440a328 13174{
411eb89e 13175 go_assert(type->is_slice_type());
2c809f8f 13176 return new Slice_construction_expression(type, NULL, vals, location);
e440a328 13177}
13178
da244e59 13179// Class Map_construction_expression.
e440a328 13180
13181// Traversal.
13182
13183int
13184Map_construction_expression::do_traverse(Traverse* traverse)
13185{
13186 if (this->vals_ != NULL
13187 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
13188 return TRAVERSE_EXIT;
13189 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13190 return TRAVERSE_EXIT;
13191 return TRAVERSE_CONTINUE;
13192}
13193
2c809f8f 13194// Flatten constructor initializer into a temporary variable since
13195// we need to take its address for __go_construct_map.
13196
13197Expression*
13198Map_construction_expression::do_flatten(Gogo* gogo, Named_object*,
13199 Statement_inserter* inserter)
13200{
13201 if (!this->is_error_expression()
13202 && this->vals_ != NULL
13203 && !this->vals_->empty()
13204 && this->constructor_temp_ == NULL)
13205 {
13206 Map_type* mt = this->type_->map_type();
13207 Type* key_type = mt->key_type();
13208 Type* val_type = mt->val_type();
13209 this->element_type_ = Type::make_builtin_struct_type(2,
13210 "__key", key_type,
13211 "__val", val_type);
13212
13213 Expression_list* value_pairs = new Expression_list();
13214 Location loc = this->location();
13215
13216 size_t i = 0;
13217 for (Expression_list::const_iterator pv = this->vals_->begin();
13218 pv != this->vals_->end();
13219 ++pv, ++i)
13220 {
13221 Expression_list* key_value_pair = new Expression_list();
91c0fd76 13222 Expression* key = *pv;
5bf8be8b 13223 if (key->is_error_expression() || key->type()->is_error_type())
13224 {
13225 go_assert(saw_errors());
13226 return Expression::make_error(loc);
13227 }
91c0fd76 13228 if (key->type()->interface_type() != NULL && !key->is_variable())
13229 {
13230 Temporary_statement* temp =
13231 Statement::make_temporary(NULL, key, loc);
13232 inserter->insert(temp);
13233 key = Expression::make_temporary_reference(temp, loc);
13234 }
13235 key = Expression::convert_for_assignment(gogo, key_type, key, loc);
2c809f8f 13236
13237 ++pv;
91c0fd76 13238 Expression* val = *pv;
5bf8be8b 13239 if (val->is_error_expression() || val->type()->is_error_type())
13240 {
13241 go_assert(saw_errors());
13242 return Expression::make_error(loc);
13243 }
91c0fd76 13244 if (val->type()->interface_type() != NULL && !val->is_variable())
13245 {
13246 Temporary_statement* temp =
13247 Statement::make_temporary(NULL, val, loc);
13248 inserter->insert(temp);
13249 val = Expression::make_temporary_reference(temp, loc);
13250 }
13251 val = Expression::convert_for_assignment(gogo, val_type, val, loc);
2c809f8f 13252
13253 key_value_pair->push_back(key);
13254 key_value_pair->push_back(val);
13255 value_pairs->push_back(
13256 Expression::make_struct_composite_literal(this->element_type_,
13257 key_value_pair, loc));
13258 }
13259
e67508fa 13260 Expression* element_count = Expression::make_integer_ul(i, NULL, loc);
6bf4793c 13261 Array_type* ctor_type =
2c809f8f 13262 Type::make_array_type(this->element_type_, element_count);
6bf4793c 13263 ctor_type->set_is_array_incomparable();
2c809f8f 13264 Expression* constructor =
13265 new Fixed_array_construction_expression(ctor_type, NULL,
13266 value_pairs, loc);
13267
13268 this->constructor_temp_ =
13269 Statement::make_temporary(NULL, constructor, loc);
13270 constructor->issue_nil_check();
13271 this->constructor_temp_->set_is_address_taken();
13272 inserter->insert(this->constructor_temp_);
13273 }
13274
13275 return this;
13276}
13277
e440a328 13278// Final type determination.
13279
13280void
13281Map_construction_expression::do_determine_type(const Type_context*)
13282{
13283 if (this->vals_ == NULL)
13284 return;
13285
13286 Map_type* mt = this->type_->map_type();
13287 Type_context key_context(mt->key_type(), false);
13288 Type_context val_context(mt->val_type(), false);
13289 for (Expression_list::const_iterator pv = this->vals_->begin();
13290 pv != this->vals_->end();
13291 ++pv)
13292 {
13293 (*pv)->determine_type(&key_context);
13294 ++pv;
13295 (*pv)->determine_type(&val_context);
13296 }
13297}
13298
13299// Check types.
13300
13301void
13302Map_construction_expression::do_check_types(Gogo*)
13303{
13304 if (this->vals_ == NULL)
13305 return;
13306
13307 Map_type* mt = this->type_->map_type();
13308 int i = 0;
13309 Type* key_type = mt->key_type();
13310 Type* val_type = mt->val_type();
13311 for (Expression_list::const_iterator pv = this->vals_->begin();
13312 pv != this->vals_->end();
13313 ++pv, ++i)
13314 {
13315 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
13316 {
631d5788 13317 go_error_at((*pv)->location(),
13318 "incompatible type for element %d key in map construction",
13319 i + 1);
e440a328 13320 this->set_is_error();
13321 }
13322 ++pv;
13323 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
13324 {
631d5788 13325 go_error_at((*pv)->location(),
13326 ("incompatible type for element %d value "
13327 "in map construction"),
e440a328 13328 i + 1);
13329 this->set_is_error();
13330 }
13331 }
13332}
13333
ea664253 13334// Return the backend representation for constructing a map.
e440a328 13335
ea664253 13336Bexpression*
13337Map_construction_expression::do_get_backend(Translate_context* context)
e440a328 13338{
2c809f8f 13339 if (this->is_error_expression())
ea664253 13340 return context->backend()->error_expression();
2c809f8f 13341 Location loc = this->location();
e440a328 13342
e440a328 13343 size_t i = 0;
2c809f8f 13344 Expression* ventries;
e440a328 13345 if (this->vals_ == NULL || this->vals_->empty())
2c809f8f 13346 ventries = Expression::make_nil(loc);
e440a328 13347 else
13348 {
2c809f8f 13349 go_assert(this->constructor_temp_ != NULL);
13350 i = this->vals_->size() / 2;
e440a328 13351
2c809f8f 13352 Expression* ctor_ref =
13353 Expression::make_temporary_reference(this->constructor_temp_, loc);
13354 ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc);
13355 }
e440a328 13356
2c809f8f 13357 Map_type* mt = this->type_->map_type();
13358 if (this->element_type_ == NULL)
13359 this->element_type_ =
13360 Type::make_builtin_struct_type(2,
13361 "__key", mt->key_type(),
13362 "__val", mt->val_type());
0d5530d9 13363 Expression* descriptor = Expression::make_type_descriptor(mt, loc);
2c809f8f 13364
13365 Type* uintptr_t = Type::lookup_integer_type("uintptr");
e67508fa 13366 Expression* count = Expression::make_integer_ul(i, uintptr_t, loc);
2c809f8f 13367
13368 Expression* entry_size =
13369 Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE);
13370
13371 unsigned int field_index;
13372 const Struct_field* valfield =
13373 this->element_type_->find_local_field("__val", &field_index);
13374 Expression* val_offset =
13375 Expression::make_struct_field_offset(this->element_type_, valfield);
2c809f8f 13376
13377 Expression* map_ctor =
0d5530d9 13378 Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count,
13379 entry_size, val_offset, ventries);
ea664253 13380 return map_ctor->get_backend(context);
2c809f8f 13381}
e440a328 13382
2c809f8f 13383// Export an array construction.
e440a328 13384
2c809f8f 13385void
13386Map_construction_expression::do_export(Export* exp) const
13387{
13388 exp->write_c_string("convert(");
13389 exp->write_type(this->type_);
13390 for (Expression_list::const_iterator pv = this->vals_->begin();
13391 pv != this->vals_->end();
13392 ++pv)
13393 {
13394 exp->write_c_string(", ");
13395 (*pv)->export_expression(exp);
13396 }
13397 exp->write_c_string(")");
13398}
e440a328 13399
2c809f8f 13400// Dump ast representation for a map construction expression.
d751bb78 13401
13402void
13403Map_construction_expression::do_dump_expression(
13404 Ast_dump_context* ast_dump_context) const
13405{
d751bb78 13406 ast_dump_context->ostream() << "{" ;
8b1c301d 13407 ast_dump_context->dump_expression_list(this->vals_, true);
d751bb78 13408 ast_dump_context->ostream() << "}";
13409}
13410
7795ac51 13411// Class Composite_literal_expression.
e440a328 13412
13413// Traversal.
13414
13415int
13416Composite_literal_expression::do_traverse(Traverse* traverse)
13417{
dbffccfc 13418 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
e440a328 13419 return TRAVERSE_EXIT;
dbffccfc 13420
13421 // If this is a struct composite literal with keys, then the keys
13422 // are field names, not expressions. We don't want to traverse them
13423 // in that case. If we do, we can give an erroneous error "variable
13424 // initializer refers to itself." See bug482.go in the testsuite.
13425 if (this->has_keys_ && this->vals_ != NULL)
13426 {
13427 // The type may not be resolvable at this point.
13428 Type* type = this->type_;
a01f2481 13429
7795ac51 13430 for (int depth = 0; depth < this->depth_; ++depth)
a01f2481 13431 {
13432 if (type->array_type() != NULL)
13433 type = type->array_type()->element_type();
13434 else if (type->map_type() != NULL)
7795ac51 13435 {
13436 if (this->key_path_[depth])
13437 type = type->map_type()->key_type();
13438 else
13439 type = type->map_type()->val_type();
13440 }
a01f2481 13441 else
13442 {
13443 // This error will be reported during lowering.
13444 return TRAVERSE_CONTINUE;
13445 }
13446 }
13447
dbffccfc 13448 while (true)
13449 {
13450 if (type->classification() == Type::TYPE_NAMED)
13451 type = type->named_type()->real_type();
13452 else if (type->classification() == Type::TYPE_FORWARD)
13453 {
13454 Type* t = type->forwarded();
13455 if (t == type)
13456 break;
13457 type = t;
13458 }
13459 else
13460 break;
13461 }
13462
13463 if (type->classification() == Type::TYPE_STRUCT)
13464 {
13465 Expression_list::iterator p = this->vals_->begin();
13466 while (p != this->vals_->end())
13467 {
13468 // Skip key.
13469 ++p;
13470 go_assert(p != this->vals_->end());
13471 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13472 return TRAVERSE_EXIT;
13473 ++p;
13474 }
13475 return TRAVERSE_CONTINUE;
13476 }
13477 }
13478
13479 if (this->vals_ != NULL)
13480 return this->vals_->traverse(traverse);
13481
13482 return TRAVERSE_CONTINUE;
e440a328 13483}
13484
13485// Lower a generic composite literal into a specific version based on
13486// the type.
13487
13488Expression*
ceeb4318 13489Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
13490 Statement_inserter* inserter, int)
e440a328 13491{
13492 Type* type = this->type_;
13493
7795ac51 13494 for (int depth = 0; depth < this->depth_; ++depth)
e440a328 13495 {
13496 if (type->array_type() != NULL)
13497 type = type->array_type()->element_type();
13498 else if (type->map_type() != NULL)
7795ac51 13499 {
13500 if (this->key_path_[depth])
13501 type = type->map_type()->key_type();
13502 else
13503 type = type->map_type()->val_type();
13504 }
e440a328 13505 else
13506 {
5c13bd80 13507 if (!type->is_error())
631d5788 13508 go_error_at(this->location(),
13509 ("may only omit types within composite literals "
13510 "of slice, array, or map type"));
e440a328 13511 return Expression::make_error(this->location());
13512 }
13513 }
13514
e00772b3 13515 Type *pt = type->points_to();
13516 bool is_pointer = false;
13517 if (pt != NULL)
13518 {
13519 is_pointer = true;
13520 type = pt;
13521 }
13522
13523 Expression* ret;
5c13bd80 13524 if (type->is_error())
e440a328 13525 return Expression::make_error(this->location());
13526 else if (type->struct_type() != NULL)
e00772b3 13527 ret = this->lower_struct(gogo, type);
e440a328 13528 else if (type->array_type() != NULL)
113ef6a5 13529 ret = this->lower_array(type);
e440a328 13530 else if (type->map_type() != NULL)
e00772b3 13531 ret = this->lower_map(gogo, function, inserter, type);
e440a328 13532 else
13533 {
631d5788 13534 go_error_at(this->location(),
13535 ("expected struct, slice, array, or map type "
13536 "for composite literal"));
e440a328 13537 return Expression::make_error(this->location());
13538 }
e00772b3 13539
13540 if (is_pointer)
2c809f8f 13541 ret = Expression::make_heap_expression(ret, this->location());
e00772b3 13542
13543 return ret;
e440a328 13544}
13545
13546// Lower a struct composite literal.
13547
13548Expression*
81c4b26b 13549Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
e440a328 13550{
b13c66cd 13551 Location location = this->location();
e440a328 13552 Struct_type* st = type->struct_type();
13553 if (this->vals_ == NULL || !this->has_keys_)
07daa4e7 13554 {
e6013c28 13555 if (this->vals_ != NULL
13556 && !this->vals_->empty()
13557 && type->named_type() != NULL
13558 && type->named_type()->named_object()->package() != NULL)
13559 {
13560 for (Struct_field_list::const_iterator pf = st->fields()->begin();
13561 pf != st->fields()->end();
13562 ++pf)
07daa4e7 13563 {
07ba7f26 13564 if (Gogo::is_hidden_name(pf->field_name())
13565 || pf->is_embedded_builtin(gogo))
631d5788 13566 go_error_at(this->location(),
13567 "assignment of unexported field %qs in %qs literal",
13568 Gogo::message_name(pf->field_name()).c_str(),
13569 type->named_type()->message_name().c_str());
07daa4e7 13570 }
13571 }
13572
13573 return new Struct_construction_expression(type, this->vals_, location);
13574 }
e440a328 13575
13576 size_t field_count = st->field_count();
13577 std::vector<Expression*> vals(field_count);
e32de7ba 13578 std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>);
e440a328 13579 Expression_list::const_iterator p = this->vals_->begin();
62750cd5 13580 Expression* external_expr = NULL;
13581 const Named_object* external_no = NULL;
e440a328 13582 while (p != this->vals_->end())
13583 {
13584 Expression* name_expr = *p;
13585
13586 ++p;
c484d925 13587 go_assert(p != this->vals_->end());
e440a328 13588 Expression* val = *p;
13589
13590 ++p;
13591
13592 if (name_expr == NULL)
13593 {
631d5788 13594 go_error_at(val->location(),
13595 "mixture of field and value initializers");
e440a328 13596 return Expression::make_error(location);
13597 }
13598
13599 bool bad_key = false;
13600 std::string name;
81c4b26b 13601 const Named_object* no = NULL;
e440a328 13602 switch (name_expr->classification())
13603 {
13604 case EXPRESSION_UNKNOWN_REFERENCE:
13605 name = name_expr->unknown_expression()->name();
7f7ce694 13606 if (type->named_type() != NULL)
13607 {
13608 // If the named object found for this field name comes from a
13609 // different package than the struct it is a part of, do not count
13610 // this incorrect lookup as a usage of the object's package.
13611 no = name_expr->unknown_expression()->named_object();
13612 if (no->package() != NULL
13613 && no->package() != type->named_type()->named_object()->package())
13614 no->package()->forget_usage(name_expr);
13615 }
e440a328 13616 break;
13617
13618 case EXPRESSION_CONST_REFERENCE:
81c4b26b 13619 no = static_cast<Const_expression*>(name_expr)->named_object();
e440a328 13620 break;
13621
13622 case EXPRESSION_TYPE:
13623 {
13624 Type* t = name_expr->type();
13625 Named_type* nt = t->named_type();
13626 if (nt == NULL)
13627 bad_key = true;
13628 else
81c4b26b 13629 no = nt->named_object();
e440a328 13630 }
13631 break;
13632
13633 case EXPRESSION_VAR_REFERENCE:
81c4b26b 13634 no = name_expr->var_expression()->named_object();
e440a328 13635 break;
13636
b0c09712 13637 case EXPRESSION_ENCLOSED_VAR_REFERENCE:
13638 no = name_expr->enclosed_var_expression()->variable();
e440a328 13639 break;
13640
b0c09712 13641 case EXPRESSION_FUNC_REFERENCE:
13642 no = name_expr->func_expression()->named_object();
e440a328 13643 break;
13644
13645 default:
13646 bad_key = true;
13647 break;
13648 }
13649 if (bad_key)
13650 {
631d5788 13651 go_error_at(name_expr->location(), "expected struct field name");
e440a328 13652 return Expression::make_error(location);
13653 }
13654
81c4b26b 13655 if (no != NULL)
13656 {
62750cd5 13657 if (no->package() != NULL && external_expr == NULL)
13658 {
13659 external_expr = name_expr;
13660 external_no = no;
13661 }
13662
81c4b26b 13663 name = no->name();
13664
13665 // A predefined name won't be packed. If it starts with a
13666 // lower case letter we need to check for that case, because
2d29d278 13667 // the field name will be packed. FIXME.
81c4b26b 13668 if (!Gogo::is_hidden_name(name)
13669 && name[0] >= 'a'
13670 && name[0] <= 'z')
13671 {
13672 Named_object* gno = gogo->lookup_global(name.c_str());
13673 if (gno == no)
13674 name = gogo->pack_hidden_name(name, false);
13675 }
13676 }
13677
e440a328 13678 unsigned int index;
13679 const Struct_field* sf = st->find_local_field(name, &index);
13680 if (sf == NULL)
13681 {
631d5788 13682 go_error_at(name_expr->location(), "unknown field %qs in %qs",
13683 Gogo::message_name(name).c_str(),
13684 (type->named_type() != NULL
13685 ? type->named_type()->message_name().c_str()
13686 : "unnamed struct"));
e440a328 13687 return Expression::make_error(location);
13688 }
13689 if (vals[index] != NULL)
13690 {
631d5788 13691 go_error_at(name_expr->location(),
13692 "duplicate value for field %qs in %qs",
13693 Gogo::message_name(name).c_str(),
13694 (type->named_type() != NULL
13695 ? type->named_type()->message_name().c_str()
13696 : "unnamed struct"));
e440a328 13697 return Expression::make_error(location);
13698 }
13699
07daa4e7 13700 if (type->named_type() != NULL
13701 && type->named_type()->named_object()->package() != NULL
07ba7f26 13702 && (Gogo::is_hidden_name(sf->field_name())
13703 || sf->is_embedded_builtin(gogo)))
631d5788 13704 go_error_at(name_expr->location(),
13705 "assignment of unexported field %qs in %qs literal",
13706 Gogo::message_name(sf->field_name()).c_str(),
13707 type->named_type()->message_name().c_str());
07daa4e7 13708
e440a328 13709 vals[index] = val;
e32de7ba 13710 traverse_order->push_back(static_cast<unsigned long>(index));
e440a328 13711 }
13712
62750cd5 13713 if (!this->all_are_names_)
13714 {
13715 // This is a weird case like bug462 in the testsuite.
13716 if (external_expr == NULL)
631d5788 13717 go_error_at(this->location(), "unknown field in %qs literal",
13718 (type->named_type() != NULL
13719 ? type->named_type()->message_name().c_str()
13720 : "unnamed struct"));
62750cd5 13721 else
631d5788 13722 go_error_at(external_expr->location(), "unknown field %qs in %qs",
13723 external_no->message_name().c_str(),
13724 (type->named_type() != NULL
13725 ? type->named_type()->message_name().c_str()
13726 : "unnamed struct"));
62750cd5 13727 return Expression::make_error(location);
13728 }
13729
e440a328 13730 Expression_list* list = new Expression_list;
13731 list->reserve(field_count);
13732 for (size_t i = 0; i < field_count; ++i)
13733 list->push_back(vals[i]);
13734
0c4f5a19 13735 Struct_construction_expression* ret =
13736 new Struct_construction_expression(type, list, location);
13737 ret->set_traverse_order(traverse_order);
13738 return ret;
e440a328 13739}
13740
e32de7ba 13741// Index/value/traversal-order triple.
00773463 13742
e32de7ba 13743struct IVT_triple {
13744 unsigned long index;
13745 unsigned long traversal_order;
13746 Expression* expr;
13747 IVT_triple(unsigned long i, unsigned long to, Expression *e)
13748 : index(i), traversal_order(to), expr(e) { }
13749 bool operator<(const IVT_triple& other) const
13750 { return this->index < other.index; }
00773463 13751};
13752
e440a328 13753// Lower an array composite literal.
13754
13755Expression*
113ef6a5 13756Composite_literal_expression::lower_array(Type* type)
e440a328 13757{
b13c66cd 13758 Location location = this->location();
e440a328 13759 if (this->vals_ == NULL || !this->has_keys_)
ffe743ca 13760 return this->make_array(type, NULL, this->vals_);
e440a328 13761
ffe743ca 13762 std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
13763 indexes->reserve(this->vals_->size());
00773463 13764 bool indexes_out_of_order = false;
ffe743ca 13765 Expression_list* vals = new Expression_list();
13766 vals->reserve(this->vals_->size());
e440a328 13767 unsigned long index = 0;
13768 Expression_list::const_iterator p = this->vals_->begin();
13769 while (p != this->vals_->end())
13770 {
13771 Expression* index_expr = *p;
13772
13773 ++p;
c484d925 13774 go_assert(p != this->vals_->end());
e440a328 13775 Expression* val = *p;
13776
13777 ++p;
13778
ffe743ca 13779 if (index_expr == NULL)
13780 {
13781 if (!indexes->empty())
13782 indexes->push_back(index);
13783 }
13784 else
e440a328 13785 {
ffe743ca 13786 if (indexes->empty() && !vals->empty())
13787 {
13788 for (size_t i = 0; i < vals->size(); ++i)
13789 indexes->push_back(i);
13790 }
13791
0c77715b 13792 Numeric_constant nc;
13793 if (!index_expr->numeric_constant_value(&nc))
e440a328 13794 {
631d5788 13795 go_error_at(index_expr->location(),
13796 "index expression is not integer constant");
e440a328 13797 return Expression::make_error(location);
13798 }
6f6d9955 13799
0c77715b 13800 switch (nc.to_unsigned_long(&index))
e440a328 13801 {
0c77715b 13802 case Numeric_constant::NC_UL_VALID:
13803 break;
13804 case Numeric_constant::NC_UL_NOTINT:
631d5788 13805 go_error_at(index_expr->location(),
13806 "index expression is not integer constant");
0c77715b 13807 return Expression::make_error(location);
13808 case Numeric_constant::NC_UL_NEGATIVE:
631d5788 13809 go_error_at(index_expr->location(),
13810 "index expression is negative");
e440a328 13811 return Expression::make_error(location);
0c77715b 13812 case Numeric_constant::NC_UL_BIG:
631d5788 13813 go_error_at(index_expr->location(), "index value overflow");
e440a328 13814 return Expression::make_error(location);
0c77715b 13815 default:
13816 go_unreachable();
e440a328 13817 }
6f6d9955 13818
13819 Named_type* ntype = Type::lookup_integer_type("int");
13820 Integer_type* inttype = ntype->integer_type();
0c77715b 13821 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8)
13822 && index >> (inttype->bits() - 1) != 0)
6f6d9955 13823 {
631d5788 13824 go_error_at(index_expr->location(), "index value overflow");
6f6d9955 13825 return Expression::make_error(location);
13826 }
13827
ffe743ca 13828 if (std::find(indexes->begin(), indexes->end(), index)
13829 != indexes->end())
e440a328 13830 {
631d5788 13831 go_error_at(index_expr->location(),
13832 "duplicate value for index %lu",
13833 index);
e440a328 13834 return Expression::make_error(location);
13835 }
ffe743ca 13836
00773463 13837 if (!indexes->empty() && index < indexes->back())
13838 indexes_out_of_order = true;
13839
ffe743ca 13840 indexes->push_back(index);
e440a328 13841 }
13842
ffe743ca 13843 vals->push_back(val);
13844
e440a328 13845 ++index;
13846 }
13847
ffe743ca 13848 if (indexes->empty())
13849 {
13850 delete indexes;
13851 indexes = NULL;
13852 }
e440a328 13853
e32de7ba 13854 std::vector<unsigned long>* traverse_order = NULL;
00773463 13855 if (indexes_out_of_order)
13856 {
e32de7ba 13857 typedef std::vector<IVT_triple> V;
00773463 13858
13859 V v;
13860 v.reserve(indexes->size());
13861 std::vector<unsigned long>::const_iterator pi = indexes->begin();
e32de7ba 13862 unsigned long torder = 0;
00773463 13863 for (Expression_list::const_iterator pe = vals->begin();
13864 pe != vals->end();
e32de7ba 13865 ++pe, ++pi, ++torder)
13866 v.push_back(IVT_triple(*pi, torder, *pe));
00773463 13867
e32de7ba 13868 std::sort(v.begin(), v.end());
00773463 13869
13870 delete indexes;
13871 delete vals;
e32de7ba 13872
00773463 13873 indexes = new std::vector<unsigned long>();
13874 indexes->reserve(v.size());
13875 vals = new Expression_list();
13876 vals->reserve(v.size());
e32de7ba 13877 traverse_order = new std::vector<unsigned long>();
13878 traverse_order->reserve(v.size());
00773463 13879
13880 for (V::const_iterator p = v.begin(); p != v.end(); ++p)
13881 {
e32de7ba 13882 indexes->push_back(p->index);
13883 vals->push_back(p->expr);
13884 traverse_order->push_back(p->traversal_order);
00773463 13885 }
13886 }
13887
e32de7ba 13888 Expression* ret = this->make_array(type, indexes, vals);
13889 Array_construction_expression* ace = ret->array_literal();
13890 if (ace != NULL && traverse_order != NULL)
13891 ace->set_traverse_order(traverse_order);
13892 return ret;
e440a328 13893}
13894
13895// Actually build the array composite literal. This handles
13896// [...]{...}.
13897
13898Expression*
ffe743ca 13899Composite_literal_expression::make_array(
13900 Type* type,
13901 const std::vector<unsigned long>* indexes,
13902 Expression_list* vals)
e440a328 13903{
b13c66cd 13904 Location location = this->location();
e440a328 13905 Array_type* at = type->array_type();
ffe743ca 13906
e440a328 13907 if (at->length() != NULL && at->length()->is_nil_expression())
13908 {
ffe743ca 13909 size_t size;
13910 if (vals == NULL)
13911 size = 0;
00773463 13912 else if (indexes != NULL)
13913 size = indexes->back() + 1;
13914 else
ffe743ca 13915 {
13916 size = vals->size();
13917 Integer_type* it = Type::lookup_integer_type("int")->integer_type();
13918 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8)
13919 && size >> (it->bits() - 1) != 0)
13920 {
631d5788 13921 go_error_at(location, "too many elements in composite literal");
ffe743ca 13922 return Expression::make_error(location);
13923 }
13924 }
ffe743ca 13925
e67508fa 13926 Expression* elen = Expression::make_integer_ul(size, NULL, location);
e440a328 13927 at = Type::make_array_type(at->element_type(), elen);
13928 type = at;
13929 }
ffe743ca 13930 else if (at->length() != NULL
13931 && !at->length()->is_error_expression()
13932 && this->vals_ != NULL)
13933 {
13934 Numeric_constant nc;
13935 unsigned long val;
13936 if (at->length()->numeric_constant_value(&nc)
13937 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
13938 {
13939 if (indexes == NULL)
13940 {
13941 if (this->vals_->size() > val)
13942 {
631d5788 13943 go_error_at(location,
13944 "too many elements in composite literal");
ffe743ca 13945 return Expression::make_error(location);
13946 }
13947 }
13948 else
13949 {
00773463 13950 unsigned long max = indexes->back();
ffe743ca 13951 if (max >= val)
13952 {
631d5788 13953 go_error_at(location,
13954 ("some element keys in composite literal "
13955 "are out of range"));
ffe743ca 13956 return Expression::make_error(location);
13957 }
13958 }
13959 }
13960 }
13961
e440a328 13962 if (at->length() != NULL)
ffe743ca 13963 return new Fixed_array_construction_expression(type, indexes, vals,
13964 location);
e440a328 13965 else
2c809f8f 13966 return new Slice_construction_expression(type, indexes, vals, location);
e440a328 13967}
13968
13969// Lower a map composite literal.
13970
13971Expression*
a287720d 13972Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
ceeb4318 13973 Statement_inserter* inserter,
a287720d 13974 Type* type)
e440a328 13975{
b13c66cd 13976 Location location = this->location();
e440a328 13977 if (this->vals_ != NULL)
13978 {
13979 if (!this->has_keys_)
13980 {
631d5788 13981 go_error_at(location, "map composite literal must have keys");
e440a328 13982 return Expression::make_error(location);
13983 }
13984
a287720d 13985 for (Expression_list::iterator p = this->vals_->begin();
e440a328 13986 p != this->vals_->end();
13987 p += 2)
13988 {
13989 if (*p == NULL)
13990 {
13991 ++p;
631d5788 13992 go_error_at((*p)->location(),
13993 ("map composite literal must "
13994 "have keys for every value"));
e440a328 13995 return Expression::make_error(location);
13996 }
a287720d 13997 // Make sure we have lowered the key; it may not have been
13998 // lowered in order to handle keys for struct composite
13999 // literals. Lower it now to get the right error message.
14000 if ((*p)->unknown_expression() != NULL)
14001 {
14002 (*p)->unknown_expression()->clear_is_composite_literal_key();
ceeb4318 14003 gogo->lower_expression(function, inserter, &*p);
c484d925 14004 go_assert((*p)->is_error_expression());
a287720d 14005 return Expression::make_error(location);
14006 }
e440a328 14007 }
14008 }
14009
14010 return new Map_construction_expression(type, this->vals_, location);
14011}
14012
d751bb78 14013// Dump ast representation for a composite literal expression.
14014
14015void
14016Composite_literal_expression::do_dump_expression(
14017 Ast_dump_context* ast_dump_context) const
14018{
8b1c301d 14019 ast_dump_context->ostream() << "composite(";
d751bb78 14020 ast_dump_context->dump_type(this->type_);
14021 ast_dump_context->ostream() << ", {";
8b1c301d 14022 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
d751bb78 14023 ast_dump_context->ostream() << "})";
14024}
14025
e440a328 14026// Make a composite literal expression.
14027
14028Expression*
14029Expression::make_composite_literal(Type* type, int depth, bool has_keys,
62750cd5 14030 Expression_list* vals, bool all_are_names,
b13c66cd 14031 Location location)
e440a328 14032{
14033 return new Composite_literal_expression(type, depth, has_keys, vals,
62750cd5 14034 all_are_names, location);
e440a328 14035}
14036
14037// Return whether this expression is a composite literal.
14038
14039bool
14040Expression::is_composite_literal() const
14041{
14042 switch (this->classification_)
14043 {
14044 case EXPRESSION_COMPOSITE_LITERAL:
14045 case EXPRESSION_STRUCT_CONSTRUCTION:
14046 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
2c809f8f 14047 case EXPRESSION_SLICE_CONSTRUCTION:
e440a328 14048 case EXPRESSION_MAP_CONSTRUCTION:
14049 return true;
14050 default:
14051 return false;
14052 }
14053}
14054
14055// Return whether this expression is a composite literal which is not
14056// constant.
14057
14058bool
14059Expression::is_nonconstant_composite_literal() const
14060{
14061 switch (this->classification_)
14062 {
14063 case EXPRESSION_STRUCT_CONSTRUCTION:
14064 {
14065 const Struct_construction_expression *psce =
14066 static_cast<const Struct_construction_expression*>(this);
14067 return !psce->is_constant_struct();
14068 }
14069 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
14070 {
14071 const Fixed_array_construction_expression *pace =
14072 static_cast<const Fixed_array_construction_expression*>(this);
14073 return !pace->is_constant_array();
14074 }
2c809f8f 14075 case EXPRESSION_SLICE_CONSTRUCTION:
e440a328 14076 {
2c809f8f 14077 const Slice_construction_expression *pace =
14078 static_cast<const Slice_construction_expression*>(this);
e440a328 14079 return !pace->is_constant_array();
14080 }
14081 case EXPRESSION_MAP_CONSTRUCTION:
14082 return true;
14083 default:
14084 return false;
14085 }
14086}
14087
35a54f17 14088// Return true if this is a variable or temporary_variable.
14089
14090bool
14091Expression::is_variable() const
14092{
14093 switch (this->classification_)
14094 {
14095 case EXPRESSION_VAR_REFERENCE:
14096 case EXPRESSION_TEMPORARY_REFERENCE:
14097 case EXPRESSION_SET_AND_USE_TEMPORARY:
b0c09712 14098 case EXPRESSION_ENCLOSED_VAR_REFERENCE:
35a54f17 14099 return true;
14100 default:
14101 return false;
14102 }
14103}
14104
e440a328 14105// Return true if this is a reference to a local variable.
14106
14107bool
14108Expression::is_local_variable() const
14109{
14110 const Var_expression* ve = this->var_expression();
14111 if (ve == NULL)
14112 return false;
14113 const Named_object* no = ve->named_object();
14114 return (no->is_result_variable()
14115 || (no->is_variable() && !no->var_value()->is_global()));
14116}
14117
14118// Class Type_guard_expression.
14119
14120// Traversal.
14121
14122int
14123Type_guard_expression::do_traverse(Traverse* traverse)
14124{
14125 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
14126 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
14127 return TRAVERSE_EXIT;
14128 return TRAVERSE_CONTINUE;
14129}
14130
2c809f8f 14131Expression*
14132Type_guard_expression::do_flatten(Gogo*, Named_object*,
14133 Statement_inserter* inserter)
14134{
5bf8be8b 14135 if (this->expr_->is_error_expression()
14136 || this->expr_->type()->is_error_type())
14137 {
14138 go_assert(saw_errors());
14139 return Expression::make_error(this->location());
14140 }
14141
2c809f8f 14142 if (!this->expr_->is_variable())
14143 {
14144 Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_,
14145 this->location());
14146 inserter->insert(temp);
14147 this->expr_ =
14148 Expression::make_temporary_reference(temp, this->location());
14149 }
14150 return this;
14151}
14152
e440a328 14153// Check types of a type guard expression. The expression must have
14154// an interface type, but the actual type conversion is checked at run
14155// time.
14156
14157void
14158Type_guard_expression::do_check_types(Gogo*)
14159{
e440a328 14160 Type* expr_type = this->expr_->type();
7e9da23f 14161 if (expr_type->interface_type() == NULL)
f725ade8 14162 {
5c13bd80 14163 if (!expr_type->is_error() && !this->type_->is_error())
f725ade8 14164 this->report_error(_("type assertion only valid for interface types"));
14165 this->set_is_error();
14166 }
e440a328 14167 else if (this->type_->interface_type() == NULL)
14168 {
14169 std::string reason;
14170 if (!expr_type->interface_type()->implements_interface(this->type_,
14171 &reason))
14172 {
5c13bd80 14173 if (!this->type_->is_error())
e440a328 14174 {
f725ade8 14175 if (reason.empty())
14176 this->report_error(_("impossible type assertion: "
14177 "type does not implement interface"));
14178 else
631d5788 14179 go_error_at(this->location(),
14180 ("impossible type assertion: "
14181 "type does not implement interface (%s)"),
14182 reason.c_str());
e440a328 14183 }
f725ade8 14184 this->set_is_error();
e440a328 14185 }
14186 }
14187}
14188
ea664253 14189// Return the backend representation for a type guard expression.
e440a328 14190
ea664253 14191Bexpression*
14192Type_guard_expression::do_get_backend(Translate_context* context)
e440a328 14193{
2c809f8f 14194 Expression* conversion;
7e9da23f 14195 if (this->type_->interface_type() != NULL)
2c809f8f 14196 conversion =
14197 Expression::convert_interface_to_interface(this->type_, this->expr_,
14198 true, this->location());
e440a328 14199 else
2c809f8f 14200 conversion =
14201 Expression::convert_for_assignment(context->gogo(), this->type_,
14202 this->expr_, this->location());
14203
21b70e8f 14204 Gogo* gogo = context->gogo();
14205 Btype* bt = this->type_->get_backend(gogo);
14206 Bexpression* bexpr = conversion->get_backend(context);
14207 return gogo->backend()->convert_expression(bt, bexpr, this->location());
e440a328 14208}
14209
d751bb78 14210// Dump ast representation for a type guard expression.
14211
14212void
2c809f8f 14213Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
d751bb78 14214 const
14215{
14216 this->expr_->dump_expression(ast_dump_context);
14217 ast_dump_context->ostream() << ".";
14218 ast_dump_context->dump_type(this->type_);
14219}
14220
e440a328 14221// Make a type guard expression.
14222
14223Expression*
14224Expression::make_type_guard(Expression* expr, Type* type,
b13c66cd 14225 Location location)
e440a328 14226{
14227 return new Type_guard_expression(expr, type, location);
14228}
14229
2c809f8f 14230// Class Heap_expression.
e440a328 14231
da244e59 14232// Return the type of the expression stored on the heap.
e440a328 14233
da244e59 14234Type*
14235Heap_expression::do_type()
14236{ return Type::make_pointer_type(this->expr_->type()); }
e440a328 14237
ea664253 14238// Return the backend representation for allocating an expression on the heap.
e440a328 14239
ea664253 14240Bexpression*
14241Heap_expression::do_get_backend(Translate_context* context)
e440a328 14242{
03118c21 14243 Type* etype = this->expr_->type();
14244 if (this->expr_->is_error_expression() || etype->is_error())
ea664253 14245 return context->backend()->error_expression();
2c809f8f 14246
02c19a1a 14247 Location loc = this->location();
2c809f8f 14248 Gogo* gogo = context->gogo();
02c19a1a 14249 Btype* btype = this->type()->get_backend(gogo);
45ff893b 14250
03118c21 14251 Expression* alloc = Expression::make_allocation(etype, loc);
45ff893b 14252 Node* n = Node::make_node(this);
14253 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
14254 alloc->allocation_expression()->set_allocate_on_stack();
14255 Bexpression* space = alloc->get_backend(context);
02c19a1a 14256
14257 Bstatement* decl;
14258 Named_object* fn = context->function();
14259 go_assert(fn != NULL);
14260 Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn);
14261 Bvariable* space_temp =
14262 gogo->backend()->temporary_variable(fndecl, context->bblock(), btype,
14263 space, true, loc, &decl);
03118c21 14264 Btype* expr_btype = etype->get_backend(gogo);
02c19a1a 14265
ea664253 14266 Bexpression* bexpr = this->expr_->get_backend(context);
03118c21 14267
14268 // If this assignment needs a write barrier, call typedmemmove. We
14269 // don't do this in the write barrier pass because in some cases
14270 // backend conversion can introduce new Heap_expression values.
14271 Bstatement* assn;
14272 if (!etype->has_pointer())
14273 {
7af8e400 14274 space = gogo->backend()->var_expression(space_temp, loc);
03118c21 14275 Bexpression* ref =
14276 gogo->backend()->indirect_expression(expr_btype, space, true, loc);
14277 assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc);
14278 }
14279 else
14280 {
14281 Bstatement* edecl;
14282 Bvariable* btemp =
14283 gogo->backend()->temporary_variable(fndecl, context->bblock(),
14284 expr_btype, bexpr, true, loc,
14285 &edecl);
14286 Bexpression* btempref = gogo->backend()->var_expression(btemp,
7af8e400 14287 loc);
03118c21 14288 Bexpression* addr = gogo->backend()->address_expression(btempref, loc);
14289
14290 Expression* td = Expression::make_type_descriptor(etype, loc);
14291 Type* etype_ptr = Type::make_pointer_type(etype);
7af8e400 14292 space = gogo->backend()->var_expression(space_temp, loc);
03118c21 14293 Expression* elhs = Expression::make_backend(space, etype_ptr, loc);
14294 Expression* erhs = Expression::make_backend(addr, etype_ptr, loc);
14295 Expression* call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3,
14296 td, elhs, erhs);
14297 Bexpression* bcall = call->get_backend(context);
14298 Bstatement* s = gogo->backend()->expression_statement(fndecl, bcall);
14299 assn = gogo->backend()->compound_statement(edecl, s);
14300 }
02c19a1a 14301 decl = gogo->backend()->compound_statement(decl, assn);
7af8e400 14302 space = gogo->backend()->var_expression(space_temp, loc);
ea664253 14303 return gogo->backend()->compound_expression(decl, space, loc);
e440a328 14304}
14305
2c809f8f 14306// Dump ast representation for a heap expression.
d751bb78 14307
14308void
2c809f8f 14309Heap_expression::do_dump_expression(
d751bb78 14310 Ast_dump_context* ast_dump_context) const
14311{
14312 ast_dump_context->ostream() << "&(";
14313 ast_dump_context->dump_expression(this->expr_);
14314 ast_dump_context->ostream() << ")";
14315}
14316
2c809f8f 14317// Allocate an expression on the heap.
e440a328 14318
14319Expression*
2c809f8f 14320Expression::make_heap_expression(Expression* expr, Location location)
e440a328 14321{
2c809f8f 14322 return new Heap_expression(expr, location);
e440a328 14323}
14324
14325// Class Receive_expression.
14326
14327// Return the type of a receive expression.
14328
14329Type*
14330Receive_expression::do_type()
14331{
e429e3bd 14332 if (this->is_error_expression())
14333 return Type::make_error_type();
e440a328 14334 Channel_type* channel_type = this->channel_->type()->channel_type();
14335 if (channel_type == NULL)
e429e3bd 14336 {
14337 this->report_error(_("expected channel"));
14338 return Type::make_error_type();
14339 }
e440a328 14340 return channel_type->element_type();
14341}
14342
14343// Check types for a receive expression.
14344
14345void
14346Receive_expression::do_check_types(Gogo*)
14347{
14348 Type* type = this->channel_->type();
5c13bd80 14349 if (type->is_error())
e440a328 14350 {
e429e3bd 14351 go_assert(saw_errors());
e440a328 14352 this->set_is_error();
14353 return;
14354 }
14355 if (type->channel_type() == NULL)
14356 {
14357 this->report_error(_("expected channel"));
14358 return;
14359 }
14360 if (!type->channel_type()->may_receive())
14361 {
14362 this->report_error(_("invalid receive on send-only channel"));
14363 return;
14364 }
14365}
14366
2c809f8f 14367// Flattening for receive expressions creates a temporary variable to store
14368// received data in for receives.
14369
14370Expression*
14371Receive_expression::do_flatten(Gogo*, Named_object*,
14372 Statement_inserter* inserter)
14373{
14374 Channel_type* channel_type = this->channel_->type()->channel_type();
14375 if (channel_type == NULL)
14376 {
14377 go_assert(saw_errors());
14378 return this;
14379 }
5bf8be8b 14380 else if (this->channel_->is_error_expression())
14381 {
14382 go_assert(saw_errors());
14383 return Expression::make_error(this->location());
14384 }
2c809f8f 14385
14386 Type* element_type = channel_type->element_type();
14387 if (this->temp_receiver_ == NULL)
14388 {
14389 this->temp_receiver_ = Statement::make_temporary(element_type, NULL,
14390 this->location());
14391 this->temp_receiver_->set_is_address_taken();
14392 inserter->insert(this->temp_receiver_);
14393 }
14394
14395 return this;
14396}
14397
ea664253 14398// Get the backend representation for a receive expression.
e440a328 14399
ea664253 14400Bexpression*
14401Receive_expression::do_get_backend(Translate_context* context)
e440a328 14402{
f24f10bb 14403 Location loc = this->location();
14404
e440a328 14405 Channel_type* channel_type = this->channel_->type()->channel_type();
5b8368f4 14406 if (channel_type == NULL)
14407 {
c484d925 14408 go_assert(this->channel_->type()->is_error());
ea664253 14409 return context->backend()->error_expression();
5b8368f4 14410 }
e440a328 14411
2c809f8f 14412 Expression* recv_ref =
14413 Expression::make_temporary_reference(this->temp_receiver_, loc);
14414 Expression* recv_addr =
14415 Expression::make_temporary_reference(this->temp_receiver_, loc);
14416 recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc);
e36a5ff5 14417 Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 2,
14418 this->channel_, recv_addr);
ea664253 14419 return Expression::make_compound(recv, recv_ref, loc)->get_backend(context);
e440a328 14420}
14421
d751bb78 14422// Dump ast representation for a receive expression.
14423
14424void
14425Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
14426{
14427 ast_dump_context->ostream() << " <- " ;
14428 ast_dump_context->dump_expression(channel_);
14429}
14430
e440a328 14431// Make a receive expression.
14432
14433Receive_expression*
b13c66cd 14434Expression::make_receive(Expression* channel, Location location)
e440a328 14435{
14436 return new Receive_expression(channel, location);
14437}
14438
e440a328 14439// An expression which evaluates to a pointer to the type descriptor
14440// of a type.
14441
14442class Type_descriptor_expression : public Expression
14443{
14444 public:
b13c66cd 14445 Type_descriptor_expression(Type* type, Location location)
e440a328 14446 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
14447 type_(type)
14448 { }
14449
14450 protected:
4b686186 14451 int
14452 do_traverse(Traverse*);
14453
e440a328 14454 Type*
14455 do_type()
14456 { return Type::make_type_descriptor_ptr_type(); }
14457
f9ca30f9 14458 bool
3ae06f68 14459 do_is_static_initializer() const
f9ca30f9 14460 { return true; }
14461
e440a328 14462 void
14463 do_determine_type(const Type_context*)
14464 { }
14465
14466 Expression*
14467 do_copy()
14468 { return this; }
14469
ea664253 14470 Bexpression*
14471 do_get_backend(Translate_context* context)
a1d23b41 14472 {
ea664253 14473 return this->type_->type_descriptor_pointer(context->gogo(),
14474 this->location());
a1d23b41 14475 }
e440a328 14476
d751bb78 14477 void
14478 do_dump_expression(Ast_dump_context*) const;
14479
e440a328 14480 private:
14481 // The type for which this is the descriptor.
14482 Type* type_;
14483};
14484
4b686186 14485int
14486Type_descriptor_expression::do_traverse(Traverse* traverse)
14487{
14488 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
14489 return TRAVERSE_EXIT;
14490 return TRAVERSE_CONTINUE;
14491}
14492
d751bb78 14493// Dump ast representation for a type descriptor expression.
14494
14495void
14496Type_descriptor_expression::do_dump_expression(
14497 Ast_dump_context* ast_dump_context) const
14498{
14499 ast_dump_context->dump_type(this->type_);
14500}
14501
e440a328 14502// Make a type descriptor expression.
14503
14504Expression*
b13c66cd 14505Expression::make_type_descriptor(Type* type, Location location)
e440a328 14506{
14507 return new Type_descriptor_expression(type, location);
14508}
14509
aa5ae575 14510// An expression which evaluates to a pointer to the Garbage Collection symbol
14511// of a type.
14512
14513class GC_symbol_expression : public Expression
14514{
14515 public:
14516 GC_symbol_expression(Type* type)
14517 : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()),
14518 type_(type)
14519 {}
14520
14521 protected:
14522 Type*
14523 do_type()
03118c21 14524 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
aa5ae575 14525
14526 bool
3ae06f68 14527 do_is_static_initializer() const
aa5ae575 14528 { return true; }
14529
14530 void
14531 do_determine_type(const Type_context*)
14532 { }
14533
14534 Expression*
14535 do_copy()
14536 { return this; }
14537
14538 Bexpression*
14539 do_get_backend(Translate_context* context)
14540 { return this->type_->gc_symbol_pointer(context->gogo()); }
14541
14542 void
14543 do_dump_expression(Ast_dump_context*) const;
14544
14545 private:
14546 // The type which this gc symbol describes.
14547 Type* type_;
14548};
14549
14550// Dump ast representation for a gc symbol expression.
14551
14552void
14553GC_symbol_expression::do_dump_expression(
14554 Ast_dump_context* ast_dump_context) const
14555{
14556 ast_dump_context->ostream() << "gcdata(";
14557 ast_dump_context->dump_type(this->type_);
14558 ast_dump_context->ostream() << ")";
14559}
14560
14561// Make a gc symbol expression.
14562
14563Expression*
14564Expression::make_gc_symbol(Type* type)
14565{
14566 return new GC_symbol_expression(type);
14567}
14568
03118c21 14569// An expression that evaluates to a pointer to a symbol holding the
14570// ptrmask data of a type.
14571
14572class Ptrmask_symbol_expression : public Expression
14573{
14574 public:
14575 Ptrmask_symbol_expression(Type* type)
14576 : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()),
14577 type_(type)
14578 {}
14579
14580 protected:
14581 Type*
14582 do_type()
14583 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
14584
14585 bool
14586 do_is_static_initializer() const
14587 { return true; }
14588
14589 void
14590 do_determine_type(const Type_context*)
14591 { }
14592
14593 Expression*
14594 do_copy()
14595 { return this; }
14596
14597 Bexpression*
14598 do_get_backend(Translate_context*);
14599
14600 void
14601 do_dump_expression(Ast_dump_context*) const;
14602
14603 private:
14604 // The type that this ptrmask symbol describes.
14605 Type* type_;
14606};
14607
14608// Return the ptrmask variable.
14609
14610Bexpression*
14611Ptrmask_symbol_expression::do_get_backend(Translate_context* context)
14612{
14613 Gogo* gogo = context->gogo();
14614
14615 // If this type does not need a gcprog, then we can use the standard
14616 // GC symbol.
14617 int64_t ptrsize, ptrdata;
14618 if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata))
14619 return this->type_->gc_symbol_pointer(gogo);
14620
14621 // Otherwise we have to build a ptrmask variable, and return a
14622 // pointer to it.
14623
14624 Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata);
14625 Location bloc = Linemap::predeclared_location();
7af8e400 14626 Bexpression* bref = gogo->backend()->var_expression(bvar, bloc);
03118c21 14627 Bexpression* baddr = gogo->backend()->address_expression(bref, bloc);
14628
14629 Type* uint8_type = Type::lookup_integer_type("uint8");
14630 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
14631 Btype* ubtype = pointer_uint8_type->get_backend(gogo);
14632 return gogo->backend()->convert_expression(ubtype, baddr, bloc);
14633}
14634
14635// Dump AST for a ptrmask symbol expression.
14636
14637void
14638Ptrmask_symbol_expression::do_dump_expression(
14639 Ast_dump_context* ast_dump_context) const
14640{
14641 ast_dump_context->ostream() << "ptrmask(";
14642 ast_dump_context->dump_type(this->type_);
14643 ast_dump_context->ostream() << ")";
14644}
14645
14646// Make a ptrmask symbol expression.
14647
14648Expression*
14649Expression::make_ptrmask_symbol(Type* type)
14650{
14651 return new Ptrmask_symbol_expression(type);
14652}
14653
e440a328 14654// An expression which evaluates to some characteristic of a type.
14655// This is only used to initialize fields of a type descriptor. Using
14656// a new expression class is slightly inefficient but gives us a good
14657// separation between the frontend and the middle-end with regard to
14658// how types are laid out.
14659
14660class Type_info_expression : public Expression
14661{
14662 public:
14663 Type_info_expression(Type* type, Type_info type_info)
b13c66cd 14664 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
e440a328 14665 type_(type), type_info_(type_info)
14666 { }
14667
14668 protected:
0e168074 14669 bool
3ae06f68 14670 do_is_static_initializer() const
0e168074 14671 { return true; }
14672
e440a328 14673 Type*
14674 do_type();
14675
14676 void
14677 do_determine_type(const Type_context*)
14678 { }
14679
14680 Expression*
14681 do_copy()
14682 { return this; }
14683
ea664253 14684 Bexpression*
14685 do_get_backend(Translate_context* context);
e440a328 14686
d751bb78 14687 void
14688 do_dump_expression(Ast_dump_context*) const;
14689
e440a328 14690 private:
14691 // The type for which we are getting information.
14692 Type* type_;
14693 // What information we want.
14694 Type_info type_info_;
14695};
14696
14697// The type is chosen to match what the type descriptor struct
14698// expects.
14699
14700Type*
14701Type_info_expression::do_type()
14702{
14703 switch (this->type_info_)
14704 {
14705 case TYPE_INFO_SIZE:
03118c21 14706 case TYPE_INFO_BACKEND_PTRDATA:
14707 case TYPE_INFO_DESCRIPTOR_PTRDATA:
e440a328 14708 return Type::lookup_integer_type("uintptr");
14709 case TYPE_INFO_ALIGNMENT:
14710 case TYPE_INFO_FIELD_ALIGNMENT:
14711 return Type::lookup_integer_type("uint8");
14712 default:
c3e6f413 14713 go_unreachable();
e440a328 14714 }
14715}
14716
ea664253 14717// Return the backend representation for type information.
e440a328 14718
ea664253 14719Bexpression*
14720Type_info_expression::do_get_backend(Translate_context* context)
e440a328 14721{
927a01eb 14722 Gogo* gogo = context->gogo();
2a305b85 14723 bool ok = true;
3f378015 14724 int64_t val;
927a01eb 14725 switch (this->type_info_)
e440a328 14726 {
927a01eb 14727 case TYPE_INFO_SIZE:
2a305b85 14728 ok = this->type_->backend_type_size(gogo, &val);
927a01eb 14729 break;
14730 case TYPE_INFO_ALIGNMENT:
2a305b85 14731 ok = this->type_->backend_type_align(gogo, &val);
927a01eb 14732 break;
14733 case TYPE_INFO_FIELD_ALIGNMENT:
2a305b85 14734 ok = this->type_->backend_type_field_align(gogo, &val);
927a01eb 14735 break;
03118c21 14736 case TYPE_INFO_BACKEND_PTRDATA:
14737 ok = this->type_->backend_type_ptrdata(gogo, &val);
14738 break;
14739 case TYPE_INFO_DESCRIPTOR_PTRDATA:
14740 ok = this->type_->descriptor_ptrdata(gogo, &val);
14741 break;
927a01eb 14742 default:
14743 go_unreachable();
e440a328 14744 }
2a305b85 14745 if (!ok)
14746 {
14747 go_assert(saw_errors());
14748 return gogo->backend()->error_expression();
14749 }
3f378015 14750 Expression* e = Expression::make_integer_int64(val, this->type(),
14751 this->location());
14752 return e->get_backend(context);
e440a328 14753}
14754
d751bb78 14755// Dump ast representation for a type info expression.
14756
14757void
14758Type_info_expression::do_dump_expression(
14759 Ast_dump_context* ast_dump_context) const
14760{
14761 ast_dump_context->ostream() << "typeinfo(";
14762 ast_dump_context->dump_type(this->type_);
14763 ast_dump_context->ostream() << ",";
14764 ast_dump_context->ostream() <<
14765 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
14766 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
03118c21 14767 : this->type_info_ == TYPE_INFO_SIZE ? "size"
14768 : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata"
14769 : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata"
d751bb78 14770 : "unknown");
14771 ast_dump_context->ostream() << ")";
14772}
14773
e440a328 14774// Make a type info expression.
14775
14776Expression*
14777Expression::make_type_info(Type* type, Type_info type_info)
14778{
14779 return new Type_info_expression(type, type_info);
14780}
14781
35a54f17 14782// An expression that evaluates to some characteristic of a slice.
14783// This is used when indexing, bound-checking, or nil checking a slice.
14784
14785class Slice_info_expression : public Expression
14786{
14787 public:
14788 Slice_info_expression(Expression* slice, Slice_info slice_info,
14789 Location location)
14790 : Expression(EXPRESSION_SLICE_INFO, location),
14791 slice_(slice), slice_info_(slice_info)
14792 { }
14793
14794 protected:
14795 Type*
14796 do_type();
14797
14798 void
14799 do_determine_type(const Type_context*)
14800 { }
14801
14802 Expression*
14803 do_copy()
14804 {
14805 return new Slice_info_expression(this->slice_->copy(), this->slice_info_,
14806 this->location());
14807 }
14808
ea664253 14809 Bexpression*
14810 do_get_backend(Translate_context* context);
35a54f17 14811
14812 void
14813 do_dump_expression(Ast_dump_context*) const;
14814
14815 void
14816 do_issue_nil_check()
14817 { this->slice_->issue_nil_check(); }
14818
14819 private:
14820 // The slice for which we are getting information.
14821 Expression* slice_;
14822 // What information we want.
14823 Slice_info slice_info_;
14824};
14825
14826// Return the type of the slice info.
14827
14828Type*
14829Slice_info_expression::do_type()
14830{
14831 switch (this->slice_info_)
14832 {
14833 case SLICE_INFO_VALUE_POINTER:
14834 return Type::make_pointer_type(
14835 this->slice_->type()->array_type()->element_type());
14836 case SLICE_INFO_LENGTH:
14837 case SLICE_INFO_CAPACITY:
14838 return Type::lookup_integer_type("int");
14839 default:
14840 go_unreachable();
14841 }
14842}
14843
ea664253 14844// Return the backend information for slice information.
35a54f17 14845
ea664253 14846Bexpression*
14847Slice_info_expression::do_get_backend(Translate_context* context)
35a54f17 14848{
14849 Gogo* gogo = context->gogo();
ea664253 14850 Bexpression* bslice = this->slice_->get_backend(context);
35a54f17 14851 switch (this->slice_info_)
14852 {
14853 case SLICE_INFO_VALUE_POINTER:
14854 case SLICE_INFO_LENGTH:
14855 case SLICE_INFO_CAPACITY:
ea664253 14856 return gogo->backend()->struct_field_expression(bslice, this->slice_info_,
14857 this->location());
35a54f17 14858 break;
14859 default:
14860 go_unreachable();
14861 }
35a54f17 14862}
14863
14864// Dump ast representation for a type info expression.
14865
14866void
14867Slice_info_expression::do_dump_expression(
14868 Ast_dump_context* ast_dump_context) const
14869{
14870 ast_dump_context->ostream() << "sliceinfo(";
14871 this->slice_->dump_expression(ast_dump_context);
14872 ast_dump_context->ostream() << ",";
14873 ast_dump_context->ostream() <<
14874 (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values"
14875 : this->slice_info_ == SLICE_INFO_LENGTH ? "length"
14876 : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity "
14877 : "unknown");
14878 ast_dump_context->ostream() << ")";
14879}
14880
14881// Make a slice info expression.
14882
14883Expression*
14884Expression::make_slice_info(Expression* slice, Slice_info slice_info,
14885 Location location)
14886{
14887 return new Slice_info_expression(slice, slice_info, location);
14888}
14889
2c809f8f 14890// An expression that represents a slice value: a struct with value pointer,
14891// length, and capacity fields.
14892
14893class Slice_value_expression : public Expression
14894{
14895 public:
14896 Slice_value_expression(Type* type, Expression* valptr, Expression* len,
14897 Expression* cap, Location location)
14898 : Expression(EXPRESSION_SLICE_VALUE, location),
14899 type_(type), valptr_(valptr), len_(len), cap_(cap)
14900 { }
14901
14902 protected:
14903 int
14904 do_traverse(Traverse*);
14905
14906 Type*
14907 do_type()
14908 { return this->type_; }
14909
14910 void
14911 do_determine_type(const Type_context*)
14912 { go_unreachable(); }
14913
14914 Expression*
14915 do_copy()
14916 {
14917 return new Slice_value_expression(this->type_, this->valptr_->copy(),
14918 this->len_->copy(), this->cap_->copy(),
14919 this->location());
14920 }
14921
ea664253 14922 Bexpression*
14923 do_get_backend(Translate_context* context);
2c809f8f 14924
14925 void
14926 do_dump_expression(Ast_dump_context*) const;
14927
14928 private:
14929 // The type of the slice value.
14930 Type* type_;
14931 // The pointer to the values in the slice.
14932 Expression* valptr_;
14933 // The length of the slice.
14934 Expression* len_;
14935 // The capacity of the slice.
14936 Expression* cap_;
14937};
14938
14939int
14940Slice_value_expression::do_traverse(Traverse* traverse)
14941{
55e8ba6a 14942 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT
14943 || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT
2c809f8f 14944 || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT
14945 || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
14946 return TRAVERSE_EXIT;
14947 return TRAVERSE_CONTINUE;
14948}
14949
ea664253 14950Bexpression*
14951Slice_value_expression::do_get_backend(Translate_context* context)
2c809f8f 14952{
14953 std::vector<Bexpression*> vals(3);
ea664253 14954 vals[0] = this->valptr_->get_backend(context);
14955 vals[1] = this->len_->get_backend(context);
14956 vals[2] = this->cap_->get_backend(context);
2c809f8f 14957
14958 Gogo* gogo = context->gogo();
14959 Btype* btype = this->type_->get_backend(gogo);
ea664253 14960 return gogo->backend()->constructor_expression(btype, vals, this->location());
2c809f8f 14961}
14962
14963void
14964Slice_value_expression::do_dump_expression(
14965 Ast_dump_context* ast_dump_context) const
14966{
14967 ast_dump_context->ostream() << "slicevalue(";
14968 ast_dump_context->ostream() << "values: ";
14969 this->valptr_->dump_expression(ast_dump_context);
14970 ast_dump_context->ostream() << ", length: ";
14971 this->len_->dump_expression(ast_dump_context);
14972 ast_dump_context->ostream() << ", capacity: ";
14973 this->cap_->dump_expression(ast_dump_context);
14974 ast_dump_context->ostream() << ")";
14975}
14976
14977Expression*
14978Expression::make_slice_value(Type* at, Expression* valptr, Expression* len,
14979 Expression* cap, Location location)
14980{
14981 go_assert(at->is_slice_type());
14982 return new Slice_value_expression(at, valptr, len, cap, location);
14983}
2387f644 14984
14985// An expression that evaluates to some characteristic of a non-empty interface.
14986// This is used to access the method table or underlying object of an interface.
14987
14988class Interface_info_expression : public Expression
14989{
14990 public:
14991 Interface_info_expression(Expression* iface, Interface_info iface_info,
2c809f8f 14992 Location location)
2387f644 14993 : Expression(EXPRESSION_INTERFACE_INFO, location),
14994 iface_(iface), iface_info_(iface_info)
14995 { }
14996
14997 protected:
14998 Type*
14999 do_type();
15000
15001 void
15002 do_determine_type(const Type_context*)
15003 { }
15004
15005 Expression*
15006 do_copy()
15007 {
15008 return new Interface_info_expression(this->iface_->copy(),
15009 this->iface_info_, this->location());
15010 }
15011
ea664253 15012 Bexpression*
15013 do_get_backend(Translate_context* context);
2387f644 15014
15015 void
15016 do_dump_expression(Ast_dump_context*) const;
15017
15018 void
15019 do_issue_nil_check()
15020 { this->iface_->issue_nil_check(); }
15021
15022 private:
15023 // The interface for which we are getting information.
15024 Expression* iface_;
15025 // What information we want.
15026 Interface_info iface_info_;
15027};
15028
15029// Return the type of the interface info.
15030
15031Type*
15032Interface_info_expression::do_type()
15033{
15034 switch (this->iface_info_)
15035 {
15036 case INTERFACE_INFO_METHODS:
15037 {
625d3118 15038 typedef Unordered_map(Interface_type*, Type*) Hashtable;
15039 static Hashtable result_types;
15040
15041 Interface_type* itype = this->iface_->type()->interface_type();
15042
15043 Hashtable::const_iterator p = result_types.find(itype);
15044 if (p != result_types.end())
15045 return p->second;
15046
2c809f8f 15047 Type* pdt = Type::make_type_descriptor_ptr_type();
625d3118 15048 if (itype->is_empty())
15049 {
15050 result_types[itype] = pdt;
15051 return pdt;
15052 }
2c809f8f 15053
2387f644 15054 Location loc = this->location();
15055 Struct_field_list* sfl = new Struct_field_list();
2387f644 15056 sfl->push_back(
15057 Struct_field(Typed_identifier("__type_descriptor", pdt, loc)));
15058
2387f644 15059 for (Typed_identifier_list::const_iterator p = itype->methods()->begin();
15060 p != itype->methods()->end();
15061 ++p)
15062 {
15063 Function_type* ft = p->type()->function_type();
15064 go_assert(ft->receiver() == NULL);
15065
15066 const Typed_identifier_list* params = ft->parameters();
15067 Typed_identifier_list* mparams = new Typed_identifier_list();
15068 if (params != NULL)
15069 mparams->reserve(params->size() + 1);
15070 Type* vt = Type::make_pointer_type(Type::make_void_type());
15071 mparams->push_back(Typed_identifier("", vt, ft->location()));
15072 if (params != NULL)
15073 {
15074 for (Typed_identifier_list::const_iterator pp = params->begin();
15075 pp != params->end();
15076 ++pp)
15077 mparams->push_back(*pp);
15078 }
15079
15080 Typed_identifier_list* mresults = (ft->results() == NULL
15081 ? NULL
15082 : ft->results()->copy());
15083 Backend_function_type* mft =
15084 Type::make_backend_function_type(NULL, mparams, mresults,
15085 ft->location());
15086
15087 std::string fname = Gogo::unpack_hidden_name(p->name());
15088 sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc)));
15089 }
15090
6bf4793c 15091 Struct_type* st = Type::make_struct_type(sfl, loc);
15092 st->set_is_struct_incomparable();
15093 Pointer_type *pt = Type::make_pointer_type(st);
625d3118 15094 result_types[itype] = pt;
15095 return pt;
2387f644 15096 }
15097 case INTERFACE_INFO_OBJECT:
15098 return Type::make_pointer_type(Type::make_void_type());
15099 default:
15100 go_unreachable();
15101 }
15102}
15103
ea664253 15104// Return the backend representation for interface information.
2387f644 15105
ea664253 15106Bexpression*
15107Interface_info_expression::do_get_backend(Translate_context* context)
2387f644 15108{
15109 Gogo* gogo = context->gogo();
ea664253 15110 Bexpression* biface = this->iface_->get_backend(context);
2387f644 15111 switch (this->iface_info_)
15112 {
15113 case INTERFACE_INFO_METHODS:
15114 case INTERFACE_INFO_OBJECT:
ea664253 15115 return gogo->backend()->struct_field_expression(biface, this->iface_info_,
15116 this->location());
2387f644 15117 break;
15118 default:
15119 go_unreachable();
15120 }
2387f644 15121}
15122
15123// Dump ast representation for an interface info expression.
15124
15125void
15126Interface_info_expression::do_dump_expression(
15127 Ast_dump_context* ast_dump_context) const
15128{
2c809f8f 15129 bool is_empty = this->iface_->type()->interface_type()->is_empty();
2387f644 15130 ast_dump_context->ostream() << "interfaceinfo(";
15131 this->iface_->dump_expression(ast_dump_context);
15132 ast_dump_context->ostream() << ",";
15133 ast_dump_context->ostream() <<
2c809f8f 15134 (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods"
15135 : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor"
2387f644 15136 : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object"
15137 : "unknown");
15138 ast_dump_context->ostream() << ")";
15139}
15140
15141// Make an interface info expression.
15142
15143Expression*
15144Expression::make_interface_info(Expression* iface, Interface_info iface_info,
15145 Location location)
15146{
15147 return new Interface_info_expression(iface, iface_info, location);
15148}
15149
2c809f8f 15150// An expression that represents an interface value. The first field is either
15151// a type descriptor for an empty interface or a pointer to the interface method
15152// table for a non-empty interface. The second field is always the object.
15153
15154class Interface_value_expression : public Expression
15155{
15156 public:
15157 Interface_value_expression(Type* type, Expression* first_field,
15158 Expression* obj, Location location)
15159 : Expression(EXPRESSION_INTERFACE_VALUE, location),
15160 type_(type), first_field_(first_field), obj_(obj)
15161 { }
15162
15163 protected:
15164 int
15165 do_traverse(Traverse*);
15166
15167 Type*
15168 do_type()
15169 { return this->type_; }
15170
15171 void
15172 do_determine_type(const Type_context*)
15173 { go_unreachable(); }
15174
15175 Expression*
15176 do_copy()
15177 {
15178 return new Interface_value_expression(this->type_,
15179 this->first_field_->copy(),
15180 this->obj_->copy(), this->location());
15181 }
15182
ea664253 15183 Bexpression*
15184 do_get_backend(Translate_context* context);
2c809f8f 15185
15186 void
15187 do_dump_expression(Ast_dump_context*) const;
15188
15189 private:
15190 // The type of the interface value.
15191 Type* type_;
15192 // The first field of the interface (either a type descriptor or a pointer
15193 // to the method table.
15194 Expression* first_field_;
15195 // The underlying object of the interface.
15196 Expression* obj_;
15197};
15198
15199int
15200Interface_value_expression::do_traverse(Traverse* traverse)
15201{
15202 if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT
15203 || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT)
15204 return TRAVERSE_EXIT;
15205 return TRAVERSE_CONTINUE;
15206}
15207
ea664253 15208Bexpression*
15209Interface_value_expression::do_get_backend(Translate_context* context)
2c809f8f 15210{
15211 std::vector<Bexpression*> vals(2);
ea664253 15212 vals[0] = this->first_field_->get_backend(context);
15213 vals[1] = this->obj_->get_backend(context);
2c809f8f 15214
15215 Gogo* gogo = context->gogo();
15216 Btype* btype = this->type_->get_backend(gogo);
ea664253 15217 return gogo->backend()->constructor_expression(btype, vals, this->location());
2c809f8f 15218}
15219
15220void
15221Interface_value_expression::do_dump_expression(
15222 Ast_dump_context* ast_dump_context) const
15223{
15224 ast_dump_context->ostream() << "interfacevalue(";
15225 ast_dump_context->ostream() <<
15226 (this->type_->interface_type()->is_empty()
15227 ? "type_descriptor: "
15228 : "methods: ");
15229 this->first_field_->dump_expression(ast_dump_context);
15230 ast_dump_context->ostream() << ", object: ";
15231 this->obj_->dump_expression(ast_dump_context);
15232 ast_dump_context->ostream() << ")";
15233}
15234
15235Expression*
15236Expression::make_interface_value(Type* type, Expression* first_value,
15237 Expression* object, Location location)
15238{
15239 return new Interface_value_expression(type, first_value, object, location);
15240}
15241
15242// An interface method table for a pair of types: an interface type and a type
15243// that implements that interface.
15244
15245class Interface_mtable_expression : public Expression
15246{
15247 public:
15248 Interface_mtable_expression(Interface_type* itype, Type* type,
15249 bool is_pointer, Location location)
15250 : Expression(EXPRESSION_INTERFACE_MTABLE, location),
15251 itype_(itype), type_(type), is_pointer_(is_pointer),
15252 method_table_type_(NULL), bvar_(NULL)
15253 { }
15254
15255 protected:
15256 int
15257 do_traverse(Traverse*);
15258
15259 Type*
15260 do_type();
15261
15262 bool
3ae06f68 15263 do_is_static_initializer() const
2c809f8f 15264 { return true; }
15265
15266 void
15267 do_determine_type(const Type_context*)
15268 { go_unreachable(); }
15269
15270 Expression*
15271 do_copy()
15272 {
15273 return new Interface_mtable_expression(this->itype_, this->type_,
15274 this->is_pointer_, this->location());
15275 }
15276
15277 bool
15278 do_is_addressable() const
15279 { return true; }
15280
ea664253 15281 Bexpression*
15282 do_get_backend(Translate_context* context);
2c809f8f 15283
15284 void
15285 do_dump_expression(Ast_dump_context*) const;
15286
15287 private:
15288 // The interface type for which the methods are defined.
15289 Interface_type* itype_;
15290 // The type to construct the interface method table for.
15291 Type* type_;
15292 // Whether this table contains the method set for the receiver type or the
15293 // pointer receiver type.
15294 bool is_pointer_;
15295 // The type of the method table.
15296 Type* method_table_type_;
15297 // The backend variable that refers to the interface method table.
15298 Bvariable* bvar_;
15299};
15300
15301int
15302Interface_mtable_expression::do_traverse(Traverse* traverse)
15303{
15304 if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT
15305 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
15306 return TRAVERSE_EXIT;
15307 return TRAVERSE_CONTINUE;
15308}
15309
15310Type*
15311Interface_mtable_expression::do_type()
15312{
15313 if (this->method_table_type_ != NULL)
15314 return this->method_table_type_;
15315
15316 const Typed_identifier_list* interface_methods = this->itype_->methods();
15317 go_assert(!interface_methods->empty());
15318
15319 Struct_field_list* sfl = new Struct_field_list;
15320 Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
15321 this->location());
15322 sfl->push_back(Struct_field(tid));
db122cb9 15323 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
2c809f8f 15324 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
15325 p != interface_methods->end();
15326 ++p)
db122cb9 15327 {
15328 // We want C function pointers here, not func descriptors; model
15329 // using void* pointers.
15330 Typed_identifier method(p->name(), unsafe_ptr_type, p->location());
15331 sfl->push_back(Struct_field(method));
15332 }
6bf4793c 15333 Struct_type* st = Type::make_struct_type(sfl, this->location());
15334 st->set_is_struct_incomparable();
15335 this->method_table_type_ = st;
2c809f8f 15336 return this->method_table_type_;
15337}
15338
ea664253 15339Bexpression*
15340Interface_mtable_expression::do_get_backend(Translate_context* context)
2c809f8f 15341{
15342 Gogo* gogo = context->gogo();
2c809f8f 15343 Location loc = Linemap::predeclared_location();
15344 if (this->bvar_ != NULL)
7af8e400 15345 return gogo->backend()->var_expression(this->bvar_, this->location());
2c809f8f 15346
15347 const Typed_identifier_list* interface_methods = this->itype_->methods();
15348 go_assert(!interface_methods->empty());
15349
19272321 15350 std::string mangled_name =
15351 gogo->interface_method_table_name(this->itype_, this->type_,
15352 this->is_pointer_);
2c809f8f 15353
1530c754 15354 // Set is_public if we are converting a named type to an interface
15355 // type that is defined in the same package as the named type, and
15356 // the interface has hidden methods. In that case the interface
15357 // method table will be defined by the package that defines the
15358 // types.
15359 bool is_public = false;
15360 if (this->type_->named_type() != NULL
15361 && (this->type_->named_type()->named_object()->package()
15362 == this->itype_->package()))
15363 {
15364 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
15365 p != interface_methods->end();
15366 ++p)
2c809f8f 15367 {
1530c754 15368 if (Gogo::is_hidden_name(p->name()))
15369 {
15370 is_public = true;
15371 break;
15372 }
2c809f8f 15373 }
15374 }
15375
1530c754 15376 if (is_public
2c809f8f 15377 && this->type_->named_type()->named_object()->package() != NULL)
15378 {
1530c754 15379 // The interface conversion table is defined elsewhere.
2c809f8f 15380 Btype* btype = this->type()->get_backend(gogo);
438b4bec 15381 std::string asm_name(go_selectively_encode_id(mangled_name));
2c809f8f 15382 this->bvar_ =
438b4bec 15383 gogo->backend()->immutable_struct_reference(mangled_name, asm_name,
15384 btype, loc);
7af8e400 15385 return gogo->backend()->var_expression(this->bvar_, this->location());
2c809f8f 15386 }
15387
15388 // The first element is the type descriptor.
15389 Type* td_type;
15390 if (!this->is_pointer_)
15391 td_type = this->type_;
15392 else
15393 td_type = Type::make_pointer_type(this->type_);
15394
db122cb9 15395 std::vector<Backend::Btyped_identifier> bstructfields;
15396
2c809f8f 15397 // Build an interface method table for a type: a type descriptor followed by a
15398 // list of function pointers, one for each interface method. This is used for
15399 // interfaces.
15400 Expression_list* svals = new Expression_list();
db122cb9 15401 Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc);
15402 svals->push_back(tdescriptor);
15403
15404 Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo);
15405 Backend::Btyped_identifier btd("_type", tdesc_btype, loc);
15406 bstructfields.push_back(btd);
2c809f8f 15407
15408 Named_type* nt = this->type_->named_type();
15409 Struct_type* st = this->type_->struct_type();
15410 go_assert(nt != NULL || st != NULL);
15411
15412 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
15413 p != interface_methods->end();
15414 ++p)
15415 {
15416 bool is_ambiguous;
15417 Method* m;
15418 if (nt != NULL)
15419 m = nt->method_function(p->name(), &is_ambiguous);
15420 else
15421 m = st->method_function(p->name(), &is_ambiguous);
15422 go_assert(m != NULL);
15423 Named_object* no = m->named_object();
15424
15425 go_assert(no->is_function() || no->is_function_declaration());
db122cb9 15426
15427 Btype* fcn_btype = m->type()->get_backend_fntype(gogo);
15428 Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc);
15429 bstructfields.push_back(bmtype);
15430
2c809f8f 15431 svals->push_back(Expression::make_func_code_reference(no, loc));
15432 }
15433
db122cb9 15434 Btype *btype = gogo->backend()->struct_type(bstructfields);
15435 std::vector<Bexpression*> ctor_bexprs;
15436 for (Expression_list::const_iterator pe = svals->begin();
15437 pe != svals->end();
15438 ++pe)
15439 {
15440 ctor_bexprs.push_back((*pe)->get_backend(context));
15441 }
15442 Bexpression* ctor =
15443 gogo->backend()->constructor_expression(btype, ctor_bexprs, loc);
2c809f8f 15444
438b4bec 15445 std::string asm_name(go_selectively_encode_id(mangled_name));
15446 this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false,
2c809f8f 15447 !is_public, btype, loc);
15448 gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false,
15449 !is_public, btype, loc, ctor);
7af8e400 15450 return gogo->backend()->var_expression(this->bvar_, loc);
2c809f8f 15451}
15452
15453void
15454Interface_mtable_expression::do_dump_expression(
15455 Ast_dump_context* ast_dump_context) const
15456{
15457 ast_dump_context->ostream() << "__go_"
15458 << (this->is_pointer_ ? "pimt__" : "imt_");
15459 ast_dump_context->dump_type(this->itype_);
15460 ast_dump_context->ostream() << "__";
15461 ast_dump_context->dump_type(this->type_);
15462}
15463
15464Expression*
15465Expression::make_interface_mtable_ref(Interface_type* itype, Type* type,
15466 bool is_pointer, Location location)
15467{
15468 return new Interface_mtable_expression(itype, type, is_pointer, location);
15469}
15470
e440a328 15471// An expression which evaluates to the offset of a field within a
15472// struct. This, like Type_info_expression, q.v., is only used to
15473// initialize fields of a type descriptor.
15474
15475class Struct_field_offset_expression : public Expression
15476{
15477 public:
15478 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
b13c66cd 15479 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
15480 Linemap::predeclared_location()),
e440a328 15481 type_(type), field_(field)
15482 { }
15483
15484 protected:
f23d7786 15485 bool
3ae06f68 15486 do_is_static_initializer() const
f23d7786 15487 { return true; }
15488
e440a328 15489 Type*
15490 do_type()
15491 { return Type::lookup_integer_type("uintptr"); }
15492
15493 void
15494 do_determine_type(const Type_context*)
15495 { }
15496
15497 Expression*
15498 do_copy()
15499 { return this; }
15500
ea664253 15501 Bexpression*
15502 do_get_backend(Translate_context* context);
e440a328 15503
d751bb78 15504 void
15505 do_dump_expression(Ast_dump_context*) const;
15506
e440a328 15507 private:
15508 // The type of the struct.
15509 Struct_type* type_;
15510 // The field.
15511 const Struct_field* field_;
15512};
15513
ea664253 15514// Return the backend representation for a struct field offset.
e440a328 15515
ea664253 15516Bexpression*
15517Struct_field_offset_expression::do_get_backend(Translate_context* context)
e440a328 15518{
e440a328 15519 const Struct_field_list* fields = this->type_->fields();
e440a328 15520 Struct_field_list::const_iterator p;
2c8bda43 15521 unsigned i = 0;
e440a328 15522 for (p = fields->begin();
15523 p != fields->end();
2c8bda43 15524 ++p, ++i)
15525 if (&*p == this->field_)
15526 break;
c484d925 15527 go_assert(&*p == this->field_);
e440a328 15528
2c8bda43 15529 Gogo* gogo = context->gogo();
15530 Btype* btype = this->type_->get_backend(gogo);
15531
3f378015 15532 int64_t offset = gogo->backend()->type_field_offset(btype, i);
2c8bda43 15533 Type* uptr_type = Type::lookup_integer_type("uintptr");
e67508fa 15534 Expression* ret =
3f378015 15535 Expression::make_integer_int64(offset, uptr_type,
15536 Linemap::predeclared_location());
ea664253 15537 return ret->get_backend(context);
e440a328 15538}
15539
d751bb78 15540// Dump ast representation for a struct field offset expression.
15541
15542void
15543Struct_field_offset_expression::do_dump_expression(
15544 Ast_dump_context* ast_dump_context) const
15545{
15546 ast_dump_context->ostream() << "unsafe.Offsetof(";
2d29d278 15547 ast_dump_context->dump_type(this->type_);
15548 ast_dump_context->ostream() << '.';
15549 ast_dump_context->ostream() <<
15550 Gogo::message_name(this->field_->field_name());
d751bb78 15551 ast_dump_context->ostream() << ")";
15552}
15553
e440a328 15554// Make an expression for a struct field offset.
15555
15556Expression*
15557Expression::make_struct_field_offset(Struct_type* type,
15558 const Struct_field* field)
15559{
15560 return new Struct_field_offset_expression(type, field);
15561}
15562
15563// An expression which evaluates to the address of an unnamed label.
15564
15565class Label_addr_expression : public Expression
15566{
15567 public:
b13c66cd 15568 Label_addr_expression(Label* label, Location location)
e440a328 15569 : Expression(EXPRESSION_LABEL_ADDR, location),
15570 label_(label)
15571 { }
15572
15573 protected:
15574 Type*
15575 do_type()
15576 { return Type::make_pointer_type(Type::make_void_type()); }
15577
15578 void
15579 do_determine_type(const Type_context*)
15580 { }
15581
15582 Expression*
15583 do_copy()
15584 { return new Label_addr_expression(this->label_, this->location()); }
15585
ea664253 15586 Bexpression*
15587 do_get_backend(Translate_context* context)
15588 { return this->label_->get_addr(context, this->location()); }
e440a328 15589
d751bb78 15590 void
15591 do_dump_expression(Ast_dump_context* ast_dump_context) const
15592 { ast_dump_context->ostream() << this->label_->name(); }
15593
e440a328 15594 private:
15595 // The label whose address we are taking.
15596 Label* label_;
15597};
15598
15599// Make an expression for the address of an unnamed label.
15600
15601Expression*
b13c66cd 15602Expression::make_label_addr(Label* label, Location location)
e440a328 15603{
15604 return new Label_addr_expression(label, location);
15605}
15606
da244e59 15607// Class Conditional_expression.
283a177b 15608
2c809f8f 15609// Traversal.
15610
15611int
15612Conditional_expression::do_traverse(Traverse* traverse)
15613{
15614 if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT
15615 || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT
15616 || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT)
15617 return TRAVERSE_EXIT;
15618 return TRAVERSE_CONTINUE;
15619}
15620
283a177b 15621// Return the type of the conditional expression.
15622
15623Type*
15624Conditional_expression::do_type()
15625{
15626 Type* result_type = Type::make_void_type();
2c809f8f 15627 if (Type::are_identical(this->then_->type(), this->else_->type(), false,
15628 NULL))
283a177b 15629 result_type = this->then_->type();
15630 else if (this->then_->is_nil_expression()
15631 || this->else_->is_nil_expression())
15632 result_type = (!this->then_->is_nil_expression()
15633 ? this->then_->type()
15634 : this->else_->type());
15635 return result_type;
15636}
15637
2c809f8f 15638// Determine type for a conditional expression.
15639
15640void
15641Conditional_expression::do_determine_type(const Type_context* context)
15642{
15643 this->cond_->determine_type_no_context();
15644 this->then_->determine_type(context);
15645 this->else_->determine_type(context);
15646}
15647
283a177b 15648// Get the backend representation of a conditional expression.
15649
ea664253 15650Bexpression*
15651Conditional_expression::do_get_backend(Translate_context* context)
283a177b 15652{
15653 Gogo* gogo = context->gogo();
15654 Btype* result_btype = this->type()->get_backend(gogo);
ea664253 15655 Bexpression* cond = this->cond_->get_backend(context);
15656 Bexpression* then = this->then_->get_backend(context);
15657 Bexpression* belse = this->else_->get_backend(context);
93715b75 15658 Bfunction* bfn = context->function()->func_value()->get_decl();
15659 return gogo->backend()->conditional_expression(bfn, result_btype, cond, then,
ea664253 15660 belse, this->location());
283a177b 15661}
15662
15663// Dump ast representation of a conditional expression.
15664
15665void
15666Conditional_expression::do_dump_expression(
15667 Ast_dump_context* ast_dump_context) const
15668{
15669 ast_dump_context->ostream() << "(";
15670 ast_dump_context->dump_expression(this->cond_);
15671 ast_dump_context->ostream() << " ? ";
15672 ast_dump_context->dump_expression(this->then_);
15673 ast_dump_context->ostream() << " : ";
15674 ast_dump_context->dump_expression(this->else_);
15675 ast_dump_context->ostream() << ") ";
15676}
15677
15678// Make a conditional expression.
15679
15680Expression*
15681Expression::make_conditional(Expression* cond, Expression* then,
15682 Expression* else_expr, Location location)
15683{
15684 return new Conditional_expression(cond, then, else_expr, location);
15685}
15686
da244e59 15687// Class Compound_expression.
2c809f8f 15688
15689// Traversal.
15690
15691int
15692Compound_expression::do_traverse(Traverse* traverse)
15693{
15694 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT
15695 || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
15696 return TRAVERSE_EXIT;
15697 return TRAVERSE_CONTINUE;
15698}
15699
15700// Return the type of the compound expression.
15701
15702Type*
15703Compound_expression::do_type()
15704{
15705 return this->expr_->type();
15706}
15707
15708// Determine type for a compound expression.
15709
15710void
15711Compound_expression::do_determine_type(const Type_context* context)
15712{
15713 this->init_->determine_type_no_context();
15714 this->expr_->determine_type(context);
15715}
15716
15717// Get the backend representation of a compound expression.
15718
ea664253 15719Bexpression*
15720Compound_expression::do_get_backend(Translate_context* context)
2c809f8f 15721{
15722 Gogo* gogo = context->gogo();
ea664253 15723 Bexpression* binit = this->init_->get_backend(context);
0ab48656 15724 Bfunction* bfunction = context->function()->func_value()->get_decl();
15725 Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction,
15726 binit);
ea664253 15727 Bexpression* bexpr = this->expr_->get_backend(context);
15728 return gogo->backend()->compound_expression(init_stmt, bexpr,
15729 this->location());
2c809f8f 15730}
15731
15732// Dump ast representation of a conditional expression.
15733
15734void
15735Compound_expression::do_dump_expression(
15736 Ast_dump_context* ast_dump_context) const
15737{
15738 ast_dump_context->ostream() << "(";
15739 ast_dump_context->dump_expression(this->init_);
15740 ast_dump_context->ostream() << ",";
15741 ast_dump_context->dump_expression(this->expr_);
15742 ast_dump_context->ostream() << ") ";
15743}
15744
15745// Make a compound expression.
15746
15747Expression*
15748Expression::make_compound(Expression* init, Expression* expr, Location location)
15749{
15750 return new Compound_expression(init, expr, location);
15751}
15752
1b4fb1e0 15753// Class Backend_expression.
15754
15755int
15756Backend_expression::do_traverse(Traverse*)
15757{
15758 return TRAVERSE_CONTINUE;
15759}
15760
15761void
15762Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
15763{
15764 ast_dump_context->ostream() << "backend_expression<";
15765 ast_dump_context->dump_type(this->type_);
15766 ast_dump_context->ostream() << ">";
15767}
15768
15769Expression*
15770Expression::make_backend(Bexpression* bexpr, Type* type, Location location)
15771{
15772 return new Backend_expression(bexpr, type, location);
15773}
15774
e440a328 15775// Import an expression. This comes at the end in order to see the
15776// various class definitions.
15777
15778Expression*
15779Expression::import_expression(Import* imp)
15780{
15781 int c = imp->peek_char();
15782 if (imp->match_c_string("- ")
15783 || imp->match_c_string("! ")
15784 || imp->match_c_string("^ "))
15785 return Unary_expression::do_import(imp);
15786 else if (c == '(')
15787 return Binary_expression::do_import(imp);
15788 else if (imp->match_c_string("true")
15789 || imp->match_c_string("false"))
15790 return Boolean_expression::do_import(imp);
15791 else if (c == '"')
15792 return String_expression::do_import(imp);
15793 else if (c == '-' || (c >= '0' && c <= '9'))
15794 {
15795 // This handles integers, floats and complex constants.
15796 return Integer_expression::do_import(imp);
15797 }
15798 else if (imp->match_c_string("nil"))
15799 return Nil_expression::do_import(imp);
15800 else if (imp->match_c_string("convert"))
15801 return Type_conversion_expression::do_import(imp);
15802 else
15803 {
631d5788 15804 go_error_at(imp->location(), "import error: expected expression");
e440a328 15805 return Expression::make_error(imp->location());
15806 }
15807}
15808
15809// Class Expression_list.
15810
15811// Traverse the list.
15812
15813int
15814Expression_list::traverse(Traverse* traverse)
15815{
15816 for (Expression_list::iterator p = this->begin();
15817 p != this->end();
15818 ++p)
15819 {
15820 if (*p != NULL)
15821 {
15822 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
15823 return TRAVERSE_EXIT;
15824 }
15825 }
15826 return TRAVERSE_CONTINUE;
15827}
15828
15829// Copy the list.
15830
15831Expression_list*
15832Expression_list::copy()
15833{
15834 Expression_list* ret = new Expression_list();
15835 for (Expression_list::iterator p = this->begin();
15836 p != this->end();
15837 ++p)
15838 {
15839 if (*p == NULL)
15840 ret->push_back(NULL);
15841 else
15842 ret->push_back((*p)->copy());
15843 }
15844 return ret;
15845}
15846
15847// Return whether an expression list has an error expression.
15848
15849bool
15850Expression_list::contains_error() const
15851{
15852 for (Expression_list::const_iterator p = this->begin();
15853 p != this->end();
15854 ++p)
15855 if (*p != NULL && (*p)->is_error_expression())
15856 return true;
15857 return false;
15858}
0c77715b 15859
15860// Class Numeric_constant.
15861
15862// Destructor.
15863
15864Numeric_constant::~Numeric_constant()
15865{
15866 this->clear();
15867}
15868
15869// Copy constructor.
15870
15871Numeric_constant::Numeric_constant(const Numeric_constant& a)
15872 : classification_(a.classification_), type_(a.type_)
15873{
15874 switch (a.classification_)
15875 {
15876 case NC_INVALID:
15877 break;
15878 case NC_INT:
15879 case NC_RUNE:
15880 mpz_init_set(this->u_.int_val, a.u_.int_val);
15881 break;
15882 case NC_FLOAT:
15883 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
15884 break;
15885 case NC_COMPLEX:
fcbea5e4 15886 mpc_init2(this->u_.complex_val, mpc_precision);
15887 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
0c77715b 15888 break;
15889 default:
15890 go_unreachable();
15891 }
15892}
15893
15894// Assignment operator.
15895
15896Numeric_constant&
15897Numeric_constant::operator=(const Numeric_constant& a)
15898{
15899 this->clear();
15900 this->classification_ = a.classification_;
15901 this->type_ = a.type_;
15902 switch (a.classification_)
15903 {
15904 case NC_INVALID:
15905 break;
15906 case NC_INT:
15907 case NC_RUNE:
15908 mpz_init_set(this->u_.int_val, a.u_.int_val);
15909 break;
15910 case NC_FLOAT:
15911 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
15912 break;
15913 case NC_COMPLEX:
fcbea5e4 15914 mpc_init2(this->u_.complex_val, mpc_precision);
15915 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
0c77715b 15916 break;
15917 default:
15918 go_unreachable();
15919 }
15920 return *this;
15921}
15922
15923// Clear the contents.
15924
15925void
15926Numeric_constant::clear()
15927{
15928 switch (this->classification_)
15929 {
15930 case NC_INVALID:
15931 break;
15932 case NC_INT:
15933 case NC_RUNE:
15934 mpz_clear(this->u_.int_val);
15935 break;
15936 case NC_FLOAT:
15937 mpfr_clear(this->u_.float_val);
15938 break;
15939 case NC_COMPLEX:
fcbea5e4 15940 mpc_clear(this->u_.complex_val);
0c77715b 15941 break;
15942 default:
15943 go_unreachable();
15944 }
15945 this->classification_ = NC_INVALID;
15946}
15947
15948// Set to an unsigned long value.
15949
15950void
15951Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
15952{
15953 this->clear();
15954 this->classification_ = NC_INT;
15955 this->type_ = type;
15956 mpz_init_set_ui(this->u_.int_val, val);
15957}
15958
15959// Set to an integer value.
15960
15961void
15962Numeric_constant::set_int(Type* type, const mpz_t val)
15963{
15964 this->clear();
15965 this->classification_ = NC_INT;
15966 this->type_ = type;
15967 mpz_init_set(this->u_.int_val, val);
15968}
15969
15970// Set to a rune value.
15971
15972void
15973Numeric_constant::set_rune(Type* type, const mpz_t val)
15974{
15975 this->clear();
15976 this->classification_ = NC_RUNE;
15977 this->type_ = type;
15978 mpz_init_set(this->u_.int_val, val);
15979}
15980
15981// Set to a floating point value.
15982
15983void
15984Numeric_constant::set_float(Type* type, const mpfr_t val)
15985{
15986 this->clear();
15987 this->classification_ = NC_FLOAT;
15988 this->type_ = type;
833b523c 15989 // Numeric constants do not have negative zero values, so remove
15990 // them here. They also don't have infinity or NaN values, but we
15991 // should never see them here.
15992 if (mpfr_zero_p(val))
15993 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN);
15994 else
15995 mpfr_init_set(this->u_.float_val, val, GMP_RNDN);
0c77715b 15996}
15997
15998// Set to a complex value.
15999
16000void
fcbea5e4 16001Numeric_constant::set_complex(Type* type, const mpc_t val)
0c77715b 16002{
16003 this->clear();
16004 this->classification_ = NC_COMPLEX;
16005 this->type_ = type;
fcbea5e4 16006 mpc_init2(this->u_.complex_val, mpc_precision);
16007 mpc_set(this->u_.complex_val, val, MPC_RNDNN);
0c77715b 16008}
16009
16010// Get an int value.
16011
16012void
16013Numeric_constant::get_int(mpz_t* val) const
16014{
16015 go_assert(this->is_int());
16016 mpz_init_set(*val, this->u_.int_val);
16017}
16018
16019// Get a rune value.
16020
16021void
16022Numeric_constant::get_rune(mpz_t* val) const
16023{
16024 go_assert(this->is_rune());
16025 mpz_init_set(*val, this->u_.int_val);
16026}
16027
16028// Get a floating point value.
16029
16030void
16031Numeric_constant::get_float(mpfr_t* val) const
16032{
16033 go_assert(this->is_float());
16034 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
16035}
16036
16037// Get a complex value.
16038
16039void
fcbea5e4 16040Numeric_constant::get_complex(mpc_t* val) const
0c77715b 16041{
16042 go_assert(this->is_complex());
fcbea5e4 16043 mpc_init2(*val, mpc_precision);
16044 mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
0c77715b 16045}
16046
16047// Express value as unsigned long if possible.
16048
16049Numeric_constant::To_unsigned_long
16050Numeric_constant::to_unsigned_long(unsigned long* val) const
16051{
16052 switch (this->classification_)
16053 {
16054 case NC_INT:
16055 case NC_RUNE:
16056 return this->mpz_to_unsigned_long(this->u_.int_val, val);
16057 case NC_FLOAT:
16058 return this->mpfr_to_unsigned_long(this->u_.float_val, val);
16059 case NC_COMPLEX:
fcbea5e4 16060 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16061 return NC_UL_NOTINT;
fcbea5e4 16062 return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val),
16063 val);
0c77715b 16064 default:
16065 go_unreachable();
16066 }
16067}
16068
16069// Express integer value as unsigned long if possible.
16070
16071Numeric_constant::To_unsigned_long
16072Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
16073 unsigned long *val) const
16074{
16075 if (mpz_sgn(ival) < 0)
16076 return NC_UL_NEGATIVE;
16077 unsigned long ui = mpz_get_ui(ival);
16078 if (mpz_cmp_ui(ival, ui) != 0)
16079 return NC_UL_BIG;
16080 *val = ui;
16081 return NC_UL_VALID;
16082}
16083
16084// Express floating point value as unsigned long if possible.
16085
16086Numeric_constant::To_unsigned_long
16087Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
16088 unsigned long *val) const
16089{
16090 if (!mpfr_integer_p(fval))
16091 return NC_UL_NOTINT;
16092 mpz_t ival;
16093 mpz_init(ival);
16094 mpfr_get_z(ival, fval, GMP_RNDN);
16095 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
16096 mpz_clear(ival);
16097 return ret;
16098}
16099
03118c21 16100// Express value as memory size if possible.
16101
16102bool
16103Numeric_constant::to_memory_size(int64_t* val) const
16104{
16105 switch (this->classification_)
16106 {
16107 case NC_INT:
16108 case NC_RUNE:
16109 return this->mpz_to_memory_size(this->u_.int_val, val);
16110 case NC_FLOAT:
16111 return this->mpfr_to_memory_size(this->u_.float_val, val);
16112 case NC_COMPLEX:
16113 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
16114 return false;
16115 return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val);
16116 default:
16117 go_unreachable();
16118 }
16119}
16120
16121// Express integer as memory size if possible.
16122
16123bool
16124Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const
16125{
16126 if (mpz_sgn(ival) < 0)
16127 return false;
16128 if (mpz_fits_slong_p(ival))
16129 {
16130 *val = static_cast<int64_t>(mpz_get_si(ival));
16131 return true;
16132 }
16133
16134 // Test >= 64, not > 64, because an int64_t can hold 63 bits of a
16135 // positive value.
16136 if (mpz_sizeinbase(ival, 2) >= 64)
16137 return false;
16138
16139 mpz_t q, r;
16140 mpz_init(q);
16141 mpz_init(r);
16142 mpz_tdiv_q_2exp(q, ival, 32);
16143 mpz_tdiv_r_2exp(r, ival, 32);
16144 go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r));
16145 *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32)
16146 + static_cast<int64_t>(mpz_get_ui(r)));
16147 mpz_clear(r);
16148 mpz_clear(q);
16149 return true;
16150}
16151
16152// Express floating point value as memory size if possible.
16153
16154bool
16155Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const
16156{
16157 if (!mpfr_integer_p(fval))
16158 return false;
16159 mpz_t ival;
16160 mpz_init(ival);
16161 mpfr_get_z(ival, fval, GMP_RNDN);
16162 bool ret = this->mpz_to_memory_size(ival, val);
16163 mpz_clear(ival);
16164 return ret;
16165}
16166
0c77715b 16167// Convert value to integer if possible.
16168
16169bool
16170Numeric_constant::to_int(mpz_t* val) const
16171{
16172 switch (this->classification_)
16173 {
16174 case NC_INT:
16175 case NC_RUNE:
16176 mpz_init_set(*val, this->u_.int_val);
16177 return true;
16178 case NC_FLOAT:
16179 if (!mpfr_integer_p(this->u_.float_val))
16180 return false;
16181 mpz_init(*val);
16182 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN);
16183 return true;
16184 case NC_COMPLEX:
fcbea5e4 16185 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))
16186 || !mpfr_integer_p(mpc_realref(this->u_.complex_val)))
0c77715b 16187 return false;
16188 mpz_init(*val);
fcbea5e4 16189 mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16190 return true;
16191 default:
16192 go_unreachable();
16193 }
16194}
16195
16196// Convert value to floating point if possible.
16197
16198bool
16199Numeric_constant::to_float(mpfr_t* val) const
16200{
16201 switch (this->classification_)
16202 {
16203 case NC_INT:
16204 case NC_RUNE:
16205 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN);
16206 return true;
16207 case NC_FLOAT:
16208 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
16209 return true;
16210 case NC_COMPLEX:
fcbea5e4 16211 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16212 return false;
fcbea5e4 16213 mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16214 return true;
16215 default:
16216 go_unreachable();
16217 }
16218}
16219
16220// Convert value to complex.
16221
16222bool
fcbea5e4 16223Numeric_constant::to_complex(mpc_t* val) const
0c77715b 16224{
fcbea5e4 16225 mpc_init2(*val, mpc_precision);
0c77715b 16226 switch (this->classification_)
16227 {
16228 case NC_INT:
16229 case NC_RUNE:
fcbea5e4 16230 mpc_set_z(*val, this->u_.int_val, MPC_RNDNN);
0c77715b 16231 return true;
16232 case NC_FLOAT:
fcbea5e4 16233 mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN);
0c77715b 16234 return true;
16235 case NC_COMPLEX:
fcbea5e4 16236 mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
0c77715b 16237 return true;
16238 default:
16239 go_unreachable();
16240 }
16241}
16242
16243// Get the type.
16244
16245Type*
16246Numeric_constant::type() const
16247{
16248 if (this->type_ != NULL)
16249 return this->type_;
16250 switch (this->classification_)
16251 {
16252 case NC_INT:
16253 return Type::make_abstract_integer_type();
16254 case NC_RUNE:
16255 return Type::make_abstract_character_type();
16256 case NC_FLOAT:
16257 return Type::make_abstract_float_type();
16258 case NC_COMPLEX:
16259 return Type::make_abstract_complex_type();
16260 default:
16261 go_unreachable();
16262 }
16263}
16264
16265// If the constant can be expressed in TYPE, then set the type of the
16266// constant to TYPE and return true. Otherwise return false, and, if
16267// ISSUE_ERROR is true, report an appropriate error message.
16268
16269bool
16270Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
16271{
16272 bool ret;
f11c2155 16273 if (type == NULL || type->is_error())
0c77715b 16274 ret = true;
16275 else if (type->integer_type() != NULL)
16276 ret = this->check_int_type(type->integer_type(), issue_error, loc);
16277 else if (type->float_type() != NULL)
16278 ret = this->check_float_type(type->float_type(), issue_error, loc);
16279 else if (type->complex_type() != NULL)
16280 ret = this->check_complex_type(type->complex_type(), issue_error, loc);
16281 else
5706ab68 16282 {
16283 ret = false;
16284 if (issue_error)
16285 go_assert(saw_errors());
16286 }
0c77715b 16287 if (ret)
16288 this->type_ = type;
16289 return ret;
16290}
16291
16292// Check whether the constant can be expressed in an integer type.
16293
16294bool
16295Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
71a45216 16296 Location location)
0c77715b 16297{
16298 mpz_t val;
16299 switch (this->classification_)
16300 {
16301 case NC_INT:
16302 case NC_RUNE:
16303 mpz_init_set(val, this->u_.int_val);
16304 break;
16305
16306 case NC_FLOAT:
16307 if (!mpfr_integer_p(this->u_.float_val))
16308 {
16309 if (issue_error)
71a45216 16310 {
631d5788 16311 go_error_at(location,
16312 "floating point constant truncated to integer");
71a45216 16313 this->set_invalid();
16314 }
0c77715b 16315 return false;
16316 }
16317 mpz_init(val);
16318 mpfr_get_z(val, this->u_.float_val, GMP_RNDN);
16319 break;
16320
16321 case NC_COMPLEX:
fcbea5e4 16322 if (!mpfr_integer_p(mpc_realref(this->u_.complex_val))
16323 || !mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16324 {
16325 if (issue_error)
71a45216 16326 {
631d5788 16327 go_error_at(location, "complex constant truncated to integer");
71a45216 16328 this->set_invalid();
16329 }
0c77715b 16330 return false;
16331 }
16332 mpz_init(val);
fcbea5e4 16333 mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16334 break;
16335
16336 default:
16337 go_unreachable();
16338 }
16339
16340 bool ret;
16341 if (type->is_abstract())
16342 ret = true;
16343 else
16344 {
16345 int bits = mpz_sizeinbase(val, 2);
16346 if (type->is_unsigned())
16347 {
16348 // For an unsigned type we can only accept a nonnegative
16349 // number, and we must be able to represents at least BITS.
16350 ret = mpz_sgn(val) >= 0 && bits <= type->bits();
16351 }
16352 else
16353 {
16354 // For a signed type we need an extra bit to indicate the
16355 // sign. We have to handle the most negative integer
16356 // specially.
16357 ret = (bits + 1 <= type->bits()
16358 || (bits <= type->bits()
16359 && mpz_sgn(val) < 0
16360 && (mpz_scan1(val, 0)
16361 == static_cast<unsigned long>(type->bits() - 1))
16362 && mpz_scan0(val, type->bits()) == ULONG_MAX));
16363 }
16364 }
16365
16366 if (!ret && issue_error)
71a45216 16367 {
631d5788 16368 go_error_at(location, "integer constant overflow");
71a45216 16369 this->set_invalid();
16370 }
0c77715b 16371
16372 return ret;
16373}
16374
16375// Check whether the constant can be expressed in a floating point
16376// type.
16377
16378bool
16379Numeric_constant::check_float_type(Float_type* type, bool issue_error,
d0bcce51 16380 Location location)
0c77715b 16381{
16382 mpfr_t val;
16383 switch (this->classification_)
16384 {
16385 case NC_INT:
16386 case NC_RUNE:
16387 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN);
16388 break;
16389
16390 case NC_FLOAT:
16391 mpfr_init_set(val, this->u_.float_val, GMP_RNDN);
16392 break;
16393
16394 case NC_COMPLEX:
fcbea5e4 16395 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16396 {
16397 if (issue_error)
71a45216 16398 {
16399 this->set_invalid();
631d5788 16400 go_error_at(location, "complex constant truncated to float");
71a45216 16401 }
0c77715b 16402 return false;
16403 }
fcbea5e4 16404 mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16405 break;
16406
16407 default:
16408 go_unreachable();
16409 }
16410
16411 bool ret;
16412 if (type->is_abstract())
16413 ret = true;
16414 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
16415 {
16416 // A NaN or Infinity always fits in the range of the type.
16417 ret = true;
16418 }
16419 else
16420 {
16421 mp_exp_t exp = mpfr_get_exp(val);
16422 mp_exp_t max_exp;
16423 switch (type->bits())
16424 {
16425 case 32:
16426 max_exp = 128;
16427 break;
16428 case 64:
16429 max_exp = 1024;
16430 break;
16431 default:
16432 go_unreachable();
16433 }
16434
16435 ret = exp <= max_exp;
d0bcce51 16436
16437 if (ret)
16438 {
16439 // Round the constant to the desired type.
16440 mpfr_t t;
16441 mpfr_init(t);
16442 switch (type->bits())
16443 {
16444 case 32:
16445 mpfr_set_prec(t, 24);
16446 break;
16447 case 64:
16448 mpfr_set_prec(t, 53);
16449 break;
16450 default:
16451 go_unreachable();
16452 }
16453 mpfr_set(t, val, GMP_RNDN);
16454 mpfr_set(val, t, GMP_RNDN);
16455 mpfr_clear(t);
16456
16457 this->set_float(type, val);
16458 }
0c77715b 16459 }
16460
16461 mpfr_clear(val);
16462
16463 if (!ret && issue_error)
71a45216 16464 {
631d5788 16465 go_error_at(location, "floating point constant overflow");
71a45216 16466 this->set_invalid();
16467 }
0c77715b 16468
16469 return ret;
16470}
16471
16472// Check whether the constant can be expressed in a complex type.
16473
16474bool
16475Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
d0bcce51 16476 Location location)
0c77715b 16477{
16478 if (type->is_abstract())
16479 return true;
16480
16481 mp_exp_t max_exp;
16482 switch (type->bits())
16483 {
16484 case 64:
16485 max_exp = 128;
16486 break;
16487 case 128:
16488 max_exp = 1024;
16489 break;
16490 default:
16491 go_unreachable();
16492 }
16493
fcbea5e4 16494 mpc_t val;
16495 mpc_init2(val, mpc_precision);
0c77715b 16496 switch (this->classification_)
16497 {
16498 case NC_INT:
16499 case NC_RUNE:
fcbea5e4 16500 mpc_set_z(val, this->u_.int_val, MPC_RNDNN);
0c77715b 16501 break;
16502
16503 case NC_FLOAT:
fcbea5e4 16504 mpc_set_fr(val, this->u_.float_val, MPC_RNDNN);
0c77715b 16505 break;
16506
16507 case NC_COMPLEX:
fcbea5e4 16508 mpc_set(val, this->u_.complex_val, MPC_RNDNN);
0c77715b 16509 break;
16510
16511 default:
16512 go_unreachable();
16513 }
16514
d0bcce51 16515 bool ret = true;
fcbea5e4 16516 if (!mpfr_nan_p(mpc_realref(val))
16517 && !mpfr_inf_p(mpc_realref(val))
16518 && !mpfr_zero_p(mpc_realref(val))
16519 && mpfr_get_exp(mpc_realref(val)) > max_exp)
d0bcce51 16520 {
16521 if (issue_error)
71a45216 16522 {
631d5788 16523 go_error_at(location, "complex real part overflow");
71a45216 16524 this->set_invalid();
16525 }
d0bcce51 16526 ret = false;
16527 }
0c77715b 16528
fcbea5e4 16529 if (!mpfr_nan_p(mpc_imagref(val))
16530 && !mpfr_inf_p(mpc_imagref(val))
16531 && !mpfr_zero_p(mpc_imagref(val))
16532 && mpfr_get_exp(mpc_imagref(val)) > max_exp)
d0bcce51 16533 {
16534 if (issue_error)
71a45216 16535 {
631d5788 16536 go_error_at(location, "complex imaginary part overflow");
71a45216 16537 this->set_invalid();
16538 }
d0bcce51 16539 ret = false;
16540 }
0c77715b 16541
d0bcce51 16542 if (ret)
16543 {
16544 // Round the constant to the desired type.
fcbea5e4 16545 mpc_t t;
d0bcce51 16546 switch (type->bits())
16547 {
16548 case 64:
fcbea5e4 16549 mpc_init2(t, 24);
d0bcce51 16550 break;
16551 case 128:
fcbea5e4 16552 mpc_init2(t, 53);
d0bcce51 16553 break;
16554 default:
16555 go_unreachable();
16556 }
fcbea5e4 16557 mpc_set(t, val, MPC_RNDNN);
16558 mpc_set(val, t, MPC_RNDNN);
16559 mpc_clear(t);
d0bcce51 16560
fcbea5e4 16561 this->set_complex(type, val);
d0bcce51 16562 }
16563
fcbea5e4 16564 mpc_clear(val);
0c77715b 16565
16566 return ret;
16567}
16568
16569// Return an Expression for this value.
16570
16571Expression*
16572Numeric_constant::expression(Location loc) const
16573{
16574 switch (this->classification_)
16575 {
16576 case NC_INT:
e67508fa 16577 return Expression::make_integer_z(&this->u_.int_val, this->type_, loc);
0c77715b 16578 case NC_RUNE:
16579 return Expression::make_character(&this->u_.int_val, this->type_, loc);
16580 case NC_FLOAT:
16581 return Expression::make_float(&this->u_.float_val, this->type_, loc);
16582 case NC_COMPLEX:
fcbea5e4 16583 return Expression::make_complex(&this->u_.complex_val, this->type_, loc);
71a45216 16584 case NC_INVALID:
16585 go_assert(saw_errors());
16586 return Expression::make_error(loc);
0c77715b 16587 default:
16588 go_unreachable();
16589 }
16590}