github.com/kidsbmilk/gofronted_all@v0.0.0-20220701224323-6479d5976c5d/go/expressions.cc (about) 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 "go-diagnostics.h" 14 #include "go-encode-id.h" 15 #include "types.h" 16 #include "export.h" 17 #include "import.h" 18 #include "statements.h" 19 #include "lex.h" 20 #include "runtime.h" 21 #include "backend.h" 22 #include "expressions.h" 23 #include "ast-dump.h" 24 25 // Class Expression. 26 27 Expression::Expression(Expression_classification classification, 28 Location location) 29 : classification_(classification), location_(location) 30 { 31 } 32 33 Expression::~Expression() 34 { 35 } 36 37 // Traverse the expressions. 38 39 int 40 Expression::traverse(Expression** pexpr, Traverse* traverse) 41 { 42 Expression* expr = *pexpr; 43 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) 44 { 45 int t = traverse->expression(pexpr); 46 if (t == TRAVERSE_EXIT) 47 return TRAVERSE_EXIT; 48 else if (t == TRAVERSE_SKIP_COMPONENTS) 49 return TRAVERSE_CONTINUE; 50 } 51 return expr->do_traverse(traverse); 52 } 53 54 // Traverse subexpressions of this expression. 55 56 int 57 Expression::traverse_subexpressions(Traverse* traverse) 58 { 59 return this->do_traverse(traverse); 60 } 61 62 // A traversal used to set the location of subexpressions. 63 64 class Set_location : public Traverse 65 { 66 public: 67 Set_location(Location loc) 68 : Traverse(traverse_expressions), 69 loc_(loc) 70 { } 71 72 int 73 expression(Expression** pexpr); 74 75 private: 76 Location loc_; 77 }; 78 79 // Set the location of an expression. 80 81 int 82 Set_location::expression(Expression** pexpr) 83 { 84 // Some expressions are shared or don't have an independent 85 // location, so we shouldn't change their location. This is the set 86 // of expressions for which do_copy is just "return this" or 87 // otherwise does not pass down the location. 88 switch ((*pexpr)->classification()) 89 { 90 case Expression::EXPRESSION_ERROR: 91 case Expression::EXPRESSION_VAR_REFERENCE: 92 case Expression::EXPRESSION_ENCLOSED_VAR_REFERENCE: 93 case Expression::EXPRESSION_STRING: 94 case Expression::EXPRESSION_FUNC_DESCRIPTOR: 95 case Expression::EXPRESSION_TYPE: 96 case Expression::EXPRESSION_BOOLEAN: 97 case Expression::EXPRESSION_CONST_REFERENCE: 98 case Expression::EXPRESSION_NIL: 99 case Expression::EXPRESSION_TYPE_DESCRIPTOR: 100 case Expression::EXPRESSION_GC_SYMBOL: 101 case Expression::EXPRESSION_PTRMASK_SYMBOL: 102 case Expression::EXPRESSION_TYPE_INFO: 103 case Expression::EXPRESSION_STRUCT_FIELD_OFFSET: 104 return TRAVERSE_CONTINUE; 105 default: 106 break; 107 } 108 109 (*pexpr)->location_ = this->loc_; 110 return TRAVERSE_CONTINUE; 111 } 112 113 // Set the location of an expression and its subexpressions. 114 115 void 116 Expression::set_location(Location loc) 117 { 118 this->location_ = loc; 119 Set_location sl(loc); 120 this->traverse_subexpressions(&sl); 121 } 122 123 // Default implementation for do_traverse for child classes. 124 125 int 126 Expression::do_traverse(Traverse*) 127 { 128 return TRAVERSE_CONTINUE; 129 } 130 131 // This virtual function is called by the parser if the value of this 132 // expression is being discarded. By default, we give an error. 133 // Expressions with side effects override. 134 135 bool 136 Expression::do_discarding_value() 137 { 138 this->unused_value_error(); 139 return false; 140 } 141 142 // This virtual function is called to export expressions. This will 143 // only be used by expressions which may be constant. 144 145 void 146 Expression::do_export(Export_function_body*) const 147 { 148 go_unreachable(); 149 } 150 151 // Write a name to the export data. 152 153 void 154 Expression::export_name(Export_function_body* efb, const Named_object* no) 155 { 156 if (no->package() != NULL) 157 { 158 char buf[50]; 159 snprintf(buf, sizeof buf, "<p%d>", efb->package_index(no->package())); 160 efb->write_c_string(buf); 161 } 162 163 if (!Gogo::is_hidden_name(no->name())) 164 efb->write_string(no->name()); 165 else 166 { 167 efb->write_c_string("."); 168 efb->write_string(Gogo::unpack_hidden_name(no->name())); 169 } 170 } 171 172 // Give an error saying that the value of the expression is not used. 173 174 void 175 Expression::unused_value_error() 176 { 177 if (this->type()->is_error()) 178 { 179 go_assert(saw_errors()); 180 this->set_is_error(); 181 } 182 else 183 this->report_error(_("value computed is not used")); 184 } 185 186 // Note that this expression is an error. This is called by children 187 // when they discover an error. 188 189 void 190 Expression::set_is_error() 191 { 192 this->classification_ = EXPRESSION_ERROR; 193 } 194 195 // For children to call to report an error conveniently. 196 197 void 198 Expression::report_error(const char* msg) 199 { 200 go_error_at(this->location_, "%s", msg); 201 this->set_is_error(); 202 } 203 204 // Set types of variables and constants. This is implemented by the 205 // child class. 206 207 void 208 Expression::determine_type(const Type_context* context) 209 { 210 this->do_determine_type(context); 211 } 212 213 // Set types when there is no context. 214 215 void 216 Expression::determine_type_no_context() 217 { 218 Type_context context; 219 this->do_determine_type(&context); 220 } 221 222 // Return true if two expressions refer to the same variable or struct 223 // field. This can only be true when there are no side effects. 224 225 bool 226 Expression::is_same_variable(Expression* a, Expression* b) 227 { 228 if (a->classification() != b->classification()) 229 return false; 230 231 Var_expression* av = a->var_expression(); 232 if (av != NULL) 233 return av->named_object() == b->var_expression()->named_object(); 234 235 Field_reference_expression* af = a->field_reference_expression(); 236 if (af != NULL) 237 { 238 Field_reference_expression* bf = b->field_reference_expression(); 239 return (af->field_index() == bf->field_index() 240 && Expression::is_same_variable(af->expr(), bf->expr())); 241 } 242 243 Unary_expression* au = a->unary_expression(); 244 if (au != NULL) 245 { 246 Unary_expression* bu = b->unary_expression(); 247 return (au->op() == OPERATOR_MULT 248 && bu->op() == OPERATOR_MULT 249 && Expression::is_same_variable(au->operand(), 250 bu->operand())); 251 } 252 253 Array_index_expression* aie = a->array_index_expression(); 254 if (aie != NULL) 255 { 256 Array_index_expression* bie = b->array_index_expression(); 257 return (aie->end() == NULL 258 && bie->end() == NULL 259 && Expression::is_same_variable(aie->array(), bie->array()) 260 && Expression::is_same_variable(aie->start(), bie->start())); 261 } 262 263 Numeric_constant aval; 264 if (a->numeric_constant_value(&aval)) 265 { 266 Numeric_constant bval; 267 if (b->numeric_constant_value(&bval)) 268 return aval.equals(bval); 269 } 270 271 return false; 272 } 273 274 // Return an expression handling any conversions which must be done during 275 // assignment. 276 277 Expression* 278 Expression::convert_for_assignment(Gogo* gogo, Type* lhs_type, 279 Expression* rhs, Location location) 280 { 281 Type* rhs_type = rhs->type(); 282 if (lhs_type->is_error() 283 || rhs_type->is_error() 284 || rhs->is_error_expression()) 285 return Expression::make_error(location); 286 287 bool are_identical = Type::are_identical(lhs_type, rhs_type, 288 (Type::COMPARE_ERRORS 289 | Type::COMPARE_TAGS), 290 NULL); 291 if (!are_identical && lhs_type->interface_type() != NULL) 292 { 293 // Type to interface conversions have been made explicit early. 294 go_assert(rhs_type->interface_type() != NULL); 295 return Expression::convert_interface_to_interface(lhs_type, rhs, false, 296 location); 297 } 298 else if (!are_identical && rhs_type->interface_type() != NULL) 299 return Expression::convert_interface_to_type(gogo, lhs_type, rhs, location); 300 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) 301 { 302 // Assigning nil to a slice. 303 Expression* nil = Expression::make_nil(location); 304 Expression* zero = Expression::make_integer_ul(0, NULL, location); 305 return Expression::make_slice_value(lhs_type, nil, zero, zero, location); 306 } 307 else if (rhs_type->is_nil_type()) 308 return Expression::make_nil(location); 309 else if (are_identical) 310 { 311 if (lhs_type->forwarded() != rhs_type->forwarded()) 312 { 313 // Different but identical types require an explicit 314 // conversion. This happens with type aliases. 315 return Expression::make_cast(lhs_type, rhs, location); 316 } 317 318 // No conversion is needed. 319 return rhs; 320 } 321 else if (lhs_type->points_to() != NULL) 322 return Expression::make_unsafe_cast(lhs_type, rhs, location); 323 else if (lhs_type->is_numeric_type()) 324 return Expression::make_cast(lhs_type, rhs, location); 325 else if ((lhs_type->struct_type() != NULL 326 && rhs_type->struct_type() != NULL) 327 || (lhs_type->array_type() != NULL 328 && rhs_type->array_type() != NULL)) 329 { 330 // This conversion must be permitted by Go, or we wouldn't have 331 // gotten here. 332 return Expression::make_unsafe_cast(lhs_type, rhs, location); 333 } 334 else 335 return rhs; 336 } 337 338 // Return an expression for a conversion from a non-interface type to an 339 // interface type. If ON_STACK is true, it can allocate the storage on 340 // stack. 341 342 Expression* 343 Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, 344 bool on_stack, Location location) 345 { 346 Interface_type* lhs_interface_type = lhs_type->interface_type(); 347 bool lhs_is_empty = lhs_interface_type->is_empty(); 348 349 // Since RHS_TYPE is a static type, we can create the interface 350 // method table at compile time. 351 352 // When setting an interface to nil, we just set both fields to 353 // NULL. 354 Type* rhs_type = rhs->type(); 355 if (rhs_type->is_nil_type()) 356 { 357 Expression* nil = Expression::make_nil(location); 358 return Expression::make_interface_value(lhs_type, nil, nil, location); 359 } 360 361 // This should have been checked already. 362 if (!lhs_interface_type->implements_interface(rhs_type, NULL)) 363 { 364 go_assert(saw_errors()); 365 return Expression::make_error(location); 366 } 367 368 // An interface is a tuple. If LHS_TYPE is an empty interface type, 369 // then the first field is the type descriptor for RHS_TYPE. 370 // Otherwise it is the interface method table for RHS_TYPE. 371 Expression* first_field; 372 if (lhs_is_empty) 373 first_field = Expression::make_type_descriptor(rhs_type, location); 374 else 375 { 376 // Build the interface method table for this interface and this 377 // object type: a list of function pointers for each interface 378 // method. 379 Named_type* rhs_named_type = rhs_type->named_type(); 380 Struct_type* rhs_struct_type = rhs_type->struct_type(); 381 bool is_pointer = false; 382 if (rhs_named_type == NULL && rhs_struct_type == NULL) 383 { 384 rhs_named_type = rhs_type->deref()->named_type(); 385 rhs_struct_type = rhs_type->deref()->struct_type(); 386 is_pointer = true; 387 } 388 if (rhs_named_type != NULL) 389 first_field = 390 rhs_named_type->interface_method_table(lhs_interface_type, 391 is_pointer); 392 else if (rhs_struct_type != NULL) 393 first_field = 394 rhs_struct_type->interface_method_table(lhs_interface_type, 395 is_pointer); 396 else 397 first_field = Expression::make_nil(location); 398 } 399 400 Expression* obj; 401 if (rhs_type->is_direct_iface_type()) 402 { 403 // We are assigning a pointer to the interface; the interface 404 // holds the pointer itself. 405 obj = unpack_direct_iface(rhs, location); 406 } 407 else 408 { 409 // We are assigning a non-pointer value to the interface; the 410 // interface gets a copy of the value in the heap if it escapes. 411 412 // An exception is &global if global is notinheap, which is a 413 // pointer value but not a direct-iface type and we can't simply 414 // take its address. 415 bool is_address = (rhs->unary_expression() != NULL 416 && rhs->unary_expression()->op() == OPERATOR_AND); 417 418 if (rhs->is_constant() && !is_address) 419 obj = Expression::make_unary(OPERATOR_AND, rhs, location); 420 else 421 { 422 obj = Expression::make_heap_expression(rhs, location); 423 if (on_stack) 424 obj->heap_expression()->set_allocate_on_stack(); 425 } 426 } 427 428 return Expression::make_interface_value(lhs_type, first_field, obj, location); 429 } 430 431 // Return an expression for the pointer-typed value of a direct interface 432 // type. Specifically, for single field struct or array, get the single 433 // field, and do this recursively. The reason for this is that we don't 434 // want to assign a struct or an array to a pointer-typed field. The 435 // backend may not like that. 436 437 Expression* 438 Expression::unpack_direct_iface(Expression* rhs, Location loc) 439 { 440 Struct_type* st = rhs->type()->struct_type(); 441 if (st != NULL) 442 { 443 go_assert(st->field_count() == 1); 444 Expression* field = Expression::make_field_reference(rhs, 0, loc); 445 return unpack_direct_iface(field, loc); 446 } 447 Array_type* at = rhs->type()->array_type(); 448 if (at != NULL) 449 { 450 int64_t len; 451 bool ok = at->int_length(&len); 452 go_assert(ok && len == 1); 453 Type* int_type = Type::lookup_integer_type("int"); 454 Expression* index = Expression::make_integer_ul(0, int_type, loc); 455 Expression* elem = Expression::make_array_index(rhs, index, NULL, NULL, loc); 456 return unpack_direct_iface(elem, loc); 457 } 458 return rhs; 459 } 460 461 // The opposite of unpack_direct_iface. 462 463 Expression* 464 Expression::pack_direct_iface(Type* t, Expression* rhs, Location loc) 465 { 466 if (rhs->type() == t) 467 return rhs; 468 Struct_type* st = t->struct_type(); 469 if (st != NULL) 470 { 471 Expression_list* vals = new Expression_list(); 472 vals->push_back(pack_direct_iface(st->field(0)->type(), rhs, loc)); 473 return Expression::make_struct_composite_literal(t, vals, loc); 474 } 475 Array_type* at = t->array_type(); 476 if (at != NULL) 477 { 478 Expression_list* vals = new Expression_list(); 479 vals->push_back(pack_direct_iface(at->element_type(), rhs, loc)); 480 return Expression::make_array_composite_literal(t, vals, loc); 481 } 482 return Expression::make_unsafe_cast(t, rhs, loc); 483 } 484 485 // Return an expression for the type descriptor of RHS. 486 487 Expression* 488 Expression::get_interface_type_descriptor(Expression* rhs) 489 { 490 go_assert(rhs->type()->interface_type() != NULL); 491 Location location = rhs->location(); 492 493 // The type descriptor is the first field of an empty interface. 494 if (rhs->type()->interface_type()->is_empty()) 495 return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, 496 location); 497 498 Expression* mtable = 499 Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); 500 501 Expression* descriptor = 502 Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, location); 503 descriptor = Expression::make_field_reference(descriptor, 0, location); 504 Expression* nil = Expression::make_nil(location); 505 506 Expression* eq = 507 Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); 508 return Expression::make_conditional(eq, nil, descriptor, location); 509 } 510 511 // Return an expression for the conversion of an interface type to an 512 // interface type. 513 514 Expression* 515 Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, 516 bool for_type_guard, 517 Location location) 518 { 519 if (Type::are_identical(lhs_type, rhs->type(), 520 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 521 NULL)) 522 return rhs; 523 524 Interface_type* lhs_interface_type = lhs_type->interface_type(); 525 bool lhs_is_empty = lhs_interface_type->is_empty(); 526 527 // In the general case this requires runtime examination of the type 528 // method table to match it up with the interface methods. 529 530 // FIXME: If all of the methods in the right hand side interface 531 // also appear in the left hand side interface, then we don't need 532 // to do a runtime check, although we still need to build a new 533 // method table. 534 535 // We are going to evaluate RHS multiple times. 536 go_assert(rhs->is_multi_eval_safe()); 537 538 // Get the type descriptor for the right hand side. This will be 539 // NULL for a nil interface. 540 Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); 541 Expression* lhs_type_expr = 542 Expression::make_type_descriptor(lhs_type, location); 543 544 Expression* first_field; 545 if (for_type_guard) 546 { 547 // A type assertion fails when converting a nil interface. 548 first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2, 549 lhs_type_expr, rhs_type_expr); 550 } 551 else if (lhs_is_empty) 552 { 553 // A conversion to an empty interface always succeeds, and the 554 // first field is just the type descriptor of the object. 555 first_field = rhs_type_expr; 556 } 557 else 558 { 559 // A conversion to a non-empty interface may fail, but unlike a 560 // type assertion converting nil will always succeed. 561 first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2, 562 lhs_type_expr, rhs_type_expr); 563 } 564 565 // The second field is simply the object pointer. 566 Expression* obj = 567 Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); 568 return Expression::make_interface_value(lhs_type, first_field, obj, location); 569 } 570 571 // Return an expression for the conversion of an interface type to a 572 // non-interface type. 573 574 Expression* 575 Expression::convert_interface_to_type(Gogo* gogo, Type *lhs_type, Expression* rhs, 576 Location location) 577 { 578 // We are going to evaluate RHS multiple times. 579 go_assert(rhs->is_multi_eval_safe()); 580 581 // Build an expression to check that the type is valid. It will 582 // panic with an appropriate runtime type error if the type is not 583 // valid. 584 // (lhs_type == rhs_type ? nil /*dummy*/ : 585 // panicdottype(lhs_type, rhs_type, inter_type)) 586 // For some Oses, we need to call runtime.eqtype instead of 587 // lhs_type == rhs_type, as we may have unmerged type descriptors 588 // from shared libraries. 589 Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, 590 location); 591 Expression* rhs_descriptor = 592 Expression::get_interface_type_descriptor(rhs); 593 594 Type* rhs_type = rhs->type(); 595 Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, 596 location); 597 598 Expression* cond; 599 if (gogo->need_eqtype()) { 600 cond = Runtime::make_call(Runtime::EQTYPE, location, 601 2, lhs_type_expr, 602 rhs_descriptor); 603 } else { 604 cond = Expression::make_binary(OPERATOR_EQEQ, lhs_type_expr, 605 rhs_descriptor, location); 606 } 607 608 rhs_descriptor = Expression::get_interface_type_descriptor(rhs); 609 Expression* panic = Runtime::make_call(Runtime::PANICDOTTYPE, location, 610 3, lhs_type_expr->copy(), 611 rhs_descriptor, 612 rhs_inter_expr); 613 Expression* nil = Expression::make_nil(location); 614 Expression* check = Expression::make_conditional(cond, nil, panic, 615 location); 616 617 // If the conversion succeeds, pull out the value. 618 Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, 619 location); 620 621 // If the value is a direct interface, then it is the value we want. 622 // Otherwise it points to the value. 623 if (lhs_type->is_direct_iface_type()) 624 obj = Expression::pack_direct_iface(lhs_type, obj, location); 625 else 626 { 627 obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, 628 location); 629 obj = Expression::make_dereference(obj, NIL_CHECK_NOT_NEEDED, 630 location); 631 } 632 return Expression::make_compound(check, obj, location); 633 } 634 635 // Convert an expression to its backend representation. This is implemented by 636 // the child class. Not that it is not in general safe to call this multiple 637 // times for a single expression, but that we don't catch such errors. 638 639 Bexpression* 640 Expression::get_backend(Translate_context* context) 641 { 642 // The child may have marked this expression as having an error. 643 if (this->classification_ == EXPRESSION_ERROR) 644 { 645 go_assert(saw_errors()); 646 return context->backend()->error_expression(); 647 } 648 649 return this->do_get_backend(context); 650 } 651 652 // Return a backend expression for VAL. 653 Bexpression* 654 Expression::backend_numeric_constant_expression(Translate_context* context, 655 Numeric_constant* val) 656 { 657 Gogo* gogo = context->gogo(); 658 Type* type = val->type(); 659 if (type == NULL) 660 return gogo->backend()->error_expression(); 661 662 Btype* btype = type->get_backend(gogo); 663 Bexpression* ret; 664 if (type->integer_type() != NULL) 665 { 666 mpz_t ival; 667 if (!val->to_int(&ival)) 668 { 669 go_assert(saw_errors()); 670 return gogo->backend()->error_expression(); 671 } 672 ret = gogo->backend()->integer_constant_expression(btype, ival); 673 mpz_clear(ival); 674 } 675 else if (type->float_type() != NULL) 676 { 677 mpfr_t fval; 678 if (!val->to_float(&fval)) 679 { 680 go_assert(saw_errors()); 681 return gogo->backend()->error_expression(); 682 } 683 ret = gogo->backend()->float_constant_expression(btype, fval); 684 mpfr_clear(fval); 685 } 686 else if (type->complex_type() != NULL) 687 { 688 mpc_t cval; 689 if (!val->to_complex(&cval)) 690 { 691 go_assert(saw_errors()); 692 return gogo->backend()->error_expression(); 693 } 694 ret = gogo->backend()->complex_constant_expression(btype, cval); 695 mpc_clear(cval); 696 } 697 else 698 go_unreachable(); 699 700 return ret; 701 } 702 703 // Insert bounds checks for an index expression. Check that that VAL 704 // >= 0 and that it fits in an int. Then check that VAL OP BOUND is 705 // true. If any condition is false, call one of the CODE runtime 706 // functions, which will panic. 707 708 void 709 Expression::check_bounds(Expression* val, Operator op, Expression* bound, 710 Runtime::Function code, 711 Runtime::Function code_u, 712 Runtime::Function code_extend, 713 Runtime::Function code_extend_u, 714 Statement_inserter* inserter, 715 Location loc) 716 { 717 go_assert(val->is_multi_eval_safe()); 718 go_assert(bound->is_multi_eval_safe()); 719 720 Type* int_type = Type::lookup_integer_type("int"); 721 int int_type_size = int_type->integer_type()->bits(); 722 723 Type* val_type = val->type(); 724 if (val_type->integer_type() == NULL) 725 { 726 go_assert(saw_errors()); 727 return; 728 } 729 int val_type_size = val_type->integer_type()->bits(); 730 bool val_is_unsigned = val_type->integer_type()->is_unsigned(); 731 732 // Check that VAL >= 0. 733 Expression* check = NULL; 734 if (!val_is_unsigned) 735 { 736 Expression* zero = Expression::make_integer_ul(0, val_type, loc); 737 check = Expression::make_binary(OPERATOR_GE, val->copy(), zero, loc); 738 } 739 740 // If VAL's type is larger than int, check that VAL fits in an int. 741 if (val_type_size > int_type_size 742 || (val_type_size == int_type_size 743 && val_is_unsigned)) 744 { 745 mpz_t one; 746 mpz_init_set_ui(one, 1UL); 747 748 // maxval = 2^(int_type_size - 1) - 1 749 mpz_t maxval; 750 mpz_init(maxval); 751 mpz_mul_2exp(maxval, one, int_type_size - 1); 752 mpz_sub_ui(maxval, maxval, 1); 753 Expression* max = Expression::make_integer_z(&maxval, val_type, loc); 754 mpz_clear(one); 755 mpz_clear(maxval); 756 757 Expression* cmp = Expression::make_binary(OPERATOR_LE, val->copy(), 758 max, loc); 759 if (check == NULL) 760 check = cmp; 761 else 762 check = Expression::make_binary(OPERATOR_ANDAND, check, cmp, loc); 763 } 764 765 // For the final check we can assume that VAL fits in an int. 766 Expression* ival; 767 if (val_type == int_type) 768 ival = val->copy(); 769 else 770 ival = Expression::make_cast(int_type, val->copy(), loc); 771 772 // BOUND is assumed to fit in an int. Either it comes from len or 773 // cap, or it was checked by an earlier call. 774 Expression* ibound; 775 if (bound->type() == int_type) 776 ibound = bound->copy(); 777 else 778 ibound = Expression::make_cast(int_type, bound->copy(), loc); 779 780 Expression* cmp = Expression::make_binary(op, ival, ibound, loc); 781 if (check == NULL) 782 check = cmp; 783 else 784 check = Expression::make_binary(OPERATOR_ANDAND, check, cmp, loc); 785 786 Runtime::Function c; 787 if (val_type_size > int_type_size) 788 { 789 if (val_is_unsigned) 790 c = code_extend_u; 791 else 792 c = code_extend; 793 } 794 else 795 { 796 if (val_is_unsigned) 797 c = code_u; 798 else 799 c = code; 800 } 801 802 Expression* ignore = Expression::make_boolean(true, loc); 803 Expression* crash = Runtime::make_call(c, loc, 2, 804 val->copy(), bound->copy()); 805 Expression* cond = Expression::make_conditional(check, ignore, crash, loc); 806 inserter->insert(Statement::make_statement(cond, true)); 807 } 808 809 void 810 Expression::dump_expression(Ast_dump_context* ast_dump_context) const 811 { 812 this->do_dump_expression(ast_dump_context); 813 } 814 815 // Error expressions. This are used to avoid cascading errors. 816 817 class Error_expression : public Expression 818 { 819 public: 820 Error_expression(Location location) 821 : Expression(EXPRESSION_ERROR, location) 822 { } 823 824 protected: 825 bool 826 do_is_constant() const 827 { return true; } 828 829 bool 830 do_numeric_constant_value(Numeric_constant* nc) const 831 { 832 nc->set_unsigned_long(NULL, 0); 833 return true; 834 } 835 836 bool 837 do_discarding_value() 838 { return true; } 839 840 Type* 841 do_type() 842 { return Type::make_error_type(); } 843 844 void 845 do_determine_type(const Type_context*) 846 { } 847 848 Expression* 849 do_copy() 850 { return this; } 851 852 bool 853 do_is_addressable() const 854 { return true; } 855 856 Bexpression* 857 do_get_backend(Translate_context* context) 858 { return context->backend()->error_expression(); } 859 860 void 861 do_dump_expression(Ast_dump_context*) const; 862 }; 863 864 // Dump the ast representation for an error expression to a dump context. 865 866 void 867 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 868 { 869 ast_dump_context->ostream() << "_Error_" ; 870 } 871 872 Expression* 873 Expression::make_error(Location location) 874 { 875 return new Error_expression(location); 876 } 877 878 // An expression which is really a type. This is used during parsing. 879 // It is an error if these survive after lowering. 880 881 class 882 Type_expression : public Expression 883 { 884 public: 885 Type_expression(Type* type, Location location) 886 : Expression(EXPRESSION_TYPE, location), 887 type_(type) 888 { } 889 890 protected: 891 int 892 do_traverse(Traverse* traverse) 893 { return Type::traverse(this->type_, traverse); } 894 895 Type* 896 do_type() 897 { return this->type_; } 898 899 void 900 do_determine_type(const Type_context*) 901 { } 902 903 void 904 do_check_types(Gogo*); 905 906 Expression* 907 do_copy() 908 { return this; } 909 910 Bexpression* 911 do_get_backend(Translate_context*) 912 { go_unreachable(); } 913 914 void do_dump_expression(Ast_dump_context*) const; 915 916 private: 917 // The type which we are representing as an expression. 918 Type* type_; 919 }; 920 921 void 922 Type_expression::do_check_types(Gogo*) 923 { 924 if (this->type_->is_error()) 925 { 926 go_assert(saw_errors()); 927 this->set_is_error(); 928 } 929 else 930 this->report_error(_("invalid use of type")); 931 } 932 933 void 934 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 935 { 936 ast_dump_context->dump_type(this->type_); 937 } 938 939 Expression* 940 Expression::make_type(Type* type, Location location) 941 { 942 return new Type_expression(type, location); 943 } 944 945 // Class Parser_expression. 946 947 Type* 948 Parser_expression::do_type() 949 { 950 // We should never really ask for the type of a Parser_expression. 951 // However, it can happen, at least when we have an invalid const 952 // whose initializer refers to the const itself. In that case we 953 // may ask for the type when lowering the const itself. 954 go_assert(saw_errors()); 955 return Type::make_error_type(); 956 } 957 958 // Class Var_expression. 959 960 // Lower a variable expression. Here we just make sure that the 961 // initialization expression of the variable has been lowered. This 962 // ensures that we will be able to determine the type of the variable 963 // if necessary. 964 965 Expression* 966 Var_expression::do_lower(Gogo* gogo, Named_object* function, 967 Statement_inserter* inserter, int) 968 { 969 if (this->variable_->is_variable()) 970 { 971 Variable* var = this->variable_->var_value(); 972 // This is either a local variable or a global variable. A 973 // reference to a variable which is local to an enclosing 974 // function will be a reference to a field in a closure. 975 if (var->is_global()) 976 { 977 function = NULL; 978 inserter = NULL; 979 } 980 var->lower_init_expression(gogo, function, inserter); 981 } 982 return this; 983 } 984 985 // Return the type of a reference to a variable. 986 987 Type* 988 Var_expression::do_type() 989 { 990 if (this->variable_->is_variable()) 991 return this->variable_->var_value()->type(); 992 else if (this->variable_->is_result_variable()) 993 return this->variable_->result_var_value()->type(); 994 else 995 go_unreachable(); 996 } 997 998 // Determine the type of a reference to a variable. 999 1000 void 1001 Var_expression::do_determine_type(const Type_context*) 1002 { 1003 if (this->variable_->is_variable()) 1004 this->variable_->var_value()->determine_type(); 1005 } 1006 1007 // Something takes the address of this variable. This means that we 1008 // may want to move the variable onto the heap. 1009 1010 void 1011 Var_expression::do_address_taken(bool escapes) 1012 { 1013 if (!escapes) 1014 { 1015 if (this->variable_->is_variable()) 1016 this->variable_->var_value()->set_non_escaping_address_taken(); 1017 else if (this->variable_->is_result_variable()) 1018 this->variable_->result_var_value()->set_non_escaping_address_taken(); 1019 else 1020 go_unreachable(); 1021 } 1022 else 1023 { 1024 if (this->variable_->is_variable()) 1025 this->variable_->var_value()->set_address_taken(); 1026 else if (this->variable_->is_result_variable()) 1027 this->variable_->result_var_value()->set_address_taken(); 1028 else 1029 go_unreachable(); 1030 } 1031 1032 if (this->variable_->is_variable() 1033 && this->variable_->var_value()->is_in_heap()) 1034 { 1035 Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP); 1036 Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); 1037 } 1038 } 1039 1040 // Export a reference to a variable. 1041 1042 void 1043 Var_expression::do_export(Export_function_body* efb) const 1044 { 1045 Named_object* no = this->variable_; 1046 if (no->is_result_variable() || !no->var_value()->is_global()) 1047 efb->write_string(Gogo::unpack_hidden_name(no->name())); 1048 else 1049 Expression::export_name(efb, no); 1050 } 1051 1052 // Get the backend representation for a reference to a variable. 1053 1054 Bexpression* 1055 Var_expression::do_get_backend(Translate_context* context) 1056 { 1057 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), 1058 context->function()); 1059 bool is_in_heap; 1060 Location loc = this->location(); 1061 Btype* btype; 1062 Gogo* gogo = context->gogo(); 1063 if (this->variable_->is_variable()) 1064 { 1065 is_in_heap = this->variable_->var_value()->is_in_heap(); 1066 btype = this->variable_->var_value()->type()->get_backend(gogo); 1067 } 1068 else if (this->variable_->is_result_variable()) 1069 { 1070 is_in_heap = this->variable_->result_var_value()->is_in_heap(); 1071 btype = this->variable_->result_var_value()->type()->get_backend(gogo); 1072 } 1073 else 1074 go_unreachable(); 1075 1076 Bexpression* ret = 1077 context->backend()->var_expression(bvar, loc); 1078 if (is_in_heap) 1079 ret = context->backend()->indirect_expression(btype, ret, true, loc); 1080 return ret; 1081 } 1082 1083 // Ast dump for variable expression. 1084 1085 void 1086 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1087 { 1088 ast_dump_context->ostream() << this->variable_->message_name() ; 1089 } 1090 1091 // Make a reference to a variable in an expression. 1092 1093 Expression* 1094 Expression::make_var_reference(Named_object* var, Location location) 1095 { 1096 if (var->is_sink()) 1097 return Expression::make_sink(location); 1098 1099 // FIXME: Creating a new object for each reference to a variable is 1100 // wasteful. 1101 return new Var_expression(var, location); 1102 } 1103 1104 // Class Enclosed_var_expression. 1105 1106 int 1107 Enclosed_var_expression::do_traverse(Traverse*) 1108 { 1109 return TRAVERSE_CONTINUE; 1110 } 1111 1112 // Lower the reference to the enclosed variable. 1113 1114 Expression* 1115 Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function, 1116 Statement_inserter* inserter, int) 1117 { 1118 gogo->lower_expression(function, inserter, &this->reference_); 1119 return this; 1120 } 1121 1122 // Flatten the reference to the enclosed variable. 1123 1124 Expression* 1125 Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function, 1126 Statement_inserter* inserter) 1127 { 1128 gogo->flatten_expression(function, inserter, &this->reference_); 1129 return this; 1130 } 1131 1132 void 1133 Enclosed_var_expression::do_address_taken(bool escapes) 1134 { 1135 if (!escapes) 1136 { 1137 if (this->variable_->is_variable()) 1138 this->variable_->var_value()->set_non_escaping_address_taken(); 1139 else if (this->variable_->is_result_variable()) 1140 this->variable_->result_var_value()->set_non_escaping_address_taken(); 1141 else 1142 go_unreachable(); 1143 } 1144 else 1145 { 1146 if (this->variable_->is_variable()) 1147 this->variable_->var_value()->set_address_taken(); 1148 else if (this->variable_->is_result_variable()) 1149 this->variable_->result_var_value()->set_address_taken(); 1150 else 1151 go_unreachable(); 1152 } 1153 1154 if (this->variable_->is_variable() 1155 && this->variable_->var_value()->is_in_heap()) 1156 Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); 1157 } 1158 1159 // Ast dump for enclosed variable expression. 1160 1161 void 1162 Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const 1163 { 1164 adc->ostream() << this->variable_->message_name(); 1165 } 1166 1167 // Make a reference to a variable within an enclosing function. 1168 1169 Expression* 1170 Expression::make_enclosing_var_reference(Expression* reference, 1171 Named_object* var, Location location) 1172 { 1173 return new Enclosed_var_expression(reference, var, location); 1174 } 1175 1176 // Class Temporary_reference_expression. 1177 1178 // The type. 1179 1180 Type* 1181 Temporary_reference_expression::do_type() 1182 { 1183 return this->statement_->type(); 1184 } 1185 1186 // Called if something takes the address of this temporary variable. 1187 // We never have to move temporary variables to the heap, but we do 1188 // need to know that they must live in the stack rather than in a 1189 // register. 1190 1191 void 1192 Temporary_reference_expression::do_address_taken(bool) 1193 { 1194 this->statement_->set_is_address_taken(); 1195 } 1196 1197 // Export a reference to a temporary. 1198 1199 void 1200 Temporary_reference_expression::do_export(Export_function_body* efb) const 1201 { 1202 unsigned int idx = efb->temporary_index(this->statement_); 1203 char buf[50]; 1204 snprintf(buf, sizeof buf, "$t%u", idx); 1205 efb->write_c_string(buf); 1206 } 1207 1208 // Import a reference to a temporary. 1209 1210 Expression* 1211 Temporary_reference_expression::do_import(Import_function_body* ifb, 1212 Location loc) 1213 { 1214 std::string id = ifb->read_identifier(); 1215 go_assert(id[0] == '$' && id[1] == 't'); 1216 const char *p = id.c_str(); 1217 char *end; 1218 long idx = strtol(p + 2, &end, 10); 1219 if (*end != '\0' || idx > 0x7fffffff) 1220 { 1221 if (!ifb->saw_error()) 1222 go_error_at(loc, 1223 ("invalid export data for %qs: " 1224 "invalid temporary reference index at %lu"), 1225 ifb->name().c_str(), 1226 static_cast<unsigned long>(ifb->off())); 1227 ifb->set_saw_error(); 1228 return Expression::make_error(loc); 1229 } 1230 1231 Temporary_statement* temp = 1232 ifb->temporary_statement(static_cast<unsigned int>(idx)); 1233 if (temp == NULL) 1234 { 1235 if (!ifb->saw_error()) 1236 go_error_at(loc, 1237 ("invalid export data for %qs: " 1238 "undefined temporary reference index at %lu"), 1239 ifb->name().c_str(), 1240 static_cast<unsigned long>(ifb->off())); 1241 ifb->set_saw_error(); 1242 return Expression::make_error(loc); 1243 } 1244 1245 return Expression::make_temporary_reference(temp, loc); 1246 } 1247 1248 // Get a backend expression referring to the variable. 1249 1250 Bexpression* 1251 Temporary_reference_expression::do_get_backend(Translate_context* context) 1252 { 1253 Gogo* gogo = context->gogo(); 1254 Bvariable* bvar = this->statement_->get_backend_variable(context); 1255 Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); 1256 1257 // The backend can't always represent the same set of recursive types 1258 // that the Go frontend can. In some cases this means that a 1259 // temporary variable won't have the right backend type. Correct 1260 // that here by adding a type cast. We need to use base() to push 1261 // the circularity down one level. 1262 Type* stype = this->statement_->type(); 1263 if (!this->is_lvalue_ 1264 && stype->points_to() != NULL 1265 && stype->points_to()->is_void_type()) 1266 { 1267 Btype* btype = this->type()->base()->get_backend(gogo); 1268 ret = gogo->backend()->convert_expression(btype, ret, this->location()); 1269 } 1270 return ret; 1271 } 1272 1273 // Ast dump for temporary reference. 1274 1275 void 1276 Temporary_reference_expression::do_dump_expression( 1277 Ast_dump_context* ast_dump_context) const 1278 { 1279 ast_dump_context->dump_temp_variable_name(this->statement_); 1280 } 1281 1282 // Make a reference to a temporary variable. 1283 1284 Temporary_reference_expression* 1285 Expression::make_temporary_reference(Temporary_statement* statement, 1286 Location location) 1287 { 1288 statement->add_use(); 1289 return new Temporary_reference_expression(statement, location); 1290 } 1291 1292 // Class Set_and_use_temporary_expression. 1293 1294 // Return the type. 1295 1296 Type* 1297 Set_and_use_temporary_expression::do_type() 1298 { 1299 return this->statement_->type(); 1300 } 1301 1302 // Determine the type of the expression. 1303 1304 void 1305 Set_and_use_temporary_expression::do_determine_type( 1306 const Type_context* context) 1307 { 1308 this->expr_->determine_type(context); 1309 } 1310 1311 // Take the address. 1312 1313 void 1314 Set_and_use_temporary_expression::do_address_taken(bool) 1315 { 1316 this->statement_->set_is_address_taken(); 1317 } 1318 1319 // Return the backend representation. 1320 1321 Bexpression* 1322 Set_and_use_temporary_expression::do_get_backend(Translate_context* context) 1323 { 1324 Location loc = this->location(); 1325 Gogo* gogo = context->gogo(); 1326 Bvariable* bvar = this->statement_->get_backend_variable(context); 1327 Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, loc); 1328 1329 Named_object* fn = context->function(); 1330 go_assert(fn != NULL); 1331 Bfunction* bfn = fn->func_value()->get_or_make_decl(gogo, fn); 1332 Bexpression* bexpr = this->expr_->get_backend(context); 1333 Bstatement* set = gogo->backend()->assignment_statement(bfn, lvar_ref, 1334 bexpr, loc); 1335 Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); 1336 Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); 1337 return ret; 1338 } 1339 1340 // Dump. 1341 1342 void 1343 Set_and_use_temporary_expression::do_dump_expression( 1344 Ast_dump_context* ast_dump_context) const 1345 { 1346 ast_dump_context->ostream() << '('; 1347 ast_dump_context->dump_temp_variable_name(this->statement_); 1348 ast_dump_context->ostream() << " = "; 1349 this->expr_->dump_expression(ast_dump_context); 1350 ast_dump_context->ostream() << ')'; 1351 } 1352 1353 // Make a set-and-use temporary. 1354 1355 Set_and_use_temporary_expression* 1356 Expression::make_set_and_use_temporary(Temporary_statement* statement, 1357 Expression* expr, Location location) 1358 { 1359 return new Set_and_use_temporary_expression(statement, expr, location); 1360 } 1361 1362 // A sink expression--a use of the blank identifier _. 1363 1364 class Sink_expression : public Expression 1365 { 1366 public: 1367 Sink_expression(Location location) 1368 : Expression(EXPRESSION_SINK, location), 1369 type_(NULL), bvar_(NULL) 1370 { } 1371 1372 protected: 1373 bool 1374 do_discarding_value() 1375 { return true; } 1376 1377 Type* 1378 do_type(); 1379 1380 void 1381 do_determine_type(const Type_context*); 1382 1383 Expression* 1384 do_copy() 1385 { return new Sink_expression(this->location()); } 1386 1387 Bexpression* 1388 do_get_backend(Translate_context*); 1389 1390 void 1391 do_dump_expression(Ast_dump_context*) const; 1392 1393 private: 1394 // The type of this sink variable. 1395 Type* type_; 1396 // The temporary variable we generate. 1397 Bvariable* bvar_; 1398 }; 1399 1400 // Return the type of a sink expression. 1401 1402 Type* 1403 Sink_expression::do_type() 1404 { 1405 if (this->type_ == NULL) 1406 return Type::make_sink_type(); 1407 return this->type_; 1408 } 1409 1410 // Determine the type of a sink expression. 1411 1412 void 1413 Sink_expression::do_determine_type(const Type_context* context) 1414 { 1415 if (context->type != NULL) 1416 this->type_ = context->type; 1417 } 1418 1419 // Return a temporary variable for a sink expression. This will 1420 // presumably be a write-only variable which the middle-end will drop. 1421 1422 Bexpression* 1423 Sink_expression::do_get_backend(Translate_context* context) 1424 { 1425 Location loc = this->location(); 1426 Gogo* gogo = context->gogo(); 1427 if (this->bvar_ == NULL) 1428 { 1429 if (this->type_ == NULL || this->type_->is_sink_type()) 1430 { 1431 go_assert(saw_errors()); 1432 return gogo->backend()->error_expression(); 1433 } 1434 1435 Named_object* fn = context->function(); 1436 go_assert(fn != NULL); 1437 Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); 1438 Btype* bt = this->type_->get_backend(context->gogo()); 1439 Bstatement* decl; 1440 this->bvar_ = 1441 gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, 1442 0, loc, &decl); 1443 Bexpression* var_ref = 1444 gogo->backend()->var_expression(this->bvar_, loc); 1445 var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); 1446 return var_ref; 1447 } 1448 return gogo->backend()->var_expression(this->bvar_, loc); 1449 } 1450 1451 // Ast dump for sink expression. 1452 1453 void 1454 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1455 { 1456 ast_dump_context->ostream() << "_" ; 1457 } 1458 1459 // Make a sink expression. 1460 1461 Expression* 1462 Expression::make_sink(Location location) 1463 { 1464 return new Sink_expression(location); 1465 } 1466 1467 // Class Func_expression. 1468 1469 // FIXME: Can a function expression appear in a constant expression? 1470 // The value is unchanging. Initializing a constant to the address of 1471 // a function seems like it could work, though there might be little 1472 // point to it. 1473 1474 // Traversal. 1475 1476 int 1477 Func_expression::do_traverse(Traverse* traverse) 1478 { 1479 return (this->closure_ == NULL 1480 ? TRAVERSE_CONTINUE 1481 : Expression::traverse(&this->closure_, traverse)); 1482 } 1483 1484 // Return the type of a function expression. 1485 1486 Type* 1487 Func_expression::do_type() 1488 { 1489 if (this->function_->is_function()) 1490 return this->function_->func_value()->type(); 1491 else if (this->function_->is_function_declaration()) 1492 return this->function_->func_declaration_value()->type(); 1493 else 1494 go_unreachable(); 1495 } 1496 1497 // Get the backend representation for the code of a function expression. 1498 1499 Bexpression* 1500 Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) 1501 { 1502 Function_type* fntype; 1503 if (no->is_function()) 1504 fntype = no->func_value()->type(); 1505 else if (no->is_function_declaration()) 1506 fntype = no->func_declaration_value()->type(); 1507 else 1508 go_unreachable(); 1509 1510 // Builtin functions are handled specially by Call_expression. We 1511 // can't take their address. 1512 if (fntype->is_builtin()) 1513 { 1514 go_error_at(loc, 1515 ("invalid use of special built-in function %qs; " 1516 "must be called"), 1517 no->message_name().c_str()); 1518 return gogo->backend()->error_expression(); 1519 } 1520 1521 Bfunction* fndecl; 1522 if (no->is_function()) 1523 fndecl = no->func_value()->get_or_make_decl(gogo, no); 1524 else if (no->is_function_declaration()) 1525 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); 1526 else 1527 go_unreachable(); 1528 1529 return gogo->backend()->function_code_expression(fndecl, loc); 1530 } 1531 1532 // Get the backend representation for a function expression. This is used when 1533 // we take the address of a function rather than simply calling it. A func 1534 // value is represented as a pointer to a block of memory. The first 1535 // word of that memory is a pointer to the function code. The 1536 // remaining parts of that memory are the addresses of variables that 1537 // the function closes over. 1538 1539 Bexpression* 1540 Func_expression::do_get_backend(Translate_context* context) 1541 { 1542 // If there is no closure, just use the function descriptor. 1543 if (this->closure_ == NULL) 1544 { 1545 Gogo* gogo = context->gogo(); 1546 Named_object* no = this->function_; 1547 Expression* descriptor; 1548 if (no->is_function()) 1549 descriptor = no->func_value()->descriptor(gogo, no); 1550 else if (no->is_function_declaration()) 1551 { 1552 if (no->func_declaration_value()->type()->is_builtin()) 1553 { 1554 go_error_at(this->location(), 1555 ("invalid use of special built-in function %qs; " 1556 "must be called"), 1557 no->message_name().c_str()); 1558 return gogo->backend()->error_expression(); 1559 } 1560 descriptor = no->func_declaration_value()->descriptor(gogo, no); 1561 } 1562 else 1563 go_unreachable(); 1564 1565 Bexpression* bdesc = descriptor->get_backend(context); 1566 return gogo->backend()->address_expression(bdesc, this->location()); 1567 } 1568 1569 go_assert(this->function_->func_value()->enclosing() != NULL); 1570 1571 // If there is a closure, then the closure is itself the function 1572 // expression. It is a pointer to a struct whose first field points 1573 // to the function code and whose remaining fields are the addresses 1574 // of the closed-over variables. 1575 Bexpression *bexpr = this->closure_->get_backend(context); 1576 1577 // Introduce a backend type conversion, to account for any differences 1578 // between the argument type (function descriptor, struct with a 1579 // single field) and the closure (struct with multiple fields). 1580 Gogo* gogo = context->gogo(); 1581 Btype *btype = this->type()->get_backend(gogo); 1582 return gogo->backend()->convert_expression(btype, bexpr, this->location()); 1583 } 1584 1585 // The cost of inlining a function reference. 1586 1587 int 1588 Func_expression::do_inlining_cost() const 1589 { 1590 // FIXME: We don't inline references to nested functions. 1591 if (this->closure_ != NULL) 1592 return 0x100000; 1593 if (this->function_->is_function() 1594 && this->function_->func_value()->enclosing() != NULL) 1595 return 0x100000; 1596 1597 return 1; 1598 } 1599 1600 // Export a reference to a function. 1601 1602 void 1603 Func_expression::do_export(Export_function_body* efb) const 1604 { 1605 Expression::export_name(efb, this->function_); 1606 } 1607 1608 // Ast dump for function. 1609 1610 void 1611 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1612 { 1613 ast_dump_context->ostream() << this->function_->name(); 1614 if (this->closure_ != NULL) 1615 { 1616 ast_dump_context->ostream() << " {closure = "; 1617 this->closure_->dump_expression(ast_dump_context); 1618 ast_dump_context->ostream() << "}"; 1619 } 1620 } 1621 1622 // Make a reference to a function in an expression. 1623 1624 Expression* 1625 Expression::make_func_reference(Named_object* function, Expression* closure, 1626 Location location) 1627 { 1628 Func_expression* fe = new Func_expression(function, closure, location); 1629 1630 // Detect references to builtin functions and set the runtime code if 1631 // appropriate. 1632 if (function->is_function_declaration()) 1633 fe->set_runtime_code(Runtime::name_to_code(function->name())); 1634 return fe; 1635 } 1636 1637 // Class Func_descriptor_expression. 1638 1639 // Constructor. 1640 1641 Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) 1642 : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), 1643 fn_(fn), dvar_(NULL) 1644 { 1645 go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); 1646 } 1647 1648 // Traversal. 1649 1650 int 1651 Func_descriptor_expression::do_traverse(Traverse*) 1652 { 1653 return TRAVERSE_CONTINUE; 1654 } 1655 1656 // All function descriptors have the same type. 1657 1658 Type* Func_descriptor_expression::descriptor_type; 1659 1660 void 1661 Func_descriptor_expression::make_func_descriptor_type() 1662 { 1663 if (Func_descriptor_expression::descriptor_type != NULL) 1664 return; 1665 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 1666 Type* struct_type = Type::make_builtin_struct_type(1, "fn", uintptr_type); 1667 Func_descriptor_expression::descriptor_type = 1668 Type::make_builtin_named_type("functionDescriptor", struct_type); 1669 } 1670 1671 Type* 1672 Func_descriptor_expression::do_type() 1673 { 1674 Func_descriptor_expression::make_func_descriptor_type(); 1675 return Func_descriptor_expression::descriptor_type; 1676 } 1677 1678 // The backend representation for a function descriptor. 1679 1680 Bexpression* 1681 Func_descriptor_expression::do_get_backend(Translate_context* context) 1682 { 1683 Named_object* no = this->fn_; 1684 Location loc = no->location(); 1685 if (this->dvar_ != NULL) 1686 return context->backend()->var_expression(this->dvar_, loc); 1687 1688 Gogo* gogo = context->gogo(); 1689 Backend_name bname; 1690 gogo->function_descriptor_backend_name(no, &bname); 1691 bool is_descriptor = false; 1692 if (no->is_function_declaration() 1693 && !no->func_declaration_value()->asm_name().empty() 1694 && Linemap::is_predeclared_location(no->location())) 1695 is_descriptor = true; 1696 1697 // The runtime package implements some functions defined in the 1698 // syscall package. Let the syscall package define the descriptor 1699 // in this case. 1700 if (gogo->compiling_runtime() 1701 && gogo->package_name() == "runtime" 1702 && no->is_function() 1703 && !no->func_value()->asm_name().empty() 1704 && no->func_value()->asm_name().compare(0, 8, "syscall.") == 0) 1705 is_descriptor = true; 1706 1707 Btype* btype = this->type()->get_backend(gogo); 1708 1709 Bvariable* bvar; 1710 if (no->package() != NULL || is_descriptor) 1711 bvar = 1712 context->backend()->immutable_struct_reference(bname.name(), 1713 bname.optional_asm_name(), 1714 btype, loc); 1715 else 1716 { 1717 Location bloc = Linemap::predeclared_location(); 1718 1719 // The runtime package has hash/equality functions that are 1720 // referenced by type descriptors outside of the runtime, so the 1721 // function descriptors must be visible even though they are not 1722 // exported. 1723 bool is_exported_runtime = false; 1724 if (gogo->compiling_runtime() 1725 && gogo->package_name() == "runtime" 1726 && (no->name().find("hash") != std::string::npos 1727 || no->name().find("equal") != std::string::npos)) 1728 is_exported_runtime = true; 1729 1730 bool is_hidden = ((no->is_function() 1731 && no->func_value()->enclosing() != NULL) 1732 || (Gogo::is_hidden_name(no->name()) 1733 && !is_exported_runtime) 1734 || Gogo::is_thunk(no)); 1735 1736 if (no->is_function() && no->func_value()->is_referenced_by_inline()) 1737 is_hidden = false; 1738 1739 unsigned int flags = 0; 1740 if (is_hidden) 1741 flags |= Backend::variable_is_hidden; 1742 bvar = context->backend()->immutable_struct(bname.name(), 1743 bname.optional_asm_name(), 1744 flags, btype, bloc); 1745 Expression_list* vals = new Expression_list(); 1746 vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); 1747 Expression* init = 1748 Expression::make_struct_composite_literal(this->type(), vals, bloc); 1749 Translate_context bcontext(gogo, NULL, NULL, NULL); 1750 bcontext.set_is_const(); 1751 Bexpression* binit = init->get_backend(&bcontext); 1752 context->backend()->immutable_struct_set_init(bvar, bname.name(), 1753 flags, btype, bloc, binit); 1754 } 1755 1756 this->dvar_ = bvar; 1757 return gogo->backend()->var_expression(bvar, loc); 1758 } 1759 1760 // Print a function descriptor expression. 1761 1762 void 1763 Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const 1764 { 1765 context->ostream() << "[descriptor " << this->fn_->name() << "]"; 1766 } 1767 1768 // Make a function descriptor expression. 1769 1770 Func_descriptor_expression* 1771 Expression::make_func_descriptor(Named_object* fn) 1772 { 1773 return new Func_descriptor_expression(fn); 1774 } 1775 1776 // Make the function descriptor type, so that it can be converted. 1777 1778 void 1779 Expression::make_func_descriptor_type() 1780 { 1781 Func_descriptor_expression::make_func_descriptor_type(); 1782 } 1783 1784 // A reference to just the code of a function. 1785 1786 class Func_code_reference_expression : public Expression 1787 { 1788 public: 1789 Func_code_reference_expression(Named_object* function, Location location) 1790 : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), 1791 function_(function) 1792 { } 1793 1794 protected: 1795 int 1796 do_traverse(Traverse*) 1797 { return TRAVERSE_CONTINUE; } 1798 1799 bool 1800 do_is_static_initializer() const 1801 { return true; } 1802 1803 Type* 1804 do_type() 1805 { return Type::make_pointer_type(Type::make_void_type()); } 1806 1807 void 1808 do_determine_type(const Type_context*) 1809 { } 1810 1811 Expression* 1812 do_copy() 1813 { 1814 return Expression::make_func_code_reference(this->function_, 1815 this->location()); 1816 } 1817 1818 Bexpression* 1819 do_get_backend(Translate_context*); 1820 1821 void 1822 do_dump_expression(Ast_dump_context* context) const 1823 { context->ostream() << "[raw " << this->function_->name() << "]" ; } 1824 1825 private: 1826 // The function. 1827 Named_object* function_; 1828 }; 1829 1830 // Get the backend representation for a reference to function code. 1831 1832 Bexpression* 1833 Func_code_reference_expression::do_get_backend(Translate_context* context) 1834 { 1835 return Func_expression::get_code_pointer(context->gogo(), this->function_, 1836 this->location()); 1837 } 1838 1839 // Make a reference to the code of a function. 1840 1841 Expression* 1842 Expression::make_func_code_reference(Named_object* function, Location location) 1843 { 1844 return new Func_code_reference_expression(function, location); 1845 } 1846 1847 // Class Unknown_expression. 1848 1849 // Return the name of an unknown expression. 1850 1851 const std::string& 1852 Unknown_expression::name() const 1853 { 1854 return this->named_object_->name(); 1855 } 1856 1857 // Lower a reference to an unknown name. 1858 1859 Expression* 1860 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) 1861 { 1862 Location location = this->location(); 1863 Named_object* no = this->named_object_; 1864 Named_object* real; 1865 if (!no->is_unknown()) 1866 real = no; 1867 else 1868 { 1869 real = no->unknown_value()->real_named_object(); 1870 if (real == NULL) 1871 { 1872 if (!this->no_error_message_) 1873 go_error_at(location, "reference to undefined name %qs", 1874 this->named_object_->message_name().c_str()); 1875 return Expression::make_error(location); 1876 } 1877 } 1878 switch (real->classification()) 1879 { 1880 case Named_object::NAMED_OBJECT_CONST: 1881 return Expression::make_const_reference(real, location); 1882 case Named_object::NAMED_OBJECT_TYPE: 1883 return Expression::make_type(real->type_value(), location); 1884 case Named_object::NAMED_OBJECT_TYPE_DECLARATION: 1885 if (!this->no_error_message_) 1886 go_error_at(location, "reference to undefined type %qs", 1887 real->message_name().c_str()); 1888 return Expression::make_error(location); 1889 case Named_object::NAMED_OBJECT_VAR: 1890 real->var_value()->set_is_used(); 1891 return Expression::make_var_reference(real, location); 1892 case Named_object::NAMED_OBJECT_FUNC: 1893 case Named_object::NAMED_OBJECT_FUNC_DECLARATION: 1894 return Expression::make_func_reference(real, NULL, location); 1895 case Named_object::NAMED_OBJECT_PACKAGE: 1896 if (!this->no_error_message_) 1897 go_error_at(location, "unexpected reference to package"); 1898 return Expression::make_error(location); 1899 default: 1900 go_unreachable(); 1901 } 1902 } 1903 1904 // Dump the ast representation for an unknown expression to a dump context. 1905 1906 void 1907 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 1908 { 1909 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() 1910 << ")"; 1911 } 1912 1913 // Make a reference to an unknown name. 1914 1915 Unknown_expression* 1916 Expression::make_unknown_reference(Named_object* no, Location location) 1917 { 1918 return new Unknown_expression(no, location); 1919 } 1920 1921 // Start exporting a type conversion for a constant, if needed. This 1922 // returns whether we need to export a closing parenthesis. 1923 1924 bool 1925 Expression::export_constant_type(Export_function_body* efb, Type* type) 1926 { 1927 if (type == NULL 1928 || type->is_abstract() 1929 || type == efb->type_context()) 1930 return false; 1931 efb->write_c_string("$convert("); 1932 efb->write_type(type); 1933 efb->write_c_string(", "); 1934 return true; 1935 } 1936 1937 // Finish a type conversion for a constant. 1938 1939 void 1940 Expression::finish_export_constant_type(Export_function_body* efb, bool needed) 1941 { 1942 if (needed) 1943 efb->write_c_string(")"); 1944 } 1945 1946 // A boolean expression. 1947 1948 class Boolean_expression : public Expression 1949 { 1950 public: 1951 Boolean_expression(bool val, Location location) 1952 : Expression(EXPRESSION_BOOLEAN, location), 1953 val_(val), type_(NULL) 1954 { } 1955 1956 static Expression* 1957 do_import(Import_expression*, Location); 1958 1959 protected: 1960 int 1961 do_traverse(Traverse*); 1962 1963 bool 1964 do_is_constant() const 1965 { return true; } 1966 1967 bool 1968 do_is_zero_value() const 1969 { return this->val_ == false; } 1970 1971 bool 1972 do_boolean_constant_value(bool* val) const 1973 { 1974 *val = this->val_; 1975 return true; 1976 } 1977 1978 bool 1979 do_is_static_initializer() const 1980 { return true; } 1981 1982 Type* 1983 do_type(); 1984 1985 void 1986 do_determine_type(const Type_context*); 1987 1988 Expression* 1989 do_copy() 1990 { return this; } 1991 1992 Bexpression* 1993 do_get_backend(Translate_context* context) 1994 { return context->backend()->boolean_constant_expression(this->val_); } 1995 1996 int 1997 do_inlining_cost() const 1998 { return 1; } 1999 2000 void 2001 do_export(Export_function_body* efb) const; 2002 2003 void 2004 do_dump_expression(Ast_dump_context* ast_dump_context) const 2005 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } 2006 2007 private: 2008 // The constant. 2009 bool val_; 2010 // The type as determined by context. 2011 Type* type_; 2012 }; 2013 2014 // Traverse a boolean expression. We just need to traverse the type 2015 // if there is one. 2016 2017 int 2018 Boolean_expression::do_traverse(Traverse* traverse) 2019 { 2020 if (this->type_ != NULL) 2021 return Type::traverse(this->type_, traverse); 2022 return TRAVERSE_CONTINUE; 2023 } 2024 2025 // Get the type. 2026 2027 Type* 2028 Boolean_expression::do_type() 2029 { 2030 if (this->type_ == NULL) 2031 this->type_ = Type::make_boolean_type(); 2032 return this->type_; 2033 } 2034 2035 // Set the type from the context. 2036 2037 void 2038 Boolean_expression::do_determine_type(const Type_context* context) 2039 { 2040 if (this->type_ != NULL && !this->type_->is_abstract()) 2041 ; 2042 else if (context->type != NULL && context->type->is_boolean_type()) 2043 this->type_ = context->type; 2044 else if (!context->may_be_abstract) 2045 this->type_ = Type::lookup_bool_type(); 2046 } 2047 2048 // Export a boolean constant. 2049 2050 void 2051 Boolean_expression::do_export(Export_function_body* efb) const 2052 { 2053 bool exported_type = Expression::export_constant_type(efb, this->type_); 2054 efb->write_c_string(this->val_ ? "$true" : "$false"); 2055 Expression::finish_export_constant_type(efb, exported_type); 2056 } 2057 2058 // Import a boolean constant. 2059 2060 Expression* 2061 Boolean_expression::do_import(Import_expression* imp, Location loc) 2062 { 2063 if (imp->version() >= EXPORT_FORMAT_V3) 2064 imp->require_c_string("$"); 2065 if (imp->peek_char() == 't') 2066 { 2067 imp->require_c_string("true"); 2068 return Expression::make_boolean(true, loc); 2069 } 2070 else 2071 { 2072 imp->require_c_string("false"); 2073 return Expression::make_boolean(false, loc); 2074 } 2075 } 2076 2077 // Make a boolean expression. 2078 2079 Expression* 2080 Expression::make_boolean(bool val, Location location) 2081 { 2082 return new Boolean_expression(val, location); 2083 } 2084 2085 // Class String_expression. 2086 2087 // Traverse a string expression. We just need to traverse the type 2088 // if there is one. 2089 2090 int 2091 String_expression::do_traverse(Traverse* traverse) 2092 { 2093 if (this->type_ != NULL) 2094 return Type::traverse(this->type_, traverse); 2095 return TRAVERSE_CONTINUE; 2096 } 2097 2098 // Get the type. 2099 2100 Type* 2101 String_expression::do_type() 2102 { 2103 if (this->type_ == NULL) 2104 this->type_ = Type::make_string_type(); 2105 return this->type_; 2106 } 2107 2108 // Set the type from the context. 2109 2110 void 2111 String_expression::do_determine_type(const Type_context* context) 2112 { 2113 if (this->type_ != NULL && !this->type_->is_abstract()) 2114 ; 2115 else if (context->type != NULL && context->type->is_string_type()) 2116 this->type_ = context->type; 2117 else if (!context->may_be_abstract) 2118 this->type_ = Type::lookup_string_type(); 2119 } 2120 2121 // Build a string constant. 2122 2123 Bexpression* 2124 String_expression::do_get_backend(Translate_context* context) 2125 { 2126 Gogo* gogo = context->gogo(); 2127 Btype* btype = Type::make_string_type()->get_backend(gogo); 2128 2129 Location loc = this->location(); 2130 std::vector<Bexpression*> init(2); 2131 2132 if (this->val_.size() == 0) 2133 init[0] = gogo->backend()->nil_pointer_expression(); 2134 else 2135 { 2136 Bexpression* str_cst = 2137 gogo->backend()->string_constant_expression(this->val_); 2138 init[0] = gogo->backend()->address_expression(str_cst, loc); 2139 } 2140 2141 Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); 2142 mpz_t lenval; 2143 mpz_init_set_ui(lenval, this->val_.length()); 2144 init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); 2145 mpz_clear(lenval); 2146 2147 return gogo->backend()->constructor_expression(btype, init, loc); 2148 } 2149 2150 // Write string literal to string dump. 2151 2152 void 2153 String_expression::export_string(String_dump* exp, 2154 const String_expression* str) 2155 { 2156 std::string s; 2157 s.reserve(str->val_.length() * 4 + 2); 2158 s += '"'; 2159 for (std::string::const_iterator p = str->val_.begin(); 2160 p != str->val_.end(); 2161 ++p) 2162 { 2163 if (*p == '\\' || *p == '"') 2164 { 2165 s += '\\'; 2166 s += *p; 2167 } 2168 else if (*p >= 0x20 && *p < 0x7f) 2169 s += *p; 2170 else if (*p == '\n') 2171 s += "\\n"; 2172 else if (*p == '\t') 2173 s += "\\t"; 2174 else 2175 { 2176 s += "\\x"; 2177 unsigned char c = *p; 2178 unsigned int dig = c >> 4; 2179 s += dig < 10 ? '0' + dig : 'A' + dig - 10; 2180 dig = c & 0xf; 2181 s += dig < 10 ? '0' + dig : 'A' + dig - 10; 2182 } 2183 } 2184 s += '"'; 2185 exp->write_string(s); 2186 } 2187 2188 // Export a string expression. 2189 2190 void 2191 String_expression::do_export(Export_function_body* efb) const 2192 { 2193 bool exported_type = Expression::export_constant_type(efb, this->type_); 2194 String_expression::export_string(efb, this); 2195 Expression::finish_export_constant_type(efb, exported_type); 2196 } 2197 2198 // Import a string expression. 2199 2200 Expression* 2201 String_expression::do_import(Import_expression* imp, Location loc) 2202 { 2203 imp->require_c_string("\""); 2204 std::string val; 2205 while (true) 2206 { 2207 int c = imp->get_char(); 2208 if (c == '"' || c == -1) 2209 break; 2210 if (c != '\\') 2211 val += static_cast<char>(c); 2212 else 2213 { 2214 c = imp->get_char(); 2215 if (c == '\\' || c == '"') 2216 val += static_cast<char>(c); 2217 else if (c == 'n') 2218 val += '\n'; 2219 else if (c == 't') 2220 val += '\t'; 2221 else if (c == 'x') 2222 { 2223 c = imp->get_char(); 2224 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; 2225 c = imp->get_char(); 2226 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; 2227 char v = (vh << 4) | vl; 2228 val += v; 2229 } 2230 else 2231 { 2232 go_error_at(imp->location(), "bad string constant"); 2233 return Expression::make_error(loc); 2234 } 2235 } 2236 } 2237 return Expression::make_string(val, loc); 2238 } 2239 2240 // Ast dump for string expression. 2241 2242 void 2243 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 2244 { 2245 String_expression::export_string(ast_dump_context, this); 2246 } 2247 2248 // Make a string expression with abstract string type (common case). 2249 2250 Expression* 2251 Expression::make_string(const std::string& val, Location location) 2252 { 2253 return new String_expression(val, NULL, location); 2254 } 2255 2256 // Make a string expression with a specific string type. 2257 2258 Expression* 2259 Expression::make_string_typed(const std::string& val, Type* type, Location location) 2260 { 2261 return new String_expression(val, type, location); 2262 } 2263 2264 // An expression that evaluates to some characteristic of a string. 2265 // This is used when indexing, bound-checking, or nil checking a string. 2266 2267 class String_info_expression : public Expression 2268 { 2269 public: 2270 String_info_expression(Expression* string, String_info string_info, 2271 Location location) 2272 : Expression(EXPRESSION_STRING_INFO, location), 2273 string_(string), string_info_(string_info) 2274 { } 2275 2276 protected: 2277 Type* 2278 do_type(); 2279 2280 void 2281 do_determine_type(const Type_context*) 2282 { go_unreachable(); } 2283 2284 Expression* 2285 do_copy() 2286 { 2287 return new String_info_expression(this->string_->copy(), this->string_info_, 2288 this->location()); 2289 } 2290 2291 Bexpression* 2292 do_get_backend(Translate_context* context); 2293 2294 void 2295 do_dump_expression(Ast_dump_context*) const; 2296 2297 void 2298 do_issue_nil_check() 2299 { this->string_->issue_nil_check(); } 2300 2301 private: 2302 // The string for which we are getting information. 2303 Expression* string_; 2304 // What information we want. 2305 String_info string_info_; 2306 }; 2307 2308 // Return the type of the string info. 2309 2310 Type* 2311 String_info_expression::do_type() 2312 { 2313 switch (this->string_info_) 2314 { 2315 case STRING_INFO_DATA: 2316 { 2317 Type* byte_type = Type::lookup_integer_type("uint8"); 2318 return Type::make_pointer_type(byte_type); 2319 } 2320 case STRING_INFO_LENGTH: 2321 return Type::lookup_integer_type("int"); 2322 default: 2323 go_unreachable(); 2324 } 2325 } 2326 2327 // Return string information in GENERIC. 2328 2329 Bexpression* 2330 String_info_expression::do_get_backend(Translate_context* context) 2331 { 2332 Gogo* gogo = context->gogo(); 2333 2334 Bexpression* bstring = this->string_->get_backend(context); 2335 switch (this->string_info_) 2336 { 2337 case STRING_INFO_DATA: 2338 case STRING_INFO_LENGTH: 2339 return gogo->backend()->struct_field_expression(bstring, 2340 this->string_info_, 2341 this->location()); 2342 break; 2343 default: 2344 go_unreachable(); 2345 } 2346 } 2347 2348 // Dump ast representation for a type info expression. 2349 2350 void 2351 String_info_expression::do_dump_expression( 2352 Ast_dump_context* ast_dump_context) const 2353 { 2354 ast_dump_context->ostream() << "stringinfo("; 2355 this->string_->dump_expression(ast_dump_context); 2356 ast_dump_context->ostream() << ","; 2357 ast_dump_context->ostream() << 2358 (this->string_info_ == STRING_INFO_DATA ? "data" 2359 : this->string_info_ == STRING_INFO_LENGTH ? "length" 2360 : "unknown"); 2361 ast_dump_context->ostream() << ")"; 2362 } 2363 2364 // Make a string info expression. 2365 2366 Expression* 2367 Expression::make_string_info(Expression* string, String_info string_info, 2368 Location location) 2369 { 2370 return new String_info_expression(string, string_info, location); 2371 } 2372 2373 // An expression that represents an string value: a struct with value pointer 2374 // and length fields. 2375 2376 class String_value_expression : public Expression 2377 { 2378 public: 2379 String_value_expression(Expression* valptr, Expression* len, Location location) 2380 : Expression(EXPRESSION_STRING_VALUE, location), 2381 valptr_(valptr), len_(len) 2382 { } 2383 2384 protected: 2385 int 2386 do_traverse(Traverse*); 2387 2388 Type* 2389 do_type() 2390 { return Type::make_string_type(); } 2391 2392 void 2393 do_determine_type(const Type_context*) 2394 { go_unreachable(); } 2395 2396 Expression* 2397 do_copy() 2398 { 2399 return new String_value_expression(this->valptr_->copy(), 2400 this->len_->copy(), 2401 this->location()); 2402 } 2403 2404 Bexpression* 2405 do_get_backend(Translate_context* context); 2406 2407 void 2408 do_dump_expression(Ast_dump_context*) const; 2409 2410 private: 2411 // The value pointer. 2412 Expression* valptr_; 2413 // The length. 2414 Expression* len_; 2415 }; 2416 2417 int 2418 String_value_expression::do_traverse(Traverse* traverse) 2419 { 2420 if (Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT 2421 || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT) 2422 return TRAVERSE_EXIT; 2423 return TRAVERSE_CONTINUE; 2424 } 2425 2426 Bexpression* 2427 String_value_expression::do_get_backend(Translate_context* context) 2428 { 2429 std::vector<Bexpression*> vals(2); 2430 vals[0] = this->valptr_->get_backend(context); 2431 vals[1] = this->len_->get_backend(context); 2432 2433 Gogo* gogo = context->gogo(); 2434 Btype* btype = Type::make_string_type()->get_backend(gogo); 2435 return gogo->backend()->constructor_expression(btype, vals, this->location()); 2436 } 2437 2438 void 2439 String_value_expression::do_dump_expression( 2440 Ast_dump_context* ast_dump_context) const 2441 { 2442 ast_dump_context->ostream() << "stringvalue("; 2443 ast_dump_context->ostream() << "value: "; 2444 this->valptr_->dump_expression(ast_dump_context); 2445 ast_dump_context->ostream() << ", length: "; 2446 this->len_->dump_expression(ast_dump_context); 2447 ast_dump_context->ostream() << ")"; 2448 } 2449 2450 Expression* 2451 Expression::make_string_value(Expression* valptr, Expression* len, 2452 Location location) 2453 { 2454 return new String_value_expression(valptr, len, location); 2455 } 2456 2457 // Make an integer expression. 2458 2459 class Integer_expression : public Expression 2460 { 2461 public: 2462 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, 2463 Location location) 2464 : Expression(EXPRESSION_INTEGER, location), 2465 type_(type), is_character_constant_(is_character_constant) 2466 { mpz_init_set(this->val_, *val); } 2467 2468 static Expression* 2469 do_import(Import_expression*, Location); 2470 2471 // Write VAL to string dump. 2472 static void 2473 export_integer(String_dump* exp, const mpz_t val); 2474 2475 // Write VAL to dump context. 2476 static void 2477 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); 2478 2479 protected: 2480 int 2481 do_traverse(Traverse*); 2482 2483 bool 2484 do_is_constant() const 2485 { return true; } 2486 2487 bool 2488 do_is_zero_value() const 2489 { return mpz_sgn(this->val_) == 0; } 2490 2491 bool 2492 do_is_static_initializer() const 2493 { return true; } 2494 2495 bool 2496 do_numeric_constant_value(Numeric_constant* nc) const; 2497 2498 Type* 2499 do_type(); 2500 2501 void 2502 do_determine_type(const Type_context* context); 2503 2504 void 2505 do_check_types(Gogo*); 2506 2507 Bexpression* 2508 do_get_backend(Translate_context*); 2509 2510 Expression* 2511 do_copy() 2512 { 2513 if (this->is_character_constant_) 2514 return Expression::make_character(&this->val_, 2515 (this->type_ == NULL 2516 ? NULL 2517 : this->type_->copy_expressions()), 2518 this->location()); 2519 else 2520 return Expression::make_integer_z(&this->val_, 2521 (this->type_ == NULL 2522 ? NULL 2523 : this->type_->copy_expressions()), 2524 this->location()); 2525 } 2526 2527 int 2528 do_inlining_cost() const 2529 { return 1; } 2530 2531 void 2532 do_export(Export_function_body*) const; 2533 2534 void 2535 do_dump_expression(Ast_dump_context*) const; 2536 2537 private: 2538 // The integer value. 2539 mpz_t val_; 2540 // The type so far. 2541 Type* type_; 2542 // Whether this is a character constant. 2543 bool is_character_constant_; 2544 }; 2545 2546 // Traverse an integer expression. We just need to traverse the type 2547 // if there is one. 2548 2549 int 2550 Integer_expression::do_traverse(Traverse* traverse) 2551 { 2552 if (this->type_ != NULL) 2553 return Type::traverse(this->type_, traverse); 2554 return TRAVERSE_CONTINUE; 2555 } 2556 2557 // Return a numeric constant for this expression. We have to mark 2558 // this as a character when appropriate. 2559 2560 bool 2561 Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const 2562 { 2563 if (this->is_character_constant_) 2564 nc->set_rune(this->type_, this->val_); 2565 else 2566 nc->set_int(this->type_, this->val_); 2567 return true; 2568 } 2569 2570 // Return the current type. If we haven't set the type yet, we return 2571 // an abstract integer type. 2572 2573 Type* 2574 Integer_expression::do_type() 2575 { 2576 if (this->type_ == NULL) 2577 { 2578 if (this->is_character_constant_) 2579 this->type_ = Type::make_abstract_character_type(); 2580 else 2581 this->type_ = Type::make_abstract_integer_type(); 2582 } 2583 return this->type_; 2584 } 2585 2586 // Set the type of the integer value. Here we may switch from an 2587 // abstract type to a real type. 2588 2589 void 2590 Integer_expression::do_determine_type(const Type_context* context) 2591 { 2592 if (this->type_ != NULL && !this->type_->is_abstract()) 2593 ; 2594 else if (context->type != NULL && context->type->is_numeric_type()) 2595 this->type_ = context->type; 2596 else if (!context->may_be_abstract) 2597 { 2598 if (this->is_character_constant_) 2599 this->type_ = Type::lookup_integer_type("int32"); 2600 else 2601 this->type_ = Type::lookup_integer_type("int"); 2602 } 2603 } 2604 2605 // Check the type of an integer constant. 2606 2607 void 2608 Integer_expression::do_check_types(Gogo*) 2609 { 2610 Type* type = this->type_; 2611 if (type == NULL) 2612 return; 2613 Numeric_constant nc; 2614 if (this->is_character_constant_) 2615 nc.set_rune(NULL, this->val_); 2616 else 2617 nc.set_int(NULL, this->val_); 2618 if (!nc.set_type(type, true, this->location())) 2619 this->set_is_error(); 2620 } 2621 2622 // Get the backend representation for an integer constant. 2623 2624 Bexpression* 2625 Integer_expression::do_get_backend(Translate_context* context) 2626 { 2627 if (this->is_error_expression() 2628 || (this->type_ != NULL && this->type_->is_error_type())) 2629 { 2630 go_assert(saw_errors()); 2631 return context->gogo()->backend()->error_expression(); 2632 } 2633 2634 Type* resolved_type = NULL; 2635 if (this->type_ != NULL && !this->type_->is_abstract()) 2636 resolved_type = this->type_; 2637 else if (this->type_ != NULL && this->type_->float_type() != NULL) 2638 { 2639 // We are converting to an abstract floating point type. 2640 resolved_type = Type::lookup_float_type("float64"); 2641 } 2642 else if (this->type_ != NULL && this->type_->complex_type() != NULL) 2643 { 2644 // We are converting to an abstract complex type. 2645 resolved_type = Type::lookup_complex_type("complex128"); 2646 } 2647 else 2648 { 2649 // If we still have an abstract type here, then this is being 2650 // used in a constant expression which didn't get reduced for 2651 // some reason. Use a type which will fit the value. We use <, 2652 // not <=, because we need an extra bit for the sign bit. 2653 int bits = mpz_sizeinbase(this->val_, 2); 2654 Type* int_type = Type::lookup_integer_type("int"); 2655 if (bits < int_type->integer_type()->bits()) 2656 resolved_type = int_type; 2657 else if (bits < 64) 2658 resolved_type = Type::lookup_integer_type("int64"); 2659 else 2660 { 2661 if (!saw_errors()) 2662 go_error_at(this->location(), 2663 "unknown type for large integer constant"); 2664 return context->gogo()->backend()->error_expression(); 2665 } 2666 } 2667 Numeric_constant nc; 2668 nc.set_int(resolved_type, this->val_); 2669 return Expression::backend_numeric_constant_expression(context, &nc); 2670 } 2671 2672 // Write VAL to export data. 2673 2674 void 2675 Integer_expression::export_integer(String_dump* exp, const mpz_t val) 2676 { 2677 char* s = mpz_get_str(NULL, 10, val); 2678 exp->write_c_string(s); 2679 free(s); 2680 } 2681 2682 // Export an integer in a constant expression. 2683 2684 void 2685 Integer_expression::do_export(Export_function_body* efb) const 2686 { 2687 bool exported_type = Expression::export_constant_type(efb, this->type_); 2688 2689 Integer_expression::export_integer(efb, this->val_); 2690 if (this->is_character_constant_) 2691 efb->write_c_string("'"); 2692 // A trailing space lets us reliably identify the end of the number. 2693 efb->write_c_string(" "); 2694 2695 Expression::finish_export_constant_type(efb, exported_type); 2696 } 2697 2698 // Import an integer, floating point, or complex value. This handles 2699 // all these types because they all start with digits. 2700 2701 Expression* 2702 Integer_expression::do_import(Import_expression* imp, Location loc) 2703 { 2704 std::string num = imp->read_identifier(); 2705 imp->require_c_string(" "); 2706 if (!num.empty() && num[num.length() - 1] == 'i') 2707 { 2708 mpfr_t real; 2709 size_t plus_pos = num.find('+', 1); 2710 size_t minus_pos = num.find('-', 1); 2711 size_t pos; 2712 if (plus_pos == std::string::npos) 2713 pos = minus_pos; 2714 else if (minus_pos == std::string::npos) 2715 pos = plus_pos; 2716 else 2717 { 2718 go_error_at(imp->location(), "bad number in import data: %qs", 2719 num.c_str()); 2720 return Expression::make_error(loc); 2721 } 2722 if (pos == std::string::npos) 2723 mpfr_init_set_ui(real, 0, MPFR_RNDN); 2724 else 2725 { 2726 std::string real_str = num.substr(0, pos); 2727 if (mpfr_init_set_str(real, real_str.c_str(), 10, MPFR_RNDN) != 0) 2728 { 2729 go_error_at(imp->location(), "bad number in import data: %qs", 2730 real_str.c_str()); 2731 return Expression::make_error(loc); 2732 } 2733 } 2734 2735 std::string imag_str; 2736 if (pos == std::string::npos) 2737 imag_str = num; 2738 else 2739 imag_str = num.substr(pos); 2740 imag_str = imag_str.substr(0, imag_str.size() - 1); 2741 mpfr_t imag; 2742 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, MPFR_RNDN) != 0) 2743 { 2744 go_error_at(imp->location(), "bad number in import data: %qs", 2745 imag_str.c_str()); 2746 return Expression::make_error(loc); 2747 } 2748 mpc_t cval; 2749 mpc_init2(cval, mpc_precision); 2750 mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); 2751 mpfr_clear(real); 2752 mpfr_clear(imag); 2753 Expression* ret = Expression::make_complex(&cval, NULL, loc); 2754 mpc_clear(cval); 2755 return ret; 2756 } 2757 else if (num.find('.') == std::string::npos 2758 && num.find('E') == std::string::npos) 2759 { 2760 bool is_character_constant = (!num.empty() 2761 && num[num.length() - 1] == '\''); 2762 if (is_character_constant) 2763 num = num.substr(0, num.length() - 1); 2764 mpz_t val; 2765 if (mpz_init_set_str(val, num.c_str(), 10) != 0) 2766 { 2767 go_error_at(imp->location(), "bad number in import data: %qs", 2768 num.c_str()); 2769 return Expression::make_error(loc); 2770 } 2771 Expression* ret; 2772 if (is_character_constant) 2773 ret = Expression::make_character(&val, NULL, loc); 2774 else 2775 ret = Expression::make_integer_z(&val, NULL, loc); 2776 mpz_clear(val); 2777 return ret; 2778 } 2779 else 2780 { 2781 mpfr_t val; 2782 if (mpfr_init_set_str(val, num.c_str(), 10, MPFR_RNDN) != 0) 2783 { 2784 go_error_at(imp->location(), "bad number in import data: %qs", 2785 num.c_str()); 2786 return Expression::make_error(loc); 2787 } 2788 Expression* ret = Expression::make_float(&val, NULL, loc); 2789 mpfr_clear(val); 2790 return ret; 2791 } 2792 } 2793 // Ast dump for integer expression. 2794 2795 void 2796 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 2797 { 2798 if (this->is_character_constant_) 2799 ast_dump_context->ostream() << '\''; 2800 Integer_expression::export_integer(ast_dump_context, this->val_); 2801 if (this->is_character_constant_) 2802 ast_dump_context->ostream() << '\''; 2803 } 2804 2805 // Build a new integer value from a multi-precision integer. 2806 2807 Expression* 2808 Expression::make_integer_z(const mpz_t* val, Type* type, Location location) 2809 { 2810 return new Integer_expression(val, type, false, location); 2811 } 2812 2813 // Build a new integer value from an unsigned long. 2814 2815 Expression* 2816 Expression::make_integer_ul(unsigned long val, Type *type, Location location) 2817 { 2818 mpz_t zval; 2819 mpz_init_set_ui(zval, val); 2820 Expression* ret = Expression::make_integer_z(&zval, type, location); 2821 mpz_clear(zval); 2822 return ret; 2823 } 2824 2825 // Build a new integer value from a signed long. 2826 2827 Expression* 2828 Expression::make_integer_sl(long val, Type *type, Location location) 2829 { 2830 mpz_t zval; 2831 mpz_init_set_si(zval, val); 2832 Expression* ret = Expression::make_integer_z(&zval, type, location); 2833 mpz_clear(zval); 2834 return ret; 2835 } 2836 2837 // Store an int64_t in an uninitialized mpz_t. 2838 2839 static void 2840 set_mpz_from_int64(mpz_t* zval, int64_t val) 2841 { 2842 if (val >= 0) 2843 { 2844 unsigned long ul = static_cast<unsigned long>(val); 2845 if (static_cast<int64_t>(ul) == val) 2846 { 2847 mpz_init_set_ui(*zval, ul); 2848 return; 2849 } 2850 } 2851 uint64_t uv; 2852 if (val >= 0) 2853 uv = static_cast<uint64_t>(val); 2854 else 2855 uv = static_cast<uint64_t>(- val); 2856 unsigned long ul = uv & 0xffffffffUL; 2857 mpz_init_set_ui(*zval, ul); 2858 mpz_t hval; 2859 mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); 2860 mpz_mul_2exp(hval, hval, 32); 2861 mpz_add(*zval, *zval, hval); 2862 mpz_clear(hval); 2863 if (val < 0) 2864 mpz_neg(*zval, *zval); 2865 } 2866 2867 // Build a new integer value from an int64_t. 2868 2869 Expression* 2870 Expression::make_integer_int64(int64_t val, Type* type, Location location) 2871 { 2872 mpz_t zval; 2873 set_mpz_from_int64(&zval, val); 2874 Expression* ret = Expression::make_integer_z(&zval, type, location); 2875 mpz_clear(zval); 2876 return ret; 2877 } 2878 2879 // Build a new character constant value. 2880 2881 Expression* 2882 Expression::make_character(const mpz_t* val, Type* type, Location location) 2883 { 2884 return new Integer_expression(val, type, true, location); 2885 } 2886 2887 // Floats. 2888 2889 class Float_expression : public Expression 2890 { 2891 public: 2892 Float_expression(const mpfr_t* val, Type* type, Location location) 2893 : Expression(EXPRESSION_FLOAT, location), 2894 type_(type) 2895 { 2896 mpfr_init_set(this->val_, *val, MPFR_RNDN); 2897 } 2898 2899 // Write VAL to export data. 2900 static void 2901 export_float(String_dump* exp, const mpfr_t val); 2902 2903 // Write VAL to dump file. 2904 static void 2905 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); 2906 2907 protected: 2908 int 2909 do_traverse(Traverse*); 2910 2911 bool 2912 do_is_constant() const 2913 { return true; } 2914 2915 bool 2916 do_is_zero_value() const 2917 { 2918 return mpfr_zero_p(this->val_) != 0 2919 && mpfr_signbit(this->val_) == 0; 2920 } 2921 2922 bool 2923 do_is_static_initializer() const 2924 { return true; } 2925 2926 bool 2927 do_numeric_constant_value(Numeric_constant* nc) const 2928 { 2929 nc->set_float(this->type_, this->val_); 2930 return true; 2931 } 2932 2933 Type* 2934 do_type(); 2935 2936 void 2937 do_determine_type(const Type_context*); 2938 2939 void 2940 do_check_types(Gogo*); 2941 2942 Expression* 2943 do_copy() 2944 { return Expression::make_float(&this->val_, 2945 (this->type_ == NULL 2946 ? NULL 2947 : this->type_->copy_expressions()), 2948 this->location()); } 2949 2950 Bexpression* 2951 do_get_backend(Translate_context*); 2952 2953 int 2954 do_inlining_cost() const 2955 { return 1; } 2956 2957 void 2958 do_export(Export_function_body*) const; 2959 2960 void 2961 do_dump_expression(Ast_dump_context*) const; 2962 2963 private: 2964 // The floating point value. 2965 mpfr_t val_; 2966 // The type so far. 2967 Type* type_; 2968 }; 2969 2970 // Traverse a float expression. We just need to traverse the type if 2971 // there is one. 2972 2973 int 2974 Float_expression::do_traverse(Traverse* traverse) 2975 { 2976 if (this->type_ != NULL) 2977 return Type::traverse(this->type_, traverse); 2978 return TRAVERSE_CONTINUE; 2979 } 2980 2981 // Return the current type. If we haven't set the type yet, we return 2982 // an abstract float type. 2983 2984 Type* 2985 Float_expression::do_type() 2986 { 2987 if (this->type_ == NULL) 2988 this->type_ = Type::make_abstract_float_type(); 2989 return this->type_; 2990 } 2991 2992 // Set the type of the float value. Here we may switch from an 2993 // abstract type to a real type. 2994 2995 void 2996 Float_expression::do_determine_type(const Type_context* context) 2997 { 2998 if (this->type_ != NULL && !this->type_->is_abstract()) 2999 ; 3000 else if (context->type != NULL 3001 && (context->type->integer_type() != NULL 3002 || context->type->float_type() != NULL 3003 || context->type->complex_type() != NULL)) 3004 this->type_ = context->type; 3005 else if (!context->may_be_abstract) 3006 this->type_ = Type::lookup_float_type("float64"); 3007 } 3008 3009 // Check the type of a float value. 3010 3011 void 3012 Float_expression::do_check_types(Gogo*) 3013 { 3014 Type* type = this->type_; 3015 if (type == NULL) 3016 return; 3017 Numeric_constant nc; 3018 nc.set_float(NULL, this->val_); 3019 if (!nc.set_type(this->type_, true, this->location())) 3020 this->set_is_error(); 3021 } 3022 3023 // Get the backend representation for a float constant. 3024 3025 Bexpression* 3026 Float_expression::do_get_backend(Translate_context* context) 3027 { 3028 if (this->is_error_expression() 3029 || (this->type_ != NULL && this->type_->is_error_type())) 3030 { 3031 go_assert(saw_errors()); 3032 return context->gogo()->backend()->error_expression(); 3033 } 3034 3035 Type* resolved_type; 3036 if (this->type_ != NULL && !this->type_->is_abstract()) 3037 resolved_type = this->type_; 3038 else if (this->type_ != NULL && this->type_->integer_type() != NULL) 3039 { 3040 // We have an abstract integer type. We just hope for the best. 3041 resolved_type = Type::lookup_integer_type("int"); 3042 } 3043 else if (this->type_ != NULL && this->type_->complex_type() != NULL) 3044 { 3045 // We are converting to an abstract complex type. 3046 resolved_type = Type::lookup_complex_type("complex128"); 3047 } 3048 else 3049 { 3050 // If we still have an abstract type here, then this is being 3051 // used in a constant expression which didn't get reduced. We 3052 // just use float64 and hope for the best. 3053 resolved_type = Type::lookup_float_type("float64"); 3054 } 3055 3056 Numeric_constant nc; 3057 nc.set_float(resolved_type, this->val_); 3058 return Expression::backend_numeric_constant_expression(context, &nc); 3059 } 3060 3061 // Write a floating point number to a string dump. 3062 3063 void 3064 Float_expression::export_float(String_dump *exp, const mpfr_t val) 3065 { 3066 mpfr_exp_t exponent; 3067 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, MPFR_RNDN); 3068 if (*s == '-') 3069 exp->write_c_string("-"); 3070 exp->write_c_string("0."); 3071 exp->write_c_string(*s == '-' ? s + 1 : s); 3072 mpfr_free_str(s); 3073 char buf[30]; 3074 snprintf(buf, sizeof buf, "E%ld", exponent); 3075 exp->write_c_string(buf); 3076 } 3077 3078 // Export a floating point number in a constant expression. 3079 3080 void 3081 Float_expression::do_export(Export_function_body* efb) const 3082 { 3083 bool exported_type = Expression::export_constant_type(efb, this->type_); 3084 3085 Float_expression::export_float(efb, this->val_); 3086 // A trailing space lets us reliably identify the end of the number. 3087 efb->write_c_string(" "); 3088 3089 Expression::finish_export_constant_type(efb, exported_type); 3090 } 3091 3092 // Dump a floating point number to the dump file. 3093 3094 void 3095 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 3096 { 3097 Float_expression::export_float(ast_dump_context, this->val_); 3098 } 3099 3100 // Make a float expression. 3101 3102 Expression* 3103 Expression::make_float(const mpfr_t* val, Type* type, Location location) 3104 { 3105 return new Float_expression(val, type, location); 3106 } 3107 3108 // Complex numbers. 3109 3110 class Complex_expression : public Expression 3111 { 3112 public: 3113 Complex_expression(const mpc_t* val, Type* type, Location location) 3114 : Expression(EXPRESSION_COMPLEX, location), 3115 type_(type) 3116 { 3117 mpc_init2(this->val_, mpc_precision); 3118 mpc_set(this->val_, *val, MPC_RNDNN); 3119 } 3120 3121 // Write VAL to string dump. 3122 static void 3123 export_complex(String_dump* exp, const mpc_t val); 3124 3125 // Write REAL/IMAG to dump context. 3126 static void 3127 dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); 3128 3129 protected: 3130 int 3131 do_traverse(Traverse*); 3132 3133 bool 3134 do_is_constant() const 3135 { return true; } 3136 3137 bool 3138 do_is_zero_value() const 3139 { 3140 return mpfr_zero_p(mpc_realref(this->val_)) != 0 3141 && mpfr_signbit(mpc_realref(this->val_)) == 0 3142 && mpfr_zero_p(mpc_imagref(this->val_)) != 0 3143 && mpfr_signbit(mpc_imagref(this->val_)) == 0; 3144 } 3145 3146 bool 3147 do_is_static_initializer() const 3148 { return true; } 3149 3150 bool 3151 do_numeric_constant_value(Numeric_constant* nc) const 3152 { 3153 nc->set_complex(this->type_, this->val_); 3154 return true; 3155 } 3156 3157 Type* 3158 do_type(); 3159 3160 void 3161 do_determine_type(const Type_context*); 3162 3163 void 3164 do_check_types(Gogo*); 3165 3166 Expression* 3167 do_copy() 3168 { 3169 return Expression::make_complex(&this->val_, 3170 (this->type_ == NULL 3171 ? NULL 3172 : this->type_->copy_expressions()), 3173 this->location()); 3174 } 3175 3176 Bexpression* 3177 do_get_backend(Translate_context*); 3178 3179 int 3180 do_inlining_cost() const 3181 { return 2; } 3182 3183 void 3184 do_export(Export_function_body*) const; 3185 3186 void 3187 do_dump_expression(Ast_dump_context*) const; 3188 3189 private: 3190 // The complex value. 3191 mpc_t val_; 3192 // The type if known. 3193 Type* type_; 3194 }; 3195 3196 // Traverse a complex expression. We just need to traverse the type 3197 // if there is one. 3198 3199 int 3200 Complex_expression::do_traverse(Traverse* traverse) 3201 { 3202 if (this->type_ != NULL) 3203 return Type::traverse(this->type_, traverse); 3204 return TRAVERSE_CONTINUE; 3205 } 3206 3207 // Return the current type. If we haven't set the type yet, we return 3208 // an abstract complex type. 3209 3210 Type* 3211 Complex_expression::do_type() 3212 { 3213 if (this->type_ == NULL) 3214 this->type_ = Type::make_abstract_complex_type(); 3215 return this->type_; 3216 } 3217 3218 // Set the type of the complex value. Here we may switch from an 3219 // abstract type to a real type. 3220 3221 void 3222 Complex_expression::do_determine_type(const Type_context* context) 3223 { 3224 if (this->type_ != NULL && !this->type_->is_abstract()) 3225 ; 3226 else if (context->type != NULL && context->type->is_numeric_type()) 3227 this->type_ = context->type; 3228 else if (!context->may_be_abstract) 3229 this->type_ = Type::lookup_complex_type("complex128"); 3230 } 3231 3232 // Check the type of a complex value. 3233 3234 void 3235 Complex_expression::do_check_types(Gogo*) 3236 { 3237 Type* type = this->type_; 3238 if (type == NULL) 3239 return; 3240 Numeric_constant nc; 3241 nc.set_complex(NULL, this->val_); 3242 if (!nc.set_type(this->type_, true, this->location())) 3243 this->set_is_error(); 3244 } 3245 3246 // Get the backend representation for a complex constant. 3247 3248 Bexpression* 3249 Complex_expression::do_get_backend(Translate_context* context) 3250 { 3251 if (this->is_error_expression() 3252 || (this->type_ != NULL && this->type_->is_error_type())) 3253 { 3254 go_assert(saw_errors()); 3255 return context->gogo()->backend()->error_expression(); 3256 } 3257 3258 Type* resolved_type; 3259 if (this->type_ != NULL && !this->type_->is_abstract()) 3260 resolved_type = this->type_; 3261 else if (this->type_ != NULL && this->type_->integer_type() != NULL) 3262 { 3263 // We are converting to an abstract integer type. 3264 resolved_type = Type::lookup_integer_type("int"); 3265 } 3266 else if (this->type_ != NULL && this->type_->float_type() != NULL) 3267 { 3268 // We are converting to an abstract float type. 3269 resolved_type = Type::lookup_float_type("float64"); 3270 } 3271 else 3272 { 3273 // If we still have an abstract type here, this is being 3274 // used in a constant expression which didn't get reduced. We 3275 // just use complex128 and hope for the best. 3276 resolved_type = Type::lookup_complex_type("complex128"); 3277 } 3278 3279 Numeric_constant nc; 3280 nc.set_complex(resolved_type, this->val_); 3281 return Expression::backend_numeric_constant_expression(context, &nc); 3282 } 3283 3284 // Write REAL/IMAG to export data. 3285 3286 void 3287 Complex_expression::export_complex(String_dump* exp, const mpc_t val) 3288 { 3289 if (!mpfr_zero_p(mpc_realref(val))) 3290 { 3291 Float_expression::export_float(exp, mpc_realref(val)); 3292 if (mpfr_sgn(mpc_imagref(val)) >= 0) 3293 exp->write_c_string("+"); 3294 } 3295 Float_expression::export_float(exp, mpc_imagref(val)); 3296 exp->write_c_string("i"); 3297 } 3298 3299 // Export a complex number in a constant expression. 3300 3301 void 3302 Complex_expression::do_export(Export_function_body* efb) const 3303 { 3304 bool exported_type = Expression::export_constant_type(efb, this->type_); 3305 3306 Complex_expression::export_complex(efb, this->val_); 3307 // A trailing space lets us reliably identify the end of the number. 3308 efb->write_c_string(" "); 3309 3310 Expression::finish_export_constant_type(efb, exported_type); 3311 } 3312 3313 // Dump a complex expression to the dump file. 3314 3315 void 3316 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 3317 { 3318 Complex_expression::export_complex(ast_dump_context, this->val_); 3319 } 3320 3321 // Make a complex expression. 3322 3323 Expression* 3324 Expression::make_complex(const mpc_t* val, Type* type, Location location) 3325 { 3326 return new Complex_expression(val, type, location); 3327 } 3328 3329 // Find a named object in an expression. 3330 3331 class Find_named_object : public Traverse 3332 { 3333 public: 3334 Find_named_object(Named_object* no) 3335 : Traverse(traverse_expressions), 3336 no_(no), found_(false) 3337 { } 3338 3339 // Whether we found the object. 3340 bool 3341 found() const 3342 { return this->found_; } 3343 3344 protected: 3345 int 3346 expression(Expression**); 3347 3348 private: 3349 // The object we are looking for. 3350 Named_object* no_; 3351 // Whether we found it. 3352 bool found_; 3353 }; 3354 3355 // Class Const_expression. 3356 3357 // Traversal. 3358 3359 int 3360 Const_expression::do_traverse(Traverse* traverse) 3361 { 3362 if (this->type_ != NULL) 3363 return Type::traverse(this->type_, traverse); 3364 return TRAVERSE_CONTINUE; 3365 } 3366 3367 // Whether this is the zero value. 3368 3369 bool 3370 Const_expression::do_is_zero_value() const 3371 { 3372 return this->constant_->const_value()->expr()->is_zero_value(); 3373 } 3374 3375 // Lower a constant expression. This is where we convert the 3376 // predeclared constant iota into an integer value. 3377 3378 Expression* 3379 Const_expression::do_lower(Gogo* gogo, Named_object*, 3380 Statement_inserter*, int iota_value) 3381 { 3382 if (this->constant_->const_value()->expr()->classification() 3383 == EXPRESSION_IOTA) 3384 { 3385 if (iota_value == -1) 3386 { 3387 go_error_at(this->location(), 3388 "iota is only defined in const declarations"); 3389 iota_value = 0; 3390 } 3391 return Expression::make_integer_ul(iota_value, NULL, this->location()); 3392 } 3393 3394 // Make sure that the constant itself has been lowered. 3395 gogo->lower_constant(this->constant_); 3396 3397 return this; 3398 } 3399 3400 // Return a numeric constant value. 3401 3402 bool 3403 Const_expression::do_numeric_constant_value(Numeric_constant* nc) const 3404 { 3405 if (this->seen_) 3406 return false; 3407 3408 Expression* e = this->constant_->const_value()->expr(); 3409 3410 this->seen_ = true; 3411 3412 bool r = e->numeric_constant_value(nc); 3413 3414 this->seen_ = false; 3415 3416 Type* ctype; 3417 if (this->type_ != NULL) 3418 ctype = this->type_; 3419 else 3420 ctype = this->constant_->const_value()->type(); 3421 if (r && ctype != NULL) 3422 { 3423 if (!nc->set_type(ctype, false, this->location())) 3424 return false; 3425 } 3426 3427 return r; 3428 } 3429 3430 bool 3431 Const_expression::do_string_constant_value(std::string* val) const 3432 { 3433 if (this->seen_) 3434 return false; 3435 3436 Expression* e = this->constant_->const_value()->expr(); 3437 3438 this->seen_ = true; 3439 bool ok = e->string_constant_value(val); 3440 this->seen_ = false; 3441 3442 return ok; 3443 } 3444 3445 bool 3446 Const_expression::do_boolean_constant_value(bool* val) const 3447 { 3448 if (this->seen_) 3449 return false; 3450 3451 Expression* e = this->constant_->const_value()->expr(); 3452 3453 this->seen_ = true; 3454 bool ok = e->boolean_constant_value(val); 3455 this->seen_ = false; 3456 3457 return ok; 3458 } 3459 3460 // Return the type of the const reference. 3461 3462 Type* 3463 Const_expression::do_type() 3464 { 3465 if (this->type_ != NULL) 3466 return this->type_; 3467 3468 Named_constant* nc = this->constant_->const_value(); 3469 3470 if (this->seen_ || nc->lowering()) 3471 { 3472 if (nc->type() == NULL || !nc->type()->is_error_type()) 3473 { 3474 Location loc = this->location(); 3475 if (!this->seen_) 3476 loc = nc->location(); 3477 go_error_at(loc, "constant refers to itself"); 3478 } 3479 this->set_is_error(); 3480 this->type_ = Type::make_error_type(); 3481 nc->set_type(this->type_); 3482 return this->type_; 3483 } 3484 3485 this->seen_ = true; 3486 3487 Type* ret = nc->type(); 3488 3489 if (ret != NULL) 3490 { 3491 this->seen_ = false; 3492 return ret; 3493 } 3494 3495 // During parsing, a named constant may have a NULL type, but we 3496 // must not return a NULL type here. 3497 ret = nc->expr()->type(); 3498 3499 this->seen_ = false; 3500 3501 if (ret->is_error_type()) 3502 nc->set_type(ret); 3503 3504 return ret; 3505 } 3506 3507 // Set the type of the const reference. 3508 3509 void 3510 Const_expression::do_determine_type(const Type_context* context) 3511 { 3512 Type* ctype = this->constant_->const_value()->type(); 3513 Type* cetype = (ctype != NULL 3514 ? ctype 3515 : this->constant_->const_value()->expr()->type()); 3516 if (ctype != NULL && !ctype->is_abstract()) 3517 ; 3518 else if (context->type != NULL 3519 && context->type->is_numeric_type() 3520 && cetype->is_numeric_type()) 3521 this->type_ = context->type; 3522 else if (context->type != NULL 3523 && context->type->is_string_type() 3524 && cetype->is_string_type()) 3525 this->type_ = context->type; 3526 else if (context->type != NULL 3527 && context->type->is_boolean_type() 3528 && cetype->is_boolean_type()) 3529 this->type_ = context->type; 3530 else if (!context->may_be_abstract) 3531 { 3532 if (cetype->is_abstract()) 3533 cetype = cetype->make_non_abstract_type(); 3534 this->type_ = cetype; 3535 } 3536 } 3537 3538 // Check for a loop in which the initializer of a constant refers to 3539 // the constant itself. 3540 3541 void 3542 Const_expression::check_for_init_loop() 3543 { 3544 if (this->type_ != NULL && this->type_->is_error()) 3545 return; 3546 3547 if (this->seen_) 3548 { 3549 this->report_error(_("constant refers to itself")); 3550 this->type_ = Type::make_error_type(); 3551 return; 3552 } 3553 3554 Expression* init = this->constant_->const_value()->expr(); 3555 Find_named_object find_named_object(this->constant_); 3556 3557 this->seen_ = true; 3558 Expression::traverse(&init, &find_named_object); 3559 this->seen_ = false; 3560 3561 if (find_named_object.found()) 3562 { 3563 if (this->type_ == NULL || !this->type_->is_error()) 3564 { 3565 this->report_error(_("constant refers to itself")); 3566 this->type_ = Type::make_error_type(); 3567 } 3568 return; 3569 } 3570 } 3571 3572 // Check types of a const reference. 3573 3574 void 3575 Const_expression::do_check_types(Gogo*) 3576 { 3577 if (this->type_ != NULL && this->type_->is_error()) 3578 return; 3579 3580 this->check_for_init_loop(); 3581 3582 // Check that numeric constant fits in type. 3583 if (this->type_ != NULL && this->type_->is_numeric_type()) 3584 { 3585 Numeric_constant nc; 3586 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) 3587 { 3588 if (!nc.set_type(this->type_, true, this->location())) 3589 this->set_is_error(); 3590 } 3591 } 3592 } 3593 3594 // Return the backend representation for a const reference. 3595 3596 Bexpression* 3597 Const_expression::do_get_backend(Translate_context* context) 3598 { 3599 if (this->is_error_expression() 3600 || (this->type_ != NULL && this->type_->is_error())) 3601 { 3602 go_assert(saw_errors()); 3603 return context->backend()->error_expression(); 3604 } 3605 3606 // If the type has been set for this expression, but the underlying 3607 // object is an abstract int or float, we try to get the abstract 3608 // value. Otherwise we may lose something in the conversion. 3609 Expression* expr = this->constant_->const_value()->expr(); 3610 if (this->type_ != NULL 3611 && this->type_->is_numeric_type() 3612 && (this->constant_->const_value()->type() == NULL 3613 || this->constant_->const_value()->type()->is_abstract())) 3614 { 3615 Numeric_constant nc; 3616 if (expr->numeric_constant_value(&nc) 3617 && nc.set_type(this->type_, false, this->location())) 3618 { 3619 Expression* e = nc.expression(this->location()); 3620 return e->get_backend(context); 3621 } 3622 } 3623 3624 if (this->type_ != NULL) 3625 expr = Expression::make_cast(this->type_, expr, this->location()); 3626 return expr->get_backend(context); 3627 } 3628 3629 // When exporting a reference to a const as part of a const 3630 // expression, we export the value. We ignore the fact that it has 3631 // a name. 3632 3633 void 3634 Const_expression::do_export(Export_function_body* efb) const 3635 { 3636 this->constant_->const_value()->expr()->export_expression(efb); 3637 } 3638 3639 // Dump ast representation for constant expression. 3640 3641 void 3642 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 3643 { 3644 ast_dump_context->ostream() << this->constant_->name(); 3645 } 3646 3647 // Make a reference to a constant in an expression. 3648 3649 Expression* 3650 Expression::make_const_reference(Named_object* constant, 3651 Location location) 3652 { 3653 return new Const_expression(constant, location); 3654 } 3655 3656 // Find a named object in an expression. 3657 3658 int 3659 Find_named_object::expression(Expression** pexpr) 3660 { 3661 switch ((*pexpr)->classification()) 3662 { 3663 case Expression::EXPRESSION_CONST_REFERENCE: 3664 { 3665 Const_expression* ce = static_cast<Const_expression*>(*pexpr); 3666 if (ce->named_object() == this->no_) 3667 break; 3668 3669 // We need to check a constant initializer explicitly, as 3670 // loops here will not be caught by the loop checking for 3671 // variable initializers. 3672 ce->check_for_init_loop(); 3673 3674 return TRAVERSE_CONTINUE; 3675 } 3676 3677 case Expression::EXPRESSION_VAR_REFERENCE: 3678 if ((*pexpr)->var_expression()->named_object() == this->no_) 3679 break; 3680 return TRAVERSE_CONTINUE; 3681 case Expression::EXPRESSION_FUNC_REFERENCE: 3682 if ((*pexpr)->func_expression()->named_object() == this->no_) 3683 break; 3684 return TRAVERSE_CONTINUE; 3685 default: 3686 return TRAVERSE_CONTINUE; 3687 } 3688 this->found_ = true; 3689 return TRAVERSE_EXIT; 3690 } 3691 3692 // The nil value. 3693 3694 class Nil_expression : public Expression 3695 { 3696 public: 3697 Nil_expression(Location location) 3698 : Expression(EXPRESSION_NIL, location) 3699 { } 3700 3701 static Expression* 3702 do_import(Import_expression*, Location); 3703 3704 protected: 3705 bool 3706 do_is_constant() const 3707 { return true; } 3708 3709 bool 3710 do_is_zero_value() const 3711 { return true; } 3712 3713 bool 3714 do_is_static_initializer() const 3715 { return true; } 3716 3717 Type* 3718 do_type() 3719 { return Type::make_nil_type(); } 3720 3721 void 3722 do_determine_type(const Type_context*) 3723 { } 3724 3725 Expression* 3726 do_copy() 3727 { return this; } 3728 3729 Bexpression* 3730 do_get_backend(Translate_context* context) 3731 { return context->backend()->nil_pointer_expression(); } 3732 3733 int 3734 do_inlining_cost() const 3735 { return 1; } 3736 3737 void 3738 do_export(Export_function_body* efb) const 3739 { efb->write_c_string("$nil"); } 3740 3741 void 3742 do_dump_expression(Ast_dump_context* ast_dump_context) const 3743 { ast_dump_context->ostream() << "nil"; } 3744 }; 3745 3746 // Import a nil expression. 3747 3748 Expression* 3749 Nil_expression::do_import(Import_expression* imp, Location loc) 3750 { 3751 if (imp->version() >= EXPORT_FORMAT_V3) 3752 imp->require_c_string("$"); 3753 imp->require_c_string("nil"); 3754 return Expression::make_nil(loc); 3755 } 3756 3757 // Make a nil expression. 3758 3759 Expression* 3760 Expression::make_nil(Location location) 3761 { 3762 return new Nil_expression(location); 3763 } 3764 3765 // The value of the predeclared constant iota. This is little more 3766 // than a marker. This will be lowered to an integer in 3767 // Const_expression::do_lower, which is where we know the value that 3768 // it should have. 3769 3770 class Iota_expression : public Parser_expression 3771 { 3772 public: 3773 Iota_expression(Location location) 3774 : Parser_expression(EXPRESSION_IOTA, location) 3775 { } 3776 3777 protected: 3778 Expression* 3779 do_lower(Gogo*, Named_object*, Statement_inserter*, int) 3780 { go_unreachable(); } 3781 3782 // There should only ever be one of these. 3783 Expression* 3784 do_copy() 3785 { go_unreachable(); } 3786 3787 void 3788 do_dump_expression(Ast_dump_context* ast_dump_context) const 3789 { ast_dump_context->ostream() << "iota"; } 3790 }; 3791 3792 // Make an iota expression. This is only called for one case: the 3793 // value of the predeclared constant iota. 3794 3795 Expression* 3796 Expression::make_iota() 3797 { 3798 static Iota_expression iota_expression(Linemap::unknown_location()); 3799 return &iota_expression; 3800 } 3801 3802 // Class Type_conversion_expression. 3803 3804 // Traversal. 3805 3806 int 3807 Type_conversion_expression::do_traverse(Traverse* traverse) 3808 { 3809 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT 3810 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 3811 return TRAVERSE_EXIT; 3812 return TRAVERSE_CONTINUE; 3813 } 3814 3815 // Convert to a constant at lowering time. Also lower conversions 3816 // from slice to pointer-to-array, as they can panic. 3817 3818 Expression* 3819 Type_conversion_expression::do_lower(Gogo*, Named_object*, 3820 Statement_inserter* inserter, int) 3821 { 3822 Type* type = this->type_; 3823 Expression* val = this->expr_; 3824 Location location = this->location(); 3825 3826 if (type->is_numeric_type()) 3827 { 3828 Numeric_constant nc; 3829 if (val->numeric_constant_value(&nc)) 3830 { 3831 if (!nc.set_type(type, true, location)) 3832 return Expression::make_error(location); 3833 return nc.expression(location); 3834 } 3835 } 3836 3837 // According to the language specification on string conversions 3838 // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): 3839 // When converting an integer into a string, the string will be a UTF-8 3840 // representation of the integer and integers "outside the range of valid 3841 // Unicode code points are converted to '\uFFFD'." 3842 if (type->is_string_type()) 3843 { 3844 Numeric_constant nc; 3845 if (val->numeric_constant_value(&nc) && nc.is_int()) 3846 { 3847 // An integer value doesn't fit in the Unicode code point range if it 3848 // overflows the Go "int" type or is negative. 3849 unsigned long ul; 3850 if (!nc.set_type(Type::lookup_integer_type("int"), false, location) 3851 || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) 3852 return Expression::make_string("\ufffd", location); 3853 } 3854 } 3855 3856 if (type->is_slice_type()) 3857 { 3858 Type* element_type = type->array_type()->element_type()->forwarded(); 3859 bool is_byte = (element_type->integer_type() != NULL 3860 && element_type->integer_type()->is_byte()); 3861 bool is_rune = (element_type->integer_type() != NULL 3862 && element_type->integer_type()->is_rune()); 3863 if (is_byte || is_rune) 3864 { 3865 std::string s; 3866 if (val->string_constant_value(&s)) 3867 { 3868 Expression_list* vals = new Expression_list(); 3869 if (is_byte) 3870 { 3871 for (std::string::const_iterator p = s.begin(); 3872 p != s.end(); 3873 p++) 3874 { 3875 unsigned char c = static_cast<unsigned char>(*p); 3876 vals->push_back(Expression::make_integer_ul(c, 3877 element_type, 3878 location)); 3879 } 3880 } 3881 else 3882 { 3883 const char *p = s.data(); 3884 const char *pend = s.data() + s.length(); 3885 while (p < pend) 3886 { 3887 unsigned int c; 3888 int adv = Lex::fetch_char(p, &c); 3889 if (adv == 0) 3890 { 3891 go_warning_at(this->location(), 0, 3892 "invalid UTF-8 encoding"); 3893 adv = 1; 3894 } 3895 p += adv; 3896 vals->push_back(Expression::make_integer_ul(c, 3897 element_type, 3898 location)); 3899 } 3900 } 3901 3902 return Expression::make_slice_composite_literal(type, vals, 3903 location); 3904 } 3905 } 3906 } 3907 3908 if (type->points_to() != NULL 3909 && type->points_to()->array_type() != NULL 3910 && !type->points_to()->is_slice_type() 3911 && val->type()->is_slice_type() 3912 && Type::are_identical(type->points_to()->array_type()->element_type(), 3913 val->type()->array_type()->element_type(), 3914 0, NULL)) 3915 { 3916 Temporary_statement* val_temp = NULL; 3917 if (!val->is_multi_eval_safe()) 3918 { 3919 val_temp = Statement::make_temporary(val->type(), NULL, location); 3920 inserter->insert(val_temp); 3921 val = Expression::make_set_and_use_temporary(val_temp, val, 3922 location); 3923 } 3924 3925 Type* int_type = Type::lookup_integer_type("int"); 3926 Temporary_statement* vallen_temp = 3927 Statement::make_temporary(int_type, NULL, location); 3928 inserter->insert(vallen_temp); 3929 3930 Expression* arrlen = type->points_to()->array_type()->length(); 3931 Expression* vallen = 3932 Expression::make_slice_info(val, Expression::SLICE_INFO_LENGTH, 3933 location); 3934 vallen = Expression::make_set_and_use_temporary(vallen_temp, vallen, 3935 location); 3936 Expression* cond = Expression::make_binary(OPERATOR_GT, arrlen, vallen, 3937 location); 3938 3939 vallen = Expression::make_temporary_reference(vallen_temp, location); 3940 Expression* panic = Runtime::make_call(Runtime::PANIC_SLICE_CONVERT, 3941 location, 2, arrlen, vallen); 3942 3943 Expression* nil = Expression::make_nil(location); 3944 Expression* check = Expression::make_conditional(cond, panic, nil, 3945 location); 3946 3947 if (val_temp == NULL) 3948 val = val->copy(); 3949 else 3950 val = Expression::make_temporary_reference(val_temp, location); 3951 Expression* ptr = 3952 Expression::make_slice_info(val, Expression::SLICE_INFO_VALUE_POINTER, 3953 location); 3954 ptr = Expression::make_unsafe_cast(type, ptr, location); 3955 3956 return Expression::make_compound(check, ptr, location); 3957 } 3958 3959 return this; 3960 } 3961 3962 // Flatten a type conversion by using a temporary variable for the slice 3963 // in slice to string conversions. 3964 3965 Expression* 3966 Type_conversion_expression::do_flatten(Gogo*, Named_object*, 3967 Statement_inserter* inserter) 3968 { 3969 if (this->type()->is_error_type() || this->expr_->is_error_expression()) 3970 { 3971 go_assert(saw_errors()); 3972 return Expression::make_error(this->location()); 3973 } 3974 3975 if (((this->type()->is_string_type() 3976 && this->expr_->type()->is_slice_type()) 3977 || this->expr_->type()->interface_type() != NULL) 3978 && !this->expr_->is_multi_eval_safe()) 3979 { 3980 Temporary_statement* temp = 3981 Statement::make_temporary(NULL, this->expr_, this->location()); 3982 inserter->insert(temp); 3983 this->expr_ = Expression::make_temporary_reference(temp, this->location()); 3984 } 3985 3986 // For interface conversion and string to/from slice conversions, 3987 // decide if we can allocate on stack. 3988 if (this->type()->interface_type() != NULL 3989 || this->type()->is_string_type() 3990 || this->expr_->type()->is_string_type()) 3991 { 3992 Node* n = Node::make_node(this); 3993 if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE) 3994 this->no_escape_ = true; 3995 } 3996 return this; 3997 } 3998 3999 // Return whether a type conversion is a constant. 4000 4001 bool 4002 Type_conversion_expression::do_is_constant() const 4003 { 4004 if (!this->expr_->is_constant()) 4005 return false; 4006 4007 // A conversion to a type that may not be used as a constant is not 4008 // a constant. For example, []byte(nil). 4009 Type* type = this->type_; 4010 if (type->integer_type() == NULL 4011 && type->float_type() == NULL 4012 && type->complex_type() == NULL 4013 && !type->is_boolean_type() 4014 && !type->is_string_type()) 4015 return false; 4016 4017 return true; 4018 } 4019 4020 // Return whether a type conversion is a zero value. 4021 4022 bool 4023 Type_conversion_expression::do_is_zero_value() const 4024 { 4025 if (!this->expr_->is_zero_value()) 4026 return false; 4027 4028 // Some type conversion from zero value is still not zero value. 4029 // For example, []byte("") or interface{}(0). 4030 // Conservatively, only report true if the RHS is nil. 4031 Type* type = this->type_; 4032 if (type->integer_type() == NULL 4033 && type->float_type() == NULL 4034 && type->complex_type() == NULL 4035 && !type->is_boolean_type() 4036 && !type->is_string_type()) 4037 return this->expr_->is_nil_expression(); 4038 4039 return true; 4040 } 4041 4042 // Return whether a type conversion can be used in a constant 4043 // initializer. 4044 4045 bool 4046 Type_conversion_expression::do_is_static_initializer() const 4047 { 4048 Type* type = this->type_; 4049 Type* expr_type = this->expr_->type(); 4050 4051 if (type->interface_type() != NULL 4052 || expr_type->interface_type() != NULL) 4053 return false; 4054 4055 if (!this->expr_->is_static_initializer()) 4056 return false; 4057 4058 if (Type::are_identical(type, expr_type, 4059 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 4060 NULL)) 4061 return true; 4062 4063 if (type->is_string_type() && expr_type->is_string_type()) 4064 return true; 4065 4066 if ((type->is_numeric_type() 4067 || type->is_boolean_type() 4068 || type->points_to() != NULL) 4069 && (expr_type->is_numeric_type() 4070 || expr_type->is_boolean_type() 4071 || expr_type->points_to() != NULL)) 4072 return true; 4073 4074 return false; 4075 } 4076 4077 // Return the constant numeric value if there is one. 4078 4079 bool 4080 Type_conversion_expression::do_numeric_constant_value( 4081 Numeric_constant* nc) const 4082 { 4083 if (!this->type_->is_numeric_type()) 4084 return false; 4085 if (!this->expr_->numeric_constant_value(nc)) 4086 return false; 4087 return nc->set_type(this->type_, false, this->location()); 4088 } 4089 4090 // Return the constant string value if there is one. 4091 4092 bool 4093 Type_conversion_expression::do_string_constant_value(std::string* val) const 4094 { 4095 if (this->type_->is_string_type() 4096 && this->expr_->type()->integer_type() != NULL) 4097 { 4098 Numeric_constant nc; 4099 if (this->expr_->numeric_constant_value(&nc)) 4100 { 4101 unsigned long ival; 4102 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) 4103 { 4104 unsigned int cval = static_cast<unsigned int>(ival); 4105 if (static_cast<unsigned long>(cval) != ival) 4106 { 4107 go_warning_at(this->location(), 0, 4108 "unicode code point 0x%lx out of range", 4109 ival); 4110 cval = 0xfffd; // Unicode "replacement character." 4111 } 4112 val->clear(); 4113 Lex::append_char(cval, true, val, this->location()); 4114 return true; 4115 } 4116 } 4117 } 4118 4119 // FIXME: Could handle conversion from const []int here. 4120 4121 return false; 4122 } 4123 4124 // Return the constant boolean value if there is one. 4125 4126 bool 4127 Type_conversion_expression::do_boolean_constant_value(bool* val) const 4128 { 4129 if (!this->type_->is_boolean_type()) 4130 return false; 4131 return this->expr_->boolean_constant_value(val); 4132 } 4133 4134 // Determine the resulting type of the conversion. 4135 4136 void 4137 Type_conversion_expression::do_determine_type(const Type_context*) 4138 { 4139 Type_context subcontext(this->type_, false); 4140 this->expr_->determine_type(&subcontext); 4141 } 4142 4143 // Check that types are convertible. 4144 4145 void 4146 Type_conversion_expression::do_check_types(Gogo*) 4147 { 4148 Type* type = this->type_; 4149 Type* expr_type = this->expr_->type(); 4150 std::string reason; 4151 4152 if (type->is_error() || expr_type->is_error()) 4153 { 4154 this->set_is_error(); 4155 return; 4156 } 4157 4158 if (this->may_convert_function_types_ 4159 && type->function_type() != NULL 4160 && expr_type->function_type() != NULL) 4161 return; 4162 4163 if (Type::are_convertible(type, expr_type, &reason)) 4164 return; 4165 4166 go_error_at(this->location(), "%s", reason.c_str()); 4167 this->set_is_error(); 4168 } 4169 4170 // Copy. 4171 4172 Expression* 4173 Type_conversion_expression::do_copy() 4174 { 4175 Expression* ret = new Type_conversion_expression(this->type_->copy_expressions(), 4176 this->expr_->copy(), 4177 this->location()); 4178 ret->conversion_expression()->set_no_copy(this->no_copy_); 4179 return ret; 4180 } 4181 4182 // Get the backend representation for a type conversion. 4183 4184 Bexpression* 4185 Type_conversion_expression::do_get_backend(Translate_context* context) 4186 { 4187 Type* type = this->type_; 4188 Type* expr_type = this->expr_->type(); 4189 4190 Gogo* gogo = context->gogo(); 4191 Btype* btype = type->get_backend(gogo); 4192 Location loc = this->location(); 4193 4194 if (Type::are_identical(type, expr_type, 4195 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 4196 NULL)) 4197 { 4198 Bexpression* bexpr = this->expr_->get_backend(context); 4199 return gogo->backend()->convert_expression(btype, bexpr, loc); 4200 } 4201 else if (type->interface_type() != NULL 4202 && expr_type->interface_type() == NULL) 4203 { 4204 Expression* conversion = 4205 Expression::convert_type_to_interface(type, this->expr_, 4206 this->no_escape_, loc); 4207 return conversion->get_backend(context); 4208 } 4209 else if (type->interface_type() != NULL 4210 || expr_type->interface_type() != NULL) 4211 { 4212 Expression* conversion = 4213 Expression::convert_for_assignment(gogo, type, this->expr_, 4214 loc); 4215 return conversion->get_backend(context); 4216 } 4217 else if (type->is_string_type() 4218 && expr_type->integer_type() != NULL) 4219 { 4220 mpz_t intval; 4221 Numeric_constant nc; 4222 if (this->expr_->numeric_constant_value(&nc) 4223 && nc.to_int(&intval)) 4224 { 4225 std::string s; 4226 unsigned int x; 4227 if (mpz_fits_uint_p(intval)) 4228 x = mpz_get_ui(intval); 4229 else 4230 { 4231 char* ms = mpz_get_str(NULL, 16, intval); 4232 go_warning_at(loc, 0, 4233 "unicode code point 0x%s out of range in string", 4234 ms); 4235 free(ms); 4236 x = 0xfffd; 4237 } 4238 Lex::append_char(x, true, &s, loc); 4239 mpz_clear(intval); 4240 Expression* se = Expression::make_string(s, loc); 4241 return se->get_backend(context); 4242 } 4243 4244 Expression* buf; 4245 if (this->no_escape_) 4246 { 4247 Type* byte_type = Type::lookup_integer_type("uint8"); 4248 Expression* buflen = 4249 Expression::make_integer_ul(4, NULL, loc); 4250 Type* array_type = Type::make_array_type(byte_type, buflen); 4251 buf = Expression::make_allocation(array_type, loc); 4252 buf->allocation_expression()->set_allocate_on_stack(); 4253 buf->allocation_expression()->set_no_zero(); 4254 } 4255 else 4256 buf = Expression::make_nil(loc); 4257 Expression* i2s_expr = 4258 Runtime::make_call(Runtime::INTSTRING, loc, 2, buf, this->expr_); 4259 return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); 4260 } 4261 else if (type->is_string_type() && expr_type->is_slice_type()) 4262 { 4263 Array_type* a = expr_type->array_type(); 4264 Type* e = a->element_type()->forwarded(); 4265 go_assert(e->integer_type() != NULL); 4266 go_assert(this->expr_->is_multi_eval_safe()); 4267 4268 Expression* buf; 4269 if (this->no_escape_ && !this->no_copy_) 4270 { 4271 Type* byte_type = Type::lookup_integer_type("uint8"); 4272 Expression* buflen = 4273 Expression::make_integer_ul(tmp_string_buf_size, NULL, loc); 4274 Type* array_type = Type::make_array_type(byte_type, buflen); 4275 buf = Expression::make_allocation(array_type, loc); 4276 buf->allocation_expression()->set_allocate_on_stack(); 4277 buf->allocation_expression()->set_no_zero(); 4278 } 4279 else 4280 buf = Expression::make_nil(loc); 4281 4282 if (e->integer_type()->is_byte()) 4283 { 4284 Expression* ptr = 4285 Expression::make_slice_info(this->expr_, SLICE_INFO_VALUE_POINTER, 4286 loc); 4287 Expression* len = 4288 Expression::make_slice_info(this->expr_, SLICE_INFO_LENGTH, loc); 4289 if (this->no_copy_) 4290 { 4291 if (gogo->debug_optimization()) 4292 go_debug(loc, "no copy string([]byte)"); 4293 Expression* str = Expression::make_string_value(ptr, len, loc); 4294 return str->get_backend(context); 4295 } 4296 return Runtime::make_call(Runtime::SLICEBYTETOSTRING, loc, 3, buf, 4297 ptr, len)->get_backend(context); 4298 } 4299 else 4300 { 4301 go_assert(e->integer_type()->is_rune()); 4302 return Runtime::make_call(Runtime::SLICERUNETOSTRING, loc, 2, buf, 4303 this->expr_)->get_backend(context); 4304 } 4305 } 4306 else if (type->is_slice_type() && expr_type->is_string_type()) 4307 { 4308 Type* e = type->array_type()->element_type()->forwarded(); 4309 go_assert(e->integer_type() != NULL); 4310 4311 Runtime::Function code; 4312 if (e->integer_type()->is_byte()) 4313 code = Runtime::STRINGTOSLICEBYTE; 4314 else 4315 { 4316 go_assert(e->integer_type()->is_rune()); 4317 code = Runtime::STRINGTOSLICERUNE; 4318 } 4319 4320 Expression* buf; 4321 if (this->no_escape_) 4322 { 4323 Expression* buflen = 4324 Expression::make_integer_ul(tmp_string_buf_size, NULL, loc); 4325 Type* array_type = Type::make_array_type(e, buflen); 4326 buf = Expression::make_allocation(array_type, loc); 4327 buf->allocation_expression()->set_allocate_on_stack(); 4328 buf->allocation_expression()->set_no_zero(); 4329 } 4330 else 4331 buf = Expression::make_nil(loc); 4332 Expression* s2a = Runtime::make_call(code, loc, 2, buf, this->expr_); 4333 return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); 4334 } 4335 else if (type->is_numeric_type()) 4336 { 4337 go_assert(Type::are_convertible(type, expr_type, NULL)); 4338 Bexpression* bexpr = this->expr_->get_backend(context); 4339 return gogo->backend()->convert_expression(btype, bexpr, loc); 4340 } 4341 else if ((type->is_unsafe_pointer_type() 4342 && (expr_type->points_to() != NULL 4343 || expr_type->integer_type())) 4344 || (expr_type->is_unsafe_pointer_type() 4345 && type->points_to() != NULL) 4346 || (this->may_convert_function_types_ 4347 && type->function_type() != NULL 4348 && expr_type->function_type() != NULL)) 4349 { 4350 Bexpression* bexpr = this->expr_->get_backend(context); 4351 return gogo->backend()->convert_expression(btype, bexpr, loc); 4352 } 4353 else 4354 { 4355 Expression* conversion = 4356 Expression::convert_for_assignment(gogo, type, this->expr_, loc); 4357 return conversion->get_backend(context); 4358 } 4359 } 4360 4361 // Cost of inlining a type conversion. 4362 4363 int 4364 Type_conversion_expression::do_inlining_cost() const 4365 { 4366 Type* type = this->type_; 4367 Type* expr_type = this->expr_->type(); 4368 if (type->interface_type() != NULL || expr_type->interface_type() != NULL) 4369 return 10; 4370 else if (type->is_string_type() && expr_type->integer_type() != NULL) 4371 return 10; 4372 else if (type->is_string_type() && expr_type->is_slice_type()) 4373 return 10; 4374 else if (type->is_slice_type() && expr_type->is_string_type()) 4375 return 10; 4376 else 4377 return 1; 4378 } 4379 4380 // Output a type conversion in a constant expression. 4381 4382 void 4383 Type_conversion_expression::do_export(Export_function_body* efb) const 4384 { 4385 efb->write_c_string("$convert("); 4386 efb->write_type(this->type_); 4387 efb->write_c_string(", "); 4388 4389 Type* old_context = efb->type_context(); 4390 efb->set_type_context(this->type_); 4391 4392 this->expr_->export_expression(efb); 4393 4394 efb->set_type_context(old_context); 4395 4396 efb->write_c_string(")"); 4397 } 4398 4399 // Import a type conversion or a struct construction. 4400 4401 Expression* 4402 Type_conversion_expression::do_import(Import_expression* imp, Location loc) 4403 { 4404 imp->require_c_string("$convert("); 4405 Type* type = imp->read_type(); 4406 imp->require_c_string(", "); 4407 Expression* val = Expression::import_expression(imp, loc); 4408 imp->require_c_string(")"); 4409 return Expression::make_cast(type, val, loc); 4410 } 4411 4412 // Dump ast representation for a type conversion expression. 4413 4414 void 4415 Type_conversion_expression::do_dump_expression( 4416 Ast_dump_context* ast_dump_context) const 4417 { 4418 ast_dump_context->dump_type(this->type_); 4419 ast_dump_context->ostream() << "("; 4420 ast_dump_context->dump_expression(this->expr_); 4421 ast_dump_context->ostream() << ") "; 4422 } 4423 4424 // Make a type cast expression. 4425 4426 Expression* 4427 Expression::make_cast(Type* type, Expression* val, Location location) 4428 { 4429 if (type->is_error_type() || val->is_error_expression()) 4430 return Expression::make_error(location); 4431 return new Type_conversion_expression(type, val, location); 4432 } 4433 4434 // Class Unsafe_type_conversion_expression. 4435 4436 // Traversal. 4437 4438 int 4439 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) 4440 { 4441 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT 4442 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 4443 return TRAVERSE_EXIT; 4444 return TRAVERSE_CONTINUE; 4445 } 4446 4447 // Return whether an unsafe type conversion can be used as a constant 4448 // initializer. 4449 4450 bool 4451 Unsafe_type_conversion_expression::do_is_static_initializer() const 4452 { 4453 Type* type = this->type_; 4454 Type* expr_type = this->expr_->type(); 4455 4456 if (type->interface_type() != NULL 4457 || expr_type->interface_type() != NULL) 4458 return false; 4459 4460 if (!this->expr_->is_static_initializer()) 4461 return false; 4462 4463 if (Type::are_convertible(type, expr_type, NULL)) 4464 return true; 4465 4466 if (type->is_string_type() && expr_type->is_string_type()) 4467 return true; 4468 4469 if ((type->is_numeric_type() 4470 || type->is_boolean_type() 4471 || type->points_to() != NULL) 4472 && (expr_type->is_numeric_type() 4473 || expr_type->is_boolean_type() 4474 || expr_type->points_to() != NULL)) 4475 return true; 4476 4477 return false; 4478 } 4479 4480 // Copy. 4481 4482 Expression* 4483 Unsafe_type_conversion_expression::do_copy() 4484 { 4485 return new Unsafe_type_conversion_expression(this->type_->copy_expressions(), 4486 this->expr_->copy(), 4487 this->location()); 4488 } 4489 4490 // Convert to backend representation. 4491 4492 Bexpression* 4493 Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) 4494 { 4495 // We are only called for a limited number of cases. 4496 4497 Type* t = this->type_; 4498 Type* et = this->expr_->type(); 4499 4500 if (t->is_error_type() 4501 || this->expr_->is_error_expression() 4502 || et->is_error_type()) 4503 { 4504 go_assert(saw_errors()); 4505 return context->backend()->error_expression(); 4506 } 4507 4508 if (t->array_type() != NULL) 4509 go_assert(et->array_type() != NULL 4510 && t->is_slice_type() == et->is_slice_type()); 4511 else if (t->struct_type() != NULL) 4512 { 4513 if (t->named_type() != NULL 4514 && et->named_type() != NULL 4515 && !Type::are_convertible(t, et, NULL)) 4516 { 4517 go_assert(saw_errors()); 4518 return context->backend()->error_expression(); 4519 } 4520 4521 go_assert(et->struct_type() != NULL 4522 && Type::are_convertible(t, et, NULL)); 4523 } 4524 else if (t->map_type() != NULL) 4525 go_assert(et->map_type() != NULL || et->points_to() != NULL); 4526 else if (t->channel_type() != NULL) 4527 go_assert(et->channel_type() != NULL || et->points_to() != NULL); 4528 else if (t->points_to() != NULL) 4529 go_assert(et->points_to() != NULL 4530 || et->channel_type() != NULL 4531 || et->map_type() != NULL 4532 || et->function_type() != NULL 4533 || et->integer_type() != NULL 4534 || et->is_nil_type()); 4535 else if (t->function_type() != NULL) 4536 go_assert(et->points_to() != NULL); 4537 else if (et->is_unsafe_pointer_type()) 4538 go_assert(t->points_to() != NULL 4539 || (t->integer_type() != NULL 4540 && t->integer_type() == Type::lookup_integer_type("uintptr")->real_type())); 4541 else if (t->interface_type() != NULL) 4542 { 4543 bool empty_iface = t->interface_type()->is_empty(); 4544 go_assert(et->interface_type() != NULL 4545 && et->interface_type()->is_empty() == empty_iface); 4546 } 4547 else if (t->integer_type() != NULL) 4548 go_assert(et->is_boolean_type() 4549 || et->integer_type() != NULL 4550 || et->function_type() != NULL 4551 || et->points_to() != NULL 4552 || et->map_type() != NULL 4553 || et->channel_type() != NULL 4554 || et->is_nil_type()); 4555 else 4556 go_unreachable(); 4557 4558 Gogo* gogo = context->gogo(); 4559 Btype* btype = t->get_backend(gogo); 4560 Bexpression* bexpr = this->expr_->get_backend(context); 4561 Location loc = this->location(); 4562 return gogo->backend()->convert_expression(btype, bexpr, loc); 4563 } 4564 4565 // Dump ast representation for an unsafe type conversion expression. 4566 4567 void 4568 Unsafe_type_conversion_expression::do_dump_expression( 4569 Ast_dump_context* ast_dump_context) const 4570 { 4571 ast_dump_context->dump_type(this->type_); 4572 ast_dump_context->ostream() << "("; 4573 ast_dump_context->dump_expression(this->expr_); 4574 ast_dump_context->ostream() << ") "; 4575 } 4576 4577 // Make an unsafe type conversion expression. 4578 4579 Expression* 4580 Expression::make_unsafe_cast(Type* type, Expression* expr, 4581 Location location) 4582 { 4583 return new Unsafe_type_conversion_expression(type, expr, location); 4584 } 4585 4586 // Class Unary_expression. 4587 4588 // Call the address_taken method of the operand if needed. This is 4589 // called after escape analysis but before inserting write barriers. 4590 4591 void 4592 Unary_expression::check_operand_address_taken(Gogo*) 4593 { 4594 if (this->op_ != OPERATOR_AND) 4595 return; 4596 4597 // If this->escapes_ is false at this point, then it was set to 4598 // false by an explicit call to set_does_not_escape, and the value 4599 // does not escape. If this->escapes_ is true, we may be able to 4600 // set it to false based on the escape analysis pass. 4601 if (this->escapes_) 4602 { 4603 Node* n = Node::make_node(this); 4604 if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) 4605 this->escapes_ = false; 4606 } 4607 4608 this->expr_->address_taken(this->escapes_); 4609 } 4610 4611 // If we are taking the address of a composite literal, and the 4612 // contents are not constant, then we want to make a heap expression 4613 // instead. 4614 4615 Expression* 4616 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) 4617 { 4618 Location loc = this->location(); 4619 Operator op = this->op_; 4620 Expression* expr = this->expr_; 4621 4622 if (op == OPERATOR_MULT && expr->is_type_expression()) 4623 return Expression::make_type(Type::make_pointer_type(expr->type()), loc); 4624 4625 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require 4626 // moving x to the heap. FIXME: Is it worth doing a real escape 4627 // analysis here? This case is found in math/unsafe.go and is 4628 // therefore worth special casing. 4629 if (op == OPERATOR_MULT) 4630 { 4631 Expression* e = expr; 4632 while (e->classification() == EXPRESSION_CONVERSION) 4633 { 4634 Type_conversion_expression* te 4635 = static_cast<Type_conversion_expression*>(e); 4636 e = te->expr(); 4637 } 4638 4639 if (e->classification() == EXPRESSION_UNARY) 4640 { 4641 Unary_expression* ue = static_cast<Unary_expression*>(e); 4642 if (ue->op_ == OPERATOR_AND) 4643 { 4644 if (e == expr) 4645 { 4646 // *&x == x. 4647 if (!ue->expr_->is_addressable() && !ue->create_temp_) 4648 { 4649 go_error_at(ue->location(), 4650 "invalid operand for unary %<&%>"); 4651 this->set_is_error(); 4652 } 4653 return ue->expr_; 4654 } 4655 ue->set_does_not_escape(); 4656 } 4657 } 4658 } 4659 4660 // Catching an invalid indirection of unsafe.Pointer here avoid 4661 // having to deal with TYPE_VOID in other places. 4662 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) 4663 { 4664 go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); 4665 return Expression::make_error(this->location()); 4666 } 4667 4668 // Check for an invalid pointer dereference. We need to do this 4669 // here because Unary_expression::do_type will return an error type 4670 // in this case. That can cause code to appear erroneous, and 4671 // therefore disappear at lowering time, without any error message. 4672 if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) 4673 { 4674 this->report_error(_("expected pointer")); 4675 return Expression::make_error(this->location()); 4676 } 4677 4678 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) 4679 { 4680 Numeric_constant nc; 4681 if (expr->numeric_constant_value(&nc)) 4682 { 4683 Numeric_constant result; 4684 bool issued_error; 4685 if (Unary_expression::eval_constant(op, &nc, loc, &result, 4686 &issued_error)) 4687 return result.expression(loc); 4688 else if (issued_error) 4689 return Expression::make_error(this->location()); 4690 } 4691 } 4692 4693 return this; 4694 } 4695 4696 // Flatten expression if a nil check must be performed and create temporary 4697 // variables if necessary. 4698 4699 Expression* 4700 Unary_expression::do_flatten(Gogo* gogo, Named_object*, 4701 Statement_inserter* inserter) 4702 { 4703 if (this->is_error_expression() 4704 || this->expr_->is_error_expression() 4705 || this->expr_->type()->is_error_type()) 4706 { 4707 go_assert(saw_errors()); 4708 return Expression::make_error(this->location()); 4709 } 4710 4711 Location location = this->location(); 4712 if (this->op_ == OPERATOR_MULT 4713 && !this->expr_->is_multi_eval_safe()) 4714 { 4715 go_assert(this->expr_->type()->points_to() != NULL); 4716 switch (this->requires_nil_check(gogo)) 4717 { 4718 case NIL_CHECK_ERROR_ENCOUNTERED: 4719 { 4720 go_assert(saw_errors()); 4721 return Expression::make_error(this->location()); 4722 } 4723 case NIL_CHECK_NOT_NEEDED: 4724 break; 4725 case NIL_CHECK_NEEDED: 4726 this->create_temp_ = true; 4727 break; 4728 case NIL_CHECK_DEFAULT: 4729 go_unreachable(); 4730 } 4731 } 4732 4733 if (this->create_temp_ && !this->expr_->is_multi_eval_safe()) 4734 { 4735 Temporary_statement* temp = 4736 Statement::make_temporary(NULL, this->expr_, location); 4737 inserter->insert(temp); 4738 this->expr_ = Expression::make_temporary_reference(temp, location); 4739 } 4740 4741 return this; 4742 } 4743 4744 // Return whether a unary expression is a constant. 4745 4746 bool 4747 Unary_expression::do_is_constant() const 4748 { 4749 if (this->op_ == OPERATOR_MULT) 4750 { 4751 // Indirecting through a pointer is only constant if the object 4752 // to which the expression points is constant, but we currently 4753 // have no way to determine that. 4754 return false; 4755 } 4756 else if (this->op_ == OPERATOR_AND) 4757 { 4758 // Taking the address of a variable is constant if it is a 4759 // global variable, not constant otherwise. In other cases taking the 4760 // address is probably not a constant. 4761 Var_expression* ve = this->expr_->var_expression(); 4762 if (ve != NULL) 4763 { 4764 Named_object* no = ve->named_object(); 4765 return no->is_variable() && no->var_value()->is_global(); 4766 } 4767 return false; 4768 } 4769 else 4770 return this->expr_->is_constant(); 4771 } 4772 4773 // Return whether a unary expression can be used as a constant 4774 // initializer. 4775 4776 bool 4777 Unary_expression::do_is_static_initializer() const 4778 { 4779 if (this->op_ == OPERATOR_MULT) 4780 return false; 4781 else if (this->op_ == OPERATOR_AND) 4782 return Unary_expression::base_is_static_initializer(this->expr_); 4783 else 4784 return this->expr_->is_static_initializer(); 4785 } 4786 4787 // Return whether the address of EXPR can be used as a static 4788 // initializer. 4789 4790 bool 4791 Unary_expression::base_is_static_initializer(Expression* expr) 4792 { 4793 // The address of a field reference can be a static initializer if 4794 // the base can be a static initializer. 4795 Field_reference_expression* fre = expr->field_reference_expression(); 4796 if (fre != NULL) 4797 return Unary_expression::base_is_static_initializer(fre->expr()); 4798 4799 // The address of an index expression can be a static initializer if 4800 // the base can be a static initializer and the index is constant. 4801 Array_index_expression* aind = expr->array_index_expression(); 4802 if (aind != NULL) 4803 return (aind->end() == NULL 4804 && aind->start()->is_constant() 4805 && Unary_expression::base_is_static_initializer(aind->array())); 4806 4807 // The address of a global variable can be a static initializer. 4808 Var_expression* ve = expr->var_expression(); 4809 if (ve != NULL) 4810 { 4811 Named_object* no = ve->named_object(); 4812 return no->is_variable() && no->var_value()->is_global(); 4813 } 4814 4815 // The address of a composite literal can be used as a static 4816 // initializer if the composite literal is itself usable as a 4817 // static initializer. 4818 if (expr->is_composite_literal() && expr->is_static_initializer()) 4819 return true; 4820 4821 // The address of a string constant can be used as a static 4822 // initializer. This can not be written in Go itself but this is 4823 // used when building a type descriptor. 4824 if (expr->string_expression() != NULL) 4825 return true; 4826 4827 return false; 4828 } 4829 4830 // Return whether this dereference expression requires an explicit nil 4831 // check. If we are dereferencing the pointer to a large struct 4832 // (greater than the specified size threshold), we need to check for 4833 // nil. We don't bother to check for small structs because we expect 4834 // the system to crash on a nil pointer dereference. However, if we 4835 // know the address of this expression is being taken, we must always 4836 // check for nil. 4837 Unary_expression::Nil_check_classification 4838 Unary_expression::requires_nil_check(Gogo* gogo) 4839 { 4840 go_assert(this->op_ == OPERATOR_MULT); 4841 go_assert(this->expr_->type()->points_to() != NULL); 4842 4843 if (this->issue_nil_check_ == NIL_CHECK_NEEDED) 4844 return NIL_CHECK_NEEDED; 4845 else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED) 4846 return NIL_CHECK_NOT_NEEDED; 4847 4848 Type* ptype = this->expr_->type()->points_to(); 4849 int64_t type_size = -1; 4850 if (!ptype->is_void_type()) 4851 { 4852 bool ok = ptype->backend_type_size(gogo, &type_size); 4853 if (!ok) 4854 return NIL_CHECK_ERROR_ENCOUNTERED; 4855 } 4856 4857 int64_t size_cutoff = gogo->nil_check_size_threshold(); 4858 if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff)) 4859 this->issue_nil_check_ = NIL_CHECK_NEEDED; 4860 else 4861 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED; 4862 return this->issue_nil_check_; 4863 } 4864 4865 // Apply unary opcode OP to UNC, setting NC. Return true if this 4866 // could be done, false if not. On overflow, issues an error and sets 4867 // *ISSUED_ERROR. 4868 4869 bool 4870 Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, 4871 Location location, Numeric_constant* nc, 4872 bool* issued_error) 4873 { 4874 *issued_error = false; 4875 switch (op) 4876 { 4877 case OPERATOR_PLUS: 4878 *nc = *unc; 4879 return true; 4880 4881 case OPERATOR_MINUS: 4882 if (unc->is_int() || unc->is_rune()) 4883 break; 4884 else if (unc->is_float()) 4885 { 4886 mpfr_t uval; 4887 unc->get_float(&uval); 4888 mpfr_t val; 4889 mpfr_init(val); 4890 mpfr_neg(val, uval, MPFR_RNDN); 4891 nc->set_float(unc->type(), val); 4892 mpfr_clear(uval); 4893 mpfr_clear(val); 4894 return true; 4895 } 4896 else if (unc->is_complex()) 4897 { 4898 mpc_t uval; 4899 unc->get_complex(&uval); 4900 mpc_t val; 4901 mpc_init2(val, mpc_precision); 4902 mpc_neg(val, uval, MPC_RNDNN); 4903 nc->set_complex(unc->type(), val); 4904 mpc_clear(uval); 4905 mpc_clear(val); 4906 return true; 4907 } 4908 else 4909 go_unreachable(); 4910 4911 case OPERATOR_XOR: 4912 break; 4913 4914 case OPERATOR_NOT: 4915 case OPERATOR_AND: 4916 case OPERATOR_MULT: 4917 return false; 4918 4919 default: 4920 go_unreachable(); 4921 } 4922 4923 if (!unc->is_int() && !unc->is_rune()) 4924 return false; 4925 4926 mpz_t uval; 4927 if (unc->is_rune()) 4928 unc->get_rune(&uval); 4929 else 4930 unc->get_int(&uval); 4931 mpz_t val; 4932 mpz_init(val); 4933 4934 switch (op) 4935 { 4936 case OPERATOR_MINUS: 4937 mpz_neg(val, uval); 4938 break; 4939 4940 case OPERATOR_NOT: 4941 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); 4942 break; 4943 4944 case OPERATOR_XOR: 4945 { 4946 Type* utype = unc->type(); 4947 if (utype->integer_type() == NULL 4948 || utype->integer_type()->is_abstract()) 4949 mpz_com(val, uval); 4950 else 4951 { 4952 // The number of HOST_WIDE_INTs that it takes to represent 4953 // UVAL. 4954 size_t count = ((mpz_sizeinbase(uval, 2) 4955 + HOST_BITS_PER_WIDE_INT 4956 - 1) 4957 / HOST_BITS_PER_WIDE_INT); 4958 4959 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; 4960 memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); 4961 4962 size_t obits = utype->integer_type()->bits(); 4963 4964 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) 4965 { 4966 mpz_t adj; 4967 mpz_init_set_ui(adj, 1); 4968 mpz_mul_2exp(adj, adj, obits); 4969 mpz_add(uval, uval, adj); 4970 mpz_clear(adj); 4971 } 4972 4973 size_t ecount; 4974 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); 4975 go_assert(ecount <= count); 4976 4977 // Trim down to the number of words required by the type. 4978 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) 4979 / HOST_BITS_PER_WIDE_INT); 4980 go_assert(ocount <= count); 4981 4982 for (size_t i = 0; i < ocount; ++i) 4983 phwi[i] = ~phwi[i]; 4984 4985 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; 4986 if (clearbits != 0) 4987 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) 4988 >> clearbits); 4989 4990 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); 4991 4992 if (!utype->integer_type()->is_unsigned() 4993 && mpz_tstbit(val, obits - 1)) 4994 { 4995 mpz_t adj; 4996 mpz_init_set_ui(adj, 1); 4997 mpz_mul_2exp(adj, adj, obits); 4998 mpz_sub(val, val, adj); 4999 mpz_clear(adj); 5000 } 5001 5002 delete[] phwi; 5003 } 5004 } 5005 break; 5006 5007 default: 5008 go_unreachable(); 5009 } 5010 5011 if (unc->is_rune()) 5012 nc->set_rune(NULL, val); 5013 else 5014 nc->set_int(NULL, val); 5015 5016 mpz_clear(uval); 5017 mpz_clear(val); 5018 5019 if (!nc->set_type(unc->type(), true, location)) 5020 { 5021 *issued_error = true; 5022 return false; 5023 } 5024 return true; 5025 } 5026 5027 // Return the integral constant value of a unary expression, if it has one. 5028 5029 bool 5030 Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const 5031 { 5032 Numeric_constant unc; 5033 if (!this->expr_->numeric_constant_value(&unc)) 5034 return false; 5035 bool issued_error; 5036 return Unary_expression::eval_constant(this->op_, &unc, this->location(), 5037 nc, &issued_error); 5038 } 5039 5040 // Return the boolean constant value of a unary expression, if it has one. 5041 5042 bool 5043 Unary_expression::do_boolean_constant_value(bool* val) const 5044 { 5045 if (this->op_ == OPERATOR_NOT 5046 && this->expr_->boolean_constant_value(val)) 5047 { 5048 *val = !*val; 5049 return true; 5050 } 5051 return false; 5052 } 5053 5054 // Return the type of a unary expression. 5055 5056 Type* 5057 Unary_expression::do_type() 5058 { 5059 switch (this->op_) 5060 { 5061 case OPERATOR_PLUS: 5062 case OPERATOR_MINUS: 5063 case OPERATOR_NOT: 5064 case OPERATOR_XOR: 5065 return this->expr_->type(); 5066 5067 case OPERATOR_AND: 5068 return Type::make_pointer_type(this->expr_->type()); 5069 5070 case OPERATOR_MULT: 5071 { 5072 Type* subtype = this->expr_->type(); 5073 Type* points_to = subtype->points_to(); 5074 if (points_to == NULL) 5075 return Type::make_error_type(); 5076 return points_to; 5077 } 5078 5079 default: 5080 go_unreachable(); 5081 } 5082 } 5083 5084 // Determine abstract types for a unary expression. 5085 5086 void 5087 Unary_expression::do_determine_type(const Type_context* context) 5088 { 5089 switch (this->op_) 5090 { 5091 case OPERATOR_PLUS: 5092 case OPERATOR_MINUS: 5093 case OPERATOR_NOT: 5094 case OPERATOR_XOR: 5095 this->expr_->determine_type(context); 5096 break; 5097 5098 case OPERATOR_AND: 5099 // Taking the address of something. 5100 { 5101 Type* subtype = (context->type == NULL 5102 ? NULL 5103 : context->type->points_to()); 5104 Type_context subcontext(subtype, false); 5105 this->expr_->determine_type(&subcontext); 5106 } 5107 break; 5108 5109 case OPERATOR_MULT: 5110 // Indirecting through a pointer. 5111 { 5112 Type* subtype = (context->type == NULL 5113 ? NULL 5114 : Type::make_pointer_type(context->type)); 5115 Type_context subcontext(subtype, false); 5116 this->expr_->determine_type(&subcontext); 5117 } 5118 break; 5119 5120 default: 5121 go_unreachable(); 5122 } 5123 } 5124 5125 // Check types for a unary expression. 5126 5127 void 5128 Unary_expression::do_check_types(Gogo*) 5129 { 5130 Type* type = this->expr_->type(); 5131 if (type->is_error()) 5132 { 5133 this->set_is_error(); 5134 return; 5135 } 5136 5137 switch (this->op_) 5138 { 5139 case OPERATOR_PLUS: 5140 case OPERATOR_MINUS: 5141 if (type->integer_type() == NULL 5142 && type->float_type() == NULL 5143 && type->complex_type() == NULL) 5144 this->report_error(_("expected numeric type")); 5145 break; 5146 5147 case OPERATOR_NOT: 5148 if (!type->is_boolean_type()) 5149 this->report_error(_("expected boolean type")); 5150 break; 5151 5152 case OPERATOR_XOR: 5153 if (type->integer_type() == NULL) 5154 this->report_error(_("expected integer")); 5155 break; 5156 5157 case OPERATOR_AND: 5158 if (!this->expr_->is_addressable()) 5159 { 5160 if (!this->create_temp_) 5161 { 5162 go_error_at(this->location(), "invalid operand for unary %<&%>"); 5163 this->set_is_error(); 5164 } 5165 } 5166 else 5167 this->expr_->issue_nil_check(); 5168 break; 5169 5170 case OPERATOR_MULT: 5171 // Indirecting through a pointer. 5172 if (type->points_to() == NULL) 5173 this->report_error(_("expected pointer")); 5174 if (type->points_to()->is_error()) 5175 this->set_is_error(); 5176 break; 5177 5178 default: 5179 go_unreachable(); 5180 } 5181 } 5182 5183 // Get the backend representation for a unary expression. 5184 5185 Bexpression* 5186 Unary_expression::do_get_backend(Translate_context* context) 5187 { 5188 Gogo* gogo = context->gogo(); 5189 Location loc = this->location(); 5190 5191 // Taking the address of a set-and-use-temporary expression requires 5192 // setting the temporary and then taking the address. 5193 if (this->op_ == OPERATOR_AND) 5194 { 5195 Set_and_use_temporary_expression* sut = 5196 this->expr_->set_and_use_temporary_expression(); 5197 if (sut != NULL) 5198 { 5199 Temporary_statement* temp = sut->temporary(); 5200 Bvariable* bvar = temp->get_backend_variable(context); 5201 Bexpression* bvar_expr = 5202 gogo->backend()->var_expression(bvar, loc); 5203 Bexpression* bval = sut->expression()->get_backend(context); 5204 5205 Named_object* fn = context->function(); 5206 go_assert(fn != NULL); 5207 Bfunction* bfn = 5208 fn->func_value()->get_or_make_decl(gogo, fn); 5209 Bstatement* bassign = 5210 gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc); 5211 Bexpression* bvar_addr = 5212 gogo->backend()->address_expression(bvar_expr, loc); 5213 return gogo->backend()->compound_expression(bassign, bvar_addr, loc); 5214 } 5215 } 5216 5217 Bexpression* ret; 5218 Bexpression* bexpr = this->expr_->get_backend(context); 5219 Btype* btype = this->expr_->type()->get_backend(gogo); 5220 switch (this->op_) 5221 { 5222 case OPERATOR_PLUS: 5223 ret = bexpr; 5224 break; 5225 5226 case OPERATOR_MINUS: 5227 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); 5228 ret = gogo->backend()->convert_expression(btype, ret, loc); 5229 break; 5230 5231 case OPERATOR_NOT: 5232 case OPERATOR_XOR: 5233 ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); 5234 break; 5235 5236 case OPERATOR_AND: 5237 if (!this->create_temp_) 5238 { 5239 // We should not see a non-constant constructor here; cases 5240 // where we would see one should have been moved onto the 5241 // heap at parse time. Taking the address of a nonconstant 5242 // constructor will not do what the programmer expects. 5243 5244 go_assert(!this->expr_->is_composite_literal() 5245 || this->expr_->is_static_initializer()); 5246 if (this->expr_->classification() == EXPRESSION_UNARY) 5247 { 5248 Unary_expression* ue = 5249 static_cast<Unary_expression*>(this->expr_); 5250 go_assert(ue->op() != OPERATOR_AND); 5251 } 5252 } 5253 5254 if (this->is_gc_root_ || this->is_slice_init_) 5255 { 5256 std::string var_name; 5257 bool copy_to_heap = false; 5258 if (this->is_gc_root_) 5259 { 5260 // Build a decl for a GC root variable. GC roots are mutable, so 5261 // they cannot be represented as an immutable_struct in the 5262 // backend. 5263 var_name = gogo->gc_root_name(); 5264 } 5265 else 5266 { 5267 // Build a decl for a slice value initializer. An immutable slice 5268 // value initializer may have to be copied to the heap if it 5269 // contains pointers in a non-constant context. 5270 var_name = gogo->initializer_name(); 5271 5272 Array_type* at = this->expr_->type()->array_type(); 5273 go_assert(at != NULL); 5274 5275 // If we are not copying the value to the heap, we will only 5276 // initialize the value once, so we can use this directly 5277 // rather than copying it. In that case we can't make it 5278 // read-only, because the program is permitted to change it. 5279 copy_to_heap = (context->function() != NULL 5280 || context->is_const()); 5281 } 5282 unsigned int flags = (Backend::variable_is_hidden 5283 | Backend::variable_address_is_taken); 5284 if (copy_to_heap) 5285 flags |= Backend::variable_is_constant; 5286 Bvariable* implicit = 5287 gogo->backend()->implicit_variable(var_name, "", btype, flags, 0); 5288 gogo->backend()->implicit_variable_set_init(implicit, var_name, btype, 5289 flags, bexpr); 5290 bexpr = gogo->backend()->var_expression(implicit, loc); 5291 5292 // If we are not copying a slice initializer to the heap, 5293 // then it can be changed by the program, so if it can 5294 // contain pointers we must register it as a GC root. 5295 if (this->is_slice_init_ 5296 && !copy_to_heap 5297 && this->expr_->type()->has_pointer()) 5298 { 5299 Bexpression* root = 5300 gogo->backend()->var_expression(implicit, loc); 5301 root = gogo->backend()->address_expression(root, loc); 5302 Type* type = Type::make_pointer_type(this->expr_->type()); 5303 gogo->add_gc_root(Expression::make_backend(root, type, loc)); 5304 } 5305 } 5306 else if ((this->expr_->is_composite_literal() 5307 || this->expr_->string_expression() != NULL) 5308 && this->expr_->is_static_initializer()) 5309 { 5310 std::string var_name(gogo->initializer_name()); 5311 unsigned int flags = (Backend::variable_is_hidden 5312 | Backend::variable_address_is_taken); 5313 Bvariable* decl = 5314 gogo->backend()->immutable_struct(var_name, "", flags, btype, loc); 5315 gogo->backend()->immutable_struct_set_init(decl, var_name, flags, 5316 btype, loc, bexpr); 5317 bexpr = gogo->backend()->var_expression(decl, loc); 5318 } 5319 else if (this->expr_->is_constant()) 5320 { 5321 std::string var_name(gogo->initializer_name()); 5322 unsigned int flags = (Backend::variable_is_hidden 5323 | Backend::variable_is_constant 5324 | Backend::variable_address_is_taken); 5325 Bvariable* decl = 5326 gogo->backend()->implicit_variable(var_name, "", btype, flags, 0); 5327 gogo->backend()->implicit_variable_set_init(decl, var_name, btype, 5328 flags, bexpr); 5329 bexpr = gogo->backend()->var_expression(decl, loc); 5330 } 5331 5332 go_assert(!this->create_temp_ || this->expr_->is_multi_eval_safe()); 5333 ret = gogo->backend()->address_expression(bexpr, loc); 5334 break; 5335 5336 case OPERATOR_MULT: 5337 { 5338 go_assert(this->expr_->type()->points_to() != NULL); 5339 5340 Type* ptype = this->expr_->type()->points_to(); 5341 Btype* pbtype = ptype->get_backend(gogo); 5342 switch (this->requires_nil_check(gogo)) 5343 { 5344 case NIL_CHECK_NOT_NEEDED: 5345 break; 5346 case NIL_CHECK_ERROR_ENCOUNTERED: 5347 { 5348 go_assert(saw_errors()); 5349 return gogo->backend()->error_expression(); 5350 } 5351 case NIL_CHECK_NEEDED: 5352 { 5353 go_assert(this->expr_->is_multi_eval_safe()); 5354 5355 // If we're nil-checking the result of a set-and-use-temporary 5356 // expression, then pick out the target temp and use that 5357 // for the final result of the conditional. 5358 Bexpression* tbexpr = bexpr; 5359 Bexpression* ubexpr = bexpr; 5360 Set_and_use_temporary_expression* sut = 5361 this->expr_->set_and_use_temporary_expression(); 5362 if (sut != NULL) { 5363 Temporary_statement* temp = sut->temporary(); 5364 Bvariable* bvar = temp->get_backend_variable(context); 5365 ubexpr = gogo->backend()->var_expression(bvar, loc); 5366 } 5367 Bexpression* nil = 5368 Expression::make_nil(loc)->get_backend(context); 5369 Bexpression* compare = 5370 gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr, 5371 nil, loc); 5372 Expression* crash = Runtime::make_call(Runtime::PANIC_MEM, 5373 loc, 0); 5374 Bexpression* bcrash = crash->get_backend(context); 5375 Bfunction* bfn = context->function()->func_value()->get_decl(); 5376 bexpr = gogo->backend()->conditional_expression(bfn, btype, 5377 compare, 5378 bcrash, ubexpr, 5379 loc); 5380 break; 5381 } 5382 case NIL_CHECK_DEFAULT: 5383 go_unreachable(); 5384 } 5385 ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); 5386 } 5387 break; 5388 5389 default: 5390 go_unreachable(); 5391 } 5392 5393 return ret; 5394 } 5395 5396 // Export a unary expression. 5397 5398 void 5399 Unary_expression::do_export(Export_function_body* efb) const 5400 { 5401 switch (this->op_) 5402 { 5403 case OPERATOR_PLUS: 5404 efb->write_c_string("+"); 5405 break; 5406 case OPERATOR_MINUS: 5407 efb->write_c_string("-"); 5408 break; 5409 case OPERATOR_NOT: 5410 efb->write_c_string("!"); 5411 break; 5412 case OPERATOR_XOR: 5413 efb->write_c_string("^"); 5414 break; 5415 case OPERATOR_AND: 5416 efb->write_c_string("&"); 5417 break; 5418 case OPERATOR_MULT: 5419 efb->write_c_string("*"); 5420 break; 5421 default: 5422 go_unreachable(); 5423 } 5424 this->expr_->export_expression(efb); 5425 } 5426 5427 // Import a unary expression. 5428 5429 Expression* 5430 Unary_expression::do_import(Import_expression* imp, Location loc) 5431 { 5432 Operator op; 5433 switch (imp->get_char()) 5434 { 5435 case '+': 5436 op = OPERATOR_PLUS; 5437 break; 5438 case '-': 5439 op = OPERATOR_MINUS; 5440 break; 5441 case '!': 5442 op = OPERATOR_NOT; 5443 break; 5444 case '^': 5445 op = OPERATOR_XOR; 5446 break; 5447 case '&': 5448 op = OPERATOR_AND; 5449 break; 5450 case '*': 5451 op = OPERATOR_MULT; 5452 break; 5453 default: 5454 go_unreachable(); 5455 } 5456 if (imp->version() < EXPORT_FORMAT_V3) 5457 imp->require_c_string(" "); 5458 Expression* expr = Expression::import_expression(imp, loc); 5459 return Expression::make_unary(op, expr, loc); 5460 } 5461 5462 // Dump ast representation of an unary expression. 5463 5464 void 5465 Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 5466 { 5467 ast_dump_context->dump_operator(this->op_); 5468 ast_dump_context->ostream() << "("; 5469 ast_dump_context->dump_expression(this->expr_); 5470 ast_dump_context->ostream() << ") "; 5471 } 5472 5473 // Make a unary expression. 5474 5475 Expression* 5476 Expression::make_unary(Operator op, Expression* expr, Location location) 5477 { 5478 return new Unary_expression(op, expr, location); 5479 } 5480 5481 Expression* 5482 Expression::make_dereference(Expression* ptr, 5483 Nil_check_classification docheck, 5484 Location location) 5485 { 5486 Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location); 5487 if (docheck == NIL_CHECK_NEEDED) 5488 deref->unary_expression()->set_requires_nil_check(true); 5489 else if (docheck == NIL_CHECK_NOT_NEEDED) 5490 deref->unary_expression()->set_requires_nil_check(false); 5491 return deref; 5492 } 5493 5494 // If this is an indirection through a pointer, return the expression 5495 // being pointed through. Otherwise return this. 5496 5497 Expression* 5498 Expression::deref() 5499 { 5500 if (this->classification_ == EXPRESSION_UNARY) 5501 { 5502 Unary_expression* ue = static_cast<Unary_expression*>(this); 5503 if (ue->op() == OPERATOR_MULT) 5504 return ue->operand(); 5505 } 5506 return this; 5507 } 5508 5509 // Class Binary_expression. 5510 5511 // Traversal. 5512 5513 int 5514 Binary_expression::do_traverse(Traverse* traverse) 5515 { 5516 int t = Expression::traverse(&this->left_, traverse); 5517 if (t == TRAVERSE_EXIT) 5518 return TRAVERSE_EXIT; 5519 return Expression::traverse(&this->right_, traverse); 5520 } 5521 5522 // Return whether this expression may be used as a static initializer. 5523 5524 bool 5525 Binary_expression::do_is_static_initializer() const 5526 { 5527 if (!this->left_->is_static_initializer() 5528 || !this->right_->is_static_initializer()) 5529 return false; 5530 5531 // Addresses can be static initializers, but we can't implement 5532 // arbitray binary expressions of them. 5533 Unary_expression* lu = this->left_->unary_expression(); 5534 Unary_expression* ru = this->right_->unary_expression(); 5535 if (lu != NULL && lu->op() == OPERATOR_AND) 5536 { 5537 if (ru != NULL && ru->op() == OPERATOR_AND) 5538 return this->op_ == OPERATOR_MINUS; 5539 else 5540 return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; 5541 } 5542 else if (ru != NULL && ru->op() == OPERATOR_AND) 5543 return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; 5544 5545 // Other cases should resolve in the backend. 5546 return true; 5547 } 5548 5549 // Return the type to use for a binary operation on operands of 5550 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as 5551 // such may be NULL or abstract. 5552 5553 bool 5554 Binary_expression::operation_type(Operator op, Type* left_type, 5555 Type* right_type, Type** result_type) 5556 { 5557 if (left_type != right_type 5558 && !left_type->is_abstract() 5559 && !right_type->is_abstract() 5560 && left_type->base() != right_type->base() 5561 && op != OPERATOR_LSHIFT 5562 && op != OPERATOR_RSHIFT) 5563 { 5564 // May be a type error--let it be diagnosed elsewhere. 5565 return false; 5566 } 5567 5568 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) 5569 { 5570 if (left_type->integer_type() != NULL) 5571 *result_type = left_type; 5572 else 5573 *result_type = Type::make_abstract_integer_type(); 5574 } 5575 else if (!left_type->is_abstract() && left_type->named_type() != NULL) 5576 *result_type = left_type; 5577 else if (!right_type->is_abstract() && right_type->named_type() != NULL) 5578 *result_type = right_type; 5579 else if (!left_type->is_abstract()) 5580 *result_type = left_type; 5581 else if (!right_type->is_abstract()) 5582 *result_type = right_type; 5583 else if (left_type->complex_type() != NULL) 5584 *result_type = left_type; 5585 else if (right_type->complex_type() != NULL) 5586 *result_type = right_type; 5587 else if (left_type->float_type() != NULL) 5588 *result_type = left_type; 5589 else if (right_type->float_type() != NULL) 5590 *result_type = right_type; 5591 else if (left_type->integer_type() != NULL 5592 && left_type->integer_type()->is_rune()) 5593 *result_type = left_type; 5594 else if (right_type->integer_type() != NULL 5595 && right_type->integer_type()->is_rune()) 5596 *result_type = right_type; 5597 else 5598 *result_type = left_type; 5599 5600 return true; 5601 } 5602 5603 // Convert an integer comparison code and an operator to a boolean 5604 // value. 5605 5606 bool 5607 Binary_expression::cmp_to_bool(Operator op, int cmp) 5608 { 5609 switch (op) 5610 { 5611 case OPERATOR_EQEQ: 5612 return cmp == 0; 5613 break; 5614 case OPERATOR_NOTEQ: 5615 return cmp != 0; 5616 break; 5617 case OPERATOR_LT: 5618 return cmp < 0; 5619 break; 5620 case OPERATOR_LE: 5621 return cmp <= 0; 5622 case OPERATOR_GT: 5623 return cmp > 0; 5624 case OPERATOR_GE: 5625 return cmp >= 0; 5626 default: 5627 go_unreachable(); 5628 } 5629 } 5630 5631 // Compare constants according to OP. 5632 5633 bool 5634 Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, 5635 Numeric_constant* right_nc, 5636 Location location, bool* result) 5637 { 5638 Type* left_type = left_nc->type(); 5639 Type* right_type = right_nc->type(); 5640 5641 Type* type; 5642 if (!Binary_expression::operation_type(op, left_type, right_type, &type)) 5643 return false; 5644 5645 // When comparing an untyped operand to a typed operand, we are 5646 // effectively coercing the untyped operand to the other operand's 5647 // type, so make sure that is valid. 5648 if (!left_nc->set_type(type, true, location) 5649 || !right_nc->set_type(type, true, location)) 5650 return false; 5651 5652 bool ret; 5653 int cmp; 5654 if (type->complex_type() != NULL) 5655 { 5656 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) 5657 return false; 5658 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); 5659 } 5660 else if (type->float_type() != NULL) 5661 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); 5662 else 5663 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); 5664 5665 if (ret) 5666 *result = Binary_expression::cmp_to_bool(op, cmp); 5667 5668 return ret; 5669 } 5670 5671 // Compare integer constants. 5672 5673 bool 5674 Binary_expression::compare_integer(const Numeric_constant* left_nc, 5675 const Numeric_constant* right_nc, 5676 int* cmp) 5677 { 5678 mpz_t left_val; 5679 if (!left_nc->to_int(&left_val)) 5680 return false; 5681 mpz_t right_val; 5682 if (!right_nc->to_int(&right_val)) 5683 { 5684 mpz_clear(left_val); 5685 return false; 5686 } 5687 5688 *cmp = mpz_cmp(left_val, right_val); 5689 5690 mpz_clear(left_val); 5691 mpz_clear(right_val); 5692 5693 return true; 5694 } 5695 5696 // Compare floating point constants. 5697 5698 bool 5699 Binary_expression::compare_float(const Numeric_constant* left_nc, 5700 const Numeric_constant* right_nc, 5701 int* cmp) 5702 { 5703 mpfr_t left_val; 5704 if (!left_nc->to_float(&left_val)) 5705 return false; 5706 mpfr_t right_val; 5707 if (!right_nc->to_float(&right_val)) 5708 { 5709 mpfr_clear(left_val); 5710 return false; 5711 } 5712 5713 // We already coerced both operands to the same type. If that type 5714 // is not an abstract type, we need to round the values accordingly. 5715 Type* type = left_nc->type(); 5716 if (!type->is_abstract() && type->float_type() != NULL) 5717 { 5718 int bits = type->float_type()->bits(); 5719 mpfr_prec_round(left_val, bits, MPFR_RNDN); 5720 mpfr_prec_round(right_val, bits, MPFR_RNDN); 5721 } 5722 5723 *cmp = mpfr_cmp(left_val, right_val); 5724 5725 mpfr_clear(left_val); 5726 mpfr_clear(right_val); 5727 5728 return true; 5729 } 5730 5731 // Compare complex constants. Complex numbers may only be compared 5732 // for equality. 5733 5734 bool 5735 Binary_expression::compare_complex(const Numeric_constant* left_nc, 5736 const Numeric_constant* right_nc, 5737 int* cmp) 5738 { 5739 mpc_t left_val; 5740 if (!left_nc->to_complex(&left_val)) 5741 return false; 5742 mpc_t right_val; 5743 if (!right_nc->to_complex(&right_val)) 5744 { 5745 mpc_clear(left_val); 5746 return false; 5747 } 5748 5749 // We already coerced both operands to the same type. If that type 5750 // is not an abstract type, we need to round the values accordingly. 5751 Type* type = left_nc->type(); 5752 if (!type->is_abstract() && type->complex_type() != NULL) 5753 { 5754 int bits = type->complex_type()->bits(); 5755 mpfr_prec_round(mpc_realref(left_val), bits / 2, MPFR_RNDN); 5756 mpfr_prec_round(mpc_imagref(left_val), bits / 2, MPFR_RNDN); 5757 mpfr_prec_round(mpc_realref(right_val), bits / 2, MPFR_RNDN); 5758 mpfr_prec_round(mpc_imagref(right_val), bits / 2, MPFR_RNDN); 5759 } 5760 5761 *cmp = mpc_cmp(left_val, right_val) != 0; 5762 5763 mpc_clear(left_val); 5764 mpc_clear(right_val); 5765 5766 return true; 5767 } 5768 5769 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return 5770 // true if this could be done, false if not. Issue errors at LOCATION 5771 // as appropriate, and sets *ISSUED_ERROR if it did. 5772 5773 bool 5774 Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, 5775 Numeric_constant* right_nc, 5776 Location location, Numeric_constant* nc, 5777 bool* issued_error) 5778 { 5779 *issued_error = false; 5780 switch (op) 5781 { 5782 case OPERATOR_OROR: 5783 case OPERATOR_ANDAND: 5784 case OPERATOR_EQEQ: 5785 case OPERATOR_NOTEQ: 5786 case OPERATOR_LT: 5787 case OPERATOR_LE: 5788 case OPERATOR_GT: 5789 case OPERATOR_GE: 5790 // These return boolean values, not numeric. 5791 return false; 5792 default: 5793 break; 5794 } 5795 5796 Type* left_type = left_nc->type(); 5797 Type* right_type = right_nc->type(); 5798 5799 Type* type; 5800 if (!Binary_expression::operation_type(op, left_type, right_type, &type)) 5801 return false; 5802 5803 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; 5804 5805 // When combining an untyped operand with a typed operand, we are 5806 // effectively coercing the untyped operand to the other operand's 5807 // type, so make sure that is valid. 5808 if (!left_nc->set_type(type, true, location)) 5809 return false; 5810 if (!is_shift && !right_nc->set_type(type, true, location)) 5811 return false; 5812 if (is_shift 5813 && ((left_type->integer_type() == NULL 5814 && !left_type->is_abstract()) 5815 || (right_type->integer_type() == NULL 5816 && !right_type->is_abstract()))) 5817 return false; 5818 5819 bool r; 5820 if (type->complex_type() != NULL) 5821 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); 5822 else if (type->float_type() != NULL) 5823 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); 5824 else 5825 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); 5826 5827 if (r) 5828 { 5829 r = nc->set_type(type, true, location); 5830 if (!r) 5831 *issued_error = true; 5832 } 5833 5834 return r; 5835 } 5836 5837 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using 5838 // integer operations. Return true if this could be done, false if 5839 // not. 5840 5841 bool 5842 Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, 5843 const Numeric_constant* right_nc, 5844 Location location, Numeric_constant* nc) 5845 { 5846 mpz_t left_val; 5847 if (!left_nc->to_int(&left_val)) 5848 return false; 5849 mpz_t right_val; 5850 if (!right_nc->to_int(&right_val)) 5851 { 5852 mpz_clear(left_val); 5853 return false; 5854 } 5855 5856 mpz_t val; 5857 mpz_init(val); 5858 5859 switch (op) 5860 { 5861 case OPERATOR_PLUS: 5862 mpz_add(val, left_val, right_val); 5863 if (mpz_sizeinbase(val, 2) > 0x100000) 5864 { 5865 go_error_at(location, "constant addition overflow"); 5866 nc->set_invalid(); 5867 mpz_set_ui(val, 1); 5868 } 5869 break; 5870 case OPERATOR_MINUS: 5871 mpz_sub(val, left_val, right_val); 5872 if (mpz_sizeinbase(val, 2) > 0x100000) 5873 { 5874 go_error_at(location, "constant subtraction overflow"); 5875 nc->set_invalid(); 5876 mpz_set_ui(val, 1); 5877 } 5878 break; 5879 case OPERATOR_OR: 5880 mpz_ior(val, left_val, right_val); 5881 break; 5882 case OPERATOR_XOR: 5883 mpz_xor(val, left_val, right_val); 5884 break; 5885 case OPERATOR_MULT: 5886 mpz_mul(val, left_val, right_val); 5887 if (mpz_sizeinbase(val, 2) > 0x100000) 5888 { 5889 go_error_at(location, "constant multiplication overflow"); 5890 nc->set_invalid(); 5891 mpz_set_ui(val, 1); 5892 } 5893 break; 5894 case OPERATOR_DIV: 5895 if (mpz_sgn(right_val) != 0) 5896 mpz_tdiv_q(val, left_val, right_val); 5897 else 5898 { 5899 go_error_at(location, "division by zero"); 5900 nc->set_invalid(); 5901 mpz_set_ui(val, 0); 5902 } 5903 break; 5904 case OPERATOR_MOD: 5905 if (mpz_sgn(right_val) != 0) 5906 mpz_tdiv_r(val, left_val, right_val); 5907 else 5908 { 5909 go_error_at(location, "division by zero"); 5910 nc->set_invalid(); 5911 mpz_set_ui(val, 0); 5912 } 5913 break; 5914 case OPERATOR_LSHIFT: 5915 { 5916 unsigned long shift = mpz_get_ui(right_val); 5917 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) 5918 mpz_mul_2exp(val, left_val, shift); 5919 else 5920 { 5921 go_error_at(location, "shift count overflow"); 5922 nc->set_invalid(); 5923 mpz_set_ui(val, 1); 5924 } 5925 break; 5926 } 5927 break; 5928 case OPERATOR_RSHIFT: 5929 { 5930 unsigned long shift = mpz_get_ui(right_val); 5931 if (mpz_cmp_ui(right_val, shift) != 0) 5932 { 5933 go_error_at(location, "shift count overflow"); 5934 nc->set_invalid(); 5935 mpz_set_ui(val, 1); 5936 } 5937 else 5938 { 5939 if (mpz_cmp_ui(left_val, 0) >= 0) 5940 mpz_tdiv_q_2exp(val, left_val, shift); 5941 else 5942 mpz_fdiv_q_2exp(val, left_val, shift); 5943 } 5944 break; 5945 } 5946 break; 5947 case OPERATOR_AND: 5948 mpz_and(val, left_val, right_val); 5949 break; 5950 case OPERATOR_BITCLEAR: 5951 { 5952 mpz_t tval; 5953 mpz_init(tval); 5954 mpz_com(tval, right_val); 5955 mpz_and(val, left_val, tval); 5956 mpz_clear(tval); 5957 } 5958 break; 5959 default: 5960 go_unreachable(); 5961 } 5962 5963 mpz_clear(left_val); 5964 mpz_clear(right_val); 5965 5966 if (left_nc->is_rune() 5967 || (op != OPERATOR_LSHIFT 5968 && op != OPERATOR_RSHIFT 5969 && right_nc->is_rune())) 5970 nc->set_rune(NULL, val); 5971 else 5972 nc->set_int(NULL, val); 5973 5974 mpz_clear(val); 5975 5976 return true; 5977 } 5978 5979 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using 5980 // floating point operations. Return true if this could be done, 5981 // false if not. 5982 5983 bool 5984 Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, 5985 const Numeric_constant* right_nc, 5986 Location location, Numeric_constant* nc) 5987 { 5988 mpfr_t left_val; 5989 if (!left_nc->to_float(&left_val)) 5990 return false; 5991 mpfr_t right_val; 5992 if (!right_nc->to_float(&right_val)) 5993 { 5994 mpfr_clear(left_val); 5995 return false; 5996 } 5997 5998 mpfr_t val; 5999 mpfr_init(val); 6000 6001 bool ret = true; 6002 switch (op) 6003 { 6004 case OPERATOR_PLUS: 6005 mpfr_add(val, left_val, right_val, MPFR_RNDN); 6006 break; 6007 case OPERATOR_MINUS: 6008 mpfr_sub(val, left_val, right_val, MPFR_RNDN); 6009 break; 6010 case OPERATOR_OR: 6011 case OPERATOR_XOR: 6012 case OPERATOR_AND: 6013 case OPERATOR_BITCLEAR: 6014 case OPERATOR_MOD: 6015 case OPERATOR_LSHIFT: 6016 case OPERATOR_RSHIFT: 6017 mpfr_set_ui(val, 0, MPFR_RNDN); 6018 ret = false; 6019 break; 6020 case OPERATOR_MULT: 6021 mpfr_mul(val, left_val, right_val, MPFR_RNDN); 6022 break; 6023 case OPERATOR_DIV: 6024 if (!mpfr_zero_p(right_val)) 6025 mpfr_div(val, left_val, right_val, MPFR_RNDN); 6026 else 6027 { 6028 go_error_at(location, "division by zero"); 6029 nc->set_invalid(); 6030 mpfr_set_ui(val, 0, MPFR_RNDN); 6031 } 6032 break; 6033 default: 6034 go_unreachable(); 6035 } 6036 6037 mpfr_clear(left_val); 6038 mpfr_clear(right_val); 6039 6040 nc->set_float(NULL, val); 6041 mpfr_clear(val); 6042 6043 return ret; 6044 } 6045 6046 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using 6047 // complex operations. Return true if this could be done, false if 6048 // not. 6049 6050 bool 6051 Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, 6052 const Numeric_constant* right_nc, 6053 Location location, Numeric_constant* nc) 6054 { 6055 mpc_t left_val; 6056 if (!left_nc->to_complex(&left_val)) 6057 return false; 6058 mpc_t right_val; 6059 if (!right_nc->to_complex(&right_val)) 6060 { 6061 mpc_clear(left_val); 6062 return false; 6063 } 6064 6065 mpc_t val; 6066 mpc_init2(val, mpc_precision); 6067 6068 bool ret = true; 6069 switch (op) 6070 { 6071 case OPERATOR_PLUS: 6072 mpc_add(val, left_val, right_val, MPC_RNDNN); 6073 break; 6074 case OPERATOR_MINUS: 6075 mpc_sub(val, left_val, right_val, MPC_RNDNN); 6076 break; 6077 case OPERATOR_OR: 6078 case OPERATOR_XOR: 6079 case OPERATOR_AND: 6080 case OPERATOR_BITCLEAR: 6081 case OPERATOR_MOD: 6082 case OPERATOR_LSHIFT: 6083 case OPERATOR_RSHIFT: 6084 mpc_set_ui(val, 0, MPC_RNDNN); 6085 ret = false; 6086 break; 6087 case OPERATOR_MULT: 6088 mpc_mul(val, left_val, right_val, MPC_RNDNN); 6089 break; 6090 case OPERATOR_DIV: 6091 if (mpc_cmp_si(right_val, 0) == 0) 6092 { 6093 go_error_at(location, "division by zero"); 6094 nc->set_invalid(); 6095 mpc_set_ui(val, 0, MPC_RNDNN); 6096 break; 6097 } 6098 mpc_div(val, left_val, right_val, MPC_RNDNN); 6099 break; 6100 default: 6101 go_unreachable(); 6102 } 6103 6104 mpc_clear(left_val); 6105 mpc_clear(right_val); 6106 6107 nc->set_complex(NULL, val); 6108 mpc_clear(val); 6109 6110 return ret; 6111 } 6112 6113 // Lower a binary expression. We have to evaluate constant 6114 // expressions now, in order to implement Go's unlimited precision 6115 // constants. 6116 6117 Expression* 6118 Binary_expression::do_lower(Gogo* gogo, Named_object*, 6119 Statement_inserter* inserter, int) 6120 { 6121 Location location = this->location(); 6122 Operator op = this->op_; 6123 Expression* left = this->left_; 6124 Expression* right = this->right_; 6125 6126 const bool is_comparison = (op == OPERATOR_EQEQ 6127 || op == OPERATOR_NOTEQ 6128 || op == OPERATOR_LT 6129 || op == OPERATOR_LE 6130 || op == OPERATOR_GT 6131 || op == OPERATOR_GE); 6132 6133 // Numeric constant expressions. 6134 { 6135 Numeric_constant left_nc; 6136 Numeric_constant right_nc; 6137 if (left->numeric_constant_value(&left_nc) 6138 && right->numeric_constant_value(&right_nc)) 6139 { 6140 if (is_comparison) 6141 { 6142 bool result; 6143 if (!Binary_expression::compare_constant(op, &left_nc, 6144 &right_nc, location, 6145 &result)) 6146 return this; 6147 return Expression::make_boolean(result, location); 6148 } 6149 else 6150 { 6151 Numeric_constant nc; 6152 bool issued_error; 6153 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, 6154 location, &nc, 6155 &issued_error)) 6156 { 6157 if (issued_error) 6158 return Expression::make_error(location); 6159 return this; 6160 } 6161 return nc.expression(location); 6162 } 6163 } 6164 } 6165 6166 // String constant expressions. 6167 // 6168 // Avoid constant folding here if the left and right types are incompatible 6169 // (leave the operation intact so that the type checker can complain about it 6170 // later on). If concatenating an abstract string with a named string type, 6171 // result type needs to be of the named type (see issue 31412). 6172 if (left->type()->is_string_type() 6173 && right->type()->is_string_type() 6174 && (left->type()->named_type() == NULL 6175 || right->type()->named_type() == NULL 6176 || left->type()->named_type() == right->type()->named_type())) 6177 { 6178 std::string left_string; 6179 std::string right_string; 6180 if (left->string_constant_value(&left_string) 6181 && right->string_constant_value(&right_string)) 6182 { 6183 if (op == OPERATOR_PLUS) 6184 { 6185 Type* result_type = (left->type()->named_type() != NULL 6186 ? left->type() 6187 : right->type()); 6188 delete left; 6189 delete right; 6190 return Expression::make_string_typed(left_string + right_string, 6191 result_type, location); 6192 } 6193 else if (is_comparison) 6194 { 6195 int cmp = left_string.compare(right_string); 6196 bool r = Binary_expression::cmp_to_bool(op, cmp); 6197 delete left; 6198 delete right; 6199 return Expression::make_boolean(r, location); 6200 } 6201 } 6202 } 6203 6204 // Lower struct, array, and some interface comparisons. 6205 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) 6206 { 6207 if (left->type()->struct_type() != NULL 6208 && right->type()->struct_type() != NULL) 6209 return this->lower_struct_comparison(gogo, inserter); 6210 else if (left->type()->array_type() != NULL 6211 && !left->type()->is_slice_type() 6212 && right->type()->array_type() != NULL 6213 && !right->type()->is_slice_type()) 6214 return this->lower_array_comparison(gogo, inserter); 6215 else if ((left->type()->interface_type() != NULL 6216 && right->type()->interface_type() == NULL) 6217 || (left->type()->interface_type() == NULL 6218 && right->type()->interface_type() != NULL)) 6219 return this->lower_interface_value_comparison(gogo, inserter); 6220 } 6221 6222 // Lower string concatenation to String_concat_expression, so that 6223 // we can group sequences of string additions. 6224 if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) 6225 { 6226 Expression_list* exprs; 6227 String_concat_expression* left_sce = 6228 this->left_->string_concat_expression(); 6229 if (left_sce != NULL) 6230 exprs = left_sce->exprs(); 6231 else 6232 { 6233 exprs = new Expression_list(); 6234 exprs->push_back(this->left_); 6235 } 6236 6237 String_concat_expression* right_sce = 6238 this->right_->string_concat_expression(); 6239 if (right_sce != NULL) 6240 exprs->append(right_sce->exprs()); 6241 else 6242 exprs->push_back(this->right_); 6243 6244 return Expression::make_string_concat(exprs); 6245 } 6246 6247 return this; 6248 } 6249 6250 // Lower a struct comparison. 6251 6252 Expression* 6253 Binary_expression::lower_struct_comparison(Gogo* gogo, 6254 Statement_inserter* inserter) 6255 { 6256 Struct_type* st = this->left_->type()->struct_type(); 6257 Struct_type* st2 = this->right_->type()->struct_type(); 6258 if (st2 == NULL) 6259 return this; 6260 if (st != st2 6261 && !Type::are_identical(st, st2, 6262 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 6263 NULL)) 6264 return this; 6265 if (!Type::are_compatible_for_comparison(true, this->left_->type(), 6266 this->right_->type(), NULL)) 6267 return this; 6268 6269 // See if we can compare using memcmp. As a heuristic, we use 6270 // memcmp rather than field references and comparisons if there are 6271 // more than two fields. 6272 if (st->compare_is_identity(gogo) && st->total_field_count() > 2) 6273 return this->lower_compare_to_memcmp(gogo, inserter); 6274 6275 Location loc = this->location(); 6276 6277 Expression* left = this->left_; 6278 Temporary_statement* left_temp = NULL; 6279 if (left->var_expression() == NULL 6280 && left->temporary_reference_expression() == NULL) 6281 { 6282 left_temp = Statement::make_temporary(left->type(), NULL, loc); 6283 inserter->insert(left_temp); 6284 left = Expression::make_set_and_use_temporary(left_temp, left, loc); 6285 } 6286 6287 Expression* right = this->right_; 6288 Temporary_statement* right_temp = NULL; 6289 if (right->var_expression() == NULL 6290 && right->temporary_reference_expression() == NULL) 6291 { 6292 right_temp = Statement::make_temporary(right->type(), NULL, loc); 6293 inserter->insert(right_temp); 6294 right = Expression::make_set_and_use_temporary(right_temp, right, loc); 6295 } 6296 6297 Expression* ret = Expression::make_boolean(true, loc); 6298 const Struct_field_list* fields = st->fields(); 6299 unsigned int field_index = 0; 6300 for (Struct_field_list::const_iterator pf = fields->begin(); 6301 pf != fields->end(); 6302 ++pf, ++field_index) 6303 { 6304 if (Gogo::is_sink_name(pf->field_name())) 6305 continue; 6306 6307 if (field_index > 0) 6308 { 6309 if (left_temp == NULL) 6310 left = left->copy(); 6311 else 6312 left = Expression::make_temporary_reference(left_temp, loc); 6313 if (right_temp == NULL) 6314 right = right->copy(); 6315 else 6316 right = Expression::make_temporary_reference(right_temp, loc); 6317 } 6318 Expression* f1 = Expression::make_field_reference(left, field_index, 6319 loc); 6320 Expression* f2 = Expression::make_field_reference(right, field_index, 6321 loc); 6322 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); 6323 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); 6324 } 6325 6326 if (this->op_ == OPERATOR_NOTEQ) 6327 ret = Expression::make_unary(OPERATOR_NOT, ret, loc); 6328 6329 return ret; 6330 } 6331 6332 // Lower an array comparison. 6333 6334 Expression* 6335 Binary_expression::lower_array_comparison(Gogo* gogo, 6336 Statement_inserter* inserter) 6337 { 6338 Array_type* at = this->left_->type()->array_type(); 6339 Array_type* at2 = this->right_->type()->array_type(); 6340 if (at2 == NULL) 6341 return this; 6342 if (at != at2 6343 && !Type::are_identical(at, at2, 6344 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 6345 NULL)) 6346 return this; 6347 if (!Type::are_compatible_for_comparison(true, this->left_->type(), 6348 this->right_->type(), NULL)) 6349 return this; 6350 6351 // Call memcmp directly if possible. This may let the middle-end 6352 // optimize the call. 6353 if (at->compare_is_identity(gogo)) 6354 return this->lower_compare_to_memcmp(gogo, inserter); 6355 6356 // Call the array comparison function. 6357 Named_object* equal_fn = 6358 at->equal_function(gogo, this->left_->type()->named_type(), NULL); 6359 6360 Location loc = this->location(); 6361 6362 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); 6363 6364 Expression_list* args = new Expression_list(); 6365 args->push_back(this->operand_address(inserter, this->left_)); 6366 args->push_back(this->operand_address(inserter, this->right_)); 6367 6368 Call_expression* ce = Expression::make_call(func, args, false, loc); 6369 6370 // Record that this is a call to a generated equality function. We 6371 // need to do this because a comparison returns an abstract boolean 6372 // type, but the function necessarily returns "bool". The 6373 // difference shows up in code like 6374 // type mybool bool 6375 // var b mybool = [10]string{} == [10]string{} 6376 // The comparison function returns "bool", but since a comparison 6377 // has an abstract boolean type we need an implicit conversion to 6378 // "mybool". The implicit conversion is inserted in 6379 // Call_expression::do_flatten. 6380 ce->set_is_equal_function(); 6381 6382 Expression* ret = ce; 6383 if (this->op_ == OPERATOR_NOTEQ) 6384 ret = Expression::make_unary(OPERATOR_NOT, ret, loc); 6385 6386 return ret; 6387 } 6388 6389 // Lower an interface to value comparison. 6390 6391 Expression* 6392 Binary_expression::lower_interface_value_comparison(Gogo*, 6393 Statement_inserter* inserter) 6394 { 6395 Type* left_type = this->left_->type(); 6396 Type* right_type = this->right_->type(); 6397 Interface_type* ift; 6398 if (left_type->interface_type() != NULL) 6399 { 6400 ift = left_type->interface_type(); 6401 if (!ift->implements_interface(right_type, NULL)) 6402 return this; 6403 } 6404 else 6405 { 6406 ift = right_type->interface_type(); 6407 if (!ift->implements_interface(left_type, NULL)) 6408 return this; 6409 } 6410 if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) 6411 return this; 6412 6413 Location loc = this->location(); 6414 6415 if (left_type->interface_type() == NULL 6416 && left_type->points_to() == NULL 6417 && !this->left_->is_addressable()) 6418 { 6419 Temporary_statement* temp = 6420 Statement::make_temporary(left_type, NULL, loc); 6421 inserter->insert(temp); 6422 this->left_ = 6423 Expression::make_set_and_use_temporary(temp, this->left_, loc); 6424 } 6425 6426 if (right_type->interface_type() == NULL 6427 && right_type->points_to() == NULL 6428 && !this->right_->is_addressable()) 6429 { 6430 Temporary_statement* temp = 6431 Statement::make_temporary(right_type, NULL, loc); 6432 inserter->insert(temp); 6433 this->right_ = 6434 Expression::make_set_and_use_temporary(temp, this->right_, loc); 6435 } 6436 6437 return this; 6438 } 6439 6440 // Lower a struct or array comparison to a call to memcmp. 6441 6442 Expression* 6443 Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) 6444 { 6445 Location loc = this->location(); 6446 6447 Expression* a1 = this->operand_address(inserter, this->left_); 6448 Expression* a2 = this->operand_address(inserter, this->right_); 6449 Expression* len = Expression::make_type_info(this->left_->type(), 6450 TYPE_INFO_SIZE); 6451 6452 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); 6453 Type* int32_type = Type::lookup_integer_type("int32"); 6454 Expression* zero = Expression::make_integer_ul(0, int32_type, loc); 6455 return Expression::make_binary(this->op_, call, zero, loc); 6456 } 6457 6458 Expression* 6459 Binary_expression::do_flatten(Gogo* gogo, Named_object*, 6460 Statement_inserter* inserter) 6461 { 6462 Location loc = this->location(); 6463 if (this->left_->type()->is_error_type() 6464 || this->right_->type()->is_error_type() 6465 || this->left_->is_error_expression() 6466 || this->right_->is_error_expression()) 6467 { 6468 go_assert(saw_errors()); 6469 return Expression::make_error(loc); 6470 } 6471 6472 Temporary_statement* temp; 6473 6474 Type* left_type = this->left_->type(); 6475 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT 6476 || this->op_ == OPERATOR_RSHIFT); 6477 bool is_idiv_op = ((this->op_ == OPERATOR_DIV && 6478 left_type->integer_type() != NULL) 6479 || this->op_ == OPERATOR_MOD); 6480 bool is_string_op = (left_type->is_string_type() 6481 && this->right_->type()->is_string_type()); 6482 6483 if (is_string_op) 6484 { 6485 // Mark string([]byte) operands to reuse the backing store. 6486 // String comparison does not keep the reference, so it is safe. 6487 Type_conversion_expression* lce = 6488 this->left_->conversion_expression(); 6489 if (lce != NULL && lce->expr()->type()->is_slice_type()) 6490 lce->set_no_copy(true); 6491 Type_conversion_expression* rce = 6492 this->right_->conversion_expression(); 6493 if (rce != NULL && rce->expr()->type()->is_slice_type()) 6494 rce->set_no_copy(true); 6495 } 6496 6497 if (is_shift_op 6498 || (is_idiv_op 6499 && (gogo->check_divide_by_zero() || gogo->check_divide_overflow())) 6500 || is_string_op) 6501 { 6502 if (!this->left_->is_multi_eval_safe()) 6503 { 6504 temp = Statement::make_temporary(NULL, this->left_, loc); 6505 inserter->insert(temp); 6506 this->left_ = Expression::make_temporary_reference(temp, loc); 6507 } 6508 if (!this->right_->is_multi_eval_safe()) 6509 { 6510 temp = 6511 Statement::make_temporary(NULL, this->right_, loc); 6512 this->right_ = Expression::make_temporary_reference(temp, loc); 6513 inserter->insert(temp); 6514 } 6515 } 6516 return this; 6517 } 6518 6519 6520 // Return the address of EXPR, cast to unsafe.Pointer. 6521 6522 Expression* 6523 Binary_expression::operand_address(Statement_inserter* inserter, 6524 Expression* expr) 6525 { 6526 Location loc = this->location(); 6527 6528 if (!expr->is_addressable()) 6529 { 6530 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, 6531 loc); 6532 inserter->insert(temp); 6533 expr = Expression::make_set_and_use_temporary(temp, expr, loc); 6534 } 6535 expr = Expression::make_unary(OPERATOR_AND, expr, loc); 6536 static_cast<Unary_expression*>(expr)->set_does_not_escape(); 6537 Type* void_type = Type::make_void_type(); 6538 Type* unsafe_pointer_type = Type::make_pointer_type(void_type); 6539 return Expression::make_cast(unsafe_pointer_type, expr, loc); 6540 } 6541 6542 // Return the numeric constant value, if it has one. 6543 6544 bool 6545 Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const 6546 { 6547 Numeric_constant left_nc; 6548 if (!this->left_->numeric_constant_value(&left_nc)) 6549 return false; 6550 Numeric_constant right_nc; 6551 if (!this->right_->numeric_constant_value(&right_nc)) 6552 return false; 6553 bool issued_error; 6554 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, 6555 this->location(), nc, &issued_error); 6556 } 6557 6558 // Return the boolean constant value, if it has one. 6559 6560 bool 6561 Binary_expression::do_boolean_constant_value(bool* val) const 6562 { 6563 bool is_comparison = false; 6564 switch (this->op_) 6565 { 6566 case OPERATOR_EQEQ: 6567 case OPERATOR_NOTEQ: 6568 case OPERATOR_LT: 6569 case OPERATOR_LE: 6570 case OPERATOR_GT: 6571 case OPERATOR_GE: 6572 is_comparison = true; 6573 break; 6574 case OPERATOR_ANDAND: 6575 case OPERATOR_OROR: 6576 break; 6577 default: 6578 return false; 6579 } 6580 6581 Numeric_constant left_nc, right_nc; 6582 if (is_comparison 6583 && this->left_->numeric_constant_value(&left_nc) 6584 && this->right_->numeric_constant_value(&right_nc)) 6585 return Binary_expression::compare_constant(this->op_, &left_nc, 6586 &right_nc, 6587 this->location(), 6588 val); 6589 6590 std::string left_str, right_str; 6591 if (is_comparison 6592 && this->left_->string_constant_value(&left_str) 6593 && this->right_->string_constant_value(&right_str)) 6594 { 6595 *val = Binary_expression::cmp_to_bool(this->op_, 6596 left_str.compare(right_str)); 6597 return true; 6598 } 6599 6600 bool left_bval; 6601 if (this->left_->boolean_constant_value(&left_bval)) 6602 { 6603 if (this->op_ == OPERATOR_ANDAND && !left_bval) 6604 { 6605 *val = false; 6606 return true; 6607 } 6608 else if (this->op_ == OPERATOR_OROR && left_bval) 6609 { 6610 *val = true; 6611 return true; 6612 } 6613 6614 bool right_bval; 6615 if (this->right_->boolean_constant_value(&right_bval)) 6616 { 6617 switch (this->op_) 6618 { 6619 case OPERATOR_EQEQ: 6620 *val = (left_bval == right_bval); 6621 return true; 6622 case OPERATOR_NOTEQ: 6623 *val = (left_bval != right_bval); 6624 return true; 6625 case OPERATOR_ANDAND: 6626 case OPERATOR_OROR: 6627 *val = right_bval; 6628 return true; 6629 default: 6630 go_unreachable(); 6631 } 6632 } 6633 } 6634 6635 return false; 6636 } 6637 6638 // Note that the value is being discarded. 6639 6640 bool 6641 Binary_expression::do_discarding_value() 6642 { 6643 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) 6644 return this->right_->discarding_value(); 6645 else 6646 { 6647 this->unused_value_error(); 6648 return false; 6649 } 6650 } 6651 6652 // Get type. 6653 6654 Type* 6655 Binary_expression::do_type() 6656 { 6657 if (this->classification() == EXPRESSION_ERROR) 6658 return Type::make_error_type(); 6659 6660 switch (this->op_) 6661 { 6662 case OPERATOR_EQEQ: 6663 case OPERATOR_NOTEQ: 6664 case OPERATOR_LT: 6665 case OPERATOR_LE: 6666 case OPERATOR_GT: 6667 case OPERATOR_GE: 6668 if (this->type_ == NULL) 6669 this->type_ = Type::make_boolean_type(); 6670 return this->type_; 6671 6672 case OPERATOR_PLUS: 6673 case OPERATOR_MINUS: 6674 case OPERATOR_OR: 6675 case OPERATOR_XOR: 6676 case OPERATOR_MULT: 6677 case OPERATOR_DIV: 6678 case OPERATOR_MOD: 6679 case OPERATOR_AND: 6680 case OPERATOR_BITCLEAR: 6681 case OPERATOR_OROR: 6682 case OPERATOR_ANDAND: 6683 { 6684 Type* type; 6685 if (!Binary_expression::operation_type(this->op_, 6686 this->left_->type(), 6687 this->right_->type(), 6688 &type)) 6689 return Type::make_error_type(); 6690 return type; 6691 } 6692 6693 case OPERATOR_LSHIFT: 6694 case OPERATOR_RSHIFT: 6695 return this->left_->type(); 6696 6697 default: 6698 go_unreachable(); 6699 } 6700 } 6701 6702 // Set type for a binary expression. 6703 6704 void 6705 Binary_expression::do_determine_type(const Type_context* context) 6706 { 6707 Type* tleft = this->left_->type(); 6708 Type* tright = this->right_->type(); 6709 6710 // Both sides should have the same type, except for the shift 6711 // operations. For a comparison, we should ignore the incoming 6712 // type. 6713 6714 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT 6715 || this->op_ == OPERATOR_RSHIFT); 6716 6717 bool is_comparison = (this->op_ == OPERATOR_EQEQ 6718 || this->op_ == OPERATOR_NOTEQ 6719 || this->op_ == OPERATOR_LT 6720 || this->op_ == OPERATOR_LE 6721 || this->op_ == OPERATOR_GT 6722 || this->op_ == OPERATOR_GE); 6723 6724 // For constant expressions, the context of the result is not useful in 6725 // determining the types of the operands. It is only legal to use abstract 6726 // boolean, numeric, and string constants as operands where it is legal to 6727 // use non-abstract boolean, numeric, and string constants, respectively. 6728 // Any issues with the operation will be resolved in the check_types pass. 6729 bool is_constant_expr = (this->left_->is_constant() 6730 && this->right_->is_constant()); 6731 6732 Type_context subcontext(*context); 6733 6734 if (is_constant_expr && !is_shift_op) 6735 { 6736 subcontext.type = NULL; 6737 subcontext.may_be_abstract = true; 6738 } 6739 else if (is_comparison) 6740 { 6741 // In a comparison, the context does not determine the types of 6742 // the operands. 6743 subcontext.type = NULL; 6744 } 6745 6746 // Set the context for the left hand operand. 6747 if (is_shift_op) 6748 { 6749 // The right hand operand of a shift plays no role in 6750 // determining the type of the left hand operand. 6751 } 6752 else if (!tleft->is_abstract()) 6753 subcontext.type = tleft; 6754 else if (!tright->is_abstract()) 6755 subcontext.type = tright; 6756 else if (subcontext.type == NULL) 6757 { 6758 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) 6759 || (tleft->float_type() != NULL && tright->float_type() != NULL) 6760 || (tleft->complex_type() != NULL && tright->complex_type() != NULL) 6761 || (tleft->is_boolean_type() && tright->is_boolean_type())) 6762 { 6763 // Both sides have an abstract integer, abstract float, 6764 // abstract complex, or abstract boolean type. Just let 6765 // CONTEXT determine whether they may remain abstract or not. 6766 } 6767 else if (tleft->complex_type() != NULL) 6768 subcontext.type = tleft; 6769 else if (tright->complex_type() != NULL) 6770 subcontext.type = tright; 6771 else if (tleft->float_type() != NULL) 6772 subcontext.type = tleft; 6773 else if (tright->float_type() != NULL) 6774 subcontext.type = tright; 6775 else 6776 subcontext.type = tleft; 6777 6778 if (subcontext.type != NULL && !context->may_be_abstract) 6779 subcontext.type = subcontext.type->make_non_abstract_type(); 6780 } 6781 6782 this->left_->determine_type(&subcontext); 6783 6784 if (is_shift_op) 6785 { 6786 // We may have inherited an unusable type for the shift operand. 6787 // Give a useful error if that happened. 6788 if (tleft->is_abstract() 6789 && subcontext.type != NULL 6790 && !subcontext.may_be_abstract 6791 && subcontext.type->interface_type() == NULL 6792 && subcontext.type->integer_type() == NULL) 6793 this->report_error(("invalid context-determined non-integer type " 6794 "for left operand of shift")); 6795 6796 // The context for the right hand operand is the same as for the 6797 // left hand operand, except for a shift operator. 6798 subcontext.type = Type::lookup_integer_type("uint"); 6799 subcontext.may_be_abstract = false; 6800 } 6801 6802 this->right_->determine_type(&subcontext); 6803 6804 if (is_comparison) 6805 { 6806 if (this->type_ != NULL && !this->type_->is_abstract()) 6807 ; 6808 else if (context->type != NULL && context->type->is_boolean_type()) 6809 this->type_ = context->type; 6810 else if (!context->may_be_abstract) 6811 this->type_ = Type::lookup_bool_type(); 6812 } 6813 } 6814 6815 // Report an error if the binary operator OP does not support TYPE. 6816 // OTYPE is the type of the other operand. Return whether the 6817 // operation is OK. This should not be used for shift. 6818 6819 bool 6820 Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, 6821 Location location) 6822 { 6823 switch (op) 6824 { 6825 case OPERATOR_OROR: 6826 case OPERATOR_ANDAND: 6827 if (!type->is_boolean_type() 6828 || !otype->is_boolean_type()) 6829 { 6830 go_error_at(location, "expected boolean type"); 6831 return false; 6832 } 6833 break; 6834 6835 case OPERATOR_EQEQ: 6836 case OPERATOR_NOTEQ: 6837 { 6838 std::string reason; 6839 if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) 6840 { 6841 go_error_at(location, "%s", reason.c_str()); 6842 return false; 6843 } 6844 } 6845 break; 6846 6847 case OPERATOR_LT: 6848 case OPERATOR_LE: 6849 case OPERATOR_GT: 6850 case OPERATOR_GE: 6851 { 6852 std::string reason; 6853 if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) 6854 { 6855 go_error_at(location, "%s", reason.c_str()); 6856 return false; 6857 } 6858 } 6859 break; 6860 6861 case OPERATOR_PLUS: 6862 case OPERATOR_PLUSEQ: 6863 if ((!type->is_numeric_type() && !type->is_string_type()) 6864 || (!otype->is_numeric_type() && !otype->is_string_type())) 6865 { 6866 go_error_at(location, 6867 "expected integer, floating, complex, or string type"); 6868 return false; 6869 } 6870 break; 6871 6872 case OPERATOR_MINUS: 6873 case OPERATOR_MINUSEQ: 6874 case OPERATOR_MULT: 6875 case OPERATOR_MULTEQ: 6876 case OPERATOR_DIV: 6877 case OPERATOR_DIVEQ: 6878 if (!type->is_numeric_type() || !otype->is_numeric_type()) 6879 { 6880 go_error_at(location, "expected integer, floating, or complex type"); 6881 return false; 6882 } 6883 break; 6884 6885 case OPERATOR_MOD: 6886 case OPERATOR_MODEQ: 6887 case OPERATOR_OR: 6888 case OPERATOR_OREQ: 6889 case OPERATOR_AND: 6890 case OPERATOR_ANDEQ: 6891 case OPERATOR_XOR: 6892 case OPERATOR_XOREQ: 6893 case OPERATOR_BITCLEAR: 6894 case OPERATOR_BITCLEAREQ: 6895 if (type->integer_type() == NULL || otype->integer_type() == NULL) 6896 { 6897 go_error_at(location, "expected integer type"); 6898 return false; 6899 } 6900 break; 6901 6902 default: 6903 go_unreachable(); 6904 } 6905 6906 return true; 6907 } 6908 6909 // Check types. 6910 6911 void 6912 Binary_expression::do_check_types(Gogo*) 6913 { 6914 if (this->classification() == EXPRESSION_ERROR) 6915 return; 6916 6917 Type* left_type = this->left_->type(); 6918 Type* right_type = this->right_->type(); 6919 if (left_type->is_error() || right_type->is_error()) 6920 { 6921 this->set_is_error(); 6922 return; 6923 } 6924 6925 if (this->op_ == OPERATOR_EQEQ 6926 || this->op_ == OPERATOR_NOTEQ 6927 || this->op_ == OPERATOR_LT 6928 || this->op_ == OPERATOR_LE 6929 || this->op_ == OPERATOR_GT 6930 || this->op_ == OPERATOR_GE) 6931 { 6932 if (left_type->is_nil_type() && right_type->is_nil_type()) 6933 { 6934 this->report_error(_("invalid comparison of nil with nil")); 6935 return; 6936 } 6937 if (!Type::are_assignable(left_type, right_type, NULL) 6938 && !Type::are_assignable(right_type, left_type, NULL)) 6939 { 6940 this->report_error(_("incompatible types in binary expression")); 6941 return; 6942 } 6943 if (!Binary_expression::check_operator_type(this->op_, left_type, 6944 right_type, 6945 this->location()) 6946 || !Binary_expression::check_operator_type(this->op_, right_type, 6947 left_type, 6948 this->location())) 6949 { 6950 this->set_is_error(); 6951 return; 6952 } 6953 } 6954 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) 6955 { 6956 if (!Type::are_compatible_for_binop(left_type, right_type)) 6957 { 6958 this->report_error(_("incompatible types in binary expression")); 6959 return; 6960 } 6961 if (!Binary_expression::check_operator_type(this->op_, left_type, 6962 right_type, 6963 this->location())) 6964 { 6965 this->set_is_error(); 6966 return; 6967 } 6968 if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) 6969 { 6970 // Division by a zero integer constant is an error. 6971 Numeric_constant rconst; 6972 unsigned long rval; 6973 if (left_type->integer_type() != NULL 6974 && this->right_->numeric_constant_value(&rconst) 6975 && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID 6976 && rval == 0) 6977 { 6978 this->report_error(_("integer division by zero")); 6979 return; 6980 } 6981 } 6982 } 6983 else 6984 { 6985 if (left_type->integer_type() == NULL) 6986 this->report_error(_("shift of non-integer operand")); 6987 6988 if (right_type->is_string_type()) 6989 this->report_error(_("shift count not integer")); 6990 else if (!right_type->is_abstract() 6991 && right_type->integer_type() == NULL) 6992 this->report_error(_("shift count not integer")); 6993 else 6994 { 6995 Numeric_constant nc; 6996 if (this->right_->numeric_constant_value(&nc)) 6997 { 6998 mpz_t val; 6999 if (!nc.to_int(&val)) 7000 this->report_error(_("shift count not integer")); 7001 else 7002 { 7003 if (mpz_sgn(val) < 0) 7004 { 7005 this->report_error(_("negative shift count")); 7006 Location rloc = this->right_->location(); 7007 this->right_ = Expression::make_integer_ul(0, right_type, 7008 rloc); 7009 } 7010 mpz_clear(val); 7011 } 7012 } 7013 } 7014 } 7015 } 7016 7017 // Get the backend representation for a binary expression. 7018 7019 Bexpression* 7020 Binary_expression::do_get_backend(Translate_context* context) 7021 { 7022 Gogo* gogo = context->gogo(); 7023 Location loc = this->location(); 7024 Type* left_type = this->left_->type(); 7025 Type* right_type = this->right_->type(); 7026 7027 bool use_left_type = true; 7028 bool is_shift_op = false; 7029 bool is_idiv_op = false; 7030 switch (this->op_) 7031 { 7032 case OPERATOR_EQEQ: 7033 case OPERATOR_NOTEQ: 7034 case OPERATOR_LT: 7035 case OPERATOR_LE: 7036 case OPERATOR_GT: 7037 case OPERATOR_GE: 7038 return Expression::comparison(context, this->type_, this->op_, 7039 this->left_, this->right_, loc); 7040 7041 case OPERATOR_OROR: 7042 case OPERATOR_ANDAND: 7043 use_left_type = false; 7044 break; 7045 case OPERATOR_PLUS: 7046 case OPERATOR_MINUS: 7047 case OPERATOR_OR: 7048 case OPERATOR_XOR: 7049 case OPERATOR_MULT: 7050 break; 7051 case OPERATOR_DIV: 7052 if (left_type->float_type() != NULL || left_type->complex_type() != NULL) 7053 break; 7054 // Fall through. 7055 case OPERATOR_MOD: 7056 is_idiv_op = true; 7057 break; 7058 case OPERATOR_LSHIFT: 7059 case OPERATOR_RSHIFT: 7060 is_shift_op = true; 7061 break; 7062 case OPERATOR_BITCLEAR: 7063 this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); 7064 case OPERATOR_AND: 7065 break; 7066 default: 7067 go_unreachable(); 7068 } 7069 7070 // The only binary operation for string is +, and that should have 7071 // been converted to a String_concat_expression in do_lower. 7072 go_assert(!left_type->is_string_type()); 7073 7074 Bexpression* left = this->left_->get_backend(context); 7075 Bexpression* right = this->right_->get_backend(context); 7076 7077 Type* type = use_left_type ? left_type : right_type; 7078 Btype* btype = type->get_backend(gogo); 7079 7080 Bexpression* ret = 7081 gogo->backend()->binary_expression(this->op_, left, right, loc); 7082 ret = gogo->backend()->convert_expression(btype, ret, loc); 7083 7084 // Initialize overflow constants. 7085 Bexpression* overflow; 7086 mpz_t zero; 7087 mpz_init_set_ui(zero, 0UL); 7088 mpz_t one; 7089 mpz_init_set_ui(one, 1UL); 7090 mpz_t neg_one; 7091 mpz_init_set_si(neg_one, -1); 7092 7093 Btype* left_btype = left_type->get_backend(gogo); 7094 Btype* right_btype = right_type->get_backend(gogo); 7095 7096 // In Go, a shift larger than the size of the type is well-defined. 7097 // This is not true in C, so we need to insert a conditional. 7098 // We also need to check for a negative shift count. 7099 if (is_shift_op) 7100 { 7101 go_assert(left_type->integer_type() != NULL); 7102 go_assert(right_type->integer_type() != NULL); 7103 7104 int bits = left_type->integer_type()->bits(); 7105 7106 Numeric_constant nc; 7107 unsigned long ul; 7108 if (!this->right_->numeric_constant_value(&nc) 7109 || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID 7110 || ul >= static_cast<unsigned long>(bits)) 7111 { 7112 mpz_t bitsval; 7113 mpz_init_set_ui(bitsval, bits); 7114 Bexpression* bits_expr = 7115 gogo->backend()->integer_constant_expression(right_btype, bitsval); 7116 Bexpression* compare = 7117 gogo->backend()->binary_expression(OPERATOR_LT, 7118 right, bits_expr, loc); 7119 7120 Bexpression* zero_expr = 7121 gogo->backend()->integer_constant_expression(left_btype, zero); 7122 overflow = zero_expr; 7123 Bfunction* bfn = context->function()->func_value()->get_decl(); 7124 if (this->op_ == OPERATOR_RSHIFT 7125 && !left_type->integer_type()->is_unsigned()) 7126 { 7127 Bexpression* neg_expr = 7128 gogo->backend()->binary_expression(OPERATOR_LT, left, 7129 zero_expr, loc); 7130 Bexpression* neg_one_expr = 7131 gogo->backend()->integer_constant_expression(left_btype, 7132 neg_one); 7133 overflow = gogo->backend()->conditional_expression(bfn, 7134 btype, 7135 neg_expr, 7136 neg_one_expr, 7137 zero_expr, 7138 loc); 7139 } 7140 ret = gogo->backend()->conditional_expression(bfn, btype, compare, 7141 ret, overflow, loc); 7142 mpz_clear(bitsval); 7143 } 7144 7145 if (!right_type->integer_type()->is_unsigned() 7146 && (!this->right_->numeric_constant_value(&nc) 7147 || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID)) 7148 { 7149 Bexpression* zero_expr = 7150 gogo->backend()->integer_constant_expression(right_btype, zero); 7151 Bexpression* compare = 7152 gogo->backend()->binary_expression(OPERATOR_LT, right, zero_expr, 7153 loc); 7154 Expression* crash = Runtime::make_call(Runtime::PANIC_SHIFT, 7155 loc, 0); 7156 Bexpression* bcrash = crash->get_backend(context); 7157 Bfunction* bfn = context->function()->func_value()->get_decl(); 7158 ret = gogo->backend()->conditional_expression(bfn, btype, compare, 7159 bcrash, ret, loc); 7160 } 7161 } 7162 7163 // Add checks for division by zero and division overflow as needed. 7164 if (is_idiv_op) 7165 { 7166 if (gogo->check_divide_by_zero()) 7167 { 7168 // right == 0 7169 Bexpression* zero_expr = 7170 gogo->backend()->integer_constant_expression(right_btype, zero); 7171 Bexpression* check = 7172 gogo->backend()->binary_expression(OPERATOR_EQEQ, 7173 right, zero_expr, loc); 7174 7175 Expression* crash = Runtime::make_call(Runtime::PANIC_DIVIDE, 7176 loc, 0); 7177 Bexpression* bcrash = crash->get_backend(context); 7178 7179 // right == 0 ? (panicdivide(), 0) : ret 7180 Bfunction* bfn = context->function()->func_value()->get_decl(); 7181 ret = gogo->backend()->conditional_expression(bfn, btype, 7182 check, bcrash, 7183 ret, loc); 7184 } 7185 7186 if (gogo->check_divide_overflow()) 7187 { 7188 // right == -1 7189 // FIXME: It would be nice to say that this test is expected 7190 // to return false. 7191 7192 Bexpression* neg_one_expr = 7193 gogo->backend()->integer_constant_expression(right_btype, neg_one); 7194 Bexpression* check = 7195 gogo->backend()->binary_expression(OPERATOR_EQEQ, 7196 right, neg_one_expr, loc); 7197 7198 Bexpression* zero_expr = 7199 gogo->backend()->integer_constant_expression(btype, zero); 7200 Bexpression* one_expr = 7201 gogo->backend()->integer_constant_expression(btype, one); 7202 Bfunction* bfn = context->function()->func_value()->get_decl(); 7203 7204 if (type->integer_type()->is_unsigned()) 7205 { 7206 // An unsigned -1 is the largest possible number, so 7207 // dividing is always 1 or 0. 7208 7209 Bexpression* cmp = 7210 gogo->backend()->binary_expression(OPERATOR_EQEQ, 7211 left, right, loc); 7212 if (this->op_ == OPERATOR_DIV) 7213 overflow = 7214 gogo->backend()->conditional_expression(bfn, btype, cmp, 7215 one_expr, zero_expr, 7216 loc); 7217 else 7218 overflow = 7219 gogo->backend()->conditional_expression(bfn, btype, cmp, 7220 zero_expr, left, 7221 loc); 7222 } 7223 else 7224 { 7225 // Computing left / -1 is the same as computing - left, 7226 // which does not overflow since Go sets -fwrapv. 7227 if (this->op_ == OPERATOR_DIV) 7228 { 7229 Expression* negate_expr = 7230 Expression::make_unary(OPERATOR_MINUS, this->left_, loc); 7231 overflow = negate_expr->get_backend(context); 7232 } 7233 else 7234 overflow = zero_expr; 7235 } 7236 overflow = gogo->backend()->convert_expression(btype, overflow, loc); 7237 7238 // right == -1 ? - left : ret 7239 ret = gogo->backend()->conditional_expression(bfn, btype, 7240 check, overflow, 7241 ret, loc); 7242 } 7243 } 7244 7245 mpz_clear(zero); 7246 mpz_clear(one); 7247 mpz_clear(neg_one); 7248 return ret; 7249 } 7250 7251 // Export a binary expression. 7252 7253 void 7254 Binary_expression::do_export(Export_function_body* efb) const 7255 { 7256 efb->write_c_string("("); 7257 this->left_->export_expression(efb); 7258 switch (this->op_) 7259 { 7260 case OPERATOR_OROR: 7261 efb->write_c_string(" || "); 7262 break; 7263 case OPERATOR_ANDAND: 7264 efb->write_c_string(" && "); 7265 break; 7266 case OPERATOR_EQEQ: 7267 efb->write_c_string(" == "); 7268 break; 7269 case OPERATOR_NOTEQ: 7270 efb->write_c_string(" != "); 7271 break; 7272 case OPERATOR_LT: 7273 efb->write_c_string(" < "); 7274 break; 7275 case OPERATOR_LE: 7276 efb->write_c_string(" <= "); 7277 break; 7278 case OPERATOR_GT: 7279 efb->write_c_string(" > "); 7280 break; 7281 case OPERATOR_GE: 7282 efb->write_c_string(" >= "); 7283 break; 7284 case OPERATOR_PLUS: 7285 efb->write_c_string(" + "); 7286 break; 7287 case OPERATOR_MINUS: 7288 efb->write_c_string(" - "); 7289 break; 7290 case OPERATOR_OR: 7291 efb->write_c_string(" | "); 7292 break; 7293 case OPERATOR_XOR: 7294 efb->write_c_string(" ^ "); 7295 break; 7296 case OPERATOR_MULT: 7297 efb->write_c_string(" * "); 7298 break; 7299 case OPERATOR_DIV: 7300 efb->write_c_string(" / "); 7301 break; 7302 case OPERATOR_MOD: 7303 efb->write_c_string(" % "); 7304 break; 7305 case OPERATOR_LSHIFT: 7306 efb->write_c_string(" << "); 7307 break; 7308 case OPERATOR_RSHIFT: 7309 efb->write_c_string(" >> "); 7310 break; 7311 case OPERATOR_AND: 7312 efb->write_c_string(" & "); 7313 break; 7314 case OPERATOR_BITCLEAR: 7315 efb->write_c_string(" &^ "); 7316 break; 7317 default: 7318 go_unreachable(); 7319 } 7320 this->right_->export_expression(efb); 7321 efb->write_c_string(")"); 7322 } 7323 7324 // Import a binary expression. 7325 7326 Expression* 7327 Binary_expression::do_import(Import_expression* imp, Location loc) 7328 { 7329 imp->require_c_string("("); 7330 7331 Expression* left = Expression::import_expression(imp, loc); 7332 7333 Operator op; 7334 if (imp->match_c_string(" || ")) 7335 { 7336 op = OPERATOR_OROR; 7337 imp->advance(4); 7338 } 7339 else if (imp->match_c_string(" && ")) 7340 { 7341 op = OPERATOR_ANDAND; 7342 imp->advance(4); 7343 } 7344 else if (imp->match_c_string(" == ")) 7345 { 7346 op = OPERATOR_EQEQ; 7347 imp->advance(4); 7348 } 7349 else if (imp->match_c_string(" != ")) 7350 { 7351 op = OPERATOR_NOTEQ; 7352 imp->advance(4); 7353 } 7354 else if (imp->match_c_string(" < ")) 7355 { 7356 op = OPERATOR_LT; 7357 imp->advance(3); 7358 } 7359 else if (imp->match_c_string(" <= ")) 7360 { 7361 op = OPERATOR_LE; 7362 imp->advance(4); 7363 } 7364 else if (imp->match_c_string(" > ")) 7365 { 7366 op = OPERATOR_GT; 7367 imp->advance(3); 7368 } 7369 else if (imp->match_c_string(" >= ")) 7370 { 7371 op = OPERATOR_GE; 7372 imp->advance(4); 7373 } 7374 else if (imp->match_c_string(" + ")) 7375 { 7376 op = OPERATOR_PLUS; 7377 imp->advance(3); 7378 } 7379 else if (imp->match_c_string(" - ")) 7380 { 7381 op = OPERATOR_MINUS; 7382 imp->advance(3); 7383 } 7384 else if (imp->match_c_string(" | ")) 7385 { 7386 op = OPERATOR_OR; 7387 imp->advance(3); 7388 } 7389 else if (imp->match_c_string(" ^ ")) 7390 { 7391 op = OPERATOR_XOR; 7392 imp->advance(3); 7393 } 7394 else if (imp->match_c_string(" * ")) 7395 { 7396 op = OPERATOR_MULT; 7397 imp->advance(3); 7398 } 7399 else if (imp->match_c_string(" / ")) 7400 { 7401 op = OPERATOR_DIV; 7402 imp->advance(3); 7403 } 7404 else if (imp->match_c_string(" % ")) 7405 { 7406 op = OPERATOR_MOD; 7407 imp->advance(3); 7408 } 7409 else if (imp->match_c_string(" << ")) 7410 { 7411 op = OPERATOR_LSHIFT; 7412 imp->advance(4); 7413 } 7414 else if (imp->match_c_string(" >> ")) 7415 { 7416 op = OPERATOR_RSHIFT; 7417 imp->advance(4); 7418 } 7419 else if (imp->match_c_string(" & ")) 7420 { 7421 op = OPERATOR_AND; 7422 imp->advance(3); 7423 } 7424 else if (imp->match_c_string(" &^ ")) 7425 { 7426 op = OPERATOR_BITCLEAR; 7427 imp->advance(4); 7428 } 7429 else if (imp->match_c_string(")")) 7430 { 7431 // Not a binary operator after all. 7432 imp->advance(1); 7433 return left; 7434 } 7435 else 7436 { 7437 go_error_at(imp->location(), "unrecognized binary operator"); 7438 return Expression::make_error(loc); 7439 } 7440 7441 Expression* right = Expression::import_expression(imp, loc); 7442 7443 imp->require_c_string(")"); 7444 7445 return Expression::make_binary(op, left, right, loc); 7446 } 7447 7448 // Dump ast representation of a binary expression. 7449 7450 void 7451 Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 7452 { 7453 ast_dump_context->ostream() << "("; 7454 ast_dump_context->dump_expression(this->left_); 7455 ast_dump_context->ostream() << " "; 7456 ast_dump_context->dump_operator(this->op_); 7457 ast_dump_context->ostream() << " "; 7458 ast_dump_context->dump_expression(this->right_); 7459 ast_dump_context->ostream() << ") "; 7460 } 7461 7462 // Make a binary expression. 7463 7464 Expression* 7465 Expression::make_binary(Operator op, Expression* left, Expression* right, 7466 Location location) 7467 { 7468 return new Binary_expression(op, left, right, location); 7469 } 7470 7471 // Implement a comparison. 7472 7473 Bexpression* 7474 Expression::comparison(Translate_context* context, Type* result_type, 7475 Operator op, Expression* left, Expression* right, 7476 Location location) 7477 { 7478 Type* left_type = left->type(); 7479 Type* right_type = right->type(); 7480 7481 Expression* zexpr = Expression::make_integer_ul(0, NULL, location); 7482 7483 if (left_type->is_string_type() && right_type->is_string_type()) 7484 { 7485 go_assert(left->is_multi_eval_safe()); 7486 go_assert(right->is_multi_eval_safe()); 7487 7488 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) 7489 { 7490 // (l.len == r.len 7491 // ? (l.ptr == r.ptr ? true : memcmp(l.ptr, r.ptr, r.len) == 0) 7492 // : false) 7493 Expression* llen = Expression::make_string_info(left, 7494 STRING_INFO_LENGTH, 7495 location); 7496 Expression* rlen = Expression::make_string_info(right, 7497 STRING_INFO_LENGTH, 7498 location); 7499 Expression* leneq = Expression::make_binary(OPERATOR_EQEQ, llen, rlen, 7500 location); 7501 Expression* lptr = Expression::make_string_info(left->copy(), 7502 STRING_INFO_DATA, 7503 location); 7504 Expression* rptr = Expression::make_string_info(right->copy(), 7505 STRING_INFO_DATA, 7506 location); 7507 Expression* ptreq = Expression::make_binary(OPERATOR_EQEQ, lptr, rptr, 7508 location); 7509 Expression* btrue = Expression::make_boolean(true, location); 7510 Expression* call = Runtime::make_call(Runtime::MEMCMP, location, 3, 7511 lptr->copy(), rptr->copy(), 7512 rlen->copy()); 7513 Type* int32_type = Type::lookup_integer_type("int32"); 7514 Expression* zero = Expression::make_integer_ul(0, int32_type, location); 7515 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, call, zero, 7516 location); 7517 Expression* cond = Expression::make_conditional(ptreq, btrue, cmp, 7518 location); 7519 Expression* bfalse = Expression::make_boolean(false, location); 7520 left = Expression::make_conditional(leneq, cond, bfalse, location); 7521 right = Expression::make_boolean(true, location); 7522 } 7523 else 7524 { 7525 left = Runtime::make_call(Runtime::CMPSTRING, location, 2, 7526 left, right); 7527 right = zexpr; 7528 } 7529 } 7530 else if ((left_type->interface_type() != NULL 7531 && right_type->interface_type() == NULL 7532 && !right_type->is_nil_type()) 7533 || (left_type->interface_type() == NULL 7534 && !left_type->is_nil_type() 7535 && right_type->interface_type() != NULL)) 7536 { 7537 // Comparing an interface value to a non-interface value. 7538 if (left_type->interface_type() == NULL) 7539 { 7540 std::swap(left_type, right_type); 7541 std::swap(left, right); 7542 } 7543 7544 // The right operand is not an interface. We need to take its 7545 // address if it is not a direct interface type. 7546 Expression* pointer_arg = NULL; 7547 if (right_type->is_direct_iface_type()) 7548 pointer_arg = Expression::unpack_direct_iface(right, location); 7549 else 7550 { 7551 go_assert(right->is_addressable()); 7552 pointer_arg = Expression::make_unary(OPERATOR_AND, right, 7553 location); 7554 } 7555 7556 Expression* descriptor = 7557 Expression::make_type_descriptor(right_type, location); 7558 left = 7559 Runtime::make_call((left_type->interface_type()->is_empty() 7560 ? Runtime::EFACEVALEQ 7561 : Runtime::IFACEVALEQ), 7562 location, 3, left, descriptor, 7563 pointer_arg); 7564 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); 7565 right = Expression::make_boolean(true, location); 7566 } 7567 else if (left_type->interface_type() != NULL 7568 && right_type->interface_type() != NULL) 7569 { 7570 Runtime::Function compare_function; 7571 if (left_type->interface_type()->is_empty() 7572 && right_type->interface_type()->is_empty()) 7573 compare_function = Runtime::EFACEEQ; 7574 else if (!left_type->interface_type()->is_empty() 7575 && !right_type->interface_type()->is_empty()) 7576 compare_function = Runtime::IFACEEQ; 7577 else 7578 { 7579 if (left_type->interface_type()->is_empty()) 7580 { 7581 std::swap(left_type, right_type); 7582 std::swap(left, right); 7583 } 7584 go_assert(!left_type->interface_type()->is_empty()); 7585 go_assert(right_type->interface_type()->is_empty()); 7586 compare_function = Runtime::IFACEEFACEEQ; 7587 } 7588 7589 left = Runtime::make_call(compare_function, location, 2, left, right); 7590 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); 7591 right = Expression::make_boolean(true, location); 7592 } 7593 7594 if (left_type->is_nil_type() 7595 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) 7596 { 7597 std::swap(left_type, right_type); 7598 std::swap(left, right); 7599 } 7600 7601 if (right_type->is_nil_type()) 7602 { 7603 right = Expression::make_nil(location); 7604 if (left_type->array_type() != NULL 7605 && left_type->array_type()->length() == NULL) 7606 { 7607 Array_type* at = left_type->array_type(); 7608 left = at->get_value_pointer(context->gogo(), left); 7609 } 7610 else if (left_type->interface_type() != NULL) 7611 { 7612 // An interface is nil if the first field is nil. 7613 left = Expression::make_field_reference(left, 0, location); 7614 } 7615 } 7616 7617 Bexpression* left_bexpr = left->get_backend(context); 7618 Bexpression* right_bexpr = right->get_backend(context); 7619 7620 Gogo* gogo = context->gogo(); 7621 Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, 7622 right_bexpr, location); 7623 if (result_type != NULL) 7624 ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), 7625 ret, location); 7626 return ret; 7627 } 7628 7629 // Class String_concat_expression. 7630 7631 bool 7632 String_concat_expression::do_is_constant() const 7633 { 7634 for (Expression_list::const_iterator pe = this->exprs_->begin(); 7635 pe != this->exprs_->end(); 7636 ++pe) 7637 { 7638 if (!(*pe)->is_constant()) 7639 return false; 7640 } 7641 return true; 7642 } 7643 7644 bool 7645 String_concat_expression::do_is_zero_value() const 7646 { 7647 for (Expression_list::const_iterator pe = this->exprs_->begin(); 7648 pe != this->exprs_->end(); 7649 ++pe) 7650 { 7651 if (!(*pe)->is_zero_value()) 7652 return false; 7653 } 7654 return true; 7655 } 7656 7657 bool 7658 String_concat_expression::do_is_static_initializer() const 7659 { 7660 for (Expression_list::const_iterator pe = this->exprs_->begin(); 7661 pe != this->exprs_->end(); 7662 ++pe) 7663 { 7664 if (!(*pe)->is_static_initializer()) 7665 return false; 7666 } 7667 return true; 7668 } 7669 7670 Type* 7671 String_concat_expression::do_type() 7672 { 7673 Type* t = this->exprs_->front()->type(); 7674 Expression_list::iterator pe = this->exprs_->begin(); 7675 ++pe; 7676 for (; pe != this->exprs_->end(); ++pe) 7677 { 7678 Type* t1; 7679 if (!Binary_expression::operation_type(OPERATOR_PLUS, t, 7680 (*pe)->type(), 7681 &t1)) 7682 return Type::make_error_type(); 7683 t = t1; 7684 } 7685 return t; 7686 } 7687 7688 void 7689 String_concat_expression::do_determine_type(const Type_context* context) 7690 { 7691 Type_context subcontext(*context); 7692 for (Expression_list::iterator pe = this->exprs_->begin(); 7693 pe != this->exprs_->end(); 7694 ++pe) 7695 { 7696 Type* t = (*pe)->type(); 7697 if (!t->is_abstract()) 7698 { 7699 subcontext.type = t; 7700 break; 7701 } 7702 } 7703 if (subcontext.type == NULL) 7704 subcontext.type = this->exprs_->front()->type(); 7705 for (Expression_list::iterator pe = this->exprs_->begin(); 7706 pe != this->exprs_->end(); 7707 ++pe) 7708 (*pe)->determine_type(&subcontext); 7709 } 7710 7711 void 7712 String_concat_expression::do_check_types(Gogo*) 7713 { 7714 if (this->is_error_expression()) 7715 return; 7716 Type* t = this->exprs_->front()->type(); 7717 if (t->is_error()) 7718 { 7719 this->set_is_error(); 7720 return; 7721 } 7722 Expression_list::iterator pe = this->exprs_->begin(); 7723 ++pe; 7724 for (; pe != this->exprs_->end(); ++pe) 7725 { 7726 Type* t1 = (*pe)->type(); 7727 if (!Type::are_compatible_for_binop(t, t1)) 7728 { 7729 this->report_error("incompatible types in binary expression"); 7730 return; 7731 } 7732 if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, 7733 this->location())) 7734 { 7735 this->set_is_error(); 7736 return; 7737 } 7738 } 7739 } 7740 7741 Expression* 7742 String_concat_expression::do_flatten(Gogo*, Named_object*, 7743 Statement_inserter* inserter) 7744 { 7745 if (this->is_error_expression()) 7746 return this; 7747 Location loc = this->location(); 7748 Type* type = this->type(); 7749 7750 // Mark string([]byte) operands to reuse the backing store. 7751 // runtime.concatstrings does not keep the reference. 7752 // 7753 // Note: in the gc runtime, if all but one inputs are empty, 7754 // concatstrings returns the only nonempty input without copy. 7755 // So it is not safe to reuse the backing store if it is a 7756 // string([]byte) conversion. So the gc compiler does the 7757 // no-copy optimization only when there is at least one 7758 // constant nonempty input. Currently the gccgo runtime 7759 // doesn't do this, so we don't do the check. 7760 for (Expression_list::iterator p = this->exprs_->begin(); 7761 p != this->exprs_->end(); 7762 ++p) 7763 { 7764 Type_conversion_expression* tce = (*p)->conversion_expression(); 7765 if (tce != NULL) 7766 tce->set_no_copy(true); 7767 } 7768 7769 Expression* buf = NULL; 7770 Node* n = Node::make_node(this); 7771 if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE) 7772 { 7773 size_t size = 0; 7774 for (Expression_list::iterator p = this->exprs_->begin(); 7775 p != this->exprs_->end(); 7776 ++p) 7777 { 7778 std::string s; 7779 if ((*p)->string_constant_value(&s)) 7780 size += s.length(); 7781 } 7782 // Make a buffer on stack if the result does not escape. 7783 // But don't do this if we know it won't fit. 7784 if (size < (size_t)tmp_string_buf_size) 7785 { 7786 Type* byte_type = Type::lookup_integer_type("uint8"); 7787 Expression* buflen = 7788 Expression::make_integer_ul(tmp_string_buf_size, NULL, loc); 7789 Expression::make_integer_ul(tmp_string_buf_size, NULL, loc); 7790 Type* array_type = Type::make_array_type(byte_type, buflen); 7791 buf = Expression::make_allocation(array_type, loc); 7792 buf->allocation_expression()->set_allocate_on_stack(); 7793 buf->allocation_expression()->set_no_zero(); 7794 } 7795 } 7796 if (buf == NULL) 7797 buf = Expression::make_nil(loc); 7798 go_assert(this->exprs_->size() > 1); 7799 Expression* len = 7800 Expression::make_integer_ul(this->exprs_->size(), NULL, loc); 7801 Array_type* array_type = Type::make_array_type(type, len); 7802 array_type->set_is_array_incomparable(); 7803 Expression* array = 7804 Expression::make_array_composite_literal(array_type, this->exprs_, 7805 loc); 7806 Temporary_statement* ts = 7807 Statement::make_temporary(array_type, array, loc); 7808 inserter->insert(ts); 7809 Expression* ref = Expression::make_temporary_reference(ts, loc); 7810 ref = Expression::make_unary(OPERATOR_AND, ref, loc); 7811 Expression* call = 7812 Runtime::make_call(Runtime::CONCATSTRINGS, loc, 3, buf, 7813 ref, len->copy()); 7814 return Expression::make_cast(type, call, loc); 7815 } 7816 7817 void 7818 String_concat_expression::do_dump_expression( 7819 Ast_dump_context* ast_dump_context) const 7820 { 7821 ast_dump_context->ostream() << "concat("; 7822 ast_dump_context->dump_expression_list(this->exprs_, false); 7823 ast_dump_context->ostream() << ")"; 7824 } 7825 7826 Expression* 7827 Expression::make_string_concat(Expression_list* exprs) 7828 { 7829 return new String_concat_expression(exprs); 7830 } 7831 7832 // Class Bound_method_expression. 7833 7834 // Traversal. 7835 7836 int 7837 Bound_method_expression::do_traverse(Traverse* traverse) 7838 { 7839 return Expression::traverse(&this->expr_, traverse); 7840 } 7841 7842 // Return the type of a bound method expression. The type of this 7843 // object is simply the type of the method with no receiver. 7844 7845 Type* 7846 Bound_method_expression::do_type() 7847 { 7848 Named_object* fn = this->method_->named_object(); 7849 Function_type* fntype; 7850 if (fn->is_function()) 7851 fntype = fn->func_value()->type(); 7852 else if (fn->is_function_declaration()) 7853 fntype = fn->func_declaration_value()->type(); 7854 else 7855 return Type::make_error_type(); 7856 return fntype->copy_without_receiver(); 7857 } 7858 7859 // Determine the types of a method expression. 7860 7861 void 7862 Bound_method_expression::do_determine_type(const Type_context*) 7863 { 7864 Named_object* fn = this->method_->named_object(); 7865 Function_type* fntype; 7866 if (fn->is_function()) 7867 fntype = fn->func_value()->type(); 7868 else if (fn->is_function_declaration()) 7869 fntype = fn->func_declaration_value()->type(); 7870 else 7871 fntype = NULL; 7872 if (fntype == NULL || !fntype->is_method()) 7873 this->expr_->determine_type_no_context(); 7874 else 7875 { 7876 Type_context subcontext(fntype->receiver()->type(), false); 7877 this->expr_->determine_type(&subcontext); 7878 } 7879 } 7880 7881 // Check the types of a method expression. 7882 7883 void 7884 Bound_method_expression::do_check_types(Gogo*) 7885 { 7886 Named_object* fn = this->method_->named_object(); 7887 if (!fn->is_function() && !fn->is_function_declaration()) 7888 { 7889 this->report_error(_("object is not a method")); 7890 return; 7891 } 7892 7893 Function_type* fntype; 7894 if (fn->is_function()) 7895 fntype = fn->func_value()->type(); 7896 else if (fn->is_function_declaration()) 7897 fntype = fn->func_declaration_value()->type(); 7898 else 7899 go_unreachable(); 7900 Type* rtype = fntype->receiver()->type()->deref(); 7901 Type* etype = (this->expr_type_ != NULL 7902 ? this->expr_type_ 7903 : this->expr_->type()); 7904 etype = etype->deref(); 7905 if (!Type::are_identical(rtype, etype, Type::COMPARE_TAGS, NULL)) 7906 this->report_error(_("method type does not match object type")); 7907 } 7908 7909 // If a bound method expression is not simply called, then it is 7910 // represented as a closure. The closure will hold a single variable, 7911 // the receiver to pass to the method. The function will be a simple 7912 // thunk that pulls that value from the closure and calls the method 7913 // with the remaining arguments. 7914 // 7915 // Because method values are not common, we don't build all thunks for 7916 // every methods, but instead only build them as we need them. In 7917 // particular, we even build them on demand for methods defined in 7918 // other packages. 7919 7920 Bound_method_expression::Method_value_thunks 7921 Bound_method_expression::method_value_thunks; 7922 7923 // Find or create the thunk for FN. 7924 7925 Named_object* 7926 Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, 7927 Named_object* fn) 7928 { 7929 std::pair<Named_object*, Named_object*> val(fn, NULL); 7930 std::pair<Method_value_thunks::iterator, bool> ins = 7931 Bound_method_expression::method_value_thunks.insert(val); 7932 if (!ins.second) 7933 { 7934 // We have seen this method before. 7935 go_assert(ins.first->second != NULL); 7936 return ins.first->second; 7937 } 7938 7939 Location loc = fn->location(); 7940 7941 Function_type* orig_fntype; 7942 if (fn->is_function()) 7943 orig_fntype = fn->func_value()->type(); 7944 else if (fn->is_function_declaration()) 7945 orig_fntype = fn->func_declaration_value()->type(); 7946 else 7947 orig_fntype = NULL; 7948 7949 if (orig_fntype == NULL || !orig_fntype->is_method()) 7950 { 7951 ins.first->second = 7952 Named_object::make_erroneous_name(gogo->thunk_name()); 7953 return ins.first->second; 7954 } 7955 7956 Struct_field_list* sfl = new Struct_field_list(); 7957 // The type here is wrong--it should be the C function type. But it 7958 // doesn't really matter. 7959 Type* vt = Type::make_pointer_type(Type::make_void_type()); 7960 sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc))); 7961 sfl->push_back(Struct_field(Typed_identifier("val", 7962 orig_fntype->receiver()->type(), 7963 loc))); 7964 Struct_type* st = Type::make_struct_type(sfl, loc); 7965 st->set_is_struct_incomparable(); 7966 Type* closure_type = Type::make_pointer_type(st); 7967 7968 Function_type* new_fntype = orig_fntype->copy_with_names(); 7969 7970 std::string thunk_name = gogo->thunk_name(); 7971 Named_object* new_no = gogo->start_function(thunk_name, new_fntype, 7972 false, loc); 7973 7974 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); 7975 cvar->set_is_used(); 7976 cvar->set_is_closure(); 7977 Named_object* cp = Named_object::make_variable("$closure" + thunk_name, 7978 NULL, cvar); 7979 new_no->func_value()->set_closure_var(cp); 7980 7981 gogo->start_block(loc); 7982 7983 // Field 0 of the closure is the function code pointer, field 1 is 7984 // the value on which to invoke the method. 7985 Expression* arg = Expression::make_var_reference(cp, loc); 7986 arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); 7987 arg = Expression::make_field_reference(arg, 1, loc); 7988 7989 Expression* bme = Expression::make_bound_method(arg, method, fn, loc); 7990 7991 const Typed_identifier_list* orig_params = orig_fntype->parameters(); 7992 Expression_list* args; 7993 if (orig_params == NULL || orig_params->empty()) 7994 args = NULL; 7995 else 7996 { 7997 const Typed_identifier_list* new_params = new_fntype->parameters(); 7998 args = new Expression_list(); 7999 for (Typed_identifier_list::const_iterator p = new_params->begin(); 8000 p != new_params->end(); 8001 ++p) 8002 { 8003 Named_object* p_no = gogo->lookup(p->name(), NULL); 8004 go_assert(p_no != NULL 8005 && p_no->is_variable() 8006 && p_no->var_value()->is_parameter()); 8007 args->push_back(Expression::make_var_reference(p_no, loc)); 8008 } 8009 } 8010 8011 Call_expression* call = Expression::make_call(bme, args, 8012 orig_fntype->is_varargs(), 8013 loc); 8014 call->set_varargs_are_lowered(); 8015 8016 Statement* s = Statement::make_return_from_call(call, loc); 8017 gogo->add_statement(s); 8018 Block* b = gogo->finish_block(loc); 8019 gogo->add_block(b, loc); 8020 8021 // This is called after lowering but before determine_types. 8022 gogo->lower_block(new_no, b); 8023 8024 gogo->finish_function(loc); 8025 8026 ins.first->second = new_no; 8027 return new_no; 8028 } 8029 8030 // Look up a thunk for FN. 8031 8032 Named_object* 8033 Bound_method_expression::lookup_thunk(Named_object* fn) 8034 { 8035 Method_value_thunks::const_iterator p = 8036 Bound_method_expression::method_value_thunks.find(fn); 8037 if (p == Bound_method_expression::method_value_thunks.end()) 8038 return NULL; 8039 return p->second; 8040 } 8041 8042 // Return an expression to check *REF for nil while dereferencing 8043 // according to FIELD_INDEXES. Update *REF to build up the field 8044 // reference. This is a static function so that we don't have to 8045 // worry about declaring Field_indexes in expressions.h. 8046 8047 static Expression* 8048 bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, 8049 Expression** ref) 8050 { 8051 if (field_indexes == NULL) 8052 return Expression::make_boolean(false, loc); 8053 Expression* cond = bme_check_nil(field_indexes->next, loc, ref); 8054 Struct_type* stype = (*ref)->type()->deref()->struct_type(); 8055 go_assert(stype != NULL 8056 && field_indexes->field_index < stype->field_count()); 8057 if ((*ref)->type()->struct_type() == NULL) 8058 { 8059 go_assert((*ref)->type()->points_to() != NULL); 8060 Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, 8061 Expression::make_nil(loc), 8062 loc); 8063 cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); 8064 *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT, 8065 loc); 8066 go_assert((*ref)->type()->struct_type() == stype); 8067 } 8068 *ref = Expression::make_field_reference(*ref, field_indexes->field_index, 8069 loc); 8070 return cond; 8071 } 8072 8073 // Flatten a method value into a struct with nil checks. We can't do 8074 // this in the lowering phase, because if the method value is called 8075 // directly we don't need a thunk. That case will have been handled 8076 // by Call_expression::do_lower, so if we get here then we do need a 8077 // thunk. 8078 8079 Expression* 8080 Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, 8081 Statement_inserter* inserter) 8082 { 8083 Location loc = this->location(); 8084 8085 Named_object* thunk = Bound_method_expression::lookup_thunk(this->function_); 8086 8087 // The thunk should have been created during the 8088 // create_function_descriptors pass. 8089 if (thunk == NULL || thunk->is_erroneous()) 8090 { 8091 go_assert(saw_errors()); 8092 return Expression::make_error(loc); 8093 } 8094 8095 // Force the expression into a variable. This is only necessary if 8096 // we are going to do nil checks below, but it's easy enough to 8097 // always do it. 8098 Expression* expr = this->expr_; 8099 if (!expr->is_multi_eval_safe()) 8100 { 8101 Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); 8102 inserter->insert(etemp); 8103 expr = Expression::make_temporary_reference(etemp, loc); 8104 } 8105 8106 // If the method expects a value, and we have a pointer, we need to 8107 // dereference the pointer. 8108 8109 Named_object* fn = this->method_->named_object(); 8110 Function_type *fntype; 8111 if (fn->is_function()) 8112 fntype = fn->func_value()->type(); 8113 else if (fn->is_function_declaration()) 8114 fntype = fn->func_declaration_value()->type(); 8115 else 8116 go_unreachable(); 8117 8118 Expression* val = expr; 8119 if (fntype->receiver()->type()->points_to() == NULL 8120 && val->type()->points_to() != NULL) 8121 val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc); 8122 8123 // Note that we are ignoring this->expr_type_ here. The thunk will 8124 // expect a closure whose second field has type this->expr_type_ (if 8125 // that is not NULL). We are going to pass it a closure whose 8126 // second field has type this->expr_->type(). Since 8127 // this->expr_type_ is only not-NULL for pointer types, we can get 8128 // away with this. 8129 8130 Struct_field_list* fields = new Struct_field_list(); 8131 fields->push_back(Struct_field(Typed_identifier("fn", 8132 thunk->func_value()->type(), 8133 loc))); 8134 fields->push_back(Struct_field(Typed_identifier("val", val->type(), loc))); 8135 Struct_type* st = Type::make_struct_type(fields, loc); 8136 st->set_is_struct_incomparable(); 8137 8138 Expression_list* vals = new Expression_list(); 8139 vals->push_back(Expression::make_func_code_reference(thunk, loc)); 8140 vals->push_back(val); 8141 8142 Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); 8143 ret = Expression::make_heap_expression(ret, loc); 8144 8145 Node* node = Node::make_node(this); 8146 if ((node->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE) 8147 ret->heap_expression()->set_allocate_on_stack(); 8148 else if (gogo->compiling_runtime() 8149 && gogo->package_name() == "runtime" 8150 && !saw_errors()) 8151 go_error_at(loc, "%s escapes to heap, not allowed in runtime", 8152 node->ast_format(gogo).c_str()); 8153 8154 // If necessary, check whether the expression or any embedded 8155 // pointers are nil. 8156 8157 Expression* nil_check = NULL; 8158 if (this->method_->field_indexes() != NULL) 8159 { 8160 Expression* ref = expr; 8161 nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); 8162 expr = ref; 8163 } 8164 8165 if (this->method_->is_value_method() && expr->type()->points_to() != NULL) 8166 { 8167 Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, 8168 Expression::make_nil(loc), 8169 loc); 8170 if (nil_check == NULL) 8171 nil_check = n; 8172 else 8173 nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); 8174 } 8175 8176 if (nil_check != NULL) 8177 { 8178 Expression* crash = Runtime::make_call(Runtime::PANIC_MEM, loc, 0); 8179 // Fix the type of the conditional expression by pretending to 8180 // evaluate to RET either way through the conditional. 8181 crash = Expression::make_compound(crash, ret, loc); 8182 ret = Expression::make_conditional(nil_check, crash, ret, loc); 8183 } 8184 8185 // RET is a pointer to a struct, but we want a function type. 8186 ret = Expression::make_unsafe_cast(this->type(), ret, loc); 8187 8188 return ret; 8189 } 8190 8191 // Dump ast representation of a bound method expression. 8192 8193 void 8194 Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 8195 const 8196 { 8197 if (this->expr_type_ != NULL) 8198 ast_dump_context->ostream() << "("; 8199 ast_dump_context->dump_expression(this->expr_); 8200 if (this->expr_type_ != NULL) 8201 { 8202 ast_dump_context->ostream() << ":"; 8203 ast_dump_context->dump_type(this->expr_type_); 8204 ast_dump_context->ostream() << ")"; 8205 } 8206 8207 ast_dump_context->ostream() << "." << this->function_->name(); 8208 } 8209 8210 // Make a method expression. 8211 8212 Bound_method_expression* 8213 Expression::make_bound_method(Expression* expr, const Method* method, 8214 Named_object* function, Location location) 8215 { 8216 return new Bound_method_expression(expr, method, function, location); 8217 } 8218 8219 // Class Builtin_call_expression. This is used for a call to a 8220 // builtin function. 8221 8222 Builtin_call_expression::Builtin_call_expression(Gogo* gogo, 8223 Expression* fn, 8224 Expression_list* args, 8225 bool is_varargs, 8226 Location location) 8227 : Call_expression(fn, args, is_varargs, location), 8228 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), 8229 recover_arg_is_set_(false) 8230 { 8231 Func_expression* fnexp = this->fn()->func_expression(); 8232 if (fnexp == NULL) 8233 { 8234 this->code_ = BUILTIN_INVALID; 8235 return; 8236 } 8237 const std::string& name(fnexp->named_object()->name()); 8238 if (name == "append") 8239 this->code_ = BUILTIN_APPEND; 8240 else if (name == "cap") 8241 this->code_ = BUILTIN_CAP; 8242 else if (name == "close") 8243 this->code_ = BUILTIN_CLOSE; 8244 else if (name == "complex") 8245 this->code_ = BUILTIN_COMPLEX; 8246 else if (name == "copy") 8247 this->code_ = BUILTIN_COPY; 8248 else if (name == "delete") 8249 this->code_ = BUILTIN_DELETE; 8250 else if (name == "imag") 8251 this->code_ = BUILTIN_IMAG; 8252 else if (name == "len") 8253 this->code_ = BUILTIN_LEN; 8254 else if (name == "make") 8255 this->code_ = BUILTIN_MAKE; 8256 else if (name == "new") 8257 this->code_ = BUILTIN_NEW; 8258 else if (name == "panic") 8259 this->code_ = BUILTIN_PANIC; 8260 else if (name == "print") 8261 this->code_ = BUILTIN_PRINT; 8262 else if (name == "println") 8263 this->code_ = BUILTIN_PRINTLN; 8264 else if (name == "real") 8265 this->code_ = BUILTIN_REAL; 8266 else if (name == "recover") 8267 this->code_ = BUILTIN_RECOVER; 8268 else if (name == "Add") 8269 this->code_ = BUILTIN_ADD; 8270 else if (name == "Alignof") 8271 this->code_ = BUILTIN_ALIGNOF; 8272 else if (name == "Offsetof") 8273 this->code_ = BUILTIN_OFFSETOF; 8274 else if (name == "Sizeof") 8275 this->code_ = BUILTIN_SIZEOF; 8276 else if (name == "Slice") 8277 this->code_ = BUILTIN_SLICE; 8278 else 8279 go_unreachable(); 8280 } 8281 8282 // Return whether this is a call to recover. This is a virtual 8283 // function called from the parent class. 8284 8285 bool 8286 Builtin_call_expression::do_is_recover_call() const 8287 { 8288 if (this->classification() == EXPRESSION_ERROR) 8289 return false; 8290 return this->code_ == BUILTIN_RECOVER; 8291 } 8292 8293 // Set the argument for a call to recover. 8294 8295 void 8296 Builtin_call_expression::do_set_recover_arg(Expression* arg) 8297 { 8298 const Expression_list* args = this->args(); 8299 go_assert(args == NULL || args->empty()); 8300 Expression_list* new_args = new Expression_list(); 8301 new_args->push_back(arg); 8302 this->set_args(new_args); 8303 this->recover_arg_is_set_ = true; 8304 } 8305 8306 // Lower a builtin call expression. This turns new and make into 8307 // specific expressions. We also convert to a constant if we can. 8308 8309 Expression* 8310 Builtin_call_expression::do_lower(Gogo*, Named_object* function, 8311 Statement_inserter* inserter, int) 8312 { 8313 if (this->is_error_expression()) 8314 return this; 8315 8316 Location loc = this->location(); 8317 8318 if (this->is_varargs() && this->code_ != BUILTIN_APPEND) 8319 { 8320 this->report_error(_("invalid use of %<...%> with builtin function")); 8321 return Expression::make_error(loc); 8322 } 8323 8324 if (this->code_ == BUILTIN_OFFSETOF) 8325 { 8326 Expression* arg = this->one_arg(); 8327 8328 if (arg->bound_method_expression() != NULL 8329 || arg->interface_field_reference_expression() != NULL) 8330 { 8331 this->report_error(_("invalid use of method value as argument " 8332 "of Offsetof")); 8333 return this; 8334 } 8335 8336 Field_reference_expression* farg = arg->field_reference_expression(); 8337 while (farg != NULL) 8338 { 8339 if (!farg->implicit()) 8340 break; 8341 // When the selector refers to an embedded field, 8342 // it must not be reached through pointer indirections. 8343 if (farg->expr()->deref() != farg->expr()) 8344 { 8345 this->report_error(_("argument of Offsetof implies " 8346 "indirection of an embedded field")); 8347 return this; 8348 } 8349 // Go up until we reach the original base. 8350 farg = farg->expr()->field_reference_expression(); 8351 } 8352 } 8353 8354 if (this->is_constant()) 8355 { 8356 Numeric_constant nc; 8357 if (this->numeric_constant_value(&nc)) 8358 return nc.expression(loc); 8359 } 8360 8361 switch (this->code_) 8362 { 8363 default: 8364 break; 8365 8366 case BUILTIN_NEW: 8367 { 8368 const Expression_list* args = this->args(); 8369 if (args == NULL || args->size() < 1) 8370 this->report_error(_("not enough arguments")); 8371 else if (args->size() > 1) 8372 this->report_error(_("too many arguments")); 8373 else 8374 { 8375 Expression* arg = args->front(); 8376 if (!arg->is_type_expression()) 8377 { 8378 go_error_at(arg->location(), "expected type"); 8379 this->set_is_error(); 8380 } 8381 else 8382 return Expression::make_allocation(arg->type(), loc); 8383 } 8384 } 8385 break; 8386 8387 case BUILTIN_MAKE: 8388 return this->lower_make(inserter); 8389 8390 case BUILTIN_RECOVER: 8391 if (function != NULL) 8392 function->func_value()->set_calls_recover(); 8393 else 8394 { 8395 // Calling recover outside of a function always returns the 8396 // nil empty interface. 8397 Type* eface = Type::make_empty_interface_type(loc); 8398 return Expression::make_cast(eface, Expression::make_nil(loc), loc); 8399 } 8400 break; 8401 8402 case BUILTIN_DELETE: 8403 { 8404 const Expression_list* args = this->args(); 8405 if (args == NULL || args->size() < 2) 8406 this->report_error(_("not enough arguments")); 8407 else if (args->size() > 2) 8408 this->report_error(_("too many arguments")); 8409 else if (args->front()->type()->map_type() == NULL) 8410 this->report_error(_("argument 1 must be a map")); 8411 else 8412 { 8413 Type* key_type = 8414 args->front()->type()->map_type()->key_type(); 8415 Expression_list::iterator pa = this->args()->begin(); 8416 pa++; 8417 Type* arg_type = (*pa)->type(); 8418 std::string reason; 8419 if (!Type::are_assignable(key_type, arg_type, &reason)) 8420 { 8421 if (reason.empty()) 8422 go_error_at(loc, "argument 2 has incompatible type"); 8423 else 8424 go_error_at(loc, "argument 2 has incompatible type (%s)", 8425 reason.c_str()); 8426 this->set_is_error(); 8427 } 8428 else if (!Type::are_identical(key_type, arg_type, 0, NULL)) 8429 *pa = Expression::make_cast(key_type, *pa, loc); 8430 } 8431 } 8432 break; 8433 8434 case BUILTIN_PRINT: 8435 case BUILTIN_PRINTLN: 8436 // Force all the arguments into temporary variables, so that we 8437 // don't try to evaluate something while holding the print lock. 8438 if (this->args() == NULL) 8439 break; 8440 for (Expression_list::iterator pa = this->args()->begin(); 8441 pa != this->args()->end(); 8442 ++pa) 8443 { 8444 if (!(*pa)->is_multi_eval_safe()) 8445 { 8446 Temporary_statement* temp = 8447 Statement::make_temporary(NULL, *pa, loc); 8448 inserter->insert(temp); 8449 *pa = Expression::make_temporary_reference(temp, loc); 8450 } 8451 } 8452 break; 8453 } 8454 8455 return this; 8456 } 8457 8458 // Flatten a builtin call expression. This turns the arguments of some 8459 // builtin calls into temporary expressions. Also expand copy and append 8460 // to runtime calls. 8461 8462 Expression* 8463 Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function, 8464 Statement_inserter* inserter) 8465 { 8466 if (this->is_error_expression()) 8467 { 8468 go_assert(saw_errors()); 8469 return this; 8470 } 8471 8472 Location loc = this->location(); 8473 8474 switch (this->code_) 8475 { 8476 default: 8477 break; 8478 8479 case BUILTIN_APPEND: 8480 return this->flatten_append(gogo, function, inserter, NULL, NULL); 8481 8482 case BUILTIN_COPY: 8483 { 8484 Type* at = this->args()->front()->type(); 8485 for (Expression_list::iterator pa = this->args()->begin(); 8486 pa != this->args()->end(); 8487 ++pa) 8488 { 8489 if ((*pa)->is_nil_expression()) 8490 { 8491 Expression* nil = Expression::make_nil(loc); 8492 Expression* zero = Expression::make_integer_ul(0, NULL, loc); 8493 *pa = Expression::make_slice_value(at, nil, zero, zero, loc); 8494 } 8495 if (!(*pa)->is_multi_eval_safe()) 8496 { 8497 Temporary_statement* temp = 8498 Statement::make_temporary(NULL, *pa, loc); 8499 inserter->insert(temp); 8500 *pa = Expression::make_temporary_reference(temp, loc); 8501 } 8502 } 8503 8504 // Lower to runtime call. 8505 const Expression_list* args = this->args(); 8506 go_assert(args != NULL && args->size() == 2); 8507 Expression* arg1 = args->front(); 8508 Expression* arg2 = args->back(); 8509 go_assert(arg1->is_multi_eval_safe()); 8510 go_assert(arg2->is_multi_eval_safe()); 8511 bool arg2_is_string = arg2->type()->is_string_type(); 8512 8513 Expression* ret; 8514 Type* et = at->array_type()->element_type(); 8515 if (et->has_pointer()) 8516 { 8517 Expression* td = Expression::make_type_descriptor(et, loc); 8518 Expression* pd = 8519 Expression::make_slice_info(arg1, SLICE_INFO_VALUE_POINTER, loc); 8520 Expression* ld = 8521 Expression::make_slice_info(arg1, SLICE_INFO_LENGTH, loc); 8522 Expression* ps = 8523 Expression::make_slice_info(arg2, SLICE_INFO_VALUE_POINTER, loc); 8524 Expression* ls = 8525 Expression::make_slice_info(arg2, SLICE_INFO_LENGTH, loc); 8526 ret = Runtime::make_call(Runtime::TYPEDSLICECOPY, loc, 8527 5, td, pd, ld, ps, ls); 8528 } 8529 else 8530 { 8531 Type* int_type = Type::lookup_integer_type("int"); 8532 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 8533 8534 // l1 = len(arg1) 8535 Named_object* lenfn = gogo->lookup_global("len"); 8536 Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); 8537 Expression_list* len_args = new Expression_list(); 8538 len_args->push_back(arg1->copy()); 8539 Expression* len1 = Expression::make_call(lenref, len_args, false, loc); 8540 gogo->lower_expression(function, inserter, &len1); 8541 gogo->flatten_expression(function, inserter, &len1); 8542 Temporary_statement* l1tmp = Statement::make_temporary(int_type, len1, loc); 8543 inserter->insert(l1tmp); 8544 8545 // l2 = len(arg2) 8546 len_args = new Expression_list(); 8547 len_args->push_back(arg2->copy()); 8548 Expression* len2 = Expression::make_call(lenref, len_args, false, loc); 8549 gogo->lower_expression(function, inserter, &len2); 8550 gogo->flatten_expression(function, inserter, &len2); 8551 Temporary_statement* l2tmp = Statement::make_temporary(int_type, len2, loc); 8552 inserter->insert(l2tmp); 8553 8554 // n = (l1 < l2 ? l1 : l2) 8555 Expression* l1ref = Expression::make_temporary_reference(l1tmp, loc); 8556 Expression* l2ref = Expression::make_temporary_reference(l2tmp, loc); 8557 Expression* cond = Expression::make_binary(OPERATOR_LT, l1ref, l2ref, loc); 8558 Expression* n = Expression::make_conditional(cond, 8559 l1ref->copy(), 8560 l2ref->copy(), 8561 loc); 8562 Temporary_statement* ntmp = Statement::make_temporary(NULL, n, loc); 8563 inserter->insert(ntmp); 8564 8565 // sz = n * sizeof(elem_type) 8566 Expression* nref = Expression::make_temporary_reference(ntmp, loc); 8567 nref = Expression::make_cast(uintptr_type, nref, loc); 8568 Expression* sz = Expression::make_type_info(et, TYPE_INFO_SIZE); 8569 sz = Expression::make_binary(OPERATOR_MULT, sz, nref, loc); 8570 8571 // memmove(arg1.ptr, arg2.ptr, sz) 8572 Expression* p1 = Expression::make_slice_info(arg1, 8573 SLICE_INFO_VALUE_POINTER, 8574 loc); 8575 Expression* p2 = (arg2_is_string 8576 ? Expression::make_string_info(arg2, 8577 STRING_INFO_DATA, 8578 loc) 8579 : Expression::make_slice_info(arg2, 8580 SLICE_INFO_VALUE_POINTER, 8581 loc)); 8582 Expression* call = Runtime::make_call(Runtime::BUILTIN_MEMMOVE, loc, 3, 8583 p1, p2, sz); 8584 8585 // n is the return value of copy 8586 nref = Expression::make_temporary_reference(ntmp, loc); 8587 ret = Expression::make_compound(call, nref, loc); 8588 } 8589 return ret; 8590 } 8591 break; 8592 8593 case BUILTIN_PANIC: 8594 for (Expression_list::iterator pa = this->args()->begin(); 8595 pa != this->args()->end(); 8596 ++pa) 8597 { 8598 if (!(*pa)->is_multi_eval_safe() 8599 && (*pa)->type()->interface_type() != NULL) 8600 { 8601 Temporary_statement* temp = 8602 Statement::make_temporary(NULL, *pa, loc); 8603 inserter->insert(temp); 8604 *pa = Expression::make_temporary_reference(temp, loc); 8605 } 8606 } 8607 break; 8608 8609 case BUILTIN_LEN: 8610 case BUILTIN_CAP: 8611 { 8612 Expression_list::iterator pa = this->args()->begin(); 8613 if (!(*pa)->is_multi_eval_safe() 8614 && ((*pa)->type()->map_type() != NULL 8615 || (*pa)->type()->channel_type() != NULL)) 8616 { 8617 Temporary_statement* temp = 8618 Statement::make_temporary(NULL, *pa, loc); 8619 inserter->insert(temp); 8620 *pa = Expression::make_temporary_reference(temp, loc); 8621 } 8622 } 8623 break; 8624 8625 case BUILTIN_DELETE: 8626 { 8627 // Lower to a runtime function call. 8628 const Expression_list* args = this->args(); 8629 8630 // Since this function returns no value it must appear in 8631 // a statement by itself, so we don't have to worry about 8632 // order of evaluation of values around it. Evaluate the 8633 // map first to get order of evaluation right. 8634 Map_type* mt = args->front()->type()->map_type(); 8635 Temporary_statement* map_temp = 8636 Statement::make_temporary(mt, args->front(), loc); 8637 inserter->insert(map_temp); 8638 8639 Temporary_statement* key_temp = 8640 Statement::make_temporary(mt->key_type(), args->back(), loc); 8641 inserter->insert(key_temp); 8642 8643 Expression* e1 = Expression::make_type_descriptor(mt, loc); 8644 Expression* e2 = Expression::make_temporary_reference(map_temp, 8645 loc); 8646 Expression* e3 = Expression::make_temporary_reference(key_temp, 8647 loc); 8648 8649 Runtime::Function code; 8650 switch (mt->algorithm(gogo)) 8651 { 8652 case Map_type::MAP_ALG_FAST32: 8653 case Map_type::MAP_ALG_FAST32PTR: 8654 { 8655 code = Runtime::MAPDELETE_FAST32; 8656 Type* uint32_type = Type::lookup_integer_type("uint32"); 8657 Type* uint32_ptr_type = Type::make_pointer_type(uint32_type); 8658 e3 = Expression::make_unary(OPERATOR_AND, e3, loc); 8659 e3 = Expression::make_unsafe_cast(uint32_ptr_type, e3, 8660 loc); 8661 e3 = Expression::make_dereference(e3, 8662 Expression::NIL_CHECK_NOT_NEEDED, 8663 loc); 8664 break; 8665 } 8666 case Map_type::MAP_ALG_FAST64: 8667 case Map_type::MAP_ALG_FAST64PTR: 8668 { 8669 code = Runtime::MAPDELETE_FAST64; 8670 Type* uint64_type = Type::lookup_integer_type("uint64"); 8671 Type* uint64_ptr_type = Type::make_pointer_type(uint64_type); 8672 e3 = Expression::make_unary(OPERATOR_AND, e3, loc); 8673 e3 = Expression::make_unsafe_cast(uint64_ptr_type, e3, 8674 loc); 8675 e3 = Expression::make_dereference(e3, 8676 Expression::NIL_CHECK_NOT_NEEDED, 8677 loc); 8678 break; 8679 } 8680 case Map_type::MAP_ALG_FASTSTR: 8681 code = Runtime::MAPDELETE_FASTSTR; 8682 break; 8683 default: 8684 code = Runtime::MAPDELETE; 8685 8686 // If the call to delete is deferred, and is in a loop, 8687 // then the loop will only have a single instance of the 8688 // temporary variable. Passing the address of the 8689 // temporary variable here means that the deferred call 8690 // will see the last value in the loop, not the current 8691 // value. So for this unusual case copy the value into 8692 // the heap. 8693 if (!this->is_deferred()) 8694 e3 = Expression::make_unary(OPERATOR_AND, e3, loc); 8695 else 8696 { 8697 Expression* a = Expression::make_allocation(mt->key_type(), 8698 loc); 8699 Temporary_statement* atemp = 8700 Statement::make_temporary(NULL, a, loc); 8701 inserter->insert(atemp); 8702 8703 a = Expression::make_temporary_reference(atemp, loc); 8704 a = Expression::make_dereference(a, NIL_CHECK_NOT_NEEDED, loc); 8705 Statement* s = Statement::make_assignment(a, e3, loc); 8706 inserter->insert(s); 8707 8708 e3 = Expression::make_temporary_reference(atemp, loc); 8709 } 8710 } 8711 8712 return Runtime::make_call(code, loc, 3, e1, e2, e3); 8713 } 8714 8715 case BUILTIN_ADD: 8716 { 8717 Expression* ptr = this->args()->front(); 8718 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 8719 ptr = Expression::make_cast(uintptr_type, ptr, loc); 8720 Expression* len = this->args()->back(); 8721 len = Expression::make_cast(uintptr_type, len, loc); 8722 Expression* add = Expression::make_binary(OPERATOR_PLUS, ptr, len, 8723 loc); 8724 return Expression::make_cast(this->args()->front()->type(), add, loc); 8725 } 8726 8727 case BUILTIN_SLICE: 8728 { 8729 Expression* ptr = this->args()->front(); 8730 Temporary_statement* ptr_temp = NULL; 8731 if (!ptr->is_multi_eval_safe()) 8732 { 8733 ptr_temp = Statement::make_temporary(NULL, ptr, loc); 8734 inserter->insert(ptr_temp); 8735 ptr = Expression::make_temporary_reference(ptr_temp, loc); 8736 } 8737 8738 Expression* len = this->args()->back(); 8739 Temporary_statement* len_temp = NULL; 8740 if (!len->is_multi_eval_safe()) 8741 { 8742 len_temp = Statement::make_temporary(NULL, len, loc); 8743 inserter->insert(len_temp); 8744 len = Expression::make_temporary_reference(len_temp, loc); 8745 } 8746 8747 bool fits_in_int; 8748 Numeric_constant nc; 8749 if (this->args()->back()->numeric_constant_value(&nc)) 8750 { 8751 // We gave an error for constants that don't fit in int in 8752 // check_types. 8753 fits_in_int = true; 8754 } 8755 else 8756 { 8757 Integer_type* itype = this->args()->back()->type()->integer_type(); 8758 go_assert(itype != NULL); 8759 int ebits = itype->bits(); 8760 int intbits = 8761 Type::lookup_integer_type("int")->integer_type()->bits(); 8762 8763 // We can treat ebits == intbits as small even for an 8764 // unsigned integer type, because we will convert the 8765 // value to int and then reject it in the runtime if it is 8766 // negative. 8767 8768 fits_in_int = ebits <= intbits; 8769 } 8770 8771 Runtime::Function code = (fits_in_int 8772 ? Runtime::UNSAFESLICE 8773 : Runtime::UNSAFESLICE64); 8774 Expression* td = 8775 Expression::make_type_descriptor(ptr->type()->points_to(), loc); 8776 Expression* check = Runtime::make_call(code, loc, 3, 8777 td, ptr, len); 8778 8779 if (ptr_temp == NULL) 8780 ptr = ptr->copy(); 8781 else 8782 ptr = Expression::make_temporary_reference(ptr_temp, loc); 8783 Expression* nil = Expression::make_nil(loc); 8784 nil = Expression::make_cast(ptr->type(), nil, loc); 8785 Expression* is_nil = Expression::make_binary(OPERATOR_EQEQ, ptr, nil, 8786 loc); 8787 8788 if (len_temp == NULL) 8789 len = len->copy(); 8790 else 8791 len = Expression::make_temporary_reference(len_temp, loc); 8792 Expression* zero = Expression::make_integer_ul(0, len->type(), loc); 8793 Expression* is_zero = Expression::make_binary(OPERATOR_EQEQ, len, zero, 8794 loc); 8795 8796 Expression* cond = Expression::make_binary(OPERATOR_ANDAND, is_nil, 8797 is_zero, loc); 8798 8799 Type* slice_type = Type::make_array_type(ptr->type()->points_to(), 8800 NULL); 8801 nil = Expression::make_nil(loc); 8802 Expression* nil_slice = Expression::make_cast(slice_type, nil, loc); 8803 8804 if (ptr_temp == NULL) 8805 ptr = ptr->copy(); 8806 else 8807 ptr = Expression::make_temporary_reference(ptr_temp, loc); 8808 8809 if (len_temp == NULL) 8810 len = len->copy(); 8811 else 8812 len = Expression::make_temporary_reference(len_temp, loc); 8813 8814 Expression* cap; 8815 if (len_temp == NULL) 8816 cap = len->copy(); 8817 else 8818 cap = Expression::make_temporary_reference(len_temp, loc); 8819 8820 Expression* slice = Expression::make_slice_value(slice_type, ptr, 8821 len, cap, loc); 8822 8823 slice = Expression::make_conditional(cond, nil_slice, slice, loc); 8824 8825 return Expression::make_compound(check, slice, loc); 8826 } 8827 } 8828 8829 return this; 8830 } 8831 8832 // Lower a make expression. 8833 8834 Expression* 8835 Builtin_call_expression::lower_make(Statement_inserter* inserter) 8836 { 8837 Location loc = this->location(); 8838 8839 const Expression_list* args = this->args(); 8840 if (args == NULL || args->size() < 1) 8841 { 8842 this->report_error(_("not enough arguments")); 8843 return Expression::make_error(this->location()); 8844 } 8845 8846 Expression_list::const_iterator parg = args->begin(); 8847 8848 Expression* first_arg = *parg; 8849 if (!first_arg->is_type_expression()) 8850 { 8851 go_error_at(first_arg->location(), "expected type"); 8852 this->set_is_error(); 8853 return Expression::make_error(this->location()); 8854 } 8855 Type* type = first_arg->type(); 8856 8857 if (!type->in_heap()) 8858 go_error_at(first_arg->location(), 8859 "cannot make slice of go:notinheap type"); 8860 8861 bool is_slice = false; 8862 bool is_map = false; 8863 bool is_chan = false; 8864 if (type->is_slice_type()) 8865 is_slice = true; 8866 else if (type->map_type() != NULL) 8867 is_map = true; 8868 else if (type->channel_type() != NULL) 8869 is_chan = true; 8870 else 8871 { 8872 this->report_error(_("invalid type for make function")); 8873 return Expression::make_error(this->location()); 8874 } 8875 8876 Type_context int_context(Type::lookup_integer_type("int"), false); 8877 8878 ++parg; 8879 Expression* len_arg; 8880 bool len_small = false; 8881 if (parg == args->end()) 8882 { 8883 if (is_slice) 8884 { 8885 this->report_error(_("length required when allocating a slice")); 8886 return Expression::make_error(this->location()); 8887 } 8888 len_arg = Expression::make_integer_ul(0, NULL, loc); 8889 len_small = true; 8890 } 8891 else 8892 { 8893 len_arg = *parg; 8894 len_arg->determine_type(&int_context); 8895 if (len_arg->type()->integer_type() == NULL) 8896 { 8897 go_error_at(len_arg->location(), "non-integer len argument in make"); 8898 return Expression::make_error(this->location()); 8899 } 8900 if (!this->check_int_value(len_arg, true, &len_small)) 8901 return Expression::make_error(this->location()); 8902 ++parg; 8903 } 8904 8905 Expression* cap_arg = NULL; 8906 bool cap_small = false; 8907 Numeric_constant nclen; 8908 Numeric_constant nccap; 8909 unsigned long vlen; 8910 unsigned long vcap; 8911 if (is_slice && parg != args->end()) 8912 { 8913 cap_arg = *parg; 8914 cap_arg->determine_type(&int_context); 8915 if (cap_arg->type()->integer_type() == NULL) 8916 { 8917 go_error_at(cap_arg->location(), "non-integer cap argument in make"); 8918 return Expression::make_error(this->location()); 8919 } 8920 if (!this->check_int_value(cap_arg, false, &cap_small)) 8921 return Expression::make_error(this->location()); 8922 8923 if (len_arg->numeric_constant_value(&nclen) 8924 && cap_arg->numeric_constant_value(&nccap) 8925 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID 8926 && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID 8927 && vlen > vcap) 8928 { 8929 this->report_error(_("len larger than cap")); 8930 return Expression::make_error(this->location()); 8931 } 8932 8933 ++parg; 8934 } 8935 8936 if (parg != args->end()) 8937 { 8938 this->report_error(_("too many arguments to make")); 8939 return Expression::make_error(this->location()); 8940 } 8941 8942 Location type_loc = first_arg->location(); 8943 8944 Expression* call; 8945 if (is_slice) 8946 { 8947 Temporary_statement* len_temp = NULL; 8948 if (!len_arg->is_constant()) 8949 { 8950 len_temp = Statement::make_temporary(NULL, len_arg, loc); 8951 inserter->insert(len_temp); 8952 len_arg = Expression::make_temporary_reference(len_temp, loc); 8953 } 8954 8955 if (cap_arg == NULL) 8956 { 8957 cap_small = len_small; 8958 if (len_temp == NULL) 8959 cap_arg = len_arg->copy(); 8960 else 8961 cap_arg = Expression::make_temporary_reference(len_temp, loc); 8962 } 8963 else if (!cap_arg->is_constant()) 8964 { 8965 Temporary_statement* cap_temp = Statement::make_temporary(NULL, 8966 cap_arg, 8967 loc); 8968 inserter->insert(cap_temp); 8969 cap_arg = Expression::make_temporary_reference(cap_temp, loc); 8970 } 8971 8972 Type* et = type->array_type()->element_type(); 8973 Expression* type_arg = Expression::make_type_descriptor(et, type_loc); 8974 Runtime::Function code = Runtime::MAKESLICE; 8975 if (!len_small || !cap_small) 8976 code = Runtime::MAKESLICE64; 8977 Expression* mem = Runtime::make_call(code, loc, 3, type_arg, len_arg, 8978 cap_arg); 8979 mem = Expression::make_unsafe_cast(Type::make_pointer_type(et), mem, 8980 loc); 8981 Type* int_type = Type::lookup_integer_type("int"); 8982 len_arg = Expression::make_cast(int_type, len_arg->copy(), loc); 8983 cap_arg = Expression::make_cast(int_type, cap_arg->copy(), loc); 8984 call = Expression::make_slice_value(type, mem, len_arg, cap_arg, loc); 8985 } 8986 else if (is_map) 8987 { 8988 Expression* type_arg = Expression::make_type_descriptor(type, type_loc); 8989 if (!len_small) 8990 call = Runtime::make_call(Runtime::MAKEMAP64, loc, 3, type_arg, 8991 len_arg, 8992 Expression::make_nil(loc)); 8993 else 8994 { 8995 if (len_arg->numeric_constant_value(&nclen) 8996 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID 8997 && vlen <= Map_type::bucket_size) 8998 call = Runtime::make_call(Runtime::MAKEMAP_SMALL, loc, 0); 8999 else 9000 call = Runtime::make_call(Runtime::MAKEMAP, loc, 3, type_arg, 9001 len_arg, 9002 Expression::make_nil(loc)); 9003 } 9004 } 9005 else if (is_chan) 9006 { 9007 Expression* type_arg = Expression::make_type_descriptor(type, type_loc); 9008 Runtime::Function code = Runtime::MAKECHAN; 9009 if (!len_small) 9010 code = Runtime::MAKECHAN64; 9011 call = Runtime::make_call(code, loc, 2, type_arg, len_arg); 9012 } 9013 else 9014 go_unreachable(); 9015 9016 return Expression::make_unsafe_cast(type, call, loc); 9017 } 9018 9019 // Flatten a call to the predeclared append function. We do this in 9020 // the flatten phase, not the lowering phase, so that we run after 9021 // type checking and after order_evaluations. If ASSIGN_LHS is not 9022 // NULL, this append is the right-hand-side of an assignment and 9023 // ASSIGN_LHS is the left-hand-side; in that case, set LHS directly 9024 // rather than returning a slice. This lets us omit a write barrier 9025 // in common cases like a = append(a, ...) when the slice does not 9026 // need to grow. ENCLOSING is not NULL iff ASSIGN_LHS is not NULL. 9027 9028 Expression* 9029 Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function, 9030 Statement_inserter* inserter, 9031 Expression* assign_lhs, 9032 Block* enclosing) 9033 { 9034 if (this->is_error_expression()) 9035 return this; 9036 9037 Location loc = this->location(); 9038 9039 const Expression_list* args = this->args(); 9040 go_assert(args != NULL && !args->empty()); 9041 9042 Type* slice_type = args->front()->type(); 9043 go_assert(slice_type->is_slice_type()); 9044 Type* element_type = slice_type->array_type()->element_type(); 9045 9046 if (args->size() == 1) 9047 { 9048 // append(s) evaluates to s. 9049 if (assign_lhs != NULL) 9050 return NULL; 9051 return args->front(); 9052 } 9053 9054 Type* int_type = Type::lookup_integer_type("int"); 9055 Type* uint_type = Type::lookup_integer_type("uint"); 9056 9057 // Implementing 9058 // append(s1, s2...) 9059 // or 9060 // append(s1, a1, a2, a3, ...) 9061 9062 // s1tmp := s1 9063 Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(), 9064 loc); 9065 inserter->insert(s1tmp); 9066 9067 // l1tmp := len(s1tmp) 9068 Named_object* lenfn = gogo->lookup_global("len"); 9069 Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); 9070 Expression_list* call_args = new Expression_list(); 9071 call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); 9072 Expression* len = Expression::make_call(lenref, call_args, false, loc); 9073 gogo->lower_expression(function, inserter, &len); 9074 gogo->flatten_expression(function, inserter, &len); 9075 Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc); 9076 inserter->insert(l1tmp); 9077 9078 Temporary_statement* s2tmp = NULL; 9079 Temporary_statement* l2tmp = NULL; 9080 Expression_list* add = NULL; 9081 Expression* len2; 9082 Call_expression* makecall = NULL; 9083 if (this->is_varargs()) 9084 { 9085 go_assert(args->size() == 2); 9086 9087 std::pair<Call_expression*, Temporary_statement*> p = 9088 Expression::find_makeslice_call(args->back()); 9089 makecall = p.first; 9090 if (makecall != NULL) 9091 { 9092 // We are handling 9093 // append(s, make([]T, len[, cap])...)) 9094 // which has already been lowered to 9095 // append(s, runtime.makeslice(T, len, cap)). 9096 // We will optimize this to directly zeroing the tail, 9097 // instead of allocating a new slice then copy. 9098 9099 // Retrieve the length and capacity. Cannot reference s2 as 9100 // we will remove the makeslice call. 9101 Expression* len_arg = makecall->args()->at(1); 9102 len_arg = Expression::make_cast(int_type, len_arg, loc); 9103 l2tmp = Statement::make_temporary(int_type, len_arg, loc); 9104 inserter->insert(l2tmp); 9105 9106 Expression* cap_arg = makecall->args()->at(2); 9107 cap_arg = Expression::make_cast(int_type, cap_arg, loc); 9108 Temporary_statement* c2tmp = 9109 Statement::make_temporary(int_type, cap_arg, loc); 9110 inserter->insert(c2tmp); 9111 9112 // Check bad len/cap here. 9113 // checkmakeslice(type, len, cap) 9114 // (Note that if len and cap are constants, we won't see a 9115 // makeslice call here, as it will be rewritten to a stack 9116 // allocated array by Mark_address_taken::expression.) 9117 Expression* elem = Expression::make_type_descriptor(element_type, 9118 loc); 9119 len2 = Expression::make_temporary_reference(l2tmp, loc); 9120 Expression* cap2 = Expression::make_temporary_reference(c2tmp, loc); 9121 Expression* check = Runtime::make_call(Runtime::CHECK_MAKE_SLICE, 9122 loc, 3, elem, len2, cap2); 9123 gogo->lower_expression(function, inserter, &check); 9124 gogo->flatten_expression(function, inserter, &check); 9125 Statement* s = Statement::make_statement(check, false); 9126 inserter->insert(s); 9127 9128 // Remove the original makeslice call. 9129 Temporary_statement* ts = p.second; 9130 if (ts != NULL && ts->uses() == 1) 9131 ts->set_init(Expression::make_nil(loc)); 9132 } 9133 else 9134 { 9135 // s2tmp := s2 9136 s2tmp = Statement::make_temporary(NULL, args->back(), loc); 9137 inserter->insert(s2tmp); 9138 9139 // l2tmp := len(s2tmp) 9140 lenref = Expression::make_func_reference(lenfn, NULL, loc); 9141 call_args = new Expression_list(); 9142 call_args->push_back(Expression::make_temporary_reference(s2tmp, loc)); 9143 len = Expression::make_call(lenref, call_args, false, loc); 9144 gogo->lower_expression(function, inserter, &len); 9145 gogo->flatten_expression(function, inserter, &len); 9146 l2tmp = Statement::make_temporary(int_type, len, loc); 9147 inserter->insert(l2tmp); 9148 } 9149 9150 // len2 = l2tmp 9151 len2 = Expression::make_temporary_reference(l2tmp, loc); 9152 } 9153 else 9154 { 9155 // We have to ensure that all the arguments are in variables 9156 // now, because otherwise if one of them is an index expression 9157 // into the current slice we could overwrite it before we fetch 9158 // it. 9159 add = new Expression_list(); 9160 Expression_list::const_iterator pa = args->begin(); 9161 for (++pa; pa != args->end(); ++pa) 9162 { 9163 if ((*pa)->is_multi_eval_safe()) 9164 add->push_back(*pa); 9165 else 9166 { 9167 Temporary_statement* tmp = Statement::make_temporary(NULL, *pa, 9168 loc); 9169 inserter->insert(tmp); 9170 add->push_back(Expression::make_temporary_reference(tmp, loc)); 9171 } 9172 } 9173 9174 // len2 = len(add) 9175 len2 = Expression::make_integer_ul(add->size(), int_type, loc); 9176 } 9177 9178 // ntmp := l1tmp + len2 9179 Expression* ref = Expression::make_temporary_reference(l1tmp, loc); 9180 Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc); 9181 gogo->lower_expression(function, inserter, &sum); 9182 gogo->flatten_expression(function, inserter, &sum); 9183 Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc); 9184 inserter->insert(ntmp); 9185 9186 // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ? 9187 // growslice(type, s1tmp, ntmp) : 9188 // s1tmp[:ntmp] 9189 // Using uint here means that if the computation of ntmp overflowed, 9190 // we will call growslice which will panic. 9191 9192 Named_object* capfn = gogo->lookup_global("cap"); 9193 Expression* capref = Expression::make_func_reference(capfn, NULL, loc); 9194 call_args = new Expression_list(); 9195 call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); 9196 Expression* cap = Expression::make_call(capref, call_args, false, loc); 9197 gogo->lower_expression(function, inserter, &cap); 9198 gogo->flatten_expression(function, inserter, &cap); 9199 Temporary_statement* c1tmp = Statement::make_temporary(int_type, cap, loc); 9200 inserter->insert(c1tmp); 9201 9202 Expression* left = Expression::make_temporary_reference(ntmp, loc); 9203 left = Expression::make_cast(uint_type, left, loc); 9204 Expression* right = Expression::make_temporary_reference(c1tmp, loc); 9205 right = Expression::make_cast(uint_type, right, loc); 9206 9207 Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc); 9208 9209 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); 9210 Expression* a1 = Expression::make_type_descriptor(element_type, loc); 9211 Expression* a2 = Expression::make_temporary_reference(s1tmp, loc); 9212 a2 = slice_type->array_type()->get_value_pointer(gogo, a2); 9213 a2 = Expression::make_cast(unsafe_ptr_type, a2, loc); 9214 Expression* a3 = Expression::make_temporary_reference(l1tmp, loc); 9215 Expression* a4 = Expression::make_temporary_reference(c1tmp, loc); 9216 Expression* a5 = Expression::make_temporary_reference(ntmp, loc); 9217 Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 5, 9218 a1, a2, a3, a4, a5); 9219 call = Expression::make_unsafe_cast(slice_type, call, loc); 9220 9221 ref = Expression::make_temporary_reference(s1tmp, loc); 9222 Expression* zero = Expression::make_integer_ul(0, int_type, loc); 9223 Expression* ref2 = Expression::make_temporary_reference(ntmp, loc); 9224 ref = Expression::make_array_index(ref, zero, ref2, NULL, loc); 9225 ref->array_index_expression()->set_needs_bounds_check(false); 9226 9227 if (assign_lhs == NULL) 9228 { 9229 Expression* rhs = Expression::make_conditional(cond, call, ref, loc); 9230 9231 gogo->lower_expression(function, inserter, &rhs); 9232 gogo->flatten_expression(function, inserter, &rhs); 9233 9234 ref = Expression::make_temporary_reference(s1tmp, loc); 9235 Statement* assign = Statement::make_assignment(ref, rhs, loc); 9236 inserter->insert(assign); 9237 } 9238 else 9239 { 9240 gogo->lower_expression(function, inserter, &cond); 9241 gogo->flatten_expression(function, inserter, &cond); 9242 gogo->lower_expression(function, inserter, &call); 9243 gogo->flatten_expression(function, inserter, &call); 9244 gogo->lower_expression(function, inserter, &ref); 9245 gogo->flatten_expression(function, inserter, &ref); 9246 9247 Block* then_block = new Block(enclosing, loc); 9248 Assignment_statement* assign = 9249 Statement::make_assignment(assign_lhs, call, loc); 9250 then_block->add_statement(assign); 9251 9252 Block* else_block = new Block(enclosing, loc); 9253 assign = Statement::make_assignment(assign_lhs->copy(), ref, loc); 9254 // This assignment will not change the pointer value, so it does 9255 // not need a write barrier. 9256 assign->set_omit_write_barrier(); 9257 else_block->add_statement(assign); 9258 9259 Statement* s = Statement::make_if_statement(cond, then_block, 9260 else_block, loc); 9261 inserter->insert(s); 9262 9263 ref = Expression::make_temporary_reference(s1tmp, loc); 9264 assign = Statement::make_assignment(ref, assign_lhs->copy(), loc); 9265 inserter->insert(assign); 9266 } 9267 9268 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 9269 9270 if (this->is_varargs()) 9271 { 9272 if (makecall != NULL) 9273 { 9274 // memclr(&s1tmp[l1tmp], l2tmp*sizeof(elem)) 9275 a1 = Expression::make_temporary_reference(s1tmp, loc); 9276 ref = Expression::make_temporary_reference(l1tmp, loc); 9277 a1 = Expression::make_array_index(a1, ref, NULL, NULL, loc); 9278 a1->array_index_expression()->set_needs_bounds_check(false); 9279 a1 = Expression::make_unary(OPERATOR_AND, a1, loc); 9280 9281 ref = Expression::make_temporary_reference(l2tmp, loc); 9282 ref = Expression::make_cast(uintptr_type, ref, loc); 9283 a2 = Expression::make_type_info(element_type, TYPE_INFO_SIZE); 9284 a2 = Expression::make_binary(OPERATOR_MULT, a2, ref, loc); 9285 9286 if (element_type->has_pointer()) 9287 call = Runtime::make_call(Runtime::MEMCLRHASPTR, loc, 2, a1, a2); 9288 else 9289 { 9290 Type* int32_type = Type::lookup_integer_type("int32"); 9291 zero = Expression::make_integer_ul(0, int32_type, loc); 9292 call = Runtime::make_call(Runtime::BUILTIN_MEMSET, loc, 3, a1, 9293 zero, a2); 9294 } 9295 9296 if (element_type->has_pointer()) 9297 { 9298 // For a slice containing pointers, growslice already zeroed 9299 // the memory. We only need to zero in non-growing case. 9300 // Note: growslice does not zero the memory in non-pointer case. 9301 ref = Expression::make_temporary_reference(ntmp, loc); 9302 ref = Expression::make_cast(uint_type, ref, loc); 9303 ref2 = Expression::make_temporary_reference(c1tmp, loc); 9304 ref2 = Expression::make_cast(uint_type, ref2, loc); 9305 cond = Expression::make_binary(OPERATOR_GT, ref, ref2, loc); 9306 zero = Expression::make_integer_ul(0, int_type, loc); 9307 call = Expression::make_conditional(cond, zero, call, loc); 9308 } 9309 } 9310 else 9311 { 9312 if (element_type->has_pointer()) 9313 { 9314 // copy(s1tmp[l1tmp:], s2tmp) 9315 a1 = Expression::make_temporary_reference(s1tmp, loc); 9316 ref = Expression::make_temporary_reference(l1tmp, loc); 9317 Expression* nil = Expression::make_nil(loc); 9318 a1 = Expression::make_array_index(a1, ref, nil, NULL, loc); 9319 a1->array_index_expression()->set_needs_bounds_check(false); 9320 9321 a2 = Expression::make_temporary_reference(s2tmp, loc); 9322 9323 Named_object* copyfn = gogo->lookup_global("copy"); 9324 Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc); 9325 call_args = new Expression_list(); 9326 call_args->push_back(a1); 9327 call_args->push_back(a2); 9328 call = Expression::make_call(copyref, call_args, false, loc); 9329 } 9330 else 9331 { 9332 // memmove(&s1tmp[l1tmp], s2tmp.ptr, l2tmp*sizeof(elem)) 9333 a1 = Expression::make_temporary_reference(s1tmp, loc); 9334 ref = Expression::make_temporary_reference(l1tmp, loc); 9335 a1 = Expression::make_array_index(a1, ref, NULL, NULL, loc); 9336 a1->array_index_expression()->set_needs_bounds_check(false); 9337 a1 = Expression::make_unary(OPERATOR_AND, a1, loc); 9338 9339 a2 = Expression::make_temporary_reference(s2tmp, loc); 9340 a2 = (a2->type()->is_string_type() 9341 ? Expression::make_string_info(a2, 9342 STRING_INFO_DATA, 9343 loc) 9344 : Expression::make_slice_info(a2, 9345 SLICE_INFO_VALUE_POINTER, 9346 loc)); 9347 9348 ref = Expression::make_temporary_reference(l2tmp, loc); 9349 ref = Expression::make_cast(uintptr_type, ref, loc); 9350 a3 = Expression::make_type_info(element_type, TYPE_INFO_SIZE); 9351 a3 = Expression::make_binary(OPERATOR_MULT, a3, ref, loc); 9352 9353 call = Runtime::make_call(Runtime::BUILTIN_MEMMOVE, loc, 3, 9354 a1, a2, a3); 9355 } 9356 } 9357 gogo->lower_expression(function, inserter, &call); 9358 gogo->flatten_expression(function, inserter, &call); 9359 inserter->insert(Statement::make_statement(call, false)); 9360 } 9361 else 9362 { 9363 // For each argument: 9364 // s1tmp[l1tmp+i] = a 9365 unsigned long i = 0; 9366 for (Expression_list::const_iterator pa = add->begin(); 9367 pa != add->end(); 9368 ++pa, ++i) 9369 { 9370 ref = Expression::make_temporary_reference(s1tmp, loc); 9371 ref2 = Expression::make_temporary_reference(l1tmp, loc); 9372 Expression* off = Expression::make_integer_ul(i, int_type, loc); 9373 ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc); 9374 Expression* lhs = Expression::make_array_index(ref, ref2, NULL, 9375 NULL, loc); 9376 lhs->array_index_expression()->set_needs_bounds_check(false); 9377 gogo->lower_expression(function, inserter, &lhs); 9378 gogo->flatten_expression(function, inserter, &lhs); 9379 Expression* elem = *pa; 9380 if (!Type::are_identical(element_type, elem->type(), 0, NULL) 9381 && element_type->interface_type() != NULL) 9382 elem = Expression::make_cast(element_type, elem, loc); 9383 // The flatten pass runs after the write barrier pass, so we 9384 // need to insert a write barrier here if necessary. 9385 // However, if ASSIGN_LHS is not NULL, we have been called 9386 // directly before the write barrier pass. 9387 Statement* assign; 9388 if (assign_lhs != NULL 9389 || !gogo->assign_needs_write_barrier(lhs, NULL)) 9390 assign = Statement::make_assignment(lhs, elem, loc); 9391 else 9392 { 9393 Function* f = function == NULL ? NULL : function->func_value(); 9394 assign = gogo->assign_with_write_barrier(f, NULL, inserter, 9395 lhs, elem, loc); 9396 } 9397 inserter->insert(assign); 9398 } 9399 } 9400 9401 if (assign_lhs != NULL) 9402 return NULL; 9403 9404 return Expression::make_temporary_reference(s1tmp, loc); 9405 } 9406 9407 // Return whether an expression has an integer value. Report an error 9408 // if not. This is used when handling calls to the predeclared make 9409 // function. Set *SMALL if the value is known to fit in type "int". 9410 9411 bool 9412 Builtin_call_expression::check_int_value(Expression* e, bool is_length, 9413 bool *small) 9414 { 9415 *small = false; 9416 9417 Numeric_constant nc; 9418 if (e->numeric_constant_value(&nc)) 9419 { 9420 unsigned long v; 9421 switch (nc.to_unsigned_long(&v)) 9422 { 9423 case Numeric_constant::NC_UL_VALID: 9424 break; 9425 case Numeric_constant::NC_UL_NOTINT: 9426 go_error_at(e->location(), "non-integer %s argument to make", 9427 is_length ? "len" : "cap"); 9428 return false; 9429 case Numeric_constant::NC_UL_NEGATIVE: 9430 go_error_at(e->location(), "negative %s argument to make", 9431 is_length ? "len" : "cap"); 9432 return false; 9433 case Numeric_constant::NC_UL_BIG: 9434 // We don't want to give a compile-time error for a 64-bit 9435 // value on a 32-bit target. 9436 break; 9437 } 9438 9439 mpz_t val; 9440 if (!nc.to_int(&val)) 9441 go_unreachable(); 9442 int bits = mpz_sizeinbase(val, 2); 9443 mpz_clear(val); 9444 Type* int_type = Type::lookup_integer_type("int"); 9445 if (bits >= int_type->integer_type()->bits()) 9446 { 9447 go_error_at(e->location(), "%s argument too large for make", 9448 is_length ? "len" : "cap"); 9449 return false; 9450 } 9451 9452 *small = true; 9453 return true; 9454 } 9455 9456 if (e->type()->integer_type() != NULL) 9457 { 9458 int ebits = e->type()->integer_type()->bits(); 9459 int intbits = Type::lookup_integer_type("int")->integer_type()->bits(); 9460 9461 // We can treat ebits == intbits as small even for an unsigned 9462 // integer type, because we will convert the value to int and 9463 // then reject it in the runtime if it is negative. 9464 *small = ebits <= intbits; 9465 9466 return true; 9467 } 9468 9469 go_error_at(e->location(), "non-integer %s argument to make", 9470 is_length ? "len" : "cap"); 9471 return false; 9472 } 9473 9474 // Return the type of the real or imag functions, given the type of 9475 // the argument. We need to map complex64 to float32 and complex128 9476 // to float64, so it has to be done by name. This returns NULL if it 9477 // can't figure out the type. 9478 9479 Type* 9480 Builtin_call_expression::real_imag_type(Type* arg_type) 9481 { 9482 if (arg_type == NULL || arg_type->is_abstract()) 9483 return NULL; 9484 Named_type* nt = arg_type->named_type(); 9485 if (nt == NULL) 9486 return NULL; 9487 while (nt->real_type()->named_type() != NULL) 9488 nt = nt->real_type()->named_type(); 9489 if (nt->name() == "complex64") 9490 return Type::lookup_float_type("float32"); 9491 else if (nt->name() == "complex128") 9492 return Type::lookup_float_type("float64"); 9493 else 9494 return NULL; 9495 } 9496 9497 // Return the type of the complex function, given the type of one of the 9498 // argments. Like real_imag_type, we have to map by name. 9499 9500 Type* 9501 Builtin_call_expression::complex_type(Type* arg_type) 9502 { 9503 if (arg_type == NULL || arg_type->is_abstract()) 9504 return NULL; 9505 Named_type* nt = arg_type->named_type(); 9506 if (nt == NULL) 9507 return NULL; 9508 while (nt->real_type()->named_type() != NULL) 9509 nt = nt->real_type()->named_type(); 9510 if (nt->name() == "float32") 9511 return Type::lookup_complex_type("complex64"); 9512 else if (nt->name() == "float64") 9513 return Type::lookup_complex_type("complex128"); 9514 else 9515 return NULL; 9516 } 9517 9518 // Return a single argument, or NULL if there isn't one. 9519 9520 Expression* 9521 Builtin_call_expression::one_arg() const 9522 { 9523 const Expression_list* args = this->args(); 9524 if (args == NULL || args->size() != 1) 9525 return NULL; 9526 return args->front(); 9527 } 9528 9529 // A traversal class which looks for a call or receive expression. 9530 9531 class Find_call_expression : public Traverse 9532 { 9533 public: 9534 Find_call_expression() 9535 : Traverse(traverse_expressions), 9536 found_(false) 9537 { } 9538 9539 int 9540 expression(Expression**); 9541 9542 bool 9543 found() 9544 { return this->found_; } 9545 9546 private: 9547 bool found_; 9548 }; 9549 9550 int 9551 Find_call_expression::expression(Expression** pexpr) 9552 { 9553 Expression* expr = *pexpr; 9554 if (!expr->is_constant() 9555 && (expr->call_expression() != NULL 9556 || expr->receive_expression() != NULL)) 9557 { 9558 this->found_ = true; 9559 return TRAVERSE_EXIT; 9560 } 9561 return TRAVERSE_CONTINUE; 9562 } 9563 9564 // Return whether calling len or cap on EXPR, of array type, is a 9565 // constant. The language spec says "the expressions len(s) and 9566 // cap(s) are constants if the type of s is an array or pointer to an 9567 // array and the expression s does not contain channel receives or 9568 // (non-constant) function calls." 9569 9570 bool 9571 Builtin_call_expression::array_len_is_constant(Expression* expr) 9572 { 9573 go_assert(expr->type()->deref()->array_type() != NULL 9574 && !expr->type()->deref()->is_slice_type()); 9575 if (expr->is_constant()) 9576 return true; 9577 Find_call_expression find_call; 9578 Expression::traverse(&expr, &find_call); 9579 return !find_call.found(); 9580 } 9581 9582 // Return whether this is constant: len of a string constant, or len 9583 // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, 9584 // unsafe.Alignof. 9585 9586 bool 9587 Builtin_call_expression::do_is_constant() const 9588 { 9589 if (this->is_error_expression()) 9590 return true; 9591 switch (this->code_) 9592 { 9593 case BUILTIN_LEN: 9594 case BUILTIN_CAP: 9595 { 9596 if (this->seen_) 9597 return false; 9598 9599 Expression* arg = this->one_arg(); 9600 if (arg == NULL) 9601 return false; 9602 Type* arg_type = arg->type(); 9603 if (arg_type->is_error()) 9604 return true; 9605 9606 if (arg_type->points_to() != NULL 9607 && arg_type->points_to()->array_type() != NULL 9608 && !arg_type->points_to()->is_slice_type()) 9609 arg_type = arg_type->points_to(); 9610 9611 if (arg_type->array_type() != NULL 9612 && arg_type->array_type()->length() != NULL) 9613 { 9614 this->seen_ = true; 9615 bool ret = Builtin_call_expression::array_len_is_constant(arg); 9616 this->seen_ = false; 9617 return ret; 9618 } 9619 9620 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) 9621 { 9622 this->seen_ = true; 9623 bool ret = arg->is_constant(); 9624 this->seen_ = false; 9625 return ret; 9626 } 9627 } 9628 break; 9629 9630 case BUILTIN_SIZEOF: 9631 case BUILTIN_ALIGNOF: 9632 return this->one_arg() != NULL; 9633 9634 case BUILTIN_OFFSETOF: 9635 { 9636 Expression* arg = this->one_arg(); 9637 if (arg == NULL) 9638 return false; 9639 return arg->field_reference_expression() != NULL; 9640 } 9641 9642 case BUILTIN_COMPLEX: 9643 { 9644 const Expression_list* args = this->args(); 9645 if (args != NULL && args->size() == 2) 9646 return args->front()->is_constant() && args->back()->is_constant(); 9647 } 9648 break; 9649 9650 case BUILTIN_REAL: 9651 case BUILTIN_IMAG: 9652 { 9653 Expression* arg = this->one_arg(); 9654 return arg != NULL && arg->is_constant(); 9655 } 9656 9657 default: 9658 break; 9659 } 9660 9661 return false; 9662 } 9663 9664 // Return a numeric constant if possible. 9665 9666 bool 9667 Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const 9668 { 9669 if (this->code_ == BUILTIN_LEN 9670 || this->code_ == BUILTIN_CAP) 9671 { 9672 Expression* arg = this->one_arg(); 9673 if (arg == NULL) 9674 return false; 9675 Type* arg_type = arg->type(); 9676 if (arg_type->is_error()) 9677 return false; 9678 9679 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) 9680 { 9681 std::string sval; 9682 if (arg->string_constant_value(&sval)) 9683 { 9684 nc->set_unsigned_long(Type::lookup_integer_type("int"), 9685 sval.length()); 9686 return true; 9687 } 9688 } 9689 9690 if (arg_type->points_to() != NULL 9691 && arg_type->points_to()->array_type() != NULL 9692 && !arg_type->points_to()->is_slice_type()) 9693 arg_type = arg_type->points_to(); 9694 9695 if (arg_type->array_type() != NULL 9696 && arg_type->array_type()->length() != NULL) 9697 { 9698 if (this->seen_) 9699 return false; 9700 9701 // We may be replacing this expression with a constant 9702 // during lowering, so verify the type to report any errors. 9703 // It's OK to verify an array type more than once. 9704 arg_type->verify(); 9705 if (!arg_type->is_error()) 9706 { 9707 Expression* e = arg_type->array_type()->length(); 9708 this->seen_ = true; 9709 bool r = e->numeric_constant_value(nc); 9710 this->seen_ = false; 9711 if (r) 9712 { 9713 if (!nc->set_type(Type::lookup_integer_type("int"), false, 9714 this->location())) 9715 r = false; 9716 } 9717 return r; 9718 } 9719 } 9720 } 9721 else if (this->code_ == BUILTIN_SIZEOF 9722 || this->code_ == BUILTIN_ALIGNOF) 9723 { 9724 Expression* arg = this->one_arg(); 9725 if (arg == NULL) 9726 return false; 9727 Type* arg_type = arg->type(); 9728 if (arg_type->is_error()) 9729 return false; 9730 if (arg_type->is_abstract()) 9731 arg_type = arg_type->make_non_abstract_type(); 9732 if (this->seen_) 9733 return false; 9734 9735 int64_t ret; 9736 if (this->code_ == BUILTIN_SIZEOF) 9737 { 9738 this->seen_ = true; 9739 bool ok = arg_type->backend_type_size(this->gogo_, &ret); 9740 this->seen_ = false; 9741 if (!ok) 9742 return false; 9743 } 9744 else if (this->code_ == BUILTIN_ALIGNOF) 9745 { 9746 bool ok; 9747 this->seen_ = true; 9748 if (arg->field_reference_expression() == NULL) 9749 ok = arg_type->backend_type_align(this->gogo_, &ret); 9750 else 9751 { 9752 // Calling unsafe.Alignof(s.f) returns the alignment of 9753 // the type of f when it is used as a field in a struct. 9754 ok = arg_type->backend_type_field_align(this->gogo_, &ret); 9755 } 9756 this->seen_ = false; 9757 if (!ok) 9758 return false; 9759 } 9760 else 9761 go_unreachable(); 9762 9763 mpz_t zval; 9764 set_mpz_from_int64(&zval, ret); 9765 nc->set_int(Type::lookup_integer_type("uintptr"), zval); 9766 mpz_clear(zval); 9767 return true; 9768 } 9769 else if (this->code_ == BUILTIN_OFFSETOF) 9770 { 9771 Expression* arg = this->one_arg(); 9772 if (arg == NULL) 9773 return false; 9774 Field_reference_expression* farg = arg->field_reference_expression(); 9775 if (farg == NULL) 9776 return false; 9777 if (this->seen_) 9778 return false; 9779 9780 int64_t total_offset = 0; 9781 while (true) 9782 { 9783 Expression* struct_expr = farg->expr(); 9784 Type* st = struct_expr->type(); 9785 if (st->struct_type() == NULL) 9786 return false; 9787 if (st->named_type() != NULL) 9788 st->named_type()->convert(this->gogo_); 9789 if (st->is_error_type()) 9790 { 9791 go_assert(saw_errors()); 9792 return false; 9793 } 9794 int64_t offset; 9795 this->seen_ = true; 9796 bool ok = st->struct_type()->backend_field_offset(this->gogo_, 9797 farg->field_index(), 9798 &offset); 9799 this->seen_ = false; 9800 if (!ok) 9801 return false; 9802 total_offset += offset; 9803 if (farg->implicit() && struct_expr->field_reference_expression() != NULL) 9804 { 9805 // Go up until we reach the original base. 9806 farg = struct_expr->field_reference_expression(); 9807 continue; 9808 } 9809 break; 9810 } 9811 mpz_t zval; 9812 set_mpz_from_int64(&zval, total_offset); 9813 nc->set_int(Type::lookup_integer_type("uintptr"), zval); 9814 mpz_clear(zval); 9815 return true; 9816 } 9817 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) 9818 { 9819 Expression* arg = this->one_arg(); 9820 if (arg == NULL) 9821 return false; 9822 9823 Numeric_constant argnc; 9824 if (!arg->numeric_constant_value(&argnc)) 9825 return false; 9826 9827 mpc_t val; 9828 if (!argnc.to_complex(&val)) 9829 return false; 9830 9831 Type* type = Builtin_call_expression::real_imag_type(argnc.type()); 9832 if (this->code_ == BUILTIN_REAL) 9833 nc->set_float(type, mpc_realref(val)); 9834 else 9835 nc->set_float(type, mpc_imagref(val)); 9836 mpc_clear(val); 9837 return true; 9838 } 9839 else if (this->code_ == BUILTIN_COMPLEX) 9840 { 9841 const Expression_list* args = this->args(); 9842 if (args == NULL || args->size() != 2) 9843 return false; 9844 9845 Numeric_constant rnc; 9846 if (!args->front()->numeric_constant_value(&rnc)) 9847 return false; 9848 Numeric_constant inc; 9849 if (!args->back()->numeric_constant_value(&inc)) 9850 return false; 9851 9852 if (rnc.type() != NULL 9853 && !rnc.type()->is_abstract() 9854 && inc.type() != NULL 9855 && !inc.type()->is_abstract() 9856 && !Type::are_identical(rnc.type(), inc.type(), 9857 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 9858 NULL)) 9859 return false; 9860 9861 mpfr_t r; 9862 if (!rnc.to_float(&r)) 9863 return false; 9864 mpfr_t i; 9865 if (!inc.to_float(&i)) 9866 { 9867 mpfr_clear(r); 9868 return false; 9869 } 9870 9871 Type* arg_type = rnc.type(); 9872 if (arg_type == NULL || arg_type->is_abstract()) 9873 arg_type = inc.type(); 9874 9875 mpc_t val; 9876 mpc_init2(val, mpc_precision); 9877 mpc_set_fr_fr(val, r, i, MPC_RNDNN); 9878 mpfr_clear(r); 9879 mpfr_clear(i); 9880 9881 Type* type = Builtin_call_expression::complex_type(arg_type); 9882 nc->set_complex(type, val); 9883 9884 mpc_clear(val); 9885 9886 return true; 9887 } 9888 9889 return false; 9890 } 9891 9892 // Give an error if we are discarding the value of an expression which 9893 // should not normally be discarded. We don't give an error for 9894 // discarding the value of an ordinary function call, but we do for 9895 // builtin functions, purely for consistency with the gc compiler. 9896 9897 bool 9898 Builtin_call_expression::do_discarding_value() 9899 { 9900 switch (this->code_) 9901 { 9902 case BUILTIN_INVALID: 9903 default: 9904 go_unreachable(); 9905 9906 case BUILTIN_APPEND: 9907 case BUILTIN_CAP: 9908 case BUILTIN_COMPLEX: 9909 case BUILTIN_IMAG: 9910 case BUILTIN_LEN: 9911 case BUILTIN_MAKE: 9912 case BUILTIN_NEW: 9913 case BUILTIN_REAL: 9914 case BUILTIN_ADD: 9915 case BUILTIN_ALIGNOF: 9916 case BUILTIN_OFFSETOF: 9917 case BUILTIN_SIZEOF: 9918 case BUILTIN_SLICE: 9919 this->unused_value_error(); 9920 return false; 9921 9922 case BUILTIN_CLOSE: 9923 case BUILTIN_COPY: 9924 case BUILTIN_DELETE: 9925 case BUILTIN_PANIC: 9926 case BUILTIN_PRINT: 9927 case BUILTIN_PRINTLN: 9928 case BUILTIN_RECOVER: 9929 return true; 9930 } 9931 } 9932 9933 // Return the type. 9934 9935 Type* 9936 Builtin_call_expression::do_type() 9937 { 9938 if (this->is_error_expression()) 9939 return Type::make_error_type(); 9940 switch (this->code_) 9941 { 9942 case BUILTIN_INVALID: 9943 default: 9944 return Type::make_error_type(); 9945 9946 case BUILTIN_NEW: 9947 { 9948 const Expression_list* args = this->args(); 9949 if (args == NULL || args->empty()) 9950 return Type::make_error_type(); 9951 return Type::make_pointer_type(args->front()->type()); 9952 } 9953 9954 case BUILTIN_MAKE: 9955 { 9956 const Expression_list* args = this->args(); 9957 if (args == NULL || args->empty()) 9958 return Type::make_error_type(); 9959 return args->front()->type(); 9960 } 9961 9962 case BUILTIN_CAP: 9963 case BUILTIN_COPY: 9964 case BUILTIN_LEN: 9965 return Type::lookup_integer_type("int"); 9966 9967 case BUILTIN_ALIGNOF: 9968 case BUILTIN_OFFSETOF: 9969 case BUILTIN_SIZEOF: 9970 return Type::lookup_integer_type("uintptr"); 9971 9972 case BUILTIN_CLOSE: 9973 case BUILTIN_DELETE: 9974 case BUILTIN_PANIC: 9975 case BUILTIN_PRINT: 9976 case BUILTIN_PRINTLN: 9977 return Type::make_void_type(); 9978 9979 case BUILTIN_RECOVER: 9980 return Type::make_empty_interface_type(Linemap::predeclared_location()); 9981 9982 case BUILTIN_APPEND: 9983 { 9984 const Expression_list* args = this->args(); 9985 if (args == NULL || args->empty()) 9986 return Type::make_error_type(); 9987 Type *ret = args->front()->type(); 9988 if (!ret->is_slice_type()) 9989 return Type::make_error_type(); 9990 return ret; 9991 } 9992 9993 case BUILTIN_REAL: 9994 case BUILTIN_IMAG: 9995 { 9996 Expression* arg = this->one_arg(); 9997 if (arg == NULL) 9998 return Type::make_error_type(); 9999 Type* t = arg->type(); 10000 if (t->is_abstract()) 10001 t = t->make_non_abstract_type(); 10002 t = Builtin_call_expression::real_imag_type(t); 10003 if (t == NULL) 10004 t = Type::make_error_type(); 10005 return t; 10006 } 10007 10008 case BUILTIN_COMPLEX: 10009 { 10010 const Expression_list* args = this->args(); 10011 if (args == NULL || args->size() != 2) 10012 return Type::make_error_type(); 10013 Type* t = args->front()->type(); 10014 if (t->is_abstract()) 10015 { 10016 t = args->back()->type(); 10017 if (t->is_abstract()) 10018 t = t->make_non_abstract_type(); 10019 } 10020 t = Builtin_call_expression::complex_type(t); 10021 if (t == NULL) 10022 t = Type::make_error_type(); 10023 return t; 10024 } 10025 10026 case BUILTIN_ADD: 10027 return Type::make_pointer_type(Type::make_void_type()); 10028 10029 case BUILTIN_SLICE: 10030 const Expression_list* args = this->args(); 10031 if (args == NULL || args->size() != 2) 10032 return Type::make_error_type(); 10033 Type* pt = args->front()->type()->points_to(); 10034 if (pt == NULL) 10035 return Type::make_error_type(); 10036 return Type::make_array_type(pt, NULL); 10037 } 10038 } 10039 10040 // Determine the type. 10041 10042 void 10043 Builtin_call_expression::do_determine_type(const Type_context* context) 10044 { 10045 if (!this->determining_types()) 10046 return; 10047 10048 this->fn()->determine_type_no_context(); 10049 10050 const Expression_list* args = this->args(); 10051 10052 bool is_print; 10053 Type* arg_type = NULL; 10054 Type* trailing_arg_types = NULL; 10055 switch (this->code_) 10056 { 10057 case BUILTIN_PRINT: 10058 case BUILTIN_PRINTLN: 10059 // Do not force a large integer constant to "int". 10060 is_print = true; 10061 break; 10062 10063 case BUILTIN_REAL: 10064 case BUILTIN_IMAG: 10065 arg_type = Builtin_call_expression::complex_type(context->type); 10066 if (arg_type == NULL) 10067 arg_type = Type::lookup_complex_type("complex128"); 10068 is_print = false; 10069 break; 10070 10071 case BUILTIN_COMPLEX: 10072 { 10073 // For the complex function the type of one operand can 10074 // determine the type of the other, as in a binary expression. 10075 arg_type = Builtin_call_expression::real_imag_type(context->type); 10076 if (arg_type == NULL) 10077 arg_type = Type::lookup_float_type("float64"); 10078 if (args != NULL && args->size() == 2) 10079 { 10080 Type* t1 = args->front()->type(); 10081 Type* t2 = args->back()->type(); 10082 if (!t1->is_abstract()) 10083 arg_type = t1; 10084 else if (!t2->is_abstract()) 10085 arg_type = t2; 10086 } 10087 is_print = false; 10088 } 10089 break; 10090 10091 case BUILTIN_APPEND: 10092 if (!this->is_varargs() 10093 && args != NULL 10094 && !args->empty() 10095 && args->front()->type()->is_slice_type()) 10096 trailing_arg_types = 10097 args->front()->type()->array_type()->element_type(); 10098 is_print = false; 10099 break; 10100 10101 case BUILTIN_ADD: 10102 case BUILTIN_SLICE: 10103 // Both unsafe.Add and unsafe.Slice take two arguments, and the 10104 // second arguments defaults to "int". 10105 if (args != NULL && args->size() == 2) 10106 { 10107 if (this->code_ == BUILTIN_SLICE) 10108 args->front()->determine_type_no_context(); 10109 else 10110 { 10111 Type* pointer = Type::make_pointer_type(Type::make_void_type()); 10112 Type_context subcontext(pointer, false); 10113 args->front()->determine_type(&subcontext); 10114 } 10115 Type* int_type = Type::lookup_integer_type("int"); 10116 Type_context subcontext(int_type, false); 10117 args->back()->determine_type(&subcontext); 10118 return; 10119 } 10120 is_print = false; 10121 break; 10122 10123 default: 10124 is_print = false; 10125 break; 10126 } 10127 10128 if (args != NULL) 10129 { 10130 for (Expression_list::const_iterator pa = args->begin(); 10131 pa != args->end(); 10132 ++pa) 10133 { 10134 Type_context subcontext; 10135 subcontext.type = arg_type; 10136 10137 if (is_print) 10138 { 10139 // We want to print large constants, we so can't just 10140 // use the appropriate nonabstract type. Use uint64 for 10141 // an integer if we know it is nonnegative, otherwise 10142 // use int64 for a integer, otherwise use float64 for a 10143 // float or complex128 for a complex. 10144 Type* want_type = NULL; 10145 Type* atype = (*pa)->type(); 10146 if (atype->is_abstract()) 10147 { 10148 if (atype->integer_type() != NULL) 10149 { 10150 Numeric_constant nc; 10151 if (this->numeric_constant_value(&nc)) 10152 { 10153 mpz_t val; 10154 if (nc.to_int(&val)) 10155 { 10156 if (mpz_sgn(val) >= 0) 10157 want_type = Type::lookup_integer_type("uint64"); 10158 mpz_clear(val); 10159 } 10160 } 10161 if (want_type == NULL) 10162 want_type = Type::lookup_integer_type("int64"); 10163 } 10164 else if (atype->float_type() != NULL) 10165 want_type = Type::lookup_float_type("float64"); 10166 else if (atype->complex_type() != NULL) 10167 want_type = Type::lookup_complex_type("complex128"); 10168 else if (atype->is_abstract_string_type()) 10169 want_type = Type::lookup_string_type(); 10170 else if (atype->is_abstract_boolean_type()) 10171 want_type = Type::lookup_bool_type(); 10172 else 10173 go_unreachable(); 10174 subcontext.type = want_type; 10175 } 10176 } 10177 10178 (*pa)->determine_type(&subcontext); 10179 10180 if (trailing_arg_types != NULL) 10181 { 10182 arg_type = trailing_arg_types; 10183 trailing_arg_types = NULL; 10184 } 10185 } 10186 } 10187 } 10188 10189 // If there is exactly one argument, return true. Otherwise give an 10190 // error message and return false. 10191 10192 bool 10193 Builtin_call_expression::check_one_arg() 10194 { 10195 const Expression_list* args = this->args(); 10196 if (args == NULL || args->size() < 1) 10197 { 10198 this->report_error(_("not enough arguments")); 10199 return false; 10200 } 10201 else if (args->size() > 1) 10202 { 10203 this->report_error(_("too many arguments")); 10204 return false; 10205 } 10206 if (args->front()->is_error_expression() 10207 || args->front()->type()->is_error()) 10208 { 10209 this->set_is_error(); 10210 return false; 10211 } 10212 return true; 10213 } 10214 10215 // Check argument types for a builtin function. 10216 10217 void 10218 Builtin_call_expression::do_check_types(Gogo*) 10219 { 10220 if (this->is_error_expression()) 10221 return; 10222 switch (this->code_) 10223 { 10224 case BUILTIN_INVALID: 10225 case BUILTIN_NEW: 10226 case BUILTIN_MAKE: 10227 case BUILTIN_DELETE: 10228 return; 10229 10230 case BUILTIN_LEN: 10231 case BUILTIN_CAP: 10232 { 10233 // The single argument may be either a string or an array or a 10234 // map or a channel, or a pointer to a closed array. 10235 if (this->check_one_arg()) 10236 { 10237 Type* arg_type = this->one_arg()->type(); 10238 if (arg_type->points_to() != NULL 10239 && arg_type->points_to()->array_type() != NULL 10240 && !arg_type->points_to()->is_slice_type()) 10241 arg_type = arg_type->points_to(); 10242 if (this->code_ == BUILTIN_CAP) 10243 { 10244 if (!arg_type->is_error() 10245 && arg_type->array_type() == NULL 10246 && arg_type->channel_type() == NULL) 10247 this->report_error(_("argument must be array or slice " 10248 "or channel")); 10249 } 10250 else 10251 { 10252 if (!arg_type->is_error() 10253 && !arg_type->is_string_type() 10254 && arg_type->array_type() == NULL 10255 && arg_type->map_type() == NULL 10256 && arg_type->channel_type() == NULL) 10257 this->report_error(_("argument must be string or " 10258 "array or slice or map or channel")); 10259 } 10260 } 10261 } 10262 break; 10263 10264 case BUILTIN_PRINT: 10265 case BUILTIN_PRINTLN: 10266 { 10267 const Expression_list* args = this->args(); 10268 if (args != NULL) 10269 { 10270 for (Expression_list::const_iterator p = args->begin(); 10271 p != args->end(); 10272 ++p) 10273 { 10274 Type* type = (*p)->type(); 10275 if (type->is_error() 10276 || type->is_string_type() 10277 || type->integer_type() != NULL 10278 || type->float_type() != NULL 10279 || type->complex_type() != NULL 10280 || type->is_boolean_type() 10281 || type->points_to() != NULL 10282 || type->interface_type() != NULL 10283 || type->channel_type() != NULL 10284 || type->map_type() != NULL 10285 || type->function_type() != NULL 10286 || type->is_slice_type()) 10287 ; 10288 else if ((*p)->is_type_expression()) 10289 { 10290 // If this is a type expression it's going to give 10291 // an error anyhow, so we don't need one here. 10292 } 10293 else 10294 this->report_error(_("unsupported argument type to " 10295 "builtin function")); 10296 } 10297 } 10298 } 10299 break; 10300 10301 case BUILTIN_CLOSE: 10302 if (this->check_one_arg()) 10303 { 10304 if (this->one_arg()->type()->channel_type() == NULL) 10305 this->report_error(_("argument must be channel")); 10306 else if (!this->one_arg()->type()->channel_type()->may_send()) 10307 this->report_error(_("cannot close receive-only channel")); 10308 } 10309 break; 10310 10311 case BUILTIN_PANIC: 10312 case BUILTIN_SIZEOF: 10313 case BUILTIN_ALIGNOF: 10314 this->check_one_arg(); 10315 break; 10316 10317 case BUILTIN_RECOVER: 10318 if (this->args() != NULL 10319 && !this->args()->empty() 10320 && !this->recover_arg_is_set_) 10321 this->report_error(_("too many arguments")); 10322 break; 10323 10324 case BUILTIN_OFFSETOF: 10325 if (this->check_one_arg()) 10326 { 10327 Expression* arg = this->one_arg(); 10328 if (arg->field_reference_expression() == NULL) 10329 this->report_error(_("argument must be a field reference")); 10330 } 10331 break; 10332 10333 case BUILTIN_COPY: 10334 { 10335 const Expression_list* args = this->args(); 10336 if (args == NULL || args->size() < 2) 10337 { 10338 this->report_error(_("not enough arguments")); 10339 break; 10340 } 10341 else if (args->size() > 2) 10342 { 10343 this->report_error(_("too many arguments")); 10344 break; 10345 } 10346 Type* arg1_type = args->front()->type(); 10347 Type* arg2_type = args->back()->type(); 10348 if (arg1_type->is_error() || arg2_type->is_error()) 10349 { 10350 this->set_is_error(); 10351 break; 10352 } 10353 10354 Type* e1; 10355 if (arg1_type->is_slice_type()) 10356 e1 = arg1_type->array_type()->element_type(); 10357 else 10358 { 10359 this->report_error(_("left argument must be a slice")); 10360 break; 10361 } 10362 10363 if (arg2_type->is_slice_type()) 10364 { 10365 Type* e2 = arg2_type->array_type()->element_type(); 10366 if (!Type::are_identical(e1, e2, Type::COMPARE_TAGS, NULL)) 10367 this->report_error(_("element types must be the same")); 10368 } 10369 else if (arg2_type->is_string_type()) 10370 { 10371 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) 10372 this->report_error(_("first argument must be []byte")); 10373 } 10374 else 10375 this->report_error(_("second argument must be slice or string")); 10376 } 10377 break; 10378 10379 case BUILTIN_APPEND: 10380 { 10381 const Expression_list* args = this->args(); 10382 if (args == NULL || args->empty()) 10383 { 10384 this->report_error(_("not enough arguments")); 10385 break; 10386 } 10387 10388 Type* slice_type = args->front()->type(); 10389 if (!slice_type->is_slice_type()) 10390 { 10391 if (slice_type->is_error_type()) 10392 break; 10393 if (slice_type->is_nil_type()) 10394 go_error_at(args->front()->location(), "use of untyped nil"); 10395 else 10396 go_error_at(args->front()->location(), 10397 "argument 1 must be a slice"); 10398 this->set_is_error(); 10399 break; 10400 } 10401 10402 Type* element_type = slice_type->array_type()->element_type(); 10403 if (!element_type->in_heap()) 10404 go_error_at(args->front()->location(), 10405 "cannot append to slice of go:notinheap type"); 10406 if (this->is_varargs()) 10407 { 10408 if (!args->back()->type()->is_slice_type() 10409 && !args->back()->type()->is_string_type()) 10410 { 10411 go_error_at(args->back()->location(), 10412 "invalid use of %<...%> with non-slice/non-string"); 10413 this->set_is_error(); 10414 break; 10415 } 10416 10417 if (args->size() < 2) 10418 { 10419 this->report_error(_("not enough arguments")); 10420 break; 10421 } 10422 if (args->size() > 2) 10423 { 10424 this->report_error(_("too many arguments")); 10425 break; 10426 } 10427 10428 if (args->back()->type()->is_string_type() 10429 && element_type->integer_type() != NULL 10430 && element_type->integer_type()->is_byte()) 10431 { 10432 // Permit append(s1, s2...) when s1 is a slice of 10433 // bytes and s2 is a string type. 10434 } 10435 else 10436 { 10437 // We have to test for assignment compatibility to a 10438 // slice of the element type, which is not necessarily 10439 // the same as the type of the first argument: the 10440 // first argument might have a named type. 10441 Type* check_type = Type::make_array_type(element_type, NULL); 10442 std::string reason; 10443 if (!Type::are_assignable(check_type, args->back()->type(), 10444 &reason)) 10445 { 10446 if (reason.empty()) 10447 go_error_at(args->back()->location(), 10448 "argument 2 has invalid type"); 10449 else 10450 go_error_at(args->back()->location(), 10451 "argument 2 has invalid type (%s)", 10452 reason.c_str()); 10453 this->set_is_error(); 10454 break; 10455 } 10456 } 10457 } 10458 else 10459 { 10460 Expression_list::const_iterator pa = args->begin(); 10461 int i = 2; 10462 for (++pa; pa != args->end(); ++pa, ++i) 10463 { 10464 std::string reason; 10465 if (!Type::are_assignable(element_type, (*pa)->type(), 10466 &reason)) 10467 { 10468 if (reason.empty()) 10469 go_error_at((*pa)->location(), 10470 "argument %d has incompatible type", i); 10471 else 10472 go_error_at((*pa)->location(), 10473 "argument %d has incompatible type (%s)", 10474 i, reason.c_str()); 10475 this->set_is_error(); 10476 } 10477 } 10478 } 10479 } 10480 break; 10481 10482 case BUILTIN_REAL: 10483 case BUILTIN_IMAG: 10484 if (this->check_one_arg()) 10485 { 10486 if (this->one_arg()->type()->complex_type() == NULL) 10487 this->report_error(_("argument must have complex type")); 10488 } 10489 break; 10490 10491 case BUILTIN_COMPLEX: 10492 { 10493 const Expression_list* args = this->args(); 10494 if (args == NULL || args->size() < 2) 10495 this->report_error(_("not enough arguments")); 10496 else if (args->size() > 2) 10497 this->report_error(_("too many arguments")); 10498 else if (args->front()->is_error_expression() 10499 || args->front()->type()->is_error() 10500 || args->back()->is_error_expression() 10501 || args->back()->type()->is_error()) 10502 this->set_is_error(); 10503 else if (!Type::are_identical(args->front()->type(), 10504 args->back()->type(), 10505 Type::COMPARE_TAGS, NULL)) 10506 this->report_error(_("complex arguments must have identical types")); 10507 else if (args->front()->type()->float_type() == NULL) 10508 this->report_error(_("complex arguments must have " 10509 "floating-point type")); 10510 } 10511 break; 10512 10513 case BUILTIN_ADD: 10514 case BUILTIN_SLICE: 10515 { 10516 Numeric_constant nc; 10517 unsigned long v; 10518 const Expression_list* args = this->args(); 10519 if (args == NULL || args->size() < 2) 10520 this->report_error(_("not enough arguments")); 10521 else if (args->size() > 2) 10522 this->report_error(_("too many arguments")); 10523 else if (args->front()->is_error_expression() 10524 || args->front()->type()->is_error() 10525 || args->back()->is_error_expression() 10526 || args->back()->type()->is_error()) 10527 this->set_is_error(); 10528 else if (args->back()->type()->integer_type() == NULL 10529 && (!args->back()->type()->is_abstract() 10530 || !args->back()->numeric_constant_value(&nc) 10531 || (nc.to_unsigned_long(&v) 10532 == Numeric_constant::NC_UL_NOTINT))) 10533 { 10534 if (this->code_ == BUILTIN_ADD) 10535 go_error_at(args->back()->location(), "non-integer offset"); 10536 else 10537 go_error_at(args->back()->location(), "non-integer size"); 10538 } 10539 else if (this->code_ == BUILTIN_ADD) 10540 { 10541 Type* pointer_type = 10542 Type::make_pointer_type(Type::make_void_type()); 10543 std::string reason; 10544 if (!Type::are_assignable(pointer_type, args->front()->type(), 10545 &reason)) 10546 { 10547 if (reason.empty()) 10548 go_error_at(args->front()->location(), 10549 "argument 1 has incompatible type"); 10550 else 10551 go_error_at(args->front()->location(), 10552 "argument 1 has incompatible type (%s)", 10553 reason.c_str()); 10554 this->set_is_error(); 10555 } 10556 } 10557 else 10558 { 10559 if (args->front()->type()->points_to() == NULL) 10560 { 10561 go_error_at(args->front()->location(), 10562 "argument 1 must be a pointer"); 10563 this->set_is_error(); 10564 } 10565 10566 unsigned int int_bits = 10567 Type::lookup_integer_type("int")->integer_type()->bits(); 10568 10569 mpz_t ival; 10570 if (args->back()->numeric_constant_value(&nc) && nc.to_int(&ival)) 10571 { 10572 if (mpz_sgn(ival) < 0 10573 || mpz_sizeinbase(ival, 2) >= int_bits) 10574 { 10575 go_error_at(args->back()->location(), 10576 "slice length out of range"); 10577 this->set_is_error(); 10578 } 10579 mpz_clear(ival); 10580 } 10581 } 10582 } 10583 break; 10584 10585 default: 10586 go_unreachable(); 10587 } 10588 } 10589 10590 Expression* 10591 Builtin_call_expression::do_copy() 10592 { 10593 Call_expression* bce = 10594 new Builtin_call_expression(this->gogo_, this->fn()->copy(), 10595 (this->args() == NULL 10596 ? NULL 10597 : this->args()->copy()), 10598 this->is_varargs(), 10599 this->location()); 10600 10601 if (this->varargs_are_lowered()) 10602 bce->set_varargs_are_lowered(); 10603 if (this->is_deferred()) 10604 bce->set_is_deferred(); 10605 if (this->is_concurrent()) 10606 bce->set_is_concurrent(); 10607 return bce; 10608 } 10609 10610 // Return the backend representation for a builtin function. 10611 10612 Bexpression* 10613 Builtin_call_expression::do_get_backend(Translate_context* context) 10614 { 10615 Gogo* gogo = context->gogo(); 10616 Location location = this->location(); 10617 10618 if (this->is_erroneous_call()) 10619 { 10620 go_assert(saw_errors()); 10621 return gogo->backend()->error_expression(); 10622 } 10623 10624 switch (this->code_) 10625 { 10626 case BUILTIN_INVALID: 10627 case BUILTIN_NEW: 10628 case BUILTIN_MAKE: 10629 case BUILTIN_ADD: 10630 case BUILTIN_SLICE: 10631 go_unreachable(); 10632 10633 case BUILTIN_LEN: 10634 case BUILTIN_CAP: 10635 { 10636 const Expression_list* args = this->args(); 10637 go_assert(args != NULL && args->size() == 1); 10638 Expression* arg = args->front(); 10639 Type* arg_type = arg->type(); 10640 10641 if (this->seen_) 10642 { 10643 go_assert(saw_errors()); 10644 return context->backend()->error_expression(); 10645 } 10646 this->seen_ = true; 10647 this->seen_ = false; 10648 if (arg_type->points_to() != NULL) 10649 { 10650 arg_type = arg_type->points_to(); 10651 go_assert(arg_type->array_type() != NULL 10652 && !arg_type->is_slice_type()); 10653 arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT, 10654 location); 10655 } 10656 10657 Type* int_type = Type::lookup_integer_type("int"); 10658 Expression* val; 10659 if (this->code_ == BUILTIN_LEN) 10660 { 10661 if (arg_type->is_string_type()) 10662 val = Expression::make_string_info(arg, STRING_INFO_LENGTH, 10663 location); 10664 else if (arg_type->array_type() != NULL) 10665 { 10666 if (this->seen_) 10667 { 10668 go_assert(saw_errors()); 10669 return context->backend()->error_expression(); 10670 } 10671 this->seen_ = true; 10672 val = arg_type->array_type()->get_length(gogo, arg); 10673 this->seen_ = false; 10674 } 10675 else if (arg_type->map_type() != NULL 10676 || arg_type->channel_type() != NULL) 10677 { 10678 // The first field is the length. If the pointer is 10679 // nil, the length is zero. 10680 Type* pint_type = Type::make_pointer_type(int_type); 10681 arg = Expression::make_unsafe_cast(pint_type, arg, location); 10682 Expression* nil = Expression::make_nil(location); 10683 nil = Expression::make_cast(pint_type, nil, location); 10684 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, 10685 arg, nil, location); 10686 Expression* zero = Expression::make_integer_ul(0, int_type, 10687 location); 10688 Expression* indir = 10689 Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, 10690 location); 10691 val = Expression::make_conditional(cmp, zero, indir, location); 10692 } 10693 else 10694 go_unreachable(); 10695 } 10696 else 10697 { 10698 if (arg_type->array_type() != NULL) 10699 { 10700 if (this->seen_) 10701 { 10702 go_assert(saw_errors()); 10703 return context->backend()->error_expression(); 10704 } 10705 this->seen_ = true; 10706 val = arg_type->array_type()->get_capacity(gogo, arg); 10707 this->seen_ = false; 10708 } 10709 else if (arg_type->channel_type() != NULL) 10710 { 10711 // The second field is the capacity. If the pointer 10712 // is nil, the capacity is zero. 10713 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 10714 Type* pint_type = Type::make_pointer_type(int_type); 10715 Expression* parg = Expression::make_unsafe_cast(uintptr_type, 10716 arg, 10717 location); 10718 int off = int_type->integer_type()->bits() / 8; 10719 Expression* eoff = Expression::make_integer_ul(off, 10720 uintptr_type, 10721 location); 10722 parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, 10723 location); 10724 parg = Expression::make_unsafe_cast(pint_type, parg, location); 10725 Expression* nil = Expression::make_nil(location); 10726 nil = Expression::make_cast(pint_type, nil, location); 10727 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, 10728 arg, nil, location); 10729 Expression* zero = Expression::make_integer_ul(0, int_type, 10730 location); 10731 Expression* indir = 10732 Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED, 10733 location); 10734 val = Expression::make_conditional(cmp, zero, indir, location); 10735 } 10736 else 10737 go_unreachable(); 10738 } 10739 10740 return Expression::make_cast(int_type, val, 10741 location)->get_backend(context); 10742 } 10743 10744 case BUILTIN_PRINT: 10745 case BUILTIN_PRINTLN: 10746 { 10747 const bool is_ln = this->code_ == BUILTIN_PRINTLN; 10748 10749 Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, 10750 location, 0); 10751 10752 const Expression_list* call_args = this->args(); 10753 if (call_args != NULL) 10754 { 10755 for (Expression_list::const_iterator p = call_args->begin(); 10756 p != call_args->end(); 10757 ++p) 10758 { 10759 if (is_ln && p != call_args->begin()) 10760 { 10761 Expression* print_space = 10762 Runtime::make_call(Runtime::PRINTSP, location, 0); 10763 10764 print_stmts = 10765 Expression::make_compound(print_stmts, print_space, 10766 location); 10767 } 10768 10769 Expression* arg = *p; 10770 Type* type = arg->type(); 10771 Runtime::Function code; 10772 if (type->is_string_type()) 10773 code = Runtime::PRINTSTRING; 10774 else if (type->integer_type() != NULL 10775 && type->integer_type()->is_unsigned()) 10776 { 10777 Type* itype = Type::lookup_integer_type("uint64"); 10778 arg = Expression::make_cast(itype, arg, location); 10779 if (gogo->compiling_runtime() 10780 && type->named_type() != NULL 10781 && gogo->unpack_hidden_name(type->named_type()->name()) 10782 == "hex") 10783 code = Runtime::PRINTHEX; 10784 else 10785 code = Runtime::PRINTUINT; 10786 } 10787 else if (type->integer_type() != NULL) 10788 { 10789 Type* itype = Type::lookup_integer_type("int64"); 10790 arg = Expression::make_cast(itype, arg, location); 10791 code = Runtime::PRINTINT; 10792 } 10793 else if (type->float_type() != NULL) 10794 { 10795 Type* dtype = Type::lookup_float_type("float64"); 10796 arg = Expression::make_cast(dtype, arg, location); 10797 code = Runtime::PRINTFLOAT; 10798 } 10799 else if (type->complex_type() != NULL) 10800 { 10801 Type* ctype = Type::lookup_complex_type("complex128"); 10802 arg = Expression::make_cast(ctype, arg, location); 10803 code = Runtime::PRINTCOMPLEX; 10804 } 10805 else if (type->is_boolean_type()) 10806 code = Runtime::PRINTBOOL; 10807 else if (type->points_to() != NULL 10808 || type->channel_type() != NULL 10809 || type->map_type() != NULL 10810 || type->function_type() != NULL) 10811 { 10812 arg = Expression::make_cast(type, arg, location); 10813 code = Runtime::PRINTPOINTER; 10814 } 10815 else if (type->interface_type() != NULL) 10816 { 10817 if (type->interface_type()->is_empty()) 10818 code = Runtime::PRINTEFACE; 10819 else 10820 code = Runtime::PRINTIFACE; 10821 } 10822 else if (type->is_slice_type()) 10823 code = Runtime::PRINTSLICE; 10824 else 10825 { 10826 go_assert(saw_errors()); 10827 return context->backend()->error_expression(); 10828 } 10829 10830 Expression* call = Runtime::make_call(code, location, 1, arg); 10831 print_stmts = Expression::make_compound(print_stmts, call, 10832 location); 10833 } 10834 } 10835 10836 if (is_ln) 10837 { 10838 Expression* print_nl = 10839 Runtime::make_call(Runtime::PRINTNL, location, 0); 10840 print_stmts = Expression::make_compound(print_stmts, print_nl, 10841 location); 10842 } 10843 10844 Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, 10845 location, 0); 10846 print_stmts = Expression::make_compound(print_stmts, unlock, location); 10847 10848 return print_stmts->get_backend(context); 10849 } 10850 10851 case BUILTIN_PANIC: 10852 { 10853 const Expression_list* args = this->args(); 10854 go_assert(args != NULL && args->size() == 1); 10855 Expression* arg = args->front(); 10856 Type *empty = 10857 Type::make_empty_interface_type(Linemap::predeclared_location()); 10858 arg = Expression::convert_for_assignment(gogo, empty, arg, location); 10859 10860 Expression* panic = 10861 Runtime::make_call(Runtime::GOPANIC, location, 1, arg); 10862 return panic->get_backend(context); 10863 } 10864 10865 case BUILTIN_RECOVER: 10866 { 10867 // The argument is set when building recover thunks. It's a 10868 // boolean value which is true if we can recover a value now. 10869 const Expression_list* args = this->args(); 10870 go_assert(args != NULL && args->size() == 1); 10871 Expression* arg = args->front(); 10872 Type *empty = 10873 Type::make_empty_interface_type(Linemap::predeclared_location()); 10874 10875 Expression* nil = Expression::make_nil(location); 10876 nil = Expression::make_interface_value(empty, nil, nil, location); 10877 10878 // We need to handle a deferred call to recover specially, 10879 // because it changes whether it can recover a panic or not. 10880 // See test7 in test/recover1.go. 10881 Expression* recover = Runtime::make_call((this->is_deferred() 10882 ? Runtime::DEFERREDRECOVER 10883 : Runtime::GORECOVER), 10884 location, 0); 10885 Expression* cond = 10886 Expression::make_conditional(arg, recover, nil, location); 10887 return cond->get_backend(context); 10888 } 10889 10890 case BUILTIN_CLOSE: 10891 { 10892 const Expression_list* args = this->args(); 10893 go_assert(args != NULL && args->size() == 1); 10894 Expression* arg = args->front(); 10895 Expression* close = Runtime::make_call(Runtime::CLOSE, location, 10896 1, arg); 10897 return close->get_backend(context); 10898 } 10899 10900 case BUILTIN_SIZEOF: 10901 case BUILTIN_OFFSETOF: 10902 case BUILTIN_ALIGNOF: 10903 { 10904 Numeric_constant nc; 10905 unsigned long val; 10906 if (!this->numeric_constant_value(&nc) 10907 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) 10908 { 10909 go_assert(saw_errors()); 10910 return context->backend()->error_expression(); 10911 } 10912 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 10913 mpz_t ival; 10914 nc.get_int(&ival); 10915 Expression* int_cst = 10916 Expression::make_integer_z(&ival, uintptr_type, location); 10917 mpz_clear(ival); 10918 return int_cst->get_backend(context); 10919 } 10920 10921 case BUILTIN_COPY: 10922 // Handled in Builtin_call_expression::do_flatten. 10923 go_unreachable(); 10924 10925 case BUILTIN_APPEND: 10926 // Handled in Builtin_call_expression::flatten_append. 10927 go_unreachable(); 10928 10929 case BUILTIN_REAL: 10930 case BUILTIN_IMAG: 10931 { 10932 const Expression_list* args = this->args(); 10933 go_assert(args != NULL && args->size() == 1); 10934 10935 Bexpression* ret; 10936 Bexpression* bcomplex = args->front()->get_backend(context); 10937 if (this->code_ == BUILTIN_REAL) 10938 ret = gogo->backend()->real_part_expression(bcomplex, location); 10939 else 10940 ret = gogo->backend()->imag_part_expression(bcomplex, location); 10941 return ret; 10942 } 10943 10944 case BUILTIN_COMPLEX: 10945 { 10946 const Expression_list* args = this->args(); 10947 go_assert(args != NULL && args->size() == 2); 10948 Bexpression* breal = args->front()->get_backend(context); 10949 Bexpression* bimag = args->back()->get_backend(context); 10950 return gogo->backend()->complex_expression(breal, bimag, location); 10951 } 10952 10953 default: 10954 go_unreachable(); 10955 } 10956 } 10957 10958 // We have to support exporting a builtin call expression, because 10959 // code can set a constant to the result of a builtin expression. 10960 10961 void 10962 Builtin_call_expression::do_export(Export_function_body* efb) const 10963 { 10964 Numeric_constant nc; 10965 if (this->numeric_constant_value(&nc)) 10966 { 10967 if (nc.is_int()) 10968 { 10969 mpz_t val; 10970 nc.get_int(&val); 10971 Integer_expression::export_integer(efb, val); 10972 mpz_clear(val); 10973 } 10974 else if (nc.is_float()) 10975 { 10976 mpfr_t fval; 10977 nc.get_float(&fval); 10978 Float_expression::export_float(efb, fval); 10979 mpfr_clear(fval); 10980 } 10981 else if (nc.is_complex()) 10982 { 10983 mpc_t cval; 10984 nc.get_complex(&cval); 10985 Complex_expression::export_complex(efb, cval); 10986 mpc_clear(cval); 10987 } 10988 else 10989 go_unreachable(); 10990 10991 // A trailing space lets us reliably identify the end of the number. 10992 efb->write_c_string(" "); 10993 } 10994 else if (this->code_ == BUILTIN_ADD || this->code_ == BUILTIN_SLICE) 10995 { 10996 char buf[50]; 10997 snprintf(buf, sizeof buf, "<p%d>%s", efb->unsafe_package_index(), 10998 (this->code_ == BUILTIN_ADD ? "Add" : "Slice")); 10999 efb->write_c_string(buf); 11000 this->export_arguments(efb); 11001 } 11002 else 11003 { 11004 const char *s = NULL; 11005 switch (this->code_) 11006 { 11007 default: 11008 go_unreachable(); 11009 case BUILTIN_APPEND: 11010 s = "append"; 11011 break; 11012 case BUILTIN_COPY: 11013 s = "copy"; 11014 break; 11015 case BUILTIN_LEN: 11016 s = "len"; 11017 break; 11018 case BUILTIN_CAP: 11019 s = "cap"; 11020 break; 11021 case BUILTIN_DELETE: 11022 s = "delete"; 11023 break; 11024 case BUILTIN_PRINT: 11025 s = "print"; 11026 break; 11027 case BUILTIN_PRINTLN: 11028 s = "println"; 11029 break; 11030 case BUILTIN_PANIC: 11031 s = "panic"; 11032 break; 11033 case BUILTIN_RECOVER: 11034 s = "recover"; 11035 break; 11036 case BUILTIN_CLOSE: 11037 s = "close"; 11038 break; 11039 case BUILTIN_REAL: 11040 s = "real"; 11041 break; 11042 case BUILTIN_IMAG: 11043 s = "imag"; 11044 break; 11045 case BUILTIN_COMPLEX: 11046 s = "complex"; 11047 break; 11048 } 11049 efb->write_c_string(s); 11050 this->export_arguments(efb); 11051 } 11052 } 11053 11054 // Class Call_expression. 11055 11056 // A Go function can be viewed in a couple of different ways. The 11057 // code of a Go function becomes a backend function with parameters 11058 // whose types are simply the backend representation of the Go types. 11059 // If there are multiple results, they are returned as a backend 11060 // struct. 11061 11062 // However, when Go code refers to a function other than simply 11063 // calling it, the backend type of that function is actually a struct. 11064 // The first field of the struct points to the Go function code 11065 // (sometimes a wrapper as described below). The remaining fields 11066 // hold addresses of closed-over variables. This struct is called a 11067 // closure. 11068 11069 // There are a few cases to consider. 11070 11071 // A direct function call of a known function in package scope. In 11072 // this case there are no closed-over variables, and we know the name 11073 // of the function code. We can simply produce a backend call to the 11074 // function directly, and not worry about the closure. 11075 11076 // A direct function call of a known function literal. In this case 11077 // we know the function code and we know the closure. We generate the 11078 // function code such that it expects an additional final argument of 11079 // the closure type. We pass the closure as the last argument, after 11080 // the other arguments. 11081 11082 // An indirect function call. In this case we have a closure. We 11083 // load the pointer to the function code from the first field of the 11084 // closure. We pass the address of the closure as the last argument. 11085 11086 // A call to a method of an interface. Type methods are always at 11087 // package scope, so we call the function directly, and don't worry 11088 // about the closure. 11089 11090 // This means that for a function at package scope we have two cases. 11091 // One is the direct call, which has no closure. The other is the 11092 // indirect call, which does have a closure. We can't simply ignore 11093 // the closure, even though it is the last argument, because that will 11094 // fail on targets where the function pops its arguments. So when 11095 // generating a closure for a package-scope function we set the 11096 // function code pointer in the closure to point to a wrapper 11097 // function. This wrapper function accepts a final argument that 11098 // points to the closure, ignores it, and calls the real function as a 11099 // direct function call. This wrapper will normally be efficient, and 11100 // can often simply be a tail call to the real function. 11101 11102 // We don't use GCC's static chain pointer because 1) we don't need 11103 // it; 2) GCC only permits using a static chain to call a known 11104 // function, so we can't use it for an indirect call anyhow. Since we 11105 // can't use it for an indirect call, we may as well not worry about 11106 // using it for a direct call either. 11107 11108 // We pass the closure last rather than first because it means that 11109 // the function wrapper we put into a closure for a package-scope 11110 // function can normally just be a tail call to the real function. 11111 11112 // For method expressions we generate a wrapper that loads the 11113 // receiver from the closure and then calls the method. This 11114 // unfortunately forces reshuffling the arguments, since there is a 11115 // new first argument, but we can't avoid reshuffling either for 11116 // method expressions or for indirect calls of package-scope 11117 // functions, and since the latter are more common we reshuffle for 11118 // method expressions. 11119 11120 // Note that the Go code retains the Go types. The extra final 11121 // argument only appears when we convert to the backend 11122 // representation. 11123 11124 // Traversal. 11125 11126 int 11127 Call_expression::do_traverse(Traverse* traverse) 11128 { 11129 // If we are calling a function in a different package that returns 11130 // an unnamed type, this may be the only chance we get to traverse 11131 // that type. We don't traverse this->type_ because it may be a 11132 // Call_multiple_result_type that will just lead back here. 11133 if (this->type_ != NULL && !this->type_->is_error_type()) 11134 { 11135 Function_type *fntype = this->get_function_type(); 11136 if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) 11137 return TRAVERSE_EXIT; 11138 } 11139 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) 11140 return TRAVERSE_EXIT; 11141 if (this->args_ != NULL) 11142 { 11143 if (this->args_->traverse(traverse) == TRAVERSE_EXIT) 11144 return TRAVERSE_EXIT; 11145 } 11146 return TRAVERSE_CONTINUE; 11147 } 11148 11149 // Lower a call statement. 11150 11151 Expression* 11152 Call_expression::do_lower(Gogo* gogo, Named_object* function, 11153 Statement_inserter* inserter, int) 11154 { 11155 Location loc = this->location(); 11156 11157 if (this->is_error_expression()) 11158 return Expression::make_error(loc); 11159 11160 // A type cast can look like a function call. 11161 if (this->fn_->is_type_expression() 11162 && this->args_ != NULL 11163 && this->args_->size() == 1) 11164 { 11165 if (this->expected_result_count_ != 0 11166 && this->expected_result_count_ != 1) 11167 { 11168 this->report_error(_("type conversion result count mismatch")); 11169 return Expression::make_error(loc); 11170 } 11171 return Expression::make_cast(this->fn_->type(), this->args_->front(), 11172 loc); 11173 } 11174 11175 // Because do_type will return an error type and thus prevent future 11176 // errors, check for that case now to ensure that the error gets 11177 // reported. 11178 Function_type* fntype = this->get_function_type(); 11179 if (fntype == NULL) 11180 { 11181 if (!this->fn_->type()->is_error()) 11182 this->report_error(_("expected function")); 11183 this->set_is_error(); 11184 return this; 11185 } 11186 11187 // Handle an argument which is a call to a function which returns 11188 // multiple results. 11189 if (this->args_ != NULL 11190 && this->args_->size() == 1 11191 && this->args_->front()->call_expression() != NULL) 11192 { 11193 size_t rc = this->args_->front()->call_expression()->result_count(); 11194 if (rc > 1 11195 && ((fntype->parameters() != NULL 11196 && (fntype->parameters()->size() == rc 11197 || (fntype->is_varargs() 11198 && fntype->parameters()->size() - 1 <= rc))) 11199 || fntype->is_builtin())) 11200 { 11201 Call_expression* call = this->args_->front()->call_expression(); 11202 call->set_is_multi_value_arg(); 11203 if (this->is_varargs_) 11204 { 11205 // It is not clear which result of a multiple result call 11206 // the ellipsis operator should be applied to. If we unpack the 11207 // the call into its individual results here, the ellipsis will be 11208 // applied to the last result. 11209 go_error_at(call->location(), 11210 _("multiple-value argument in single-value context")); 11211 return Expression::make_error(call->location()); 11212 } 11213 11214 Expression_list* args = new Expression_list; 11215 for (size_t i = 0; i < rc; ++i) 11216 args->push_back(Expression::make_call_result(call, i)); 11217 // We can't return a new call expression here, because this 11218 // one may be referenced by Call_result expressions. We 11219 // also can't delete the old arguments, because we may still 11220 // traverse them somewhere up the call stack. FIXME. 11221 this->args_ = args; 11222 } 11223 } 11224 11225 // Recognize a call to a builtin function. 11226 if (fntype->is_builtin()) 11227 { 11228 Builtin_call_expression* bce = 11229 new Builtin_call_expression(gogo, this->fn_, this->args_, 11230 this->is_varargs_, loc); 11231 if (this->is_deferred_) 11232 bce->set_is_deferred(); 11233 if (this->is_concurrent_) 11234 bce->set_is_concurrent(); 11235 return bce; 11236 } 11237 11238 // If this call returns multiple results, create a temporary 11239 // variable to hold them. 11240 if (this->result_count() > 1 && this->call_temp_ == NULL) 11241 { 11242 Struct_field_list* sfl = new Struct_field_list(); 11243 const Typed_identifier_list* results = fntype->results(); 11244 11245 int i = 0; 11246 char buf[20]; 11247 for (Typed_identifier_list::const_iterator p = results->begin(); 11248 p != results->end(); 11249 ++p, ++i) 11250 { 11251 snprintf(buf, sizeof buf, "res%d", i); 11252 sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); 11253 } 11254 11255 Struct_type* st = Type::make_struct_type(sfl, loc); 11256 st->set_is_struct_incomparable(); 11257 this->call_temp_ = Statement::make_temporary(st, NULL, loc); 11258 inserter->insert(this->call_temp_); 11259 } 11260 11261 // Handle a call to a varargs function by packaging up the extra 11262 // parameters. 11263 if (fntype->is_varargs()) 11264 { 11265 const Typed_identifier_list* parameters = fntype->parameters(); 11266 go_assert(parameters != NULL && !parameters->empty()); 11267 Type* varargs_type = parameters->back().type(); 11268 this->lower_varargs(gogo, function, inserter, varargs_type, 11269 parameters->size(), SLICE_STORAGE_MAY_ESCAPE); 11270 } 11271 11272 // If this is call to a method, call the method directly passing the 11273 // object as the first parameter. 11274 Bound_method_expression* bme = this->fn_->bound_method_expression(); 11275 if (bme != NULL && !this->is_deferred_ && !this->is_concurrent_) 11276 { 11277 Named_object* methodfn = bme->function(); 11278 Function_type* mft = (methodfn->is_function() 11279 ? methodfn->func_value()->type() 11280 : methodfn->func_declaration_value()->type()); 11281 Expression* first_arg = bme->first_argument(); 11282 11283 // We always pass a pointer when calling a method, except for 11284 // direct interface types when calling a value method. 11285 if (!first_arg->type()->is_error() 11286 && first_arg->type()->points_to() == NULL 11287 && !first_arg->type()->is_direct_iface_type()) 11288 { 11289 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); 11290 // We may need to create a temporary variable so that we can 11291 // take the address. We can't do that here because it will 11292 // mess up the order of evaluation. 11293 Unary_expression* ue = static_cast<Unary_expression*>(first_arg); 11294 ue->set_create_temp(); 11295 } 11296 else if (mft->receiver()->type()->points_to() == NULL 11297 && first_arg->type()->points_to() != NULL 11298 && first_arg->type()->points_to()->is_direct_iface_type()) 11299 first_arg = Expression::make_dereference(first_arg, 11300 Expression::NIL_CHECK_DEFAULT, 11301 loc); 11302 11303 // If we are calling a method which was inherited from an 11304 // embedded struct, and the method did not get a stub, then the 11305 // first type may be wrong. 11306 Type* fatype = bme->first_argument_type(); 11307 if (fatype != NULL) 11308 { 11309 if (fatype->points_to() == NULL) 11310 fatype = Type::make_pointer_type(fatype); 11311 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); 11312 } 11313 11314 Expression_list* new_args = new Expression_list(); 11315 new_args->push_back(first_arg); 11316 if (this->args_ != NULL) 11317 { 11318 for (Expression_list::const_iterator p = this->args_->begin(); 11319 p != this->args_->end(); 11320 ++p) 11321 new_args->push_back(*p); 11322 } 11323 11324 // We have to change in place because this structure may be 11325 // referenced by Call_result_expressions. We can't delete the 11326 // old arguments, because we may be traversing them up in some 11327 // caller. FIXME. 11328 this->args_ = new_args; 11329 this->fn_ = Expression::make_func_reference(methodfn, NULL, 11330 bme->location()); 11331 } 11332 11333 // If this is a call to an imported function for which we have an 11334 // inlinable function body, add it to the list of functions to give 11335 // to the backend as inlining opportunities. 11336 Func_expression* fe = this->fn_->func_expression(); 11337 if (fe != NULL 11338 && fe->named_object()->is_function_declaration() 11339 && fe->named_object()->func_declaration_value()->has_imported_body()) 11340 gogo->add_imported_inlinable_function(fe->named_object()); 11341 11342 return this; 11343 } 11344 11345 // Lower a call to a varargs function. FUNCTION is the function in 11346 // which the call occurs--it's not the function we are calling. 11347 // VARARGS_TYPE is the type of the varargs parameter, a slice type. 11348 // PARAM_COUNT is the number of parameters of the function we are 11349 // calling; the last of these parameters will be the varargs 11350 // parameter. 11351 11352 void 11353 Call_expression::lower_varargs(Gogo* gogo, Named_object* function, 11354 Statement_inserter* inserter, 11355 Type* varargs_type, size_t param_count, 11356 Slice_storage_escape_disp escape_disp) 11357 { 11358 if (this->varargs_are_lowered_) 11359 return; 11360 11361 Location loc = this->location(); 11362 11363 go_assert(param_count > 0); 11364 go_assert(varargs_type->is_slice_type()); 11365 11366 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); 11367 if (arg_count < param_count - 1) 11368 { 11369 // Not enough arguments; will be caught in check_types. 11370 return; 11371 } 11372 11373 Expression_list* old_args = this->args_; 11374 Expression_list* new_args = new Expression_list(); 11375 bool push_empty_arg = false; 11376 if (old_args == NULL || old_args->empty()) 11377 { 11378 go_assert(param_count == 1); 11379 push_empty_arg = true; 11380 } 11381 else 11382 { 11383 Expression_list::const_iterator pa; 11384 int i = 1; 11385 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) 11386 { 11387 if (static_cast<size_t>(i) == param_count) 11388 break; 11389 new_args->push_back(*pa); 11390 } 11391 11392 // We have reached the varargs parameter. 11393 11394 bool issued_error = false; 11395 if (pa == old_args->end()) 11396 push_empty_arg = true; 11397 else if (pa + 1 == old_args->end() && this->is_varargs_) 11398 new_args->push_back(*pa); 11399 else if (this->is_varargs_) 11400 { 11401 if ((*pa)->type()->is_slice_type()) 11402 this->report_error(_("too many arguments")); 11403 else 11404 { 11405 go_error_at(this->location(), 11406 _("invalid use of %<...%> with non-slice")); 11407 this->set_is_error(); 11408 } 11409 return; 11410 } 11411 else 11412 { 11413 Type* element_type = varargs_type->array_type()->element_type(); 11414 Expression_list* vals = new Expression_list; 11415 for (; pa != old_args->end(); ++pa, ++i) 11416 { 11417 // Check types here so that we get a better message. 11418 Type* patype = (*pa)->type(); 11419 Location paloc = (*pa)->location(); 11420 if (!this->check_argument_type(i, element_type, patype, 11421 paloc, issued_error)) 11422 continue; 11423 vals->push_back(*pa); 11424 } 11425 Slice_construction_expression* sce = 11426 Expression::make_slice_composite_literal(varargs_type, vals, loc); 11427 if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) 11428 sce->set_storage_does_not_escape(); 11429 Expression* val = sce; 11430 gogo->lower_expression(function, inserter, &val); 11431 new_args->push_back(val); 11432 } 11433 } 11434 11435 if (push_empty_arg) 11436 new_args->push_back(Expression::make_nil(loc)); 11437 11438 // We can't return a new call expression here, because this one may 11439 // be referenced by Call_result expressions. FIXME. We can't 11440 // delete OLD_ARGS because we may have both a Call_expression and a 11441 // Builtin_call_expression which refer to them. FIXME. 11442 this->args_ = new_args; 11443 this->varargs_are_lowered_ = true; 11444 } 11445 11446 // Flatten a call with multiple results into a temporary. 11447 11448 Expression* 11449 Call_expression::do_flatten(Gogo* gogo, Named_object*, 11450 Statement_inserter* inserter) 11451 { 11452 if (this->is_erroneous_call()) 11453 { 11454 go_assert(saw_errors()); 11455 return Expression::make_error(this->location()); 11456 } 11457 11458 if (this->is_flattened_) 11459 return this; 11460 this->is_flattened_ = true; 11461 11462 // Add temporary variables for all arguments that require type 11463 // conversion. 11464 Function_type* fntype = this->get_function_type(); 11465 if (fntype == NULL) 11466 { 11467 go_assert(saw_errors()); 11468 return this; 11469 } 11470 if (this->args_ != NULL && !this->args_->empty() 11471 && fntype->parameters() != NULL && !fntype->parameters()->empty()) 11472 { 11473 bool is_interface_method = 11474 this->fn_->interface_field_reference_expression() != NULL; 11475 11476 Expression_list *args = new Expression_list(); 11477 Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); 11478 Expression_list::const_iterator pa = this->args_->begin(); 11479 if (!is_interface_method && fntype->is_method()) 11480 { 11481 // The receiver argument. 11482 args->push_back(*pa); 11483 ++pa; 11484 } 11485 for (; pa != this->args_->end(); ++pa, ++pp) 11486 { 11487 go_assert(pp != fntype->parameters()->end()); 11488 if (Type::are_identical(pp->type(), (*pa)->type(), 11489 Type::COMPARE_TAGS, NULL)) 11490 args->push_back(*pa); 11491 else 11492 { 11493 Location loc = (*pa)->location(); 11494 Expression* arg = *pa; 11495 if (!arg->is_multi_eval_safe()) 11496 { 11497 Temporary_statement *temp = 11498 Statement::make_temporary(NULL, arg, loc); 11499 inserter->insert(temp); 11500 arg = Expression::make_temporary_reference(temp, loc); 11501 } 11502 arg = Expression::convert_for_assignment(gogo, pp->type(), arg, 11503 loc); 11504 args->push_back(arg); 11505 } 11506 } 11507 delete this->args_; 11508 this->args_ = args; 11509 } 11510 11511 // Lower to compiler intrinsic if possible. 11512 Func_expression* fe = this->fn_->func_expression(); 11513 if (!this->is_concurrent_ && !this->is_deferred_ 11514 && fe != NULL 11515 && (fe->named_object()->is_function_declaration() 11516 || fe->named_object()->is_function())) 11517 { 11518 Expression* ret = this->intrinsify(gogo, inserter); 11519 if (ret != NULL) 11520 return ret; 11521 } 11522 11523 // Add an implicit conversion to a boolean type, if needed. See the 11524 // comment in Binary_expression::lower_array_comparison. 11525 if (this->is_equal_function_ 11526 && this->type_ != NULL 11527 && this->type_ != Type::lookup_bool_type()) 11528 return Expression::make_cast(this->type_, this, this->location()); 11529 11530 return this; 11531 } 11532 11533 // Lower a call to a compiler intrinsic if possible. 11534 // Returns NULL if it is not an intrinsic. 11535 11536 Expression* 11537 Call_expression::intrinsify(Gogo* gogo, 11538 Statement_inserter* inserter) 11539 { 11540 Func_expression* fe = this->fn_->func_expression(); 11541 Named_object* no = fe->named_object(); 11542 std::string name = Gogo::unpack_hidden_name(no->name()); 11543 std::string package = (no->package() != NULL 11544 ? no->package()->pkgpath() 11545 : gogo->pkgpath()); 11546 bool is_method = ((no->is_function() && no->func_value()->is_method()) 11547 || (no->is_function_declaration() 11548 && no->func_declaration_value()->is_method())); 11549 Location loc = this->location(); 11550 11551 Type* int_type = Type::lookup_integer_type("int"); 11552 Type* int32_type = Type::lookup_integer_type("int32"); 11553 Type* int64_type = Type::lookup_integer_type("int64"); 11554 Type* uint_type = Type::lookup_integer_type("uint"); 11555 Type* uint8_type = Type::lookup_integer_type("uint8"); 11556 Type* uint32_type = Type::lookup_integer_type("uint32"); 11557 Type* uint64_type = Type::lookup_integer_type("uint64"); 11558 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 11559 Type* pointer_type = Type::make_pointer_type(Type::make_void_type()); 11560 11561 int int_size = int_type->named_type()->real_type()->integer_type()->bits() / 8; 11562 int ptr_size = uintptr_type->named_type()->real_type()->integer_type()->bits() / 8; 11563 11564 if (package == "sync/atomic") 11565 { 11566 if (is_method) 11567 return NULL; 11568 11569 // sync/atomic functions and runtime/internal/atomic functions 11570 // are very similar. In order not to duplicate code, we just 11571 // redirect to the latter and let the code below to handle them. 11572 // Note: no StorePointer, SwapPointer, and CompareAndSwapPointer, 11573 // as they need write barriers. 11574 if (name == "LoadInt32") 11575 name = "Loadint32"; 11576 else if (name == "LoadInt64") 11577 name = "Loadint64"; 11578 else if (name == "LoadUint32") 11579 name = "Load"; 11580 else if (name == "LoadUint64") 11581 name = "Load64"; 11582 else if (name == "LoadUintptr") 11583 name = "Loaduintptr"; 11584 else if (name == "LoadPointer") 11585 name = "Loadp"; 11586 else if (name == "StoreInt32") 11587 name = "Storeint32"; 11588 else if (name == "StoreInt64") 11589 name = "Storeint64"; 11590 else if (name == "StoreUint32") 11591 name = "Store"; 11592 else if (name == "StoreUint64") 11593 name = "Store64"; 11594 else if (name == "StoreUintptr") 11595 name = "Storeuintptr"; 11596 else if (name == "AddInt32") 11597 name = "Xaddint32"; 11598 else if (name == "AddInt64") 11599 name = "Xaddint64"; 11600 else if (name == "AddUint32") 11601 name = "Xadd"; 11602 else if (name == "AddUint64") 11603 name = "Xadd64"; 11604 else if (name == "AddUintptr") 11605 name = "Xadduintptr"; 11606 else if (name == "SwapInt32") 11607 name = "Xchgint32"; 11608 else if (name == "SwapInt64") 11609 name = "Xchgint64"; 11610 else if (name == "SwapUint32") 11611 name = "Xchg"; 11612 else if (name == "SwapUint64") 11613 name = "Xchg64"; 11614 else if (name == "SwapUintptr") 11615 name = "Xchguintptr"; 11616 else if (name == "CompareAndSwapInt32") 11617 name = "Casint32"; 11618 else if (name == "CompareAndSwapInt64") 11619 name = "Casint64"; 11620 else if (name == "CompareAndSwapUint32") 11621 name = "Cas"; 11622 else if (name == "CompareAndSwapUint64") 11623 name = "Cas64"; 11624 else if (name == "CompareAndSwapUintptr") 11625 name = "Casuintptr"; 11626 else 11627 return NULL; 11628 11629 package = "runtime/internal/atomic"; 11630 } 11631 11632 if (package == "runtime/internal/sys") 11633 { 11634 if (is_method) 11635 return NULL; 11636 11637 // runtime/internal/sys functions and math/bits functions 11638 // are very similar. In order not to duplicate code, we just 11639 // redirect to the latter and let the code below to handle them. 11640 if (name == "Bswap32") 11641 name = "ReverseBytes32"; 11642 else if (name == "Bswap64") 11643 name = "ReverseBytes64"; 11644 else if (name == "Ctz32") 11645 name = "TrailingZeros32"; 11646 else if (name == "Ctz64") 11647 name = "TrailingZeros64"; 11648 else 11649 return NULL; 11650 11651 package = "math/bits"; 11652 } 11653 11654 if (package == "runtime") 11655 { 11656 if (is_method) 11657 return NULL; 11658 11659 // Handle a couple of special runtime functions. In the runtime 11660 // package, getcallerpc returns the PC of the caller, and 11661 // getcallersp returns the frame pointer of the caller. Implement 11662 // these by turning them into calls to GCC builtin functions. We 11663 // could implement them in normal code, but then we would have to 11664 // explicitly unwind the stack. These functions are intended to be 11665 // efficient. Note that this technique obviously only works for 11666 // direct calls, but that is the only way they are used. 11667 if (name == "getcallerpc" 11668 && (this->args_ == NULL || this->args_->size() == 0)) 11669 { 11670 Expression* arg = Expression::make_integer_ul(0, uint32_type, loc); 11671 Expression* call = 11672 Runtime::make_call(Runtime::BUILTIN_RETURN_ADDRESS, loc, 11673 1, arg); 11674 // The builtin functions return void*, but the Go functions return uintptr. 11675 return Expression::make_cast(uintptr_type, call, loc); 11676 } 11677 else if (name == "getcallersp" 11678 && (this->args_ == NULL || this->args_->size() == 0)) 11679 11680 { 11681 Expression* call = 11682 Runtime::make_call(Runtime::BUILTIN_DWARF_CFA, loc, 0); 11683 // The builtin functions return void*, but the Go functions return uintptr. 11684 return Expression::make_cast(uintptr_type, call, loc); 11685 } 11686 } 11687 else if (package == "math/bits") 11688 { 11689 if (is_method) 11690 return NULL; 11691 11692 if ((name == "ReverseBytes16" || name == "ReverseBytes32" 11693 || name == "ReverseBytes64" || name == "ReverseBytes") 11694 && this->args_ != NULL && this->args_->size() == 1) 11695 { 11696 Runtime::Function code; 11697 if (name == "ReverseBytes16") 11698 code = Runtime::BUILTIN_BSWAP16; 11699 else if (name == "ReverseBytes32") 11700 code = Runtime::BUILTIN_BSWAP32; 11701 else if (name == "ReverseBytes64") 11702 code = Runtime::BUILTIN_BSWAP64; 11703 else if (name == "ReverseBytes") 11704 code = (int_size == 8 ? Runtime::BUILTIN_BSWAP64 : Runtime::BUILTIN_BSWAP32); 11705 else 11706 go_unreachable(); 11707 Expression* arg = this->args_->front(); 11708 Expression* call = Runtime::make_call(code, loc, 1, arg); 11709 if (name == "ReverseBytes") 11710 return Expression::make_cast(uint_type, call, loc); 11711 return call; 11712 } 11713 else if ((name == "TrailingZeros8" || name == "TrailingZeros16") 11714 && this->args_ != NULL && this->args_->size() == 1) 11715 { 11716 // GCC does not have a ctz8 or ctz16 intrinsic. We do 11717 // ctz32(0x100 | arg) or ctz32(0x10000 | arg). 11718 Expression* arg = this->args_->front(); 11719 arg = Expression::make_cast(uint32_type, arg, loc); 11720 unsigned long mask = (name == "TrailingZeros8" ? 0x100 : 0x10000); 11721 Expression* c = Expression::make_integer_ul(mask, uint32_type, loc); 11722 arg = Expression::make_binary(OPERATOR_OR, arg, c, loc); 11723 Expression* call = Runtime::make_call(Runtime::BUILTIN_CTZ, loc, 1, arg); 11724 return Expression::make_cast(int_type, call, loc); 11725 } 11726 else if ((name == "TrailingZeros32" 11727 || (name == "TrailingZeros" && int_size == 4)) 11728 && this->args_ != NULL && this->args_->size() == 1) 11729 { 11730 Expression* arg = this->args_->front(); 11731 if (!arg->is_multi_eval_safe()) 11732 { 11733 Temporary_statement* ts = Statement::make_temporary(uint32_type, arg, loc); 11734 inserter->insert(ts); 11735 arg = Expression::make_temporary_reference(ts, loc); 11736 } 11737 // arg == 0 ? 32 : __builtin_ctz(arg) 11738 Expression* zero = Expression::make_integer_ul(0, uint32_type, loc); 11739 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc); 11740 Expression* c32 = Expression::make_integer_ul(32, int_type, loc); 11741 Expression* call = Runtime::make_call(Runtime::BUILTIN_CTZ, loc, 1, arg->copy()); 11742 call = Expression::make_cast(int_type, call, loc); 11743 return Expression::make_conditional(cmp, c32, call, loc); 11744 } 11745 else if ((name == "TrailingZeros64" 11746 || (name == "TrailingZeros" && int_size == 8)) 11747 && this->args_ != NULL && this->args_->size() == 1) 11748 { 11749 Expression* arg = this->args_->front(); 11750 if (!arg->is_multi_eval_safe()) 11751 { 11752 Temporary_statement* ts = Statement::make_temporary(uint64_type, arg, loc); 11753 inserter->insert(ts); 11754 arg = Expression::make_temporary_reference(ts, loc); 11755 } 11756 // arg == 0 ? 64 : __builtin_ctzll(arg) 11757 Expression* zero = Expression::make_integer_ul(0, uint64_type, loc); 11758 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc); 11759 Expression* c64 = Expression::make_integer_ul(64, int_type, loc); 11760 Expression* call = Runtime::make_call(Runtime::BUILTIN_CTZLL, loc, 1, arg->copy()); 11761 call = Expression::make_cast(int_type, call, loc); 11762 return Expression::make_conditional(cmp, c64, call, loc); 11763 } 11764 else if ((name == "LeadingZeros8" || name == "LeadingZeros16" 11765 || name == "Len8" || name == "Len16") 11766 && this->args_ != NULL && this->args_->size() == 1) 11767 { 11768 // GCC does not have a clz8 ir clz16 intrinsic. We do 11769 // clz32(arg<<24 | 0xffffff) or clz32(arg<<16 | 0xffff). 11770 Expression* arg = this->args_->front(); 11771 arg = Expression::make_cast(uint32_type, arg, loc); 11772 unsigned long shift = 11773 ((name == "LeadingZeros8" || name == "Len8") ? 24 : 16); 11774 Expression* c = Expression::make_integer_ul(shift, uint32_type, loc); 11775 arg = Expression::make_binary(OPERATOR_LSHIFT, arg, c, loc); 11776 unsigned long mask = 11777 ((name == "LeadingZeros8" || name == "Len8") ? 0xffffff : 0xffff); 11778 c = Expression::make_integer_ul(mask, uint32_type, loc); 11779 arg = Expression::make_binary(OPERATOR_OR, arg, c, loc); 11780 Expression* call = Runtime::make_call(Runtime::BUILTIN_CLZ, loc, 1, arg); 11781 call = Expression::make_cast(int_type, call, loc); 11782 // len = width - clz 11783 if (name == "Len8") 11784 { 11785 c = Expression::make_integer_ul(8, int_type, loc); 11786 return Expression::make_binary(OPERATOR_MINUS, c, call, loc); 11787 } 11788 else if (name == "Len16") 11789 { 11790 c = Expression::make_integer_ul(16, int_type, loc); 11791 return Expression::make_binary(OPERATOR_MINUS, c, call, loc); 11792 } 11793 return call; 11794 } 11795 else if ((name == "LeadingZeros32" || name == "Len32" 11796 || ((name == "LeadingZeros" || name == "Len") && int_size == 4)) 11797 && this->args_ != NULL && this->args_->size() == 1) 11798 { 11799 Expression* arg = this->args_->front(); 11800 if (!arg->is_multi_eval_safe()) 11801 { 11802 Temporary_statement* ts = Statement::make_temporary(uint32_type, arg, loc); 11803 inserter->insert(ts); 11804 arg = Expression::make_temporary_reference(ts, loc); 11805 } 11806 // arg == 0 ? 32 : __builtin_clz(arg) 11807 Expression* zero = Expression::make_integer_ul(0, uint32_type, loc); 11808 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc); 11809 Expression* c32 = Expression::make_integer_ul(32, int_type, loc); 11810 Expression* call = Runtime::make_call(Runtime::BUILTIN_CLZ, loc, 1, arg->copy()); 11811 call = Expression::make_cast(int_type, call, loc); 11812 Expression* cond = Expression::make_conditional(cmp, c32, call, loc); 11813 // len = 32 - clz 11814 if (name == "Len32" || name == "Len") 11815 return Expression::make_binary(OPERATOR_MINUS, c32->copy(), cond, loc); 11816 return cond; 11817 } 11818 else if ((name == "LeadingZeros64" || name == "Len64" 11819 || ((name == "LeadingZeros" || name == "Len") && int_size == 8)) 11820 && this->args_ != NULL && this->args_->size() == 1) 11821 { 11822 Expression* arg = this->args_->front(); 11823 if (!arg->is_multi_eval_safe()) 11824 { 11825 Temporary_statement* ts = Statement::make_temporary(uint64_type, arg, loc); 11826 inserter->insert(ts); 11827 arg = Expression::make_temporary_reference(ts, loc); 11828 } 11829 // arg == 0 ? 64 : __builtin_clzll(arg) 11830 Expression* zero = Expression::make_integer_ul(0, uint64_type, loc); 11831 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc); 11832 Expression* c64 = Expression::make_integer_ul(64, int_type, loc); 11833 Expression* call = Runtime::make_call(Runtime::BUILTIN_CLZLL, loc, 1, arg->copy()); 11834 call = Expression::make_cast(int_type, call, loc); 11835 Expression* cond = Expression::make_conditional(cmp, c64, call, loc); 11836 // len = 64 - clz 11837 if (name == "Len64" || name == "Len") 11838 return Expression::make_binary(OPERATOR_MINUS, c64->copy(), cond, loc); 11839 return cond; 11840 } 11841 else if ((name == "OnesCount8" || name == "OnesCount16" 11842 || name == "OnesCount32" || name == "OnesCount64" 11843 || name == "OnesCount") 11844 && this->args_ != NULL && this->args_->size() == 1) 11845 { 11846 Runtime::Function code; 11847 if (name == "OnesCount64") 11848 code = Runtime::BUILTIN_POPCOUNTLL; 11849 else if (name == "OnesCount") 11850 code = (int_size == 8 ? Runtime::BUILTIN_POPCOUNTLL : Runtime::BUILTIN_POPCOUNT); 11851 else 11852 code = Runtime::BUILTIN_POPCOUNT; 11853 Expression* arg = this->args_->front(); 11854 Expression* call = Runtime::make_call(code, loc, 1, arg); 11855 return Expression::make_cast(int_type, call, loc); 11856 } 11857 } 11858 else if (package == "runtime/internal/atomic") 11859 { 11860 int memorder = __ATOMIC_SEQ_CST; 11861 11862 if (is_method) 11863 { 11864 Function_type* ftype = (no->is_function() 11865 ? no->func_value()->type() 11866 : no->func_declaration_value()->type()); 11867 Type* rtype = ftype->receiver()->type()->deref(); 11868 go_assert(rtype->named_type() != NULL); 11869 const std::string& rname(rtype->named_type()->name()); 11870 if (rname == "Int32") 11871 { 11872 if (name == "Load") 11873 name = "LoadInt32"; 11874 else if (name == "Store") 11875 name = "Storeint32"; 11876 else if (name == "CompareAndSwap") 11877 name = "Casint32"; 11878 else if (name == "Swap") 11879 name = "Xchgint32"; 11880 else if (name == "Add") 11881 name = "Xaddint32"; 11882 else 11883 go_unreachable(); 11884 } 11885 else if (rname == "Int64") 11886 { 11887 if (name == "Load") 11888 name = "LoadInt64"; 11889 else if (name == "Store") 11890 name = "Storeint64"; 11891 else if (name == "CompareAndSwap") 11892 name = "Casint64"; 11893 else if (name == "Swap") 11894 name = "Xchgint64"; 11895 else if (name == "Add") 11896 name = "Xaddint64"; 11897 else 11898 go_unreachable(); 11899 } 11900 else if (rname == "Uint8") 11901 { 11902 if (name == "Load") 11903 name = "Load8"; 11904 else if (name == "Store") 11905 name = "Store8"; 11906 else if (name == "And") 11907 name = "And8"; 11908 else if (name == "Or") 11909 name = "Or8"; 11910 else 11911 go_unreachable(); 11912 } 11913 else if (rname == "Uint32") 11914 { 11915 if (name == "Load") 11916 name = "Load"; 11917 else if (name == "LoadAcquire") 11918 name = "LoadAcq"; 11919 else if (name == "Store") 11920 name = "Store"; 11921 else if (name == "CompareAndSwap") 11922 name = "Cas"; 11923 else if (name == "CompareAndSwapRelease") 11924 name = "CasRel"; 11925 else if (name == "Swap") 11926 name = "Xchg"; 11927 else if (name == "And") 11928 name = "And"; 11929 else if (name == "Or") 11930 name = "Or"; 11931 else if (name == "Add") 11932 name = "Xadd"; 11933 else 11934 go_unreachable(); 11935 } 11936 else if (rname == "Uint64") 11937 { 11938 if (name == "Load") 11939 name = "Load64"; 11940 else if (name == "Store") 11941 name = "Store64"; 11942 else if (name == "CompareAndSwap") 11943 name = "Cas64"; 11944 else if (name == "Swap") 11945 name = "Xchgt64"; 11946 else if (name == "Add") 11947 name = "Xadd64"; 11948 else 11949 go_unreachable(); 11950 } 11951 else if (rname == "Uintptr") 11952 { 11953 if (name == "Load") 11954 name = "Loaduintptr"; 11955 else if (name == "LoadAcquire") 11956 name = "Loadacquintptr"; 11957 else if (name == "Store") 11958 name = "Storeuintptr"; 11959 else if (name == "StoreRelease") 11960 name = "StoreReluintptr"; 11961 else if (name == "CompareAndSwap") 11962 name = "Casuintptr"; 11963 else if (name == "Swap") 11964 name = "Xchguintptr"; 11965 else if (name == "Add") 11966 name = "Xadduintptr"; 11967 else 11968 go_unreachable(); 11969 } 11970 else if (rname == "Float64") 11971 { 11972 // Needs unsafe type conversion. Don't intrinsify for now. 11973 return NULL; 11974 } 11975 else if (rname == "UnsafePointer") 11976 { 11977 if (name == "Load") 11978 name = "Loadp"; 11979 else if (name == "StoreNoWB") 11980 name = "StorepoWB"; 11981 else if (name == "CompareAndSwapNoWB") 11982 name = "Casp1"; 11983 else 11984 go_unreachable(); 11985 } 11986 else 11987 go_unreachable(); 11988 } 11989 11990 if ((name == "Load" || name == "Load64" || name == "Loadint64" || name == "Loadp" 11991 || name == "Loaduint" || name == "Loaduintptr" || name == "LoadAcq" 11992 || name == "Loadint32" || name == "Load8") 11993 && this->args_ != NULL && this->args_->size() == 1) 11994 { 11995 if (int_size < 8 && (name == "Load64" || name == "Loadint64")) 11996 // On 32-bit architectures we need to check alignment. 11997 // Not intrinsify for now. 11998 return NULL; 11999 12000 Runtime::Function code; 12001 Type* res_type; 12002 if (name == "Load") 12003 { 12004 code = Runtime::ATOMIC_LOAD_4; 12005 res_type = uint32_type; 12006 } 12007 else if (name == "Load64") 12008 { 12009 code = Runtime::ATOMIC_LOAD_8; 12010 res_type = uint64_type; 12011 } 12012 else if (name == "Loadint32") 12013 { 12014 code = Runtime::ATOMIC_LOAD_4; 12015 res_type = int32_type; 12016 } 12017 else if (name == "Loadint64") 12018 { 12019 code = Runtime::ATOMIC_LOAD_8; 12020 res_type = int64_type; 12021 } 12022 else if (name == "Loaduint") 12023 { 12024 code = (int_size == 8 12025 ? Runtime::ATOMIC_LOAD_8 12026 : Runtime::ATOMIC_LOAD_4); 12027 res_type = uint_type; 12028 } 12029 else if (name == "Loaduintptr") 12030 { 12031 code = (ptr_size == 8 12032 ? Runtime::ATOMIC_LOAD_8 12033 : Runtime::ATOMIC_LOAD_4); 12034 res_type = uintptr_type; 12035 } 12036 else if (name == "Loadp") 12037 { 12038 code = (ptr_size == 8 12039 ? Runtime::ATOMIC_LOAD_8 12040 : Runtime::ATOMIC_LOAD_4); 12041 res_type = pointer_type; 12042 } 12043 else if (name == "LoadAcq") 12044 { 12045 code = Runtime::ATOMIC_LOAD_4; 12046 res_type = uint32_type; 12047 memorder = __ATOMIC_ACQUIRE; 12048 } 12049 else if (name == "Load8") 12050 { 12051 code = Runtime::ATOMIC_LOAD_1; 12052 res_type = uint8_type; 12053 } 12054 else 12055 go_unreachable(); 12056 Expression* a1 = this->args_->front(); 12057 Expression* a2 = Expression::make_integer_ul(memorder, int32_type, loc); 12058 Expression* call = Runtime::make_call(code, loc, 2, a1, a2); 12059 return Expression::make_unsafe_cast(res_type, call, loc); 12060 } 12061 12062 if ((name == "Store" || name == "Store64" || name == "StorepNoWB" 12063 || name == "Storeuintptr" || name == "StoreRel" 12064 || name == "Storeint32" || name == "Storeint64") 12065 && this->args_ != NULL && this->args_->size() == 2) 12066 { 12067 if (int_size < 8 && (name == "Store64" || name == "Storeint64")) 12068 return NULL; 12069 12070 Runtime::Function code; 12071 Expression* a1 = this->args_->at(0); 12072 Expression* a2 = this->args_->at(1); 12073 if (name == "Store") 12074 code = Runtime::ATOMIC_STORE_4; 12075 else if (name == "Store64") 12076 code = Runtime::ATOMIC_STORE_8; 12077 else if (name == "Storeint32") 12078 code = Runtime::ATOMIC_STORE_4; 12079 else if (name == "Storeint64") 12080 code = Runtime::ATOMIC_STORE_8; 12081 else if (name == "Storeuintptr") 12082 code = (ptr_size == 8 ? Runtime::ATOMIC_STORE_8 : Runtime::ATOMIC_STORE_4); 12083 else if (name == "StorepNoWB") 12084 { 12085 code = (ptr_size == 8 ? Runtime::ATOMIC_STORE_8 : Runtime::ATOMIC_STORE_4); 12086 a2 = Expression::make_unsafe_cast(uintptr_type, a2, loc); 12087 a2 = Expression::make_cast(uint64_type, a2, loc); 12088 } 12089 else if (name == "StoreRel") 12090 { 12091 code = Runtime::ATOMIC_STORE_4; 12092 memorder = __ATOMIC_RELEASE; 12093 } 12094 else if (name == "Store8") 12095 code = Runtime::ATOMIC_STORE_1; 12096 else 12097 go_unreachable(); 12098 Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc); 12099 return Runtime::make_call(code, loc, 3, a1, a2, a3); 12100 } 12101 12102 if ((name == "Xchg" || name == "Xchg64" || name == "Xchguintptr" 12103 || name == "Xchgint32" || name == "Xchgint64") 12104 && this->args_ != NULL && this->args_->size() == 2) 12105 { 12106 if (int_size < 8 && (name == "Xchg64" || name == "Xchgint64")) 12107 return NULL; 12108 12109 Runtime::Function code; 12110 Type* res_type; 12111 if (name == "Xchg") 12112 { 12113 code = Runtime::ATOMIC_EXCHANGE_4; 12114 res_type = uint32_type; 12115 } 12116 else if (name == "Xchg64") 12117 { 12118 code = Runtime::ATOMIC_EXCHANGE_8; 12119 res_type = uint64_type; 12120 } 12121 else if (name == "Xchgint32") 12122 { 12123 code = Runtime::ATOMIC_EXCHANGE_4; 12124 res_type = int32_type; 12125 } 12126 else if (name == "Xchgint64") 12127 { 12128 code = Runtime::ATOMIC_EXCHANGE_8; 12129 res_type = int64_type; 12130 } 12131 else if (name == "Xchguintptr") 12132 { 12133 code = (ptr_size == 8 12134 ? Runtime::ATOMIC_EXCHANGE_8 12135 : Runtime::ATOMIC_EXCHANGE_4); 12136 res_type = uintptr_type; 12137 } 12138 else 12139 go_unreachable(); 12140 Expression* a1 = this->args_->at(0); 12141 Expression* a2 = this->args_->at(1); 12142 Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc); 12143 Expression* call = Runtime::make_call(code, loc, 3, a1, a2, a3); 12144 return Expression::make_cast(res_type, call, loc); 12145 } 12146 12147 if ((name == "Cas" || name == "Cas64" || name == "Casuintptr" 12148 || name == "Casp1" || name == "CasRel" 12149 || name == "Casint32" || name == "Casint64") 12150 && this->args_ != NULL && this->args_->size() == 3) 12151 { 12152 if (int_size < 8 && (name == "Cas64" || name == "Casint64")) 12153 return NULL; 12154 12155 Runtime::Function code; 12156 Expression* a1 = this->args_->at(0); 12157 12158 // Builtin cas takes a pointer to the old value. 12159 // Store it in a temporary and take the address. 12160 Expression* a2 = this->args_->at(1); 12161 Temporary_statement* ts = Statement::make_temporary(NULL, a2, loc); 12162 inserter->insert(ts); 12163 a2 = Expression::make_temporary_reference(ts, loc); 12164 a2 = Expression::make_unary(OPERATOR_AND, a2, loc); 12165 12166 Expression* a3 = this->args_->at(2); 12167 if (name == "Cas") 12168 code = Runtime::ATOMIC_COMPARE_EXCHANGE_4; 12169 else if (name == "Cas64") 12170 code = Runtime::ATOMIC_COMPARE_EXCHANGE_8; 12171 else if (name == "Casint32") 12172 code = Runtime::ATOMIC_COMPARE_EXCHANGE_4; 12173 else if (name == "Casint64") 12174 code = Runtime::ATOMIC_COMPARE_EXCHANGE_8; 12175 else if (name == "Casuintptr") 12176 code = (ptr_size == 8 12177 ? Runtime::ATOMIC_COMPARE_EXCHANGE_8 12178 : Runtime::ATOMIC_COMPARE_EXCHANGE_4); 12179 else if (name == "Casp1") 12180 { 12181 code = (ptr_size == 8 12182 ? Runtime::ATOMIC_COMPARE_EXCHANGE_8 12183 : Runtime::ATOMIC_COMPARE_EXCHANGE_4); 12184 a3 = Expression::make_unsafe_cast(uintptr_type, a3, loc); 12185 a3 = Expression::make_cast(uint64_type, a3, loc); 12186 } 12187 else if (name == "CasRel") 12188 { 12189 code = Runtime::ATOMIC_COMPARE_EXCHANGE_4; 12190 memorder = __ATOMIC_RELEASE; 12191 } 12192 else 12193 go_unreachable(); 12194 Expression* a4 = Expression::make_boolean(false, loc); 12195 Expression* a5 = Expression::make_integer_ul(memorder, int32_type, loc); 12196 Expression* a6 = Expression::make_integer_ul(__ATOMIC_RELAXED, int32_type, loc); 12197 return Runtime::make_call(code, loc, 6, a1, a2, a3, a4, a5, a6); 12198 } 12199 12200 if ((name == "Xadd" || name == "Xadd64" || name == "Xaddint64" 12201 || name == "Xadduintptr" || name == "Xaddint32") 12202 && this->args_ != NULL && this->args_->size() == 2) 12203 { 12204 if (int_size < 8 && (name == "Xadd64" || name == "Xaddint64")) 12205 return NULL; 12206 12207 Runtime::Function code; 12208 Type* res_type; 12209 if (name == "Xadd") 12210 { 12211 code = Runtime::ATOMIC_ADD_FETCH_4; 12212 res_type = uint32_type; 12213 } 12214 else if (name == "Xadd64") 12215 { 12216 code = Runtime::ATOMIC_ADD_FETCH_8; 12217 res_type = uint64_type; 12218 } 12219 else if (name == "Xaddint32") 12220 { 12221 code = Runtime::ATOMIC_ADD_FETCH_4; 12222 res_type = int32_type; 12223 } 12224 else if (name == "Xaddint64") 12225 { 12226 code = Runtime::ATOMIC_ADD_FETCH_8; 12227 res_type = int64_type; 12228 } 12229 else if (name == "Xadduintptr") 12230 { 12231 code = (ptr_size == 8 12232 ? Runtime::ATOMIC_ADD_FETCH_8 12233 : Runtime::ATOMIC_ADD_FETCH_4); 12234 res_type = uintptr_type; 12235 } 12236 else 12237 go_unreachable(); 12238 Expression* a1 = this->args_->at(0); 12239 Expression* a2 = this->args_->at(1); 12240 Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc); 12241 Expression* call = Runtime::make_call(code, loc, 3, a1, a2, a3); 12242 return Expression::make_cast(res_type, call, loc); 12243 } 12244 12245 if ((name == "And8" || name == "Or8") 12246 && this->args_ != NULL && this->args_->size() == 2) 12247 { 12248 Runtime::Function code; 12249 if (name == "And8") 12250 code = Runtime::ATOMIC_AND_FETCH_1; 12251 else if (name == "Or8") 12252 code = Runtime::ATOMIC_OR_FETCH_1; 12253 else 12254 go_unreachable(); 12255 Expression* a1 = this->args_->at(0); 12256 Expression* a2 = this->args_->at(1); 12257 Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc); 12258 return Runtime::make_call(code, loc, 3, a1, a2, a3); 12259 } 12260 } 12261 else if (package == "internal/abi") 12262 { 12263 if (is_method) 12264 return NULL; 12265 12266 if ((name == "FuncPCABI0" || name == "FuncPCABIInternal") 12267 && this->args_ != NULL 12268 && this->args_->size() == 1) 12269 { 12270 // We expect to see a conversion from the expression to "any". 12271 Expression* expr = this->args_->front(); 12272 Type_conversion_expression* tce = expr->conversion_expression(); 12273 if (tce != NULL) 12274 expr = tce->expr(); 12275 Func_expression* fe = expr->func_expression(); 12276 Interface_field_reference_expression* interface_method = 12277 expr->interface_field_reference_expression(); 12278 if (fe != NULL) 12279 { 12280 Named_object* no = fe->named_object(); 12281 Expression* ref = Expression::make_func_code_reference(no, loc); 12282 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 12283 return Expression::make_cast(uintptr_type, ref, loc); 12284 } 12285 else if (interface_method != NULL) 12286 return interface_method->get_function(); 12287 else 12288 { 12289 expr = this->args_->front(); 12290 go_assert(expr->type()->interface_type() != NULL 12291 && expr->type()->interface_type()->is_empty()); 12292 expr = Expression::make_interface_info(expr, 12293 INTERFACE_INFO_OBJECT, 12294 loc); 12295 // Trust that this is a function type, which means that 12296 // it is a direct iface type and we can use EXPR 12297 // directly. The backend representation of this 12298 // function is a pointer to a struct whose first field 12299 // is the actual function to call. 12300 Type* pvoid = Type::make_pointer_type(Type::make_void_type()); 12301 Type* pfntype = Type::make_pointer_type(pvoid); 12302 Expression* ref = make_unsafe_cast(pfntype, expr, loc); 12303 return Expression::make_dereference(ref, NIL_CHECK_NOT_NEEDED, 12304 loc); 12305 } 12306 } 12307 } 12308 12309 return NULL; 12310 } 12311 12312 // Make implicit type conversions explicit. 12313 12314 void 12315 Call_expression::do_add_conversions() 12316 { 12317 // Skip call that requires a thunk. We generate conversions inside the thunk. 12318 if (this->is_concurrent_ || this->is_deferred_) 12319 return; 12320 12321 if (this->args_ == NULL || this->args_->empty()) 12322 return; 12323 12324 Function_type* fntype = this->get_function_type(); 12325 if (fntype == NULL) 12326 { 12327 go_assert(saw_errors()); 12328 return; 12329 } 12330 if (fntype->parameters() == NULL || fntype->parameters()->empty()) 12331 return; 12332 12333 Location loc = this->location(); 12334 Expression_list::iterator pa = this->args_->begin(); 12335 Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); 12336 bool is_interface_method = 12337 this->fn_->interface_field_reference_expression() != NULL; 12338 size_t argcount = this->args_->size(); 12339 if (!is_interface_method && fntype->is_method()) 12340 { 12341 // Skip the receiver argument, which cannot be interface. 12342 pa++; 12343 argcount--; 12344 } 12345 if (argcount != fntype->parameters()->size()) 12346 { 12347 go_assert(saw_errors()); 12348 return; 12349 } 12350 for (; pa != this->args_->end(); ++pa, ++pp) 12351 { 12352 Type* pt = pp->type(); 12353 if (!Type::are_identical(pt, (*pa)->type(), 0, NULL) 12354 && pt->interface_type() != NULL) 12355 *pa = Expression::make_cast(pt, *pa, loc); 12356 } 12357 } 12358 12359 // Get the function type. This can return NULL in error cases. 12360 12361 Function_type* 12362 Call_expression::get_function_type() const 12363 { 12364 return this->fn_->type()->function_type(); 12365 } 12366 12367 // Return the number of values which this call will return. 12368 12369 size_t 12370 Call_expression::result_count() const 12371 { 12372 const Function_type* fntype = this->get_function_type(); 12373 if (fntype == NULL) 12374 return 0; 12375 if (fntype->results() == NULL) 12376 return 0; 12377 return fntype->results()->size(); 12378 } 12379 12380 // Return the temporary that holds the result for a call with multiple 12381 // results. 12382 12383 Temporary_statement* 12384 Call_expression::results() const 12385 { 12386 if (this->call_temp_ == NULL) 12387 { 12388 go_assert(saw_errors()); 12389 return NULL; 12390 } 12391 return this->call_temp_; 12392 } 12393 12394 // Set the number of results expected from a call expression. 12395 12396 void 12397 Call_expression::set_expected_result_count(size_t count) 12398 { 12399 go_assert(this->expected_result_count_ == 0); 12400 this->expected_result_count_ = count; 12401 } 12402 12403 // Return whether this is a call to the predeclared function recover. 12404 12405 bool 12406 Call_expression::is_recover_call() const 12407 { 12408 return this->do_is_recover_call(); 12409 } 12410 12411 // Set the argument to the recover function. 12412 12413 void 12414 Call_expression::set_recover_arg(Expression* arg) 12415 { 12416 this->do_set_recover_arg(arg); 12417 } 12418 12419 // Virtual functions also implemented by Builtin_call_expression. 12420 12421 bool 12422 Call_expression::do_is_recover_call() const 12423 { 12424 return false; 12425 } 12426 12427 void 12428 Call_expression::do_set_recover_arg(Expression*) 12429 { 12430 go_unreachable(); 12431 } 12432 12433 // We have found an error with this call expression; return true if 12434 // we should report it. 12435 12436 bool 12437 Call_expression::issue_error() 12438 { 12439 if (this->issued_error_) 12440 return false; 12441 else 12442 { 12443 this->issued_error_ = true; 12444 return true; 12445 } 12446 } 12447 12448 // Whether or not this call contains errors, either in the call or the 12449 // arguments to the call. 12450 12451 bool 12452 Call_expression::is_erroneous_call() 12453 { 12454 if (this->is_error_expression() || this->fn()->is_error_expression()) 12455 return true; 12456 12457 if (this->args() == NULL) 12458 return false; 12459 for (Expression_list::iterator pa = this->args()->begin(); 12460 pa != this->args()->end(); 12461 ++pa) 12462 { 12463 if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) 12464 return true; 12465 } 12466 return false; 12467 } 12468 12469 // Get the type. 12470 12471 Type* 12472 Call_expression::do_type() 12473 { 12474 if (this->is_error_expression()) 12475 return Type::make_error_type(); 12476 if (this->type_ != NULL) 12477 return this->type_; 12478 12479 Type* ret; 12480 Function_type* fntype = this->get_function_type(); 12481 if (fntype == NULL) 12482 return Type::make_error_type(); 12483 12484 const Typed_identifier_list* results = fntype->results(); 12485 if (results == NULL) 12486 ret = Type::make_void_type(); 12487 else if (results->size() == 1) 12488 ret = results->begin()->type(); 12489 else 12490 ret = Type::make_call_multiple_result_type(this); 12491 12492 this->type_ = ret; 12493 12494 return this->type_; 12495 } 12496 12497 // Determine types for a call expression. We can use the function 12498 // parameter types to set the types of the arguments. 12499 12500 void 12501 Call_expression::do_determine_type(const Type_context* context) 12502 { 12503 if (!this->determining_types()) 12504 return; 12505 12506 this->fn_->determine_type_no_context(); 12507 Function_type* fntype = this->get_function_type(); 12508 const Typed_identifier_list* parameters = NULL; 12509 if (fntype != NULL) 12510 parameters = fntype->parameters(); 12511 if (this->args_ != NULL) 12512 { 12513 Typed_identifier_list::const_iterator pt; 12514 if (parameters != NULL) 12515 pt = parameters->begin(); 12516 bool first = true; 12517 for (Expression_list::const_iterator pa = this->args_->begin(); 12518 pa != this->args_->end(); 12519 ++pa) 12520 { 12521 if (first) 12522 { 12523 first = false; 12524 // If this is a method, the first argument is the 12525 // receiver. 12526 if (fntype != NULL && fntype->is_method()) 12527 { 12528 Type* rtype = fntype->receiver()->type(); 12529 // The receiver is always passed as a pointer. 12530 if (rtype->points_to() == NULL) 12531 rtype = Type::make_pointer_type(rtype); 12532 Type_context subcontext(rtype, false); 12533 (*pa)->determine_type(&subcontext); 12534 continue; 12535 } 12536 } 12537 12538 if (parameters != NULL && pt != parameters->end()) 12539 { 12540 Type_context subcontext(pt->type(), false); 12541 (*pa)->determine_type(&subcontext); 12542 ++pt; 12543 } 12544 else 12545 (*pa)->determine_type_no_context(); 12546 } 12547 } 12548 12549 // If this is a call to a generated equality function, we determine 12550 // the type based on the context. See the comment in 12551 // Binary_expression::lower_array_comparison. 12552 if (this->is_equal_function_ 12553 && !context->may_be_abstract 12554 && context->type != NULL 12555 && context->type->is_boolean_type() 12556 && context->type != Type::lookup_bool_type()) 12557 { 12558 go_assert(this->type_ == NULL 12559 || this->type_ == Type::lookup_bool_type() 12560 || this->type_ == context->type 12561 || this->type_->is_error()); 12562 this->type_ = context->type; 12563 } 12564 } 12565 12566 // Called when determining types for a Call_expression. Return true 12567 // if we should go ahead, false if they have already been determined. 12568 12569 bool 12570 Call_expression::determining_types() 12571 { 12572 if (this->types_are_determined_) 12573 return false; 12574 else 12575 { 12576 this->types_are_determined_ = true; 12577 return true; 12578 } 12579 } 12580 12581 // Check types for parameter I. 12582 12583 bool 12584 Call_expression::check_argument_type(int i, const Type* parameter_type, 12585 const Type* argument_type, 12586 Location argument_location, 12587 bool issued_error) 12588 { 12589 std::string reason; 12590 if (!Type::are_assignable(parameter_type, argument_type, &reason)) 12591 { 12592 if (!issued_error) 12593 { 12594 if (reason.empty()) 12595 go_error_at(argument_location, "argument %d has incompatible type", i); 12596 else 12597 go_error_at(argument_location, 12598 "argument %d has incompatible type (%s)", 12599 i, reason.c_str()); 12600 } 12601 this->set_is_error(); 12602 return false; 12603 } 12604 return true; 12605 } 12606 12607 // Check types. 12608 12609 void 12610 Call_expression::do_check_types(Gogo*) 12611 { 12612 if (this->classification() == EXPRESSION_ERROR) 12613 return; 12614 12615 Function_type* fntype = this->get_function_type(); 12616 if (fntype == NULL) 12617 { 12618 if (!this->fn_->type()->is_error()) 12619 this->report_error(_("expected function")); 12620 return; 12621 } 12622 12623 if (this->expected_result_count_ != 0 12624 && this->expected_result_count_ != this->result_count()) 12625 { 12626 if (this->issue_error()) 12627 this->report_error(_("function result count mismatch")); 12628 this->set_is_error(); 12629 return; 12630 } 12631 12632 bool is_method = fntype->is_method(); 12633 if (is_method) 12634 { 12635 go_assert(this->args_ != NULL && !this->args_->empty()); 12636 Type* rtype = fntype->receiver()->type(); 12637 Expression* first_arg = this->args_->front(); 12638 // We dereference the values since receivers are always passed 12639 // as pointers. 12640 std::string reason; 12641 if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), 12642 &reason)) 12643 { 12644 if (reason.empty()) 12645 this->report_error(_("incompatible type for receiver")); 12646 else 12647 { 12648 go_error_at(this->location(), 12649 "incompatible type for receiver (%s)", 12650 reason.c_str()); 12651 this->set_is_error(); 12652 } 12653 } 12654 } 12655 12656 // Note that varargs was handled by the lower_varargs() method, so 12657 // we don't have to worry about it here unless something is wrong. 12658 if (this->is_varargs_ && !this->varargs_are_lowered_) 12659 { 12660 if (!fntype->is_varargs()) 12661 { 12662 go_error_at(this->location(), 12663 _("invalid use of %<...%> calling non-variadic function")); 12664 this->set_is_error(); 12665 return; 12666 } 12667 } 12668 12669 const Typed_identifier_list* parameters = fntype->parameters(); 12670 if (this->args_ == NULL || this->args_->size() == 0) 12671 { 12672 if (parameters != NULL && !parameters->empty()) 12673 this->report_error(_("not enough arguments")); 12674 } 12675 else if (parameters == NULL) 12676 { 12677 if (!is_method || this->args_->size() > 1) 12678 this->report_error(_("too many arguments")); 12679 } 12680 else if (this->args_->size() == 1 12681 && this->args_->front()->call_expression() != NULL 12682 && this->args_->front()->call_expression()->result_count() > 1) 12683 { 12684 // This is F(G()) when G returns more than one result. If the 12685 // results can be matched to parameters, it would have been 12686 // lowered in do_lower. If we get here we know there is a 12687 // mismatch. 12688 if (this->args_->front()->call_expression()->result_count() 12689 < parameters->size()) 12690 this->report_error(_("not enough arguments")); 12691 else 12692 this->report_error(_("too many arguments")); 12693 } 12694 else 12695 { 12696 int i = 0; 12697 Expression_list::const_iterator pa = this->args_->begin(); 12698 if (is_method) 12699 ++pa; 12700 for (Typed_identifier_list::const_iterator pt = parameters->begin(); 12701 pt != parameters->end(); 12702 ++pt, ++pa, ++i) 12703 { 12704 if (pa == this->args_->end()) 12705 { 12706 this->report_error(_("not enough arguments")); 12707 return; 12708 } 12709 this->check_argument_type(i + 1, pt->type(), (*pa)->type(), 12710 (*pa)->location(), false); 12711 } 12712 if (pa != this->args_->end()) 12713 this->report_error(_("too many arguments")); 12714 } 12715 } 12716 12717 Expression* 12718 Call_expression::do_copy() 12719 { 12720 Call_expression* call = 12721 Expression::make_call(this->fn_->copy(), 12722 (this->args_ == NULL 12723 ? NULL 12724 : this->args_->copy()), 12725 this->is_varargs_, this->location()); 12726 12727 if (this->varargs_are_lowered_) 12728 call->set_varargs_are_lowered(); 12729 if (this->is_deferred_) 12730 call->set_is_deferred(); 12731 if (this->is_concurrent_) 12732 call->set_is_concurrent(); 12733 return call; 12734 } 12735 12736 // Return whether we have to use a temporary variable to ensure that 12737 // we evaluate this call expression in order. If the call returns no 12738 // results then it will inevitably be executed last. 12739 12740 bool 12741 Call_expression::do_must_eval_in_order() const 12742 { 12743 return this->result_count() > 0; 12744 } 12745 12746 // Get the function and the first argument to use when calling an 12747 // interface method. 12748 12749 Expression* 12750 Call_expression::interface_method_function( 12751 Interface_field_reference_expression* interface_method, 12752 Expression** first_arg_ptr, 12753 Location location) 12754 { 12755 Expression* object = interface_method->get_underlying_object(); 12756 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); 12757 *first_arg_ptr = 12758 Expression::make_unsafe_cast(unsafe_ptr_type, object, location); 12759 return interface_method->get_function(); 12760 } 12761 12762 // Build the call expression. 12763 12764 Bexpression* 12765 Call_expression::do_get_backend(Translate_context* context) 12766 { 12767 Location location = this->location(); 12768 12769 if (this->call_ != NULL) 12770 { 12771 // If the call returns multiple results, make a new reference to 12772 // the temporary. 12773 if (this->call_temp_ != NULL) 12774 { 12775 Expression* ref = 12776 Expression::make_temporary_reference(this->call_temp_, location); 12777 return ref->get_backend(context); 12778 } 12779 12780 return this->call_; 12781 } 12782 12783 Function_type* fntype = this->get_function_type(); 12784 if (fntype == NULL) 12785 return context->backend()->error_expression(); 12786 12787 if (this->fn_->is_error_expression()) 12788 return context->backend()->error_expression(); 12789 12790 Gogo* gogo = context->gogo(); 12791 12792 Func_expression* func = this->fn_->func_expression(); 12793 Interface_field_reference_expression* interface_method = 12794 this->fn_->interface_field_reference_expression(); 12795 const bool has_closure = func != NULL && func->closure() != NULL; 12796 const bool is_interface_method = interface_method != NULL; 12797 12798 bool has_closure_arg; 12799 if (has_closure) 12800 has_closure_arg = true; 12801 else if (func != NULL) 12802 has_closure_arg = false; 12803 else if (is_interface_method) 12804 has_closure_arg = false; 12805 else 12806 has_closure_arg = true; 12807 12808 Expression* first_arg = NULL; 12809 if (!is_interface_method && fntype->is_method()) 12810 { 12811 first_arg = this->args_->front(); 12812 if (first_arg->type()->points_to() == NULL 12813 && first_arg->type()->is_direct_iface_type()) 12814 first_arg = Expression::unpack_direct_iface(first_arg, 12815 first_arg->location()); 12816 } 12817 12818 int nargs; 12819 std::vector<Bexpression*> fn_args; 12820 if (this->args_ == NULL || this->args_->empty()) 12821 { 12822 nargs = is_interface_method ? 1 : 0; 12823 if (nargs > 0) 12824 fn_args.resize(1); 12825 } 12826 else if (fntype->parameters() == NULL || fntype->parameters()->empty()) 12827 { 12828 // Passing a receiver parameter. 12829 go_assert(!is_interface_method 12830 && fntype->is_method() 12831 && this->args_->size() == 1); 12832 nargs = 1; 12833 fn_args.resize(1); 12834 fn_args[0] = first_arg->get_backend(context); 12835 } 12836 else 12837 { 12838 const Typed_identifier_list* params = fntype->parameters(); 12839 12840 nargs = this->args_->size(); 12841 int i = is_interface_method ? 1 : 0; 12842 nargs += i; 12843 fn_args.resize(nargs); 12844 12845 Typed_identifier_list::const_iterator pp = params->begin(); 12846 Expression_list::const_iterator pe = this->args_->begin(); 12847 if (!is_interface_method && fntype->is_method()) 12848 { 12849 fn_args[i] = first_arg->get_backend(context); 12850 ++pe; 12851 ++i; 12852 } 12853 for (; pe != this->args_->end(); ++pe, ++pp, ++i) 12854 { 12855 go_assert(pp != params->end()); 12856 Expression* arg = 12857 Expression::convert_for_assignment(gogo, pp->type(), *pe, 12858 location); 12859 fn_args[i] = arg->get_backend(context); 12860 } 12861 go_assert(pp == params->end()); 12862 go_assert(i == nargs); 12863 } 12864 12865 Expression* fn; 12866 Expression* closure = NULL; 12867 if (func != NULL) 12868 { 12869 Named_object* no = func->named_object(); 12870 fn = Expression::make_func_code_reference(no, location); 12871 if (has_closure) 12872 closure = func->closure(); 12873 } 12874 else if (!is_interface_method) 12875 { 12876 closure = this->fn_; 12877 12878 // The backend representation of this function type is a pointer 12879 // to a struct whose first field is the actual function to call. 12880 Type* pfntype = 12881 Type::make_pointer_type( 12882 Type::make_pointer_type(Type::make_void_type())); 12883 fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); 12884 fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location); 12885 } 12886 else 12887 { 12888 Expression* arg0; 12889 fn = this->interface_method_function(interface_method, &arg0, 12890 location); 12891 fn_args[0] = arg0->get_backend(context); 12892 } 12893 12894 Bexpression* bclosure = NULL; 12895 if (has_closure_arg) 12896 bclosure = closure->get_backend(context); 12897 else 12898 go_assert(closure == NULL); 12899 12900 Bexpression* bfn = fn->get_backend(context); 12901 12902 // When not calling a named function directly, use a type conversion 12903 // in case the type of the function is a recursive type which refers 12904 // to itself. We don't do this for an interface method because 1) 12905 // an interface method never refers to itself, so we always have a 12906 // function type here; 2) we pass an extra first argument to an 12907 // interface method, so fntype is not correct. 12908 if (func == NULL && !is_interface_method) 12909 { 12910 Btype* bft = fntype->get_backend_fntype(gogo); 12911 bfn = gogo->backend()->convert_expression(bft, bfn, location); 12912 } 12913 12914 Bfunction* bfunction = NULL; 12915 if (context->function()) 12916 bfunction = context->function()->func_value()->get_decl(); 12917 Bexpression* call = gogo->backend()->call_expression(bfunction, bfn, 12918 fn_args, bclosure, 12919 location); 12920 12921 if (this->call_temp_ != NULL) 12922 { 12923 // This case occurs when the call returns multiple results. 12924 12925 Expression* ref = Expression::make_temporary_reference(this->call_temp_, 12926 location); 12927 Bexpression* bref = ref->get_backend(context); 12928 Bstatement* bassn = gogo->backend()->assignment_statement(bfunction, 12929 bref, call, 12930 location); 12931 12932 ref = Expression::make_temporary_reference(this->call_temp_, location); 12933 this->call_ = ref->get_backend(context); 12934 12935 return gogo->backend()->compound_expression(bassn, this->call_, 12936 location); 12937 } 12938 12939 this->call_ = call; 12940 return this->call_; 12941 } 12942 12943 // The cost of inlining a call expression. 12944 12945 int 12946 Call_expression::do_inlining_cost() const 12947 { 12948 Func_expression* fn = this->fn_->func_expression(); 12949 12950 // FIXME: We don't yet support all kinds of calls. 12951 if (fn != NULL && fn->closure() != NULL) 12952 return 0x100000; 12953 if (this->fn_->interface_field_reference_expression()) 12954 return 0x100000; 12955 if (this->get_function_type()->is_method()) 12956 return 0x100000; 12957 12958 return 5; 12959 } 12960 12961 // Export a call expression. 12962 12963 void 12964 Call_expression::do_export(Export_function_body* efb) const 12965 { 12966 bool simple_call = (this->fn_->func_expression() != NULL); 12967 if (!simple_call) 12968 efb->write_c_string("("); 12969 this->fn_->export_expression(efb); 12970 if (!simple_call) 12971 efb->write_c_string(")"); 12972 this->export_arguments(efb); 12973 } 12974 12975 // Export call expression arguments. 12976 12977 void 12978 Call_expression::export_arguments(Export_function_body* efb) const 12979 { 12980 efb->write_c_string("("); 12981 if (this->args_ != NULL && !this->args_->empty()) 12982 { 12983 Expression_list::const_iterator pa = this->args_->begin(); 12984 (*pa)->export_expression(efb); 12985 for (pa++; pa != this->args_->end(); pa++) 12986 { 12987 efb->write_c_string(", "); 12988 (*pa)->export_expression(efb); 12989 } 12990 if (this->is_varargs_) 12991 efb->write_c_string("..."); 12992 } 12993 efb->write_c_string(")"); 12994 } 12995 12996 // Dump ast representation for a call expression. 12997 12998 void 12999 Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 13000 { 13001 this->fn_->dump_expression(ast_dump_context); 13002 ast_dump_context->ostream() << "("; 13003 if (args_ != NULL) 13004 ast_dump_context->dump_expression_list(this->args_); 13005 13006 ast_dump_context->ostream() << ") "; 13007 } 13008 13009 // Make a call expression. 13010 13011 Call_expression* 13012 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, 13013 Location location) 13014 { 13015 return new Call_expression(fn, args, is_varargs, location); 13016 } 13017 13018 // Class Call_result_expression. 13019 13020 // Traverse a call result. 13021 13022 int 13023 Call_result_expression::do_traverse(Traverse* traverse) 13024 { 13025 if (traverse->remember_expression(this->call_)) 13026 { 13027 // We have already traversed the call expression. 13028 return TRAVERSE_CONTINUE; 13029 } 13030 return Expression::traverse(&this->call_, traverse); 13031 } 13032 13033 // Get the type. 13034 13035 Type* 13036 Call_result_expression::do_type() 13037 { 13038 if (this->classification() == EXPRESSION_ERROR) 13039 return Type::make_error_type(); 13040 13041 // THIS->CALL_ can be replaced with a temporary reference due to 13042 // Call_expression::do_must_eval_in_order when there is an error. 13043 Call_expression* ce = this->call_->call_expression(); 13044 if (ce == NULL) 13045 { 13046 this->set_is_error(); 13047 return Type::make_error_type(); 13048 } 13049 Function_type* fntype = ce->get_function_type(); 13050 if (fntype == NULL) 13051 { 13052 if (ce->issue_error()) 13053 { 13054 if (!ce->fn()->type()->is_error()) 13055 this->report_error(_("expected function")); 13056 } 13057 this->set_is_error(); 13058 return Type::make_error_type(); 13059 } 13060 const Typed_identifier_list* results = fntype->results(); 13061 if (results == NULL || results->size() < 2) 13062 { 13063 if (ce->issue_error()) 13064 this->report_error(_("number of results does not match " 13065 "number of values")); 13066 return Type::make_error_type(); 13067 } 13068 Typed_identifier_list::const_iterator pr = results->begin(); 13069 for (unsigned int i = 0; i < this->index_; ++i) 13070 { 13071 if (pr == results->end()) 13072 break; 13073 ++pr; 13074 } 13075 if (pr == results->end()) 13076 { 13077 if (ce->issue_error()) 13078 this->report_error(_("number of results does not match " 13079 "number of values")); 13080 return Type::make_error_type(); 13081 } 13082 return pr->type(); 13083 } 13084 13085 // Check the type. Just make sure that we trigger the warning in 13086 // do_type. 13087 13088 void 13089 Call_result_expression::do_check_types(Gogo*) 13090 { 13091 this->type(); 13092 } 13093 13094 // Determine the type. We have nothing to do here, but the 0 result 13095 // needs to pass down to the caller. 13096 13097 void 13098 Call_result_expression::do_determine_type(const Type_context*) 13099 { 13100 this->call_->determine_type_no_context(); 13101 } 13102 13103 // Return the backend representation. We just refer to the temporary set by the 13104 // call expression. We don't do this at lowering time because it makes it 13105 // hard to evaluate the call at the right time. 13106 13107 Bexpression* 13108 Call_result_expression::do_get_backend(Translate_context* context) 13109 { 13110 Call_expression* ce = this->call_->call_expression(); 13111 if (ce == NULL) 13112 { 13113 go_assert(this->call_->is_error_expression()); 13114 return context->backend()->error_expression(); 13115 } 13116 Temporary_statement* ts = ce->results(); 13117 if (ts == NULL) 13118 { 13119 go_assert(saw_errors()); 13120 return context->backend()->error_expression(); 13121 } 13122 Expression* ref = Expression::make_temporary_reference(ts, this->location()); 13123 ref = Expression::make_field_reference(ref, this->index_, this->location()); 13124 return ref->get_backend(context); 13125 } 13126 13127 // Dump ast representation for a call result expression. 13128 13129 void 13130 Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 13131 const 13132 { 13133 // FIXME: Wouldn't it be better if the call is assigned to a temporary 13134 // (struct) and the fields are referenced instead. 13135 ast_dump_context->ostream() << this->index_ << "@("; 13136 ast_dump_context->dump_expression(this->call_); 13137 ast_dump_context->ostream() << ")"; 13138 } 13139 13140 // Make a reference to a single result of a call which returns 13141 // multiple results. 13142 13143 Expression* 13144 Expression::make_call_result(Call_expression* call, unsigned int index) 13145 { 13146 return new Call_result_expression(call, index); 13147 } 13148 13149 // Class Index_expression. 13150 13151 // Traversal. 13152 13153 int 13154 Index_expression::do_traverse(Traverse* traverse) 13155 { 13156 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT 13157 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT 13158 || (this->end_ != NULL 13159 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) 13160 || (this->cap_ != NULL 13161 && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) 13162 return TRAVERSE_EXIT; 13163 return TRAVERSE_CONTINUE; 13164 } 13165 13166 // Lower an index expression. This converts the generic index 13167 // expression into an array index, a string index, or a map index. 13168 13169 Expression* 13170 Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) 13171 { 13172 Location location = this->location(); 13173 Expression* left = this->left_; 13174 Expression* start = this->start_; 13175 Expression* end = this->end_; 13176 Expression* cap = this->cap_; 13177 13178 Type* type = left->type(); 13179 if (type->is_error()) 13180 { 13181 go_assert(saw_errors()); 13182 return Expression::make_error(location); 13183 } 13184 else if (left->is_type_expression()) 13185 { 13186 go_error_at(location, "attempt to index type expression"); 13187 return Expression::make_error(location); 13188 } 13189 else if (type->array_type() != NULL) 13190 return Expression::make_array_index(left, start, end, cap, location); 13191 else if (type->points_to() != NULL 13192 && type->points_to()->array_type() != NULL 13193 && !type->points_to()->is_slice_type()) 13194 { 13195 Expression* deref = 13196 Expression::make_dereference(left, NIL_CHECK_DEFAULT, location); 13197 13198 // For an ordinary index into the array, the pointer will be 13199 // dereferenced. For a slice it will not--the resulting slice 13200 // will simply reuse the pointer, which is incorrect if that 13201 // pointer is nil. 13202 if (end != NULL || cap != NULL) 13203 deref->issue_nil_check(); 13204 13205 return Expression::make_array_index(deref, start, end, cap, location); 13206 } 13207 else if (type->is_string_type()) 13208 { 13209 if (cap != NULL) 13210 { 13211 go_error_at(location, "invalid 3-index slice of string"); 13212 return Expression::make_error(location); 13213 } 13214 return Expression::make_string_index(left, start, end, location); 13215 } 13216 else if (type->map_type() != NULL) 13217 { 13218 if (end != NULL || cap != NULL) 13219 { 13220 go_error_at(location, "invalid slice of map"); 13221 return Expression::make_error(location); 13222 } 13223 return Expression::make_map_index(left, start, location); 13224 } 13225 else if (cap != NULL) 13226 { 13227 go_error_at(location, 13228 "invalid 3-index slice of object that is not a slice"); 13229 return Expression::make_error(location); 13230 } 13231 else if (end != NULL) 13232 { 13233 go_error_at(location, 13234 ("attempt to slice object that is not " 13235 "array, slice, or string")); 13236 return Expression::make_error(location); 13237 } 13238 else 13239 { 13240 go_error_at(location, 13241 ("attempt to index object that is not " 13242 "array, slice, string, or map")); 13243 return Expression::make_error(location); 13244 } 13245 } 13246 13247 // Write an indexed expression 13248 // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. 13249 13250 void 13251 Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, 13252 const Expression* expr, 13253 const Expression* start, 13254 const Expression* end, 13255 const Expression* cap) 13256 { 13257 expr->dump_expression(ast_dump_context); 13258 ast_dump_context->ostream() << "["; 13259 start->dump_expression(ast_dump_context); 13260 if (end != NULL) 13261 { 13262 ast_dump_context->ostream() << ":"; 13263 end->dump_expression(ast_dump_context); 13264 } 13265 if (cap != NULL) 13266 { 13267 ast_dump_context->ostream() << ":"; 13268 cap->dump_expression(ast_dump_context); 13269 } 13270 ast_dump_context->ostream() << "]"; 13271 } 13272 13273 // Dump ast representation for an index expression. 13274 13275 void 13276 Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 13277 const 13278 { 13279 Index_expression::dump_index_expression(ast_dump_context, this->left_, 13280 this->start_, this->end_, this->cap_); 13281 } 13282 13283 // Make an index expression. 13284 13285 Expression* 13286 Expression::make_index(Expression* left, Expression* start, Expression* end, 13287 Expression* cap, Location location) 13288 { 13289 return new Index_expression(left, start, end, cap, location); 13290 } 13291 13292 // Class Array_index_expression. 13293 13294 // Array index traversal. 13295 13296 int 13297 Array_index_expression::do_traverse(Traverse* traverse) 13298 { 13299 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) 13300 return TRAVERSE_EXIT; 13301 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) 13302 return TRAVERSE_EXIT; 13303 if (this->end_ != NULL) 13304 { 13305 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) 13306 return TRAVERSE_EXIT; 13307 } 13308 if (this->cap_ != NULL) 13309 { 13310 if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) 13311 return TRAVERSE_EXIT; 13312 } 13313 return TRAVERSE_CONTINUE; 13314 } 13315 13316 // Return the type of an array index. 13317 13318 Type* 13319 Array_index_expression::do_type() 13320 { 13321 if (this->type_ == NULL) 13322 { 13323 Array_type* type = this->array_->type()->array_type(); 13324 if (type == NULL) 13325 this->type_ = Type::make_error_type(); 13326 else if (this->end_ == NULL) 13327 this->type_ = type->element_type(); 13328 else if (type->is_slice_type()) 13329 { 13330 // A slice of a slice has the same type as the original 13331 // slice. 13332 this->type_ = this->array_->type()->deref(); 13333 } 13334 else 13335 { 13336 // A slice of an array is a slice. 13337 this->type_ = Type::make_array_type(type->element_type(), NULL); 13338 } 13339 } 13340 return this->type_; 13341 } 13342 13343 // Set the type of an array index. 13344 13345 void 13346 Array_index_expression::do_determine_type(const Type_context*) 13347 { 13348 this->array_->determine_type_no_context(); 13349 13350 Type_context index_context(Type::lookup_integer_type("int"), false); 13351 this->start_->determine_type(&index_context); 13352 if (this->end_ != NULL) 13353 this->end_->determine_type(&index_context); 13354 if (this->cap_ != NULL) 13355 this->cap_->determine_type(&index_context); 13356 } 13357 13358 // Check types of an array index. 13359 13360 void 13361 Array_index_expression::do_check_types(Gogo*) 13362 { 13363 Numeric_constant nc; 13364 unsigned long v; 13365 if (this->start_->type()->integer_type() == NULL 13366 && !this->start_->type()->is_error() 13367 && (!this->start_->type()->is_abstract() 13368 || !this->start_->numeric_constant_value(&nc) 13369 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 13370 this->report_error(_("index must be integer")); 13371 if (this->end_ != NULL 13372 && this->end_->type()->integer_type() == NULL 13373 && !this->end_->type()->is_error() 13374 && !this->end_->is_nil_expression() 13375 && !this->end_->is_error_expression() 13376 && (!this->end_->type()->is_abstract() 13377 || !this->end_->numeric_constant_value(&nc) 13378 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 13379 this->report_error(_("slice end must be integer")); 13380 if (this->cap_ != NULL 13381 && this->cap_->type()->integer_type() == NULL 13382 && !this->cap_->type()->is_error() 13383 && !this->cap_->is_nil_expression() 13384 && !this->cap_->is_error_expression() 13385 && (!this->cap_->type()->is_abstract() 13386 || !this->cap_->numeric_constant_value(&nc) 13387 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 13388 this->report_error(_("slice capacity must be integer")); 13389 13390 Array_type* array_type = this->array_->type()->array_type(); 13391 if (array_type == NULL) 13392 { 13393 go_assert(this->array_->type()->is_error()); 13394 return; 13395 } 13396 13397 unsigned int int_bits = 13398 Type::lookup_integer_type("int")->integer_type()->bits(); 13399 13400 Numeric_constant lvalnc; 13401 mpz_t lval; 13402 bool lval_valid = (array_type->length() != NULL 13403 && array_type->length()->numeric_constant_value(&lvalnc) 13404 && lvalnc.to_int(&lval)); 13405 Numeric_constant inc; 13406 mpz_t ival; 13407 bool ival_valid = false; 13408 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) 13409 { 13410 ival_valid = true; 13411 if (mpz_sgn(ival) < 0 13412 || mpz_sizeinbase(ival, 2) >= int_bits 13413 || (lval_valid 13414 && (this->end_ == NULL 13415 ? mpz_cmp(ival, lval) >= 0 13416 : mpz_cmp(ival, lval) > 0))) 13417 { 13418 go_error_at(this->start_->location(), "array index out of bounds"); 13419 this->set_is_error(); 13420 } 13421 } 13422 if (this->end_ != NULL && !this->end_->is_nil_expression()) 13423 { 13424 Numeric_constant enc; 13425 mpz_t eval; 13426 bool eval_valid = false; 13427 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) 13428 { 13429 eval_valid = true; 13430 if (mpz_sgn(eval) < 0 13431 || mpz_sizeinbase(eval, 2) >= int_bits 13432 || (lval_valid && mpz_cmp(eval, lval) > 0)) 13433 { 13434 go_error_at(this->end_->location(), "array index out of bounds"); 13435 this->set_is_error(); 13436 } 13437 else if (ival_valid && mpz_cmp(ival, eval) > 0) 13438 this->report_error(_("inverted slice range")); 13439 } 13440 13441 Numeric_constant cnc; 13442 mpz_t cval; 13443 if (this->cap_ != NULL 13444 && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) 13445 { 13446 if (mpz_sgn(cval) < 0 13447 || mpz_sizeinbase(cval, 2) >= int_bits 13448 || (lval_valid && mpz_cmp(cval, lval) > 0)) 13449 { 13450 go_error_at(this->cap_->location(), "array index out of bounds"); 13451 this->set_is_error(); 13452 } 13453 else if (ival_valid && mpz_cmp(ival, cval) > 0) 13454 { 13455 go_error_at(this->cap_->location(), 13456 "invalid slice index: capacity less than start"); 13457 this->set_is_error(); 13458 } 13459 else if (eval_valid && mpz_cmp(eval, cval) > 0) 13460 { 13461 go_error_at(this->cap_->location(), 13462 "invalid slice index: capacity less than length"); 13463 this->set_is_error(); 13464 } 13465 mpz_clear(cval); 13466 } 13467 13468 if (eval_valid) 13469 mpz_clear(eval); 13470 } 13471 if (ival_valid) 13472 mpz_clear(ival); 13473 if (lval_valid) 13474 mpz_clear(lval); 13475 13476 // A slice of an array requires an addressable array. A slice of a 13477 // slice is always possible. 13478 if (this->end_ != NULL && !array_type->is_slice_type()) 13479 { 13480 if (!this->array_->is_addressable()) 13481 this->report_error(_("slice of unaddressable value")); 13482 else 13483 // Set the array address taken but not escape. The escape 13484 // analysis will make it escape to heap when needed. 13485 this->array_->address_taken(false); 13486 } 13487 } 13488 13489 // The subexpressions of an array index must be evaluated in order. 13490 // If this is indexing into an array, rather than a slice, then only 13491 // the index should be evaluated. Since this is called for values on 13492 // the left hand side of an assigment, evaluating the array, meaning 13493 // copying the array, will cause a different array to be modified. 13494 13495 bool 13496 Array_index_expression::do_must_eval_subexpressions_in_order( 13497 int* skip) const 13498 { 13499 *skip = this->array_->type()->is_slice_type() ? 0 : 1; 13500 return true; 13501 } 13502 13503 // Flatten array indexing: add temporary variables and bounds checks. 13504 13505 Expression* 13506 Array_index_expression::do_flatten(Gogo* gogo, Named_object*, 13507 Statement_inserter* inserter) 13508 { 13509 if (this->is_flattened_) 13510 return this; 13511 this->is_flattened_ = true; 13512 13513 Location loc = this->location(); 13514 13515 if (this->is_error_expression()) 13516 return Expression::make_error(loc); 13517 13518 Expression* array = this->array_; 13519 Expression* start = this->start_; 13520 Expression* end = this->end_; 13521 Expression* cap = this->cap_; 13522 if (array->is_error_expression() 13523 || array->type()->is_error_type() 13524 || start->is_error_expression() 13525 || start->type()->is_error_type() 13526 || (end != NULL 13527 && (end->is_error_expression() || end->type()->is_error_type())) 13528 || (cap != NULL 13529 && (cap->is_error_expression() || cap->type()->is_error_type()))) 13530 { 13531 go_assert(saw_errors()); 13532 return Expression::make_error(loc); 13533 } 13534 13535 Array_type* array_type = this->array_->type()->array_type(); 13536 if (array_type == NULL) 13537 { 13538 go_assert(saw_errors()); 13539 return Expression::make_error(loc); 13540 } 13541 13542 Temporary_statement* temp; 13543 if (array_type->is_slice_type() && !array->is_multi_eval_safe()) 13544 { 13545 temp = Statement::make_temporary(NULL, array, loc); 13546 inserter->insert(temp); 13547 this->array_ = Expression::make_temporary_reference(temp, loc); 13548 array = this->array_; 13549 } 13550 if (!start->is_multi_eval_safe()) 13551 { 13552 temp = Statement::make_temporary(NULL, start, loc); 13553 inserter->insert(temp); 13554 this->start_ = Expression::make_temporary_reference(temp, loc); 13555 start = this->start_; 13556 } 13557 if (end != NULL 13558 && !end->is_nil_expression() 13559 && !end->is_multi_eval_safe()) 13560 { 13561 temp = Statement::make_temporary(NULL, end, loc); 13562 inserter->insert(temp); 13563 this->end_ = Expression::make_temporary_reference(temp, loc); 13564 end = this->end_; 13565 } 13566 if (cap != NULL && !cap->is_multi_eval_safe()) 13567 { 13568 temp = Statement::make_temporary(NULL, cap, loc); 13569 inserter->insert(temp); 13570 this->cap_ = Expression::make_temporary_reference(temp, loc); 13571 cap = this->cap_; 13572 } 13573 13574 if (!this->needs_bounds_check_) 13575 return this; 13576 13577 Expression* len; 13578 if (!array_type->is_slice_type()) 13579 { 13580 len = array_type->get_length(gogo, this->array_); 13581 go_assert(len->is_constant()); 13582 } 13583 else 13584 { 13585 len = array_type->get_length(gogo, this->array_->copy()); 13586 temp = Statement::make_temporary(NULL, len, loc); 13587 inserter->insert(temp); 13588 len = Expression::make_temporary_reference(temp, loc); 13589 } 13590 13591 Expression* scap = NULL; 13592 if (array_type->is_slice_type()) 13593 { 13594 scap = array_type->get_capacity(gogo, this->array_->copy()); 13595 temp = Statement::make_temporary(NULL, scap, loc); 13596 inserter->insert(temp); 13597 scap = Expression::make_temporary_reference(temp, loc); 13598 } 13599 13600 // The order of bounds checks here matches the order used by the gc 13601 // compiler, as tested by issue30116[u].go. 13602 13603 if (cap != NULL) 13604 { 13605 if (array_type->is_slice_type()) 13606 Expression::check_bounds(cap, OPERATOR_LE, scap, 13607 Runtime::PANIC_SLICE3_ACAP, 13608 Runtime::PANIC_SLICE3_ACAP_U, 13609 Runtime::PANIC_EXTEND_SLICE3_ACAP, 13610 Runtime::PANIC_EXTEND_SLICE3_ACAP_U, 13611 inserter, loc); 13612 else 13613 Expression::check_bounds(cap, OPERATOR_LE, len, 13614 Runtime::PANIC_SLICE3_ALEN, 13615 Runtime::PANIC_SLICE3_ALEN_U, 13616 Runtime::PANIC_EXTEND_SLICE3_ALEN, 13617 Runtime::PANIC_EXTEND_SLICE3_ALEN_U, 13618 inserter, loc); 13619 13620 Expression* start_bound = cap; 13621 if (end != NULL && !end->is_nil_expression()) 13622 { 13623 Expression::check_bounds(end, OPERATOR_LE, cap, 13624 Runtime::PANIC_SLICE3_B, 13625 Runtime::PANIC_SLICE3_B_U, 13626 Runtime::PANIC_EXTEND_SLICE3_B, 13627 Runtime::PANIC_EXTEND_SLICE3_B_U, 13628 inserter, loc); 13629 start_bound = end; 13630 } 13631 13632 Expression::check_bounds(start, OPERATOR_LE, start_bound, 13633 Runtime::PANIC_SLICE3_C, 13634 Runtime::PANIC_SLICE3_C_U, 13635 Runtime::PANIC_EXTEND_SLICE3_C, 13636 Runtime::PANIC_EXTEND_SLICE3_C_U, 13637 inserter, loc); 13638 } 13639 else if (end != NULL && !end->is_nil_expression()) 13640 { 13641 if (array_type->is_slice_type()) 13642 Expression::check_bounds(end, OPERATOR_LE, scap, 13643 Runtime::PANIC_SLICE_ACAP, 13644 Runtime::PANIC_SLICE_ACAP_U, 13645 Runtime::PANIC_EXTEND_SLICE_ACAP, 13646 Runtime::PANIC_EXTEND_SLICE_ACAP_U, 13647 inserter, loc); 13648 else 13649 Expression::check_bounds(end, OPERATOR_LE, len, 13650 Runtime::PANIC_SLICE_ALEN, 13651 Runtime::PANIC_SLICE_ALEN_U, 13652 Runtime::PANIC_EXTEND_SLICE_ALEN, 13653 Runtime::PANIC_EXTEND_SLICE_ALEN_U, 13654 inserter, loc); 13655 13656 Expression::check_bounds(start, OPERATOR_LE, end, 13657 Runtime::PANIC_SLICE_B, 13658 Runtime::PANIC_SLICE_B_U, 13659 Runtime::PANIC_EXTEND_SLICE_B, 13660 Runtime::PANIC_EXTEND_SLICE_B_U, 13661 inserter, loc); 13662 } 13663 else if (end != NULL) 13664 { 13665 Expression* start_bound; 13666 if (array_type->is_slice_type()) 13667 start_bound = scap; 13668 else 13669 start_bound = len; 13670 Expression::check_bounds(start, OPERATOR_LE, start_bound, 13671 Runtime::PANIC_SLICE_B, 13672 Runtime::PANIC_SLICE_B_U, 13673 Runtime::PANIC_EXTEND_SLICE_B, 13674 Runtime::PANIC_EXTEND_SLICE_B_U, 13675 inserter, loc); 13676 } 13677 else 13678 Expression::check_bounds(start, OPERATOR_LT, len, 13679 Runtime::PANIC_INDEX, 13680 Runtime::PANIC_INDEX_U, 13681 Runtime::PANIC_EXTEND_INDEX, 13682 Runtime::PANIC_EXTEND_INDEX_U, 13683 inserter, loc); 13684 13685 return this; 13686 } 13687 13688 // Return whether this expression is addressable. 13689 13690 bool 13691 Array_index_expression::do_is_addressable() const 13692 { 13693 // A slice expression is not addressable. 13694 if (this->end_ != NULL) 13695 return false; 13696 13697 // An index into a slice is addressable. 13698 if (this->array_->type()->is_slice_type()) 13699 return true; 13700 13701 // An index into an array is addressable if the array is 13702 // addressable. 13703 return this->array_->is_addressable(); 13704 } 13705 13706 void 13707 Array_index_expression::do_address_taken(bool escapes) 13708 { 13709 // In &x[0], if x is a slice, then x's address is not taken. 13710 if (!this->array_->type()->is_slice_type()) 13711 this->array_->address_taken(escapes); 13712 } 13713 13714 // Get the backend representation for an array index. 13715 13716 Bexpression* 13717 Array_index_expression::do_get_backend(Translate_context* context) 13718 { 13719 Array_type* array_type = this->array_->type()->array_type(); 13720 if (array_type == NULL) 13721 { 13722 go_assert(this->array_->type()->is_error()); 13723 return context->backend()->error_expression(); 13724 } 13725 go_assert(!array_type->is_slice_type() 13726 || this->array_->is_multi_eval_safe()); 13727 13728 Location loc = this->location(); 13729 Gogo* gogo = context->gogo(); 13730 13731 Type* int_type = Type::lookup_integer_type("int"); 13732 Btype* int_btype = int_type->get_backend(gogo); 13733 13734 // Convert the length and capacity to "int". FIXME: Do we need to 13735 // do this? 13736 Bexpression* length = NULL; 13737 if (this->end_ == NULL || this->end_->is_nil_expression()) 13738 { 13739 Expression* len = array_type->get_length(gogo, this->array_); 13740 length = len->get_backend(context); 13741 length = gogo->backend()->convert_expression(int_btype, length, loc); 13742 } 13743 13744 Bexpression* capacity = NULL; 13745 if (this->end_ != NULL) 13746 { 13747 Expression* cap = array_type->get_capacity(gogo, this->array_); 13748 capacity = cap->get_backend(context); 13749 capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); 13750 } 13751 13752 Bexpression* cap_arg = capacity; 13753 if (this->cap_ != NULL) 13754 { 13755 cap_arg = this->cap_->get_backend(context); 13756 cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); 13757 } 13758 13759 if (length == NULL) 13760 length = cap_arg; 13761 13762 if (this->start_->type()->integer_type() == NULL 13763 && !Type::are_convertible(int_type, this->start_->type(), NULL)) 13764 { 13765 go_assert(saw_errors()); 13766 return context->backend()->error_expression(); 13767 } 13768 13769 Bexpression* start = this->start_->get_backend(context); 13770 start = gogo->backend()->convert_expression(int_btype, start, loc); 13771 13772 Bfunction* bfn = context->function()->func_value()->get_decl(); 13773 if (this->end_ == NULL) 13774 { 13775 // Simple array indexing. 13776 Bexpression* ret; 13777 if (!array_type->is_slice_type()) 13778 { 13779 Bexpression* array = this->array_->get_backend(context); 13780 ret = gogo->backend()->array_index_expression(array, start, loc); 13781 } 13782 else 13783 { 13784 Expression* valptr = array_type->get_value_pointer(gogo, 13785 this->array_); 13786 Bexpression* ptr = valptr->get_backend(context); 13787 ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); 13788 13789 Type* ele_type = this->array_->type()->array_type()->element_type(); 13790 Btype* ele_btype = ele_type->get_backend(gogo); 13791 ret = gogo->backend()->indirect_expression(ele_btype, ptr, false, 13792 loc); 13793 } 13794 return ret; 13795 } 13796 13797 // Slice expression. 13798 13799 Bexpression* end; 13800 if (this->end_->is_nil_expression()) 13801 end = length; 13802 else 13803 { 13804 end = this->end_->get_backend(context); 13805 end = gogo->backend()->convert_expression(int_btype, end, loc); 13806 } 13807 13808 Bexpression* result_length = 13809 gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); 13810 13811 Bexpression* result_capacity = 13812 gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); 13813 13814 // If the new capacity is zero, don't change val. Otherwise we can 13815 // get a pointer to the next object in memory, keeping it live 13816 // unnecessarily. When the capacity is zero, the actual pointer 13817 // value doesn't matter. 13818 Bexpression* zero = 13819 Expression::make_integer_ul(0, int_type, loc)->get_backend(context); 13820 Bexpression* cond = 13821 gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero, 13822 loc); 13823 Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype, 13824 cond, zero, 13825 start, loc); 13826 Expression* valptr = array_type->get_value_pointer(gogo, this->array_); 13827 Bexpression* val = valptr->get_backend(context); 13828 val = gogo->backend()->pointer_offset_expression(val, offset, loc); 13829 13830 Btype* struct_btype = this->type()->get_backend(gogo); 13831 std::vector<Bexpression*> init; 13832 init.push_back(val); 13833 init.push_back(result_length); 13834 init.push_back(result_capacity); 13835 13836 return gogo->backend()->constructor_expression(struct_btype, init, loc); 13837 } 13838 13839 // Export an array index expression. 13840 13841 void 13842 Array_index_expression::do_export(Export_function_body* efb) const 13843 { 13844 efb->write_c_string("("); 13845 this->array_->export_expression(efb); 13846 efb->write_c_string(")["); 13847 13848 Type* old_context = efb->type_context(); 13849 efb->set_type_context(Type::lookup_integer_type("int")); 13850 13851 this->start_->export_expression(efb); 13852 if (this->end_ == NULL) 13853 go_assert(this->cap_ == NULL); 13854 else 13855 { 13856 efb->write_c_string(":"); 13857 if (!this->end_->is_nil_expression()) 13858 this->end_->export_expression(efb); 13859 if (this->cap_ != NULL) 13860 { 13861 efb->write_c_string(":"); 13862 this->cap_->export_expression(efb); 13863 } 13864 } 13865 13866 efb->set_type_context(old_context); 13867 13868 efb->write_c_string("]"); 13869 } 13870 13871 // Dump ast representation for an array index expression. 13872 13873 void 13874 Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 13875 const 13876 { 13877 Index_expression::dump_index_expression(ast_dump_context, this->array_, 13878 this->start_, this->end_, this->cap_); 13879 } 13880 13881 // Make an array index expression. END and CAP may be NULL. 13882 13883 Expression* 13884 Expression::make_array_index(Expression* array, Expression* start, 13885 Expression* end, Expression* cap, 13886 Location location) 13887 { 13888 return new Array_index_expression(array, start, end, cap, location); 13889 } 13890 13891 // Class String_index_expression. 13892 13893 // String index traversal. 13894 13895 int 13896 String_index_expression::do_traverse(Traverse* traverse) 13897 { 13898 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) 13899 return TRAVERSE_EXIT; 13900 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) 13901 return TRAVERSE_EXIT; 13902 if (this->end_ != NULL) 13903 { 13904 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) 13905 return TRAVERSE_EXIT; 13906 } 13907 return TRAVERSE_CONTINUE; 13908 } 13909 13910 Expression* 13911 String_index_expression::do_flatten(Gogo*, Named_object*, 13912 Statement_inserter* inserter) 13913 { 13914 if (this->is_flattened_) 13915 return this; 13916 this->is_flattened_ = true; 13917 13918 Location loc = this->location(); 13919 13920 if (this->is_error_expression()) 13921 return Expression::make_error(loc); 13922 13923 Expression* string = this->string_; 13924 Expression* start = this->start_; 13925 Expression* end = this->end_; 13926 if (string->is_error_expression() 13927 || string->type()->is_error_type() 13928 || start->is_error_expression() 13929 || start->type()->is_error_type() 13930 || (end != NULL 13931 && (end->is_error_expression() || end->type()->is_error_type()))) 13932 { 13933 go_assert(saw_errors()); 13934 return Expression::make_error(loc); 13935 } 13936 13937 Temporary_statement* temp; 13938 if (!string->is_multi_eval_safe()) 13939 { 13940 temp = Statement::make_temporary(NULL, string, loc); 13941 inserter->insert(temp); 13942 this->string_ = Expression::make_temporary_reference(temp, loc); 13943 string = this->string_; 13944 } 13945 if (!start->is_multi_eval_safe()) 13946 { 13947 temp = Statement::make_temporary(NULL, start, loc); 13948 inserter->insert(temp); 13949 this->start_ = Expression::make_temporary_reference(temp, loc); 13950 start = this->start_; 13951 } 13952 if (end != NULL 13953 && !end->is_nil_expression() 13954 && !end->is_multi_eval_safe()) 13955 { 13956 temp = Statement::make_temporary(NULL, end, loc); 13957 inserter->insert(temp); 13958 this->end_ = Expression::make_temporary_reference(temp, loc); 13959 end = this->end_; 13960 } 13961 13962 Expression* len = Expression::make_string_info(string->copy(), 13963 STRING_INFO_LENGTH, loc); 13964 temp = Statement::make_temporary(NULL, len, loc); 13965 inserter->insert(temp); 13966 len = Expression::make_temporary_reference(temp, loc); 13967 13968 // The order of bounds checks here matches the order used by the gc 13969 // compiler, as tested by issue30116[u].go. 13970 13971 if (end != NULL && !end->is_nil_expression()) 13972 { 13973 Expression::check_bounds(end, OPERATOR_LE, len, 13974 Runtime::PANIC_SLICE_ALEN, 13975 Runtime::PANIC_SLICE_ALEN_U, 13976 Runtime::PANIC_EXTEND_SLICE_ALEN, 13977 Runtime::PANIC_EXTEND_SLICE_ALEN_U, 13978 inserter, loc); 13979 Expression::check_bounds(start, OPERATOR_LE, end, 13980 Runtime::PANIC_SLICE_B, 13981 Runtime::PANIC_SLICE_B_U, 13982 Runtime::PANIC_EXTEND_SLICE_B, 13983 Runtime::PANIC_EXTEND_SLICE_B_U, 13984 inserter, loc); 13985 } 13986 else if (end != NULL) 13987 Expression::check_bounds(start, OPERATOR_LE, len, 13988 Runtime::PANIC_SLICE_B, 13989 Runtime::PANIC_SLICE_B_U, 13990 Runtime::PANIC_EXTEND_SLICE_B, 13991 Runtime::PANIC_EXTEND_SLICE_B_U, 13992 inserter, loc); 13993 else 13994 Expression::check_bounds(start, OPERATOR_LT, len, 13995 Runtime::PANIC_INDEX, 13996 Runtime::PANIC_INDEX_U, 13997 Runtime::PANIC_EXTEND_INDEX, 13998 Runtime::PANIC_EXTEND_INDEX_U, 13999 inserter, loc); 14000 14001 return this; 14002 } 14003 14004 // Return the type of a string index. 14005 14006 Type* 14007 String_index_expression::do_type() 14008 { 14009 if (this->end_ == NULL) 14010 return Type::lookup_integer_type("byte"); 14011 else 14012 return this->string_->type(); 14013 } 14014 14015 // Determine the type of a string index. 14016 14017 void 14018 String_index_expression::do_determine_type(const Type_context*) 14019 { 14020 this->string_->determine_type_no_context(); 14021 14022 Type_context index_context(Type::lookup_integer_type("int"), false); 14023 this->start_->determine_type(&index_context); 14024 if (this->end_ != NULL) 14025 this->end_->determine_type(&index_context); 14026 } 14027 14028 // Check types of a string index. 14029 14030 void 14031 String_index_expression::do_check_types(Gogo*) 14032 { 14033 Numeric_constant nc; 14034 unsigned long v; 14035 if (this->start_->type()->integer_type() == NULL 14036 && !this->start_->type()->is_error() 14037 && (!this->start_->type()->is_abstract() 14038 || !this->start_->numeric_constant_value(&nc) 14039 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 14040 this->report_error(_("index must be integer")); 14041 if (this->end_ != NULL 14042 && this->end_->type()->integer_type() == NULL 14043 && !this->end_->type()->is_error() 14044 && !this->end_->is_nil_expression() 14045 && !this->end_->is_error_expression() 14046 && (!this->end_->type()->is_abstract() 14047 || !this->end_->numeric_constant_value(&nc) 14048 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) 14049 this->report_error(_("slice end must be integer")); 14050 14051 std::string sval; 14052 bool sval_valid = this->string_->string_constant_value(&sval); 14053 14054 Numeric_constant inc; 14055 mpz_t ival; 14056 bool ival_valid = false; 14057 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) 14058 { 14059 ival_valid = true; 14060 if (mpz_sgn(ival) < 0 14061 || (sval_valid 14062 && (this->end_ == NULL 14063 ? mpz_cmp_ui(ival, sval.length()) >= 0 14064 : mpz_cmp_ui(ival, sval.length()) > 0))) 14065 { 14066 go_error_at(this->start_->location(), "string index out of bounds"); 14067 this->set_is_error(); 14068 } 14069 } 14070 if (this->end_ != NULL && !this->end_->is_nil_expression()) 14071 { 14072 Numeric_constant enc; 14073 mpz_t eval; 14074 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) 14075 { 14076 if (mpz_sgn(eval) < 0 14077 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) 14078 { 14079 go_error_at(this->end_->location(), "string index out of bounds"); 14080 this->set_is_error(); 14081 } 14082 else if (ival_valid && mpz_cmp(ival, eval) > 0) 14083 this->report_error(_("inverted slice range")); 14084 mpz_clear(eval); 14085 } 14086 } 14087 if (ival_valid) 14088 mpz_clear(ival); 14089 } 14090 14091 // Get the backend representation for a string index. 14092 14093 Bexpression* 14094 String_index_expression::do_get_backend(Translate_context* context) 14095 { 14096 Location loc = this->location(); 14097 Gogo* gogo = context->gogo(); 14098 14099 Type* int_type = Type::lookup_integer_type("int"); 14100 14101 // It is possible that an error occurred earlier because the start index 14102 // cannot be represented as an integer type. In this case, we shouldn't 14103 // try casting the starting index into an integer since 14104 // Type_conversion_expression will fail to get the backend representation. 14105 // FIXME. 14106 if (this->start_->type()->integer_type() == NULL 14107 && !Type::are_convertible(int_type, this->start_->type(), NULL)) 14108 { 14109 go_assert(saw_errors()); 14110 return context->backend()->error_expression(); 14111 } 14112 14113 go_assert(this->string_->is_multi_eval_safe()); 14114 go_assert(this->start_->is_multi_eval_safe()); 14115 14116 Expression* start = Expression::make_cast(int_type, this->start_, loc); 14117 Bfunction* bfn = context->function()->func_value()->get_decl(); 14118 14119 Expression* length = 14120 Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); 14121 Expression* bytes = 14122 Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); 14123 14124 Bexpression* bstart = start->get_backend(context); 14125 Bexpression* ptr = bytes->get_backend(context); 14126 14127 if (this->end_ == NULL) 14128 { 14129 ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); 14130 Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); 14131 return gogo->backend()->indirect_expression(ubtype, ptr, false, loc); 14132 } 14133 14134 Expression* end = NULL; 14135 if (this->end_->is_nil_expression()) 14136 end = length; 14137 else 14138 { 14139 go_assert(this->end_->is_multi_eval_safe()); 14140 end = Expression::make_cast(int_type, this->end_, loc); 14141 } 14142 14143 end = end->copy(); 14144 Bexpression* bend = end->get_backend(context); 14145 Bexpression* new_length = 14146 gogo->backend()->binary_expression(OPERATOR_MINUS, bend, bstart, loc); 14147 14148 // If the new length is zero, don't change pointer. Otherwise we can 14149 // get a pointer to the next object in memory, keeping it live 14150 // unnecessarily. When the length is zero, the actual pointer 14151 // value doesn't matter. 14152 Btype* int_btype = int_type->get_backend(gogo); 14153 Bexpression* zero = 14154 Expression::make_integer_ul(0, int_type, loc)->get_backend(context); 14155 Bexpression* cond = 14156 gogo->backend()->binary_expression(OPERATOR_EQEQ, new_length, zero, 14157 loc); 14158 Bexpression* offset = 14159 gogo->backend()->conditional_expression(bfn, int_btype, cond, zero, 14160 bstart, loc); 14161 14162 ptr = gogo->backend()->pointer_offset_expression(ptr, offset, loc); 14163 14164 Btype* str_btype = this->type()->get_backend(gogo); 14165 std::vector<Bexpression*> init; 14166 init.push_back(ptr); 14167 init.push_back(new_length); 14168 return gogo->backend()->constructor_expression(str_btype, init, loc); 14169 } 14170 14171 // Export a string index expression. 14172 14173 void 14174 String_index_expression::do_export(Export_function_body* efb) const 14175 { 14176 efb->write_c_string("("); 14177 this->string_->export_expression(efb); 14178 efb->write_c_string(")["); 14179 14180 Type* old_context = efb->type_context(); 14181 efb->set_type_context(Type::lookup_integer_type("int")); 14182 14183 this->start_->export_expression(efb); 14184 if (this->end_ != NULL) 14185 { 14186 efb->write_c_string(":"); 14187 if (!this->end_->is_nil_expression()) 14188 this->end_->export_expression(efb); 14189 } 14190 14191 efb->set_type_context(old_context); 14192 14193 efb->write_c_string("]"); 14194 } 14195 14196 // Dump ast representation for a string index expression. 14197 14198 void 14199 String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 14200 const 14201 { 14202 Index_expression::dump_index_expression(ast_dump_context, this->string_, 14203 this->start_, this->end_, NULL); 14204 } 14205 14206 // Make a string index expression. END may be NULL. 14207 14208 Expression* 14209 Expression::make_string_index(Expression* string, Expression* start, 14210 Expression* end, Location location) 14211 { 14212 return new String_index_expression(string, start, end, location); 14213 } 14214 14215 // Class Map_index. 14216 14217 // Get the type of the map. 14218 14219 Map_type* 14220 Map_index_expression::get_map_type() const 14221 { 14222 Map_type* mt = this->map_->type()->map_type(); 14223 if (mt == NULL) 14224 go_assert(saw_errors()); 14225 return mt; 14226 } 14227 14228 // Map index traversal. 14229 14230 int 14231 Map_index_expression::do_traverse(Traverse* traverse) 14232 { 14233 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) 14234 return TRAVERSE_EXIT; 14235 return Expression::traverse(&this->index_, traverse); 14236 } 14237 14238 // We need to pass in a pointer to the key, so flatten the index into a 14239 // temporary variable if it isn't already. The value pointer will be 14240 // dereferenced and checked for nil, so flatten into a temporary to avoid 14241 // recomputation. 14242 14243 Expression* 14244 Map_index_expression::do_flatten(Gogo* gogo, Named_object*, 14245 Statement_inserter* inserter) 14246 { 14247 Location loc = this->location(); 14248 Map_type* mt = this->get_map_type(); 14249 if (this->index()->is_error_expression() 14250 || this->index()->type()->is_error_type() 14251 || mt->is_error_type()) 14252 { 14253 go_assert(saw_errors()); 14254 return Expression::make_error(loc); 14255 } 14256 14257 // Avoid copy for string([]byte) conversions used in map keys. 14258 // mapaccess doesn't keep the reference, so this is safe. 14259 Type_conversion_expression* ce = this->index_->conversion_expression(); 14260 if (ce != NULL && ce->type()->is_string_type() 14261 && ce->expr()->type()->is_slice_type()) 14262 ce->set_no_copy(true); 14263 14264 if (!Type::are_identical(mt->key_type(), this->index_->type(), 14265 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 14266 NULL)) 14267 { 14268 if (this->index_->type()->interface_type() != NULL 14269 && !this->index_->is_multi_eval_safe()) 14270 { 14271 Temporary_statement* temp = 14272 Statement::make_temporary(NULL, this->index_, loc); 14273 inserter->insert(temp); 14274 this->index_ = Expression::make_temporary_reference(temp, loc); 14275 } 14276 this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), 14277 this->index_, loc); 14278 } 14279 14280 if (!this->index_->is_multi_eval_safe()) 14281 { 14282 Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, 14283 loc); 14284 inserter->insert(temp); 14285 this->index_ = Expression::make_temporary_reference(temp, loc); 14286 } 14287 14288 if (this->value_pointer_ == NULL) 14289 this->get_value_pointer(gogo); 14290 if (this->value_pointer_->is_error_expression() 14291 || this->value_pointer_->type()->is_error_type()) 14292 return Expression::make_error(loc); 14293 if (!this->value_pointer_->is_multi_eval_safe()) 14294 { 14295 Temporary_statement* temp = 14296 Statement::make_temporary(NULL, this->value_pointer_, loc); 14297 inserter->insert(temp); 14298 this->value_pointer_ = Expression::make_temporary_reference(temp, loc); 14299 } 14300 14301 return this; 14302 } 14303 14304 // Return the type of a map index. 14305 14306 Type* 14307 Map_index_expression::do_type() 14308 { 14309 Map_type* mt = this->get_map_type(); 14310 if (mt == NULL) 14311 return Type::make_error_type(); 14312 return mt->val_type(); 14313 } 14314 14315 // Fix the type of a map index. 14316 14317 void 14318 Map_index_expression::do_determine_type(const Type_context*) 14319 { 14320 this->map_->determine_type_no_context(); 14321 Map_type* mt = this->get_map_type(); 14322 Type* key_type = mt == NULL ? NULL : mt->key_type(); 14323 Type_context subcontext(key_type, false); 14324 this->index_->determine_type(&subcontext); 14325 } 14326 14327 // Check types of a map index. 14328 14329 void 14330 Map_index_expression::do_check_types(Gogo*) 14331 { 14332 std::string reason; 14333 Map_type* mt = this->get_map_type(); 14334 if (mt == NULL) 14335 return; 14336 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) 14337 { 14338 if (reason.empty()) 14339 this->report_error(_("incompatible type for map index")); 14340 else 14341 { 14342 go_error_at(this->location(), "incompatible type for map index (%s)", 14343 reason.c_str()); 14344 this->set_is_error(); 14345 } 14346 } 14347 } 14348 14349 // Add explicit type conversions. 14350 14351 void 14352 Map_index_expression::do_add_conversions() 14353 { 14354 Map_type* mt = this->get_map_type(); 14355 if (mt == NULL) 14356 return; 14357 Type* lt = mt->key_type(); 14358 Type* rt = this->index_->type(); 14359 if (!Type::are_identical(lt, rt, 0, NULL) 14360 && lt->interface_type() != NULL) 14361 this->index_ = Expression::make_cast(lt, this->index_, this->location()); 14362 } 14363 14364 // Get the backend representation for a map index. 14365 14366 Bexpression* 14367 Map_index_expression::do_get_backend(Translate_context* context) 14368 { 14369 Map_type* type = this->get_map_type(); 14370 if (type == NULL) 14371 { 14372 go_assert(saw_errors()); 14373 return context->backend()->error_expression(); 14374 } 14375 14376 go_assert(this->value_pointer_ != NULL 14377 && this->value_pointer_->is_multi_eval_safe()); 14378 14379 Expression* val = Expression::make_dereference(this->value_pointer_, 14380 NIL_CHECK_NOT_NEEDED, 14381 this->location()); 14382 return val->get_backend(context); 14383 } 14384 14385 // Get an expression for the map index. This returns an expression 14386 // that evaluates to a pointer to a value. If the key is not in the 14387 // map, the pointer will point to a zero value. 14388 14389 Expression* 14390 Map_index_expression::get_value_pointer(Gogo* gogo) 14391 { 14392 if (this->value_pointer_ == NULL) 14393 { 14394 Map_type* type = this->get_map_type(); 14395 if (type == NULL) 14396 { 14397 go_assert(saw_errors()); 14398 return Expression::make_error(this->location()); 14399 } 14400 14401 Location loc = this->location(); 14402 Expression* map_ref = this->map_; 14403 14404 Expression* index_ptr = Expression::make_unary(OPERATOR_AND, 14405 this->index_, 14406 loc); 14407 14408 Expression* type_expr = Expression::make_type_descriptor(type, loc); 14409 Expression* zero = type->fat_zero_value(gogo); 14410 Expression* map_index; 14411 if (zero == NULL) 14412 { 14413 Runtime::Function code; 14414 Expression* key; 14415 switch (type->algorithm(gogo)) 14416 { 14417 case Map_type::MAP_ALG_FAST32: 14418 case Map_type::MAP_ALG_FAST32PTR: 14419 { 14420 Type* uint32_type = Type::lookup_integer_type("uint32"); 14421 Type* uint32_ptr_type = Type::make_pointer_type(uint32_type); 14422 key = Expression::make_unsafe_cast(uint32_ptr_type, index_ptr, 14423 loc); 14424 key = Expression::make_dereference(key, NIL_CHECK_NOT_NEEDED, 14425 loc); 14426 code = Runtime::MAPACCESS1_FAST32; 14427 break; 14428 } 14429 case Map_type::MAP_ALG_FAST64: 14430 case Map_type::MAP_ALG_FAST64PTR: 14431 { 14432 Type* uint64_type = Type::lookup_integer_type("uint64"); 14433 Type* uint64_ptr_type = Type::make_pointer_type(uint64_type); 14434 key = Expression::make_unsafe_cast(uint64_ptr_type, index_ptr, 14435 loc); 14436 key = Expression::make_dereference(key, NIL_CHECK_NOT_NEEDED, 14437 loc); 14438 code = Runtime::MAPACCESS1_FAST64; 14439 break; 14440 } 14441 case Map_type::MAP_ALG_FASTSTR: 14442 key = this->index_; 14443 code = Runtime::MAPACCESS1_FASTSTR; 14444 break; 14445 default: 14446 key = index_ptr; 14447 code = Runtime::MAPACCESS1; 14448 break; 14449 } 14450 map_index = Runtime::make_call(code, loc, 3, 14451 type_expr, map_ref, key); 14452 } 14453 else 14454 map_index = Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, 14455 type_expr, map_ref, index_ptr, zero); 14456 14457 Type* val_type = type->val_type(); 14458 this->value_pointer_ = 14459 Expression::make_unsafe_cast(Type::make_pointer_type(val_type), 14460 map_index, this->location()); 14461 } 14462 14463 return this->value_pointer_; 14464 } 14465 14466 // Export a map index expression. 14467 14468 void 14469 Map_index_expression::do_export(Export_function_body* efb) const 14470 { 14471 efb->write_c_string("("); 14472 this->map_->export_expression(efb); 14473 efb->write_c_string(")["); 14474 14475 Type* old_context = efb->type_context(); 14476 efb->set_type_context(this->get_map_type()->key_type()); 14477 14478 this->index_->export_expression(efb); 14479 14480 efb->set_type_context(old_context); 14481 14482 efb->write_c_string("]"); 14483 } 14484 14485 // Dump ast representation for a map index expression 14486 14487 void 14488 Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 14489 const 14490 { 14491 Index_expression::dump_index_expression(ast_dump_context, this->map_, 14492 this->index_, NULL, NULL); 14493 } 14494 14495 // Make a map index expression. 14496 14497 Map_index_expression* 14498 Expression::make_map_index(Expression* map, Expression* index, 14499 Location location) 14500 { 14501 return new Map_index_expression(map, index, location); 14502 } 14503 14504 // Class Field_reference_expression. 14505 14506 // Lower a field reference expression. There is nothing to lower, but 14507 // this is where we generate the tracking information for fields with 14508 // the magic go:"track" tag. 14509 14510 Expression* 14511 Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, 14512 Statement_inserter* inserter, int) 14513 { 14514 Struct_type* struct_type = this->expr_->type()->struct_type(); 14515 if (struct_type == NULL) 14516 { 14517 // Error will be reported elsewhere. 14518 return this; 14519 } 14520 const Struct_field* field = struct_type->field(this->field_index_); 14521 if (field == NULL) 14522 return this; 14523 if (!field->has_tag()) 14524 return this; 14525 if (field->tag().find("go:\"track\"") == std::string::npos) 14526 return this; 14527 14528 // References from functions generated by the compiler don't count. 14529 if (function != NULL && function->func_value()->is_type_specific_function()) 14530 return this; 14531 14532 // We have found a reference to a tracked field. Build a call to 14533 // the runtime function __go_fieldtrack with a string that describes 14534 // the field. FIXME: We should only call this once per referenced 14535 // field per function, not once for each reference to the field. 14536 14537 if (this->called_fieldtrack_) 14538 return this; 14539 this->called_fieldtrack_ = true; 14540 14541 Location loc = this->location(); 14542 14543 std::string s = "fieldtrack \""; 14544 Named_type* nt = this->expr_->type()->unalias()->named_type(); 14545 if (nt == NULL || nt->named_object()->package() == NULL) 14546 s.append(gogo->pkgpath()); 14547 else 14548 s.append(nt->named_object()->package()->pkgpath()); 14549 s.push_back('.'); 14550 if (nt != NULL) 14551 s.append(Gogo::unpack_hidden_name(nt->name())); 14552 s.push_back('.'); 14553 s.append(Gogo::unpack_hidden_name(field->field_name())); 14554 s.push_back('"'); 14555 14556 // We can't use a string here, because internally a string holds a 14557 // pointer to the actual bytes; when the linker garbage collects the 14558 // string, it won't garbage collect the bytes. So we use a 14559 // [...]byte. 14560 14561 Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); 14562 14563 Type* byte_type = Type::lookup_integer_type("byte"); 14564 Array_type* array_type = Type::make_array_type(byte_type, length_expr); 14565 array_type->set_is_array_incomparable(); 14566 14567 Expression_list* bytes = new Expression_list(); 14568 for (std::string::const_iterator p = s.begin(); p != s.end(); p++) 14569 { 14570 unsigned char c = static_cast<unsigned char>(*p); 14571 bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); 14572 } 14573 14574 Expression* e = Expression::make_composite_literal(array_type, 0, false, 14575 bytes, false, loc); 14576 14577 Variable* var = new Variable(array_type, e, true, false, false, loc); 14578 14579 static int count; 14580 char buf[50]; 14581 snprintf(buf, sizeof buf, "fieldtrack.%d", count); 14582 ++count; 14583 14584 Named_object* no = gogo->add_variable(buf, var); 14585 e = Expression::make_var_reference(no, loc); 14586 e = Expression::make_unary(OPERATOR_AND, e, loc); 14587 14588 Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); 14589 gogo->lower_expression(function, inserter, &call); 14590 inserter->insert(Statement::make_statement(call, false)); 14591 14592 // Put this function, and the global variable we just created, into 14593 // unique sections. This will permit the linker to garbage collect 14594 // them if they are not referenced. The effect is that the only 14595 // strings, indicating field references, that will wind up in the 14596 // executable will be those for functions that are actually needed. 14597 if (function != NULL) 14598 function->func_value()->set_in_unique_section(); 14599 var->set_in_unique_section(); 14600 14601 return this; 14602 } 14603 14604 // Return the type of a field reference. 14605 14606 Type* 14607 Field_reference_expression::do_type() 14608 { 14609 Type* type = this->expr_->type(); 14610 if (type->is_error()) 14611 return type; 14612 Struct_type* struct_type = type->struct_type(); 14613 go_assert(struct_type != NULL); 14614 return struct_type->field(this->field_index_)->type(); 14615 } 14616 14617 // Check the types for a field reference. 14618 14619 void 14620 Field_reference_expression::do_check_types(Gogo*) 14621 { 14622 Type* type = this->expr_->type(); 14623 if (type->is_error()) 14624 return; 14625 Struct_type* struct_type = type->struct_type(); 14626 go_assert(struct_type != NULL); 14627 go_assert(struct_type->field(this->field_index_) != NULL); 14628 } 14629 14630 // Get the backend representation for a field reference. 14631 14632 Bexpression* 14633 Field_reference_expression::do_get_backend(Translate_context* context) 14634 { 14635 Bexpression* bstruct = this->expr_->get_backend(context); 14636 return context->gogo()->backend()->struct_field_expression(bstruct, 14637 this->field_index_, 14638 this->location()); 14639 } 14640 14641 // Dump ast representation for a field reference expression. 14642 14643 void 14644 Field_reference_expression::do_dump_expression( 14645 Ast_dump_context* ast_dump_context) const 14646 { 14647 this->expr_->dump_expression(ast_dump_context); 14648 ast_dump_context->ostream() << "." << this->field_index_; 14649 } 14650 14651 // Make a reference to a qualified identifier in an expression. 14652 14653 Field_reference_expression* 14654 Expression::make_field_reference(Expression* expr, unsigned int field_index, 14655 Location location) 14656 { 14657 return new Field_reference_expression(expr, field_index, location); 14658 } 14659 14660 // Class Interface_field_reference_expression. 14661 14662 // Return an expression for the pointer to the function to call. 14663 14664 Expression* 14665 Interface_field_reference_expression::get_function() 14666 { 14667 Expression* ref = this->expr_; 14668 Location loc = this->location(); 14669 if (ref->type()->points_to() != NULL) 14670 ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc); 14671 14672 Expression* mtable = 14673 Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); 14674 Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); 14675 14676 std::string name = Gogo::unpack_hidden_name(this->name_); 14677 unsigned int index; 14678 const Struct_field* field = mtable_type->find_local_field(name, &index); 14679 go_assert(field != NULL); 14680 14681 mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc); 14682 return Expression::make_field_reference(mtable, index, loc); 14683 } 14684 14685 // Return an expression for the first argument to pass to the interface 14686 // function. 14687 14688 Expression* 14689 Interface_field_reference_expression::get_underlying_object() 14690 { 14691 Expression* expr = this->expr_; 14692 if (expr->type()->points_to() != NULL) 14693 expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT, 14694 this->location()); 14695 return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, 14696 this->location()); 14697 } 14698 14699 // Traversal. 14700 14701 int 14702 Interface_field_reference_expression::do_traverse(Traverse* traverse) 14703 { 14704 return Expression::traverse(&this->expr_, traverse); 14705 } 14706 14707 // Lower the expression. If this expression is not called, we need to 14708 // evaluate the expression twice when converting to the backend 14709 // interface. So introduce a temporary variable if necessary. 14710 14711 Expression* 14712 Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, 14713 Statement_inserter* inserter) 14714 { 14715 if (this->expr_->is_error_expression() 14716 || this->expr_->type()->is_error_type()) 14717 { 14718 go_assert(saw_errors()); 14719 return Expression::make_error(this->location()); 14720 } 14721 14722 if (!this->expr_->is_multi_eval_safe()) 14723 { 14724 Temporary_statement* temp = 14725 Statement::make_temporary(NULL, this->expr_, this->location()); 14726 inserter->insert(temp); 14727 this->expr_ = Expression::make_temporary_reference(temp, this->location()); 14728 } 14729 return this; 14730 } 14731 14732 // Return the type of an interface field reference. 14733 14734 Type* 14735 Interface_field_reference_expression::do_type() 14736 { 14737 Type* expr_type = this->expr_->type(); 14738 14739 Type* points_to = expr_type->points_to(); 14740 if (points_to != NULL) 14741 expr_type = points_to; 14742 14743 Interface_type* interface_type = expr_type->interface_type(); 14744 if (interface_type == NULL) 14745 return Type::make_error_type(); 14746 14747 const Typed_identifier* method = interface_type->find_method(this->name_); 14748 if (method == NULL) 14749 return Type::make_error_type(); 14750 14751 return method->type(); 14752 } 14753 14754 // Determine types. 14755 14756 void 14757 Interface_field_reference_expression::do_determine_type(const Type_context*) 14758 { 14759 this->expr_->determine_type_no_context(); 14760 } 14761 14762 // Check the types for an interface field reference. 14763 14764 void 14765 Interface_field_reference_expression::do_check_types(Gogo*) 14766 { 14767 Type* type = this->expr_->type(); 14768 14769 Type* points_to = type->points_to(); 14770 if (points_to != NULL) 14771 type = points_to; 14772 14773 Interface_type* interface_type = type->interface_type(); 14774 if (interface_type == NULL) 14775 { 14776 if (!type->is_error_type()) 14777 this->report_error(_("expected interface or pointer to interface")); 14778 } 14779 else 14780 { 14781 const Typed_identifier* method = 14782 interface_type->find_method(this->name_); 14783 if (method == NULL) 14784 { 14785 go_error_at(this->location(), "method %qs not in interface", 14786 Gogo::message_name(this->name_).c_str()); 14787 this->set_is_error(); 14788 } 14789 } 14790 } 14791 14792 // If an interface field reference is not simply called, then it is 14793 // represented as a closure. The closure will hold a single variable, 14794 // the value of the interface on which the method should be called. 14795 // The function will be a simple thunk that pulls the value from the 14796 // closure and calls the method with the remaining arguments. 14797 14798 // Because method values are not common, we don't build all thunks for 14799 // all possible interface methods, but instead only build them as we 14800 // need them. In particular, we even build them on demand for 14801 // interface methods defined in other packages. 14802 14803 Interface_field_reference_expression::Interface_method_thunks 14804 Interface_field_reference_expression::interface_method_thunks; 14805 14806 // Find or create the thunk to call method NAME on TYPE. 14807 14808 Named_object* 14809 Interface_field_reference_expression::create_thunk(Gogo* gogo, 14810 Interface_type* type, 14811 const std::string& name) 14812 { 14813 std::pair<Interface_type*, Method_thunks*> val(type, NULL); 14814 std::pair<Interface_method_thunks::iterator, bool> ins = 14815 Interface_field_reference_expression::interface_method_thunks.insert(val); 14816 if (ins.second) 14817 { 14818 // This is the first time we have seen this interface. 14819 ins.first->second = new Method_thunks(); 14820 } 14821 14822 for (Method_thunks::const_iterator p = ins.first->second->begin(); 14823 p != ins.first->second->end(); 14824 p++) 14825 if (p->first == name) 14826 return p->second; 14827 14828 Location loc = type->location(); 14829 14830 const Typed_identifier* method_id = type->find_method(name); 14831 if (method_id == NULL) 14832 return Named_object::make_erroneous_name(gogo->thunk_name()); 14833 14834 Function_type* orig_fntype = method_id->type()->function_type(); 14835 if (orig_fntype == NULL) 14836 return Named_object::make_erroneous_name(gogo->thunk_name()); 14837 14838 Struct_field_list* sfl = new Struct_field_list(); 14839 // The type here is wrong--it should be the C function type. But it 14840 // doesn't really matter. 14841 Type* vt = Type::make_pointer_type(Type::make_void_type()); 14842 sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc))); 14843 sfl->push_back(Struct_field(Typed_identifier("val", type, loc))); 14844 Struct_type* st = Type::make_struct_type(sfl, loc); 14845 st->set_is_struct_incomparable(); 14846 Type* closure_type = Type::make_pointer_type(st); 14847 14848 Function_type* new_fntype = orig_fntype->copy_with_names(); 14849 14850 std::string thunk_name = gogo->thunk_name(); 14851 Named_object* new_no = gogo->start_function(thunk_name, new_fntype, 14852 false, loc); 14853 14854 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); 14855 cvar->set_is_used(); 14856 cvar->set_is_closure(); 14857 Named_object* cp = Named_object::make_variable("$closure" + thunk_name, 14858 NULL, cvar); 14859 new_no->func_value()->set_closure_var(cp); 14860 14861 gogo->start_block(loc); 14862 14863 // Field 0 of the closure is the function code pointer, field 1 is 14864 // the value on which to invoke the method. 14865 Expression* arg = Expression::make_var_reference(cp, loc); 14866 arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); 14867 arg = Expression::make_field_reference(arg, 1, loc); 14868 14869 Expression *ifre = Expression::make_interface_field_reference(arg, name, 14870 loc); 14871 14872 const Typed_identifier_list* orig_params = orig_fntype->parameters(); 14873 Expression_list* args; 14874 if (orig_params == NULL || orig_params->empty()) 14875 args = NULL; 14876 else 14877 { 14878 const Typed_identifier_list* new_params = new_fntype->parameters(); 14879 args = new Expression_list(); 14880 for (Typed_identifier_list::const_iterator p = new_params->begin(); 14881 p != new_params->end(); 14882 ++p) 14883 { 14884 Named_object* p_no = gogo->lookup(p->name(), NULL); 14885 go_assert(p_no != NULL 14886 && p_no->is_variable() 14887 && p_no->var_value()->is_parameter()); 14888 args->push_back(Expression::make_var_reference(p_no, loc)); 14889 } 14890 } 14891 14892 Call_expression* call = Expression::make_call(ifre, args, 14893 orig_fntype->is_varargs(), 14894 loc); 14895 call->set_varargs_are_lowered(); 14896 14897 Statement* s = Statement::make_return_from_call(call, loc); 14898 gogo->add_statement(s); 14899 Block* b = gogo->finish_block(loc); 14900 gogo->add_block(b, loc); 14901 14902 // This is called after lowering but before determine_types. 14903 gogo->lower_block(new_no, b); 14904 14905 gogo->finish_function(loc); 14906 14907 ins.first->second->push_back(std::make_pair(name, new_no)); 14908 return new_no; 14909 } 14910 14911 // Lookup a thunk to call method NAME on TYPE. 14912 14913 Named_object* 14914 Interface_field_reference_expression::lookup_thunk(Interface_type* type, 14915 const std::string& name) 14916 { 14917 Interface_method_thunks::const_iterator p = 14918 Interface_field_reference_expression::interface_method_thunks.find(type); 14919 if (p == Interface_field_reference_expression::interface_method_thunks.end()) 14920 return NULL; 14921 for (Method_thunks::const_iterator pm = p->second->begin(); 14922 pm != p->second->end(); 14923 ++pm) 14924 if (pm->first == name) 14925 return pm->second; 14926 return NULL; 14927 } 14928 14929 // Get the backend representation for a method value. 14930 14931 Bexpression* 14932 Interface_field_reference_expression::do_get_backend(Translate_context* context) 14933 { 14934 Interface_type* type = this->expr_->type()->interface_type(); 14935 if (type == NULL) 14936 { 14937 go_assert(saw_errors()); 14938 return context->backend()->error_expression(); 14939 } 14940 14941 Named_object* thunk = 14942 Interface_field_reference_expression::lookup_thunk(type, this->name_); 14943 14944 // The thunk should have been created during the 14945 // create_function_descriptors pass. 14946 if (thunk == NULL || thunk->is_erroneous()) 14947 { 14948 go_assert(saw_errors()); 14949 return context->backend()->error_expression(); 14950 } 14951 14952 // FIXME: We should lower this earlier, but we can't it lower it in 14953 // the lowering pass because at that point we don't know whether we 14954 // need to create the thunk or not. If the expression is called, we 14955 // don't need the thunk. 14956 14957 Location loc = this->location(); 14958 14959 Struct_field_list* fields = new Struct_field_list(); 14960 fields->push_back(Struct_field(Typed_identifier("fn", 14961 thunk->func_value()->type(), 14962 loc))); 14963 fields->push_back(Struct_field(Typed_identifier("val", 14964 this->expr_->type(), 14965 loc))); 14966 Struct_type* st = Type::make_struct_type(fields, loc); 14967 st->set_is_struct_incomparable(); 14968 14969 Expression_list* vals = new Expression_list(); 14970 vals->push_back(Expression::make_func_code_reference(thunk, loc)); 14971 vals->push_back(this->expr_); 14972 14973 Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); 14974 Bexpression* bclosure = 14975 Expression::make_heap_expression(expr, loc)->get_backend(context); 14976 14977 Gogo* gogo = context->gogo(); 14978 Btype* btype = this->type()->get_backend(gogo); 14979 bclosure = gogo->backend()->convert_expression(btype, bclosure, loc); 14980 14981 Expression* nil_check = 14982 Expression::make_binary(OPERATOR_EQEQ, this->expr_, 14983 Expression::make_nil(loc), loc); 14984 Bexpression* bnil_check = nil_check->get_backend(context); 14985 14986 Expression* crash = Runtime::make_call(Runtime::PANIC_MEM, loc, 0); 14987 Bexpression* bcrash = crash->get_backend(context); 14988 14989 Bfunction* bfn = context->function()->func_value()->get_decl(); 14990 Bexpression* bcond = 14991 gogo->backend()->conditional_expression(bfn, NULL, 14992 bnil_check, bcrash, NULL, loc); 14993 Bfunction* bfunction = context->function()->func_value()->get_decl(); 14994 Bstatement* cond_statement = 14995 gogo->backend()->expression_statement(bfunction, bcond); 14996 return gogo->backend()->compound_expression(cond_statement, bclosure, loc); 14997 } 14998 14999 // Dump ast representation for an interface field reference. 15000 15001 void 15002 Interface_field_reference_expression::do_dump_expression( 15003 Ast_dump_context* ast_dump_context) const 15004 { 15005 this->expr_->dump_expression(ast_dump_context); 15006 ast_dump_context->ostream() << "." << this->name_; 15007 } 15008 15009 // Make a reference to a field in an interface. 15010 15011 Expression* 15012 Expression::make_interface_field_reference(Expression* expr, 15013 const std::string& field, 15014 Location location) 15015 { 15016 return new Interface_field_reference_expression(expr, field, location); 15017 } 15018 15019 // A general selector. This is a Parser_expression for LEFT.NAME. It 15020 // is lowered after we know the type of the left hand side. 15021 15022 class Selector_expression : public Parser_expression 15023 { 15024 public: 15025 Selector_expression(Expression* left, const std::string& name, 15026 Location location) 15027 : Parser_expression(EXPRESSION_SELECTOR, location), 15028 left_(left), name_(name) 15029 { } 15030 15031 protected: 15032 int 15033 do_traverse(Traverse* traverse) 15034 { return Expression::traverse(&this->left_, traverse); } 15035 15036 Expression* 15037 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 15038 15039 Expression* 15040 do_copy() 15041 { 15042 return new Selector_expression(this->left_->copy(), this->name_, 15043 this->location()); 15044 } 15045 15046 void 15047 do_dump_expression(Ast_dump_context* ast_dump_context) const; 15048 15049 private: 15050 Expression* 15051 lower_method_expression(Gogo*); 15052 15053 // The expression on the left hand side. 15054 Expression* left_; 15055 // The name on the right hand side. 15056 std::string name_; 15057 }; 15058 15059 // Lower a selector expression once we know the real type of the left 15060 // hand side. 15061 15062 Expression* 15063 Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, 15064 int) 15065 { 15066 Expression* left = this->left_; 15067 if (left->is_type_expression()) 15068 return this->lower_method_expression(gogo); 15069 return Type::bind_field_or_method(gogo, left->type(), left, this->name_, 15070 this->location()); 15071 } 15072 15073 // Lower a method expression T.M or (*T).M. We turn this into a 15074 // function literal. 15075 15076 Expression* 15077 Selector_expression::lower_method_expression(Gogo* gogo) 15078 { 15079 Location location = this->location(); 15080 Type* left_type = this->left_->type(); 15081 Type* type = left_type; 15082 const std::string& name(this->name_); 15083 15084 bool is_pointer; 15085 if (type->points_to() == NULL) 15086 is_pointer = false; 15087 else 15088 { 15089 is_pointer = true; 15090 type = type->points_to(); 15091 } 15092 15093 Named_type* nt = type->named_type(); 15094 Struct_type* st = type->struct_type(); 15095 bool is_ambiguous; 15096 Method* method = NULL; 15097 if (nt != NULL) 15098 method = nt->method_function(name, &is_ambiguous); 15099 else if (st != NULL) 15100 method = st->method_function(name, &is_ambiguous); 15101 const Typed_identifier* imethod = NULL; 15102 if (method == NULL && !is_pointer) 15103 { 15104 Interface_type* it = type->interface_type(); 15105 if (it != NULL) 15106 imethod = it->find_method(name); 15107 } 15108 15109 if ((method == NULL && imethod == NULL) 15110 || (left_type->named_type() != NULL && left_type->points_to() != NULL)) 15111 { 15112 if (nt != NULL) 15113 { 15114 if (!is_ambiguous) 15115 go_error_at(location, "type %<%s%s%> has no method %<%s%>", 15116 is_pointer ? "*" : "", 15117 nt->message_name().c_str(), 15118 Gogo::message_name(name).c_str()); 15119 else 15120 go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", 15121 Gogo::message_name(name).c_str(), 15122 is_pointer ? "*" : "", 15123 nt->message_name().c_str()); 15124 } 15125 else 15126 { 15127 if (!is_ambiguous) 15128 go_error_at(location, "type has no method %<%s%>", 15129 Gogo::message_name(name).c_str()); 15130 else 15131 go_error_at(location, "method %<%s%> is ambiguous", 15132 Gogo::message_name(name).c_str()); 15133 } 15134 return Expression::make_error(location); 15135 } 15136 15137 if (method != NULL && !is_pointer && !method->is_value_method()) 15138 { 15139 go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", 15140 nt->message_name().c_str(), 15141 Gogo::message_name(name).c_str()); 15142 return Expression::make_error(location); 15143 } 15144 15145 // Build a new function type in which the receiver becomes the first 15146 // argument. 15147 Function_type* method_type; 15148 if (method != NULL) 15149 { 15150 method_type = method->type(); 15151 go_assert(method_type->is_method()); 15152 } 15153 else 15154 { 15155 method_type = imethod->type()->function_type(); 15156 go_assert(method_type != NULL && !method_type->is_method()); 15157 } 15158 15159 const char* const receiver_name = "$this"; 15160 Typed_identifier_list* parameters = new Typed_identifier_list(); 15161 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), 15162 location)); 15163 15164 const Typed_identifier_list* method_parameters = method_type->parameters(); 15165 if (method_parameters != NULL) 15166 { 15167 int i = 0; 15168 for (Typed_identifier_list::const_iterator p = method_parameters->begin(); 15169 p != method_parameters->end(); 15170 ++p, ++i) 15171 { 15172 if (!p->name().empty() && !Gogo::is_sink_name(p->name())) 15173 parameters->push_back(*p); 15174 else 15175 { 15176 char buf[20]; 15177 snprintf(buf, sizeof buf, "$param%d", i); 15178 parameters->push_back(Typed_identifier(buf, p->type(), 15179 p->location())); 15180 } 15181 } 15182 } 15183 15184 const Typed_identifier_list* method_results = method_type->results(); 15185 Typed_identifier_list* results; 15186 if (method_results == NULL) 15187 results = NULL; 15188 else 15189 { 15190 results = new Typed_identifier_list(); 15191 for (Typed_identifier_list::const_iterator p = method_results->begin(); 15192 p != method_results->end(); 15193 ++p) 15194 results->push_back(*p); 15195 } 15196 15197 Function_type* fntype = Type::make_function_type(NULL, parameters, results, 15198 location); 15199 if (method_type->is_varargs()) 15200 fntype->set_is_varargs(); 15201 15202 // We generate methods which always takes a pointer to the receiver 15203 // as their first argument. If this is for a pointer type, we can 15204 // simply reuse the existing function. We use an internal hack to 15205 // get the right type. 15206 // FIXME: This optimization is disabled because it doesn't yet work 15207 // with function descriptors when the method expression is not 15208 // directly called. 15209 if (method != NULL && is_pointer && false) 15210 { 15211 Named_object* mno = (method->needs_stub_method() 15212 ? method->stub_object() 15213 : method->named_object()); 15214 Expression* f = Expression::make_func_reference(mno, NULL, location); 15215 f = Expression::make_cast(fntype, f, location); 15216 Type_conversion_expression* tce = 15217 static_cast<Type_conversion_expression*>(f); 15218 tce->set_may_convert_function_types(); 15219 return f; 15220 } 15221 15222 Named_object* no = gogo->start_function(gogo->thunk_name(), fntype, false, 15223 location); 15224 15225 Named_object* vno = gogo->lookup(receiver_name, NULL); 15226 go_assert(vno != NULL); 15227 Expression* ve = Expression::make_var_reference(vno, location); 15228 Expression* bm; 15229 if (method != NULL) 15230 bm = Type::bind_field_or_method(gogo, type, ve, name, location); 15231 else 15232 bm = Expression::make_interface_field_reference(ve, name, location); 15233 15234 // Even though we found the method above, if it has an error type we 15235 // may see an error here. 15236 if (bm->is_error_expression()) 15237 { 15238 gogo->finish_function(location); 15239 return bm; 15240 } 15241 15242 Expression_list* args; 15243 if (parameters->size() <= 1) 15244 args = NULL; 15245 else 15246 { 15247 args = new Expression_list(); 15248 Typed_identifier_list::const_iterator p = parameters->begin(); 15249 ++p; 15250 for (; p != parameters->end(); ++p) 15251 { 15252 vno = gogo->lookup(p->name(), NULL); 15253 go_assert(vno != NULL); 15254 args->push_back(Expression::make_var_reference(vno, location)); 15255 } 15256 } 15257 15258 gogo->start_block(location); 15259 15260 Call_expression* call = Expression::make_call(bm, args, 15261 method_type->is_varargs(), 15262 location); 15263 15264 Statement* s = Statement::make_return_from_call(call, location); 15265 gogo->add_statement(s); 15266 15267 Block* b = gogo->finish_block(location); 15268 15269 gogo->add_block(b, location); 15270 15271 // Lower the call in case there are multiple results. 15272 gogo->lower_block(no, b); 15273 gogo->flatten_block(no, b); 15274 15275 gogo->finish_function(location); 15276 15277 return Expression::make_func_reference(no, NULL, location); 15278 } 15279 15280 // Dump the ast for a selector expression. 15281 15282 void 15283 Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 15284 const 15285 { 15286 ast_dump_context->dump_expression(this->left_); 15287 ast_dump_context->ostream() << "."; 15288 ast_dump_context->ostream() << this->name_; 15289 } 15290 15291 // Make a selector expression. 15292 15293 Expression* 15294 Expression::make_selector(Expression* left, const std::string& name, 15295 Location location) 15296 { 15297 return new Selector_expression(left, name, location); 15298 } 15299 15300 // Class Allocation_expression. 15301 15302 int 15303 Allocation_expression::do_traverse(Traverse* traverse) 15304 { 15305 return Type::traverse(this->type_, traverse); 15306 } 15307 15308 Type* 15309 Allocation_expression::do_type() 15310 { 15311 return Type::make_pointer_type(this->type_); 15312 } 15313 15314 void 15315 Allocation_expression::do_check_types(Gogo*) 15316 { 15317 if (!this->type_->in_heap()) 15318 go_error_at(this->location(), "cannot heap allocate go:notinheap type"); 15319 } 15320 15321 // Make a copy of an allocation expression. 15322 15323 Expression* 15324 Allocation_expression::do_copy() 15325 { 15326 Allocation_expression* alloc = 15327 new Allocation_expression(this->type_->copy_expressions(), 15328 this->location()); 15329 if (this->allocate_on_stack_) 15330 alloc->set_allocate_on_stack(); 15331 if (this->no_zero_) 15332 alloc->set_no_zero(); 15333 return alloc; 15334 } 15335 15336 // Return the backend representation for an allocation expression. 15337 15338 Bexpression* 15339 Allocation_expression::do_get_backend(Translate_context* context) 15340 { 15341 Gogo* gogo = context->gogo(); 15342 Location loc = this->location(); 15343 Btype* btype = this->type_->get_backend(gogo); 15344 15345 if (this->allocate_on_stack_) 15346 { 15347 int64_t size; 15348 bool ok = this->type_->backend_type_size(gogo, &size); 15349 if (!ok) 15350 { 15351 go_assert(saw_errors()); 15352 return gogo->backend()->error_expression(); 15353 } 15354 Bstatement* decl; 15355 Named_object* fn = context->function(); 15356 go_assert(fn != NULL); 15357 Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); 15358 Bexpression* init = (this->no_zero_ 15359 ? NULL 15360 : gogo->backend()->zero_expression(btype)); 15361 Bvariable* temp = 15362 gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, 15363 init, 15364 Backend::variable_address_is_taken, 15365 loc, &decl); 15366 Bexpression* ret = gogo->backend()->var_expression(temp, loc); 15367 ret = gogo->backend()->address_expression(ret, loc); 15368 ret = gogo->backend()->compound_expression(decl, ret, loc); 15369 return ret; 15370 } 15371 15372 Bexpression* space = 15373 gogo->allocate_memory(this->type_, loc)->get_backend(context); 15374 Btype* pbtype = gogo->backend()->pointer_type(btype); 15375 return gogo->backend()->convert_expression(pbtype, space, loc); 15376 } 15377 15378 // Dump ast representation for an allocation expression. 15379 15380 void 15381 Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 15382 const 15383 { 15384 ast_dump_context->ostream() << "new("; 15385 ast_dump_context->dump_type(this->type_); 15386 ast_dump_context->ostream() << ")"; 15387 } 15388 15389 // Make an allocation expression. 15390 15391 Expression* 15392 Expression::make_allocation(Type* type, Location location) 15393 { 15394 return new Allocation_expression(type, location); 15395 } 15396 15397 // Class Ordered_value_list. 15398 15399 int 15400 Ordered_value_list::traverse_vals(Traverse* traverse) 15401 { 15402 if (this->vals_ != NULL) 15403 { 15404 if (this->traverse_order_ == NULL) 15405 { 15406 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) 15407 return TRAVERSE_EXIT; 15408 } 15409 else 15410 { 15411 for (std::vector<unsigned long>::const_iterator p = 15412 this->traverse_order_->begin(); 15413 p != this->traverse_order_->end(); 15414 ++p) 15415 { 15416 if (Expression::traverse(&this->vals_->at(*p), traverse) 15417 == TRAVERSE_EXIT) 15418 return TRAVERSE_EXIT; 15419 } 15420 } 15421 } 15422 return TRAVERSE_CONTINUE; 15423 } 15424 15425 // Class Struct_construction_expression. 15426 15427 // Traversal. 15428 15429 int 15430 Struct_construction_expression::do_traverse(Traverse* traverse) 15431 { 15432 if (this->traverse_vals(traverse) == TRAVERSE_EXIT) 15433 return TRAVERSE_EXIT; 15434 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 15435 return TRAVERSE_EXIT; 15436 return TRAVERSE_CONTINUE; 15437 } 15438 15439 // Return whether this is a constant initializer. 15440 15441 bool 15442 Struct_construction_expression::is_constant_struct() const 15443 { 15444 if (this->vals() == NULL) 15445 return true; 15446 for (Expression_list::const_iterator pv = this->vals()->begin(); 15447 pv != this->vals()->end(); 15448 ++pv) 15449 { 15450 if (*pv != NULL 15451 && !(*pv)->is_constant() 15452 && (!(*pv)->is_composite_literal() 15453 || (*pv)->is_nonconstant_composite_literal())) 15454 return false; 15455 } 15456 15457 const Struct_field_list* fields = this->type_->struct_type()->fields(); 15458 for (Struct_field_list::const_iterator pf = fields->begin(); 15459 pf != fields->end(); 15460 ++pf) 15461 { 15462 // There are no constant constructors for interfaces. 15463 if (pf->type()->interface_type() != NULL) 15464 return false; 15465 } 15466 15467 return true; 15468 } 15469 15470 // Return whether this is a zero value. 15471 15472 bool 15473 Struct_construction_expression::do_is_zero_value() const 15474 { 15475 if (this->vals() == NULL) 15476 return true; 15477 for (Expression_list::const_iterator pv = this->vals()->begin(); 15478 pv != this->vals()->end(); 15479 ++pv) 15480 if (*pv != NULL && !(*pv)->is_zero_value()) 15481 return false; 15482 15483 const Struct_field_list* fields = this->type_->struct_type()->fields(); 15484 for (Struct_field_list::const_iterator pf = fields->begin(); 15485 pf != fields->end(); 15486 ++pf) 15487 { 15488 // Interface conversion may cause a zero value being converted 15489 // to a non-zero value, like interface{}(0). Be conservative. 15490 if (pf->type()->interface_type() != NULL) 15491 return false; 15492 } 15493 15494 return true; 15495 } 15496 15497 // Return whether this struct can be used as a constant initializer. 15498 15499 bool 15500 Struct_construction_expression::do_is_static_initializer() const 15501 { 15502 if (this->vals() == NULL) 15503 return true; 15504 for (Expression_list::const_iterator pv = this->vals()->begin(); 15505 pv != this->vals()->end(); 15506 ++pv) 15507 { 15508 if (*pv != NULL && !(*pv)->is_static_initializer()) 15509 return false; 15510 } 15511 15512 const Struct_field_list* fields = this->type_->struct_type()->fields(); 15513 for (Struct_field_list::const_iterator pf = fields->begin(); 15514 pf != fields->end(); 15515 ++pf) 15516 { 15517 // There are no constant constructors for interfaces. 15518 if (pf->type()->interface_type() != NULL) 15519 return false; 15520 } 15521 15522 return true; 15523 } 15524 15525 // Final type determination. 15526 15527 void 15528 Struct_construction_expression::do_determine_type(const Type_context*) 15529 { 15530 if (this->vals() == NULL) 15531 return; 15532 const Struct_field_list* fields = this->type_->struct_type()->fields(); 15533 Expression_list::const_iterator pv = this->vals()->begin(); 15534 for (Struct_field_list::const_iterator pf = fields->begin(); 15535 pf != fields->end(); 15536 ++pf, ++pv) 15537 { 15538 if (pv == this->vals()->end()) 15539 return; 15540 if (*pv != NULL) 15541 { 15542 Type_context subcontext(pf->type(), false); 15543 (*pv)->determine_type(&subcontext); 15544 } 15545 } 15546 // Extra values are an error we will report elsewhere; we still want 15547 // to determine the type to avoid knockon errors. 15548 for (; pv != this->vals()->end(); ++pv) 15549 (*pv)->determine_type_no_context(); 15550 } 15551 15552 // Check types. 15553 15554 void 15555 Struct_construction_expression::do_check_types(Gogo*) 15556 { 15557 if (this->vals() == NULL) 15558 return; 15559 15560 Struct_type* st = this->type_->struct_type(); 15561 if (this->vals()->size() > st->field_count()) 15562 { 15563 this->report_error(_("too many expressions for struct")); 15564 return; 15565 } 15566 15567 const Struct_field_list* fields = st->fields(); 15568 Expression_list::const_iterator pv = this->vals()->begin(); 15569 int i = 0; 15570 for (Struct_field_list::const_iterator pf = fields->begin(); 15571 pf != fields->end(); 15572 ++pf, ++pv, ++i) 15573 { 15574 if (pv == this->vals()->end()) 15575 { 15576 this->report_error(_("too few expressions for struct")); 15577 break; 15578 } 15579 15580 if (*pv == NULL) 15581 continue; 15582 15583 std::string reason; 15584 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) 15585 { 15586 if (reason.empty()) 15587 go_error_at((*pv)->location(), 15588 "incompatible type for field %d in struct construction", 15589 i + 1); 15590 else 15591 go_error_at((*pv)->location(), 15592 ("incompatible type for field %d in " 15593 "struct construction (%s)"), 15594 i + 1, reason.c_str()); 15595 this->set_is_error(); 15596 } 15597 } 15598 go_assert(pv == this->vals()->end()); 15599 } 15600 15601 // Copy. 15602 15603 Expression* 15604 Struct_construction_expression::do_copy() 15605 { 15606 Struct_construction_expression* ret = 15607 new Struct_construction_expression(this->type_->copy_expressions(), 15608 (this->vals() == NULL 15609 ? NULL 15610 : this->vals()->copy()), 15611 this->location()); 15612 if (this->traverse_order() != NULL) 15613 ret->set_traverse_order(this->traverse_order()); 15614 return ret; 15615 } 15616 15617 // Make implicit type conversions explicit. 15618 15619 void 15620 Struct_construction_expression::do_add_conversions() 15621 { 15622 if (this->vals() == NULL) 15623 return; 15624 15625 Location loc = this->location(); 15626 const Struct_field_list* fields = this->type_->struct_type()->fields(); 15627 Expression_list::iterator pv = this->vals()->begin(); 15628 for (Struct_field_list::const_iterator pf = fields->begin(); 15629 pf != fields->end(); 15630 ++pf, ++pv) 15631 { 15632 if (pv == this->vals()->end()) 15633 break; 15634 if (*pv != NULL) 15635 { 15636 Type* ft = pf->type(); 15637 if (!Type::are_identical(ft, (*pv)->type(), 0, NULL) 15638 && ft->interface_type() != NULL) 15639 *pv = Expression::make_cast(ft, *pv, loc); 15640 } 15641 } 15642 } 15643 15644 // Return the backend representation for constructing a struct. 15645 15646 Bexpression* 15647 Struct_construction_expression::do_get_backend(Translate_context* context) 15648 { 15649 Gogo* gogo = context->gogo(); 15650 15651 Btype* btype = this->type_->get_backend(gogo); 15652 if (this->vals() == NULL) 15653 return gogo->backend()->zero_expression(btype); 15654 15655 const Struct_field_list* fields = this->type_->struct_type()->fields(); 15656 Expression_list::const_iterator pv = this->vals()->begin(); 15657 std::vector<Bexpression*> init; 15658 for (Struct_field_list::const_iterator pf = fields->begin(); 15659 pf != fields->end(); 15660 ++pf) 15661 { 15662 Btype* fbtype = pf->type()->get_backend(gogo); 15663 if (pv == this->vals()->end()) 15664 init.push_back(gogo->backend()->zero_expression(fbtype)); 15665 else if (*pv == NULL) 15666 { 15667 init.push_back(gogo->backend()->zero_expression(fbtype)); 15668 ++pv; 15669 } 15670 else 15671 { 15672 Expression* val = 15673 Expression::convert_for_assignment(gogo, pf->type(), 15674 *pv, this->location()); 15675 init.push_back(val->get_backend(context)); 15676 ++pv; 15677 } 15678 } 15679 if (this->type_->struct_type()->has_padding()) 15680 { 15681 // Feed an extra value if there is a padding field. 15682 Btype *fbtype = Type::lookup_integer_type("uint8")->get_backend(gogo); 15683 init.push_back(gogo->backend()->zero_expression(fbtype)); 15684 } 15685 return gogo->backend()->constructor_expression(btype, init, this->location()); 15686 } 15687 15688 // Export a struct construction. 15689 15690 void 15691 Struct_construction_expression::do_export(Export_function_body* efb) const 15692 { 15693 efb->write_c_string("$convert("); 15694 efb->write_type(this->type_); 15695 for (Expression_list::const_iterator pv = this->vals()->begin(); 15696 pv != this->vals()->end(); 15697 ++pv) 15698 { 15699 efb->write_c_string(", "); 15700 if (*pv != NULL) 15701 (*pv)->export_expression(efb); 15702 } 15703 efb->write_c_string(")"); 15704 } 15705 15706 // Dump ast representation of a struct construction expression. 15707 15708 void 15709 Struct_construction_expression::do_dump_expression( 15710 Ast_dump_context* ast_dump_context) const 15711 { 15712 ast_dump_context->dump_type(this->type_); 15713 ast_dump_context->ostream() << "{"; 15714 ast_dump_context->dump_expression_list(this->vals()); 15715 ast_dump_context->ostream() << "}"; 15716 } 15717 15718 // Make a struct composite literal. This used by the thunk code. 15719 15720 Expression* 15721 Expression::make_struct_composite_literal(Type* type, Expression_list* vals, 15722 Location location) 15723 { 15724 go_assert(type->struct_type() != NULL); 15725 return new Struct_construction_expression(type, vals, location); 15726 } 15727 15728 // Class Array_construction_expression. 15729 15730 // Traversal. 15731 15732 int 15733 Array_construction_expression::do_traverse(Traverse* traverse) 15734 { 15735 if (this->traverse_vals(traverse) == TRAVERSE_EXIT) 15736 return TRAVERSE_EXIT; 15737 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 15738 return TRAVERSE_EXIT; 15739 return TRAVERSE_CONTINUE; 15740 } 15741 15742 // Return whether this is a constant initializer. 15743 15744 bool 15745 Array_construction_expression::is_constant_array() const 15746 { 15747 if (this->vals() == NULL) 15748 return true; 15749 15750 // There are no constant constructors for interfaces. 15751 if (this->type_->array_type()->element_type()->interface_type() != NULL) 15752 return false; 15753 15754 for (Expression_list::const_iterator pv = this->vals()->begin(); 15755 pv != this->vals()->end(); 15756 ++pv) 15757 { 15758 if (*pv != NULL 15759 && !(*pv)->is_constant() 15760 && (!(*pv)->is_composite_literal() 15761 || (*pv)->is_nonconstant_composite_literal())) 15762 return false; 15763 } 15764 return true; 15765 } 15766 15767 // Return whether this is a zero value. 15768 15769 bool 15770 Array_construction_expression::do_is_zero_value() const 15771 { 15772 if (this->vals() == NULL) 15773 return true; 15774 15775 // Interface conversion may cause a zero value being converted 15776 // to a non-zero value, like interface{}(0). Be conservative. 15777 if (this->type_->array_type()->element_type()->interface_type() != NULL) 15778 return false; 15779 15780 for (Expression_list::const_iterator pv = this->vals()->begin(); 15781 pv != this->vals()->end(); 15782 ++pv) 15783 if (*pv != NULL && !(*pv)->is_zero_value()) 15784 return false; 15785 15786 return true; 15787 } 15788 15789 // Return whether this can be used a constant initializer. 15790 15791 bool 15792 Array_construction_expression::do_is_static_initializer() const 15793 { 15794 if (this->vals() == NULL) 15795 return true; 15796 15797 // There are no constant constructors for interfaces. 15798 if (this->type_->array_type()->element_type()->interface_type() != NULL) 15799 return false; 15800 15801 for (Expression_list::const_iterator pv = this->vals()->begin(); 15802 pv != this->vals()->end(); 15803 ++pv) 15804 { 15805 if (*pv != NULL && !(*pv)->is_static_initializer()) 15806 return false; 15807 } 15808 return true; 15809 } 15810 15811 // Final type determination. 15812 15813 void 15814 Array_construction_expression::do_determine_type(const Type_context*) 15815 { 15816 if (this->is_error_expression()) 15817 { 15818 go_assert(saw_errors()); 15819 return; 15820 } 15821 15822 if (this->vals() == NULL) 15823 return; 15824 Array_type* at = this->type_->array_type(); 15825 if (at == NULL || at->is_error() || at->element_type()->is_error()) 15826 { 15827 go_assert(saw_errors()); 15828 this->set_is_error(); 15829 return; 15830 } 15831 Type_context subcontext(at->element_type(), false); 15832 for (Expression_list::const_iterator pv = this->vals()->begin(); 15833 pv != this->vals()->end(); 15834 ++pv) 15835 { 15836 if (*pv != NULL) 15837 (*pv)->determine_type(&subcontext); 15838 } 15839 } 15840 15841 // Check types. 15842 15843 void 15844 Array_construction_expression::do_check_types(Gogo*) 15845 { 15846 if (this->is_error_expression()) 15847 { 15848 go_assert(saw_errors()); 15849 return; 15850 } 15851 15852 if (this->vals() == NULL) 15853 return; 15854 15855 Array_type* at = this->type_->array_type(); 15856 if (at == NULL || at->is_error() || at->element_type()->is_error()) 15857 { 15858 go_assert(saw_errors()); 15859 this->set_is_error(); 15860 return; 15861 } 15862 int i = 0; 15863 Type* element_type = at->element_type(); 15864 for (Expression_list::const_iterator pv = this->vals()->begin(); 15865 pv != this->vals()->end(); 15866 ++pv, ++i) 15867 { 15868 if (*pv != NULL 15869 && !Type::are_assignable(element_type, (*pv)->type(), NULL)) 15870 { 15871 go_error_at((*pv)->location(), 15872 "incompatible type for element %d in composite literal", 15873 i + 1); 15874 this->set_is_error(); 15875 } 15876 } 15877 } 15878 15879 // Make implicit type conversions explicit. 15880 15881 void 15882 Array_construction_expression::do_add_conversions() 15883 { 15884 if (this->is_error_expression()) 15885 { 15886 go_assert(saw_errors()); 15887 return; 15888 } 15889 15890 if (this->vals() == NULL) 15891 return; 15892 15893 Type* et = this->type_->array_type()->element_type(); 15894 if (et->interface_type() == NULL) 15895 return; 15896 15897 Location loc = this->location(); 15898 for (Expression_list::iterator pv = this->vals()->begin(); 15899 pv != this->vals()->end(); 15900 ++pv) 15901 if (!Type::are_identical(et, (*pv)->type(), 0, NULL)) 15902 *pv = Expression::make_cast(et, *pv, loc); 15903 } 15904 15905 // Get a constructor expression for the array values. 15906 15907 Bexpression* 15908 Array_construction_expression::get_constructor(Translate_context* context, 15909 Btype* array_btype) 15910 { 15911 Type* element_type = this->type_->array_type()->element_type(); 15912 15913 std::vector<unsigned long> indexes; 15914 std::vector<Bexpression*> vals; 15915 Gogo* gogo = context->gogo(); 15916 if (this->vals() != NULL) 15917 { 15918 size_t i = 0; 15919 std::vector<unsigned long>::const_iterator pi; 15920 if (this->indexes_ != NULL) 15921 pi = this->indexes_->begin(); 15922 for (Expression_list::const_iterator pv = this->vals()->begin(); 15923 pv != this->vals()->end(); 15924 ++pv, ++i) 15925 { 15926 if (this->indexes_ != NULL) 15927 go_assert(pi != this->indexes_->end()); 15928 15929 if (this->indexes_ == NULL) 15930 indexes.push_back(i); 15931 else 15932 indexes.push_back(*pi); 15933 if (*pv == NULL) 15934 { 15935 Btype* ebtype = element_type->get_backend(gogo); 15936 Bexpression *zv = gogo->backend()->zero_expression(ebtype); 15937 vals.push_back(zv); 15938 } 15939 else 15940 { 15941 Expression* val_expr = 15942 Expression::convert_for_assignment(gogo, element_type, *pv, 15943 this->location()); 15944 vals.push_back(val_expr->get_backend(context)); 15945 } 15946 if (this->indexes_ != NULL) 15947 ++pi; 15948 } 15949 if (this->indexes_ != NULL) 15950 go_assert(pi == this->indexes_->end()); 15951 } 15952 return gogo->backend()->array_constructor_expression(array_btype, indexes, 15953 vals, this->location()); 15954 } 15955 15956 // Export an array construction. 15957 15958 void 15959 Array_construction_expression::do_export(Export_function_body* efb) const 15960 { 15961 efb->write_c_string("$convert("); 15962 efb->write_type(this->type_); 15963 if (this->vals() != NULL) 15964 { 15965 std::vector<unsigned long>::const_iterator pi; 15966 if (this->indexes_ != NULL) 15967 pi = this->indexes_->begin(); 15968 for (Expression_list::const_iterator pv = this->vals()->begin(); 15969 pv != this->vals()->end(); 15970 ++pv) 15971 { 15972 efb->write_c_string(", "); 15973 15974 if (this->indexes_ != NULL) 15975 { 15976 char buf[100]; 15977 snprintf(buf, sizeof buf, "%lu", *pi); 15978 efb->write_c_string(buf); 15979 efb->write_c_string(":"); 15980 } 15981 15982 if (*pv != NULL) 15983 (*pv)->export_expression(efb); 15984 15985 if (this->indexes_ != NULL) 15986 ++pi; 15987 } 15988 } 15989 efb->write_c_string(")"); 15990 } 15991 15992 // Dump ast representation of an array construction expression. 15993 15994 void 15995 Array_construction_expression::do_dump_expression( 15996 Ast_dump_context* ast_dump_context) const 15997 { 15998 Expression* length = this->type_->array_type()->length(); 15999 16000 ast_dump_context->ostream() << "[" ; 16001 if (length != NULL) 16002 { 16003 ast_dump_context->dump_expression(length); 16004 } 16005 ast_dump_context->ostream() << "]" ; 16006 ast_dump_context->dump_type(this->type_); 16007 this->dump_slice_storage_expression(ast_dump_context); 16008 ast_dump_context->ostream() << "{" ; 16009 if (this->indexes_ == NULL) 16010 ast_dump_context->dump_expression_list(this->vals()); 16011 else 16012 { 16013 Expression_list::const_iterator pv = this->vals()->begin(); 16014 for (std::vector<unsigned long>::const_iterator pi = 16015 this->indexes_->begin(); 16016 pi != this->indexes_->end(); 16017 ++pi, ++pv) 16018 { 16019 if (pi != this->indexes_->begin()) 16020 ast_dump_context->ostream() << ", "; 16021 ast_dump_context->ostream() << *pi << ':'; 16022 ast_dump_context->dump_expression(*pv); 16023 } 16024 } 16025 ast_dump_context->ostream() << "}" ; 16026 16027 } 16028 16029 // Class Fixed_array_construction_expression. 16030 16031 Fixed_array_construction_expression::Fixed_array_construction_expression( 16032 Type* type, const std::vector<unsigned long>* indexes, 16033 Expression_list* vals, Location location) 16034 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, 16035 type, indexes, vals, location) 16036 { go_assert(type->array_type() != NULL && !type->is_slice_type()); } 16037 16038 16039 // Copy. 16040 16041 Expression* 16042 Fixed_array_construction_expression::do_copy() 16043 { 16044 Type* t = this->type()->copy_expressions(); 16045 return new Fixed_array_construction_expression(t, this->indexes(), 16046 (this->vals() == NULL 16047 ? NULL 16048 : this->vals()->copy()), 16049 this->location()); 16050 } 16051 16052 // Return the backend representation for constructing a fixed array. 16053 16054 Bexpression* 16055 Fixed_array_construction_expression::do_get_backend(Translate_context* context) 16056 { 16057 Type* type = this->type(); 16058 Btype* btype = type->get_backend(context->gogo()); 16059 return this->get_constructor(context, btype); 16060 } 16061 16062 Expression* 16063 Expression::make_array_composite_literal(Type* type, Expression_list* vals, 16064 Location location) 16065 { 16066 go_assert(type->array_type() != NULL && !type->is_slice_type()); 16067 return new Fixed_array_construction_expression(type, NULL, vals, location); 16068 } 16069 16070 // Class Slice_construction_expression. 16071 16072 Slice_construction_expression::Slice_construction_expression( 16073 Type* type, const std::vector<unsigned long>* indexes, 16074 Expression_list* vals, Location location) 16075 : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, 16076 type, indexes, vals, location), 16077 valtype_(NULL), array_val_(NULL), slice_storage_(NULL), 16078 storage_escapes_(true) 16079 { 16080 go_assert(type->is_slice_type()); 16081 16082 unsigned long lenval; 16083 Expression* length; 16084 if (vals == NULL || vals->empty()) 16085 lenval = 0; 16086 else 16087 { 16088 if (this->indexes() == NULL) 16089 lenval = vals->size(); 16090 else 16091 lenval = indexes->back() + 1; 16092 } 16093 Type* int_type = Type::lookup_integer_type("int"); 16094 length = Expression::make_integer_ul(lenval, int_type, location); 16095 Type* element_type = type->array_type()->element_type(); 16096 Array_type* array_type = Type::make_array_type(element_type, length); 16097 array_type->set_is_array_incomparable(); 16098 this->valtype_ = array_type; 16099 } 16100 16101 // Traversal. 16102 16103 int 16104 Slice_construction_expression::do_traverse(Traverse* traverse) 16105 { 16106 if (this->Array_construction_expression::do_traverse(traverse) 16107 == TRAVERSE_EXIT) 16108 return TRAVERSE_EXIT; 16109 if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) 16110 return TRAVERSE_EXIT; 16111 if (this->array_val_ != NULL 16112 && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) 16113 return TRAVERSE_EXIT; 16114 if (this->slice_storage_ != NULL 16115 && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) 16116 return TRAVERSE_EXIT; 16117 return TRAVERSE_CONTINUE; 16118 } 16119 16120 // Helper routine to create fixed array value underlying the slice literal. 16121 // May be called during flattening, or later during do_get_backend(). 16122 16123 Expression* 16124 Slice_construction_expression::create_array_val() 16125 { 16126 Array_type* array_type = this->type()->array_type(); 16127 if (array_type == NULL) 16128 { 16129 go_assert(this->type()->is_error()); 16130 return NULL; 16131 } 16132 16133 Location loc = this->location(); 16134 go_assert(this->valtype_ != NULL); 16135 16136 Expression_list* vals = this->vals(); 16137 return new Fixed_array_construction_expression( 16138 this->valtype_, this->indexes(), vals, loc); 16139 } 16140 16141 // If we're previous established that the slice storage does not 16142 // escape, then create a separate array temp val here for it. We 16143 // need to do this as part of flattening so as to be able to insert 16144 // the new temp statement. 16145 16146 Expression* 16147 Slice_construction_expression::do_flatten(Gogo*, Named_object*, 16148 Statement_inserter* inserter) 16149 { 16150 if (this->type()->array_type() == NULL) 16151 { 16152 go_assert(saw_errors()); 16153 return Expression::make_error(this->location()); 16154 } 16155 16156 // Create a stack-allocated storage temp if storage won't escape 16157 if (!this->storage_escapes_ 16158 && this->slice_storage_ == NULL 16159 && this->element_count() > 0) 16160 { 16161 Location loc = this->location(); 16162 this->array_val_ = this->create_array_val(); 16163 go_assert(this->array_val_ != NULL); 16164 Temporary_statement* temp = 16165 Statement::make_temporary(this->valtype_, this->array_val_, loc); 16166 inserter->insert(temp); 16167 this->slice_storage_ = Expression::make_temporary_reference(temp, loc); 16168 } 16169 return this; 16170 } 16171 16172 // When dumping a slice construction expression that has an explicit 16173 // storeage temp, emit the temp here (if we don't do this the storage 16174 // temp appears unused in the AST dump). 16175 16176 void 16177 Slice_construction_expression:: 16178 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const 16179 { 16180 if (this->slice_storage_ == NULL) 16181 return; 16182 ast_dump_context->ostream() << "storage=" ; 16183 ast_dump_context->dump_expression(this->slice_storage_); 16184 } 16185 16186 // Copy. 16187 16188 Expression* 16189 Slice_construction_expression::do_copy() 16190 { 16191 return new Slice_construction_expression(this->type()->copy_expressions(), 16192 this->indexes(), 16193 (this->vals() == NULL 16194 ? NULL 16195 : this->vals()->copy()), 16196 this->location()); 16197 } 16198 16199 // Return the backend representation for constructing a slice. 16200 16201 Bexpression* 16202 Slice_construction_expression::do_get_backend(Translate_context* context) 16203 { 16204 if (this->array_val_ == NULL) 16205 this->array_val_ = this->create_array_val(); 16206 if (this->array_val_ == NULL) 16207 { 16208 go_assert(this->type()->is_error()); 16209 return context->backend()->error_expression(); 16210 } 16211 16212 Location loc = this->location(); 16213 16214 bool is_static_initializer = this->array_val_->is_static_initializer(); 16215 16216 // We have to copy the initial values into heap memory if we are in 16217 // a function or if the values are not constants. 16218 bool copy_to_heap = context->function() != NULL || !is_static_initializer; 16219 16220 Expression* space; 16221 16222 if (this->slice_storage_ != NULL) 16223 { 16224 go_assert(!this->storage_escapes_); 16225 space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); 16226 } 16227 else if (!copy_to_heap) 16228 { 16229 // The initializer will only run once. 16230 space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); 16231 space->unary_expression()->set_is_slice_init(); 16232 } 16233 else 16234 { 16235 go_assert(this->storage_escapes_ || this->element_count() == 0); 16236 space = Expression::make_heap_expression(this->array_val_, loc); 16237 } 16238 Array_type* at = this->valtype_->array_type(); 16239 Type* et = at->element_type(); 16240 space = Expression::make_unsafe_cast(Type::make_pointer_type(et), 16241 space, loc); 16242 16243 // Build a constructor for the slice. 16244 Expression* len = at->length(); 16245 Expression* slice_val = 16246 Expression::make_slice_value(this->type(), space, len, len, loc); 16247 return slice_val->get_backend(context); 16248 } 16249 16250 // Make a slice composite literal. This is used by the type 16251 // descriptor code. 16252 16253 Slice_construction_expression* 16254 Expression::make_slice_composite_literal(Type* type, Expression_list* vals, 16255 Location location) 16256 { 16257 go_assert(type->is_slice_type()); 16258 return new Slice_construction_expression(type, NULL, vals, location); 16259 } 16260 16261 // Class Map_construction_expression. 16262 16263 // Traversal. 16264 16265 int 16266 Map_construction_expression::do_traverse(Traverse* traverse) 16267 { 16268 if (this->vals_ != NULL 16269 && this->vals_->traverse(traverse) == TRAVERSE_EXIT) 16270 return TRAVERSE_EXIT; 16271 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 16272 return TRAVERSE_EXIT; 16273 return TRAVERSE_CONTINUE; 16274 } 16275 16276 // Flatten constructor initializer into a temporary variable since 16277 // we need to take its address for __go_construct_map. 16278 16279 Expression* 16280 Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, 16281 Statement_inserter* inserter) 16282 { 16283 if (!this->is_error_expression() 16284 && this->vals_ != NULL 16285 && !this->vals_->empty() 16286 && this->constructor_temp_ == NULL) 16287 { 16288 Map_type* mt = this->type_->map_type(); 16289 Type* key_type = mt->key_type(); 16290 Type* val_type = mt->val_type(); 16291 this->element_type_ = Type::make_builtin_struct_type(2, 16292 "__key", key_type, 16293 "__val", val_type); 16294 16295 Expression_list* value_pairs = new Expression_list(); 16296 Location loc = this->location(); 16297 16298 size_t i = 0; 16299 for (Expression_list::const_iterator pv = this->vals_->begin(); 16300 pv != this->vals_->end(); 16301 ++pv, ++i) 16302 { 16303 Expression_list* key_value_pair = new Expression_list(); 16304 Expression* key = *pv; 16305 if (key->is_error_expression() || key->type()->is_error_type()) 16306 { 16307 go_assert(saw_errors()); 16308 return Expression::make_error(loc); 16309 } 16310 if (key->type()->interface_type() != NULL 16311 && !key->is_multi_eval_safe()) 16312 { 16313 Temporary_statement* temp = 16314 Statement::make_temporary(NULL, key, loc); 16315 inserter->insert(temp); 16316 key = Expression::make_temporary_reference(temp, loc); 16317 } 16318 key = Expression::convert_for_assignment(gogo, key_type, key, loc); 16319 16320 ++pv; 16321 Expression* val = *pv; 16322 if (val->is_error_expression() || val->type()->is_error_type()) 16323 { 16324 go_assert(saw_errors()); 16325 return Expression::make_error(loc); 16326 } 16327 if (val->type()->interface_type() != NULL 16328 && !val->is_multi_eval_safe()) 16329 { 16330 Temporary_statement* temp = 16331 Statement::make_temporary(NULL, val, loc); 16332 inserter->insert(temp); 16333 val = Expression::make_temporary_reference(temp, loc); 16334 } 16335 val = Expression::convert_for_assignment(gogo, val_type, val, loc); 16336 16337 key_value_pair->push_back(key); 16338 key_value_pair->push_back(val); 16339 value_pairs->push_back( 16340 Expression::make_struct_composite_literal(this->element_type_, 16341 key_value_pair, loc)); 16342 } 16343 16344 Expression* element_count = Expression::make_integer_ul(i, NULL, loc); 16345 Array_type* ctor_type = 16346 Type::make_array_type(this->element_type_, element_count); 16347 ctor_type->set_is_array_incomparable(); 16348 Expression* constructor = 16349 new Fixed_array_construction_expression(ctor_type, NULL, 16350 value_pairs, loc); 16351 16352 this->constructor_temp_ = 16353 Statement::make_temporary(NULL, constructor, loc); 16354 constructor->issue_nil_check(); 16355 this->constructor_temp_->set_is_address_taken(); 16356 inserter->insert(this->constructor_temp_); 16357 } 16358 16359 return this; 16360 } 16361 16362 // Final type determination. 16363 16364 void 16365 Map_construction_expression::do_determine_type(const Type_context*) 16366 { 16367 if (this->vals_ == NULL) 16368 return; 16369 16370 Map_type* mt = this->type_->map_type(); 16371 Type_context key_context(mt->key_type(), false); 16372 Type_context val_context(mt->val_type(), false); 16373 for (Expression_list::const_iterator pv = this->vals_->begin(); 16374 pv != this->vals_->end(); 16375 ++pv) 16376 { 16377 (*pv)->determine_type(&key_context); 16378 ++pv; 16379 (*pv)->determine_type(&val_context); 16380 } 16381 } 16382 16383 // Check types. 16384 16385 void 16386 Map_construction_expression::do_check_types(Gogo*) 16387 { 16388 if (this->vals_ == NULL) 16389 return; 16390 16391 Map_type* mt = this->type_->map_type(); 16392 int i = 0; 16393 Type* key_type = mt->key_type(); 16394 Type* val_type = mt->val_type(); 16395 for (Expression_list::const_iterator pv = this->vals_->begin(); 16396 pv != this->vals_->end(); 16397 ++pv, ++i) 16398 { 16399 if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) 16400 { 16401 go_error_at((*pv)->location(), 16402 "incompatible type for element %d key in map construction", 16403 i + 1); 16404 this->set_is_error(); 16405 } 16406 ++pv; 16407 if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) 16408 { 16409 go_error_at((*pv)->location(), 16410 ("incompatible type for element %d value " 16411 "in map construction"), 16412 i + 1); 16413 this->set_is_error(); 16414 } 16415 } 16416 } 16417 16418 // Copy. 16419 16420 Expression* 16421 Map_construction_expression::do_copy() 16422 { 16423 return new Map_construction_expression(this->type_->copy_expressions(), 16424 (this->vals_ == NULL 16425 ? NULL 16426 : this->vals_->copy()), 16427 this->location()); 16428 } 16429 16430 // Make implicit type conversions explicit. 16431 16432 void 16433 Map_construction_expression::do_add_conversions() 16434 { 16435 if (this->vals_ == NULL || this->vals_->empty()) 16436 return; 16437 16438 Map_type* mt = this->type_->map_type(); 16439 Type* kt = mt->key_type(); 16440 Type* vt = mt->val_type(); 16441 bool key_is_interface = (kt->interface_type() != NULL); 16442 bool val_is_interface = (vt->interface_type() != NULL); 16443 if (!key_is_interface && !val_is_interface) 16444 return; 16445 16446 Location loc = this->location(); 16447 for (Expression_list::iterator pv = this->vals_->begin(); 16448 pv != this->vals_->end(); 16449 ++pv) 16450 { 16451 if (key_is_interface && 16452 !Type::are_identical(kt, (*pv)->type(), 0, NULL)) 16453 *pv = Expression::make_cast(kt, *pv, loc); 16454 ++pv; 16455 if (val_is_interface && 16456 !Type::are_identical(vt, (*pv)->type(), 0, NULL)) 16457 *pv = Expression::make_cast(vt, *pv, loc); 16458 } 16459 } 16460 16461 // Return the backend representation for constructing a map. 16462 16463 Bexpression* 16464 Map_construction_expression::do_get_backend(Translate_context* context) 16465 { 16466 if (this->is_error_expression()) 16467 return context->backend()->error_expression(); 16468 Location loc = this->location(); 16469 16470 size_t i = 0; 16471 Expression* ventries; 16472 if (this->vals_ == NULL || this->vals_->empty()) 16473 ventries = Expression::make_nil(loc); 16474 else 16475 { 16476 go_assert(this->constructor_temp_ != NULL); 16477 i = this->vals_->size() / 2; 16478 16479 Expression* ctor_ref = 16480 Expression::make_temporary_reference(this->constructor_temp_, loc); 16481 ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); 16482 } 16483 16484 Map_type* mt = this->type_->map_type(); 16485 if (this->element_type_ == NULL) 16486 this->element_type_ = 16487 Type::make_builtin_struct_type(2, 16488 "__key", mt->key_type(), 16489 "__val", mt->val_type()); 16490 Expression* descriptor = Expression::make_type_descriptor(mt, loc); 16491 16492 Type* uintptr_t = Type::lookup_integer_type("uintptr"); 16493 Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); 16494 16495 Expression* entry_size = 16496 Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); 16497 16498 unsigned int field_index; 16499 const Struct_field* valfield = 16500 this->element_type_->find_local_field("__val", &field_index); 16501 Expression* val_offset = 16502 Expression::make_struct_field_offset(this->element_type_, valfield); 16503 16504 Expression* map_ctor = 16505 Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, 16506 entry_size, val_offset, ventries); 16507 return map_ctor->get_backend(context); 16508 } 16509 16510 // Export an array construction. 16511 16512 void 16513 Map_construction_expression::do_export(Export_function_body* efb) const 16514 { 16515 efb->write_c_string("$convert("); 16516 efb->write_type(this->type_); 16517 for (Expression_list::const_iterator pv = this->vals_->begin(); 16518 pv != this->vals_->end(); 16519 ++pv) 16520 { 16521 efb->write_c_string(", "); 16522 (*pv)->export_expression(efb); 16523 } 16524 efb->write_c_string(")"); 16525 } 16526 16527 // Dump ast representation for a map construction expression. 16528 16529 void 16530 Map_construction_expression::do_dump_expression( 16531 Ast_dump_context* ast_dump_context) const 16532 { 16533 ast_dump_context->ostream() << "{" ; 16534 ast_dump_context->dump_expression_list(this->vals_, true); 16535 ast_dump_context->ostream() << "}"; 16536 } 16537 16538 // A composite literal key. This is seen during parsing, but is not 16539 // resolved to a named_object in case this is a composite literal of 16540 // struct type. 16541 16542 class Composite_literal_key_expression : public Parser_expression 16543 { 16544 public: 16545 Composite_literal_key_expression(const std::string& name, Location location) 16546 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL_KEY, location), 16547 name_(name) 16548 { } 16549 16550 const std::string& 16551 name() const 16552 { return this->name_; } 16553 16554 protected: 16555 Expression* 16556 do_lower(Gogo*, Named_object*, Statement_inserter*, int); 16557 16558 Expression* 16559 do_copy() 16560 { 16561 return new Composite_literal_key_expression(this->name_, this->location()); 16562 } 16563 16564 void 16565 do_dump_expression(Ast_dump_context*) const; 16566 16567 private: 16568 // The name. 16569 std::string name_; 16570 }; 16571 16572 // Lower a composite literal key. We will never get here for keys in 16573 // composite literals of struct types, because that is prevented by 16574 // Composite_literal_expression::do_traverse. So if we do get here, 16575 // this must be a regular name reference after all. 16576 16577 Expression* 16578 Composite_literal_key_expression::do_lower(Gogo* gogo, Named_object*, 16579 Statement_inserter*, int) 16580 { 16581 Named_object* no = gogo->lookup(this->name_, NULL); 16582 if (no == NULL) 16583 { 16584 // Gogo::lookup doesn't look in the global namespace, and names 16585 // used in composite literal keys aren't seen by 16586 // Gogo::define_global_names, so we have to look in the global 16587 // namespace ourselves. 16588 no = gogo->lookup_global(Gogo::unpack_hidden_name(this->name_).c_str()); 16589 if (no == NULL) 16590 { 16591 go_error_at(this->location(), "reference to undefined name %qs", 16592 Gogo::message_name(this->name_).c_str()); 16593 return Expression::make_error(this->location()); 16594 } 16595 } 16596 return Expression::make_unknown_reference(no, this->location()); 16597 } 16598 16599 // Dump a composite literal key. 16600 16601 void 16602 Composite_literal_key_expression::do_dump_expression( 16603 Ast_dump_context* ast_dump_context) const 16604 { 16605 ast_dump_context->ostream() << "_UnknownName_(" << this->name_ << ")"; 16606 } 16607 16608 // Make a composite literal key. 16609 16610 Expression* 16611 Expression::make_composite_literal_key(const std::string& name, 16612 Location location) 16613 { 16614 return new Composite_literal_key_expression(name, location); 16615 } 16616 16617 // Class Composite_literal_expression. 16618 16619 // Traversal. 16620 16621 int 16622 Composite_literal_expression::do_traverse(Traverse* traverse) 16623 { 16624 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 16625 return TRAVERSE_EXIT; 16626 16627 // If this is a struct composite literal with keys, then the keys 16628 // are field names, not expressions. We don't want to traverse them 16629 // in that case. If we do, we can give an erroneous error "variable 16630 // initializer refers to itself." See bug482.go in the testsuite. 16631 if (this->has_keys_ && this->vals_ != NULL) 16632 { 16633 // The type may not be resolvable at this point. 16634 Type* type = this->type_; 16635 16636 for (int depth = 0; depth < this->depth_; ++depth) 16637 { 16638 type = type->deref(); 16639 if (type->array_type() != NULL) 16640 type = type->array_type()->element_type(); 16641 else if (type->map_type() != NULL) 16642 { 16643 if (this->key_path_[depth]) 16644 type = type->map_type()->key_type(); 16645 else 16646 type = type->map_type()->val_type(); 16647 } 16648 else 16649 { 16650 // This error will be reported during lowering. 16651 return TRAVERSE_CONTINUE; 16652 } 16653 } 16654 type = type->deref(); 16655 16656 while (true) 16657 { 16658 if (type->classification() == Type::TYPE_NAMED) 16659 type = type->named_type()->real_type(); 16660 else if (type->classification() == Type::TYPE_FORWARD) 16661 { 16662 Type* t = type->forwarded(); 16663 if (t == type) 16664 break; 16665 type = t; 16666 } 16667 else 16668 break; 16669 } 16670 16671 if (type->classification() == Type::TYPE_STRUCT) 16672 { 16673 Expression_list::iterator p = this->vals_->begin(); 16674 while (p != this->vals_->end()) 16675 { 16676 // Skip key. 16677 ++p; 16678 go_assert(p != this->vals_->end()); 16679 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) 16680 return TRAVERSE_EXIT; 16681 ++p; 16682 } 16683 return TRAVERSE_CONTINUE; 16684 } 16685 } 16686 16687 if (this->vals_ != NULL) 16688 return this->vals_->traverse(traverse); 16689 16690 return TRAVERSE_CONTINUE; 16691 } 16692 16693 // Lower a generic composite literal into a specific version based on 16694 // the type. 16695 16696 Expression* 16697 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, 16698 Statement_inserter* inserter, int) 16699 { 16700 Type* type = this->type_; 16701 16702 for (int depth = 0; depth < this->depth_; ++depth) 16703 { 16704 type = type->deref(); 16705 if (type->array_type() != NULL) 16706 type = type->array_type()->element_type(); 16707 else if (type->map_type() != NULL) 16708 { 16709 if (this->key_path_[depth]) 16710 type = type->map_type()->key_type(); 16711 else 16712 type = type->map_type()->val_type(); 16713 } 16714 else 16715 { 16716 if (!type->is_error()) 16717 go_error_at(this->location(), 16718 ("may only omit types within composite literals " 16719 "of slice, array, or map type")); 16720 return Expression::make_error(this->location()); 16721 } 16722 } 16723 16724 Type *pt = type->points_to(); 16725 bool is_pointer = false; 16726 if (pt != NULL) 16727 { 16728 is_pointer = true; 16729 type = pt; 16730 } 16731 16732 Expression* ret; 16733 if (type->is_error()) 16734 return Expression::make_error(this->location()); 16735 else if (type->struct_type() != NULL) 16736 ret = this->lower_struct(gogo, type); 16737 else if (type->array_type() != NULL) 16738 ret = this->lower_array(type); 16739 else if (type->map_type() != NULL) 16740 ret = this->lower_map(gogo, function, inserter, type); 16741 else 16742 { 16743 go_error_at(this->location(), 16744 ("expected struct, slice, array, or map type " 16745 "for composite literal")); 16746 return Expression::make_error(this->location()); 16747 } 16748 16749 if (is_pointer) 16750 ret = Expression::make_heap_expression(ret, this->location()); 16751 16752 return ret; 16753 } 16754 16755 // Lower a struct composite literal. 16756 16757 Expression* 16758 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) 16759 { 16760 Location location = this->location(); 16761 Struct_type* st = type->struct_type(); 16762 if (this->vals_ == NULL || !this->has_keys_) 16763 { 16764 if (this->vals_ != NULL 16765 && !this->vals_->empty() 16766 && type->named_type() != NULL 16767 && type->named_type()->named_object()->package() != NULL) 16768 { 16769 for (Struct_field_list::const_iterator pf = st->fields()->begin(); 16770 pf != st->fields()->end(); 16771 ++pf) 16772 { 16773 if (Gogo::is_hidden_name(pf->field_name()) 16774 || pf->is_embedded_builtin(gogo)) 16775 go_error_at(this->location(), 16776 "assignment of unexported field %qs in %qs literal", 16777 Gogo::message_name(pf->field_name()).c_str(), 16778 type->named_type()->message_name().c_str()); 16779 } 16780 } 16781 16782 return new Struct_construction_expression(type, this->vals_, location); 16783 } 16784 16785 size_t field_count = st->field_count(); 16786 std::vector<Expression*> vals(field_count); 16787 std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>); 16788 Expression_list::const_iterator p = this->vals_->begin(); 16789 Expression* external_expr = NULL; 16790 const Named_object* external_no = NULL; 16791 while (p != this->vals_->end()) 16792 { 16793 Expression* name_expr = *p; 16794 16795 ++p; 16796 go_assert(p != this->vals_->end()); 16797 Expression* val = *p; 16798 16799 ++p; 16800 16801 if (name_expr == NULL) 16802 { 16803 go_error_at(val->location(), 16804 "mixture of field and value initializers"); 16805 return Expression::make_error(location); 16806 } 16807 16808 bool bad_key = false; 16809 std::string name; 16810 const Named_object* no = NULL; 16811 switch (name_expr->classification()) 16812 { 16813 case EXPRESSION_COMPOSITE_LITERAL_KEY: 16814 name = 16815 static_cast<Composite_literal_key_expression*>(name_expr)->name(); 16816 break; 16817 16818 case EXPRESSION_UNKNOWN_REFERENCE: 16819 name = name_expr->unknown_expression()->name(); 16820 if (type->named_type() != NULL) 16821 { 16822 // If the named object found for this field name comes from a 16823 // different package than the struct it is a part of, do not count 16824 // this incorrect lookup as a usage of the object's package. 16825 no = name_expr->unknown_expression()->named_object(); 16826 if (no->package() != NULL 16827 && no->package() != type->named_type()->named_object()->package()) 16828 no->package()->forget_usage(name_expr); 16829 } 16830 break; 16831 16832 case EXPRESSION_CONST_REFERENCE: 16833 no = static_cast<Const_expression*>(name_expr)->named_object(); 16834 break; 16835 16836 case EXPRESSION_TYPE: 16837 { 16838 Type* t = name_expr->type(); 16839 Named_type* nt = t->named_type(); 16840 if (nt == NULL) 16841 bad_key = true; 16842 else 16843 no = nt->named_object(); 16844 } 16845 break; 16846 16847 case EXPRESSION_VAR_REFERENCE: 16848 no = name_expr->var_expression()->named_object(); 16849 break; 16850 16851 case EXPRESSION_ENCLOSED_VAR_REFERENCE: 16852 no = name_expr->enclosed_var_expression()->variable(); 16853 break; 16854 16855 case EXPRESSION_FUNC_REFERENCE: 16856 no = name_expr->func_expression()->named_object(); 16857 break; 16858 16859 default: 16860 bad_key = true; 16861 break; 16862 } 16863 if (bad_key) 16864 { 16865 go_error_at(name_expr->location(), "expected struct field name"); 16866 return Expression::make_error(location); 16867 } 16868 16869 if (no != NULL) 16870 { 16871 if (no->package() != NULL && external_expr == NULL) 16872 { 16873 external_expr = name_expr; 16874 external_no = no; 16875 } 16876 16877 name = no->name(); 16878 16879 // A predefined name won't be packed. If it starts with a 16880 // lower case letter we need to check for that case, because 16881 // the field name will be packed. FIXME. 16882 if (!Gogo::is_hidden_name(name) 16883 && name[0] >= 'a' 16884 && name[0] <= 'z') 16885 { 16886 Named_object* gno = gogo->lookup_global(name.c_str()); 16887 if (gno == no) 16888 name = gogo->pack_hidden_name(name, false); 16889 } 16890 } 16891 16892 unsigned int index; 16893 const Struct_field* sf = st->find_local_field(name, &index); 16894 if (sf == NULL) 16895 { 16896 go_error_at(name_expr->location(), "unknown field %qs in %qs", 16897 Gogo::message_name(name).c_str(), 16898 (type->named_type() != NULL 16899 ? type->named_type()->message_name().c_str() 16900 : "unnamed struct")); 16901 return Expression::make_error(location); 16902 } 16903 if (vals[index] != NULL) 16904 { 16905 go_error_at(name_expr->location(), 16906 "duplicate value for field %qs in %qs", 16907 Gogo::message_name(name).c_str(), 16908 (type->named_type() != NULL 16909 ? type->named_type()->message_name().c_str() 16910 : "unnamed struct")); 16911 return Expression::make_error(location); 16912 } 16913 16914 if (type->named_type() != NULL 16915 && type->named_type()->named_object()->package() != NULL 16916 && (Gogo::is_hidden_name(sf->field_name()) 16917 || sf->is_embedded_builtin(gogo))) 16918 go_error_at(name_expr->location(), 16919 "assignment of unexported field %qs in %qs literal", 16920 Gogo::message_name(sf->field_name()).c_str(), 16921 type->named_type()->message_name().c_str()); 16922 16923 vals[index] = val; 16924 traverse_order->push_back(static_cast<unsigned long>(index)); 16925 } 16926 16927 if (!this->all_are_names_) 16928 { 16929 // This is a weird case like bug462 in the testsuite. 16930 if (external_expr == NULL) 16931 go_error_at(this->location(), "unknown field in %qs literal", 16932 (type->named_type() != NULL 16933 ? type->named_type()->message_name().c_str() 16934 : "unnamed struct")); 16935 else 16936 go_error_at(external_expr->location(), "unknown field %qs in %qs", 16937 external_no->message_name().c_str(), 16938 (type->named_type() != NULL 16939 ? type->named_type()->message_name().c_str() 16940 : "unnamed struct")); 16941 return Expression::make_error(location); 16942 } 16943 16944 Expression_list* list = new Expression_list; 16945 list->reserve(field_count); 16946 for (size_t i = 0; i < field_count; ++i) 16947 list->push_back(vals[i]); 16948 16949 Struct_construction_expression* ret = 16950 new Struct_construction_expression(type, list, location); 16951 ret->set_traverse_order(traverse_order); 16952 return ret; 16953 } 16954 16955 // Index/value/traversal-order triple. 16956 16957 struct IVT_triple { 16958 unsigned long index; 16959 unsigned long traversal_order; 16960 Expression* expr; 16961 IVT_triple(unsigned long i, unsigned long to, Expression *e) 16962 : index(i), traversal_order(to), expr(e) { } 16963 bool operator<(const IVT_triple& other) const 16964 { return this->index < other.index; } 16965 }; 16966 16967 // Lower an array composite literal. 16968 16969 Expression* 16970 Composite_literal_expression::lower_array(Type* type) 16971 { 16972 Location location = this->location(); 16973 if (this->vals_ == NULL || !this->has_keys_) 16974 return this->make_array(type, NULL, this->vals_); 16975 16976 std::vector<unsigned long>* indexes = new std::vector<unsigned long>; 16977 indexes->reserve(this->vals_->size()); 16978 bool indexes_out_of_order = false; 16979 Expression_list* vals = new Expression_list(); 16980 vals->reserve(this->vals_->size()); 16981 unsigned long index = 0; 16982 Expression_list::const_iterator p = this->vals_->begin(); 16983 while (p != this->vals_->end()) 16984 { 16985 Expression* index_expr = *p; 16986 16987 ++p; 16988 go_assert(p != this->vals_->end()); 16989 Expression* val = *p; 16990 16991 ++p; 16992 16993 if (index_expr == NULL) 16994 { 16995 if (std::find(indexes->begin(), indexes->end(), index) 16996 != indexes->end()) 16997 { 16998 go_error_at(val->location(), 16999 "duplicate value for index %lu", index); 17000 return Expression::make_error(location); 17001 } 17002 if (!indexes->empty()) 17003 indexes->push_back(index); 17004 } 17005 else 17006 { 17007 if (indexes->empty() && !vals->empty()) 17008 { 17009 for (size_t i = 0; i < vals->size(); ++i) 17010 indexes->push_back(i); 17011 } 17012 17013 Numeric_constant nc; 17014 if (!index_expr->numeric_constant_value(&nc)) 17015 { 17016 go_error_at(index_expr->location(), 17017 "index expression is not integer constant"); 17018 return Expression::make_error(location); 17019 } 17020 17021 switch (nc.to_unsigned_long(&index)) 17022 { 17023 case Numeric_constant::NC_UL_VALID: 17024 break; 17025 case Numeric_constant::NC_UL_NOTINT: 17026 go_error_at(index_expr->location(), 17027 "index expression is not integer constant"); 17028 return Expression::make_error(location); 17029 case Numeric_constant::NC_UL_NEGATIVE: 17030 go_error_at(index_expr->location(), 17031 "index expression is negative"); 17032 return Expression::make_error(location); 17033 case Numeric_constant::NC_UL_BIG: 17034 go_error_at(index_expr->location(), "index value overflow"); 17035 return Expression::make_error(location); 17036 default: 17037 go_unreachable(); 17038 } 17039 17040 Named_type* ntype = Type::lookup_integer_type("int"); 17041 Integer_type* inttype = ntype->integer_type(); 17042 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) 17043 && index >> (inttype->bits() - 1) != 0) 17044 { 17045 go_error_at(index_expr->location(), "index value overflow"); 17046 return Expression::make_error(location); 17047 } 17048 17049 if (std::find(indexes->begin(), indexes->end(), index) 17050 != indexes->end()) 17051 { 17052 go_error_at(index_expr->location(), 17053 "duplicate value for index %lu", 17054 index); 17055 return Expression::make_error(location); 17056 } 17057 17058 if (!indexes->empty() && index < indexes->back()) 17059 indexes_out_of_order = true; 17060 17061 indexes->push_back(index); 17062 } 17063 17064 vals->push_back(val); 17065 17066 ++index; 17067 } 17068 17069 if (indexes->empty()) 17070 { 17071 delete indexes; 17072 indexes = NULL; 17073 } 17074 17075 std::vector<unsigned long>* traverse_order = NULL; 17076 if (indexes_out_of_order) 17077 { 17078 typedef std::vector<IVT_triple> V; 17079 17080 V v; 17081 v.reserve(indexes->size()); 17082 std::vector<unsigned long>::const_iterator pi = indexes->begin(); 17083 unsigned long torder = 0; 17084 for (Expression_list::const_iterator pe = vals->begin(); 17085 pe != vals->end(); 17086 ++pe, ++pi, ++torder) 17087 v.push_back(IVT_triple(*pi, torder, *pe)); 17088 17089 std::sort(v.begin(), v.end()); 17090 17091 delete indexes; 17092 delete vals; 17093 17094 indexes = new std::vector<unsigned long>(); 17095 indexes->reserve(v.size()); 17096 vals = new Expression_list(); 17097 vals->reserve(v.size()); 17098 traverse_order = new std::vector<unsigned long>(); 17099 traverse_order->reserve(v.size()); 17100 17101 for (V::const_iterator pv = v.begin(); pv != v.end(); ++pv) 17102 { 17103 indexes->push_back(pv->index); 17104 vals->push_back(pv->expr); 17105 traverse_order->push_back(pv->traversal_order); 17106 } 17107 } 17108 17109 Expression* ret = this->make_array(type, indexes, vals); 17110 Array_construction_expression* ace = ret->array_literal(); 17111 if (ace != NULL && traverse_order != NULL) 17112 ace->set_traverse_order(traverse_order); 17113 return ret; 17114 } 17115 17116 // Actually build the array composite literal. This handles 17117 // [...]{...}. 17118 17119 Expression* 17120 Composite_literal_expression::make_array( 17121 Type* type, 17122 const std::vector<unsigned long>* indexes, 17123 Expression_list* vals) 17124 { 17125 Location location = this->location(); 17126 Array_type* at = type->array_type(); 17127 17128 if (at->length() != NULL && at->length()->is_nil_expression()) 17129 { 17130 size_t size; 17131 if (vals == NULL) 17132 size = 0; 17133 else if (indexes != NULL) 17134 size = indexes->back() + 1; 17135 else 17136 { 17137 size = vals->size(); 17138 Integer_type* it = Type::lookup_integer_type("int")->integer_type(); 17139 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) 17140 && size >> (it->bits() - 1) != 0) 17141 { 17142 go_error_at(location, "too many elements in composite literal"); 17143 return Expression::make_error(location); 17144 } 17145 } 17146 17147 Expression* elen = Expression::make_integer_ul(size, NULL, location); 17148 at = Type::make_array_type(at->element_type(), elen); 17149 type = at; 17150 } 17151 else if (at->length() != NULL 17152 && !at->length()->is_error_expression() 17153 && this->vals_ != NULL) 17154 { 17155 Numeric_constant nc; 17156 unsigned long val; 17157 if (at->length()->numeric_constant_value(&nc) 17158 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) 17159 { 17160 if (indexes == NULL) 17161 { 17162 if (this->vals_->size() > val) 17163 { 17164 go_error_at(location, 17165 "too many elements in composite literal"); 17166 return Expression::make_error(location); 17167 } 17168 } 17169 else 17170 { 17171 unsigned long max = indexes->back(); 17172 if (max >= val) 17173 { 17174 go_error_at(location, 17175 ("some element keys in composite literal " 17176 "are out of range")); 17177 return Expression::make_error(location); 17178 } 17179 } 17180 } 17181 } 17182 17183 if (at->length() != NULL) 17184 return new Fixed_array_construction_expression(type, indexes, vals, 17185 location); 17186 else 17187 return new Slice_construction_expression(type, indexes, vals, location); 17188 } 17189 17190 // Lower a map composite literal. 17191 17192 Expression* 17193 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, 17194 Statement_inserter* inserter, 17195 Type* type) 17196 { 17197 Location location = this->location(); 17198 Unordered_map(unsigned int, std::vector<Expression*>) st; 17199 Unordered_map(unsigned int, std::vector<Expression*>) nt; 17200 bool saw_false = false; 17201 bool saw_true = false; 17202 if (this->vals_ != NULL) 17203 { 17204 if (!this->has_keys_) 17205 { 17206 go_error_at(location, "map composite literal must have keys"); 17207 return Expression::make_error(location); 17208 } 17209 17210 for (Expression_list::iterator p = this->vals_->begin(); 17211 p != this->vals_->end(); 17212 p += 2) 17213 { 17214 if (*p == NULL) 17215 { 17216 ++p; 17217 go_error_at((*p)->location(), 17218 ("map composite literal must " 17219 "have keys for every value")); 17220 return Expression::make_error(location); 17221 } 17222 // Make sure we have lowered the key; it may not have been 17223 // lowered in order to handle keys for struct composite 17224 // literals. Lower it now to get the right error message. 17225 if ((*p)->unknown_expression() != NULL) 17226 { 17227 gogo->lower_expression(function, inserter, &*p); 17228 go_assert((*p)->is_error_expression()); 17229 return Expression::make_error(location); 17230 } 17231 // Check if there are duplicate constant keys. 17232 if (!(*p)->is_constant()) 17233 continue; 17234 std::string sval; 17235 Numeric_constant nval; 17236 bool bval; 17237 if ((*p)->string_constant_value(&sval)) // Check string keys. 17238 { 17239 unsigned int h = Gogo::hash_string(sval, 0); 17240 // Search the index h in the hash map. 17241 Unordered_map(unsigned int, std::vector<Expression*>)::iterator mit; 17242 mit = st.find(h); 17243 if (mit == st.end()) 17244 { 17245 // No duplicate since h is a new index. 17246 // Create a new vector indexed by h and add it to the hash map. 17247 std::vector<Expression*> l; 17248 l.push_back(*p); 17249 std::pair<unsigned int, std::vector<Expression*> > val(h, l); 17250 st.insert(val); 17251 } 17252 else 17253 { 17254 // Do further check since index h already exists. 17255 for (std::vector<Expression*>::iterator lit = 17256 mit->second.begin(); 17257 lit != mit->second.end(); 17258 lit++) 17259 { 17260 std::string s; 17261 bool ok = (*lit)->string_constant_value(&s); 17262 go_assert(ok); 17263 if (s == sval) 17264 { 17265 go_error_at((*p)->location(), ("duplicate key " 17266 "in map literal")); 17267 return Expression::make_error(location); 17268 } 17269 } 17270 // Add this new string key to the vector indexed by h. 17271 mit->second.push_back(*p); 17272 } 17273 } 17274 else if ((*p)->numeric_constant_value(&nval)) // Check numeric keys. 17275 { 17276 unsigned int h = nval.hash(0); 17277 Unordered_map(unsigned int, std::vector<Expression*>)::iterator mit; 17278 mit = nt.find(h); 17279 if (mit == nt.end()) 17280 { 17281 // No duplicate since h is a new code. 17282 // Create a new vector indexed by h and add it to the hash map. 17283 std::vector<Expression*> l; 17284 l.push_back(*p); 17285 std::pair<unsigned int, std::vector<Expression*> > val(h, l); 17286 nt.insert(val); 17287 } 17288 else 17289 { 17290 // Do further check since h already exists. 17291 for (std::vector<Expression*>::iterator lit = 17292 mit->second.begin(); 17293 lit != mit->second.end(); 17294 lit++) 17295 { 17296 Numeric_constant rval; 17297 bool ok = (*lit)->numeric_constant_value(&rval); 17298 go_assert(ok); 17299 if (nval.equals(rval)) 17300 { 17301 go_error_at((*p)->location(), 17302 "duplicate key in map literal"); 17303 return Expression::make_error(location); 17304 } 17305 } 17306 // Add this new numeric key to the vector indexed by h. 17307 mit->second.push_back(*p); 17308 } 17309 } 17310 else if ((*p)->boolean_constant_value(&bval)) 17311 { 17312 if ((bval && saw_true) || (!bval && saw_false)) 17313 { 17314 go_error_at((*p)->location(), 17315 "duplicate key in map literal"); 17316 return Expression::make_error(location); 17317 } 17318 if (bval) 17319 saw_true = true; 17320 else 17321 saw_false = true; 17322 } 17323 } 17324 } 17325 17326 return new Map_construction_expression(type, this->vals_, location); 17327 } 17328 17329 // Copy. 17330 17331 Expression* 17332 Composite_literal_expression::do_copy() 17333 { 17334 Composite_literal_expression* ret = 17335 new Composite_literal_expression(this->type_->copy_expressions(), 17336 this->depth_, this->has_keys_, 17337 (this->vals_ == NULL 17338 ? NULL 17339 : this->vals_->copy()), 17340 this->all_are_names_, 17341 this->location()); 17342 ret->key_path_ = this->key_path_; 17343 return ret; 17344 } 17345 17346 // Dump ast representation for a composite literal expression. 17347 17348 void 17349 Composite_literal_expression::do_dump_expression( 17350 Ast_dump_context* ast_dump_context) const 17351 { 17352 ast_dump_context->ostream() << "composite("; 17353 ast_dump_context->dump_type(this->type_); 17354 ast_dump_context->ostream() << ", {"; 17355 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); 17356 ast_dump_context->ostream() << "})"; 17357 } 17358 17359 // Make a composite literal expression. 17360 17361 Expression* 17362 Expression::make_composite_literal(Type* type, int depth, bool has_keys, 17363 Expression_list* vals, bool all_are_names, 17364 Location location) 17365 { 17366 return new Composite_literal_expression(type, depth, has_keys, vals, 17367 all_are_names, location); 17368 } 17369 17370 // Return whether this expression is a composite literal. 17371 17372 bool 17373 Expression::is_composite_literal() const 17374 { 17375 switch (this->classification_) 17376 { 17377 case EXPRESSION_COMPOSITE_LITERAL: 17378 case EXPRESSION_STRUCT_CONSTRUCTION: 17379 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: 17380 case EXPRESSION_SLICE_CONSTRUCTION: 17381 case EXPRESSION_MAP_CONSTRUCTION: 17382 return true; 17383 default: 17384 return false; 17385 } 17386 } 17387 17388 // Return whether this expression is a composite literal which is not 17389 // constant. 17390 17391 bool 17392 Expression::is_nonconstant_composite_literal() const 17393 { 17394 switch (this->classification_) 17395 { 17396 case EXPRESSION_STRUCT_CONSTRUCTION: 17397 { 17398 const Struct_construction_expression *psce = 17399 static_cast<const Struct_construction_expression*>(this); 17400 return !psce->is_constant_struct(); 17401 } 17402 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: 17403 { 17404 const Fixed_array_construction_expression *pace = 17405 static_cast<const Fixed_array_construction_expression*>(this); 17406 return !pace->is_constant_array(); 17407 } 17408 case EXPRESSION_SLICE_CONSTRUCTION: 17409 { 17410 const Slice_construction_expression *pace = 17411 static_cast<const Slice_construction_expression*>(this); 17412 return !pace->is_constant_array(); 17413 } 17414 case EXPRESSION_MAP_CONSTRUCTION: 17415 return true; 17416 default: 17417 return false; 17418 } 17419 } 17420 17421 // Return true if this is a variable or temporary_variable. 17422 17423 bool 17424 Expression::is_variable() const 17425 { 17426 switch (this->classification_) 17427 { 17428 case EXPRESSION_VAR_REFERENCE: 17429 case EXPRESSION_TEMPORARY_REFERENCE: 17430 case EXPRESSION_SET_AND_USE_TEMPORARY: 17431 case EXPRESSION_ENCLOSED_VAR_REFERENCE: 17432 return true; 17433 default: 17434 return false; 17435 } 17436 } 17437 17438 // Return true if this is a reference to a local variable. 17439 17440 bool 17441 Expression::is_local_variable() const 17442 { 17443 const Var_expression* ve = this->var_expression(); 17444 if (ve == NULL) 17445 return false; 17446 const Named_object* no = ve->named_object(); 17447 return (no->is_result_variable() 17448 || (no->is_variable() && !no->var_value()->is_global())); 17449 } 17450 17451 // Return true if multiple evaluations are OK. 17452 17453 bool 17454 Expression::is_multi_eval_safe() 17455 { 17456 switch (this->classification_) 17457 { 17458 case EXPRESSION_VAR_REFERENCE: 17459 { 17460 // A variable is a simple reference if not stored in the heap. 17461 const Named_object* no = this->var_expression()->named_object(); 17462 if (no->is_variable()) 17463 return !no->var_value()->is_in_heap(); 17464 else if (no->is_result_variable()) 17465 return !no->result_var_value()->is_in_heap(); 17466 else 17467 go_unreachable(); 17468 } 17469 17470 case EXPRESSION_TEMPORARY_REFERENCE: 17471 return true; 17472 17473 default: 17474 break; 17475 } 17476 17477 if (!this->is_constant()) 17478 return false; 17479 17480 // Only numeric and boolean constants are really multi-evaluation 17481 // safe. We don't want multiple copies of string constants. 17482 Type* type = this->type(); 17483 return type->is_numeric_type() || type->is_boolean_type(); 17484 } 17485 17486 const Named_object* 17487 Expression::named_constant() const 17488 { 17489 if (this->classification() != EXPRESSION_CONST_REFERENCE) 17490 return NULL; 17491 const Const_expression* ce = static_cast<const Const_expression*>(this); 17492 return ce->named_object(); 17493 } 17494 17495 // Class Type_guard_expression. 17496 17497 // Traversal. 17498 17499 int 17500 Type_guard_expression::do_traverse(Traverse* traverse) 17501 { 17502 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT 17503 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 17504 return TRAVERSE_EXIT; 17505 return TRAVERSE_CONTINUE; 17506 } 17507 17508 Expression* 17509 Type_guard_expression::do_flatten(Gogo*, Named_object*, 17510 Statement_inserter* inserter) 17511 { 17512 if (this->expr_->is_error_expression() 17513 || this->expr_->type()->is_error_type()) 17514 { 17515 go_assert(saw_errors()); 17516 return Expression::make_error(this->location()); 17517 } 17518 17519 if (!this->expr_->is_multi_eval_safe()) 17520 { 17521 Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, 17522 this->location()); 17523 inserter->insert(temp); 17524 this->expr_ = 17525 Expression::make_temporary_reference(temp, this->location()); 17526 } 17527 return this; 17528 } 17529 17530 // Check types of a type guard expression. The expression must have 17531 // an interface type, but the actual type conversion is checked at run 17532 // time. 17533 17534 void 17535 Type_guard_expression::do_check_types(Gogo*) 17536 { 17537 Type* expr_type = this->expr_->type(); 17538 if (expr_type->interface_type() == NULL) 17539 { 17540 if (!expr_type->is_error() && !this->type_->is_error()) 17541 this->report_error(_("type assertion only valid for interface types")); 17542 this->set_is_error(); 17543 } 17544 else if (this->type_->interface_type() == NULL) 17545 { 17546 std::string reason; 17547 if (!expr_type->interface_type()->implements_interface(this->type_, 17548 &reason)) 17549 { 17550 if (!this->type_->is_error()) 17551 { 17552 if (reason.empty()) 17553 this->report_error(_("impossible type assertion: " 17554 "type does not implement interface")); 17555 else 17556 go_error_at(this->location(), 17557 ("impossible type assertion: " 17558 "type does not implement interface (%s)"), 17559 reason.c_str()); 17560 } 17561 this->set_is_error(); 17562 } 17563 } 17564 } 17565 17566 // Copy. 17567 17568 Expression* 17569 Type_guard_expression::do_copy() 17570 { 17571 return new Type_guard_expression(this->expr_->copy(), 17572 this->type_->copy_expressions(), 17573 this->location()); 17574 } 17575 17576 // Return the backend representation for a type guard expression. 17577 17578 Bexpression* 17579 Type_guard_expression::do_get_backend(Translate_context* context) 17580 { 17581 Expression* conversion; 17582 if (this->type_->interface_type() != NULL) 17583 conversion = 17584 Expression::convert_interface_to_interface(this->type_, this->expr_, 17585 true, this->location()); 17586 else 17587 conversion = 17588 Expression::convert_for_assignment(context->gogo(), this->type_, 17589 this->expr_, this->location()); 17590 17591 Gogo* gogo = context->gogo(); 17592 Btype* bt = this->type_->get_backend(gogo); 17593 Bexpression* bexpr = conversion->get_backend(context); 17594 return gogo->backend()->convert_expression(bt, bexpr, this->location()); 17595 } 17596 17597 // Dump ast representation for a type guard expression. 17598 17599 void 17600 Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) 17601 const 17602 { 17603 this->expr_->dump_expression(ast_dump_context); 17604 ast_dump_context->ostream() << "."; 17605 ast_dump_context->dump_type(this->type_); 17606 } 17607 17608 // Make a type guard expression. 17609 17610 Expression* 17611 Expression::make_type_guard(Expression* expr, Type* type, 17612 Location location) 17613 { 17614 return new Type_guard_expression(expr, type, location); 17615 } 17616 17617 // Class Heap_expression. 17618 17619 // Return the type of the expression stored on the heap. 17620 17621 Type* 17622 Heap_expression::do_type() 17623 { return Type::make_pointer_type(this->expr_->type()); } 17624 17625 // Return the backend representation for allocating an expression on the heap. 17626 17627 Bexpression* 17628 Heap_expression::do_get_backend(Translate_context* context) 17629 { 17630 Type* etype = this->expr_->type(); 17631 if (this->expr_->is_error_expression() || etype->is_error()) 17632 return context->backend()->error_expression(); 17633 17634 Location loc = this->location(); 17635 Gogo* gogo = context->gogo(); 17636 Btype* btype = this->type()->get_backend(gogo); 17637 17638 Expression* alloc = Expression::make_allocation(etype, loc); 17639 if (this->allocate_on_stack_) 17640 alloc->allocation_expression()->set_allocate_on_stack(); 17641 Bexpression* space = alloc->get_backend(context); 17642 17643 Bstatement* decl; 17644 Named_object* fn = context->function(); 17645 go_assert(fn != NULL); 17646 Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); 17647 Bvariable* space_temp = 17648 gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, 17649 space, 17650 Backend::variable_address_is_taken, 17651 loc, &decl); 17652 Btype* expr_btype = etype->get_backend(gogo); 17653 17654 Bexpression* bexpr = this->expr_->get_backend(context); 17655 17656 // If this assignment needs a write barrier, call typedmemmove. We 17657 // don't do this in the write barrier pass because in some cases 17658 // backend conversion can introduce new Heap_expression values. 17659 Bstatement* assn; 17660 if (!etype->has_pointer() || this->allocate_on_stack_) 17661 { 17662 space = gogo->backend()->var_expression(space_temp, loc); 17663 Bexpression* ref = 17664 gogo->backend()->indirect_expression(expr_btype, space, true, loc); 17665 assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc); 17666 } 17667 else 17668 { 17669 Bstatement* edecl; 17670 Bvariable* btemp = 17671 gogo->backend()->temporary_variable(fndecl, context->bblock(), 17672 expr_btype, bexpr, 17673 Backend::variable_address_is_taken, 17674 loc, &edecl); 17675 Bexpression* btempref = gogo->backend()->var_expression(btemp, 17676 loc); 17677 space = gogo->backend()->var_expression(space_temp, loc); 17678 Type* etype_ptr = Type::make_pointer_type(etype); 17679 Expression* elhs = Expression::make_backend(space, etype_ptr, loc); 17680 Expression* erhs; 17681 Expression* call; 17682 if (etype->is_direct_iface_type()) 17683 { 17684 // Single pointer. 17685 Type* uintptr_type = Type::lookup_integer_type("uintptr"); 17686 erhs = Expression::make_backend(btempref, etype, loc); 17687 erhs = Expression::unpack_direct_iface(erhs, loc); 17688 erhs = Expression::make_unsafe_cast(uintptr_type, erhs, loc); 17689 call = Runtime::make_call(Runtime::GCWRITEBARRIER, loc, 2, 17690 elhs, erhs); 17691 } 17692 else 17693 { 17694 Expression* td = Expression::make_type_descriptor(etype, loc); 17695 Bexpression* addr = 17696 gogo->backend()->address_expression(btempref, loc); 17697 erhs = Expression::make_backend(addr, etype_ptr, loc); 17698 call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3, 17699 td, elhs, erhs); 17700 } 17701 Statement* cs = Statement::make_statement(call, false); 17702 17703 space = gogo->backend()->var_expression(space_temp, loc); 17704 Bexpression* ref = 17705 gogo->backend()->indirect_expression(expr_btype, space, true, loc); 17706 Expression* eref = Expression::make_backend(ref, etype, loc); 17707 btempref = gogo->backend()->var_expression(btemp, loc); 17708 erhs = Expression::make_backend(btempref, etype, loc); 17709 Statement* as = Statement::make_assignment(eref, erhs, loc); 17710 17711 as = gogo->check_write_barrier(context->block(), as, cs); 17712 Bstatement* s = as->get_backend(context); 17713 17714 assn = gogo->backend()->compound_statement(edecl, s); 17715 } 17716 decl = gogo->backend()->compound_statement(decl, assn); 17717 space = gogo->backend()->var_expression(space_temp, loc); 17718 return gogo->backend()->compound_expression(decl, space, loc); 17719 } 17720 17721 // Dump ast representation for a heap expression. 17722 17723 void 17724 Heap_expression::do_dump_expression( 17725 Ast_dump_context* ast_dump_context) const 17726 { 17727 ast_dump_context->ostream() << "&("; 17728 ast_dump_context->dump_expression(this->expr_); 17729 ast_dump_context->ostream() << ")"; 17730 } 17731 17732 // Allocate an expression on the heap. 17733 17734 Expression* 17735 Expression::make_heap_expression(Expression* expr, Location location) 17736 { 17737 return new Heap_expression(expr, location); 17738 } 17739 17740 // Class Receive_expression. 17741 17742 // Return the type of a receive expression. 17743 17744 Type* 17745 Receive_expression::do_type() 17746 { 17747 if (this->is_error_expression()) 17748 return Type::make_error_type(); 17749 Channel_type* channel_type = this->channel_->type()->channel_type(); 17750 if (channel_type == NULL) 17751 { 17752 this->report_error(_("expected channel")); 17753 return Type::make_error_type(); 17754 } 17755 return channel_type->element_type(); 17756 } 17757 17758 // Check types for a receive expression. 17759 17760 void 17761 Receive_expression::do_check_types(Gogo*) 17762 { 17763 Type* type = this->channel_->type(); 17764 if (type->is_error()) 17765 { 17766 go_assert(saw_errors()); 17767 this->set_is_error(); 17768 return; 17769 } 17770 if (type->channel_type() == NULL) 17771 { 17772 this->report_error(_("expected channel")); 17773 return; 17774 } 17775 if (!type->channel_type()->may_receive()) 17776 { 17777 this->report_error(_("invalid receive on send-only channel")); 17778 return; 17779 } 17780 } 17781 17782 // Flattening for receive expressions creates a temporary variable to store 17783 // received data in for receives. 17784 17785 Expression* 17786 Receive_expression::do_flatten(Gogo*, Named_object*, 17787 Statement_inserter* inserter) 17788 { 17789 Channel_type* channel_type = this->channel_->type()->channel_type(); 17790 if (channel_type == NULL) 17791 { 17792 go_assert(saw_errors()); 17793 return this; 17794 } 17795 else if (this->channel_->is_error_expression()) 17796 { 17797 go_assert(saw_errors()); 17798 return Expression::make_error(this->location()); 17799 } 17800 17801 Type* element_type = channel_type->element_type(); 17802 if (this->temp_receiver_ == NULL) 17803 { 17804 this->temp_receiver_ = Statement::make_temporary(element_type, NULL, 17805 this->location()); 17806 this->temp_receiver_->set_is_address_taken(); 17807 inserter->insert(this->temp_receiver_); 17808 } 17809 17810 return this; 17811 } 17812 17813 // Get the backend representation for a receive expression. 17814 17815 Bexpression* 17816 Receive_expression::do_get_backend(Translate_context* context) 17817 { 17818 Location loc = this->location(); 17819 17820 Channel_type* channel_type = this->channel_->type()->channel_type(); 17821 if (channel_type == NULL) 17822 { 17823 go_assert(this->channel_->type()->is_error()); 17824 return context->backend()->error_expression(); 17825 } 17826 17827 Expression* recv_ref = 17828 Expression::make_temporary_reference(this->temp_receiver_, loc); 17829 Expression* recv_addr = 17830 Expression::make_temporary_reference(this->temp_receiver_, loc); 17831 recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); 17832 Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 2, 17833 this->channel_, recv_addr); 17834 return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); 17835 } 17836 17837 // Export a receive expression. 17838 17839 void 17840 Receive_expression::do_export(Export_function_body* efb) const 17841 { 17842 efb->write_c_string("<-"); 17843 this->channel_->export_expression(efb); 17844 } 17845 17846 // Dump ast representation for a receive expression. 17847 17848 void 17849 Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 17850 { 17851 ast_dump_context->ostream() << " <- " ; 17852 ast_dump_context->dump_expression(channel_); 17853 } 17854 17855 // Import a receive expression. 17856 17857 Expression* 17858 Receive_expression::do_import(Import_expression* imp, Location loc) 17859 { 17860 imp->require_c_string("<-"); 17861 Expression* expr = Expression::import_expression(imp, loc); 17862 return Expression::make_receive(expr, loc); 17863 } 17864 17865 // Make a receive expression. 17866 17867 Receive_expression* 17868 Expression::make_receive(Expression* channel, Location location) 17869 { 17870 return new Receive_expression(channel, location); 17871 } 17872 17873 // An expression which evaluates to a pointer to the type descriptor 17874 // of a type. 17875 17876 class Type_descriptor_expression : public Expression 17877 { 17878 public: 17879 Type_descriptor_expression(Type* type, Location location) 17880 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), 17881 type_(type) 17882 { } 17883 17884 protected: 17885 int 17886 do_traverse(Traverse*); 17887 17888 Type* 17889 do_type() 17890 { return Type::make_type_descriptor_ptr_type(); } 17891 17892 bool 17893 do_is_static_initializer() const 17894 { return true; } 17895 17896 void 17897 do_determine_type(const Type_context*) 17898 { } 17899 17900 Expression* 17901 do_copy() 17902 { return this; } 17903 17904 Bexpression* 17905 do_get_backend(Translate_context* context) 17906 { 17907 return this->type_->type_descriptor_pointer(context->gogo(), 17908 this->location()); 17909 } 17910 17911 void 17912 do_dump_expression(Ast_dump_context*) const; 17913 17914 private: 17915 // The type for which this is the descriptor. 17916 Type* type_; 17917 }; 17918 17919 int 17920 Type_descriptor_expression::do_traverse(Traverse* traverse) 17921 { 17922 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 17923 return TRAVERSE_EXIT; 17924 return TRAVERSE_CONTINUE; 17925 } 17926 17927 // Dump ast representation for a type descriptor expression. 17928 17929 void 17930 Type_descriptor_expression::do_dump_expression( 17931 Ast_dump_context* ast_dump_context) const 17932 { 17933 ast_dump_context->dump_type(this->type_); 17934 } 17935 17936 // Make a type descriptor expression. 17937 17938 Expression* 17939 Expression::make_type_descriptor(Type* type, Location location) 17940 { 17941 return new Type_descriptor_expression(type, location); 17942 } 17943 17944 // An expression which evaluates to a pointer to the Garbage Collection symbol 17945 // of a type. 17946 17947 class GC_symbol_expression : public Expression 17948 { 17949 public: 17950 GC_symbol_expression(Type* type) 17951 : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), 17952 type_(type) 17953 {} 17954 17955 protected: 17956 Type* 17957 do_type() 17958 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } 17959 17960 bool 17961 do_is_static_initializer() const 17962 { return true; } 17963 17964 void 17965 do_determine_type(const Type_context*) 17966 { } 17967 17968 Expression* 17969 do_copy() 17970 { return this; } 17971 17972 Bexpression* 17973 do_get_backend(Translate_context* context) 17974 { return this->type_->gc_symbol_pointer(context->gogo()); } 17975 17976 void 17977 do_dump_expression(Ast_dump_context*) const; 17978 17979 private: 17980 // The type which this gc symbol describes. 17981 Type* type_; 17982 }; 17983 17984 // Dump ast representation for a gc symbol expression. 17985 17986 void 17987 GC_symbol_expression::do_dump_expression( 17988 Ast_dump_context* ast_dump_context) const 17989 { 17990 ast_dump_context->ostream() << "gcdata("; 17991 ast_dump_context->dump_type(this->type_); 17992 ast_dump_context->ostream() << ")"; 17993 } 17994 17995 // Make a gc symbol expression. 17996 17997 Expression* 17998 Expression::make_gc_symbol(Type* type) 17999 { 18000 return new GC_symbol_expression(type); 18001 } 18002 18003 // An expression that evaluates to a pointer to a symbol holding the 18004 // ptrmask data of a type. 18005 18006 class Ptrmask_symbol_expression : public Expression 18007 { 18008 public: 18009 Ptrmask_symbol_expression(Type* type) 18010 : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()), 18011 type_(type) 18012 {} 18013 18014 protected: 18015 Type* 18016 do_type() 18017 { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } 18018 18019 bool 18020 do_is_static_initializer() const 18021 { return true; } 18022 18023 void 18024 do_determine_type(const Type_context*) 18025 { } 18026 18027 Expression* 18028 do_copy() 18029 { return this; } 18030 18031 Bexpression* 18032 do_get_backend(Translate_context*); 18033 18034 void 18035 do_dump_expression(Ast_dump_context*) const; 18036 18037 private: 18038 // The type that this ptrmask symbol describes. 18039 Type* type_; 18040 }; 18041 18042 // Return the ptrmask variable. 18043 18044 Bexpression* 18045 Ptrmask_symbol_expression::do_get_backend(Translate_context* context) 18046 { 18047 Gogo* gogo = context->gogo(); 18048 18049 // If this type does not need a gcprog, then we can use the standard 18050 // GC symbol. 18051 int64_t ptrsize, ptrdata; 18052 if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata)) 18053 return this->type_->gc_symbol_pointer(gogo); 18054 18055 // Otherwise we have to build a ptrmask variable, and return a 18056 // pointer to it. 18057 18058 Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata); 18059 Location bloc = Linemap::predeclared_location(); 18060 Bexpression* bref = gogo->backend()->var_expression(bvar, bloc); 18061 Bexpression* baddr = gogo->backend()->address_expression(bref, bloc); 18062 18063 Type* uint8_type = Type::lookup_integer_type("uint8"); 18064 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type); 18065 Btype* ubtype = pointer_uint8_type->get_backend(gogo); 18066 return gogo->backend()->convert_expression(ubtype, baddr, bloc); 18067 } 18068 18069 // Dump AST for a ptrmask symbol expression. 18070 18071 void 18072 Ptrmask_symbol_expression::do_dump_expression( 18073 Ast_dump_context* ast_dump_context) const 18074 { 18075 ast_dump_context->ostream() << "ptrmask("; 18076 ast_dump_context->dump_type(this->type_); 18077 ast_dump_context->ostream() << ")"; 18078 } 18079 18080 // Make a ptrmask symbol expression. 18081 18082 Expression* 18083 Expression::make_ptrmask_symbol(Type* type) 18084 { 18085 return new Ptrmask_symbol_expression(type); 18086 } 18087 18088 // An expression which evaluates to some characteristic of a type. 18089 // This is only used to initialize fields of a type descriptor. Using 18090 // a new expression class is slightly inefficient but gives us a good 18091 // separation between the frontend and the middle-end with regard to 18092 // how types are laid out. 18093 18094 class Type_info_expression : public Expression 18095 { 18096 public: 18097 Type_info_expression(Type* type, Type_info type_info) 18098 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), 18099 type_(type), type_info_(type_info) 18100 { } 18101 18102 protected: 18103 bool 18104 do_is_static_initializer() const 18105 { return true; } 18106 18107 Type* 18108 do_type(); 18109 18110 void 18111 do_determine_type(const Type_context*) 18112 { } 18113 18114 Expression* 18115 do_copy() 18116 { return this; } 18117 18118 Bexpression* 18119 do_get_backend(Translate_context* context); 18120 18121 void 18122 do_dump_expression(Ast_dump_context*) const; 18123 18124 private: 18125 // The type for which we are getting information. 18126 Type* type_; 18127 // What information we want. 18128 Type_info type_info_; 18129 }; 18130 18131 // The type is chosen to match what the type descriptor struct 18132 // expects. 18133 18134 Type* 18135 Type_info_expression::do_type() 18136 { 18137 switch (this->type_info_) 18138 { 18139 case TYPE_INFO_SIZE: 18140 case TYPE_INFO_BACKEND_PTRDATA: 18141 case TYPE_INFO_DESCRIPTOR_PTRDATA: 18142 return Type::lookup_integer_type("uintptr"); 18143 case TYPE_INFO_ALIGNMENT: 18144 case TYPE_INFO_FIELD_ALIGNMENT: 18145 return Type::lookup_integer_type("uint8"); 18146 default: 18147 go_unreachable(); 18148 } 18149 } 18150 18151 // Return the backend representation for type information. 18152 18153 Bexpression* 18154 Type_info_expression::do_get_backend(Translate_context* context) 18155 { 18156 Gogo* gogo = context->gogo(); 18157 bool ok = true; 18158 int64_t val; 18159 switch (this->type_info_) 18160 { 18161 case TYPE_INFO_SIZE: 18162 ok = this->type_->backend_type_size(gogo, &val); 18163 break; 18164 case TYPE_INFO_ALIGNMENT: 18165 ok = this->type_->backend_type_align(gogo, &val); 18166 break; 18167 case TYPE_INFO_FIELD_ALIGNMENT: 18168 ok = this->type_->backend_type_field_align(gogo, &val); 18169 break; 18170 case TYPE_INFO_BACKEND_PTRDATA: 18171 ok = this->type_->backend_type_ptrdata(gogo, &val); 18172 break; 18173 case TYPE_INFO_DESCRIPTOR_PTRDATA: 18174 ok = this->type_->descriptor_ptrdata(gogo, &val); 18175 break; 18176 default: 18177 go_unreachable(); 18178 } 18179 if (!ok) 18180 { 18181 go_assert(saw_errors()); 18182 return gogo->backend()->error_expression(); 18183 } 18184 Expression* e = Expression::make_integer_int64(val, this->type(), 18185 this->location()); 18186 return e->get_backend(context); 18187 } 18188 18189 // Dump ast representation for a type info expression. 18190 18191 void 18192 Type_info_expression::do_dump_expression( 18193 Ast_dump_context* ast_dump_context) const 18194 { 18195 ast_dump_context->ostream() << "typeinfo("; 18196 ast_dump_context->dump_type(this->type_); 18197 ast_dump_context->ostream() << ","; 18198 ast_dump_context->ostream() << 18199 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" 18200 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" 18201 : this->type_info_ == TYPE_INFO_SIZE ? "size" 18202 : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata" 18203 : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata" 18204 : "unknown"); 18205 ast_dump_context->ostream() << ")"; 18206 } 18207 18208 // Make a type info expression. 18209 18210 Expression* 18211 Expression::make_type_info(Type* type, Type_info type_info) 18212 { 18213 return new Type_info_expression(type, type_info); 18214 } 18215 18216 // Slice_info_expression. 18217 18218 // Return the type of the slice info. 18219 18220 Type* 18221 Slice_info_expression::do_type() 18222 { 18223 switch (this->slice_info_) 18224 { 18225 case SLICE_INFO_VALUE_POINTER: 18226 return Type::make_pointer_type( 18227 this->slice_->type()->array_type()->element_type()); 18228 case SLICE_INFO_LENGTH: 18229 case SLICE_INFO_CAPACITY: 18230 return Type::lookup_integer_type("int"); 18231 default: 18232 go_unreachable(); 18233 } 18234 } 18235 18236 // Return the backend information for slice information. 18237 18238 Bexpression* 18239 Slice_info_expression::do_get_backend(Translate_context* context) 18240 { 18241 Gogo* gogo = context->gogo(); 18242 Bexpression* bslice = this->slice_->get_backend(context); 18243 switch (this->slice_info_) 18244 { 18245 case SLICE_INFO_VALUE_POINTER: 18246 case SLICE_INFO_LENGTH: 18247 case SLICE_INFO_CAPACITY: 18248 return gogo->backend()->struct_field_expression(bslice, this->slice_info_, 18249 this->location()); 18250 break; 18251 default: 18252 go_unreachable(); 18253 } 18254 } 18255 18256 // Dump ast representation for a type info expression. 18257 18258 void 18259 Slice_info_expression::do_dump_expression( 18260 Ast_dump_context* ast_dump_context) const 18261 { 18262 ast_dump_context->ostream() << "sliceinfo("; 18263 this->slice_->dump_expression(ast_dump_context); 18264 ast_dump_context->ostream() << ","; 18265 ast_dump_context->ostream() << 18266 (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" 18267 : this->slice_info_ == SLICE_INFO_LENGTH ? "length" 18268 : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " 18269 : "unknown"); 18270 ast_dump_context->ostream() << ")"; 18271 } 18272 18273 // Make a slice info expression. 18274 18275 Expression* 18276 Expression::make_slice_info(Expression* slice, Slice_info slice_info, 18277 Location location) 18278 { 18279 return new Slice_info_expression(slice, slice_info, location); 18280 } 18281 18282 // Class Slice_value_expression. 18283 18284 int 18285 Slice_value_expression::do_traverse(Traverse* traverse) 18286 { 18287 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT 18288 || Expression::traverse(&this->valmem_, traverse) == TRAVERSE_EXIT 18289 || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT 18290 || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) 18291 return TRAVERSE_EXIT; 18292 return TRAVERSE_CONTINUE; 18293 } 18294 18295 Expression* 18296 Slice_value_expression::do_copy() 18297 { 18298 return new Slice_value_expression(this->type_->copy_expressions(), 18299 this->valmem_->copy(), 18300 this->len_->copy(), this->cap_->copy(), 18301 this->location()); 18302 } 18303 18304 Bexpression* 18305 Slice_value_expression::do_get_backend(Translate_context* context) 18306 { 18307 std::vector<Bexpression*> vals(3); 18308 vals[0] = this->valmem_->get_backend(context); 18309 vals[1] = this->len_->get_backend(context); 18310 vals[2] = this->cap_->get_backend(context); 18311 18312 Gogo* gogo = context->gogo(); 18313 Btype* btype = this->type_->get_backend(gogo); 18314 return gogo->backend()->constructor_expression(btype, vals, this->location()); 18315 } 18316 18317 void 18318 Slice_value_expression::do_dump_expression( 18319 Ast_dump_context* ast_dump_context) const 18320 { 18321 ast_dump_context->ostream() << "slicevalue("; 18322 ast_dump_context->ostream() << "values: "; 18323 this->valmem_->dump_expression(ast_dump_context); 18324 ast_dump_context->ostream() << ", length: "; 18325 this->len_->dump_expression(ast_dump_context); 18326 ast_dump_context->ostream() << ", capacity: "; 18327 this->cap_->dump_expression(ast_dump_context); 18328 ast_dump_context->ostream() << ")"; 18329 } 18330 18331 Expression* 18332 Expression::make_slice_value(Type* at, Expression* valmem, Expression* len, 18333 Expression* cap, Location location) 18334 { 18335 go_assert(at->is_slice_type()); 18336 go_assert(valmem->is_nil_expression() 18337 || (at->array_type()->element_type() 18338 == valmem->type()->points_to())); 18339 return new Slice_value_expression(at, valmem, len, cap, location); 18340 } 18341 18342 // Look through the expression of a Slice_value_expression's valmem to 18343 // find an call to makeslice. If found, return the call expression and 18344 // the containing temporary statement (if any). 18345 18346 std::pair<Call_expression*, Temporary_statement*> 18347 Expression::find_makeslice_call(Expression* expr) 18348 { 18349 Unsafe_type_conversion_expression* utce = 18350 expr->unsafe_conversion_expression(); 18351 if (utce != NULL) 18352 expr = utce->expr(); 18353 18354 Slice_value_expression* sve = expr->slice_value_expression(); 18355 if (sve == NULL) 18356 return std::make_pair<Call_expression*, Temporary_statement*>(NULL, NULL); 18357 expr = sve->valmem(); 18358 18359 utce = expr->unsafe_conversion_expression(); 18360 if (utce != NULL) 18361 expr = utce->expr(); 18362 18363 Temporary_reference_expression* tre = expr->temporary_reference_expression(); 18364 Temporary_statement* ts = (tre != NULL ? tre->statement() : NULL); 18365 if (ts != NULL && ts->init() != NULL && !ts->assigned() 18366 && !ts->is_address_taken()) 18367 expr = ts->init(); 18368 18369 Call_expression* call = expr->call_expression(); 18370 if (call == NULL) 18371 return std::make_pair<Call_expression*, Temporary_statement*>(NULL, NULL); 18372 18373 Func_expression* fe = call->fn()->func_expression(); 18374 if (fe != NULL 18375 && fe->runtime_code() == Runtime::MAKESLICE) 18376 return std::make_pair(call, ts); 18377 18378 return std::make_pair<Call_expression*, Temporary_statement*>(NULL, NULL); 18379 } 18380 18381 // An expression that evaluates to some characteristic of a non-empty interface. 18382 // This is used to access the method table or underlying object of an interface. 18383 18384 class Interface_info_expression : public Expression 18385 { 18386 public: 18387 Interface_info_expression(Expression* iface, Interface_info iface_info, 18388 Location location) 18389 : Expression(EXPRESSION_INTERFACE_INFO, location), 18390 iface_(iface), iface_info_(iface_info) 18391 { } 18392 18393 protected: 18394 Type* 18395 do_type(); 18396 18397 void 18398 do_determine_type(const Type_context*) 18399 { } 18400 18401 Expression* 18402 do_copy() 18403 { 18404 return new Interface_info_expression(this->iface_->copy(), 18405 this->iface_info_, this->location()); 18406 } 18407 18408 Bexpression* 18409 do_get_backend(Translate_context* context); 18410 18411 void 18412 do_dump_expression(Ast_dump_context*) const; 18413 18414 void 18415 do_issue_nil_check() 18416 { this->iface_->issue_nil_check(); } 18417 18418 private: 18419 // The interface for which we are getting information. 18420 Expression* iface_; 18421 // What information we want. 18422 Interface_info iface_info_; 18423 }; 18424 18425 // Return the type of the interface info. 18426 18427 Type* 18428 Interface_info_expression::do_type() 18429 { 18430 switch (this->iface_info_) 18431 { 18432 case INTERFACE_INFO_METHODS: 18433 { 18434 typedef Unordered_map(Interface_type*, Type*) Hashtable; 18435 static Hashtable result_types; 18436 18437 Interface_type* itype = this->iface_->type()->interface_type(); 18438 18439 Hashtable::const_iterator pr = result_types.find(itype); 18440 if (pr != result_types.end()) 18441 return pr->second; 18442 18443 Type* pdt = Type::make_type_descriptor_ptr_type(); 18444 if (itype->is_empty()) 18445 { 18446 result_types[itype] = pdt; 18447 return pdt; 18448 } 18449 18450 Location loc = this->location(); 18451 Struct_field_list* sfl = new Struct_field_list(); 18452 sfl->push_back( 18453 Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); 18454 18455 for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); 18456 p != itype->methods()->end(); 18457 ++p) 18458 { 18459 Function_type* ft = p->type()->function_type(); 18460 go_assert(ft->receiver() == NULL); 18461 18462 const Typed_identifier_list* params = ft->parameters(); 18463 Typed_identifier_list* mparams = new Typed_identifier_list(); 18464 if (params != NULL) 18465 mparams->reserve(params->size() + 1); 18466 Type* vt = Type::make_pointer_type(Type::make_void_type()); 18467 mparams->push_back(Typed_identifier("", vt, ft->location())); 18468 if (params != NULL) 18469 { 18470 for (Typed_identifier_list::const_iterator pp = params->begin(); 18471 pp != params->end(); 18472 ++pp) 18473 mparams->push_back(*pp); 18474 } 18475 18476 Typed_identifier_list* mresults = (ft->results() == NULL 18477 ? NULL 18478 : ft->results()->copy()); 18479 Backend_function_type* mft = 18480 Type::make_backend_function_type(NULL, mparams, mresults, 18481 ft->location()); 18482 18483 std::string fname = Gogo::unpack_hidden_name(p->name()); 18484 sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); 18485 } 18486 18487 Struct_type* st = Type::make_struct_type(sfl, loc); 18488 st->set_is_struct_incomparable(); 18489 Pointer_type *pt = Type::make_pointer_type(st); 18490 result_types[itype] = pt; 18491 return pt; 18492 } 18493 case INTERFACE_INFO_OBJECT: 18494 return Type::make_pointer_type(Type::make_void_type()); 18495 default: 18496 go_unreachable(); 18497 } 18498 } 18499 18500 // Return the backend representation for interface information. 18501 18502 Bexpression* 18503 Interface_info_expression::do_get_backend(Translate_context* context) 18504 { 18505 Gogo* gogo = context->gogo(); 18506 Bexpression* biface = this->iface_->get_backend(context); 18507 switch (this->iface_info_) 18508 { 18509 case INTERFACE_INFO_METHODS: 18510 case INTERFACE_INFO_OBJECT: 18511 return gogo->backend()->struct_field_expression(biface, this->iface_info_, 18512 this->location()); 18513 break; 18514 default: 18515 go_unreachable(); 18516 } 18517 } 18518 18519 // Dump ast representation for an interface info expression. 18520 18521 void 18522 Interface_info_expression::do_dump_expression( 18523 Ast_dump_context* ast_dump_context) const 18524 { 18525 bool is_empty = this->iface_->type()->interface_type()->is_empty(); 18526 ast_dump_context->ostream() << "interfaceinfo("; 18527 this->iface_->dump_expression(ast_dump_context); 18528 ast_dump_context->ostream() << ","; 18529 ast_dump_context->ostream() << 18530 (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" 18531 : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" 18532 : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" 18533 : "unknown"); 18534 ast_dump_context->ostream() << ")"; 18535 } 18536 18537 // Make an interface info expression. 18538 18539 Expression* 18540 Expression::make_interface_info(Expression* iface, Interface_info iface_info, 18541 Location location) 18542 { 18543 return new Interface_info_expression(iface, iface_info, location); 18544 } 18545 18546 // An expression that represents an interface value. The first field is either 18547 // a type descriptor for an empty interface or a pointer to the interface method 18548 // table for a non-empty interface. The second field is always the object. 18549 18550 class Interface_value_expression : public Expression 18551 { 18552 public: 18553 Interface_value_expression(Type* type, Expression* first_field, 18554 Expression* obj, Location location) 18555 : Expression(EXPRESSION_INTERFACE_VALUE, location), 18556 type_(type), first_field_(first_field), obj_(obj) 18557 { } 18558 18559 protected: 18560 int 18561 do_traverse(Traverse*); 18562 18563 Type* 18564 do_type() 18565 { return this->type_; } 18566 18567 void 18568 do_determine_type(const Type_context*) 18569 { go_unreachable(); } 18570 18571 Expression* 18572 do_copy() 18573 { 18574 return new Interface_value_expression(this->type_->copy_expressions(), 18575 this->first_field_->copy(), 18576 this->obj_->copy(), this->location()); 18577 } 18578 18579 Bexpression* 18580 do_get_backend(Translate_context* context); 18581 18582 void 18583 do_dump_expression(Ast_dump_context*) const; 18584 18585 private: 18586 // The type of the interface value. 18587 Type* type_; 18588 // The first field of the interface (either a type descriptor or a pointer 18589 // to the method table. 18590 Expression* first_field_; 18591 // The underlying object of the interface. 18592 Expression* obj_; 18593 }; 18594 18595 int 18596 Interface_value_expression::do_traverse(Traverse* traverse) 18597 { 18598 if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT 18599 || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) 18600 return TRAVERSE_EXIT; 18601 return TRAVERSE_CONTINUE; 18602 } 18603 18604 Bexpression* 18605 Interface_value_expression::do_get_backend(Translate_context* context) 18606 { 18607 std::vector<Bexpression*> vals(2); 18608 vals[0] = this->first_field_->get_backend(context); 18609 vals[1] = this->obj_->get_backend(context); 18610 18611 Gogo* gogo = context->gogo(); 18612 Btype* btype = this->type_->get_backend(gogo); 18613 return gogo->backend()->constructor_expression(btype, vals, this->location()); 18614 } 18615 18616 void 18617 Interface_value_expression::do_dump_expression( 18618 Ast_dump_context* ast_dump_context) const 18619 { 18620 ast_dump_context->ostream() << "interfacevalue("; 18621 ast_dump_context->ostream() << 18622 (this->type_->interface_type()->is_empty() 18623 ? "type_descriptor: " 18624 : "methods: "); 18625 this->first_field_->dump_expression(ast_dump_context); 18626 ast_dump_context->ostream() << ", object: "; 18627 this->obj_->dump_expression(ast_dump_context); 18628 ast_dump_context->ostream() << ")"; 18629 } 18630 18631 Expression* 18632 Expression::make_interface_value(Type* type, Expression* first_value, 18633 Expression* object, Location location) 18634 { 18635 return new Interface_value_expression(type, first_value, object, location); 18636 } 18637 18638 // An interface method table for a pair of types: an interface type and a type 18639 // that implements that interface. 18640 18641 class Interface_mtable_expression : public Expression 18642 { 18643 public: 18644 Interface_mtable_expression(Interface_type* itype, Type* type, 18645 bool is_pointer, Location location) 18646 : Expression(EXPRESSION_INTERFACE_MTABLE, location), 18647 itype_(itype), type_(type), is_pointer_(is_pointer), 18648 method_table_type_(NULL), bvar_(NULL) 18649 { } 18650 18651 protected: 18652 int 18653 do_traverse(Traverse*); 18654 18655 Type* 18656 do_type(); 18657 18658 bool 18659 do_is_static_initializer() const 18660 { return true; } 18661 18662 void 18663 do_determine_type(const Type_context*) 18664 { go_unreachable(); } 18665 18666 Expression* 18667 do_copy() 18668 { 18669 Interface_type* itype = this->itype_->copy_expressions()->interface_type(); 18670 return new Interface_mtable_expression(itype, 18671 this->type_->copy_expressions(), 18672 this->is_pointer_, this->location()); 18673 } 18674 18675 bool 18676 do_is_addressable() const 18677 { return true; } 18678 18679 Bexpression* 18680 do_get_backend(Translate_context* context); 18681 18682 void 18683 do_dump_expression(Ast_dump_context*) const; 18684 18685 private: 18686 // The interface type for which the methods are defined. 18687 Interface_type* itype_; 18688 // The type to construct the interface method table for. 18689 Type* type_; 18690 // Whether this table contains the method set for the receiver type or the 18691 // pointer receiver type. 18692 bool is_pointer_; 18693 // The type of the method table. 18694 Type* method_table_type_; 18695 // The backend variable that refers to the interface method table. 18696 Bvariable* bvar_; 18697 }; 18698 18699 int 18700 Interface_mtable_expression::do_traverse(Traverse* traverse) 18701 { 18702 if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT 18703 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) 18704 return TRAVERSE_EXIT; 18705 return TRAVERSE_CONTINUE; 18706 } 18707 18708 Type* 18709 Interface_mtable_expression::do_type() 18710 { 18711 if (this->method_table_type_ != NULL) 18712 return this->method_table_type_; 18713 18714 const Typed_identifier_list* interface_methods = this->itype_->methods(); 18715 go_assert(!interface_methods->empty()); 18716 18717 Struct_field_list* sfl = new Struct_field_list; 18718 Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), 18719 this->location()); 18720 sfl->push_back(Struct_field(tid)); 18721 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); 18722 for (Typed_identifier_list::const_iterator p = interface_methods->begin(); 18723 p != interface_methods->end(); 18724 ++p) 18725 { 18726 // We want C function pointers here, not func descriptors; model 18727 // using void* pointers. 18728 Typed_identifier method(p->name(), unsafe_ptr_type, p->location()); 18729 sfl->push_back(Struct_field(method)); 18730 } 18731 Struct_type* st = Type::make_struct_type(sfl, this->location()); 18732 st->set_is_struct_incomparable(); 18733 this->method_table_type_ = st; 18734 return this->method_table_type_; 18735 } 18736 18737 Bexpression* 18738 Interface_mtable_expression::do_get_backend(Translate_context* context) 18739 { 18740 Gogo* gogo = context->gogo(); 18741 Location loc = Linemap::predeclared_location(); 18742 if (this->bvar_ != NULL) 18743 return gogo->backend()->var_expression(this->bvar_, this->location()); 18744 18745 const Typed_identifier_list* interface_methods = this->itype_->methods(); 18746 go_assert(!interface_methods->empty()); 18747 18748 std::string mangled_name = 18749 gogo->interface_method_table_name(this->itype_, this->type_, 18750 this->is_pointer_); 18751 18752 // Set is_public if we are converting a named type to an interface 18753 // type that is defined in the same package as the named type, and 18754 // the interface has hidden methods. In that case the interface 18755 // method table will be defined by the package that defines the 18756 // types. 18757 bool is_public = false; 18758 if (this->type_->named_type() != NULL 18759 && (this->type_->named_type()->named_object()->package() 18760 == this->itype_->package())) 18761 { 18762 for (Typed_identifier_list::const_iterator p = interface_methods->begin(); 18763 p != interface_methods->end(); 18764 ++p) 18765 { 18766 if (Gogo::is_hidden_name(p->name())) 18767 { 18768 is_public = true; 18769 break; 18770 } 18771 } 18772 } 18773 18774 if (is_public 18775 && this->type_->named_type()->named_object()->package() != NULL) 18776 { 18777 // The interface conversion table is defined elsewhere. 18778 Btype* btype = this->type()->get_backend(gogo); 18779 this->bvar_ = 18780 gogo->backend()->immutable_struct_reference(mangled_name, "", 18781 btype, loc); 18782 return gogo->backend()->var_expression(this->bvar_, this->location()); 18783 } 18784 18785 // The first element is the type descriptor. 18786 Type* td_type; 18787 if (!this->is_pointer_) 18788 td_type = this->type_; 18789 else 18790 td_type = Type::make_pointer_type(this->type_); 18791 18792 std::vector<Backend::Btyped_identifier> bstructfields; 18793 18794 // Build an interface method table for a type: a type descriptor followed by a 18795 // list of function pointers, one for each interface method. This is used for 18796 // interfaces. 18797 Expression_list* svals = new Expression_list(); 18798 Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc); 18799 svals->push_back(tdescriptor); 18800 18801 Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo); 18802 Backend::Btyped_identifier btd("_type", tdesc_btype, loc); 18803 bstructfields.push_back(btd); 18804 18805 Named_type* nt = this->type_->named_type(); 18806 Struct_type* st = this->type_->struct_type(); 18807 go_assert(nt != NULL || st != NULL); 18808 18809 for (Typed_identifier_list::const_iterator p = interface_methods->begin(); 18810 p != interface_methods->end(); 18811 ++p) 18812 { 18813 bool is_ambiguous; 18814 Method* m; 18815 if (nt != NULL) 18816 m = nt->method_function(p->name(), &is_ambiguous); 18817 else 18818 m = st->method_function(p->name(), &is_ambiguous); 18819 go_assert(m != NULL); 18820 18821 // See the comment in Type::method_constructor. 18822 bool use_direct_iface_stub = false; 18823 if (m->is_value_method() 18824 && this->is_pointer_ 18825 && this->type_->is_direct_iface_type()) 18826 use_direct_iface_stub = true; 18827 if (!m->is_value_method() 18828 && this->is_pointer_ 18829 && !this->type_->in_heap()) 18830 use_direct_iface_stub = true; 18831 Named_object* no = (use_direct_iface_stub 18832 ? m->iface_stub_object() 18833 : m->named_object()); 18834 18835 go_assert(no->is_function() || no->is_function_declaration()); 18836 18837 Function_type* fcn_type = (no->is_function() 18838 ? no->func_value()->type() 18839 : no->func_declaration_value()->type()); 18840 Btype* fcn_btype = fcn_type->get_backend_fntype(gogo); 18841 Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc); 18842 bstructfields.push_back(bmtype); 18843 18844 svals->push_back(Expression::make_func_code_reference(no, loc)); 18845 } 18846 18847 Btype *btype = gogo->backend()->struct_type(bstructfields); 18848 std::vector<Bexpression*> ctor_bexprs; 18849 for (Expression_list::const_iterator pe = svals->begin(); 18850 pe != svals->end(); 18851 ++pe) 18852 { 18853 ctor_bexprs.push_back((*pe)->get_backend(context)); 18854 } 18855 Bexpression* ctor = 18856 gogo->backend()->constructor_expression(btype, ctor_bexprs, loc); 18857 18858 unsigned int flags = 0; 18859 if (!is_public) 18860 flags |= Backend::variable_is_hidden; 18861 this->bvar_ = gogo->backend()->immutable_struct(mangled_name, "", flags, 18862 btype, loc); 18863 gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, flags, 18864 btype, loc, ctor); 18865 return gogo->backend()->var_expression(this->bvar_, loc); 18866 } 18867 18868 void 18869 Interface_mtable_expression::do_dump_expression( 18870 Ast_dump_context* ast_dump_context) const 18871 { 18872 ast_dump_context->ostream() << "__go_" 18873 << (this->is_pointer_ ? "pimt__" : "imt_"); 18874 ast_dump_context->dump_type(this->itype_); 18875 ast_dump_context->ostream() << "__"; 18876 ast_dump_context->dump_type(this->type_); 18877 } 18878 18879 Expression* 18880 Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, 18881 bool is_pointer, Location location) 18882 { 18883 return new Interface_mtable_expression(itype, type, is_pointer, location); 18884 } 18885 18886 // An expression which evaluates to the offset of a field within a 18887 // struct. This, like Type_info_expression, q.v., is only used to 18888 // initialize fields of a type descriptor. 18889 18890 class Struct_field_offset_expression : public Expression 18891 { 18892 public: 18893 Struct_field_offset_expression(Struct_type* type, const Struct_field* field) 18894 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, 18895 Linemap::predeclared_location()), 18896 type_(type), field_(field) 18897 { } 18898 18899 protected: 18900 bool 18901 do_is_static_initializer() const 18902 { return true; } 18903 18904 Type* 18905 do_type() 18906 { return Type::lookup_integer_type("uintptr"); } 18907 18908 void 18909 do_determine_type(const Type_context*) 18910 { } 18911 18912 Expression* 18913 do_copy() 18914 { return this; } 18915 18916 Bexpression* 18917 do_get_backend(Translate_context* context); 18918 18919 void 18920 do_dump_expression(Ast_dump_context*) const; 18921 18922 private: 18923 // The type of the struct. 18924 Struct_type* type_; 18925 // The field. 18926 const Struct_field* field_; 18927 }; 18928 18929 // Return the backend representation for a struct field offset. 18930 18931 Bexpression* 18932 Struct_field_offset_expression::do_get_backend(Translate_context* context) 18933 { 18934 const Struct_field_list* fields = this->type_->fields(); 18935 Struct_field_list::const_iterator p; 18936 unsigned i = 0; 18937 for (p = fields->begin(); 18938 p != fields->end(); 18939 ++p, ++i) 18940 if (&*p == this->field_) 18941 break; 18942 go_assert(&*p == this->field_); 18943 18944 Gogo* gogo = context->gogo(); 18945 Btype* btype = this->type_->get_backend(gogo); 18946 18947 int64_t offset = gogo->backend()->type_field_offset(btype, i); 18948 Type* uptr_type = Type::lookup_integer_type("uintptr"); 18949 Expression* ret = 18950 Expression::make_integer_int64(offset, uptr_type, 18951 Linemap::predeclared_location()); 18952 return ret->get_backend(context); 18953 } 18954 18955 // Dump ast representation for a struct field offset expression. 18956 18957 void 18958 Struct_field_offset_expression::do_dump_expression( 18959 Ast_dump_context* ast_dump_context) const 18960 { 18961 ast_dump_context->ostream() << "unsafe.Offsetof("; 18962 ast_dump_context->dump_type(this->type_); 18963 ast_dump_context->ostream() << '.'; 18964 ast_dump_context->ostream() << 18965 Gogo::message_name(this->field_->field_name()); 18966 ast_dump_context->ostream() << ")"; 18967 } 18968 18969 // Make an expression for a struct field offset. 18970 18971 Expression* 18972 Expression::make_struct_field_offset(Struct_type* type, 18973 const Struct_field* field) 18974 { 18975 return new Struct_field_offset_expression(type, field); 18976 } 18977 18978 // An expression which evaluates to the address of an unnamed label. 18979 18980 class Label_addr_expression : public Expression 18981 { 18982 public: 18983 Label_addr_expression(Label* label, Location location) 18984 : Expression(EXPRESSION_LABEL_ADDR, location), 18985 label_(label) 18986 { } 18987 18988 protected: 18989 Type* 18990 do_type() 18991 { return Type::make_pointer_type(Type::make_void_type()); } 18992 18993 void 18994 do_determine_type(const Type_context*) 18995 { } 18996 18997 Expression* 18998 do_copy() 18999 { return new Label_addr_expression(this->label_, this->location()); } 19000 19001 Bexpression* 19002 do_get_backend(Translate_context* context) 19003 { return this->label_->get_addr(context, this->location()); } 19004 19005 void 19006 do_dump_expression(Ast_dump_context* ast_dump_context) const 19007 { ast_dump_context->ostream() << this->label_->name(); } 19008 19009 private: 19010 // The label whose address we are taking. 19011 Label* label_; 19012 }; 19013 19014 // Make an expression for the address of an unnamed label. 19015 19016 Expression* 19017 Expression::make_label_addr(Label* label, Location location) 19018 { 19019 return new Label_addr_expression(label, location); 19020 } 19021 19022 // Class Conditional_expression. 19023 19024 // Traversal. 19025 19026 int 19027 Conditional_expression::do_traverse(Traverse* traverse) 19028 { 19029 if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT 19030 || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT 19031 || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) 19032 return TRAVERSE_EXIT; 19033 return TRAVERSE_CONTINUE; 19034 } 19035 19036 // Return the type of the conditional expression. 19037 19038 Type* 19039 Conditional_expression::do_type() 19040 { 19041 Type* result_type = Type::make_void_type(); 19042 if (Type::are_identical(this->then_->type(), this->else_->type(), 19043 Type::COMPARE_ERRORS | Type::COMPARE_TAGS, 19044 NULL)) 19045 result_type = this->then_->type(); 19046 else if (this->then_->is_nil_expression() 19047 || this->else_->is_nil_expression()) 19048 result_type = (!this->then_->is_nil_expression() 19049 ? this->then_->type() 19050 : this->else_->type()); 19051 return result_type; 19052 } 19053 19054 // Determine type for a conditional expression. 19055 19056 void 19057 Conditional_expression::do_determine_type(const Type_context* context) 19058 { 19059 this->cond_->determine_type_no_context(); 19060 this->then_->determine_type(context); 19061 this->else_->determine_type(context); 19062 } 19063 19064 // Get the backend representation of a conditional expression. 19065 19066 Bexpression* 19067 Conditional_expression::do_get_backend(Translate_context* context) 19068 { 19069 Gogo* gogo = context->gogo(); 19070 Btype* result_btype = this->type()->get_backend(gogo); 19071 Bexpression* cond = this->cond_->get_backend(context); 19072 Bexpression* then = this->then_->get_backend(context); 19073 Bexpression* belse = this->else_->get_backend(context); 19074 Bfunction* bfn = context->function()->func_value()->get_decl(); 19075 return gogo->backend()->conditional_expression(bfn, result_btype, cond, then, 19076 belse, this->location()); 19077 } 19078 19079 // Dump ast representation of a conditional expression. 19080 19081 void 19082 Conditional_expression::do_dump_expression( 19083 Ast_dump_context* ast_dump_context) const 19084 { 19085 ast_dump_context->ostream() << "("; 19086 ast_dump_context->dump_expression(this->cond_); 19087 ast_dump_context->ostream() << " ? "; 19088 ast_dump_context->dump_expression(this->then_); 19089 ast_dump_context->ostream() << " : "; 19090 ast_dump_context->dump_expression(this->else_); 19091 ast_dump_context->ostream() << ") "; 19092 } 19093 19094 // Make a conditional expression. 19095 19096 Expression* 19097 Expression::make_conditional(Expression* cond, Expression* then, 19098 Expression* else_expr, Location location) 19099 { 19100 return new Conditional_expression(cond, then, else_expr, location); 19101 } 19102 19103 // Class Compound_expression. 19104 19105 // Traversal. 19106 19107 int 19108 Compound_expression::do_traverse(Traverse* traverse) 19109 { 19110 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT 19111 || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) 19112 return TRAVERSE_EXIT; 19113 return TRAVERSE_CONTINUE; 19114 } 19115 19116 // Return the type of the compound expression. 19117 19118 Type* 19119 Compound_expression::do_type() 19120 { 19121 return this->expr_->type(); 19122 } 19123 19124 // Determine type for a compound expression. 19125 19126 void 19127 Compound_expression::do_determine_type(const Type_context* context) 19128 { 19129 this->init_->determine_type_no_context(); 19130 this->expr_->determine_type(context); 19131 } 19132 19133 // Get the backend representation of a compound expression. 19134 19135 Bexpression* 19136 Compound_expression::do_get_backend(Translate_context* context) 19137 { 19138 Gogo* gogo = context->gogo(); 19139 Bexpression* binit = this->init_->get_backend(context); 19140 Bfunction* bfunction = context->function()->func_value()->get_decl(); 19141 Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction, 19142 binit); 19143 Bexpression* bexpr = this->expr_->get_backend(context); 19144 return gogo->backend()->compound_expression(init_stmt, bexpr, 19145 this->location()); 19146 } 19147 19148 // Dump ast representation of a conditional expression. 19149 19150 void 19151 Compound_expression::do_dump_expression( 19152 Ast_dump_context* ast_dump_context) const 19153 { 19154 ast_dump_context->ostream() << "("; 19155 ast_dump_context->dump_expression(this->init_); 19156 ast_dump_context->ostream() << ","; 19157 ast_dump_context->dump_expression(this->expr_); 19158 ast_dump_context->ostream() << ") "; 19159 } 19160 19161 // Make a compound expression. 19162 19163 Expression* 19164 Expression::make_compound(Expression* init, Expression* expr, Location location) 19165 { 19166 return new Compound_expression(init, expr, location); 19167 } 19168 19169 // Class Backend_expression. 19170 19171 int 19172 Backend_expression::do_traverse(Traverse*) 19173 { 19174 return TRAVERSE_CONTINUE; 19175 } 19176 19177 Expression* 19178 Backend_expression::do_copy() 19179 { 19180 return new Backend_expression(this->bexpr_, this->type_->copy_expressions(), 19181 this->location()); 19182 } 19183 19184 void 19185 Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const 19186 { 19187 ast_dump_context->ostream() << "backend_expression<"; 19188 ast_dump_context->dump_type(this->type_); 19189 ast_dump_context->ostream() << ">"; 19190 } 19191 19192 Expression* 19193 Expression::make_backend(Bexpression* bexpr, Type* type, Location location) 19194 { 19195 return new Backend_expression(bexpr, type, location); 19196 } 19197 19198 // Import an expression. This comes at the end in order to see the 19199 // various class definitions. 19200 19201 Expression* 19202 Expression::import_expression(Import_expression* imp, Location loc) 19203 { 19204 Expression* expr = Expression::import_expression_without_suffix(imp, loc); 19205 while (true) 19206 { 19207 if (imp->match_c_string("(")) 19208 { 19209 imp->advance(1); 19210 Expression_list* args = new Expression_list(); 19211 bool is_varargs = false; 19212 while (!imp->match_c_string(")")) 19213 { 19214 Expression* arg = Expression::import_expression(imp, loc); 19215 if (arg->is_error_expression()) 19216 return arg; 19217 args->push_back(arg); 19218 if (imp->match_c_string(")")) 19219 break; 19220 else if (imp->match_c_string("...)")) 19221 { 19222 imp->advance(3); 19223 is_varargs = true; 19224 break; 19225 } 19226 imp->require_c_string(", "); 19227 } 19228 imp->require_c_string(")"); 19229 expr = Expression::make_call(expr, args, is_varargs, loc); 19230 expr->call_expression()->set_varargs_are_lowered(); 19231 } 19232 else if (imp->match_c_string("[")) 19233 { 19234 imp->advance(1); 19235 Expression* start = Expression::import_expression(imp, loc); 19236 Expression* end = NULL; 19237 Expression* cap = NULL; 19238 if (imp->match_c_string(":")) 19239 { 19240 imp->advance(1); 19241 int c = imp->peek_char(); 19242 if (c == ':' || c == ']') 19243 end = Expression::make_nil(loc); 19244 else 19245 end = Expression::import_expression(imp, loc); 19246 if (imp->match_c_string(":")) 19247 { 19248 imp->advance(1); 19249 cap = Expression::import_expression(imp, loc); 19250 } 19251 } 19252 imp->require_c_string("]"); 19253 expr = Expression::make_index(expr, start, end, cap, loc); 19254 } 19255 else 19256 break; 19257 } 19258 19259 return expr; 19260 } 19261 19262 // Import an expression without considering a suffix (function 19263 // arguments, index operations, etc.). 19264 19265 Expression* 19266 Expression::import_expression_without_suffix(Import_expression* imp, 19267 Location loc) 19268 { 19269 int c = imp->peek_char(); 19270 if (c == '+' || c == '-' || c == '!' || c == '^' || c == '&' || c == '*') 19271 return Unary_expression::do_import(imp, loc); 19272 else if (c == '(') 19273 return Binary_expression::do_import(imp, loc); 19274 else if (imp->match_c_string("$true") 19275 || imp->match_c_string("$false") 19276 || (imp->version() < EXPORT_FORMAT_V3 19277 && (imp->match_c_string("true") 19278 || imp->match_c_string("false")))) 19279 return Boolean_expression::do_import(imp, loc); 19280 else if (c == '"') 19281 return String_expression::do_import(imp, loc); 19282 else if (c == '-' || (c >= '0' && c <= '9')) 19283 { 19284 // This handles integers, floats and complex constants. 19285 return Integer_expression::do_import(imp, loc); 19286 } 19287 else if (imp->match_c_string("<-")) 19288 return Receive_expression::do_import(imp, loc); 19289 else if (imp->match_c_string("$nil") 19290 || (imp->version() < EXPORT_FORMAT_V3 19291 && imp->match_c_string("nil"))) 19292 return Nil_expression::do_import(imp, loc); 19293 else if (imp->match_c_string("$convert") 19294 || (imp->version() < EXPORT_FORMAT_V3 19295 && imp->match_c_string("convert"))) 19296 return Type_conversion_expression::do_import(imp, loc); 19297 19298 Import_function_body* ifb = imp->ifb(); 19299 if (ifb == NULL) 19300 { 19301 go_error_at(imp->location(), "import error: expected expression"); 19302 return Expression::make_error(loc); 19303 } 19304 if (ifb->saw_error()) 19305 return Expression::make_error(loc); 19306 19307 if (ifb->match_c_string("$t")) 19308 return Temporary_reference_expression::do_import(ifb, loc); 19309 19310 return Expression::import_identifier(ifb, loc); 19311 } 19312 19313 // Import an identifier in an expression. This is a reference to a 19314 // variable or function. 19315 19316 Expression* 19317 Expression::import_identifier(Import_function_body* ifb, Location loc) 19318 { 19319 std::string id; 19320 Package* pkg; 19321 bool is_exported; 19322 if (!Import::read_qualified_identifier(ifb, &id, &pkg, &is_exported)) 19323 { 19324 if (!ifb->saw_error()) 19325 go_error_at(ifb->location(), 19326 "import error for %qs: bad qualified identifier at %lu", 19327 ifb->name().c_str(), 19328 static_cast<unsigned long>(ifb->off())); 19329 ifb->set_saw_error(); 19330 return Expression::make_error(loc); 19331 } 19332 19333 Named_object* no = NULL; 19334 if (pkg == NULL && is_exported) 19335 no = ifb->block()->bindings()->lookup(id); 19336 if (no == NULL) 19337 { 19338 const Package* ipkg = pkg; 19339 if (ipkg == NULL) 19340 ipkg = ifb->function()->package(); 19341 if (!is_exported) 19342 id = '.' + ipkg->pkgpath() + '.' + id; 19343 no = ipkg->bindings()->lookup(id); 19344 } 19345 if (no == NULL) 19346 no = ifb->gogo()->lookup_global(id.c_str()); 19347 19348 if (no == NULL) 19349 { 19350 if (!ifb->saw_error()) 19351 go_error_at(ifb->location(), 19352 "import error for %qs: lookup of %qs failed", 19353 ifb->name().c_str(), id.c_str()); 19354 ifb->set_saw_error(); 19355 return Expression::make_error(loc); 19356 } 19357 19358 if (no->is_variable() || no->is_result_variable()) 19359 return Expression::make_var_reference(no, loc); 19360 else if (no->is_function() || no->is_function_declaration()) 19361 return Expression::make_func_reference(no, NULL, loc); 19362 else 19363 { 19364 if (!ifb->saw_error()) 19365 go_error_at(ifb->location(), 19366 ("import error for %qs: " 19367 "unexpected type of identifier %qs (%d)"), 19368 ifb->name().c_str(), 19369 id.c_str(), no->classification()); 19370 ifb->set_saw_error(); 19371 return Expression::make_error(loc); 19372 } 19373 } 19374 19375 // Class Expression_list. 19376 19377 // Traverse the list. 19378 19379 int 19380 Expression_list::traverse(Traverse* traverse) 19381 { 19382 for (Expression_list::iterator p = this->begin(); 19383 p != this->end(); 19384 ++p) 19385 { 19386 if (*p != NULL) 19387 { 19388 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) 19389 return TRAVERSE_EXIT; 19390 } 19391 } 19392 return TRAVERSE_CONTINUE; 19393 } 19394 19395 // Copy the list. 19396 19397 Expression_list* 19398 Expression_list::copy() 19399 { 19400 Expression_list* ret = new Expression_list(); 19401 for (Expression_list::iterator p = this->begin(); 19402 p != this->end(); 19403 ++p) 19404 { 19405 if (*p == NULL) 19406 ret->push_back(NULL); 19407 else 19408 ret->push_back((*p)->copy()); 19409 } 19410 return ret; 19411 } 19412 19413 // Return whether an expression list has an error expression. 19414 19415 bool 19416 Expression_list::contains_error() const 19417 { 19418 for (Expression_list::const_iterator p = this->begin(); 19419 p != this->end(); 19420 ++p) 19421 if (*p != NULL && (*p)->is_error_expression()) 19422 return true; 19423 return false; 19424 } 19425 19426 // Class Numeric_constant. 19427 19428 // Destructor. 19429 19430 Numeric_constant::~Numeric_constant() 19431 { 19432 this->clear(); 19433 } 19434 19435 // Copy constructor. 19436 19437 Numeric_constant::Numeric_constant(const Numeric_constant& a) 19438 : classification_(a.classification_), type_(a.type_) 19439 { 19440 switch (a.classification_) 19441 { 19442 case NC_INVALID: 19443 break; 19444 case NC_INT: 19445 case NC_RUNE: 19446 mpz_init_set(this->u_.int_val, a.u_.int_val); 19447 break; 19448 case NC_FLOAT: 19449 mpfr_init_set(this->u_.float_val, a.u_.float_val, MPFR_RNDN); 19450 break; 19451 case NC_COMPLEX: 19452 mpc_init2(this->u_.complex_val, mpc_precision); 19453 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); 19454 break; 19455 default: 19456 go_unreachable(); 19457 } 19458 } 19459 19460 // Assignment operator. 19461 19462 Numeric_constant& 19463 Numeric_constant::operator=(const Numeric_constant& a) 19464 { 19465 this->clear(); 19466 this->classification_ = a.classification_; 19467 this->type_ = a.type_; 19468 switch (a.classification_) 19469 { 19470 case NC_INVALID: 19471 break; 19472 case NC_INT: 19473 case NC_RUNE: 19474 mpz_init_set(this->u_.int_val, a.u_.int_val); 19475 break; 19476 case NC_FLOAT: 19477 mpfr_init_set(this->u_.float_val, a.u_.float_val, MPFR_RNDN); 19478 break; 19479 case NC_COMPLEX: 19480 mpc_init2(this->u_.complex_val, mpc_precision); 19481 mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); 19482 break; 19483 default: 19484 go_unreachable(); 19485 } 19486 return *this; 19487 } 19488 19489 // Check equality with another numeric constant. 19490 19491 bool 19492 Numeric_constant::equals(const Numeric_constant& a) const 19493 { 19494 if (this->classification_ != a.classification_) 19495 return false; 19496 19497 if (this->type_ != NULL && a.type_ != NULL 19498 && !Type::are_identical(this->type_, a.type_, 19499 Type::COMPARE_ALIASES, NULL)) 19500 return false; 19501 19502 switch (a.classification_) 19503 { 19504 case NC_INVALID: 19505 break; 19506 case NC_INT: 19507 case NC_RUNE: 19508 return mpz_cmp(this->u_.int_val, a.u_.int_val) == 0; 19509 case NC_FLOAT: 19510 return mpfr_cmp(this->u_.float_val, a.u_.float_val) == 0; 19511 case NC_COMPLEX: 19512 return mpc_cmp(this->u_.complex_val, a.u_.complex_val) == 0; 19513 default: 19514 go_unreachable(); 19515 } 19516 return false; 19517 } 19518 19519 // Clear the contents. 19520 19521 void 19522 Numeric_constant::clear() 19523 { 19524 switch (this->classification_) 19525 { 19526 case NC_INVALID: 19527 break; 19528 case NC_INT: 19529 case NC_RUNE: 19530 mpz_clear(this->u_.int_val); 19531 break; 19532 case NC_FLOAT: 19533 mpfr_clear(this->u_.float_val); 19534 break; 19535 case NC_COMPLEX: 19536 mpc_clear(this->u_.complex_val); 19537 break; 19538 default: 19539 go_unreachable(); 19540 } 19541 this->classification_ = NC_INVALID; 19542 } 19543 19544 // Set to an unsigned long value. 19545 19546 void 19547 Numeric_constant::set_unsigned_long(Type* type, unsigned long val) 19548 { 19549 this->clear(); 19550 this->classification_ = NC_INT; 19551 this->type_ = type; 19552 mpz_init_set_ui(this->u_.int_val, val); 19553 } 19554 19555 // Set to an integer value. 19556 19557 void 19558 Numeric_constant::set_int(Type* type, const mpz_t val) 19559 { 19560 this->clear(); 19561 this->classification_ = NC_INT; 19562 this->type_ = type; 19563 mpz_init_set(this->u_.int_val, val); 19564 } 19565 19566 // Set to a rune value. 19567 19568 void 19569 Numeric_constant::set_rune(Type* type, const mpz_t val) 19570 { 19571 this->clear(); 19572 this->classification_ = NC_RUNE; 19573 this->type_ = type; 19574 mpz_init_set(this->u_.int_val, val); 19575 } 19576 19577 // Set to a floating point value. 19578 19579 void 19580 Numeric_constant::set_float(Type* type, const mpfr_t val) 19581 { 19582 this->clear(); 19583 this->classification_ = NC_FLOAT; 19584 this->type_ = type; 19585 19586 // Numeric constants do not have negative zero values, so remove 19587 // them here. They also don't have infinity or NaN values, but we 19588 // should never see them here. 19589 int bits = 0; 19590 if (type != NULL 19591 && type->float_type() != NULL 19592 && !type->float_type()->is_abstract()) 19593 bits = type->float_type()->bits(); 19594 if (Numeric_constant::is_float_neg_zero(val, bits)) 19595 mpfr_init_set_ui(this->u_.float_val, 0, MPFR_RNDN); 19596 else 19597 mpfr_init_set(this->u_.float_val, val, MPFR_RNDN); 19598 } 19599 19600 // Set to a complex value. 19601 19602 void 19603 Numeric_constant::set_complex(Type* type, const mpc_t val) 19604 { 19605 this->clear(); 19606 this->classification_ = NC_COMPLEX; 19607 this->type_ = type; 19608 19609 // Avoid negative zero as in set_float. 19610 int bits = 0; 19611 if (type != NULL 19612 && type->complex_type() != NULL 19613 && !type->complex_type()->is_abstract()) 19614 bits = type->complex_type()->bits() / 2; 19615 19616 mpfr_t real; 19617 mpfr_init_set(real, mpc_realref(val), MPFR_RNDN); 19618 if (Numeric_constant::is_float_neg_zero(real, bits)) 19619 mpfr_set_ui(real, 0, MPFR_RNDN); 19620 19621 mpfr_t imag; 19622 mpfr_init_set(imag, mpc_imagref(val), MPFR_RNDN); 19623 if (Numeric_constant::is_float_neg_zero(imag, bits)) 19624 mpfr_set_ui(imag, 0, MPFR_RNDN); 19625 19626 mpc_init2(this->u_.complex_val, mpc_precision); 19627 mpc_set_fr_fr(this->u_.complex_val, real, imag, MPC_RNDNN); 19628 19629 mpfr_clear(real); 19630 mpfr_clear(imag); 19631 } 19632 19633 // Return whether VAL, at a precision of BITS, is a negative zero. 19634 // BITS may be zero in which case it is ignored. 19635 19636 bool 19637 Numeric_constant::is_float_neg_zero(const mpfr_t val, int bits) 19638 { 19639 if (!mpfr_signbit(val)) 19640 return false; 19641 if (mpfr_zero_p(val)) 19642 return true; 19643 mpfr_exp_t min_exp; 19644 switch (bits) 19645 { 19646 case 0: 19647 return false; 19648 case 32: 19649 // In a denormalized float32 the exponent is -126, and there are 19650 // 24 bits of which at least the last must be 1, so the smallest 19651 // representable non-zero exponent is -126 - (24 - 1) == -149. 19652 min_exp = -149; 19653 break; 19654 case 64: 19655 // Minimum exponent is -1022, there are 53 bits. 19656 min_exp = -1074; 19657 break; 19658 default: 19659 go_unreachable(); 19660 } 19661 return mpfr_get_exp(val) < min_exp; 19662 } 19663 19664 // Get an int value. 19665 19666 void 19667 Numeric_constant::get_int(mpz_t* val) const 19668 { 19669 go_assert(this->is_int()); 19670 mpz_init_set(*val, this->u_.int_val); 19671 } 19672 19673 // Get a rune value. 19674 19675 void 19676 Numeric_constant::get_rune(mpz_t* val) const 19677 { 19678 go_assert(this->is_rune()); 19679 mpz_init_set(*val, this->u_.int_val); 19680 } 19681 19682 // Get a floating point value. 19683 19684 void 19685 Numeric_constant::get_float(mpfr_t* val) const 19686 { 19687 go_assert(this->is_float()); 19688 mpfr_init_set(*val, this->u_.float_val, MPFR_RNDN); 19689 } 19690 19691 // Get a complex value. 19692 19693 void 19694 Numeric_constant::get_complex(mpc_t* val) const 19695 { 19696 go_assert(this->is_complex()); 19697 mpc_init2(*val, mpc_precision); 19698 mpc_set(*val, this->u_.complex_val, MPC_RNDNN); 19699 } 19700 19701 // Express value as unsigned long if possible. 19702 19703 Numeric_constant::To_unsigned_long 19704 Numeric_constant::to_unsigned_long(unsigned long* val) const 19705 { 19706 switch (this->classification_) 19707 { 19708 case NC_INT: 19709 case NC_RUNE: 19710 return this->mpz_to_unsigned_long(this->u_.int_val, val); 19711 case NC_FLOAT: 19712 return this->mpfr_to_unsigned_long(this->u_.float_val, val); 19713 case NC_COMPLEX: 19714 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 19715 return NC_UL_NOTINT; 19716 return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), 19717 val); 19718 default: 19719 go_unreachable(); 19720 } 19721 } 19722 19723 // Express integer value as unsigned long if possible. 19724 19725 Numeric_constant::To_unsigned_long 19726 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, 19727 unsigned long *val) const 19728 { 19729 if (mpz_sgn(ival) < 0) 19730 return NC_UL_NEGATIVE; 19731 unsigned long ui = mpz_get_ui(ival); 19732 if (mpz_cmp_ui(ival, ui) != 0) 19733 return NC_UL_BIG; 19734 *val = ui; 19735 return NC_UL_VALID; 19736 } 19737 19738 // Express floating point value as unsigned long if possible. 19739 19740 Numeric_constant::To_unsigned_long 19741 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, 19742 unsigned long *val) const 19743 { 19744 if (!mpfr_integer_p(fval)) 19745 return NC_UL_NOTINT; 19746 mpz_t ival; 19747 mpz_init(ival); 19748 mpfr_get_z(ival, fval, MPFR_RNDN); 19749 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); 19750 mpz_clear(ival); 19751 return ret; 19752 } 19753 19754 // Express value as memory size if possible. 19755 19756 bool 19757 Numeric_constant::to_memory_size(int64_t* val) const 19758 { 19759 switch (this->classification_) 19760 { 19761 case NC_INT: 19762 case NC_RUNE: 19763 return this->mpz_to_memory_size(this->u_.int_val, val); 19764 case NC_FLOAT: 19765 return this->mpfr_to_memory_size(this->u_.float_val, val); 19766 case NC_COMPLEX: 19767 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 19768 return false; 19769 return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val); 19770 default: 19771 go_unreachable(); 19772 } 19773 } 19774 19775 // Express integer as memory size if possible. 19776 19777 bool 19778 Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const 19779 { 19780 if (mpz_sgn(ival) < 0) 19781 return false; 19782 if (mpz_fits_slong_p(ival)) 19783 { 19784 *val = static_cast<int64_t>(mpz_get_si(ival)); 19785 return true; 19786 } 19787 19788 // Test >= 64, not > 64, because an int64_t can hold 63 bits of a 19789 // positive value. 19790 if (mpz_sizeinbase(ival, 2) >= 64) 19791 return false; 19792 19793 mpz_t q, r; 19794 mpz_init(q); 19795 mpz_init(r); 19796 mpz_tdiv_q_2exp(q, ival, 32); 19797 mpz_tdiv_r_2exp(r, ival, 32); 19798 go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r)); 19799 *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32) 19800 + static_cast<int64_t>(mpz_get_ui(r))); 19801 mpz_clear(r); 19802 mpz_clear(q); 19803 return true; 19804 } 19805 19806 // Express floating point value as memory size if possible. 19807 19808 bool 19809 Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const 19810 { 19811 if (!mpfr_integer_p(fval)) 19812 return false; 19813 mpz_t ival; 19814 mpz_init(ival); 19815 mpfr_get_z(ival, fval, MPFR_RNDN); 19816 bool ret = this->mpz_to_memory_size(ival, val); 19817 mpz_clear(ival); 19818 return ret; 19819 } 19820 19821 // Convert value to integer if possible. 19822 19823 bool 19824 Numeric_constant::to_int(mpz_t* val) const 19825 { 19826 switch (this->classification_) 19827 { 19828 case NC_INT: 19829 case NC_RUNE: 19830 mpz_init_set(*val, this->u_.int_val); 19831 return true; 19832 case NC_FLOAT: 19833 if (!mpfr_integer_p(this->u_.float_val)) 19834 return false; 19835 mpz_init(*val); 19836 mpfr_get_z(*val, this->u_.float_val, MPFR_RNDN); 19837 return true; 19838 case NC_COMPLEX: 19839 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) 19840 || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) 19841 return false; 19842 mpz_init(*val); 19843 mpfr_get_z(*val, mpc_realref(this->u_.complex_val), MPFR_RNDN); 19844 return true; 19845 default: 19846 go_unreachable(); 19847 } 19848 } 19849 19850 // Convert value to floating point if possible. 19851 19852 bool 19853 Numeric_constant::to_float(mpfr_t* val) const 19854 { 19855 switch (this->classification_) 19856 { 19857 case NC_INT: 19858 case NC_RUNE: 19859 mpfr_init_set_z(*val, this->u_.int_val, MPFR_RNDN); 19860 return true; 19861 case NC_FLOAT: 19862 mpfr_init_set(*val, this->u_.float_val, MPFR_RNDN); 19863 return true; 19864 case NC_COMPLEX: 19865 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 19866 return false; 19867 mpfr_init_set(*val, mpc_realref(this->u_.complex_val), MPFR_RNDN); 19868 return true; 19869 default: 19870 go_unreachable(); 19871 } 19872 } 19873 19874 // Convert value to complex. 19875 19876 bool 19877 Numeric_constant::to_complex(mpc_t* val) const 19878 { 19879 mpc_init2(*val, mpc_precision); 19880 switch (this->classification_) 19881 { 19882 case NC_INT: 19883 case NC_RUNE: 19884 mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); 19885 return true; 19886 case NC_FLOAT: 19887 mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); 19888 return true; 19889 case NC_COMPLEX: 19890 mpc_set(*val, this->u_.complex_val, MPC_RNDNN); 19891 return true; 19892 default: 19893 go_unreachable(); 19894 } 19895 } 19896 19897 // Get the type. 19898 19899 Type* 19900 Numeric_constant::type() const 19901 { 19902 if (this->type_ != NULL) 19903 return this->type_; 19904 switch (this->classification_) 19905 { 19906 case NC_INT: 19907 return Type::make_abstract_integer_type(); 19908 case NC_RUNE: 19909 return Type::make_abstract_character_type(); 19910 case NC_FLOAT: 19911 return Type::make_abstract_float_type(); 19912 case NC_COMPLEX: 19913 return Type::make_abstract_complex_type(); 19914 default: 19915 go_unreachable(); 19916 } 19917 } 19918 19919 // If the constant can be expressed in TYPE, then set the type of the 19920 // constant to TYPE and return true. Otherwise return false, and, if 19921 // ISSUE_ERROR is true, report an appropriate error message. 19922 19923 bool 19924 Numeric_constant::set_type(Type* type, bool issue_error, Location loc) 19925 { 19926 bool ret; 19927 if (type == NULL || type->is_error()) 19928 ret = true; 19929 else if (type->integer_type() != NULL) 19930 ret = this->check_int_type(type->integer_type(), issue_error, loc); 19931 else if (type->float_type() != NULL) 19932 ret = this->check_float_type(type->float_type(), issue_error, loc); 19933 else if (type->complex_type() != NULL) 19934 ret = this->check_complex_type(type->complex_type(), issue_error, loc); 19935 else 19936 { 19937 ret = false; 19938 if (issue_error) 19939 go_assert(saw_errors()); 19940 } 19941 if (ret) 19942 this->type_ = type; 19943 return ret; 19944 } 19945 19946 // Check whether the constant can be expressed in an integer type. 19947 19948 bool 19949 Numeric_constant::check_int_type(Integer_type* type, bool issue_error, 19950 Location location) 19951 { 19952 mpz_t val; 19953 switch (this->classification_) 19954 { 19955 case NC_INT: 19956 case NC_RUNE: 19957 mpz_init_set(val, this->u_.int_val); 19958 break; 19959 19960 case NC_FLOAT: 19961 if (!mpfr_integer_p(this->u_.float_val)) 19962 { 19963 if (issue_error) 19964 { 19965 go_error_at(location, 19966 "floating-point constant truncated to integer"); 19967 this->set_invalid(); 19968 } 19969 return false; 19970 } 19971 mpz_init(val); 19972 mpfr_get_z(val, this->u_.float_val, MPFR_RNDN); 19973 break; 19974 19975 case NC_COMPLEX: 19976 if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) 19977 || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 19978 { 19979 if (issue_error) 19980 { 19981 go_error_at(location, "complex constant truncated to integer"); 19982 this->set_invalid(); 19983 } 19984 return false; 19985 } 19986 mpz_init(val); 19987 mpfr_get_z(val, mpc_realref(this->u_.complex_val), MPFR_RNDN); 19988 break; 19989 19990 default: 19991 go_unreachable(); 19992 } 19993 19994 bool ret; 19995 if (type->is_abstract()) 19996 ret = true; 19997 else 19998 { 19999 int bits = mpz_sizeinbase(val, 2); 20000 if (type->is_unsigned()) 20001 { 20002 // For an unsigned type we can only accept a nonnegative 20003 // number, and we must be able to represents at least BITS. 20004 ret = mpz_sgn(val) >= 0 && bits <= type->bits(); 20005 } 20006 else 20007 { 20008 // For a signed type we need an extra bit to indicate the 20009 // sign. We have to handle the most negative integer 20010 // specially. 20011 ret = (bits + 1 <= type->bits() 20012 || (bits <= type->bits() 20013 && mpz_sgn(val) < 0 20014 && (mpz_scan1(val, 0) 20015 == static_cast<unsigned long>(type->bits() - 1)) 20016 && mpz_scan0(val, type->bits()) == ULONG_MAX)); 20017 } 20018 } 20019 20020 if (!ret && issue_error) 20021 { 20022 go_error_at(location, "integer constant overflow"); 20023 this->set_invalid(); 20024 } 20025 20026 return ret; 20027 } 20028 20029 // Check whether the constant can be expressed in a floating point 20030 // type. 20031 20032 bool 20033 Numeric_constant::check_float_type(Float_type* type, bool issue_error, 20034 Location location) 20035 { 20036 mpfr_t val; 20037 switch (this->classification_) 20038 { 20039 case NC_INT: 20040 case NC_RUNE: 20041 mpfr_init_set_z(val, this->u_.int_val, MPFR_RNDN); 20042 break; 20043 20044 case NC_FLOAT: 20045 mpfr_init_set(val, this->u_.float_val, MPFR_RNDN); 20046 break; 20047 20048 case NC_COMPLEX: 20049 if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) 20050 { 20051 if (issue_error) 20052 { 20053 this->set_invalid(); 20054 go_error_at(location, 20055 "complex constant truncated to floating-point"); 20056 } 20057 return false; 20058 } 20059 mpfr_init_set(val, mpc_realref(this->u_.complex_val), MPFR_RNDN); 20060 break; 20061 20062 default: 20063 go_unreachable(); 20064 } 20065 20066 bool ret; 20067 if (type->is_abstract()) 20068 ret = true; 20069 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) 20070 { 20071 // A NaN or Infinity always fits in the range of the type. 20072 ret = true; 20073 } 20074 else 20075 { 20076 mpfr_exp_t exp = mpfr_get_exp(val); 20077 mpfr_exp_t max_exp; 20078 switch (type->bits()) 20079 { 20080 case 32: 20081 max_exp = 128; 20082 break; 20083 case 64: 20084 max_exp = 1024; 20085 break; 20086 default: 20087 go_unreachable(); 20088 } 20089 20090 ret = exp <= max_exp; 20091 20092 if (ret) 20093 { 20094 // Round the constant to the desired type. 20095 mpfr_t t; 20096 mpfr_init(t); 20097 switch (type->bits()) 20098 { 20099 case 32: 20100 mpfr_set_prec(t, 24); 20101 break; 20102 case 64: 20103 mpfr_set_prec(t, 53); 20104 break; 20105 default: 20106 go_unreachable(); 20107 } 20108 mpfr_set(t, val, MPFR_RNDN); 20109 mpfr_set(val, t, MPFR_RNDN); 20110 mpfr_clear(t); 20111 20112 this->set_float(type, val); 20113 } 20114 } 20115 20116 mpfr_clear(val); 20117 20118 if (!ret && issue_error) 20119 { 20120 go_error_at(location, "floating-point constant overflow"); 20121 this->set_invalid(); 20122 } 20123 20124 return ret; 20125 } 20126 20127 // Check whether the constant can be expressed in a complex type. 20128 20129 bool 20130 Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, 20131 Location location) 20132 { 20133 if (type->is_abstract()) 20134 return true; 20135 20136 mpfr_exp_t max_exp; 20137 switch (type->bits()) 20138 { 20139 case 64: 20140 max_exp = 128; 20141 break; 20142 case 128: 20143 max_exp = 1024; 20144 break; 20145 default: 20146 go_unreachable(); 20147 } 20148 20149 mpc_t val; 20150 mpc_init2(val, mpc_precision); 20151 switch (this->classification_) 20152 { 20153 case NC_INT: 20154 case NC_RUNE: 20155 mpc_set_z(val, this->u_.int_val, MPC_RNDNN); 20156 break; 20157 20158 case NC_FLOAT: 20159 mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); 20160 break; 20161 20162 case NC_COMPLEX: 20163 mpc_set(val, this->u_.complex_val, MPC_RNDNN); 20164 break; 20165 20166 default: 20167 go_unreachable(); 20168 } 20169 20170 bool ret = true; 20171 if (!mpfr_nan_p(mpc_realref(val)) 20172 && !mpfr_inf_p(mpc_realref(val)) 20173 && !mpfr_zero_p(mpc_realref(val)) 20174 && mpfr_get_exp(mpc_realref(val)) > max_exp) 20175 { 20176 if (issue_error) 20177 { 20178 go_error_at(location, "complex real part overflow"); 20179 this->set_invalid(); 20180 } 20181 ret = false; 20182 } 20183 20184 if (!mpfr_nan_p(mpc_imagref(val)) 20185 && !mpfr_inf_p(mpc_imagref(val)) 20186 && !mpfr_zero_p(mpc_imagref(val)) 20187 && mpfr_get_exp(mpc_imagref(val)) > max_exp) 20188 { 20189 if (issue_error) 20190 { 20191 go_error_at(location, "complex imaginary part overflow"); 20192 this->set_invalid(); 20193 } 20194 ret = false; 20195 } 20196 20197 if (ret) 20198 { 20199 // Round the constant to the desired type. 20200 mpc_t t; 20201 switch (type->bits()) 20202 { 20203 case 64: 20204 mpc_init2(t, 24); 20205 break; 20206 case 128: 20207 mpc_init2(t, 53); 20208 break; 20209 default: 20210 go_unreachable(); 20211 } 20212 mpc_set(t, val, MPC_RNDNN); 20213 mpc_set(val, t, MPC_RNDNN); 20214 mpc_clear(t); 20215 20216 this->set_complex(type, val); 20217 } 20218 20219 mpc_clear(val); 20220 20221 return ret; 20222 } 20223 20224 // Return an Expression for this value. 20225 20226 Expression* 20227 Numeric_constant::expression(Location loc) const 20228 { 20229 switch (this->classification_) 20230 { 20231 case NC_INT: 20232 return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); 20233 case NC_RUNE: 20234 return Expression::make_character(&this->u_.int_val, this->type_, loc); 20235 case NC_FLOAT: 20236 return Expression::make_float(&this->u_.float_val, this->type_, loc); 20237 case NC_COMPLEX: 20238 return Expression::make_complex(&this->u_.complex_val, this->type_, loc); 20239 case NC_INVALID: 20240 go_assert(saw_errors()); 20241 return Expression::make_error(loc); 20242 default: 20243 go_unreachable(); 20244 } 20245 } 20246 20247 // Calculate a hash code with a given seed. 20248 20249 unsigned int 20250 Numeric_constant::hash(unsigned int seed) const 20251 { 20252 unsigned long val; 20253 const unsigned int PRIME = 97; 20254 long e = 0; 20255 double f = 1.0; 20256 mpfr_t m; 20257 20258 switch (this->classification_) 20259 { 20260 case NC_INVALID: 20261 return PRIME; 20262 case NC_INT: 20263 case NC_RUNE: 20264 val = mpz_get_ui(this->u_.int_val); 20265 break; 20266 case NC_COMPLEX: 20267 mpfr_init(m); 20268 mpc_abs(m, this->u_.complex_val, MPFR_RNDN); 20269 val = mpfr_get_ui(m, MPFR_RNDN); 20270 mpfr_clear(m); 20271 break; 20272 case NC_FLOAT: 20273 f = mpfr_get_d_2exp(&e, this->u_.float_val, MPFR_RNDN) * 4294967295.0; 20274 val = static_cast<unsigned long>(e + static_cast<long>(f)); 20275 break; 20276 default: 20277 go_unreachable(); 20278 } 20279 20280 return (static_cast<unsigned int>(val) + seed) * PRIME; 20281 }