PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/gdb-7.4.50.20120714/gdb/f-exp.y

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