PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/insight-7.4.50/gdb/c-exp.y

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