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

/avr-gdb-7.1/gdb-7.1/gdb/f-exp.y

#
Happy | 1222 lines | 1043 code | 179 blank | 0 comment | 0 complexity | 37e41bee2b662b0af563f0b7545bc9d0 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, 1990, 1991, 1993, 1994, 1995, 1996, 2000, 2001,
  3. 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. Free Software Foundation, Inc.
  5. Contributed by Motorola. Adapted from the C parser by Farooq Butt
  6. (fmbutt@engage.sps.mot.com).
  7. This file is part of GDB.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  18. /* This was blantantly ripped off the C expression parser, please
  19. be aware of that as you look at its basic structure -FMB */
  20. /* Parse a F77 expression from text in a string,
  21. and return the result as a struct expression pointer.
  22. That structure contains arithmetic operations in reverse polish,
  23. with constants represented by operations that are followed by special data.
  24. See expression.h for the details of the format.
  25. What is important here is that it can be built up sequentially
  26. during the process of parsing; the lower levels of the tree always
  27. come first in the result.
  28. Note that malloc's and realloc's in this file are transformed to
  29. xmalloc and xrealloc respectively by the same sed command in the
  30. makefile that remaps any other malloc/realloc inserted by the parser
  31. generator. Doing this with #defines and trying to control the interaction
  32. with include files (<malloc.h> and <stdlib.h> for example) just became
  33. too messy, particularly when such includes can be inserted at random
  34. times by the parser generator. */
  35. %{
  36. #include "defs.h"
  37. #include "gdb_string.h"
  38. #include "expression.h"
  39. #include "value.h"
  40. #include "parser-defs.h"
  41. #include "language.h"
  42. #include "f-lang.h"
  43. #include "bfd.h" /* Required by objfiles.h. */
  44. #include "symfile.h" /* Required by objfiles.h. */
  45. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  46. #include "block.h"
  47. #include <ctype.h>
  48. #define parse_type builtin_type (parse_gdbarch)
  49. #define parse_f_type builtin_f_type (parse_gdbarch)
  50. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  51. as well as gratuitiously global symbol names, so we can have multiple
  52. yacc generated parsers in gdb. Note that these are only the variables
  53. produced by yacc. If other parser generators (bison, byacc, etc) produce
  54. additional global names that conflict at link time, then those parser
  55. generators need to be fixed instead of adding those names to this list. */
  56. #define yymaxdepth f_maxdepth
  57. #define yyparse f_parse
  58. #define yylex f_lex
  59. #define yyerror f_error
  60. #define yylval f_lval
  61. #define yychar f_char
  62. #define yydebug f_debug
  63. #define yypact f_pact
  64. #define yyr1 f_r1
  65. #define yyr2 f_r2
  66. #define yydef f_def
  67. #define yychk f_chk
  68. #define yypgo f_pgo
  69. #define yyact f_act
  70. #define yyexca f_exca
  71. #define yyerrflag f_errflag
  72. #define yynerrs f_nerrs
  73. #define yyps f_ps
  74. #define yypv f_pv
  75. #define yys f_s
  76. #define yy_yys f_yys
  77. #define yystate f_state
  78. #define yytmp f_tmp
  79. #define yyv f_v
  80. #define yy_yyv f_yyv
  81. #define yyval f_val
  82. #define yylloc f_lloc
  83. #define yyreds f_reds /* With YYDEBUG defined */
  84. #define yytoks f_toks /* With YYDEBUG defined */
  85. #define yyname f_name /* With YYDEBUG defined */
  86. #define yyrule f_rule /* With YYDEBUG defined */
  87. #define yylhs f_yylhs
  88. #define yylen f_yylen
  89. #define yydefred f_yydefred
  90. #define yydgoto f_yydgoto
  91. #define yysindex f_yysindex
  92. #define yyrindex f_yyrindex
  93. #define yygindex f_yygindex
  94. #define yytable f_yytable
  95. #define yycheck f_yycheck
  96. #ifndef YYDEBUG
  97. #define YYDEBUG 1 /* Default to yydebug support */
  98. #endif
  99. #define YYFPRINTF parser_fprintf
  100. int yyparse (void);
  101. static int yylex (void);
  102. void yyerror (char *);
  103. static void growbuf_by_size (int);
  104. static int match_string_literal (void);
  105. %}
  106. /* Although the yacc "value" of an expression is not used,
  107. since the result is stored in the structure being created,
  108. other node types do have values. */
  109. %union
  110. {
  111. LONGEST lval;
  112. struct {
  113. LONGEST val;
  114. struct type *type;
  115. } typed_val;
  116. DOUBLEST dval;
  117. struct symbol *sym;
  118. struct type *tval;
  119. struct stoken sval;
  120. struct ttype tsym;
  121. struct symtoken ssym;
  122. int voidval;
  123. struct block *bval;
  124. enum exp_opcode opcode;
  125. struct internalvar *ivar;
  126. struct type **tvec;
  127. int *ivec;
  128. }
  129. %{
  130. /* YYSTYPE gets defined by %union */
  131. static int parse_number (char *, int, int, YYSTYPE *);
  132. %}
  133. %type <voidval> exp type_exp start variable
  134. %type <tval> type typebase
  135. %type <tvec> nonempty_typelist
  136. /* %type <bval> block */
  137. /* Fancy type parsing. */
  138. %type <voidval> func_mod direct_abs_decl abs_decl
  139. %type <tval> ptype
  140. %token <typed_val> INT
  141. %token <dval> FLOAT
  142. /* Both NAME and TYPENAME tokens represent symbols in the input,
  143. and both convey their data as strings.
  144. But a TYPENAME is a string that happens to be defined as a typedef
  145. or builtin type name (such as int or char)
  146. and a NAME is any other symbol.
  147. Contexts where this distinction is not important can use the
  148. nonterminal "name", which matches either NAME or TYPENAME. */
  149. %token <sval> STRING_LITERAL
  150. %token <lval> BOOLEAN_LITERAL
  151. %token <ssym> NAME
  152. %token <tsym> TYPENAME
  153. %type <sval> name
  154. %type <ssym> name_not_typename
  155. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  156. but which would parse as a valid number in the current input radix.
  157. E.g. "c" when input_radix==16. Depending on the parse, it will be
  158. turned into a name or into a number. */
  159. %token <ssym> NAME_OR_INT
  160. %token SIZEOF
  161. %token ERROR
  162. /* Special type cases, put in to allow the parser to distinguish different
  163. legal basetypes. */
  164. %token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_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_KEYWORD
  505. { $$ = parse_f_type->builtin_logical; }
  506. | LOGICAL_S2_KEYWORD
  507. { $$ = parse_f_type->builtin_logical_s2; }
  508. | LOGICAL_S1_KEYWORD
  509. { $$ = parse_f_type->builtin_logical_s1; }
  510. | REAL_KEYWORD
  511. { $$ = parse_f_type->builtin_real; }
  512. | REAL_S8_KEYWORD
  513. { $$ = parse_f_type->builtin_real_s8; }
  514. | REAL_S16_KEYWORD
  515. { $$ = parse_f_type->builtin_real_s16; }
  516. | COMPLEX_S8_KEYWORD
  517. { $$ = parse_f_type->builtin_complex_s8; }
  518. | COMPLEX_S16_KEYWORD
  519. { $$ = parse_f_type->builtin_complex_s16; }
  520. | COMPLEX_S32_KEYWORD
  521. { $$ = parse_f_type->builtin_complex_s32; }
  522. ;
  523. nonempty_typelist
  524. : type
  525. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  526. $<ivec>$[0] = 1; /* Number of types in vector */
  527. $$[1] = $1;
  528. }
  529. | nonempty_typelist ',' type
  530. { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  531. $$ = (struct type **) realloc ((char *) $1, len);
  532. $$[$<ivec>$[0]] = $3;
  533. }
  534. ;
  535. name : NAME
  536. { $$ = $1.stoken; }
  537. ;
  538. name_not_typename : NAME
  539. /* These would be useful if name_not_typename was useful, but it is just
  540. a fake for "variable", so these cause reduce/reduce conflicts because
  541. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  542. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  543. context where only a name could occur, this might be useful.
  544. | NAME_OR_INT
  545. */
  546. ;
  547. %%
  548. /* Take care of parsing a number (anything that starts with a digit).
  549. Set yylval and return the token type; update lexptr.
  550. LEN is the number of characters in it. */
  551. /*** Needs some error checking for the float case ***/
  552. static int
  553. parse_number (p, len, parsed_float, putithere)
  554. char *p;
  555. int len;
  556. int parsed_float;
  557. YYSTYPE *putithere;
  558. {
  559. LONGEST n = 0;
  560. LONGEST prevn = 0;
  561. int c;
  562. int base = input_radix;
  563. int unsigned_p = 0;
  564. int long_p = 0;
  565. ULONGEST high_bit;
  566. struct type *signed_type;
  567. struct type *unsigned_type;
  568. if (parsed_float)
  569. {
  570. /* It's a float since it contains a point or an exponent. */
  571. /* [dD] is not understood as an exponent by atof, change it to 'e'. */
  572. char *tmp, *tmp2;
  573. tmp = xstrdup (p);
  574. for (tmp2 = tmp; *tmp2; ++tmp2)
  575. if (*tmp2 == 'd' || *tmp2 == 'D')
  576. *tmp2 = 'e';
  577. putithere->dval = atof (tmp);
  578. free (tmp);
  579. return FLOAT;
  580. }
  581. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  582. if (p[0] == '0')
  583. switch (p[1])
  584. {
  585. case 'x':
  586. case 'X':
  587. if (len >= 3)
  588. {
  589. p += 2;
  590. base = 16;
  591. len -= 2;
  592. }
  593. break;
  594. case 't':
  595. case 'T':
  596. case 'd':
  597. case 'D':
  598. if (len >= 3)
  599. {
  600. p += 2;
  601. base = 10;
  602. len -= 2;
  603. }
  604. break;
  605. default:
  606. base = 8;
  607. break;
  608. }
  609. while (len-- > 0)
  610. {
  611. c = *p++;
  612. if (isupper (c))
  613. c = tolower (c);
  614. if (len == 0 && c == 'l')
  615. long_p = 1;
  616. else if (len == 0 && c == 'u')
  617. unsigned_p = 1;
  618. else
  619. {
  620. int i;
  621. if (c >= '0' && c <= '9')
  622. i = c - '0';
  623. else if (c >= 'a' && c <= 'f')
  624. i = c - 'a' + 10;
  625. else
  626. return ERROR; /* Char not a digit */
  627. if (i >= base)
  628. return ERROR; /* Invalid digit in this base */
  629. n *= base;
  630. n += i;
  631. }
  632. /* Portably test for overflow (only works for nonzero values, so make
  633. a second check for zero). */
  634. if ((prevn >= n) && n != 0)
  635. unsigned_p=1; /* Try something unsigned */
  636. /* If range checking enabled, portably test for unsigned overflow. */
  637. if (RANGE_CHECK && n != 0)
  638. {
  639. if ((unsigned_p && (unsigned)prevn >= (unsigned)n))
  640. range_error("Overflow on numeric constant.");
  641. }
  642. prevn = n;
  643. }
  644. /* If the number is too big to be an int, or it's got an l suffix
  645. then it's a long. Work out if this has to be a long by
  646. shifting right and and seeing if anything remains, and the
  647. target int size is different to the target long size.
  648. In the expression below, we could have tested
  649. (n >> gdbarch_int_bit (parse_gdbarch))
  650. to see if it was zero,
  651. but too many compilers warn about that, when ints and longs
  652. are the same size. So we shift it twice, with fewer bits
  653. each time, for the same result. */
  654. if ((gdbarch_int_bit (parse_gdbarch) != gdbarch_long_bit (parse_gdbarch)
  655. && ((n >> 2)
  656. >> (gdbarch_int_bit (parse_gdbarch)-2))) /* Avoid shift warning */
  657. || long_p)
  658. {
  659. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch)-1);
  660. unsigned_type = parse_type->builtin_unsigned_long;
  661. signed_type = parse_type->builtin_long;
  662. }
  663. else
  664. {
  665. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch)-1);
  666. unsigned_type = parse_type->builtin_unsigned_int;
  667. signed_type = parse_type->builtin_int;
  668. }
  669. putithere->typed_val.val = n;
  670. /* If the high bit of the worked out type is set then this number
  671. has to be unsigned. */
  672. if (unsigned_p || (n & high_bit))
  673. putithere->typed_val.type = unsigned_type;
  674. else
  675. putithere->typed_val.type = signed_type;
  676. return INT;
  677. }
  678. struct token
  679. {
  680. char *operator;
  681. int token;
  682. enum exp_opcode opcode;
  683. };
  684. static const struct token dot_ops[] =
  685. {
  686. { ".and.", BOOL_AND, BINOP_END },
  687. { ".AND.", BOOL_AND, BINOP_END },
  688. { ".or.", BOOL_OR, BINOP_END },
  689. { ".OR.", BOOL_OR, BINOP_END },
  690. { ".not.", BOOL_NOT, BINOP_END },
  691. { ".NOT.", BOOL_NOT, BINOP_END },
  692. { ".eq.", EQUAL, BINOP_END },
  693. { ".EQ.", EQUAL, BINOP_END },
  694. { ".eqv.", EQUAL, BINOP_END },
  695. { ".NEQV.", NOTEQUAL, BINOP_END },
  696. { ".neqv.", NOTEQUAL, BINOP_END },
  697. { ".EQV.", EQUAL, BINOP_END },
  698. { ".ne.", NOTEQUAL, BINOP_END },
  699. { ".NE.", NOTEQUAL, BINOP_END },
  700. { ".le.", LEQ, BINOP_END },
  701. { ".LE.", LEQ, BINOP_END },
  702. { ".ge.", GEQ, BINOP_END },
  703. { ".GE.", GEQ, BINOP_END },
  704. { ".gt.", GREATERTHAN, BINOP_END },
  705. { ".GT.", GREATERTHAN, BINOP_END },
  706. { ".lt.", LESSTHAN, BINOP_END },
  707. { ".LT.", LESSTHAN, BINOP_END },
  708. { NULL, 0, 0 }
  709. };
  710. struct f77_boolean_val
  711. {
  712. char *name;
  713. int value;
  714. };
  715. static const struct f77_boolean_val boolean_values[] =
  716. {
  717. { ".true.", 1 },
  718. { ".TRUE.", 1 },
  719. { ".false.", 0 },
  720. { ".FALSE.", 0 },
  721. { NULL, 0 }
  722. };
  723. static const struct token f77_keywords[] =
  724. {
  725. { "complex_16", COMPLEX_S16_KEYWORD, BINOP_END },
  726. { "complex_32", COMPLEX_S32_KEYWORD, BINOP_END },
  727. { "character", CHARACTER, BINOP_END },
  728. { "integer_2", INT_S2_KEYWORD, BINOP_END },
  729. { "logical_1", LOGICAL_S1_KEYWORD, BINOP_END },
  730. { "logical_2", LOGICAL_S2_KEYWORD, BINOP_END },
  731. { "complex_8", COMPLEX_S8_KEYWORD, BINOP_END },
  732. { "integer", INT_KEYWORD, BINOP_END },
  733. { "logical", LOGICAL_KEYWORD, BINOP_END },
  734. { "real_16", REAL_S16_KEYWORD, BINOP_END },
  735. { "complex", COMPLEX_S8_KEYWORD, BINOP_END },
  736. { "sizeof", SIZEOF, BINOP_END },
  737. { "real_8", REAL_S8_KEYWORD, BINOP_END },
  738. { "real", REAL_KEYWORD, BINOP_END },
  739. { NULL, 0, 0 }
  740. };
  741. /* Implementation of a dynamically expandable buffer for processing input
  742. characters acquired through lexptr and building a value to return in
  743. yylval. Ripped off from ch-exp.y */
  744. static char *tempbuf; /* Current buffer contents */
  745. static int tempbufsize; /* Size of allocated buffer */
  746. static int tempbufindex; /* Current index into buffer */
  747. #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
  748. #define CHECKBUF(size) \
  749. do { \
  750. if (tempbufindex + (size) >= tempbufsize) \
  751. { \
  752. growbuf_by_size (size); \
  753. } \
  754. } while (0);
  755. /* Grow the static temp buffer if necessary, including allocating the first one
  756. on demand. */
  757. static void
  758. growbuf_by_size (count)
  759. int count;
  760. {
  761. int growby;
  762. growby = max (count, GROWBY_MIN_SIZE);
  763. tempbufsize += growby;
  764. if (tempbuf == NULL)
  765. tempbuf = (char *) malloc (tempbufsize);
  766. else
  767. tempbuf = (char *) realloc (tempbuf, tempbufsize);
  768. }
  769. /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
  770. string-literals.
  771. Recognize a string literal. A string literal is a nonzero sequence
  772. of characters enclosed in matching single quotes, except that
  773. a single character inside single quotes is a character literal, which
  774. we reject as a string literal. To embed the terminator character inside
  775. a string, it is simply doubled (I.E. 'this''is''one''string') */
  776. static int
  777. match_string_literal ()
  778. {
  779. char *tokptr = lexptr;
  780. for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
  781. {
  782. CHECKBUF (1);
  783. if (*tokptr == *lexptr)
  784. {
  785. if (*(tokptr + 1) == *lexptr)
  786. tokptr++;
  787. else
  788. break;
  789. }
  790. tempbuf[tempbufindex++] = *tokptr;
  791. }
  792. if (*tokptr == '\0' /* no terminator */
  793. || tempbufindex == 0) /* no string */
  794. return 0;
  795. else
  796. {
  797. tempbuf[tempbufindex] = '\0';
  798. yylval.sval.ptr = tempbuf;
  799. yylval.sval.length = tempbufindex;
  800. lexptr = ++tokptr;
  801. return STRING_LITERAL;
  802. }
  803. }
  804. /* Read one token, getting characters through lexptr. */
  805. static int
  806. yylex ()
  807. {
  808. int c;
  809. int namelen;
  810. unsigned int i,token;
  811. char *tokstart;
  812. retry:
  813. prev_lexptr = lexptr;
  814. tokstart = lexptr;
  815. /* First of all, let us make sure we are not dealing with the
  816. special tokens .true. and .false. which evaluate to 1 and 0. */
  817. if (*lexptr == '.')
  818. {
  819. for (i = 0; boolean_values[i].name != NULL; i++)
  820. {
  821. if (strncmp (tokstart, boolean_values[i].name,
  822. strlen (boolean_values[i].name)) == 0)
  823. {
  824. lexptr += strlen (boolean_values[i].name);
  825. yylval.lval = boolean_values[i].value;
  826. return BOOLEAN_LITERAL;
  827. }
  828. }
  829. }
  830. /* See if it is a special .foo. operator. */
  831. for (i = 0; dot_ops[i].operator != NULL; i++)
  832. if (strncmp (tokstart, dot_ops[i].operator, strlen (dot_ops[i].operator)) == 0)
  833. {
  834. lexptr += strlen (dot_ops[i].operator);
  835. yylval.opcode = dot_ops[i].opcode;
  836. return dot_ops[i].token;
  837. }
  838. /* See if it is an exponentiation operator. */
  839. if (strncmp (tokstart, "**", 2) == 0)
  840. {
  841. lexptr += 2;
  842. yylval.opcode = BINOP_EXP;
  843. return STARSTAR;
  844. }
  845. switch (c = *tokstart)
  846. {
  847. case 0:
  848. return 0;
  849. case ' ':
  850. case '\t':
  851. case '\n':
  852. lexptr++;
  853. goto retry;
  854. case '\'':
  855. token = match_string_literal ();
  856. if (token != 0)
  857. return (token);
  858. break;
  859. case '(':
  860. paren_depth++;
  861. lexptr++;
  862. return c;
  863. case ')':
  864. if (paren_depth == 0)
  865. return 0;
  866. paren_depth--;
  867. lexptr++;
  868. return c;
  869. case ',':
  870. if (comma_terminates && paren_depth == 0)
  871. return 0;
  872. lexptr++;
  873. return c;
  874. case '.':
  875. /* Might be a floating point number. */
  876. if (lexptr[1] < '0' || lexptr[1] > '9')
  877. goto symbol; /* Nope, must be a symbol. */
  878. /* FALL THRU into number case. */
  879. case '0':
  880. case '1':
  881. case '2':
  882. case '3':
  883. case '4':
  884. case '5':
  885. case '6':
  886. case '7':
  887. case '8':
  888. case '9':
  889. {
  890. /* It's a number. */
  891. int got_dot = 0, got_e = 0, got_d = 0, toktype;
  892. char *p = tokstart;
  893. int hex = input_radix > 10;
  894. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  895. {
  896. p += 2;
  897. hex = 1;
  898. }
  899. else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  900. {
  901. p += 2;
  902. hex = 0;
  903. }
  904. for (;; ++p)
  905. {
  906. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  907. got_dot = got_e = 1;
  908. else if (!hex && !got_d && (*p == 'd' || *p == 'D'))
  909. got_dot = got_d = 1;
  910. else if (!hex && !got_dot && *p == '.')
  911. got_dot = 1;
  912. else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
  913. || (got_d && (p[-1] == 'd' || p[-1] == 'D')))
  914. && (*p == '-' || *p == '+'))
  915. /* This is the sign of the exponent, not the end of the
  916. number. */
  917. continue;
  918. /* We will take any letters or digits. parse_number will
  919. complain if past the radix, or if L or U are not final. */
  920. else if ((*p < '0' || *p > '9')
  921. && ((*p < 'a' || *p > 'z')
  922. && (*p < 'A' || *p > 'Z')))
  923. break;
  924. }
  925. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e|got_d,
  926. &yylval);
  927. if (toktype == ERROR)
  928. {
  929. char *err_copy = (char *) alloca (p - tokstart + 1);
  930. memcpy (err_copy, tokstart, p - tokstart);
  931. err_copy[p - tokstart] = 0;
  932. error ("Invalid number \"%s\".", err_copy);
  933. }
  934. lexptr = p;
  935. return toktype;
  936. }
  937. case '+':
  938. case '-':
  939. case '*':
  940. case '/':
  941. case '%':
  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. symbol:
  958. lexptr++;
  959. return c;
  960. }
  961. if (!(c == '_' || c == '$'
  962. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  963. /* We must have come across a bad character (e.g. ';'). */
  964. error ("Invalid character '%c' in expression.", c);
  965. namelen = 0;
  966. for (c = tokstart[namelen];
  967. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  968. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  969. c = tokstart[++namelen]);
  970. /* The token "if" terminates the expression and is NOT
  971. removed from the input stream. */
  972. if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  973. return 0;
  974. lexptr += namelen;
  975. /* Catch specific keywords. */
  976. for (i = 0; f77_keywords[i].operator != NULL; i++)
  977. if (strncmp (tokstart, f77_keywords[i].operator,
  978. strlen(f77_keywords[i].operator)) == 0)
  979. {
  980. /* lexptr += strlen(f77_keywords[i].operator); */
  981. yylval.opcode = f77_keywords[i].opcode;
  982. return f77_keywords[i].token;
  983. }
  984. yylval.sval.ptr = tokstart;
  985. yylval.sval.length = namelen;
  986. if (*tokstart == '$')
  987. {
  988. write_dollar_variable (yylval.sval);
  989. return VARIABLE;
  990. }
  991. /* Use token-type TYPENAME for symbols that happen to be defined
  992. currently as names of types; NAME for other symbols.
  993. The caller is not constrained to care about the distinction. */
  994. {
  995. char *tmp = copy_name (yylval.sval);
  996. struct symbol *sym;
  997. int is_a_field_of_this = 0;
  998. int hextype;
  999. sym = lookup_symbol (tmp, expression_context_block,
  1000. VAR_DOMAIN,
  1001. parse_language->la_language == language_cplus
  1002. ? &is_a_field_of_this : NULL);
  1003. if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1004. {
  1005. yylval.tsym.type = SYMBOL_TYPE (sym);
  1006. return TYPENAME;
  1007. }
  1008. yylval.tsym.type
  1009. = language_lookup_primitive_type_by_name (parse_language,
  1010. parse_gdbarch, tmp);
  1011. if (yylval.tsym.type != NULL)
  1012. return TYPENAME;
  1013. /* Input names that aren't symbols but ARE valid hex numbers,
  1014. when the input radix permits them, can be names or numbers
  1015. depending on the parse. Note we support radixes > 16 here. */
  1016. if (!sym
  1017. && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1018. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1019. {
  1020. YYSTYPE newlval; /* Its value is ignored. */
  1021. hextype = parse_number (tokstart, namelen, 0, &newlval);
  1022. if (hextype == INT)
  1023. {
  1024. yylval.ssym.sym = sym;
  1025. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1026. return NAME_OR_INT;
  1027. }
  1028. }
  1029. /* Any other kind of symbol */
  1030. yylval.ssym.sym = sym;
  1031. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1032. return NAME;
  1033. }
  1034. }
  1035. void
  1036. yyerror (msg)
  1037. char *msg;
  1038. {
  1039. if (prev_lexptr)
  1040. lexptr = prev_lexptr;
  1041. error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
  1042. }