PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/gdb-7/gdb/c-exp.y

http://github.com/davshao/dflygsocdrm
Happy | 2502 lines | 2203 code | 299 blank | 0 comment | 0 complexity | f8f4d122c273efd9ce416a5bdcc1bdb6 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, LGPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception, 0BSD, BSD-3-Clause, GPL-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. #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. write_exp_elt_opcode (OP_VAR_VALUE);
  654. /* block_found is set by lookup_symbol. */
  655. write_exp_elt_block (block_found);
  656. write_exp_elt_sym (sym);
  657. write_exp_elt_opcode (OP_VAR_VALUE); }
  658. ;
  659. qualified_name: TYPENAME COLONCOLON name
  660. {
  661. struct type *type = $1.type;
  662. CHECK_TYPEDEF (type);
  663. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  664. && TYPE_CODE (type) != TYPE_CODE_UNION
  665. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  666. error (_("`%s' is not defined as an aggregate type."),
  667. TYPE_NAME (type));
  668. write_exp_elt_opcode (OP_SCOPE);
  669. write_exp_elt_type (type);
  670. write_exp_string ($3);
  671. write_exp_elt_opcode (OP_SCOPE);
  672. }
  673. | TYPENAME COLONCOLON '~' name
  674. {
  675. struct type *type = $1.type;
  676. struct stoken tmp_token;
  677. CHECK_TYPEDEF (type);
  678. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  679. && TYPE_CODE (type) != TYPE_CODE_UNION
  680. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  681. error (_("`%s' is not defined as an aggregate type."),
  682. TYPE_NAME (type));
  683. tmp_token.ptr = (char*) alloca ($4.length + 2);
  684. tmp_token.length = $4.length + 1;
  685. tmp_token.ptr[0] = '~';
  686. memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
  687. tmp_token.ptr[tmp_token.length] = 0;
  688. /* Check for valid destructor name. */
  689. destructor_name_p (tmp_token.ptr, $1.type);
  690. write_exp_elt_opcode (OP_SCOPE);
  691. write_exp_elt_type (type);
  692. write_exp_string (tmp_token);
  693. write_exp_elt_opcode (OP_SCOPE);
  694. }
  695. | TYPENAME COLONCOLON name COLONCOLON name
  696. {
  697. char *copy = copy_name ($3);
  698. error (_("No type \"%s\" within class "
  699. "or namespace \"%s\"."),
  700. copy, TYPE_NAME ($1.type));
  701. }
  702. ;
  703. variable: qualified_name
  704. | COLONCOLON name_not_typename
  705. {
  706. char *name = copy_name ($2.stoken);
  707. struct symbol *sym;
  708. struct minimal_symbol *msymbol;
  709. sym =
  710. lookup_symbol (name, (const struct block *) NULL,
  711. VAR_DOMAIN, (int *) NULL);
  712. if (sym)
  713. {
  714. write_exp_elt_opcode (OP_VAR_VALUE);
  715. write_exp_elt_block (NULL);
  716. write_exp_elt_sym (sym);
  717. write_exp_elt_opcode (OP_VAR_VALUE);
  718. break;
  719. }
  720. msymbol = lookup_minimal_symbol (name, NULL, NULL);
  721. if (msymbol != NULL)
  722. write_exp_msymbol (msymbol);
  723. else if (!have_full_symbols () && !have_partial_symbols ())
  724. error (_("No symbol table is loaded. Use the \"file\" command."));
  725. else
  726. error (_("No symbol \"%s\" in current context."), name);
  727. }
  728. ;
  729. variable: name_not_typename
  730. { struct symbol *sym = $1.sym;
  731. if (sym)
  732. {
  733. if (symbol_read_needs_frame (sym))
  734. {
  735. if (innermost_block == 0
  736. || contained_in (block_found,
  737. innermost_block))
  738. innermost_block = block_found;
  739. }
  740. write_exp_elt_opcode (OP_VAR_VALUE);
  741. /* We want to use the selected frame, not
  742. another more inner frame which happens to
  743. be in the same block. */
  744. write_exp_elt_block (NULL);
  745. write_exp_elt_sym (sym);
  746. write_exp_elt_opcode (OP_VAR_VALUE);
  747. }
  748. else if ($1.is_a_field_of_this)
  749. {
  750. /* C++: it hangs off of `this'. Must
  751. not inadvertently convert from a method call
  752. to data ref. */
  753. if (innermost_block == 0
  754. || contained_in (block_found,
  755. innermost_block))
  756. innermost_block = block_found;
  757. write_exp_elt_opcode (OP_THIS);
  758. write_exp_elt_opcode (OP_THIS);
  759. write_exp_elt_opcode (STRUCTOP_PTR);
  760. write_exp_string ($1.stoken);
  761. write_exp_elt_opcode (STRUCTOP_PTR);
  762. }
  763. else
  764. {
  765. struct minimal_symbol *msymbol;
  766. char *arg = copy_name ($1.stoken);
  767. msymbol =
  768. lookup_minimal_symbol (arg, NULL, NULL);
  769. if (msymbol != NULL)
  770. write_exp_msymbol (msymbol);
  771. else if (!have_full_symbols () && !have_partial_symbols ())
  772. error (_("No symbol table is loaded. Use the \"file\" command."));
  773. else
  774. error (_("No symbol \"%s\" in current context."),
  775. copy_name ($1.stoken));
  776. }
  777. }
  778. ;
  779. space_identifier : '@' NAME
  780. { push_type_address_space (copy_name ($2.stoken));
  781. push_type (tp_space_identifier);
  782. }
  783. ;
  784. const_or_volatile: const_or_volatile_noopt
  785. |
  786. ;
  787. cv_with_space_id : const_or_volatile space_identifier const_or_volatile
  788. ;
  789. const_or_volatile_or_space_identifier_noopt: cv_with_space_id
  790. | const_or_volatile_noopt
  791. ;
  792. const_or_volatile_or_space_identifier:
  793. const_or_volatile_or_space_identifier_noopt
  794. |
  795. ;
  796. abs_decl: '*'
  797. { push_type (tp_pointer); $$ = 0; }
  798. | '*' abs_decl
  799. { push_type (tp_pointer); $$ = $2; }
  800. | '&'
  801. { push_type (tp_reference); $$ = 0; }
  802. | '&' abs_decl
  803. { push_type (tp_reference); $$ = $2; }
  804. | direct_abs_decl
  805. ;
  806. direct_abs_decl: '(' abs_decl ')'
  807. { $$ = $2; }
  808. | direct_abs_decl array_mod
  809. {
  810. push_type_int ($2);
  811. push_type (tp_array);
  812. }
  813. | array_mod
  814. {
  815. push_type_int ($1);
  816. push_type (tp_array);
  817. $$ = 0;
  818. }
  819. | direct_abs_decl func_mod
  820. { push_type (tp_function); }
  821. | func_mod
  822. { push_type (tp_function); }
  823. ;
  824. array_mod: '[' ']'
  825. { $$ = -1; }
  826. | '[' INT ']'
  827. { $$ = $2.val; }
  828. ;
  829. func_mod: '(' ')'
  830. { $$ = 0; }
  831. | '(' nonempty_typelist ')'
  832. { free ($2); $$ = 0; }
  833. ;
  834. /* We used to try to recognize pointer to member types here, but
  835. that didn't work (shift/reduce conflicts meant that these rules never
  836. got executed). The problem is that
  837. int (foo::bar::baz::bizzle)
  838. is a function type but
  839. int (foo::bar::baz::bizzle::*)
  840. is a pointer to member type. Stroustrup loses again! */
  841. type : ptype
  842. ;
  843. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  844. : TYPENAME
  845. { $$ = $1.type; }
  846. | INT_KEYWORD
  847. { $$ = lookup_signed_typename (parse_language,
  848. parse_gdbarch,
  849. "int"); }
  850. | LONG
  851. { $$ = lookup_signed_typename (parse_language,
  852. parse_gdbarch,
  853. "long"); }
  854. | SHORT
  855. { $$ = lookup_signed_typename (parse_language,
  856. parse_gdbarch,
  857. "short"); }
  858. | LONG INT_KEYWORD
  859. { $$ = lookup_signed_typename (parse_language,
  860. parse_gdbarch,
  861. "long"); }
  862. | LONG SIGNED_KEYWORD INT_KEYWORD
  863. { $$ = lookup_signed_typename (parse_language,
  864. parse_gdbarch,
  865. "long"); }
  866. | LONG SIGNED_KEYWORD
  867. { $$ = lookup_signed_typename (parse_language,
  868. parse_gdbarch,
  869. "long"); }
  870. | SIGNED_KEYWORD LONG INT_KEYWORD
  871. { $$ = lookup_signed_typename (parse_language,
  872. parse_gdbarch,
  873. "long"); }
  874. | UNSIGNED LONG INT_KEYWORD
  875. { $$ = lookup_unsigned_typename (parse_language,
  876. parse_gdbarch,
  877. "long"); }
  878. | LONG UNSIGNED INT_KEYWORD
  879. { $$ = lookup_unsigned_typename (parse_language,
  880. parse_gdbarch,
  881. "long"); }
  882. | LONG UNSIGNED
  883. { $$ = lookup_unsigned_typename (parse_language,
  884. parse_gdbarch,
  885. "long"); }
  886. | LONG LONG
  887. { $$ = lookup_signed_typename (parse_language,
  888. parse_gdbarch,
  889. "long long"); }
  890. | LONG LONG INT_KEYWORD
  891. { $$ = lookup_signed_typename (parse_language,
  892. parse_gdbarch,
  893. "long long"); }
  894. | LONG LONG SIGNED_KEYWORD INT_KEYWORD
  895. { $$ = lookup_signed_typename (parse_language,
  896. parse_gdbarch,
  897. "long long"); }
  898. | LONG LONG SIGNED_KEYWORD
  899. { $$ = lookup_signed_typename (parse_language,
  900. parse_gdbarch,
  901. "long long"); }
  902. | SIGNED_KEYWORD LONG LONG
  903. { $$ = lookup_signed_typename (parse_language,
  904. parse_gdbarch,
  905. "long long"); }
  906. | SIGNED_KEYWORD LONG LONG INT_KEYWORD
  907. { $$ = lookup_signed_typename (parse_language,
  908. parse_gdbarch,
  909. "long long"); }
  910. | UNSIGNED LONG LONG
  911. { $$ = lookup_unsigned_typename (parse_language,
  912. parse_gdbarch,
  913. "long long"); }
  914. | UNSIGNED LONG LONG INT_KEYWORD
  915. { $$ = lookup_unsigned_typename (parse_language,
  916. parse_gdbarch,
  917. "long long"); }
  918. | LONG LONG UNSIGNED
  919. { $$ = lookup_unsigned_typename (parse_language,
  920. parse_gdbarch,
  921. "long long"); }
  922. | LONG LONG UNSIGNED INT_KEYWORD
  923. { $$ = lookup_unsigned_typename (parse_language,
  924. parse_gdbarch,
  925. "long long"); }
  926. | SHORT INT_KEYWORD
  927. { $$ = lookup_signed_typename (parse_language,
  928. parse_gdbarch,
  929. "short"); }
  930. | SHORT SIGNED_KEYWORD INT_KEYWORD
  931. { $$ = lookup_signed_typename (parse_language,
  932. parse_gdbarch,
  933. "short"); }
  934. | SHORT SIGNED_KEYWORD
  935. { $$ = lookup_signed_typename (parse_language,
  936. parse_gdbarch,
  937. "short"); }
  938. | UNSIGNED SHORT INT_KEYWORD
  939. { $$ = lookup_unsigned_typename (parse_language,
  940. parse_gdbarch,
  941. "short"); }
  942. | SHORT UNSIGNED
  943. { $$ = lookup_unsigned_typename (parse_language,
  944. parse_gdbarch,
  945. "short"); }
  946. | SHORT UNSIGNED INT_KEYWORD
  947. { $$ = lookup_unsigned_typename (parse_language,
  948. parse_gdbarch,
  949. "short"); }
  950. | DOUBLE_KEYWORD
  951. { $$ = lookup_typename (parse_language, parse_gdbarch,
  952. "double", (struct block *) NULL,
  953. 0); }
  954. | LONG DOUBLE_KEYWORD
  955. { $$ = lookup_typename (parse_language, parse_gdbarch,
  956. "long double",
  957. (struct block *) NULL, 0); }
  958. | STRUCT name
  959. { $$ = lookup_struct (copy_name ($2),
  960. expression_context_block); }
  961. | CLASS name
  962. { $$ = lookup_struct (copy_name ($2),
  963. expression_context_block); }
  964. | UNION name
  965. { $$ = lookup_union (copy_name ($2),
  966. expression_context_block); }
  967. | ENUM name
  968. { $$ = lookup_enum (copy_name ($2),
  969. expression_context_block); }
  970. | UNSIGNED typename
  971. { $$ = lookup_unsigned_typename (parse_language,
  972. parse_gdbarch,
  973. TYPE_NAME($2.type)); }
  974. | UNSIGNED
  975. { $$ = lookup_unsigned_typename (parse_language,
  976. parse_gdbarch,
  977. "int"); }
  978. | SIGNED_KEYWORD typename
  979. { $$ = lookup_signed_typename (parse_language,
  980. parse_gdbarch,
  981. TYPE_NAME($2.type)); }
  982. | SIGNED_KEYWORD
  983. { $$ = lookup_signed_typename (parse_language,
  984. parse_gdbarch,
  985. "int"); }
  986. /* It appears that this rule for templates is never
  987. reduced; template recognition happens by lookahead
  988. in the token processing code in yylex. */
  989. | TEMPLATE name '<' type '>'
  990. { $$ = lookup_template_type(copy_name($2), $4,
  991. expression_context_block);
  992. }
  993. | const_or_volatile_or_space_identifier_noopt typebase
  994. { $$ = follow_types ($2); }
  995. | typebase const_or_volatile_or_space_identifier_noopt
  996. { $$ = follow_types ($1); }
  997. ;
  998. typename: TYPENAME
  999. | INT_KEYWORD
  1000. {
  1001. $$.stoken.ptr = "int";
  1002. $$.stoken.length = 3;
  1003. $$.type = lookup_signed_typename (parse_language,
  1004. parse_gdbarch,
  1005. "int");
  1006. }
  1007. | LONG
  1008. {
  1009. $$.stoken.ptr = "long";
  1010. $$.stoken.length = 4;
  1011. $$.type = lookup_signed_typename (parse_language,
  1012. parse_gdbarch,
  1013. "long");
  1014. }
  1015. | SHORT
  1016. {
  1017. $$.stoken.ptr = "short";
  1018. $$.stoken.length = 5;
  1019. $$.type = lookup_signed_typename (parse_language,
  1020. parse_gdbarch,
  1021. "short");
  1022. }
  1023. ;
  1024. nonempty_typelist
  1025. : type
  1026. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  1027. $<ivec>$[0] = 1; /* Number of types in vector */
  1028. $$[1] = $1;
  1029. }
  1030. | nonempty_typelist ',' type
  1031. { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  1032. $$ = (struct type **) realloc ((char *) $1, len);
  1033. $$[$<ivec>$[0]] = $3;
  1034. }
  1035. ;
  1036. ptype : typebase
  1037. | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
  1038. { $$ = follow_types ($1); }
  1039. ;
  1040. const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
  1041. | VOLATILE_KEYWORD CONST_KEYWORD
  1042. ;
  1043. const_or_volatile_noopt: const_and_volatile
  1044. { push_type (tp_const);
  1045. push_type (tp_volatile);
  1046. }
  1047. | CONST_KEYWORD
  1048. { push_type (tp_const); }
  1049. | VOLATILE_KEYWORD
  1050. { push_type (tp_volatile); }
  1051. ;
  1052. operator: OPERATOR NEW
  1053. { $$ = operator_stoken (" new"); }
  1054. | OPERATOR DELETE
  1055. { $$ = operator_stoken (" delete"); }
  1056. | OPERATOR NEW '[' ']'
  1057. { $$ = operator_stoken (" new[]"); }
  1058. | OPERATOR DELETE '[' ']'
  1059. { $$ = operator_stoken (" delete[]"); }
  1060. | OPERATOR '+'
  1061. { $$ = operator_stoken ("+"); }
  1062. | OPERATOR '-'
  1063. { $$ = operator_stoken ("-"); }
  1064. | OPERATOR '*'
  1065. { $$ = operator_stoken ("*"); }
  1066. | OPERATOR '/'
  1067. { $$ = operator_stoken ("/"); }
  1068. | OPERATOR '%'
  1069. { $$ = operator_stoken ("%"); }
  1070. | OPERATOR '^'
  1071. { $$ = operator_stoken ("^"); }
  1072. | OPERATOR '&'
  1073. { $$ = operator_stoken ("&"); }
  1074. | OPERATOR '|'
  1075. { $$ = operator_stoken ("|"); }
  1076. | OPERATOR '~'
  1077. { $$ = operator_stoken ("~"); }
  1078. | OPERATOR '!'
  1079. { $$ = operator_stoken ("!"); }
  1080. | OPERATOR '='
  1081. { $$ = operator_stoken ("="); }
  1082. | OPERATOR '<'
  1083. { $$ = operator_stoken ("<"); }
  1084. | OPERATOR '>'
  1085. { $$ = operator_stoken (">"); }
  1086. | OPERATOR ASSIGN_MODIFY
  1087. { const char *op = "unknown";
  1088. switch ($2)
  1089. {
  1090. case BINOP_RSH:
  1091. op = ">>=";
  1092. break;
  1093. case BINOP_LSH:
  1094. op = "<<=";
  1095. break;
  1096. case BINOP_ADD:
  1097. op = "+=";
  1098. break;
  1099. case BINOP_SUB:
  1100. op = "-=";
  1101. break;
  1102. case BINOP_MUL:
  1103. op = "*=";
  1104. break;
  1105. case BINOP_DIV:
  1106. op = "/=";
  1107. break;
  1108. case BINOP_REM:
  1109. op = "%=";
  1110. break;
  1111. case BINOP_BITWISE_IOR:
  1112. op = "|=";
  1113. break;
  1114. case BINOP_BITWISE_AND:
  1115. op = "&=";
  1116. break;
  1117. case BINOP_BITWISE_XOR:
  1118. op = "^=";
  1119. break;
  1120. default:
  1121. break;
  1122. }
  1123. $$ = operator_stoken (op);
  1124. }
  1125. | OPERATOR LSH
  1126. { $$ = operator_stoken ("<<"); }
  1127. | OPERATOR RSH
  1128. { $$ = operator_stoken (">>"); }
  1129. | OPERATOR EQUAL
  1130. { $$ = operator_stoken ("=="); }
  1131. | OPERATOR NOTEQUAL
  1132. { $$ = operator_stoken ("!="); }
  1133. | OPERATOR LEQ
  1134. { $$ = operator_stoken ("<="); }
  1135. | OPERATOR GEQ
  1136. { $$ = operator_stoken (">="); }
  1137. | OPERATOR ANDAND
  1138. { $$ = operator_stoken ("&&"); }
  1139. | OPERATOR OROR
  1140. { $$ = operator_stoken ("||"); }
  1141. | OPERATOR INCREMENT
  1142. { $$ = operator_stoken ("++"); }
  1143. | OPERATOR DECREMENT
  1144. { $$ = operator_stoken ("--"); }
  1145. | OPERATOR ','
  1146. { $$ = operator_stoken (","); }
  1147. | OPERATOR ARROW_STAR
  1148. { $$ = operator_stoken ("->*"); }
  1149. | OPERATOR ARROW
  1150. { $$ = operator_stoken ("->"); }
  1151. | OPERATOR '(' ')'
  1152. { $$ = operator_stoken ("()"); }
  1153. | OPERATOR '[' ']'
  1154. { $$ = operator_stoken ("[]"); }
  1155. | OPERATOR ptype
  1156. { char *name;
  1157. long length;
  1158. struct ui_file *buf = mem_fileopen ();
  1159. c_print_type ($2, NULL, buf, -1, 0);
  1160. name = ui_file_xstrdup (buf, &length);
  1161. ui_file_delete (buf);
  1162. $$ = operator_stoken (name);
  1163. free (name);
  1164. }
  1165. ;
  1166. name : NAME { $$ = $1.stoken; }
  1167. | BLOCKNAME { $$ = $1.stoken; }
  1168. | TYPENAME { $$ = $1.stoken; }
  1169. | NAME_OR_INT { $$ = $1.stoken; }
  1170. | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
  1171. | operator { $$ = $1; }
  1172. ;
  1173. name_not_typename : NAME
  1174. | BLOCKNAME
  1175. /* These would be useful if name_not_typename was useful, but it is just
  1176. a fake for "variable", so these cause reduce/reduce conflicts because
  1177. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  1178. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  1179. context where only a name could occur, this might be useful.
  1180. | NAME_OR_INT
  1181. */
  1182. | operator
  1183. {
  1184. $$.stoken = $1;
  1185. $$.sym = lookup_symbol ($1.ptr,
  1186. expression_context_block,
  1187. VAR_DOMAIN,
  1188. &$$.is_a_field_of_this);
  1189. }
  1190. | UNKNOWN_CPP_NAME
  1191. ;
  1192. %%
  1193. /* Returns a stoken of the operator name given by OP (which does not
  1194. include the string "operator"). */
  1195. static struct stoken
  1196. operator_stoken (const char *op)
  1197. {
  1198. static const char *operator_string = "operator";
  1199. struct stoken st = { NULL, 0 };
  1200. st.length = strlen (operator_string) + strlen (op);
  1201. st.ptr = malloc (st.length + 1);
  1202. strcpy (st.ptr, operator_string);
  1203. strcat (st.ptr, op);
  1204. /* The toplevel (c_parse) will free the memory allocated here. */
  1205. make_cleanup (free, st.ptr);
  1206. return st;
  1207. };
  1208. /* Take care of parsing a number (anything that starts with a digit).
  1209. Set yylval and return the token type; update lexptr.
  1210. LEN is the number of characters in it. */
  1211. /*** Needs some error checking for the float case ***/
  1212. static int
  1213. parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
  1214. {
  1215. /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
  1216. here, and we do kind of silly things like cast to unsigned. */
  1217. LONGEST n = 0;
  1218. LONGEST prevn = 0;
  1219. ULONGEST un;
  1220. int i = 0;
  1221. int c;
  1222. int base = input_radix;
  1223. int unsigned_p = 0;
  1224. /* Number of "L" suffixes encountered. */
  1225. int long_p = 0;
  1226. /* We have found a "L" or "U" suffix. */
  1227. int found_suffix = 0;
  1228. ULONGEST high_bit;
  1229. struct type *signed_type;
  1230. struct type *unsigned_type;
  1231. if (parsed_float)
  1232. {
  1233. const char *suffix;
  1234. int suffix_len;
  1235. /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
  1236. point. Return DECFLOAT. */
  1237. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
  1238. {
  1239. p[len - 2] = '\0';
  1240. putithere->typed_val_decfloat.type
  1241. = parse_type->builtin_decfloat;
  1242. decimal_from_string (putithere->typed_val_decfloat.val, 4,
  1243. gdbarch_byte_order (parse_gdbarch), p);
  1244. p[len - 2] = 'd';
  1245. return DECFLOAT;
  1246. }
  1247. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
  1248. {
  1249. p[len - 2] = '\0';
  1250. putithere->typed_val_decfloat.type
  1251. = parse_type->builtin_decdouble;
  1252. decimal_from_string (putithere->typed_val_decfloat.val, 8,
  1253. gdbarch_byte_order (parse_gdbarch), p);
  1254. p[len - 2] = 'd';
  1255. return DECFLOAT;
  1256. }
  1257. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
  1258. {
  1259. p[len - 2] = '\0';
  1260. putithere->typed_val_decfloat.type
  1261. = parse_type->builtin_declong;
  1262. decimal_from_string (putithere->typed_val_decfloat.val, 16,
  1263. gdbarch_byte_order (parse_gdbarch), p);
  1264. p[len - 2] = 'd';
  1265. return DECFLOAT;
  1266. }
  1267. if (! parse_c_float (parse_gdbarch, p, len,
  1268. &putithere->typed_val_float.dval,
  1269. &putithere->typed_val_float.type))
  1270. return ERROR;
  1271. return FLOAT;
  1272. }
  1273. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  1274. if (p[0] == '0')
  1275. switch (p[1])
  1276. {
  1277. case 'x':
  1278. case 'X':
  1279. if (len >= 3)
  1280. {
  1281. p += 2;
  1282. base = 16;
  1283. len -= 2;
  1284. }
  1285. break;
  1286. case 'b':
  1287. case 'B':
  1288. if (len >= 3)
  1289. {
  1290. p += 2;
  1291. base = 2;
  1292. len -= 2;
  1293. }
  1294. break;
  1295. case 't':
  1296. case 'T':
  1297. case 'd':
  1298. case 'D':
  1299. if (len >= 3)
  1300. {
  1301. p += 2;
  1302. base = 10;
  1303. len -= 2;
  1304. }
  1305. break;
  1306. default:
  1307. base = 8;
  1308. break;
  1309. }
  1310. while (len-- > 0)
  1311. {
  1312. c = *p++;
  1313. if (c >= 'A' && c <= 'Z')
  1314. c += 'a' - 'A';
  1315. if (c != 'l' && c != 'u')
  1316. n *= base;
  1317. if (c >= '0' && c <= '9')
  1318. {
  1319. if (found_suffix)
  1320. return ERROR;
  1321. n += i = c - '0';
  1322. }
  1323. else
  1324. {
  1325. if (base > 10 && c >= 'a' && c <= 'f')
  1326. {
  1327. if (found_suffix)
  1328. return ERROR;
  1329. n += i = c - 'a' + 10;
  1330. }
  1331. else if (c == 'l')
  1332. {
  1333. ++long_p;
  1334. found_suffix = 1;
  1335. }
  1336. else if (c == 'u')
  1337. {
  1338. unsigned_p = 1;
  1339. found_suffix = 1;
  1340. }
  1341. else
  1342. return ERROR; /* Char not a digit */
  1343. }
  1344. if (i >= base)
  1345. return ERROR; /* Invalid digit in this base */
  1346. /* Portably test for overflow (only works for nonzero values, so make
  1347. a second check for zero). FIXME: Can't we just make n and prevn
  1348. unsigned and avoid this? */
  1349. if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  1350. unsigned_p = 1; /* Try something unsigned */
  1351. /* Portably test for unsigned overflow.
  1352. FIXME: This check is wrong; for example it doesn't find overflow
  1353. on 0x123456789 when LONGEST is 32 bits. */
  1354. if (c != 'l' && c != 'u' && n != 0)
  1355. {
  1356. if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  1357. error (_("Numeric constant too large."));
  1358. }
  1359. prevn = n;
  1360. }
  1361. /* An integer constant is an int, a long, or a long long. An L
  1362. suffix forces it to be long; an LL suffix forces it to be long
  1363. long. If not forced to a larger size, it gets the first type of
  1364. the above that it fits in. To figure out whether it fits, we
  1365. shift it right and see whether anything remains. Note that we
  1366. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  1367. operation, because many compilers will warn about such a shift
  1368. (which always produces a zero result). Sometimes gdbarch_int_bit
  1369. or gdbarch_long_bit will be that big, sometimes not. To deal with
  1370. the case where it is we just always shift the value more than
  1371. once, with fewer bits each time. */
  1372. un = (ULONGEST)n >> 2;
  1373. if (long_p == 0
  1374. && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
  1375. {
  1376. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
  1377. /* A large decimal (not hex or octal) constant (between INT_MAX
  1378. and UINT_MAX) is a long or unsigned long, according to ANSI,
  1379. never an unsigned int, but this code treats it as unsigned
  1380. int. This probably should be fixed. GCC gives a warning on
  1381. such constants. */
  1382. unsigned_type = parse_type->builtin_unsigned_int;
  1383. signed_type = parse_type->builtin_int;
  1384. }
  1385. else if (long_p <= 1
  1386. && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
  1387. {
  1388. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
  1389. unsigned_type = parse_type->builtin_unsigned_long;
  1390. signed_type = parse_type->builtin_long;
  1391. }
  1392. else
  1393. {
  1394. int shift;
  1395. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  1396. < gdbarch_long_long_bit (parse_gdbarch))
  1397. /* A long long does not fit in a LONGEST. */
  1398. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  1399. else
  1400. shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
  1401. high_bit = (ULONGEST) 1 << shift;
  1402. unsigned_type = parse_type->builtin_unsigned_long_long;
  1403. signed_type = parse_type->builtin_long_long;
  1404. }
  1405. putithere->typed_val_int.val = n;
  1406. /* If the high bit of the worked out type is set then this number
  1407. has to be unsigned. */
  1408. if (unsigned_p || (n & high_bit))
  1409. {
  1410. putithere->typed_val_int.type = unsigned_type;
  1411. }
  1412. else
  1413. {
  1414. putithere->typed_val_int.type = signed_type;
  1415. }
  1416. return INT;
  1417. }
  1418. /* Temporary obstack used for holding strings. */
  1419. static struct obstack tempbuf;
  1420. static int tempbuf_init;
  1421. /* Parse a C escape sequence. The initial backslash of the sequence
  1422. is at (*PTR)[-1]. *PTR will be updated to point to just after the
  1423. last character of the sequence. If OUTPUT is not NULL, the
  1424. translated form of the escape sequence will be written there. If
  1425. OUTPUT is NULL, no output is written and the call will only affect
  1426. *PTR. If an escape sequence is expressed in target bytes, then the
  1427. entire sequence will simply be copied to OUTPUT. Return 1 if any
  1428. character was emitted, 0 otherwise. */
  1429. int
  1430. c_parse_escape (char **ptr, struct obstack *output)
  1431. {
  1432. char *tokptr = *ptr;
  1433. int result = 1;
  1434. /* Some escape sequences undergo character set conversion. Those we
  1435. translate here. */
  1436. switch (*tokptr)
  1437. {
  1438. /* Hex escapes do not undergo character set conversion, so keep
  1439. the escape sequence for later. */
  1440. case 'x':
  1441. if (output)
  1442. obstack_grow_str (output, "\\x");
  1443. ++tokptr;
  1444. if (!isxdigit (*tokptr))
  1445. error (_("\\x escape without a following hex digit"));
  1446. while (isxdigit (*tokptr))
  1447. {
  1448. if (output)
  1449. obstack_1grow (output, *tokptr);
  1450. ++tokptr;
  1451. }
  1452. break;
  1453. /* Octal escapes do not undergo character set conversion, so
  1454. keep the escape sequence for later. */
  1455. case '0':
  1456. case '1':
  1457. case '2':
  1458. case '3':
  1459. case '4':
  1460. case '5':
  1461. case '6':
  1462. case '7':
  1463. {
  1464. int i;
  1465. if (output)
  1466. obstack_grow_str (output, "\\");
  1467. for (i = 0;
  1468. i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
  1469. ++i)
  1470. {
  1471. if (output)
  1472. obstack_1grow (output, *tokptr);
  1473. ++tokptr;
  1474. }
  1475. }
  1476. break;
  1477. /* We handle UCNs later. We could handle them here, but that
  1478. would mean a spurious error in the case where the UCN could
  1479. be converted to the target charset but not the host
  1480. charset. */
  1481. case 'u':
  1482. case 'U':
  1483. {
  1484. char c = *tokptr;
  1485. int i, len = c == 'U' ? 8 : 4;
  1486. if (output)
  1487. {
  1488. obstack_1grow (output, '\\');
  1489. obstack_1grow (output, *tokptr);
  1490. }
  1491. ++tokptr;
  1492. if (!isxdigit (*tokptr))
  1493. error (_("\\%c escape without a following hex digit"), c);
  1494. for (i = 0; i < len && isxdigit (*tokptr); ++i)
  1495. {
  1496. if (output)
  1497. obstack_1grow (output, *tokptr);
  1498. ++tokptr;
  1499. }
  1500. }
  1501. break;
  1502. /* We must pass backslash through so that it does not
  1503. cause quoting during the second expansion. */
  1504. case '\\':
  1505. if (output)
  1506. obstack_grow_str (output, "\\\\");
  1507. ++tokptr;
  1508. break;
  1509. /* Escapes which undergo conversion. */
  1510. case 'a':
  1511. if (output)
  1512. obstack_1grow (output, '\a');
  1513. ++tokptr;
  1514. break;
  1515. case 'b':
  1516. if (output)
  1517. obstack_1grow (output, '\b');
  1518. ++tokptr;
  1519. break;
  1520. case 'f':
  1521. if (output)
  1522. obstack_1grow (output, '\f');
  1523. ++tokptr;
  1524. break;
  1525. case 'n':
  1526. if (output)
  1527. obstack_1grow (output, '\n');
  1528. ++tokptr;
  1529. break;
  1530. case 'r':
  1531. if (output)
  1532. obstack_1grow (output, '\r');
  1533. ++tokptr;
  1534. break;
  1535. case 't':
  1536. if (output)
  1537. obstack_1grow (output, '\t');
  1538. ++tokptr;
  1539. break;
  1540. case 'v':
  1541. if (output)
  1542. obstack_1grow (output, '\v');
  1543. ++tokptr;
  1544. break;
  1545. /* GCC extension. */
  1546. case 'e':
  1547. if (output)
  1548. obstack_1grow (output, HOST_ESCAPE_CHAR);
  1549. ++tokptr;
  1550. break;
  1551. /* Backslash-newline expands to nothing at all. */
  1552. case '\n':
  1553. ++tokptr;
  1554. result = 0;
  1555. break;
  1556. /* A few escapes just expand to the character itself. */
  1557. case '\'':
  1558. case '\"':
  1559. case '?':
  1560. /* GCC extensions. */
  1561. case '(':
  1562. case '{':
  1563. case '[':
  1564. case '%':
  1565. /* Unrecognized escapes turn into the character itself. */
  1566. default:
  1567. if (output)
  1568. obstack_1grow (output, *tokptr);
  1569. ++tokptr;
  1570. break;
  1571. }
  1572. *ptr = tokptr;
  1573. return result;
  1574. }
  1575. /* Parse a string or character literal from TOKPTR. The string or
  1576. character may be wide or unicode. *OUTPTR is set to just after the
  1577. end of the literal in the input string. The resulting token is
  1578. stored in VALUE. This returns a token value, either STRING or
  1579. CHAR, depending on what was parsed. *HOST_CHARS is set to the
  1580. number of host characters in the literal. */
  1581. static int
  1582. parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
  1583. int *host_chars)
  1584. {
  1585. int quote;
  1586. enum c_string_type type;
  1587. /* Build the gdb internal form of the input string in tempbuf. Note
  1588. that the buffer is null byte terminated *only* for the
  1589. convenience of debugging gdb itself and printing the buffer
  1590. contents when the buffer contains no embedded nulls. Gdb does
  1591. not depend upon the buffer being null byte terminated, it uses
  1592. the length string instead. This allows gdb to handle C strings
  1593. (as well as strings in other languages) with embedded null
  1594. bytes */
  1595. if (!tempbuf_init)
  1596. tempbuf_init = 1;
  1597. else
  1598. obstack_free (&tempbuf, NULL);
  1599. obstack_init (&tempbuf);
  1600. /* Record the string type. */
  1601. if (*tokptr == 'L')
  1602. {
  1603. type = C_WIDE_STRING;
  1604. ++tokptr;
  1605. }
  1606. else if (*tokptr == 'u')
  1607. {
  1608. type = C_STRING_16;
  1609. ++tokptr;
  1610. }
  1611. else if (*tokptr == 'U')
  1612. {
  1613. type = C_STRING_32;
  1614. ++tokptr;
  1615. }
  1616. else
  1617. type = C_STRING;
  1618. /* Skip the quote. */
  1619. quote = *tokptr;
  1620. if (quote == '\'')
  1621. type |= C_CHAR;
  1622. ++tokptr;
  1623. *host_chars = 0;
  1624. while (*tokptr)
  1625. {
  1626. char c = *tokptr;
  1627. if (c == '\\')
  1628. {
  1629. ++tokptr;
  1630. *host_chars += c_parse_escape (&tokptr, &tempbuf);
  1631. }
  1632. else if (c == quote)
  1633. break;
  1634. else
  1635. {
  1636. obstack_1grow (&tempbuf, c);
  1637. ++tokptr;
  1638. /* FIXME: this does the wrong thing with multi-byte host
  1639. characters. We could use mbrlen here, but that would
  1640. make "set host-charset" a bit less useful. */
  1641. ++*host_chars;
  1642. }
  1643. }
  1644. if (*tokptr != quote)
  1645. {
  1646. if (quote == '"')
  1647. error (_("Unterminated string in expression."));
  1648. else
  1649. error (_("Unmatched single quote."));
  1650. }
  1651. ++tokptr;
  1652. value->type = type;
  1653. value->ptr = obstack_base (&tempbuf);
  1654. value->length = obstack_object_size (&tempbuf);
  1655. *outptr = tokptr;
  1656. return quote == '"' ? STRING : CHAR;
  1657. }
  1658. struct token
  1659. {
  1660. char *operator;
  1661. int token;
  1662. enum exp_opcode opcode;
  1663. int cxx_only;
  1664. };
  1665. static const struct token tokentab3[] =
  1666. {
  1667. {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
  1668. {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
  1669. {"->*", ARROW_STAR, BINOP_END, 1}
  1670. };
  1671. static const struct token tokentab2[] =
  1672. {
  1673. {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
  1674. {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
  1675. {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
  1676. {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
  1677. {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
  1678. {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
  1679. {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
  1680. {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
  1681. {"++", INCREMENT, BINOP_END, 0},
  1682. {"--", DECREMENT, BINOP_END, 0},
  1683. {"->", ARROW, BINOP_END, 0},
  1684. {"&&", ANDAND, BINOP_END, 0},
  1685. {"||", OROR, BINOP_END, 0},
  1686. /* "::" is *not* only C++: gdb overrides its meaning in several
  1687. different ways, e.g., 'filename'::func, function::variable. */
  1688. {"::", COLONCOLON, BINOP_END, 0},
  1689. {"<<", LSH, BINOP_END, 0},
  1690. {">>", RSH, BINOP_END, 0},
  1691. {"==", EQUAL, BINOP_END, 0},
  1692. {"!=", NOTEQUAL, BINOP_END, 0},
  1693. {"<=", LEQ, BINOP_END, 0},
  1694. {">=", GEQ, BINOP_END, 0},
  1695. {".*", DOT_STAR, BINOP_END, 1}
  1696. };
  1697. /* Identifier-like tokens. */
  1698. static const struct token ident_tokens[] =
  1699. {
  1700. {"unsigned", UNSIGNED, OP_NULL, 0},
  1701. {"template", TEMPLATE, OP_NULL, 1},
  1702. {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
  1703. {"struct", STRUCT, OP_NULL, 0},
  1704. {"signed", SIGNED_KEYWORD, OP_NULL, 0},
  1705. {"sizeof", SIZEOF, OP_NULL, 0},
  1706. {"double", DOUBLE_KEYWORD, OP_NULL, 0},
  1707. {"false", FALSEKEYWORD, OP_NULL, 1},
  1708. {"class", CLASS, OP_NULL, 1},
  1709. {"union", UNION, OP_NULL, 0},
  1710. {"short", SHORT, OP_NULL, 0},
  1711. {"const", CONST_KEYWORD,

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