PageRenderTime 200ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
Happy | 2473 lines | 2169 code | 304 blank | 0 comment | 0 complexity | 66cbfb99025f09d78090830f55f8f89f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1
  1. /* YACC parser for C expressions, for GDB.
  2. Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
  3. 1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010
  4. Free Software Foundation, Inc.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Parse a C expression from text in a string,
  17. and return the result as a struct expression pointer.
  18. That structure contains arithmetic operations in reverse polish,
  19. with constants represented by operations that are followed by special data.
  20. See expression.h for the details of the format.
  21. What is important here is that it can be built up sequentially
  22. during the process of parsing; the lower levels of the tree always
  23. come first in the result.
  24. Note that malloc's and realloc's in this file are transformed to
  25. xmalloc and xrealloc respectively by the same sed command in the
  26. makefile that remaps any other malloc/realloc inserted by the parser
  27. generator. Doing this with #defines and trying to control the interaction
  28. with include files (<malloc.h> and <stdlib.h> for example) just became
  29. too messy, particularly when such includes can be inserted at random
  30. times by the parser generator. */
  31. %{
  32. #include "defs.h"
  33. #include "gdb_string.h"
  34. #include <ctype.h>
  35. #include "expression.h"
  36. #include "value.h"
  37. #include "parser-defs.h"
  38. #include "language.h"
  39. #include "c-lang.h"
  40. #include "bfd.h" /* Required by objfiles.h. */
  41. #include "symfile.h" /* Required by objfiles.h. */
  42. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  43. #include "charset.h"
  44. #include "block.h"
  45. #include "cp-support.h"
  46. #include "dfp.h"
  47. #include "gdb_assert.h"
  48. #include "macroscope.h"
  49. #define parse_type builtin_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 c_maxdepth
  57. #define yyparse c_parse_internal
  58. #define yylex c_lex
  59. #define yyerror c_error
  60. #define yylval c_lval
  61. #define yychar c_char
  62. #define yydebug c_debug
  63. #define yypact c_pact
  64. #define yyr1 c_r1
  65. #define yyr2 c_r2
  66. #define yydef c_def
  67. #define yychk c_chk
  68. #define yypgo c_pgo
  69. #define yyact c_act
  70. #define yyexca c_exca
  71. #define yyerrflag c_errflag
  72. #define yynerrs c_nerrs
  73. #define yyps c_ps
  74. #define yypv c_pv
  75. #define yys c_s
  76. #define yy_yys c_yys
  77. #define yystate c_state
  78. #define yytmp c_tmp
  79. #define yyv c_v
  80. #define yy_yyv c_yyv
  81. #define yyval c_val
  82. #define yylloc c_lloc
  83. #define yyreds c_reds /* With YYDEBUG defined */
  84. #define yytoks c_toks /* With YYDEBUG defined */
  85. #define yyname c_name /* With YYDEBUG defined */
  86. #define yyrule c_rule /* With YYDEBUG defined */
  87. #define yylhs c_yylhs
  88. #define yylen c_yylen
  89. #define yydefred c_yydefred
  90. #define yydgoto c_yydgoto
  91. #define yysindex c_yysindex
  92. #define yyrindex c_yyrindex
  93. #define yygindex c_yygindex
  94. #define yytable c_yytable
  95. #define yycheck c_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. %}
  104. /* Although the yacc "value" of an expression is not used,
  105. since the result is stored in the structure being created,
  106. other node types do have values. */
  107. %union
  108. {
  109. LONGEST lval;
  110. struct {
  111. LONGEST val;
  112. struct type *type;
  113. } typed_val_int;
  114. struct {
  115. DOUBLEST dval;
  116. struct type *type;
  117. } typed_val_float;
  118. struct {
  119. gdb_byte val[16];
  120. struct type *type;
  121. } typed_val_decfloat;
  122. struct symbol *sym;
  123. struct type *tval;
  124. struct stoken sval;
  125. struct typed_stoken tsval;
  126. struct ttype tsym;
  127. struct symtoken ssym;
  128. int voidval;
  129. struct block *bval;
  130. enum exp_opcode opcode;
  131. struct internalvar *ivar;
  132. struct stoken_vector svec;
  133. struct type **tvec;
  134. int *ivec;
  135. }
  136. %{
  137. /* YYSTYPE gets defined by %union */
  138. static int parse_number (char *, int, int, YYSTYPE *);
  139. static struct stoken operator_stoken (const char *);
  140. %}
  141. %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
  142. %type <lval> rcurly
  143. %type <tval> type typebase qualified_type
  144. %type <tvec> nonempty_typelist
  145. /* %type <bval> block */
  146. /* Fancy type parsing. */
  147. %type <voidval> func_mod direct_abs_decl abs_decl
  148. %type <tval> ptype
  149. %type <lval> array_mod
  150. %token <typed_val_int> INT
  151. %token <typed_val_float> FLOAT
  152. %token <typed_val_decfloat> DECFLOAT
  153. /* Both NAME and TYPENAME tokens represent symbols in the input,
  154. and both convey their data as strings.
  155. But a TYPENAME is a string that happens to be defined as a typedef
  156. or builtin type name (such as int or char)
  157. and a NAME is any other symbol.
  158. Contexts where this distinction is not important can use the
  159. nonterminal "name", which matches either NAME or TYPENAME. */
  160. %token <tsval> STRING
  161. %token <tsval> CHAR
  162. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
  163. %token <voidval> COMPLETE
  164. %token <tsym> TYPENAME
  165. %type <sval> name
  166. %type <svec> string_exp
  167. %type <ssym> name_not_typename
  168. %type <tsym> typename
  169. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  170. but which would parse as a valid number in the current input radix.
  171. E.g. "c" when input_radix==16. Depending on the parse, it will be
  172. turned into a name or into a number. */
  173. %token <ssym> NAME_OR_INT
  174. %token OPERATOR
  175. %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
  176. %token TEMPLATE
  177. %token ERROR
  178. %token NEW DELETE
  179. %type <sval> operator
  180. %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
  181. /* Special type cases, put in to allow the parser to distinguish different
  182. legal basetypes. */
  183. %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
  184. %token <voidval> VARIABLE
  185. %token <opcode> ASSIGN_MODIFY
  186. /* C++ */
  187. %token TRUEKEYWORD
  188. %token FALSEKEYWORD
  189. %left ','
  190. %left ABOVE_COMMA
  191. %right '=' ASSIGN_MODIFY
  192. %right '?'
  193. %left OROR
  194. %left ANDAND
  195. %left '|'
  196. %left '^'
  197. %left '&'
  198. %left EQUAL NOTEQUAL
  199. %left '<' '>' LEQ GEQ
  200. %left LSH RSH
  201. %left '@'
  202. %left '+' '-'
  203. %left '*' '/' '%'
  204. %right UNARY INCREMENT DECREMENT
  205. %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
  206. %token <ssym> BLOCKNAME
  207. %token <bval> FILENAME
  208. %type <bval> block
  209. %left COLONCOLON
  210. %%
  211. start : exp1
  212. | type_exp
  213. ;
  214. type_exp: type
  215. { write_exp_elt_opcode(OP_TYPE);
  216. write_exp_elt_type($1);
  217. write_exp_elt_opcode(OP_TYPE);}
  218. ;
  219. /* Expressions, including the comma operator. */
  220. exp1 : exp
  221. | exp1 ',' exp
  222. { write_exp_elt_opcode (BINOP_COMMA); }
  223. ;
  224. /* Expressions, not including the comma operator. */
  225. exp : '*' exp %prec UNARY
  226. { write_exp_elt_opcode (UNOP_IND); }
  227. ;
  228. exp : '&' exp %prec UNARY
  229. { write_exp_elt_opcode (UNOP_ADDR); }
  230. ;
  231. exp : '-' exp %prec UNARY
  232. { write_exp_elt_opcode (UNOP_NEG); }
  233. ;
  234. exp : '+' exp %prec UNARY
  235. { write_exp_elt_opcode (UNOP_PLUS); }
  236. ;
  237. exp : '!' exp %prec UNARY
  238. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  239. ;
  240. exp : '~' exp %prec UNARY
  241. { write_exp_elt_opcode (UNOP_COMPLEMENT); }
  242. ;
  243. exp : INCREMENT exp %prec UNARY
  244. { write_exp_elt_opcode (UNOP_PREINCREMENT); }
  245. ;
  246. exp : DECREMENT exp %prec UNARY
  247. { write_exp_elt_opcode (UNOP_PREDECREMENT); }
  248. ;
  249. exp : exp INCREMENT %prec UNARY
  250. { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
  251. ;
  252. exp : exp DECREMENT %prec UNARY
  253. { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
  254. ;
  255. exp : SIZEOF exp %prec UNARY
  256. { write_exp_elt_opcode (UNOP_SIZEOF); }
  257. ;
  258. exp : exp ARROW name
  259. { write_exp_elt_opcode (STRUCTOP_PTR);
  260. write_exp_string ($3);
  261. write_exp_elt_opcode (STRUCTOP_PTR); }
  262. ;
  263. exp : exp ARROW name COMPLETE
  264. { mark_struct_expression ();
  265. write_exp_elt_opcode (STRUCTOP_PTR);
  266. write_exp_string ($3);
  267. write_exp_elt_opcode (STRUCTOP_PTR); }
  268. ;
  269. exp : exp ARROW COMPLETE
  270. { struct stoken s;
  271. mark_struct_expression ();
  272. write_exp_elt_opcode (STRUCTOP_PTR);
  273. s.ptr = "";
  274. s.length = 0;
  275. write_exp_string (s);
  276. write_exp_elt_opcode (STRUCTOP_PTR); }
  277. ;
  278. exp : exp ARROW qualified_name
  279. { /* exp->type::name becomes exp->*(&type::name) */
  280. /* Note: this doesn't work if name is a
  281. static member! FIXME */
  282. write_exp_elt_opcode (UNOP_ADDR);
  283. write_exp_elt_opcode (STRUCTOP_MPTR); }
  284. ;
  285. exp : exp ARROW_STAR exp
  286. { write_exp_elt_opcode (STRUCTOP_MPTR); }
  287. ;
  288. exp : exp '.' name
  289. { write_exp_elt_opcode (STRUCTOP_STRUCT);
  290. write_exp_string ($3);
  291. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  292. ;
  293. exp : exp '.' name COMPLETE
  294. { mark_struct_expression ();
  295. write_exp_elt_opcode (STRUCTOP_STRUCT);
  296. write_exp_string ($3);
  297. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  298. ;
  299. exp : exp '.' COMPLETE
  300. { struct stoken s;
  301. mark_struct_expression ();
  302. write_exp_elt_opcode (STRUCTOP_STRUCT);
  303. s.ptr = "";
  304. s.length = 0;
  305. write_exp_string (s);
  306. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  307. ;
  308. exp : exp '.' qualified_name
  309. { /* exp.type::name becomes exp.*(&type::name) */
  310. /* Note: this doesn't work if name is a
  311. static member! FIXME */
  312. write_exp_elt_opcode (UNOP_ADDR);
  313. write_exp_elt_opcode (STRUCTOP_MEMBER); }
  314. ;
  315. exp : exp DOT_STAR exp
  316. { write_exp_elt_opcode (STRUCTOP_MEMBER); }
  317. ;
  318. exp : exp '[' exp1 ']'
  319. { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
  320. ;
  321. exp : exp '('
  322. /* This is to save the value of arglist_len
  323. being accumulated by an outer function call. */
  324. { start_arglist (); }
  325. arglist ')' %prec ARROW
  326. { write_exp_elt_opcode (OP_FUNCALL);
  327. write_exp_elt_longcst ((LONGEST) end_arglist ());
  328. write_exp_elt_opcode (OP_FUNCALL); }
  329. ;
  330. lcurly : '{'
  331. { start_arglist (); }
  332. ;
  333. arglist :
  334. ;
  335. arglist : exp
  336. { arglist_len = 1; }
  337. ;
  338. arglist : arglist ',' exp %prec ABOVE_COMMA
  339. { arglist_len++; }
  340. ;
  341. exp : exp '(' nonempty_typelist ')' const_or_volatile
  342. { int i;
  343. write_exp_elt_opcode (TYPE_INSTANCE);
  344. write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
  345. for (i = 0; i < $<ivec>3[0]; ++i)
  346. write_exp_elt_type ($<tvec>3[i + 1]);
  347. write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
  348. write_exp_elt_opcode (TYPE_INSTANCE);
  349. free ($3);
  350. }
  351. ;
  352. rcurly : '}'
  353. { $$ = end_arglist () - 1; }
  354. ;
  355. exp : lcurly arglist rcurly %prec ARROW
  356. { write_exp_elt_opcode (OP_ARRAY);
  357. write_exp_elt_longcst ((LONGEST) 0);
  358. write_exp_elt_longcst ((LONGEST) $3);
  359. write_exp_elt_opcode (OP_ARRAY); }
  360. ;
  361. exp : lcurly type rcurly exp %prec UNARY
  362. { write_exp_elt_opcode (UNOP_MEMVAL);
  363. write_exp_elt_type ($2);
  364. write_exp_elt_opcode (UNOP_MEMVAL); }
  365. ;
  366. exp : '(' type ')' exp %prec UNARY
  367. { write_exp_elt_opcode (UNOP_CAST);
  368. write_exp_elt_type ($2);
  369. write_exp_elt_opcode (UNOP_CAST); }
  370. ;
  371. exp : '(' exp1 ')'
  372. { }
  373. ;
  374. /* Binary operators in order of decreasing precedence. */
  375. exp : exp '@' exp
  376. { write_exp_elt_opcode (BINOP_REPEAT); }
  377. ;
  378. exp : exp '*' exp
  379. { write_exp_elt_opcode (BINOP_MUL); }
  380. ;
  381. exp : exp '/' exp
  382. { write_exp_elt_opcode (BINOP_DIV); }
  383. ;
  384. exp : exp '%' exp
  385. { write_exp_elt_opcode (BINOP_REM); }
  386. ;
  387. exp : exp '+' exp
  388. { write_exp_elt_opcode (BINOP_ADD); }
  389. ;
  390. exp : exp '-' exp
  391. { write_exp_elt_opcode (BINOP_SUB); }
  392. ;
  393. exp : exp LSH exp
  394. { write_exp_elt_opcode (BINOP_LSH); }
  395. ;
  396. exp : exp RSH exp
  397. { write_exp_elt_opcode (BINOP_RSH); }
  398. ;
  399. exp : exp EQUAL exp
  400. { write_exp_elt_opcode (BINOP_EQUAL); }
  401. ;
  402. exp : exp NOTEQUAL exp
  403. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  404. ;
  405. exp : exp LEQ exp
  406. { write_exp_elt_opcode (BINOP_LEQ); }
  407. ;
  408. exp : exp GEQ exp
  409. { write_exp_elt_opcode (BINOP_GEQ); }
  410. ;
  411. exp : exp '<' exp
  412. { write_exp_elt_opcode (BINOP_LESS); }
  413. ;
  414. exp : exp '>' exp
  415. { write_exp_elt_opcode (BINOP_GTR); }
  416. ;
  417. exp : exp '&' exp
  418. { write_exp_elt_opcode (BINOP_BITWISE_AND); }
  419. ;
  420. exp : exp '^' exp
  421. { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
  422. ;
  423. exp : exp '|' exp
  424. { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
  425. ;
  426. exp : exp ANDAND exp
  427. { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
  428. ;
  429. exp : exp OROR exp
  430. { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
  431. ;
  432. exp : exp '?' exp ':' exp %prec '?'
  433. { write_exp_elt_opcode (TERNOP_COND); }
  434. ;
  435. exp : exp '=' exp
  436. { write_exp_elt_opcode (BINOP_ASSIGN); }
  437. ;
  438. exp : exp ASSIGN_MODIFY exp
  439. { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  440. write_exp_elt_opcode ($2);
  441. write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  442. ;
  443. exp : INT
  444. { write_exp_elt_opcode (OP_LONG);
  445. write_exp_elt_type ($1.type);
  446. write_exp_elt_longcst ((LONGEST)($1.val));
  447. write_exp_elt_opcode (OP_LONG); }
  448. ;
  449. exp : CHAR
  450. {
  451. struct stoken_vector vec;
  452. vec.len = 1;
  453. vec.tokens = &$1;
  454. write_exp_string_vector ($1.type, &vec);
  455. }
  456. ;
  457. exp : NAME_OR_INT
  458. { YYSTYPE val;
  459. parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
  460. write_exp_elt_opcode (OP_LONG);
  461. write_exp_elt_type (val.typed_val_int.type);
  462. write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
  463. write_exp_elt_opcode (OP_LONG);
  464. }
  465. ;
  466. exp : FLOAT
  467. { write_exp_elt_opcode (OP_DOUBLE);
  468. write_exp_elt_type ($1.type);
  469. write_exp_elt_dblcst ($1.dval);
  470. write_exp_elt_opcode (OP_DOUBLE); }
  471. ;
  472. exp : DECFLOAT
  473. { write_exp_elt_opcode (OP_DECFLOAT);
  474. write_exp_elt_type ($1.type);
  475. write_exp_elt_decfloatcst ($1.val);
  476. write_exp_elt_opcode (OP_DECFLOAT); }
  477. ;
  478. exp : variable
  479. ;
  480. exp : VARIABLE
  481. /* Already written by write_dollar_variable. */
  482. ;
  483. exp : SIZEOF '(' type ')' %prec UNARY
  484. { write_exp_elt_opcode (OP_LONG);
  485. write_exp_elt_type (parse_type->builtin_int);
  486. CHECK_TYPEDEF ($3);
  487. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  488. write_exp_elt_opcode (OP_LONG); }
  489. ;
  490. exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
  491. { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
  492. write_exp_elt_type ($3);
  493. write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
  494. ;
  495. exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
  496. { write_exp_elt_opcode (UNOP_CAST);
  497. write_exp_elt_type ($3);
  498. write_exp_elt_opcode (UNOP_CAST); }
  499. ;
  500. exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
  501. { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
  502. write_exp_elt_type ($3);
  503. write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
  504. ;
  505. exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
  506. { /* We could do more error checking here, but
  507. it doesn't seem worthwhile. */
  508. write_exp_elt_opcode (UNOP_CAST);
  509. write_exp_elt_type ($3);
  510. write_exp_elt_opcode (UNOP_CAST); }
  511. ;
  512. string_exp:
  513. STRING
  514. {
  515. /* We copy the string here, and not in the
  516. lexer, to guarantee that we do not leak a
  517. string. Note that we follow the
  518. NUL-termination convention of the
  519. lexer. */
  520. struct typed_stoken *vec = XNEW (struct typed_stoken);
  521. $$.len = 1;
  522. $$.tokens = vec;
  523. vec->type = $1.type;
  524. vec->length = $1.length;
  525. vec->ptr = malloc ($1.length + 1);
  526. memcpy (vec->ptr, $1.ptr, $1.length + 1);
  527. }
  528. | string_exp STRING
  529. {
  530. /* Note that we NUL-terminate here, but just
  531. for convenience. */
  532. char *p;
  533. ++$$.len;
  534. $$.tokens = realloc ($$.tokens,
  535. $$.len * sizeof (struct typed_stoken));
  536. p = malloc ($2.length + 1);
  537. memcpy (p, $2.ptr, $2.length + 1);
  538. $$.tokens[$$.len - 1].type = $2.type;
  539. $$.tokens[$$.len - 1].length = $2.length;
  540. $$.tokens[$$.len - 1].ptr = p;
  541. }
  542. ;
  543. exp : string_exp
  544. {
  545. int i;
  546. enum c_string_type type = C_STRING;
  547. for (i = 0; i < $1.len; ++i)
  548. {
  549. switch ($1.tokens[i].type)
  550. {
  551. case C_STRING:
  552. break;
  553. case C_WIDE_STRING:
  554. case C_STRING_16:
  555. case C_STRING_32:
  556. if (type != C_STRING
  557. && type != $1.tokens[i].type)
  558. error ("Undefined string concatenation.");
  559. type = $1.tokens[i].type;
  560. break;
  561. default:
  562. /* internal error */
  563. internal_error (__FILE__, __LINE__,
  564. "unrecognized type in string concatenation");
  565. }
  566. }
  567. write_exp_string_vector (type, &$1);
  568. for (i = 0; i < $1.len; ++i)
  569. free ($1.tokens[i].ptr);
  570. free ($1.tokens);
  571. }
  572. ;
  573. /* C++. */
  574. exp : TRUEKEYWORD
  575. { write_exp_elt_opcode (OP_LONG);
  576. write_exp_elt_type (parse_type->builtin_bool);
  577. write_exp_elt_longcst ((LONGEST) 1);
  578. write_exp_elt_opcode (OP_LONG); }
  579. ;
  580. exp : FALSEKEYWORD
  581. { write_exp_elt_opcode (OP_LONG);
  582. write_exp_elt_type (parse_type->builtin_bool);
  583. write_exp_elt_longcst ((LONGEST) 0);
  584. write_exp_elt_opcode (OP_LONG); }
  585. ;
  586. /* end of C++. */
  587. block : BLOCKNAME
  588. {
  589. if ($1.sym)
  590. $$ = SYMBOL_BLOCK_VALUE ($1.sym);
  591. else
  592. error ("No file or function \"%s\".",
  593. copy_name ($1.stoken));
  594. }
  595. | FILENAME
  596. {
  597. $$ = $1;
  598. }
  599. ;
  600. block : block COLONCOLON name
  601. { struct symbol *tem
  602. = lookup_symbol (copy_name ($3), $1,
  603. VAR_DOMAIN, (int *) NULL);
  604. if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  605. error ("No function \"%s\" in specified context.",
  606. copy_name ($3));
  607. $$ = SYMBOL_BLOCK_VALUE (tem); }
  608. ;
  609. variable: block COLONCOLON name
  610. { struct symbol *sym;
  611. sym = lookup_symbol (copy_name ($3), $1,
  612. VAR_DOMAIN, (int *) NULL);
  613. if (sym == 0)
  614. error ("No symbol \"%s\" in specified context.",
  615. copy_name ($3));
  616. write_exp_elt_opcode (OP_VAR_VALUE);
  617. /* block_found is set by lookup_symbol. */
  618. write_exp_elt_block (block_found);
  619. write_exp_elt_sym (sym);
  620. write_exp_elt_opcode (OP_VAR_VALUE); }
  621. ;
  622. qualified_name: typebase COLONCOLON name
  623. {
  624. struct type *type = $1;
  625. CHECK_TYPEDEF (type);
  626. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  627. && TYPE_CODE (type) != TYPE_CODE_UNION
  628. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  629. error ("`%s' is not defined as an aggregate type.",
  630. TYPE_NAME (type));
  631. write_exp_elt_opcode (OP_SCOPE);
  632. write_exp_elt_type (type);
  633. write_exp_string ($3);
  634. write_exp_elt_opcode (OP_SCOPE);
  635. }
  636. | typebase COLONCOLON '~' name
  637. {
  638. struct type *type = $1;
  639. struct stoken tmp_token;
  640. CHECK_TYPEDEF (type);
  641. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  642. && TYPE_CODE (type) != TYPE_CODE_UNION
  643. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  644. error ("`%s' is not defined as an aggregate type.",
  645. TYPE_NAME (type));
  646. tmp_token.ptr = (char*) alloca ($4.length + 2);
  647. tmp_token.length = $4.length + 1;
  648. tmp_token.ptr[0] = '~';
  649. memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
  650. tmp_token.ptr[tmp_token.length] = 0;
  651. /* Check for valid destructor name. */
  652. destructor_name_p (tmp_token.ptr, type);
  653. write_exp_elt_opcode (OP_SCOPE);
  654. write_exp_elt_type (type);
  655. write_exp_string (tmp_token);
  656. write_exp_elt_opcode (OP_SCOPE);
  657. }
  658. ;
  659. variable: qualified_name
  660. | COLONCOLON name
  661. {
  662. char *name = copy_name ($2);
  663. struct symbol *sym;
  664. struct minimal_symbol *msymbol;
  665. sym =
  666. lookup_symbol (name, (const struct block *) NULL,
  667. VAR_DOMAIN, (int *) NULL);
  668. if (sym)
  669. {
  670. write_exp_elt_opcode (OP_VAR_VALUE);
  671. write_exp_elt_block (NULL);
  672. write_exp_elt_sym (sym);
  673. write_exp_elt_opcode (OP_VAR_VALUE);
  674. break;
  675. }
  676. msymbol = lookup_minimal_symbol (name, NULL, NULL);
  677. if (msymbol != NULL)
  678. write_exp_msymbol (msymbol);
  679. else if (!have_full_symbols () && !have_partial_symbols ())
  680. error ("No symbol table is loaded. Use the \"file\" command.");
  681. else
  682. error ("No symbol \"%s\" in current context.", name);
  683. }
  684. ;
  685. variable: name_not_typename
  686. { struct symbol *sym = $1.sym;
  687. if (sym)
  688. {
  689. if (symbol_read_needs_frame (sym))
  690. {
  691. if (innermost_block == 0
  692. || contained_in (block_found,
  693. innermost_block))
  694. innermost_block = block_found;
  695. }
  696. write_exp_elt_opcode (OP_VAR_VALUE);
  697. /* We want to use the selected frame, not
  698. another more inner frame which happens to
  699. be in the same block. */
  700. write_exp_elt_block (NULL);
  701. write_exp_elt_sym (sym);
  702. write_exp_elt_opcode (OP_VAR_VALUE);
  703. }
  704. else if ($1.is_a_field_of_this)
  705. {
  706. /* C++: it hangs off of `this'. Must
  707. not inadvertently convert from a method call
  708. to data ref. */
  709. if (innermost_block == 0
  710. || contained_in (block_found,
  711. innermost_block))
  712. innermost_block = block_found;
  713. write_exp_elt_opcode (OP_THIS);
  714. write_exp_elt_opcode (OP_THIS);
  715. write_exp_elt_opcode (STRUCTOP_PTR);
  716. write_exp_string ($1.stoken);
  717. write_exp_elt_opcode (STRUCTOP_PTR);
  718. }
  719. else
  720. {
  721. struct minimal_symbol *msymbol;
  722. char *arg = copy_name ($1.stoken);
  723. msymbol =
  724. lookup_minimal_symbol (arg, NULL, NULL);
  725. if (msymbol != NULL)
  726. write_exp_msymbol (msymbol);
  727. else if (!have_full_symbols () && !have_partial_symbols ())
  728. error ("No symbol table is loaded. Use the \"file\" command.");
  729. else
  730. error ("No symbol \"%s\" in current context.",
  731. copy_name ($1.stoken));
  732. }
  733. }
  734. ;
  735. space_identifier : '@' NAME
  736. { push_type_address_space (copy_name ($2.stoken));
  737. push_type (tp_space_identifier);
  738. }
  739. ;
  740. const_or_volatile: const_or_volatile_noopt
  741. |
  742. ;
  743. cv_with_space_id : const_or_volatile space_identifier const_or_volatile
  744. ;
  745. const_or_volatile_or_space_identifier_noopt: cv_with_space_id
  746. | const_or_volatile_noopt
  747. ;
  748. const_or_volatile_or_space_identifier:
  749. const_or_volatile_or_space_identifier_noopt
  750. |
  751. ;
  752. abs_decl: '*'
  753. { push_type (tp_pointer); $$ = 0; }
  754. | '*' abs_decl
  755. { push_type (tp_pointer); $$ = $2; }
  756. | '&'
  757. { push_type (tp_reference); $$ = 0; }
  758. | '&' abs_decl
  759. { push_type (tp_reference); $$ = $2; }
  760. | direct_abs_decl
  761. ;
  762. direct_abs_decl: '(' abs_decl ')'
  763. { $$ = $2; }
  764. | direct_abs_decl array_mod
  765. {
  766. push_type_int ($2);
  767. push_type (tp_array);
  768. }
  769. | array_mod
  770. {
  771. push_type_int ($1);
  772. push_type (tp_array);
  773. $$ = 0;
  774. }
  775. | direct_abs_decl func_mod
  776. { push_type (tp_function); }
  777. | func_mod
  778. { push_type (tp_function); }
  779. ;
  780. array_mod: '[' ']'
  781. { $$ = -1; }
  782. | '[' INT ']'
  783. { $$ = $2.val; }
  784. ;
  785. func_mod: '(' ')'
  786. { $$ = 0; }
  787. | '(' nonempty_typelist ')'
  788. { free ($2); $$ = 0; }
  789. ;
  790. /* We used to try to recognize pointer to member types here, but
  791. that didn't work (shift/reduce conflicts meant that these rules never
  792. got executed). The problem is that
  793. int (foo::bar::baz::bizzle)
  794. is a function type but
  795. int (foo::bar::baz::bizzle::*)
  796. is a pointer to member type. Stroustrup loses again! */
  797. type : ptype
  798. ;
  799. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  800. : TYPENAME
  801. { $$ = $1.type; }
  802. | INT_KEYWORD
  803. { $$ = parse_type->builtin_int; }
  804. | LONG
  805. { $$ = parse_type->builtin_long; }
  806. | SHORT
  807. { $$ = parse_type->builtin_short; }
  808. | LONG INT_KEYWORD
  809. { $$ = parse_type->builtin_long; }
  810. | LONG SIGNED_KEYWORD INT_KEYWORD
  811. { $$ = parse_type->builtin_long; }
  812. | LONG SIGNED_KEYWORD
  813. { $$ = parse_type->builtin_long; }
  814. | SIGNED_KEYWORD LONG INT_KEYWORD
  815. { $$ = parse_type->builtin_long; }
  816. | UNSIGNED LONG INT_KEYWORD
  817. { $$ = parse_type->builtin_unsigned_long; }
  818. | LONG UNSIGNED INT_KEYWORD
  819. { $$ = parse_type->builtin_unsigned_long; }
  820. | LONG UNSIGNED
  821. { $$ = parse_type->builtin_unsigned_long; }
  822. | LONG LONG
  823. { $$ = parse_type->builtin_long_long; }
  824. | LONG LONG INT_KEYWORD
  825. { $$ = parse_type->builtin_long_long; }
  826. | LONG LONG SIGNED_KEYWORD INT_KEYWORD
  827. { $$ = parse_type->builtin_long_long; }
  828. | LONG LONG SIGNED_KEYWORD
  829. { $$ = parse_type->builtin_long_long; }
  830. | SIGNED_KEYWORD LONG LONG
  831. { $$ = parse_type->builtin_long_long; }
  832. | SIGNED_KEYWORD LONG LONG INT_KEYWORD
  833. { $$ = parse_type->builtin_long_long; }
  834. | UNSIGNED LONG LONG
  835. { $$ = parse_type->builtin_unsigned_long_long; }
  836. | UNSIGNED LONG LONG INT_KEYWORD
  837. { $$ = parse_type->builtin_unsigned_long_long; }
  838. | LONG LONG UNSIGNED
  839. { $$ = parse_type->builtin_unsigned_long_long; }
  840. | LONG LONG UNSIGNED INT_KEYWORD
  841. { $$ = parse_type->builtin_unsigned_long_long; }
  842. | SHORT INT_KEYWORD
  843. { $$ = parse_type->builtin_short; }
  844. | SHORT SIGNED_KEYWORD INT_KEYWORD
  845. { $$ = parse_type->builtin_short; }
  846. | SHORT SIGNED_KEYWORD
  847. { $$ = parse_type->builtin_short; }
  848. | UNSIGNED SHORT INT_KEYWORD
  849. { $$ = parse_type->builtin_unsigned_short; }
  850. | SHORT UNSIGNED
  851. { $$ = parse_type->builtin_unsigned_short; }
  852. | SHORT UNSIGNED INT_KEYWORD
  853. { $$ = parse_type->builtin_unsigned_short; }
  854. | DOUBLE_KEYWORD
  855. { $$ = parse_type->builtin_double; }
  856. | LONG DOUBLE_KEYWORD
  857. { $$ = parse_type->builtin_long_double; }
  858. | STRUCT name
  859. { $$ = lookup_struct (copy_name ($2),
  860. expression_context_block); }
  861. | CLASS name
  862. { $$ = lookup_struct (copy_name ($2),
  863. expression_context_block); }
  864. | UNION name
  865. { $$ = lookup_union (copy_name ($2),
  866. expression_context_block); }
  867. | ENUM name
  868. { $$ = lookup_enum (copy_name ($2),
  869. expression_context_block); }
  870. | UNSIGNED typename
  871. { $$ = lookup_unsigned_typename (parse_language,
  872. parse_gdbarch,
  873. TYPE_NAME($2.type)); }
  874. | UNSIGNED
  875. { $$ = parse_type->builtin_unsigned_int; }
  876. | SIGNED_KEYWORD typename
  877. { $$ = lookup_signed_typename (parse_language,
  878. parse_gdbarch,
  879. TYPE_NAME($2.type)); }
  880. | SIGNED_KEYWORD
  881. { $$ = parse_type->builtin_int; }
  882. /* It appears that this rule for templates is never
  883. reduced; template recognition happens by lookahead
  884. in the token processing code in yylex. */
  885. | TEMPLATE name '<' type '>'
  886. { $$ = lookup_template_type(copy_name($2), $4,
  887. expression_context_block);
  888. }
  889. | const_or_volatile_or_space_identifier_noopt typebase
  890. { $$ = follow_types ($2); }
  891. | typebase const_or_volatile_or_space_identifier_noopt
  892. { $$ = follow_types ($1); }
  893. | qualified_type
  894. ;
  895. /* FIXME: carlton/2003-09-25: This next bit leads to lots of
  896. reduce-reduce conflicts, because the parser doesn't know whether or
  897. not to use qualified_name or qualified_type: the rules are
  898. identical. If the parser is parsing 'A::B::x', then, when it sees
  899. the second '::', it knows that the expression to the left of it has
  900. to be a type, so it uses qualified_type. But if it is parsing just
  901. 'A::B', then it doesn't have any way of knowing which rule to use,
  902. so there's a reduce-reduce conflict; it picks qualified_name, since
  903. that occurs earlier in this file than qualified_type.
  904. There's no good way to fix this with the grammar as it stands; as
  905. far as I can tell, some of the problems arise from ambiguities that
  906. GDB introduces ('start' can be either an expression or a type), but
  907. some of it is inherent to the nature of C++ (you want to treat the
  908. input "(FOO)" fairly differently depending on whether FOO is an
  909. expression or a type, and if FOO is a complex expression, this can
  910. be hard to determine at the right time). Fortunately, it works
  911. pretty well in most cases. For example, if you do 'ptype A::B',
  912. where A::B is a nested type, then the parser will mistakenly
  913. misidentify it as an expression; but evaluate_subexp will get
  914. called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
  915. will work out anyways. But there are situations where the parser
  916. will get confused: the most common one that I've run into is when
  917. you want to do
  918. print *((A::B *) x)"
  919. where the parser doesn't realize that A::B has to be a type until
  920. it hits the first right paren, at which point it's too late. (The
  921. workaround is to type "print *(('A::B' *) x)" instead.) (And
  922. another solution is to fix our symbol-handling code so that the
  923. user never wants to type something like that in the first place,
  924. because we get all the types right without the user's help!)
  925. Perhaps we could fix this by making the lexer smarter. Some of
  926. this functionality used to be in the lexer, but in a way that
  927. worked even less well than the current solution: that attempt
  928. involved having the parser sometimes handle '::' and having the
  929. lexer sometimes handle it, and without a clear division of
  930. responsibility, it quickly degenerated into a big mess. Probably
  931. the eventual correct solution will give more of a role to the lexer
  932. (ideally via code that is shared between the lexer and
  933. decode_line_1), but I'm not holding my breath waiting for somebody
  934. to get around to cleaning this up... */
  935. qualified_type: typebase COLONCOLON name
  936. {
  937. struct type *type = $1;
  938. struct type *new_type;
  939. char *ncopy = alloca ($3.length + 1);
  940. memcpy (ncopy, $3.ptr, $3.length);
  941. ncopy[$3.length] = '\0';
  942. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  943. && TYPE_CODE (type) != TYPE_CODE_UNION
  944. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  945. error ("`%s' is not defined as an aggregate type.",
  946. TYPE_NAME (type));
  947. new_type = cp_lookup_nested_type (type, ncopy,
  948. expression_context_block);
  949. if (new_type == NULL)
  950. error ("No type \"%s\" within class or namespace \"%s\".",
  951. ncopy, TYPE_NAME (type));
  952. $$ = new_type;
  953. }
  954. ;
  955. typename: TYPENAME
  956. | INT_KEYWORD
  957. {
  958. $$.stoken.ptr = "int";
  959. $$.stoken.length = 3;
  960. $$.type = parse_type->builtin_int;
  961. }
  962. | LONG
  963. {
  964. $$.stoken.ptr = "long";
  965. $$.stoken.length = 4;
  966. $$.type = parse_type->builtin_long;
  967. }
  968. | SHORT
  969. {
  970. $$.stoken.ptr = "short";
  971. $$.stoken.length = 5;
  972. $$.type = parse_type->builtin_short;
  973. }
  974. ;
  975. nonempty_typelist
  976. : type
  977. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  978. $<ivec>$[0] = 1; /* Number of types in vector */
  979. $$[1] = $1;
  980. }
  981. | nonempty_typelist ',' type
  982. { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  983. $$ = (struct type **) realloc ((char *) $1, len);
  984. $$[$<ivec>$[0]] = $3;
  985. }
  986. ;
  987. ptype : typebase
  988. | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
  989. { $$ = follow_types ($1); }
  990. ;
  991. const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
  992. | VOLATILE_KEYWORD CONST_KEYWORD
  993. ;
  994. const_or_volatile_noopt: const_and_volatile
  995. { push_type (tp_const);
  996. push_type (tp_volatile);
  997. }
  998. | CONST_KEYWORD
  999. { push_type (tp_const); }
  1000. | VOLATILE_KEYWORD
  1001. { push_type (tp_volatile); }
  1002. ;
  1003. operator: OPERATOR NEW
  1004. { $$ = operator_stoken (" new"); }
  1005. | OPERATOR DELETE
  1006. { $$ = operator_stoken (" delete"); }
  1007. | OPERATOR NEW '[' ']'
  1008. { $$ = operator_stoken (" new[]"); }
  1009. | OPERATOR DELETE '[' ']'
  1010. { $$ = operator_stoken (" delete[]"); }
  1011. | OPERATOR '+'
  1012. { $$ = operator_stoken ("+"); }
  1013. | OPERATOR '-'
  1014. { $$ = operator_stoken ("-"); }
  1015. | OPERATOR '*'
  1016. { $$ = operator_stoken ("*"); }
  1017. | OPERATOR '/'
  1018. { $$ = operator_stoken ("/"); }
  1019. | OPERATOR '%'
  1020. { $$ = operator_stoken ("%"); }
  1021. | OPERATOR '^'
  1022. { $$ = operator_stoken ("^"); }
  1023. | OPERATOR '&'
  1024. { $$ = operator_stoken ("&"); }
  1025. | OPERATOR '|'
  1026. { $$ = operator_stoken ("|"); }
  1027. | OPERATOR '~'
  1028. { $$ = operator_stoken ("~"); }
  1029. | OPERATOR '!'
  1030. { $$ = operator_stoken ("!"); }
  1031. | OPERATOR '='
  1032. { $$ = operator_stoken ("="); }
  1033. | OPERATOR '<'
  1034. { $$ = operator_stoken ("<"); }
  1035. | OPERATOR '>'
  1036. { $$ = operator_stoken (">"); }
  1037. | OPERATOR ASSIGN_MODIFY
  1038. { const char *op = "unknown";
  1039. switch ($2)
  1040. {
  1041. case BINOP_RSH:
  1042. op = ">>=";
  1043. break;
  1044. case BINOP_LSH:
  1045. op = "<<=";
  1046. break;
  1047. case BINOP_ADD:
  1048. op = "+=";
  1049. break;
  1050. case BINOP_SUB:
  1051. op = "-=";
  1052. break;
  1053. case BINOP_MUL:
  1054. op = "*=";
  1055. break;
  1056. case BINOP_DIV:
  1057. op = "/=";
  1058. break;
  1059. case BINOP_REM:
  1060. op = "%=";
  1061. break;
  1062. case BINOP_BITWISE_IOR:
  1063. op = "|=";
  1064. break;
  1065. case BINOP_BITWISE_AND:
  1066. op = "&=";
  1067. break;
  1068. case BINOP_BITWISE_XOR:
  1069. op = "^=";
  1070. break;
  1071. default:
  1072. break;
  1073. }
  1074. $$ = operator_stoken (op);
  1075. }
  1076. | OPERATOR LSH
  1077. { $$ = operator_stoken ("<<"); }
  1078. | OPERATOR RSH
  1079. { $$ = operator_stoken (">>"); }
  1080. | OPERATOR EQUAL
  1081. { $$ = operator_stoken ("=="); }
  1082. | OPERATOR NOTEQUAL
  1083. { $$ = operator_stoken ("!="); }
  1084. | OPERATOR LEQ
  1085. { $$ = operator_stoken ("<="); }
  1086. | OPERATOR GEQ
  1087. { $$ = operator_stoken (">="); }
  1088. | OPERATOR ANDAND
  1089. { $$ = operator_stoken ("&&"); }
  1090. | OPERATOR OROR
  1091. { $$ = operator_stoken ("||"); }
  1092. | OPERATOR INCREMENT
  1093. { $$ = operator_stoken ("++"); }
  1094. | OPERATOR DECREMENT
  1095. { $$ = operator_stoken ("--"); }
  1096. | OPERATOR ','
  1097. { $$ = operator_stoken (","); }
  1098. | OPERATOR ARROW_STAR
  1099. { $$ = operator_stoken ("->*"); }
  1100. | OPERATOR ARROW
  1101. { $$ = operator_stoken ("->"); }
  1102. | OPERATOR '(' ')'
  1103. { $$ = operator_stoken ("()"); }
  1104. | OPERATOR '[' ']'
  1105. { $$ = operator_stoken ("[]"); }
  1106. | OPERATOR ptype
  1107. { char *name;
  1108. long length;
  1109. struct ui_file *buf = mem_fileopen ();
  1110. c_print_type ($2, NULL, buf, -1, 0);
  1111. name = ui_file_xstrdup (buf, &length);
  1112. ui_file_delete (buf);
  1113. $$ = operator_stoken (name);
  1114. free (name);
  1115. }
  1116. ;
  1117. name : NAME { $$ = $1.stoken; }
  1118. | BLOCKNAME { $$ = $1.stoken; }
  1119. | TYPENAME { $$ = $1.stoken; }
  1120. | NAME_OR_INT { $$ = $1.stoken; }
  1121. | operator { $$ = $1; }
  1122. ;
  1123. name_not_typename : NAME
  1124. | BLOCKNAME
  1125. /* These would be useful if name_not_typename was useful, but it is just
  1126. a fake for "variable", so these cause reduce/reduce conflicts because
  1127. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  1128. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  1129. context where only a name could occur, this might be useful.
  1130. | NAME_OR_INT
  1131. */
  1132. ;
  1133. %%
  1134. /* Returns a stoken of the operator name given by OP (which does not
  1135. include the string "operator"). */
  1136. static struct stoken
  1137. operator_stoken (const char *op)
  1138. {
  1139. static const char *operator_string = "operator";
  1140. struct stoken st = { NULL, 0 };
  1141. st.length = strlen (operator_string) + strlen (op);
  1142. st.ptr = malloc (st.length + 1);
  1143. strcpy (st.ptr, operator_string);
  1144. strcat (st.ptr, op);
  1145. /* The toplevel (c_parse) will free the memory allocated here. */
  1146. make_cleanup (free, st.ptr);
  1147. return st;
  1148. };
  1149. /* Take care of parsing a number (anything that starts with a digit).
  1150. Set yylval and return the token type; update lexptr.
  1151. LEN is the number of characters in it. */
  1152. /*** Needs some error checking for the float case ***/
  1153. static int
  1154. parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
  1155. {
  1156. /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
  1157. here, and we do kind of silly things like cast to unsigned. */
  1158. LONGEST n = 0;
  1159. LONGEST prevn = 0;
  1160. ULONGEST un;
  1161. int i = 0;
  1162. int c;
  1163. int base = input_radix;
  1164. int unsigned_p = 0;
  1165. /* Number of "L" suffixes encountered. */
  1166. int long_p = 0;
  1167. /* We have found a "L" or "U" suffix. */
  1168. int found_suffix = 0;
  1169. ULONGEST high_bit;
  1170. struct type *signed_type;
  1171. struct type *unsigned_type;
  1172. if (parsed_float)
  1173. {
  1174. /* It's a float since it contains a point or an exponent. */
  1175. char *s;
  1176. int num; /* number of tokens scanned by scanf */
  1177. char saved_char;
  1178. /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
  1179. point. Return DECFLOAT. */
  1180. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
  1181. {
  1182. p[len - 2] = '\0';
  1183. putithere->typed_val_decfloat.type
  1184. = parse_type->builtin_decfloat;
  1185. decimal_from_string (putithere->typed_val_decfloat.val, 4,
  1186. gdbarch_byte_order (parse_gdbarch), p);
  1187. p[len - 2] = 'd';
  1188. return DECFLOAT;
  1189. }
  1190. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
  1191. {
  1192. p[len - 2] = '\0';
  1193. putithere->typed_val_decfloat.type
  1194. = parse_type->builtin_decdouble;
  1195. decimal_from_string (putithere->typed_val_decfloat.val, 8,
  1196. gdbarch_byte_order (parse_gdbarch), p);
  1197. p[len - 2] = 'd';
  1198. return DECFLOAT;
  1199. }
  1200. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
  1201. {
  1202. p[len - 2] = '\0';
  1203. putithere->typed_val_decfloat.type
  1204. = parse_type->builtin_declong;
  1205. decimal_from_string (putithere->typed_val_decfloat.val, 16,
  1206. gdbarch_byte_order (parse_gdbarch), p);
  1207. p[len - 2] = 'd';
  1208. return DECFLOAT;
  1209. }
  1210. s = malloc (len);
  1211. saved_char = p[len];
  1212. p[len] = 0; /* null-terminate the token */
  1213. num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
  1214. &putithere->typed_val_float.dval, s);
  1215. p[len] = saved_char; /* restore the input stream */
  1216. if (num == 1)
  1217. putithere->typed_val_float.type =
  1218. parse_type->builtin_double;
  1219. if (num == 2 )
  1220. {
  1221. /* See if it has any float suffix: 'f' for float, 'l' for long
  1222. double. */
  1223. if (!strcasecmp (s, "f"))
  1224. putithere->typed_val_float.type =
  1225. parse_type->builtin_float;
  1226. else if (!strcasecmp (s, "l"))
  1227. putithere->typed_val_float.type =
  1228. parse_type->builtin_long_double;
  1229. else
  1230. {
  1231. free (s);
  1232. return ERROR;
  1233. }
  1234. }
  1235. free (s);
  1236. return FLOAT;
  1237. }
  1238. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  1239. if (p[0] == '0')
  1240. switch (p[1])
  1241. {
  1242. case 'x':
  1243. case 'X':
  1244. if (len >= 3)
  1245. {
  1246. p += 2;
  1247. base = 16;
  1248. len -= 2;
  1249. }
  1250. break;
  1251. case 'b':
  1252. case 'B':
  1253. if (len >= 3)
  1254. {
  1255. p += 2;
  1256. base = 2;
  1257. len -= 2;
  1258. }
  1259. break;
  1260. case 't':
  1261. case 'T':
  1262. case 'd':
  1263. case 'D':
  1264. if (len >= 3)
  1265. {
  1266. p += 2;
  1267. base = 10;
  1268. len -= 2;
  1269. }
  1270. break;
  1271. default:
  1272. base = 8;
  1273. break;
  1274. }
  1275. while (len-- > 0)
  1276. {
  1277. c = *p++;
  1278. if (c >= 'A' && c <= 'Z')
  1279. c += 'a' - 'A';
  1280. if (c != 'l' && c != 'u')
  1281. n *= base;
  1282. if (c >= '0' && c <= '9')
  1283. {
  1284. if (found_suffix)
  1285. return ERROR;
  1286. n += i = c - '0';
  1287. }
  1288. else
  1289. {
  1290. if (base > 10 && c >= 'a' && c <= 'f')
  1291. {
  1292. if (found_suffix)
  1293. return ERROR;
  1294. n += i = c - 'a' + 10;
  1295. }
  1296. else if (c == 'l')
  1297. {
  1298. ++long_p;
  1299. found_suffix = 1;
  1300. }
  1301. else if (c == 'u')
  1302. {
  1303. unsigned_p = 1;
  1304. found_suffix = 1;
  1305. }
  1306. else
  1307. return ERROR; /* Char not a digit */
  1308. }
  1309. if (i >= base)
  1310. return ERROR; /* Invalid digit in this base */
  1311. /* Portably test for overflow (only works for nonzero values, so make
  1312. a second check for zero). FIXME: Can't we just make n and prevn
  1313. unsigned and avoid this? */
  1314. if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  1315. unsigned_p = 1; /* Try something unsigned */
  1316. /* Portably test for unsigned overflow.
  1317. FIXME: This check is wrong; for example it doesn't find overflow
  1318. on 0x123456789 when LONGEST is 32 bits. */
  1319. if (c != 'l' && c != 'u' && n != 0)
  1320. {
  1321. if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  1322. error ("Numeric constant too large.");
  1323. }
  1324. prevn = n;
  1325. }
  1326. /* An integer constant is an int, a long, or a long long. An L
  1327. suffix forces it to be long; an LL suffix forces it to be long
  1328. long. If not forced to a larger size, it gets the first type of
  1329. the above that it fits in. To figure out whether it fits, we
  1330. shift it right and see whether anything remains. Note that we
  1331. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  1332. operation, because many compilers will warn about such a shift
  1333. (which always produces a zero result). Sometimes gdbarch_int_bit
  1334. or gdbarch_long_bit will be that big, sometimes not. To deal with
  1335. the case where it is we just always shift the value more than
  1336. once, with fewer bits each time. */
  1337. un = (ULONGEST)n >> 2;
  1338. if (long_p == 0
  1339. && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
  1340. {
  1341. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
  1342. /* A large decimal (not hex or octal) constant (between INT_MAX
  1343. and UINT_MAX) is a long or unsigned long, according to ANSI,
  1344. never an unsigned int, but this code treats it as unsigned
  1345. int. This probably should be fixed. GCC gives a warning on
  1346. such constants. */
  1347. unsigned_type = parse_type->builtin_unsigned_int;
  1348. signed_type = parse_type->builtin_int;
  1349. }
  1350. else if (long_p <= 1
  1351. && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
  1352. {
  1353. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
  1354. unsigned_type = parse_type->builtin_unsigned_long;
  1355. signed_type = parse_type->builtin_long;
  1356. }
  1357. else
  1358. {
  1359. int shift;
  1360. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  1361. < gdbarch_long_long_bit (parse_gdbarch))
  1362. /* A long long does not fit in a LONGEST. */
  1363. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  1364. else
  1365. shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
  1366. high_bit = (ULONGEST) 1 << shift;
  1367. unsigned_type = parse_type->builtin_unsigned_long_long;
  1368. signed_type = parse_type->builtin_long_long;
  1369. }
  1370. putithere->typed_val_int.val = n;
  1371. /* If the high bit of the worked out type is set then this number
  1372. has to be unsigned. */
  1373. if (unsigned_p || (n & high_bit))
  1374. {
  1375. putithere->typed_val_int.type = unsigned_type;
  1376. }
  1377. else
  1378. {
  1379. putithere->typed_val_int.type = signed_type;
  1380. }
  1381. return INT;
  1382. }
  1383. /* Temporary obstack used for holding strings. */
  1384. static struct obstack tempbuf;
  1385. static int tempbuf_init;
  1386. /* Parse a C escape sequence. The initial backslash of the sequence
  1387. is at (*PTR)[-1]. *PTR will be updated to point to just after the
  1388. last character of the sequence. If OUTPUT is not NULL, the
  1389. translated form of the escape sequence will be written there. If
  1390. OUTPUT is NULL, no output is written and the call will only affect
  1391. *PTR. If an escape sequence is expressed in target bytes, then the
  1392. entire sequence will simply be copied to OUTPUT. Return 1 if any
  1393. character was emitted, 0 otherwise. */
  1394. int
  1395. c_parse_escape (char **ptr, struct obstack *output)
  1396. {
  1397. char *tokptr = *ptr;
  1398. int result = 1;
  1399. /* Some escape sequences undergo character set conversion. Those we
  1400. translate here. */
  1401. switch (*tokptr)
  1402. {
  1403. /* Hex escapes do not undergo character set conversion, so keep
  1404. the escape sequence for later. */
  1405. case 'x':
  1406. if (output)
  1407. obstack_grow_str (output, "\\x");
  1408. ++tokptr;
  1409. if (!isxdigit (*tokptr))
  1410. error (_("\\x escape without a following hex digit"));
  1411. while (isxdigit (*tokptr))
  1412. {
  1413. if (output)
  1414. obstack_1grow (output, *tokptr);
  1415. ++tokptr;
  1416. }
  1417. break;
  1418. /* Octal escapes do not undergo character set conversion, so
  1419. keep the escape sequence for later. */
  1420. case '0':
  1421. case '1':
  1422. case '2':
  1423. case '3':
  1424. case '4':
  1425. case '5':
  1426. case '6':
  1427. case '7':
  1428. {
  1429. int i;
  1430. if (output)
  1431. obstack_grow_str (output, "\\");
  1432. for (i = 0;
  1433. i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
  1434. ++i)
  1435. {
  1436. if (output)
  1437. obstack_1grow (output, *tokptr);
  1438. ++tokptr;
  1439. }
  1440. }
  1441. break;
  1442. /* We handle UCNs later. We could handle them here, but that
  1443. would mean a spurious error in the case where the UCN could
  1444. be converted to the target charset but not the host
  1445. charset. */
  1446. case 'u':
  1447. case 'U':
  1448. {
  1449. char c = *tokptr;
  1450. int i, len = c == 'U' ? 8 : 4;
  1451. if (output)
  1452. {
  1453. obstack_1grow (output, '\\');
  1454. obstack_1grow (output, *tokptr);
  1455. }
  1456. ++tokptr;
  1457. if (!isxdigit (*tokptr))
  1458. error (_("\\%c escape without a following hex digit"), c);
  1459. for (i = 0; i < len && isxdigit (*tokptr); ++i)
  1460. {
  1461. if (output)
  1462. obstack_1grow (output, *tokptr);
  1463. ++tokptr;
  1464. }
  1465. }
  1466. break;
  1467. /* We must pass backslash through so that it does not
  1468. cause quoting during the second expansion. */
  1469. case '\\':
  1470. if (output)
  1471. obstack_grow_str (output, "\\\\");
  1472. ++tokptr;
  1473. break;
  1474. /* Escapes which undergo conversion. */
  1475. case 'a':
  1476. if (output)
  1477. obstack_1grow (output, '\a');
  1478. ++tokptr;
  1479. break;
  1480. case 'b':
  1481. if (output)
  1482. obstack_1grow (output, '\b');
  1483. ++tokptr;
  1484. break;
  1485. case 'f':
  1486. if (output)
  1487. obstack_1grow (output, '\f');
  1488. ++tokptr;
  1489. break;
  1490. case 'n':
  1491. if (output)
  1492. obstack_1grow (output, '\n');
  1493. ++tokptr;
  1494. break;
  1495. case 'r':
  1496. if (output)
  1497. obstack_1grow (output, '\r');
  1498. ++tokptr;
  1499. break;
  1500. case 't':
  1501. if (output)
  1502. obstack_1grow (output, '\t');
  1503. ++tokptr;
  1504. break;
  1505. case 'v':
  1506. if (output)
  1507. obstack_1grow (output, '\v');
  1508. ++tokptr;
  1509. break;
  1510. /* GCC extension. */
  1511. case 'e':
  1512. if (output)
  1513. obstack_1grow (output, HOST_ESCAPE_CHAR);
  1514. ++tokptr;
  1515. break;
  1516. /* Backslash-newline expands to nothing at all. */
  1517. case '\n':
  1518. ++tokptr;
  1519. result = 0;
  1520. break;
  1521. /* A few escapes just expand to the character itself. */
  1522. case '\'':
  1523. case '\"':
  1524. case '?':
  1525. /* GCC extensions. */
  1526. case '(':
  1527. case '{':
  1528. case '[':
  1529. case '%':
  1530. /* Unrecognized escapes turn into the character itself. */
  1531. default:
  1532. if (output)
  1533. obstack_1grow (output, *tokptr);
  1534. ++tokptr;
  1535. break;
  1536. }
  1537. *ptr = tokptr;
  1538. return result;
  1539. }
  1540. /* Parse a string or character literal from TOKPTR. The string or
  1541. character may be wide or unicode. *OUTPTR is set to just after the
  1542. end of the literal in the input string. The resulting token is
  1543. stored in VALUE. This returns a token value, either STRING or
  1544. CHAR, depending on what was parsed. *HOST_CHARS is set to the
  1545. number of host characters in the literal. */
  1546. static int
  1547. parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
  1548. int *host_chars)
  1549. {
  1550. int quote, i;
  1551. enum c_string_type type;
  1552. /* Build the gdb internal form of the input string in tempbuf. Note
  1553. that the buffer is null byte terminated *only* for the
  1554. convenience of debugging gdb itself and printing the buffer
  1555. contents when the buffer contains no embedded nulls. Gdb does
  1556. not depend upon the buffer being null byte terminated, it uses
  1557. the length string instead. This allows gdb to handle C strings
  1558. (as well as strings in other languages) with embedded null
  1559. bytes */
  1560. if (!tempbuf_init)
  1561. tempbuf_init = 1;
  1562. else
  1563. obstack_free (&tempbuf, NULL);
  1564. obstack_init (&tempbuf);
  1565. /* Record the string type. */
  1566. if (*tokptr == 'L')
  1567. {
  1568. type = C_WIDE_STRING;
  1569. ++tokptr;
  1570. }
  1571. else if (*tokptr == 'u')
  1572. {
  1573. type = C_STRING_16;
  1574. ++tokptr;
  1575. }
  1576. else if (*tokptr == 'U')
  1577. {
  1578. type = C_STRING_32;
  1579. ++tokptr;
  1580. }
  1581. else
  1582. type = C_STRING;
  1583. /* Skip the quote. */
  1584. quote = *tokptr;
  1585. if (quote == '\'')
  1586. type |= C_CHAR;
  1587. ++tokptr;
  1588. *host_chars = 0;
  1589. while (*tokptr)
  1590. {
  1591. char c = *tokptr;
  1592. if (c == '\\')
  1593. {
  1594. ++tokptr;
  1595. *host_chars += c_parse_escape (&tokptr, &tempbuf);
  1596. }
  1597. else if (c == quote)
  1598. break;
  1599. else
  1600. {
  1601. obstack_1grow (&tempbuf, c);
  1602. ++tokptr;
  1603. /* FIXME: this does the wrong thing with multi-byte host
  1604. characters. We could use mbrlen here, but that would
  1605. make "set host-charset" a bit less useful. */
  1606. ++*host_chars;
  1607. }
  1608. }
  1609. if (*tokptr != quote)
  1610. {
  1611. if (quote == '"')
  1612. error ("Unterminated string in expression.");
  1613. else
  1614. error ("Unmatched single quote.");
  1615. }
  1616. ++tokptr;
  1617. value->type = type;
  1618. value->ptr = obstack_base (&tempbuf);
  1619. value->length = obstack_object_size (&tempbuf);
  1620. *outptr = tokptr;
  1621. return quote == '"' ? STRING : CHAR;
  1622. }
  1623. struct token
  1624. {
  1625. char *operator;
  1626. int token;
  1627. enum exp_opcode opcode;
  1628. int cxx_only;
  1629. };
  1630. static const struct token tokentab3[] =
  1631. {
  1632. {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
  1633. {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
  1634. {"->*", ARROW_STAR, BINOP_END, 1}
  1635. };
  1636. static const struct token tokentab2[] =
  1637. {
  1638. {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
  1639. {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
  1640. {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
  1641. {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
  1642. {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
  1643. {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
  1644. {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
  1645. {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
  1646. {"++", INCREMENT, BINOP_END, 0},
  1647. {"--", DECREMENT, BINOP_END, 0},
  1648. {"->", ARROW, BINOP_END, 0},
  1649. {"&&", ANDAND, BINOP_END, 0},
  1650. {"||", OROR, BINOP_END, 0},
  1651. /* "::" is *not* only C++: gdb overrides its meaning in several
  1652. different ways, e.g., 'filename'::func, function::variable. */
  1653. {"::", COLONCOLON, BINOP_END, 0},
  1654. {"<<", LSH, BINOP_END, 0},
  1655. {">>", RSH, BINOP_END, 0},
  1656. {"==", EQUAL, BINOP_END, 0},
  1657. {"!=", NOTEQUAL, BINOP_END, 0},
  1658. {"<=", LEQ, BINOP_END, 0},
  1659. {">=", GEQ, BINOP_END, 0},
  1660. {".*", DOT_STAR, BINOP_END, 1}
  1661. };
  1662. /* Identifier-like tokens. */
  1663. static const struct token ident_tokens[] =
  1664. {
  1665. {"unsigned", UNSIGNED, OP_NULL, 0},
  1666. {"template", TEMPLATE, OP_NULL, 1},
  1667. {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
  1668. {"struct", STRUCT, OP_NULL, 0},
  1669. {"signed", SIGNED_KEYWORD, OP_NULL, 0},
  1670. {"sizeof", SIZEOF, OP_NULL, 0},
  1671. {"double", DOUBLE_KEYWORD, OP_NULL, 0},
  1672. {"false", FALSEKEYWORD, OP_NULL, 1},
  1673. {"class", CLASS, OP_NULL, 1},
  1674. {"union", UNION, OP_NULL, 0},
  1675. {"short", SHORT, OP_NULL, 0},
  1676. {"const", CONST_KEYWORD, OP_NULL, 0},
  1677. {"enum", ENUM, OP_NULL, 0},
  1678. {"long", LONG, OP_NULL, 0},
  1679. {"true", TRUEKEYWORD, OP_NULL, 1},
  1680. {"int", INT_KEYWORD, OP_NULL, 0},
  1681. {"new", NEW, OP_NULL, 1},
  1682. {"delete", DELETE, OP_NULL, 1},
  1683. {"operator", OPERATOR, OP_NULL, 1},
  1684. {"and", ANDAND, BINOP_END, 1},
  1685. {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
  1686. {"bitand", '&', OP_NULL, 1},
  1687. {"bitor", '|', OP_NULL, 1},
  1688. {"compl", '~', OP_NULL, 1},
  1689. {"not", '!', OP_NULL, 1},
  1690. {"not_eq", NOTEQUAL, BINOP_END, 1},
  1691. {"or", OROR, BINOP_END, 1},
  1692. {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
  1693. {"xor", '^', OP_NULL, 1},
  1694. {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
  1695. {"const_cast", CONST_CAST, OP_NULL, 1 },
  1696. {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
  1697. {"static_cast", STATIC_CAST, OP_NULL, 1 },
  1698. {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
  1699. };
  1700. /* When we find that lexptr (the global var defined in parse.c) is
  1701. pointing at a macro invocation, we expand the invocation, and call
  1702. scan_macro_expansion to save the old lexptr here and point lexptr
  1703. into the expanded text. When we reach the end of that, we call
  1704. end_macro_expansion to pop back to the value we saved here. The
  1705. macro expansion code promises to return only fully-expanded text,
  1706. so we don't need to "push" more than one level.
  1707. This is disgusting, of course. It would be cleaner to do all macro
  1708. expansion beforehand, and then hand that to lexptr. But we don't
  1709. really know where the expression ends. Remember, in a command like
  1710. (gdb) break *ADDRESS if CONDITION
  1711. we evaluate ADDRESS in the scope of the current frame, but we
  1712. evaluate CONDITION in the scope of the breakpoint's location. So
  1713. it's simply wrong to try to macro-expand the whole thing at once. */
  1714. static char *macro_original_text;
  1715. /* We save all intermediate macro expansions on this obstack for the
  1716. duration of a single parse. The expansion text may sometimes have
  1717. to live past the end of the expansion, due to yacc lookahead.
  1718. Rather than try to be clever about saving the data for a single
  1719. token, we simply keep it all and delete it after parsing has
  1720. completed. */
  1721. static struct obstack expansion_obstack;
  1722. static void
  1723. scan_macro_expansion (char *expansion)
  1724. {
  1725. char *copy;
  1726. /* We'd better not be trying to push the stack twice. */
  1727. gdb_assert (! macro_original_text);
  1728. /* Copy to the obstack, and then free the intermediate
  1729. expansion. */
  1730. copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
  1731. xfree (expansion);
  1732. /* Save the old lexptr value, so we can return to it when we're done
  1733. parsing the expanded text. */
  1734. macro_original_text = lexptr;
  1735. lexptr = copy;
  1736. }
  1737. static int
  1738. scanning_macro_expansion (void)
  1739. {
  1740. return macro_original_text != 0;
  1741. }
  1742. static void
  1743. finished_macro_expansion (void)
  1744. {
  1745. /* There'd better be something to pop back to. */
  1746. gdb_assert (macro_original_text);
  1747. /* Pop back to the original text. */
  1748. lexptr = macro_original_text;
  1749. macro_original_text = 0;
  1750. }
  1751. static void
  1752. scan_macro_cleanup (void *dummy)
  1753. {
  1754. if (macro_original_text)
  1755. finished_macro_expansion ();
  1756. obstack_free (&expansion_obstack, NULL);
  1757. }
  1758. /* Return true iff the token represents a C++ cast operator. */
  1759. static int
  1760. is_cast_operator (const char *token, int len)
  1761. {
  1762. return (! strncmp (token, "dynamic_cast", len)
  1763. || ! strncmp (token, "static_cast", len)
  1764. || ! strncmp (token, "reinterpret_cast", len)
  1765. || ! strncmp (token, "const_cast", len));
  1766. }
  1767. /* The scope used for macro expansion. */
  1768. static struct macro_scope *expression_macro_scope;
  1769. /* This is set if a NAME token appeared at the very end of the input
  1770. string, with no whitespace separating the name from the EOF. This
  1771. is used only when parsing to do field name completion. */
  1772. static int saw_name_at_eof;
  1773. /* This is set if the previously-returned token was a structure
  1774. operator -- either '.' or ARROW. This is used only when parsing to
  1775. do field name completion. */
  1776. static int last_was_structop;
  1777. /* Read one token, getting characters through lexptr. */
  1778. static int
  1779. yylex (void)
  1780. {
  1781. int c;
  1782. int namelen;
  1783. unsigned int i;
  1784. char *tokstart;
  1785. int saw_structop = last_was_structop;
  1786. char *copy;
  1787. last_was_structop = 0;
  1788. retry:
  1789. /* Check if this is a macro invocation that we need to expand. */
  1790. if (! scanning_macro_expansion ())
  1791. {
  1792. char *expanded = macro_expand_next (&lexptr,
  1793. standard_macro_lookup,
  1794. expression_macro_scope);
  1795. if (expanded)
  1796. scan_macro_expansion (expanded);
  1797. }
  1798. prev_lexptr = lexptr;
  1799. tokstart = lexptr;
  1800. /* See if it is a special token of length 3. */
  1801. for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  1802. if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
  1803. {
  1804. if (tokentab3[i].cxx_only
  1805. && parse_language->la_language != language_cplus)
  1806. break;
  1807. lexptr += 3;
  1808. yylval.opcode = tokentab3[i].opcode;
  1809. return tokentab3[i].token;
  1810. }
  1811. /* See if it is a special token of length 2. */
  1812. for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  1813. if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
  1814. {
  1815. if (tokentab2[i].cxx_only
  1816. && parse_language->la_language != language_cplus)
  1817. break;
  1818. lexptr += 2;
  1819. yylval.opcode = tokentab2[i].opcode;
  1820. if (in_parse_field && tokentab2[i].token == ARROW)
  1821. last_was_structop = 1;
  1822. return tokentab2[i].token;
  1823. }
  1824. switch (c = *tokstart)
  1825. {
  1826. case 0:
  1827. /* If we were just scanning the result of a macro expansion,
  1828. then we need to resume scanning the original text.
  1829. If we're parsing for field name completion, and the previous
  1830. token allows such completion, return a COMPLETE token.
  1831. Otherwise, we were already scanning the original text, and
  1832. we're really done. */
  1833. if (scanning_macro_expansion ())
  1834. {
  1835. finished_macro_expansion ();
  1836. goto retry;
  1837. }
  1838. else if (saw_name_at_eof)
  1839. {
  1840. saw_name_at_eof = 0;
  1841. return COMPLETE;
  1842. }
  1843. else if (saw_structop)
  1844. return COMPLETE;
  1845. else
  1846. return 0;
  1847. case ' ':
  1848. case '\t':
  1849. case '\n':
  1850. lexptr++;
  1851. goto retry;
  1852. case '[':
  1853. case '(':
  1854. paren_depth++;
  1855. lexptr++;
  1856. return c;
  1857. case ']':
  1858. case ')':
  1859. if (paren_depth == 0)
  1860. return 0;
  1861. paren_depth--;
  1862. lexptr++;
  1863. return c;
  1864. case ',':
  1865. if (comma_terminates
  1866. && paren_depth == 0
  1867. && ! scanning_macro_expansion ())
  1868. return 0;
  1869. lexptr++;
  1870. return c;
  1871. case '.':
  1872. /* Might be a floating point number. */
  1873. if (lexptr[1] < '0' || lexptr[1] > '9')
  1874. {
  1875. if (in_parse_field)
  1876. last_was_structop = 1;
  1877. goto symbol; /* Nope, must be a symbol. */
  1878. }
  1879. /* FALL THRU into number case. */
  1880. case '0':
  1881. case '1':
  1882. case '2':
  1883. case '3':
  1884. case '4':
  1885. case '5':
  1886. case '6':
  1887. case '7':
  1888. case '8':
  1889. case '9':
  1890. {
  1891. /* It's a number. */
  1892. int got_dot = 0, got_e = 0, toktype;
  1893. char *p = tokstart;
  1894. int hex = input_radix > 10;
  1895. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1896. {
  1897. p += 2;
  1898. hex = 1;
  1899. }
  1900. else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  1901. {
  1902. p += 2;
  1903. hex = 0;
  1904. }
  1905. for (;; ++p)
  1906. {
  1907. /* This test includes !hex because 'e' is a valid hex digit
  1908. and thus does not indicate a floating point number when
  1909. the radix is hex. */
  1910. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1911. got_dot = got_e = 1;
  1912. /* This test does not include !hex, because a '.' always indicates
  1913. a decimal floating point number regardless of the radix. */
  1914. else if (!got_dot && *p == '.')
  1915. got_dot = 1;
  1916. else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1917. && (*p == '-' || *p == '+'))
  1918. /* This is the sign of the exponent, not the end of the
  1919. number. */
  1920. continue;
  1921. /* We will take any letters or digits. parse_number will
  1922. complain if past the radix, or if L or U are not final. */
  1923. else if ((*p < '0' || *p > '9')
  1924. && ((*p < 'a' || *p > 'z')
  1925. && (*p < 'A' || *p > 'Z')))
  1926. break;
  1927. }
  1928. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
  1929. if (toktype == ERROR)
  1930. {
  1931. char *err_copy = (char *) alloca (p - tokstart + 1);
  1932. memcpy (err_copy, tokstart, p - tokstart);
  1933. err_copy[p - tokstart] = 0;
  1934. error ("Invalid number \"%s\".", err_copy);
  1935. }
  1936. lexptr = p;
  1937. return toktype;
  1938. }
  1939. case '+':
  1940. case '-':
  1941. case '*':
  1942. case '/':
  1943. case '%':
  1944. case '|':
  1945. case '&':
  1946. case '^':
  1947. case '~':
  1948. case '!':
  1949. case '@':
  1950. case '<':
  1951. case '>':
  1952. case '?':
  1953. case ':':
  1954. case '=':
  1955. case '{':
  1956. case '}':
  1957. symbol:
  1958. lexptr++;
  1959. return c;
  1960. case 'L':
  1961. case 'u':
  1962. case 'U':
  1963. if (tokstart[1] != '"' && tokstart[1] != '\'')
  1964. break;
  1965. /* Fall through. */
  1966. case '\'':
  1967. case '"':
  1968. {
  1969. int host_len;
  1970. int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
  1971. &host_len);
  1972. if (result == CHAR)
  1973. {
  1974. if (host_len == 0)
  1975. error ("Empty character constant.");
  1976. else if (host_len > 2 && c == '\'')
  1977. {
  1978. ++tokstart;
  1979. namelen = lexptr - tokstart - 1;
  1980. goto tryname;
  1981. }
  1982. else if (host_len > 1)
  1983. error ("Invalid character constant.");
  1984. }
  1985. return result;
  1986. }
  1987. }
  1988. if (!(c == '_' || c == '$'
  1989. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1990. /* We must have come across a bad character (e.g. ';'). */
  1991. error ("Invalid character '%c' in expression.", c);
  1992. /* It's a name. See how long it is. */
  1993. namelen = 0;
  1994. for (c = tokstart[namelen];
  1995. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1996. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
  1997. {
  1998. /* Template parameter lists are part of the name.
  1999. FIXME: This mishandles `print $a<4&&$a>3'. */
  2000. if (c == '<')
  2001. {
  2002. if (! is_cast_operator (tokstart, namelen))
  2003. {
  2004. /* Scan ahead to get rest of the template specification. Note
  2005. that we look ahead only when the '<' adjoins non-whitespace
  2006. characters; for comparison expressions, e.g. "a < b > c",
  2007. there must be spaces before the '<', etc. */
  2008. char * p = find_template_name_end (tokstart + namelen);
  2009. if (p)
  2010. namelen = p - tokstart;
  2011. }
  2012. break;
  2013. }
  2014. c = tokstart[++namelen];
  2015. }
  2016. /* The token "if" terminates the expression and is NOT removed from
  2017. the input stream. It doesn't count if it appears in the
  2018. expansion of a macro. */
  2019. if (namelen == 2
  2020. && tokstart[0] == 'i'
  2021. && tokstart[1] == 'f'
  2022. && ! scanning_macro_expansion ())
  2023. {
  2024. return 0;
  2025. }
  2026. /* For the same reason (breakpoint conditions), "thread N"
  2027. terminates the expression. "thread" could be an identifier, but
  2028. an identifier is never followed by a number without intervening
  2029. punctuation. "task" is similar. Handle abbreviations of these,
  2030. similarly to breakpoint.c:find_condition_and_thread. */
  2031. if (namelen >= 1
  2032. && (strncmp (tokstart, "thread", namelen) == 0
  2033. || strncmp (tokstart, "task", namelen) == 0)
  2034. && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
  2035. && ! scanning_macro_expansion ())
  2036. {
  2037. char *p = tokstart + namelen + 1;
  2038. while (*p == ' ' || *p == '\t')
  2039. p++;
  2040. if (*p >= '0' && *p <= '9')
  2041. return 0;
  2042. }
  2043. lexptr += namelen;
  2044. tryname:
  2045. yylval.sval.ptr = tokstart;
  2046. yylval.sval.length = namelen;
  2047. /* Catch specific keywords. */
  2048. copy = copy_name (yylval.sval);
  2049. for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
  2050. if (strcmp (copy, ident_tokens[i].operator) == 0)
  2051. {
  2052. if (ident_tokens[i].cxx_only
  2053. && parse_language->la_language != language_cplus)
  2054. break;
  2055. /* It is ok to always set this, even though we don't always
  2056. strictly need to. */
  2057. yylval.opcode = ident_tokens[i].opcode;
  2058. return ident_tokens[i].token;
  2059. }
  2060. if (*tokstart == '$')
  2061. {
  2062. write_dollar_variable (yylval.sval);
  2063. return VARIABLE;
  2064. }
  2065. /* Use token-type BLOCKNAME for symbols that happen to be defined as
  2066. functions or symtabs. If this is not so, then ...
  2067. Use token-type TYPENAME for symbols that happen to be defined
  2068. currently as names of types; NAME for other symbols.
  2069. The caller is not constrained to care about the distinction. */
  2070. {
  2071. struct symbol *sym;
  2072. int is_a_field_of_this = 0;
  2073. int hextype;
  2074. sym = lookup_symbol (copy, expression_context_block,
  2075. VAR_DOMAIN,
  2076. parse_language->la_language == language_cplus
  2077. ? &is_a_field_of_this : (int *) NULL);
  2078. /* Call lookup_symtab, not lookup_partial_symtab, in case there are
  2079. no psymtabs (coff, xcoff, or some future change to blow away the
  2080. psymtabs once once symbols are read). */
  2081. if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  2082. {
  2083. yylval.ssym.sym = sym;
  2084. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  2085. return BLOCKNAME;
  2086. }
  2087. else if (!sym)
  2088. { /* See if it's a file name. */
  2089. struct symtab *symtab;
  2090. symtab = lookup_symtab (copy);
  2091. if (symtab)
  2092. {
  2093. yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
  2094. return FILENAME;
  2095. }
  2096. }
  2097. if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  2098. {
  2099. /* NOTE: carlton/2003-09-25: There used to be code here to
  2100. handle nested types. It didn't work very well. See the
  2101. comment before qualified_type for more info. */
  2102. yylval.tsym.type = SYMBOL_TYPE (sym);
  2103. return TYPENAME;
  2104. }
  2105. yylval.tsym.type
  2106. = language_lookup_primitive_type_by_name (parse_language,
  2107. parse_gdbarch, copy);
  2108. if (yylval.tsym.type != NULL)
  2109. return TYPENAME;
  2110. /* Input names that aren't symbols but ARE valid hex numbers,
  2111. when the input radix permits them, can be names or numbers
  2112. depending on the parse. Note we support radixes > 16 here. */
  2113. if (!sym
  2114. && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  2115. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  2116. {
  2117. YYSTYPE newlval; /* Its value is ignored. */
  2118. hextype = parse_number (tokstart, namelen, 0, &newlval);
  2119. if (hextype == INT)
  2120. {
  2121. yylval.ssym.sym = sym;
  2122. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  2123. return NAME_OR_INT;
  2124. }
  2125. }
  2126. /* Any other kind of symbol */
  2127. yylval.ssym.sym = sym;
  2128. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  2129. if (in_parse_field && *lexptr == '\0')
  2130. saw_name_at_eof = 1;
  2131. return NAME;
  2132. }
  2133. }
  2134. int
  2135. c_parse (void)
  2136. {
  2137. int result;
  2138. struct cleanup *back_to = make_cleanup (free_current_contents,
  2139. &expression_macro_scope);
  2140. /* Set up the scope for macro expansion. */
  2141. expression_macro_scope = NULL;
  2142. if (expression_context_block)
  2143. expression_macro_scope
  2144. = sal_macro_scope (find_pc_line (expression_context_pc, 0));
  2145. else
  2146. expression_macro_scope = default_macro_scope ();
  2147. if (! expression_macro_scope)
  2148. expression_macro_scope = user_macro_scope ();
  2149. /* Initialize macro expansion code. */
  2150. obstack_init (&expansion_obstack);
  2151. gdb_assert (! macro_original_text);
  2152. make_cleanup (scan_macro_cleanup, 0);
  2153. make_cleanup_restore_integer (&yydebug);
  2154. yydebug = parser_debug;
  2155. /* Initialize some state used by the lexer. */
  2156. last_was_structop = 0;
  2157. saw_name_at_eof = 0;
  2158. result = yyparse ();
  2159. do_cleanups (back_to);
  2160. return result;
  2161. }
  2162. void
  2163. yyerror (char *msg)
  2164. {
  2165. if (prev_lexptr)
  2166. lexptr = prev_lexptr;
  2167. error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
  2168. }