PageRenderTime 75ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/gdb-7.4.50.20120714/gdb/c-exp.y

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

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