1 /* Evaluate expressions for GDB. 2 3 Copyright (C) 1986-2023 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "symtab.h" 22 #include "gdbtypes.h" 23 #include "value.h" 24 #include "expression.h" 25 #include "target.h" 26 #include "frame.h" 27 #include "gdbthread.h" 28 #include "language.h" /* For CAST_IS_CONVERSION. */ 29 #include "cp-abi.h" 30 #include "infcall.h" 31 #include "objc-lang.h" 32 #include "block.h" 33 #include "parser-defs.h" 34 #include "cp-support.h" 35 #include "ui-out.h" 36 #include "regcache.h" 37 #include "user-regs.h" 38 #include "valprint.h" 39 #include "gdbsupport/gdb_obstack.h" 40 #include "objfiles.h" 41 #include "typeprint.h" 42 #include <ctype.h> 43 #include "expop.h" 44 #include "c-exp.h" 45 #include "inferior.h" 46 47 48 /* Parse the string EXP as a C expression, evaluate it, 49 and return the result as a number. */ 50 51 CORE_ADDR 52 parse_and_eval_address (const char *exp) 53 { 54 expression_up expr = parse_expression (exp); 55 56 return value_as_address (evaluate_expression (expr.get ())); 57 } 58 59 /* Like parse_and_eval_address, but treats the value of the expression 60 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */ 61 LONGEST 62 parse_and_eval_long (const char *exp) 63 { 64 expression_up expr = parse_expression (exp); 65 66 return value_as_long (evaluate_expression (expr.get ())); 67 } 68 69 struct value * 70 parse_and_eval (const char *exp) 71 { 72 expression_up expr = parse_expression (exp); 73 74 return evaluate_expression (expr.get ()); 75 } 76 77 /* Parse up to a comma (or to a closeparen) 78 in the string EXPP as an expression, evaluate it, and return the value. 79 EXPP is advanced to point to the comma. */ 80 81 struct value * 82 parse_to_comma_and_eval (const char **expp) 83 { 84 expression_up expr = parse_exp_1 (expp, 0, nullptr, 1); 85 86 return evaluate_expression (expr.get ()); 87 } 88 89 90 /* See expression.h. */ 91 92 struct value * 93 expression::evaluate (struct type *expect_type, enum noside noside) 94 { 95 gdb::optional<enable_thread_stack_temporaries> stack_temporaries; 96 if (target_has_execution () && inferior_ptid != null_ptid 97 && language_defn->la_language == language_cplus 98 && !thread_stack_temporaries_enabled_p (inferior_thread ())) 99 stack_temporaries.emplace (inferior_thread ()); 100 101 struct value *retval = op->evaluate (expect_type, this, noside); 102 103 if (stack_temporaries.has_value () 104 && value_in_thread_stack_temporaries (retval, inferior_thread ())) 105 retval = value_non_lval (retval); 106 107 return retval; 108 } 109 110 /* See value.h. */ 111 112 struct value * 113 evaluate_expression (struct expression *exp, struct type *expect_type) 114 { 115 return exp->evaluate (expect_type, EVAL_NORMAL); 116 } 117 118 /* Evaluate an expression, avoiding all memory references 119 and getting a value whose type alone is correct. */ 120 121 struct value * 122 evaluate_type (struct expression *exp) 123 { 124 return exp->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS); 125 } 126 127 /* Find the current value of a watchpoint on EXP. Return the value in 128 *VALP and *RESULTP and the chain of intermediate and final values 129 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does 130 not need them. 131 132 If PRESERVE_ERRORS is true, then exceptions are passed through. 133 Otherwise, if PRESERVE_ERRORS is false, then if a memory error 134 occurs while evaluating the expression, *RESULTP will be set to 135 NULL. *RESULTP may be a lazy value, if the result could not be 136 read from memory. It is used to determine whether a value is 137 user-specified (we should watch the whole value) or intermediate 138 (we should watch only the bit used to locate the final value). 139 140 If the final value, or any intermediate value, could not be read 141 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be 142 set to any referenced values. *VALP will never be a lazy value. 143 This is the value which we store in struct breakpoint. 144 145 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be 146 released from the value chain. If VAL_CHAIN is NULL, all generated 147 values will be left on the value chain. */ 148 149 void 150 fetch_subexp_value (struct expression *exp, 151 expr::operation *op, 152 struct value **valp, struct value **resultp, 153 std::vector<value_ref_ptr> *val_chain, 154 bool preserve_errors) 155 { 156 struct value *mark, *new_mark, *result; 157 158 *valp = NULL; 159 if (resultp) 160 *resultp = NULL; 161 if (val_chain) 162 val_chain->clear (); 163 164 /* Evaluate the expression. */ 165 mark = value_mark (); 166 result = NULL; 167 168 try 169 { 170 result = op->evaluate (nullptr, exp, EVAL_NORMAL); 171 } 172 catch (const gdb_exception &ex) 173 { 174 /* Ignore memory errors if we want watchpoints pointing at 175 inaccessible memory to still be created; otherwise, throw the 176 error to some higher catcher. */ 177 switch (ex.error) 178 { 179 case MEMORY_ERROR: 180 if (!preserve_errors) 181 break; 182 /* Fall through. */ 183 default: 184 throw; 185 break; 186 } 187 } 188 189 new_mark = value_mark (); 190 if (mark == new_mark) 191 return; 192 if (resultp) 193 *resultp = result; 194 195 /* Make sure it's not lazy, so that after the target stops again we 196 have a non-lazy previous value to compare with. */ 197 if (result != NULL) 198 { 199 if (!value_lazy (result)) 200 *valp = result; 201 else 202 { 203 204 try 205 { 206 value_fetch_lazy (result); 207 *valp = result; 208 } 209 catch (const gdb_exception_error &except) 210 { 211 } 212 } 213 } 214 215 if (val_chain) 216 { 217 /* Return the chain of intermediate values. We use this to 218 decide which addresses to watch. */ 219 *val_chain = value_release_to_mark (mark); 220 } 221 } 222 223 /* Promote value ARG1 as appropriate before performing a unary operation 224 on this argument. 225 If the result is not appropriate for any particular language then it 226 needs to patch this function. */ 227 228 void 229 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch, 230 struct value **arg1) 231 { 232 struct type *type1; 233 234 *arg1 = coerce_ref (*arg1); 235 type1 = check_typedef (value_type (*arg1)); 236 237 if (is_integral_type (type1)) 238 { 239 switch (language->la_language) 240 { 241 default: 242 /* Perform integral promotion for ANSI C/C++. 243 If not appropriate for any particular language 244 it needs to modify this function. */ 245 { 246 struct type *builtin_int = builtin_type (gdbarch)->builtin_int; 247 248 if (type1->length () < builtin_int->length ()) 249 *arg1 = value_cast (builtin_int, *arg1); 250 } 251 break; 252 } 253 } 254 } 255 256 /* Promote values ARG1 and ARG2 as appropriate before performing a binary 257 operation on those two operands. 258 If the result is not appropriate for any particular language then it 259 needs to patch this function. */ 260 261 void 262 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch, 263 struct value **arg1, struct value **arg2) 264 { 265 struct type *promoted_type = NULL; 266 struct type *type1; 267 struct type *type2; 268 269 *arg1 = coerce_ref (*arg1); 270 *arg2 = coerce_ref (*arg2); 271 272 type1 = check_typedef (value_type (*arg1)); 273 type2 = check_typedef (value_type (*arg2)); 274 275 if ((type1->code () != TYPE_CODE_FLT 276 && type1->code () != TYPE_CODE_DECFLOAT 277 && !is_integral_type (type1)) 278 || (type2->code () != TYPE_CODE_FLT 279 && type2->code () != TYPE_CODE_DECFLOAT 280 && !is_integral_type (type2))) 281 return; 282 283 if (is_fixed_point_type (type1) || is_fixed_point_type (type2)) 284 return; 285 286 if (type1->code () == TYPE_CODE_DECFLOAT 287 || type2->code () == TYPE_CODE_DECFLOAT) 288 { 289 /* No promotion required. */ 290 } 291 else if (type1->code () == TYPE_CODE_FLT 292 || type2->code () == TYPE_CODE_FLT) 293 { 294 switch (language->la_language) 295 { 296 case language_c: 297 case language_cplus: 298 case language_asm: 299 case language_objc: 300 case language_opencl: 301 /* No promotion required. */ 302 break; 303 304 default: 305 /* For other languages the result type is unchanged from gdb 306 version 6.7 for backward compatibility. 307 If either arg was long double, make sure that value is also long 308 double. Otherwise use double. */ 309 if (type1->length () * 8 > gdbarch_double_bit (gdbarch) 310 || type2->length () * 8 > gdbarch_double_bit (gdbarch)) 311 promoted_type = builtin_type (gdbarch)->builtin_long_double; 312 else 313 promoted_type = builtin_type (gdbarch)->builtin_double; 314 break; 315 } 316 } 317 else if (type1->code () == TYPE_CODE_BOOL 318 && type2->code () == TYPE_CODE_BOOL) 319 { 320 /* No promotion required. */ 321 } 322 else 323 /* Integral operations here. */ 324 /* FIXME: Also mixed integral/booleans, with result an integer. */ 325 { 326 const struct builtin_type *builtin = builtin_type (gdbarch); 327 unsigned int promoted_len1 = type1->length (); 328 unsigned int promoted_len2 = type2->length (); 329 int is_unsigned1 = type1->is_unsigned (); 330 int is_unsigned2 = type2->is_unsigned (); 331 unsigned int result_len; 332 int unsigned_operation; 333 334 /* Determine type length and signedness after promotion for 335 both operands. */ 336 if (promoted_len1 < builtin->builtin_int->length ()) 337 { 338 is_unsigned1 = 0; 339 promoted_len1 = builtin->builtin_int->length (); 340 } 341 if (promoted_len2 < builtin->builtin_int->length ()) 342 { 343 is_unsigned2 = 0; 344 promoted_len2 = builtin->builtin_int->length (); 345 } 346 347 if (promoted_len1 > promoted_len2) 348 { 349 unsigned_operation = is_unsigned1; 350 result_len = promoted_len1; 351 } 352 else if (promoted_len2 > promoted_len1) 353 { 354 unsigned_operation = is_unsigned2; 355 result_len = promoted_len2; 356 } 357 else 358 { 359 unsigned_operation = is_unsigned1 || is_unsigned2; 360 result_len = promoted_len1; 361 } 362 363 switch (language->la_language) 364 { 365 case language_c: 366 case language_cplus: 367 case language_asm: 368 case language_objc: 369 if (result_len <= builtin->builtin_int->length ()) 370 { 371 promoted_type = (unsigned_operation 372 ? builtin->builtin_unsigned_int 373 : builtin->builtin_int); 374 } 375 else if (result_len <= builtin->builtin_long->length ()) 376 { 377 promoted_type = (unsigned_operation 378 ? builtin->builtin_unsigned_long 379 : builtin->builtin_long); 380 } 381 else 382 { 383 promoted_type = (unsigned_operation 384 ? builtin->builtin_unsigned_long_long 385 : builtin->builtin_long_long); 386 } 387 break; 388 case language_opencl: 389 if (result_len 390 <= lookup_signed_typename (language, "int")->length()) 391 { 392 promoted_type = 393 (unsigned_operation 394 ? lookup_unsigned_typename (language, "int") 395 : lookup_signed_typename (language, "int")); 396 } 397 else if (result_len 398 <= lookup_signed_typename (language, "long")->length()) 399 { 400 promoted_type = 401 (unsigned_operation 402 ? lookup_unsigned_typename (language, "long") 403 : lookup_signed_typename (language,"long")); 404 } 405 break; 406 default: 407 /* For other languages the result type is unchanged from gdb 408 version 6.7 for backward compatibility. 409 If either arg was long long, make sure that value is also long 410 long. Otherwise use long. */ 411 if (unsigned_operation) 412 { 413 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT) 414 promoted_type = builtin->builtin_unsigned_long_long; 415 else 416 promoted_type = builtin->builtin_unsigned_long; 417 } 418 else 419 { 420 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT) 421 promoted_type = builtin->builtin_long_long; 422 else 423 promoted_type = builtin->builtin_long; 424 } 425 break; 426 } 427 } 428 429 if (promoted_type) 430 { 431 /* Promote both operands to common type. */ 432 *arg1 = value_cast (promoted_type, *arg1); 433 *arg2 = value_cast (promoted_type, *arg2); 434 } 435 } 436 437 static int 438 ptrmath_type_p (const struct language_defn *lang, struct type *type) 439 { 440 type = check_typedef (type); 441 if (TYPE_IS_REFERENCE (type)) 442 type = type->target_type (); 443 444 switch (type->code ()) 445 { 446 case TYPE_CODE_PTR: 447 case TYPE_CODE_FUNC: 448 return 1; 449 450 case TYPE_CODE_ARRAY: 451 return type->is_vector () ? 0 : lang->c_style_arrays_p (); 452 453 default: 454 return 0; 455 } 456 } 457 458 /* Represents a fake method with the given parameter types. This is 459 used by the parser to construct a temporary "expected" type for 460 method overload resolution. FLAGS is used as instance flags of the 461 new type, in order to be able to make the new type represent a 462 const/volatile overload. */ 463 464 class fake_method 465 { 466 public: 467 fake_method (type_instance_flags flags, 468 int num_types, struct type **param_types); 469 ~fake_method (); 470 471 /* The constructed type. */ 472 struct type *type () { return &m_type; } 473 474 private: 475 struct type m_type {}; 476 main_type m_main_type {}; 477 }; 478 479 fake_method::fake_method (type_instance_flags flags, 480 int num_types, struct type **param_types) 481 { 482 struct type *type = &m_type; 483 484 TYPE_MAIN_TYPE (type) = &m_main_type; 485 type->set_length (1); 486 type->set_code (TYPE_CODE_METHOD); 487 TYPE_CHAIN (type) = type; 488 type->set_instance_flags (flags); 489 if (num_types > 0) 490 { 491 if (param_types[num_types - 1] == NULL) 492 { 493 --num_types; 494 type->set_has_varargs (true); 495 } 496 else if (check_typedef (param_types[num_types - 1])->code () 497 == TYPE_CODE_VOID) 498 { 499 --num_types; 500 /* Caller should have ensured this. */ 501 gdb_assert (num_types == 0); 502 type->set_is_prototyped (true); 503 } 504 } 505 506 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by 507 neither an objfile nor a gdbarch. As a result we must manually 508 allocate memory for auxiliary fields, and free the memory ourselves 509 when we are done with it. */ 510 type->set_num_fields (num_types); 511 type->set_fields 512 ((struct field *) xzalloc (sizeof (struct field) * num_types)); 513 514 while (num_types-- > 0) 515 type->field (num_types).set_type (param_types[num_types]); 516 } 517 518 fake_method::~fake_method () 519 { 520 xfree (m_type.fields ()); 521 } 522 523 namespace expr 524 { 525 526 value * 527 type_instance_operation::evaluate (struct type *expect_type, 528 struct expression *exp, 529 enum noside noside) 530 { 531 type_instance_flags flags = std::get<0> (m_storage); 532 std::vector<type *> &types = std::get<1> (m_storage); 533 534 fake_method fake_expect_type (flags, types.size (), types.data ()); 535 return std::get<2> (m_storage)->evaluate (fake_expect_type.type (), 536 exp, noside); 537 } 538 539 } 540 541 /* Helper for evaluating an OP_VAR_VALUE. */ 542 543 value * 544 evaluate_var_value (enum noside noside, const block *blk, symbol *var) 545 { 546 /* JYG: We used to just return value_zero of the symbol type if 547 we're asked to avoid side effects. Otherwise we return 548 value_of_variable (...). However I'm not sure if 549 value_of_variable () has any side effect. We need a full value 550 object returned here for whatis_exp () to call evaluate_type () 551 and then pass the full value to value_rtti_target_type () if we 552 are dealing with a pointer or reference to a base class and print 553 object is on. */ 554 555 struct value *ret = NULL; 556 557 try 558 { 559 ret = value_of_variable (var, blk); 560 } 561 562 catch (const gdb_exception_error &except) 563 { 564 if (noside != EVAL_AVOID_SIDE_EFFECTS) 565 throw; 566 567 ret = value_zero (var->type (), not_lval); 568 } 569 570 return ret; 571 } 572 573 namespace expr 574 575 { 576 577 value * 578 var_value_operation::evaluate (struct type *expect_type, 579 struct expression *exp, 580 enum noside noside) 581 { 582 symbol *var = std::get<0> (m_storage).symbol; 583 if (var->type ()->code () == TYPE_CODE_ERROR) 584 error_unknown_type (var->print_name ()); 585 return evaluate_var_value (noside, std::get<0> (m_storage).block, var); 586 } 587 588 } /* namespace expr */ 589 590 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */ 591 592 value * 593 evaluate_var_msym_value (enum noside noside, 594 struct objfile *objfile, minimal_symbol *msymbol) 595 { 596 CORE_ADDR address; 597 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address); 598 599 if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ()) 600 return value_zero (the_type, not_lval); 601 else 602 return value_at_lazy (the_type, address); 603 } 604 605 /* See expression.h. */ 606 607 value * 608 evaluate_subexp_do_call (expression *exp, enum noside noside, 609 value *callee, 610 gdb::array_view<value *> argvec, 611 const char *function_name, 612 type *default_return_type) 613 { 614 if (callee == NULL) 615 error (_("Cannot evaluate function -- may be inlined")); 616 if (noside == EVAL_AVOID_SIDE_EFFECTS) 617 { 618 /* If the return type doesn't look like a function type, 619 call an error. This can happen if somebody tries to turn 620 a variable into a function call. */ 621 622 type *ftype = value_type (callee); 623 624 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION) 625 { 626 /* We don't know anything about what the internal 627 function might return, but we have to return 628 something. */ 629 return value_zero (builtin_type (exp->gdbarch)->builtin_int, 630 not_lval); 631 } 632 else if (ftype->code () == TYPE_CODE_XMETHOD) 633 { 634 type *return_type = result_type_of_xmethod (callee, argvec); 635 636 if (return_type == NULL) 637 error (_("Xmethod is missing return type.")); 638 return value_zero (return_type, not_lval); 639 } 640 else if (ftype->code () == TYPE_CODE_FUNC 641 || ftype->code () == TYPE_CODE_METHOD) 642 { 643 if (ftype->is_gnu_ifunc ()) 644 { 645 CORE_ADDR address = value_address (callee); 646 type *resolved_type = find_gnu_ifunc_target_type (address); 647 648 if (resolved_type != NULL) 649 ftype = resolved_type; 650 } 651 652 type *return_type = ftype->target_type (); 653 654 if (return_type == NULL) 655 return_type = default_return_type; 656 657 if (return_type == NULL) 658 error_call_unknown_return_type (function_name); 659 660 return allocate_value (return_type); 661 } 662 else 663 error (_("Expression of type other than " 664 "\"Function returning ...\" used as function")); 665 } 666 switch (value_type (callee)->code ()) 667 { 668 case TYPE_CODE_INTERNAL_FUNCTION: 669 return call_internal_function (exp->gdbarch, exp->language_defn, 670 callee, argvec.size (), argvec.data ()); 671 case TYPE_CODE_XMETHOD: 672 return call_xmethod (callee, argvec); 673 default: 674 return call_function_by_hand (callee, default_return_type, argvec); 675 } 676 } 677 678 namespace expr 679 { 680 681 value * 682 operation::evaluate_funcall (struct type *expect_type, 683 struct expression *exp, 684 enum noside noside, 685 const char *function_name, 686 const std::vector<operation_up> &args) 687 { 688 std::vector<value *> vals (args.size ()); 689 690 value *callee = evaluate_with_coercion (exp, noside); 691 struct type *type = value_type (callee); 692 if (type->code () == TYPE_CODE_PTR) 693 type = type->target_type (); 694 for (int i = 0; i < args.size (); ++i) 695 { 696 if (i < type->num_fields ()) 697 vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside); 698 else 699 vals[i] = args[i]->evaluate_with_coercion (exp, noside); 700 } 701 702 return evaluate_subexp_do_call (exp, noside, callee, vals, 703 function_name, expect_type); 704 } 705 706 value * 707 var_value_operation::evaluate_funcall (struct type *expect_type, 708 struct expression *exp, 709 enum noside noside, 710 const std::vector<operation_up> &args) 711 { 712 if (!overload_resolution 713 || exp->language_defn->la_language != language_cplus) 714 return operation::evaluate_funcall (expect_type, exp, noside, args); 715 716 std::vector<value *> argvec (args.size ()); 717 for (int i = 0; i < args.size (); ++i) 718 argvec[i] = args[i]->evaluate_with_coercion (exp, noside); 719 720 struct symbol *symp; 721 find_overload_match (argvec, NULL, NON_METHOD, 722 NULL, std::get<0> (m_storage).symbol, 723 NULL, &symp, NULL, 0, noside); 724 725 if (symp->type ()->code () == TYPE_CODE_ERROR) 726 error_unknown_type (symp->print_name ()); 727 value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block, 728 symp); 729 730 return evaluate_subexp_do_call (exp, noside, callee, argvec, 731 nullptr, expect_type); 732 } 733 734 value * 735 scope_operation::evaluate_funcall (struct type *expect_type, 736 struct expression *exp, 737 enum noside noside, 738 const std::vector<operation_up> &args) 739 { 740 if (!overload_resolution 741 || exp->language_defn->la_language != language_cplus) 742 return operation::evaluate_funcall (expect_type, exp, noside, args); 743 744 /* Unpack it locally so we can properly handle overload 745 resolution. */ 746 const std::string &name = std::get<1> (m_storage); 747 struct type *type = std::get<0> (m_storage); 748 749 symbol *function = NULL; 750 const char *function_name = NULL; 751 std::vector<value *> argvec (1 + args.size ()); 752 if (type->code () == TYPE_CODE_NAMESPACE) 753 { 754 function = cp_lookup_symbol_namespace (type->name (), 755 name.c_str (), 756 get_selected_block (0), 757 VAR_DOMAIN).symbol; 758 if (function == NULL) 759 error (_("No symbol \"%s\" in namespace \"%s\"."), 760 name.c_str (), type->name ()); 761 } 762 else 763 { 764 gdb_assert (type->code () == TYPE_CODE_STRUCT 765 || type->code () == TYPE_CODE_UNION); 766 function_name = name.c_str (); 767 768 /* We need a properly typed value for method lookup. */ 769 argvec[0] = value_zero (type, lval_memory); 770 } 771 772 for (int i = 0; i < args.size (); ++i) 773 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside); 774 gdb::array_view<value *> arg_view = argvec; 775 776 value *callee = nullptr; 777 if (function_name != nullptr) 778 { 779 int static_memfuncp; 780 781 find_overload_match (arg_view, function_name, METHOD, 782 &argvec[0], nullptr, &callee, nullptr, 783 &static_memfuncp, 0, noside); 784 if (!static_memfuncp) 785 { 786 /* For the time being, we don't handle this. */ 787 error (_("Call to overloaded function %s requires " 788 "`this' pointer"), 789 function_name); 790 } 791 792 arg_view = arg_view.slice (1); 793 } 794 else 795 { 796 symbol *symp; 797 arg_view = arg_view.slice (1); 798 find_overload_match (arg_view, nullptr, 799 NON_METHOD, nullptr, function, 800 nullptr, &symp, nullptr, 1, noside); 801 callee = value_of_variable (symp, get_selected_block (0)); 802 } 803 804 return evaluate_subexp_do_call (exp, noside, callee, arg_view, 805 nullptr, expect_type); 806 } 807 808 value * 809 structop_member_base::evaluate_funcall (struct type *expect_type, 810 struct expression *exp, 811 enum noside noside, 812 const std::vector<operation_up> &args) 813 { 814 /* First, evaluate the structure into lhs. */ 815 value *lhs; 816 if (opcode () == STRUCTOP_MEMBER) 817 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside); 818 else 819 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); 820 821 std::vector<value *> vals (args.size () + 1); 822 gdb::array_view<value *> val_view = vals; 823 /* If the function is a virtual function, then the aggregate 824 value (providing the structure) plays its part by providing 825 the vtable. Otherwise, it is just along for the ride: call 826 the function directly. */ 827 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); 828 value *callee; 829 830 type *a1_type = check_typedef (value_type (rhs)); 831 if (a1_type->code () == TYPE_CODE_METHODPTR) 832 { 833 if (noside == EVAL_AVOID_SIDE_EFFECTS) 834 callee = value_zero (a1_type->target_type (), not_lval); 835 else 836 callee = cplus_method_ptr_to_value (&lhs, rhs); 837 838 vals[0] = lhs; 839 } 840 else if (a1_type->code () == TYPE_CODE_MEMBERPTR) 841 { 842 struct type *type_ptr 843 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type)); 844 struct type *target_type_ptr 845 = lookup_pointer_type (a1_type->target_type ()); 846 847 /* Now, convert this value to an address. */ 848 lhs = value_cast (type_ptr, lhs); 849 850 long mem_offset = value_as_long (rhs); 851 852 callee = value_from_pointer (target_type_ptr, 853 value_as_long (lhs) + mem_offset); 854 callee = value_ind (callee); 855 856 val_view = val_view.slice (1); 857 } 858 else 859 error (_("Non-pointer-to-member value used in pointer-to-member " 860 "construct")); 861 862 for (int i = 0; i < args.size (); ++i) 863 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside); 864 865 return evaluate_subexp_do_call (exp, noside, callee, val_view, 866 nullptr, expect_type); 867 868 } 869 870 value * 871 structop_base_operation::evaluate_funcall 872 (struct type *expect_type, struct expression *exp, enum noside noside, 873 const std::vector<operation_up> &args) 874 { 875 /* Allocate space for the function call arguments, Including space for a 876 `this' pointer at the start. */ 877 std::vector<value *> vals (args.size () + 1); 878 /* First, evaluate the structure into vals[0]. */ 879 enum exp_opcode op = opcode (); 880 if (op == STRUCTOP_STRUCT) 881 { 882 /* If v is a variable in a register, and the user types 883 v.method (), this will produce an error, because v has no 884 address. 885 886 A possible way around this would be to allocate a copy of 887 the variable on the stack, copy in the contents, call the 888 function, and copy out the contents. I.e. convert this 889 from call by reference to call by copy-return (or 890 whatever it's called). However, this does not work 891 because it is not the same: the method being called could 892 stash a copy of the address, and then future uses through 893 that address (after the method returns) would be expected 894 to use the variable itself, not some copy of it. */ 895 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside); 896 } 897 else 898 { 899 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); 900 /* Check to see if the operator '->' has been overloaded. 901 If the operator has been overloaded replace vals[0] with the 902 value returned by the custom operator and continue 903 evaluation. */ 904 while (unop_user_defined_p (op, vals[0])) 905 { 906 struct value *value = nullptr; 907 try 908 { 909 value = value_x_unop (vals[0], op, noside); 910 } 911 catch (const gdb_exception_error &except) 912 { 913 if (except.error == NOT_FOUND_ERROR) 914 break; 915 else 916 throw; 917 } 918 919 vals[0] = value; 920 } 921 } 922 923 /* Evaluate the arguments. The '+ 1' here is to allow for the `this' 924 pointer we placed into vals[0]. */ 925 for (int i = 0; i < args.size (); ++i) 926 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside); 927 928 /* The array view includes the `this' pointer. */ 929 gdb::array_view<value *> arg_view (vals); 930 931 int static_memfuncp; 932 value *callee; 933 const char *tstr = std::get<1> (m_storage).c_str (); 934 if (overload_resolution 935 && exp->language_defn->la_language == language_cplus) 936 { 937 /* Language is C++, do some overload resolution before 938 evaluation. */ 939 value *val0 = vals[0]; 940 find_overload_match (arg_view, tstr, METHOD, 941 &val0, nullptr, &callee, nullptr, 942 &static_memfuncp, 0, noside); 943 vals[0] = val0; 944 } 945 else 946 /* Non-C++ case -- or no overload resolution. */ 947 { 948 struct value *temp = vals[0]; 949 950 callee = value_struct_elt (&temp, arg_view, tstr, 951 &static_memfuncp, 952 op == STRUCTOP_STRUCT 953 ? "structure" : "structure pointer"); 954 /* value_struct_elt updates temp with the correct value of the 955 ``this'' pointer if necessary, so modify it to reflect any 956 ``this'' changes. */ 957 vals[0] = value_from_longest (lookup_pointer_type (value_type (temp)), 958 value_address (temp) 959 + value_embedded_offset (temp)); 960 } 961 962 /* Take out `this' if needed. */ 963 if (static_memfuncp) 964 arg_view = arg_view.slice (1); 965 966 return evaluate_subexp_do_call (exp, noside, callee, arg_view, 967 nullptr, expect_type); 968 } 969 970 /* Helper for structop_base_operation::complete which recursively adds 971 field and method names from TYPE, a struct or union type, to the 972 OUTPUT list. PREFIX is prepended to each result. */ 973 974 static void 975 add_struct_fields (struct type *type, completion_list &output, 976 const char *fieldname, int namelen, const char *prefix) 977 { 978 int i; 979 int computed_type_name = 0; 980 const char *type_name = NULL; 981 982 type = check_typedef (type); 983 for (i = 0; i < type->num_fields (); ++i) 984 { 985 if (i < TYPE_N_BASECLASSES (type)) 986 add_struct_fields (TYPE_BASECLASS (type, i), 987 output, fieldname, namelen, prefix); 988 else if (type->field (i).name ()) 989 { 990 if (type->field (i).name ()[0] != '\0') 991 { 992 if (! strncmp (type->field (i).name (), 993 fieldname, namelen)) 994 output.emplace_back (concat (prefix, type->field (i).name (), 995 nullptr)); 996 } 997 else if (type->field (i).type ()->code () == TYPE_CODE_UNION) 998 { 999 /* Recurse into anonymous unions. */ 1000 add_struct_fields (type->field (i).type (), 1001 output, fieldname, namelen, prefix); 1002 } 1003 } 1004 } 1005 1006 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) 1007 { 1008 const char *name = TYPE_FN_FIELDLIST_NAME (type, i); 1009 1010 if (name && ! strncmp (name, fieldname, namelen)) 1011 { 1012 if (!computed_type_name) 1013 { 1014 type_name = type->name (); 1015 computed_type_name = 1; 1016 } 1017 /* Omit constructors from the completion list. */ 1018 if (!type_name || strcmp (type_name, name)) 1019 output.emplace_back (concat (prefix, name, nullptr)); 1020 } 1021 } 1022 } 1023 1024 /* See expop.h. */ 1025 1026 bool 1027 structop_base_operation::complete (struct expression *exp, 1028 completion_tracker &tracker, 1029 const char *prefix) 1030 { 1031 const std::string &fieldname = std::get<1> (m_storage); 1032 1033 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, 1034 EVAL_AVOID_SIDE_EFFECTS); 1035 struct type *type = value_type (lhs); 1036 for (;;) 1037 { 1038 type = check_typedef (type); 1039 if (!type->is_pointer_or_reference ()) 1040 break; 1041 type = type->target_type (); 1042 } 1043 1044 if (type->code () == TYPE_CODE_UNION 1045 || type->code () == TYPE_CODE_STRUCT) 1046 { 1047 completion_list result; 1048 1049 add_struct_fields (type, result, fieldname.c_str (), 1050 fieldname.length (), prefix); 1051 tracker.add_completions (std::move (result)); 1052 return true; 1053 } 1054 1055 return false; 1056 } 1057 1058 } /* namespace expr */ 1059 1060 /* Return true if type is integral or reference to integral */ 1061 1062 static bool 1063 is_integral_or_integral_reference (struct type *type) 1064 { 1065 if (is_integral_type (type)) 1066 return true; 1067 1068 type = check_typedef (type); 1069 return (type != nullptr 1070 && TYPE_IS_REFERENCE (type) 1071 && is_integral_type (type->target_type ())); 1072 } 1073 1074 /* Helper function that implements the body of OP_SCOPE. */ 1075 1076 struct value * 1077 eval_op_scope (struct type *expect_type, struct expression *exp, 1078 enum noside noside, 1079 struct type *type, const char *string) 1080 { 1081 struct value *arg1 = value_aggregate_elt (type, string, expect_type, 1082 0, noside); 1083 if (arg1 == NULL) 1084 error (_("There is no field named %s"), string); 1085 return arg1; 1086 } 1087 1088 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */ 1089 1090 struct value * 1091 eval_op_var_entry_value (struct type *expect_type, struct expression *exp, 1092 enum noside noside, symbol *sym) 1093 { 1094 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1095 return value_zero (sym->type (), not_lval); 1096 1097 if (SYMBOL_COMPUTED_OPS (sym) == NULL 1098 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL) 1099 error (_("Symbol \"%s\" does not have any specific entry value"), 1100 sym->print_name ()); 1101 1102 frame_info_ptr frame = get_selected_frame (NULL); 1103 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame); 1104 } 1105 1106 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */ 1107 1108 struct value * 1109 eval_op_var_msym_value (struct type *expect_type, struct expression *exp, 1110 enum noside noside, bool outermost_p, 1111 bound_minimal_symbol msymbol) 1112 { 1113 value *val = evaluate_var_msym_value (noside, msymbol.objfile, 1114 msymbol.minsym); 1115 1116 struct type *type = value_type (val); 1117 if (type->code () == TYPE_CODE_ERROR 1118 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p)) 1119 error_unknown_type (msymbol.minsym->print_name ()); 1120 return val; 1121 } 1122 1123 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */ 1124 1125 struct value * 1126 eval_op_func_static_var (struct type *expect_type, struct expression *exp, 1127 enum noside noside, 1128 value *func, const char *var) 1129 { 1130 CORE_ADDR addr = value_address (func); 1131 const block *blk = block_for_pc (addr); 1132 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL); 1133 if (sym.symbol == NULL) 1134 error (_("No symbol \"%s\" in specified context."), var); 1135 return evaluate_var_value (noside, sym.block, sym.symbol); 1136 } 1137 1138 /* Helper function that implements the body of OP_REGISTER. */ 1139 1140 struct value * 1141 eval_op_register (struct type *expect_type, struct expression *exp, 1142 enum noside noside, const char *name) 1143 { 1144 int regno; 1145 struct value *val; 1146 1147 regno = user_reg_map_name_to_regnum (exp->gdbarch, 1148 name, strlen (name)); 1149 if (regno == -1) 1150 error (_("Register $%s not available."), name); 1151 1152 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return 1153 a value with the appropriate register type. Unfortunately, 1154 we don't have easy access to the type of user registers. 1155 So for these registers, we fetch the register value regardless 1156 of the evaluation mode. */ 1157 if (noside == EVAL_AVOID_SIDE_EFFECTS 1158 && regno < gdbarch_num_cooked_regs (exp->gdbarch)) 1159 val = value_zero (register_type (exp->gdbarch, regno), not_lval); 1160 else 1161 val = value_of_register (regno, get_selected_frame (NULL)); 1162 if (val == NULL) 1163 error (_("Value of register %s not available."), name); 1164 else 1165 return val; 1166 } 1167 1168 namespace expr 1169 { 1170 1171 value * 1172 string_operation::evaluate (struct type *expect_type, 1173 struct expression *exp, 1174 enum noside noside) 1175 { 1176 const std::string &str = std::get<0> (m_storage); 1177 struct type *type = language_string_char_type (exp->language_defn, 1178 exp->gdbarch); 1179 return value_string (str.c_str (), str.size (), type); 1180 } 1181 1182 } /* namespace expr */ 1183 1184 /* Helper function that implements the body of OP_OBJC_SELECTOR. */ 1185 1186 struct value * 1187 eval_op_objc_selector (struct type *expect_type, struct expression *exp, 1188 enum noside noside, 1189 const char *sel) 1190 { 1191 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr; 1192 return value_from_longest (selector_type, 1193 lookup_child_selector (exp->gdbarch, sel)); 1194 } 1195 1196 /* A helper function for TERNOP_SLICE. */ 1197 1198 struct value * 1199 eval_op_ternop (struct type *expect_type, struct expression *exp, 1200 enum noside noside, 1201 struct value *array, struct value *low, struct value *upper) 1202 { 1203 int lowbound = value_as_long (low); 1204 int upperbound = value_as_long (upper); 1205 return value_slice (array, lowbound, upperbound - lowbound + 1); 1206 } 1207 1208 /* A helper function for STRUCTOP_STRUCT. */ 1209 1210 struct value * 1211 eval_op_structop_struct (struct type *expect_type, struct expression *exp, 1212 enum noside noside, 1213 struct value *arg1, const char *string) 1214 { 1215 struct value *arg3 = value_struct_elt (&arg1, {}, string, 1216 NULL, "structure"); 1217 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1218 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3)); 1219 return arg3; 1220 } 1221 1222 /* A helper function for STRUCTOP_PTR. */ 1223 1224 struct value * 1225 eval_op_structop_ptr (struct type *expect_type, struct expression *exp, 1226 enum noside noside, 1227 struct value *arg1, const char *string) 1228 { 1229 /* Check to see if operator '->' has been overloaded. If so replace 1230 arg1 with the value returned by evaluating operator->(). */ 1231 while (unop_user_defined_p (STRUCTOP_PTR, arg1)) 1232 { 1233 struct value *value = NULL; 1234 try 1235 { 1236 value = value_x_unop (arg1, STRUCTOP_PTR, noside); 1237 } 1238 1239 catch (const gdb_exception_error &except) 1240 { 1241 if (except.error == NOT_FOUND_ERROR) 1242 break; 1243 else 1244 throw; 1245 } 1246 1247 arg1 = value; 1248 } 1249 1250 /* JYG: if print object is on we need to replace the base type 1251 with rtti type in order to continue on with successful 1252 lookup of member / method only available in the rtti type. */ 1253 { 1254 struct type *arg_type = value_type (arg1); 1255 struct type *real_type; 1256 int full, using_enc; 1257 LONGEST top; 1258 struct value_print_options opts; 1259 1260 get_user_print_options (&opts); 1261 if (opts.objectprint && arg_type->target_type () 1262 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT)) 1263 { 1264 real_type = value_rtti_indirect_type (arg1, &full, &top, 1265 &using_enc); 1266 if (real_type) 1267 arg1 = value_cast (real_type, arg1); 1268 } 1269 } 1270 1271 struct value *arg3 = value_struct_elt (&arg1, {}, string, 1272 NULL, "structure pointer"); 1273 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1274 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3)); 1275 return arg3; 1276 } 1277 1278 /* A helper function for STRUCTOP_MEMBER. */ 1279 1280 struct value * 1281 eval_op_member (struct type *expect_type, struct expression *exp, 1282 enum noside noside, 1283 struct value *arg1, struct value *arg2) 1284 { 1285 long mem_offset; 1286 1287 struct value *arg3; 1288 struct type *type = check_typedef (value_type (arg2)); 1289 switch (type->code ()) 1290 { 1291 case TYPE_CODE_METHODPTR: 1292 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1293 return value_zero (type->target_type (), not_lval); 1294 else 1295 { 1296 arg2 = cplus_method_ptr_to_value (&arg1, arg2); 1297 gdb_assert (value_type (arg2)->code () == TYPE_CODE_PTR); 1298 return value_ind (arg2); 1299 } 1300 1301 case TYPE_CODE_MEMBERPTR: 1302 /* Now, convert these values to an address. */ 1303 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)), 1304 arg1, 1); 1305 1306 mem_offset = value_as_long (arg2); 1307 1308 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()), 1309 value_as_long (arg1) + mem_offset); 1310 return value_ind (arg3); 1311 1312 default: 1313 error (_("non-pointer-to-member value used " 1314 "in pointer-to-member construct")); 1315 } 1316 } 1317 1318 /* A helper function for BINOP_ADD. */ 1319 1320 struct value * 1321 eval_op_add (struct type *expect_type, struct expression *exp, 1322 enum noside noside, 1323 struct value *arg1, struct value *arg2) 1324 { 1325 if (binop_user_defined_p (BINOP_ADD, arg1, arg2)) 1326 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside); 1327 else if (ptrmath_type_p (exp->language_defn, value_type (arg1)) 1328 && is_integral_or_integral_reference (value_type (arg2))) 1329 return value_ptradd (arg1, value_as_long (arg2)); 1330 else if (ptrmath_type_p (exp->language_defn, value_type (arg2)) 1331 && is_integral_or_integral_reference (value_type (arg1))) 1332 return value_ptradd (arg2, value_as_long (arg1)); 1333 else 1334 { 1335 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1336 return value_binop (arg1, arg2, BINOP_ADD); 1337 } 1338 } 1339 1340 /* A helper function for BINOP_SUB. */ 1341 1342 struct value * 1343 eval_op_sub (struct type *expect_type, struct expression *exp, 1344 enum noside noside, 1345 struct value *arg1, struct value *arg2) 1346 { 1347 if (binop_user_defined_p (BINOP_SUB, arg1, arg2)) 1348 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside); 1349 else if (ptrmath_type_p (exp->language_defn, value_type (arg1)) 1350 && ptrmath_type_p (exp->language_defn, value_type (arg2))) 1351 { 1352 /* FIXME -- should be ptrdiff_t */ 1353 struct type *type = builtin_type (exp->gdbarch)->builtin_long; 1354 return value_from_longest (type, value_ptrdiff (arg1, arg2)); 1355 } 1356 else if (ptrmath_type_p (exp->language_defn, value_type (arg1)) 1357 && is_integral_or_integral_reference (value_type (arg2))) 1358 return value_ptradd (arg1, - value_as_long (arg2)); 1359 else 1360 { 1361 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1362 return value_binop (arg1, arg2, BINOP_SUB); 1363 } 1364 } 1365 1366 /* Helper function for several different binary operations. */ 1367 1368 struct value * 1369 eval_op_binary (struct type *expect_type, struct expression *exp, 1370 enum noside noside, enum exp_opcode op, 1371 struct value *arg1, struct value *arg2) 1372 { 1373 if (binop_user_defined_p (op, arg1, arg2)) 1374 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1375 else 1376 { 1377 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero, 1378 fudge arg2 to avoid division-by-zero, the caller is 1379 (theoretically) only looking for the type of the result. */ 1380 if (noside == EVAL_AVOID_SIDE_EFFECTS 1381 /* ??? Do we really want to test for BINOP_MOD here? 1382 The implementation of value_binop gives it a well-defined 1383 value. */ 1384 && (op == BINOP_DIV 1385 || op == BINOP_INTDIV 1386 || op == BINOP_REM 1387 || op == BINOP_MOD) 1388 && value_logical_not (arg2)) 1389 { 1390 struct value *v_one; 1391 1392 v_one = value_one (value_type (arg2)); 1393 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one); 1394 return value_binop (arg1, v_one, op); 1395 } 1396 else 1397 { 1398 /* For shift and integer exponentiation operations, 1399 only promote the first argument. */ 1400 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP) 1401 && is_integral_type (value_type (arg2))) 1402 unop_promote (exp->language_defn, exp->gdbarch, &arg1); 1403 else 1404 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1405 1406 return value_binop (arg1, arg2, op); 1407 } 1408 } 1409 } 1410 1411 /* A helper function for BINOP_SUBSCRIPT. */ 1412 1413 struct value * 1414 eval_op_subscript (struct type *expect_type, struct expression *exp, 1415 enum noside noside, enum exp_opcode op, 1416 struct value *arg1, struct value *arg2) 1417 { 1418 if (binop_user_defined_p (op, arg1, arg2)) 1419 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1420 else 1421 { 1422 /* If the user attempts to subscript something that is not an 1423 array or pointer type (like a plain int variable for example), 1424 then report this as an error. */ 1425 1426 arg1 = coerce_ref (arg1); 1427 struct type *type = check_typedef (value_type (arg1)); 1428 if (type->code () != TYPE_CODE_ARRAY 1429 && type->code () != TYPE_CODE_PTR) 1430 { 1431 if (type->name ()) 1432 error (_("cannot subscript something of type `%s'"), 1433 type->name ()); 1434 else 1435 error (_("cannot subscript requested type")); 1436 } 1437 1438 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1439 return value_zero (type->target_type (), VALUE_LVAL (arg1)); 1440 else 1441 return value_subscript (arg1, value_as_long (arg2)); 1442 } 1443 } 1444 1445 /* A helper function for BINOP_EQUAL. */ 1446 1447 struct value * 1448 eval_op_equal (struct type *expect_type, struct expression *exp, 1449 enum noside noside, enum exp_opcode op, 1450 struct value *arg1, struct value *arg2) 1451 { 1452 if (binop_user_defined_p (op, arg1, arg2)) 1453 { 1454 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1455 } 1456 else 1457 { 1458 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1459 int tem = value_equal (arg1, arg2); 1460 struct type *type = language_bool_type (exp->language_defn, 1461 exp->gdbarch); 1462 return value_from_longest (type, (LONGEST) tem); 1463 } 1464 } 1465 1466 /* A helper function for BINOP_NOTEQUAL. */ 1467 1468 struct value * 1469 eval_op_notequal (struct type *expect_type, struct expression *exp, 1470 enum noside noside, enum exp_opcode op, 1471 struct value *arg1, struct value *arg2) 1472 { 1473 if (binop_user_defined_p (op, arg1, arg2)) 1474 { 1475 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1476 } 1477 else 1478 { 1479 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1480 int tem = value_equal (arg1, arg2); 1481 struct type *type = language_bool_type (exp->language_defn, 1482 exp->gdbarch); 1483 return value_from_longest (type, (LONGEST) ! tem); 1484 } 1485 } 1486 1487 /* A helper function for BINOP_LESS. */ 1488 1489 struct value * 1490 eval_op_less (struct type *expect_type, struct expression *exp, 1491 enum noside noside, enum exp_opcode op, 1492 struct value *arg1, struct value *arg2) 1493 { 1494 if (binop_user_defined_p (op, arg1, arg2)) 1495 { 1496 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1497 } 1498 else 1499 { 1500 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1501 int tem = value_less (arg1, arg2); 1502 struct type *type = language_bool_type (exp->language_defn, 1503 exp->gdbarch); 1504 return value_from_longest (type, (LONGEST) tem); 1505 } 1506 } 1507 1508 /* A helper function for BINOP_GTR. */ 1509 1510 struct value * 1511 eval_op_gtr (struct type *expect_type, struct expression *exp, 1512 enum noside noside, enum exp_opcode op, 1513 struct value *arg1, struct value *arg2) 1514 { 1515 if (binop_user_defined_p (op, arg1, arg2)) 1516 { 1517 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1518 } 1519 else 1520 { 1521 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1522 int tem = value_less (arg2, arg1); 1523 struct type *type = language_bool_type (exp->language_defn, 1524 exp->gdbarch); 1525 return value_from_longest (type, (LONGEST) tem); 1526 } 1527 } 1528 1529 /* A helper function for BINOP_GEQ. */ 1530 1531 struct value * 1532 eval_op_geq (struct type *expect_type, struct expression *exp, 1533 enum noside noside, enum exp_opcode op, 1534 struct value *arg1, struct value *arg2) 1535 { 1536 if (binop_user_defined_p (op, arg1, arg2)) 1537 { 1538 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1539 } 1540 else 1541 { 1542 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1543 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2); 1544 struct type *type = language_bool_type (exp->language_defn, 1545 exp->gdbarch); 1546 return value_from_longest (type, (LONGEST) tem); 1547 } 1548 } 1549 1550 /* A helper function for BINOP_LEQ. */ 1551 1552 struct value * 1553 eval_op_leq (struct type *expect_type, struct expression *exp, 1554 enum noside noside, enum exp_opcode op, 1555 struct value *arg1, struct value *arg2) 1556 { 1557 if (binop_user_defined_p (op, arg1, arg2)) 1558 { 1559 return value_x_binop (arg1, arg2, op, OP_NULL, noside); 1560 } 1561 else 1562 { 1563 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); 1564 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2); 1565 struct type *type = language_bool_type (exp->language_defn, 1566 exp->gdbarch); 1567 return value_from_longest (type, (LONGEST) tem); 1568 } 1569 } 1570 1571 /* A helper function for BINOP_REPEAT. */ 1572 1573 struct value * 1574 eval_op_repeat (struct type *expect_type, struct expression *exp, 1575 enum noside noside, enum exp_opcode op, 1576 struct value *arg1, struct value *arg2) 1577 { 1578 struct type *type = check_typedef (value_type (arg2)); 1579 if (type->code () != TYPE_CODE_INT 1580 && type->code () != TYPE_CODE_ENUM) 1581 error (_("Non-integral right operand for \"@\" operator.")); 1582 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1583 { 1584 return allocate_repeat_value (value_type (arg1), 1585 longest_to_int (value_as_long (arg2))); 1586 } 1587 else 1588 return value_repeat (arg1, longest_to_int (value_as_long (arg2))); 1589 } 1590 1591 /* A helper function for UNOP_PLUS. */ 1592 1593 struct value * 1594 eval_op_plus (struct type *expect_type, struct expression *exp, 1595 enum noside noside, enum exp_opcode op, 1596 struct value *arg1) 1597 { 1598 if (unop_user_defined_p (op, arg1)) 1599 return value_x_unop (arg1, op, noside); 1600 else 1601 { 1602 unop_promote (exp->language_defn, exp->gdbarch, &arg1); 1603 return value_pos (arg1); 1604 } 1605 } 1606 1607 /* A helper function for UNOP_NEG. */ 1608 1609 struct value * 1610 eval_op_neg (struct type *expect_type, struct expression *exp, 1611 enum noside noside, enum exp_opcode op, 1612 struct value *arg1) 1613 { 1614 if (unop_user_defined_p (op, arg1)) 1615 return value_x_unop (arg1, op, noside); 1616 else 1617 { 1618 unop_promote (exp->language_defn, exp->gdbarch, &arg1); 1619 return value_neg (arg1); 1620 } 1621 } 1622 1623 /* A helper function for UNOP_COMPLEMENT. */ 1624 1625 struct value * 1626 eval_op_complement (struct type *expect_type, struct expression *exp, 1627 enum noside noside, enum exp_opcode op, 1628 struct value *arg1) 1629 { 1630 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1)) 1631 return value_x_unop (arg1, UNOP_COMPLEMENT, noside); 1632 else 1633 { 1634 unop_promote (exp->language_defn, exp->gdbarch, &arg1); 1635 return value_complement (arg1); 1636 } 1637 } 1638 1639 /* A helper function for UNOP_LOGICAL_NOT. */ 1640 1641 struct value * 1642 eval_op_lognot (struct type *expect_type, struct expression *exp, 1643 enum noside noside, enum exp_opcode op, 1644 struct value *arg1) 1645 { 1646 if (unop_user_defined_p (op, arg1)) 1647 return value_x_unop (arg1, op, noside); 1648 else 1649 { 1650 struct type *type = language_bool_type (exp->language_defn, 1651 exp->gdbarch); 1652 return value_from_longest (type, (LONGEST) value_logical_not (arg1)); 1653 } 1654 } 1655 1656 /* A helper function for UNOP_IND. */ 1657 1658 struct value * 1659 eval_op_ind (struct type *expect_type, struct expression *exp, 1660 enum noside noside, 1661 struct value *arg1) 1662 { 1663 struct type *type = check_typedef (value_type (arg1)); 1664 if (type->code () == TYPE_CODE_METHODPTR 1665 || type->code () == TYPE_CODE_MEMBERPTR) 1666 error (_("Attempt to dereference pointer " 1667 "to member without an object")); 1668 if (unop_user_defined_p (UNOP_IND, arg1)) 1669 return value_x_unop (arg1, UNOP_IND, noside); 1670 else if (noside == EVAL_AVOID_SIDE_EFFECTS) 1671 { 1672 type = check_typedef (value_type (arg1)); 1673 1674 /* If the type pointed to is dynamic then in order to resolve the 1675 dynamic properties we must actually dereference the pointer. 1676 There is a risk that this dereference will have side-effects 1677 in the inferior, but being able to print accurate type 1678 information seems worth the risk. */ 1679 if (!type->is_pointer_or_reference () 1680 || !is_dynamic_type (type->target_type ())) 1681 { 1682 if (type->is_pointer_or_reference () 1683 /* In C you can dereference an array to get the 1st elt. */ 1684 || type->code () == TYPE_CODE_ARRAY) 1685 return value_zero (type->target_type (), 1686 lval_memory); 1687 else if (type->code () == TYPE_CODE_INT) 1688 /* GDB allows dereferencing an int. */ 1689 return value_zero (builtin_type (exp->gdbarch)->builtin_int, 1690 lval_memory); 1691 else 1692 error (_("Attempt to take contents of a non-pointer value.")); 1693 } 1694 } 1695 1696 /* Allow * on an integer so we can cast it to whatever we want. 1697 This returns an int, which seems like the most C-like thing to 1698 do. "long long" variables are rare enough that 1699 BUILTIN_TYPE_LONGEST would seem to be a mistake. */ 1700 if (type->code () == TYPE_CODE_INT) 1701 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int, 1702 (CORE_ADDR) value_as_address (arg1)); 1703 return value_ind (arg1); 1704 } 1705 1706 /* A helper function for UNOP_ALIGNOF. */ 1707 1708 struct value * 1709 eval_op_alignof (struct type *expect_type, struct expression *exp, 1710 enum noside noside, 1711 struct value *arg1) 1712 { 1713 struct type *type = value_type (arg1); 1714 /* FIXME: This should be size_t. */ 1715 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; 1716 ULONGEST align = type_align (type); 1717 if (align == 0) 1718 error (_("could not determine alignment of type")); 1719 return value_from_longest (size_type, align); 1720 } 1721 1722 /* A helper function for UNOP_MEMVAL. */ 1723 1724 struct value * 1725 eval_op_memval (struct type *expect_type, struct expression *exp, 1726 enum noside noside, 1727 struct value *arg1, struct type *type) 1728 { 1729 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1730 return value_zero (type, lval_memory); 1731 else 1732 return value_at_lazy (type, value_as_address (arg1)); 1733 } 1734 1735 /* A helper function for UNOP_PREINCREMENT. */ 1736 1737 struct value * 1738 eval_op_preinc (struct type *expect_type, struct expression *exp, 1739 enum noside noside, enum exp_opcode op, 1740 struct value *arg1) 1741 { 1742 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1743 return arg1; 1744 else if (unop_user_defined_p (op, arg1)) 1745 { 1746 return value_x_unop (arg1, op, noside); 1747 } 1748 else 1749 { 1750 struct value *arg2; 1751 if (ptrmath_type_p (exp->language_defn, value_type (arg1))) 1752 arg2 = value_ptradd (arg1, 1); 1753 else 1754 { 1755 struct value *tmp = arg1; 1756 1757 arg2 = value_one (value_type (arg1)); 1758 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); 1759 arg2 = value_binop (tmp, arg2, BINOP_ADD); 1760 } 1761 1762 return value_assign (arg1, arg2); 1763 } 1764 } 1765 1766 /* A helper function for UNOP_PREDECREMENT. */ 1767 1768 struct value * 1769 eval_op_predec (struct type *expect_type, struct expression *exp, 1770 enum noside noside, enum exp_opcode op, 1771 struct value *arg1) 1772 { 1773 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1774 return arg1; 1775 else if (unop_user_defined_p (op, arg1)) 1776 { 1777 return value_x_unop (arg1, op, noside); 1778 } 1779 else 1780 { 1781 struct value *arg2; 1782 if (ptrmath_type_p (exp->language_defn, value_type (arg1))) 1783 arg2 = value_ptradd (arg1, -1); 1784 else 1785 { 1786 struct value *tmp = arg1; 1787 1788 arg2 = value_one (value_type (arg1)); 1789 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); 1790 arg2 = value_binop (tmp, arg2, BINOP_SUB); 1791 } 1792 1793 return value_assign (arg1, arg2); 1794 } 1795 } 1796 1797 /* A helper function for UNOP_POSTINCREMENT. */ 1798 1799 struct value * 1800 eval_op_postinc (struct type *expect_type, struct expression *exp, 1801 enum noside noside, enum exp_opcode op, 1802 struct value *arg1) 1803 { 1804 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1805 return arg1; 1806 else if (unop_user_defined_p (op, arg1)) 1807 { 1808 return value_x_unop (arg1, op, noside); 1809 } 1810 else 1811 { 1812 struct value *arg3 = value_non_lval (arg1); 1813 struct value *arg2; 1814 1815 if (ptrmath_type_p (exp->language_defn, value_type (arg1))) 1816 arg2 = value_ptradd (arg1, 1); 1817 else 1818 { 1819 struct value *tmp = arg1; 1820 1821 arg2 = value_one (value_type (arg1)); 1822 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); 1823 arg2 = value_binop (tmp, arg2, BINOP_ADD); 1824 } 1825 1826 value_assign (arg1, arg2); 1827 return arg3; 1828 } 1829 } 1830 1831 /* A helper function for UNOP_POSTDECREMENT. */ 1832 1833 struct value * 1834 eval_op_postdec (struct type *expect_type, struct expression *exp, 1835 enum noside noside, enum exp_opcode op, 1836 struct value *arg1) 1837 { 1838 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1839 return arg1; 1840 else if (unop_user_defined_p (op, arg1)) 1841 { 1842 return value_x_unop (arg1, op, noside); 1843 } 1844 else 1845 { 1846 struct value *arg3 = value_non_lval (arg1); 1847 struct value *arg2; 1848 1849 if (ptrmath_type_p (exp->language_defn, value_type (arg1))) 1850 arg2 = value_ptradd (arg1, -1); 1851 else 1852 { 1853 struct value *tmp = arg1; 1854 1855 arg2 = value_one (value_type (arg1)); 1856 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); 1857 arg2 = value_binop (tmp, arg2, BINOP_SUB); 1858 } 1859 1860 value_assign (arg1, arg2); 1861 return arg3; 1862 } 1863 } 1864 1865 /* A helper function for OP_TYPE. */ 1866 1867 struct value * 1868 eval_op_type (struct type *expect_type, struct expression *exp, 1869 enum noside noside, struct type *type) 1870 { 1871 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1872 return allocate_value (type); 1873 else 1874 error (_("Attempt to use a type name as an expression")); 1875 } 1876 1877 /* A helper function for BINOP_ASSIGN_MODIFY. */ 1878 1879 struct value * 1880 eval_binop_assign_modify (struct type *expect_type, struct expression *exp, 1881 enum noside noside, enum exp_opcode op, 1882 struct value *arg1, struct value *arg2) 1883 { 1884 if (noside == EVAL_AVOID_SIDE_EFFECTS) 1885 return arg1; 1886 if (binop_user_defined_p (op, arg1, arg2)) 1887 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside); 1888 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn, 1889 value_type (arg1)) 1890 && is_integral_type (value_type (arg2))) 1891 arg2 = value_ptradd (arg1, value_as_long (arg2)); 1892 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn, 1893 value_type (arg1)) 1894 && is_integral_type (value_type (arg2))) 1895 arg2 = value_ptradd (arg1, - value_as_long (arg2)); 1896 else 1897 { 1898 struct value *tmp = arg1; 1899 1900 /* For shift and integer exponentiation operations, 1901 only promote the first argument. */ 1902 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP) 1903 && is_integral_type (value_type (arg2))) 1904 unop_promote (exp->language_defn, exp->gdbarch, &tmp); 1905 else 1906 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); 1907 1908 arg2 = value_binop (tmp, arg2, op); 1909 } 1910 return value_assign (arg1, arg2); 1911 } 1912 1913 /* Note that ARGS needs 2 empty slots up front and must end with a 1914 null pointer. */ 1915 static struct value * 1916 eval_op_objc_msgcall (struct type *expect_type, struct expression *exp, 1917 enum noside noside, CORE_ADDR selector, 1918 value *target, gdb::array_view<value *> args) 1919 { 1920 CORE_ADDR responds_selector = 0; 1921 CORE_ADDR method_selector = 0; 1922 1923 int struct_return = 0; 1924 1925 struct value *msg_send = NULL; 1926 struct value *msg_send_stret = NULL; 1927 int gnu_runtime = 0; 1928 1929 struct value *method = NULL; 1930 struct value *called_method = NULL; 1931 1932 struct type *selector_type = NULL; 1933 struct type *long_type; 1934 struct type *type; 1935 1936 struct value *ret = NULL; 1937 CORE_ADDR addr = 0; 1938 1939 value *argvec[5]; 1940 1941 long_type = builtin_type (exp->gdbarch)->builtin_long; 1942 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr; 1943 1944 if (value_as_long (target) == 0) 1945 return value_from_longest (long_type, 0); 1946 1947 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym) 1948 gnu_runtime = 1; 1949 1950 /* Find the method dispatch (Apple runtime) or method lookup 1951 (GNU runtime) function for Objective-C. These will be used 1952 to lookup the symbol information for the method. If we 1953 can't find any symbol information, then we'll use these to 1954 call the method, otherwise we can call the method 1955 directly. The msg_send_stret function is used in the special 1956 case of a method that returns a structure (Apple runtime 1957 only). */ 1958 if (gnu_runtime) 1959 { 1960 type = selector_type; 1961 1962 type = lookup_function_type (type); 1963 type = lookup_pointer_type (type); 1964 type = lookup_function_type (type); 1965 type = lookup_pointer_type (type); 1966 1967 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL); 1968 msg_send_stret 1969 = find_function_in_inferior ("objc_msg_lookup", NULL); 1970 1971 msg_send = value_from_pointer (type, value_as_address (msg_send)); 1972 msg_send_stret = value_from_pointer (type, 1973 value_as_address (msg_send_stret)); 1974 } 1975 else 1976 { 1977 msg_send = find_function_in_inferior ("objc_msgSend", NULL); 1978 /* Special dispatcher for methods returning structs. */ 1979 msg_send_stret 1980 = find_function_in_inferior ("objc_msgSend_stret", NULL); 1981 } 1982 1983 /* Verify the target object responds to this method. The 1984 standard top-level 'Object' class uses a different name for 1985 the verification method than the non-standard, but more 1986 often used, 'NSObject' class. Make sure we check for both. */ 1987 1988 responds_selector 1989 = lookup_child_selector (exp->gdbarch, "respondsToSelector:"); 1990 if (responds_selector == 0) 1991 responds_selector 1992 = lookup_child_selector (exp->gdbarch, "respondsTo:"); 1993 1994 if (responds_selector == 0) 1995 error (_("no 'respondsTo:' or 'respondsToSelector:' method")); 1996 1997 method_selector 1998 = lookup_child_selector (exp->gdbarch, "methodForSelector:"); 1999 if (method_selector == 0) 2000 method_selector 2001 = lookup_child_selector (exp->gdbarch, "methodFor:"); 2002 2003 if (method_selector == 0) 2004 error (_("no 'methodFor:' or 'methodForSelector:' method")); 2005 2006 /* Call the verification method, to make sure that the target 2007 class implements the desired method. */ 2008 2009 argvec[0] = msg_send; 2010 argvec[1] = target; 2011 argvec[2] = value_from_longest (long_type, responds_selector); 2012 argvec[3] = value_from_longest (long_type, selector); 2013 argvec[4] = 0; 2014 2015 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); 2016 if (gnu_runtime) 2017 { 2018 /* Function objc_msg_lookup returns a pointer. */ 2019 argvec[0] = ret; 2020 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); 2021 } 2022 if (value_as_long (ret) == 0) 2023 error (_("Target does not respond to this message selector.")); 2024 2025 /* Call "methodForSelector:" method, to get the address of a 2026 function method that implements this selector for this 2027 class. If we can find a symbol at that address, then we 2028 know the return type, parameter types etc. (that's a good 2029 thing). */ 2030 2031 argvec[0] = msg_send; 2032 argvec[1] = target; 2033 argvec[2] = value_from_longest (long_type, method_selector); 2034 argvec[3] = value_from_longest (long_type, selector); 2035 argvec[4] = 0; 2036 2037 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); 2038 if (gnu_runtime) 2039 { 2040 argvec[0] = ret; 2041 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); 2042 } 2043 2044 /* ret should now be the selector. */ 2045 2046 addr = value_as_long (ret); 2047 if (addr) 2048 { 2049 struct symbol *sym = NULL; 2050 2051 /* The address might point to a function descriptor; 2052 resolve it to the actual code address instead. */ 2053 addr = gdbarch_convert_from_func_ptr_addr 2054 (exp->gdbarch, addr, current_inferior ()->top_target ()); 2055 2056 /* Is it a high_level symbol? */ 2057 sym = find_pc_function (addr); 2058 if (sym != NULL) 2059 method = value_of_variable (sym, 0); 2060 } 2061 2062 /* If we found a method with symbol information, check to see 2063 if it returns a struct. Otherwise assume it doesn't. */ 2064 2065 if (method) 2066 { 2067 CORE_ADDR funaddr; 2068 struct type *val_type; 2069 2070 funaddr = find_function_addr (method, &val_type); 2071 2072 block_for_pc (funaddr); 2073 2074 val_type = check_typedef (val_type); 2075 2076 if ((val_type == NULL) 2077 || (val_type->code () == TYPE_CODE_ERROR)) 2078 { 2079 if (expect_type != NULL) 2080 val_type = expect_type; 2081 } 2082 2083 struct_return = using_struct_return (exp->gdbarch, method, 2084 val_type); 2085 } 2086 else if (expect_type != NULL) 2087 { 2088 struct_return = using_struct_return (exp->gdbarch, NULL, 2089 check_typedef (expect_type)); 2090 } 2091 2092 /* Found a function symbol. Now we will substitute its 2093 value in place of the message dispatcher (obj_msgSend), 2094 so that we call the method directly instead of thru 2095 the dispatcher. The main reason for doing this is that 2096 we can now evaluate the return value and parameter values 2097 according to their known data types, in case we need to 2098 do things like promotion, dereferencing, special handling 2099 of structs and doubles, etc. 2100 2101 We want to use the type signature of 'method', but still 2102 jump to objc_msgSend() or objc_msgSend_stret() to better 2103 mimic the behavior of the runtime. */ 2104 2105 if (method) 2106 { 2107 if (value_type (method)->code () != TYPE_CODE_FUNC) 2108 error (_("method address has symbol information " 2109 "with non-function type; skipping")); 2110 2111 /* Create a function pointer of the appropriate type, and 2112 replace its value with the value of msg_send or 2113 msg_send_stret. We must use a pointer here, as 2114 msg_send and msg_send_stret are of pointer type, and 2115 the representation may be different on systems that use 2116 function descriptors. */ 2117 if (struct_return) 2118 called_method 2119 = value_from_pointer (lookup_pointer_type (value_type (method)), 2120 value_as_address (msg_send_stret)); 2121 else 2122 called_method 2123 = value_from_pointer (lookup_pointer_type (value_type (method)), 2124 value_as_address (msg_send)); 2125 } 2126 else 2127 { 2128 if (struct_return) 2129 called_method = msg_send_stret; 2130 else 2131 called_method = msg_send; 2132 } 2133 2134 2135 if (noside == EVAL_AVOID_SIDE_EFFECTS) 2136 { 2137 /* If the return type doesn't look like a function type, 2138 call an error. This can happen if somebody tries to 2139 turn a variable into a function call. This is here 2140 because people often want to call, eg, strcmp, which 2141 gdb doesn't know is a function. If gdb isn't asked for 2142 it's opinion (ie. through "whatis"), it won't offer 2143 it. */ 2144 2145 struct type *callee_type = value_type (called_method); 2146 2147 if (callee_type && callee_type->code () == TYPE_CODE_PTR) 2148 callee_type = callee_type->target_type (); 2149 callee_type = callee_type->target_type (); 2150 2151 if (callee_type) 2152 { 2153 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type) 2154 return allocate_value (expect_type); 2155 else 2156 return allocate_value (callee_type); 2157 } 2158 else 2159 error (_("Expression of type other than " 2160 "\"method returning ...\" used as a method")); 2161 } 2162 2163 /* Now depending on whether we found a symbol for the method, 2164 we will either call the runtime dispatcher or the method 2165 directly. */ 2166 2167 args[0] = target; 2168 args[1] = value_from_longest (long_type, selector); 2169 2170 if (gnu_runtime && (method != NULL)) 2171 { 2172 /* Function objc_msg_lookup returns a pointer. */ 2173 struct type *tem_type = value_type (called_method); 2174 tem_type = lookup_pointer_type (lookup_function_type (tem_type)); 2175 deprecated_set_value_type (called_method, tem_type); 2176 called_method = call_function_by_hand (called_method, NULL, args); 2177 } 2178 2179 return call_function_by_hand (called_method, NULL, args); 2180 } 2181 2182 /* Helper function for MULTI_SUBSCRIPT. */ 2183 2184 static struct value * 2185 eval_multi_subscript (struct type *expect_type, struct expression *exp, 2186 enum noside noside, value *arg1, 2187 gdb::array_view<value *> args) 2188 { 2189 for (value *arg2 : args) 2190 { 2191 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2)) 2192 { 2193 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside); 2194 } 2195 else 2196 { 2197 arg1 = coerce_ref (arg1); 2198 struct type *type = check_typedef (value_type (arg1)); 2199 2200 switch (type->code ()) 2201 { 2202 case TYPE_CODE_PTR: 2203 case TYPE_CODE_ARRAY: 2204 case TYPE_CODE_STRING: 2205 arg1 = value_subscript (arg1, value_as_long (arg2)); 2206 break; 2207 2208 default: 2209 if (type->name ()) 2210 error (_("cannot subscript something of type `%s'"), 2211 type->name ()); 2212 else 2213 error (_("cannot subscript requested type")); 2214 } 2215 } 2216 } 2217 return (arg1); 2218 } 2219 2220 namespace expr 2221 { 2222 2223 value * 2224 objc_msgcall_operation::evaluate (struct type *expect_type, 2225 struct expression *exp, 2226 enum noside noside) 2227 { 2228 enum noside sub_no_side = EVAL_NORMAL; 2229 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr; 2230 2231 if (noside == EVAL_AVOID_SIDE_EFFECTS) 2232 sub_no_side = EVAL_NORMAL; 2233 else 2234 sub_no_side = noside; 2235 value *target 2236 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side); 2237 2238 if (value_as_long (target) == 0) 2239 sub_no_side = EVAL_AVOID_SIDE_EFFECTS; 2240 else 2241 sub_no_side = noside; 2242 std::vector<operation_up> &args = std::get<2> (m_storage); 2243 value **argvec = XALLOCAVEC (struct value *, args.size () + 3); 2244 argvec[0] = nullptr; 2245 argvec[1] = nullptr; 2246 for (int i = 0; i < args.size (); ++i) 2247 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side); 2248 argvec[args.size () + 2] = nullptr; 2249 2250 return eval_op_objc_msgcall (expect_type, exp, noside, std:: 2251 get<0> (m_storage), target, 2252 gdb::make_array_view (argvec, 2253 args.size () + 3)); 2254 } 2255 2256 value * 2257 multi_subscript_operation::evaluate (struct type *expect_type, 2258 struct expression *exp, 2259 enum noside noside) 2260 { 2261 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside); 2262 std::vector<operation_up> &values = std::get<1> (m_storage); 2263 value **argvec = XALLOCAVEC (struct value *, values.size ()); 2264 for (int ix = 0; ix < values.size (); ++ix) 2265 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside); 2266 return eval_multi_subscript (expect_type, exp, noside, arg1, 2267 gdb::make_array_view (argvec, values.size ())); 2268 } 2269 2270 value * 2271 logical_and_operation::evaluate (struct type *expect_type, 2272 struct expression *exp, 2273 enum noside noside) 2274 { 2275 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); 2276 2277 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, 2278 EVAL_AVOID_SIDE_EFFECTS); 2279 2280 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2)) 2281 { 2282 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); 2283 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside); 2284 } 2285 else 2286 { 2287 bool tem = value_logical_not (arg1); 2288 if (!tem) 2289 { 2290 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); 2291 tem = value_logical_not (arg2); 2292 } 2293 struct type *type = language_bool_type (exp->language_defn, 2294 exp->gdbarch); 2295 return value_from_longest (type, !tem); 2296 } 2297 } 2298 2299 value * 2300 logical_or_operation::evaluate (struct type *expect_type, 2301 struct expression *exp, 2302 enum noside noside) 2303 { 2304 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); 2305 2306 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, 2307 EVAL_AVOID_SIDE_EFFECTS); 2308 2309 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2)) 2310 { 2311 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); 2312 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside); 2313 } 2314 else 2315 { 2316 bool tem = value_logical_not (arg1); 2317 if (tem) 2318 { 2319 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); 2320 tem = value_logical_not (arg2); 2321 } 2322 2323 struct type *type = language_bool_type (exp->language_defn, 2324 exp->gdbarch); 2325 return value_from_longest (type, !tem); 2326 } 2327 } 2328 2329 value * 2330 adl_func_operation::evaluate (struct type *expect_type, 2331 struct expression *exp, 2332 enum noside noside) 2333 { 2334 std::vector<operation_up> &arg_ops = std::get<2> (m_storage); 2335 std::vector<value *> args (arg_ops.size ()); 2336 for (int i = 0; i < arg_ops.size (); ++i) 2337 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside); 2338 2339 struct symbol *symp; 2340 find_overload_match (args, std::get<0> (m_storage).c_str (), 2341 NON_METHOD, 2342 nullptr, nullptr, 2343 nullptr, &symp, nullptr, 0, noside); 2344 if (symp->type ()->code () == TYPE_CODE_ERROR) 2345 error_unknown_type (symp->print_name ()); 2346 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp); 2347 return evaluate_subexp_do_call (exp, noside, callee, args, 2348 nullptr, expect_type); 2349 2350 } 2351 2352 /* This function evaluates brace-initializers (in C/C++) for 2353 structure types. */ 2354 2355 struct value * 2356 array_operation::evaluate_struct_tuple (struct value *struct_val, 2357 struct expression *exp, 2358 enum noside noside, int nargs) 2359 { 2360 const std::vector<operation_up> &in_args = std::get<2> (m_storage); 2361 struct type *struct_type = check_typedef (value_type (struct_val)); 2362 struct type *field_type; 2363 int fieldno = -1; 2364 2365 int idx = 0; 2366 while (--nargs >= 0) 2367 { 2368 struct value *val = NULL; 2369 int bitpos, bitsize; 2370 bfd_byte *addr; 2371 2372 fieldno++; 2373 /* Skip static fields. */ 2374 while (fieldno < struct_type->num_fields () 2375 && field_is_static (&struct_type->field (fieldno))) 2376 fieldno++; 2377 if (fieldno >= struct_type->num_fields ()) 2378 error (_("too many initializers")); 2379 field_type = struct_type->field (fieldno).type (); 2380 if (field_type->code () == TYPE_CODE_UNION 2381 && struct_type->field (fieldno).name ()[0] == '0') 2382 error (_("don't know which variant you want to set")); 2383 2384 /* Here, struct_type is the type of the inner struct, 2385 while substruct_type is the type of the inner struct. 2386 These are the same for normal structures, but a variant struct 2387 contains anonymous union fields that contain substruct fields. 2388 The value fieldno is the index of the top-level (normal or 2389 anonymous union) field in struct_field, while the value 2390 subfieldno is the index of the actual real (named inner) field 2391 in substruct_type. */ 2392 2393 field_type = struct_type->field (fieldno).type (); 2394 if (val == 0) 2395 val = in_args[idx++]->evaluate (field_type, exp, noside); 2396 2397 /* Now actually set the field in struct_val. */ 2398 2399 /* Assign val to field fieldno. */ 2400 if (value_type (val) != field_type) 2401 val = value_cast (field_type, val); 2402 2403 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno); 2404 bitpos = struct_type->field (fieldno).loc_bitpos (); 2405 addr = value_contents_writeable (struct_val).data () + bitpos / 8; 2406 if (bitsize) 2407 modify_field (struct_type, addr, 2408 value_as_long (val), bitpos % 8, bitsize); 2409 else 2410 memcpy (addr, value_contents (val).data (), 2411 value_type (val)->length ()); 2412 2413 } 2414 return struct_val; 2415 } 2416 2417 value * 2418 array_operation::evaluate (struct type *expect_type, 2419 struct expression *exp, 2420 enum noside noside) 2421 { 2422 int tem; 2423 int tem2 = std::get<0> (m_storage); 2424 int tem3 = std::get<1> (m_storage); 2425 const std::vector<operation_up> &in_args = std::get<2> (m_storage); 2426 int nargs = tem3 - tem2 + 1; 2427 struct type *type = expect_type ? check_typedef (expect_type) : nullptr; 2428 2429 if (expect_type != nullptr 2430 && type->code () == TYPE_CODE_STRUCT) 2431 { 2432 struct value *rec = allocate_value (expect_type); 2433 2434 memset (value_contents_raw (rec).data (), '\0', type->length ()); 2435 return evaluate_struct_tuple (rec, exp, noside, nargs); 2436 } 2437 2438 if (expect_type != nullptr 2439 && type->code () == TYPE_CODE_ARRAY) 2440 { 2441 struct type *range_type = type->index_type (); 2442 struct type *element_type = type->target_type (); 2443 struct value *array = allocate_value (expect_type); 2444 int element_size = check_typedef (element_type)->length (); 2445 LONGEST low_bound, high_bound, index; 2446 2447 if (!get_discrete_bounds (range_type, &low_bound, &high_bound)) 2448 { 2449 low_bound = 0; 2450 high_bound = (type->length () / element_size) - 1; 2451 } 2452 index = low_bound; 2453 memset (value_contents_raw (array).data (), 0, expect_type->length ()); 2454 for (tem = nargs; --nargs >= 0;) 2455 { 2456 struct value *element; 2457 2458 element = in_args[index - low_bound]->evaluate (element_type, 2459 exp, noside); 2460 if (value_type (element) != element_type) 2461 element = value_cast (element_type, element); 2462 if (index > high_bound) 2463 /* To avoid memory corruption. */ 2464 error (_("Too many array elements")); 2465 memcpy (value_contents_raw (array).data () 2466 + (index - low_bound) * element_size, 2467 value_contents (element).data (), 2468 element_size); 2469 index++; 2470 } 2471 return array; 2472 } 2473 2474 if (expect_type != nullptr 2475 && type->code () == TYPE_CODE_SET) 2476 { 2477 struct value *set = allocate_value (expect_type); 2478 gdb_byte *valaddr = value_contents_raw (set).data (); 2479 struct type *element_type = type->index_type (); 2480 struct type *check_type = element_type; 2481 LONGEST low_bound, high_bound; 2482 2483 /* Get targettype of elementtype. */ 2484 while (check_type->code () == TYPE_CODE_RANGE 2485 || check_type->code () == TYPE_CODE_TYPEDEF) 2486 check_type = check_type->target_type (); 2487 2488 if (!get_discrete_bounds (element_type, &low_bound, &high_bound)) 2489 error (_("(power)set type with unknown size")); 2490 memset (valaddr, '\0', type->length ()); 2491 int idx = 0; 2492 for (tem = 0; tem < nargs; tem++) 2493 { 2494 LONGEST range_low, range_high; 2495 struct type *range_low_type, *range_high_type; 2496 struct value *elem_val; 2497 2498 elem_val = in_args[idx++]->evaluate (element_type, exp, noside); 2499 range_low_type = range_high_type = value_type (elem_val); 2500 range_low = range_high = value_as_long (elem_val); 2501 2502 /* Check types of elements to avoid mixture of elements from 2503 different types. Also check if type of element is "compatible" 2504 with element type of powerset. */ 2505 if (range_low_type->code () == TYPE_CODE_RANGE) 2506 range_low_type = range_low_type->target_type (); 2507 if (range_high_type->code () == TYPE_CODE_RANGE) 2508 range_high_type = range_high_type->target_type (); 2509 if ((range_low_type->code () != range_high_type->code ()) 2510 || (range_low_type->code () == TYPE_CODE_ENUM 2511 && (range_low_type != range_high_type))) 2512 /* different element modes. */ 2513 error (_("POWERSET tuple elements of different mode")); 2514 if ((check_type->code () != range_low_type->code ()) 2515 || (check_type->code () == TYPE_CODE_ENUM 2516 && range_low_type != check_type)) 2517 error (_("incompatible POWERSET tuple elements")); 2518 if (range_low > range_high) 2519 { 2520 warning (_("empty POWERSET tuple range")); 2521 continue; 2522 } 2523 if (range_low < low_bound || range_high > high_bound) 2524 error (_("POWERSET tuple element out of range")); 2525 range_low -= low_bound; 2526 range_high -= low_bound; 2527 for (; range_low <= range_high; range_low++) 2528 { 2529 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT; 2530 2531 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG) 2532 bit_index = TARGET_CHAR_BIT - 1 - bit_index; 2533 valaddr[(unsigned) range_low / TARGET_CHAR_BIT] 2534 |= 1 << bit_index; 2535 } 2536 } 2537 return set; 2538 } 2539 2540 value **argvec = XALLOCAVEC (struct value *, nargs); 2541 for (tem = 0; tem < nargs; tem++) 2542 { 2543 /* Ensure that array expressions are coerced into pointer 2544 objects. */ 2545 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside); 2546 } 2547 return value_array (tem2, tem3, argvec); 2548 } 2549 2550 value * 2551 unop_extract_operation::evaluate (struct type *expect_type, 2552 struct expression *exp, 2553 enum noside noside) 2554 { 2555 value *old_value = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); 2556 struct type *type = get_type (); 2557 2558 if (type->length () > value_type (old_value)->length ()) 2559 error (_("length type is larger than the value type")); 2560 2561 struct value *result = allocate_value (type); 2562 value_contents_copy (result, 0, old_value, 0, type->length ()); 2563 return result; 2564 } 2565 2566 } 2567 2568 2569 /* Helper for evaluate_subexp_for_address. */ 2570 2571 static value * 2572 evaluate_subexp_for_address_base (struct expression *exp, enum noside noside, 2573 value *x) 2574 { 2575 if (noside == EVAL_AVOID_SIDE_EFFECTS) 2576 { 2577 struct type *type = check_typedef (value_type (x)); 2578 2579 if (TYPE_IS_REFERENCE (type)) 2580 return value_zero (lookup_pointer_type (type->target_type ()), 2581 not_lval); 2582 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x)) 2583 return value_zero (lookup_pointer_type (value_type (x)), 2584 not_lval); 2585 else 2586 error (_("Attempt to take address of " 2587 "value not located in memory.")); 2588 } 2589 return value_addr (x); 2590 } 2591 2592 namespace expr 2593 { 2594 2595 value * 2596 operation::evaluate_for_cast (struct type *expect_type, 2597 struct expression *exp, 2598 enum noside noside) 2599 { 2600 value *val = evaluate (expect_type, exp, noside); 2601 return value_cast (expect_type, val); 2602 } 2603 2604 value * 2605 operation::evaluate_for_address (struct expression *exp, enum noside noside) 2606 { 2607 value *val = evaluate (nullptr, exp, noside); 2608 return evaluate_subexp_for_address_base (exp, noside, val); 2609 } 2610 2611 value * 2612 scope_operation::evaluate_for_address (struct expression *exp, 2613 enum noside noside) 2614 { 2615 value *x = value_aggregate_elt (std::get<0> (m_storage), 2616 std::get<1> (m_storage).c_str (), 2617 NULL, 1, noside); 2618 if (x == NULL) 2619 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ()); 2620 return x; 2621 } 2622 2623 value * 2624 unop_ind_base_operation::evaluate_for_address (struct expression *exp, 2625 enum noside noside) 2626 { 2627 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); 2628 2629 /* We can't optimize out "&*" if there's a user-defined operator*. */ 2630 if (unop_user_defined_p (UNOP_IND, x)) 2631 { 2632 x = value_x_unop (x, UNOP_IND, noside); 2633 return evaluate_subexp_for_address_base (exp, noside, x); 2634 } 2635 2636 return coerce_array (x); 2637 } 2638 2639 value * 2640 var_msym_value_operation::evaluate_for_address (struct expression *exp, 2641 enum noside noside) 2642 { 2643 const bound_minimal_symbol &b = std::get<0> (m_storage); 2644 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym); 2645 if (noside == EVAL_AVOID_SIDE_EFFECTS) 2646 { 2647 struct type *type = lookup_pointer_type (value_type (val)); 2648 return value_zero (type, not_lval); 2649 } 2650 else 2651 return value_addr (val); 2652 } 2653 2654 value * 2655 unop_memval_operation::evaluate_for_address (struct expression *exp, 2656 enum noside noside) 2657 { 2658 return value_cast (lookup_pointer_type (std::get<1> (m_storage)), 2659 std::get<0> (m_storage)->evaluate (nullptr, exp, noside)); 2660 } 2661 2662 value * 2663 unop_memval_type_operation::evaluate_for_address (struct expression *exp, 2664 enum noside noside) 2665 { 2666 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp, 2667 EVAL_AVOID_SIDE_EFFECTS); 2668 struct type *type = value_type (typeval); 2669 return value_cast (lookup_pointer_type (type), 2670 std::get<1> (m_storage)->evaluate (nullptr, exp, noside)); 2671 } 2672 2673 value * 2674 var_value_operation::evaluate_for_address (struct expression *exp, 2675 enum noside noside) 2676 { 2677 symbol *var = std::get<0> (m_storage).symbol; 2678 2679 /* C++: The "address" of a reference should yield the address 2680 * of the object pointed to. Let value_addr() deal with it. */ 2681 if (TYPE_IS_REFERENCE (var->type ())) 2682 return operation::evaluate_for_address (exp, noside); 2683 2684 if (noside == EVAL_AVOID_SIDE_EFFECTS) 2685 { 2686 struct type *type = lookup_pointer_type (var->type ()); 2687 enum address_class sym_class = var->aclass (); 2688 2689 if (sym_class == LOC_CONST 2690 || sym_class == LOC_CONST_BYTES 2691 || sym_class == LOC_REGISTER) 2692 error (_("Attempt to take address of register or constant.")); 2693 2694 return value_zero (type, not_lval); 2695 } 2696 else 2697 return address_of_variable (var, std::get<0> (m_storage).block); 2698 } 2699 2700 value * 2701 var_value_operation::evaluate_with_coercion (struct expression *exp, 2702 enum noside noside) 2703 { 2704 struct symbol *var = std::get<0> (m_storage).symbol; 2705 struct type *type = check_typedef (var->type ()); 2706 if (type->code () == TYPE_CODE_ARRAY 2707 && !type->is_vector () 2708 && CAST_IS_CONVERSION (exp->language_defn)) 2709 { 2710 struct value *val = address_of_variable (var, 2711 std::get<0> (m_storage).block); 2712 return value_cast (lookup_pointer_type (type->target_type ()), val); 2713 } 2714 return evaluate (nullptr, exp, noside); 2715 } 2716 2717 } 2718 2719 /* Helper function for evaluating the size of a type. */ 2720 2721 static value * 2722 evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type) 2723 { 2724 /* FIXME: This should be size_t. */ 2725 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; 2726 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof: 2727 "When applied to a reference or a reference type, the result is 2728 the size of the referenced type." */ 2729 type = check_typedef (type); 2730 if (exp->language_defn->la_language == language_cplus 2731 && (TYPE_IS_REFERENCE (type))) 2732 type = check_typedef (type->target_type ()); 2733 return value_from_longest (size_type, (LONGEST) type->length ()); 2734 } 2735 2736 namespace expr 2737 { 2738 2739 value * 2740 operation::evaluate_for_sizeof (struct expression *exp, enum noside noside) 2741 { 2742 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS); 2743 return evaluate_subexp_for_sizeof_base (exp, value_type (val)); 2744 } 2745 2746 value * 2747 var_msym_value_operation::evaluate_for_sizeof (struct expression *exp, 2748 enum noside noside) 2749 2750 { 2751 const bound_minimal_symbol &b = std::get<0> (m_storage); 2752 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym); 2753 2754 struct type *type = value_type (mval); 2755 if (type->code () == TYPE_CODE_ERROR) 2756 error_unknown_type (b.minsym->print_name ()); 2757 2758 /* FIXME: This should be size_t. */ 2759 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; 2760 return value_from_longest (size_type, type->length ()); 2761 } 2762 2763 value * 2764 subscript_operation::evaluate_for_sizeof (struct expression *exp, 2765 enum noside noside) 2766 { 2767 if (noside == EVAL_NORMAL) 2768 { 2769 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, 2770 EVAL_AVOID_SIDE_EFFECTS); 2771 struct type *type = check_typedef (value_type (val)); 2772 if (type->code () == TYPE_CODE_ARRAY) 2773 { 2774 type = check_typedef (type->target_type ()); 2775 if (type->code () == TYPE_CODE_ARRAY) 2776 { 2777 type = type->index_type (); 2778 /* Only re-evaluate the right hand side if the resulting type 2779 is a variable length type. */ 2780 if (type->bounds ()->flag_bound_evaluated) 2781 { 2782 val = evaluate (nullptr, exp, EVAL_NORMAL); 2783 /* FIXME: This should be size_t. */ 2784 struct type *size_type 2785 = builtin_type (exp->gdbarch)->builtin_int; 2786 return value_from_longest 2787 (size_type, (LONGEST) value_type (val)->length ()); 2788 } 2789 } 2790 } 2791 } 2792 2793 return operation::evaluate_for_sizeof (exp, noside); 2794 } 2795 2796 value * 2797 unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp, 2798 enum noside noside) 2799 { 2800 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, 2801 EVAL_AVOID_SIDE_EFFECTS); 2802 struct type *type = check_typedef (value_type (val)); 2803 if (!type->is_pointer_or_reference () 2804 && type->code () != TYPE_CODE_ARRAY) 2805 error (_("Attempt to take contents of a non-pointer value.")); 2806 type = type->target_type (); 2807 if (is_dynamic_type (type)) 2808 type = value_type (value_ind (val)); 2809 /* FIXME: This should be size_t. */ 2810 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; 2811 return value_from_longest (size_type, (LONGEST) type->length ()); 2812 } 2813 2814 value * 2815 unop_memval_operation::evaluate_for_sizeof (struct expression *exp, 2816 enum noside noside) 2817 { 2818 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage)); 2819 } 2820 2821 value * 2822 unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp, 2823 enum noside noside) 2824 { 2825 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp, 2826 EVAL_AVOID_SIDE_EFFECTS); 2827 return evaluate_subexp_for_sizeof_base (exp, value_type (typeval)); 2828 } 2829 2830 value * 2831 var_value_operation::evaluate_for_sizeof (struct expression *exp, 2832 enum noside noside) 2833 { 2834 struct type *type = std::get<0> (m_storage).symbol->type (); 2835 if (is_dynamic_type (type)) 2836 { 2837 value *val = evaluate (nullptr, exp, EVAL_NORMAL); 2838 type = value_type (val); 2839 if (type->code () == TYPE_CODE_ARRAY) 2840 { 2841 /* FIXME: This should be size_t. */ 2842 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; 2843 if (type_not_allocated (type) || type_not_associated (type)) 2844 return value_zero (size_type, not_lval); 2845 else if (is_dynamic_type (type->index_type ()) 2846 && type->bounds ()->high.kind () == PROP_UNDEFINED) 2847 return allocate_optimized_out_value (size_type); 2848 } 2849 } 2850 return evaluate_subexp_for_sizeof_base (exp, type); 2851 } 2852 2853 value * 2854 var_msym_value_operation::evaluate_for_cast (struct type *to_type, 2855 struct expression *exp, 2856 enum noside noside) 2857 { 2858 if (noside == EVAL_AVOID_SIDE_EFFECTS) 2859 return value_zero (to_type, not_lval); 2860 2861 const bound_minimal_symbol &b = std::get<0> (m_storage); 2862 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym); 2863 2864 val = value_cast (to_type, val); 2865 2866 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */ 2867 if (VALUE_LVAL (val) == lval_memory) 2868 { 2869 if (value_lazy (val)) 2870 value_fetch_lazy (val); 2871 VALUE_LVAL (val) = not_lval; 2872 } 2873 return val; 2874 } 2875 2876 value * 2877 var_value_operation::evaluate_for_cast (struct type *to_type, 2878 struct expression *exp, 2879 enum noside noside) 2880 { 2881 value *val = evaluate_var_value (noside, 2882 std::get<0> (m_storage).block, 2883 std::get<0> (m_storage).symbol); 2884 2885 val = value_cast (to_type, val); 2886 2887 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */ 2888 if (VALUE_LVAL (val) == lval_memory) 2889 { 2890 if (value_lazy (val)) 2891 value_fetch_lazy (val); 2892 VALUE_LVAL (val) = not_lval; 2893 } 2894 return val; 2895 } 2896 2897 } 2898 2899 /* Parse a type expression in the string [P..P+LENGTH). */ 2900 2901 struct type * 2902 parse_and_eval_type (const char *p, int length) 2903 { 2904 char *tmp = (char *) alloca (length + 4); 2905 2906 tmp[0] = '('; 2907 memcpy (tmp + 1, p, length); 2908 tmp[length + 1] = ')'; 2909 tmp[length + 2] = '0'; 2910 tmp[length + 3] = '\0'; 2911 expression_up expr = parse_expression (tmp); 2912 expr::unop_cast_operation *op 2913 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ()); 2914 if (op == nullptr) 2915 error (_("Internal error in eval_type.")); 2916 return op->get_type (); 2917 } 2918