]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/gofrontend/expressions.cc
* system-linux-arm.ads (Memory_Size): Use Long_Integer'Size
[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
54211955 147 if (lhs_type->forwarded() != rhs_type->forwarded()
148 && 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 }
54211955 156 else if (lhs_type->forwarded() != rhs_type->forwarded()
157 && rhs_type->interface_type() != NULL)
2c809f8f 158 return Expression::convert_interface_to_type(lhs_type, rhs, location);
411eb89e 159 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
e440a328 160 {
2c809f8f 161 // Assigning nil to a slice.
2c809f8f 162 Expression* nil = Expression::make_nil(location);
e67508fa 163 Expression* zero = Expression::make_integer_ul(0, NULL, location);
2c809f8f 164 return Expression::make_slice_value(lhs_type, nil, zero, zero, location);
e440a328 165 }
166 else if (rhs_type->is_nil_type())
2c809f8f 167 return Expression::make_nil(location);
168 else if (Type::are_identical(lhs_type, rhs_type, false, NULL))
e440a328 169 {
170 // No conversion is needed.
2c809f8f 171 return rhs;
172 }
173 else if (lhs_type->points_to() != NULL)
174 return Expression::make_unsafe_cast(lhs_type, rhs, location);
175 else if (lhs_type->is_numeric_type())
176 return Expression::make_cast(lhs_type, rhs, location);
177 else if ((lhs_type->struct_type() != NULL
178 && rhs_type->struct_type() != NULL)
179 || (lhs_type->array_type() != NULL
180 && rhs_type->array_type() != NULL))
e440a328 181 {
182 // This conversion must be permitted by Go, or we wouldn't have
183 // gotten here.
2c809f8f 184 return Expression::make_unsafe_cast(lhs_type, rhs, location);
e440a328 185 }
186 else
2c809f8f 187 return rhs;
e440a328 188}
189
2c809f8f 190// Return an expression for a conversion from a non-interface type to an
e440a328 191// interface type.
192
2c809f8f 193Expression*
194Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs,
195 Location location)
e440a328 196{
e440a328 197 Interface_type* lhs_interface_type = lhs_type->interface_type();
198 bool lhs_is_empty = lhs_interface_type->is_empty();
199
200 // Since RHS_TYPE is a static type, we can create the interface
201 // method table at compile time.
202
203 // When setting an interface to nil, we just set both fields to
204 // NULL.
2c809f8f 205 Type* rhs_type = rhs->type();
e440a328 206 if (rhs_type->is_nil_type())
63697958 207 {
2c809f8f 208 Expression* nil = Expression::make_nil(location);
209 return Expression::make_interface_value(lhs_type, nil, nil, location);
63697958 210 }
e440a328 211
212 // This should have been checked already.
c484d925 213 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
e440a328 214
e440a328 215 // An interface is a tuple. If LHS_TYPE is an empty interface type,
216 // then the first field is the type descriptor for RHS_TYPE.
217 // Otherwise it is the interface method table for RHS_TYPE.
2c809f8f 218 Expression* first_field;
e440a328 219 if (lhs_is_empty)
2c809f8f 220 first_field = Expression::make_type_descriptor(rhs_type, location);
e440a328 221 else
222 {
223 // Build the interface method table for this interface and this
224 // object type: a list of function pointers for each interface
225 // method.
226 Named_type* rhs_named_type = rhs_type->named_type();
c0cab2ec 227 Struct_type* rhs_struct_type = rhs_type->struct_type();
e440a328 228 bool is_pointer = false;
c0cab2ec 229 if (rhs_named_type == NULL && rhs_struct_type == NULL)
e440a328 230 {
231 rhs_named_type = rhs_type->deref()->named_type();
c0cab2ec 232 rhs_struct_type = rhs_type->deref()->struct_type();
e440a328 233 is_pointer = true;
234 }
c0cab2ec 235 if (rhs_named_type != NULL)
2c809f8f 236 first_field =
237 rhs_named_type->interface_method_table(lhs_interface_type,
238 is_pointer);
c0cab2ec 239 else if (rhs_struct_type != NULL)
2c809f8f 240 first_field =
241 rhs_struct_type->interface_method_table(lhs_interface_type,
242 is_pointer);
c0cab2ec 243 else
2c809f8f 244 first_field = Expression::make_nil(location);
e440a328 245 }
e440a328 246
2c809f8f 247 Expression* obj;
e440a328 248 if (rhs_type->points_to() != NULL)
249 {
2c809f8f 250 // We are assigning a pointer to the interface; the interface
e440a328 251 // holds the pointer itself.
2c809f8f 252 obj = rhs;
253 }
254 else
255 {
256 // We are assigning a non-pointer value to the interface; the
45ff893b 257 // interface gets a copy of the value in the heap if it escapes.
258 // TODO(cmang): Associate escape state state of RHS with newly
259 // created OBJ.
2c809f8f 260 obj = Expression::make_heap_expression(rhs, location);
e440a328 261 }
262
2c809f8f 263 return Expression::make_interface_value(lhs_type, first_field, obj, location);
264}
e440a328 265
2c809f8f 266// Return an expression for the type descriptor of RHS.
e440a328 267
2c809f8f 268Expression*
269Expression::get_interface_type_descriptor(Expression* rhs)
270{
271 go_assert(rhs->type()->interface_type() != NULL);
272 Location location = rhs->location();
e440a328 273
2c809f8f 274 // The type descriptor is the first field of an empty interface.
275 if (rhs->type()->interface_type()->is_empty())
276 return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR,
277 location);
278
279 Expression* mtable =
280 Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location);
e440a328 281
2c809f8f 282 Expression* descriptor =
283 Expression::make_unary(OPERATOR_MULT, mtable, location);
284 descriptor = Expression::make_field_reference(descriptor, 0, location);
285 Expression* nil = Expression::make_nil(location);
e440a328 286
2c809f8f 287 Expression* eq =
288 Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location);
289 return Expression::make_conditional(eq, nil, descriptor, location);
e440a328 290}
291
2c809f8f 292// Return an expression for the conversion of an interface type to an
e440a328 293// interface type.
294
2c809f8f 295Expression*
296Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs,
297 bool for_type_guard,
298 Location location)
e440a328 299{
8ba8cc87 300 if (Type::are_identical(lhs_type, rhs->type(), false, NULL))
301 return rhs;
302
e440a328 303 Interface_type* lhs_interface_type = lhs_type->interface_type();
304 bool lhs_is_empty = lhs_interface_type->is_empty();
305
e440a328 306 // In the general case this requires runtime examination of the type
307 // method table to match it up with the interface methods.
308
309 // FIXME: If all of the methods in the right hand side interface
310 // also appear in the left hand side interface, then we don't need
311 // to do a runtime check, although we still need to build a new
312 // method table.
313
8ba8cc87 314 // We are going to evaluate RHS multiple times.
315 go_assert(rhs->is_variable());
316
e440a328 317 // Get the type descriptor for the right hand side. This will be
318 // NULL for a nil interface.
2c809f8f 319 Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs);
320 Expression* lhs_type_expr =
321 Expression::make_type_descriptor(lhs_type, location);
e440a328 322
2c809f8f 323 Expression* first_field;
e440a328 324 if (for_type_guard)
325 {
326 // A type assertion fails when converting a nil interface.
6098d6cb 327 first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2,
328 lhs_type_expr, rhs_type_expr);
e440a328 329 }
330 else if (lhs_is_empty)
331 {
2c809f8f 332 // A conversion to an empty interface always succeeds, and the
e440a328 333 // first field is just the type descriptor of the object.
2c809f8f 334 first_field = rhs_type_expr;
e440a328 335 }
336 else
337 {
338 // A conversion to a non-empty interface may fail, but unlike a
339 // type assertion converting nil will always succeed.
6098d6cb 340 first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2,
341 lhs_type_expr, rhs_type_expr);
e440a328 342 }
343
344 // The second field is simply the object pointer.
2c809f8f 345 Expression* obj =
346 Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location);
347 return Expression::make_interface_value(lhs_type, first_field, obj, location);
e440a328 348}
349
2c809f8f 350// Return an expression for the conversion of an interface type to a
e440a328 351// non-interface type.
352
2c809f8f 353Expression*
354Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs,
355 Location location)
e440a328 356{
8ba8cc87 357 // We are going to evaluate RHS multiple times.
358 go_assert(rhs->is_variable());
359
e440a328 360 // Call a function to check that the type is valid. The function
361 // will panic with an appropriate runtime type error if the type is
362 // not valid.
2c809f8f 363 Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type,
364 location);
365 Expression* rhs_descriptor =
366 Expression::get_interface_type_descriptor(rhs);
367
368 Type* rhs_type = rhs->type();
369 Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type,
370 location);
371
6098d6cb 372 Expression* check_iface = Runtime::make_call(Runtime::ASSERTI2T,
2c809f8f 373 location, 3, lhs_type_expr,
374 rhs_descriptor, rhs_inter_expr);
e440a328 375
376 // If the call succeeds, pull out the value.
2c809f8f 377 Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT,
378 location);
e440a328 379
380 // If the value is a pointer, then it is the value we want.
381 // Otherwise it points to the value.
382 if (lhs_type->points_to() == NULL)
383 {
2c809f8f 384 obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj,
385 location);
386 obj = Expression::make_unary(OPERATOR_MULT, obj, location);
e440a328 387 }
2c809f8f 388 return Expression::make_compound(check_iface, obj, location);
e440a328 389}
390
ea664253 391// Convert an expression to its backend representation. This is implemented by
392// the child class. Not that it is not in general safe to call this multiple
e440a328 393// times for a single expression, but that we don't catch such errors.
394
ea664253 395Bexpression*
396Expression::get_backend(Translate_context* context)
e440a328 397{
398 // The child may have marked this expression as having an error.
399 if (this->classification_ == EXPRESSION_ERROR)
ea664253 400 return context->backend()->error_expression();
e440a328 401
ea664253 402 return this->do_get_backend(context);
e440a328 403}
404
48c2a53a 405// Return a backend expression for VAL.
406Bexpression*
407Expression::backend_numeric_constant_expression(Translate_context* context,
408 Numeric_constant* val)
e440a328 409{
48c2a53a 410 Gogo* gogo = context->gogo();
411 Type* type = val->type();
412 if (type == NULL)
413 return gogo->backend()->error_expression();
e440a328 414
48c2a53a 415 Btype* btype = type->get_backend(gogo);
416 Bexpression* ret;
417 if (type->integer_type() != NULL)
e440a328 418 {
419 mpz_t ival;
48c2a53a 420 if (!val->to_int(&ival))
421 {
422 go_assert(saw_errors());
423 return gogo->backend()->error_expression();
424 }
425 ret = gogo->backend()->integer_constant_expression(btype, ival);
e440a328 426 mpz_clear(ival);
e440a328 427 }
48c2a53a 428 else if (type->float_type() != NULL)
e440a328 429 {
48c2a53a 430 mpfr_t fval;
431 if (!val->to_float(&fval))
432 {
433 go_assert(saw_errors());
434 return gogo->backend()->error_expression();
435 }
436 ret = gogo->backend()->float_constant_expression(btype, fval);
437 mpfr_clear(fval);
e440a328 438 }
48c2a53a 439 else if (type->complex_type() != NULL)
e440a328 440 {
fcbea5e4 441 mpc_t cval;
442 if (!val->to_complex(&cval))
48c2a53a 443 {
444 go_assert(saw_errors());
445 return gogo->backend()->error_expression();
446 }
fcbea5e4 447 ret = gogo->backend()->complex_constant_expression(btype, cval);
448 mpc_clear(cval);
e440a328 449 }
450 else
c3e6f413 451 go_unreachable();
e440a328 452
48c2a53a 453 return ret;
e440a328 454}
455
2c809f8f 456// Return an expression which evaluates to true if VAL, of arbitrary integer
457// type, is negative or is more than the maximum value of the Go type "int".
e440a328 458
2c809f8f 459Expression*
460Expression::check_bounds(Expression* val, Location loc)
e440a328 461{
2c809f8f 462 Type* val_type = val->type();
463 Type* bound_type = Type::lookup_integer_type("int");
464
465 int val_type_size;
466 bool val_is_unsigned = false;
467 if (val_type->integer_type() != NULL)
468 {
469 val_type_size = val_type->integer_type()->bits();
470 val_is_unsigned = val_type->integer_type()->is_unsigned();
471 }
472 else
473 {
474 if (!val_type->is_numeric_type()
475 || !Type::are_convertible(bound_type, val_type, NULL))
476 {
477 go_assert(saw_errors());
478 return Expression::make_boolean(true, loc);
479 }
e440a328 480
2c809f8f 481 if (val_type->complex_type() != NULL)
482 val_type_size = val_type->complex_type()->bits();
483 else
484 val_type_size = val_type->float_type()->bits();
485 }
486
487 Expression* negative_index = Expression::make_boolean(false, loc);
488 Expression* index_overflows = Expression::make_boolean(false, loc);
489 if (!val_is_unsigned)
e440a328 490 {
e67508fa 491 Expression* zero = Expression::make_integer_ul(0, val_type, loc);
2c809f8f 492 negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc);
e440a328 493 }
494
2c809f8f 495 int bound_type_size = bound_type->integer_type()->bits();
c3068ac0 496 if (val_type_size > bound_type_size
497 || (val_type_size == bound_type_size
2c809f8f 498 && val_is_unsigned))
499 {
500 mpz_t one;
501 mpz_init_set_ui(one, 1UL);
502
503 // maxval = 2^(bound_type_size - 1) - 1
504 mpz_t maxval;
505 mpz_init(maxval);
506 mpz_mul_2exp(maxval, one, bound_type_size - 1);
507 mpz_sub_ui(maxval, maxval, 1);
e67508fa 508 Expression* max = Expression::make_integer_z(&maxval, val_type, loc);
2c809f8f 509 mpz_clear(one);
510 mpz_clear(maxval);
511
512 index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc);
e440a328 513 }
514
2c809f8f 515 return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows,
516 loc);
e440a328 517}
518
d751bb78 519void
520Expression::dump_expression(Ast_dump_context* ast_dump_context) const
521{
522 this->do_dump_expression(ast_dump_context);
523}
524
e440a328 525// Error expressions. This are used to avoid cascading errors.
526
527class Error_expression : public Expression
528{
529 public:
b13c66cd 530 Error_expression(Location location)
e440a328 531 : Expression(EXPRESSION_ERROR, location)
532 { }
533
534 protected:
535 bool
536 do_is_constant() const
537 { return true; }
538
539 bool
0c77715b 540 do_numeric_constant_value(Numeric_constant* nc) const
e440a328 541 {
0c77715b 542 nc->set_unsigned_long(NULL, 0);
e440a328 543 return true;
544 }
545
4f2138d7 546 bool
e440a328 547 do_discarding_value()
4f2138d7 548 { return true; }
e440a328 549
550 Type*
551 do_type()
552 { return Type::make_error_type(); }
553
554 void
555 do_determine_type(const Type_context*)
556 { }
557
558 Expression*
559 do_copy()
560 { return this; }
561
562 bool
563 do_is_addressable() const
564 { return true; }
565
ea664253 566 Bexpression*
567 do_get_backend(Translate_context* context)
568 { return context->backend()->error_expression(); }
d751bb78 569
570 void
571 do_dump_expression(Ast_dump_context*) const;
e440a328 572};
573
d751bb78 574// Dump the ast representation for an error expression to a dump context.
575
576void
577Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
578{
579 ast_dump_context->ostream() << "_Error_" ;
580}
581
e440a328 582Expression*
b13c66cd 583Expression::make_error(Location location)
e440a328 584{
585 return new Error_expression(location);
586}
587
588// An expression which is really a type. This is used during parsing.
589// It is an error if these survive after lowering.
590
591class
592Type_expression : public Expression
593{
594 public:
b13c66cd 595 Type_expression(Type* type, Location location)
e440a328 596 : Expression(EXPRESSION_TYPE, location),
597 type_(type)
598 { }
599
600 protected:
601 int
602 do_traverse(Traverse* traverse)
603 { return Type::traverse(this->type_, traverse); }
604
605 Type*
606 do_type()
607 { return this->type_; }
608
609 void
610 do_determine_type(const Type_context*)
611 { }
612
613 void
614 do_check_types(Gogo*)
615 { this->report_error(_("invalid use of type")); }
616
617 Expression*
618 do_copy()
619 { return this; }
620
ea664253 621 Bexpression*
622 do_get_backend(Translate_context*)
c3e6f413 623 { go_unreachable(); }
e440a328 624
d751bb78 625 void do_dump_expression(Ast_dump_context*) const;
626
e440a328 627 private:
628 // The type which we are representing as an expression.
629 Type* type_;
630};
631
d751bb78 632void
633Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
634{
635 ast_dump_context->dump_type(this->type_);
636}
637
e440a328 638Expression*
b13c66cd 639Expression::make_type(Type* type, Location location)
e440a328 640{
641 return new Type_expression(type, location);
642}
643
e03bdf36 644// Class Parser_expression.
645
646Type*
647Parser_expression::do_type()
648{
649 // We should never really ask for the type of a Parser_expression.
650 // However, it can happen, at least when we have an invalid const
651 // whose initializer refers to the const itself. In that case we
652 // may ask for the type when lowering the const itself.
c484d925 653 go_assert(saw_errors());
e03bdf36 654 return Type::make_error_type();
655}
656
e440a328 657// Class Var_expression.
658
659// Lower a variable expression. Here we just make sure that the
660// initialization expression of the variable has been lowered. This
661// ensures that we will be able to determine the type of the variable
662// if necessary.
663
664Expression*
ceeb4318 665Var_expression::do_lower(Gogo* gogo, Named_object* function,
666 Statement_inserter* inserter, int)
e440a328 667{
668 if (this->variable_->is_variable())
669 {
670 Variable* var = this->variable_->var_value();
671 // This is either a local variable or a global variable. A
672 // reference to a variable which is local to an enclosing
673 // function will be a reference to a field in a closure.
674 if (var->is_global())
ceeb4318 675 {
676 function = NULL;
677 inserter = NULL;
678 }
679 var->lower_init_expression(gogo, function, inserter);
e440a328 680 }
681 return this;
682}
683
e440a328 684// Return the type of a reference to a variable.
685
686Type*
687Var_expression::do_type()
688{
689 if (this->variable_->is_variable())
690 return this->variable_->var_value()->type();
691 else if (this->variable_->is_result_variable())
692 return this->variable_->result_var_value()->type();
693 else
c3e6f413 694 go_unreachable();
e440a328 695}
696
0ab09e06 697// Determine the type of a reference to a variable.
698
699void
700Var_expression::do_determine_type(const Type_context*)
701{
702 if (this->variable_->is_variable())
703 this->variable_->var_value()->determine_type();
704}
705
e440a328 706// Something takes the address of this variable. This means that we
707// may want to move the variable onto the heap.
708
709void
710Var_expression::do_address_taken(bool escapes)
711{
712 if (!escapes)
f325319b 713 {
714 if (this->variable_->is_variable())
715 this->variable_->var_value()->set_non_escaping_address_taken();
716 else if (this->variable_->is_result_variable())
717 this->variable_->result_var_value()->set_non_escaping_address_taken();
718 else
719 go_unreachable();
720 }
e440a328 721 else
f325319b 722 {
723 if (this->variable_->is_variable())
724 this->variable_->var_value()->set_address_taken();
725 else if (this->variable_->is_result_variable())
726 this->variable_->result_var_value()->set_address_taken();
727 else
728 go_unreachable();
729 }
45ff893b 730
731 if (this->variable_->is_variable()
732 && this->variable_->var_value()->is_in_heap())
733 {
734 Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP);
735 Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP);
736 }
e440a328 737}
738
ea664253 739// Get the backend representation for a reference to a variable.
e440a328 740
ea664253 741Bexpression*
742Var_expression::do_get_backend(Translate_context* context)
e440a328 743{
fe2f84cf 744 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
745 context->function());
fe2f84cf 746 bool is_in_heap;
c6777780 747 Location loc = this->location();
9b27b43c 748 Btype* btype;
749 Gogo* gogo = context->gogo();
fe2f84cf 750 if (this->variable_->is_variable())
9b27b43c 751 {
752 is_in_heap = this->variable_->var_value()->is_in_heap();
753 btype = this->variable_->var_value()->type()->get_backend(gogo);
754 }
fe2f84cf 755 else if (this->variable_->is_result_variable())
9b27b43c 756 {
757 is_in_heap = this->variable_->result_var_value()->is_in_heap();
758 btype = this->variable_->result_var_value()->type()->get_backend(gogo);
759 }
fe2f84cf 760 else
c3e6f413 761 go_unreachable();
c6777780 762
d4e6573e 763 Bexpression* ret =
764 context->backend()->var_expression(bvar, this->in_lvalue_pos_, loc);
fe2f84cf 765 if (is_in_heap)
9b27b43c 766 ret = context->backend()->indirect_expression(btype, ret, true, loc);
ea664253 767 return ret;
e440a328 768}
769
d751bb78 770// Ast dump for variable expression.
771
772void
773Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
774{
775 ast_dump_context->ostream() << this->variable_->name() ;
776}
777
e440a328 778// Make a reference to a variable in an expression.
779
780Expression*
b13c66cd 781Expression::make_var_reference(Named_object* var, Location location)
e440a328 782{
783 if (var->is_sink())
784 return Expression::make_sink(location);
785
786 // FIXME: Creating a new object for each reference to a variable is
787 // wasteful.
788 return new Var_expression(var, location);
789}
790
b0c09712 791// Class Enclosed_var_expression.
792
793int
794Enclosed_var_expression::do_traverse(Traverse*)
795{
796 return TRAVERSE_CONTINUE;
797}
798
799// Lower the reference to the enclosed variable.
800
801Expression*
802Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function,
803 Statement_inserter* inserter, int)
804{
805 gogo->lower_expression(function, inserter, &this->reference_);
806 return this;
807}
808
809// Flatten the reference to the enclosed variable.
810
811Expression*
812Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function,
813 Statement_inserter* inserter)
814{
815 gogo->flatten_expression(function, inserter, &this->reference_);
816 return this;
817}
818
819void
820Enclosed_var_expression::do_address_taken(bool escapes)
821{
822 if (!escapes)
823 {
824 if (this->variable_->is_variable())
825 this->variable_->var_value()->set_non_escaping_address_taken();
826 else if (this->variable_->is_result_variable())
827 this->variable_->result_var_value()->set_non_escaping_address_taken();
828 else
829 go_unreachable();
830 }
831 else
832 {
833 if (this->variable_->is_variable())
834 this->variable_->var_value()->set_address_taken();
835 else if (this->variable_->is_result_variable())
836 this->variable_->result_var_value()->set_address_taken();
837 else
838 go_unreachable();
839 }
45ff893b 840
841 if (this->variable_->is_variable()
842 && this->variable_->var_value()->is_in_heap())
843 Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP);
b0c09712 844}
845
846// Ast dump for enclosed variable expression.
847
848void
849Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const
850{
851 adc->ostream() << this->variable_->name();
852}
853
854// Make a reference to a variable within an enclosing function.
855
856Expression*
857Expression::make_enclosing_var_reference(Expression* reference,
858 Named_object* var, Location location)
859{
860 return new Enclosed_var_expression(reference, var, location);
861}
862
e440a328 863// Class Temporary_reference_expression.
864
865// The type.
866
867Type*
868Temporary_reference_expression::do_type()
869{
870 return this->statement_->type();
871}
872
873// Called if something takes the address of this temporary variable.
874// We never have to move temporary variables to the heap, but we do
875// need to know that they must live in the stack rather than in a
876// register.
877
878void
879Temporary_reference_expression::do_address_taken(bool)
880{
881 this->statement_->set_is_address_taken();
882}
883
ea664253 884// Get a backend expression referring to the variable.
e440a328 885
ea664253 886Bexpression*
887Temporary_reference_expression::do_get_backend(Translate_context* context)
e440a328 888{
cd440cff 889 Gogo* gogo = context->gogo();
eefc1ed3 890 Bvariable* bvar = this->statement_->get_backend_variable(context);
d4e6573e 891 Varexpr_context ve_ctxt = (this->is_lvalue_ ? VE_lvalue : VE_rvalue);
892
893 Bexpression* ret = gogo->backend()->var_expression(bvar, ve_ctxt,
894 this->location());
eefc1ed3 895
cd440cff 896 // The backend can't always represent the same set of recursive types
eefc1ed3 897 // that the Go frontend can. In some cases this means that a
898 // temporary variable won't have the right backend type. Correct
899 // that here by adding a type cast. We need to use base() to push
900 // the circularity down one level.
cd440cff 901 Type* stype = this->statement_->type();
ceeb4318 902 if (!this->is_lvalue_
03118c21 903 && stype->points_to() != NULL
904 && stype->points_to()->is_void_type())
eefc1ed3 905 {
cd440cff 906 Btype* btype = this->type()->base()->get_backend(gogo);
907 ret = gogo->backend()->convert_expression(btype, ret, this->location());
eefc1ed3 908 }
ea664253 909 return ret;
e440a328 910}
911
d751bb78 912// Ast dump for temporary reference.
913
914void
915Temporary_reference_expression::do_dump_expression(
916 Ast_dump_context* ast_dump_context) const
917{
918 ast_dump_context->dump_temp_variable_name(this->statement_);
919}
920
e440a328 921// Make a reference to a temporary variable.
922
ceeb4318 923Temporary_reference_expression*
e440a328 924Expression::make_temporary_reference(Temporary_statement* statement,
b13c66cd 925 Location location)
e440a328 926{
927 return new Temporary_reference_expression(statement, location);
928}
929
e9d3367e 930// Class Set_and_use_temporary_expression.
931
932// Return the type.
933
934Type*
935Set_and_use_temporary_expression::do_type()
936{
937 return this->statement_->type();
938}
939
0afbb937 940// Determine the type of the expression.
941
942void
943Set_and_use_temporary_expression::do_determine_type(
944 const Type_context* context)
945{
946 this->expr_->determine_type(context);
947}
948
e9d3367e 949// Take the address.
950
951void
952Set_and_use_temporary_expression::do_address_taken(bool)
953{
954 this->statement_->set_is_address_taken();
955}
956
957// Return the backend representation.
958
ea664253 959Bexpression*
960Set_and_use_temporary_expression::do_get_backend(Translate_context* context)
e9d3367e 961{
e9d3367e 962 Location loc = this->location();
a302c105 963 Gogo* gogo = context->gogo();
964 Bvariable* bvar = this->statement_->get_backend_variable(context);
d4e6573e 965 Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, VE_rvalue, loc);
a302c105 966
0ab48656 967 Named_object* fn = context->function();
968 go_assert(fn != NULL);
969 Bfunction* bfn = fn->func_value()->get_or_make_decl(gogo, fn);
ea664253 970 Bexpression* bexpr = this->expr_->get_backend(context);
0ab48656 971 Bstatement* set = gogo->backend()->assignment_statement(bfn, lvar_ref,
972 bexpr, loc);
d4e6573e 973 Bexpression* var_ref = gogo->backend()->var_expression(bvar, VE_lvalue, loc);
a302c105 974 Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc);
ea664253 975 return ret;
e9d3367e 976}
977
978// Dump.
979
980void
981Set_and_use_temporary_expression::do_dump_expression(
982 Ast_dump_context* ast_dump_context) const
983{
984 ast_dump_context->ostream() << '(';
985 ast_dump_context->dump_temp_variable_name(this->statement_);
986 ast_dump_context->ostream() << " = ";
987 this->expr_->dump_expression(ast_dump_context);
988 ast_dump_context->ostream() << ')';
989}
990
991// Make a set-and-use temporary.
992
993Set_and_use_temporary_expression*
994Expression::make_set_and_use_temporary(Temporary_statement* statement,
995 Expression* expr, Location location)
996{
997 return new Set_and_use_temporary_expression(statement, expr, location);
998}
999
e440a328 1000// A sink expression--a use of the blank identifier _.
1001
1002class Sink_expression : public Expression
1003{
1004 public:
b13c66cd 1005 Sink_expression(Location location)
e440a328 1006 : Expression(EXPRESSION_SINK, location),
aa93217a 1007 type_(NULL), bvar_(NULL)
e440a328 1008 { }
1009
1010 protected:
4f2138d7 1011 bool
e440a328 1012 do_discarding_value()
4f2138d7 1013 { return true; }
e440a328 1014
1015 Type*
1016 do_type();
1017
1018 void
1019 do_determine_type(const Type_context*);
1020
1021 Expression*
1022 do_copy()
1023 { return new Sink_expression(this->location()); }
1024
ea664253 1025 Bexpression*
1026 do_get_backend(Translate_context*);
e440a328 1027
d751bb78 1028 void
1029 do_dump_expression(Ast_dump_context*) const;
1030
e440a328 1031 private:
1032 // The type of this sink variable.
1033 Type* type_;
1034 // The temporary variable we generate.
aa93217a 1035 Bvariable* bvar_;
e440a328 1036};
1037
1038// Return the type of a sink expression.
1039
1040Type*
1041Sink_expression::do_type()
1042{
1043 if (this->type_ == NULL)
1044 return Type::make_sink_type();
1045 return this->type_;
1046}
1047
1048// Determine the type of a sink expression.
1049
1050void
1051Sink_expression::do_determine_type(const Type_context* context)
1052{
1053 if (context->type != NULL)
1054 this->type_ = context->type;
1055}
1056
1057// Return a temporary variable for a sink expression. This will
1058// presumably be a write-only variable which the middle-end will drop.
1059
ea664253 1060Bexpression*
1061Sink_expression::do_get_backend(Translate_context* context)
e440a328 1062{
aa93217a 1063 Location loc = this->location();
1064 Gogo* gogo = context->gogo();
1065 if (this->bvar_ == NULL)
e440a328 1066 {
c484d925 1067 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
aa93217a 1068 Named_object* fn = context->function();
1069 go_assert(fn != NULL);
1070 Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn);
9f0e0513 1071 Btype* bt = this->type_->get_backend(context->gogo());
aa93217a 1072 Bstatement* decl;
1073 this->bvar_ =
1074 gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL,
1075 false, loc, &decl);
d4e6573e 1076 Bexpression* var_ref =
1077 gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc);
aa93217a 1078 var_ref = gogo->backend()->compound_expression(decl, var_ref, loc);
ea664253 1079 return var_ref;
e440a328 1080 }
d4e6573e 1081 return gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc);
e440a328 1082}
1083
d751bb78 1084// Ast dump for sink expression.
1085
1086void
1087Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1088{
1089 ast_dump_context->ostream() << "_" ;
1090}
1091
e440a328 1092// Make a sink expression.
1093
1094Expression*
b13c66cd 1095Expression::make_sink(Location location)
e440a328 1096{
1097 return new Sink_expression(location);
1098}
1099
1100// Class Func_expression.
1101
1102// FIXME: Can a function expression appear in a constant expression?
1103// The value is unchanging. Initializing a constant to the address of
1104// a function seems like it could work, though there might be little
1105// point to it.
1106
e440a328 1107// Traversal.
1108
1109int
1110Func_expression::do_traverse(Traverse* traverse)
1111{
1112 return (this->closure_ == NULL
1113 ? TRAVERSE_CONTINUE
1114 : Expression::traverse(&this->closure_, traverse));
1115}
1116
1117// Return the type of a function expression.
1118
1119Type*
1120Func_expression::do_type()
1121{
1122 if (this->function_->is_function())
1123 return this->function_->func_value()->type();
1124 else if (this->function_->is_function_declaration())
1125 return this->function_->func_declaration_value()->type();
1126 else
c3e6f413 1127 go_unreachable();
e440a328 1128}
1129
ea664253 1130// Get the backend representation for the code of a function expression.
e440a328 1131
97267c39 1132Bexpression*
8381eda7 1133Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc)
e440a328 1134{
1135 Function_type* fntype;
8381eda7 1136 if (no->is_function())
1137 fntype = no->func_value()->type();
1138 else if (no->is_function_declaration())
1139 fntype = no->func_declaration_value()->type();
e440a328 1140 else
c3e6f413 1141 go_unreachable();
e440a328 1142
1143 // Builtin functions are handled specially by Call_expression. We
1144 // can't take their address.
1145 if (fntype->is_builtin())
1146 {
631d5788 1147 go_error_at(loc,
1148 "invalid use of special builtin function %qs; must be called",
1149 no->message_name().c_str());
97267c39 1150 return gogo->backend()->error_expression();
e440a328 1151 }
1152
97267c39 1153 Bfunction* fndecl;
e440a328 1154 if (no->is_function())
cf3cae55 1155 fndecl = no->func_value()->get_or_make_decl(gogo, no);
e440a328 1156 else if (no->is_function_declaration())
cf3cae55 1157 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no);
e440a328 1158 else
c3e6f413 1159 go_unreachable();
e440a328 1160
97267c39 1161 return gogo->backend()->function_code_expression(fndecl, loc);
e440a328 1162}
1163
ea664253 1164// Get the backend representation for a function expression. This is used when
1165// we take the address of a function rather than simply calling it. A func
8381eda7 1166// value is represented as a pointer to a block of memory. The first
1167// word of that memory is a pointer to the function code. The
1168// remaining parts of that memory are the addresses of variables that
1169// the function closes over.
e440a328 1170
ea664253 1171Bexpression*
1172Func_expression::do_get_backend(Translate_context* context)
e440a328 1173{
8381eda7 1174 // If there is no closure, just use the function descriptor.
2010c17a 1175 if (this->closure_ == NULL)
8381eda7 1176 {
1177 Gogo* gogo = context->gogo();
1178 Named_object* no = this->function_;
1179 Expression* descriptor;
1180 if (no->is_function())
1181 descriptor = no->func_value()->descriptor(gogo, no);
1182 else if (no->is_function_declaration())
1183 {
1184 if (no->func_declaration_value()->type()->is_builtin())
1185 {
631d5788 1186 go_error_at(this->location(),
1187 ("invalid use of special builtin function %qs; "
1188 "must be called"),
1189 no->message_name().c_str());
ea664253 1190 return gogo->backend()->error_expression();
8381eda7 1191 }
1192 descriptor = no->func_declaration_value()->descriptor(gogo, no);
1193 }
1194 else
1195 go_unreachable();
2010c17a 1196
ea664253 1197 Bexpression* bdesc = descriptor->get_backend(context);
1198 return gogo->backend()->address_expression(bdesc, this->location());
8381eda7 1199 }
e440a328 1200
8381eda7 1201 go_assert(this->function_->func_value()->enclosing() != NULL);
e440a328 1202
8381eda7 1203 // If there is a closure, then the closure is itself the function
1204 // expression. It is a pointer to a struct whose first field points
1205 // to the function code and whose remaining fields are the addresses
1206 // of the closed-over variables.
ea664253 1207 return this->closure_->get_backend(context);
e440a328 1208}
1209
d751bb78 1210// Ast dump for function.
1211
1212void
1213Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1214{
8b1c301d 1215 ast_dump_context->ostream() << this->function_->name();
1216 if (this->closure_ != NULL)
1217 {
1218 ast_dump_context->ostream() << " {closure = ";
1219 this->closure_->dump_expression(ast_dump_context);
1220 ast_dump_context->ostream() << "}";
1221 }
d751bb78 1222}
1223
e440a328 1224// Make a reference to a function in an expression.
1225
1226Expression*
1227Expression::make_func_reference(Named_object* function, Expression* closure,
b13c66cd 1228 Location location)
e440a328 1229{
b1d7ecfa 1230 Func_expression* fe = new Func_expression(function, closure, location);
1231
1232 // Detect references to builtin functions and set the runtime code if
1233 // appropriate.
1234 if (function->is_function_declaration())
1235 fe->set_runtime_code(Runtime::name_to_code(function->name()));
1236 return fe;
e440a328 1237}
1238
c6837989 1239// Class Func_descriptor_expression.
8381eda7 1240
c6837989 1241// Constructor.
8381eda7 1242
c6837989 1243Func_descriptor_expression::Func_descriptor_expression(Named_object* fn)
1244 : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()),
f8bdf81a 1245 fn_(fn), dvar_(NULL)
c6837989 1246{
1247 go_assert(!fn->is_function() || !fn->func_value()->needs_closure());
1248}
8381eda7 1249
c6837989 1250// Traversal.
8381eda7 1251
c6837989 1252int
1253Func_descriptor_expression::do_traverse(Traverse*)
1254{
1255 return TRAVERSE_CONTINUE;
1256}
8381eda7 1257
1258// All function descriptors have the same type.
1259
1260Type* Func_descriptor_expression::descriptor_type;
1261
1262void
1263Func_descriptor_expression::make_func_descriptor_type()
1264{
1265 if (Func_descriptor_expression::descriptor_type != NULL)
1266 return;
1267 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1268 Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type);
1269 Func_descriptor_expression::descriptor_type =
1270 Type::make_builtin_named_type("functionDescriptor", struct_type);
1271}
1272
1273Type*
1274Func_descriptor_expression::do_type()
1275{
1276 Func_descriptor_expression::make_func_descriptor_type();
1277 return Func_descriptor_expression::descriptor_type;
1278}
1279
ea664253 1280// The backend representation for a function descriptor.
8381eda7 1281
ea664253 1282Bexpression*
1283Func_descriptor_expression::do_get_backend(Translate_context* context)
8381eda7 1284{
8381eda7 1285 Named_object* no = this->fn_;
1286 Location loc = no->location();
ea664253 1287 if (this->dvar_ != NULL)
d4e6573e 1288 return context->backend()->var_expression(this->dvar_, VE_rvalue, loc);
8381eda7 1289
ea664253 1290 Gogo* gogo = context->gogo();
8381eda7 1291 std::string var_name;
09e57698 1292 bool is_descriptor = false;
1293 if (no->is_function_declaration()
1294 && !no->func_declaration_value()->asm_name().empty()
1295 && Linemap::is_predeclared_location(no->location()))
1296 {
6098d6cb 1297 if (no->func_declaration_value()->asm_name().substr(0, 8) != "runtime.")
1298 var_name = no->func_declaration_value()->asm_name() + "_descriptor";
1299 else
1300 var_name = no->func_declaration_value()->asm_name() + "$descriptor";
09e57698 1301 is_descriptor = true;
1302 }
8381eda7 1303 else
09e57698 1304 {
1305 if (no->package() == NULL)
1306 var_name = gogo->pkgpath_symbol();
1307 else
1308 var_name = no->package()->pkgpath_symbol();
1309 var_name.push_back('.');
1310 var_name.append(Gogo::unpack_hidden_name(no->name()));
1311 var_name.append("$descriptor");
1312 }
8381eda7 1313
1314 Btype* btype = this->type()->get_backend(gogo);
1315
1316 Bvariable* bvar;
438b4bec 1317 std::string asm_name(go_selectively_encode_id(var_name));
09e57698 1318 if (no->package() != NULL || is_descriptor)
438b4bec 1319 bvar = context->backend()->immutable_struct_reference(var_name, asm_name,
1320 btype, loc);
8381eda7 1321 else
1322 {
1323 Location bloc = Linemap::predeclared_location();
1324 bool is_hidden = ((no->is_function()
1325 && no->func_value()->enclosing() != NULL)
1326 || Gogo::is_thunk(no));
438b4bec 1327 bvar = context->backend()->immutable_struct(var_name, asm_name,
1328 is_hidden, false,
8381eda7 1329 btype, bloc);
1330 Expression_list* vals = new Expression_list();
f8bdf81a 1331 vals->push_back(Expression::make_func_code_reference(this->fn_, bloc));
8381eda7 1332 Expression* init =
1333 Expression::make_struct_composite_literal(this->type(), vals, bloc);
1334 Translate_context bcontext(gogo, NULL, NULL, NULL);
1335 bcontext.set_is_const();
ea664253 1336 Bexpression* binit = init->get_backend(&bcontext);
8381eda7 1337 context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden,
1338 false, btype, bloc, binit);
1339 }
1340
1341 this->dvar_ = bvar;
d4e6573e 1342 return gogo->backend()->var_expression(bvar, VE_rvalue, loc);
8381eda7 1343}
1344
c6837989 1345// Print a function descriptor expression.
1346
1347void
1348Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const
1349{
1350 context->ostream() << "[descriptor " << this->fn_->name() << "]";
1351}
1352
8381eda7 1353// Make a function descriptor expression.
1354
c6837989 1355Func_descriptor_expression*
1356Expression::make_func_descriptor(Named_object* fn)
8381eda7 1357{
c6837989 1358 return new Func_descriptor_expression(fn);
8381eda7 1359}
1360
1361// Make the function descriptor type, so that it can be converted.
1362
1363void
1364Expression::make_func_descriptor_type()
1365{
1366 Func_descriptor_expression::make_func_descriptor_type();
1367}
1368
1369// A reference to just the code of a function.
1370
1371class Func_code_reference_expression : public Expression
1372{
1373 public:
1374 Func_code_reference_expression(Named_object* function, Location location)
1375 : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location),
1376 function_(function)
1377 { }
1378
1379 protected:
1380 int
1381 do_traverse(Traverse*)
1382 { return TRAVERSE_CONTINUE; }
1383
f9ca30f9 1384 bool
3ae06f68 1385 do_is_static_initializer() const
f9ca30f9 1386 { return true; }
1387
8381eda7 1388 Type*
1389 do_type()
1390 { return Type::make_pointer_type(Type::make_void_type()); }
1391
1392 void
1393 do_determine_type(const Type_context*)
1394 { }
1395
1396 Expression*
1397 do_copy()
1398 {
1399 return Expression::make_func_code_reference(this->function_,
1400 this->location());
1401 }
1402
ea664253 1403 Bexpression*
1404 do_get_backend(Translate_context*);
8381eda7 1405
1406 void
1407 do_dump_expression(Ast_dump_context* context) const
1408 { context->ostream() << "[raw " << this->function_->name() << "]" ; }
1409
1410 private:
1411 // The function.
1412 Named_object* function_;
1413};
1414
ea664253 1415// Get the backend representation for a reference to function code.
8381eda7 1416
ea664253 1417Bexpression*
1418Func_code_reference_expression::do_get_backend(Translate_context* context)
8381eda7 1419{
ea664253 1420 return Func_expression::get_code_pointer(context->gogo(), this->function_,
1421 this->location());
8381eda7 1422}
1423
1424// Make a reference to the code of a function.
1425
1426Expression*
1427Expression::make_func_code_reference(Named_object* function, Location location)
1428{
1429 return new Func_code_reference_expression(function, location);
1430}
1431
e440a328 1432// Class Unknown_expression.
1433
1434// Return the name of an unknown expression.
1435
1436const std::string&
1437Unknown_expression::name() const
1438{
1439 return this->named_object_->name();
1440}
1441
1442// Lower a reference to an unknown name.
1443
1444Expression*
ceeb4318 1445Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 1446{
b13c66cd 1447 Location location = this->location();
e440a328 1448 Named_object* no = this->named_object_;
deded542 1449 Named_object* real;
1450 if (!no->is_unknown())
1451 real = no;
1452 else
e440a328 1453 {
deded542 1454 real = no->unknown_value()->real_named_object();
1455 if (real == NULL)
1456 {
1457 if (this->is_composite_literal_key_)
1458 return this;
acf8e158 1459 if (!this->no_error_message_)
631d5788 1460 go_error_at(location, "reference to undefined name %qs",
1461 this->named_object_->message_name().c_str());
deded542 1462 return Expression::make_error(location);
1463 }
e440a328 1464 }
1465 switch (real->classification())
1466 {
1467 case Named_object::NAMED_OBJECT_CONST:
1468 return Expression::make_const_reference(real, location);
1469 case Named_object::NAMED_OBJECT_TYPE:
1470 return Expression::make_type(real->type_value(), location);
1471 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1472 if (this->is_composite_literal_key_)
1473 return this;
acf8e158 1474 if (!this->no_error_message_)
631d5788 1475 go_error_at(location, "reference to undefined type %qs",
1476 real->message_name().c_str());
e440a328 1477 return Expression::make_error(location);
1478 case Named_object::NAMED_OBJECT_VAR:
7d834090 1479 real->var_value()->set_is_used();
e440a328 1480 return Expression::make_var_reference(real, location);
1481 case Named_object::NAMED_OBJECT_FUNC:
1482 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1483 return Expression::make_func_reference(real, NULL, location);
1484 case Named_object::NAMED_OBJECT_PACKAGE:
1485 if (this->is_composite_literal_key_)
1486 return this;
acf8e158 1487 if (!this->no_error_message_)
631d5788 1488 go_error_at(location, "unexpected reference to package");
e440a328 1489 return Expression::make_error(location);
1490 default:
c3e6f413 1491 go_unreachable();
e440a328 1492 }
1493}
1494
d751bb78 1495// Dump the ast representation for an unknown expression to a dump context.
1496
1497void
1498Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1499{
1500 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1501 << ")";
d751bb78 1502}
1503
e440a328 1504// Make a reference to an unknown name.
1505
acf8e158 1506Unknown_expression*
b13c66cd 1507Expression::make_unknown_reference(Named_object* no, Location location)
e440a328 1508{
e440a328 1509 return new Unknown_expression(no, location);
1510}
1511
1512// A boolean expression.
1513
1514class Boolean_expression : public Expression
1515{
1516 public:
b13c66cd 1517 Boolean_expression(bool val, Location location)
e440a328 1518 : Expression(EXPRESSION_BOOLEAN, location),
1519 val_(val), type_(NULL)
1520 { }
1521
1522 static Expression*
1523 do_import(Import*);
1524
1525 protected:
1526 bool
1527 do_is_constant() const
1528 { return true; }
1529
0e168074 1530 bool
3ae06f68 1531 do_is_static_initializer() const
0e168074 1532 { return true; }
1533
e440a328 1534 Type*
1535 do_type();
1536
1537 void
1538 do_determine_type(const Type_context*);
1539
1540 Expression*
1541 do_copy()
1542 { return this; }
1543
ea664253 1544 Bexpression*
1545 do_get_backend(Translate_context* context)
1546 { return context->backend()->boolean_constant_expression(this->val_); }
e440a328 1547
1548 void
1549 do_export(Export* exp) const
1550 { exp->write_c_string(this->val_ ? "true" : "false"); }
1551
d751bb78 1552 void
1553 do_dump_expression(Ast_dump_context* ast_dump_context) const
1554 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1555
e440a328 1556 private:
1557 // The constant.
1558 bool val_;
1559 // The type as determined by context.
1560 Type* type_;
1561};
1562
1563// Get the type.
1564
1565Type*
1566Boolean_expression::do_type()
1567{
1568 if (this->type_ == NULL)
1569 this->type_ = Type::make_boolean_type();
1570 return this->type_;
1571}
1572
1573// Set the type from the context.
1574
1575void
1576Boolean_expression::do_determine_type(const Type_context* context)
1577{
1578 if (this->type_ != NULL && !this->type_->is_abstract())
1579 ;
1580 else if (context->type != NULL && context->type->is_boolean_type())
1581 this->type_ = context->type;
1582 else if (!context->may_be_abstract)
1583 this->type_ = Type::lookup_bool_type();
1584}
1585
1586// Import a boolean constant.
1587
1588Expression*
1589Boolean_expression::do_import(Import* imp)
1590{
1591 if (imp->peek_char() == 't')
1592 {
1593 imp->require_c_string("true");
1594 return Expression::make_boolean(true, imp->location());
1595 }
1596 else
1597 {
1598 imp->require_c_string("false");
1599 return Expression::make_boolean(false, imp->location());
1600 }
1601}
1602
1603// Make a boolean expression.
1604
1605Expression*
b13c66cd 1606Expression::make_boolean(bool val, Location location)
e440a328 1607{
1608 return new Boolean_expression(val, location);
1609}
1610
1611// Class String_expression.
1612
1613// Get the type.
1614
1615Type*
1616String_expression::do_type()
1617{
1618 if (this->type_ == NULL)
1619 this->type_ = Type::make_string_type();
1620 return this->type_;
1621}
1622
1623// Set the type from the context.
1624
1625void
1626String_expression::do_determine_type(const Type_context* context)
1627{
1628 if (this->type_ != NULL && !this->type_->is_abstract())
1629 ;
1630 else if (context->type != NULL && context->type->is_string_type())
1631 this->type_ = context->type;
1632 else if (!context->may_be_abstract)
1633 this->type_ = Type::lookup_string_type();
1634}
1635
1636// Build a string constant.
1637
ea664253 1638Bexpression*
1639String_expression::do_get_backend(Translate_context* context)
e440a328 1640{
2c809f8f 1641 Gogo* gogo = context->gogo();
1642 Btype* btype = Type::make_string_type()->get_backend(gogo);
1643
1644 Location loc = this->location();
1645 std::vector<Bexpression*> init(2);
1646 Bexpression* str_cst =
1647 gogo->backend()->string_constant_expression(this->val_);
1648 init[0] = gogo->backend()->address_expression(str_cst, loc);
1649
1650 Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo);
1651 mpz_t lenval;
1652 mpz_init_set_ui(lenval, this->val_.length());
1653 init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval);
1654 mpz_clear(lenval);
1655
ea664253 1656 return gogo->backend()->constructor_expression(btype, init, loc);
e440a328 1657}
1658
8b1c301d 1659 // Write string literal to string dump.
e440a328 1660
1661void
8b1c301d 1662String_expression::export_string(String_dump* exp,
1663 const String_expression* str)
e440a328 1664{
1665 std::string s;
8b1c301d 1666 s.reserve(str->val_.length() * 4 + 2);
e440a328 1667 s += '"';
8b1c301d 1668 for (std::string::const_iterator p = str->val_.begin();
1669 p != str->val_.end();
e440a328 1670 ++p)
1671 {
1672 if (*p == '\\' || *p == '"')
1673 {
1674 s += '\\';
1675 s += *p;
1676 }
1677 else if (*p >= 0x20 && *p < 0x7f)
1678 s += *p;
1679 else if (*p == '\n')
1680 s += "\\n";
1681 else if (*p == '\t')
1682 s += "\\t";
1683 else
1684 {
1685 s += "\\x";
1686 unsigned char c = *p;
1687 unsigned int dig = c >> 4;
1688 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1689 dig = c & 0xf;
1690 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1691 }
1692 }
1693 s += '"';
1694 exp->write_string(s);
1695}
1696
8b1c301d 1697// Export a string expression.
1698
1699void
1700String_expression::do_export(Export* exp) const
1701{
1702 String_expression::export_string(exp, this);
1703}
1704
e440a328 1705// Import a string expression.
1706
1707Expression*
1708String_expression::do_import(Import* imp)
1709{
1710 imp->require_c_string("\"");
1711 std::string val;
1712 while (true)
1713 {
1714 int c = imp->get_char();
1715 if (c == '"' || c == -1)
1716 break;
1717 if (c != '\\')
1718 val += static_cast<char>(c);
1719 else
1720 {
1721 c = imp->get_char();
1722 if (c == '\\' || c == '"')
1723 val += static_cast<char>(c);
1724 else if (c == 'n')
1725 val += '\n';
1726 else if (c == 't')
1727 val += '\t';
1728 else if (c == 'x')
1729 {
1730 c = imp->get_char();
1731 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1732 c = imp->get_char();
1733 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1734 char v = (vh << 4) | vl;
1735 val += v;
1736 }
1737 else
1738 {
631d5788 1739 go_error_at(imp->location(), "bad string constant");
e440a328 1740 return Expression::make_error(imp->location());
1741 }
1742 }
1743 }
1744 return Expression::make_string(val, imp->location());
1745}
1746
d751bb78 1747// Ast dump for string expression.
1748
1749void
1750String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1751{
8b1c301d 1752 String_expression::export_string(ast_dump_context, this);
d751bb78 1753}
1754
e440a328 1755// Make a string expression.
1756
1757Expression*
b13c66cd 1758Expression::make_string(const std::string& val, Location location)
e440a328 1759{
1760 return new String_expression(val, location);
1761}
1762
2c809f8f 1763// An expression that evaluates to some characteristic of a string.
1764// This is used when indexing, bound-checking, or nil checking a string.
1765
1766class String_info_expression : public Expression
1767{
1768 public:
1769 String_info_expression(Expression* string, String_info string_info,
1770 Location location)
1771 : Expression(EXPRESSION_STRING_INFO, location),
1772 string_(string), string_info_(string_info)
1773 { }
1774
1775 protected:
1776 Type*
1777 do_type();
1778
1779 void
1780 do_determine_type(const Type_context*)
1781 { go_unreachable(); }
1782
1783 Expression*
1784 do_copy()
1785 {
1786 return new String_info_expression(this->string_->copy(), this->string_info_,
1787 this->location());
1788 }
1789
ea664253 1790 Bexpression*
1791 do_get_backend(Translate_context* context);
2c809f8f 1792
1793 void
1794 do_dump_expression(Ast_dump_context*) const;
1795
1796 void
1797 do_issue_nil_check()
1798 { this->string_->issue_nil_check(); }
1799
1800 private:
1801 // The string for which we are getting information.
1802 Expression* string_;
1803 // What information we want.
1804 String_info string_info_;
1805};
1806
1807// Return the type of the string info.
1808
1809Type*
1810String_info_expression::do_type()
1811{
1812 switch (this->string_info_)
1813 {
1814 case STRING_INFO_DATA:
1815 {
1816 Type* byte_type = Type::lookup_integer_type("uint8");
1817 return Type::make_pointer_type(byte_type);
1818 }
1819 case STRING_INFO_LENGTH:
1820 return Type::lookup_integer_type("int");
1821 default:
1822 go_unreachable();
1823 }
1824}
1825
1826// Return string information in GENERIC.
1827
ea664253 1828Bexpression*
1829String_info_expression::do_get_backend(Translate_context* context)
2c809f8f 1830{
1831 Gogo* gogo = context->gogo();
1832
ea664253 1833 Bexpression* bstring = this->string_->get_backend(context);
2c809f8f 1834 switch (this->string_info_)
1835 {
1836 case STRING_INFO_DATA:
1837 case STRING_INFO_LENGTH:
ea664253 1838 return gogo->backend()->struct_field_expression(bstring,
1839 this->string_info_,
1840 this->location());
2c809f8f 1841 break;
1842 default:
1843 go_unreachable();
1844 }
2c809f8f 1845}
1846
1847// Dump ast representation for a type info expression.
1848
1849void
1850String_info_expression::do_dump_expression(
1851 Ast_dump_context* ast_dump_context) const
1852{
1853 ast_dump_context->ostream() << "stringinfo(";
1854 this->string_->dump_expression(ast_dump_context);
1855 ast_dump_context->ostream() << ",";
1856 ast_dump_context->ostream() <<
1857 (this->string_info_ == STRING_INFO_DATA ? "data"
1858 : this->string_info_ == STRING_INFO_LENGTH ? "length"
1859 : "unknown");
1860 ast_dump_context->ostream() << ")";
1861}
1862
1863// Make a string info expression.
1864
1865Expression*
1866Expression::make_string_info(Expression* string, String_info string_info,
1867 Location location)
1868{
1869 return new String_info_expression(string, string_info, location);
1870}
1871
e440a328 1872// Make an integer expression.
1873
1874class Integer_expression : public Expression
1875{
1876 public:
5d4b8566 1877 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1878 Location location)
e440a328 1879 : Expression(EXPRESSION_INTEGER, location),
5d4b8566 1880 type_(type), is_character_constant_(is_character_constant)
e440a328 1881 { mpz_init_set(this->val_, *val); }
1882
1883 static Expression*
1884 do_import(Import*);
1885
8b1c301d 1886 // Write VAL to string dump.
e440a328 1887 static void
8b1c301d 1888 export_integer(String_dump* exp, const mpz_t val);
e440a328 1889
d751bb78 1890 // Write VAL to dump context.
1891 static void
1892 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1893
e440a328 1894 protected:
1895 bool
1896 do_is_constant() const
1897 { return true; }
1898
0e168074 1899 bool
3ae06f68 1900 do_is_static_initializer() const
0e168074 1901 { return true; }
1902
e440a328 1903 bool
0c77715b 1904 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 1905
1906 Type*
1907 do_type();
1908
1909 void
1910 do_determine_type(const Type_context* context);
1911
1912 void
1913 do_check_types(Gogo*);
1914
ea664253 1915 Bexpression*
1916 do_get_backend(Translate_context*);
e440a328 1917
1918 Expression*
1919 do_copy()
5d4b8566 1920 {
1921 if (this->is_character_constant_)
1922 return Expression::make_character(&this->val_, this->type_,
1923 this->location());
1924 else
e67508fa 1925 return Expression::make_integer_z(&this->val_, this->type_,
1926 this->location());
5d4b8566 1927 }
e440a328 1928
1929 void
1930 do_export(Export*) const;
1931
d751bb78 1932 void
1933 do_dump_expression(Ast_dump_context*) const;
1934
e440a328 1935 private:
1936 // The integer value.
1937 mpz_t val_;
1938 // The type so far.
1939 Type* type_;
5d4b8566 1940 // Whether this is a character constant.
1941 bool is_character_constant_;
e440a328 1942};
1943
0c77715b 1944// Return a numeric constant for this expression. We have to mark
1945// this as a character when appropriate.
e440a328 1946
1947bool
0c77715b 1948Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 1949{
0c77715b 1950 if (this->is_character_constant_)
1951 nc->set_rune(this->type_, this->val_);
1952 else
1953 nc->set_int(this->type_, this->val_);
e440a328 1954 return true;
1955}
1956
1957// Return the current type. If we haven't set the type yet, we return
1958// an abstract integer type.
1959
1960Type*
1961Integer_expression::do_type()
1962{
1963 if (this->type_ == NULL)
5d4b8566 1964 {
1965 if (this->is_character_constant_)
1966 this->type_ = Type::make_abstract_character_type();
1967 else
1968 this->type_ = Type::make_abstract_integer_type();
1969 }
e440a328 1970 return this->type_;
1971}
1972
1973// Set the type of the integer value. Here we may switch from an
1974// abstract type to a real type.
1975
1976void
1977Integer_expression::do_determine_type(const Type_context* context)
1978{
1979 if (this->type_ != NULL && !this->type_->is_abstract())
1980 ;
0c77715b 1981 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 1982 this->type_ = context->type;
1983 else if (!context->may_be_abstract)
5d4b8566 1984 {
1985 if (this->is_character_constant_)
1986 this->type_ = Type::lookup_integer_type("int32");
1987 else
1988 this->type_ = Type::lookup_integer_type("int");
1989 }
e440a328 1990}
1991
e440a328 1992// Check the type of an integer constant.
1993
1994void
1995Integer_expression::do_check_types(Gogo*)
1996{
0c77715b 1997 Type* type = this->type_;
1998 if (type == NULL)
e440a328 1999 return;
0c77715b 2000 Numeric_constant nc;
2001 if (this->is_character_constant_)
2002 nc.set_rune(NULL, this->val_);
2003 else
2004 nc.set_int(NULL, this->val_);
2005 if (!nc.set_type(type, true, this->location()))
e440a328 2006 this->set_is_error();
2007}
2008
ea664253 2009// Get the backend representation for an integer constant.
e440a328 2010
ea664253 2011Bexpression*
2012Integer_expression::do_get_backend(Translate_context* context)
e440a328 2013{
12373dd5 2014 if (this->is_error_expression()
2015 || (this->type_ != NULL && this->type_->is_error_type()))
2016 {
2017 go_assert(saw_errors());
2018 return context->gogo()->backend()->error_expression();
2019 }
2020
48c2a53a 2021 Type* resolved_type = NULL;
e440a328 2022 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2023 resolved_type = this->type_;
e440a328 2024 else if (this->type_ != NULL && this->type_->float_type() != NULL)
2025 {
2026 // We are converting to an abstract floating point type.
48c2a53a 2027 resolved_type = Type::lookup_float_type("float64");
e440a328 2028 }
2029 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
2030 {
2031 // We are converting to an abstract complex type.
48c2a53a 2032 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2033 }
2034 else
2035 {
2036 // If we still have an abstract type here, then this is being
2037 // used in a constant expression which didn't get reduced for
2038 // some reason. Use a type which will fit the value. We use <,
2039 // not <=, because we need an extra bit for the sign bit.
2040 int bits = mpz_sizeinbase(this->val_, 2);
1b1f2abf 2041 Type* int_type = Type::lookup_integer_type("int");
2042 if (bits < int_type->integer_type()->bits())
48c2a53a 2043 resolved_type = int_type;
e440a328 2044 else if (bits < 64)
48c2a53a 2045 resolved_type = Type::lookup_integer_type("int64");
e440a328 2046 else
48c2a53a 2047 {
2048 if (!saw_errors())
631d5788 2049 go_error_at(this->location(),
2050 "unknown type for large integer constant");
ea664253 2051 return context->gogo()->backend()->error_expression();
48c2a53a 2052 }
e440a328 2053 }
48c2a53a 2054 Numeric_constant nc;
2055 nc.set_int(resolved_type, this->val_);
ea664253 2056 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2057}
2058
2059// Write VAL to export data.
2060
2061void
8b1c301d 2062Integer_expression::export_integer(String_dump* exp, const mpz_t val)
e440a328 2063{
2064 char* s = mpz_get_str(NULL, 10, val);
2065 exp->write_c_string(s);
2066 free(s);
2067}
2068
2069// Export an integer in a constant expression.
2070
2071void
2072Integer_expression::do_export(Export* exp) const
2073{
2074 Integer_expression::export_integer(exp, this->val_);
5d4b8566 2075 if (this->is_character_constant_)
2076 exp->write_c_string("'");
e440a328 2077 // A trailing space lets us reliably identify the end of the number.
2078 exp->write_c_string(" ");
2079}
2080
2081// Import an integer, floating point, or complex value. This handles
2082// all these types because they all start with digits.
2083
2084Expression*
2085Integer_expression::do_import(Import* imp)
2086{
2087 std::string num = imp->read_identifier();
2088 imp->require_c_string(" ");
2089 if (!num.empty() && num[num.length() - 1] == 'i')
2090 {
2091 mpfr_t real;
2092 size_t plus_pos = num.find('+', 1);
2093 size_t minus_pos = num.find('-', 1);
2094 size_t pos;
2095 if (plus_pos == std::string::npos)
2096 pos = minus_pos;
2097 else if (minus_pos == std::string::npos)
2098 pos = plus_pos;
2099 else
2100 {
631d5788 2101 go_error_at(imp->location(), "bad number in import data: %qs",
2102 num.c_str());
e440a328 2103 return Expression::make_error(imp->location());
2104 }
2105 if (pos == std::string::npos)
2106 mpfr_set_ui(real, 0, GMP_RNDN);
2107 else
2108 {
2109 std::string real_str = num.substr(0, pos);
2110 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
2111 {
631d5788 2112 go_error_at(imp->location(), "bad number in import data: %qs",
2113 real_str.c_str());
e440a328 2114 return Expression::make_error(imp->location());
2115 }
2116 }
2117
2118 std::string imag_str;
2119 if (pos == std::string::npos)
2120 imag_str = num;
2121 else
2122 imag_str = num.substr(pos);
2123 imag_str = imag_str.substr(0, imag_str.size() - 1);
2124 mpfr_t imag;
2125 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2126 {
631d5788 2127 go_error_at(imp->location(), "bad number in import data: %qs",
2128 imag_str.c_str());
e440a328 2129 return Expression::make_error(imp->location());
2130 }
fcbea5e4 2131 mpc_t cval;
2132 mpc_init2(cval, mpc_precision);
2133 mpc_set_fr_fr(cval, real, imag, MPC_RNDNN);
e440a328 2134 mpfr_clear(real);
2135 mpfr_clear(imag);
fcbea5e4 2136 Expression* ret = Expression::make_complex(&cval, NULL, imp->location());
2137 mpc_clear(cval);
e440a328 2138 return ret;
2139 }
2140 else if (num.find('.') == std::string::npos
2141 && num.find('E') == std::string::npos)
2142 {
5d4b8566 2143 bool is_character_constant = (!num.empty()
2144 && num[num.length() - 1] == '\'');
2145 if (is_character_constant)
2146 num = num.substr(0, num.length() - 1);
e440a328 2147 mpz_t val;
2148 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2149 {
631d5788 2150 go_error_at(imp->location(), "bad number in import data: %qs",
2151 num.c_str());
e440a328 2152 return Expression::make_error(imp->location());
2153 }
5d4b8566 2154 Expression* ret;
2155 if (is_character_constant)
2156 ret = Expression::make_character(&val, NULL, imp->location());
2157 else
e67508fa 2158 ret = Expression::make_integer_z(&val, NULL, imp->location());
e440a328 2159 mpz_clear(val);
2160 return ret;
2161 }
2162 else
2163 {
2164 mpfr_t val;
2165 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2166 {
631d5788 2167 go_error_at(imp->location(), "bad number in import data: %qs",
2168 num.c_str());
e440a328 2169 return Expression::make_error(imp->location());
2170 }
2171 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2172 mpfr_clear(val);
2173 return ret;
2174 }
2175}
d751bb78 2176// Ast dump for integer expression.
2177
2178void
2179Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2180{
5d4b8566 2181 if (this->is_character_constant_)
2182 ast_dump_context->ostream() << '\'';
8b1c301d 2183 Integer_expression::export_integer(ast_dump_context, this->val_);
5d4b8566 2184 if (this->is_character_constant_)
2185 ast_dump_context->ostream() << '\'';
d751bb78 2186}
2187
e67508fa 2188// Build a new integer value from a multi-precision integer.
e440a328 2189
2190Expression*
e67508fa 2191Expression::make_integer_z(const mpz_t* val, Type* type, Location location)
5d4b8566 2192{
2193 return new Integer_expression(val, type, false, location);
2194}
2195
e67508fa 2196// Build a new integer value from an unsigned long.
2197
2198Expression*
2199Expression::make_integer_ul(unsigned long val, Type *type, Location location)
2200{
2201 mpz_t zval;
2202 mpz_init_set_ui(zval, val);
2203 Expression* ret = Expression::make_integer_z(&zval, type, location);
2204 mpz_clear(zval);
2205 return ret;
2206}
2207
2208// Build a new integer value from a signed long.
2209
2210Expression*
2211Expression::make_integer_sl(long val, Type *type, Location location)
2212{
2213 mpz_t zval;
2214 mpz_init_set_si(zval, val);
2215 Expression* ret = Expression::make_integer_z(&zval, type, location);
2216 mpz_clear(zval);
2217 return ret;
2218}
2219
3f378015 2220// Store an int64_t in an uninitialized mpz_t.
2221
2222static void
2223set_mpz_from_int64(mpz_t* zval, int64_t val)
2224{
2225 if (val >= 0)
2226 {
2227 unsigned long ul = static_cast<unsigned long>(val);
2228 if (static_cast<int64_t>(ul) == val)
2229 {
2230 mpz_init_set_ui(*zval, ul);
2231 return;
2232 }
2233 }
2234 uint64_t uv;
2235 if (val >= 0)
2236 uv = static_cast<uint64_t>(val);
2237 else
2238 uv = static_cast<uint64_t>(- val);
2239 unsigned long ul = uv & 0xffffffffUL;
2240 mpz_init_set_ui(*zval, ul);
2241 mpz_t hval;
2242 mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32));
2243 mpz_mul_2exp(hval, hval, 32);
2244 mpz_add(*zval, *zval, hval);
2245 mpz_clear(hval);
2246 if (val < 0)
2247 mpz_neg(*zval, *zval);
2248}
2249
2250// Build a new integer value from an int64_t.
2251
2252Expression*
2253Expression::make_integer_int64(int64_t val, Type* type, Location location)
2254{
2255 mpz_t zval;
2256 set_mpz_from_int64(&zval, val);
2257 Expression* ret = Expression::make_integer_z(&zval, type, location);
2258 mpz_clear(zval);
2259 return ret;
2260}
2261
5d4b8566 2262// Build a new character constant value.
2263
2264Expression*
2265Expression::make_character(const mpz_t* val, Type* type, Location location)
e440a328 2266{
5d4b8566 2267 return new Integer_expression(val, type, true, location);
e440a328 2268}
2269
2270// Floats.
2271
2272class Float_expression : public Expression
2273{
2274 public:
b13c66cd 2275 Float_expression(const mpfr_t* val, Type* type, Location location)
e440a328 2276 : Expression(EXPRESSION_FLOAT, location),
2277 type_(type)
2278 {
2279 mpfr_init_set(this->val_, *val, GMP_RNDN);
2280 }
2281
e440a328 2282 // Write VAL to export data.
2283 static void
8b1c301d 2284 export_float(String_dump* exp, const mpfr_t val);
2285
d751bb78 2286 // Write VAL to dump file.
2287 static void
2288 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
e440a328 2289
2290 protected:
2291 bool
2292 do_is_constant() const
2293 { return true; }
2294
0e168074 2295 bool
3ae06f68 2296 do_is_static_initializer() const
0e168074 2297 { return true; }
2298
e440a328 2299 bool
0c77715b 2300 do_numeric_constant_value(Numeric_constant* nc) const
2301 {
2302 nc->set_float(this->type_, this->val_);
2303 return true;
2304 }
e440a328 2305
2306 Type*
2307 do_type();
2308
2309 void
2310 do_determine_type(const Type_context*);
2311
2312 void
2313 do_check_types(Gogo*);
2314
2315 Expression*
2316 do_copy()
2317 { return Expression::make_float(&this->val_, this->type_,
2318 this->location()); }
2319
ea664253 2320 Bexpression*
2321 do_get_backend(Translate_context*);
e440a328 2322
2323 void
2324 do_export(Export*) const;
2325
d751bb78 2326 void
2327 do_dump_expression(Ast_dump_context*) const;
2328
e440a328 2329 private:
2330 // The floating point value.
2331 mpfr_t val_;
2332 // The type so far.
2333 Type* type_;
2334};
2335
e440a328 2336// Return the current type. If we haven't set the type yet, we return
2337// an abstract float type.
2338
2339Type*
2340Float_expression::do_type()
2341{
2342 if (this->type_ == NULL)
2343 this->type_ = Type::make_abstract_float_type();
2344 return this->type_;
2345}
2346
2347// Set the type of the float value. Here we may switch from an
2348// abstract type to a real type.
2349
2350void
2351Float_expression::do_determine_type(const Type_context* context)
2352{
2353 if (this->type_ != NULL && !this->type_->is_abstract())
2354 ;
2355 else if (context->type != NULL
2356 && (context->type->integer_type() != NULL
2357 || context->type->float_type() != NULL
2358 || context->type->complex_type() != NULL))
2359 this->type_ = context->type;
2360 else if (!context->may_be_abstract)
48080209 2361 this->type_ = Type::lookup_float_type("float64");
e440a328 2362}
2363
e440a328 2364// Check the type of a float value.
2365
2366void
2367Float_expression::do_check_types(Gogo*)
2368{
0c77715b 2369 Type* type = this->type_;
2370 if (type == NULL)
e440a328 2371 return;
0c77715b 2372 Numeric_constant nc;
2373 nc.set_float(NULL, this->val_);
2374 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2375 this->set_is_error();
e440a328 2376}
2377
ea664253 2378// Get the backend representation for a float constant.
e440a328 2379
ea664253 2380Bexpression*
2381Float_expression::do_get_backend(Translate_context* context)
e440a328 2382{
12373dd5 2383 if (this->is_error_expression()
2384 || (this->type_ != NULL && this->type_->is_error_type()))
2385 {
2386 go_assert(saw_errors());
2387 return context->gogo()->backend()->error_expression();
2388 }
2389
48c2a53a 2390 Type* resolved_type;
e440a328 2391 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2392 resolved_type = this->type_;
e440a328 2393 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2394 {
2395 // We have an abstract integer type. We just hope for the best.
48c2a53a 2396 resolved_type = Type::lookup_integer_type("int");
2397 }
2398 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
2399 {
2400 // We are converting to an abstract complex type.
2401 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2402 }
2403 else
2404 {
2405 // If we still have an abstract type here, then this is being
2406 // used in a constant expression which didn't get reduced. We
2407 // just use float64 and hope for the best.
48c2a53a 2408 resolved_type = Type::lookup_float_type("float64");
e440a328 2409 }
48c2a53a 2410
2411 Numeric_constant nc;
2412 nc.set_float(resolved_type, this->val_);
ea664253 2413 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2414}
2415
8b1c301d 2416// Write a floating point number to a string dump.
e440a328 2417
2418void
8b1c301d 2419Float_expression::export_float(String_dump *exp, const mpfr_t val)
e440a328 2420{
2421 mp_exp_t exponent;
2422 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2423 if (*s == '-')
2424 exp->write_c_string("-");
2425 exp->write_c_string("0.");
2426 exp->write_c_string(*s == '-' ? s + 1 : s);
2427 mpfr_free_str(s);
2428 char buf[30];
2429 snprintf(buf, sizeof buf, "E%ld", exponent);
2430 exp->write_c_string(buf);
2431}
2432
2433// Export a floating point number in a constant expression.
2434
2435void
2436Float_expression::do_export(Export* exp) const
2437{
2438 Float_expression::export_float(exp, this->val_);
2439 // A trailing space lets us reliably identify the end of the number.
2440 exp->write_c_string(" ");
2441}
2442
d751bb78 2443// Dump a floating point number to the dump file.
2444
2445void
2446Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2447{
8b1c301d 2448 Float_expression::export_float(ast_dump_context, this->val_);
d751bb78 2449}
2450
e440a328 2451// Make a float expression.
2452
2453Expression*
b13c66cd 2454Expression::make_float(const mpfr_t* val, Type* type, Location location)
e440a328 2455{
2456 return new Float_expression(val, type, location);
2457}
2458
2459// Complex numbers.
2460
2461class Complex_expression : public Expression
2462{
2463 public:
fcbea5e4 2464 Complex_expression(const mpc_t* val, Type* type, Location location)
e440a328 2465 : Expression(EXPRESSION_COMPLEX, location),
2466 type_(type)
2467 {
fcbea5e4 2468 mpc_init2(this->val_, mpc_precision);
2469 mpc_set(this->val_, *val, MPC_RNDNN);
e440a328 2470 }
2471
fcbea5e4 2472 // Write VAL to string dump.
e440a328 2473 static void
fcbea5e4 2474 export_complex(String_dump* exp, const mpc_t val);
e440a328 2475
d751bb78 2476 // Write REAL/IMAG to dump context.
2477 static void
fcbea5e4 2478 dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val);
d751bb78 2479
e440a328 2480 protected:
2481 bool
2482 do_is_constant() const
2483 { return true; }
2484
0e168074 2485 bool
3ae06f68 2486 do_is_static_initializer() const
0e168074 2487 { return true; }
2488
e440a328 2489 bool
0c77715b 2490 do_numeric_constant_value(Numeric_constant* nc) const
2491 {
fcbea5e4 2492 nc->set_complex(this->type_, this->val_);
0c77715b 2493 return true;
2494 }
e440a328 2495
2496 Type*
2497 do_type();
2498
2499 void
2500 do_determine_type(const Type_context*);
2501
2502 void
2503 do_check_types(Gogo*);
2504
2505 Expression*
2506 do_copy()
2507 {
fcbea5e4 2508 return Expression::make_complex(&this->val_, this->type_,
e440a328 2509 this->location());
2510 }
2511
ea664253 2512 Bexpression*
2513 do_get_backend(Translate_context*);
e440a328 2514
2515 void
2516 do_export(Export*) const;
2517
d751bb78 2518 void
2519 do_dump_expression(Ast_dump_context*) const;
abd26de0 2520
e440a328 2521 private:
fcbea5e4 2522 // The complex value.
2523 mpc_t val_;
e440a328 2524 // The type if known.
2525 Type* type_;
2526};
2527
e440a328 2528// Return the current type. If we haven't set the type yet, we return
2529// an abstract complex type.
2530
2531Type*
2532Complex_expression::do_type()
2533{
2534 if (this->type_ == NULL)
2535 this->type_ = Type::make_abstract_complex_type();
2536 return this->type_;
2537}
2538
2539// Set the type of the complex value. Here we may switch from an
2540// abstract type to a real type.
2541
2542void
2543Complex_expression::do_determine_type(const Type_context* context)
2544{
2545 if (this->type_ != NULL && !this->type_->is_abstract())
2546 ;
abd26de0 2547 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 2548 this->type_ = context->type;
2549 else if (!context->may_be_abstract)
48080209 2550 this->type_ = Type::lookup_complex_type("complex128");
e440a328 2551}
2552
e440a328 2553// Check the type of a complex value.
2554
2555void
2556Complex_expression::do_check_types(Gogo*)
2557{
0c77715b 2558 Type* type = this->type_;
2559 if (type == NULL)
e440a328 2560 return;
0c77715b 2561 Numeric_constant nc;
fcbea5e4 2562 nc.set_complex(NULL, this->val_);
0c77715b 2563 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2564 this->set_is_error();
2565}
2566
ea664253 2567// Get the backend representation for a complex constant.
e440a328 2568
ea664253 2569Bexpression*
2570Complex_expression::do_get_backend(Translate_context* context)
e440a328 2571{
12373dd5 2572 if (this->is_error_expression()
2573 || (this->type_ != NULL && this->type_->is_error_type()))
2574 {
2575 go_assert(saw_errors());
2576 return context->gogo()->backend()->error_expression();
2577 }
2578
48c2a53a 2579 Type* resolved_type;
e440a328 2580 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2581 resolved_type = this->type_;
2582 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2583 {
2584 // We are converting to an abstract integer type.
2585 resolved_type = Type::lookup_integer_type("int");
2586 }
2587 else if (this->type_ != NULL && this->type_->float_type() != NULL)
2588 {
2589 // We are converting to an abstract float type.
2590 resolved_type = Type::lookup_float_type("float64");
2591 }
e440a328 2592 else
2593 {
47ae02b7 2594 // If we still have an abstract type here, this is being
e440a328 2595 // used in a constant expression which didn't get reduced. We
2596 // just use complex128 and hope for the best.
48c2a53a 2597 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2598 }
48c2a53a 2599
2600 Numeric_constant nc;
fcbea5e4 2601 nc.set_complex(resolved_type, this->val_);
ea664253 2602 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2603}
2604
2605// Write REAL/IMAG to export data.
2606
2607void
fcbea5e4 2608Complex_expression::export_complex(String_dump* exp, const mpc_t val)
e440a328 2609{
fcbea5e4 2610 if (!mpfr_zero_p(mpc_realref(val)))
e440a328 2611 {
fcbea5e4 2612 Float_expression::export_float(exp, mpc_realref(val));
d1db782d 2613 if (mpfr_sgn(mpc_imagref(val)) >= 0)
e440a328 2614 exp->write_c_string("+");
2615 }
fcbea5e4 2616 Float_expression::export_float(exp, mpc_imagref(val));
e440a328 2617 exp->write_c_string("i");
2618}
2619
2620// Export a complex number in a constant expression.
2621
2622void
2623Complex_expression::do_export(Export* exp) const
2624{
fcbea5e4 2625 Complex_expression::export_complex(exp, this->val_);
e440a328 2626 // A trailing space lets us reliably identify the end of the number.
2627 exp->write_c_string(" ");
2628}
2629
d751bb78 2630// Dump a complex expression to the dump file.
2631
2632void
2633Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2634{
fcbea5e4 2635 Complex_expression::export_complex(ast_dump_context, this->val_);
d751bb78 2636}
2637
e440a328 2638// Make a complex expression.
2639
2640Expression*
fcbea5e4 2641Expression::make_complex(const mpc_t* val, Type* type, Location location)
e440a328 2642{
fcbea5e4 2643 return new Complex_expression(val, type, location);
e440a328 2644}
2645
d5b605df 2646// Find a named object in an expression.
2647
2648class Find_named_object : public Traverse
2649{
2650 public:
2651 Find_named_object(Named_object* no)
2652 : Traverse(traverse_expressions),
2653 no_(no), found_(false)
2654 { }
2655
2656 // Whether we found the object.
2657 bool
2658 found() const
2659 { return this->found_; }
2660
2661 protected:
2662 int
2663 expression(Expression**);
2664
2665 private:
2666 // The object we are looking for.
2667 Named_object* no_;
2668 // Whether we found it.
2669 bool found_;
2670};
2671
e440a328 2672// A reference to a const in an expression.
2673
2674class Const_expression : public Expression
2675{
2676 public:
b13c66cd 2677 Const_expression(Named_object* constant, Location location)
e440a328 2678 : Expression(EXPRESSION_CONST_REFERENCE, location),
13e818f5 2679 constant_(constant), type_(NULL), seen_(false)
e440a328 2680 { }
2681
d5b605df 2682 Named_object*
2683 named_object()
2684 { return this->constant_; }
2685
a7f064d5 2686 // Check that the initializer does not refer to the constant itself.
2687 void
2688 check_for_init_loop();
2689
e440a328 2690 protected:
ba4aedd4 2691 int
2692 do_traverse(Traverse*);
2693
e440a328 2694 Expression*
ceeb4318 2695 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 2696
2697 bool
2698 do_is_constant() const
2699 { return true; }
2700
0e168074 2701 bool
3ae06f68 2702 do_is_static_initializer() const
0e168074 2703 { return true; }
2704
e440a328 2705 bool
0c77715b 2706 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 2707
2708 bool
af6b489a 2709 do_string_constant_value(std::string* val) const;
e440a328 2710
2711 Type*
2712 do_type();
2713
2714 // The type of a const is set by the declaration, not the use.
2715 void
2716 do_determine_type(const Type_context*);
2717
2718 void
2719 do_check_types(Gogo*);
2720
2721 Expression*
2722 do_copy()
2723 { return this; }
2724
ea664253 2725 Bexpression*
2726 do_get_backend(Translate_context* context);
e440a328 2727
2728 // When exporting a reference to a const as part of a const
2729 // expression, we export the value. We ignore the fact that it has
2730 // a name.
2731 void
2732 do_export(Export* exp) const
2733 { this->constant_->const_value()->expr()->export_expression(exp); }
2734
d751bb78 2735 void
2736 do_dump_expression(Ast_dump_context*) const;
2737
e440a328 2738 private:
2739 // The constant.
2740 Named_object* constant_;
2741 // The type of this reference. This is used if the constant has an
2742 // abstract type.
2743 Type* type_;
13e818f5 2744 // Used to prevent infinite recursion when a constant incorrectly
2745 // refers to itself.
2746 mutable bool seen_;
e440a328 2747};
2748
ba4aedd4 2749// Traversal.
2750
2751int
2752Const_expression::do_traverse(Traverse* traverse)
2753{
2754 if (this->type_ != NULL)
2755 return Type::traverse(this->type_, traverse);
2756 return TRAVERSE_CONTINUE;
2757}
2758
e440a328 2759// Lower a constant expression. This is where we convert the
2760// predeclared constant iota into an integer value.
2761
2762Expression*
ceeb4318 2763Const_expression::do_lower(Gogo* gogo, Named_object*,
2764 Statement_inserter*, int iota_value)
e440a328 2765{
2766 if (this->constant_->const_value()->expr()->classification()
2767 == EXPRESSION_IOTA)
2768 {
2769 if (iota_value == -1)
2770 {
631d5788 2771 go_error_at(this->location(),
2772 "iota is only defined in const declarations");
e440a328 2773 iota_value = 0;
2774 }
e67508fa 2775 return Expression::make_integer_ul(iota_value, NULL, this->location());
e440a328 2776 }
2777
2778 // Make sure that the constant itself has been lowered.
2779 gogo->lower_constant(this->constant_);
2780
2781 return this;
2782}
2783
0c77715b 2784// Return a numeric constant value.
e440a328 2785
2786bool
0c77715b 2787Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 2788{
13e818f5 2789 if (this->seen_)
2790 return false;
2791
e440a328 2792 Expression* e = this->constant_->const_value()->expr();
0c77715b 2793
13e818f5 2794 this->seen_ = true;
2795
0c77715b 2796 bool r = e->numeric_constant_value(nc);
e440a328 2797
13e818f5 2798 this->seen_ = false;
2799
e440a328 2800 Type* ctype;
2801 if (this->type_ != NULL)
2802 ctype = this->type_;
2803 else
2804 ctype = this->constant_->const_value()->type();
e440a328 2805 if (r && ctype != NULL)
2806 {
0c77715b 2807 if (!nc->set_type(ctype, false, this->location()))
e440a328 2808 return false;
e440a328 2809 }
e440a328 2810
e440a328 2811 return r;
2812}
2813
af6b489a 2814bool
2815Const_expression::do_string_constant_value(std::string* val) const
2816{
2817 if (this->seen_)
2818 return false;
2819
2820 Expression* e = this->constant_->const_value()->expr();
2821
2822 this->seen_ = true;
2823 bool ok = e->string_constant_value(val);
2824 this->seen_ = false;
2825
2826 return ok;
2827}
2828
e440a328 2829// Return the type of the const reference.
2830
2831Type*
2832Const_expression::do_type()
2833{
2834 if (this->type_ != NULL)
2835 return this->type_;
13e818f5 2836
2f78f012 2837 Named_constant* nc = this->constant_->const_value();
2838
2839 if (this->seen_ || nc->lowering())
13e818f5 2840 {
2841 this->report_error(_("constant refers to itself"));
2842 this->type_ = Type::make_error_type();
2843 return this->type_;
2844 }
2845
2846 this->seen_ = true;
2847
e440a328 2848 Type* ret = nc->type();
13e818f5 2849
e440a328 2850 if (ret != NULL)
13e818f5 2851 {
2852 this->seen_ = false;
2853 return ret;
2854 }
2855
e440a328 2856 // During parsing, a named constant may have a NULL type, but we
2857 // must not return a NULL type here.
13e818f5 2858 ret = nc->expr()->type();
2859
2860 this->seen_ = false;
2861
2862 return ret;
e440a328 2863}
2864
2865// Set the type of the const reference.
2866
2867void
2868Const_expression::do_determine_type(const Type_context* context)
2869{
2870 Type* ctype = this->constant_->const_value()->type();
2871 Type* cetype = (ctype != NULL
2872 ? ctype
2873 : this->constant_->const_value()->expr()->type());
2874 if (ctype != NULL && !ctype->is_abstract())
2875 ;
2876 else if (context->type != NULL
0c77715b 2877 && context->type->is_numeric_type()
2878 && cetype->is_numeric_type())
e440a328 2879 this->type_ = context->type;
2880 else if (context->type != NULL
2881 && context->type->is_string_type()
2882 && cetype->is_string_type())
2883 this->type_ = context->type;
2884 else if (context->type != NULL
2885 && context->type->is_boolean_type()
2886 && cetype->is_boolean_type())
2887 this->type_ = context->type;
2888 else if (!context->may_be_abstract)
2889 {
2890 if (cetype->is_abstract())
2891 cetype = cetype->make_non_abstract_type();
2892 this->type_ = cetype;
2893 }
2894}
2895
a7f064d5 2896// Check for a loop in which the initializer of a constant refers to
2897// the constant itself.
e440a328 2898
2899void
a7f064d5 2900Const_expression::check_for_init_loop()
e440a328 2901{
5c13bd80 2902 if (this->type_ != NULL && this->type_->is_error())
d5b605df 2903 return;
2904
a7f064d5 2905 if (this->seen_)
2906 {
2907 this->report_error(_("constant refers to itself"));
2908 this->type_ = Type::make_error_type();
2909 return;
2910 }
2911
d5b605df 2912 Expression* init = this->constant_->const_value()->expr();
2913 Find_named_object find_named_object(this->constant_);
a7f064d5 2914
2915 this->seen_ = true;
d5b605df 2916 Expression::traverse(&init, &find_named_object);
a7f064d5 2917 this->seen_ = false;
2918
d5b605df 2919 if (find_named_object.found())
2920 {
5c13bd80 2921 if (this->type_ == NULL || !this->type_->is_error())
a7f064d5 2922 {
2923 this->report_error(_("constant refers to itself"));
2924 this->type_ = Type::make_error_type();
2925 }
d5b605df 2926 return;
2927 }
a7f064d5 2928}
2929
2930// Check types of a const reference.
2931
2932void
2933Const_expression::do_check_types(Gogo*)
2934{
5c13bd80 2935 if (this->type_ != NULL && this->type_->is_error())
a7f064d5 2936 return;
2937
2938 this->check_for_init_loop();
d5b605df 2939
0c77715b 2940 // Check that numeric constant fits in type.
2941 if (this->type_ != NULL && this->type_->is_numeric_type())
e440a328 2942 {
0c77715b 2943 Numeric_constant nc;
2944 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
e440a328 2945 {
0c77715b 2946 if (!nc.set_type(this->type_, true, this->location()))
2947 this->set_is_error();
e440a328 2948 }
e440a328 2949 }
2950}
2951
ea664253 2952// Return the backend representation for a const reference.
e440a328 2953
ea664253 2954Bexpression*
2955Const_expression::do_get_backend(Translate_context* context)
e440a328 2956{
12373dd5 2957 if (this->is_error_expression()
2958 || (this->type_ != NULL && this->type_->is_error()))
2959 {
2960 go_assert(saw_errors());
2961 return context->backend()->error_expression();
2962 }
e440a328 2963
2964 // If the type has been set for this expression, but the underlying
2965 // object is an abstract int or float, we try to get the abstract
2966 // value. Otherwise we may lose something in the conversion.
f2de4532 2967 Expression* expr = this->constant_->const_value()->expr();
e440a328 2968 if (this->type_ != NULL
0c77715b 2969 && this->type_->is_numeric_type()
a68492b4 2970 && (this->constant_->const_value()->type() == NULL
2971 || this->constant_->const_value()->type()->is_abstract()))
e440a328 2972 {
0c77715b 2973 Numeric_constant nc;
2974 if (expr->numeric_constant_value(&nc)
2975 && nc.set_type(this->type_, false, this->location()))
e440a328 2976 {
0c77715b 2977 Expression* e = nc.expression(this->location());
ea664253 2978 return e->get_backend(context);
e440a328 2979 }
e440a328 2980 }
2981
2c809f8f 2982 if (this->type_ != NULL)
f2de4532 2983 expr = Expression::make_cast(this->type_, expr, this->location());
ea664253 2984 return expr->get_backend(context);
e440a328 2985}
2986
d751bb78 2987// Dump ast representation for constant expression.
2988
2989void
2990Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2991{
2992 ast_dump_context->ostream() << this->constant_->name();
2993}
2994
e440a328 2995// Make a reference to a constant in an expression.
2996
2997Expression*
2998Expression::make_const_reference(Named_object* constant,
b13c66cd 2999 Location location)
e440a328 3000{
3001 return new Const_expression(constant, location);
3002}
3003
d5b605df 3004// Find a named object in an expression.
3005
3006int
3007Find_named_object::expression(Expression** pexpr)
3008{
3009 switch ((*pexpr)->classification())
3010 {
3011 case Expression::EXPRESSION_CONST_REFERENCE:
a7f064d5 3012 {
3013 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3014 if (ce->named_object() == this->no_)
3015 break;
3016
3017 // We need to check a constant initializer explicitly, as
3018 // loops here will not be caught by the loop checking for
3019 // variable initializers.
3020 ce->check_for_init_loop();
3021
3022 return TRAVERSE_CONTINUE;
3023 }
3024
d5b605df 3025 case Expression::EXPRESSION_VAR_REFERENCE:
3026 if ((*pexpr)->var_expression()->named_object() == this->no_)
3027 break;
3028 return TRAVERSE_CONTINUE;
3029 case Expression::EXPRESSION_FUNC_REFERENCE:
3030 if ((*pexpr)->func_expression()->named_object() == this->no_)
3031 break;
3032 return TRAVERSE_CONTINUE;
3033 default:
3034 return TRAVERSE_CONTINUE;
3035 }
3036 this->found_ = true;
3037 return TRAVERSE_EXIT;
3038}
3039
e440a328 3040// The nil value.
3041
3042class Nil_expression : public Expression
3043{
3044 public:
b13c66cd 3045 Nil_expression(Location location)
e440a328 3046 : Expression(EXPRESSION_NIL, location)
3047 { }
3048
3049 static Expression*
3050 do_import(Import*);
3051
3052 protected:
3053 bool
3054 do_is_constant() const
3055 { return true; }
3056
f9ca30f9 3057 bool
3ae06f68 3058 do_is_static_initializer() const
f9ca30f9 3059 { return true; }
3060
e440a328 3061 Type*
3062 do_type()
3063 { return Type::make_nil_type(); }
3064
3065 void
3066 do_determine_type(const Type_context*)
3067 { }
3068
3069 Expression*
3070 do_copy()
3071 { return this; }
3072
ea664253 3073 Bexpression*
3074 do_get_backend(Translate_context* context)
3075 { return context->backend()->nil_pointer_expression(); }
e440a328 3076
3077 void
3078 do_export(Export* exp) const
3079 { exp->write_c_string("nil"); }
d751bb78 3080
3081 void
3082 do_dump_expression(Ast_dump_context* ast_dump_context) const
3083 { ast_dump_context->ostream() << "nil"; }
e440a328 3084};
3085
3086// Import a nil expression.
3087
3088Expression*
3089Nil_expression::do_import(Import* imp)
3090{
3091 imp->require_c_string("nil");
3092 return Expression::make_nil(imp->location());
3093}
3094
3095// Make a nil expression.
3096
3097Expression*
b13c66cd 3098Expression::make_nil(Location location)
e440a328 3099{
3100 return new Nil_expression(location);
3101}
3102
3103// The value of the predeclared constant iota. This is little more
3104// than a marker. This will be lowered to an integer in
3105// Const_expression::do_lower, which is where we know the value that
3106// it should have.
3107
3108class Iota_expression : public Parser_expression
3109{
3110 public:
b13c66cd 3111 Iota_expression(Location location)
e440a328 3112 : Parser_expression(EXPRESSION_IOTA, location)
3113 { }
3114
3115 protected:
3116 Expression*
ceeb4318 3117 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
c3e6f413 3118 { go_unreachable(); }
e440a328 3119
3120 // There should only ever be one of these.
3121 Expression*
3122 do_copy()
c3e6f413 3123 { go_unreachable(); }
d751bb78 3124
3125 void
3126 do_dump_expression(Ast_dump_context* ast_dump_context) const
3127 { ast_dump_context->ostream() << "iota"; }
e440a328 3128};
3129
3130// Make an iota expression. This is only called for one case: the
3131// value of the predeclared constant iota.
3132
3133Expression*
3134Expression::make_iota()
3135{
b13c66cd 3136 static Iota_expression iota_expression(Linemap::unknown_location());
e440a328 3137 return &iota_expression;
3138}
3139
da244e59 3140// Class Type_conversion_expression.
e440a328 3141
3142// Traversal.
3143
3144int
3145Type_conversion_expression::do_traverse(Traverse* traverse)
3146{
3147 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3148 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3149 return TRAVERSE_EXIT;
3150 return TRAVERSE_CONTINUE;
3151}
3152
3153// Convert to a constant at lowering time.
3154
3155Expression*
ceeb4318 3156Type_conversion_expression::do_lower(Gogo*, Named_object*,
3157 Statement_inserter*, int)
e440a328 3158{
3159 Type* type = this->type_;
3160 Expression* val = this->expr_;
b13c66cd 3161 Location location = this->location();
e440a328 3162
0c77715b 3163 if (type->is_numeric_type())
e440a328 3164 {
0c77715b 3165 Numeric_constant nc;
3166 if (val->numeric_constant_value(&nc))
e440a328 3167 {
0c77715b 3168 if (!nc.set_type(type, true, location))
3169 return Expression::make_error(location);
3170 return nc.expression(location);
e440a328 3171 }
e440a328 3172 }
3173
d7739c9a 3174 // According to the language specification on string conversions
3175 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
3176 // When converting an integer into a string, the string will be a UTF-8
3177 // representation of the integer and integers "outside the range of valid
3178 // Unicode code points are converted to '\uFFFD'."
3179 if (type->is_string_type())
3180 {
3181 Numeric_constant nc;
3182 if (val->numeric_constant_value(&nc) && nc.is_int())
3183 {
3184 // An integer value doesn't fit in the Unicode code point range if it
3185 // overflows the Go "int" type or is negative.
3186 unsigned long ul;
3187 if (!nc.set_type(Type::lookup_integer_type("int"), false, location)
3188 || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE)
3189 return Expression::make_string("\ufffd", location);
3190 }
3191 }
3192
55072f2b 3193 if (type->is_slice_type())
e440a328 3194 {
3195 Type* element_type = type->array_type()->element_type()->forwarded();
60963afd 3196 bool is_byte = (element_type->integer_type() != NULL
3197 && element_type->integer_type()->is_byte());
3198 bool is_rune = (element_type->integer_type() != NULL
3199 && element_type->integer_type()->is_rune());
3200 if (is_byte || is_rune)
e440a328 3201 {
3202 std::string s;
3203 if (val->string_constant_value(&s))
3204 {
3205 Expression_list* vals = new Expression_list();
3206 if (is_byte)
3207 {
3208 for (std::string::const_iterator p = s.begin();
3209 p != s.end();
3210 p++)
3211 {
e67508fa 3212 unsigned char c = static_cast<unsigned char>(*p);
3213 vals->push_back(Expression::make_integer_ul(c,
3214 element_type,
3215 location));
e440a328 3216 }
3217 }
3218 else
3219 {
3220 const char *p = s.data();
3221 const char *pend = s.data() + s.length();
3222 while (p < pend)
3223 {
3224 unsigned int c;
3225 int adv = Lex::fetch_char(p, &c);
3226 if (adv == 0)
3227 {
631d5788 3228 go_warning_at(this->location(), 0,
e440a328 3229 "invalid UTF-8 encoding");
3230 adv = 1;
3231 }
3232 p += adv;
e67508fa 3233 vals->push_back(Expression::make_integer_ul(c,
3234 element_type,
3235 location));
e440a328 3236 }
3237 }
3238
3239 return Expression::make_slice_composite_literal(type, vals,
3240 location);
3241 }
3242 }
3243 }
3244
3245 return this;
3246}
3247
35a54f17 3248// Flatten a type conversion by using a temporary variable for the slice
3249// in slice to string conversions.
3250
3251Expression*
3252Type_conversion_expression::do_flatten(Gogo*, Named_object*,
3253 Statement_inserter* inserter)
3254{
5bf8be8b 3255 if (this->type()->is_error_type() || this->expr_->is_error_expression())
3256 {
3257 go_assert(saw_errors());
3258 return Expression::make_error(this->location());
3259 }
3260
2c809f8f 3261 if (((this->type()->is_string_type()
3262 && this->expr_->type()->is_slice_type())
8ba8cc87 3263 || this->expr_->type()->interface_type() != NULL)
35a54f17 3264 && !this->expr_->is_variable())
3265 {
3266 Temporary_statement* temp =
3267 Statement::make_temporary(NULL, this->expr_, this->location());
3268 inserter->insert(temp);
3269 this->expr_ = Expression::make_temporary_reference(temp, this->location());
3270 }
3271 return this;
3272}
3273
1ca01a59 3274// Return whether a type conversion is a constant.
3275
3276bool
3277Type_conversion_expression::do_is_constant() const
3278{
3279 if (!this->expr_->is_constant())
3280 return false;
3281
3282 // A conversion to a type that may not be used as a constant is not
3283 // a constant. For example, []byte(nil).
3284 Type* type = this->type_;
3285 if (type->integer_type() == NULL
3286 && type->float_type() == NULL
3287 && type->complex_type() == NULL
3288 && !type->is_boolean_type()
3289 && !type->is_string_type())
3290 return false;
3291
3292 return true;
3293}
3294
3ae06f68 3295// Return whether a type conversion can be used in a constant
3296// initializer.
0e168074 3297
3298bool
3ae06f68 3299Type_conversion_expression::do_is_static_initializer() const
0e168074 3300{
3301 Type* type = this->type_;
3302 Type* expr_type = this->expr_->type();
3303
3304 if (type->interface_type() != NULL
3305 || expr_type->interface_type() != NULL)
3306 return false;
3307
3ae06f68 3308 if (!this->expr_->is_static_initializer())
0e168074 3309 return false;
3310
3311 if (Type::are_identical(type, expr_type, false, NULL))
3312 return true;
3313
03118c21 3314 if (type->is_string_type() && expr_type->is_string_type())
3315 return true;
3316
3317 if ((type->is_numeric_type()
3318 || type->is_boolean_type()
3319 || type->points_to() != NULL)
3320 && (expr_type->is_numeric_type()
3321 || expr_type->is_boolean_type()
3322 || expr_type->points_to() != NULL))
3323 return true;
3324
3325 return false;
0e168074 3326}
3327
0c77715b 3328// Return the constant numeric value if there is one.
e440a328 3329
3330bool
0c77715b 3331Type_conversion_expression::do_numeric_constant_value(
3332 Numeric_constant* nc) const
e440a328 3333{
0c77715b 3334 if (!this->type_->is_numeric_type())
e440a328 3335 return false;
0c77715b 3336 if (!this->expr_->numeric_constant_value(nc))
e440a328 3337 return false;
0c77715b 3338 return nc->set_type(this->type_, false, this->location());
e440a328 3339}
3340
3341// Return the constant string value if there is one.
3342
3343bool
3344Type_conversion_expression::do_string_constant_value(std::string* val) const
3345{
3346 if (this->type_->is_string_type()
3347 && this->expr_->type()->integer_type() != NULL)
3348 {
0c77715b 3349 Numeric_constant nc;
3350 if (this->expr_->numeric_constant_value(&nc))
e440a328 3351 {
0c77715b 3352 unsigned long ival;
3353 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
e440a328 3354 {
0c77715b 3355 val->clear();
3356 Lex::append_char(ival, true, val, this->location());
e440a328 3357 return true;
3358 }
3359 }
e440a328 3360 }
3361
3362 // FIXME: Could handle conversion from const []int here.
3363
3364 return false;
3365}
3366
da244e59 3367// Determine the resulting type of the conversion.
3368
3369void
3370Type_conversion_expression::do_determine_type(const Type_context*)
3371{
3372 Type_context subcontext(this->type_, false);
3373 this->expr_->determine_type(&subcontext);
3374}
3375
e440a328 3376// Check that types are convertible.
3377
3378void
3379Type_conversion_expression::do_check_types(Gogo*)
3380{
3381 Type* type = this->type_;
3382 Type* expr_type = this->expr_->type();
3383 std::string reason;
3384
5c13bd80 3385 if (type->is_error() || expr_type->is_error())
842f6425 3386 {
842f6425 3387 this->set_is_error();
3388 return;
3389 }
3390
e440a328 3391 if (this->may_convert_function_types_
3392 && type->function_type() != NULL
3393 && expr_type->function_type() != NULL)
3394 return;
3395
3396 if (Type::are_convertible(type, expr_type, &reason))
3397 return;
3398
631d5788 3399 go_error_at(this->location(), "%s", reason.c_str());
e440a328 3400 this->set_is_error();
3401}
3402
ea664253 3403// Get the backend representation for a type conversion.
e440a328 3404
ea664253 3405Bexpression*
3406Type_conversion_expression::do_get_backend(Translate_context* context)
e440a328 3407{
e440a328 3408 Type* type = this->type_;
3409 Type* expr_type = this->expr_->type();
2c809f8f 3410
3411 Gogo* gogo = context->gogo();
3412 Btype* btype = type->get_backend(gogo);
ea664253 3413 Bexpression* bexpr = this->expr_->get_backend(context);
2c809f8f 3414 Location loc = this->location();
3415
3416 if (Type::are_identical(type, expr_type, false, NULL))
ea664253 3417 return gogo->backend()->convert_expression(btype, bexpr, loc);
2c809f8f 3418 else if (type->interface_type() != NULL
3419 || expr_type->interface_type() != NULL)
e440a328 3420 {
2c809f8f 3421 Expression* conversion =
3422 Expression::convert_for_assignment(gogo, type, this->expr_,
3423 this->location());
ea664253 3424 return conversion->get_backend(context);
e440a328 3425 }
3426 else if (type->is_string_type()
3427 && expr_type->integer_type() != NULL)
3428 {
2c809f8f 3429 mpz_t intval;
3430 Numeric_constant nc;
3431 if (this->expr_->numeric_constant_value(&nc)
3432 && nc.to_int(&intval)
3433 && mpz_fits_ushort_p(intval))
e440a328 3434 {
e440a328 3435 std::string s;
2c809f8f 3436 Lex::append_char(mpz_get_ui(intval), true, &s, loc);
3437 mpz_clear(intval);
3438 Expression* se = Expression::make_string(s, loc);
ea664253 3439 return se->get_backend(context);
e440a328 3440 }
3441
f16ab008 3442 Expression* i2s_expr =
736a16ba 3443 Runtime::make_call(Runtime::INTSTRING, loc, 2,
3444 Expression::make_nil(loc), this->expr_);
ea664253 3445 return Expression::make_cast(type, i2s_expr, loc)->get_backend(context);
e440a328 3446 }
55072f2b 3447 else if (type->is_string_type() && expr_type->is_slice_type())
e440a328 3448 {
55072f2b 3449 Array_type* a = expr_type->array_type();
e440a328 3450 Type* e = a->element_type()->forwarded();
c484d925 3451 go_assert(e->integer_type() != NULL);
35a54f17 3452 go_assert(this->expr_->is_variable());
3453
3454 Runtime::Function code;
60963afd 3455 if (e->integer_type()->is_byte())
736a16ba 3456 code = Runtime::SLICEBYTETOSTRING;
e440a328 3457 else
35a54f17 3458 {
3459 go_assert(e->integer_type()->is_rune());
736a16ba 3460 code = Runtime::SLICERUNETOSTRING;
35a54f17 3461 }
736a16ba 3462 return Runtime::make_call(code, loc, 2, Expression::make_nil(loc),
3463 this->expr_)->get_backend(context);
e440a328 3464 }
411eb89e 3465 else if (type->is_slice_type() && expr_type->is_string_type())
e440a328 3466 {
3467 Type* e = type->array_type()->element_type()->forwarded();
c484d925 3468 go_assert(e->integer_type() != NULL);
6c252e42 3469
2c809f8f 3470 Runtime::Function code;
60963afd 3471 if (e->integer_type()->is_byte())
736a16ba 3472 code = Runtime::STRINGTOSLICEBYTE;
e440a328 3473 else
3474 {
60963afd 3475 go_assert(e->integer_type()->is_rune());
736a16ba 3476 code = Runtime::STRINGTOSLICERUNE;
e440a328 3477 }
736a16ba 3478 Expression* s2a = Runtime::make_call(code, loc, 2,
3479 Expression::make_nil(loc),
3480 this->expr_);
ea664253 3481 return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context);
2c809f8f 3482 }
3483 else if (type->is_numeric_type())
3484 {
3485 go_assert(Type::are_convertible(type, expr_type, NULL));
ea664253 3486 return gogo->backend()->convert_expression(btype, bexpr, loc);
e440a328 3487 }
3488 else if ((type->is_unsafe_pointer_type()
2c809f8f 3489 && (expr_type->points_to() != NULL
3490 || expr_type->integer_type()))
3491 || (expr_type->is_unsafe_pointer_type()
3492 && type->points_to() != NULL)
3493 || (this->may_convert_function_types_
3494 && type->function_type() != NULL
3495 && expr_type->function_type() != NULL))
ea664253 3496 return gogo->backend()->convert_expression(btype, bexpr, loc);
e440a328 3497 else
2c809f8f 3498 {
3499 Expression* conversion =
3500 Expression::convert_for_assignment(gogo, type, this->expr_, loc);
ea664253 3501 return conversion->get_backend(context);
2c809f8f 3502 }
e440a328 3503}
3504
3505// Output a type conversion in a constant expression.
3506
3507void
3508Type_conversion_expression::do_export(Export* exp) const
3509{
3510 exp->write_c_string("convert(");
3511 exp->write_type(this->type_);
3512 exp->write_c_string(", ");
3513 this->expr_->export_expression(exp);
3514 exp->write_c_string(")");
3515}
3516
3517// Import a type conversion or a struct construction.
3518
3519Expression*
3520Type_conversion_expression::do_import(Import* imp)
3521{
3522 imp->require_c_string("convert(");
3523 Type* type = imp->read_type();
3524 imp->require_c_string(", ");
3525 Expression* val = Expression::import_expression(imp);
3526 imp->require_c_string(")");
3527 return Expression::make_cast(type, val, imp->location());
3528}
3529
d751bb78 3530// Dump ast representation for a type conversion expression.
3531
3532void
3533Type_conversion_expression::do_dump_expression(
3534 Ast_dump_context* ast_dump_context) const
3535{
3536 ast_dump_context->dump_type(this->type_);
3537 ast_dump_context->ostream() << "(";
3538 ast_dump_context->dump_expression(this->expr_);
3539 ast_dump_context->ostream() << ") ";
3540}
3541
e440a328 3542// Make a type cast expression.
3543
3544Expression*
b13c66cd 3545Expression::make_cast(Type* type, Expression* val, Location location)
e440a328 3546{
3547 if (type->is_error_type() || val->is_error_expression())
3548 return Expression::make_error(location);
3549 return new Type_conversion_expression(type, val, location);
3550}
3551
98f62f7a 3552// Class Unsafe_type_conversion_expression.
9581e91d 3553
3554// Traversal.
3555
3556int
3557Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3558{
3559 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3560 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3561 return TRAVERSE_EXIT;
3562 return TRAVERSE_CONTINUE;
3563}
3564
3ae06f68 3565// Return whether an unsafe type conversion can be used as a constant
3566// initializer.
aa5ae575 3567
3568bool
3ae06f68 3569Unsafe_type_conversion_expression::do_is_static_initializer() const
aa5ae575 3570{
3571 Type* type = this->type_;
3572 Type* expr_type = this->expr_->type();
3573
3574 if (type->interface_type() != NULL
3575 || expr_type->interface_type() != NULL)
3576 return false;
3577
3ae06f68 3578 if (!this->expr_->is_static_initializer())
aa5ae575 3579 return false;
3580
3581 if (Type::are_convertible(type, expr_type, NULL))
3582 return true;
3583
03118c21 3584 if (type->is_string_type() && expr_type->is_string_type())
3585 return true;
3586
3587 if ((type->is_numeric_type()
3588 || type->is_boolean_type()
3589 || type->points_to() != NULL)
3590 && (expr_type->is_numeric_type()
3591 || expr_type->is_boolean_type()
3592 || expr_type->points_to() != NULL))
3593 return true;
3594
3595 return false;
aa5ae575 3596}
3597
9581e91d 3598// Convert to backend representation.
3599
ea664253 3600Bexpression*
3601Unsafe_type_conversion_expression::do_get_backend(Translate_context* context)
9581e91d 3602{
3603 // We are only called for a limited number of cases.
3604
3605 Type* t = this->type_;
3606 Type* et = this->expr_->type();
5c4802f1 3607
3608 if (t->is_error_type()
3609 || this->expr_->is_error_expression()
3610 || et->is_error_type())
3611 {
3612 go_assert(saw_errors());
3613 return context->backend()->error_expression();
3614 }
3615
2c809f8f 3616 if (t->array_type() != NULL)
3617 go_assert(et->array_type() != NULL
3618 && t->is_slice_type() == et->is_slice_type());
3619 else if (t->struct_type() != NULL)
9581e91d 3620 {
2c809f8f 3621 if (t->named_type() != NULL
3622 && et->named_type() != NULL
3623 && !Type::are_convertible(t, et, NULL))
3624 {
3625 go_assert(saw_errors());
ea664253 3626 return context->backend()->error_expression();
2c809f8f 3627 }
3628
3629 go_assert(et->struct_type() != NULL
3630 && Type::are_convertible(t, et, NULL));
3631 }
3632 else if (t->map_type() != NULL)
c484d925 3633 go_assert(et->map_type() != NULL);
9581e91d 3634 else if (t->channel_type() != NULL)
c484d925 3635 go_assert(et->channel_type() != NULL);
09ea332d 3636 else if (t->points_to() != NULL)
2c809f8f 3637 go_assert(et->points_to() != NULL
3638 || et->channel_type() != NULL
3639 || et->map_type() != NULL
3640 || et->function_type() != NULL
132ed071 3641 || et->integer_type() != NULL
2c809f8f 3642 || et->is_nil_type());
9581e91d 3643 else if (et->is_unsafe_pointer_type())
c484d925 3644 go_assert(t->points_to() != NULL);
2c809f8f 3645 else if (t->interface_type() != NULL)
9581e91d 3646 {
2c809f8f 3647 bool empty_iface = t->interface_type()->is_empty();
c484d925 3648 go_assert(et->interface_type() != NULL
2c809f8f 3649 && et->interface_type()->is_empty() == empty_iface);
9581e91d 3650 }
588e3cf9 3651 else if (t->integer_type() != NULL)
2c809f8f 3652 go_assert(et->is_boolean_type()
3653 || et->integer_type() != NULL
3654 || et->function_type() != NULL
3655 || et->points_to() != NULL
3656 || et->map_type() != NULL
8ba8cc87 3657 || et->channel_type() != NULL
3658 || et->is_nil_type());
cd39797e 3659 else if (t->function_type() != NULL)
3660 go_assert(et->points_to() != NULL);
9581e91d 3661 else
c3e6f413 3662 go_unreachable();
9581e91d 3663
2c809f8f 3664 Gogo* gogo = context->gogo();
3665 Btype* btype = t->get_backend(gogo);
ea664253 3666 Bexpression* bexpr = this->expr_->get_backend(context);
2c809f8f 3667 Location loc = this->location();
ea664253 3668 return gogo->backend()->convert_expression(btype, bexpr, loc);
9581e91d 3669}
3670
d751bb78 3671// Dump ast representation for an unsafe type conversion expression.
3672
3673void
3674Unsafe_type_conversion_expression::do_dump_expression(
3675 Ast_dump_context* ast_dump_context) const
3676{
3677 ast_dump_context->dump_type(this->type_);
3678 ast_dump_context->ostream() << "(";
3679 ast_dump_context->dump_expression(this->expr_);
3680 ast_dump_context->ostream() << ") ";
3681}
3682
9581e91d 3683// Make an unsafe type conversion expression.
3684
3685Expression*
3686Expression::make_unsafe_cast(Type* type, Expression* expr,
b13c66cd 3687 Location location)
9581e91d 3688{
3689 return new Unsafe_type_conversion_expression(type, expr, location);
3690}
3691
76f85fd6 3692// Class Unary_expression.
e440a328 3693
03118c21 3694// Call the address_taken method of the operand if needed. This is
3695// called after escape analysis but before inserting write barriers.
3696
3697void
3698Unary_expression::check_operand_address_taken(Gogo* gogo)
3699{
3700 if (this->op_ != OPERATOR_AND)
3701 return;
3702
3703 // If this->escapes_ is false at this point, then it was set to
3704 // false by an explicit call to set_does_not_escape, and the value
3705 // does not escape. If this->escapes_ is true, we may be able to
3706 // set it to false if taking the address of a variable that does not
3707 // escape.
3708 Node* n = Node::make_node(this);
3709 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
3710 this->escapes_ = false;
3711
3712 // When compiling the runtime, the address operator does not cause
3713 // local variables to escape. When escape analysis becomes the
3714 // default, this should be changed to make it an error if we have an
3715 // address operator that escapes.
3716 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
3717 this->escapes_ = false;
3718
3719 Named_object* var = NULL;
3720 if (this->expr_->var_expression() != NULL)
3721 var = this->expr_->var_expression()->named_object();
3722 else if (this->expr_->enclosed_var_expression() != NULL)
3723 var = this->expr_->enclosed_var_expression()->variable();
3724
3725 if (this->escapes_ && var != NULL)
3726 {
3727 if (var->is_variable())
3728 this->escapes_ = var->var_value()->escapes();
3729 if (var->is_result_variable())
3730 this->escapes_ = var->result_var_value()->escapes();
3731 }
3732
3733 this->expr_->address_taken(this->escapes_);
3734}
3735
e440a328 3736// If we are taking the address of a composite literal, and the
2c809f8f 3737// contents are not constant, then we want to make a heap expression
e440a328 3738// instead.
3739
3740Expression*
ceeb4318 3741Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 3742{
b13c66cd 3743 Location loc = this->location();
e440a328 3744 Operator op = this->op_;
3745 Expression* expr = this->expr_;
3746
3747 if (op == OPERATOR_MULT && expr->is_type_expression())
3748 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3749
3750 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3751 // moving x to the heap. FIXME: Is it worth doing a real escape
3752 // analysis here? This case is found in math/unsafe.go and is
3753 // therefore worth special casing.
3754 if (op == OPERATOR_MULT)
3755 {
3756 Expression* e = expr;
3757 while (e->classification() == EXPRESSION_CONVERSION)
3758 {
3759 Type_conversion_expression* te
3760 = static_cast<Type_conversion_expression*>(e);
3761 e = te->expr();
3762 }
3763
3764 if (e->classification() == EXPRESSION_UNARY)
3765 {
3766 Unary_expression* ue = static_cast<Unary_expression*>(e);
3767 if (ue->op_ == OPERATOR_AND)
3768 {
3769 if (e == expr)
3770 {
3771 // *&x == x.
f4dea966 3772 if (!ue->expr_->is_addressable() && !ue->create_temp_)
3773 {
631d5788 3774 go_error_at(ue->location(),
3775 "invalid operand for unary %<&%>");
f4dea966 3776 this->set_is_error();
3777 }
e440a328 3778 return ue->expr_;
3779 }
3780 ue->set_does_not_escape();
3781 }
3782 }
3783 }
3784
55661ce9 3785 // Catching an invalid indirection of unsafe.Pointer here avoid
3786 // having to deal with TYPE_VOID in other places.
3787 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3788 {
631d5788 3789 go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
55661ce9 3790 return Expression::make_error(this->location());
3791 }
3792
d9f3743a 3793 // Check for an invalid pointer dereference. We need to do this
3794 // here because Unary_expression::do_type will return an error type
3795 // in this case. That can cause code to appear erroneous, and
3796 // therefore disappear at lowering time, without any error message.
3797 if (op == OPERATOR_MULT && expr->type()->points_to() == NULL)
3798 {
3799 this->report_error(_("expected pointer"));
3800 return Expression::make_error(this->location());
3801 }
3802
59a401fe 3803 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
e440a328 3804 {
0c77715b 3805 Numeric_constant nc;
3806 if (expr->numeric_constant_value(&nc))
e440a328 3807 {
0c77715b 3808 Numeric_constant result;
af7a5274 3809 bool issued_error;
3810 if (Unary_expression::eval_constant(op, &nc, loc, &result,
3811 &issued_error))
0c77715b 3812 return result.expression(loc);
af7a5274 3813 else if (issued_error)
3814 return Expression::make_error(this->location());
e440a328 3815 }
3816 }
3817
3818 return this;
3819}
3820
f9ca30f9 3821// Flatten expression if a nil check must be performed and create temporary
3822// variables if necessary.
3823
3824Expression*
3825Unary_expression::do_flatten(Gogo* gogo, Named_object*,
3826 Statement_inserter* inserter)
3827{
5bf8be8b 3828 if (this->is_error_expression()
3829 || this->expr_->is_error_expression()
3830 || this->expr_->type()->is_error_type())
3831 {
3832 go_assert(saw_errors());
3833 return Expression::make_error(this->location());
3834 }
f4dea966 3835
f9ca30f9 3836 Location location = this->location();
3837 if (this->op_ == OPERATOR_MULT
3838 && !this->expr_->is_variable())
3839 {
3840 go_assert(this->expr_->type()->points_to() != NULL);
3841 Type* ptype = this->expr_->type()->points_to();
3842 if (!ptype->is_void_type())
3843 {
2a305b85 3844 int64_t s;
3845 bool ok = ptype->backend_type_size(gogo, &s);
3846 if (!ok)
3847 {
3848 go_assert(saw_errors());
3849 return Expression::make_error(this->location());
3850 }
f9ca30f9 3851 if (s >= 4096 || this->issue_nil_check_)
3852 {
3853 Temporary_statement* temp =
3854 Statement::make_temporary(NULL, this->expr_, location);
3855 inserter->insert(temp);
3856 this->expr_ =
3857 Expression::make_temporary_reference(temp, location);
3858 }
3859 }
3860 }
3861
3862 if (this->create_temp_ && !this->expr_->is_variable())
3863 {
3864 Temporary_statement* temp =
3865 Statement::make_temporary(NULL, this->expr_, location);
3866 inserter->insert(temp);
3867 this->expr_ = Expression::make_temporary_reference(temp, location);
3868 }
3869
3870 return this;
3871}
3872
e440a328 3873// Return whether a unary expression is a constant.
3874
3875bool
3876Unary_expression::do_is_constant() const
3877{
3878 if (this->op_ == OPERATOR_MULT)
3879 {
3880 // Indirecting through a pointer is only constant if the object
3881 // to which the expression points is constant, but we currently
3882 // have no way to determine that.
3883 return false;
3884 }
3885 else if (this->op_ == OPERATOR_AND)
3886 {
3887 // Taking the address of a variable is constant if it is a
f9ca30f9 3888 // global variable, not constant otherwise. In other cases taking the
3889 // address is probably not a constant.
e440a328 3890 Var_expression* ve = this->expr_->var_expression();
3891 if (ve != NULL)
3892 {
3893 Named_object* no = ve->named_object();
3894 return no->is_variable() && no->var_value()->is_global();
3895 }
3896 return false;
3897 }
3898 else
3899 return this->expr_->is_constant();
3900}
3901
3ae06f68 3902// Return whether a unary expression can be used as a constant
3903// initializer.
3904
3905bool
3906Unary_expression::do_is_static_initializer() const
3907{
3908 if (this->op_ == OPERATOR_MULT)
3909 return false;
3910 else if (this->op_ == OPERATOR_AND)
de048538 3911 return Unary_expression::base_is_static_initializer(this->expr_);
3912 else
3913 return this->expr_->is_static_initializer();
3914}
3ae06f68 3915
de048538 3916// Return whether the address of EXPR can be used as a static
3917// initializer.
3ae06f68 3918
de048538 3919bool
3920Unary_expression::base_is_static_initializer(Expression* expr)
3921{
3922 // The address of a field reference can be a static initializer if
3923 // the base can be a static initializer.
3924 Field_reference_expression* fre = expr->field_reference_expression();
3925 if (fre != NULL)
3926 return Unary_expression::base_is_static_initializer(fre->expr());
3927
3928 // The address of an index expression can be a static initializer if
3929 // the base can be a static initializer and the index is constant.
3930 Array_index_expression* aind = expr->array_index_expression();
3931 if (aind != NULL)
3932 return (aind->end() == NULL
3933 && aind->start()->is_constant()
3934 && Unary_expression::base_is_static_initializer(aind->array()));
3935
3936 // The address of a global variable can be a static initializer.
3937 Var_expression* ve = expr->var_expression();
3938 if (ve != NULL)
3939 {
3940 Named_object* no = ve->named_object();
3941 return no->is_variable() && no->var_value()->is_global();
3942 }
3943
3944 // The address of a composite literal can be used as a static
3945 // initializer if the composite literal is itself usable as a
3946 // static initializer.
3947 if (expr->is_composite_literal() && expr->is_static_initializer())
3948 return true;
3ae06f68 3949
de048538 3950 // The address of a string constant can be used as a static
3951 // initializer. This can not be written in Go itself but this is
3952 // used when building a type descriptor.
3953 if (expr->string_expression() != NULL)
3954 return true;
3955
3956 return false;
3ae06f68 3957}
3958
0c77715b 3959// Apply unary opcode OP to UNC, setting NC. Return true if this
af7a5274 3960// could be done, false if not. On overflow, issues an error and sets
3961// *ISSUED_ERROR.
e440a328 3962
3963bool
0c77715b 3964Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
af7a5274 3965 Location location, Numeric_constant* nc,
3966 bool* issued_error)
e440a328 3967{
af7a5274 3968 *issued_error = false;
e440a328 3969 switch (op)
3970 {
3971 case OPERATOR_PLUS:
0c77715b 3972 *nc = *unc;
e440a328 3973 return true;
0c77715b 3974
e440a328 3975 case OPERATOR_MINUS:
0c77715b 3976 if (unc->is_int() || unc->is_rune())
3977 break;
3978 else if (unc->is_float())
3979 {
3980 mpfr_t uval;
3981 unc->get_float(&uval);
3982 mpfr_t val;
3983 mpfr_init(val);
3984 mpfr_neg(val, uval, GMP_RNDN);
3985 nc->set_float(unc->type(), val);
3986 mpfr_clear(uval);
3987 mpfr_clear(val);
3988 return true;
3989 }
3990 else if (unc->is_complex())
3991 {
fcbea5e4 3992 mpc_t uval;
3993 unc->get_complex(&uval);
3994 mpc_t val;
3995 mpc_init2(val, mpc_precision);
3996 mpc_neg(val, uval, MPC_RNDNN);
3997 nc->set_complex(unc->type(), val);
3998 mpc_clear(uval);
3999 mpc_clear(val);
0c77715b 4000 return true;
4001 }
e440a328 4002 else
0c77715b 4003 go_unreachable();
e440a328 4004
0c77715b 4005 case OPERATOR_XOR:
4006 break;
68448d53 4007
59a401fe 4008 case OPERATOR_NOT:
e440a328 4009 case OPERATOR_AND:
4010 case OPERATOR_MULT:
4011 return false;
0c77715b 4012
e440a328 4013 default:
c3e6f413 4014 go_unreachable();
e440a328 4015 }
e440a328 4016
0c77715b 4017 if (!unc->is_int() && !unc->is_rune())
4018 return false;
4019
4020 mpz_t uval;
8387e1df 4021 if (unc->is_rune())
4022 unc->get_rune(&uval);
4023 else
4024 unc->get_int(&uval);
0c77715b 4025 mpz_t val;
4026 mpz_init(val);
e440a328 4027
e440a328 4028 switch (op)
4029 {
e440a328 4030 case OPERATOR_MINUS:
0c77715b 4031 mpz_neg(val, uval);
4032 break;
4033
e440a328 4034 case OPERATOR_NOT:
0c77715b 4035 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4036 break;
4037
e440a328 4038 case OPERATOR_XOR:
0c77715b 4039 {
4040 Type* utype = unc->type();
4041 if (utype->integer_type() == NULL
4042 || utype->integer_type()->is_abstract())
4043 mpz_com(val, uval);
4044 else
4045 {
4046 // The number of HOST_WIDE_INTs that it takes to represent
4047 // UVAL.
4048 size_t count = ((mpz_sizeinbase(uval, 2)
4049 + HOST_BITS_PER_WIDE_INT
4050 - 1)
4051 / HOST_BITS_PER_WIDE_INT);
e440a328 4052
0c77715b 4053 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4054 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4055
4056 size_t obits = utype->integer_type()->bits();
4057
4058 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
4059 {
4060 mpz_t adj;
4061 mpz_init_set_ui(adj, 1);
4062 mpz_mul_2exp(adj, adj, obits);
4063 mpz_add(uval, uval, adj);
4064 mpz_clear(adj);
4065 }
4066
4067 size_t ecount;
4068 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
4069 go_assert(ecount <= count);
4070
4071 // Trim down to the number of words required by the type.
4072 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4073 / HOST_BITS_PER_WIDE_INT);
4074 go_assert(ocount <= count);
4075
4076 for (size_t i = 0; i < ocount; ++i)
4077 phwi[i] = ~phwi[i];
4078
4079 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4080 if (clearbits != 0)
4081 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4082 >> clearbits);
4083
4084 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4085
4086 if (!utype->integer_type()->is_unsigned()
4087 && mpz_tstbit(val, obits - 1))
4088 {
4089 mpz_t adj;
4090 mpz_init_set_ui(adj, 1);
4091 mpz_mul_2exp(adj, adj, obits);
4092 mpz_sub(val, val, adj);
4093 mpz_clear(adj);
4094 }
4095
4096 delete[] phwi;
4097 }
4098 }
4099 break;
e440a328 4100
e440a328 4101 default:
c3e6f413 4102 go_unreachable();
e440a328 4103 }
e440a328 4104
0c77715b 4105 if (unc->is_rune())
4106 nc->set_rune(NULL, val);
e440a328 4107 else
0c77715b 4108 nc->set_int(NULL, val);
e440a328 4109
0c77715b 4110 mpz_clear(uval);
4111 mpz_clear(val);
e440a328 4112
af7a5274 4113 if (!nc->set_type(unc->type(), true, location))
4114 {
4115 *issued_error = true;
4116 return false;
4117 }
4118 return true;
e440a328 4119}
4120
0c77715b 4121// Return the integral constant value of a unary expression, if it has one.
e440a328 4122
4123bool
0c77715b 4124Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 4125{
0c77715b 4126 Numeric_constant unc;
4127 if (!this->expr_->numeric_constant_value(&unc))
4128 return false;
af7a5274 4129 bool issued_error;
0c77715b 4130 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
af7a5274 4131 nc, &issued_error);
e440a328 4132}
4133
4134// Return the type of a unary expression.
4135
4136Type*
4137Unary_expression::do_type()
4138{
4139 switch (this->op_)
4140 {
4141 case OPERATOR_PLUS:
4142 case OPERATOR_MINUS:
4143 case OPERATOR_NOT:
4144 case OPERATOR_XOR:
4145 return this->expr_->type();
4146
4147 case OPERATOR_AND:
4148 return Type::make_pointer_type(this->expr_->type());
4149
4150 case OPERATOR_MULT:
4151 {
4152 Type* subtype = this->expr_->type();
4153 Type* points_to = subtype->points_to();
4154 if (points_to == NULL)
4155 return Type::make_error_type();
4156 return points_to;
4157 }
4158
4159 default:
c3e6f413 4160 go_unreachable();
e440a328 4161 }
4162}
4163
4164// Determine abstract types for a unary expression.
4165
4166void
4167Unary_expression::do_determine_type(const Type_context* context)
4168{
4169 switch (this->op_)
4170 {
4171 case OPERATOR_PLUS:
4172 case OPERATOR_MINUS:
4173 case OPERATOR_NOT:
4174 case OPERATOR_XOR:
4175 this->expr_->determine_type(context);
4176 break;
4177
4178 case OPERATOR_AND:
4179 // Taking the address of something.
4180 {
4181 Type* subtype = (context->type == NULL
4182 ? NULL
4183 : context->type->points_to());
4184 Type_context subcontext(subtype, false);
4185 this->expr_->determine_type(&subcontext);
4186 }
4187 break;
4188
4189 case OPERATOR_MULT:
4190 // Indirecting through a pointer.
4191 {
4192 Type* subtype = (context->type == NULL
4193 ? NULL
4194 : Type::make_pointer_type(context->type));
4195 Type_context subcontext(subtype, false);
4196 this->expr_->determine_type(&subcontext);
4197 }
4198 break;
4199
4200 default:
c3e6f413 4201 go_unreachable();
e440a328 4202 }
4203}
4204
4205// Check types for a unary expression.
4206
4207void
4208Unary_expression::do_check_types(Gogo*)
4209{
9fe897ef 4210 Type* type = this->expr_->type();
5c13bd80 4211 if (type->is_error())
9fe897ef 4212 {
4213 this->set_is_error();
4214 return;
4215 }
4216
e440a328 4217 switch (this->op_)
4218 {
4219 case OPERATOR_PLUS:
4220 case OPERATOR_MINUS:
9fe897ef 4221 if (type->integer_type() == NULL
4222 && type->float_type() == NULL
4223 && type->complex_type() == NULL)
4224 this->report_error(_("expected numeric type"));
e440a328 4225 break;
4226
4227 case OPERATOR_NOT:
59a401fe 4228 if (!type->is_boolean_type())
4229 this->report_error(_("expected boolean type"));
4230 break;
4231
e440a328 4232 case OPERATOR_XOR:
b3b1474e 4233 if (type->integer_type() == NULL)
4234 this->report_error(_("expected integer"));
e440a328 4235 break;
4236
4237 case OPERATOR_AND:
4238 if (!this->expr_->is_addressable())
09ea332d 4239 {
4240 if (!this->create_temp_)
f4dea966 4241 {
631d5788 4242 go_error_at(this->location(), "invalid operand for unary %<&%>");
f4dea966 4243 this->set_is_error();
4244 }
09ea332d 4245 }
e440a328 4246 else
da244e59 4247 this->expr_->issue_nil_check();
e440a328 4248 break;
4249
4250 case OPERATOR_MULT:
4251 // Indirecting through a pointer.
9fe897ef 4252 if (type->points_to() == NULL)
4253 this->report_error(_("expected pointer"));
7661d702 4254 if (type->points_to()->is_error())
4255 this->set_is_error();
e440a328 4256 break;
4257
4258 default:
c3e6f413 4259 go_unreachable();
e440a328 4260 }
4261}
4262
ea664253 4263// Get the backend representation for a unary expression.
e440a328 4264
ea664253 4265Bexpression*
4266Unary_expression::do_get_backend(Translate_context* context)
e440a328 4267{
1b1f2abf 4268 Gogo* gogo = context->gogo();
e9d3367e 4269 Location loc = this->location();
4270
4271 // Taking the address of a set-and-use-temporary expression requires
4272 // setting the temporary and then taking the address.
4273 if (this->op_ == OPERATOR_AND)
4274 {
4275 Set_and_use_temporary_expression* sut =
4276 this->expr_->set_and_use_temporary_expression();
4277 if (sut != NULL)
4278 {
4279 Temporary_statement* temp = sut->temporary();
4280 Bvariable* bvar = temp->get_backend_variable(context);
d4e6573e 4281 Bexpression* bvar_expr =
4282 gogo->backend()->var_expression(bvar, VE_lvalue, loc);
ea664253 4283 Bexpression* bval = sut->expression()->get_backend(context);
f9ca30f9 4284
0ab48656 4285 Named_object* fn = context->function();
4286 go_assert(fn != NULL);
4287 Bfunction* bfn =
4288 fn->func_value()->get_or_make_decl(gogo, fn);
f9ca30f9 4289 Bstatement* bassign =
0ab48656 4290 gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc);
f9ca30f9 4291 Bexpression* bvar_addr =
4292 gogo->backend()->address_expression(bvar_expr, loc);
ea664253 4293 return gogo->backend()->compound_expression(bassign, bvar_addr, loc);
e9d3367e 4294 }
4295 }
4296
f9ca30f9 4297 Bexpression* ret;
ea664253 4298 Bexpression* bexpr = this->expr_->get_backend(context);
f9ca30f9 4299 Btype* btype = this->expr_->type()->get_backend(gogo);
e440a328 4300 switch (this->op_)
4301 {
4302 case OPERATOR_PLUS:
f9ca30f9 4303 ret = bexpr;
4304 break;
e440a328 4305
4306 case OPERATOR_MINUS:
f9ca30f9 4307 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
4308 ret = gogo->backend()->convert_expression(btype, ret, loc);
4309 break;
e440a328 4310
4311 case OPERATOR_NOT:
e440a328 4312 case OPERATOR_XOR:
f9ca30f9 4313 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
4314 break;
e440a328 4315
4316 case OPERATOR_AND:
09ea332d 4317 if (!this->create_temp_)
4318 {
4319 // We should not see a non-constant constructor here; cases
4320 // where we would see one should have been moved onto the
4321 // heap at parse time. Taking the address of a nonconstant
4322 // constructor will not do what the programmer expects.
f9ca30f9 4323
4324 go_assert(!this->expr_->is_composite_literal()
3ae06f68 4325 || this->expr_->is_static_initializer());
24060bf9 4326 if (this->expr_->classification() == EXPRESSION_UNARY)
4327 {
4328 Unary_expression* ue =
4329 static_cast<Unary_expression*>(this->expr_);
4330 go_assert(ue->op() != OPERATOR_AND);
4331 }
09ea332d 4332 }
e440a328 4333
f23d7786 4334 static unsigned int counter;
4335 char buf[100];
4336 if (this->is_gc_root_ || this->is_slice_init_)
76f85fd6 4337 {
f23d7786 4338 bool copy_to_heap = false;
4339 if (this->is_gc_root_)
4340 {
4341 // Build a decl for a GC root variable. GC roots are mutable, so
4342 // they cannot be represented as an immutable_struct in the
4343 // backend.
4344 static unsigned int root_counter;
4345 snprintf(buf, sizeof buf, "gc%u", root_counter);
4346 ++root_counter;
4347 }
4348 else
4349 {
4350 // Build a decl for a slice value initializer. An immutable slice
4351 // value initializer may have to be copied to the heap if it
4352 // contains pointers in a non-constant context.
4353 snprintf(buf, sizeof buf, "C%u", counter);
4354 ++counter;
4355
4356 Array_type* at = this->expr_->type()->array_type();
4357 go_assert(at != NULL);
4358
4359 // If we are not copying the value to the heap, we will only
4360 // initialize the value once, so we can use this directly
4361 // rather than copying it. In that case we can't make it
4362 // read-only, because the program is permitted to change it.
3ae06f68 4363 copy_to_heap = context->function() != NULL;
f23d7786 4364 }
438b4bec 4365 std::string asm_name(go_selectively_encode_id(buf));
f23d7786 4366 Bvariable* implicit =
438b4bec 4367 gogo->backend()->implicit_variable(buf, asm_name,
4368 btype, true, copy_to_heap,
4369 false, 0);
aa5ae575 4370 gogo->backend()->implicit_variable_set_init(implicit, buf, btype,
4371 true, copy_to_heap, false,
4372 bexpr);
d4e6573e 4373 bexpr = gogo->backend()->var_expression(implicit, VE_lvalue, loc);
1b4fb1e0 4374
4375 // If we are not copying a slice initializer to the heap,
4376 // then it can be changed by the program, so if it can
4377 // contain pointers we must register it as a GC root.
4378 if (this->is_slice_init_
4379 && !copy_to_heap
4380 && this->expr_->type()->has_pointer())
4381 {
4382 Bexpression* root =
d4e6573e 4383 gogo->backend()->var_expression(implicit, VE_lvalue, loc);
1b4fb1e0 4384 root = gogo->backend()->address_expression(root, loc);
4385 Type* type = Type::make_pointer_type(this->expr_->type());
4386 gogo->add_gc_root(Expression::make_backend(root, type, loc));
4387 }
76f85fd6 4388 }
4389 else if ((this->expr_->is_composite_literal()
3ae06f68 4390 || this->expr_->string_expression() != NULL)
4391 && this->expr_->is_static_initializer())
f9ca30f9 4392 {
76f85fd6 4393 // Build a decl for a constant constructor.
f9ca30f9 4394 snprintf(buf, sizeof buf, "C%u", counter);
4395 ++counter;
4396
438b4bec 4397 std::string asm_name(go_selectively_encode_id(buf));
f9ca30f9 4398 Bvariable* decl =
438b4bec 4399 gogo->backend()->immutable_struct(buf, asm_name,
4400 true, false, btype, loc);
f9ca30f9 4401 gogo->backend()->immutable_struct_set_init(decl, buf, true, false,
4402 btype, loc, bexpr);
d4e6573e 4403 bexpr = gogo->backend()->var_expression(decl, VE_lvalue, loc);
f9ca30f9 4404 }
09ea332d 4405
f9ca30f9 4406 go_assert(!this->create_temp_ || this->expr_->is_variable());
4407 ret = gogo->backend()->address_expression(bexpr, loc);
4408 break;
e440a328 4409
4410 case OPERATOR_MULT:
4411 {
f9ca30f9 4412 go_assert(this->expr_->type()->points_to() != NULL);
e440a328 4413
4414 // If we are dereferencing the pointer to a large struct, we
4415 // need to check for nil. We don't bother to check for small
4416 // structs because we expect the system to crash on a nil
56080003 4417 // pointer dereference. However, if we know the address of this
4418 // expression is being taken, we must always check for nil.
f9ca30f9 4419
4420 Type* ptype = this->expr_->type()->points_to();
4421 Btype* pbtype = ptype->get_backend(gogo);
4422 if (!ptype->is_void_type())
e440a328 4423 {
2a305b85 4424 int64_t s;
4425 bool ok = ptype->backend_type_size(gogo, &s);
4426 if (!ok)
4427 {
4428 go_assert(saw_errors());
4429 return gogo->backend()->error_expression();
4430 }
f9ca30f9 4431 if (s >= 4096 || this->issue_nil_check_)
19b4f09b 4432 {
f9ca30f9 4433 go_assert(this->expr_->is_variable());
ea664253 4434 Bexpression* nil =
4435 Expression::make_nil(loc)->get_backend(context);
f9ca30f9 4436 Bexpression* compare =
4437 gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr,
4438 nil, loc);
f9ca30f9 4439 Bexpression* crash =
ea664253 4440 gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4441 loc)->get_backend(context);
93715b75 4442 Bfunction* bfn = context->function()->func_value()->get_decl();
4443 bexpr = gogo->backend()->conditional_expression(bfn, btype,
4444 compare,
f9ca30f9 4445 crash, bexpr,
4446 loc);
4447
19b4f09b 4448 }
e440a328 4449 }
9b27b43c 4450 ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc);
e440a328 4451 }
f9ca30f9 4452 break;
e440a328 4453
4454 default:
c3e6f413 4455 go_unreachable();
e440a328 4456 }
f9ca30f9 4457
ea664253 4458 return ret;
e440a328 4459}
4460
4461// Export a unary expression.
4462
4463void
4464Unary_expression::do_export(Export* exp) const
4465{
4466 switch (this->op_)
4467 {
4468 case OPERATOR_PLUS:
4469 exp->write_c_string("+ ");
4470 break;
4471 case OPERATOR_MINUS:
4472 exp->write_c_string("- ");
4473 break;
4474 case OPERATOR_NOT:
4475 exp->write_c_string("! ");
4476 break;
4477 case OPERATOR_XOR:
4478 exp->write_c_string("^ ");
4479 break;
4480 case OPERATOR_AND:
4481 case OPERATOR_MULT:
4482 default:
c3e6f413 4483 go_unreachable();
e440a328 4484 }
4485 this->expr_->export_expression(exp);
4486}
4487
4488// Import a unary expression.
4489
4490Expression*
4491Unary_expression::do_import(Import* imp)
4492{
4493 Operator op;
4494 switch (imp->get_char())
4495 {
4496 case '+':
4497 op = OPERATOR_PLUS;
4498 break;
4499 case '-':
4500 op = OPERATOR_MINUS;
4501 break;
4502 case '!':
4503 op = OPERATOR_NOT;
4504 break;
4505 case '^':
4506 op = OPERATOR_XOR;
4507 break;
4508 default:
c3e6f413 4509 go_unreachable();
e440a328 4510 }
4511 imp->require_c_string(" ");
4512 Expression* expr = Expression::import_expression(imp);
4513 return Expression::make_unary(op, expr, imp->location());
4514}
4515
d751bb78 4516// Dump ast representation of an unary expression.
4517
4518void
4519Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4520{
4521 ast_dump_context->dump_operator(this->op_);
4522 ast_dump_context->ostream() << "(";
4523 ast_dump_context->dump_expression(this->expr_);
4524 ast_dump_context->ostream() << ") ";
4525}
4526
e440a328 4527// Make a unary expression.
4528
4529Expression*
b13c66cd 4530Expression::make_unary(Operator op, Expression* expr, Location location)
e440a328 4531{
4532 return new Unary_expression(op, expr, location);
4533}
4534
4535// If this is an indirection through a pointer, return the expression
4536// being pointed through. Otherwise return this.
4537
4538Expression*
4539Expression::deref()
4540{
4541 if (this->classification_ == EXPRESSION_UNARY)
4542 {
4543 Unary_expression* ue = static_cast<Unary_expression*>(this);
4544 if (ue->op() == OPERATOR_MULT)
4545 return ue->operand();
4546 }
4547 return this;
4548}
4549
4550// Class Binary_expression.
4551
4552// Traversal.
4553
4554int
4555Binary_expression::do_traverse(Traverse* traverse)
4556{
4557 int t = Expression::traverse(&this->left_, traverse);
4558 if (t == TRAVERSE_EXIT)
4559 return TRAVERSE_EXIT;
4560 return Expression::traverse(&this->right_, traverse);
4561}
4562
3ae06f68 4563// Return whether this expression may be used as a static initializer.
4564
4565bool
4566Binary_expression::do_is_static_initializer() const
4567{
4568 if (!this->left_->is_static_initializer()
4569 || !this->right_->is_static_initializer())
4570 return false;
4571
4572 // Addresses can be static initializers, but we can't implement
4573 // arbitray binary expressions of them.
4574 Unary_expression* lu = this->left_->unary_expression();
4575 Unary_expression* ru = this->right_->unary_expression();
4576 if (lu != NULL && lu->op() == OPERATOR_AND)
4577 {
4578 if (ru != NULL && ru->op() == OPERATOR_AND)
4579 return this->op_ == OPERATOR_MINUS;
4580 else
4581 return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS;
4582 }
4583 else if (ru != NULL && ru->op() == OPERATOR_AND)
4584 return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS;
4585
4586 // Other cases should resolve in the backend.
4587 return true;
4588}
4589
0c77715b 4590// Return the type to use for a binary operation on operands of
4591// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4592// such may be NULL or abstract.
4593
4594bool
4595Binary_expression::operation_type(Operator op, Type* left_type,
4596 Type* right_type, Type** result_type)
4597{
4598 if (left_type != right_type
4599 && !left_type->is_abstract()
4600 && !right_type->is_abstract()
4601 && left_type->base() != right_type->base()
4602 && op != OPERATOR_LSHIFT
4603 && op != OPERATOR_RSHIFT)
4604 {
4605 // May be a type error--let it be diagnosed elsewhere.
4606 return false;
4607 }
4608
4609 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4610 {
4611 if (left_type->integer_type() != NULL)
4612 *result_type = left_type;
4613 else
4614 *result_type = Type::make_abstract_integer_type();
4615 }
4616 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
4617 *result_type = left_type;
4618 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
4619 *result_type = right_type;
4620 else if (!left_type->is_abstract())
4621 *result_type = left_type;
4622 else if (!right_type->is_abstract())
4623 *result_type = right_type;
4624 else if (left_type->complex_type() != NULL)
4625 *result_type = left_type;
4626 else if (right_type->complex_type() != NULL)
4627 *result_type = right_type;
4628 else if (left_type->float_type() != NULL)
4629 *result_type = left_type;
4630 else if (right_type->float_type() != NULL)
4631 *result_type = right_type;
4632 else if (left_type->integer_type() != NULL
4633 && left_type->integer_type()->is_rune())
4634 *result_type = left_type;
4635 else if (right_type->integer_type() != NULL
4636 && right_type->integer_type()->is_rune())
4637 *result_type = right_type;
4638 else
4639 *result_type = left_type;
4640
4641 return true;
4642}
4643
4644// Convert an integer comparison code and an operator to a boolean
4645// value.
e440a328 4646
4647bool
0c77715b 4648Binary_expression::cmp_to_bool(Operator op, int cmp)
e440a328 4649{
e440a328 4650 switch (op)
4651 {
4652 case OPERATOR_EQEQ:
0c77715b 4653 return cmp == 0;
4654 break;
e440a328 4655 case OPERATOR_NOTEQ:
0c77715b 4656 return cmp != 0;
4657 break;
e440a328 4658 case OPERATOR_LT:
0c77715b 4659 return cmp < 0;
4660 break;
e440a328 4661 case OPERATOR_LE:
0c77715b 4662 return cmp <= 0;
e440a328 4663 case OPERATOR_GT:
0c77715b 4664 return cmp > 0;
e440a328 4665 case OPERATOR_GE:
0c77715b 4666 return cmp >= 0;
e440a328 4667 default:
c3e6f413 4668 go_unreachable();
e440a328 4669 }
4670}
4671
0c77715b 4672// Compare constants according to OP.
e440a328 4673
4674bool
0c77715b 4675Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
4676 Numeric_constant* right_nc,
4677 Location location, bool* result)
e440a328 4678{
0c77715b 4679 Type* left_type = left_nc->type();
4680 Type* right_type = right_nc->type();
4681
4682 Type* type;
4683 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4684 return false;
4685
4686 // When comparing an untyped operand to a typed operand, we are
4687 // effectively coercing the untyped operand to the other operand's
4688 // type, so make sure that is valid.
4689 if (!left_nc->set_type(type, true, location)
4690 || !right_nc->set_type(type, true, location))
4691 return false;
4692
4693 bool ret;
4694 int cmp;
4695 if (type->complex_type() != NULL)
4696 {
4697 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
4698 return false;
4699 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
4700 }
4701 else if (type->float_type() != NULL)
4702 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
e440a328 4703 else
0c77715b 4704 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
4705
4706 if (ret)
4707 *result = Binary_expression::cmp_to_bool(op, cmp);
4708
4709 return ret;
4710}
4711
4712// Compare integer constants.
4713
4714bool
4715Binary_expression::compare_integer(const Numeric_constant* left_nc,
4716 const Numeric_constant* right_nc,
4717 int* cmp)
4718{
4719 mpz_t left_val;
4720 if (!left_nc->to_int(&left_val))
4721 return false;
4722 mpz_t right_val;
4723 if (!right_nc->to_int(&right_val))
e440a328 4724 {
0c77715b 4725 mpz_clear(left_val);
4726 return false;
e440a328 4727 }
0c77715b 4728
4729 *cmp = mpz_cmp(left_val, right_val);
4730
4731 mpz_clear(left_val);
4732 mpz_clear(right_val);
4733
4734 return true;
4735}
4736
4737// Compare floating point constants.
4738
4739bool
4740Binary_expression::compare_float(const Numeric_constant* left_nc,
4741 const Numeric_constant* right_nc,
4742 int* cmp)
4743{
4744 mpfr_t left_val;
4745 if (!left_nc->to_float(&left_val))
4746 return false;
4747 mpfr_t right_val;
4748 if (!right_nc->to_float(&right_val))
e440a328 4749 {
0c77715b 4750 mpfr_clear(left_val);
4751 return false;
4752 }
4753
4754 // We already coerced both operands to the same type. If that type
4755 // is not an abstract type, we need to round the values accordingly.
4756 Type* type = left_nc->type();
4757 if (!type->is_abstract() && type->float_type() != NULL)
4758 {
4759 int bits = type->float_type()->bits();
4760 mpfr_prec_round(left_val, bits, GMP_RNDN);
4761 mpfr_prec_round(right_val, bits, GMP_RNDN);
e440a328 4762 }
0c77715b 4763
4764 *cmp = mpfr_cmp(left_val, right_val);
4765
4766 mpfr_clear(left_val);
4767 mpfr_clear(right_val);
4768
4769 return true;
e440a328 4770}
4771
0c77715b 4772// Compare complex constants. Complex numbers may only be compared
4773// for equality.
e440a328 4774
4775bool
0c77715b 4776Binary_expression::compare_complex(const Numeric_constant* left_nc,
4777 const Numeric_constant* right_nc,
4778 int* cmp)
e440a328 4779{
fcbea5e4 4780 mpc_t left_val;
4781 if (!left_nc->to_complex(&left_val))
0c77715b 4782 return false;
fcbea5e4 4783 mpc_t right_val;
4784 if (!right_nc->to_complex(&right_val))
e440a328 4785 {
fcbea5e4 4786 mpc_clear(left_val);
0c77715b 4787 return false;
e440a328 4788 }
0c77715b 4789
4790 // We already coerced both operands to the same type. If that type
4791 // is not an abstract type, we need to round the values accordingly.
4792 Type* type = left_nc->type();
4793 if (!type->is_abstract() && type->complex_type() != NULL)
e440a328 4794 {
0c77715b 4795 int bits = type->complex_type()->bits();
fcbea5e4 4796 mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN);
4797 mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN);
4798 mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN);
4799 mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN);
e440a328 4800 }
0c77715b 4801
fcbea5e4 4802 *cmp = mpc_cmp(left_val, right_val) != 0;
0c77715b 4803
fcbea5e4 4804 mpc_clear(left_val);
4805 mpc_clear(right_val);
0c77715b 4806
4807 return true;
e440a328 4808}
4809
0c77715b 4810// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4811// true if this could be done, false if not. Issue errors at LOCATION
af7a5274 4812// as appropriate, and sets *ISSUED_ERROR if it did.
e440a328 4813
4814bool
0c77715b 4815Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
4816 Numeric_constant* right_nc,
af7a5274 4817 Location location, Numeric_constant* nc,
4818 bool* issued_error)
e440a328 4819{
af7a5274 4820 *issued_error = false;
e440a328 4821 switch (op)
4822 {
4823 case OPERATOR_OROR:
4824 case OPERATOR_ANDAND:
4825 case OPERATOR_EQEQ:
4826 case OPERATOR_NOTEQ:
4827 case OPERATOR_LT:
4828 case OPERATOR_LE:
4829 case OPERATOR_GT:
4830 case OPERATOR_GE:
9767e2d3 4831 // These return boolean values, not numeric.
4832 return false;
0c77715b 4833 default:
4834 break;
4835 }
4836
4837 Type* left_type = left_nc->type();
4838 Type* right_type = right_nc->type();
4839
4840 Type* type;
4841 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4842 return false;
4843
4844 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
4845
4846 // When combining an untyped operand with a typed operand, we are
4847 // effectively coercing the untyped operand to the other operand's
4848 // type, so make sure that is valid.
4849 if (!left_nc->set_type(type, true, location))
4850 return false;
4851 if (!is_shift && !right_nc->set_type(type, true, location))
4852 return false;
85334a21 4853 if (is_shift
4854 && ((left_type->integer_type() == NULL
4855 && !left_type->is_abstract())
4856 || (right_type->integer_type() == NULL
4857 && !right_type->is_abstract())))
4858 return false;
0c77715b 4859
4860 bool r;
4861 if (type->complex_type() != NULL)
4862 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc);
4863 else if (type->float_type() != NULL)
4864 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc);
4865 else
4866 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
4867
4868 if (r)
af7a5274 4869 {
4870 r = nc->set_type(type, true, location);
4871 if (!r)
4872 *issued_error = true;
4873 }
0c77715b 4874
4875 return r;
4876}
4877
4878// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4879// integer operations. Return true if this could be done, false if
4880// not.
4881
4882bool
4883Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
4884 const Numeric_constant* right_nc,
4885 Location location, Numeric_constant* nc)
4886{
4887 mpz_t left_val;
4888 if (!left_nc->to_int(&left_val))
4889 return false;
4890 mpz_t right_val;
4891 if (!right_nc->to_int(&right_val))
4892 {
4893 mpz_clear(left_val);
e440a328 4894 return false;
0c77715b 4895 }
4896
4897 mpz_t val;
4898 mpz_init(val);
4899
4900 switch (op)
4901 {
e440a328 4902 case OPERATOR_PLUS:
4903 mpz_add(val, left_val, right_val);
2c809f8f 4904 if (mpz_sizeinbase(val, 2) > 0x100000)
4905 {
631d5788 4906 go_error_at(location, "constant addition overflow");
71a45216 4907 nc->set_invalid();
2c809f8f 4908 mpz_set_ui(val, 1);
4909 }
e440a328 4910 break;
4911 case OPERATOR_MINUS:
4912 mpz_sub(val, left_val, right_val);
2c809f8f 4913 if (mpz_sizeinbase(val, 2) > 0x100000)
4914 {
631d5788 4915 go_error_at(location, "constant subtraction overflow");
71a45216 4916 nc->set_invalid();
2c809f8f 4917 mpz_set_ui(val, 1);
4918 }
e440a328 4919 break;
4920 case OPERATOR_OR:
4921 mpz_ior(val, left_val, right_val);
4922 break;
4923 case OPERATOR_XOR:
4924 mpz_xor(val, left_val, right_val);
4925 break;
4926 case OPERATOR_MULT:
4927 mpz_mul(val, left_val, right_val);
2c809f8f 4928 if (mpz_sizeinbase(val, 2) > 0x100000)
4929 {
631d5788 4930 go_error_at(location, "constant multiplication overflow");
71a45216 4931 nc->set_invalid();
2c809f8f 4932 mpz_set_ui(val, 1);
4933 }
e440a328 4934 break;
4935 case OPERATOR_DIV:
4936 if (mpz_sgn(right_val) != 0)
4937 mpz_tdiv_q(val, left_val, right_val);
4938 else
4939 {
631d5788 4940 go_error_at(location, "division by zero");
71a45216 4941 nc->set_invalid();
e440a328 4942 mpz_set_ui(val, 0);
e440a328 4943 }
4944 break;
4945 case OPERATOR_MOD:
4946 if (mpz_sgn(right_val) != 0)
4947 mpz_tdiv_r(val, left_val, right_val);
4948 else
4949 {
631d5788 4950 go_error_at(location, "division by zero");
71a45216 4951 nc->set_invalid();
e440a328 4952 mpz_set_ui(val, 0);
e440a328 4953 }
4954 break;
4955 case OPERATOR_LSHIFT:
4956 {
4957 unsigned long shift = mpz_get_ui(right_val);
0c77715b 4958 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
4959 mpz_mul_2exp(val, left_val, shift);
4960 else
e440a328 4961 {
631d5788 4962 go_error_at(location, "shift count overflow");
71a45216 4963 nc->set_invalid();
2c809f8f 4964 mpz_set_ui(val, 1);
e440a328 4965 }
e440a328 4966 break;
4967 }
4968 break;
4969 case OPERATOR_RSHIFT:
4970 {
4971 unsigned long shift = mpz_get_ui(right_val);
4972 if (mpz_cmp_ui(right_val, shift) != 0)
4973 {
631d5788 4974 go_error_at(location, "shift count overflow");
71a45216 4975 nc->set_invalid();
2c809f8f 4976 mpz_set_ui(val, 1);
e440a328 4977 }
e440a328 4978 else
0c77715b 4979 {
4980 if (mpz_cmp_ui(left_val, 0) >= 0)
4981 mpz_tdiv_q_2exp(val, left_val, shift);
4982 else
4983 mpz_fdiv_q_2exp(val, left_val, shift);
4984 }
e440a328 4985 break;
4986 }
4987 break;
4988 case OPERATOR_AND:
4989 mpz_and(val, left_val, right_val);
4990 break;
4991 case OPERATOR_BITCLEAR:
4992 {
4993 mpz_t tval;
4994 mpz_init(tval);
4995 mpz_com(tval, right_val);
4996 mpz_and(val, left_val, tval);
4997 mpz_clear(tval);
4998 }
4999 break;
5000 default:
c3e6f413 5001 go_unreachable();
e440a328 5002 }
5003
0c77715b 5004 mpz_clear(left_val);
5005 mpz_clear(right_val);
e440a328 5006
0c77715b 5007 if (left_nc->is_rune()
5008 || (op != OPERATOR_LSHIFT
5009 && op != OPERATOR_RSHIFT
5010 && right_nc->is_rune()))
5011 nc->set_rune(NULL, val);
5012 else
5013 nc->set_int(NULL, val);
5014
5015 mpz_clear(val);
e440a328 5016
5017 return true;
5018}
5019
0c77715b 5020// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5021// floating point operations. Return true if this could be done,
5022// false if not.
e440a328 5023
5024bool
0c77715b 5025Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
5026 const Numeric_constant* right_nc,
5027 Location location, Numeric_constant* nc)
e440a328 5028{
0c77715b 5029 mpfr_t left_val;
5030 if (!left_nc->to_float(&left_val))
5031 return false;
5032 mpfr_t right_val;
5033 if (!right_nc->to_float(&right_val))
e440a328 5034 {
0c77715b 5035 mpfr_clear(left_val);
e440a328 5036 return false;
0c77715b 5037 }
5038
5039 mpfr_t val;
5040 mpfr_init(val);
5041
5042 bool ret = true;
5043 switch (op)
5044 {
e440a328 5045 case OPERATOR_PLUS:
5046 mpfr_add(val, left_val, right_val, GMP_RNDN);
5047 break;
5048 case OPERATOR_MINUS:
5049 mpfr_sub(val, left_val, right_val, GMP_RNDN);
5050 break;
5051 case OPERATOR_OR:
5052 case OPERATOR_XOR:
5053 case OPERATOR_AND:
5054 case OPERATOR_BITCLEAR:
0c77715b 5055 case OPERATOR_MOD:
5056 case OPERATOR_LSHIFT:
5057 case OPERATOR_RSHIFT:
5058 mpfr_set_ui(val, 0, GMP_RNDN);
5059 ret = false;
5060 break;
e440a328 5061 case OPERATOR_MULT:
5062 mpfr_mul(val, left_val, right_val, GMP_RNDN);
5063 break;
5064 case OPERATOR_DIV:
0c77715b 5065 if (!mpfr_zero_p(right_val))
5066 mpfr_div(val, left_val, right_val, GMP_RNDN);
5067 else
5068 {
631d5788 5069 go_error_at(location, "division by zero");
71a45216 5070 nc->set_invalid();
0c77715b 5071 mpfr_set_ui(val, 0, GMP_RNDN);
5072 }
e440a328 5073 break;
e440a328 5074 default:
c3e6f413 5075 go_unreachable();
e440a328 5076 }
5077
0c77715b 5078 mpfr_clear(left_val);
5079 mpfr_clear(right_val);
e440a328 5080
0c77715b 5081 nc->set_float(NULL, val);
5082 mpfr_clear(val);
e440a328 5083
0c77715b 5084 return ret;
e440a328 5085}
5086
0c77715b 5087// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
5088// complex operations. Return true if this could be done, false if
5089// not.
e440a328 5090
5091bool
0c77715b 5092Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
5093 const Numeric_constant* right_nc,
5094 Location location, Numeric_constant* nc)
e440a328 5095{
fcbea5e4 5096 mpc_t left_val;
5097 if (!left_nc->to_complex(&left_val))
0c77715b 5098 return false;
fcbea5e4 5099 mpc_t right_val;
5100 if (!right_nc->to_complex(&right_val))
e440a328 5101 {
fcbea5e4 5102 mpc_clear(left_val);
e440a328 5103 return false;
0c77715b 5104 }
5105
fcbea5e4 5106 mpc_t val;
5107 mpc_init2(val, mpc_precision);
0c77715b 5108
5109 bool ret = true;
5110 switch (op)
5111 {
e440a328 5112 case OPERATOR_PLUS:
fcbea5e4 5113 mpc_add(val, left_val, right_val, MPC_RNDNN);
e440a328 5114 break;
5115 case OPERATOR_MINUS:
fcbea5e4 5116 mpc_sub(val, left_val, right_val, MPC_RNDNN);
e440a328 5117 break;
5118 case OPERATOR_OR:
5119 case OPERATOR_XOR:
5120 case OPERATOR_AND:
5121 case OPERATOR_BITCLEAR:
0c77715b 5122 case OPERATOR_MOD:
5123 case OPERATOR_LSHIFT:
5124 case OPERATOR_RSHIFT:
fcbea5e4 5125 mpc_set_ui(val, 0, MPC_RNDNN);
0c77715b 5126 ret = false;
5127 break;
e440a328 5128 case OPERATOR_MULT:
fcbea5e4 5129 mpc_mul(val, left_val, right_val, MPC_RNDNN);
e440a328 5130 break;
5131 case OPERATOR_DIV:
fcbea5e4 5132 if (mpc_cmp_si(right_val, 0) == 0)
5133 {
631d5788 5134 go_error_at(location, "division by zero");
71a45216 5135 nc->set_invalid();
fcbea5e4 5136 mpc_set_ui(val, 0, MPC_RNDNN);
5137 break;
5138 }
5139 mpc_div(val, left_val, right_val, MPC_RNDNN);
e440a328 5140 break;
e440a328 5141 default:
c3e6f413 5142 go_unreachable();
e440a328 5143 }
5144
fcbea5e4 5145 mpc_clear(left_val);
5146 mpc_clear(right_val);
e440a328 5147
fcbea5e4 5148 nc->set_complex(NULL, val);
5149 mpc_clear(val);
e440a328 5150
0c77715b 5151 return ret;
e440a328 5152}
5153
5154// Lower a binary expression. We have to evaluate constant
5155// expressions now, in order to implement Go's unlimited precision
5156// constants.
5157
5158Expression*
e9d3367e 5159Binary_expression::do_lower(Gogo* gogo, Named_object*,
5160 Statement_inserter* inserter, int)
e440a328 5161{
b13c66cd 5162 Location location = this->location();
e440a328 5163 Operator op = this->op_;
5164 Expression* left = this->left_;
5165 Expression* right = this->right_;
5166
5167 const bool is_comparison = (op == OPERATOR_EQEQ
5168 || op == OPERATOR_NOTEQ
5169 || op == OPERATOR_LT
5170 || op == OPERATOR_LE
5171 || op == OPERATOR_GT
5172 || op == OPERATOR_GE);
5173
0c77715b 5174 // Numeric constant expressions.
e440a328 5175 {
0c77715b 5176 Numeric_constant left_nc;
5177 Numeric_constant right_nc;
5178 if (left->numeric_constant_value(&left_nc)
5179 && right->numeric_constant_value(&right_nc))
e440a328 5180 {
0c77715b 5181 if (is_comparison)
e440a328 5182 {
0c77715b 5183 bool result;
5184 if (!Binary_expression::compare_constant(op, &left_nc,
5185 &right_nc, location,
5186 &result))
5187 return this;
e90c9dfc 5188 return Expression::make_cast(Type::make_boolean_type(),
0c77715b 5189 Expression::make_boolean(result,
5190 location),
5191 location);
e440a328 5192 }
5193 else
5194 {
0c77715b 5195 Numeric_constant nc;
af7a5274 5196 bool issued_error;
0c77715b 5197 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
af7a5274 5198 location, &nc,
5199 &issued_error))
5200 {
5201 if (issued_error)
5202 return Expression::make_error(location);
71a45216 5203 return this;
af7a5274 5204 }
0c77715b 5205 return nc.expression(location);
e440a328 5206 }
5207 }
e440a328 5208 }
5209
5210 // String constant expressions.
315fa98d 5211 if (left->type()->is_string_type() && right->type()->is_string_type())
e440a328 5212 {
5213 std::string left_string;
5214 std::string right_string;
5215 if (left->string_constant_value(&left_string)
5216 && right->string_constant_value(&right_string))
315fa98d 5217 {
5218 if (op == OPERATOR_PLUS)
5219 return Expression::make_string(left_string + right_string,
5220 location);
5221 else if (is_comparison)
5222 {
5223 int cmp = left_string.compare(right_string);
0c77715b 5224 bool r = Binary_expression::cmp_to_bool(op, cmp);
e90c9dfc 5225 return Expression::make_boolean(r, location);
b40dc774 5226 }
5227 }
b40dc774 5228 }
5229
ceeb12d7 5230 // Lower struct, array, and some interface comparisons.
e9d3367e 5231 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5232 {
b79832ca 5233 if (left->type()->struct_type() != NULL
5234 && right->type()->struct_type() != NULL)
e9d3367e 5235 return this->lower_struct_comparison(gogo, inserter);
5236 else if (left->type()->array_type() != NULL
b79832ca 5237 && !left->type()->is_slice_type()
5238 && right->type()->array_type() != NULL
5239 && !right->type()->is_slice_type())
e9d3367e 5240 return this->lower_array_comparison(gogo, inserter);
ceeb12d7 5241 else if ((left->type()->interface_type() != NULL
5242 && right->type()->interface_type() == NULL)
5243 || (left->type()->interface_type() == NULL
5244 && right->type()->interface_type() != NULL))
5245 return this->lower_interface_value_comparison(gogo, inserter);
e9d3367e 5246 }
5247
736a16ba 5248 // Lower string concatenation to String_concat_expression, so that
5249 // we can group sequences of string additions.
5250 if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS)
5251 {
5252 Expression_list* exprs;
5253 String_concat_expression* left_sce =
5254 this->left_->string_concat_expression();
5255 if (left_sce != NULL)
5256 exprs = left_sce->exprs();
5257 else
5258 {
5259 exprs = new Expression_list();
5260 exprs->push_back(this->left_);
5261 }
5262
5263 String_concat_expression* right_sce =
5264 this->right_->string_concat_expression();
5265 if (right_sce != NULL)
5266 exprs->append(right_sce->exprs());
5267 else
5268 exprs->push_back(this->right_);
5269
5270 return Expression::make_string_concat(exprs);
5271 }
5272
e440a328 5273 return this;
5274}
5275
e9d3367e 5276// Lower a struct comparison.
5277
5278Expression*
5279Binary_expression::lower_struct_comparison(Gogo* gogo,
5280 Statement_inserter* inserter)
5281{
5282 Struct_type* st = this->left_->type()->struct_type();
5283 Struct_type* st2 = this->right_->type()->struct_type();
5284 if (st2 == NULL)
5285 return this;
5286 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5287 return this;
5288 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5289 this->right_->type(), NULL))
5290 return this;
5291
5292 // See if we can compare using memcmp. As a heuristic, we use
5293 // memcmp rather than field references and comparisons if there are
5294 // more than two fields.
113ef6a5 5295 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
e9d3367e 5296 return this->lower_compare_to_memcmp(gogo, inserter);
5297
5298 Location loc = this->location();
5299
5300 Expression* left = this->left_;
5301 Temporary_statement* left_temp = NULL;
5302 if (left->var_expression() == NULL
5303 && left->temporary_reference_expression() == NULL)
5304 {
5305 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5306 inserter->insert(left_temp);
5307 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5308 }
5309
5310 Expression* right = this->right_;
5311 Temporary_statement* right_temp = NULL;
5312 if (right->var_expression() == NULL
5313 && right->temporary_reference_expression() == NULL)
5314 {
5315 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5316 inserter->insert(right_temp);
5317 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5318 }
5319
5320 Expression* ret = Expression::make_boolean(true, loc);
5321 const Struct_field_list* fields = st->fields();
5322 unsigned int field_index = 0;
5323 for (Struct_field_list::const_iterator pf = fields->begin();
5324 pf != fields->end();
5325 ++pf, ++field_index)
5326 {
f5165c05 5327 if (Gogo::is_sink_name(pf->field_name()))
5328 continue;
5329
e9d3367e 5330 if (field_index > 0)
5331 {
5332 if (left_temp == NULL)
5333 left = left->copy();
5334 else
5335 left = Expression::make_temporary_reference(left_temp, loc);
5336 if (right_temp == NULL)
5337 right = right->copy();
5338 else
5339 right = Expression::make_temporary_reference(right_temp, loc);
5340 }
5341 Expression* f1 = Expression::make_field_reference(left, field_index,
5342 loc);
5343 Expression* f2 = Expression::make_field_reference(right, field_index,
5344 loc);
5345 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5346 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5347 }
5348
5349 if (this->op_ == OPERATOR_NOTEQ)
5350 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5351
5352 return ret;
5353}
5354
5355// Lower an array comparison.
5356
5357Expression*
5358Binary_expression::lower_array_comparison(Gogo* gogo,
5359 Statement_inserter* inserter)
5360{
5361 Array_type* at = this->left_->type()->array_type();
5362 Array_type* at2 = this->right_->type()->array_type();
5363 if (at2 == NULL)
5364 return this;
5365 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5366 return this;
5367 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5368 this->right_->type(), NULL))
5369 return this;
5370
5371 // Call memcmp directly if possible. This may let the middle-end
5372 // optimize the call.
113ef6a5 5373 if (at->compare_is_identity(gogo))
e9d3367e 5374 return this->lower_compare_to_memcmp(gogo, inserter);
5375
5376 // Call the array comparison function.
5377 Named_object* hash_fn;
5378 Named_object* equal_fn;
5379 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5380 &hash_fn, &equal_fn);
5381
5382 Location loc = this->location();
5383
5384 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5385
5386 Expression_list* args = new Expression_list();
5387 args->push_back(this->operand_address(inserter, this->left_));
5388 args->push_back(this->operand_address(inserter, this->right_));
e9d3367e 5389
5390 Expression* ret = Expression::make_call(func, args, false, loc);
5391
5392 if (this->op_ == OPERATOR_NOTEQ)
5393 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5394
5395 return ret;
5396}
5397
ceeb12d7 5398// Lower an interface to value comparison.
5399
5400Expression*
5401Binary_expression::lower_interface_value_comparison(Gogo*,
5402 Statement_inserter* inserter)
5403{
5404 Type* left_type = this->left_->type();
5405 Type* right_type = this->right_->type();
5406 Interface_type* ift;
5407 if (left_type->interface_type() != NULL)
5408 {
5409 ift = left_type->interface_type();
5410 if (!ift->implements_interface(right_type, NULL))
5411 return this;
5412 }
5413 else
5414 {
5415 ift = right_type->interface_type();
5416 if (!ift->implements_interface(left_type, NULL))
5417 return this;
5418 }
5419 if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL))
5420 return this;
5421
5422 Location loc = this->location();
5423
5424 if (left_type->interface_type() == NULL
5425 && left_type->points_to() == NULL
5426 && !this->left_->is_addressable())
5427 {
5428 Temporary_statement* temp =
5429 Statement::make_temporary(left_type, NULL, loc);
5430 inserter->insert(temp);
5431 this->left_ =
5432 Expression::make_set_and_use_temporary(temp, this->left_, loc);
5433 }
5434
5435 if (right_type->interface_type() == NULL
5436 && right_type->points_to() == NULL
5437 && !this->right_->is_addressable())
5438 {
5439 Temporary_statement* temp =
5440 Statement::make_temporary(right_type, NULL, loc);
5441 inserter->insert(temp);
5442 this->right_ =
5443 Expression::make_set_and_use_temporary(temp, this->right_, loc);
5444 }
5445
5446 return this;
5447}
5448
e9d3367e 5449// Lower a struct or array comparison to a call to memcmp.
5450
5451Expression*
5452Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5453{
5454 Location loc = this->location();
5455
5456 Expression* a1 = this->operand_address(inserter, this->left_);
5457 Expression* a2 = this->operand_address(inserter, this->right_);
5458 Expression* len = Expression::make_type_info(this->left_->type(),
5459 TYPE_INFO_SIZE);
5460
5461 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
e67508fa 5462 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
e9d3367e 5463 return Expression::make_binary(this->op_, call, zero, loc);
5464}
5465
a32698ee 5466Expression*
5c3f3470 5467Binary_expression::do_flatten(Gogo* gogo, Named_object*,
a32698ee 5468 Statement_inserter* inserter)
5469{
5470 Location loc = this->location();
5bf8be8b 5471 if (this->left_->type()->is_error_type()
5472 || this->right_->type()->is_error_type()
5473 || this->left_->is_error_expression()
5474 || this->right_->is_error_expression())
5475 {
5476 go_assert(saw_errors());
5477 return Expression::make_error(loc);
5478 }
5479
a32698ee 5480 Temporary_statement* temp;
a32698ee 5481
5482 Type* left_type = this->left_->type();
5483 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5484 || this->op_ == OPERATOR_RSHIFT);
5485 bool is_idiv_op = ((this->op_ == OPERATOR_DIV &&
5486 left_type->integer_type() != NULL)
5487 || this->op_ == OPERATOR_MOD);
5488
a32698ee 5489 if (is_shift_op
5c3f3470 5490 || (is_idiv_op
5491 && (gogo->check_divide_by_zero() || gogo->check_divide_overflow())))
a32698ee 5492 {
545ab43b 5493 if (!this->left_->is_variable() && !this->left_->is_constant())
a32698ee 5494 {
5495 temp = Statement::make_temporary(NULL, this->left_, loc);
5496 inserter->insert(temp);
5497 this->left_ = Expression::make_temporary_reference(temp, loc);
5498 }
545ab43b 5499 if (!this->right_->is_variable() && !this->right_->is_constant())
a32698ee 5500 {
5501 temp =
5502 Statement::make_temporary(NULL, this->right_, loc);
5503 this->right_ = Expression::make_temporary_reference(temp, loc);
5504 inserter->insert(temp);
5505 }
5506 }
5507 return this;
5508}
5509
5510
e9d3367e 5511// Return the address of EXPR, cast to unsafe.Pointer.
5512
5513Expression*
5514Binary_expression::operand_address(Statement_inserter* inserter,
5515 Expression* expr)
5516{
5517 Location loc = this->location();
5518
5519 if (!expr->is_addressable())
5520 {
5521 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5522 loc);
5523 inserter->insert(temp);
5524 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5525 }
5526 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5527 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5528 Type* void_type = Type::make_void_type();
5529 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5530 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5531}
5532
0c77715b 5533// Return the numeric constant value, if it has one.
e440a328 5534
5535bool
0c77715b 5536Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 5537{
0c77715b 5538 Numeric_constant left_nc;
5539 if (!this->left_->numeric_constant_value(&left_nc))
5540 return false;
5541 Numeric_constant right_nc;
5542 if (!this->right_->numeric_constant_value(&right_nc))
5543 return false;
af7a5274 5544 bool issued_error;
9767e2d3 5545 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
af7a5274 5546 this->location(), nc, &issued_error);
e440a328 5547}
5548
5549// Note that the value is being discarded.
5550
4f2138d7 5551bool
e440a328 5552Binary_expression::do_discarding_value()
5553{
5554 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
4f2138d7 5555 return this->right_->discarding_value();
e440a328 5556 else
4f2138d7 5557 {
5558 this->unused_value_error();
5559 return false;
5560 }
e440a328 5561}
5562
5563// Get type.
5564
5565Type*
5566Binary_expression::do_type()
5567{
5f5fea79 5568 if (this->classification() == EXPRESSION_ERROR)
5569 return Type::make_error_type();
5570
e440a328 5571 switch (this->op_)
5572 {
e440a328 5573 case OPERATOR_EQEQ:
5574 case OPERATOR_NOTEQ:
5575 case OPERATOR_LT:
5576 case OPERATOR_LE:
5577 case OPERATOR_GT:
5578 case OPERATOR_GE:
e90c9dfc 5579 if (this->type_ == NULL)
5580 this->type_ = Type::make_boolean_type();
5581 return this->type_;
e440a328 5582
5583 case OPERATOR_PLUS:
5584 case OPERATOR_MINUS:
5585 case OPERATOR_OR:
5586 case OPERATOR_XOR:
5587 case OPERATOR_MULT:
5588 case OPERATOR_DIV:
5589 case OPERATOR_MOD:
5590 case OPERATOR_AND:
5591 case OPERATOR_BITCLEAR:
e90c9dfc 5592 case OPERATOR_OROR:
5593 case OPERATOR_ANDAND:
e440a328 5594 {
0c77715b 5595 Type* type;
5596 if (!Binary_expression::operation_type(this->op_,
5597 this->left_->type(),
5598 this->right_->type(),
5599 &type))
5600 return Type::make_error_type();
5601 return type;
e440a328 5602 }
5603
5604 case OPERATOR_LSHIFT:
5605 case OPERATOR_RSHIFT:
5606 return this->left_->type();
5607
5608 default:
c3e6f413 5609 go_unreachable();
e440a328 5610 }
5611}
5612
5613// Set type for a binary expression.
5614
5615void
5616Binary_expression::do_determine_type(const Type_context* context)
5617{
5618 Type* tleft = this->left_->type();
5619 Type* tright = this->right_->type();
5620
5621 // Both sides should have the same type, except for the shift
5622 // operations. For a comparison, we should ignore the incoming
5623 // type.
5624
5625 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5626 || this->op_ == OPERATOR_RSHIFT);
5627
5628 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5629 || this->op_ == OPERATOR_NOTEQ
5630 || this->op_ == OPERATOR_LT
5631 || this->op_ == OPERATOR_LE
5632 || this->op_ == OPERATOR_GT
5633 || this->op_ == OPERATOR_GE);
5634
c999c2a7 5635 // For constant expressions, the context of the result is not useful in
5636 // determining the types of the operands. It is only legal to use abstract
5637 // boolean, numeric, and string constants as operands where it is legal to
5638 // use non-abstract boolean, numeric, and string constants, respectively.
5639 // Any issues with the operation will be resolved in the check_types pass.
5640 bool is_constant_expr = (this->left_->is_constant()
5641 && this->right_->is_constant());
5642
e440a328 5643 Type_context subcontext(*context);
5644
af7a5274 5645 if (is_constant_expr)
5646 {
5647 subcontext.type = NULL;
5648 subcontext.may_be_abstract = true;
5649 }
5650 else if (is_comparison)
e440a328 5651 {
5652 // In a comparison, the context does not determine the types of
5653 // the operands.
5654 subcontext.type = NULL;
5655 }
5656
5657 // Set the context for the left hand operand.
5658 if (is_shift_op)
5659 {
b40dc774 5660 // The right hand operand of a shift plays no role in
5661 // determining the type of the left hand operand.
e440a328 5662 }
5663 else if (!tleft->is_abstract())
5664 subcontext.type = tleft;
5665 else if (!tright->is_abstract())
5666 subcontext.type = tright;
5667 else if (subcontext.type == NULL)
5668 {
5669 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5670 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5671 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5672 {
5673 // Both sides have an abstract integer, abstract float, or
5674 // abstract complex type. Just let CONTEXT determine
5675 // whether they may remain abstract or not.
5676 }
5677 else if (tleft->complex_type() != NULL)
5678 subcontext.type = tleft;
5679 else if (tright->complex_type() != NULL)
5680 subcontext.type = tright;
5681 else if (tleft->float_type() != NULL)
5682 subcontext.type = tleft;
5683 else if (tright->float_type() != NULL)
5684 subcontext.type = tright;
5685 else
5686 subcontext.type = tleft;
f58a23ae 5687
5688 if (subcontext.type != NULL && !context->may_be_abstract)
5689 subcontext.type = subcontext.type->make_non_abstract_type();
e440a328 5690 }
5691
af7a5274 5692 this->left_->determine_type(&subcontext);
e440a328 5693
e440a328 5694 if (is_shift_op)
5695 {
b40dc774 5696 // We may have inherited an unusable type for the shift operand.
5697 // Give a useful error if that happened.
5698 if (tleft->is_abstract()
5699 && subcontext.type != NULL
8ab6effb 5700 && !subcontext.may_be_abstract
f6bc81e6 5701 && subcontext.type->interface_type() == NULL
8ab6effb 5702 && subcontext.type->integer_type() == NULL)
b40dc774 5703 this->report_error(("invalid context-determined non-integer type "
8ab6effb 5704 "for left operand of shift"));
b40dc774 5705
5706 // The context for the right hand operand is the same as for the
5707 // left hand operand, except for a shift operator.
e440a328 5708 subcontext.type = Type::lookup_integer_type("uint");
5709 subcontext.may_be_abstract = false;
5710 }
5711
af7a5274 5712 this->right_->determine_type(&subcontext);
e90c9dfc 5713
5714 if (is_comparison)
5715 {
5716 if (this->type_ != NULL && !this->type_->is_abstract())
5717 ;
5718 else if (context->type != NULL && context->type->is_boolean_type())
5719 this->type_ = context->type;
5720 else if (!context->may_be_abstract)
5721 this->type_ = Type::lookup_bool_type();
5722 }
e440a328 5723}
5724
5725// Report an error if the binary operator OP does not support TYPE.
be8b5eee 5726// OTYPE is the type of the other operand. Return whether the
5727// operation is OK. This should not be used for shift.
e440a328 5728
5729bool
be8b5eee 5730Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
b13c66cd 5731 Location location)
e440a328 5732{
5733 switch (op)
5734 {
5735 case OPERATOR_OROR:
5736 case OPERATOR_ANDAND:
c999c2a7 5737 if (!type->is_boolean_type()
5738 || !otype->is_boolean_type())
e440a328 5739 {
631d5788 5740 go_error_at(location, "expected boolean type");
e440a328 5741 return false;
5742 }
5743 break;
5744
5745 case OPERATOR_EQEQ:
5746 case OPERATOR_NOTEQ:
e9d3367e 5747 {
5748 std::string reason;
5749 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
5750 {
631d5788 5751 go_error_at(location, "%s", reason.c_str());
e9d3367e 5752 return false;
5753 }
5754 }
e440a328 5755 break;
5756
5757 case OPERATOR_LT:
5758 case OPERATOR_LE:
5759 case OPERATOR_GT:
5760 case OPERATOR_GE:
e9d3367e 5761 {
5762 std::string reason;
5763 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
5764 {
631d5788 5765 go_error_at(location, "%s", reason.c_str());
e9d3367e 5766 return false;
5767 }
5768 }
e440a328 5769 break;
5770
5771 case OPERATOR_PLUS:
5772 case OPERATOR_PLUSEQ:
c999c2a7 5773 if ((!type->is_numeric_type() && !type->is_string_type())
5774 || (!otype->is_numeric_type() && !otype->is_string_type()))
e440a328 5775 {
631d5788 5776 go_error_at(location,
e440a328 5777 "expected integer, floating, complex, or string type");
5778 return false;
5779 }
5780 break;
5781
5782 case OPERATOR_MINUS:
5783 case OPERATOR_MINUSEQ:
5784 case OPERATOR_MULT:
5785 case OPERATOR_MULTEQ:
5786 case OPERATOR_DIV:
5787 case OPERATOR_DIVEQ:
c999c2a7 5788 if (!type->is_numeric_type() || !otype->is_numeric_type())
e440a328 5789 {
631d5788 5790 go_error_at(location, "expected integer, floating, or complex type");
e440a328 5791 return false;
5792 }
5793 break;
5794
5795 case OPERATOR_MOD:
5796 case OPERATOR_MODEQ:
5797 case OPERATOR_OR:
5798 case OPERATOR_OREQ:
5799 case OPERATOR_AND:
5800 case OPERATOR_ANDEQ:
5801 case OPERATOR_XOR:
5802 case OPERATOR_XOREQ:
5803 case OPERATOR_BITCLEAR:
5804 case OPERATOR_BITCLEAREQ:
c999c2a7 5805 if (type->integer_type() == NULL || otype->integer_type() == NULL)
e440a328 5806 {
631d5788 5807 go_error_at(location, "expected integer type");
e440a328 5808 return false;
5809 }
5810 break;
5811
5812 default:
c3e6f413 5813 go_unreachable();
e440a328 5814 }
5815
5816 return true;
5817}
5818
5819// Check types.
5820
5821void
5822Binary_expression::do_check_types(Gogo*)
5823{
5f5fea79 5824 if (this->classification() == EXPRESSION_ERROR)
5825 return;
5826
e440a328 5827 Type* left_type = this->left_->type();
5828 Type* right_type = this->right_->type();
5c13bd80 5829 if (left_type->is_error() || right_type->is_error())
9fe897ef 5830 {
5831 this->set_is_error();
5832 return;
5833 }
e440a328 5834
5835 if (this->op_ == OPERATOR_EQEQ
5836 || this->op_ == OPERATOR_NOTEQ
5837 || this->op_ == OPERATOR_LT
5838 || this->op_ == OPERATOR_LE
5839 || this->op_ == OPERATOR_GT
5840 || this->op_ == OPERATOR_GE)
5841 {
907c5ecd 5842 if (left_type->is_nil_type() && right_type->is_nil_type())
5843 {
5844 this->report_error(_("invalid comparison of nil with nil"));
5845 return;
5846 }
e440a328 5847 if (!Type::are_assignable(left_type, right_type, NULL)
5848 && !Type::are_assignable(right_type, left_type, NULL))
5849 {
5850 this->report_error(_("incompatible types in binary expression"));
5851 return;
5852 }
5853 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5854 right_type,
e440a328 5855 this->location())
5856 || !Binary_expression::check_operator_type(this->op_, right_type,
be8b5eee 5857 left_type,
e440a328 5858 this->location()))
5859 {
5860 this->set_is_error();
5861 return;
5862 }
5863 }
5864 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5865 {
5866 if (!Type::are_compatible_for_binop(left_type, right_type))
5867 {
5868 this->report_error(_("incompatible types in binary expression"));
5869 return;
5870 }
5871 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5872 right_type,
e440a328 5873 this->location()))
5874 {
5875 this->set_is_error();
5876 return;
5877 }
5c65b19d 5878 if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD)
5879 {
5880 // Division by a zero integer constant is an error.
5881 Numeric_constant rconst;
5882 unsigned long rval;
5883 if (left_type->integer_type() != NULL
5884 && this->right_->numeric_constant_value(&rconst)
5885 && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID
5886 && rval == 0)
5887 {
5888 this->report_error(_("integer division by zero"));
5889 return;
5890 }
5891 }
e440a328 5892 }
5893 else
5894 {
5895 if (left_type->integer_type() == NULL)
5896 this->report_error(_("shift of non-integer operand"));
5897
6b5e0fac 5898 if (right_type->is_string_type())
5899 this->report_error(_("shift count not unsigned integer"));
5900 else if (!right_type->is_abstract()
e440a328 5901 && (right_type->integer_type() == NULL
5902 || !right_type->integer_type()->is_unsigned()))
5903 this->report_error(_("shift count not unsigned integer"));
5904 else
5905 {
0c77715b 5906 Numeric_constant nc;
5907 if (this->right_->numeric_constant_value(&nc))
e440a328 5908 {
0c77715b 5909 mpz_t val;
5910 if (!nc.to_int(&val))
5911 this->report_error(_("shift count not unsigned integer"));
5912 else
a4eba91b 5913 {
0c77715b 5914 if (mpz_sgn(val) < 0)
5915 {
5916 this->report_error(_("negative shift count"));
0c77715b 5917 Location rloc = this->right_->location();
e67508fa 5918 this->right_ = Expression::make_integer_ul(0, right_type,
5919 rloc);
0c77715b 5920 }
5921 mpz_clear(val);
a4eba91b 5922 }
e440a328 5923 }
e440a328 5924 }
5925 }
5926}
5927
ea664253 5928// Get the backend representation for a binary expression.
e440a328 5929
ea664253 5930Bexpression*
5931Binary_expression::do_get_backend(Translate_context* context)
e440a328 5932{
1b1f2abf 5933 Gogo* gogo = context->gogo();
a32698ee 5934 Location loc = this->location();
5935 Type* left_type = this->left_->type();
5936 Type* right_type = this->right_->type();
1b1f2abf 5937
e440a328 5938 bool use_left_type = true;
5939 bool is_shift_op = false;
29a2d1d8 5940 bool is_idiv_op = false;
e440a328 5941 switch (this->op_)
5942 {
5943 case OPERATOR_EQEQ:
5944 case OPERATOR_NOTEQ:
5945 case OPERATOR_LT:
5946 case OPERATOR_LE:
5947 case OPERATOR_GT:
5948 case OPERATOR_GE:
ea664253 5949 return Expression::comparison(context, this->type_, this->op_,
5950 this->left_, this->right_, loc);
e440a328 5951
5952 case OPERATOR_OROR:
e440a328 5953 case OPERATOR_ANDAND:
e440a328 5954 use_left_type = false;
5955 break;
5956 case OPERATOR_PLUS:
e440a328 5957 case OPERATOR_MINUS:
e440a328 5958 case OPERATOR_OR:
e440a328 5959 case OPERATOR_XOR:
e440a328 5960 case OPERATOR_MULT:
e440a328 5961 break;
5962 case OPERATOR_DIV:
a32698ee 5963 if (left_type->float_type() != NULL || left_type->complex_type() != NULL)
5964 break;
729f8831 5965 // Fall through.
e440a328 5966 case OPERATOR_MOD:
29a2d1d8 5967 is_idiv_op = true;
e440a328 5968 break;
5969 case OPERATOR_LSHIFT:
e440a328 5970 case OPERATOR_RSHIFT:
e440a328 5971 is_shift_op = true;
5972 break;
e440a328 5973 case OPERATOR_BITCLEAR:
a32698ee 5974 this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc);
5975 case OPERATOR_AND:
e440a328 5976 break;
5977 default:
c3e6f413 5978 go_unreachable();
e440a328 5979 }
5980
736a16ba 5981 // The only binary operation for string is +, and that should have
5982 // been converted to a String_concat_expression in do_lower.
5983 go_assert(!left_type->is_string_type());
a32698ee 5984
5985 // For complex division Go might want slightly different results than the
5986 // backend implementation provides, so we have our own runtime routine.
1850e20c 5987 if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL)
5988 {
a32698ee 5989 Runtime::Function complex_code;
1850e20c 5990 switch (this->left_->type()->complex_type()->bits())
5991 {
5992 case 64:
a32698ee 5993 complex_code = Runtime::COMPLEX64_DIV;
1850e20c 5994 break;
5995 case 128:
a32698ee 5996 complex_code = Runtime::COMPLEX128_DIV;
1850e20c 5997 break;
5998 default:
5999 go_unreachable();
6000 }
a32698ee 6001 Expression* complex_div =
6002 Runtime::make_call(complex_code, loc, 2, this->left_, this->right_);
ea664253 6003 return complex_div->get_backend(context);
1850e20c 6004 }
6005
ea664253 6006 Bexpression* left = this->left_->get_backend(context);
6007 Bexpression* right = this->right_->get_backend(context);
e440a328 6008
a32698ee 6009 Type* type = use_left_type ? left_type : right_type;
6010 Btype* btype = type->get_backend(gogo);
6011
6012 Bexpression* ret =
6013 gogo->backend()->binary_expression(this->op_, left, right, loc);
6014 ret = gogo->backend()->convert_expression(btype, ret, loc);
e440a328 6015
a32698ee 6016 // Initialize overflow constants.
6017 Bexpression* overflow;
6018 mpz_t zero;
6019 mpz_init_set_ui(zero, 0UL);
6020 mpz_t one;
6021 mpz_init_set_ui(one, 1UL);
6022 mpz_t neg_one;
6023 mpz_init_set_si(neg_one, -1);
e440a328 6024
a32698ee 6025 Btype* left_btype = left_type->get_backend(gogo);
6026 Btype* right_btype = right_type->get_backend(gogo);
e440a328 6027
6028 // In Go, a shift larger than the size of the type is well-defined.
a32698ee 6029 // This is not true in C, so we need to insert a conditional.
e440a328 6030 if (is_shift_op)
6031 {
a32698ee 6032 go_assert(left_type->integer_type() != NULL);
e440a328 6033
a32698ee 6034 mpz_t bitsval;
6035 int bits = left_type->integer_type()->bits();
6036 mpz_init_set_ui(bitsval, bits);
6037 Bexpression* bits_expr =
6038 gogo->backend()->integer_constant_expression(right_btype, bitsval);
6039 Bexpression* compare =
6040 gogo->backend()->binary_expression(OPERATOR_LT,
6041 right, bits_expr, loc);
e440a328 6042
a32698ee 6043 Bexpression* zero_expr =
6044 gogo->backend()->integer_constant_expression(left_btype, zero);
6045 overflow = zero_expr;
93715b75 6046 Bfunction* bfn = context->function()->func_value()->get_decl();
e440a328 6047 if (this->op_ == OPERATOR_RSHIFT
a32698ee 6048 && !left_type->integer_type()->is_unsigned())
e440a328 6049 {
a32698ee 6050 Bexpression* neg_expr =
6051 gogo->backend()->binary_expression(OPERATOR_LT, left,
6052 zero_expr, loc);
6053 Bexpression* neg_one_expr =
6054 gogo->backend()->integer_constant_expression(left_btype, neg_one);
93715b75 6055 overflow = gogo->backend()->conditional_expression(bfn,
6056 btype, neg_expr,
a32698ee 6057 neg_one_expr,
6058 zero_expr, loc);
29a2d1d8 6059 }
93715b75 6060 ret = gogo->backend()->conditional_expression(bfn, btype, compare, ret,
a32698ee 6061 overflow, loc);
6062 mpz_clear(bitsval);
29a2d1d8 6063 }
6064
6065 // Add checks for division by zero and division overflow as needed.
6066 if (is_idiv_op)
6067 {
5c3f3470 6068 if (gogo->check_divide_by_zero())
29a2d1d8 6069 {
6070 // right == 0
a32698ee 6071 Bexpression* zero_expr =
6072 gogo->backend()->integer_constant_expression(right_btype, zero);
6073 Bexpression* check =
6074 gogo->backend()->binary_expression(OPERATOR_EQEQ,
6075 right, zero_expr, loc);
29a2d1d8 6076
a32698ee 6077 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO)
29a2d1d8 6078 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO;
ea664253 6079 Bexpression* crash = gogo->runtime_error(errcode,
6080 loc)->get_backend(context);
29a2d1d8 6081
6082 // right == 0 ? (__go_runtime_error(...), 0) : ret
93715b75 6083 Bfunction* bfn = context->function()->func_value()->get_decl();
6084 ret = gogo->backend()->conditional_expression(bfn, btype,
6085 check, crash,
ea664253 6086 ret, loc);
b13c66cd 6087 }
6088
5c3f3470 6089 if (gogo->check_divide_overflow())
29a2d1d8 6090 {
6091 // right == -1
6092 // FIXME: It would be nice to say that this test is expected
6093 // to return false.
a32698ee 6094
6095 Bexpression* neg_one_expr =
6096 gogo->backend()->integer_constant_expression(right_btype, neg_one);
6097 Bexpression* check =
6098 gogo->backend()->binary_expression(OPERATOR_EQEQ,
6099 right, neg_one_expr, loc);
6100
6101 Bexpression* zero_expr =
6102 gogo->backend()->integer_constant_expression(btype, zero);
6103 Bexpression* one_expr =
6104 gogo->backend()->integer_constant_expression(btype, one);
93715b75 6105 Bfunction* bfn = context->function()->func_value()->get_decl();
a32698ee 6106
6107 if (type->integer_type()->is_unsigned())
29a2d1d8 6108 {
6109 // An unsigned -1 is the largest possible number, so
6110 // dividing is always 1 or 0.
a32698ee 6111
6112 Bexpression* cmp =
6113 gogo->backend()->binary_expression(OPERATOR_EQEQ,
6114 left, right, loc);
29a2d1d8 6115 if (this->op_ == OPERATOR_DIV)
a32698ee 6116 overflow =
93715b75 6117 gogo->backend()->conditional_expression(bfn, btype, cmp,
a32698ee 6118 one_expr, zero_expr,
6119 loc);
29a2d1d8 6120 else
a32698ee 6121 overflow =
93715b75 6122 gogo->backend()->conditional_expression(bfn, btype, cmp,
a32698ee 6123 zero_expr, left,
6124 loc);
29a2d1d8 6125 }
6126 else
6127 {
6128 // Computing left / -1 is the same as computing - left,
6129 // which does not overflow since Go sets -fwrapv.
6130 if (this->op_ == OPERATOR_DIV)
a32698ee 6131 {
6132 Expression* negate_expr =
6133 Expression::make_unary(OPERATOR_MINUS, this->left_, loc);
ea664253 6134 overflow = negate_expr->get_backend(context);
a32698ee 6135 }
29a2d1d8 6136 else
a32698ee 6137 overflow = zero_expr;
29a2d1d8 6138 }
a32698ee 6139 overflow = gogo->backend()->convert_expression(btype, overflow, loc);
29a2d1d8 6140
6141 // right == -1 ? - left : ret
93715b75 6142 ret = gogo->backend()->conditional_expression(bfn, btype,
6143 check, overflow,
a32698ee 6144 ret, loc);
29a2d1d8 6145 }
e440a328 6146 }
6147
a32698ee 6148 mpz_clear(zero);
6149 mpz_clear(one);
6150 mpz_clear(neg_one);
ea664253 6151 return ret;
e440a328 6152}
6153
6154// Export a binary expression.
6155
6156void
6157Binary_expression::do_export(Export* exp) const
6158{
6159 exp->write_c_string("(");
6160 this->left_->export_expression(exp);
6161 switch (this->op_)
6162 {
6163 case OPERATOR_OROR:
6164 exp->write_c_string(" || ");
6165 break;
6166 case OPERATOR_ANDAND:
6167 exp->write_c_string(" && ");
6168 break;
6169 case OPERATOR_EQEQ:
6170 exp->write_c_string(" == ");
6171 break;
6172 case OPERATOR_NOTEQ:
6173 exp->write_c_string(" != ");
6174 break;
6175 case OPERATOR_LT:
6176 exp->write_c_string(" < ");
6177 break;
6178 case OPERATOR_LE:
6179 exp->write_c_string(" <= ");
6180 break;
6181 case OPERATOR_GT:
6182 exp->write_c_string(" > ");
6183 break;
6184 case OPERATOR_GE:
6185 exp->write_c_string(" >= ");
6186 break;
6187 case OPERATOR_PLUS:
6188 exp->write_c_string(" + ");
6189 break;
6190 case OPERATOR_MINUS:
6191 exp->write_c_string(" - ");
6192 break;
6193 case OPERATOR_OR:
6194 exp->write_c_string(" | ");
6195 break;
6196 case OPERATOR_XOR:
6197 exp->write_c_string(" ^ ");
6198 break;
6199 case OPERATOR_MULT:
6200 exp->write_c_string(" * ");
6201 break;
6202 case OPERATOR_DIV:
6203 exp->write_c_string(" / ");
6204 break;
6205 case OPERATOR_MOD:
6206 exp->write_c_string(" % ");
6207 break;
6208 case OPERATOR_LSHIFT:
6209 exp->write_c_string(" << ");
6210 break;
6211 case OPERATOR_RSHIFT:
6212 exp->write_c_string(" >> ");
6213 break;
6214 case OPERATOR_AND:
6215 exp->write_c_string(" & ");
6216 break;
6217 case OPERATOR_BITCLEAR:
6218 exp->write_c_string(" &^ ");
6219 break;
6220 default:
c3e6f413 6221 go_unreachable();
e440a328 6222 }
6223 this->right_->export_expression(exp);
6224 exp->write_c_string(")");
6225}
6226
6227// Import a binary expression.
6228
6229Expression*
6230Binary_expression::do_import(Import* imp)
6231{
6232 imp->require_c_string("(");
6233
6234 Expression* left = Expression::import_expression(imp);
6235
6236 Operator op;
6237 if (imp->match_c_string(" || "))
6238 {
6239 op = OPERATOR_OROR;
6240 imp->advance(4);
6241 }
6242 else if (imp->match_c_string(" && "))
6243 {
6244 op = OPERATOR_ANDAND;
6245 imp->advance(4);
6246 }
6247 else if (imp->match_c_string(" == "))
6248 {
6249 op = OPERATOR_EQEQ;
6250 imp->advance(4);
6251 }
6252 else if (imp->match_c_string(" != "))
6253 {
6254 op = OPERATOR_NOTEQ;
6255 imp->advance(4);
6256 }
6257 else if (imp->match_c_string(" < "))
6258 {
6259 op = OPERATOR_LT;
6260 imp->advance(3);
6261 }
6262 else if (imp->match_c_string(" <= "))
6263 {
6264 op = OPERATOR_LE;
6265 imp->advance(4);
6266 }
6267 else if (imp->match_c_string(" > "))
6268 {
6269 op = OPERATOR_GT;
6270 imp->advance(3);
6271 }
6272 else if (imp->match_c_string(" >= "))
6273 {
6274 op = OPERATOR_GE;
6275 imp->advance(4);
6276 }
6277 else if (imp->match_c_string(" + "))
6278 {
6279 op = OPERATOR_PLUS;
6280 imp->advance(3);
6281 }
6282 else if (imp->match_c_string(" - "))
6283 {
6284 op = OPERATOR_MINUS;
6285 imp->advance(3);
6286 }
6287 else if (imp->match_c_string(" | "))
6288 {
6289 op = OPERATOR_OR;
6290 imp->advance(3);
6291 }
6292 else if (imp->match_c_string(" ^ "))
6293 {
6294 op = OPERATOR_XOR;
6295 imp->advance(3);
6296 }
6297 else if (imp->match_c_string(" * "))
6298 {
6299 op = OPERATOR_MULT;
6300 imp->advance(3);
6301 }
6302 else if (imp->match_c_string(" / "))
6303 {
6304 op = OPERATOR_DIV;
6305 imp->advance(3);
6306 }
6307 else if (imp->match_c_string(" % "))
6308 {
6309 op = OPERATOR_MOD;
6310 imp->advance(3);
6311 }
6312 else if (imp->match_c_string(" << "))
6313 {
6314 op = OPERATOR_LSHIFT;
6315 imp->advance(4);
6316 }
6317 else if (imp->match_c_string(" >> "))
6318 {
6319 op = OPERATOR_RSHIFT;
6320 imp->advance(4);
6321 }
6322 else if (imp->match_c_string(" & "))
6323 {
6324 op = OPERATOR_AND;
6325 imp->advance(3);
6326 }
6327 else if (imp->match_c_string(" &^ "))
6328 {
6329 op = OPERATOR_BITCLEAR;
6330 imp->advance(4);
6331 }
6332 else
6333 {
631d5788 6334 go_error_at(imp->location(), "unrecognized binary operator");
e440a328 6335 return Expression::make_error(imp->location());
6336 }
6337
6338 Expression* right = Expression::import_expression(imp);
6339
6340 imp->require_c_string(")");
6341
6342 return Expression::make_binary(op, left, right, imp->location());
6343}
6344
d751bb78 6345// Dump ast representation of a binary expression.
6346
6347void
6348Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6349{
6350 ast_dump_context->ostream() << "(";
6351 ast_dump_context->dump_expression(this->left_);
6352 ast_dump_context->ostream() << " ";
6353 ast_dump_context->dump_operator(this->op_);
6354 ast_dump_context->ostream() << " ";
6355 ast_dump_context->dump_expression(this->right_);
6356 ast_dump_context->ostream() << ") ";
6357}
6358
e440a328 6359// Make a binary expression.
6360
6361Expression*
6362Expression::make_binary(Operator op, Expression* left, Expression* right,
b13c66cd 6363 Location location)
e440a328 6364{
6365 return new Binary_expression(op, left, right, location);
6366}
6367
6368// Implement a comparison.
6369
a32698ee 6370Bexpression*
6371Expression::comparison(Translate_context* context, Type* result_type,
6372 Operator op, Expression* left, Expression* right,
6373 Location location)
e440a328 6374{
2387f644 6375 Type* left_type = left->type();
6376 Type* right_type = right->type();
ceeb12d7 6377
e67508fa 6378 Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
1b1f2abf 6379
15c67ee2 6380 if (left_type->is_string_type() && right_type->is_string_type())
e440a328 6381 {
6098d6cb 6382 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
6383 {
6384 left = Runtime::make_call(Runtime::EQSTRING, location, 2,
6385 left, right);
6386 right = Expression::make_boolean(true, location);
6387 }
6388 else
6389 {
6390 left = Runtime::make_call(Runtime::CMPSTRING, location, 2,
6391 left, right);
6392 right = zexpr;
6393 }
e440a328 6394 }
15c67ee2 6395 else if ((left_type->interface_type() != NULL
6396 && right_type->interface_type() == NULL
6397 && !right_type->is_nil_type())
6398 || (left_type->interface_type() == NULL
6399 && !left_type->is_nil_type()
6400 && right_type->interface_type() != NULL))
e440a328 6401 {
6402 // Comparing an interface value to a non-interface value.
6403 if (left_type->interface_type() == NULL)
6404 {
6405 std::swap(left_type, right_type);
2387f644 6406 std::swap(left, right);
e440a328 6407 }
6408
6409 // The right operand is not an interface. We need to take its
6410 // address if it is not a pointer.
ceeb12d7 6411 Expression* pointer_arg = NULL;
e440a328 6412 if (right_type->points_to() != NULL)
2387f644 6413 pointer_arg = right;
e440a328 6414 else
6415 {
2387f644 6416 go_assert(right->is_addressable());
6417 pointer_arg = Expression::make_unary(OPERATOR_AND, right,
ceeb12d7 6418 location);
e440a328 6419 }
e440a328 6420
2387f644 6421 Expression* descriptor =
6422 Expression::make_type_descriptor(right_type, location);
6423 left =
ceeb12d7 6424 Runtime::make_call((left_type->interface_type()->is_empty()
6098d6cb 6425 ? Runtime::EFACEVALEQ
6426 : Runtime::IFACEVALEQ),
2387f644 6427 location, 3, left, descriptor,
ceeb12d7 6428 pointer_arg);
6098d6cb 6429 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6430 right = Expression::make_boolean(true, location);
e440a328 6431 }
6432 else if (left_type->interface_type() != NULL
6433 && right_type->interface_type() != NULL)
6434 {
ceeb12d7 6435 Runtime::Function compare_function;
739bad04 6436 if (left_type->interface_type()->is_empty()
6437 && right_type->interface_type()->is_empty())
6098d6cb 6438 compare_function = Runtime::EFACEEQ;
739bad04 6439 else if (!left_type->interface_type()->is_empty()
6440 && !right_type->interface_type()->is_empty())
6098d6cb 6441 compare_function = Runtime::IFACEEQ;
739bad04 6442 else
6443 {
6444 if (left_type->interface_type()->is_empty())
6445 {
739bad04 6446 std::swap(left_type, right_type);
2387f644 6447 std::swap(left, right);
739bad04 6448 }
c484d925 6449 go_assert(!left_type->interface_type()->is_empty());
6450 go_assert(right_type->interface_type()->is_empty());
6098d6cb 6451 compare_function = Runtime::IFACEEFACEEQ;
739bad04 6452 }
6453
2387f644 6454 left = Runtime::make_call(compare_function, location, 2, left, right);
6098d6cb 6455 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6456 right = Expression::make_boolean(true, location);
e440a328 6457 }
6458
6459 if (left_type->is_nil_type()
6460 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6461 {
6462 std::swap(left_type, right_type);
2387f644 6463 std::swap(left, right);
e440a328 6464 }
6465
6466 if (right_type->is_nil_type())
6467 {
2387f644 6468 right = Expression::make_nil(location);
e440a328 6469 if (left_type->array_type() != NULL
6470 && left_type->array_type()->length() == NULL)
6471 {
6472 Array_type* at = left_type->array_type();
2387f644 6473 left = at->get_value_pointer(context->gogo(), left);
e440a328 6474 }
6475 else if (left_type->interface_type() != NULL)
6476 {
6477 // An interface is nil if the first field is nil.
2387f644 6478 left = Expression::make_field_reference(left, 0, location);
e440a328 6479 }
6480 }
6481
ea664253 6482 Bexpression* left_bexpr = left->get_backend(context);
6483 Bexpression* right_bexpr = right->get_backend(context);
e90c9dfc 6484
a32698ee 6485 Gogo* gogo = context->gogo();
6486 Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr,
6487 right_bexpr, location);
6488 if (result_type != NULL)
6489 ret = gogo->backend()->convert_expression(result_type->get_backend(gogo),
6490 ret, location);
e440a328 6491 return ret;
6492}
6493
736a16ba 6494// Class String_concat_expression.
6495
6496bool
6497String_concat_expression::do_is_constant() const
6498{
6499 for (Expression_list::const_iterator pe = this->exprs_->begin();
6500 pe != this->exprs_->end();
6501 ++pe)
6502 {
6503 if (!(*pe)->is_constant())
6504 return false;
6505 }
6506 return true;
6507}
6508
6509bool
3ae06f68 6510String_concat_expression::do_is_static_initializer() const
736a16ba 6511{
6512 for (Expression_list::const_iterator pe = this->exprs_->begin();
6513 pe != this->exprs_->end();
6514 ++pe)
6515 {
3ae06f68 6516 if (!(*pe)->is_static_initializer())
736a16ba 6517 return false;
6518 }
6519 return true;
6520}
6521
6522Type*
6523String_concat_expression::do_type()
6524{
6525 Type* t = this->exprs_->front()->type();
6526 Expression_list::iterator pe = this->exprs_->begin();
6527 ++pe;
6528 for (; pe != this->exprs_->end(); ++pe)
6529 {
6530 Type* t1;
6531 if (!Binary_expression::operation_type(OPERATOR_PLUS, t,
6532 (*pe)->type(),
6533 &t1))
6534 return Type::make_error_type();
6535 t = t1;
6536 }
6537 return t;
6538}
6539
6540void
6541String_concat_expression::do_determine_type(const Type_context* context)
6542{
6543 Type_context subcontext(*context);
6544 for (Expression_list::iterator pe = this->exprs_->begin();
6545 pe != this->exprs_->end();
6546 ++pe)
6547 {
6548 Type* t = (*pe)->type();
6549 if (!t->is_abstract())
6550 {
6551 subcontext.type = t;
6552 break;
6553 }
6554 }
6555 if (subcontext.type == NULL)
6556 subcontext.type = this->exprs_->front()->type();
6557 for (Expression_list::iterator pe = this->exprs_->begin();
6558 pe != this->exprs_->end();
6559 ++pe)
6560 (*pe)->determine_type(&subcontext);
6561}
6562
6563void
6564String_concat_expression::do_check_types(Gogo*)
6565{
6566 if (this->is_error_expression())
6567 return;
6568 Type* t = this->exprs_->front()->type();
6569 if (t->is_error())
6570 {
6571 this->set_is_error();
6572 return;
6573 }
6574 Expression_list::iterator pe = this->exprs_->begin();
6575 ++pe;
6576 for (; pe != this->exprs_->end(); ++pe)
6577 {
6578 Type* t1 = (*pe)->type();
6579 if (!Type::are_compatible_for_binop(t, t1))
6580 {
6581 this->report_error("incompatible types in binary expression");
6582 return;
6583 }
6584 if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1,
6585 this->location()))
6586 {
6587 this->set_is_error();
6588 return;
6589 }
6590 }
6591}
6592
6593Expression*
6594String_concat_expression::do_flatten(Gogo*, Named_object*,
6595 Statement_inserter*)
6596{
6597 if (this->is_error_expression())
6598 return this;
6599 Location loc = this->location();
6600 Type* type = this->type();
6601 Expression* nil_arg = Expression::make_nil(loc);
6602 Expression* call;
6603 switch (this->exprs_->size())
6604 {
6605 case 0: case 1:
6606 go_unreachable();
6607
6608 case 2: case 3: case 4: case 5:
6609 {
6610 Expression* len = Expression::make_integer_ul(this->exprs_->size(),
6611 NULL, loc);
6612 Array_type* arg_type = Type::make_array_type(type, len);
6613 arg_type->set_is_array_incomparable();
6614 Expression* arg =
6615 Expression::make_array_composite_literal(arg_type, this->exprs_,
6616 loc);
6617 Runtime::Function code;
6618 switch (this->exprs_->size())
6619 {
6620 default:
6621 go_unreachable();
6622 case 2:
6623 code = Runtime::CONCATSTRING2;
6624 break;
6625 case 3:
6626 code = Runtime::CONCATSTRING3;
6627 break;
6628 case 4:
6629 code = Runtime::CONCATSTRING4;
6630 break;
6631 case 5:
6632 code = Runtime::CONCATSTRING5;
6633 break;
6634 }
6635 call = Runtime::make_call(code, loc, 2, nil_arg, arg);
6636 }
6637 break;
6638
6639 default:
6640 {
6641 Type* arg_type = Type::make_array_type(type, NULL);
6642 Slice_construction_expression* sce =
6643 Expression::make_slice_composite_literal(arg_type, this->exprs_,
6644 loc);
6645 sce->set_storage_does_not_escape();
6646 call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg,
6647 sce);
6648 }
6649 break;
6650 }
6651
6652 return Expression::make_cast(type, call, loc);
6653}
6654
6655void
6656String_concat_expression::do_dump_expression(
6657 Ast_dump_context* ast_dump_context) const
6658{
6659 ast_dump_context->ostream() << "concat(";
6660 ast_dump_context->dump_expression_list(this->exprs_, false);
6661 ast_dump_context->ostream() << ")";
6662}
6663
6664Expression*
6665Expression::make_string_concat(Expression_list* exprs)
6666{
6667 return new String_concat_expression(exprs);
6668}
6669
e440a328 6670// Class Bound_method_expression.
6671
6672// Traversal.
6673
6674int
6675Bound_method_expression::do_traverse(Traverse* traverse)
6676{
e0659c9e 6677 return Expression::traverse(&this->expr_, traverse);
e440a328 6678}
6679
6680// Return the type of a bound method expression. The type of this
0afbb937 6681// object is simply the type of the method with no receiver.
e440a328 6682
6683Type*
6684Bound_method_expression::do_type()
6685{
0afbb937 6686 Named_object* fn = this->method_->named_object();
6687 Function_type* fntype;
6688 if (fn->is_function())
6689 fntype = fn->func_value()->type();
6690 else if (fn->is_function_declaration())
6691 fntype = fn->func_declaration_value()->type();
e0659c9e 6692 else
6693 return Type::make_error_type();
0afbb937 6694 return fntype->copy_without_receiver();
e440a328 6695}
6696
6697// Determine the types of a method expression.
6698
6699void
6700Bound_method_expression::do_determine_type(const Type_context*)
6701{
0afbb937 6702 Named_object* fn = this->method_->named_object();
6703 Function_type* fntype;
6704 if (fn->is_function())
6705 fntype = fn->func_value()->type();
6706 else if (fn->is_function_declaration())
6707 fntype = fn->func_declaration_value()->type();
6708 else
6709 fntype = NULL;
e440a328 6710 if (fntype == NULL || !fntype->is_method())
6711 this->expr_->determine_type_no_context();
6712 else
6713 {
6714 Type_context subcontext(fntype->receiver()->type(), false);
6715 this->expr_->determine_type(&subcontext);
6716 }
6717}
6718
6719// Check the types of a method expression.
6720
6721void
6722Bound_method_expression::do_check_types(Gogo*)
6723{
0afbb937 6724 Named_object* fn = this->method_->named_object();
6725 if (!fn->is_function() && !fn->is_function_declaration())
6726 {
6727 this->report_error(_("object is not a method"));
6728 return;
6729 }
6730
6731 Function_type* fntype;
6732 if (fn->is_function())
6733 fntype = fn->func_value()->type();
6734 else if (fn->is_function_declaration())
6735 fntype = fn->func_declaration_value()->type();
e440a328 6736 else
0afbb937 6737 go_unreachable();
6738 Type* rtype = fntype->receiver()->type()->deref();
6739 Type* etype = (this->expr_type_ != NULL
6740 ? this->expr_type_
6741 : this->expr_->type());
6742 etype = etype->deref();
6743 if (!Type::are_identical(rtype, etype, true, NULL))
6744 this->report_error(_("method type does not match object type"));
6745}
6746
6747// If a bound method expression is not simply called, then it is
6748// represented as a closure. The closure will hold a single variable,
6749// the receiver to pass to the method. The function will be a simple
6750// thunk that pulls that value from the closure and calls the method
6751// with the remaining arguments.
6752//
6753// Because method values are not common, we don't build all thunks for
6754// every methods, but instead only build them as we need them. In
6755// particular, we even build them on demand for methods defined in
6756// other packages.
6757
6758Bound_method_expression::Method_value_thunks
6759 Bound_method_expression::method_value_thunks;
6760
6761// Find or create the thunk for METHOD.
6762
6763Named_object*
6764Bound_method_expression::create_thunk(Gogo* gogo, const Method* method,
6765 Named_object* fn)
6766{
6767 std::pair<Named_object*, Named_object*> val(fn, NULL);
6768 std::pair<Method_value_thunks::iterator, bool> ins =
6769 Bound_method_expression::method_value_thunks.insert(val);
6770 if (!ins.second)
6771 {
6772 // We have seen this method before.
6773 go_assert(ins.first->second != NULL);
6774 return ins.first->second;
6775 }
6776
6777 Location loc = fn->location();
6778
6779 Function_type* orig_fntype;
6780 if (fn->is_function())
6781 orig_fntype = fn->func_value()->type();
6782 else if (fn->is_function_declaration())
6783 orig_fntype = fn->func_declaration_value()->type();
6784 else
6785 orig_fntype = NULL;
6786
6787 if (orig_fntype == NULL || !orig_fntype->is_method())
e440a328 6788 {
0afbb937 6789 ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name());
6790 return ins.first->second;
e440a328 6791 }
0afbb937 6792
6793 Struct_field_list* sfl = new Struct_field_list();
f8bdf81a 6794 // The type here is wrong--it should be the C function type. But it
6795 // doesn't really matter.
0afbb937 6796 Type* vt = Type::make_pointer_type(Type::make_void_type());
6797 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
6798 sfl->push_back(Struct_field(Typed_identifier("val.1",
6799 orig_fntype->receiver()->type(),
6800 loc)));
6bf4793c 6801 Struct_type* st = Type::make_struct_type(sfl, loc);
6802 st->set_is_struct_incomparable();
6803 Type* closure_type = Type::make_pointer_type(st);
0afbb937 6804
f8bdf81a 6805 Function_type* new_fntype = orig_fntype->copy_with_names();
0afbb937 6806
da244e59 6807 std::string thunk_name = Gogo::thunk_name();
6808 Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
0afbb937 6809 false, loc);
6810
f8bdf81a 6811 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
6812 cvar->set_is_used();
1ecc6157 6813 cvar->set_is_closure();
da244e59 6814 Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
6815 NULL, cvar);
f8bdf81a 6816 new_no->func_value()->set_closure_var(cp);
0afbb937 6817
f8bdf81a 6818 gogo->start_block(loc);
0afbb937 6819
6820 // Field 0 of the closure is the function code pointer, field 1 is
6821 // the value on which to invoke the method.
6822 Expression* arg = Expression::make_var_reference(cp, loc);
6823 arg = Expression::make_unary(OPERATOR_MULT, arg, loc);
6824 arg = Expression::make_field_reference(arg, 1, loc);
6825
6826 Expression* bme = Expression::make_bound_method(arg, method, fn, loc);
6827
6828 const Typed_identifier_list* orig_params = orig_fntype->parameters();
6829 Expression_list* args;
6830 if (orig_params == NULL || orig_params->empty())
6831 args = NULL;
6832 else
6833 {
6834 const Typed_identifier_list* new_params = new_fntype->parameters();
6835 args = new Expression_list();
6836 for (Typed_identifier_list::const_iterator p = new_params->begin();
f8bdf81a 6837 p != new_params->end();
0afbb937 6838 ++p)
6839 {
6840 Named_object* p_no = gogo->lookup(p->name(), NULL);
6841 go_assert(p_no != NULL
6842 && p_no->is_variable()
6843 && p_no->var_value()->is_parameter());
6844 args->push_back(Expression::make_var_reference(p_no, loc));
6845 }
6846 }
6847
6848 Call_expression* call = Expression::make_call(bme, args,
6849 orig_fntype->is_varargs(),
6850 loc);
6851 call->set_varargs_are_lowered();
6852
6853 Statement* s = Statement::make_return_from_call(call, loc);
6854 gogo->add_statement(s);
6855 Block* b = gogo->finish_block(loc);
6856 gogo->add_block(b, loc);
6857 gogo->lower_block(new_no, b);
a32698ee 6858 gogo->flatten_block(new_no, b);
0afbb937 6859 gogo->finish_function(loc);
6860
6861 ins.first->second = new_no;
6862 return new_no;
6863}
6864
6865// Return an expression to check *REF for nil while dereferencing
6866// according to FIELD_INDEXES. Update *REF to build up the field
6867// reference. This is a static function so that we don't have to
6868// worry about declaring Field_indexes in expressions.h.
6869
6870static Expression*
6871bme_check_nil(const Method::Field_indexes* field_indexes, Location loc,
6872 Expression** ref)
6873{
6874 if (field_indexes == NULL)
6875 return Expression::make_boolean(false, loc);
6876 Expression* cond = bme_check_nil(field_indexes->next, loc, ref);
6877 Struct_type* stype = (*ref)->type()->deref()->struct_type();
6878 go_assert(stype != NULL
6879 && field_indexes->field_index < stype->field_count());
6880 if ((*ref)->type()->struct_type() == NULL)
6881 {
6882 go_assert((*ref)->type()->points_to() != NULL);
6883 Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref,
6884 Expression::make_nil(loc),
6885 loc);
6886 cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc);
6887 *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc);
6888 go_assert((*ref)->type()->struct_type() == stype);
6889 }
6890 *ref = Expression::make_field_reference(*ref, field_indexes->field_index,
6891 loc);
6892 return cond;
e440a328 6893}
6894
cd39797e 6895// Flatten a method value into a struct with nil checks. We can't do
6896// this in the lowering phase, because if the method value is called
6897// directly we don't need a thunk. That case will have been handled
6898// by Call_expression::do_lower, so if we get here then we do need a
6899// thunk.
e440a328 6900
cd39797e 6901Expression*
6902Bound_method_expression::do_flatten(Gogo* gogo, Named_object*,
6903 Statement_inserter* inserter)
e440a328 6904{
cd39797e 6905 Location loc = this->location();
6906
6907 Named_object* thunk = Bound_method_expression::create_thunk(gogo,
0afbb937 6908 this->method_,
6909 this->function_);
6910 if (thunk->is_erroneous())
6911 {
6912 go_assert(saw_errors());
cd39797e 6913 return Expression::make_error(loc);
0afbb937 6914 }
6915
cd39797e 6916 // Force the expression into a variable. This is only necessary if
6917 // we are going to do nil checks below, but it's easy enough to
6918 // always do it.
6919 Expression* expr = this->expr_;
6920 if (!expr->is_variable())
6921 {
6922 Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc);
6923 inserter->insert(etemp);
6924 expr = Expression::make_temporary_reference(etemp, loc);
6925 }
0afbb937 6926
6927 // If the method expects a value, and we have a pointer, we need to
6928 // dereference the pointer.
6929
6930 Named_object* fn = this->method_->named_object();
cd39797e 6931 Function_type *fntype;
0afbb937 6932 if (fn->is_function())
6933 fntype = fn->func_value()->type();
6934 else if (fn->is_function_declaration())
6935 fntype = fn->func_declaration_value()->type();
6936 else
6937 go_unreachable();
6938
cd39797e 6939 Expression* val = expr;
0afbb937 6940 if (fntype->receiver()->type()->points_to() == NULL
6941 && val->type()->points_to() != NULL)
6942 val = Expression::make_unary(OPERATOR_MULT, val, loc);
6943
6944 // Note that we are ignoring this->expr_type_ here. The thunk will
6945 // expect a closure whose second field has type this->expr_type_ (if
6946 // that is not NULL). We are going to pass it a closure whose
6947 // second field has type this->expr_->type(). Since
6948 // this->expr_type_ is only not-NULL for pointer types, we can get
6949 // away with this.
6950
6951 Struct_field_list* fields = new Struct_field_list();
6952 fields->push_back(Struct_field(Typed_identifier("fn.0",
6953 thunk->func_value()->type(),
6954 loc)));
6955 fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc)));
6956 Struct_type* st = Type::make_struct_type(fields, loc);
6bf4793c 6957 st->set_is_struct_incomparable();
0afbb937 6958
6959 Expression_list* vals = new Expression_list();
6960 vals->push_back(Expression::make_func_code_reference(thunk, loc));
6961 vals->push_back(val);
6962
6963 Expression* ret = Expression::make_struct_composite_literal(st, vals, loc);
0afbb937 6964
cd39797e 6965 if (!gogo->compiling_runtime() || gogo->package_name() != "runtime")
6966 ret = Expression::make_heap_expression(ret, loc);
6967 else
6968 {
6969 // When compiling the runtime, method closures do not escape.
6970 // When escape analysis becomes the default, and applies to
6971 // method closures, this should be changed to make it an error
6972 // if a method closure escapes.
6973 Temporary_statement* ctemp = Statement::make_temporary(st, ret, loc);
6974 inserter->insert(ctemp);
6975 ret = Expression::make_temporary_reference(ctemp, loc);
6976 ret = Expression::make_unary(OPERATOR_AND, ret, loc);
6977 ret->unary_expression()->set_does_not_escape();
6978 }
6979
6980 // If necessary, check whether the expression or any embedded
6981 // pointers are nil.
0afbb937 6982
df7ef1fd 6983 Expression* nil_check = NULL;
0afbb937 6984 if (this->method_->field_indexes() != NULL)
6985 {
0afbb937 6986 Expression* ref = expr;
6987 nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref);
6988 expr = ref;
6989 }
6990
6991 if (this->method_->is_value_method() && expr->type()->points_to() != NULL)
6992 {
6993 Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr,
6994 Expression::make_nil(loc),
6995 loc);
6996 if (nil_check == NULL)
6997 nil_check = n;
6998 else
6999 nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc);
7000 }
7001
7002 if (nil_check != NULL)
7003 {
cd39797e 7004 Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
7005 loc);
7006 // Fix the type of the conditional expression by pretending to
7007 // evaluate to RET either way through the conditional.
7008 crash = Expression::make_compound(crash, ret, loc);
7009 ret = Expression::make_conditional(nil_check, crash, ret, loc);
7010 }
7011
7012 // RET is a pointer to a struct, but we want a function type.
7013 ret = Expression::make_unsafe_cast(this->type(), ret, loc);
7014
7015 return ret;
e440a328 7016}
7017
d751bb78 7018// Dump ast representation of a bound method expression.
7019
7020void
7021Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
7022 const
7023{
7024 if (this->expr_type_ != NULL)
7025 ast_dump_context->ostream() << "(";
7026 ast_dump_context->dump_expression(this->expr_);
7027 if (this->expr_type_ != NULL)
7028 {
7029 ast_dump_context->ostream() << ":";
7030 ast_dump_context->dump_type(this->expr_type_);
7031 ast_dump_context->ostream() << ")";
7032 }
7033
0afbb937 7034 ast_dump_context->ostream() << "." << this->function_->name();
d751bb78 7035}
7036
e440a328 7037// Make a method expression.
7038
7039Bound_method_expression*
0afbb937 7040Expression::make_bound_method(Expression* expr, const Method* method,
7041 Named_object* function, Location location)
e440a328 7042{
0afbb937 7043 return new Bound_method_expression(expr, method, function, location);
e440a328 7044}
7045
7046// Class Builtin_call_expression. This is used for a call to a
7047// builtin function.
7048
7049class Builtin_call_expression : public Call_expression
7050{
7051 public:
7052 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
b13c66cd 7053 bool is_varargs, Location location);
e440a328 7054
7055 protected:
7056 // This overrides Call_expression::do_lower.
7057 Expression*
ceeb4318 7058 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 7059
35a54f17 7060 Expression*
7061 do_flatten(Gogo*, Named_object*, Statement_inserter*);
7062
e440a328 7063 bool
7064 do_is_constant() const;
7065
7066 bool
0c77715b 7067 do_numeric_constant_value(Numeric_constant*) const;
e440a328 7068
4f2138d7 7069 bool
a7549a6a 7070 do_discarding_value();
7071
e440a328 7072 Type*
7073 do_type();
7074
7075 void
7076 do_determine_type(const Type_context*);
7077
7078 void
7079 do_check_types(Gogo*);
7080
7081 Expression*
72666aed 7082 do_copy();
e440a328 7083
ea664253 7084 Bexpression*
7085 do_get_backend(Translate_context*);
e440a328 7086
7087 void
7088 do_export(Export*) const;
7089
7090 virtual bool
7091 do_is_recover_call() const;
7092
7093 virtual void
7094 do_set_recover_arg(Expression*);
7095
7096 private:
7097 // The builtin functions.
7098 enum Builtin_function_code
7099 {
7100 BUILTIN_INVALID,
7101
7102 // Predeclared builtin functions.
7103 BUILTIN_APPEND,
7104 BUILTIN_CAP,
7105 BUILTIN_CLOSE,
48080209 7106 BUILTIN_COMPLEX,
e440a328 7107 BUILTIN_COPY,
1cce762f 7108 BUILTIN_DELETE,
e440a328 7109 BUILTIN_IMAG,
7110 BUILTIN_LEN,
7111 BUILTIN_MAKE,
7112 BUILTIN_NEW,
7113 BUILTIN_PANIC,
7114 BUILTIN_PRINT,
7115 BUILTIN_PRINTLN,
7116 BUILTIN_REAL,
7117 BUILTIN_RECOVER,
7118
7119 // Builtin functions from the unsafe package.
7120 BUILTIN_ALIGNOF,
7121 BUILTIN_OFFSETOF,
7122 BUILTIN_SIZEOF
7123 };
7124
7125 Expression*
7126 one_arg() const;
7127
7128 bool
7129 check_one_arg();
7130
7131 static Type*
7132 real_imag_type(Type*);
7133
7134 static Type*
48080209 7135 complex_type(Type*);
e440a328 7136
a9182619 7137 Expression*
321e5ad2 7138 lower_make(Statement_inserter*);
7139
7140 Expression* flatten_append(Gogo*, Named_object*, Statement_inserter*);
a9182619 7141
7142 bool
ccea2b36 7143 check_int_value(Expression*, bool is_length, bool* small);
a9182619 7144
e440a328 7145 // A pointer back to the general IR structure. This avoids a global
7146 // variable, or passing it around everywhere.
7147 Gogo* gogo_;
7148 // The builtin function being called.
7149 Builtin_function_code code_;
0f914071 7150 // Used to stop endless loops when the length of an array uses len
7151 // or cap of the array itself.
7152 mutable bool seen_;
6334270b 7153 // Whether the argument is set for calls to BUILTIN_RECOVER.
7154 bool recover_arg_is_set_;
e440a328 7155};
7156
7157Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7158 Expression* fn,
7159 Expression_list* args,
7160 bool is_varargs,
b13c66cd 7161 Location location)
e440a328 7162 : Call_expression(fn, args, is_varargs, location),
6334270b 7163 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false),
7164 recover_arg_is_set_(false)
e440a328 7165{
7166 Func_expression* fnexp = this->fn()->func_expression();
79651b1f 7167 if (fnexp == NULL)
7168 {
7169 this->code_ = BUILTIN_INVALID;
7170 return;
7171 }
e440a328 7172 const std::string& name(fnexp->named_object()->name());
7173 if (name == "append")
7174 this->code_ = BUILTIN_APPEND;
7175 else if (name == "cap")
7176 this->code_ = BUILTIN_CAP;
7177 else if (name == "close")
7178 this->code_ = BUILTIN_CLOSE;
48080209 7179 else if (name == "complex")
7180 this->code_ = BUILTIN_COMPLEX;
e440a328 7181 else if (name == "copy")
7182 this->code_ = BUILTIN_COPY;
1cce762f 7183 else if (name == "delete")
7184 this->code_ = BUILTIN_DELETE;
e440a328 7185 else if (name == "imag")
7186 this->code_ = BUILTIN_IMAG;
7187 else if (name == "len")
7188 this->code_ = BUILTIN_LEN;
7189 else if (name == "make")
7190 this->code_ = BUILTIN_MAKE;
7191 else if (name == "new")
7192 this->code_ = BUILTIN_NEW;
7193 else if (name == "panic")
7194 this->code_ = BUILTIN_PANIC;
7195 else if (name == "print")
7196 this->code_ = BUILTIN_PRINT;
7197 else if (name == "println")
7198 this->code_ = BUILTIN_PRINTLN;
7199 else if (name == "real")
7200 this->code_ = BUILTIN_REAL;
7201 else if (name == "recover")
7202 this->code_ = BUILTIN_RECOVER;
7203 else if (name == "Alignof")
7204 this->code_ = BUILTIN_ALIGNOF;
7205 else if (name == "Offsetof")
7206 this->code_ = BUILTIN_OFFSETOF;
7207 else if (name == "Sizeof")
7208 this->code_ = BUILTIN_SIZEOF;
7209 else
c3e6f413 7210 go_unreachable();
e440a328 7211}
7212
7213// Return whether this is a call to recover. This is a virtual
7214// function called from the parent class.
7215
7216bool
7217Builtin_call_expression::do_is_recover_call() const
7218{
7219 if (this->classification() == EXPRESSION_ERROR)
7220 return false;
7221 return this->code_ == BUILTIN_RECOVER;
7222}
7223
7224// Set the argument for a call to recover.
7225
7226void
7227Builtin_call_expression::do_set_recover_arg(Expression* arg)
7228{
7229 const Expression_list* args = this->args();
c484d925 7230 go_assert(args == NULL || args->empty());
e440a328 7231 Expression_list* new_args = new Expression_list();
7232 new_args->push_back(arg);
7233 this->set_args(new_args);
6334270b 7234 this->recover_arg_is_set_ = true;
e440a328 7235}
7236
e440a328 7237// Lower a builtin call expression. This turns new and make into
7238// specific expressions. We also convert to a constant if we can.
7239
7240Expression*
321e5ad2 7241Builtin_call_expression::do_lower(Gogo*, Named_object* function,
ceeb4318 7242 Statement_inserter* inserter, int)
e440a328 7243{
79651b1f 7244 if (this->is_error_expression())
a9182619 7245 return this;
7246
b13c66cd 7247 Location loc = this->location();
1cce762f 7248
a8725655 7249 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7250 {
7251 this->report_error(_("invalid use of %<...%> with builtin function"));
1cce762f 7252 return Expression::make_error(loc);
a8725655 7253 }
7254
393ba00b 7255 if (this->code_ == BUILTIN_OFFSETOF)
7256 {
7257 Expression* arg = this->one_arg();
12e69faa 7258
7259 if (arg->bound_method_expression() != NULL
7260 || arg->interface_field_reference_expression() != NULL)
7261 {
7262 this->report_error(_("invalid use of method value as argument "
7263 "of Offsetof"));
7264 return this;
7265 }
7266
393ba00b 7267 Field_reference_expression* farg = arg->field_reference_expression();
7268 while (farg != NULL)
7269 {
7270 if (!farg->implicit())
7271 break;
7272 // When the selector refers to an embedded field,
7273 // it must not be reached through pointer indirections.
7274 if (farg->expr()->deref() != farg->expr())
7275 {
12e69faa 7276 this->report_error(_("argument of Offsetof implies "
7277 "indirection of an embedded field"));
393ba00b 7278 return this;
7279 }
7280 // Go up until we reach the original base.
7281 farg = farg->expr()->field_reference_expression();
7282 }
7283 }
7284
1cce762f 7285 if (this->is_constant())
e440a328 7286 {
0c77715b 7287 Numeric_constant nc;
7288 if (this->numeric_constant_value(&nc))
7289 return nc.expression(loc);
e440a328 7290 }
1cce762f 7291
7292 switch (this->code_)
e440a328 7293 {
1cce762f 7294 default:
7295 break;
7296
7297 case BUILTIN_NEW:
7298 {
7299 const Expression_list* args = this->args();
7300 if (args == NULL || args->size() < 1)
7301 this->report_error(_("not enough arguments"));
7302 else if (args->size() > 1)
7303 this->report_error(_("too many arguments"));
7304 else
7305 {
7306 Expression* arg = args->front();
7307 if (!arg->is_type_expression())
7308 {
631d5788 7309 go_error_at(arg->location(), "expected type");
1cce762f 7310 this->set_is_error();
7311 }
7312 else
7313 return Expression::make_allocation(arg->type(), loc);
7314 }
7315 }
7316 break;
7317
7318 case BUILTIN_MAKE:
321e5ad2 7319 return this->lower_make(inserter);
1cce762f 7320
7321 case BUILTIN_RECOVER:
e440a328 7322 if (function != NULL)
7323 function->func_value()->set_calls_recover();
7324 else
7325 {
7326 // Calling recover outside of a function always returns the
7327 // nil empty interface.
823c7e3d 7328 Type* eface = Type::make_empty_interface_type(loc);
1cce762f 7329 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
e440a328 7330 }
1cce762f 7331 break;
7332
1cce762f 7333 case BUILTIN_DELETE:
7334 {
7335 // Lower to a runtime function call.
7336 const Expression_list* args = this->args();
7337 if (args == NULL || args->size() < 2)
7338 this->report_error(_("not enough arguments"));
7339 else if (args->size() > 2)
7340 this->report_error(_("too many arguments"));
7341 else if (args->front()->type()->map_type() == NULL)
7342 this->report_error(_("argument 1 must be a map"));
7343 else
7344 {
7345 // Since this function returns no value it must appear in
7346 // a statement by itself, so we don't have to worry about
7347 // order of evaluation of values around it. Evaluate the
7348 // map first to get order of evaluation right.
7349 Map_type* mt = args->front()->type()->map_type();
7350 Temporary_statement* map_temp =
7351 Statement::make_temporary(mt, args->front(), loc);
7352 inserter->insert(map_temp);
7353
7354 Temporary_statement* key_temp =
7355 Statement::make_temporary(mt->key_type(), args->back(), loc);
7356 inserter->insert(key_temp);
7357
0d5530d9 7358 Expression* e1 = Expression::make_type_descriptor(mt, loc);
7359 Expression* e2 = Expression::make_temporary_reference(map_temp,
1cce762f 7360 loc);
0d5530d9 7361 Expression* e3 = Expression::make_temporary_reference(key_temp,
1cce762f 7362 loc);
0d5530d9 7363 e3 = Expression::make_unary(OPERATOR_AND, e3, loc);
1cce762f 7364 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
0d5530d9 7365 3, e1, e2, e3);
1cce762f 7366 }
7367 }
7368 break;
88b03a70 7369
7370 case BUILTIN_PRINT:
7371 case BUILTIN_PRINTLN:
7372 // Force all the arguments into temporary variables, so that we
7373 // don't try to evaluate something while holding the print lock.
7374 if (this->args() == NULL)
7375 break;
7376 for (Expression_list::iterator pa = this->args()->begin();
7377 pa != this->args()->end();
7378 ++pa)
7379 {
493ce3ee 7380 if (!(*pa)->is_variable() && !(*pa)->is_constant())
88b03a70 7381 {
7382 Temporary_statement* temp =
7383 Statement::make_temporary(NULL, *pa, loc);
7384 inserter->insert(temp);
7385 *pa = Expression::make_temporary_reference(temp, loc);
7386 }
7387 }
7388 break;
e440a328 7389 }
7390
7391 return this;
7392}
7393
35a54f17 7394// Flatten a builtin call expression. This turns the arguments of copy and
7395// append into temporary expressions.
7396
7397Expression*
321e5ad2 7398Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function,
35a54f17 7399 Statement_inserter* inserter)
7400{
16cb7fec 7401 Location loc = this->location();
7402
7403 switch (this->code_)
35a54f17 7404 {
16cb7fec 7405 default:
7406 break;
7407
7408 case BUILTIN_APPEND:
321e5ad2 7409 return this->flatten_append(gogo, function, inserter);
7410
16cb7fec 7411 case BUILTIN_COPY:
7412 {
7413 Type* at = this->args()->front()->type();
7414 for (Expression_list::iterator pa = this->args()->begin();
7415 pa != this->args()->end();
7416 ++pa)
7417 {
7418 if ((*pa)->is_nil_expression())
7419 {
7420 Expression* nil = Expression::make_nil(loc);
7421 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
7422 *pa = Expression::make_slice_value(at, nil, zero, zero, loc);
7423 }
7424 if (!(*pa)->is_variable())
7425 {
7426 Temporary_statement* temp =
7427 Statement::make_temporary(NULL, *pa, loc);
7428 inserter->insert(temp);
7429 *pa = Expression::make_temporary_reference(temp, loc);
7430 }
7431 }
7432 }
7433 break;
7434
7435 case BUILTIN_PANIC:
35a54f17 7436 for (Expression_list::iterator pa = this->args()->begin();
16cb7fec 7437 pa != this->args()->end();
7438 ++pa)
7439 {
7440 if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL)
55e8ba6a 7441 {
16cb7fec 7442 Temporary_statement* temp =
7443 Statement::make_temporary(NULL, *pa, loc);
7444 inserter->insert(temp);
7445 *pa = Expression::make_temporary_reference(temp, loc);
55e8ba6a 7446 }
16cb7fec 7447 }
7739537f 7448 break;
0d5530d9 7449
7450 case BUILTIN_LEN:
132ed071 7451 case BUILTIN_CAP:
321e5ad2 7452 {
7453 Expression_list::iterator pa = this->args()->begin();
7454 if (!(*pa)->is_variable()
7455 && ((*pa)->type()->map_type() != NULL
7456 || (*pa)->type()->channel_type() != NULL))
7457 {
7458 Temporary_statement* temp =
7459 Statement::make_temporary(NULL, *pa, loc);
7460 inserter->insert(temp);
7461 *pa = Expression::make_temporary_reference(temp, loc);
7462 }
7463 }
7464 break;
35a54f17 7465 }
16cb7fec 7466
35a54f17 7467 return this;
7468}
7469
a9182619 7470// Lower a make expression.
7471
7472Expression*
321e5ad2 7473Builtin_call_expression::lower_make(Statement_inserter* inserter)
a9182619 7474{
b13c66cd 7475 Location loc = this->location();
a9182619 7476
7477 const Expression_list* args = this->args();
7478 if (args == NULL || args->size() < 1)
7479 {
7480 this->report_error(_("not enough arguments"));
7481 return Expression::make_error(this->location());
7482 }
7483
7484 Expression_list::const_iterator parg = args->begin();
7485
7486 Expression* first_arg = *parg;
7487 if (!first_arg->is_type_expression())
7488 {
631d5788 7489 go_error_at(first_arg->location(), "expected type");
a9182619 7490 this->set_is_error();
7491 return Expression::make_error(this->location());
7492 }
7493 Type* type = first_arg->type();
7494
7495 bool is_slice = false;
7496 bool is_map = false;
7497 bool is_chan = false;
411eb89e 7498 if (type->is_slice_type())
a9182619 7499 is_slice = true;
7500 else if (type->map_type() != NULL)
7501 is_map = true;
7502 else if (type->channel_type() != NULL)
7503 is_chan = true;
7504 else
7505 {
7506 this->report_error(_("invalid type for make function"));
7507 return Expression::make_error(this->location());
7508 }
7509
f6bc81e6 7510 Type_context int_context(Type::lookup_integer_type("int"), false);
7511
a9182619 7512 ++parg;
7513 Expression* len_arg;
ccea2b36 7514 bool len_small = false;
a9182619 7515 if (parg == args->end())
7516 {
7517 if (is_slice)
7518 {
7519 this->report_error(_("length required when allocating a slice"));
7520 return Expression::make_error(this->location());
7521 }
e67508fa 7522 len_arg = Expression::make_integer_ul(0, NULL, loc);
a9182619 7523 }
7524 else
7525 {
7526 len_arg = *parg;
f6bc81e6 7527 len_arg->determine_type(&int_context);
ccea2b36 7528 if (!this->check_int_value(len_arg, true, &len_small))
1ad00fd4 7529 return Expression::make_error(this->location());
a9182619 7530 ++parg;
7531 }
7532
7533 Expression* cap_arg = NULL;
ccea2b36 7534 bool cap_small = false;
a9182619 7535 if (is_slice && parg != args->end())
7536 {
7537 cap_arg = *parg;
f6bc81e6 7538 cap_arg->determine_type(&int_context);
ccea2b36 7539 if (!this->check_int_value(cap_arg, false, &cap_small))
1ad00fd4 7540 return Expression::make_error(this->location());
7541
7542 Numeric_constant nclen;
7543 Numeric_constant nccap;
7544 unsigned long vlen;
7545 unsigned long vcap;
7546 if (len_arg->numeric_constant_value(&nclen)
7547 && cap_arg->numeric_constant_value(&nccap)
7548 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
7549 && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID
7550 && vlen > vcap)
a9182619 7551 {
1ad00fd4 7552 this->report_error(_("len larger than cap"));
a9182619 7553 return Expression::make_error(this->location());
7554 }
1ad00fd4 7555
a9182619 7556 ++parg;
7557 }
7558
7559 if (parg != args->end())
7560 {
7561 this->report_error(_("too many arguments to make"));
7562 return Expression::make_error(this->location());
7563 }
7564
b13c66cd 7565 Location type_loc = first_arg->location();
a9182619 7566
7567 Expression* call;
7568 if (is_slice)
7569 {
321e5ad2 7570 Type* et = type->array_type()->element_type();
7571 Expression* type_arg = Expression::make_type_descriptor(et, type_loc);
a9182619 7572 if (cap_arg == NULL)
321e5ad2 7573 {
7574 Temporary_statement* temp = Statement::make_temporary(NULL,
7575 len_arg,
7576 loc);
7577 inserter->insert(temp);
7578 len_arg = Expression::make_temporary_reference(temp, loc);
7579 cap_arg = Expression::make_temporary_reference(temp, loc);
ccea2b36 7580 cap_small = len_small;
321e5ad2 7581 }
ccea2b36 7582
7583 Runtime::Function code = Runtime::MAKESLICE;
7584 if (!len_small || !cap_small)
7585 code = Runtime::MAKESLICE64;
7586 call = Runtime::make_call(code, loc, 3, type_arg, len_arg, cap_arg);
a9182619 7587 }
7588 else if (is_map)
321e5ad2 7589 {
7590 Expression* type_arg = Expression::make_type_descriptor(type, type_loc);
7591 call = Runtime::make_call(Runtime::MAKEMAP, loc, 4, type_arg, len_arg,
7592 Expression::make_nil(loc),
7593 Expression::make_nil(loc));
7594 }
a9182619 7595 else if (is_chan)
321e5ad2 7596 {
7597 Expression* type_arg = Expression::make_type_descriptor(type, type_loc);
7598 call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg);
7599 }
a9182619 7600 else
7601 go_unreachable();
7602
7603 return Expression::make_unsafe_cast(type, call, loc);
7604}
7605
321e5ad2 7606// Flatten a call to the predeclared append function. We do this in
7607// the flatten phase, not the lowering phase, so that we run after
7608// type checking and after order_evaluations.
7609
7610Expression*
7611Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function,
7612 Statement_inserter* inserter)
7613{
7614 if (this->is_error_expression())
7615 return this;
7616
7617 Location loc = this->location();
7618
7619 const Expression_list* args = this->args();
7620 go_assert(args != NULL && !args->empty());
7621
7622 Type* slice_type = args->front()->type();
7623 go_assert(slice_type->is_slice_type());
7624 Type* element_type = slice_type->array_type()->element_type();
7625
7626 if (args->size() == 1)
7627 {
7628 // append(s) evaluates to s.
7629 return args->front();
7630 }
7631
7632 Type* int_type = Type::lookup_integer_type("int");
7633 Type* uint_type = Type::lookup_integer_type("uint");
7634
7635 // Implementing
7636 // append(s1, s2...)
7637 // or
7638 // append(s1, a1, a2, a3, ...)
7639
7640 // s1tmp := s1
7641 Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(),
7642 loc);
7643 inserter->insert(s1tmp);
7644
7645 // l1tmp := len(s1tmp)
7646 Named_object* lenfn = gogo->lookup_global("len");
7647 Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc);
7648 Expression_list* call_args = new Expression_list();
7649 call_args->push_back(Expression::make_temporary_reference(s1tmp, loc));
7650 Expression* len = Expression::make_call(lenref, call_args, false, loc);
7651 gogo->lower_expression(function, inserter, &len);
7652 gogo->flatten_expression(function, inserter, &len);
7653 Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc);
7654 inserter->insert(l1tmp);
7655
7656 Temporary_statement* s2tmp = NULL;
7657 Temporary_statement* l2tmp = NULL;
7658 Expression_list* add = NULL;
7659 Expression* len2;
7660 if (this->is_varargs())
7661 {
7662 go_assert(args->size() == 2);
7663
7664 // s2tmp := s2
7665 s2tmp = Statement::make_temporary(NULL, args->back(), loc);
7666 inserter->insert(s2tmp);
7667
7668 // l2tmp := len(s2tmp)
7669 lenref = Expression::make_func_reference(lenfn, NULL, loc);
7670 call_args = new Expression_list();
7671 call_args->push_back(Expression::make_temporary_reference(s2tmp, loc));
7672 len = Expression::make_call(lenref, call_args, false, loc);
7673 gogo->lower_expression(function, inserter, &len);
7674 gogo->flatten_expression(function, inserter, &len);
7675 l2tmp = Statement::make_temporary(int_type, len, loc);
7676 inserter->insert(l2tmp);
7677
7678 // len2 = l2tmp
7679 len2 = Expression::make_temporary_reference(l2tmp, loc);
7680 }
7681 else
7682 {
7683 // We have to ensure that all the arguments are in variables
7684 // now, because otherwise if one of them is an index expression
7685 // into the current slice we could overwrite it before we fetch
7686 // it.
7687 add = new Expression_list();
7688 Expression_list::const_iterator pa = args->begin();
7689 for (++pa; pa != args->end(); ++pa)
7690 {
7691 if ((*pa)->is_variable())
7692 add->push_back(*pa);
7693 else
7694 {
7695 Temporary_statement* tmp = Statement::make_temporary(NULL, *pa,
7696 loc);
7697 inserter->insert(tmp);
7698 add->push_back(Expression::make_temporary_reference(tmp, loc));
7699 }
7700 }
7701
7702 // len2 = len(add)
7703 len2 = Expression::make_integer_ul(add->size(), int_type, loc);
7704 }
7705
7706 // ntmp := l1tmp + len2
7707 Expression* ref = Expression::make_temporary_reference(l1tmp, loc);
7708 Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc);
7709 gogo->lower_expression(function, inserter, &sum);
7710 gogo->flatten_expression(function, inserter, &sum);
7711 Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc);
7712 inserter->insert(ntmp);
7713
7714 // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ?
7715 // growslice(type, s1tmp, ntmp) :
7716 // s1tmp[:ntmp]
7717 // Using uint here means that if the computation of ntmp overflowed,
7718 // we will call growslice which will panic.
7719
7720 Expression* left = Expression::make_temporary_reference(ntmp, loc);
7721 left = Expression::make_cast(uint_type, left, loc);
7722
7723 Named_object* capfn = gogo->lookup_global("cap");
7724 Expression* capref = Expression::make_func_reference(capfn, NULL, loc);
7725 call_args = new Expression_list();
7726 call_args->push_back(Expression::make_temporary_reference(s1tmp, loc));
7727 Expression* right = Expression::make_call(capref, call_args, false, loc);
7728 right = Expression::make_cast(uint_type, right, loc);
7729
7730 Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc);
7731
7732 Expression* a1 = Expression::make_type_descriptor(element_type, loc);
7733 Expression* a2 = Expression::make_temporary_reference(s1tmp, loc);
7734 Expression* a3 = Expression::make_temporary_reference(ntmp, loc);
7735 Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3,
7736 a1, a2, a3);
7737 call = Expression::make_unsafe_cast(slice_type, call, loc);
7738
7739 ref = Expression::make_temporary_reference(s1tmp, loc);
7740 Expression* zero = Expression::make_integer_ul(0, int_type, loc);
7741 Expression* ref2 = Expression::make_temporary_reference(ntmp, loc);
7742 // FIXME: Mark this index as not requiring bounds checks.
7743 ref = Expression::make_index(ref, zero, ref2, NULL, loc);
7744
7745 Expression* rhs = Expression::make_conditional(cond, call, ref, loc);
7746
7747 gogo->lower_expression(function, inserter, &rhs);
7748 gogo->flatten_expression(function, inserter, &rhs);
7749
7750 Expression* lhs = Expression::make_temporary_reference(s1tmp, loc);
7751 Statement* assign = Statement::make_assignment(lhs, rhs, loc);
7752 inserter->insert(assign);
7753
7754 if (this->is_varargs())
7755 {
7756 // copy(s1tmp[l1tmp:], s2tmp)
7757 a1 = Expression::make_temporary_reference(s1tmp, loc);
7758 ref = Expression::make_temporary_reference(l1tmp, loc);
7759 Expression* nil = Expression::make_nil(loc);
7760 // FIXME: Mark this index as not requiring bounds checks.
7761 a1 = Expression::make_index(a1, ref, nil, NULL, loc);
7762
7763 a2 = Expression::make_temporary_reference(s2tmp, loc);
7764
7765 Named_object* copyfn = gogo->lookup_global("copy");
7766 Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc);
7767 call_args = new Expression_list();
7768 call_args->push_back(a1);
7769 call_args->push_back(a2);
7770 call = Expression::make_call(copyref, call_args, false, loc);
7771 gogo->lower_expression(function, inserter, &call);
7772 gogo->flatten_expression(function, inserter, &call);
7773 inserter->insert(Statement::make_statement(call, false));
7774 }
7775 else
7776 {
7777 // For each argument:
7778 // s1tmp[l1tmp+i] = a
7779 unsigned long i = 0;
7780 for (Expression_list::const_iterator pa = add->begin();
7781 pa != add->end();
7782 ++pa, ++i)
7783 {
7784 ref = Expression::make_temporary_reference(s1tmp, loc);
7785 ref2 = Expression::make_temporary_reference(l1tmp, loc);
7786 Expression* off = Expression::make_integer_ul(i, int_type, loc);
7787 ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc);
7788 // FIXME: Mark this index as not requiring bounds checks.
7789 lhs = Expression::make_index(ref, ref2, NULL, NULL, loc);
7790 gogo->lower_expression(function, inserter, &lhs);
7791 gogo->flatten_expression(function, inserter, &lhs);
03118c21 7792 // The flatten pass runs after the write barrier pass, so we
7793 // need to insert a write barrier here if necessary.
7794 if (!gogo->assign_needs_write_barrier(lhs))
7795 assign = Statement::make_assignment(lhs, *pa, loc);
7796 else
7797 {
7798 Function* f = function == NULL ? NULL : function->func_value();
7799 assign = gogo->assign_with_write_barrier(f, NULL, inserter,
7800 lhs, *pa, loc);
7801 }
321e5ad2 7802 inserter->insert(assign);
7803 }
7804 }
7805
7806 return Expression::make_temporary_reference(s1tmp, loc);
7807}
7808
a9182619 7809// Return whether an expression has an integer value. Report an error
7810// if not. This is used when handling calls to the predeclared make
ccea2b36 7811// function. Set *SMALL if the value is known to fit in type "int".
a9182619 7812
7813bool
ccea2b36 7814Builtin_call_expression::check_int_value(Expression* e, bool is_length,
7815 bool *small)
a9182619 7816{
ccea2b36 7817 *small = false;
7818
0c77715b 7819 Numeric_constant nc;
1ad00fd4 7820 if (e->numeric_constant_value(&nc))
a9182619 7821 {
1ad00fd4 7822 unsigned long v;
7823 switch (nc.to_unsigned_long(&v))
7824 {
7825 case Numeric_constant::NC_UL_VALID:
1b10c5e7 7826 break;
1ad00fd4 7827 case Numeric_constant::NC_UL_NOTINT:
631d5788 7828 go_error_at(e->location(), "non-integer %s argument to make",
7829 is_length ? "len" : "cap");
1ad00fd4 7830 return false;
7831 case Numeric_constant::NC_UL_NEGATIVE:
631d5788 7832 go_error_at(e->location(), "negative %s argument to make",
7833 is_length ? "len" : "cap");
1ad00fd4 7834 return false;
7835 case Numeric_constant::NC_UL_BIG:
7836 // We don't want to give a compile-time error for a 64-bit
7837 // value on a 32-bit target.
1b10c5e7 7838 break;
1ad00fd4 7839 }
1b10c5e7 7840
7841 mpz_t val;
7842 if (!nc.to_int(&val))
7843 go_unreachable();
7844 int bits = mpz_sizeinbase(val, 2);
7845 mpz_clear(val);
7846 Type* int_type = Type::lookup_integer_type("int");
7847 if (bits >= int_type->integer_type()->bits())
7848 {
631d5788 7849 go_error_at(e->location(), "%s argument too large for make",
7850 is_length ? "len" : "cap");
1b10c5e7 7851 return false;
7852 }
7853
ccea2b36 7854 *small = true;
1b10c5e7 7855 return true;
a9182619 7856 }
7857
1ad00fd4 7858 if (e->type()->integer_type() != NULL)
ccea2b36 7859 {
7860 int ebits = e->type()->integer_type()->bits();
7861 int intbits = Type::lookup_integer_type("int")->integer_type()->bits();
7862
7863 // We can treat ebits == intbits as small even for an unsigned
7864 // integer type, because we will convert the value to int and
7865 // then reject it in the runtime if it is negative.
7866 *small = ebits <= intbits;
7867
7868 return true;
7869 }
1ad00fd4 7870
631d5788 7871 go_error_at(e->location(), "non-integer %s argument to make",
7872 is_length ? "len" : "cap");
a9182619 7873 return false;
7874}
7875
e440a328 7876// Return the type of the real or imag functions, given the type of
fcbea5e4 7877// the argument. We need to map complex64 to float32 and complex128
7878// to float64, so it has to be done by name. This returns NULL if it
7879// can't figure out the type.
e440a328 7880
7881Type*
7882Builtin_call_expression::real_imag_type(Type* arg_type)
7883{
7884 if (arg_type == NULL || arg_type->is_abstract())
7885 return NULL;
7886 Named_type* nt = arg_type->named_type();
7887 if (nt == NULL)
7888 return NULL;
7889 while (nt->real_type()->named_type() != NULL)
7890 nt = nt->real_type()->named_type();
48080209 7891 if (nt->name() == "complex64")
e440a328 7892 return Type::lookup_float_type("float32");
7893 else if (nt->name() == "complex128")
7894 return Type::lookup_float_type("float64");
7895 else
7896 return NULL;
7897}
7898
48080209 7899// Return the type of the complex function, given the type of one of the
e440a328 7900// argments. Like real_imag_type, we have to map by name.
7901
7902Type*
48080209 7903Builtin_call_expression::complex_type(Type* arg_type)
e440a328 7904{
7905 if (arg_type == NULL || arg_type->is_abstract())
7906 return NULL;
7907 Named_type* nt = arg_type->named_type();
7908 if (nt == NULL)
7909 return NULL;
7910 while (nt->real_type()->named_type() != NULL)
7911 nt = nt->real_type()->named_type();
48080209 7912 if (nt->name() == "float32")
e440a328 7913 return Type::lookup_complex_type("complex64");
7914 else if (nt->name() == "float64")
7915 return Type::lookup_complex_type("complex128");
7916 else
7917 return NULL;
7918}
7919
7920// Return a single argument, or NULL if there isn't one.
7921
7922Expression*
7923Builtin_call_expression::one_arg() const
7924{
7925 const Expression_list* args = this->args();
aa615cb3 7926 if (args == NULL || args->size() != 1)
e440a328 7927 return NULL;
7928 return args->front();
7929}
7930
83921647 7931// A traversal class which looks for a call or receive expression.
7932
7933class Find_call_expression : public Traverse
7934{
7935 public:
7936 Find_call_expression()
7937 : Traverse(traverse_expressions),
7938 found_(false)
7939 { }
7940
7941 int
7942 expression(Expression**);
7943
7944 bool
7945 found()
7946 { return this->found_; }
7947
7948 private:
7949 bool found_;
7950};
7951
7952int
7953Find_call_expression::expression(Expression** pexpr)
7954{
7955 if ((*pexpr)->call_expression() != NULL
7956 || (*pexpr)->receive_expression() != NULL)
7957 {
7958 this->found_ = true;
7959 return TRAVERSE_EXIT;
7960 }
7961 return TRAVERSE_CONTINUE;
7962}
7963
7964// Return whether this is constant: len of a string constant, or len
7965// or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7966// unsafe.Alignof.
e440a328 7967
7968bool
7969Builtin_call_expression::do_is_constant() const
7970{
12e69faa 7971 if (this->is_error_expression())
7972 return true;
e440a328 7973 switch (this->code_)
7974 {
7975 case BUILTIN_LEN:
7976 case BUILTIN_CAP:
7977 {
0f914071 7978 if (this->seen_)
7979 return false;
7980
e440a328 7981 Expression* arg = this->one_arg();
7982 if (arg == NULL)
7983 return false;
7984 Type* arg_type = arg->type();
7985
7986 if (arg_type->points_to() != NULL
7987 && arg_type->points_to()->array_type() != NULL
411eb89e 7988 && !arg_type->points_to()->is_slice_type())
e440a328 7989 arg_type = arg_type->points_to();
7990
83921647 7991 // The len and cap functions are only constant if there are no
7992 // function calls or channel operations in the arguments.
7993 // Otherwise we have to make the call.
7994 if (!arg->is_constant())
7995 {
7996 Find_call_expression find_call;
7997 Expression::traverse(&arg, &find_call);
7998 if (find_call.found())
7999 return false;
8000 }
8001
e440a328 8002 if (arg_type->array_type() != NULL
8003 && arg_type->array_type()->length() != NULL)
0f914071 8004 return true;
e440a328 8005
8006 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
0f914071 8007 {
8008 this->seen_ = true;
8009 bool ret = arg->is_constant();
8010 this->seen_ = false;
8011 return ret;
8012 }
e440a328 8013 }
8014 break;
8015
8016 case BUILTIN_SIZEOF:
8017 case BUILTIN_ALIGNOF:
8018 return this->one_arg() != NULL;
8019
8020 case BUILTIN_OFFSETOF:
8021 {
8022 Expression* arg = this->one_arg();
8023 if (arg == NULL)
8024 return false;
8025 return arg->field_reference_expression() != NULL;
8026 }
8027
48080209 8028 case BUILTIN_COMPLEX:
e440a328 8029 {
8030 const Expression_list* args = this->args();
8031 if (args != NULL && args->size() == 2)
8032 return args->front()->is_constant() && args->back()->is_constant();
8033 }
8034 break;
8035
8036 case BUILTIN_REAL:
8037 case BUILTIN_IMAG:
8038 {
8039 Expression* arg = this->one_arg();
8040 return arg != NULL && arg->is_constant();
8041 }
8042
8043 default:
8044 break;
8045 }
8046
8047 return false;
8048}
8049
0c77715b 8050// Return a numeric constant if possible.
e440a328 8051
8052bool
0c77715b 8053Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 8054{
8055 if (this->code_ == BUILTIN_LEN
8056 || this->code_ == BUILTIN_CAP)
8057 {
8058 Expression* arg = this->one_arg();
8059 if (arg == NULL)
8060 return false;
8061 Type* arg_type = arg->type();
8062
8063 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
8064 {
8065 std::string sval;
8066 if (arg->string_constant_value(&sval))
8067 {
0c77715b 8068 nc->set_unsigned_long(Type::lookup_integer_type("int"),
8069 sval.length());
e440a328 8070 return true;
8071 }
8072 }
8073
8074 if (arg_type->points_to() != NULL
8075 && arg_type->points_to()->array_type() != NULL
411eb89e 8076 && !arg_type->points_to()->is_slice_type())
e440a328 8077 arg_type = arg_type->points_to();
8078
8079 if (arg_type->array_type() != NULL
8080 && arg_type->array_type()->length() != NULL)
8081 {
0f914071 8082 if (this->seen_)
8083 return false;
e440a328 8084 Expression* e = arg_type->array_type()->length();
0f914071 8085 this->seen_ = true;
0c77715b 8086 bool r = e->numeric_constant_value(nc);
0f914071 8087 this->seen_ = false;
8088 if (r)
e440a328 8089 {
0c77715b 8090 if (!nc->set_type(Type::lookup_integer_type("int"), false,
8091 this->location()))
8092 r = false;
e440a328 8093 }
0c77715b 8094 return r;
e440a328 8095 }
8096 }
8097 else if (this->code_ == BUILTIN_SIZEOF
8098 || this->code_ == BUILTIN_ALIGNOF)
8099 {
8100 Expression* arg = this->one_arg();
8101 if (arg == NULL)
8102 return false;
8103 Type* arg_type = arg->type();
5c13bd80 8104 if (arg_type->is_error())
e440a328 8105 return false;
8106 if (arg_type->is_abstract())
8107 return false;
2c809f8f 8108 if (this->seen_)
8109 return false;
927a01eb 8110
3f378015 8111 int64_t ret;
e440a328 8112 if (this->code_ == BUILTIN_SIZEOF)
8113 {
2c809f8f 8114 this->seen_ = true;
8115 bool ok = arg_type->backend_type_size(this->gogo_, &ret);
8116 this->seen_ = false;
8117 if (!ok)
e440a328 8118 return false;
8119 }
8120 else if (this->code_ == BUILTIN_ALIGNOF)
8121 {
2c809f8f 8122 bool ok;
8123 this->seen_ = true;
637bd3af 8124 if (arg->field_reference_expression() == NULL)
2c809f8f 8125 ok = arg_type->backend_type_align(this->gogo_, &ret);
637bd3af 8126 else
e440a328 8127 {
8128 // Calling unsafe.Alignof(s.f) returns the alignment of
8129 // the type of f when it is used as a field in a struct.
2c809f8f 8130 ok = arg_type->backend_type_field_align(this->gogo_, &ret);
e440a328 8131 }
2c809f8f 8132 this->seen_ = false;
8133 if (!ok)
8134 return false;
e440a328 8135 }
8136 else
c3e6f413 8137 go_unreachable();
927a01eb 8138
3f378015 8139 mpz_t zval;
8140 set_mpz_from_int64(&zval, ret);
8141 nc->set_int(Type::lookup_integer_type("uintptr"), zval);
8142 mpz_clear(zval);
e440a328 8143 return true;
8144 }
8145 else if (this->code_ == BUILTIN_OFFSETOF)
8146 {
8147 Expression* arg = this->one_arg();
8148 if (arg == NULL)
8149 return false;
8150 Field_reference_expression* farg = arg->field_reference_expression();
8151 if (farg == NULL)
8152 return false;
2c809f8f 8153 if (this->seen_)
8154 return false;
8155
3f378015 8156 int64_t total_offset = 0;
9a4bd570 8157 while (true)
8158 {
8159 Expression* struct_expr = farg->expr();
8160 Type* st = struct_expr->type();
8161 if (st->struct_type() == NULL)
8162 return false;
8163 if (st->named_type() != NULL)
8164 st->named_type()->convert(this->gogo_);
3f378015 8165 int64_t offset;
2c809f8f 8166 this->seen_ = true;
8167 bool ok = st->struct_type()->backend_field_offset(this->gogo_,
8168 farg->field_index(),
8169 &offset);
8170 this->seen_ = false;
8171 if (!ok)
8172 return false;
9a4bd570 8173 total_offset += offset;
8174 if (farg->implicit() && struct_expr->field_reference_expression() != NULL)
8175 {
8176 // Go up until we reach the original base.
8177 farg = struct_expr->field_reference_expression();
8178 continue;
8179 }
8180 break;
8181 }
3f378015 8182 mpz_t zval;
8183 set_mpz_from_int64(&zval, total_offset);
8184 nc->set_int(Type::lookup_integer_type("uintptr"), zval);
8185 mpz_clear(zval);
e440a328 8186 return true;
8187 }
0c77715b 8188 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
e440a328 8189 {
8190 Expression* arg = this->one_arg();
8191 if (arg == NULL)
8192 return false;
8193
0c77715b 8194 Numeric_constant argnc;
8195 if (!arg->numeric_constant_value(&argnc))
8196 return false;
8197
fcbea5e4 8198 mpc_t val;
8199 if (!argnc.to_complex(&val))
0c77715b 8200 return false;
e440a328 8201
0c77715b 8202 Type* type = Builtin_call_expression::real_imag_type(argnc.type());
8203 if (this->code_ == BUILTIN_REAL)
fcbea5e4 8204 nc->set_float(type, mpc_realref(val));
0c77715b 8205 else
fcbea5e4 8206 nc->set_float(type, mpc_imagref(val));
8207 mpc_clear(val);
0c77715b 8208 return true;
e440a328 8209 }
0c77715b 8210 else if (this->code_ == BUILTIN_COMPLEX)
e440a328 8211 {
8212 const Expression_list* args = this->args();
8213 if (args == NULL || args->size() != 2)
8214 return false;
8215
0c77715b 8216 Numeric_constant rnc;
8217 if (!args->front()->numeric_constant_value(&rnc))
8218 return false;
8219 Numeric_constant inc;
8220 if (!args->back()->numeric_constant_value(&inc))
8221 return false;
8222
8223 if (rnc.type() != NULL
8224 && !rnc.type()->is_abstract()
8225 && inc.type() != NULL
8226 && !inc.type()->is_abstract()
8227 && !Type::are_identical(rnc.type(), inc.type(), false, NULL))
8228 return false;
8229
e440a328 8230 mpfr_t r;
0c77715b 8231 if (!rnc.to_float(&r))
8232 return false;
8233 mpfr_t i;
8234 if (!inc.to_float(&i))
e440a328 8235 {
8236 mpfr_clear(r);
8237 return false;
8238 }
8239
0c77715b 8240 Type* arg_type = rnc.type();
8241 if (arg_type == NULL || arg_type->is_abstract())
8242 arg_type = inc.type();
e440a328 8243
fcbea5e4 8244 mpc_t val;
8245 mpc_init2(val, mpc_precision);
8246 mpc_set_fr_fr(val, r, i, MPC_RNDNN);
e440a328 8247 mpfr_clear(r);
8248 mpfr_clear(i);
8249
fcbea5e4 8250 Type* type = Builtin_call_expression::complex_type(arg_type);
8251 nc->set_complex(type, val);
8252
8253 mpc_clear(val);
8254
0c77715b 8255 return true;
e440a328 8256 }
8257
8258 return false;
8259}
8260
a7549a6a 8261// Give an error if we are discarding the value of an expression which
8262// should not normally be discarded. We don't give an error for
8263// discarding the value of an ordinary function call, but we do for
8264// builtin functions, purely for consistency with the gc compiler.
8265
4f2138d7 8266bool
a7549a6a 8267Builtin_call_expression::do_discarding_value()
8268{
8269 switch (this->code_)
8270 {
8271 case BUILTIN_INVALID:
8272 default:
8273 go_unreachable();
8274
8275 case BUILTIN_APPEND:
8276 case BUILTIN_CAP:
8277 case BUILTIN_COMPLEX:
8278 case BUILTIN_IMAG:
8279 case BUILTIN_LEN:
8280 case BUILTIN_MAKE:
8281 case BUILTIN_NEW:
8282 case BUILTIN_REAL:
8283 case BUILTIN_ALIGNOF:
8284 case BUILTIN_OFFSETOF:
8285 case BUILTIN_SIZEOF:
8286 this->unused_value_error();
4f2138d7 8287 return false;
a7549a6a 8288
8289 case BUILTIN_CLOSE:
8290 case BUILTIN_COPY:
1cce762f 8291 case BUILTIN_DELETE:
a7549a6a 8292 case BUILTIN_PANIC:
8293 case BUILTIN_PRINT:
8294 case BUILTIN_PRINTLN:
8295 case BUILTIN_RECOVER:
4f2138d7 8296 return true;
a7549a6a 8297 }
8298}
8299
e440a328 8300// Return the type.
8301
8302Type*
8303Builtin_call_expression::do_type()
8304{
79651b1f 8305 if (this->is_error_expression())
8306 return Type::make_error_type();
e440a328 8307 switch (this->code_)
8308 {
8309 case BUILTIN_INVALID:
8310 default:
79651b1f 8311 return Type::make_error_type();
e440a328 8312
8313 case BUILTIN_NEW:
8314 case BUILTIN_MAKE:
8315 {
8316 const Expression_list* args = this->args();
8317 if (args == NULL || args->empty())
8318 return Type::make_error_type();
8319 return Type::make_pointer_type(args->front()->type());
8320 }
8321
8322 case BUILTIN_CAP:
8323 case BUILTIN_COPY:
8324 case BUILTIN_LEN:
7ba86326 8325 return Type::lookup_integer_type("int");
8326
e440a328 8327 case BUILTIN_ALIGNOF:
8328 case BUILTIN_OFFSETOF:
8329 case BUILTIN_SIZEOF:
7ba86326 8330 return Type::lookup_integer_type("uintptr");
e440a328 8331
8332 case BUILTIN_CLOSE:
1cce762f 8333 case BUILTIN_DELETE:
e440a328 8334 case BUILTIN_PANIC:
8335 case BUILTIN_PRINT:
8336 case BUILTIN_PRINTLN:
8337 return Type::make_void_type();
8338
e440a328 8339 case BUILTIN_RECOVER:
823c7e3d 8340 return Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 8341
8342 case BUILTIN_APPEND:
8343 {
8344 const Expression_list* args = this->args();
8345 if (args == NULL || args->empty())
8346 return Type::make_error_type();
3ff4863b 8347 Type *ret = args->front()->type();
8348 if (!ret->is_slice_type())
8349 return Type::make_error_type();
8350 return ret;
e440a328 8351 }
8352
8353 case BUILTIN_REAL:
8354 case BUILTIN_IMAG:
8355 {
8356 Expression* arg = this->one_arg();
8357 if (arg == NULL)
8358 return Type::make_error_type();
8359 Type* t = arg->type();
8360 if (t->is_abstract())
8361 t = t->make_non_abstract_type();
8362 t = Builtin_call_expression::real_imag_type(t);
8363 if (t == NULL)
8364 t = Type::make_error_type();
8365 return t;
8366 }
8367
48080209 8368 case BUILTIN_COMPLEX:
e440a328 8369 {
8370 const Expression_list* args = this->args();
8371 if (args == NULL || args->size() != 2)
8372 return Type::make_error_type();
8373 Type* t = args->front()->type();
8374 if (t->is_abstract())
8375 {
8376 t = args->back()->type();
8377 if (t->is_abstract())
8378 t = t->make_non_abstract_type();
8379 }
48080209 8380 t = Builtin_call_expression::complex_type(t);
e440a328 8381 if (t == NULL)
8382 t = Type::make_error_type();
8383 return t;
8384 }
8385 }
8386}
8387
8388// Determine the type.
8389
8390void
8391Builtin_call_expression::do_determine_type(const Type_context* context)
8392{
fb94b0ca 8393 if (!this->determining_types())
8394 return;
8395
e440a328 8396 this->fn()->determine_type_no_context();
8397
8398 const Expression_list* args = this->args();
8399
8400 bool is_print;
8401 Type* arg_type = NULL;
321e5ad2 8402 Type* trailing_arg_types = NULL;
e440a328 8403 switch (this->code_)
8404 {
8405 case BUILTIN_PRINT:
8406 case BUILTIN_PRINTLN:
8407 // Do not force a large integer constant to "int".
8408 is_print = true;
8409 break;
8410
8411 case BUILTIN_REAL:
8412 case BUILTIN_IMAG:
48080209 8413 arg_type = Builtin_call_expression::complex_type(context->type);
f6bc81e6 8414 if (arg_type == NULL)
8415 arg_type = Type::lookup_complex_type("complex128");
e440a328 8416 is_print = false;
8417 break;
8418
48080209 8419 case BUILTIN_COMPLEX:
e440a328 8420 {
48080209 8421 // For the complex function the type of one operand can
e440a328 8422 // determine the type of the other, as in a binary expression.
8423 arg_type = Builtin_call_expression::real_imag_type(context->type);
f6bc81e6 8424 if (arg_type == NULL)
8425 arg_type = Type::lookup_float_type("float64");
e440a328 8426 if (args != NULL && args->size() == 2)
8427 {
8428 Type* t1 = args->front()->type();
c849bb59 8429 Type* t2 = args->back()->type();
e440a328 8430 if (!t1->is_abstract())
8431 arg_type = t1;
8432 else if (!t2->is_abstract())
8433 arg_type = t2;
8434 }
8435 is_print = false;
8436 }
8437 break;
8438
321e5ad2 8439 case BUILTIN_APPEND:
8440 if (!this->is_varargs()
8441 && args != NULL
8442 && !args->empty()
8443 && args->front()->type()->is_slice_type())
8444 trailing_arg_types =
8445 args->front()->type()->array_type()->element_type();
8446 is_print = false;
8447 break;
8448
e440a328 8449 default:
8450 is_print = false;
8451 break;
8452 }
8453
8454 if (args != NULL)
8455 {
8456 for (Expression_list::const_iterator pa = args->begin();
8457 pa != args->end();
8458 ++pa)
8459 {
8460 Type_context subcontext;
8461 subcontext.type = arg_type;
8462
8463 if (is_print)
8464 {
8465 // We want to print large constants, we so can't just
8466 // use the appropriate nonabstract type. Use uint64 for
8467 // an integer if we know it is nonnegative, otherwise
8468 // use int64 for a integer, otherwise use float64 for a
8469 // float or complex128 for a complex.
8470 Type* want_type = NULL;
8471 Type* atype = (*pa)->type();
8472 if (atype->is_abstract())
8473 {
8474 if (atype->integer_type() != NULL)
8475 {
0c77715b 8476 Numeric_constant nc;
8477 if (this->numeric_constant_value(&nc))
8478 {
8479 mpz_t val;
8480 if (nc.to_int(&val))
8481 {
8482 if (mpz_sgn(val) >= 0)
8483 want_type = Type::lookup_integer_type("uint64");
8484 mpz_clear(val);
8485 }
8486 }
8487 if (want_type == NULL)
e440a328 8488 want_type = Type::lookup_integer_type("int64");
e440a328 8489 }
8490 else if (atype->float_type() != NULL)
8491 want_type = Type::lookup_float_type("float64");
8492 else if (atype->complex_type() != NULL)
8493 want_type = Type::lookup_complex_type("complex128");
8494 else if (atype->is_abstract_string_type())
8495 want_type = Type::lookup_string_type();
8496 else if (atype->is_abstract_boolean_type())
8497 want_type = Type::lookup_bool_type();
8498 else
c3e6f413 8499 go_unreachable();
e440a328 8500 subcontext.type = want_type;
8501 }
8502 }
8503
8504 (*pa)->determine_type(&subcontext);
321e5ad2 8505
8506 if (trailing_arg_types != NULL)
8507 {
8508 arg_type = trailing_arg_types;
8509 trailing_arg_types = NULL;
8510 }
e440a328 8511 }
8512 }
8513}
8514
8515// If there is exactly one argument, return true. Otherwise give an
8516// error message and return false.
8517
8518bool
8519Builtin_call_expression::check_one_arg()
8520{
8521 const Expression_list* args = this->args();
8522 if (args == NULL || args->size() < 1)
8523 {
8524 this->report_error(_("not enough arguments"));
8525 return false;
8526 }
8527 else if (args->size() > 1)
8528 {
8529 this->report_error(_("too many arguments"));
8530 return false;
8531 }
8532 if (args->front()->is_error_expression()
5c13bd80 8533 || args->front()->type()->is_error())
e440a328 8534 {
8535 this->set_is_error();
8536 return false;
8537 }
8538 return true;
8539}
8540
8541// Check argument types for a builtin function.
8542
8543void
8544Builtin_call_expression::do_check_types(Gogo*)
8545{
375646ea 8546 if (this->is_error_expression())
8547 return;
e440a328 8548 switch (this->code_)
8549 {
8550 case BUILTIN_INVALID:
8551 case BUILTIN_NEW:
8552 case BUILTIN_MAKE:
cd238b8d 8553 case BUILTIN_DELETE:
e440a328 8554 return;
8555
8556 case BUILTIN_LEN:
8557 case BUILTIN_CAP:
8558 {
8559 // The single argument may be either a string or an array or a
8560 // map or a channel, or a pointer to a closed array.
8561 if (this->check_one_arg())
8562 {
8563 Type* arg_type = this->one_arg()->type();
8564 if (arg_type->points_to() != NULL
8565 && arg_type->points_to()->array_type() != NULL
411eb89e 8566 && !arg_type->points_to()->is_slice_type())
e440a328 8567 arg_type = arg_type->points_to();
8568 if (this->code_ == BUILTIN_CAP)
8569 {
5c13bd80 8570 if (!arg_type->is_error()
e440a328 8571 && arg_type->array_type() == NULL
8572 && arg_type->channel_type() == NULL)
8573 this->report_error(_("argument must be array or slice "
8574 "or channel"));
8575 }
8576 else
8577 {
5c13bd80 8578 if (!arg_type->is_error()
e440a328 8579 && !arg_type->is_string_type()
8580 && arg_type->array_type() == NULL
8581 && arg_type->map_type() == NULL
8582 && arg_type->channel_type() == NULL)
8583 this->report_error(_("argument must be string or "
8584 "array or slice or map or channel"));
8585 }
8586 }
8587 }
8588 break;
8589
8590 case BUILTIN_PRINT:
8591 case BUILTIN_PRINTLN:
8592 {
8593 const Expression_list* args = this->args();
8594 if (args == NULL)
8595 {
8596 if (this->code_ == BUILTIN_PRINT)
631d5788 8597 go_warning_at(this->location(), 0,
e440a328 8598 "no arguments for builtin function %<%s%>",
8599 (this->code_ == BUILTIN_PRINT
8600 ? "print"
8601 : "println"));
8602 }
8603 else
8604 {
8605 for (Expression_list::const_iterator p = args->begin();
8606 p != args->end();
8607 ++p)
8608 {
8609 Type* type = (*p)->type();
5c13bd80 8610 if (type->is_error()
e440a328 8611 || type->is_string_type()
8612 || type->integer_type() != NULL
8613 || type->float_type() != NULL
8614 || type->complex_type() != NULL
8615 || type->is_boolean_type()
8616 || type->points_to() != NULL
8617 || type->interface_type() != NULL
8618 || type->channel_type() != NULL
8619 || type->map_type() != NULL
8620 || type->function_type() != NULL
411eb89e 8621 || type->is_slice_type())
e440a328 8622 ;
acf8e158 8623 else if ((*p)->is_type_expression())
8624 {
8625 // If this is a type expression it's going to give
8626 // an error anyhow, so we don't need one here.
8627 }
e440a328 8628 else
8629 this->report_error(_("unsupported argument type to "
8630 "builtin function"));
8631 }
8632 }
8633 }
8634 break;
8635
8636 case BUILTIN_CLOSE:
e440a328 8637 if (this->check_one_arg())
8638 {
8639 if (this->one_arg()->type()->channel_type() == NULL)
8640 this->report_error(_("argument must be channel"));
5202d986 8641 else if (!this->one_arg()->type()->channel_type()->may_send())
8642 this->report_error(_("cannot close receive-only channel"));
e440a328 8643 }
8644 break;
8645
8646 case BUILTIN_PANIC:
8647 case BUILTIN_SIZEOF:
8648 case BUILTIN_ALIGNOF:
8649 this->check_one_arg();
8650 break;
8651
8652 case BUILTIN_RECOVER:
6334270b 8653 if (this->args() != NULL
8654 && !this->args()->empty()
8655 && !this->recover_arg_is_set_)
e440a328 8656 this->report_error(_("too many arguments"));
8657 break;
8658
8659 case BUILTIN_OFFSETOF:
8660 if (this->check_one_arg())
8661 {
8662 Expression* arg = this->one_arg();
8663 if (arg->field_reference_expression() == NULL)
8664 this->report_error(_("argument must be a field reference"));
8665 }
8666 break;
8667
8668 case BUILTIN_COPY:
8669 {
8670 const Expression_list* args = this->args();
8671 if (args == NULL || args->size() < 2)
8672 {
8673 this->report_error(_("not enough arguments"));
8674 break;
8675 }
8676 else if (args->size() > 2)
8677 {
8678 this->report_error(_("too many arguments"));
8679 break;
8680 }
8681 Type* arg1_type = args->front()->type();
8682 Type* arg2_type = args->back()->type();
5c13bd80 8683 if (arg1_type->is_error() || arg2_type->is_error())
6bebb39d 8684 {
8685 this->set_is_error();
8686 break;
8687 }
e440a328 8688
8689 Type* e1;
411eb89e 8690 if (arg1_type->is_slice_type())
e440a328 8691 e1 = arg1_type->array_type()->element_type();
8692 else
8693 {
8694 this->report_error(_("left argument must be a slice"));
8695 break;
8696 }
8697
411eb89e 8698 if (arg2_type->is_slice_type())
60963afd 8699 {
8700 Type* e2 = arg2_type->array_type()->element_type();
8701 if (!Type::are_identical(e1, e2, true, NULL))
8702 this->report_error(_("element types must be the same"));
8703 }
e440a328 8704 else if (arg2_type->is_string_type())
e440a328 8705 {
60963afd 8706 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
8707 this->report_error(_("first argument must be []byte"));
e440a328 8708 }
60963afd 8709 else
8710 this->report_error(_("second argument must be slice or string"));
e440a328 8711 }
8712 break;
8713
8714 case BUILTIN_APPEND:
8715 {
8716 const Expression_list* args = this->args();
321e5ad2 8717 if (args == NULL || args->empty())
e440a328 8718 {
8719 this->report_error(_("not enough arguments"));
8720 break;
8721 }
321e5ad2 8722
8723 Type* slice_type = args->front()->type();
8724 if (!slice_type->is_slice_type())
6bebb39d 8725 {
321e5ad2 8726 if (slice_type->is_error_type())
8727 break;
8728 if (slice_type->is_nil_type())
8729 go_error_at(args->front()->location(), "use of untyped nil");
8730 else
8731 go_error_at(args->front()->location(),
8732 "argument 1 must be a slice");
6bebb39d 8733 this->set_is_error();
8734 break;
8735 }
cd238b8d 8736
321e5ad2 8737 Type* element_type = slice_type->array_type()->element_type();
8738 if (this->is_varargs())
4fd4fcf4 8739 {
321e5ad2 8740 if (!args->back()->type()->is_slice_type()
8741 && !args->back()->type()->is_string_type())
8742 {
8743 go_error_at(args->back()->location(),
8744 "invalid use of %<...%> with non-slice/non-string");
8745 this->set_is_error();
8746 break;
8747 }
4fd4fcf4 8748
321e5ad2 8749 if (args->size() < 2)
8750 {
8751 this->report_error(_("not enough arguments"));
8752 break;
8753 }
8754 if (args->size() > 2)
8755 {
8756 this->report_error(_("too many arguments"));
8757 break;
8758 }
8759
8760 if (args->back()->type()->is_string_type()
8761 && element_type->integer_type() != NULL
8762 && element_type->integer_type()->is_byte())
8763 {
8764 // Permit append(s1, s2...) when s1 is a slice of
8765 // bytes and s2 is a string type.
8766 }
e440a328 8767 else
8768 {
321e5ad2 8769 // We have to test for assignment compatibility to a
8770 // slice of the element type, which is not necessarily
8771 // the same as the type of the first argument: the
8772 // first argument might have a named type.
8773 Type* check_type = Type::make_array_type(element_type, NULL);
8774 std::string reason;
8775 if (!Type::are_assignable(check_type, args->back()->type(),
8776 &reason))
8777 {
8778 if (reason.empty())
8779 go_error_at(args->back()->location(),
8780 "argument 2 has invalid type");
8781 else
8782 go_error_at(args->back()->location(),
8783 "argument 2 has invalid type (%s)",
8784 reason.c_str());
8785 this->set_is_error();
8786 break;
8787 }
8788 }
8789 }
8790 else
8791 {
8792 Expression_list::const_iterator pa = args->begin();
8793 int i = 2;
8794 for (++pa; pa != args->end(); ++pa, ++i)
8795 {
8796 std::string reason;
8797 if (!Type::are_assignable(element_type, (*pa)->type(),
8798 &reason))
8799 {
8800 if (reason.empty())
8801 go_error_at((*pa)->location(),
8802 "argument %d has incompatible type", i);
8803 else
8804 go_error_at((*pa)->location(),
8805 "argument %d has incompatible type (%s)",
8806 i, reason.c_str());
8807 this->set_is_error();
8808 }
e440a328 8809 }
8810 }
e440a328 8811 }
321e5ad2 8812 break;
e440a328 8813
8814 case BUILTIN_REAL:
8815 case BUILTIN_IMAG:
8816 if (this->check_one_arg())
8817 {
8818 if (this->one_arg()->type()->complex_type() == NULL)
8819 this->report_error(_("argument must have complex type"));
8820 }
8821 break;
8822
48080209 8823 case BUILTIN_COMPLEX:
e440a328 8824 {
8825 const Expression_list* args = this->args();
8826 if (args == NULL || args->size() < 2)
8827 this->report_error(_("not enough arguments"));
8828 else if (args->size() > 2)
8829 this->report_error(_("too many arguments"));
8830 else if (args->front()->is_error_expression()
5c13bd80 8831 || args->front()->type()->is_error()
e440a328 8832 || args->back()->is_error_expression()
5c13bd80 8833 || args->back()->type()->is_error())
e440a328 8834 this->set_is_error();
8835 else if (!Type::are_identical(args->front()->type(),
07ba8be5 8836 args->back()->type(), true, NULL))
48080209 8837 this->report_error(_("complex arguments must have identical types"));
e440a328 8838 else if (args->front()->type()->float_type() == NULL)
48080209 8839 this->report_error(_("complex arguments must have "
e440a328 8840 "floating-point type"));
8841 }
8842 break;
8843
8844 default:
c3e6f413 8845 go_unreachable();
e440a328 8846 }
8847}
8848
72666aed 8849Expression*
8850Builtin_call_expression::do_copy()
8851{
8852 Call_expression* bce =
8853 new Builtin_call_expression(this->gogo_, this->fn()->copy(),
da244e59 8854 (this->args() == NULL
8855 ? NULL
8856 : this->args()->copy()),
72666aed 8857 this->is_varargs(),
8858 this->location());
8859
8860 if (this->varargs_are_lowered())
8861 bce->set_varargs_are_lowered();
8862 return bce;
8863}
8864
ea664253 8865// Return the backend representation for a builtin function.
e440a328 8866
ea664253 8867Bexpression*
8868Builtin_call_expression::do_get_backend(Translate_context* context)
e440a328 8869{
8870 Gogo* gogo = context->gogo();
b13c66cd 8871 Location location = this->location();
a0d8874e 8872
8873 if (this->is_erroneous_call())
8874 {
8875 go_assert(saw_errors());
8876 return gogo->backend()->error_expression();
8877 }
8878
e440a328 8879 switch (this->code_)
8880 {
8881 case BUILTIN_INVALID:
8882 case BUILTIN_NEW:
8883 case BUILTIN_MAKE:
c3e6f413 8884 go_unreachable();
e440a328 8885
8886 case BUILTIN_LEN:
8887 case BUILTIN_CAP:
8888 {
8889 const Expression_list* args = this->args();
c484d925 8890 go_assert(args != NULL && args->size() == 1);
2c809f8f 8891 Expression* arg = args->front();
e440a328 8892 Type* arg_type = arg->type();
0f914071 8893
8894 if (this->seen_)
8895 {
c484d925 8896 go_assert(saw_errors());
ea664253 8897 return context->backend()->error_expression();
0f914071 8898 }
8899 this->seen_ = true;
0f914071 8900 this->seen_ = false;
e440a328 8901 if (arg_type->points_to() != NULL)
8902 {
8903 arg_type = arg_type->points_to();
c484d925 8904 go_assert(arg_type->array_type() != NULL
411eb89e 8905 && !arg_type->is_slice_type());
2c809f8f 8906 arg = Expression::make_unary(OPERATOR_MULT, arg, location);
e440a328 8907 }
8908
1b1f2abf 8909 Type* int_type = Type::lookup_integer_type("int");
2c809f8f 8910 Expression* val;
e440a328 8911 if (this->code_ == BUILTIN_LEN)
8912 {
8913 if (arg_type->is_string_type())
2c809f8f 8914 val = Expression::make_string_info(arg, STRING_INFO_LENGTH,
8915 location);
e440a328 8916 else if (arg_type->array_type() != NULL)
0f914071 8917 {
8918 if (this->seen_)
8919 {
c484d925 8920 go_assert(saw_errors());
ea664253 8921 return context->backend()->error_expression();
0f914071 8922 }
8923 this->seen_ = true;
2c809f8f 8924 val = arg_type->array_type()->get_length(gogo, arg);
0f914071 8925 this->seen_ = false;
8926 }
0d5530d9 8927 else if (arg_type->map_type() != NULL
8928 || arg_type->channel_type() != NULL)
8929 {
8930 // The first field is the length. If the pointer is
8931 // nil, the length is zero.
8932 Type* pint_type = Type::make_pointer_type(int_type);
8933 arg = Expression::make_unsafe_cast(pint_type, arg, location);
8934 Expression* nil = Expression::make_nil(location);
8935 nil = Expression::make_cast(pint_type, nil, location);
8936 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ,
8937 arg, nil, location);
8938 Expression* zero = Expression::make_integer_ul(0, int_type,
8939 location);
8940 Expression* indir = Expression::make_unary(OPERATOR_MULT,
8941 arg, location);
8942 val = Expression::make_conditional(cmp, zero, indir, location);
8943 }
e440a328 8944 else
c3e6f413 8945 go_unreachable();
e440a328 8946 }
8947 else
8948 {
8949 if (arg_type->array_type() != NULL)
0f914071 8950 {
8951 if (this->seen_)
8952 {
c484d925 8953 go_assert(saw_errors());
ea664253 8954 return context->backend()->error_expression();
0f914071 8955 }
8956 this->seen_ = true;
2c809f8f 8957 val = arg_type->array_type()->get_capacity(gogo, arg);
0f914071 8958 this->seen_ = false;
8959 }
e440a328 8960 else if (arg_type->channel_type() != NULL)
132ed071 8961 {
8962 // The second field is the capacity. If the pointer
8963 // is nil, the capacity is zero.
8964 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8965 Type* pint_type = Type::make_pointer_type(int_type);
8966 Expression* parg = Expression::make_unsafe_cast(uintptr_type,
8967 arg,
8968 location);
8969 int off = int_type->integer_type()->bits() / 8;
8970 Expression* eoff = Expression::make_integer_ul(off,
8971 uintptr_type,
8972 location);
8973 parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff,
8974 location);
8975 parg = Expression::make_unsafe_cast(pint_type, parg, location);
8976 Expression* nil = Expression::make_nil(location);
8977 nil = Expression::make_cast(pint_type, nil, location);
8978 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ,
8979 arg, nil, location);
8980 Expression* zero = Expression::make_integer_ul(0, int_type,
8981 location);
8982 Expression* indir = Expression::make_unary(OPERATOR_MULT,
8983 parg, location);
8984 val = Expression::make_conditional(cmp, zero, indir, location);
8985 }
e440a328 8986 else
c3e6f413 8987 go_unreachable();
e440a328 8988 }
8989
2c809f8f 8990 return Expression::make_cast(int_type, val,
ea664253 8991 location)->get_backend(context);
e440a328 8992 }
8993
8994 case BUILTIN_PRINT:
8995 case BUILTIN_PRINTLN:
8996 {
8997 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
88b03a70 8998
8999 Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK,
9000 location, 0);
e440a328 9001
9002 const Expression_list* call_args = this->args();
9003 if (call_args != NULL)
9004 {
9005 for (Expression_list::const_iterator p = call_args->begin();
9006 p != call_args->end();
9007 ++p)
9008 {
9009 if (is_ln && p != call_args->begin())
9010 {
2c809f8f 9011 Expression* print_space =
88b03a70 9012 Runtime::make_call(Runtime::PRINTSP, location, 0);
e440a328 9013
2c809f8f 9014 print_stmts =
9015 Expression::make_compound(print_stmts, print_space,
9016 location);
9017 }
e440a328 9018
2c809f8f 9019 Expression* arg = *p;
9020 Type* type = arg->type();
9021 Runtime::Function code;
e440a328 9022 if (type->is_string_type())
88b03a70 9023 code = Runtime::PRINTSTRING;
e440a328 9024 else if (type->integer_type() != NULL
9025 && type->integer_type()->is_unsigned())
9026 {
e440a328 9027 Type* itype = Type::lookup_integer_type("uint64");
2c809f8f 9028 arg = Expression::make_cast(itype, arg, location);
88b03a70 9029 code = Runtime::PRINTUINT;
e440a328 9030 }
9031 else if (type->integer_type() != NULL)
9032 {
e440a328 9033 Type* itype = Type::lookup_integer_type("int64");
2c809f8f 9034 arg = Expression::make_cast(itype, arg, location);
88b03a70 9035 code = Runtime::PRINTINT;
e440a328 9036 }
9037 else if (type->float_type() != NULL)
9038 {
2c809f8f 9039 Type* dtype = Type::lookup_float_type("float64");
9040 arg = Expression::make_cast(dtype, arg, location);
88b03a70 9041 code = Runtime::PRINTFLOAT;
e440a328 9042 }
9043 else if (type->complex_type() != NULL)
9044 {
2c809f8f 9045 Type* ctype = Type::lookup_complex_type("complex128");
9046 arg = Expression::make_cast(ctype, arg, location);
88b03a70 9047 code = Runtime::PRINTCOMPLEX;
e440a328 9048 }
9049 else if (type->is_boolean_type())
88b03a70 9050 code = Runtime::PRINTBOOL;
e440a328 9051 else if (type->points_to() != NULL
9052 || type->channel_type() != NULL
9053 || type->map_type() != NULL
9054 || type->function_type() != NULL)
9055 {
2c809f8f 9056 arg = Expression::make_cast(type, arg, location);
88b03a70 9057 code = Runtime::PRINTPOINTER;
e440a328 9058 }
9059 else if (type->interface_type() != NULL)
9060 {
9061 if (type->interface_type()->is_empty())
88b03a70 9062 code = Runtime::PRINTEFACE;
e440a328 9063 else
88b03a70 9064 code = Runtime::PRINTIFACE;
e440a328 9065 }
411eb89e 9066 else if (type->is_slice_type())
88b03a70 9067 code = Runtime::PRINTSLICE;
e440a328 9068 else
cd238b8d 9069 {
9070 go_assert(saw_errors());
ea664253 9071 return context->backend()->error_expression();
cd238b8d 9072 }
e440a328 9073
2c809f8f 9074 Expression* call = Runtime::make_call(code, location, 1, arg);
88b03a70 9075 print_stmts = Expression::make_compound(print_stmts, call,
9076 location);
e440a328 9077 }
9078 }
9079
9080 if (is_ln)
9081 {
2c809f8f 9082 Expression* print_nl =
88b03a70 9083 Runtime::make_call(Runtime::PRINTNL, location, 0);
9084 print_stmts = Expression::make_compound(print_stmts, print_nl,
9085 location);
e440a328 9086 }
9087
88b03a70 9088 Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK,
9089 location, 0);
9090 print_stmts = Expression::make_compound(print_stmts, unlock, location);
32e3ff69 9091
ea664253 9092 return print_stmts->get_backend(context);
e440a328 9093 }
9094
9095 case BUILTIN_PANIC:
9096 {
9097 const Expression_list* args = this->args();
c484d925 9098 go_assert(args != NULL && args->size() == 1);
e440a328 9099 Expression* arg = args->front();
b13c66cd 9100 Type *empty =
823c7e3d 9101 Type::make_empty_interface_type(Linemap::predeclared_location());
2c809f8f 9102 arg = Expression::convert_for_assignment(gogo, empty, arg, location);
9103
9104 Expression* panic =
03ac9de4 9105 Runtime::make_call(Runtime::GOPANIC, location, 1, arg);
ea664253 9106 return panic->get_backend(context);
e440a328 9107 }
9108
9109 case BUILTIN_RECOVER:
9110 {
9111 // The argument is set when building recover thunks. It's a
9112 // boolean value which is true if we can recover a value now.
9113 const Expression_list* args = this->args();
c484d925 9114 go_assert(args != NULL && args->size() == 1);
e440a328 9115 Expression* arg = args->front();
b13c66cd 9116 Type *empty =
823c7e3d 9117 Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 9118
e440a328 9119 Expression* nil = Expression::make_nil(location);
2c809f8f 9120 nil = Expression::convert_for_assignment(gogo, empty, nil, location);
e440a328 9121
9122 // We need to handle a deferred call to recover specially,
9123 // because it changes whether it can recover a panic or not.
9124 // See test7 in test/recover1.go.
2c809f8f 9125 Expression* recover = Runtime::make_call((this->is_deferred()
03ac9de4 9126 ? Runtime::DEFERREDRECOVER
9127 : Runtime::GORECOVER),
2c809f8f 9128 location, 0);
9129 Expression* cond =
9130 Expression::make_conditional(arg, recover, nil, location);
ea664253 9131 return cond->get_backend(context);
e440a328 9132 }
9133
9134 case BUILTIN_CLOSE:
e440a328 9135 {
9136 const Expression_list* args = this->args();
c484d925 9137 go_assert(args != NULL && args->size() == 1);
e440a328 9138 Expression* arg = args->front();
2c809f8f 9139 Expression* close = Runtime::make_call(Runtime::CLOSE, location,
9140 1, arg);
ea664253 9141 return close->get_backend(context);
e440a328 9142 }
9143
9144 case BUILTIN_SIZEOF:
9145 case BUILTIN_OFFSETOF:
9146 case BUILTIN_ALIGNOF:
9147 {
0c77715b 9148 Numeric_constant nc;
9149 unsigned long val;
9150 if (!this->numeric_constant_value(&nc)
9151 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
7f1d9abd 9152 {
c484d925 9153 go_assert(saw_errors());
ea664253 9154 return context->backend()->error_expression();
7f1d9abd 9155 }
7ba86326 9156 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2c809f8f 9157 mpz_t ival;
9158 nc.get_int(&ival);
9159 Expression* int_cst =
e67508fa 9160 Expression::make_integer_z(&ival, uintptr_type, location);
2c809f8f 9161 mpz_clear(ival);
ea664253 9162 return int_cst->get_backend(context);
e440a328 9163 }
9164
9165 case BUILTIN_COPY:
9166 {
9167 const Expression_list* args = this->args();
c484d925 9168 go_assert(args != NULL && args->size() == 2);
e440a328 9169 Expression* arg1 = args->front();
9170 Expression* arg2 = args->back();
9171
e440a328 9172 Type* arg1_type = arg1->type();
9173 Array_type* at = arg1_type->array_type();
35a54f17 9174 go_assert(arg1->is_variable());
321e5ad2 9175
9176 Expression* call;
e440a328 9177
9178 Type* arg2_type = arg2->type();
2c809f8f 9179 go_assert(arg2->is_variable());
321e5ad2 9180 if (arg2_type->is_string_type())
9181 call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location,
9182 2, arg1, arg2);
e440a328 9183 else
9184 {
321e5ad2 9185 Type* et = at->element_type();
9186 if (et->has_pointer())
9187 {
9188 Expression* td = Expression::make_type_descriptor(et,
9189 location);
9190 call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location,
9191 3, td, arg1, arg2);
9192 }
9193 else
9194 {
9195 Expression* sz = Expression::make_type_info(et,
9196 TYPE_INFO_SIZE);
9197 call = Runtime::make_call(Runtime::SLICECOPY, location, 3,
9198 arg1, arg2, sz);
9199 }
e440a328 9200 }
2c809f8f 9201
321e5ad2 9202 return call->get_backend(context);
e440a328 9203 }
9204
9205 case BUILTIN_APPEND:
321e5ad2 9206 // Handled in Builtin_call_expression::flatten_append.
9207 go_unreachable();
e440a328 9208
9209 case BUILTIN_REAL:
9210 case BUILTIN_IMAG:
9211 {
9212 const Expression_list* args = this->args();
c484d925 9213 go_assert(args != NULL && args->size() == 1);
2c809f8f 9214
9215 Bexpression* ret;
ea664253 9216 Bexpression* bcomplex = args->front()->get_backend(context);
2c809f8f 9217 if (this->code_ == BUILTIN_REAL)
9218 ret = gogo->backend()->real_part_expression(bcomplex, location);
9219 else
9220 ret = gogo->backend()->imag_part_expression(bcomplex, location);
ea664253 9221 return ret;
e440a328 9222 }
9223
48080209 9224 case BUILTIN_COMPLEX:
e440a328 9225 {
9226 const Expression_list* args = this->args();
c484d925 9227 go_assert(args != NULL && args->size() == 2);
ea664253 9228 Bexpression* breal = args->front()->get_backend(context);
9229 Bexpression* bimag = args->back()->get_backend(context);
9230 return gogo->backend()->complex_expression(breal, bimag, location);
e440a328 9231 }
9232
9233 default:
c3e6f413 9234 go_unreachable();
e440a328 9235 }
9236}
9237
9238// We have to support exporting a builtin call expression, because
9239// code can set a constant to the result of a builtin expression.
9240
9241void
9242Builtin_call_expression::do_export(Export* exp) const
9243{
0c77715b 9244 Numeric_constant nc;
9245 if (!this->numeric_constant_value(&nc))
9246 {
631d5788 9247 go_error_at(this->location(), "value is not constant");
0c77715b 9248 return;
9249 }
e440a328 9250
0c77715b 9251 if (nc.is_int())
e440a328 9252 {
0c77715b 9253 mpz_t val;
9254 nc.get_int(&val);
e440a328 9255 Integer_expression::export_integer(exp, val);
0c77715b 9256 mpz_clear(val);
e440a328 9257 }
0c77715b 9258 else if (nc.is_float())
e440a328 9259 {
9260 mpfr_t fval;
0c77715b 9261 nc.get_float(&fval);
9262 Float_expression::export_float(exp, fval);
e440a328 9263 mpfr_clear(fval);
9264 }
0c77715b 9265 else if (nc.is_complex())
e440a328 9266 {
fcbea5e4 9267 mpc_t cval;
9268 nc.get_complex(&cval);
9269 Complex_expression::export_complex(exp, cval);
9270 mpc_clear(cval);
e440a328 9271 }
0c77715b 9272 else
9273 go_unreachable();
e440a328 9274
9275 // A trailing space lets us reliably identify the end of the number.
9276 exp->write_c_string(" ");
9277}
9278
9279// Class Call_expression.
9280
8381eda7 9281// A Go function can be viewed in a couple of different ways. The
9282// code of a Go function becomes a backend function with parameters
9283// whose types are simply the backend representation of the Go types.
9284// If there are multiple results, they are returned as a backend
9285// struct.
9286
9287// However, when Go code refers to a function other than simply
9288// calling it, the backend type of that function is actually a struct.
9289// The first field of the struct points to the Go function code
9290// (sometimes a wrapper as described below). The remaining fields
9291// hold addresses of closed-over variables. This struct is called a
9292// closure.
9293
9294// There are a few cases to consider.
9295
9296// A direct function call of a known function in package scope. In
9297// this case there are no closed-over variables, and we know the name
9298// of the function code. We can simply produce a backend call to the
9299// function directly, and not worry about the closure.
9300
9301// A direct function call of a known function literal. In this case
9302// we know the function code and we know the closure. We generate the
9303// function code such that it expects an additional final argument of
9304// the closure type. We pass the closure as the last argument, after
9305// the other arguments.
9306
9307// An indirect function call. In this case we have a closure. We
9308// load the pointer to the function code from the first field of the
9309// closure. We pass the address of the closure as the last argument.
9310
9311// A call to a method of an interface. Type methods are always at
9312// package scope, so we call the function directly, and don't worry
9313// about the closure.
9314
9315// This means that for a function at package scope we have two cases.
9316// One is the direct call, which has no closure. The other is the
9317// indirect call, which does have a closure. We can't simply ignore
9318// the closure, even though it is the last argument, because that will
9319// fail on targets where the function pops its arguments. So when
9320// generating a closure for a package-scope function we set the
9321// function code pointer in the closure to point to a wrapper
9322// function. This wrapper function accepts a final argument that
9323// points to the closure, ignores it, and calls the real function as a
9324// direct function call. This wrapper will normally be efficient, and
9325// can often simply be a tail call to the real function.
9326
9327// We don't use GCC's static chain pointer because 1) we don't need
9328// it; 2) GCC only permits using a static chain to call a known
9329// function, so we can't use it for an indirect call anyhow. Since we
9330// can't use it for an indirect call, we may as well not worry about
9331// using it for a direct call either.
9332
9333// We pass the closure last rather than first because it means that
9334// the function wrapper we put into a closure for a package-scope
9335// function can normally just be a tail call to the real function.
9336
9337// For method expressions we generate a wrapper that loads the
9338// receiver from the closure and then calls the method. This
9339// unfortunately forces reshuffling the arguments, since there is a
9340// new first argument, but we can't avoid reshuffling either for
9341// method expressions or for indirect calls of package-scope
9342// functions, and since the latter are more common we reshuffle for
9343// method expressions.
9344
9345// Note that the Go code retains the Go types. The extra final
9346// argument only appears when we convert to the backend
9347// representation.
9348
e440a328 9349// Traversal.
9350
9351int
9352Call_expression::do_traverse(Traverse* traverse)
9353{
0c0dacab 9354 // If we are calling a function in a different package that returns
9355 // an unnamed type, this may be the only chance we get to traverse
9356 // that type. We don't traverse this->type_ because it may be a
9357 // Call_multiple_result_type that will just lead back here.
9358 if (this->type_ != NULL && !this->type_->is_error_type())
9359 {
9360 Function_type *fntype = this->get_function_type();
9361 if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT)
9362 return TRAVERSE_EXIT;
9363 }
e440a328 9364 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9365 return TRAVERSE_EXIT;
9366 if (this->args_ != NULL)
9367 {
9368 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9369 return TRAVERSE_EXIT;
9370 }
9371 return TRAVERSE_CONTINUE;
9372}
9373
9374// Lower a call statement.
9375
9376Expression*
ceeb4318 9377Call_expression::do_lower(Gogo* gogo, Named_object* function,
9378 Statement_inserter* inserter, int)
e440a328 9379{
b13c66cd 9380 Location loc = this->location();
09ea332d 9381
ceeb4318 9382 // A type cast can look like a function call.
e440a328 9383 if (this->fn_->is_type_expression()
9384 && this->args_ != NULL
9385 && this->args_->size() == 1)
9386 return Expression::make_cast(this->fn_->type(), this->args_->front(),
09ea332d 9387 loc);
e440a328 9388
88f06749 9389 // Because do_type will return an error type and thus prevent future
9390 // errors, check for that case now to ensure that the error gets
9391 // reported.
37448b10 9392 Function_type* fntype = this->get_function_type();
9393 if (fntype == NULL)
88f06749 9394 {
9395 if (!this->fn_->type()->is_error())
9396 this->report_error(_("expected function"));
5f1045b5 9397 this->set_is_error();
9398 return this;
88f06749 9399 }
9400
e440a328 9401 // Handle an argument which is a call to a function which returns
9402 // multiple results.
9403 if (this->args_ != NULL
9404 && this->args_->size() == 1
37448b10 9405 && this->args_->front()->call_expression() != NULL)
e440a328 9406 {
e440a328 9407 size_t rc = this->args_->front()->call_expression()->result_count();
9408 if (rc > 1
37448b10 9409 && ((fntype->parameters() != NULL
9410 && (fntype->parameters()->size() == rc
9411 || (fntype->is_varargs()
9412 && fntype->parameters()->size() - 1 <= rc)))
9413 || fntype->is_builtin()))
e440a328 9414 {
9415 Call_expression* call = this->args_->front()->call_expression();
e90ecd2d 9416 call->set_is_multi_value_arg();
c33af8e4 9417 if (this->is_varargs_)
9418 {
9419 // It is not clear which result of a multiple result call
9420 // the ellipsis operator should be applied to. If we unpack the
9421 // the call into its individual results here, the ellipsis will be
9422 // applied to the last result.
631d5788 9423 go_error_at(call->location(),
9424 _("multiple-value argument in single-value context"));
c33af8e4 9425 return Expression::make_error(call->location());
9426 }
9427
e440a328 9428 Expression_list* args = new Expression_list;
9429 for (size_t i = 0; i < rc; ++i)
9430 args->push_back(Expression::make_call_result(call, i));
9431 // We can't return a new call expression here, because this
42535814 9432 // one may be referenced by Call_result expressions. We
9433 // also can't delete the old arguments, because we may still
9434 // traverse them somewhere up the call stack. FIXME.
e440a328 9435 this->args_ = args;
9436 }
9437 }
9438
37448b10 9439 // Recognize a call to a builtin function.
9440 if (fntype->is_builtin())
9441 return new Builtin_call_expression(gogo, this->fn_, this->args_,
9442 this->is_varargs_, loc);
9443
ceeb4318 9444 // If this call returns multiple results, create a temporary
9445 // variable for each result.
9446 size_t rc = this->result_count();
9447 if (rc > 1 && this->results_ == NULL)
9448 {
9449 std::vector<Temporary_statement*>* temps =
9450 new std::vector<Temporary_statement*>;
9451 temps->reserve(rc);
37448b10 9452 const Typed_identifier_list* results = fntype->results();
ceeb4318 9453 for (Typed_identifier_list::const_iterator p = results->begin();
9454 p != results->end();
9455 ++p)
9456 {
9457 Temporary_statement* temp = Statement::make_temporary(p->type(),
09ea332d 9458 NULL, loc);
ceeb4318 9459 inserter->insert(temp);
9460 temps->push_back(temp);
9461 }
9462 this->results_ = temps;
9463 }
9464
e440a328 9465 // Handle a call to a varargs function by packaging up the extra
9466 // parameters.
37448b10 9467 if (fntype->is_varargs())
e440a328 9468 {
e440a328 9469 const Typed_identifier_list* parameters = fntype->parameters();
c484d925 9470 go_assert(parameters != NULL && !parameters->empty());
e440a328 9471 Type* varargs_type = parameters->back().type();
09ea332d 9472 this->lower_varargs(gogo, function, inserter, varargs_type,
0e9a2e72 9473 parameters->size(), SLICE_STORAGE_MAY_ESCAPE);
09ea332d 9474 }
9475
9476 // If this is call to a method, call the method directly passing the
9477 // object as the first parameter.
9478 Bound_method_expression* bme = this->fn_->bound_method_expression();
9479 if (bme != NULL)
9480 {
0afbb937 9481 Named_object* methodfn = bme->function();
09ea332d 9482 Expression* first_arg = bme->first_argument();
9483
9484 // We always pass a pointer when calling a method.
9485 if (first_arg->type()->points_to() == NULL
9486 && !first_arg->type()->is_error())
9487 {
9488 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9489 // We may need to create a temporary variable so that we can
9490 // take the address. We can't do that here because it will
9491 // mess up the order of evaluation.
9492 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9493 ue->set_create_temp();
9494 }
9495
9496 // If we are calling a method which was inherited from an
9497 // embedded struct, and the method did not get a stub, then the
9498 // first type may be wrong.
9499 Type* fatype = bme->first_argument_type();
9500 if (fatype != NULL)
9501 {
9502 if (fatype->points_to() == NULL)
9503 fatype = Type::make_pointer_type(fatype);
9504 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9505 }
9506
9507 Expression_list* new_args = new Expression_list();
9508 new_args->push_back(first_arg);
9509 if (this->args_ != NULL)
9510 {
9511 for (Expression_list::const_iterator p = this->args_->begin();
9512 p != this->args_->end();
9513 ++p)
9514 new_args->push_back(*p);
9515 }
9516
9517 // We have to change in place because this structure may be
9518 // referenced by Call_result_expressions. We can't delete the
9519 // old arguments, because we may be traversing them up in some
9520 // caller. FIXME.
9521 this->args_ = new_args;
0afbb937 9522 this->fn_ = Expression::make_func_reference(methodfn, NULL,
09ea332d 9523 bme->location());
e440a328 9524 }
9525
105f9a24 9526 // Handle a couple of special runtime functions. In the runtime
9527 // package, getcallerpc returns the PC of the caller, and
9528 // getcallersp returns the frame pointer of the caller. Implement
9529 // these by turning them into calls to GCC builtin functions. We
9530 // could implement them in normal code, but then we would have to
9531 // explicitly unwind the stack. These functions are intended to be
9532 // efficient. Note that this technique obviously only works for
9533 // direct calls, but that is the only way they are used. The actual
9534 // argument to these functions is always the address of a parameter;
9535 // we don't need that for the GCC builtin functions, so we just
9536 // ignore it.
9537 if (gogo->compiling_runtime()
9538 && this->args_ != NULL
9539 && this->args_->size() == 1
9540 && gogo->package_name() == "runtime")
9541 {
9542 Func_expression* fe = this->fn_->func_expression();
9543 if (fe != NULL
9544 && fe->named_object()->is_function_declaration()
9545 && fe->named_object()->package() == NULL)
9546 {
9547 std::string n = Gogo::unpack_hidden_name(fe->named_object()->name());
9548 if (n == "getcallerpc")
9549 {
9550 static Named_object* builtin_return_address;
9551 return this->lower_to_builtin(&builtin_return_address,
9552 "__builtin_return_address",
9553 0);
9554 }
9555 else if (n == "getcallersp")
9556 {
9557 static Named_object* builtin_frame_address;
9558 return this->lower_to_builtin(&builtin_frame_address,
9559 "__builtin_frame_address",
9560 1);
9561 }
9562 }
9563 }
9564
e440a328 9565 return this;
9566}
9567
9568// Lower a call to a varargs function. FUNCTION is the function in
9569// which the call occurs--it's not the function we are calling.
9570// VARARGS_TYPE is the type of the varargs parameter, a slice type.
9571// PARAM_COUNT is the number of parameters of the function we are
9572// calling; the last of these parameters will be the varargs
9573// parameter.
9574
09ea332d 9575void
e440a328 9576Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
ceeb4318 9577 Statement_inserter* inserter,
0e9a2e72 9578 Type* varargs_type, size_t param_count,
9579 Slice_storage_escape_disp escape_disp)
e440a328 9580{
03118c21 9581 // When compiling the runtime, varargs slices do not escape. When
9582 // escape analysis becomes the default, this should be changed to
9583 // make it an error if we have a varargs slice that escapes.
9584 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
9585 escape_disp = SLICE_STORAGE_DOES_NOT_ESCAPE;
9586
e440a328 9587 if (this->varargs_are_lowered_)
09ea332d 9588 return;
e440a328 9589
b13c66cd 9590 Location loc = this->location();
e440a328 9591
c484d925 9592 go_assert(param_count > 0);
411eb89e 9593 go_assert(varargs_type->is_slice_type());
e440a328 9594
9595 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9596 if (arg_count < param_count - 1)
9597 {
9598 // Not enough arguments; will be caught in check_types.
09ea332d 9599 return;
e440a328 9600 }
9601
9602 Expression_list* old_args = this->args_;
9603 Expression_list* new_args = new Expression_list();
9604 bool push_empty_arg = false;
9605 if (old_args == NULL || old_args->empty())
9606 {
c484d925 9607 go_assert(param_count == 1);
e440a328 9608 push_empty_arg = true;
9609 }
9610 else
9611 {
9612 Expression_list::const_iterator pa;
9613 int i = 1;
9614 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9615 {
9616 if (static_cast<size_t>(i) == param_count)
9617 break;
9618 new_args->push_back(*pa);
9619 }
9620
9621 // We have reached the varargs parameter.
9622
9623 bool issued_error = false;
9624 if (pa == old_args->end())
9625 push_empty_arg = true;
9626 else if (pa + 1 == old_args->end() && this->is_varargs_)
9627 new_args->push_back(*pa);
9628 else if (this->is_varargs_)
9629 {
a6645f74 9630 if ((*pa)->type()->is_slice_type())
9631 this->report_error(_("too many arguments"));
9632 else
9633 {
631d5788 9634 go_error_at(this->location(),
9635 _("invalid use of %<...%> with non-slice"));
a6645f74 9636 this->set_is_error();
9637 }
09ea332d 9638 return;
e440a328 9639 }
e440a328 9640 else
9641 {
9642 Type* element_type = varargs_type->array_type()->element_type();
9643 Expression_list* vals = new Expression_list;
9644 for (; pa != old_args->end(); ++pa, ++i)
9645 {
9646 // Check types here so that we get a better message.
9647 Type* patype = (*pa)->type();
b13c66cd 9648 Location paloc = (*pa)->location();
e440a328 9649 if (!this->check_argument_type(i, element_type, patype,
9650 paloc, issued_error))
9651 continue;
9652 vals->push_back(*pa);
9653 }
0e9a2e72 9654 Slice_construction_expression* sce =
e440a328 9655 Expression::make_slice_composite_literal(varargs_type, vals, loc);
0e9a2e72 9656 if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE)
9657 sce->set_storage_does_not_escape();
9658 Expression* val = sce;
09ea332d 9659 gogo->lower_expression(function, inserter, &val);
e440a328 9660 new_args->push_back(val);
9661 }
9662 }
9663
9664 if (push_empty_arg)
9665 new_args->push_back(Expression::make_nil(loc));
9666
9667 // We can't return a new call expression here, because this one may
6d4c2432 9668 // be referenced by Call_result expressions. FIXME. We can't
9669 // delete OLD_ARGS because we may have both a Call_expression and a
9670 // Builtin_call_expression which refer to them. FIXME.
e440a328 9671 this->args_ = new_args;
9672 this->varargs_are_lowered_ = true;
e440a328 9673}
9674
105f9a24 9675// Return a call to __builtin_return_address or __builtin_frame_address.
9676
9677Expression*
9678Call_expression::lower_to_builtin(Named_object** pno, const char* name,
9679 int arg)
9680{
9681 if (*pno == NULL)
9682 *pno = Gogo::declare_builtin_rf_address(name);
9683
9684 Location loc = this->location();
9685
9686 Expression* fn = Expression::make_func_reference(*pno, NULL, loc);
9687 Expression* a = Expression::make_integer_ul(arg, NULL, loc);
9688 Expression_list *args = new Expression_list();
9689 args->push_back(a);
9690 Expression* call = Expression::make_call(fn, args, false, loc);
9691
9692 // The builtin functions return void*, but the Go functions return uintptr.
9693 Type* uintptr_type = Type::lookup_integer_type("uintptr");
9694 return Expression::make_cast(uintptr_type, call, loc);
9695}
9696
2c809f8f 9697// Flatten a call with multiple results into a temporary.
9698
9699Expression*
b8e86a51 9700Call_expression::do_flatten(Gogo* gogo, Named_object*,
9701 Statement_inserter* inserter)
2c809f8f 9702{
5bf8be8b 9703 if (this->is_erroneous_call())
9704 {
9705 go_assert(saw_errors());
9706 return Expression::make_error(this->location());
9707 }
b8e86a51 9708
91c0fd76 9709 if (this->is_flattened_)
9710 return this;
9711 this->is_flattened_ = true;
9712
b8e86a51 9713 // Add temporary variables for all arguments that require type
9714 // conversion.
9715 Function_type* fntype = this->get_function_type();
9782d556 9716 if (fntype == NULL)
9717 {
9718 go_assert(saw_errors());
9719 return this;
9720 }
b8e86a51 9721 if (this->args_ != NULL && !this->args_->empty()
9722 && fntype->parameters() != NULL && !fntype->parameters()->empty())
9723 {
9724 bool is_interface_method =
9725 this->fn_->interface_field_reference_expression() != NULL;
9726
9727 Expression_list *args = new Expression_list();
9728 Typed_identifier_list::const_iterator pp = fntype->parameters()->begin();
9729 Expression_list::const_iterator pa = this->args_->begin();
9730 if (!is_interface_method && fntype->is_method())
9731 {
9732 // The receiver argument.
9733 args->push_back(*pa);
9734 ++pa;
9735 }
9736 for (; pa != this->args_->end(); ++pa, ++pp)
9737 {
9738 go_assert(pp != fntype->parameters()->end());
9739 if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL))
9740 args->push_back(*pa);
9741 else
9742 {
9743 Location loc = (*pa)->location();
8ba8cc87 9744 Expression* arg = *pa;
9745 if (!arg->is_variable())
9746 {
9747 Temporary_statement *temp =
9748 Statement::make_temporary(NULL, arg, loc);
9749 inserter->insert(temp);
9750 arg = Expression::make_temporary_reference(temp, loc);
9751 }
9752 arg = Expression::convert_for_assignment(gogo, pp->type(), arg,
9753 loc);
9754 args->push_back(arg);
b8e86a51 9755 }
9756 }
9757 delete this->args_;
9758 this->args_ = args;
9759 }
9760
2c809f8f 9761 size_t rc = this->result_count();
9762 if (rc > 1 && this->call_temp_ == NULL)
9763 {
9764 Struct_field_list* sfl = new Struct_field_list();
9765 Function_type* fntype = this->get_function_type();
9766 const Typed_identifier_list* results = fntype->results();
9767 Location loc = this->location();
9768
9769 int i = 0;
61575e0f 9770 char buf[20];
2c809f8f 9771 for (Typed_identifier_list::const_iterator p = results->begin();
9772 p != results->end();
9773 ++p, ++i)
9774 {
9775 snprintf(buf, sizeof buf, "res%d", i);
9776 sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc)));
9777 }
9778
9779 Struct_type* st = Type::make_struct_type(sfl, loc);
6bf4793c 9780 st->set_is_struct_incomparable();
2c809f8f 9781 this->call_temp_ = Statement::make_temporary(st, NULL, loc);
9782 inserter->insert(this->call_temp_);
9783 }
9784
9785 return this;
9786}
9787
ceeb4318 9788// Get the function type. This can return NULL in error cases.
e440a328 9789
9790Function_type*
9791Call_expression::get_function_type() const
9792{
9793 return this->fn_->type()->function_type();
9794}
9795
9796// Return the number of values which this call will return.
9797
9798size_t
9799Call_expression::result_count() const
9800{
9801 const Function_type* fntype = this->get_function_type();
9802 if (fntype == NULL)
9803 return 0;
9804 if (fntype->results() == NULL)
9805 return 0;
9806 return fntype->results()->size();
9807}
9808
ceeb4318 9809// Return the temporary which holds a result.
9810
9811Temporary_statement*
9812Call_expression::result(size_t i) const
9813{
cd238b8d 9814 if (this->results_ == NULL || this->results_->size() <= i)
9815 {
9816 go_assert(saw_errors());
9817 return NULL;
9818 }
ceeb4318 9819 return (*this->results_)[i];
9820}
9821
1373401e 9822// Set the number of results expected from a call expression.
9823
9824void
9825Call_expression::set_expected_result_count(size_t count)
9826{
9827 go_assert(this->expected_result_count_ == 0);
9828 this->expected_result_count_ = count;
9829}
9830
e440a328 9831// Return whether this is a call to the predeclared function recover.
9832
9833bool
9834Call_expression::is_recover_call() const
9835{
9836 return this->do_is_recover_call();
9837}
9838
9839// Set the argument to the recover function.
9840
9841void
9842Call_expression::set_recover_arg(Expression* arg)
9843{
9844 this->do_set_recover_arg(arg);
9845}
9846
9847// Virtual functions also implemented by Builtin_call_expression.
9848
9849bool
9850Call_expression::do_is_recover_call() const
9851{
9852 return false;
9853}
9854
9855void
9856Call_expression::do_set_recover_arg(Expression*)
9857{
c3e6f413 9858 go_unreachable();
e440a328 9859}
9860
ceeb4318 9861// We have found an error with this call expression; return true if
9862// we should report it.
9863
9864bool
9865Call_expression::issue_error()
9866{
9867 if (this->issued_error_)
9868 return false;
9869 else
9870 {
9871 this->issued_error_ = true;
9872 return true;
9873 }
9874}
9875
5bf8be8b 9876// Whether or not this call contains errors, either in the call or the
9877// arguments to the call.
9878
9879bool
9880Call_expression::is_erroneous_call()
9881{
9882 if (this->is_error_expression() || this->fn()->is_error_expression())
9883 return true;
9884
9885 if (this->args() == NULL)
9886 return false;
9887 for (Expression_list::iterator pa = this->args()->begin();
9888 pa != this->args()->end();
9889 ++pa)
9890 {
9891 if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression())
9892 return true;
9893 }
9894 return false;
9895}
9896
e440a328 9897// Get the type.
9898
9899Type*
9900Call_expression::do_type()
9901{
9902 if (this->type_ != NULL)
9903 return this->type_;
9904
9905 Type* ret;
9906 Function_type* fntype = this->get_function_type();
9907 if (fntype == NULL)
9908 return Type::make_error_type();
9909
9910 const Typed_identifier_list* results = fntype->results();
9911 if (results == NULL)
9912 ret = Type::make_void_type();
9913 else if (results->size() == 1)
9914 ret = results->begin()->type();
9915 else
9916 ret = Type::make_call_multiple_result_type(this);
9917
9918 this->type_ = ret;
9919
9920 return this->type_;
9921}
9922
9923// Determine types for a call expression. We can use the function
9924// parameter types to set the types of the arguments.
9925
9926void
9927Call_expression::do_determine_type(const Type_context*)
9928{
fb94b0ca 9929 if (!this->determining_types())
9930 return;
9931
e440a328 9932 this->fn_->determine_type_no_context();
9933 Function_type* fntype = this->get_function_type();
9934 const Typed_identifier_list* parameters = NULL;
9935 if (fntype != NULL)
9936 parameters = fntype->parameters();
9937 if (this->args_ != NULL)
9938 {
9939 Typed_identifier_list::const_iterator pt;
9940 if (parameters != NULL)
9941 pt = parameters->begin();
09ea332d 9942 bool first = true;
e440a328 9943 for (Expression_list::const_iterator pa = this->args_->begin();
9944 pa != this->args_->end();
9945 ++pa)
9946 {
09ea332d 9947 if (first)
9948 {
9949 first = false;
9950 // If this is a method, the first argument is the
9951 // receiver.
9952 if (fntype != NULL && fntype->is_method())
9953 {
9954 Type* rtype = fntype->receiver()->type();
9955 // The receiver is always passed as a pointer.
9956 if (rtype->points_to() == NULL)
9957 rtype = Type::make_pointer_type(rtype);
9958 Type_context subcontext(rtype, false);
9959 (*pa)->determine_type(&subcontext);
9960 continue;
9961 }
9962 }
9963
e440a328 9964 if (parameters != NULL && pt != parameters->end())
9965 {
9966 Type_context subcontext(pt->type(), false);
9967 (*pa)->determine_type(&subcontext);
9968 ++pt;
9969 }
9970 else
9971 (*pa)->determine_type_no_context();
9972 }
9973 }
9974}
9975
fb94b0ca 9976// Called when determining types for a Call_expression. Return true
9977// if we should go ahead, false if they have already been determined.
9978
9979bool
9980Call_expression::determining_types()
9981{
9982 if (this->types_are_determined_)
9983 return false;
9984 else
9985 {
9986 this->types_are_determined_ = true;
9987 return true;
9988 }
9989}
9990
e440a328 9991// Check types for parameter I.
9992
9993bool
9994Call_expression::check_argument_type(int i, const Type* parameter_type,
9995 const Type* argument_type,
b13c66cd 9996 Location argument_location,
e440a328 9997 bool issued_error)
9998{
9999 std::string reason;
1eae365b 10000 if (!Type::are_assignable(parameter_type, argument_type, &reason))
e440a328 10001 {
10002 if (!issued_error)
10003 {
10004 if (reason.empty())
631d5788 10005 go_error_at(argument_location, "argument %d has incompatible type", i);
e440a328 10006 else
631d5788 10007 go_error_at(argument_location,
10008 "argument %d has incompatible type (%s)",
10009 i, reason.c_str());
e440a328 10010 }
10011 this->set_is_error();
10012 return false;
10013 }
10014 return true;
10015}
10016
10017// Check types.
10018
10019void
10020Call_expression::do_check_types(Gogo*)
10021{
a6645f74 10022 if (this->classification() == EXPRESSION_ERROR)
10023 return;
10024
e440a328 10025 Function_type* fntype = this->get_function_type();
10026 if (fntype == NULL)
10027 {
5c13bd80 10028 if (!this->fn_->type()->is_error())
e440a328 10029 this->report_error(_("expected function"));
10030 return;
10031 }
10032
1373401e 10033 if (this->expected_result_count_ != 0
10034 && this->expected_result_count_ != this->result_count())
10035 {
10036 if (this->issue_error())
10037 this->report_error(_("function result count mismatch"));
10038 this->set_is_error();
10039 return;
10040 }
10041
09ea332d 10042 bool is_method = fntype->is_method();
10043 if (is_method)
e440a328 10044 {
09ea332d 10045 go_assert(this->args_ != NULL && !this->args_->empty());
10046 Type* rtype = fntype->receiver()->type();
10047 Expression* first_arg = this->args_->front();
1eae365b 10048 // We dereference the values since receivers are always passed
10049 // as pointers.
09ea332d 10050 std::string reason;
1eae365b 10051 if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(),
10052 &reason))
e440a328 10053 {
09ea332d 10054 if (reason.empty())
10055 this->report_error(_("incompatible type for receiver"));
10056 else
e440a328 10057 {
631d5788 10058 go_error_at(this->location(),
10059 "incompatible type for receiver (%s)",
10060 reason.c_str());
09ea332d 10061 this->set_is_error();
e440a328 10062 }
10063 }
10064 }
10065
10066 // Note that varargs was handled by the lower_varargs() method, so
a6645f74 10067 // we don't have to worry about it here unless something is wrong.
10068 if (this->is_varargs_ && !this->varargs_are_lowered_)
10069 {
10070 if (!fntype->is_varargs())
10071 {
631d5788 10072 go_error_at(this->location(),
10073 _("invalid use of %<...%> calling non-variadic function"));
a6645f74 10074 this->set_is_error();
10075 return;
10076 }
10077 }
e440a328 10078
10079 const Typed_identifier_list* parameters = fntype->parameters();
10080 if (this->args_ == NULL)
10081 {
10082 if (parameters != NULL && !parameters->empty())
10083 this->report_error(_("not enough arguments"));
10084 }
10085 else if (parameters == NULL)
09ea332d 10086 {
10087 if (!is_method || this->args_->size() > 1)
10088 this->report_error(_("too many arguments"));
10089 }
1373401e 10090 else if (this->args_->size() == 1
10091 && this->args_->front()->call_expression() != NULL
10092 && this->args_->front()->call_expression()->result_count() > 1)
10093 {
10094 // This is F(G()) when G returns more than one result. If the
10095 // results can be matched to parameters, it would have been
10096 // lowered in do_lower. If we get here we know there is a
10097 // mismatch.
10098 if (this->args_->front()->call_expression()->result_count()
10099 < parameters->size())
10100 this->report_error(_("not enough arguments"));
10101 else
10102 this->report_error(_("too many arguments"));
10103 }
e440a328 10104 else
10105 {
10106 int i = 0;
09ea332d 10107 Expression_list::const_iterator pa = this->args_->begin();
10108 if (is_method)
10109 ++pa;
10110 for (Typed_identifier_list::const_iterator pt = parameters->begin();
10111 pt != parameters->end();
10112 ++pt, ++pa, ++i)
e440a328 10113 {
09ea332d 10114 if (pa == this->args_->end())
e440a328 10115 {
09ea332d 10116 this->report_error(_("not enough arguments"));
e440a328 10117 return;
10118 }
10119 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
10120 (*pa)->location(), false);
10121 }
09ea332d 10122 if (pa != this->args_->end())
10123 this->report_error(_("too many arguments"));
e440a328 10124 }
10125}
10126
72666aed 10127Expression*
10128Call_expression::do_copy()
10129{
10130 Call_expression* call =
10131 Expression::make_call(this->fn_->copy(),
10132 (this->args_ == NULL
10133 ? NULL
10134 : this->args_->copy()),
10135 this->is_varargs_, this->location());
10136
10137 if (this->varargs_are_lowered_)
10138 call->set_varargs_are_lowered();
10139 return call;
10140}
10141
e440a328 10142// Return whether we have to use a temporary variable to ensure that
10143// we evaluate this call expression in order. If the call returns no
ceeb4318 10144// results then it will inevitably be executed last.
e440a328 10145
10146bool
10147Call_expression::do_must_eval_in_order() const
10148{
ceeb4318 10149 return this->result_count() > 0;
e440a328 10150}
10151
e440a328 10152// Get the function and the first argument to use when calling an
10153// interface method.
10154
2387f644 10155Expression*
e440a328 10156Call_expression::interface_method_function(
e440a328 10157 Interface_field_reference_expression* interface_method,
2387f644 10158 Expression** first_arg_ptr)
e440a328 10159{
2387f644 10160 *first_arg_ptr = interface_method->get_underlying_object();
10161 return interface_method->get_function();
e440a328 10162}
10163
10164// Build the call expression.
10165
ea664253 10166Bexpression*
10167Call_expression::do_get_backend(Translate_context* context)
e440a328 10168{
2c809f8f 10169 if (this->call_ != NULL)
ea664253 10170 return this->call_;
e440a328 10171
10172 Function_type* fntype = this->get_function_type();
10173 if (fntype == NULL)
ea664253 10174 return context->backend()->error_expression();
e440a328 10175
10176 if (this->fn_->is_error_expression())
ea664253 10177 return context->backend()->error_expression();
e440a328 10178
10179 Gogo* gogo = context->gogo();
b13c66cd 10180 Location location = this->location();
e440a328 10181
10182 Func_expression* func = this->fn_->func_expression();
e440a328 10183 Interface_field_reference_expression* interface_method =
10184 this->fn_->interface_field_reference_expression();
10185 const bool has_closure = func != NULL && func->closure() != NULL;
09ea332d 10186 const bool is_interface_method = interface_method != NULL;
e440a328 10187
f8bdf81a 10188 bool has_closure_arg;
8381eda7 10189 if (has_closure)
f8bdf81a 10190 has_closure_arg = true;
8381eda7 10191 else if (func != NULL)
f8bdf81a 10192 has_closure_arg = false;
8381eda7 10193 else if (is_interface_method)
f8bdf81a 10194 has_closure_arg = false;
8381eda7 10195 else
f8bdf81a 10196 has_closure_arg = true;
8381eda7 10197
e440a328 10198 int nargs;
2c809f8f 10199 std::vector<Bexpression*> fn_args;
e440a328 10200 if (this->args_ == NULL || this->args_->empty())
10201 {
f8bdf81a 10202 nargs = is_interface_method ? 1 : 0;
2c809f8f 10203 if (nargs > 0)
10204 fn_args.resize(1);
e440a328 10205 }
09ea332d 10206 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
10207 {
10208 // Passing a receiver parameter.
10209 go_assert(!is_interface_method
10210 && fntype->is_method()
10211 && this->args_->size() == 1);
f8bdf81a 10212 nargs = 1;
2c809f8f 10213 fn_args.resize(1);
ea664253 10214 fn_args[0] = this->args_->front()->get_backend(context);
09ea332d 10215 }
e440a328 10216 else
10217 {
10218 const Typed_identifier_list* params = fntype->parameters();
e440a328 10219
10220 nargs = this->args_->size();
09ea332d 10221 int i = is_interface_method ? 1 : 0;
e440a328 10222 nargs += i;
2c809f8f 10223 fn_args.resize(nargs);
e440a328 10224
10225 Typed_identifier_list::const_iterator pp = params->begin();
09ea332d 10226 Expression_list::const_iterator pe = this->args_->begin();
10227 if (!is_interface_method && fntype->is_method())
10228 {
ea664253 10229 fn_args[i] = (*pe)->get_backend(context);
09ea332d 10230 ++pe;
10231 ++i;
10232 }
10233 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
e440a328 10234 {
c484d925 10235 go_assert(pp != params->end());
2c809f8f 10236 Expression* arg =
10237 Expression::convert_for_assignment(gogo, pp->type(), *pe,
10238 location);
ea664253 10239 fn_args[i] = arg->get_backend(context);
e440a328 10240 }
c484d925 10241 go_assert(pp == params->end());
f8bdf81a 10242 go_assert(i == nargs);
e440a328 10243 }
10244
2c809f8f 10245 Expression* fn;
10246 Expression* closure = NULL;
8381eda7 10247 if (func != NULL)
10248 {
10249 Named_object* no = func->named_object();
2c809f8f 10250 fn = Expression::make_func_code_reference(no, location);
10251 if (has_closure)
10252 closure = func->closure();
8381eda7 10253 }
09ea332d 10254 else if (!is_interface_method)
8381eda7 10255 {
2c809f8f 10256 closure = this->fn_;
10257
10258 // The backend representation of this function type is a pointer
10259 // to a struct whose first field is the actual function to call.
10260 Type* pfntype =
10261 Type::make_pointer_type(
10262 Type::make_pointer_type(Type::make_void_type()));
10263 fn = Expression::make_unsafe_cast(pfntype, this->fn_, location);
10264 fn = Expression::make_unary(OPERATOR_MULT, fn, location);
10265 }
e440a328 10266 else
cf609de4 10267 {
2387f644 10268 Expression* first_arg;
2c809f8f 10269 fn = this->interface_method_function(interface_method, &first_arg);
ea664253 10270 fn_args[0] = first_arg->get_backend(context);
e440a328 10271 }
10272
1ecc6157 10273 Bexpression* bclosure = NULL;
10274 if (has_closure_arg)
10275 bclosure = closure->get_backend(context);
f8bdf81a 10276 else
1ecc6157 10277 go_assert(closure == NULL);
f8bdf81a 10278
ea664253 10279 Bexpression* bfn = fn->get_backend(context);
80d1e1a8 10280
10281 // When not calling a named function directly, use a type conversion
10282 // in case the type of the function is a recursive type which refers
10283 // to itself. We don't do this for an interface method because 1)
10284 // an interface method never refers to itself, so we always have a
10285 // function type here; 2) we pass an extra first argument to an
10286 // interface method, so fntype is not correct.
10287 if (func == NULL && !is_interface_method)
10288 {
10289 Btype* bft = fntype->get_backend_fntype(gogo);
10290 bfn = gogo->backend()->convert_expression(bft, bfn, location);
10291 }
10292
1ecc6157 10293 Bexpression* call = gogo->backend()->call_expression(bfn, fn_args,
10294 bclosure, location);
e440a328 10295
2c809f8f 10296 if (this->results_ != NULL)
e440a328 10297 {
03118c21 10298 Bexpression* bcall_ref = this->call_result_ref(context);
0ab48656 10299 Bfunction* bfunction = context->function()->func_value()->get_decl();
2c809f8f 10300 Bstatement* assn_stmt =
0ab48656 10301 gogo->backend()->assignment_statement(bfunction,
10302 bcall_ref, call, location);
e440a328 10303
03118c21 10304 this->call_ = this->set_results(context);
e440a328 10305
2c809f8f 10306 Bexpression* set_and_call =
10307 gogo->backend()->compound_expression(assn_stmt, this->call_,
10308 location);
ea664253 10309 return set_and_call;
2c809f8f 10310 }
e440a328 10311
2c809f8f 10312 this->call_ = call;
ea664253 10313 return this->call_;
e440a328 10314}
10315
03118c21 10316// Return the backend representation of a reference to the struct used
10317// to capture the result of a multiple-output call.
10318
10319Bexpression*
10320Call_expression::call_result_ref(Translate_context* context)
10321{
10322 go_assert(this->call_temp_ != NULL);
10323 Location location = this->location();
10324 Expression* call_ref =
10325 Expression::make_temporary_reference(this->call_temp_, location);
10326 Bexpression* bcall_ref = call_ref->get_backend(context);
10327 return bcall_ref;
10328}
10329
ceeb4318 10330// Set the result variables if this call returns multiple results.
10331
2c809f8f 10332Bexpression*
03118c21 10333Call_expression::set_results(Translate_context* context)
ceeb4318 10334{
2c809f8f 10335 Gogo* gogo = context->gogo();
ceeb4318 10336
2c809f8f 10337 Bexpression* results = NULL;
b13c66cd 10338 Location loc = this->location();
2c809f8f 10339
03118c21 10340 go_assert(this->call_temp_ != NULL);
10341
ceeb4318 10342 size_t rc = this->result_count();
2c809f8f 10343 for (size_t i = 0; i < rc; ++i)
ceeb4318 10344 {
ceeb4318 10345 Temporary_statement* temp = this->result(i);
cd238b8d 10346 if (temp == NULL)
10347 {
10348 go_assert(saw_errors());
2c809f8f 10349 return gogo->backend()->error_expression();
cd238b8d 10350 }
ceeb4318 10351 Temporary_reference_expression* ref =
10352 Expression::make_temporary_reference(temp, loc);
10353 ref->set_is_lvalue();
ceeb4318 10354
0ab48656 10355 Bfunction* bfunction = context->function()->func_value()->get_decl();
ea664253 10356 Bexpression* result_ref = ref->get_backend(context);
03118c21 10357 Bexpression* bcall_ref = this->call_result_ref(context);
2c809f8f 10358 Bexpression* call_result =
03118c21 10359 gogo->backend()->struct_field_expression(bcall_ref, i, loc);
2c809f8f 10360 Bstatement* assn_stmt =
0ab48656 10361 gogo->backend()->assignment_statement(bfunction,
10362 result_ref, call_result, loc);
ceeb4318 10363
03118c21 10364 bcall_ref = this->call_result_ref(context);
10365 call_result = gogo->backend()->struct_field_expression(bcall_ref, i, loc);
2c809f8f 10366 Bexpression* result =
10367 gogo->backend()->compound_expression(assn_stmt, call_result, loc);
ceeb4318 10368
2c809f8f 10369 if (results == NULL)
10370 results = result;
10371 else
10372 {
0ab48656 10373 Bstatement* expr_stmt =
10374 gogo->backend()->expression_statement(bfunction, result);
2c809f8f 10375 results =
10376 gogo->backend()->compound_expression(expr_stmt, results, loc);
10377 }
10378 }
10379 return results;
ceeb4318 10380}
10381
d751bb78 10382// Dump ast representation for a call expressin.
10383
10384void
10385Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
10386{
10387 this->fn_->dump_expression(ast_dump_context);
10388 ast_dump_context->ostream() << "(";
10389 if (args_ != NULL)
10390 ast_dump_context->dump_expression_list(this->args_);
10391
10392 ast_dump_context->ostream() << ") ";
10393}
10394
e440a328 10395// Make a call expression.
10396
10397Call_expression*
10398Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
b13c66cd 10399 Location location)
e440a328 10400{
10401 return new Call_expression(fn, args, is_varargs, location);
10402}
10403
da244e59 10404// Class Call_result_expression.
e440a328 10405
10406// Traverse a call result.
10407
10408int
10409Call_result_expression::do_traverse(Traverse* traverse)
10410{
10411 if (traverse->remember_expression(this->call_))
10412 {
10413 // We have already traversed the call expression.
10414 return TRAVERSE_CONTINUE;
10415 }
10416 return Expression::traverse(&this->call_, traverse);
10417}
10418
10419// Get the type.
10420
10421Type*
10422Call_result_expression::do_type()
10423{
425dd051 10424 if (this->classification() == EXPRESSION_ERROR)
10425 return Type::make_error_type();
10426
e440a328 10427 // THIS->CALL_ can be replaced with a temporary reference due to
10428 // Call_expression::do_must_eval_in_order when there is an error.
10429 Call_expression* ce = this->call_->call_expression();
10430 if (ce == NULL)
5e85f268 10431 {
10432 this->set_is_error();
10433 return Type::make_error_type();
10434 }
e440a328 10435 Function_type* fntype = ce->get_function_type();
10436 if (fntype == NULL)
5e85f268 10437 {
e37658e2 10438 if (ce->issue_error())
99b3f06f 10439 {
10440 if (!ce->fn()->type()->is_error())
10441 this->report_error(_("expected function"));
10442 }
5e85f268 10443 this->set_is_error();
10444 return Type::make_error_type();
10445 }
e440a328 10446 const Typed_identifier_list* results = fntype->results();
ceeb4318 10447 if (results == NULL || results->size() < 2)
7b8d861f 10448 {
ceeb4318 10449 if (ce->issue_error())
10450 this->report_error(_("number of results does not match "
10451 "number of values"));
7b8d861f 10452 return Type::make_error_type();
10453 }
e440a328 10454 Typed_identifier_list::const_iterator pr = results->begin();
10455 for (unsigned int i = 0; i < this->index_; ++i)
10456 {
10457 if (pr == results->end())
425dd051 10458 break;
e440a328 10459 ++pr;
10460 }
10461 if (pr == results->end())
425dd051 10462 {
ceeb4318 10463 if (ce->issue_error())
10464 this->report_error(_("number of results does not match "
10465 "number of values"));
425dd051 10466 return Type::make_error_type();
10467 }
e440a328 10468 return pr->type();
10469}
10470
425dd051 10471// Check the type. Just make sure that we trigger the warning in
10472// do_type.
e440a328 10473
10474void
10475Call_result_expression::do_check_types(Gogo*)
10476{
425dd051 10477 this->type();
e440a328 10478}
10479
10480// Determine the type. We have nothing to do here, but the 0 result
10481// needs to pass down to the caller.
10482
10483void
10484Call_result_expression::do_determine_type(const Type_context*)
10485{
fb94b0ca 10486 this->call_->determine_type_no_context();
e440a328 10487}
10488
ea664253 10489// Return the backend representation. We just refer to the temporary set by the
10490// call expression. We don't do this at lowering time because it makes it
ceeb4318 10491// hard to evaluate the call at the right time.
e440a328 10492
ea664253 10493Bexpression*
10494Call_result_expression::do_get_backend(Translate_context* context)
e440a328 10495{
ceeb4318 10496 Call_expression* ce = this->call_->call_expression();
cd238b8d 10497 if (ce == NULL)
10498 {
10499 go_assert(this->call_->is_error_expression());
ea664253 10500 return context->backend()->error_expression();
cd238b8d 10501 }
ceeb4318 10502 Temporary_statement* ts = ce->result(this->index_);
cd238b8d 10503 if (ts == NULL)
10504 {
10505 go_assert(saw_errors());
ea664253 10506 return context->backend()->error_expression();
cd238b8d 10507 }
ceeb4318 10508 Expression* ref = Expression::make_temporary_reference(ts, this->location());
ea664253 10509 return ref->get_backend(context);
e440a328 10510}
10511
d751bb78 10512// Dump ast representation for a call result expression.
10513
10514void
10515Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10516 const
10517{
10518 // FIXME: Wouldn't it be better if the call is assigned to a temporary
10519 // (struct) and the fields are referenced instead.
10520 ast_dump_context->ostream() << this->index_ << "@(";
10521 ast_dump_context->dump_expression(this->call_);
10522 ast_dump_context->ostream() << ")";
10523}
10524
e440a328 10525// Make a reference to a single result of a call which returns
10526// multiple results.
10527
10528Expression*
10529Expression::make_call_result(Call_expression* call, unsigned int index)
10530{
10531 return new Call_result_expression(call, index);
10532}
10533
10534// Class Index_expression.
10535
10536// Traversal.
10537
10538int
10539Index_expression::do_traverse(Traverse* traverse)
10540{
10541 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
10542 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
10543 || (this->end_ != NULL
acf2b673 10544 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10545 || (this->cap_ != NULL
10546 && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT))
e440a328 10547 return TRAVERSE_EXIT;
10548 return TRAVERSE_CONTINUE;
10549}
10550
10551// Lower an index expression. This converts the generic index
10552// expression into an array index, a string index, or a map index.
10553
10554Expression*
ceeb4318 10555Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 10556{
b13c66cd 10557 Location location = this->location();
e440a328 10558 Expression* left = this->left_;
10559 Expression* start = this->start_;
10560 Expression* end = this->end_;
acf2b673 10561 Expression* cap = this->cap_;
e440a328 10562
10563 Type* type = left->type();
5c13bd80 10564 if (type->is_error())
d9f3743a 10565 {
10566 go_assert(saw_errors());
10567 return Expression::make_error(location);
10568 }
b0cf7ddd 10569 else if (left->is_type_expression())
10570 {
631d5788 10571 go_error_at(location, "attempt to index type expression");
b0cf7ddd 10572 return Expression::make_error(location);
10573 }
e440a328 10574 else if (type->array_type() != NULL)
acf2b673 10575 return Expression::make_array_index(left, start, end, cap, location);
e440a328 10576 else if (type->points_to() != NULL
10577 && type->points_to()->array_type() != NULL
411eb89e 10578 && !type->points_to()->is_slice_type())
e440a328 10579 {
10580 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
10581 location);
38092374 10582
10583 // For an ordinary index into the array, the pointer will be
10584 // dereferenced. For a slice it will not--the resulting slice
10585 // will simply reuse the pointer, which is incorrect if that
10586 // pointer is nil.
10587 if (end != NULL || cap != NULL)
10588 deref->issue_nil_check();
10589
acf2b673 10590 return Expression::make_array_index(deref, start, end, cap, location);
e440a328 10591 }
10592 else if (type->is_string_type())
acf2b673 10593 {
10594 if (cap != NULL)
10595 {
631d5788 10596 go_error_at(location, "invalid 3-index slice of string");
acf2b673 10597 return Expression::make_error(location);
10598 }
10599 return Expression::make_string_index(left, start, end, location);
10600 }
e440a328 10601 else if (type->map_type() != NULL)
10602 {
acf2b673 10603 if (end != NULL || cap != NULL)
e440a328 10604 {
631d5788 10605 go_error_at(location, "invalid slice of map");
e440a328 10606 return Expression::make_error(location);
10607 }
0d5530d9 10608 return Expression::make_map_index(left, start, location);
e440a328 10609 }
10610 else
10611 {
631d5788 10612 go_error_at(location,
10613 "attempt to index object which is not array, string, or map");
e440a328 10614 return Expression::make_error(location);
10615 }
10616}
10617
acf2b673 10618// Write an indexed expression
10619// (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
d751bb78 10620
10621void
10622Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10623 const Expression* expr,
10624 const Expression* start,
acf2b673 10625 const Expression* end,
10626 const Expression* cap)
d751bb78 10627{
10628 expr->dump_expression(ast_dump_context);
10629 ast_dump_context->ostream() << "[";
10630 start->dump_expression(ast_dump_context);
10631 if (end != NULL)
10632 {
10633 ast_dump_context->ostream() << ":";
10634 end->dump_expression(ast_dump_context);
10635 }
acf2b673 10636 if (cap != NULL)
10637 {
10638 ast_dump_context->ostream() << ":";
10639 cap->dump_expression(ast_dump_context);
10640 }
d751bb78 10641 ast_dump_context->ostream() << "]";
10642}
10643
10644// Dump ast representation for an index expression.
10645
10646void
10647Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10648 const
10649{
10650 Index_expression::dump_index_expression(ast_dump_context, this->left_,
acf2b673 10651 this->start_, this->end_, this->cap_);
d751bb78 10652}
10653
e440a328 10654// Make an index expression.
10655
10656Expression*
10657Expression::make_index(Expression* left, Expression* start, Expression* end,
acf2b673 10658 Expression* cap, Location location)
e440a328 10659{
acf2b673 10660 return new Index_expression(left, start, end, cap, location);
e440a328 10661}
10662
da244e59 10663// Class Array_index_expression.
e440a328 10664
10665// Array index traversal.
10666
10667int
10668Array_index_expression::do_traverse(Traverse* traverse)
10669{
10670 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10671 return TRAVERSE_EXIT;
10672 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10673 return TRAVERSE_EXIT;
10674 if (this->end_ != NULL)
10675 {
10676 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10677 return TRAVERSE_EXIT;
10678 }
acf2b673 10679 if (this->cap_ != NULL)
10680 {
10681 if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
10682 return TRAVERSE_EXIT;
10683 }
e440a328 10684 return TRAVERSE_CONTINUE;
10685}
10686
10687// Return the type of an array index.
10688
10689Type*
10690Array_index_expression::do_type()
10691{
10692 if (this->type_ == NULL)
10693 {
10694 Array_type* type = this->array_->type()->array_type();
10695 if (type == NULL)
10696 this->type_ = Type::make_error_type();
10697 else if (this->end_ == NULL)
10698 this->type_ = type->element_type();
411eb89e 10699 else if (type->is_slice_type())
e440a328 10700 {
10701 // A slice of a slice has the same type as the original
10702 // slice.
10703 this->type_ = this->array_->type()->deref();
10704 }
10705 else
10706 {
10707 // A slice of an array is a slice.
10708 this->type_ = Type::make_array_type(type->element_type(), NULL);
10709 }
10710 }
10711 return this->type_;
10712}
10713
10714// Set the type of an array index.
10715
10716void
10717Array_index_expression::do_determine_type(const Type_context*)
10718{
10719 this->array_->determine_type_no_context();
f77aa642 10720
10721 Type_context index_context(Type::lookup_integer_type("int"), false);
10722 if (this->start_->is_constant())
10723 this->start_->determine_type(&index_context);
10724 else
10725 this->start_->determine_type_no_context();
e440a328 10726 if (this->end_ != NULL)
f77aa642 10727 {
10728 if (this->end_->is_constant())
10729 this->end_->determine_type(&index_context);
10730 else
10731 this->end_->determine_type_no_context();
10732 }
acf2b673 10733 if (this->cap_ != NULL)
f77aa642 10734 {
10735 if (this->cap_->is_constant())
10736 this->cap_->determine_type(&index_context);
10737 else
10738 this->cap_->determine_type_no_context();
10739 }
e440a328 10740}
10741
10742// Check types of an array index.
10743
10744void
d0a50ed8 10745Array_index_expression::do_check_types(Gogo* gogo)
e440a328 10746{
f6bc81e6 10747 Numeric_constant nc;
10748 unsigned long v;
10749 if (this->start_->type()->integer_type() == NULL
10750 && !this->start_->type()->is_error()
10751 && (!this->start_->numeric_constant_value(&nc)
10752 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 10753 this->report_error(_("index must be integer"));
10754 if (this->end_ != NULL
10755 && this->end_->type()->integer_type() == NULL
99b3f06f 10756 && !this->end_->type()->is_error()
10757 && !this->end_->is_nil_expression()
f6bc81e6 10758 && !this->end_->is_error_expression()
10759 && (!this->end_->numeric_constant_value(&nc)
10760 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 10761 this->report_error(_("slice end must be integer"));
acf2b673 10762 if (this->cap_ != NULL
10763 && this->cap_->type()->integer_type() == NULL
10764 && !this->cap_->type()->is_error()
10765 && !this->cap_->is_nil_expression()
10766 && !this->cap_->is_error_expression()
10767 && (!this->cap_->numeric_constant_value(&nc)
10768 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
10769 this->report_error(_("slice capacity must be integer"));
e440a328 10770
10771 Array_type* array_type = this->array_->type()->array_type();
f9c68f17 10772 if (array_type == NULL)
10773 {
c484d925 10774 go_assert(this->array_->type()->is_error());
f9c68f17 10775 return;
10776 }
e440a328 10777
10778 unsigned int int_bits =
10779 Type::lookup_integer_type("int")->integer_type()->bits();
10780
0c77715b 10781 Numeric_constant lvalnc;
e440a328 10782 mpz_t lval;
e440a328 10783 bool lval_valid = (array_type->length() != NULL
0c77715b 10784 && array_type->length()->numeric_constant_value(&lvalnc)
10785 && lvalnc.to_int(&lval));
10786 Numeric_constant inc;
e440a328 10787 mpz_t ival;
0bd5d859 10788 bool ival_valid = false;
0c77715b 10789 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 10790 {
0bd5d859 10791 ival_valid = true;
e440a328 10792 if (mpz_sgn(ival) < 0
10793 || mpz_sizeinbase(ival, 2) >= int_bits
10794 || (lval_valid
10795 && (this->end_ == NULL
10796 ? mpz_cmp(ival, lval) >= 0
10797 : mpz_cmp(ival, lval) > 0)))
10798 {
631d5788 10799 go_error_at(this->start_->location(), "array index out of bounds");
e440a328 10800 this->set_is_error();
10801 }
10802 }
10803 if (this->end_ != NULL && !this->end_->is_nil_expression())
10804 {
0c77715b 10805 Numeric_constant enc;
10806 mpz_t eval;
acf2b673 10807 bool eval_valid = false;
0c77715b 10808 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 10809 {
acf2b673 10810 eval_valid = true;
0c77715b 10811 if (mpz_sgn(eval) < 0
10812 || mpz_sizeinbase(eval, 2) >= int_bits
10813 || (lval_valid && mpz_cmp(eval, lval) > 0))
e440a328 10814 {
631d5788 10815 go_error_at(this->end_->location(), "array index out of bounds");
e440a328 10816 this->set_is_error();
10817 }
0bd5d859 10818 else if (ival_valid && mpz_cmp(ival, eval) > 0)
10819 this->report_error(_("inverted slice range"));
e440a328 10820 }
acf2b673 10821
10822 Numeric_constant cnc;
10823 mpz_t cval;
10824 if (this->cap_ != NULL
10825 && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval))
10826 {
10827 if (mpz_sgn(cval) < 0
10828 || mpz_sizeinbase(cval, 2) >= int_bits
10829 || (lval_valid && mpz_cmp(cval, lval) > 0))
10830 {
631d5788 10831 go_error_at(this->cap_->location(), "array index out of bounds");
acf2b673 10832 this->set_is_error();
10833 }
10834 else if (ival_valid && mpz_cmp(ival, cval) > 0)
10835 {
631d5788 10836 go_error_at(this->cap_->location(),
10837 "invalid slice index: capacity less than start");
acf2b673 10838 this->set_is_error();
10839 }
10840 else if (eval_valid && mpz_cmp(eval, cval) > 0)
10841 {
631d5788 10842 go_error_at(this->cap_->location(),
10843 "invalid slice index: capacity less than length");
acf2b673 10844 this->set_is_error();
10845 }
10846 mpz_clear(cval);
10847 }
10848
10849 if (eval_valid)
10850 mpz_clear(eval);
e440a328 10851 }
0bd5d859 10852 if (ival_valid)
10853 mpz_clear(ival);
0c77715b 10854 if (lval_valid)
10855 mpz_clear(lval);
e440a328 10856
10857 // A slice of an array requires an addressable array. A slice of a
10858 // slice is always possible.
411eb89e 10859 if (this->end_ != NULL && !array_type->is_slice_type())
88ec30c8 10860 {
10861 if (!this->array_->is_addressable())
8da39c3b 10862 this->report_error(_("slice of unaddressable value"));
88ec30c8 10863 else
d0a50ed8 10864 {
10865 bool escapes = true;
10866
10867 // When compiling the runtime, a slice operation does not
10868 // cause local variables to escape. When escape analysis
10869 // becomes the default, this should be changed to make it an
10870 // error if we have a slice operation that escapes.
10871 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
10872 escapes = false;
10873
10874 this->array_->address_taken(escapes);
10875 }
88ec30c8 10876 }
e440a328 10877}
10878
2c809f8f 10879// Flatten array indexing by using temporary variables for slices and indexes.
35a54f17 10880
10881Expression*
10882Array_index_expression::do_flatten(Gogo*, Named_object*,
10883 Statement_inserter* inserter)
10884{
10885 Location loc = this->location();
5bf8be8b 10886 Expression* array = this->array_;
10887 Expression* start = this->start_;
10888 Expression* end = this->end_;
10889 Expression* cap = this->cap_;
10890 if (array->is_error_expression()
10891 || array->type()->is_error_type()
10892 || start->is_error_expression()
10893 || start->type()->is_error_type()
10894 || (end != NULL
10895 && (end->is_error_expression() || end->type()->is_error_type()))
10896 || (cap != NULL
10897 && (cap->is_error_expression() || cap->type()->is_error_type())))
10898 {
10899 go_assert(saw_errors());
10900 return Expression::make_error(loc);
10901 }
10902
2c809f8f 10903 Temporary_statement* temp;
5bf8be8b 10904 if (array->type()->is_slice_type() && !array->is_variable())
35a54f17 10905 {
5bf8be8b 10906 temp = Statement::make_temporary(NULL, array, loc);
35a54f17 10907 inserter->insert(temp);
10908 this->array_ = Expression::make_temporary_reference(temp, loc);
10909 }
5bf8be8b 10910 if (!start->is_variable())
2c809f8f 10911 {
5bf8be8b 10912 temp = Statement::make_temporary(NULL, start, loc);
2c809f8f 10913 inserter->insert(temp);
10914 this->start_ = Expression::make_temporary_reference(temp, loc);
10915 }
5bf8be8b 10916 if (end != NULL
10917 && !end->is_nil_expression()
10918 && !end->is_variable())
2c809f8f 10919 {
5bf8be8b 10920 temp = Statement::make_temporary(NULL, end, loc);
2c809f8f 10921 inserter->insert(temp);
10922 this->end_ = Expression::make_temporary_reference(temp, loc);
10923 }
03118c21 10924 if (cap != NULL && !cap->is_variable())
2c809f8f 10925 {
5bf8be8b 10926 temp = Statement::make_temporary(NULL, cap, loc);
2c809f8f 10927 inserter->insert(temp);
10928 this->cap_ = Expression::make_temporary_reference(temp, loc);
10929 }
10930
35a54f17 10931 return this;
10932}
10933
e440a328 10934// Return whether this expression is addressable.
10935
10936bool
10937Array_index_expression::do_is_addressable() const
10938{
10939 // A slice expression is not addressable.
10940 if (this->end_ != NULL)
10941 return false;
10942
10943 // An index into a slice is addressable.
411eb89e 10944 if (this->array_->type()->is_slice_type())
e440a328 10945 return true;
10946
10947 // An index into an array is addressable if the array is
10948 // addressable.
10949 return this->array_->is_addressable();
10950}
10951
ea664253 10952// Get the backend representation for an array index.
e440a328 10953
ea664253 10954Bexpression*
10955Array_index_expression::do_get_backend(Translate_context* context)
e440a328 10956{
e440a328 10957 Array_type* array_type = this->array_->type()->array_type();
d8cd8e2d 10958 if (array_type == NULL)
10959 {
c484d925 10960 go_assert(this->array_->type()->is_error());
ea664253 10961 return context->backend()->error_expression();
d8cd8e2d 10962 }
35a54f17 10963 go_assert(!array_type->is_slice_type() || this->array_->is_variable());
e440a328 10964
2c809f8f 10965 Location loc = this->location();
10966 Gogo* gogo = context->gogo();
10967
6dfedc16 10968 Type* int_type = Type::lookup_integer_type("int");
10969 Btype* int_btype = int_type->get_backend(gogo);
e440a328 10970
2c809f8f 10971 // We need to convert the length and capacity to the Go "int" type here
10972 // because the length of a fixed-length array could be of type "uintptr"
10973 // and gimple disallows binary operations between "uintptr" and other
10974 // integer types. FIXME.
10975 Bexpression* length = NULL;
a04bfdfc 10976 if (this->end_ == NULL || this->end_->is_nil_expression())
10977 {
35a54f17 10978 Expression* len = array_type->get_length(gogo, this->array_);
ea664253 10979 length = len->get_backend(context);
2c809f8f 10980 length = gogo->backend()->convert_expression(int_btype, length, loc);
a04bfdfc 10981 }
10982
2c809f8f 10983 Bexpression* capacity = NULL;
a04bfdfc 10984 if (this->end_ != NULL)
10985 {
35a54f17 10986 Expression* cap = array_type->get_capacity(gogo, this->array_);
ea664253 10987 capacity = cap->get_backend(context);
2c809f8f 10988 capacity = gogo->backend()->convert_expression(int_btype, capacity, loc);
a04bfdfc 10989 }
10990
2c809f8f 10991 Bexpression* cap_arg = capacity;
acf2b673 10992 if (this->cap_ != NULL)
10993 {
ea664253 10994 cap_arg = this->cap_->get_backend(context);
2c809f8f 10995 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
acf2b673 10996 }
10997
2c809f8f 10998 if (length == NULL)
10999 length = cap_arg;
e440a328 11000
11001 int code = (array_type->length() != NULL
11002 ? (this->end_ == NULL
11003 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
11004 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
11005 : (this->end_ == NULL
11006 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
11007 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
ea664253 11008 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context);
2c809f8f 11009
6dfedc16 11010 if (this->start_->type()->integer_type() == NULL
11011 && !Type::are_convertible(int_type, this->start_->type(), NULL))
11012 {
11013 go_assert(saw_errors());
11014 return context->backend()->error_expression();
11015 }
d9f3743a 11016
ea664253 11017 Bexpression* bad_index =
d9f3743a 11018 Expression::check_bounds(this->start_, loc)->get_backend(context);
2c809f8f 11019
ea664253 11020 Bexpression* start = this->start_->get_backend(context);
2c809f8f 11021 start = gogo->backend()->convert_expression(int_btype, start, loc);
11022 Bexpression* start_too_large =
11023 gogo->backend()->binary_expression((this->end_ == NULL
11024 ? OPERATOR_GE
11025 : OPERATOR_GT),
11026 start,
11027 (this->end_ == NULL
11028 ? length
11029 : capacity),
11030 loc);
11031 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large,
11032 bad_index, loc);
e440a328 11033
93715b75 11034 Bfunction* bfn = context->function()->func_value()->get_decl();
e440a328 11035 if (this->end_ == NULL)
11036 {
11037 // Simple array indexing. This has to return an l-value, so
2c809f8f 11038 // wrap the index check into START.
11039 start =
93715b75 11040 gogo->backend()->conditional_expression(bfn, int_btype, bad_index,
2c809f8f 11041 crash, start, loc);
e440a328 11042
2c809f8f 11043 Bexpression* ret;
e440a328 11044 if (array_type->length() != NULL)
11045 {
ea664253 11046 Bexpression* array = this->array_->get_backend(context);
2c809f8f 11047 ret = gogo->backend()->array_index_expression(array, start, loc);
e440a328 11048 }
11049 else
11050 {
2c809f8f 11051 // Slice.
11052 Expression* valptr =
35a54f17 11053 array_type->get_value_pointer(gogo, this->array_);
ea664253 11054 Bexpression* ptr = valptr->get_backend(context);
2c809f8f 11055 ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc);
9b27b43c 11056
11057 Type* ele_type = this->array_->type()->array_type()->element_type();
11058 Btype* ele_btype = ele_type->get_backend(gogo);
11059 ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc);
e440a328 11060 }
ea664253 11061 return ret;
e440a328 11062 }
11063
11064 // Array slice.
11065
acf2b673 11066 if (this->cap_ != NULL)
11067 {
2c809f8f 11068 Bexpression* bounds_bcheck =
ea664253 11069 Expression::check_bounds(this->cap_, loc)->get_backend(context);
2c809f8f 11070 bad_index =
11071 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck,
11072 bad_index, loc);
11073 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
11074
11075 Bexpression* cap_too_small =
11076 gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc);
11077 Bexpression* cap_too_large =
11078 gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc);
11079 Bexpression* bad_cap =
11080 gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small,
11081 cap_too_large, loc);
11082 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap,
11083 bad_index, loc);
11084 }
11085
11086 Bexpression* end;
e440a328 11087 if (this->end_->is_nil_expression())
2c809f8f 11088 end = length;
e440a328 11089 else
11090 {
2c809f8f 11091 Bexpression* bounds_bcheck =
ea664253 11092 Expression::check_bounds(this->end_, loc)->get_backend(context);
e440a328 11093
2c809f8f 11094 bad_index =
11095 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck,
11096 bad_index, loc);
e440a328 11097
ea664253 11098 end = this->end_->get_backend(context);
2c809f8f 11099 end = gogo->backend()->convert_expression(int_btype, end, loc);
11100 Bexpression* end_too_small =
11101 gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc);
11102 Bexpression* end_too_large =
11103 gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc);
11104 Bexpression* bad_end =
11105 gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small,
11106 end_too_large, loc);
11107 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end,
11108 bad_index, loc);
e440a328 11109 }
11110
2c809f8f 11111 Bexpression* result_length =
11112 gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc);
e440a328 11113
2c809f8f 11114 Bexpression* result_capacity =
11115 gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc);
e440a328 11116
03118c21 11117 // If the new capacity is zero, don't change val. Otherwise we can
11118 // get a pointer to the next object in memory, keeping it live
11119 // unnecessarily. When the capacity is zero, the actual pointer
11120 // value doesn't matter.
11121 Bexpression* zero =
11122 Expression::make_integer_ul(0, int_type, loc)->get_backend(context);
11123 Bexpression* cond =
11124 gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero,
11125 loc);
11126 Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype,
11127 cond, zero,
11128 start, loc);
11129 Expression* valptr = array_type->get_value_pointer(gogo, this->array_);
11130 Bexpression* val = valptr->get_backend(context);
11131 val = gogo->backend()->pointer_offset_expression(val, offset, loc);
11132
2c809f8f 11133 Btype* struct_btype = this->type()->get_backend(gogo);
11134 std::vector<Bexpression*> init;
11135 init.push_back(val);
11136 init.push_back(result_length);
11137 init.push_back(result_capacity);
e440a328 11138
2c809f8f 11139 Bexpression* ctor =
11140 gogo->backend()->constructor_expression(struct_btype, init, loc);
93715b75 11141 return gogo->backend()->conditional_expression(bfn, struct_btype, bad_index,
ea664253 11142 crash, ctor, loc);
e440a328 11143}
11144
d751bb78 11145// Dump ast representation for an array index expression.
11146
11147void
11148Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11149 const
11150{
11151 Index_expression::dump_index_expression(ast_dump_context, this->array_,
acf2b673 11152 this->start_, this->end_, this->cap_);
d751bb78 11153}
11154
acf2b673 11155// Make an array index expression. END and CAP may be NULL.
e440a328 11156
11157Expression*
11158Expression::make_array_index(Expression* array, Expression* start,
acf2b673 11159 Expression* end, Expression* cap,
11160 Location location)
e440a328 11161{
acf2b673 11162 return new Array_index_expression(array, start, end, cap, location);
e440a328 11163}
11164
50075d74 11165// Class String_index_expression.
e440a328 11166
11167// String index traversal.
11168
11169int
11170String_index_expression::do_traverse(Traverse* traverse)
11171{
11172 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
11173 return TRAVERSE_EXIT;
11174 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
11175 return TRAVERSE_EXIT;
11176 if (this->end_ != NULL)
11177 {
11178 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
11179 return TRAVERSE_EXIT;
11180 }
11181 return TRAVERSE_CONTINUE;
11182}
11183
2c809f8f 11184Expression*
11185String_index_expression::do_flatten(Gogo*, Named_object*,
11186 Statement_inserter* inserter)
e440a328 11187{
2c809f8f 11188 Location loc = this->location();
5bf8be8b 11189 Expression* string = this->string_;
11190 Expression* start = this->start_;
11191 Expression* end = this->end_;
11192 if (string->is_error_expression()
11193 || string->type()->is_error_type()
11194 || start->is_error_expression()
11195 || start->type()->is_error_type()
11196 || (end != NULL
11197 && (end->is_error_expression() || end->type()->is_error_type())))
11198 {
11199 go_assert(saw_errors());
11200 return Expression::make_error(loc);
11201 }
11202
11203 Temporary_statement* temp;
2c809f8f 11204 if (!this->string_->is_variable())
11205 {
11206 temp = Statement::make_temporary(NULL, this->string_, loc);
11207 inserter->insert(temp);
11208 this->string_ = Expression::make_temporary_reference(temp, loc);
11209 }
11210 if (!this->start_->is_variable())
11211 {
11212 temp = Statement::make_temporary(NULL, this->start_, loc);
11213 inserter->insert(temp);
11214 this->start_ = Expression::make_temporary_reference(temp, loc);
11215 }
11216 if (this->end_ != NULL
11217 && !this->end_->is_nil_expression()
11218 && !this->end_->is_variable())
11219 {
11220 temp = Statement::make_temporary(NULL, this->end_, loc);
11221 inserter->insert(temp);
11222 this->end_ = Expression::make_temporary_reference(temp, loc);
11223 }
11224
11225 return this;
11226}
11227
11228// Return the type of a string index.
11229
11230Type*
11231String_index_expression::do_type()
11232{
11233 if (this->end_ == NULL)
11234 return Type::lookup_integer_type("uint8");
11235 else
11236 return this->string_->type();
11237}
11238
11239// Determine the type of a string index.
11240
11241void
11242String_index_expression::do_determine_type(const Type_context*)
11243{
11244 this->string_->determine_type_no_context();
f77aa642 11245
11246 Type_context index_context(Type::lookup_integer_type("int"), false);
11247 if (this->start_->is_constant())
11248 this->start_->determine_type(&index_context);
11249 else
11250 this->start_->determine_type_no_context();
e440a328 11251 if (this->end_ != NULL)
f77aa642 11252 {
11253 if (this->end_->is_constant())
11254 this->end_->determine_type(&index_context);
11255 else
11256 this->end_->determine_type_no_context();
11257 }
e440a328 11258}
11259
11260// Check types of a string index.
11261
11262void
11263String_index_expression::do_check_types(Gogo*)
11264{
acdc230d 11265 Numeric_constant nc;
11266 unsigned long v;
11267 if (this->start_->type()->integer_type() == NULL
11268 && !this->start_->type()->is_error()
11269 && (!this->start_->numeric_constant_value(&nc)
11270 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 11271 this->report_error(_("index must be integer"));
11272 if (this->end_ != NULL
11273 && this->end_->type()->integer_type() == NULL
acdc230d 11274 && !this->end_->type()->is_error()
11275 && !this->end_->is_nil_expression()
11276 && !this->end_->is_error_expression()
11277 && (!this->end_->numeric_constant_value(&nc)
11278 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 11279 this->report_error(_("slice end must be integer"));
11280
11281 std::string sval;
11282 bool sval_valid = this->string_->string_constant_value(&sval);
11283
0c77715b 11284 Numeric_constant inc;
e440a328 11285 mpz_t ival;
0bd5d859 11286 bool ival_valid = false;
0c77715b 11287 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 11288 {
0bd5d859 11289 ival_valid = true;
e440a328 11290 if (mpz_sgn(ival) < 0
b10f32fb 11291 || (sval_valid
11292 && (this->end_ == NULL
11293 ? mpz_cmp_ui(ival, sval.length()) >= 0
11294 : mpz_cmp_ui(ival, sval.length()) > 0)))
e440a328 11295 {
631d5788 11296 go_error_at(this->start_->location(), "string index out of bounds");
e440a328 11297 this->set_is_error();
11298 }
11299 }
11300 if (this->end_ != NULL && !this->end_->is_nil_expression())
11301 {
0c77715b 11302 Numeric_constant enc;
11303 mpz_t eval;
11304 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 11305 {
0c77715b 11306 if (mpz_sgn(eval) < 0
11307 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
e440a328 11308 {
631d5788 11309 go_error_at(this->end_->location(), "string index out of bounds");
e440a328 11310 this->set_is_error();
11311 }
0bd5d859 11312 else if (ival_valid && mpz_cmp(ival, eval) > 0)
11313 this->report_error(_("inverted slice range"));
0c77715b 11314 mpz_clear(eval);
e440a328 11315 }
11316 }
0bd5d859 11317 if (ival_valid)
11318 mpz_clear(ival);
e440a328 11319}
11320
ea664253 11321// Get the backend representation for a string index.
e440a328 11322
ea664253 11323Bexpression*
11324String_index_expression::do_get_backend(Translate_context* context)
e440a328 11325{
b13c66cd 11326 Location loc = this->location();
2c809f8f 11327 Expression* string_arg = this->string_;
11328 if (this->string_->type()->points_to() != NULL)
11329 string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc);
e440a328 11330
2c809f8f 11331 Expression* bad_index = Expression::check_bounds(this->start_, loc);
e440a328 11332
2c809f8f 11333 int code = (this->end_ == NULL
11334 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
11335 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
e440a328 11336
2c809f8f 11337 Gogo* gogo = context->gogo();
ea664253 11338 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context);
1b1f2abf 11339
11340 Type* int_type = Type::lookup_integer_type("int");
e440a328 11341
2c809f8f 11342 // It is possible that an error occurred earlier because the start index
11343 // cannot be represented as an integer type. In this case, we shouldn't
11344 // try casting the starting index into an integer since
11345 // Type_conversion_expression will fail to get the backend representation.
11346 // FIXME.
11347 if (this->start_->type()->integer_type() == NULL
11348 && !Type::are_convertible(int_type, this->start_->type(), NULL))
11349 {
11350 go_assert(saw_errors());
ea664253 11351 return context->backend()->error_expression();
2c809f8f 11352 }
e440a328 11353
2c809f8f 11354 Expression* start = Expression::make_cast(int_type, this->start_, loc);
93715b75 11355 Bfunction* bfn = context->function()->func_value()->get_decl();
e440a328 11356
2c809f8f 11357 if (this->end_ == NULL)
11358 {
11359 Expression* length =
11360 Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc);
e440a328 11361
2c809f8f 11362 Expression* start_too_large =
11363 Expression::make_binary(OPERATOR_GE, start, length, loc);
11364 bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large,
11365 bad_index, loc);
11366 Expression* bytes =
11367 Expression::make_string_info(this->string_, STRING_INFO_DATA, loc);
e440a328 11368
ea664253 11369 Bexpression* bstart = start->get_backend(context);
11370 Bexpression* ptr = bytes->get_backend(context);
2c809f8f 11371 ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc);
9b27b43c 11372 Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo);
11373 Bexpression* index =
11374 gogo->backend()->indirect_expression(ubtype, ptr, true, loc);
e440a328 11375
2c809f8f 11376 Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo);
ea664253 11377 Bexpression* index_error = bad_index->get_backend(context);
93715b75 11378 return gogo->backend()->conditional_expression(bfn, byte_btype,
11379 index_error, crash,
11380 index, loc);
2c809f8f 11381 }
11382
11383 Expression* end = NULL;
11384 if (this->end_->is_nil_expression())
e67508fa 11385 end = Expression::make_integer_sl(-1, int_type, loc);
e440a328 11386 else
11387 {
2c809f8f 11388 Expression* bounds_check = Expression::check_bounds(this->end_, loc);
11389 bad_index =
11390 Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc);
11391 end = Expression::make_cast(int_type, this->end_, loc);
e440a328 11392 }
2c809f8f 11393
11394 Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3,
11395 string_arg, start, end);
ea664253 11396 Bexpression* bstrslice = strslice->get_backend(context);
2c809f8f 11397
11398 Btype* str_btype = strslice->type()->get_backend(gogo);
ea664253 11399 Bexpression* index_error = bad_index->get_backend(context);
93715b75 11400 return gogo->backend()->conditional_expression(bfn, str_btype, index_error,
ea664253 11401 crash, bstrslice, loc);
e440a328 11402}
11403
d751bb78 11404// Dump ast representation for a string index expression.
11405
11406void
11407String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11408 const
11409{
acf2b673 11410 Index_expression::dump_index_expression(ast_dump_context, this->string_,
11411 this->start_, this->end_, NULL);
d751bb78 11412}
11413
e440a328 11414// Make a string index expression. END may be NULL.
11415
11416Expression*
11417Expression::make_string_index(Expression* string, Expression* start,
b13c66cd 11418 Expression* end, Location location)
e440a328 11419{
11420 return new String_index_expression(string, start, end, location);
11421}
11422
11423// Class Map_index.
11424
11425// Get the type of the map.
11426
11427Map_type*
11428Map_index_expression::get_map_type() const
11429{
0d5530d9 11430 Map_type* mt = this->map_->type()->map_type();
c7524fae 11431 if (mt == NULL)
c484d925 11432 go_assert(saw_errors());
e440a328 11433 return mt;
11434}
11435
11436// Map index traversal.
11437
11438int
11439Map_index_expression::do_traverse(Traverse* traverse)
11440{
11441 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
11442 return TRAVERSE_EXIT;
11443 return Expression::traverse(&this->index_, traverse);
11444}
11445
2c809f8f 11446// We need to pass in a pointer to the key, so flatten the index into a
11447// temporary variable if it isn't already. The value pointer will be
11448// dereferenced and checked for nil, so flatten into a temporary to avoid
11449// recomputation.
11450
11451Expression*
91c0fd76 11452Map_index_expression::do_flatten(Gogo* gogo, Named_object*,
2c809f8f 11453 Statement_inserter* inserter)
11454{
91c0fd76 11455 Location loc = this->location();
2c809f8f 11456 Map_type* mt = this->get_map_type();
5bf8be8b 11457 if (this->index()->is_error_expression()
11458 || this->index()->type()->is_error_type()
11459 || mt->is_error_type())
11460 {
11461 go_assert(saw_errors());
11462 return Expression::make_error(loc);
11463 }
11464
91c0fd76 11465 if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL))
11466 {
11467 if (this->index_->type()->interface_type() != NULL
11468 && !this->index_->is_variable())
11469 {
11470 Temporary_statement* temp =
11471 Statement::make_temporary(NULL, this->index_, loc);
11472 inserter->insert(temp);
11473 this->index_ = Expression::make_temporary_reference(temp, loc);
11474 }
11475 this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(),
11476 this->index_, loc);
11477 }
2c809f8f 11478
11479 if (!this->index_->is_variable())
11480 {
11481 Temporary_statement* temp = Statement::make_temporary(NULL, this->index_,
91c0fd76 11482 loc);
2c809f8f 11483 inserter->insert(temp);
91c0fd76 11484 this->index_ = Expression::make_temporary_reference(temp, loc);
2c809f8f 11485 }
11486
11487 if (this->value_pointer_ == NULL)
0d5530d9 11488 this->get_value_pointer(gogo);
5bf8be8b 11489 if (this->value_pointer_->is_error_expression()
11490 || this->value_pointer_->type()->is_error_type())
11491 return Expression::make_error(loc);
2c809f8f 11492 if (!this->value_pointer_->is_variable())
11493 {
11494 Temporary_statement* temp =
91c0fd76 11495 Statement::make_temporary(NULL, this->value_pointer_, loc);
2c809f8f 11496 inserter->insert(temp);
91c0fd76 11497 this->value_pointer_ = Expression::make_temporary_reference(temp, loc);
2c809f8f 11498 }
11499
11500 return this;
11501}
11502
e440a328 11503// Return the type of a map index.
11504
11505Type*
11506Map_index_expression::do_type()
11507{
c7524fae 11508 Map_type* mt = this->get_map_type();
11509 if (mt == NULL)
11510 return Type::make_error_type();
0d5530d9 11511 return mt->val_type();
e440a328 11512}
11513
11514// Fix the type of a map index.
11515
11516void
11517Map_index_expression::do_determine_type(const Type_context*)
11518{
11519 this->map_->determine_type_no_context();
c7524fae 11520 Map_type* mt = this->get_map_type();
11521 Type* key_type = mt == NULL ? NULL : mt->key_type();
11522 Type_context subcontext(key_type, false);
e440a328 11523 this->index_->determine_type(&subcontext);
11524}
11525
11526// Check types of a map index.
11527
11528void
11529Map_index_expression::do_check_types(Gogo*)
11530{
11531 std::string reason;
c7524fae 11532 Map_type* mt = this->get_map_type();
11533 if (mt == NULL)
11534 return;
11535 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
e440a328 11536 {
11537 if (reason.empty())
11538 this->report_error(_("incompatible type for map index"));
11539 else
11540 {
631d5788 11541 go_error_at(this->location(), "incompatible type for map index (%s)",
11542 reason.c_str());
e440a328 11543 this->set_is_error();
11544 }
11545 }
11546}
11547
ea664253 11548// Get the backend representation for a map index.
e440a328 11549
ea664253 11550Bexpression*
11551Map_index_expression::do_get_backend(Translate_context* context)
e440a328 11552{
11553 Map_type* type = this->get_map_type();
c7524fae 11554 if (type == NULL)
2c809f8f 11555 {
11556 go_assert(saw_errors());
ea664253 11557 return context->backend()->error_expression();
2c809f8f 11558 }
e440a328 11559
2c809f8f 11560 go_assert(this->value_pointer_ != NULL
11561 && this->value_pointer_->is_variable());
e440a328 11562
0d5530d9 11563 Expression* val = Expression::make_unary(OPERATOR_MULT, this->value_pointer_,
11564 this->location());
11565 return val->get_backend(context);
e440a328 11566}
11567
0d5530d9 11568// Get an expression for the map index. This returns an expression
11569// that evaluates to a pointer to a value. If the key is not in the
11570// map, the pointer will point to a zero value.
e440a328 11571
2c809f8f 11572Expression*
0d5530d9 11573Map_index_expression::get_value_pointer(Gogo* gogo)
e440a328 11574{
2c809f8f 11575 if (this->value_pointer_ == NULL)
746d2e73 11576 {
2c809f8f 11577 Map_type* type = this->get_map_type();
11578 if (type == NULL)
746d2e73 11579 {
2c809f8f 11580 go_assert(saw_errors());
11581 return Expression::make_error(this->location());
746d2e73 11582 }
e440a328 11583
2c809f8f 11584 Location loc = this->location();
11585 Expression* map_ref = this->map_;
e440a328 11586
0d5530d9 11587 Expression* index_ptr = Expression::make_unary(OPERATOR_AND,
11588 this->index_,
2c809f8f 11589 loc);
0d5530d9 11590
11591 Expression* zero = type->fat_zero_value(gogo);
11592
11593 Expression* map_index;
11594
11595 if (zero == NULL)
11596 map_index =
11597 Runtime::make_call(Runtime::MAPACCESS1, loc, 3,
11598 Expression::make_type_descriptor(type, loc),
11599 map_ref, index_ptr);
11600 else
11601 map_index =
11602 Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4,
11603 Expression::make_type_descriptor(type, loc),
11604 map_ref, index_ptr, zero);
2c809f8f 11605
11606 Type* val_type = type->val_type();
11607 this->value_pointer_ =
11608 Expression::make_unsafe_cast(Type::make_pointer_type(val_type),
11609 map_index, this->location());
11610 }
0d5530d9 11611
2c809f8f 11612 return this->value_pointer_;
e440a328 11613}
11614
d751bb78 11615// Dump ast representation for a map index expression
11616
11617void
11618Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11619 const
11620{
acf2b673 11621 Index_expression::dump_index_expression(ast_dump_context, this->map_,
11622 this->index_, NULL, NULL);
d751bb78 11623}
11624
e440a328 11625// Make a map index expression.
11626
11627Map_index_expression*
11628Expression::make_map_index(Expression* map, Expression* index,
b13c66cd 11629 Location location)
e440a328 11630{
11631 return new Map_index_expression(map, index, location);
11632}
11633
11634// Class Field_reference_expression.
11635
149eabc5 11636// Lower a field reference expression. There is nothing to lower, but
11637// this is where we generate the tracking information for fields with
11638// the magic go:"track" tag.
11639
11640Expression*
11641Field_reference_expression::do_lower(Gogo* gogo, Named_object* function,
11642 Statement_inserter* inserter, int)
11643{
11644 Struct_type* struct_type = this->expr_->type()->struct_type();
11645 if (struct_type == NULL)
11646 {
11647 // Error will be reported elsewhere.
11648 return this;
11649 }
11650 const Struct_field* field = struct_type->field(this->field_index_);
11651 if (field == NULL)
11652 return this;
11653 if (!field->has_tag())
11654 return this;
11655 if (field->tag().find("go:\"track\"") == std::string::npos)
11656 return this;
11657
604e278d 11658 // References from functions generated by the compiler don't count.
c6292d1d 11659 if (function != NULL && function->func_value()->is_type_specific_function())
604e278d 11660 return this;
11661
149eabc5 11662 // We have found a reference to a tracked field. Build a call to
11663 // the runtime function __go_fieldtrack with a string that describes
11664 // the field. FIXME: We should only call this once per referenced
11665 // field per function, not once for each reference to the field.
11666
11667 if (this->called_fieldtrack_)
11668 return this;
11669 this->called_fieldtrack_ = true;
11670
11671 Location loc = this->location();
11672
11673 std::string s = "fieldtrack \"";
11674 Named_type* nt = this->expr_->type()->named_type();
11675 if (nt == NULL || nt->named_object()->package() == NULL)
11676 s.append(gogo->pkgpath());
11677 else
11678 s.append(nt->named_object()->package()->pkgpath());
11679 s.push_back('.');
11680 if (nt != NULL)
5c29ad36 11681 s.append(Gogo::unpack_hidden_name(nt->name()));
149eabc5 11682 s.push_back('.');
11683 s.append(field->field_name());
11684 s.push_back('"');
11685
11686 // We can't use a string here, because internally a string holds a
11687 // pointer to the actual bytes; when the linker garbage collects the
11688 // string, it won't garbage collect the bytes. So we use a
11689 // [...]byte.
11690
e67508fa 11691 Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc);
149eabc5 11692
11693 Type* byte_type = gogo->lookup_global("byte")->type_value();
6bf4793c 11694 Array_type* array_type = Type::make_array_type(byte_type, length_expr);
11695 array_type->set_is_array_incomparable();
149eabc5 11696
11697 Expression_list* bytes = new Expression_list();
11698 for (std::string::const_iterator p = s.begin(); p != s.end(); p++)
11699 {
e67508fa 11700 unsigned char c = static_cast<unsigned char>(*p);
11701 bytes->push_back(Expression::make_integer_ul(c, NULL, loc));
149eabc5 11702 }
11703
11704 Expression* e = Expression::make_composite_literal(array_type, 0, false,
62750cd5 11705 bytes, false, loc);
149eabc5 11706
11707 Variable* var = new Variable(array_type, e, true, false, false, loc);
11708
11709 static int count;
11710 char buf[50];
11711 snprintf(buf, sizeof buf, "fieldtrack.%d", count);
11712 ++count;
11713
11714 Named_object* no = gogo->add_variable(buf, var);
11715 e = Expression::make_var_reference(no, loc);
11716 e = Expression::make_unary(OPERATOR_AND, e, loc);
11717
11718 Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e);
604e278d 11719 gogo->lower_expression(function, inserter, &call);
149eabc5 11720 inserter->insert(Statement::make_statement(call, false));
11721
11722 // Put this function, and the global variable we just created, into
11723 // unique sections. This will permit the linker to garbage collect
11724 // them if they are not referenced. The effect is that the only
11725 // strings, indicating field references, that will wind up in the
11726 // executable will be those for functions that are actually needed.
66a6be58 11727 if (function != NULL)
11728 function->func_value()->set_in_unique_section();
149eabc5 11729 var->set_in_unique_section();
11730
11731 return this;
11732}
11733
e440a328 11734// Return the type of a field reference.
11735
11736Type*
11737Field_reference_expression::do_type()
11738{
b0e628fb 11739 Type* type = this->expr_->type();
5c13bd80 11740 if (type->is_error())
b0e628fb 11741 return type;
11742 Struct_type* struct_type = type->struct_type();
c484d925 11743 go_assert(struct_type != NULL);
e440a328 11744 return struct_type->field(this->field_index_)->type();
11745}
11746
11747// Check the types for a field reference.
11748
11749void
11750Field_reference_expression::do_check_types(Gogo*)
11751{
b0e628fb 11752 Type* type = this->expr_->type();
5c13bd80 11753 if (type->is_error())
b0e628fb 11754 return;
11755 Struct_type* struct_type = type->struct_type();
c484d925 11756 go_assert(struct_type != NULL);
11757 go_assert(struct_type->field(this->field_index_) != NULL);
e440a328 11758}
11759
ea664253 11760// Get the backend representation for a field reference.
e440a328 11761
ea664253 11762Bexpression*
11763Field_reference_expression::do_get_backend(Translate_context* context)
e440a328 11764{
ea664253 11765 Bexpression* bstruct = this->expr_->get_backend(context);
11766 return context->gogo()->backend()->struct_field_expression(bstruct,
11767 this->field_index_,
11768 this->location());
e440a328 11769}
11770
d751bb78 11771// Dump ast representation for a field reference expression.
11772
11773void
11774Field_reference_expression::do_dump_expression(
11775 Ast_dump_context* ast_dump_context) const
11776{
11777 this->expr_->dump_expression(ast_dump_context);
11778 ast_dump_context->ostream() << "." << this->field_index_;
11779}
11780
e440a328 11781// Make a reference to a qualified identifier in an expression.
11782
11783Field_reference_expression*
11784Expression::make_field_reference(Expression* expr, unsigned int field_index,
b13c66cd 11785 Location location)
e440a328 11786{
11787 return new Field_reference_expression(expr, field_index, location);
11788}
11789
11790// Class Interface_field_reference_expression.
11791
2387f644 11792// Return an expression for the pointer to the function to call.
e440a328 11793
2387f644 11794Expression*
11795Interface_field_reference_expression::get_function()
e440a328 11796{
2387f644 11797 Expression* ref = this->expr_;
11798 Location loc = this->location();
11799 if (ref->type()->points_to() != NULL)
11800 ref = Expression::make_unary(OPERATOR_MULT, ref, loc);
e440a328 11801
2387f644 11802 Expression* mtable =
11803 Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc);
11804 Struct_type* mtable_type = mtable->type()->points_to()->struct_type();
e440a328 11805
11806 std::string name = Gogo::unpack_hidden_name(this->name_);
2387f644 11807 unsigned int index;
11808 const Struct_field* field = mtable_type->find_local_field(name, &index);
11809 go_assert(field != NULL);
11810 mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc);
11811 return Expression::make_field_reference(mtable, index, loc);
e440a328 11812}
11813
2387f644 11814// Return an expression for the first argument to pass to the interface
e440a328 11815// function.
11816
2387f644 11817Expression*
11818Interface_field_reference_expression::get_underlying_object()
e440a328 11819{
2387f644 11820 Expression* expr = this->expr_;
11821 if (expr->type()->points_to() != NULL)
11822 expr = Expression::make_unary(OPERATOR_MULT, expr, this->location());
11823 return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT,
11824 this->location());
e440a328 11825}
11826
11827// Traversal.
11828
11829int
11830Interface_field_reference_expression::do_traverse(Traverse* traverse)
11831{
11832 return Expression::traverse(&this->expr_, traverse);
11833}
11834
0afbb937 11835// Lower the expression. If this expression is not called, we need to
11836// evaluate the expression twice when converting to the backend
11837// interface. So introduce a temporary variable if necessary.
11838
11839Expression*
9782d556 11840Interface_field_reference_expression::do_flatten(Gogo*, Named_object*,
11841 Statement_inserter* inserter)
0afbb937 11842{
5bf8be8b 11843 if (this->expr_->is_error_expression()
11844 || this->expr_->type()->is_error_type())
11845 {
11846 go_assert(saw_errors());
11847 return Expression::make_error(this->location());
11848 }
11849
2387f644 11850 if (!this->expr_->is_variable())
0afbb937 11851 {
11852 Temporary_statement* temp =
11853 Statement::make_temporary(this->expr_->type(), NULL, this->location());
11854 inserter->insert(temp);
11855 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_,
11856 this->location());
11857 }
11858 return this;
11859}
11860
e440a328 11861// Return the type of an interface field reference.
11862
11863Type*
11864Interface_field_reference_expression::do_type()
11865{
11866 Type* expr_type = this->expr_->type();
11867
11868 Type* points_to = expr_type->points_to();
11869 if (points_to != NULL)
11870 expr_type = points_to;
11871
11872 Interface_type* interface_type = expr_type->interface_type();
11873 if (interface_type == NULL)
11874 return Type::make_error_type();
11875
11876 const Typed_identifier* method = interface_type->find_method(this->name_);
11877 if (method == NULL)
11878 return Type::make_error_type();
11879
11880 return method->type();
11881}
11882
11883// Determine types.
11884
11885void
11886Interface_field_reference_expression::do_determine_type(const Type_context*)
11887{
11888 this->expr_->determine_type_no_context();
11889}
11890
11891// Check the types for an interface field reference.
11892
11893void
11894Interface_field_reference_expression::do_check_types(Gogo*)
11895{
11896 Type* type = this->expr_->type();
11897
11898 Type* points_to = type->points_to();
11899 if (points_to != NULL)
11900 type = points_to;
11901
11902 Interface_type* interface_type = type->interface_type();
11903 if (interface_type == NULL)
5c491127 11904 {
11905 if (!type->is_error_type())
11906 this->report_error(_("expected interface or pointer to interface"));
11907 }
e440a328 11908 else
11909 {
11910 const Typed_identifier* method =
11911 interface_type->find_method(this->name_);
11912 if (method == NULL)
11913 {
631d5788 11914 go_error_at(this->location(), "method %qs not in interface",
11915 Gogo::message_name(this->name_).c_str());
e440a328 11916 this->set_is_error();
11917 }
11918 }
11919}
11920
0afbb937 11921// If an interface field reference is not simply called, then it is
11922// represented as a closure. The closure will hold a single variable,
11923// the value of the interface on which the method should be called.
11924// The function will be a simple thunk that pulls the value from the
11925// closure and calls the method with the remaining arguments.
11926
11927// Because method values are not common, we don't build all thunks for
11928// all possible interface methods, but instead only build them as we
11929// need them. In particular, we even build them on demand for
11930// interface methods defined in other packages.
11931
11932Interface_field_reference_expression::Interface_method_thunks
11933 Interface_field_reference_expression::interface_method_thunks;
11934
11935// Find or create the thunk to call method NAME on TYPE.
11936
11937Named_object*
11938Interface_field_reference_expression::create_thunk(Gogo* gogo,
11939 Interface_type* type,
11940 const std::string& name)
11941{
11942 std::pair<Interface_type*, Method_thunks*> val(type, NULL);
11943 std::pair<Interface_method_thunks::iterator, bool> ins =
11944 Interface_field_reference_expression::interface_method_thunks.insert(val);
11945 if (ins.second)
11946 {
11947 // This is the first time we have seen this interface.
11948 ins.first->second = new Method_thunks();
11949 }
11950
11951 for (Method_thunks::const_iterator p = ins.first->second->begin();
11952 p != ins.first->second->end();
11953 p++)
11954 if (p->first == name)
11955 return p->second;
11956
11957 Location loc = type->location();
11958
11959 const Typed_identifier* method_id = type->find_method(name);
11960 if (method_id == NULL)
11961 return Named_object::make_erroneous_name(Gogo::thunk_name());
11962
11963 Function_type* orig_fntype = method_id->type()->function_type();
11964 if (orig_fntype == NULL)
11965 return Named_object::make_erroneous_name(Gogo::thunk_name());
11966
11967 Struct_field_list* sfl = new Struct_field_list();
f8bdf81a 11968 // The type here is wrong--it should be the C function type. But it
11969 // doesn't really matter.
0afbb937 11970 Type* vt = Type::make_pointer_type(Type::make_void_type());
11971 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
11972 sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc)));
6bf4793c 11973 Struct_type* st = Type::make_struct_type(sfl, loc);
11974 st->set_is_struct_incomparable();
11975 Type* closure_type = Type::make_pointer_type(st);
0afbb937 11976
f8bdf81a 11977 Function_type* new_fntype = orig_fntype->copy_with_names();
0afbb937 11978
da244e59 11979 std::string thunk_name = Gogo::thunk_name();
11980 Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
0afbb937 11981 false, loc);
11982
f8bdf81a 11983 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
11984 cvar->set_is_used();
1ecc6157 11985 cvar->set_is_closure();
da244e59 11986 Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
11987 NULL, cvar);
f8bdf81a 11988 new_no->func_value()->set_closure_var(cp);
0afbb937 11989
f8bdf81a 11990 gogo->start_block(loc);
0afbb937 11991
11992 // Field 0 of the closure is the function code pointer, field 1 is
11993 // the value on which to invoke the method.
11994 Expression* arg = Expression::make_var_reference(cp, loc);
11995 arg = Expression::make_unary(OPERATOR_MULT, arg, loc);
11996 arg = Expression::make_field_reference(arg, 1, loc);
11997
11998 Expression *ifre = Expression::make_interface_field_reference(arg, name,
11999 loc);
12000
12001 const Typed_identifier_list* orig_params = orig_fntype->parameters();
12002 Expression_list* args;
12003 if (orig_params == NULL || orig_params->empty())
12004 args = NULL;
12005 else
12006 {
12007 const Typed_identifier_list* new_params = new_fntype->parameters();
12008 args = new Expression_list();
12009 for (Typed_identifier_list::const_iterator p = new_params->begin();
f8bdf81a 12010 p != new_params->end();
0afbb937 12011 ++p)
12012 {
12013 Named_object* p_no = gogo->lookup(p->name(), NULL);
12014 go_assert(p_no != NULL
12015 && p_no->is_variable()
12016 && p_no->var_value()->is_parameter());
12017 args->push_back(Expression::make_var_reference(p_no, loc));
12018 }
12019 }
12020
12021 Call_expression* call = Expression::make_call(ifre, args,
12022 orig_fntype->is_varargs(),
12023 loc);
12024 call->set_varargs_are_lowered();
12025
12026 Statement* s = Statement::make_return_from_call(call, loc);
12027 gogo->add_statement(s);
12028 Block* b = gogo->finish_block(loc);
12029 gogo->add_block(b, loc);
12030 gogo->lower_block(new_no, b);
a32698ee 12031 gogo->flatten_block(new_no, b);
0afbb937 12032 gogo->finish_function(loc);
12033
12034 ins.first->second->push_back(std::make_pair(name, new_no));
12035 return new_no;
12036}
12037
ea664253 12038// Get the backend representation for a method value.
e440a328 12039
ea664253 12040Bexpression*
12041Interface_field_reference_expression::do_get_backend(Translate_context* context)
e440a328 12042{
0afbb937 12043 Interface_type* type = this->expr_->type()->interface_type();
12044 if (type == NULL)
12045 {
12046 go_assert(saw_errors());
ea664253 12047 return context->backend()->error_expression();
0afbb937 12048 }
12049
12050 Named_object* thunk =
12051 Interface_field_reference_expression::create_thunk(context->gogo(),
12052 type, this->name_);
12053 if (thunk->is_erroneous())
12054 {
12055 go_assert(saw_errors());
ea664253 12056 return context->backend()->error_expression();
0afbb937 12057 }
12058
12059 // FIXME: We should lower this earlier, but we can't it lower it in
12060 // the lowering pass because at that point we don't know whether we
12061 // need to create the thunk or not. If the expression is called, we
12062 // don't need the thunk.
12063
12064 Location loc = this->location();
12065
12066 Struct_field_list* fields = new Struct_field_list();
12067 fields->push_back(Struct_field(Typed_identifier("fn.0",
12068 thunk->func_value()->type(),
12069 loc)));
12070 fields->push_back(Struct_field(Typed_identifier("val.1",
12071 this->expr_->type(),
12072 loc)));
12073 Struct_type* st = Type::make_struct_type(fields, loc);
6bf4793c 12074 st->set_is_struct_incomparable();
0afbb937 12075
12076 Expression_list* vals = new Expression_list();
12077 vals->push_back(Expression::make_func_code_reference(thunk, loc));
12078 vals->push_back(this->expr_);
12079
12080 Expression* expr = Expression::make_struct_composite_literal(st, vals, loc);
ea664253 12081 Bexpression* bclosure =
12082 Expression::make_heap_expression(expr, loc)->get_backend(context);
0afbb937 12083
2387f644 12084 Expression* nil_check =
12085 Expression::make_binary(OPERATOR_EQEQ, this->expr_,
12086 Expression::make_nil(loc), loc);
ea664253 12087 Bexpression* bnil_check = nil_check->get_backend(context);
0afbb937 12088
2387f644 12089 Gogo* gogo = context->gogo();
ea664253 12090 Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
12091 loc)->get_backend(context);
2387f644 12092
93715b75 12093 Bfunction* bfn = context->function()->func_value()->get_decl();
2387f644 12094 Bexpression* bcond =
93715b75 12095 gogo->backend()->conditional_expression(bfn, NULL,
12096 bnil_check, bcrash, NULL, loc);
0ab48656 12097 Bfunction* bfunction = context->function()->func_value()->get_decl();
12098 Bstatement* cond_statement =
12099 gogo->backend()->expression_statement(bfunction, bcond);
ea664253 12100 return gogo->backend()->compound_expression(cond_statement, bclosure, loc);
e440a328 12101}
12102
d751bb78 12103// Dump ast representation for an interface field reference.
12104
12105void
12106Interface_field_reference_expression::do_dump_expression(
12107 Ast_dump_context* ast_dump_context) const
12108{
12109 this->expr_->dump_expression(ast_dump_context);
12110 ast_dump_context->ostream() << "." << this->name_;
12111}
12112
e440a328 12113// Make a reference to a field in an interface.
12114
12115Expression*
12116Expression::make_interface_field_reference(Expression* expr,
12117 const std::string& field,
b13c66cd 12118 Location location)
e440a328 12119{
12120 return new Interface_field_reference_expression(expr, field, location);
12121}
12122
12123// A general selector. This is a Parser_expression for LEFT.NAME. It
12124// is lowered after we know the type of the left hand side.
12125
12126class Selector_expression : public Parser_expression
12127{
12128 public:
12129 Selector_expression(Expression* left, const std::string& name,
b13c66cd 12130 Location location)
e440a328 12131 : Parser_expression(EXPRESSION_SELECTOR, location),
12132 left_(left), name_(name)
12133 { }
12134
12135 protected:
12136 int
12137 do_traverse(Traverse* traverse)
12138 { return Expression::traverse(&this->left_, traverse); }
12139
12140 Expression*
ceeb4318 12141 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 12142
12143 Expression*
12144 do_copy()
12145 {
12146 return new Selector_expression(this->left_->copy(), this->name_,
12147 this->location());
12148 }
12149
d751bb78 12150 void
12151 do_dump_expression(Ast_dump_context* ast_dump_context) const;
12152
e440a328 12153 private:
12154 Expression*
12155 lower_method_expression(Gogo*);
12156
12157 // The expression on the left hand side.
12158 Expression* left_;
12159 // The name on the right hand side.
12160 std::string name_;
12161};
12162
12163// Lower a selector expression once we know the real type of the left
12164// hand side.
12165
12166Expression*
ceeb4318 12167Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
12168 int)
e440a328 12169{
12170 Expression* left = this->left_;
12171 if (left->is_type_expression())
12172 return this->lower_method_expression(gogo);
12173 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
12174 this->location());
12175}
12176
12177// Lower a method expression T.M or (*T).M. We turn this into a
12178// function literal.
12179
12180Expression*
12181Selector_expression::lower_method_expression(Gogo* gogo)
12182{
b13c66cd 12183 Location location = this->location();
868b439e 12184 Type* left_type = this->left_->type();
12185 Type* type = left_type;
e440a328 12186 const std::string& name(this->name_);
12187
12188 bool is_pointer;
12189 if (type->points_to() == NULL)
12190 is_pointer = false;
12191 else
12192 {
12193 is_pointer = true;
12194 type = type->points_to();
12195 }
12196 Named_type* nt = type->named_type();
12197 if (nt == NULL)
12198 {
631d5788 12199 go_error_at(location,
12200 ("method expression requires named type or "
12201 "pointer to named type"));
e440a328 12202 return Expression::make_error(location);
12203 }
12204
12205 bool is_ambiguous;
12206 Method* method = nt->method_function(name, &is_ambiguous);
ab1468c3 12207 const Typed_identifier* imethod = NULL;
dcc8506b 12208 if (method == NULL && !is_pointer)
ab1468c3 12209 {
12210 Interface_type* it = nt->interface_type();
12211 if (it != NULL)
12212 imethod = it->find_method(name);
12213 }
12214
868b439e 12215 if ((method == NULL && imethod == NULL)
12216 || (left_type->named_type() != NULL && left_type->points_to() != NULL))
e440a328 12217 {
12218 if (!is_ambiguous)
631d5788 12219 go_error_at(location, "type %<%s%s%> has no method %<%s%>",
12220 is_pointer ? "*" : "",
12221 nt->message_name().c_str(),
12222 Gogo::message_name(name).c_str());
e440a328 12223 else
631d5788 12224 go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
12225 Gogo::message_name(name).c_str(),
12226 is_pointer ? "*" : "",
12227 nt->message_name().c_str());
e440a328 12228 return Expression::make_error(location);
12229 }
12230
ab1468c3 12231 if (method != NULL && !is_pointer && !method->is_value_method())
e440a328 12232 {
631d5788 12233 go_error_at(location, "method requires pointer (use %<(*%s).%s%>)",
12234 nt->message_name().c_str(),
12235 Gogo::message_name(name).c_str());
e440a328 12236 return Expression::make_error(location);
12237 }
12238
12239 // Build a new function type in which the receiver becomes the first
12240 // argument.
ab1468c3 12241 Function_type* method_type;
12242 if (method != NULL)
12243 {
12244 method_type = method->type();
c484d925 12245 go_assert(method_type->is_method());
ab1468c3 12246 }
12247 else
12248 {
12249 method_type = imethod->type()->function_type();
c484d925 12250 go_assert(method_type != NULL && !method_type->is_method());
ab1468c3 12251 }
e440a328 12252
12253 const char* const receiver_name = "$this";
12254 Typed_identifier_list* parameters = new Typed_identifier_list();
12255 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
12256 location));
12257
12258 const Typed_identifier_list* method_parameters = method_type->parameters();
12259 if (method_parameters != NULL)
12260 {
f470da59 12261 int i = 0;
e440a328 12262 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
12263 p != method_parameters->end();
f470da59 12264 ++p, ++i)
12265 {
68883531 12266 if (!p->name().empty())
f470da59 12267 parameters->push_back(*p);
12268 else
12269 {
12270 char buf[20];
12271 snprintf(buf, sizeof buf, "$param%d", i);
12272 parameters->push_back(Typed_identifier(buf, p->type(),
12273 p->location()));
12274 }
12275 }
e440a328 12276 }
12277
12278 const Typed_identifier_list* method_results = method_type->results();
12279 Typed_identifier_list* results;
12280 if (method_results == NULL)
12281 results = NULL;
12282 else
12283 {
12284 results = new Typed_identifier_list();
12285 for (Typed_identifier_list::const_iterator p = method_results->begin();
12286 p != method_results->end();
12287 ++p)
12288 results->push_back(*p);
12289 }
12290
12291 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
12292 location);
12293 if (method_type->is_varargs())
12294 fntype->set_is_varargs();
12295
12296 // We generate methods which always takes a pointer to the receiver
12297 // as their first argument. If this is for a pointer type, we can
12298 // simply reuse the existing function. We use an internal hack to
12299 // get the right type.
8381eda7 12300 // FIXME: This optimization is disabled because it doesn't yet work
12301 // with function descriptors when the method expression is not
12302 // directly called.
12303 if (method != NULL && is_pointer && false)
e440a328 12304 {
12305 Named_object* mno = (method->needs_stub_method()
12306 ? method->stub_object()
12307 : method->named_object());
12308 Expression* f = Expression::make_func_reference(mno, NULL, location);
12309 f = Expression::make_cast(fntype, f, location);
12310 Type_conversion_expression* tce =
12311 static_cast<Type_conversion_expression*>(f);
12312 tce->set_may_convert_function_types();
12313 return f;
12314 }
12315
12316 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
12317 location);
12318
12319 Named_object* vno = gogo->lookup(receiver_name, NULL);
c484d925 12320 go_assert(vno != NULL);
e440a328 12321 Expression* ve = Expression::make_var_reference(vno, location);
ab1468c3 12322 Expression* bm;
12323 if (method != NULL)
12324 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
12325 else
12326 bm = Expression::make_interface_field_reference(ve, name, location);
f690b0bb 12327
12328 // Even though we found the method above, if it has an error type we
12329 // may see an error here.
12330 if (bm->is_error_expression())
463fe805 12331 {
12332 gogo->finish_function(location);
12333 return bm;
12334 }
e440a328 12335
12336 Expression_list* args;
f470da59 12337 if (parameters->size() <= 1)
e440a328 12338 args = NULL;
12339 else
12340 {
12341 args = new Expression_list();
f470da59 12342 Typed_identifier_list::const_iterator p = parameters->begin();
12343 ++p;
12344 for (; p != parameters->end(); ++p)
e440a328 12345 {
12346 vno = gogo->lookup(p->name(), NULL);
c484d925 12347 go_assert(vno != NULL);
e440a328 12348 args->push_back(Expression::make_var_reference(vno, location));
12349 }
12350 }
12351
ceeb4318 12352 gogo->start_block(location);
12353
e440a328 12354 Call_expression* call = Expression::make_call(bm, args,
12355 method_type->is_varargs(),
12356 location);
12357
0afbb937 12358 Statement* s = Statement::make_return_from_call(call, location);
e440a328 12359 gogo->add_statement(s);
12360
ceeb4318 12361 Block* b = gogo->finish_block(location);
12362
12363 gogo->add_block(b, location);
12364
12365 // Lower the call in case there are multiple results.
12366 gogo->lower_block(no, b);
a32698ee 12367 gogo->flatten_block(no, b);
ceeb4318 12368
e440a328 12369 gogo->finish_function(location);
12370
12371 return Expression::make_func_reference(no, NULL, location);
12372}
12373
d751bb78 12374// Dump the ast for a selector expression.
12375
12376void
12377Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12378 const
12379{
12380 ast_dump_context->dump_expression(this->left_);
12381 ast_dump_context->ostream() << ".";
12382 ast_dump_context->ostream() << this->name_;
12383}
12384
e440a328 12385// Make a selector expression.
12386
12387Expression*
12388Expression::make_selector(Expression* left, const std::string& name,
b13c66cd 12389 Location location)
e440a328 12390{
12391 return new Selector_expression(left, name, location);
12392}
12393
da244e59 12394// Class Allocation_expression.
e440a328 12395
da244e59 12396int
12397Allocation_expression::do_traverse(Traverse* traverse)
e440a328 12398{
da244e59 12399 return Type::traverse(this->type_, traverse);
12400}
e440a328 12401
da244e59 12402Type*
12403Allocation_expression::do_type()
12404{
12405 return Type::make_pointer_type(this->type_);
12406}
e440a328 12407
da244e59 12408// Make a copy of an allocation expression.
e440a328 12409
da244e59 12410Expression*
12411Allocation_expression::do_copy()
12412{
12413 Allocation_expression* alloc =
12414 new Allocation_expression(this->type_, this->location());
12415 if (this->allocate_on_stack_)
12416 alloc->set_allocate_on_stack();
12417 return alloc;
12418}
e440a328 12419
ea664253 12420// Return the backend representation for an allocation expression.
e440a328 12421
ea664253 12422Bexpression*
12423Allocation_expression::do_get_backend(Translate_context* context)
e440a328 12424{
2c809f8f 12425 Gogo* gogo = context->gogo();
12426 Location loc = this->location();
da244e59 12427
45ff893b 12428 Node* n = Node::make_node(this);
12429 if (this->allocate_on_stack_
12430 || (n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
da244e59 12431 {
2a305b85 12432 int64_t size;
12433 bool ok = this->type_->backend_type_size(gogo, &size);
12434 if (!ok)
12435 {
12436 go_assert(saw_errors());
12437 return gogo->backend()->error_expression();
12438 }
d5d1c295 12439 return gogo->backend()->stack_allocation_expression(size, loc);
da244e59 12440 }
12441
2a305b85 12442 Btype* btype = this->type_->get_backend(gogo);
12443 Bexpression* space =
ea664253 12444 gogo->allocate_memory(this->type_, loc)->get_backend(context);
d5d1c295 12445 Btype* pbtype = gogo->backend()->pointer_type(btype);
ea664253 12446 return gogo->backend()->convert_expression(pbtype, space, loc);
e440a328 12447}
12448
d751bb78 12449// Dump ast representation for an allocation expression.
12450
12451void
12452Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12453 const
12454{
12455 ast_dump_context->ostream() << "new(";
12456 ast_dump_context->dump_type(this->type_);
12457 ast_dump_context->ostream() << ")";
12458}
12459
e440a328 12460// Make an allocation expression.
12461
12462Expression*
b13c66cd 12463Expression::make_allocation(Type* type, Location location)
e440a328 12464{
12465 return new Allocation_expression(type, location);
12466}
12467
e32de7ba 12468// Class Ordered_value_list.
e440a328 12469
12470int
e32de7ba 12471Ordered_value_list::traverse_vals(Traverse* traverse)
e440a328 12472{
0c4f5a19 12473 if (this->vals_ != NULL)
12474 {
12475 if (this->traverse_order_ == NULL)
12476 {
12477 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12478 return TRAVERSE_EXIT;
12479 }
12480 else
12481 {
e32de7ba 12482 for (std::vector<unsigned long>::const_iterator p =
12483 this->traverse_order_->begin();
0c4f5a19 12484 p != this->traverse_order_->end();
12485 ++p)
12486 {
12487 if (Expression::traverse(&this->vals_->at(*p), traverse)
12488 == TRAVERSE_EXIT)
12489 return TRAVERSE_EXIT;
12490 }
12491 }
12492 }
e32de7ba 12493 return TRAVERSE_CONTINUE;
12494}
12495
12496// Class Struct_construction_expression.
12497
12498// Traversal.
12499
12500int
12501Struct_construction_expression::do_traverse(Traverse* traverse)
12502{
12503 if (this->traverse_vals(traverse) == TRAVERSE_EXIT)
12504 return TRAVERSE_EXIT;
e440a328 12505 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12506 return TRAVERSE_EXIT;
12507 return TRAVERSE_CONTINUE;
12508}
12509
12510// Return whether this is a constant initializer.
12511
12512bool
12513Struct_construction_expression::is_constant_struct() const
12514{
e32de7ba 12515 if (this->vals() == NULL)
e440a328 12516 return true;
e32de7ba 12517 for (Expression_list::const_iterator pv = this->vals()->begin();
12518 pv != this->vals()->end();
e440a328 12519 ++pv)
12520 {
12521 if (*pv != NULL
12522 && !(*pv)->is_constant()
12523 && (!(*pv)->is_composite_literal()
12524 || (*pv)->is_nonconstant_composite_literal()))
12525 return false;
12526 }
12527
12528 const Struct_field_list* fields = this->type_->struct_type()->fields();
12529 for (Struct_field_list::const_iterator pf = fields->begin();
12530 pf != fields->end();
12531 ++pf)
12532 {
12533 // There are no constant constructors for interfaces.
12534 if (pf->type()->interface_type() != NULL)
12535 return false;
12536 }
12537
12538 return true;
12539}
12540
3ae06f68 12541// Return whether this struct can be used as a constant initializer.
f9ca30f9 12542
12543bool
3ae06f68 12544Struct_construction_expression::do_is_static_initializer() const
f9ca30f9 12545{
e32de7ba 12546 if (this->vals() == NULL)
f9ca30f9 12547 return true;
e32de7ba 12548 for (Expression_list::const_iterator pv = this->vals()->begin();
12549 pv != this->vals()->end();
f9ca30f9 12550 ++pv)
12551 {
3ae06f68 12552 if (*pv != NULL && !(*pv)->is_static_initializer())
f9ca30f9 12553 return false;
12554 }
de048538 12555
12556 const Struct_field_list* fields = this->type_->struct_type()->fields();
12557 for (Struct_field_list::const_iterator pf = fields->begin();
12558 pf != fields->end();
12559 ++pf)
12560 {
12561 // There are no constant constructors for interfaces.
12562 if (pf->type()->interface_type() != NULL)
12563 return false;
12564 }
12565
f9ca30f9 12566 return true;
12567}
12568
e440a328 12569// Final type determination.
12570
12571void
12572Struct_construction_expression::do_determine_type(const Type_context*)
12573{
e32de7ba 12574 if (this->vals() == NULL)
e440a328 12575 return;
12576 const Struct_field_list* fields = this->type_->struct_type()->fields();
e32de7ba 12577 Expression_list::const_iterator pv = this->vals()->begin();
e440a328 12578 for (Struct_field_list::const_iterator pf = fields->begin();
12579 pf != fields->end();
12580 ++pf, ++pv)
12581 {
e32de7ba 12582 if (pv == this->vals()->end())
e440a328 12583 return;
12584 if (*pv != NULL)
12585 {
12586 Type_context subcontext(pf->type(), false);
12587 (*pv)->determine_type(&subcontext);
12588 }
12589 }
a6cb4c0e 12590 // Extra values are an error we will report elsewhere; we still want
12591 // to determine the type to avoid knockon errors.
e32de7ba 12592 for (; pv != this->vals()->end(); ++pv)
a6cb4c0e 12593 (*pv)->determine_type_no_context();
e440a328 12594}
12595
12596// Check types.
12597
12598void
12599Struct_construction_expression::do_check_types(Gogo*)
12600{
e32de7ba 12601 if (this->vals() == NULL)
e440a328 12602 return;
12603
12604 Struct_type* st = this->type_->struct_type();
e32de7ba 12605 if (this->vals()->size() > st->field_count())
e440a328 12606 {
12607 this->report_error(_("too many expressions for struct"));
12608 return;
12609 }
12610
12611 const Struct_field_list* fields = st->fields();
e32de7ba 12612 Expression_list::const_iterator pv = this->vals()->begin();
e440a328 12613 int i = 0;
12614 for (Struct_field_list::const_iterator pf = fields->begin();
12615 pf != fields->end();
12616 ++pf, ++pv, ++i)
12617 {
e32de7ba 12618 if (pv == this->vals()->end())
e440a328 12619 {
12620 this->report_error(_("too few expressions for struct"));
12621 break;
12622 }
12623
12624 if (*pv == NULL)
12625 continue;
12626
12627 std::string reason;
12628 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
12629 {
12630 if (reason.empty())
631d5788 12631 go_error_at((*pv)->location(),
12632 "incompatible type for field %d in struct construction",
12633 i + 1);
e440a328 12634 else
631d5788 12635 go_error_at((*pv)->location(),
12636 ("incompatible type for field %d in "
12637 "struct construction (%s)"),
12638 i + 1, reason.c_str());
e440a328 12639 this->set_is_error();
12640 }
12641 }
e32de7ba 12642 go_assert(pv == this->vals()->end());
e440a328 12643}
12644
8ba8cc87 12645// Flatten a struct construction expression. Store the values into
12646// temporaries in case they need interface conversion.
12647
12648Expression*
12649Struct_construction_expression::do_flatten(Gogo*, Named_object*,
12650 Statement_inserter* inserter)
12651{
e32de7ba 12652 if (this->vals() == NULL)
8ba8cc87 12653 return this;
12654
12655 // If this is a constant struct, we don't need temporaries.
de048538 12656 if (this->is_constant_struct() || this->is_static_initializer())
8ba8cc87 12657 return this;
12658
12659 Location loc = this->location();
e32de7ba 12660 for (Expression_list::iterator pv = this->vals()->begin();
12661 pv != this->vals()->end();
8ba8cc87 12662 ++pv)
12663 {
12664 if (*pv != NULL)
12665 {
5bf8be8b 12666 if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type())
12667 {
12668 go_assert(saw_errors());
12669 return Expression::make_error(loc);
12670 }
8ba8cc87 12671 if (!(*pv)->is_variable())
12672 {
12673 Temporary_statement* temp =
12674 Statement::make_temporary(NULL, *pv, loc);
12675 inserter->insert(temp);
12676 *pv = Expression::make_temporary_reference(temp, loc);
12677 }
12678 }
12679 }
12680 return this;
12681}
12682
ea664253 12683// Return the backend representation for constructing a struct.
e440a328 12684
ea664253 12685Bexpression*
12686Struct_construction_expression::do_get_backend(Translate_context* context)
e440a328 12687{
12688 Gogo* gogo = context->gogo();
12689
2c809f8f 12690 Btype* btype = this->type_->get_backend(gogo);
e32de7ba 12691 if (this->vals() == NULL)
ea664253 12692 return gogo->backend()->zero_expression(btype);
e440a328 12693
e440a328 12694 const Struct_field_list* fields = this->type_->struct_type()->fields();
e32de7ba 12695 Expression_list::const_iterator pv = this->vals()->begin();
2c809f8f 12696 std::vector<Bexpression*> init;
12697 for (Struct_field_list::const_iterator pf = fields->begin();
12698 pf != fields->end();
12699 ++pf)
e440a328 12700 {
63697958 12701 Btype* fbtype = pf->type()->get_backend(gogo);
e32de7ba 12702 if (pv == this->vals()->end())
2c809f8f 12703 init.push_back(gogo->backend()->zero_expression(fbtype));
e440a328 12704 else if (*pv == NULL)
12705 {
2c809f8f 12706 init.push_back(gogo->backend()->zero_expression(fbtype));
e440a328 12707 ++pv;
12708 }
12709 else
12710 {
2c809f8f 12711 Expression* val =
12712 Expression::convert_for_assignment(gogo, pf->type(),
12713 *pv, this->location());
ea664253 12714 init.push_back(val->get_backend(context));
e440a328 12715 ++pv;
12716 }
e440a328 12717 }
ea664253 12718 return gogo->backend()->constructor_expression(btype, init, this->location());
e440a328 12719}
12720
12721// Export a struct construction.
12722
12723void
12724Struct_construction_expression::do_export(Export* exp) const
12725{
12726 exp->write_c_string("convert(");
12727 exp->write_type(this->type_);
e32de7ba 12728 for (Expression_list::const_iterator pv = this->vals()->begin();
12729 pv != this->vals()->end();
e440a328 12730 ++pv)
12731 {
12732 exp->write_c_string(", ");
12733 if (*pv != NULL)
12734 (*pv)->export_expression(exp);
12735 }
12736 exp->write_c_string(")");
12737}
12738
d751bb78 12739// Dump ast representation of a struct construction expression.
12740
12741void
12742Struct_construction_expression::do_dump_expression(
12743 Ast_dump_context* ast_dump_context) const
12744{
d751bb78 12745 ast_dump_context->dump_type(this->type_);
12746 ast_dump_context->ostream() << "{";
e32de7ba 12747 ast_dump_context->dump_expression_list(this->vals());
d751bb78 12748 ast_dump_context->ostream() << "}";
12749}
12750
e440a328 12751// Make a struct composite literal. This used by the thunk code.
12752
12753Expression*
12754Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
b13c66cd 12755 Location location)
e440a328 12756{
c484d925 12757 go_assert(type->struct_type() != NULL);
e440a328 12758 return new Struct_construction_expression(type, vals, location);
12759}
12760
da244e59 12761// Class Array_construction_expression.
e440a328 12762
12763// Traversal.
12764
12765int
12766Array_construction_expression::do_traverse(Traverse* traverse)
12767{
e32de7ba 12768 if (this->traverse_vals(traverse) == TRAVERSE_EXIT)
e440a328 12769 return TRAVERSE_EXIT;
12770 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12771 return TRAVERSE_EXIT;
12772 return TRAVERSE_CONTINUE;
12773}
12774
12775// Return whether this is a constant initializer.
12776
12777bool
12778Array_construction_expression::is_constant_array() const
12779{
e32de7ba 12780 if (this->vals() == NULL)
e440a328 12781 return true;
12782
12783 // There are no constant constructors for interfaces.
12784 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12785 return false;
12786
e32de7ba 12787 for (Expression_list::const_iterator pv = this->vals()->begin();
12788 pv != this->vals()->end();
e440a328 12789 ++pv)
12790 {
12791 if (*pv != NULL
12792 && !(*pv)->is_constant()
12793 && (!(*pv)->is_composite_literal()
12794 || (*pv)->is_nonconstant_composite_literal()))
12795 return false;
12796 }
12797 return true;
12798}
12799
3ae06f68 12800// Return whether this can be used a constant initializer.
f9ca30f9 12801
12802bool
3ae06f68 12803Array_construction_expression::do_is_static_initializer() const
f9ca30f9 12804{
e32de7ba 12805 if (this->vals() == NULL)
f9ca30f9 12806 return true;
de048538 12807
12808 // There are no constant constructors for interfaces.
12809 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12810 return false;
12811
e32de7ba 12812 for (Expression_list::const_iterator pv = this->vals()->begin();
12813 pv != this->vals()->end();
f9ca30f9 12814 ++pv)
12815 {
3ae06f68 12816 if (*pv != NULL && !(*pv)->is_static_initializer())
f9ca30f9 12817 return false;
12818 }
12819 return true;
12820}
12821
e440a328 12822// Final type determination.
12823
12824void
12825Array_construction_expression::do_determine_type(const Type_context*)
12826{
e32de7ba 12827 if (this->vals() == NULL)
e440a328 12828 return;
12829 Type_context subcontext(this->type_->array_type()->element_type(), false);
e32de7ba 12830 for (Expression_list::const_iterator pv = this->vals()->begin();
12831 pv != this->vals()->end();
e440a328 12832 ++pv)
12833 {
12834 if (*pv != NULL)
12835 (*pv)->determine_type(&subcontext);
12836 }
12837}
12838
12839// Check types.
12840
12841void
12842Array_construction_expression::do_check_types(Gogo*)
12843{
e32de7ba 12844 if (this->vals() == NULL)
e440a328 12845 return;
12846
12847 Array_type* at = this->type_->array_type();
12848 int i = 0;
12849 Type* element_type = at->element_type();
e32de7ba 12850 for (Expression_list::const_iterator pv = this->vals()->begin();
12851 pv != this->vals()->end();
e440a328 12852 ++pv, ++i)
12853 {
12854 if (*pv != NULL
12855 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12856 {
631d5788 12857 go_error_at((*pv)->location(),
12858 "incompatible type for element %d in composite literal",
12859 i + 1);
e440a328 12860 this->set_is_error();
12861 }
12862 }
e440a328 12863}
12864
8ba8cc87 12865// Flatten an array construction expression. Store the values into
12866// temporaries in case they need interface conversion.
12867
12868Expression*
12869Array_construction_expression::do_flatten(Gogo*, Named_object*,
12870 Statement_inserter* inserter)
12871{
e32de7ba 12872 if (this->vals() == NULL)
8ba8cc87 12873 return this;
12874
12875 // If this is a constant array, we don't need temporaries.
de048538 12876 if (this->is_constant_array() || this->is_static_initializer())
8ba8cc87 12877 return this;
12878
12879 Location loc = this->location();
e32de7ba 12880 for (Expression_list::iterator pv = this->vals()->begin();
12881 pv != this->vals()->end();
8ba8cc87 12882 ++pv)
12883 {
12884 if (*pv != NULL)
12885 {
5bf8be8b 12886 if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type())
12887 {
12888 go_assert(saw_errors());
12889 return Expression::make_error(loc);
12890 }
8ba8cc87 12891 if (!(*pv)->is_variable())
12892 {
12893 Temporary_statement* temp =
12894 Statement::make_temporary(NULL, *pv, loc);
12895 inserter->insert(temp);
12896 *pv = Expression::make_temporary_reference(temp, loc);
12897 }
12898 }
12899 }
12900 return this;
12901}
12902
2c809f8f 12903// Get a constructor expression for the array values.
e440a328 12904
2c809f8f 12905Bexpression*
12906Array_construction_expression::get_constructor(Translate_context* context,
12907 Btype* array_btype)
e440a328 12908{
e440a328 12909 Type* element_type = this->type_->array_type()->element_type();
2c809f8f 12910
12911 std::vector<unsigned long> indexes;
12912 std::vector<Bexpression*> vals;
12913 Gogo* gogo = context->gogo();
e32de7ba 12914 if (this->vals() != NULL)
e440a328 12915 {
12916 size_t i = 0;
ffe743ca 12917 std::vector<unsigned long>::const_iterator pi;
12918 if (this->indexes_ != NULL)
12919 pi = this->indexes_->begin();
e32de7ba 12920 for (Expression_list::const_iterator pv = this->vals()->begin();
12921 pv != this->vals()->end();
e440a328 12922 ++pv, ++i)
12923 {
ffe743ca 12924 if (this->indexes_ != NULL)
12925 go_assert(pi != this->indexes_->end());
ffe743ca 12926
12927 if (this->indexes_ == NULL)
2c809f8f 12928 indexes.push_back(i);
ffe743ca 12929 else
2c809f8f 12930 indexes.push_back(*pi);
e440a328 12931 if (*pv == NULL)
63697958 12932 {
63697958 12933 Btype* ebtype = element_type->get_backend(gogo);
12934 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
2c809f8f 12935 vals.push_back(zv);
63697958 12936 }
e440a328 12937 else
12938 {
2c809f8f 12939 Expression* val_expr =
12940 Expression::convert_for_assignment(gogo, element_type, *pv,
12941 this->location());
ea664253 12942 vals.push_back(val_expr->get_backend(context));
e440a328 12943 }
ffe743ca 12944 if (this->indexes_ != NULL)
12945 ++pi;
e440a328 12946 }
ffe743ca 12947 if (this->indexes_ != NULL)
12948 go_assert(pi == this->indexes_->end());
e440a328 12949 }
2c809f8f 12950 return gogo->backend()->array_constructor_expression(array_btype, indexes,
12951 vals, this->location());
e440a328 12952}
12953
12954// Export an array construction.
12955
12956void
12957Array_construction_expression::do_export(Export* exp) const
12958{
12959 exp->write_c_string("convert(");
12960 exp->write_type(this->type_);
e32de7ba 12961 if (this->vals() != NULL)
e440a328 12962 {
ffe743ca 12963 std::vector<unsigned long>::const_iterator pi;
12964 if (this->indexes_ != NULL)
12965 pi = this->indexes_->begin();
e32de7ba 12966 for (Expression_list::const_iterator pv = this->vals()->begin();
12967 pv != this->vals()->end();
e440a328 12968 ++pv)
12969 {
12970 exp->write_c_string(", ");
ffe743ca 12971
12972 if (this->indexes_ != NULL)
12973 {
12974 char buf[100];
12975 snprintf(buf, sizeof buf, "%lu", *pi);
12976 exp->write_c_string(buf);
12977 exp->write_c_string(":");
12978 }
12979
e440a328 12980 if (*pv != NULL)
12981 (*pv)->export_expression(exp);
ffe743ca 12982
12983 if (this->indexes_ != NULL)
12984 ++pi;
e440a328 12985 }
12986 }
12987 exp->write_c_string(")");
12988}
12989
0e9a2e72 12990// Dump ast representation of an array construction expression.
d751bb78 12991
12992void
12993Array_construction_expression::do_dump_expression(
12994 Ast_dump_context* ast_dump_context) const
12995{
ffe743ca 12996 Expression* length = this->type_->array_type()->length();
8b1c301d 12997
12998 ast_dump_context->ostream() << "[" ;
12999 if (length != NULL)
13000 {
13001 ast_dump_context->dump_expression(length);
13002 }
13003 ast_dump_context->ostream() << "]" ;
d751bb78 13004 ast_dump_context->dump_type(this->type_);
0e9a2e72 13005 this->dump_slice_storage_expression(ast_dump_context);
d751bb78 13006 ast_dump_context->ostream() << "{" ;
ffe743ca 13007 if (this->indexes_ == NULL)
e32de7ba 13008 ast_dump_context->dump_expression_list(this->vals());
ffe743ca 13009 else
13010 {
e32de7ba 13011 Expression_list::const_iterator pv = this->vals()->begin();
ffe743ca 13012 for (std::vector<unsigned long>::const_iterator pi =
13013 this->indexes_->begin();
13014 pi != this->indexes_->end();
13015 ++pi, ++pv)
13016 {
13017 if (pi != this->indexes_->begin())
13018 ast_dump_context->ostream() << ", ";
13019 ast_dump_context->ostream() << *pi << ':';
13020 ast_dump_context->dump_expression(*pv);
13021 }
13022 }
d751bb78 13023 ast_dump_context->ostream() << "}" ;
13024
13025}
13026
da244e59 13027// Class Fixed_array_construction_expression.
e440a328 13028
da244e59 13029Fixed_array_construction_expression::Fixed_array_construction_expression(
13030 Type* type, const std::vector<unsigned long>* indexes,
13031 Expression_list* vals, Location location)
13032 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
13033 type, indexes, vals, location)
13034{ go_assert(type->array_type() != NULL && !type->is_slice_type()); }
e440a328 13035
ea664253 13036// Return the backend representation for constructing a fixed array.
e440a328 13037
ea664253 13038Bexpression*
13039Fixed_array_construction_expression::do_get_backend(Translate_context* context)
e440a328 13040{
9f0e0513 13041 Type* type = this->type();
13042 Btype* btype = type->get_backend(context->gogo());
ea664253 13043 return this->get_constructor(context, btype);
e440a328 13044}
13045
76f85fd6 13046Expression*
13047Expression::make_array_composite_literal(Type* type, Expression_list* vals,
13048 Location location)
13049{
13050 go_assert(type->array_type() != NULL && !type->is_slice_type());
13051 return new Fixed_array_construction_expression(type, NULL, vals, location);
13052}
13053
da244e59 13054// Class Slice_construction_expression.
e440a328 13055
da244e59 13056Slice_construction_expression::Slice_construction_expression(
13057 Type* type, const std::vector<unsigned long>* indexes,
13058 Expression_list* vals, Location location)
13059 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION,
13060 type, indexes, vals, location),
0e9a2e72 13061 valtype_(NULL), array_val_(NULL), slice_storage_(NULL),
13062 storage_escapes_(true)
e440a328 13063{
da244e59 13064 go_assert(type->is_slice_type());
23d77f91 13065
da244e59 13066 unsigned long lenval;
13067 Expression* length;
13068 if (vals == NULL || vals->empty())
13069 lenval = 0;
13070 else
13071 {
13072 if (this->indexes() == NULL)
13073 lenval = vals->size();
13074 else
13075 lenval = indexes->back() + 1;
13076 }
13077 Type* int_type = Type::lookup_integer_type("int");
13078 length = Expression::make_integer_ul(lenval, int_type, location);
13079 Type* element_type = type->array_type()->element_type();
6bf4793c 13080 Array_type* array_type = Type::make_array_type(element_type, length);
13081 array_type->set_is_array_incomparable();
13082 this->valtype_ = array_type;
da244e59 13083}
e440a328 13084
23d77f91 13085// Traversal.
13086
13087int
13088Slice_construction_expression::do_traverse(Traverse* traverse)
13089{
13090 if (this->Array_construction_expression::do_traverse(traverse)
13091 == TRAVERSE_EXIT)
13092 return TRAVERSE_EXIT;
13093 if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT)
13094 return TRAVERSE_EXIT;
0e9a2e72 13095 if (this->array_val_ != NULL
13096 && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT)
13097 return TRAVERSE_EXIT;
13098 if (this->slice_storage_ != NULL
13099 && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT)
13100 return TRAVERSE_EXIT;
23d77f91 13101 return TRAVERSE_CONTINUE;
13102}
13103
0e9a2e72 13104// Helper routine to create fixed array value underlying the slice literal.
13105// May be called during flattening, or later during do_get_backend().
e440a328 13106
0e9a2e72 13107Expression*
13108Slice_construction_expression::create_array_val()
e440a328 13109{
f9c68f17 13110 Array_type* array_type = this->type()->array_type();
13111 if (array_type == NULL)
13112 {
c484d925 13113 go_assert(this->type()->is_error());
0e9a2e72 13114 return NULL;
f9c68f17 13115 }
13116
f23d7786 13117 Location loc = this->location();
23d77f91 13118 go_assert(this->valtype_ != NULL);
3d60812e 13119
f23d7786 13120 Expression_list* vals = this->vals();
0e9a2e72 13121 return new Fixed_array_construction_expression(
13122 this->valtype_, this->indexes(), vals, loc);
13123}
13124
13125// If we're previous established that the slice storage does not
13126// escape, then create a separate array temp val here for it. We
13127// need to do this as part of flattening so as to be able to insert
13128// the new temp statement.
13129
13130Expression*
13131Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no,
13132 Statement_inserter* inserter)
13133{
13134 if (this->type()->array_type() == NULL)
13135 return NULL;
13136
13137 // Base class flattening first
13138 this->Array_construction_expression::do_flatten(gogo, no, inserter);
13139
a1bbc2c3 13140 // Create a stack-allocated storage temp if storage won't escape
03118c21 13141 if (!this->storage_escapes_
13142 && this->slice_storage_ == NULL
13143 && this->element_count() > 0)
0e9a2e72 13144 {
13145 Location loc = this->location();
03118c21 13146 this->array_val_ = this->create_array_val();
0e9a2e72 13147 go_assert(this->array_val_);
13148 Temporary_statement* temp =
13149 Statement::make_temporary(this->valtype_, this->array_val_, loc);
13150 inserter->insert(temp);
13151 this->slice_storage_ = Expression::make_temporary_reference(temp, loc);
13152 }
13153 return this;
13154}
13155
13156// When dumping a slice construction expression that has an explicit
13157// storeage temp, emit the temp here (if we don't do this the storage
13158// temp appears unused in the AST dump).
13159
13160void
13161Slice_construction_expression::
13162dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const
13163{
13164 if (this->slice_storage_ == NULL)
13165 return;
13166 ast_dump_context->ostream() << "storage=" ;
13167 ast_dump_context->dump_expression(this->slice_storage_);
13168}
13169
13170// Return the backend representation for constructing a slice.
13171
13172Bexpression*
13173Slice_construction_expression::do_get_backend(Translate_context* context)
13174{
13175 if (this->array_val_ == NULL)
03118c21 13176 this->array_val_ = this->create_array_val();
0e9a2e72 13177 if (this->array_val_ == NULL)
13178 {
13179 go_assert(this->type()->is_error());
13180 return context->backend()->error_expression();
13181 }
13182
13183 Location loc = this->location();
e440a328 13184
3ae06f68 13185 bool is_static_initializer = this->array_val_->is_static_initializer();
d8829beb 13186
13187 // We have to copy the initial values into heap memory if we are in
3ae06f68 13188 // a function or if the values are not constants.
13189 bool copy_to_heap = context->function() != NULL || !is_static_initializer;
e440a328 13190
f23d7786 13191 Expression* space;
0e9a2e72 13192
13193 if (this->slice_storage_ != NULL)
13194 {
13195 go_assert(!this->storage_escapes_);
13196 space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc);
13197 }
13198 else if (!copy_to_heap)
e440a328 13199 {
f23d7786 13200 // The initializer will only run once.
0e9a2e72 13201 space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc);
f23d7786 13202 space->unary_expression()->set_is_slice_init();
e440a328 13203 }
13204 else
45ff893b 13205 {
0e9a2e72 13206 space = Expression::make_heap_expression(this->array_val_, loc);
45ff893b 13207 Node* n = Node::make_node(this);
13208 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
13209 {
13210 n = Node::make_node(space);
13211 n->set_encoding(Node::ESCAPE_NONE);
13212 }
13213 }
e440a328 13214
2c809f8f 13215 // Build a constructor for the slice.
f23d7786 13216 Expression* len = this->valtype_->array_type()->length();
13217 Expression* slice_val =
13218 Expression::make_slice_value(this->type(), space, len, len, loc);
ea664253 13219 return slice_val->get_backend(context);
e440a328 13220}
13221
13222// Make a slice composite literal. This is used by the type
13223// descriptor code.
13224
0e9a2e72 13225Slice_construction_expression*
e440a328 13226Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
b13c66cd 13227 Location location)
e440a328 13228{
411eb89e 13229 go_assert(type->is_slice_type());
2c809f8f 13230 return new Slice_construction_expression(type, NULL, vals, location);
e440a328 13231}
13232
da244e59 13233// Class Map_construction_expression.
e440a328 13234
13235// Traversal.
13236
13237int
13238Map_construction_expression::do_traverse(Traverse* traverse)
13239{
13240 if (this->vals_ != NULL
13241 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
13242 return TRAVERSE_EXIT;
13243 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13244 return TRAVERSE_EXIT;
13245 return TRAVERSE_CONTINUE;
13246}
13247
2c809f8f 13248// Flatten constructor initializer into a temporary variable since
13249// we need to take its address for __go_construct_map.
13250
13251Expression*
13252Map_construction_expression::do_flatten(Gogo* gogo, Named_object*,
13253 Statement_inserter* inserter)
13254{
13255 if (!this->is_error_expression()
13256 && this->vals_ != NULL
13257 && !this->vals_->empty()
13258 && this->constructor_temp_ == NULL)
13259 {
13260 Map_type* mt = this->type_->map_type();
13261 Type* key_type = mt->key_type();
13262 Type* val_type = mt->val_type();
13263 this->element_type_ = Type::make_builtin_struct_type(2,
13264 "__key", key_type,
13265 "__val", val_type);
13266
13267 Expression_list* value_pairs = new Expression_list();
13268 Location loc = this->location();
13269
13270 size_t i = 0;
13271 for (Expression_list::const_iterator pv = this->vals_->begin();
13272 pv != this->vals_->end();
13273 ++pv, ++i)
13274 {
13275 Expression_list* key_value_pair = new Expression_list();
91c0fd76 13276 Expression* key = *pv;
5bf8be8b 13277 if (key->is_error_expression() || key->type()->is_error_type())
13278 {
13279 go_assert(saw_errors());
13280 return Expression::make_error(loc);
13281 }
91c0fd76 13282 if (key->type()->interface_type() != NULL && !key->is_variable())
13283 {
13284 Temporary_statement* temp =
13285 Statement::make_temporary(NULL, key, loc);
13286 inserter->insert(temp);
13287 key = Expression::make_temporary_reference(temp, loc);
13288 }
13289 key = Expression::convert_for_assignment(gogo, key_type, key, loc);
2c809f8f 13290
13291 ++pv;
91c0fd76 13292 Expression* val = *pv;
5bf8be8b 13293 if (val->is_error_expression() || val->type()->is_error_type())
13294 {
13295 go_assert(saw_errors());
13296 return Expression::make_error(loc);
13297 }
91c0fd76 13298 if (val->type()->interface_type() != NULL && !val->is_variable())
13299 {
13300 Temporary_statement* temp =
13301 Statement::make_temporary(NULL, val, loc);
13302 inserter->insert(temp);
13303 val = Expression::make_temporary_reference(temp, loc);
13304 }
13305 val = Expression::convert_for_assignment(gogo, val_type, val, loc);
2c809f8f 13306
13307 key_value_pair->push_back(key);
13308 key_value_pair->push_back(val);
13309 value_pairs->push_back(
13310 Expression::make_struct_composite_literal(this->element_type_,
13311 key_value_pair, loc));
13312 }
13313
e67508fa 13314 Expression* element_count = Expression::make_integer_ul(i, NULL, loc);
6bf4793c 13315 Array_type* ctor_type =
2c809f8f 13316 Type::make_array_type(this->element_type_, element_count);
6bf4793c 13317 ctor_type->set_is_array_incomparable();
2c809f8f 13318 Expression* constructor =
13319 new Fixed_array_construction_expression(ctor_type, NULL,
13320 value_pairs, loc);
13321
13322 this->constructor_temp_ =
13323 Statement::make_temporary(NULL, constructor, loc);
13324 constructor->issue_nil_check();
13325 this->constructor_temp_->set_is_address_taken();
13326 inserter->insert(this->constructor_temp_);
13327 }
13328
13329 return this;
13330}
13331
e440a328 13332// Final type determination.
13333
13334void
13335Map_construction_expression::do_determine_type(const Type_context*)
13336{
13337 if (this->vals_ == NULL)
13338 return;
13339
13340 Map_type* mt = this->type_->map_type();
13341 Type_context key_context(mt->key_type(), false);
13342 Type_context val_context(mt->val_type(), false);
13343 for (Expression_list::const_iterator pv = this->vals_->begin();
13344 pv != this->vals_->end();
13345 ++pv)
13346 {
13347 (*pv)->determine_type(&key_context);
13348 ++pv;
13349 (*pv)->determine_type(&val_context);
13350 }
13351}
13352
13353// Check types.
13354
13355void
13356Map_construction_expression::do_check_types(Gogo*)
13357{
13358 if (this->vals_ == NULL)
13359 return;
13360
13361 Map_type* mt = this->type_->map_type();
13362 int i = 0;
13363 Type* key_type = mt->key_type();
13364 Type* val_type = mt->val_type();
13365 for (Expression_list::const_iterator pv = this->vals_->begin();
13366 pv != this->vals_->end();
13367 ++pv, ++i)
13368 {
13369 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
13370 {
631d5788 13371 go_error_at((*pv)->location(),
13372 "incompatible type for element %d key in map construction",
13373 i + 1);
e440a328 13374 this->set_is_error();
13375 }
13376 ++pv;
13377 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
13378 {
631d5788 13379 go_error_at((*pv)->location(),
13380 ("incompatible type for element %d value "
13381 "in map construction"),
e440a328 13382 i + 1);
13383 this->set_is_error();
13384 }
13385 }
13386}
13387
ea664253 13388// Return the backend representation for constructing a map.
e440a328 13389
ea664253 13390Bexpression*
13391Map_construction_expression::do_get_backend(Translate_context* context)
e440a328 13392{
2c809f8f 13393 if (this->is_error_expression())
ea664253 13394 return context->backend()->error_expression();
2c809f8f 13395 Location loc = this->location();
e440a328 13396
e440a328 13397 size_t i = 0;
2c809f8f 13398 Expression* ventries;
e440a328 13399 if (this->vals_ == NULL || this->vals_->empty())
2c809f8f 13400 ventries = Expression::make_nil(loc);
e440a328 13401 else
13402 {
2c809f8f 13403 go_assert(this->constructor_temp_ != NULL);
13404 i = this->vals_->size() / 2;
e440a328 13405
2c809f8f 13406 Expression* ctor_ref =
13407 Expression::make_temporary_reference(this->constructor_temp_, loc);
13408 ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc);
13409 }
e440a328 13410
2c809f8f 13411 Map_type* mt = this->type_->map_type();
13412 if (this->element_type_ == NULL)
13413 this->element_type_ =
13414 Type::make_builtin_struct_type(2,
13415 "__key", mt->key_type(),
13416 "__val", mt->val_type());
0d5530d9 13417 Expression* descriptor = Expression::make_type_descriptor(mt, loc);
2c809f8f 13418
13419 Type* uintptr_t = Type::lookup_integer_type("uintptr");
e67508fa 13420 Expression* count = Expression::make_integer_ul(i, uintptr_t, loc);
2c809f8f 13421
13422 Expression* entry_size =
13423 Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE);
13424
13425 unsigned int field_index;
13426 const Struct_field* valfield =
13427 this->element_type_->find_local_field("__val", &field_index);
13428 Expression* val_offset =
13429 Expression::make_struct_field_offset(this->element_type_, valfield);
2c809f8f 13430
13431 Expression* map_ctor =
0d5530d9 13432 Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count,
13433 entry_size, val_offset, ventries);
ea664253 13434 return map_ctor->get_backend(context);
2c809f8f 13435}
e440a328 13436
2c809f8f 13437// Export an array construction.
e440a328 13438
2c809f8f 13439void
13440Map_construction_expression::do_export(Export* exp) const
13441{
13442 exp->write_c_string("convert(");
13443 exp->write_type(this->type_);
13444 for (Expression_list::const_iterator pv = this->vals_->begin();
13445 pv != this->vals_->end();
13446 ++pv)
13447 {
13448 exp->write_c_string(", ");
13449 (*pv)->export_expression(exp);
13450 }
13451 exp->write_c_string(")");
13452}
e440a328 13453
2c809f8f 13454// Dump ast representation for a map construction expression.
d751bb78 13455
13456void
13457Map_construction_expression::do_dump_expression(
13458 Ast_dump_context* ast_dump_context) const
13459{
d751bb78 13460 ast_dump_context->ostream() << "{" ;
8b1c301d 13461 ast_dump_context->dump_expression_list(this->vals_, true);
d751bb78 13462 ast_dump_context->ostream() << "}";
13463}
13464
7795ac51 13465// Class Composite_literal_expression.
e440a328 13466
13467// Traversal.
13468
13469int
13470Composite_literal_expression::do_traverse(Traverse* traverse)
13471{
dbffccfc 13472 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
e440a328 13473 return TRAVERSE_EXIT;
dbffccfc 13474
13475 // If this is a struct composite literal with keys, then the keys
13476 // are field names, not expressions. We don't want to traverse them
13477 // in that case. If we do, we can give an erroneous error "variable
13478 // initializer refers to itself." See bug482.go in the testsuite.
13479 if (this->has_keys_ && this->vals_ != NULL)
13480 {
13481 // The type may not be resolvable at this point.
13482 Type* type = this->type_;
a01f2481 13483
7795ac51 13484 for (int depth = 0; depth < this->depth_; ++depth)
a01f2481 13485 {
13486 if (type->array_type() != NULL)
13487 type = type->array_type()->element_type();
13488 else if (type->map_type() != NULL)
7795ac51 13489 {
13490 if (this->key_path_[depth])
13491 type = type->map_type()->key_type();
13492 else
13493 type = type->map_type()->val_type();
13494 }
a01f2481 13495 else
13496 {
13497 // This error will be reported during lowering.
13498 return TRAVERSE_CONTINUE;
13499 }
13500 }
13501
dbffccfc 13502 while (true)
13503 {
13504 if (type->classification() == Type::TYPE_NAMED)
13505 type = type->named_type()->real_type();
13506 else if (type->classification() == Type::TYPE_FORWARD)
13507 {
13508 Type* t = type->forwarded();
13509 if (t == type)
13510 break;
13511 type = t;
13512 }
13513 else
13514 break;
13515 }
13516
13517 if (type->classification() == Type::TYPE_STRUCT)
13518 {
13519 Expression_list::iterator p = this->vals_->begin();
13520 while (p != this->vals_->end())
13521 {
13522 // Skip key.
13523 ++p;
13524 go_assert(p != this->vals_->end());
13525 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13526 return TRAVERSE_EXIT;
13527 ++p;
13528 }
13529 return TRAVERSE_CONTINUE;
13530 }
13531 }
13532
13533 if (this->vals_ != NULL)
13534 return this->vals_->traverse(traverse);
13535
13536 return TRAVERSE_CONTINUE;
e440a328 13537}
13538
13539// Lower a generic composite literal into a specific version based on
13540// the type.
13541
13542Expression*
ceeb4318 13543Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
13544 Statement_inserter* inserter, int)
e440a328 13545{
13546 Type* type = this->type_;
13547
7795ac51 13548 for (int depth = 0; depth < this->depth_; ++depth)
e440a328 13549 {
13550 if (type->array_type() != NULL)
13551 type = type->array_type()->element_type();
13552 else if (type->map_type() != NULL)
7795ac51 13553 {
13554 if (this->key_path_[depth])
13555 type = type->map_type()->key_type();
13556 else
13557 type = type->map_type()->val_type();
13558 }
e440a328 13559 else
13560 {
5c13bd80 13561 if (!type->is_error())
631d5788 13562 go_error_at(this->location(),
13563 ("may only omit types within composite literals "
13564 "of slice, array, or map type"));
e440a328 13565 return Expression::make_error(this->location());
13566 }
13567 }
13568
e00772b3 13569 Type *pt = type->points_to();
13570 bool is_pointer = false;
13571 if (pt != NULL)
13572 {
13573 is_pointer = true;
13574 type = pt;
13575 }
13576
13577 Expression* ret;
5c13bd80 13578 if (type->is_error())
e440a328 13579 return Expression::make_error(this->location());
13580 else if (type->struct_type() != NULL)
e00772b3 13581 ret = this->lower_struct(gogo, type);
e440a328 13582 else if (type->array_type() != NULL)
113ef6a5 13583 ret = this->lower_array(type);
e440a328 13584 else if (type->map_type() != NULL)
e00772b3 13585 ret = this->lower_map(gogo, function, inserter, type);
e440a328 13586 else
13587 {
631d5788 13588 go_error_at(this->location(),
13589 ("expected struct, slice, array, or map type "
13590 "for composite literal"));
e440a328 13591 return Expression::make_error(this->location());
13592 }
e00772b3 13593
13594 if (is_pointer)
2c809f8f 13595 ret = Expression::make_heap_expression(ret, this->location());
e00772b3 13596
13597 return ret;
e440a328 13598}
13599
13600// Lower a struct composite literal.
13601
13602Expression*
81c4b26b 13603Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
e440a328 13604{
b13c66cd 13605 Location location = this->location();
e440a328 13606 Struct_type* st = type->struct_type();
13607 if (this->vals_ == NULL || !this->has_keys_)
07daa4e7 13608 {
e6013c28 13609 if (this->vals_ != NULL
13610 && !this->vals_->empty()
13611 && type->named_type() != NULL
13612 && type->named_type()->named_object()->package() != NULL)
13613 {
13614 for (Struct_field_list::const_iterator pf = st->fields()->begin();
13615 pf != st->fields()->end();
13616 ++pf)
07daa4e7 13617 {
07ba7f26 13618 if (Gogo::is_hidden_name(pf->field_name())
13619 || pf->is_embedded_builtin(gogo))
631d5788 13620 go_error_at(this->location(),
13621 "assignment of unexported field %qs in %qs literal",
13622 Gogo::message_name(pf->field_name()).c_str(),
13623 type->named_type()->message_name().c_str());
07daa4e7 13624 }
13625 }
13626
13627 return new Struct_construction_expression(type, this->vals_, location);
13628 }
e440a328 13629
13630 size_t field_count = st->field_count();
13631 std::vector<Expression*> vals(field_count);
e32de7ba 13632 std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>);
e440a328 13633 Expression_list::const_iterator p = this->vals_->begin();
62750cd5 13634 Expression* external_expr = NULL;
13635 const Named_object* external_no = NULL;
e440a328 13636 while (p != this->vals_->end())
13637 {
13638 Expression* name_expr = *p;
13639
13640 ++p;
c484d925 13641 go_assert(p != this->vals_->end());
e440a328 13642 Expression* val = *p;
13643
13644 ++p;
13645
13646 if (name_expr == NULL)
13647 {
631d5788 13648 go_error_at(val->location(),
13649 "mixture of field and value initializers");
e440a328 13650 return Expression::make_error(location);
13651 }
13652
13653 bool bad_key = false;
13654 std::string name;
81c4b26b 13655 const Named_object* no = NULL;
e440a328 13656 switch (name_expr->classification())
13657 {
13658 case EXPRESSION_UNKNOWN_REFERENCE:
13659 name = name_expr->unknown_expression()->name();
7f7ce694 13660 if (type->named_type() != NULL)
13661 {
13662 // If the named object found for this field name comes from a
13663 // different package than the struct it is a part of, do not count
13664 // this incorrect lookup as a usage of the object's package.
13665 no = name_expr->unknown_expression()->named_object();
13666 if (no->package() != NULL
13667 && no->package() != type->named_type()->named_object()->package())
13668 no->package()->forget_usage(name_expr);
13669 }
e440a328 13670 break;
13671
13672 case EXPRESSION_CONST_REFERENCE:
81c4b26b 13673 no = static_cast<Const_expression*>(name_expr)->named_object();
e440a328 13674 break;
13675
13676 case EXPRESSION_TYPE:
13677 {
13678 Type* t = name_expr->type();
13679 Named_type* nt = t->named_type();
13680 if (nt == NULL)
13681 bad_key = true;
13682 else
81c4b26b 13683 no = nt->named_object();
e440a328 13684 }
13685 break;
13686
13687 case EXPRESSION_VAR_REFERENCE:
81c4b26b 13688 no = name_expr->var_expression()->named_object();
e440a328 13689 break;
13690
b0c09712 13691 case EXPRESSION_ENCLOSED_VAR_REFERENCE:
13692 no = name_expr->enclosed_var_expression()->variable();
e440a328 13693 break;
13694
b0c09712 13695 case EXPRESSION_FUNC_REFERENCE:
13696 no = name_expr->func_expression()->named_object();
e440a328 13697 break;
13698
13699 default:
13700 bad_key = true;
13701 break;
13702 }
13703 if (bad_key)
13704 {
631d5788 13705 go_error_at(name_expr->location(), "expected struct field name");
e440a328 13706 return Expression::make_error(location);
13707 }
13708
81c4b26b 13709 if (no != NULL)
13710 {
62750cd5 13711 if (no->package() != NULL && external_expr == NULL)
13712 {
13713 external_expr = name_expr;
13714 external_no = no;
13715 }
13716
81c4b26b 13717 name = no->name();
13718
13719 // A predefined name won't be packed. If it starts with a
13720 // lower case letter we need to check for that case, because
2d29d278 13721 // the field name will be packed. FIXME.
81c4b26b 13722 if (!Gogo::is_hidden_name(name)
13723 && name[0] >= 'a'
13724 && name[0] <= 'z')
13725 {
13726 Named_object* gno = gogo->lookup_global(name.c_str());
13727 if (gno == no)
13728 name = gogo->pack_hidden_name(name, false);
13729 }
13730 }
13731
e440a328 13732 unsigned int index;
13733 const Struct_field* sf = st->find_local_field(name, &index);
13734 if (sf == NULL)
13735 {
631d5788 13736 go_error_at(name_expr->location(), "unknown field %qs in %qs",
13737 Gogo::message_name(name).c_str(),
13738 (type->named_type() != NULL
13739 ? type->named_type()->message_name().c_str()
13740 : "unnamed struct"));
e440a328 13741 return Expression::make_error(location);
13742 }
13743 if (vals[index] != NULL)
13744 {
631d5788 13745 go_error_at(name_expr->location(),
13746 "duplicate value for field %qs in %qs",
13747 Gogo::message_name(name).c_str(),
13748 (type->named_type() != NULL
13749 ? type->named_type()->message_name().c_str()
13750 : "unnamed struct"));
e440a328 13751 return Expression::make_error(location);
13752 }
13753
07daa4e7 13754 if (type->named_type() != NULL
13755 && type->named_type()->named_object()->package() != NULL
07ba7f26 13756 && (Gogo::is_hidden_name(sf->field_name())
13757 || sf->is_embedded_builtin(gogo)))
631d5788 13758 go_error_at(name_expr->location(),
13759 "assignment of unexported field %qs in %qs literal",
13760 Gogo::message_name(sf->field_name()).c_str(),
13761 type->named_type()->message_name().c_str());
07daa4e7 13762
e440a328 13763 vals[index] = val;
e32de7ba 13764 traverse_order->push_back(static_cast<unsigned long>(index));
e440a328 13765 }
13766
62750cd5 13767 if (!this->all_are_names_)
13768 {
13769 // This is a weird case like bug462 in the testsuite.
13770 if (external_expr == NULL)
631d5788 13771 go_error_at(this->location(), "unknown field in %qs literal",
13772 (type->named_type() != NULL
13773 ? type->named_type()->message_name().c_str()
13774 : "unnamed struct"));
62750cd5 13775 else
631d5788 13776 go_error_at(external_expr->location(), "unknown field %qs in %qs",
13777 external_no->message_name().c_str(),
13778 (type->named_type() != NULL
13779 ? type->named_type()->message_name().c_str()
13780 : "unnamed struct"));
62750cd5 13781 return Expression::make_error(location);
13782 }
13783
e440a328 13784 Expression_list* list = new Expression_list;
13785 list->reserve(field_count);
13786 for (size_t i = 0; i < field_count; ++i)
13787 list->push_back(vals[i]);
13788
0c4f5a19 13789 Struct_construction_expression* ret =
13790 new Struct_construction_expression(type, list, location);
13791 ret->set_traverse_order(traverse_order);
13792 return ret;
e440a328 13793}
13794
e32de7ba 13795// Index/value/traversal-order triple.
00773463 13796
e32de7ba 13797struct IVT_triple {
13798 unsigned long index;
13799 unsigned long traversal_order;
13800 Expression* expr;
13801 IVT_triple(unsigned long i, unsigned long to, Expression *e)
13802 : index(i), traversal_order(to), expr(e) { }
13803 bool operator<(const IVT_triple& other) const
13804 { return this->index < other.index; }
00773463 13805};
13806
e440a328 13807// Lower an array composite literal.
13808
13809Expression*
113ef6a5 13810Composite_literal_expression::lower_array(Type* type)
e440a328 13811{
b13c66cd 13812 Location location = this->location();
e440a328 13813 if (this->vals_ == NULL || !this->has_keys_)
ffe743ca 13814 return this->make_array(type, NULL, this->vals_);
e440a328 13815
ffe743ca 13816 std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
13817 indexes->reserve(this->vals_->size());
00773463 13818 bool indexes_out_of_order = false;
ffe743ca 13819 Expression_list* vals = new Expression_list();
13820 vals->reserve(this->vals_->size());
e440a328 13821 unsigned long index = 0;
13822 Expression_list::const_iterator p = this->vals_->begin();
13823 while (p != this->vals_->end())
13824 {
13825 Expression* index_expr = *p;
13826
13827 ++p;
c484d925 13828 go_assert(p != this->vals_->end());
e440a328 13829 Expression* val = *p;
13830
13831 ++p;
13832
ffe743ca 13833 if (index_expr == NULL)
13834 {
13835 if (!indexes->empty())
13836 indexes->push_back(index);
13837 }
13838 else
e440a328 13839 {
ffe743ca 13840 if (indexes->empty() && !vals->empty())
13841 {
13842 for (size_t i = 0; i < vals->size(); ++i)
13843 indexes->push_back(i);
13844 }
13845
0c77715b 13846 Numeric_constant nc;
13847 if (!index_expr->numeric_constant_value(&nc))
e440a328 13848 {
631d5788 13849 go_error_at(index_expr->location(),
13850 "index expression is not integer constant");
e440a328 13851 return Expression::make_error(location);
13852 }
6f6d9955 13853
0c77715b 13854 switch (nc.to_unsigned_long(&index))
e440a328 13855 {
0c77715b 13856 case Numeric_constant::NC_UL_VALID:
13857 break;
13858 case Numeric_constant::NC_UL_NOTINT:
631d5788 13859 go_error_at(index_expr->location(),
13860 "index expression is not integer constant");
0c77715b 13861 return Expression::make_error(location);
13862 case Numeric_constant::NC_UL_NEGATIVE:
631d5788 13863 go_error_at(index_expr->location(),
13864 "index expression is negative");
e440a328 13865 return Expression::make_error(location);
0c77715b 13866 case Numeric_constant::NC_UL_BIG:
631d5788 13867 go_error_at(index_expr->location(), "index value overflow");
e440a328 13868 return Expression::make_error(location);
0c77715b 13869 default:
13870 go_unreachable();
e440a328 13871 }
6f6d9955 13872
13873 Named_type* ntype = Type::lookup_integer_type("int");
13874 Integer_type* inttype = ntype->integer_type();
0c77715b 13875 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8)
13876 && index >> (inttype->bits() - 1) != 0)
6f6d9955 13877 {
631d5788 13878 go_error_at(index_expr->location(), "index value overflow");
6f6d9955 13879 return Expression::make_error(location);
13880 }
13881
ffe743ca 13882 if (std::find(indexes->begin(), indexes->end(), index)
13883 != indexes->end())
e440a328 13884 {
631d5788 13885 go_error_at(index_expr->location(),
13886 "duplicate value for index %lu",
13887 index);
e440a328 13888 return Expression::make_error(location);
13889 }
ffe743ca 13890
00773463 13891 if (!indexes->empty() && index < indexes->back())
13892 indexes_out_of_order = true;
13893
ffe743ca 13894 indexes->push_back(index);
e440a328 13895 }
13896
ffe743ca 13897 vals->push_back(val);
13898
e440a328 13899 ++index;
13900 }
13901
ffe743ca 13902 if (indexes->empty())
13903 {
13904 delete indexes;
13905 indexes = NULL;
13906 }
e440a328 13907
e32de7ba 13908 std::vector<unsigned long>* traverse_order = NULL;
00773463 13909 if (indexes_out_of_order)
13910 {
e32de7ba 13911 typedef std::vector<IVT_triple> V;
00773463 13912
13913 V v;
13914 v.reserve(indexes->size());
13915 std::vector<unsigned long>::const_iterator pi = indexes->begin();
e32de7ba 13916 unsigned long torder = 0;
00773463 13917 for (Expression_list::const_iterator pe = vals->begin();
13918 pe != vals->end();
e32de7ba 13919 ++pe, ++pi, ++torder)
13920 v.push_back(IVT_triple(*pi, torder, *pe));
00773463 13921
e32de7ba 13922 std::sort(v.begin(), v.end());
00773463 13923
13924 delete indexes;
13925 delete vals;
e32de7ba 13926
00773463 13927 indexes = new std::vector<unsigned long>();
13928 indexes->reserve(v.size());
13929 vals = new Expression_list();
13930 vals->reserve(v.size());
e32de7ba 13931 traverse_order = new std::vector<unsigned long>();
13932 traverse_order->reserve(v.size());
00773463 13933
13934 for (V::const_iterator p = v.begin(); p != v.end(); ++p)
13935 {
e32de7ba 13936 indexes->push_back(p->index);
13937 vals->push_back(p->expr);
13938 traverse_order->push_back(p->traversal_order);
00773463 13939 }
13940 }
13941
e32de7ba 13942 Expression* ret = this->make_array(type, indexes, vals);
13943 Array_construction_expression* ace = ret->array_literal();
13944 if (ace != NULL && traverse_order != NULL)
13945 ace->set_traverse_order(traverse_order);
13946 return ret;
e440a328 13947}
13948
13949// Actually build the array composite literal. This handles
13950// [...]{...}.
13951
13952Expression*
ffe743ca 13953Composite_literal_expression::make_array(
13954 Type* type,
13955 const std::vector<unsigned long>* indexes,
13956 Expression_list* vals)
e440a328 13957{
b13c66cd 13958 Location location = this->location();
e440a328 13959 Array_type* at = type->array_type();
ffe743ca 13960
e440a328 13961 if (at->length() != NULL && at->length()->is_nil_expression())
13962 {
ffe743ca 13963 size_t size;
13964 if (vals == NULL)
13965 size = 0;
00773463 13966 else if (indexes != NULL)
13967 size = indexes->back() + 1;
13968 else
ffe743ca 13969 {
13970 size = vals->size();
13971 Integer_type* it = Type::lookup_integer_type("int")->integer_type();
13972 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8)
13973 && size >> (it->bits() - 1) != 0)
13974 {
631d5788 13975 go_error_at(location, "too many elements in composite literal");
ffe743ca 13976 return Expression::make_error(location);
13977 }
13978 }
ffe743ca 13979
e67508fa 13980 Expression* elen = Expression::make_integer_ul(size, NULL, location);
e440a328 13981 at = Type::make_array_type(at->element_type(), elen);
13982 type = at;
13983 }
ffe743ca 13984 else if (at->length() != NULL
13985 && !at->length()->is_error_expression()
13986 && this->vals_ != NULL)
13987 {
13988 Numeric_constant nc;
13989 unsigned long val;
13990 if (at->length()->numeric_constant_value(&nc)
13991 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
13992 {
13993 if (indexes == NULL)
13994 {
13995 if (this->vals_->size() > val)
13996 {
631d5788 13997 go_error_at(location,
13998 "too many elements in composite literal");
ffe743ca 13999 return Expression::make_error(location);
14000 }
14001 }
14002 else
14003 {
00773463 14004 unsigned long max = indexes->back();
ffe743ca 14005 if (max >= val)
14006 {
631d5788 14007 go_error_at(location,
14008 ("some element keys in composite literal "
14009 "are out of range"));
ffe743ca 14010 return Expression::make_error(location);
14011 }
14012 }
14013 }
14014 }
14015
e440a328 14016 if (at->length() != NULL)
ffe743ca 14017 return new Fixed_array_construction_expression(type, indexes, vals,
14018 location);
e440a328 14019 else
2c809f8f 14020 return new Slice_construction_expression(type, indexes, vals, location);
e440a328 14021}
14022
14023// Lower a map composite literal.
14024
14025Expression*
a287720d 14026Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
ceeb4318 14027 Statement_inserter* inserter,
a287720d 14028 Type* type)
e440a328 14029{
b13c66cd 14030 Location location = this->location();
e440a328 14031 if (this->vals_ != NULL)
14032 {
14033 if (!this->has_keys_)
14034 {
631d5788 14035 go_error_at(location, "map composite literal must have keys");
e440a328 14036 return Expression::make_error(location);
14037 }
14038
a287720d 14039 for (Expression_list::iterator p = this->vals_->begin();
e440a328 14040 p != this->vals_->end();
14041 p += 2)
14042 {
14043 if (*p == NULL)
14044 {
14045 ++p;
631d5788 14046 go_error_at((*p)->location(),
14047 ("map composite literal must "
14048 "have keys for every value"));
e440a328 14049 return Expression::make_error(location);
14050 }
a287720d 14051 // Make sure we have lowered the key; it may not have been
14052 // lowered in order to handle keys for struct composite
14053 // literals. Lower it now to get the right error message.
14054 if ((*p)->unknown_expression() != NULL)
14055 {
14056 (*p)->unknown_expression()->clear_is_composite_literal_key();
ceeb4318 14057 gogo->lower_expression(function, inserter, &*p);
c484d925 14058 go_assert((*p)->is_error_expression());
a287720d 14059 return Expression::make_error(location);
14060 }
e440a328 14061 }
14062 }
14063
14064 return new Map_construction_expression(type, this->vals_, location);
14065}
14066
d751bb78 14067// Dump ast representation for a composite literal expression.
14068
14069void
14070Composite_literal_expression::do_dump_expression(
14071 Ast_dump_context* ast_dump_context) const
14072{
8b1c301d 14073 ast_dump_context->ostream() << "composite(";
d751bb78 14074 ast_dump_context->dump_type(this->type_);
14075 ast_dump_context->ostream() << ", {";
8b1c301d 14076 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
d751bb78 14077 ast_dump_context->ostream() << "})";
14078}
14079
e440a328 14080// Make a composite literal expression.
14081
14082Expression*
14083Expression::make_composite_literal(Type* type, int depth, bool has_keys,
62750cd5 14084 Expression_list* vals, bool all_are_names,
b13c66cd 14085 Location location)
e440a328 14086{
14087 return new Composite_literal_expression(type, depth, has_keys, vals,
62750cd5 14088 all_are_names, location);
e440a328 14089}
14090
14091// Return whether this expression is a composite literal.
14092
14093bool
14094Expression::is_composite_literal() const
14095{
14096 switch (this->classification_)
14097 {
14098 case EXPRESSION_COMPOSITE_LITERAL:
14099 case EXPRESSION_STRUCT_CONSTRUCTION:
14100 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
2c809f8f 14101 case EXPRESSION_SLICE_CONSTRUCTION:
e440a328 14102 case EXPRESSION_MAP_CONSTRUCTION:
14103 return true;
14104 default:
14105 return false;
14106 }
14107}
14108
14109// Return whether this expression is a composite literal which is not
14110// constant.
14111
14112bool
14113Expression::is_nonconstant_composite_literal() const
14114{
14115 switch (this->classification_)
14116 {
14117 case EXPRESSION_STRUCT_CONSTRUCTION:
14118 {
14119 const Struct_construction_expression *psce =
14120 static_cast<const Struct_construction_expression*>(this);
14121 return !psce->is_constant_struct();
14122 }
14123 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
14124 {
14125 const Fixed_array_construction_expression *pace =
14126 static_cast<const Fixed_array_construction_expression*>(this);
14127 return !pace->is_constant_array();
14128 }
2c809f8f 14129 case EXPRESSION_SLICE_CONSTRUCTION:
e440a328 14130 {
2c809f8f 14131 const Slice_construction_expression *pace =
14132 static_cast<const Slice_construction_expression*>(this);
e440a328 14133 return !pace->is_constant_array();
14134 }
14135 case EXPRESSION_MAP_CONSTRUCTION:
14136 return true;
14137 default:
14138 return false;
14139 }
14140}
14141
35a54f17 14142// Return true if this is a variable or temporary_variable.
14143
14144bool
14145Expression::is_variable() const
14146{
14147 switch (this->classification_)
14148 {
14149 case EXPRESSION_VAR_REFERENCE:
14150 case EXPRESSION_TEMPORARY_REFERENCE:
14151 case EXPRESSION_SET_AND_USE_TEMPORARY:
b0c09712 14152 case EXPRESSION_ENCLOSED_VAR_REFERENCE:
35a54f17 14153 return true;
14154 default:
14155 return false;
14156 }
14157}
14158
e440a328 14159// Return true if this is a reference to a local variable.
14160
14161bool
14162Expression::is_local_variable() const
14163{
14164 const Var_expression* ve = this->var_expression();
14165 if (ve == NULL)
14166 return false;
14167 const Named_object* no = ve->named_object();
14168 return (no->is_result_variable()
14169 || (no->is_variable() && !no->var_value()->is_global()));
14170}
14171
14172// Class Type_guard_expression.
14173
14174// Traversal.
14175
14176int
14177Type_guard_expression::do_traverse(Traverse* traverse)
14178{
14179 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
14180 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
14181 return TRAVERSE_EXIT;
14182 return TRAVERSE_CONTINUE;
14183}
14184
2c809f8f 14185Expression*
14186Type_guard_expression::do_flatten(Gogo*, Named_object*,
14187 Statement_inserter* inserter)
14188{
5bf8be8b 14189 if (this->expr_->is_error_expression()
14190 || this->expr_->type()->is_error_type())
14191 {
14192 go_assert(saw_errors());
14193 return Expression::make_error(this->location());
14194 }
14195
2c809f8f 14196 if (!this->expr_->is_variable())
14197 {
14198 Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_,
14199 this->location());
14200 inserter->insert(temp);
14201 this->expr_ =
14202 Expression::make_temporary_reference(temp, this->location());
14203 }
14204 return this;
14205}
14206
e440a328 14207// Check types of a type guard expression. The expression must have
14208// an interface type, but the actual type conversion is checked at run
14209// time.
14210
14211void
14212Type_guard_expression::do_check_types(Gogo*)
14213{
e440a328 14214 Type* expr_type = this->expr_->type();
7e9da23f 14215 if (expr_type->interface_type() == NULL)
f725ade8 14216 {
5c13bd80 14217 if (!expr_type->is_error() && !this->type_->is_error())
f725ade8 14218 this->report_error(_("type assertion only valid for interface types"));
14219 this->set_is_error();
14220 }
e440a328 14221 else if (this->type_->interface_type() == NULL)
14222 {
14223 std::string reason;
14224 if (!expr_type->interface_type()->implements_interface(this->type_,
14225 &reason))
14226 {
5c13bd80 14227 if (!this->type_->is_error())
e440a328 14228 {
f725ade8 14229 if (reason.empty())
14230 this->report_error(_("impossible type assertion: "
14231 "type does not implement interface"));
14232 else
631d5788 14233 go_error_at(this->location(),
14234 ("impossible type assertion: "
14235 "type does not implement interface (%s)"),
14236 reason.c_str());
e440a328 14237 }
f725ade8 14238 this->set_is_error();
e440a328 14239 }
14240 }
14241}
14242
ea664253 14243// Return the backend representation for a type guard expression.
e440a328 14244
ea664253 14245Bexpression*
14246Type_guard_expression::do_get_backend(Translate_context* context)
e440a328 14247{
2c809f8f 14248 Expression* conversion;
7e9da23f 14249 if (this->type_->interface_type() != NULL)
2c809f8f 14250 conversion =
14251 Expression::convert_interface_to_interface(this->type_, this->expr_,
14252 true, this->location());
e440a328 14253 else
2c809f8f 14254 conversion =
14255 Expression::convert_for_assignment(context->gogo(), this->type_,
14256 this->expr_, this->location());
14257
ea664253 14258 return conversion->get_backend(context);
e440a328 14259}
14260
d751bb78 14261// Dump ast representation for a type guard expression.
14262
14263void
2c809f8f 14264Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
d751bb78 14265 const
14266{
14267 this->expr_->dump_expression(ast_dump_context);
14268 ast_dump_context->ostream() << ".";
14269 ast_dump_context->dump_type(this->type_);
14270}
14271
e440a328 14272// Make a type guard expression.
14273
14274Expression*
14275Expression::make_type_guard(Expression* expr, Type* type,
b13c66cd 14276 Location location)
e440a328 14277{
14278 return new Type_guard_expression(expr, type, location);
14279}
14280
2c809f8f 14281// Class Heap_expression.
e440a328 14282
da244e59 14283// Return the type of the expression stored on the heap.
e440a328 14284
da244e59 14285Type*
14286Heap_expression::do_type()
14287{ return Type::make_pointer_type(this->expr_->type()); }
e440a328 14288
ea664253 14289// Return the backend representation for allocating an expression on the heap.
e440a328 14290
ea664253 14291Bexpression*
14292Heap_expression::do_get_backend(Translate_context* context)
e440a328 14293{
03118c21 14294 Type* etype = this->expr_->type();
14295 if (this->expr_->is_error_expression() || etype->is_error())
ea664253 14296 return context->backend()->error_expression();
2c809f8f 14297
02c19a1a 14298 Location loc = this->location();
2c809f8f 14299 Gogo* gogo = context->gogo();
02c19a1a 14300 Btype* btype = this->type()->get_backend(gogo);
45ff893b 14301
03118c21 14302 Expression* alloc = Expression::make_allocation(etype, loc);
45ff893b 14303 Node* n = Node::make_node(this);
14304 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
14305 alloc->allocation_expression()->set_allocate_on_stack();
14306 Bexpression* space = alloc->get_backend(context);
02c19a1a 14307
14308 Bstatement* decl;
14309 Named_object* fn = context->function();
14310 go_assert(fn != NULL);
14311 Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn);
14312 Bvariable* space_temp =
14313 gogo->backend()->temporary_variable(fndecl, context->bblock(), btype,
14314 space, true, loc, &decl);
03118c21 14315 Btype* expr_btype = etype->get_backend(gogo);
02c19a1a 14316
ea664253 14317 Bexpression* bexpr = this->expr_->get_backend(context);
03118c21 14318
14319 // If this assignment needs a write barrier, call typedmemmove. We
14320 // don't do this in the write barrier pass because in some cases
14321 // backend conversion can introduce new Heap_expression values.
14322 Bstatement* assn;
14323 if (!etype->has_pointer())
14324 {
14325 space = gogo->backend()->var_expression(space_temp, VE_lvalue, loc);
14326 Bexpression* ref =
14327 gogo->backend()->indirect_expression(expr_btype, space, true, loc);
14328 assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc);
14329 }
14330 else
14331 {
14332 Bstatement* edecl;
14333 Bvariable* btemp =
14334 gogo->backend()->temporary_variable(fndecl, context->bblock(),
14335 expr_btype, bexpr, true, loc,
14336 &edecl);
14337 Bexpression* btempref = gogo->backend()->var_expression(btemp,
14338 VE_lvalue, loc);
14339 Bexpression* addr = gogo->backend()->address_expression(btempref, loc);
14340
14341 Expression* td = Expression::make_type_descriptor(etype, loc);
14342 Type* etype_ptr = Type::make_pointer_type(etype);
14343 space = gogo->backend()->var_expression(space_temp, VE_rvalue, loc);
14344 Expression* elhs = Expression::make_backend(space, etype_ptr, loc);
14345 Expression* erhs = Expression::make_backend(addr, etype_ptr, loc);
14346 Expression* call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3,
14347 td, elhs, erhs);
14348 Bexpression* bcall = call->get_backend(context);
14349 Bstatement* s = gogo->backend()->expression_statement(fndecl, bcall);
14350 assn = gogo->backend()->compound_statement(edecl, s);
14351 }
02c19a1a 14352 decl = gogo->backend()->compound_statement(decl, assn);
d4e6573e 14353 space = gogo->backend()->var_expression(space_temp, VE_rvalue, loc);
ea664253 14354 return gogo->backend()->compound_expression(decl, space, loc);
e440a328 14355}
14356
2c809f8f 14357// Dump ast representation for a heap expression.
d751bb78 14358
14359void
2c809f8f 14360Heap_expression::do_dump_expression(
d751bb78 14361 Ast_dump_context* ast_dump_context) const
14362{
14363 ast_dump_context->ostream() << "&(";
14364 ast_dump_context->dump_expression(this->expr_);
14365 ast_dump_context->ostream() << ")";
14366}
14367
2c809f8f 14368// Allocate an expression on the heap.
e440a328 14369
14370Expression*
2c809f8f 14371Expression::make_heap_expression(Expression* expr, Location location)
e440a328 14372{
2c809f8f 14373 return new Heap_expression(expr, location);
e440a328 14374}
14375
14376// Class Receive_expression.
14377
14378// Return the type of a receive expression.
14379
14380Type*
14381Receive_expression::do_type()
14382{
e429e3bd 14383 if (this->is_error_expression())
14384 return Type::make_error_type();
e440a328 14385 Channel_type* channel_type = this->channel_->type()->channel_type();
14386 if (channel_type == NULL)
e429e3bd 14387 {
14388 this->report_error(_("expected channel"));
14389 return Type::make_error_type();
14390 }
e440a328 14391 return channel_type->element_type();
14392}
14393
14394// Check types for a receive expression.
14395
14396void
14397Receive_expression::do_check_types(Gogo*)
14398{
14399 Type* type = this->channel_->type();
5c13bd80 14400 if (type->is_error())
e440a328 14401 {
e429e3bd 14402 go_assert(saw_errors());
e440a328 14403 this->set_is_error();
14404 return;
14405 }
14406 if (type->channel_type() == NULL)
14407 {
14408 this->report_error(_("expected channel"));
14409 return;
14410 }
14411 if (!type->channel_type()->may_receive())
14412 {
14413 this->report_error(_("invalid receive on send-only channel"));
14414 return;
14415 }
14416}
14417
2c809f8f 14418// Flattening for receive expressions creates a temporary variable to store
14419// received data in for receives.
14420
14421Expression*
14422Receive_expression::do_flatten(Gogo*, Named_object*,
14423 Statement_inserter* inserter)
14424{
14425 Channel_type* channel_type = this->channel_->type()->channel_type();
14426 if (channel_type == NULL)
14427 {
14428 go_assert(saw_errors());
14429 return this;
14430 }
5bf8be8b 14431 else if (this->channel_->is_error_expression())
14432 {
14433 go_assert(saw_errors());
14434 return Expression::make_error(this->location());
14435 }
2c809f8f 14436
14437 Type* element_type = channel_type->element_type();
14438 if (this->temp_receiver_ == NULL)
14439 {
14440 this->temp_receiver_ = Statement::make_temporary(element_type, NULL,
14441 this->location());
14442 this->temp_receiver_->set_is_address_taken();
14443 inserter->insert(this->temp_receiver_);
14444 }
14445
14446 return this;
14447}
14448
ea664253 14449// Get the backend representation for a receive expression.
e440a328 14450
ea664253 14451Bexpression*
14452Receive_expression::do_get_backend(Translate_context* context)
e440a328 14453{
f24f10bb 14454 Location loc = this->location();
14455
e440a328 14456 Channel_type* channel_type = this->channel_->type()->channel_type();
5b8368f4 14457 if (channel_type == NULL)
14458 {
c484d925 14459 go_assert(this->channel_->type()->is_error());
ea664253 14460 return context->backend()->error_expression();
5b8368f4 14461 }
f24f10bb 14462 Expression* td = Expression::make_type_descriptor(channel_type, loc);
e440a328 14463
2c809f8f 14464 Expression* recv_ref =
14465 Expression::make_temporary_reference(this->temp_receiver_, loc);
14466 Expression* recv_addr =
14467 Expression::make_temporary_reference(this->temp_receiver_, loc);
14468 recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc);
132ed071 14469 Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 3,
14470 td, this->channel_, recv_addr);
ea664253 14471 return Expression::make_compound(recv, recv_ref, loc)->get_backend(context);
e440a328 14472}
14473
d751bb78 14474// Dump ast representation for a receive expression.
14475
14476void
14477Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
14478{
14479 ast_dump_context->ostream() << " <- " ;
14480 ast_dump_context->dump_expression(channel_);
14481}
14482
e440a328 14483// Make a receive expression.
14484
14485Receive_expression*
b13c66cd 14486Expression::make_receive(Expression* channel, Location location)
e440a328 14487{
14488 return new Receive_expression(channel, location);
14489}
14490
e440a328 14491// An expression which evaluates to a pointer to the type descriptor
14492// of a type.
14493
14494class Type_descriptor_expression : public Expression
14495{
14496 public:
b13c66cd 14497 Type_descriptor_expression(Type* type, Location location)
e440a328 14498 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
14499 type_(type)
14500 { }
14501
14502 protected:
4b686186 14503 int
14504 do_traverse(Traverse*);
14505
e440a328 14506 Type*
14507 do_type()
14508 { return Type::make_type_descriptor_ptr_type(); }
14509
f9ca30f9 14510 bool
3ae06f68 14511 do_is_static_initializer() const
f9ca30f9 14512 { return true; }
14513
e440a328 14514 void
14515 do_determine_type(const Type_context*)
14516 { }
14517
14518 Expression*
14519 do_copy()
14520 { return this; }
14521
ea664253 14522 Bexpression*
14523 do_get_backend(Translate_context* context)
a1d23b41 14524 {
ea664253 14525 return this->type_->type_descriptor_pointer(context->gogo(),
14526 this->location());
a1d23b41 14527 }
e440a328 14528
d751bb78 14529 void
14530 do_dump_expression(Ast_dump_context*) const;
14531
e440a328 14532 private:
14533 // The type for which this is the descriptor.
14534 Type* type_;
14535};
14536
4b686186 14537int
14538Type_descriptor_expression::do_traverse(Traverse* traverse)
14539{
14540 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
14541 return TRAVERSE_EXIT;
14542 return TRAVERSE_CONTINUE;
14543}
14544
d751bb78 14545// Dump ast representation for a type descriptor expression.
14546
14547void
14548Type_descriptor_expression::do_dump_expression(
14549 Ast_dump_context* ast_dump_context) const
14550{
14551 ast_dump_context->dump_type(this->type_);
14552}
14553
e440a328 14554// Make a type descriptor expression.
14555
14556Expression*
b13c66cd 14557Expression::make_type_descriptor(Type* type, Location location)
e440a328 14558{
14559 return new Type_descriptor_expression(type, location);
14560}
14561
aa5ae575 14562// An expression which evaluates to a pointer to the Garbage Collection symbol
14563// of a type.
14564
14565class GC_symbol_expression : public Expression
14566{
14567 public:
14568 GC_symbol_expression(Type* type)
14569 : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()),
14570 type_(type)
14571 {}
14572
14573 protected:
14574 Type*
14575 do_type()
03118c21 14576 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
aa5ae575 14577
14578 bool
3ae06f68 14579 do_is_static_initializer() const
aa5ae575 14580 { return true; }
14581
14582 void
14583 do_determine_type(const Type_context*)
14584 { }
14585
14586 Expression*
14587 do_copy()
14588 { return this; }
14589
14590 Bexpression*
14591 do_get_backend(Translate_context* context)
14592 { return this->type_->gc_symbol_pointer(context->gogo()); }
14593
14594 void
14595 do_dump_expression(Ast_dump_context*) const;
14596
14597 private:
14598 // The type which this gc symbol describes.
14599 Type* type_;
14600};
14601
14602// Dump ast representation for a gc symbol expression.
14603
14604void
14605GC_symbol_expression::do_dump_expression(
14606 Ast_dump_context* ast_dump_context) const
14607{
14608 ast_dump_context->ostream() << "gcdata(";
14609 ast_dump_context->dump_type(this->type_);
14610 ast_dump_context->ostream() << ")";
14611}
14612
14613// Make a gc symbol expression.
14614
14615Expression*
14616Expression::make_gc_symbol(Type* type)
14617{
14618 return new GC_symbol_expression(type);
14619}
14620
03118c21 14621// An expression that evaluates to a pointer to a symbol holding the
14622// ptrmask data of a type.
14623
14624class Ptrmask_symbol_expression : public Expression
14625{
14626 public:
14627 Ptrmask_symbol_expression(Type* type)
14628 : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()),
14629 type_(type)
14630 {}
14631
14632 protected:
14633 Type*
14634 do_type()
14635 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
14636
14637 bool
14638 do_is_static_initializer() const
14639 { return true; }
14640
14641 void
14642 do_determine_type(const Type_context*)
14643 { }
14644
14645 Expression*
14646 do_copy()
14647 { return this; }
14648
14649 Bexpression*
14650 do_get_backend(Translate_context*);
14651
14652 void
14653 do_dump_expression(Ast_dump_context*) const;
14654
14655 private:
14656 // The type that this ptrmask symbol describes.
14657 Type* type_;
14658};
14659
14660// Return the ptrmask variable.
14661
14662Bexpression*
14663Ptrmask_symbol_expression::do_get_backend(Translate_context* context)
14664{
14665 Gogo* gogo = context->gogo();
14666
14667 // If this type does not need a gcprog, then we can use the standard
14668 // GC symbol.
14669 int64_t ptrsize, ptrdata;
14670 if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata))
14671 return this->type_->gc_symbol_pointer(gogo);
14672
14673 // Otherwise we have to build a ptrmask variable, and return a
14674 // pointer to it.
14675
14676 Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata);
14677 Location bloc = Linemap::predeclared_location();
14678 Bexpression* bref = gogo->backend()->var_expression(bvar, VE_rvalue, bloc);
14679 Bexpression* baddr = gogo->backend()->address_expression(bref, bloc);
14680
14681 Type* uint8_type = Type::lookup_integer_type("uint8");
14682 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
14683 Btype* ubtype = pointer_uint8_type->get_backend(gogo);
14684 return gogo->backend()->convert_expression(ubtype, baddr, bloc);
14685}
14686
14687// Dump AST for a ptrmask symbol expression.
14688
14689void
14690Ptrmask_symbol_expression::do_dump_expression(
14691 Ast_dump_context* ast_dump_context) const
14692{
14693 ast_dump_context->ostream() << "ptrmask(";
14694 ast_dump_context->dump_type(this->type_);
14695 ast_dump_context->ostream() << ")";
14696}
14697
14698// Make a ptrmask symbol expression.
14699
14700Expression*
14701Expression::make_ptrmask_symbol(Type* type)
14702{
14703 return new Ptrmask_symbol_expression(type);
14704}
14705
e440a328 14706// An expression which evaluates to some characteristic of a type.
14707// This is only used to initialize fields of a type descriptor. Using
14708// a new expression class is slightly inefficient but gives us a good
14709// separation between the frontend and the middle-end with regard to
14710// how types are laid out.
14711
14712class Type_info_expression : public Expression
14713{
14714 public:
14715 Type_info_expression(Type* type, Type_info type_info)
b13c66cd 14716 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
e440a328 14717 type_(type), type_info_(type_info)
14718 { }
14719
14720 protected:
0e168074 14721 bool
3ae06f68 14722 do_is_static_initializer() const
0e168074 14723 { return true; }
14724
e440a328 14725 Type*
14726 do_type();
14727
14728 void
14729 do_determine_type(const Type_context*)
14730 { }
14731
14732 Expression*
14733 do_copy()
14734 { return this; }
14735
ea664253 14736 Bexpression*
14737 do_get_backend(Translate_context* context);
e440a328 14738
d751bb78 14739 void
14740 do_dump_expression(Ast_dump_context*) const;
14741
e440a328 14742 private:
14743 // The type for which we are getting information.
14744 Type* type_;
14745 // What information we want.
14746 Type_info type_info_;
14747};
14748
14749// The type is chosen to match what the type descriptor struct
14750// expects.
14751
14752Type*
14753Type_info_expression::do_type()
14754{
14755 switch (this->type_info_)
14756 {
14757 case TYPE_INFO_SIZE:
03118c21 14758 case TYPE_INFO_BACKEND_PTRDATA:
14759 case TYPE_INFO_DESCRIPTOR_PTRDATA:
e440a328 14760 return Type::lookup_integer_type("uintptr");
14761 case TYPE_INFO_ALIGNMENT:
14762 case TYPE_INFO_FIELD_ALIGNMENT:
14763 return Type::lookup_integer_type("uint8");
14764 default:
c3e6f413 14765 go_unreachable();
e440a328 14766 }
14767}
14768
ea664253 14769// Return the backend representation for type information.
e440a328 14770
ea664253 14771Bexpression*
14772Type_info_expression::do_get_backend(Translate_context* context)
e440a328 14773{
927a01eb 14774 Gogo* gogo = context->gogo();
2a305b85 14775 bool ok = true;
3f378015 14776 int64_t val;
927a01eb 14777 switch (this->type_info_)
e440a328 14778 {
927a01eb 14779 case TYPE_INFO_SIZE:
2a305b85 14780 ok = this->type_->backend_type_size(gogo, &val);
927a01eb 14781 break;
14782 case TYPE_INFO_ALIGNMENT:
2a305b85 14783 ok = this->type_->backend_type_align(gogo, &val);
927a01eb 14784 break;
14785 case TYPE_INFO_FIELD_ALIGNMENT:
2a305b85 14786 ok = this->type_->backend_type_field_align(gogo, &val);
927a01eb 14787 break;
03118c21 14788 case TYPE_INFO_BACKEND_PTRDATA:
14789 ok = this->type_->backend_type_ptrdata(gogo, &val);
14790 break;
14791 case TYPE_INFO_DESCRIPTOR_PTRDATA:
14792 ok = this->type_->descriptor_ptrdata(gogo, &val);
14793 break;
927a01eb 14794 default:
14795 go_unreachable();
e440a328 14796 }
2a305b85 14797 if (!ok)
14798 {
14799 go_assert(saw_errors());
14800 return gogo->backend()->error_expression();
14801 }
3f378015 14802 Expression* e = Expression::make_integer_int64(val, this->type(),
14803 this->location());
14804 return e->get_backend(context);
e440a328 14805}
14806
d751bb78 14807// Dump ast representation for a type info expression.
14808
14809void
14810Type_info_expression::do_dump_expression(
14811 Ast_dump_context* ast_dump_context) const
14812{
14813 ast_dump_context->ostream() << "typeinfo(";
14814 ast_dump_context->dump_type(this->type_);
14815 ast_dump_context->ostream() << ",";
14816 ast_dump_context->ostream() <<
14817 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
14818 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
03118c21 14819 : this->type_info_ == TYPE_INFO_SIZE ? "size"
14820 : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata"
14821 : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata"
d751bb78 14822 : "unknown");
14823 ast_dump_context->ostream() << ")";
14824}
14825
e440a328 14826// Make a type info expression.
14827
14828Expression*
14829Expression::make_type_info(Type* type, Type_info type_info)
14830{
14831 return new Type_info_expression(type, type_info);
14832}
14833
35a54f17 14834// An expression that evaluates to some characteristic of a slice.
14835// This is used when indexing, bound-checking, or nil checking a slice.
14836
14837class Slice_info_expression : public Expression
14838{
14839 public:
14840 Slice_info_expression(Expression* slice, Slice_info slice_info,
14841 Location location)
14842 : Expression(EXPRESSION_SLICE_INFO, location),
14843 slice_(slice), slice_info_(slice_info)
14844 { }
14845
14846 protected:
14847 Type*
14848 do_type();
14849
14850 void
14851 do_determine_type(const Type_context*)
14852 { }
14853
14854 Expression*
14855 do_copy()
14856 {
14857 return new Slice_info_expression(this->slice_->copy(), this->slice_info_,
14858 this->location());
14859 }
14860
ea664253 14861 Bexpression*
14862 do_get_backend(Translate_context* context);
35a54f17 14863
14864 void
14865 do_dump_expression(Ast_dump_context*) const;
14866
14867 void
14868 do_issue_nil_check()
14869 { this->slice_->issue_nil_check(); }
14870
14871 private:
14872 // The slice for which we are getting information.
14873 Expression* slice_;
14874 // What information we want.
14875 Slice_info slice_info_;
14876};
14877
14878// Return the type of the slice info.
14879
14880Type*
14881Slice_info_expression::do_type()
14882{
14883 switch (this->slice_info_)
14884 {
14885 case SLICE_INFO_VALUE_POINTER:
14886 return Type::make_pointer_type(
14887 this->slice_->type()->array_type()->element_type());
14888 case SLICE_INFO_LENGTH:
14889 case SLICE_INFO_CAPACITY:
14890 return Type::lookup_integer_type("int");
14891 default:
14892 go_unreachable();
14893 }
14894}
14895
ea664253 14896// Return the backend information for slice information.
35a54f17 14897
ea664253 14898Bexpression*
14899Slice_info_expression::do_get_backend(Translate_context* context)
35a54f17 14900{
14901 Gogo* gogo = context->gogo();
ea664253 14902 Bexpression* bslice = this->slice_->get_backend(context);
35a54f17 14903 switch (this->slice_info_)
14904 {
14905 case SLICE_INFO_VALUE_POINTER:
14906 case SLICE_INFO_LENGTH:
14907 case SLICE_INFO_CAPACITY:
ea664253 14908 return gogo->backend()->struct_field_expression(bslice, this->slice_info_,
14909 this->location());
35a54f17 14910 break;
14911 default:
14912 go_unreachable();
14913 }
35a54f17 14914}
14915
14916// Dump ast representation for a type info expression.
14917
14918void
14919Slice_info_expression::do_dump_expression(
14920 Ast_dump_context* ast_dump_context) const
14921{
14922 ast_dump_context->ostream() << "sliceinfo(";
14923 this->slice_->dump_expression(ast_dump_context);
14924 ast_dump_context->ostream() << ",";
14925 ast_dump_context->ostream() <<
14926 (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values"
14927 : this->slice_info_ == SLICE_INFO_LENGTH ? "length"
14928 : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity "
14929 : "unknown");
14930 ast_dump_context->ostream() << ")";
14931}
14932
14933// Make a slice info expression.
14934
14935Expression*
14936Expression::make_slice_info(Expression* slice, Slice_info slice_info,
14937 Location location)
14938{
14939 return new Slice_info_expression(slice, slice_info, location);
14940}
14941
2c809f8f 14942// An expression that represents a slice value: a struct with value pointer,
14943// length, and capacity fields.
14944
14945class Slice_value_expression : public Expression
14946{
14947 public:
14948 Slice_value_expression(Type* type, Expression* valptr, Expression* len,
14949 Expression* cap, Location location)
14950 : Expression(EXPRESSION_SLICE_VALUE, location),
14951 type_(type), valptr_(valptr), len_(len), cap_(cap)
14952 { }
14953
14954 protected:
14955 int
14956 do_traverse(Traverse*);
14957
14958 Type*
14959 do_type()
14960 { return this->type_; }
14961
14962 void
14963 do_determine_type(const Type_context*)
14964 { go_unreachable(); }
14965
14966 Expression*
14967 do_copy()
14968 {
14969 return new Slice_value_expression(this->type_, this->valptr_->copy(),
14970 this->len_->copy(), this->cap_->copy(),
14971 this->location());
14972 }
14973
ea664253 14974 Bexpression*
14975 do_get_backend(Translate_context* context);
2c809f8f 14976
14977 void
14978 do_dump_expression(Ast_dump_context*) const;
14979
14980 private:
14981 // The type of the slice value.
14982 Type* type_;
14983 // The pointer to the values in the slice.
14984 Expression* valptr_;
14985 // The length of the slice.
14986 Expression* len_;
14987 // The capacity of the slice.
14988 Expression* cap_;
14989};
14990
14991int
14992Slice_value_expression::do_traverse(Traverse* traverse)
14993{
55e8ba6a 14994 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT
14995 || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT
2c809f8f 14996 || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT
14997 || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
14998 return TRAVERSE_EXIT;
14999 return TRAVERSE_CONTINUE;
15000}
15001
ea664253 15002Bexpression*
15003Slice_value_expression::do_get_backend(Translate_context* context)
2c809f8f 15004{
15005 std::vector<Bexpression*> vals(3);
ea664253 15006 vals[0] = this->valptr_->get_backend(context);
15007 vals[1] = this->len_->get_backend(context);
15008 vals[2] = this->cap_->get_backend(context);
2c809f8f 15009
15010 Gogo* gogo = context->gogo();
15011 Btype* btype = this->type_->get_backend(gogo);
ea664253 15012 return gogo->backend()->constructor_expression(btype, vals, this->location());
2c809f8f 15013}
15014
15015void
15016Slice_value_expression::do_dump_expression(
15017 Ast_dump_context* ast_dump_context) const
15018{
15019 ast_dump_context->ostream() << "slicevalue(";
15020 ast_dump_context->ostream() << "values: ";
15021 this->valptr_->dump_expression(ast_dump_context);
15022 ast_dump_context->ostream() << ", length: ";
15023 this->len_->dump_expression(ast_dump_context);
15024 ast_dump_context->ostream() << ", capacity: ";
15025 this->cap_->dump_expression(ast_dump_context);
15026 ast_dump_context->ostream() << ")";
15027}
15028
15029Expression*
15030Expression::make_slice_value(Type* at, Expression* valptr, Expression* len,
15031 Expression* cap, Location location)
15032{
15033 go_assert(at->is_slice_type());
15034 return new Slice_value_expression(at, valptr, len, cap, location);
15035}
2387f644 15036
15037// An expression that evaluates to some characteristic of a non-empty interface.
15038// This is used to access the method table or underlying object of an interface.
15039
15040class Interface_info_expression : public Expression
15041{
15042 public:
15043 Interface_info_expression(Expression* iface, Interface_info iface_info,
2c809f8f 15044 Location location)
2387f644 15045 : Expression(EXPRESSION_INTERFACE_INFO, location),
15046 iface_(iface), iface_info_(iface_info)
15047 { }
15048
15049 protected:
15050 Type*
15051 do_type();
15052
15053 void
15054 do_determine_type(const Type_context*)
15055 { }
15056
15057 Expression*
15058 do_copy()
15059 {
15060 return new Interface_info_expression(this->iface_->copy(),
15061 this->iface_info_, this->location());
15062 }
15063
ea664253 15064 Bexpression*
15065 do_get_backend(Translate_context* context);
2387f644 15066
15067 void
15068 do_dump_expression(Ast_dump_context*) const;
15069
15070 void
15071 do_issue_nil_check()
15072 { this->iface_->issue_nil_check(); }
15073
15074 private:
15075 // The interface for which we are getting information.
15076 Expression* iface_;
15077 // What information we want.
15078 Interface_info iface_info_;
15079};
15080
15081// Return the type of the interface info.
15082
15083Type*
15084Interface_info_expression::do_type()
15085{
15086 switch (this->iface_info_)
15087 {
15088 case INTERFACE_INFO_METHODS:
15089 {
625d3118 15090 typedef Unordered_map(Interface_type*, Type*) Hashtable;
15091 static Hashtable result_types;
15092
15093 Interface_type* itype = this->iface_->type()->interface_type();
15094
15095 Hashtable::const_iterator p = result_types.find(itype);
15096 if (p != result_types.end())
15097 return p->second;
15098
2c809f8f 15099 Type* pdt = Type::make_type_descriptor_ptr_type();
625d3118 15100 if (itype->is_empty())
15101 {
15102 result_types[itype] = pdt;
15103 return pdt;
15104 }
2c809f8f 15105
2387f644 15106 Location loc = this->location();
15107 Struct_field_list* sfl = new Struct_field_list();
2387f644 15108 sfl->push_back(
15109 Struct_field(Typed_identifier("__type_descriptor", pdt, loc)));
15110
2387f644 15111 for (Typed_identifier_list::const_iterator p = itype->methods()->begin();
15112 p != itype->methods()->end();
15113 ++p)
15114 {
15115 Function_type* ft = p->type()->function_type();
15116 go_assert(ft->receiver() == NULL);
15117
15118 const Typed_identifier_list* params = ft->parameters();
15119 Typed_identifier_list* mparams = new Typed_identifier_list();
15120 if (params != NULL)
15121 mparams->reserve(params->size() + 1);
15122 Type* vt = Type::make_pointer_type(Type::make_void_type());
15123 mparams->push_back(Typed_identifier("", vt, ft->location()));
15124 if (params != NULL)
15125 {
15126 for (Typed_identifier_list::const_iterator pp = params->begin();
15127 pp != params->end();
15128 ++pp)
15129 mparams->push_back(*pp);
15130 }
15131
15132 Typed_identifier_list* mresults = (ft->results() == NULL
15133 ? NULL
15134 : ft->results()->copy());
15135 Backend_function_type* mft =
15136 Type::make_backend_function_type(NULL, mparams, mresults,
15137 ft->location());
15138
15139 std::string fname = Gogo::unpack_hidden_name(p->name());
15140 sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc)));
15141 }
15142
6bf4793c 15143 Struct_type* st = Type::make_struct_type(sfl, loc);
15144 st->set_is_struct_incomparable();
15145 Pointer_type *pt = Type::make_pointer_type(st);
625d3118 15146 result_types[itype] = pt;
15147 return pt;
2387f644 15148 }
15149 case INTERFACE_INFO_OBJECT:
15150 return Type::make_pointer_type(Type::make_void_type());
15151 default:
15152 go_unreachable();
15153 }
15154}
15155
ea664253 15156// Return the backend representation for interface information.
2387f644 15157
ea664253 15158Bexpression*
15159Interface_info_expression::do_get_backend(Translate_context* context)
2387f644 15160{
15161 Gogo* gogo = context->gogo();
ea664253 15162 Bexpression* biface = this->iface_->get_backend(context);
2387f644 15163 switch (this->iface_info_)
15164 {
15165 case INTERFACE_INFO_METHODS:
15166 case INTERFACE_INFO_OBJECT:
ea664253 15167 return gogo->backend()->struct_field_expression(biface, this->iface_info_,
15168 this->location());
2387f644 15169 break;
15170 default:
15171 go_unreachable();
15172 }
2387f644 15173}
15174
15175// Dump ast representation for an interface info expression.
15176
15177void
15178Interface_info_expression::do_dump_expression(
15179 Ast_dump_context* ast_dump_context) const
15180{
2c809f8f 15181 bool is_empty = this->iface_->type()->interface_type()->is_empty();
2387f644 15182 ast_dump_context->ostream() << "interfaceinfo(";
15183 this->iface_->dump_expression(ast_dump_context);
15184 ast_dump_context->ostream() << ",";
15185 ast_dump_context->ostream() <<
2c809f8f 15186 (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods"
15187 : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor"
2387f644 15188 : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object"
15189 : "unknown");
15190 ast_dump_context->ostream() << ")";
15191}
15192
15193// Make an interface info expression.
15194
15195Expression*
15196Expression::make_interface_info(Expression* iface, Interface_info iface_info,
15197 Location location)
15198{
15199 return new Interface_info_expression(iface, iface_info, location);
15200}
15201
2c809f8f 15202// An expression that represents an interface value. The first field is either
15203// a type descriptor for an empty interface or a pointer to the interface method
15204// table for a non-empty interface. The second field is always the object.
15205
15206class Interface_value_expression : public Expression
15207{
15208 public:
15209 Interface_value_expression(Type* type, Expression* first_field,
15210 Expression* obj, Location location)
15211 : Expression(EXPRESSION_INTERFACE_VALUE, location),
15212 type_(type), first_field_(first_field), obj_(obj)
15213 { }
15214
15215 protected:
15216 int
15217 do_traverse(Traverse*);
15218
15219 Type*
15220 do_type()
15221 { return this->type_; }
15222
15223 void
15224 do_determine_type(const Type_context*)
15225 { go_unreachable(); }
15226
15227 Expression*
15228 do_copy()
15229 {
15230 return new Interface_value_expression(this->type_,
15231 this->first_field_->copy(),
15232 this->obj_->copy(), this->location());
15233 }
15234
ea664253 15235 Bexpression*
15236 do_get_backend(Translate_context* context);
2c809f8f 15237
15238 void
15239 do_dump_expression(Ast_dump_context*) const;
15240
15241 private:
15242 // The type of the interface value.
15243 Type* type_;
15244 // The first field of the interface (either a type descriptor or a pointer
15245 // to the method table.
15246 Expression* first_field_;
15247 // The underlying object of the interface.
15248 Expression* obj_;
15249};
15250
15251int
15252Interface_value_expression::do_traverse(Traverse* traverse)
15253{
15254 if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT
15255 || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT)
15256 return TRAVERSE_EXIT;
15257 return TRAVERSE_CONTINUE;
15258}
15259
ea664253 15260Bexpression*
15261Interface_value_expression::do_get_backend(Translate_context* context)
2c809f8f 15262{
15263 std::vector<Bexpression*> vals(2);
ea664253 15264 vals[0] = this->first_field_->get_backend(context);
15265 vals[1] = this->obj_->get_backend(context);
2c809f8f 15266
15267 Gogo* gogo = context->gogo();
15268 Btype* btype = this->type_->get_backend(gogo);
ea664253 15269 return gogo->backend()->constructor_expression(btype, vals, this->location());
2c809f8f 15270}
15271
15272void
15273Interface_value_expression::do_dump_expression(
15274 Ast_dump_context* ast_dump_context) const
15275{
15276 ast_dump_context->ostream() << "interfacevalue(";
15277 ast_dump_context->ostream() <<
15278 (this->type_->interface_type()->is_empty()
15279 ? "type_descriptor: "
15280 : "methods: ");
15281 this->first_field_->dump_expression(ast_dump_context);
15282 ast_dump_context->ostream() << ", object: ";
15283 this->obj_->dump_expression(ast_dump_context);
15284 ast_dump_context->ostream() << ")";
15285}
15286
15287Expression*
15288Expression::make_interface_value(Type* type, Expression* first_value,
15289 Expression* object, Location location)
15290{
15291 return new Interface_value_expression(type, first_value, object, location);
15292}
15293
15294// An interface method table for a pair of types: an interface type and a type
15295// that implements that interface.
15296
15297class Interface_mtable_expression : public Expression
15298{
15299 public:
15300 Interface_mtable_expression(Interface_type* itype, Type* type,
15301 bool is_pointer, Location location)
15302 : Expression(EXPRESSION_INTERFACE_MTABLE, location),
15303 itype_(itype), type_(type), is_pointer_(is_pointer),
15304 method_table_type_(NULL), bvar_(NULL)
15305 { }
15306
15307 protected:
15308 int
15309 do_traverse(Traverse*);
15310
15311 Type*
15312 do_type();
15313
15314 bool
3ae06f68 15315 do_is_static_initializer() const
2c809f8f 15316 { return true; }
15317
15318 void
15319 do_determine_type(const Type_context*)
15320 { go_unreachable(); }
15321
15322 Expression*
15323 do_copy()
15324 {
15325 return new Interface_mtable_expression(this->itype_, this->type_,
15326 this->is_pointer_, this->location());
15327 }
15328
15329 bool
15330 do_is_addressable() const
15331 { return true; }
15332
ea664253 15333 Bexpression*
15334 do_get_backend(Translate_context* context);
2c809f8f 15335
15336 void
15337 do_dump_expression(Ast_dump_context*) const;
15338
15339 private:
15340 // The interface type for which the methods are defined.
15341 Interface_type* itype_;
15342 // The type to construct the interface method table for.
15343 Type* type_;
15344 // Whether this table contains the method set for the receiver type or the
15345 // pointer receiver type.
15346 bool is_pointer_;
15347 // The type of the method table.
15348 Type* method_table_type_;
15349 // The backend variable that refers to the interface method table.
15350 Bvariable* bvar_;
15351};
15352
15353int
15354Interface_mtable_expression::do_traverse(Traverse* traverse)
15355{
15356 if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT
15357 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
15358 return TRAVERSE_EXIT;
15359 return TRAVERSE_CONTINUE;
15360}
15361
15362Type*
15363Interface_mtable_expression::do_type()
15364{
15365 if (this->method_table_type_ != NULL)
15366 return this->method_table_type_;
15367
15368 const Typed_identifier_list* interface_methods = this->itype_->methods();
15369 go_assert(!interface_methods->empty());
15370
15371 Struct_field_list* sfl = new Struct_field_list;
15372 Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
15373 this->location());
15374 sfl->push_back(Struct_field(tid));
15375 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
15376 p != interface_methods->end();
15377 ++p)
15378 sfl->push_back(Struct_field(*p));
6bf4793c 15379 Struct_type* st = Type::make_struct_type(sfl, this->location());
15380 st->set_is_struct_incomparable();
15381 this->method_table_type_ = st;
2c809f8f 15382 return this->method_table_type_;
15383}
15384
ea664253 15385Bexpression*
15386Interface_mtable_expression::do_get_backend(Translate_context* context)
2c809f8f 15387{
15388 Gogo* gogo = context->gogo();
2c809f8f 15389 Location loc = Linemap::predeclared_location();
15390 if (this->bvar_ != NULL)
d4e6573e 15391 return gogo->backend()->var_expression(this->bvar_, VE_rvalue,
15392 this->location());
2c809f8f 15393
15394 const Typed_identifier_list* interface_methods = this->itype_->methods();
15395 go_assert(!interface_methods->empty());
15396
15397 std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_")
15398 + this->itype_->mangled_name(gogo)
15399 + "__"
15400 + this->type_->mangled_name(gogo));
15401
15402 // See whether this interface has any hidden methods.
15403 bool has_hidden_methods = false;
15404 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
15405 p != interface_methods->end();
15406 ++p)
15407 {
15408 if (Gogo::is_hidden_name(p->name()))
15409 {
15410 has_hidden_methods = true;
15411 break;
15412 }
15413 }
15414
15415 // We already know that the named type is convertible to the
15416 // interface. If the interface has hidden methods, and the named
15417 // type is defined in a different package, then the interface
15418 // conversion table will be defined by that other package.
15419 if (has_hidden_methods
15420 && this->type_->named_type() != NULL
15421 && this->type_->named_type()->named_object()->package() != NULL)
15422 {
15423 Btype* btype = this->type()->get_backend(gogo);
438b4bec 15424 std::string asm_name(go_selectively_encode_id(mangled_name));
2c809f8f 15425 this->bvar_ =
438b4bec 15426 gogo->backend()->immutable_struct_reference(mangled_name, asm_name,
15427 btype, loc);
d4e6573e 15428 return gogo->backend()->var_expression(this->bvar_, VE_rvalue,
15429 this->location());
2c809f8f 15430 }
15431
15432 // The first element is the type descriptor.
15433 Type* td_type;
15434 if (!this->is_pointer_)
15435 td_type = this->type_;
15436 else
15437 td_type = Type::make_pointer_type(this->type_);
15438
15439 // Build an interface method table for a type: a type descriptor followed by a
15440 // list of function pointers, one for each interface method. This is used for
15441 // interfaces.
15442 Expression_list* svals = new Expression_list();
15443 svals->push_back(Expression::make_type_descriptor(td_type, loc));
15444
15445 Named_type* nt = this->type_->named_type();
15446 Struct_type* st = this->type_->struct_type();
15447 go_assert(nt != NULL || st != NULL);
15448
15449 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
15450 p != interface_methods->end();
15451 ++p)
15452 {
15453 bool is_ambiguous;
15454 Method* m;
15455 if (nt != NULL)
15456 m = nt->method_function(p->name(), &is_ambiguous);
15457 else
15458 m = st->method_function(p->name(), &is_ambiguous);
15459 go_assert(m != NULL);
15460 Named_object* no = m->named_object();
15461
15462 go_assert(no->is_function() || no->is_function_declaration());
15463 svals->push_back(Expression::make_func_code_reference(no, loc));
15464 }
15465
15466 Btype* btype = this->type()->get_backend(gogo);
15467 Expression* mtable = Expression::make_struct_composite_literal(this->type(),
15468 svals, loc);
ea664253 15469 Bexpression* ctor = mtable->get_backend(context);
2c809f8f 15470
15471 bool is_public = has_hidden_methods && this->type_->named_type() != NULL;
438b4bec 15472 std::string asm_name(go_selectively_encode_id(mangled_name));
15473 this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false,
2c809f8f 15474 !is_public, btype, loc);
15475 gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false,
15476 !is_public, btype, loc, ctor);
d4e6573e 15477 return gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc);
2c809f8f 15478}
15479
15480void
15481Interface_mtable_expression::do_dump_expression(
15482 Ast_dump_context* ast_dump_context) const
15483{
15484 ast_dump_context->ostream() << "__go_"
15485 << (this->is_pointer_ ? "pimt__" : "imt_");
15486 ast_dump_context->dump_type(this->itype_);
15487 ast_dump_context->ostream() << "__";
15488 ast_dump_context->dump_type(this->type_);
15489}
15490
15491Expression*
15492Expression::make_interface_mtable_ref(Interface_type* itype, Type* type,
15493 bool is_pointer, Location location)
15494{
15495 return new Interface_mtable_expression(itype, type, is_pointer, location);
15496}
15497
e440a328 15498// An expression which evaluates to the offset of a field within a
15499// struct. This, like Type_info_expression, q.v., is only used to
15500// initialize fields of a type descriptor.
15501
15502class Struct_field_offset_expression : public Expression
15503{
15504 public:
15505 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
b13c66cd 15506 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
15507 Linemap::predeclared_location()),
e440a328 15508 type_(type), field_(field)
15509 { }
15510
15511 protected:
f23d7786 15512 bool
3ae06f68 15513 do_is_static_initializer() const
f23d7786 15514 { return true; }
15515
e440a328 15516 Type*
15517 do_type()
15518 { return Type::lookup_integer_type("uintptr"); }
15519
15520 void
15521 do_determine_type(const Type_context*)
15522 { }
15523
15524 Expression*
15525 do_copy()
15526 { return this; }
15527
ea664253 15528 Bexpression*
15529 do_get_backend(Translate_context* context);
e440a328 15530
d751bb78 15531 void
15532 do_dump_expression(Ast_dump_context*) const;
15533
e440a328 15534 private:
15535 // The type of the struct.
15536 Struct_type* type_;
15537 // The field.
15538 const Struct_field* field_;
15539};
15540
ea664253 15541// Return the backend representation for a struct field offset.
e440a328 15542
ea664253 15543Bexpression*
15544Struct_field_offset_expression::do_get_backend(Translate_context* context)
e440a328 15545{
e440a328 15546 const Struct_field_list* fields = this->type_->fields();
e440a328 15547 Struct_field_list::const_iterator p;
2c8bda43 15548 unsigned i = 0;
e440a328 15549 for (p = fields->begin();
15550 p != fields->end();
2c8bda43 15551 ++p, ++i)
15552 if (&*p == this->field_)
15553 break;
c484d925 15554 go_assert(&*p == this->field_);
e440a328 15555
2c8bda43 15556 Gogo* gogo = context->gogo();
15557 Btype* btype = this->type_->get_backend(gogo);
15558
3f378015 15559 int64_t offset = gogo->backend()->type_field_offset(btype, i);
2c8bda43 15560 Type* uptr_type = Type::lookup_integer_type("uintptr");
e67508fa 15561 Expression* ret =
3f378015 15562 Expression::make_integer_int64(offset, uptr_type,
15563 Linemap::predeclared_location());
ea664253 15564 return ret->get_backend(context);
e440a328 15565}
15566
d751bb78 15567// Dump ast representation for a struct field offset expression.
15568
15569void
15570Struct_field_offset_expression::do_dump_expression(
15571 Ast_dump_context* ast_dump_context) const
15572{
15573 ast_dump_context->ostream() << "unsafe.Offsetof(";
2d29d278 15574 ast_dump_context->dump_type(this->type_);
15575 ast_dump_context->ostream() << '.';
15576 ast_dump_context->ostream() <<
15577 Gogo::message_name(this->field_->field_name());
d751bb78 15578 ast_dump_context->ostream() << ")";
15579}
15580
e440a328 15581// Make an expression for a struct field offset.
15582
15583Expression*
15584Expression::make_struct_field_offset(Struct_type* type,
15585 const Struct_field* field)
15586{
15587 return new Struct_field_offset_expression(type, field);
15588}
15589
15590// An expression which evaluates to the address of an unnamed label.
15591
15592class Label_addr_expression : public Expression
15593{
15594 public:
b13c66cd 15595 Label_addr_expression(Label* label, Location location)
e440a328 15596 : Expression(EXPRESSION_LABEL_ADDR, location),
15597 label_(label)
15598 { }
15599
15600 protected:
15601 Type*
15602 do_type()
15603 { return Type::make_pointer_type(Type::make_void_type()); }
15604
15605 void
15606 do_determine_type(const Type_context*)
15607 { }
15608
15609 Expression*
15610 do_copy()
15611 { return new Label_addr_expression(this->label_, this->location()); }
15612
ea664253 15613 Bexpression*
15614 do_get_backend(Translate_context* context)
15615 { return this->label_->get_addr(context, this->location()); }
e440a328 15616
d751bb78 15617 void
15618 do_dump_expression(Ast_dump_context* ast_dump_context) const
15619 { ast_dump_context->ostream() << this->label_->name(); }
15620
e440a328 15621 private:
15622 // The label whose address we are taking.
15623 Label* label_;
15624};
15625
15626// Make an expression for the address of an unnamed label.
15627
15628Expression*
b13c66cd 15629Expression::make_label_addr(Label* label, Location location)
e440a328 15630{
15631 return new Label_addr_expression(label, location);
15632}
15633
da244e59 15634// Class Conditional_expression.
283a177b 15635
2c809f8f 15636// Traversal.
15637
15638int
15639Conditional_expression::do_traverse(Traverse* traverse)
15640{
15641 if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT
15642 || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT
15643 || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT)
15644 return TRAVERSE_EXIT;
15645 return TRAVERSE_CONTINUE;
15646}
15647
283a177b 15648// Return the type of the conditional expression.
15649
15650Type*
15651Conditional_expression::do_type()
15652{
15653 Type* result_type = Type::make_void_type();
2c809f8f 15654 if (Type::are_identical(this->then_->type(), this->else_->type(), false,
15655 NULL))
283a177b 15656 result_type = this->then_->type();
15657 else if (this->then_->is_nil_expression()
15658 || this->else_->is_nil_expression())
15659 result_type = (!this->then_->is_nil_expression()
15660 ? this->then_->type()
15661 : this->else_->type());
15662 return result_type;
15663}
15664
2c809f8f 15665// Determine type for a conditional expression.
15666
15667void
15668Conditional_expression::do_determine_type(const Type_context* context)
15669{
15670 this->cond_->determine_type_no_context();
15671 this->then_->determine_type(context);
15672 this->else_->determine_type(context);
15673}
15674
283a177b 15675// Get the backend representation of a conditional expression.
15676
ea664253 15677Bexpression*
15678Conditional_expression::do_get_backend(Translate_context* context)
283a177b 15679{
15680 Gogo* gogo = context->gogo();
15681 Btype* result_btype = this->type()->get_backend(gogo);
ea664253 15682 Bexpression* cond = this->cond_->get_backend(context);
15683 Bexpression* then = this->then_->get_backend(context);
15684 Bexpression* belse = this->else_->get_backend(context);
93715b75 15685 Bfunction* bfn = context->function()->func_value()->get_decl();
15686 return gogo->backend()->conditional_expression(bfn, result_btype, cond, then,
ea664253 15687 belse, this->location());
283a177b 15688}
15689
15690// Dump ast representation of a conditional expression.
15691
15692void
15693Conditional_expression::do_dump_expression(
15694 Ast_dump_context* ast_dump_context) const
15695{
15696 ast_dump_context->ostream() << "(";
15697 ast_dump_context->dump_expression(this->cond_);
15698 ast_dump_context->ostream() << " ? ";
15699 ast_dump_context->dump_expression(this->then_);
15700 ast_dump_context->ostream() << " : ";
15701 ast_dump_context->dump_expression(this->else_);
15702 ast_dump_context->ostream() << ") ";
15703}
15704
15705// Make a conditional expression.
15706
15707Expression*
15708Expression::make_conditional(Expression* cond, Expression* then,
15709 Expression* else_expr, Location location)
15710{
15711 return new Conditional_expression(cond, then, else_expr, location);
15712}
15713
da244e59 15714// Class Compound_expression.
2c809f8f 15715
15716// Traversal.
15717
15718int
15719Compound_expression::do_traverse(Traverse* traverse)
15720{
15721 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT
15722 || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
15723 return TRAVERSE_EXIT;
15724 return TRAVERSE_CONTINUE;
15725}
15726
15727// Return the type of the compound expression.
15728
15729Type*
15730Compound_expression::do_type()
15731{
15732 return this->expr_->type();
15733}
15734
15735// Determine type for a compound expression.
15736
15737void
15738Compound_expression::do_determine_type(const Type_context* context)
15739{
15740 this->init_->determine_type_no_context();
15741 this->expr_->determine_type(context);
15742}
15743
15744// Get the backend representation of a compound expression.
15745
ea664253 15746Bexpression*
15747Compound_expression::do_get_backend(Translate_context* context)
2c809f8f 15748{
15749 Gogo* gogo = context->gogo();
ea664253 15750 Bexpression* binit = this->init_->get_backend(context);
0ab48656 15751 Bfunction* bfunction = context->function()->func_value()->get_decl();
15752 Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction,
15753 binit);
ea664253 15754 Bexpression* bexpr = this->expr_->get_backend(context);
15755 return gogo->backend()->compound_expression(init_stmt, bexpr,
15756 this->location());
2c809f8f 15757}
15758
15759// Dump ast representation of a conditional expression.
15760
15761void
15762Compound_expression::do_dump_expression(
15763 Ast_dump_context* ast_dump_context) const
15764{
15765 ast_dump_context->ostream() << "(";
15766 ast_dump_context->dump_expression(this->init_);
15767 ast_dump_context->ostream() << ",";
15768 ast_dump_context->dump_expression(this->expr_);
15769 ast_dump_context->ostream() << ") ";
15770}
15771
15772// Make a compound expression.
15773
15774Expression*
15775Expression::make_compound(Expression* init, Expression* expr, Location location)
15776{
15777 return new Compound_expression(init, expr, location);
15778}
15779
1b4fb1e0 15780// Class Backend_expression.
15781
15782int
15783Backend_expression::do_traverse(Traverse*)
15784{
15785 return TRAVERSE_CONTINUE;
15786}
15787
15788void
15789Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
15790{
15791 ast_dump_context->ostream() << "backend_expression<";
15792 ast_dump_context->dump_type(this->type_);
15793 ast_dump_context->ostream() << ">";
15794}
15795
15796Expression*
15797Expression::make_backend(Bexpression* bexpr, Type* type, Location location)
15798{
15799 return new Backend_expression(bexpr, type, location);
15800}
15801
e440a328 15802// Import an expression. This comes at the end in order to see the
15803// various class definitions.
15804
15805Expression*
15806Expression::import_expression(Import* imp)
15807{
15808 int c = imp->peek_char();
15809 if (imp->match_c_string("- ")
15810 || imp->match_c_string("! ")
15811 || imp->match_c_string("^ "))
15812 return Unary_expression::do_import(imp);
15813 else if (c == '(')
15814 return Binary_expression::do_import(imp);
15815 else if (imp->match_c_string("true")
15816 || imp->match_c_string("false"))
15817 return Boolean_expression::do_import(imp);
15818 else if (c == '"')
15819 return String_expression::do_import(imp);
15820 else if (c == '-' || (c >= '0' && c <= '9'))
15821 {
15822 // This handles integers, floats and complex constants.
15823 return Integer_expression::do_import(imp);
15824 }
15825 else if (imp->match_c_string("nil"))
15826 return Nil_expression::do_import(imp);
15827 else if (imp->match_c_string("convert"))
15828 return Type_conversion_expression::do_import(imp);
15829 else
15830 {
631d5788 15831 go_error_at(imp->location(), "import error: expected expression");
e440a328 15832 return Expression::make_error(imp->location());
15833 }
15834}
15835
15836// Class Expression_list.
15837
15838// Traverse the list.
15839
15840int
15841Expression_list::traverse(Traverse* traverse)
15842{
15843 for (Expression_list::iterator p = this->begin();
15844 p != this->end();
15845 ++p)
15846 {
15847 if (*p != NULL)
15848 {
15849 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
15850 return TRAVERSE_EXIT;
15851 }
15852 }
15853 return TRAVERSE_CONTINUE;
15854}
15855
15856// Copy the list.
15857
15858Expression_list*
15859Expression_list::copy()
15860{
15861 Expression_list* ret = new Expression_list();
15862 for (Expression_list::iterator p = this->begin();
15863 p != this->end();
15864 ++p)
15865 {
15866 if (*p == NULL)
15867 ret->push_back(NULL);
15868 else
15869 ret->push_back((*p)->copy());
15870 }
15871 return ret;
15872}
15873
15874// Return whether an expression list has an error expression.
15875
15876bool
15877Expression_list::contains_error() const
15878{
15879 for (Expression_list::const_iterator p = this->begin();
15880 p != this->end();
15881 ++p)
15882 if (*p != NULL && (*p)->is_error_expression())
15883 return true;
15884 return false;
15885}
0c77715b 15886
15887// Class Numeric_constant.
15888
15889// Destructor.
15890
15891Numeric_constant::~Numeric_constant()
15892{
15893 this->clear();
15894}
15895
15896// Copy constructor.
15897
15898Numeric_constant::Numeric_constant(const Numeric_constant& a)
15899 : classification_(a.classification_), type_(a.type_)
15900{
15901 switch (a.classification_)
15902 {
15903 case NC_INVALID:
15904 break;
15905 case NC_INT:
15906 case NC_RUNE:
15907 mpz_init_set(this->u_.int_val, a.u_.int_val);
15908 break;
15909 case NC_FLOAT:
15910 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
15911 break;
15912 case NC_COMPLEX:
fcbea5e4 15913 mpc_init2(this->u_.complex_val, mpc_precision);
15914 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
0c77715b 15915 break;
15916 default:
15917 go_unreachable();
15918 }
15919}
15920
15921// Assignment operator.
15922
15923Numeric_constant&
15924Numeric_constant::operator=(const Numeric_constant& a)
15925{
15926 this->clear();
15927 this->classification_ = a.classification_;
15928 this->type_ = a.type_;
15929 switch (a.classification_)
15930 {
15931 case NC_INVALID:
15932 break;
15933 case NC_INT:
15934 case NC_RUNE:
15935 mpz_init_set(this->u_.int_val, a.u_.int_val);
15936 break;
15937 case NC_FLOAT:
15938 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
15939 break;
15940 case NC_COMPLEX:
fcbea5e4 15941 mpc_init2(this->u_.complex_val, mpc_precision);
15942 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
0c77715b 15943 break;
15944 default:
15945 go_unreachable();
15946 }
15947 return *this;
15948}
15949
15950// Clear the contents.
15951
15952void
15953Numeric_constant::clear()
15954{
15955 switch (this->classification_)
15956 {
15957 case NC_INVALID:
15958 break;
15959 case NC_INT:
15960 case NC_RUNE:
15961 mpz_clear(this->u_.int_val);
15962 break;
15963 case NC_FLOAT:
15964 mpfr_clear(this->u_.float_val);
15965 break;
15966 case NC_COMPLEX:
fcbea5e4 15967 mpc_clear(this->u_.complex_val);
0c77715b 15968 break;
15969 default:
15970 go_unreachable();
15971 }
15972 this->classification_ = NC_INVALID;
15973}
15974
15975// Set to an unsigned long value.
15976
15977void
15978Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
15979{
15980 this->clear();
15981 this->classification_ = NC_INT;
15982 this->type_ = type;
15983 mpz_init_set_ui(this->u_.int_val, val);
15984}
15985
15986// Set to an integer value.
15987
15988void
15989Numeric_constant::set_int(Type* type, const mpz_t val)
15990{
15991 this->clear();
15992 this->classification_ = NC_INT;
15993 this->type_ = type;
15994 mpz_init_set(this->u_.int_val, val);
15995}
15996
15997// Set to a rune value.
15998
15999void
16000Numeric_constant::set_rune(Type* type, const mpz_t val)
16001{
16002 this->clear();
16003 this->classification_ = NC_RUNE;
16004 this->type_ = type;
16005 mpz_init_set(this->u_.int_val, val);
16006}
16007
16008// Set to a floating point value.
16009
16010void
16011Numeric_constant::set_float(Type* type, const mpfr_t val)
16012{
16013 this->clear();
16014 this->classification_ = NC_FLOAT;
16015 this->type_ = type;
833b523c 16016 // Numeric constants do not have negative zero values, so remove
16017 // them here. They also don't have infinity or NaN values, but we
16018 // should never see them here.
16019 if (mpfr_zero_p(val))
16020 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN);
16021 else
16022 mpfr_init_set(this->u_.float_val, val, GMP_RNDN);
0c77715b 16023}
16024
16025// Set to a complex value.
16026
16027void
fcbea5e4 16028Numeric_constant::set_complex(Type* type, const mpc_t val)
0c77715b 16029{
16030 this->clear();
16031 this->classification_ = NC_COMPLEX;
16032 this->type_ = type;
fcbea5e4 16033 mpc_init2(this->u_.complex_val, mpc_precision);
16034 mpc_set(this->u_.complex_val, val, MPC_RNDNN);
0c77715b 16035}
16036
16037// Get an int value.
16038
16039void
16040Numeric_constant::get_int(mpz_t* val) const
16041{
16042 go_assert(this->is_int());
16043 mpz_init_set(*val, this->u_.int_val);
16044}
16045
16046// Get a rune value.
16047
16048void
16049Numeric_constant::get_rune(mpz_t* val) const
16050{
16051 go_assert(this->is_rune());
16052 mpz_init_set(*val, this->u_.int_val);
16053}
16054
16055// Get a floating point value.
16056
16057void
16058Numeric_constant::get_float(mpfr_t* val) const
16059{
16060 go_assert(this->is_float());
16061 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
16062}
16063
16064// Get a complex value.
16065
16066void
fcbea5e4 16067Numeric_constant::get_complex(mpc_t* val) const
0c77715b 16068{
16069 go_assert(this->is_complex());
fcbea5e4 16070 mpc_init2(*val, mpc_precision);
16071 mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
0c77715b 16072}
16073
16074// Express value as unsigned long if possible.
16075
16076Numeric_constant::To_unsigned_long
16077Numeric_constant::to_unsigned_long(unsigned long* val) const
16078{
16079 switch (this->classification_)
16080 {
16081 case NC_INT:
16082 case NC_RUNE:
16083 return this->mpz_to_unsigned_long(this->u_.int_val, val);
16084 case NC_FLOAT:
16085 return this->mpfr_to_unsigned_long(this->u_.float_val, val);
16086 case NC_COMPLEX:
fcbea5e4 16087 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16088 return NC_UL_NOTINT;
fcbea5e4 16089 return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val),
16090 val);
0c77715b 16091 default:
16092 go_unreachable();
16093 }
16094}
16095
16096// Express integer value as unsigned long if possible.
16097
16098Numeric_constant::To_unsigned_long
16099Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
16100 unsigned long *val) const
16101{
16102 if (mpz_sgn(ival) < 0)
16103 return NC_UL_NEGATIVE;
16104 unsigned long ui = mpz_get_ui(ival);
16105 if (mpz_cmp_ui(ival, ui) != 0)
16106 return NC_UL_BIG;
16107 *val = ui;
16108 return NC_UL_VALID;
16109}
16110
16111// Express floating point value as unsigned long if possible.
16112
16113Numeric_constant::To_unsigned_long
16114Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
16115 unsigned long *val) const
16116{
16117 if (!mpfr_integer_p(fval))
16118 return NC_UL_NOTINT;
16119 mpz_t ival;
16120 mpz_init(ival);
16121 mpfr_get_z(ival, fval, GMP_RNDN);
16122 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
16123 mpz_clear(ival);
16124 return ret;
16125}
16126
03118c21 16127// Express value as memory size if possible.
16128
16129bool
16130Numeric_constant::to_memory_size(int64_t* val) const
16131{
16132 switch (this->classification_)
16133 {
16134 case NC_INT:
16135 case NC_RUNE:
16136 return this->mpz_to_memory_size(this->u_.int_val, val);
16137 case NC_FLOAT:
16138 return this->mpfr_to_memory_size(this->u_.float_val, val);
16139 case NC_COMPLEX:
16140 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
16141 return false;
16142 return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val);
16143 default:
16144 go_unreachable();
16145 }
16146}
16147
16148// Express integer as memory size if possible.
16149
16150bool
16151Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const
16152{
16153 if (mpz_sgn(ival) < 0)
16154 return false;
16155 if (mpz_fits_slong_p(ival))
16156 {
16157 *val = static_cast<int64_t>(mpz_get_si(ival));
16158 return true;
16159 }
16160
16161 // Test >= 64, not > 64, because an int64_t can hold 63 bits of a
16162 // positive value.
16163 if (mpz_sizeinbase(ival, 2) >= 64)
16164 return false;
16165
16166 mpz_t q, r;
16167 mpz_init(q);
16168 mpz_init(r);
16169 mpz_tdiv_q_2exp(q, ival, 32);
16170 mpz_tdiv_r_2exp(r, ival, 32);
16171 go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r));
16172 *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32)
16173 + static_cast<int64_t>(mpz_get_ui(r)));
16174 mpz_clear(r);
16175 mpz_clear(q);
16176 return true;
16177}
16178
16179// Express floating point value as memory size if possible.
16180
16181bool
16182Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const
16183{
16184 if (!mpfr_integer_p(fval))
16185 return false;
16186 mpz_t ival;
16187 mpz_init(ival);
16188 mpfr_get_z(ival, fval, GMP_RNDN);
16189 bool ret = this->mpz_to_memory_size(ival, val);
16190 mpz_clear(ival);
16191 return ret;
16192}
16193
0c77715b 16194// Convert value to integer if possible.
16195
16196bool
16197Numeric_constant::to_int(mpz_t* val) const
16198{
16199 switch (this->classification_)
16200 {
16201 case NC_INT:
16202 case NC_RUNE:
16203 mpz_init_set(*val, this->u_.int_val);
16204 return true;
16205 case NC_FLOAT:
16206 if (!mpfr_integer_p(this->u_.float_val))
16207 return false;
16208 mpz_init(*val);
16209 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN);
16210 return true;
16211 case NC_COMPLEX:
fcbea5e4 16212 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))
16213 || !mpfr_integer_p(mpc_realref(this->u_.complex_val)))
0c77715b 16214 return false;
16215 mpz_init(*val);
fcbea5e4 16216 mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16217 return true;
16218 default:
16219 go_unreachable();
16220 }
16221}
16222
16223// Convert value to floating point if possible.
16224
16225bool
16226Numeric_constant::to_float(mpfr_t* val) const
16227{
16228 switch (this->classification_)
16229 {
16230 case NC_INT:
16231 case NC_RUNE:
16232 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN);
16233 return true;
16234 case NC_FLOAT:
16235 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
16236 return true;
16237 case NC_COMPLEX:
fcbea5e4 16238 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16239 return false;
fcbea5e4 16240 mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16241 return true;
16242 default:
16243 go_unreachable();
16244 }
16245}
16246
16247// Convert value to complex.
16248
16249bool
fcbea5e4 16250Numeric_constant::to_complex(mpc_t* val) const
0c77715b 16251{
fcbea5e4 16252 mpc_init2(*val, mpc_precision);
0c77715b 16253 switch (this->classification_)
16254 {
16255 case NC_INT:
16256 case NC_RUNE:
fcbea5e4 16257 mpc_set_z(*val, this->u_.int_val, MPC_RNDNN);
0c77715b 16258 return true;
16259 case NC_FLOAT:
fcbea5e4 16260 mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN);
0c77715b 16261 return true;
16262 case NC_COMPLEX:
fcbea5e4 16263 mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
0c77715b 16264 return true;
16265 default:
16266 go_unreachable();
16267 }
16268}
16269
16270// Get the type.
16271
16272Type*
16273Numeric_constant::type() const
16274{
16275 if (this->type_ != NULL)
16276 return this->type_;
16277 switch (this->classification_)
16278 {
16279 case NC_INT:
16280 return Type::make_abstract_integer_type();
16281 case NC_RUNE:
16282 return Type::make_abstract_character_type();
16283 case NC_FLOAT:
16284 return Type::make_abstract_float_type();
16285 case NC_COMPLEX:
16286 return Type::make_abstract_complex_type();
16287 default:
16288 go_unreachable();
16289 }
16290}
16291
16292// If the constant can be expressed in TYPE, then set the type of the
16293// constant to TYPE and return true. Otherwise return false, and, if
16294// ISSUE_ERROR is true, report an appropriate error message.
16295
16296bool
16297Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
16298{
16299 bool ret;
f11c2155 16300 if (type == NULL || type->is_error())
0c77715b 16301 ret = true;
16302 else if (type->integer_type() != NULL)
16303 ret = this->check_int_type(type->integer_type(), issue_error, loc);
16304 else if (type->float_type() != NULL)
16305 ret = this->check_float_type(type->float_type(), issue_error, loc);
16306 else if (type->complex_type() != NULL)
16307 ret = this->check_complex_type(type->complex_type(), issue_error, loc);
16308 else
5706ab68 16309 {
16310 ret = false;
16311 if (issue_error)
16312 go_assert(saw_errors());
16313 }
0c77715b 16314 if (ret)
16315 this->type_ = type;
16316 return ret;
16317}
16318
16319// Check whether the constant can be expressed in an integer type.
16320
16321bool
16322Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
71a45216 16323 Location location)
0c77715b 16324{
16325 mpz_t val;
16326 switch (this->classification_)
16327 {
16328 case NC_INT:
16329 case NC_RUNE:
16330 mpz_init_set(val, this->u_.int_val);
16331 break;
16332
16333 case NC_FLOAT:
16334 if (!mpfr_integer_p(this->u_.float_val))
16335 {
16336 if (issue_error)
71a45216 16337 {
631d5788 16338 go_error_at(location,
16339 "floating point constant truncated to integer");
71a45216 16340 this->set_invalid();
16341 }
0c77715b 16342 return false;
16343 }
16344 mpz_init(val);
16345 mpfr_get_z(val, this->u_.float_val, GMP_RNDN);
16346 break;
16347
16348 case NC_COMPLEX:
fcbea5e4 16349 if (!mpfr_integer_p(mpc_realref(this->u_.complex_val))
16350 || !mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16351 {
16352 if (issue_error)
71a45216 16353 {
631d5788 16354 go_error_at(location, "complex constant truncated to integer");
71a45216 16355 this->set_invalid();
16356 }
0c77715b 16357 return false;
16358 }
16359 mpz_init(val);
fcbea5e4 16360 mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16361 break;
16362
16363 default:
16364 go_unreachable();
16365 }
16366
16367 bool ret;
16368 if (type->is_abstract())
16369 ret = true;
16370 else
16371 {
16372 int bits = mpz_sizeinbase(val, 2);
16373 if (type->is_unsigned())
16374 {
16375 // For an unsigned type we can only accept a nonnegative
16376 // number, and we must be able to represents at least BITS.
16377 ret = mpz_sgn(val) >= 0 && bits <= type->bits();
16378 }
16379 else
16380 {
16381 // For a signed type we need an extra bit to indicate the
16382 // sign. We have to handle the most negative integer
16383 // specially.
16384 ret = (bits + 1 <= type->bits()
16385 || (bits <= type->bits()
16386 && mpz_sgn(val) < 0
16387 && (mpz_scan1(val, 0)
16388 == static_cast<unsigned long>(type->bits() - 1))
16389 && mpz_scan0(val, type->bits()) == ULONG_MAX));
16390 }
16391 }
16392
16393 if (!ret && issue_error)
71a45216 16394 {
631d5788 16395 go_error_at(location, "integer constant overflow");
71a45216 16396 this->set_invalid();
16397 }
0c77715b 16398
16399 return ret;
16400}
16401
16402// Check whether the constant can be expressed in a floating point
16403// type.
16404
16405bool
16406Numeric_constant::check_float_type(Float_type* type, bool issue_error,
d0bcce51 16407 Location location)
0c77715b 16408{
16409 mpfr_t val;
16410 switch (this->classification_)
16411 {
16412 case NC_INT:
16413 case NC_RUNE:
16414 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN);
16415 break;
16416
16417 case NC_FLOAT:
16418 mpfr_init_set(val, this->u_.float_val, GMP_RNDN);
16419 break;
16420
16421 case NC_COMPLEX:
fcbea5e4 16422 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 16423 {
16424 if (issue_error)
71a45216 16425 {
16426 this->set_invalid();
631d5788 16427 go_error_at(location, "complex constant truncated to float");
71a45216 16428 }
0c77715b 16429 return false;
16430 }
fcbea5e4 16431 mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 16432 break;
16433
16434 default:
16435 go_unreachable();
16436 }
16437
16438 bool ret;
16439 if (type->is_abstract())
16440 ret = true;
16441 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
16442 {
16443 // A NaN or Infinity always fits in the range of the type.
16444 ret = true;
16445 }
16446 else
16447 {
16448 mp_exp_t exp = mpfr_get_exp(val);
16449 mp_exp_t max_exp;
16450 switch (type->bits())
16451 {
16452 case 32:
16453 max_exp = 128;
16454 break;
16455 case 64:
16456 max_exp = 1024;
16457 break;
16458 default:
16459 go_unreachable();
16460 }
16461
16462 ret = exp <= max_exp;
d0bcce51 16463
16464 if (ret)
16465 {
16466 // Round the constant to the desired type.
16467 mpfr_t t;
16468 mpfr_init(t);
16469 switch (type->bits())
16470 {
16471 case 32:
16472 mpfr_set_prec(t, 24);
16473 break;
16474 case 64:
16475 mpfr_set_prec(t, 53);
16476 break;
16477 default:
16478 go_unreachable();
16479 }
16480 mpfr_set(t, val, GMP_RNDN);
16481 mpfr_set(val, t, GMP_RNDN);
16482 mpfr_clear(t);
16483
16484 this->set_float(type, val);
16485 }
0c77715b 16486 }
16487
16488 mpfr_clear(val);
16489
16490 if (!ret && issue_error)
71a45216 16491 {
631d5788 16492 go_error_at(location, "floating point constant overflow");
71a45216 16493 this->set_invalid();
16494 }
0c77715b 16495
16496 return ret;
16497}
16498
16499// Check whether the constant can be expressed in a complex type.
16500
16501bool
16502Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
d0bcce51 16503 Location location)
0c77715b 16504{
16505 if (type->is_abstract())
16506 return true;
16507
16508 mp_exp_t max_exp;
16509 switch (type->bits())
16510 {
16511 case 64:
16512 max_exp = 128;
16513 break;
16514 case 128:
16515 max_exp = 1024;
16516 break;
16517 default:
16518 go_unreachable();
16519 }
16520
fcbea5e4 16521 mpc_t val;
16522 mpc_init2(val, mpc_precision);
0c77715b 16523 switch (this->classification_)
16524 {
16525 case NC_INT:
16526 case NC_RUNE:
fcbea5e4 16527 mpc_set_z(val, this->u_.int_val, MPC_RNDNN);
0c77715b 16528 break;
16529
16530 case NC_FLOAT:
fcbea5e4 16531 mpc_set_fr(val, this->u_.float_val, MPC_RNDNN);
0c77715b 16532 break;
16533
16534 case NC_COMPLEX:
fcbea5e4 16535 mpc_set(val, this->u_.complex_val, MPC_RNDNN);
0c77715b 16536 break;
16537
16538 default:
16539 go_unreachable();
16540 }
16541
d0bcce51 16542 bool ret = true;
fcbea5e4 16543 if (!mpfr_nan_p(mpc_realref(val))
16544 && !mpfr_inf_p(mpc_realref(val))
16545 && !mpfr_zero_p(mpc_realref(val))
16546 && mpfr_get_exp(mpc_realref(val)) > max_exp)
d0bcce51 16547 {
16548 if (issue_error)
71a45216 16549 {
631d5788 16550 go_error_at(location, "complex real part overflow");
71a45216 16551 this->set_invalid();
16552 }
d0bcce51 16553 ret = false;
16554 }
0c77715b 16555
fcbea5e4 16556 if (!mpfr_nan_p(mpc_imagref(val))
16557 && !mpfr_inf_p(mpc_imagref(val))
16558 && !mpfr_zero_p(mpc_imagref(val))
16559 && mpfr_get_exp(mpc_imagref(val)) > max_exp)
d0bcce51 16560 {
16561 if (issue_error)
71a45216 16562 {
631d5788 16563 go_error_at(location, "complex imaginary part overflow");
71a45216 16564 this->set_invalid();
16565 }
d0bcce51 16566 ret = false;
16567 }
0c77715b 16568
d0bcce51 16569 if (ret)
16570 {
16571 // Round the constant to the desired type.
fcbea5e4 16572 mpc_t t;
d0bcce51 16573 switch (type->bits())
16574 {
16575 case 64:
fcbea5e4 16576 mpc_init2(t, 24);
d0bcce51 16577 break;
16578 case 128:
fcbea5e4 16579 mpc_init2(t, 53);
d0bcce51 16580 break;
16581 default:
16582 go_unreachable();
16583 }
fcbea5e4 16584 mpc_set(t, val, MPC_RNDNN);
16585 mpc_set(val, t, MPC_RNDNN);
16586 mpc_clear(t);
d0bcce51 16587
fcbea5e4 16588 this->set_complex(type, val);
d0bcce51 16589 }
16590
fcbea5e4 16591 mpc_clear(val);
0c77715b 16592
16593 return ret;
16594}
16595
16596// Return an Expression for this value.
16597
16598Expression*
16599Numeric_constant::expression(Location loc) const
16600{
16601 switch (this->classification_)
16602 {
16603 case NC_INT:
e67508fa 16604 return Expression::make_integer_z(&this->u_.int_val, this->type_, loc);
0c77715b 16605 case NC_RUNE:
16606 return Expression::make_character(&this->u_.int_val, this->type_, loc);
16607 case NC_FLOAT:
16608 return Expression::make_float(&this->u_.float_val, this->type_, loc);
16609 case NC_COMPLEX:
fcbea5e4 16610 return Expression::make_complex(&this->u_.complex_val, this->type_, loc);
71a45216 16611 case NC_INVALID:
16612 go_assert(saw_errors());
16613 return Expression::make_error(loc);
0c77715b 16614 default:
16615 go_unreachable();
16616 }
16617}