PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Happy | 2473 lines | 2169 code | 304 blank | 0 comment | 0 complexity | 66cbfb99025f09d78090830f55f8f89f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1

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, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
  3. 1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010
  4. Free Software Foundation, Inc.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Parse a C expression from text in a string,
  17. and return the result as a struct expression pointer.
  18. That structure contains arithmetic operations in reverse polish,
  19. with constants represented by operations that are followed by special data.
  20. See expression.h for the details of the format.
  21. What is important here is that it can be built up sequentially
  22. during the process of parsing; the lower levels of the tree always
  23. come first in the result.
  24. Note that malloc's and realloc's in this file are transformed to
  25. xmalloc and xrealloc respectively by the same sed command in the
  26. makefile that remaps any other malloc/realloc inserted by the parser
  27. generator. Doing this with #defines and trying to control the interaction
  28. with include files (<malloc.h> and <stdlib.h> for example) just became
  29. too messy, particularly when such includes can be inserted at random
  30. times by the parser generator. */
  31. %{
  32. #include "defs.h"
  33. #include "gdb_string.h"
  34. #include <ctype.h>
  35. #include "expression.h"
  36. #include "value.h"
  37. #include "parser-defs.h"
  38. #include "language.h"
  39. #include "c-lang.h"
  40. #include "bfd.h" /* Required by objfiles.h. */
  41. #include "symfile.h" /* Required by objfiles.h. */
  42. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  43. #include "charset.h"
  44. #include "block.h"
  45. #include "cp-support.h"
  46. #include "dfp.h"
  47. #include "gdb_assert.h"
  48. #include "macroscope.h"
  49. #define parse_type builtin_type (parse_gdbarch)
  50. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  51. as well as gratuitiously global symbol names, so we can have multiple
  52. yacc generated parsers in gdb. Note that these are only the variables
  53. produced by yacc. If other parser generators (bison, byacc, etc) produce
  54. additional global names that conflict at link time, then those parser
  55. generators need to be fixed instead of adding those names to this list. */
  56. #define yymaxdepth c_maxdepth
  57. #define yyparse c_parse_internal
  58. #define yylex c_lex
  59. #define yyerror c_error
  60. #define yylval c_lval
  61. #define yychar c_char
  62. #define yydebug c_debug
  63. #define yypact c_pact
  64. #define yyr1 c_r1
  65. #define yyr2 c_r2
  66. #define yydef c_def
  67. #define yychk c_chk
  68. #define yypgo c_pgo
  69. #define yyact c_act
  70. #define yyexca c_exca
  71. #define yyerrflag c_errflag
  72. #define yynerrs c_nerrs
  73. #define yyps c_ps
  74. #define yypv c_pv
  75. #define yys c_s
  76. #define yy_yys c_yys
  77. #define yystate c_state
  78. #define yytmp c_tmp
  79. #define yyv c_v
  80. #define yy_yyv c_yyv
  81. #define yyval c_val
  82. #define yylloc c_lloc
  83. #define yyreds c_reds /* With YYDEBUG defined */
  84. #define yytoks c_toks /* With YYDEBUG defined */
  85. #define yyname c_name /* With YYDEBUG defined */
  86. #define yyrule c_rule /* With YYDEBUG defined */
  87. #define yylhs c_yylhs
  88. #define yylen c_yylen
  89. #define yydefred c_yydefred
  90. #define yydgoto c_yydgoto
  91. #define yysindex c_yysindex
  92. #define yyrindex c_yyrindex
  93. #define yygindex c_yygindex
  94. #define yytable c_yytable
  95. #define yycheck c_yycheck
  96. #ifndef YYDEBUG
  97. #define YYDEBUG 1 /* Default to yydebug support */
  98. #endif
  99. #define YYFPRINTF parser_fprintf
  100. int yyparse (void);
  101. static int yylex (void);
  102. void yyerror (char *);
  103. %}
  104. /* Although the yacc "value" of an expression is not used,
  105. since the result is stored in the structure being created,
  106. other node types do have values. */
  107. %union
  108. {
  109. LONGEST lval;
  110. struct {
  111. LONGEST val;
  112. struct type *type;
  113. } typed_val_int;
  114. struct {
  115. DOUBLEST dval;
  116. struct type *type;
  117. } typed_val_float;
  118. struct {
  119. gdb_byte val[16];
  120. struct type *type;
  121. } typed_val_decfloat;
  122. struct symbol *sym;
  123. struct type *tval;
  124. struct stoken sval;
  125. struct typed_stoken tsval;
  126. struct ttype tsym;
  127. struct symtoken ssym;
  128. int voidval;
  129. struct block *bval;
  130. enum exp_opcode opcode;
  131. struct internalvar *ivar;
  132. struct stoken_vector svec;
  133. struct type **tvec;
  134. int *ivec;
  135. }
  136. %{
  137. /* YYSTYPE gets defined by %union */
  138. static int parse_number (char *, int, int, YYSTYPE *);
  139. static struct stoken operator_stoken (const char *);
  140. %}
  141. %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
  142. %type <lval> rcurly
  143. %type <tval> type typebase qualified_type
  144. %type <tvec> nonempty_typelist
  145. /* %type <bval> block */
  146. /* Fancy type parsing. */
  147. %type <voidval> func_mod direct_abs_decl abs_decl
  148. %type <tval> ptype
  149. %type <lval> array_mod
  150. %token <typed_val_int> INT
  151. %token <typed_val_float> FLOAT
  152. %token <typed_val_decfloat> DECFLOAT
  153. /* Both NAME and TYPENAME tokens represent symbols in the input,
  154. and both convey their data as strings.
  155. But a TYPENAME is a string that happens to be defined as a typedef
  156. or builtin type name (such as int or char)
  157. and a NAME is any other symbol.
  158. Contexts where this distinction is not important can use the
  159. nonterminal "name", which matches either NAME or TYPENAME. */
  160. %token <tsval> STRING
  161. %token <tsval> CHAR
  162. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
  163. %token <voidval> COMPLETE
  164. %token <tsym> TYPENAME
  165. %type <sval> name
  166. %type <svec> string_exp
  167. %type <ssym> name_not_typename
  168. %type <tsym> typename
  169. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  170. but which would parse as a valid number in the current input radix.
  171. E.g. "c" when input_radix==16. Depending on the parse, it will be
  172. turned into a name or into a number. */
  173. %token <ssym> NAME_OR_INT
  174. %token OPERATOR
  175. %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
  176. %token TEMPLATE
  177. %token ERROR
  178. %token NEW DELETE
  179. %type <sval> operator
  180. %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
  181. /* Special type cases, put in to allow the parser to distinguish different
  182. legal basetypes. */
  183. %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
  184. %token <voidval> VARIABLE
  185. %token <opcode> ASSIGN_MODIFY
  186. /* C++ */
  187. %token TRUEKEYWORD
  188. %token FALSEKEYWORD
  189. %left ','
  190. %left ABOVE_COMMA
  191. %right '=' ASSIGN_MODIFY
  192. %right '?'
  193. %left OROR
  194. %left ANDAND
  195. %left '|'
  196. %left '^'
  197. %left '&'
  198. %left EQUAL NOTEQUAL
  199. %left '<' '>' LEQ GEQ
  200. %left LSH RSH
  201. %left '@'
  202. %left '+' '-'
  203. %left '*' '/' '%'
  204. %right UNARY INCREMENT DECREMENT
  205. %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
  206. %token <ssym> BLOCKNAME
  207. %token <bval> FILENAME
  208. %type <bval> block
  209. %left COLONCOLON
  210. %%
  211. start : exp1
  212. | type_exp
  213. ;
  214. type_exp: type
  215. { write_exp_elt_opcode(OP_TYPE);
  216. write_exp_elt_type($1);
  217. write_exp_elt_opcode(OP_TYPE);}
  218. ;
  219. /* Expressions, including the comma operator. */
  220. exp1 : exp
  221. | exp1 ',' exp
  222. { write_exp_elt_opcode (BINOP_COMMA); }
  223. ;
  224. /* Expressions, not including the comma operator. */
  225. exp : '*' exp %prec UNARY
  226. { write_exp_elt_opcode (UNOP_IND); }
  227. ;
  228. exp : '&' exp %prec UNARY
  229. { write_exp_elt_opcode (UNOP_ADDR); }
  230. ;
  231. exp : '-' exp %prec UNARY
  232. { write_exp_elt_opcode (UNOP_NEG); }
  233. ;
  234. exp : '+' exp %prec UNARY
  235. { write_exp_elt_opcode (UNOP_PLUS); }
  236. ;
  237. exp : '!' exp %prec UNARY
  238. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  239. ;
  240. exp : '~' exp %prec UNARY
  241. { write_exp_elt_opcode (UNOP_COMPLEMENT); }
  242. ;
  243. exp : INCREMENT exp %prec UNARY
  244. { write_exp_elt_opcode (UNOP_PREINCREMENT); }
  245. ;
  246. exp : DECREMENT exp %prec UNARY
  247. { write_exp_elt_opcode (UNOP_PREDECREMENT); }
  248. ;
  249. exp : exp INCREMENT %prec UNARY
  250. { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
  251. ;
  252. exp : exp DECREMENT %prec UNARY
  253. { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
  254. ;
  255. exp : SIZEOF exp %prec UNARY
  256. { write_exp_elt_opcode (UNOP_SIZEOF); }
  257. ;
  258. exp : exp ARROW name
  259. { write_exp_elt_opcode (STRUCTOP_PTR);
  260. write_exp_string ($3);
  261. write_exp_elt_opcode (STRUCTOP_PTR); }
  262. ;
  263. exp : exp ARROW name COMPLETE
  264. { mark_struct_expression ();
  265. write_exp_elt_opcode (STRUCTOP_PTR);
  266. write_exp_string ($3);
  267. write_exp_elt_opcode (STRUCTOP_PTR); }
  268. ;
  269. exp : exp ARROW COMPLETE
  270. { struct stoken s;
  271. mark_struct_expression ();
  272. write_exp_elt_opcode (STRUCTOP_PTR);
  273. s.ptr = "";
  274. s.length = 0;
  275. write_exp_string (s);
  276. write_exp_elt_opcode (STRUCTOP_PTR); }
  277. ;
  278. exp : exp ARROW qualified_name
  279. { /* exp->type::name becomes exp->*(&type::name) */
  280. /* Note: this doesn't work if name is a
  281. static member! FIXME */
  282. write_exp_elt_opcode (UNOP_ADDR);
  283. write_exp_elt_opcode (STRUCTOP_MPTR); }
  284. ;
  285. exp : exp ARROW_STAR exp
  286. { write_exp_elt_opcode (STRUCTOP_MPTR); }
  287. ;
  288. exp : exp '.' name
  289. { write_exp_elt_opcode (STRUCTOP_STRUCT);
  290. write_exp_string ($3);
  291. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  292. ;
  293. exp : exp '.' name COMPLETE
  294. { mark_struct_expression ();
  295. write_exp_elt_opcode (STRUCTOP_STRUCT);
  296. write_exp_string ($3);
  297. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  298. ;
  299. exp : exp '.' COMPLETE
  300. { struct stoken s;
  301. mark_struct_expression ();
  302. write_exp_elt_opcode (STRUCTOP_STRUCT);
  303. s.ptr = "";
  304. s.length = 0;
  305. write_exp_string (s);
  306. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  307. ;
  308. exp : exp '.' qualified_name
  309. { /* exp.type::name becomes exp.*(&type::name) */
  310. /* Note: this doesn't work if name is a
  311. static member! FIXME */
  312. write_exp_elt_opcode (UNOP_ADDR);
  313. write_exp_elt_opcode (STRUCTOP_MEMBER); }
  314. ;
  315. exp : exp DOT_STAR exp
  316. { write_exp_elt_opcode (STRUCTOP_MEMBER); }
  317. ;
  318. exp : exp '[' exp1 ']'
  319. { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
  320. ;
  321. exp : exp '('
  322. /* This is to save the value of arglist_len
  323. being accumulated by an outer function call. */
  324. { start_arglist (); }
  325. arglist ')' %prec ARROW
  326. { write_exp_elt_opcode (OP_FUNCALL);
  327. write_exp_elt_longcst ((LONGEST) end_arglist ());
  328. write_exp_elt_opcode (OP_FUNCALL); }
  329. ;
  330. lcurly : '{'
  331. { start_arglist (); }
  332. ;
  333. arglist :
  334. ;
  335. arglist : exp
  336. { arglist_len = 1; }
  337. ;
  338. arglist : arglist ',' exp %prec ABOVE_COMMA
  339. { arglist_len++; }
  340. ;
  341. exp : exp '(' nonempty_typelist ')' const_or_volatile
  342. { int i;
  343. write_exp_elt_opcode (TYPE_INSTANCE);
  344. write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
  345. for (i = 0; i < $<ivec>3[0]; ++i)
  346. write_exp_elt_type ($<tvec>3[i + 1]);
  347. write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
  348. write_exp_elt_opcode (TYPE_INSTANCE);
  349. free ($3);
  350. }
  351. ;
  352. rcurly : '}'
  353. { $$ = end_arglist () - 1; }
  354. ;
  355. exp : lcurly arglist rcurly %prec ARROW
  356. { write_exp_elt_opcode (OP_ARRAY);
  357. write_exp_elt_longcst ((LONGEST) 0);
  358. write_exp_elt_longcst ((LONGEST) $3);
  359. write_exp_elt_opcode (OP_ARRAY); }
  360. ;
  361. exp : lcurly type rcurly exp %prec UNARY
  362. { write_exp_elt_opcode (UNOP_MEMVAL);
  363. write_exp_elt_type ($2);
  364. write_exp_elt_opcode (UNOP_MEMVAL); }
  365. ;
  366. exp : '(' type ')' exp %prec UNARY
  367. { write_exp_elt_opcode (UNOP_CAST);
  368. write_exp_elt_type ($2);
  369. write_exp_elt_opcode (UNOP_CAST); }
  370. ;
  371. exp : '(' exp1 ')'
  372. { }
  373. ;
  374. /* Binary operators in order of decreasing precedence. */
  375. exp : exp '@' exp
  376. { write_exp_elt_opcode (BINOP_REPEAT); }
  377. ;
  378. exp : exp '*' exp
  379. { write_exp_elt_opcode (BINOP_MUL); }
  380. ;
  381. exp : exp '/' exp
  382. { write_exp_elt_opcode (BINOP_DIV); }
  383. ;
  384. exp : exp '%' exp
  385. { write_exp_elt_opcode (BINOP_REM); }
  386. ;
  387. exp : exp '+' exp
  388. { write_exp_elt_opcode (BINOP_ADD); }
  389. ;
  390. exp : exp '-' exp
  391. { write_exp_elt_opcode (BINOP_SUB); }
  392. ;
  393. exp : exp LSH exp
  394. { write_exp_elt_opcode (BINOP_LSH); }
  395. ;
  396. exp : exp RSH exp
  397. { write_exp_elt_opcode (BINOP_RSH); }
  398. ;
  399. exp : exp EQUAL exp
  400. { write_exp_elt_opcode (BINOP_EQUAL); }
  401. ;
  402. exp : exp NOTEQUAL exp
  403. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  404. ;
  405. exp : exp LEQ exp
  406. { write_exp_elt_opcode (BINOP_LEQ); }
  407. ;
  408. exp : exp GEQ exp
  409. { write_exp_elt_opcode (BINOP_GEQ); }
  410. ;
  411. exp : exp '<' exp
  412. { write_exp_elt_opcode (BINOP_LESS); }
  413. ;
  414. exp : exp '>' exp
  415. { write_exp_elt_opcode (BINOP_GTR); }
  416. ;
  417. exp : exp '&' exp
  418. { write_exp_elt_opcode (BINOP_BITWISE_AND); }
  419. ;
  420. exp : exp '^' exp
  421. { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
  422. ;
  423. exp : exp '|' exp
  424. { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
  425. ;
  426. exp : exp ANDAND exp
  427. { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
  428. ;
  429. exp : exp OROR exp
  430. { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
  431. ;
  432. exp : exp '?' exp ':' exp %prec '?'
  433. { write_exp_elt_opcode (TERNOP_COND); }
  434. ;
  435. exp : exp '=' exp
  436. { write_exp_elt_opcode (BINOP_ASSIGN); }
  437. ;
  438. exp : exp ASSIGN_MODIFY exp
  439. { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  440. write_exp_elt_opcode ($2);
  441. write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  442. ;
  443. exp : INT
  444. { write_exp_elt_opcode (OP_LONG);
  445. write_exp_elt_type ($1.type);
  446. write_exp_elt_longcst ((LONGEST)($1.val));
  447. write_exp_elt_opcode (OP_LONG); }
  448. ;
  449. exp : CHAR
  450. {
  451. struct stoken_vector vec;
  452. vec.len = 1;
  453. vec.tokens = &$1;
  454. write_exp_string_vector ($1.type, &vec);
  455. }
  456. ;
  457. exp : NAME_OR_INT
  458. { YYSTYPE val;
  459. parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
  460. write_exp_elt_opcode (OP_LONG);
  461. write_exp_elt_type (val.typed_val_int.type);
  462. write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
  463. write_exp_elt_opcode (OP_LONG);
  464. }
  465. ;
  466. exp : FLOAT
  467. { write_exp_elt_opcode (OP_DOUBLE);
  468. write_exp_elt_type ($1.type);
  469. write_exp_elt_dblcst ($1.dval);
  470. write_exp_elt_opcode (OP_DOUBLE); }
  471. ;
  472. exp : DECFLOAT
  473. { write_exp_elt_opcode (OP_DECFLOAT);
  474. write_exp_elt_type ($1.type);
  475. write_exp_elt_decfloatcst ($1.val);
  476. write_exp_elt_opcode (OP_DECFLOAT); }
  477. ;
  478. exp : variable
  479. ;
  480. exp : VARIABLE
  481. /* Already written by write_dollar_variable. */
  482. ;
  483. exp : SIZEOF '(' type ')' %prec UNARY
  484. { write_exp_elt_opcode (OP_LONG);
  485. write_exp_elt_type (parse_type->builtin_int);
  486. CHECK_TYPEDEF ($3);
  487. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  488. write_exp_elt_opcode (OP_LONG); }
  489. ;
  490. exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
  491. { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
  492. write_exp_elt_type ($3);
  493. write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
  494. ;
  495. exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
  496. { write_exp_elt_opcode (UNOP_CAST);
  497. write_exp_elt_type ($3);
  498. write_exp_elt_opcode (UNOP_CAST); }
  499. ;
  500. exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
  501. { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
  502. write_exp_elt_type ($3);
  503. write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
  504. ;
  505. exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
  506. { /* We could do more error checking here, but
  507. it doesn't seem worthwhile. */
  508. write_exp_elt_opcode (UNOP_CAST);
  509. write_exp_elt_type ($3);
  510. write_exp_elt_opcode (UNOP_CAST); }
  511. ;
  512. string_exp:
  513. STRING
  514. {
  515. /* We copy the string here, and not in the
  516. lexer, to guarantee that we do not leak a
  517. string. Note that we follow the
  518. NUL-termination convention of the
  519. lexer. */
  520. struct typed_stoken *vec = XNEW (struct typed_stoken);
  521. $$.len = 1;
  522. $$.tokens = vec;
  523. vec->type = $1.type;
  524. vec->length = $1.length;
  525. vec->ptr = malloc ($1.length + 1);
  526. memcpy (vec->ptr, $1.ptr, $1.length + 1);
  527. }
  528. | string_exp STRING
  529. {
  530. /* Note that we NUL-terminate here, but just
  531. for convenience. */
  532. char *p;
  533. ++$$.len;
  534. $$.tokens = realloc ($$.tokens,
  535. $$.len * sizeof (struct typed_stoken));
  536. p = malloc ($2.length + 1);
  537. memcpy (p, $2.ptr, $2.length + 1);
  538. $$.tokens[$$.len - 1].type = $2.type;
  539. $$.tokens[$$.len - 1].length = $2.length;
  540. $$.tokens[$$.len - 1].ptr = p;
  541. }
  542. ;
  543. exp : string_exp
  544. {
  545. int i;
  546. enum c_string_type type = C_STRING;
  547. for (i = 0; i < $1.len; ++i)
  548. {
  549. switch ($1.tokens[i].type)
  550. {
  551. case C_STRING:
  552. break;
  553. case C_WIDE_STRING:
  554. case C_STRING_16:
  555. case C_STRING_32:
  556. if (type != C_STRING
  557. && type != $1.tokens[i].type)
  558. error ("Undefined string concatenation.");
  559. type = $1.tokens[i].type;
  560. break;
  561. default:
  562. /* internal error */
  563. internal_error (__FILE__, __LINE__,
  564. "unrecognized type in string concatenation");
  565. }
  566. }
  567. write_exp_string_vector (type, &$1);
  568. for (i = 0; i < $1.len; ++i)
  569. free ($1.tokens[i].ptr);
  570. free ($1.tokens);
  571. }
  572. ;
  573. /* C++. */
  574. exp : TRUEKEYWORD
  575. { write_exp_elt_opcode (OP_LONG);
  576. write_exp_elt_type (parse_type->builtin_bool);
  577. write_exp_elt_longcst ((LONGEST) 1);
  578. write_exp_elt_opcode (OP_LONG); }
  579. ;
  580. exp : FALSEKEYWORD
  581. { write_exp_elt_opcode (OP_LONG);
  582. write_exp_elt_type (parse_type->builtin_bool);
  583. write_exp_elt_longcst ((LONGEST) 0);
  584. write_exp_elt_opcode (OP_LONG); }
  585. ;
  586. /* end of C++. */
  587. block : BLOCKNAME
  588. {
  589. if ($1.sym)
  590. $$ = SYMBOL_BLOCK_VALUE ($1.sym);
  591. else
  592. error ("No file or function \"%s\".",
  593. copy_name ($1.stoken));
  594. }
  595. | FILENAME
  596. {
  597. $$ = $1;
  598. }
  599. ;
  600. block : block COLONCOLON name
  601. { struct symbol *tem
  602. = lookup_symbol (copy_name ($3), $1,
  603. VAR_DOMAIN, (int *) NULL);
  604. if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  605. error ("No function \"%s\" in specified context.",
  606. copy_name ($3));
  607. $$ = SYMBOL_BLOCK_VALUE (tem); }
  608. ;
  609. variable: block COLONCOLON name
  610. { struct symbol *sym;
  611. sym = lookup_symbol (copy_name ($3), $1,
  612. VAR_DOMAIN, (int *) NULL);
  613. if (sym == 0)
  614. error ("No symbol \"%s\" in specified context.",
  615. copy_name ($3));
  616. write_exp_elt_opcode (OP_VAR_VALUE);
  617. /* block_found is set by lookup_symbol. */
  618. write_exp_elt_block (block_found);
  619. write_exp_elt_sym (sym);
  620. write_exp_elt_opcode (OP_VAR_VALUE); }
  621. ;
  622. qualified_name: typebase COLONCOLON name
  623. {
  624. struct type *type = $1;
  625. CHECK_TYPEDEF (type);
  626. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  627. && TYPE_CODE (type) != TYPE_CODE_UNION
  628. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  629. error ("`%s' is not defined as an aggregate type.",
  630. TYPE_NAME (type));
  631. write_exp_elt_opcode (OP_SCOPE);
  632. write_exp_elt_type (type);
  633. write_exp_string ($3);
  634. write_exp_elt_opcode (OP_SCOPE);
  635. }
  636. | typebase COLONCOLON '~' name
  637. {
  638. struct type *type = $1;
  639. struct stoken tmp_token;
  640. CHECK_TYPEDEF (type);
  641. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  642. && TYPE_CODE (type) != TYPE_CODE_UNION
  643. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  644. error ("`%s' is not defined as an aggregate type.",
  645. TYPE_NAME (type));
  646. tmp_token.ptr = (char*) alloca ($4.length + 2);
  647. tmp_token.length = $4.length + 1;
  648. tmp_token.ptr[0] = '~';
  649. memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
  650. tmp_token.ptr[tmp_token.length] = 0;
  651. /* Check for valid destructor name. */
  652. destructor_name_p (tmp_token.ptr, type);
  653. write_exp_elt_opcode (OP_SCOPE);
  654. write_exp_elt_type (type);
  655. write_exp_string (tmp_token);
  656. write_exp_elt_opcode (OP_SCOPE);
  657. }
  658. ;
  659. variable: qualified_name
  660. | COLONCOLON name
  661. {
  662. char *name = copy_name ($2);
  663. struct symbol *sym;
  664. struct minimal_symbol *msymbol;
  665. sym =
  666. lookup_symbol (name, (const struct block *) NULL,
  667. VAR_DOMAIN, (int *) NULL);
  668. if (sym)
  669. {
  670. write_exp_elt_opcode (OP_VAR_VALUE);
  671. write_exp_elt_block (NULL);
  672. write_exp_elt_sym (sym);
  673. write_exp_elt_opcode (OP_VAR_VALUE);
  674. break;
  675. }
  676. msymbol = lookup_minimal_symbol (name, NULL, NULL);
  677. if (msymbol != NULL)
  678. write_exp_msymbol (msymbol);
  679. else if (!have_full_symbols () && !have_partial_symbols ())
  680. error ("No symbol table is loaded. Use the \"file\" command.");
  681. else
  682. error ("No symbol \"%s\" in current context.", name);
  683. }
  684. ;
  685. variable: name_not_typename
  686. { struct symbol *sym = $1.sym;
  687. if (sym)
  688. {
  689. if (symbol_read_needs_frame (sym))
  690. {
  691. if (innermost_block == 0
  692. || contained_in (block_found,
  693. innermost_block))
  694. innermost_block = block_found;
  695. }
  696. write_exp_elt_opcode (OP_VAR_VALUE);
  697. /* We want to use the selected frame, not
  698. another more inner frame which happens to
  699. be in the same block. */
  700. write_exp_elt_block (NULL);
  701. write_exp_elt_sym (sym);
  702. write_exp_elt_opcode (OP_VAR_VALUE);
  703. }
  704. else if ($1.is_a_field_of_this)
  705. {
  706. /* C++: it hangs off of `this'. Must
  707. not inadvertently convert from a method call
  708. to data ref. */
  709. if (innermost_block == 0
  710. || contained_in (block_found,
  711. innermost_block))
  712. innermost_block = block_found;
  713. write_exp_elt_opcode (OP_THIS);
  714. write_exp_elt_opcode (OP_THIS);
  715. write_exp_elt_opcode (STRUCTOP_PTR);
  716. write_exp_string ($1.stoken);
  717. write_exp_elt_opcode (STRUCTOP_PTR);
  718. }
  719. else
  720. {
  721. struct minimal_symbol *msymbol;
  722. char *arg = copy_name ($1.stoken);
  723. msymbol =
  724. lookup_minimal_symbol (arg, NULL, NULL);
  725. if (msymbol != NULL)
  726. write_exp_msymbol (msymbol);
  727. else if (!have_full_symbols () && !have_partial_symbols ())
  728. error ("No symbol table is loaded. Use the \"file\" command.");
  729. else
  730. error ("No symbol \"%s\" in current context.",
  731. copy_name ($1.stoken));
  732. }
  733. }
  734. ;
  735. space_identifier : '@' NAME
  736. { push_type_address_space (copy_name ($2.stoken));
  737. push_type (tp_space_identifier);
  738. }
  739. ;
  740. const_or_volatile: const_or_volatile_noopt
  741. |
  742. ;
  743. cv_with_space_id : const_or_volatile space_identifier const_or_volatile
  744. ;
  745. const_or_volatile_or_space_identifier_noopt: cv_with_space_id
  746. | const_or_volatile_noopt
  747. ;
  748. const_or_volatile_or_space_identifier:
  749. const_or_volatile_or_space_identifier_noopt
  750. |
  751. ;
  752. abs_decl: '*'
  753. { push_type (tp_pointer); $$ = 0; }
  754. | '*' abs_decl
  755. { push_type (tp_pointer); $$ = $2; }
  756. | '&'
  757. { push_type (tp_reference); $$ = 0; }
  758. | '&' abs_decl
  759. { push_type (tp_reference); $$ = $2; }
  760. | direct_abs_decl
  761. ;
  762. direct_abs_decl: '(' abs_decl ')'
  763. { $$ = $2; }
  764. | direct_abs_decl array_mod
  765. {
  766. push_type_int ($2);
  767. push_type (tp_array);
  768. }
  769. | array_mod
  770. {
  771. push_type_int ($1);
  772. push_type (tp_array);
  773. $$ = 0;
  774. }
  775. | direct_abs_decl func_mod
  776. { push_type (tp_function); }
  777. | func_mod
  778. { push_type (tp_function); }
  779. ;
  780. array_mod: '[' ']'
  781. { $$ = -1; }
  782. | '[' INT ']'
  783. { $$ = $2.val; }
  784. ;
  785. func_mod: '(' ')'
  786. { $$ = 0; }
  787. | '(' nonempty_typelist ')'
  788. { free ($2); $$ = 0; }
  789. ;
  790. /* We used to try to recognize pointer to member types here, but
  791. that didn't work (shift/reduce conflicts meant that these rules never
  792. got executed). The problem is that
  793. int (foo::bar::baz::bizzle)
  794. is a function type but
  795. int (foo::bar::baz::bizzle::*)
  796. is a pointer to member type. Stroustrup loses again! */
  797. type : ptype
  798. ;
  799. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  800. : TYPENAME
  801. { $$ = $1.type; }
  802. | INT_KEYWORD
  803. { $$ = parse_type->builtin_int; }
  804. | LONG
  805. { $$ = parse_type->builtin_long; }
  806. | SHORT
  807. { $$ = parse_type->builtin_short; }
  808. | LONG INT_KEYWORD
  809. { $$ = parse_type->builtin_long; }
  810. | LONG SIGNED_KEYWORD INT_KEYWORD
  811. { $$ = parse_type->builtin_long; }
  812. | LONG SIGNED_KEYWORD
  813. { $$ = parse_type->builtin_long; }
  814. | SIGNED_KEYWORD LONG INT_KEYWORD
  815. { $$ = parse_type->builtin_long; }
  816. | UNSIGNED LONG INT_KEYWORD
  817. { $$ = parse_type->builtin_unsigned_long; }
  818. | LONG UNSIGNED INT_KEYWORD
  819. { $$ = parse_type->builtin_unsigned_long; }
  820. | LONG UNSIGNED
  821. { $$ = parse_type->builtin_unsigned_long; }
  822. | LONG LONG
  823. { $$ = parse_type->builtin_long_long; }
  824. | LONG LONG INT_KEYWORD
  825. { $$ = parse_type->builtin_long_long; }
  826. | LONG LONG SIGNED_KEYWORD INT_KEYWORD
  827. { $$ = parse_type->builtin_long_long; }
  828. | LONG LONG SIGNED_KEYWORD
  829. { $$ = parse_type->builtin_long_long; }
  830. | SIGNED_KEYWORD LONG LONG
  831. { $$ = parse_type->builtin_long_long; }
  832. | SIGNED_KEYWORD LONG LONG INT_KEYWORD
  833. { $$ = parse_type->builtin_long_long; }
  834. | UNSIGNED LONG LONG
  835. { $$ = parse_type->builtin_unsigned_long_long; }
  836. | UNSIGNED LONG LONG INT_KEYWORD
  837. { $$ = parse_type->builtin_unsigned_long_long; }
  838. | LONG LONG UNSIGNED
  839. { $$ = parse_type->builtin_unsigned_long_long; }
  840. | LONG LONG UNSIGNED INT_KEYWORD
  841. { $$ = parse_type->builtin_unsigned_long_long; }
  842. | SHORT INT_KEYWORD
  843. { $$ = parse_type->builtin_short; }
  844. | SHORT SIGNED_KEYWORD INT_KEYWORD
  845. { $$ = parse_type->builtin_short; }
  846. | SHORT SIGNED_KEYWORD
  847. { $$ = parse_type->builtin_short; }
  848. | UNSIGNED SHORT INT_KEYWORD
  849. { $$ = parse_type->builtin_unsigned_short; }
  850. | SHORT UNSIGNED
  851. { $$ = parse_type->builtin_unsigned_short; }
  852. | SHORT UNSIGNED INT_KEYWORD
  853. { $$ = parse_type->builtin_unsigned_short; }
  854. | DOUBLE_KEYWORD
  855. { $$ = parse_type->builtin_double; }
  856. | LONG DOUBLE_KEYWORD
  857. { $$ = parse_type->builtin_long_double; }
  858. | STRUCT name
  859. { $$ = lookup_struct (copy_name ($2),
  860. expression_context_block); }
  861. | CLASS name
  862. { $$ = lookup_struct (copy_name ($2),
  863. expression_context_block); }
  864. | UNION name
  865. { $$ = lookup_union (copy_name ($2),
  866. expression_context_block); }
  867. | ENUM name
  868. { $$ = lookup_enum (copy_name ($2),
  869. expression_context_block); }
  870. | UNSIGNED typename
  871. { $$ = lookup_unsigned_typename (parse_language,
  872. parse_gdbarch,
  873. TYPE_NAME($2.type)); }
  874. | UNSIGNED
  875. { $$ = parse_type->builtin_unsigned_int; }
  876. | SIGNED_KEYWORD typename
  877. { $$ = lookup_signed_typename (parse_language,
  878. parse_gdbarch,
  879. TYPE_NAME($2.type)); }
  880. | SIGNED_KEYWORD
  881. { $$ = parse_type->builtin_int; }
  882. /* It appears that this rule for templates is never
  883. reduced; template recognition happens by lookahead
  884. in the token processing code in yylex. */
  885. | TEMPLATE name '<' type '>'
  886. { $$ = lookup_template_type(copy_name($2), $4,
  887. expression_context_block);
  888. }
  889. | const_or_volatile_or_space_identifier_noopt typebase
  890. { $$ = follow_types ($2); }
  891. | typebase const_or_volatile_or_space_identifier_noopt
  892. { $$ = follow_types ($1); }
  893. | qualified_type
  894. ;
  895. /* FIXME: carlton/2003-09-25: This next bit leads to lots of
  896. reduce-reduce conflicts, because the parser doesn't know whether or
  897. not to use qualified_name or qualified_type: the rules are
  898. identical. If the parser is parsing 'A::B::x', then, when it sees
  899. the second '::', it knows that the expression to the left of it has
  900. to be a type, so it uses qualified_type. But if it is parsing just
  901. 'A::B', then it doesn't have any way of knowing which rule to use,
  902. so there's a reduce-reduce conflict; it picks qualified_name, since
  903. that occurs earlier in this file than qualified_type.
  904. There's no good way to fix this with the grammar as it stands; as
  905. far as I can tell, some of the problems arise from ambiguities that
  906. GDB introduces ('start' can be either an expression or a type), but
  907. some of it is inherent to the nature of C++ (you want to treat the
  908. input "(FOO)" fairly differently depending on whether FOO is an
  909. expression or a type, and if FOO is a complex expression, this can
  910. be hard to determine at the right time). Fortunately, it works
  911. pretty well in most cases. For example, if you do 'ptype A::B',
  912. where A::B is a nested type, then the parser will mistakenly
  913. misidentify it as an expression; but evaluate_subexp will get
  914. called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
  915. will work out anyways. But there are situations where the parser
  916. will get confused: the most common one that I've run into is when
  917. you want to do
  918. print *((A::B *) x)"
  919. where the parser doesn't realize that A::B has to be a type until
  920. it hits the first right paren, at which point it's too late. (The
  921. workaround is to type "print *(('A::B' *) x)" instead.) (And
  922. another solution is to fix our symbol-handling code so that the
  923. user never wants to type something like that in the first place,
  924. because we get all the types right without the user's help!)
  925. Perhaps we could fix this by making the lexer smarter. Some of
  926. this functionality used to be in the lexer, but in a way that
  927. worked even less well than the current solution: that attempt
  928. involved having the parser sometimes handle '::' and having the
  929. lexer sometimes handle it, and without a clear division of
  930. responsibility, it quickly degenerated into a big mess. Probably
  931. the eventual correct solution will give more of a role to the lexer
  932. (ideally via code that is shared between the lexer and
  933. decode_line_1), but I'm not holding my breath waiting for somebody
  934. to get around to cleaning this up... */
  935. qualified_type: typebase COLONCOLON name
  936. {
  937. struct type *type = $1;
  938. struct type *new_type;
  939. char *ncopy = alloca ($3.length + 1);
  940. memcpy (ncopy, $3.ptr, $3.length);
  941. ncopy[$3.length] = '\0';
  942. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  943. && TYPE_CODE (type) != TYPE_CODE_UNION
  944. && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
  945. error ("`%s' is not defined as an aggregate type.",
  946. TYPE_NAME (type));
  947. new_type = cp_lookup_nested_type (type, ncopy,
  948. expression_context_block);
  949. if (new_type == NULL)
  950. error ("No type \"%s\" within class or namespace \"%s\".",
  951. ncopy, TYPE_NAME (type));
  952. $$ = new_type;
  953. }
  954. ;
  955. typename: TYPENAME
  956. | INT_KEYWORD
  957. {
  958. $$.stoken.ptr = "int";
  959. $$.stoken.length = 3;
  960. $$.type = parse_type->builtin_int;
  961. }
  962. | LONG
  963. {
  964. $$.stoken.ptr = "long";
  965. $$.stoken.length = 4;
  966. $$.type = parse_type->builtin_long;
  967. }
  968. | SHORT
  969. {
  970. $$.stoken.ptr = "short";
  971. $$.stoken.length = 5;
  972. $$.type = parse_type->builtin_short;
  973. }
  974. ;
  975. nonempty_typelist
  976. : type
  977. { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  978. $<ivec>$[0] = 1; /* Number of types in vector */
  979. $$[1] = $1;
  980. }
  981. | nonempty_typelist ',' type
  982. { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  983. $$ = (struct type **) realloc ((char *) $1, len);
  984. $$[$<ivec>$[0]] = $3;
  985. }
  986. ;
  987. ptype : typebase
  988. | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
  989. { $$ = follow_types ($1); }
  990. ;
  991. const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
  992. | VOLATILE_KEYWORD CONST_KEYWORD
  993. ;
  994. const_or_volatile_noopt: const_and_volatile
  995. { push_type (tp_const);
  996. push_type (tp_volatile);
  997. }
  998. | CONST_KEYWORD
  999. { push_type (tp_const); }
  1000. | VOLATILE_KEYWORD
  1001. { push_type (tp_volatile); }
  1002. ;
  1003. operator: OPERATOR NEW
  1004. { $$ = operator_stoken (" new"); }
  1005. | OPERATOR DELETE
  1006. { $$ = operator_stoken (" delete"); }
  1007. | OPERATOR NEW '[' ']'
  1008. { $$ = operator_stoken (" new[]"); }
  1009. | OPERATOR DELETE '[' ']'
  1010. { $$ = operator_stoken (" delete[]"); }
  1011. | OPERATOR '+'
  1012. { $$ = operator_stoken ("+"); }
  1013. | OPERATOR '-'
  1014. { $$ = operator_stoken ("-"); }
  1015. | OPERATOR '*'
  1016. { $$ = operator_stoken ("*"); }
  1017. | OPERATOR '/'
  1018. { $$ = operator_stoken ("/"); }
  1019. | OPERATOR '%'
  1020. { $$ = operator_stoken ("%"); }
  1021. | OPERATOR '^'
  1022. { $$ = operator_stoken ("^"); }
  1023. | OPERATOR '&'
  1024. { $$ = operator_stoken ("&"); }
  1025. | OPERATOR '|'
  1026. { $$ = operator_stoken ("|"); }
  1027. | OPERATOR '~'
  1028. { $$ = operator_stoken ("~"); }
  1029. | OPERATOR '!'
  1030. { $$ = operator_stoken ("!"); }
  1031. | OPERATOR '='
  1032. { $$ = operator_stoken ("="); }
  1033. | OPERATOR '<'
  1034. { $$ = operator_stoken ("<"); }
  1035. | OPERATOR '>'
  1036. { $$ = operator_stoken (">"); }
  1037. | OPERATOR ASSIGN_MODIFY
  1038. { const char *op = "unknown";
  1039. switch ($2)
  1040. {
  1041. case BINOP_RSH:
  1042. op = ">>=";
  1043. break;
  1044. case BINOP_LSH:
  1045. op = "<<=";
  1046. break;
  1047. case BINOP_ADD:
  1048. op = "+=";
  1049. break;
  1050. case BINOP_SUB:
  1051. op = "-=";
  1052. break;
  1053. case BINOP_MUL:
  1054. op = "*=";
  1055. break;
  1056. case BINOP_DIV:
  1057. op = "/=";
  1058. break;
  1059. case BINOP_REM:
  1060. op = "%=";
  1061. break;
  1062. case BINOP_BITWISE_IOR:
  1063. op = "|=";
  1064. break;
  1065. case BINOP_BITWISE_AND:
  1066. op = "&=";
  1067. break;
  1068. case BINOP_BITWISE_XOR:
  1069. op = "^=";
  1070. break;
  1071. default:
  1072. break;
  1073. }
  1074. $$ = operator_stoken (op);
  1075. }
  1076. | OPERATOR LSH
  1077. { $$ = operator_stoken ("<<"); }
  1078. | OPERATOR RSH
  1079. { $$ = operator_stoken (">>"); }
  1080. | OPERATOR EQUAL
  1081. { $$ = operator_stoken ("=="); }
  1082. | OPERATOR NOTEQUAL
  1083. { $$ = operator_stoken ("!="); }
  1084. | OPERATOR LEQ
  1085. { $$ = operator_stoken ("<="); }
  1086. | OPERATOR GEQ
  1087. { $$ = operator_stoken (">="); }
  1088. | OPERATOR ANDAND
  1089. { $$ = operator_stoken ("&&"); }
  1090. | OPERATOR OROR
  1091. { $$ = operator_stoken ("||"); }
  1092. | OPERATOR INCREMENT
  1093. { $$ = operator_stoken ("++"); }
  1094. | OPERATOR DECREMENT
  1095. { $$ = operator_stoken ("--"); }
  1096. | OPERATOR ','
  1097. { $$ = operator_stoken (","); }
  1098. | OPERATOR ARROW_STAR
  1099. { $$ = operator_stoken ("->*"); }
  1100. | OPERATOR ARROW
  1101. { $$ = operator_stoken ("->"); }
  1102. | OPERATOR '(' ')'
  1103. { $$ = operator_stoken ("()"); }
  1104. | OPERATOR '[' ']'
  1105. { $$ = operator_stoken ("[]"); }
  1106. | OPERATOR ptype
  1107. { char *name;
  1108. long length;
  1109. struct ui_file *buf = mem_fileopen ();
  1110. c_print_type ($2, NULL, buf, -1, 0);
  1111. name = ui_file_xstrdup (buf, &length);
  1112. ui_file_delete (buf);
  1113. $$ = operator_stoken (name);
  1114. free (name);
  1115. }
  1116. ;
  1117. name : NAME { $$ = $1.stoken; }
  1118. | BLOCKNAME { $$ = $1.stoken; }
  1119. | TYPENAME { $$ = $1.stoken; }
  1120. | NAME_OR_INT { $$ = $1.stoken; }
  1121. | operator { $$ = $1; }
  1122. ;
  1123. name_not_typename : NAME
  1124. | BLOCKNAME
  1125. /* These would be useful if name_not_typename was useful, but it is just
  1126. a fake for "variable", so these cause reduce/reduce conflicts because
  1127. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  1128. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  1129. context where only a name could occur, this might be useful.
  1130. | NAME_OR_INT
  1131. */
  1132. ;
  1133. %%
  1134. /* Returns a stoken of the operator name given by OP (which does not
  1135. include the string "operator"). */
  1136. static struct stoken
  1137. operator_stoken (const char *op)
  1138. {
  1139. static const char *operator_string = "operator";
  1140. struct stoken st = { NULL, 0 };
  1141. st.length = strlen (operator_string) + strlen (op);
  1142. st.ptr = malloc (st.length + 1);
  1143. strcpy (st.ptr, operator_string);
  1144. strcat (st.ptr, op);
  1145. /* The toplevel (c_parse) will free the memory allocated here. */
  1146. make_cleanup (free, st.ptr);
  1147. return st;
  1148. };
  1149. /* Take care of parsing a number (anything that starts with a digit).
  1150. Set yylval and return the token type; update lexptr.
  1151. LEN is the number of characters in it. */
  1152. /*** Needs some error checking for the float case ***/
  1153. static int
  1154. parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
  1155. {
  1156. /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
  1157. here, and we do kind of silly things like cast to unsigned. */
  1158. LONGEST n = 0;
  1159. LONGEST prevn = 0;
  1160. ULONGEST un;
  1161. int i = 0;
  1162. int c;
  1163. int base = input_radix;
  1164. int unsigned_p = 0;
  1165. /* Number of "L" suffixes encountered. */
  1166. int long_p = 0;
  1167. /* We have found a "L" or "U" suffix. */
  1168. int found_suffix = 0;
  1169. ULONGEST high_bit;
  1170. struct type *signed_type;
  1171. struct type *unsigned_type;
  1172. if (parsed_float)
  1173. {
  1174. /* It's a float since it contains a point or an exponent. */
  1175. char *s;
  1176. int num; /* number of tokens scanned by scanf */
  1177. char saved_char;
  1178. /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
  1179. point. Return DECFLOAT. */
  1180. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
  1181. {
  1182. p[len - 2] = '\0';
  1183. putithere->typed_val_decfloat.type
  1184. = parse_type->builtin_decfloat;
  1185. decimal_from_string (putithere->typed_val_decfloat.val, 4,
  1186. gdbarch_byte_order (parse_gdbarch), p);
  1187. p[len - 2] = 'd';
  1188. return DECFLOAT;
  1189. }
  1190. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
  1191. {
  1192. p[len - 2] = '\0';
  1193. putithere->typed_val_decfloat.type
  1194. = parse_type->builtin_decdouble;
  1195. decimal_from_string (putithere->typed_val_decfloat.val, 8,
  1196. gdbarch_byte_order (parse_gdbarch), p);
  1197. p[len - 2] = 'd';
  1198. return DECFLOAT;
  1199. }
  1200. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
  1201. {
  1202. p[len - 2] = '\0';
  1203. putithere->typed_val_decfloat.type
  1204. = parse_type->builtin_declong;
  1205. decimal_from_string (putithere->typed_val_decfloat.val, 16,
  1206. gdbarch_byte_order (parse_gdbarch), p);
  1207. p[len - 2] = 'd';
  1208. return DECFLOAT;
  1209. }
  1210. s = malloc (len);
  1211. saved_char = p[len];
  1212. p[len] = 0; /* null-terminate the token */
  1213. num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
  1214. &putithere->typed_val_float.dval, s);
  1215. p[len] = saved_char; /* restore the input stream */
  1216. if (num == 1)
  1217. putithere->typed_val_float.type =
  1218. parse_type->builtin_double;
  1219. if (num == 2 )
  1220. {
  1221. /* See if it has any float suffix: 'f' for float, 'l' for long
  1222. double. */
  1223. if (!strcasecmp (s, "f"))
  1224. putithere->typed_val_float.type =
  1225. parse_type->builtin_float;
  1226. else if (!strcasecmp (s, "l"))
  1227. putithere->typed_val_float.type =
  1228. parse_type->builtin_long_double;
  1229. else
  1230. {
  1231. free (s);
  1232. return ERROR;
  1233. }
  1234. }
  1235. free (s);
  1236. return FLOAT;
  1237. }
  1238. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  1239. if (p[0] == '0')
  1240. switch (p[1])
  1241. {
  1242. case 'x':
  1243. case 'X':
  1244. if (len >= 3)
  1245. {
  1246. p += 2;
  1247. base = 16;
  1248. len -= 2;
  1249. }
  1250. break;
  1251. case 'b':
  1252. case 'B':
  1253. if (len >= 3)
  1254. {
  1255. p += 2;
  1256. base = 2;
  1257. len -= 2;
  1258. }
  1259. break;
  1260. case 't':
  1261. case 'T':
  1262. case 'd':
  1263. case 'D':
  1264. if (len >= 3)
  1265. {
  1266. p += 2;
  1267. base = 10;
  1268. len -= 2;
  1269. }
  1270. break;
  1271. default:
  1272. base = 8;
  1273. break;
  1274. }
  1275. while (len-- > 0)
  1276. {
  1277. c = *p++;
  1278. if (c >= 'A' && c <= 'Z')
  1279. c += 'a' - 'A';
  1280. if (c != 'l' && c != 'u')
  1281. n *= base;
  1282. if (c >= '0' && c <= '9')
  1283. {
  1284. if (found_suffix)
  1285. return ERROR;
  1286. n += i = c - '0';
  1287. }
  1288. else
  1289. {
  1290. if (base > 10 && c >= 'a' && c <= 'f')
  1291. {
  1292. if (found_suffix)
  1293. return ERROR;
  1294. n += i = c - 'a' + 10;
  1295. }
  1296. else if (c == 'l')
  1297. {
  1298. ++long_p;
  1299. found_suffix = 1;
  1300. }
  1301. else if (c == 'u')
  1302. {
  1303. unsigned_p = 1;
  1304. found_suffix = 1;
  1305. }
  1306. else
  1307. return ERROR; /* Char not a digit */
  1308. }
  1309. if (i >= base)
  1310. return ERROR; /* Invalid digit in this base */
  1311. /* Portably test for overflow (only works for nonzero values, so make
  1312. a second check for zero). FIXME: Can't we just make n and prevn
  1313. unsigned and avoid this? */
  1314. if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  1315. unsigned_p = 1; /* Try something unsigned */
  1316. /* Portably test for unsigned overflow.
  1317. FIXME: This check is wrong; for example it doesn't find overflow
  1318. on 0x123456789 when LONGEST is 32 bits. */
  1319. if (c != 'l' && c != 'u' && n != 0)
  1320. {
  1321. if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  1322. error ("Numeric constant too large.");
  1323. }
  1324. prevn = n;
  1325. }
  1326. /* An integer constant is an int, a long, or a long long. An L
  1327. suffix forces it to be long; an LL suffix forces it to be long
  1328. long. If not forced to a larger size, it gets the first type of
  1329. the above that it fits in. To figure out whether it fits, we
  1330. shift it right and see whether anything remains. Note that we
  1331. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  1332. operation, because many compilers will warn about such a shift
  1333. (which always produces a zero result). Sometimes gdbarch_int_bit
  1334. or gdbarch_long_bit will be that big, sometimes not. To deal with
  1335. the case where it is we just always shift the value more than
  1336. once, with fewer bits each time. */
  1337. un = (ULONGEST)n >> 2;
  1338. if (long_p == 0
  1339. && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
  1340. {
  1341. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
  1342. /* A large decimal (not hex or octal) constant (between INT_MAX
  1343. and UINT_MAX) is a long or unsigned long, according to ANSI,
  1344. never an unsigned int, but this code treats it as unsigned
  1345. int. This probably should be fixed. GCC gives a warning on
  1346. such constants. */
  1347. unsigned_type = parse_type->builtin_unsigned_int;
  1348. signed_type = parse_type->builtin_int;
  1349. }
  1350. else if (long_p <= 1
  1351. && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
  1352. {
  1353. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
  1354. unsigned_type = parse_type->builtin_unsigned_long;
  1355. signed_type = parse_type->builtin_long;
  1356. }
  1357. else
  1358. {
  1359. int shift;
  1360. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  1361. < gdbarch_long_long_bit (parse_gdbarch))
  1362. /* A long long does not fit in a LONGEST. */
  1363. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  1364. else
  1365. shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
  1366. high_bit = (ULONGEST) 1 << shift;
  1367. unsigned_type = parse_type->builtin_unsigned_long_long;
  1368. signed_type = parse_type->builtin_long_long;
  1369. }
  1370. putithere->typed_val_int.val = n;
  1371. /* If the high bit of the worked out type is set then this number
  1372. has to be unsigned. */
  1373. if (unsigned_p || (n & high_bit))
  1374. {
  1375. putithere->typed_val_int.type = unsigned_type;
  1376. }
  1377. else
  1378. {
  1379. putithere->typed_val_int.type = signed_type;
  1380. }
  1381. return INT;
  1382. }
  1383. /* Temporary obstack used for holding strings. */
  1384. static struct obstack tempbuf;
  1385. static int tempbuf_init;
  1386. /* Parse a C escape sequence. The initial backslash of the sequence
  1387. is at (*PTR)[-1]. *PTR will be updated to point to just after the
  1388. last character of the sequence. If OUTPUT is not NULL, the
  1389. translated form of the escape sequence will be written there. If
  1390. OUTPUT is NULL, no output is written and the call will only affect
  1391. *PTR. If an escape sequence is expressed in target bytes, then the
  1392. entire sequence will simply be copied to OUTPUT. Return 1 if any
  1393. character was emitted, 0 otherwise. */
  1394. int
  1395. c_parse_escape (char **ptr, struct obstack *output)
  1396. {
  1397. char *tokptr = *ptr;
  1398. int result = 1;
  1399. /* Some escape sequences undergo character set conversion. Those we
  1400. translate here. */
  1401. switch (*tokptr)
  1402. {
  1403. /* Hex escapes do not undergo character set conversion, so keep
  1404. the escape sequence for later. */
  1405. case 'x':
  1406. if (output)
  1407. obstack_grow_str (output, "\\x");
  1408. ++tokptr;
  1409. if (!isxdigit (*tokptr))
  1410. error (_("\\x escape without a following hex digit"));
  1411. while (isxdigit (*tokptr))
  1412. {
  1413. if (output)
  1414. obstack_1grow (output, *tokptr);
  1415. ++tokptr;
  1416. }
  1417. break;
  1418. /* Octal escapes do not undergo character set conversion, so
  1419. keep the escape sequence for later. */
  1420. case '0':
  1421. case '1':
  1422. case '2':
  1423. case '3':
  1424. case '4':
  1425. case '5':
  1426. case '6':
  1427. case '7':
  1428. {
  1429. int i;
  1430. if (output)
  1431. obstack_grow_str (output, "\\");
  1432. for (i = 0;
  1433. i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
  1434. ++i)
  1435. {
  1436. if (output)
  1437. obstack_1grow (output, *tokptr);
  1438. ++tokptr;
  1439. }
  1440. }
  1441. break;
  1442. /* We handle UCNs later. We could handle them here, but that
  1443. would mean a spurious error in the case where the UCN could
  1444. be converted to the target charset but not the host
  1445. charset. */
  1446. case 'u':
  1447. case 'U':
  1448. {
  1449. char c = *tokptr;
  1450. int i, len = c == 'U' ? 8 : 4;
  1451. if (output)
  1452. {
  1453. obstack_1grow (output, '\\');
  1454. obstack_1grow (output, *tokptr);
  1455. }
  1456. ++tokptr;
  1457. if (!isxdigit (*tokptr))
  1458. error (_("\\%c escape without a following hex digit"), c);
  1459. for (i = 0; i < len && isxdigit (*tokptr); ++i)
  1460. {
  1461. if (output)
  1462. obstack_1grow (output, *tokptr);
  1463. ++tokptr;
  1464. }
  1465. }
  1466. break;
  1467. /* We must pass backslash through so that it does not
  1468. cause quoting during the second expansion. */
  1469. case '\\':
  1470. if (output)
  1471. obstack_grow_str (output, "\\\\");
  1472. ++tokptr;
  1473. break;
  1474. /* Escapes which undergo conversion. */
  1475. case 'a':
  1476. if (output)
  1477. obstack_1grow (output, '\a');
  1478. ++tokptr;
  1479. break;
  1480. case 'b':
  1481. if (output)
  1482. obstack_1grow (output, '\b');
  1483. ++tokptr;
  1484. break;
  1485. case 'f':
  1486. if (output)
  1487. obstack_1grow (output, '\f');
  1488. ++tokptr;
  1489. break;
  1490. case 'n':
  1491. if (output)
  1492. obstack_1grow (output, '\n');
  1493. ++tokptr;
  1494. break;
  1495. case 'r':
  1496. if (output)
  1497. obstack_1grow (output, '\r');
  1498. ++tokptr;
  1499. break;
  1500. case 't':
  1501. if (output)
  1502. obstack_1grow (output, '\t');
  1503. ++tokptr;
  1504. break;
  1505. case 'v':
  1506. if (output)
  1507. obstack_1grow (output, '\v');
  1508. ++tokptr;
  1509. break;
  1510. /* GCC extension. */
  1511. case 'e':
  1512. if (output)
  1513. obstack_1grow (output, HOST_ESCAPE_CHAR);
  1514. ++tokptr;
  1515. break;
  1516. /* Backslash-newline expands to nothing at all. */
  1517. case '\n':
  1518. ++tokptr;
  1519. result = 0;
  1520. break;
  1521. /* A few escapes just expand to the character itself. */
  1522. case '\'':
  1523. case '\"':
  1524. case '?':
  1525. /* GCC extensions. */
  1526. case '(':
  1527. case '{':
  1528. case '[':
  1529. case '%':
  1530. /* Unrecognized escapes turn into the character itself. */
  1531. default:
  1532. if (output)
  1533. obstack_1grow (output, *tokptr);
  1534. ++tokptr;
  1535. break;
  1536. }
  1537. *ptr = tokptr;
  1538. return result;
  1539. }
  1540. /* Parse a string or character literal from TOKPTR. The string or
  1541. character may be wide or unicode. *OUTPTR is set to just after the
  1542. end of the literal in the input string. The resulting token is
  1543. stored in VALUE. This returns a token value, either STRING or
  1544. CHAR, depending on what was parsed. *HOST_CHARS is set to the
  1545. number of host characters in the literal. */
  1546. static int
  1547. parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
  1548. int *host_chars)
  1549. {
  1550. int quote, i;
  1551. enum c_string_type type;
  1552. /* Build the gdb internal form of the input string in tempbuf. Note
  1553. that the buffer is null byte terminated *only* for the
  1554. convenience of debugging gdb itself and printing the buffer
  1555. contents when the buffer contains no embedded nulls. Gdb does
  1556. not depend upon the buffer being null byte terminated, it uses
  1557. the length string instead. This allows gdb to handle C strings
  1558. (as well as strings in other languages) with embedded null
  1559. bytes */
  1560. if (!tempbuf_init)
  1561. tempbuf_init = 1;
  1562. else
  1563. obstack_free (&tempbuf, NULL);
  1564. obstack_init (&tempbuf);
  1565. /* Record the string type. */
  1566. if (*tokptr == 'L')
  1567. {
  1568. type = C_WIDE_STRING;
  1569. ++tokptr;
  1570. }
  1571. else if (*tokptr == 'u')
  1572. {
  1573. type = C_STRING_16;
  1574. ++tokptr;
  1575. }
  1576. else if (*tokptr == 'U')
  1577. {
  1578. type = C_STRING_32;
  1579. ++tokptr;
  1580. }
  1581. else
  1582. type = C_STRING;
  1583. /* Skip the quote. */
  1584. quote = *tokptr;
  1585. if (quote == '\'')
  1586. type |= C_CHAR;
  1587. ++tokptr;
  1588. *host_chars = 0;
  1589. while (*tokptr)
  1590. {
  1591. char c = *tokptr;
  1592. if (c == '\\')
  1593. {
  1594. ++tokptr;
  1595. *host_chars += c_parse_escape (&tokptr, &tempbuf);
  1596. }
  1597. else if (c == quote)
  1598. break;
  1599. else
  1600. {
  1601. obstack_1grow (&tempbuf, c);
  1602. ++tokptr;
  1603. /* FIXME: this does the wrong thing with multi-byte host
  1604. characters. We could use mbrlen here, but that would
  1605. make "set host-charset" a bit less useful. */
  1606. ++*host_chars;
  1607. }
  1608. }
  1609. if (*tokptr != quote)
  1610. {
  1611. if (quote == '"')
  1612. error ("Unterminated string in expression.");
  1613. else
  1614. error ("Unmatched single quote.");
  1615. }
  1616. ++tokptr;
  1617. value->type = type;
  1618. value->ptr = obstack_base (&tempbuf);
  1619. value->length = obstack_object_size (&tempbuf);
  1620. *outptr = tokptr;
  1621. return quote == '"' ? STRING : CHAR;
  1622. }
  1623. struct token
  1624. {
  1625. char *operator;
  1626. int token;
  1627. enum exp_opcode opcode;
  1628. int cxx_only;
  1629. };
  1630. static const struct token tokentab3[] =
  1631. {
  1632. {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
  1633. {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
  1634. {"->*", ARROW_STAR, BINOP_END, 1}
  1635. };
  1636. static const struct token tokentab2[] =
  1637. {
  1638. {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
  1639. {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
  1640. {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
  1641. {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
  1642. {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
  1643. {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
  1644. {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
  1645. {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
  1646. {"++", INCREMENT, BINOP_END, 0},
  1647. {"--", DECREMENT, BINOP_END, 0},
  1648. {"->", ARROW, BINOP_END, 0},
  1649. {"&&", ANDAND, BINOP_END, 0},
  1650. {"||", OROR, BINOP_END, 0},
  1651. /* "::" is *not* only C++: gdb overrides its meaning in several
  1652. different ways, e.g., 'filename'::func, function::variable. */
  1653. {"::", COLONCOLON, BINOP_END, 0},
  1654. {"<<", LSH, BINOP_END, 0},
  1655. {">>", RSH, BINOP_END, 0},
  1656. {"==", EQUAL, BINOP_END, 0},
  1657. {"!=", NOTEQUAL, BINOP_END, 0},
  1658. {"<=", LEQ, BINOP_END, 0},
  1659. {">=", GEQ, BINOP_END, 0},
  1660. {".*", DOT_STAR, BINOP_END, 1}
  1661. };
  1662. /* Identifier-like to…

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