PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/gdb-7/gdb/f-exp.y

http://github.com/davshao/dflygsocdrm
Happy | 1227 lines | 1048 code | 179 blank | 0 comment | 0 complexity | 1931d8dfb98cd694156f7e16c3d288a0 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, LGPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception, 0BSD, BSD-3-Clause, GPL-2.0
  1. /* YACC parser for Fortran expressions, for GDB.
  2. Copyright (C) 1986, 1989-1991, 1993-1996, 2000-2012 Free Software
  3. Foundation, Inc.
  4. Contributed by Motorola. Adapted from the C parser by Farooq Butt
  5. (fmbutt@engage.sps.mot.com).
  6. This file is part of GDB.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  17. /* This was blantantly ripped off the C expression parser, please
  18. be aware of that as you look at its basic structure -FMB */
  19. /* Parse a F77 expression from text in a string,
  20. and return the result as a struct expression pointer.
  21. That structure contains arithmetic operations in reverse polish,
  22. with constants represented by operations that are followed by special data.
  23. See expression.h for the details of the format.
  24. What is important here is that it can be built up sequentially
  25. during the process of parsing; the lower levels of the tree always
  26. come first in the result.
  27. Note that malloc's and realloc's in this file are transformed to
  28. xmalloc and xrealloc respectively by the same sed command in the
  29. makefile that remaps any other malloc/realloc inserted by the parser
  30. generator. Doing this with #defines and trying to control the interaction
  31. with include files (<malloc.h> and <stdlib.h> for example) just became
  32. too messy, particularly when such includes can be inserted at random
  33. times by the parser generator. */
  34. %{
  35. #include "defs.h"
  36. #include "gdb_string.h"
  37. #include "expression.h"
  38. #include "value.h"
  39. #include "parser-defs.h"
  40. #include "language.h"
  41. #include "f-lang.h"
  42. #include "bfd.h" /* Required by objfiles.h. */
  43. #include "symfile.h" /* Required by objfiles.h. */
  44. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  45. #include "block.h"
  46. #include <ctype.h>
  47. #define parse_type builtin_type (parse_gdbarch)
  48. #define parse_f_type builtin_f_type (parse_gdbarch)
  49. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  50. as well as gratuitiously global symbol names, so we can have multiple
  51. yacc generated parsers in gdb. Note that these are only the variables
  52. produced by yacc. If other parser generators (bison, byacc, etc) produce
  53. additional global names that conflict at link time, then those parser
  54. generators need to be fixed instead of adding those names to this list. */
  55. #define yymaxdepth f_maxdepth
  56. #define yyparse f_parse
  57. #define yylex f_lex
  58. #define yyerror f_error
  59. #define yylval f_lval
  60. #define yychar f_char
  61. #define yydebug f_debug
  62. #define yypact f_pact
  63. #define yyr1 f_r1
  64. #define yyr2 f_r2
  65. #define yydef f_def
  66. #define yychk f_chk
  67. #define yypgo f_pgo
  68. #define yyact f_act
  69. #define yyexca f_exca
  70. #define yyerrflag f_errflag
  71. #define yynerrs f_nerrs
  72. #define yyps f_ps
  73. #define yypv f_pv
  74. #define yys f_s
  75. #define yy_yys f_yys
  76. #define yystate f_state
  77. #define yytmp f_tmp
  78. #define yyv f_v
  79. #define yy_yyv f_yyv
  80. #define yyval f_val
  81. #define yylloc f_lloc
  82. #define yyreds f_reds /* With YYDEBUG defined */
  83. #define yytoks f_toks /* With YYDEBUG defined */
  84. #define yyname f_name /* With YYDEBUG defined */
  85. #define yyrule f_rule /* With YYDEBUG defined */
  86. #define yylhs f_yylhs
  87. #define yylen f_yylen
  88. #define yydefred f_yydefred
  89. #define yydgoto f_yydgoto
  90. #define yysindex f_yysindex
  91. #define yyrindex f_yyrindex
  92. #define yygindex f_yygindex
  93. #define yytable f_yytable
  94. #define yycheck f_yycheck
  95. #ifndef YYDEBUG
  96. #define YYDEBUG 1 /* Default to yydebug support */
  97. #endif
  98. #define YYFPRINTF parser_fprintf
  99. int yyparse (void);
  100. static int yylex (void);
  101. void yyerror (char *);
  102. static void growbuf_by_size (int);
  103. static int match_string_literal (void);
  104. %}
  105. /* Although the yacc "value" of an expression is not used,
  106. since the result is stored in the structure being created,
  107. other node types do have values. */
  108. %union
  109. {
  110. LONGEST lval;
  111. struct {
  112. LONGEST val;
  113. struct type *type;
  114. } typed_val;
  115. DOUBLEST dval;
  116. struct symbol *sym;
  117. struct type *tval;
  118. struct stoken sval;
  119. struct ttype tsym;
  120. struct symtoken ssym;
  121. int voidval;
  122. struct block *bval;
  123. enum exp_opcode opcode;
  124. struct internalvar *ivar;
  125. struct type **tvec;
  126. int *ivec;
  127. }
  128. %{
  129. /* YYSTYPE gets defined by %union */
  130. static int parse_number (char *, int, int, YYSTYPE *);
  131. %}
  132. %type <voidval> exp type_exp start variable
  133. %type <tval> type typebase
  134. %type <tvec> nonempty_typelist
  135. /* %type <bval> block */
  136. /* Fancy type parsing. */
  137. %type <voidval> func_mod direct_abs_decl abs_decl
  138. %type <tval> ptype
  139. %token <typed_val> INT
  140. %token <dval> FLOAT
  141. /* Both NAME and TYPENAME tokens represent symbols in the input,
  142. and both convey their data as strings.
  143. But a TYPENAME is a string that happens to be defined as a typedef
  144. or builtin type name (such as int or char)
  145. and a NAME is any other symbol.
  146. Contexts where this distinction is not important can use the
  147. nonterminal "name", which matches either NAME or TYPENAME. */
  148. %token <sval> STRING_LITERAL
  149. %token <lval> BOOLEAN_LITERAL
  150. %token <ssym> NAME
  151. %token <tsym> TYPENAME
  152. %type <sval> name
  153. %type <ssym> name_not_typename
  154. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  155. but which would parse as a valid number in the current input radix.
  156. E.g. "c" when input_radix==16. Depending on the parse, it will be
  157. turned into a name or into a number. */
  158. %token <ssym> NAME_OR_INT
  159. %token SIZEOF
  160. %token ERROR
  161. /* Special type cases, put in to allow the parser to distinguish different
  162. legal basetypes. */
  163. %token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD
  164. %token LOGICAL_S8_KEYWORD
  165. %token LOGICAL_KEYWORD REAL_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
  166. %token COMPLEX_S8_KEYWORD COMPLEX_S16_KEYWORD COMPLEX_S32_KEYWORD
  167. %token BOOL_AND BOOL_OR BOOL_NOT
  168. %token <lval> CHARACTER
  169. %token <voidval> VARIABLE
  170. %token <opcode> ASSIGN_MODIFY
  171. %left ','
  172. %left ABOVE_COMMA
  173. %right '=' ASSIGN_MODIFY
  174. %right '?'
  175. %left BOOL_OR
  176. %right BOOL_NOT
  177. %left BOOL_AND
  178. %left '|'
  179. %left '^'
  180. %left '&'
  181. %left EQUAL NOTEQUAL
  182. %left LESSTHAN GREATERTHAN LEQ GEQ
  183. %left LSH RSH
  184. %left '@'
  185. %left '+' '-'
  186. %left '*' '/'
  187. %right STARSTAR
  188. %right '%'
  189. %right UNARY
  190. %right '('
  191. %%
  192. start : exp
  193. | type_exp
  194. ;
  195. type_exp: type
  196. { write_exp_elt_opcode(OP_TYPE);
  197. write_exp_elt_type($1);
  198. write_exp_elt_opcode(OP_TYPE); }
  199. ;
  200. exp : '(' exp ')'
  201. { }
  202. ;
  203. /* Expressions, not including the comma operator. */
  204. exp : '*' exp %prec UNARY
  205. { write_exp_elt_opcode (UNOP_IND); }
  206. ;
  207. exp : '&' exp %prec UNARY
  208. { write_exp_elt_opcode (UNOP_ADDR); }
  209. ;
  210. exp : '-' exp %prec UNARY
  211. { write_exp_elt_opcode (UNOP_NEG); }
  212. ;
  213. exp : BOOL_NOT exp %prec UNARY
  214. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  215. ;
  216. exp : '~' exp %prec UNARY
  217. { write_exp_elt_opcode (UNOP_COMPLEMENT); }
  218. ;
  219. exp : SIZEOF exp %prec UNARY
  220. { write_exp_elt_opcode (UNOP_SIZEOF); }
  221. ;
  222. /* No more explicit array operators, we treat everything in F77 as
  223. a function call. The disambiguation as to whether we are
  224. doing a subscript operation or a function call is done
  225. later in eval.c. */
  226. exp : exp '('
  227. { start_arglist (); }
  228. arglist ')'
  229. { write_exp_elt_opcode (OP_F77_UNDETERMINED_ARGLIST);
  230. write_exp_elt_longcst ((LONGEST) end_arglist ());
  231. write_exp_elt_opcode (OP_F77_UNDETERMINED_ARGLIST); }
  232. ;
  233. arglist :
  234. ;
  235. arglist : exp
  236. { arglist_len = 1; }
  237. ;
  238. arglist : subrange
  239. { arglist_len = 1; }
  240. ;
  241. arglist : arglist ',' exp %prec ABOVE_COMMA
  242. { arglist_len++; }
  243. ;
  244. /* There are four sorts of subrange types in F90. */
  245. subrange: exp ':' exp %prec ABOVE_COMMA
  246. { write_exp_elt_opcode (OP_F90_RANGE);
  247. write_exp_elt_longcst (NONE_BOUND_DEFAULT);
  248. write_exp_elt_opcode (OP_F90_RANGE); }
  249. ;
  250. subrange: exp ':' %prec ABOVE_COMMA
  251. { write_exp_elt_opcode (OP_F90_RANGE);
  252. write_exp_elt_longcst (HIGH_BOUND_DEFAULT);
  253. write_exp_elt_opcode (OP_F90_RANGE); }
  254. ;
  255. subrange: ':' exp %prec ABOVE_COMMA
  256. { write_exp_elt_opcode (OP_F90_RANGE);
  257. write_exp_elt_longcst (LOW_BOUND_DEFAULT);
  258. write_exp_elt_opcode (OP_F90_RANGE); }
  259. ;
  260. subrange: ':' %prec ABOVE_COMMA
  261. { write_exp_elt_opcode (OP_F90_RANGE);
  262. write_exp_elt_longcst (BOTH_BOUND_DEFAULT);
  263. write_exp_elt_opcode (OP_F90_RANGE); }
  264. ;
  265. complexnum: exp ',' exp
  266. { }
  267. ;
  268. exp : '(' complexnum ')'
  269. { write_exp_elt_opcode(OP_COMPLEX);
  270. write_exp_elt_type (parse_f_type->builtin_complex_s16);
  271. write_exp_elt_opcode(OP_COMPLEX); }
  272. ;
  273. exp : '(' type ')' exp %prec UNARY
  274. { write_exp_elt_opcode (UNOP_CAST);
  275. write_exp_elt_type ($2);
  276. write_exp_elt_opcode (UNOP_CAST); }
  277. ;
  278. exp : exp '%' name
  279. { write_exp_elt_opcode (STRUCTOP_STRUCT);
  280. write_exp_string ($3);
  281. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  282. ;
  283. /* Binary operators in order of decreasing precedence. */
  284. exp : exp '@' exp
  285. { write_exp_elt_opcode (BINOP_REPEAT); }
  286. ;
  287. exp : exp STARSTAR exp
  288. { write_exp_elt_opcode (BINOP_EXP); }
  289. ;
  290. exp : exp '*' exp
  291. { write_exp_elt_opcode (BINOP_MUL); }
  292. ;
  293. exp : exp '/' exp
  294. { write_exp_elt_opcode (BINOP_DIV); }
  295. ;
  296. exp : exp '+' exp
  297. { write_exp_elt_opcode (BINOP_ADD); }
  298. ;
  299. exp : exp '-' exp
  300. { write_exp_elt_opcode (BINOP_SUB); }
  301. ;
  302. exp : exp LSH exp
  303. { write_exp_elt_opcode (BINOP_LSH); }
  304. ;
  305. exp : exp RSH exp
  306. { write_exp_elt_opcode (BINOP_RSH); }
  307. ;
  308. exp : exp EQUAL exp
  309. { write_exp_elt_opcode (BINOP_EQUAL); }
  310. ;
  311. exp : exp NOTEQUAL exp
  312. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  313. ;
  314. exp : exp LEQ exp
  315. { write_exp_elt_opcode (BINOP_LEQ); }
  316. ;
  317. exp : exp GEQ exp
  318. { write_exp_elt_opcode (BINOP_GEQ); }
  319. ;
  320. exp : exp LESSTHAN exp
  321. { write_exp_elt_opcode (BINOP_LESS); }
  322. ;
  323. exp : exp GREATERTHAN exp
  324. { write_exp_elt_opcode (BINOP_GTR); }
  325. ;
  326. exp : exp '&' exp
  327. { write_exp_elt_opcode (BINOP_BITWISE_AND); }
  328. ;
  329. exp : exp '^' exp
  330. { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
  331. ;
  332. exp : exp '|' exp
  333. { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
  334. ;
  335. exp : exp BOOL_AND exp
  336. { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
  337. ;
  338. exp : exp BOOL_OR exp
  339. { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
  340. ;
  341. exp : exp '=' exp
  342. { write_exp_elt_opcode (BINOP_ASSIGN); }
  343. ;
  344. exp : exp ASSIGN_MODIFY exp
  345. { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  346. write_exp_elt_opcode ($2);
  347. write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  348. ;
  349. exp : INT
  350. { write_exp_elt_opcode (OP_LONG);
  351. write_exp_elt_type ($1.type);
  352. write_exp_elt_longcst ((LONGEST)($1.val));
  353. write_exp_elt_opcode (OP_LONG); }
  354. ;
  355. exp : NAME_OR_INT
  356. { YYSTYPE val;
  357. parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
  358. write_exp_elt_opcode (OP_LONG);
  359. write_exp_elt_type (val.typed_val.type);
  360. write_exp_elt_longcst ((LONGEST)val.typed_val.val);
  361. write_exp_elt_opcode (OP_LONG); }
  362. ;
  363. exp : FLOAT
  364. { write_exp_elt_opcode (OP_DOUBLE);
  365. write_exp_elt_type (parse_f_type->builtin_real_s8);
  366. write_exp_elt_dblcst ($1);
  367. write_exp_elt_opcode (OP_DOUBLE); }
  368. ;
  369. exp : variable
  370. ;
  371. exp : VARIABLE
  372. ;
  373. exp : SIZEOF '(' type ')' %prec UNARY
  374. { write_exp_elt_opcode (OP_LONG);
  375. write_exp_elt_type (parse_f_type->builtin_integer);
  376. CHECK_TYPEDEF ($3);
  377. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  378. write_exp_elt_opcode (OP_LONG); }
  379. ;
  380. exp : BOOLEAN_LITERAL
  381. { write_exp_elt_opcode (OP_BOOL);
  382. write_exp_elt_longcst ((LONGEST) $1);
  383. write_exp_elt_opcode (OP_BOOL);
  384. }
  385. ;
  386. exp : STRING_LITERAL
  387. {
  388. write_exp_elt_opcode (OP_STRING);
  389. write_exp_string ($1);
  390. write_exp_elt_opcode (OP_STRING);
  391. }
  392. ;
  393. variable: name_not_typename
  394. { struct symbol *sym = $1.sym;
  395. if (sym)
  396. {
  397. if (symbol_read_needs_frame (sym))
  398. {
  399. if (innermost_block == 0
  400. || contained_in (block_found,
  401. innermost_block))
  402. innermost_block = block_found;
  403. }
  404. write_exp_elt_opcode (OP_VAR_VALUE);
  405. /* We want to use the selected frame, not
  406. another more inner frame which happens to
  407. be in the same block. */
  408. write_exp_elt_block (NULL);
  409. write_exp_elt_sym (sym);
  410. write_exp_elt_opcode (OP_VAR_VALUE);
  411. break;
  412. }
  413. else
  414. {
  415. struct minimal_symbol *msymbol;
  416. char *arg = copy_name ($1.stoken);
  417. msymbol =
  418. lookup_minimal_symbol (arg, NULL, NULL);
  419. if (msymbol != NULL)
  420. write_exp_msymbol (msymbol);
  421. else if (!have_full_symbols () && !have_partial_symbols ())
  422. error (_("No symbol table is loaded. Use the \"file\" command."));
  423. else
  424. error (_("No symbol \"%s\" in current context."),
  425. copy_name ($1.stoken));
  426. }
  427. }
  428. ;
  429. type : ptype
  430. ;
  431. ptype : typebase
  432. | typebase abs_decl
  433. {
  434. /* This is where the interesting stuff happens. */
  435. int done = 0;
  436. int array_size;
  437. struct type *follow_type = $1;
  438. struct type *range_type;
  439. while (!done)
  440. switch (pop_type ())
  441. {
  442. case tp_end:
  443. done = 1;
  444. break;
  445. case tp_pointer:
  446. follow_type = lookup_pointer_type (follow_type);
  447. break;
  448. case tp_reference:
  449. follow_type = lookup_reference_type (follow_type);
  450. break;
  451. case tp_array:
  452. array_size = pop_type_int ();
  453. if (array_size != -1)
  454. {
  455. range_type =
  456. create_range_type ((struct type *) NULL,
  457. parse_f_type->builtin_integer,
  458. 0, array_size - 1);
  459. follow_type =
  460. create_array_type ((struct type *) NULL,
  461. follow_type, range_type);
  462. }
  463. else
  464. follow_type = lookup_pointer_type (follow_type);
  465. break;
  466. case tp_function:
  467. follow_type = lookup_function_type (follow_type);
  468. break;
  469. }
  470. $$ = follow_type;
  471. }
  472. ;
  473. abs_decl: '*'
  474. { push_type (tp_pointer); $$ = 0; }
  475. | '*' abs_decl
  476. { push_type (tp_pointer); $$ = $2; }
  477. | '&'
  478. { push_type (tp_reference); $$ = 0; }
  479. | '&' abs_decl
  480. { push_type (tp_reference); $$ = $2; }
  481. | direct_abs_decl
  482. ;
  483. direct_abs_decl: '(' abs_decl ')'
  484. { $$ = $2; }
  485. | direct_abs_decl func_mod
  486. { push_type (tp_function); }
  487. | func_mod
  488. { push_type (tp_function); }
  489. ;
  490. func_mod: '(' ')'
  491. { $$ = 0; }
  492. | '(' nonempty_typelist ')'
  493. { free ($2); $$ = 0; }
  494. ;
  495. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  496. : TYPENAME
  497. { $$ = $1.type; }
  498. | INT_KEYWORD
  499. { $$ = parse_f_type->builtin_integer; }
  500. | INT_S2_KEYWORD
  501. { $$ = parse_f_type->builtin_integer_s2; }
  502. | CHARACTER
  503. { $$ = parse_f_type->builtin_character; }
  504. | LOGICAL_S8_KEYWORD
  505. { $$ = parse_f_type->builtin_logical_s8; }
  506. | LOGICAL_KEYWORD
  507. { $$ = parse_f_type->builtin_logical; }
  508. | LOGICAL_S2_KEYWORD
  509. { $$ = parse_f_type->builtin_logical_s2; }
  510. | LOGICAL_S1_KEYWORD
  511. { $$ = parse_f_type->builtin_logical_s1; }
  512. | REAL_KEYWORD
  513. { $$ = parse_f_type->builtin_real; }
  514. | REAL_S8_KEYWORD
  515. { $$ = parse_f_type->builtin_real_s8; }
  516. | REAL_S16_KEYWORD
  517. { $$ = parse_f_type->builtin_real_s16; }
  518. | COMPLEX_S8_KEYWORD
  519. { $$ = parse_f_type->builtin_complex_s8; }
  520. | COMPLEX_S16_KEYWORD
  521. { $$ = parse_f_type->builtin_complex_s16; }
  522. | COMPLEX_S32_KEYWORD
  523. { $$ = parse_f_type->builtin_complex_s32; }
  524. ;
  525. nonempty_typelist
  526. : type
  527. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  528. $<ivec>$[0] = 1; /* Number of types in vector */
  529. $$[1] = $1;
  530. }
  531. | nonempty_typelist ',' type
  532. { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  533. $$ = (struct type **) realloc ((char *) $1, len);
  534. $$[$<ivec>$[0]] = $3;
  535. }
  536. ;
  537. name : NAME
  538. { $$ = $1.stoken; }
  539. ;
  540. name_not_typename : NAME
  541. /* These would be useful if name_not_typename was useful, but it is just
  542. a fake for "variable", so these cause reduce/reduce conflicts because
  543. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  544. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  545. context where only a name could occur, this might be useful.
  546. | NAME_OR_INT
  547. */
  548. ;
  549. %%
  550. /* Take care of parsing a number (anything that starts with a digit).
  551. Set yylval and return the token type; update lexptr.
  552. LEN is the number of characters in it. */
  553. /*** Needs some error checking for the float case ***/
  554. static int
  555. parse_number (p, len, parsed_float, putithere)
  556. char *p;
  557. int len;
  558. int parsed_float;
  559. YYSTYPE *putithere;
  560. {
  561. LONGEST n = 0;
  562. LONGEST prevn = 0;
  563. int c;
  564. int base = input_radix;
  565. int unsigned_p = 0;
  566. int long_p = 0;
  567. ULONGEST high_bit;
  568. struct type *signed_type;
  569. struct type *unsigned_type;
  570. if (parsed_float)
  571. {
  572. /* It's a float since it contains a point or an exponent. */
  573. /* [dD] is not understood as an exponent by atof, change it to 'e'. */
  574. char *tmp, *tmp2;
  575. tmp = xstrdup (p);
  576. for (tmp2 = tmp; *tmp2; ++tmp2)
  577. if (*tmp2 == 'd' || *tmp2 == 'D')
  578. *tmp2 = 'e';
  579. putithere->dval = atof (tmp);
  580. free (tmp);
  581. return FLOAT;
  582. }
  583. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  584. if (p[0] == '0')
  585. switch (p[1])
  586. {
  587. case 'x':
  588. case 'X':
  589. if (len >= 3)
  590. {
  591. p += 2;
  592. base = 16;
  593. len -= 2;
  594. }
  595. break;
  596. case 't':
  597. case 'T':
  598. case 'd':
  599. case 'D':
  600. if (len >= 3)
  601. {
  602. p += 2;
  603. base = 10;
  604. len -= 2;
  605. }
  606. break;
  607. default:
  608. base = 8;
  609. break;
  610. }
  611. while (len-- > 0)
  612. {
  613. c = *p++;
  614. if (isupper (c))
  615. c = tolower (c);
  616. if (len == 0 && c == 'l')
  617. long_p = 1;
  618. else if (len == 0 && c == 'u')
  619. unsigned_p = 1;
  620. else
  621. {
  622. int i;
  623. if (c >= '0' && c <= '9')
  624. i = c - '0';
  625. else if (c >= 'a' && c <= 'f')
  626. i = c - 'a' + 10;
  627. else
  628. return ERROR; /* Char not a digit */
  629. if (i >= base)
  630. return ERROR; /* Invalid digit in this base */
  631. n *= base;
  632. n += i;
  633. }
  634. /* Portably test for overflow (only works for nonzero values, so make
  635. a second check for zero). */
  636. if ((prevn >= n) && n != 0)
  637. unsigned_p=1; /* Try something unsigned */
  638. /* If range checking enabled, portably test for unsigned overflow. */
  639. if (RANGE_CHECK && n != 0)
  640. {
  641. if ((unsigned_p && (unsigned)prevn >= (unsigned)n))
  642. range_error (_("Overflow on numeric constant."));
  643. }
  644. prevn = n;
  645. }
  646. /* If the number is too big to be an int, or it's got an l suffix
  647. then it's a long. Work out if this has to be a long by
  648. shifting right and seeing if anything remains, and the
  649. target int size is different to the target long size.
  650. In the expression below, we could have tested
  651. (n >> gdbarch_int_bit (parse_gdbarch))
  652. to see if it was zero,
  653. but too many compilers warn about that, when ints and longs
  654. are the same size. So we shift it twice, with fewer bits
  655. each time, for the same result. */
  656. if ((gdbarch_int_bit (parse_gdbarch) != gdbarch_long_bit (parse_gdbarch)
  657. && ((n >> 2)
  658. >> (gdbarch_int_bit (parse_gdbarch)-2))) /* Avoid shift warning */
  659. || long_p)
  660. {
  661. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch)-1);
  662. unsigned_type = parse_type->builtin_unsigned_long;
  663. signed_type = parse_type->builtin_long;
  664. }
  665. else
  666. {
  667. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch)-1);
  668. unsigned_type = parse_type->builtin_unsigned_int;
  669. signed_type = parse_type->builtin_int;
  670. }
  671. putithere->typed_val.val = n;
  672. /* If the high bit of the worked out type is set then this number
  673. has to be unsigned. */
  674. if (unsigned_p || (n & high_bit))
  675. putithere->typed_val.type = unsigned_type;
  676. else
  677. putithere->typed_val.type = signed_type;
  678. return INT;
  679. }
  680. struct token
  681. {
  682. char *operator;
  683. int token;
  684. enum exp_opcode opcode;
  685. };
  686. static const struct token dot_ops[] =
  687. {
  688. { ".and.", BOOL_AND, BINOP_END },
  689. { ".AND.", BOOL_AND, BINOP_END },
  690. { ".or.", BOOL_OR, BINOP_END },
  691. { ".OR.", BOOL_OR, BINOP_END },
  692. { ".not.", BOOL_NOT, BINOP_END },
  693. { ".NOT.", BOOL_NOT, BINOP_END },
  694. { ".eq.", EQUAL, BINOP_END },
  695. { ".EQ.", EQUAL, BINOP_END },
  696. { ".eqv.", EQUAL, BINOP_END },
  697. { ".NEQV.", NOTEQUAL, BINOP_END },
  698. { ".neqv.", NOTEQUAL, BINOP_END },
  699. { ".EQV.", EQUAL, BINOP_END },
  700. { ".ne.", NOTEQUAL, BINOP_END },
  701. { ".NE.", NOTEQUAL, BINOP_END },
  702. { ".le.", LEQ, BINOP_END },
  703. { ".LE.", LEQ, BINOP_END },
  704. { ".ge.", GEQ, BINOP_END },
  705. { ".GE.", GEQ, BINOP_END },
  706. { ".gt.", GREATERTHAN, BINOP_END },
  707. { ".GT.", GREATERTHAN, BINOP_END },
  708. { ".lt.", LESSTHAN, BINOP_END },
  709. { ".LT.", LESSTHAN, BINOP_END },
  710. { NULL, 0, 0 }
  711. };
  712. struct f77_boolean_val
  713. {
  714. char *name;
  715. int value;
  716. };
  717. static const struct f77_boolean_val boolean_values[] =
  718. {
  719. { ".true.", 1 },
  720. { ".TRUE.", 1 },
  721. { ".false.", 0 },
  722. { ".FALSE.", 0 },
  723. { NULL, 0 }
  724. };
  725. static const struct token f77_keywords[] =
  726. {
  727. { "complex_16", COMPLEX_S16_KEYWORD, BINOP_END },
  728. { "complex_32", COMPLEX_S32_KEYWORD, BINOP_END },
  729. { "character", CHARACTER, BINOP_END },
  730. { "integer_2", INT_S2_KEYWORD, BINOP_END },
  731. { "logical_1", LOGICAL_S1_KEYWORD, BINOP_END },
  732. { "logical_2", LOGICAL_S2_KEYWORD, BINOP_END },
  733. { "logical_8", LOGICAL_S8_KEYWORD, BINOP_END },
  734. { "complex_8", COMPLEX_S8_KEYWORD, BINOP_END },
  735. { "integer", INT_KEYWORD, BINOP_END },
  736. { "logical", LOGICAL_KEYWORD, BINOP_END },
  737. { "real_16", REAL_S16_KEYWORD, BINOP_END },
  738. { "complex", COMPLEX_S8_KEYWORD, BINOP_END },
  739. { "sizeof", SIZEOF, BINOP_END },
  740. { "real_8", REAL_S8_KEYWORD, BINOP_END },
  741. { "real", REAL_KEYWORD, BINOP_END },
  742. { NULL, 0, 0 }
  743. };
  744. /* Implementation of a dynamically expandable buffer for processing input
  745. characters acquired through lexptr and building a value to return in
  746. yylval. Ripped off from ch-exp.y */
  747. static char *tempbuf; /* Current buffer contents */
  748. static int tempbufsize; /* Size of allocated buffer */
  749. static int tempbufindex; /* Current index into buffer */
  750. #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
  751. #define CHECKBUF(size) \
  752. do { \
  753. if (tempbufindex + (size) >= tempbufsize) \
  754. { \
  755. growbuf_by_size (size); \
  756. } \
  757. } while (0);
  758. /* Grow the static temp buffer if necessary, including allocating the
  759. first one on demand. */
  760. static void
  761. growbuf_by_size (count)
  762. int count;
  763. {
  764. int growby;
  765. growby = max (count, GROWBY_MIN_SIZE);
  766. tempbufsize += growby;
  767. if (tempbuf == NULL)
  768. tempbuf = (char *) malloc (tempbufsize);
  769. else
  770. tempbuf = (char *) realloc (tempbuf, tempbufsize);
  771. }
  772. /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
  773. string-literals.
  774. Recognize a string literal. A string literal is a nonzero sequence
  775. of characters enclosed in matching single quotes, except that
  776. a single character inside single quotes is a character literal, which
  777. we reject as a string literal. To embed the terminator character inside
  778. a string, it is simply doubled (I.E. 'this''is''one''string') */
  779. static int
  780. match_string_literal (void)
  781. {
  782. char *tokptr = lexptr;
  783. for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
  784. {
  785. CHECKBUF (1);
  786. if (*tokptr == *lexptr)
  787. {
  788. if (*(tokptr + 1) == *lexptr)
  789. tokptr++;
  790. else
  791. break;
  792. }
  793. tempbuf[tempbufindex++] = *tokptr;
  794. }
  795. if (*tokptr == '\0' /* no terminator */
  796. || tempbufindex == 0) /* no string */
  797. return 0;
  798. else
  799. {
  800. tempbuf[tempbufindex] = '\0';
  801. yylval.sval.ptr = tempbuf;
  802. yylval.sval.length = tempbufindex;
  803. lexptr = ++tokptr;
  804. return STRING_LITERAL;
  805. }
  806. }
  807. /* Read one token, getting characters through lexptr. */
  808. static int
  809. yylex (void)
  810. {
  811. int c;
  812. int namelen;
  813. unsigned int i,token;
  814. char *tokstart;
  815. retry:
  816. prev_lexptr = lexptr;
  817. tokstart = lexptr;
  818. /* First of all, let us make sure we are not dealing with the
  819. special tokens .true. and .false. which evaluate to 1 and 0. */
  820. if (*lexptr == '.')
  821. {
  822. for (i = 0; boolean_values[i].name != NULL; i++)
  823. {
  824. if (strncmp (tokstart, boolean_values[i].name,
  825. strlen (boolean_values[i].name)) == 0)
  826. {
  827. lexptr += strlen (boolean_values[i].name);
  828. yylval.lval = boolean_values[i].value;
  829. return BOOLEAN_LITERAL;
  830. }
  831. }
  832. }
  833. /* See if it is a special .foo. operator. */
  834. for (i = 0; dot_ops[i].operator != NULL; i++)
  835. if (strncmp (tokstart, dot_ops[i].operator,
  836. strlen (dot_ops[i].operator)) == 0)
  837. {
  838. lexptr += strlen (dot_ops[i].operator);
  839. yylval.opcode = dot_ops[i].opcode;
  840. return dot_ops[i].token;
  841. }
  842. /* See if it is an exponentiation operator. */
  843. if (strncmp (tokstart, "**", 2) == 0)
  844. {
  845. lexptr += 2;
  846. yylval.opcode = BINOP_EXP;
  847. return STARSTAR;
  848. }
  849. switch (c = *tokstart)
  850. {
  851. case 0:
  852. return 0;
  853. case ' ':
  854. case '\t':
  855. case '\n':
  856. lexptr++;
  857. goto retry;
  858. case '\'':
  859. token = match_string_literal ();
  860. if (token != 0)
  861. return (token);
  862. break;
  863. case '(':
  864. paren_depth++;
  865. lexptr++;
  866. return c;
  867. case ')':
  868. if (paren_depth == 0)
  869. return 0;
  870. paren_depth--;
  871. lexptr++;
  872. return c;
  873. case ',':
  874. if (comma_terminates && paren_depth == 0)
  875. return 0;
  876. lexptr++;
  877. return c;
  878. case '.':
  879. /* Might be a floating point number. */
  880. if (lexptr[1] < '0' || lexptr[1] > '9')
  881. goto symbol; /* Nope, must be a symbol. */
  882. /* FALL THRU into number case. */
  883. case '0':
  884. case '1':
  885. case '2':
  886. case '3':
  887. case '4':
  888. case '5':
  889. case '6':
  890. case '7':
  891. case '8':
  892. case '9':
  893. {
  894. /* It's a number. */
  895. int got_dot = 0, got_e = 0, got_d = 0, toktype;
  896. char *p = tokstart;
  897. int hex = input_radix > 10;
  898. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  899. {
  900. p += 2;
  901. hex = 1;
  902. }
  903. else if (c == '0' && (p[1]=='t' || p[1]=='T'
  904. || p[1]=='d' || p[1]=='D'))
  905. {
  906. p += 2;
  907. hex = 0;
  908. }
  909. for (;; ++p)
  910. {
  911. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  912. got_dot = got_e = 1;
  913. else if (!hex && !got_d && (*p == 'd' || *p == 'D'))
  914. got_dot = got_d = 1;
  915. else if (!hex && !got_dot && *p == '.')
  916. got_dot = 1;
  917. else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
  918. || (got_d && (p[-1] == 'd' || p[-1] == 'D')))
  919. && (*p == '-' || *p == '+'))
  920. /* This is the sign of the exponent, not the end of the
  921. number. */
  922. continue;
  923. /* We will take any letters or digits. parse_number will
  924. complain if past the radix, or if L or U are not final. */
  925. else if ((*p < '0' || *p > '9')
  926. && ((*p < 'a' || *p > 'z')
  927. && (*p < 'A' || *p > 'Z')))
  928. break;
  929. }
  930. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e|got_d,
  931. &yylval);
  932. if (toktype == ERROR)
  933. {
  934. char *err_copy = (char *) alloca (p - tokstart + 1);
  935. memcpy (err_copy, tokstart, p - tokstart);
  936. err_copy[p - tokstart] = 0;
  937. error (_("Invalid number \"%s\"."), err_copy);
  938. }
  939. lexptr = p;
  940. return toktype;
  941. }
  942. case '+':
  943. case '-':
  944. case '*':
  945. case '/':
  946. case '%':
  947. case '|':
  948. case '&':
  949. case '^':
  950. case '~':
  951. case '!':
  952. case '@':
  953. case '<':
  954. case '>':
  955. case '[':
  956. case ']':
  957. case '?':
  958. case ':':
  959. case '=':
  960. case '{':
  961. case '}':
  962. symbol:
  963. lexptr++;
  964. return c;
  965. }
  966. if (!(c == '_' || c == '$' || c ==':'
  967. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  968. /* We must have come across a bad character (e.g. ';'). */
  969. error (_("Invalid character '%c' in expression."), c);
  970. namelen = 0;
  971. for (c = tokstart[namelen];
  972. (c == '_' || c == '$' || c == ':' || (c >= '0' && c <= '9')
  973. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  974. c = tokstart[++namelen]);
  975. /* The token "if" terminates the expression and is NOT
  976. removed from the input stream. */
  977. if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  978. return 0;
  979. lexptr += namelen;
  980. /* Catch specific keywords. */
  981. for (i = 0; f77_keywords[i].operator != NULL; i++)
  982. if (strlen (f77_keywords[i].operator) == namelen
  983. && strncmp (tokstart, f77_keywords[i].operator, namelen) == 0)
  984. {
  985. /* lexptr += strlen(f77_keywords[i].operator); */
  986. yylval.opcode = f77_keywords[i].opcode;
  987. return f77_keywords[i].token;
  988. }
  989. yylval.sval.ptr = tokstart;
  990. yylval.sval.length = namelen;
  991. if (*tokstart == '$')
  992. {
  993. write_dollar_variable (yylval.sval);
  994. return VARIABLE;
  995. }
  996. /* Use token-type TYPENAME for symbols that happen to be defined
  997. currently as names of types; NAME for other symbols.
  998. The caller is not constrained to care about the distinction. */
  999. {
  1000. char *tmp = copy_name (yylval.sval);
  1001. struct symbol *sym;
  1002. int is_a_field_of_this = 0;
  1003. int hextype;
  1004. sym = lookup_symbol (tmp, expression_context_block,
  1005. VAR_DOMAIN,
  1006. parse_language->la_language == language_cplus
  1007. ? &is_a_field_of_this : NULL);
  1008. if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1009. {
  1010. yylval.tsym.type = SYMBOL_TYPE (sym);
  1011. return TYPENAME;
  1012. }
  1013. yylval.tsym.type
  1014. = language_lookup_primitive_type_by_name (parse_language,
  1015. parse_gdbarch, tmp);
  1016. if (yylval.tsym.type != NULL)
  1017. return TYPENAME;
  1018. /* Input names that aren't symbols but ARE valid hex numbers,
  1019. when the input radix permits them, can be names or numbers
  1020. depending on the parse. Note we support radixes > 16 here. */
  1021. if (!sym
  1022. && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1023. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1024. {
  1025. YYSTYPE newlval; /* Its value is ignored. */
  1026. hextype = parse_number (tokstart, namelen, 0, &newlval);
  1027. if (hextype == INT)
  1028. {
  1029. yylval.ssym.sym = sym;
  1030. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1031. return NAME_OR_INT;
  1032. }
  1033. }
  1034. /* Any other kind of symbol */
  1035. yylval.ssym.sym = sym;
  1036. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1037. return NAME;
  1038. }
  1039. }
  1040. void
  1041. yyerror (msg)
  1042. char *msg;
  1043. {
  1044. if (prev_lexptr)
  1045. lexptr = prev_lexptr;
  1046. error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
  1047. }