PageRenderTime 60ms CodeModel.GetById 24ms 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

Large files files are truncated, but you can click here to view the full file

  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. {"sig…

Large files files are truncated, but you can click here to view the full file