1// expressions.cc -- Go frontend expression handling. 2 3// Copyright 2009 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7#include "go-system.h" 8 9#include <algorithm> 10 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" 18#include "runtime.h" 19#include "backend.h" 20#include "expressions.h" 21#include "ast-dump.h" 22 23// Class Expression. 24 25Expression::Expression(Expression_classification classification, 26 Location location) 27 : classification_(classification), location_(location) 28{ 29} 30 31Expression::~Expression() 32{ 33} 34 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 69// expression is being discarded. By default, we give an error. 70// Expressions with side effects override. 71 72bool 73Expression::do_discarding_value() 74{ 75 this->unused_value_error(); 76 return false; 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{ 85 go_unreachable(); 86} 87 88// Give an error saying that the value of the expression is not used. 89 90void 91Expression::unused_value_error() 92{ 93 this->report_error(_("value computed is not used")); 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 132// Return an expression handling any conversions which must be done during 133// assignment. 134 135Expression* 136Expression::convert_for_assignment(Gogo* gogo, Type* lhs_type, 137 Expression* rhs, Location location) 138{ 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); 144 145 if (lhs_type->forwarded() != rhs_type->forwarded() 146 && lhs_type->interface_type() != NULL) 147 { 148 if (rhs_type->interface_type() == NULL) 149 return Expression::convert_type_to_interface(lhs_type, rhs, location); 150 else 151 return Expression::convert_interface_to_interface(lhs_type, rhs, false, 152 location); 153 } 154 else if (lhs_type->forwarded() != rhs_type->forwarded() 155 && rhs_type->interface_type() != NULL) 156 return Expression::convert_interface_to_type(lhs_type, rhs, location); 157 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) 158 { 159 // Assigning nil to a slice. 160 Expression* nil = Expression::make_nil(location); 161 Expression* zero = Expression::make_integer_ul(0, NULL, location); 162 return Expression::make_slice_value(lhs_type, nil, zero, zero, location); 163 } 164 else if (rhs_type->is_nil_type()) 165 return Expression::make_nil(location); 166 else if (Type::are_identical(lhs_type, rhs_type, false, NULL)) 167 { 168 // No conversion is needed. 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)) 179 { 180 // Avoid confusion from zero sized variables which may be 181 // represented as non-zero-sized. 182 // TODO(cmang): This check is for a GCC-specific issue, and should be 183 // removed from the frontend. FIXME. 184 int64_t lhs_size = 185 gogo->backend()->type_size(lhs_type->get_backend(gogo)); 186 int64_t rhs_size = 187 gogo->backend()->type_size(rhs_type->get_backend(gogo)); 188 if (rhs_size == 0 || lhs_size == 0) 189 return rhs; 190 191 // This conversion must be permitted by Go, or we wouldn't have 192 // gotten here. 193 return Expression::make_unsafe_cast(lhs_type, rhs, location); 194 } 195 else 196 return rhs; 197} 198 199// Return an expression for a conversion from a non-interface type to an 200// interface type. 201 202Expression* 203Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, 204 Location location) 205{ 206 Interface_type* lhs_interface_type = lhs_type->interface_type(); 207 bool lhs_is_empty = lhs_interface_type->is_empty(); 208 209 // Since RHS_TYPE is a static type, we can create the interface 210 // method table at compile time. 211 212 // When setting an interface to nil, we just set both fields to 213 // NULL. 214 Type* rhs_type = rhs->type(); 215 if (rhs_type->is_nil_type()) 216 { 217 Expression* nil = Expression::make_nil(location); 218 return Expression::make_interface_value(lhs_type, nil, nil, location); 219 } 220 221 // This should have been checked already. 222 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); 223 224 // An interface is a tuple. If LHS_TYPE is an empty interface type, 225 // then the first field is the type descriptor for RHS_TYPE. 226 // Otherwise it is the interface method table for RHS_TYPE. 227 Expression* first_field; 228 if (lhs_is_empty) 229 first_field = Expression::make_type_descriptor(rhs_type, location); 230 else 231 { 232 // Build the interface method table for this interface and this 233 // object type: a list of function pointers for each interface 234 // method. 235 Named_type* rhs_named_type = rhs_type->named_type(); 236 Struct_type* rhs_struct_type = rhs_type->struct_type(); 237 bool is_pointer = false; 238 if (rhs_named_type == NULL && rhs_struct_type == NULL) 239 { 240 rhs_named_type = rhs_type->deref()->named_type(); 241 rhs_struct_type = rhs_type->deref()->struct_type(); 242 is_pointer = true; 243 } 244 if (rhs_named_type != NULL) 245 first_field = 246 rhs_named_type->interface_method_table(lhs_interface_type, 247 is_pointer); 248 else if (rhs_struct_type != NULL) 249 first_field = 250 rhs_struct_type->interface_method_table(lhs_interface_type, 251 is_pointer); 252 else 253 first_field = Expression::make_nil(location); 254 } 255 256 Expression* obj; 257 if (rhs_type->points_to() != NULL) 258 { 259 // We are assigning a pointer to the interface; the interface 260 // holds the pointer itself. 261 obj = rhs; 262 } 263 else 264 { 265 // We are assigning a non-pointer value to the interface; the 266 // interface gets a copy of the value in the heap. 267 obj = Expression::make_heap_expression(rhs, location); 268 } 269 270 return Expression::make_interface_value(lhs_type, first_field, obj, location); 271} 272 273// Return an expression for the type descriptor of RHS. 274 275Expression* 276Expression::get_interface_type_descriptor(Expression* rhs) 277{ 278 go_assert(rhs->type()->interface_type() != NULL); 279 Location location = rhs->location(); 280 281 // The type descriptor is the first field of an empty interface. 282 if (rhs->type()->interface_type()->is_empty()) 283 return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, 284 location); 285 286 Expression* mtable = 287 Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); 288 289 Expression* descriptor = 290 Expression::make_unary(OPERATOR_MULT, mtable, location); 291 descriptor = Expression::make_field_reference(descriptor, 0, location); 292 Expression* nil = Expression::make_nil(location); 293 294 Expression* eq = 295 Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); 296 return Expression::make_conditional(eq, nil, descriptor, location); 297} 298 299// Return an expression for the conversion of an interface type to an 300// interface type. 301 302Expression* 303Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, 304 bool for_type_guard, 305 Location location) 306{ 307 if (Type::are_identical(lhs_type, rhs->type(), false, NULL)) 308 return rhs; 309 310 Interface_type* lhs_interface_type = lhs_type->interface_type(); 311 bool lhs_is_empty = lhs_interface_type->is_empty(); 312 313 // In the general case this requires runtime examination of the type 314 // method table to match it up with the interface methods. 315 316 // FIXME: If all of the methods in the right hand side interface 317 // also appear in the left hand side interface, then we don't need 318 // to do a runtime check, although we still need to build a new 319 // method table. 320 321 // We are going to evaluate RHS multiple times. 322 go_assert(rhs->is_variable()); 323 324 // Get the type descriptor for the right hand side. This will be 325 // NULL for a nil interface. 326 Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); 327 Expression* lhs_type_expr = 328 Expression::make_type_descriptor(lhs_type, location); 329 330 Expression* first_field; 331 if (for_type_guard) 332 { 333 // A type assertion fails when converting a nil interface. 334 first_field = 335 Runtime::make_call(Runtime::ASSERT_INTERFACE, location, 2, 336 lhs_type_expr, rhs_type_expr); 337 } 338 else if (lhs_is_empty) 339 { 340 // A conversion to an empty interface always succeeds, and the 341 // first field is just the type descriptor of the object. 342 first_field = rhs_type_expr; 343 } 344 else 345 { 346 // A conversion to a non-empty interface may fail, but unlike a 347 // type assertion converting nil will always succeed. 348 first_field = 349 Runtime::make_call(Runtime::CONVERT_INTERFACE, location, 2, 350 lhs_type_expr, rhs_type_expr); 351 } 352 353 // The second field is simply the object pointer. 354 Expression* obj = 355 Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); 356 return Expression::make_interface_value(lhs_type, first_field, obj, location); 357} 358 359// Return an expression for the conversion of an interface type to a 360// non-interface type. 361 362Expression* 363Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, 364 Location location) 365{ 366 // We are going to evaluate RHS multiple times. 367 go_assert(rhs->is_variable()); 368 369 // Call a function to check that the type is valid. The function 370 // will panic with an appropriate runtime type error if the type is 371 // not valid. 372 Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, 373 location); 374 Expression* rhs_descriptor = 375 Expression::get_interface_type_descriptor(rhs); 376 377 Type* rhs_type = rhs->type(); 378 Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, 379 location); 380 381 Expression* check_iface = Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE, 382 location, 3, lhs_type_expr, 383 rhs_descriptor, rhs_inter_expr); 384 385 // If the call succeeds, pull out the value. 386 Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, 387 location); 388 389 // If the value is a pointer, then it is the value we want. 390 // Otherwise it points to the value. 391 if (lhs_type->points_to() == NULL) 392 { 393 obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, 394 location); 395 obj = Expression::make_unary(OPERATOR_MULT, obj, location); 396 } 397 return Expression::make_compound(check_iface, obj, location); 398} 399 400// Convert an expression to its backend representation. This is implemented by 401// the child class. Not that it is not in general safe to call this multiple 402// times for a single expression, but that we don't catch such errors. 403 404Bexpression* 405Expression::get_backend(Translate_context* context) 406{ 407 // The child may have marked this expression as having an error. 408 if (this->classification_ == EXPRESSION_ERROR) 409 return context->backend()->error_expression(); 410 411 return this->do_get_backend(context); 412} 413 414// Return a backend expression for VAL. 415Bexpression* 416Expression::backend_numeric_constant_expression(Translate_context* context, 417 Numeric_constant* val) 418{ 419 Gogo* gogo = context->gogo(); 420 Type* type = val->type(); 421 if (type == NULL) 422 return gogo->backend()->error_expression(); 423 424 Btype* btype = type->get_backend(gogo); 425 Bexpression* ret; 426 if (type->integer_type() != NULL) 427 { 428 mpz_t ival; 429 if (!val->to_int(&ival)) 430 { 431 go_assert(saw_errors()); 432 return gogo->backend()->error_expression(); 433 } 434 ret = gogo->backend()->integer_constant_expression(btype, ival); 435 mpz_clear(ival); 436 } 437 else if (type->float_type() != NULL) 438 { 439 mpfr_t fval; 440 if (!val->to_float(&fval)) 441 { 442 go_assert(saw_errors()); 443 return gogo->backend()->error_expression(); 444 } 445 ret = gogo->backend()->float_constant_expression(btype, fval); 446 mpfr_clear(fval); 447 } 448 else if (type->complex_type() != NULL) 449 { 450 mpc_t cval; 451 if (!val->to_complex(&cval)) 452 { 453 go_assert(saw_errors()); 454 return gogo->backend()->error_expression(); 455 } 456 ret = gogo->backend()->complex_constant_expression(btype, cval); 457 mpc_clear(cval); 458 } 459 else 460 go_unreachable(); 461 462 return ret; 463} 464 465// Return an expression which evaluates to true if VAL, of arbitrary integer 466// type, is negative or is more than the maximum value of the Go type "int". 467 468Expression* 469Expression::check_bounds(Expression* val, Location loc) 470{ 471 Type* val_type = val->type(); 472 Type* bound_type = Type::lookup_integer_type("int"); 473 474 int val_type_size; 475 bool val_is_unsigned = false; 476 if (val_type->integer_type() != NULL) 477 { 478 val_type_size = val_type->integer_type()->bits(); 479 val_is_unsigned = val_type->integer_type()->is_unsigned(); 480 } 481 else 482 { 483 if (!val_type->is_numeric_type() 484 || !Type::are_convertible(bound_type, val_type, NULL)) 485 { 486 go_assert(saw_errors()); 487 return Expression::make_boolean(true, loc); 488 } 489 490 if (val_type->complex_type() != NULL) 491 val_type_size = val_type->complex_type()->bits(); 492 else 493 val_type_size = val_type->float_type()->bits(); 494 } 495 496 Expression* negative_index = Expression::make_boolean(false, loc); 497 Expression* index_overflows = Expression::make_boolean(false, loc); 498 if (!val_is_unsigned) 499 { 500 Expression* zero = Expression::make_integer_ul(0, val_type, loc); 501 negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); 502 } 503 504 int bound_type_size = bound_type->integer_type()->bits(); 505 if (val_type_size > bound_type_size 506 || (val_type_size == bound_type_size 507 && val_is_unsigned)) 508 { 509 mpz_t one; 510 mpz_init_set_ui(one, 1UL); 511 512 // maxval = 2^(bound_type_size - 1) - 1 513 mpz_t maxval; 514 mpz_init(maxval); 515 mpz_mul_2exp(maxval, one, bound_type_size - 1); 516 mpz_sub_ui(maxval, maxval, 1); 517 Expression* max = Expression::make_integer_z(&maxval, val_type, loc); 518 mpz_clear(one); 519 mpz_clear(maxval); 520 521 index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); 522 } 523 524 return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, 525 loc); 526} 527 528void 529Expression::dump_expression(Ast_dump_context* ast_dump_context) const 530{ 531 this->do_dump_expression(ast_dump_context); 532} 533 534// Error expressions. This are used to avoid cascading errors. 535 536class Error_expression : public Expression 537{ 538 public: 539 Error_expression(Location location) 540 : Expression(EXPRESSION_ERROR, location) 541 { } 542 543 protected: 544 bool 545 do_is_constant() const 546 { return true; } 547 548 bool 549 do_is_immutable() const 550 { return true; } 551 552 bool 553 do_numeric_constant_value(Numeric_constant* nc) const 554 { 555 nc->set_unsigned_long(NULL, 0); 556 return true; 557 } 558 559 bool 560 do_discarding_value() 561 { return true; } 562 563 Type* 564 do_type() 565 { return Type::make_error_type(); } 566 567 void 568 do_determine_type(const Type_context*) 569 { } 570 571 Expression* 572 do_copy() 573 { return this; } 574 575 bool 576 do_is_addressable() const 577 { return true; } 578 579 Bexpression* 580 do_get_backend(Translate_context* context) 581 { return context->backend()->error_expression(); } 582 583 void 584 do_dump_expression(Ast_dump_context*) const; 585}; 586 587// Dump the ast representation for an error expression to a dump context. 588 589void 590Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 591{ 592 ast_dump_context->ostream() << "_Error_" ; 593} 594 595Expression* 596Expression::make_error(Location location) 597{ 598 return new Error_expression(location); 599} 600 601// An expression which is really a type. This is used during parsing. 602// It is an error if these survive after lowering. 603 604class 605Type_expression : public Expression 606{ 607 public: 608 Type_expression(Type* type, Location location) 609 : Expression(EXPRESSION_TYPE, location), 610 type_(type) 611 { } 612 613 protected: 614 int 615 do_traverse(Traverse* traverse) 616 { return Type::traverse(this->type_, traverse); } 617 618 Type* 619 do_type() 620 { return this->type_; } 621 622 void 623 do_determine_type(const Type_context*) 624 { } 625 626 void 627 do_check_types(Gogo*) 628 { this->report_error(_("invalid use of type")); } 629 630 Expression* 631 do_copy() 632 { return this; } 633 634 Bexpression* 635 do_get_backend(Translate_context*) 636 { go_unreachable(); } 637 638 void do_dump_expression(Ast_dump_context*) const; 639 640 private: 641 // The type which we are representing as an expression. 642 Type* type_; 643}; 644 645void 646Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 647{ 648 ast_dump_context->dump_type(this->type_); 649} 650 651Expression* 652Expression::make_type(Type* type, Location location) 653{ 654 return new Type_expression(type, location); 655} 656 657// Class Parser_expression. 658 659Type* 660Parser_expression::do_type() 661{ 662 // We should never really ask for the type of a Parser_expression. 663 // However, it can happen, at least when we have an invalid const 664 // whose initializer refers to the const itself. In that case we 665 // may ask for the type when lowering the const itself. 666 go_assert(saw_errors()); 667 return Type::make_error_type(); 668} 669 670// Class Var_expression. 671 672// Lower a variable expression. Here we just make sure that the 673// initialization expression of the variable has been lowered. This 674// ensures that we will be able to determine the type of the variable 675// if necessary. 676 677Expression* 678Var_expression::do_lower(Gogo* gogo, Named_object* function, 679 Statement_inserter* inserter, int) 680{ 681 if (this->variable_->is_variable()) 682 { 683 Variable* var = this->variable_->var_value(); 684 // This is either a local variable or a global variable. A 685 // reference to a variable which is local to an enclosing 686 // function will be a reference to a field in a closure. 687 if (var->is_global()) 688 { 689 function = NULL; 690 inserter = NULL; 691 } 692 var->lower_init_expression(gogo, function, inserter); 693 } 694 return this; 695} 696 697// Return the type of a reference to a variable. 698 699Type* 700Var_expression::do_type() 701{ 702 if (this->variable_->is_variable()) 703 return this->variable_->var_value()->type(); 704 else if (this->variable_->is_result_variable()) 705 return this->variable_->result_var_value()->type(); 706 else 707 go_unreachable(); 708} 709 710// Determine the type of a reference to a variable. 711 712void 713Var_expression::do_determine_type(const Type_context*) 714{ 715 if (this->variable_->is_variable()) 716 this->variable_->var_value()->determine_type(); 717} 718 719// Something takes the address of this variable. This means that we 720// may want to move the variable onto the heap. 721 722void 723Var_expression::do_address_taken(bool escapes) 724{ 725 if (!escapes) 726 { 727 if (this->variable_->is_variable()) 728 this->variable_->var_value()->set_non_escaping_address_taken(); 729 else if (this->variable_->is_result_variable()) 730 this->variable_->result_var_value()->set_non_escaping_address_taken(); 731 else 732 go_unreachable(); 733 } 734 else 735 { 736 if (this->variable_->is_variable()) 737 this->variable_->var_value()->set_address_taken(); 738 else if (this->variable_->is_result_variable()) 739 this->variable_->result_var_value()->set_address_taken(); 740 else 741 go_unreachable(); 742 } 743} 744 745// Get the backend representation for a reference to a variable. 746 747Bexpression* 748Var_expression::do_get_backend(Translate_context* context) 749{ 750 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), 751 context->function()); 752 bool is_in_heap; 753 Location loc = this->location(); 754 Btype* btype; 755 Gogo* gogo = context->gogo(); 756 if (this->variable_->is_variable()) 757 { 758 is_in_heap = this->variable_->var_value()->is_in_heap(); 759 btype = this->variable_->var_value()->type()->get_backend(gogo); 760 } 761 else if (this->variable_->is_result_variable()) 762 { 763 is_in_heap = this->variable_->result_var_value()->is_in_heap(); 764 btype = this->variable_->result_var_value()->type()->get_backend(gogo); 765 } 766 else 767 go_unreachable(); 768 769 Bexpression* ret = context->backend()->var_expression(bvar, loc); 770 if (is_in_heap) 771 ret = context->backend()->indirect_expression(btype, ret, true, loc); 772 return ret; 773} 774 775// Ast dump for variable expression. 776 777void 778Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 779{ 780 ast_dump_context->ostream() << this->variable_->name() ; 781} 782 783// Make a reference to a variable in an expression. 784 785Expression* 786Expression::make_var_reference(Named_object* var, Location location) 787{ 788 if (var->is_sink()) 789 return Expression::make_sink(location); 790 791 // FIXME: Creating a new object for each reference to a variable is 792 // wasteful. 793 return new Var_expression(var, location); 794} 795 796// Class Temporary_reference_expression. 797 798// The type. 799 800Type* 801Temporary_reference_expression::do_type() 802{ 803 return this->statement_->type(); 804} 805 806// Called if something takes the address of this temporary variable. 807// We never have to move temporary variables to the heap, but we do 808// need to know that they must live in the stack rather than in a 809// register. 810 811void 812Temporary_reference_expression::do_address_taken(bool) 813{ 814 this->statement_->set_is_address_taken(); 815} 816 817// Get a backend expression referring to the variable. 818 819Bexpression* 820Temporary_reference_expression::do_get_backend(Translate_context* context) 821{ 822 Gogo* gogo = context->gogo(); 823 Bvariable* bvar = this->statement_->get_backend_variable(context); 824 Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); 825 826 // The backend can't always represent the same set of recursive types 827 // that the Go frontend can. In some cases this means that a 828 // temporary variable won't have the right backend type. Correct 829 // that here by adding a type cast. We need to use base() to push 830 // the circularity down one level. 831 Type* stype = this->statement_->type(); 832 if (!this->is_lvalue_ 833 && stype->has_pointer() 834 && stype->deref()->is_void_type()) 835 { 836 Btype* btype = this->type()->base()->get_backend(gogo); 837 ret = gogo->backend()->convert_expression(btype, ret, this->location()); 838 } 839 return ret; 840} 841 842// Ast dump for temporary reference. 843 844void 845Temporary_reference_expression::do_dump_expression( 846 Ast_dump_context* ast_dump_context) const 847{ 848 ast_dump_context->dump_temp_variable_name(this->statement_); 849} 850 851// Make a reference to a temporary variable. 852 853Temporary_reference_expression* 854Expression::make_temporary_reference(Temporary_statement* statement, 855 Location location) 856{ 857 return new Temporary_reference_expression(statement, location); 858} 859 860// Class Set_and_use_temporary_expression. 861 862// Return the type. 863 864Type* 865Set_and_use_temporary_expression::do_type() 866{ 867 return this->statement_->type(); 868} 869 870// Determine the type of the expression. 871 872void 873Set_and_use_temporary_expression::do_determine_type( 874 const Type_context* context) 875{ 876 this->expr_->determine_type(context); 877} 878 879// Take the address. 880 881void 882Set_and_use_temporary_expression::do_address_taken(bool) 883{ 884 this->statement_->set_is_address_taken(); 885} 886 887// Return the backend representation. 888 889Bexpression* 890Set_and_use_temporary_expression::do_get_backend(Translate_context* context) 891{ 892 Location loc = this->location(); 893 Gogo* gogo = context->gogo(); 894 Bvariable* bvar = this->statement_->get_backend_variable(context); 895 Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); 896 897 Bexpression* bexpr = this->expr_->get_backend(context); 898 Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc); 899 var_ref = gogo->backend()->var_expression(bvar, loc); 900 Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); 901 return ret; 902} 903 904// Dump. 905 906void 907Set_and_use_temporary_expression::do_dump_expression( 908 Ast_dump_context* ast_dump_context) const 909{ 910 ast_dump_context->ostream() << '('; 911 ast_dump_context->dump_temp_variable_name(this->statement_); 912 ast_dump_context->ostream() << " = "; 913 this->expr_->dump_expression(ast_dump_context); 914 ast_dump_context->ostream() << ')'; 915} 916 917// Make a set-and-use temporary. 918 919Set_and_use_temporary_expression* 920Expression::make_set_and_use_temporary(Temporary_statement* statement, 921 Expression* expr, Location location) 922{ 923 return new Set_and_use_temporary_expression(statement, expr, location); 924} 925 926// A sink expression--a use of the blank identifier _. 927 928class Sink_expression : public Expression 929{ 930 public: 931 Sink_expression(Location location) 932 : Expression(EXPRESSION_SINK, location), 933 type_(NULL), bvar_(NULL) 934 { } 935 936 protected: 937 bool 938 do_discarding_value() 939 { return true; } 940 941 Type* 942 do_type(); 943 944 void 945 do_determine_type(const Type_context*); 946 947 Expression* 948 do_copy() 949 { return new Sink_expression(this->location()); } 950 951 Bexpression* 952 do_get_backend(Translate_context*); 953 954 void 955 do_dump_expression(Ast_dump_context*) const; 956 957 private: 958 // The type of this sink variable. 959 Type* type_; 960 // The temporary variable we generate. 961 Bvariable* bvar_; 962}; 963 964// Return the type of a sink expression. 965 966Type* 967Sink_expression::do_type() 968{ 969 if (this->type_ == NULL) 970 return Type::make_sink_type(); 971 return this->type_; 972} 973 974// Determine the type of a sink expression. 975 976void 977Sink_expression::do_determine_type(const Type_context* context) 978{ 979 if (context->type != NULL) 980 this->type_ = context->type; 981} 982 983// Return a temporary variable for a sink expression. This will 984// presumably be a write-only variable which the middle-end will drop. 985 986Bexpression* 987Sink_expression::do_get_backend(Translate_context* context) 988{ 989 Location loc = this->location(); 990 Gogo* gogo = context->gogo(); 991 if (this->bvar_ == NULL) 992 { 993 go_assert(this->type_ != NULL && !this->type_->is_sink_type()); 994 Named_object* fn = context->function(); 995 go_assert(fn != NULL); 996 Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); 997 Btype* bt = this->type_->get_backend(context->gogo()); 998 Bstatement* decl; 999 this->bvar_ = 1000 gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, 1001 false, loc, &decl); 1002 Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc); 1003 var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); 1004 return var_ref; 1005 } 1006 return gogo->backend()->var_expression(this->bvar_, loc); 1007} 1008 1009// Ast dump for sink expression. 1010 1011void 1012Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1013{ 1014 ast_dump_context->ostream() << "_" ; 1015} 1016 1017// Make a sink expression. 1018 1019Expression* 1020Expression::make_sink(Location location) 1021{ 1022 return new Sink_expression(location); 1023} 1024 1025// Class Func_expression. 1026 1027// FIXME: Can a function expression appear in a constant expression? 1028// The value is unchanging. Initializing a constant to the address of 1029// a function seems like it could work, though there might be little 1030// point to it. 1031 1032// Traversal. 1033 1034int 1035Func_expression::do_traverse(Traverse* traverse) 1036{ 1037 return (this->closure_ == NULL 1038 ? TRAVERSE_CONTINUE 1039 : Expression::traverse(&this->closure_, traverse)); 1040} 1041 1042// Return the type of a function expression. 1043 1044Type* 1045Func_expression::do_type() 1046{ 1047 if (this->function_->is_function()) 1048 return this->function_->func_value()->type(); 1049 else if (this->function_->is_function_declaration()) 1050 return this->function_->func_declaration_value()->type(); 1051 else 1052 go_unreachable(); 1053} 1054 1055// Get the backend representation for the code of a function expression. 1056 1057Bexpression* 1058Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) 1059{ 1060 Function_type* fntype; 1061 if (no->is_function()) 1062 fntype = no->func_value()->type(); 1063 else if (no->is_function_declaration()) 1064 fntype = no->func_declaration_value()->type(); 1065 else 1066 go_unreachable(); 1067 1068 // Builtin functions are handled specially by Call_expression. We 1069 // can't take their address. 1070 if (fntype->is_builtin()) 1071 { 1072 error_at(loc, 1073 "invalid use of special builtin function %qs; must be called", 1074 no->message_name().c_str()); 1075 return gogo->backend()->error_expression(); 1076 } 1077 1078 Bfunction* fndecl; 1079 if (no->is_function()) 1080 fndecl = no->func_value()->get_or_make_decl(gogo, no); 1081 else if (no->is_function_declaration()) 1082 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); 1083 else 1084 go_unreachable(); 1085 1086 return gogo->backend()->function_code_expression(fndecl, loc); 1087} 1088 1089// Get the backend representation for a function expression. This is used when 1090// we take the address of a function rather than simply calling it. A func 1091// value is represented as a pointer to a block of memory. The first 1092// word of that memory is a pointer to the function code. The 1093// remaining parts of that memory are the addresses of variables that 1094// the function closes over. 1095 1096Bexpression* 1097Func_expression::do_get_backend(Translate_context* context) 1098{ 1099 // If there is no closure, just use the function descriptor. 1100 if (this->closure_ == NULL) 1101 { 1102 Gogo* gogo = context->gogo(); 1103 Named_object* no = this->function_; 1104 Expression* descriptor; 1105 if (no->is_function()) 1106 descriptor = no->func_value()->descriptor(gogo, no); 1107 else if (no->is_function_declaration()) 1108 { 1109 if (no->func_declaration_value()->type()->is_builtin()) 1110 { 1111 error_at(this->location(), 1112 ("invalid use of special builtin function %qs; " 1113 "must be called"), 1114 no->message_name().c_str()); 1115 return gogo->backend()->error_expression(); 1116 } 1117 descriptor = no->func_declaration_value()->descriptor(gogo, no); 1118 } 1119 else 1120 go_unreachable(); 1121 1122 Bexpression* bdesc = descriptor->get_backend(context); 1123 return gogo->backend()->address_expression(bdesc, this->location()); 1124 } 1125 1126 go_assert(this->function_->func_value()->enclosing() != NULL); 1127 1128 // If there is a closure, then the closure is itself the function 1129 // expression. It is a pointer to a struct whose first field points 1130 // to the function code and whose remaining fields are the addresses 1131 // of the closed-over variables. 1132 return this->closure_->get_backend(context); 1133} 1134 1135// Ast dump for function. 1136 1137void 1138Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1139{ 1140 ast_dump_context->ostream() << this->function_->name(); 1141 if (this->closure_ != NULL) 1142 { 1143 ast_dump_context->ostream() << " {closure = "; 1144 this->closure_->dump_expression(ast_dump_context); 1145 ast_dump_context->ostream() << "}"; 1146 } 1147} 1148 1149// Make a reference to a function in an expression. 1150 1151Expression* 1152Expression::make_func_reference(Named_object* function, Expression* closure, 1153 Location location) 1154{ 1155 return new Func_expression(function, closure, location); 1156} 1157 1158// Class Func_descriptor_expression. 1159 1160// Constructor. 1161 1162Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) 1163 : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), 1164 fn_(fn), dvar_(NULL) 1165{ 1166 go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); 1167} 1168 1169// Traversal. 1170 1171int 1172Func_descriptor_expression::do_traverse(Traverse*) 1173{ 1174 return TRAVERSE_CONTINUE; 1175} 1176 1177// All function descriptors have the same type. 1178 1179Type* Func_descriptor_expression::descriptor_type; 1180 1181void 1182Func_descriptor_expression::make_func_descriptor_type() 1183{ 1184 if (Func_descriptor_expression::descriptor_type != NULL) 1185 return; 1186 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 1187 Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); 1188 Func_descriptor_expression::descriptor_type = 1189 Type::make_builtin_named_type("functionDescriptor", struct_type); 1190} 1191 1192Type* 1193Func_descriptor_expression::do_type() 1194{ 1195 Func_descriptor_expression::make_func_descriptor_type(); 1196 return Func_descriptor_expression::descriptor_type; 1197} 1198 1199// The backend representation for a function descriptor. 1200 1201Bexpression* 1202Func_descriptor_expression::do_get_backend(Translate_context* context) 1203{ 1204 Named_object* no = this->fn_; 1205 Location loc = no->location(); 1206 if (this->dvar_ != NULL) 1207 return context->backend()->var_expression(this->dvar_, loc); 1208 1209 Gogo* gogo = context->gogo(); 1210 std::string var_name; 1211 if (no->package() == NULL) 1212 var_name = gogo->pkgpath_symbol(); 1213 else 1214 var_name = no->package()->pkgpath_symbol(); 1215 var_name.push_back('.'); 1216 var_name.append(Gogo::unpack_hidden_name(no->name())); 1217 var_name.append("$descriptor"); 1218 1219 Btype* btype = this->type()->get_backend(gogo); 1220 1221 Bvariable* bvar; 1222 if (no->package() != NULL 1223 || Linemap::is_predeclared_location(no->location())) 1224 bvar = context->backend()->immutable_struct_reference(var_name, btype, 1225 loc); 1226 else 1227 { 1228 Location bloc = Linemap::predeclared_location(); 1229 bool is_hidden = ((no->is_function() 1230 && no->func_value()->enclosing() != NULL) 1231 || Gogo::is_thunk(no)); 1232 bvar = context->backend()->immutable_struct(var_name, is_hidden, false, 1233 btype, bloc); 1234 Expression_list* vals = new Expression_list(); 1235 vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); 1236 Expression* init = 1237 Expression::make_struct_composite_literal(this->type(), vals, bloc); 1238 Translate_context bcontext(gogo, NULL, NULL, NULL); 1239 bcontext.set_is_const(); 1240 Bexpression* binit = init->get_backend(&bcontext); 1241 context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, 1242 false, btype, bloc, binit); 1243 } 1244 1245 this->dvar_ = bvar; 1246 return gogo->backend()->var_expression(bvar, loc); 1247} 1248 1249// Print a function descriptor expression. 1250 1251void 1252Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const 1253{ 1254 context->ostream() << "[descriptor " << this->fn_->name() << "]"; 1255} 1256 1257// Make a function descriptor expression. 1258 1259Func_descriptor_expression* 1260Expression::make_func_descriptor(Named_object* fn) 1261{ 1262 return new Func_descriptor_expression(fn); 1263} 1264 1265// Make the function descriptor type, so that it can be converted. 1266 1267void 1268Expression::make_func_descriptor_type() 1269{ 1270 Func_descriptor_expression::make_func_descriptor_type(); 1271} 1272 1273// A reference to just the code of a function. 1274 1275class Func_code_reference_expression : public Expression 1276{ 1277 public: 1278 Func_code_reference_expression(Named_object* function, Location location) 1279 : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), 1280 function_(function) 1281 { } 1282 1283 protected: 1284 int 1285 do_traverse(Traverse*) 1286 { return TRAVERSE_CONTINUE; } 1287 1288 bool 1289 do_is_immutable() const 1290 { return true; } 1291 1292 Type* 1293 do_type() 1294 { return Type::make_pointer_type(Type::make_void_type()); } 1295 1296 void 1297 do_determine_type(const Type_context*) 1298 { } 1299 1300 Expression* 1301 do_copy() 1302 { 1303 return Expression::make_func_code_reference(this->function_, 1304 this->location()); 1305 } 1306 1307 Bexpression* 1308 do_get_backend(Translate_context*); 1309 1310 void 1311 do_dump_expression(Ast_dump_context* context) const 1312 { context->ostream() << "[raw " << this->function_->name() << "]" ; } 1313 1314 private: 1315 // The function. 1316 Named_object* function_; 1317}; 1318 1319// Get the backend representation for a reference to function code. 1320 1321Bexpression* 1322Func_code_reference_expression::do_get_backend(Translate_context* context) 1323{ 1324 return Func_expression::get_code_pointer(context->gogo(), this->function_, 1325 this->location()); 1326} 1327 1328// Make a reference to the code of a function. 1329 1330Expression* 1331Expression::make_func_code_reference(Named_object* function, Location location) 1332{ 1333 return new Func_code_reference_expression(function, location); 1334} 1335 1336// Class Unknown_expression. 1337 1338// Return the name of an unknown expression. 1339 1340const std::string& 1341Unknown_expression::name() const 1342{ 1343 return this->named_object_->name(); 1344} 1345 1346// Lower a reference to an unknown name. 1347 1348Expression* 1349Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) 1350{ 1351 Location location = this->location(); 1352 Named_object* no = this->named_object_; 1353 Named_object* real; 1354 if (!no->is_unknown()) 1355 real = no; 1356 else 1357 { 1358 real = no->unknown_value()->real_named_object(); 1359 if (real == NULL) 1360 { 1361 if (this->is_composite_literal_key_) 1362 return this; 1363 if (!this->no_error_message_) 1364 error_at(location, "reference to undefined name %qs", 1365 this->named_object_->message_name().c_str()); 1366 return Expression::make_error(location); 1367 } 1368 } 1369 switch (real->classification()) 1370 { 1371 case Named_object::NAMED_OBJECT_CONST: 1372 return Expression::make_const_reference(real, location); 1373 case Named_object::NAMED_OBJECT_TYPE: 1374 return Expression::make_type(real->type_value(), location); 1375 case Named_object::NAMED_OBJECT_TYPE_DECLARATION: 1376 if (this->is_composite_literal_key_) 1377 return this; 1378 if (!this->no_error_message_) 1379 error_at(location, "reference to undefined type %qs", 1380 real->message_name().c_str()); 1381 return Expression::make_error(location); 1382 case Named_object::NAMED_OBJECT_VAR: 1383 real->var_value()->set_is_used(); 1384 return Expression::make_var_reference(real, location); 1385 case Named_object::NAMED_OBJECT_FUNC: 1386 case Named_object::NAMED_OBJECT_FUNC_DECLARATION: 1387 return Expression::make_func_reference(real, NULL, location); 1388 case Named_object::NAMED_OBJECT_PACKAGE: 1389 if (this->is_composite_literal_key_) 1390 return this; 1391 if (!this->no_error_message_) 1392 error_at(location, "unexpected reference to package"); 1393 return Expression::make_error(location); 1394 default: 1395 go_unreachable(); 1396 } 1397} 1398 1399// Dump the ast representation for an unknown expression to a dump context. 1400 1401void 1402Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1403{ 1404 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() 1405 << ")"; 1406} 1407 1408// Make a reference to an unknown name. 1409 1410Unknown_expression* 1411Expression::make_unknown_reference(Named_object* no, Location location) 1412{ 1413 return new Unknown_expression(no, location); 1414} 1415 1416// A boolean expression. 1417 1418class Boolean_expression : public Expression 1419{ 1420 public: 1421 Boolean_expression(bool val, Location location) 1422 : Expression(EXPRESSION_BOOLEAN, location), 1423 val_(val), type_(NULL) 1424 { } 1425 1426 static Expression* 1427 do_import(Import*); 1428 1429 protected: 1430 bool 1431 do_is_constant() const 1432 { return true; } 1433 1434 bool 1435 do_is_immutable() const 1436 { return true; } 1437 1438 Type* 1439 do_type(); 1440 1441 void 1442 do_determine_type(const Type_context*); 1443 1444 Expression* 1445 do_copy() 1446 { return this; } 1447 1448 Bexpression* 1449 do_get_backend(Translate_context* context) 1450 { return context->backend()->boolean_constant_expression(this->val_); } 1451 1452 void 1453 do_export(Export* exp) const 1454 { exp->write_c_string(this->val_ ? "true" : "false"); } 1455 1456 void 1457 do_dump_expression(Ast_dump_context* ast_dump_context) const 1458 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } 1459 1460 private: 1461 // The constant. 1462 bool val_; 1463 // The type as determined by context. 1464 Type* type_; 1465}; 1466 1467// Get the type. 1468 1469Type* 1470Boolean_expression::do_type() 1471{ 1472 if (this->type_ == NULL) 1473 this->type_ = Type::make_boolean_type(); 1474 return this->type_; 1475} 1476 1477// Set the type from the context. 1478 1479void 1480Boolean_expression::do_determine_type(const Type_context* context) 1481{ 1482 if (this->type_ != NULL && !this->type_->is_abstract()) 1483 ; 1484 else if (context->type != NULL && context->type->is_boolean_type()) 1485 this->type_ = context->type; 1486 else if (!context->may_be_abstract) 1487 this->type_ = Type::lookup_bool_type(); 1488} 1489 1490// Import a boolean constant. 1491 1492Expression* 1493Boolean_expression::do_import(Import* imp) 1494{ 1495 if (imp->peek_char() == 't') 1496 { 1497 imp->require_c_string("true"); 1498 return Expression::make_boolean(true, imp->location()); 1499 } 1500 else 1501 { 1502 imp->require_c_string("false"); 1503 return Expression::make_boolean(false, imp->location()); 1504 } 1505} 1506 1507// Make a boolean expression. 1508 1509Expression* 1510Expression::make_boolean(bool val, Location location) 1511{ 1512 return new Boolean_expression(val, location); 1513} 1514 1515// Class String_expression. 1516 1517// Get the type. 1518 1519Type* 1520String_expression::do_type() 1521{ 1522 if (this->type_ == NULL) 1523 this->type_ = Type::make_string_type(); 1524 return this->type_; 1525} 1526 1527// Set the type from the context. 1528 1529void 1530String_expression::do_determine_type(const Type_context* context) 1531{ 1532 if (this->type_ != NULL && !this->type_->is_abstract()) 1533 ; 1534 else if (context->type != NULL && context->type->is_string_type()) 1535 this->type_ = context->type; 1536 else if (!context->may_be_abstract) 1537 this->type_ = Type::lookup_string_type(); 1538} 1539 1540// Build a string constant. 1541 1542Bexpression* 1543String_expression::do_get_backend(Translate_context* context) 1544{ 1545 Gogo* gogo = context->gogo(); 1546 Btype* btype = Type::make_string_type()->get_backend(gogo); 1547 1548 Location loc = this->location(); 1549 std::vector<Bexpression*> init(2); 1550 Bexpression* str_cst = 1551 gogo->backend()->string_constant_expression(this->val_); 1552 init[0] = gogo->backend()->address_expression(str_cst, loc); 1553 1554 Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); 1555 mpz_t lenval; 1556 mpz_init_set_ui(lenval, this->val_.length()); 1557 init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); 1558 mpz_clear(lenval); 1559 1560 return gogo->backend()->constructor_expression(btype, init, loc); 1561} 1562 1563 // Write string literal to string dump. 1564 1565void 1566String_expression::export_string(String_dump* exp, 1567 const String_expression* str) 1568{ 1569 std::string s; 1570 s.reserve(str->val_.length() * 4 + 2); 1571 s += '"'; 1572 for (std::string::const_iterator p = str->val_.begin(); 1573 p != str->val_.end(); 1574 ++p) 1575 { 1576 if (*p == '\\' || *p == '"') 1577 { 1578 s += '\\'; 1579 s += *p; 1580 } 1581 else if (*p >= 0x20 && *p < 0x7f) 1582 s += *p; 1583 else if (*p == '\n') 1584 s += "\\n"; 1585 else if (*p == '\t') 1586 s += "\\t"; 1587 else 1588 { 1589 s += "\\x"; 1590 unsigned char c = *p; 1591 unsigned int dig = c >> 4; 1592 s += dig < 10 ? '0' + dig : 'A' + dig - 10; 1593 dig = c & 0xf; 1594 s += dig < 10 ? '0' + dig : 'A' + dig - 10; 1595 } 1596 } 1597 s += '"'; 1598 exp->write_string(s); 1599} 1600 1601// Export a string expression. 1602 1603void 1604String_expression::do_export(Export* exp) const 1605{ 1606 String_expression::export_string(exp, this); 1607} 1608 1609// Import a string expression. 1610 1611Expression* 1612String_expression::do_import(Import* imp) 1613{ 1614 imp->require_c_string("\""); 1615 std::string val; 1616 while (true) 1617 { 1618 int c = imp->get_char(); 1619 if (c == '"' || c == -1) 1620 break; 1621 if (c != '\\') 1622 val += static_cast<char>(c); 1623 else 1624 { 1625 c = imp->get_char(); 1626 if (c == '\\' || c == '"') 1627 val += static_cast<char>(c); 1628 else if (c == 'n') 1629 val += '\n'; 1630 else if (c == 't') 1631 val += '\t'; 1632 else if (c == 'x') 1633 { 1634 c = imp->get_char(); 1635 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; 1636 c = imp->get_char(); 1637 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; 1638 char v = (vh << 4) | vl; 1639 val += v; 1640 } 1641 else 1642 { 1643 error_at(imp->location(), "bad string constant"); 1644 return Expression::make_error(imp->location()); 1645 } 1646 } 1647 } 1648 return Expression::make_string(val, imp->location()); 1649} 1650 1651// Ast dump for string expression. 1652 1653void 1654String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1655{ 1656 String_expression::export_string(ast_dump_context, this); 1657} 1658 1659// Make a string expression. 1660 1661Expression* 1662Expression::make_string(const std::string& val, Location location) 1663{ 1664 return new String_expression(val, location); 1665} 1666 1667// An expression that evaluates to some characteristic of a string. 1668// This is used when indexing, bound-checking, or nil checking a string. 1669 1670class String_info_expression : public Expression 1671{ 1672 public: 1673 String_info_expression(Expression* string, String_info string_info, 1674 Location location) 1675 : Expression(EXPRESSION_STRING_INFO, location), 1676 string_(string), string_info_(string_info) 1677 { } 1678 1679 protected: 1680 Type* 1681 do_type(); 1682 1683 void 1684 do_determine_type(const Type_context*) 1685 { go_unreachable(); } 1686 1687 Expression* 1688 do_copy() 1689 { 1690 return new String_info_expression(this->string_->copy(), this->string_info_, 1691 this->location()); 1692 } 1693 1694 Bexpression* 1695 do_get_backend(Translate_context* context); 1696 1697 void 1698 do_dump_expression(Ast_dump_context*) const; 1699 1700 void 1701 do_issue_nil_check() 1702 { this->string_->issue_nil_check(); } 1703 1704 private: 1705 // The string for which we are getting information. 1706 Expression* string_; 1707 // What information we want. 1708 String_info string_info_; 1709}; 1710 1711// Return the type of the string info. 1712 1713Type* 1714String_info_expression::do_type() 1715{ 1716 switch (this->string_info_) 1717 { 1718 case STRING_INFO_DATA: 1719 { 1720 Type* byte_type = Type::lookup_integer_type("uint8"); 1721 return Type::make_pointer_type(byte_type); 1722 } 1723 case STRING_INFO_LENGTH: 1724 return Type::lookup_integer_type("int"); 1725 default: 1726 go_unreachable(); 1727 } 1728} 1729 1730// Return string information in GENERIC. 1731 1732Bexpression* 1733String_info_expression::do_get_backend(Translate_context* context) 1734{ 1735 Gogo* gogo = context->gogo(); 1736 1737 Bexpression* bstring = this->string_->get_backend(context); 1738 switch (this->string_info_) 1739 { 1740 case STRING_INFO_DATA: 1741 case STRING_INFO_LENGTH: 1742 return gogo->backend()->struct_field_expression(bstring, 1743 this->string_info_, 1744 this->location()); 1745 break; 1746 default: 1747 go_unreachable(); 1748 } 1749} 1750 1751// Dump ast representation for a type info expression. 1752 1753void 1754String_info_expression::do_dump_expression( 1755 Ast_dump_context* ast_dump_context) const 1756{ 1757 ast_dump_context->ostream() << "stringinfo("; 1758 this->string_->dump_expression(ast_dump_context); 1759 ast_dump_context->ostream() << ","; 1760 ast_dump_context->ostream() << 1761 (this->string_info_ == STRING_INFO_DATA ? "data" 1762 : this->string_info_ == STRING_INFO_LENGTH ? "length" 1763 : "unknown"); 1764 ast_dump_context->ostream() << ")"; 1765} 1766 1767// Make a string info expression. 1768 1769Expression* 1770Expression::make_string_info(Expression* string, String_info string_info, 1771 Location location) 1772{ 1773 return new String_info_expression(string, string_info, location); 1774} 1775 1776// Make an integer expression. 1777 1778class Integer_expression : public Expression 1779{ 1780 public: 1781 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, 1782 Location location) 1783 : Expression(EXPRESSION_INTEGER, location), 1784 type_(type), is_character_constant_(is_character_constant) 1785 { mpz_init_set(this->val_, *val); } 1786 1787 static Expression* 1788 do_import(Import*); 1789 1790 // Write VAL to string dump. 1791 static void 1792 export_integer(String_dump* exp, const mpz_t val); 1793 1794 // Write VAL to dump context. 1795 static void 1796 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); 1797 1798 protected: 1799 bool 1800 do_is_constant() const 1801 { return true; } 1802 1803 bool 1804 do_is_immutable() const 1805 { return true; } 1806 1807 bool 1808 do_numeric_constant_value(Numeric_constant* nc) const; 1809 1810 Type* 1811 do_type(); 1812 1813 void 1814 do_determine_type(const Type_context* context); 1815 1816 void 1817 do_check_types(Gogo*); 1818 1819 Bexpression* 1820 do_get_backend(Translate_context*); 1821 1822 Expression* 1823 do_copy() 1824 { 1825 if (this->is_character_constant_) 1826 return Expression::make_character(&this->val_, this->type_, 1827 this->location()); 1828 else 1829 return Expression::make_integer_z(&this->val_, this->type_, 1830 this->location()); 1831 } 1832 1833 void 1834 do_export(Export*) const; 1835 1836 void 1837 do_dump_expression(Ast_dump_context*) const; 1838 1839 private: 1840 // The integer value. 1841 mpz_t val_; 1842 // The type so far. 1843 Type* type_; 1844 // Whether this is a character constant. 1845 bool is_character_constant_; 1846}; 1847 1848// Return a numeric constant for this expression. We have to mark 1849// this as a character when appropriate. 1850 1851bool 1852Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const 1853{ 1854 if (this->is_character_constant_) 1855 nc->set_rune(this->type_, this->val_); 1856 else 1857 nc->set_int(this->type_, this->val_); 1858 return true; 1859} 1860 1861// Return the current type. If we haven't set the type yet, we return 1862// an abstract integer type. 1863 1864Type* 1865Integer_expression::do_type() 1866{ 1867 if (this->type_ == NULL) 1868 { 1869 if (this->is_character_constant_) 1870 this->type_ = Type::make_abstract_character_type(); 1871 else 1872 this->type_ = Type::make_abstract_integer_type(); 1873 } 1874 return this->type_; 1875} 1876 1877// Set the type of the integer value. Here we may switch from an 1878// abstract type to a real type. 1879 1880void 1881Integer_expression::do_determine_type(const Type_context* context) 1882{ 1883 if (this->type_ != NULL && !this->type_->is_abstract()) 1884 ; 1885 else if (context->type != NULL && context->type->is_numeric_type()) 1886 this->type_ = context->type; 1887 else if (!context->may_be_abstract) 1888 { 1889 if (this->is_character_constant_) 1890 this->type_ = Type::lookup_integer_type("int32"); 1891 else 1892 this->type_ = Type::lookup_integer_type("int"); 1893 } 1894} 1895 1896// Check the type of an integer constant. 1897 1898void 1899Integer_expression::do_check_types(Gogo*) 1900{ 1901 Type* type = this->type_; 1902 if (type == NULL) 1903 return; 1904 Numeric_constant nc; 1905 if (this->is_character_constant_) 1906 nc.set_rune(NULL, this->val_); 1907 else 1908 nc.set_int(NULL, this->val_); 1909 if (!nc.set_type(type, true, this->location())) 1910 this->set_is_error(); 1911} 1912 1913// Get the backend representation for an integer constant. 1914 1915Bexpression* 1916Integer_expression::do_get_backend(Translate_context* context) 1917{ 1918 Type* resolved_type = NULL; 1919 if (this->type_ != NULL && !this->type_->is_abstract()) 1920 resolved_type = this->type_; 1921 else if (this->type_ != NULL && this->type_->float_type() != NULL) 1922 { 1923 // We are converting to an abstract floating point type. 1924 resolved_type = Type::lookup_float_type("float64"); 1925 } 1926 else if (this->type_ != NULL && this->type_->complex_type() != NULL) 1927 { 1928 // We are converting to an abstract complex type. 1929 resolved_type = Type::lookup_complex_type("complex128"); 1930 } 1931 else 1932 { 1933 // If we still have an abstract type here, then this is being 1934 // used in a constant expression which didn't get reduced for 1935 // some reason. Use a type which will fit the value. We use <, 1936 // not <=, because we need an extra bit for the sign bit. 1937 int bits = mpz_sizeinbase(this->val_, 2); 1938 Type* int_type = Type::lookup_integer_type("int"); 1939 if (bits < int_type->integer_type()->bits()) 1940 resolved_type = int_type; 1941 else if (bits < 64) 1942 resolved_type = Type::lookup_integer_type("int64"); 1943 else 1944 { 1945 if (!saw_errors()) 1946 error_at(this->location(), 1947 "unknown type for large integer constant"); 1948 return context->gogo()->backend()->error_expression(); 1949 } 1950 } 1951 Numeric_constant nc; 1952 nc.set_int(resolved_type, this->val_); 1953 return Expression::backend_numeric_constant_expression(context, &nc); 1954} 1955 1956// Write VAL to export data. 1957 1958void 1959Integer_expression::export_integer(String_dump* exp, const mpz_t val) 1960{ 1961 char* s = mpz_get_str(NULL, 10, val); 1962 exp->write_c_string(s); 1963 free(s); 1964} 1965 1966// Export an integer in a constant expression. 1967 1968void 1969Integer_expression::do_export(Export* exp) const 1970{ 1971 Integer_expression::export_integer(exp, this->val_); 1972 if (this->is_character_constant_) 1973 exp->write_c_string("'"); 1974 // A trailing space lets us reliably identify the end of the number. 1975 exp->write_c_string(" "); 1976} 1977 1978// Import an integer, floating point, or complex value. This handles 1979// all these types because they all start with digits. 1980 1981Expression* 1982Integer_expression::do_import(Import* imp) 1983{ 1984 std::string num = imp->read_identifier(); 1985 imp->require_c_string(" "); 1986 if (!num.empty() && num[num.length() - 1] == 'i') 1987 { 1988 mpfr_t real; 1989 size_t plus_pos = num.find('+', 1); 1990 size_t minus_pos = num.find('-', 1); 1991 size_t pos; 1992 if (plus_pos == std::string::npos) 1993 pos = minus_pos; 1994 else if (minus_pos == std::string::npos) 1995 pos = plus_pos; 1996 else 1997 { 1998 error_at(imp->location(), "bad number in import data: %qs", 1999 num.c_str()); 2000 return Expression::make_error(imp->location()); 2001 } 2002 if (pos == std::string::npos) 2003 mpfr_set_ui(real, 0, GMP_RNDN); 2004 else 2005 { 2006 std::string real_str = num.substr(0, pos); 2007 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) 2008 { 2009 error_at(imp->location(), "bad number in import data: %qs", 2010 real_str.c_str()); 2011 return Expression::make_error(imp->location()); 2012 } 2013 } 2014 2015 std::string imag_str; 2016 if (pos == std::string::npos) 2017 imag_str = num; 2018 else 2019 imag_str = num.substr(pos); 2020 imag_str = imag_str.substr(0, imag_str.size() - 1); 2021 mpfr_t imag; 2022 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) 2023 { 2024 error_at(imp->location(), "bad number in import data: %qs", 2025 imag_str.c_str()); 2026 return Expression::make_error(imp->location()); 2027 } 2028 mpc_t cval; 2029 mpc_init2(cval, mpc_precision); 2030 mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); 2031 mpfr_clear(real); 2032 mpfr_clear(imag); 2033 Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); 2034 mpc_clear(cval); 2035 return ret; 2036 } 2037 else if (num.find('.') == std::string::npos 2038 && num.find('E') == std::string::npos) 2039 { 2040 bool is_character_constant = (!num.empty() 2041 && num[num.length() - 1] == '\''); 2042 if (is_character_constant) 2043 num = num.substr(0, num.length() - 1); 2044 mpz_t val; 2045 if (mpz_init_set_str(val, num.c_str(), 10) != 0) 2046 { 2047 error_at(imp->location(), "bad number in import data: %qs", 2048 num.c_str()); 2049 return Expression::make_error(imp->location()); 2050 } 2051 Expression* ret; 2052 if (is_character_constant) 2053 ret = Expression::make_character(&val, NULL, imp->location()); 2054 else 2055 ret = Expression::make_integer_z(&val, NULL, imp->location()); 2056 mpz_clear(val); 2057 return ret; 2058 } 2059 else 2060 { 2061 mpfr_t val; 2062 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) 2063 { 2064 error_at(imp->location(), "bad number in import data: %qs", 2065 num.c_str()); 2066 return Expression::make_error(imp->location()); 2067 } 2068 Expression* ret = Expression::make_float(&val, NULL, imp->location()); 2069 mpfr_clear(val); 2070 return ret; 2071 } 2072} 2073// Ast dump for integer expression. 2074 2075void 2076Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 2077{ 2078 if (this->is_character_constant_) 2079 ast_dump_context->ostream() << '\''; 2080 Integer_expression::export_integer(ast_dump_context, this->val_); 2081 if (this->is_character_constant_) 2082 ast_dump_context->ostream() << '\''; 2083} 2084 2085// Build a new integer value from a multi-precision integer. 2086 2087Expression* 2088Expression::make_integer_z(const mpz_t* val, Type* type, Location location) 2089{ 2090 return new Integer_expression(val, type, false, location); 2091} 2092 2093// Build a new integer value from an unsigned long. 2094 2095Expression* 2096Expression::make_integer_ul(unsigned long val, Type *type, Location location) 2097{ 2098 mpz_t zval; 2099 mpz_init_set_ui(zval, val); 2100 Expression* ret = Expression::make_integer_z(&zval, type, location); 2101 mpz_clear(zval); 2102 return ret; 2103} 2104 2105// Build a new integer value from a signed long. 2106 2107Expression* 2108Expression::make_integer_sl(long val, Type *type, Location location) 2109{ 2110 mpz_t zval; 2111 mpz_init_set_si(zval, val); 2112 Expression* ret = Expression::make_integer_z(&zval, type, location); 2113 mpz_clear(zval); 2114 return ret; 2115} 2116 2117// Store an int64_t in an uninitialized mpz_t. 2118 2119static void 2120set_mpz_from_int64(mpz_t* zval, int64_t val) 2121{ 2122 if (val >= 0) 2123 { 2124 unsigned long ul = static_cast<unsigned long>(val); 2125 if (static_cast<int64_t>(ul) == val) 2126 { 2127 mpz_init_set_ui(*zval, ul); 2128 return; 2129 } 2130 } 2131 uint64_t uv; 2132 if (val >= 0) 2133 uv = static_cast<uint64_t>(val); 2134 else 2135 uv = static_cast<uint64_t>(- val); 2136 unsigned long ul = uv & 0xffffffffUL; 2137 mpz_init_set_ui(*zval, ul); 2138 mpz_t hval; 2139 mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); 2140 mpz_mul_2exp(hval, hval, 32); 2141 mpz_add(*zval, *zval, hval); 2142 mpz_clear(hval); 2143 if (val < 0) 2144 mpz_neg(*zval, *zval); 2145} 2146 2147// Build a new integer value from an int64_t. 2148 2149Expression* 2150Expression::make_integer_int64(int64_t val, Type* type, Location location) 2151{ 2152 mpz_t zval; 2153 set_mpz_from_int64(&zval, val); 2154 Expression* ret = Expression::make_integer_z(&zval, type, location); 2155 mpz_clear(zval); 2156 return ret; 2157} 2158 2159// Build a new character constant value. 2160 2161Expression* 2162Expression::make_character(const mpz_t* val, Type* type, Location location) 2163{ 2164 return new Integer_expression(val, type, true, location); 2165} 2166 2167// Floats. 2168 2169class Float_expression : public Expression 2170{ 2171 public: 2172 Float_expression(const mpfr_t* val, Type* type, Location location) 2173 : Expression(EXPRESSION_FLOAT, location), 2174 type_(type) 2175 { 2176 mpfr_init_set(this->val_, *val, GMP_RNDN); 2177 } 2178 2179 // Write VAL to export data. 2180 static void 2181 export_float(String_dump* exp, const mpfr_t val); 2182 2183 // Write VAL to dump file. 2184 static void 2185 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); 2186 2187 protected: 2188 bool 2189 do_is_constant() const 2190 { return true; } 2191 2192 bool 2193 do_is_immutable() const 2194 { return true; } 2195 2196 bool 2197 do_numeric_constant_value(Numeric_constant* nc) const 2198 { 2199 nc->set_float(this->type_, this->val_); 2200 return true; 2201 } 2202 2203 Type* 2204 do_type(); 2205 2206 void 2207 do_determine_type(const Type_context*); 2208 2209 void 2210 do_check_types(Gogo*); 2211 2212 Expression* 2213 do_copy() 2214 { return Expression::make_float(&this->val_, this->type_, 2215 this->location()); } 2216 2217 Bexpression* 2218 do_get_backend(Translate_context*); 2219 2220 void 2221 do_export(Export*) const; 2222 2223 void 2224 do_dump_expression(Ast_dump_context*) const; 2225 2226 private: 2227 // The floating point value. 2228 mpfr_t val_; 2229 // The type so far. 2230 Type* type_; 2231}; 2232 2233// Return the current type. If we haven't set the type yet, we return 2234// an abstract float type. 2235 2236Type* 2237Float_expression::do_type() 2238{ 2239 if (this->type_ == NULL) 2240 this->type_ = Type::make_abstract_float_type(); 2241 return this->type_; 2242} 2243 2244// Set the type of the float value. Here we may switch from an 2245// abstract type to a real type. 2246 2247void 2248Float_expression::do_determine_type(const Type_context* context) 2249{ 2250 if (this->type_ != NULL && !this->type_->is_abstract()) 2251 ; 2252 else if (context->type != NULL 2253 && (context->type->integer_type() != NULL 2254 || context->type->float_type() != NULL 2255 || context->type->complex_type() != NULL)) 2256 this->type_ = context->type; 2257 else if (!context->may_be_abstract) 2258 this->type_ = Type::lookup_float_type("float64"); 2259} 2260 2261// Check the type of a float value. 2262 2263void 2264Float_expression::do_check_types(Gogo*) 2265{ 2266 Type* type = this->type_; 2267 if (type == NULL) 2268 return; 2269 Numeric_constant nc; 2270 nc.set_float(NULL, this->val_); 2271 if (!nc.set_type(this->type_, true, this->location())) 2272 this->set_is_error(); 2273} 2274 2275// Get the backend representation for a float constant. 2276 2277Bexpression* 2278Float_expression::do_get_backend(Translate_context* context) 2279{ 2280 Type* resolved_type; 2281 if (this->type_ != NULL && !this->type_->is_abstract()) 2282 resolved_type = this->type_; 2283 else if (this->type_ != NULL && this->type_->integer_type() != NULL) 2284 { 2285 // We have an abstract integer type. We just hope for the best. 2286 resolved_type = Type::lookup_integer_type("int"); 2287 } 2288 else if (this->type_ != NULL && this->type_->complex_type() != NULL) 2289 { 2290 // We are converting to an abstract complex type. 2291 resolved_type = Type::lookup_complex_type("complex128"); 2292 } 2293 else 2294 { 2295 // If we still have an abstract type here, then this is being 2296 // used in a constant expression which didn't get reduced. We 2297 // just use float64 and hope for the best. 2298 resolved_type = Type::lookup_float_type("float64"); 2299 } 2300 2301 Numeric_constant nc; 2302 nc.set_float(resolved_type, this->val_); 2303 return Expression::backend_numeric_constant_expression(context, &nc); 2304} 2305 2306// Write a floating point number to a string dump. 2307 2308void 2309Float_expression::export_float(String_dump *exp, const mpfr_t val) 2310{ 2311 mp_exp_t exponent; 2312 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); 2313 if (*s == '-') 2314 exp->write_c_string("-"); 2315 exp->write_c_string("0."); 2316 exp->write_c_string(*s == '-' ? s + 1 : s); 2317 mpfr_free_str(s); 2318 char buf[30]; 2319 snprintf(buf, sizeof buf, "E%ld", exponent); 2320 exp->write_c_string(buf); 2321} 2322 2323// Export a floating point number in a constant expression. 2324 2325void 2326Float_expression::do_export(Export* exp) const 2327{ 2328 Float_expression::export_float(exp, this->val_); 2329 // A trailing space lets us reliably identify the end of the number. 2330 exp->write_c_string(" "); 2331} 2332 2333// Dump a floating point number to the dump file. 2334 2335void 2336Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 2337{ 2338 Float_expression::export_float(ast_dump_context, this->val_); 2339} 2340 2341// Make a float expression. 2342 2343Expression* 2344Expression::make_float(const mpfr_t* val, Type* type, Location location) 2345{ 2346 return new Float_expression(val, type, location); 2347} 2348 2349// Complex numbers. 2350 2351class Complex_expression : public Expression 2352{ 2353 public: 2354 Complex_expression(const mpc_t* val, Type* type, Location location) 2355 : Expression(EXPRESSION_COMPLEX, location), 2356 type_(type) 2357 { 2358 mpc_init2(this->val_, mpc_precision); 2359 mpc_set(this->val_, *val, MPC_RNDNN); 2360 } 2361 2362 // Write VAL to string dump. 2363 static void 2364 export_complex(String_dump* exp, const mpc_t val); 2365 2366 // Write REAL/IMAG to dump context. 2367 static void 2368 dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); 2369 2370 protected: 2371 bool 2372 do_is_constant() const 2373 { return true; } 2374 2375 bool 2376 do_is_immutable() const 2377 { return true; } 2378 2379 bool 2380 do_numeric_constant_value(Numeric_constant* nc) const 2381 { 2382 nc->set_complex(this->type_, this->val_); 2383 return true; 2384 } 2385 2386 Type* 2387 do_type(); 2388 2389 void 2390 do_determine_type(const Type_context*); 2391 2392 void 2393 do_check_types(Gogo*); 2394 2395 Expression* 2396 do_copy() 2397 { 2398 return Expression::make_complex(&this->val_, this->type_, 2399 this->location()); 2400 } 2401 2402 Bexpression* 2403 do_get_backend(Translate_context*); 2404 2405 void 2406 do_export(Export*) const; 2407 2408 void 2409 do_dump_expression(Ast_dump_context*) const; 2410 2411 private: 2412 // The complex value. 2413 mpc_t val_; 2414 // The type if known. 2415 Type* type_; 2416}; 2417 2418// Return the current type. If we haven't set the type yet, we return 2419// an abstract complex type. 2420 2421Type* 2422Complex_expression::do_type() 2423{ 2424 if (this->type_ == NULL) 2425 this->type_ = Type::make_abstract_complex_type(); 2426 return this->type_; 2427} 2428 2429// Set the type of the complex value. Here we may switch from an 2430// abstract type to a real type. 2431 2432void 2433Complex_expression::do_determine_type(const Type_context* context) 2434{ 2435 if (this->type_ != NULL && !this->type_->is_abstract()) 2436 ; 2437 else if (context->type != NULL 2438 && context->type->complex_type() != NULL) 2439 this->type_ = context->type; 2440 else if (!context->may_be_abstract) 2441 this->type_ = Type::lookup_complex_type("complex128"); 2442} 2443 2444// Check the type of a complex value. 2445 2446void 2447Complex_expression::do_check_types(Gogo*) 2448{ 2449 Type* type = this->type_; 2450 if (type == NULL) 2451 return; 2452 Numeric_constant nc; 2453 nc.set_complex(NULL, this->val_); 2454 if (!nc.set_type(this->type_, true, this->location())) 2455 this->set_is_error(); 2456} 2457 2458// Get the backend representation for a complex constant. 2459 2460Bexpression* 2461Complex_expression::do_get_backend(Translate_context* context) 2462{ 2463 Type* resolved_type; 2464 if (this->type_ != NULL && !this->type_->is_abstract()) 2465 resolved_type = this->type_; 2466 else if (this->type_ != NULL && this->type_->integer_type() != NULL) 2467 { 2468 // We are converting to an abstract integer type. 2469 resolved_type = Type::lookup_integer_type("int"); 2470 } 2471 else if (this->type_ != NULL && this->type_->float_type() != NULL) 2472 { 2473 // We are converting to an abstract float type. 2474 resolved_type = Type::lookup_float_type("float64"); 2475 } 2476 else 2477 { 2478 // If we still have an abstract type here, this this is being 2479 // used in a constant expression which didn't get reduced. We 2480 // just use complex128 and hope for the best. 2481 resolved_type = Type::lookup_complex_type("complex128"); 2482 } 2483 2484 Numeric_constant nc; 2485 nc.set_complex(resolved_type, this->val_); 2486 return Expression::backend_numeric_constant_expression(context, &nc); 2487} 2488 2489// Write REAL/IMAG to export data. 2490 2491void 2492Complex_expression::export_complex(String_dump* exp, const mpc_t val) 2493{ 2494 if (!mpfr_zero_p(mpc_realref(val))) 2495 { 2496 Float_expression::export_float(exp, mpc_realref(val)); 2497 if (mpfr_sgn(mpc_imagref(val)) >= 0) 2498 exp->write_c_string("+"); 2499 } 2500 Float_expression::export_float(exp, mpc_imagref(val)); 2501 exp->write_c_string("i"); 2502} 2503 2504// Export a complex number in a constant expression. 2505 2506void 2507Complex_expression::do_export(Export* exp) const 2508{ 2509 Complex_expression::export_complex(exp, this->val_); 2510 // A trailing space lets us reliably identify the end of the number. 2511 exp->write_c_string(" "); 2512} 2513 2514// Dump a complex expression to the dump file. 2515 2516void 2517Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 2518{ 2519 Complex_expression::export_complex(ast_dump_context, this->val_); 2520} 2521 2522// Make a complex expression. 2523 2524Expression* 2525Expression::make_complex(const mpc_t* val, Type* type, Location location) 2526{ 2527 return new Complex_expression(val, type, location); 2528} 2529 2530// Find a named object in an expression. 2531 2532class Find_named_object : public Traverse 2533{ 2534 public: 2535 Find_named_object(Named_object* no) 2536 : Traverse(traverse_expressions), 2537 no_(no), found_(false) 2538 { } 2539 2540 // Whether we found the object. 2541 bool 2542 found() const 2543 { return this->found_; } 2544 2545 protected: 2546 int 2547 expression(Expression**); 2548 2549 private: 2550 // The object we are looking for. 2551 Named_object* no_; 2552 // Whether we found it. 2553 bool found_; 2554}; 2555 2556// A reference to a const in an expression. 2557 2558class Const_expression : public Expression 2559{ 2560 public: 2561 Const_expression(Named_object* constant, Location location) 2562 : Expression(EXPRESSION_CONST_REFERENCE, location), 2563 constant_(constant), type_(NULL), seen_(false) 2564 { } 2565 2566 Named_object* 2567 named_object() 2568 { return this->constant_; } 2569 2570 // Check that the initializer does not refer to the constant itself. 2571 void 2572 check_for_init_loop(); 2573 2574 protected: 2575 int 2576 do_traverse(Traverse*); 2577 2578 Expression* 2579 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 2580 2581 bool 2582 do_is_constant() const 2583 { return true; } 2584 2585 bool 2586 do_is_immutable() const 2587 { return true; } 2588 2589 bool 2590 do_numeric_constant_value(Numeric_constant* nc) const; 2591 2592 bool 2593 do_string_constant_value(std::string* val) const; 2594 2595 Type* 2596 do_type(); 2597 2598 // The type of a const is set by the declaration, not the use. 2599 void 2600 do_determine_type(const Type_context*); 2601 2602 void 2603 do_check_types(Gogo*); 2604 2605 Expression* 2606 do_copy() 2607 { return this; } 2608 2609 Bexpression* 2610 do_get_backend(Translate_context* context); 2611 2612 // When exporting a reference to a const as part of a const 2613 // expression, we export the value. We ignore the fact that it has 2614 // a name. 2615 void 2616 do_export(Export* exp) const 2617 { this->constant_->const_value()->expr()->export_expression(exp); } 2618 2619 void 2620 do_dump_expression(Ast_dump_context*) const; 2621 2622 private: 2623 // The constant. 2624 Named_object* constant_; 2625 // The type of this reference. This is used if the constant has an 2626 // abstract type. 2627 Type* type_; 2628 // Used to prevent infinite recursion when a constant incorrectly 2629 // refers to itself. 2630 mutable bool seen_; 2631}; 2632 2633// Traversal. 2634 2635int 2636Const_expression::do_traverse(Traverse* traverse) 2637{ 2638 if (this->type_ != NULL) 2639 return Type::traverse(this->type_, traverse); 2640 return TRAVERSE_CONTINUE; 2641} 2642 2643// Lower a constant expression. This is where we convert the 2644// predeclared constant iota into an integer value. 2645 2646Expression* 2647Const_expression::do_lower(Gogo* gogo, Named_object*, 2648 Statement_inserter*, int iota_value) 2649{ 2650 if (this->constant_->const_value()->expr()->classification() 2651 == EXPRESSION_IOTA) 2652 { 2653 if (iota_value == -1) 2654 { 2655 error_at(this->location(), 2656 "iota is only defined in const declarations"); 2657 iota_value = 0; 2658 } 2659 return Expression::make_integer_ul(iota_value, NULL, this->location()); 2660 } 2661 2662 // Make sure that the constant itself has been lowered. 2663 gogo->lower_constant(this->constant_); 2664 2665 return this; 2666} 2667 2668// Return a numeric constant value. 2669 2670bool 2671Const_expression::do_numeric_constant_value(Numeric_constant* nc) const 2672{ 2673 if (this->seen_) 2674 return false; 2675 2676 Expression* e = this->constant_->const_value()->expr(); 2677 2678 this->seen_ = true; 2679 2680 bool r = e->numeric_constant_value(nc); 2681 2682 this->seen_ = false; 2683 2684 Type* ctype; 2685 if (this->type_ != NULL) 2686 ctype = this->type_; 2687 else 2688 ctype = this->constant_->const_value()->type(); 2689 if (r && ctype != NULL) 2690 { 2691 if (!nc->set_type(ctype, false, this->location())) 2692 return false; 2693 } 2694 2695 return r; 2696} 2697 2698bool 2699Const_expression::do_string_constant_value(std::string* val) const 2700{ 2701 if (this->seen_) 2702 return false; 2703 2704 Expression* e = this->constant_->const_value()->expr(); 2705 2706 this->seen_ = true; 2707 bool ok = e->string_constant_value(val); 2708 this->seen_ = false; 2709 2710 return ok; 2711} 2712 2713// Return the type of the const reference. 2714 2715Type* 2716Const_expression::do_type() 2717{ 2718 if (this->type_ != NULL) 2719 return this->type_; 2720 2721 Named_constant* nc = this->constant_->const_value(); 2722 2723 if (this->seen_ || nc->lowering()) 2724 { 2725 this->report_error(_("constant refers to itself")); 2726 this->type_ = Type::make_error_type(); 2727 return this->type_; 2728 } 2729 2730 this->seen_ = true; 2731 2732 Type* ret = nc->type(); 2733 2734 if (ret != NULL) 2735 { 2736 this->seen_ = false; 2737 return ret; 2738 } 2739 2740 // During parsing, a named constant may have a NULL type, but we 2741 // must not return a NULL type here. 2742 ret = nc->expr()->type(); 2743 2744 this->seen_ = false; 2745 2746 return ret; 2747} 2748 2749// Set the type of the const reference. 2750 2751void 2752Const_expression::do_determine_type(const Type_context* context) 2753{ 2754 Type* ctype = this->constant_->const_value()->type(); 2755 Type* cetype = (ctype != NULL 2756 ? ctype 2757 : this->constant_->const_value()->expr()->type()); 2758 if (ctype != NULL && !ctype->is_abstract()) 2759 ; 2760 else if (context->type != NULL 2761 && context->type->is_numeric_type() 2762 && cetype->is_numeric_type()) 2763 this->type_ = context->type; 2764 else if (context->type != NULL 2765 && context->type->is_string_type() 2766 && cetype->is_string_type()) 2767 this->type_ = context->type; 2768 else if (context->type != NULL 2769 && context->type->is_boolean_type() 2770 && cetype->is_boolean_type()) 2771 this->type_ = context->type; 2772 else if (!context->may_be_abstract) 2773 { 2774 if (cetype->is_abstract()) 2775 cetype = cetype->make_non_abstract_type(); 2776 this->type_ = cetype; 2777 } 2778} 2779 2780// Check for a loop in which the initializer of a constant refers to 2781// the constant itself. 2782 2783void 2784Const_expression::check_for_init_loop() 2785{ 2786 if (this->type_ != NULL && this->type_->is_error()) 2787 return; 2788 2789 if (this->seen_) 2790 { 2791 this->report_error(_("constant refers to itself")); 2792 this->type_ = Type::make_error_type(); 2793 return; 2794 } 2795 2796 Expression* init = this->constant_->const_value()->expr(); 2797 Find_named_object find_named_object(this->constant_); 2798 2799 this->seen_ = true; 2800 Expression::traverse(&init, &find_named_object); 2801 this->seen_ = false; 2802 2803 if (find_named_object.found()) 2804 { 2805 if (this->type_ == NULL || !this->type_->is_error()) 2806 { 2807 this->report_error(_("constant refers to itself")); 2808 this->type_ = Type::make_error_type(); 2809 } 2810 return; 2811 } 2812} 2813 2814// Check types of a const reference. 2815 2816void 2817Const_expression::do_check_types(Gogo*) 2818{ 2819 if (this->type_ != NULL && this->type_->is_error()) 2820 return; 2821 2822 this->check_for_init_loop(); 2823 2824 // Check that numeric constant fits in type. 2825 if (this->type_ != NULL && this->type_->is_numeric_type()) 2826 { 2827 Numeric_constant nc; 2828 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) 2829 { 2830 if (!nc.set_type(this->type_, true, this->location())) 2831 this->set_is_error(); 2832 } 2833 } 2834} 2835 2836// Return the backend representation for a const reference. 2837 2838Bexpression* 2839Const_expression::do_get_backend(Translate_context* context) 2840{ 2841 if (this->type_ != NULL && this->type_->is_error()) 2842 return context->backend()->error_expression(); 2843 2844 // If the type has been set for this expression, but the underlying 2845 // object is an abstract int or float, we try to get the abstract 2846 // value. Otherwise we may lose something in the conversion. 2847 Expression* expr = this->constant_->const_value()->expr(); 2848 if (this->type_ != NULL 2849 && this->type_->is_numeric_type() 2850 && (this->constant_->const_value()->type() == NULL 2851 || this->constant_->const_value()->type()->is_abstract())) 2852 { 2853 Numeric_constant nc; 2854 if (expr->numeric_constant_value(&nc) 2855 && nc.set_type(this->type_, false, this->location())) 2856 { 2857 Expression* e = nc.expression(this->location()); 2858 return e->get_backend(context); 2859 } 2860 } 2861 2862 if (this->type_ != NULL) 2863 expr = Expression::make_cast(this->type_, expr, this->location()); 2864 return expr->get_backend(context); 2865} 2866 2867// Dump ast representation for constant expression. 2868 2869void 2870Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 2871{ 2872 ast_dump_context->ostream() << this->constant_->name(); 2873} 2874 2875// Make a reference to a constant in an expression. 2876 2877Expression* 2878Expression::make_const_reference(Named_object* constant, 2879 Location location) 2880{ 2881 return new Const_expression(constant, location); 2882} 2883 2884// Find a named object in an expression. 2885 2886int 2887Find_named_object::expression(Expression** pexpr) 2888{ 2889 switch ((*pexpr)->classification()) 2890 { 2891 case Expression::EXPRESSION_CONST_REFERENCE: 2892 { 2893 Const_expression* ce = static_cast<Const_expression*>(*pexpr); 2894 if (ce->named_object() == this->no_) 2895 break; 2896 2897 // We need to check a constant initializer explicitly, as 2898 // loops here will not be caught by the loop checking for 2899 // variable initializers. 2900 ce->check_for_init_loop(); 2901 2902 return TRAVERSE_CONTINUE; 2903 } 2904 2905 case Expression::EXPRESSION_VAR_REFERENCE: 2906 if ((*pexpr)->var_expression()->named_object() == this->no_) 2907 break; 2908 return TRAVERSE_CONTINUE; 2909 case Expression::EXPRESSION_FUNC_REFERENCE: 2910 if ((*pexpr)->func_expression()->named_object() == this->no_) 2911 break; 2912 return TRAVERSE_CONTINUE; 2913 default: 2914 return TRAVERSE_CONTINUE; 2915 } 2916 this->found_ = true; 2917 return TRAVERSE_EXIT; 2918} 2919 2920// The nil value. 2921 2922class Nil_expression : public Expression 2923{ 2924 public: 2925 Nil_expression(Location location) 2926 : Expression(EXPRESSION_NIL, location) 2927 { } 2928 2929 static Expression* 2930 do_import(Import*); 2931 2932 protected: 2933 bool 2934 do_is_constant() const 2935 { return true; } 2936 2937 bool 2938 do_is_immutable() const 2939 { return true; } 2940 2941 Type* 2942 do_type() 2943 { return Type::make_nil_type(); } 2944 2945 void 2946 do_determine_type(const Type_context*) 2947 { } 2948 2949 Expression* 2950 do_copy() 2951 { return this; } 2952 2953 Bexpression* 2954 do_get_backend(Translate_context* context) 2955 { return context->backend()->nil_pointer_expression(); } 2956 2957 void 2958 do_export(Export* exp) const 2959 { exp->write_c_string("nil"); } 2960 2961 void 2962 do_dump_expression(Ast_dump_context* ast_dump_context) const 2963 { ast_dump_context->ostream() << "nil"; } 2964}; 2965 2966// Import a nil expression. 2967 2968Expression* 2969Nil_expression::do_import(Import* imp) 2970{ 2971 imp->require_c_string("nil"); 2972 return Expression::make_nil(imp->location()); 2973} 2974 2975// Make a nil expression. 2976 2977Expression* 2978Expression::make_nil(Location location) 2979{ 2980 return new Nil_expression(location); 2981} 2982 2983// The value of the predeclared constant iota. This is little more 2984// than a marker. This will be lowered to an integer in 2985// Const_expression::do_lower, which is where we know the value that 2986// it should have. 2987 2988class Iota_expression : public Parser_expression 2989{ 2990 public: 2991 Iota_expression(Location location) 2992 : Parser_expression(EXPRESSION_IOTA, location) 2993 { } 2994 2995 protected: 2996 Expression* 2997 do_lower(Gogo*, Named_object*, Statement_inserter*, int) 2998 { go_unreachable(); } 2999 3000 // There should only ever be one of these. 3001 Expression* 3002 do_copy() 3003 { go_unreachable(); } 3004 3005 void 3006 do_dump_expression(Ast_dump_context* ast_dump_context) const 3007 { ast_dump_context->ostream() << "iota"; } 3008}; 3009 3010// Make an iota expression. This is only called for one case: the 3011// value of the predeclared constant iota. 3012 3013Expression* 3014Expression::make_iota() 3015{ 3016 static Iota_expression iota_expression(Linemap::unknown_location()); 3017 return &iota_expression; 3018} 3019 3020// A type conversion expression. 3021 3022class Type_conversion_expression : public Expression 3023{ 3024 public: 3025 Type_conversion_expression(Type* type, Expression* expr, 3026 Location location) 3027 : Expression(EXPRESSION_CONVERSION, location), 3028 type_(type), expr_(expr), may_convert_function_types_(false) 3029 { } 3030 3031 // Return the type to which we are converting. 3032 Type* 3033 type() const 3034 { return this->type_; } 3035 3036 // Return the expression which we are converting. 3037 Expression* 3038 expr() const 3039 { return this->expr_; } 3040 3041 // Permit converting from one function type to another. This is 3042 // used internally for method expressions. 3043 void 3044 set_may_convert_function_types() 3045 { 3046 this->may_convert_function_types_ = true; 3047 } 3048 3049 // Import a type conversion expression. 3050 static Expression* 3051 do_import(Import*); 3052 3053 protected: 3054 int 3055 do_traverse(Traverse* traverse); 3056 3057 Expression* 3058 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 3059 3060 Expression* 3061 do_flatten(Gogo*, Named_object*, Statement_inserter*); 3062 3063 bool 3064 do_is_constant() const; 3065 3066 bool 3067 do_is_immutable() const; 3068 3069 bool 3070 do_numeric_constant_value(Numeric_constant*) const; 3071 3072 bool 3073 do_string_constant_value(std::string*) const; 3074 3075 Type* 3076 do_type() 3077 { return this->type_; } 3078 3079 void 3080 do_determine_type(const Type_context*) 3081 { 3082 Type_context subcontext(this->type_, false); 3083 this->expr_->determine_type(&subcontext); 3084 } 3085 3086 void 3087 do_check_types(Gogo*); 3088 3089 Expression* 3090 do_copy() 3091 { 3092 return new Type_conversion_expression(this->type_, this->expr_->copy(), 3093 this->location()); 3094 } 3095 3096 Bexpression* 3097 do_get_backend(Translate_context* context); 3098 3099 void 3100 do_export(Export*) const; 3101 3102 void 3103 do_dump_expression(Ast_dump_context*) const; 3104 3105 private: 3106 // The type to convert to. 3107 Type* type_; 3108 // The expression to convert. 3109 Expression* expr_; 3110 // True if this is permitted to convert function types. This is 3111 // used internally for method expressions. 3112 bool may_convert_function_types_; 3113}; 3114 3115// Traversal. 3116 3117int 3118Type_conversion_expression::do_traverse(Traverse* traverse) 3119{ 3120 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT 3121 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 3122 return TRAVERSE_EXIT; 3123 return TRAVERSE_CONTINUE; 3124} 3125 3126// Convert to a constant at lowering time. 3127 3128Expression* 3129Type_conversion_expression::do_lower(Gogo*, Named_object*, 3130 Statement_inserter*, int) 3131{ 3132 Type* type = this->type_; 3133 Expression* val = this->expr_; 3134 Location location = this->location(); 3135 3136 if (type->is_numeric_type()) 3137 { 3138 Numeric_constant nc; 3139 if (val->numeric_constant_value(&nc)) 3140 { 3141 if (!nc.set_type(type, true, location)) 3142 return Expression::make_error(location); 3143 return nc.expression(location); 3144 } 3145 } 3146 3147 if (type->is_slice_type()) 3148 { 3149 Type* element_type = type->array_type()->element_type()->forwarded(); 3150 bool is_byte = (element_type->integer_type() != NULL 3151 && element_type->integer_type()->is_byte()); 3152 bool is_rune = (element_type->integer_type() != NULL 3153 && element_type->integer_type()->is_rune()); 3154 if (is_byte || is_rune) 3155 { 3156 std::string s; 3157 if (val->string_constant_value(&s)) 3158 { 3159 Expression_list* vals = new Expression_list(); 3160 if (is_byte) 3161 { 3162 for (std::string::const_iterator p = s.begin(); 3163 p != s.end(); 3164 p++) 3165 { 3166 unsigned char c = static_cast<unsigned char>(*p); 3167 vals->push_back(Expression::make_integer_ul(c, 3168 element_type, 3169 location)); 3170 } 3171 } 3172 else 3173 { 3174 const char *p = s.data(); 3175 const char *pend = s.data() + s.length(); 3176 while (p < pend) 3177 { 3178 unsigned int c; 3179 int adv = Lex::fetch_char(p, &c); 3180 if (adv == 0) 3181 { 3182 warning_at(this->location(), 0, 3183 "invalid UTF-8 encoding"); 3184 adv = 1; 3185 } 3186 p += adv; 3187 vals->push_back(Expression::make_integer_ul(c, 3188 element_type, 3189 location)); 3190 } 3191 } 3192 3193 return Expression::make_slice_composite_literal(type, vals, 3194 location); 3195 } 3196 } 3197 } 3198 3199 return this; 3200} 3201 3202// Flatten a type conversion by using a temporary variable for the slice 3203// in slice to string conversions. 3204 3205Expression* 3206Type_conversion_expression::do_flatten(Gogo*, Named_object*, 3207 Statement_inserter* inserter) 3208{ 3209 if (((this->type()->is_string_type() 3210 && this->expr_->type()->is_slice_type()) 3211 || this->expr_->type()->interface_type() != NULL) 3212 && !this->expr_->is_variable()) 3213 { 3214 Temporary_statement* temp = 3215 Statement::make_temporary(NULL, this->expr_, this->location()); 3216 inserter->insert(temp); 3217 this->expr_ = Expression::make_temporary_reference(temp, this->location()); 3218 } 3219 return this; 3220} 3221 3222// Return whether a type conversion is a constant. 3223 3224bool 3225Type_conversion_expression::do_is_constant() const 3226{ 3227 if (!this->expr_->is_constant()) 3228 return false; 3229 3230 // A conversion to a type that may not be used as a constant is not 3231 // a constant. For example, []byte(nil). 3232 Type* type = this->type_; 3233 if (type->integer_type() == NULL 3234 && type->float_type() == NULL 3235 && type->complex_type() == NULL 3236 && !type->is_boolean_type() 3237 && !type->is_string_type()) 3238 return false; 3239 3240 return true; 3241} 3242 3243// Return whether a type conversion is immutable. 3244 3245bool 3246Type_conversion_expression::do_is_immutable() const 3247{ 3248 Type* type = this->type_; 3249 Type* expr_type = this->expr_->type(); 3250 3251 if (type->interface_type() != NULL 3252 || expr_type->interface_type() != NULL) 3253 return false; 3254 3255 if (!this->expr_->is_immutable()) 3256 return false; 3257 3258 if (Type::are_identical(type, expr_type, false, NULL)) 3259 return true; 3260 3261 return type->is_basic_type() && expr_type->is_basic_type(); 3262} 3263 3264// Return the constant numeric value if there is one. 3265 3266bool 3267Type_conversion_expression::do_numeric_constant_value( 3268 Numeric_constant* nc) const 3269{ 3270 if (!this->type_->is_numeric_type()) 3271 return false; 3272 if (!this->expr_->numeric_constant_value(nc)) 3273 return false; 3274 return nc->set_type(this->type_, false, this->location()); 3275} 3276 3277// Return the constant string value if there is one. 3278 3279bool 3280Type_conversion_expression::do_string_constant_value(std::string* val) const 3281{ 3282 if (this->type_->is_string_type() 3283 && this->expr_->type()->integer_type() != NULL) 3284 { 3285 Numeric_constant nc; 3286 if (this->expr_->numeric_constant_value(&nc)) 3287 { 3288 unsigned long ival; 3289 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) 3290 { 3291 val->clear(); 3292 Lex::append_char(ival, true, val, this->location()); 3293 return true; 3294 } 3295 } 3296 } 3297 3298 // FIXME: Could handle conversion from const []int here. 3299 3300 return false; 3301} 3302 3303// Check that types are convertible. 3304 3305void 3306Type_conversion_expression::do_check_types(Gogo*) 3307{ 3308 Type* type = this->type_; 3309 Type* expr_type = this->expr_->type(); 3310 std::string reason; 3311 3312 if (type->is_error() || expr_type->is_error()) 3313 { 3314 this->set_is_error(); 3315 return; 3316 } 3317 3318 if (this->may_convert_function_types_ 3319 && type->function_type() != NULL 3320 && expr_type->function_type() != NULL) 3321 return; 3322 3323 if (Type::are_convertible(type, expr_type, &reason)) 3324 return; 3325 3326 error_at(this->location(), "%s", reason.c_str()); 3327 this->set_is_error(); 3328} 3329 3330// Get the backend representation for a type conversion. 3331 3332Bexpression* 3333Type_conversion_expression::do_get_backend(Translate_context* context) 3334{ 3335 Type* type = this->type_; 3336 Type* expr_type = this->expr_->type(); 3337 3338 Gogo* gogo = context->gogo(); 3339 Btype* btype = type->get_backend(gogo); 3340 Bexpression* bexpr = this->expr_->get_backend(context); 3341 Location loc = this->location(); 3342 3343 if (Type::are_identical(type, expr_type, false, NULL)) 3344 return gogo->backend()->convert_expression(btype, bexpr, loc); 3345 else if (type->interface_type() != NULL 3346 || expr_type->interface_type() != NULL) 3347 { 3348 Expression* conversion = 3349 Expression::convert_for_assignment(gogo, type, this->expr_, 3350 this->location()); 3351 return conversion->get_backend(context); 3352 } 3353 else if (type->is_string_type() 3354 && expr_type->integer_type() != NULL) 3355 { 3356 mpz_t intval; 3357 Numeric_constant nc; 3358 if (this->expr_->numeric_constant_value(&nc) 3359 && nc.to_int(&intval) 3360 && mpz_fits_ushort_p(intval)) 3361 { 3362 std::string s; 3363 Lex::append_char(mpz_get_ui(intval), true, &s, loc); 3364 mpz_clear(intval); 3365 Expression* se = Expression::make_string(s, loc); 3366 return se->get_backend(context); 3367 } 3368 3369 Expression* i2s_expr = 3370 Runtime::make_call(Runtime::INT_TO_STRING, loc, 1, this->expr_); 3371 return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); 3372 } 3373 else if (type->is_string_type() && expr_type->is_slice_type()) 3374 { 3375 Array_type* a = expr_type->array_type(); 3376 Type* e = a->element_type()->forwarded(); 3377 go_assert(e->integer_type() != NULL); 3378 go_assert(this->expr_->is_variable()); 3379 3380 Runtime::Function code; 3381 if (e->integer_type()->is_byte()) 3382 code = Runtime::BYTE_ARRAY_TO_STRING; 3383 else 3384 { 3385 go_assert(e->integer_type()->is_rune()); 3386 code = Runtime::INT_ARRAY_TO_STRING; 3387 } 3388 Expression* valptr = a->get_value_pointer(gogo, this->expr_); 3389 Expression* len = a->get_length(gogo, this->expr_); 3390 return Runtime::make_call(code, loc, 2, valptr, 3391 len)->get_backend(context); 3392 } 3393 else if (type->is_slice_type() && expr_type->is_string_type()) 3394 { 3395 Type* e = type->array_type()->element_type()->forwarded(); 3396 go_assert(e->integer_type() != NULL); 3397 3398 Runtime::Function code; 3399 if (e->integer_type()->is_byte()) 3400 code = Runtime::STRING_TO_BYTE_ARRAY; 3401 else 3402 { 3403 go_assert(e->integer_type()->is_rune()); 3404 code = Runtime::STRING_TO_INT_ARRAY; 3405 } 3406 Expression* s2a = Runtime::make_call(code, loc, 1, this->expr_); 3407 return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); 3408 } 3409 else if (type->is_numeric_type()) 3410 { 3411 go_assert(Type::are_convertible(type, expr_type, NULL)); 3412 return gogo->backend()->convert_expression(btype, bexpr, loc); 3413 } 3414 else if ((type->is_unsafe_pointer_type() 3415 && (expr_type->points_to() != NULL 3416 || expr_type->integer_type())) 3417 || (expr_type->is_unsafe_pointer_type() 3418 && type->points_to() != NULL) 3419 || (this->may_convert_function_types_ 3420 && type->function_type() != NULL 3421 && expr_type->function_type() != NULL)) 3422 return gogo->backend()->convert_expression(btype, bexpr, loc); 3423 else 3424 { 3425 Expression* conversion = 3426 Expression::convert_for_assignment(gogo, type, this->expr_, loc); 3427 return conversion->get_backend(context); 3428 } 3429} 3430 3431// Output a type conversion in a constant expression. 3432 3433void 3434Type_conversion_expression::do_export(Export* exp) const 3435{ 3436 exp->write_c_string("convert("); 3437 exp->write_type(this->type_); 3438 exp->write_c_string(", "); 3439 this->expr_->export_expression(exp); 3440 exp->write_c_string(")"); 3441} 3442 3443// Import a type conversion or a struct construction. 3444 3445Expression* 3446Type_conversion_expression::do_import(Import* imp) 3447{ 3448 imp->require_c_string("convert("); 3449 Type* type = imp->read_type(); 3450 imp->require_c_string(", "); 3451 Expression* val = Expression::import_expression(imp); 3452 imp->require_c_string(")"); 3453 return Expression::make_cast(type, val, imp->location()); 3454} 3455 3456// Dump ast representation for a type conversion expression. 3457 3458void 3459Type_conversion_expression::do_dump_expression( 3460 Ast_dump_context* ast_dump_context) const 3461{ 3462 ast_dump_context->dump_type(this->type_); 3463 ast_dump_context->ostream() << "("; 3464 ast_dump_context->dump_expression(this->expr_); 3465 ast_dump_context->ostream() << ") "; 3466} 3467 3468// Make a type cast expression. 3469 3470Expression* 3471Expression::make_cast(Type* type, Expression* val, Location location) 3472{ 3473 if (type->is_error_type() || val->is_error_expression()) 3474 return Expression::make_error(location); 3475 return new Type_conversion_expression(type, val, location); 3476} 3477 3478// An unsafe type conversion, used to pass values to builtin functions. 3479 3480class Unsafe_type_conversion_expression : public Expression 3481{ 3482 public: 3483 Unsafe_type_conversion_expression(Type* type, Expression* expr, 3484 Location location) 3485 : Expression(EXPRESSION_UNSAFE_CONVERSION, location), 3486 type_(type), expr_(expr) 3487 { } 3488 3489 protected: 3490 int 3491 do_traverse(Traverse* traverse); 3492 3493 bool 3494 do_is_immutable() const; 3495 3496 Type* 3497 do_type() 3498 { return this->type_; } 3499 3500 void 3501 do_determine_type(const Type_context*) 3502 { this->expr_->determine_type_no_context(); } 3503 3504 Expression* 3505 do_copy() 3506 { 3507 return new Unsafe_type_conversion_expression(this->type_, 3508 this->expr_->copy(), 3509 this->location()); 3510 } 3511 3512 Bexpression* 3513 do_get_backend(Translate_context*); 3514 3515 void 3516 do_dump_expression(Ast_dump_context*) const; 3517 3518 private: 3519 // The type to convert to. 3520 Type* type_; 3521 // The expression to convert. 3522 Expression* expr_; 3523}; 3524 3525// Traversal. 3526 3527int 3528Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) 3529{ 3530 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT 3531 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 3532 return TRAVERSE_EXIT; 3533 return TRAVERSE_CONTINUE; 3534} 3535 3536// Return whether an unsafe type conversion is immutable. 3537 3538bool 3539Unsafe_type_conversion_expression::do_is_immutable() const 3540{ 3541 Type* type = this->type_; 3542 Type* expr_type = this->expr_->type(); 3543 3544 if (type->interface_type() != NULL 3545 || expr_type->interface_type() != NULL) 3546 return false; 3547 3548 if (!this->expr_->is_immutable()) 3549 return false; 3550 3551 if (Type::are_convertible(type, expr_type, NULL)) 3552 return true; 3553 3554 return type->is_basic_type() && expr_type->is_basic_type(); 3555} 3556 3557// Convert to backend representation. 3558 3559Bexpression* 3560Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) 3561{ 3562 // We are only called for a limited number of cases. 3563 3564 Type* t = this->type_; 3565 Type* et = this->expr_->type(); 3566 if (t->array_type() != NULL) 3567 go_assert(et->array_type() != NULL 3568 && t->is_slice_type() == et->is_slice_type()); 3569 else if (t->struct_type() != NULL) 3570 { 3571 if (t->named_type() != NULL 3572 && et->named_type() != NULL 3573 && !Type::are_convertible(t, et, NULL)) 3574 { 3575 go_assert(saw_errors()); 3576 return context->backend()->error_expression(); 3577 } 3578 3579 go_assert(et->struct_type() != NULL 3580 && Type::are_convertible(t, et, NULL)); 3581 } 3582 else if (t->map_type() != NULL) 3583 go_assert(et->map_type() != NULL); 3584 else if (t->channel_type() != NULL) 3585 go_assert(et->channel_type() != NULL); 3586 else if (t->points_to() != NULL) 3587 go_assert(et->points_to() != NULL 3588 || et->channel_type() != NULL 3589 || et->map_type() != NULL 3590 || et->function_type() != NULL 3591 || et->is_nil_type()); 3592 else if (et->is_unsafe_pointer_type()) 3593 go_assert(t->points_to() != NULL); 3594 else if (t->interface_type() != NULL) 3595 { 3596 bool empty_iface = t->interface_type()->is_empty(); 3597 go_assert(et->interface_type() != NULL 3598 && et->interface_type()->is_empty() == empty_iface); 3599 } 3600 else if (t->integer_type() != NULL) 3601 go_assert(et->is_boolean_type() 3602 || et->integer_type() != NULL 3603 || et->function_type() != NULL 3604 || et->points_to() != NULL 3605 || et->map_type() != NULL 3606 || et->channel_type() != NULL 3607 || et->is_nil_type()); 3608 else 3609 go_unreachable(); 3610 3611 Gogo* gogo = context->gogo(); 3612 Btype* btype = t->get_backend(gogo); 3613 Bexpression* bexpr = this->expr_->get_backend(context); 3614 Location loc = this->location(); 3615 return gogo->backend()->convert_expression(btype, bexpr, loc); 3616} 3617 3618// Dump ast representation for an unsafe type conversion expression. 3619 3620void 3621Unsafe_type_conversion_expression::do_dump_expression( 3622 Ast_dump_context* ast_dump_context) const 3623{ 3624 ast_dump_context->dump_type(this->type_); 3625 ast_dump_context->ostream() << "("; 3626 ast_dump_context->dump_expression(this->expr_); 3627 ast_dump_context->ostream() << ") "; 3628} 3629 3630// Make an unsafe type conversion expression. 3631 3632Expression* 3633Expression::make_unsafe_cast(Type* type, Expression* expr, 3634 Location location) 3635{ 3636 return new Unsafe_type_conversion_expression(type, expr, location); 3637} 3638 3639// Class Unary_expression. 3640 3641// If we are taking the address of a composite literal, and the 3642// contents are not constant, then we want to make a heap expression 3643// instead. 3644 3645Expression* 3646Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) 3647{ 3648 Location loc = this->location(); 3649 Operator op = this->op_; 3650 Expression* expr = this->expr_; 3651 3652 if (op == OPERATOR_MULT && expr->is_type_expression()) 3653 return Expression::make_type(Type::make_pointer_type(expr->type()), loc); 3654 3655 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require 3656 // moving x to the heap. FIXME: Is it worth doing a real escape 3657 // analysis here? This case is found in math/unsafe.go and is 3658 // therefore worth special casing. 3659 if (op == OPERATOR_MULT) 3660 { 3661 Expression* e = expr; 3662 while (e->classification() == EXPRESSION_CONVERSION) 3663 { 3664 Type_conversion_expression* te 3665 = static_cast<Type_conversion_expression*>(e); 3666 e = te->expr(); 3667 } 3668 3669 if (e->classification() == EXPRESSION_UNARY) 3670 { 3671 Unary_expression* ue = static_cast<Unary_expression*>(e); 3672 if (ue->op_ == OPERATOR_AND) 3673 { 3674 if (e == expr) 3675 { 3676 // *&x == x. 3677 if (!ue->expr_->is_addressable() && !ue->create_temp_) 3678 { 3679 error_at(ue->location(), 3680 "invalid operand for unary %<&%>"); 3681 this->set_is_error(); 3682 } 3683 return ue->expr_; 3684 } 3685 ue->set_does_not_escape(); 3686 } 3687 } 3688 } 3689 3690 // Catching an invalid indirection of unsafe.Pointer here avoid 3691 // having to deal with TYPE_VOID in other places. 3692 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) 3693 { 3694 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); 3695 return Expression::make_error(this->location()); 3696 } 3697 3698 // Check for an invalid pointer dereference. We need to do this 3699 // here because Unary_expression::do_type will return an error type 3700 // in this case. That can cause code to appear erroneous, and 3701 // therefore disappear at lowering time, without any error message. 3702 if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) 3703 { 3704 this->report_error(_("expected pointer")); 3705 return Expression::make_error(this->location()); 3706 } 3707 3708 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) 3709 { 3710 Numeric_constant nc; 3711 if (expr->numeric_constant_value(&nc)) 3712 { 3713 Numeric_constant result; 3714 if (Unary_expression::eval_constant(op, &nc, loc, &result)) 3715 return result.expression(loc); 3716 } 3717 } 3718 3719 return this; 3720} 3721 3722// Flatten expression if a nil check must be performed and create temporary 3723// variables if necessary. 3724 3725Expression* 3726Unary_expression::do_flatten(Gogo* gogo, Named_object*, 3727 Statement_inserter* inserter) 3728{ 3729 if (this->is_error_expression() || this->expr_->is_error_expression()) 3730 return Expression::make_error(this->location()); 3731 3732 Location location = this->location(); 3733 if (this->op_ == OPERATOR_MULT 3734 && !this->expr_->is_variable()) 3735 { 3736 go_assert(this->expr_->type()->points_to() != NULL); 3737 Type* ptype = this->expr_->type()->points_to(); 3738 if (!ptype->is_void_type()) 3739 { 3740 Btype* pbtype = ptype->get_backend(gogo); 3741 int64_t s = gogo->backend()->type_size(pbtype); 3742 if (s >= 4096 || this->issue_nil_check_) 3743 { 3744 Temporary_statement* temp = 3745 Statement::make_temporary(NULL, this->expr_, location); 3746 inserter->insert(temp); 3747 this->expr_ = 3748 Expression::make_temporary_reference(temp, location); 3749 } 3750 } 3751 } 3752 3753 if (this->create_temp_ && !this->expr_->is_variable()) 3754 { 3755 Temporary_statement* temp = 3756 Statement::make_temporary(NULL, this->expr_, location); 3757 inserter->insert(temp); 3758 this->expr_ = Expression::make_temporary_reference(temp, location); 3759 } 3760 3761 return this; 3762} 3763 3764// Return whether a unary expression is a constant. 3765 3766bool 3767Unary_expression::do_is_constant() const 3768{ 3769 if (this->op_ == OPERATOR_MULT) 3770 { 3771 // Indirecting through a pointer is only constant if the object 3772 // to which the expression points is constant, but we currently 3773 // have no way to determine that. 3774 return false; 3775 } 3776 else if (this->op_ == OPERATOR_AND) 3777 { 3778 // Taking the address of a variable is constant if it is a 3779 // global variable, not constant otherwise. In other cases taking the 3780 // address is probably not a constant. 3781 Var_expression* ve = this->expr_->var_expression(); 3782 if (ve != NULL) 3783 { 3784 Named_object* no = ve->named_object(); 3785 return no->is_variable() && no->var_value()->is_global(); 3786 } 3787 return false; 3788 } 3789 else 3790 return this->expr_->is_constant(); 3791} 3792 3793// Apply unary opcode OP to UNC, setting NC. Return true if this 3794// could be done, false if not. Issue errors for overflow. 3795 3796bool 3797Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, 3798 Location location, Numeric_constant* nc) 3799{ 3800 switch (op) 3801 { 3802 case OPERATOR_PLUS: 3803 *nc = *unc; 3804 return true; 3805 3806 case OPERATOR_MINUS: 3807 if (unc->is_int() || unc->is_rune()) 3808 break; 3809 else if (unc->is_float()) 3810 { 3811 mpfr_t uval; 3812 unc->get_float(&uval); 3813 mpfr_t val; 3814 mpfr_init(val); 3815 mpfr_neg(val, uval, GMP_RNDN); 3816 nc->set_float(unc->type(), val); 3817 mpfr_clear(uval); 3818 mpfr_clear(val); 3819 return true; 3820 } 3821 else if (unc->is_complex()) 3822 { 3823 mpc_t uval; 3824 unc->get_complex(&uval); 3825 mpc_t val; 3826 mpc_init2(val, mpc_precision); 3827 mpc_neg(val, uval, MPC_RNDNN); 3828 nc->set_complex(unc->type(), val); 3829 mpc_clear(uval); 3830 mpc_clear(val); 3831 return true; 3832 } 3833 else 3834 go_unreachable(); 3835 3836 case OPERATOR_XOR: 3837 break; 3838 3839 case OPERATOR_NOT: 3840 case OPERATOR_AND: 3841 case OPERATOR_MULT: 3842 return false; 3843 3844 default: 3845 go_unreachable(); 3846 } 3847 3848 if (!unc->is_int() && !unc->is_rune()) 3849 return false; 3850 3851 mpz_t uval; 3852 if (unc->is_rune()) 3853 unc->get_rune(&uval); 3854 else 3855 unc->get_int(&uval); 3856 mpz_t val; 3857 mpz_init(val); 3858 3859 switch (op) 3860 { 3861 case OPERATOR_MINUS: 3862 mpz_neg(val, uval); 3863 break; 3864 3865 case OPERATOR_NOT: 3866 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); 3867 break; 3868 3869 case OPERATOR_XOR: 3870 { 3871 Type* utype = unc->type(); 3872 if (utype->integer_type() == NULL 3873 || utype->integer_type()->is_abstract()) 3874 mpz_com(val, uval); 3875 else 3876 { 3877 // The number of HOST_WIDE_INTs that it takes to represent 3878 // UVAL. 3879 size_t count = ((mpz_sizeinbase(uval, 2) 3880 + HOST_BITS_PER_WIDE_INT 3881 - 1) 3882 / HOST_BITS_PER_WIDE_INT); 3883 3884 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; 3885 memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); 3886 3887 size_t obits = utype->integer_type()->bits(); 3888 3889 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) 3890 { 3891 mpz_t adj; 3892 mpz_init_set_ui(adj, 1); 3893 mpz_mul_2exp(adj, adj, obits); 3894 mpz_add(uval, uval, adj); 3895 mpz_clear(adj); 3896 } 3897 3898 size_t ecount; 3899 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); 3900 go_assert(ecount <= count); 3901 3902 // Trim down to the number of words required by the type. 3903 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) 3904 / HOST_BITS_PER_WIDE_INT); 3905 go_assert(ocount <= count); 3906 3907 for (size_t i = 0; i < ocount; ++i) 3908 phwi[i] = ~phwi[i]; 3909 3910 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; 3911 if (clearbits != 0) 3912 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) 3913 >> clearbits); 3914 3915 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); 3916 3917 if (!utype->integer_type()->is_unsigned() 3918 && mpz_tstbit(val, obits - 1)) 3919 { 3920 mpz_t adj; 3921 mpz_init_set_ui(adj, 1); 3922 mpz_mul_2exp(adj, adj, obits); 3923 mpz_sub(val, val, adj); 3924 mpz_clear(adj); 3925 } 3926 3927 delete[] phwi; 3928 } 3929 } 3930 break; 3931 3932 default: 3933 go_unreachable(); 3934 } 3935 3936 if (unc->is_rune()) 3937 nc->set_rune(NULL, val); 3938 else 3939 nc->set_int(NULL, val); 3940 3941 mpz_clear(uval); 3942 mpz_clear(val); 3943 3944 return nc->set_type(unc->type(), true, location); 3945} 3946 3947// Return the integral constant value of a unary expression, if it has one. 3948 3949bool 3950Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const 3951{ 3952 Numeric_constant unc; 3953 if (!this->expr_->numeric_constant_value(&unc)) 3954 return false; 3955 return Unary_expression::eval_constant(this->op_, &unc, this->location(), 3956 nc); 3957} 3958 3959// Return the type of a unary expression. 3960 3961Type* 3962Unary_expression::do_type() 3963{ 3964 switch (this->op_) 3965 { 3966 case OPERATOR_PLUS: 3967 case OPERATOR_MINUS: 3968 case OPERATOR_NOT: 3969 case OPERATOR_XOR: 3970 return this->expr_->type(); 3971 3972 case OPERATOR_AND: 3973 return Type::make_pointer_type(this->expr_->type()); 3974 3975 case OPERATOR_MULT: 3976 { 3977 Type* subtype = this->expr_->type(); 3978 Type* points_to = subtype->points_to(); 3979 if (points_to == NULL) 3980 return Type::make_error_type(); 3981 return points_to; 3982 } 3983 3984 default: 3985 go_unreachable(); 3986 } 3987} 3988 3989// Determine abstract types for a unary expression. 3990 3991void 3992Unary_expression::do_determine_type(const Type_context* context) 3993{ 3994 switch (this->op_) 3995 { 3996 case OPERATOR_PLUS: 3997 case OPERATOR_MINUS: 3998 case OPERATOR_NOT: 3999 case OPERATOR_XOR: 4000 this->expr_->determine_type(context); 4001 break; 4002 4003 case OPERATOR_AND: 4004 // Taking the address of something. 4005 { 4006 Type* subtype = (context->type == NULL 4007 ? NULL 4008 : context->type->points_to()); 4009 Type_context subcontext(subtype, false); 4010 this->expr_->determine_type(&subcontext); 4011 } 4012 break; 4013 4014 case OPERATOR_MULT: 4015 // Indirecting through a pointer. 4016 { 4017 Type* subtype = (context->type == NULL 4018 ? NULL 4019 : Type::make_pointer_type(context->type)); 4020 Type_context subcontext(subtype, false); 4021 this->expr_->determine_type(&subcontext); 4022 } 4023 break; 4024 4025 default: 4026 go_unreachable(); 4027 } 4028} 4029 4030// Check types for a unary expression. 4031 4032void 4033Unary_expression::do_check_types(Gogo*) 4034{ 4035 Type* type = this->expr_->type(); 4036 if (type->is_error()) 4037 { 4038 this->set_is_error(); 4039 return; 4040 } 4041 4042 switch (this->op_) 4043 { 4044 case OPERATOR_PLUS: 4045 case OPERATOR_MINUS: 4046 if (type->integer_type() == NULL 4047 && type->float_type() == NULL 4048 && type->complex_type() == NULL) 4049 this->report_error(_("expected numeric type")); 4050 break; 4051 4052 case OPERATOR_NOT: 4053 if (!type->is_boolean_type()) 4054 this->report_error(_("expected boolean type")); 4055 break; 4056 4057 case OPERATOR_XOR: 4058 if (type->integer_type() == NULL 4059 && !type->is_boolean_type()) 4060 this->report_error(_("expected integer or boolean type")); 4061 break; 4062 4063 case OPERATOR_AND: 4064 if (!this->expr_->is_addressable()) 4065 { 4066 if (!this->create_temp_) 4067 { 4068 error_at(this->location(), "invalid operand for unary %<&%>"); 4069 this->set_is_error(); 4070 } 4071 } 4072 else 4073 { 4074 this->expr_->address_taken(this->escapes_); 4075 this->expr_->issue_nil_check(); 4076 } 4077 break; 4078 4079 case OPERATOR_MULT: 4080 // Indirecting through a pointer. 4081 if (type->points_to() == NULL) 4082 this->report_error(_("expected pointer")); 4083 break; 4084 4085 default: 4086 go_unreachable(); 4087 } 4088} 4089 4090// Get the backend representation for a unary expression. 4091 4092Bexpression* 4093Unary_expression::do_get_backend(Translate_context* context) 4094{ 4095 Gogo* gogo = context->gogo(); 4096 Location loc = this->location(); 4097 4098 // Taking the address of a set-and-use-temporary expression requires 4099 // setting the temporary and then taking the address. 4100 if (this->op_ == OPERATOR_AND) 4101 { 4102 Set_and_use_temporary_expression* sut = 4103 this->expr_->set_and_use_temporary_expression(); 4104 if (sut != NULL) 4105 { 4106 Temporary_statement* temp = sut->temporary(); 4107 Bvariable* bvar = temp->get_backend_variable(context); 4108 Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc); 4109 Bexpression* bval = sut->expression()->get_backend(context); 4110 4111 Bstatement* bassign = 4112 gogo->backend()->assignment_statement(bvar_expr, bval, loc); 4113 Bexpression* bvar_addr = 4114 gogo->backend()->address_expression(bvar_expr, loc); 4115 return gogo->backend()->compound_expression(bassign, bvar_addr, loc); 4116 } 4117 } 4118 4119 Bexpression* ret; 4120 Bexpression* bexpr = this->expr_->get_backend(context); 4121 Btype* btype = this->expr_->type()->get_backend(gogo); 4122 switch (this->op_) 4123 { 4124 case OPERATOR_PLUS: 4125 ret = bexpr; 4126 break; 4127 4128 case OPERATOR_MINUS: 4129 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); 4130 ret = gogo->backend()->convert_expression(btype, ret, loc); 4131 break; 4132 4133 case OPERATOR_NOT: 4134 case OPERATOR_XOR: 4135 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); 4136 break; 4137 4138 case OPERATOR_AND: 4139 if (!this->create_temp_) 4140 { 4141 // We should not see a non-constant constructor here; cases 4142 // where we would see one should have been moved onto the 4143 // heap at parse time. Taking the address of a nonconstant 4144 // constructor will not do what the programmer expects. 4145 4146 go_assert(!this->expr_->is_composite_literal() 4147 || this->expr_->is_immutable()); 4148 if (this->expr_->classification() == EXPRESSION_UNARY) 4149 { 4150 Unary_expression* ue = 4151 static_cast<Unary_expression*>(this->expr_); 4152 go_assert(ue->op() != OPERATOR_AND); 4153 } 4154 } 4155 4156 static unsigned int counter; 4157 char buf[100]; 4158 if (this->is_gc_root_ || this->is_slice_init_) 4159 { 4160 bool copy_to_heap = false; 4161 if (this->is_gc_root_) 4162 { 4163 // Build a decl for a GC root variable. GC roots are mutable, so 4164 // they cannot be represented as an immutable_struct in the 4165 // backend. 4166 static unsigned int root_counter; 4167 snprintf(buf, sizeof buf, "gc%u", root_counter); 4168 ++root_counter; 4169 } 4170 else 4171 { 4172 // Build a decl for a slice value initializer. An immutable slice 4173 // value initializer may have to be copied to the heap if it 4174 // contains pointers in a non-constant context. 4175 snprintf(buf, sizeof buf, "C%u", counter); 4176 ++counter; 4177 4178 Array_type* at = this->expr_->type()->array_type(); 4179 go_assert(at != NULL); 4180 4181 // If we are not copying the value to the heap, we will only 4182 // initialize the value once, so we can use this directly 4183 // rather than copying it. In that case we can't make it 4184 // read-only, because the program is permitted to change it. 4185 copy_to_heap = (at->element_type()->has_pointer() 4186 && !context->is_const()); 4187 } 4188 Bvariable* implicit = 4189 gogo->backend()->implicit_variable(buf, btype, true, copy_to_heap, 4190 false, 0); 4191 gogo->backend()->implicit_variable_set_init(implicit, buf, btype, 4192 true, copy_to_heap, false, 4193 bexpr); 4194 bexpr = gogo->backend()->var_expression(implicit, loc); 4195 } 4196 else if ((this->expr_->is_composite_literal() 4197 || this->expr_->string_expression() != NULL) 4198 && this->expr_->is_immutable()) 4199 { 4200 // Build a decl for a constant constructor. 4201 snprintf(buf, sizeof buf, "C%u", counter); 4202 ++counter; 4203 4204 Bvariable* decl = 4205 gogo->backend()->immutable_struct(buf, true, false, btype, loc); 4206 gogo->backend()->immutable_struct_set_init(decl, buf, true, false, 4207 btype, loc, bexpr); 4208 bexpr = gogo->backend()->var_expression(decl, loc); 4209 } 4210 4211 go_assert(!this->create_temp_ || this->expr_->is_variable()); 4212 ret = gogo->backend()->address_expression(bexpr, loc); 4213 break; 4214 4215 case OPERATOR_MULT: 4216 { 4217 go_assert(this->expr_->type()->points_to() != NULL); 4218 4219 // If we are dereferencing the pointer to a large struct, we 4220 // need to check for nil. We don't bother to check for small 4221 // structs because we expect the system to crash on a nil 4222 // pointer dereference. However, if we know the address of this 4223 // expression is being taken, we must always check for nil. 4224 4225 Type* ptype = this->expr_->type()->points_to(); 4226 Btype* pbtype = ptype->get_backend(gogo); 4227 if (!ptype->is_void_type()) 4228 { 4229 int64_t s = gogo->backend()->type_size(pbtype); 4230 if (s >= 4096 || this->issue_nil_check_) 4231 { 4232 go_assert(this->expr_->is_variable()); 4233 Bexpression* nil = 4234 Expression::make_nil(loc)->get_backend(context); 4235 Bexpression* compare = 4236 gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr, 4237 nil, loc); 4238 Bexpression* crash = 4239 gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, 4240 loc)->get_backend(context); 4241 bexpr = gogo->backend()->conditional_expression(btype, compare, 4242 crash, bexpr, 4243 loc); 4244 4245 } 4246 } 4247 ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); 4248 } 4249 break; 4250 4251 default: 4252 go_unreachable(); 4253 } 4254 4255 return ret; 4256} 4257 4258// Export a unary expression. 4259 4260void 4261Unary_expression::do_export(Export* exp) const 4262{ 4263 switch (this->op_) 4264 { 4265 case OPERATOR_PLUS: 4266 exp->write_c_string("+ "); 4267 break; 4268 case OPERATOR_MINUS: 4269 exp->write_c_string("- "); 4270 break; 4271 case OPERATOR_NOT: 4272 exp->write_c_string("! "); 4273 break; 4274 case OPERATOR_XOR: 4275 exp->write_c_string("^ "); 4276 break; 4277 case OPERATOR_AND: 4278 case OPERATOR_MULT: 4279 default: 4280 go_unreachable(); 4281 } 4282 this->expr_->export_expression(exp); 4283} 4284 4285// Import a unary expression. 4286 4287Expression* 4288Unary_expression::do_import(Import* imp) 4289{ 4290 Operator op; 4291 switch (imp->get_char()) 4292 { 4293 case '+': 4294 op = OPERATOR_PLUS; 4295 break; 4296 case '-': 4297 op = OPERATOR_MINUS; 4298 break; 4299 case '!': 4300 op = OPERATOR_NOT; 4301 break; 4302 case '^': 4303 op = OPERATOR_XOR; 4304 break; 4305 default: 4306 go_unreachable(); 4307 } 4308 imp->require_c_string(" "); 4309 Expression* expr = Expression::import_expression(imp); 4310 return Expression::make_unary(op, expr, imp->location()); 4311} 4312 4313// Dump ast representation of an unary expression. 4314 4315void 4316Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 4317{ 4318 ast_dump_context->dump_operator(this->op_); 4319 ast_dump_context->ostream() << "("; 4320 ast_dump_context->dump_expression(this->expr_); 4321 ast_dump_context->ostream() << ") "; 4322} 4323 4324// Make a unary expression. 4325 4326Expression* 4327Expression::make_unary(Operator op, Expression* expr, Location location) 4328{ 4329 return new Unary_expression(op, expr, location); 4330} 4331 4332// If this is an indirection through a pointer, return the expression 4333// being pointed through. Otherwise return this. 4334 4335Expression* 4336Expression::deref() 4337{ 4338 if (this->classification_ == EXPRESSION_UNARY) 4339 { 4340 Unary_expression* ue = static_cast<Unary_expression*>(this); 4341 if (ue->op() == OPERATOR_MULT) 4342 return ue->operand(); 4343 } 4344 return this; 4345} 4346 4347// Class Binary_expression. 4348 4349// Traversal. 4350 4351int 4352Binary_expression::do_traverse(Traverse* traverse) 4353{ 4354 int t = Expression::traverse(&this->left_, traverse); 4355 if (t == TRAVERSE_EXIT) 4356 return TRAVERSE_EXIT; 4357 return Expression::traverse(&this->right_, traverse); 4358} 4359 4360// Return the type to use for a binary operation on operands of 4361// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as 4362// such may be NULL or abstract. 4363 4364bool 4365Binary_expression::operation_type(Operator op, Type* left_type, 4366 Type* right_type, Type** result_type) 4367{ 4368 if (left_type != right_type 4369 && !left_type->is_abstract() 4370 && !right_type->is_abstract() 4371 && left_type->base() != right_type->base() 4372 && op != OPERATOR_LSHIFT 4373 && op != OPERATOR_RSHIFT) 4374 { 4375 // May be a type error--let it be diagnosed elsewhere. 4376 return false; 4377 } 4378 4379 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) 4380 { 4381 if (left_type->integer_type() != NULL) 4382 *result_type = left_type; 4383 else 4384 *result_type = Type::make_abstract_integer_type(); 4385 } 4386 else if (!left_type->is_abstract() && left_type->named_type() != NULL) 4387 *result_type = left_type; 4388 else if (!right_type->is_abstract() && right_type->named_type() != NULL) 4389 *result_type = right_type; 4390 else if (!left_type->is_abstract()) 4391 *result_type = left_type; 4392 else if (!right_type->is_abstract()) 4393 *result_type = right_type; 4394 else if (left_type->complex_type() != NULL) 4395 *result_type = left_type; 4396 else if (right_type->complex_type() != NULL) 4397 *result_type = right_type; 4398 else if (left_type->float_type() != NULL) 4399 *result_type = left_type; 4400 else if (right_type->float_type() != NULL) 4401 *result_type = right_type; 4402 else if (left_type->integer_type() != NULL 4403 && left_type->integer_type()->is_rune()) 4404 *result_type = left_type; 4405 else if (right_type->integer_type() != NULL 4406 && right_type->integer_type()->is_rune()) 4407 *result_type = right_type; 4408 else 4409 *result_type = left_type; 4410 4411 return true; 4412} 4413 4414// Convert an integer comparison code and an operator to a boolean 4415// value. 4416 4417bool 4418Binary_expression::cmp_to_bool(Operator op, int cmp) 4419{ 4420 switch (op) 4421 { 4422 case OPERATOR_EQEQ: 4423 return cmp == 0; 4424 break; 4425 case OPERATOR_NOTEQ: 4426 return cmp != 0; 4427 break; 4428 case OPERATOR_LT: 4429 return cmp < 0; 4430 break; 4431 case OPERATOR_LE: 4432 return cmp <= 0; 4433 case OPERATOR_GT: 4434 return cmp > 0; 4435 case OPERATOR_GE: 4436 return cmp >= 0; 4437 default: 4438 go_unreachable(); 4439 } 4440} 4441 4442// Compare constants according to OP. 4443 4444bool 4445Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, 4446 Numeric_constant* right_nc, 4447 Location location, bool* result) 4448{ 4449 Type* left_type = left_nc->type(); 4450 Type* right_type = right_nc->type(); 4451 4452 Type* type; 4453 if (!Binary_expression::operation_type(op, left_type, right_type, &type)) 4454 return false; 4455 4456 // When comparing an untyped operand to a typed operand, we are 4457 // effectively coercing the untyped operand to the other operand's 4458 // type, so make sure that is valid. 4459 if (!left_nc->set_type(type, true, location) 4460 || !right_nc->set_type(type, true, location)) 4461 return false; 4462 4463 bool ret; 4464 int cmp; 4465 if (type->complex_type() != NULL) 4466 { 4467 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) 4468 return false; 4469 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); 4470 } 4471 else if (type->float_type() != NULL) 4472 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); 4473 else 4474 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); 4475 4476 if (ret) 4477 *result = Binary_expression::cmp_to_bool(op, cmp); 4478 4479 return ret; 4480} 4481 4482// Compare integer constants. 4483 4484bool 4485Binary_expression::compare_integer(const Numeric_constant* left_nc, 4486 const Numeric_constant* right_nc, 4487 int* cmp) 4488{ 4489 mpz_t left_val; 4490 if (!left_nc->to_int(&left_val)) 4491 return false; 4492 mpz_t right_val; 4493 if (!right_nc->to_int(&right_val)) 4494 { 4495 mpz_clear(left_val); 4496 return false; 4497 } 4498 4499 *cmp = mpz_cmp(left_val, right_val); 4500 4501 mpz_clear(left_val); 4502 mpz_clear(right_val); 4503 4504 return true; 4505} 4506 4507// Compare floating point constants. 4508 4509bool 4510Binary_expression::compare_float(const Numeric_constant* left_nc, 4511 const Numeric_constant* right_nc, 4512 int* cmp) 4513{ 4514 mpfr_t left_val; 4515 if (!left_nc->to_float(&left_val)) 4516 return false; 4517 mpfr_t right_val; 4518 if (!right_nc->to_float(&right_val)) 4519 { 4520 mpfr_clear(left_val); 4521 return false; 4522 } 4523 4524 // We already coerced both operands to the same type. If that type 4525 // is not an abstract type, we need to round the values accordingly. 4526 Type* type = left_nc->type(); 4527 if (!type->is_abstract() && type->float_type() != NULL) 4528 { 4529 int bits = type->float_type()->bits(); 4530 mpfr_prec_round(left_val, bits, GMP_RNDN); 4531 mpfr_prec_round(right_val, bits, GMP_RNDN); 4532 } 4533 4534 *cmp = mpfr_cmp(left_val, right_val); 4535 4536 mpfr_clear(left_val); 4537 mpfr_clear(right_val); 4538 4539 return true; 4540} 4541 4542// Compare complex constants. Complex numbers may only be compared 4543// for equality. 4544 4545bool 4546Binary_expression::compare_complex(const Numeric_constant* left_nc, 4547 const Numeric_constant* right_nc, 4548 int* cmp) 4549{ 4550 mpc_t left_val; 4551 if (!left_nc->to_complex(&left_val)) 4552 return false; 4553 mpc_t right_val; 4554 if (!right_nc->to_complex(&right_val)) 4555 { 4556 mpc_clear(left_val); 4557 return false; 4558 } 4559 4560 // We already coerced both operands to the same type. If that type 4561 // is not an abstract type, we need to round the values accordingly. 4562 Type* type = left_nc->type(); 4563 if (!type->is_abstract() && type->complex_type() != NULL) 4564 { 4565 int bits = type->complex_type()->bits(); 4566 mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); 4567 mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); 4568 mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); 4569 mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); 4570 } 4571 4572 *cmp = mpc_cmp(left_val, right_val) != 0; 4573 4574 mpc_clear(left_val); 4575 mpc_clear(right_val); 4576 4577 return true; 4578} 4579 4580// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return 4581// true if this could be done, false if not. Issue errors at LOCATION 4582// as appropriate. 4583 4584bool 4585Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, 4586 Numeric_constant* right_nc, 4587 Location location, Numeric_constant* nc) 4588{ 4589 switch (op) 4590 { 4591 case OPERATOR_OROR: 4592 case OPERATOR_ANDAND: 4593 case OPERATOR_EQEQ: 4594 case OPERATOR_NOTEQ: 4595 case OPERATOR_LT: 4596 case OPERATOR_LE: 4597 case OPERATOR_GT: 4598 case OPERATOR_GE: 4599 // These return boolean values, not numeric. 4600 return false; 4601 default: 4602 break; 4603 } 4604 4605 Type* left_type = left_nc->type(); 4606 Type* right_type = right_nc->type(); 4607 4608 Type* type; 4609 if (!Binary_expression::operation_type(op, left_type, right_type, &type)) 4610 return false; 4611 4612 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; 4613 4614 // When combining an untyped operand with a typed operand, we are 4615 // effectively coercing the untyped operand to the other operand's 4616 // type, so make sure that is valid. 4617 if (!left_nc->set_type(type, true, location)) 4618 return false; 4619 if (!is_shift && !right_nc->set_type(type, true, location)) 4620 return false; 4621 4622 bool r; 4623 if (type->complex_type() != NULL) 4624 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); 4625 else if (type->float_type() != NULL) 4626 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); 4627 else 4628 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); 4629 4630 if (r) 4631 r = nc->set_type(type, true, location); 4632 4633 return r; 4634} 4635 4636// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using 4637// integer operations. Return true if this could be done, false if 4638// not. 4639 4640bool 4641Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, 4642 const Numeric_constant* right_nc, 4643 Location location, Numeric_constant* nc) 4644{ 4645 mpz_t left_val; 4646 if (!left_nc->to_int(&left_val)) 4647 return false; 4648 mpz_t right_val; 4649 if (!right_nc->to_int(&right_val)) 4650 { 4651 mpz_clear(left_val); 4652 return false; 4653 } 4654 4655 mpz_t val; 4656 mpz_init(val); 4657 4658 switch (op) 4659 { 4660 case OPERATOR_PLUS: 4661 mpz_add(val, left_val, right_val); 4662 if (mpz_sizeinbase(val, 2) > 0x100000) 4663 { 4664 error_at(location, "constant addition overflow"); 4665 mpz_set_ui(val, 1); 4666 } 4667 break; 4668 case OPERATOR_MINUS: 4669 mpz_sub(val, left_val, right_val); 4670 if (mpz_sizeinbase(val, 2) > 0x100000) 4671 { 4672 error_at(location, "constant subtraction overflow"); 4673 mpz_set_ui(val, 1); 4674 } 4675 break; 4676 case OPERATOR_OR: 4677 mpz_ior(val, left_val, right_val); 4678 break; 4679 case OPERATOR_XOR: 4680 mpz_xor(val, left_val, right_val); 4681 break; 4682 case OPERATOR_MULT: 4683 mpz_mul(val, left_val, right_val); 4684 if (mpz_sizeinbase(val, 2) > 0x100000) 4685 { 4686 error_at(location, "constant multiplication overflow"); 4687 mpz_set_ui(val, 1); 4688 } 4689 break; 4690 case OPERATOR_DIV: 4691 if (mpz_sgn(right_val) != 0) 4692 mpz_tdiv_q(val, left_val, right_val); 4693 else 4694 { 4695 error_at(location, "division by zero"); 4696 mpz_set_ui(val, 0); 4697 } 4698 break; 4699 case OPERATOR_MOD: 4700 if (mpz_sgn(right_val) != 0) 4701 mpz_tdiv_r(val, left_val, right_val); 4702 else 4703 { 4704 error_at(location, "division by zero"); 4705 mpz_set_ui(val, 0); 4706 } 4707 break; 4708 case OPERATOR_LSHIFT: 4709 { 4710 unsigned long shift = mpz_get_ui(right_val); 4711 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) 4712 mpz_mul_2exp(val, left_val, shift); 4713 else 4714 { 4715 error_at(location, "shift count overflow"); 4716 mpz_set_ui(val, 1); 4717 } 4718 break; 4719 } 4720 break; 4721 case OPERATOR_RSHIFT: 4722 { 4723 unsigned long shift = mpz_get_ui(right_val); 4724 if (mpz_cmp_ui(right_val, shift) != 0) 4725 { 4726 error_at(location, "shift count overflow"); 4727 mpz_set_ui(val, 1); 4728 } 4729 else 4730 { 4731 if (mpz_cmp_ui(left_val, 0) >= 0) 4732 mpz_tdiv_q_2exp(val, left_val, shift); 4733 else 4734 mpz_fdiv_q_2exp(val, left_val, shift); 4735 } 4736 break; 4737 } 4738 break; 4739 case OPERATOR_AND: 4740 mpz_and(val, left_val, right_val); 4741 break; 4742 case OPERATOR_BITCLEAR: 4743 { 4744 mpz_t tval; 4745 mpz_init(tval); 4746 mpz_com(tval, right_val); 4747 mpz_and(val, left_val, tval); 4748 mpz_clear(tval); 4749 } 4750 break; 4751 default: 4752 go_unreachable(); 4753 } 4754 4755 mpz_clear(left_val); 4756 mpz_clear(right_val); 4757 4758 if (left_nc->is_rune() 4759 || (op != OPERATOR_LSHIFT 4760 && op != OPERATOR_RSHIFT 4761 && right_nc->is_rune())) 4762 nc->set_rune(NULL, val); 4763 else 4764 nc->set_int(NULL, val); 4765 4766 mpz_clear(val); 4767 4768 return true; 4769} 4770 4771// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using 4772// floating point operations. Return true if this could be done, 4773// false if not. 4774 4775bool 4776Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, 4777 const Numeric_constant* right_nc, 4778 Location location, Numeric_constant* nc) 4779{ 4780 mpfr_t left_val; 4781 if (!left_nc->to_float(&left_val)) 4782 return false; 4783 mpfr_t right_val; 4784 if (!right_nc->to_float(&right_val)) 4785 { 4786 mpfr_clear(left_val); 4787 return false; 4788 } 4789 4790 mpfr_t val; 4791 mpfr_init(val); 4792 4793 bool ret = true; 4794 switch (op) 4795 { 4796 case OPERATOR_PLUS: 4797 mpfr_add(val, left_val, right_val, GMP_RNDN); 4798 break; 4799 case OPERATOR_MINUS: 4800 mpfr_sub(val, left_val, right_val, GMP_RNDN); 4801 break; 4802 case OPERATOR_OR: 4803 case OPERATOR_XOR: 4804 case OPERATOR_AND: 4805 case OPERATOR_BITCLEAR: 4806 case OPERATOR_MOD: 4807 case OPERATOR_LSHIFT: 4808 case OPERATOR_RSHIFT: 4809 mpfr_set_ui(val, 0, GMP_RNDN); 4810 ret = false; 4811 break; 4812 case OPERATOR_MULT: 4813 mpfr_mul(val, left_val, right_val, GMP_RNDN); 4814 break; 4815 case OPERATOR_DIV: 4816 if (!mpfr_zero_p(right_val)) 4817 mpfr_div(val, left_val, right_val, GMP_RNDN); 4818 else 4819 { 4820 error_at(location, "division by zero"); 4821 mpfr_set_ui(val, 0, GMP_RNDN); 4822 } 4823 break; 4824 default: 4825 go_unreachable(); 4826 } 4827 4828 mpfr_clear(left_val); 4829 mpfr_clear(right_val); 4830 4831 nc->set_float(NULL, val); 4832 mpfr_clear(val); 4833 4834 return ret; 4835} 4836 4837// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using 4838// complex operations. Return true if this could be done, false if 4839// not. 4840 4841bool 4842Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, 4843 const Numeric_constant* right_nc, 4844 Location location, Numeric_constant* nc) 4845{ 4846 mpc_t left_val; 4847 if (!left_nc->to_complex(&left_val)) 4848 return false; 4849 mpc_t right_val; 4850 if (!right_nc->to_complex(&right_val)) 4851 { 4852 mpc_clear(left_val); 4853 return false; 4854 } 4855 4856 mpc_t val; 4857 mpc_init2(val, mpc_precision); 4858 4859 bool ret = true; 4860 switch (op) 4861 { 4862 case OPERATOR_PLUS: 4863 mpc_add(val, left_val, right_val, MPC_RNDNN); 4864 break; 4865 case OPERATOR_MINUS: 4866 mpc_sub(val, left_val, right_val, MPC_RNDNN); 4867 break; 4868 case OPERATOR_OR: 4869 case OPERATOR_XOR: 4870 case OPERATOR_AND: 4871 case OPERATOR_BITCLEAR: 4872 case OPERATOR_MOD: 4873 case OPERATOR_LSHIFT: 4874 case OPERATOR_RSHIFT: 4875 mpc_set_ui(val, 0, MPC_RNDNN); 4876 ret = false; 4877 break; 4878 case OPERATOR_MULT: 4879 mpc_mul(val, left_val, right_val, MPC_RNDNN); 4880 break; 4881 case OPERATOR_DIV: 4882 if (mpc_cmp_si(right_val, 0) == 0) 4883 { 4884 error_at(location, "division by zero"); 4885 mpc_set_ui(val, 0, MPC_RNDNN); 4886 break; 4887 } 4888 mpc_div(val, left_val, right_val, MPC_RNDNN); 4889 break; 4890 default: 4891 go_unreachable(); 4892 } 4893 4894 mpc_clear(left_val); 4895 mpc_clear(right_val); 4896 4897 nc->set_complex(NULL, val); 4898 mpc_clear(val); 4899 4900 return ret; 4901} 4902 4903// Lower a binary expression. We have to evaluate constant 4904// expressions now, in order to implement Go's unlimited precision 4905// constants. 4906 4907Expression* 4908Binary_expression::do_lower(Gogo* gogo, Named_object*, 4909 Statement_inserter* inserter, int) 4910{ 4911 Location location = this->location(); 4912 Operator op = this->op_; 4913 Expression* left = this->left_; 4914 Expression* right = this->right_; 4915 4916 const bool is_comparison = (op == OPERATOR_EQEQ 4917 || op == OPERATOR_NOTEQ 4918 || op == OPERATOR_LT 4919 || op == OPERATOR_LE 4920 || op == OPERATOR_GT 4921 || op == OPERATOR_GE); 4922 4923 // Numeric constant expressions. 4924 { 4925 Numeric_constant left_nc; 4926 Numeric_constant right_nc; 4927 if (left->numeric_constant_value(&left_nc) 4928 && right->numeric_constant_value(&right_nc)) 4929 { 4930 if (is_comparison) 4931 { 4932 bool result; 4933 if (!Binary_expression::compare_constant(op, &left_nc, 4934 &right_nc, location, 4935 &result)) 4936 return this; 4937 return Expression::make_cast(Type::make_boolean_type(), 4938 Expression::make_boolean(result, 4939 location), 4940 location); 4941 } 4942 else 4943 { 4944 Numeric_constant nc; 4945 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, 4946 location, &nc)) 4947 return this; 4948 return nc.expression(location); 4949 } 4950 } 4951 } 4952 4953 // String constant expressions. 4954 if (left->type()->is_string_type() && right->type()->is_string_type()) 4955 { 4956 std::string left_string; 4957 std::string right_string; 4958 if (left->string_constant_value(&left_string) 4959 && right->string_constant_value(&right_string)) 4960 { 4961 if (op == OPERATOR_PLUS) 4962 return Expression::make_string(left_string + right_string, 4963 location); 4964 else if (is_comparison) 4965 { 4966 int cmp = left_string.compare(right_string); 4967 bool r = Binary_expression::cmp_to_bool(op, cmp); 4968 return Expression::make_boolean(r, location); 4969 } 4970 } 4971 } 4972 4973 // Lower struct, array, and some interface comparisons. 4974 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) 4975 { 4976 if (left->type()->struct_type() != NULL 4977 && right->type()->struct_type() != NULL) 4978 return this->lower_struct_comparison(gogo, inserter); 4979 else if (left->type()->array_type() != NULL 4980 && !left->type()->is_slice_type() 4981 && right->type()->array_type() != NULL 4982 && !right->type()->is_slice_type()) 4983 return this->lower_array_comparison(gogo, inserter); 4984 else if ((left->type()->interface_type() != NULL 4985 && right->type()->interface_type() == NULL) 4986 || (left->type()->interface_type() == NULL 4987 && right->type()->interface_type() != NULL)) 4988 return this->lower_interface_value_comparison(gogo, inserter); 4989 } 4990 4991 return this; 4992} 4993 4994// Lower a struct comparison. 4995 4996Expression* 4997Binary_expression::lower_struct_comparison(Gogo* gogo, 4998 Statement_inserter* inserter) 4999{ 5000 Struct_type* st = this->left_->type()->struct_type(); 5001 Struct_type* st2 = this->right_->type()->struct_type(); 5002 if (st2 == NULL) 5003 return this; 5004 if (st != st2 && !Type::are_identical(st, st2, false, NULL)) 5005 return this; 5006 if (!Type::are_compatible_for_comparison(true, this->left_->type(), 5007 this->right_->type(), NULL)) 5008 return this; 5009 5010 // See if we can compare using memcmp. As a heuristic, we use 5011 // memcmp rather than field references and comparisons if there are 5012 // more than two fields. 5013 if (st->compare_is_identity(gogo) && st->total_field_count() > 2) 5014 return this->lower_compare_to_memcmp(gogo, inserter); 5015 5016 Location loc = this->location(); 5017 5018 Expression* left = this->left_; 5019 Temporary_statement* left_temp = NULL; 5020 if (left->var_expression() == NULL 5021 && left->temporary_reference_expression() == NULL) 5022 { 5023 left_temp = Statement::make_temporary(left->type(), NULL, loc); 5024 inserter->insert(left_temp); 5025 left = Expression::make_set_and_use_temporary(left_temp, left, loc); 5026 } 5027 5028 Expression* right = this->right_; 5029 Temporary_statement* right_temp = NULL; 5030 if (right->var_expression() == NULL 5031 && right->temporary_reference_expression() == NULL) 5032 { 5033 right_temp = Statement::make_temporary(right->type(), NULL, loc); 5034 inserter->insert(right_temp); 5035 right = Expression::make_set_and_use_temporary(right_temp, right, loc); 5036 } 5037 5038 Expression* ret = Expression::make_boolean(true, loc); 5039 const Struct_field_list* fields = st->fields(); 5040 unsigned int field_index = 0; 5041 for (Struct_field_list::const_iterator pf = fields->begin(); 5042 pf != fields->end(); 5043 ++pf, ++field_index) 5044 { 5045 if (Gogo::is_sink_name(pf->field_name())) 5046 continue; 5047 5048 if (field_index > 0) 5049 { 5050 if (left_temp == NULL) 5051 left = left->copy(); 5052 else 5053 left = Expression::make_temporary_reference(left_temp, loc); 5054 if (right_temp == NULL) 5055 right = right->copy(); 5056 else 5057 right = Expression::make_temporary_reference(right_temp, loc); 5058 } 5059 Expression* f1 = Expression::make_field_reference(left, field_index, 5060 loc); 5061 Expression* f2 = Expression::make_field_reference(right, field_index, 5062 loc); 5063 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); 5064 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); 5065 } 5066 5067 if (this->op_ == OPERATOR_NOTEQ) 5068 ret = Expression::make_unary(OPERATOR_NOT, ret, loc); 5069 5070 return ret; 5071} 5072 5073// Lower an array comparison. 5074 5075Expression* 5076Binary_expression::lower_array_comparison(Gogo* gogo, 5077 Statement_inserter* inserter) 5078{ 5079 Array_type* at = this->left_->type()->array_type(); 5080 Array_type* at2 = this->right_->type()->array_type(); 5081 if (at2 == NULL) 5082 return this; 5083 if (at != at2 && !Type::are_identical(at, at2, false, NULL)) 5084 return this; 5085 if (!Type::are_compatible_for_comparison(true, this->left_->type(), 5086 this->right_->type(), NULL)) 5087 return this; 5088 5089 // Call memcmp directly if possible. This may let the middle-end 5090 // optimize the call. 5091 if (at->compare_is_identity(gogo)) 5092 return this->lower_compare_to_memcmp(gogo, inserter); 5093 5094 // Call the array comparison function. 5095 Named_object* hash_fn; 5096 Named_object* equal_fn; 5097 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, 5098 &hash_fn, &equal_fn); 5099 5100 Location loc = this->location(); 5101 5102 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); 5103 5104 Expression_list* args = new Expression_list(); 5105 args->push_back(this->operand_address(inserter, this->left_)); 5106 args->push_back(this->operand_address(inserter, this->right_)); 5107 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); 5108 5109 Expression* ret = Expression::make_call(func, args, false, loc); 5110 5111 if (this->op_ == OPERATOR_NOTEQ) 5112 ret = Expression::make_unary(OPERATOR_NOT, ret, loc); 5113 5114 return ret; 5115} 5116 5117// Lower an interface to value comparison. 5118 5119Expression* 5120Binary_expression::lower_interface_value_comparison(Gogo*, 5121 Statement_inserter* inserter) 5122{ 5123 Type* left_type = this->left_->type(); 5124 Type* right_type = this->right_->type(); 5125 Interface_type* ift; 5126 if (left_type->interface_type() != NULL) 5127 { 5128 ift = left_type->interface_type(); 5129 if (!ift->implements_interface(right_type, NULL)) 5130 return this; 5131 } 5132 else 5133 { 5134 ift = right_type->interface_type(); 5135 if (!ift->implements_interface(left_type, NULL)) 5136 return this; 5137 } 5138 if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) 5139 return this; 5140 5141 Location loc = this->location(); 5142 5143 if (left_type->interface_type() == NULL 5144 && left_type->points_to() == NULL 5145 && !this->left_->is_addressable()) 5146 { 5147 Temporary_statement* temp = 5148 Statement::make_temporary(left_type, NULL, loc); 5149 inserter->insert(temp); 5150 this->left_ = 5151 Expression::make_set_and_use_temporary(temp, this->left_, loc); 5152 } 5153 5154 if (right_type->interface_type() == NULL 5155 && right_type->points_to() == NULL 5156 && !this->right_->is_addressable()) 5157 { 5158 Temporary_statement* temp = 5159 Statement::make_temporary(right_type, NULL, loc); 5160 inserter->insert(temp); 5161 this->right_ = 5162 Expression::make_set_and_use_temporary(temp, this->right_, loc); 5163 } 5164 5165 return this; 5166} 5167 5168// Lower a struct or array comparison to a call to memcmp. 5169 5170Expression* 5171Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) 5172{ 5173 Location loc = this->location(); 5174 5175 Expression* a1 = this->operand_address(inserter, this->left_); 5176 Expression* a2 = this->operand_address(inserter, this->right_); 5177 Expression* len = Expression::make_type_info(this->left_->type(), 5178 TYPE_INFO_SIZE); 5179 5180 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); 5181 Expression* zero = Expression::make_integer_ul(0, NULL, loc); 5182 return Expression::make_binary(this->op_, call, zero, loc); 5183} 5184 5185Expression* 5186Binary_expression::do_flatten(Gogo* gogo, Named_object*, 5187 Statement_inserter* inserter) 5188{ 5189 if (this->classification() == EXPRESSION_ERROR) 5190 return this; 5191 5192 Location loc = this->location(); 5193 Temporary_statement* temp; 5194 if (this->left_->type()->is_string_type() 5195 && this->op_ == OPERATOR_PLUS) 5196 { 5197 if (!this->left_->is_variable() 5198 && !this->left_->is_constant()) 5199 { 5200 temp = Statement::make_temporary(NULL, this->left_, loc); 5201 inserter->insert(temp); 5202 this->left_ = Expression::make_temporary_reference(temp, loc); 5203 } 5204 if (!this->right_->is_variable() 5205 && !this->right_->is_constant()) 5206 { 5207 temp = 5208 Statement::make_temporary(this->left_->type(), this->right_, loc); 5209 this->right_ = Expression::make_temporary_reference(temp, loc); 5210 inserter->insert(temp); 5211 } 5212 } 5213 5214 Type* left_type = this->left_->type(); 5215 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT 5216 || this->op_ == OPERATOR_RSHIFT); 5217 bool is_idiv_op = ((this->op_ == OPERATOR_DIV && 5218 left_type->integer_type() != NULL) 5219 || this->op_ == OPERATOR_MOD); 5220 5221 if (is_shift_op 5222 || (is_idiv_op 5223 && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) 5224 { 5225 if (!this->left_->is_variable()) 5226 { 5227 temp = Statement::make_temporary(NULL, this->left_, loc); 5228 inserter->insert(temp); 5229 this->left_ = Expression::make_temporary_reference(temp, loc); 5230 } 5231 if (!this->right_->is_variable()) 5232 { 5233 temp = 5234 Statement::make_temporary(NULL, this->right_, loc); 5235 this->right_ = Expression::make_temporary_reference(temp, loc); 5236 inserter->insert(temp); 5237 } 5238 } 5239 return this; 5240} 5241 5242 5243// Return the address of EXPR, cast to unsafe.Pointer. 5244 5245Expression* 5246Binary_expression::operand_address(Statement_inserter* inserter, 5247 Expression* expr) 5248{ 5249 Location loc = this->location(); 5250 5251 if (!expr->is_addressable()) 5252 { 5253 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, 5254 loc); 5255 inserter->insert(temp); 5256 expr = Expression::make_set_and_use_temporary(temp, expr, loc); 5257 } 5258 expr = Expression::make_unary(OPERATOR_AND, expr, loc); 5259 static_cast<Unary_expression*>(expr)->set_does_not_escape(); 5260 Type* void_type = Type::make_void_type(); 5261 Type* unsafe_pointer_type = Type::make_pointer_type(void_type); 5262 return Expression::make_cast(unsafe_pointer_type, expr, loc); 5263} 5264 5265// Return the numeric constant value, if it has one. 5266 5267bool 5268Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const 5269{ 5270 Numeric_constant left_nc; 5271 if (!this->left_->numeric_constant_value(&left_nc)) 5272 return false; 5273 Numeric_constant right_nc; 5274 if (!this->right_->numeric_constant_value(&right_nc)) 5275 return false; 5276 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, 5277 this->location(), nc); 5278} 5279 5280// Note that the value is being discarded. 5281 5282bool 5283Binary_expression::do_discarding_value() 5284{ 5285 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) 5286 return this->right_->discarding_value(); 5287 else 5288 { 5289 this->unused_value_error(); 5290 return false; 5291 } 5292} 5293 5294// Get type. 5295 5296Type* 5297Binary_expression::do_type() 5298{ 5299 if (this->classification() == EXPRESSION_ERROR) 5300 return Type::make_error_type(); 5301 5302 switch (this->op_) 5303 { 5304 case OPERATOR_EQEQ: 5305 case OPERATOR_NOTEQ: 5306 case OPERATOR_LT: 5307 case OPERATOR_LE: 5308 case OPERATOR_GT: 5309 case OPERATOR_GE: 5310 if (this->type_ == NULL) 5311 this->type_ = Type::make_boolean_type(); 5312 return this->type_; 5313 5314 case OPERATOR_PLUS: 5315 case OPERATOR_MINUS: 5316 case OPERATOR_OR: 5317 case OPERATOR_XOR: 5318 case OPERATOR_MULT: 5319 case OPERATOR_DIV: 5320 case OPERATOR_MOD: 5321 case OPERATOR_AND: 5322 case OPERATOR_BITCLEAR: 5323 case OPERATOR_OROR: 5324 case OPERATOR_ANDAND: 5325 { 5326 Type* type; 5327 if (!Binary_expression::operation_type(this->op_, 5328 this->left_->type(), 5329 this->right_->type(), 5330 &type)) 5331 return Type::make_error_type(); 5332 return type; 5333 } 5334 5335 case OPERATOR_LSHIFT: 5336 case OPERATOR_RSHIFT: 5337 return this->left_->type(); 5338 5339 default: 5340 go_unreachable(); 5341 } 5342} 5343 5344// Set type for a binary expression. 5345 5346void 5347Binary_expression::do_determine_type(const Type_context* context) 5348{ 5349 Type* tleft = this->left_->type(); 5350 Type* tright = this->right_->type(); 5351 5352 // Both sides should have the same type, except for the shift 5353 // operations. For a comparison, we should ignore the incoming 5354 // type. 5355 5356 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT 5357 || this->op_ == OPERATOR_RSHIFT); 5358 5359 bool is_comparison = (this->op_ == OPERATOR_EQEQ 5360 || this->op_ == OPERATOR_NOTEQ 5361 || this->op_ == OPERATOR_LT 5362 || this->op_ == OPERATOR_LE 5363 || this->op_ == OPERATOR_GT 5364 || this->op_ == OPERATOR_GE); 5365 5366 Type_context subcontext(*context); 5367 5368 if (is_comparison) 5369 { 5370 // In a comparison, the context does not determine the types of 5371 // the operands. 5372 subcontext.type = NULL; 5373 } 5374 5375 // Set the context for the left hand operand. 5376 if (is_shift_op) 5377 { 5378 // The right hand operand of a shift plays no role in 5379 // determining the type of the left hand operand. 5380 } 5381 else if (!tleft->is_abstract()) 5382 subcontext.type = tleft; 5383 else if (!tright->is_abstract()) 5384 subcontext.type = tright; 5385 else if (subcontext.type == NULL) 5386 { 5387 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) 5388 || (tleft->float_type() != NULL && tright->float_type() != NULL) 5389 || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) 5390 { 5391 // Both sides have an abstract integer, abstract float, or 5392 // abstract complex type. Just let CONTEXT determine 5393 // whether they may remain abstract or not. 5394 } 5395 else if (tleft->complex_type() != NULL) 5396 subcontext.type = tleft; 5397 else if (tright->complex_type() != NULL) 5398 subcontext.type = tright; 5399 else if (tleft->float_type() != NULL) 5400 subcontext.type = tleft; 5401 else if (tright->float_type() != NULL) 5402 subcontext.type = tright; 5403 else 5404 subcontext.type = tleft; 5405 5406 if (subcontext.type != NULL && !context->may_be_abstract) 5407 subcontext.type = subcontext.type->make_non_abstract_type(); 5408 } 5409 5410 this->left_->determine_type(&subcontext); 5411 5412 if (is_shift_op) 5413 { 5414 // We may have inherited an unusable type for the shift operand. 5415 // Give a useful error if that happened. 5416 if (tleft->is_abstract() 5417 && subcontext.type != NULL 5418 && !subcontext.may_be_abstract 5419 && subcontext.type->interface_type() == NULL 5420 && subcontext.type->integer_type() == NULL) 5421 this->report_error(("invalid context-determined non-integer type " 5422 "for left operand of shift")); 5423 5424 // The context for the right hand operand is the same as for the 5425 // left hand operand, except for a shift operator. 5426 subcontext.type = Type::lookup_integer_type("uint"); 5427 subcontext.may_be_abstract = false; 5428 } 5429 5430 this->right_->determine_type(&subcontext); 5431 5432 if (is_comparison) 5433 { 5434 if (this->type_ != NULL && !this->type_->is_abstract()) 5435 ; 5436 else if (context->type != NULL && context->type->is_boolean_type()) 5437 this->type_ = context->type; 5438 else if (!context->may_be_abstract) 5439 this->type_ = Type::lookup_bool_type(); 5440 } 5441} 5442 5443// Report an error if the binary operator OP does not support TYPE. 5444// OTYPE is the type of the other operand. Return whether the 5445// operation is OK. This should not be used for shift. 5446 5447bool 5448Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, 5449 Location location) 5450{ 5451 switch (op) 5452 { 5453 case OPERATOR_OROR: 5454 case OPERATOR_ANDAND: 5455 if (!type->is_boolean_type()) 5456 { 5457 error_at(location, "expected boolean type"); 5458 return false; 5459 } 5460 break; 5461 5462 case OPERATOR_EQEQ: 5463 case OPERATOR_NOTEQ: 5464 { 5465 std::string reason; 5466 if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) 5467 { 5468 error_at(location, "%s", reason.c_str()); 5469 return false; 5470 } 5471 } 5472 break; 5473 5474 case OPERATOR_LT: 5475 case OPERATOR_LE: 5476 case OPERATOR_GT: 5477 case OPERATOR_GE: 5478 { 5479 std::string reason; 5480 if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) 5481 { 5482 error_at(location, "%s", reason.c_str()); 5483 return false; 5484 } 5485 } 5486 break; 5487 5488 case OPERATOR_PLUS: 5489 case OPERATOR_PLUSEQ: 5490 if (type->integer_type() == NULL 5491 && type->float_type() == NULL 5492 && type->complex_type() == NULL 5493 && !type->is_string_type()) 5494 { 5495 error_at(location, 5496 "expected integer, floating, complex, or string type"); 5497 return false; 5498 } 5499 break; 5500 5501 case OPERATOR_MINUS: 5502 case OPERATOR_MINUSEQ: 5503 case OPERATOR_MULT: 5504 case OPERATOR_MULTEQ: 5505 case OPERATOR_DIV: 5506 case OPERATOR_DIVEQ: 5507 if (type->integer_type() == NULL 5508 && type->float_type() == NULL 5509 && type->complex_type() == NULL) 5510 { 5511 error_at(location, "expected integer, floating, or complex type"); 5512 return false; 5513 } 5514 break; 5515 5516 case OPERATOR_MOD: 5517 case OPERATOR_MODEQ: 5518 case OPERATOR_OR: 5519 case OPERATOR_OREQ: 5520 case OPERATOR_AND: 5521 case OPERATOR_ANDEQ: 5522 case OPERATOR_XOR: 5523 case OPERATOR_XOREQ: 5524 case OPERATOR_BITCLEAR: 5525 case OPERATOR_BITCLEAREQ: 5526 if (type->integer_type() == NULL) 5527 { 5528 error_at(location, "expected integer type"); 5529 return false; 5530 } 5531 break; 5532 5533 default: 5534 go_unreachable(); 5535 } 5536 5537 return true; 5538} 5539 5540// Check types. 5541 5542void 5543Binary_expression::do_check_types(Gogo*) 5544{ 5545 if (this->classification() == EXPRESSION_ERROR) 5546 return; 5547 5548 Type* left_type = this->left_->type(); 5549 Type* right_type = this->right_->type(); 5550 if (left_type->is_error() || right_type->is_error()) 5551 { 5552 this->set_is_error(); 5553 return; 5554 } 5555 5556 if (this->op_ == OPERATOR_EQEQ 5557 || this->op_ == OPERATOR_NOTEQ 5558 || this->op_ == OPERATOR_LT 5559 || this->op_ == OPERATOR_LE 5560 || this->op_ == OPERATOR_GT 5561 || this->op_ == OPERATOR_GE) 5562 { 5563 if (left_type->is_nil_type() && right_type->is_nil_type()) 5564 { 5565 this->report_error(_("invalid comparison of nil with nil")); 5566 return; 5567 } 5568 if (!Type::are_assignable(left_type, right_type, NULL) 5569 && !Type::are_assignable(right_type, left_type, NULL)) 5570 { 5571 this->report_error(_("incompatible types in binary expression")); 5572 return; 5573 } 5574 if (!Binary_expression::check_operator_type(this->op_, left_type, 5575 right_type, 5576 this->location()) 5577 || !Binary_expression::check_operator_type(this->op_, right_type, 5578 left_type, 5579 this->location())) 5580 { 5581 this->set_is_error(); 5582 return; 5583 } 5584 } 5585 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) 5586 { 5587 if (!Type::are_compatible_for_binop(left_type, right_type)) 5588 { 5589 this->report_error(_("incompatible types in binary expression")); 5590 return; 5591 } 5592 if (!Binary_expression::check_operator_type(this->op_, left_type, 5593 right_type, 5594 this->location())) 5595 { 5596 this->set_is_error(); 5597 return; 5598 } 5599 if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) 5600 { 5601 // Division by a zero integer constant is an error. 5602 Numeric_constant rconst; 5603 unsigned long rval; 5604 if (left_type->integer_type() != NULL 5605 && this->right_->numeric_constant_value(&rconst) 5606 && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID 5607 && rval == 0) 5608 { 5609 this->report_error(_("integer division by zero")); 5610 return; 5611 } 5612 } 5613 } 5614 else 5615 { 5616 if (left_type->integer_type() == NULL) 5617 this->report_error(_("shift of non-integer operand")); 5618 5619 if (!right_type->is_abstract() 5620 && (right_type->integer_type() == NULL 5621 || !right_type->integer_type()->is_unsigned())) 5622 this->report_error(_("shift count not unsigned integer")); 5623 else 5624 { 5625 Numeric_constant nc; 5626 if (this->right_->numeric_constant_value(&nc)) 5627 { 5628 mpz_t val; 5629 if (!nc.to_int(&val)) 5630 this->report_error(_("shift count not unsigned integer")); 5631 else 5632 { 5633 if (mpz_sgn(val) < 0) 5634 { 5635 this->report_error(_("negative shift count")); 5636 Location rloc = this->right_->location(); 5637 this->right_ = Expression::make_integer_ul(0, right_type, 5638 rloc); 5639 } 5640 mpz_clear(val); 5641 } 5642 } 5643 } 5644 } 5645} 5646 5647// Get the backend representation for a binary expression. 5648 5649Bexpression* 5650Binary_expression::do_get_backend(Translate_context* context) 5651{ 5652 Gogo* gogo = context->gogo(); 5653 Location loc = this->location(); 5654 Type* left_type = this->left_->type(); 5655 Type* right_type = this->right_->type(); 5656 5657 bool use_left_type = true; 5658 bool is_shift_op = false; 5659 bool is_idiv_op = false; 5660 switch (this->op_) 5661 { 5662 case OPERATOR_EQEQ: 5663 case OPERATOR_NOTEQ: 5664 case OPERATOR_LT: 5665 case OPERATOR_LE: 5666 case OPERATOR_GT: 5667 case OPERATOR_GE: 5668 return Expression::comparison(context, this->type_, this->op_, 5669 this->left_, this->right_, loc); 5670 5671 case OPERATOR_OROR: 5672 case OPERATOR_ANDAND: 5673 use_left_type = false; 5674 break; 5675 case OPERATOR_PLUS: 5676 case OPERATOR_MINUS: 5677 case OPERATOR_OR: 5678 case OPERATOR_XOR: 5679 case OPERATOR_MULT: 5680 break; 5681 case OPERATOR_DIV: 5682 if (left_type->float_type() != NULL || left_type->complex_type() != NULL) 5683 break; 5684 case OPERATOR_MOD: 5685 is_idiv_op = true; 5686 break; 5687 case OPERATOR_LSHIFT: 5688 case OPERATOR_RSHIFT: 5689 is_shift_op = true; 5690 break; 5691 case OPERATOR_BITCLEAR: 5692 this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); 5693 case OPERATOR_AND: 5694 break; 5695 default: 5696 go_unreachable(); 5697 } 5698 5699 if (left_type->is_string_type()) 5700 { 5701 go_assert(this->op_ == OPERATOR_PLUS); 5702 Expression* string_plus = 5703 Runtime::make_call(Runtime::STRING_PLUS, loc, 2, 5704 this->left_, this->right_); 5705 return string_plus->get_backend(context); 5706 } 5707 5708 // For complex division Go might want slightly different results than the 5709 // backend implementation provides, so we have our own runtime routine. 5710 if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) 5711 { 5712 Runtime::Function complex_code; 5713 switch (this->left_->type()->complex_type()->bits()) 5714 { 5715 case 64: 5716 complex_code = Runtime::COMPLEX64_DIV; 5717 break; 5718 case 128: 5719 complex_code = Runtime::COMPLEX128_DIV; 5720 break; 5721 default: 5722 go_unreachable(); 5723 } 5724 Expression* complex_div = 5725 Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); 5726 return complex_div->get_backend(context); 5727 } 5728 5729 Bexpression* left = this->left_->get_backend(context); 5730 Bexpression* right = this->right_->get_backend(context); 5731 5732 Type* type = use_left_type ? left_type : right_type; 5733 Btype* btype = type->get_backend(gogo); 5734 5735 Bexpression* ret = 5736 gogo->backend()->binary_expression(this->op_, left, right, loc); 5737 ret = gogo->backend()->convert_expression(btype, ret, loc); 5738 5739 // Initialize overflow constants. 5740 Bexpression* overflow; 5741 mpz_t zero; 5742 mpz_init_set_ui(zero, 0UL); 5743 mpz_t one; 5744 mpz_init_set_ui(one, 1UL); 5745 mpz_t neg_one; 5746 mpz_init_set_si(neg_one, -1); 5747 5748 Btype* left_btype = left_type->get_backend(gogo); 5749 Btype* right_btype = right_type->get_backend(gogo); 5750 5751 // In Go, a shift larger than the size of the type is well-defined. 5752 // This is not true in C, so we need to insert a conditional. 5753 if (is_shift_op) 5754 { 5755 go_assert(left_type->integer_type() != NULL); 5756 5757 mpz_t bitsval; 5758 int bits = left_type->integer_type()->bits(); 5759 mpz_init_set_ui(bitsval, bits); 5760 Bexpression* bits_expr = 5761 gogo->backend()->integer_constant_expression(right_btype, bitsval); 5762 Bexpression* compare = 5763 gogo->backend()->binary_expression(OPERATOR_LT, 5764 right, bits_expr, loc); 5765 5766 Bexpression* zero_expr = 5767 gogo->backend()->integer_constant_expression(left_btype, zero); 5768 overflow = zero_expr; 5769 if (this->op_ == OPERATOR_RSHIFT 5770 && !left_type->integer_type()->is_unsigned()) 5771 { 5772 Bexpression* neg_expr = 5773 gogo->backend()->binary_expression(OPERATOR_LT, left, 5774 zero_expr, loc); 5775 Bexpression* neg_one_expr = 5776 gogo->backend()->integer_constant_expression(left_btype, neg_one); 5777 overflow = gogo->backend()->conditional_expression(btype, neg_expr, 5778 neg_one_expr, 5779 zero_expr, loc); 5780 } 5781 ret = gogo->backend()->conditional_expression(btype, compare, ret, 5782 overflow, loc); 5783 mpz_clear(bitsval); 5784 } 5785 5786 // Add checks for division by zero and division overflow as needed. 5787 if (is_idiv_op) 5788 { 5789 if (gogo->check_divide_by_zero()) 5790 { 5791 // right == 0 5792 Bexpression* zero_expr = 5793 gogo->backend()->integer_constant_expression(right_btype, zero); 5794 Bexpression* check = 5795 gogo->backend()->binary_expression(OPERATOR_EQEQ, 5796 right, zero_expr, loc); 5797 5798 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) 5799 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; 5800 Bexpression* crash = gogo->runtime_error(errcode, 5801 loc)->get_backend(context); 5802 5803 // right == 0 ? (__go_runtime_error(...), 0) : ret 5804 ret = gogo->backend()->conditional_expression(btype, check, crash, 5805 ret, loc); 5806 } 5807 5808 if (gogo->check_divide_overflow()) 5809 { 5810 // right == -1 5811 // FIXME: It would be nice to say that this test is expected 5812 // to return false. 5813 5814 Bexpression* neg_one_expr = 5815 gogo->backend()->integer_constant_expression(right_btype, neg_one); 5816 Bexpression* check = 5817 gogo->backend()->binary_expression(OPERATOR_EQEQ, 5818 right, neg_one_expr, loc); 5819 5820 Bexpression* zero_expr = 5821 gogo->backend()->integer_constant_expression(btype, zero); 5822 Bexpression* one_expr = 5823 gogo->backend()->integer_constant_expression(btype, one); 5824 5825 if (type->integer_type()->is_unsigned()) 5826 { 5827 // An unsigned -1 is the largest possible number, so 5828 // dividing is always 1 or 0. 5829 5830 Bexpression* cmp = 5831 gogo->backend()->binary_expression(OPERATOR_EQEQ, 5832 left, right, loc); 5833 if (this->op_ == OPERATOR_DIV) 5834 overflow = 5835 gogo->backend()->conditional_expression(btype, cmp, 5836 one_expr, zero_expr, 5837 loc); 5838 else 5839 overflow = 5840 gogo->backend()->conditional_expression(btype, cmp, 5841 zero_expr, left, 5842 loc); 5843 } 5844 else 5845 { 5846 // Computing left / -1 is the same as computing - left, 5847 // which does not overflow since Go sets -fwrapv. 5848 if (this->op_ == OPERATOR_DIV) 5849 { 5850 Expression* negate_expr = 5851 Expression::make_unary(OPERATOR_MINUS, this->left_, loc); 5852 overflow = negate_expr->get_backend(context); 5853 } 5854 else 5855 overflow = zero_expr; 5856 } 5857 overflow = gogo->backend()->convert_expression(btype, overflow, loc); 5858 5859 // right == -1 ? - left : ret 5860 ret = gogo->backend()->conditional_expression(btype, check, overflow, 5861 ret, loc); 5862 } 5863 } 5864 5865 mpz_clear(zero); 5866 mpz_clear(one); 5867 mpz_clear(neg_one); 5868 return ret; 5869} 5870 5871// Export a binary expression. 5872 5873void 5874Binary_expression::do_export(Export* exp) const 5875{ 5876 exp->write_c_string("("); 5877 this->left_->export_expression(exp); 5878 switch (this->op_) 5879 { 5880 case OPERATOR_OROR: 5881 exp->write_c_string(" || "); 5882 break; 5883 case OPERATOR_ANDAND: 5884 exp->write_c_string(" && "); 5885 break; 5886 case OPERATOR_EQEQ: 5887 exp->write_c_string(" == "); 5888 break; 5889 case OPERATOR_NOTEQ: 5890 exp->write_c_string(" != "); 5891 break; 5892 case OPERATOR_LT: 5893 exp->write_c_string(" < "); 5894 break; 5895 case OPERATOR_LE: 5896 exp->write_c_string(" <= "); 5897 break; 5898 case OPERATOR_GT: 5899 exp->write_c_string(" > "); 5900 break; 5901 case OPERATOR_GE: 5902 exp->write_c_string(" >= "); 5903 break; 5904 case OPERATOR_PLUS: 5905 exp->write_c_string(" + "); 5906 break; 5907 case OPERATOR_MINUS: 5908 exp->write_c_string(" - "); 5909 break; 5910 case OPERATOR_OR: 5911 exp->write_c_string(" | "); 5912 break; 5913 case OPERATOR_XOR: 5914 exp->write_c_string(" ^ "); 5915 break; 5916 case OPERATOR_MULT: 5917 exp->write_c_string(" * "); 5918 break; 5919 case OPERATOR_DIV: 5920 exp->write_c_string(" / "); 5921 break; 5922 case OPERATOR_MOD: 5923 exp->write_c_string(" % "); 5924 break; 5925 case OPERATOR_LSHIFT: 5926 exp->write_c_string(" << "); 5927 break; 5928 case OPERATOR_RSHIFT: 5929 exp->write_c_string(" >> "); 5930 break; 5931 case OPERATOR_AND: 5932 exp->write_c_string(" & "); 5933 break; 5934 case OPERATOR_BITCLEAR: 5935 exp->write_c_string(" &^ "); 5936 break; 5937 default: 5938 go_unreachable(); 5939 } 5940 this->right_->export_expression(exp); 5941 exp->write_c_string(")"); 5942} 5943 5944// Import a binary expression. 5945 5946Expression* 5947Binary_expression::do_import(Import* imp) 5948{ 5949 imp->require_c_string("("); 5950 5951 Expression* left = Expression::import_expression(imp); 5952 5953 Operator op; 5954 if (imp->match_c_string(" || ")) 5955 { 5956 op = OPERATOR_OROR; 5957 imp->advance(4); 5958 } 5959 else if (imp->match_c_string(" && ")) 5960 { 5961 op = OPERATOR_ANDAND; 5962 imp->advance(4); 5963 } 5964 else if (imp->match_c_string(" == ")) 5965 { 5966 op = OPERATOR_EQEQ; 5967 imp->advance(4); 5968 } 5969 else if (imp->match_c_string(" != ")) 5970 { 5971 op = OPERATOR_NOTEQ; 5972 imp->advance(4); 5973 } 5974 else if (imp->match_c_string(" < ")) 5975 { 5976 op = OPERATOR_LT; 5977 imp->advance(3); 5978 } 5979 else if (imp->match_c_string(" <= ")) 5980 { 5981 op = OPERATOR_LE; 5982 imp->advance(4); 5983 } 5984 else if (imp->match_c_string(" > ")) 5985 { 5986 op = OPERATOR_GT; 5987 imp->advance(3); 5988 } 5989 else if (imp->match_c_string(" >= ")) 5990 { 5991 op = OPERATOR_GE; 5992 imp->advance(4); 5993 } 5994 else if (imp->match_c_string(" + ")) 5995 { 5996 op = OPERATOR_PLUS; 5997 imp->advance(3); 5998 } 5999 else if (imp->match_c_string(" - ")) 6000 { 6001 op = OPERATOR_MINUS; 6002 imp->advance(3); 6003 } 6004 else if (imp->match_c_string(" | ")) 6005 { 6006 op = OPERATOR_OR; 6007 imp->advance(3); 6008 } 6009 else if (imp->match_c_string(" ^ ")) 6010 { 6011 op = OPERATOR_XOR; 6012 imp->advance(3); 6013 } 6014 else if (imp->match_c_string(" * ")) 6015 { 6016 op = OPERATOR_MULT; 6017 imp->advance(3); 6018 } 6019 else if (imp->match_c_string(" / ")) 6020 { 6021 op = OPERATOR_DIV; 6022 imp->advance(3); 6023 } 6024 else if (imp->match_c_string(" % ")) 6025 { 6026 op = OPERATOR_MOD; 6027 imp->advance(3); 6028 } 6029 else if (imp->match_c_string(" << ")) 6030 { 6031 op = OPERATOR_LSHIFT; 6032 imp->advance(4); 6033 } 6034 else if (imp->match_c_string(" >> ")) 6035 { 6036 op = OPERATOR_RSHIFT; 6037 imp->advance(4); 6038 } 6039 else if (imp->match_c_string(" & ")) 6040 { 6041 op = OPERATOR_AND; 6042 imp->advance(3); 6043 } 6044 else if (imp->match_c_string(" &^ ")) 6045 { 6046 op = OPERATOR_BITCLEAR; 6047 imp->advance(4); 6048 } 6049 else 6050 { 6051 error_at(imp->location(), "unrecognized binary operator"); 6052 return Expression::make_error(imp->location()); 6053 } 6054 6055 Expression* right = Expression::import_expression(imp); 6056 6057 imp->require_c_string(")"); 6058 6059 return Expression::make_binary(op, left, right, imp->location()); 6060} 6061 6062// Dump ast representation of a binary expression. 6063 6064void 6065Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 6066{ 6067 ast_dump_context->ostream() << "("; 6068 ast_dump_context->dump_expression(this->left_); 6069 ast_dump_context->ostream() << " "; 6070 ast_dump_context->dump_operator(this->op_); 6071 ast_dump_context->ostream() << " "; 6072 ast_dump_context->dump_expression(this->right_); 6073 ast_dump_context->ostream() << ") "; 6074} 6075 6076// Make a binary expression. 6077 6078Expression* 6079Expression::make_binary(Operator op, Expression* left, Expression* right, 6080 Location location) 6081{ 6082 return new Binary_expression(op, left, right, location); 6083} 6084 6085// Implement a comparison. 6086 6087Bexpression* 6088Expression::comparison(Translate_context* context, Type* result_type, 6089 Operator op, Expression* left, Expression* right, 6090 Location location) 6091{ 6092 Type* left_type = left->type(); 6093 Type* right_type = right->type(); 6094 6095 Expression* zexpr = Expression::make_integer_ul(0, NULL, location); 6096 6097 if (left_type->is_string_type() && right_type->is_string_type()) 6098 { 6099 left = Runtime::make_call(Runtime::STRCMP, location, 2, 6100 left, right); 6101 right = zexpr; 6102 } 6103 else if ((left_type->interface_type() != NULL 6104 && right_type->interface_type() == NULL 6105 && !right_type->is_nil_type()) 6106 || (left_type->interface_type() == NULL 6107 && !left_type->is_nil_type() 6108 && right_type->interface_type() != NULL)) 6109 { 6110 // Comparing an interface value to a non-interface value. 6111 if (left_type->interface_type() == NULL) 6112 { 6113 std::swap(left_type, right_type); 6114 std::swap(left, right); 6115 } 6116 6117 // The right operand is not an interface. We need to take its 6118 // address if it is not a pointer. 6119 Expression* pointer_arg = NULL; 6120 if (right_type->points_to() != NULL) 6121 pointer_arg = right; 6122 else 6123 { 6124 go_assert(right->is_addressable()); 6125 pointer_arg = Expression::make_unary(OPERATOR_AND, right, 6126 location); 6127 } 6128 6129 Expression* descriptor = 6130 Expression::make_type_descriptor(right_type, location); 6131 left = 6132 Runtime::make_call((left_type->interface_type()->is_empty() 6133 ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE 6134 : Runtime::INTERFACE_VALUE_COMPARE), 6135 location, 3, left, descriptor, 6136 pointer_arg); 6137 right = zexpr; 6138 } 6139 else if (left_type->interface_type() != NULL 6140 && right_type->interface_type() != NULL) 6141 { 6142 Runtime::Function compare_function; 6143 if (left_type->interface_type()->is_empty() 6144 && right_type->interface_type()->is_empty()) 6145 compare_function = Runtime::EMPTY_INTERFACE_COMPARE; 6146 else if (!left_type->interface_type()->is_empty() 6147 && !right_type->interface_type()->is_empty()) 6148 compare_function = Runtime::INTERFACE_COMPARE; 6149 else 6150 { 6151 if (left_type->interface_type()->is_empty()) 6152 { 6153 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); 6154 std::swap(left_type, right_type); 6155 std::swap(left, right); 6156 } 6157 go_assert(!left_type->interface_type()->is_empty()); 6158 go_assert(right_type->interface_type()->is_empty()); 6159 compare_function = Runtime::INTERFACE_EMPTY_COMPARE; 6160 } 6161 6162 left = Runtime::make_call(compare_function, location, 2, left, right); 6163 right = zexpr; 6164 } 6165 6166 if (left_type->is_nil_type() 6167 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) 6168 { 6169 std::swap(left_type, right_type); 6170 std::swap(left, right); 6171 } 6172 6173 if (right_type->is_nil_type()) 6174 { 6175 right = Expression::make_nil(location); 6176 if (left_type->array_type() != NULL 6177 && left_type->array_type()->length() == NULL) 6178 { 6179 Array_type* at = left_type->array_type(); 6180 left = at->get_value_pointer(context->gogo(), left); 6181 } 6182 else if (left_type->interface_type() != NULL) 6183 { 6184 // An interface is nil if the first field is nil. 6185 left = Expression::make_field_reference(left, 0, location); 6186 } 6187 } 6188 6189 Bexpression* left_bexpr = left->get_backend(context); 6190 Bexpression* right_bexpr = right->get_backend(context); 6191 6192 Gogo* gogo = context->gogo(); 6193 Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, 6194 right_bexpr, location); 6195 if (result_type != NULL) 6196 ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), 6197 ret, location); 6198 return ret; 6199} 6200 6201// Class Bound_method_expression. 6202 6203// Traversal. 6204 6205int 6206Bound_method_expression::do_traverse(Traverse* traverse) 6207{ 6208 return Expression::traverse(&this->expr_, traverse); 6209} 6210 6211// Lower the expression. If this is a method value rather than being 6212// called, and the method is accessed via a pointer, we may need to 6213// add nil checks. Introduce a temporary variable so that those nil 6214// checks do not cause multiple evaluation. 6215 6216Expression* 6217Bound_method_expression::do_lower(Gogo*, Named_object*, 6218 Statement_inserter* inserter, int) 6219{ 6220 // For simplicity we use a temporary for every call to an embedded 6221 // method, even though some of them might be pure value methods and 6222 // not require a temporary. 6223 if (this->expr_->var_expression() == NULL 6224 && this->expr_->temporary_reference_expression() == NULL 6225 && this->expr_->set_and_use_temporary_expression() == NULL 6226 && (this->method_->field_indexes() != NULL 6227 || (this->method_->is_value_method() 6228 && this->expr_->type()->points_to() != NULL))) 6229 { 6230 Temporary_statement* temp = 6231 Statement::make_temporary(this->expr_->type(), NULL, this->location()); 6232 inserter->insert(temp); 6233 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, 6234 this->location()); 6235 } 6236 return this; 6237} 6238 6239// Return the type of a bound method expression. The type of this 6240// object is simply the type of the method with no receiver. 6241 6242Type* 6243Bound_method_expression::do_type() 6244{ 6245 Named_object* fn = this->method_->named_object(); 6246 Function_type* fntype; 6247 if (fn->is_function()) 6248 fntype = fn->func_value()->type(); 6249 else if (fn->is_function_declaration()) 6250 fntype = fn->func_declaration_value()->type(); 6251 else 6252 return Type::make_error_type(); 6253 return fntype->copy_without_receiver(); 6254} 6255 6256// Determine the types of a method expression. 6257 6258void 6259Bound_method_expression::do_determine_type(const Type_context*) 6260{ 6261 Named_object* fn = this->method_->named_object(); 6262 Function_type* fntype; 6263 if (fn->is_function()) 6264 fntype = fn->func_value()->type(); 6265 else if (fn->is_function_declaration()) 6266 fntype = fn->func_declaration_value()->type(); 6267 else 6268 fntype = NULL; 6269 if (fntype == NULL || !fntype->is_method()) 6270 this->expr_->determine_type_no_context(); 6271 else 6272 { 6273 Type_context subcontext(fntype->receiver()->type(), false); 6274 this->expr_->determine_type(&subcontext); 6275 } 6276} 6277 6278// Check the types of a method expression. 6279 6280void 6281Bound_method_expression::do_check_types(Gogo*) 6282{ 6283 Named_object* fn = this->method_->named_object(); 6284 if (!fn->is_function() && !fn->is_function_declaration()) 6285 { 6286 this->report_error(_("object is not a method")); 6287 return; 6288 } 6289 6290 Function_type* fntype; 6291 if (fn->is_function()) 6292 fntype = fn->func_value()->type(); 6293 else if (fn->is_function_declaration()) 6294 fntype = fn->func_declaration_value()->type(); 6295 else 6296 go_unreachable(); 6297 Type* rtype = fntype->receiver()->type()->deref(); 6298 Type* etype = (this->expr_type_ != NULL 6299 ? this->expr_type_ 6300 : this->expr_->type()); 6301 etype = etype->deref(); 6302 if (!Type::are_identical(rtype, etype, true, NULL)) 6303 this->report_error(_("method type does not match object type")); 6304} 6305 6306// If a bound method expression is not simply called, then it is 6307// represented as a closure. The closure will hold a single variable, 6308// the receiver to pass to the method. The function will be a simple 6309// thunk that pulls that value from the closure and calls the method 6310// with the remaining arguments. 6311// 6312// Because method values are not common, we don't build all thunks for 6313// every methods, but instead only build them as we need them. In 6314// particular, we even build them on demand for methods defined in 6315// other packages. 6316 6317Bound_method_expression::Method_value_thunks 6318 Bound_method_expression::method_value_thunks; 6319 6320// Find or create the thunk for METHOD. 6321 6322Named_object* 6323Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, 6324 Named_object* fn) 6325{ 6326 std::pair<Named_object*, Named_object*> val(fn, NULL); 6327 std::pair<Method_value_thunks::iterator, bool> ins = 6328 Bound_method_expression::method_value_thunks.insert(val); 6329 if (!ins.second) 6330 { 6331 // We have seen this method before. 6332 go_assert(ins.first->second != NULL); 6333 return ins.first->second; 6334 } 6335 6336 Location loc = fn->location(); 6337 6338 Function_type* orig_fntype; 6339 if (fn->is_function()) 6340 orig_fntype = fn->func_value()->type(); 6341 else if (fn->is_function_declaration()) 6342 orig_fntype = fn->func_declaration_value()->type(); 6343 else 6344 orig_fntype = NULL; 6345 6346 if (orig_fntype == NULL || !orig_fntype->is_method()) 6347 { 6348 ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); 6349 return ins.first->second; 6350 } 6351 6352 Struct_field_list* sfl = new Struct_field_list(); 6353 // The type here is wrong--it should be the C function type. But it 6354 // doesn't really matter. 6355 Type* vt = Type::make_pointer_type(Type::make_void_type()); 6356 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); 6357 sfl->push_back(Struct_field(Typed_identifier("val.1", 6358 orig_fntype->receiver()->type(), 6359 loc))); 6360 Type* closure_type = Type::make_struct_type(sfl, loc); 6361 closure_type = Type::make_pointer_type(closure_type); 6362 6363 Function_type* new_fntype = orig_fntype->copy_with_names(); 6364 6365 Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, 6366 false, loc); 6367 6368 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); 6369 cvar->set_is_used(); 6370 cvar->set_is_closure(); 6371 Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); 6372 new_no->func_value()->set_closure_var(cp); 6373 6374 gogo->start_block(loc); 6375 6376 // Field 0 of the closure is the function code pointer, field 1 is 6377 // the value on which to invoke the method. 6378 Expression* arg = Expression::make_var_reference(cp, loc); 6379 arg = Expression::make_unary(OPERATOR_MULT, arg, loc); 6380 arg = Expression::make_field_reference(arg, 1, loc); 6381 6382 Expression* bme = Expression::make_bound_method(arg, method, fn, loc); 6383 6384 const Typed_identifier_list* orig_params = orig_fntype->parameters(); 6385 Expression_list* args; 6386 if (orig_params == NULL || orig_params->empty()) 6387 args = NULL; 6388 else 6389 { 6390 const Typed_identifier_list* new_params = new_fntype->parameters(); 6391 args = new Expression_list(); 6392 for (Typed_identifier_list::const_iterator p = new_params->begin(); 6393 p != new_params->end(); 6394 ++p) 6395 { 6396 Named_object* p_no = gogo->lookup(p->name(), NULL); 6397 go_assert(p_no != NULL 6398 && p_no->is_variable() 6399 && p_no->var_value()->is_parameter()); 6400 args->push_back(Expression::make_var_reference(p_no, loc)); 6401 } 6402 } 6403 6404 Call_expression* call = Expression::make_call(bme, args, 6405 orig_fntype->is_varargs(), 6406 loc); 6407 call->set_varargs_are_lowered(); 6408 6409 Statement* s = Statement::make_return_from_call(call, loc); 6410 gogo->add_statement(s); 6411 Block* b = gogo->finish_block(loc); 6412 gogo->add_block(b, loc); 6413 gogo->lower_block(new_no, b); 6414 gogo->flatten_block(new_no, b); 6415 gogo->finish_function(loc); 6416 6417 ins.first->second = new_no; 6418 return new_no; 6419} 6420 6421// Return an expression to check *REF for nil while dereferencing 6422// according to FIELD_INDEXES. Update *REF to build up the field 6423// reference. This is a static function so that we don't have to 6424// worry about declaring Field_indexes in expressions.h. 6425 6426static Expression* 6427bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, 6428 Expression** ref) 6429{ 6430 if (field_indexes == NULL) 6431 return Expression::make_boolean(false, loc); 6432 Expression* cond = bme_check_nil(field_indexes->next, loc, ref); 6433 Struct_type* stype = (*ref)->type()->deref()->struct_type(); 6434 go_assert(stype != NULL 6435 && field_indexes->field_index < stype->field_count()); 6436 if ((*ref)->type()->struct_type() == NULL) 6437 { 6438 go_assert((*ref)->type()->points_to() != NULL); 6439 Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, 6440 Expression::make_nil(loc), 6441 loc); 6442 cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); 6443 *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); 6444 go_assert((*ref)->type()->struct_type() == stype); 6445 } 6446 *ref = Expression::make_field_reference(*ref, field_indexes->field_index, 6447 loc); 6448 return cond; 6449} 6450 6451// Get the backend representation for a method value. 6452 6453Bexpression* 6454Bound_method_expression::do_get_backend(Translate_context* context) 6455{ 6456 Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(), 6457 this->method_, 6458 this->function_); 6459 if (thunk->is_erroneous()) 6460 { 6461 go_assert(saw_errors()); 6462 return context->backend()->error_expression(); 6463 } 6464 6465 // FIXME: We should lower this earlier, but we can't lower it in the 6466 // lowering pass because at that point we don't know whether we need 6467 // to create the thunk or not. If the expression is called, we 6468 // don't need the thunk. 6469 6470 Location loc = this->location(); 6471 6472 // If the method expects a value, and we have a pointer, we need to 6473 // dereference the pointer. 6474 6475 Named_object* fn = this->method_->named_object(); 6476 Function_type* fntype; 6477 if (fn->is_function()) 6478 fntype = fn->func_value()->type(); 6479 else if (fn->is_function_declaration()) 6480 fntype = fn->func_declaration_value()->type(); 6481 else 6482 go_unreachable(); 6483 6484 Expression* val = this->expr_; 6485 if (fntype->receiver()->type()->points_to() == NULL 6486 && val->type()->points_to() != NULL) 6487 val = Expression::make_unary(OPERATOR_MULT, val, loc); 6488 6489 // Note that we are ignoring this->expr_type_ here. The thunk will 6490 // expect a closure whose second field has type this->expr_type_ (if 6491 // that is not NULL). We are going to pass it a closure whose 6492 // second field has type this->expr_->type(). Since 6493 // this->expr_type_ is only not-NULL for pointer types, we can get 6494 // away with this. 6495 6496 Struct_field_list* fields = new Struct_field_list(); 6497 fields->push_back(Struct_field(Typed_identifier("fn.0", 6498 thunk->func_value()->type(), 6499 loc))); 6500 fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); 6501 Struct_type* st = Type::make_struct_type(fields, loc); 6502 6503 Expression_list* vals = new Expression_list(); 6504 vals->push_back(Expression::make_func_code_reference(thunk, loc)); 6505 vals->push_back(val); 6506 6507 Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); 6508 ret = Expression::make_heap_expression(ret, loc); 6509 6510 // See whether the expression or any embedded pointers are nil. 6511 6512 Expression* nil_check = NULL; 6513 Expression* expr = this->expr_; 6514 if (this->method_->field_indexes() != NULL) 6515 { 6516 // Note that we are evaluating this->expr_ twice, but that is OK 6517 // because in the lowering pass we forced it into a temporary 6518 // variable. 6519 Expression* ref = expr; 6520 nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); 6521 expr = ref; 6522 } 6523 6524 if (this->method_->is_value_method() && expr->type()->points_to() != NULL) 6525 { 6526 Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, 6527 Expression::make_nil(loc), 6528 loc); 6529 if (nil_check == NULL) 6530 nil_check = n; 6531 else 6532 nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); 6533 } 6534 6535 Bexpression* bme = ret->get_backend(context); 6536 if (nil_check != NULL) 6537 { 6538 Gogo* gogo = context->gogo(); 6539 Bexpression* crash = 6540 gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, 6541 loc)->get_backend(context); 6542 Btype* btype = ret->type()->get_backend(gogo); 6543 Bexpression* bcheck = nil_check->get_backend(context); 6544 bme = gogo->backend()->conditional_expression(btype, bcheck, crash, 6545 bme, loc); 6546 } 6547 return bme; 6548} 6549 6550// Dump ast representation of a bound method expression. 6551 6552void 6553Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 6554 const 6555{ 6556 if (this->expr_type_ != NULL) 6557 ast_dump_context->ostream() << "("; 6558 ast_dump_context->dump_expression(this->expr_); 6559 if (this->expr_type_ != NULL) 6560 { 6561 ast_dump_context->ostream() << ":"; 6562 ast_dump_context->dump_type(this->expr_type_); 6563 ast_dump_context->ostream() << ")"; 6564 } 6565 6566 ast_dump_context->ostream() << "." << this->function_->name(); 6567} 6568 6569// Make a method expression. 6570 6571Bound_method_expression* 6572Expression::make_bound_method(Expression* expr, const Method* method, 6573 Named_object* function, Location location) 6574{ 6575 return new Bound_method_expression(expr, method, function, location); 6576} 6577 6578// Class Builtin_call_expression. This is used for a call to a 6579// builtin function. 6580 6581class Builtin_call_expression : public Call_expression 6582{ 6583 public: 6584 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, 6585 bool is_varargs, Location location); 6586 6587 protected: 6588 // This overrides Call_expression::do_lower. 6589 Expression* 6590 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 6591 6592 Expression* 6593 do_flatten(Gogo*, Named_object*, Statement_inserter*); 6594 6595 bool 6596 do_is_constant() const; 6597 6598 bool 6599 do_numeric_constant_value(Numeric_constant*) const; 6600 6601 bool 6602 do_discarding_value(); 6603 6604 Type* 6605 do_type(); 6606 6607 void 6608 do_determine_type(const Type_context*); 6609 6610 void 6611 do_check_types(Gogo*); 6612 6613 Expression* 6614 do_copy(); 6615 6616 Bexpression* 6617 do_get_backend(Translate_context*); 6618 6619 void 6620 do_export(Export*) const; 6621 6622 virtual bool 6623 do_is_recover_call() const; 6624 6625 virtual void 6626 do_set_recover_arg(Expression*); 6627 6628 private: 6629 // The builtin functions. 6630 enum Builtin_function_code 6631 { 6632 BUILTIN_INVALID, 6633 6634 // Predeclared builtin functions. 6635 BUILTIN_APPEND, 6636 BUILTIN_CAP, 6637 BUILTIN_CLOSE, 6638 BUILTIN_COMPLEX, 6639 BUILTIN_COPY, 6640 BUILTIN_DELETE, 6641 BUILTIN_IMAG, 6642 BUILTIN_LEN, 6643 BUILTIN_MAKE, 6644 BUILTIN_NEW, 6645 BUILTIN_PANIC, 6646 BUILTIN_PRINT, 6647 BUILTIN_PRINTLN, 6648 BUILTIN_REAL, 6649 BUILTIN_RECOVER, 6650 6651 // Builtin functions from the unsafe package. 6652 BUILTIN_ALIGNOF, 6653 BUILTIN_OFFSETOF, 6654 BUILTIN_SIZEOF 6655 }; 6656 6657 Expression* 6658 one_arg() const; 6659 6660 bool 6661 check_one_arg(); 6662 6663 static Type* 6664 real_imag_type(Type*); 6665 6666 static Type* 6667 complex_type(Type*); 6668 6669 Expression* 6670 lower_make(); 6671 6672 bool 6673 check_int_value(Expression*, bool is_length); 6674 6675 // A pointer back to the general IR structure. This avoids a global 6676 // variable, or passing it around everywhere. 6677 Gogo* gogo_; 6678 // The builtin function being called. 6679 Builtin_function_code code_; 6680 // Used to stop endless loops when the length of an array uses len 6681 // or cap of the array itself. 6682 mutable bool seen_; 6683 // Whether the argument is set for calls to BUILTIN_RECOVER. 6684 bool recover_arg_is_set_; 6685}; 6686 6687Builtin_call_expression::Builtin_call_expression(Gogo* gogo, 6688 Expression* fn, 6689 Expression_list* args, 6690 bool is_varargs, 6691 Location location) 6692 : Call_expression(fn, args, is_varargs, location), 6693 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), 6694 recover_arg_is_set_(false) 6695{ 6696 Func_expression* fnexp = this->fn()->func_expression(); 6697 go_assert(fnexp != NULL); 6698 const std::string& name(fnexp->named_object()->name()); 6699 if (name == "append") 6700 this->code_ = BUILTIN_APPEND; 6701 else if (name == "cap") 6702 this->code_ = BUILTIN_CAP; 6703 else if (name == "close") 6704 this->code_ = BUILTIN_CLOSE; 6705 else if (name == "complex") 6706 this->code_ = BUILTIN_COMPLEX; 6707 else if (name == "copy") 6708 this->code_ = BUILTIN_COPY; 6709 else if (name == "delete") 6710 this->code_ = BUILTIN_DELETE; 6711 else if (name == "imag") 6712 this->code_ = BUILTIN_IMAG; 6713 else if (name == "len") 6714 this->code_ = BUILTIN_LEN; 6715 else if (name == "make") 6716 this->code_ = BUILTIN_MAKE; 6717 else if (name == "new") 6718 this->code_ = BUILTIN_NEW; 6719 else if (name == "panic") 6720 this->code_ = BUILTIN_PANIC; 6721 else if (name == "print") 6722 this->code_ = BUILTIN_PRINT; 6723 else if (name == "println") 6724 this->code_ = BUILTIN_PRINTLN; 6725 else if (name == "real") 6726 this->code_ = BUILTIN_REAL; 6727 else if (name == "recover") 6728 this->code_ = BUILTIN_RECOVER; 6729 else if (name == "Alignof") 6730 this->code_ = BUILTIN_ALIGNOF; 6731 else if (name == "Offsetof") 6732 this->code_ = BUILTIN_OFFSETOF; 6733 else if (name == "Sizeof") 6734 this->code_ = BUILTIN_SIZEOF; 6735 else 6736 go_unreachable(); 6737} 6738 6739// Return whether this is a call to recover. This is a virtual 6740// function called from the parent class. 6741 6742bool 6743Builtin_call_expression::do_is_recover_call() const 6744{ 6745 if (this->classification() == EXPRESSION_ERROR) 6746 return false; 6747 return this->code_ == BUILTIN_RECOVER; 6748} 6749 6750// Set the argument for a call to recover. 6751 6752void 6753Builtin_call_expression::do_set_recover_arg(Expression* arg) 6754{ 6755 const Expression_list* args = this->args(); 6756 go_assert(args == NULL || args->empty()); 6757 Expression_list* new_args = new Expression_list(); 6758 new_args->push_back(arg); 6759 this->set_args(new_args); 6760 this->recover_arg_is_set_ = true; 6761} 6762 6763// Lower a builtin call expression. This turns new and make into 6764// specific expressions. We also convert to a constant if we can. 6765 6766Expression* 6767Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, 6768 Statement_inserter* inserter, int) 6769{ 6770 if (this->classification() == EXPRESSION_ERROR) 6771 return this; 6772 6773 Location loc = this->location(); 6774 6775 if (this->is_varargs() && this->code_ != BUILTIN_APPEND) 6776 { 6777 this->report_error(_("invalid use of %<...%> with builtin function")); 6778 return Expression::make_error(loc); 6779 } 6780 6781 if (this->code_ == BUILTIN_OFFSETOF) 6782 { 6783 Expression* arg = this->one_arg(); 6784 6785 if (arg->bound_method_expression() != NULL 6786 || arg->interface_field_reference_expression() != NULL) 6787 { 6788 this->report_error(_("invalid use of method value as argument " 6789 "of Offsetof")); 6790 return this; 6791 } 6792 6793 Field_reference_expression* farg = arg->field_reference_expression(); 6794 while (farg != NULL) 6795 { 6796 if (!farg->implicit()) 6797 break; 6798 // When the selector refers to an embedded field, 6799 // it must not be reached through pointer indirections. 6800 if (farg->expr()->deref() != farg->expr()) 6801 { 6802 this->report_error(_("argument of Offsetof implies " 6803 "indirection of an embedded field")); 6804 return this; 6805 } 6806 // Go up until we reach the original base. 6807 farg = farg->expr()->field_reference_expression(); 6808 } 6809 } 6810 6811 if (this->is_constant()) 6812 { 6813 Numeric_constant nc; 6814 if (this->numeric_constant_value(&nc)) 6815 return nc.expression(loc); 6816 } 6817 6818 switch (this->code_) 6819 { 6820 default: 6821 break; 6822 6823 case BUILTIN_NEW: 6824 { 6825 const Expression_list* args = this->args(); 6826 if (args == NULL || args->size() < 1) 6827 this->report_error(_("not enough arguments")); 6828 else if (args->size() > 1) 6829 this->report_error(_("too many arguments")); 6830 else 6831 { 6832 Expression* arg = args->front(); 6833 if (!arg->is_type_expression()) 6834 { 6835 error_at(arg->location(), "expected type"); 6836 this->set_is_error(); 6837 } 6838 else 6839 return Expression::make_allocation(arg->type(), loc); 6840 } 6841 } 6842 break; 6843 6844 case BUILTIN_MAKE: 6845 return this->lower_make(); 6846 6847 case BUILTIN_RECOVER: 6848 if (function != NULL) 6849 function->func_value()->set_calls_recover(); 6850 else 6851 { 6852 // Calling recover outside of a function always returns the 6853 // nil empty interface. 6854 Type* eface = Type::make_empty_interface_type(loc); 6855 return Expression::make_cast(eface, Expression::make_nil(loc), loc); 6856 } 6857 break; 6858 6859 case BUILTIN_APPEND: 6860 { 6861 // Lower the varargs. 6862 const Expression_list* args = this->args(); 6863 if (args == NULL || args->empty()) 6864 return this; 6865 Type* slice_type = args->front()->type(); 6866 if (!slice_type->is_slice_type()) 6867 { 6868 if (slice_type->is_nil_type()) 6869 error_at(args->front()->location(), "use of untyped nil"); 6870 else 6871 error_at(args->front()->location(), 6872 "argument 1 must be a slice"); 6873 this->set_is_error(); 6874 return this; 6875 } 6876 Type* element_type = slice_type->array_type()->element_type(); 6877 this->lower_varargs(gogo, function, inserter, 6878 Type::make_array_type(element_type, NULL), 6879 2); 6880 } 6881 break; 6882 6883 case BUILTIN_DELETE: 6884 { 6885 // Lower to a runtime function call. 6886 const Expression_list* args = this->args(); 6887 if (args == NULL || args->size() < 2) 6888 this->report_error(_("not enough arguments")); 6889 else if (args->size() > 2) 6890 this->report_error(_("too many arguments")); 6891 else if (args->front()->type()->map_type() == NULL) 6892 this->report_error(_("argument 1 must be a map")); 6893 else 6894 { 6895 // Since this function returns no value it must appear in 6896 // a statement by itself, so we don't have to worry about 6897 // order of evaluation of values around it. Evaluate the 6898 // map first to get order of evaluation right. 6899 Map_type* mt = args->front()->type()->map_type(); 6900 Temporary_statement* map_temp = 6901 Statement::make_temporary(mt, args->front(), loc); 6902 inserter->insert(map_temp); 6903 6904 Temporary_statement* key_temp = 6905 Statement::make_temporary(mt->key_type(), args->back(), loc); 6906 inserter->insert(key_temp); 6907 6908 Expression* e1 = Expression::make_temporary_reference(map_temp, 6909 loc); 6910 Expression* e2 = Expression::make_temporary_reference(key_temp, 6911 loc); 6912 e2 = Expression::make_unary(OPERATOR_AND, e2, loc); 6913 return Runtime::make_call(Runtime::MAPDELETE, this->location(), 6914 2, e1, e2); 6915 } 6916 } 6917 break; 6918 } 6919 6920 return this; 6921} 6922 6923// Flatten a builtin call expression. This turns the arguments of copy and 6924// append into temporary expressions. 6925 6926Expression* 6927Builtin_call_expression::do_flatten(Gogo*, Named_object*, 6928 Statement_inserter* inserter) 6929{ 6930 Location loc = this->location(); 6931 6932 switch (this->code_) 6933 { 6934 default: 6935 break; 6936 6937 case BUILTIN_APPEND: 6938 case BUILTIN_COPY: 6939 { 6940 Type* at = this->args()->front()->type(); 6941 for (Expression_list::iterator pa = this->args()->begin(); 6942 pa != this->args()->end(); 6943 ++pa) 6944 { 6945 if ((*pa)->is_nil_expression()) 6946 { 6947 Expression* nil = Expression::make_nil(loc); 6948 Expression* zero = Expression::make_integer_ul(0, NULL, loc); 6949 *pa = Expression::make_slice_value(at, nil, zero, zero, loc); 6950 } 6951 if (!(*pa)->is_variable()) 6952 { 6953 Temporary_statement* temp = 6954 Statement::make_temporary(NULL, *pa, loc); 6955 inserter->insert(temp); 6956 *pa = Expression::make_temporary_reference(temp, loc); 6957 } 6958 } 6959 } 6960 break; 6961 6962 case BUILTIN_PANIC: 6963 for (Expression_list::iterator pa = this->args()->begin(); 6964 pa != this->args()->end(); 6965 ++pa) 6966 { 6967 if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) 6968 { 6969 Temporary_statement* temp = 6970 Statement::make_temporary(NULL, *pa, loc); 6971 inserter->insert(temp); 6972 *pa = Expression::make_temporary_reference(temp, loc); 6973 } 6974 } 6975 } 6976 6977 return this; 6978} 6979 6980// Lower a make expression. 6981 6982Expression* 6983Builtin_call_expression::lower_make() 6984{ 6985 Location loc = this->location(); 6986 6987 const Expression_list* args = this->args(); 6988 if (args == NULL || args->size() < 1) 6989 { 6990 this->report_error(_("not enough arguments")); 6991 return Expression::make_error(this->location()); 6992 } 6993 6994 Expression_list::const_iterator parg = args->begin(); 6995 6996 Expression* first_arg = *parg; 6997 if (!first_arg->is_type_expression()) 6998 { 6999 error_at(first_arg->location(), "expected type"); 7000 this->set_is_error(); 7001 return Expression::make_error(this->location()); 7002 } 7003 Type* type = first_arg->type(); 7004 7005 bool is_slice = false; 7006 bool is_map = false; 7007 bool is_chan = false; 7008 if (type->is_slice_type()) 7009 is_slice = true; 7010 else if (type->map_type() != NULL) 7011 is_map = true; 7012 else if (type->channel_type() != NULL) 7013 is_chan = true; 7014 else 7015 { 7016 this->report_error(_("invalid type for make function")); 7017 return Expression::make_error(this->location()); 7018 } 7019 7020 bool have_big_args = false; 7021 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 7022 int uintptr_bits = uintptr_type->integer_type()->bits(); 7023 7024 Type_context int_context(Type::lookup_integer_type("int"), false); 7025 7026 ++parg; 7027 Expression* len_arg; 7028 if (parg == args->end()) 7029 { 7030 if (is_slice) 7031 { 7032 this->report_error(_("length required when allocating a slice")); 7033 return Expression::make_error(this->location()); 7034 } 7035 len_arg = Expression::make_integer_ul(0, NULL, loc); 7036 } 7037 else 7038 { 7039 len_arg = *parg; 7040 len_arg->determine_type(&int_context); 7041 if (!this->check_int_value(len_arg, true)) 7042 return Expression::make_error(this->location()); 7043 if (len_arg->type()->integer_type() != NULL 7044 && len_arg->type()->integer_type()->bits() > uintptr_bits) 7045 have_big_args = true; 7046 ++parg; 7047 } 7048 7049 Expression* cap_arg = NULL; 7050 if (is_slice && parg != args->end()) 7051 { 7052 cap_arg = *parg; 7053 cap_arg->determine_type(&int_context); 7054 if (!this->check_int_value(cap_arg, false)) 7055 return Expression::make_error(this->location()); 7056 7057 Numeric_constant nclen; 7058 Numeric_constant nccap; 7059 unsigned long vlen; 7060 unsigned long vcap; 7061 if (len_arg->numeric_constant_value(&nclen) 7062 && cap_arg->numeric_constant_value(&nccap) 7063 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID 7064 && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID 7065 && vlen > vcap) 7066 { 7067 this->report_error(_("len larger than cap")); 7068 return Expression::make_error(this->location()); 7069 } 7070 7071 if (cap_arg->type()->integer_type() != NULL 7072 && cap_arg->type()->integer_type()->bits() > uintptr_bits) 7073 have_big_args = true; 7074 ++parg; 7075 } 7076 7077 if (parg != args->end()) 7078 { 7079 this->report_error(_("too many arguments to make")); 7080 return Expression::make_error(this->location()); 7081 } 7082 7083 Location type_loc = first_arg->location(); 7084 Expression* type_arg; 7085 if (is_slice || is_chan) 7086 type_arg = Expression::make_type_descriptor(type, type_loc); 7087 else if (is_map) 7088 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); 7089 else 7090 go_unreachable(); 7091 7092 Expression* call; 7093 if (is_slice) 7094 { 7095 if (cap_arg == NULL) 7096 call = Runtime::make_call((have_big_args 7097 ? Runtime::MAKESLICE1BIG 7098 : Runtime::MAKESLICE1), 7099 loc, 2, type_arg, len_arg); 7100 else 7101 call = Runtime::make_call((have_big_args 7102 ? Runtime::MAKESLICE2BIG 7103 : Runtime::MAKESLICE2), 7104 loc, 3, type_arg, len_arg, cap_arg); 7105 } 7106 else if (is_map) 7107 call = Runtime::make_call((have_big_args 7108 ? Runtime::MAKEMAPBIG 7109 : Runtime::MAKEMAP), 7110 loc, 2, type_arg, len_arg); 7111 else if (is_chan) 7112 call = Runtime::make_call((have_big_args 7113 ? Runtime::MAKECHANBIG 7114 : Runtime::MAKECHAN), 7115 loc, 2, type_arg, len_arg); 7116 else 7117 go_unreachable(); 7118 7119 return Expression::make_unsafe_cast(type, call, loc); 7120} 7121 7122// Return whether an expression has an integer value. Report an error 7123// if not. This is used when handling calls to the predeclared make 7124// function. 7125 7126bool 7127Builtin_call_expression::check_int_value(Expression* e, bool is_length) 7128{ 7129 Numeric_constant nc; 7130 if (e->numeric_constant_value(&nc)) 7131 { 7132 unsigned long v; 7133 switch (nc.to_unsigned_long(&v)) 7134 { 7135 case Numeric_constant::NC_UL_VALID: 7136 break; 7137 case Numeric_constant::NC_UL_NOTINT: 7138 error_at(e->location(), "non-integer %s argument to make", 7139 is_length ? "len" : "cap"); 7140 return false; 7141 case Numeric_constant::NC_UL_NEGATIVE: 7142 error_at(e->location(), "negative %s argument to make", 7143 is_length ? "len" : "cap"); 7144 return false; 7145 case Numeric_constant::NC_UL_BIG: 7146 // We don't want to give a compile-time error for a 64-bit 7147 // value on a 32-bit target. 7148 break; 7149 } 7150 7151 mpz_t val; 7152 if (!nc.to_int(&val)) 7153 go_unreachable(); 7154 int bits = mpz_sizeinbase(val, 2); 7155 mpz_clear(val); 7156 Type* int_type = Type::lookup_integer_type("int"); 7157 if (bits >= int_type->integer_type()->bits()) 7158 { 7159 error_at(e->location(), "%s argument too large for make", 7160 is_length ? "len" : "cap"); 7161 return false; 7162 } 7163 7164 return true; 7165 } 7166 7167 if (e->type()->integer_type() != NULL) 7168 return true; 7169 7170 error_at(e->location(), "non-integer %s argument to make", 7171 is_length ? "len" : "cap"); 7172 return false; 7173} 7174 7175// Return the type of the real or imag functions, given the type of 7176// the argument. We need to map complex64 to float32 and complex128 7177// to float64, so it has to be done by name. This returns NULL if it 7178// can't figure out the type. 7179 7180Type* 7181Builtin_call_expression::real_imag_type(Type* arg_type) 7182{ 7183 if (arg_type == NULL || arg_type->is_abstract()) 7184 return NULL; 7185 Named_type* nt = arg_type->named_type(); 7186 if (nt == NULL) 7187 return NULL; 7188 while (nt->real_type()->named_type() != NULL) 7189 nt = nt->real_type()->named_type(); 7190 if (nt->name() == "complex64") 7191 return Type::lookup_float_type("float32"); 7192 else if (nt->name() == "complex128") 7193 return Type::lookup_float_type("float64"); 7194 else 7195 return NULL; 7196} 7197 7198// Return the type of the complex function, given the type of one of the 7199// argments. Like real_imag_type, we have to map by name. 7200 7201Type* 7202Builtin_call_expression::complex_type(Type* arg_type) 7203{ 7204 if (arg_type == NULL || arg_type->is_abstract()) 7205 return NULL; 7206 Named_type* nt = arg_type->named_type(); 7207 if (nt == NULL) 7208 return NULL; 7209 while (nt->real_type()->named_type() != NULL) 7210 nt = nt->real_type()->named_type(); 7211 if (nt->name() == "float32") 7212 return Type::lookup_complex_type("complex64"); 7213 else if (nt->name() == "float64") 7214 return Type::lookup_complex_type("complex128"); 7215 else 7216 return NULL; 7217} 7218 7219// Return a single argument, or NULL if there isn't one. 7220 7221Expression* 7222Builtin_call_expression::one_arg() const 7223{ 7224 const Expression_list* args = this->args(); 7225 if (args == NULL || args->size() != 1) 7226 return NULL; 7227 return args->front(); 7228} 7229 7230// A traversal class which looks for a call or receive expression. 7231 7232class Find_call_expression : public Traverse 7233{ 7234 public: 7235 Find_call_expression() 7236 : Traverse(traverse_expressions), 7237 found_(false) 7238 { } 7239 7240 int 7241 expression(Expression**); 7242 7243 bool 7244 found() 7245 { return this->found_; } 7246 7247 private: 7248 bool found_; 7249}; 7250 7251int 7252Find_call_expression::expression(Expression** pexpr) 7253{ 7254 if ((*pexpr)->call_expression() != NULL 7255 || (*pexpr)->receive_expression() != NULL) 7256 { 7257 this->found_ = true; 7258 return TRAVERSE_EXIT; 7259 } 7260 return TRAVERSE_CONTINUE; 7261} 7262 7263// Return whether this is constant: len of a string constant, or len 7264// or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, 7265// unsafe.Alignof. 7266 7267bool 7268Builtin_call_expression::do_is_constant() const 7269{ 7270 if (this->is_error_expression()) 7271 return true; 7272 switch (this->code_) 7273 { 7274 case BUILTIN_LEN: 7275 case BUILTIN_CAP: 7276 { 7277 if (this->seen_) 7278 return false; 7279 7280 Expression* arg = this->one_arg(); 7281 if (arg == NULL) 7282 return false; 7283 Type* arg_type = arg->type(); 7284 7285 if (arg_type->points_to() != NULL 7286 && arg_type->points_to()->array_type() != NULL 7287 && !arg_type->points_to()->is_slice_type()) 7288 arg_type = arg_type->points_to(); 7289 7290 // The len and cap functions are only constant if there are no 7291 // function calls or channel operations in the arguments. 7292 // Otherwise we have to make the call. 7293 if (!arg->is_constant()) 7294 { 7295 Find_call_expression find_call; 7296 Expression::traverse(&arg, &find_call); 7297 if (find_call.found()) 7298 return false; 7299 } 7300 7301 if (arg_type->array_type() != NULL 7302 && arg_type->array_type()->length() != NULL) 7303 return true; 7304 7305 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) 7306 { 7307 this->seen_ = true; 7308 bool ret = arg->is_constant(); 7309 this->seen_ = false; 7310 return ret; 7311 } 7312 } 7313 break; 7314 7315 case BUILTIN_SIZEOF: 7316 case BUILTIN_ALIGNOF: 7317 return this->one_arg() != NULL; 7318 7319 case BUILTIN_OFFSETOF: 7320 { 7321 Expression* arg = this->one_arg(); 7322 if (arg == NULL) 7323 return false; 7324 return arg->field_reference_expression() != NULL; 7325 } 7326 7327 case BUILTIN_COMPLEX: 7328 { 7329 const Expression_list* args = this->args(); 7330 if (args != NULL && args->size() == 2) 7331 return args->front()->is_constant() && args->back()->is_constant(); 7332 } 7333 break; 7334 7335 case BUILTIN_REAL: 7336 case BUILTIN_IMAG: 7337 { 7338 Expression* arg = this->one_arg(); 7339 return arg != NULL && arg->is_constant(); 7340 } 7341 7342 default: 7343 break; 7344 } 7345 7346 return false; 7347} 7348 7349// Return a numeric constant if possible. 7350 7351bool 7352Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const 7353{ 7354 if (this->code_ == BUILTIN_LEN 7355 || this->code_ == BUILTIN_CAP) 7356 { 7357 Expression* arg = this->one_arg(); 7358 if (arg == NULL) 7359 return false; 7360 Type* arg_type = arg->type(); 7361 7362 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) 7363 { 7364 std::string sval; 7365 if (arg->string_constant_value(&sval)) 7366 { 7367 nc->set_unsigned_long(Type::lookup_integer_type("int"), 7368 sval.length()); 7369 return true; 7370 } 7371 } 7372 7373 if (arg_type->points_to() != NULL 7374 && arg_type->points_to()->array_type() != NULL 7375 && !arg_type->points_to()->is_slice_type()) 7376 arg_type = arg_type->points_to(); 7377 7378 if (arg_type->array_type() != NULL 7379 && arg_type->array_type()->length() != NULL) 7380 { 7381 if (this->seen_) 7382 return false; 7383 Expression* e = arg_type->array_type()->length(); 7384 this->seen_ = true; 7385 bool r = e->numeric_constant_value(nc); 7386 this->seen_ = false; 7387 if (r) 7388 { 7389 if (!nc->set_type(Type::lookup_integer_type("int"), false, 7390 this->location())) 7391 r = false; 7392 } 7393 return r; 7394 } 7395 } 7396 else if (this->code_ == BUILTIN_SIZEOF 7397 || this->code_ == BUILTIN_ALIGNOF) 7398 { 7399 Expression* arg = this->one_arg(); 7400 if (arg == NULL) 7401 return false; 7402 Type* arg_type = arg->type(); 7403 if (arg_type->is_error()) 7404 return false; 7405 if (arg_type->is_abstract()) 7406 return false; 7407 if (this->seen_) 7408 return false; 7409 7410 int64_t ret; 7411 if (this->code_ == BUILTIN_SIZEOF) 7412 { 7413 this->seen_ = true; 7414 bool ok = arg_type->backend_type_size(this->gogo_, &ret); 7415 this->seen_ = false; 7416 if (!ok) 7417 return false; 7418 } 7419 else if (this->code_ == BUILTIN_ALIGNOF) 7420 { 7421 bool ok; 7422 this->seen_ = true; 7423 if (arg->field_reference_expression() == NULL) 7424 ok = arg_type->backend_type_align(this->gogo_, &ret); 7425 else 7426 { 7427 // Calling unsafe.Alignof(s.f) returns the alignment of 7428 // the type of f when it is used as a field in a struct. 7429 ok = arg_type->backend_type_field_align(this->gogo_, &ret); 7430 } 7431 this->seen_ = false; 7432 if (!ok) 7433 return false; 7434 } 7435 else 7436 go_unreachable(); 7437 7438 mpz_t zval; 7439 set_mpz_from_int64(&zval, ret); 7440 nc->set_int(Type::lookup_integer_type("uintptr"), zval); 7441 mpz_clear(zval); 7442 return true; 7443 } 7444 else if (this->code_ == BUILTIN_OFFSETOF) 7445 { 7446 Expression* arg = this->one_arg(); 7447 if (arg == NULL) 7448 return false; 7449 Field_reference_expression* farg = arg->field_reference_expression(); 7450 if (farg == NULL) 7451 return false; 7452 if (this->seen_) 7453 return false; 7454 7455 int64_t total_offset = 0; 7456 while (true) 7457 { 7458 Expression* struct_expr = farg->expr(); 7459 Type* st = struct_expr->type(); 7460 if (st->struct_type() == NULL) 7461 return false; 7462 if (st->named_type() != NULL) 7463 st->named_type()->convert(this->gogo_); 7464 int64_t offset; 7465 this->seen_ = true; 7466 bool ok = st->struct_type()->backend_field_offset(this->gogo_, 7467 farg->field_index(), 7468 &offset); 7469 this->seen_ = false; 7470 if (!ok) 7471 return false; 7472 total_offset += offset; 7473 if (farg->implicit() && struct_expr->field_reference_expression() != NULL) 7474 { 7475 // Go up until we reach the original base. 7476 farg = struct_expr->field_reference_expression(); 7477 continue; 7478 } 7479 break; 7480 } 7481 mpz_t zval; 7482 set_mpz_from_int64(&zval, total_offset); 7483 nc->set_int(Type::lookup_integer_type("uintptr"), zval); 7484 mpz_clear(zval); 7485 return true; 7486 } 7487 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) 7488 { 7489 Expression* arg = this->one_arg(); 7490 if (arg == NULL) 7491 return false; 7492 7493 Numeric_constant argnc; 7494 if (!arg->numeric_constant_value(&argnc)) 7495 return false; 7496 7497 mpc_t val; 7498 if (!argnc.to_complex(&val)) 7499 return false; 7500 7501 Type* type = Builtin_call_expression::real_imag_type(argnc.type()); 7502 if (this->code_ == BUILTIN_REAL) 7503 nc->set_float(type, mpc_realref(val)); 7504 else 7505 nc->set_float(type, mpc_imagref(val)); 7506 mpc_clear(val); 7507 return true; 7508 } 7509 else if (this->code_ == BUILTIN_COMPLEX) 7510 { 7511 const Expression_list* args = this->args(); 7512 if (args == NULL || args->size() != 2) 7513 return false; 7514 7515 Numeric_constant rnc; 7516 if (!args->front()->numeric_constant_value(&rnc)) 7517 return false; 7518 Numeric_constant inc; 7519 if (!args->back()->numeric_constant_value(&inc)) 7520 return false; 7521 7522 if (rnc.type() != NULL 7523 && !rnc.type()->is_abstract() 7524 && inc.type() != NULL 7525 && !inc.type()->is_abstract() 7526 && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) 7527 return false; 7528 7529 mpfr_t r; 7530 if (!rnc.to_float(&r)) 7531 return false; 7532 mpfr_t i; 7533 if (!inc.to_float(&i)) 7534 { 7535 mpfr_clear(r); 7536 return false; 7537 } 7538 7539 Type* arg_type = rnc.type(); 7540 if (arg_type == NULL || arg_type->is_abstract()) 7541 arg_type = inc.type(); 7542 7543 mpc_t val; 7544 mpc_init2(val, mpc_precision); 7545 mpc_set_fr_fr(val, r, i, MPC_RNDNN); 7546 mpfr_clear(r); 7547 mpfr_clear(i); 7548 7549 Type* type = Builtin_call_expression::complex_type(arg_type); 7550 nc->set_complex(type, val); 7551 7552 mpc_clear(val); 7553 7554 return true; 7555 } 7556 7557 return false; 7558} 7559 7560// Give an error if we are discarding the value of an expression which 7561// should not normally be discarded. We don't give an error for 7562// discarding the value of an ordinary function call, but we do for 7563// builtin functions, purely for consistency with the gc compiler. 7564 7565bool 7566Builtin_call_expression::do_discarding_value() 7567{ 7568 switch (this->code_) 7569 { 7570 case BUILTIN_INVALID: 7571 default: 7572 go_unreachable(); 7573 7574 case BUILTIN_APPEND: 7575 case BUILTIN_CAP: 7576 case BUILTIN_COMPLEX: 7577 case BUILTIN_IMAG: 7578 case BUILTIN_LEN: 7579 case BUILTIN_MAKE: 7580 case BUILTIN_NEW: 7581 case BUILTIN_REAL: 7582 case BUILTIN_ALIGNOF: 7583 case BUILTIN_OFFSETOF: 7584 case BUILTIN_SIZEOF: 7585 this->unused_value_error(); 7586 return false; 7587 7588 case BUILTIN_CLOSE: 7589 case BUILTIN_COPY: 7590 case BUILTIN_DELETE: 7591 case BUILTIN_PANIC: 7592 case BUILTIN_PRINT: 7593 case BUILTIN_PRINTLN: 7594 case BUILTIN_RECOVER: 7595 return true; 7596 } 7597} 7598 7599// Return the type. 7600 7601Type* 7602Builtin_call_expression::do_type() 7603{ 7604 switch (this->code_) 7605 { 7606 case BUILTIN_INVALID: 7607 default: 7608 go_unreachable(); 7609 7610 case BUILTIN_NEW: 7611 case BUILTIN_MAKE: 7612 { 7613 const Expression_list* args = this->args(); 7614 if (args == NULL || args->empty()) 7615 return Type::make_error_type(); 7616 return Type::make_pointer_type(args->front()->type()); 7617 } 7618 7619 case BUILTIN_CAP: 7620 case BUILTIN_COPY: 7621 case BUILTIN_LEN: 7622 return Type::lookup_integer_type("int"); 7623 7624 case BUILTIN_ALIGNOF: 7625 case BUILTIN_OFFSETOF: 7626 case BUILTIN_SIZEOF: 7627 return Type::lookup_integer_type("uintptr"); 7628 7629 case BUILTIN_CLOSE: 7630 case BUILTIN_DELETE: 7631 case BUILTIN_PANIC: 7632 case BUILTIN_PRINT: 7633 case BUILTIN_PRINTLN: 7634 return Type::make_void_type(); 7635 7636 case BUILTIN_RECOVER: 7637 return Type::make_empty_interface_type(Linemap::predeclared_location()); 7638 7639 case BUILTIN_APPEND: 7640 { 7641 const Expression_list* args = this->args(); 7642 if (args == NULL || args->empty()) 7643 return Type::make_error_type(); 7644 Type *ret = args->front()->type(); 7645 if (!ret->is_slice_type()) 7646 return Type::make_error_type(); 7647 return ret; 7648 } 7649 7650 case BUILTIN_REAL: 7651 case BUILTIN_IMAG: 7652 { 7653 Expression* arg = this->one_arg(); 7654 if (arg == NULL) 7655 return Type::make_error_type(); 7656 Type* t = arg->type(); 7657 if (t->is_abstract()) 7658 t = t->make_non_abstract_type(); 7659 t = Builtin_call_expression::real_imag_type(t); 7660 if (t == NULL) 7661 t = Type::make_error_type(); 7662 return t; 7663 } 7664 7665 case BUILTIN_COMPLEX: 7666 { 7667 const Expression_list* args = this->args(); 7668 if (args == NULL || args->size() != 2) 7669 return Type::make_error_type(); 7670 Type* t = args->front()->type(); 7671 if (t->is_abstract()) 7672 { 7673 t = args->back()->type(); 7674 if (t->is_abstract()) 7675 t = t->make_non_abstract_type(); 7676 } 7677 t = Builtin_call_expression::complex_type(t); 7678 if (t == NULL) 7679 t = Type::make_error_type(); 7680 return t; 7681 } 7682 } 7683} 7684 7685// Determine the type. 7686 7687void 7688Builtin_call_expression::do_determine_type(const Type_context* context) 7689{ 7690 if (!this->determining_types()) 7691 return; 7692 7693 this->fn()->determine_type_no_context(); 7694 7695 const Expression_list* args = this->args(); 7696 7697 bool is_print; 7698 Type* arg_type = NULL; 7699 switch (this->code_) 7700 { 7701 case BUILTIN_PRINT: 7702 case BUILTIN_PRINTLN: 7703 // Do not force a large integer constant to "int". 7704 is_print = true; 7705 break; 7706 7707 case BUILTIN_REAL: 7708 case BUILTIN_IMAG: 7709 arg_type = Builtin_call_expression::complex_type(context->type); 7710 if (arg_type == NULL) 7711 arg_type = Type::lookup_complex_type("complex128"); 7712 is_print = false; 7713 break; 7714 7715 case BUILTIN_COMPLEX: 7716 { 7717 // For the complex function the type of one operand can 7718 // determine the type of the other, as in a binary expression. 7719 arg_type = Builtin_call_expression::real_imag_type(context->type); 7720 if (arg_type == NULL) 7721 arg_type = Type::lookup_float_type("float64"); 7722 if (args != NULL && args->size() == 2) 7723 { 7724 Type* t1 = args->front()->type(); 7725 Type* t2 = args->back()->type(); 7726 if (!t1->is_abstract()) 7727 arg_type = t1; 7728 else if (!t2->is_abstract()) 7729 arg_type = t2; 7730 } 7731 is_print = false; 7732 } 7733 break; 7734 7735 default: 7736 is_print = false; 7737 break; 7738 } 7739 7740 if (args != NULL) 7741 { 7742 for (Expression_list::const_iterator pa = args->begin(); 7743 pa != args->end(); 7744 ++pa) 7745 { 7746 Type_context subcontext; 7747 subcontext.type = arg_type; 7748 7749 if (is_print) 7750 { 7751 // We want to print large constants, we so can't just 7752 // use the appropriate nonabstract type. Use uint64 for 7753 // an integer if we know it is nonnegative, otherwise 7754 // use int64 for a integer, otherwise use float64 for a 7755 // float or complex128 for a complex. 7756 Type* want_type = NULL; 7757 Type* atype = (*pa)->type(); 7758 if (atype->is_abstract()) 7759 { 7760 if (atype->integer_type() != NULL) 7761 { 7762 Numeric_constant nc; 7763 if (this->numeric_constant_value(&nc)) 7764 { 7765 mpz_t val; 7766 if (nc.to_int(&val)) 7767 { 7768 if (mpz_sgn(val) >= 0) 7769 want_type = Type::lookup_integer_type("uint64"); 7770 mpz_clear(val); 7771 } 7772 } 7773 if (want_type == NULL) 7774 want_type = Type::lookup_integer_type("int64"); 7775 } 7776 else if (atype->float_type() != NULL) 7777 want_type = Type::lookup_float_type("float64"); 7778 else if (atype->complex_type() != NULL) 7779 want_type = Type::lookup_complex_type("complex128"); 7780 else if (atype->is_abstract_string_type()) 7781 want_type = Type::lookup_string_type(); 7782 else if (atype->is_abstract_boolean_type()) 7783 want_type = Type::lookup_bool_type(); 7784 else 7785 go_unreachable(); 7786 subcontext.type = want_type; 7787 } 7788 } 7789 7790 (*pa)->determine_type(&subcontext); 7791 } 7792 } 7793} 7794 7795// If there is exactly one argument, return true. Otherwise give an 7796// error message and return false. 7797 7798bool 7799Builtin_call_expression::check_one_arg() 7800{ 7801 const Expression_list* args = this->args(); 7802 if (args == NULL || args->size() < 1) 7803 { 7804 this->report_error(_("not enough arguments")); 7805 return false; 7806 } 7807 else if (args->size() > 1) 7808 { 7809 this->report_error(_("too many arguments")); 7810 return false; 7811 } 7812 if (args->front()->is_error_expression() 7813 || args->front()->type()->is_error()) 7814 { 7815 this->set_is_error(); 7816 return false; 7817 } 7818 return true; 7819} 7820 7821// Check argument types for a builtin function. 7822 7823void 7824Builtin_call_expression::do_check_types(Gogo*) 7825{ 7826 if (this->is_error_expression()) 7827 return; 7828 switch (this->code_) 7829 { 7830 case BUILTIN_INVALID: 7831 case BUILTIN_NEW: 7832 case BUILTIN_MAKE: 7833 case BUILTIN_DELETE: 7834 return; 7835 7836 case BUILTIN_LEN: 7837 case BUILTIN_CAP: 7838 { 7839 // The single argument may be either a string or an array or a 7840 // map or a channel, or a pointer to a closed array. 7841 if (this->check_one_arg()) 7842 { 7843 Type* arg_type = this->one_arg()->type(); 7844 if (arg_type->points_to() != NULL 7845 && arg_type->points_to()->array_type() != NULL 7846 && !arg_type->points_to()->is_slice_type()) 7847 arg_type = arg_type->points_to(); 7848 if (this->code_ == BUILTIN_CAP) 7849 { 7850 if (!arg_type->is_error() 7851 && arg_type->array_type() == NULL 7852 && arg_type->channel_type() == NULL) 7853 this->report_error(_("argument must be array or slice " 7854 "or channel")); 7855 } 7856 else 7857 { 7858 if (!arg_type->is_error() 7859 && !arg_type->is_string_type() 7860 && arg_type->array_type() == NULL 7861 && arg_type->map_type() == NULL 7862 && arg_type->channel_type() == NULL) 7863 this->report_error(_("argument must be string or " 7864 "array or slice or map or channel")); 7865 } 7866 } 7867 } 7868 break; 7869 7870 case BUILTIN_PRINT: 7871 case BUILTIN_PRINTLN: 7872 { 7873 const Expression_list* args = this->args(); 7874 if (args == NULL) 7875 { 7876 if (this->code_ == BUILTIN_PRINT) 7877 warning_at(this->location(), 0, 7878 "no arguments for builtin function %<%s%>", 7879 (this->code_ == BUILTIN_PRINT 7880 ? "print" 7881 : "println")); 7882 } 7883 else 7884 { 7885 for (Expression_list::const_iterator p = args->begin(); 7886 p != args->end(); 7887 ++p) 7888 { 7889 Type* type = (*p)->type(); 7890 if (type->is_error() 7891 || type->is_string_type() 7892 || type->integer_type() != NULL 7893 || type->float_type() != NULL 7894 || type->complex_type() != NULL 7895 || type->is_boolean_type() 7896 || type->points_to() != NULL 7897 || type->interface_type() != NULL 7898 || type->channel_type() != NULL 7899 || type->map_type() != NULL 7900 || type->function_type() != NULL 7901 || type->is_slice_type()) 7902 ; 7903 else if ((*p)->is_type_expression()) 7904 { 7905 // If this is a type expression it's going to give 7906 // an error anyhow, so we don't need one here. 7907 } 7908 else 7909 this->report_error(_("unsupported argument type to " 7910 "builtin function")); 7911 } 7912 } 7913 } 7914 break; 7915 7916 case BUILTIN_CLOSE: 7917 if (this->check_one_arg()) 7918 { 7919 if (this->one_arg()->type()->channel_type() == NULL) 7920 this->report_error(_("argument must be channel")); 7921 else if (!this->one_arg()->type()->channel_type()->may_send()) 7922 this->report_error(_("cannot close receive-only channel")); 7923 } 7924 break; 7925 7926 case BUILTIN_PANIC: 7927 case BUILTIN_SIZEOF: 7928 case BUILTIN_ALIGNOF: 7929 this->check_one_arg(); 7930 break; 7931 7932 case BUILTIN_RECOVER: 7933 if (this->args() != NULL 7934 && !this->args()->empty() 7935 && !this->recover_arg_is_set_) 7936 this->report_error(_("too many arguments")); 7937 break; 7938 7939 case BUILTIN_OFFSETOF: 7940 if (this->check_one_arg()) 7941 { 7942 Expression* arg = this->one_arg(); 7943 if (arg->field_reference_expression() == NULL) 7944 this->report_error(_("argument must be a field reference")); 7945 } 7946 break; 7947 7948 case BUILTIN_COPY: 7949 { 7950 const Expression_list* args = this->args(); 7951 if (args == NULL || args->size() < 2) 7952 { 7953 this->report_error(_("not enough arguments")); 7954 break; 7955 } 7956 else if (args->size() > 2) 7957 { 7958 this->report_error(_("too many arguments")); 7959 break; 7960 } 7961 Type* arg1_type = args->front()->type(); 7962 Type* arg2_type = args->back()->type(); 7963 if (arg1_type->is_error() || arg2_type->is_error()) 7964 { 7965 this->set_is_error(); 7966 break; 7967 } 7968 7969 Type* e1; 7970 if (arg1_type->is_slice_type()) 7971 e1 = arg1_type->array_type()->element_type(); 7972 else 7973 { 7974 this->report_error(_("left argument must be a slice")); 7975 break; 7976 } 7977 7978 if (arg2_type->is_slice_type()) 7979 { 7980 Type* e2 = arg2_type->array_type()->element_type(); 7981 if (!Type::are_identical(e1, e2, true, NULL)) 7982 this->report_error(_("element types must be the same")); 7983 } 7984 else if (arg2_type->is_string_type()) 7985 { 7986 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) 7987 this->report_error(_("first argument must be []byte")); 7988 } 7989 else 7990 this->report_error(_("second argument must be slice or string")); 7991 } 7992 break; 7993 7994 case BUILTIN_APPEND: 7995 { 7996 const Expression_list* args = this->args(); 7997 if (args == NULL || args->size() < 2) 7998 { 7999 this->report_error(_("not enough arguments")); 8000 break; 8001 } 8002 if (args->size() > 2) 8003 { 8004 this->report_error(_("too many arguments")); 8005 break; 8006 } 8007 if (args->front()->type()->is_error() 8008 || args->back()->type()->is_error()) 8009 { 8010 this->set_is_error(); 8011 break; 8012 } 8013 8014 Array_type* at = args->front()->type()->array_type(); 8015 Type* e = at->element_type(); 8016 8017 // The language permits appending a string to a []byte, as a 8018 // special case. 8019 if (args->back()->type()->is_string_type()) 8020 { 8021 if (e->integer_type() != NULL && e->integer_type()->is_byte()) 8022 break; 8023 } 8024 8025 // The language says that the second argument must be 8026 // assignable to a slice of the element type of the first 8027 // argument. We already know the first argument is a slice 8028 // type. 8029 Type* arg2_type = Type::make_array_type(e, NULL); 8030 std::string reason; 8031 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) 8032 { 8033 if (reason.empty()) 8034 this->report_error(_("argument 2 has invalid type")); 8035 else 8036 { 8037 error_at(this->location(), "argument 2 has invalid type (%s)", 8038 reason.c_str()); 8039 this->set_is_error(); 8040 } 8041 } 8042 break; 8043 } 8044 8045 case BUILTIN_REAL: 8046 case BUILTIN_IMAG: 8047 if (this->check_one_arg()) 8048 { 8049 if (this->one_arg()->type()->complex_type() == NULL) 8050 this->report_error(_("argument must have complex type")); 8051 } 8052 break; 8053 8054 case BUILTIN_COMPLEX: 8055 { 8056 const Expression_list* args = this->args(); 8057 if (args == NULL || args->size() < 2) 8058 this->report_error(_("not enough arguments")); 8059 else if (args->size() > 2) 8060 this->report_error(_("too many arguments")); 8061 else if (args->front()->is_error_expression() 8062 || args->front()->type()->is_error() 8063 || args->back()->is_error_expression() 8064 || args->back()->type()->is_error()) 8065 this->set_is_error(); 8066 else if (!Type::are_identical(args->front()->type(), 8067 args->back()->type(), true, NULL)) 8068 this->report_error(_("complex arguments must have identical types")); 8069 else if (args->front()->type()->float_type() == NULL) 8070 this->report_error(_("complex arguments must have " 8071 "floating-point type")); 8072 } 8073 break; 8074 8075 default: 8076 go_unreachable(); 8077 } 8078} 8079 8080Expression* 8081Builtin_call_expression::do_copy() 8082{ 8083 Call_expression* bce = 8084 new Builtin_call_expression(this->gogo_, this->fn()->copy(), 8085 this->args()->copy(), 8086 this->is_varargs(), 8087 this->location()); 8088 8089 if (this->varargs_are_lowered()) 8090 bce->set_varargs_are_lowered(); 8091 return bce; 8092} 8093 8094// Return the backend representation for a builtin function. 8095 8096Bexpression* 8097Builtin_call_expression::do_get_backend(Translate_context* context) 8098{ 8099 Gogo* gogo = context->gogo(); 8100 Location location = this->location(); 8101 switch (this->code_) 8102 { 8103 case BUILTIN_INVALID: 8104 case BUILTIN_NEW: 8105 case BUILTIN_MAKE: 8106 go_unreachable(); 8107 8108 case BUILTIN_LEN: 8109 case BUILTIN_CAP: 8110 { 8111 const Expression_list* args = this->args(); 8112 go_assert(args != NULL && args->size() == 1); 8113 Expression* arg = args->front(); 8114 Type* arg_type = arg->type(); 8115 8116 if (this->seen_) 8117 { 8118 go_assert(saw_errors()); 8119 return context->backend()->error_expression(); 8120 } 8121 this->seen_ = true; 8122 this->seen_ = false; 8123 if (arg_type->points_to() != NULL) 8124 { 8125 arg_type = arg_type->points_to(); 8126 go_assert(arg_type->array_type() != NULL 8127 && !arg_type->is_slice_type()); 8128 arg = Expression::make_unary(OPERATOR_MULT, arg, location); 8129 } 8130 8131 Type* int_type = Type::lookup_integer_type("int"); 8132 Expression* val; 8133 if (this->code_ == BUILTIN_LEN) 8134 { 8135 if (arg_type->is_string_type()) 8136 val = Expression::make_string_info(arg, STRING_INFO_LENGTH, 8137 location); 8138 else if (arg_type->array_type() != NULL) 8139 { 8140 if (this->seen_) 8141 { 8142 go_assert(saw_errors()); 8143 return context->backend()->error_expression(); 8144 } 8145 this->seen_ = true; 8146 val = arg_type->array_type()->get_length(gogo, arg); 8147 this->seen_ = false; 8148 } 8149 else if (arg_type->map_type() != NULL) 8150 val = Runtime::make_call(Runtime::MAP_LEN, location, 1, arg); 8151 else if (arg_type->channel_type() != NULL) 8152 val = Runtime::make_call(Runtime::CHAN_LEN, location, 1, arg); 8153 else 8154 go_unreachable(); 8155 } 8156 else 8157 { 8158 if (arg_type->array_type() != NULL) 8159 { 8160 if (this->seen_) 8161 { 8162 go_assert(saw_errors()); 8163 return context->backend()->error_expression(); 8164 } 8165 this->seen_ = true; 8166 val = arg_type->array_type()->get_capacity(gogo, arg); 8167 this->seen_ = false; 8168 } 8169 else if (arg_type->channel_type() != NULL) 8170 val = Runtime::make_call(Runtime::CHAN_CAP, location, 1, arg); 8171 else 8172 go_unreachable(); 8173 } 8174 8175 return Expression::make_cast(int_type, val, 8176 location)->get_backend(context); 8177 } 8178 8179 case BUILTIN_PRINT: 8180 case BUILTIN_PRINTLN: 8181 { 8182 const bool is_ln = this->code_ == BUILTIN_PRINTLN; 8183 Expression* print_stmts = NULL; 8184 8185 const Expression_list* call_args = this->args(); 8186 if (call_args != NULL) 8187 { 8188 for (Expression_list::const_iterator p = call_args->begin(); 8189 p != call_args->end(); 8190 ++p) 8191 { 8192 if (is_ln && p != call_args->begin()) 8193 { 8194 Expression* print_space = 8195 Runtime::make_call(Runtime::PRINT_SPACE, 8196 this->location(), 0); 8197 8198 print_stmts = 8199 Expression::make_compound(print_stmts, print_space, 8200 location); 8201 } 8202 8203 Expression* arg = *p; 8204 Type* type = arg->type(); 8205 Runtime::Function code; 8206 if (type->is_string_type()) 8207 code = Runtime::PRINT_STRING; 8208 else if (type->integer_type() != NULL 8209 && type->integer_type()->is_unsigned()) 8210 { 8211 Type* itype = Type::lookup_integer_type("uint64"); 8212 arg = Expression::make_cast(itype, arg, location); 8213 code = Runtime::PRINT_UINT64; 8214 } 8215 else if (type->integer_type() != NULL) 8216 { 8217 Type* itype = Type::lookup_integer_type("int64"); 8218 arg = Expression::make_cast(itype, arg, location); 8219 code = Runtime::PRINT_INT64; 8220 } 8221 else if (type->float_type() != NULL) 8222 { 8223 Type* dtype = Type::lookup_float_type("float64"); 8224 arg = Expression::make_cast(dtype, arg, location); 8225 code = Runtime::PRINT_DOUBLE; 8226 } 8227 else if (type->complex_type() != NULL) 8228 { 8229 Type* ctype = Type::lookup_complex_type("complex128"); 8230 arg = Expression::make_cast(ctype, arg, location); 8231 code = Runtime::PRINT_COMPLEX; 8232 } 8233 else if (type->is_boolean_type()) 8234 code = Runtime::PRINT_BOOL; 8235 else if (type->points_to() != NULL 8236 || type->channel_type() != NULL 8237 || type->map_type() != NULL 8238 || type->function_type() != NULL) 8239 { 8240 arg = Expression::make_cast(type, arg, location); 8241 code = Runtime::PRINT_POINTER; 8242 } 8243 else if (type->interface_type() != NULL) 8244 { 8245 if (type->interface_type()->is_empty()) 8246 code = Runtime::PRINT_EMPTY_INTERFACE; 8247 else 8248 code = Runtime::PRINT_INTERFACE; 8249 } 8250 else if (type->is_slice_type()) 8251 code = Runtime::PRINT_SLICE; 8252 else 8253 { 8254 go_assert(saw_errors()); 8255 return context->backend()->error_expression(); 8256 } 8257 8258 Expression* call = Runtime::make_call(code, location, 1, arg); 8259 if (print_stmts == NULL) 8260 print_stmts = call; 8261 else 8262 print_stmts = Expression::make_compound(print_stmts, call, 8263 location); 8264 } 8265 } 8266 8267 if (is_ln) 8268 { 8269 Expression* print_nl = 8270 Runtime::make_call(Runtime::PRINT_NL, location, 0); 8271 if (print_stmts == NULL) 8272 print_stmts = print_nl; 8273 else 8274 print_stmts = Expression::make_compound(print_stmts, print_nl, 8275 location); 8276 } 8277 8278 return print_stmts->get_backend(context); 8279 } 8280 8281 case BUILTIN_PANIC: 8282 { 8283 const Expression_list* args = this->args(); 8284 go_assert(args != NULL && args->size() == 1); 8285 Expression* arg = args->front(); 8286 Type *empty = 8287 Type::make_empty_interface_type(Linemap::predeclared_location()); 8288 arg = Expression::convert_for_assignment(gogo, empty, arg, location); 8289 8290 Expression* panic = 8291 Runtime::make_call(Runtime::PANIC, location, 1, arg); 8292 return panic->get_backend(context); 8293 } 8294 8295 case BUILTIN_RECOVER: 8296 { 8297 // The argument is set when building recover thunks. It's a 8298 // boolean value which is true if we can recover a value now. 8299 const Expression_list* args = this->args(); 8300 go_assert(args != NULL && args->size() == 1); 8301 Expression* arg = args->front(); 8302 Type *empty = 8303 Type::make_empty_interface_type(Linemap::predeclared_location()); 8304 8305 Expression* nil = Expression::make_nil(location); 8306 nil = Expression::convert_for_assignment(gogo, empty, nil, location); 8307 8308 // We need to handle a deferred call to recover specially, 8309 // because it changes whether it can recover a panic or not. 8310 // See test7 in test/recover1.go. 8311 Expression* recover = Runtime::make_call((this->is_deferred() 8312 ? Runtime::DEFERRED_RECOVER 8313 : Runtime::RECOVER), 8314 location, 0); 8315 Expression* cond = 8316 Expression::make_conditional(arg, recover, nil, location); 8317 return cond->get_backend(context); 8318 } 8319 8320 case BUILTIN_CLOSE: 8321 { 8322 const Expression_list* args = this->args(); 8323 go_assert(args != NULL && args->size() == 1); 8324 Expression* arg = args->front(); 8325 Expression* close = Runtime::make_call(Runtime::CLOSE, location, 8326 1, arg); 8327 return close->get_backend(context); 8328 } 8329 8330 case BUILTIN_SIZEOF: 8331 case BUILTIN_OFFSETOF: 8332 case BUILTIN_ALIGNOF: 8333 { 8334 Numeric_constant nc; 8335 unsigned long val; 8336 if (!this->numeric_constant_value(&nc) 8337 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) 8338 { 8339 go_assert(saw_errors()); 8340 return context->backend()->error_expression(); 8341 } 8342 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 8343 mpz_t ival; 8344 nc.get_int(&ival); 8345 Expression* int_cst = 8346 Expression::make_integer_z(&ival, uintptr_type, location); 8347 mpz_clear(ival); 8348 return int_cst->get_backend(context); 8349 } 8350 8351 case BUILTIN_COPY: 8352 { 8353 const Expression_list* args = this->args(); 8354 go_assert(args != NULL && args->size() == 2); 8355 Expression* arg1 = args->front(); 8356 Expression* arg2 = args->back(); 8357 8358 Type* arg1_type = arg1->type(); 8359 Array_type* at = arg1_type->array_type(); 8360 go_assert(arg1->is_variable()); 8361 Expression* arg1_val = at->get_value_pointer(gogo, arg1); 8362 Expression* arg1_len = at->get_length(gogo, arg1); 8363 8364 Type* arg2_type = arg2->type(); 8365 go_assert(arg2->is_variable()); 8366 Expression* arg2_val; 8367 Expression* arg2_len; 8368 if (arg2_type->is_slice_type()) 8369 { 8370 at = arg2_type->array_type(); 8371 arg2_val = at->get_value_pointer(gogo, arg2); 8372 arg2_len = at->get_length(gogo, arg2); 8373 } 8374 else 8375 { 8376 go_assert(arg2->is_variable()); 8377 arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, 8378 location); 8379 arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, 8380 location); 8381 } 8382 Expression* cond = 8383 Expression::make_binary(OPERATOR_LT, arg1_len, arg2_len, location); 8384 Expression* length = 8385 Expression::make_conditional(cond, arg1_len, arg2_len, location); 8386 8387 Type* element_type = at->element_type(); 8388 Btype* element_btype = element_type->get_backend(gogo); 8389 int64_t element_size = gogo->backend()->type_size(element_btype); 8390 Expression* size_expr = Expression::make_integer_int64(element_size, 8391 length->type(), 8392 location); 8393 Expression* bytecount = 8394 Expression::make_binary(OPERATOR_MULT, size_expr, length, location); 8395 Expression* copy = Runtime::make_call(Runtime::COPY, location, 3, 8396 arg1_val, arg2_val, bytecount); 8397 8398 Expression* compound = Expression::make_compound(copy, length, location); 8399 return compound->get_backend(context); 8400 } 8401 8402 case BUILTIN_APPEND: 8403 { 8404 const Expression_list* args = this->args(); 8405 go_assert(args != NULL && args->size() == 2); 8406 Expression* arg1 = args->front(); 8407 Expression* arg2 = args->back(); 8408 8409 Array_type* at = arg1->type()->array_type(); 8410 Type* element_type = at->element_type()->forwarded(); 8411 8412 go_assert(arg2->is_variable()); 8413 Expression* arg2_val; 8414 Expression* arg2_len; 8415 int64_t size; 8416 if (arg2->type()->is_string_type() 8417 && element_type->integer_type() != NULL 8418 && element_type->integer_type()->is_byte()) 8419 { 8420 arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, 8421 location); 8422 arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, 8423 location); 8424 size = 1; 8425 } 8426 else 8427 { 8428 arg2_val = at->get_value_pointer(gogo, arg2); 8429 arg2_len = at->get_length(gogo, arg2); 8430 Btype* element_btype = element_type->get_backend(gogo); 8431 size = gogo->backend()->type_size(element_btype); 8432 } 8433 Expression* element_size = 8434 Expression::make_integer_int64(size, NULL, location); 8435 8436 Expression* append = Runtime::make_call(Runtime::APPEND, location, 4, 8437 arg1, arg2_val, arg2_len, 8438 element_size); 8439 append = Expression::make_unsafe_cast(arg1->type(), append, location); 8440 return append->get_backend(context); 8441 } 8442 8443 case BUILTIN_REAL: 8444 case BUILTIN_IMAG: 8445 { 8446 const Expression_list* args = this->args(); 8447 go_assert(args != NULL && args->size() == 1); 8448 8449 Bexpression* ret; 8450 Bexpression* bcomplex = args->front()->get_backend(context); 8451 if (this->code_ == BUILTIN_REAL) 8452 ret = gogo->backend()->real_part_expression(bcomplex, location); 8453 else 8454 ret = gogo->backend()->imag_part_expression(bcomplex, location); 8455 return ret; 8456 } 8457 8458 case BUILTIN_COMPLEX: 8459 { 8460 const Expression_list* args = this->args(); 8461 go_assert(args != NULL && args->size() == 2); 8462 Bexpression* breal = args->front()->get_backend(context); 8463 Bexpression* bimag = args->back()->get_backend(context); 8464 return gogo->backend()->complex_expression(breal, bimag, location); 8465 } 8466 8467 default: 8468 go_unreachable(); 8469 } 8470} 8471 8472// We have to support exporting a builtin call expression, because 8473// code can set a constant to the result of a builtin expression. 8474 8475void 8476Builtin_call_expression::do_export(Export* exp) const 8477{ 8478 Numeric_constant nc; 8479 if (!this->numeric_constant_value(&nc)) 8480 { 8481 error_at(this->location(), "value is not constant"); 8482 return; 8483 } 8484 8485 if (nc.is_int()) 8486 { 8487 mpz_t val; 8488 nc.get_int(&val); 8489 Integer_expression::export_integer(exp, val); 8490 mpz_clear(val); 8491 } 8492 else if (nc.is_float()) 8493 { 8494 mpfr_t fval; 8495 nc.get_float(&fval); 8496 Float_expression::export_float(exp, fval); 8497 mpfr_clear(fval); 8498 } 8499 else if (nc.is_complex()) 8500 { 8501 mpc_t cval; 8502 nc.get_complex(&cval); 8503 Complex_expression::export_complex(exp, cval); 8504 mpc_clear(cval); 8505 } 8506 else 8507 go_unreachable(); 8508 8509 // A trailing space lets us reliably identify the end of the number. 8510 exp->write_c_string(" "); 8511} 8512 8513// Class Call_expression. 8514 8515// A Go function can be viewed in a couple of different ways. The 8516// code of a Go function becomes a backend function with parameters 8517// whose types are simply the backend representation of the Go types. 8518// If there are multiple results, they are returned as a backend 8519// struct. 8520 8521// However, when Go code refers to a function other than simply 8522// calling it, the backend type of that function is actually a struct. 8523// The first field of the struct points to the Go function code 8524// (sometimes a wrapper as described below). The remaining fields 8525// hold addresses of closed-over variables. This struct is called a 8526// closure. 8527 8528// There are a few cases to consider. 8529 8530// A direct function call of a known function in package scope. In 8531// this case there are no closed-over variables, and we know the name 8532// of the function code. We can simply produce a backend call to the 8533// function directly, and not worry about the closure. 8534 8535// A direct function call of a known function literal. In this case 8536// we know the function code and we know the closure. We generate the 8537// function code such that it expects an additional final argument of 8538// the closure type. We pass the closure as the last argument, after 8539// the other arguments. 8540 8541// An indirect function call. In this case we have a closure. We 8542// load the pointer to the function code from the first field of the 8543// closure. We pass the address of the closure as the last argument. 8544 8545// A call to a method of an interface. Type methods are always at 8546// package scope, so we call the function directly, and don't worry 8547// about the closure. 8548 8549// This means that for a function at package scope we have two cases. 8550// One is the direct call, which has no closure. The other is the 8551// indirect call, which does have a closure. We can't simply ignore 8552// the closure, even though it is the last argument, because that will 8553// fail on targets where the function pops its arguments. So when 8554// generating a closure for a package-scope function we set the 8555// function code pointer in the closure to point to a wrapper 8556// function. This wrapper function accepts a final argument that 8557// points to the closure, ignores it, and calls the real function as a 8558// direct function call. This wrapper will normally be efficient, and 8559// can often simply be a tail call to the real function. 8560 8561// We don't use GCC's static chain pointer because 1) we don't need 8562// it; 2) GCC only permits using a static chain to call a known 8563// function, so we can't use it for an indirect call anyhow. Since we 8564// can't use it for an indirect call, we may as well not worry about 8565// using it for a direct call either. 8566 8567// We pass the closure last rather than first because it means that 8568// the function wrapper we put into a closure for a package-scope 8569// function can normally just be a tail call to the real function. 8570 8571// For method expressions we generate a wrapper that loads the 8572// receiver from the closure and then calls the method. This 8573// unfortunately forces reshuffling the arguments, since there is a 8574// new first argument, but we can't avoid reshuffling either for 8575// method expressions or for indirect calls of package-scope 8576// functions, and since the latter are more common we reshuffle for 8577// method expressions. 8578 8579// Note that the Go code retains the Go types. The extra final 8580// argument only appears when we convert to the backend 8581// representation. 8582 8583// Traversal. 8584 8585int 8586Call_expression::do_traverse(Traverse* traverse) 8587{ 8588 // If we are calling a function in a different package that returns 8589 // an unnamed type, this may be the only chance we get to traverse 8590 // that type. We don't traverse this->type_ because it may be a 8591 // Call_multiple_result_type that will just lead back here. 8592 if (this->type_ != NULL && !this->type_->is_error_type()) 8593 { 8594 Function_type *fntype = this->get_function_type(); 8595 if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) 8596 return TRAVERSE_EXIT; 8597 } 8598 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) 8599 return TRAVERSE_EXIT; 8600 if (this->args_ != NULL) 8601 { 8602 if (this->args_->traverse(traverse) == TRAVERSE_EXIT) 8603 return TRAVERSE_EXIT; 8604 } 8605 return TRAVERSE_CONTINUE; 8606} 8607 8608// Lower a call statement. 8609 8610Expression* 8611Call_expression::do_lower(Gogo* gogo, Named_object* function, 8612 Statement_inserter* inserter, int) 8613{ 8614 Location loc = this->location(); 8615 8616 // A type cast can look like a function call. 8617 if (this->fn_->is_type_expression() 8618 && this->args_ != NULL 8619 && this->args_->size() == 1) 8620 return Expression::make_cast(this->fn_->type(), this->args_->front(), 8621 loc); 8622 8623 // Because do_type will return an error type and thus prevent future 8624 // errors, check for that case now to ensure that the error gets 8625 // reported. 8626 Function_type* fntype = this->get_function_type(); 8627 if (fntype == NULL) 8628 { 8629 if (!this->fn_->type()->is_error()) 8630 this->report_error(_("expected function")); 8631 return Expression::make_error(loc); 8632 } 8633 8634 // Handle an argument which is a call to a function which returns 8635 // multiple results. 8636 if (this->args_ != NULL 8637 && this->args_->size() == 1 8638 && this->args_->front()->call_expression() != NULL) 8639 { 8640 size_t rc = this->args_->front()->call_expression()->result_count(); 8641 if (rc > 1 8642 && ((fntype->parameters() != NULL 8643 && (fntype->parameters()->size() == rc 8644 || (fntype->is_varargs() 8645 && fntype->parameters()->size() - 1 <= rc))) 8646 || fntype->is_builtin())) 8647 { 8648 Call_expression* call = this->args_->front()->call_expression(); 8649 call->set_is_multi_value_arg(); 8650 if (this->is_varargs_) 8651 { 8652 // It is not clear which result of a multiple result call 8653 // the ellipsis operator should be applied to. If we unpack the 8654 // the call into its individual results here, the ellipsis will be 8655 // applied to the last result. 8656 error_at(call->location(), 8657 _("multiple-value argument in single-value context")); 8658 return Expression::make_error(call->location()); 8659 } 8660 8661 Expression_list* args = new Expression_list; 8662 for (size_t i = 0; i < rc; ++i) 8663 args->push_back(Expression::make_call_result(call, i)); 8664 // We can't return a new call expression here, because this 8665 // one may be referenced by Call_result expressions. We 8666 // also can't delete the old arguments, because we may still 8667 // traverse them somewhere up the call stack. FIXME. 8668 this->args_ = args; 8669 } 8670 } 8671 8672 // Recognize a call to a builtin function. 8673 if (fntype->is_builtin()) 8674 return new Builtin_call_expression(gogo, this->fn_, this->args_, 8675 this->is_varargs_, loc); 8676 8677 // If this call returns multiple results, create a temporary 8678 // variable for each result. 8679 size_t rc = this->result_count(); 8680 if (rc > 1 && this->results_ == NULL) 8681 { 8682 std::vector<Temporary_statement*>* temps = 8683 new std::vector<Temporary_statement*>; 8684 temps->reserve(rc); 8685 const Typed_identifier_list* results = fntype->results(); 8686 for (Typed_identifier_list::const_iterator p = results->begin(); 8687 p != results->end(); 8688 ++p) 8689 { 8690 Temporary_statement* temp = Statement::make_temporary(p->type(), 8691 NULL, loc); 8692 inserter->insert(temp); 8693 temps->push_back(temp); 8694 } 8695 this->results_ = temps; 8696 } 8697 8698 // Handle a call to a varargs function by packaging up the extra 8699 // parameters. 8700 if (fntype->is_varargs()) 8701 { 8702 const Typed_identifier_list* parameters = fntype->parameters(); 8703 go_assert(parameters != NULL && !parameters->empty()); 8704 Type* varargs_type = parameters->back().type(); 8705 this->lower_varargs(gogo, function, inserter, varargs_type, 8706 parameters->size()); 8707 } 8708 8709 // If this is call to a method, call the method directly passing the 8710 // object as the first parameter. 8711 Bound_method_expression* bme = this->fn_->bound_method_expression(); 8712 if (bme != NULL) 8713 { 8714 Named_object* methodfn = bme->function(); 8715 Expression* first_arg = bme->first_argument(); 8716 8717 // We always pass a pointer when calling a method. 8718 if (first_arg->type()->points_to() == NULL 8719 && !first_arg->type()->is_error()) 8720 { 8721 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); 8722 // We may need to create a temporary variable so that we can 8723 // take the address. We can't do that here because it will 8724 // mess up the order of evaluation. 8725 Unary_expression* ue = static_cast<Unary_expression*>(first_arg); 8726 ue->set_create_temp(); 8727 } 8728 8729 // If we are calling a method which was inherited from an 8730 // embedded struct, and the method did not get a stub, then the 8731 // first type may be wrong. 8732 Type* fatype = bme->first_argument_type(); 8733 if (fatype != NULL) 8734 { 8735 if (fatype->points_to() == NULL) 8736 fatype = Type::make_pointer_type(fatype); 8737 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); 8738 } 8739 8740 Expression_list* new_args = new Expression_list(); 8741 new_args->push_back(first_arg); 8742 if (this->args_ != NULL) 8743 { 8744 for (Expression_list::const_iterator p = this->args_->begin(); 8745 p != this->args_->end(); 8746 ++p) 8747 new_args->push_back(*p); 8748 } 8749 8750 // We have to change in place because this structure may be 8751 // referenced by Call_result_expressions. We can't delete the 8752 // old arguments, because we may be traversing them up in some 8753 // caller. FIXME. 8754 this->args_ = new_args; 8755 this->fn_ = Expression::make_func_reference(methodfn, NULL, 8756 bme->location()); 8757 } 8758 8759 return this; 8760} 8761 8762// Lower a call to a varargs function. FUNCTION is the function in 8763// which the call occurs--it's not the function we are calling. 8764// VARARGS_TYPE is the type of the varargs parameter, a slice type. 8765// PARAM_COUNT is the number of parameters of the function we are 8766// calling; the last of these parameters will be the varargs 8767// parameter. 8768 8769void 8770Call_expression::lower_varargs(Gogo* gogo, Named_object* function, 8771 Statement_inserter* inserter, 8772 Type* varargs_type, size_t param_count) 8773{ 8774 if (this->varargs_are_lowered_) 8775 return; 8776 8777 Location loc = this->location(); 8778 8779 go_assert(param_count > 0); 8780 go_assert(varargs_type->is_slice_type()); 8781 8782 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); 8783 if (arg_count < param_count - 1) 8784 { 8785 // Not enough arguments; will be caught in check_types. 8786 return; 8787 } 8788 8789 Expression_list* old_args = this->args_; 8790 Expression_list* new_args = new Expression_list(); 8791 bool push_empty_arg = false; 8792 if (old_args == NULL || old_args->empty()) 8793 { 8794 go_assert(param_count == 1); 8795 push_empty_arg = true; 8796 } 8797 else 8798 { 8799 Expression_list::const_iterator pa; 8800 int i = 1; 8801 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) 8802 { 8803 if (static_cast<size_t>(i) == param_count) 8804 break; 8805 new_args->push_back(*pa); 8806 } 8807 8808 // We have reached the varargs parameter. 8809 8810 bool issued_error = false; 8811 if (pa == old_args->end()) 8812 push_empty_arg = true; 8813 else if (pa + 1 == old_args->end() && this->is_varargs_) 8814 new_args->push_back(*pa); 8815 else if (this->is_varargs_) 8816 { 8817 if ((*pa)->type()->is_slice_type()) 8818 this->report_error(_("too many arguments")); 8819 else 8820 { 8821 error_at(this->location(), 8822 _("invalid use of %<...%> with non-slice")); 8823 this->set_is_error(); 8824 } 8825 return; 8826 } 8827 else 8828 { 8829 Type* element_type = varargs_type->array_type()->element_type(); 8830 Expression_list* vals = new Expression_list; 8831 for (; pa != old_args->end(); ++pa, ++i) 8832 { 8833 // Check types here so that we get a better message. 8834 Type* patype = (*pa)->type(); 8835 Location paloc = (*pa)->location(); 8836 if (!this->check_argument_type(i, element_type, patype, 8837 paloc, issued_error)) 8838 continue; 8839 vals->push_back(*pa); 8840 } 8841 Expression* val = 8842 Expression::make_slice_composite_literal(varargs_type, vals, loc); 8843 gogo->lower_expression(function, inserter, &val); 8844 new_args->push_back(val); 8845 } 8846 } 8847 8848 if (push_empty_arg) 8849 new_args->push_back(Expression::make_nil(loc)); 8850 8851 // We can't return a new call expression here, because this one may 8852 // be referenced by Call_result expressions. FIXME. We can't 8853 // delete OLD_ARGS because we may have both a Call_expression and a 8854 // Builtin_call_expression which refer to them. FIXME. 8855 this->args_ = new_args; 8856 this->varargs_are_lowered_ = true; 8857} 8858 8859// Flatten a call with multiple results into a temporary. 8860 8861Expression* 8862Call_expression::do_flatten(Gogo* gogo, Named_object*, 8863 Statement_inserter* inserter) 8864{ 8865 if (this->classification() == EXPRESSION_ERROR) 8866 return this; 8867 8868 if (this->is_flattened_) 8869 return this; 8870 this->is_flattened_ = true; 8871 8872 // Add temporary variables for all arguments that require type 8873 // conversion. 8874 Function_type* fntype = this->get_function_type(); 8875 if (fntype == NULL) 8876 { 8877 go_assert(saw_errors()); 8878 return this; 8879 } 8880 if (this->args_ != NULL && !this->args_->empty() 8881 && fntype->parameters() != NULL && !fntype->parameters()->empty()) 8882 { 8883 bool is_interface_method = 8884 this->fn_->interface_field_reference_expression() != NULL; 8885 8886 Expression_list *args = new Expression_list(); 8887 Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); 8888 Expression_list::const_iterator pa = this->args_->begin(); 8889 if (!is_interface_method && fntype->is_method()) 8890 { 8891 // The receiver argument. 8892 args->push_back(*pa); 8893 ++pa; 8894 } 8895 for (; pa != this->args_->end(); ++pa, ++pp) 8896 { 8897 go_assert(pp != fntype->parameters()->end()); 8898 if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) 8899 args->push_back(*pa); 8900 else 8901 { 8902 Location loc = (*pa)->location(); 8903 Expression* arg = *pa; 8904 if (!arg->is_variable()) 8905 { 8906 Temporary_statement *temp = 8907 Statement::make_temporary(NULL, arg, loc); 8908 inserter->insert(temp); 8909 arg = Expression::make_temporary_reference(temp, loc); 8910 } 8911 arg = Expression::convert_for_assignment(gogo, pp->type(), arg, 8912 loc); 8913 args->push_back(arg); 8914 } 8915 } 8916 delete this->args_; 8917 this->args_ = args; 8918 } 8919 8920 size_t rc = this->result_count(); 8921 if (rc > 1 && this->call_temp_ == NULL) 8922 { 8923 Struct_field_list* sfl = new Struct_field_list(); 8924 Function_type* fntype = this->get_function_type(); 8925 const Typed_identifier_list* results = fntype->results(); 8926 Location loc = this->location(); 8927 8928 int i = 0; 8929 char buf[10]; 8930 for (Typed_identifier_list::const_iterator p = results->begin(); 8931 p != results->end(); 8932 ++p, ++i) 8933 { 8934 snprintf(buf, sizeof buf, "res%d", i); 8935 sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); 8936 } 8937 8938 Struct_type* st = Type::make_struct_type(sfl, loc); 8939 this->call_temp_ = Statement::make_temporary(st, NULL, loc); 8940 inserter->insert(this->call_temp_); 8941 } 8942 8943 return this; 8944} 8945 8946// Get the function type. This can return NULL in error cases. 8947 8948Function_type* 8949Call_expression::get_function_type() const 8950{ 8951 return this->fn_->type()->function_type(); 8952} 8953 8954// Return the number of values which this call will return. 8955 8956size_t 8957Call_expression::result_count() const 8958{ 8959 const Function_type* fntype = this->get_function_type(); 8960 if (fntype == NULL) 8961 return 0; 8962 if (fntype->results() == NULL) 8963 return 0; 8964 return fntype->results()->size(); 8965} 8966 8967// Return the temporary which holds a result. 8968 8969Temporary_statement* 8970Call_expression::result(size_t i) const 8971{ 8972 if (this->results_ == NULL || this->results_->size() <= i) 8973 { 8974 go_assert(saw_errors()); 8975 return NULL; 8976 } 8977 return (*this->results_)[i]; 8978} 8979 8980// Set the number of results expected from a call expression. 8981 8982void 8983Call_expression::set_expected_result_count(size_t count) 8984{ 8985 go_assert(this->expected_result_count_ == 0); 8986 this->expected_result_count_ = count; 8987} 8988 8989// Return whether this is a call to the predeclared function recover. 8990 8991bool 8992Call_expression::is_recover_call() const 8993{ 8994 return this->do_is_recover_call(); 8995} 8996 8997// Set the argument to the recover function. 8998 8999void 9000Call_expression::set_recover_arg(Expression* arg) 9001{ 9002 this->do_set_recover_arg(arg); 9003} 9004 9005// Virtual functions also implemented by Builtin_call_expression. 9006 9007bool 9008Call_expression::do_is_recover_call() const 9009{ 9010 return false; 9011} 9012 9013void 9014Call_expression::do_set_recover_arg(Expression*) 9015{ 9016 go_unreachable(); 9017} 9018 9019// We have found an error with this call expression; return true if 9020// we should report it. 9021 9022bool 9023Call_expression::issue_error() 9024{ 9025 if (this->issued_error_) 9026 return false; 9027 else 9028 { 9029 this->issued_error_ = true; 9030 return true; 9031 } 9032} 9033 9034// Get the type. 9035 9036Type* 9037Call_expression::do_type() 9038{ 9039 if (this->type_ != NULL) 9040 return this->type_; 9041 9042 Type* ret; 9043 Function_type* fntype = this->get_function_type(); 9044 if (fntype == NULL) 9045 return Type::make_error_type(); 9046 9047 const Typed_identifier_list* results = fntype->results(); 9048 if (results == NULL) 9049 ret = Type::make_void_type(); 9050 else if (results->size() == 1) 9051 ret = results->begin()->type(); 9052 else 9053 ret = Type::make_call_multiple_result_type(this); 9054 9055 this->type_ = ret; 9056 9057 return this->type_; 9058} 9059 9060// Determine types for a call expression. We can use the function 9061// parameter types to set the types of the arguments. 9062 9063void 9064Call_expression::do_determine_type(const Type_context*) 9065{ 9066 if (!this->determining_types()) 9067 return; 9068 9069 this->fn_->determine_type_no_context(); 9070 Function_type* fntype = this->get_function_type(); 9071 const Typed_identifier_list* parameters = NULL; 9072 if (fntype != NULL) 9073 parameters = fntype->parameters(); 9074 if (this->args_ != NULL) 9075 { 9076 Typed_identifier_list::const_iterator pt; 9077 if (parameters != NULL) 9078 pt = parameters->begin(); 9079 bool first = true; 9080 for (Expression_list::const_iterator pa = this->args_->begin(); 9081 pa != this->args_->end(); 9082 ++pa) 9083 { 9084 if (first) 9085 { 9086 first = false; 9087 // If this is a method, the first argument is the 9088 // receiver. 9089 if (fntype != NULL && fntype->is_method()) 9090 { 9091 Type* rtype = fntype->receiver()->type(); 9092 // The receiver is always passed as a pointer. 9093 if (rtype->points_to() == NULL) 9094 rtype = Type::make_pointer_type(rtype); 9095 Type_context subcontext(rtype, false); 9096 (*pa)->determine_type(&subcontext); 9097 continue; 9098 } 9099 } 9100 9101 if (parameters != NULL && pt != parameters->end()) 9102 { 9103 Type_context subcontext(pt->type(), false); 9104 (*pa)->determine_type(&subcontext); 9105 ++pt; 9106 } 9107 else 9108 (*pa)->determine_type_no_context(); 9109 } 9110 } 9111} 9112 9113// Called when determining types for a Call_expression. Return true 9114// if we should go ahead, false if they have already been determined. 9115 9116bool 9117Call_expression::determining_types() 9118{ 9119 if (this->types_are_determined_) 9120 return false; 9121 else 9122 { 9123 this->types_are_determined_ = true; 9124 return true; 9125 } 9126} 9127 9128// Check types for parameter I. 9129 9130bool 9131Call_expression::check_argument_type(int i, const Type* parameter_type, 9132 const Type* argument_type, 9133 Location argument_location, 9134 bool issued_error) 9135{ 9136 std::string reason; 9137 if (!Type::are_assignable(parameter_type, argument_type, &reason)) 9138 { 9139 if (!issued_error) 9140 { 9141 if (reason.empty()) 9142 error_at(argument_location, "argument %d has incompatible type", i); 9143 else 9144 error_at(argument_location, 9145 "argument %d has incompatible type (%s)", 9146 i, reason.c_str()); 9147 } 9148 this->set_is_error(); 9149 return false; 9150 } 9151 return true; 9152} 9153 9154// Check types. 9155 9156void 9157Call_expression::do_check_types(Gogo*) 9158{ 9159 if (this->classification() == EXPRESSION_ERROR) 9160 return; 9161 9162 Function_type* fntype = this->get_function_type(); 9163 if (fntype == NULL) 9164 { 9165 if (!this->fn_->type()->is_error()) 9166 this->report_error(_("expected function")); 9167 return; 9168 } 9169 9170 if (this->expected_result_count_ != 0 9171 && this->expected_result_count_ != this->result_count()) 9172 { 9173 if (this->issue_error()) 9174 this->report_error(_("function result count mismatch")); 9175 this->set_is_error(); 9176 return; 9177 } 9178 9179 bool is_method = fntype->is_method(); 9180 if (is_method) 9181 { 9182 go_assert(this->args_ != NULL && !this->args_->empty()); 9183 Type* rtype = fntype->receiver()->type(); 9184 Expression* first_arg = this->args_->front(); 9185 // We dereference the values since receivers are always passed 9186 // as pointers. 9187 std::string reason; 9188 if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), 9189 &reason)) 9190 { 9191 if (reason.empty()) 9192 this->report_error(_("incompatible type for receiver")); 9193 else 9194 { 9195 error_at(this->location(), 9196 "incompatible type for receiver (%s)", 9197 reason.c_str()); 9198 this->set_is_error(); 9199 } 9200 } 9201 } 9202 9203 // Note that varargs was handled by the lower_varargs() method, so 9204 // we don't have to worry about it here unless something is wrong. 9205 if (this->is_varargs_ && !this->varargs_are_lowered_) 9206 { 9207 if (!fntype->is_varargs()) 9208 { 9209 error_at(this->location(), 9210 _("invalid use of %<...%> calling non-variadic function")); 9211 this->set_is_error(); 9212 return; 9213 } 9214 } 9215 9216 const Typed_identifier_list* parameters = fntype->parameters(); 9217 if (this->args_ == NULL) 9218 { 9219 if (parameters != NULL && !parameters->empty()) 9220 this->report_error(_("not enough arguments")); 9221 } 9222 else if (parameters == NULL) 9223 { 9224 if (!is_method || this->args_->size() > 1) 9225 this->report_error(_("too many arguments")); 9226 } 9227 else if (this->args_->size() == 1 9228 && this->args_->front()->call_expression() != NULL 9229 && this->args_->front()->call_expression()->result_count() > 1) 9230 { 9231 // This is F(G()) when G returns more than one result. If the 9232 // results can be matched to parameters, it would have been 9233 // lowered in do_lower. If we get here we know there is a 9234 // mismatch. 9235 if (this->args_->front()->call_expression()->result_count() 9236 < parameters->size()) 9237 this->report_error(_("not enough arguments")); 9238 else 9239 this->report_error(_("too many arguments")); 9240 } 9241 else 9242 { 9243 int i = 0; 9244 Expression_list::const_iterator pa = this->args_->begin(); 9245 if (is_method) 9246 ++pa; 9247 for (Typed_identifier_list::const_iterator pt = parameters->begin(); 9248 pt != parameters->end(); 9249 ++pt, ++pa, ++i) 9250 { 9251 if (pa == this->args_->end()) 9252 { 9253 this->report_error(_("not enough arguments")); 9254 return; 9255 } 9256 this->check_argument_type(i + 1, pt->type(), (*pa)->type(), 9257 (*pa)->location(), false); 9258 } 9259 if (pa != this->args_->end()) 9260 this->report_error(_("too many arguments")); 9261 } 9262} 9263 9264Expression* 9265Call_expression::do_copy() 9266{ 9267 Call_expression* call = 9268 Expression::make_call(this->fn_->copy(), 9269 (this->args_ == NULL 9270 ? NULL 9271 : this->args_->copy()), 9272 this->is_varargs_, this->location()); 9273 9274 if (this->varargs_are_lowered_) 9275 call->set_varargs_are_lowered(); 9276 return call; 9277} 9278 9279// Return whether we have to use a temporary variable to ensure that 9280// we evaluate this call expression in order. If the call returns no 9281// results then it will inevitably be executed last. 9282 9283bool 9284Call_expression::do_must_eval_in_order() const 9285{ 9286 return this->result_count() > 0; 9287} 9288 9289// Get the function and the first argument to use when calling an 9290// interface method. 9291 9292Expression* 9293Call_expression::interface_method_function( 9294 Interface_field_reference_expression* interface_method, 9295 Expression** first_arg_ptr) 9296{ 9297 *first_arg_ptr = interface_method->get_underlying_object(); 9298 return interface_method->get_function(); 9299} 9300 9301// Build the call expression. 9302 9303Bexpression* 9304Call_expression::do_get_backend(Translate_context* context) 9305{ 9306 if (this->call_ != NULL) 9307 return this->call_; 9308 9309 Function_type* fntype = this->get_function_type(); 9310 if (fntype == NULL) 9311 return context->backend()->error_expression(); 9312 9313 if (this->fn_->is_error_expression()) 9314 return context->backend()->error_expression(); 9315 9316 Gogo* gogo = context->gogo(); 9317 Location location = this->location(); 9318 9319 Func_expression* func = this->fn_->func_expression(); 9320 Interface_field_reference_expression* interface_method = 9321 this->fn_->interface_field_reference_expression(); 9322 const bool has_closure = func != NULL && func->closure() != NULL; 9323 const bool is_interface_method = interface_method != NULL; 9324 9325 bool has_closure_arg; 9326 if (has_closure) 9327 has_closure_arg = true; 9328 else if (func != NULL) 9329 has_closure_arg = false; 9330 else if (is_interface_method) 9331 has_closure_arg = false; 9332 else 9333 has_closure_arg = true; 9334 9335 int nargs; 9336 std::vector<Bexpression*> fn_args; 9337 if (this->args_ == NULL || this->args_->empty()) 9338 { 9339 nargs = is_interface_method ? 1 : 0; 9340 if (nargs > 0) 9341 fn_args.resize(1); 9342 } 9343 else if (fntype->parameters() == NULL || fntype->parameters()->empty()) 9344 { 9345 // Passing a receiver parameter. 9346 go_assert(!is_interface_method 9347 && fntype->is_method() 9348 && this->args_->size() == 1); 9349 nargs = 1; 9350 fn_args.resize(1); 9351 fn_args[0] = this->args_->front()->get_backend(context); 9352 } 9353 else 9354 { 9355 const Typed_identifier_list* params = fntype->parameters(); 9356 9357 nargs = this->args_->size(); 9358 int i = is_interface_method ? 1 : 0; 9359 nargs += i; 9360 fn_args.resize(nargs); 9361 9362 Typed_identifier_list::const_iterator pp = params->begin(); 9363 Expression_list::const_iterator pe = this->args_->begin(); 9364 if (!is_interface_method && fntype->is_method()) 9365 { 9366 fn_args[i] = (*pe)->get_backend(context); 9367 ++pe; 9368 ++i; 9369 } 9370 for (; pe != this->args_->end(); ++pe, ++pp, ++i) 9371 { 9372 go_assert(pp != params->end()); 9373 Expression* arg = 9374 Expression::convert_for_assignment(gogo, pp->type(), *pe, 9375 location); 9376 fn_args[i] = arg->get_backend(context); 9377 } 9378 go_assert(pp == params->end()); 9379 go_assert(i == nargs); 9380 } 9381 9382 Expression* fn; 9383 Expression* closure = NULL; 9384 if (func != NULL) 9385 { 9386 Named_object* no = func->named_object(); 9387 fn = Expression::make_func_code_reference(no, location); 9388 if (has_closure) 9389 closure = func->closure(); 9390 } 9391 else if (!is_interface_method) 9392 { 9393 closure = this->fn_; 9394 9395 // The backend representation of this function type is a pointer 9396 // to a struct whose first field is the actual function to call. 9397 Type* pfntype = 9398 Type::make_pointer_type( 9399 Type::make_pointer_type(Type::make_void_type())); 9400 fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); 9401 fn = Expression::make_unary(OPERATOR_MULT, fn, location); 9402 } 9403 else 9404 { 9405 Expression* first_arg; 9406 fn = this->interface_method_function(interface_method, &first_arg); 9407 fn_args[0] = first_arg->get_backend(context); 9408 } 9409 9410 Bexpression* bclosure = NULL; 9411 if (has_closure_arg) 9412 bclosure = closure->get_backend(context); 9413 else 9414 go_assert(closure == NULL); 9415 9416 Bexpression* bfn = fn->get_backend(context); 9417 9418 // When not calling a named function directly, use a type conversion 9419 // in case the type of the function is a recursive type which refers 9420 // to itself. We don't do this for an interface method because 1) 9421 // an interface method never refers to itself, so we always have a 9422 // function type here; 2) we pass an extra first argument to an 9423 // interface method, so fntype is not correct. 9424 if (func == NULL && !is_interface_method) 9425 { 9426 Btype* bft = fntype->get_backend_fntype(gogo); 9427 bfn = gogo->backend()->convert_expression(bft, bfn, location); 9428 } 9429 9430 Bexpression* call = gogo->backend()->call_expression(bfn, fn_args, 9431 bclosure, location); 9432 9433 if (this->results_ != NULL) 9434 { 9435 go_assert(this->call_temp_ != NULL); 9436 Expression* call_ref = 9437 Expression::make_temporary_reference(this->call_temp_, location); 9438 Bexpression* bcall_ref = call_ref->get_backend(context); 9439 Bstatement* assn_stmt = 9440 gogo->backend()->assignment_statement(bcall_ref, call, location); 9441 9442 this->call_ = this->set_results(context, bcall_ref); 9443 9444 Bexpression* set_and_call = 9445 gogo->backend()->compound_expression(assn_stmt, this->call_, 9446 location); 9447 return set_and_call; 9448 } 9449 9450 this->call_ = call; 9451 return this->call_; 9452} 9453 9454// Set the result variables if this call returns multiple results. 9455 9456Bexpression* 9457Call_expression::set_results(Translate_context* context, Bexpression* call) 9458{ 9459 Gogo* gogo = context->gogo(); 9460 9461 Bexpression* results = NULL; 9462 Location loc = this->location(); 9463 9464 size_t rc = this->result_count(); 9465 for (size_t i = 0; i < rc; ++i) 9466 { 9467 Temporary_statement* temp = this->result(i); 9468 if (temp == NULL) 9469 { 9470 go_assert(saw_errors()); 9471 return gogo->backend()->error_expression(); 9472 } 9473 Temporary_reference_expression* ref = 9474 Expression::make_temporary_reference(temp, loc); 9475 ref->set_is_lvalue(); 9476 9477 Bexpression* result_ref = ref->get_backend(context); 9478 Bexpression* call_result = 9479 gogo->backend()->struct_field_expression(call, i, loc); 9480 Bstatement* assn_stmt = 9481 gogo->backend()->assignment_statement(result_ref, call_result, loc); 9482 9483 Bexpression* result = 9484 gogo->backend()->compound_expression(assn_stmt, call_result, loc); 9485 9486 if (results == NULL) 9487 results = result; 9488 else 9489 { 9490 Bstatement* expr_stmt = gogo->backend()->expression_statement(result); 9491 results = 9492 gogo->backend()->compound_expression(expr_stmt, results, loc); 9493 } 9494 } 9495 return results; 9496} 9497 9498// Dump ast representation for a call expressin. 9499 9500void 9501Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 9502{ 9503 this->fn_->dump_expression(ast_dump_context); 9504 ast_dump_context->ostream() << "("; 9505 if (args_ != NULL) 9506 ast_dump_context->dump_expression_list(this->args_); 9507 9508 ast_dump_context->ostream() << ") "; 9509} 9510 9511// Make a call expression. 9512 9513Call_expression* 9514Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, 9515 Location location) 9516{ 9517 return new Call_expression(fn, args, is_varargs, location); 9518} 9519 9520// A single result from a call which returns multiple results. 9521 9522class Call_result_expression : public Expression 9523{ 9524 public: 9525 Call_result_expression(Call_expression* call, unsigned int index) 9526 : Expression(EXPRESSION_CALL_RESULT, call->location()), 9527 call_(call), index_(index) 9528 { } 9529 9530 protected: 9531 int 9532 do_traverse(Traverse*); 9533 9534 Type* 9535 do_type(); 9536 9537 void 9538 do_determine_type(const Type_context*); 9539 9540 void 9541 do_check_types(Gogo*); 9542 9543 Expression* 9544 do_copy() 9545 { 9546 return new Call_result_expression(this->call_->call_expression(), 9547 this->index_); 9548 } 9549 9550 bool 9551 do_must_eval_in_order() const 9552 { return true; } 9553 9554 Bexpression* 9555 do_get_backend(Translate_context*); 9556 9557 void 9558 do_dump_expression(Ast_dump_context*) const; 9559 9560 private: 9561 // The underlying call expression. 9562 Expression* call_; 9563 // Which result we want. 9564 unsigned int index_; 9565}; 9566 9567// Traverse a call result. 9568 9569int 9570Call_result_expression::do_traverse(Traverse* traverse) 9571{ 9572 if (traverse->remember_expression(this->call_)) 9573 { 9574 // We have already traversed the call expression. 9575 return TRAVERSE_CONTINUE; 9576 } 9577 return Expression::traverse(&this->call_, traverse); 9578} 9579 9580// Get the type. 9581 9582Type* 9583Call_result_expression::do_type() 9584{ 9585 if (this->classification() == EXPRESSION_ERROR) 9586 return Type::make_error_type(); 9587 9588 // THIS->CALL_ can be replaced with a temporary reference due to 9589 // Call_expression::do_must_eval_in_order when there is an error. 9590 Call_expression* ce = this->call_->call_expression(); 9591 if (ce == NULL) 9592 { 9593 this->set_is_error(); 9594 return Type::make_error_type(); 9595 } 9596 Function_type* fntype = ce->get_function_type(); 9597 if (fntype == NULL) 9598 { 9599 if (ce->issue_error()) 9600 { 9601 if (!ce->fn()->type()->is_error()) 9602 this->report_error(_("expected function")); 9603 } 9604 this->set_is_error(); 9605 return Type::make_error_type(); 9606 } 9607 const Typed_identifier_list* results = fntype->results(); 9608 if (results == NULL || results->size() < 2) 9609 { 9610 if (ce->issue_error()) 9611 this->report_error(_("number of results does not match " 9612 "number of values")); 9613 return Type::make_error_type(); 9614 } 9615 Typed_identifier_list::const_iterator pr = results->begin(); 9616 for (unsigned int i = 0; i < this->index_; ++i) 9617 { 9618 if (pr == results->end()) 9619 break; 9620 ++pr; 9621 } 9622 if (pr == results->end()) 9623 { 9624 if (ce->issue_error()) 9625 this->report_error(_("number of results does not match " 9626 "number of values")); 9627 return Type::make_error_type(); 9628 } 9629 return pr->type(); 9630} 9631 9632// Check the type. Just make sure that we trigger the warning in 9633// do_type. 9634 9635void 9636Call_result_expression::do_check_types(Gogo*) 9637{ 9638 this->type(); 9639} 9640 9641// Determine the type. We have nothing to do here, but the 0 result 9642// needs to pass down to the caller. 9643 9644void 9645Call_result_expression::do_determine_type(const Type_context*) 9646{ 9647 this->call_->determine_type_no_context(); 9648} 9649 9650// Return the backend representation. We just refer to the temporary set by the 9651// call expression. We don't do this at lowering time because it makes it 9652// hard to evaluate the call at the right time. 9653 9654Bexpression* 9655Call_result_expression::do_get_backend(Translate_context* context) 9656{ 9657 Call_expression* ce = this->call_->call_expression(); 9658 if (ce == NULL) 9659 { 9660 go_assert(this->call_->is_error_expression()); 9661 return context->backend()->error_expression(); 9662 } 9663 Temporary_statement* ts = ce->result(this->index_); 9664 if (ts == NULL) 9665 { 9666 go_assert(saw_errors()); 9667 return context->backend()->error_expression(); 9668 } 9669 Expression* ref = Expression::make_temporary_reference(ts, this->location()); 9670 return ref->get_backend(context); 9671} 9672 9673// Dump ast representation for a call result expression. 9674 9675void 9676Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 9677 const 9678{ 9679 // FIXME: Wouldn't it be better if the call is assigned to a temporary 9680 // (struct) and the fields are referenced instead. 9681 ast_dump_context->ostream() << this->index_ << "@("; 9682 ast_dump_context->dump_expression(this->call_); 9683 ast_dump_context->ostream() << ")"; 9684} 9685 9686// Make a reference to a single result of a call which returns 9687// multiple results. 9688 9689Expression* 9690Expression::make_call_result(Call_expression* call, unsigned int index) 9691{ 9692 return new Call_result_expression(call, index); 9693} 9694 9695// Class Index_expression. 9696 9697// Traversal. 9698 9699int 9700Index_expression::do_traverse(Traverse* traverse) 9701{ 9702 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT 9703 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT 9704 || (this->end_ != NULL 9705 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) 9706 || (this->cap_ != NULL 9707 && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) 9708 return TRAVERSE_EXIT; 9709 return TRAVERSE_CONTINUE; 9710} 9711 9712// Lower an index expression. This converts the generic index 9713// expression into an array index, a string index, or a map index. 9714 9715Expression* 9716Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) 9717{ 9718 Location location = this->location(); 9719 Expression* left = this->left_; 9720 Expression* start = this->start_; 9721 Expression* end = this->end_; 9722 Expression* cap = this->cap_; 9723 9724 Type* type = left->type(); 9725 if (type->is_error()) 9726 { 9727 go_assert(saw_errors()); 9728 return Expression::make_error(location); 9729 } 9730 else if (left->is_type_expression()) 9731 { 9732 error_at(location, "attempt to index type expression"); 9733 return Expression::make_error(location); 9734 } 9735 else if (type->array_type() != NULL) 9736 return Expression::make_array_index(left, start, end, cap, location); 9737 else if (type->points_to() != NULL 9738 && type->points_to()->array_type() != NULL 9739 && !type->points_to()->is_slice_type()) 9740 { 9741 Expression* deref = Expression::make_unary(OPERATOR_MULT, left, 9742 location); 9743 9744 // For an ordinary index into the array, the pointer will be 9745 // dereferenced. For a slice it will not--the resulting slice 9746 // will simply reuse the pointer, which is incorrect if that 9747 // pointer is nil. 9748 if (end != NULL || cap != NULL) 9749 deref->issue_nil_check(); 9750 9751 return Expression::make_array_index(deref, start, end, cap, location); 9752 } 9753 else if (type->is_string_type()) 9754 { 9755 if (cap != NULL) 9756 { 9757 error_at(location, "invalid 3-index slice of string"); 9758 return Expression::make_error(location); 9759 } 9760 return Expression::make_string_index(left, start, end, location); 9761 } 9762 else if (type->map_type() != NULL) 9763 { 9764 if (end != NULL || cap != NULL) 9765 { 9766 error_at(location, "invalid slice of map"); 9767 return Expression::make_error(location); 9768 } 9769 Map_index_expression* ret = Expression::make_map_index(left, start, 9770 location); 9771 if (this->is_lvalue_) 9772 ret->set_is_lvalue(); 9773 return ret; 9774 } 9775 else 9776 { 9777 error_at(location, 9778 "attempt to index object which is not array, string, or map"); 9779 return Expression::make_error(location); 9780 } 9781} 9782 9783// Write an indexed expression 9784// (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. 9785 9786void 9787Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, 9788 const Expression* expr, 9789 const Expression* start, 9790 const Expression* end, 9791 const Expression* cap) 9792{ 9793 expr->dump_expression(ast_dump_context); 9794 ast_dump_context->ostream() << "["; 9795 start->dump_expression(ast_dump_context); 9796 if (end != NULL) 9797 { 9798 ast_dump_context->ostream() << ":"; 9799 end->dump_expression(ast_dump_context); 9800 } 9801 if (cap != NULL) 9802 { 9803 ast_dump_context->ostream() << ":"; 9804 cap->dump_expression(ast_dump_context); 9805 } 9806 ast_dump_context->ostream() << "]"; 9807} 9808 9809// Dump ast representation for an index expression. 9810 9811void 9812Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 9813 const 9814{ 9815 Index_expression::dump_index_expression(ast_dump_context, this->left_, 9816 this->start_, this->end_, this->cap_); 9817} 9818 9819// Make an index expression. 9820 9821Expression* 9822Expression::make_index(Expression* left, Expression* start, Expression* end, 9823 Expression* cap, Location location) 9824{ 9825 return new Index_expression(left, start, end, cap, location); 9826} 9827 9828// An array index. This is used for both indexing and slicing. 9829 9830class Array_index_expression : public Expression 9831{ 9832 public: 9833 Array_index_expression(Expression* array, Expression* start, 9834 Expression* end, Expression* cap, Location location) 9835 : Expression(EXPRESSION_ARRAY_INDEX, location), 9836 array_(array), start_(start), end_(end), cap_(cap), type_(NULL) 9837 { } 9838 9839 protected: 9840 int 9841 do_traverse(Traverse*); 9842 9843 Expression* 9844 do_flatten(Gogo*, Named_object*, Statement_inserter*); 9845 9846 Type* 9847 do_type(); 9848 9849 void 9850 do_determine_type(const Type_context*); 9851 9852 void 9853 do_check_types(Gogo*); 9854 9855 Expression* 9856 do_copy() 9857 { 9858 return Expression::make_array_index(this->array_->copy(), 9859 this->start_->copy(), 9860 (this->end_ == NULL 9861 ? NULL 9862 : this->end_->copy()), 9863 (this->cap_ == NULL 9864 ? NULL 9865 : this->cap_->copy()), 9866 this->location()); 9867 } 9868 9869 bool 9870 do_must_eval_subexpressions_in_order(int* skip) const 9871 { 9872 *skip = 1; 9873 return true; 9874 } 9875 9876 bool 9877 do_is_addressable() const; 9878 9879 void 9880 do_address_taken(bool escapes) 9881 { this->array_->address_taken(escapes); } 9882 9883 void 9884 do_issue_nil_check() 9885 { this->array_->issue_nil_check(); } 9886 9887 Bexpression* 9888 do_get_backend(Translate_context*); 9889 9890 void 9891 do_dump_expression(Ast_dump_context*) const; 9892 9893 private: 9894 // The array we are getting a value from. 9895 Expression* array_; 9896 // The start or only index. 9897 Expression* start_; 9898 // The end index of a slice. This may be NULL for a simple array 9899 // index, or it may be a nil expression for the length of the array. 9900 Expression* end_; 9901 // The capacity argument of a slice. This may be NULL for an array index or 9902 // slice. 9903 Expression* cap_; 9904 // The type of the expression. 9905 Type* type_; 9906}; 9907 9908// Array index traversal. 9909 9910int 9911Array_index_expression::do_traverse(Traverse* traverse) 9912{ 9913 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) 9914 return TRAVERSE_EXIT; 9915 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) 9916 return TRAVERSE_EXIT; 9917 if (this->end_ != NULL) 9918 { 9919 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) 9920 return TRAVERSE_EXIT; 9921 } 9922 if (this->cap_ != NULL) 9923 { 9924 if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) 9925 return TRAVERSE_EXIT; 9926 } 9927 return TRAVERSE_CONTINUE; 9928} 9929 9930// Return the type of an array index. 9931 9932Type* 9933Array_index_expression::do_type() 9934{ 9935 if (this->type_ == NULL) 9936 { 9937 Array_type* type = this->array_->type()->array_type(); 9938 if (type == NULL) 9939 this->type_ = Type::make_error_type(); 9940 else if (this->end_ == NULL) 9941 this->type_ = type->element_type(); 9942 else if (type->is_slice_type()) 9943 { 9944 // A slice of a slice has the same type as the original 9945 // slice. 9946 this->type_ = this->array_->type()->deref(); 9947 } 9948 else 9949 { 9950 // A slice of an array is a slice. 9951 this->type_ = Type::make_array_type(type->element_type(), NULL); 9952 } 9953 } 9954 return this->type_; 9955} 9956 9957// Set the type of an array index. 9958 9959void 9960Array_index_expression::do_determine_type(const Type_context*) 9961{ 9962 this->array_->determine_type_no_context(); 9963 this->start_->determine_type_no_context(); 9964 if (this->end_ != NULL) 9965 this->end_->determine_type_no_context(); 9966 if (this->cap_ != NULL) 9967 this->cap_->determine_type_no_context(); 9968} 9969 9970// Check types of an array index. 9971 9972void 9973Array_index_expression::do_check_types(Gogo*) 9974{ 9975 Numeric_constant nc; 9976 unsigned long v; 9977 if (this->start_->type()->integer_type() == NULL 9978 && !this->start_->type()->is_error() 9979 && (!this->start_->numeric_constant_value(&nc) 9980 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 9981 this->report_error(_("index must be integer")); 9982 if (this->end_ != NULL 9983 && this->end_->type()->integer_type() == NULL 9984 && !this->end_->type()->is_error() 9985 && !this->end_->is_nil_expression() 9986 && !this->end_->is_error_expression() 9987 && (!this->end_->numeric_constant_value(&nc) 9988 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 9989 this->report_error(_("slice end must be integer")); 9990 if (this->cap_ != NULL 9991 && this->cap_->type()->integer_type() == NULL 9992 && !this->cap_->type()->is_error() 9993 && !this->cap_->is_nil_expression() 9994 && !this->cap_->is_error_expression() 9995 && (!this->cap_->numeric_constant_value(&nc) 9996 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 9997 this->report_error(_("slice capacity must be integer")); 9998 9999 Array_type* array_type = this->array_->type()->array_type(); 10000 if (array_type == NULL) 10001 { 10002 go_assert(this->array_->type()->is_error()); 10003 return; 10004 } 10005 10006 unsigned int int_bits = 10007 Type::lookup_integer_type("int")->integer_type()->bits(); 10008 10009 Numeric_constant lvalnc; 10010 mpz_t lval; 10011 bool lval_valid = (array_type->length() != NULL 10012 && array_type->length()->numeric_constant_value(&lvalnc) 10013 && lvalnc.to_int(&lval)); 10014 Numeric_constant inc; 10015 mpz_t ival; 10016 bool ival_valid = false; 10017 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) 10018 { 10019 ival_valid = true; 10020 if (mpz_sgn(ival) < 0 10021 || mpz_sizeinbase(ival, 2) >= int_bits 10022 || (lval_valid 10023 && (this->end_ == NULL 10024 ? mpz_cmp(ival, lval) >= 0 10025 : mpz_cmp(ival, lval) > 0))) 10026 { 10027 error_at(this->start_->location(), "array index out of bounds"); 10028 this->set_is_error(); 10029 } 10030 } 10031 if (this->end_ != NULL && !this->end_->is_nil_expression()) 10032 { 10033 Numeric_constant enc; 10034 mpz_t eval; 10035 bool eval_valid = false; 10036 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) 10037 { 10038 eval_valid = true; 10039 if (mpz_sgn(eval) < 0 10040 || mpz_sizeinbase(eval, 2) >= int_bits 10041 || (lval_valid && mpz_cmp(eval, lval) > 0)) 10042 { 10043 error_at(this->end_->location(), "array index out of bounds"); 10044 this->set_is_error(); 10045 } 10046 else if (ival_valid && mpz_cmp(ival, eval) > 0) 10047 this->report_error(_("inverted slice range")); 10048 } 10049 10050 Numeric_constant cnc; 10051 mpz_t cval; 10052 if (this->cap_ != NULL 10053 && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) 10054 { 10055 if (mpz_sgn(cval) < 0 10056 || mpz_sizeinbase(cval, 2) >= int_bits 10057 || (lval_valid && mpz_cmp(cval, lval) > 0)) 10058 { 10059 error_at(this->cap_->location(), "array index out of bounds"); 10060 this->set_is_error(); 10061 } 10062 else if (ival_valid && mpz_cmp(ival, cval) > 0) 10063 { 10064 error_at(this->cap_->location(), 10065 "invalid slice index: capacity less than start"); 10066 this->set_is_error(); 10067 } 10068 else if (eval_valid && mpz_cmp(eval, cval) > 0) 10069 { 10070 error_at(this->cap_->location(), 10071 "invalid slice index: capacity less than length"); 10072 this->set_is_error(); 10073 } 10074 mpz_clear(cval); 10075 } 10076 10077 if (eval_valid) 10078 mpz_clear(eval); 10079 } 10080 if (ival_valid) 10081 mpz_clear(ival); 10082 if (lval_valid) 10083 mpz_clear(lval); 10084 10085 // A slice of an array requires an addressable array. A slice of a 10086 // slice is always possible. 10087 if (this->end_ != NULL && !array_type->is_slice_type()) 10088 { 10089 if (!this->array_->is_addressable()) 10090 this->report_error(_("slice of unaddressable value")); 10091 else 10092 this->array_->address_taken(true); 10093 } 10094} 10095 10096// Flatten array indexing by using temporary variables for slices and indexes. 10097 10098Expression* 10099Array_index_expression::do_flatten(Gogo*, Named_object*, 10100 Statement_inserter* inserter) 10101{ 10102 Location loc = this->location(); 10103 Temporary_statement* temp; 10104 if (this->array_->type()->is_slice_type() && !this->array_->is_variable()) 10105 { 10106 temp = Statement::make_temporary(NULL, this->array_, loc); 10107 inserter->insert(temp); 10108 this->array_ = Expression::make_temporary_reference(temp, loc); 10109 } 10110 if (!this->start_->is_variable()) 10111 { 10112 temp = Statement::make_temporary(NULL, this->start_, loc); 10113 inserter->insert(temp); 10114 this->start_ = Expression::make_temporary_reference(temp, loc); 10115 } 10116 if (this->end_ != NULL 10117 && !this->end_->is_nil_expression() 10118 && !this->end_->is_variable()) 10119 { 10120 temp = Statement::make_temporary(NULL, this->end_, loc); 10121 inserter->insert(temp); 10122 this->end_ = Expression::make_temporary_reference(temp, loc); 10123 } 10124 if (this->cap_ != NULL && !this->cap_->is_variable()) 10125 { 10126 temp = Statement::make_temporary(NULL, this->cap_, loc); 10127 inserter->insert(temp); 10128 this->cap_ = Expression::make_temporary_reference(temp, loc); 10129 } 10130 10131 return this; 10132} 10133 10134// Return whether this expression is addressable. 10135 10136bool 10137Array_index_expression::do_is_addressable() const 10138{ 10139 // A slice expression is not addressable. 10140 if (this->end_ != NULL) 10141 return false; 10142 10143 // An index into a slice is addressable. 10144 if (this->array_->type()->is_slice_type()) 10145 return true; 10146 10147 // An index into an array is addressable if the array is 10148 // addressable. 10149 return this->array_->is_addressable(); 10150} 10151 10152// Get the backend representation for an array index. 10153 10154Bexpression* 10155Array_index_expression::do_get_backend(Translate_context* context) 10156{ 10157 Array_type* array_type = this->array_->type()->array_type(); 10158 if (array_type == NULL) 10159 { 10160 go_assert(this->array_->type()->is_error()); 10161 return context->backend()->error_expression(); 10162 } 10163 go_assert(!array_type->is_slice_type() || this->array_->is_variable()); 10164 10165 Location loc = this->location(); 10166 Gogo* gogo = context->gogo(); 10167 10168 Type* int_type = Type::lookup_integer_type("int"); 10169 Btype* int_btype = int_type->get_backend(gogo); 10170 10171 // We need to convert the length and capacity to the Go "int" type here 10172 // because the length of a fixed-length array could be of type "uintptr" 10173 // and gimple disallows binary operations between "uintptr" and other 10174 // integer types. FIXME. 10175 Bexpression* length = NULL; 10176 if (this->end_ == NULL || this->end_->is_nil_expression()) 10177 { 10178 Expression* len = array_type->get_length(gogo, this->array_); 10179 length = len->get_backend(context); 10180 length = gogo->backend()->convert_expression(int_btype, length, loc); 10181 } 10182 10183 Bexpression* capacity = NULL; 10184 if (this->end_ != NULL) 10185 { 10186 Expression* cap = array_type->get_capacity(gogo, this->array_); 10187 capacity = cap->get_backend(context); 10188 capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); 10189 } 10190 10191 Bexpression* cap_arg = capacity; 10192 if (this->cap_ != NULL) 10193 { 10194 cap_arg = this->cap_->get_backend(context); 10195 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); 10196 } 10197 10198 if (length == NULL) 10199 length = cap_arg; 10200 10201 int code = (array_type->length() != NULL 10202 ? (this->end_ == NULL 10203 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS 10204 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) 10205 : (this->end_ == NULL 10206 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS 10207 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); 10208 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); 10209 10210 if (this->start_->type()->integer_type() == NULL 10211 && !Type::are_convertible(int_type, this->start_->type(), NULL)) 10212 { 10213 go_assert(saw_errors()); 10214 return context->backend()->error_expression(); 10215 } 10216 10217 Bexpression* bad_index = 10218 Expression::check_bounds(this->start_, loc)->get_backend(context); 10219 10220 Bexpression* start = this->start_->get_backend(context); 10221 start = gogo->backend()->convert_expression(int_btype, start, loc); 10222 Bexpression* start_too_large = 10223 gogo->backend()->binary_expression((this->end_ == NULL 10224 ? OPERATOR_GE 10225 : OPERATOR_GT), 10226 start, 10227 (this->end_ == NULL 10228 ? length 10229 : capacity), 10230 loc); 10231 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, 10232 bad_index, loc); 10233 10234 if (this->end_ == NULL) 10235 { 10236 // Simple array indexing. This has to return an l-value, so 10237 // wrap the index check into START. 10238 start = 10239 gogo->backend()->conditional_expression(int_btype, bad_index, 10240 crash, start, loc); 10241 10242 Bexpression* ret; 10243 if (array_type->length() != NULL) 10244 { 10245 Bexpression* array = this->array_->get_backend(context); 10246 ret = gogo->backend()->array_index_expression(array, start, loc); 10247 } 10248 else 10249 { 10250 // Slice. 10251 Expression* valptr = 10252 array_type->get_value_pointer(gogo, this->array_); 10253 Bexpression* ptr = valptr->get_backend(context); 10254 ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); 10255 10256 Type* ele_type = this->array_->type()->array_type()->element_type(); 10257 Btype* ele_btype = ele_type->get_backend(gogo); 10258 ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); 10259 } 10260 return ret; 10261 } 10262 10263 // Array slice. 10264 10265 if (this->cap_ != NULL) 10266 { 10267 Bexpression* bounds_bcheck = 10268 Expression::check_bounds(this->cap_, loc)->get_backend(context); 10269 bad_index = 10270 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, 10271 bad_index, loc); 10272 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); 10273 10274 Bexpression* cap_too_small = 10275 gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); 10276 Bexpression* cap_too_large = 10277 gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); 10278 Bexpression* bad_cap = 10279 gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, 10280 cap_too_large, loc); 10281 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, 10282 bad_index, loc); 10283 } 10284 10285 Bexpression* end; 10286 if (this->end_->is_nil_expression()) 10287 end = length; 10288 else 10289 { 10290 Bexpression* bounds_bcheck = 10291 Expression::check_bounds(this->end_, loc)->get_backend(context); 10292 10293 bad_index = 10294 gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, 10295 bad_index, loc); 10296 10297 end = this->end_->get_backend(context); 10298 end = gogo->backend()->convert_expression(int_btype, end, loc); 10299 Bexpression* end_too_small = 10300 gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); 10301 Bexpression* end_too_large = 10302 gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); 10303 Bexpression* bad_end = 10304 gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, 10305 end_too_large, loc); 10306 bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, 10307 bad_index, loc); 10308 } 10309 10310 Expression* valptr = array_type->get_value_pointer(gogo, this->array_); 10311 Bexpression* val = valptr->get_backend(context); 10312 val = gogo->backend()->pointer_offset_expression(val, start, loc); 10313 10314 Bexpression* result_length = 10315 gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); 10316 10317 Bexpression* result_capacity = 10318 gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); 10319 10320 Btype* struct_btype = this->type()->get_backend(gogo); 10321 std::vector<Bexpression*> init; 10322 init.push_back(val); 10323 init.push_back(result_length); 10324 init.push_back(result_capacity); 10325 10326 Bexpression* ctor = 10327 gogo->backend()->constructor_expression(struct_btype, init, loc); 10328 return gogo->backend()->conditional_expression(struct_btype, bad_index, 10329 crash, ctor, loc); 10330} 10331 10332// Dump ast representation for an array index expression. 10333 10334void 10335Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 10336 const 10337{ 10338 Index_expression::dump_index_expression(ast_dump_context, this->array_, 10339 this->start_, this->end_, this->cap_); 10340} 10341 10342// Make an array index expression. END and CAP may be NULL. 10343 10344Expression* 10345Expression::make_array_index(Expression* array, Expression* start, 10346 Expression* end, Expression* cap, 10347 Location location) 10348{ 10349 return new Array_index_expression(array, start, end, cap, location); 10350} 10351 10352// A string index. This is used for both indexing and slicing. 10353 10354class String_index_expression : public Expression 10355{ 10356 public: 10357 String_index_expression(Expression* string, Expression* start, 10358 Expression* end, Location location) 10359 : Expression(EXPRESSION_STRING_INDEX, location), 10360 string_(string), start_(start), end_(end) 10361 { } 10362 10363 protected: 10364 int 10365 do_traverse(Traverse*); 10366 10367 Expression* 10368 do_flatten(Gogo*, Named_object*, Statement_inserter*); 10369 10370 Type* 10371 do_type(); 10372 10373 void 10374 do_determine_type(const Type_context*); 10375 10376 void 10377 do_check_types(Gogo*); 10378 10379 Expression* 10380 do_copy() 10381 { 10382 return Expression::make_string_index(this->string_->copy(), 10383 this->start_->copy(), 10384 (this->end_ == NULL 10385 ? NULL 10386 : this->end_->copy()), 10387 this->location()); 10388 } 10389 10390 bool 10391 do_must_eval_subexpressions_in_order(int* skip) const 10392 { 10393 *skip = 1; 10394 return true; 10395 } 10396 10397 Bexpression* 10398 do_get_backend(Translate_context*); 10399 10400 void 10401 do_dump_expression(Ast_dump_context*) const; 10402 10403 private: 10404 // The string we are getting a value from. 10405 Expression* string_; 10406 // The start or only index. 10407 Expression* start_; 10408 // The end index of a slice. This may be NULL for a single index, 10409 // or it may be a nil expression for the length of the string. 10410 Expression* end_; 10411}; 10412 10413// String index traversal. 10414 10415int 10416String_index_expression::do_traverse(Traverse* traverse) 10417{ 10418 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) 10419 return TRAVERSE_EXIT; 10420 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) 10421 return TRAVERSE_EXIT; 10422 if (this->end_ != NULL) 10423 { 10424 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) 10425 return TRAVERSE_EXIT; 10426 } 10427 return TRAVERSE_CONTINUE; 10428} 10429 10430Expression* 10431String_index_expression::do_flatten(Gogo*, Named_object*, 10432 Statement_inserter* inserter) 10433{ 10434 Temporary_statement* temp; 10435 Location loc = this->location(); 10436 if (!this->string_->is_variable()) 10437 { 10438 temp = Statement::make_temporary(NULL, this->string_, loc); 10439 inserter->insert(temp); 10440 this->string_ = Expression::make_temporary_reference(temp, loc); 10441 } 10442 if (!this->start_->is_variable()) 10443 { 10444 temp = Statement::make_temporary(NULL, this->start_, loc); 10445 inserter->insert(temp); 10446 this->start_ = Expression::make_temporary_reference(temp, loc); 10447 } 10448 if (this->end_ != NULL 10449 && !this->end_->is_nil_expression() 10450 && !this->end_->is_variable()) 10451 { 10452 temp = Statement::make_temporary(NULL, this->end_, loc); 10453 inserter->insert(temp); 10454 this->end_ = Expression::make_temporary_reference(temp, loc); 10455 } 10456 10457 return this; 10458} 10459 10460// Return the type of a string index. 10461 10462Type* 10463String_index_expression::do_type() 10464{ 10465 if (this->end_ == NULL) 10466 return Type::lookup_integer_type("uint8"); 10467 else 10468 return this->string_->type(); 10469} 10470 10471// Determine the type of a string index. 10472 10473void 10474String_index_expression::do_determine_type(const Type_context*) 10475{ 10476 this->string_->determine_type_no_context(); 10477 this->start_->determine_type_no_context(); 10478 if (this->end_ != NULL) 10479 this->end_->determine_type_no_context(); 10480} 10481 10482// Check types of a string index. 10483 10484void 10485String_index_expression::do_check_types(Gogo*) 10486{ 10487 Numeric_constant nc; 10488 unsigned long v; 10489 if (this->start_->type()->integer_type() == NULL 10490 && !this->start_->type()->is_error() 10491 && (!this->start_->numeric_constant_value(&nc) 10492 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 10493 this->report_error(_("index must be integer")); 10494 if (this->end_ != NULL 10495 && this->end_->type()->integer_type() == NULL 10496 && !this->end_->type()->is_error() 10497 && !this->end_->is_nil_expression() 10498 && !this->end_->is_error_expression() 10499 && (!this->end_->numeric_constant_value(&nc) 10500 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 10501 this->report_error(_("slice end must be integer")); 10502 10503 std::string sval; 10504 bool sval_valid = this->string_->string_constant_value(&sval); 10505 10506 Numeric_constant inc; 10507 mpz_t ival; 10508 bool ival_valid = false; 10509 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) 10510 { 10511 ival_valid = true; 10512 if (mpz_sgn(ival) < 0 10513 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) 10514 { 10515 error_at(this->start_->location(), "string index out of bounds"); 10516 this->set_is_error(); 10517 } 10518 } 10519 if (this->end_ != NULL && !this->end_->is_nil_expression()) 10520 { 10521 Numeric_constant enc; 10522 mpz_t eval; 10523 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) 10524 { 10525 if (mpz_sgn(eval) < 0 10526 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) 10527 { 10528 error_at(this->end_->location(), "string index out of bounds"); 10529 this->set_is_error(); 10530 } 10531 else if (ival_valid && mpz_cmp(ival, eval) > 0) 10532 this->report_error(_("inverted slice range")); 10533 mpz_clear(eval); 10534 } 10535 } 10536 if (ival_valid) 10537 mpz_clear(ival); 10538} 10539 10540// Get the backend representation for a string index. 10541 10542Bexpression* 10543String_index_expression::do_get_backend(Translate_context* context) 10544{ 10545 Location loc = this->location(); 10546 Expression* string_arg = this->string_; 10547 if (this->string_->type()->points_to() != NULL) 10548 string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc); 10549 10550 Expression* bad_index = Expression::check_bounds(this->start_, loc); 10551 10552 int code = (this->end_ == NULL 10553 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS 10554 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); 10555 10556 Gogo* gogo = context->gogo(); 10557 Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); 10558 10559 Type* int_type = Type::lookup_integer_type("int"); 10560 10561 // It is possible that an error occurred earlier because the start index 10562 // cannot be represented as an integer type. In this case, we shouldn't 10563 // try casting the starting index into an integer since 10564 // Type_conversion_expression will fail to get the backend representation. 10565 // FIXME. 10566 if (this->start_->type()->integer_type() == NULL 10567 && !Type::are_convertible(int_type, this->start_->type(), NULL)) 10568 { 10569 go_assert(saw_errors()); 10570 return context->backend()->error_expression(); 10571 } 10572 10573 Expression* start = Expression::make_cast(int_type, this->start_, loc); 10574 10575 if (this->end_ == NULL) 10576 { 10577 Expression* length = 10578 Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); 10579 10580 Expression* start_too_large = 10581 Expression::make_binary(OPERATOR_GE, start, length, loc); 10582 bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, 10583 bad_index, loc); 10584 Expression* bytes = 10585 Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); 10586 10587 Bexpression* bstart = start->get_backend(context); 10588 Bexpression* ptr = bytes->get_backend(context); 10589 ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); 10590 Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); 10591 Bexpression* index = 10592 gogo->backend()->indirect_expression(ubtype, ptr, true, loc); 10593 10594 Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); 10595 Bexpression* index_error = bad_index->get_backend(context); 10596 return gogo->backend()->conditional_expression(byte_btype, index_error, 10597 crash, index, loc); 10598 } 10599 10600 Expression* end = NULL; 10601 if (this->end_->is_nil_expression()) 10602 end = Expression::make_integer_sl(-1, int_type, loc); 10603 else 10604 { 10605 Expression* bounds_check = Expression::check_bounds(this->end_, loc); 10606 bad_index = 10607 Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); 10608 end = Expression::make_cast(int_type, this->end_, loc); 10609 } 10610 10611 Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, 10612 string_arg, start, end); 10613 Bexpression* bstrslice = strslice->get_backend(context); 10614 10615 Btype* str_btype = strslice->type()->get_backend(gogo); 10616 Bexpression* index_error = bad_index->get_backend(context); 10617 return gogo->backend()->conditional_expression(str_btype, index_error, 10618 crash, bstrslice, loc); 10619} 10620 10621// Dump ast representation for a string index expression. 10622 10623void 10624String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 10625 const 10626{ 10627 Index_expression::dump_index_expression(ast_dump_context, this->string_, 10628 this->start_, this->end_, NULL); 10629} 10630 10631// Make a string index expression. END may be NULL. 10632 10633Expression* 10634Expression::make_string_index(Expression* string, Expression* start, 10635 Expression* end, Location location) 10636{ 10637 return new String_index_expression(string, start, end, location); 10638} 10639 10640// Class Map_index. 10641 10642// Get the type of the map. 10643 10644Map_type* 10645Map_index_expression::get_map_type() const 10646{ 10647 Map_type* mt = this->map_->type()->deref()->map_type(); 10648 if (mt == NULL) 10649 go_assert(saw_errors()); 10650 return mt; 10651} 10652 10653// Map index traversal. 10654 10655int 10656Map_index_expression::do_traverse(Traverse* traverse) 10657{ 10658 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) 10659 return TRAVERSE_EXIT; 10660 return Expression::traverse(&this->index_, traverse); 10661} 10662 10663// We need to pass in a pointer to the key, so flatten the index into a 10664// temporary variable if it isn't already. The value pointer will be 10665// dereferenced and checked for nil, so flatten into a temporary to avoid 10666// recomputation. 10667 10668Expression* 10669Map_index_expression::do_flatten(Gogo* gogo, Named_object*, 10670 Statement_inserter* inserter) 10671{ 10672 Location loc = this->location(); 10673 Map_type* mt = this->get_map_type(); 10674 if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) 10675 { 10676 if (this->index_->type()->interface_type() != NULL 10677 && !this->index_->is_variable()) 10678 { 10679 Temporary_statement* temp = 10680 Statement::make_temporary(NULL, this->index_, loc); 10681 inserter->insert(temp); 10682 this->index_ = Expression::make_temporary_reference(temp, loc); 10683 } 10684 this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), 10685 this->index_, loc); 10686 } 10687 10688 if (!this->index_->is_variable()) 10689 { 10690 Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, 10691 loc); 10692 inserter->insert(temp); 10693 this->index_ = Expression::make_temporary_reference(temp, loc); 10694 } 10695 10696 if (this->value_pointer_ == NULL) 10697 this->get_value_pointer(this->is_lvalue_); 10698 if (!this->value_pointer_->is_variable()) 10699 { 10700 Temporary_statement* temp = 10701 Statement::make_temporary(NULL, this->value_pointer_, loc); 10702 inserter->insert(temp); 10703 this->value_pointer_ = Expression::make_temporary_reference(temp, loc); 10704 } 10705 10706 return this; 10707} 10708 10709// Return the type of a map index. 10710 10711Type* 10712Map_index_expression::do_type() 10713{ 10714 Map_type* mt = this->get_map_type(); 10715 if (mt == NULL) 10716 return Type::make_error_type(); 10717 Type* type = mt->val_type(); 10718 // If this map index is in a tuple assignment, we actually return a 10719 // pointer to the value type. Tuple_map_assignment_statement is 10720 // responsible for handling this correctly. We need to get the type 10721 // right in case this gets assigned to a temporary variable. 10722 if (this->is_in_tuple_assignment_) 10723 type = Type::make_pointer_type(type); 10724 return type; 10725} 10726 10727// Fix the type of a map index. 10728 10729void 10730Map_index_expression::do_determine_type(const Type_context*) 10731{ 10732 this->map_->determine_type_no_context(); 10733 Map_type* mt = this->get_map_type(); 10734 Type* key_type = mt == NULL ? NULL : mt->key_type(); 10735 Type_context subcontext(key_type, false); 10736 this->index_->determine_type(&subcontext); 10737} 10738 10739// Check types of a map index. 10740 10741void 10742Map_index_expression::do_check_types(Gogo*) 10743{ 10744 std::string reason; 10745 Map_type* mt = this->get_map_type(); 10746 if (mt == NULL) 10747 return; 10748 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) 10749 { 10750 if (reason.empty()) 10751 this->report_error(_("incompatible type for map index")); 10752 else 10753 { 10754 error_at(this->location(), "incompatible type for map index (%s)", 10755 reason.c_str()); 10756 this->set_is_error(); 10757 } 10758 } 10759} 10760 10761// Get the backend representation for a map index. 10762 10763Bexpression* 10764Map_index_expression::do_get_backend(Translate_context* context) 10765{ 10766 Map_type* type = this->get_map_type(); 10767 if (type == NULL) 10768 { 10769 go_assert(saw_errors()); 10770 return context->backend()->error_expression(); 10771 } 10772 10773 go_assert(this->value_pointer_ != NULL 10774 && this->value_pointer_->is_variable()); 10775 10776 Bexpression* ret; 10777 if (this->is_lvalue_) 10778 { 10779 Expression* val = 10780 Expression::make_unary(OPERATOR_MULT, this->value_pointer_, 10781 this->location()); 10782 ret = val->get_backend(context); 10783 } 10784 else if (this->is_in_tuple_assignment_) 10785 { 10786 // Tuple_map_assignment_statement is responsible for using this 10787 // appropriately. 10788 ret = this->value_pointer_->get_backend(context); 10789 } 10790 else 10791 { 10792 Location loc = this->location(); 10793 10794 Expression* nil_check = 10795 Expression::make_binary(OPERATOR_EQEQ, this->value_pointer_, 10796 Expression::make_nil(loc), loc); 10797 Bexpression* bnil_check = nil_check->get_backend(context); 10798 Expression* val = 10799 Expression::make_unary(OPERATOR_MULT, this->value_pointer_, loc); 10800 Bexpression* bval = val->get_backend(context); 10801 10802 Gogo* gogo = context->gogo(); 10803 Btype* val_btype = type->val_type()->get_backend(gogo); 10804 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); 10805 ret = gogo->backend()->conditional_expression(val_btype, bnil_check, 10806 val_zero, bval, loc); 10807 } 10808 return ret; 10809} 10810 10811// Get an expression for the map index. This returns an expression which 10812// evaluates to a pointer to a value. The pointer will be NULL if the key is 10813// not in the map. 10814 10815Expression* 10816Map_index_expression::get_value_pointer(bool insert) 10817{ 10818 if (this->value_pointer_ == NULL) 10819 { 10820 Map_type* type = this->get_map_type(); 10821 if (type == NULL) 10822 { 10823 go_assert(saw_errors()); 10824 return Expression::make_error(this->location()); 10825 } 10826 10827 Location loc = this->location(); 10828 Expression* map_ref = this->map_; 10829 if (this->map_->type()->points_to() != NULL) 10830 map_ref = Expression::make_unary(OPERATOR_MULT, map_ref, loc); 10831 10832 Expression* index_ptr = Expression::make_unary(OPERATOR_AND, this->index_, 10833 loc); 10834 Expression* map_index = 10835 Runtime::make_call(Runtime::MAP_INDEX, loc, 3, 10836 map_ref, index_ptr, 10837 Expression::make_boolean(insert, loc)); 10838 10839 Type* val_type = type->val_type(); 10840 this->value_pointer_ = 10841 Expression::make_unsafe_cast(Type::make_pointer_type(val_type), 10842 map_index, this->location()); 10843 } 10844 return this->value_pointer_; 10845} 10846 10847// Dump ast representation for a map index expression 10848 10849void 10850Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 10851 const 10852{ 10853 Index_expression::dump_index_expression(ast_dump_context, this->map_, 10854 this->index_, NULL, NULL); 10855} 10856 10857// Make a map index expression. 10858 10859Map_index_expression* 10860Expression::make_map_index(Expression* map, Expression* index, 10861 Location location) 10862{ 10863 return new Map_index_expression(map, index, location); 10864} 10865 10866// Class Field_reference_expression. 10867 10868// Lower a field reference expression. There is nothing to lower, but 10869// this is where we generate the tracking information for fields with 10870// the magic go:"track" tag. 10871 10872Expression* 10873Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, 10874 Statement_inserter* inserter, int) 10875{ 10876 Struct_type* struct_type = this->expr_->type()->struct_type(); 10877 if (struct_type == NULL) 10878 { 10879 // Error will be reported elsewhere. 10880 return this; 10881 } 10882 const Struct_field* field = struct_type->field(this->field_index_); 10883 if (field == NULL) 10884 return this; 10885 if (!field->has_tag()) 10886 return this; 10887 if (field->tag().find("go:\"track\"") == std::string::npos) 10888 return this; 10889 10890 // References from functions generated by the compiler don't count. 10891 if (function != NULL && function->func_value()->is_type_specific_function()) 10892 return this; 10893 10894 // We have found a reference to a tracked field. Build a call to 10895 // the runtime function __go_fieldtrack with a string that describes 10896 // the field. FIXME: We should only call this once per referenced 10897 // field per function, not once for each reference to the field. 10898 10899 if (this->called_fieldtrack_) 10900 return this; 10901 this->called_fieldtrack_ = true; 10902 10903 Location loc = this->location(); 10904 10905 std::string s = "fieldtrack \""; 10906 Named_type* nt = this->expr_->type()->named_type(); 10907 if (nt == NULL || nt->named_object()->package() == NULL) 10908 s.append(gogo->pkgpath()); 10909 else 10910 s.append(nt->named_object()->package()->pkgpath()); 10911 s.push_back('.'); 10912 if (nt != NULL) 10913 s.append(Gogo::unpack_hidden_name(nt->name())); 10914 s.push_back('.'); 10915 s.append(field->field_name()); 10916 s.push_back('"'); 10917 10918 // We can't use a string here, because internally a string holds a 10919 // pointer to the actual bytes; when the linker garbage collects the 10920 // string, it won't garbage collect the bytes. So we use a 10921 // [...]byte. 10922 10923 Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); 10924 10925 Type* byte_type = gogo->lookup_global("byte")->type_value(); 10926 Type* array_type = Type::make_array_type(byte_type, length_expr); 10927 10928 Expression_list* bytes = new Expression_list(); 10929 for (std::string::const_iterator p = s.begin(); p != s.end(); p++) 10930 { 10931 unsigned char c = static_cast<unsigned char>(*p); 10932 bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); 10933 } 10934 10935 Expression* e = Expression::make_composite_literal(array_type, 0, false, 10936 bytes, false, loc); 10937 10938 Variable* var = new Variable(array_type, e, true, false, false, loc); 10939 10940 static int count; 10941 char buf[50]; 10942 snprintf(buf, sizeof buf, "fieldtrack.%d", count); 10943 ++count; 10944 10945 Named_object* no = gogo->add_variable(buf, var); 10946 e = Expression::make_var_reference(no, loc); 10947 e = Expression::make_unary(OPERATOR_AND, e, loc); 10948 10949 Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); 10950 gogo->lower_expression(function, inserter, &call); 10951 inserter->insert(Statement::make_statement(call, false)); 10952 10953 // Put this function, and the global variable we just created, into 10954 // unique sections. This will permit the linker to garbage collect 10955 // them if they are not referenced. The effect is that the only 10956 // strings, indicating field references, that will wind up in the 10957 // executable will be those for functions that are actually needed. 10958 if (function != NULL) 10959 function->func_value()->set_in_unique_section(); 10960 var->set_in_unique_section(); 10961 10962 return this; 10963} 10964 10965// Return the type of a field reference. 10966 10967Type* 10968Field_reference_expression::do_type() 10969{ 10970 Type* type = this->expr_->type(); 10971 if (type->is_error()) 10972 return type; 10973 Struct_type* struct_type = type->struct_type(); 10974 go_assert(struct_type != NULL); 10975 return struct_type->field(this->field_index_)->type(); 10976} 10977 10978// Check the types for a field reference. 10979 10980void 10981Field_reference_expression::do_check_types(Gogo*) 10982{ 10983 Type* type = this->expr_->type(); 10984 if (type->is_error()) 10985 return; 10986 Struct_type* struct_type = type->struct_type(); 10987 go_assert(struct_type != NULL); 10988 go_assert(struct_type->field(this->field_index_) != NULL); 10989} 10990 10991// Get the backend representation for a field reference. 10992 10993Bexpression* 10994Field_reference_expression::do_get_backend(Translate_context* context) 10995{ 10996 Bexpression* bstruct = this->expr_->get_backend(context); 10997 return context->gogo()->backend()->struct_field_expression(bstruct, 10998 this->field_index_, 10999 this->location()); 11000} 11001 11002// Dump ast representation for a field reference expression. 11003 11004void 11005Field_reference_expression::do_dump_expression( 11006 Ast_dump_context* ast_dump_context) const 11007{ 11008 this->expr_->dump_expression(ast_dump_context); 11009 ast_dump_context->ostream() << "." << this->field_index_; 11010} 11011 11012// Make a reference to a qualified identifier in an expression. 11013 11014Field_reference_expression* 11015Expression::make_field_reference(Expression* expr, unsigned int field_index, 11016 Location location) 11017{ 11018 return new Field_reference_expression(expr, field_index, location); 11019} 11020 11021// Class Interface_field_reference_expression. 11022 11023// Return an expression for the pointer to the function to call. 11024 11025Expression* 11026Interface_field_reference_expression::get_function() 11027{ 11028 Expression* ref = this->expr_; 11029 Location loc = this->location(); 11030 if (ref->type()->points_to() != NULL) 11031 ref = Expression::make_unary(OPERATOR_MULT, ref, loc); 11032 11033 Expression* mtable = 11034 Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); 11035 Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); 11036 11037 std::string name = Gogo::unpack_hidden_name(this->name_); 11038 unsigned int index; 11039 const Struct_field* field = mtable_type->find_local_field(name, &index); 11040 go_assert(field != NULL); 11041 mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc); 11042 return Expression::make_field_reference(mtable, index, loc); 11043} 11044 11045// Return an expression for the first argument to pass to the interface 11046// function. 11047 11048Expression* 11049Interface_field_reference_expression::get_underlying_object() 11050{ 11051 Expression* expr = this->expr_; 11052 if (expr->type()->points_to() != NULL) 11053 expr = Expression::make_unary(OPERATOR_MULT, expr, this->location()); 11054 return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, 11055 this->location()); 11056} 11057 11058// Traversal. 11059 11060int 11061Interface_field_reference_expression::do_traverse(Traverse* traverse) 11062{ 11063 return Expression::traverse(&this->expr_, traverse); 11064} 11065 11066// Lower the expression. If this expression is not called, we need to 11067// evaluate the expression twice when converting to the backend 11068// interface. So introduce a temporary variable if necessary. 11069 11070Expression* 11071Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, 11072 Statement_inserter* inserter) 11073{ 11074 if (!this->expr_->is_variable()) 11075 { 11076 Temporary_statement* temp = 11077 Statement::make_temporary(this->expr_->type(), NULL, this->location()); 11078 inserter->insert(temp); 11079 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, 11080 this->location()); 11081 } 11082 return this; 11083} 11084 11085// Return the type of an interface field reference. 11086 11087Type* 11088Interface_field_reference_expression::do_type() 11089{ 11090 Type* expr_type = this->expr_->type(); 11091 11092 Type* points_to = expr_type->points_to(); 11093 if (points_to != NULL) 11094 expr_type = points_to; 11095 11096 Interface_type* interface_type = expr_type->interface_type(); 11097 if (interface_type == NULL) 11098 return Type::make_error_type(); 11099 11100 const Typed_identifier* method = interface_type->find_method(this->name_); 11101 if (method == NULL) 11102 return Type::make_error_type(); 11103 11104 return method->type(); 11105} 11106 11107// Determine types. 11108 11109void 11110Interface_field_reference_expression::do_determine_type(const Type_context*) 11111{ 11112 this->expr_->determine_type_no_context(); 11113} 11114 11115// Check the types for an interface field reference. 11116 11117void 11118Interface_field_reference_expression::do_check_types(Gogo*) 11119{ 11120 Type* type = this->expr_->type(); 11121 11122 Type* points_to = type->points_to(); 11123 if (points_to != NULL) 11124 type = points_to; 11125 11126 Interface_type* interface_type = type->interface_type(); 11127 if (interface_type == NULL) 11128 { 11129 if (!type->is_error_type()) 11130 this->report_error(_("expected interface or pointer to interface")); 11131 } 11132 else 11133 { 11134 const Typed_identifier* method = 11135 interface_type->find_method(this->name_); 11136 if (method == NULL) 11137 { 11138 error_at(this->location(), "method %qs not in interface", 11139 Gogo::message_name(this->name_).c_str()); 11140 this->set_is_error(); 11141 } 11142 } 11143} 11144 11145// If an interface field reference is not simply called, then it is 11146// represented as a closure. The closure will hold a single variable, 11147// the value of the interface on which the method should be called. 11148// The function will be a simple thunk that pulls the value from the 11149// closure and calls the method with the remaining arguments. 11150 11151// Because method values are not common, we don't build all thunks for 11152// all possible interface methods, but instead only build them as we 11153// need them. In particular, we even build them on demand for 11154// interface methods defined in other packages. 11155 11156Interface_field_reference_expression::Interface_method_thunks 11157 Interface_field_reference_expression::interface_method_thunks; 11158 11159// Find or create the thunk to call method NAME on TYPE. 11160 11161Named_object* 11162Interface_field_reference_expression::create_thunk(Gogo* gogo, 11163 Interface_type* type, 11164 const std::string& name) 11165{ 11166 std::pair<Interface_type*, Method_thunks*> val(type, NULL); 11167 std::pair<Interface_method_thunks::iterator, bool> ins = 11168 Interface_field_reference_expression::interface_method_thunks.insert(val); 11169 if (ins.second) 11170 { 11171 // This is the first time we have seen this interface. 11172 ins.first->second = new Method_thunks(); 11173 } 11174 11175 for (Method_thunks::const_iterator p = ins.first->second->begin(); 11176 p != ins.first->second->end(); 11177 p++) 11178 if (p->first == name) 11179 return p->second; 11180 11181 Location loc = type->location(); 11182 11183 const Typed_identifier* method_id = type->find_method(name); 11184 if (method_id == NULL) 11185 return Named_object::make_erroneous_name(Gogo::thunk_name()); 11186 11187 Function_type* orig_fntype = method_id->type()->function_type(); 11188 if (orig_fntype == NULL) 11189 return Named_object::make_erroneous_name(Gogo::thunk_name()); 11190 11191 Struct_field_list* sfl = new Struct_field_list(); 11192 // The type here is wrong--it should be the C function type. But it 11193 // doesn't really matter. 11194 Type* vt = Type::make_pointer_type(Type::make_void_type()); 11195 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); 11196 sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); 11197 Type* closure_type = Type::make_struct_type(sfl, loc); 11198 closure_type = Type::make_pointer_type(closure_type); 11199 11200 Function_type* new_fntype = orig_fntype->copy_with_names(); 11201 11202 Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, 11203 false, loc); 11204 11205 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); 11206 cvar->set_is_used(); 11207 cvar->set_is_closure(); 11208 Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); 11209 new_no->func_value()->set_closure_var(cp); 11210 11211 gogo->start_block(loc); 11212 11213 // Field 0 of the closure is the function code pointer, field 1 is 11214 // the value on which to invoke the method. 11215 Expression* arg = Expression::make_var_reference(cp, loc); 11216 arg = Expression::make_unary(OPERATOR_MULT, arg, loc); 11217 arg = Expression::make_field_reference(arg, 1, loc); 11218 11219 Expression *ifre = Expression::make_interface_field_reference(arg, name, 11220 loc); 11221 11222 const Typed_identifier_list* orig_params = orig_fntype->parameters(); 11223 Expression_list* args; 11224 if (orig_params == NULL || orig_params->empty()) 11225 args = NULL; 11226 else 11227 { 11228 const Typed_identifier_list* new_params = new_fntype->parameters(); 11229 args = new Expression_list(); 11230 for (Typed_identifier_list::const_iterator p = new_params->begin(); 11231 p != new_params->end(); 11232 ++p) 11233 { 11234 Named_object* p_no = gogo->lookup(p->name(), NULL); 11235 go_assert(p_no != NULL 11236 && p_no->is_variable() 11237 && p_no->var_value()->is_parameter()); 11238 args->push_back(Expression::make_var_reference(p_no, loc)); 11239 } 11240 } 11241 11242 Call_expression* call = Expression::make_call(ifre, args, 11243 orig_fntype->is_varargs(), 11244 loc); 11245 call->set_varargs_are_lowered(); 11246 11247 Statement* s = Statement::make_return_from_call(call, loc); 11248 gogo->add_statement(s); 11249 Block* b = gogo->finish_block(loc); 11250 gogo->add_block(b, loc); 11251 gogo->lower_block(new_no, b); 11252 gogo->flatten_block(new_no, b); 11253 gogo->finish_function(loc); 11254 11255 ins.first->second->push_back(std::make_pair(name, new_no)); 11256 return new_no; 11257} 11258 11259// Get the backend representation for a method value. 11260 11261Bexpression* 11262Interface_field_reference_expression::do_get_backend(Translate_context* context) 11263{ 11264 Interface_type* type = this->expr_->type()->interface_type(); 11265 if (type == NULL) 11266 { 11267 go_assert(saw_errors()); 11268 return context->backend()->error_expression(); 11269 } 11270 11271 Named_object* thunk = 11272 Interface_field_reference_expression::create_thunk(context->gogo(), 11273 type, this->name_); 11274 if (thunk->is_erroneous()) 11275 { 11276 go_assert(saw_errors()); 11277 return context->backend()->error_expression(); 11278 } 11279 11280 // FIXME: We should lower this earlier, but we can't it lower it in 11281 // the lowering pass because at that point we don't know whether we 11282 // need to create the thunk or not. If the expression is called, we 11283 // don't need the thunk. 11284 11285 Location loc = this->location(); 11286 11287 Struct_field_list* fields = new Struct_field_list(); 11288 fields->push_back(Struct_field(Typed_identifier("fn.0", 11289 thunk->func_value()->type(), 11290 loc))); 11291 fields->push_back(Struct_field(Typed_identifier("val.1", 11292 this->expr_->type(), 11293 loc))); 11294 Struct_type* st = Type::make_struct_type(fields, loc); 11295 11296 Expression_list* vals = new Expression_list(); 11297 vals->push_back(Expression::make_func_code_reference(thunk, loc)); 11298 vals->push_back(this->expr_); 11299 11300 Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); 11301 Bexpression* bclosure = 11302 Expression::make_heap_expression(expr, loc)->get_backend(context); 11303 11304 Expression* nil_check = 11305 Expression::make_binary(OPERATOR_EQEQ, this->expr_, 11306 Expression::make_nil(loc), loc); 11307 Bexpression* bnil_check = nil_check->get_backend(context); 11308 11309 Gogo* gogo = context->gogo(); 11310 Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, 11311 loc)->get_backend(context); 11312 11313 Bexpression* bcond = 11314 gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc); 11315 Bstatement* cond_statement = gogo->backend()->expression_statement(bcond); 11316 return gogo->backend()->compound_expression(cond_statement, bclosure, loc); 11317} 11318 11319// Dump ast representation for an interface field reference. 11320 11321void 11322Interface_field_reference_expression::do_dump_expression( 11323 Ast_dump_context* ast_dump_context) const 11324{ 11325 this->expr_->dump_expression(ast_dump_context); 11326 ast_dump_context->ostream() << "." << this->name_; 11327} 11328 11329// Make a reference to a field in an interface. 11330 11331Expression* 11332Expression::make_interface_field_reference(Expression* expr, 11333 const std::string& field, 11334 Location location) 11335{ 11336 return new Interface_field_reference_expression(expr, field, location); 11337} 11338 11339// A general selector. This is a Parser_expression for LEFT.NAME. It 11340// is lowered after we know the type of the left hand side. 11341 11342class Selector_expression : public Parser_expression 11343{ 11344 public: 11345 Selector_expression(Expression* left, const std::string& name, 11346 Location location) 11347 : Parser_expression(EXPRESSION_SELECTOR, location), 11348 left_(left), name_(name) 11349 { } 11350 11351 protected: 11352 int 11353 do_traverse(Traverse* traverse) 11354 { return Expression::traverse(&this->left_, traverse); } 11355 11356 Expression* 11357 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 11358 11359 Expression* 11360 do_copy() 11361 { 11362 return new Selector_expression(this->left_->copy(), this->name_, 11363 this->location()); 11364 } 11365 11366 void 11367 do_dump_expression(Ast_dump_context* ast_dump_context) const; 11368 11369 private: 11370 Expression* 11371 lower_method_expression(Gogo*); 11372 11373 // The expression on the left hand side. 11374 Expression* left_; 11375 // The name on the right hand side. 11376 std::string name_; 11377}; 11378 11379// Lower a selector expression once we know the real type of the left 11380// hand side. 11381 11382Expression* 11383Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, 11384 int) 11385{ 11386 Expression* left = this->left_; 11387 if (left->is_type_expression()) 11388 return this->lower_method_expression(gogo); 11389 return Type::bind_field_or_method(gogo, left->type(), left, this->name_, 11390 this->location()); 11391} 11392 11393// Lower a method expression T.M or (*T).M. We turn this into a 11394// function literal. 11395 11396Expression* 11397Selector_expression::lower_method_expression(Gogo* gogo) 11398{ 11399 Location location = this->location(); 11400 Type* type = this->left_->type(); 11401 const std::string& name(this->name_); 11402 11403 bool is_pointer; 11404 if (type->points_to() == NULL) 11405 is_pointer = false; 11406 else 11407 { 11408 is_pointer = true; 11409 type = type->points_to(); 11410 } 11411 Named_type* nt = type->named_type(); 11412 if (nt == NULL) 11413 { 11414 error_at(location, 11415 ("method expression requires named type or " 11416 "pointer to named type")); 11417 return Expression::make_error(location); 11418 } 11419 11420 bool is_ambiguous; 11421 Method* method = nt->method_function(name, &is_ambiguous); 11422 const Typed_identifier* imethod = NULL; 11423 if (method == NULL && !is_pointer) 11424 { 11425 Interface_type* it = nt->interface_type(); 11426 if (it != NULL) 11427 imethod = it->find_method(name); 11428 } 11429 11430 if (method == NULL && imethod == NULL) 11431 { 11432 if (!is_ambiguous) 11433 error_at(location, "type %<%s%s%> has no method %<%s%>", 11434 is_pointer ? "*" : "", 11435 nt->message_name().c_str(), 11436 Gogo::message_name(name).c_str()); 11437 else 11438 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", 11439 Gogo::message_name(name).c_str(), 11440 is_pointer ? "*" : "", 11441 nt->message_name().c_str()); 11442 return Expression::make_error(location); 11443 } 11444 11445 if (method != NULL && !is_pointer && !method->is_value_method()) 11446 { 11447 error_at(location, "method requires pointer (use %<(*%s).%s)%>", 11448 nt->message_name().c_str(), 11449 Gogo::message_name(name).c_str()); 11450 return Expression::make_error(location); 11451 } 11452 11453 // Build a new function type in which the receiver becomes the first 11454 // argument. 11455 Function_type* method_type; 11456 if (method != NULL) 11457 { 11458 method_type = method->type(); 11459 go_assert(method_type->is_method()); 11460 } 11461 else 11462 { 11463 method_type = imethod->type()->function_type(); 11464 go_assert(method_type != NULL && !method_type->is_method()); 11465 } 11466 11467 const char* const receiver_name = "$this"; 11468 Typed_identifier_list* parameters = new Typed_identifier_list(); 11469 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), 11470 location)); 11471 11472 const Typed_identifier_list* method_parameters = method_type->parameters(); 11473 if (method_parameters != NULL) 11474 { 11475 int i = 0; 11476 for (Typed_identifier_list::const_iterator p = method_parameters->begin(); 11477 p != method_parameters->end(); 11478 ++p, ++i) 11479 { 11480 if (!p->name().empty()) 11481 parameters->push_back(*p); 11482 else 11483 { 11484 char buf[20]; 11485 snprintf(buf, sizeof buf, "$param%d", i); 11486 parameters->push_back(Typed_identifier(buf, p->type(), 11487 p->location())); 11488 } 11489 } 11490 } 11491 11492 const Typed_identifier_list* method_results = method_type->results(); 11493 Typed_identifier_list* results; 11494 if (method_results == NULL) 11495 results = NULL; 11496 else 11497 { 11498 results = new Typed_identifier_list(); 11499 for (Typed_identifier_list::const_iterator p = method_results->begin(); 11500 p != method_results->end(); 11501 ++p) 11502 results->push_back(*p); 11503 } 11504 11505 Function_type* fntype = Type::make_function_type(NULL, parameters, results, 11506 location); 11507 if (method_type->is_varargs()) 11508 fntype->set_is_varargs(); 11509 11510 // We generate methods which always takes a pointer to the receiver 11511 // as their first argument. If this is for a pointer type, we can 11512 // simply reuse the existing function. We use an internal hack to 11513 // get the right type. 11514 // FIXME: This optimization is disabled because it doesn't yet work 11515 // with function descriptors when the method expression is not 11516 // directly called. 11517 if (method != NULL && is_pointer && false) 11518 { 11519 Named_object* mno = (method->needs_stub_method() 11520 ? method->stub_object() 11521 : method->named_object()); 11522 Expression* f = Expression::make_func_reference(mno, NULL, location); 11523 f = Expression::make_cast(fntype, f, location); 11524 Type_conversion_expression* tce = 11525 static_cast<Type_conversion_expression*>(f); 11526 tce->set_may_convert_function_types(); 11527 return f; 11528 } 11529 11530 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, 11531 location); 11532 11533 Named_object* vno = gogo->lookup(receiver_name, NULL); 11534 go_assert(vno != NULL); 11535 Expression* ve = Expression::make_var_reference(vno, location); 11536 Expression* bm; 11537 if (method != NULL) 11538 bm = Type::bind_field_or_method(gogo, nt, ve, name, location); 11539 else 11540 bm = Expression::make_interface_field_reference(ve, name, location); 11541 11542 // Even though we found the method above, if it has an error type we 11543 // may see an error here. 11544 if (bm->is_error_expression()) 11545 { 11546 gogo->finish_function(location); 11547 return bm; 11548 } 11549 11550 Expression_list* args; 11551 if (parameters->size() <= 1) 11552 args = NULL; 11553 else 11554 { 11555 args = new Expression_list(); 11556 Typed_identifier_list::const_iterator p = parameters->begin(); 11557 ++p; 11558 for (; p != parameters->end(); ++p) 11559 { 11560 vno = gogo->lookup(p->name(), NULL); 11561 go_assert(vno != NULL); 11562 args->push_back(Expression::make_var_reference(vno, location)); 11563 } 11564 } 11565 11566 gogo->start_block(location); 11567 11568 Call_expression* call = Expression::make_call(bm, args, 11569 method_type->is_varargs(), 11570 location); 11571 11572 Statement* s = Statement::make_return_from_call(call, location); 11573 gogo->add_statement(s); 11574 11575 Block* b = gogo->finish_block(location); 11576 11577 gogo->add_block(b, location); 11578 11579 // Lower the call in case there are multiple results. 11580 gogo->lower_block(no, b); 11581 gogo->flatten_block(no, b); 11582 11583 gogo->finish_function(location); 11584 11585 return Expression::make_func_reference(no, NULL, location); 11586} 11587 11588// Dump the ast for a selector expression. 11589 11590void 11591Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 11592 const 11593{ 11594 ast_dump_context->dump_expression(this->left_); 11595 ast_dump_context->ostream() << "."; 11596 ast_dump_context->ostream() << this->name_; 11597} 11598 11599// Make a selector expression. 11600 11601Expression* 11602Expression::make_selector(Expression* left, const std::string& name, 11603 Location location) 11604{ 11605 return new Selector_expression(left, name, location); 11606} 11607 11608// Implement the builtin function new. 11609 11610class Allocation_expression : public Expression 11611{ 11612 public: 11613 Allocation_expression(Type* type, Location location) 11614 : Expression(EXPRESSION_ALLOCATION, location), 11615 type_(type) 11616 { } 11617 11618 protected: 11619 int 11620 do_traverse(Traverse* traverse) 11621 { return Type::traverse(this->type_, traverse); } 11622 11623 Type* 11624 do_type() 11625 { return Type::make_pointer_type(this->type_); } 11626 11627 void 11628 do_determine_type(const Type_context*) 11629 { } 11630 11631 Expression* 11632 do_copy() 11633 { return new Allocation_expression(this->type_, this->location()); } 11634 11635 Bexpression* 11636 do_get_backend(Translate_context*); 11637 11638 void 11639 do_dump_expression(Ast_dump_context*) const; 11640 11641 private: 11642 // The type we are allocating. 11643 Type* type_; 11644}; 11645 11646// Return the backend representation for an allocation expression. 11647 11648Bexpression* 11649Allocation_expression::do_get_backend(Translate_context* context) 11650{ 11651 Gogo* gogo = context->gogo(); 11652 Location loc = this->location(); 11653 Bexpression* space = 11654 gogo->allocate_memory(this->type_, loc)->get_backend(context); 11655 Btype* pbtype = gogo->backend()->pointer_type(this->type_->get_backend(gogo)); 11656 return gogo->backend()->convert_expression(pbtype, space, loc); 11657} 11658 11659// Dump ast representation for an allocation expression. 11660 11661void 11662Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 11663 const 11664{ 11665 ast_dump_context->ostream() << "new("; 11666 ast_dump_context->dump_type(this->type_); 11667 ast_dump_context->ostream() << ")"; 11668} 11669 11670// Make an allocation expression. 11671 11672Expression* 11673Expression::make_allocation(Type* type, Location location) 11674{ 11675 return new Allocation_expression(type, location); 11676} 11677 11678// Construct a struct. 11679 11680class Struct_construction_expression : public Expression 11681{ 11682 public: 11683 Struct_construction_expression(Type* type, Expression_list* vals, 11684 Location location) 11685 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), 11686 type_(type), vals_(vals), traverse_order_(NULL) 11687 { } 11688 11689 // Set the traversal order, used to ensure that we implement the 11690 // order of evaluation rules. Takes ownership of the argument. 11691 void 11692 set_traverse_order(std::vector<int>* traverse_order) 11693 { this->traverse_order_ = traverse_order; } 11694 11695 // Return whether this is a constant initializer. 11696 bool 11697 is_constant_struct() const; 11698 11699 protected: 11700 int 11701 do_traverse(Traverse* traverse); 11702 11703 bool 11704 do_is_immutable() const; 11705 11706 Type* 11707 do_type() 11708 { return this->type_; } 11709 11710 void 11711 do_determine_type(const Type_context*); 11712 11713 void 11714 do_check_types(Gogo*); 11715 11716 Expression* 11717 do_copy() 11718 { 11719 Struct_construction_expression* ret = 11720 new Struct_construction_expression(this->type_, 11721 (this->vals_ == NULL 11722 ? NULL 11723 : this->vals_->copy()), 11724 this->location()); 11725 if (this->traverse_order_ != NULL) 11726 ret->set_traverse_order(this->traverse_order_); 11727 return ret; 11728 } 11729 11730 Expression* 11731 do_flatten(Gogo*, Named_object*, Statement_inserter*); 11732 11733 Bexpression* 11734 do_get_backend(Translate_context*); 11735 11736 void 11737 do_export(Export*) const; 11738 11739 void 11740 do_dump_expression(Ast_dump_context*) const; 11741 11742 private: 11743 // The type of the struct to construct. 11744 Type* type_; 11745 // The list of values, in order of the fields in the struct. A NULL 11746 // entry means that the field should be zero-initialized. 11747 Expression_list* vals_; 11748 // If not NULL, the order in which to traverse vals_. This is used 11749 // so that we implement the order of evaluation rules correctly. 11750 std::vector<int>* traverse_order_; 11751}; 11752 11753// Traversal. 11754 11755int 11756Struct_construction_expression::do_traverse(Traverse* traverse) 11757{ 11758 if (this->vals_ != NULL) 11759 { 11760 if (this->traverse_order_ == NULL) 11761 { 11762 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) 11763 return TRAVERSE_EXIT; 11764 } 11765 else 11766 { 11767 for (std::vector<int>::const_iterator p = 11768 this->traverse_order_->begin(); 11769 p != this->traverse_order_->end(); 11770 ++p) 11771 { 11772 if (Expression::traverse(&this->vals_->at(*p), traverse) 11773 == TRAVERSE_EXIT) 11774 return TRAVERSE_EXIT; 11775 } 11776 } 11777 } 11778 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 11779 return TRAVERSE_EXIT; 11780 return TRAVERSE_CONTINUE; 11781} 11782 11783// Return whether this is a constant initializer. 11784 11785bool 11786Struct_construction_expression::is_constant_struct() const 11787{ 11788 if (this->vals_ == NULL) 11789 return true; 11790 for (Expression_list::const_iterator pv = this->vals_->begin(); 11791 pv != this->vals_->end(); 11792 ++pv) 11793 { 11794 if (*pv != NULL 11795 && !(*pv)->is_constant() 11796 && (!(*pv)->is_composite_literal() 11797 || (*pv)->is_nonconstant_composite_literal())) 11798 return false; 11799 } 11800 11801 const Struct_field_list* fields = this->type_->struct_type()->fields(); 11802 for (Struct_field_list::const_iterator pf = fields->begin(); 11803 pf != fields->end(); 11804 ++pf) 11805 { 11806 // There are no constant constructors for interfaces. 11807 if (pf->type()->interface_type() != NULL) 11808 return false; 11809 } 11810 11811 return true; 11812} 11813 11814// Return whether this struct is immutable. 11815 11816bool 11817Struct_construction_expression::do_is_immutable() const 11818{ 11819 if (this->vals_ == NULL) 11820 return true; 11821 for (Expression_list::const_iterator pv = this->vals_->begin(); 11822 pv != this->vals_->end(); 11823 ++pv) 11824 { 11825 if (*pv != NULL && !(*pv)->is_immutable()) 11826 return false; 11827 } 11828 return true; 11829} 11830 11831// Final type determination. 11832 11833void 11834Struct_construction_expression::do_determine_type(const Type_context*) 11835{ 11836 if (this->vals_ == NULL) 11837 return; 11838 const Struct_field_list* fields = this->type_->struct_type()->fields(); 11839 Expression_list::const_iterator pv = this->vals_->begin(); 11840 for (Struct_field_list::const_iterator pf = fields->begin(); 11841 pf != fields->end(); 11842 ++pf, ++pv) 11843 { 11844 if (pv == this->vals_->end()) 11845 return; 11846 if (*pv != NULL) 11847 { 11848 Type_context subcontext(pf->type(), false); 11849 (*pv)->determine_type(&subcontext); 11850 } 11851 } 11852 // Extra values are an error we will report elsewhere; we still want 11853 // to determine the type to avoid knockon errors. 11854 for (; pv != this->vals_->end(); ++pv) 11855 (*pv)->determine_type_no_context(); 11856} 11857 11858// Check types. 11859 11860void 11861Struct_construction_expression::do_check_types(Gogo*) 11862{ 11863 if (this->vals_ == NULL) 11864 return; 11865 11866 Struct_type* st = this->type_->struct_type(); 11867 if (this->vals_->size() > st->field_count()) 11868 { 11869 this->report_error(_("too many expressions for struct")); 11870 return; 11871 } 11872 11873 const Struct_field_list* fields = st->fields(); 11874 Expression_list::const_iterator pv = this->vals_->begin(); 11875 int i = 0; 11876 for (Struct_field_list::const_iterator pf = fields->begin(); 11877 pf != fields->end(); 11878 ++pf, ++pv, ++i) 11879 { 11880 if (pv == this->vals_->end()) 11881 { 11882 this->report_error(_("too few expressions for struct")); 11883 break; 11884 } 11885 11886 if (*pv == NULL) 11887 continue; 11888 11889 std::string reason; 11890 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) 11891 { 11892 if (reason.empty()) 11893 error_at((*pv)->location(), 11894 "incompatible type for field %d in struct construction", 11895 i + 1); 11896 else 11897 error_at((*pv)->location(), 11898 ("incompatible type for field %d in " 11899 "struct construction (%s)"), 11900 i + 1, reason.c_str()); 11901 this->set_is_error(); 11902 } 11903 } 11904 go_assert(pv == this->vals_->end()); 11905} 11906 11907// Flatten a struct construction expression. Store the values into 11908// temporaries in case they need interface conversion. 11909 11910Expression* 11911Struct_construction_expression::do_flatten(Gogo*, Named_object*, 11912 Statement_inserter* inserter) 11913{ 11914 if (this->vals_ == NULL) 11915 return this; 11916 11917 // If this is a constant struct, we don't need temporaries. 11918 if (this->is_constant_struct()) 11919 return this; 11920 11921 Location loc = this->location(); 11922 for (Expression_list::iterator pv = this->vals_->begin(); 11923 pv != this->vals_->end(); 11924 ++pv) 11925 { 11926 if (*pv != NULL) 11927 { 11928 if (!(*pv)->is_variable()) 11929 { 11930 Temporary_statement* temp = 11931 Statement::make_temporary(NULL, *pv, loc); 11932 inserter->insert(temp); 11933 *pv = Expression::make_temporary_reference(temp, loc); 11934 } 11935 } 11936 } 11937 return this; 11938} 11939 11940// Return the backend representation for constructing a struct. 11941 11942Bexpression* 11943Struct_construction_expression::do_get_backend(Translate_context* context) 11944{ 11945 Gogo* gogo = context->gogo(); 11946 11947 Btype* btype = this->type_->get_backend(gogo); 11948 if (this->vals_ == NULL) 11949 return gogo->backend()->zero_expression(btype); 11950 11951 const Struct_field_list* fields = this->type_->struct_type()->fields(); 11952 Expression_list::const_iterator pv = this->vals_->begin(); 11953 std::vector<Bexpression*> init; 11954 for (Struct_field_list::const_iterator pf = fields->begin(); 11955 pf != fields->end(); 11956 ++pf) 11957 { 11958 Btype* fbtype = pf->type()->get_backend(gogo); 11959 if (pv == this->vals_->end()) 11960 init.push_back(gogo->backend()->zero_expression(fbtype)); 11961 else if (*pv == NULL) 11962 { 11963 init.push_back(gogo->backend()->zero_expression(fbtype)); 11964 ++pv; 11965 } 11966 else 11967 { 11968 Expression* val = 11969 Expression::convert_for_assignment(gogo, pf->type(), 11970 *pv, this->location()); 11971 init.push_back(val->get_backend(context)); 11972 ++pv; 11973 } 11974 } 11975 return gogo->backend()->constructor_expression(btype, init, this->location()); 11976} 11977 11978// Export a struct construction. 11979 11980void 11981Struct_construction_expression::do_export(Export* exp) const 11982{ 11983 exp->write_c_string("convert("); 11984 exp->write_type(this->type_); 11985 for (Expression_list::const_iterator pv = this->vals_->begin(); 11986 pv != this->vals_->end(); 11987 ++pv) 11988 { 11989 exp->write_c_string(", "); 11990 if (*pv != NULL) 11991 (*pv)->export_expression(exp); 11992 } 11993 exp->write_c_string(")"); 11994} 11995 11996// Dump ast representation of a struct construction expression. 11997 11998void 11999Struct_construction_expression::do_dump_expression( 12000 Ast_dump_context* ast_dump_context) const 12001{ 12002 ast_dump_context->dump_type(this->type_); 12003 ast_dump_context->ostream() << "{"; 12004 ast_dump_context->dump_expression_list(this->vals_); 12005 ast_dump_context->ostream() << "}"; 12006} 12007 12008// Make a struct composite literal. This used by the thunk code. 12009 12010Expression* 12011Expression::make_struct_composite_literal(Type* type, Expression_list* vals, 12012 Location location) 12013{ 12014 go_assert(type->struct_type() != NULL); 12015 return new Struct_construction_expression(type, vals, location); 12016} 12017 12018// Construct an array. This class is not used directly; instead we 12019// use the child classes, Fixed_array_construction_expression and 12020// Slice_construction_expression. 12021 12022class Array_construction_expression : public Expression 12023{ 12024 protected: 12025 Array_construction_expression(Expression_classification classification, 12026 Type* type, 12027 const std::vector<unsigned long>* indexes, 12028 Expression_list* vals, Location location) 12029 : Expression(classification, location), 12030 type_(type), indexes_(indexes), vals_(vals) 12031 { go_assert(indexes == NULL || indexes->size() == vals->size()); } 12032 12033 public: 12034 // Return whether this is a constant initializer. 12035 bool 12036 is_constant_array() const; 12037 12038 // Return the number of elements. 12039 size_t 12040 element_count() const 12041 { return this->vals_ == NULL ? 0 : this->vals_->size(); } 12042 12043protected: 12044 virtual int 12045 do_traverse(Traverse* traverse); 12046 12047 bool 12048 do_is_immutable() const; 12049 12050 Type* 12051 do_type() 12052 { return this->type_; } 12053 12054 void 12055 do_determine_type(const Type_context*); 12056 12057 void 12058 do_check_types(Gogo*); 12059 12060 void 12061 do_export(Export*) const; 12062 12063 // The indexes. 12064 const std::vector<unsigned long>* 12065 indexes() 12066 { return this->indexes_; } 12067 12068 // The list of values. 12069 Expression_list* 12070 vals() 12071 { return this->vals_; } 12072 12073 Expression* 12074 do_flatten(Gogo*, Named_object*, Statement_inserter*); 12075 12076 // Get the backend constructor for the array values. 12077 Bexpression* 12078 get_constructor(Translate_context* context, Btype* btype); 12079 12080 void 12081 do_dump_expression(Ast_dump_context*) const; 12082 12083 private: 12084 // The type of the array to construct. 12085 Type* type_; 12086 // The list of indexes into the array, one for each value. This may 12087 // be NULL, in which case the indexes start at zero and increment. 12088 const std::vector<unsigned long>* indexes_; 12089 // The list of values. This may be NULL if there are no values. 12090 Expression_list* vals_; 12091}; 12092 12093// Traversal. 12094 12095int 12096Array_construction_expression::do_traverse(Traverse* traverse) 12097{ 12098 if (this->vals_ != NULL 12099 && this->vals_->traverse(traverse) == TRAVERSE_EXIT) 12100 return TRAVERSE_EXIT; 12101 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 12102 return TRAVERSE_EXIT; 12103 return TRAVERSE_CONTINUE; 12104} 12105 12106// Return whether this is a constant initializer. 12107 12108bool 12109Array_construction_expression::is_constant_array() const 12110{ 12111 if (this->vals_ == NULL) 12112 return true; 12113 12114 // There are no constant constructors for interfaces. 12115 if (this->type_->array_type()->element_type()->interface_type() != NULL) 12116 return false; 12117 12118 for (Expression_list::const_iterator pv = this->vals_->begin(); 12119 pv != this->vals_->end(); 12120 ++pv) 12121 { 12122 if (*pv != NULL 12123 && !(*pv)->is_constant() 12124 && (!(*pv)->is_composite_literal() 12125 || (*pv)->is_nonconstant_composite_literal())) 12126 return false; 12127 } 12128 return true; 12129} 12130 12131// Return whether this is an immutable array initializer. 12132 12133bool 12134Array_construction_expression::do_is_immutable() const 12135{ 12136 if (this->vals_ == NULL) 12137 return true; 12138 for (Expression_list::const_iterator pv = this->vals_->begin(); 12139 pv != this->vals_->end(); 12140 ++pv) 12141 { 12142 if (*pv != NULL && !(*pv)->is_immutable()) 12143 return false; 12144 } 12145 return true; 12146} 12147 12148// Final type determination. 12149 12150void 12151Array_construction_expression::do_determine_type(const Type_context*) 12152{ 12153 if (this->vals_ == NULL) 12154 return; 12155 Type_context subcontext(this->type_->array_type()->element_type(), false); 12156 for (Expression_list::const_iterator pv = this->vals_->begin(); 12157 pv != this->vals_->end(); 12158 ++pv) 12159 { 12160 if (*pv != NULL) 12161 (*pv)->determine_type(&subcontext); 12162 } 12163} 12164 12165// Check types. 12166 12167void 12168Array_construction_expression::do_check_types(Gogo*) 12169{ 12170 if (this->vals_ == NULL) 12171 return; 12172 12173 Array_type* at = this->type_->array_type(); 12174 int i = 0; 12175 Type* element_type = at->element_type(); 12176 for (Expression_list::const_iterator pv = this->vals_->begin(); 12177 pv != this->vals_->end(); 12178 ++pv, ++i) 12179 { 12180 if (*pv != NULL 12181 && !Type::are_assignable(element_type, (*pv)->type(), NULL)) 12182 { 12183 error_at((*pv)->location(), 12184 "incompatible type for element %d in composite literal", 12185 i + 1); 12186 this->set_is_error(); 12187 } 12188 } 12189} 12190 12191// Flatten an array construction expression. Store the values into 12192// temporaries in case they need interface conversion. 12193 12194Expression* 12195Array_construction_expression::do_flatten(Gogo*, Named_object*, 12196 Statement_inserter* inserter) 12197{ 12198 if (this->vals_ == NULL) 12199 return this; 12200 12201 // If this is a constant array, we don't need temporaries. 12202 if (this->is_constant_array()) 12203 return this; 12204 12205 Location loc = this->location(); 12206 for (Expression_list::iterator pv = this->vals_->begin(); 12207 pv != this->vals_->end(); 12208 ++pv) 12209 { 12210 if (*pv != NULL) 12211 { 12212 if (!(*pv)->is_variable()) 12213 { 12214 Temporary_statement* temp = 12215 Statement::make_temporary(NULL, *pv, loc); 12216 inserter->insert(temp); 12217 *pv = Expression::make_temporary_reference(temp, loc); 12218 } 12219 } 12220 } 12221 return this; 12222} 12223 12224// Get a constructor expression for the array values. 12225 12226Bexpression* 12227Array_construction_expression::get_constructor(Translate_context* context, 12228 Btype* array_btype) 12229{ 12230 Type* element_type = this->type_->array_type()->element_type(); 12231 12232 std::vector<unsigned long> indexes; 12233 std::vector<Bexpression*> vals; 12234 Gogo* gogo = context->gogo(); 12235 if (this->vals_ != NULL) 12236 { 12237 size_t i = 0; 12238 std::vector<unsigned long>::const_iterator pi; 12239 if (this->indexes_ != NULL) 12240 pi = this->indexes_->begin(); 12241 for (Expression_list::const_iterator pv = this->vals_->begin(); 12242 pv != this->vals_->end(); 12243 ++pv, ++i) 12244 { 12245 if (this->indexes_ != NULL) 12246 go_assert(pi != this->indexes_->end()); 12247 12248 if (this->indexes_ == NULL) 12249 indexes.push_back(i); 12250 else 12251 indexes.push_back(*pi); 12252 if (*pv == NULL) 12253 { 12254 Btype* ebtype = element_type->get_backend(gogo); 12255 Bexpression *zv = gogo->backend()->zero_expression(ebtype); 12256 vals.push_back(zv); 12257 } 12258 else 12259 { 12260 Expression* val_expr = 12261 Expression::convert_for_assignment(gogo, element_type, *pv, 12262 this->location()); 12263 vals.push_back(val_expr->get_backend(context)); 12264 } 12265 if (this->indexes_ != NULL) 12266 ++pi; 12267 } 12268 if (this->indexes_ != NULL) 12269 go_assert(pi == this->indexes_->end()); 12270 } 12271 return gogo->backend()->array_constructor_expression(array_btype, indexes, 12272 vals, this->location()); 12273} 12274 12275// Export an array construction. 12276 12277void 12278Array_construction_expression::do_export(Export* exp) const 12279{ 12280 exp->write_c_string("convert("); 12281 exp->write_type(this->type_); 12282 if (this->vals_ != NULL) 12283 { 12284 std::vector<unsigned long>::const_iterator pi; 12285 if (this->indexes_ != NULL) 12286 pi = this->indexes_->begin(); 12287 for (Expression_list::const_iterator pv = this->vals_->begin(); 12288 pv != this->vals_->end(); 12289 ++pv) 12290 { 12291 exp->write_c_string(", "); 12292 12293 if (this->indexes_ != NULL) 12294 { 12295 char buf[100]; 12296 snprintf(buf, sizeof buf, "%lu", *pi); 12297 exp->write_c_string(buf); 12298 exp->write_c_string(":"); 12299 } 12300 12301 if (*pv != NULL) 12302 (*pv)->export_expression(exp); 12303 12304 if (this->indexes_ != NULL) 12305 ++pi; 12306 } 12307 } 12308 exp->write_c_string(")"); 12309} 12310 12311// Dump ast representation of an array construction expressin. 12312 12313void 12314Array_construction_expression::do_dump_expression( 12315 Ast_dump_context* ast_dump_context) const 12316{ 12317 Expression* length = this->type_->array_type()->length(); 12318 12319 ast_dump_context->ostream() << "[" ; 12320 if (length != NULL) 12321 { 12322 ast_dump_context->dump_expression(length); 12323 } 12324 ast_dump_context->ostream() << "]" ; 12325 ast_dump_context->dump_type(this->type_); 12326 ast_dump_context->ostream() << "{" ; 12327 if (this->indexes_ == NULL) 12328 ast_dump_context->dump_expression_list(this->vals_); 12329 else 12330 { 12331 Expression_list::const_iterator pv = this->vals_->begin(); 12332 for (std::vector<unsigned long>::const_iterator pi = 12333 this->indexes_->begin(); 12334 pi != this->indexes_->end(); 12335 ++pi, ++pv) 12336 { 12337 if (pi != this->indexes_->begin()) 12338 ast_dump_context->ostream() << ", "; 12339 ast_dump_context->ostream() << *pi << ':'; 12340 ast_dump_context->dump_expression(*pv); 12341 } 12342 } 12343 ast_dump_context->ostream() << "}" ; 12344 12345} 12346 12347// Construct a fixed array. 12348 12349class Fixed_array_construction_expression : 12350 public Array_construction_expression 12351{ 12352 public: 12353 Fixed_array_construction_expression(Type* type, 12354 const std::vector<unsigned long>* indexes, 12355 Expression_list* vals, Location location) 12356 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, 12357 type, indexes, vals, location) 12358 { go_assert(type->array_type() != NULL && !type->is_slice_type()); } 12359 12360 protected: 12361 Expression* 12362 do_copy() 12363 { 12364 return new Fixed_array_construction_expression(this->type(), 12365 this->indexes(), 12366 (this->vals() == NULL 12367 ? NULL 12368 : this->vals()->copy()), 12369 this->location()); 12370 } 12371 12372 Bexpression* 12373 do_get_backend(Translate_context*); 12374}; 12375 12376// Return the backend representation for constructing a fixed array. 12377 12378Bexpression* 12379Fixed_array_construction_expression::do_get_backend(Translate_context* context) 12380{ 12381 Type* type = this->type(); 12382 Btype* btype = type->get_backend(context->gogo()); 12383 return this->get_constructor(context, btype); 12384} 12385 12386Expression* 12387Expression::make_array_composite_literal(Type* type, Expression_list* vals, 12388 Location location) 12389{ 12390 go_assert(type->array_type() != NULL && !type->is_slice_type()); 12391 return new Fixed_array_construction_expression(type, NULL, vals, location); 12392} 12393 12394// Construct a slice. 12395 12396class Slice_construction_expression : public Array_construction_expression 12397{ 12398 public: 12399 Slice_construction_expression(Type* type, 12400 const std::vector<unsigned long>* indexes, 12401 Expression_list* vals, Location location) 12402 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, 12403 type, indexes, vals, location), 12404 valtype_(NULL) 12405 { 12406 go_assert(type->is_slice_type()); 12407 12408 unsigned long lenval; 12409 Expression* length; 12410 if (vals == NULL || vals->empty()) 12411 lenval = 0; 12412 else 12413 { 12414 if (this->indexes() == NULL) 12415 lenval = vals->size(); 12416 else 12417 lenval = indexes->back() + 1; 12418 } 12419 Type* int_type = Type::lookup_integer_type("int"); 12420 length = Expression::make_integer_ul(lenval, int_type, location); 12421 Type* element_type = type->array_type()->element_type(); 12422 this->valtype_ = Type::make_array_type(element_type, length); 12423 } 12424 12425 protected: 12426 // Note that taking the address of a slice literal is invalid. 12427 12428 int 12429 do_traverse(Traverse* traverse); 12430 12431 Expression* 12432 do_copy() 12433 { 12434 return new Slice_construction_expression(this->type(), this->indexes(), 12435 (this->vals() == NULL 12436 ? NULL 12437 : this->vals()->copy()), 12438 this->location()); 12439 } 12440 12441 Bexpression* 12442 do_get_backend(Translate_context*); 12443 12444 private: 12445 // The type of the values in this slice. 12446 Type* valtype_; 12447}; 12448 12449// Traversal. 12450 12451int 12452Slice_construction_expression::do_traverse(Traverse* traverse) 12453{ 12454 if (this->Array_construction_expression::do_traverse(traverse) 12455 == TRAVERSE_EXIT) 12456 return TRAVERSE_EXIT; 12457 if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) 12458 return TRAVERSE_EXIT; 12459 return TRAVERSE_CONTINUE; 12460} 12461 12462// Return the backend representation for constructing a slice. 12463 12464Bexpression* 12465Slice_construction_expression::do_get_backend(Translate_context* context) 12466{ 12467 Array_type* array_type = this->type()->array_type(); 12468 if (array_type == NULL) 12469 { 12470 go_assert(this->type()->is_error()); 12471 return context->backend()->error_expression(); 12472 } 12473 12474 Location loc = this->location(); 12475 Type* element_type = array_type->element_type(); 12476 go_assert(this->valtype_ != NULL); 12477 12478 Expression_list* vals = this->vals(); 12479 if (this->vals() == NULL || this->vals()->empty()) 12480 { 12481 // We need to create a unique value for the empty array literal. 12482 vals = new Expression_list; 12483 vals->push_back(NULL); 12484 } 12485 Expression* array_val = 12486 new Fixed_array_construction_expression(this->valtype_, this->indexes(), 12487 vals, loc); 12488 12489 bool is_constant_initializer = array_val->is_immutable(); 12490 12491 // We have to copy the initial values into heap memory if we are in 12492 // a function or if the values are not constants. We also have to 12493 // copy them if they may contain pointers in a non-constant context, 12494 // as otherwise the garbage collector won't see them. 12495 bool copy_to_heap = (context->function() != NULL 12496 || !is_constant_initializer 12497 || (element_type->has_pointer() 12498 && !context->is_const())); 12499 12500 Expression* space; 12501 if (!copy_to_heap) 12502 { 12503 // The initializer will only run once. 12504 space = Expression::make_unary(OPERATOR_AND, array_val, loc); 12505 space->unary_expression()->set_is_slice_init(); 12506 } 12507 else 12508 space = Expression::make_heap_expression(array_val, loc); 12509 12510 // Build a constructor for the slice. 12511 12512 Expression* len = this->valtype_->array_type()->length(); 12513 Expression* slice_val = 12514 Expression::make_slice_value(this->type(), space, len, len, loc); 12515 return slice_val->get_backend(context); 12516} 12517 12518// Make a slice composite literal. This is used by the type 12519// descriptor code. 12520 12521Expression* 12522Expression::make_slice_composite_literal(Type* type, Expression_list* vals, 12523 Location location) 12524{ 12525 go_assert(type->is_slice_type()); 12526 return new Slice_construction_expression(type, NULL, vals, location); 12527} 12528 12529// Construct a map. 12530 12531class Map_construction_expression : public Expression 12532{ 12533 public: 12534 Map_construction_expression(Type* type, Expression_list* vals, 12535 Location location) 12536 : Expression(EXPRESSION_MAP_CONSTRUCTION, location), 12537 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL) 12538 { go_assert(vals == NULL || vals->size() % 2 == 0); } 12539 12540 protected: 12541 int 12542 do_traverse(Traverse* traverse); 12543 12544 Expression* 12545 do_flatten(Gogo*, Named_object*, Statement_inserter*); 12546 12547 Type* 12548 do_type() 12549 { return this->type_; } 12550 12551 void 12552 do_determine_type(const Type_context*); 12553 12554 void 12555 do_check_types(Gogo*); 12556 12557 Expression* 12558 do_copy() 12559 { 12560 return new Map_construction_expression(this->type_, 12561 (this->vals_ == NULL 12562 ? NULL 12563 : this->vals_->copy()), 12564 this->location()); 12565 } 12566 12567 Bexpression* 12568 do_get_backend(Translate_context*); 12569 12570 void 12571 do_export(Export*) const; 12572 12573 void 12574 do_dump_expression(Ast_dump_context*) const; 12575 12576 private: 12577 // The type of the map to construct. 12578 Type* type_; 12579 // The list of values. 12580 Expression_list* vals_; 12581 // The type of the key-value pair struct for each map element. 12582 Struct_type* element_type_; 12583 // A temporary reference to the variable storing the constructor initializer. 12584 Temporary_statement* constructor_temp_; 12585}; 12586 12587// Traversal. 12588 12589int 12590Map_construction_expression::do_traverse(Traverse* traverse) 12591{ 12592 if (this->vals_ != NULL 12593 && this->vals_->traverse(traverse) == TRAVERSE_EXIT) 12594 return TRAVERSE_EXIT; 12595 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 12596 return TRAVERSE_EXIT; 12597 return TRAVERSE_CONTINUE; 12598} 12599 12600// Flatten constructor initializer into a temporary variable since 12601// we need to take its address for __go_construct_map. 12602 12603Expression* 12604Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, 12605 Statement_inserter* inserter) 12606{ 12607 if (!this->is_error_expression() 12608 && this->vals_ != NULL 12609 && !this->vals_->empty() 12610 && this->constructor_temp_ == NULL) 12611 { 12612 Map_type* mt = this->type_->map_type(); 12613 Type* key_type = mt->key_type(); 12614 Type* val_type = mt->val_type(); 12615 this->element_type_ = Type::make_builtin_struct_type(2, 12616 "__key", key_type, 12617 "__val", val_type); 12618 12619 Expression_list* value_pairs = new Expression_list(); 12620 Location loc = this->location(); 12621 12622 size_t i = 0; 12623 for (Expression_list::const_iterator pv = this->vals_->begin(); 12624 pv != this->vals_->end(); 12625 ++pv, ++i) 12626 { 12627 Expression_list* key_value_pair = new Expression_list(); 12628 Expression* key = *pv; 12629 if (key->type()->interface_type() != NULL && !key->is_variable()) 12630 { 12631 Temporary_statement* temp = 12632 Statement::make_temporary(NULL, key, loc); 12633 inserter->insert(temp); 12634 key = Expression::make_temporary_reference(temp, loc); 12635 } 12636 key = Expression::convert_for_assignment(gogo, key_type, key, loc); 12637 12638 ++pv; 12639 Expression* val = *pv; 12640 if (val->type()->interface_type() != NULL && !val->is_variable()) 12641 { 12642 Temporary_statement* temp = 12643 Statement::make_temporary(NULL, val, loc); 12644 inserter->insert(temp); 12645 val = Expression::make_temporary_reference(temp, loc); 12646 } 12647 val = Expression::convert_for_assignment(gogo, val_type, val, loc); 12648 12649 key_value_pair->push_back(key); 12650 key_value_pair->push_back(val); 12651 value_pairs->push_back( 12652 Expression::make_struct_composite_literal(this->element_type_, 12653 key_value_pair, loc)); 12654 } 12655 12656 Expression* element_count = Expression::make_integer_ul(i, NULL, loc); 12657 Type* ctor_type = 12658 Type::make_array_type(this->element_type_, element_count); 12659 Expression* constructor = 12660 new Fixed_array_construction_expression(ctor_type, NULL, 12661 value_pairs, loc); 12662 12663 this->constructor_temp_ = 12664 Statement::make_temporary(NULL, constructor, loc); 12665 constructor->issue_nil_check(); 12666 this->constructor_temp_->set_is_address_taken(); 12667 inserter->insert(this->constructor_temp_); 12668 } 12669 12670 return this; 12671} 12672 12673// Final type determination. 12674 12675void 12676Map_construction_expression::do_determine_type(const Type_context*) 12677{ 12678 if (this->vals_ == NULL) 12679 return; 12680 12681 Map_type* mt = this->type_->map_type(); 12682 Type_context key_context(mt->key_type(), false); 12683 Type_context val_context(mt->val_type(), false); 12684 for (Expression_list::const_iterator pv = this->vals_->begin(); 12685 pv != this->vals_->end(); 12686 ++pv) 12687 { 12688 (*pv)->determine_type(&key_context); 12689 ++pv; 12690 (*pv)->determine_type(&val_context); 12691 } 12692} 12693 12694// Check types. 12695 12696void 12697Map_construction_expression::do_check_types(Gogo*) 12698{ 12699 if (this->vals_ == NULL) 12700 return; 12701 12702 Map_type* mt = this->type_->map_type(); 12703 int i = 0; 12704 Type* key_type = mt->key_type(); 12705 Type* val_type = mt->val_type(); 12706 for (Expression_list::const_iterator pv = this->vals_->begin(); 12707 pv != this->vals_->end(); 12708 ++pv, ++i) 12709 { 12710 if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) 12711 { 12712 error_at((*pv)->location(), 12713 "incompatible type for element %d key in map construction", 12714 i + 1); 12715 this->set_is_error(); 12716 } 12717 ++pv; 12718 if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) 12719 { 12720 error_at((*pv)->location(), 12721 ("incompatible type for element %d value " 12722 "in map construction"), 12723 i + 1); 12724 this->set_is_error(); 12725 } 12726 } 12727} 12728 12729// Return the backend representation for constructing a map. 12730 12731Bexpression* 12732Map_construction_expression::do_get_backend(Translate_context* context) 12733{ 12734 if (this->is_error_expression()) 12735 return context->backend()->error_expression(); 12736 Location loc = this->location(); 12737 12738 size_t i = 0; 12739 Expression* ventries; 12740 if (this->vals_ == NULL || this->vals_->empty()) 12741 ventries = Expression::make_nil(loc); 12742 else 12743 { 12744 go_assert(this->constructor_temp_ != NULL); 12745 i = this->vals_->size() / 2; 12746 12747 Expression* ctor_ref = 12748 Expression::make_temporary_reference(this->constructor_temp_, loc); 12749 ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); 12750 } 12751 12752 Map_type* mt = this->type_->map_type(); 12753 if (this->element_type_ == NULL) 12754 this->element_type_ = 12755 Type::make_builtin_struct_type(2, 12756 "__key", mt->key_type(), 12757 "__val", mt->val_type()); 12758 Expression* descriptor = Expression::make_map_descriptor(mt, loc); 12759 12760 Type* uintptr_t = Type::lookup_integer_type("uintptr"); 12761 Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); 12762 12763 Expression* entry_size = 12764 Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); 12765 12766 unsigned int field_index; 12767 const Struct_field* valfield = 12768 this->element_type_->find_local_field("__val", &field_index); 12769 Expression* val_offset = 12770 Expression::make_struct_field_offset(this->element_type_, valfield); 12771 Expression* val_size = 12772 Expression::make_type_info(mt->val_type(), TYPE_INFO_SIZE); 12773 12774 Expression* map_ctor = 12775 Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 6, descriptor, count, 12776 entry_size, val_offset, val_size, ventries); 12777 return map_ctor->get_backend(context); 12778} 12779 12780// Export an array construction. 12781 12782void 12783Map_construction_expression::do_export(Export* exp) const 12784{ 12785 exp->write_c_string("convert("); 12786 exp->write_type(this->type_); 12787 for (Expression_list::const_iterator pv = this->vals_->begin(); 12788 pv != this->vals_->end(); 12789 ++pv) 12790 { 12791 exp->write_c_string(", "); 12792 (*pv)->export_expression(exp); 12793 } 12794 exp->write_c_string(")"); 12795} 12796 12797// Dump ast representation for a map construction expression. 12798 12799void 12800Map_construction_expression::do_dump_expression( 12801 Ast_dump_context* ast_dump_context) const 12802{ 12803 ast_dump_context->ostream() << "{" ; 12804 ast_dump_context->dump_expression_list(this->vals_, true); 12805 ast_dump_context->ostream() << "}"; 12806} 12807 12808// A general composite literal. This is lowered to a type specific 12809// version. 12810 12811class Composite_literal_expression : public Parser_expression 12812{ 12813 public: 12814 Composite_literal_expression(Type* type, int depth, bool has_keys, 12815 Expression_list* vals, bool all_are_names, 12816 Location location) 12817 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), 12818 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys), 12819 all_are_names_(all_are_names) 12820 { } 12821 12822 protected: 12823 int 12824 do_traverse(Traverse* traverse); 12825 12826 Expression* 12827 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 12828 12829 Expression* 12830 do_copy() 12831 { 12832 return new Composite_literal_expression(this->type_, this->depth_, 12833 this->has_keys_, 12834 (this->vals_ == NULL 12835 ? NULL 12836 : this->vals_->copy()), 12837 this->all_are_names_, 12838 this->location()); 12839 } 12840 12841 void 12842 do_dump_expression(Ast_dump_context*) const; 12843 12844 private: 12845 Expression* 12846 lower_struct(Gogo*, Type*); 12847 12848 Expression* 12849 lower_array(Type*); 12850 12851 Expression* 12852 make_array(Type*, const std::vector<unsigned long>*, Expression_list*); 12853 12854 Expression* 12855 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); 12856 12857 // The type of the composite literal. 12858 Type* type_; 12859 // The depth within a list of composite literals within a composite 12860 // literal, when the type is omitted. 12861 int depth_; 12862 // The values to put in the composite literal. 12863 Expression_list* vals_; 12864 // If this is true, then VALS_ is a list of pairs: a key and a 12865 // value. In an array initializer, a missing key will be NULL. 12866 bool has_keys_; 12867 // If this is true, then HAS_KEYS_ is true, and every key is a 12868 // simple identifier. 12869 bool all_are_names_; 12870}; 12871 12872// Traversal. 12873 12874int 12875Composite_literal_expression::do_traverse(Traverse* traverse) 12876{ 12877 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 12878 return TRAVERSE_EXIT; 12879 12880 // If this is a struct composite literal with keys, then the keys 12881 // are field names, not expressions. We don't want to traverse them 12882 // in that case. If we do, we can give an erroneous error "variable 12883 // initializer refers to itself." See bug482.go in the testsuite. 12884 if (this->has_keys_ && this->vals_ != NULL) 12885 { 12886 // The type may not be resolvable at this point. 12887 Type* type = this->type_; 12888 12889 for (int depth = this->depth_; depth > 0; --depth) 12890 { 12891 if (type->array_type() != NULL) 12892 type = type->array_type()->element_type(); 12893 else if (type->map_type() != NULL) 12894 type = type->map_type()->val_type(); 12895 else 12896 { 12897 // This error will be reported during lowering. 12898 return TRAVERSE_CONTINUE; 12899 } 12900 } 12901 12902 while (true) 12903 { 12904 if (type->classification() == Type::TYPE_NAMED) 12905 type = type->named_type()->real_type(); 12906 else if (type->classification() == Type::TYPE_FORWARD) 12907 { 12908 Type* t = type->forwarded(); 12909 if (t == type) 12910 break; 12911 type = t; 12912 } 12913 else 12914 break; 12915 } 12916 12917 if (type->classification() == Type::TYPE_STRUCT) 12918 { 12919 Expression_list::iterator p = this->vals_->begin(); 12920 while (p != this->vals_->end()) 12921 { 12922 // Skip key. 12923 ++p; 12924 go_assert(p != this->vals_->end()); 12925 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) 12926 return TRAVERSE_EXIT; 12927 ++p; 12928 } 12929 return TRAVERSE_CONTINUE; 12930 } 12931 } 12932 12933 if (this->vals_ != NULL) 12934 return this->vals_->traverse(traverse); 12935 12936 return TRAVERSE_CONTINUE; 12937} 12938 12939// Lower a generic composite literal into a specific version based on 12940// the type. 12941 12942Expression* 12943Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, 12944 Statement_inserter* inserter, int) 12945{ 12946 Type* type = this->type_; 12947 12948 for (int depth = this->depth_; depth > 0; --depth) 12949 { 12950 if (type->array_type() != NULL) 12951 type = type->array_type()->element_type(); 12952 else if (type->map_type() != NULL) 12953 type = type->map_type()->val_type(); 12954 else 12955 { 12956 if (!type->is_error()) 12957 error_at(this->location(), 12958 ("may only omit types within composite literals " 12959 "of slice, array, or map type")); 12960 return Expression::make_error(this->location()); 12961 } 12962 } 12963 12964 Type *pt = type->points_to(); 12965 bool is_pointer = false; 12966 if (pt != NULL) 12967 { 12968 is_pointer = true; 12969 type = pt; 12970 } 12971 12972 Expression* ret; 12973 if (type->is_error()) 12974 return Expression::make_error(this->location()); 12975 else if (type->struct_type() != NULL) 12976 ret = this->lower_struct(gogo, type); 12977 else if (type->array_type() != NULL) 12978 ret = this->lower_array(type); 12979 else if (type->map_type() != NULL) 12980 ret = this->lower_map(gogo, function, inserter, type); 12981 else 12982 { 12983 error_at(this->location(), 12984 ("expected struct, slice, array, or map type " 12985 "for composite literal")); 12986 return Expression::make_error(this->location()); 12987 } 12988 12989 if (is_pointer) 12990 ret = Expression::make_heap_expression(ret, this->location()); 12991 12992 return ret; 12993} 12994 12995// Lower a struct composite literal. 12996 12997Expression* 12998Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) 12999{ 13000 Location location = this->location(); 13001 Struct_type* st = type->struct_type(); 13002 if (this->vals_ == NULL || !this->has_keys_) 13003 { 13004 if (this->vals_ != NULL 13005 && !this->vals_->empty() 13006 && type->named_type() != NULL 13007 && type->named_type()->named_object()->package() != NULL) 13008 { 13009 for (Struct_field_list::const_iterator pf = st->fields()->begin(); 13010 pf != st->fields()->end(); 13011 ++pf) 13012 { 13013 if (Gogo::is_hidden_name(pf->field_name()) 13014 || pf->is_embedded_builtin(gogo)) 13015 error_at(this->location(), 13016 "assignment of unexported field %qs in %qs literal", 13017 Gogo::message_name(pf->field_name()).c_str(), 13018 type->named_type()->message_name().c_str()); 13019 } 13020 } 13021 13022 return new Struct_construction_expression(type, this->vals_, location); 13023 } 13024 13025 size_t field_count = st->field_count(); 13026 std::vector<Expression*> vals(field_count); 13027 std::vector<int>* traverse_order = new(std::vector<int>); 13028 Expression_list::const_iterator p = this->vals_->begin(); 13029 Expression* external_expr = NULL; 13030 const Named_object* external_no = NULL; 13031 while (p != this->vals_->end()) 13032 { 13033 Expression* name_expr = *p; 13034 13035 ++p; 13036 go_assert(p != this->vals_->end()); 13037 Expression* val = *p; 13038 13039 ++p; 13040 13041 if (name_expr == NULL) 13042 { 13043 error_at(val->location(), "mixture of field and value initializers"); 13044 return Expression::make_error(location); 13045 } 13046 13047 bool bad_key = false; 13048 std::string name; 13049 const Named_object* no = NULL; 13050 switch (name_expr->classification()) 13051 { 13052 case EXPRESSION_UNKNOWN_REFERENCE: 13053 name = name_expr->unknown_expression()->name(); 13054 if (type->named_type() != NULL) 13055 { 13056 // If the named object found for this field name comes from a 13057 // different package than the struct it is a part of, do not count 13058 // this incorrect lookup as a usage of the object's package. 13059 no = name_expr->unknown_expression()->named_object(); 13060 if (no->package() != NULL 13061 && no->package() != type->named_type()->named_object()->package()) 13062 no->package()->forget_usage(name_expr); 13063 } 13064 break; 13065 13066 case EXPRESSION_CONST_REFERENCE: 13067 no = static_cast<Const_expression*>(name_expr)->named_object(); 13068 break; 13069 13070 case EXPRESSION_TYPE: 13071 { 13072 Type* t = name_expr->type(); 13073 Named_type* nt = t->named_type(); 13074 if (nt == NULL) 13075 bad_key = true; 13076 else 13077 no = nt->named_object(); 13078 } 13079 break; 13080 13081 case EXPRESSION_VAR_REFERENCE: 13082 no = name_expr->var_expression()->named_object(); 13083 break; 13084 13085 case EXPRESSION_FUNC_REFERENCE: 13086 no = name_expr->func_expression()->named_object(); 13087 break; 13088 13089 case EXPRESSION_UNARY: 13090 // If there is a local variable around with the same name as 13091 // the field, and this occurs in the closure, then the 13092 // parser may turn the field reference into an indirection 13093 // through the closure. FIXME: This is a mess. 13094 { 13095 bad_key = true; 13096 Unary_expression* ue = static_cast<Unary_expression*>(name_expr); 13097 if (ue->op() == OPERATOR_MULT) 13098 { 13099 Field_reference_expression* fre = 13100 ue->operand()->field_reference_expression(); 13101 if (fre != NULL) 13102 { 13103 Struct_type* st = 13104 fre->expr()->type()->deref()->struct_type(); 13105 if (st != NULL) 13106 { 13107 const Struct_field* sf = st->field(fre->field_index()); 13108 name = sf->field_name(); 13109 13110 // See below. FIXME. 13111 if (!Gogo::is_hidden_name(name) 13112 && name[0] >= 'a' 13113 && name[0] <= 'z') 13114 { 13115 if (gogo->lookup_global(name.c_str()) != NULL) 13116 name = gogo->pack_hidden_name(name, false); 13117 } 13118 13119 char buf[20]; 13120 snprintf(buf, sizeof buf, "%u", fre->field_index()); 13121 size_t buflen = strlen(buf); 13122 if (name.compare(name.length() - buflen, buflen, buf) 13123 == 0) 13124 { 13125 name = name.substr(0, name.length() - buflen); 13126 bad_key = false; 13127 } 13128 } 13129 } 13130 } 13131 } 13132 break; 13133 13134 default: 13135 bad_key = true; 13136 break; 13137 } 13138 if (bad_key) 13139 { 13140 error_at(name_expr->location(), "expected struct field name"); 13141 return Expression::make_error(location); 13142 } 13143 13144 if (no != NULL) 13145 { 13146 if (no->package() != NULL && external_expr == NULL) 13147 { 13148 external_expr = name_expr; 13149 external_no = no; 13150 } 13151 13152 name = no->name(); 13153 13154 // A predefined name won't be packed. If it starts with a 13155 // lower case letter we need to check for that case, because 13156 // the field name will be packed. FIXME. 13157 if (!Gogo::is_hidden_name(name) 13158 && name[0] >= 'a' 13159 && name[0] <= 'z') 13160 { 13161 Named_object* gno = gogo->lookup_global(name.c_str()); 13162 if (gno == no) 13163 name = gogo->pack_hidden_name(name, false); 13164 } 13165 } 13166 13167 unsigned int index; 13168 const Struct_field* sf = st->find_local_field(name, &index); 13169 if (sf == NULL) 13170 { 13171 error_at(name_expr->location(), "unknown field %qs in %qs", 13172 Gogo::message_name(name).c_str(), 13173 (type->named_type() != NULL 13174 ? type->named_type()->message_name().c_str() 13175 : "unnamed struct")); 13176 return Expression::make_error(location); 13177 } 13178 if (vals[index] != NULL) 13179 { 13180 error_at(name_expr->location(), 13181 "duplicate value for field %qs in %qs", 13182 Gogo::message_name(name).c_str(), 13183 (type->named_type() != NULL 13184 ? type->named_type()->message_name().c_str() 13185 : "unnamed struct")); 13186 return Expression::make_error(location); 13187 } 13188 13189 if (type->named_type() != NULL 13190 && type->named_type()->named_object()->package() != NULL 13191 && (Gogo::is_hidden_name(sf->field_name()) 13192 || sf->is_embedded_builtin(gogo))) 13193 error_at(name_expr->location(), 13194 "assignment of unexported field %qs in %qs literal", 13195 Gogo::message_name(sf->field_name()).c_str(), 13196 type->named_type()->message_name().c_str()); 13197 13198 vals[index] = val; 13199 traverse_order->push_back(index); 13200 } 13201 13202 if (!this->all_are_names_) 13203 { 13204 // This is a weird case like bug462 in the testsuite. 13205 if (external_expr == NULL) 13206 error_at(this->location(), "unknown field in %qs literal", 13207 (type->named_type() != NULL 13208 ? type->named_type()->message_name().c_str() 13209 : "unnamed struct")); 13210 else 13211 error_at(external_expr->location(), "unknown field %qs in %qs", 13212 external_no->message_name().c_str(), 13213 (type->named_type() != NULL 13214 ? type->named_type()->message_name().c_str() 13215 : "unnamed struct")); 13216 return Expression::make_error(location); 13217 } 13218 13219 Expression_list* list = new Expression_list; 13220 list->reserve(field_count); 13221 for (size_t i = 0; i < field_count; ++i) 13222 list->push_back(vals[i]); 13223 13224 Struct_construction_expression* ret = 13225 new Struct_construction_expression(type, list, location); 13226 ret->set_traverse_order(traverse_order); 13227 return ret; 13228} 13229 13230// Used to sort an index/value array. 13231 13232class Index_value_compare 13233{ 13234 public: 13235 bool 13236 operator()(const std::pair<unsigned long, Expression*>& a, 13237 const std::pair<unsigned long, Expression*>& b) 13238 { return a.first < b.first; } 13239}; 13240 13241// Lower an array composite literal. 13242 13243Expression* 13244Composite_literal_expression::lower_array(Type* type) 13245{ 13246 Location location = this->location(); 13247 if (this->vals_ == NULL || !this->has_keys_) 13248 return this->make_array(type, NULL, this->vals_); 13249 13250 std::vector<unsigned long>* indexes = new std::vector<unsigned long>; 13251 indexes->reserve(this->vals_->size()); 13252 bool indexes_out_of_order = false; 13253 Expression_list* vals = new Expression_list(); 13254 vals->reserve(this->vals_->size()); 13255 unsigned long index = 0; 13256 Expression_list::const_iterator p = this->vals_->begin(); 13257 while (p != this->vals_->end()) 13258 { 13259 Expression* index_expr = *p; 13260 13261 ++p; 13262 go_assert(p != this->vals_->end()); 13263 Expression* val = *p; 13264 13265 ++p; 13266 13267 if (index_expr == NULL) 13268 { 13269 if (!indexes->empty()) 13270 indexes->push_back(index); 13271 } 13272 else 13273 { 13274 if (indexes->empty() && !vals->empty()) 13275 { 13276 for (size_t i = 0; i < vals->size(); ++i) 13277 indexes->push_back(i); 13278 } 13279 13280 Numeric_constant nc; 13281 if (!index_expr->numeric_constant_value(&nc)) 13282 { 13283 error_at(index_expr->location(), 13284 "index expression is not integer constant"); 13285 return Expression::make_error(location); 13286 } 13287 13288 switch (nc.to_unsigned_long(&index)) 13289 { 13290 case Numeric_constant::NC_UL_VALID: 13291 break; 13292 case Numeric_constant::NC_UL_NOTINT: 13293 error_at(index_expr->location(), 13294 "index expression is not integer constant"); 13295 return Expression::make_error(location); 13296 case Numeric_constant::NC_UL_NEGATIVE: 13297 error_at(index_expr->location(), "index expression is negative"); 13298 return Expression::make_error(location); 13299 case Numeric_constant::NC_UL_BIG: 13300 error_at(index_expr->location(), "index value overflow"); 13301 return Expression::make_error(location); 13302 default: 13303 go_unreachable(); 13304 } 13305 13306 Named_type* ntype = Type::lookup_integer_type("int"); 13307 Integer_type* inttype = ntype->integer_type(); 13308 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) 13309 && index >> (inttype->bits() - 1) != 0) 13310 { 13311 error_at(index_expr->location(), "index value overflow"); 13312 return Expression::make_error(location); 13313 } 13314 13315 if (std::find(indexes->begin(), indexes->end(), index) 13316 != indexes->end()) 13317 { 13318 error_at(index_expr->location(), "duplicate value for index %lu", 13319 index); 13320 return Expression::make_error(location); 13321 } 13322 13323 if (!indexes->empty() && index < indexes->back()) 13324 indexes_out_of_order = true; 13325 13326 indexes->push_back(index); 13327 } 13328 13329 vals->push_back(val); 13330 13331 ++index; 13332 } 13333 13334 if (indexes->empty()) 13335 { 13336 delete indexes; 13337 indexes = NULL; 13338 } 13339 13340 if (indexes_out_of_order) 13341 { 13342 typedef std::vector<std::pair<unsigned long, Expression*> > V; 13343 13344 V v; 13345 v.reserve(indexes->size()); 13346 std::vector<unsigned long>::const_iterator pi = indexes->begin(); 13347 for (Expression_list::const_iterator pe = vals->begin(); 13348 pe != vals->end(); 13349 ++pe, ++pi) 13350 v.push_back(std::make_pair(*pi, *pe)); 13351 13352 std::sort(v.begin(), v.end(), Index_value_compare()); 13353 13354 delete indexes; 13355 delete vals; 13356 indexes = new std::vector<unsigned long>(); 13357 indexes->reserve(v.size()); 13358 vals = new Expression_list(); 13359 vals->reserve(v.size()); 13360 13361 for (V::const_iterator p = v.begin(); p != v.end(); ++p) 13362 { 13363 indexes->push_back(p->first); 13364 vals->push_back(p->second); 13365 } 13366 } 13367 13368 return this->make_array(type, indexes, vals); 13369} 13370 13371// Actually build the array composite literal. This handles 13372// [...]{...}. 13373 13374Expression* 13375Composite_literal_expression::make_array( 13376 Type* type, 13377 const std::vector<unsigned long>* indexes, 13378 Expression_list* vals) 13379{ 13380 Location location = this->location(); 13381 Array_type* at = type->array_type(); 13382 13383 if (at->length() != NULL && at->length()->is_nil_expression()) 13384 { 13385 size_t size; 13386 if (vals == NULL) 13387 size = 0; 13388 else if (indexes != NULL) 13389 size = indexes->back() + 1; 13390 else 13391 { 13392 size = vals->size(); 13393 Integer_type* it = Type::lookup_integer_type("int")->integer_type(); 13394 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) 13395 && size >> (it->bits() - 1) != 0) 13396 { 13397 error_at(location, "too many elements in composite literal"); 13398 return Expression::make_error(location); 13399 } 13400 } 13401 13402 Expression* elen = Expression::make_integer_ul(size, NULL, location); 13403 at = Type::make_array_type(at->element_type(), elen); 13404 type = at; 13405 } 13406 else if (at->length() != NULL 13407 && !at->length()->is_error_expression() 13408 && this->vals_ != NULL) 13409 { 13410 Numeric_constant nc; 13411 unsigned long val; 13412 if (at->length()->numeric_constant_value(&nc) 13413 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) 13414 { 13415 if (indexes == NULL) 13416 { 13417 if (this->vals_->size() > val) 13418 { 13419 error_at(location, "too many elements in composite literal"); 13420 return Expression::make_error(location); 13421 } 13422 } 13423 else 13424 { 13425 unsigned long max = indexes->back(); 13426 if (max >= val) 13427 { 13428 error_at(location, 13429 ("some element keys in composite literal " 13430 "are out of range")); 13431 return Expression::make_error(location); 13432 } 13433 } 13434 } 13435 } 13436 13437 if (at->length() != NULL) 13438 return new Fixed_array_construction_expression(type, indexes, vals, 13439 location); 13440 else 13441 return new Slice_construction_expression(type, indexes, vals, location); 13442} 13443 13444// Lower a map composite literal. 13445 13446Expression* 13447Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, 13448 Statement_inserter* inserter, 13449 Type* type) 13450{ 13451 Location location = this->location(); 13452 if (this->vals_ != NULL) 13453 { 13454 if (!this->has_keys_) 13455 { 13456 error_at(location, "map composite literal must have keys"); 13457 return Expression::make_error(location); 13458 } 13459 13460 for (Expression_list::iterator p = this->vals_->begin(); 13461 p != this->vals_->end(); 13462 p += 2) 13463 { 13464 if (*p == NULL) 13465 { 13466 ++p; 13467 error_at((*p)->location(), 13468 "map composite literal must have keys for every value"); 13469 return Expression::make_error(location); 13470 } 13471 // Make sure we have lowered the key; it may not have been 13472 // lowered in order to handle keys for struct composite 13473 // literals. Lower it now to get the right error message. 13474 if ((*p)->unknown_expression() != NULL) 13475 { 13476 (*p)->unknown_expression()->clear_is_composite_literal_key(); 13477 gogo->lower_expression(function, inserter, &*p); 13478 go_assert((*p)->is_error_expression()); 13479 return Expression::make_error(location); 13480 } 13481 } 13482 } 13483 13484 return new Map_construction_expression(type, this->vals_, location); 13485} 13486 13487// Dump ast representation for a composite literal expression. 13488 13489void 13490Composite_literal_expression::do_dump_expression( 13491 Ast_dump_context* ast_dump_context) const 13492{ 13493 ast_dump_context->ostream() << "composite("; 13494 ast_dump_context->dump_type(this->type_); 13495 ast_dump_context->ostream() << ", {"; 13496 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); 13497 ast_dump_context->ostream() << "})"; 13498} 13499 13500// Make a composite literal expression. 13501 13502Expression* 13503Expression::make_composite_literal(Type* type, int depth, bool has_keys, 13504 Expression_list* vals, bool all_are_names, 13505 Location location) 13506{ 13507 return new Composite_literal_expression(type, depth, has_keys, vals, 13508 all_are_names, location); 13509} 13510 13511// Return whether this expression is a composite literal. 13512 13513bool 13514Expression::is_composite_literal() const 13515{ 13516 switch (this->classification_) 13517 { 13518 case EXPRESSION_COMPOSITE_LITERAL: 13519 case EXPRESSION_STRUCT_CONSTRUCTION: 13520 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: 13521 case EXPRESSION_SLICE_CONSTRUCTION: 13522 case EXPRESSION_MAP_CONSTRUCTION: 13523 return true; 13524 default: 13525 return false; 13526 } 13527} 13528 13529// Return whether this expression is a composite literal which is not 13530// constant. 13531 13532bool 13533Expression::is_nonconstant_composite_literal() const 13534{ 13535 switch (this->classification_) 13536 { 13537 case EXPRESSION_STRUCT_CONSTRUCTION: 13538 { 13539 const Struct_construction_expression *psce = 13540 static_cast<const Struct_construction_expression*>(this); 13541 return !psce->is_constant_struct(); 13542 } 13543 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: 13544 { 13545 const Fixed_array_construction_expression *pace = 13546 static_cast<const Fixed_array_construction_expression*>(this); 13547 return !pace->is_constant_array(); 13548 } 13549 case EXPRESSION_SLICE_CONSTRUCTION: 13550 { 13551 const Slice_construction_expression *pace = 13552 static_cast<const Slice_construction_expression*>(this); 13553 return !pace->is_constant_array(); 13554 } 13555 case EXPRESSION_MAP_CONSTRUCTION: 13556 return true; 13557 default: 13558 return false; 13559 } 13560} 13561 13562// Return true if this is a variable or temporary_variable. 13563 13564bool 13565Expression::is_variable() const 13566{ 13567 switch (this->classification_) 13568 { 13569 case EXPRESSION_VAR_REFERENCE: 13570 case EXPRESSION_TEMPORARY_REFERENCE: 13571 case EXPRESSION_SET_AND_USE_TEMPORARY: 13572 return true; 13573 default: 13574 return false; 13575 } 13576} 13577 13578// Return true if this is a reference to a local variable. 13579 13580bool 13581Expression::is_local_variable() const 13582{ 13583 const Var_expression* ve = this->var_expression(); 13584 if (ve == NULL) 13585 return false; 13586 const Named_object* no = ve->named_object(); 13587 return (no->is_result_variable() 13588 || (no->is_variable() && !no->var_value()->is_global())); 13589} 13590 13591// Class Type_guard_expression. 13592 13593// Traversal. 13594 13595int 13596Type_guard_expression::do_traverse(Traverse* traverse) 13597{ 13598 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT 13599 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 13600 return TRAVERSE_EXIT; 13601 return TRAVERSE_CONTINUE; 13602} 13603 13604Expression* 13605Type_guard_expression::do_flatten(Gogo*, Named_object*, 13606 Statement_inserter* inserter) 13607{ 13608 if (!this->expr_->is_variable()) 13609 { 13610 Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, 13611 this->location()); 13612 inserter->insert(temp); 13613 this->expr_ = 13614 Expression::make_temporary_reference(temp, this->location()); 13615 } 13616 return this; 13617} 13618 13619// Check types of a type guard expression. The expression must have 13620// an interface type, but the actual type conversion is checked at run 13621// time. 13622 13623void 13624Type_guard_expression::do_check_types(Gogo*) 13625{ 13626 Type* expr_type = this->expr_->type(); 13627 if (expr_type->interface_type() == NULL) 13628 { 13629 if (!expr_type->is_error() && !this->type_->is_error()) 13630 this->report_error(_("type assertion only valid for interface types")); 13631 this->set_is_error(); 13632 } 13633 else if (this->type_->interface_type() == NULL) 13634 { 13635 std::string reason; 13636 if (!expr_type->interface_type()->implements_interface(this->type_, 13637 &reason)) 13638 { 13639 if (!this->type_->is_error()) 13640 { 13641 if (reason.empty()) 13642 this->report_error(_("impossible type assertion: " 13643 "type does not implement interface")); 13644 else 13645 error_at(this->location(), 13646 ("impossible type assertion: " 13647 "type does not implement interface (%s)"), 13648 reason.c_str()); 13649 } 13650 this->set_is_error(); 13651 } 13652 } 13653} 13654 13655// Return the backend representation for a type guard expression. 13656 13657Bexpression* 13658Type_guard_expression::do_get_backend(Translate_context* context) 13659{ 13660 Expression* conversion; 13661 if (this->type_->interface_type() != NULL) 13662 conversion = 13663 Expression::convert_interface_to_interface(this->type_, this->expr_, 13664 true, this->location()); 13665 else 13666 conversion = 13667 Expression::convert_for_assignment(context->gogo(), this->type_, 13668 this->expr_, this->location()); 13669 13670 return conversion->get_backend(context); 13671} 13672 13673// Dump ast representation for a type guard expression. 13674 13675void 13676Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 13677 const 13678{ 13679 this->expr_->dump_expression(ast_dump_context); 13680 ast_dump_context->ostream() << "."; 13681 ast_dump_context->dump_type(this->type_); 13682} 13683 13684// Make a type guard expression. 13685 13686Expression* 13687Expression::make_type_guard(Expression* expr, Type* type, 13688 Location location) 13689{ 13690 return new Type_guard_expression(expr, type, location); 13691} 13692 13693// Class Heap_expression. 13694 13695// When you take the address of an escaping expression, it is allocated 13696// on the heap. This class implements that. 13697 13698class Heap_expression : public Expression 13699{ 13700 public: 13701 Heap_expression(Expression* expr, Location location) 13702 : Expression(EXPRESSION_HEAP, location), 13703 expr_(expr) 13704 { } 13705 13706 protected: 13707 int 13708 do_traverse(Traverse* traverse) 13709 { return Expression::traverse(&this->expr_, traverse); } 13710 13711 Type* 13712 do_type() 13713 { return Type::make_pointer_type(this->expr_->type()); } 13714 13715 void 13716 do_determine_type(const Type_context*) 13717 { this->expr_->determine_type_no_context(); } 13718 13719 Expression* 13720 do_copy() 13721 { 13722 return Expression::make_heap_expression(this->expr_->copy(), 13723 this->location()); 13724 } 13725 13726 Bexpression* 13727 do_get_backend(Translate_context*); 13728 13729 // We only export global objects, and the parser does not generate 13730 // this in global scope. 13731 void 13732 do_export(Export*) const 13733 { go_unreachable(); } 13734 13735 void 13736 do_dump_expression(Ast_dump_context*) const; 13737 13738 private: 13739 // The expression which is being put on the heap. 13740 Expression* expr_; 13741}; 13742 13743// Return the backend representation for allocating an expression on the heap. 13744 13745Bexpression* 13746Heap_expression::do_get_backend(Translate_context* context) 13747{ 13748 if (this->expr_->is_error_expression() || this->expr_->type()->is_error()) 13749 return context->backend()->error_expression(); 13750 13751 Location loc = this->location(); 13752 Gogo* gogo = context->gogo(); 13753 Btype* btype = this->type()->get_backend(gogo); 13754 Bexpression* space = Expression::make_allocation(this->expr_->type(), 13755 loc)->get_backend(context); 13756 13757 Bstatement* decl; 13758 Named_object* fn = context->function(); 13759 go_assert(fn != NULL); 13760 Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); 13761 Bvariable* space_temp = 13762 gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, 13763 space, true, loc, &decl); 13764 space = gogo->backend()->var_expression(space_temp, loc); 13765 Btype* expr_btype = this->expr_->type()->get_backend(gogo); 13766 Bexpression* ref = 13767 gogo->backend()->indirect_expression(expr_btype, space, true, loc); 13768 13769 Bexpression* bexpr = this->expr_->get_backend(context); 13770 Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc); 13771 decl = gogo->backend()->compound_statement(decl, assn); 13772 space = gogo->backend()->var_expression(space_temp, loc); 13773 return gogo->backend()->compound_expression(decl, space, loc); 13774} 13775 13776// Dump ast representation for a heap expression. 13777 13778void 13779Heap_expression::do_dump_expression( 13780 Ast_dump_context* ast_dump_context) const 13781{ 13782 ast_dump_context->ostream() << "&("; 13783 ast_dump_context->dump_expression(this->expr_); 13784 ast_dump_context->ostream() << ")"; 13785} 13786 13787// Allocate an expression on the heap. 13788 13789Expression* 13790Expression::make_heap_expression(Expression* expr, Location location) 13791{ 13792 return new Heap_expression(expr, location); 13793} 13794 13795// Class Receive_expression. 13796 13797// Return the type of a receive expression. 13798 13799Type* 13800Receive_expression::do_type() 13801{ 13802 Channel_type* channel_type = this->channel_->type()->channel_type(); 13803 if (channel_type == NULL) 13804 return Type::make_error_type(); 13805 return channel_type->element_type(); 13806} 13807 13808// Check types for a receive expression. 13809 13810void 13811Receive_expression::do_check_types(Gogo*) 13812{ 13813 Type* type = this->channel_->type(); 13814 if (type->is_error()) 13815 { 13816 this->set_is_error(); 13817 return; 13818 } 13819 if (type->channel_type() == NULL) 13820 { 13821 this->report_error(_("expected channel")); 13822 return; 13823 } 13824 if (!type->channel_type()->may_receive()) 13825 { 13826 this->report_error(_("invalid receive on send-only channel")); 13827 return; 13828 } 13829} 13830 13831// Flattening for receive expressions creates a temporary variable to store 13832// received data in for receives. 13833 13834Expression* 13835Receive_expression::do_flatten(Gogo*, Named_object*, 13836 Statement_inserter* inserter) 13837{ 13838 Channel_type* channel_type = this->channel_->type()->channel_type(); 13839 if (channel_type == NULL) 13840 { 13841 go_assert(saw_errors()); 13842 return this; 13843 } 13844 13845 Type* element_type = channel_type->element_type(); 13846 if (this->temp_receiver_ == NULL) 13847 { 13848 this->temp_receiver_ = Statement::make_temporary(element_type, NULL, 13849 this->location()); 13850 this->temp_receiver_->set_is_address_taken(); 13851 inserter->insert(this->temp_receiver_); 13852 } 13853 13854 return this; 13855} 13856 13857// Get the backend representation for a receive expression. 13858 13859Bexpression* 13860Receive_expression::do_get_backend(Translate_context* context) 13861{ 13862 Location loc = this->location(); 13863 13864 Channel_type* channel_type = this->channel_->type()->channel_type(); 13865 if (channel_type == NULL) 13866 { 13867 go_assert(this->channel_->type()->is_error()); 13868 return context->backend()->error_expression(); 13869 } 13870 Expression* td = Expression::make_type_descriptor(channel_type, loc); 13871 13872 Expression* recv_ref = 13873 Expression::make_temporary_reference(this->temp_receiver_, loc); 13874 Expression* recv_addr = 13875 Expression::make_temporary_reference(this->temp_receiver_, loc); 13876 recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); 13877 Expression* recv = 13878 Runtime::make_call(Runtime::RECEIVE, loc, 3, 13879 td, this->channel_, recv_addr); 13880 return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); 13881} 13882 13883// Dump ast representation for a receive expression. 13884 13885void 13886Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 13887{ 13888 ast_dump_context->ostream() << " <- " ; 13889 ast_dump_context->dump_expression(channel_); 13890} 13891 13892// Make a receive expression. 13893 13894Receive_expression* 13895Expression::make_receive(Expression* channel, Location location) 13896{ 13897 return new Receive_expression(channel, location); 13898} 13899 13900// An expression which evaluates to a pointer to the type descriptor 13901// of a type. 13902 13903class Type_descriptor_expression : public Expression 13904{ 13905 public: 13906 Type_descriptor_expression(Type* type, Location location) 13907 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), 13908 type_(type) 13909 { } 13910 13911 protected: 13912 int 13913 do_traverse(Traverse*); 13914 13915 Type* 13916 do_type() 13917 { return Type::make_type_descriptor_ptr_type(); } 13918 13919 bool 13920 do_is_immutable() const 13921 { return true; } 13922 13923 void 13924 do_determine_type(const Type_context*) 13925 { } 13926 13927 Expression* 13928 do_copy() 13929 { return this; } 13930 13931 Bexpression* 13932 do_get_backend(Translate_context* context) 13933 { 13934 return this->type_->type_descriptor_pointer(context->gogo(), 13935 this->location()); 13936 } 13937 13938 void 13939 do_dump_expression(Ast_dump_context*) const; 13940 13941 private: 13942 // The type for which this is the descriptor. 13943 Type* type_; 13944}; 13945 13946int 13947Type_descriptor_expression::do_traverse(Traverse* traverse) 13948{ 13949 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 13950 return TRAVERSE_EXIT; 13951 return TRAVERSE_CONTINUE; 13952} 13953 13954// Dump ast representation for a type descriptor expression. 13955 13956void 13957Type_descriptor_expression::do_dump_expression( 13958 Ast_dump_context* ast_dump_context) const 13959{ 13960 ast_dump_context->dump_type(this->type_); 13961} 13962 13963// Make a type descriptor expression. 13964 13965Expression* 13966Expression::make_type_descriptor(Type* type, Location location) 13967{ 13968 return new Type_descriptor_expression(type, location); 13969} 13970 13971// An expression which evaluates to a pointer to the Garbage Collection symbol 13972// of a type. 13973 13974class GC_symbol_expression : public Expression 13975{ 13976 public: 13977 GC_symbol_expression(Type* type) 13978 : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), 13979 type_(type) 13980 {} 13981 13982 protected: 13983 Type* 13984 do_type() 13985 { return Type::lookup_integer_type("uintptr"); } 13986 13987 bool 13988 do_is_immutable() const 13989 { return true; } 13990 13991 void 13992 do_determine_type(const Type_context*) 13993 { } 13994 13995 Expression* 13996 do_copy() 13997 { return this; } 13998 13999 Bexpression* 14000 do_get_backend(Translate_context* context) 14001 { return this->type_->gc_symbol_pointer(context->gogo()); } 14002 14003 void 14004 do_dump_expression(Ast_dump_context*) const; 14005 14006 private: 14007 // The type which this gc symbol describes. 14008 Type* type_; 14009}; 14010 14011// Dump ast representation for a gc symbol expression. 14012 14013void 14014GC_symbol_expression::do_dump_expression( 14015 Ast_dump_context* ast_dump_context) const 14016{ 14017 ast_dump_context->ostream() << "gcdata("; 14018 ast_dump_context->dump_type(this->type_); 14019 ast_dump_context->ostream() << ")"; 14020} 14021 14022// Make a gc symbol expression. 14023 14024Expression* 14025Expression::make_gc_symbol(Type* type) 14026{ 14027 return new GC_symbol_expression(type); 14028} 14029 14030// An expression which evaluates to some characteristic of a type. 14031// This is only used to initialize fields of a type descriptor. Using 14032// a new expression class is slightly inefficient but gives us a good 14033// separation between the frontend and the middle-end with regard to 14034// how types are laid out. 14035 14036class Type_info_expression : public Expression 14037{ 14038 public: 14039 Type_info_expression(Type* type, Type_info type_info) 14040 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), 14041 type_(type), type_info_(type_info) 14042 { } 14043 14044 protected: 14045 bool 14046 do_is_immutable() const 14047 { return true; } 14048 14049 Type* 14050 do_type(); 14051 14052 void 14053 do_determine_type(const Type_context*) 14054 { } 14055 14056 Expression* 14057 do_copy() 14058 { return this; } 14059 14060 Bexpression* 14061 do_get_backend(Translate_context* context); 14062 14063 void 14064 do_dump_expression(Ast_dump_context*) const; 14065 14066 private: 14067 // The type for which we are getting information. 14068 Type* type_; 14069 // What information we want. 14070 Type_info type_info_; 14071}; 14072 14073// The type is chosen to match what the type descriptor struct 14074// expects. 14075 14076Type* 14077Type_info_expression::do_type() 14078{ 14079 switch (this->type_info_) 14080 { 14081 case TYPE_INFO_SIZE: 14082 return Type::lookup_integer_type("uintptr"); 14083 case TYPE_INFO_ALIGNMENT: 14084 case TYPE_INFO_FIELD_ALIGNMENT: 14085 return Type::lookup_integer_type("uint8"); 14086 default: 14087 go_unreachable(); 14088 } 14089} 14090 14091// Return the backend representation for type information. 14092 14093Bexpression* 14094Type_info_expression::do_get_backend(Translate_context* context) 14095{ 14096 Btype* btype = this->type_->get_backend(context->gogo()); 14097 Gogo* gogo = context->gogo(); 14098 int64_t val; 14099 switch (this->type_info_) 14100 { 14101 case TYPE_INFO_SIZE: 14102 val = gogo->backend()->type_size(btype); 14103 break; 14104 case TYPE_INFO_ALIGNMENT: 14105 val = gogo->backend()->type_alignment(btype); 14106 break; 14107 case TYPE_INFO_FIELD_ALIGNMENT: 14108 val = gogo->backend()->type_field_alignment(btype); 14109 break; 14110 default: 14111 go_unreachable(); 14112 } 14113 Expression* e = Expression::make_integer_int64(val, this->type(), 14114 this->location()); 14115 return e->get_backend(context); 14116} 14117 14118// Dump ast representation for a type info expression. 14119 14120void 14121Type_info_expression::do_dump_expression( 14122 Ast_dump_context* ast_dump_context) const 14123{ 14124 ast_dump_context->ostream() << "typeinfo("; 14125 ast_dump_context->dump_type(this->type_); 14126 ast_dump_context->ostream() << ","; 14127 ast_dump_context->ostream() << 14128 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" 14129 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" 14130 : this->type_info_ == TYPE_INFO_SIZE ? "size " 14131 : "unknown"); 14132 ast_dump_context->ostream() << ")"; 14133} 14134 14135// Make a type info expression. 14136 14137Expression* 14138Expression::make_type_info(Type* type, Type_info type_info) 14139{ 14140 return new Type_info_expression(type, type_info); 14141} 14142 14143// An expression that evaluates to some characteristic of a slice. 14144// This is used when indexing, bound-checking, or nil checking a slice. 14145 14146class Slice_info_expression : public Expression 14147{ 14148 public: 14149 Slice_info_expression(Expression* slice, Slice_info slice_info, 14150 Location location) 14151 : Expression(EXPRESSION_SLICE_INFO, location), 14152 slice_(slice), slice_info_(slice_info) 14153 { } 14154 14155 protected: 14156 Type* 14157 do_type(); 14158 14159 void 14160 do_determine_type(const Type_context*) 14161 { } 14162 14163 Expression* 14164 do_copy() 14165 { 14166 return new Slice_info_expression(this->slice_->copy(), this->slice_info_, 14167 this->location()); 14168 } 14169 14170 Bexpression* 14171 do_get_backend(Translate_context* context); 14172 14173 void 14174 do_dump_expression(Ast_dump_context*) const; 14175 14176 void 14177 do_issue_nil_check() 14178 { this->slice_->issue_nil_check(); } 14179 14180 private: 14181 // The slice for which we are getting information. 14182 Expression* slice_; 14183 // What information we want. 14184 Slice_info slice_info_; 14185}; 14186 14187// Return the type of the slice info. 14188 14189Type* 14190Slice_info_expression::do_type() 14191{ 14192 switch (this->slice_info_) 14193 { 14194 case SLICE_INFO_VALUE_POINTER: 14195 return Type::make_pointer_type( 14196 this->slice_->type()->array_type()->element_type()); 14197 case SLICE_INFO_LENGTH: 14198 case SLICE_INFO_CAPACITY: 14199 return Type::lookup_integer_type("int"); 14200 default: 14201 go_unreachable(); 14202 } 14203} 14204 14205// Return the backend information for slice information. 14206 14207Bexpression* 14208Slice_info_expression::do_get_backend(Translate_context* context) 14209{ 14210 Gogo* gogo = context->gogo(); 14211 Bexpression* bslice = this->slice_->get_backend(context); 14212 switch (this->slice_info_) 14213 { 14214 case SLICE_INFO_VALUE_POINTER: 14215 case SLICE_INFO_LENGTH: 14216 case SLICE_INFO_CAPACITY: 14217 return gogo->backend()->struct_field_expression(bslice, this->slice_info_, 14218 this->location()); 14219 break; 14220 default: 14221 go_unreachable(); 14222 } 14223} 14224 14225// Dump ast representation for a type info expression. 14226 14227void 14228Slice_info_expression::do_dump_expression( 14229 Ast_dump_context* ast_dump_context) const 14230{ 14231 ast_dump_context->ostream() << "sliceinfo("; 14232 this->slice_->dump_expression(ast_dump_context); 14233 ast_dump_context->ostream() << ","; 14234 ast_dump_context->ostream() << 14235 (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" 14236 : this->slice_info_ == SLICE_INFO_LENGTH ? "length" 14237 : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " 14238 : "unknown"); 14239 ast_dump_context->ostream() << ")"; 14240} 14241 14242// Make a slice info expression. 14243 14244Expression* 14245Expression::make_slice_info(Expression* slice, Slice_info slice_info, 14246 Location location) 14247{ 14248 return new Slice_info_expression(slice, slice_info, location); 14249} 14250 14251// An expression that represents a slice value: a struct with value pointer, 14252// length, and capacity fields. 14253 14254class Slice_value_expression : public Expression 14255{ 14256 public: 14257 Slice_value_expression(Type* type, Expression* valptr, Expression* len, 14258 Expression* cap, Location location) 14259 : Expression(EXPRESSION_SLICE_VALUE, location), 14260 type_(type), valptr_(valptr), len_(len), cap_(cap) 14261 { } 14262 14263 protected: 14264 int 14265 do_traverse(Traverse*); 14266 14267 Type* 14268 do_type() 14269 { return this->type_; } 14270 14271 void 14272 do_determine_type(const Type_context*) 14273 { go_unreachable(); } 14274 14275 Expression* 14276 do_copy() 14277 { 14278 return new Slice_value_expression(this->type_, this->valptr_->copy(), 14279 this->len_->copy(), this->cap_->copy(), 14280 this->location()); 14281 } 14282 14283 Bexpression* 14284 do_get_backend(Translate_context* context); 14285 14286 void 14287 do_dump_expression(Ast_dump_context*) const; 14288 14289 private: 14290 // The type of the slice value. 14291 Type* type_; 14292 // The pointer to the values in the slice. 14293 Expression* valptr_; 14294 // The length of the slice. 14295 Expression* len_; 14296 // The capacity of the slice. 14297 Expression* cap_; 14298}; 14299 14300int 14301Slice_value_expression::do_traverse(Traverse* traverse) 14302{ 14303 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT 14304 || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT 14305 || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT 14306 || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) 14307 return TRAVERSE_EXIT; 14308 return TRAVERSE_CONTINUE; 14309} 14310 14311Bexpression* 14312Slice_value_expression::do_get_backend(Translate_context* context) 14313{ 14314 std::vector<Bexpression*> vals(3); 14315 vals[0] = this->valptr_->get_backend(context); 14316 vals[1] = this->len_->get_backend(context); 14317 vals[2] = this->cap_->get_backend(context); 14318 14319 Gogo* gogo = context->gogo(); 14320 Btype* btype = this->type_->get_backend(gogo); 14321 return gogo->backend()->constructor_expression(btype, vals, this->location()); 14322} 14323 14324void 14325Slice_value_expression::do_dump_expression( 14326 Ast_dump_context* ast_dump_context) const 14327{ 14328 ast_dump_context->ostream() << "slicevalue("; 14329 ast_dump_context->ostream() << "values: "; 14330 this->valptr_->dump_expression(ast_dump_context); 14331 ast_dump_context->ostream() << ", length: "; 14332 this->len_->dump_expression(ast_dump_context); 14333 ast_dump_context->ostream() << ", capacity: "; 14334 this->cap_->dump_expression(ast_dump_context); 14335 ast_dump_context->ostream() << ")"; 14336} 14337 14338Expression* 14339Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, 14340 Expression* cap, Location location) 14341{ 14342 go_assert(at->is_slice_type()); 14343 return new Slice_value_expression(at, valptr, len, cap, location); 14344} 14345 14346// An expression that evaluates to some characteristic of a non-empty interface. 14347// This is used to access the method table or underlying object of an interface. 14348 14349class Interface_info_expression : public Expression 14350{ 14351 public: 14352 Interface_info_expression(Expression* iface, Interface_info iface_info, 14353 Location location) 14354 : Expression(EXPRESSION_INTERFACE_INFO, location), 14355 iface_(iface), iface_info_(iface_info) 14356 { } 14357 14358 protected: 14359 Type* 14360 do_type(); 14361 14362 void 14363 do_determine_type(const Type_context*) 14364 { } 14365 14366 Expression* 14367 do_copy() 14368 { 14369 return new Interface_info_expression(this->iface_->copy(), 14370 this->iface_info_, this->location()); 14371 } 14372 14373 Bexpression* 14374 do_get_backend(Translate_context* context); 14375 14376 void 14377 do_dump_expression(Ast_dump_context*) const; 14378 14379 void 14380 do_issue_nil_check() 14381 { this->iface_->issue_nil_check(); } 14382 14383 private: 14384 // The interface for which we are getting information. 14385 Expression* iface_; 14386 // What information we want. 14387 Interface_info iface_info_; 14388}; 14389 14390// Return the type of the interface info. 14391 14392Type* 14393Interface_info_expression::do_type() 14394{ 14395 switch (this->iface_info_) 14396 { 14397 case INTERFACE_INFO_METHODS: 14398 { 14399 Type* pdt = Type::make_type_descriptor_ptr_type(); 14400 if (this->iface_->type()->interface_type()->is_empty()) 14401 return pdt; 14402 14403 Location loc = this->location(); 14404 Struct_field_list* sfl = new Struct_field_list(); 14405 sfl->push_back( 14406 Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); 14407 14408 Interface_type* itype = this->iface_->type()->interface_type(); 14409 for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); 14410 p != itype->methods()->end(); 14411 ++p) 14412 { 14413 Function_type* ft = p->type()->function_type(); 14414 go_assert(ft->receiver() == NULL); 14415 14416 const Typed_identifier_list* params = ft->parameters(); 14417 Typed_identifier_list* mparams = new Typed_identifier_list(); 14418 if (params != NULL) 14419 mparams->reserve(params->size() + 1); 14420 Type* vt = Type::make_pointer_type(Type::make_void_type()); 14421 mparams->push_back(Typed_identifier("", vt, ft->location())); 14422 if (params != NULL) 14423 { 14424 for (Typed_identifier_list::const_iterator pp = params->begin(); 14425 pp != params->end(); 14426 ++pp) 14427 mparams->push_back(*pp); 14428 } 14429 14430 Typed_identifier_list* mresults = (ft->results() == NULL 14431 ? NULL 14432 : ft->results()->copy()); 14433 Backend_function_type* mft = 14434 Type::make_backend_function_type(NULL, mparams, mresults, 14435 ft->location()); 14436 14437 std::string fname = Gogo::unpack_hidden_name(p->name()); 14438 sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); 14439 } 14440 14441 return Type::make_pointer_type(Type::make_struct_type(sfl, loc)); 14442 } 14443 case INTERFACE_INFO_OBJECT: 14444 return Type::make_pointer_type(Type::make_void_type()); 14445 default: 14446 go_unreachable(); 14447 } 14448} 14449 14450// Return the backend representation for interface information. 14451 14452Bexpression* 14453Interface_info_expression::do_get_backend(Translate_context* context) 14454{ 14455 Gogo* gogo = context->gogo(); 14456 Bexpression* biface = this->iface_->get_backend(context); 14457 switch (this->iface_info_) 14458 { 14459 case INTERFACE_INFO_METHODS: 14460 case INTERFACE_INFO_OBJECT: 14461 return gogo->backend()->struct_field_expression(biface, this->iface_info_, 14462 this->location()); 14463 break; 14464 default: 14465 go_unreachable(); 14466 } 14467} 14468 14469// Dump ast representation for an interface info expression. 14470 14471void 14472Interface_info_expression::do_dump_expression( 14473 Ast_dump_context* ast_dump_context) const 14474{ 14475 bool is_empty = this->iface_->type()->interface_type()->is_empty(); 14476 ast_dump_context->ostream() << "interfaceinfo("; 14477 this->iface_->dump_expression(ast_dump_context); 14478 ast_dump_context->ostream() << ","; 14479 ast_dump_context->ostream() << 14480 (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" 14481 : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" 14482 : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" 14483 : "unknown"); 14484 ast_dump_context->ostream() << ")"; 14485} 14486 14487// Make an interface info expression. 14488 14489Expression* 14490Expression::make_interface_info(Expression* iface, Interface_info iface_info, 14491 Location location) 14492{ 14493 return new Interface_info_expression(iface, iface_info, location); 14494} 14495 14496// An expression that represents an interface value. The first field is either 14497// a type descriptor for an empty interface or a pointer to the interface method 14498// table for a non-empty interface. The second field is always the object. 14499 14500class Interface_value_expression : public Expression 14501{ 14502 public: 14503 Interface_value_expression(Type* type, Expression* first_field, 14504 Expression* obj, Location location) 14505 : Expression(EXPRESSION_INTERFACE_VALUE, location), 14506 type_(type), first_field_(first_field), obj_(obj) 14507 { } 14508 14509 protected: 14510 int 14511 do_traverse(Traverse*); 14512 14513 Type* 14514 do_type() 14515 { return this->type_; } 14516 14517 void 14518 do_determine_type(const Type_context*) 14519 { go_unreachable(); } 14520 14521 Expression* 14522 do_copy() 14523 { 14524 return new Interface_value_expression(this->type_, 14525 this->first_field_->copy(), 14526 this->obj_->copy(), this->location()); 14527 } 14528 14529 Bexpression* 14530 do_get_backend(Translate_context* context); 14531 14532 void 14533 do_dump_expression(Ast_dump_context*) const; 14534 14535 private: 14536 // The type of the interface value. 14537 Type* type_; 14538 // The first field of the interface (either a type descriptor or a pointer 14539 // to the method table. 14540 Expression* first_field_; 14541 // The underlying object of the interface. 14542 Expression* obj_; 14543}; 14544 14545int 14546Interface_value_expression::do_traverse(Traverse* traverse) 14547{ 14548 if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT 14549 || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) 14550 return TRAVERSE_EXIT; 14551 return TRAVERSE_CONTINUE; 14552} 14553 14554Bexpression* 14555Interface_value_expression::do_get_backend(Translate_context* context) 14556{ 14557 std::vector<Bexpression*> vals(2); 14558 vals[0] = this->first_field_->get_backend(context); 14559 vals[1] = this->obj_->get_backend(context); 14560 14561 Gogo* gogo = context->gogo(); 14562 Btype* btype = this->type_->get_backend(gogo); 14563 return gogo->backend()->constructor_expression(btype, vals, this->location()); 14564} 14565 14566void 14567Interface_value_expression::do_dump_expression( 14568 Ast_dump_context* ast_dump_context) const 14569{ 14570 ast_dump_context->ostream() << "interfacevalue("; 14571 ast_dump_context->ostream() << 14572 (this->type_->interface_type()->is_empty() 14573 ? "type_descriptor: " 14574 : "methods: "); 14575 this->first_field_->dump_expression(ast_dump_context); 14576 ast_dump_context->ostream() << ", object: "; 14577 this->obj_->dump_expression(ast_dump_context); 14578 ast_dump_context->ostream() << ")"; 14579} 14580 14581Expression* 14582Expression::make_interface_value(Type* type, Expression* first_value, 14583 Expression* object, Location location) 14584{ 14585 return new Interface_value_expression(type, first_value, object, location); 14586} 14587 14588// An interface method table for a pair of types: an interface type and a type 14589// that implements that interface. 14590 14591class Interface_mtable_expression : public Expression 14592{ 14593 public: 14594 Interface_mtable_expression(Interface_type* itype, Type* type, 14595 bool is_pointer, Location location) 14596 : Expression(EXPRESSION_INTERFACE_MTABLE, location), 14597 itype_(itype), type_(type), is_pointer_(is_pointer), 14598 method_table_type_(NULL), bvar_(NULL) 14599 { } 14600 14601 protected: 14602 int 14603 do_traverse(Traverse*); 14604 14605 Type* 14606 do_type(); 14607 14608 bool 14609 is_immutable() const 14610 { return true; } 14611 14612 void 14613 do_determine_type(const Type_context*) 14614 { go_unreachable(); } 14615 14616 Expression* 14617 do_copy() 14618 { 14619 return new Interface_mtable_expression(this->itype_, this->type_, 14620 this->is_pointer_, this->location()); 14621 } 14622 14623 bool 14624 do_is_addressable() const 14625 { return true; } 14626 14627 Bexpression* 14628 do_get_backend(Translate_context* context); 14629 14630 void 14631 do_dump_expression(Ast_dump_context*) const; 14632 14633 private: 14634 // The interface type for which the methods are defined. 14635 Interface_type* itype_; 14636 // The type to construct the interface method table for. 14637 Type* type_; 14638 // Whether this table contains the method set for the receiver type or the 14639 // pointer receiver type. 14640 bool is_pointer_; 14641 // The type of the method table. 14642 Type* method_table_type_; 14643 // The backend variable that refers to the interface method table. 14644 Bvariable* bvar_; 14645}; 14646 14647int 14648Interface_mtable_expression::do_traverse(Traverse* traverse) 14649{ 14650 if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT 14651 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 14652 return TRAVERSE_EXIT; 14653 return TRAVERSE_CONTINUE; 14654} 14655 14656Type* 14657Interface_mtable_expression::do_type() 14658{ 14659 if (this->method_table_type_ != NULL) 14660 return this->method_table_type_; 14661 14662 const Typed_identifier_list* interface_methods = this->itype_->methods(); 14663 go_assert(!interface_methods->empty()); 14664 14665 Struct_field_list* sfl = new Struct_field_list; 14666 Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), 14667 this->location()); 14668 sfl->push_back(Struct_field(tid)); 14669 for (Typed_identifier_list::const_iterator p = interface_methods->begin(); 14670 p != interface_methods->end(); 14671 ++p) 14672 sfl->push_back(Struct_field(*p)); 14673 this->method_table_type_ = Type::make_struct_type(sfl, this->location()); 14674 return this->method_table_type_; 14675} 14676 14677Bexpression* 14678Interface_mtable_expression::do_get_backend(Translate_context* context) 14679{ 14680 Gogo* gogo = context->gogo(); 14681 Location loc = Linemap::predeclared_location(); 14682 if (this->bvar_ != NULL) 14683 return gogo->backend()->var_expression(this->bvar_, this->location()); 14684 14685 const Typed_identifier_list* interface_methods = this->itype_->methods(); 14686 go_assert(!interface_methods->empty()); 14687 14688 std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_") 14689 + this->itype_->mangled_name(gogo) 14690 + "__" 14691 + this->type_->mangled_name(gogo)); 14692 14693 // See whether this interface has any hidden methods. 14694 bool has_hidden_methods = false; 14695 for (Typed_identifier_list::const_iterator p = interface_methods->begin(); 14696 p != interface_methods->end(); 14697 ++p) 14698 { 14699 if (Gogo::is_hidden_name(p->name())) 14700 { 14701 has_hidden_methods = true; 14702 break; 14703 } 14704 } 14705 14706 // We already know that the named type is convertible to the 14707 // interface. If the interface has hidden methods, and the named 14708 // type is defined in a different package, then the interface 14709 // conversion table will be defined by that other package. 14710 if (has_hidden_methods 14711 && this->type_->named_type() != NULL 14712 && this->type_->named_type()->named_object()->package() != NULL) 14713 { 14714 Btype* btype = this->type()->get_backend(gogo); 14715 this->bvar_ = 14716 gogo->backend()->immutable_struct_reference(mangled_name, btype, loc); 14717 return gogo->backend()->var_expression(this->bvar_, this->location()); 14718 } 14719 14720 // The first element is the type descriptor. 14721 Type* td_type; 14722 if (!this->is_pointer_) 14723 td_type = this->type_; 14724 else 14725 td_type = Type::make_pointer_type(this->type_); 14726 14727 // Build an interface method table for a type: a type descriptor followed by a 14728 // list of function pointers, one for each interface method. This is used for 14729 // interfaces. 14730 Expression_list* svals = new Expression_list(); 14731 svals->push_back(Expression::make_type_descriptor(td_type, loc)); 14732 14733 Named_type* nt = this->type_->named_type(); 14734 Struct_type* st = this->type_->struct_type(); 14735 go_assert(nt != NULL || st != NULL); 14736 14737 for (Typed_identifier_list::const_iterator p = interface_methods->begin(); 14738 p != interface_methods->end(); 14739 ++p) 14740 { 14741 bool is_ambiguous; 14742 Method* m; 14743 if (nt != NULL) 14744 m = nt->method_function(p->name(), &is_ambiguous); 14745 else 14746 m = st->method_function(p->name(), &is_ambiguous); 14747 go_assert(m != NULL); 14748 Named_object* no = m->named_object(); 14749 14750 go_assert(no->is_function() || no->is_function_declaration()); 14751 svals->push_back(Expression::make_func_code_reference(no, loc)); 14752 } 14753 14754 Btype* btype = this->type()->get_backend(gogo); 14755 Expression* mtable = Expression::make_struct_composite_literal(this->type(), 14756 svals, loc); 14757 Bexpression* ctor = mtable->get_backend(context); 14758 14759 bool is_public = has_hidden_methods && this->type_->named_type() != NULL; 14760 this->bvar_ = gogo->backend()->immutable_struct(mangled_name, false, 14761 !is_public, btype, loc); 14762 gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, 14763 !is_public, btype, loc, ctor); 14764 return gogo->backend()->var_expression(this->bvar_, loc); 14765} 14766 14767void 14768Interface_mtable_expression::do_dump_expression( 14769 Ast_dump_context* ast_dump_context) const 14770{ 14771 ast_dump_context->ostream() << "__go_" 14772 << (this->is_pointer_ ? "pimt__" : "imt_"); 14773 ast_dump_context->dump_type(this->itype_); 14774 ast_dump_context->ostream() << "__"; 14775 ast_dump_context->dump_type(this->type_); 14776} 14777 14778Expression* 14779Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, 14780 bool is_pointer, Location location) 14781{ 14782 return new Interface_mtable_expression(itype, type, is_pointer, location); 14783} 14784 14785// An expression which evaluates to the offset of a field within a 14786// struct. This, like Type_info_expression, q.v., is only used to 14787// initialize fields of a type descriptor. 14788 14789class Struct_field_offset_expression : public Expression 14790{ 14791 public: 14792 Struct_field_offset_expression(Struct_type* type, const Struct_field* field) 14793 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, 14794 Linemap::predeclared_location()), 14795 type_(type), field_(field) 14796 { } 14797 14798 protected: 14799 bool 14800 do_is_immutable() const 14801 { return true; } 14802 14803 Type* 14804 do_type() 14805 { return Type::lookup_integer_type("uintptr"); } 14806 14807 void 14808 do_determine_type(const Type_context*) 14809 { } 14810 14811 Expression* 14812 do_copy() 14813 { return this; } 14814 14815 Bexpression* 14816 do_get_backend(Translate_context* context); 14817 14818 void 14819 do_dump_expression(Ast_dump_context*) const; 14820 14821 private: 14822 // The type of the struct. 14823 Struct_type* type_; 14824 // The field. 14825 const Struct_field* field_; 14826}; 14827 14828// Return the backend representation for a struct field offset. 14829 14830Bexpression* 14831Struct_field_offset_expression::do_get_backend(Translate_context* context) 14832{ 14833 const Struct_field_list* fields = this->type_->fields(); 14834 Struct_field_list::const_iterator p; 14835 unsigned i = 0; 14836 for (p = fields->begin(); 14837 p != fields->end(); 14838 ++p, ++i) 14839 if (&*p == this->field_) 14840 break; 14841 go_assert(&*p == this->field_); 14842 14843 Gogo* gogo = context->gogo(); 14844 Btype* btype = this->type_->get_backend(gogo); 14845 14846 int64_t offset = gogo->backend()->type_field_offset(btype, i); 14847 Type* uptr_type = Type::lookup_integer_type("uintptr"); 14848 Expression* ret = 14849 Expression::make_integer_int64(offset, uptr_type, 14850 Linemap::predeclared_location()); 14851 return ret->get_backend(context); 14852} 14853 14854// Dump ast representation for a struct field offset expression. 14855 14856void 14857Struct_field_offset_expression::do_dump_expression( 14858 Ast_dump_context* ast_dump_context) const 14859{ 14860 ast_dump_context->ostream() << "unsafe.Offsetof("; 14861 ast_dump_context->dump_type(this->type_); 14862 ast_dump_context->ostream() << '.'; 14863 ast_dump_context->ostream() << 14864 Gogo::message_name(this->field_->field_name()); 14865 ast_dump_context->ostream() << ")"; 14866} 14867 14868// Make an expression for a struct field offset. 14869 14870Expression* 14871Expression::make_struct_field_offset(Struct_type* type, 14872 const Struct_field* field) 14873{ 14874 return new Struct_field_offset_expression(type, field); 14875} 14876 14877// An expression which evaluates to a pointer to the map descriptor of 14878// a map type. 14879 14880class Map_descriptor_expression : public Expression 14881{ 14882 public: 14883 Map_descriptor_expression(Map_type* type, Location location) 14884 : Expression(EXPRESSION_MAP_DESCRIPTOR, location), 14885 type_(type) 14886 { } 14887 14888 protected: 14889 Type* 14890 do_type() 14891 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } 14892 14893 void 14894 do_determine_type(const Type_context*) 14895 { } 14896 14897 Expression* 14898 do_copy() 14899 { return this; } 14900 14901 Bexpression* 14902 do_get_backend(Translate_context* context) 14903 { 14904 return this->type_->map_descriptor_pointer(context->gogo(), 14905 this->location()); 14906 } 14907 14908 void 14909 do_dump_expression(Ast_dump_context*) const; 14910 14911 private: 14912 // The type for which this is the descriptor. 14913 Map_type* type_; 14914}; 14915 14916// Dump ast representation for a map descriptor expression. 14917 14918void 14919Map_descriptor_expression::do_dump_expression( 14920 Ast_dump_context* ast_dump_context) const 14921{ 14922 ast_dump_context->ostream() << "map_descriptor("; 14923 ast_dump_context->dump_type(this->type_); 14924 ast_dump_context->ostream() << ")"; 14925} 14926 14927// Make a map descriptor expression. 14928 14929Expression* 14930Expression::make_map_descriptor(Map_type* type, Location location) 14931{ 14932 return new Map_descriptor_expression(type, location); 14933} 14934 14935// An expression which evaluates to the address of an unnamed label. 14936 14937class Label_addr_expression : public Expression 14938{ 14939 public: 14940 Label_addr_expression(Label* label, Location location) 14941 : Expression(EXPRESSION_LABEL_ADDR, location), 14942 label_(label) 14943 { } 14944 14945 protected: 14946 Type* 14947 do_type() 14948 { return Type::make_pointer_type(Type::make_void_type()); } 14949 14950 void 14951 do_determine_type(const Type_context*) 14952 { } 14953 14954 Expression* 14955 do_copy() 14956 { return new Label_addr_expression(this->label_, this->location()); } 14957 14958 Bexpression* 14959 do_get_backend(Translate_context* context) 14960 { return this->label_->get_addr(context, this->location()); } 14961 14962 void 14963 do_dump_expression(Ast_dump_context* ast_dump_context) const 14964 { ast_dump_context->ostream() << this->label_->name(); } 14965 14966 private: 14967 // The label whose address we are taking. 14968 Label* label_; 14969}; 14970 14971// Make an expression for the address of an unnamed label. 14972 14973Expression* 14974Expression::make_label_addr(Label* label, Location location) 14975{ 14976 return new Label_addr_expression(label, location); 14977} 14978 14979// Conditional expressions. 14980 14981class Conditional_expression : public Expression 14982{ 14983 public: 14984 Conditional_expression(Expression* cond, Expression* then_expr, 14985 Expression* else_expr, Location location) 14986 : Expression(EXPRESSION_CONDITIONAL, location), 14987 cond_(cond), then_(then_expr), else_(else_expr) 14988 {} 14989 14990 protected: 14991 int 14992 do_traverse(Traverse*); 14993 14994 Type* 14995 do_type(); 14996 14997 void 14998 do_determine_type(const Type_context*); 14999 15000 Expression* 15001 do_copy() 15002 { 15003 return new Conditional_expression(this->cond_->copy(), this->then_->copy(), 15004 this->else_->copy(), this->location()); 15005 } 15006 15007 Bexpression* 15008 do_get_backend(Translate_context* context); 15009 15010 void 15011 do_dump_expression(Ast_dump_context*) const; 15012 15013 private: 15014 // The condition to be checked. 15015 Expression* cond_; 15016 // The expression to execute if the condition is true. 15017 Expression* then_; 15018 // The expression to execute if the condition is false. 15019 Expression* else_; 15020}; 15021 15022// Traversal. 15023 15024int 15025Conditional_expression::do_traverse(Traverse* traverse) 15026{ 15027 if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT 15028 || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT 15029 || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) 15030 return TRAVERSE_EXIT; 15031 return TRAVERSE_CONTINUE; 15032} 15033 15034// Return the type of the conditional expression. 15035 15036Type* 15037Conditional_expression::do_type() 15038{ 15039 Type* result_type = Type::make_void_type(); 15040 if (Type::are_identical(this->then_->type(), this->else_->type(), false, 15041 NULL)) 15042 result_type = this->then_->type(); 15043 else if (this->then_->is_nil_expression() 15044 || this->else_->is_nil_expression()) 15045 result_type = (!this->then_->is_nil_expression() 15046 ? this->then_->type() 15047 : this->else_->type()); 15048 return result_type; 15049} 15050 15051// Determine type for a conditional expression. 15052 15053void 15054Conditional_expression::do_determine_type(const Type_context* context) 15055{ 15056 this->cond_->determine_type_no_context(); 15057 this->then_->determine_type(context); 15058 this->else_->determine_type(context); 15059} 15060 15061// Get the backend representation of a conditional expression. 15062 15063Bexpression* 15064Conditional_expression::do_get_backend(Translate_context* context) 15065{ 15066 Gogo* gogo = context->gogo(); 15067 Btype* result_btype = this->type()->get_backend(gogo); 15068 Bexpression* cond = this->cond_->get_backend(context); 15069 Bexpression* then = this->then_->get_backend(context); 15070 Bexpression* belse = this->else_->get_backend(context); 15071 return gogo->backend()->conditional_expression(result_btype, cond, then, 15072 belse, this->location()); 15073} 15074 15075// Dump ast representation of a conditional expression. 15076 15077void 15078Conditional_expression::do_dump_expression( 15079 Ast_dump_context* ast_dump_context) const 15080{ 15081 ast_dump_context->ostream() << "("; 15082 ast_dump_context->dump_expression(this->cond_); 15083 ast_dump_context->ostream() << " ? "; 15084 ast_dump_context->dump_expression(this->then_); 15085 ast_dump_context->ostream() << " : "; 15086 ast_dump_context->dump_expression(this->else_); 15087 ast_dump_context->ostream() << ") "; 15088} 15089 15090// Make a conditional expression. 15091 15092Expression* 15093Expression::make_conditional(Expression* cond, Expression* then, 15094 Expression* else_expr, Location location) 15095{ 15096 return new Conditional_expression(cond, then, else_expr, location); 15097} 15098 15099// Compound expressions. 15100 15101class Compound_expression : public Expression 15102{ 15103 public: 15104 Compound_expression(Expression* init, Expression* expr, Location location) 15105 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr) 15106 {} 15107 15108 protected: 15109 int 15110 do_traverse(Traverse*); 15111 15112 Type* 15113 do_type(); 15114 15115 void 15116 do_determine_type(const Type_context*); 15117 15118 Expression* 15119 do_copy() 15120 { 15121 return new Compound_expression(this->init_->copy(), this->expr_->copy(), 15122 this->location()); 15123 } 15124 15125 Bexpression* 15126 do_get_backend(Translate_context* context); 15127 15128 void 15129 do_dump_expression(Ast_dump_context*) const; 15130 15131 private: 15132 // The expression that is evaluated first and discarded. 15133 Expression* init_; 15134 // The expression that is evaluated and returned. 15135 Expression* expr_; 15136}; 15137 15138// Traversal. 15139 15140int 15141Compound_expression::do_traverse(Traverse* traverse) 15142{ 15143 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT 15144 || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) 15145 return TRAVERSE_EXIT; 15146 return TRAVERSE_CONTINUE; 15147} 15148 15149// Return the type of the compound expression. 15150 15151Type* 15152Compound_expression::do_type() 15153{ 15154 return this->expr_->type(); 15155} 15156 15157// Determine type for a compound expression. 15158 15159void 15160Compound_expression::do_determine_type(const Type_context* context) 15161{ 15162 this->init_->determine_type_no_context(); 15163 this->expr_->determine_type(context); 15164} 15165 15166// Get the backend representation of a compound expression. 15167 15168Bexpression* 15169Compound_expression::do_get_backend(Translate_context* context) 15170{ 15171 Gogo* gogo = context->gogo(); 15172 Bexpression* binit = this->init_->get_backend(context); 15173 Bstatement* init_stmt = gogo->backend()->expression_statement(binit); 15174 Bexpression* bexpr = this->expr_->get_backend(context); 15175 return gogo->backend()->compound_expression(init_stmt, bexpr, 15176 this->location()); 15177} 15178 15179// Dump ast representation of a conditional expression. 15180 15181void 15182Compound_expression::do_dump_expression( 15183 Ast_dump_context* ast_dump_context) const 15184{ 15185 ast_dump_context->ostream() << "("; 15186 ast_dump_context->dump_expression(this->init_); 15187 ast_dump_context->ostream() << ","; 15188 ast_dump_context->dump_expression(this->expr_); 15189 ast_dump_context->ostream() << ") "; 15190} 15191 15192// Make a compound expression. 15193 15194Expression* 15195Expression::make_compound(Expression* init, Expression* expr, Location location) 15196{ 15197 return new Compound_expression(init, expr, location); 15198} 15199 15200// Import an expression. This comes at the end in order to see the 15201// various class definitions. 15202 15203Expression* 15204Expression::import_expression(Import* imp) 15205{ 15206 int c = imp->peek_char(); 15207 if (imp->match_c_string("- ") 15208 || imp->match_c_string("! ") 15209 || imp->match_c_string("^ ")) 15210 return Unary_expression::do_import(imp); 15211 else if (c == '(') 15212 return Binary_expression::do_import(imp); 15213 else if (imp->match_c_string("true") 15214 || imp->match_c_string("false")) 15215 return Boolean_expression::do_import(imp); 15216 else if (c == '"') 15217 return String_expression::do_import(imp); 15218 else if (c == '-' || (c >= '0' && c <= '9')) 15219 { 15220 // This handles integers, floats and complex constants. 15221 return Integer_expression::do_import(imp); 15222 } 15223 else if (imp->match_c_string("nil")) 15224 return Nil_expression::do_import(imp); 15225 else if (imp->match_c_string("convert")) 15226 return Type_conversion_expression::do_import(imp); 15227 else 15228 { 15229 error_at(imp->location(), "import error: expected expression"); 15230 return Expression::make_error(imp->location()); 15231 } 15232} 15233 15234// Class Expression_list. 15235 15236// Traverse the list. 15237 15238int 15239Expression_list::traverse(Traverse* traverse) 15240{ 15241 for (Expression_list::iterator p = this->begin(); 15242 p != this->end(); 15243 ++p) 15244 { 15245 if (*p != NULL) 15246 { 15247 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) 15248 return TRAVERSE_EXIT; 15249 } 15250 } 15251 return TRAVERSE_CONTINUE; 15252} 15253 15254// Copy the list. 15255 15256Expression_list* 15257Expression_list::copy() 15258{ 15259 Expression_list* ret = new Expression_list(); 15260 for (Expression_list::iterator p = this->begin(); 15261 p != this->end(); 15262 ++p) 15263 { 15264 if (*p == NULL) 15265 ret->push_back(NULL); 15266 else 15267 ret->push_back((*p)->copy()); 15268 } 15269 return ret; 15270} 15271 15272// Return whether an expression list has an error expression. 15273 15274bool 15275Expression_list::contains_error() const 15276{ 15277 for (Expression_list::const_iterator p = this->begin(); 15278 p != this->end(); 15279 ++p) 15280 if (*p != NULL && (*p)->is_error_expression()) 15281 return true; 15282 return false; 15283} 15284 15285// Class Numeric_constant. 15286 15287// Destructor. 15288 15289Numeric_constant::~Numeric_constant() 15290{ 15291 this->clear(); 15292} 15293 15294// Copy constructor. 15295 15296Numeric_constant::Numeric_constant(const Numeric_constant& a) 15297 : classification_(a.classification_), type_(a.type_) 15298{ 15299 switch (a.classification_) 15300 { 15301 case NC_INVALID: 15302 break; 15303 case NC_INT: 15304 case NC_RUNE: 15305 mpz_init_set(this->u_.int_val, a.u_.int_val); 15306 break; 15307 case NC_FLOAT: 15308 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); 15309 break; 15310 case NC_COMPLEX: 15311 mpc_init2(this->u_.complex_val, mpc_precision); 15312 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); 15313 break; 15314 default: 15315 go_unreachable(); 15316 } 15317} 15318 15319// Assignment operator. 15320 15321Numeric_constant& 15322Numeric_constant::operator=(const Numeric_constant& a) 15323{ 15324 this->clear(); 15325 this->classification_ = a.classification_; 15326 this->type_ = a.type_; 15327 switch (a.classification_) 15328 { 15329 case NC_INVALID: 15330 break; 15331 case NC_INT: 15332 case NC_RUNE: 15333 mpz_init_set(this->u_.int_val, a.u_.int_val); 15334 break; 15335 case NC_FLOAT: 15336 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); 15337 break; 15338 case NC_COMPLEX: 15339 mpc_init2(this->u_.complex_val, mpc_precision); 15340 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); 15341 break; 15342 default: 15343 go_unreachable(); 15344 } 15345 return *this; 15346} 15347 15348// Clear the contents. 15349 15350void 15351Numeric_constant::clear() 15352{ 15353 switch (this->classification_) 15354 { 15355 case NC_INVALID: 15356 break; 15357 case NC_INT: 15358 case NC_RUNE: 15359 mpz_clear(this->u_.int_val); 15360 break; 15361 case NC_FLOAT: 15362 mpfr_clear(this->u_.float_val); 15363 break; 15364 case NC_COMPLEX: 15365 mpc_clear(this->u_.complex_val); 15366 break; 15367 default: 15368 go_unreachable(); 15369 } 15370 this->classification_ = NC_INVALID; 15371} 15372 15373// Set to an unsigned long value. 15374 15375void 15376Numeric_constant::set_unsigned_long(Type* type, unsigned long val) 15377{ 15378 this->clear(); 15379 this->classification_ = NC_INT; 15380 this->type_ = type; 15381 mpz_init_set_ui(this->u_.int_val, val); 15382} 15383 15384// Set to an integer value. 15385 15386void 15387Numeric_constant::set_int(Type* type, const mpz_t val) 15388{ 15389 this->clear(); 15390 this->classification_ = NC_INT; 15391 this->type_ = type; 15392 mpz_init_set(this->u_.int_val, val); 15393} 15394 15395// Set to a rune value. 15396 15397void 15398Numeric_constant::set_rune(Type* type, const mpz_t val) 15399{ 15400 this->clear(); 15401 this->classification_ = NC_RUNE; 15402 this->type_ = type; 15403 mpz_init_set(this->u_.int_val, val); 15404} 15405 15406// Set to a floating point value. 15407 15408void 15409Numeric_constant::set_float(Type* type, const mpfr_t val) 15410{ 15411 this->clear(); 15412 this->classification_ = NC_FLOAT; 15413 this->type_ = type; 15414 // Numeric constants do not have negative zero values, so remove 15415 // them here. They also don't have infinity or NaN values, but we 15416 // should never see them here. 15417 if (mpfr_zero_p(val)) 15418 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); 15419 else 15420 mpfr_init_set(this->u_.float_val, val, GMP_RNDN); 15421} 15422 15423// Set to a complex value. 15424 15425void 15426Numeric_constant::set_complex(Type* type, const mpc_t val) 15427{ 15428 this->clear(); 15429 this->classification_ = NC_COMPLEX; 15430 this->type_ = type; 15431 mpc_init2(this->u_.complex_val, mpc_precision); 15432 mpc_set(this->u_.complex_val, val, MPC_RNDNN); 15433} 15434 15435// Get an int value. 15436 15437void 15438Numeric_constant::get_int(mpz_t* val) const 15439{ 15440 go_assert(this->is_int()); 15441 mpz_init_set(*val, this->u_.int_val); 15442} 15443 15444// Get a rune value. 15445 15446void 15447Numeric_constant::get_rune(mpz_t* val) const 15448{ 15449 go_assert(this->is_rune()); 15450 mpz_init_set(*val, this->u_.int_val); 15451} 15452 15453// Get a floating point value. 15454 15455void 15456Numeric_constant::get_float(mpfr_t* val) const 15457{ 15458 go_assert(this->is_float()); 15459 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); 15460} 15461 15462// Get a complex value. 15463 15464void 15465Numeric_constant::get_complex(mpc_t* val) const 15466{ 15467 go_assert(this->is_complex()); 15468 mpc_init2(*val, mpc_precision); 15469 mpc_set(*val, this->u_.complex_val, MPC_RNDNN); 15470} 15471 15472// Express value as unsigned long if possible. 15473 15474Numeric_constant::To_unsigned_long 15475Numeric_constant::to_unsigned_long(unsigned long* val) const 15476{ 15477 switch (this->classification_) 15478 { 15479 case NC_INT: 15480 case NC_RUNE: 15481 return this->mpz_to_unsigned_long(this->u_.int_val, val); 15482 case NC_FLOAT: 15483 return this->mpfr_to_unsigned_long(this->u_.float_val, val); 15484 case NC_COMPLEX: 15485 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 15486 return NC_UL_NOTINT; 15487 return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), 15488 val); 15489 default: 15490 go_unreachable(); 15491 } 15492} 15493 15494// Express integer value as unsigned long if possible. 15495 15496Numeric_constant::To_unsigned_long 15497Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, 15498 unsigned long *val) const 15499{ 15500 if (mpz_sgn(ival) < 0) 15501 return NC_UL_NEGATIVE; 15502 unsigned long ui = mpz_get_ui(ival); 15503 if (mpz_cmp_ui(ival, ui) != 0) 15504 return NC_UL_BIG; 15505 *val = ui; 15506 return NC_UL_VALID; 15507} 15508 15509// Express floating point value as unsigned long if possible. 15510 15511Numeric_constant::To_unsigned_long 15512Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, 15513 unsigned long *val) const 15514{ 15515 if (!mpfr_integer_p(fval)) 15516 return NC_UL_NOTINT; 15517 mpz_t ival; 15518 mpz_init(ival); 15519 mpfr_get_z(ival, fval, GMP_RNDN); 15520 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); 15521 mpz_clear(ival); 15522 return ret; 15523} 15524 15525// Convert value to integer if possible. 15526 15527bool 15528Numeric_constant::to_int(mpz_t* val) const 15529{ 15530 switch (this->classification_) 15531 { 15532 case NC_INT: 15533 case NC_RUNE: 15534 mpz_init_set(*val, this->u_.int_val); 15535 return true; 15536 case NC_FLOAT: 15537 if (!mpfr_integer_p(this->u_.float_val)) 15538 return false; 15539 mpz_init(*val); 15540 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); 15541 return true; 15542 case NC_COMPLEX: 15543 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) 15544 || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) 15545 return false; 15546 mpz_init(*val); 15547 mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); 15548 return true; 15549 default: 15550 go_unreachable(); 15551 } 15552} 15553 15554// Convert value to floating point if possible. 15555 15556bool 15557Numeric_constant::to_float(mpfr_t* val) const 15558{ 15559 switch (this->classification_) 15560 { 15561 case NC_INT: 15562 case NC_RUNE: 15563 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); 15564 return true; 15565 case NC_FLOAT: 15566 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); 15567 return true; 15568 case NC_COMPLEX: 15569 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 15570 return false; 15571 mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); 15572 return true; 15573 default: 15574 go_unreachable(); 15575 } 15576} 15577 15578// Convert value to complex. 15579 15580bool 15581Numeric_constant::to_complex(mpc_t* val) const 15582{ 15583 mpc_init2(*val, mpc_precision); 15584 switch (this->classification_) 15585 { 15586 case NC_INT: 15587 case NC_RUNE: 15588 mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); 15589 return true; 15590 case NC_FLOAT: 15591 mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); 15592 return true; 15593 case NC_COMPLEX: 15594 mpc_set(*val, this->u_.complex_val, MPC_RNDNN); 15595 return true; 15596 default: 15597 go_unreachable(); 15598 } 15599} 15600 15601// Get the type. 15602 15603Type* 15604Numeric_constant::type() const 15605{ 15606 if (this->type_ != NULL) 15607 return this->type_; 15608 switch (this->classification_) 15609 { 15610 case NC_INT: 15611 return Type::make_abstract_integer_type(); 15612 case NC_RUNE: 15613 return Type::make_abstract_character_type(); 15614 case NC_FLOAT: 15615 return Type::make_abstract_float_type(); 15616 case NC_COMPLEX: 15617 return Type::make_abstract_complex_type(); 15618 default: 15619 go_unreachable(); 15620 } 15621} 15622 15623// If the constant can be expressed in TYPE, then set the type of the 15624// constant to TYPE and return true. Otherwise return false, and, if 15625// ISSUE_ERROR is true, report an appropriate error message. 15626 15627bool 15628Numeric_constant::set_type(Type* type, bool issue_error, Location loc) 15629{ 15630 bool ret; 15631 if (type == NULL || type->is_error()) 15632 ret = true; 15633 else if (type->integer_type() != NULL) 15634 ret = this->check_int_type(type->integer_type(), issue_error, loc); 15635 else if (type->float_type() != NULL) 15636 ret = this->check_float_type(type->float_type(), issue_error, loc); 15637 else if (type->complex_type() != NULL) 15638 ret = this->check_complex_type(type->complex_type(), issue_error, loc); 15639 else 15640 go_unreachable(); 15641 if (ret) 15642 this->type_ = type; 15643 return ret; 15644} 15645 15646// Check whether the constant can be expressed in an integer type. 15647 15648bool 15649Numeric_constant::check_int_type(Integer_type* type, bool issue_error, 15650 Location location) const 15651{ 15652 mpz_t val; 15653 switch (this->classification_) 15654 { 15655 case NC_INT: 15656 case NC_RUNE: 15657 mpz_init_set(val, this->u_.int_val); 15658 break; 15659 15660 case NC_FLOAT: 15661 if (!mpfr_integer_p(this->u_.float_val)) 15662 { 15663 if (issue_error) 15664 error_at(location, "floating point constant truncated to integer"); 15665 return false; 15666 } 15667 mpz_init(val); 15668 mpfr_get_z(val, this->u_.float_val, GMP_RNDN); 15669 break; 15670 15671 case NC_COMPLEX: 15672 if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) 15673 || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 15674 { 15675 if (issue_error) 15676 error_at(location, "complex constant truncated to integer"); 15677 return false; 15678 } 15679 mpz_init(val); 15680 mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); 15681 break; 15682 15683 default: 15684 go_unreachable(); 15685 } 15686 15687 bool ret; 15688 if (type->is_abstract()) 15689 ret = true; 15690 else 15691 { 15692 int bits = mpz_sizeinbase(val, 2); 15693 if (type->is_unsigned()) 15694 { 15695 // For an unsigned type we can only accept a nonnegative 15696 // number, and we must be able to represents at least BITS. 15697 ret = mpz_sgn(val) >= 0 && bits <= type->bits(); 15698 } 15699 else 15700 { 15701 // For a signed type we need an extra bit to indicate the 15702 // sign. We have to handle the most negative integer 15703 // specially. 15704 ret = (bits + 1 <= type->bits() 15705 || (bits <= type->bits() 15706 && mpz_sgn(val) < 0 15707 && (mpz_scan1(val, 0) 15708 == static_cast<unsigned long>(type->bits() - 1)) 15709 && mpz_scan0(val, type->bits()) == ULONG_MAX)); 15710 } 15711 } 15712 15713 if (!ret && issue_error) 15714 error_at(location, "integer constant overflow"); 15715 15716 return ret; 15717} 15718 15719// Check whether the constant can be expressed in a floating point 15720// type. 15721 15722bool 15723Numeric_constant::check_float_type(Float_type* type, bool issue_error, 15724 Location location) 15725{ 15726 mpfr_t val; 15727 switch (this->classification_) 15728 { 15729 case NC_INT: 15730 case NC_RUNE: 15731 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); 15732 break; 15733 15734 case NC_FLOAT: 15735 mpfr_init_set(val, this->u_.float_val, GMP_RNDN); 15736 break; 15737 15738 case NC_COMPLEX: 15739 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 15740 { 15741 if (issue_error) 15742 error_at(location, "complex constant truncated to float"); 15743 return false; 15744 } 15745 mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); 15746 break; 15747 15748 default: 15749 go_unreachable(); 15750 } 15751 15752 bool ret; 15753 if (type->is_abstract()) 15754 ret = true; 15755 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) 15756 { 15757 // A NaN or Infinity always fits in the range of the type. 15758 ret = true; 15759 } 15760 else 15761 { 15762 mp_exp_t exp = mpfr_get_exp(val); 15763 mp_exp_t max_exp; 15764 switch (type->bits()) 15765 { 15766 case 32: 15767 max_exp = 128; 15768 break; 15769 case 64: 15770 max_exp = 1024; 15771 break; 15772 default: 15773 go_unreachable(); 15774 } 15775 15776 ret = exp <= max_exp; 15777 15778 if (ret) 15779 { 15780 // Round the constant to the desired type. 15781 mpfr_t t; 15782 mpfr_init(t); 15783 switch (type->bits()) 15784 { 15785 case 32: 15786 mpfr_set_prec(t, 24); 15787 break; 15788 case 64: 15789 mpfr_set_prec(t, 53); 15790 break; 15791 default: 15792 go_unreachable(); 15793 } 15794 mpfr_set(t, val, GMP_RNDN); 15795 mpfr_set(val, t, GMP_RNDN); 15796 mpfr_clear(t); 15797 15798 this->set_float(type, val); 15799 } 15800 } 15801 15802 mpfr_clear(val); 15803 15804 if (!ret && issue_error) 15805 error_at(location, "floating point constant overflow"); 15806 15807 return ret; 15808} 15809 15810// Check whether the constant can be expressed in a complex type. 15811 15812bool 15813Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, 15814 Location location) 15815{ 15816 if (type->is_abstract()) 15817 return true; 15818 15819 mp_exp_t max_exp; 15820 switch (type->bits()) 15821 { 15822 case 64: 15823 max_exp = 128; 15824 break; 15825 case 128: 15826 max_exp = 1024; 15827 break; 15828 default: 15829 go_unreachable(); 15830 } 15831 15832 mpc_t val; 15833 mpc_init2(val, mpc_precision); 15834 switch (this->classification_) 15835 { 15836 case NC_INT: 15837 case NC_RUNE: 15838 mpc_set_z(val, this->u_.int_val, MPC_RNDNN); 15839 break; 15840 15841 case NC_FLOAT: 15842 mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); 15843 break; 15844 15845 case NC_COMPLEX: 15846 mpc_set(val, this->u_.complex_val, MPC_RNDNN); 15847 break; 15848 15849 default: 15850 go_unreachable(); 15851 } 15852 15853 bool ret = true; 15854 if (!mpfr_nan_p(mpc_realref(val)) 15855 && !mpfr_inf_p(mpc_realref(val)) 15856 && !mpfr_zero_p(mpc_realref(val)) 15857 && mpfr_get_exp(mpc_realref(val)) > max_exp) 15858 { 15859 if (issue_error) 15860 error_at(location, "complex real part overflow"); 15861 ret = false; 15862 } 15863 15864 if (!mpfr_nan_p(mpc_imagref(val)) 15865 && !mpfr_inf_p(mpc_imagref(val)) 15866 && !mpfr_zero_p(mpc_imagref(val)) 15867 && mpfr_get_exp(mpc_imagref(val)) > max_exp) 15868 { 15869 if (issue_error) 15870 error_at(location, "complex imaginary part overflow"); 15871 ret = false; 15872 } 15873 15874 if (ret) 15875 { 15876 // Round the constant to the desired type. 15877 mpc_t t; 15878 switch (type->bits()) 15879 { 15880 case 64: 15881 mpc_init2(t, 24); 15882 break; 15883 case 128: 15884 mpc_init2(t, 53); 15885 break; 15886 default: 15887 go_unreachable(); 15888 } 15889 mpc_set(t, val, MPC_RNDNN); 15890 mpc_set(val, t, MPC_RNDNN); 15891 mpc_clear(t); 15892 15893 this->set_complex(type, val); 15894 } 15895 15896 mpc_clear(val); 15897 15898 return ret; 15899} 15900 15901// Return an Expression for this value. 15902 15903Expression* 15904Numeric_constant::expression(Location loc) const 15905{ 15906 switch (this->classification_) 15907 { 15908 case NC_INT: 15909 return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); 15910 case NC_RUNE: 15911 return Expression::make_character(&this->u_.int_val, this->type_, loc); 15912 case NC_FLOAT: 15913 return Expression::make_float(&this->u_.float_val, this->type_, loc); 15914 case NC_COMPLEX: 15915 return Expression::make_complex(&this->u_.complex_val, this->type_, loc); 15916 default: 15917 go_unreachable(); 15918 } 15919} 15920