d-exp.y 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /* YACC parser for D expressions, for GDB.
  2. Copyright (C) 2014-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. /* This file is derived from c-exp.y, jv-exp.y. */
  15. /* Parse a D expression from text in a string,
  16. and return the result as a struct expression pointer.
  17. That structure contains arithmetic operations in reverse polish,
  18. with constants represented by operations that are followed by special data.
  19. See expression.h for the details of the format.
  20. What is important here is that it can be built up sequentially
  21. during the process of parsing; the lower levels of the tree always
  22. come first in the result.
  23. Note that malloc's and realloc's in this file are transformed to
  24. xmalloc and xrealloc respectively by the same sed command in the
  25. makefile that remaps any other malloc/realloc inserted by the parser
  26. generator. Doing this with #defines and trying to control the interaction
  27. with include files (<malloc.h> and <stdlib.h> for example) just became
  28. too messy, particularly when such includes can be inserted at random
  29. times by the parser generator. */
  30. %{
  31. #include "defs.h"
  32. #include <ctype.h>
  33. #include "expression.h"
  34. #include "value.h"
  35. #include "parser-defs.h"
  36. #include "language.h"
  37. #include "c-lang.h"
  38. #include "d-lang.h"
  39. #include "bfd.h" /* Required by objfiles.h. */
  40. #include "symfile.h" /* Required by objfiles.h. */
  41. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  42. #include "charset.h"
  43. #include "block.h"
  44. #include "type-stack.h"
  45. #include "expop.h"
  46. #define parse_type(ps) builtin_type (ps->gdbarch ())
  47. #define parse_d_type(ps) builtin_d_type (ps->gdbarch ())
  48. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
  49. etc). */
  50. #define GDB_YY_REMAP_PREFIX d_
  51. #include "yy-remap.h"
  52. /* The state of the parser, used internally when we are parsing the
  53. expression. */
  54. static struct parser_state *pstate = NULL;
  55. /* The current type stack. */
  56. static struct type_stack *type_stack;
  57. int yyparse (void);
  58. static int yylex (void);
  59. static void yyerror (const char *);
  60. static int type_aggregate_p (struct type *);
  61. using namespace expr;
  62. %}
  63. /* Although the yacc "value" of an expression is not used,
  64. since the result is stored in the structure being created,
  65. other node types do have values. */
  66. %union
  67. {
  68. struct {
  69. LONGEST val;
  70. struct type *type;
  71. } typed_val_int;
  72. struct {
  73. gdb_byte val[16];
  74. struct type *type;
  75. } typed_val_float;
  76. struct symbol *sym;
  77. struct type *tval;
  78. struct typed_stoken tsval;
  79. struct stoken sval;
  80. struct ttype tsym;
  81. struct symtoken ssym;
  82. int ival;
  83. int voidval;
  84. enum exp_opcode opcode;
  85. struct stoken_vector svec;
  86. }
  87. %{
  88. /* YYSTYPE gets defined by %union */
  89. static int parse_number (struct parser_state *, const char *,
  90. int, int, YYSTYPE *);
  91. %}
  92. %token <sval> IDENTIFIER UNKNOWN_NAME
  93. %token <tsym> TYPENAME
  94. %token <voidval> COMPLETE
  95. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  96. but which would parse as a valid number in the current input radix.
  97. E.g. "c" when input_radix==16. Depending on the parse, it will be
  98. turned into a name or into a number. */
  99. %token <sval> NAME_OR_INT
  100. %token <typed_val_int> INTEGER_LITERAL
  101. %token <typed_val_float> FLOAT_LITERAL
  102. %token <tsval> CHARACTER_LITERAL
  103. %token <tsval> STRING_LITERAL
  104. %type <svec> StringExp
  105. %type <tval> BasicType TypeExp
  106. %type <sval> IdentifierExp
  107. %type <ival> ArrayLiteral
  108. %token ENTRY
  109. %token ERROR
  110. /* Keywords that have a constant value. */
  111. %token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
  112. /* Class 'super' accessor. */
  113. %token SUPER_KEYWORD
  114. /* Properties. */
  115. %token CAST_KEYWORD SIZEOF_KEYWORD
  116. %token TYPEOF_KEYWORD TYPEID_KEYWORD
  117. %token INIT_KEYWORD
  118. /* Comparison keywords. */
  119. /* Type storage classes. */
  120. %token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
  121. /* Non-scalar type keywords. */
  122. %token STRUCT_KEYWORD UNION_KEYWORD
  123. %token CLASS_KEYWORD INTERFACE_KEYWORD
  124. %token ENUM_KEYWORD TEMPLATE_KEYWORD
  125. %token DELEGATE_KEYWORD FUNCTION_KEYWORD
  126. %token <sval> DOLLAR_VARIABLE
  127. %token <opcode> ASSIGN_MODIFY
  128. %left ','
  129. %right '=' ASSIGN_MODIFY
  130. %right '?'
  131. %left OROR
  132. %left ANDAND
  133. %left '|'
  134. %left '^'
  135. %left '&'
  136. %left EQUAL NOTEQUAL '<' '>' LEQ GEQ
  137. %right LSH RSH
  138. %left '+' '-'
  139. %left '*' '/' '%'
  140. %right HATHAT
  141. %left IDENTITY NOTIDENTITY
  142. %right INCREMENT DECREMENT
  143. %right '.' '[' '('
  144. %token DOTDOT
  145. %%
  146. start :
  147. Expression
  148. | TypeExp
  149. ;
  150. /* Expressions, including the comma operator. */
  151. Expression:
  152. CommaExpression
  153. ;
  154. CommaExpression:
  155. AssignExpression
  156. | AssignExpression ',' CommaExpression
  157. { pstate->wrap2<comma_operation> (); }
  158. ;
  159. AssignExpression:
  160. ConditionalExpression
  161. | ConditionalExpression '=' AssignExpression
  162. { pstate->wrap2<assign_operation> (); }
  163. | ConditionalExpression ASSIGN_MODIFY AssignExpression
  164. {
  165. operation_up rhs = pstate->pop ();
  166. operation_up lhs = pstate->pop ();
  167. pstate->push_new<assign_modify_operation>
  168. ($2, std::move (lhs), std::move (rhs));
  169. }
  170. ;
  171. ConditionalExpression:
  172. OrOrExpression
  173. | OrOrExpression '?' Expression ':' ConditionalExpression
  174. {
  175. operation_up last = pstate->pop ();
  176. operation_up mid = pstate->pop ();
  177. operation_up first = pstate->pop ();
  178. pstate->push_new<ternop_cond_operation>
  179. (std::move (first), std::move (mid),
  180. std::move (last));
  181. }
  182. ;
  183. OrOrExpression:
  184. AndAndExpression
  185. | OrOrExpression OROR AndAndExpression
  186. { pstate->wrap2<logical_or_operation> (); }
  187. ;
  188. AndAndExpression:
  189. OrExpression
  190. | AndAndExpression ANDAND OrExpression
  191. { pstate->wrap2<logical_and_operation> (); }
  192. ;
  193. OrExpression:
  194. XorExpression
  195. | OrExpression '|' XorExpression
  196. { pstate->wrap2<bitwise_ior_operation> (); }
  197. ;
  198. XorExpression:
  199. AndExpression
  200. | XorExpression '^' AndExpression
  201. { pstate->wrap2<bitwise_xor_operation> (); }
  202. ;
  203. AndExpression:
  204. CmpExpression
  205. | AndExpression '&' CmpExpression
  206. { pstate->wrap2<bitwise_and_operation> (); }
  207. ;
  208. CmpExpression:
  209. ShiftExpression
  210. | EqualExpression
  211. | IdentityExpression
  212. | RelExpression
  213. ;
  214. EqualExpression:
  215. ShiftExpression EQUAL ShiftExpression
  216. { pstate->wrap2<equal_operation> (); }
  217. | ShiftExpression NOTEQUAL ShiftExpression
  218. { pstate->wrap2<notequal_operation> (); }
  219. ;
  220. IdentityExpression:
  221. ShiftExpression IDENTITY ShiftExpression
  222. { pstate->wrap2<equal_operation> (); }
  223. | ShiftExpression NOTIDENTITY ShiftExpression
  224. { pstate->wrap2<notequal_operation> (); }
  225. ;
  226. RelExpression:
  227. ShiftExpression '<' ShiftExpression
  228. { pstate->wrap2<less_operation> (); }
  229. | ShiftExpression LEQ ShiftExpression
  230. { pstate->wrap2<leq_operation> (); }
  231. | ShiftExpression '>' ShiftExpression
  232. { pstate->wrap2<gtr_operation> (); }
  233. | ShiftExpression GEQ ShiftExpression
  234. { pstate->wrap2<geq_operation> (); }
  235. ;
  236. ShiftExpression:
  237. AddExpression
  238. | ShiftExpression LSH AddExpression
  239. { pstate->wrap2<lsh_operation> (); }
  240. | ShiftExpression RSH AddExpression
  241. { pstate->wrap2<rsh_operation> (); }
  242. ;
  243. AddExpression:
  244. MulExpression
  245. | AddExpression '+' MulExpression
  246. { pstate->wrap2<add_operation> (); }
  247. | AddExpression '-' MulExpression
  248. { pstate->wrap2<sub_operation> (); }
  249. | AddExpression '~' MulExpression
  250. { pstate->wrap2<concat_operation> (); }
  251. ;
  252. MulExpression:
  253. UnaryExpression
  254. | MulExpression '*' UnaryExpression
  255. { pstate->wrap2<mul_operation> (); }
  256. | MulExpression '/' UnaryExpression
  257. { pstate->wrap2<div_operation> (); }
  258. | MulExpression '%' UnaryExpression
  259. { pstate->wrap2<rem_operation> (); }
  260. UnaryExpression:
  261. '&' UnaryExpression
  262. { pstate->wrap<unop_addr_operation> (); }
  263. | INCREMENT UnaryExpression
  264. { pstate->wrap<preinc_operation> (); }
  265. | DECREMENT UnaryExpression
  266. { pstate->wrap<predec_operation> (); }
  267. | '*' UnaryExpression
  268. { pstate->wrap<unop_ind_operation> (); }
  269. | '-' UnaryExpression
  270. { pstate->wrap<unary_neg_operation> (); }
  271. | '+' UnaryExpression
  272. { pstate->wrap<unary_plus_operation> (); }
  273. | '!' UnaryExpression
  274. { pstate->wrap<unary_logical_not_operation> (); }
  275. | '~' UnaryExpression
  276. { pstate->wrap<unary_complement_operation> (); }
  277. | TypeExp '.' SIZEOF_KEYWORD
  278. { pstate->wrap<unop_sizeof_operation> (); }
  279. | CastExpression
  280. | PowExpression
  281. ;
  282. CastExpression:
  283. CAST_KEYWORD '(' TypeExp ')' UnaryExpression
  284. { pstate->wrap2<unop_cast_type_operation> (); }
  285. /* C style cast is illegal D, but is still recognised in
  286. the grammar, so we keep this around for convenience. */
  287. | '(' TypeExp ')' UnaryExpression
  288. { pstate->wrap2<unop_cast_type_operation> (); }
  289. ;
  290. PowExpression:
  291. PostfixExpression
  292. | PostfixExpression HATHAT UnaryExpression
  293. { pstate->wrap2<exp_operation> (); }
  294. ;
  295. PostfixExpression:
  296. PrimaryExpression
  297. | PostfixExpression '.' COMPLETE
  298. {
  299. structop_base_operation *op
  300. = new structop_ptr_operation (pstate->pop (), "");
  301. pstate->mark_struct_expression (op);
  302. pstate->push (operation_up (op));
  303. }
  304. | PostfixExpression '.' IDENTIFIER
  305. {
  306. pstate->push_new<structop_operation>
  307. (pstate->pop (), copy_name ($3));
  308. }
  309. | PostfixExpression '.' IDENTIFIER COMPLETE
  310. {
  311. structop_base_operation *op
  312. = new structop_operation (pstate->pop (), copy_name ($3));
  313. pstate->mark_struct_expression (op);
  314. pstate->push (operation_up (op));
  315. }
  316. | PostfixExpression '.' SIZEOF_KEYWORD
  317. { pstate->wrap<unop_sizeof_operation> (); }
  318. | PostfixExpression INCREMENT
  319. { pstate->wrap<postinc_operation> (); }
  320. | PostfixExpression DECREMENT
  321. { pstate->wrap<postdec_operation> (); }
  322. | CallExpression
  323. | IndexExpression
  324. | SliceExpression
  325. ;
  326. ArgumentList:
  327. AssignExpression
  328. { pstate->arglist_len = 1; }
  329. | ArgumentList ',' AssignExpression
  330. { pstate->arglist_len++; }
  331. ;
  332. ArgumentList_opt:
  333. /* EMPTY */
  334. { pstate->arglist_len = 0; }
  335. | ArgumentList
  336. ;
  337. CallExpression:
  338. PostfixExpression '('
  339. { pstate->start_arglist (); }
  340. ArgumentList_opt ')'
  341. {
  342. std::vector<operation_up> args
  343. = pstate->pop_vector (pstate->end_arglist ());
  344. pstate->push_new<funcall_operation>
  345. (pstate->pop (), std::move (args));
  346. }
  347. ;
  348. IndexExpression:
  349. PostfixExpression '[' ArgumentList ']'
  350. { if (pstate->arglist_len > 0)
  351. {
  352. std::vector<operation_up> args
  353. = pstate->pop_vector (pstate->arglist_len);
  354. pstate->push_new<multi_subscript_operation>
  355. (pstate->pop (), std::move (args));
  356. }
  357. else
  358. pstate->wrap2<subscript_operation> ();
  359. }
  360. ;
  361. SliceExpression:
  362. PostfixExpression '[' ']'
  363. { /* Do nothing. */ }
  364. | PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']'
  365. {
  366. operation_up last = pstate->pop ();
  367. operation_up mid = pstate->pop ();
  368. operation_up first = pstate->pop ();
  369. pstate->push_new<ternop_slice_operation>
  370. (std::move (first), std::move (mid),
  371. std::move (last));
  372. }
  373. ;
  374. PrimaryExpression:
  375. '(' Expression ')'
  376. { /* Do nothing. */ }
  377. | IdentifierExp
  378. { struct bound_minimal_symbol msymbol;
  379. std::string copy = copy_name ($1);
  380. struct field_of_this_result is_a_field_of_this;
  381. struct block_symbol sym;
  382. /* Handle VAR, which could be local or global. */
  383. sym = lookup_symbol (copy.c_str (),
  384. pstate->expression_context_block,
  385. VAR_DOMAIN, &is_a_field_of_this);
  386. if (sym.symbol && sym.symbol->aclass () != LOC_TYPEDEF)
  387. {
  388. if (symbol_read_needs_frame (sym.symbol))
  389. pstate->block_tracker->update (sym);
  390. pstate->push_new<var_value_operation> (sym);
  391. }
  392. else if (is_a_field_of_this.type != NULL)
  393. {
  394. /* It hangs off of `this'. Must not inadvertently convert from a
  395. method call to data ref. */
  396. pstate->block_tracker->update (sym);
  397. operation_up thisop
  398. = make_operation<op_this_operation> ();
  399. pstate->push_new<structop_ptr_operation>
  400. (std::move (thisop), std::move (copy));
  401. }
  402. else
  403. {
  404. /* Lookup foreign name in global static symbols. */
  405. msymbol = lookup_bound_minimal_symbol (copy.c_str ());
  406. if (msymbol.minsym != NULL)
  407. pstate->push_new<var_msym_value_operation> (msymbol);
  408. else if (!have_full_symbols () && !have_partial_symbols ())
  409. error (_("No symbol table is loaded. Use the \"file\" command"));
  410. else
  411. error (_("No symbol \"%s\" in current context."),
  412. copy.c_str ());
  413. }
  414. }
  415. | TypeExp '.' IdentifierExp
  416. { struct type *type = check_typedef ($1);
  417. /* Check if the qualified name is in the global
  418. context. However if the symbol has not already
  419. been resolved, it's not likely to be found. */
  420. if (type->code () == TYPE_CODE_MODULE)
  421. {
  422. struct block_symbol sym;
  423. const char *type_name = TYPE_SAFE_NAME (type);
  424. int type_name_len = strlen (type_name);
  425. std::string name
  426. = string_printf ("%.*s.%.*s",
  427. type_name_len, type_name,
  428. $3.length, $3.ptr);
  429. sym =
  430. lookup_symbol (name.c_str (),
  431. (const struct block *) NULL,
  432. VAR_DOMAIN, NULL);
  433. pstate->push_symbol (name.c_str (), sym);
  434. }
  435. else
  436. {
  437. /* Check if the qualified name resolves as a member
  438. of an aggregate or an enum type. */
  439. if (!type_aggregate_p (type))
  440. error (_("`%s' is not defined as an aggregate type."),
  441. TYPE_SAFE_NAME (type));
  442. pstate->push_new<scope_operation>
  443. (type, copy_name ($3));
  444. }
  445. }
  446. | DOLLAR_VARIABLE
  447. { pstate->push_dollar ($1); }
  448. | NAME_OR_INT
  449. { YYSTYPE val;
  450. parse_number (pstate, $1.ptr, $1.length, 0, &val);
  451. pstate->push_new<long_const_operation>
  452. (val.typed_val_int.type, val.typed_val_int.val); }
  453. | NULL_KEYWORD
  454. { struct type *type = parse_d_type (pstate)->builtin_void;
  455. type = lookup_pointer_type (type);
  456. pstate->push_new<long_const_operation> (type, 0); }
  457. | TRUE_KEYWORD
  458. { pstate->push_new<bool_operation> (true); }
  459. | FALSE_KEYWORD
  460. { pstate->push_new<bool_operation> (false); }
  461. | INTEGER_LITERAL
  462. { pstate->push_new<long_const_operation> ($1.type, $1.val); }
  463. | FLOAT_LITERAL
  464. {
  465. float_data data;
  466. std::copy (std::begin ($1.val), std::end ($1.val),
  467. std::begin (data));
  468. pstate->push_new<float_const_operation> ($1.type, data);
  469. }
  470. | CHARACTER_LITERAL
  471. { struct stoken_vector vec;
  472. vec.len = 1;
  473. vec.tokens = &$1;
  474. pstate->push_c_string (0, &vec); }
  475. | StringExp
  476. { int i;
  477. pstate->push_c_string (0, &$1);
  478. for (i = 0; i < $1.len; ++i)
  479. free ($1.tokens[i].ptr);
  480. free ($1.tokens); }
  481. | ArrayLiteral
  482. {
  483. std::vector<operation_up> args
  484. = pstate->pop_vector ($1);
  485. pstate->push_new<array_operation>
  486. (0, $1 - 1, std::move (args));
  487. }
  488. | TYPEOF_KEYWORD '(' Expression ')'
  489. { pstate->wrap<typeof_operation> (); }
  490. ;
  491. ArrayLiteral:
  492. '[' ArgumentList_opt ']'
  493. { $$ = pstate->arglist_len; }
  494. ;
  495. IdentifierExp:
  496. IDENTIFIER
  497. ;
  498. StringExp:
  499. STRING_LITERAL
  500. { /* We copy the string here, and not in the
  501. lexer, to guarantee that we do not leak a
  502. string. Note that we follow the
  503. NUL-termination convention of the
  504. lexer. */
  505. struct typed_stoken *vec = XNEW (struct typed_stoken);
  506. $$.len = 1;
  507. $$.tokens = vec;
  508. vec->type = $1.type;
  509. vec->length = $1.length;
  510. vec->ptr = (char *) malloc ($1.length + 1);
  511. memcpy (vec->ptr, $1.ptr, $1.length + 1);
  512. }
  513. | StringExp STRING_LITERAL
  514. { /* Note that we NUL-terminate here, but just
  515. for convenience. */
  516. char *p;
  517. ++$$.len;
  518. $$.tokens
  519. = XRESIZEVEC (struct typed_stoken, $$.tokens, $$.len);
  520. p = (char *) malloc ($2.length + 1);
  521. memcpy (p, $2.ptr, $2.length + 1);
  522. $$.tokens[$$.len - 1].type = $2.type;
  523. $$.tokens[$$.len - 1].length = $2.length;
  524. $$.tokens[$$.len - 1].ptr = p;
  525. }
  526. ;
  527. TypeExp:
  528. '(' TypeExp ')'
  529. { /* Do nothing. */ }
  530. | BasicType
  531. { pstate->push_new<type_operation> ($1); }
  532. | BasicType BasicType2
  533. { $$ = type_stack->follow_types ($1);
  534. pstate->push_new<type_operation> ($$);
  535. }
  536. ;
  537. BasicType2:
  538. '*'
  539. { type_stack->push (tp_pointer); }
  540. | '*' BasicType2
  541. { type_stack->push (tp_pointer); }
  542. | '[' INTEGER_LITERAL ']'
  543. { type_stack->push ($2.val);
  544. type_stack->push (tp_array); }
  545. | '[' INTEGER_LITERAL ']' BasicType2
  546. { type_stack->push ($2.val);
  547. type_stack->push (tp_array); }
  548. ;
  549. BasicType:
  550. TYPENAME
  551. { $$ = $1.type; }
  552. ;
  553. %%
  554. /* Return true if the type is aggregate-like. */
  555. static int
  556. type_aggregate_p (struct type *type)
  557. {
  558. return (type->code () == TYPE_CODE_STRUCT
  559. || type->code () == TYPE_CODE_UNION
  560. || type->code () == TYPE_CODE_MODULE
  561. || (type->code () == TYPE_CODE_ENUM
  562. && type->is_declared_class ()));
  563. }
  564. /* Take care of parsing a number (anything that starts with a digit).
  565. Set yylval and return the token type; update lexptr.
  566. LEN is the number of characters in it. */
  567. /*** Needs some error checking for the float case ***/
  568. static int
  569. parse_number (struct parser_state *ps, const char *p,
  570. int len, int parsed_float, YYSTYPE *putithere)
  571. {
  572. ULONGEST n = 0;
  573. ULONGEST prevn = 0;
  574. ULONGEST un;
  575. int i = 0;
  576. int c;
  577. int base = input_radix;
  578. int unsigned_p = 0;
  579. int long_p = 0;
  580. /* We have found a "L" or "U" suffix. */
  581. int found_suffix = 0;
  582. ULONGEST high_bit;
  583. struct type *signed_type;
  584. struct type *unsigned_type;
  585. if (parsed_float)
  586. {
  587. char *s, *sp;
  588. /* Strip out all embedded '_' before passing to parse_float. */
  589. s = (char *) alloca (len + 1);
  590. sp = s;
  591. while (len-- > 0)
  592. {
  593. if (*p != '_')
  594. *sp++ = *p;
  595. p++;
  596. }
  597. *sp = '\0';
  598. len = strlen (s);
  599. /* Check suffix for `i' , `fi' or `li' (idouble, ifloat or ireal). */
  600. if (len >= 1 && tolower (s[len - 1]) == 'i')
  601. {
  602. if (len >= 2 && tolower (s[len - 2]) == 'f')
  603. {
  604. putithere->typed_val_float.type
  605. = parse_d_type (ps)->builtin_ifloat;
  606. len -= 2;
  607. }
  608. else if (len >= 2 && tolower (s[len - 2]) == 'l')
  609. {
  610. putithere->typed_val_float.type
  611. = parse_d_type (ps)->builtin_ireal;
  612. len -= 2;
  613. }
  614. else
  615. {
  616. putithere->typed_val_float.type
  617. = parse_d_type (ps)->builtin_idouble;
  618. len -= 1;
  619. }
  620. }
  621. /* Check suffix for `f' or `l'' (float or real). */
  622. else if (len >= 1 && tolower (s[len - 1]) == 'f')
  623. {
  624. putithere->typed_val_float.type
  625. = parse_d_type (ps)->builtin_float;
  626. len -= 1;
  627. }
  628. else if (len >= 1 && tolower (s[len - 1]) == 'l')
  629. {
  630. putithere->typed_val_float.type
  631. = parse_d_type (ps)->builtin_real;
  632. len -= 1;
  633. }
  634. /* Default type if no suffix. */
  635. else
  636. {
  637. putithere->typed_val_float.type
  638. = parse_d_type (ps)->builtin_double;
  639. }
  640. if (!parse_float (s, len,
  641. putithere->typed_val_float.type,
  642. putithere->typed_val_float.val))
  643. return ERROR;
  644. return FLOAT_LITERAL;
  645. }
  646. /* Handle base-switching prefixes 0x, 0b, 0 */
  647. if (p[0] == '0')
  648. switch (p[1])
  649. {
  650. case 'x':
  651. case 'X':
  652. if (len >= 3)
  653. {
  654. p += 2;
  655. base = 16;
  656. len -= 2;
  657. }
  658. break;
  659. case 'b':
  660. case 'B':
  661. if (len >= 3)
  662. {
  663. p += 2;
  664. base = 2;
  665. len -= 2;
  666. }
  667. break;
  668. default:
  669. base = 8;
  670. break;
  671. }
  672. while (len-- > 0)
  673. {
  674. c = *p++;
  675. if (c == '_')
  676. continue; /* Ignore embedded '_'. */
  677. if (c >= 'A' && c <= 'Z')
  678. c += 'a' - 'A';
  679. if (c != 'l' && c != 'u')
  680. n *= base;
  681. if (c >= '0' && c <= '9')
  682. {
  683. if (found_suffix)
  684. return ERROR;
  685. n += i = c - '0';
  686. }
  687. else
  688. {
  689. if (base > 10 && c >= 'a' && c <= 'f')
  690. {
  691. if (found_suffix)
  692. return ERROR;
  693. n += i = c - 'a' + 10;
  694. }
  695. else if (c == 'l' && long_p == 0)
  696. {
  697. long_p = 1;
  698. found_suffix = 1;
  699. }
  700. else if (c == 'u' && unsigned_p == 0)
  701. {
  702. unsigned_p = 1;
  703. found_suffix = 1;
  704. }
  705. else
  706. return ERROR; /* Char not a digit */
  707. }
  708. if (i >= base)
  709. return ERROR; /* Invalid digit in this base. */
  710. /* Portably test for integer overflow. */
  711. if (c != 'l' && c != 'u')
  712. {
  713. ULONGEST n2 = prevn * base;
  714. if ((n2 / base != prevn) || (n2 + i < prevn))
  715. error (_("Numeric constant too large."));
  716. }
  717. prevn = n;
  718. }
  719. /* An integer constant is an int or a long. An L suffix forces it to
  720. be long, and a U suffix forces it to be unsigned. To figure out
  721. whether it fits, we shift it right and see whether anything remains.
  722. Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
  723. more in one operation, because many compilers will warn about such a
  724. shift (which always produces a zero result). To deal with the case
  725. where it is we just always shift the value more than once, with fewer
  726. bits each time. */
  727. un = (ULONGEST) n >> 2;
  728. if (long_p == 0 && (un >> 30) == 0)
  729. {
  730. high_bit = ((ULONGEST) 1) << 31;
  731. signed_type = parse_d_type (ps)->builtin_int;
  732. /* For decimal notation, keep the sign of the worked out type. */
  733. if (base == 10 && !unsigned_p)
  734. unsigned_type = parse_d_type (ps)->builtin_long;
  735. else
  736. unsigned_type = parse_d_type (ps)->builtin_uint;
  737. }
  738. else
  739. {
  740. int shift;
  741. if (sizeof (ULONGEST) * HOST_CHAR_BIT < 64)
  742. /* A long long does not fit in a LONGEST. */
  743. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  744. else
  745. shift = 63;
  746. high_bit = (ULONGEST) 1 << shift;
  747. signed_type = parse_d_type (ps)->builtin_long;
  748. unsigned_type = parse_d_type (ps)->builtin_ulong;
  749. }
  750. putithere->typed_val_int.val = n;
  751. /* If the high bit of the worked out type is set then this number
  752. has to be unsigned_type. */
  753. if (unsigned_p || (n & high_bit))
  754. putithere->typed_val_int.type = unsigned_type;
  755. else
  756. putithere->typed_val_int.type = signed_type;
  757. return INTEGER_LITERAL;
  758. }
  759. /* Temporary obstack used for holding strings. */
  760. static struct obstack tempbuf;
  761. static int tempbuf_init;
  762. /* Parse a string or character literal from TOKPTR. The string or
  763. character may be wide or unicode. *OUTPTR is set to just after the
  764. end of the literal in the input string. The resulting token is
  765. stored in VALUE. This returns a token value, either STRING or
  766. CHAR, depending on what was parsed. *HOST_CHARS is set to the
  767. number of host characters in the literal. */
  768. static int
  769. parse_string_or_char (const char *tokptr, const char **outptr,
  770. struct typed_stoken *value, int *host_chars)
  771. {
  772. int quote;
  773. /* Build the gdb internal form of the input string in tempbuf. Note
  774. that the buffer is null byte terminated *only* for the
  775. convenience of debugging gdb itself and printing the buffer
  776. contents when the buffer contains no embedded nulls. Gdb does
  777. not depend upon the buffer being null byte terminated, it uses
  778. the length string instead. This allows gdb to handle C strings
  779. (as well as strings in other languages) with embedded null
  780. bytes */
  781. if (!tempbuf_init)
  782. tempbuf_init = 1;
  783. else
  784. obstack_free (&tempbuf, NULL);
  785. obstack_init (&tempbuf);
  786. /* Skip the quote. */
  787. quote = *tokptr;
  788. ++tokptr;
  789. *host_chars = 0;
  790. while (*tokptr)
  791. {
  792. char c = *tokptr;
  793. if (c == '\\')
  794. {
  795. ++tokptr;
  796. *host_chars += c_parse_escape (&tokptr, &tempbuf);
  797. }
  798. else if (c == quote)
  799. break;
  800. else
  801. {
  802. obstack_1grow (&tempbuf, c);
  803. ++tokptr;
  804. /* FIXME: this does the wrong thing with multi-byte host
  805. characters. We could use mbrlen here, but that would
  806. make "set host-charset" a bit less useful. */
  807. ++*host_chars;
  808. }
  809. }
  810. if (*tokptr != quote)
  811. {
  812. if (quote == '"' || quote == '`')
  813. error (_("Unterminated string in expression."));
  814. else
  815. error (_("Unmatched single quote."));
  816. }
  817. ++tokptr;
  818. /* FIXME: should instead use own language string_type enum
  819. and handle D-specific string suffixes here. */
  820. if (quote == '\'')
  821. value->type = C_CHAR;
  822. else
  823. value->type = C_STRING;
  824. value->ptr = (char *) obstack_base (&tempbuf);
  825. value->length = obstack_object_size (&tempbuf);
  826. *outptr = tokptr;
  827. return quote == '\'' ? CHARACTER_LITERAL : STRING_LITERAL;
  828. }
  829. struct token
  830. {
  831. const char *oper;
  832. int token;
  833. enum exp_opcode opcode;
  834. };
  835. static const struct token tokentab3[] =
  836. {
  837. {"^^=", ASSIGN_MODIFY, BINOP_EXP},
  838. {"<<=", ASSIGN_MODIFY, BINOP_LSH},
  839. {">>=", ASSIGN_MODIFY, BINOP_RSH},
  840. };
  841. static const struct token tokentab2[] =
  842. {
  843. {"+=", ASSIGN_MODIFY, BINOP_ADD},
  844. {"-=", ASSIGN_MODIFY, BINOP_SUB},
  845. {"*=", ASSIGN_MODIFY, BINOP_MUL},
  846. {"/=", ASSIGN_MODIFY, BINOP_DIV},
  847. {"%=", ASSIGN_MODIFY, BINOP_REM},
  848. {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
  849. {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
  850. {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
  851. {"++", INCREMENT, OP_NULL},
  852. {"--", DECREMENT, OP_NULL},
  853. {"&&", ANDAND, OP_NULL},
  854. {"||", OROR, OP_NULL},
  855. {"^^", HATHAT, OP_NULL},
  856. {"<<", LSH, OP_NULL},
  857. {">>", RSH, OP_NULL},
  858. {"==", EQUAL, OP_NULL},
  859. {"!=", NOTEQUAL, OP_NULL},
  860. {"<=", LEQ, OP_NULL},
  861. {">=", GEQ, OP_NULL},
  862. {"..", DOTDOT, OP_NULL},
  863. };
  864. /* Identifier-like tokens. */
  865. static const struct token ident_tokens[] =
  866. {
  867. {"is", IDENTITY, OP_NULL},
  868. {"!is", NOTIDENTITY, OP_NULL},
  869. {"cast", CAST_KEYWORD, OP_NULL},
  870. {"const", CONST_KEYWORD, OP_NULL},
  871. {"immutable", IMMUTABLE_KEYWORD, OP_NULL},
  872. {"shared", SHARED_KEYWORD, OP_NULL},
  873. {"super", SUPER_KEYWORD, OP_NULL},
  874. {"null", NULL_KEYWORD, OP_NULL},
  875. {"true", TRUE_KEYWORD, OP_NULL},
  876. {"false", FALSE_KEYWORD, OP_NULL},
  877. {"init", INIT_KEYWORD, OP_NULL},
  878. {"sizeof", SIZEOF_KEYWORD, OP_NULL},
  879. {"typeof", TYPEOF_KEYWORD, OP_NULL},
  880. {"typeid", TYPEID_KEYWORD, OP_NULL},
  881. {"delegate", DELEGATE_KEYWORD, OP_NULL},
  882. {"function", FUNCTION_KEYWORD, OP_NULL},
  883. {"struct", STRUCT_KEYWORD, OP_NULL},
  884. {"union", UNION_KEYWORD, OP_NULL},
  885. {"class", CLASS_KEYWORD, OP_NULL},
  886. {"interface", INTERFACE_KEYWORD, OP_NULL},
  887. {"enum", ENUM_KEYWORD, OP_NULL},
  888. {"template", TEMPLATE_KEYWORD, OP_NULL},
  889. };
  890. /* This is set if a NAME token appeared at the very end of the input
  891. string, with no whitespace separating the name from the EOF. This
  892. is used only when parsing to do field name completion. */
  893. static int saw_name_at_eof;
  894. /* This is set if the previously-returned token was a structure operator.
  895. This is used only when parsing to do field name completion. */
  896. static int last_was_structop;
  897. /* Depth of parentheses. */
  898. static int paren_depth;
  899. /* Read one token, getting characters through lexptr. */
  900. static int
  901. lex_one_token (struct parser_state *par_state)
  902. {
  903. int c;
  904. int namelen;
  905. const char *tokstart;
  906. int saw_structop = last_was_structop;
  907. last_was_structop = 0;
  908. retry:
  909. pstate->prev_lexptr = pstate->lexptr;
  910. tokstart = pstate->lexptr;
  911. /* See if it is a special token of length 3. */
  912. for (const auto &token : tokentab3)
  913. if (strncmp (tokstart, token.oper, 3) == 0)
  914. {
  915. pstate->lexptr += 3;
  916. yylval.opcode = token.opcode;
  917. return token.token;
  918. }
  919. /* See if it is a special token of length 2. */
  920. for (const auto &token : tokentab2)
  921. if (strncmp (tokstart, token.oper, 2) == 0)
  922. {
  923. pstate->lexptr += 2;
  924. yylval.opcode = token.opcode;
  925. return token.token;
  926. }
  927. switch (c = *tokstart)
  928. {
  929. case 0:
  930. /* If we're parsing for field name completion, and the previous
  931. token allows such completion, return a COMPLETE token.
  932. Otherwise, we were already scanning the original text, and
  933. we're really done. */
  934. if (saw_name_at_eof)
  935. {
  936. saw_name_at_eof = 0;
  937. return COMPLETE;
  938. }
  939. else if (saw_structop)
  940. return COMPLETE;
  941. else
  942. return 0;
  943. case ' ':
  944. case '\t':
  945. case '\n':
  946. pstate->lexptr++;
  947. goto retry;
  948. case '[':
  949. case '(':
  950. paren_depth++;
  951. pstate->lexptr++;
  952. return c;
  953. case ']':
  954. case ')':
  955. if (paren_depth == 0)
  956. return 0;
  957. paren_depth--;
  958. pstate->lexptr++;
  959. return c;
  960. case ',':
  961. if (pstate->comma_terminates && paren_depth == 0)
  962. return 0;
  963. pstate->lexptr++;
  964. return c;
  965. case '.':
  966. /* Might be a floating point number. */
  967. if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
  968. {
  969. if (pstate->parse_completion)
  970. last_was_structop = 1;
  971. goto symbol; /* Nope, must be a symbol. */
  972. }
  973. /* FALL THRU. */
  974. case '0':
  975. case '1':
  976. case '2':
  977. case '3':
  978. case '4':
  979. case '5':
  980. case '6':
  981. case '7':
  982. case '8':
  983. case '9':
  984. {
  985. /* It's a number. */
  986. int got_dot = 0, got_e = 0, toktype;
  987. const char *p = tokstart;
  988. int hex = input_radix > 10;
  989. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  990. {
  991. p += 2;
  992. hex = 1;
  993. }
  994. for (;; ++p)
  995. {
  996. /* Hex exponents start with 'p', because 'e' is a valid hex
  997. digit and thus does not indicate a floating point number
  998. when the radix is hex. */
  999. if ((!hex && !got_e && tolower (p[0]) == 'e')
  1000. || (hex && !got_e && tolower (p[0] == 'p')))
  1001. got_dot = got_e = 1;
  1002. /* A '.' always indicates a decimal floating point number
  1003. regardless of the radix. If we have a '..' then its the
  1004. end of the number and the beginning of a slice. */
  1005. else if (!got_dot && (p[0] == '.' && p[1] != '.'))
  1006. got_dot = 1;
  1007. /* This is the sign of the exponent, not the end of the number. */
  1008. else if (got_e && (tolower (p[-1]) == 'e' || tolower (p[-1]) == 'p')
  1009. && (*p == '-' || *p == '+'))
  1010. continue;
  1011. /* We will take any letters or digits, ignoring any embedded '_'.
  1012. parse_number will complain if past the radix, or if L or U are
  1013. not final. */
  1014. else if ((*p < '0' || *p > '9') && (*p != '_')
  1015. && ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z')))
  1016. break;
  1017. }
  1018. toktype = parse_number (par_state, tokstart, p - tokstart,
  1019. got_dot|got_e, &yylval);
  1020. if (toktype == ERROR)
  1021. {
  1022. char *err_copy = (char *) alloca (p - tokstart + 1);
  1023. memcpy (err_copy, tokstart, p - tokstart);
  1024. err_copy[p - tokstart] = 0;
  1025. error (_("Invalid number \"%s\"."), err_copy);
  1026. }
  1027. pstate->lexptr = p;
  1028. return toktype;
  1029. }
  1030. case '@':
  1031. {
  1032. const char *p = &tokstart[1];
  1033. size_t len = strlen ("entry");
  1034. while (isspace (*p))
  1035. p++;
  1036. if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
  1037. && p[len] != '_')
  1038. {
  1039. pstate->lexptr = &p[len];
  1040. return ENTRY;
  1041. }
  1042. }
  1043. /* FALLTHRU */
  1044. case '+':
  1045. case '-':
  1046. case '*':
  1047. case '/':
  1048. case '%':
  1049. case '|':
  1050. case '&':
  1051. case '^':
  1052. case '~':
  1053. case '!':
  1054. case '<':
  1055. case '>':
  1056. case '?':
  1057. case ':':
  1058. case '=':
  1059. case '{':
  1060. case '}':
  1061. symbol:
  1062. pstate->lexptr++;
  1063. return c;
  1064. case '\'':
  1065. case '"':
  1066. case '`':
  1067. {
  1068. int host_len;
  1069. int result = parse_string_or_char (tokstart, &pstate->lexptr,
  1070. &yylval.tsval, &host_len);
  1071. if (result == CHARACTER_LITERAL)
  1072. {
  1073. if (host_len == 0)
  1074. error (_("Empty character constant."));
  1075. else if (host_len > 2 && c == '\'')
  1076. {
  1077. ++tokstart;
  1078. namelen = pstate->lexptr - tokstart - 1;
  1079. goto tryname;
  1080. }
  1081. else if (host_len > 1)
  1082. error (_("Invalid character constant."));
  1083. }
  1084. return result;
  1085. }
  1086. }
  1087. if (!(c == '_' || c == '$'
  1088. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1089. /* We must have come across a bad character (e.g. ';'). */
  1090. error (_("Invalid character '%c' in expression"), c);
  1091. /* It's a name. See how long it is. */
  1092. namelen = 0;
  1093. for (c = tokstart[namelen];
  1094. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1095. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
  1096. c = tokstart[++namelen];
  1097. /* The token "if" terminates the expression and is NOT
  1098. removed from the input stream. */
  1099. if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1100. return 0;
  1101. /* For the same reason (breakpoint conditions), "thread N"
  1102. terminates the expression. "thread" could be an identifier, but
  1103. an identifier is never followed by a number without intervening
  1104. punctuation. "task" is similar. Handle abbreviations of these,
  1105. similarly to breakpoint.c:find_condition_and_thread. */
  1106. if (namelen >= 1
  1107. && (strncmp (tokstart, "thread", namelen) == 0
  1108. || strncmp (tokstart, "task", namelen) == 0)
  1109. && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
  1110. {
  1111. const char *p = tokstart + namelen + 1;
  1112. while (*p == ' ' || *p == '\t')
  1113. p++;
  1114. if (*p >= '0' && *p <= '9')
  1115. return 0;
  1116. }
  1117. pstate->lexptr += namelen;
  1118. tryname:
  1119. yylval.sval.ptr = tokstart;
  1120. yylval.sval.length = namelen;
  1121. /* Catch specific keywords. */
  1122. std::string copy = copy_name (yylval.sval);
  1123. for (const auto &token : ident_tokens)
  1124. if (copy == token.oper)
  1125. {
  1126. /* It is ok to always set this, even though we don't always
  1127. strictly need to. */
  1128. yylval.opcode = token.opcode;
  1129. return token.token;
  1130. }
  1131. if (*tokstart == '$')
  1132. return DOLLAR_VARIABLE;
  1133. yylval.tsym.type
  1134. = language_lookup_primitive_type (par_state->language (),
  1135. par_state->gdbarch (), copy.c_str ());
  1136. if (yylval.tsym.type != NULL)
  1137. return TYPENAME;
  1138. /* Input names that aren't symbols but ARE valid hex numbers,
  1139. when the input radix permits them, can be names or numbers
  1140. depending on the parse. Note we support radixes > 16 here. */
  1141. if ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1142. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))
  1143. {
  1144. YYSTYPE newlval; /* Its value is ignored. */
  1145. int hextype = parse_number (par_state, tokstart, namelen, 0, &newlval);
  1146. if (hextype == INTEGER_LITERAL)
  1147. return NAME_OR_INT;
  1148. }
  1149. if (pstate->parse_completion && *pstate->lexptr == '\0')
  1150. saw_name_at_eof = 1;
  1151. return IDENTIFIER;
  1152. }
  1153. /* An object of this type is pushed on a FIFO by the "outer" lexer. */
  1154. struct token_and_value
  1155. {
  1156. int token;
  1157. YYSTYPE value;
  1158. };
  1159. /* A FIFO of tokens that have been read but not yet returned to the
  1160. parser. */
  1161. static std::vector<token_and_value> token_fifo;
  1162. /* Non-zero if the lexer should return tokens from the FIFO. */
  1163. static int popping;
  1164. /* Temporary storage for yylex; this holds symbol names as they are
  1165. built up. */
  1166. static auto_obstack name_obstack;
  1167. /* Classify an IDENTIFIER token. The contents of the token are in `yylval'.
  1168. Updates yylval and returns the new token type. BLOCK is the block
  1169. in which lookups start; this can be NULL to mean the global scope. */
  1170. static int
  1171. classify_name (struct parser_state *par_state, const struct block *block)
  1172. {
  1173. struct block_symbol sym;
  1174. struct field_of_this_result is_a_field_of_this;
  1175. std::string copy = copy_name (yylval.sval);
  1176. sym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN, &is_a_field_of_this);
  1177. if (sym.symbol && sym.symbol->aclass () == LOC_TYPEDEF)
  1178. {
  1179. yylval.tsym.type = sym.symbol->type ();
  1180. return TYPENAME;
  1181. }
  1182. else if (sym.symbol == NULL)
  1183. {
  1184. /* Look-up first for a module name, then a type. */
  1185. sym = lookup_symbol (copy.c_str (), block, MODULE_DOMAIN, NULL);
  1186. if (sym.symbol == NULL)
  1187. sym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN, NULL);
  1188. if (sym.symbol != NULL)
  1189. {
  1190. yylval.tsym.type = sym.symbol->type ();
  1191. return TYPENAME;
  1192. }
  1193. return UNKNOWN_NAME;
  1194. }
  1195. return IDENTIFIER;
  1196. }
  1197. /* Like classify_name, but used by the inner loop of the lexer, when a
  1198. name might have already been seen. CONTEXT is the context type, or
  1199. NULL if this is the first component of a name. */
  1200. static int
  1201. classify_inner_name (struct parser_state *par_state,
  1202. const struct block *block, struct type *context)
  1203. {
  1204. struct type *type;
  1205. if (context == NULL)
  1206. return classify_name (par_state, block);
  1207. type = check_typedef (context);
  1208. if (!type_aggregate_p (type))
  1209. return ERROR;
  1210. std::string copy = copy_name (yylval.ssym.stoken);
  1211. yylval.ssym.sym = d_lookup_nested_symbol (type, copy.c_str (), block);
  1212. if (yylval.ssym.sym.symbol == NULL)
  1213. return ERROR;
  1214. if (yylval.ssym.sym.symbol->aclass () == LOC_TYPEDEF)
  1215. {
  1216. yylval.tsym.type = yylval.ssym.sym.symbol->type ();
  1217. return TYPENAME;
  1218. }
  1219. return IDENTIFIER;
  1220. }
  1221. /* The outer level of a two-level lexer. This calls the inner lexer
  1222. to return tokens. It then either returns these tokens, or
  1223. aggregates them into a larger token. This lets us work around a
  1224. problem in our parsing approach, where the parser could not
  1225. distinguish between qualified names and qualified types at the
  1226. right point. */
  1227. static int
  1228. yylex (void)
  1229. {
  1230. token_and_value current;
  1231. int last_was_dot;
  1232. struct type *context_type = NULL;
  1233. int last_to_examine, next_to_examine, checkpoint;
  1234. const struct block *search_block;
  1235. if (popping && !token_fifo.empty ())
  1236. goto do_pop;
  1237. popping = 0;
  1238. /* Read the first token and decide what to do. */
  1239. current.token = lex_one_token (pstate);
  1240. if (current.token != IDENTIFIER && current.token != '.')
  1241. return current.token;
  1242. /* Read any sequence of alternating "." and identifier tokens into
  1243. the token FIFO. */
  1244. current.value = yylval;
  1245. token_fifo.push_back (current);
  1246. last_was_dot = current.token == '.';
  1247. while (1)
  1248. {
  1249. current.token = lex_one_token (pstate);
  1250. current.value = yylval;
  1251. token_fifo.push_back (current);
  1252. if ((last_was_dot && current.token != IDENTIFIER)
  1253. || (!last_was_dot && current.token != '.'))
  1254. break;
  1255. last_was_dot = !last_was_dot;
  1256. }
  1257. popping = 1;
  1258. /* We always read one extra token, so compute the number of tokens
  1259. to examine accordingly. */
  1260. last_to_examine = token_fifo.size () - 2;
  1261. next_to_examine = 0;
  1262. current = token_fifo[next_to_examine];
  1263. ++next_to_examine;
  1264. /* If we are not dealing with a typename, now is the time to find out. */
  1265. if (current.token == IDENTIFIER)
  1266. {
  1267. yylval = current.value;
  1268. current.token = classify_name (pstate, pstate->expression_context_block);
  1269. current.value = yylval;
  1270. }
  1271. /* If the IDENTIFIER is not known, it could be a package symbol,
  1272. first try building up a name until we find the qualified module. */
  1273. if (current.token == UNKNOWN_NAME)
  1274. {
  1275. name_obstack.clear ();
  1276. obstack_grow (&name_obstack, current.value.sval.ptr,
  1277. current.value.sval.length);
  1278. last_was_dot = 0;
  1279. while (next_to_examine <= last_to_examine)
  1280. {
  1281. token_and_value next;
  1282. next = token_fifo[next_to_examine];
  1283. ++next_to_examine;
  1284. if (next.token == IDENTIFIER && last_was_dot)
  1285. {
  1286. /* Update the partial name we are constructing. */
  1287. obstack_grow_str (&name_obstack, ".");
  1288. obstack_grow (&name_obstack, next.value.sval.ptr,
  1289. next.value.sval.length);
  1290. yylval.sval.ptr = (char *) obstack_base (&name_obstack);
  1291. yylval.sval.length = obstack_object_size (&name_obstack);
  1292. current.token = classify_name (pstate,
  1293. pstate->expression_context_block);
  1294. current.value = yylval;
  1295. /* We keep going until we find a TYPENAME. */
  1296. if (current.token == TYPENAME)
  1297. {
  1298. /* Install it as the first token in the FIFO. */
  1299. token_fifo[0] = current;
  1300. token_fifo.erase (token_fifo.begin () + 1,
  1301. token_fifo.begin () + next_to_examine);
  1302. break;
  1303. }
  1304. }
  1305. else if (next.token == '.' && !last_was_dot)
  1306. last_was_dot = 1;
  1307. else
  1308. {
  1309. /* We've reached the end of the name. */
  1310. break;
  1311. }
  1312. }
  1313. /* Reset our current token back to the start, if we found nothing
  1314. this means that we will just jump to do pop. */
  1315. current = token_fifo[0];
  1316. next_to_examine = 1;
  1317. }
  1318. if (current.token != TYPENAME && current.token != '.')
  1319. goto do_pop;
  1320. name_obstack.clear ();
  1321. checkpoint = 0;
  1322. if (current.token == '.')
  1323. search_block = NULL;
  1324. else
  1325. {
  1326. gdb_assert (current.token == TYPENAME);
  1327. search_block = pstate->expression_context_block;
  1328. obstack_grow (&name_obstack, current.value.sval.ptr,
  1329. current.value.sval.length);
  1330. context_type = current.value.tsym.type;
  1331. checkpoint = 1;
  1332. }
  1333. last_was_dot = current.token == '.';
  1334. while (next_to_examine <= last_to_examine)
  1335. {
  1336. token_and_value next;
  1337. next = token_fifo[next_to_examine];
  1338. ++next_to_examine;
  1339. if (next.token == IDENTIFIER && last_was_dot)
  1340. {
  1341. int classification;
  1342. yylval = next.value;
  1343. classification = classify_inner_name (pstate, search_block,
  1344. context_type);
  1345. /* We keep going until we either run out of names, or until
  1346. we have a qualified name which is not a type. */
  1347. if (classification != TYPENAME && classification != IDENTIFIER)
  1348. break;
  1349. /* Accept up to this token. */
  1350. checkpoint = next_to_examine;
  1351. /* Update the partial name we are constructing. */
  1352. if (context_type != NULL)
  1353. {
  1354. /* We don't want to put a leading "." into the name. */
  1355. obstack_grow_str (&name_obstack, ".");
  1356. }
  1357. obstack_grow (&name_obstack, next.value.sval.ptr,
  1358. next.value.sval.length);
  1359. yylval.sval.ptr = (char *) obstack_base (&name_obstack);
  1360. yylval.sval.length = obstack_object_size (&name_obstack);
  1361. current.value = yylval;
  1362. current.token = classification;
  1363. last_was_dot = 0;
  1364. if (classification == IDENTIFIER)
  1365. break;
  1366. context_type = yylval.tsym.type;
  1367. }
  1368. else if (next.token == '.' && !last_was_dot)
  1369. last_was_dot = 1;
  1370. else
  1371. {
  1372. /* We've reached the end of the name. */
  1373. break;
  1374. }
  1375. }
  1376. /* If we have a replacement token, install it as the first token in
  1377. the FIFO, and delete the other constituent tokens. */
  1378. if (checkpoint > 0)
  1379. {
  1380. token_fifo[0] = current;
  1381. if (checkpoint > 1)
  1382. token_fifo.erase (token_fifo.begin () + 1,
  1383. token_fifo.begin () + checkpoint);
  1384. }
  1385. do_pop:
  1386. current = token_fifo[0];
  1387. token_fifo.erase (token_fifo.begin ());
  1388. yylval = current.value;
  1389. return current.token;
  1390. }
  1391. int
  1392. d_parse (struct parser_state *par_state)
  1393. {
  1394. /* Setting up the parser state. */
  1395. scoped_restore pstate_restore = make_scoped_restore (&pstate);
  1396. gdb_assert (par_state != NULL);
  1397. pstate = par_state;
  1398. scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
  1399. parser_debug);
  1400. struct type_stack stack;
  1401. scoped_restore restore_type_stack = make_scoped_restore (&type_stack,
  1402. &stack);
  1403. /* Initialize some state used by the lexer. */
  1404. last_was_structop = 0;
  1405. saw_name_at_eof = 0;
  1406. paren_depth = 0;
  1407. token_fifo.clear ();
  1408. popping = 0;
  1409. name_obstack.clear ();
  1410. int result = yyparse ();
  1411. if (!result)
  1412. pstate->set_operation (pstate->pop ());
  1413. return result;
  1414. }
  1415. static void
  1416. yyerror (const char *msg)
  1417. {
  1418. if (pstate->prev_lexptr)
  1419. pstate->lexptr = pstate->prev_lexptr;
  1420. error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
  1421. }