PageRenderTime 64ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/gdb-7.4.50.20120603/gdb/c-exp.y

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