]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/gofrontend/expressions.cc
* reorg.c (try_merge_delay_insns): Declare i and j inside the
[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"
13#include "types.h"
14#include "export.h"
15#include "import.h"
16#include "statements.h"
17#include "lex.h"
a9182619 18#include "runtime.h"
6e193e6f 19#include "backend.h"
e440a328 20#include "expressions.h"
d751bb78 21#include "ast-dump.h"
e440a328 22
23// Class Expression.
24
25Expression::Expression(Expression_classification classification,
b13c66cd 26 Location location)
e440a328 27 : classification_(classification), location_(location)
28{
29}
30
31Expression::~Expression()
32{
33}
34
e440a328 35// Traverse the expressions.
36
37int
38Expression::traverse(Expression** pexpr, Traverse* traverse)
39{
40 Expression* expr = *pexpr;
41 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
42 {
43 int t = traverse->expression(pexpr);
44 if (t == TRAVERSE_EXIT)
45 return TRAVERSE_EXIT;
46 else if (t == TRAVERSE_SKIP_COMPONENTS)
47 return TRAVERSE_CONTINUE;
48 }
49 return expr->do_traverse(traverse);
50}
51
52// Traverse subexpressions of this expression.
53
54int
55Expression::traverse_subexpressions(Traverse* traverse)
56{
57 return this->do_traverse(traverse);
58}
59
60// Default implementation for do_traverse for child classes.
61
62int
63Expression::do_traverse(Traverse*)
64{
65 return TRAVERSE_CONTINUE;
66}
67
68// This virtual function is called by the parser if the value of this
a7549a6a 69// expression is being discarded. By default, we give an error.
70// Expressions with side effects override.
e440a328 71
4f2138d7 72bool
e440a328 73Expression::do_discarding_value()
74{
a7549a6a 75 this->unused_value_error();
4f2138d7 76 return false;
e440a328 77}
78
79// This virtual function is called to export expressions. This will
80// only be used by expressions which may be constant.
81
82void
83Expression::do_export(Export*) const
84{
c3e6f413 85 go_unreachable();
e440a328 86}
87
a7549a6a 88// Give an error saying that the value of the expression is not used.
e440a328 89
90void
a7549a6a 91Expression::unused_value_error()
e440a328 92{
4f2138d7 93 this->report_error(_("value computed is not used"));
e440a328 94}
95
96// Note that this expression is an error. This is called by children
97// when they discover an error.
98
99void
100Expression::set_is_error()
101{
102 this->classification_ = EXPRESSION_ERROR;
103}
104
105// For children to call to report an error conveniently.
106
107void
108Expression::report_error(const char* msg)
109{
110 error_at(this->location_, "%s", msg);
111 this->set_is_error();
112}
113
114// Set types of variables and constants. This is implemented by the
115// child class.
116
117void
118Expression::determine_type(const Type_context* context)
119{
120 this->do_determine_type(context);
121}
122
123// Set types when there is no context.
124
125void
126Expression::determine_type_no_context()
127{
128 Type_context context;
129 this->do_determine_type(&context);
130}
131
2c809f8f 132// Return an expression handling any conversions which must be done during
e440a328 133// assignment.
134
2c809f8f 135Expression*
b4a33049 136Expression::convert_for_assignment(Gogo*, Type* lhs_type,
2c809f8f 137 Expression* rhs, Location location)
e440a328 138{
2c809f8f 139 Type* rhs_type = rhs->type();
140 if (lhs_type->is_error()
141 || rhs_type->is_error()
142 || rhs->is_error_expression())
143 return Expression::make_error(location);
e440a328 144
54211955 145 if (lhs_type->forwarded() != rhs_type->forwarded()
146 && lhs_type->interface_type() != NULL)
e440a328 147 {
148 if (rhs_type->interface_type() == NULL)
2c809f8f 149 return Expression::convert_type_to_interface(lhs_type, rhs, location);
e440a328 150 else
2c809f8f 151 return Expression::convert_interface_to_interface(lhs_type, rhs, false,
152 location);
e440a328 153 }
54211955 154 else if (lhs_type->forwarded() != rhs_type->forwarded()
155 && rhs_type->interface_type() != NULL)
2c809f8f 156 return Expression::convert_interface_to_type(lhs_type, rhs, location);
411eb89e 157 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
e440a328 158 {
2c809f8f 159 // Assigning nil to a slice.
2c809f8f 160 Expression* nil = Expression::make_nil(location);
e67508fa 161 Expression* zero = Expression::make_integer_ul(0, NULL, location);
2c809f8f 162 return Expression::make_slice_value(lhs_type, nil, zero, zero, location);
e440a328 163 }
164 else if (rhs_type->is_nil_type())
2c809f8f 165 return Expression::make_nil(location);
166 else if (Type::are_identical(lhs_type, rhs_type, false, NULL))
e440a328 167 {
168 // No conversion is needed.
2c809f8f 169 return rhs;
170 }
171 else if (lhs_type->points_to() != NULL)
172 return Expression::make_unsafe_cast(lhs_type, rhs, location);
173 else if (lhs_type->is_numeric_type())
174 return Expression::make_cast(lhs_type, rhs, location);
175 else if ((lhs_type->struct_type() != NULL
176 && rhs_type->struct_type() != NULL)
177 || (lhs_type->array_type() != NULL
178 && rhs_type->array_type() != NULL))
e440a328 179 {
180 // This conversion must be permitted by Go, or we wouldn't have
181 // gotten here.
2c809f8f 182 return Expression::make_unsafe_cast(lhs_type, rhs, location);
e440a328 183 }
184 else
2c809f8f 185 return rhs;
e440a328 186}
187
2c809f8f 188// Return an expression for a conversion from a non-interface type to an
e440a328 189// interface type.
190
2c809f8f 191Expression*
192Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs,
193 Location location)
e440a328 194{
e440a328 195 Interface_type* lhs_interface_type = lhs_type->interface_type();
196 bool lhs_is_empty = lhs_interface_type->is_empty();
197
198 // Since RHS_TYPE is a static type, we can create the interface
199 // method table at compile time.
200
201 // When setting an interface to nil, we just set both fields to
202 // NULL.
2c809f8f 203 Type* rhs_type = rhs->type();
e440a328 204 if (rhs_type->is_nil_type())
63697958 205 {
2c809f8f 206 Expression* nil = Expression::make_nil(location);
207 return Expression::make_interface_value(lhs_type, nil, nil, location);
63697958 208 }
e440a328 209
210 // This should have been checked already.
c484d925 211 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
e440a328 212
e440a328 213 // An interface is a tuple. If LHS_TYPE is an empty interface type,
214 // then the first field is the type descriptor for RHS_TYPE.
215 // Otherwise it is the interface method table for RHS_TYPE.
2c809f8f 216 Expression* first_field;
e440a328 217 if (lhs_is_empty)
2c809f8f 218 first_field = Expression::make_type_descriptor(rhs_type, location);
e440a328 219 else
220 {
221 // Build the interface method table for this interface and this
222 // object type: a list of function pointers for each interface
223 // method.
224 Named_type* rhs_named_type = rhs_type->named_type();
c0cab2ec 225 Struct_type* rhs_struct_type = rhs_type->struct_type();
e440a328 226 bool is_pointer = false;
c0cab2ec 227 if (rhs_named_type == NULL && rhs_struct_type == NULL)
e440a328 228 {
229 rhs_named_type = rhs_type->deref()->named_type();
c0cab2ec 230 rhs_struct_type = rhs_type->deref()->struct_type();
e440a328 231 is_pointer = true;
232 }
c0cab2ec 233 if (rhs_named_type != NULL)
2c809f8f 234 first_field =
235 rhs_named_type->interface_method_table(lhs_interface_type,
236 is_pointer);
c0cab2ec 237 else if (rhs_struct_type != NULL)
2c809f8f 238 first_field =
239 rhs_struct_type->interface_method_table(lhs_interface_type,
240 is_pointer);
c0cab2ec 241 else
2c809f8f 242 first_field = Expression::make_nil(location);
e440a328 243 }
e440a328 244
2c809f8f 245 Expression* obj;
e440a328 246 if (rhs_type->points_to() != NULL)
247 {
2c809f8f 248 // We are assigning a pointer to the interface; the interface
e440a328 249 // holds the pointer itself.
2c809f8f 250 obj = rhs;
251 }
252 else
253 {
254 // We are assigning a non-pointer value to the interface; the
255 // interface gets a copy of the value in the heap.
256 obj = Expression::make_heap_expression(rhs, location);
e440a328 257 }
258
2c809f8f 259 return Expression::make_interface_value(lhs_type, first_field, obj, location);
260}
e440a328 261
2c809f8f 262// Return an expression for the type descriptor of RHS.
e440a328 263
2c809f8f 264Expression*
265Expression::get_interface_type_descriptor(Expression* rhs)
266{
267 go_assert(rhs->type()->interface_type() != NULL);
268 Location location = rhs->location();
e440a328 269
2c809f8f 270 // The type descriptor is the first field of an empty interface.
271 if (rhs->type()->interface_type()->is_empty())
272 return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR,
273 location);
274
275 Expression* mtable =
276 Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location);
e440a328 277
2c809f8f 278 Expression* descriptor =
279 Expression::make_unary(OPERATOR_MULT, mtable, location);
280 descriptor = Expression::make_field_reference(descriptor, 0, location);
281 Expression* nil = Expression::make_nil(location);
e440a328 282
2c809f8f 283 Expression* eq =
284 Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location);
285 return Expression::make_conditional(eq, nil, descriptor, location);
e440a328 286}
287
2c809f8f 288// Return an expression for the conversion of an interface type to an
e440a328 289// interface type.
290
2c809f8f 291Expression*
292Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs,
293 bool for_type_guard,
294 Location location)
e440a328 295{
8ba8cc87 296 if (Type::are_identical(lhs_type, rhs->type(), false, NULL))
297 return rhs;
298
e440a328 299 Interface_type* lhs_interface_type = lhs_type->interface_type();
300 bool lhs_is_empty = lhs_interface_type->is_empty();
301
e440a328 302 // In the general case this requires runtime examination of the type
303 // method table to match it up with the interface methods.
304
305 // FIXME: If all of the methods in the right hand side interface
306 // also appear in the left hand side interface, then we don't need
307 // to do a runtime check, although we still need to build a new
308 // method table.
309
8ba8cc87 310 // We are going to evaluate RHS multiple times.
311 go_assert(rhs->is_variable());
312
e440a328 313 // Get the type descriptor for the right hand side. This will be
314 // NULL for a nil interface.
2c809f8f 315 Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs);
316 Expression* lhs_type_expr =
317 Expression::make_type_descriptor(lhs_type, location);
e440a328 318
2c809f8f 319 Expression* first_field;
e440a328 320 if (for_type_guard)
321 {
322 // A type assertion fails when converting a nil interface.
2c809f8f 323 first_field =
324 Runtime::make_call(Runtime::ASSERT_INTERFACE, location, 2,
325 lhs_type_expr, rhs_type_expr);
e440a328 326 }
327 else if (lhs_is_empty)
328 {
2c809f8f 329 // A conversion to an empty interface always succeeds, and the
e440a328 330 // first field is just the type descriptor of the object.
2c809f8f 331 first_field = rhs_type_expr;
e440a328 332 }
333 else
334 {
335 // A conversion to a non-empty interface may fail, but unlike a
336 // type assertion converting nil will always succeed.
2c809f8f 337 first_field =
338 Runtime::make_call(Runtime::CONVERT_INTERFACE, location, 2,
339 lhs_type_expr, rhs_type_expr);
e440a328 340 }
341
342 // The second field is simply the object pointer.
2c809f8f 343 Expression* obj =
344 Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location);
345 return Expression::make_interface_value(lhs_type, first_field, obj, location);
e440a328 346}
347
2c809f8f 348// Return an expression for the conversion of an interface type to a
e440a328 349// non-interface type.
350
2c809f8f 351Expression*
352Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs,
353 Location location)
e440a328 354{
8ba8cc87 355 // We are going to evaluate RHS multiple times.
356 go_assert(rhs->is_variable());
357
e440a328 358 // Call a function to check that the type is valid. The function
359 // will panic with an appropriate runtime type error if the type is
360 // not valid.
2c809f8f 361 Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type,
362 location);
363 Expression* rhs_descriptor =
364 Expression::get_interface_type_descriptor(rhs);
365
366 Type* rhs_type = rhs->type();
367 Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type,
368 location);
369
370 Expression* check_iface = Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE,
371 location, 3, lhs_type_expr,
372 rhs_descriptor, rhs_inter_expr);
e440a328 373
374 // If the call succeeds, pull out the value.
2c809f8f 375 Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT,
376 location);
e440a328 377
378 // If the value is a pointer, then it is the value we want.
379 // Otherwise it points to the value.
380 if (lhs_type->points_to() == NULL)
381 {
2c809f8f 382 obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj,
383 location);
384 obj = Expression::make_unary(OPERATOR_MULT, obj, location);
e440a328 385 }
2c809f8f 386 return Expression::make_compound(check_iface, obj, location);
e440a328 387}
388
ea664253 389// Convert an expression to its backend representation. This is implemented by
390// the child class. Not that it is not in general safe to call this multiple
e440a328 391// times for a single expression, but that we don't catch such errors.
392
ea664253 393Bexpression*
394Expression::get_backend(Translate_context* context)
e440a328 395{
396 // The child may have marked this expression as having an error.
397 if (this->classification_ == EXPRESSION_ERROR)
ea664253 398 return context->backend()->error_expression();
e440a328 399
ea664253 400 return this->do_get_backend(context);
e440a328 401}
402
48c2a53a 403// Return a backend expression for VAL.
404Bexpression*
405Expression::backend_numeric_constant_expression(Translate_context* context,
406 Numeric_constant* val)
e440a328 407{
48c2a53a 408 Gogo* gogo = context->gogo();
409 Type* type = val->type();
410 if (type == NULL)
411 return gogo->backend()->error_expression();
e440a328 412
48c2a53a 413 Btype* btype = type->get_backend(gogo);
414 Bexpression* ret;
415 if (type->integer_type() != NULL)
e440a328 416 {
417 mpz_t ival;
48c2a53a 418 if (!val->to_int(&ival))
419 {
420 go_assert(saw_errors());
421 return gogo->backend()->error_expression();
422 }
423 ret = gogo->backend()->integer_constant_expression(btype, ival);
e440a328 424 mpz_clear(ival);
e440a328 425 }
48c2a53a 426 else if (type->float_type() != NULL)
e440a328 427 {
48c2a53a 428 mpfr_t fval;
429 if (!val->to_float(&fval))
430 {
431 go_assert(saw_errors());
432 return gogo->backend()->error_expression();
433 }
434 ret = gogo->backend()->float_constant_expression(btype, fval);
435 mpfr_clear(fval);
e440a328 436 }
48c2a53a 437 else if (type->complex_type() != NULL)
e440a328 438 {
fcbea5e4 439 mpc_t cval;
440 if (!val->to_complex(&cval))
48c2a53a 441 {
442 go_assert(saw_errors());
443 return gogo->backend()->error_expression();
444 }
fcbea5e4 445 ret = gogo->backend()->complex_constant_expression(btype, cval);
446 mpc_clear(cval);
e440a328 447 }
448 else
c3e6f413 449 go_unreachable();
e440a328 450
48c2a53a 451 return ret;
e440a328 452}
453
2c809f8f 454// Return an expression which evaluates to true if VAL, of arbitrary integer
455// type, is negative or is more than the maximum value of the Go type "int".
e440a328 456
2c809f8f 457Expression*
458Expression::check_bounds(Expression* val, Location loc)
e440a328 459{
2c809f8f 460 Type* val_type = val->type();
461 Type* bound_type = Type::lookup_integer_type("int");
462
463 int val_type_size;
464 bool val_is_unsigned = false;
465 if (val_type->integer_type() != NULL)
466 {
467 val_type_size = val_type->integer_type()->bits();
468 val_is_unsigned = val_type->integer_type()->is_unsigned();
469 }
470 else
471 {
472 if (!val_type->is_numeric_type()
473 || !Type::are_convertible(bound_type, val_type, NULL))
474 {
475 go_assert(saw_errors());
476 return Expression::make_boolean(true, loc);
477 }
e440a328 478
2c809f8f 479 if (val_type->complex_type() != NULL)
480 val_type_size = val_type->complex_type()->bits();
481 else
482 val_type_size = val_type->float_type()->bits();
483 }
484
485 Expression* negative_index = Expression::make_boolean(false, loc);
486 Expression* index_overflows = Expression::make_boolean(false, loc);
487 if (!val_is_unsigned)
e440a328 488 {
e67508fa 489 Expression* zero = Expression::make_integer_ul(0, val_type, loc);
2c809f8f 490 negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc);
e440a328 491 }
492
2c809f8f 493 int bound_type_size = bound_type->integer_type()->bits();
c3068ac0 494 if (val_type_size > bound_type_size
495 || (val_type_size == bound_type_size
2c809f8f 496 && val_is_unsigned))
497 {
498 mpz_t one;
499 mpz_init_set_ui(one, 1UL);
500
501 // maxval = 2^(bound_type_size - 1) - 1
502 mpz_t maxval;
503 mpz_init(maxval);
504 mpz_mul_2exp(maxval, one, bound_type_size - 1);
505 mpz_sub_ui(maxval, maxval, 1);
e67508fa 506 Expression* max = Expression::make_integer_z(&maxval, val_type, loc);
2c809f8f 507 mpz_clear(one);
508 mpz_clear(maxval);
509
510 index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc);
e440a328 511 }
512
2c809f8f 513 return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows,
514 loc);
e440a328 515}
516
d751bb78 517void
518Expression::dump_expression(Ast_dump_context* ast_dump_context) const
519{
520 this->do_dump_expression(ast_dump_context);
521}
522
e440a328 523// Error expressions. This are used to avoid cascading errors.
524
525class Error_expression : public Expression
526{
527 public:
b13c66cd 528 Error_expression(Location location)
e440a328 529 : Expression(EXPRESSION_ERROR, location)
530 { }
531
532 protected:
533 bool
534 do_is_constant() const
535 { return true; }
536
0e168074 537 bool
538 do_is_immutable() const
539 { return true; }
540
e440a328 541 bool
0c77715b 542 do_numeric_constant_value(Numeric_constant* nc) const
e440a328 543 {
0c77715b 544 nc->set_unsigned_long(NULL, 0);
e440a328 545 return true;
546 }
547
4f2138d7 548 bool
e440a328 549 do_discarding_value()
4f2138d7 550 { return true; }
e440a328 551
552 Type*
553 do_type()
554 { return Type::make_error_type(); }
555
556 void
557 do_determine_type(const Type_context*)
558 { }
559
560 Expression*
561 do_copy()
562 { return this; }
563
564 bool
565 do_is_addressable() const
566 { return true; }
567
ea664253 568 Bexpression*
569 do_get_backend(Translate_context* context)
570 { return context->backend()->error_expression(); }
d751bb78 571
572 void
573 do_dump_expression(Ast_dump_context*) const;
e440a328 574};
575
d751bb78 576// Dump the ast representation for an error expression to a dump context.
577
578void
579Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
580{
581 ast_dump_context->ostream() << "_Error_" ;
582}
583
e440a328 584Expression*
b13c66cd 585Expression::make_error(Location location)
e440a328 586{
587 return new Error_expression(location);
588}
589
590// An expression which is really a type. This is used during parsing.
591// It is an error if these survive after lowering.
592
593class
594Type_expression : public Expression
595{
596 public:
b13c66cd 597 Type_expression(Type* type, Location location)
e440a328 598 : Expression(EXPRESSION_TYPE, location),
599 type_(type)
600 { }
601
602 protected:
603 int
604 do_traverse(Traverse* traverse)
605 { return Type::traverse(this->type_, traverse); }
606
607 Type*
608 do_type()
609 { return this->type_; }
610
611 void
612 do_determine_type(const Type_context*)
613 { }
614
615 void
616 do_check_types(Gogo*)
617 { this->report_error(_("invalid use of type")); }
618
619 Expression*
620 do_copy()
621 { return this; }
622
ea664253 623 Bexpression*
624 do_get_backend(Translate_context*)
c3e6f413 625 { go_unreachable(); }
e440a328 626
d751bb78 627 void do_dump_expression(Ast_dump_context*) const;
628
e440a328 629 private:
630 // The type which we are representing as an expression.
631 Type* type_;
632};
633
d751bb78 634void
635Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
636{
637 ast_dump_context->dump_type(this->type_);
638}
639
e440a328 640Expression*
b13c66cd 641Expression::make_type(Type* type, Location location)
e440a328 642{
643 return new Type_expression(type, location);
644}
645
e03bdf36 646// Class Parser_expression.
647
648Type*
649Parser_expression::do_type()
650{
651 // We should never really ask for the type of a Parser_expression.
652 // However, it can happen, at least when we have an invalid const
653 // whose initializer refers to the const itself. In that case we
654 // may ask for the type when lowering the const itself.
c484d925 655 go_assert(saw_errors());
e03bdf36 656 return Type::make_error_type();
657}
658
e440a328 659// Class Var_expression.
660
661// Lower a variable expression. Here we just make sure that the
662// initialization expression of the variable has been lowered. This
663// ensures that we will be able to determine the type of the variable
664// if necessary.
665
666Expression*
ceeb4318 667Var_expression::do_lower(Gogo* gogo, Named_object* function,
668 Statement_inserter* inserter, int)
e440a328 669{
670 if (this->variable_->is_variable())
671 {
672 Variable* var = this->variable_->var_value();
673 // This is either a local variable or a global variable. A
674 // reference to a variable which is local to an enclosing
675 // function will be a reference to a field in a closure.
676 if (var->is_global())
ceeb4318 677 {
678 function = NULL;
679 inserter = NULL;
680 }
681 var->lower_init_expression(gogo, function, inserter);
e440a328 682 }
683 return this;
684}
685
e440a328 686// Return the type of a reference to a variable.
687
688Type*
689Var_expression::do_type()
690{
691 if (this->variable_->is_variable())
692 return this->variable_->var_value()->type();
693 else if (this->variable_->is_result_variable())
694 return this->variable_->result_var_value()->type();
695 else
c3e6f413 696 go_unreachable();
e440a328 697}
698
0ab09e06 699// Determine the type of a reference to a variable.
700
701void
702Var_expression::do_determine_type(const Type_context*)
703{
704 if (this->variable_->is_variable())
705 this->variable_->var_value()->determine_type();
706}
707
e440a328 708// Something takes the address of this variable. This means that we
709// may want to move the variable onto the heap.
710
711void
712Var_expression::do_address_taken(bool escapes)
713{
714 if (!escapes)
f325319b 715 {
716 if (this->variable_->is_variable())
717 this->variable_->var_value()->set_non_escaping_address_taken();
718 else if (this->variable_->is_result_variable())
719 this->variable_->result_var_value()->set_non_escaping_address_taken();
720 else
721 go_unreachable();
722 }
e440a328 723 else
f325319b 724 {
725 if (this->variable_->is_variable())
726 this->variable_->var_value()->set_address_taken();
727 else if (this->variable_->is_result_variable())
728 this->variable_->result_var_value()->set_address_taken();
729 else
730 go_unreachable();
731 }
e440a328 732}
733
ea664253 734// Get the backend representation for a reference to a variable.
e440a328 735
ea664253 736Bexpression*
737Var_expression::do_get_backend(Translate_context* context)
e440a328 738{
fe2f84cf 739 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
740 context->function());
fe2f84cf 741 bool is_in_heap;
c6777780 742 Location loc = this->location();
9b27b43c 743 Btype* btype;
744 Gogo* gogo = context->gogo();
fe2f84cf 745 if (this->variable_->is_variable())
9b27b43c 746 {
747 is_in_heap = this->variable_->var_value()->is_in_heap();
748 btype = this->variable_->var_value()->type()->get_backend(gogo);
749 }
fe2f84cf 750 else if (this->variable_->is_result_variable())
9b27b43c 751 {
752 is_in_heap = this->variable_->result_var_value()->is_in_heap();
753 btype = this->variable_->result_var_value()->type()->get_backend(gogo);
754 }
fe2f84cf 755 else
c3e6f413 756 go_unreachable();
c6777780 757
758 Bexpression* ret = context->backend()->var_expression(bvar, loc);
fe2f84cf 759 if (is_in_heap)
9b27b43c 760 ret = context->backend()->indirect_expression(btype, ret, true, loc);
ea664253 761 return ret;
e440a328 762}
763
d751bb78 764// Ast dump for variable expression.
765
766void
767Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
768{
769 ast_dump_context->ostream() << this->variable_->name() ;
770}
771
e440a328 772// Make a reference to a variable in an expression.
773
774Expression*
b13c66cd 775Expression::make_var_reference(Named_object* var, Location location)
e440a328 776{
777 if (var->is_sink())
778 return Expression::make_sink(location);
779
780 // FIXME: Creating a new object for each reference to a variable is
781 // wasteful.
782 return new Var_expression(var, location);
783}
784
785// Class Temporary_reference_expression.
786
787// The type.
788
789Type*
790Temporary_reference_expression::do_type()
791{
792 return this->statement_->type();
793}
794
795// Called if something takes the address of this temporary variable.
796// We never have to move temporary variables to the heap, but we do
797// need to know that they must live in the stack rather than in a
798// register.
799
800void
801Temporary_reference_expression::do_address_taken(bool)
802{
803 this->statement_->set_is_address_taken();
804}
805
ea664253 806// Get a backend expression referring to the variable.
e440a328 807
ea664253 808Bexpression*
809Temporary_reference_expression::do_get_backend(Translate_context* context)
e440a328 810{
cd440cff 811 Gogo* gogo = context->gogo();
eefc1ed3 812 Bvariable* bvar = this->statement_->get_backend_variable(context);
cd440cff 813 Bexpression* ret = gogo->backend()->var_expression(bvar, this->location());
eefc1ed3 814
cd440cff 815 // The backend can't always represent the same set of recursive types
eefc1ed3 816 // that the Go frontend can. In some cases this means that a
817 // temporary variable won't have the right backend type. Correct
818 // that here by adding a type cast. We need to use base() to push
819 // the circularity down one level.
cd440cff 820 Type* stype = this->statement_->type();
ceeb4318 821 if (!this->is_lvalue_
cd440cff 822 && stype->has_pointer()
823 && stype->deref()->is_void_type())
eefc1ed3 824 {
cd440cff 825 Btype* btype = this->type()->base()->get_backend(gogo);
826 ret = gogo->backend()->convert_expression(btype, ret, this->location());
eefc1ed3 827 }
ea664253 828 return ret;
e440a328 829}
830
d751bb78 831// Ast dump for temporary reference.
832
833void
834Temporary_reference_expression::do_dump_expression(
835 Ast_dump_context* ast_dump_context) const
836{
837 ast_dump_context->dump_temp_variable_name(this->statement_);
838}
839
e440a328 840// Make a reference to a temporary variable.
841
ceeb4318 842Temporary_reference_expression*
e440a328 843Expression::make_temporary_reference(Temporary_statement* statement,
b13c66cd 844 Location location)
e440a328 845{
846 return new Temporary_reference_expression(statement, location);
847}
848
e9d3367e 849// Class Set_and_use_temporary_expression.
850
851// Return the type.
852
853Type*
854Set_and_use_temporary_expression::do_type()
855{
856 return this->statement_->type();
857}
858
0afbb937 859// Determine the type of the expression.
860
861void
862Set_and_use_temporary_expression::do_determine_type(
863 const Type_context* context)
864{
865 this->expr_->determine_type(context);
866}
867
e9d3367e 868// Take the address.
869
870void
871Set_and_use_temporary_expression::do_address_taken(bool)
872{
873 this->statement_->set_is_address_taken();
874}
875
876// Return the backend representation.
877
ea664253 878Bexpression*
879Set_and_use_temporary_expression::do_get_backend(Translate_context* context)
e9d3367e 880{
e9d3367e 881 Location loc = this->location();
a302c105 882 Gogo* gogo = context->gogo();
883 Bvariable* bvar = this->statement_->get_backend_variable(context);
884 Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc);
885
ea664253 886 Bexpression* bexpr = this->expr_->get_backend(context);
a302c105 887 Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc);
888 var_ref = gogo->backend()->var_expression(bvar, loc);
889 Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc);
ea664253 890 return ret;
e9d3367e 891}
892
893// Dump.
894
895void
896Set_and_use_temporary_expression::do_dump_expression(
897 Ast_dump_context* ast_dump_context) const
898{
899 ast_dump_context->ostream() << '(';
900 ast_dump_context->dump_temp_variable_name(this->statement_);
901 ast_dump_context->ostream() << " = ";
902 this->expr_->dump_expression(ast_dump_context);
903 ast_dump_context->ostream() << ')';
904}
905
906// Make a set-and-use temporary.
907
908Set_and_use_temporary_expression*
909Expression::make_set_and_use_temporary(Temporary_statement* statement,
910 Expression* expr, Location location)
911{
912 return new Set_and_use_temporary_expression(statement, expr, location);
913}
914
e440a328 915// A sink expression--a use of the blank identifier _.
916
917class Sink_expression : public Expression
918{
919 public:
b13c66cd 920 Sink_expression(Location location)
e440a328 921 : Expression(EXPRESSION_SINK, location),
aa93217a 922 type_(NULL), bvar_(NULL)
e440a328 923 { }
924
925 protected:
4f2138d7 926 bool
e440a328 927 do_discarding_value()
4f2138d7 928 { return true; }
e440a328 929
930 Type*
931 do_type();
932
933 void
934 do_determine_type(const Type_context*);
935
936 Expression*
937 do_copy()
938 { return new Sink_expression(this->location()); }
939
ea664253 940 Bexpression*
941 do_get_backend(Translate_context*);
e440a328 942
d751bb78 943 void
944 do_dump_expression(Ast_dump_context*) const;
945
e440a328 946 private:
947 // The type of this sink variable.
948 Type* type_;
949 // The temporary variable we generate.
aa93217a 950 Bvariable* bvar_;
e440a328 951};
952
953// Return the type of a sink expression.
954
955Type*
956Sink_expression::do_type()
957{
958 if (this->type_ == NULL)
959 return Type::make_sink_type();
960 return this->type_;
961}
962
963// Determine the type of a sink expression.
964
965void
966Sink_expression::do_determine_type(const Type_context* context)
967{
968 if (context->type != NULL)
969 this->type_ = context->type;
970}
971
972// Return a temporary variable for a sink expression. This will
973// presumably be a write-only variable which the middle-end will drop.
974
ea664253 975Bexpression*
976Sink_expression::do_get_backend(Translate_context* context)
e440a328 977{
aa93217a 978 Location loc = this->location();
979 Gogo* gogo = context->gogo();
980 if (this->bvar_ == NULL)
e440a328 981 {
c484d925 982 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
aa93217a 983 Named_object* fn = context->function();
984 go_assert(fn != NULL);
985 Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn);
9f0e0513 986 Btype* bt = this->type_->get_backend(context->gogo());
aa93217a 987 Bstatement* decl;
988 this->bvar_ =
989 gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL,
990 false, loc, &decl);
991 Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc);
992 var_ref = gogo->backend()->compound_expression(decl, var_ref, loc);
ea664253 993 return var_ref;
e440a328 994 }
ea664253 995 return gogo->backend()->var_expression(this->bvar_, loc);
e440a328 996}
997
d751bb78 998// Ast dump for sink expression.
999
1000void
1001Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1002{
1003 ast_dump_context->ostream() << "_" ;
1004}
1005
e440a328 1006// Make a sink expression.
1007
1008Expression*
b13c66cd 1009Expression::make_sink(Location location)
e440a328 1010{
1011 return new Sink_expression(location);
1012}
1013
1014// Class Func_expression.
1015
1016// FIXME: Can a function expression appear in a constant expression?
1017// The value is unchanging. Initializing a constant to the address of
1018// a function seems like it could work, though there might be little
1019// point to it.
1020
e440a328 1021// Traversal.
1022
1023int
1024Func_expression::do_traverse(Traverse* traverse)
1025{
1026 return (this->closure_ == NULL
1027 ? TRAVERSE_CONTINUE
1028 : Expression::traverse(&this->closure_, traverse));
1029}
1030
1031// Return the type of a function expression.
1032
1033Type*
1034Func_expression::do_type()
1035{
1036 if (this->function_->is_function())
1037 return this->function_->func_value()->type();
1038 else if (this->function_->is_function_declaration())
1039 return this->function_->func_declaration_value()->type();
1040 else
c3e6f413 1041 go_unreachable();
e440a328 1042}
1043
ea664253 1044// Get the backend representation for the code of a function expression.
e440a328 1045
97267c39 1046Bexpression*
8381eda7 1047Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc)
e440a328 1048{
1049 Function_type* fntype;
8381eda7 1050 if (no->is_function())
1051 fntype = no->func_value()->type();
1052 else if (no->is_function_declaration())
1053 fntype = no->func_declaration_value()->type();
e440a328 1054 else
c3e6f413 1055 go_unreachable();
e440a328 1056
1057 // Builtin functions are handled specially by Call_expression. We
1058 // can't take their address.
1059 if (fntype->is_builtin())
1060 {
8381eda7 1061 error_at(loc,
cb0e02f3 1062 "invalid use of special builtin function %qs; must be called",
8381eda7 1063 no->message_name().c_str());
97267c39 1064 return gogo->backend()->error_expression();
e440a328 1065 }
1066
97267c39 1067 Bfunction* fndecl;
e440a328 1068 if (no->is_function())
cf3cae55 1069 fndecl = no->func_value()->get_or_make_decl(gogo, no);
e440a328 1070 else if (no->is_function_declaration())
cf3cae55 1071 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no);
e440a328 1072 else
c3e6f413 1073 go_unreachable();
e440a328 1074
97267c39 1075 return gogo->backend()->function_code_expression(fndecl, loc);
e440a328 1076}
1077
ea664253 1078// Get the backend representation for a function expression. This is used when
1079// we take the address of a function rather than simply calling it. A func
8381eda7 1080// value is represented as a pointer to a block of memory. The first
1081// word of that memory is a pointer to the function code. The
1082// remaining parts of that memory are the addresses of variables that
1083// the function closes over.
e440a328 1084
ea664253 1085Bexpression*
1086Func_expression::do_get_backend(Translate_context* context)
e440a328 1087{
8381eda7 1088 // If there is no closure, just use the function descriptor.
2010c17a 1089 if (this->closure_ == NULL)
8381eda7 1090 {
1091 Gogo* gogo = context->gogo();
1092 Named_object* no = this->function_;
1093 Expression* descriptor;
1094 if (no->is_function())
1095 descriptor = no->func_value()->descriptor(gogo, no);
1096 else if (no->is_function_declaration())
1097 {
1098 if (no->func_declaration_value()->type()->is_builtin())
1099 {
1100 error_at(this->location(),
1101 ("invalid use of special builtin function %qs; "
1102 "must be called"),
1103 no->message_name().c_str());
ea664253 1104 return gogo->backend()->error_expression();
8381eda7 1105 }
1106 descriptor = no->func_declaration_value()->descriptor(gogo, no);
1107 }
1108 else
1109 go_unreachable();
2010c17a 1110
ea664253 1111 Bexpression* bdesc = descriptor->get_backend(context);
1112 return gogo->backend()->address_expression(bdesc, this->location());
8381eda7 1113 }
e440a328 1114
8381eda7 1115 go_assert(this->function_->func_value()->enclosing() != NULL);
e440a328 1116
8381eda7 1117 // If there is a closure, then the closure is itself the function
1118 // expression. It is a pointer to a struct whose first field points
1119 // to the function code and whose remaining fields are the addresses
1120 // of the closed-over variables.
ea664253 1121 return this->closure_->get_backend(context);
e440a328 1122}
1123
d751bb78 1124// Ast dump for function.
1125
1126void
1127Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1128{
8b1c301d 1129 ast_dump_context->ostream() << this->function_->name();
1130 if (this->closure_ != NULL)
1131 {
1132 ast_dump_context->ostream() << " {closure = ";
1133 this->closure_->dump_expression(ast_dump_context);
1134 ast_dump_context->ostream() << "}";
1135 }
d751bb78 1136}
1137
e440a328 1138// Make a reference to a function in an expression.
1139
1140Expression*
1141Expression::make_func_reference(Named_object* function, Expression* closure,
b13c66cd 1142 Location location)
e440a328 1143{
b1d7ecfa 1144 Func_expression* fe = new Func_expression(function, closure, location);
1145
1146 // Detect references to builtin functions and set the runtime code if
1147 // appropriate.
1148 if (function->is_function_declaration())
1149 fe->set_runtime_code(Runtime::name_to_code(function->name()));
1150 return fe;
e440a328 1151}
1152
c6837989 1153// Class Func_descriptor_expression.
8381eda7 1154
c6837989 1155// Constructor.
8381eda7 1156
c6837989 1157Func_descriptor_expression::Func_descriptor_expression(Named_object* fn)
1158 : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()),
f8bdf81a 1159 fn_(fn), dvar_(NULL)
c6837989 1160{
1161 go_assert(!fn->is_function() || !fn->func_value()->needs_closure());
1162}
8381eda7 1163
c6837989 1164// Traversal.
8381eda7 1165
c6837989 1166int
1167Func_descriptor_expression::do_traverse(Traverse*)
1168{
1169 return TRAVERSE_CONTINUE;
1170}
8381eda7 1171
1172// All function descriptors have the same type.
1173
1174Type* Func_descriptor_expression::descriptor_type;
1175
1176void
1177Func_descriptor_expression::make_func_descriptor_type()
1178{
1179 if (Func_descriptor_expression::descriptor_type != NULL)
1180 return;
1181 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1182 Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type);
1183 Func_descriptor_expression::descriptor_type =
1184 Type::make_builtin_named_type("functionDescriptor", struct_type);
1185}
1186
1187Type*
1188Func_descriptor_expression::do_type()
1189{
1190 Func_descriptor_expression::make_func_descriptor_type();
1191 return Func_descriptor_expression::descriptor_type;
1192}
1193
ea664253 1194// The backend representation for a function descriptor.
8381eda7 1195
ea664253 1196Bexpression*
1197Func_descriptor_expression::do_get_backend(Translate_context* context)
8381eda7 1198{
8381eda7 1199 Named_object* no = this->fn_;
1200 Location loc = no->location();
ea664253 1201 if (this->dvar_ != NULL)
1202 return context->backend()->var_expression(this->dvar_, loc);
8381eda7 1203
ea664253 1204 Gogo* gogo = context->gogo();
8381eda7 1205 std::string var_name;
09e57698 1206 bool is_descriptor = false;
1207 if (no->is_function_declaration()
1208 && !no->func_declaration_value()->asm_name().empty()
1209 && Linemap::is_predeclared_location(no->location()))
1210 {
1211 var_name = no->func_declaration_value()->asm_name() + "_descriptor";
1212 is_descriptor = true;
1213 }
8381eda7 1214 else
09e57698 1215 {
1216 if (no->package() == NULL)
1217 var_name = gogo->pkgpath_symbol();
1218 else
1219 var_name = no->package()->pkgpath_symbol();
1220 var_name.push_back('.');
1221 var_name.append(Gogo::unpack_hidden_name(no->name()));
1222 var_name.append("$descriptor");
1223 }
8381eda7 1224
1225 Btype* btype = this->type()->get_backend(gogo);
1226
1227 Bvariable* bvar;
09e57698 1228 if (no->package() != NULL || is_descriptor)
f8bdf81a 1229 bvar = context->backend()->immutable_struct_reference(var_name, btype,
1230 loc);
8381eda7 1231 else
1232 {
1233 Location bloc = Linemap::predeclared_location();
1234 bool is_hidden = ((no->is_function()
1235 && no->func_value()->enclosing() != NULL)
1236 || Gogo::is_thunk(no));
1237 bvar = context->backend()->immutable_struct(var_name, is_hidden, false,
1238 btype, bloc);
1239 Expression_list* vals = new Expression_list();
f8bdf81a 1240 vals->push_back(Expression::make_func_code_reference(this->fn_, bloc));
8381eda7 1241 Expression* init =
1242 Expression::make_struct_composite_literal(this->type(), vals, bloc);
1243 Translate_context bcontext(gogo, NULL, NULL, NULL);
1244 bcontext.set_is_const();
ea664253 1245 Bexpression* binit = init->get_backend(&bcontext);
8381eda7 1246 context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden,
1247 false, btype, bloc, binit);
1248 }
1249
1250 this->dvar_ = bvar;
ea664253 1251 return gogo->backend()->var_expression(bvar, loc);
8381eda7 1252}
1253
c6837989 1254// Print a function descriptor expression.
1255
1256void
1257Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const
1258{
1259 context->ostream() << "[descriptor " << this->fn_->name() << "]";
1260}
1261
8381eda7 1262// Make a function descriptor expression.
1263
c6837989 1264Func_descriptor_expression*
1265Expression::make_func_descriptor(Named_object* fn)
8381eda7 1266{
c6837989 1267 return new Func_descriptor_expression(fn);
8381eda7 1268}
1269
1270// Make the function descriptor type, so that it can be converted.
1271
1272void
1273Expression::make_func_descriptor_type()
1274{
1275 Func_descriptor_expression::make_func_descriptor_type();
1276}
1277
1278// A reference to just the code of a function.
1279
1280class Func_code_reference_expression : public Expression
1281{
1282 public:
1283 Func_code_reference_expression(Named_object* function, Location location)
1284 : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location),
1285 function_(function)
1286 { }
1287
1288 protected:
1289 int
1290 do_traverse(Traverse*)
1291 { return TRAVERSE_CONTINUE; }
1292
f9ca30f9 1293 bool
1294 do_is_immutable() const
1295 { return true; }
1296
8381eda7 1297 Type*
1298 do_type()
1299 { return Type::make_pointer_type(Type::make_void_type()); }
1300
1301 void
1302 do_determine_type(const Type_context*)
1303 { }
1304
1305 Expression*
1306 do_copy()
1307 {
1308 return Expression::make_func_code_reference(this->function_,
1309 this->location());
1310 }
1311
ea664253 1312 Bexpression*
1313 do_get_backend(Translate_context*);
8381eda7 1314
1315 void
1316 do_dump_expression(Ast_dump_context* context) const
1317 { context->ostream() << "[raw " << this->function_->name() << "]" ; }
1318
1319 private:
1320 // The function.
1321 Named_object* function_;
1322};
1323
ea664253 1324// Get the backend representation for a reference to function code.
8381eda7 1325
ea664253 1326Bexpression*
1327Func_code_reference_expression::do_get_backend(Translate_context* context)
8381eda7 1328{
ea664253 1329 return Func_expression::get_code_pointer(context->gogo(), this->function_,
1330 this->location());
8381eda7 1331}
1332
1333// Make a reference to the code of a function.
1334
1335Expression*
1336Expression::make_func_code_reference(Named_object* function, Location location)
1337{
1338 return new Func_code_reference_expression(function, location);
1339}
1340
e440a328 1341// Class Unknown_expression.
1342
1343// Return the name of an unknown expression.
1344
1345const std::string&
1346Unknown_expression::name() const
1347{
1348 return this->named_object_->name();
1349}
1350
1351// Lower a reference to an unknown name.
1352
1353Expression*
ceeb4318 1354Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 1355{
b13c66cd 1356 Location location = this->location();
e440a328 1357 Named_object* no = this->named_object_;
deded542 1358 Named_object* real;
1359 if (!no->is_unknown())
1360 real = no;
1361 else
e440a328 1362 {
deded542 1363 real = no->unknown_value()->real_named_object();
1364 if (real == NULL)
1365 {
1366 if (this->is_composite_literal_key_)
1367 return this;
acf8e158 1368 if (!this->no_error_message_)
1369 error_at(location, "reference to undefined name %qs",
1370 this->named_object_->message_name().c_str());
deded542 1371 return Expression::make_error(location);
1372 }
e440a328 1373 }
1374 switch (real->classification())
1375 {
1376 case Named_object::NAMED_OBJECT_CONST:
1377 return Expression::make_const_reference(real, location);
1378 case Named_object::NAMED_OBJECT_TYPE:
1379 return Expression::make_type(real->type_value(), location);
1380 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1381 if (this->is_composite_literal_key_)
1382 return this;
acf8e158 1383 if (!this->no_error_message_)
1384 error_at(location, "reference to undefined type %qs",
1385 real->message_name().c_str());
e440a328 1386 return Expression::make_error(location);
1387 case Named_object::NAMED_OBJECT_VAR:
7d834090 1388 real->var_value()->set_is_used();
e440a328 1389 return Expression::make_var_reference(real, location);
1390 case Named_object::NAMED_OBJECT_FUNC:
1391 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1392 return Expression::make_func_reference(real, NULL, location);
1393 case Named_object::NAMED_OBJECT_PACKAGE:
1394 if (this->is_composite_literal_key_)
1395 return this;
acf8e158 1396 if (!this->no_error_message_)
1397 error_at(location, "unexpected reference to package");
e440a328 1398 return Expression::make_error(location);
1399 default:
c3e6f413 1400 go_unreachable();
e440a328 1401 }
1402}
1403
d751bb78 1404// Dump the ast representation for an unknown expression to a dump context.
1405
1406void
1407Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1408{
1409 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1410 << ")";
d751bb78 1411}
1412
e440a328 1413// Make a reference to an unknown name.
1414
acf8e158 1415Unknown_expression*
b13c66cd 1416Expression::make_unknown_reference(Named_object* no, Location location)
e440a328 1417{
e440a328 1418 return new Unknown_expression(no, location);
1419}
1420
1421// A boolean expression.
1422
1423class Boolean_expression : public Expression
1424{
1425 public:
b13c66cd 1426 Boolean_expression(bool val, Location location)
e440a328 1427 : Expression(EXPRESSION_BOOLEAN, location),
1428 val_(val), type_(NULL)
1429 { }
1430
1431 static Expression*
1432 do_import(Import*);
1433
1434 protected:
1435 bool
1436 do_is_constant() const
1437 { return true; }
1438
0e168074 1439 bool
1440 do_is_immutable() const
1441 { return true; }
1442
e440a328 1443 Type*
1444 do_type();
1445
1446 void
1447 do_determine_type(const Type_context*);
1448
1449 Expression*
1450 do_copy()
1451 { return this; }
1452
ea664253 1453 Bexpression*
1454 do_get_backend(Translate_context* context)
1455 { return context->backend()->boolean_constant_expression(this->val_); }
e440a328 1456
1457 void
1458 do_export(Export* exp) const
1459 { exp->write_c_string(this->val_ ? "true" : "false"); }
1460
d751bb78 1461 void
1462 do_dump_expression(Ast_dump_context* ast_dump_context) const
1463 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1464
e440a328 1465 private:
1466 // The constant.
1467 bool val_;
1468 // The type as determined by context.
1469 Type* type_;
1470};
1471
1472// Get the type.
1473
1474Type*
1475Boolean_expression::do_type()
1476{
1477 if (this->type_ == NULL)
1478 this->type_ = Type::make_boolean_type();
1479 return this->type_;
1480}
1481
1482// Set the type from the context.
1483
1484void
1485Boolean_expression::do_determine_type(const Type_context* context)
1486{
1487 if (this->type_ != NULL && !this->type_->is_abstract())
1488 ;
1489 else if (context->type != NULL && context->type->is_boolean_type())
1490 this->type_ = context->type;
1491 else if (!context->may_be_abstract)
1492 this->type_ = Type::lookup_bool_type();
1493}
1494
1495// Import a boolean constant.
1496
1497Expression*
1498Boolean_expression::do_import(Import* imp)
1499{
1500 if (imp->peek_char() == 't')
1501 {
1502 imp->require_c_string("true");
1503 return Expression::make_boolean(true, imp->location());
1504 }
1505 else
1506 {
1507 imp->require_c_string("false");
1508 return Expression::make_boolean(false, imp->location());
1509 }
1510}
1511
1512// Make a boolean expression.
1513
1514Expression*
b13c66cd 1515Expression::make_boolean(bool val, Location location)
e440a328 1516{
1517 return new Boolean_expression(val, location);
1518}
1519
1520// Class String_expression.
1521
1522// Get the type.
1523
1524Type*
1525String_expression::do_type()
1526{
1527 if (this->type_ == NULL)
1528 this->type_ = Type::make_string_type();
1529 return this->type_;
1530}
1531
1532// Set the type from the context.
1533
1534void
1535String_expression::do_determine_type(const Type_context* context)
1536{
1537 if (this->type_ != NULL && !this->type_->is_abstract())
1538 ;
1539 else if (context->type != NULL && context->type->is_string_type())
1540 this->type_ = context->type;
1541 else if (!context->may_be_abstract)
1542 this->type_ = Type::lookup_string_type();
1543}
1544
1545// Build a string constant.
1546
ea664253 1547Bexpression*
1548String_expression::do_get_backend(Translate_context* context)
e440a328 1549{
2c809f8f 1550 Gogo* gogo = context->gogo();
1551 Btype* btype = Type::make_string_type()->get_backend(gogo);
1552
1553 Location loc = this->location();
1554 std::vector<Bexpression*> init(2);
1555 Bexpression* str_cst =
1556 gogo->backend()->string_constant_expression(this->val_);
1557 init[0] = gogo->backend()->address_expression(str_cst, loc);
1558
1559 Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo);
1560 mpz_t lenval;
1561 mpz_init_set_ui(lenval, this->val_.length());
1562 init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval);
1563 mpz_clear(lenval);
1564
ea664253 1565 return gogo->backend()->constructor_expression(btype, init, loc);
e440a328 1566}
1567
8b1c301d 1568 // Write string literal to string dump.
e440a328 1569
1570void
8b1c301d 1571String_expression::export_string(String_dump* exp,
1572 const String_expression* str)
e440a328 1573{
1574 std::string s;
8b1c301d 1575 s.reserve(str->val_.length() * 4 + 2);
e440a328 1576 s += '"';
8b1c301d 1577 for (std::string::const_iterator p = str->val_.begin();
1578 p != str->val_.end();
e440a328 1579 ++p)
1580 {
1581 if (*p == '\\' || *p == '"')
1582 {
1583 s += '\\';
1584 s += *p;
1585 }
1586 else if (*p >= 0x20 && *p < 0x7f)
1587 s += *p;
1588 else if (*p == '\n')
1589 s += "\\n";
1590 else if (*p == '\t')
1591 s += "\\t";
1592 else
1593 {
1594 s += "\\x";
1595 unsigned char c = *p;
1596 unsigned int dig = c >> 4;
1597 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1598 dig = c & 0xf;
1599 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1600 }
1601 }
1602 s += '"';
1603 exp->write_string(s);
1604}
1605
8b1c301d 1606// Export a string expression.
1607
1608void
1609String_expression::do_export(Export* exp) const
1610{
1611 String_expression::export_string(exp, this);
1612}
1613
e440a328 1614// Import a string expression.
1615
1616Expression*
1617String_expression::do_import(Import* imp)
1618{
1619 imp->require_c_string("\"");
1620 std::string val;
1621 while (true)
1622 {
1623 int c = imp->get_char();
1624 if (c == '"' || c == -1)
1625 break;
1626 if (c != '\\')
1627 val += static_cast<char>(c);
1628 else
1629 {
1630 c = imp->get_char();
1631 if (c == '\\' || c == '"')
1632 val += static_cast<char>(c);
1633 else if (c == 'n')
1634 val += '\n';
1635 else if (c == 't')
1636 val += '\t';
1637 else if (c == 'x')
1638 {
1639 c = imp->get_char();
1640 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1641 c = imp->get_char();
1642 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1643 char v = (vh << 4) | vl;
1644 val += v;
1645 }
1646 else
1647 {
1648 error_at(imp->location(), "bad string constant");
1649 return Expression::make_error(imp->location());
1650 }
1651 }
1652 }
1653 return Expression::make_string(val, imp->location());
1654}
1655
d751bb78 1656// Ast dump for string expression.
1657
1658void
1659String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1660{
8b1c301d 1661 String_expression::export_string(ast_dump_context, this);
d751bb78 1662}
1663
e440a328 1664// Make a string expression.
1665
1666Expression*
b13c66cd 1667Expression::make_string(const std::string& val, Location location)
e440a328 1668{
1669 return new String_expression(val, location);
1670}
1671
2c809f8f 1672// An expression that evaluates to some characteristic of a string.
1673// This is used when indexing, bound-checking, or nil checking a string.
1674
1675class String_info_expression : public Expression
1676{
1677 public:
1678 String_info_expression(Expression* string, String_info string_info,
1679 Location location)
1680 : Expression(EXPRESSION_STRING_INFO, location),
1681 string_(string), string_info_(string_info)
1682 { }
1683
1684 protected:
1685 Type*
1686 do_type();
1687
1688 void
1689 do_determine_type(const Type_context*)
1690 { go_unreachable(); }
1691
1692 Expression*
1693 do_copy()
1694 {
1695 return new String_info_expression(this->string_->copy(), this->string_info_,
1696 this->location());
1697 }
1698
ea664253 1699 Bexpression*
1700 do_get_backend(Translate_context* context);
2c809f8f 1701
1702 void
1703 do_dump_expression(Ast_dump_context*) const;
1704
1705 void
1706 do_issue_nil_check()
1707 { this->string_->issue_nil_check(); }
1708
1709 private:
1710 // The string for which we are getting information.
1711 Expression* string_;
1712 // What information we want.
1713 String_info string_info_;
1714};
1715
1716// Return the type of the string info.
1717
1718Type*
1719String_info_expression::do_type()
1720{
1721 switch (this->string_info_)
1722 {
1723 case STRING_INFO_DATA:
1724 {
1725 Type* byte_type = Type::lookup_integer_type("uint8");
1726 return Type::make_pointer_type(byte_type);
1727 }
1728 case STRING_INFO_LENGTH:
1729 return Type::lookup_integer_type("int");
1730 default:
1731 go_unreachable();
1732 }
1733}
1734
1735// Return string information in GENERIC.
1736
ea664253 1737Bexpression*
1738String_info_expression::do_get_backend(Translate_context* context)
2c809f8f 1739{
1740 Gogo* gogo = context->gogo();
1741
ea664253 1742 Bexpression* bstring = this->string_->get_backend(context);
2c809f8f 1743 switch (this->string_info_)
1744 {
1745 case STRING_INFO_DATA:
1746 case STRING_INFO_LENGTH:
ea664253 1747 return gogo->backend()->struct_field_expression(bstring,
1748 this->string_info_,
1749 this->location());
2c809f8f 1750 break;
1751 default:
1752 go_unreachable();
1753 }
2c809f8f 1754}
1755
1756// Dump ast representation for a type info expression.
1757
1758void
1759String_info_expression::do_dump_expression(
1760 Ast_dump_context* ast_dump_context) const
1761{
1762 ast_dump_context->ostream() << "stringinfo(";
1763 this->string_->dump_expression(ast_dump_context);
1764 ast_dump_context->ostream() << ",";
1765 ast_dump_context->ostream() <<
1766 (this->string_info_ == STRING_INFO_DATA ? "data"
1767 : this->string_info_ == STRING_INFO_LENGTH ? "length"
1768 : "unknown");
1769 ast_dump_context->ostream() << ")";
1770}
1771
1772// Make a string info expression.
1773
1774Expression*
1775Expression::make_string_info(Expression* string, String_info string_info,
1776 Location location)
1777{
1778 return new String_info_expression(string, string_info, location);
1779}
1780
e440a328 1781// Make an integer expression.
1782
1783class Integer_expression : public Expression
1784{
1785 public:
5d4b8566 1786 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1787 Location location)
e440a328 1788 : Expression(EXPRESSION_INTEGER, location),
5d4b8566 1789 type_(type), is_character_constant_(is_character_constant)
e440a328 1790 { mpz_init_set(this->val_, *val); }
1791
1792 static Expression*
1793 do_import(Import*);
1794
8b1c301d 1795 // Write VAL to string dump.
e440a328 1796 static void
8b1c301d 1797 export_integer(String_dump* exp, const mpz_t val);
e440a328 1798
d751bb78 1799 // Write VAL to dump context.
1800 static void
1801 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1802
e440a328 1803 protected:
1804 bool
1805 do_is_constant() const
1806 { return true; }
1807
0e168074 1808 bool
1809 do_is_immutable() const
1810 { return true; }
1811
e440a328 1812 bool
0c77715b 1813 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 1814
1815 Type*
1816 do_type();
1817
1818 void
1819 do_determine_type(const Type_context* context);
1820
1821 void
1822 do_check_types(Gogo*);
1823
ea664253 1824 Bexpression*
1825 do_get_backend(Translate_context*);
e440a328 1826
1827 Expression*
1828 do_copy()
5d4b8566 1829 {
1830 if (this->is_character_constant_)
1831 return Expression::make_character(&this->val_, this->type_,
1832 this->location());
1833 else
e67508fa 1834 return Expression::make_integer_z(&this->val_, this->type_,
1835 this->location());
5d4b8566 1836 }
e440a328 1837
1838 void
1839 do_export(Export*) const;
1840
d751bb78 1841 void
1842 do_dump_expression(Ast_dump_context*) const;
1843
e440a328 1844 private:
1845 // The integer value.
1846 mpz_t val_;
1847 // The type so far.
1848 Type* type_;
5d4b8566 1849 // Whether this is a character constant.
1850 bool is_character_constant_;
e440a328 1851};
1852
0c77715b 1853// Return a numeric constant for this expression. We have to mark
1854// this as a character when appropriate.
e440a328 1855
1856bool
0c77715b 1857Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 1858{
0c77715b 1859 if (this->is_character_constant_)
1860 nc->set_rune(this->type_, this->val_);
1861 else
1862 nc->set_int(this->type_, this->val_);
e440a328 1863 return true;
1864}
1865
1866// Return the current type. If we haven't set the type yet, we return
1867// an abstract integer type.
1868
1869Type*
1870Integer_expression::do_type()
1871{
1872 if (this->type_ == NULL)
5d4b8566 1873 {
1874 if (this->is_character_constant_)
1875 this->type_ = Type::make_abstract_character_type();
1876 else
1877 this->type_ = Type::make_abstract_integer_type();
1878 }
e440a328 1879 return this->type_;
1880}
1881
1882// Set the type of the integer value. Here we may switch from an
1883// abstract type to a real type.
1884
1885void
1886Integer_expression::do_determine_type(const Type_context* context)
1887{
1888 if (this->type_ != NULL && !this->type_->is_abstract())
1889 ;
0c77715b 1890 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 1891 this->type_ = context->type;
1892 else if (!context->may_be_abstract)
5d4b8566 1893 {
1894 if (this->is_character_constant_)
1895 this->type_ = Type::lookup_integer_type("int32");
1896 else
1897 this->type_ = Type::lookup_integer_type("int");
1898 }
e440a328 1899}
1900
e440a328 1901// Check the type of an integer constant.
1902
1903void
1904Integer_expression::do_check_types(Gogo*)
1905{
0c77715b 1906 Type* type = this->type_;
1907 if (type == NULL)
e440a328 1908 return;
0c77715b 1909 Numeric_constant nc;
1910 if (this->is_character_constant_)
1911 nc.set_rune(NULL, this->val_);
1912 else
1913 nc.set_int(NULL, this->val_);
1914 if (!nc.set_type(type, true, this->location()))
e440a328 1915 this->set_is_error();
1916}
1917
ea664253 1918// Get the backend representation for an integer constant.
e440a328 1919
ea664253 1920Bexpression*
1921Integer_expression::do_get_backend(Translate_context* context)
e440a328 1922{
12373dd5 1923 if (this->is_error_expression()
1924 || (this->type_ != NULL && this->type_->is_error_type()))
1925 {
1926 go_assert(saw_errors());
1927 return context->gogo()->backend()->error_expression();
1928 }
1929
48c2a53a 1930 Type* resolved_type = NULL;
e440a328 1931 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 1932 resolved_type = this->type_;
e440a328 1933 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1934 {
1935 // We are converting to an abstract floating point type.
48c2a53a 1936 resolved_type = Type::lookup_float_type("float64");
e440a328 1937 }
1938 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1939 {
1940 // We are converting to an abstract complex type.
48c2a53a 1941 resolved_type = Type::lookup_complex_type("complex128");
e440a328 1942 }
1943 else
1944 {
1945 // If we still have an abstract type here, then this is being
1946 // used in a constant expression which didn't get reduced for
1947 // some reason. Use a type which will fit the value. We use <,
1948 // not <=, because we need an extra bit for the sign bit.
1949 int bits = mpz_sizeinbase(this->val_, 2);
1b1f2abf 1950 Type* int_type = Type::lookup_integer_type("int");
1951 if (bits < int_type->integer_type()->bits())
48c2a53a 1952 resolved_type = int_type;
e440a328 1953 else if (bits < 64)
48c2a53a 1954 resolved_type = Type::lookup_integer_type("int64");
e440a328 1955 else
48c2a53a 1956 {
1957 if (!saw_errors())
1958 error_at(this->location(),
1959 "unknown type for large integer constant");
ea664253 1960 return context->gogo()->backend()->error_expression();
48c2a53a 1961 }
e440a328 1962 }
48c2a53a 1963 Numeric_constant nc;
1964 nc.set_int(resolved_type, this->val_);
ea664253 1965 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 1966}
1967
1968// Write VAL to export data.
1969
1970void
8b1c301d 1971Integer_expression::export_integer(String_dump* exp, const mpz_t val)
e440a328 1972{
1973 char* s = mpz_get_str(NULL, 10, val);
1974 exp->write_c_string(s);
1975 free(s);
1976}
1977
1978// Export an integer in a constant expression.
1979
1980void
1981Integer_expression::do_export(Export* exp) const
1982{
1983 Integer_expression::export_integer(exp, this->val_);
5d4b8566 1984 if (this->is_character_constant_)
1985 exp->write_c_string("'");
e440a328 1986 // A trailing space lets us reliably identify the end of the number.
1987 exp->write_c_string(" ");
1988}
1989
1990// Import an integer, floating point, or complex value. This handles
1991// all these types because they all start with digits.
1992
1993Expression*
1994Integer_expression::do_import(Import* imp)
1995{
1996 std::string num = imp->read_identifier();
1997 imp->require_c_string(" ");
1998 if (!num.empty() && num[num.length() - 1] == 'i')
1999 {
2000 mpfr_t real;
2001 size_t plus_pos = num.find('+', 1);
2002 size_t minus_pos = num.find('-', 1);
2003 size_t pos;
2004 if (plus_pos == std::string::npos)
2005 pos = minus_pos;
2006 else if (minus_pos == std::string::npos)
2007 pos = plus_pos;
2008 else
2009 {
2010 error_at(imp->location(), "bad number in import data: %qs",
2011 num.c_str());
2012 return Expression::make_error(imp->location());
2013 }
2014 if (pos == std::string::npos)
2015 mpfr_set_ui(real, 0, GMP_RNDN);
2016 else
2017 {
2018 std::string real_str = num.substr(0, pos);
2019 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
2020 {
2021 error_at(imp->location(), "bad number in import data: %qs",
2022 real_str.c_str());
2023 return Expression::make_error(imp->location());
2024 }
2025 }
2026
2027 std::string imag_str;
2028 if (pos == std::string::npos)
2029 imag_str = num;
2030 else
2031 imag_str = num.substr(pos);
2032 imag_str = imag_str.substr(0, imag_str.size() - 1);
2033 mpfr_t imag;
2034 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2035 {
2036 error_at(imp->location(), "bad number in import data: %qs",
2037 imag_str.c_str());
2038 return Expression::make_error(imp->location());
2039 }
fcbea5e4 2040 mpc_t cval;
2041 mpc_init2(cval, mpc_precision);
2042 mpc_set_fr_fr(cval, real, imag, MPC_RNDNN);
e440a328 2043 mpfr_clear(real);
2044 mpfr_clear(imag);
fcbea5e4 2045 Expression* ret = Expression::make_complex(&cval, NULL, imp->location());
2046 mpc_clear(cval);
e440a328 2047 return ret;
2048 }
2049 else if (num.find('.') == std::string::npos
2050 && num.find('E') == std::string::npos)
2051 {
5d4b8566 2052 bool is_character_constant = (!num.empty()
2053 && num[num.length() - 1] == '\'');
2054 if (is_character_constant)
2055 num = num.substr(0, num.length() - 1);
e440a328 2056 mpz_t val;
2057 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2058 {
2059 error_at(imp->location(), "bad number in import data: %qs",
2060 num.c_str());
2061 return Expression::make_error(imp->location());
2062 }
5d4b8566 2063 Expression* ret;
2064 if (is_character_constant)
2065 ret = Expression::make_character(&val, NULL, imp->location());
2066 else
e67508fa 2067 ret = Expression::make_integer_z(&val, NULL, imp->location());
e440a328 2068 mpz_clear(val);
2069 return ret;
2070 }
2071 else
2072 {
2073 mpfr_t val;
2074 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2075 {
2076 error_at(imp->location(), "bad number in import data: %qs",
2077 num.c_str());
2078 return Expression::make_error(imp->location());
2079 }
2080 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2081 mpfr_clear(val);
2082 return ret;
2083 }
2084}
d751bb78 2085// Ast dump for integer expression.
2086
2087void
2088Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2089{
5d4b8566 2090 if (this->is_character_constant_)
2091 ast_dump_context->ostream() << '\'';
8b1c301d 2092 Integer_expression::export_integer(ast_dump_context, this->val_);
5d4b8566 2093 if (this->is_character_constant_)
2094 ast_dump_context->ostream() << '\'';
d751bb78 2095}
2096
e67508fa 2097// Build a new integer value from a multi-precision integer.
e440a328 2098
2099Expression*
e67508fa 2100Expression::make_integer_z(const mpz_t* val, Type* type, Location location)
5d4b8566 2101{
2102 return new Integer_expression(val, type, false, location);
2103}
2104
e67508fa 2105// Build a new integer value from an unsigned long.
2106
2107Expression*
2108Expression::make_integer_ul(unsigned long val, Type *type, Location location)
2109{
2110 mpz_t zval;
2111 mpz_init_set_ui(zval, val);
2112 Expression* ret = Expression::make_integer_z(&zval, type, location);
2113 mpz_clear(zval);
2114 return ret;
2115}
2116
2117// Build a new integer value from a signed long.
2118
2119Expression*
2120Expression::make_integer_sl(long val, Type *type, Location location)
2121{
2122 mpz_t zval;
2123 mpz_init_set_si(zval, val);
2124 Expression* ret = Expression::make_integer_z(&zval, type, location);
2125 mpz_clear(zval);
2126 return ret;
2127}
2128
3f378015 2129// Store an int64_t in an uninitialized mpz_t.
2130
2131static void
2132set_mpz_from_int64(mpz_t* zval, int64_t val)
2133{
2134 if (val >= 0)
2135 {
2136 unsigned long ul = static_cast<unsigned long>(val);
2137 if (static_cast<int64_t>(ul) == val)
2138 {
2139 mpz_init_set_ui(*zval, ul);
2140 return;
2141 }
2142 }
2143 uint64_t uv;
2144 if (val >= 0)
2145 uv = static_cast<uint64_t>(val);
2146 else
2147 uv = static_cast<uint64_t>(- val);
2148 unsigned long ul = uv & 0xffffffffUL;
2149 mpz_init_set_ui(*zval, ul);
2150 mpz_t hval;
2151 mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32));
2152 mpz_mul_2exp(hval, hval, 32);
2153 mpz_add(*zval, *zval, hval);
2154 mpz_clear(hval);
2155 if (val < 0)
2156 mpz_neg(*zval, *zval);
2157}
2158
2159// Build a new integer value from an int64_t.
2160
2161Expression*
2162Expression::make_integer_int64(int64_t val, Type* type, Location location)
2163{
2164 mpz_t zval;
2165 set_mpz_from_int64(&zval, val);
2166 Expression* ret = Expression::make_integer_z(&zval, type, location);
2167 mpz_clear(zval);
2168 return ret;
2169}
2170
5d4b8566 2171// Build a new character constant value.
2172
2173Expression*
2174Expression::make_character(const mpz_t* val, Type* type, Location location)
e440a328 2175{
5d4b8566 2176 return new Integer_expression(val, type, true, location);
e440a328 2177}
2178
2179// Floats.
2180
2181class Float_expression : public Expression
2182{
2183 public:
b13c66cd 2184 Float_expression(const mpfr_t* val, Type* type, Location location)
e440a328 2185 : Expression(EXPRESSION_FLOAT, location),
2186 type_(type)
2187 {
2188 mpfr_init_set(this->val_, *val, GMP_RNDN);
2189 }
2190
e440a328 2191 // Write VAL to export data.
2192 static void
8b1c301d 2193 export_float(String_dump* exp, const mpfr_t val);
2194
d751bb78 2195 // Write VAL to dump file.
2196 static void
2197 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
e440a328 2198
2199 protected:
2200 bool
2201 do_is_constant() const
2202 { return true; }
2203
0e168074 2204 bool
2205 do_is_immutable() const
2206 { return true; }
2207
e440a328 2208 bool
0c77715b 2209 do_numeric_constant_value(Numeric_constant* nc) const
2210 {
2211 nc->set_float(this->type_, this->val_);
2212 return true;
2213 }
e440a328 2214
2215 Type*
2216 do_type();
2217
2218 void
2219 do_determine_type(const Type_context*);
2220
2221 void
2222 do_check_types(Gogo*);
2223
2224 Expression*
2225 do_copy()
2226 { return Expression::make_float(&this->val_, this->type_,
2227 this->location()); }
2228
ea664253 2229 Bexpression*
2230 do_get_backend(Translate_context*);
e440a328 2231
2232 void
2233 do_export(Export*) const;
2234
d751bb78 2235 void
2236 do_dump_expression(Ast_dump_context*) const;
2237
e440a328 2238 private:
2239 // The floating point value.
2240 mpfr_t val_;
2241 // The type so far.
2242 Type* type_;
2243};
2244
e440a328 2245// Return the current type. If we haven't set the type yet, we return
2246// an abstract float type.
2247
2248Type*
2249Float_expression::do_type()
2250{
2251 if (this->type_ == NULL)
2252 this->type_ = Type::make_abstract_float_type();
2253 return this->type_;
2254}
2255
2256// Set the type of the float value. Here we may switch from an
2257// abstract type to a real type.
2258
2259void
2260Float_expression::do_determine_type(const Type_context* context)
2261{
2262 if (this->type_ != NULL && !this->type_->is_abstract())
2263 ;
2264 else if (context->type != NULL
2265 && (context->type->integer_type() != NULL
2266 || context->type->float_type() != NULL
2267 || context->type->complex_type() != NULL))
2268 this->type_ = context->type;
2269 else if (!context->may_be_abstract)
48080209 2270 this->type_ = Type::lookup_float_type("float64");
e440a328 2271}
2272
e440a328 2273// Check the type of a float value.
2274
2275void
2276Float_expression::do_check_types(Gogo*)
2277{
0c77715b 2278 Type* type = this->type_;
2279 if (type == NULL)
e440a328 2280 return;
0c77715b 2281 Numeric_constant nc;
2282 nc.set_float(NULL, this->val_);
2283 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2284 this->set_is_error();
e440a328 2285}
2286
ea664253 2287// Get the backend representation for a float constant.
e440a328 2288
ea664253 2289Bexpression*
2290Float_expression::do_get_backend(Translate_context* context)
e440a328 2291{
12373dd5 2292 if (this->is_error_expression()
2293 || (this->type_ != NULL && this->type_->is_error_type()))
2294 {
2295 go_assert(saw_errors());
2296 return context->gogo()->backend()->error_expression();
2297 }
2298
48c2a53a 2299 Type* resolved_type;
e440a328 2300 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2301 resolved_type = this->type_;
e440a328 2302 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2303 {
2304 // We have an abstract integer type. We just hope for the best.
48c2a53a 2305 resolved_type = Type::lookup_integer_type("int");
2306 }
2307 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
2308 {
2309 // We are converting to an abstract complex type.
2310 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2311 }
2312 else
2313 {
2314 // If we still have an abstract type here, then this is being
2315 // used in a constant expression which didn't get reduced. We
2316 // just use float64 and hope for the best.
48c2a53a 2317 resolved_type = Type::lookup_float_type("float64");
e440a328 2318 }
48c2a53a 2319
2320 Numeric_constant nc;
2321 nc.set_float(resolved_type, this->val_);
ea664253 2322 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2323}
2324
8b1c301d 2325// Write a floating point number to a string dump.
e440a328 2326
2327void
8b1c301d 2328Float_expression::export_float(String_dump *exp, const mpfr_t val)
e440a328 2329{
2330 mp_exp_t exponent;
2331 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2332 if (*s == '-')
2333 exp->write_c_string("-");
2334 exp->write_c_string("0.");
2335 exp->write_c_string(*s == '-' ? s + 1 : s);
2336 mpfr_free_str(s);
2337 char buf[30];
2338 snprintf(buf, sizeof buf, "E%ld", exponent);
2339 exp->write_c_string(buf);
2340}
2341
2342// Export a floating point number in a constant expression.
2343
2344void
2345Float_expression::do_export(Export* exp) const
2346{
2347 Float_expression::export_float(exp, this->val_);
2348 // A trailing space lets us reliably identify the end of the number.
2349 exp->write_c_string(" ");
2350}
2351
d751bb78 2352// Dump a floating point number to the dump file.
2353
2354void
2355Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2356{
8b1c301d 2357 Float_expression::export_float(ast_dump_context, this->val_);
d751bb78 2358}
2359
e440a328 2360// Make a float expression.
2361
2362Expression*
b13c66cd 2363Expression::make_float(const mpfr_t* val, Type* type, Location location)
e440a328 2364{
2365 return new Float_expression(val, type, location);
2366}
2367
2368// Complex numbers.
2369
2370class Complex_expression : public Expression
2371{
2372 public:
fcbea5e4 2373 Complex_expression(const mpc_t* val, Type* type, Location location)
e440a328 2374 : Expression(EXPRESSION_COMPLEX, location),
2375 type_(type)
2376 {
fcbea5e4 2377 mpc_init2(this->val_, mpc_precision);
2378 mpc_set(this->val_, *val, MPC_RNDNN);
e440a328 2379 }
2380
fcbea5e4 2381 // Write VAL to string dump.
e440a328 2382 static void
fcbea5e4 2383 export_complex(String_dump* exp, const mpc_t val);
e440a328 2384
d751bb78 2385 // Write REAL/IMAG to dump context.
2386 static void
fcbea5e4 2387 dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val);
d751bb78 2388
e440a328 2389 protected:
2390 bool
2391 do_is_constant() const
2392 { return true; }
2393
0e168074 2394 bool
2395 do_is_immutable() const
2396 { return true; }
2397
e440a328 2398 bool
0c77715b 2399 do_numeric_constant_value(Numeric_constant* nc) const
2400 {
fcbea5e4 2401 nc->set_complex(this->type_, this->val_);
0c77715b 2402 return true;
2403 }
e440a328 2404
2405 Type*
2406 do_type();
2407
2408 void
2409 do_determine_type(const Type_context*);
2410
2411 void
2412 do_check_types(Gogo*);
2413
2414 Expression*
2415 do_copy()
2416 {
fcbea5e4 2417 return Expression::make_complex(&this->val_, this->type_,
e440a328 2418 this->location());
2419 }
2420
ea664253 2421 Bexpression*
2422 do_get_backend(Translate_context*);
e440a328 2423
2424 void
2425 do_export(Export*) const;
2426
d751bb78 2427 void
2428 do_dump_expression(Ast_dump_context*) const;
abd26de0 2429
e440a328 2430 private:
fcbea5e4 2431 // The complex value.
2432 mpc_t val_;
e440a328 2433 // The type if known.
2434 Type* type_;
2435};
2436
e440a328 2437// Return the current type. If we haven't set the type yet, we return
2438// an abstract complex type.
2439
2440Type*
2441Complex_expression::do_type()
2442{
2443 if (this->type_ == NULL)
2444 this->type_ = Type::make_abstract_complex_type();
2445 return this->type_;
2446}
2447
2448// Set the type of the complex value. Here we may switch from an
2449// abstract type to a real type.
2450
2451void
2452Complex_expression::do_determine_type(const Type_context* context)
2453{
2454 if (this->type_ != NULL && !this->type_->is_abstract())
2455 ;
abd26de0 2456 else if (context->type != NULL && context->type->is_numeric_type())
e440a328 2457 this->type_ = context->type;
2458 else if (!context->may_be_abstract)
48080209 2459 this->type_ = Type::lookup_complex_type("complex128");
e440a328 2460}
2461
e440a328 2462// Check the type of a complex value.
2463
2464void
2465Complex_expression::do_check_types(Gogo*)
2466{
0c77715b 2467 Type* type = this->type_;
2468 if (type == NULL)
e440a328 2469 return;
0c77715b 2470 Numeric_constant nc;
fcbea5e4 2471 nc.set_complex(NULL, this->val_);
0c77715b 2472 if (!nc.set_type(this->type_, true, this->location()))
e440a328 2473 this->set_is_error();
2474}
2475
ea664253 2476// Get the backend representation for a complex constant.
e440a328 2477
ea664253 2478Bexpression*
2479Complex_expression::do_get_backend(Translate_context* context)
e440a328 2480{
12373dd5 2481 if (this->is_error_expression()
2482 || (this->type_ != NULL && this->type_->is_error_type()))
2483 {
2484 go_assert(saw_errors());
2485 return context->gogo()->backend()->error_expression();
2486 }
2487
48c2a53a 2488 Type* resolved_type;
e440a328 2489 if (this->type_ != NULL && !this->type_->is_abstract())
48c2a53a 2490 resolved_type = this->type_;
2491 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2492 {
2493 // We are converting to an abstract integer type.
2494 resolved_type = Type::lookup_integer_type("int");
2495 }
2496 else if (this->type_ != NULL && this->type_->float_type() != NULL)
2497 {
2498 // We are converting to an abstract float type.
2499 resolved_type = Type::lookup_float_type("float64");
2500 }
e440a328 2501 else
2502 {
47ae02b7 2503 // If we still have an abstract type here, this is being
e440a328 2504 // used in a constant expression which didn't get reduced. We
2505 // just use complex128 and hope for the best.
48c2a53a 2506 resolved_type = Type::lookup_complex_type("complex128");
e440a328 2507 }
48c2a53a 2508
2509 Numeric_constant nc;
fcbea5e4 2510 nc.set_complex(resolved_type, this->val_);
ea664253 2511 return Expression::backend_numeric_constant_expression(context, &nc);
e440a328 2512}
2513
2514// Write REAL/IMAG to export data.
2515
2516void
fcbea5e4 2517Complex_expression::export_complex(String_dump* exp, const mpc_t val)
e440a328 2518{
fcbea5e4 2519 if (!mpfr_zero_p(mpc_realref(val)))
e440a328 2520 {
fcbea5e4 2521 Float_expression::export_float(exp, mpc_realref(val));
d1db782d 2522 if (mpfr_sgn(mpc_imagref(val)) >= 0)
e440a328 2523 exp->write_c_string("+");
2524 }
fcbea5e4 2525 Float_expression::export_float(exp, mpc_imagref(val));
e440a328 2526 exp->write_c_string("i");
2527}
2528
2529// Export a complex number in a constant expression.
2530
2531void
2532Complex_expression::do_export(Export* exp) const
2533{
fcbea5e4 2534 Complex_expression::export_complex(exp, this->val_);
e440a328 2535 // A trailing space lets us reliably identify the end of the number.
2536 exp->write_c_string(" ");
2537}
2538
d751bb78 2539// Dump a complex expression to the dump file.
2540
2541void
2542Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2543{
fcbea5e4 2544 Complex_expression::export_complex(ast_dump_context, this->val_);
d751bb78 2545}
2546
e440a328 2547// Make a complex expression.
2548
2549Expression*
fcbea5e4 2550Expression::make_complex(const mpc_t* val, Type* type, Location location)
e440a328 2551{
fcbea5e4 2552 return new Complex_expression(val, type, location);
e440a328 2553}
2554
d5b605df 2555// Find a named object in an expression.
2556
2557class Find_named_object : public Traverse
2558{
2559 public:
2560 Find_named_object(Named_object* no)
2561 : Traverse(traverse_expressions),
2562 no_(no), found_(false)
2563 { }
2564
2565 // Whether we found the object.
2566 bool
2567 found() const
2568 { return this->found_; }
2569
2570 protected:
2571 int
2572 expression(Expression**);
2573
2574 private:
2575 // The object we are looking for.
2576 Named_object* no_;
2577 // Whether we found it.
2578 bool found_;
2579};
2580
e440a328 2581// A reference to a const in an expression.
2582
2583class Const_expression : public Expression
2584{
2585 public:
b13c66cd 2586 Const_expression(Named_object* constant, Location location)
e440a328 2587 : Expression(EXPRESSION_CONST_REFERENCE, location),
13e818f5 2588 constant_(constant), type_(NULL), seen_(false)
e440a328 2589 { }
2590
d5b605df 2591 Named_object*
2592 named_object()
2593 { return this->constant_; }
2594
a7f064d5 2595 // Check that the initializer does not refer to the constant itself.
2596 void
2597 check_for_init_loop();
2598
e440a328 2599 protected:
ba4aedd4 2600 int
2601 do_traverse(Traverse*);
2602
e440a328 2603 Expression*
ceeb4318 2604 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 2605
2606 bool
2607 do_is_constant() const
2608 { return true; }
2609
0e168074 2610 bool
2611 do_is_immutable() const
2612 { return true; }
2613
e440a328 2614 bool
0c77715b 2615 do_numeric_constant_value(Numeric_constant* nc) const;
e440a328 2616
2617 bool
af6b489a 2618 do_string_constant_value(std::string* val) const;
e440a328 2619
2620 Type*
2621 do_type();
2622
2623 // The type of a const is set by the declaration, not the use.
2624 void
2625 do_determine_type(const Type_context*);
2626
2627 void
2628 do_check_types(Gogo*);
2629
2630 Expression*
2631 do_copy()
2632 { return this; }
2633
ea664253 2634 Bexpression*
2635 do_get_backend(Translate_context* context);
e440a328 2636
2637 // When exporting a reference to a const as part of a const
2638 // expression, we export the value. We ignore the fact that it has
2639 // a name.
2640 void
2641 do_export(Export* exp) const
2642 { this->constant_->const_value()->expr()->export_expression(exp); }
2643
d751bb78 2644 void
2645 do_dump_expression(Ast_dump_context*) const;
2646
e440a328 2647 private:
2648 // The constant.
2649 Named_object* constant_;
2650 // The type of this reference. This is used if the constant has an
2651 // abstract type.
2652 Type* type_;
13e818f5 2653 // Used to prevent infinite recursion when a constant incorrectly
2654 // refers to itself.
2655 mutable bool seen_;
e440a328 2656};
2657
ba4aedd4 2658// Traversal.
2659
2660int
2661Const_expression::do_traverse(Traverse* traverse)
2662{
2663 if (this->type_ != NULL)
2664 return Type::traverse(this->type_, traverse);
2665 return TRAVERSE_CONTINUE;
2666}
2667
e440a328 2668// Lower a constant expression. This is where we convert the
2669// predeclared constant iota into an integer value.
2670
2671Expression*
ceeb4318 2672Const_expression::do_lower(Gogo* gogo, Named_object*,
2673 Statement_inserter*, int iota_value)
e440a328 2674{
2675 if (this->constant_->const_value()->expr()->classification()
2676 == EXPRESSION_IOTA)
2677 {
2678 if (iota_value == -1)
2679 {
2680 error_at(this->location(),
2681 "iota is only defined in const declarations");
2682 iota_value = 0;
2683 }
e67508fa 2684 return Expression::make_integer_ul(iota_value, NULL, this->location());
e440a328 2685 }
2686
2687 // Make sure that the constant itself has been lowered.
2688 gogo->lower_constant(this->constant_);
2689
2690 return this;
2691}
2692
0c77715b 2693// Return a numeric constant value.
e440a328 2694
2695bool
0c77715b 2696Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 2697{
13e818f5 2698 if (this->seen_)
2699 return false;
2700
e440a328 2701 Expression* e = this->constant_->const_value()->expr();
0c77715b 2702
13e818f5 2703 this->seen_ = true;
2704
0c77715b 2705 bool r = e->numeric_constant_value(nc);
e440a328 2706
13e818f5 2707 this->seen_ = false;
2708
e440a328 2709 Type* ctype;
2710 if (this->type_ != NULL)
2711 ctype = this->type_;
2712 else
2713 ctype = this->constant_->const_value()->type();
e440a328 2714 if (r && ctype != NULL)
2715 {
0c77715b 2716 if (!nc->set_type(ctype, false, this->location()))
e440a328 2717 return false;
e440a328 2718 }
e440a328 2719
e440a328 2720 return r;
2721}
2722
af6b489a 2723bool
2724Const_expression::do_string_constant_value(std::string* val) const
2725{
2726 if (this->seen_)
2727 return false;
2728
2729 Expression* e = this->constant_->const_value()->expr();
2730
2731 this->seen_ = true;
2732 bool ok = e->string_constant_value(val);
2733 this->seen_ = false;
2734
2735 return ok;
2736}
2737
e440a328 2738// Return the type of the const reference.
2739
2740Type*
2741Const_expression::do_type()
2742{
2743 if (this->type_ != NULL)
2744 return this->type_;
13e818f5 2745
2f78f012 2746 Named_constant* nc = this->constant_->const_value();
2747
2748 if (this->seen_ || nc->lowering())
13e818f5 2749 {
2750 this->report_error(_("constant refers to itself"));
2751 this->type_ = Type::make_error_type();
2752 return this->type_;
2753 }
2754
2755 this->seen_ = true;
2756
e440a328 2757 Type* ret = nc->type();
13e818f5 2758
e440a328 2759 if (ret != NULL)
13e818f5 2760 {
2761 this->seen_ = false;
2762 return ret;
2763 }
2764
e440a328 2765 // During parsing, a named constant may have a NULL type, but we
2766 // must not return a NULL type here.
13e818f5 2767 ret = nc->expr()->type();
2768
2769 this->seen_ = false;
2770
2771 return ret;
e440a328 2772}
2773
2774// Set the type of the const reference.
2775
2776void
2777Const_expression::do_determine_type(const Type_context* context)
2778{
2779 Type* ctype = this->constant_->const_value()->type();
2780 Type* cetype = (ctype != NULL
2781 ? ctype
2782 : this->constant_->const_value()->expr()->type());
2783 if (ctype != NULL && !ctype->is_abstract())
2784 ;
2785 else if (context->type != NULL
0c77715b 2786 && context->type->is_numeric_type()
2787 && cetype->is_numeric_type())
e440a328 2788 this->type_ = context->type;
2789 else if (context->type != NULL
2790 && context->type->is_string_type()
2791 && cetype->is_string_type())
2792 this->type_ = context->type;
2793 else if (context->type != NULL
2794 && context->type->is_boolean_type()
2795 && cetype->is_boolean_type())
2796 this->type_ = context->type;
2797 else if (!context->may_be_abstract)
2798 {
2799 if (cetype->is_abstract())
2800 cetype = cetype->make_non_abstract_type();
2801 this->type_ = cetype;
2802 }
2803}
2804
a7f064d5 2805// Check for a loop in which the initializer of a constant refers to
2806// the constant itself.
e440a328 2807
2808void
a7f064d5 2809Const_expression::check_for_init_loop()
e440a328 2810{
5c13bd80 2811 if (this->type_ != NULL && this->type_->is_error())
d5b605df 2812 return;
2813
a7f064d5 2814 if (this->seen_)
2815 {
2816 this->report_error(_("constant refers to itself"));
2817 this->type_ = Type::make_error_type();
2818 return;
2819 }
2820
d5b605df 2821 Expression* init = this->constant_->const_value()->expr();
2822 Find_named_object find_named_object(this->constant_);
a7f064d5 2823
2824 this->seen_ = true;
d5b605df 2825 Expression::traverse(&init, &find_named_object);
a7f064d5 2826 this->seen_ = false;
2827
d5b605df 2828 if (find_named_object.found())
2829 {
5c13bd80 2830 if (this->type_ == NULL || !this->type_->is_error())
a7f064d5 2831 {
2832 this->report_error(_("constant refers to itself"));
2833 this->type_ = Type::make_error_type();
2834 }
d5b605df 2835 return;
2836 }
a7f064d5 2837}
2838
2839// Check types of a const reference.
2840
2841void
2842Const_expression::do_check_types(Gogo*)
2843{
5c13bd80 2844 if (this->type_ != NULL && this->type_->is_error())
a7f064d5 2845 return;
2846
2847 this->check_for_init_loop();
d5b605df 2848
0c77715b 2849 // Check that numeric constant fits in type.
2850 if (this->type_ != NULL && this->type_->is_numeric_type())
e440a328 2851 {
0c77715b 2852 Numeric_constant nc;
2853 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
e440a328 2854 {
0c77715b 2855 if (!nc.set_type(this->type_, true, this->location()))
2856 this->set_is_error();
e440a328 2857 }
e440a328 2858 }
2859}
2860
ea664253 2861// Return the backend representation for a const reference.
e440a328 2862
ea664253 2863Bexpression*
2864Const_expression::do_get_backend(Translate_context* context)
e440a328 2865{
12373dd5 2866 if (this->is_error_expression()
2867 || (this->type_ != NULL && this->type_->is_error()))
2868 {
2869 go_assert(saw_errors());
2870 return context->backend()->error_expression();
2871 }
e440a328 2872
2873 // If the type has been set for this expression, but the underlying
2874 // object is an abstract int or float, we try to get the abstract
2875 // value. Otherwise we may lose something in the conversion.
f2de4532 2876 Expression* expr = this->constant_->const_value()->expr();
e440a328 2877 if (this->type_ != NULL
0c77715b 2878 && this->type_->is_numeric_type()
a68492b4 2879 && (this->constant_->const_value()->type() == NULL
2880 || this->constant_->const_value()->type()->is_abstract()))
e440a328 2881 {
0c77715b 2882 Numeric_constant nc;
2883 if (expr->numeric_constant_value(&nc)
2884 && nc.set_type(this->type_, false, this->location()))
e440a328 2885 {
0c77715b 2886 Expression* e = nc.expression(this->location());
ea664253 2887 return e->get_backend(context);
e440a328 2888 }
e440a328 2889 }
2890
2c809f8f 2891 if (this->type_ != NULL)
f2de4532 2892 expr = Expression::make_cast(this->type_, expr, this->location());
ea664253 2893 return expr->get_backend(context);
e440a328 2894}
2895
d751bb78 2896// Dump ast representation for constant expression.
2897
2898void
2899Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2900{
2901 ast_dump_context->ostream() << this->constant_->name();
2902}
2903
e440a328 2904// Make a reference to a constant in an expression.
2905
2906Expression*
2907Expression::make_const_reference(Named_object* constant,
b13c66cd 2908 Location location)
e440a328 2909{
2910 return new Const_expression(constant, location);
2911}
2912
d5b605df 2913// Find a named object in an expression.
2914
2915int
2916Find_named_object::expression(Expression** pexpr)
2917{
2918 switch ((*pexpr)->classification())
2919 {
2920 case Expression::EXPRESSION_CONST_REFERENCE:
a7f064d5 2921 {
2922 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2923 if (ce->named_object() == this->no_)
2924 break;
2925
2926 // We need to check a constant initializer explicitly, as
2927 // loops here will not be caught by the loop checking for
2928 // variable initializers.
2929 ce->check_for_init_loop();
2930
2931 return TRAVERSE_CONTINUE;
2932 }
2933
d5b605df 2934 case Expression::EXPRESSION_VAR_REFERENCE:
2935 if ((*pexpr)->var_expression()->named_object() == this->no_)
2936 break;
2937 return TRAVERSE_CONTINUE;
2938 case Expression::EXPRESSION_FUNC_REFERENCE:
2939 if ((*pexpr)->func_expression()->named_object() == this->no_)
2940 break;
2941 return TRAVERSE_CONTINUE;
2942 default:
2943 return TRAVERSE_CONTINUE;
2944 }
2945 this->found_ = true;
2946 return TRAVERSE_EXIT;
2947}
2948
e440a328 2949// The nil value.
2950
2951class Nil_expression : public Expression
2952{
2953 public:
b13c66cd 2954 Nil_expression(Location location)
e440a328 2955 : Expression(EXPRESSION_NIL, location)
2956 { }
2957
2958 static Expression*
2959 do_import(Import*);
2960
2961 protected:
2962 bool
2963 do_is_constant() const
2964 { return true; }
2965
f9ca30f9 2966 bool
2967 do_is_immutable() const
2968 { return true; }
2969
e440a328 2970 Type*
2971 do_type()
2972 { return Type::make_nil_type(); }
2973
2974 void
2975 do_determine_type(const Type_context*)
2976 { }
2977
2978 Expression*
2979 do_copy()
2980 { return this; }
2981
ea664253 2982 Bexpression*
2983 do_get_backend(Translate_context* context)
2984 { return context->backend()->nil_pointer_expression(); }
e440a328 2985
2986 void
2987 do_export(Export* exp) const
2988 { exp->write_c_string("nil"); }
d751bb78 2989
2990 void
2991 do_dump_expression(Ast_dump_context* ast_dump_context) const
2992 { ast_dump_context->ostream() << "nil"; }
e440a328 2993};
2994
2995// Import a nil expression.
2996
2997Expression*
2998Nil_expression::do_import(Import* imp)
2999{
3000 imp->require_c_string("nil");
3001 return Expression::make_nil(imp->location());
3002}
3003
3004// Make a nil expression.
3005
3006Expression*
b13c66cd 3007Expression::make_nil(Location location)
e440a328 3008{
3009 return new Nil_expression(location);
3010}
3011
3012// The value of the predeclared constant iota. This is little more
3013// than a marker. This will be lowered to an integer in
3014// Const_expression::do_lower, which is where we know the value that
3015// it should have.
3016
3017class Iota_expression : public Parser_expression
3018{
3019 public:
b13c66cd 3020 Iota_expression(Location location)
e440a328 3021 : Parser_expression(EXPRESSION_IOTA, location)
3022 { }
3023
3024 protected:
3025 Expression*
ceeb4318 3026 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
c3e6f413 3027 { go_unreachable(); }
e440a328 3028
3029 // There should only ever be one of these.
3030 Expression*
3031 do_copy()
c3e6f413 3032 { go_unreachable(); }
d751bb78 3033
3034 void
3035 do_dump_expression(Ast_dump_context* ast_dump_context) const
3036 { ast_dump_context->ostream() << "iota"; }
e440a328 3037};
3038
3039// Make an iota expression. This is only called for one case: the
3040// value of the predeclared constant iota.
3041
3042Expression*
3043Expression::make_iota()
3044{
b13c66cd 3045 static Iota_expression iota_expression(Linemap::unknown_location());
e440a328 3046 return &iota_expression;
3047}
3048
da244e59 3049// Class Type_conversion_expression.
e440a328 3050
3051// Traversal.
3052
3053int
3054Type_conversion_expression::do_traverse(Traverse* traverse)
3055{
3056 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3057 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3058 return TRAVERSE_EXIT;
3059 return TRAVERSE_CONTINUE;
3060}
3061
3062// Convert to a constant at lowering time.
3063
3064Expression*
ceeb4318 3065Type_conversion_expression::do_lower(Gogo*, Named_object*,
3066 Statement_inserter*, int)
e440a328 3067{
3068 Type* type = this->type_;
3069 Expression* val = this->expr_;
b13c66cd 3070 Location location = this->location();
e440a328 3071
0c77715b 3072 if (type->is_numeric_type())
e440a328 3073 {
0c77715b 3074 Numeric_constant nc;
3075 if (val->numeric_constant_value(&nc))
e440a328 3076 {
0c77715b 3077 if (!nc.set_type(type, true, location))
3078 return Expression::make_error(location);
3079 return nc.expression(location);
e440a328 3080 }
e440a328 3081 }
3082
d7739c9a 3083 // According to the language specification on string conversions
3084 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
3085 // When converting an integer into a string, the string will be a UTF-8
3086 // representation of the integer and integers "outside the range of valid
3087 // Unicode code points are converted to '\uFFFD'."
3088 if (type->is_string_type())
3089 {
3090 Numeric_constant nc;
3091 if (val->numeric_constant_value(&nc) && nc.is_int())
3092 {
3093 // An integer value doesn't fit in the Unicode code point range if it
3094 // overflows the Go "int" type or is negative.
3095 unsigned long ul;
3096 if (!nc.set_type(Type::lookup_integer_type("int"), false, location)
3097 || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE)
3098 return Expression::make_string("\ufffd", location);
3099 }
3100 }
3101
55072f2b 3102 if (type->is_slice_type())
e440a328 3103 {
3104 Type* element_type = type->array_type()->element_type()->forwarded();
60963afd 3105 bool is_byte = (element_type->integer_type() != NULL
3106 && element_type->integer_type()->is_byte());
3107 bool is_rune = (element_type->integer_type() != NULL
3108 && element_type->integer_type()->is_rune());
3109 if (is_byte || is_rune)
e440a328 3110 {
3111 std::string s;
3112 if (val->string_constant_value(&s))
3113 {
3114 Expression_list* vals = new Expression_list();
3115 if (is_byte)
3116 {
3117 for (std::string::const_iterator p = s.begin();
3118 p != s.end();
3119 p++)
3120 {
e67508fa 3121 unsigned char c = static_cast<unsigned char>(*p);
3122 vals->push_back(Expression::make_integer_ul(c,
3123 element_type,
3124 location));
e440a328 3125 }
3126 }
3127 else
3128 {
3129 const char *p = s.data();
3130 const char *pend = s.data() + s.length();
3131 while (p < pend)
3132 {
3133 unsigned int c;
3134 int adv = Lex::fetch_char(p, &c);
3135 if (adv == 0)
3136 {
3137 warning_at(this->location(), 0,
3138 "invalid UTF-8 encoding");
3139 adv = 1;
3140 }
3141 p += adv;
e67508fa 3142 vals->push_back(Expression::make_integer_ul(c,
3143 element_type,
3144 location));
e440a328 3145 }
3146 }
3147
3148 return Expression::make_slice_composite_literal(type, vals,
3149 location);
3150 }
3151 }
3152 }
3153
3154 return this;
3155}
3156
35a54f17 3157// Flatten a type conversion by using a temporary variable for the slice
3158// in slice to string conversions.
3159
3160Expression*
3161Type_conversion_expression::do_flatten(Gogo*, Named_object*,
3162 Statement_inserter* inserter)
3163{
5bf8be8b 3164 if (this->type()->is_error_type() || this->expr_->is_error_expression())
3165 {
3166 go_assert(saw_errors());
3167 return Expression::make_error(this->location());
3168 }
3169
2c809f8f 3170 if (((this->type()->is_string_type()
3171 && this->expr_->type()->is_slice_type())
8ba8cc87 3172 || this->expr_->type()->interface_type() != NULL)
35a54f17 3173 && !this->expr_->is_variable())
3174 {
3175 Temporary_statement* temp =
3176 Statement::make_temporary(NULL, this->expr_, this->location());
3177 inserter->insert(temp);
3178 this->expr_ = Expression::make_temporary_reference(temp, this->location());
3179 }
3180 return this;
3181}
3182
1ca01a59 3183// Return whether a type conversion is a constant.
3184
3185bool
3186Type_conversion_expression::do_is_constant() const
3187{
3188 if (!this->expr_->is_constant())
3189 return false;
3190
3191 // A conversion to a type that may not be used as a constant is not
3192 // a constant. For example, []byte(nil).
3193 Type* type = this->type_;
3194 if (type->integer_type() == NULL
3195 && type->float_type() == NULL
3196 && type->complex_type() == NULL
3197 && !type->is_boolean_type()
3198 && !type->is_string_type())
3199 return false;
3200
3201 return true;
3202}
3203
0e168074 3204// Return whether a type conversion is immutable.
3205
3206bool
3207Type_conversion_expression::do_is_immutable() const
3208{
3209 Type* type = this->type_;
3210 Type* expr_type = this->expr_->type();
3211
3212 if (type->interface_type() != NULL
3213 || expr_type->interface_type() != NULL)
3214 return false;
3215
3216 if (!this->expr_->is_immutable())
3217 return false;
3218
3219 if (Type::are_identical(type, expr_type, false, NULL))
3220 return true;
3221
3222 return type->is_basic_type() && expr_type->is_basic_type();
3223}
3224
0c77715b 3225// Return the constant numeric value if there is one.
e440a328 3226
3227bool
0c77715b 3228Type_conversion_expression::do_numeric_constant_value(
3229 Numeric_constant* nc) const
e440a328 3230{
0c77715b 3231 if (!this->type_->is_numeric_type())
e440a328 3232 return false;
0c77715b 3233 if (!this->expr_->numeric_constant_value(nc))
e440a328 3234 return false;
0c77715b 3235 return nc->set_type(this->type_, false, this->location());
e440a328 3236}
3237
3238// Return the constant string value if there is one.
3239
3240bool
3241Type_conversion_expression::do_string_constant_value(std::string* val) const
3242{
3243 if (this->type_->is_string_type()
3244 && this->expr_->type()->integer_type() != NULL)
3245 {
0c77715b 3246 Numeric_constant nc;
3247 if (this->expr_->numeric_constant_value(&nc))
e440a328 3248 {
0c77715b 3249 unsigned long ival;
3250 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
e440a328 3251 {
0c77715b 3252 val->clear();
3253 Lex::append_char(ival, true, val, this->location());
e440a328 3254 return true;
3255 }
3256 }
e440a328 3257 }
3258
3259 // FIXME: Could handle conversion from const []int here.
3260
3261 return false;
3262}
3263
da244e59 3264// Determine the resulting type of the conversion.
3265
3266void
3267Type_conversion_expression::do_determine_type(const Type_context*)
3268{
3269 Type_context subcontext(this->type_, false);
3270 this->expr_->determine_type(&subcontext);
3271}
3272
e440a328 3273// Check that types are convertible.
3274
3275void
3276Type_conversion_expression::do_check_types(Gogo*)
3277{
3278 Type* type = this->type_;
3279 Type* expr_type = this->expr_->type();
3280 std::string reason;
3281
5c13bd80 3282 if (type->is_error() || expr_type->is_error())
842f6425 3283 {
842f6425 3284 this->set_is_error();
3285 return;
3286 }
3287
e440a328 3288 if (this->may_convert_function_types_
3289 && type->function_type() != NULL
3290 && expr_type->function_type() != NULL)
3291 return;
3292
3293 if (Type::are_convertible(type, expr_type, &reason))
3294 return;
3295
3296 error_at(this->location(), "%s", reason.c_str());
3297 this->set_is_error();
3298}
3299
ea664253 3300// Get the backend representation for a type conversion.
e440a328 3301
ea664253 3302Bexpression*
3303Type_conversion_expression::do_get_backend(Translate_context* context)
e440a328 3304{
e440a328 3305 Type* type = this->type_;
3306 Type* expr_type = this->expr_->type();
2c809f8f 3307
3308 Gogo* gogo = context->gogo();
3309 Btype* btype = type->get_backend(gogo);
ea664253 3310 Bexpression* bexpr = this->expr_->get_backend(context);
2c809f8f 3311 Location loc = this->location();
3312
3313 if (Type::are_identical(type, expr_type, false, NULL))
ea664253 3314 return gogo->backend()->convert_expression(btype, bexpr, loc);
2c809f8f 3315 else if (type->interface_type() != NULL
3316 || expr_type->interface_type() != NULL)
e440a328 3317 {
2c809f8f 3318 Expression* conversion =
3319 Expression::convert_for_assignment(gogo, type, this->expr_,
3320 this->location());
ea664253 3321 return conversion->get_backend(context);
e440a328 3322 }
3323 else if (type->is_string_type()
3324 && expr_type->integer_type() != NULL)
3325 {
2c809f8f 3326 mpz_t intval;
3327 Numeric_constant nc;
3328 if (this->expr_->numeric_constant_value(&nc)
3329 && nc.to_int(&intval)
3330 && mpz_fits_ushort_p(intval))
e440a328 3331 {
e440a328 3332 std::string s;
2c809f8f 3333 Lex::append_char(mpz_get_ui(intval), true, &s, loc);
3334 mpz_clear(intval);
3335 Expression* se = Expression::make_string(s, loc);
ea664253 3336 return se->get_backend(context);
e440a328 3337 }
3338
f16ab008 3339 Expression* i2s_expr =
2c809f8f 3340 Runtime::make_call(Runtime::INT_TO_STRING, loc, 1, this->expr_);
ea664253 3341 return Expression::make_cast(type, i2s_expr, loc)->get_backend(context);
e440a328 3342 }
55072f2b 3343 else if (type->is_string_type() && expr_type->is_slice_type())
e440a328 3344 {
55072f2b 3345 Array_type* a = expr_type->array_type();
e440a328 3346 Type* e = a->element_type()->forwarded();
c484d925 3347 go_assert(e->integer_type() != NULL);
35a54f17 3348 go_assert(this->expr_->is_variable());
3349
3350 Runtime::Function code;
60963afd 3351 if (e->integer_type()->is_byte())
35a54f17 3352 code = Runtime::BYTE_ARRAY_TO_STRING;
e440a328 3353 else
35a54f17 3354 {
3355 go_assert(e->integer_type()->is_rune());
3356 code = Runtime::INT_ARRAY_TO_STRING;
3357 }
3358 Expression* valptr = a->get_value_pointer(gogo, this->expr_);
3359 Expression* len = a->get_length(gogo, this->expr_);
ea664253 3360 return Runtime::make_call(code, loc, 2, valptr,
3361 len)->get_backend(context);
e440a328 3362 }
411eb89e 3363 else if (type->is_slice_type() && expr_type->is_string_type())
e440a328 3364 {
3365 Type* e = type->array_type()->element_type()->forwarded();
c484d925 3366 go_assert(e->integer_type() != NULL);
6c252e42 3367
2c809f8f 3368 Runtime::Function code;
60963afd 3369 if (e->integer_type()->is_byte())
2c809f8f 3370 code = Runtime::STRING_TO_BYTE_ARRAY;
e440a328 3371 else
3372 {
60963afd 3373 go_assert(e->integer_type()->is_rune());
2c809f8f 3374 code = Runtime::STRING_TO_INT_ARRAY;
e440a328 3375 }
2c809f8f 3376 Expression* s2a = Runtime::make_call(code, loc, 1, this->expr_);
ea664253 3377 return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context);
2c809f8f 3378 }
3379 else if (type->is_numeric_type())
3380 {
3381 go_assert(Type::are_convertible(type, expr_type, NULL));
ea664253 3382 return gogo->backend()->convert_expression(btype, bexpr, loc);
e440a328 3383 }
3384 else if ((type->is_unsafe_pointer_type()
2c809f8f 3385 && (expr_type->points_to() != NULL
3386 || expr_type->integer_type()))
3387 || (expr_type->is_unsafe_pointer_type()
3388 && type->points_to() != NULL)
3389 || (this->may_convert_function_types_
3390 && type->function_type() != NULL
3391 && expr_type->function_type() != NULL))
ea664253 3392 return gogo->backend()->convert_expression(btype, bexpr, loc);
e440a328 3393 else
2c809f8f 3394 {
3395 Expression* conversion =
3396 Expression::convert_for_assignment(gogo, type, this->expr_, loc);
ea664253 3397 return conversion->get_backend(context);
2c809f8f 3398 }
e440a328 3399}
3400
3401// Output a type conversion in a constant expression.
3402
3403void
3404Type_conversion_expression::do_export(Export* exp) const
3405{
3406 exp->write_c_string("convert(");
3407 exp->write_type(this->type_);
3408 exp->write_c_string(", ");
3409 this->expr_->export_expression(exp);
3410 exp->write_c_string(")");
3411}
3412
3413// Import a type conversion or a struct construction.
3414
3415Expression*
3416Type_conversion_expression::do_import(Import* imp)
3417{
3418 imp->require_c_string("convert(");
3419 Type* type = imp->read_type();
3420 imp->require_c_string(", ");
3421 Expression* val = Expression::import_expression(imp);
3422 imp->require_c_string(")");
3423 return Expression::make_cast(type, val, imp->location());
3424}
3425
d751bb78 3426// Dump ast representation for a type conversion expression.
3427
3428void
3429Type_conversion_expression::do_dump_expression(
3430 Ast_dump_context* ast_dump_context) const
3431{
3432 ast_dump_context->dump_type(this->type_);
3433 ast_dump_context->ostream() << "(";
3434 ast_dump_context->dump_expression(this->expr_);
3435 ast_dump_context->ostream() << ") ";
3436}
3437
e440a328 3438// Make a type cast expression.
3439
3440Expression*
b13c66cd 3441Expression::make_cast(Type* type, Expression* val, Location location)
e440a328 3442{
3443 if (type->is_error_type() || val->is_error_expression())
3444 return Expression::make_error(location);
3445 return new Type_conversion_expression(type, val, location);
3446}
3447
98f62f7a 3448// Class Unsafe_type_conversion_expression.
9581e91d 3449
3450// Traversal.
3451
3452int
3453Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3454{
3455 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3456 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3457 return TRAVERSE_EXIT;
3458 return TRAVERSE_CONTINUE;
3459}
3460
aa5ae575 3461// Return whether an unsafe type conversion is immutable.
3462
3463bool
3464Unsafe_type_conversion_expression::do_is_immutable() const
3465{
3466 Type* type = this->type_;
3467 Type* expr_type = this->expr_->type();
3468
3469 if (type->interface_type() != NULL
3470 || expr_type->interface_type() != NULL)
3471 return false;
3472
3473 if (!this->expr_->is_immutable())
3474 return false;
3475
3476 if (Type::are_convertible(type, expr_type, NULL))
3477 return true;
3478
3479 return type->is_basic_type() && expr_type->is_basic_type();
3480}
3481
9581e91d 3482// Convert to backend representation.
3483
ea664253 3484Bexpression*
3485Unsafe_type_conversion_expression::do_get_backend(Translate_context* context)
9581e91d 3486{
3487 // We are only called for a limited number of cases.
3488
3489 Type* t = this->type_;
3490 Type* et = this->expr_->type();
5c4802f1 3491
3492 if (t->is_error_type()
3493 || this->expr_->is_error_expression()
3494 || et->is_error_type())
3495 {
3496 go_assert(saw_errors());
3497 return context->backend()->error_expression();
3498 }
3499
2c809f8f 3500 if (t->array_type() != NULL)
3501 go_assert(et->array_type() != NULL
3502 && t->is_slice_type() == et->is_slice_type());
3503 else if (t->struct_type() != NULL)
9581e91d 3504 {
2c809f8f 3505 if (t->named_type() != NULL
3506 && et->named_type() != NULL
3507 && !Type::are_convertible(t, et, NULL))
3508 {
3509 go_assert(saw_errors());
ea664253 3510 return context->backend()->error_expression();
2c809f8f 3511 }
3512
3513 go_assert(et->struct_type() != NULL
3514 && Type::are_convertible(t, et, NULL));
3515 }
3516 else if (t->map_type() != NULL)
c484d925 3517 go_assert(et->map_type() != NULL);
9581e91d 3518 else if (t->channel_type() != NULL)
c484d925 3519 go_assert(et->channel_type() != NULL);
09ea332d 3520 else if (t->points_to() != NULL)
2c809f8f 3521 go_assert(et->points_to() != NULL
3522 || et->channel_type() != NULL
3523 || et->map_type() != NULL
3524 || et->function_type() != NULL
3525 || et->is_nil_type());
9581e91d 3526 else if (et->is_unsafe_pointer_type())
c484d925 3527 go_assert(t->points_to() != NULL);
2c809f8f 3528 else if (t->interface_type() != NULL)
9581e91d 3529 {
2c809f8f 3530 bool empty_iface = t->interface_type()->is_empty();
c484d925 3531 go_assert(et->interface_type() != NULL
2c809f8f 3532 && et->interface_type()->is_empty() == empty_iface);
9581e91d 3533 }
588e3cf9 3534 else if (t->integer_type() != NULL)
2c809f8f 3535 go_assert(et->is_boolean_type()
3536 || et->integer_type() != NULL
3537 || et->function_type() != NULL
3538 || et->points_to() != NULL
3539 || et->map_type() != NULL
8ba8cc87 3540 || et->channel_type() != NULL
3541 || et->is_nil_type());
9581e91d 3542 else
c3e6f413 3543 go_unreachable();
9581e91d 3544
2c809f8f 3545 Gogo* gogo = context->gogo();
3546 Btype* btype = t->get_backend(gogo);
ea664253 3547 Bexpression* bexpr = this->expr_->get_backend(context);
2c809f8f 3548 Location loc = this->location();
ea664253 3549 return gogo->backend()->convert_expression(btype, bexpr, loc);
9581e91d 3550}
3551
d751bb78 3552// Dump ast representation for an unsafe type conversion expression.
3553
3554void
3555Unsafe_type_conversion_expression::do_dump_expression(
3556 Ast_dump_context* ast_dump_context) const
3557{
3558 ast_dump_context->dump_type(this->type_);
3559 ast_dump_context->ostream() << "(";
3560 ast_dump_context->dump_expression(this->expr_);
3561 ast_dump_context->ostream() << ") ";
3562}
3563
9581e91d 3564// Make an unsafe type conversion expression.
3565
3566Expression*
3567Expression::make_unsafe_cast(Type* type, Expression* expr,
b13c66cd 3568 Location location)
9581e91d 3569{
3570 return new Unsafe_type_conversion_expression(type, expr, location);
3571}
3572
76f85fd6 3573// Class Unary_expression.
e440a328 3574
3575// If we are taking the address of a composite literal, and the
2c809f8f 3576// contents are not constant, then we want to make a heap expression
e440a328 3577// instead.
3578
3579Expression*
ceeb4318 3580Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 3581{
b13c66cd 3582 Location loc = this->location();
e440a328 3583 Operator op = this->op_;
3584 Expression* expr = this->expr_;
3585
3586 if (op == OPERATOR_MULT && expr->is_type_expression())
3587 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3588
3589 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3590 // moving x to the heap. FIXME: Is it worth doing a real escape
3591 // analysis here? This case is found in math/unsafe.go and is
3592 // therefore worth special casing.
3593 if (op == OPERATOR_MULT)
3594 {
3595 Expression* e = expr;
3596 while (e->classification() == EXPRESSION_CONVERSION)
3597 {
3598 Type_conversion_expression* te
3599 = static_cast<Type_conversion_expression*>(e);
3600 e = te->expr();
3601 }
3602
3603 if (e->classification() == EXPRESSION_UNARY)
3604 {
3605 Unary_expression* ue = static_cast<Unary_expression*>(e);
3606 if (ue->op_ == OPERATOR_AND)
3607 {
3608 if (e == expr)
3609 {
3610 // *&x == x.
f4dea966 3611 if (!ue->expr_->is_addressable() && !ue->create_temp_)
3612 {
3613 error_at(ue->location(),
3614 "invalid operand for unary %<&%>");
3615 this->set_is_error();
3616 }
e440a328 3617 return ue->expr_;
3618 }
3619 ue->set_does_not_escape();
3620 }
3621 }
3622 }
3623
55661ce9 3624 // Catching an invalid indirection of unsafe.Pointer here avoid
3625 // having to deal with TYPE_VOID in other places.
3626 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3627 {
3628 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3629 return Expression::make_error(this->location());
3630 }
3631
d9f3743a 3632 // Check for an invalid pointer dereference. We need to do this
3633 // here because Unary_expression::do_type will return an error type
3634 // in this case. That can cause code to appear erroneous, and
3635 // therefore disappear at lowering time, without any error message.
3636 if (op == OPERATOR_MULT && expr->type()->points_to() == NULL)
3637 {
3638 this->report_error(_("expected pointer"));
3639 return Expression::make_error(this->location());
3640 }
3641
59a401fe 3642 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
e440a328 3643 {
0c77715b 3644 Numeric_constant nc;
3645 if (expr->numeric_constant_value(&nc))
e440a328 3646 {
0c77715b 3647 Numeric_constant result;
3648 if (Unary_expression::eval_constant(op, &nc, loc, &result))
3649 return result.expression(loc);
e440a328 3650 }
3651 }
3652
3653 return this;
3654}
3655
f9ca30f9 3656// Flatten expression if a nil check must be performed and create temporary
3657// variables if necessary.
3658
3659Expression*
3660Unary_expression::do_flatten(Gogo* gogo, Named_object*,
3661 Statement_inserter* inserter)
3662{
5bf8be8b 3663 if (this->is_error_expression()
3664 || this->expr_->is_error_expression()
3665 || this->expr_->type()->is_error_type())
3666 {
3667 go_assert(saw_errors());
3668 return Expression::make_error(this->location());
3669 }
f4dea966 3670
f9ca30f9 3671 Location location = this->location();
3672 if (this->op_ == OPERATOR_MULT
3673 && !this->expr_->is_variable())
3674 {
3675 go_assert(this->expr_->type()->points_to() != NULL);
3676 Type* ptype = this->expr_->type()->points_to();
3677 if (!ptype->is_void_type())
3678 {
2a305b85 3679 int64_t s;
3680 bool ok = ptype->backend_type_size(gogo, &s);
3681 if (!ok)
3682 {
3683 go_assert(saw_errors());
3684 return Expression::make_error(this->location());
3685 }
f9ca30f9 3686 if (s >= 4096 || this->issue_nil_check_)
3687 {
3688 Temporary_statement* temp =
3689 Statement::make_temporary(NULL, this->expr_, location);
3690 inserter->insert(temp);
3691 this->expr_ =
3692 Expression::make_temporary_reference(temp, location);
3693 }
3694 }
3695 }
3696
da244e59 3697 if (this->op_ == OPERATOR_AND)
3698 {
8ff995b5 3699 // If this->escapes_ is false at this point, then it was set to
3700 // false by an explicit call to set_does_not_escape, and the
3701 // value does not escape. If this->escapes_ is true, we may be
3702 // able to set it to false if taking the address of a variable
3703 // that does not escape.
3704 if (this->escapes_ && this->expr_->var_expression() != NULL)
da244e59 3705 {
3706 Named_object* var = this->expr_->var_expression()->named_object();
3707 if (var->is_variable())
3708 this->escapes_ = var->var_value()->escapes();
3709 if (var->is_result_variable())
3710 this->escapes_ = var->result_var_value()->escapes();
3711 }
3712 this->expr_->address_taken(this->escapes_);
3713 }
3714
f9ca30f9 3715 if (this->create_temp_ && !this->expr_->is_variable())
3716 {
3717 Temporary_statement* temp =
3718 Statement::make_temporary(NULL, this->expr_, location);
3719 inserter->insert(temp);
3720 this->expr_ = Expression::make_temporary_reference(temp, location);
3721 }
3722
3723 return this;
3724}
3725
e440a328 3726// Return whether a unary expression is a constant.
3727
3728bool
3729Unary_expression::do_is_constant() const
3730{
3731 if (this->op_ == OPERATOR_MULT)
3732 {
3733 // Indirecting through a pointer is only constant if the object
3734 // to which the expression points is constant, but we currently
3735 // have no way to determine that.
3736 return false;
3737 }
3738 else if (this->op_ == OPERATOR_AND)
3739 {
3740 // Taking the address of a variable is constant if it is a
f9ca30f9 3741 // global variable, not constant otherwise. In other cases taking the
3742 // address is probably not a constant.
e440a328 3743 Var_expression* ve = this->expr_->var_expression();
3744 if (ve != NULL)
3745 {
3746 Named_object* no = ve->named_object();
3747 return no->is_variable() && no->var_value()->is_global();
3748 }
3749 return false;
3750 }
3751 else
3752 return this->expr_->is_constant();
3753}
3754
0c77715b 3755// Apply unary opcode OP to UNC, setting NC. Return true if this
3756// could be done, false if not. Issue errors for overflow.
e440a328 3757
3758bool
0c77715b 3759Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
3760 Location location, Numeric_constant* nc)
e440a328 3761{
3762 switch (op)
3763 {
3764 case OPERATOR_PLUS:
0c77715b 3765 *nc = *unc;
e440a328 3766 return true;
0c77715b 3767
e440a328 3768 case OPERATOR_MINUS:
0c77715b 3769 if (unc->is_int() || unc->is_rune())
3770 break;
3771 else if (unc->is_float())
3772 {
3773 mpfr_t uval;
3774 unc->get_float(&uval);
3775 mpfr_t val;
3776 mpfr_init(val);
3777 mpfr_neg(val, uval, GMP_RNDN);
3778 nc->set_float(unc->type(), val);
3779 mpfr_clear(uval);
3780 mpfr_clear(val);
3781 return true;
3782 }
3783 else if (unc->is_complex())
3784 {
fcbea5e4 3785 mpc_t uval;
3786 unc->get_complex(&uval);
3787 mpc_t val;
3788 mpc_init2(val, mpc_precision);
3789 mpc_neg(val, uval, MPC_RNDNN);
3790 nc->set_complex(unc->type(), val);
3791 mpc_clear(uval);
3792 mpc_clear(val);
0c77715b 3793 return true;
3794 }
e440a328 3795 else
0c77715b 3796 go_unreachable();
e440a328 3797
0c77715b 3798 case OPERATOR_XOR:
3799 break;
68448d53 3800
59a401fe 3801 case OPERATOR_NOT:
e440a328 3802 case OPERATOR_AND:
3803 case OPERATOR_MULT:
3804 return false;
0c77715b 3805
e440a328 3806 default:
c3e6f413 3807 go_unreachable();
e440a328 3808 }
e440a328 3809
0c77715b 3810 if (!unc->is_int() && !unc->is_rune())
3811 return false;
3812
3813 mpz_t uval;
8387e1df 3814 if (unc->is_rune())
3815 unc->get_rune(&uval);
3816 else
3817 unc->get_int(&uval);
0c77715b 3818 mpz_t val;
3819 mpz_init(val);
e440a328 3820
e440a328 3821 switch (op)
3822 {
e440a328 3823 case OPERATOR_MINUS:
0c77715b 3824 mpz_neg(val, uval);
3825 break;
3826
e440a328 3827 case OPERATOR_NOT:
0c77715b 3828 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3829 break;
3830
e440a328 3831 case OPERATOR_XOR:
0c77715b 3832 {
3833 Type* utype = unc->type();
3834 if (utype->integer_type() == NULL
3835 || utype->integer_type()->is_abstract())
3836 mpz_com(val, uval);
3837 else
3838 {
3839 // The number of HOST_WIDE_INTs that it takes to represent
3840 // UVAL.
3841 size_t count = ((mpz_sizeinbase(uval, 2)
3842 + HOST_BITS_PER_WIDE_INT
3843 - 1)
3844 / HOST_BITS_PER_WIDE_INT);
e440a328 3845
0c77715b 3846 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3847 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3848
3849 size_t obits = utype->integer_type()->bits();
3850
3851 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
3852 {
3853 mpz_t adj;
3854 mpz_init_set_ui(adj, 1);
3855 mpz_mul_2exp(adj, adj, obits);
3856 mpz_add(uval, uval, adj);
3857 mpz_clear(adj);
3858 }
3859
3860 size_t ecount;
3861 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3862 go_assert(ecount <= count);
3863
3864 // Trim down to the number of words required by the type.
3865 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3866 / HOST_BITS_PER_WIDE_INT);
3867 go_assert(ocount <= count);
3868
3869 for (size_t i = 0; i < ocount; ++i)
3870 phwi[i] = ~phwi[i];
3871
3872 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3873 if (clearbits != 0)
3874 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3875 >> clearbits);
3876
3877 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3878
3879 if (!utype->integer_type()->is_unsigned()
3880 && mpz_tstbit(val, obits - 1))
3881 {
3882 mpz_t adj;
3883 mpz_init_set_ui(adj, 1);
3884 mpz_mul_2exp(adj, adj, obits);
3885 mpz_sub(val, val, adj);
3886 mpz_clear(adj);
3887 }
3888
3889 delete[] phwi;
3890 }
3891 }
3892 break;
e440a328 3893
e440a328 3894 default:
c3e6f413 3895 go_unreachable();
e440a328 3896 }
e440a328 3897
0c77715b 3898 if (unc->is_rune())
3899 nc->set_rune(NULL, val);
e440a328 3900 else
0c77715b 3901 nc->set_int(NULL, val);
e440a328 3902
0c77715b 3903 mpz_clear(uval);
3904 mpz_clear(val);
e440a328 3905
0c77715b 3906 return nc->set_type(unc->type(), true, location);
e440a328 3907}
3908
0c77715b 3909// Return the integral constant value of a unary expression, if it has one.
e440a328 3910
3911bool
0c77715b 3912Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 3913{
0c77715b 3914 Numeric_constant unc;
3915 if (!this->expr_->numeric_constant_value(&unc))
3916 return false;
3917 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
3918 nc);
e440a328 3919}
3920
3921// Return the type of a unary expression.
3922
3923Type*
3924Unary_expression::do_type()
3925{
3926 switch (this->op_)
3927 {
3928 case OPERATOR_PLUS:
3929 case OPERATOR_MINUS:
3930 case OPERATOR_NOT:
3931 case OPERATOR_XOR:
3932 return this->expr_->type();
3933
3934 case OPERATOR_AND:
3935 return Type::make_pointer_type(this->expr_->type());
3936
3937 case OPERATOR_MULT:
3938 {
3939 Type* subtype = this->expr_->type();
3940 Type* points_to = subtype->points_to();
3941 if (points_to == NULL)
3942 return Type::make_error_type();
3943 return points_to;
3944 }
3945
3946 default:
c3e6f413 3947 go_unreachable();
e440a328 3948 }
3949}
3950
3951// Determine abstract types for a unary expression.
3952
3953void
3954Unary_expression::do_determine_type(const Type_context* context)
3955{
3956 switch (this->op_)
3957 {
3958 case OPERATOR_PLUS:
3959 case OPERATOR_MINUS:
3960 case OPERATOR_NOT:
3961 case OPERATOR_XOR:
3962 this->expr_->determine_type(context);
3963 break;
3964
3965 case OPERATOR_AND:
3966 // Taking the address of something.
3967 {
3968 Type* subtype = (context->type == NULL
3969 ? NULL
3970 : context->type->points_to());
3971 Type_context subcontext(subtype, false);
3972 this->expr_->determine_type(&subcontext);
3973 }
3974 break;
3975
3976 case OPERATOR_MULT:
3977 // Indirecting through a pointer.
3978 {
3979 Type* subtype = (context->type == NULL
3980 ? NULL
3981 : Type::make_pointer_type(context->type));
3982 Type_context subcontext(subtype, false);
3983 this->expr_->determine_type(&subcontext);
3984 }
3985 break;
3986
3987 default:
c3e6f413 3988 go_unreachable();
e440a328 3989 }
3990}
3991
3992// Check types for a unary expression.
3993
3994void
3995Unary_expression::do_check_types(Gogo*)
3996{
9fe897ef 3997 Type* type = this->expr_->type();
5c13bd80 3998 if (type->is_error())
9fe897ef 3999 {
4000 this->set_is_error();
4001 return;
4002 }
4003
e440a328 4004 switch (this->op_)
4005 {
4006 case OPERATOR_PLUS:
4007 case OPERATOR_MINUS:
9fe897ef 4008 if (type->integer_type() == NULL
4009 && type->float_type() == NULL
4010 && type->complex_type() == NULL)
4011 this->report_error(_("expected numeric type"));
e440a328 4012 break;
4013
4014 case OPERATOR_NOT:
59a401fe 4015 if (!type->is_boolean_type())
4016 this->report_error(_("expected boolean type"));
4017 break;
4018
e440a328 4019 case OPERATOR_XOR:
b3b1474e 4020 if (type->integer_type() == NULL)
4021 this->report_error(_("expected integer"));
e440a328 4022 break;
4023
4024 case OPERATOR_AND:
4025 if (!this->expr_->is_addressable())
09ea332d 4026 {
4027 if (!this->create_temp_)
f4dea966 4028 {
4029 error_at(this->location(), "invalid operand for unary %<&%>");
4030 this->set_is_error();
4031 }
09ea332d 4032 }
e440a328 4033 else
da244e59 4034 this->expr_->issue_nil_check();
e440a328 4035 break;
4036
4037 case OPERATOR_MULT:
4038 // Indirecting through a pointer.
9fe897ef 4039 if (type->points_to() == NULL)
4040 this->report_error(_("expected pointer"));
7661d702 4041 if (type->points_to()->is_error())
4042 this->set_is_error();
e440a328 4043 break;
4044
4045 default:
c3e6f413 4046 go_unreachable();
e440a328 4047 }
4048}
4049
ea664253 4050// Get the backend representation for a unary expression.
e440a328 4051
ea664253 4052Bexpression*
4053Unary_expression::do_get_backend(Translate_context* context)
e440a328 4054{
1b1f2abf 4055 Gogo* gogo = context->gogo();
e9d3367e 4056 Location loc = this->location();
4057
4058 // Taking the address of a set-and-use-temporary expression requires
4059 // setting the temporary and then taking the address.
4060 if (this->op_ == OPERATOR_AND)
4061 {
4062 Set_and_use_temporary_expression* sut =
4063 this->expr_->set_and_use_temporary_expression();
4064 if (sut != NULL)
4065 {
4066 Temporary_statement* temp = sut->temporary();
4067 Bvariable* bvar = temp->get_backend_variable(context);
f9ca30f9 4068 Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc);
ea664253 4069 Bexpression* bval = sut->expression()->get_backend(context);
f9ca30f9 4070
4071 Bstatement* bassign =
4072 gogo->backend()->assignment_statement(bvar_expr, bval, loc);
4073 Bexpression* bvar_addr =
4074 gogo->backend()->address_expression(bvar_expr, loc);
ea664253 4075 return gogo->backend()->compound_expression(bassign, bvar_addr, loc);
e9d3367e 4076 }
4077 }
4078
f9ca30f9 4079 Bexpression* ret;
ea664253 4080 Bexpression* bexpr = this->expr_->get_backend(context);
f9ca30f9 4081 Btype* btype = this->expr_->type()->get_backend(gogo);
e440a328 4082 switch (this->op_)
4083 {
4084 case OPERATOR_PLUS:
f9ca30f9 4085 ret = bexpr;
4086 break;
e440a328 4087
4088 case OPERATOR_MINUS:
f9ca30f9 4089 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
4090 ret = gogo->backend()->convert_expression(btype, ret, loc);
4091 break;
e440a328 4092
4093 case OPERATOR_NOT:
e440a328 4094 case OPERATOR_XOR:
f9ca30f9 4095 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
4096 break;
e440a328 4097
4098 case OPERATOR_AND:
09ea332d 4099 if (!this->create_temp_)
4100 {
4101 // We should not see a non-constant constructor here; cases
4102 // where we would see one should have been moved onto the
4103 // heap at parse time. Taking the address of a nonconstant
4104 // constructor will not do what the programmer expects.
f9ca30f9 4105
4106 go_assert(!this->expr_->is_composite_literal()
4107 || this->expr_->is_immutable());
24060bf9 4108 if (this->expr_->classification() == EXPRESSION_UNARY)
4109 {
4110 Unary_expression* ue =
4111 static_cast<Unary_expression*>(this->expr_);
4112 go_assert(ue->op() != OPERATOR_AND);
4113 }
09ea332d 4114 }
e440a328 4115
f23d7786 4116 static unsigned int counter;
4117 char buf[100];
4118 if (this->is_gc_root_ || this->is_slice_init_)
76f85fd6 4119 {
f23d7786 4120 bool copy_to_heap = false;
4121 if (this->is_gc_root_)
4122 {
4123 // Build a decl for a GC root variable. GC roots are mutable, so
4124 // they cannot be represented as an immutable_struct in the
4125 // backend.
4126 static unsigned int root_counter;
4127 snprintf(buf, sizeof buf, "gc%u", root_counter);
4128 ++root_counter;
4129 }
4130 else
4131 {
4132 // Build a decl for a slice value initializer. An immutable slice
4133 // value initializer may have to be copied to the heap if it
4134 // contains pointers in a non-constant context.
4135 snprintf(buf, sizeof buf, "C%u", counter);
4136 ++counter;
4137
4138 Array_type* at = this->expr_->type()->array_type();
4139 go_assert(at != NULL);
4140
4141 // If we are not copying the value to the heap, we will only
4142 // initialize the value once, so we can use this directly
4143 // rather than copying it. In that case we can't make it
4144 // read-only, because the program is permitted to change it.
4145 copy_to_heap = (at->element_type()->has_pointer()
4146 && !context->is_const());
4147 }
4148 Bvariable* implicit =
aa5ae575 4149 gogo->backend()->implicit_variable(buf, btype, true, copy_to_heap,
5892f89f 4150 false, 0);
aa5ae575 4151 gogo->backend()->implicit_variable_set_init(implicit, buf, btype,
4152 true, copy_to_heap, false,
4153 bexpr);
f23d7786 4154 bexpr = gogo->backend()->var_expression(implicit, loc);
76f85fd6 4155 }
4156 else if ((this->expr_->is_composite_literal()
f9ca30f9 4157 || this->expr_->string_expression() != NULL)
4158 && this->expr_->is_immutable())
4159 {
76f85fd6 4160 // Build a decl for a constant constructor.
f9ca30f9 4161 snprintf(buf, sizeof buf, "C%u", counter);
4162 ++counter;
4163
4164 Bvariable* decl =
4165 gogo->backend()->immutable_struct(buf, true, false, btype, loc);
4166 gogo->backend()->immutable_struct_set_init(decl, buf, true, false,
4167 btype, loc, bexpr);
4168 bexpr = gogo->backend()->var_expression(decl, loc);
4169 }
09ea332d 4170
f9ca30f9 4171 go_assert(!this->create_temp_ || this->expr_->is_variable());
4172 ret = gogo->backend()->address_expression(bexpr, loc);
4173 break;
e440a328 4174
4175 case OPERATOR_MULT:
4176 {
f9ca30f9 4177 go_assert(this->expr_->type()->points_to() != NULL);
e440a328 4178
4179 // If we are dereferencing the pointer to a large struct, we
4180 // need to check for nil. We don't bother to check for small
4181 // structs because we expect the system to crash on a nil
56080003 4182 // pointer dereference. However, if we know the address of this
4183 // expression is being taken, we must always check for nil.
f9ca30f9 4184
4185 Type* ptype = this->expr_->type()->points_to();
4186 Btype* pbtype = ptype->get_backend(gogo);
4187 if (!ptype->is_void_type())
e440a328 4188 {
2a305b85 4189 int64_t s;
4190 bool ok = ptype->backend_type_size(gogo, &s);
4191 if (!ok)
4192 {
4193 go_assert(saw_errors());
4194 return gogo->backend()->error_expression();
4195 }
f9ca30f9 4196 if (s >= 4096 || this->issue_nil_check_)
19b4f09b 4197 {
f9ca30f9 4198 go_assert(this->expr_->is_variable());
ea664253 4199 Bexpression* nil =
4200 Expression::make_nil(loc)->get_backend(context);
f9ca30f9 4201 Bexpression* compare =
4202 gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr,
4203 nil, loc);
f9ca30f9 4204 Bexpression* crash =
ea664253 4205 gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4206 loc)->get_backend(context);
f9ca30f9 4207 bexpr = gogo->backend()->conditional_expression(btype, compare,
4208 crash, bexpr,
4209 loc);
4210
19b4f09b 4211 }
e440a328 4212 }
9b27b43c 4213 ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc);
e440a328 4214 }
f9ca30f9 4215 break;
e440a328 4216
4217 default:
c3e6f413 4218 go_unreachable();
e440a328 4219 }
f9ca30f9 4220
ea664253 4221 return ret;
e440a328 4222}
4223
4224// Export a unary expression.
4225
4226void
4227Unary_expression::do_export(Export* exp) const
4228{
4229 switch (this->op_)
4230 {
4231 case OPERATOR_PLUS:
4232 exp->write_c_string("+ ");
4233 break;
4234 case OPERATOR_MINUS:
4235 exp->write_c_string("- ");
4236 break;
4237 case OPERATOR_NOT:
4238 exp->write_c_string("! ");
4239 break;
4240 case OPERATOR_XOR:
4241 exp->write_c_string("^ ");
4242 break;
4243 case OPERATOR_AND:
4244 case OPERATOR_MULT:
4245 default:
c3e6f413 4246 go_unreachable();
e440a328 4247 }
4248 this->expr_->export_expression(exp);
4249}
4250
4251// Import a unary expression.
4252
4253Expression*
4254Unary_expression::do_import(Import* imp)
4255{
4256 Operator op;
4257 switch (imp->get_char())
4258 {
4259 case '+':
4260 op = OPERATOR_PLUS;
4261 break;
4262 case '-':
4263 op = OPERATOR_MINUS;
4264 break;
4265 case '!':
4266 op = OPERATOR_NOT;
4267 break;
4268 case '^':
4269 op = OPERATOR_XOR;
4270 break;
4271 default:
c3e6f413 4272 go_unreachable();
e440a328 4273 }
4274 imp->require_c_string(" ");
4275 Expression* expr = Expression::import_expression(imp);
4276 return Expression::make_unary(op, expr, imp->location());
4277}
4278
d751bb78 4279// Dump ast representation of an unary expression.
4280
4281void
4282Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4283{
4284 ast_dump_context->dump_operator(this->op_);
4285 ast_dump_context->ostream() << "(";
4286 ast_dump_context->dump_expression(this->expr_);
4287 ast_dump_context->ostream() << ") ";
4288}
4289
e440a328 4290// Make a unary expression.
4291
4292Expression*
b13c66cd 4293Expression::make_unary(Operator op, Expression* expr, Location location)
e440a328 4294{
4295 return new Unary_expression(op, expr, location);
4296}
4297
4298// If this is an indirection through a pointer, return the expression
4299// being pointed through. Otherwise return this.
4300
4301Expression*
4302Expression::deref()
4303{
4304 if (this->classification_ == EXPRESSION_UNARY)
4305 {
4306 Unary_expression* ue = static_cast<Unary_expression*>(this);
4307 if (ue->op() == OPERATOR_MULT)
4308 return ue->operand();
4309 }
4310 return this;
4311}
4312
4313// Class Binary_expression.
4314
4315// Traversal.
4316
4317int
4318Binary_expression::do_traverse(Traverse* traverse)
4319{
4320 int t = Expression::traverse(&this->left_, traverse);
4321 if (t == TRAVERSE_EXIT)
4322 return TRAVERSE_EXIT;
4323 return Expression::traverse(&this->right_, traverse);
4324}
4325
0c77715b 4326// Return the type to use for a binary operation on operands of
4327// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4328// such may be NULL or abstract.
4329
4330bool
4331Binary_expression::operation_type(Operator op, Type* left_type,
4332 Type* right_type, Type** result_type)
4333{
4334 if (left_type != right_type
4335 && !left_type->is_abstract()
4336 && !right_type->is_abstract()
4337 && left_type->base() != right_type->base()
4338 && op != OPERATOR_LSHIFT
4339 && op != OPERATOR_RSHIFT)
4340 {
4341 // May be a type error--let it be diagnosed elsewhere.
4342 return false;
4343 }
4344
4345 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4346 {
4347 if (left_type->integer_type() != NULL)
4348 *result_type = left_type;
4349 else
4350 *result_type = Type::make_abstract_integer_type();
4351 }
4352 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
4353 *result_type = left_type;
4354 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
4355 *result_type = right_type;
4356 else if (!left_type->is_abstract())
4357 *result_type = left_type;
4358 else if (!right_type->is_abstract())
4359 *result_type = right_type;
4360 else if (left_type->complex_type() != NULL)
4361 *result_type = left_type;
4362 else if (right_type->complex_type() != NULL)
4363 *result_type = right_type;
4364 else if (left_type->float_type() != NULL)
4365 *result_type = left_type;
4366 else if (right_type->float_type() != NULL)
4367 *result_type = right_type;
4368 else if (left_type->integer_type() != NULL
4369 && left_type->integer_type()->is_rune())
4370 *result_type = left_type;
4371 else if (right_type->integer_type() != NULL
4372 && right_type->integer_type()->is_rune())
4373 *result_type = right_type;
4374 else
4375 *result_type = left_type;
4376
4377 return true;
4378}
4379
4380// Convert an integer comparison code and an operator to a boolean
4381// value.
e440a328 4382
4383bool
0c77715b 4384Binary_expression::cmp_to_bool(Operator op, int cmp)
e440a328 4385{
e440a328 4386 switch (op)
4387 {
4388 case OPERATOR_EQEQ:
0c77715b 4389 return cmp == 0;
4390 break;
e440a328 4391 case OPERATOR_NOTEQ:
0c77715b 4392 return cmp != 0;
4393 break;
e440a328 4394 case OPERATOR_LT:
0c77715b 4395 return cmp < 0;
4396 break;
e440a328 4397 case OPERATOR_LE:
0c77715b 4398 return cmp <= 0;
e440a328 4399 case OPERATOR_GT:
0c77715b 4400 return cmp > 0;
e440a328 4401 case OPERATOR_GE:
0c77715b 4402 return cmp >= 0;
e440a328 4403 default:
c3e6f413 4404 go_unreachable();
e440a328 4405 }
4406}
4407
0c77715b 4408// Compare constants according to OP.
e440a328 4409
4410bool
0c77715b 4411Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
4412 Numeric_constant* right_nc,
4413 Location location, bool* result)
e440a328 4414{
0c77715b 4415 Type* left_type = left_nc->type();
4416 Type* right_type = right_nc->type();
4417
4418 Type* type;
4419 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4420 return false;
4421
4422 // When comparing an untyped operand to a typed operand, we are
4423 // effectively coercing the untyped operand to the other operand's
4424 // type, so make sure that is valid.
4425 if (!left_nc->set_type(type, true, location)
4426 || !right_nc->set_type(type, true, location))
4427 return false;
4428
4429 bool ret;
4430 int cmp;
4431 if (type->complex_type() != NULL)
4432 {
4433 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
4434 return false;
4435 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
4436 }
4437 else if (type->float_type() != NULL)
4438 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
e440a328 4439 else
0c77715b 4440 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
4441
4442 if (ret)
4443 *result = Binary_expression::cmp_to_bool(op, cmp);
4444
4445 return ret;
4446}
4447
4448// Compare integer constants.
4449
4450bool
4451Binary_expression::compare_integer(const Numeric_constant* left_nc,
4452 const Numeric_constant* right_nc,
4453 int* cmp)
4454{
4455 mpz_t left_val;
4456 if (!left_nc->to_int(&left_val))
4457 return false;
4458 mpz_t right_val;
4459 if (!right_nc->to_int(&right_val))
e440a328 4460 {
0c77715b 4461 mpz_clear(left_val);
4462 return false;
e440a328 4463 }
0c77715b 4464
4465 *cmp = mpz_cmp(left_val, right_val);
4466
4467 mpz_clear(left_val);
4468 mpz_clear(right_val);
4469
4470 return true;
4471}
4472
4473// Compare floating point constants.
4474
4475bool
4476Binary_expression::compare_float(const Numeric_constant* left_nc,
4477 const Numeric_constant* right_nc,
4478 int* cmp)
4479{
4480 mpfr_t left_val;
4481 if (!left_nc->to_float(&left_val))
4482 return false;
4483 mpfr_t right_val;
4484 if (!right_nc->to_float(&right_val))
e440a328 4485 {
0c77715b 4486 mpfr_clear(left_val);
4487 return false;
4488 }
4489
4490 // We already coerced both operands to the same type. If that type
4491 // is not an abstract type, we need to round the values accordingly.
4492 Type* type = left_nc->type();
4493 if (!type->is_abstract() && type->float_type() != NULL)
4494 {
4495 int bits = type->float_type()->bits();
4496 mpfr_prec_round(left_val, bits, GMP_RNDN);
4497 mpfr_prec_round(right_val, bits, GMP_RNDN);
e440a328 4498 }
0c77715b 4499
4500 *cmp = mpfr_cmp(left_val, right_val);
4501
4502 mpfr_clear(left_val);
4503 mpfr_clear(right_val);
4504
4505 return true;
e440a328 4506}
4507
0c77715b 4508// Compare complex constants. Complex numbers may only be compared
4509// for equality.
e440a328 4510
4511bool
0c77715b 4512Binary_expression::compare_complex(const Numeric_constant* left_nc,
4513 const Numeric_constant* right_nc,
4514 int* cmp)
e440a328 4515{
fcbea5e4 4516 mpc_t left_val;
4517 if (!left_nc->to_complex(&left_val))
0c77715b 4518 return false;
fcbea5e4 4519 mpc_t right_val;
4520 if (!right_nc->to_complex(&right_val))
e440a328 4521 {
fcbea5e4 4522 mpc_clear(left_val);
0c77715b 4523 return false;
e440a328 4524 }
0c77715b 4525
4526 // We already coerced both operands to the same type. If that type
4527 // is not an abstract type, we need to round the values accordingly.
4528 Type* type = left_nc->type();
4529 if (!type->is_abstract() && type->complex_type() != NULL)
e440a328 4530 {
0c77715b 4531 int bits = type->complex_type()->bits();
fcbea5e4 4532 mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN);
4533 mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN);
4534 mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN);
4535 mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN);
e440a328 4536 }
0c77715b 4537
fcbea5e4 4538 *cmp = mpc_cmp(left_val, right_val) != 0;
0c77715b 4539
fcbea5e4 4540 mpc_clear(left_val);
4541 mpc_clear(right_val);
0c77715b 4542
4543 return true;
e440a328 4544}
4545
0c77715b 4546// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4547// true if this could be done, false if not. Issue errors at LOCATION
4548// as appropriate.
e440a328 4549
4550bool
0c77715b 4551Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
4552 Numeric_constant* right_nc,
4553 Location location, Numeric_constant* nc)
e440a328 4554{
e440a328 4555 switch (op)
4556 {
4557 case OPERATOR_OROR:
4558 case OPERATOR_ANDAND:
4559 case OPERATOR_EQEQ:
4560 case OPERATOR_NOTEQ:
4561 case OPERATOR_LT:
4562 case OPERATOR_LE:
4563 case OPERATOR_GT:
4564 case OPERATOR_GE:
9767e2d3 4565 // These return boolean values, not numeric.
4566 return false;
0c77715b 4567 default:
4568 break;
4569 }
4570
4571 Type* left_type = left_nc->type();
4572 Type* right_type = right_nc->type();
4573
4574 Type* type;
4575 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4576 return false;
4577
4578 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
4579
4580 // When combining an untyped operand with a typed operand, we are
4581 // effectively coercing the untyped operand to the other operand's
4582 // type, so make sure that is valid.
4583 if (!left_nc->set_type(type, true, location))
4584 return false;
4585 if (!is_shift && !right_nc->set_type(type, true, location))
4586 return false;
85334a21 4587 if (is_shift
4588 && ((left_type->integer_type() == NULL
4589 && !left_type->is_abstract())
4590 || (right_type->integer_type() == NULL
4591 && !right_type->is_abstract())))
4592 return false;
0c77715b 4593
4594 bool r;
4595 if (type->complex_type() != NULL)
4596 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc);
4597 else if (type->float_type() != NULL)
4598 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc);
4599 else
4600 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
4601
4602 if (r)
4603 r = nc->set_type(type, true, location);
4604
4605 return r;
4606}
4607
4608// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4609// integer operations. Return true if this could be done, false if
4610// not.
4611
4612bool
4613Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
4614 const Numeric_constant* right_nc,
4615 Location location, Numeric_constant* nc)
4616{
4617 mpz_t left_val;
4618 if (!left_nc->to_int(&left_val))
4619 return false;
4620 mpz_t right_val;
4621 if (!right_nc->to_int(&right_val))
4622 {
4623 mpz_clear(left_val);
e440a328 4624 return false;
0c77715b 4625 }
4626
4627 mpz_t val;
4628 mpz_init(val);
4629
4630 switch (op)
4631 {
e440a328 4632 case OPERATOR_PLUS:
4633 mpz_add(val, left_val, right_val);
2c809f8f 4634 if (mpz_sizeinbase(val, 2) > 0x100000)
4635 {
4636 error_at(location, "constant addition overflow");
71a45216 4637 nc->set_invalid();
2c809f8f 4638 mpz_set_ui(val, 1);
4639 }
e440a328 4640 break;
4641 case OPERATOR_MINUS:
4642 mpz_sub(val, left_val, right_val);
2c809f8f 4643 if (mpz_sizeinbase(val, 2) > 0x100000)
4644 {
4645 error_at(location, "constant subtraction overflow");
71a45216 4646 nc->set_invalid();
2c809f8f 4647 mpz_set_ui(val, 1);
4648 }
e440a328 4649 break;
4650 case OPERATOR_OR:
4651 mpz_ior(val, left_val, right_val);
4652 break;
4653 case OPERATOR_XOR:
4654 mpz_xor(val, left_val, right_val);
4655 break;
4656 case OPERATOR_MULT:
4657 mpz_mul(val, left_val, right_val);
2c809f8f 4658 if (mpz_sizeinbase(val, 2) > 0x100000)
4659 {
4660 error_at(location, "constant multiplication overflow");
71a45216 4661 nc->set_invalid();
2c809f8f 4662 mpz_set_ui(val, 1);
4663 }
e440a328 4664 break;
4665 case OPERATOR_DIV:
4666 if (mpz_sgn(right_val) != 0)
4667 mpz_tdiv_q(val, left_val, right_val);
4668 else
4669 {
4670 error_at(location, "division by zero");
71a45216 4671 nc->set_invalid();
e440a328 4672 mpz_set_ui(val, 0);
e440a328 4673 }
4674 break;
4675 case OPERATOR_MOD:
4676 if (mpz_sgn(right_val) != 0)
4677 mpz_tdiv_r(val, left_val, right_val);
4678 else
4679 {
4680 error_at(location, "division by zero");
71a45216 4681 nc->set_invalid();
e440a328 4682 mpz_set_ui(val, 0);
e440a328 4683 }
4684 break;
4685 case OPERATOR_LSHIFT:
4686 {
4687 unsigned long shift = mpz_get_ui(right_val);
0c77715b 4688 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
4689 mpz_mul_2exp(val, left_val, shift);
4690 else
e440a328 4691 {
4692 error_at(location, "shift count overflow");
71a45216 4693 nc->set_invalid();
2c809f8f 4694 mpz_set_ui(val, 1);
e440a328 4695 }
e440a328 4696 break;
4697 }
4698 break;
4699 case OPERATOR_RSHIFT:
4700 {
4701 unsigned long shift = mpz_get_ui(right_val);
4702 if (mpz_cmp_ui(right_val, shift) != 0)
4703 {
4704 error_at(location, "shift count overflow");
71a45216 4705 nc->set_invalid();
2c809f8f 4706 mpz_set_ui(val, 1);
e440a328 4707 }
e440a328 4708 else
0c77715b 4709 {
4710 if (mpz_cmp_ui(left_val, 0) >= 0)
4711 mpz_tdiv_q_2exp(val, left_val, shift);
4712 else
4713 mpz_fdiv_q_2exp(val, left_val, shift);
4714 }
e440a328 4715 break;
4716 }
4717 break;
4718 case OPERATOR_AND:
4719 mpz_and(val, left_val, right_val);
4720 break;
4721 case OPERATOR_BITCLEAR:
4722 {
4723 mpz_t tval;
4724 mpz_init(tval);
4725 mpz_com(tval, right_val);
4726 mpz_and(val, left_val, tval);
4727 mpz_clear(tval);
4728 }
4729 break;
4730 default:
c3e6f413 4731 go_unreachable();
e440a328 4732 }
4733
0c77715b 4734 mpz_clear(left_val);
4735 mpz_clear(right_val);
e440a328 4736
0c77715b 4737 if (left_nc->is_rune()
4738 || (op != OPERATOR_LSHIFT
4739 && op != OPERATOR_RSHIFT
4740 && right_nc->is_rune()))
4741 nc->set_rune(NULL, val);
4742 else
4743 nc->set_int(NULL, val);
4744
4745 mpz_clear(val);
e440a328 4746
4747 return true;
4748}
4749
0c77715b 4750// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4751// floating point operations. Return true if this could be done,
4752// false if not.
e440a328 4753
4754bool
0c77715b 4755Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
4756 const Numeric_constant* right_nc,
4757 Location location, Numeric_constant* nc)
e440a328 4758{
0c77715b 4759 mpfr_t left_val;
4760 if (!left_nc->to_float(&left_val))
4761 return false;
4762 mpfr_t right_val;
4763 if (!right_nc->to_float(&right_val))
e440a328 4764 {
0c77715b 4765 mpfr_clear(left_val);
e440a328 4766 return false;
0c77715b 4767 }
4768
4769 mpfr_t val;
4770 mpfr_init(val);
4771
4772 bool ret = true;
4773 switch (op)
4774 {
e440a328 4775 case OPERATOR_PLUS:
4776 mpfr_add(val, left_val, right_val, GMP_RNDN);
4777 break;
4778 case OPERATOR_MINUS:
4779 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4780 break;
4781 case OPERATOR_OR:
4782 case OPERATOR_XOR:
4783 case OPERATOR_AND:
4784 case OPERATOR_BITCLEAR:
0c77715b 4785 case OPERATOR_MOD:
4786 case OPERATOR_LSHIFT:
4787 case OPERATOR_RSHIFT:
4788 mpfr_set_ui(val, 0, GMP_RNDN);
4789 ret = false;
4790 break;
e440a328 4791 case OPERATOR_MULT:
4792 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4793 break;
4794 case OPERATOR_DIV:
0c77715b 4795 if (!mpfr_zero_p(right_val))
4796 mpfr_div(val, left_val, right_val, GMP_RNDN);
4797 else
4798 {
4799 error_at(location, "division by zero");
71a45216 4800 nc->set_invalid();
0c77715b 4801 mpfr_set_ui(val, 0, GMP_RNDN);
4802 }
e440a328 4803 break;
e440a328 4804 default:
c3e6f413 4805 go_unreachable();
e440a328 4806 }
4807
0c77715b 4808 mpfr_clear(left_val);
4809 mpfr_clear(right_val);
e440a328 4810
0c77715b 4811 nc->set_float(NULL, val);
4812 mpfr_clear(val);
e440a328 4813
0c77715b 4814 return ret;
e440a328 4815}
4816
0c77715b 4817// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4818// complex operations. Return true if this could be done, false if
4819// not.
e440a328 4820
4821bool
0c77715b 4822Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
4823 const Numeric_constant* right_nc,
4824 Location location, Numeric_constant* nc)
e440a328 4825{
fcbea5e4 4826 mpc_t left_val;
4827 if (!left_nc->to_complex(&left_val))
0c77715b 4828 return false;
fcbea5e4 4829 mpc_t right_val;
4830 if (!right_nc->to_complex(&right_val))
e440a328 4831 {
fcbea5e4 4832 mpc_clear(left_val);
e440a328 4833 return false;
0c77715b 4834 }
4835
fcbea5e4 4836 mpc_t val;
4837 mpc_init2(val, mpc_precision);
0c77715b 4838
4839 bool ret = true;
4840 switch (op)
4841 {
e440a328 4842 case OPERATOR_PLUS:
fcbea5e4 4843 mpc_add(val, left_val, right_val, MPC_RNDNN);
e440a328 4844 break;
4845 case OPERATOR_MINUS:
fcbea5e4 4846 mpc_sub(val, left_val, right_val, MPC_RNDNN);
e440a328 4847 break;
4848 case OPERATOR_OR:
4849 case OPERATOR_XOR:
4850 case OPERATOR_AND:
4851 case OPERATOR_BITCLEAR:
0c77715b 4852 case OPERATOR_MOD:
4853 case OPERATOR_LSHIFT:
4854 case OPERATOR_RSHIFT:
fcbea5e4 4855 mpc_set_ui(val, 0, MPC_RNDNN);
0c77715b 4856 ret = false;
4857 break;
e440a328 4858 case OPERATOR_MULT:
fcbea5e4 4859 mpc_mul(val, left_val, right_val, MPC_RNDNN);
e440a328 4860 break;
4861 case OPERATOR_DIV:
fcbea5e4 4862 if (mpc_cmp_si(right_val, 0) == 0)
4863 {
4864 error_at(location, "division by zero");
71a45216 4865 nc->set_invalid();
fcbea5e4 4866 mpc_set_ui(val, 0, MPC_RNDNN);
4867 break;
4868 }
4869 mpc_div(val, left_val, right_val, MPC_RNDNN);
e440a328 4870 break;
e440a328 4871 default:
c3e6f413 4872 go_unreachable();
e440a328 4873 }
4874
fcbea5e4 4875 mpc_clear(left_val);
4876 mpc_clear(right_val);
e440a328 4877
fcbea5e4 4878 nc->set_complex(NULL, val);
4879 mpc_clear(val);
e440a328 4880
0c77715b 4881 return ret;
e440a328 4882}
4883
4884// Lower a binary expression. We have to evaluate constant
4885// expressions now, in order to implement Go's unlimited precision
4886// constants.
4887
4888Expression*
e9d3367e 4889Binary_expression::do_lower(Gogo* gogo, Named_object*,
4890 Statement_inserter* inserter, int)
e440a328 4891{
b13c66cd 4892 Location location = this->location();
e440a328 4893 Operator op = this->op_;
4894 Expression* left = this->left_;
4895 Expression* right = this->right_;
4896
4897 const bool is_comparison = (op == OPERATOR_EQEQ
4898 || op == OPERATOR_NOTEQ
4899 || op == OPERATOR_LT
4900 || op == OPERATOR_LE
4901 || op == OPERATOR_GT
4902 || op == OPERATOR_GE);
4903
0c77715b 4904 // Numeric constant expressions.
e440a328 4905 {
0c77715b 4906 Numeric_constant left_nc;
4907 Numeric_constant right_nc;
4908 if (left->numeric_constant_value(&left_nc)
4909 && right->numeric_constant_value(&right_nc))
e440a328 4910 {
0c77715b 4911 if (is_comparison)
e440a328 4912 {
0c77715b 4913 bool result;
4914 if (!Binary_expression::compare_constant(op, &left_nc,
4915 &right_nc, location,
4916 &result))
4917 return this;
e90c9dfc 4918 return Expression::make_cast(Type::make_boolean_type(),
0c77715b 4919 Expression::make_boolean(result,
4920 location),
4921 location);
e440a328 4922 }
4923 else
4924 {
0c77715b 4925 Numeric_constant nc;
4926 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
4927 location, &nc))
71a45216 4928 return this;
0c77715b 4929 return nc.expression(location);
e440a328 4930 }
4931 }
e440a328 4932 }
4933
4934 // String constant expressions.
315fa98d 4935 if (left->type()->is_string_type() && right->type()->is_string_type())
e440a328 4936 {
4937 std::string left_string;
4938 std::string right_string;
4939 if (left->string_constant_value(&left_string)
4940 && right->string_constant_value(&right_string))
315fa98d 4941 {
4942 if (op == OPERATOR_PLUS)
4943 return Expression::make_string(left_string + right_string,
4944 location);
4945 else if (is_comparison)
4946 {
4947 int cmp = left_string.compare(right_string);
0c77715b 4948 bool r = Binary_expression::cmp_to_bool(op, cmp);
e90c9dfc 4949 return Expression::make_boolean(r, location);
b40dc774 4950 }
4951 }
b40dc774 4952 }
4953
ceeb12d7 4954 // Lower struct, array, and some interface comparisons.
e9d3367e 4955 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
4956 {
b79832ca 4957 if (left->type()->struct_type() != NULL
4958 && right->type()->struct_type() != NULL)
e9d3367e 4959 return this->lower_struct_comparison(gogo, inserter);
4960 else if (left->type()->array_type() != NULL
b79832ca 4961 && !left->type()->is_slice_type()
4962 && right->type()->array_type() != NULL
4963 && !right->type()->is_slice_type())
e9d3367e 4964 return this->lower_array_comparison(gogo, inserter);
ceeb12d7 4965 else if ((left->type()->interface_type() != NULL
4966 && right->type()->interface_type() == NULL)
4967 || (left->type()->interface_type() == NULL
4968 && right->type()->interface_type() != NULL))
4969 return this->lower_interface_value_comparison(gogo, inserter);
e9d3367e 4970 }
4971
e440a328 4972 return this;
4973}
4974
e9d3367e 4975// Lower a struct comparison.
4976
4977Expression*
4978Binary_expression::lower_struct_comparison(Gogo* gogo,
4979 Statement_inserter* inserter)
4980{
4981 Struct_type* st = this->left_->type()->struct_type();
4982 Struct_type* st2 = this->right_->type()->struct_type();
4983 if (st2 == NULL)
4984 return this;
4985 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
4986 return this;
4987 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
4988 this->right_->type(), NULL))
4989 return this;
4990
4991 // See if we can compare using memcmp. As a heuristic, we use
4992 // memcmp rather than field references and comparisons if there are
4993 // more than two fields.
113ef6a5 4994 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
e9d3367e 4995 return this->lower_compare_to_memcmp(gogo, inserter);
4996
4997 Location loc = this->location();
4998
4999 Expression* left = this->left_;
5000 Temporary_statement* left_temp = NULL;
5001 if (left->var_expression() == NULL
5002 && left->temporary_reference_expression() == NULL)
5003 {
5004 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5005 inserter->insert(left_temp);
5006 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5007 }
5008
5009 Expression* right = this->right_;
5010 Temporary_statement* right_temp = NULL;
5011 if (right->var_expression() == NULL
5012 && right->temporary_reference_expression() == NULL)
5013 {
5014 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5015 inserter->insert(right_temp);
5016 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5017 }
5018
5019 Expression* ret = Expression::make_boolean(true, loc);
5020 const Struct_field_list* fields = st->fields();
5021 unsigned int field_index = 0;
5022 for (Struct_field_list::const_iterator pf = fields->begin();
5023 pf != fields->end();
5024 ++pf, ++field_index)
5025 {
f5165c05 5026 if (Gogo::is_sink_name(pf->field_name()))
5027 continue;
5028
e9d3367e 5029 if (field_index > 0)
5030 {
5031 if (left_temp == NULL)
5032 left = left->copy();
5033 else
5034 left = Expression::make_temporary_reference(left_temp, loc);
5035 if (right_temp == NULL)
5036 right = right->copy();
5037 else
5038 right = Expression::make_temporary_reference(right_temp, loc);
5039 }
5040 Expression* f1 = Expression::make_field_reference(left, field_index,
5041 loc);
5042 Expression* f2 = Expression::make_field_reference(right, field_index,
5043 loc);
5044 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5045 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5046 }
5047
5048 if (this->op_ == OPERATOR_NOTEQ)
5049 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5050
5051 return ret;
5052}
5053
5054// Lower an array comparison.
5055
5056Expression*
5057Binary_expression::lower_array_comparison(Gogo* gogo,
5058 Statement_inserter* inserter)
5059{
5060 Array_type* at = this->left_->type()->array_type();
5061 Array_type* at2 = this->right_->type()->array_type();
5062 if (at2 == NULL)
5063 return this;
5064 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5065 return this;
5066 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5067 this->right_->type(), NULL))
5068 return this;
5069
5070 // Call memcmp directly if possible. This may let the middle-end
5071 // optimize the call.
113ef6a5 5072 if (at->compare_is_identity(gogo))
e9d3367e 5073 return this->lower_compare_to_memcmp(gogo, inserter);
5074
5075 // Call the array comparison function.
5076 Named_object* hash_fn;
5077 Named_object* equal_fn;
5078 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5079 &hash_fn, &equal_fn);
5080
5081 Location loc = this->location();
5082
5083 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5084
5085 Expression_list* args = new Expression_list();
5086 args->push_back(this->operand_address(inserter, this->left_));
5087 args->push_back(this->operand_address(inserter, this->right_));
5088 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
5089
5090 Expression* ret = Expression::make_call(func, args, false, loc);
5091
5092 if (this->op_ == OPERATOR_NOTEQ)
5093 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5094
5095 return ret;
5096}
5097
ceeb12d7 5098// Lower an interface to value comparison.
5099
5100Expression*
5101Binary_expression::lower_interface_value_comparison(Gogo*,
5102 Statement_inserter* inserter)
5103{
5104 Type* left_type = this->left_->type();
5105 Type* right_type = this->right_->type();
5106 Interface_type* ift;
5107 if (left_type->interface_type() != NULL)
5108 {
5109 ift = left_type->interface_type();
5110 if (!ift->implements_interface(right_type, NULL))
5111 return this;
5112 }
5113 else
5114 {
5115 ift = right_type->interface_type();
5116 if (!ift->implements_interface(left_type, NULL))
5117 return this;
5118 }
5119 if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL))
5120 return this;
5121
5122 Location loc = this->location();
5123
5124 if (left_type->interface_type() == NULL
5125 && left_type->points_to() == NULL
5126 && !this->left_->is_addressable())
5127 {
5128 Temporary_statement* temp =
5129 Statement::make_temporary(left_type, NULL, loc);
5130 inserter->insert(temp);
5131 this->left_ =
5132 Expression::make_set_and_use_temporary(temp, this->left_, loc);
5133 }
5134
5135 if (right_type->interface_type() == NULL
5136 && right_type->points_to() == NULL
5137 && !this->right_->is_addressable())
5138 {
5139 Temporary_statement* temp =
5140 Statement::make_temporary(right_type, NULL, loc);
5141 inserter->insert(temp);
5142 this->right_ =
5143 Expression::make_set_and_use_temporary(temp, this->right_, loc);
5144 }
5145
5146 return this;
5147}
5148
e9d3367e 5149// Lower a struct or array comparison to a call to memcmp.
5150
5151Expression*
5152Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5153{
5154 Location loc = this->location();
5155
5156 Expression* a1 = this->operand_address(inserter, this->left_);
5157 Expression* a2 = this->operand_address(inserter, this->right_);
5158 Expression* len = Expression::make_type_info(this->left_->type(),
5159 TYPE_INFO_SIZE);
5160
5161 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
e67508fa 5162 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
e9d3367e 5163 return Expression::make_binary(this->op_, call, zero, loc);
5164}
5165
a32698ee 5166Expression*
5c3f3470 5167Binary_expression::do_flatten(Gogo* gogo, Named_object*,
a32698ee 5168 Statement_inserter* inserter)
5169{
5170 Location loc = this->location();
5bf8be8b 5171 if (this->left_->type()->is_error_type()
5172 || this->right_->type()->is_error_type()
5173 || this->left_->is_error_expression()
5174 || this->right_->is_error_expression())
5175 {
5176 go_assert(saw_errors());
5177 return Expression::make_error(loc);
5178 }
5179
a32698ee 5180 Temporary_statement* temp;
5181 if (this->left_->type()->is_string_type()
5182 && this->op_ == OPERATOR_PLUS)
5183 {
0108f7b4 5184 if (!this->left_->is_variable()
5185 && !this->left_->is_constant())
a32698ee 5186 {
5187 temp = Statement::make_temporary(NULL, this->left_, loc);
5188 inserter->insert(temp);
5189 this->left_ = Expression::make_temporary_reference(temp, loc);
5190 }
0108f7b4 5191 if (!this->right_->is_variable()
5192 && !this->right_->is_constant())
a32698ee 5193 {
5194 temp =
5195 Statement::make_temporary(this->left_->type(), this->right_, loc);
5196 this->right_ = Expression::make_temporary_reference(temp, loc);
5197 inserter->insert(temp);
5198 }
5199 }
5200
5201 Type* left_type = this->left_->type();
5202 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5203 || this->op_ == OPERATOR_RSHIFT);
5204 bool is_idiv_op = ((this->op_ == OPERATOR_DIV &&
5205 left_type->integer_type() != NULL)
5206 || this->op_ == OPERATOR_MOD);
5207
a32698ee 5208 if (is_shift_op
5c3f3470 5209 || (is_idiv_op
5210 && (gogo->check_divide_by_zero() || gogo->check_divide_overflow())))
a32698ee 5211 {
545ab43b 5212 if (!this->left_->is_variable() && !this->left_->is_constant())
a32698ee 5213 {
5214 temp = Statement::make_temporary(NULL, this->left_, loc);
5215 inserter->insert(temp);
5216 this->left_ = Expression::make_temporary_reference(temp, loc);
5217 }
545ab43b 5218 if (!this->right_->is_variable() && !this->right_->is_constant())
a32698ee 5219 {
5220 temp =
5221 Statement::make_temporary(NULL, this->right_, loc);
5222 this->right_ = Expression::make_temporary_reference(temp, loc);
5223 inserter->insert(temp);
5224 }
5225 }
5226 return this;
5227}
5228
5229
e9d3367e 5230// Return the address of EXPR, cast to unsafe.Pointer.
5231
5232Expression*
5233Binary_expression::operand_address(Statement_inserter* inserter,
5234 Expression* expr)
5235{
5236 Location loc = this->location();
5237
5238 if (!expr->is_addressable())
5239 {
5240 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5241 loc);
5242 inserter->insert(temp);
5243 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5244 }
5245 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5246 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5247 Type* void_type = Type::make_void_type();
5248 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5249 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5250}
5251
0c77715b 5252// Return the numeric constant value, if it has one.
e440a328 5253
5254bool
0c77715b 5255Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 5256{
0c77715b 5257 Numeric_constant left_nc;
5258 if (!this->left_->numeric_constant_value(&left_nc))
5259 return false;
5260 Numeric_constant right_nc;
5261 if (!this->right_->numeric_constant_value(&right_nc))
5262 return false;
9767e2d3 5263 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
0c77715b 5264 this->location(), nc);
e440a328 5265}
5266
5267// Note that the value is being discarded.
5268
4f2138d7 5269bool
e440a328 5270Binary_expression::do_discarding_value()
5271{
5272 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
4f2138d7 5273 return this->right_->discarding_value();
e440a328 5274 else
4f2138d7 5275 {
5276 this->unused_value_error();
5277 return false;
5278 }
e440a328 5279}
5280
5281// Get type.
5282
5283Type*
5284Binary_expression::do_type()
5285{
5f5fea79 5286 if (this->classification() == EXPRESSION_ERROR)
5287 return Type::make_error_type();
5288
e440a328 5289 switch (this->op_)
5290 {
e440a328 5291 case OPERATOR_EQEQ:
5292 case OPERATOR_NOTEQ:
5293 case OPERATOR_LT:
5294 case OPERATOR_LE:
5295 case OPERATOR_GT:
5296 case OPERATOR_GE:
e90c9dfc 5297 if (this->type_ == NULL)
5298 this->type_ = Type::make_boolean_type();
5299 return this->type_;
e440a328 5300
5301 case OPERATOR_PLUS:
5302 case OPERATOR_MINUS:
5303 case OPERATOR_OR:
5304 case OPERATOR_XOR:
5305 case OPERATOR_MULT:
5306 case OPERATOR_DIV:
5307 case OPERATOR_MOD:
5308 case OPERATOR_AND:
5309 case OPERATOR_BITCLEAR:
e90c9dfc 5310 case OPERATOR_OROR:
5311 case OPERATOR_ANDAND:
e440a328 5312 {
0c77715b 5313 Type* type;
5314 if (!Binary_expression::operation_type(this->op_,
5315 this->left_->type(),
5316 this->right_->type(),
5317 &type))
5318 return Type::make_error_type();
5319 return type;
e440a328 5320 }
5321
5322 case OPERATOR_LSHIFT:
5323 case OPERATOR_RSHIFT:
5324 return this->left_->type();
5325
5326 default:
c3e6f413 5327 go_unreachable();
e440a328 5328 }
5329}
5330
5331// Set type for a binary expression.
5332
5333void
5334Binary_expression::do_determine_type(const Type_context* context)
5335{
5336 Type* tleft = this->left_->type();
5337 Type* tright = this->right_->type();
5338
5339 // Both sides should have the same type, except for the shift
5340 // operations. For a comparison, we should ignore the incoming
5341 // type.
5342
5343 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5344 || this->op_ == OPERATOR_RSHIFT);
5345
5346 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5347 || this->op_ == OPERATOR_NOTEQ
5348 || this->op_ == OPERATOR_LT
5349 || this->op_ == OPERATOR_LE
5350 || this->op_ == OPERATOR_GT
5351 || this->op_ == OPERATOR_GE);
5352
c999c2a7 5353 // For constant expressions, the context of the result is not useful in
5354 // determining the types of the operands. It is only legal to use abstract
5355 // boolean, numeric, and string constants as operands where it is legal to
5356 // use non-abstract boolean, numeric, and string constants, respectively.
5357 // Any issues with the operation will be resolved in the check_types pass.
5358 bool is_constant_expr = (this->left_->is_constant()
5359 && this->right_->is_constant());
5360
e440a328 5361 Type_context subcontext(*context);
5362
5363 if (is_comparison)
5364 {
5365 // In a comparison, the context does not determine the types of
5366 // the operands.
5367 subcontext.type = NULL;
5368 }
5369
5370 // Set the context for the left hand operand.
5371 if (is_shift_op)
5372 {
b40dc774 5373 // The right hand operand of a shift plays no role in
5374 // determining the type of the left hand operand.
e440a328 5375 }
5376 else if (!tleft->is_abstract())
5377 subcontext.type = tleft;
5378 else if (!tright->is_abstract())
5379 subcontext.type = tright;
5380 else if (subcontext.type == NULL)
5381 {
5382 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5383 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5384 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5385 {
5386 // Both sides have an abstract integer, abstract float, or
5387 // abstract complex type. Just let CONTEXT determine
5388 // whether they may remain abstract or not.
5389 }
5390 else if (tleft->complex_type() != NULL)
5391 subcontext.type = tleft;
5392 else if (tright->complex_type() != NULL)
5393 subcontext.type = tright;
5394 else if (tleft->float_type() != NULL)
5395 subcontext.type = tleft;
5396 else if (tright->float_type() != NULL)
5397 subcontext.type = tright;
5398 else
5399 subcontext.type = tleft;
f58a23ae 5400
5401 if (subcontext.type != NULL && !context->may_be_abstract)
5402 subcontext.type = subcontext.type->make_non_abstract_type();
e440a328 5403 }
5404
c999c2a7 5405 if (!is_constant_expr)
5406 this->left_->determine_type(&subcontext);
e440a328 5407
e440a328 5408 if (is_shift_op)
5409 {
b40dc774 5410 // We may have inherited an unusable type for the shift operand.
5411 // Give a useful error if that happened.
5412 if (tleft->is_abstract()
5413 && subcontext.type != NULL
8ab6effb 5414 && !subcontext.may_be_abstract
f6bc81e6 5415 && subcontext.type->interface_type() == NULL
8ab6effb 5416 && subcontext.type->integer_type() == NULL)
b40dc774 5417 this->report_error(("invalid context-determined non-integer type "
8ab6effb 5418 "for left operand of shift"));
b40dc774 5419
5420 // The context for the right hand operand is the same as for the
5421 // left hand operand, except for a shift operator.
e440a328 5422 subcontext.type = Type::lookup_integer_type("uint");
5423 subcontext.may_be_abstract = false;
5424 }
5425
c999c2a7 5426 if (!is_constant_expr)
5427 this->right_->determine_type(&subcontext);
e90c9dfc 5428
5429 if (is_comparison)
5430 {
5431 if (this->type_ != NULL && !this->type_->is_abstract())
5432 ;
5433 else if (context->type != NULL && context->type->is_boolean_type())
5434 this->type_ = context->type;
5435 else if (!context->may_be_abstract)
5436 this->type_ = Type::lookup_bool_type();
5437 }
e440a328 5438}
5439
5440// Report an error if the binary operator OP does not support TYPE.
be8b5eee 5441// OTYPE is the type of the other operand. Return whether the
5442// operation is OK. This should not be used for shift.
e440a328 5443
5444bool
be8b5eee 5445Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
b13c66cd 5446 Location location)
e440a328 5447{
5448 switch (op)
5449 {
5450 case OPERATOR_OROR:
5451 case OPERATOR_ANDAND:
c999c2a7 5452 if (!type->is_boolean_type()
5453 || !otype->is_boolean_type())
e440a328 5454 {
5455 error_at(location, "expected boolean type");
5456 return false;
5457 }
5458 break;
5459
5460 case OPERATOR_EQEQ:
5461 case OPERATOR_NOTEQ:
e9d3367e 5462 {
5463 std::string reason;
5464 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
5465 {
5466 error_at(location, "%s", reason.c_str());
5467 return false;
5468 }
5469 }
e440a328 5470 break;
5471
5472 case OPERATOR_LT:
5473 case OPERATOR_LE:
5474 case OPERATOR_GT:
5475 case OPERATOR_GE:
e9d3367e 5476 {
5477 std::string reason;
5478 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
5479 {
5480 error_at(location, "%s", reason.c_str());
5481 return false;
5482 }
5483 }
e440a328 5484 break;
5485
5486 case OPERATOR_PLUS:
5487 case OPERATOR_PLUSEQ:
c999c2a7 5488 if ((!type->is_numeric_type() && !type->is_string_type())
5489 || (!otype->is_numeric_type() && !otype->is_string_type()))
e440a328 5490 {
5491 error_at(location,
5492 "expected integer, floating, complex, or string type");
5493 return false;
5494 }
5495 break;
5496
5497 case OPERATOR_MINUS:
5498 case OPERATOR_MINUSEQ:
5499 case OPERATOR_MULT:
5500 case OPERATOR_MULTEQ:
5501 case OPERATOR_DIV:
5502 case OPERATOR_DIVEQ:
c999c2a7 5503 if (!type->is_numeric_type() || !otype->is_numeric_type())
e440a328 5504 {
5505 error_at(location, "expected integer, floating, or complex type");
5506 return false;
5507 }
5508 break;
5509
5510 case OPERATOR_MOD:
5511 case OPERATOR_MODEQ:
5512 case OPERATOR_OR:
5513 case OPERATOR_OREQ:
5514 case OPERATOR_AND:
5515 case OPERATOR_ANDEQ:
5516 case OPERATOR_XOR:
5517 case OPERATOR_XOREQ:
5518 case OPERATOR_BITCLEAR:
5519 case OPERATOR_BITCLEAREQ:
c999c2a7 5520 if (type->integer_type() == NULL || otype->integer_type() == NULL)
e440a328 5521 {
5522 error_at(location, "expected integer type");
5523 return false;
5524 }
5525 break;
5526
5527 default:
c3e6f413 5528 go_unreachable();
e440a328 5529 }
5530
5531 return true;
5532}
5533
5534// Check types.
5535
5536void
5537Binary_expression::do_check_types(Gogo*)
5538{
5f5fea79 5539 if (this->classification() == EXPRESSION_ERROR)
5540 return;
5541
e440a328 5542 Type* left_type = this->left_->type();
5543 Type* right_type = this->right_->type();
5c13bd80 5544 if (left_type->is_error() || right_type->is_error())
9fe897ef 5545 {
5546 this->set_is_error();
5547 return;
5548 }
e440a328 5549
5550 if (this->op_ == OPERATOR_EQEQ
5551 || this->op_ == OPERATOR_NOTEQ
5552 || this->op_ == OPERATOR_LT
5553 || this->op_ == OPERATOR_LE
5554 || this->op_ == OPERATOR_GT
5555 || this->op_ == OPERATOR_GE)
5556 {
907c5ecd 5557 if (left_type->is_nil_type() && right_type->is_nil_type())
5558 {
5559 this->report_error(_("invalid comparison of nil with nil"));
5560 return;
5561 }
e440a328 5562 if (!Type::are_assignable(left_type, right_type, NULL)
5563 && !Type::are_assignable(right_type, left_type, NULL))
5564 {
5565 this->report_error(_("incompatible types in binary expression"));
5566 return;
5567 }
5568 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5569 right_type,
e440a328 5570 this->location())
5571 || !Binary_expression::check_operator_type(this->op_, right_type,
be8b5eee 5572 left_type,
e440a328 5573 this->location()))
5574 {
5575 this->set_is_error();
5576 return;
5577 }
5578 }
5579 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5580 {
5581 if (!Type::are_compatible_for_binop(left_type, right_type))
5582 {
5583 this->report_error(_("incompatible types in binary expression"));
5584 return;
5585 }
5586 if (!Binary_expression::check_operator_type(this->op_, left_type,
be8b5eee 5587 right_type,
e440a328 5588 this->location()))
5589 {
5590 this->set_is_error();
5591 return;
5592 }
5c65b19d 5593 if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD)
5594 {
5595 // Division by a zero integer constant is an error.
5596 Numeric_constant rconst;
5597 unsigned long rval;
5598 if (left_type->integer_type() != NULL
5599 && this->right_->numeric_constant_value(&rconst)
5600 && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID
5601 && rval == 0)
5602 {
5603 this->report_error(_("integer division by zero"));
5604 return;
5605 }
5606 }
e440a328 5607 }
5608 else
5609 {
5610 if (left_type->integer_type() == NULL)
5611 this->report_error(_("shift of non-integer operand"));
5612
6b5e0fac 5613 if (right_type->is_string_type())
5614 this->report_error(_("shift count not unsigned integer"));
5615 else if (!right_type->is_abstract()
e440a328 5616 && (right_type->integer_type() == NULL
5617 || !right_type->integer_type()->is_unsigned()))
5618 this->report_error(_("shift count not unsigned integer"));
5619 else
5620 {
0c77715b 5621 Numeric_constant nc;
5622 if (this->right_->numeric_constant_value(&nc))
e440a328 5623 {
0c77715b 5624 mpz_t val;
5625 if (!nc.to_int(&val))
5626 this->report_error(_("shift count not unsigned integer"));
5627 else
a4eba91b 5628 {
0c77715b 5629 if (mpz_sgn(val) < 0)
5630 {
5631 this->report_error(_("negative shift count"));
0c77715b 5632 Location rloc = this->right_->location();
e67508fa 5633 this->right_ = Expression::make_integer_ul(0, right_type,
5634 rloc);
0c77715b 5635 }
5636 mpz_clear(val);
a4eba91b 5637 }
e440a328 5638 }
e440a328 5639 }
5640 }
5641}
5642
ea664253 5643// Get the backend representation for a binary expression.
e440a328 5644
ea664253 5645Bexpression*
5646Binary_expression::do_get_backend(Translate_context* context)
e440a328 5647{
1b1f2abf 5648 Gogo* gogo = context->gogo();
a32698ee 5649 Location loc = this->location();
5650 Type* left_type = this->left_->type();
5651 Type* right_type = this->right_->type();
1b1f2abf 5652
e440a328 5653 bool use_left_type = true;
5654 bool is_shift_op = false;
29a2d1d8 5655 bool is_idiv_op = false;
e440a328 5656 switch (this->op_)
5657 {
5658 case OPERATOR_EQEQ:
5659 case OPERATOR_NOTEQ:
5660 case OPERATOR_LT:
5661 case OPERATOR_LE:
5662 case OPERATOR_GT:
5663 case OPERATOR_GE:
ea664253 5664 return Expression::comparison(context, this->type_, this->op_,
5665 this->left_, this->right_, loc);
e440a328 5666
5667 case OPERATOR_OROR:
e440a328 5668 case OPERATOR_ANDAND:
e440a328 5669 use_left_type = false;
5670 break;
5671 case OPERATOR_PLUS:
e440a328 5672 case OPERATOR_MINUS:
e440a328 5673 case OPERATOR_OR:
e440a328 5674 case OPERATOR_XOR:
e440a328 5675 case OPERATOR_MULT:
e440a328 5676 break;
5677 case OPERATOR_DIV:
a32698ee 5678 if (left_type->float_type() != NULL || left_type->complex_type() != NULL)
5679 break;
e440a328 5680 case OPERATOR_MOD:
29a2d1d8 5681 is_idiv_op = true;
e440a328 5682 break;
5683 case OPERATOR_LSHIFT:
e440a328 5684 case OPERATOR_RSHIFT:
e440a328 5685 is_shift_op = true;
5686 break;
e440a328 5687 case OPERATOR_BITCLEAR:
a32698ee 5688 this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc);
5689 case OPERATOR_AND:
e440a328 5690 break;
5691 default:
c3e6f413 5692 go_unreachable();
e440a328 5693 }
5694
a32698ee 5695 if (left_type->is_string_type())
e440a328 5696 {
c484d925 5697 go_assert(this->op_ == OPERATOR_PLUS);
a32698ee 5698 Expression* string_plus =
5699 Runtime::make_call(Runtime::STRING_PLUS, loc, 2,
5700 this->left_, this->right_);
ea664253 5701 return string_plus->get_backend(context);
a32698ee 5702 }
5703
5704 // For complex division Go might want slightly different results than the
5705 // backend implementation provides, so we have our own runtime routine.
1850e20c 5706 if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL)
5707 {
a32698ee 5708 Runtime::Function complex_code;
1850e20c 5709 switch (this->left_->type()->complex_type()->bits())
5710 {
5711 case 64:
a32698ee 5712 complex_code = Runtime::COMPLEX64_DIV;
1850e20c 5713 break;
5714 case 128:
a32698ee 5715 complex_code = Runtime::COMPLEX128_DIV;
1850e20c 5716 break;
5717 default:
5718 go_unreachable();
5719 }
a32698ee 5720 Expression* complex_div =
5721 Runtime::make_call(complex_code, loc, 2, this->left_, this->right_);
ea664253 5722 return complex_div->get_backend(context);
1850e20c 5723 }
5724
ea664253 5725 Bexpression* left = this->left_->get_backend(context);
5726 Bexpression* right = this->right_->get_backend(context);
e440a328 5727
a32698ee 5728 Type* type = use_left_type ? left_type : right_type;
5729 Btype* btype = type->get_backend(gogo);
5730
5731 Bexpression* ret =
5732 gogo->backend()->binary_expression(this->op_, left, right, loc);
5733 ret = gogo->backend()->convert_expression(btype, ret, loc);
e440a328 5734
a32698ee 5735 // Initialize overflow constants.
5736 Bexpression* overflow;
5737 mpz_t zero;
5738 mpz_init_set_ui(zero, 0UL);
5739 mpz_t one;
5740 mpz_init_set_ui(one, 1UL);
5741 mpz_t neg_one;
5742 mpz_init_set_si(neg_one, -1);
e440a328 5743
a32698ee 5744 Btype* left_btype = left_type->get_backend(gogo);
5745 Btype* right_btype = right_type->get_backend(gogo);
e440a328 5746
5747 // In Go, a shift larger than the size of the type is well-defined.
a32698ee 5748 // This is not true in C, so we need to insert a conditional.
e440a328 5749 if (is_shift_op)
5750 {
a32698ee 5751 go_assert(left_type->integer_type() != NULL);
e440a328 5752
a32698ee 5753 mpz_t bitsval;
5754 int bits = left_type->integer_type()->bits();
5755 mpz_init_set_ui(bitsval, bits);
5756 Bexpression* bits_expr =
5757 gogo->backend()->integer_constant_expression(right_btype, bitsval);
5758 Bexpression* compare =
5759 gogo->backend()->binary_expression(OPERATOR_LT,
5760 right, bits_expr, loc);
e440a328 5761
a32698ee 5762 Bexpression* zero_expr =
5763 gogo->backend()->integer_constant_expression(left_btype, zero);
5764 overflow = zero_expr;
e440a328 5765 if (this->op_ == OPERATOR_RSHIFT
a32698ee 5766 && !left_type->integer_type()->is_unsigned())
e440a328 5767 {
a32698ee 5768 Bexpression* neg_expr =
5769 gogo->backend()->binary_expression(OPERATOR_LT, left,
5770 zero_expr, loc);
5771 Bexpression* neg_one_expr =
5772 gogo->backend()->integer_constant_expression(left_btype, neg_one);
5773 overflow = gogo->backend()->conditional_expression(btype, neg_expr,
5774 neg_one_expr,
5775 zero_expr, loc);
29a2d1d8 5776 }
a32698ee 5777 ret = gogo->backend()->conditional_expression(btype, compare, ret,
5778 overflow, loc);
5779 mpz_clear(bitsval);
29a2d1d8 5780 }
5781
5782 // Add checks for division by zero and division overflow as needed.
5783 if (is_idiv_op)
5784 {
5c3f3470 5785 if (gogo->check_divide_by_zero())
29a2d1d8 5786 {
5787 // right == 0
a32698ee 5788 Bexpression* zero_expr =
5789 gogo->backend()->integer_constant_expression(right_btype, zero);
5790 Bexpression* check =
5791 gogo->backend()->binary_expression(OPERATOR_EQEQ,
5792 right, zero_expr, loc);
29a2d1d8 5793
a32698ee 5794 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO)
29a2d1d8 5795 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO;
ea664253 5796 Bexpression* crash = gogo->runtime_error(errcode,
5797 loc)->get_backend(context);
29a2d1d8 5798
5799 // right == 0 ? (__go_runtime_error(...), 0) : ret
ea664253 5800 ret = gogo->backend()->conditional_expression(btype, check, crash,
5801 ret, loc);
b13c66cd 5802 }
5803
5c3f3470 5804 if (gogo->check_divide_overflow())
29a2d1d8 5805 {
5806 // right == -1
5807 // FIXME: It would be nice to say that this test is expected
5808 // to return false.
a32698ee 5809
5810 Bexpression* neg_one_expr =
5811 gogo->backend()->integer_constant_expression(right_btype, neg_one);
5812 Bexpression* check =
5813 gogo->backend()->binary_expression(OPERATOR_EQEQ,
5814 right, neg_one_expr, loc);
5815
5816 Bexpression* zero_expr =
5817 gogo->backend()->integer_constant_expression(btype, zero);
5818 Bexpression* one_expr =
5819 gogo->backend()->integer_constant_expression(btype, one);
5820
5821 if (type->integer_type()->is_unsigned())
29a2d1d8 5822 {
5823 // An unsigned -1 is the largest possible number, so
5824 // dividing is always 1 or 0.
a32698ee 5825
5826 Bexpression* cmp =
5827 gogo->backend()->binary_expression(OPERATOR_EQEQ,
5828 left, right, loc);
29a2d1d8 5829 if (this->op_ == OPERATOR_DIV)
a32698ee 5830 overflow =
5831 gogo->backend()->conditional_expression(btype, cmp,
5832 one_expr, zero_expr,
5833 loc);
29a2d1d8 5834 else
a32698ee 5835 overflow =
5836 gogo->backend()->conditional_expression(btype, cmp,
5837 zero_expr, left,
5838 loc);
29a2d1d8 5839 }
5840 else
5841 {
5842 // Computing left / -1 is the same as computing - left,
5843 // which does not overflow since Go sets -fwrapv.
5844 if (this->op_ == OPERATOR_DIV)
a32698ee 5845 {
5846 Expression* negate_expr =
5847 Expression::make_unary(OPERATOR_MINUS, this->left_, loc);
ea664253 5848 overflow = negate_expr->get_backend(context);
a32698ee 5849 }
29a2d1d8 5850 else
a32698ee 5851 overflow = zero_expr;
29a2d1d8 5852 }
a32698ee 5853 overflow = gogo->backend()->convert_expression(btype, overflow, loc);
29a2d1d8 5854
5855 // right == -1 ? - left : ret
a32698ee 5856 ret = gogo->backend()->conditional_expression(btype, check, overflow,
5857 ret, loc);
29a2d1d8 5858 }
e440a328 5859 }
5860
a32698ee 5861 mpz_clear(zero);
5862 mpz_clear(one);
5863 mpz_clear(neg_one);
ea664253 5864 return ret;
e440a328 5865}
5866
5867// Export a binary expression.
5868
5869void
5870Binary_expression::do_export(Export* exp) const
5871{
5872 exp->write_c_string("(");
5873 this->left_->export_expression(exp);
5874 switch (this->op_)
5875 {
5876 case OPERATOR_OROR:
5877 exp->write_c_string(" || ");
5878 break;
5879 case OPERATOR_ANDAND:
5880 exp->write_c_string(" && ");
5881 break;
5882 case OPERATOR_EQEQ:
5883 exp->write_c_string(" == ");
5884 break;
5885 case OPERATOR_NOTEQ:
5886 exp->write_c_string(" != ");
5887 break;
5888 case OPERATOR_LT:
5889 exp->write_c_string(" < ");
5890 break;
5891 case OPERATOR_LE:
5892 exp->write_c_string(" <= ");
5893 break;
5894 case OPERATOR_GT:
5895 exp->write_c_string(" > ");
5896 break;
5897 case OPERATOR_GE:
5898 exp->write_c_string(" >= ");
5899 break;
5900 case OPERATOR_PLUS:
5901 exp->write_c_string(" + ");
5902 break;
5903 case OPERATOR_MINUS:
5904 exp->write_c_string(" - ");
5905 break;
5906 case OPERATOR_OR:
5907 exp->write_c_string(" | ");
5908 break;
5909 case OPERATOR_XOR:
5910 exp->write_c_string(" ^ ");
5911 break;
5912 case OPERATOR_MULT:
5913 exp->write_c_string(" * ");
5914 break;
5915 case OPERATOR_DIV:
5916 exp->write_c_string(" / ");
5917 break;
5918 case OPERATOR_MOD:
5919 exp->write_c_string(" % ");
5920 break;
5921 case OPERATOR_LSHIFT:
5922 exp->write_c_string(" << ");
5923 break;
5924 case OPERATOR_RSHIFT:
5925 exp->write_c_string(" >> ");
5926 break;
5927 case OPERATOR_AND:
5928 exp->write_c_string(" & ");
5929 break;
5930 case OPERATOR_BITCLEAR:
5931 exp->write_c_string(" &^ ");
5932 break;
5933 default:
c3e6f413 5934 go_unreachable();
e440a328 5935 }
5936 this->right_->export_expression(exp);
5937 exp->write_c_string(")");
5938}
5939
5940// Import a binary expression.
5941
5942Expression*
5943Binary_expression::do_import(Import* imp)
5944{
5945 imp->require_c_string("(");
5946
5947 Expression* left = Expression::import_expression(imp);
5948
5949 Operator op;
5950 if (imp->match_c_string(" || "))
5951 {
5952 op = OPERATOR_OROR;
5953 imp->advance(4);
5954 }
5955 else if (imp->match_c_string(" && "))
5956 {
5957 op = OPERATOR_ANDAND;
5958 imp->advance(4);
5959 }
5960 else if (imp->match_c_string(" == "))
5961 {
5962 op = OPERATOR_EQEQ;
5963 imp->advance(4);
5964 }
5965 else if (imp->match_c_string(" != "))
5966 {
5967 op = OPERATOR_NOTEQ;
5968 imp->advance(4);
5969 }
5970 else if (imp->match_c_string(" < "))
5971 {
5972 op = OPERATOR_LT;
5973 imp->advance(3);
5974 }
5975 else if (imp->match_c_string(" <= "))
5976 {
5977 op = OPERATOR_LE;
5978 imp->advance(4);
5979 }
5980 else if (imp->match_c_string(" > "))
5981 {
5982 op = OPERATOR_GT;
5983 imp->advance(3);
5984 }
5985 else if (imp->match_c_string(" >= "))
5986 {
5987 op = OPERATOR_GE;
5988 imp->advance(4);
5989 }
5990 else if (imp->match_c_string(" + "))
5991 {
5992 op = OPERATOR_PLUS;
5993 imp->advance(3);
5994 }
5995 else if (imp->match_c_string(" - "))
5996 {
5997 op = OPERATOR_MINUS;
5998 imp->advance(3);
5999 }
6000 else if (imp->match_c_string(" | "))
6001 {
6002 op = OPERATOR_OR;
6003 imp->advance(3);
6004 }
6005 else if (imp->match_c_string(" ^ "))
6006 {
6007 op = OPERATOR_XOR;
6008 imp->advance(3);
6009 }
6010 else if (imp->match_c_string(" * "))
6011 {
6012 op = OPERATOR_MULT;
6013 imp->advance(3);
6014 }
6015 else if (imp->match_c_string(" / "))
6016 {
6017 op = OPERATOR_DIV;
6018 imp->advance(3);
6019 }
6020 else if (imp->match_c_string(" % "))
6021 {
6022 op = OPERATOR_MOD;
6023 imp->advance(3);
6024 }
6025 else if (imp->match_c_string(" << "))
6026 {
6027 op = OPERATOR_LSHIFT;
6028 imp->advance(4);
6029 }
6030 else if (imp->match_c_string(" >> "))
6031 {
6032 op = OPERATOR_RSHIFT;
6033 imp->advance(4);
6034 }
6035 else if (imp->match_c_string(" & "))
6036 {
6037 op = OPERATOR_AND;
6038 imp->advance(3);
6039 }
6040 else if (imp->match_c_string(" &^ "))
6041 {
6042 op = OPERATOR_BITCLEAR;
6043 imp->advance(4);
6044 }
6045 else
6046 {
6047 error_at(imp->location(), "unrecognized binary operator");
6048 return Expression::make_error(imp->location());
6049 }
6050
6051 Expression* right = Expression::import_expression(imp);
6052
6053 imp->require_c_string(")");
6054
6055 return Expression::make_binary(op, left, right, imp->location());
6056}
6057
d751bb78 6058// Dump ast representation of a binary expression.
6059
6060void
6061Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6062{
6063 ast_dump_context->ostream() << "(";
6064 ast_dump_context->dump_expression(this->left_);
6065 ast_dump_context->ostream() << " ";
6066 ast_dump_context->dump_operator(this->op_);
6067 ast_dump_context->ostream() << " ";
6068 ast_dump_context->dump_expression(this->right_);
6069 ast_dump_context->ostream() << ") ";
6070}
6071
e440a328 6072// Make a binary expression.
6073
6074Expression*
6075Expression::make_binary(Operator op, Expression* left, Expression* right,
b13c66cd 6076 Location location)
e440a328 6077{
6078 return new Binary_expression(op, left, right, location);
6079}
6080
6081// Implement a comparison.
6082
a32698ee 6083Bexpression*
6084Expression::comparison(Translate_context* context, Type* result_type,
6085 Operator op, Expression* left, Expression* right,
6086 Location location)
e440a328 6087{
2387f644 6088 Type* left_type = left->type();
6089 Type* right_type = right->type();
ceeb12d7 6090
e67508fa 6091 Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
1b1f2abf 6092
15c67ee2 6093 if (left_type->is_string_type() && right_type->is_string_type())
e440a328 6094 {
2387f644 6095 left = Runtime::make_call(Runtime::STRCMP, location, 2,
6096 left, right);
6097 right = zexpr;
e440a328 6098 }
15c67ee2 6099 else if ((left_type->interface_type() != NULL
6100 && right_type->interface_type() == NULL
6101 && !right_type->is_nil_type())
6102 || (left_type->interface_type() == NULL
6103 && !left_type->is_nil_type()
6104 && right_type->interface_type() != NULL))
e440a328 6105 {
6106 // Comparing an interface value to a non-interface value.
6107 if (left_type->interface_type() == NULL)
6108 {
6109 std::swap(left_type, right_type);
2387f644 6110 std::swap(left, right);
e440a328 6111 }
6112
6113 // The right operand is not an interface. We need to take its
6114 // address if it is not a pointer.
ceeb12d7 6115 Expression* pointer_arg = NULL;
e440a328 6116 if (right_type->points_to() != NULL)
2387f644 6117 pointer_arg = right;
e440a328 6118 else
6119 {
2387f644 6120 go_assert(right->is_addressable());
6121 pointer_arg = Expression::make_unary(OPERATOR_AND, right,
ceeb12d7 6122 location);
e440a328 6123 }
e440a328 6124
2387f644 6125 Expression* descriptor =
6126 Expression::make_type_descriptor(right_type, location);
6127 left =
ceeb12d7 6128 Runtime::make_call((left_type->interface_type()->is_empty()
6129 ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE
6130 : Runtime::INTERFACE_VALUE_COMPARE),
2387f644 6131 location, 3, left, descriptor,
ceeb12d7 6132 pointer_arg);
2387f644 6133 right = zexpr;
e440a328 6134 }
6135 else if (left_type->interface_type() != NULL
6136 && right_type->interface_type() != NULL)
6137 {
ceeb12d7 6138 Runtime::Function compare_function;
739bad04 6139 if (left_type->interface_type()->is_empty()
6140 && right_type->interface_type()->is_empty())
ceeb12d7 6141 compare_function = Runtime::EMPTY_INTERFACE_COMPARE;
739bad04 6142 else if (!left_type->interface_type()->is_empty()
6143 && !right_type->interface_type()->is_empty())
ceeb12d7 6144 compare_function = Runtime::INTERFACE_COMPARE;
739bad04 6145 else
6146 {
6147 if (left_type->interface_type()->is_empty())
6148 {
c484d925 6149 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
739bad04 6150 std::swap(left_type, right_type);
2387f644 6151 std::swap(left, right);
739bad04 6152 }
c484d925 6153 go_assert(!left_type->interface_type()->is_empty());
6154 go_assert(right_type->interface_type()->is_empty());
ceeb12d7 6155 compare_function = Runtime::INTERFACE_EMPTY_COMPARE;
739bad04 6156 }
6157
2387f644 6158 left = Runtime::make_call(compare_function, location, 2, left, right);
6159 right = zexpr;
e440a328 6160 }
6161
6162 if (left_type->is_nil_type()
6163 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6164 {
6165 std::swap(left_type, right_type);
2387f644 6166 std::swap(left, right);
e440a328 6167 }
6168
6169 if (right_type->is_nil_type())
6170 {
2387f644 6171 right = Expression::make_nil(location);
e440a328 6172 if (left_type->array_type() != NULL
6173 && left_type->array_type()->length() == NULL)
6174 {
6175 Array_type* at = left_type->array_type();
2387f644 6176 left = at->get_value_pointer(context->gogo(), left);
e440a328 6177 }
6178 else if (left_type->interface_type() != NULL)
6179 {
6180 // An interface is nil if the first field is nil.
2387f644 6181 left = Expression::make_field_reference(left, 0, location);
e440a328 6182 }
6183 }
6184
ea664253 6185 Bexpression* left_bexpr = left->get_backend(context);
6186 Bexpression* right_bexpr = right->get_backend(context);
e90c9dfc 6187
a32698ee 6188 Gogo* gogo = context->gogo();
6189 Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr,
6190 right_bexpr, location);
6191 if (result_type != NULL)
6192 ret = gogo->backend()->convert_expression(result_type->get_backend(gogo),
6193 ret, location);
e440a328 6194 return ret;
6195}
6196
6197// Class Bound_method_expression.
6198
6199// Traversal.
6200
6201int
6202Bound_method_expression::do_traverse(Traverse* traverse)
6203{
e0659c9e 6204 return Expression::traverse(&this->expr_, traverse);
e440a328 6205}
6206
0afbb937 6207// Lower the expression. If this is a method value rather than being
6208// called, and the method is accessed via a pointer, we may need to
6209// add nil checks. Introduce a temporary variable so that those nil
6210// checks do not cause multiple evaluation.
6211
6212Expression*
6213Bound_method_expression::do_lower(Gogo*, Named_object*,
6214 Statement_inserter* inserter, int)
6215{
6216 // For simplicity we use a temporary for every call to an embedded
6217 // method, even though some of them might be pure value methods and
6218 // not require a temporary.
6219 if (this->expr_->var_expression() == NULL
6220 && this->expr_->temporary_reference_expression() == NULL
6221 && this->expr_->set_and_use_temporary_expression() == NULL
6222 && (this->method_->field_indexes() != NULL
6223 || (this->method_->is_value_method()
6224 && this->expr_->type()->points_to() != NULL)))
6225 {
6226 Temporary_statement* temp =
6227 Statement::make_temporary(this->expr_->type(), NULL, this->location());
6228 inserter->insert(temp);
6229 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_,
6230 this->location());
6231 }
6232 return this;
6233}
6234
e440a328 6235// Return the type of a bound method expression. The type of this
0afbb937 6236// object is simply the type of the method with no receiver.
e440a328 6237
6238Type*
6239Bound_method_expression::do_type()
6240{
0afbb937 6241 Named_object* fn = this->method_->named_object();
6242 Function_type* fntype;
6243 if (fn->is_function())
6244 fntype = fn->func_value()->type();
6245 else if (fn->is_function_declaration())
6246 fntype = fn->func_declaration_value()->type();
e0659c9e 6247 else
6248 return Type::make_error_type();
0afbb937 6249 return fntype->copy_without_receiver();
e440a328 6250}
6251
6252// Determine the types of a method expression.
6253
6254void
6255Bound_method_expression::do_determine_type(const Type_context*)
6256{
0afbb937 6257 Named_object* fn = this->method_->named_object();
6258 Function_type* fntype;
6259 if (fn->is_function())
6260 fntype = fn->func_value()->type();
6261 else if (fn->is_function_declaration())
6262 fntype = fn->func_declaration_value()->type();
6263 else
6264 fntype = NULL;
e440a328 6265 if (fntype == NULL || !fntype->is_method())
6266 this->expr_->determine_type_no_context();
6267 else
6268 {
6269 Type_context subcontext(fntype->receiver()->type(), false);
6270 this->expr_->determine_type(&subcontext);
6271 }
6272}
6273
6274// Check the types of a method expression.
6275
6276void
6277Bound_method_expression::do_check_types(Gogo*)
6278{
0afbb937 6279 Named_object* fn = this->method_->named_object();
6280 if (!fn->is_function() && !fn->is_function_declaration())
6281 {
6282 this->report_error(_("object is not a method"));
6283 return;
6284 }
6285
6286 Function_type* fntype;
6287 if (fn->is_function())
6288 fntype = fn->func_value()->type();
6289 else if (fn->is_function_declaration())
6290 fntype = fn->func_declaration_value()->type();
e440a328 6291 else
0afbb937 6292 go_unreachable();
6293 Type* rtype = fntype->receiver()->type()->deref();
6294 Type* etype = (this->expr_type_ != NULL
6295 ? this->expr_type_
6296 : this->expr_->type());
6297 etype = etype->deref();
6298 if (!Type::are_identical(rtype, etype, true, NULL))
6299 this->report_error(_("method type does not match object type"));
6300}
6301
6302// If a bound method expression is not simply called, then it is
6303// represented as a closure. The closure will hold a single variable,
6304// the receiver to pass to the method. The function will be a simple
6305// thunk that pulls that value from the closure and calls the method
6306// with the remaining arguments.
6307//
6308// Because method values are not common, we don't build all thunks for
6309// every methods, but instead only build them as we need them. In
6310// particular, we even build them on demand for methods defined in
6311// other packages.
6312
6313Bound_method_expression::Method_value_thunks
6314 Bound_method_expression::method_value_thunks;
6315
6316// Find or create the thunk for METHOD.
6317
6318Named_object*
6319Bound_method_expression::create_thunk(Gogo* gogo, const Method* method,
6320 Named_object* fn)
6321{
6322 std::pair<Named_object*, Named_object*> val(fn, NULL);
6323 std::pair<Method_value_thunks::iterator, bool> ins =
6324 Bound_method_expression::method_value_thunks.insert(val);
6325 if (!ins.second)
6326 {
6327 // We have seen this method before.
6328 go_assert(ins.first->second != NULL);
6329 return ins.first->second;
6330 }
6331
6332 Location loc = fn->location();
6333
6334 Function_type* orig_fntype;
6335 if (fn->is_function())
6336 orig_fntype = fn->func_value()->type();
6337 else if (fn->is_function_declaration())
6338 orig_fntype = fn->func_declaration_value()->type();
6339 else
6340 orig_fntype = NULL;
6341
6342 if (orig_fntype == NULL || !orig_fntype->is_method())
e440a328 6343 {
0afbb937 6344 ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name());
6345 return ins.first->second;
e440a328 6346 }
0afbb937 6347
6348 Struct_field_list* sfl = new Struct_field_list();
f8bdf81a 6349 // The type here is wrong--it should be the C function type. But it
6350 // doesn't really matter.
0afbb937 6351 Type* vt = Type::make_pointer_type(Type::make_void_type());
6352 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
6353 sfl->push_back(Struct_field(Typed_identifier("val.1",
6354 orig_fntype->receiver()->type(),
6355 loc)));
6356 Type* closure_type = Type::make_struct_type(sfl, loc);
6357 closure_type = Type::make_pointer_type(closure_type);
6358
f8bdf81a 6359 Function_type* new_fntype = orig_fntype->copy_with_names();
0afbb937 6360
da244e59 6361 std::string thunk_name = Gogo::thunk_name();
6362 Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
0afbb937 6363 false, loc);
6364
f8bdf81a 6365 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
6366 cvar->set_is_used();
1ecc6157 6367 cvar->set_is_closure();
da244e59 6368 Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
6369 NULL, cvar);
f8bdf81a 6370 new_no->func_value()->set_closure_var(cp);
0afbb937 6371
f8bdf81a 6372 gogo->start_block(loc);
0afbb937 6373
6374 // Field 0 of the closure is the function code pointer, field 1 is
6375 // the value on which to invoke the method.
6376 Expression* arg = Expression::make_var_reference(cp, loc);
6377 arg = Expression::make_unary(OPERATOR_MULT, arg, loc);
6378 arg = Expression::make_field_reference(arg, 1, loc);
6379
6380 Expression* bme = Expression::make_bound_method(arg, method, fn, loc);
6381
6382 const Typed_identifier_list* orig_params = orig_fntype->parameters();
6383 Expression_list* args;
6384 if (orig_params == NULL || orig_params->empty())
6385 args = NULL;
6386 else
6387 {
6388 const Typed_identifier_list* new_params = new_fntype->parameters();
6389 args = new Expression_list();
6390 for (Typed_identifier_list::const_iterator p = new_params->begin();
f8bdf81a 6391 p != new_params->end();
0afbb937 6392 ++p)
6393 {
6394 Named_object* p_no = gogo->lookup(p->name(), NULL);
6395 go_assert(p_no != NULL
6396 && p_no->is_variable()
6397 && p_no->var_value()->is_parameter());
6398 args->push_back(Expression::make_var_reference(p_no, loc));
6399 }
6400 }
6401
6402 Call_expression* call = Expression::make_call(bme, args,
6403 orig_fntype->is_varargs(),
6404 loc);
6405 call->set_varargs_are_lowered();
6406
6407 Statement* s = Statement::make_return_from_call(call, loc);
6408 gogo->add_statement(s);
6409 Block* b = gogo->finish_block(loc);
6410 gogo->add_block(b, loc);
6411 gogo->lower_block(new_no, b);
a32698ee 6412 gogo->flatten_block(new_no, b);
0afbb937 6413 gogo->finish_function(loc);
6414
6415 ins.first->second = new_no;
6416 return new_no;
6417}
6418
6419// Return an expression to check *REF for nil while dereferencing
6420// according to FIELD_INDEXES. Update *REF to build up the field
6421// reference. This is a static function so that we don't have to
6422// worry about declaring Field_indexes in expressions.h.
6423
6424static Expression*
6425bme_check_nil(const Method::Field_indexes* field_indexes, Location loc,
6426 Expression** ref)
6427{
6428 if (field_indexes == NULL)
6429 return Expression::make_boolean(false, loc);
6430 Expression* cond = bme_check_nil(field_indexes->next, loc, ref);
6431 Struct_type* stype = (*ref)->type()->deref()->struct_type();
6432 go_assert(stype != NULL
6433 && field_indexes->field_index < stype->field_count());
6434 if ((*ref)->type()->struct_type() == NULL)
6435 {
6436 go_assert((*ref)->type()->points_to() != NULL);
6437 Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref,
6438 Expression::make_nil(loc),
6439 loc);
6440 cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc);
6441 *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc);
6442 go_assert((*ref)->type()->struct_type() == stype);
6443 }
6444 *ref = Expression::make_field_reference(*ref, field_indexes->field_index,
6445 loc);
6446 return cond;
e440a328 6447}
6448
ea664253 6449// Get the backend representation for a method value.
e440a328 6450
ea664253 6451Bexpression*
6452Bound_method_expression::do_get_backend(Translate_context* context)
e440a328 6453{
0afbb937 6454 Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(),
6455 this->method_,
6456 this->function_);
6457 if (thunk->is_erroneous())
6458 {
6459 go_assert(saw_errors());
ea664253 6460 return context->backend()->error_expression();
0afbb937 6461 }
6462
6463 // FIXME: We should lower this earlier, but we can't lower it in the
6464 // lowering pass because at that point we don't know whether we need
6465 // to create the thunk or not. If the expression is called, we
6466 // don't need the thunk.
6467
6468 Location loc = this->location();
6469
6470 // If the method expects a value, and we have a pointer, we need to
6471 // dereference the pointer.
6472
6473 Named_object* fn = this->method_->named_object();
6474 Function_type* fntype;
6475 if (fn->is_function())
6476 fntype = fn->func_value()->type();
6477 else if (fn->is_function_declaration())
6478 fntype = fn->func_declaration_value()->type();
6479 else
6480 go_unreachable();
6481
6482 Expression* val = this->expr_;
6483 if (fntype->receiver()->type()->points_to() == NULL
6484 && val->type()->points_to() != NULL)
6485 val = Expression::make_unary(OPERATOR_MULT, val, loc);
6486
6487 // Note that we are ignoring this->expr_type_ here. The thunk will
6488 // expect a closure whose second field has type this->expr_type_ (if
6489 // that is not NULL). We are going to pass it a closure whose
6490 // second field has type this->expr_->type(). Since
6491 // this->expr_type_ is only not-NULL for pointer types, we can get
6492 // away with this.
6493
6494 Struct_field_list* fields = new Struct_field_list();
6495 fields->push_back(Struct_field(Typed_identifier("fn.0",
6496 thunk->func_value()->type(),
6497 loc)));
6498 fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc)));
6499 Struct_type* st = Type::make_struct_type(fields, loc);
6500
6501 Expression_list* vals = new Expression_list();
6502 vals->push_back(Expression::make_func_code_reference(thunk, loc));
6503 vals->push_back(val);
6504
6505 Expression* ret = Expression::make_struct_composite_literal(st, vals, loc);
2c809f8f 6506 ret = Expression::make_heap_expression(ret, loc);
0afbb937 6507
0afbb937 6508 // See whether the expression or any embedded pointers are nil.
6509
df7ef1fd 6510 Expression* nil_check = NULL;
0afbb937 6511 Expression* expr = this->expr_;
6512 if (this->method_->field_indexes() != NULL)
6513 {
6514 // Note that we are evaluating this->expr_ twice, but that is OK
6515 // because in the lowering pass we forced it into a temporary
6516 // variable.
6517 Expression* ref = expr;
6518 nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref);
6519 expr = ref;
6520 }
6521
6522 if (this->method_->is_value_method() && expr->type()->points_to() != NULL)
6523 {
6524 Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr,
6525 Expression::make_nil(loc),
6526 loc);
6527 if (nil_check == NULL)
6528 nil_check = n;
6529 else
6530 nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc);
6531 }
6532
ea664253 6533 Bexpression* bme = ret->get_backend(context);
0afbb937 6534 if (nil_check != NULL)
6535 {
df7ef1fd 6536 Gogo* gogo = context->gogo();
ea664253 6537 Bexpression* crash =
6538 gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
6539 loc)->get_backend(context);
df7ef1fd 6540 Btype* btype = ret->type()->get_backend(gogo);
ea664253 6541 Bexpression* bcheck = nil_check->get_backend(context);
6542 bme = gogo->backend()->conditional_expression(btype, bcheck, crash,
df7ef1fd 6543 bme, loc);
6544 }
ea664253 6545 return bme;
e440a328 6546}
6547
d751bb78 6548// Dump ast representation of a bound method expression.
6549
6550void
6551Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
6552 const
6553{
6554 if (this->expr_type_ != NULL)
6555 ast_dump_context->ostream() << "(";
6556 ast_dump_context->dump_expression(this->expr_);
6557 if (this->expr_type_ != NULL)
6558 {
6559 ast_dump_context->ostream() << ":";
6560 ast_dump_context->dump_type(this->expr_type_);
6561 ast_dump_context->ostream() << ")";
6562 }
6563
0afbb937 6564 ast_dump_context->ostream() << "." << this->function_->name();
d751bb78 6565}
6566
e440a328 6567// Make a method expression.
6568
6569Bound_method_expression*
0afbb937 6570Expression::make_bound_method(Expression* expr, const Method* method,
6571 Named_object* function, Location location)
e440a328 6572{
0afbb937 6573 return new Bound_method_expression(expr, method, function, location);
e440a328 6574}
6575
6576// Class Builtin_call_expression. This is used for a call to a
6577// builtin function.
6578
6579class Builtin_call_expression : public Call_expression
6580{
6581 public:
6582 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
b13c66cd 6583 bool is_varargs, Location location);
e440a328 6584
6585 protected:
6586 // This overrides Call_expression::do_lower.
6587 Expression*
ceeb4318 6588 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 6589
35a54f17 6590 Expression*
6591 do_flatten(Gogo*, Named_object*, Statement_inserter*);
6592
e440a328 6593 bool
6594 do_is_constant() const;
6595
6596 bool
0c77715b 6597 do_numeric_constant_value(Numeric_constant*) const;
e440a328 6598
4f2138d7 6599 bool
a7549a6a 6600 do_discarding_value();
6601
e440a328 6602 Type*
6603 do_type();
6604
6605 void
6606 do_determine_type(const Type_context*);
6607
6608 void
6609 do_check_types(Gogo*);
6610
6611 Expression*
72666aed 6612 do_copy();
e440a328 6613
ea664253 6614 Bexpression*
6615 do_get_backend(Translate_context*);
e440a328 6616
6617 void
6618 do_export(Export*) const;
6619
6620 virtual bool
6621 do_is_recover_call() const;
6622
6623 virtual void
6624 do_set_recover_arg(Expression*);
6625
6626 private:
6627 // The builtin functions.
6628 enum Builtin_function_code
6629 {
6630 BUILTIN_INVALID,
6631
6632 // Predeclared builtin functions.
6633 BUILTIN_APPEND,
6634 BUILTIN_CAP,
6635 BUILTIN_CLOSE,
48080209 6636 BUILTIN_COMPLEX,
e440a328 6637 BUILTIN_COPY,
1cce762f 6638 BUILTIN_DELETE,
e440a328 6639 BUILTIN_IMAG,
6640 BUILTIN_LEN,
6641 BUILTIN_MAKE,
6642 BUILTIN_NEW,
6643 BUILTIN_PANIC,
6644 BUILTIN_PRINT,
6645 BUILTIN_PRINTLN,
6646 BUILTIN_REAL,
6647 BUILTIN_RECOVER,
6648
6649 // Builtin functions from the unsafe package.
6650 BUILTIN_ALIGNOF,
6651 BUILTIN_OFFSETOF,
6652 BUILTIN_SIZEOF
6653 };
6654
6655 Expression*
6656 one_arg() const;
6657
6658 bool
6659 check_one_arg();
6660
6661 static Type*
6662 real_imag_type(Type*);
6663
6664 static Type*
48080209 6665 complex_type(Type*);
e440a328 6666
a9182619 6667 Expression*
6668 lower_make();
6669
6670 bool
1ad00fd4 6671 check_int_value(Expression*, bool is_length);
a9182619 6672
e440a328 6673 // A pointer back to the general IR structure. This avoids a global
6674 // variable, or passing it around everywhere.
6675 Gogo* gogo_;
6676 // The builtin function being called.
6677 Builtin_function_code code_;
0f914071 6678 // Used to stop endless loops when the length of an array uses len
6679 // or cap of the array itself.
6680 mutable bool seen_;
6334270b 6681 // Whether the argument is set for calls to BUILTIN_RECOVER.
6682 bool recover_arg_is_set_;
e440a328 6683};
6684
6685Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6686 Expression* fn,
6687 Expression_list* args,
6688 bool is_varargs,
b13c66cd 6689 Location location)
e440a328 6690 : Call_expression(fn, args, is_varargs, location),
6334270b 6691 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false),
6692 recover_arg_is_set_(false)
e440a328 6693{
6694 Func_expression* fnexp = this->fn()->func_expression();
79651b1f 6695 if (fnexp == NULL)
6696 {
6697 this->code_ = BUILTIN_INVALID;
6698 return;
6699 }
e440a328 6700 const std::string& name(fnexp->named_object()->name());
6701 if (name == "append")
6702 this->code_ = BUILTIN_APPEND;
6703 else if (name == "cap")
6704 this->code_ = BUILTIN_CAP;
6705 else if (name == "close")
6706 this->code_ = BUILTIN_CLOSE;
48080209 6707 else if (name == "complex")
6708 this->code_ = BUILTIN_COMPLEX;
e440a328 6709 else if (name == "copy")
6710 this->code_ = BUILTIN_COPY;
1cce762f 6711 else if (name == "delete")
6712 this->code_ = BUILTIN_DELETE;
e440a328 6713 else if (name == "imag")
6714 this->code_ = BUILTIN_IMAG;
6715 else if (name == "len")
6716 this->code_ = BUILTIN_LEN;
6717 else if (name == "make")
6718 this->code_ = BUILTIN_MAKE;
6719 else if (name == "new")
6720 this->code_ = BUILTIN_NEW;
6721 else if (name == "panic")
6722 this->code_ = BUILTIN_PANIC;
6723 else if (name == "print")
6724 this->code_ = BUILTIN_PRINT;
6725 else if (name == "println")
6726 this->code_ = BUILTIN_PRINTLN;
6727 else if (name == "real")
6728 this->code_ = BUILTIN_REAL;
6729 else if (name == "recover")
6730 this->code_ = BUILTIN_RECOVER;
6731 else if (name == "Alignof")
6732 this->code_ = BUILTIN_ALIGNOF;
6733 else if (name == "Offsetof")
6734 this->code_ = BUILTIN_OFFSETOF;
6735 else if (name == "Sizeof")
6736 this->code_ = BUILTIN_SIZEOF;
6737 else
c3e6f413 6738 go_unreachable();
e440a328 6739}
6740
6741// Return whether this is a call to recover. This is a virtual
6742// function called from the parent class.
6743
6744bool
6745Builtin_call_expression::do_is_recover_call() const
6746{
6747 if (this->classification() == EXPRESSION_ERROR)
6748 return false;
6749 return this->code_ == BUILTIN_RECOVER;
6750}
6751
6752// Set the argument for a call to recover.
6753
6754void
6755Builtin_call_expression::do_set_recover_arg(Expression* arg)
6756{
6757 const Expression_list* args = this->args();
c484d925 6758 go_assert(args == NULL || args->empty());
e440a328 6759 Expression_list* new_args = new Expression_list();
6760 new_args->push_back(arg);
6761 this->set_args(new_args);
6334270b 6762 this->recover_arg_is_set_ = true;
e440a328 6763}
6764
e440a328 6765// Lower a builtin call expression. This turns new and make into
6766// specific expressions. We also convert to a constant if we can.
6767
6768Expression*
ceeb4318 6769Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
6770 Statement_inserter* inserter, int)
e440a328 6771{
79651b1f 6772 if (this->is_error_expression())
a9182619 6773 return this;
6774
b13c66cd 6775 Location loc = this->location();
1cce762f 6776
a8725655 6777 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6778 {
6779 this->report_error(_("invalid use of %<...%> with builtin function"));
1cce762f 6780 return Expression::make_error(loc);
a8725655 6781 }
6782
393ba00b 6783 if (this->code_ == BUILTIN_OFFSETOF)
6784 {
6785 Expression* arg = this->one_arg();
12e69faa 6786
6787 if (arg->bound_method_expression() != NULL
6788 || arg->interface_field_reference_expression() != NULL)
6789 {
6790 this->report_error(_("invalid use of method value as argument "
6791 "of Offsetof"));
6792 return this;
6793 }
6794
393ba00b 6795 Field_reference_expression* farg = arg->field_reference_expression();
6796 while (farg != NULL)
6797 {
6798 if (!farg->implicit())
6799 break;
6800 // When the selector refers to an embedded field,
6801 // it must not be reached through pointer indirections.
6802 if (farg->expr()->deref() != farg->expr())
6803 {
12e69faa 6804 this->report_error(_("argument of Offsetof implies "
6805 "indirection of an embedded field"));
393ba00b 6806 return this;
6807 }
6808 // Go up until we reach the original base.
6809 farg = farg->expr()->field_reference_expression();
6810 }
6811 }
6812
1cce762f 6813 if (this->is_constant())
e440a328 6814 {
0c77715b 6815 Numeric_constant nc;
6816 if (this->numeric_constant_value(&nc))
6817 return nc.expression(loc);
e440a328 6818 }
1cce762f 6819
6820 switch (this->code_)
e440a328 6821 {
1cce762f 6822 default:
6823 break;
6824
6825 case BUILTIN_NEW:
6826 {
6827 const Expression_list* args = this->args();
6828 if (args == NULL || args->size() < 1)
6829 this->report_error(_("not enough arguments"));
6830 else if (args->size() > 1)
6831 this->report_error(_("too many arguments"));
6832 else
6833 {
6834 Expression* arg = args->front();
6835 if (!arg->is_type_expression())
6836 {
6837 error_at(arg->location(), "expected type");
6838 this->set_is_error();
6839 }
6840 else
6841 return Expression::make_allocation(arg->type(), loc);
6842 }
6843 }
6844 break;
6845
6846 case BUILTIN_MAKE:
6847 return this->lower_make();
6848
6849 case BUILTIN_RECOVER:
e440a328 6850 if (function != NULL)
6851 function->func_value()->set_calls_recover();
6852 else
6853 {
6854 // Calling recover outside of a function always returns the
6855 // nil empty interface.
823c7e3d 6856 Type* eface = Type::make_empty_interface_type(loc);
1cce762f 6857 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
e440a328 6858 }
1cce762f 6859 break;
6860
6861 case BUILTIN_APPEND:
6862 {
6863 // Lower the varargs.
6864 const Expression_list* args = this->args();
6865 if (args == NULL || args->empty())
e440a328 6866 return this;
1cce762f 6867 Type* slice_type = args->front()->type();
6868 if (!slice_type->is_slice_type())
6869 {
3ff4863b 6870 if (slice_type->is_nil_type())
6871 error_at(args->front()->location(), "use of untyped nil");
6872 else
6873 error_at(args->front()->location(),
6874 "argument 1 must be a slice");
1cce762f 6875 this->set_is_error();
6876 return this;
6877 }
19fd40c3 6878 Type* element_type = slice_type->array_type()->element_type();
6879 this->lower_varargs(gogo, function, inserter,
6880 Type::make_array_type(element_type, NULL),
6881 2);
1cce762f 6882 }
6883 break;
6884
6885 case BUILTIN_DELETE:
6886 {
6887 // Lower to a runtime function call.
6888 const Expression_list* args = this->args();
6889 if (args == NULL || args->size() < 2)
6890 this->report_error(_("not enough arguments"));
6891 else if (args->size() > 2)
6892 this->report_error(_("too many arguments"));
6893 else if (args->front()->type()->map_type() == NULL)
6894 this->report_error(_("argument 1 must be a map"));
6895 else
6896 {
6897 // Since this function returns no value it must appear in
6898 // a statement by itself, so we don't have to worry about
6899 // order of evaluation of values around it. Evaluate the
6900 // map first to get order of evaluation right.
6901 Map_type* mt = args->front()->type()->map_type();
6902 Temporary_statement* map_temp =
6903 Statement::make_temporary(mt, args->front(), loc);
6904 inserter->insert(map_temp);
6905
6906 Temporary_statement* key_temp =
6907 Statement::make_temporary(mt->key_type(), args->back(), loc);
6908 inserter->insert(key_temp);
6909
6910 Expression* e1 = Expression::make_temporary_reference(map_temp,
6911 loc);
6912 Expression* e2 = Expression::make_temporary_reference(key_temp,
6913 loc);
6914 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
6915 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
6916 2, e1, e2);
6917 }
6918 }
6919 break;
e440a328 6920 }
6921
6922 return this;
6923}
6924
35a54f17 6925// Flatten a builtin call expression. This turns the arguments of copy and
6926// append into temporary expressions.
6927
6928Expression*
6929Builtin_call_expression::do_flatten(Gogo*, Named_object*,
6930 Statement_inserter* inserter)
6931{
16cb7fec 6932 Location loc = this->location();
6933
6934 switch (this->code_)
35a54f17 6935 {
16cb7fec 6936 default:
6937 break;
6938
6939 case BUILTIN_APPEND:
6940 case BUILTIN_COPY:
6941 {
6942 Type* at = this->args()->front()->type();
6943 for (Expression_list::iterator pa = this->args()->begin();
6944 pa != this->args()->end();
6945 ++pa)
6946 {
6947 if ((*pa)->is_nil_expression())
6948 {
6949 Expression* nil = Expression::make_nil(loc);
6950 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
6951 *pa = Expression::make_slice_value(at, nil, zero, zero, loc);
6952 }
6953 if (!(*pa)->is_variable())
6954 {
6955 Temporary_statement* temp =
6956 Statement::make_temporary(NULL, *pa, loc);
6957 inserter->insert(temp);
6958 *pa = Expression::make_temporary_reference(temp, loc);
6959 }
6960 }
6961 }
6962 break;
6963
6964 case BUILTIN_PANIC:
35a54f17 6965 for (Expression_list::iterator pa = this->args()->begin();
16cb7fec 6966 pa != this->args()->end();
6967 ++pa)
6968 {
6969 if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL)
55e8ba6a 6970 {
16cb7fec 6971 Temporary_statement* temp =
6972 Statement::make_temporary(NULL, *pa, loc);
6973 inserter->insert(temp);
6974 *pa = Expression::make_temporary_reference(temp, loc);
55e8ba6a 6975 }
16cb7fec 6976 }
35a54f17 6977 }
16cb7fec 6978
35a54f17 6979 return this;
6980}
6981
a9182619 6982// Lower a make expression.
6983
6984Expression*
6985Builtin_call_expression::lower_make()
6986{
b13c66cd 6987 Location loc = this->location();
a9182619 6988
6989 const Expression_list* args = this->args();
6990 if (args == NULL || args->size() < 1)
6991 {
6992 this->report_error(_("not enough arguments"));
6993 return Expression::make_error(this->location());
6994 }
6995
6996 Expression_list::const_iterator parg = args->begin();
6997
6998 Expression* first_arg = *parg;
6999 if (!first_arg->is_type_expression())
7000 {
7001 error_at(first_arg->location(), "expected type");
7002 this->set_is_error();
7003 return Expression::make_error(this->location());
7004 }
7005 Type* type = first_arg->type();
7006
7007 bool is_slice = false;
7008 bool is_map = false;
7009 bool is_chan = false;
411eb89e 7010 if (type->is_slice_type())
a9182619 7011 is_slice = true;
7012 else if (type->map_type() != NULL)
7013 is_map = true;
7014 else if (type->channel_type() != NULL)
7015 is_chan = true;
7016 else
7017 {
7018 this->report_error(_("invalid type for make function"));
7019 return Expression::make_error(this->location());
7020 }
7021
ac84c822 7022 bool have_big_args = false;
7023 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7024 int uintptr_bits = uintptr_type->integer_type()->bits();
7025
f6bc81e6 7026 Type_context int_context(Type::lookup_integer_type("int"), false);
7027
a9182619 7028 ++parg;
7029 Expression* len_arg;
7030 if (parg == args->end())
7031 {
7032 if (is_slice)
7033 {
7034 this->report_error(_("length required when allocating a slice"));
7035 return Expression::make_error(this->location());
7036 }
e67508fa 7037 len_arg = Expression::make_integer_ul(0, NULL, loc);
a9182619 7038 }
7039 else
7040 {
7041 len_arg = *parg;
f6bc81e6 7042 len_arg->determine_type(&int_context);
1ad00fd4 7043 if (!this->check_int_value(len_arg, true))
7044 return Expression::make_error(this->location());
ac84c822 7045 if (len_arg->type()->integer_type() != NULL
7046 && len_arg->type()->integer_type()->bits() > uintptr_bits)
7047 have_big_args = true;
a9182619 7048 ++parg;
7049 }
7050
7051 Expression* cap_arg = NULL;
7052 if (is_slice && parg != args->end())
7053 {
7054 cap_arg = *parg;
f6bc81e6 7055 cap_arg->determine_type(&int_context);
1ad00fd4 7056 if (!this->check_int_value(cap_arg, false))
7057 return Expression::make_error(this->location());
7058
7059 Numeric_constant nclen;
7060 Numeric_constant nccap;
7061 unsigned long vlen;
7062 unsigned long vcap;
7063 if (len_arg->numeric_constant_value(&nclen)
7064 && cap_arg->numeric_constant_value(&nccap)
7065 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
7066 && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID
7067 && vlen > vcap)
a9182619 7068 {
1ad00fd4 7069 this->report_error(_("len larger than cap"));
a9182619 7070 return Expression::make_error(this->location());
7071 }
1ad00fd4 7072
ac84c822 7073 if (cap_arg->type()->integer_type() != NULL
7074 && cap_arg->type()->integer_type()->bits() > uintptr_bits)
7075 have_big_args = true;
a9182619 7076 ++parg;
7077 }
7078
7079 if (parg != args->end())
7080 {
7081 this->report_error(_("too many arguments to make"));
7082 return Expression::make_error(this->location());
7083 }
7084
b13c66cd 7085 Location type_loc = first_arg->location();
a9182619 7086 Expression* type_arg;
7087 if (is_slice || is_chan)
7088 type_arg = Expression::make_type_descriptor(type, type_loc);
7089 else if (is_map)
7090 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7091 else
7092 go_unreachable();
7093
7094 Expression* call;
7095 if (is_slice)
7096 {
7097 if (cap_arg == NULL)
ac84c822 7098 call = Runtime::make_call((have_big_args
7099 ? Runtime::MAKESLICE1BIG
7100 : Runtime::MAKESLICE1),
7101 loc, 2, type_arg, len_arg);
a9182619 7102 else
ac84c822 7103 call = Runtime::make_call((have_big_args
7104 ? Runtime::MAKESLICE2BIG
7105 : Runtime::MAKESLICE2),
7106 loc, 3, type_arg, len_arg, cap_arg);
a9182619 7107 }
7108 else if (is_map)
ac84c822 7109 call = Runtime::make_call((have_big_args
7110 ? Runtime::MAKEMAPBIG
7111 : Runtime::MAKEMAP),
7112 loc, 2, type_arg, len_arg);
a9182619 7113 else if (is_chan)
ac84c822 7114 call = Runtime::make_call((have_big_args
7115 ? Runtime::MAKECHANBIG
7116 : Runtime::MAKECHAN),
7117 loc, 2, type_arg, len_arg);
a9182619 7118 else
7119 go_unreachable();
7120
7121 return Expression::make_unsafe_cast(type, call, loc);
7122}
7123
7124// Return whether an expression has an integer value. Report an error
7125// if not. This is used when handling calls to the predeclared make
7126// function.
7127
7128bool
1ad00fd4 7129Builtin_call_expression::check_int_value(Expression* e, bool is_length)
a9182619 7130{
0c77715b 7131 Numeric_constant nc;
1ad00fd4 7132 if (e->numeric_constant_value(&nc))
a9182619 7133 {
1ad00fd4 7134 unsigned long v;
7135 switch (nc.to_unsigned_long(&v))
7136 {
7137 case Numeric_constant::NC_UL_VALID:
1b10c5e7 7138 break;
1ad00fd4 7139 case Numeric_constant::NC_UL_NOTINT:
7140 error_at(e->location(), "non-integer %s argument to make",
7141 is_length ? "len" : "cap");
7142 return false;
7143 case Numeric_constant::NC_UL_NEGATIVE:
7144 error_at(e->location(), "negative %s argument to make",
7145 is_length ? "len" : "cap");
7146 return false;
7147 case Numeric_constant::NC_UL_BIG:
7148 // We don't want to give a compile-time error for a 64-bit
7149 // value on a 32-bit target.
1b10c5e7 7150 break;
1ad00fd4 7151 }
1b10c5e7 7152
7153 mpz_t val;
7154 if (!nc.to_int(&val))
7155 go_unreachable();
7156 int bits = mpz_sizeinbase(val, 2);
7157 mpz_clear(val);
7158 Type* int_type = Type::lookup_integer_type("int");
7159 if (bits >= int_type->integer_type()->bits())
7160 {
7161 error_at(e->location(), "%s argument too large for make",
7162 is_length ? "len" : "cap");
7163 return false;
7164 }
7165
7166 return true;
a9182619 7167 }
7168
1ad00fd4 7169 if (e->type()->integer_type() != NULL)
7170 return true;
7171
7172 error_at(e->location(), "non-integer %s argument to make",
7173 is_length ? "len" : "cap");
a9182619 7174 return false;
7175}
7176
e440a328 7177// Return the type of the real or imag functions, given the type of
fcbea5e4 7178// the argument. We need to map complex64 to float32 and complex128
7179// to float64, so it has to be done by name. This returns NULL if it
7180// can't figure out the type.
e440a328 7181
7182Type*
7183Builtin_call_expression::real_imag_type(Type* arg_type)
7184{
7185 if (arg_type == NULL || arg_type->is_abstract())
7186 return NULL;
7187 Named_type* nt = arg_type->named_type();
7188 if (nt == NULL)
7189 return NULL;
7190 while (nt->real_type()->named_type() != NULL)
7191 nt = nt->real_type()->named_type();
48080209 7192 if (nt->name() == "complex64")
e440a328 7193 return Type::lookup_float_type("float32");
7194 else if (nt->name() == "complex128")
7195 return Type::lookup_float_type("float64");
7196 else
7197 return NULL;
7198}
7199
48080209 7200// Return the type of the complex function, given the type of one of the
e440a328 7201// argments. Like real_imag_type, we have to map by name.
7202
7203Type*
48080209 7204Builtin_call_expression::complex_type(Type* arg_type)
e440a328 7205{
7206 if (arg_type == NULL || arg_type->is_abstract())
7207 return NULL;
7208 Named_type* nt = arg_type->named_type();
7209 if (nt == NULL)
7210 return NULL;
7211 while (nt->real_type()->named_type() != NULL)
7212 nt = nt->real_type()->named_type();
48080209 7213 if (nt->name() == "float32")
e440a328 7214 return Type::lookup_complex_type("complex64");
7215 else if (nt->name() == "float64")
7216 return Type::lookup_complex_type("complex128");
7217 else
7218 return NULL;
7219}
7220
7221// Return a single argument, or NULL if there isn't one.
7222
7223Expression*
7224Builtin_call_expression::one_arg() const
7225{
7226 const Expression_list* args = this->args();
aa615cb3 7227 if (args == NULL || args->size() != 1)
e440a328 7228 return NULL;
7229 return args->front();
7230}
7231
83921647 7232// A traversal class which looks for a call or receive expression.
7233
7234class Find_call_expression : public Traverse
7235{
7236 public:
7237 Find_call_expression()
7238 : Traverse(traverse_expressions),
7239 found_(false)
7240 { }
7241
7242 int
7243 expression(Expression**);
7244
7245 bool
7246 found()
7247 { return this->found_; }
7248
7249 private:
7250 bool found_;
7251};
7252
7253int
7254Find_call_expression::expression(Expression** pexpr)
7255{
7256 if ((*pexpr)->call_expression() != NULL
7257 || (*pexpr)->receive_expression() != NULL)
7258 {
7259 this->found_ = true;
7260 return TRAVERSE_EXIT;
7261 }
7262 return TRAVERSE_CONTINUE;
7263}
7264
7265// Return whether this is constant: len of a string constant, or len
7266// or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7267// unsafe.Alignof.
e440a328 7268
7269bool
7270Builtin_call_expression::do_is_constant() const
7271{
12e69faa 7272 if (this->is_error_expression())
7273 return true;
e440a328 7274 switch (this->code_)
7275 {
7276 case BUILTIN_LEN:
7277 case BUILTIN_CAP:
7278 {
0f914071 7279 if (this->seen_)
7280 return false;
7281
e440a328 7282 Expression* arg = this->one_arg();
7283 if (arg == NULL)
7284 return false;
7285 Type* arg_type = arg->type();
7286
7287 if (arg_type->points_to() != NULL
7288 && arg_type->points_to()->array_type() != NULL
411eb89e 7289 && !arg_type->points_to()->is_slice_type())
e440a328 7290 arg_type = arg_type->points_to();
7291
83921647 7292 // The len and cap functions are only constant if there are no
7293 // function calls or channel operations in the arguments.
7294 // Otherwise we have to make the call.
7295 if (!arg->is_constant())
7296 {
7297 Find_call_expression find_call;
7298 Expression::traverse(&arg, &find_call);
7299 if (find_call.found())
7300 return false;
7301 }
7302
e440a328 7303 if (arg_type->array_type() != NULL
7304 && arg_type->array_type()->length() != NULL)
0f914071 7305 return true;
e440a328 7306
7307 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
0f914071 7308 {
7309 this->seen_ = true;
7310 bool ret = arg->is_constant();
7311 this->seen_ = false;
7312 return ret;
7313 }
e440a328 7314 }
7315 break;
7316
7317 case BUILTIN_SIZEOF:
7318 case BUILTIN_ALIGNOF:
7319 return this->one_arg() != NULL;
7320
7321 case BUILTIN_OFFSETOF:
7322 {
7323 Expression* arg = this->one_arg();
7324 if (arg == NULL)
7325 return false;
7326 return arg->field_reference_expression() != NULL;
7327 }
7328
48080209 7329 case BUILTIN_COMPLEX:
e440a328 7330 {
7331 const Expression_list* args = this->args();
7332 if (args != NULL && args->size() == 2)
7333 return args->front()->is_constant() && args->back()->is_constant();
7334 }
7335 break;
7336
7337 case BUILTIN_REAL:
7338 case BUILTIN_IMAG:
7339 {
7340 Expression* arg = this->one_arg();
7341 return arg != NULL && arg->is_constant();
7342 }
7343
7344 default:
7345 break;
7346 }
7347
7348 return false;
7349}
7350
0c77715b 7351// Return a numeric constant if possible.
e440a328 7352
7353bool
0c77715b 7354Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const
e440a328 7355{
7356 if (this->code_ == BUILTIN_LEN
7357 || this->code_ == BUILTIN_CAP)
7358 {
7359 Expression* arg = this->one_arg();
7360 if (arg == NULL)
7361 return false;
7362 Type* arg_type = arg->type();
7363
7364 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7365 {
7366 std::string sval;
7367 if (arg->string_constant_value(&sval))
7368 {
0c77715b 7369 nc->set_unsigned_long(Type::lookup_integer_type("int"),
7370 sval.length());
e440a328 7371 return true;
7372 }
7373 }
7374
7375 if (arg_type->points_to() != NULL
7376 && arg_type->points_to()->array_type() != NULL
411eb89e 7377 && !arg_type->points_to()->is_slice_type())
e440a328 7378 arg_type = arg_type->points_to();
7379
7380 if (arg_type->array_type() != NULL
7381 && arg_type->array_type()->length() != NULL)
7382 {
0f914071 7383 if (this->seen_)
7384 return false;
e440a328 7385 Expression* e = arg_type->array_type()->length();
0f914071 7386 this->seen_ = true;
0c77715b 7387 bool r = e->numeric_constant_value(nc);
0f914071 7388 this->seen_ = false;
7389 if (r)
e440a328 7390 {
0c77715b 7391 if (!nc->set_type(Type::lookup_integer_type("int"), false,
7392 this->location()))
7393 r = false;
e440a328 7394 }
0c77715b 7395 return r;
e440a328 7396 }
7397 }
7398 else if (this->code_ == BUILTIN_SIZEOF
7399 || this->code_ == BUILTIN_ALIGNOF)
7400 {
7401 Expression* arg = this->one_arg();
7402 if (arg == NULL)
7403 return false;
7404 Type* arg_type = arg->type();
5c13bd80 7405 if (arg_type->is_error())
e440a328 7406 return false;
7407 if (arg_type->is_abstract())
7408 return false;
2c809f8f 7409 if (this->seen_)
7410 return false;
927a01eb 7411
3f378015 7412 int64_t ret;
e440a328 7413 if (this->code_ == BUILTIN_SIZEOF)
7414 {
2c809f8f 7415 this->seen_ = true;
7416 bool ok = arg_type->backend_type_size(this->gogo_, &ret);
7417 this->seen_ = false;
7418 if (!ok)
e440a328 7419 return false;
7420 }
7421 else if (this->code_ == BUILTIN_ALIGNOF)
7422 {
2c809f8f 7423 bool ok;
7424 this->seen_ = true;
637bd3af 7425 if (arg->field_reference_expression() == NULL)
2c809f8f 7426 ok = arg_type->backend_type_align(this->gogo_, &ret);
637bd3af 7427 else
e440a328 7428 {
7429 // Calling unsafe.Alignof(s.f) returns the alignment of
7430 // the type of f when it is used as a field in a struct.
2c809f8f 7431 ok = arg_type->backend_type_field_align(this->gogo_, &ret);
e440a328 7432 }
2c809f8f 7433 this->seen_ = false;
7434 if (!ok)
7435 return false;
e440a328 7436 }
7437 else
c3e6f413 7438 go_unreachable();
927a01eb 7439
3f378015 7440 mpz_t zval;
7441 set_mpz_from_int64(&zval, ret);
7442 nc->set_int(Type::lookup_integer_type("uintptr"), zval);
7443 mpz_clear(zval);
e440a328 7444 return true;
7445 }
7446 else if (this->code_ == BUILTIN_OFFSETOF)
7447 {
7448 Expression* arg = this->one_arg();
7449 if (arg == NULL)
7450 return false;
7451 Field_reference_expression* farg = arg->field_reference_expression();
7452 if (farg == NULL)
7453 return false;
2c809f8f 7454 if (this->seen_)
7455 return false;
7456
3f378015 7457 int64_t total_offset = 0;
9a4bd570 7458 while (true)
7459 {
7460 Expression* struct_expr = farg->expr();
7461 Type* st = struct_expr->type();
7462 if (st->struct_type() == NULL)
7463 return false;
7464 if (st->named_type() != NULL)
7465 st->named_type()->convert(this->gogo_);
3f378015 7466 int64_t offset;
2c809f8f 7467 this->seen_ = true;
7468 bool ok = st->struct_type()->backend_field_offset(this->gogo_,
7469 farg->field_index(),
7470 &offset);
7471 this->seen_ = false;
7472 if (!ok)
7473 return false;
9a4bd570 7474 total_offset += offset;
7475 if (farg->implicit() && struct_expr->field_reference_expression() != NULL)
7476 {
7477 // Go up until we reach the original base.
7478 farg = struct_expr->field_reference_expression();
7479 continue;
7480 }
7481 break;
7482 }
3f378015 7483 mpz_t zval;
7484 set_mpz_from_int64(&zval, total_offset);
7485 nc->set_int(Type::lookup_integer_type("uintptr"), zval);
7486 mpz_clear(zval);
e440a328 7487 return true;
7488 }
0c77715b 7489 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
e440a328 7490 {
7491 Expression* arg = this->one_arg();
7492 if (arg == NULL)
7493 return false;
7494
0c77715b 7495 Numeric_constant argnc;
7496 if (!arg->numeric_constant_value(&argnc))
7497 return false;
7498
fcbea5e4 7499 mpc_t val;
7500 if (!argnc.to_complex(&val))
0c77715b 7501 return false;
e440a328 7502
0c77715b 7503 Type* type = Builtin_call_expression::real_imag_type(argnc.type());
7504 if (this->code_ == BUILTIN_REAL)
fcbea5e4 7505 nc->set_float(type, mpc_realref(val));
0c77715b 7506 else
fcbea5e4 7507 nc->set_float(type, mpc_imagref(val));
7508 mpc_clear(val);
0c77715b 7509 return true;
e440a328 7510 }
0c77715b 7511 else if (this->code_ == BUILTIN_COMPLEX)
e440a328 7512 {
7513 const Expression_list* args = this->args();
7514 if (args == NULL || args->size() != 2)
7515 return false;
7516
0c77715b 7517 Numeric_constant rnc;
7518 if (!args->front()->numeric_constant_value(&rnc))
7519 return false;
7520 Numeric_constant inc;
7521 if (!args->back()->numeric_constant_value(&inc))
7522 return false;
7523
7524 if (rnc.type() != NULL
7525 && !rnc.type()->is_abstract()
7526 && inc.type() != NULL
7527 && !inc.type()->is_abstract()
7528 && !Type::are_identical(rnc.type(), inc.type(), false, NULL))
7529 return false;
7530
e440a328 7531 mpfr_t r;
0c77715b 7532 if (!rnc.to_float(&r))
7533 return false;
7534 mpfr_t i;
7535 if (!inc.to_float(&i))
e440a328 7536 {
7537 mpfr_clear(r);
7538 return false;
7539 }
7540
0c77715b 7541 Type* arg_type = rnc.type();
7542 if (arg_type == NULL || arg_type->is_abstract())
7543 arg_type = inc.type();
e440a328 7544
fcbea5e4 7545 mpc_t val;
7546 mpc_init2(val, mpc_precision);
7547 mpc_set_fr_fr(val, r, i, MPC_RNDNN);
e440a328 7548 mpfr_clear(r);
7549 mpfr_clear(i);
7550
fcbea5e4 7551 Type* type = Builtin_call_expression::complex_type(arg_type);
7552 nc->set_complex(type, val);
7553
7554 mpc_clear(val);
7555
0c77715b 7556 return true;
e440a328 7557 }
7558
7559 return false;
7560}
7561
a7549a6a 7562// Give an error if we are discarding the value of an expression which
7563// should not normally be discarded. We don't give an error for
7564// discarding the value of an ordinary function call, but we do for
7565// builtin functions, purely for consistency with the gc compiler.
7566
4f2138d7 7567bool
a7549a6a 7568Builtin_call_expression::do_discarding_value()
7569{
7570 switch (this->code_)
7571 {
7572 case BUILTIN_INVALID:
7573 default:
7574 go_unreachable();
7575
7576 case BUILTIN_APPEND:
7577 case BUILTIN_CAP:
7578 case BUILTIN_COMPLEX:
7579 case BUILTIN_IMAG:
7580 case BUILTIN_LEN:
7581 case BUILTIN_MAKE:
7582 case BUILTIN_NEW:
7583 case BUILTIN_REAL:
7584 case BUILTIN_ALIGNOF:
7585 case BUILTIN_OFFSETOF:
7586 case BUILTIN_SIZEOF:
7587 this->unused_value_error();
4f2138d7 7588 return false;
a7549a6a 7589
7590 case BUILTIN_CLOSE:
7591 case BUILTIN_COPY:
1cce762f 7592 case BUILTIN_DELETE:
a7549a6a 7593 case BUILTIN_PANIC:
7594 case BUILTIN_PRINT:
7595 case BUILTIN_PRINTLN:
7596 case BUILTIN_RECOVER:
4f2138d7 7597 return true;
a7549a6a 7598 }
7599}
7600
e440a328 7601// Return the type.
7602
7603Type*
7604Builtin_call_expression::do_type()
7605{
79651b1f 7606 if (this->is_error_expression())
7607 return Type::make_error_type();
e440a328 7608 switch (this->code_)
7609 {
7610 case BUILTIN_INVALID:
7611 default:
79651b1f 7612 return Type::make_error_type();
e440a328 7613
7614 case BUILTIN_NEW:
7615 case BUILTIN_MAKE:
7616 {
7617 const Expression_list* args = this->args();
7618 if (args == NULL || args->empty())
7619 return Type::make_error_type();
7620 return Type::make_pointer_type(args->front()->type());
7621 }
7622
7623 case BUILTIN_CAP:
7624 case BUILTIN_COPY:
7625 case BUILTIN_LEN:
7ba86326 7626 return Type::lookup_integer_type("int");
7627
e440a328 7628 case BUILTIN_ALIGNOF:
7629 case BUILTIN_OFFSETOF:
7630 case BUILTIN_SIZEOF:
7ba86326 7631 return Type::lookup_integer_type("uintptr");
e440a328 7632
7633 case BUILTIN_CLOSE:
1cce762f 7634 case BUILTIN_DELETE:
e440a328 7635 case BUILTIN_PANIC:
7636 case BUILTIN_PRINT:
7637 case BUILTIN_PRINTLN:
7638 return Type::make_void_type();
7639
e440a328 7640 case BUILTIN_RECOVER:
823c7e3d 7641 return Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 7642
7643 case BUILTIN_APPEND:
7644 {
7645 const Expression_list* args = this->args();
7646 if (args == NULL || args->empty())
7647 return Type::make_error_type();
3ff4863b 7648 Type *ret = args->front()->type();
7649 if (!ret->is_slice_type())
7650 return Type::make_error_type();
7651 return ret;
e440a328 7652 }
7653
7654 case BUILTIN_REAL:
7655 case BUILTIN_IMAG:
7656 {
7657 Expression* arg = this->one_arg();
7658 if (arg == NULL)
7659 return Type::make_error_type();
7660 Type* t = arg->type();
7661 if (t->is_abstract())
7662 t = t->make_non_abstract_type();
7663 t = Builtin_call_expression::real_imag_type(t);
7664 if (t == NULL)
7665 t = Type::make_error_type();
7666 return t;
7667 }
7668
48080209 7669 case BUILTIN_COMPLEX:
e440a328 7670 {
7671 const Expression_list* args = this->args();
7672 if (args == NULL || args->size() != 2)
7673 return Type::make_error_type();
7674 Type* t = args->front()->type();
7675 if (t->is_abstract())
7676 {
7677 t = args->back()->type();
7678 if (t->is_abstract())
7679 t = t->make_non_abstract_type();
7680 }
48080209 7681 t = Builtin_call_expression::complex_type(t);
e440a328 7682 if (t == NULL)
7683 t = Type::make_error_type();
7684 return t;
7685 }
7686 }
7687}
7688
7689// Determine the type.
7690
7691void
7692Builtin_call_expression::do_determine_type(const Type_context* context)
7693{
fb94b0ca 7694 if (!this->determining_types())
7695 return;
7696
e440a328 7697 this->fn()->determine_type_no_context();
7698
7699 const Expression_list* args = this->args();
7700
7701 bool is_print;
7702 Type* arg_type = NULL;
7703 switch (this->code_)
7704 {
7705 case BUILTIN_PRINT:
7706 case BUILTIN_PRINTLN:
7707 // Do not force a large integer constant to "int".
7708 is_print = true;
7709 break;
7710
7711 case BUILTIN_REAL:
7712 case BUILTIN_IMAG:
48080209 7713 arg_type = Builtin_call_expression::complex_type(context->type);
f6bc81e6 7714 if (arg_type == NULL)
7715 arg_type = Type::lookup_complex_type("complex128");
e440a328 7716 is_print = false;
7717 break;
7718
48080209 7719 case BUILTIN_COMPLEX:
e440a328 7720 {
48080209 7721 // For the complex function the type of one operand can
e440a328 7722 // determine the type of the other, as in a binary expression.
7723 arg_type = Builtin_call_expression::real_imag_type(context->type);
f6bc81e6 7724 if (arg_type == NULL)
7725 arg_type = Type::lookup_float_type("float64");
e440a328 7726 if (args != NULL && args->size() == 2)
7727 {
7728 Type* t1 = args->front()->type();
c849bb59 7729 Type* t2 = args->back()->type();
e440a328 7730 if (!t1->is_abstract())
7731 arg_type = t1;
7732 else if (!t2->is_abstract())
7733 arg_type = t2;
7734 }
7735 is_print = false;
7736 }
7737 break;
7738
7739 default:
7740 is_print = false;
7741 break;
7742 }
7743
7744 if (args != NULL)
7745 {
7746 for (Expression_list::const_iterator pa = args->begin();
7747 pa != args->end();
7748 ++pa)
7749 {
7750 Type_context subcontext;
7751 subcontext.type = arg_type;
7752
7753 if (is_print)
7754 {
7755 // We want to print large constants, we so can't just
7756 // use the appropriate nonabstract type. Use uint64 for
7757 // an integer if we know it is nonnegative, otherwise
7758 // use int64 for a integer, otherwise use float64 for a
7759 // float or complex128 for a complex.
7760 Type* want_type = NULL;
7761 Type* atype = (*pa)->type();
7762 if (atype->is_abstract())
7763 {
7764 if (atype->integer_type() != NULL)
7765 {
0c77715b 7766 Numeric_constant nc;
7767 if (this->numeric_constant_value(&nc))
7768 {
7769 mpz_t val;
7770 if (nc.to_int(&val))
7771 {
7772 if (mpz_sgn(val) >= 0)
7773 want_type = Type::lookup_integer_type("uint64");
7774 mpz_clear(val);
7775 }
7776 }
7777 if (want_type == NULL)
e440a328 7778 want_type = Type::lookup_integer_type("int64");
e440a328 7779 }
7780 else if (atype->float_type() != NULL)
7781 want_type = Type::lookup_float_type("float64");
7782 else if (atype->complex_type() != NULL)
7783 want_type = Type::lookup_complex_type("complex128");
7784 else if (atype->is_abstract_string_type())
7785 want_type = Type::lookup_string_type();
7786 else if (atype->is_abstract_boolean_type())
7787 want_type = Type::lookup_bool_type();
7788 else
c3e6f413 7789 go_unreachable();
e440a328 7790 subcontext.type = want_type;
7791 }
7792 }
7793
7794 (*pa)->determine_type(&subcontext);
7795 }
7796 }
7797}
7798
7799// If there is exactly one argument, return true. Otherwise give an
7800// error message and return false.
7801
7802bool
7803Builtin_call_expression::check_one_arg()
7804{
7805 const Expression_list* args = this->args();
7806 if (args == NULL || args->size() < 1)
7807 {
7808 this->report_error(_("not enough arguments"));
7809 return false;
7810 }
7811 else if (args->size() > 1)
7812 {
7813 this->report_error(_("too many arguments"));
7814 return false;
7815 }
7816 if (args->front()->is_error_expression()
5c13bd80 7817 || args->front()->type()->is_error())
e440a328 7818 {
7819 this->set_is_error();
7820 return false;
7821 }
7822 return true;
7823}
7824
7825// Check argument types for a builtin function.
7826
7827void
7828Builtin_call_expression::do_check_types(Gogo*)
7829{
375646ea 7830 if (this->is_error_expression())
7831 return;
e440a328 7832 switch (this->code_)
7833 {
7834 case BUILTIN_INVALID:
7835 case BUILTIN_NEW:
7836 case BUILTIN_MAKE:
cd238b8d 7837 case BUILTIN_DELETE:
e440a328 7838 return;
7839
7840 case BUILTIN_LEN:
7841 case BUILTIN_CAP:
7842 {
7843 // The single argument may be either a string or an array or a
7844 // map or a channel, or a pointer to a closed array.
7845 if (this->check_one_arg())
7846 {
7847 Type* arg_type = this->one_arg()->type();
7848 if (arg_type->points_to() != NULL
7849 && arg_type->points_to()->array_type() != NULL
411eb89e 7850 && !arg_type->points_to()->is_slice_type())
e440a328 7851 arg_type = arg_type->points_to();
7852 if (this->code_ == BUILTIN_CAP)
7853 {
5c13bd80 7854 if (!arg_type->is_error()
e440a328 7855 && arg_type->array_type() == NULL
7856 && arg_type->channel_type() == NULL)
7857 this->report_error(_("argument must be array or slice "
7858 "or channel"));
7859 }
7860 else
7861 {
5c13bd80 7862 if (!arg_type->is_error()
e440a328 7863 && !arg_type->is_string_type()
7864 && arg_type->array_type() == NULL
7865 && arg_type->map_type() == NULL
7866 && arg_type->channel_type() == NULL)
7867 this->report_error(_("argument must be string or "
7868 "array or slice or map or channel"));
7869 }
7870 }
7871 }
7872 break;
7873
7874 case BUILTIN_PRINT:
7875 case BUILTIN_PRINTLN:
7876 {
7877 const Expression_list* args = this->args();
7878 if (args == NULL)
7879 {
7880 if (this->code_ == BUILTIN_PRINT)
7881 warning_at(this->location(), 0,
7882 "no arguments for builtin function %<%s%>",
7883 (this->code_ == BUILTIN_PRINT
7884 ? "print"
7885 : "println"));
7886 }
7887 else
7888 {
7889 for (Expression_list::const_iterator p = args->begin();
7890 p != args->end();
7891 ++p)
7892 {
7893 Type* type = (*p)->type();
5c13bd80 7894 if (type->is_error()
e440a328 7895 || type->is_string_type()
7896 || type->integer_type() != NULL
7897 || type->float_type() != NULL
7898 || type->complex_type() != NULL
7899 || type->is_boolean_type()
7900 || type->points_to() != NULL
7901 || type->interface_type() != NULL
7902 || type->channel_type() != NULL
7903 || type->map_type() != NULL
7904 || type->function_type() != NULL
411eb89e 7905 || type->is_slice_type())
e440a328 7906 ;
acf8e158 7907 else if ((*p)->is_type_expression())
7908 {
7909 // If this is a type expression it's going to give
7910 // an error anyhow, so we don't need one here.
7911 }
e440a328 7912 else
7913 this->report_error(_("unsupported argument type to "
7914 "builtin function"));
7915 }
7916 }
7917 }
7918 break;
7919
7920 case BUILTIN_CLOSE:
e440a328 7921 if (this->check_one_arg())
7922 {
7923 if (this->one_arg()->type()->channel_type() == NULL)
7924 this->report_error(_("argument must be channel"));
5202d986 7925 else if (!this->one_arg()->type()->channel_type()->may_send())
7926 this->report_error(_("cannot close receive-only channel"));
e440a328 7927 }
7928 break;
7929
7930 case BUILTIN_PANIC:
7931 case BUILTIN_SIZEOF:
7932 case BUILTIN_ALIGNOF:
7933 this->check_one_arg();
7934 break;
7935
7936 case BUILTIN_RECOVER:
6334270b 7937 if (this->args() != NULL
7938 && !this->args()->empty()
7939 && !this->recover_arg_is_set_)
e440a328 7940 this->report_error(_("too many arguments"));
7941 break;
7942
7943 case BUILTIN_OFFSETOF:
7944 if (this->check_one_arg())
7945 {
7946 Expression* arg = this->one_arg();
7947 if (arg->field_reference_expression() == NULL)
7948 this->report_error(_("argument must be a field reference"));
7949 }
7950 break;
7951
7952 case BUILTIN_COPY:
7953 {
7954 const Expression_list* args = this->args();
7955 if (args == NULL || args->size() < 2)
7956 {
7957 this->report_error(_("not enough arguments"));
7958 break;
7959 }
7960 else if (args->size() > 2)
7961 {
7962 this->report_error(_("too many arguments"));
7963 break;
7964 }
7965 Type* arg1_type = args->front()->type();
7966 Type* arg2_type = args->back()->type();
5c13bd80 7967 if (arg1_type->is_error() || arg2_type->is_error())
6bebb39d 7968 {
7969 this->set_is_error();
7970 break;
7971 }
e440a328 7972
7973 Type* e1;
411eb89e 7974 if (arg1_type->is_slice_type())
e440a328 7975 e1 = arg1_type->array_type()->element_type();
7976 else
7977 {
7978 this->report_error(_("left argument must be a slice"));
7979 break;
7980 }
7981
411eb89e 7982 if (arg2_type->is_slice_type())
60963afd 7983 {
7984 Type* e2 = arg2_type->array_type()->element_type();
7985 if (!Type::are_identical(e1, e2, true, NULL))
7986 this->report_error(_("element types must be the same"));
7987 }
e440a328 7988 else if (arg2_type->is_string_type())
e440a328 7989 {
60963afd 7990 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
7991 this->report_error(_("first argument must be []byte"));
e440a328 7992 }
60963afd 7993 else
7994 this->report_error(_("second argument must be slice or string"));
e440a328 7995 }
7996 break;
7997
7998 case BUILTIN_APPEND:
7999 {
8000 const Expression_list* args = this->args();
b0d311a1 8001 if (args == NULL || args->size() < 2)
e440a328 8002 {
8003 this->report_error(_("not enough arguments"));
8004 break;
8005 }
0b7755ec 8006 if (args->size() > 2)
8007 {
8008 this->report_error(_("too many arguments"));
8009 break;
8010 }
cd238b8d 8011 if (args->front()->type()->is_error()
8012 || args->back()->type()->is_error())
6bebb39d 8013 {
8014 this->set_is_error();
8015 break;
8016 }
cd238b8d 8017
8018 Array_type* at = args->front()->type()->array_type();
8019 Type* e = at->element_type();
4fd4fcf4 8020
8021 // The language permits appending a string to a []byte, as a
8022 // special case.
8023 if (args->back()->type()->is_string_type())
8024 {
60963afd 8025 if (e->integer_type() != NULL && e->integer_type()->is_byte())
4fd4fcf4 8026 break;
8027 }
8028
19fd40c3 8029 // The language says that the second argument must be
8030 // assignable to a slice of the element type of the first
8031 // argument. We already know the first argument is a slice
8032 // type.
cd238b8d 8033 Type* arg2_type = Type::make_array_type(e, NULL);
e440a328 8034 std::string reason;
19fd40c3 8035 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason))
e440a328 8036 {
8037 if (reason.empty())
19fd40c3 8038 this->report_error(_("argument 2 has invalid type"));
e440a328 8039 else
8040 {
19fd40c3 8041 error_at(this->location(), "argument 2 has invalid type (%s)",
e440a328 8042 reason.c_str());
8043 this->set_is_error();
8044 }
8045 }
8046 break;
8047 }
8048
8049 case BUILTIN_REAL:
8050 case BUILTIN_IMAG:
8051 if (this->check_one_arg())
8052 {
8053 if (this->one_arg()->type()->complex_type() == NULL)
8054 this->report_error(_("argument must have complex type"));
8055 }
8056 break;
8057
48080209 8058 case BUILTIN_COMPLEX:
e440a328 8059 {
8060 const Expression_list* args = this->args();
8061 if (args == NULL || args->size() < 2)
8062 this->report_error(_("not enough arguments"));
8063 else if (args->size() > 2)
8064 this->report_error(_("too many arguments"));
8065 else if (args->front()->is_error_expression()
5c13bd80 8066 || args->front()->type()->is_error()
e440a328 8067 || args->back()->is_error_expression()
5c13bd80 8068 || args->back()->type()->is_error())
e440a328 8069 this->set_is_error();
8070 else if (!Type::are_identical(args->front()->type(),
07ba8be5 8071 args->back()->type(), true, NULL))
48080209 8072 this->report_error(_("complex arguments must have identical types"));
e440a328 8073 else if (args->front()->type()->float_type() == NULL)
48080209 8074 this->report_error(_("complex arguments must have "
e440a328 8075 "floating-point type"));
8076 }
8077 break;
8078
8079 default:
c3e6f413 8080 go_unreachable();
e440a328 8081 }
8082}
8083
72666aed 8084Expression*
8085Builtin_call_expression::do_copy()
8086{
8087 Call_expression* bce =
8088 new Builtin_call_expression(this->gogo_, this->fn()->copy(),
da244e59 8089 (this->args() == NULL
8090 ? NULL
8091 : this->args()->copy()),
72666aed 8092 this->is_varargs(),
8093 this->location());
8094
8095 if (this->varargs_are_lowered())
8096 bce->set_varargs_are_lowered();
8097 return bce;
8098}
8099
ea664253 8100// Return the backend representation for a builtin function.
e440a328 8101
ea664253 8102Bexpression*
8103Builtin_call_expression::do_get_backend(Translate_context* context)
e440a328 8104{
8105 Gogo* gogo = context->gogo();
b13c66cd 8106 Location location = this->location();
a0d8874e 8107
8108 if (this->is_erroneous_call())
8109 {
8110 go_assert(saw_errors());
8111 return gogo->backend()->error_expression();
8112 }
8113
e440a328 8114 switch (this->code_)
8115 {
8116 case BUILTIN_INVALID:
8117 case BUILTIN_NEW:
8118 case BUILTIN_MAKE:
c3e6f413 8119 go_unreachable();
e440a328 8120
8121 case BUILTIN_LEN:
8122 case BUILTIN_CAP:
8123 {
8124 const Expression_list* args = this->args();
c484d925 8125 go_assert(args != NULL && args->size() == 1);
2c809f8f 8126 Expression* arg = args->front();
e440a328 8127 Type* arg_type = arg->type();
0f914071 8128
8129 if (this->seen_)
8130 {
c484d925 8131 go_assert(saw_errors());
ea664253 8132 return context->backend()->error_expression();
0f914071 8133 }
8134 this->seen_ = true;
0f914071 8135 this->seen_ = false;
e440a328 8136 if (arg_type->points_to() != NULL)
8137 {
8138 arg_type = arg_type->points_to();
c484d925 8139 go_assert(arg_type->array_type() != NULL
411eb89e 8140 && !arg_type->is_slice_type());
2c809f8f 8141 arg = Expression::make_unary(OPERATOR_MULT, arg, location);
e440a328 8142 }
8143
1b1f2abf 8144 Type* int_type = Type::lookup_integer_type("int");
2c809f8f 8145 Expression* val;
e440a328 8146 if (this->code_ == BUILTIN_LEN)
8147 {
8148 if (arg_type->is_string_type())
2c809f8f 8149 val = Expression::make_string_info(arg, STRING_INFO_LENGTH,
8150 location);
e440a328 8151 else if (arg_type->array_type() != NULL)
0f914071 8152 {
8153 if (this->seen_)
8154 {
c484d925 8155 go_assert(saw_errors());
ea664253 8156 return context->backend()->error_expression();
0f914071 8157 }
8158 this->seen_ = true;
2c809f8f 8159 val = arg_type->array_type()->get_length(gogo, arg);
0f914071 8160 this->seen_ = false;
8161 }
e440a328 8162 else if (arg_type->map_type() != NULL)
2c809f8f 8163 val = Runtime::make_call(Runtime::MAP_LEN, location, 1, arg);
e440a328 8164 else if (arg_type->channel_type() != NULL)
2c809f8f 8165 val = Runtime::make_call(Runtime::CHAN_LEN, location, 1, arg);
e440a328 8166 else
c3e6f413 8167 go_unreachable();
e440a328 8168 }
8169 else
8170 {
8171 if (arg_type->array_type() != NULL)
0f914071 8172 {
8173 if (this->seen_)
8174 {
c484d925 8175 go_assert(saw_errors());
ea664253 8176 return context->backend()->error_expression();
0f914071 8177 }
8178 this->seen_ = true;
2c809f8f 8179 val = arg_type->array_type()->get_capacity(gogo, arg);
0f914071 8180 this->seen_ = false;
8181 }
e440a328 8182 else if (arg_type->channel_type() != NULL)
2c809f8f 8183 val = Runtime::make_call(Runtime::CHAN_CAP, location, 1, arg);
e440a328 8184 else
c3e6f413 8185 go_unreachable();
e440a328 8186 }
8187
2c809f8f 8188 return Expression::make_cast(int_type, val,
ea664253 8189 location)->get_backend(context);
e440a328 8190 }
8191
8192 case BUILTIN_PRINT:
8193 case BUILTIN_PRINTLN:
8194 {
8195 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
2c809f8f 8196 Expression* print_stmts = NULL;
e440a328 8197
8198 const Expression_list* call_args = this->args();
8199 if (call_args != NULL)
8200 {
8201 for (Expression_list::const_iterator p = call_args->begin();
8202 p != call_args->end();
8203 ++p)
8204 {
8205 if (is_ln && p != call_args->begin())
8206 {
2c809f8f 8207 Expression* print_space =
8208 Runtime::make_call(Runtime::PRINT_SPACE,
8209 this->location(), 0);
e440a328 8210
2c809f8f 8211 print_stmts =
8212 Expression::make_compound(print_stmts, print_space,
8213 location);
8214 }
e440a328 8215
2c809f8f 8216 Expression* arg = *p;
8217 Type* type = arg->type();
8218 Runtime::Function code;
e440a328 8219 if (type->is_string_type())
2c809f8f 8220 code = Runtime::PRINT_STRING;
e440a328 8221 else if (type->integer_type() != NULL
8222 && type->integer_type()->is_unsigned())
8223 {
e440a328 8224 Type* itype = Type::lookup_integer_type("uint64");
2c809f8f 8225 arg = Expression::make_cast(itype, arg, location);
8226 code = Runtime::PRINT_UINT64;
e440a328 8227 }
8228 else if (type->integer_type() != NULL)
8229 {
e440a328 8230 Type* itype = Type::lookup_integer_type("int64");
2c809f8f 8231 arg = Expression::make_cast(itype, arg, location);
8232 code = Runtime::PRINT_INT64;
e440a328 8233 }
8234 else if (type->float_type() != NULL)
8235 {
2c809f8f 8236 Type* dtype = Type::lookup_float_type("float64");
8237 arg = Expression::make_cast(dtype, arg, location);
8238 code = Runtime::PRINT_DOUBLE;
e440a328 8239 }
8240 else if (type->complex_type() != NULL)
8241 {
2c809f8f 8242 Type* ctype = Type::lookup_complex_type("complex128");
8243 arg = Expression::make_cast(ctype, arg, location);
8244 code = Runtime::PRINT_COMPLEX;
e440a328 8245 }
8246 else if (type->is_boolean_type())
2c809f8f 8247 code = Runtime::PRINT_BOOL;
e440a328 8248 else if (type->points_to() != NULL
8249 || type->channel_type() != NULL
8250 || type->map_type() != NULL
8251 || type->function_type() != NULL)
8252 {
2c809f8f 8253 arg = Expression::make_cast(type, arg, location);
8254 code = Runtime::PRINT_POINTER;
e440a328 8255 }
8256 else if (type->interface_type() != NULL)
8257 {
8258 if (type->interface_type()->is_empty())
2c809f8f 8259 code = Runtime::PRINT_EMPTY_INTERFACE;
e440a328 8260 else
2c809f8f 8261 code = Runtime::PRINT_INTERFACE;
e440a328 8262 }
411eb89e 8263 else if (type->is_slice_type())
2c809f8f 8264 code = Runtime::PRINT_SLICE;
e440a328 8265 else
cd238b8d 8266 {
8267 go_assert(saw_errors());
ea664253 8268 return context->backend()->error_expression();
cd238b8d 8269 }
e440a328 8270
2c809f8f 8271 Expression* call = Runtime::make_call(code, location, 1, arg);
8272 if (print_stmts == NULL)
8273 print_stmts = call;
8274 else
8275 print_stmts = Expression::make_compound(print_stmts, call,
8276 location);
e440a328 8277 }
8278 }
8279
8280 if (is_ln)
8281 {
2c809f8f 8282 Expression* print_nl =
8283 Runtime::make_call(Runtime::PRINT_NL, location, 0);
8284 if (print_stmts == NULL)
8285 print_stmts = print_nl;
8286 else
8287 print_stmts = Expression::make_compound(print_stmts, print_nl,
8288 location);
e440a328 8289 }
8290
32e3ff69 8291 // There aren't any arguments to the print builtin. The compiler
8292 // issues a warning for this so we should avoid getting the backend
8293 // representation for this call. Instead, perform a no-op.
8294 if (print_stmts == NULL)
8295 return context->backend()->boolean_constant_expression(false);
8296
ea664253 8297 return print_stmts->get_backend(context);
e440a328 8298 }
8299
8300 case BUILTIN_PANIC:
8301 {
8302 const Expression_list* args = this->args();
c484d925 8303 go_assert(args != NULL && args->size() == 1);
e440a328 8304 Expression* arg = args->front();
b13c66cd 8305 Type *empty =
823c7e3d 8306 Type::make_empty_interface_type(Linemap::predeclared_location());
2c809f8f 8307 arg = Expression::convert_for_assignment(gogo, empty, arg, location);
8308
8309 Expression* panic =
8310 Runtime::make_call(Runtime::PANIC, location, 1, arg);
ea664253 8311 return panic->get_backend(context);
e440a328 8312 }
8313
8314 case BUILTIN_RECOVER:
8315 {
8316 // The argument is set when building recover thunks. It's a
8317 // boolean value which is true if we can recover a value now.
8318 const Expression_list* args = this->args();
c484d925 8319 go_assert(args != NULL && args->size() == 1);
e440a328 8320 Expression* arg = args->front();
b13c66cd 8321 Type *empty =
823c7e3d 8322 Type::make_empty_interface_type(Linemap::predeclared_location());
e440a328 8323
e440a328 8324 Expression* nil = Expression::make_nil(location);
2c809f8f 8325 nil = Expression::convert_for_assignment(gogo, empty, nil, location);
e440a328 8326
8327 // We need to handle a deferred call to recover specially,
8328 // because it changes whether it can recover a panic or not.
8329 // See test7 in test/recover1.go.
2c809f8f 8330 Expression* recover = Runtime::make_call((this->is_deferred()
8331 ? Runtime::DEFERRED_RECOVER
8332 : Runtime::RECOVER),
8333 location, 0);
8334 Expression* cond =
8335 Expression::make_conditional(arg, recover, nil, location);
ea664253 8336 return cond->get_backend(context);
e440a328 8337 }
8338
8339 case BUILTIN_CLOSE:
e440a328 8340 {
8341 const Expression_list* args = this->args();
c484d925 8342 go_assert(args != NULL && args->size() == 1);
e440a328 8343 Expression* arg = args->front();
2c809f8f 8344 Expression* close = Runtime::make_call(Runtime::CLOSE, location,
8345 1, arg);
ea664253 8346 return close->get_backend(context);
e440a328 8347 }
8348
8349 case BUILTIN_SIZEOF:
8350 case BUILTIN_OFFSETOF:
8351 case BUILTIN_ALIGNOF:
8352 {
0c77715b 8353 Numeric_constant nc;
8354 unsigned long val;
8355 if (!this->numeric_constant_value(&nc)
8356 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
7f1d9abd 8357 {
c484d925 8358 go_assert(saw_errors());
ea664253 8359 return context->backend()->error_expression();
7f1d9abd 8360 }
7ba86326 8361 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2c809f8f 8362 mpz_t ival;
8363 nc.get_int(&ival);
8364 Expression* int_cst =
e67508fa 8365 Expression::make_integer_z(&ival, uintptr_type, location);
2c809f8f 8366 mpz_clear(ival);
ea664253 8367 return int_cst->get_backend(context);
e440a328 8368 }
8369
8370 case BUILTIN_COPY:
8371 {
8372 const Expression_list* args = this->args();
c484d925 8373 go_assert(args != NULL && args->size() == 2);
e440a328 8374 Expression* arg1 = args->front();
8375 Expression* arg2 = args->back();
8376
e440a328 8377 Type* arg1_type = arg1->type();
8378 Array_type* at = arg1_type->array_type();
35a54f17 8379 go_assert(arg1->is_variable());
2c809f8f 8380 Expression* arg1_val = at->get_value_pointer(gogo, arg1);
8381 Expression* arg1_len = at->get_length(gogo, arg1);
e440a328 8382
8383 Type* arg2_type = arg2->type();
2c809f8f 8384 go_assert(arg2->is_variable());
8385 Expression* arg2_val;
8386 Expression* arg2_len;
411eb89e 8387 if (arg2_type->is_slice_type())
e440a328 8388 {
8389 at = arg2_type->array_type();
2c809f8f 8390 arg2_val = at->get_value_pointer(gogo, arg2);
8391 arg2_len = at->get_length(gogo, arg2);
e440a328 8392 }
8393 else
8394 {
2c809f8f 8395 go_assert(arg2->is_variable());
8396 arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA,
8397 location);
8398 arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH,
8399 location);
e440a328 8400 }
2c809f8f 8401 Expression* cond =
8402 Expression::make_binary(OPERATOR_LT, arg1_len, arg2_len, location);
8403 Expression* length =
8404 Expression::make_conditional(cond, arg1_len, arg2_len, location);
e440a328 8405
8406 Type* element_type = at->element_type();
2a305b85 8407 int64_t element_size;
8408 bool ok = element_type->backend_type_size(gogo, &element_size);
8409 if (!ok)
8410 {
8411 go_assert(saw_errors());
8412 return gogo->backend()->error_expression();
8413 }
8414
3f378015 8415 Expression* size_expr = Expression::make_integer_int64(element_size,
8416 length->type(),
8417 location);
2c809f8f 8418 Expression* bytecount =
8419 Expression::make_binary(OPERATOR_MULT, size_expr, length, location);
8420 Expression* copy = Runtime::make_call(Runtime::COPY, location, 3,
8421 arg1_val, arg2_val, bytecount);
8422
8423 Expression* compound = Expression::make_compound(copy, length, location);
ea664253 8424 return compound->get_backend(context);
e440a328 8425 }
8426
8427 case BUILTIN_APPEND:
8428 {
8429 const Expression_list* args = this->args();
c484d925 8430 go_assert(args != NULL && args->size() == 2);
e440a328 8431 Expression* arg1 = args->front();
8432 Expression* arg2 = args->back();
8433
9d44fbe3 8434 Array_type* at = arg1->type()->array_type();
4fd4fcf4 8435 Type* element_type = at->element_type()->forwarded();
9d44fbe3 8436
2c809f8f 8437 go_assert(arg2->is_variable());
8438 Expression* arg2_val;
8439 Expression* arg2_len;
3f378015 8440 int64_t size;
4fd4fcf4 8441 if (arg2->type()->is_string_type()
60963afd 8442 && element_type->integer_type() != NULL
8443 && element_type->integer_type()->is_byte())
4fd4fcf4 8444 {
2c809f8f 8445 arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA,
8446 location);
8447 arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH,
8448 location);
e67508fa 8449 size = 1;
4fd4fcf4 8450 }
8451 else
8452 {
2c809f8f 8453 arg2_val = at->get_value_pointer(gogo, arg2);
8454 arg2_len = at->get_length(gogo, arg2);
2a305b85 8455 bool ok = element_type->backend_type_size(gogo, &size);
8456 if (!ok)
8457 {
8458 go_assert(saw_errors());
8459 return gogo->backend()->error_expression();
8460 }
4fd4fcf4 8461 }
2c809f8f 8462 Expression* element_size =
3f378015 8463 Expression::make_integer_int64(size, NULL, location);
2c809f8f 8464
8465 Expression* append = Runtime::make_call(Runtime::APPEND, location, 4,
8466 arg1, arg2_val, arg2_len,
8467 element_size);
8468 append = Expression::make_unsafe_cast(arg1->type(), append, location);
ea664253 8469 return append->get_backend(context);
e440a328 8470 }
8471
8472 case BUILTIN_REAL:
8473 case BUILTIN_IMAG:
8474 {
8475 const Expression_list* args = this->args();
c484d925 8476 go_assert(args != NULL && args->size() == 1);
2c809f8f 8477
8478 Bexpression* ret;
ea664253 8479 Bexpression* bcomplex = args->front()->get_backend(context);
2c809f8f 8480 if (this->code_ == BUILTIN_REAL)
8481 ret = gogo->backend()->real_part_expression(bcomplex, location);
8482 else
8483 ret = gogo->backend()->imag_part_expression(bcomplex, location);
ea664253 8484 return ret;
e440a328 8485 }
8486
48080209 8487 case BUILTIN_COMPLEX:
e440a328 8488 {
8489 const Expression_list* args = this->args();
c484d925 8490 go_assert(args != NULL && args->size() == 2);
ea664253 8491 Bexpression* breal = args->front()->get_backend(context);
8492 Bexpression* bimag = args->back()->get_backend(context);
8493 return gogo->backend()->complex_expression(breal, bimag, location);
e440a328 8494 }
8495
8496 default:
c3e6f413 8497 go_unreachable();
e440a328 8498 }
8499}
8500
8501// We have to support exporting a builtin call expression, because
8502// code can set a constant to the result of a builtin expression.
8503
8504void
8505Builtin_call_expression::do_export(Export* exp) const
8506{
0c77715b 8507 Numeric_constant nc;
8508 if (!this->numeric_constant_value(&nc))
8509 {
8510 error_at(this->location(), "value is not constant");
8511 return;
8512 }
e440a328 8513
0c77715b 8514 if (nc.is_int())
e440a328 8515 {
0c77715b 8516 mpz_t val;
8517 nc.get_int(&val);
e440a328 8518 Integer_expression::export_integer(exp, val);
0c77715b 8519 mpz_clear(val);
e440a328 8520 }
0c77715b 8521 else if (nc.is_float())
e440a328 8522 {
8523 mpfr_t fval;
0c77715b 8524 nc.get_float(&fval);
8525 Float_expression::export_float(exp, fval);
e440a328 8526 mpfr_clear(fval);
8527 }
0c77715b 8528 else if (nc.is_complex())
e440a328 8529 {
fcbea5e4 8530 mpc_t cval;
8531 nc.get_complex(&cval);
8532 Complex_expression::export_complex(exp, cval);
8533 mpc_clear(cval);
e440a328 8534 }
0c77715b 8535 else
8536 go_unreachable();
e440a328 8537
8538 // A trailing space lets us reliably identify the end of the number.
8539 exp->write_c_string(" ");
8540}
8541
8542// Class Call_expression.
8543
8381eda7 8544// A Go function can be viewed in a couple of different ways. The
8545// code of a Go function becomes a backend function with parameters
8546// whose types are simply the backend representation of the Go types.
8547// If there are multiple results, they are returned as a backend
8548// struct.
8549
8550// However, when Go code refers to a function other than simply
8551// calling it, the backend type of that function is actually a struct.
8552// The first field of the struct points to the Go function code
8553// (sometimes a wrapper as described below). The remaining fields
8554// hold addresses of closed-over variables. This struct is called a
8555// closure.
8556
8557// There are a few cases to consider.
8558
8559// A direct function call of a known function in package scope. In
8560// this case there are no closed-over variables, and we know the name
8561// of the function code. We can simply produce a backend call to the
8562// function directly, and not worry about the closure.
8563
8564// A direct function call of a known function literal. In this case
8565// we know the function code and we know the closure. We generate the
8566// function code such that it expects an additional final argument of
8567// the closure type. We pass the closure as the last argument, after
8568// the other arguments.
8569
8570// An indirect function call. In this case we have a closure. We
8571// load the pointer to the function code from the first field of the
8572// closure. We pass the address of the closure as the last argument.
8573
8574// A call to a method of an interface. Type methods are always at
8575// package scope, so we call the function directly, and don't worry
8576// about the closure.
8577
8578// This means that for a function at package scope we have two cases.
8579// One is the direct call, which has no closure. The other is the
8580// indirect call, which does have a closure. We can't simply ignore
8581// the closure, even though it is the last argument, because that will
8582// fail on targets where the function pops its arguments. So when
8583// generating a closure for a package-scope function we set the
8584// function code pointer in the closure to point to a wrapper
8585// function. This wrapper function accepts a final argument that
8586// points to the closure, ignores it, and calls the real function as a
8587// direct function call. This wrapper will normally be efficient, and
8588// can often simply be a tail call to the real function.
8589
8590// We don't use GCC's static chain pointer because 1) we don't need
8591// it; 2) GCC only permits using a static chain to call a known
8592// function, so we can't use it for an indirect call anyhow. Since we
8593// can't use it for an indirect call, we may as well not worry about
8594// using it for a direct call either.
8595
8596// We pass the closure last rather than first because it means that
8597// the function wrapper we put into a closure for a package-scope
8598// function can normally just be a tail call to the real function.
8599
8600// For method expressions we generate a wrapper that loads the
8601// receiver from the closure and then calls the method. This
8602// unfortunately forces reshuffling the arguments, since there is a
8603// new first argument, but we can't avoid reshuffling either for
8604// method expressions or for indirect calls of package-scope
8605// functions, and since the latter are more common we reshuffle for
8606// method expressions.
8607
8608// Note that the Go code retains the Go types. The extra final
8609// argument only appears when we convert to the backend
8610// representation.
8611
e440a328 8612// Traversal.
8613
8614int
8615Call_expression::do_traverse(Traverse* traverse)
8616{
0c0dacab 8617 // If we are calling a function in a different package that returns
8618 // an unnamed type, this may be the only chance we get to traverse
8619 // that type. We don't traverse this->type_ because it may be a
8620 // Call_multiple_result_type that will just lead back here.
8621 if (this->type_ != NULL && !this->type_->is_error_type())
8622 {
8623 Function_type *fntype = this->get_function_type();
8624 if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT)
8625 return TRAVERSE_EXIT;
8626 }
e440a328 8627 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8628 return TRAVERSE_EXIT;
8629 if (this->args_ != NULL)
8630 {
8631 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8632 return TRAVERSE_EXIT;
8633 }
8634 return TRAVERSE_CONTINUE;
8635}
8636
8637// Lower a call statement.
8638
8639Expression*
ceeb4318 8640Call_expression::do_lower(Gogo* gogo, Named_object* function,
8641 Statement_inserter* inserter, int)
e440a328 8642{
b13c66cd 8643 Location loc = this->location();
09ea332d 8644
ceeb4318 8645 // A type cast can look like a function call.
e440a328 8646 if (this->fn_->is_type_expression()
8647 && this->args_ != NULL
8648 && this->args_->size() == 1)
8649 return Expression::make_cast(this->fn_->type(), this->args_->front(),
09ea332d 8650 loc);
e440a328 8651
88f06749 8652 // Because do_type will return an error type and thus prevent future
8653 // errors, check for that case now to ensure that the error gets
8654 // reported.
37448b10 8655 Function_type* fntype = this->get_function_type();
8656 if (fntype == NULL)
88f06749 8657 {
8658 if (!this->fn_->type()->is_error())
8659 this->report_error(_("expected function"));
5f1045b5 8660 this->set_is_error();
8661 return this;
88f06749 8662 }
8663
e440a328 8664 // Handle an argument which is a call to a function which returns
8665 // multiple results.
8666 if (this->args_ != NULL
8667 && this->args_->size() == 1
37448b10 8668 && this->args_->front()->call_expression() != NULL)
e440a328 8669 {
e440a328 8670 size_t rc = this->args_->front()->call_expression()->result_count();
8671 if (rc > 1
37448b10 8672 && ((fntype->parameters() != NULL
8673 && (fntype->parameters()->size() == rc
8674 || (fntype->is_varargs()
8675 && fntype->parameters()->size() - 1 <= rc)))
8676 || fntype->is_builtin()))
e440a328 8677 {
8678 Call_expression* call = this->args_->front()->call_expression();
e90ecd2d 8679 call->set_is_multi_value_arg();
c33af8e4 8680 if (this->is_varargs_)
8681 {
8682 // It is not clear which result of a multiple result call
8683 // the ellipsis operator should be applied to. If we unpack the
8684 // the call into its individual results here, the ellipsis will be
8685 // applied to the last result.
8686 error_at(call->location(),
8687 _("multiple-value argument in single-value context"));
8688 return Expression::make_error(call->location());
8689 }
8690
e440a328 8691 Expression_list* args = new Expression_list;
8692 for (size_t i = 0; i < rc; ++i)
8693 args->push_back(Expression::make_call_result(call, i));
8694 // We can't return a new call expression here, because this
42535814 8695 // one may be referenced by Call_result expressions. We
8696 // also can't delete the old arguments, because we may still
8697 // traverse them somewhere up the call stack. FIXME.
e440a328 8698 this->args_ = args;
8699 }
8700 }
8701
37448b10 8702 // Recognize a call to a builtin function.
8703 if (fntype->is_builtin())
8704 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8705 this->is_varargs_, loc);
8706
ceeb4318 8707 // If this call returns multiple results, create a temporary
8708 // variable for each result.
8709 size_t rc = this->result_count();
8710 if (rc > 1 && this->results_ == NULL)
8711 {
8712 std::vector<Temporary_statement*>* temps =
8713 new std::vector<Temporary_statement*>;
8714 temps->reserve(rc);
37448b10 8715 const Typed_identifier_list* results = fntype->results();
ceeb4318 8716 for (Typed_identifier_list::const_iterator p = results->begin();
8717 p != results->end();
8718 ++p)
8719 {
8720 Temporary_statement* temp = Statement::make_temporary(p->type(),
09ea332d 8721 NULL, loc);
ceeb4318 8722 inserter->insert(temp);
8723 temps->push_back(temp);
8724 }
8725 this->results_ = temps;
8726 }
8727
e440a328 8728 // Handle a call to a varargs function by packaging up the extra
8729 // parameters.
37448b10 8730 if (fntype->is_varargs())
e440a328 8731 {
e440a328 8732 const Typed_identifier_list* parameters = fntype->parameters();
c484d925 8733 go_assert(parameters != NULL && !parameters->empty());
e440a328 8734 Type* varargs_type = parameters->back().type();
09ea332d 8735 this->lower_varargs(gogo, function, inserter, varargs_type,
8736 parameters->size());
8737 }
8738
8739 // If this is call to a method, call the method directly passing the
8740 // object as the first parameter.
8741 Bound_method_expression* bme = this->fn_->bound_method_expression();
8742 if (bme != NULL)
8743 {
0afbb937 8744 Named_object* methodfn = bme->function();
09ea332d 8745 Expression* first_arg = bme->first_argument();
8746
8747 // We always pass a pointer when calling a method.
8748 if (first_arg->type()->points_to() == NULL
8749 && !first_arg->type()->is_error())
8750 {
8751 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
8752 // We may need to create a temporary variable so that we can
8753 // take the address. We can't do that here because it will
8754 // mess up the order of evaluation.
8755 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
8756 ue->set_create_temp();
8757 }
8758
8759 // If we are calling a method which was inherited from an
8760 // embedded struct, and the method did not get a stub, then the
8761 // first type may be wrong.
8762 Type* fatype = bme->first_argument_type();
8763 if (fatype != NULL)
8764 {
8765 if (fatype->points_to() == NULL)
8766 fatype = Type::make_pointer_type(fatype);
8767 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
8768 }
8769
8770 Expression_list* new_args = new Expression_list();
8771 new_args->push_back(first_arg);
8772 if (this->args_ != NULL)
8773 {
8774 for (Expression_list::const_iterator p = this->args_->begin();
8775 p != this->args_->end();
8776 ++p)
8777 new_args->push_back(*p);
8778 }
8779
8780 // We have to change in place because this structure may be
8781 // referenced by Call_result_expressions. We can't delete the
8782 // old arguments, because we may be traversing them up in some
8783 // caller. FIXME.
8784 this->args_ = new_args;
0afbb937 8785 this->fn_ = Expression::make_func_reference(methodfn, NULL,
09ea332d 8786 bme->location());
e440a328 8787 }
8788
8789 return this;
8790}
8791
8792// Lower a call to a varargs function. FUNCTION is the function in
8793// which the call occurs--it's not the function we are calling.
8794// VARARGS_TYPE is the type of the varargs parameter, a slice type.
8795// PARAM_COUNT is the number of parameters of the function we are
8796// calling; the last of these parameters will be the varargs
8797// parameter.
8798
09ea332d 8799void
e440a328 8800Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
ceeb4318 8801 Statement_inserter* inserter,
e440a328 8802 Type* varargs_type, size_t param_count)
8803{
8804 if (this->varargs_are_lowered_)
09ea332d 8805 return;
e440a328 8806
b13c66cd 8807 Location loc = this->location();
e440a328 8808
c484d925 8809 go_assert(param_count > 0);
411eb89e 8810 go_assert(varargs_type->is_slice_type());
e440a328 8811
8812 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8813 if (arg_count < param_count - 1)
8814 {
8815 // Not enough arguments; will be caught in check_types.
09ea332d 8816 return;
e440a328 8817 }
8818
8819 Expression_list* old_args = this->args_;
8820 Expression_list* new_args = new Expression_list();
8821 bool push_empty_arg = false;
8822 if (old_args == NULL || old_args->empty())
8823 {
c484d925 8824 go_assert(param_count == 1);
e440a328 8825 push_empty_arg = true;
8826 }
8827 else
8828 {
8829 Expression_list::const_iterator pa;
8830 int i = 1;
8831 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8832 {
8833 if (static_cast<size_t>(i) == param_count)
8834 break;
8835 new_args->push_back(*pa);
8836 }
8837
8838 // We have reached the varargs parameter.
8839
8840 bool issued_error = false;
8841 if (pa == old_args->end())
8842 push_empty_arg = true;
8843 else if (pa + 1 == old_args->end() && this->is_varargs_)
8844 new_args->push_back(*pa);
8845 else if (this->is_varargs_)
8846 {
a6645f74 8847 if ((*pa)->type()->is_slice_type())
8848 this->report_error(_("too many arguments"));
8849 else
8850 {
8851 error_at(this->location(),
8852 _("invalid use of %<...%> with non-slice"));
8853 this->set_is_error();
8854 }
09ea332d 8855 return;
e440a328 8856 }
e440a328 8857 else
8858 {
8859 Type* element_type = varargs_type->array_type()->element_type();
8860 Expression_list* vals = new Expression_list;
8861 for (; pa != old_args->end(); ++pa, ++i)
8862 {
8863 // Check types here so that we get a better message.
8864 Type* patype = (*pa)->type();
b13c66cd 8865 Location paloc = (*pa)->location();
e440a328 8866 if (!this->check_argument_type(i, element_type, patype,
8867 paloc, issued_error))
8868 continue;
8869 vals->push_back(*pa);
8870 }
8871 Expression* val =
8872 Expression::make_slice_composite_literal(varargs_type, vals, loc);
09ea332d 8873 gogo->lower_expression(function, inserter, &val);
e440a328 8874 new_args->push_back(val);
8875 }
8876 }
8877
8878 if (push_empty_arg)
8879 new_args->push_back(Expression::make_nil(loc));
8880
8881 // We can't return a new call expression here, because this one may
6d4c2432 8882 // be referenced by Call_result expressions. FIXME. We can't
8883 // delete OLD_ARGS because we may have both a Call_expression and a
8884 // Builtin_call_expression which refer to them. FIXME.
e440a328 8885 this->args_ = new_args;
8886 this->varargs_are_lowered_ = true;
e440a328 8887}
8888
2c809f8f 8889// Flatten a call with multiple results into a temporary.
8890
8891Expression*
b8e86a51 8892Call_expression::do_flatten(Gogo* gogo, Named_object*,
8893 Statement_inserter* inserter)
2c809f8f 8894{
5bf8be8b 8895 if (this->is_erroneous_call())
8896 {
8897 go_assert(saw_errors());
8898 return Expression::make_error(this->location());
8899 }
b8e86a51 8900
91c0fd76 8901 if (this->is_flattened_)
8902 return this;
8903 this->is_flattened_ = true;
8904
b8e86a51 8905 // Add temporary variables for all arguments that require type
8906 // conversion.
8907 Function_type* fntype = this->get_function_type();
9782d556 8908 if (fntype == NULL)
8909 {
8910 go_assert(saw_errors());
8911 return this;
8912 }
b8e86a51 8913 if (this->args_ != NULL && !this->args_->empty()
8914 && fntype->parameters() != NULL && !fntype->parameters()->empty())
8915 {
8916 bool is_interface_method =
8917 this->fn_->interface_field_reference_expression() != NULL;
8918
8919 Expression_list *args = new Expression_list();
8920 Typed_identifier_list::const_iterator pp = fntype->parameters()->begin();
8921 Expression_list::const_iterator pa = this->args_->begin();
8922 if (!is_interface_method && fntype->is_method())
8923 {
8924 // The receiver argument.
8925 args->push_back(*pa);
8926 ++pa;
8927 }
8928 for (; pa != this->args_->end(); ++pa, ++pp)
8929 {
8930 go_assert(pp != fntype->parameters()->end());
8931 if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL))
8932 args->push_back(*pa);
8933 else
8934 {
8935 Location loc = (*pa)->location();
8ba8cc87 8936 Expression* arg = *pa;
8937 if (!arg->is_variable())
8938 {
8939 Temporary_statement *temp =
8940 Statement::make_temporary(NULL, arg, loc);
8941 inserter->insert(temp);
8942 arg = Expression::make_temporary_reference(temp, loc);
8943 }
8944 arg = Expression::convert_for_assignment(gogo, pp->type(), arg,
8945 loc);
8946 args->push_back(arg);
b8e86a51 8947 }
8948 }
8949 delete this->args_;
8950 this->args_ = args;
8951 }
8952
2c809f8f 8953 size_t rc = this->result_count();
8954 if (rc > 1 && this->call_temp_ == NULL)
8955 {
8956 Struct_field_list* sfl = new Struct_field_list();
8957 Function_type* fntype = this->get_function_type();
8958 const Typed_identifier_list* results = fntype->results();
8959 Location loc = this->location();
8960
8961 int i = 0;
8962 char buf[10];
8963 for (Typed_identifier_list::const_iterator p = results->begin();
8964 p != results->end();
8965 ++p, ++i)
8966 {
8967 snprintf(buf, sizeof buf, "res%d", i);
8968 sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc)));
8969 }
8970
8971 Struct_type* st = Type::make_struct_type(sfl, loc);
8972 this->call_temp_ = Statement::make_temporary(st, NULL, loc);
8973 inserter->insert(this->call_temp_);
8974 }
8975
8976 return this;
8977}
8978
ceeb4318 8979// Get the function type. This can return NULL in error cases.
e440a328 8980
8981Function_type*
8982Call_expression::get_function_type() const
8983{
8984 return this->fn_->type()->function_type();
8985}
8986
8987// Return the number of values which this call will return.
8988
8989size_t
8990Call_expression::result_count() const
8991{
8992 const Function_type* fntype = this->get_function_type();
8993 if (fntype == NULL)
8994 return 0;
8995 if (fntype->results() == NULL)
8996 return 0;
8997 return fntype->results()->size();
8998}
8999
ceeb4318 9000// Return the temporary which holds a result.
9001
9002Temporary_statement*
9003Call_expression::result(size_t i) const
9004{
cd238b8d 9005 if (this->results_ == NULL || this->results_->size() <= i)
9006 {
9007 go_assert(saw_errors());
9008 return NULL;
9009 }
ceeb4318 9010 return (*this->results_)[i];
9011}
9012
1373401e 9013// Set the number of results expected from a call expression.
9014
9015void
9016Call_expression::set_expected_result_count(size_t count)
9017{
9018 go_assert(this->expected_result_count_ == 0);
9019 this->expected_result_count_ = count;
9020}
9021
e440a328 9022// Return whether this is a call to the predeclared function recover.
9023
9024bool
9025Call_expression::is_recover_call() const
9026{
9027 return this->do_is_recover_call();
9028}
9029
9030// Set the argument to the recover function.
9031
9032void
9033Call_expression::set_recover_arg(Expression* arg)
9034{
9035 this->do_set_recover_arg(arg);
9036}
9037
9038// Virtual functions also implemented by Builtin_call_expression.
9039
9040bool
9041Call_expression::do_is_recover_call() const
9042{
9043 return false;
9044}
9045
9046void
9047Call_expression::do_set_recover_arg(Expression*)
9048{
c3e6f413 9049 go_unreachable();
e440a328 9050}
9051
ceeb4318 9052// We have found an error with this call expression; return true if
9053// we should report it.
9054
9055bool
9056Call_expression::issue_error()
9057{
9058 if (this->issued_error_)
9059 return false;
9060 else
9061 {
9062 this->issued_error_ = true;
9063 return true;
9064 }
9065}
9066
5bf8be8b 9067// Whether or not this call contains errors, either in the call or the
9068// arguments to the call.
9069
9070bool
9071Call_expression::is_erroneous_call()
9072{
9073 if (this->is_error_expression() || this->fn()->is_error_expression())
9074 return true;
9075
9076 if (this->args() == NULL)
9077 return false;
9078 for (Expression_list::iterator pa = this->args()->begin();
9079 pa != this->args()->end();
9080 ++pa)
9081 {
9082 if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression())
9083 return true;
9084 }
9085 return false;
9086}
9087
e440a328 9088// Get the type.
9089
9090Type*
9091Call_expression::do_type()
9092{
9093 if (this->type_ != NULL)
9094 return this->type_;
9095
9096 Type* ret;
9097 Function_type* fntype = this->get_function_type();
9098 if (fntype == NULL)
9099 return Type::make_error_type();
9100
9101 const Typed_identifier_list* results = fntype->results();
9102 if (results == NULL)
9103 ret = Type::make_void_type();
9104 else if (results->size() == 1)
9105 ret = results->begin()->type();
9106 else
9107 ret = Type::make_call_multiple_result_type(this);
9108
9109 this->type_ = ret;
9110
9111 return this->type_;
9112}
9113
9114// Determine types for a call expression. We can use the function
9115// parameter types to set the types of the arguments.
9116
9117void
9118Call_expression::do_determine_type(const Type_context*)
9119{
fb94b0ca 9120 if (!this->determining_types())
9121 return;
9122
e440a328 9123 this->fn_->determine_type_no_context();
9124 Function_type* fntype = this->get_function_type();
9125 const Typed_identifier_list* parameters = NULL;
9126 if (fntype != NULL)
9127 parameters = fntype->parameters();
9128 if (this->args_ != NULL)
9129 {
9130 Typed_identifier_list::const_iterator pt;
9131 if (parameters != NULL)
9132 pt = parameters->begin();
09ea332d 9133 bool first = true;
e440a328 9134 for (Expression_list::const_iterator pa = this->args_->begin();
9135 pa != this->args_->end();
9136 ++pa)
9137 {
09ea332d 9138 if (first)
9139 {
9140 first = false;
9141 // If this is a method, the first argument is the
9142 // receiver.
9143 if (fntype != NULL && fntype->is_method())
9144 {
9145 Type* rtype = fntype->receiver()->type();
9146 // The receiver is always passed as a pointer.
9147 if (rtype->points_to() == NULL)
9148 rtype = Type::make_pointer_type(rtype);
9149 Type_context subcontext(rtype, false);
9150 (*pa)->determine_type(&subcontext);
9151 continue;
9152 }
9153 }
9154
e440a328 9155 if (parameters != NULL && pt != parameters->end())
9156 {
9157 Type_context subcontext(pt->type(), false);
9158 (*pa)->determine_type(&subcontext);
9159 ++pt;
9160 }
9161 else
9162 (*pa)->determine_type_no_context();
9163 }
9164 }
9165}
9166
fb94b0ca 9167// Called when determining types for a Call_expression. Return true
9168// if we should go ahead, false if they have already been determined.
9169
9170bool
9171Call_expression::determining_types()
9172{
9173 if (this->types_are_determined_)
9174 return false;
9175 else
9176 {
9177 this->types_are_determined_ = true;
9178 return true;
9179 }
9180}
9181
e440a328 9182// Check types for parameter I.
9183
9184bool
9185Call_expression::check_argument_type(int i, const Type* parameter_type,
9186 const Type* argument_type,
b13c66cd 9187 Location argument_location,
e440a328 9188 bool issued_error)
9189{
9190 std::string reason;
1eae365b 9191 if (!Type::are_assignable(parameter_type, argument_type, &reason))
e440a328 9192 {
9193 if (!issued_error)
9194 {
9195 if (reason.empty())
9196 error_at(argument_location, "argument %d has incompatible type", i);
9197 else
9198 error_at(argument_location,
9199 "argument %d has incompatible type (%s)",
9200 i, reason.c_str());
9201 }
9202 this->set_is_error();
9203 return false;
9204 }
9205 return true;
9206}
9207
9208// Check types.
9209
9210void
9211Call_expression::do_check_types(Gogo*)
9212{
a6645f74 9213 if (this->classification() == EXPRESSION_ERROR)
9214 return;
9215
e440a328 9216 Function_type* fntype = this->get_function_type();
9217 if (fntype == NULL)
9218 {
5c13bd80 9219 if (!this->fn_->type()->is_error())
e440a328 9220 this->report_error(_("expected function"));
9221 return;
9222 }
9223
1373401e 9224 if (this->expected_result_count_ != 0
9225 && this->expected_result_count_ != this->result_count())
9226 {
9227 if (this->issue_error())
9228 this->report_error(_("function result count mismatch"));
9229 this->set_is_error();
9230 return;
9231 }
9232
09ea332d 9233 bool is_method = fntype->is_method();
9234 if (is_method)
e440a328 9235 {
09ea332d 9236 go_assert(this->args_ != NULL && !this->args_->empty());
9237 Type* rtype = fntype->receiver()->type();
9238 Expression* first_arg = this->args_->front();
1eae365b 9239 // We dereference the values since receivers are always passed
9240 // as pointers.
09ea332d 9241 std::string reason;
1eae365b 9242 if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(),
9243 &reason))
e440a328 9244 {
09ea332d 9245 if (reason.empty())
9246 this->report_error(_("incompatible type for receiver"));
9247 else
e440a328 9248 {
09ea332d 9249 error_at(this->location(),
9250 "incompatible type for receiver (%s)",
9251 reason.c_str());
9252 this->set_is_error();
e440a328 9253 }
9254 }
9255 }
9256
9257 // Note that varargs was handled by the lower_varargs() method, so
a6645f74 9258 // we don't have to worry about it here unless something is wrong.
9259 if (this->is_varargs_ && !this->varargs_are_lowered_)
9260 {
9261 if (!fntype->is_varargs())
9262 {
9263 error_at(this->location(),
9264 _("invalid use of %<...%> calling non-variadic function"));
9265 this->set_is_error();
9266 return;
9267 }
9268 }
e440a328 9269
9270 const Typed_identifier_list* parameters = fntype->parameters();
9271 if (this->args_ == NULL)
9272 {
9273 if (parameters != NULL && !parameters->empty())
9274 this->report_error(_("not enough arguments"));
9275 }
9276 else if (parameters == NULL)
09ea332d 9277 {
9278 if (!is_method || this->args_->size() > 1)
9279 this->report_error(_("too many arguments"));
9280 }
1373401e 9281 else if (this->args_->size() == 1
9282 && this->args_->front()->call_expression() != NULL
9283 && this->args_->front()->call_expression()->result_count() > 1)
9284 {
9285 // This is F(G()) when G returns more than one result. If the
9286 // results can be matched to parameters, it would have been
9287 // lowered in do_lower. If we get here we know there is a
9288 // mismatch.
9289 if (this->args_->front()->call_expression()->result_count()
9290 < parameters->size())
9291 this->report_error(_("not enough arguments"));
9292 else
9293 this->report_error(_("too many arguments"));
9294 }
e440a328 9295 else
9296 {
9297 int i = 0;
09ea332d 9298 Expression_list::const_iterator pa = this->args_->begin();
9299 if (is_method)
9300 ++pa;
9301 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9302 pt != parameters->end();
9303 ++pt, ++pa, ++i)
e440a328 9304 {
09ea332d 9305 if (pa == this->args_->end())
e440a328 9306 {
09ea332d 9307 this->report_error(_("not enough arguments"));
e440a328 9308 return;
9309 }
9310 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9311 (*pa)->location(), false);
9312 }
09ea332d 9313 if (pa != this->args_->end())
9314 this->report_error(_("too many arguments"));
e440a328 9315 }
9316}
9317
72666aed 9318Expression*
9319Call_expression::do_copy()
9320{
9321 Call_expression* call =
9322 Expression::make_call(this->fn_->copy(),
9323 (this->args_ == NULL
9324 ? NULL
9325 : this->args_->copy()),
9326 this->is_varargs_, this->location());
9327
9328 if (this->varargs_are_lowered_)
9329 call->set_varargs_are_lowered();
9330 return call;
9331}
9332
e440a328 9333// Return whether we have to use a temporary variable to ensure that
9334// we evaluate this call expression in order. If the call returns no
ceeb4318 9335// results then it will inevitably be executed last.
e440a328 9336
9337bool
9338Call_expression::do_must_eval_in_order() const
9339{
ceeb4318 9340 return this->result_count() > 0;
e440a328 9341}
9342
e440a328 9343// Get the function and the first argument to use when calling an
9344// interface method.
9345
2387f644 9346Expression*
e440a328 9347Call_expression::interface_method_function(
e440a328 9348 Interface_field_reference_expression* interface_method,
2387f644 9349 Expression** first_arg_ptr)
e440a328 9350{
2387f644 9351 *first_arg_ptr = interface_method->get_underlying_object();
9352 return interface_method->get_function();
e440a328 9353}
9354
9355// Build the call expression.
9356
ea664253 9357Bexpression*
9358Call_expression::do_get_backend(Translate_context* context)
e440a328 9359{
2c809f8f 9360 if (this->call_ != NULL)
ea664253 9361 return this->call_;
e440a328 9362
9363 Function_type* fntype = this->get_function_type();
9364 if (fntype == NULL)
ea664253 9365 return context->backend()->error_expression();
e440a328 9366
9367 if (this->fn_->is_error_expression())
ea664253 9368 return context->backend()->error_expression();
e440a328 9369
9370 Gogo* gogo = context->gogo();
b13c66cd 9371 Location location = this->location();
e440a328 9372
9373 Func_expression* func = this->fn_->func_expression();
e440a328 9374 Interface_field_reference_expression* interface_method =
9375 this->fn_->interface_field_reference_expression();
9376 const bool has_closure = func != NULL && func->closure() != NULL;
09ea332d 9377 const bool is_interface_method = interface_method != NULL;
e440a328 9378
f8bdf81a 9379 bool has_closure_arg;
8381eda7 9380 if (has_closure)
f8bdf81a 9381 has_closure_arg = true;
8381eda7 9382 else if (func != NULL)
f8bdf81a 9383 has_closure_arg = false;
8381eda7 9384 else if (is_interface_method)
f8bdf81a 9385 has_closure_arg = false;
8381eda7 9386 else
f8bdf81a 9387 has_closure_arg = true;
8381eda7 9388
e440a328 9389 int nargs;
2c809f8f 9390 std::vector<Bexpression*> fn_args;
e440a328 9391 if (this->args_ == NULL || this->args_->empty())
9392 {
f8bdf81a 9393 nargs = is_interface_method ? 1 : 0;
2c809f8f 9394 if (nargs > 0)
9395 fn_args.resize(1);
e440a328 9396 }
09ea332d 9397 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9398 {
9399 // Passing a receiver parameter.
9400 go_assert(!is_interface_method
9401 && fntype->is_method()
9402 && this->args_->size() == 1);
f8bdf81a 9403 nargs = 1;
2c809f8f 9404 fn_args.resize(1);
ea664253 9405 fn_args[0] = this->args_->front()->get_backend(context);
09ea332d 9406 }
e440a328 9407 else
9408 {
9409 const Typed_identifier_list* params = fntype->parameters();
e440a328 9410
9411 nargs = this->args_->size();
09ea332d 9412 int i = is_interface_method ? 1 : 0;
e440a328 9413 nargs += i;
2c809f8f 9414 fn_args.resize(nargs);
e440a328 9415
9416 Typed_identifier_list::const_iterator pp = params->begin();
09ea332d 9417 Expression_list::const_iterator pe = this->args_->begin();
9418 if (!is_interface_method && fntype->is_method())
9419 {
ea664253 9420 fn_args[i] = (*pe)->get_backend(context);
09ea332d 9421 ++pe;
9422 ++i;
9423 }
9424 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
e440a328 9425 {
c484d925 9426 go_assert(pp != params->end());
2c809f8f 9427 Expression* arg =
9428 Expression::convert_for_assignment(gogo, pp->type(), *pe,
9429 location);
ea664253 9430 fn_args[i] = arg->get_backend(context);
e440a328 9431 }
c484d925 9432 go_assert(pp == params->end());
f8bdf81a 9433 go_assert(i == nargs);
e440a328 9434 }
9435
2c809f8f 9436 Expression* fn;
9437 Expression* closure = NULL;
8381eda7 9438 if (func != NULL)
9439 {
9440 Named_object* no = func->named_object();
2c809f8f 9441 fn = Expression::make_func_code_reference(no, location);
9442 if (has_closure)
9443 closure = func->closure();
8381eda7 9444 }
09ea332d 9445 else if (!is_interface_method)
8381eda7 9446 {
2c809f8f 9447 closure = this->fn_;
9448
9449 // The backend representation of this function type is a pointer
9450 // to a struct whose first field is the actual function to call.
9451 Type* pfntype =
9452 Type::make_pointer_type(
9453 Type::make_pointer_type(Type::make_void_type()));
9454 fn = Expression::make_unsafe_cast(pfntype, this->fn_, location);
9455 fn = Expression::make_unary(OPERATOR_MULT, fn, location);
9456 }
e440a328 9457 else
cf609de4 9458 {
2387f644 9459 Expression* first_arg;
2c809f8f 9460 fn = this->interface_method_function(interface_method, &first_arg);
ea664253 9461 fn_args[0] = first_arg->get_backend(context);
e440a328 9462 }
9463
1ecc6157 9464 Bexpression* bclosure = NULL;
9465 if (has_closure_arg)
9466 bclosure = closure->get_backend(context);
f8bdf81a 9467 else
1ecc6157 9468 go_assert(closure == NULL);
f8bdf81a 9469
ea664253 9470 Bexpression* bfn = fn->get_backend(context);
80d1e1a8 9471
9472 // When not calling a named function directly, use a type conversion
9473 // in case the type of the function is a recursive type which refers
9474 // to itself. We don't do this for an interface method because 1)
9475 // an interface method never refers to itself, so we always have a
9476 // function type here; 2) we pass an extra first argument to an
9477 // interface method, so fntype is not correct.
9478 if (func == NULL && !is_interface_method)
9479 {
9480 Btype* bft = fntype->get_backend_fntype(gogo);
9481 bfn = gogo->backend()->convert_expression(bft, bfn, location);
9482 }
9483
1ecc6157 9484 Bexpression* call = gogo->backend()->call_expression(bfn, fn_args,
9485 bclosure, location);
e440a328 9486
2c809f8f 9487 if (this->results_ != NULL)
e440a328 9488 {
2c809f8f 9489 go_assert(this->call_temp_ != NULL);
9490 Expression* call_ref =
9491 Expression::make_temporary_reference(this->call_temp_, location);
ea664253 9492 Bexpression* bcall_ref = call_ref->get_backend(context);
2c809f8f 9493 Bstatement* assn_stmt =
9494 gogo->backend()->assignment_statement(bcall_ref, call, location);
e440a328 9495
2c809f8f 9496 this->call_ = this->set_results(context, bcall_ref);
e440a328 9497
2c809f8f 9498 Bexpression* set_and_call =
9499 gogo->backend()->compound_expression(assn_stmt, this->call_,
9500 location);
ea664253 9501 return set_and_call;
2c809f8f 9502 }
e440a328 9503
2c809f8f 9504 this->call_ = call;
ea664253 9505 return this->call_;
e440a328 9506}
9507
ceeb4318 9508// Set the result variables if this call returns multiple results.
9509
2c809f8f 9510Bexpression*
9511Call_expression::set_results(Translate_context* context, Bexpression* call)
ceeb4318 9512{
2c809f8f 9513 Gogo* gogo = context->gogo();
ceeb4318 9514
2c809f8f 9515 Bexpression* results = NULL;
b13c66cd 9516 Location loc = this->location();
2c809f8f 9517
ceeb4318 9518 size_t rc = this->result_count();
2c809f8f 9519 for (size_t i = 0; i < rc; ++i)
ceeb4318 9520 {
ceeb4318 9521 Temporary_statement* temp = this->result(i);
cd238b8d 9522 if (temp == NULL)
9523 {
9524 go_assert(saw_errors());
2c809f8f 9525 return gogo->backend()->error_expression();
cd238b8d 9526 }
ceeb4318 9527 Temporary_reference_expression* ref =
9528 Expression::make_temporary_reference(temp, loc);
9529 ref->set_is_lvalue();
ceeb4318 9530
ea664253 9531 Bexpression* result_ref = ref->get_backend(context);
2c809f8f 9532 Bexpression* call_result =
9533 gogo->backend()->struct_field_expression(call, i, loc);
9534 Bstatement* assn_stmt =
9535 gogo->backend()->assignment_statement(result_ref, call_result, loc);
ceeb4318 9536
2c809f8f 9537 Bexpression* result =
9538 gogo->backend()->compound_expression(assn_stmt, call_result, loc);
ceeb4318 9539
2c809f8f 9540 if (results == NULL)
9541 results = result;
9542 else
9543 {
9544 Bstatement* expr_stmt = gogo->backend()->expression_statement(result);
9545 results =
9546 gogo->backend()->compound_expression(expr_stmt, results, loc);
9547 }
9548 }
9549 return results;
ceeb4318 9550}
9551
d751bb78 9552// Dump ast representation for a call expressin.
9553
9554void
9555Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
9556{
9557 this->fn_->dump_expression(ast_dump_context);
9558 ast_dump_context->ostream() << "(";
9559 if (args_ != NULL)
9560 ast_dump_context->dump_expression_list(this->args_);
9561
9562 ast_dump_context->ostream() << ") ";
9563}
9564
e440a328 9565// Make a call expression.
9566
9567Call_expression*
9568Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
b13c66cd 9569 Location location)
e440a328 9570{
9571 return new Call_expression(fn, args, is_varargs, location);
9572}
9573
da244e59 9574// Class Call_result_expression.
e440a328 9575
9576// Traverse a call result.
9577
9578int
9579Call_result_expression::do_traverse(Traverse* traverse)
9580{
9581 if (traverse->remember_expression(this->call_))
9582 {
9583 // We have already traversed the call expression.
9584 return TRAVERSE_CONTINUE;
9585 }
9586 return Expression::traverse(&this->call_, traverse);
9587}
9588
9589// Get the type.
9590
9591Type*
9592Call_result_expression::do_type()
9593{
425dd051 9594 if (this->classification() == EXPRESSION_ERROR)
9595 return Type::make_error_type();
9596
e440a328 9597 // THIS->CALL_ can be replaced with a temporary reference due to
9598 // Call_expression::do_must_eval_in_order when there is an error.
9599 Call_expression* ce = this->call_->call_expression();
9600 if (ce == NULL)
5e85f268 9601 {
9602 this->set_is_error();
9603 return Type::make_error_type();
9604 }
e440a328 9605 Function_type* fntype = ce->get_function_type();
9606 if (fntype == NULL)
5e85f268 9607 {
e37658e2 9608 if (ce->issue_error())
99b3f06f 9609 {
9610 if (!ce->fn()->type()->is_error())
9611 this->report_error(_("expected function"));
9612 }
5e85f268 9613 this->set_is_error();
9614 return Type::make_error_type();
9615 }
e440a328 9616 const Typed_identifier_list* results = fntype->results();
ceeb4318 9617 if (results == NULL || results->size() < 2)
7b8d861f 9618 {
ceeb4318 9619 if (ce->issue_error())
9620 this->report_error(_("number of results does not match "
9621 "number of values"));
7b8d861f 9622 return Type::make_error_type();
9623 }
e440a328 9624 Typed_identifier_list::const_iterator pr = results->begin();
9625 for (unsigned int i = 0; i < this->index_; ++i)
9626 {
9627 if (pr == results->end())
425dd051 9628 break;
e440a328 9629 ++pr;
9630 }
9631 if (pr == results->end())
425dd051 9632 {
ceeb4318 9633 if (ce->issue_error())
9634 this->report_error(_("number of results does not match "
9635 "number of values"));
425dd051 9636 return Type::make_error_type();
9637 }
e440a328 9638 return pr->type();
9639}
9640
425dd051 9641// Check the type. Just make sure that we trigger the warning in
9642// do_type.
e440a328 9643
9644void
9645Call_result_expression::do_check_types(Gogo*)
9646{
425dd051 9647 this->type();
e440a328 9648}
9649
9650// Determine the type. We have nothing to do here, but the 0 result
9651// needs to pass down to the caller.
9652
9653void
9654Call_result_expression::do_determine_type(const Type_context*)
9655{
fb94b0ca 9656 this->call_->determine_type_no_context();
e440a328 9657}
9658
ea664253 9659// Return the backend representation. We just refer to the temporary set by the
9660// call expression. We don't do this at lowering time because it makes it
ceeb4318 9661// hard to evaluate the call at the right time.
e440a328 9662
ea664253 9663Bexpression*
9664Call_result_expression::do_get_backend(Translate_context* context)
e440a328 9665{
ceeb4318 9666 Call_expression* ce = this->call_->call_expression();
cd238b8d 9667 if (ce == NULL)
9668 {
9669 go_assert(this->call_->is_error_expression());
ea664253 9670 return context->backend()->error_expression();
cd238b8d 9671 }
ceeb4318 9672 Temporary_statement* ts = ce->result(this->index_);
cd238b8d 9673 if (ts == NULL)
9674 {
9675 go_assert(saw_errors());
ea664253 9676 return context->backend()->error_expression();
cd238b8d 9677 }
ceeb4318 9678 Expression* ref = Expression::make_temporary_reference(ts, this->location());
ea664253 9679 return ref->get_backend(context);
e440a328 9680}
9681
d751bb78 9682// Dump ast representation for a call result expression.
9683
9684void
9685Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9686 const
9687{
9688 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9689 // (struct) and the fields are referenced instead.
9690 ast_dump_context->ostream() << this->index_ << "@(";
9691 ast_dump_context->dump_expression(this->call_);
9692 ast_dump_context->ostream() << ")";
9693}
9694
e440a328 9695// Make a reference to a single result of a call which returns
9696// multiple results.
9697
9698Expression*
9699Expression::make_call_result(Call_expression* call, unsigned int index)
9700{
9701 return new Call_result_expression(call, index);
9702}
9703
9704// Class Index_expression.
9705
9706// Traversal.
9707
9708int
9709Index_expression::do_traverse(Traverse* traverse)
9710{
9711 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9712 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9713 || (this->end_ != NULL
acf2b673 9714 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9715 || (this->cap_ != NULL
9716 && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT))
e440a328 9717 return TRAVERSE_EXIT;
9718 return TRAVERSE_CONTINUE;
9719}
9720
9721// Lower an index expression. This converts the generic index
9722// expression into an array index, a string index, or a map index.
9723
9724Expression*
ceeb4318 9725Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
e440a328 9726{
b13c66cd 9727 Location location = this->location();
e440a328 9728 Expression* left = this->left_;
9729 Expression* start = this->start_;
9730 Expression* end = this->end_;
acf2b673 9731 Expression* cap = this->cap_;
e440a328 9732
9733 Type* type = left->type();
5c13bd80 9734 if (type->is_error())
d9f3743a 9735 {
9736 go_assert(saw_errors());
9737 return Expression::make_error(location);
9738 }
b0cf7ddd 9739 else if (left->is_type_expression())
9740 {
9741 error_at(location, "attempt to index type expression");
9742 return Expression::make_error(location);
9743 }
e440a328 9744 else if (type->array_type() != NULL)
acf2b673 9745 return Expression::make_array_index(left, start, end, cap, location);
e440a328 9746 else if (type->points_to() != NULL
9747 && type->points_to()->array_type() != NULL
411eb89e 9748 && !type->points_to()->is_slice_type())
e440a328 9749 {
9750 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9751 location);
38092374 9752
9753 // For an ordinary index into the array, the pointer will be
9754 // dereferenced. For a slice it will not--the resulting slice
9755 // will simply reuse the pointer, which is incorrect if that
9756 // pointer is nil.
9757 if (end != NULL || cap != NULL)
9758 deref->issue_nil_check();
9759
acf2b673 9760 return Expression::make_array_index(deref, start, end, cap, location);
e440a328 9761 }
9762 else if (type->is_string_type())
acf2b673 9763 {
9764 if (cap != NULL)
9765 {
9766 error_at(location, "invalid 3-index slice of string");
9767 return Expression::make_error(location);
9768 }
9769 return Expression::make_string_index(left, start, end, location);
9770 }
e440a328 9771 else if (type->map_type() != NULL)
9772 {
acf2b673 9773 if (end != NULL || cap != NULL)
e440a328 9774 {
9775 error_at(location, "invalid slice of map");
9776 return Expression::make_error(location);
9777 }
6d4c2432 9778 Map_index_expression* ret = Expression::make_map_index(left, start,
9779 location);
e440a328 9780 if (this->is_lvalue_)
9781 ret->set_is_lvalue();
9782 return ret;
9783 }
9784 else
9785 {
9786 error_at(location,
9787 "attempt to index object which is not array, string, or map");
9788 return Expression::make_error(location);
9789 }
9790}
9791
acf2b673 9792// Write an indexed expression
9793// (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
d751bb78 9794
9795void
9796Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
9797 const Expression* expr,
9798 const Expression* start,
acf2b673 9799 const Expression* end,
9800 const Expression* cap)
d751bb78 9801{
9802 expr->dump_expression(ast_dump_context);
9803 ast_dump_context->ostream() << "[";
9804 start->dump_expression(ast_dump_context);
9805 if (end != NULL)
9806 {
9807 ast_dump_context->ostream() << ":";
9808 end->dump_expression(ast_dump_context);
9809 }
acf2b673 9810 if (cap != NULL)
9811 {
9812 ast_dump_context->ostream() << ":";
9813 cap->dump_expression(ast_dump_context);
9814 }
d751bb78 9815 ast_dump_context->ostream() << "]";
9816}
9817
9818// Dump ast representation for an index expression.
9819
9820void
9821Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9822 const
9823{
9824 Index_expression::dump_index_expression(ast_dump_context, this->left_,
acf2b673 9825 this->start_, this->end_, this->cap_);
d751bb78 9826}
9827
e440a328 9828// Make an index expression.
9829
9830Expression*
9831Expression::make_index(Expression* left, Expression* start, Expression* end,
acf2b673 9832 Expression* cap, Location location)
e440a328 9833{
acf2b673 9834 return new Index_expression(left, start, end, cap, location);
e440a328 9835}
9836
da244e59 9837// Class Array_index_expression.
e440a328 9838
9839// Array index traversal.
9840
9841int
9842Array_index_expression::do_traverse(Traverse* traverse)
9843{
9844 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9845 return TRAVERSE_EXIT;
9846 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9847 return TRAVERSE_EXIT;
9848 if (this->end_ != NULL)
9849 {
9850 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9851 return TRAVERSE_EXIT;
9852 }
acf2b673 9853 if (this->cap_ != NULL)
9854 {
9855 if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
9856 return TRAVERSE_EXIT;
9857 }
e440a328 9858 return TRAVERSE_CONTINUE;
9859}
9860
9861// Return the type of an array index.
9862
9863Type*
9864Array_index_expression::do_type()
9865{
9866 if (this->type_ == NULL)
9867 {
9868 Array_type* type = this->array_->type()->array_type();
9869 if (type == NULL)
9870 this->type_ = Type::make_error_type();
9871 else if (this->end_ == NULL)
9872 this->type_ = type->element_type();
411eb89e 9873 else if (type->is_slice_type())
e440a328 9874 {
9875 // A slice of a slice has the same type as the original
9876 // slice.
9877 this->type_ = this->array_->type()->deref();
9878 }
9879 else
9880 {
9881 // A slice of an array is a slice.
9882 this->type_ = Type::make_array_type(type->element_type(), NULL);
9883 }
9884 }
9885 return this->type_;
9886}
9887
9888// Set the type of an array index.
9889
9890void
9891Array_index_expression::do_determine_type(const Type_context*)
9892{
9893 this->array_->determine_type_no_context();
f77aa642 9894
9895 Type_context index_context(Type::lookup_integer_type("int"), false);
9896 if (this->start_->is_constant())
9897 this->start_->determine_type(&index_context);
9898 else
9899 this->start_->determine_type_no_context();
e440a328 9900 if (this->end_ != NULL)
f77aa642 9901 {
9902 if (this->end_->is_constant())
9903 this->end_->determine_type(&index_context);
9904 else
9905 this->end_->determine_type_no_context();
9906 }
acf2b673 9907 if (this->cap_ != NULL)
f77aa642 9908 {
9909 if (this->cap_->is_constant())
9910 this->cap_->determine_type(&index_context);
9911 else
9912 this->cap_->determine_type_no_context();
9913 }
e440a328 9914}
9915
9916// Check types of an array index.
9917
9918void
9919Array_index_expression::do_check_types(Gogo*)
9920{
f6bc81e6 9921 Numeric_constant nc;
9922 unsigned long v;
9923 if (this->start_->type()->integer_type() == NULL
9924 && !this->start_->type()->is_error()
9925 && (!this->start_->numeric_constant_value(&nc)
9926 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 9927 this->report_error(_("index must be integer"));
9928 if (this->end_ != NULL
9929 && this->end_->type()->integer_type() == NULL
99b3f06f 9930 && !this->end_->type()->is_error()
9931 && !this->end_->is_nil_expression()
f6bc81e6 9932 && !this->end_->is_error_expression()
9933 && (!this->end_->numeric_constant_value(&nc)
9934 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 9935 this->report_error(_("slice end must be integer"));
acf2b673 9936 if (this->cap_ != NULL
9937 && this->cap_->type()->integer_type() == NULL
9938 && !this->cap_->type()->is_error()
9939 && !this->cap_->is_nil_expression()
9940 && !this->cap_->is_error_expression()
9941 && (!this->cap_->numeric_constant_value(&nc)
9942 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
9943 this->report_error(_("slice capacity must be integer"));
e440a328 9944
9945 Array_type* array_type = this->array_->type()->array_type();
f9c68f17 9946 if (array_type == NULL)
9947 {
c484d925 9948 go_assert(this->array_->type()->is_error());
f9c68f17 9949 return;
9950 }
e440a328 9951
9952 unsigned int int_bits =
9953 Type::lookup_integer_type("int")->integer_type()->bits();
9954
0c77715b 9955 Numeric_constant lvalnc;
e440a328 9956 mpz_t lval;
e440a328 9957 bool lval_valid = (array_type->length() != NULL
0c77715b 9958 && array_type->length()->numeric_constant_value(&lvalnc)
9959 && lvalnc.to_int(&lval));
9960 Numeric_constant inc;
e440a328 9961 mpz_t ival;
0bd5d859 9962 bool ival_valid = false;
0c77715b 9963 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 9964 {
0bd5d859 9965 ival_valid = true;
e440a328 9966 if (mpz_sgn(ival) < 0
9967 || mpz_sizeinbase(ival, 2) >= int_bits
9968 || (lval_valid
9969 && (this->end_ == NULL
9970 ? mpz_cmp(ival, lval) >= 0
9971 : mpz_cmp(ival, lval) > 0)))
9972 {
9973 error_at(this->start_->location(), "array index out of bounds");
9974 this->set_is_error();
9975 }
9976 }
9977 if (this->end_ != NULL && !this->end_->is_nil_expression())
9978 {
0c77715b 9979 Numeric_constant enc;
9980 mpz_t eval;
acf2b673 9981 bool eval_valid = false;
0c77715b 9982 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 9983 {
acf2b673 9984 eval_valid = true;
0c77715b 9985 if (mpz_sgn(eval) < 0
9986 || mpz_sizeinbase(eval, 2) >= int_bits
9987 || (lval_valid && mpz_cmp(eval, lval) > 0))
e440a328 9988 {
9989 error_at(this->end_->location(), "array index out of bounds");
9990 this->set_is_error();
9991 }
0bd5d859 9992 else if (ival_valid && mpz_cmp(ival, eval) > 0)
9993 this->report_error(_("inverted slice range"));
e440a328 9994 }
acf2b673 9995
9996 Numeric_constant cnc;
9997 mpz_t cval;
9998 if (this->cap_ != NULL
9999 && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval))
10000 {
10001 if (mpz_sgn(cval) < 0
10002 || mpz_sizeinbase(cval, 2) >= int_bits
10003 || (lval_valid && mpz_cmp(cval, lval) > 0))
10004 {
10005 error_at(this->cap_->location(), "array index out of bounds");
10006 this->set_is_error();
10007 }
10008 else if (ival_valid && mpz_cmp(ival, cval) > 0)
10009 {
10010 error_at(this->cap_->location(),
10011 "invalid slice index: capacity less than start");
10012 this->set_is_error();
10013 }
10014 else if (eval_valid && mpz_cmp(eval, cval) > 0)
10015 {
10016 error_at(this->cap_->location(),
10017 "invalid slice index: capacity less than length");
10018 this->set_is_error();
10019 }
10020 mpz_clear(cval);
10021 }
10022
10023 if (eval_valid)
10024 mpz_clear(eval);
e440a328 10025 }
0bd5d859 10026 if (ival_valid)
10027 mpz_clear(ival);
0c77715b 10028 if (lval_valid)
10029 mpz_clear(lval);
e440a328 10030
10031 // A slice of an array requires an addressable array. A slice of a
10032 // slice is always possible.
411eb89e 10033 if (this->end_ != NULL && !array_type->is_slice_type())
88ec30c8 10034 {
10035 if (!this->array_->is_addressable())
8da39c3b 10036 this->report_error(_("slice of unaddressable value"));
88ec30c8 10037 else
10038 this->array_->address_taken(true);
10039 }
e440a328 10040}
10041
2c809f8f 10042// Flatten array indexing by using temporary variables for slices and indexes.
35a54f17 10043
10044Expression*
10045Array_index_expression::do_flatten(Gogo*, Named_object*,
10046 Statement_inserter* inserter)
10047{
10048 Location loc = this->location();
5bf8be8b 10049 Expression* array = this->array_;
10050 Expression* start = this->start_;
10051 Expression* end = this->end_;
10052 Expression* cap = this->cap_;
10053 if (array->is_error_expression()
10054 || array->type()->is_error_type()
10055 || start->is_error_expression()
10056 || start->type()->is_error_type()
10057 || (end != NULL
10058 && (end->is_error_expression() || end->type()->is_error_type()))
10059 || (cap != NULL
10060 && (cap->is_error_expression() || cap->type()->is_error_type())))
10061 {
10062 go_assert(saw_errors());
10063 return Expression::make_error(loc);
10064 }
10065
2c809f8f 10066 Temporary_statement* temp;
5bf8be8b 10067 if (array->type()->is_slice_type() && !array->is_variable())
35a54f17 10068 {
5bf8be8b 10069 temp = Statement::make_temporary(NULL, array, loc);
35a54f17 10070 inserter->insert(temp);
10071 this->array_ = Expression::make_temporary_reference(temp, loc);
10072 }
5bf8be8b 10073 if (!start->is_variable())
2c809f8f 10074 {
5bf8be8b 10075 temp = Statement::make_temporary(NULL, start, loc);
2c809f8f 10076 inserter->insert(temp);
10077 this->start_ = Expression::make_temporary_reference(temp, loc);
10078 }
5bf8be8b 10079 if (end != NULL
10080 && !end->is_nil_expression()
10081 && !end->is_variable())
2c809f8f 10082 {
5bf8be8b 10083 temp = Statement::make_temporary(NULL, end, loc);
2c809f8f 10084 inserter->insert(temp);
10085 this->end_ = Expression::make_temporary_reference(temp, loc);
10086 }
5bf8be8b 10087 if (cap!= NULL && !cap->is_variable())
2c809f8f 10088 {
5bf8be8b 10089 temp = Statement::make_temporary(NULL, cap, loc);
2c809f8f 10090 inserter->insert(temp);
10091 this->cap_ = Expression::make_temporary_reference(temp, loc);
10092 }
10093
35a54f17 10094 return this;
10095}
10096
e440a328 10097// Return whether this expression is addressable.
10098
10099bool
10100Array_index_expression::do_is_addressable() const
10101{
10102 // A slice expression is not addressable.
10103 if (this->end_ != NULL)
10104 return false;
10105
10106 // An index into a slice is addressable.
411eb89e 10107 if (this->array_->type()->is_slice_type())
e440a328 10108 return true;
10109
10110 // An index into an array is addressable if the array is
10111 // addressable.
10112 return this->array_->is_addressable();
10113}
10114
ea664253 10115// Get the backend representation for an array index.
e440a328 10116
ea664253 10117Bexpression*
10118Array_index_expression::do_get_backend(Translate_context* context)
e440a328 10119{
e440a328 10120 Array_type* array_type = this->array_->type()->array_type();
d8cd8e2d 10121 if (array_type == NULL)
10122 {
c484d925 10123 go_assert(this->array_->type()->is_error());
ea664253 10124 return context->backend()->error_expression();
d8cd8e2d 10125 }
35a54f17 10126 go_assert(!array_type->is_slice_type() || this->array_->is_variable());
e440a328 10127
2c809f8f 10128 Location loc = this->location();
10129 Gogo* gogo = context->gogo();
10130
6dfedc16 10131 Type* int_type = Type::lookup_integer_type("int");
10132 Btype* int_btype = int_type->get_backend(gogo);
e440a328 10133
2c809f8f 10134 // We need to convert the length and capacity to the Go "int" type here
10135 // because the length of a fixed-length array could be of type "uintptr"
10136 // and gimple disallows binary operations between "uintptr" and other
10137 // integer types. FIXME.
10138 Bexpression* length = NULL;
a04bfdfc 10139 if (this->end_ == NULL || this->end_->is_nil_expression())
10140 {
35a54f17 10141 Expression* len = array_type->get_length(gogo, this->array_);
ea664253 10142 length = len->get_backend(context);
2c809f8f 10143 length = gogo->backend()->convert_expression(int_btype, length, loc);
a04bfdfc 10144 }
10145
2c809f8f 10146 Bexpression* capacity = NULL;
a04bfdfc 10147 if (this->end_ != NULL)
10148 {
35a54f17 10149 Expression* cap = array_type->get_capacity(gogo, this->array_);
ea664253 10150 capacity = cap->get_backend(context);
2c809f8f 10151 capacity = gogo->backend()->convert_expression(int_btype, capacity, loc);
a04bfdfc 10152 }
10153
2c809f8f 10154 Bexpression* cap_arg = capacity;
acf2b673 10155 if (this->cap_ != NULL)
10156 {
ea664253 10157 cap_arg = this->cap_->get_backend(context);
2c809f8f 10158 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
acf2b673 10159 }
10160
2c809f8f 10161 if (length == NULL)
10162 length = cap_arg;
e440a328 10163
10164 int code = (array_type->length() != NULL
10165 ? (this->end_ == NULL
10166 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10167 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10168 : (this->end_ == NULL
10169 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10170 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
ea664253 10171 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context);
2c809f8f 10172
6dfedc16 10173 if (this->start_->type()->integer_type() == NULL
10174 && !Type::are_convertible(int_type, this->start_->type(), NULL))
10175 {
10176 go_assert(saw_errors());
10177 return context->backend()->error_expression();
10178 }
d9f3743a 10179
ea664253 10180 Bexpression* bad_index =
d9f3743a 10181 Expression::check_bounds(this->start_, loc)->get_backend(context);
2c809f8f 10182
ea664253 10183 Bexpression* start = this->start_->get_backend(context);
2c809f8f 10184 start = gogo->backend()->convert_expression(int_btype, start, loc);
10185 Bexpression* start_too_large =
10186 gogo->backend()->binary_expression((this->end_ == NULL
10187 ? OPERATOR_GE
10188 : OPERATOR_GT),
10189 start,
10190 (this->end_ == NULL
10191 ? length
10192 : capacity),
10193 loc);
10194 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large,
10195 bad_index, loc);
e440a328 10196
10197 if (this->end_ == NULL)
10198 {
10199 // Simple array indexing. This has to return an l-value, so
2c809f8f 10200 // wrap the index check into START.
10201 start =
10202 gogo->backend()->conditional_expression(int_btype, bad_index,
10203 crash, start, loc);
e440a328 10204
2c809f8f 10205 Bexpression* ret;
e440a328 10206 if (array_type->length() != NULL)
10207 {
ea664253 10208 Bexpression* array = this->array_->get_backend(context);
2c809f8f 10209 ret = gogo->backend()->array_index_expression(array, start, loc);
e440a328 10210 }
10211 else
10212 {
2c809f8f 10213 // Slice.
10214 Expression* valptr =
35a54f17 10215 array_type->get_value_pointer(gogo, this->array_);
ea664253 10216 Bexpression* ptr = valptr->get_backend(context);
2c809f8f 10217 ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc);
9b27b43c 10218
10219 Type* ele_type = this->array_->type()->array_type()->element_type();
10220 Btype* ele_btype = ele_type->get_backend(gogo);
10221 ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc);
e440a328 10222 }
ea664253 10223 return ret;
e440a328 10224 }
10225
10226 // Array slice.
10227
acf2b673 10228 if (this->cap_ != NULL)
10229 {
2c809f8f 10230 Bexpression* bounds_bcheck =
ea664253 10231 Expression::check_bounds(this->cap_, loc)->get_backend(context);
2c809f8f 10232 bad_index =
10233 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck,
10234 bad_index, loc);
10235 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
10236
10237 Bexpression* cap_too_small =
10238 gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc);
10239 Bexpression* cap_too_large =
10240 gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc);
10241 Bexpression* bad_cap =
10242 gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small,
10243 cap_too_large, loc);
10244 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap,
10245 bad_index, loc);
10246 }
10247
10248 Bexpression* end;
e440a328 10249 if (this->end_->is_nil_expression())
2c809f8f 10250 end = length;
e440a328 10251 else
10252 {
2c809f8f 10253 Bexpression* bounds_bcheck =
ea664253 10254 Expression::check_bounds(this->end_, loc)->get_backend(context);
e440a328 10255
2c809f8f 10256 bad_index =
10257 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck,
10258 bad_index, loc);
e440a328 10259
ea664253 10260 end = this->end_->get_backend(context);
2c809f8f 10261 end = gogo->backend()->convert_expression(int_btype, end, loc);
10262 Bexpression* end_too_small =
10263 gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc);
10264 Bexpression* end_too_large =
10265 gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc);
10266 Bexpression* bad_end =
10267 gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small,
10268 end_too_large, loc);
10269 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end,
10270 bad_index, loc);
e440a328 10271 }
10272
35a54f17 10273 Expression* valptr = array_type->get_value_pointer(gogo, this->array_);
ea664253 10274 Bexpression* val = valptr->get_backend(context);
2c809f8f 10275 val = gogo->backend()->pointer_offset_expression(val, start, loc);
e440a328 10276
2c809f8f 10277 Bexpression* result_length =
10278 gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc);
e440a328 10279
2c809f8f 10280 Bexpression* result_capacity =
10281 gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc);
e440a328 10282
2c809f8f 10283 Btype* struct_btype = this->type()->get_backend(gogo);
10284 std::vector<Bexpression*> init;
10285 init.push_back(val);
10286 init.push_back(result_length);
10287 init.push_back(result_capacity);
e440a328 10288
2c809f8f 10289 Bexpression* ctor =
10290 gogo->backend()->constructor_expression(struct_btype, init, loc);
ea664253 10291 return gogo->backend()->conditional_expression(struct_btype, bad_index,
10292 crash, ctor, loc);
e440a328 10293}
10294
d751bb78 10295// Dump ast representation for an array index expression.
10296
10297void
10298Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10299 const
10300{
10301 Index_expression::dump_index_expression(ast_dump_context, this->array_,
acf2b673 10302 this->start_, this->end_, this->cap_);
d751bb78 10303}
10304
acf2b673 10305// Make an array index expression. END and CAP may be NULL.
e440a328 10306
10307Expression*
10308Expression::make_array_index(Expression* array, Expression* start,
acf2b673 10309 Expression* end, Expression* cap,
10310 Location location)
e440a328 10311{
acf2b673 10312 return new Array_index_expression(array, start, end, cap, location);
e440a328 10313}
10314
10315// A string index. This is used for both indexing and slicing.
10316
10317class String_index_expression : public Expression
10318{
10319 public:
10320 String_index_expression(Expression* string, Expression* start,
b13c66cd 10321 Expression* end, Location location)
e440a328 10322 : Expression(EXPRESSION_STRING_INDEX, location),
10323 string_(string), start_(start), end_(end)
10324 { }
10325
10326 protected:
10327 int
10328 do_traverse(Traverse*);
10329
2c809f8f 10330 Expression*
10331 do_flatten(Gogo*, Named_object*, Statement_inserter*);
10332
e440a328 10333 Type*
10334 do_type();
10335
10336 void
10337 do_determine_type(const Type_context*);
10338
10339 void
10340 do_check_types(Gogo*);
10341
10342 Expression*
10343 do_copy()
10344 {
10345 return Expression::make_string_index(this->string_->copy(),
10346 this->start_->copy(),
10347 (this->end_ == NULL
10348 ? NULL
10349 : this->end_->copy()),
10350 this->location());
10351 }
10352
baef9f7a 10353 bool
10354 do_must_eval_subexpressions_in_order(int* skip) const
10355 {
10356 *skip = 1;
10357 return true;
10358 }
10359
ea664253 10360 Bexpression*
10361 do_get_backend(Translate_context*);
e440a328 10362
d751bb78 10363 void
10364 do_dump_expression(Ast_dump_context*) const;
10365
e440a328 10366 private:
10367 // The string we are getting a value from.
10368 Expression* string_;
10369 // The start or only index.
10370 Expression* start_;
10371 // The end index of a slice. This may be NULL for a single index,
10372 // or it may be a nil expression for the length of the string.
10373 Expression* end_;
10374};
10375
10376// String index traversal.
10377
10378int
10379String_index_expression::do_traverse(Traverse* traverse)
10380{
10381 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10382 return TRAVERSE_EXIT;
10383 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10384 return TRAVERSE_EXIT;
10385 if (this->end_ != NULL)
10386 {
10387 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10388 return TRAVERSE_EXIT;
10389 }
10390 return TRAVERSE_CONTINUE;
10391}
10392
2c809f8f 10393Expression*
10394String_index_expression::do_flatten(Gogo*, Named_object*,
10395 Statement_inserter* inserter)
e440a328 10396{
2c809f8f 10397 Location loc = this->location();
5bf8be8b 10398 Expression* string = this->string_;
10399 Expression* start = this->start_;
10400 Expression* end = this->end_;
10401 if (string->is_error_expression()
10402 || string->type()->is_error_type()
10403 || start->is_error_expression()
10404 || start->type()->is_error_type()
10405 || (end != NULL
10406 && (end->is_error_expression() || end->type()->is_error_type())))
10407 {
10408 go_assert(saw_errors());
10409 return Expression::make_error(loc);
10410 }
10411
10412 Temporary_statement* temp;
2c809f8f 10413 if (!this->string_->is_variable())
10414 {
10415 temp = Statement::make_temporary(NULL, this->string_, loc);
10416 inserter->insert(temp);
10417 this->string_ = Expression::make_temporary_reference(temp, loc);
10418 }
10419 if (!this->start_->is_variable())
10420 {
10421 temp = Statement::make_temporary(NULL, this->start_, loc);
10422 inserter->insert(temp);
10423 this->start_ = Expression::make_temporary_reference(temp, loc);
10424 }
10425 if (this->end_ != NULL
10426 && !this->end_->is_nil_expression()
10427 && !this->end_->is_variable())
10428 {
10429 temp = Statement::make_temporary(NULL, this->end_, loc);
10430 inserter->insert(temp);
10431 this->end_ = Expression::make_temporary_reference(temp, loc);
10432 }
10433
10434 return this;
10435}
10436
10437// Return the type of a string index.
10438
10439Type*
10440String_index_expression::do_type()
10441{
10442 if (this->end_ == NULL)
10443 return Type::lookup_integer_type("uint8");
10444 else
10445 return this->string_->type();
10446}
10447
10448// Determine the type of a string index.
10449
10450void
10451String_index_expression::do_determine_type(const Type_context*)
10452{
10453 this->string_->determine_type_no_context();
f77aa642 10454
10455 Type_context index_context(Type::lookup_integer_type("int"), false);
10456 if (this->start_->is_constant())
10457 this->start_->determine_type(&index_context);
10458 else
10459 this->start_->determine_type_no_context();
e440a328 10460 if (this->end_ != NULL)
f77aa642 10461 {
10462 if (this->end_->is_constant())
10463 this->end_->determine_type(&index_context);
10464 else
10465 this->end_->determine_type_no_context();
10466 }
e440a328 10467}
10468
10469// Check types of a string index.
10470
10471void
10472String_index_expression::do_check_types(Gogo*)
10473{
acdc230d 10474 Numeric_constant nc;
10475 unsigned long v;
10476 if (this->start_->type()->integer_type() == NULL
10477 && !this->start_->type()->is_error()
10478 && (!this->start_->numeric_constant_value(&nc)
10479 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 10480 this->report_error(_("index must be integer"));
10481 if (this->end_ != NULL
10482 && this->end_->type()->integer_type() == NULL
acdc230d 10483 && !this->end_->type()->is_error()
10484 && !this->end_->is_nil_expression()
10485 && !this->end_->is_error_expression()
10486 && (!this->end_->numeric_constant_value(&nc)
10487 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
e440a328 10488 this->report_error(_("slice end must be integer"));
10489
10490 std::string sval;
10491 bool sval_valid = this->string_->string_constant_value(&sval);
10492
0c77715b 10493 Numeric_constant inc;
e440a328 10494 mpz_t ival;
0bd5d859 10495 bool ival_valid = false;
0c77715b 10496 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
e440a328 10497 {
0bd5d859 10498 ival_valid = true;
e440a328 10499 if (mpz_sgn(ival) < 0
b10f32fb 10500 || (sval_valid
10501 && (this->end_ == NULL
10502 ? mpz_cmp_ui(ival, sval.length()) >= 0
10503 : mpz_cmp_ui(ival, sval.length()) > 0)))
e440a328 10504 {
10505 error_at(this->start_->location(), "string index out of bounds");
10506 this->set_is_error();
10507 }
10508 }
10509 if (this->end_ != NULL && !this->end_->is_nil_expression())
10510 {
0c77715b 10511 Numeric_constant enc;
10512 mpz_t eval;
10513 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
e440a328 10514 {
0c77715b 10515 if (mpz_sgn(eval) < 0
10516 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
e440a328 10517 {
10518 error_at(this->end_->location(), "string index out of bounds");
10519 this->set_is_error();
10520 }
0bd5d859 10521 else if (ival_valid && mpz_cmp(ival, eval) > 0)
10522 this->report_error(_("inverted slice range"));
0c77715b 10523 mpz_clear(eval);
e440a328 10524 }
10525 }
0bd5d859 10526 if (ival_valid)
10527 mpz_clear(ival);
e440a328 10528}
10529
ea664253 10530// Get the backend representation for a string index.
e440a328 10531
ea664253 10532Bexpression*
10533String_index_expression::do_get_backend(Translate_context* context)
e440a328 10534{
b13c66cd 10535 Location loc = this->location();
2c809f8f 10536 Expression* string_arg = this->string_;
10537 if (this->string_->type()->points_to() != NULL)
10538 string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc);
e440a328 10539
2c809f8f 10540 Expression* bad_index = Expression::check_bounds(this->start_, loc);
e440a328 10541
2c809f8f 10542 int code = (this->end_ == NULL
10543 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10544 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
e440a328 10545
2c809f8f 10546 Gogo* gogo = context->gogo();
ea664253 10547 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context);
1b1f2abf 10548
10549 Type* int_type = Type::lookup_integer_type("int");
e440a328 10550
2c809f8f 10551 // It is possible that an error occurred earlier because the start index
10552 // cannot be represented as an integer type. In this case, we shouldn't
10553 // try casting the starting index into an integer since
10554 // Type_conversion_expression will fail to get the backend representation.
10555 // FIXME.
10556 if (this->start_->type()->integer_type() == NULL
10557 && !Type::are_convertible(int_type, this->start_->type(), NULL))
10558 {
10559 go_assert(saw_errors());
ea664253 10560 return context->backend()->error_expression();
2c809f8f 10561 }
e440a328 10562
2c809f8f 10563 Expression* start = Expression::make_cast(int_type, this->start_, loc);
e440a328 10564
2c809f8f 10565 if (this->end_ == NULL)
10566 {
10567 Expression* length =
10568 Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc);
e440a328 10569
2c809f8f 10570 Expression* start_too_large =
10571 Expression::make_binary(OPERATOR_GE, start, length, loc);
10572 bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large,
10573 bad_index, loc);
10574 Expression* bytes =
10575 Expression::make_string_info(this->string_, STRING_INFO_DATA, loc);
e440a328 10576
ea664253 10577 Bexpression* bstart = start->get_backend(context);
10578 Bexpression* ptr = bytes->get_backend(context);
2c809f8f 10579 ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc);
9b27b43c 10580 Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo);
10581 Bexpression* index =
10582 gogo->backend()->indirect_expression(ubtype, ptr, true, loc);
e440a328 10583
2c809f8f 10584 Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo);
ea664253 10585 Bexpression* index_error = bad_index->get_backend(context);
10586 return gogo->backend()->conditional_expression(byte_btype, index_error,
10587 crash, index, loc);
2c809f8f 10588 }
10589
10590 Expression* end = NULL;
10591 if (this->end_->is_nil_expression())
e67508fa 10592 end = Expression::make_integer_sl(-1, int_type, loc);
e440a328 10593 else
10594 {
2c809f8f 10595 Expression* bounds_check = Expression::check_bounds(this->end_, loc);
10596 bad_index =
10597 Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc);
10598 end = Expression::make_cast(int_type, this->end_, loc);
e440a328 10599 }
2c809f8f 10600
10601 Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3,
10602 string_arg, start, end);
ea664253 10603 Bexpression* bstrslice = strslice->get_backend(context);
2c809f8f 10604
10605 Btype* str_btype = strslice->type()->get_backend(gogo);
ea664253 10606 Bexpression* index_error = bad_index->get_backend(context);
10607 return gogo->backend()->conditional_expression(str_btype, index_error,
10608 crash, bstrslice, loc);
e440a328 10609}
10610
d751bb78 10611// Dump ast representation for a string index expression.
10612
10613void
10614String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10615 const
10616{
acf2b673 10617 Index_expression::dump_index_expression(ast_dump_context, this->string_,
10618 this->start_, this->end_, NULL);
d751bb78 10619}
10620
e440a328 10621// Make a string index expression. END may be NULL.
10622
10623Expression*
10624Expression::make_string_index(Expression* string, Expression* start,
b13c66cd 10625 Expression* end, Location location)
e440a328 10626{
10627 return new String_index_expression(string, start, end, location);
10628}
10629
10630// Class Map_index.
10631
10632// Get the type of the map.
10633
10634Map_type*
10635Map_index_expression::get_map_type() const
10636{
10637 Map_type* mt = this->map_->type()->deref()->map_type();
c7524fae 10638 if (mt == NULL)
c484d925 10639 go_assert(saw_errors());
e440a328 10640 return mt;
10641}
10642
10643// Map index traversal.
10644
10645int
10646Map_index_expression::do_traverse(Traverse* traverse)
10647{
10648 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
10649 return TRAVERSE_EXIT;
10650 return Expression::traverse(&this->index_, traverse);
10651}
10652
2c809f8f 10653// We need to pass in a pointer to the key, so flatten the index into a
10654// temporary variable if it isn't already. The value pointer will be
10655// dereferenced and checked for nil, so flatten into a temporary to avoid
10656// recomputation.
10657
10658Expression*
91c0fd76 10659Map_index_expression::do_flatten(Gogo* gogo, Named_object*,
2c809f8f 10660 Statement_inserter* inserter)
10661{
91c0fd76 10662 Location loc = this->location();
2c809f8f 10663 Map_type* mt = this->get_map_type();
5bf8be8b 10664 if (this->index()->is_error_expression()
10665 || this->index()->type()->is_error_type()
10666 || mt->is_error_type())
10667 {
10668 go_assert(saw_errors());
10669 return Expression::make_error(loc);
10670 }
10671
91c0fd76 10672 if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL))
10673 {
10674 if (this->index_->type()->interface_type() != NULL
10675 && !this->index_->is_variable())
10676 {
10677 Temporary_statement* temp =
10678 Statement::make_temporary(NULL, this->index_, loc);
10679 inserter->insert(temp);
10680 this->index_ = Expression::make_temporary_reference(temp, loc);
10681 }
10682 this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(),
10683 this->index_, loc);
10684 }
2c809f8f 10685
10686 if (!this->index_->is_variable())
10687 {
10688 Temporary_statement* temp = Statement::make_temporary(NULL, this->index_,
91c0fd76 10689 loc);
2c809f8f 10690 inserter->insert(temp);
91c0fd76 10691 this->index_ = Expression::make_temporary_reference(temp, loc);
2c809f8f 10692 }
10693
10694 if (this->value_pointer_ == NULL)
10695 this->get_value_pointer(this->is_lvalue_);
5bf8be8b 10696 if (this->value_pointer_->is_error_expression()
10697 || this->value_pointer_->type()->is_error_type())
10698 return Expression::make_error(loc);
2c809f8f 10699 if (!this->value_pointer_->is_variable())
10700 {
10701 Temporary_statement* temp =
91c0fd76 10702 Statement::make_temporary(NULL, this->value_pointer_, loc);
2c809f8f 10703 inserter->insert(temp);
91c0fd76 10704 this->value_pointer_ = Expression::make_temporary_reference(temp, loc);
2c809f8f 10705 }
10706
10707 return this;
10708}
10709
e440a328 10710// Return the type of a map index.
10711
10712Type*
10713Map_index_expression::do_type()
10714{
c7524fae 10715 Map_type* mt = this->get_map_type();
10716 if (mt == NULL)
10717 return Type::make_error_type();
10718 Type* type = mt->val_type();
e440a328 10719 // If this map index is in a tuple assignment, we actually return a
10720 // pointer to the value type. Tuple_map_assignment_statement is
10721 // responsible for handling this correctly. We need to get the type
10722 // right in case this gets assigned to a temporary variable.
10723 if (this->is_in_tuple_assignment_)
10724 type = Type::make_pointer_type(type);
10725 return type;
10726}
10727
10728// Fix the type of a map index.
10729
10730void
10731Map_index_expression::do_determine_type(const Type_context*)
10732{
10733 this->map_->determine_type_no_context();
c7524fae 10734 Map_type* mt = this->get_map_type();
10735 Type* key_type = mt == NULL ? NULL : mt->key_type();
10736 Type_context subcontext(key_type, false);
e440a328 10737 this->index_->determine_type(&subcontext);
10738}
10739
10740// Check types of a map index.
10741
10742void
10743Map_index_expression::do_check_types(Gogo*)
10744{
10745 std::string reason;
c7524fae 10746 Map_type* mt = this->get_map_type();
10747 if (mt == NULL)
10748 return;
10749 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
e440a328 10750 {
10751 if (reason.empty())
10752 this->report_error(_("incompatible type for map index"));
10753 else
10754 {
10755 error_at(this->location(), "incompatible type for map index (%s)",
10756 reason.c_str());
10757 this->set_is_error();
10758 }
10759 }
10760}
10761
ea664253 10762// Get the backend representation for a map index.
e440a328 10763
ea664253 10764Bexpression*
10765Map_index_expression::do_get_backend(Translate_context* context)
e440a328 10766{
10767 Map_type* type = this->get_map_type();
c7524fae 10768 if (type == NULL)
2c809f8f 10769 {
10770 go_assert(saw_errors());
ea664253 10771 return context->backend()->error_expression();
2c809f8f 10772 }
e440a328 10773
2c809f8f 10774 go_assert(this->value_pointer_ != NULL
10775 && this->value_pointer_->is_variable());
e440a328 10776
2c809f8f 10777 Bexpression* ret;
e440a328 10778 if (this->is_lvalue_)
2c809f8f 10779 {
10780 Expression* val =
10781 Expression::make_unary(OPERATOR_MULT, this->value_pointer_,
10782 this->location());
ea664253 10783 ret = val->get_backend(context);
2c809f8f 10784 }
e440a328 10785 else if (this->is_in_tuple_assignment_)
10786 {
10787 // Tuple_map_assignment_statement is responsible for using this
10788 // appropriately.
ea664253 10789 ret = this->value_pointer_->get_backend(context);
e440a328 10790 }
10791 else
10792 {
2c809f8f 10793 Location loc = this->location();
10794
10795 Expression* nil_check =
10796 Expression::make_binary(OPERATOR_EQEQ, this->value_pointer_,
10797 Expression::make_nil(loc), loc);
ea664253 10798 Bexpression* bnil_check = nil_check->get_backend(context);
2c809f8f 10799 Expression* val =
10800 Expression::make_unary(OPERATOR_MULT, this->value_pointer_, loc);
ea664253 10801 Bexpression* bval = val->get_backend(context);
2c809f8f 10802
63697958 10803 Gogo* gogo = context->gogo();
10804 Btype* val_btype = type->val_type()->get_backend(gogo);
10805 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
2c809f8f 10806 ret = gogo->backend()->conditional_expression(val_btype, bnil_check,
10807 val_zero, bval, loc);
e440a328 10808 }
ea664253 10809 return ret;
e440a328 10810}
10811
2c809f8f 10812// Get an expression for the map index. This returns an expression which
10813// evaluates to a pointer to a value. The pointer will be NULL if the key is
e440a328 10814// not in the map.
10815
2c809f8f 10816Expression*
10817Map_index_expression::get_value_pointer(bool insert)
e440a328 10818{
2c809f8f 10819 if (this->value_pointer_ == NULL)
746d2e73 10820 {
2c809f8f 10821 Map_type* type = this->get_map_type();
10822 if (type == NULL)
746d2e73 10823 {
2c809f8f 10824 go_assert(saw_errors());
10825 return Expression::make_error(this->location());
746d2e73 10826 }
e440a328 10827
2c809f8f 10828 Location loc = this->location();
10829 Expression* map_ref = this->map_;
10830 if (this->map_->type()->points_to() != NULL)
10831 map_ref = Expression::make_unary(OPERATOR_MULT, map_ref, loc);
e440a328 10832
2c809f8f 10833 Expression* index_ptr = Expression::make_unary(OPERATOR_AND, this->index_,
10834 loc);
10835 Expression* map_index =
10836 Runtime::make_call(Runtime::MAP_INDEX, loc, 3,
10837 map_ref, index_ptr,
10838 Expression::make_boolean(insert, loc));
10839
10840 Type* val_type = type->val_type();
10841 this->value_pointer_ =
10842 Expression::make_unsafe_cast(Type::make_pointer_type(val_type),
10843 map_index, this->location());
10844 }
10845 return this->value_pointer_;
e440a328 10846}
10847
d751bb78 10848// Dump ast representation for a map index expression
10849
10850void
10851Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10852 const
10853{
acf2b673 10854 Index_expression::dump_index_expression(ast_dump_context, this->map_,
10855 this->index_, NULL, NULL);
d751bb78 10856}
10857
e440a328 10858// Make a map index expression.
10859
10860Map_index_expression*
10861Expression::make_map_index(Expression* map, Expression* index,
b13c66cd 10862 Location location)
e440a328 10863{
10864 return new Map_index_expression(map, index, location);
10865}
10866
10867// Class Field_reference_expression.
10868
149eabc5 10869// Lower a field reference expression. There is nothing to lower, but
10870// this is where we generate the tracking information for fields with
10871// the magic go:"track" tag.
10872
10873Expression*
10874Field_reference_expression::do_lower(Gogo* gogo, Named_object* function,
10875 Statement_inserter* inserter, int)
10876{
10877 Struct_type* struct_type = this->expr_->type()->struct_type();
10878 if (struct_type == NULL)
10879 {
10880 // Error will be reported elsewhere.
10881 return this;
10882 }
10883 const Struct_field* field = struct_type->field(this->field_index_);
10884 if (field == NULL)
10885 return this;
10886 if (!field->has_tag())
10887 return this;
10888 if (field->tag().find("go:\"track\"") == std::string::npos)
10889 return this;
10890
604e278d 10891 // References from functions generated by the compiler don't count.
c6292d1d 10892 if (function != NULL && function->func_value()->is_type_specific_function())
604e278d 10893 return this;
10894
149eabc5 10895 // We have found a reference to a tracked field. Build a call to
10896 // the runtime function __go_fieldtrack with a string that describes
10897 // the field. FIXME: We should only call this once per referenced
10898 // field per function, not once for each reference to the field.
10899
10900 if (this->called_fieldtrack_)
10901 return this;
10902 this->called_fieldtrack_ = true;
10903
10904 Location loc = this->location();
10905
10906 std::string s = "fieldtrack \"";
10907 Named_type* nt = this->expr_->type()->named_type();
10908 if (nt == NULL || nt->named_object()->package() == NULL)
10909 s.append(gogo->pkgpath());
10910 else
10911 s.append(nt->named_object()->package()->pkgpath());
10912 s.push_back('.');
10913 if (nt != NULL)
5c29ad36 10914 s.append(Gogo::unpack_hidden_name(nt->name()));
149eabc5 10915 s.push_back('.');
10916 s.append(field->field_name());
10917 s.push_back('"');
10918
10919 // We can't use a string here, because internally a string holds a
10920 // pointer to the actual bytes; when the linker garbage collects the
10921 // string, it won't garbage collect the bytes. So we use a
10922 // [...]byte.
10923
e67508fa 10924 Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc);
149eabc5 10925
10926 Type* byte_type = gogo->lookup_global("byte")->type_value();
10927 Type* array_type = Type::make_array_type(byte_type, length_expr);
10928
10929 Expression_list* bytes = new Expression_list();
10930 for (std::string::const_iterator p = s.begin(); p != s.end(); p++)
10931 {
e67508fa 10932 unsigned char c = static_cast<unsigned char>(*p);
10933 bytes->push_back(Expression::make_integer_ul(c, NULL, loc));
149eabc5 10934 }
10935
10936 Expression* e = Expression::make_composite_literal(array_type, 0, false,
62750cd5 10937 bytes, false, loc);
149eabc5 10938
10939 Variable* var = new Variable(array_type, e, true, false, false, loc);
10940
10941 static int count;
10942 char buf[50];
10943 snprintf(buf, sizeof buf, "fieldtrack.%d", count);
10944 ++count;
10945
10946 Named_object* no = gogo->add_variable(buf, var);
10947 e = Expression::make_var_reference(no, loc);
10948 e = Expression::make_unary(OPERATOR_AND, e, loc);
10949
10950 Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e);
604e278d 10951 gogo->lower_expression(function, inserter, &call);
149eabc5 10952 inserter->insert(Statement::make_statement(call, false));
10953
10954 // Put this function, and the global variable we just created, into
10955 // unique sections. This will permit the linker to garbage collect
10956 // them if they are not referenced. The effect is that the only
10957 // strings, indicating field references, that will wind up in the
10958 // executable will be those for functions that are actually needed.
66a6be58 10959 if (function != NULL)
10960 function->func_value()->set_in_unique_section();
149eabc5 10961 var->set_in_unique_section();
10962
10963 return this;
10964}
10965
e440a328 10966// Return the type of a field reference.
10967
10968Type*
10969Field_reference_expression::do_type()
10970{
b0e628fb 10971 Type* type = this->expr_->type();
5c13bd80 10972 if (type->is_error())
b0e628fb 10973 return type;
10974 Struct_type* struct_type = type->struct_type();
c484d925 10975 go_assert(struct_type != NULL);
e440a328 10976 return struct_type->field(this->field_index_)->type();
10977}
10978
10979// Check the types for a field reference.
10980
10981void
10982Field_reference_expression::do_check_types(Gogo*)
10983{
b0e628fb 10984 Type* type = this->expr_->type();
5c13bd80 10985 if (type->is_error())
b0e628fb 10986 return;
10987 Struct_type* struct_type = type->struct_type();
c484d925 10988 go_assert(struct_type != NULL);
10989 go_assert(struct_type->field(this->field_index_) != NULL);
e440a328 10990}
10991
ea664253 10992// Get the backend representation for a field reference.
e440a328 10993
ea664253 10994Bexpression*
10995Field_reference_expression::do_get_backend(Translate_context* context)
e440a328 10996{
ea664253 10997 Bexpression* bstruct = this->expr_->get_backend(context);
10998 return context->gogo()->backend()->struct_field_expression(bstruct,
10999 this->field_index_,
11000 this->location());
e440a328 11001}
11002
d751bb78 11003// Dump ast representation for a field reference expression.
11004
11005void
11006Field_reference_expression::do_dump_expression(
11007 Ast_dump_context* ast_dump_context) const
11008{
11009 this->expr_->dump_expression(ast_dump_context);
11010 ast_dump_context->ostream() << "." << this->field_index_;
11011}
11012
e440a328 11013// Make a reference to a qualified identifier in an expression.
11014
11015Field_reference_expression*
11016Expression::make_field_reference(Expression* expr, unsigned int field_index,
b13c66cd 11017 Location location)
e440a328 11018{
11019 return new Field_reference_expression(expr, field_index, location);
11020}
11021
11022// Class Interface_field_reference_expression.
11023
2387f644 11024// Return an expression for the pointer to the function to call.
e440a328 11025
2387f644 11026Expression*
11027Interface_field_reference_expression::get_function()
e440a328 11028{
2387f644 11029 Expression* ref = this->expr_;
11030 Location loc = this->location();
11031 if (ref->type()->points_to() != NULL)
11032 ref = Expression::make_unary(OPERATOR_MULT, ref, loc);
e440a328 11033
2387f644 11034 Expression* mtable =
11035 Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc);
11036 Struct_type* mtable_type = mtable->type()->points_to()->struct_type();
e440a328 11037
11038 std::string name = Gogo::unpack_hidden_name(this->name_);
2387f644 11039 unsigned int index;
11040 const Struct_field* field = mtable_type->find_local_field(name, &index);
11041 go_assert(field != NULL);
11042 mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc);
11043 return Expression::make_field_reference(mtable, index, loc);
e440a328 11044}
11045
2387f644 11046// Return an expression for the first argument to pass to the interface
e440a328 11047// function.
11048
2387f644 11049Expression*
11050Interface_field_reference_expression::get_underlying_object()
e440a328 11051{
2387f644 11052 Expression* expr = this->expr_;
11053 if (expr->type()->points_to() != NULL)
11054 expr = Expression::make_unary(OPERATOR_MULT, expr, this->location());
11055 return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT,
11056 this->location());
e440a328 11057}
11058
11059// Traversal.
11060
11061int
11062Interface_field_reference_expression::do_traverse(Traverse* traverse)
11063{
11064 return Expression::traverse(&this->expr_, traverse);
11065}
11066
0afbb937 11067// Lower the expression. If this expression is not called, we need to
11068// evaluate the expression twice when converting to the backend
11069// interface. So introduce a temporary variable if necessary.
11070
11071Expression*
9782d556 11072Interface_field_reference_expression::do_flatten(Gogo*, Named_object*,
11073 Statement_inserter* inserter)
0afbb937 11074{
5bf8be8b 11075 if (this->expr_->is_error_expression()
11076 || this->expr_->type()->is_error_type())
11077 {
11078 go_assert(saw_errors());
11079 return Expression::make_error(this->location());
11080 }
11081
2387f644 11082 if (!this->expr_->is_variable())
0afbb937 11083 {
11084 Temporary_statement* temp =
11085 Statement::make_temporary(this->expr_->type(), NULL, this->location());
11086 inserter->insert(temp);
11087 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_,
11088 this->location());
11089 }
11090 return this;
11091}
11092
e440a328 11093// Return the type of an interface field reference.
11094
11095Type*
11096Interface_field_reference_expression::do_type()
11097{
11098 Type* expr_type = this->expr_->type();
11099
11100 Type* points_to = expr_type->points_to();
11101 if (points_to != NULL)
11102 expr_type = points_to;
11103
11104 Interface_type* interface_type = expr_type->interface_type();
11105 if (interface_type == NULL)
11106 return Type::make_error_type();
11107
11108 const Typed_identifier* method = interface_type->find_method(this->name_);
11109 if (method == NULL)
11110 return Type::make_error_type();
11111
11112 return method->type();
11113}
11114
11115// Determine types.
11116
11117void
11118Interface_field_reference_expression::do_determine_type(const Type_context*)
11119{
11120 this->expr_->determine_type_no_context();
11121}
11122
11123// Check the types for an interface field reference.
11124
11125void
11126Interface_field_reference_expression::do_check_types(Gogo*)
11127{
11128 Type* type = this->expr_->type();
11129
11130 Type* points_to = type->points_to();
11131 if (points_to != NULL)
11132 type = points_to;
11133
11134 Interface_type* interface_type = type->interface_type();
11135 if (interface_type == NULL)
5c491127 11136 {
11137 if (!type->is_error_type())
11138 this->report_error(_("expected interface or pointer to interface"));
11139 }
e440a328 11140 else
11141 {
11142 const Typed_identifier* method =
11143 interface_type->find_method(this->name_);
11144 if (method == NULL)
11145 {
11146 error_at(this->location(), "method %qs not in interface",
11147 Gogo::message_name(this->name_).c_str());
11148 this->set_is_error();
11149 }
11150 }
11151}
11152
0afbb937 11153// If an interface field reference is not simply called, then it is
11154// represented as a closure. The closure will hold a single variable,
11155// the value of the interface on which the method should be called.
11156// The function will be a simple thunk that pulls the value from the
11157// closure and calls the method with the remaining arguments.
11158
11159// Because method values are not common, we don't build all thunks for
11160// all possible interface methods, but instead only build them as we
11161// need them. In particular, we even build them on demand for
11162// interface methods defined in other packages.
11163
11164Interface_field_reference_expression::Interface_method_thunks
11165 Interface_field_reference_expression::interface_method_thunks;
11166
11167// Find or create the thunk to call method NAME on TYPE.
11168
11169Named_object*
11170Interface_field_reference_expression::create_thunk(Gogo* gogo,
11171 Interface_type* type,
11172 const std::string& name)
11173{
11174 std::pair<Interface_type*, Method_thunks*> val(type, NULL);
11175 std::pair<Interface_method_thunks::iterator, bool> ins =
11176 Interface_field_reference_expression::interface_method_thunks.insert(val);
11177 if (ins.second)
11178 {
11179 // This is the first time we have seen this interface.
11180 ins.first->second = new Method_thunks();
11181 }
11182
11183 for (Method_thunks::const_iterator p = ins.first->second->begin();
11184 p != ins.first->second->end();
11185 p++)
11186 if (p->first == name)
11187 return p->second;
11188
11189 Location loc = type->location();
11190
11191 const Typed_identifier* method_id = type->find_method(name);
11192 if (method_id == NULL)
11193 return Named_object::make_erroneous_name(Gogo::thunk_name());
11194
11195 Function_type* orig_fntype = method_id->type()->function_type();
11196 if (orig_fntype == NULL)
11197 return Named_object::make_erroneous_name(Gogo::thunk_name());
11198
11199 Struct_field_list* sfl = new Struct_field_list();
f8bdf81a 11200 // The type here is wrong--it should be the C function type. But it
11201 // doesn't really matter.
0afbb937 11202 Type* vt = Type::make_pointer_type(Type::make_void_type());
11203 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
11204 sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc)));
11205 Type* closure_type = Type::make_struct_type(sfl, loc);
11206 closure_type = Type::make_pointer_type(closure_type);
11207
f8bdf81a 11208 Function_type* new_fntype = orig_fntype->copy_with_names();
0afbb937 11209
da244e59 11210 std::string thunk_name = Gogo::thunk_name();
11211 Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
0afbb937 11212 false, loc);
11213
f8bdf81a 11214 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
11215 cvar->set_is_used();
1ecc6157 11216 cvar->set_is_closure();
da244e59 11217 Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
11218 NULL, cvar);
f8bdf81a 11219 new_no->func_value()->set_closure_var(cp);
0afbb937 11220
f8bdf81a 11221 gogo->start_block(loc);
0afbb937 11222
11223 // Field 0 of the closure is the function code pointer, field 1 is
11224 // the value on which to invoke the method.
11225 Expression* arg = Expression::make_var_reference(cp, loc);
11226 arg = Expression::make_unary(OPERATOR_MULT, arg, loc);
11227 arg = Expression::make_field_reference(arg, 1, loc);
11228
11229 Expression *ifre = Expression::make_interface_field_reference(arg, name,
11230 loc);
11231
11232 const Typed_identifier_list* orig_params = orig_fntype->parameters();
11233 Expression_list* args;
11234 if (orig_params == NULL || orig_params->empty())
11235 args = NULL;
11236 else
11237 {
11238 const Typed_identifier_list* new_params = new_fntype->parameters();
11239 args = new Expression_list();
11240 for (Typed_identifier_list::const_iterator p = new_params->begin();
f8bdf81a 11241 p != new_params->end();
0afbb937 11242 ++p)
11243 {
11244 Named_object* p_no = gogo->lookup(p->name(), NULL);
11245 go_assert(p_no != NULL
11246 && p_no->is_variable()
11247 && p_no->var_value()->is_parameter());
11248 args->push_back(Expression::make_var_reference(p_no, loc));
11249 }
11250 }
11251
11252 Call_expression* call = Expression::make_call(ifre, args,
11253 orig_fntype->is_varargs(),
11254 loc);
11255 call->set_varargs_are_lowered();
11256
11257 Statement* s = Statement::make_return_from_call(call, loc);
11258 gogo->add_statement(s);
11259 Block* b = gogo->finish_block(loc);
11260 gogo->add_block(b, loc);
11261 gogo->lower_block(new_no, b);
a32698ee 11262 gogo->flatten_block(new_no, b);
0afbb937 11263 gogo->finish_function(loc);
11264
11265 ins.first->second->push_back(std::make_pair(name, new_no));
11266 return new_no;
11267}
11268
ea664253 11269// Get the backend representation for a method value.
e440a328 11270
ea664253 11271Bexpression*
11272Interface_field_reference_expression::do_get_backend(Translate_context* context)
e440a328 11273{
0afbb937 11274 Interface_type* type = this->expr_->type()->interface_type();
11275 if (type == NULL)
11276 {
11277 go_assert(saw_errors());
ea664253 11278 return context->backend()->error_expression();
0afbb937 11279 }
11280
11281 Named_object* thunk =
11282 Interface_field_reference_expression::create_thunk(context->gogo(),
11283 type, this->name_);
11284 if (thunk->is_erroneous())
11285 {
11286 go_assert(saw_errors());
ea664253 11287 return context->backend()->error_expression();
0afbb937 11288 }
11289
11290 // FIXME: We should lower this earlier, but we can't it lower it in
11291 // the lowering pass because at that point we don't know whether we
11292 // need to create the thunk or not. If the expression is called, we
11293 // don't need the thunk.
11294
11295 Location loc = this->location();
11296
11297 Struct_field_list* fields = new Struct_field_list();
11298 fields->push_back(Struct_field(Typed_identifier("fn.0",
11299 thunk->func_value()->type(),
11300 loc)));
11301 fields->push_back(Struct_field(Typed_identifier("val.1",
11302 this->expr_->type(),
11303 loc)));
11304 Struct_type* st = Type::make_struct_type(fields, loc);
11305
11306 Expression_list* vals = new Expression_list();
11307 vals->push_back(Expression::make_func_code_reference(thunk, loc));
11308 vals->push_back(this->expr_);
11309
11310 Expression* expr = Expression::make_struct_composite_literal(st, vals, loc);
ea664253 11311 Bexpression* bclosure =
11312 Expression::make_heap_expression(expr, loc)->get_backend(context);
0afbb937 11313
2387f644 11314 Expression* nil_check =
11315 Expression::make_binary(OPERATOR_EQEQ, this->expr_,
11316 Expression::make_nil(loc), loc);
ea664253 11317 Bexpression* bnil_check = nil_check->get_backend(context);
0afbb937 11318
2387f644 11319 Gogo* gogo = context->gogo();
ea664253 11320 Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
11321 loc)->get_backend(context);
2387f644 11322
11323 Bexpression* bcond =
a32698ee 11324 gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc);
2387f644 11325 Bstatement* cond_statement = gogo->backend()->expression_statement(bcond);
ea664253 11326 return gogo->backend()->compound_expression(cond_statement, bclosure, loc);
e440a328 11327}
11328
d751bb78 11329// Dump ast representation for an interface field reference.
11330
11331void
11332Interface_field_reference_expression::do_dump_expression(
11333 Ast_dump_context* ast_dump_context) const
11334{
11335 this->expr_->dump_expression(ast_dump_context);
11336 ast_dump_context->ostream() << "." << this->name_;
11337}
11338
e440a328 11339// Make a reference to a field in an interface.
11340
11341Expression*
11342Expression::make_interface_field_reference(Expression* expr,
11343 const std::string& field,
b13c66cd 11344 Location location)
e440a328 11345{
11346 return new Interface_field_reference_expression(expr, field, location);
11347}
11348
11349// A general selector. This is a Parser_expression for LEFT.NAME. It
11350// is lowered after we know the type of the left hand side.
11351
11352class Selector_expression : public Parser_expression
11353{
11354 public:
11355 Selector_expression(Expression* left, const std::string& name,
b13c66cd 11356 Location location)
e440a328 11357 : Parser_expression(EXPRESSION_SELECTOR, location),
11358 left_(left), name_(name)
11359 { }
11360
11361 protected:
11362 int
11363 do_traverse(Traverse* traverse)
11364 { return Expression::traverse(&this->left_, traverse); }
11365
11366 Expression*
ceeb4318 11367 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
e440a328 11368
11369 Expression*
11370 do_copy()
11371 {
11372 return new Selector_expression(this->left_->copy(), this->name_,
11373 this->location());
11374 }
11375
d751bb78 11376 void
11377 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11378
e440a328 11379 private:
11380 Expression*
11381 lower_method_expression(Gogo*);
11382
11383 // The expression on the left hand side.
11384 Expression* left_;
11385 // The name on the right hand side.
11386 std::string name_;
11387};
11388
11389// Lower a selector expression once we know the real type of the left
11390// hand side.
11391
11392Expression*
ceeb4318 11393Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11394 int)
e440a328 11395{
11396 Expression* left = this->left_;
11397 if (left->is_type_expression())
11398 return this->lower_method_expression(gogo);
11399 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11400 this->location());
11401}
11402
11403// Lower a method expression T.M or (*T).M. We turn this into a
11404// function literal.
11405
11406Expression*
11407Selector_expression::lower_method_expression(Gogo* gogo)
11408{
b13c66cd 11409 Location location = this->location();
e440a328 11410 Type* type = this->left_->type();
11411 const std::string& name(this->name_);
11412
11413 bool is_pointer;
11414 if (type->points_to() == NULL)
11415 is_pointer = false;
11416 else
11417 {
11418 is_pointer = true;
11419 type = type->points_to();
11420 }
11421 Named_type* nt = type->named_type();
11422 if (nt == NULL)
11423 {
11424 error_at(location,
11425 ("method expression requires named type or "
11426 "pointer to named type"));
11427 return Expression::make_error(location);
11428 }
11429
11430 bool is_ambiguous;
11431 Method* method = nt->method_function(name, &is_ambiguous);
ab1468c3 11432 const Typed_identifier* imethod = NULL;
dcc8506b 11433 if (method == NULL && !is_pointer)
ab1468c3 11434 {
11435 Interface_type* it = nt->interface_type();
11436 if (it != NULL)
11437 imethod = it->find_method(name);
11438 }
11439
11440 if (method == NULL && imethod == NULL)
e440a328 11441 {
11442 if (!is_ambiguous)
dcc8506b 11443 error_at(location, "type %<%s%s%> has no method %<%s%>",
11444 is_pointer ? "*" : "",
e440a328 11445 nt->message_name().c_str(),
11446 Gogo::message_name(name).c_str());
11447 else
dcc8506b 11448 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
e440a328 11449 Gogo::message_name(name).c_str(),
dcc8506b 11450 is_pointer ? "*" : "",
e440a328 11451 nt->message_name().c_str());
11452 return Expression::make_error(location);
11453 }
11454
ab1468c3 11455 if (method != NULL && !is_pointer && !method->is_value_method())
e440a328 11456 {
11457 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
11458 nt->message_name().c_str(),
11459 Gogo::message_name(name).c_str());
11460 return Expression::make_error(location);
11461 }
11462
11463 // Build a new function type in which the receiver becomes the first
11464 // argument.
ab1468c3 11465 Function_type* method_type;
11466 if (method != NULL)
11467 {
11468 method_type = method->type();
c484d925 11469 go_assert(method_type->is_method());
ab1468c3 11470 }
11471 else
11472 {
11473 method_type = imethod->type()->function_type();
c484d925 11474 go_assert(method_type != NULL && !method_type->is_method());
ab1468c3 11475 }
e440a328 11476
11477 const char* const receiver_name = "$this";
11478 Typed_identifier_list* parameters = new Typed_identifier_list();
11479 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
11480 location));
11481
11482 const Typed_identifier_list* method_parameters = method_type->parameters();
11483 if (method_parameters != NULL)
11484 {
f470da59 11485 int i = 0;
e440a328 11486 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11487 p != method_parameters->end();
f470da59 11488 ++p, ++i)
11489 {
68883531 11490 if (!p->name().empty())
f470da59 11491 parameters->push_back(*p);
11492 else
11493 {
11494 char buf[20];
11495 snprintf(buf, sizeof buf, "$param%d", i);
11496 parameters->push_back(Typed_identifier(buf, p->type(),
11497 p->location()));
11498 }
11499 }
e440a328 11500 }
11501
11502 const Typed_identifier_list* method_results = method_type->results();
11503 Typed_identifier_list* results;
11504 if (method_results == NULL)
11505 results = NULL;
11506 else
11507 {
11508 results = new Typed_identifier_list();
11509 for (Typed_identifier_list::const_iterator p = method_results->begin();
11510 p != method_results->end();
11511 ++p)
11512 results->push_back(*p);
11513 }
11514
11515 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
11516 location);
11517 if (method_type->is_varargs())
11518 fntype->set_is_varargs();
11519
11520 // We generate methods which always takes a pointer to the receiver
11521 // as their first argument. If this is for a pointer type, we can
11522 // simply reuse the existing function. We use an internal hack to
11523 // get the right type.
8381eda7 11524 // FIXME: This optimization is disabled because it doesn't yet work
11525 // with function descriptors when the method expression is not
11526 // directly called.
11527 if (method != NULL && is_pointer && false)
e440a328 11528 {
11529 Named_object* mno = (method->needs_stub_method()
11530 ? method->stub_object()
11531 : method->named_object());
11532 Expression* f = Expression::make_func_reference(mno, NULL, location);
11533 f = Expression::make_cast(fntype, f, location);
11534 Type_conversion_expression* tce =
11535 static_cast<Type_conversion_expression*>(f);
11536 tce->set_may_convert_function_types();
11537 return f;
11538 }
11539
11540 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
11541 location);
11542
11543 Named_object* vno = gogo->lookup(receiver_name, NULL);
c484d925 11544 go_assert(vno != NULL);
e440a328 11545 Expression* ve = Expression::make_var_reference(vno, location);
ab1468c3 11546 Expression* bm;
11547 if (method != NULL)
11548 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
11549 else
11550 bm = Expression::make_interface_field_reference(ve, name, location);
f690b0bb 11551
11552 // Even though we found the method above, if it has an error type we
11553 // may see an error here.
11554 if (bm->is_error_expression())
463fe805 11555 {
11556 gogo->finish_function(location);
11557 return bm;
11558 }
e440a328 11559
11560 Expression_list* args;
f470da59 11561 if (parameters->size() <= 1)
e440a328 11562 args = NULL;
11563 else
11564 {
11565 args = new Expression_list();
f470da59 11566 Typed_identifier_list::const_iterator p = parameters->begin();
11567 ++p;
11568 for (; p != parameters->end(); ++p)
e440a328 11569 {
11570 vno = gogo->lookup(p->name(), NULL);
c484d925 11571 go_assert(vno != NULL);
e440a328 11572 args->push_back(Expression::make_var_reference(vno, location));
11573 }
11574 }
11575
ceeb4318 11576 gogo->start_block(location);
11577
e440a328 11578 Call_expression* call = Expression::make_call(bm, args,
11579 method_type->is_varargs(),
11580 location);
11581
0afbb937 11582 Statement* s = Statement::make_return_from_call(call, location);
e440a328 11583 gogo->add_statement(s);
11584
ceeb4318 11585 Block* b = gogo->finish_block(location);
11586
11587 gogo->add_block(b, location);
11588
11589 // Lower the call in case there are multiple results.
11590 gogo->lower_block(no, b);
a32698ee 11591 gogo->flatten_block(no, b);
ceeb4318 11592
e440a328 11593 gogo->finish_function(location);
11594
11595 return Expression::make_func_reference(no, NULL, location);
11596}
11597
d751bb78 11598// Dump the ast for a selector expression.
11599
11600void
11601Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11602 const
11603{
11604 ast_dump_context->dump_expression(this->left_);
11605 ast_dump_context->ostream() << ".";
11606 ast_dump_context->ostream() << this->name_;
11607}
11608
e440a328 11609// Make a selector expression.
11610
11611Expression*
11612Expression::make_selector(Expression* left, const std::string& name,
b13c66cd 11613 Location location)
e440a328 11614{
11615 return new Selector_expression(left, name, location);
11616}
11617
da244e59 11618// Class Allocation_expression.
e440a328 11619
da244e59 11620int
11621Allocation_expression::do_traverse(Traverse* traverse)
e440a328 11622{
da244e59 11623 return Type::traverse(this->type_, traverse);
11624}
e440a328 11625
da244e59 11626Type*
11627Allocation_expression::do_type()
11628{
11629 return Type::make_pointer_type(this->type_);
11630}
e440a328 11631
da244e59 11632// Make a copy of an allocation expression.
e440a328 11633
da244e59 11634Expression*
11635Allocation_expression::do_copy()
11636{
11637 Allocation_expression* alloc =
11638 new Allocation_expression(this->type_, this->location());
11639 if (this->allocate_on_stack_)
11640 alloc->set_allocate_on_stack();
11641 return alloc;
11642}
e440a328 11643
ea664253 11644// Return the backend representation for an allocation expression.
e440a328 11645
ea664253 11646Bexpression*
11647Allocation_expression::do_get_backend(Translate_context* context)
e440a328 11648{
2c809f8f 11649 Gogo* gogo = context->gogo();
11650 Location loc = this->location();
da244e59 11651
d5d1c295 11652 if (this->allocate_on_stack_)
da244e59 11653 {
2a305b85 11654 int64_t size;
11655 bool ok = this->type_->backend_type_size(gogo, &size);
11656 if (!ok)
11657 {
11658 go_assert(saw_errors());
11659 return gogo->backend()->error_expression();
11660 }
d5d1c295 11661 return gogo->backend()->stack_allocation_expression(size, loc);
da244e59 11662 }
11663
2a305b85 11664 Btype* btype = this->type_->get_backend(gogo);
11665 Bexpression* space =
ea664253 11666 gogo->allocate_memory(this->type_, loc)->get_backend(context);
d5d1c295 11667 Btype* pbtype = gogo->backend()->pointer_type(btype);
ea664253 11668 return gogo->backend()->convert_expression(pbtype, space, loc);
e440a328 11669}
11670
d751bb78 11671// Dump ast representation for an allocation expression.
11672
11673void
11674Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11675 const
11676{
11677 ast_dump_context->ostream() << "new(";
11678 ast_dump_context->dump_type(this->type_);
11679 ast_dump_context->ostream() << ")";
11680}
11681
e440a328 11682// Make an allocation expression.
11683
11684Expression*
b13c66cd 11685Expression::make_allocation(Type* type, Location location)
e440a328 11686{
11687 return new Allocation_expression(type, location);
11688}
11689
da244e59 11690// Class Struct_construction_expression.
e440a328 11691
11692// Traversal.
11693
11694int
11695Struct_construction_expression::do_traverse(Traverse* traverse)
11696{
0c4f5a19 11697 if (this->vals_ != NULL)
11698 {
11699 if (this->traverse_order_ == NULL)
11700 {
11701 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11702 return TRAVERSE_EXIT;
11703 }
11704 else
11705 {
11706 for (std::vector<int>::const_iterator p =
11707 this->traverse_order_->begin();
11708 p != this->traverse_order_->end();
11709 ++p)
11710 {
11711 if (Expression::traverse(&this->vals_->at(*p), traverse)
11712 == TRAVERSE_EXIT)
11713 return TRAVERSE_EXIT;
11714 }
11715 }
11716 }
e440a328 11717 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11718 return TRAVERSE_EXIT;
11719 return TRAVERSE_CONTINUE;
11720}
11721
11722// Return whether this is a constant initializer.
11723
11724bool
11725Struct_construction_expression::is_constant_struct() const
11726{
11727 if (this->vals_ == NULL)
11728 return true;
11729 for (Expression_list::const_iterator pv = this->vals_->begin();
11730 pv != this->vals_->end();
11731 ++pv)
11732 {
11733 if (*pv != NULL
11734 && !(*pv)->is_constant()
11735 && (!(*pv)->is_composite_literal()
11736 || (*pv)->is_nonconstant_composite_literal()))
11737 return false;
11738 }
11739
11740 const Struct_field_list* fields = this->type_->struct_type()->fields();
11741 for (Struct_field_list::const_iterator pf = fields->begin();
11742 pf != fields->end();
11743 ++pf)
11744 {
11745 // There are no constant constructors for interfaces.
11746 if (pf->type()->interface_type() != NULL)
11747 return false;
11748 }
11749
11750 return true;
11751}
11752
f9ca30f9 11753// Return whether this struct is immutable.
11754
11755bool
11756Struct_construction_expression::do_is_immutable() const
11757{
11758 if (this->vals_ == NULL)
11759 return true;
11760 for (Expression_list::const_iterator pv = this->vals_->begin();
11761 pv != this->vals_->end();
11762 ++pv)
11763 {
11764 if (*pv != NULL && !(*pv)->is_immutable())
11765 return false;
11766 }
11767 return true;
11768}
11769
e440a328 11770// Final type determination.
11771
11772void
11773Struct_construction_expression::do_determine_type(const Type_context*)
11774{
11775 if (this->vals_ == NULL)
11776 return;
11777 const Struct_field_list* fields = this->type_->struct_type()->fields();
11778 Expression_list::const_iterator pv = this->vals_->begin();
11779 for (Struct_field_list::const_iterator pf = fields->begin();
11780 pf != fields->end();
11781 ++pf, ++pv)
11782 {
11783 if (pv == this->vals_->end())
11784 return;
11785 if (*pv != NULL)
11786 {
11787 Type_context subcontext(pf->type(), false);
11788 (*pv)->determine_type(&subcontext);
11789 }
11790 }
a6cb4c0e 11791 // Extra values are an error we will report elsewhere; we still want
11792 // to determine the type to avoid knockon errors.
11793 for (; pv != this->vals_->end(); ++pv)
11794 (*pv)->determine_type_no_context();
e440a328 11795}
11796
11797// Check types.
11798
11799void
11800Struct_construction_expression::do_check_types(Gogo*)
11801{
11802 if (this->vals_ == NULL)
11803 return;
11804
11805 Struct_type* st = this->type_->struct_type();
11806 if (this->vals_->size() > st->field_count())
11807 {
11808 this->report_error(_("too many expressions for struct"));
11809 return;
11810 }
11811
11812 const Struct_field_list* fields = st->fields();
11813 Expression_list::const_iterator pv = this->vals_->begin();
11814 int i = 0;
11815 for (Struct_field_list::const_iterator pf = fields->begin();
11816 pf != fields->end();
11817 ++pf, ++pv, ++i)
11818 {
11819 if (pv == this->vals_->end())
11820 {
11821 this->report_error(_("too few expressions for struct"));
11822 break;
11823 }
11824
11825 if (*pv == NULL)
11826 continue;
11827
11828 std::string reason;
11829 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
11830 {
11831 if (reason.empty())
11832 error_at((*pv)->location(),
11833 "incompatible type for field %d in struct construction",
11834 i + 1);
11835 else
11836 error_at((*pv)->location(),
11837 ("incompatible type for field %d in "
11838 "struct construction (%s)"),
11839 i + 1, reason.c_str());
11840 this->set_is_error();
11841 }
11842 }
c484d925 11843 go_assert(pv == this->vals_->end());
e440a328 11844}
11845
8ba8cc87 11846// Flatten a struct construction expression. Store the values into
11847// temporaries in case they need interface conversion.
11848
11849Expression*
11850Struct_construction_expression::do_flatten(Gogo*, Named_object*,
11851 Statement_inserter* inserter)
11852{
11853 if (this->vals_ == NULL)
11854 return this;
11855
11856 // If this is a constant struct, we don't need temporaries.
11857 if (this->is_constant_struct())
11858 return this;
11859
11860 Location loc = this->location();
11861 for (Expression_list::iterator pv = this->vals_->begin();
11862 pv != this->vals_->end();
11863 ++pv)
11864 {
11865 if (*pv != NULL)
11866 {
5bf8be8b 11867 if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type())
11868 {
11869 go_assert(saw_errors());
11870 return Expression::make_error(loc);
11871 }
8ba8cc87 11872 if (!(*pv)->is_variable())
11873 {
11874 Temporary_statement* temp =
11875 Statement::make_temporary(NULL, *pv, loc);
11876 inserter->insert(temp);
11877 *pv = Expression::make_temporary_reference(temp, loc);
11878 }
11879 }
11880 }
11881 return this;
11882}
11883
ea664253 11884// Return the backend representation for constructing a struct.
e440a328 11885
ea664253 11886Bexpression*
11887Struct_construction_expression::do_get_backend(Translate_context* context)
e440a328 11888{
11889 Gogo* gogo = context->gogo();
11890
2c809f8f 11891 Btype* btype = this->type_->get_backend(gogo);
e440a328 11892 if (this->vals_ == NULL)
ea664253 11893 return gogo->backend()->zero_expression(btype);
e440a328 11894
e440a328 11895 const Struct_field_list* fields = this->type_->struct_type()->fields();
e440a328 11896 Expression_list::const_iterator pv = this->vals_->begin();
2c809f8f 11897 std::vector<Bexpression*> init;
11898 for (Struct_field_list::const_iterator pf = fields->begin();
11899 pf != fields->end();
11900 ++pf)
e440a328 11901 {
63697958 11902 Btype* fbtype = pf->type()->get_backend(gogo);
e440a328 11903 if (pv == this->vals_->end())
2c809f8f 11904 init.push_back(gogo->backend()->zero_expression(fbtype));
e440a328 11905 else if (*pv == NULL)
11906 {
2c809f8f 11907 init.push_back(gogo->backend()->zero_expression(fbtype));
e440a328 11908 ++pv;
11909 }
11910 else
11911 {
2c809f8f 11912 Expression* val =
11913 Expression::convert_for_assignment(gogo, pf->type(),
11914 *pv, this->location());
ea664253 11915 init.push_back(val->get_backend(context));
e440a328 11916 ++pv;
11917 }
e440a328 11918 }
ea664253 11919 return gogo->backend()->constructor_expression(btype, init, this->location());
e440a328 11920}
11921
11922// Export a struct construction.
11923
11924void
11925Struct_construction_expression::do_export(Export* exp) const
11926{
11927 exp->write_c_string("convert(");
11928 exp->write_type(this->type_);
11929 for (Expression_list::const_iterator pv = this->vals_->begin();
11930 pv != this->vals_->end();
11931 ++pv)
11932 {
11933 exp->write_c_string(", ");
11934 if (*pv != NULL)
11935 (*pv)->export_expression(exp);
11936 }
11937 exp->write_c_string(")");
11938}
11939
d751bb78 11940// Dump ast representation of a struct construction expression.
11941
11942void
11943Struct_construction_expression::do_dump_expression(
11944 Ast_dump_context* ast_dump_context) const
11945{
d751bb78 11946 ast_dump_context->dump_type(this->type_);
11947 ast_dump_context->ostream() << "{";
11948 ast_dump_context->dump_expression_list(this->vals_);
11949 ast_dump_context->ostream() << "}";
11950}
11951
e440a328 11952// Make a struct composite literal. This used by the thunk code.
11953
11954Expression*
11955Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
b13c66cd 11956 Location location)
e440a328 11957{
c484d925 11958 go_assert(type->struct_type() != NULL);
e440a328 11959 return new Struct_construction_expression(type, vals, location);
11960}
11961
da244e59 11962// Class Array_construction_expression.
e440a328 11963
11964// Traversal.
11965
11966int
11967Array_construction_expression::do_traverse(Traverse* traverse)
11968{
11969 if (this->vals_ != NULL
11970 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11971 return TRAVERSE_EXIT;
11972 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11973 return TRAVERSE_EXIT;
11974 return TRAVERSE_CONTINUE;
11975}
11976
11977// Return whether this is a constant initializer.
11978
11979bool
11980Array_construction_expression::is_constant_array() const
11981{
11982 if (this->vals_ == NULL)
11983 return true;
11984
11985 // There are no constant constructors for interfaces.
11986 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11987 return false;
11988
11989 for (Expression_list::const_iterator pv = this->vals_->begin();
11990 pv != this->vals_->end();
11991 ++pv)
11992 {
11993 if (*pv != NULL
11994 && !(*pv)->is_constant()
11995 && (!(*pv)->is_composite_literal()
11996 || (*pv)->is_nonconstant_composite_literal()))
11997 return false;
11998 }
11999 return true;
12000}
12001
f9ca30f9 12002// Return whether this is an immutable array initializer.
12003
12004bool
12005Array_construction_expression::do_is_immutable() const
12006{
12007 if (this->vals_ == NULL)
12008 return true;
12009 for (Expression_list::const_iterator pv = this->vals_->begin();
12010 pv != this->vals_->end();
12011 ++pv)
12012 {
12013 if (*pv != NULL && !(*pv)->is_immutable())
12014 return false;
12015 }
12016 return true;
12017}
12018
e440a328 12019// Final type determination.
12020
12021void
12022Array_construction_expression::do_determine_type(const Type_context*)
12023{
12024 if (this->vals_ == NULL)
12025 return;
12026 Type_context subcontext(this->type_->array_type()->element_type(), false);
12027 for (Expression_list::const_iterator pv = this->vals_->begin();
12028 pv != this->vals_->end();
12029 ++pv)
12030 {
12031 if (*pv != NULL)
12032 (*pv)->determine_type(&subcontext);
12033 }
12034}
12035
12036// Check types.
12037
12038void
12039Array_construction_expression::do_check_types(Gogo*)
12040{
12041 if (this->vals_ == NULL)
12042 return;
12043
12044 Array_type* at = this->type_->array_type();
12045 int i = 0;
12046 Type* element_type = at->element_type();
12047 for (Expression_list::const_iterator pv = this->vals_->begin();
12048 pv != this->vals_->end();
12049 ++pv, ++i)
12050 {
12051 if (*pv != NULL
12052 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12053 {
12054 error_at((*pv)->location(),
12055 "incompatible type for element %d in composite literal",
12056 i + 1);
12057 this->set_is_error();
12058 }
12059 }
e440a328 12060}
12061
8ba8cc87 12062// Flatten an array construction expression. Store the values into
12063// temporaries in case they need interface conversion.
12064
12065Expression*
12066Array_construction_expression::do_flatten(Gogo*, Named_object*,
12067 Statement_inserter* inserter)
12068{
12069 if (this->vals_ == NULL)
12070 return this;
12071
12072 // If this is a constant array, we don't need temporaries.
12073 if (this->is_constant_array())
12074 return this;
12075
12076 Location loc = this->location();
12077 for (Expression_list::iterator pv = this->vals_->begin();
12078 pv != this->vals_->end();
12079 ++pv)
12080 {
12081 if (*pv != NULL)
12082 {
5bf8be8b 12083 if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type())
12084 {
12085 go_assert(saw_errors());
12086 return Expression::make_error(loc);
12087 }
8ba8cc87 12088 if (!(*pv)->is_variable())
12089 {
12090 Temporary_statement* temp =
12091 Statement::make_temporary(NULL, *pv, loc);
12092 inserter->insert(temp);
12093 *pv = Expression::make_temporary_reference(temp, loc);
12094 }
12095 }
12096 }
12097 return this;
12098}
12099
2c809f8f 12100// Get a constructor expression for the array values.
e440a328 12101
2c809f8f 12102Bexpression*
12103Array_construction_expression::get_constructor(Translate_context* context,
12104 Btype* array_btype)
e440a328 12105{
e440a328 12106 Type* element_type = this->type_->array_type()->element_type();
2c809f8f 12107
12108 std::vector<unsigned long> indexes;
12109 std::vector<Bexpression*> vals;
12110 Gogo* gogo = context->gogo();
e440a328 12111 if (this->vals_ != NULL)
12112 {
12113 size_t i = 0;
ffe743ca 12114 std::vector<unsigned long>::const_iterator pi;
12115 if (this->indexes_ != NULL)
12116 pi = this->indexes_->begin();
e440a328 12117 for (Expression_list::const_iterator pv = this->vals_->begin();
12118 pv != this->vals_->end();
12119 ++pv, ++i)
12120 {
ffe743ca 12121 if (this->indexes_ != NULL)
12122 go_assert(pi != this->indexes_->end());
ffe743ca 12123
12124 if (this->indexes_ == NULL)
2c809f8f 12125 indexes.push_back(i);
ffe743ca 12126 else
2c809f8f 12127 indexes.push_back(*pi);
e440a328 12128 if (*pv == NULL)
63697958 12129 {
63697958 12130 Btype* ebtype = element_type->get_backend(gogo);
12131 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
2c809f8f 12132 vals.push_back(zv);
63697958 12133 }
e440a328 12134 else
12135 {
2c809f8f 12136 Expression* val_expr =
12137 Expression::convert_for_assignment(gogo, element_type, *pv,
12138 this->location());
ea664253 12139 vals.push_back(val_expr->get_backend(context));
e440a328 12140 }
ffe743ca 12141 if (this->indexes_ != NULL)
12142 ++pi;
e440a328 12143 }
ffe743ca 12144 if (this->indexes_ != NULL)
12145 go_assert(pi == this->indexes_->end());
e440a328 12146 }
2c809f8f 12147 return gogo->backend()->array_constructor_expression(array_btype, indexes,
12148 vals, this->location());
e440a328 12149}
12150
12151// Export an array construction.
12152
12153void
12154Array_construction_expression::do_export(Export* exp) const
12155{
12156 exp->write_c_string("convert(");
12157 exp->write_type(this->type_);
12158 if (this->vals_ != NULL)
12159 {
ffe743ca 12160 std::vector<unsigned long>::const_iterator pi;
12161 if (this->indexes_ != NULL)
12162 pi = this->indexes_->begin();
e440a328 12163 for (Expression_list::const_iterator pv = this->vals_->begin();
12164 pv != this->vals_->end();
12165 ++pv)
12166 {
12167 exp->write_c_string(", ");
ffe743ca 12168
12169 if (this->indexes_ != NULL)
12170 {
12171 char buf[100];
12172 snprintf(buf, sizeof buf, "%lu", *pi);
12173 exp->write_c_string(buf);
12174 exp->write_c_string(":");
12175 }
12176
e440a328 12177 if (*pv != NULL)
12178 (*pv)->export_expression(exp);
ffe743ca 12179
12180 if (this->indexes_ != NULL)
12181 ++pi;
e440a328 12182 }
12183 }
12184 exp->write_c_string(")");
12185}
12186
d751bb78 12187// Dump ast representation of an array construction expressin.
12188
12189void
12190Array_construction_expression::do_dump_expression(
12191 Ast_dump_context* ast_dump_context) const
12192{
ffe743ca 12193 Expression* length = this->type_->array_type()->length();
8b1c301d 12194
12195 ast_dump_context->ostream() << "[" ;
12196 if (length != NULL)
12197 {
12198 ast_dump_context->dump_expression(length);
12199 }
12200 ast_dump_context->ostream() << "]" ;
d751bb78 12201 ast_dump_context->dump_type(this->type_);
12202 ast_dump_context->ostream() << "{" ;
ffe743ca 12203 if (this->indexes_ == NULL)
12204 ast_dump_context->dump_expression_list(this->vals_);
12205 else
12206 {
12207 Expression_list::const_iterator pv = this->vals_->begin();
12208 for (std::vector<unsigned long>::const_iterator pi =
12209 this->indexes_->begin();
12210 pi != this->indexes_->end();
12211 ++pi, ++pv)
12212 {
12213 if (pi != this->indexes_->begin())
12214 ast_dump_context->ostream() << ", ";
12215 ast_dump_context->ostream() << *pi << ':';
12216 ast_dump_context->dump_expression(*pv);
12217 }
12218 }
d751bb78 12219 ast_dump_context->ostream() << "}" ;
12220
12221}
12222
da244e59 12223// Class Fixed_array_construction_expression.
e440a328 12224
da244e59 12225Fixed_array_construction_expression::Fixed_array_construction_expression(
12226 Type* type, const std::vector<unsigned long>* indexes,
12227 Expression_list* vals, Location location)
12228 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
12229 type, indexes, vals, location)
12230{ go_assert(type->array_type() != NULL && !type->is_slice_type()); }
e440a328 12231
ea664253 12232// Return the backend representation for constructing a fixed array.
e440a328 12233
ea664253 12234Bexpression*
12235Fixed_array_construction_expression::do_get_backend(Translate_context* context)
e440a328 12236{
9f0e0513 12237 Type* type = this->type();
12238 Btype* btype = type->get_backend(context->gogo());
ea664253 12239 return this->get_constructor(context, btype);
e440a328 12240}
12241
76f85fd6 12242Expression*
12243Expression::make_array_composite_literal(Type* type, Expression_list* vals,
12244 Location location)
12245{
12246 go_assert(type->array_type() != NULL && !type->is_slice_type());
12247 return new Fixed_array_construction_expression(type, NULL, vals, location);
12248}
12249
da244e59 12250// Class Slice_construction_expression.
e440a328 12251
da244e59 12252Slice_construction_expression::Slice_construction_expression(
12253 Type* type, const std::vector<unsigned long>* indexes,
12254 Expression_list* vals, Location location)
12255 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION,
12256 type, indexes, vals, location),
12257 valtype_(NULL)
e440a328 12258{
da244e59 12259 go_assert(type->is_slice_type());
23d77f91 12260
da244e59 12261 unsigned long lenval;
12262 Expression* length;
12263 if (vals == NULL || vals->empty())
12264 lenval = 0;
12265 else
12266 {
12267 if (this->indexes() == NULL)
12268 lenval = vals->size();
12269 else
12270 lenval = indexes->back() + 1;
12271 }
12272 Type* int_type = Type::lookup_integer_type("int");
12273 length = Expression::make_integer_ul(lenval, int_type, location);
12274 Type* element_type = type->array_type()->element_type();
12275 this->valtype_ = Type::make_array_type(element_type, length);
12276}
e440a328 12277
e440a328 12278
23d77f91 12279// Traversal.
12280
12281int
12282Slice_construction_expression::do_traverse(Traverse* traverse)
12283{
12284 if (this->Array_construction_expression::do_traverse(traverse)
12285 == TRAVERSE_EXIT)
12286 return TRAVERSE_EXIT;
12287 if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT)
12288 return TRAVERSE_EXIT;
12289 return TRAVERSE_CONTINUE;
12290}
12291
ea664253 12292// Return the backend representation for constructing a slice.
e440a328 12293
ea664253 12294Bexpression*
12295Slice_construction_expression::do_get_backend(Translate_context* context)
e440a328 12296{
f9c68f17 12297 Array_type* array_type = this->type()->array_type();
12298 if (array_type == NULL)
12299 {
c484d925 12300 go_assert(this->type()->is_error());
ea664253 12301 return context->backend()->error_expression();
f9c68f17 12302 }
12303
f23d7786 12304 Location loc = this->location();
f9c68f17 12305 Type* element_type = array_type->element_type();
23d77f91 12306 go_assert(this->valtype_ != NULL);
3d60812e 12307
f23d7786 12308 Expression_list* vals = this->vals();
e440a328 12309 if (this->vals() == NULL || this->vals()->empty())
12310 {
f23d7786 12311 // We need to create a unique value for the empty array literal.
12312 vals = new Expression_list;
12313 vals->push_back(NULL);
e440a328 12314 }
f23d7786 12315 Expression* array_val =
12316 new Fixed_array_construction_expression(this->valtype_, this->indexes(),
12317 vals, loc);
e440a328 12318
f23d7786 12319 bool is_constant_initializer = array_val->is_immutable();
d8829beb 12320
12321 // We have to copy the initial values into heap memory if we are in
12322 // a function or if the values are not constants. We also have to
12323 // copy them if they may contain pointers in a non-constant context,
12324 // as otherwise the garbage collector won't see them.
12325 bool copy_to_heap = (context->function() != NULL
12326 || !is_constant_initializer
12327 || (element_type->has_pointer()
12328 && !context->is_const()));
e440a328 12329
f23d7786 12330 Expression* space;
d8829beb 12331 if (!copy_to_heap)
e440a328 12332 {
f23d7786 12333 // The initializer will only run once.
12334 space = Expression::make_unary(OPERATOR_AND, array_val, loc);
12335 space->unary_expression()->set_is_slice_init();
e440a328 12336 }
12337 else
f23d7786 12338 space = Expression::make_heap_expression(array_val, loc);
e440a328 12339
2c809f8f 12340 // Build a constructor for the slice.
e440a328 12341
f23d7786 12342 Expression* len = this->valtype_->array_type()->length();
12343 Expression* slice_val =
12344 Expression::make_slice_value(this->type(), space, len, len, loc);
ea664253 12345 return slice_val->get_backend(context);
e440a328 12346}
12347
12348// Make a slice composite literal. This is used by the type
12349// descriptor code.
12350
12351Expression*
12352Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
b13c66cd 12353 Location location)
e440a328 12354{
411eb89e 12355 go_assert(type->is_slice_type());
2c809f8f 12356 return new Slice_construction_expression(type, NULL, vals, location);
e440a328 12357}
12358
da244e59 12359// Class Map_construction_expression.
e440a328 12360
12361// Traversal.
12362
12363int
12364Map_construction_expression::do_traverse(Traverse* traverse)
12365{
12366 if (this->vals_ != NULL
12367 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12368 return TRAVERSE_EXIT;
12369 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12370 return TRAVERSE_EXIT;
12371 return TRAVERSE_CONTINUE;
12372}
12373
2c809f8f 12374// Flatten constructor initializer into a temporary variable since
12375// we need to take its address for __go_construct_map.
12376
12377Expression*
12378Map_construction_expression::do_flatten(Gogo* gogo, Named_object*,
12379 Statement_inserter* inserter)
12380{
12381 if (!this->is_error_expression()
12382 && this->vals_ != NULL
12383 && !this->vals_->empty()
12384 && this->constructor_temp_ == NULL)
12385 {
12386 Map_type* mt = this->type_->map_type();
12387 Type* key_type = mt->key_type();
12388 Type* val_type = mt->val_type();
12389 this->element_type_ = Type::make_builtin_struct_type(2,
12390 "__key", key_type,
12391 "__val", val_type);
12392
12393 Expression_list* value_pairs = new Expression_list();
12394 Location loc = this->location();
12395
12396 size_t i = 0;
12397 for (Expression_list::const_iterator pv = this->vals_->begin();
12398 pv != this->vals_->end();
12399 ++pv, ++i)
12400 {
12401 Expression_list* key_value_pair = new Expression_list();
91c0fd76 12402 Expression* key = *pv;
5bf8be8b 12403 if (key->is_error_expression() || key->type()->is_error_type())
12404 {
12405 go_assert(saw_errors());
12406 return Expression::make_error(loc);
12407 }
91c0fd76 12408 if (key->type()->interface_type() != NULL && !key->is_variable())
12409 {
12410 Temporary_statement* temp =
12411 Statement::make_temporary(NULL, key, loc);
12412 inserter->insert(temp);
12413 key = Expression::make_temporary_reference(temp, loc);
12414 }
12415 key = Expression::convert_for_assignment(gogo, key_type, key, loc);
2c809f8f 12416
12417 ++pv;
91c0fd76 12418 Expression* val = *pv;
5bf8be8b 12419 if (val->is_error_expression() || val->type()->is_error_type())
12420 {
12421 go_assert(saw_errors());
12422 return Expression::make_error(loc);
12423 }
91c0fd76 12424 if (val->type()->interface_type() != NULL && !val->is_variable())
12425 {
12426 Temporary_statement* temp =
12427 Statement::make_temporary(NULL, val, loc);
12428 inserter->insert(temp);
12429 val = Expression::make_temporary_reference(temp, loc);
12430 }
12431 val = Expression::convert_for_assignment(gogo, val_type, val, loc);
2c809f8f 12432
12433 key_value_pair->push_back(key);
12434 key_value_pair->push_back(val);
12435 value_pairs->push_back(
12436 Expression::make_struct_composite_literal(this->element_type_,
12437 key_value_pair, loc));
12438 }
12439
e67508fa 12440 Expression* element_count = Expression::make_integer_ul(i, NULL, loc);
2c809f8f 12441 Type* ctor_type =
12442 Type::make_array_type(this->element_type_, element_count);
12443 Expression* constructor =
12444 new Fixed_array_construction_expression(ctor_type, NULL,
12445 value_pairs, loc);
12446
12447 this->constructor_temp_ =
12448 Statement::make_temporary(NULL, constructor, loc);
12449 constructor->issue_nil_check();
12450 this->constructor_temp_->set_is_address_taken();
12451 inserter->insert(this->constructor_temp_);
12452 }
12453
12454 return this;
12455}
12456
e440a328 12457// Final type determination.
12458
12459void
12460Map_construction_expression::do_determine_type(const Type_context*)
12461{
12462 if (this->vals_ == NULL)
12463 return;
12464
12465 Map_type* mt = this->type_->map_type();
12466 Type_context key_context(mt->key_type(), false);
12467 Type_context val_context(mt->val_type(), false);
12468 for (Expression_list::const_iterator pv = this->vals_->begin();
12469 pv != this->vals_->end();
12470 ++pv)
12471 {
12472 (*pv)->determine_type(&key_context);
12473 ++pv;
12474 (*pv)->determine_type(&val_context);
12475 }
12476}
12477
12478// Check types.
12479
12480void
12481Map_construction_expression::do_check_types(Gogo*)
12482{
12483 if (this->vals_ == NULL)
12484 return;
12485
12486 Map_type* mt = this->type_->map_type();
12487 int i = 0;
12488 Type* key_type = mt->key_type();
12489 Type* val_type = mt->val_type();
12490 for (Expression_list::const_iterator pv = this->vals_->begin();
12491 pv != this->vals_->end();
12492 ++pv, ++i)
12493 {
12494 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12495 {
12496 error_at((*pv)->location(),
12497 "incompatible type for element %d key in map construction",
12498 i + 1);
12499 this->set_is_error();
12500 }
12501 ++pv;
12502 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12503 {
12504 error_at((*pv)->location(),
12505 ("incompatible type for element %d value "
12506 "in map construction"),
12507 i + 1);
12508 this->set_is_error();
12509 }
12510 }
12511}
12512
ea664253 12513// Return the backend representation for constructing a map.
e440a328 12514
ea664253 12515Bexpression*
12516Map_construction_expression::do_get_backend(Translate_context* context)
e440a328 12517{
2c809f8f 12518 if (this->is_error_expression())
ea664253 12519 return context->backend()->error_expression();
2c809f8f 12520 Location loc = this->location();
e440a328 12521
e440a328 12522 size_t i = 0;
2c809f8f 12523 Expression* ventries;
e440a328 12524 if (this->vals_ == NULL || this->vals_->empty())
2c809f8f 12525 ventries = Expression::make_nil(loc);
e440a328 12526 else
12527 {
2c809f8f 12528 go_assert(this->constructor_temp_ != NULL);
12529 i = this->vals_->size() / 2;
e440a328 12530
2c809f8f 12531 Expression* ctor_ref =
12532 Expression::make_temporary_reference(this->constructor_temp_, loc);
12533 ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc);
12534 }
e440a328 12535
2c809f8f 12536 Map_type* mt = this->type_->map_type();
12537 if (this->element_type_ == NULL)
12538 this->element_type_ =
12539 Type::make_builtin_struct_type(2,
12540 "__key", mt->key_type(),
12541 "__val", mt->val_type());
12542 Expression* descriptor = Expression::make_map_descriptor(mt, loc);
12543
12544 Type* uintptr_t = Type::lookup_integer_type("uintptr");
e67508fa 12545 Expression* count = Expression::make_integer_ul(i, uintptr_t, loc);
2c809f8f 12546
12547 Expression* entry_size =
12548 Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE);
12549
12550 unsigned int field_index;
12551 const Struct_field* valfield =
12552 this->element_type_->find_local_field("__val", &field_index);
12553 Expression* val_offset =
12554 Expression::make_struct_field_offset(this->element_type_, valfield);
12555 Expression* val_size =
12556 Expression::make_type_info(mt->val_type(), TYPE_INFO_SIZE);
12557
12558 Expression* map_ctor =
12559 Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 6, descriptor, count,
12560 entry_size, val_offset, val_size, ventries);
ea664253 12561 return map_ctor->get_backend(context);
2c809f8f 12562}
e440a328 12563
2c809f8f 12564// Export an array construction.
e440a328 12565
2c809f8f 12566void
12567Map_construction_expression::do_export(Export* exp) const
12568{
12569 exp->write_c_string("convert(");
12570 exp->write_type(this->type_);
12571 for (Expression_list::const_iterator pv = this->vals_->begin();
12572 pv != this->vals_->end();
12573 ++pv)
12574 {
12575 exp->write_c_string(", ");
12576 (*pv)->export_expression(exp);
12577 }
12578 exp->write_c_string(")");
12579}
e440a328 12580
2c809f8f 12581// Dump ast representation for a map construction expression.
d751bb78 12582
12583void
12584Map_construction_expression::do_dump_expression(
12585 Ast_dump_context* ast_dump_context) const
12586{
d751bb78 12587 ast_dump_context->ostream() << "{" ;
8b1c301d 12588 ast_dump_context->dump_expression_list(this->vals_, true);
d751bb78 12589 ast_dump_context->ostream() << "}";
12590}
12591
7795ac51 12592// Class Composite_literal_expression.
e440a328 12593
12594// Traversal.
12595
12596int
12597Composite_literal_expression::do_traverse(Traverse* traverse)
12598{
dbffccfc 12599 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
e440a328 12600 return TRAVERSE_EXIT;
dbffccfc 12601
12602 // If this is a struct composite literal with keys, then the keys
12603 // are field names, not expressions. We don't want to traverse them
12604 // in that case. If we do, we can give an erroneous error "variable
12605 // initializer refers to itself." See bug482.go in the testsuite.
12606 if (this->has_keys_ && this->vals_ != NULL)
12607 {
12608 // The type may not be resolvable at this point.
12609 Type* type = this->type_;
a01f2481 12610
7795ac51 12611 for (int depth = 0; depth < this->depth_; ++depth)
a01f2481 12612 {
12613 if (type->array_type() != NULL)
12614 type = type->array_type()->element_type();
12615 else if (type->map_type() != NULL)
7795ac51 12616 {
12617 if (this->key_path_[depth])
12618 type = type->map_type()->key_type();
12619 else
12620 type = type->map_type()->val_type();
12621 }
a01f2481 12622 else
12623 {
12624 // This error will be reported during lowering.
12625 return TRAVERSE_CONTINUE;
12626 }
12627 }
12628
dbffccfc 12629 while (true)
12630 {
12631 if (type->classification() == Type::TYPE_NAMED)
12632 type = type->named_type()->real_type();
12633 else if (type->classification() == Type::TYPE_FORWARD)
12634 {
12635 Type* t = type->forwarded();
12636 if (t == type)
12637 break;
12638 type = t;
12639 }
12640 else
12641 break;
12642 }
12643
12644 if (type->classification() == Type::TYPE_STRUCT)
12645 {
12646 Expression_list::iterator p = this->vals_->begin();
12647 while (p != this->vals_->end())
12648 {
12649 // Skip key.
12650 ++p;
12651 go_assert(p != this->vals_->end());
12652 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12653 return TRAVERSE_EXIT;
12654 ++p;
12655 }
12656 return TRAVERSE_CONTINUE;
12657 }
12658 }
12659
12660 if (this->vals_ != NULL)
12661 return this->vals_->traverse(traverse);
12662
12663 return TRAVERSE_CONTINUE;
e440a328 12664}
12665
12666// Lower a generic composite literal into a specific version based on
12667// the type.
12668
12669Expression*
ceeb4318 12670Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
12671 Statement_inserter* inserter, int)
e440a328 12672{
12673 Type* type = this->type_;
12674
7795ac51 12675 for (int depth = 0; depth < this->depth_; ++depth)
e440a328 12676 {
12677 if (type->array_type() != NULL)
12678 type = type->array_type()->element_type();
12679 else if (type->map_type() != NULL)
7795ac51 12680 {
12681 if (this->key_path_[depth])
12682 type = type->map_type()->key_type();
12683 else
12684 type = type->map_type()->val_type();
12685 }
e440a328 12686 else
12687 {
5c13bd80 12688 if (!type->is_error())
e440a328 12689 error_at(this->location(),
12690 ("may only omit types within composite literals "
12691 "of slice, array, or map type"));
12692 return Expression::make_error(this->location());
12693 }
12694 }
12695
e00772b3 12696 Type *pt = type->points_to();
12697 bool is_pointer = false;
12698 if (pt != NULL)
12699 {
12700 is_pointer = true;
12701 type = pt;
12702 }
12703
12704 Expression* ret;
5c13bd80 12705 if (type->is_error())
e440a328 12706 return Expression::make_error(this->location());
12707 else if (type->struct_type() != NULL)
e00772b3 12708 ret = this->lower_struct(gogo, type);
e440a328 12709 else if (type->array_type() != NULL)
113ef6a5 12710 ret = this->lower_array(type);
e440a328 12711 else if (type->map_type() != NULL)
e00772b3 12712 ret = this->lower_map(gogo, function, inserter, type);
e440a328 12713 else
12714 {
12715 error_at(this->location(),
12716 ("expected struct, slice, array, or map type "
12717 "for composite literal"));
12718 return Expression::make_error(this->location());
12719 }
e00772b3 12720
12721 if (is_pointer)
2c809f8f 12722 ret = Expression::make_heap_expression(ret, this->location());
e00772b3 12723
12724 return ret;
e440a328 12725}
12726
12727// Lower a struct composite literal.
12728
12729Expression*
81c4b26b 12730Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
e440a328 12731{
b13c66cd 12732 Location location = this->location();
e440a328 12733 Struct_type* st = type->struct_type();
12734 if (this->vals_ == NULL || !this->has_keys_)
07daa4e7 12735 {
e6013c28 12736 if (this->vals_ != NULL
12737 && !this->vals_->empty()
12738 && type->named_type() != NULL
12739 && type->named_type()->named_object()->package() != NULL)
12740 {
12741 for (Struct_field_list::const_iterator pf = st->fields()->begin();
12742 pf != st->fields()->end();
12743 ++pf)
07daa4e7 12744 {
07ba7f26 12745 if (Gogo::is_hidden_name(pf->field_name())
12746 || pf->is_embedded_builtin(gogo))
07daa4e7 12747 error_at(this->location(),
e6013c28 12748 "assignment of unexported field %qs in %qs literal",
12749 Gogo::message_name(pf->field_name()).c_str(),
12750 type->named_type()->message_name().c_str());
07daa4e7 12751 }
12752 }
12753
12754 return new Struct_construction_expression(type, this->vals_, location);
12755 }
e440a328 12756
12757 size_t field_count = st->field_count();
12758 std::vector<Expression*> vals(field_count);
0c4f5a19 12759 std::vector<int>* traverse_order = new(std::vector<int>);
e440a328 12760 Expression_list::const_iterator p = this->vals_->begin();
62750cd5 12761 Expression* external_expr = NULL;
12762 const Named_object* external_no = NULL;
e440a328 12763 while (p != this->vals_->end())
12764 {
12765 Expression* name_expr = *p;
12766
12767 ++p;
c484d925 12768 go_assert(p != this->vals_->end());
e440a328 12769 Expression* val = *p;
12770
12771 ++p;
12772
12773 if (name_expr == NULL)
12774 {
12775 error_at(val->location(), "mixture of field and value initializers");
12776 return Expression::make_error(location);
12777 }
12778
12779 bool bad_key = false;
12780 std::string name;
81c4b26b 12781 const Named_object* no = NULL;
e440a328 12782 switch (name_expr->classification())
12783 {
12784 case EXPRESSION_UNKNOWN_REFERENCE:
12785 name = name_expr->unknown_expression()->name();
7f7ce694 12786 if (type->named_type() != NULL)
12787 {
12788 // If the named object found for this field name comes from a
12789 // different package than the struct it is a part of, do not count
12790 // this incorrect lookup as a usage of the object's package.
12791 no = name_expr->unknown_expression()->named_object();
12792 if (no->package() != NULL
12793 && no->package() != type->named_type()->named_object()->package())
12794 no->package()->forget_usage(name_expr);
12795 }
e440a328 12796 break;
12797
12798 case EXPRESSION_CONST_REFERENCE:
81c4b26b 12799 no = static_cast<Const_expression*>(name_expr)->named_object();
e440a328 12800 break;
12801
12802 case EXPRESSION_TYPE:
12803 {
12804 Type* t = name_expr->type();
12805 Named_type* nt = t->named_type();
12806 if (nt == NULL)
12807 bad_key = true;
12808 else
81c4b26b 12809 no = nt->named_object();
e440a328 12810 }
12811 break;
12812
12813 case EXPRESSION_VAR_REFERENCE:
81c4b26b 12814 no = name_expr->var_expression()->named_object();
e440a328 12815 break;
12816
12817 case EXPRESSION_FUNC_REFERENCE:
81c4b26b 12818 no = name_expr->func_expression()->named_object();
e440a328 12819 break;
12820
12821 case EXPRESSION_UNARY:
12822 // If there is a local variable around with the same name as
12823 // the field, and this occurs in the closure, then the
12824 // parser may turn the field reference into an indirection
12825 // through the closure. FIXME: This is a mess.
12826 {
12827 bad_key = true;
12828 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
12829 if (ue->op() == OPERATOR_MULT)
12830 {
12831 Field_reference_expression* fre =
12832 ue->operand()->field_reference_expression();
12833 if (fre != NULL)
12834 {
12835 Struct_type* st =
12836 fre->expr()->type()->deref()->struct_type();
12837 if (st != NULL)
12838 {
12839 const Struct_field* sf = st->field(fre->field_index());
12840 name = sf->field_name();
2d29d278 12841
12842 // See below. FIXME.
12843 if (!Gogo::is_hidden_name(name)
12844 && name[0] >= 'a'
12845 && name[0] <= 'z')
12846 {
12847 if (gogo->lookup_global(name.c_str()) != NULL)
12848 name = gogo->pack_hidden_name(name, false);
12849 }
12850
e440a328 12851 char buf[20];
12852 snprintf(buf, sizeof buf, "%u", fre->field_index());
12853 size_t buflen = strlen(buf);
12854 if (name.compare(name.length() - buflen, buflen, buf)
12855 == 0)
12856 {
12857 name = name.substr(0, name.length() - buflen);
12858 bad_key = false;
12859 }
12860 }
12861 }
12862 }
12863 }
12864 break;
12865
12866 default:
12867 bad_key = true;
12868 break;
12869 }
12870 if (bad_key)
12871 {
12872 error_at(name_expr->location(), "expected struct field name");
12873 return Expression::make_error(location);
12874 }
12875
81c4b26b 12876 if (no != NULL)
12877 {
62750cd5 12878 if (no->package() != NULL && external_expr == NULL)
12879 {
12880 external_expr = name_expr;
12881 external_no = no;
12882 }
12883
81c4b26b 12884 name = no->name();
12885
12886 // A predefined name won't be packed. If it starts with a
12887 // lower case letter we need to check for that case, because
2d29d278 12888 // the field name will be packed. FIXME.
81c4b26b 12889 if (!Gogo::is_hidden_name(name)
12890 && name[0] >= 'a'
12891 && name[0] <= 'z')
12892 {
12893 Named_object* gno = gogo->lookup_global(name.c_str());
12894 if (gno == no)
12895 name = gogo->pack_hidden_name(name, false);
12896 }
12897 }
12898
e440a328 12899 unsigned int index;
12900 const Struct_field* sf = st->find_local_field(name, &index);
12901 if (sf == NULL)
12902 {
12903 error_at(name_expr->location(), "unknown field %qs in %qs",
12904 Gogo::message_name(name).c_str(),
12905 (type->named_type() != NULL
12906 ? type->named_type()->message_name().c_str()
12907 : "unnamed struct"));
12908 return Expression::make_error(location);
12909 }
12910 if (vals[index] != NULL)
12911 {
12912 error_at(name_expr->location(),
12913 "duplicate value for field %qs in %qs",
12914 Gogo::message_name(name).c_str(),
12915 (type->named_type() != NULL
12916 ? type->named_type()->message_name().c_str()
12917 : "unnamed struct"));
12918 return Expression::make_error(location);
12919 }
12920
07daa4e7 12921 if (type->named_type() != NULL
12922 && type->named_type()->named_object()->package() != NULL
07ba7f26 12923 && (Gogo::is_hidden_name(sf->field_name())
12924 || sf->is_embedded_builtin(gogo)))
07daa4e7 12925 error_at(name_expr->location(),
12926 "assignment of unexported field %qs in %qs literal",
12927 Gogo::message_name(sf->field_name()).c_str(),
12928 type->named_type()->message_name().c_str());
07daa4e7 12929
e440a328 12930 vals[index] = val;
0c4f5a19 12931 traverse_order->push_back(index);
e440a328 12932 }
12933
62750cd5 12934 if (!this->all_are_names_)
12935 {
12936 // This is a weird case like bug462 in the testsuite.
12937 if (external_expr == NULL)
12938 error_at(this->location(), "unknown field in %qs literal",
12939 (type->named_type() != NULL
12940 ? type->named_type()->message_name().c_str()
12941 : "unnamed struct"));
12942 else
12943 error_at(external_expr->location(), "unknown field %qs in %qs",
12944 external_no->message_name().c_str(),
12945 (type->named_type() != NULL
12946 ? type->named_type()->message_name().c_str()
12947 : "unnamed struct"));
12948 return Expression::make_error(location);
12949 }
12950
e440a328 12951 Expression_list* list = new Expression_list;
12952 list->reserve(field_count);
12953 for (size_t i = 0; i < field_count; ++i)
12954 list->push_back(vals[i]);
12955
0c4f5a19 12956 Struct_construction_expression* ret =
12957 new Struct_construction_expression(type, list, location);
12958 ret->set_traverse_order(traverse_order);
12959 return ret;
e440a328 12960}
12961
00773463 12962// Used to sort an index/value array.
12963
12964class Index_value_compare
12965{
12966 public:
12967 bool
12968 operator()(const std::pair<unsigned long, Expression*>& a,
12969 const std::pair<unsigned long, Expression*>& b)
12970 { return a.first < b.first; }
12971};
12972
e440a328 12973// Lower an array composite literal.
12974
12975Expression*
113ef6a5 12976Composite_literal_expression::lower_array(Type* type)
e440a328 12977{
b13c66cd 12978 Location location = this->location();
e440a328 12979 if (this->vals_ == NULL || !this->has_keys_)
ffe743ca 12980 return this->make_array(type, NULL, this->vals_);
e440a328 12981
ffe743ca 12982 std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
12983 indexes->reserve(this->vals_->size());
00773463 12984 bool indexes_out_of_order = false;
ffe743ca 12985 Expression_list* vals = new Expression_list();
12986 vals->reserve(this->vals_->size());
e440a328 12987 unsigned long index = 0;
12988 Expression_list::const_iterator p = this->vals_->begin();
12989 while (p != this->vals_->end())
12990 {
12991 Expression* index_expr = *p;
12992
12993 ++p;
c484d925 12994 go_assert(p != this->vals_->end());
e440a328 12995 Expression* val = *p;
12996
12997 ++p;
12998
ffe743ca 12999 if (index_expr == NULL)
13000 {
13001 if (!indexes->empty())
13002 indexes->push_back(index);
13003 }
13004 else
e440a328 13005 {
ffe743ca 13006 if (indexes->empty() && !vals->empty())
13007 {
13008 for (size_t i = 0; i < vals->size(); ++i)
13009 indexes->push_back(i);
13010 }
13011
0c77715b 13012 Numeric_constant nc;
13013 if (!index_expr->numeric_constant_value(&nc))
e440a328 13014 {
e440a328 13015 error_at(index_expr->location(),
13016 "index expression is not integer constant");
13017 return Expression::make_error(location);
13018 }
6f6d9955 13019
0c77715b 13020 switch (nc.to_unsigned_long(&index))
e440a328 13021 {
0c77715b 13022 case Numeric_constant::NC_UL_VALID:
13023 break;
13024 case Numeric_constant::NC_UL_NOTINT:
13025 error_at(index_expr->location(),
13026 "index expression is not integer constant");
13027 return Expression::make_error(location);
13028 case Numeric_constant::NC_UL_NEGATIVE:
e440a328 13029 error_at(index_expr->location(), "index expression is negative");
13030 return Expression::make_error(location);
0c77715b 13031 case Numeric_constant::NC_UL_BIG:
e440a328 13032 error_at(index_expr->location(), "index value overflow");
13033 return Expression::make_error(location);
0c77715b 13034 default:
13035 go_unreachable();
e440a328 13036 }
6f6d9955 13037
13038 Named_type* ntype = Type::lookup_integer_type("int");
13039 Integer_type* inttype = ntype->integer_type();
0c77715b 13040 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8)
13041 && index >> (inttype->bits() - 1) != 0)
6f6d9955 13042 {
6f6d9955 13043 error_at(index_expr->location(), "index value overflow");
13044 return Expression::make_error(location);
13045 }
13046
ffe743ca 13047 if (std::find(indexes->begin(), indexes->end(), index)
13048 != indexes->end())
e440a328 13049 {
ffe743ca 13050 error_at(index_expr->location(), "duplicate value for index %lu",
e440a328 13051 index);
13052 return Expression::make_error(location);
13053 }
ffe743ca 13054
00773463 13055 if (!indexes->empty() && index < indexes->back())
13056 indexes_out_of_order = true;
13057
ffe743ca 13058 indexes->push_back(index);
e440a328 13059 }
13060
ffe743ca 13061 vals->push_back(val);
13062
e440a328 13063 ++index;
13064 }
13065
ffe743ca 13066 if (indexes->empty())
13067 {
13068 delete indexes;
13069 indexes = NULL;
13070 }
e440a328 13071
00773463 13072 if (indexes_out_of_order)
13073 {
13074 typedef std::vector<std::pair<unsigned long, Expression*> > V;
13075
13076 V v;
13077 v.reserve(indexes->size());
13078 std::vector<unsigned long>::const_iterator pi = indexes->begin();
13079 for (Expression_list::const_iterator pe = vals->begin();
13080 pe != vals->end();
13081 ++pe, ++pi)
13082 v.push_back(std::make_pair(*pi, *pe));
13083
13084 std::sort(v.begin(), v.end(), Index_value_compare());
13085
13086 delete indexes;
13087 delete vals;
13088 indexes = new std::vector<unsigned long>();
13089 indexes->reserve(v.size());
13090 vals = new Expression_list();
13091 vals->reserve(v.size());
13092
13093 for (V::const_iterator p = v.begin(); p != v.end(); ++p)
13094 {
13095 indexes->push_back(p->first);
13096 vals->push_back(p->second);
13097 }
13098 }
13099
ffe743ca 13100 return this->make_array(type, indexes, vals);
e440a328 13101}
13102
13103// Actually build the array composite literal. This handles
13104// [...]{...}.
13105
13106Expression*
ffe743ca 13107Composite_literal_expression::make_array(
13108 Type* type,
13109 const std::vector<unsigned long>* indexes,
13110 Expression_list* vals)
e440a328 13111{
b13c66cd 13112 Location location = this->location();
e440a328 13113 Array_type* at = type->array_type();
ffe743ca 13114
e440a328 13115 if (at->length() != NULL && at->length()->is_nil_expression())
13116 {
ffe743ca 13117 size_t size;
13118 if (vals == NULL)
13119 size = 0;
00773463 13120 else if (indexes != NULL)
13121 size = indexes->back() + 1;
13122 else
ffe743ca 13123 {
13124 size = vals->size();
13125 Integer_type* it = Type::lookup_integer_type("int")->integer_type();
13126 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8)
13127 && size >> (it->bits() - 1) != 0)
13128 {
13129 error_at(location, "too many elements in composite literal");
13130 return Expression::make_error(location);
13131 }
13132 }
ffe743ca 13133
e67508fa 13134 Expression* elen = Expression::make_integer_ul(size, NULL, location);
e440a328 13135 at = Type::make_array_type(at->element_type(), elen);
13136 type = at;
13137 }
ffe743ca 13138 else if (at->length() != NULL
13139 && !at->length()->is_error_expression()
13140 && this->vals_ != NULL)
13141 {
13142 Numeric_constant nc;
13143 unsigned long val;
13144 if (at->length()->numeric_constant_value(&nc)
13145 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
13146 {
13147 if (indexes == NULL)
13148 {
13149 if (this->vals_->size() > val)
13150 {
13151 error_at(location, "too many elements in composite literal");
13152 return Expression::make_error(location);
13153 }
13154 }
13155 else
13156 {
00773463 13157 unsigned long max = indexes->back();
ffe743ca 13158 if (max >= val)
13159 {
13160 error_at(location,
13161 ("some element keys in composite literal "
13162 "are out of range"));
13163 return Expression::make_error(location);
13164 }
13165 }
13166 }
13167 }
13168
e440a328 13169 if (at->length() != NULL)
ffe743ca 13170 return new Fixed_array_construction_expression(type, indexes, vals,
13171 location);
e440a328 13172 else
2c809f8f 13173 return new Slice_construction_expression(type, indexes, vals, location);
e440a328 13174}
13175
13176// Lower a map composite literal.
13177
13178Expression*
a287720d 13179Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
ceeb4318 13180 Statement_inserter* inserter,
a287720d 13181 Type* type)
e440a328 13182{
b13c66cd 13183 Location location = this->location();
e440a328 13184 if (this->vals_ != NULL)
13185 {
13186 if (!this->has_keys_)
13187 {
13188 error_at(location, "map composite literal must have keys");
13189 return Expression::make_error(location);
13190 }
13191
a287720d 13192 for (Expression_list::iterator p = this->vals_->begin();
e440a328 13193 p != this->vals_->end();
13194 p += 2)
13195 {
13196 if (*p == NULL)
13197 {
13198 ++p;
13199 error_at((*p)->location(),
13200 "map composite literal must have keys for every value");
13201 return Expression::make_error(location);
13202 }
a287720d 13203 // Make sure we have lowered the key; it may not have been
13204 // lowered in order to handle keys for struct composite
13205 // literals. Lower it now to get the right error message.
13206 if ((*p)->unknown_expression() != NULL)
13207 {
13208 (*p)->unknown_expression()->clear_is_composite_literal_key();
ceeb4318 13209 gogo->lower_expression(function, inserter, &*p);
c484d925 13210 go_assert((*p)->is_error_expression());
a287720d 13211 return Expression::make_error(location);
13212 }
e440a328 13213 }
13214 }
13215
13216 return new Map_construction_expression(type, this->vals_, location);
13217}
13218
d751bb78 13219// Dump ast representation for a composite literal expression.
13220
13221void
13222Composite_literal_expression::do_dump_expression(
13223 Ast_dump_context* ast_dump_context) const
13224{
8b1c301d 13225 ast_dump_context->ostream() << "composite(";
d751bb78 13226 ast_dump_context->dump_type(this->type_);
13227 ast_dump_context->ostream() << ", {";
8b1c301d 13228 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
d751bb78 13229 ast_dump_context->ostream() << "})";
13230}
13231
e440a328 13232// Make a composite literal expression.
13233
13234Expression*
13235Expression::make_composite_literal(Type* type, int depth, bool has_keys,
62750cd5 13236 Expression_list* vals, bool all_are_names,
b13c66cd 13237 Location location)
e440a328 13238{
13239 return new Composite_literal_expression(type, depth, has_keys, vals,
62750cd5 13240 all_are_names, location);
e440a328 13241}
13242
13243// Return whether this expression is a composite literal.
13244
13245bool
13246Expression::is_composite_literal() const
13247{
13248 switch (this->classification_)
13249 {
13250 case EXPRESSION_COMPOSITE_LITERAL:
13251 case EXPRESSION_STRUCT_CONSTRUCTION:
13252 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
2c809f8f 13253 case EXPRESSION_SLICE_CONSTRUCTION:
e440a328 13254 case EXPRESSION_MAP_CONSTRUCTION:
13255 return true;
13256 default:
13257 return false;
13258 }
13259}
13260
13261// Return whether this expression is a composite literal which is not
13262// constant.
13263
13264bool
13265Expression::is_nonconstant_composite_literal() const
13266{
13267 switch (this->classification_)
13268 {
13269 case EXPRESSION_STRUCT_CONSTRUCTION:
13270 {
13271 const Struct_construction_expression *psce =
13272 static_cast<const Struct_construction_expression*>(this);
13273 return !psce->is_constant_struct();
13274 }
13275 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13276 {
13277 const Fixed_array_construction_expression *pace =
13278 static_cast<const Fixed_array_construction_expression*>(this);
13279 return !pace->is_constant_array();
13280 }
2c809f8f 13281 case EXPRESSION_SLICE_CONSTRUCTION:
e440a328 13282 {
2c809f8f 13283 const Slice_construction_expression *pace =
13284 static_cast<const Slice_construction_expression*>(this);
e440a328 13285 return !pace->is_constant_array();
13286 }
13287 case EXPRESSION_MAP_CONSTRUCTION:
13288 return true;
13289 default:
13290 return false;
13291 }
13292}
13293
35a54f17 13294// Return true if this is a variable or temporary_variable.
13295
13296bool
13297Expression::is_variable() const
13298{
13299 switch (this->classification_)
13300 {
13301 case EXPRESSION_VAR_REFERENCE:
13302 case EXPRESSION_TEMPORARY_REFERENCE:
13303 case EXPRESSION_SET_AND_USE_TEMPORARY:
13304 return true;
13305 default:
13306 return false;
13307 }
13308}
13309
e440a328 13310// Return true if this is a reference to a local variable.
13311
13312bool
13313Expression::is_local_variable() const
13314{
13315 const Var_expression* ve = this->var_expression();
13316 if (ve == NULL)
13317 return false;
13318 const Named_object* no = ve->named_object();
13319 return (no->is_result_variable()
13320 || (no->is_variable() && !no->var_value()->is_global()));
13321}
13322
13323// Class Type_guard_expression.
13324
13325// Traversal.
13326
13327int
13328Type_guard_expression::do_traverse(Traverse* traverse)
13329{
13330 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
13331 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13332 return TRAVERSE_EXIT;
13333 return TRAVERSE_CONTINUE;
13334}
13335
2c809f8f 13336Expression*
13337Type_guard_expression::do_flatten(Gogo*, Named_object*,
13338 Statement_inserter* inserter)
13339{
5bf8be8b 13340 if (this->expr_->is_error_expression()
13341 || this->expr_->type()->is_error_type())
13342 {
13343 go_assert(saw_errors());
13344 return Expression::make_error(this->location());
13345 }
13346
2c809f8f 13347 if (!this->expr_->is_variable())
13348 {
13349 Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_,
13350 this->location());
13351 inserter->insert(temp);
13352 this->expr_ =
13353 Expression::make_temporary_reference(temp, this->location());
13354 }
13355 return this;
13356}
13357
e440a328 13358// Check types of a type guard expression. The expression must have
13359// an interface type, but the actual type conversion is checked at run
13360// time.
13361
13362void
13363Type_guard_expression::do_check_types(Gogo*)
13364{
e440a328 13365 Type* expr_type = this->expr_->type();
7e9da23f 13366 if (expr_type->interface_type() == NULL)
f725ade8 13367 {
5c13bd80 13368 if (!expr_type->is_error() && !this->type_->is_error())
f725ade8 13369 this->report_error(_("type assertion only valid for interface types"));
13370 this->set_is_error();
13371 }
e440a328 13372 else if (this->type_->interface_type() == NULL)
13373 {
13374 std::string reason;
13375 if (!expr_type->interface_type()->implements_interface(this->type_,
13376 &reason))
13377 {
5c13bd80 13378 if (!this->type_->is_error())
e440a328 13379 {
f725ade8 13380 if (reason.empty())
13381 this->report_error(_("impossible type assertion: "
13382 "type does not implement interface"));
13383 else
13384 error_at(this->location(),
13385 ("impossible type assertion: "
13386 "type does not implement interface (%s)"),
13387 reason.c_str());
e440a328 13388 }
f725ade8 13389 this->set_is_error();
e440a328 13390 }
13391 }
13392}
13393
ea664253 13394// Return the backend representation for a type guard expression.
e440a328 13395
ea664253 13396Bexpression*
13397Type_guard_expression::do_get_backend(Translate_context* context)
e440a328 13398{
2c809f8f 13399 Expression* conversion;
7e9da23f 13400 if (this->type_->interface_type() != NULL)
2c809f8f 13401 conversion =
13402 Expression::convert_interface_to_interface(this->type_, this->expr_,
13403 true, this->location());
e440a328 13404 else
2c809f8f 13405 conversion =
13406 Expression::convert_for_assignment(context->gogo(), this->type_,
13407 this->expr_, this->location());
13408
ea664253 13409 return conversion->get_backend(context);
e440a328 13410}
13411
d751bb78 13412// Dump ast representation for a type guard expression.
13413
13414void
2c809f8f 13415Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
d751bb78 13416 const
13417{
13418 this->expr_->dump_expression(ast_dump_context);
13419 ast_dump_context->ostream() << ".";
13420 ast_dump_context->dump_type(this->type_);
13421}
13422
e440a328 13423// Make a type guard expression.
13424
13425Expression*
13426Expression::make_type_guard(Expression* expr, Type* type,
b13c66cd 13427 Location location)
e440a328 13428{
13429 return new Type_guard_expression(expr, type, location);
13430}
13431
2c809f8f 13432// Class Heap_expression.
e440a328 13433
da244e59 13434// Return the type of the expression stored on the heap.
e440a328 13435
da244e59 13436Type*
13437Heap_expression::do_type()
13438{ return Type::make_pointer_type(this->expr_->type()); }
e440a328 13439
ea664253 13440// Return the backend representation for allocating an expression on the heap.
e440a328 13441
ea664253 13442Bexpression*
13443Heap_expression::do_get_backend(Translate_context* context)
e440a328 13444{
02c19a1a 13445 if (this->expr_->is_error_expression() || this->expr_->type()->is_error())
ea664253 13446 return context->backend()->error_expression();
2c809f8f 13447
02c19a1a 13448 Location loc = this->location();
2c809f8f 13449 Gogo* gogo = context->gogo();
02c19a1a 13450 Btype* btype = this->type()->get_backend(gogo);
ea664253 13451 Bexpression* space = Expression::make_allocation(this->expr_->type(),
13452 loc)->get_backend(context);
02c19a1a 13453
13454 Bstatement* decl;
13455 Named_object* fn = context->function();
13456 go_assert(fn != NULL);
13457 Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn);
13458 Bvariable* space_temp =
13459 gogo->backend()->temporary_variable(fndecl, context->bblock(), btype,
13460 space, true, loc, &decl);
13461 space = gogo->backend()->var_expression(space_temp, loc);
9b27b43c 13462 Btype* expr_btype = this->expr_->type()->get_backend(gogo);
13463 Bexpression* ref =
13464 gogo->backend()->indirect_expression(expr_btype, space, true, loc);
02c19a1a 13465
ea664253 13466 Bexpression* bexpr = this->expr_->get_backend(context);
02c19a1a 13467 Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc);
13468 decl = gogo->backend()->compound_statement(decl, assn);
13469 space = gogo->backend()->var_expression(space_temp, loc);
ea664253 13470 return gogo->backend()->compound_expression(decl, space, loc);
e440a328 13471}
13472
2c809f8f 13473// Dump ast representation for a heap expression.
d751bb78 13474
13475void
2c809f8f 13476Heap_expression::do_dump_expression(
d751bb78 13477 Ast_dump_context* ast_dump_context) const
13478{
13479 ast_dump_context->ostream() << "&(";
13480 ast_dump_context->dump_expression(this->expr_);
13481 ast_dump_context->ostream() << ")";
13482}
13483
2c809f8f 13484// Allocate an expression on the heap.
e440a328 13485
13486Expression*
2c809f8f 13487Expression::make_heap_expression(Expression* expr, Location location)
e440a328 13488{
2c809f8f 13489 return new Heap_expression(expr, location);
e440a328 13490}
13491
13492// Class Receive_expression.
13493
13494// Return the type of a receive expression.
13495
13496Type*
13497Receive_expression::do_type()
13498{
e429e3bd 13499 if (this->is_error_expression())
13500 return Type::make_error_type();
e440a328 13501 Channel_type* channel_type = this->channel_->type()->channel_type();
13502 if (channel_type == NULL)
e429e3bd 13503 {
13504 this->report_error(_("expected channel"));
13505 return Type::make_error_type();
13506 }
e440a328 13507 return channel_type->element_type();
13508}
13509
13510// Check types for a receive expression.
13511
13512void
13513Receive_expression::do_check_types(Gogo*)
13514{
13515 Type* type = this->channel_->type();
5c13bd80 13516 if (type->is_error())
e440a328 13517 {
e429e3bd 13518 go_assert(saw_errors());
e440a328 13519 this->set_is_error();
13520 return;
13521 }
13522 if (type->channel_type() == NULL)
13523 {
13524 this->report_error(_("expected channel"));
13525 return;
13526 }
13527 if (!type->channel_type()->may_receive())
13528 {
13529 this->report_error(_("invalid receive on send-only channel"));
13530 return;
13531 }
13532}
13533
2c809f8f 13534// Flattening for receive expressions creates a temporary variable to store
13535// received data in for receives.
13536
13537Expression*
13538Receive_expression::do_flatten(Gogo*, Named_object*,
13539 Statement_inserter* inserter)
13540{
13541 Channel_type* channel_type = this->channel_->type()->channel_type();
13542 if (channel_type == NULL)
13543 {
13544 go_assert(saw_errors());
13545 return this;
13546 }
5bf8be8b 13547 else if (this->channel_->is_error_expression())
13548 {
13549 go_assert(saw_errors());
13550 return Expression::make_error(this->location());
13551 }
2c809f8f 13552
13553 Type* element_type = channel_type->element_type();
13554 if (this->temp_receiver_ == NULL)
13555 {
13556 this->temp_receiver_ = Statement::make_temporary(element_type, NULL,
13557 this->location());
13558 this->temp_receiver_->set_is_address_taken();
13559 inserter->insert(this->temp_receiver_);
13560 }
13561
13562 return this;
13563}
13564
ea664253 13565// Get the backend representation for a receive expression.
e440a328 13566
ea664253 13567Bexpression*
13568Receive_expression::do_get_backend(Translate_context* context)
e440a328 13569{
f24f10bb 13570 Location loc = this->location();
13571
e440a328 13572 Channel_type* channel_type = this->channel_->type()->channel_type();
5b8368f4 13573 if (channel_type == NULL)
13574 {
c484d925 13575 go_assert(this->channel_->type()->is_error());
ea664253 13576 return context->backend()->error_expression();
5b8368f4 13577 }
f24f10bb 13578 Expression* td = Expression::make_type_descriptor(channel_type, loc);
e440a328 13579
2c809f8f 13580 Expression* recv_ref =
13581 Expression::make_temporary_reference(this->temp_receiver_, loc);
13582 Expression* recv_addr =
13583 Expression::make_temporary_reference(this->temp_receiver_, loc);
13584 recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc);
13585 Expression* recv =
13586 Runtime::make_call(Runtime::RECEIVE, loc, 3,
13587 td, this->channel_, recv_addr);
ea664253 13588 return Expression::make_compound(recv, recv_ref, loc)->get_backend(context);
e440a328 13589}
13590
d751bb78 13591// Dump ast representation for a receive expression.
13592
13593void
13594Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13595{
13596 ast_dump_context->ostream() << " <- " ;
13597 ast_dump_context->dump_expression(channel_);
13598}
13599
e440a328 13600// Make a receive expression.
13601
13602Receive_expression*
b13c66cd 13603Expression::make_receive(Expression* channel, Location location)
e440a328 13604{
13605 return new Receive_expression(channel, location);
13606}
13607
e440a328 13608// An expression which evaluates to a pointer to the type descriptor
13609// of a type.
13610
13611class Type_descriptor_expression : public Expression
13612{
13613 public:
b13c66cd 13614 Type_descriptor_expression(Type* type, Location location)
e440a328 13615 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13616 type_(type)
13617 { }
13618
13619 protected:
4b686186 13620 int
13621 do_traverse(Traverse*);
13622
e440a328 13623 Type*
13624 do_type()
13625 { return Type::make_type_descriptor_ptr_type(); }
13626
f9ca30f9 13627 bool
13628 do_is_immutable() const
13629 { return true; }
13630
e440a328 13631 void
13632 do_determine_type(const Type_context*)
13633 { }
13634
13635 Expression*
13636 do_copy()
13637 { return this; }
13638
ea664253 13639 Bexpression*
13640 do_get_backend(Translate_context* context)
a1d23b41 13641 {
ea664253 13642 return this->type_->type_descriptor_pointer(context->gogo(),
13643 this->location());
a1d23b41 13644 }
e440a328 13645
d751bb78 13646 void
13647 do_dump_expression(Ast_dump_context*) const;
13648
e440a328 13649 private:
13650 // The type for which this is the descriptor.
13651 Type* type_;
13652};
13653
4b686186 13654int
13655Type_descriptor_expression::do_traverse(Traverse* traverse)
13656{
13657 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13658 return TRAVERSE_EXIT;
13659 return TRAVERSE_CONTINUE;
13660}
13661
d751bb78 13662// Dump ast representation for a type descriptor expression.
13663
13664void
13665Type_descriptor_expression::do_dump_expression(
13666 Ast_dump_context* ast_dump_context) const
13667{
13668 ast_dump_context->dump_type(this->type_);
13669}
13670
e440a328 13671// Make a type descriptor expression.
13672
13673Expression*
b13c66cd 13674Expression::make_type_descriptor(Type* type, Location location)
e440a328 13675{
13676 return new Type_descriptor_expression(type, location);
13677}
13678
aa5ae575 13679// An expression which evaluates to a pointer to the Garbage Collection symbol
13680// of a type.
13681
13682class GC_symbol_expression : public Expression
13683{
13684 public:
13685 GC_symbol_expression(Type* type)
13686 : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()),
13687 type_(type)
13688 {}
13689
13690 protected:
13691 Type*
13692 do_type()
23d77f91 13693 { return Type::lookup_integer_type("uintptr"); }
aa5ae575 13694
13695 bool
13696 do_is_immutable() const
13697 { return true; }
13698
13699 void
13700 do_determine_type(const Type_context*)
13701 { }
13702
13703 Expression*
13704 do_copy()
13705 { return this; }
13706
13707 Bexpression*
13708 do_get_backend(Translate_context* context)
13709 { return this->type_->gc_symbol_pointer(context->gogo()); }
13710
13711 void
13712 do_dump_expression(Ast_dump_context*) const;
13713
13714 private:
13715 // The type which this gc symbol describes.
13716 Type* type_;
13717};
13718
13719// Dump ast representation for a gc symbol expression.
13720
13721void
13722GC_symbol_expression::do_dump_expression(
13723 Ast_dump_context* ast_dump_context) const
13724{
13725 ast_dump_context->ostream() << "gcdata(";
13726 ast_dump_context->dump_type(this->type_);
13727 ast_dump_context->ostream() << ")";
13728}
13729
13730// Make a gc symbol expression.
13731
13732Expression*
13733Expression::make_gc_symbol(Type* type)
13734{
13735 return new GC_symbol_expression(type);
13736}
13737
e440a328 13738// An expression which evaluates to some characteristic of a type.
13739// This is only used to initialize fields of a type descriptor. Using
13740// a new expression class is slightly inefficient but gives us a good
13741// separation between the frontend and the middle-end with regard to
13742// how types are laid out.
13743
13744class Type_info_expression : public Expression
13745{
13746 public:
13747 Type_info_expression(Type* type, Type_info type_info)
b13c66cd 13748 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
e440a328 13749 type_(type), type_info_(type_info)
13750 { }
13751
13752 protected:
0e168074 13753 bool
13754 do_is_immutable() const
13755 { return true; }
13756
e440a328 13757 Type*
13758 do_type();
13759
13760 void
13761 do_determine_type(const Type_context*)
13762 { }
13763
13764 Expression*
13765 do_copy()
13766 { return this; }
13767
ea664253 13768 Bexpression*
13769 do_get_backend(Translate_context* context);
e440a328 13770
d751bb78 13771 void
13772 do_dump_expression(Ast_dump_context*) const;
13773
e440a328 13774 private:
13775 // The type for which we are getting information.
13776 Type* type_;
13777 // What information we want.
13778 Type_info type_info_;
13779};
13780
13781// The type is chosen to match what the type descriptor struct
13782// expects.
13783
13784Type*
13785Type_info_expression::do_type()
13786{
13787 switch (this->type_info_)
13788 {
13789 case TYPE_INFO_SIZE:
13790 return Type::lookup_integer_type("uintptr");
13791 case TYPE_INFO_ALIGNMENT:
13792 case TYPE_INFO_FIELD_ALIGNMENT:
13793 return Type::lookup_integer_type("uint8");
13794 default:
c3e6f413 13795 go_unreachable();
e440a328 13796 }
13797}
13798
ea664253 13799// Return the backend representation for type information.
e440a328 13800
ea664253 13801Bexpression*
13802Type_info_expression::do_get_backend(Translate_context* context)
e440a328 13803{
927a01eb 13804 Gogo* gogo = context->gogo();
2a305b85 13805 bool ok = true;
3f378015 13806 int64_t val;
927a01eb 13807 switch (this->type_info_)
e440a328 13808 {
927a01eb 13809 case TYPE_INFO_SIZE:
2a305b85 13810 ok = this->type_->backend_type_size(gogo, &val);
927a01eb 13811 break;
13812 case TYPE_INFO_ALIGNMENT:
2a305b85 13813 ok = this->type_->backend_type_align(gogo, &val);
927a01eb 13814 break;
13815 case TYPE_INFO_FIELD_ALIGNMENT:
2a305b85 13816 ok = this->type_->backend_type_field_align(gogo, &val);
927a01eb 13817 break;
13818 default:
13819 go_unreachable();
e440a328 13820 }
2a305b85 13821 if (!ok)
13822 {
13823 go_assert(saw_errors());
13824 return gogo->backend()->error_expression();
13825 }
3f378015 13826 Expression* e = Expression::make_integer_int64(val, this->type(),
13827 this->location());
13828 return e->get_backend(context);
e440a328 13829}
13830
d751bb78 13831// Dump ast representation for a type info expression.
13832
13833void
13834Type_info_expression::do_dump_expression(
13835 Ast_dump_context* ast_dump_context) const
13836{
13837 ast_dump_context->ostream() << "typeinfo(";
13838 ast_dump_context->dump_type(this->type_);
13839 ast_dump_context->ostream() << ",";
13840 ast_dump_context->ostream() <<
13841 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
13842 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
13843 : this->type_info_ == TYPE_INFO_SIZE ? "size "
13844 : "unknown");
13845 ast_dump_context->ostream() << ")";
13846}
13847
e440a328 13848// Make a type info expression.
13849
13850Expression*
13851Expression::make_type_info(Type* type, Type_info type_info)
13852{
13853 return new Type_info_expression(type, type_info);
13854}
13855
35a54f17 13856// An expression that evaluates to some characteristic of a slice.
13857// This is used when indexing, bound-checking, or nil checking a slice.
13858
13859class Slice_info_expression : public Expression
13860{
13861 public:
13862 Slice_info_expression(Expression* slice, Slice_info slice_info,
13863 Location location)
13864 : Expression(EXPRESSION_SLICE_INFO, location),
13865 slice_(slice), slice_info_(slice_info)
13866 { }
13867
13868 protected:
13869 Type*
13870 do_type();
13871
13872 void
13873 do_determine_type(const Type_context*)
13874 { }
13875
13876 Expression*
13877 do_copy()
13878 {
13879 return new Slice_info_expression(this->slice_->copy(), this->slice_info_,
13880 this->location());
13881 }
13882
ea664253 13883 Bexpression*
13884 do_get_backend(Translate_context* context);
35a54f17 13885
13886 void
13887 do_dump_expression(Ast_dump_context*) const;
13888
13889 void
13890 do_issue_nil_check()
13891 { this->slice_->issue_nil_check(); }
13892
13893 private:
13894 // The slice for which we are getting information.
13895 Expression* slice_;
13896 // What information we want.
13897 Slice_info slice_info_;
13898};
13899
13900// Return the type of the slice info.
13901
13902Type*
13903Slice_info_expression::do_type()
13904{
13905 switch (this->slice_info_)
13906 {
13907 case SLICE_INFO_VALUE_POINTER:
13908 return Type::make_pointer_type(
13909 this->slice_->type()->array_type()->element_type());
13910 case SLICE_INFO_LENGTH:
13911 case SLICE_INFO_CAPACITY:
13912 return Type::lookup_integer_type("int");
13913 default:
13914 go_unreachable();
13915 }
13916}
13917
ea664253 13918// Return the backend information for slice information.
35a54f17 13919
ea664253 13920Bexpression*
13921Slice_info_expression::do_get_backend(Translate_context* context)
35a54f17 13922{
13923 Gogo* gogo = context->gogo();
ea664253 13924 Bexpression* bslice = this->slice_->get_backend(context);
35a54f17 13925 switch (this->slice_info_)
13926 {
13927 case SLICE_INFO_VALUE_POINTER:
13928 case SLICE_INFO_LENGTH:
13929 case SLICE_INFO_CAPACITY:
ea664253 13930 return gogo->backend()->struct_field_expression(bslice, this->slice_info_,
13931 this->location());
35a54f17 13932 break;
13933 default:
13934 go_unreachable();
13935 }
35a54f17 13936}
13937
13938// Dump ast representation for a type info expression.
13939
13940void
13941Slice_info_expression::do_dump_expression(
13942 Ast_dump_context* ast_dump_context) const
13943{
13944 ast_dump_context->ostream() << "sliceinfo(";
13945 this->slice_->dump_expression(ast_dump_context);
13946 ast_dump_context->ostream() << ",";
13947 ast_dump_context->ostream() <<
13948 (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values"
13949 : this->slice_info_ == SLICE_INFO_LENGTH ? "length"
13950 : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity "
13951 : "unknown");
13952 ast_dump_context->ostream() << ")";
13953}
13954
13955// Make a slice info expression.
13956
13957Expression*
13958Expression::make_slice_info(Expression* slice, Slice_info slice_info,
13959 Location location)
13960{
13961 return new Slice_info_expression(slice, slice_info, location);
13962}
13963
2c809f8f 13964// An expression that represents a slice value: a struct with value pointer,
13965// length, and capacity fields.
13966
13967class Slice_value_expression : public Expression
13968{
13969 public:
13970 Slice_value_expression(Type* type, Expression* valptr, Expression* len,
13971 Expression* cap, Location location)
13972 : Expression(EXPRESSION_SLICE_VALUE, location),
13973 type_(type), valptr_(valptr), len_(len), cap_(cap)
13974 { }
13975
13976 protected:
13977 int
13978 do_traverse(Traverse*);
13979
13980 Type*
13981 do_type()
13982 { return this->type_; }
13983
13984 void
13985 do_determine_type(const Type_context*)
13986 { go_unreachable(); }
13987
13988 Expression*
13989 do_copy()
13990 {
13991 return new Slice_value_expression(this->type_, this->valptr_->copy(),
13992 this->len_->copy(), this->cap_->copy(),
13993 this->location());
13994 }
13995
ea664253 13996 Bexpression*
13997 do_get_backend(Translate_context* context);
2c809f8f 13998
13999 void
14000 do_dump_expression(Ast_dump_context*) const;
14001
14002 private:
14003 // The type of the slice value.
14004 Type* type_;
14005 // The pointer to the values in the slice.
14006 Expression* valptr_;
14007 // The length of the slice.
14008 Expression* len_;
14009 // The capacity of the slice.
14010 Expression* cap_;
14011};
14012
14013int
14014Slice_value_expression::do_traverse(Traverse* traverse)
14015{
55e8ba6a 14016 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT
14017 || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT
2c809f8f 14018 || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT
14019 || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
14020 return TRAVERSE_EXIT;
14021 return TRAVERSE_CONTINUE;
14022}
14023
ea664253 14024Bexpression*
14025Slice_value_expression::do_get_backend(Translate_context* context)
2c809f8f 14026{
14027 std::vector<Bexpression*> vals(3);
ea664253 14028 vals[0] = this->valptr_->get_backend(context);
14029 vals[1] = this->len_->get_backend(context);
14030 vals[2] = this->cap_->get_backend(context);
2c809f8f 14031
14032 Gogo* gogo = context->gogo();
14033 Btype* btype = this->type_->get_backend(gogo);
ea664253 14034 return gogo->backend()->constructor_expression(btype, vals, this->location());
2c809f8f 14035}
14036
14037void
14038Slice_value_expression::do_dump_expression(
14039 Ast_dump_context* ast_dump_context) const
14040{
14041 ast_dump_context->ostream() << "slicevalue(";
14042 ast_dump_context->ostream() << "values: ";
14043 this->valptr_->dump_expression(ast_dump_context);
14044 ast_dump_context->ostream() << ", length: ";
14045 this->len_->dump_expression(ast_dump_context);
14046 ast_dump_context->ostream() << ", capacity: ";
14047 this->cap_->dump_expression(ast_dump_context);
14048 ast_dump_context->ostream() << ")";
14049}
14050
14051Expression*
14052Expression::make_slice_value(Type* at, Expression* valptr, Expression* len,
14053 Expression* cap, Location location)
14054{
14055 go_assert(at->is_slice_type());
14056 return new Slice_value_expression(at, valptr, len, cap, location);
14057}
2387f644 14058
14059// An expression that evaluates to some characteristic of a non-empty interface.
14060// This is used to access the method table or underlying object of an interface.
14061
14062class Interface_info_expression : public Expression
14063{
14064 public:
14065 Interface_info_expression(Expression* iface, Interface_info iface_info,
2c809f8f 14066 Location location)
2387f644 14067 : Expression(EXPRESSION_INTERFACE_INFO, location),
14068 iface_(iface), iface_info_(iface_info)
14069 { }
14070
14071 protected:
14072 Type*
14073 do_type();
14074
14075 void
14076 do_determine_type(const Type_context*)
14077 { }
14078
14079 Expression*
14080 do_copy()
14081 {
14082 return new Interface_info_expression(this->iface_->copy(),
14083 this->iface_info_, this->location());
14084 }
14085
ea664253 14086 Bexpression*
14087 do_get_backend(Translate_context* context);
2387f644 14088
14089 void
14090 do_dump_expression(Ast_dump_context*) const;
14091
14092 void
14093 do_issue_nil_check()
14094 { this->iface_->issue_nil_check(); }
14095
14096 private:
14097 // The interface for which we are getting information.
14098 Expression* iface_;
14099 // What information we want.
14100 Interface_info iface_info_;
14101};
14102
14103// Return the type of the interface info.
14104
14105Type*
14106Interface_info_expression::do_type()
14107{
14108 switch (this->iface_info_)
14109 {
14110 case INTERFACE_INFO_METHODS:
14111 {
2c809f8f 14112 Type* pdt = Type::make_type_descriptor_ptr_type();
14113 if (this->iface_->type()->interface_type()->is_empty())
14114 return pdt;
14115
2387f644 14116 Location loc = this->location();
14117 Struct_field_list* sfl = new Struct_field_list();
2387f644 14118 sfl->push_back(
14119 Struct_field(Typed_identifier("__type_descriptor", pdt, loc)));
14120
14121 Interface_type* itype = this->iface_->type()->interface_type();
14122 for (Typed_identifier_list::const_iterator p = itype->methods()->begin();
14123 p != itype->methods()->end();
14124 ++p)
14125 {
14126 Function_type* ft = p->type()->function_type();
14127 go_assert(ft->receiver() == NULL);
14128
14129 const Typed_identifier_list* params = ft->parameters();
14130 Typed_identifier_list* mparams = new Typed_identifier_list();
14131 if (params != NULL)
14132 mparams->reserve(params->size() + 1);
14133 Type* vt = Type::make_pointer_type(Type::make_void_type());
14134 mparams->push_back(Typed_identifier("", vt, ft->location()));
14135 if (params != NULL)
14136 {
14137 for (Typed_identifier_list::const_iterator pp = params->begin();
14138 pp != params->end();
14139 ++pp)
14140 mparams->push_back(*pp);
14141 }
14142
14143 Typed_identifier_list* mresults = (ft->results() == NULL
14144 ? NULL
14145 : ft->results()->copy());
14146 Backend_function_type* mft =
14147 Type::make_backend_function_type(NULL, mparams, mresults,
14148 ft->location());
14149
14150 std::string fname = Gogo::unpack_hidden_name(p->name());
14151 sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc)));
14152 }
14153
14154 return Type::make_pointer_type(Type::make_struct_type(sfl, loc));
14155 }
14156 case INTERFACE_INFO_OBJECT:
14157 return Type::make_pointer_type(Type::make_void_type());
14158 default:
14159 go_unreachable();
14160 }
14161}
14162
ea664253 14163// Return the backend representation for interface information.
2387f644 14164
ea664253 14165Bexpression*
14166Interface_info_expression::do_get_backend(Translate_context* context)
2387f644 14167{
14168 Gogo* gogo = context->gogo();
ea664253 14169 Bexpression* biface = this->iface_->get_backend(context);
2387f644 14170 switch (this->iface_info_)
14171 {
14172 case INTERFACE_INFO_METHODS:
14173 case INTERFACE_INFO_OBJECT:
ea664253 14174 return gogo->backend()->struct_field_expression(biface, this->iface_info_,
14175 this->location());
2387f644 14176 break;
14177 default:
14178 go_unreachable();
14179 }
2387f644 14180}
14181
14182// Dump ast representation for an interface info expression.
14183
14184void
14185Interface_info_expression::do_dump_expression(
14186 Ast_dump_context* ast_dump_context) const
14187{
2c809f8f 14188 bool is_empty = this->iface_->type()->interface_type()->is_empty();
2387f644 14189 ast_dump_context->ostream() << "interfaceinfo(";
14190 this->iface_->dump_expression(ast_dump_context);
14191 ast_dump_context->ostream() << ",";
14192 ast_dump_context->ostream() <<
2c809f8f 14193 (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods"
14194 : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor"
2387f644 14195 : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object"
14196 : "unknown");
14197 ast_dump_context->ostream() << ")";
14198}
14199
14200// Make an interface info expression.
14201
14202Expression*
14203Expression::make_interface_info(Expression* iface, Interface_info iface_info,
14204 Location location)
14205{
14206 return new Interface_info_expression(iface, iface_info, location);
14207}
14208
2c809f8f 14209// An expression that represents an interface value. The first field is either
14210// a type descriptor for an empty interface or a pointer to the interface method
14211// table for a non-empty interface. The second field is always the object.
14212
14213class Interface_value_expression : public Expression
14214{
14215 public:
14216 Interface_value_expression(Type* type, Expression* first_field,
14217 Expression* obj, Location location)
14218 : Expression(EXPRESSION_INTERFACE_VALUE, location),
14219 type_(type), first_field_(first_field), obj_(obj)
14220 { }
14221
14222 protected:
14223 int
14224 do_traverse(Traverse*);
14225
14226 Type*
14227 do_type()
14228 { return this->type_; }
14229
14230 void
14231 do_determine_type(const Type_context*)
14232 { go_unreachable(); }
14233
14234 Expression*
14235 do_copy()
14236 {
14237 return new Interface_value_expression(this->type_,
14238 this->first_field_->copy(),
14239 this->obj_->copy(), this->location());
14240 }
14241
ea664253 14242 Bexpression*
14243 do_get_backend(Translate_context* context);
2c809f8f 14244
14245 void
14246 do_dump_expression(Ast_dump_context*) const;
14247
14248 private:
14249 // The type of the interface value.
14250 Type* type_;
14251 // The first field of the interface (either a type descriptor or a pointer
14252 // to the method table.
14253 Expression* first_field_;
14254 // The underlying object of the interface.
14255 Expression* obj_;
14256};
14257
14258int
14259Interface_value_expression::do_traverse(Traverse* traverse)
14260{
14261 if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT
14262 || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT)
14263 return TRAVERSE_EXIT;
14264 return TRAVERSE_CONTINUE;
14265}
14266
ea664253 14267Bexpression*
14268Interface_value_expression::do_get_backend(Translate_context* context)
2c809f8f 14269{
14270 std::vector<Bexpression*> vals(2);
ea664253 14271 vals[0] = this->first_field_->get_backend(context);
14272 vals[1] = this->obj_->get_backend(context);
2c809f8f 14273
14274 Gogo* gogo = context->gogo();
14275 Btype* btype = this->type_->get_backend(gogo);
ea664253 14276 return gogo->backend()->constructor_expression(btype, vals, this->location());
2c809f8f 14277}
14278
14279void
14280Interface_value_expression::do_dump_expression(
14281 Ast_dump_context* ast_dump_context) const
14282{
14283 ast_dump_context->ostream() << "interfacevalue(";
14284 ast_dump_context->ostream() <<
14285 (this->type_->interface_type()->is_empty()
14286 ? "type_descriptor: "
14287 : "methods: ");
14288 this->first_field_->dump_expression(ast_dump_context);
14289 ast_dump_context->ostream() << ", object: ";
14290 this->obj_->dump_expression(ast_dump_context);
14291 ast_dump_context->ostream() << ")";
14292}
14293
14294Expression*
14295Expression::make_interface_value(Type* type, Expression* first_value,
14296 Expression* object, Location location)
14297{
14298 return new Interface_value_expression(type, first_value, object, location);
14299}
14300
14301// An interface method table for a pair of types: an interface type and a type
14302// that implements that interface.
14303
14304class Interface_mtable_expression : public Expression
14305{
14306 public:
14307 Interface_mtable_expression(Interface_type* itype, Type* type,
14308 bool is_pointer, Location location)
14309 : Expression(EXPRESSION_INTERFACE_MTABLE, location),
14310 itype_(itype), type_(type), is_pointer_(is_pointer),
14311 method_table_type_(NULL), bvar_(NULL)
14312 { }
14313
14314 protected:
14315 int
14316 do_traverse(Traverse*);
14317
14318 Type*
14319 do_type();
14320
14321 bool
14322 is_immutable() const
14323 { return true; }
14324
14325 void
14326 do_determine_type(const Type_context*)
14327 { go_unreachable(); }
14328
14329 Expression*
14330 do_copy()
14331 {
14332 return new Interface_mtable_expression(this->itype_, this->type_,
14333 this->is_pointer_, this->location());
14334 }
14335
14336 bool
14337 do_is_addressable() const
14338 { return true; }
14339
ea664253 14340 Bexpression*
14341 do_get_backend(Translate_context* context);
2c809f8f 14342
14343 void
14344 do_dump_expression(Ast_dump_context*) const;
14345
14346 private:
14347 // The interface type for which the methods are defined.
14348 Interface_type* itype_;
14349 // The type to construct the interface method table for.
14350 Type* type_;
14351 // Whether this table contains the method set for the receiver type or the
14352 // pointer receiver type.
14353 bool is_pointer_;
14354 // The type of the method table.
14355 Type* method_table_type_;
14356 // The backend variable that refers to the interface method table.
14357 Bvariable* bvar_;
14358};
14359
14360int
14361Interface_mtable_expression::do_traverse(Traverse* traverse)
14362{
14363 if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT
14364 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
14365 return TRAVERSE_EXIT;
14366 return TRAVERSE_CONTINUE;
14367}
14368
14369Type*
14370Interface_mtable_expression::do_type()
14371{
14372 if (this->method_table_type_ != NULL)
14373 return this->method_table_type_;
14374
14375 const Typed_identifier_list* interface_methods = this->itype_->methods();
14376 go_assert(!interface_methods->empty());
14377
14378 Struct_field_list* sfl = new Struct_field_list;
14379 Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
14380 this->location());
14381 sfl->push_back(Struct_field(tid));
14382 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
14383 p != interface_methods->end();
14384 ++p)
14385 sfl->push_back(Struct_field(*p));
14386 this->method_table_type_ = Type::make_struct_type(sfl, this->location());
14387 return this->method_table_type_;
14388}
14389
ea664253 14390Bexpression*
14391Interface_mtable_expression::do_get_backend(Translate_context* context)
2c809f8f 14392{
14393 Gogo* gogo = context->gogo();
2c809f8f 14394 Location loc = Linemap::predeclared_location();
14395 if (this->bvar_ != NULL)
ea664253 14396 return gogo->backend()->var_expression(this->bvar_, this->location());
2c809f8f 14397
14398 const Typed_identifier_list* interface_methods = this->itype_->methods();
14399 go_assert(!interface_methods->empty());
14400
14401 std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_")
14402 + this->itype_->mangled_name(gogo)
14403 + "__"
14404 + this->type_->mangled_name(gogo));
14405
14406 // See whether this interface has any hidden methods.
14407 bool has_hidden_methods = false;
14408 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
14409 p != interface_methods->end();
14410 ++p)
14411 {
14412 if (Gogo::is_hidden_name(p->name()))
14413 {
14414 has_hidden_methods = true;
14415 break;
14416 }
14417 }
14418
14419 // We already know that the named type is convertible to the
14420 // interface. If the interface has hidden methods, and the named
14421 // type is defined in a different package, then the interface
14422 // conversion table will be defined by that other package.
14423 if (has_hidden_methods
14424 && this->type_->named_type() != NULL
14425 && this->type_->named_type()->named_object()->package() != NULL)
14426 {
14427 Btype* btype = this->type()->get_backend(gogo);
14428 this->bvar_ =
14429 gogo->backend()->immutable_struct_reference(mangled_name, btype, loc);
ea664253 14430 return gogo->backend()->var_expression(this->bvar_, this->location());
2c809f8f 14431 }
14432
14433 // The first element is the type descriptor.
14434 Type* td_type;
14435 if (!this->is_pointer_)
14436 td_type = this->type_;
14437 else
14438 td_type = Type::make_pointer_type(this->type_);
14439
14440 // Build an interface method table for a type: a type descriptor followed by a
14441 // list of function pointers, one for each interface method. This is used for
14442 // interfaces.
14443 Expression_list* svals = new Expression_list();
14444 svals->push_back(Expression::make_type_descriptor(td_type, loc));
14445
14446 Named_type* nt = this->type_->named_type();
14447 Struct_type* st = this->type_->struct_type();
14448 go_assert(nt != NULL || st != NULL);
14449
14450 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
14451 p != interface_methods->end();
14452 ++p)
14453 {
14454 bool is_ambiguous;
14455 Method* m;
14456 if (nt != NULL)
14457 m = nt->method_function(p->name(), &is_ambiguous);
14458 else
14459 m = st->method_function(p->name(), &is_ambiguous);
14460 go_assert(m != NULL);
14461 Named_object* no = m->named_object();
14462
14463 go_assert(no->is_function() || no->is_function_declaration());
14464 svals->push_back(Expression::make_func_code_reference(no, loc));
14465 }
14466
14467 Btype* btype = this->type()->get_backend(gogo);
14468 Expression* mtable = Expression::make_struct_composite_literal(this->type(),
14469 svals, loc);
ea664253 14470 Bexpression* ctor = mtable->get_backend(context);
2c809f8f 14471
14472 bool is_public = has_hidden_methods && this->type_->named_type() != NULL;
14473 this->bvar_ = gogo->backend()->immutable_struct(mangled_name, false,
14474 !is_public, btype, loc);
14475 gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false,
14476 !is_public, btype, loc, ctor);
ea664253 14477 return gogo->backend()->var_expression(this->bvar_, loc);
2c809f8f 14478}
14479
14480void
14481Interface_mtable_expression::do_dump_expression(
14482 Ast_dump_context* ast_dump_context) const
14483{
14484 ast_dump_context->ostream() << "__go_"
14485 << (this->is_pointer_ ? "pimt__" : "imt_");
14486 ast_dump_context->dump_type(this->itype_);
14487 ast_dump_context->ostream() << "__";
14488 ast_dump_context->dump_type(this->type_);
14489}
14490
14491Expression*
14492Expression::make_interface_mtable_ref(Interface_type* itype, Type* type,
14493 bool is_pointer, Location location)
14494{
14495 return new Interface_mtable_expression(itype, type, is_pointer, location);
14496}
14497
e440a328 14498// An expression which evaluates to the offset of a field within a
14499// struct. This, like Type_info_expression, q.v., is only used to
14500// initialize fields of a type descriptor.
14501
14502class Struct_field_offset_expression : public Expression
14503{
14504 public:
14505 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
b13c66cd 14506 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
14507 Linemap::predeclared_location()),
e440a328 14508 type_(type), field_(field)
14509 { }
14510
14511 protected:
f23d7786 14512 bool
14513 do_is_immutable() const
14514 { return true; }
14515
e440a328 14516 Type*
14517 do_type()
14518 { return Type::lookup_integer_type("uintptr"); }
14519
14520 void
14521 do_determine_type(const Type_context*)
14522 { }
14523
14524 Expression*
14525 do_copy()
14526 { return this; }
14527
ea664253 14528 Bexpression*
14529 do_get_backend(Translate_context* context);
e440a328 14530
d751bb78 14531 void
14532 do_dump_expression(Ast_dump_context*) const;
14533
e440a328 14534 private:
14535 // The type of the struct.
14536 Struct_type* type_;
14537 // The field.
14538 const Struct_field* field_;
14539};
14540
ea664253 14541// Return the backend representation for a struct field offset.
e440a328 14542
ea664253 14543Bexpression*
14544Struct_field_offset_expression::do_get_backend(Translate_context* context)
e440a328 14545{
e440a328 14546 const Struct_field_list* fields = this->type_->fields();
e440a328 14547 Struct_field_list::const_iterator p;
2c8bda43 14548 unsigned i = 0;
e440a328 14549 for (p = fields->begin();
14550 p != fields->end();
2c8bda43 14551 ++p, ++i)
14552 if (&*p == this->field_)
14553 break;
c484d925 14554 go_assert(&*p == this->field_);
e440a328 14555
2c8bda43 14556 Gogo* gogo = context->gogo();
14557 Btype* btype = this->type_->get_backend(gogo);
14558
3f378015 14559 int64_t offset = gogo->backend()->type_field_offset(btype, i);
2c8bda43 14560 Type* uptr_type = Type::lookup_integer_type("uintptr");
e67508fa 14561 Expression* ret =
3f378015 14562 Expression::make_integer_int64(offset, uptr_type,
14563 Linemap::predeclared_location());
ea664253 14564 return ret->get_backend(context);
e440a328 14565}
14566
d751bb78 14567// Dump ast representation for a struct field offset expression.
14568
14569void
14570Struct_field_offset_expression::do_dump_expression(
14571 Ast_dump_context* ast_dump_context) const
14572{
14573 ast_dump_context->ostream() << "unsafe.Offsetof(";
2d29d278 14574 ast_dump_context->dump_type(this->type_);
14575 ast_dump_context->ostream() << '.';
14576 ast_dump_context->ostream() <<
14577 Gogo::message_name(this->field_->field_name());
d751bb78 14578 ast_dump_context->ostream() << ")";
14579}
14580
e440a328 14581// Make an expression for a struct field offset.
14582
14583Expression*
14584Expression::make_struct_field_offset(Struct_type* type,
14585 const Struct_field* field)
14586{
14587 return new Struct_field_offset_expression(type, field);
14588}
14589
a9182619 14590// An expression which evaluates to a pointer to the map descriptor of
14591// a map type.
14592
14593class Map_descriptor_expression : public Expression
14594{
14595 public:
b13c66cd 14596 Map_descriptor_expression(Map_type* type, Location location)
a9182619 14597 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
14598 type_(type)
14599 { }
14600
14601 protected:
14602 Type*
14603 do_type()
14604 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14605
14606 void
14607 do_determine_type(const Type_context*)
14608 { }
14609
14610 Expression*
14611 do_copy()
14612 { return this; }
14613
ea664253 14614 Bexpression*
14615 do_get_backend(Translate_context* context)
a9182619 14616 {
ea664253 14617 return this->type_->map_descriptor_pointer(context->gogo(),
14618 this->location());
a9182619 14619 }
14620
d751bb78 14621 void
14622 do_dump_expression(Ast_dump_context*) const;
14623
a9182619 14624 private:
14625 // The type for which this is the descriptor.
14626 Map_type* type_;
14627};
14628
d751bb78 14629// Dump ast representation for a map descriptor expression.
14630
14631void
14632Map_descriptor_expression::do_dump_expression(
14633 Ast_dump_context* ast_dump_context) const
14634{
14635 ast_dump_context->ostream() << "map_descriptor(";
14636 ast_dump_context->dump_type(this->type_);
14637 ast_dump_context->ostream() << ")";
14638}
14639
a9182619 14640// Make a map descriptor expression.
14641
14642Expression*
b13c66cd 14643Expression::make_map_descriptor(Map_type* type, Location location)
a9182619 14644{
14645 return new Map_descriptor_expression(type, location);
14646}
14647
e440a328 14648// An expression which evaluates to the address of an unnamed label.
14649
14650class Label_addr_expression : public Expression
14651{
14652 public:
b13c66cd 14653 Label_addr_expression(Label* label, Location location)
e440a328 14654 : Expression(EXPRESSION_LABEL_ADDR, location),
14655 label_(label)
14656 { }
14657
14658 protected:
14659 Type*
14660 do_type()
14661 { return Type::make_pointer_type(Type::make_void_type()); }
14662
14663 void
14664 do_determine_type(const Type_context*)
14665 { }
14666
14667 Expression*
14668 do_copy()
14669 { return new Label_addr_expression(this->label_, this->location()); }
14670
ea664253 14671 Bexpression*
14672 do_get_backend(Translate_context* context)
14673 { return this->label_->get_addr(context, this->location()); }
e440a328 14674
d751bb78 14675 void
14676 do_dump_expression(Ast_dump_context* ast_dump_context) const
14677 { ast_dump_context->ostream() << this->label_->name(); }
14678
e440a328 14679 private:
14680 // The label whose address we are taking.
14681 Label* label_;
14682};
14683
14684// Make an expression for the address of an unnamed label.
14685
14686Expression*
b13c66cd 14687Expression::make_label_addr(Label* label, Location location)
e440a328 14688{
14689 return new Label_addr_expression(label, location);
14690}
14691
da244e59 14692// Class Conditional_expression.
283a177b 14693
2c809f8f 14694// Traversal.
14695
14696int
14697Conditional_expression::do_traverse(Traverse* traverse)
14698{
14699 if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT
14700 || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT
14701 || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT)
14702 return TRAVERSE_EXIT;
14703 return TRAVERSE_CONTINUE;
14704}
14705
283a177b 14706// Return the type of the conditional expression.
14707
14708Type*
14709Conditional_expression::do_type()
14710{
14711 Type* result_type = Type::make_void_type();
2c809f8f 14712 if (Type::are_identical(this->then_->type(), this->else_->type(), false,
14713 NULL))
283a177b 14714 result_type = this->then_->type();
14715 else if (this->then_->is_nil_expression()
14716 || this->else_->is_nil_expression())
14717 result_type = (!this->then_->is_nil_expression()
14718 ? this->then_->type()
14719 : this->else_->type());
14720 return result_type;
14721}
14722
2c809f8f 14723// Determine type for a conditional expression.
14724
14725void
14726Conditional_expression::do_determine_type(const Type_context* context)
14727{
14728 this->cond_->determine_type_no_context();
14729 this->then_->determine_type(context);
14730 this->else_->determine_type(context);
14731}
14732
283a177b 14733// Get the backend representation of a conditional expression.
14734
ea664253 14735Bexpression*
14736Conditional_expression::do_get_backend(Translate_context* context)
283a177b 14737{
14738 Gogo* gogo = context->gogo();
14739 Btype* result_btype = this->type()->get_backend(gogo);
ea664253 14740 Bexpression* cond = this->cond_->get_backend(context);
14741 Bexpression* then = this->then_->get_backend(context);
14742 Bexpression* belse = this->else_->get_backend(context);
14743 return gogo->backend()->conditional_expression(result_btype, cond, then,
14744 belse, this->location());
283a177b 14745}
14746
14747// Dump ast representation of a conditional expression.
14748
14749void
14750Conditional_expression::do_dump_expression(
14751 Ast_dump_context* ast_dump_context) const
14752{
14753 ast_dump_context->ostream() << "(";
14754 ast_dump_context->dump_expression(this->cond_);
14755 ast_dump_context->ostream() << " ? ";
14756 ast_dump_context->dump_expression(this->then_);
14757 ast_dump_context->ostream() << " : ";
14758 ast_dump_context->dump_expression(this->else_);
14759 ast_dump_context->ostream() << ") ";
14760}
14761
14762// Make a conditional expression.
14763
14764Expression*
14765Expression::make_conditional(Expression* cond, Expression* then,
14766 Expression* else_expr, Location location)
14767{
14768 return new Conditional_expression(cond, then, else_expr, location);
14769}
14770
da244e59 14771// Class Compound_expression.
2c809f8f 14772
14773// Traversal.
14774
14775int
14776Compound_expression::do_traverse(Traverse* traverse)
14777{
14778 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT
14779 || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
14780 return TRAVERSE_EXIT;
14781 return TRAVERSE_CONTINUE;
14782}
14783
14784// Return the type of the compound expression.
14785
14786Type*
14787Compound_expression::do_type()
14788{
14789 return this->expr_->type();
14790}
14791
14792// Determine type for a compound expression.
14793
14794void
14795Compound_expression::do_determine_type(const Type_context* context)
14796{
14797 this->init_->determine_type_no_context();
14798 this->expr_->determine_type(context);
14799}
14800
14801// Get the backend representation of a compound expression.
14802
ea664253 14803Bexpression*
14804Compound_expression::do_get_backend(Translate_context* context)
2c809f8f 14805{
14806 Gogo* gogo = context->gogo();
ea664253 14807 Bexpression* binit = this->init_->get_backend(context);
2c809f8f 14808 Bstatement* init_stmt = gogo->backend()->expression_statement(binit);
ea664253 14809 Bexpression* bexpr = this->expr_->get_backend(context);
14810 return gogo->backend()->compound_expression(init_stmt, bexpr,
14811 this->location());
2c809f8f 14812}
14813
14814// Dump ast representation of a conditional expression.
14815
14816void
14817Compound_expression::do_dump_expression(
14818 Ast_dump_context* ast_dump_context) const
14819{
14820 ast_dump_context->ostream() << "(";
14821 ast_dump_context->dump_expression(this->init_);
14822 ast_dump_context->ostream() << ",";
14823 ast_dump_context->dump_expression(this->expr_);
14824 ast_dump_context->ostream() << ") ";
14825}
14826
14827// Make a compound expression.
14828
14829Expression*
14830Expression::make_compound(Expression* init, Expression* expr, Location location)
14831{
14832 return new Compound_expression(init, expr, location);
14833}
14834
e440a328 14835// Import an expression. This comes at the end in order to see the
14836// various class definitions.
14837
14838Expression*
14839Expression::import_expression(Import* imp)
14840{
14841 int c = imp->peek_char();
14842 if (imp->match_c_string("- ")
14843 || imp->match_c_string("! ")
14844 || imp->match_c_string("^ "))
14845 return Unary_expression::do_import(imp);
14846 else if (c == '(')
14847 return Binary_expression::do_import(imp);
14848 else if (imp->match_c_string("true")
14849 || imp->match_c_string("false"))
14850 return Boolean_expression::do_import(imp);
14851 else if (c == '"')
14852 return String_expression::do_import(imp);
14853 else if (c == '-' || (c >= '0' && c <= '9'))
14854 {
14855 // This handles integers, floats and complex constants.
14856 return Integer_expression::do_import(imp);
14857 }
14858 else if (imp->match_c_string("nil"))
14859 return Nil_expression::do_import(imp);
14860 else if (imp->match_c_string("convert"))
14861 return Type_conversion_expression::do_import(imp);
14862 else
14863 {
14864 error_at(imp->location(), "import error: expected expression");
14865 return Expression::make_error(imp->location());
14866 }
14867}
14868
14869// Class Expression_list.
14870
14871// Traverse the list.
14872
14873int
14874Expression_list::traverse(Traverse* traverse)
14875{
14876 for (Expression_list::iterator p = this->begin();
14877 p != this->end();
14878 ++p)
14879 {
14880 if (*p != NULL)
14881 {
14882 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
14883 return TRAVERSE_EXIT;
14884 }
14885 }
14886 return TRAVERSE_CONTINUE;
14887}
14888
14889// Copy the list.
14890
14891Expression_list*
14892Expression_list::copy()
14893{
14894 Expression_list* ret = new Expression_list();
14895 for (Expression_list::iterator p = this->begin();
14896 p != this->end();
14897 ++p)
14898 {
14899 if (*p == NULL)
14900 ret->push_back(NULL);
14901 else
14902 ret->push_back((*p)->copy());
14903 }
14904 return ret;
14905}
14906
14907// Return whether an expression list has an error expression.
14908
14909bool
14910Expression_list::contains_error() const
14911{
14912 for (Expression_list::const_iterator p = this->begin();
14913 p != this->end();
14914 ++p)
14915 if (*p != NULL && (*p)->is_error_expression())
14916 return true;
14917 return false;
14918}
0c77715b 14919
14920// Class Numeric_constant.
14921
14922// Destructor.
14923
14924Numeric_constant::~Numeric_constant()
14925{
14926 this->clear();
14927}
14928
14929// Copy constructor.
14930
14931Numeric_constant::Numeric_constant(const Numeric_constant& a)
14932 : classification_(a.classification_), type_(a.type_)
14933{
14934 switch (a.classification_)
14935 {
14936 case NC_INVALID:
14937 break;
14938 case NC_INT:
14939 case NC_RUNE:
14940 mpz_init_set(this->u_.int_val, a.u_.int_val);
14941 break;
14942 case NC_FLOAT:
14943 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
14944 break;
14945 case NC_COMPLEX:
fcbea5e4 14946 mpc_init2(this->u_.complex_val, mpc_precision);
14947 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
0c77715b 14948 break;
14949 default:
14950 go_unreachable();
14951 }
14952}
14953
14954// Assignment operator.
14955
14956Numeric_constant&
14957Numeric_constant::operator=(const Numeric_constant& a)
14958{
14959 this->clear();
14960 this->classification_ = a.classification_;
14961 this->type_ = a.type_;
14962 switch (a.classification_)
14963 {
14964 case NC_INVALID:
14965 break;
14966 case NC_INT:
14967 case NC_RUNE:
14968 mpz_init_set(this->u_.int_val, a.u_.int_val);
14969 break;
14970 case NC_FLOAT:
14971 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
14972 break;
14973 case NC_COMPLEX:
fcbea5e4 14974 mpc_init2(this->u_.complex_val, mpc_precision);
14975 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
0c77715b 14976 break;
14977 default:
14978 go_unreachable();
14979 }
14980 return *this;
14981}
14982
14983// Clear the contents.
14984
14985void
14986Numeric_constant::clear()
14987{
14988 switch (this->classification_)
14989 {
14990 case NC_INVALID:
14991 break;
14992 case NC_INT:
14993 case NC_RUNE:
14994 mpz_clear(this->u_.int_val);
14995 break;
14996 case NC_FLOAT:
14997 mpfr_clear(this->u_.float_val);
14998 break;
14999 case NC_COMPLEX:
fcbea5e4 15000 mpc_clear(this->u_.complex_val);
0c77715b 15001 break;
15002 default:
15003 go_unreachable();
15004 }
15005 this->classification_ = NC_INVALID;
15006}
15007
15008// Set to an unsigned long value.
15009
15010void
15011Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
15012{
15013 this->clear();
15014 this->classification_ = NC_INT;
15015 this->type_ = type;
15016 mpz_init_set_ui(this->u_.int_val, val);
15017}
15018
15019// Set to an integer value.
15020
15021void
15022Numeric_constant::set_int(Type* type, const mpz_t val)
15023{
15024 this->clear();
15025 this->classification_ = NC_INT;
15026 this->type_ = type;
15027 mpz_init_set(this->u_.int_val, val);
15028}
15029
15030// Set to a rune value.
15031
15032void
15033Numeric_constant::set_rune(Type* type, const mpz_t val)
15034{
15035 this->clear();
15036 this->classification_ = NC_RUNE;
15037 this->type_ = type;
15038 mpz_init_set(this->u_.int_val, val);
15039}
15040
15041// Set to a floating point value.
15042
15043void
15044Numeric_constant::set_float(Type* type, const mpfr_t val)
15045{
15046 this->clear();
15047 this->classification_ = NC_FLOAT;
15048 this->type_ = type;
833b523c 15049 // Numeric constants do not have negative zero values, so remove
15050 // them here. They also don't have infinity or NaN values, but we
15051 // should never see them here.
15052 if (mpfr_zero_p(val))
15053 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN);
15054 else
15055 mpfr_init_set(this->u_.float_val, val, GMP_RNDN);
0c77715b 15056}
15057
15058// Set to a complex value.
15059
15060void
fcbea5e4 15061Numeric_constant::set_complex(Type* type, const mpc_t val)
0c77715b 15062{
15063 this->clear();
15064 this->classification_ = NC_COMPLEX;
15065 this->type_ = type;
fcbea5e4 15066 mpc_init2(this->u_.complex_val, mpc_precision);
15067 mpc_set(this->u_.complex_val, val, MPC_RNDNN);
0c77715b 15068}
15069
15070// Get an int value.
15071
15072void
15073Numeric_constant::get_int(mpz_t* val) const
15074{
15075 go_assert(this->is_int());
15076 mpz_init_set(*val, this->u_.int_val);
15077}
15078
15079// Get a rune value.
15080
15081void
15082Numeric_constant::get_rune(mpz_t* val) const
15083{
15084 go_assert(this->is_rune());
15085 mpz_init_set(*val, this->u_.int_val);
15086}
15087
15088// Get a floating point value.
15089
15090void
15091Numeric_constant::get_float(mpfr_t* val) const
15092{
15093 go_assert(this->is_float());
15094 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
15095}
15096
15097// Get a complex value.
15098
15099void
fcbea5e4 15100Numeric_constant::get_complex(mpc_t* val) const
0c77715b 15101{
15102 go_assert(this->is_complex());
fcbea5e4 15103 mpc_init2(*val, mpc_precision);
15104 mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
0c77715b 15105}
15106
15107// Express value as unsigned long if possible.
15108
15109Numeric_constant::To_unsigned_long
15110Numeric_constant::to_unsigned_long(unsigned long* val) const
15111{
15112 switch (this->classification_)
15113 {
15114 case NC_INT:
15115 case NC_RUNE:
15116 return this->mpz_to_unsigned_long(this->u_.int_val, val);
15117 case NC_FLOAT:
15118 return this->mpfr_to_unsigned_long(this->u_.float_val, val);
15119 case NC_COMPLEX:
fcbea5e4 15120 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 15121 return NC_UL_NOTINT;
fcbea5e4 15122 return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val),
15123 val);
0c77715b 15124 default:
15125 go_unreachable();
15126 }
15127}
15128
15129// Express integer value as unsigned long if possible.
15130
15131Numeric_constant::To_unsigned_long
15132Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
15133 unsigned long *val) const
15134{
15135 if (mpz_sgn(ival) < 0)
15136 return NC_UL_NEGATIVE;
15137 unsigned long ui = mpz_get_ui(ival);
15138 if (mpz_cmp_ui(ival, ui) != 0)
15139 return NC_UL_BIG;
15140 *val = ui;
15141 return NC_UL_VALID;
15142}
15143
15144// Express floating point value as unsigned long if possible.
15145
15146Numeric_constant::To_unsigned_long
15147Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
15148 unsigned long *val) const
15149{
15150 if (!mpfr_integer_p(fval))
15151 return NC_UL_NOTINT;
15152 mpz_t ival;
15153 mpz_init(ival);
15154 mpfr_get_z(ival, fval, GMP_RNDN);
15155 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
15156 mpz_clear(ival);
15157 return ret;
15158}
15159
15160// Convert value to integer if possible.
15161
15162bool
15163Numeric_constant::to_int(mpz_t* val) const
15164{
15165 switch (this->classification_)
15166 {
15167 case NC_INT:
15168 case NC_RUNE:
15169 mpz_init_set(*val, this->u_.int_val);
15170 return true;
15171 case NC_FLOAT:
15172 if (!mpfr_integer_p(this->u_.float_val))
15173 return false;
15174 mpz_init(*val);
15175 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN);
15176 return true;
15177 case NC_COMPLEX:
fcbea5e4 15178 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))
15179 || !mpfr_integer_p(mpc_realref(this->u_.complex_val)))
0c77715b 15180 return false;
15181 mpz_init(*val);
fcbea5e4 15182 mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 15183 return true;
15184 default:
15185 go_unreachable();
15186 }
15187}
15188
15189// Convert value to floating point if possible.
15190
15191bool
15192Numeric_constant::to_float(mpfr_t* val) const
15193{
15194 switch (this->classification_)
15195 {
15196 case NC_INT:
15197 case NC_RUNE:
15198 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN);
15199 return true;
15200 case NC_FLOAT:
15201 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
15202 return true;
15203 case NC_COMPLEX:
fcbea5e4 15204 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 15205 return false;
fcbea5e4 15206 mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 15207 return true;
15208 default:
15209 go_unreachable();
15210 }
15211}
15212
15213// Convert value to complex.
15214
15215bool
fcbea5e4 15216Numeric_constant::to_complex(mpc_t* val) const
0c77715b 15217{
fcbea5e4 15218 mpc_init2(*val, mpc_precision);
0c77715b 15219 switch (this->classification_)
15220 {
15221 case NC_INT:
15222 case NC_RUNE:
fcbea5e4 15223 mpc_set_z(*val, this->u_.int_val, MPC_RNDNN);
0c77715b 15224 return true;
15225 case NC_FLOAT:
fcbea5e4 15226 mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN);
0c77715b 15227 return true;
15228 case NC_COMPLEX:
fcbea5e4 15229 mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
0c77715b 15230 return true;
15231 default:
15232 go_unreachable();
15233 }
15234}
15235
15236// Get the type.
15237
15238Type*
15239Numeric_constant::type() const
15240{
15241 if (this->type_ != NULL)
15242 return this->type_;
15243 switch (this->classification_)
15244 {
15245 case NC_INT:
15246 return Type::make_abstract_integer_type();
15247 case NC_RUNE:
15248 return Type::make_abstract_character_type();
15249 case NC_FLOAT:
15250 return Type::make_abstract_float_type();
15251 case NC_COMPLEX:
15252 return Type::make_abstract_complex_type();
15253 default:
15254 go_unreachable();
15255 }
15256}
15257
15258// If the constant can be expressed in TYPE, then set the type of the
15259// constant to TYPE and return true. Otherwise return false, and, if
15260// ISSUE_ERROR is true, report an appropriate error message.
15261
15262bool
15263Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
15264{
15265 bool ret;
f11c2155 15266 if (type == NULL || type->is_error())
0c77715b 15267 ret = true;
15268 else if (type->integer_type() != NULL)
15269 ret = this->check_int_type(type->integer_type(), issue_error, loc);
15270 else if (type->float_type() != NULL)
15271 ret = this->check_float_type(type->float_type(), issue_error, loc);
15272 else if (type->complex_type() != NULL)
15273 ret = this->check_complex_type(type->complex_type(), issue_error, loc);
15274 else
5706ab68 15275 {
15276 ret = false;
15277 if (issue_error)
15278 go_assert(saw_errors());
15279 }
0c77715b 15280 if (ret)
15281 this->type_ = type;
15282 return ret;
15283}
15284
15285// Check whether the constant can be expressed in an integer type.
15286
15287bool
15288Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
71a45216 15289 Location location)
0c77715b 15290{
15291 mpz_t val;
15292 switch (this->classification_)
15293 {
15294 case NC_INT:
15295 case NC_RUNE:
15296 mpz_init_set(val, this->u_.int_val);
15297 break;
15298
15299 case NC_FLOAT:
15300 if (!mpfr_integer_p(this->u_.float_val))
15301 {
15302 if (issue_error)
71a45216 15303 {
15304 error_at(location,
15305 "floating point constant truncated to integer");
15306 this->set_invalid();
15307 }
0c77715b 15308 return false;
15309 }
15310 mpz_init(val);
15311 mpfr_get_z(val, this->u_.float_val, GMP_RNDN);
15312 break;
15313
15314 case NC_COMPLEX:
fcbea5e4 15315 if (!mpfr_integer_p(mpc_realref(this->u_.complex_val))
15316 || !mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 15317 {
15318 if (issue_error)
71a45216 15319 {
15320 error_at(location, "complex constant truncated to integer");
15321 this->set_invalid();
15322 }
0c77715b 15323 return false;
15324 }
15325 mpz_init(val);
fcbea5e4 15326 mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 15327 break;
15328
15329 default:
15330 go_unreachable();
15331 }
15332
15333 bool ret;
15334 if (type->is_abstract())
15335 ret = true;
15336 else
15337 {
15338 int bits = mpz_sizeinbase(val, 2);
15339 if (type->is_unsigned())
15340 {
15341 // For an unsigned type we can only accept a nonnegative
15342 // number, and we must be able to represents at least BITS.
15343 ret = mpz_sgn(val) >= 0 && bits <= type->bits();
15344 }
15345 else
15346 {
15347 // For a signed type we need an extra bit to indicate the
15348 // sign. We have to handle the most negative integer
15349 // specially.
15350 ret = (bits + 1 <= type->bits()
15351 || (bits <= type->bits()
15352 && mpz_sgn(val) < 0
15353 && (mpz_scan1(val, 0)
15354 == static_cast<unsigned long>(type->bits() - 1))
15355 && mpz_scan0(val, type->bits()) == ULONG_MAX));
15356 }
15357 }
15358
15359 if (!ret && issue_error)
71a45216 15360 {
15361 error_at(location, "integer constant overflow");
15362 this->set_invalid();
15363 }
0c77715b 15364
15365 return ret;
15366}
15367
15368// Check whether the constant can be expressed in a floating point
15369// type.
15370
15371bool
15372Numeric_constant::check_float_type(Float_type* type, bool issue_error,
d0bcce51 15373 Location location)
0c77715b 15374{
15375 mpfr_t val;
15376 switch (this->classification_)
15377 {
15378 case NC_INT:
15379 case NC_RUNE:
15380 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN);
15381 break;
15382
15383 case NC_FLOAT:
15384 mpfr_init_set(val, this->u_.float_val, GMP_RNDN);
15385 break;
15386
15387 case NC_COMPLEX:
fcbea5e4 15388 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
0c77715b 15389 {
15390 if (issue_error)
71a45216 15391 {
15392 this->set_invalid();
15393 error_at(location, "complex constant truncated to float");
15394 }
0c77715b 15395 return false;
15396 }
fcbea5e4 15397 mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN);
0c77715b 15398 break;
15399
15400 default:
15401 go_unreachable();
15402 }
15403
15404 bool ret;
15405 if (type->is_abstract())
15406 ret = true;
15407 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
15408 {
15409 // A NaN or Infinity always fits in the range of the type.
15410 ret = true;
15411 }
15412 else
15413 {
15414 mp_exp_t exp = mpfr_get_exp(val);
15415 mp_exp_t max_exp;
15416 switch (type->bits())
15417 {
15418 case 32:
15419 max_exp = 128;
15420 break;
15421 case 64:
15422 max_exp = 1024;
15423 break;
15424 default:
15425 go_unreachable();
15426 }
15427
15428 ret = exp <= max_exp;
d0bcce51 15429
15430 if (ret)
15431 {
15432 // Round the constant to the desired type.
15433 mpfr_t t;
15434 mpfr_init(t);
15435 switch (type->bits())
15436 {
15437 case 32:
15438 mpfr_set_prec(t, 24);
15439 break;
15440 case 64:
15441 mpfr_set_prec(t, 53);
15442 break;
15443 default:
15444 go_unreachable();
15445 }
15446 mpfr_set(t, val, GMP_RNDN);
15447 mpfr_set(val, t, GMP_RNDN);
15448 mpfr_clear(t);
15449
15450 this->set_float(type, val);
15451 }
0c77715b 15452 }
15453
15454 mpfr_clear(val);
15455
15456 if (!ret && issue_error)
71a45216 15457 {
15458 error_at(location, "floating point constant overflow");
15459 this->set_invalid();
15460 }
0c77715b 15461
15462 return ret;
15463}
15464
15465// Check whether the constant can be expressed in a complex type.
15466
15467bool
15468Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
d0bcce51 15469 Location location)
0c77715b 15470{
15471 if (type->is_abstract())
15472 return true;
15473
15474 mp_exp_t max_exp;
15475 switch (type->bits())
15476 {
15477 case 64:
15478 max_exp = 128;
15479 break;
15480 case 128:
15481 max_exp = 1024;
15482 break;
15483 default:
15484 go_unreachable();
15485 }
15486
fcbea5e4 15487 mpc_t val;
15488 mpc_init2(val, mpc_precision);
0c77715b 15489 switch (this->classification_)
15490 {
15491 case NC_INT:
15492 case NC_RUNE:
fcbea5e4 15493 mpc_set_z(val, this->u_.int_val, MPC_RNDNN);
0c77715b 15494 break;
15495
15496 case NC_FLOAT:
fcbea5e4 15497 mpc_set_fr(val, this->u_.float_val, MPC_RNDNN);
0c77715b 15498 break;
15499
15500 case NC_COMPLEX:
fcbea5e4 15501 mpc_set(val, this->u_.complex_val, MPC_RNDNN);
0c77715b 15502 break;
15503
15504 default:
15505 go_unreachable();
15506 }
15507
d0bcce51 15508 bool ret = true;
fcbea5e4 15509 if (!mpfr_nan_p(mpc_realref(val))
15510 && !mpfr_inf_p(mpc_realref(val))
15511 && !mpfr_zero_p(mpc_realref(val))
15512 && mpfr_get_exp(mpc_realref(val)) > max_exp)
d0bcce51 15513 {
15514 if (issue_error)
71a45216 15515 {
15516 error_at(location, "complex real part overflow");
15517 this->set_invalid();
15518 }
d0bcce51 15519 ret = false;
15520 }
0c77715b 15521
fcbea5e4 15522 if (!mpfr_nan_p(mpc_imagref(val))
15523 && !mpfr_inf_p(mpc_imagref(val))
15524 && !mpfr_zero_p(mpc_imagref(val))
15525 && mpfr_get_exp(mpc_imagref(val)) > max_exp)
d0bcce51 15526 {
15527 if (issue_error)
71a45216 15528 {
15529 error_at(location, "complex imaginary part overflow");
15530 this->set_invalid();
15531 }
d0bcce51 15532 ret = false;
15533 }
0c77715b 15534
d0bcce51 15535 if (ret)
15536 {
15537 // Round the constant to the desired type.
fcbea5e4 15538 mpc_t t;
d0bcce51 15539 switch (type->bits())
15540 {
15541 case 64:
fcbea5e4 15542 mpc_init2(t, 24);
d0bcce51 15543 break;
15544 case 128:
fcbea5e4 15545 mpc_init2(t, 53);
d0bcce51 15546 break;
15547 default:
15548 go_unreachable();
15549 }
fcbea5e4 15550 mpc_set(t, val, MPC_RNDNN);
15551 mpc_set(val, t, MPC_RNDNN);
15552 mpc_clear(t);
d0bcce51 15553
fcbea5e4 15554 this->set_complex(type, val);
d0bcce51 15555 }
15556
fcbea5e4 15557 mpc_clear(val);
0c77715b 15558
15559 return ret;
15560}
15561
15562// Return an Expression for this value.
15563
15564Expression*
15565Numeric_constant::expression(Location loc) const
15566{
15567 switch (this->classification_)
15568 {
15569 case NC_INT:
e67508fa 15570 return Expression::make_integer_z(&this->u_.int_val, this->type_, loc);
0c77715b 15571 case NC_RUNE:
15572 return Expression::make_character(&this->u_.int_val, this->type_, loc);
15573 case NC_FLOAT:
15574 return Expression::make_float(&this->u_.float_val, this->type_, loc);
15575 case NC_COMPLEX:
fcbea5e4 15576 return Expression::make_complex(&this->u_.complex_val, this->type_, loc);
71a45216 15577 case NC_INVALID:
15578 go_assert(saw_errors());
15579 return Expression::make_error(loc);
0c77715b 15580 default:
15581 go_unreachable();
15582 }
15583}