eval.c 85 KB

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