ada-exp.y 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. /* YACC parser for Ada 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. /* Parse an Ada expression from text in a string,
  15. and return the result as a struct expression pointer.
  16. That structure contains arithmetic operations in reverse polish,
  17. with constants represented by operations that are followed by special data.
  18. See expression.h for the details of the format.
  19. What is important here is that it can be built up sequentially
  20. during the process of parsing; the lower levels of the tree always
  21. come first in the result.
  22. malloc's and realloc's in this file are transformed to
  23. xmalloc and xrealloc respectively by the same sed command in the
  24. makefile that remaps any other malloc/realloc inserted by the parser
  25. generator. Doing this with #defines and trying to control the interaction
  26. with include files (<malloc.h> and <stdlib.h> for example) just became
  27. too messy, particularly when such includes can be inserted at random
  28. times by the parser generator. */
  29. %{
  30. #include "defs.h"
  31. #include <ctype.h>
  32. #include "expression.h"
  33. #include "value.h"
  34. #include "parser-defs.h"
  35. #include "language.h"
  36. #include "ada-lang.h"
  37. #include "bfd.h" /* Required by objfiles.h. */
  38. #include "symfile.h" /* Required by objfiles.h. */
  39. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  40. #include "frame.h"
  41. #include "block.h"
  42. #include "ada-exp.h"
  43. #define parse_type(ps) builtin_type (ps->gdbarch ())
  44. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
  45. etc). */
  46. #define GDB_YY_REMAP_PREFIX ada_
  47. #include "yy-remap.h"
  48. struct name_info {
  49. struct symbol *sym;
  50. struct minimal_symbol *msym;
  51. const struct block *block;
  52. struct stoken stoken;
  53. };
  54. /* The state of the parser, used internally when we are parsing the
  55. expression. */
  56. static struct parser_state *pstate = NULL;
  57. /* The original expression string. */
  58. static const char *original_expr;
  59. int yyparse (void);
  60. static int yylex (void);
  61. static void yyerror (const char *);
  62. static void write_int (struct parser_state *, LONGEST, struct type *);
  63. static void write_object_renaming (struct parser_state *,
  64. const struct block *, const char *, int,
  65. const char *, int);
  66. static struct type* write_var_or_type (struct parser_state *,
  67. const struct block *, struct stoken);
  68. static struct type *write_var_or_type_completion (struct parser_state *,
  69. const struct block *,
  70. struct stoken);
  71. static void write_name_assoc (struct parser_state *, struct stoken);
  72. static const struct block *block_lookup (const struct block *, const char *);
  73. static void write_ambiguous_var (struct parser_state *,
  74. const struct block *, const char *, int);
  75. static struct type *type_int (struct parser_state *);
  76. static struct type *type_long (struct parser_state *);
  77. static struct type *type_long_long (struct parser_state *);
  78. static struct type *type_long_double (struct parser_state *);
  79. static struct type *type_for_char (struct parser_state *, ULONGEST);
  80. static struct type *type_boolean (struct parser_state *);
  81. static struct type *type_system_address (struct parser_state *);
  82. static std::string find_completion_bounds (struct parser_state *);
  83. using namespace expr;
  84. /* Handle Ada type resolution for OP. DEPROCEDURE_P and CONTEXT_TYPE
  85. are passed to the resolve method, if called. */
  86. static operation_up
  87. resolve (operation_up &&op, bool deprocedure_p, struct type *context_type)
  88. {
  89. operation_up result = std::move (op);
  90. ada_resolvable *res = dynamic_cast<ada_resolvable *> (result.get ());
  91. if (res != nullptr)
  92. return res->replace (std::move (result),
  93. pstate->expout.get (),
  94. deprocedure_p,
  95. pstate->parse_completion,
  96. pstate->block_tracker,
  97. context_type);
  98. return result;
  99. }
  100. /* Like parser_state::pop, but handles Ada type resolution.
  101. DEPROCEDURE_P and CONTEXT_TYPE are passed to the resolve method, if
  102. called. */
  103. static operation_up
  104. ada_pop (bool deprocedure_p = true, struct type *context_type = nullptr)
  105. {
  106. /* Of course it's ok to call parser_state::pop here... */
  107. return resolve (pstate->pop (), deprocedure_p, context_type);
  108. }
  109. /* Like parser_state::wrap, but use ada_pop to pop the value. */
  110. template<typename T>
  111. void
  112. ada_wrap ()
  113. {
  114. operation_up arg = ada_pop ();
  115. pstate->push_new<T> (std::move (arg));
  116. }
  117. /* Create and push an address-of operation, as appropriate for Ada.
  118. If TYPE is not NULL, the resulting operation will be wrapped in a
  119. cast to TYPE. */
  120. static void
  121. ada_addrof (struct type *type = nullptr)
  122. {
  123. operation_up arg = ada_pop (false);
  124. operation_up addr = make_operation<unop_addr_operation> (std::move (arg));
  125. operation_up wrapped
  126. = make_operation<ada_wrapped_operation> (std::move (addr));
  127. if (type != nullptr)
  128. wrapped = make_operation<unop_cast_operation> (std::move (wrapped), type);
  129. pstate->push (std::move (wrapped));
  130. }
  131. /* Handle operator overloading. Either returns a function all
  132. operation wrapping the arguments, or it returns null, leaving the
  133. caller to construct the appropriate operation. If RHS is null, a
  134. unary operator is assumed. */
  135. static operation_up
  136. maybe_overload (enum exp_opcode op, operation_up &lhs, operation_up &rhs)
  137. {
  138. struct value *args[2];
  139. int nargs = 1;
  140. args[0] = lhs->evaluate (nullptr, pstate->expout.get (),
  141. EVAL_AVOID_SIDE_EFFECTS);
  142. if (rhs == nullptr)
  143. args[1] = nullptr;
  144. else
  145. {
  146. args[1] = rhs->evaluate (nullptr, pstate->expout.get (),
  147. EVAL_AVOID_SIDE_EFFECTS);
  148. ++nargs;
  149. }
  150. block_symbol fn = ada_find_operator_symbol (op, pstate->parse_completion,
  151. nargs, args);
  152. if (fn.symbol == nullptr)
  153. return {};
  154. if (symbol_read_needs_frame (fn.symbol))
  155. pstate->block_tracker->update (fn.block, INNERMOST_BLOCK_FOR_SYMBOLS);
  156. operation_up callee = make_operation<ada_var_value_operation> (fn);
  157. std::vector<operation_up> argvec;
  158. argvec.push_back (std::move (lhs));
  159. if (rhs != nullptr)
  160. argvec.push_back (std::move (rhs));
  161. return make_operation<ada_funcall_operation> (std::move (callee),
  162. std::move (argvec));
  163. }
  164. /* Like parser_state::wrap, but use ada_pop to pop the value, and
  165. handle unary overloading. */
  166. template<typename T>
  167. void
  168. ada_wrap_overload (enum exp_opcode op)
  169. {
  170. operation_up arg = ada_pop ();
  171. operation_up empty;
  172. operation_up call = maybe_overload (op, arg, empty);
  173. if (call == nullptr)
  174. call = make_operation<T> (std::move (arg));
  175. pstate->push (std::move (call));
  176. }
  177. /* A variant of parser_state::wrap2 that uses ada_pop to pop both
  178. operands, and then pushes a new Ada-wrapped operation of the
  179. template type T. */
  180. template<typename T>
  181. void
  182. ada_un_wrap2 (enum exp_opcode op)
  183. {
  184. operation_up rhs = ada_pop ();
  185. operation_up lhs = ada_pop ();
  186. operation_up wrapped = maybe_overload (op, lhs, rhs);
  187. if (wrapped == nullptr)
  188. {
  189. wrapped = make_operation<T> (std::move (lhs), std::move (rhs));
  190. wrapped = make_operation<ada_wrapped_operation> (std::move (wrapped));
  191. }
  192. pstate->push (std::move (wrapped));
  193. }
  194. /* A variant of parser_state::wrap2 that uses ada_pop to pop both
  195. operands. Unlike ada_un_wrap2, ada_wrapped_operation is not
  196. used. */
  197. template<typename T>
  198. void
  199. ada_wrap2 (enum exp_opcode op)
  200. {
  201. operation_up rhs = ada_pop ();
  202. operation_up lhs = ada_pop ();
  203. operation_up call = maybe_overload (op, lhs, rhs);
  204. if (call == nullptr)
  205. call = make_operation<T> (std::move (lhs), std::move (rhs));
  206. pstate->push (std::move (call));
  207. }
  208. /* A variant of parser_state::wrap2 that uses ada_pop to pop both
  209. operands. OP is also passed to the constructor of the new binary
  210. operation. */
  211. template<typename T>
  212. void
  213. ada_wrap_op (enum exp_opcode op)
  214. {
  215. operation_up rhs = ada_pop ();
  216. operation_up lhs = ada_pop ();
  217. operation_up call = maybe_overload (op, lhs, rhs);
  218. if (call == nullptr)
  219. call = make_operation<T> (op, std::move (lhs), std::move (rhs));
  220. pstate->push (std::move (call));
  221. }
  222. /* Pop three operands using ada_pop, then construct a new ternary
  223. operation of type T and push it. */
  224. template<typename T>
  225. void
  226. ada_wrap3 ()
  227. {
  228. operation_up rhs = ada_pop ();
  229. operation_up mid = ada_pop ();
  230. operation_up lhs = ada_pop ();
  231. pstate->push_new<T> (std::move (lhs), std::move (mid), std::move (rhs));
  232. }
  233. /* Pop NARGS operands, then a callee operand, and use these to
  234. construct and push a new Ada function call operation. */
  235. static void
  236. ada_funcall (int nargs)
  237. {
  238. /* We use the ordinary pop here, because we're going to do
  239. resolution in a separate step, in order to handle array
  240. indices. */
  241. std::vector<operation_up> args = pstate->pop_vector (nargs);
  242. /* Call parser_state::pop here, because we don't want to
  243. function-convert the callee slot of a call we're already
  244. constructing. */
  245. operation_up callee = pstate->pop ();
  246. ada_var_value_operation *vvo
  247. = dynamic_cast<ada_var_value_operation *> (callee.get ());
  248. int array_arity = 0;
  249. struct type *callee_t = nullptr;
  250. if (vvo == nullptr
  251. || vvo->get_symbol ()->domain () != UNDEF_DOMAIN)
  252. {
  253. struct value *callee_v = callee->evaluate (nullptr,
  254. pstate->expout.get (),
  255. EVAL_AVOID_SIDE_EFFECTS);
  256. callee_t = ada_check_typedef (value_type (callee_v));
  257. array_arity = ada_array_arity (callee_t);
  258. }
  259. for (int i = 0; i < nargs; ++i)
  260. {
  261. struct type *subtype = nullptr;
  262. if (i < array_arity)
  263. subtype = ada_index_type (callee_t, i + 1, "array type");
  264. args[i] = resolve (std::move (args[i]), true, subtype);
  265. }
  266. std::unique_ptr<ada_funcall_operation> funcall
  267. (new ada_funcall_operation (std::move (callee), std::move (args)));
  268. funcall->resolve (pstate->expout.get (), true, pstate->parse_completion,
  269. pstate->block_tracker, nullptr);
  270. pstate->push (std::move (funcall));
  271. }
  272. /* The components being constructed during this parse. */
  273. static std::vector<ada_component_up> components;
  274. /* Create a new ada_component_up of the indicated type and arguments,
  275. and push it on the global 'components' vector. */
  276. template<typename T, typename... Arg>
  277. void
  278. push_component (Arg... args)
  279. {
  280. components.emplace_back (new T (std::forward<Arg> (args)...));
  281. }
  282. /* Examine the final element of the 'components' vector, and return it
  283. as a pointer to an ada_choices_component. The caller is
  284. responsible for ensuring that the final element is in fact an
  285. ada_choices_component. */
  286. static ada_choices_component *
  287. choice_component ()
  288. {
  289. ada_component *last = components.back ().get ();
  290. ada_choices_component *result = dynamic_cast<ada_choices_component *> (last);
  291. gdb_assert (result != nullptr);
  292. return result;
  293. }
  294. /* Pop the most recent component from the global stack, and return
  295. it. */
  296. static ada_component_up
  297. pop_component ()
  298. {
  299. ada_component_up result = std::move (components.back ());
  300. components.pop_back ();
  301. return result;
  302. }
  303. /* Pop the N most recent components from the global stack, and return
  304. them in a vector. */
  305. static std::vector<ada_component_up>
  306. pop_components (int n)
  307. {
  308. std::vector<ada_component_up> result (n);
  309. for (int i = 1; i <= n; ++i)
  310. result[n - i] = pop_component ();
  311. return result;
  312. }
  313. /* The associations being constructed during this parse. */
  314. static std::vector<ada_association_up> associations;
  315. /* Create a new ada_association_up of the indicated type and
  316. arguments, and push it on the global 'associations' vector. */
  317. template<typename T, typename... Arg>
  318. void
  319. push_association (Arg... args)
  320. {
  321. associations.emplace_back (new T (std::forward<Arg> (args)...));
  322. }
  323. /* Pop the most recent association from the global stack, and return
  324. it. */
  325. static ada_association_up
  326. pop_association ()
  327. {
  328. ada_association_up result = std::move (associations.back ());
  329. associations.pop_back ();
  330. return result;
  331. }
  332. /* Pop the N most recent associations from the global stack, and
  333. return them in a vector. */
  334. static std::vector<ada_association_up>
  335. pop_associations (int n)
  336. {
  337. std::vector<ada_association_up> result (n);
  338. for (int i = 1; i <= n; ++i)
  339. result[n - i] = pop_association ();
  340. return result;
  341. }
  342. /* Expression completer for attributes. */
  343. struct ada_tick_completer : public expr_completion_base
  344. {
  345. explicit ada_tick_completer (std::string &&name)
  346. : m_name (std::move (name))
  347. {
  348. }
  349. bool complete (struct expression *exp,
  350. completion_tracker &tracker) override;
  351. private:
  352. std::string m_name;
  353. };
  354. /* Make a new ada_tick_completer and wrap it in a unique pointer. */
  355. static std::unique_ptr<expr_completion_base>
  356. make_tick_completer (struct stoken tok)
  357. {
  358. return (std::unique_ptr<expr_completion_base>
  359. (new ada_tick_completer (std::string (tok.ptr, tok.length))));
  360. }
  361. %}
  362. %union
  363. {
  364. LONGEST lval;
  365. struct {
  366. LONGEST val;
  367. struct type *type;
  368. } typed_val;
  369. struct {
  370. gdb_byte val[16];
  371. struct type *type;
  372. } typed_val_float;
  373. struct type *tval;
  374. struct stoken sval;
  375. const struct block *bval;
  376. struct internalvar *ivar;
  377. }
  378. %type <lval> positional_list component_groups component_associations
  379. %type <lval> aggregate_component_list
  380. %type <tval> var_or_type type_prefix opt_type_prefix
  381. %token <typed_val> INT NULL_PTR CHARLIT
  382. %token <typed_val_float> FLOAT
  383. %token TRUEKEYWORD FALSEKEYWORD
  384. %token COLONCOLON
  385. %token <sval> STRING NAME DOT_ID TICK_COMPLETE DOT_COMPLETE NAME_COMPLETE
  386. %type <bval> block
  387. %type <lval> arglist tick_arglist
  388. /* Special type cases, put in to allow the parser to distinguish different
  389. legal basetypes. */
  390. %token <sval> DOLLAR_VARIABLE
  391. %nonassoc ASSIGN
  392. %left _AND_ OR XOR THEN ELSE
  393. %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
  394. %left '@'
  395. %left '+' '-' '&'
  396. %left UNARY
  397. %left '*' '/' MOD REM
  398. %right STARSTAR ABS NOT
  399. /* Artificial token to give NAME => ... and NAME | priority over reducing
  400. NAME to <primary> and to give <primary>' priority over reducing <primary>
  401. to <simple_exp>. */
  402. %nonassoc VAR
  403. %nonassoc ARROW '|'
  404. %right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
  405. %right TICK_MAX TICK_MIN TICK_MODULUS
  406. %right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
  407. %right TICK_COMPLETE
  408. /* The following are right-associative only so that reductions at this
  409. precedence have lower precedence than '.' and '('. The syntax still
  410. forces a.b.c, e.g., to be LEFT-associated. */
  411. %right '.' '(' '[' DOT_ID DOT_COMPLETE
  412. %token NEW OTHERS
  413. %%
  414. start : exp1
  415. ;
  416. /* Expressions, including the sequencing operator. */
  417. exp1 : exp
  418. | exp1 ';' exp
  419. { ada_wrap2<comma_operation> (BINOP_COMMA); }
  420. | primary ASSIGN exp /* Extension for convenience */
  421. {
  422. operation_up rhs = pstate->pop ();
  423. operation_up lhs = ada_pop ();
  424. value *lhs_val
  425. = lhs->evaluate (nullptr, pstate->expout.get (),
  426. EVAL_AVOID_SIDE_EFFECTS);
  427. rhs = resolve (std::move (rhs), true,
  428. value_type (lhs_val));
  429. pstate->push_new<ada_assign_operation>
  430. (std::move (lhs), std::move (rhs));
  431. }
  432. ;
  433. /* Expressions, not including the sequencing operator. */
  434. primary : primary DOT_ID
  435. {
  436. if (strcmp ($2.ptr, "all") == 0)
  437. ada_wrap<ada_unop_ind_operation> ();
  438. else
  439. {
  440. operation_up arg = ada_pop ();
  441. pstate->push_new<ada_structop_operation>
  442. (std::move (arg), copy_name ($2));
  443. }
  444. }
  445. ;
  446. primary : primary DOT_COMPLETE
  447. {
  448. /* This is done even for ".all", because
  449. that might be a prefix. */
  450. operation_up arg = ada_pop ();
  451. ada_structop_operation *str_op
  452. = (new ada_structop_operation
  453. (std::move (arg), copy_name ($2)));
  454. str_op->set_prefix (find_completion_bounds (pstate));
  455. pstate->push (operation_up (str_op));
  456. pstate->mark_struct_expression (str_op);
  457. }
  458. ;
  459. primary : primary '(' arglist ')'
  460. { ada_funcall ($3); }
  461. | var_or_type '(' arglist ')'
  462. {
  463. if ($1 != NULL)
  464. {
  465. if ($3 != 1)
  466. error (_("Invalid conversion"));
  467. operation_up arg = ada_pop ();
  468. pstate->push_new<unop_cast_operation>
  469. (std::move (arg), $1);
  470. }
  471. else
  472. ada_funcall ($3);
  473. }
  474. ;
  475. primary : var_or_type '\'' '(' exp ')'
  476. {
  477. if ($1 == NULL)
  478. error (_("Type required for qualification"));
  479. operation_up arg = ada_pop (true,
  480. check_typedef ($1));
  481. pstate->push_new<ada_qual_operation>
  482. (std::move (arg), $1);
  483. }
  484. ;
  485. primary :
  486. primary '(' simple_exp DOTDOT simple_exp ')'
  487. { ada_wrap3<ada_ternop_slice_operation> (); }
  488. | var_or_type '(' simple_exp DOTDOT simple_exp ')'
  489. { if ($1 == NULL)
  490. ada_wrap3<ada_ternop_slice_operation> ();
  491. else
  492. error (_("Cannot slice a type"));
  493. }
  494. ;
  495. primary : '(' exp1 ')' { }
  496. ;
  497. /* The following rule causes a conflict with the type conversion
  498. var_or_type (exp)
  499. To get around it, we give '(' higher priority and add bridge rules for
  500. var_or_type (exp, exp, ...)
  501. var_or_type (exp .. exp)
  502. We also have the action for var_or_type(exp) generate a function call
  503. when the first symbol does not denote a type. */
  504. primary : var_or_type %prec VAR
  505. { if ($1 != NULL)
  506. pstate->push_new<type_operation> ($1);
  507. }
  508. ;
  509. primary : DOLLAR_VARIABLE /* Various GDB extensions */
  510. { pstate->push_dollar ($1); }
  511. ;
  512. primary : aggregate
  513. {
  514. pstate->push_new<ada_aggregate_operation>
  515. (pop_component ());
  516. }
  517. ;
  518. simple_exp : primary
  519. ;
  520. simple_exp : '-' simple_exp %prec UNARY
  521. { ada_wrap_overload<ada_neg_operation> (UNOP_NEG); }
  522. ;
  523. simple_exp : '+' simple_exp %prec UNARY
  524. {
  525. operation_up arg = ada_pop ();
  526. operation_up empty;
  527. /* If an overloaded operator was found, use
  528. it. Otherwise, unary + has no effect and
  529. the argument can be pushed instead. */
  530. operation_up call = maybe_overload (UNOP_PLUS, arg,
  531. empty);
  532. if (call != nullptr)
  533. arg = std::move (call);
  534. pstate->push (std::move (arg));
  535. }
  536. ;
  537. simple_exp : NOT simple_exp %prec UNARY
  538. {
  539. ada_wrap_overload<unary_logical_not_operation>
  540. (UNOP_LOGICAL_NOT);
  541. }
  542. ;
  543. simple_exp : ABS simple_exp %prec UNARY
  544. { ada_wrap_overload<ada_abs_operation> (UNOP_ABS); }
  545. ;
  546. arglist : { $$ = 0; }
  547. ;
  548. arglist : exp
  549. { $$ = 1; }
  550. | NAME ARROW exp
  551. { $$ = 1; }
  552. | arglist ',' exp
  553. { $$ = $1 + 1; }
  554. | arglist ',' NAME ARROW exp
  555. { $$ = $1 + 1; }
  556. ;
  557. primary : '{' var_or_type '}' primary %prec '.'
  558. /* GDB extension */
  559. {
  560. if ($2 == NULL)
  561. error (_("Type required within braces in coercion"));
  562. operation_up arg = ada_pop ();
  563. pstate->push_new<unop_memval_operation>
  564. (std::move (arg), $2);
  565. }
  566. ;
  567. /* Binary operators in order of decreasing precedence. */
  568. simple_exp : simple_exp STARSTAR simple_exp
  569. { ada_wrap2<ada_binop_exp_operation> (BINOP_EXP); }
  570. ;
  571. simple_exp : simple_exp '*' simple_exp
  572. { ada_wrap2<ada_binop_mul_operation> (BINOP_MUL); }
  573. ;
  574. simple_exp : simple_exp '/' simple_exp
  575. { ada_wrap2<ada_binop_div_operation> (BINOP_DIV); }
  576. ;
  577. simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
  578. { ada_wrap2<ada_binop_rem_operation> (BINOP_REM); }
  579. ;
  580. simple_exp : simple_exp MOD simple_exp
  581. { ada_wrap2<ada_binop_mod_operation> (BINOP_MOD); }
  582. ;
  583. simple_exp : simple_exp '@' simple_exp /* GDB extension */
  584. { ada_wrap2<repeat_operation> (BINOP_REPEAT); }
  585. ;
  586. simple_exp : simple_exp '+' simple_exp
  587. { ada_wrap_op<ada_binop_addsub_operation> (BINOP_ADD); }
  588. ;
  589. simple_exp : simple_exp '&' simple_exp
  590. { ada_wrap2<ada_concat_operation> (BINOP_CONCAT); }
  591. ;
  592. simple_exp : simple_exp '-' simple_exp
  593. { ada_wrap_op<ada_binop_addsub_operation> (BINOP_SUB); }
  594. ;
  595. relation : simple_exp
  596. ;
  597. relation : simple_exp '=' simple_exp
  598. { ada_wrap_op<ada_binop_equal_operation> (BINOP_EQUAL); }
  599. ;
  600. relation : simple_exp NOTEQUAL simple_exp
  601. { ada_wrap_op<ada_binop_equal_operation> (BINOP_NOTEQUAL); }
  602. ;
  603. relation : simple_exp LEQ simple_exp
  604. { ada_un_wrap2<leq_operation> (BINOP_LEQ); }
  605. ;
  606. relation : simple_exp IN simple_exp DOTDOT simple_exp
  607. { ada_wrap3<ada_ternop_range_operation> (); }
  608. | simple_exp IN primary TICK_RANGE tick_arglist
  609. {
  610. operation_up rhs = ada_pop ();
  611. operation_up lhs = ada_pop ();
  612. pstate->push_new<ada_binop_in_bounds_operation>
  613. (std::move (lhs), std::move (rhs), $5);
  614. }
  615. | simple_exp IN var_or_type %prec TICK_ACCESS
  616. {
  617. if ($3 == NULL)
  618. error (_("Right operand of 'in' must be type"));
  619. operation_up arg = ada_pop ();
  620. pstate->push_new<ada_unop_range_operation>
  621. (std::move (arg), $3);
  622. }
  623. | simple_exp NOT IN simple_exp DOTDOT simple_exp
  624. { ada_wrap3<ada_ternop_range_operation> ();
  625. ada_wrap<unary_logical_not_operation> (); }
  626. | simple_exp NOT IN primary TICK_RANGE tick_arglist
  627. {
  628. operation_up rhs = ada_pop ();
  629. operation_up lhs = ada_pop ();
  630. pstate->push_new<ada_binop_in_bounds_operation>
  631. (std::move (lhs), std::move (rhs), $6);
  632. ada_wrap<unary_logical_not_operation> ();
  633. }
  634. | simple_exp NOT IN var_or_type %prec TICK_ACCESS
  635. {
  636. if ($4 == NULL)
  637. error (_("Right operand of 'in' must be type"));
  638. operation_up arg = ada_pop ();
  639. pstate->push_new<ada_unop_range_operation>
  640. (std::move (arg), $4);
  641. ada_wrap<unary_logical_not_operation> ();
  642. }
  643. ;
  644. relation : simple_exp GEQ simple_exp
  645. { ada_un_wrap2<geq_operation> (BINOP_GEQ); }
  646. ;
  647. relation : simple_exp '<' simple_exp
  648. { ada_un_wrap2<less_operation> (BINOP_LESS); }
  649. ;
  650. relation : simple_exp '>' simple_exp
  651. { ada_un_wrap2<gtr_operation> (BINOP_GTR); }
  652. ;
  653. exp : relation
  654. | and_exp
  655. | and_then_exp
  656. | or_exp
  657. | or_else_exp
  658. | xor_exp
  659. ;
  660. and_exp :
  661. relation _AND_ relation
  662. { ada_wrap2<ada_bitwise_and_operation>
  663. (BINOP_BITWISE_AND); }
  664. | and_exp _AND_ relation
  665. { ada_wrap2<ada_bitwise_and_operation>
  666. (BINOP_BITWISE_AND); }
  667. ;
  668. and_then_exp :
  669. relation _AND_ THEN relation
  670. { ada_wrap2<logical_and_operation>
  671. (BINOP_LOGICAL_AND); }
  672. | and_then_exp _AND_ THEN relation
  673. { ada_wrap2<logical_and_operation>
  674. (BINOP_LOGICAL_AND); }
  675. ;
  676. or_exp :
  677. relation OR relation
  678. { ada_wrap2<ada_bitwise_ior_operation>
  679. (BINOP_BITWISE_IOR); }
  680. | or_exp OR relation
  681. { ada_wrap2<ada_bitwise_ior_operation>
  682. (BINOP_BITWISE_IOR); }
  683. ;
  684. or_else_exp :
  685. relation OR ELSE relation
  686. { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
  687. | or_else_exp OR ELSE relation
  688. { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
  689. ;
  690. xor_exp : relation XOR relation
  691. { ada_wrap2<ada_bitwise_xor_operation>
  692. (BINOP_BITWISE_XOR); }
  693. | xor_exp XOR relation
  694. { ada_wrap2<ada_bitwise_xor_operation>
  695. (BINOP_BITWISE_XOR); }
  696. ;
  697. /* Primaries can denote types (OP_TYPE). In cases such as
  698. primary TICK_ADDRESS, where a type would be invalid, it will be
  699. caught when evaluate_subexp in ada-lang.c tries to evaluate the
  700. primary, expecting a value. Precedence rules resolve the ambiguity
  701. in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A
  702. construct such as aType'access'access will again cause an error when
  703. aType'access evaluates to a type that evaluate_subexp attempts to
  704. evaluate. */
  705. primary : primary TICK_ACCESS
  706. { ada_addrof (); }
  707. | primary TICK_ADDRESS
  708. { ada_addrof (type_system_address (pstate)); }
  709. | primary TICK_COMPLETE
  710. {
  711. pstate->mark_completion (make_tick_completer ($2));
  712. }
  713. | primary TICK_FIRST tick_arglist
  714. {
  715. operation_up arg = ada_pop ();
  716. pstate->push_new<ada_unop_atr_operation>
  717. (std::move (arg), OP_ATR_FIRST, $3);
  718. }
  719. | primary TICK_LAST tick_arglist
  720. {
  721. operation_up arg = ada_pop ();
  722. pstate->push_new<ada_unop_atr_operation>
  723. (std::move (arg), OP_ATR_LAST, $3);
  724. }
  725. | primary TICK_LENGTH tick_arglist
  726. {
  727. operation_up arg = ada_pop ();
  728. pstate->push_new<ada_unop_atr_operation>
  729. (std::move (arg), OP_ATR_LENGTH, $3);
  730. }
  731. | primary TICK_SIZE
  732. { ada_wrap<ada_atr_size_operation> (); }
  733. | primary TICK_TAG
  734. { ada_wrap<ada_atr_tag_operation> (); }
  735. | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
  736. { ada_wrap2<ada_binop_min_operation> (BINOP_MIN); }
  737. | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
  738. { ada_wrap2<ada_binop_max_operation> (BINOP_MAX); }
  739. | opt_type_prefix TICK_POS '(' exp ')'
  740. { ada_wrap<ada_pos_operation> (); }
  741. | type_prefix TICK_VAL '(' exp ')'
  742. {
  743. operation_up arg = ada_pop ();
  744. pstate->push_new<ada_atr_val_operation>
  745. ($1, std::move (arg));
  746. }
  747. | type_prefix TICK_MODULUS
  748. {
  749. struct type *type_arg = check_typedef ($1);
  750. if (!ada_is_modular_type (type_arg))
  751. error (_("'modulus must be applied to modular type"));
  752. write_int (pstate, ada_modulus (type_arg),
  753. TYPE_TARGET_TYPE (type_arg));
  754. }
  755. ;
  756. tick_arglist : %prec '('
  757. { $$ = 1; }
  758. | '(' INT ')'
  759. { $$ = $2.val; }
  760. ;
  761. type_prefix :
  762. var_or_type
  763. {
  764. if ($1 == NULL)
  765. error (_("Prefix must be type"));
  766. $$ = $1;
  767. }
  768. ;
  769. opt_type_prefix :
  770. type_prefix
  771. { $$ = $1; }
  772. | /* EMPTY */
  773. { $$ = parse_type (pstate)->builtin_void; }
  774. ;
  775. primary : INT
  776. { write_int (pstate, (LONGEST) $1.val, $1.type); }
  777. ;
  778. primary : CHARLIT
  779. {
  780. pstate->push_new<ada_char_operation> ($1.type, $1.val);
  781. }
  782. ;
  783. primary : FLOAT
  784. {
  785. float_data data;
  786. std::copy (std::begin ($1.val), std::end ($1.val),
  787. std::begin (data));
  788. pstate->push_new<float_const_operation>
  789. ($1.type, data);
  790. ada_wrap<ada_wrapped_operation> ();
  791. }
  792. ;
  793. primary : NULL_PTR
  794. {
  795. struct type *null_ptr_type
  796. = lookup_pointer_type (parse_type (pstate)->builtin_int0);
  797. write_int (pstate, 0, null_ptr_type);
  798. }
  799. ;
  800. primary : STRING
  801. {
  802. pstate->push_new<ada_string_operation>
  803. (copy_name ($1));
  804. }
  805. ;
  806. primary : TRUEKEYWORD
  807. { write_int (pstate, 1, type_boolean (pstate)); }
  808. | FALSEKEYWORD
  809. { write_int (pstate, 0, type_boolean (pstate)); }
  810. ;
  811. primary : NEW NAME
  812. { error (_("NEW not implemented.")); }
  813. ;
  814. var_or_type: NAME %prec VAR
  815. { $$ = write_var_or_type (pstate, NULL, $1); }
  816. | NAME_COMPLETE %prec VAR
  817. {
  818. $$ = write_var_or_type_completion (pstate,
  819. NULL,
  820. $1);
  821. }
  822. | block NAME %prec VAR
  823. { $$ = write_var_or_type (pstate, $1, $2); }
  824. | block NAME_COMPLETE %prec VAR
  825. {
  826. $$ = write_var_or_type_completion (pstate,
  827. $1,
  828. $2);
  829. }
  830. | NAME TICK_ACCESS
  831. {
  832. $$ = write_var_or_type (pstate, NULL, $1);
  833. if ($$ == NULL)
  834. ada_addrof ();
  835. else
  836. $$ = lookup_pointer_type ($$);
  837. }
  838. | block NAME TICK_ACCESS
  839. {
  840. $$ = write_var_or_type (pstate, $1, $2);
  841. if ($$ == NULL)
  842. ada_addrof ();
  843. else
  844. $$ = lookup_pointer_type ($$);
  845. }
  846. ;
  847. /* GDB extension */
  848. block : NAME COLONCOLON
  849. { $$ = block_lookup (NULL, $1.ptr); }
  850. | block NAME COLONCOLON
  851. { $$ = block_lookup ($1, $2.ptr); }
  852. ;
  853. aggregate :
  854. '(' aggregate_component_list ')'
  855. {
  856. std::vector<ada_component_up> components
  857. = pop_components ($2);
  858. push_component<ada_aggregate_component>
  859. (std::move (components));
  860. }
  861. ;
  862. aggregate_component_list :
  863. component_groups { $$ = $1; }
  864. | positional_list exp
  865. {
  866. push_component<ada_positional_component>
  867. ($1, ada_pop ());
  868. $$ = $1 + 1;
  869. }
  870. | positional_list component_groups
  871. { $$ = $1 + $2; }
  872. ;
  873. positional_list :
  874. exp ','
  875. {
  876. push_component<ada_positional_component>
  877. (0, ada_pop ());
  878. $$ = 1;
  879. }
  880. | positional_list exp ','
  881. {
  882. push_component<ada_positional_component>
  883. ($1, ada_pop ());
  884. $$ = $1 + 1;
  885. }
  886. ;
  887. component_groups:
  888. others { $$ = 1; }
  889. | component_group { $$ = 1; }
  890. | component_group ',' component_groups
  891. { $$ = $3 + 1; }
  892. ;
  893. others : OTHERS ARROW exp
  894. {
  895. push_component<ada_others_component> (ada_pop ());
  896. }
  897. ;
  898. component_group :
  899. component_associations
  900. {
  901. ada_choices_component *choices = choice_component ();
  902. choices->set_associations (pop_associations ($1));
  903. }
  904. ;
  905. /* We use this somewhat obscure definition in order to handle NAME => and
  906. NAME | differently from exp => and exp |. ARROW and '|' have a precedence
  907. above that of the reduction of NAME to var_or_type. By delaying
  908. decisions until after the => or '|', we convert the ambiguity to a
  909. resolved shift/reduce conflict. */
  910. component_associations :
  911. NAME ARROW exp
  912. {
  913. push_component<ada_choices_component> (ada_pop ());
  914. write_name_assoc (pstate, $1);
  915. $$ = 1;
  916. }
  917. | simple_exp ARROW exp
  918. {
  919. push_component<ada_choices_component> (ada_pop ());
  920. push_association<ada_name_association> (ada_pop ());
  921. $$ = 1;
  922. }
  923. | simple_exp DOTDOT simple_exp ARROW exp
  924. {
  925. push_component<ada_choices_component> (ada_pop ());
  926. operation_up rhs = ada_pop ();
  927. operation_up lhs = ada_pop ();
  928. push_association<ada_discrete_range_association>
  929. (std::move (lhs), std::move (rhs));
  930. $$ = 1;
  931. }
  932. | NAME '|' component_associations
  933. {
  934. write_name_assoc (pstate, $1);
  935. $$ = $3 + 1;
  936. }
  937. | simple_exp '|' component_associations
  938. {
  939. push_association<ada_name_association> (ada_pop ());
  940. $$ = $3 + 1;
  941. }
  942. | simple_exp DOTDOT simple_exp '|' component_associations
  943. {
  944. operation_up rhs = ada_pop ();
  945. operation_up lhs = ada_pop ();
  946. push_association<ada_discrete_range_association>
  947. (std::move (lhs), std::move (rhs));
  948. $$ = $5 + 1;
  949. }
  950. ;
  951. /* Some extensions borrowed from C, for the benefit of those who find they
  952. can't get used to Ada notation in GDB. */
  953. primary : '*' primary %prec '.'
  954. { ada_wrap<ada_unop_ind_operation> (); }
  955. | '&' primary %prec '.'
  956. { ada_addrof (); }
  957. | primary '[' exp ']'
  958. {
  959. ada_wrap2<subscript_operation> (BINOP_SUBSCRIPT);
  960. ada_wrap<ada_wrapped_operation> ();
  961. }
  962. ;
  963. %%
  964. /* yylex defined in ada-lex.c: Reads one token, getting characters */
  965. /* through lexptr. */
  966. /* Remap normal flex interface names (yylex) as well as gratuitiously */
  967. /* global symbol names, so we can have multiple flex-generated parsers */
  968. /* in gdb. */
  969. /* (See note above on previous definitions for YACC.) */
  970. #define yy_create_buffer ada_yy_create_buffer
  971. #define yy_delete_buffer ada_yy_delete_buffer
  972. #define yy_init_buffer ada_yy_init_buffer
  973. #define yy_load_buffer_state ada_yy_load_buffer_state
  974. #define yy_switch_to_buffer ada_yy_switch_to_buffer
  975. #define yyrestart ada_yyrestart
  976. #define yytext ada_yytext
  977. static struct obstack temp_parse_space;
  978. /* The following kludge was found necessary to prevent conflicts between */
  979. /* defs.h and non-standard stdlib.h files. */
  980. #define qsort __qsort__dummy
  981. #include "ada-lex.c"
  982. int
  983. ada_parse (struct parser_state *par_state)
  984. {
  985. /* Setting up the parser state. */
  986. scoped_restore pstate_restore = make_scoped_restore (&pstate);
  987. gdb_assert (par_state != NULL);
  988. pstate = par_state;
  989. original_expr = par_state->lexptr;
  990. scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
  991. parser_debug);
  992. lexer_init (yyin); /* (Re-)initialize lexer. */
  993. obstack_free (&temp_parse_space, NULL);
  994. obstack_init (&temp_parse_space);
  995. components.clear ();
  996. associations.clear ();
  997. int result = yyparse ();
  998. if (!result)
  999. {
  1000. struct type *context_type = nullptr;
  1001. if (par_state->void_context_p)
  1002. context_type = parse_type (par_state)->builtin_void;
  1003. pstate->set_operation (ada_pop (true, context_type));
  1004. }
  1005. return result;
  1006. }
  1007. static void
  1008. yyerror (const char *msg)
  1009. {
  1010. error (_("Error in expression, near `%s'."), pstate->lexptr);
  1011. }
  1012. /* Emit expression to access an instance of SYM, in block BLOCK (if
  1013. non-NULL). */
  1014. static void
  1015. write_var_from_sym (struct parser_state *par_state, block_symbol sym)
  1016. {
  1017. if (symbol_read_needs_frame (sym.symbol))
  1018. par_state->block_tracker->update (sym.block, INNERMOST_BLOCK_FOR_SYMBOLS);
  1019. par_state->push_new<ada_var_value_operation> (sym);
  1020. }
  1021. /* Write integer or boolean constant ARG of type TYPE. */
  1022. static void
  1023. write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
  1024. {
  1025. pstate->push_new<long_const_operation> (type, arg);
  1026. ada_wrap<ada_wrapped_operation> ();
  1027. }
  1028. /* Emit expression corresponding to the renamed object named
  1029. designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
  1030. context of ORIG_LEFT_CONTEXT, to which is applied the operations
  1031. encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of
  1032. cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it
  1033. defaults to the currently selected block. ORIG_SYMBOL is the
  1034. symbol that originally encoded the renaming. It is needed only
  1035. because its prefix also qualifies any index variables used to index
  1036. or slice an array. It should not be necessary once we go to the
  1037. new encoding entirely (FIXME pnh 7/20/2007). */
  1038. static void
  1039. write_object_renaming (struct parser_state *par_state,
  1040. const struct block *orig_left_context,
  1041. const char *renamed_entity, int renamed_entity_len,
  1042. const char *renaming_expr, int max_depth)
  1043. {
  1044. char *name;
  1045. enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
  1046. struct block_symbol sym_info;
  1047. if (max_depth <= 0)
  1048. error (_("Could not find renamed symbol"));
  1049. if (orig_left_context == NULL)
  1050. orig_left_context = get_selected_block (NULL);
  1051. name = obstack_strndup (&temp_parse_space, renamed_entity,
  1052. renamed_entity_len);
  1053. ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
  1054. if (sym_info.symbol == NULL)
  1055. error (_("Could not find renamed variable: %s"), ada_decode (name).c_str ());
  1056. else if (sym_info.symbol->aclass () == LOC_TYPEDEF)
  1057. /* We have a renaming of an old-style renaming symbol. Don't
  1058. trust the block information. */
  1059. sym_info.block = orig_left_context;
  1060. {
  1061. const char *inner_renamed_entity;
  1062. int inner_renamed_entity_len;
  1063. const char *inner_renaming_expr;
  1064. switch (ada_parse_renaming (sym_info.symbol, &inner_renamed_entity,
  1065. &inner_renamed_entity_len,
  1066. &inner_renaming_expr))
  1067. {
  1068. case ADA_NOT_RENAMING:
  1069. write_var_from_sym (par_state, sym_info);
  1070. break;
  1071. case ADA_OBJECT_RENAMING:
  1072. write_object_renaming (par_state, sym_info.block,
  1073. inner_renamed_entity, inner_renamed_entity_len,
  1074. inner_renaming_expr, max_depth - 1);
  1075. break;
  1076. default:
  1077. goto BadEncoding;
  1078. }
  1079. }
  1080. slice_state = SIMPLE_INDEX;
  1081. while (*renaming_expr == 'X')
  1082. {
  1083. renaming_expr += 1;
  1084. switch (*renaming_expr) {
  1085. case 'A':
  1086. renaming_expr += 1;
  1087. ada_wrap<ada_unop_ind_operation> ();
  1088. break;
  1089. case 'L':
  1090. slice_state = LOWER_BOUND;
  1091. /* FALLTHROUGH */
  1092. case 'S':
  1093. renaming_expr += 1;
  1094. if (isdigit (*renaming_expr))
  1095. {
  1096. char *next;
  1097. long val = strtol (renaming_expr, &next, 10);
  1098. if (next == renaming_expr)
  1099. goto BadEncoding;
  1100. renaming_expr = next;
  1101. write_int (par_state, val, type_int (par_state));
  1102. }
  1103. else
  1104. {
  1105. const char *end;
  1106. char *index_name;
  1107. struct block_symbol index_sym_info;
  1108. end = strchr (renaming_expr, 'X');
  1109. if (end == NULL)
  1110. end = renaming_expr + strlen (renaming_expr);
  1111. index_name = obstack_strndup (&temp_parse_space, renaming_expr,
  1112. end - renaming_expr);
  1113. renaming_expr = end;
  1114. ada_lookup_encoded_symbol (index_name, orig_left_context,
  1115. VAR_DOMAIN, &index_sym_info);
  1116. if (index_sym_info.symbol == NULL)
  1117. error (_("Could not find %s"), index_name);
  1118. else if (index_sym_info.symbol->aclass () == LOC_TYPEDEF)
  1119. /* Index is an old-style renaming symbol. */
  1120. index_sym_info.block = orig_left_context;
  1121. write_var_from_sym (par_state, index_sym_info);
  1122. }
  1123. if (slice_state == SIMPLE_INDEX)
  1124. ada_funcall (1);
  1125. else if (slice_state == LOWER_BOUND)
  1126. slice_state = UPPER_BOUND;
  1127. else if (slice_state == UPPER_BOUND)
  1128. {
  1129. ada_wrap3<ada_ternop_slice_operation> ();
  1130. slice_state = SIMPLE_INDEX;
  1131. }
  1132. break;
  1133. case 'R':
  1134. {
  1135. const char *end;
  1136. renaming_expr += 1;
  1137. if (slice_state != SIMPLE_INDEX)
  1138. goto BadEncoding;
  1139. end = strchr (renaming_expr, 'X');
  1140. if (end == NULL)
  1141. end = renaming_expr + strlen (renaming_expr);
  1142. operation_up arg = ada_pop ();
  1143. pstate->push_new<ada_structop_operation>
  1144. (std::move (arg), std::string (renaming_expr,
  1145. end - renaming_expr));
  1146. renaming_expr = end;
  1147. break;
  1148. }
  1149. default:
  1150. goto BadEncoding;
  1151. }
  1152. }
  1153. if (slice_state == SIMPLE_INDEX)
  1154. return;
  1155. BadEncoding:
  1156. error (_("Internal error in encoding of renaming declaration"));
  1157. }
  1158. static const struct block*
  1159. block_lookup (const struct block *context, const char *raw_name)
  1160. {
  1161. const char *name;
  1162. struct symtab *symtab;
  1163. const struct block *result = NULL;
  1164. std::string name_storage;
  1165. if (raw_name[0] == '\'')
  1166. {
  1167. raw_name += 1;
  1168. name = raw_name;
  1169. }
  1170. else
  1171. {
  1172. name_storage = ada_encode (raw_name);
  1173. name = name_storage.c_str ();
  1174. }
  1175. std::vector<struct block_symbol> syms
  1176. = ada_lookup_symbol_list (name, context, VAR_DOMAIN);
  1177. if (context == NULL
  1178. && (syms.empty () || syms[0].symbol->aclass () != LOC_BLOCK))
  1179. symtab = lookup_symtab (name);
  1180. else
  1181. symtab = NULL;
  1182. if (symtab != NULL)
  1183. result = BLOCKVECTOR_BLOCK (symtab->compunit ()->blockvector (),
  1184. STATIC_BLOCK);
  1185. else if (syms.empty () || syms[0].symbol->aclass () != LOC_BLOCK)
  1186. {
  1187. if (context == NULL)
  1188. error (_("No file or function \"%s\"."), raw_name);
  1189. else
  1190. error (_("No function \"%s\" in specified context."), raw_name);
  1191. }
  1192. else
  1193. {
  1194. if (syms.size () > 1)
  1195. warning (_("Function name \"%s\" ambiguous here"), raw_name);
  1196. result = SYMBOL_BLOCK_VALUE (syms[0].symbol);
  1197. }
  1198. return result;
  1199. }
  1200. static struct symbol*
  1201. select_possible_type_sym (const std::vector<struct block_symbol> &syms)
  1202. {
  1203. int i;
  1204. int preferred_index;
  1205. struct type *preferred_type;
  1206. preferred_index = -1; preferred_type = NULL;
  1207. for (i = 0; i < syms.size (); i += 1)
  1208. switch (syms[i].symbol->aclass ())
  1209. {
  1210. case LOC_TYPEDEF:
  1211. if (ada_prefer_type (syms[i].symbol->type (), preferred_type))
  1212. {
  1213. preferred_index = i;
  1214. preferred_type = syms[i].symbol->type ();
  1215. }
  1216. break;
  1217. case LOC_REGISTER:
  1218. case LOC_ARG:
  1219. case LOC_REF_ARG:
  1220. case LOC_REGPARM_ADDR:
  1221. case LOC_LOCAL:
  1222. case LOC_COMPUTED:
  1223. return NULL;
  1224. default:
  1225. break;
  1226. }
  1227. if (preferred_type == NULL)
  1228. return NULL;
  1229. return syms[preferred_index].symbol;
  1230. }
  1231. static struct type*
  1232. find_primitive_type (struct parser_state *par_state, const char *name)
  1233. {
  1234. struct type *type;
  1235. type = language_lookup_primitive_type (par_state->language (),
  1236. par_state->gdbarch (),
  1237. name);
  1238. if (type == NULL && strcmp ("system__address", name) == 0)
  1239. type = type_system_address (par_state);
  1240. if (type != NULL)
  1241. {
  1242. /* Check to see if we have a regular definition of this
  1243. type that just didn't happen to have been read yet. */
  1244. struct symbol *sym;
  1245. char *expanded_name =
  1246. (char *) alloca (strlen (name) + sizeof ("standard__"));
  1247. strcpy (expanded_name, "standard__");
  1248. strcat (expanded_name, name);
  1249. sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN).symbol;
  1250. if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
  1251. type = sym->type ();
  1252. }
  1253. return type;
  1254. }
  1255. static int
  1256. chop_selector (const char *name, int end)
  1257. {
  1258. int i;
  1259. for (i = end - 1; i > 0; i -= 1)
  1260. if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
  1261. return i;
  1262. return -1;
  1263. }
  1264. /* If NAME is a string beginning with a separator (either '__', or
  1265. '.'), chop this separator and return the result; else, return
  1266. NAME. */
  1267. static const char *
  1268. chop_separator (const char *name)
  1269. {
  1270. if (*name == '.')
  1271. return name + 1;
  1272. if (name[0] == '_' && name[1] == '_')
  1273. return name + 2;
  1274. return name;
  1275. }
  1276. /* Given that SELS is a string of the form (<sep><identifier>)*, where
  1277. <sep> is '__' or '.', write the indicated sequence of
  1278. STRUCTOP_STRUCT expression operators. Returns a pointer to the
  1279. last operation that was pushed. */
  1280. static ada_structop_operation *
  1281. write_selectors (struct parser_state *par_state, const char *sels)
  1282. {
  1283. ada_structop_operation *result = nullptr;
  1284. while (*sels != '\0')
  1285. {
  1286. const char *p = chop_separator (sels);
  1287. sels = p;
  1288. while (*sels != '\0' && *sels != '.'
  1289. && (sels[0] != '_' || sels[1] != '_'))
  1290. sels += 1;
  1291. operation_up arg = ada_pop ();
  1292. result = new ada_structop_operation (std::move (arg),
  1293. std::string (p, sels - p));
  1294. pstate->push (operation_up (result));
  1295. }
  1296. return result;
  1297. }
  1298. /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
  1299. NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes
  1300. a temporary symbol that is valid until the next call to ada_parse.
  1301. */
  1302. static void
  1303. write_ambiguous_var (struct parser_state *par_state,
  1304. const struct block *block, const char *name, int len)
  1305. {
  1306. struct symbol *sym = new (&temp_parse_space) symbol ();
  1307. sym->set_domain (UNDEF_DOMAIN);
  1308. sym->set_linkage_name (obstack_strndup (&temp_parse_space, name, len));
  1309. sym->set_language (language_ada, nullptr);
  1310. block_symbol bsym { sym, block };
  1311. par_state->push_new<ada_var_value_operation> (bsym);
  1312. }
  1313. /* A convenient wrapper around ada_get_field_index that takes
  1314. a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
  1315. of a NUL-terminated field name. */
  1316. static int
  1317. ada_nget_field_index (const struct type *type, const char *field_name0,
  1318. int field_name_len, int maybe_missing)
  1319. {
  1320. char *field_name = (char *) alloca ((field_name_len + 1) * sizeof (char));
  1321. strncpy (field_name, field_name0, field_name_len);
  1322. field_name[field_name_len] = '\0';
  1323. return ada_get_field_index (type, field_name, maybe_missing);
  1324. }
  1325. /* If encoded_field_name is the name of a field inside symbol SYM,
  1326. then return the type of that field. Otherwise, return NULL.
  1327. This function is actually recursive, so if ENCODED_FIELD_NAME
  1328. doesn't match one of the fields of our symbol, then try to see
  1329. if ENCODED_FIELD_NAME could not be a succession of field names
  1330. (in other words, the user entered an expression of the form
  1331. TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
  1332. each field name sequentially to obtain the desired field type.
  1333. In case of failure, we return NULL. */
  1334. static struct type *
  1335. get_symbol_field_type (struct symbol *sym, const char *encoded_field_name)
  1336. {
  1337. const char *field_name = encoded_field_name;
  1338. const char *subfield_name;
  1339. struct type *type = sym->type ();
  1340. int fieldno;
  1341. if (type == NULL || field_name == NULL)
  1342. return NULL;
  1343. type = check_typedef (type);
  1344. while (field_name[0] != '\0')
  1345. {
  1346. field_name = chop_separator (field_name);
  1347. fieldno = ada_get_field_index (type, field_name, 1);
  1348. if (fieldno >= 0)
  1349. return type->field (fieldno).type ();
  1350. subfield_name = field_name;
  1351. while (*subfield_name != '\0' && *subfield_name != '.'
  1352. && (subfield_name[0] != '_' || subfield_name[1] != '_'))
  1353. subfield_name += 1;
  1354. if (subfield_name[0] == '\0')
  1355. return NULL;
  1356. fieldno = ada_nget_field_index (type, field_name,
  1357. subfield_name - field_name, 1);
  1358. if (fieldno < 0)
  1359. return NULL;
  1360. type = type->field (fieldno).type ();
  1361. field_name = subfield_name;
  1362. }
  1363. return NULL;
  1364. }
  1365. /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
  1366. expression_block_context if NULL). If it denotes a type, return
  1367. that type. Otherwise, write expression code to evaluate it as an
  1368. object and return NULL. In this second case, NAME0 will, in general,
  1369. have the form <name>(.<selector_name>)*, where <name> is an object
  1370. or renaming encoded in the debugging data. Calls error if no
  1371. prefix <name> matches a name in the debugging data (i.e., matches
  1372. either a complete name or, as a wild-card match, the final
  1373. identifier). */
  1374. static struct type*
  1375. write_var_or_type (struct parser_state *par_state,
  1376. const struct block *block, struct stoken name0)
  1377. {
  1378. int depth;
  1379. char *encoded_name;
  1380. int name_len;
  1381. if (block == NULL)
  1382. block = par_state->expression_context_block;
  1383. std::string name_storage = ada_encode (name0.ptr);
  1384. name_len = name_storage.size ();
  1385. encoded_name = obstack_strndup (&temp_parse_space, name_storage.c_str (),
  1386. name_len);
  1387. for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
  1388. {
  1389. int tail_index;
  1390. tail_index = name_len;
  1391. while (tail_index > 0)
  1392. {
  1393. struct symbol *type_sym;
  1394. struct symbol *renaming_sym;
  1395. const char* renaming;
  1396. int renaming_len;
  1397. const char* renaming_expr;
  1398. int terminator = encoded_name[tail_index];
  1399. encoded_name[tail_index] = '\0';
  1400. /* In order to avoid double-encoding, we want to only pass
  1401. the decoded form to lookup functions. */
  1402. std::string decoded_name = ada_decode (encoded_name);
  1403. encoded_name[tail_index] = terminator;
  1404. std::vector<struct block_symbol> syms
  1405. = ada_lookup_symbol_list (decoded_name.c_str (), block, VAR_DOMAIN);
  1406. type_sym = select_possible_type_sym (syms);
  1407. if (type_sym != NULL)
  1408. renaming_sym = type_sym;
  1409. else if (syms.size () == 1)
  1410. renaming_sym = syms[0].symbol;
  1411. else
  1412. renaming_sym = NULL;
  1413. switch (ada_parse_renaming (renaming_sym, &renaming,
  1414. &renaming_len, &renaming_expr))
  1415. {
  1416. case ADA_NOT_RENAMING:
  1417. break;
  1418. case ADA_PACKAGE_RENAMING:
  1419. case ADA_EXCEPTION_RENAMING:
  1420. case ADA_SUBPROGRAM_RENAMING:
  1421. {
  1422. int alloc_len = renaming_len + name_len - tail_index + 1;
  1423. char *new_name
  1424. = (char *) obstack_alloc (&temp_parse_space, alloc_len);
  1425. strncpy (new_name, renaming, renaming_len);
  1426. strcpy (new_name + renaming_len, encoded_name + tail_index);
  1427. encoded_name = new_name;
  1428. name_len = renaming_len + name_len - tail_index;
  1429. goto TryAfterRenaming;
  1430. }
  1431. case ADA_OBJECT_RENAMING:
  1432. write_object_renaming (par_state, block, renaming, renaming_len,
  1433. renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
  1434. write_selectors (par_state, encoded_name + tail_index);
  1435. return NULL;
  1436. default:
  1437. internal_error (__FILE__, __LINE__,
  1438. _("impossible value from ada_parse_renaming"));
  1439. }
  1440. if (type_sym != NULL)
  1441. {
  1442. struct type *field_type;
  1443. if (tail_index == name_len)
  1444. return type_sym->type ();
  1445. /* We have some extraneous characters after the type name.
  1446. If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
  1447. then try to get the type of FIELDN. */
  1448. field_type
  1449. = get_symbol_field_type (type_sym, encoded_name + tail_index);
  1450. if (field_type != NULL)
  1451. return field_type;
  1452. else
  1453. error (_("Invalid attempt to select from type: \"%s\"."),
  1454. name0.ptr);
  1455. }
  1456. else if (tail_index == name_len && syms.empty ())
  1457. {
  1458. struct type *type = find_primitive_type (par_state,
  1459. encoded_name);
  1460. if (type != NULL)
  1461. return type;
  1462. }
  1463. if (syms.size () == 1)
  1464. {
  1465. write_var_from_sym (par_state, syms[0]);
  1466. write_selectors (par_state, encoded_name + tail_index);
  1467. return NULL;
  1468. }
  1469. else if (syms.empty ())
  1470. {
  1471. struct bound_minimal_symbol msym
  1472. = ada_lookup_simple_minsym (decoded_name.c_str ());
  1473. if (msym.minsym != NULL)
  1474. {
  1475. par_state->push_new<ada_var_msym_value_operation> (msym);
  1476. /* Maybe cause error here rather than later? FIXME? */
  1477. write_selectors (par_state, encoded_name + tail_index);
  1478. return NULL;
  1479. }
  1480. if (tail_index == name_len
  1481. && strncmp (encoded_name, "standard__",
  1482. sizeof ("standard__") - 1) == 0)
  1483. error (_("No definition of \"%s\" found."), name0.ptr);
  1484. tail_index = chop_selector (encoded_name, tail_index);
  1485. }
  1486. else
  1487. {
  1488. write_ambiguous_var (par_state, block, encoded_name,
  1489. tail_index);
  1490. write_selectors (par_state, encoded_name + tail_index);
  1491. return NULL;
  1492. }
  1493. }
  1494. if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
  1495. error (_("No symbol table is loaded. Use the \"file\" command."));
  1496. if (block == par_state->expression_context_block)
  1497. error (_("No definition of \"%s\" in current context."), name0.ptr);
  1498. else
  1499. error (_("No definition of \"%s\" in specified context."), name0.ptr);
  1500. TryAfterRenaming: ;
  1501. }
  1502. error (_("Could not find renamed symbol \"%s\""), name0.ptr);
  1503. }
  1504. /* Because ada_completer_word_break_characters does not contain '.' --
  1505. and it cannot easily be added, this breaks other completions -- we
  1506. have to recreate the completion word-splitting here, so that we can
  1507. provide a prefix that is then used when completing field names.
  1508. Without this, an attempt like "complete print abc.d" will give a
  1509. result like "print def" rather than "print abc.def". */
  1510. static std::string
  1511. find_completion_bounds (struct parser_state *par_state)
  1512. {
  1513. const char *end = pstate->lexptr;
  1514. /* First the end of the prefix. Here we stop at the token start or
  1515. at '.' or space. */
  1516. for (; end > original_expr && end[-1] != '.' && !isspace (end[-1]); --end)
  1517. {
  1518. /* Nothing. */
  1519. }
  1520. /* Now find the start of the prefix. */
  1521. const char *ptr = end;
  1522. /* Here we allow '.'. */
  1523. for (;
  1524. ptr > original_expr && (ptr[-1] == '.'
  1525. || ptr[-1] == '_'
  1526. || (ptr[-1] >= 'a' && ptr[-1] <= 'z')
  1527. || (ptr[-1] >= 'A' && ptr[-1] <= 'Z')
  1528. || (ptr[-1] & 0xff) >= 0x80);
  1529. --ptr)
  1530. {
  1531. /* Nothing. */
  1532. }
  1533. /* ... except, skip leading spaces. */
  1534. ptr = skip_spaces (ptr);
  1535. return std::string (ptr, end);
  1536. }
  1537. /* A wrapper for write_var_or_type that is used specifically when
  1538. completion is requested for the last of a sequence of
  1539. identifiers. */
  1540. static struct type *
  1541. write_var_or_type_completion (struct parser_state *par_state,
  1542. const struct block *block, struct stoken name0)
  1543. {
  1544. int tail_index = chop_selector (name0.ptr, name0.length);
  1545. /* If there's no separator, just defer to ordinary symbol
  1546. completion. */
  1547. if (tail_index == -1)
  1548. return write_var_or_type (par_state, block, name0);
  1549. std::string copy (name0.ptr, tail_index);
  1550. struct type *type = write_var_or_type (par_state, block,
  1551. { copy.c_str (),
  1552. (int) copy.length () });
  1553. /* For completion purposes, it's enough that we return a type
  1554. here. */
  1555. if (type != nullptr)
  1556. return type;
  1557. ada_structop_operation *op = write_selectors (par_state,
  1558. name0.ptr + tail_index);
  1559. op->set_prefix (find_completion_bounds (par_state));
  1560. par_state->mark_struct_expression (op);
  1561. return nullptr;
  1562. }
  1563. /* Write a left side of a component association (e.g., NAME in NAME =>
  1564. exp). If NAME has the form of a selected component, write it as an
  1565. ordinary expression. If it is a simple variable that unambiguously
  1566. corresponds to exactly one symbol that does not denote a type or an
  1567. object renaming, also write it normally as an OP_VAR_VALUE.
  1568. Otherwise, write it as an OP_NAME.
  1569. Unfortunately, we don't know at this point whether NAME is supposed
  1570. to denote a record component name or the value of an array index.
  1571. Therefore, it is not appropriate to disambiguate an ambiguous name
  1572. as we normally would, nor to replace a renaming with its referent.
  1573. As a result, in the (one hopes) rare case that one writes an
  1574. aggregate such as (R => 42) where R renames an object or is an
  1575. ambiguous name, one must write instead ((R) => 42). */
  1576. static void
  1577. write_name_assoc (struct parser_state *par_state, struct stoken name)
  1578. {
  1579. if (strchr (name.ptr, '.') == NULL)
  1580. {
  1581. std::vector<struct block_symbol> syms
  1582. = ada_lookup_symbol_list (name.ptr,
  1583. par_state->expression_context_block,
  1584. VAR_DOMAIN);
  1585. if (syms.size () != 1 || syms[0].symbol->aclass () == LOC_TYPEDEF)
  1586. pstate->push_new<ada_string_operation> (copy_name (name));
  1587. else
  1588. write_var_from_sym (par_state, syms[0]);
  1589. }
  1590. else
  1591. if (write_var_or_type (par_state, NULL, name) != NULL)
  1592. error (_("Invalid use of type."));
  1593. push_association<ada_name_association> (ada_pop ());
  1594. }
  1595. static struct type *
  1596. type_int (struct parser_state *par_state)
  1597. {
  1598. return parse_type (par_state)->builtin_int;
  1599. }
  1600. static struct type *
  1601. type_long (struct parser_state *par_state)
  1602. {
  1603. return parse_type (par_state)->builtin_long;
  1604. }
  1605. static struct type *
  1606. type_long_long (struct parser_state *par_state)
  1607. {
  1608. return parse_type (par_state)->builtin_long_long;
  1609. }
  1610. static struct type *
  1611. type_long_double (struct parser_state *par_state)
  1612. {
  1613. return parse_type (par_state)->builtin_long_double;
  1614. }
  1615. static struct type *
  1616. type_for_char (struct parser_state *par_state, ULONGEST value)
  1617. {
  1618. if (value <= 0xff)
  1619. return language_string_char_type (par_state->language (),
  1620. par_state->gdbarch ());
  1621. else if (value <= 0xffff)
  1622. return language_lookup_primitive_type (par_state->language (),
  1623. par_state->gdbarch (),
  1624. "wide_character");
  1625. return language_lookup_primitive_type (par_state->language (),
  1626. par_state->gdbarch (),
  1627. "wide_wide_character");
  1628. }
  1629. static struct type *
  1630. type_boolean (struct parser_state *par_state)
  1631. {
  1632. return parse_type (par_state)->builtin_bool;
  1633. }
  1634. static struct type *
  1635. type_system_address (struct parser_state *par_state)
  1636. {
  1637. struct type *type
  1638. = language_lookup_primitive_type (par_state->language (),
  1639. par_state->gdbarch (),
  1640. "system__address");
  1641. return type != NULL ? type : parse_type (par_state)->builtin_data_ptr;
  1642. }
  1643. void _initialize_ada_exp ();
  1644. void
  1645. _initialize_ada_exp ()
  1646. {
  1647. obstack_init (&temp_parse_space);
  1648. }