PageRenderTime 215ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/gdb/c-exp.y

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