PageRenderTime 1895ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Happy | 1682 lines | 1491 code | 191 blank | 0 comment | 0 complexity | a03aa5559fe82dcdd963c406c09345df MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1
  1. /* YACC parser for Pascal expressions, for GDB.
  2. Copyright (C) 2000, 2006, 2007, 2008, 2009, 2010
  3. Free Software 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. /* This file is derived from c-exp.y */
  16. /* Parse a Pascal expression from text in a string,
  17. and return the result as a struct expression pointer.
  18. That structure contains arithmetic operations in reverse polish,
  19. with constants represented by operations that are followed by special data.
  20. See expression.h for the details of the format.
  21. What is important here is that it can be built up sequentially
  22. during the process of parsing; the lower levels of the tree always
  23. come first in the result.
  24. Note that malloc's and realloc's in this file are transformed to
  25. xmalloc and xrealloc respectively by the same sed command in the
  26. makefile that remaps any other malloc/realloc inserted by the parser
  27. generator. Doing this with #defines and trying to control the interaction
  28. with include files (<malloc.h> and <stdlib.h> for example) just became
  29. too messy, particularly when such includes can be inserted at random
  30. times by the parser generator. */
  31. /* Known bugs or limitations:
  32. - pascal string operations are not supported at all.
  33. - there are some problems with boolean types.
  34. - Pascal type hexadecimal constants are not supported
  35. because they conflict with the internal variables format.
  36. Probably also lots of other problems, less well defined PM */
  37. %{
  38. #include "defs.h"
  39. #include "gdb_string.h"
  40. #include <ctype.h>
  41. #include "expression.h"
  42. #include "value.h"
  43. #include "parser-defs.h"
  44. #include "language.h"
  45. #include "p-lang.h"
  46. #include "bfd.h" /* Required by objfiles.h. */
  47. #include "symfile.h" /* Required by objfiles.h. */
  48. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  49. #include "block.h"
  50. #define parse_type builtin_type (parse_gdbarch)
  51. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  52. as well as gratuitiously global symbol names, so we can have multiple
  53. yacc generated parsers in gdb. Note that these are only the variables
  54. produced by yacc. If other parser generators (bison, byacc, etc) produce
  55. additional global names that conflict at link time, then those parser
  56. generators need to be fixed instead of adding those names to this list. */
  57. #define yymaxdepth pascal_maxdepth
  58. #define yyparse pascal_parse
  59. #define yylex pascal_lex
  60. #define yyerror pascal_error
  61. #define yylval pascal_lval
  62. #define yychar pascal_char
  63. #define yydebug pascal_debug
  64. #define yypact pascal_pact
  65. #define yyr1 pascal_r1
  66. #define yyr2 pascal_r2
  67. #define yydef pascal_def
  68. #define yychk pascal_chk
  69. #define yypgo pascal_pgo
  70. #define yyact pascal_act
  71. #define yyexca pascal_exca
  72. #define yyerrflag pascal_errflag
  73. #define yynerrs pascal_nerrs
  74. #define yyps pascal_ps
  75. #define yypv pascal_pv
  76. #define yys pascal_s
  77. #define yy_yys pascal_yys
  78. #define yystate pascal_state
  79. #define yytmp pascal_tmp
  80. #define yyv pascal_v
  81. #define yy_yyv pascal_yyv
  82. #define yyval pascal_val
  83. #define yylloc pascal_lloc
  84. #define yyreds pascal_reds /* With YYDEBUG defined */
  85. #define yytoks pascal_toks /* With YYDEBUG defined */
  86. #define yyname pascal_name /* With YYDEBUG defined */
  87. #define yyrule pascal_rule /* With YYDEBUG defined */
  88. #define yylhs pascal_yylhs
  89. #define yylen pascal_yylen
  90. #define yydefred pascal_yydefred
  91. #define yydgoto pascal_yydgoto
  92. #define yysindex pascal_yysindex
  93. #define yyrindex pascal_yyrindex
  94. #define yygindex pascal_yygindex
  95. #define yytable pascal_yytable
  96. #define yycheck pascal_yycheck
  97. #ifndef YYDEBUG
  98. #define YYDEBUG 1 /* Default to yydebug support */
  99. #endif
  100. #define YYFPRINTF parser_fprintf
  101. int yyparse (void);
  102. static int yylex (void);
  103. void
  104. yyerror (char *);
  105. static char * uptok (char *, int);
  106. %}
  107. /* Although the yacc "value" of an expression is not used,
  108. since the result is stored in the structure being created,
  109. other node types do have values. */
  110. %union
  111. {
  112. LONGEST lval;
  113. struct {
  114. LONGEST val;
  115. struct type *type;
  116. } typed_val_int;
  117. struct {
  118. DOUBLEST dval;
  119. struct type *type;
  120. } typed_val_float;
  121. struct symbol *sym;
  122. struct type *tval;
  123. struct stoken sval;
  124. struct ttype tsym;
  125. struct symtoken ssym;
  126. int voidval;
  127. struct block *bval;
  128. enum exp_opcode opcode;
  129. struct internalvar *ivar;
  130. struct type **tvec;
  131. int *ivec;
  132. }
  133. %{
  134. /* YYSTYPE gets defined by %union */
  135. static int
  136. parse_number (char *, int, int, YYSTYPE *);
  137. static struct type *current_type;
  138. static int leftdiv_is_integer;
  139. static void push_current_type (void);
  140. static void pop_current_type (void);
  141. static int search_field;
  142. %}
  143. %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
  144. %type <tval> type typebase
  145. /* %type <bval> block */
  146. /* Fancy type parsing. */
  147. %type <tval> ptype
  148. %token <typed_val_int> INT
  149. %token <typed_val_float> FLOAT
  150. /* Both NAME and TYPENAME tokens represent symbols in the input,
  151. and both convey their data as strings.
  152. But a TYPENAME is a string that happens to be defined as a typedef
  153. or builtin type name (such as int or char)
  154. and a NAME is any other symbol.
  155. Contexts where this distinction is not important can use the
  156. nonterminal "name", which matches either NAME or TYPENAME. */
  157. %token <sval> STRING
  158. %token <sval> FIELDNAME
  159. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
  160. %token <tsym> TYPENAME
  161. %type <sval> name
  162. %type <ssym> name_not_typename
  163. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  164. but which would parse as a valid number in the current input radix.
  165. E.g. "c" when input_radix==16. Depending on the parse, it will be
  166. turned into a name or into a number. */
  167. %token <ssym> NAME_OR_INT
  168. %token STRUCT CLASS SIZEOF COLONCOLON
  169. %token ERROR
  170. /* Special type cases, put in to allow the parser to distinguish different
  171. legal basetypes. */
  172. %token <voidval> VARIABLE
  173. /* Object pascal */
  174. %token THIS
  175. %token <lval> TRUEKEYWORD FALSEKEYWORD
  176. %left ','
  177. %left ABOVE_COMMA
  178. %right ASSIGN
  179. %left NOT
  180. %left OR
  181. %left XOR
  182. %left ANDAND
  183. %left '=' NOTEQUAL
  184. %left '<' '>' LEQ GEQ
  185. %left LSH RSH DIV MOD
  186. %left '@'
  187. %left '+' '-'
  188. %left '*' '/'
  189. %right UNARY INCREMENT DECREMENT
  190. %right ARROW '.' '[' '('
  191. %left '^'
  192. %token <ssym> BLOCKNAME
  193. %type <bval> block
  194. %left COLONCOLON
  195. %%
  196. start : { current_type = NULL;
  197. search_field = 0;
  198. leftdiv_is_integer = 0;
  199. }
  200. normal_start {}
  201. ;
  202. normal_start :
  203. exp1
  204. | type_exp
  205. ;
  206. type_exp: type
  207. { write_exp_elt_opcode(OP_TYPE);
  208. write_exp_elt_type($1);
  209. write_exp_elt_opcode(OP_TYPE);
  210. current_type = $1; } ;
  211. /* Expressions, including the comma operator. */
  212. exp1 : exp
  213. | exp1 ',' exp
  214. { write_exp_elt_opcode (BINOP_COMMA); }
  215. ;
  216. /* Expressions, not including the comma operator. */
  217. exp : exp '^' %prec UNARY
  218. { write_exp_elt_opcode (UNOP_IND);
  219. if (current_type)
  220. current_type = TYPE_TARGET_TYPE (current_type); }
  221. ;
  222. exp : '@' exp %prec UNARY
  223. { write_exp_elt_opcode (UNOP_ADDR);
  224. if (current_type)
  225. current_type = TYPE_POINTER_TYPE (current_type); }
  226. ;
  227. exp : '-' exp %prec UNARY
  228. { write_exp_elt_opcode (UNOP_NEG); }
  229. ;
  230. exp : NOT exp %prec UNARY
  231. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  232. ;
  233. exp : INCREMENT '(' exp ')' %prec UNARY
  234. { write_exp_elt_opcode (UNOP_PREINCREMENT); }
  235. ;
  236. exp : DECREMENT '(' exp ')' %prec UNARY
  237. { write_exp_elt_opcode (UNOP_PREDECREMENT); }
  238. ;
  239. exp : exp '.' { search_field = 1; }
  240. FIELDNAME
  241. /* name */
  242. { write_exp_elt_opcode (STRUCTOP_STRUCT);
  243. write_exp_string ($4);
  244. write_exp_elt_opcode (STRUCTOP_STRUCT);
  245. search_field = 0;
  246. if (current_type)
  247. { while (TYPE_CODE (current_type) == TYPE_CODE_PTR)
  248. current_type = TYPE_TARGET_TYPE (current_type);
  249. current_type = lookup_struct_elt_type (
  250. current_type, $4.ptr, 0); };
  251. } ;
  252. exp : exp '['
  253. /* We need to save the current_type value */
  254. { char *arrayname;
  255. int arrayfieldindex;
  256. arrayfieldindex = is_pascal_string_type (
  257. current_type, NULL, NULL,
  258. NULL, NULL, &arrayname);
  259. if (arrayfieldindex)
  260. {
  261. struct stoken stringsval;
  262. stringsval.ptr = alloca (strlen (arrayname) + 1);
  263. stringsval.length = strlen (arrayname);
  264. strcpy (stringsval.ptr, arrayname);
  265. current_type = TYPE_FIELD_TYPE (current_type,
  266. arrayfieldindex - 1);
  267. write_exp_elt_opcode (STRUCTOP_STRUCT);
  268. write_exp_string (stringsval);
  269. write_exp_elt_opcode (STRUCTOP_STRUCT);
  270. }
  271. push_current_type (); }
  272. exp1 ']'
  273. { pop_current_type ();
  274. write_exp_elt_opcode (BINOP_SUBSCRIPT);
  275. if (current_type)
  276. current_type = TYPE_TARGET_TYPE (current_type); }
  277. ;
  278. exp : exp '('
  279. /* This is to save the value of arglist_len
  280. being accumulated by an outer function call. */
  281. { push_current_type ();
  282. start_arglist (); }
  283. arglist ')' %prec ARROW
  284. { write_exp_elt_opcode (OP_FUNCALL);
  285. write_exp_elt_longcst ((LONGEST) end_arglist ());
  286. write_exp_elt_opcode (OP_FUNCALL);
  287. pop_current_type ();
  288. if (current_type)
  289. current_type = TYPE_TARGET_TYPE (current_type);
  290. }
  291. ;
  292. arglist :
  293. | exp
  294. { arglist_len = 1; }
  295. | arglist ',' exp %prec ABOVE_COMMA
  296. { arglist_len++; }
  297. ;
  298. exp : type '(' exp ')' %prec UNARY
  299. { if (current_type)
  300. {
  301. /* Allow automatic dereference of classes. */
  302. if ((TYPE_CODE (current_type) == TYPE_CODE_PTR)
  303. && (TYPE_CODE (TYPE_TARGET_TYPE (current_type)) == TYPE_CODE_CLASS)
  304. && (TYPE_CODE ($1) == TYPE_CODE_CLASS))
  305. write_exp_elt_opcode (UNOP_IND);
  306. }
  307. write_exp_elt_opcode (UNOP_CAST);
  308. write_exp_elt_type ($1);
  309. write_exp_elt_opcode (UNOP_CAST);
  310. current_type = $1; }
  311. ;
  312. exp : '(' exp1 ')'
  313. { }
  314. ;
  315. /* Binary operators in order of decreasing precedence. */
  316. exp : exp '*' exp
  317. { write_exp_elt_opcode (BINOP_MUL); }
  318. ;
  319. exp : exp '/' {
  320. if (current_type && is_integral_type (current_type))
  321. leftdiv_is_integer = 1;
  322. }
  323. exp
  324. {
  325. if (leftdiv_is_integer && current_type
  326. && is_integral_type (current_type))
  327. {
  328. write_exp_elt_opcode (UNOP_CAST);
  329. write_exp_elt_type (parse_type->builtin_long_double);
  330. current_type = parse_type->builtin_long_double;
  331. write_exp_elt_opcode (UNOP_CAST);
  332. leftdiv_is_integer = 0;
  333. }
  334. write_exp_elt_opcode (BINOP_DIV);
  335. }
  336. ;
  337. exp : exp DIV exp
  338. { write_exp_elt_opcode (BINOP_INTDIV); }
  339. ;
  340. exp : exp MOD exp
  341. { write_exp_elt_opcode (BINOP_REM); }
  342. ;
  343. exp : exp '+' exp
  344. { write_exp_elt_opcode (BINOP_ADD); }
  345. ;
  346. exp : exp '-' exp
  347. { write_exp_elt_opcode (BINOP_SUB); }
  348. ;
  349. exp : exp LSH exp
  350. { write_exp_elt_opcode (BINOP_LSH); }
  351. ;
  352. exp : exp RSH exp
  353. { write_exp_elt_opcode (BINOP_RSH); }
  354. ;
  355. exp : exp '=' exp
  356. { write_exp_elt_opcode (BINOP_EQUAL);
  357. current_type = parse_type->builtin_bool;
  358. }
  359. ;
  360. exp : exp NOTEQUAL exp
  361. { write_exp_elt_opcode (BINOP_NOTEQUAL);
  362. current_type = parse_type->builtin_bool;
  363. }
  364. ;
  365. exp : exp LEQ exp
  366. { write_exp_elt_opcode (BINOP_LEQ);
  367. current_type = parse_type->builtin_bool;
  368. }
  369. ;
  370. exp : exp GEQ exp
  371. { write_exp_elt_opcode (BINOP_GEQ);
  372. current_type = parse_type->builtin_bool;
  373. }
  374. ;
  375. exp : exp '<' exp
  376. { write_exp_elt_opcode (BINOP_LESS);
  377. current_type = parse_type->builtin_bool;
  378. }
  379. ;
  380. exp : exp '>' exp
  381. { write_exp_elt_opcode (BINOP_GTR);
  382. current_type = parse_type->builtin_bool;
  383. }
  384. ;
  385. exp : exp ANDAND exp
  386. { write_exp_elt_opcode (BINOP_BITWISE_AND); }
  387. ;
  388. exp : exp XOR exp
  389. { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
  390. ;
  391. exp : exp OR exp
  392. { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
  393. ;
  394. exp : exp ASSIGN exp
  395. { write_exp_elt_opcode (BINOP_ASSIGN); }
  396. ;
  397. exp : TRUEKEYWORD
  398. { write_exp_elt_opcode (OP_BOOL);
  399. write_exp_elt_longcst ((LONGEST) $1);
  400. current_type = parse_type->builtin_bool;
  401. write_exp_elt_opcode (OP_BOOL); }
  402. ;
  403. exp : FALSEKEYWORD
  404. { write_exp_elt_opcode (OP_BOOL);
  405. write_exp_elt_longcst ((LONGEST) $1);
  406. current_type = parse_type->builtin_bool;
  407. write_exp_elt_opcode (OP_BOOL); }
  408. ;
  409. exp : INT
  410. { write_exp_elt_opcode (OP_LONG);
  411. write_exp_elt_type ($1.type);
  412. current_type = $1.type;
  413. write_exp_elt_longcst ((LONGEST)($1.val));
  414. write_exp_elt_opcode (OP_LONG); }
  415. ;
  416. exp : NAME_OR_INT
  417. { YYSTYPE val;
  418. parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
  419. write_exp_elt_opcode (OP_LONG);
  420. write_exp_elt_type (val.typed_val_int.type);
  421. current_type = val.typed_val_int.type;
  422. write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
  423. write_exp_elt_opcode (OP_LONG);
  424. }
  425. ;
  426. exp : FLOAT
  427. { write_exp_elt_opcode (OP_DOUBLE);
  428. write_exp_elt_type ($1.type);
  429. current_type = $1.type;
  430. write_exp_elt_dblcst ($1.dval);
  431. write_exp_elt_opcode (OP_DOUBLE); }
  432. ;
  433. exp : variable
  434. ;
  435. exp : VARIABLE
  436. /* Already written by write_dollar_variable. */
  437. ;
  438. exp : SIZEOF '(' type ')' %prec UNARY
  439. { write_exp_elt_opcode (OP_LONG);
  440. write_exp_elt_type (parse_type->builtin_int);
  441. CHECK_TYPEDEF ($3);
  442. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  443. write_exp_elt_opcode (OP_LONG); }
  444. ;
  445. exp : STRING
  446. { /* C strings are converted into array constants with
  447. an explicit null byte added at the end. Thus
  448. the array upper bound is the string length.
  449. There is no such thing in C as a completely empty
  450. string. */
  451. char *sp = $1.ptr; int count = $1.length;
  452. while (count-- > 0)
  453. {
  454. write_exp_elt_opcode (OP_LONG);
  455. write_exp_elt_type (parse_type->builtin_char);
  456. write_exp_elt_longcst ((LONGEST)(*sp++));
  457. write_exp_elt_opcode (OP_LONG);
  458. }
  459. write_exp_elt_opcode (OP_LONG);
  460. write_exp_elt_type (parse_type->builtin_char);
  461. write_exp_elt_longcst ((LONGEST)'\0');
  462. write_exp_elt_opcode (OP_LONG);
  463. write_exp_elt_opcode (OP_ARRAY);
  464. write_exp_elt_longcst ((LONGEST) 0);
  465. write_exp_elt_longcst ((LONGEST) ($1.length));
  466. write_exp_elt_opcode (OP_ARRAY); }
  467. ;
  468. /* Object pascal */
  469. exp : THIS
  470. {
  471. struct value * this_val;
  472. struct type * this_type;
  473. write_exp_elt_opcode (OP_THIS);
  474. write_exp_elt_opcode (OP_THIS);
  475. /* we need type of this */
  476. this_val = value_of_this (0);
  477. if (this_val)
  478. this_type = value_type (this_val);
  479. else
  480. this_type = NULL;
  481. if (this_type)
  482. {
  483. if (TYPE_CODE (this_type) == TYPE_CODE_PTR)
  484. {
  485. this_type = TYPE_TARGET_TYPE (this_type);
  486. write_exp_elt_opcode (UNOP_IND);
  487. }
  488. }
  489. current_type = this_type;
  490. }
  491. ;
  492. /* end of object pascal. */
  493. block : BLOCKNAME
  494. {
  495. if ($1.sym != 0)
  496. $$ = SYMBOL_BLOCK_VALUE ($1.sym);
  497. else
  498. {
  499. struct symtab *tem =
  500. lookup_symtab (copy_name ($1.stoken));
  501. if (tem)
  502. $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
  503. else
  504. error ("No file or function \"%s\".",
  505. copy_name ($1.stoken));
  506. }
  507. }
  508. ;
  509. block : block COLONCOLON name
  510. { struct symbol *tem
  511. = lookup_symbol (copy_name ($3), $1,
  512. VAR_DOMAIN, (int *) NULL);
  513. if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  514. error ("No function \"%s\" in specified context.",
  515. copy_name ($3));
  516. $$ = SYMBOL_BLOCK_VALUE (tem); }
  517. ;
  518. variable: block COLONCOLON name
  519. { struct symbol *sym;
  520. sym = lookup_symbol (copy_name ($3), $1,
  521. VAR_DOMAIN, (int *) NULL);
  522. if (sym == 0)
  523. error ("No symbol \"%s\" in specified context.",
  524. copy_name ($3));
  525. write_exp_elt_opcode (OP_VAR_VALUE);
  526. /* block_found is set by lookup_symbol. */
  527. write_exp_elt_block (block_found);
  528. write_exp_elt_sym (sym);
  529. write_exp_elt_opcode (OP_VAR_VALUE); }
  530. ;
  531. qualified_name: typebase COLONCOLON name
  532. {
  533. struct type *type = $1;
  534. if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  535. && TYPE_CODE (type) != TYPE_CODE_UNION)
  536. error ("`%s' is not defined as an aggregate type.",
  537. TYPE_NAME (type));
  538. write_exp_elt_opcode (OP_SCOPE);
  539. write_exp_elt_type (type);
  540. write_exp_string ($3);
  541. write_exp_elt_opcode (OP_SCOPE);
  542. }
  543. ;
  544. variable: qualified_name
  545. | COLONCOLON name
  546. {
  547. char *name = copy_name ($2);
  548. struct symbol *sym;
  549. struct minimal_symbol *msymbol;
  550. sym =
  551. lookup_symbol (name, (const struct block *) NULL,
  552. VAR_DOMAIN, (int *) NULL);
  553. if (sym)
  554. {
  555. write_exp_elt_opcode (OP_VAR_VALUE);
  556. write_exp_elt_block (NULL);
  557. write_exp_elt_sym (sym);
  558. write_exp_elt_opcode (OP_VAR_VALUE);
  559. break;
  560. }
  561. msymbol = lookup_minimal_symbol (name, NULL, NULL);
  562. if (msymbol != NULL)
  563. write_exp_msymbol (msymbol);
  564. else if (!have_full_symbols () && !have_partial_symbols ())
  565. error ("No symbol table is loaded. Use the \"file\" command.");
  566. else
  567. error ("No symbol \"%s\" in current context.", name);
  568. }
  569. ;
  570. variable: name_not_typename
  571. { struct symbol *sym = $1.sym;
  572. if (sym)
  573. {
  574. if (symbol_read_needs_frame (sym))
  575. {
  576. if (innermost_block == 0
  577. || contained_in (block_found,
  578. innermost_block))
  579. innermost_block = block_found;
  580. }
  581. write_exp_elt_opcode (OP_VAR_VALUE);
  582. /* We want to use the selected frame, not
  583. another more inner frame which happens to
  584. be in the same block. */
  585. write_exp_elt_block (NULL);
  586. write_exp_elt_sym (sym);
  587. write_exp_elt_opcode (OP_VAR_VALUE);
  588. current_type = sym->type; }
  589. else if ($1.is_a_field_of_this)
  590. {
  591. struct value * this_val;
  592. struct type * this_type;
  593. /* Object pascal: it hangs off of `this'. Must
  594. not inadvertently convert from a method call
  595. to data ref. */
  596. if (innermost_block == 0
  597. || contained_in (block_found,
  598. innermost_block))
  599. innermost_block = block_found;
  600. write_exp_elt_opcode (OP_THIS);
  601. write_exp_elt_opcode (OP_THIS);
  602. write_exp_elt_opcode (STRUCTOP_PTR);
  603. write_exp_string ($1.stoken);
  604. write_exp_elt_opcode (STRUCTOP_PTR);
  605. /* we need type of this */
  606. this_val = value_of_this (0);
  607. if (this_val)
  608. this_type = value_type (this_val);
  609. else
  610. this_type = NULL;
  611. if (this_type)
  612. current_type = lookup_struct_elt_type (
  613. this_type,
  614. copy_name ($1.stoken), 0);
  615. else
  616. current_type = NULL;
  617. }
  618. else
  619. {
  620. struct minimal_symbol *msymbol;
  621. char *arg = copy_name ($1.stoken);
  622. msymbol =
  623. lookup_minimal_symbol (arg, NULL, NULL);
  624. if (msymbol != NULL)
  625. write_exp_msymbol (msymbol);
  626. else if (!have_full_symbols () && !have_partial_symbols ())
  627. error ("No symbol table is loaded. Use the \"file\" command.");
  628. else
  629. error ("No symbol \"%s\" in current context.",
  630. copy_name ($1.stoken));
  631. }
  632. }
  633. ;
  634. ptype : typebase
  635. ;
  636. /* We used to try to recognize more pointer to member types here, but
  637. that didn't work (shift/reduce conflicts meant that these rules never
  638. got executed). The problem is that
  639. int (foo::bar::baz::bizzle)
  640. is a function type but
  641. int (foo::bar::baz::bizzle::*)
  642. is a pointer to member type. Stroustrup loses again! */
  643. type : ptype
  644. ;
  645. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  646. : '^' typebase
  647. { $$ = lookup_pointer_type ($2); }
  648. | TYPENAME
  649. { $$ = $1.type; }
  650. | STRUCT name
  651. { $$ = lookup_struct (copy_name ($2),
  652. expression_context_block); }
  653. | CLASS name
  654. { $$ = lookup_struct (copy_name ($2),
  655. expression_context_block); }
  656. /* "const" and "volatile" are curently ignored. A type qualifier
  657. after the type is handled in the ptype rule. I think these could
  658. be too. */
  659. ;
  660. name : NAME { $$ = $1.stoken; }
  661. | BLOCKNAME { $$ = $1.stoken; }
  662. | TYPENAME { $$ = $1.stoken; }
  663. | NAME_OR_INT { $$ = $1.stoken; }
  664. ;
  665. name_not_typename : NAME
  666. | BLOCKNAME
  667. /* These would be useful if name_not_typename was useful, but it is just
  668. a fake for "variable", so these cause reduce/reduce conflicts because
  669. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  670. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  671. context where only a name could occur, this might be useful.
  672. | NAME_OR_INT
  673. */
  674. ;
  675. %%
  676. /* Take care of parsing a number (anything that starts with a digit).
  677. Set yylval and return the token type; update lexptr.
  678. LEN is the number of characters in it. */
  679. /*** Needs some error checking for the float case ***/
  680. static int
  681. parse_number (p, len, parsed_float, putithere)
  682. char *p;
  683. int len;
  684. int parsed_float;
  685. YYSTYPE *putithere;
  686. {
  687. /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
  688. here, and we do kind of silly things like cast to unsigned. */
  689. LONGEST n = 0;
  690. LONGEST prevn = 0;
  691. ULONGEST un;
  692. int i = 0;
  693. int c;
  694. int base = input_radix;
  695. int unsigned_p = 0;
  696. /* Number of "L" suffixes encountered. */
  697. int long_p = 0;
  698. /* We have found a "L" or "U" suffix. */
  699. int found_suffix = 0;
  700. ULONGEST high_bit;
  701. struct type *signed_type;
  702. struct type *unsigned_type;
  703. if (parsed_float)
  704. {
  705. /* It's a float since it contains a point or an exponent. */
  706. char c;
  707. int num = 0; /* number of tokens scanned by scanf */
  708. char saved_char = p[len];
  709. p[len] = 0; /* null-terminate the token */
  710. num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%c",
  711. &putithere->typed_val_float.dval, &c);
  712. p[len] = saved_char; /* restore the input stream */
  713. if (num != 1) /* check scanf found ONLY a float ... */
  714. return ERROR;
  715. /* See if it has `f' or `l' suffix (float or long double). */
  716. c = tolower (p[len - 1]);
  717. if (c == 'f')
  718. putithere->typed_val_float.type = parse_type->builtin_float;
  719. else if (c == 'l')
  720. putithere->typed_val_float.type = parse_type->builtin_long_double;
  721. else if (isdigit (c) || c == '.')
  722. putithere->typed_val_float.type = parse_type->builtin_double;
  723. else
  724. return ERROR;
  725. return FLOAT;
  726. }
  727. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  728. if (p[0] == '0')
  729. switch (p[1])
  730. {
  731. case 'x':
  732. case 'X':
  733. if (len >= 3)
  734. {
  735. p += 2;
  736. base = 16;
  737. len -= 2;
  738. }
  739. break;
  740. case 't':
  741. case 'T':
  742. case 'd':
  743. case 'D':
  744. if (len >= 3)
  745. {
  746. p += 2;
  747. base = 10;
  748. len -= 2;
  749. }
  750. break;
  751. default:
  752. base = 8;
  753. break;
  754. }
  755. while (len-- > 0)
  756. {
  757. c = *p++;
  758. if (c >= 'A' && c <= 'Z')
  759. c += 'a' - 'A';
  760. if (c != 'l' && c != 'u')
  761. n *= base;
  762. if (c >= '0' && c <= '9')
  763. {
  764. if (found_suffix)
  765. return ERROR;
  766. n += i = c - '0';
  767. }
  768. else
  769. {
  770. if (base > 10 && c >= 'a' && c <= 'f')
  771. {
  772. if (found_suffix)
  773. return ERROR;
  774. n += i = c - 'a' + 10;
  775. }
  776. else if (c == 'l')
  777. {
  778. ++long_p;
  779. found_suffix = 1;
  780. }
  781. else if (c == 'u')
  782. {
  783. unsigned_p = 1;
  784. found_suffix = 1;
  785. }
  786. else
  787. return ERROR; /* Char not a digit */
  788. }
  789. if (i >= base)
  790. return ERROR; /* Invalid digit in this base */
  791. /* Portably test for overflow (only works for nonzero values, so make
  792. a second check for zero). FIXME: Can't we just make n and prevn
  793. unsigned and avoid this? */
  794. if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  795. unsigned_p = 1; /* Try something unsigned */
  796. /* Portably test for unsigned overflow.
  797. FIXME: This check is wrong; for example it doesn't find overflow
  798. on 0x123456789 when LONGEST is 32 bits. */
  799. if (c != 'l' && c != 'u' && n != 0)
  800. {
  801. if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  802. error ("Numeric constant too large.");
  803. }
  804. prevn = n;
  805. }
  806. /* An integer constant is an int, a long, or a long long. An L
  807. suffix forces it to be long; an LL suffix forces it to be long
  808. long. If not forced to a larger size, it gets the first type of
  809. the above that it fits in. To figure out whether it fits, we
  810. shift it right and see whether anything remains. Note that we
  811. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  812. operation, because many compilers will warn about such a shift
  813. (which always produces a zero result). Sometimes gdbarch_int_bit
  814. or gdbarch_long_bit will be that big, sometimes not. To deal with
  815. the case where it is we just always shift the value more than
  816. once, with fewer bits each time. */
  817. un = (ULONGEST)n >> 2;
  818. if (long_p == 0
  819. && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
  820. {
  821. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
  822. /* A large decimal (not hex or octal) constant (between INT_MAX
  823. and UINT_MAX) is a long or unsigned long, according to ANSI,
  824. never an unsigned int, but this code treats it as unsigned
  825. int. This probably should be fixed. GCC gives a warning on
  826. such constants. */
  827. unsigned_type = parse_type->builtin_unsigned_int;
  828. signed_type = parse_type->builtin_int;
  829. }
  830. else if (long_p <= 1
  831. && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
  832. {
  833. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
  834. unsigned_type = parse_type->builtin_unsigned_long;
  835. signed_type = parse_type->builtin_long;
  836. }
  837. else
  838. {
  839. int shift;
  840. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  841. < gdbarch_long_long_bit (parse_gdbarch))
  842. /* A long long does not fit in a LONGEST. */
  843. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  844. else
  845. shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
  846. high_bit = (ULONGEST) 1 << shift;
  847. unsigned_type = parse_type->builtin_unsigned_long_long;
  848. signed_type = parse_type->builtin_long_long;
  849. }
  850. putithere->typed_val_int.val = n;
  851. /* If the high bit of the worked out type is set then this number
  852. has to be unsigned. */
  853. if (unsigned_p || (n & high_bit))
  854. {
  855. putithere->typed_val_int.type = unsigned_type;
  856. }
  857. else
  858. {
  859. putithere->typed_val_int.type = signed_type;
  860. }
  861. return INT;
  862. }
  863. struct type_push
  864. {
  865. struct type *stored;
  866. struct type_push *next;
  867. };
  868. static struct type_push *tp_top = NULL;
  869. static void
  870. push_current_type (void)
  871. {
  872. struct type_push *tpnew;
  873. tpnew = (struct type_push *) malloc (sizeof (struct type_push));
  874. tpnew->next = tp_top;
  875. tpnew->stored = current_type;
  876. current_type = NULL;
  877. tp_top = tpnew;
  878. }
  879. static void
  880. pop_current_type (void)
  881. {
  882. struct type_push *tp = tp_top;
  883. if (tp)
  884. {
  885. current_type = tp->stored;
  886. tp_top = tp->next;
  887. free (tp);
  888. }
  889. }
  890. struct token
  891. {
  892. char *operator;
  893. int token;
  894. enum exp_opcode opcode;
  895. };
  896. static const struct token tokentab3[] =
  897. {
  898. {"shr", RSH, BINOP_END},
  899. {"shl", LSH, BINOP_END},
  900. {"and", ANDAND, BINOP_END},
  901. {"div", DIV, BINOP_END},
  902. {"not", NOT, BINOP_END},
  903. {"mod", MOD, BINOP_END},
  904. {"inc", INCREMENT, BINOP_END},
  905. {"dec", DECREMENT, BINOP_END},
  906. {"xor", XOR, BINOP_END}
  907. };
  908. static const struct token tokentab2[] =
  909. {
  910. {"or", OR, BINOP_END},
  911. {"<>", NOTEQUAL, BINOP_END},
  912. {"<=", LEQ, BINOP_END},
  913. {">=", GEQ, BINOP_END},
  914. {":=", ASSIGN, BINOP_END},
  915. {"::", COLONCOLON, BINOP_END} };
  916. /* Allocate uppercased var */
  917. /* make an uppercased copy of tokstart */
  918. static char * uptok (tokstart, namelen)
  919. char *tokstart;
  920. int namelen;
  921. {
  922. int i;
  923. char *uptokstart = (char *)malloc(namelen+1);
  924. for (i = 0;i <= namelen;i++)
  925. {
  926. if ((tokstart[i]>='a' && tokstart[i]<='z'))
  927. uptokstart[i] = tokstart[i]-('a'-'A');
  928. else
  929. uptokstart[i] = tokstart[i];
  930. }
  931. uptokstart[namelen]='\0';
  932. return uptokstart;
  933. }
  934. /* Read one token, getting characters through lexptr. */
  935. static int
  936. yylex ()
  937. {
  938. int c;
  939. int namelen;
  940. unsigned int i;
  941. char *tokstart;
  942. char *uptokstart;
  943. char *tokptr;
  944. char *p;
  945. int explen, tempbufindex;
  946. static char *tempbuf;
  947. static int tempbufsize;
  948. retry:
  949. prev_lexptr = lexptr;
  950. tokstart = lexptr;
  951. explen = strlen (lexptr);
  952. /* See if it is a special token of length 3. */
  953. if (explen > 2)
  954. for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
  955. if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
  956. && (!isalpha (tokentab3[i].operator[0]) || explen == 3
  957. || (!isalpha (tokstart[3]) && !isdigit (tokstart[3]) && tokstart[3] != '_')))
  958. {
  959. lexptr += 3;
  960. yylval.opcode = tokentab3[i].opcode;
  961. return tokentab3[i].token;
  962. }
  963. /* See if it is a special token of length 2. */
  964. if (explen > 1)
  965. for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
  966. if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
  967. && (!isalpha (tokentab2[i].operator[0]) || explen == 2
  968. || (!isalpha (tokstart[2]) && !isdigit (tokstart[2]) && tokstart[2] != '_')))
  969. {
  970. lexptr += 2;
  971. yylval.opcode = tokentab2[i].opcode;
  972. return tokentab2[i].token;
  973. }
  974. switch (c = *tokstart)
  975. {
  976. case 0:
  977. return 0;
  978. case ' ':
  979. case '\t':
  980. case '\n':
  981. lexptr++;
  982. goto retry;
  983. case '\'':
  984. /* We either have a character constant ('0' or '\177' for example)
  985. or we have a quoted symbol reference ('foo(int,int)' in object pascal
  986. for example). */
  987. lexptr++;
  988. c = *lexptr++;
  989. if (c == '\\')
  990. c = parse_escape (&lexptr);
  991. else if (c == '\'')
  992. error ("Empty character constant.");
  993. yylval.typed_val_int.val = c;
  994. yylval.typed_val_int.type = parse_type->builtin_char;
  995. c = *lexptr++;
  996. if (c != '\'')
  997. {
  998. namelen = skip_quoted (tokstart) - tokstart;
  999. if (namelen > 2)
  1000. {
  1001. lexptr = tokstart + namelen;
  1002. if (lexptr[-1] != '\'')
  1003. error ("Unmatched single quote.");
  1004. namelen -= 2;
  1005. tokstart++;
  1006. uptokstart = uptok(tokstart,namelen);
  1007. goto tryname;
  1008. }
  1009. error ("Invalid character constant.");
  1010. }
  1011. return INT;
  1012. case '(':
  1013. paren_depth++;
  1014. lexptr++;
  1015. return c;
  1016. case ')':
  1017. if (paren_depth == 0)
  1018. return 0;
  1019. paren_depth--;
  1020. lexptr++;
  1021. return c;
  1022. case ',':
  1023. if (comma_terminates && paren_depth == 0)
  1024. return 0;
  1025. lexptr++;
  1026. return c;
  1027. case '.':
  1028. /* Might be a floating point number. */
  1029. if (lexptr[1] < '0' || lexptr[1] > '9')
  1030. goto symbol; /* Nope, must be a symbol. */
  1031. /* FALL THRU into number case. */
  1032. case '0':
  1033. case '1':
  1034. case '2':
  1035. case '3':
  1036. case '4':
  1037. case '5':
  1038. case '6':
  1039. case '7':
  1040. case '8':
  1041. case '9':
  1042. {
  1043. /* It's a number. */
  1044. int got_dot = 0, got_e = 0, toktype;
  1045. char *p = tokstart;
  1046. int hex = input_radix > 10;
  1047. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1048. {
  1049. p += 2;
  1050. hex = 1;
  1051. }
  1052. else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  1053. {
  1054. p += 2;
  1055. hex = 0;
  1056. }
  1057. for (;; ++p)
  1058. {
  1059. /* This test includes !hex because 'e' is a valid hex digit
  1060. and thus does not indicate a floating point number when
  1061. the radix is hex. */
  1062. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1063. got_dot = got_e = 1;
  1064. /* This test does not include !hex, because a '.' always indicates
  1065. a decimal floating point number regardless of the radix. */
  1066. else if (!got_dot && *p == '.')
  1067. got_dot = 1;
  1068. else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1069. && (*p == '-' || *p == '+'))
  1070. /* This is the sign of the exponent, not the end of the
  1071. number. */
  1072. continue;
  1073. /* We will take any letters or digits. parse_number will
  1074. complain if past the radix, or if L or U are not final. */
  1075. else if ((*p < '0' || *p > '9')
  1076. && ((*p < 'a' || *p > 'z')
  1077. && (*p < 'A' || *p > 'Z')))
  1078. break;
  1079. }
  1080. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
  1081. if (toktype == ERROR)
  1082. {
  1083. char *err_copy = (char *) alloca (p - tokstart + 1);
  1084. memcpy (err_copy, tokstart, p - tokstart);
  1085. err_copy[p - tokstart] = 0;
  1086. error ("Invalid number \"%s\".", err_copy);
  1087. }
  1088. lexptr = p;
  1089. return toktype;
  1090. }
  1091. case '+':
  1092. case '-':
  1093. case '*':
  1094. case '/':
  1095. case '|':
  1096. case '&':
  1097. case '^':
  1098. case '~':
  1099. case '!':
  1100. case '@':
  1101. case '<':
  1102. case '>':
  1103. case '[':
  1104. case ']':
  1105. case '?':
  1106. case ':':
  1107. case '=':
  1108. case '{':
  1109. case '}':
  1110. symbol:
  1111. lexptr++;
  1112. return c;
  1113. case '"':
  1114. /* Build the gdb internal form of the input string in tempbuf,
  1115. translating any standard C escape forms seen. Note that the
  1116. buffer is null byte terminated *only* for the convenience of
  1117. debugging gdb itself and printing the buffer contents when
  1118. the buffer contains no embedded nulls. Gdb does not depend
  1119. upon the buffer being null byte terminated, it uses the length
  1120. string instead. This allows gdb to handle C strings (as well
  1121. as strings in other languages) with embedded null bytes */
  1122. tokptr = ++tokstart;
  1123. tempbufindex = 0;
  1124. do {
  1125. /* Grow the static temp buffer if necessary, including allocating
  1126. the first one on demand. */
  1127. if (tempbufindex + 1 >= tempbufsize)
  1128. {
  1129. tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
  1130. }
  1131. switch (*tokptr)
  1132. {
  1133. case '\0':
  1134. case '"':
  1135. /* Do nothing, loop will terminate. */
  1136. break;
  1137. case '\\':
  1138. tokptr++;
  1139. c = parse_escape (&tokptr);
  1140. if (c == -1)
  1141. {
  1142. continue;
  1143. }
  1144. tempbuf[tempbufindex++] = c;
  1145. break;
  1146. default:
  1147. tempbuf[tempbufindex++] = *tokptr++;
  1148. break;
  1149. }
  1150. } while ((*tokptr != '"') && (*tokptr != '\0'));
  1151. if (*tokptr++ != '"')
  1152. {
  1153. error ("Unterminated string in expression.");
  1154. }
  1155. tempbuf[tempbufindex] = '\0'; /* See note above */
  1156. yylval.sval.ptr = tempbuf;
  1157. yylval.sval.length = tempbufindex;
  1158. lexptr = tokptr;
  1159. return (STRING);
  1160. }
  1161. if (!(c == '_' || c == '$'
  1162. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1163. /* We must have come across a bad character (e.g. ';'). */
  1164. error ("Invalid character '%c' in expression.", c);
  1165. /* It's a name. See how long it is. */
  1166. namelen = 0;
  1167. for (c = tokstart[namelen];
  1168. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1169. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
  1170. {
  1171. /* Template parameter lists are part of the name.
  1172. FIXME: This mishandles `print $a<4&&$a>3'. */
  1173. if (c == '<')
  1174. {
  1175. int i = namelen;
  1176. int nesting_level = 1;
  1177. while (tokstart[++i])
  1178. {
  1179. if (tokstart[i] == '<')
  1180. nesting_level++;
  1181. else if (tokstart[i] == '>')
  1182. {
  1183. if (--nesting_level == 0)
  1184. break;
  1185. }
  1186. }
  1187. if (tokstart[i] == '>')
  1188. namelen = i;
  1189. else
  1190. break;
  1191. }
  1192. /* do NOT uppercase internals because of registers !!! */
  1193. c = tokstart[++namelen];
  1194. }
  1195. uptokstart = uptok(tokstart,namelen);
  1196. /* The token "if" terminates the expression and is NOT
  1197. removed from the input stream. */
  1198. if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
  1199. {
  1200. free (uptokstart);
  1201. return 0;
  1202. }
  1203. lexptr += namelen;
  1204. tryname:
  1205. /* Catch specific keywords. Should be done with a data structure. */
  1206. switch (namelen)
  1207. {
  1208. case 6:
  1209. if (strcmp (uptokstart, "OBJECT") == 0)
  1210. {
  1211. free (uptokstart);
  1212. return CLASS;
  1213. }
  1214. if (strcmp (uptokstart, "RECORD") == 0)
  1215. {
  1216. free (uptokstart);
  1217. return STRUCT;
  1218. }
  1219. if (strcmp (uptokstart, "SIZEOF") == 0)
  1220. {
  1221. free (uptokstart);
  1222. return SIZEOF;
  1223. }
  1224. break;
  1225. case 5:
  1226. if (strcmp (uptokstart, "CLASS") == 0)
  1227. {
  1228. free (uptokstart);
  1229. return CLASS;
  1230. }
  1231. if (strcmp (uptokstart, "FALSE") == 0)
  1232. {
  1233. yylval.lval = 0;
  1234. free (uptokstart);
  1235. return FALSEKEYWORD;
  1236. }
  1237. break;
  1238. case 4:
  1239. if (strcmp (uptokstart, "TRUE") == 0)
  1240. {
  1241. yylval.lval = 1;
  1242. free (uptokstart);
  1243. return TRUEKEYWORD;
  1244. }
  1245. if (strcmp (uptokstart, "SELF") == 0)
  1246. {
  1247. /* here we search for 'this' like
  1248. inserted in FPC stabs debug info */
  1249. static const char this_name[] = "this";
  1250. if (lookup_symbol (this_name, expression_context_block,
  1251. VAR_DOMAIN, (int *) NULL))
  1252. {
  1253. free (uptokstart);
  1254. return THIS;
  1255. }
  1256. }
  1257. break;
  1258. default:
  1259. break;
  1260. }
  1261. yylval.sval.ptr = tokstart;
  1262. yylval.sval.length = namelen;
  1263. if (*tokstart == '$')
  1264. {
  1265. /* $ is the normal prefix for pascal hexadecimal values
  1266. but this conflicts with the GDB use for debugger variables
  1267. so in expression to enter hexadecimal values
  1268. we still need to use C syntax with 0xff */
  1269. write_dollar_variable (yylval.sval);
  1270. free (uptokstart);
  1271. return VARIABLE;
  1272. }
  1273. /* Use token-type BLOCKNAME for symbols that happen to be defined as
  1274. functions or symtabs. If this is not so, then ...
  1275. Use token-type TYPENAME for symbols that happen to be defined
  1276. currently as names of types; NAME for other symbols.
  1277. The caller is not constrained to care about the distinction. */
  1278. {
  1279. char *tmp = copy_name (yylval.sval);
  1280. struct symbol *sym;
  1281. int is_a_field_of_this = 0;
  1282. int is_a_field = 0;
  1283. int hextype;
  1284. if (search_field && current_type)
  1285. is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
  1286. if (is_a_field)
  1287. sym = NULL;
  1288. else
  1289. sym = lookup_symbol (tmp, expression_context_block,
  1290. VAR_DOMAIN, &is_a_field_of_this);
  1291. /* second chance uppercased (as Free Pascal does). */
  1292. if (!sym && !is_a_field_of_this && !is_a_field)
  1293. {
  1294. for (i = 0; i <= namelen; i++)
  1295. {
  1296. if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
  1297. tmp[i] -= ('a'-'A');
  1298. }
  1299. if (search_field && current_type)
  1300. is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
  1301. if (is_a_field)
  1302. sym = NULL;
  1303. else
  1304. sym = lookup_symbol (tmp, expression_context_block,
  1305. VAR_DOMAIN, &is_a_field_of_this);
  1306. if (sym || is_a_field_of_this || is_a_field)
  1307. for (i = 0; i <= namelen; i++)
  1308. {
  1309. if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
  1310. tokstart[i] -= ('a'-'A');
  1311. }
  1312. }
  1313. /* Third chance Capitalized (as GPC does). */
  1314. if (!sym && !is_a_field_of_this && !is_a_field)
  1315. {
  1316. for (i = 0; i <= namelen; i++)
  1317. {
  1318. if (i == 0)
  1319. {
  1320. if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
  1321. tmp[i] -= ('a'-'A');
  1322. }
  1323. else
  1324. if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
  1325. tmp[i] -= ('A'-'a');
  1326. }
  1327. if (search_field && current_type)
  1328. is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
  1329. if (is_a_field)
  1330. sym = NULL;
  1331. else
  1332. sym = lookup_symbol (tmp, expression_context_block,
  1333. VAR_DOMAIN, &is_a_field_of_this);
  1334. if (sym || is_a_field_of_this || is_a_field)
  1335. for (i = 0; i <= namelen; i++)
  1336. {
  1337. if (i == 0)
  1338. {
  1339. if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
  1340. tokstart[i] -= ('a'-'A');
  1341. }
  1342. else
  1343. if ((tokstart[i] >= 'A' && tokstart[i] <= 'Z'))
  1344. tokstart[i] -= ('A'-'a');
  1345. }
  1346. }
  1347. if (is_a_field)
  1348. {
  1349. tempbuf = (char *) realloc (tempbuf, namelen + 1);
  1350. strncpy (tempbuf, tokstart, namelen); tempbuf [namelen] = 0;
  1351. yylval.sval.ptr = tempbuf;
  1352. yylval.sval.length = namelen;
  1353. free (uptokstart);
  1354. return FIELDNAME;
  1355. }
  1356. /* Call lookup_symtab, not lookup_partial_symtab, in case there are
  1357. no psymtabs (coff, xcoff, or some future change to blow away the
  1358. psymtabs once once symbols are read). */
  1359. if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  1360. || lookup_symtab (tmp))
  1361. {
  1362. yylval.ssym.sym = sym;
  1363. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1364. free (uptokstart);
  1365. return BLOCKNAME;
  1366. }
  1367. if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1368. {
  1369. #if 1
  1370. /* Despite the following flaw, we need to keep this code enabled.
  1371. Because we can get called from check_stub_method, if we don't
  1372. handle nested types then it screws many operations in any
  1373. program which uses nested types. */
  1374. /* In "A::x", if x is a member function of A and there happens
  1375. to be a type (nested or not, since the stabs don't make that
  1376. distinction) named x, then this code incorrectly thinks we
  1377. are dealing with nested types rather than a member function. */
  1378. char *p;
  1379. char *namestart;
  1380. struct symbol *best_sym;
  1381. /* Look ahead to detect nested types. This probably should be
  1382. done in the grammar, but trying seemed to introduce a lot
  1383. of shift/reduce and reduce/reduce conflicts. It's possible
  1384. that it could be done, though. Or perhaps a non-grammar, but
  1385. less ad hoc, approach would work well. */
  1386. /* Since we do not currently have any way of distinguishing
  1387. a nested type from a non-nested one (the stabs don't tell
  1388. us whether a type is nested), we just ignore the
  1389. containing type. */
  1390. p = lexptr;
  1391. best_sym = sym;
  1392. while (1)
  1393. {
  1394. /* Skip whitespace. */
  1395. while (*p == ' ' || *p == '\t' || *p == '\n')
  1396. ++p;
  1397. if (*p == ':' && p[1] == ':')
  1398. {
  1399. /* Skip the `::'. */
  1400. p += 2;
  1401. /* Skip whitespace. */
  1402. while (*p == ' ' || *p == '\t' || *p == '\n')
  1403. ++p;
  1404. namestart = p;
  1405. while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
  1406. || (*p >= 'a' && *p <= 'z')
  1407. || (*p >= 'A' && *p <= 'Z'))
  1408. ++p;
  1409. if (p != namestart)
  1410. {
  1411. struct symbol *cur_sym;
  1412. /* As big as the whole rest of the expression, which is
  1413. at least big enough. */
  1414. char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
  1415. char *tmp1;
  1416. tmp1 = ncopy;
  1417. memcpy (tmp1, tmp, strlen (tmp));
  1418. tmp1 += strlen (tmp);
  1419. memcpy (tmp1, "::", 2);
  1420. tmp1 += 2;
  1421. memcpy (tmp1, namestart, p - namestart);
  1422. tmp1[p - namestart] = '\0';
  1423. cur_sym = lookup_symbol (ncopy, expression_context_block,
  1424. VAR_DOMAIN, (int *) NULL);
  1425. if (cur_sym)
  1426. {
  1427. if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
  1428. {
  1429. best_sym = cur_sym;
  1430. lexptr = p;
  1431. }
  1432. else
  1433. break;
  1434. }
  1435. else
  1436. break;
  1437. }
  1438. else
  1439. break;
  1440. }
  1441. else
  1442. break;
  1443. }
  1444. yylval.tsym.type = SYMBOL_TYPE (best_sym);
  1445. #else /* not 0 */
  1446. yylval.tsym.type = SYMBOL_TYPE (sym);
  1447. #endif /* not 0 */
  1448. free (uptokstart);
  1449. return TYPENAME;
  1450. }
  1451. yylval.tsym.type
  1452. = language_lookup_primitive_type_by_name (parse_language,
  1453. parse_gdbarch, tmp);
  1454. if (yylval.tsym.type != NULL)
  1455. {
  1456. free (uptokstart);
  1457. return TYPENAME;
  1458. }
  1459. /* Input names that aren't symbols but ARE valid hex numbers,
  1460. when the input radix permits them, can be names or numbers
  1461. depending on the parse. Note we support radixes > 16 here. */
  1462. if (!sym
  1463. && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1464. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1465. {
  1466. YYSTYPE newlval; /* Its value is ignored. */
  1467. hextype = parse_number (tokstart, namelen, 0, &newlval);
  1468. if (hextype == INT)
  1469. {
  1470. yylval.ssym.sym = sym;
  1471. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1472. free (uptokstart);
  1473. return NAME_OR_INT;
  1474. }
  1475. }
  1476. free(uptokstart);
  1477. /* Any other kind of symbol */
  1478. yylval.ssym.sym = sym;
  1479. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1480. return NAME;
  1481. }
  1482. }
  1483. void
  1484. yyerror (msg)
  1485. char *msg;
  1486. {
  1487. if (prev_lexptr)
  1488. lexptr = prev_lexptr;
  1489. error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
  1490. }