f-exp.y 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /* YACC parser for Fortran expressions, for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. Contributed by Motorola. Adapted from the C parser by Farooq Butt
  4. (fmbutt@engage.sps.mot.com).
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* This was blantantly ripped off the C expression parser, please
  17. be aware of that as you look at its basic structure -FMB */
  18. /* Parse a F77 expression from text in a string,
  19. and return the result as a struct expression pointer.
  20. That structure contains arithmetic operations in reverse polish,
  21. with constants represented by operations that are followed by special data.
  22. See expression.h for the details of the format.
  23. What is important here is that it can be built up sequentially
  24. during the process of parsing; the lower levels of the tree always
  25. come first in the result.
  26. Note that malloc's and realloc's in this file are transformed to
  27. xmalloc and xrealloc respectively by the same sed command in the
  28. makefile that remaps any other malloc/realloc inserted by the parser
  29. generator. Doing this with #defines and trying to control the interaction
  30. with include files (<malloc.h> and <stdlib.h> for example) just became
  31. too messy, particularly when such includes can be inserted at random
  32. times by the parser generator. */
  33. %{
  34. #include "defs.h"
  35. #include "expression.h"
  36. #include "value.h"
  37. #include "parser-defs.h"
  38. #include "language.h"
  39. #include "f-lang.h"
  40. #include "bfd.h" /* Required by objfiles.h. */
  41. #include "symfile.h" /* Required by objfiles.h. */
  42. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  43. #include "block.h"
  44. #include <ctype.h>
  45. #include <algorithm>
  46. #include "type-stack.h"
  47. #include "f-exp.h"
  48. #define parse_type(ps) builtin_type (ps->gdbarch ())
  49. #define parse_f_type(ps) builtin_f_type (ps->gdbarch ())
  50. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
  51. etc). */
  52. #define GDB_YY_REMAP_PREFIX f_
  53. #include "yy-remap.h"
  54. /* The state of the parser, used internally when we are parsing the
  55. expression. */
  56. static struct parser_state *pstate = NULL;
  57. /* Depth of parentheses. */
  58. static int paren_depth;
  59. /* The current type stack. */
  60. static struct type_stack *type_stack;
  61. int yyparse (void);
  62. static int yylex (void);
  63. static void yyerror (const char *);
  64. static void growbuf_by_size (int);
  65. static int match_string_literal (void);
  66. static void push_kind_type (LONGEST val, struct type *type);
  67. static struct type *convert_to_kind_type (struct type *basetype, int kind);
  68. using namespace expr;
  69. %}
  70. /* Although the yacc "value" of an expression is not used,
  71. since the result is stored in the structure being created,
  72. other node types do have values. */
  73. %union
  74. {
  75. LONGEST lval;
  76. struct {
  77. LONGEST val;
  78. struct type *type;
  79. } typed_val;
  80. struct {
  81. gdb_byte val[16];
  82. struct type *type;
  83. } typed_val_float;
  84. struct symbol *sym;
  85. struct type *tval;
  86. struct stoken sval;
  87. struct ttype tsym;
  88. struct symtoken ssym;
  89. int voidval;
  90. enum exp_opcode opcode;
  91. struct internalvar *ivar;
  92. struct type **tvec;
  93. int *ivec;
  94. }
  95. %{
  96. /* YYSTYPE gets defined by %union */
  97. static int parse_number (struct parser_state *, const char *, int,
  98. int, YYSTYPE *);
  99. %}
  100. %type <voidval> exp type_exp start variable
  101. %type <tval> type typebase
  102. %type <tvec> nonempty_typelist
  103. /* %type <bval> block */
  104. /* Fancy type parsing. */
  105. %type <voidval> func_mod direct_abs_decl abs_decl
  106. %type <tval> ptype
  107. %token <typed_val> INT
  108. %token <typed_val_float> FLOAT
  109. /* Both NAME and TYPENAME tokens represent symbols in the input,
  110. and both convey their data as strings.
  111. But a TYPENAME is a string that happens to be defined as a typedef
  112. or builtin type name (such as int or char)
  113. and a NAME is any other symbol.
  114. Contexts where this distinction is not important can use the
  115. nonterminal "name", which matches either NAME or TYPENAME. */
  116. %token <sval> STRING_LITERAL
  117. %token <lval> BOOLEAN_LITERAL
  118. %token <ssym> NAME
  119. %token <tsym> TYPENAME
  120. %token <voidval> COMPLETE
  121. %type <sval> name
  122. %type <ssym> name_not_typename
  123. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  124. but which would parse as a valid number in the current input radix.
  125. E.g. "c" when input_radix==16. Depending on the parse, it will be
  126. turned into a name or into a number. */
  127. %token <ssym> NAME_OR_INT
  128. %token SIZEOF KIND
  129. %token ERROR
  130. /* Special type cases, put in to allow the parser to distinguish different
  131. legal basetypes. */
  132. %token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD
  133. %token LOGICAL_S8_KEYWORD
  134. %token LOGICAL_KEYWORD REAL_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
  135. %token COMPLEX_KEYWORD
  136. %token COMPLEX_S8_KEYWORD COMPLEX_S16_KEYWORD COMPLEX_S32_KEYWORD
  137. %token BOOL_AND BOOL_OR BOOL_NOT
  138. %token SINGLE DOUBLE PRECISION
  139. %token <lval> CHARACTER
  140. %token <sval> DOLLAR_VARIABLE
  141. %token <opcode> ASSIGN_MODIFY
  142. %token <opcode> UNOP_INTRINSIC BINOP_INTRINSIC
  143. %token <opcode> UNOP_OR_BINOP_INTRINSIC
  144. %left ','
  145. %left ABOVE_COMMA
  146. %right '=' ASSIGN_MODIFY
  147. %right '?'
  148. %left BOOL_OR
  149. %right BOOL_NOT
  150. %left BOOL_AND
  151. %left '|'
  152. %left '^'
  153. %left '&'
  154. %left EQUAL NOTEQUAL
  155. %left LESSTHAN GREATERTHAN LEQ GEQ
  156. %left LSH RSH
  157. %left '@'
  158. %left '+' '-'
  159. %left '*' '/'
  160. %right STARSTAR
  161. %right '%'
  162. %right UNARY
  163. %right '('
  164. %%
  165. start : exp
  166. | type_exp
  167. ;
  168. type_exp: type
  169. { pstate->push_new<type_operation> ($1); }
  170. ;
  171. exp : '(' exp ')'
  172. { }
  173. ;
  174. /* Expressions, not including the comma operator. */
  175. exp : '*' exp %prec UNARY
  176. { pstate->wrap<unop_ind_operation> (); }
  177. ;
  178. exp : '&' exp %prec UNARY
  179. { pstate->wrap<unop_addr_operation> (); }
  180. ;
  181. exp : '-' exp %prec UNARY
  182. { pstate->wrap<unary_neg_operation> (); }
  183. ;
  184. exp : BOOL_NOT exp %prec UNARY
  185. { pstate->wrap<unary_logical_not_operation> (); }
  186. ;
  187. exp : '~' exp %prec UNARY
  188. { pstate->wrap<unary_complement_operation> (); }
  189. ;
  190. exp : SIZEOF exp %prec UNARY
  191. { pstate->wrap<unop_sizeof_operation> (); }
  192. ;
  193. exp : KIND '(' exp ')' %prec UNARY
  194. { pstate->wrap<fortran_kind_operation> (); }
  195. ;
  196. exp : UNOP_OR_BINOP_INTRINSIC '('
  197. { pstate->start_arglist (); }
  198. one_or_two_args ')'
  199. {
  200. int n = pstate->end_arglist ();
  201. gdb_assert (n == 1 || n == 2);
  202. if ($1 == FORTRAN_ASSOCIATED)
  203. {
  204. if (n == 1)
  205. pstate->wrap<fortran_associated_1arg> ();
  206. else
  207. pstate->wrap2<fortran_associated_2arg> ();
  208. }
  209. else if ($1 == FORTRAN_ARRAY_SIZE)
  210. {
  211. if (n == 1)
  212. pstate->wrap<fortran_array_size_1arg> ();
  213. else
  214. pstate->wrap2<fortran_array_size_2arg> ();
  215. }
  216. else
  217. {
  218. std::vector<operation_up> args
  219. = pstate->pop_vector (n);
  220. gdb_assert ($1 == FORTRAN_LBOUND
  221. || $1 == FORTRAN_UBOUND);
  222. operation_up op;
  223. if (n == 1)
  224. op.reset
  225. (new fortran_bound_1arg ($1,
  226. std::move (args[0])));
  227. else
  228. op.reset
  229. (new fortran_bound_2arg ($1,
  230. std::move (args[0]),
  231. std::move (args[1])));
  232. pstate->push (std::move (op));
  233. }
  234. }
  235. ;
  236. one_or_two_args
  237. : exp
  238. { pstate->arglist_len = 1; }
  239. | exp ',' exp
  240. { pstate->arglist_len = 2; }
  241. ;
  242. /* No more explicit array operators, we treat everything in F77 as
  243. a function call. The disambiguation as to whether we are
  244. doing a subscript operation or a function call is done
  245. later in eval.c. */
  246. exp : exp '('
  247. { pstate->start_arglist (); }
  248. arglist ')'
  249. {
  250. std::vector<operation_up> args
  251. = pstate->pop_vector (pstate->end_arglist ());
  252. pstate->push_new<fortran_undetermined>
  253. (pstate->pop (), std::move (args));
  254. }
  255. ;
  256. exp : UNOP_INTRINSIC '(' exp ')'
  257. {
  258. switch ($1)
  259. {
  260. case UNOP_ABS:
  261. pstate->wrap<fortran_abs_operation> ();
  262. break;
  263. case UNOP_FORTRAN_FLOOR:
  264. pstate->wrap<fortran_floor_operation> ();
  265. break;
  266. case UNOP_FORTRAN_CEILING:
  267. pstate->wrap<fortran_ceil_operation> ();
  268. break;
  269. case UNOP_FORTRAN_ALLOCATED:
  270. pstate->wrap<fortran_allocated_operation> ();
  271. break;
  272. case UNOP_FORTRAN_RANK:
  273. pstate->wrap<fortran_rank_operation> ();
  274. break;
  275. case UNOP_FORTRAN_SHAPE:
  276. pstate->wrap<fortran_array_shape_operation> ();
  277. break;
  278. case UNOP_FORTRAN_LOC:
  279. pstate->wrap<fortran_loc_operation> ();
  280. break;
  281. default:
  282. gdb_assert_not_reached ("unhandled intrinsic");
  283. }
  284. }
  285. ;
  286. exp : BINOP_INTRINSIC '(' exp ',' exp ')'
  287. {
  288. switch ($1)
  289. {
  290. case BINOP_MOD:
  291. pstate->wrap2<fortran_mod_operation> ();
  292. break;
  293. case BINOP_FORTRAN_MODULO:
  294. pstate->wrap2<fortran_modulo_operation> ();
  295. break;
  296. case BINOP_FORTRAN_CMPLX:
  297. pstate->wrap2<fortran_cmplx_operation> ();
  298. break;
  299. default:
  300. gdb_assert_not_reached ("unhandled intrinsic");
  301. }
  302. }
  303. ;
  304. arglist :
  305. ;
  306. arglist : exp
  307. { pstate->arglist_len = 1; }
  308. ;
  309. arglist : subrange
  310. { pstate->arglist_len = 1; }
  311. ;
  312. arglist : arglist ',' exp %prec ABOVE_COMMA
  313. { pstate->arglist_len++; }
  314. ;
  315. arglist : arglist ',' subrange %prec ABOVE_COMMA
  316. { pstate->arglist_len++; }
  317. ;
  318. /* There are four sorts of subrange types in F90. */
  319. subrange: exp ':' exp %prec ABOVE_COMMA
  320. {
  321. operation_up high = pstate->pop ();
  322. operation_up low = pstate->pop ();
  323. pstate->push_new<fortran_range_operation>
  324. (RANGE_STANDARD, std::move (low),
  325. std::move (high), operation_up ());
  326. }
  327. ;
  328. subrange: exp ':' %prec ABOVE_COMMA
  329. {
  330. operation_up low = pstate->pop ();
  331. pstate->push_new<fortran_range_operation>
  332. (RANGE_HIGH_BOUND_DEFAULT, std::move (low),
  333. operation_up (), operation_up ());
  334. }
  335. ;
  336. subrange: ':' exp %prec ABOVE_COMMA
  337. {
  338. operation_up high = pstate->pop ();
  339. pstate->push_new<fortran_range_operation>
  340. (RANGE_LOW_BOUND_DEFAULT, operation_up (),
  341. std::move (high), operation_up ());
  342. }
  343. ;
  344. subrange: ':' %prec ABOVE_COMMA
  345. {
  346. pstate->push_new<fortran_range_operation>
  347. (RANGE_LOW_BOUND_DEFAULT
  348. | RANGE_HIGH_BOUND_DEFAULT,
  349. operation_up (), operation_up (),
  350. operation_up ());
  351. }
  352. ;
  353. /* And each of the four subrange types can also have a stride. */
  354. subrange: exp ':' exp ':' exp %prec ABOVE_COMMA
  355. {
  356. operation_up stride = pstate->pop ();
  357. operation_up high = pstate->pop ();
  358. operation_up low = pstate->pop ();
  359. pstate->push_new<fortran_range_operation>
  360. (RANGE_STANDARD | RANGE_HAS_STRIDE,
  361. std::move (low), std::move (high),
  362. std::move (stride));
  363. }
  364. ;
  365. subrange: exp ':' ':' exp %prec ABOVE_COMMA
  366. {
  367. operation_up stride = pstate->pop ();
  368. operation_up low = pstate->pop ();
  369. pstate->push_new<fortran_range_operation>
  370. (RANGE_HIGH_BOUND_DEFAULT
  371. | RANGE_HAS_STRIDE,
  372. std::move (low), operation_up (),
  373. std::move (stride));
  374. }
  375. ;
  376. subrange: ':' exp ':' exp %prec ABOVE_COMMA
  377. {
  378. operation_up stride = pstate->pop ();
  379. operation_up high = pstate->pop ();
  380. pstate->push_new<fortran_range_operation>
  381. (RANGE_LOW_BOUND_DEFAULT
  382. | RANGE_HAS_STRIDE,
  383. operation_up (), std::move (high),
  384. std::move (stride));
  385. }
  386. ;
  387. subrange: ':' ':' exp %prec ABOVE_COMMA
  388. {
  389. operation_up stride = pstate->pop ();
  390. pstate->push_new<fortran_range_operation>
  391. (RANGE_LOW_BOUND_DEFAULT
  392. | RANGE_HIGH_BOUND_DEFAULT
  393. | RANGE_HAS_STRIDE,
  394. operation_up (), operation_up (),
  395. std::move (stride));
  396. }
  397. ;
  398. complexnum: exp ',' exp
  399. { }
  400. ;
  401. exp : '(' complexnum ')'
  402. {
  403. operation_up rhs = pstate->pop ();
  404. operation_up lhs = pstate->pop ();
  405. pstate->push_new<complex_operation>
  406. (std::move (lhs), std::move (rhs),
  407. parse_f_type (pstate)->builtin_complex_s16);
  408. }
  409. ;
  410. exp : '(' type ')' exp %prec UNARY
  411. {
  412. pstate->push_new<unop_cast_operation>
  413. (pstate->pop (), $2);
  414. }
  415. ;
  416. exp : exp '%' name
  417. {
  418. pstate->push_new<fortran_structop_operation>
  419. (pstate->pop (), copy_name ($3));
  420. }
  421. ;
  422. exp : exp '%' name COMPLETE
  423. {
  424. structop_base_operation *op
  425. = new fortran_structop_operation (pstate->pop (),
  426. copy_name ($3));
  427. pstate->mark_struct_expression (op);
  428. pstate->push (operation_up (op));
  429. }
  430. ;
  431. exp : exp '%' COMPLETE
  432. {
  433. structop_base_operation *op
  434. = new fortran_structop_operation (pstate->pop (),
  435. "");
  436. pstate->mark_struct_expression (op);
  437. pstate->push (operation_up (op));
  438. }
  439. ;
  440. /* Binary operators in order of decreasing precedence. */
  441. exp : exp '@' exp
  442. { pstate->wrap2<repeat_operation> (); }
  443. ;
  444. exp : exp STARSTAR exp
  445. { pstate->wrap2<exp_operation> (); }
  446. ;
  447. exp : exp '*' exp
  448. { pstate->wrap2<mul_operation> (); }
  449. ;
  450. exp : exp '/' exp
  451. { pstate->wrap2<div_operation> (); }
  452. ;
  453. exp : exp '+' exp
  454. { pstate->wrap2<add_operation> (); }
  455. ;
  456. exp : exp '-' exp
  457. { pstate->wrap2<sub_operation> (); }
  458. ;
  459. exp : exp LSH exp
  460. { pstate->wrap2<lsh_operation> (); }
  461. ;
  462. exp : exp RSH exp
  463. { pstate->wrap2<rsh_operation> (); }
  464. ;
  465. exp : exp EQUAL exp
  466. { pstate->wrap2<equal_operation> (); }
  467. ;
  468. exp : exp NOTEQUAL exp
  469. { pstate->wrap2<notequal_operation> (); }
  470. ;
  471. exp : exp LEQ exp
  472. { pstate->wrap2<leq_operation> (); }
  473. ;
  474. exp : exp GEQ exp
  475. { pstate->wrap2<geq_operation> (); }
  476. ;
  477. exp : exp LESSTHAN exp
  478. { pstate->wrap2<less_operation> (); }
  479. ;
  480. exp : exp GREATERTHAN exp
  481. { pstate->wrap2<gtr_operation> (); }
  482. ;
  483. exp : exp '&' exp
  484. { pstate->wrap2<bitwise_and_operation> (); }
  485. ;
  486. exp : exp '^' exp
  487. { pstate->wrap2<bitwise_xor_operation> (); }
  488. ;
  489. exp : exp '|' exp
  490. { pstate->wrap2<bitwise_ior_operation> (); }
  491. ;
  492. exp : exp BOOL_AND exp
  493. { pstate->wrap2<logical_and_operation> (); }
  494. ;
  495. exp : exp BOOL_OR exp
  496. { pstate->wrap2<logical_or_operation> (); }
  497. ;
  498. exp : exp '=' exp
  499. { pstate->wrap2<assign_operation> (); }
  500. ;
  501. exp : exp ASSIGN_MODIFY exp
  502. {
  503. operation_up rhs = pstate->pop ();
  504. operation_up lhs = pstate->pop ();
  505. pstate->push_new<assign_modify_operation>
  506. ($2, std::move (lhs), std::move (rhs));
  507. }
  508. ;
  509. exp : INT
  510. {
  511. pstate->push_new<long_const_operation>
  512. ($1.type, $1.val);
  513. }
  514. ;
  515. exp : NAME_OR_INT
  516. { YYSTYPE val;
  517. parse_number (pstate, $1.stoken.ptr,
  518. $1.stoken.length, 0, &val);
  519. pstate->push_new<long_const_operation>
  520. (val.typed_val.type,
  521. val.typed_val.val);
  522. }
  523. ;
  524. exp : FLOAT
  525. {
  526. float_data data;
  527. std::copy (std::begin ($1.val), std::end ($1.val),
  528. std::begin (data));
  529. pstate->push_new<float_const_operation> ($1.type, data);
  530. }
  531. ;
  532. exp : variable
  533. ;
  534. exp : DOLLAR_VARIABLE
  535. { pstate->push_dollar ($1); }
  536. ;
  537. exp : SIZEOF '(' type ')' %prec UNARY
  538. {
  539. $3 = check_typedef ($3);
  540. pstate->push_new<long_const_operation>
  541. (parse_f_type (pstate)->builtin_integer,
  542. TYPE_LENGTH ($3));
  543. }
  544. ;
  545. exp : BOOLEAN_LITERAL
  546. { pstate->push_new<bool_operation> ($1); }
  547. ;
  548. exp : STRING_LITERAL
  549. {
  550. pstate->push_new<string_operation>
  551. (copy_name ($1));
  552. }
  553. ;
  554. variable: name_not_typename
  555. { struct block_symbol sym = $1.sym;
  556. std::string name = copy_name ($1.stoken);
  557. pstate->push_symbol (name.c_str (), sym);
  558. }
  559. ;
  560. type : ptype
  561. ;
  562. ptype : typebase
  563. | typebase abs_decl
  564. {
  565. /* This is where the interesting stuff happens. */
  566. int done = 0;
  567. int array_size;
  568. struct type *follow_type = $1;
  569. struct type *range_type;
  570. while (!done)
  571. switch (type_stack->pop ())
  572. {
  573. case tp_end:
  574. done = 1;
  575. break;
  576. case tp_pointer:
  577. follow_type = lookup_pointer_type (follow_type);
  578. break;
  579. case tp_reference:
  580. follow_type = lookup_lvalue_reference_type (follow_type);
  581. break;
  582. case tp_array:
  583. array_size = type_stack->pop_int ();
  584. if (array_size != -1)
  585. {
  586. range_type =
  587. create_static_range_type ((struct type *) NULL,
  588. parse_f_type (pstate)
  589. ->builtin_integer,
  590. 0, array_size - 1);
  591. follow_type =
  592. create_array_type ((struct type *) NULL,
  593. follow_type, range_type);
  594. }
  595. else
  596. follow_type = lookup_pointer_type (follow_type);
  597. break;
  598. case tp_function:
  599. follow_type = lookup_function_type (follow_type);
  600. break;
  601. case tp_kind:
  602. {
  603. int kind_val = type_stack->pop_int ();
  604. follow_type
  605. = convert_to_kind_type (follow_type, kind_val);
  606. }
  607. break;
  608. }
  609. $$ = follow_type;
  610. }
  611. ;
  612. abs_decl: '*'
  613. { type_stack->push (tp_pointer); $$ = 0; }
  614. | '*' abs_decl
  615. { type_stack->push (tp_pointer); $$ = $2; }
  616. | '&'
  617. { type_stack->push (tp_reference); $$ = 0; }
  618. | '&' abs_decl
  619. { type_stack->push (tp_reference); $$ = $2; }
  620. | direct_abs_decl
  621. ;
  622. direct_abs_decl: '(' abs_decl ')'
  623. { $$ = $2; }
  624. | '(' KIND '=' INT ')'
  625. { push_kind_type ($4.val, $4.type); }
  626. | '*' INT
  627. { push_kind_type ($2.val, $2.type); }
  628. | direct_abs_decl func_mod
  629. { type_stack->push (tp_function); }
  630. | func_mod
  631. { type_stack->push (tp_function); }
  632. ;
  633. func_mod: '(' ')'
  634. { $$ = 0; }
  635. | '(' nonempty_typelist ')'
  636. { free ($2); $$ = 0; }
  637. ;
  638. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  639. : TYPENAME
  640. { $$ = $1.type; }
  641. | INT_KEYWORD
  642. { $$ = parse_f_type (pstate)->builtin_integer; }
  643. | INT_S2_KEYWORD
  644. { $$ = parse_f_type (pstate)->builtin_integer_s2; }
  645. | CHARACTER
  646. { $$ = parse_f_type (pstate)->builtin_character; }
  647. | LOGICAL_S8_KEYWORD
  648. { $$ = parse_f_type (pstate)->builtin_logical_s8; }
  649. | LOGICAL_KEYWORD
  650. { $$ = parse_f_type (pstate)->builtin_logical; }
  651. | LOGICAL_S2_KEYWORD
  652. { $$ = parse_f_type (pstate)->builtin_logical_s2; }
  653. | LOGICAL_S1_KEYWORD
  654. { $$ = parse_f_type (pstate)->builtin_logical_s1; }
  655. | REAL_KEYWORD
  656. { $$ = parse_f_type (pstate)->builtin_real; }
  657. | REAL_S8_KEYWORD
  658. { $$ = parse_f_type (pstate)->builtin_real_s8; }
  659. | REAL_S16_KEYWORD
  660. { $$ = parse_f_type (pstate)->builtin_real_s16; }
  661. | COMPLEX_KEYWORD
  662. { $$ = parse_f_type (pstate)->builtin_complex_s8; }
  663. | COMPLEX_S8_KEYWORD
  664. { $$ = parse_f_type (pstate)->builtin_complex_s8; }
  665. | COMPLEX_S16_KEYWORD
  666. { $$ = parse_f_type (pstate)->builtin_complex_s16; }
  667. | COMPLEX_S32_KEYWORD
  668. { $$ = parse_f_type (pstate)->builtin_complex_s32; }
  669. | SINGLE PRECISION
  670. { $$ = parse_f_type (pstate)->builtin_real;}
  671. | DOUBLE PRECISION
  672. { $$ = parse_f_type (pstate)->builtin_real_s8;}
  673. | SINGLE COMPLEX_KEYWORD
  674. { $$ = parse_f_type (pstate)->builtin_complex_s8;}
  675. | DOUBLE COMPLEX_KEYWORD
  676. { $$ = parse_f_type (pstate)->builtin_complex_s16;}
  677. ;
  678. nonempty_typelist
  679. : type
  680. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  681. $<ivec>$[0] = 1; /* Number of types in vector */
  682. $$[1] = $1;
  683. }
  684. | nonempty_typelist ',' type
  685. { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  686. $$ = (struct type **) realloc ((char *) $1, len);
  687. $$[$<ivec>$[0]] = $3;
  688. }
  689. ;
  690. name
  691. : NAME
  692. { $$ = $1.stoken; }
  693. | TYPENAME
  694. { $$ = $1.stoken; }
  695. ;
  696. name_not_typename : NAME
  697. /* These would be useful if name_not_typename was useful, but it is just
  698. a fake for "variable", so these cause reduce/reduce conflicts because
  699. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  700. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  701. context where only a name could occur, this might be useful.
  702. | NAME_OR_INT
  703. */
  704. ;
  705. %%
  706. /* Take care of parsing a number (anything that starts with a digit).
  707. Set yylval and return the token type; update lexptr.
  708. LEN is the number of characters in it. */
  709. /*** Needs some error checking for the float case ***/
  710. static int
  711. parse_number (struct parser_state *par_state,
  712. const char *p, int len, int parsed_float, YYSTYPE *putithere)
  713. {
  714. ULONGEST n = 0;
  715. ULONGEST prevn = 0;
  716. int c;
  717. int base = input_radix;
  718. int unsigned_p = 0;
  719. int long_p = 0;
  720. ULONGEST high_bit;
  721. struct type *signed_type;
  722. struct type *unsigned_type;
  723. if (parsed_float)
  724. {
  725. /* It's a float since it contains a point or an exponent. */
  726. /* [dD] is not understood as an exponent by parse_float,
  727. change it to 'e'. */
  728. char *tmp, *tmp2;
  729. tmp = xstrdup (p);
  730. for (tmp2 = tmp; *tmp2; ++tmp2)
  731. if (*tmp2 == 'd' || *tmp2 == 'D')
  732. *tmp2 = 'e';
  733. /* FIXME: Should this use different types? */
  734. putithere->typed_val_float.type = parse_f_type (pstate)->builtin_real_s8;
  735. bool parsed = parse_float (tmp, len,
  736. putithere->typed_val_float.type,
  737. putithere->typed_val_float.val);
  738. free (tmp);
  739. return parsed? FLOAT : ERROR;
  740. }
  741. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  742. if (p[0] == '0' && len > 1)
  743. switch (p[1])
  744. {
  745. case 'x':
  746. case 'X':
  747. if (len >= 3)
  748. {
  749. p += 2;
  750. base = 16;
  751. len -= 2;
  752. }
  753. break;
  754. case 't':
  755. case 'T':
  756. case 'd':
  757. case 'D':
  758. if (len >= 3)
  759. {
  760. p += 2;
  761. base = 10;
  762. len -= 2;
  763. }
  764. break;
  765. default:
  766. base = 8;
  767. break;
  768. }
  769. while (len-- > 0)
  770. {
  771. c = *p++;
  772. if (isupper (c))
  773. c = tolower (c);
  774. if (len == 0 && c == 'l')
  775. long_p = 1;
  776. else if (len == 0 && c == 'u')
  777. unsigned_p = 1;
  778. else
  779. {
  780. int i;
  781. if (c >= '0' && c <= '9')
  782. i = c - '0';
  783. else if (c >= 'a' && c <= 'f')
  784. i = c - 'a' + 10;
  785. else
  786. return ERROR; /* Char not a digit */
  787. if (i >= base)
  788. return ERROR; /* Invalid digit in this base */
  789. n *= base;
  790. n += i;
  791. }
  792. /* Portably test for overflow (only works for nonzero values, so make
  793. a second check for zero). */
  794. if ((prevn >= n) && n != 0)
  795. unsigned_p=1; /* Try something unsigned */
  796. /* If range checking enabled, portably test for unsigned overflow. */
  797. if (RANGE_CHECK && n != 0)
  798. {
  799. if ((unsigned_p && prevn >= n))
  800. range_error (_("Overflow on numeric constant."));
  801. }
  802. prevn = n;
  803. }
  804. /* If the number is too big to be an int, or it's got an l suffix
  805. then it's a long. Work out if this has to be a long by
  806. shifting right and seeing if anything remains, and the
  807. target int size is different to the target long size.
  808. In the expression below, we could have tested
  809. (n >> gdbarch_int_bit (parse_gdbarch))
  810. to see if it was zero,
  811. but too many compilers warn about that, when ints and longs
  812. are the same size. So we shift it twice, with fewer bits
  813. each time, for the same result. */
  814. if ((gdbarch_int_bit (par_state->gdbarch ())
  815. != gdbarch_long_bit (par_state->gdbarch ())
  816. && ((n >> 2)
  817. >> (gdbarch_int_bit (par_state->gdbarch ())-2))) /* Avoid
  818. shift warning */
  819. || long_p)
  820. {
  821. high_bit = ((ULONGEST)1)
  822. << (gdbarch_long_bit (par_state->gdbarch ())-1);
  823. unsigned_type = parse_type (par_state)->builtin_unsigned_long;
  824. signed_type = parse_type (par_state)->builtin_long;
  825. }
  826. else
  827. {
  828. high_bit =
  829. ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
  830. unsigned_type = parse_type (par_state)->builtin_unsigned_int;
  831. signed_type = parse_type (par_state)->builtin_int;
  832. }
  833. putithere->typed_val.val = n;
  834. /* If the high bit of the worked out type is set then this number
  835. has to be unsigned. */
  836. if (unsigned_p || (n & high_bit))
  837. putithere->typed_val.type = unsigned_type;
  838. else
  839. putithere->typed_val.type = signed_type;
  840. return INT;
  841. }
  842. /* Called to setup the type stack when we encounter a '(kind=N)' type
  843. modifier, performs some bounds checking on 'N' and then pushes this to
  844. the type stack followed by the 'tp_kind' marker. */
  845. static void
  846. push_kind_type (LONGEST val, struct type *type)
  847. {
  848. int ival;
  849. if (type->is_unsigned ())
  850. {
  851. ULONGEST uval = static_cast <ULONGEST> (val);
  852. if (uval > INT_MAX)
  853. error (_("kind value out of range"));
  854. ival = static_cast <int> (uval);
  855. }
  856. else
  857. {
  858. if (val > INT_MAX || val < 0)
  859. error (_("kind value out of range"));
  860. ival = static_cast <int> (val);
  861. }
  862. type_stack->push (ival);
  863. type_stack->push (tp_kind);
  864. }
  865. /* Called when a type has a '(kind=N)' modifier after it, for example
  866. 'character(kind=1)'. The BASETYPE is the type described by 'character'
  867. in our example, and KIND is the integer '1'. This function returns a
  868. new type that represents the basetype of a specific kind. */
  869. static struct type *
  870. convert_to_kind_type (struct type *basetype, int kind)
  871. {
  872. if (basetype == parse_f_type (pstate)->builtin_character)
  873. {
  874. /* Character of kind 1 is a special case, this is the same as the
  875. base character type. */
  876. if (kind == 1)
  877. return parse_f_type (pstate)->builtin_character;
  878. }
  879. else if (basetype == parse_f_type (pstate)->builtin_complex_s8)
  880. {
  881. if (kind == 4)
  882. return parse_f_type (pstate)->builtin_complex_s8;
  883. else if (kind == 8)
  884. return parse_f_type (pstate)->builtin_complex_s16;
  885. else if (kind == 16)
  886. return parse_f_type (pstate)->builtin_complex_s32;
  887. }
  888. else if (basetype == parse_f_type (pstate)->builtin_real)
  889. {
  890. if (kind == 4)
  891. return parse_f_type (pstate)->builtin_real;
  892. else if (kind == 8)
  893. return parse_f_type (pstate)->builtin_real_s8;
  894. else if (kind == 16)
  895. return parse_f_type (pstate)->builtin_real_s16;
  896. }
  897. else if (basetype == parse_f_type (pstate)->builtin_logical)
  898. {
  899. if (kind == 1)
  900. return parse_f_type (pstate)->builtin_logical_s1;
  901. else if (kind == 2)
  902. return parse_f_type (pstate)->builtin_logical_s2;
  903. else if (kind == 4)
  904. return parse_f_type (pstate)->builtin_logical;
  905. else if (kind == 8)
  906. return parse_f_type (pstate)->builtin_logical_s8;
  907. }
  908. else if (basetype == parse_f_type (pstate)->builtin_integer)
  909. {
  910. if (kind == 2)
  911. return parse_f_type (pstate)->builtin_integer_s2;
  912. else if (kind == 4)
  913. return parse_f_type (pstate)->builtin_integer;
  914. else if (kind == 8)
  915. return parse_f_type (pstate)->builtin_integer_s8;
  916. }
  917. error (_("unsupported kind %d for type %s"),
  918. kind, TYPE_SAFE_NAME (basetype));
  919. /* Should never get here. */
  920. return nullptr;
  921. }
  922. struct token
  923. {
  924. /* The string to match against. */
  925. const char *oper;
  926. /* The lexer token to return. */
  927. int token;
  928. /* The expression opcode to embed within the token. */
  929. enum exp_opcode opcode;
  930. /* When this is true the string in OPER is matched exactly including
  931. case, when this is false OPER is matched case insensitively. */
  932. bool case_sensitive;
  933. };
  934. /* List of Fortran operators. */
  935. static const struct token fortran_operators[] =
  936. {
  937. { ".and.", BOOL_AND, OP_NULL, false },
  938. { ".or.", BOOL_OR, OP_NULL, false },
  939. { ".not.", BOOL_NOT, OP_NULL, false },
  940. { ".eq.", EQUAL, OP_NULL, false },
  941. { ".eqv.", EQUAL, OP_NULL, false },
  942. { ".neqv.", NOTEQUAL, OP_NULL, false },
  943. { ".xor.", NOTEQUAL, OP_NULL, false },
  944. { "==", EQUAL, OP_NULL, false },
  945. { ".ne.", NOTEQUAL, OP_NULL, false },
  946. { "/=", NOTEQUAL, OP_NULL, false },
  947. { ".le.", LEQ, OP_NULL, false },
  948. { "<=", LEQ, OP_NULL, false },
  949. { ".ge.", GEQ, OP_NULL, false },
  950. { ">=", GEQ, OP_NULL, false },
  951. { ".gt.", GREATERTHAN, OP_NULL, false },
  952. { ">", GREATERTHAN, OP_NULL, false },
  953. { ".lt.", LESSTHAN, OP_NULL, false },
  954. { "<", LESSTHAN, OP_NULL, false },
  955. { "**", STARSTAR, BINOP_EXP, false },
  956. };
  957. /* Holds the Fortran representation of a boolean, and the integer value we
  958. substitute in when one of the matching strings is parsed. */
  959. struct f77_boolean_val
  960. {
  961. /* The string representing a Fortran boolean. */
  962. const char *name;
  963. /* The integer value to replace it with. */
  964. int value;
  965. };
  966. /* The set of Fortran booleans. These are matched case insensitively. */
  967. static const struct f77_boolean_val boolean_values[] =
  968. {
  969. { ".true.", 1 },
  970. { ".false.", 0 }
  971. };
  972. static const struct token f77_keywords[] =
  973. {
  974. /* Historically these have always been lowercase only in GDB. */
  975. { "complex_16", COMPLEX_S16_KEYWORD, OP_NULL, true },
  976. { "complex_32", COMPLEX_S32_KEYWORD, OP_NULL, true },
  977. { "character", CHARACTER, OP_NULL, true },
  978. { "integer_2", INT_S2_KEYWORD, OP_NULL, true },
  979. { "logical_1", LOGICAL_S1_KEYWORD, OP_NULL, true },
  980. { "logical_2", LOGICAL_S2_KEYWORD, OP_NULL, true },
  981. { "logical_8", LOGICAL_S8_KEYWORD, OP_NULL, true },
  982. { "complex_8", COMPLEX_S8_KEYWORD, OP_NULL, true },
  983. { "integer", INT_KEYWORD, OP_NULL, true },
  984. { "logical", LOGICAL_KEYWORD, OP_NULL, true },
  985. { "real_16", REAL_S16_KEYWORD, OP_NULL, true },
  986. { "complex", COMPLEX_KEYWORD, OP_NULL, true },
  987. { "sizeof", SIZEOF, OP_NULL, true },
  988. { "real_8", REAL_S8_KEYWORD, OP_NULL, true },
  989. { "real", REAL_KEYWORD, OP_NULL, true },
  990. { "single", SINGLE, OP_NULL, true },
  991. { "double", DOUBLE, OP_NULL, true },
  992. { "precision", PRECISION, OP_NULL, true },
  993. /* The following correspond to actual functions in Fortran and are case
  994. insensitive. */
  995. { "kind", KIND, OP_NULL, false },
  996. { "abs", UNOP_INTRINSIC, UNOP_ABS, false },
  997. { "mod", BINOP_INTRINSIC, BINOP_MOD, false },
  998. { "floor", UNOP_INTRINSIC, UNOP_FORTRAN_FLOOR, false },
  999. { "ceiling", UNOP_INTRINSIC, UNOP_FORTRAN_CEILING, false },
  1000. { "modulo", BINOP_INTRINSIC, BINOP_FORTRAN_MODULO, false },
  1001. { "cmplx", BINOP_INTRINSIC, BINOP_FORTRAN_CMPLX, false },
  1002. { "lbound", UNOP_OR_BINOP_INTRINSIC, FORTRAN_LBOUND, false },
  1003. { "ubound", UNOP_OR_BINOP_INTRINSIC, FORTRAN_UBOUND, false },
  1004. { "allocated", UNOP_INTRINSIC, UNOP_FORTRAN_ALLOCATED, false },
  1005. { "associated", UNOP_OR_BINOP_INTRINSIC, FORTRAN_ASSOCIATED, false },
  1006. { "rank", UNOP_INTRINSIC, UNOP_FORTRAN_RANK, false },
  1007. { "size", UNOP_OR_BINOP_INTRINSIC, FORTRAN_ARRAY_SIZE, false },
  1008. { "shape", UNOP_INTRINSIC, UNOP_FORTRAN_SHAPE, false },
  1009. { "loc", UNOP_INTRINSIC, UNOP_FORTRAN_LOC, false },
  1010. };
  1011. /* Implementation of a dynamically expandable buffer for processing input
  1012. characters acquired through lexptr and building a value to return in
  1013. yylval. Ripped off from ch-exp.y */
  1014. static char *tempbuf; /* Current buffer contents */
  1015. static int tempbufsize; /* Size of allocated buffer */
  1016. static int tempbufindex; /* Current index into buffer */
  1017. #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
  1018. #define CHECKBUF(size) \
  1019. do { \
  1020. if (tempbufindex + (size) >= tempbufsize) \
  1021. { \
  1022. growbuf_by_size (size); \
  1023. } \
  1024. } while (0);
  1025. /* Grow the static temp buffer if necessary, including allocating the
  1026. first one on demand. */
  1027. static void
  1028. growbuf_by_size (int count)
  1029. {
  1030. int growby;
  1031. growby = std::max (count, GROWBY_MIN_SIZE);
  1032. tempbufsize += growby;
  1033. if (tempbuf == NULL)
  1034. tempbuf = (char *) malloc (tempbufsize);
  1035. else
  1036. tempbuf = (char *) realloc (tempbuf, tempbufsize);
  1037. }
  1038. /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
  1039. string-literals.
  1040. Recognize a string literal. A string literal is a nonzero sequence
  1041. of characters enclosed in matching single quotes, except that
  1042. a single character inside single quotes is a character literal, which
  1043. we reject as a string literal. To embed the terminator character inside
  1044. a string, it is simply doubled (I.E. 'this''is''one''string') */
  1045. static int
  1046. match_string_literal (void)
  1047. {
  1048. const char *tokptr = pstate->lexptr;
  1049. for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
  1050. {
  1051. CHECKBUF (1);
  1052. if (*tokptr == *pstate->lexptr)
  1053. {
  1054. if (*(tokptr + 1) == *pstate->lexptr)
  1055. tokptr++;
  1056. else
  1057. break;
  1058. }
  1059. tempbuf[tempbufindex++] = *tokptr;
  1060. }
  1061. if (*tokptr == '\0' /* no terminator */
  1062. || tempbufindex == 0) /* no string */
  1063. return 0;
  1064. else
  1065. {
  1066. tempbuf[tempbufindex] = '\0';
  1067. yylval.sval.ptr = tempbuf;
  1068. yylval.sval.length = tempbufindex;
  1069. pstate->lexptr = ++tokptr;
  1070. return STRING_LITERAL;
  1071. }
  1072. }
  1073. /* This is set if a NAME token appeared at the very end of the input
  1074. string, with no whitespace separating the name from the EOF. This
  1075. is used only when parsing to do field name completion. */
  1076. static bool saw_name_at_eof;
  1077. /* This is set if the previously-returned token was a structure
  1078. operator '%'. */
  1079. static bool last_was_structop;
  1080. /* Read one token, getting characters through lexptr. */
  1081. static int
  1082. yylex (void)
  1083. {
  1084. int c;
  1085. int namelen;
  1086. unsigned int token;
  1087. const char *tokstart;
  1088. bool saw_structop = last_was_structop;
  1089. last_was_structop = false;
  1090. retry:
  1091. pstate->prev_lexptr = pstate->lexptr;
  1092. tokstart = pstate->lexptr;
  1093. /* First of all, let us make sure we are not dealing with the
  1094. special tokens .true. and .false. which evaluate to 1 and 0. */
  1095. if (*pstate->lexptr == '.')
  1096. {
  1097. for (const auto &candidate : boolean_values)
  1098. {
  1099. if (strncasecmp (tokstart, candidate.name,
  1100. strlen (candidate.name)) == 0)
  1101. {
  1102. pstate->lexptr += strlen (candidate.name);
  1103. yylval.lval = candidate.value;
  1104. return BOOLEAN_LITERAL;
  1105. }
  1106. }
  1107. }
  1108. /* See if it is a Fortran operator. */
  1109. for (const auto &candidate : fortran_operators)
  1110. if (strncasecmp (tokstart, candidate.oper,
  1111. strlen (candidate.oper)) == 0)
  1112. {
  1113. gdb_assert (!candidate.case_sensitive);
  1114. pstate->lexptr += strlen (candidate.oper);
  1115. yylval.opcode = candidate.opcode;
  1116. return candidate.token;
  1117. }
  1118. switch (c = *tokstart)
  1119. {
  1120. case 0:
  1121. if (saw_name_at_eof)
  1122. {
  1123. saw_name_at_eof = false;
  1124. return COMPLETE;
  1125. }
  1126. else if (pstate->parse_completion && saw_structop)
  1127. return COMPLETE;
  1128. return 0;
  1129. case ' ':
  1130. case '\t':
  1131. case '\n':
  1132. pstate->lexptr++;
  1133. goto retry;
  1134. case '\'':
  1135. token = match_string_literal ();
  1136. if (token != 0)
  1137. return (token);
  1138. break;
  1139. case '(':
  1140. paren_depth++;
  1141. pstate->lexptr++;
  1142. return c;
  1143. case ')':
  1144. if (paren_depth == 0)
  1145. return 0;
  1146. paren_depth--;
  1147. pstate->lexptr++;
  1148. return c;
  1149. case ',':
  1150. if (pstate->comma_terminates && paren_depth == 0)
  1151. return 0;
  1152. pstate->lexptr++;
  1153. return c;
  1154. case '.':
  1155. /* Might be a floating point number. */
  1156. if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
  1157. goto symbol; /* Nope, must be a symbol. */
  1158. /* FALL THRU. */
  1159. case '0':
  1160. case '1':
  1161. case '2':
  1162. case '3':
  1163. case '4':
  1164. case '5':
  1165. case '6':
  1166. case '7':
  1167. case '8':
  1168. case '9':
  1169. {
  1170. /* It's a number. */
  1171. int got_dot = 0, got_e = 0, got_d = 0, toktype;
  1172. const char *p = tokstart;
  1173. int hex = input_radix > 10;
  1174. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1175. {
  1176. p += 2;
  1177. hex = 1;
  1178. }
  1179. else if (c == '0' && (p[1]=='t' || p[1]=='T'
  1180. || p[1]=='d' || p[1]=='D'))
  1181. {
  1182. p += 2;
  1183. hex = 0;
  1184. }
  1185. for (;; ++p)
  1186. {
  1187. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1188. got_dot = got_e = 1;
  1189. else if (!hex && !got_d && (*p == 'd' || *p == 'D'))
  1190. got_dot = got_d = 1;
  1191. else if (!hex && !got_dot && *p == '.')
  1192. got_dot = 1;
  1193. else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
  1194. || (got_d && (p[-1] == 'd' || p[-1] == 'D')))
  1195. && (*p == '-' || *p == '+'))
  1196. /* This is the sign of the exponent, not the end of the
  1197. number. */
  1198. continue;
  1199. /* We will take any letters or digits. parse_number will
  1200. complain if past the radix, or if L or U are not final. */
  1201. else if ((*p < '0' || *p > '9')
  1202. && ((*p < 'a' || *p > 'z')
  1203. && (*p < 'A' || *p > 'Z')))
  1204. break;
  1205. }
  1206. toktype = parse_number (pstate, tokstart, p - tokstart,
  1207. got_dot|got_e|got_d,
  1208. &yylval);
  1209. if (toktype == ERROR)
  1210. {
  1211. char *err_copy = (char *) alloca (p - tokstart + 1);
  1212. memcpy (err_copy, tokstart, p - tokstart);
  1213. err_copy[p - tokstart] = 0;
  1214. error (_("Invalid number \"%s\"."), err_copy);
  1215. }
  1216. pstate->lexptr = p;
  1217. return toktype;
  1218. }
  1219. case '%':
  1220. last_was_structop = true;
  1221. /* Fall through. */
  1222. case '+':
  1223. case '-':
  1224. case '*':
  1225. case '/':
  1226. case '|':
  1227. case '&':
  1228. case '^':
  1229. case '~':
  1230. case '!':
  1231. case '@':
  1232. case '<':
  1233. case '>':
  1234. case '[':
  1235. case ']':
  1236. case '?':
  1237. case ':':
  1238. case '=':
  1239. case '{':
  1240. case '}':
  1241. symbol:
  1242. pstate->lexptr++;
  1243. return c;
  1244. }
  1245. if (!(c == '_' || c == '$' || c ==':'
  1246. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1247. /* We must have come across a bad character (e.g. ';'). */
  1248. error (_("Invalid character '%c' in expression."), c);
  1249. namelen = 0;
  1250. for (c = tokstart[namelen];
  1251. (c == '_' || c == '$' || c == ':' || (c >= '0' && c <= '9')
  1252. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  1253. c = tokstart[++namelen]);
  1254. /* The token "if" terminates the expression and is NOT
  1255. removed from the input stream. */
  1256. if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1257. return 0;
  1258. pstate->lexptr += namelen;
  1259. /* Catch specific keywords. */
  1260. for (const auto &keyword : f77_keywords)
  1261. if (strlen (keyword.oper) == namelen
  1262. && ((!keyword.case_sensitive
  1263. && strncasecmp (tokstart, keyword.oper, namelen) == 0)
  1264. || (keyword.case_sensitive
  1265. && strncmp (tokstart, keyword.oper, namelen) == 0)))
  1266. {
  1267. yylval.opcode = keyword.opcode;
  1268. return keyword.token;
  1269. }
  1270. yylval.sval.ptr = tokstart;
  1271. yylval.sval.length = namelen;
  1272. if (*tokstart == '$')
  1273. return DOLLAR_VARIABLE;
  1274. /* Use token-type TYPENAME for symbols that happen to be defined
  1275. currently as names of types; NAME for other symbols.
  1276. The caller is not constrained to care about the distinction. */
  1277. {
  1278. std::string tmp = copy_name (yylval.sval);
  1279. struct block_symbol result;
  1280. const enum domain_enum_tag lookup_domains[] =
  1281. {
  1282. STRUCT_DOMAIN,
  1283. VAR_DOMAIN,
  1284. MODULE_DOMAIN
  1285. };
  1286. int hextype;
  1287. for (const auto &domain : lookup_domains)
  1288. {
  1289. result = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
  1290. domain, NULL);
  1291. if (result.symbol && result.symbol->aclass () == LOC_TYPEDEF)
  1292. {
  1293. yylval.tsym.type = result.symbol->type ();
  1294. return TYPENAME;
  1295. }
  1296. if (result.symbol)
  1297. break;
  1298. }
  1299. yylval.tsym.type
  1300. = language_lookup_primitive_type (pstate->language (),
  1301. pstate->gdbarch (), tmp.c_str ());
  1302. if (yylval.tsym.type != NULL)
  1303. return TYPENAME;
  1304. /* Input names that aren't symbols but ARE valid hex numbers,
  1305. when the input radix permits them, can be names or numbers
  1306. depending on the parse. Note we support radixes > 16 here. */
  1307. if (!result.symbol
  1308. && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1309. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1310. {
  1311. YYSTYPE newlval; /* Its value is ignored. */
  1312. hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
  1313. if (hextype == INT)
  1314. {
  1315. yylval.ssym.sym = result;
  1316. yylval.ssym.is_a_field_of_this = false;
  1317. return NAME_OR_INT;
  1318. }
  1319. }
  1320. if (pstate->parse_completion && *pstate->lexptr == '\0')
  1321. saw_name_at_eof = true;
  1322. /* Any other kind of symbol */
  1323. yylval.ssym.sym = result;
  1324. yylval.ssym.is_a_field_of_this = false;
  1325. return NAME;
  1326. }
  1327. }
  1328. int
  1329. f_language::parser (struct parser_state *par_state) const
  1330. {
  1331. /* Setting up the parser state. */
  1332. scoped_restore pstate_restore = make_scoped_restore (&pstate);
  1333. scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
  1334. parser_debug);
  1335. gdb_assert (par_state != NULL);
  1336. pstate = par_state;
  1337. last_was_structop = false;
  1338. saw_name_at_eof = false;
  1339. paren_depth = 0;
  1340. struct type_stack stack;
  1341. scoped_restore restore_type_stack = make_scoped_restore (&type_stack,
  1342. &stack);
  1343. int result = yyparse ();
  1344. if (!result)
  1345. pstate->set_operation (pstate->pop ());
  1346. return result;
  1347. }
  1348. static void
  1349. yyerror (const char *msg)
  1350. {
  1351. if (pstate->prev_lexptr)
  1352. pstate->lexptr = pstate->prev_lexptr;
  1353. error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
  1354. }