PageRenderTime 54ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/gdb-7.4.50.20120714/gdb/go-exp.y

#
Happy | 1623 lines | 1371 code | 252 blank | 0 comment | 0 complexity | f21a524734651446435b228371162c9d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1
  1. /* YACC parser for Go expressions, for GDB.
  2. Copyright (C) 2012 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* This file is derived from c-exp.y, p-exp.y. */
  15. /* Parse a Go 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. /* Known bugs or limitations:
  31. - Unicode
  32. - &^
  33. - '_' (blank identifier)
  34. - automatic deref of pointers
  35. - method expressions
  36. - interfaces, channels, etc.
  37. And lots of other things.
  38. I'm sure there's some cleanup to do.
  39. */
  40. %{
  41. #include "defs.h"
  42. #include "gdb_string.h"
  43. #include <ctype.h>
  44. #include "expression.h"
  45. #include "value.h"
  46. #include "parser-defs.h"
  47. #include "language.h"
  48. #include "c-lang.h"
  49. #include "go-lang.h"
  50. #include "bfd.h" /* Required by objfiles.h. */
  51. #include "symfile.h" /* Required by objfiles.h. */
  52. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  53. #include "charset.h"
  54. #include "block.h"
  55. #define parse_type builtin_type (parse_gdbarch)
  56. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  57. as well as gratuitiously global symbol names, so we can have multiple
  58. yacc generated parsers in gdb. Note that these are only the variables
  59. produced by yacc. If other parser generators (bison, byacc, etc) produce
  60. additional global names that conflict at link time, then those parser
  61. generators need to be fixed instead of adding those names to this list. */
  62. #define yymaxdepth go_maxdepth
  63. #define yyparse go_parse_internal
  64. #define yylex go_lex
  65. #define yyerror go_error
  66. #define yylval go_lval
  67. #define yychar go_char
  68. #define yydebug go_debug
  69. #define yypact go_pact
  70. #define yyr1 go_r1
  71. #define yyr2 go_r2
  72. #define yydef go_def
  73. #define yychk go_chk
  74. #define yypgo go_pgo
  75. #define yyact go_act
  76. #define yyexca go_exca
  77. #define yyerrflag go_errflag
  78. #define yynerrs go_nerrs
  79. #define yyps go_ps
  80. #define yypv go_pv
  81. #define yys go_s
  82. #define yy_yys go_yys
  83. #define yystate go_state
  84. #define yytmp go_tmp
  85. #define yyv go_v
  86. #define yy_yyv go_yyv
  87. #define yyval go_val
  88. #define yylloc go_lloc
  89. #define yyreds go_reds /* With YYDEBUG defined */
  90. #define yytoks go_toks /* With YYDEBUG defined */
  91. #define yyname go_name /* With YYDEBUG defined */
  92. #define yyrule go_rule /* With YYDEBUG defined */
  93. #define yylhs go_yylhs
  94. #define yylen go_yylen
  95. #define yydefred go_yydefred
  96. #define yydgoto go_yydgoto
  97. #define yysindex go_yysindex
  98. #define yyrindex go_yyrindex
  99. #define yygindex go_yygindex
  100. #define yytable go_yytable
  101. #define yycheck go_yycheck
  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 stoken sval;
  125. struct symtoken ssym;
  126. struct type *tval;
  127. struct typed_stoken tsval;
  128. struct ttype tsym;
  129. int voidval;
  130. enum exp_opcode opcode;
  131. struct internalvar *ivar;
  132. struct stoken_vector svec;
  133. }
  134. %{
  135. /* YYSTYPE gets defined by %union. */
  136. static int parse_number (char *, int, int, YYSTYPE *);
  137. static int parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
  138. DOUBLEST *d, struct type **t);
  139. %}
  140. %type <voidval> exp exp1 type_exp start variable lcurly
  141. %type <lval> rcurly
  142. %type <tval> type
  143. %token <typed_val_int> INT
  144. %token <typed_val_float> FLOAT
  145. /* Both NAME and TYPENAME tokens represent symbols in the input,
  146. and both convey their data as strings.
  147. But a TYPENAME is a string that happens to be defined as a type
  148. or builtin type name (such as int or char)
  149. and a NAME is any other symbol.
  150. Contexts where this distinction is not important can use the
  151. nonterminal "name", which matches either NAME or TYPENAME. */
  152. %token <tsval> RAW_STRING
  153. %token <tsval> STRING
  154. %token <tsval> CHAR
  155. %token <ssym> NAME
  156. %token <tsym> TYPENAME /* Not TYPE_NAME cus already taken. */
  157. %token <voidval> COMPLETE
  158. /*%type <sval> name*/
  159. %type <svec> string_exp
  160. %type <ssym> name_not_typename
  161. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  162. but which would parse as a valid number in the current input radix.
  163. E.g. "c" when input_radix==16. Depending on the parse, it will be
  164. turned into a name or into a number. */
  165. %token <ssym> NAME_OR_INT
  166. %token <lval> TRUE_KEYWORD FALSE_KEYWORD
  167. %token STRUCT_KEYWORD INTERFACE_KEYWORD TYPE_KEYWORD CHAN_KEYWORD
  168. %token SIZEOF_KEYWORD
  169. %token LEN_KEYWORD CAP_KEYWORD
  170. %token NEW_KEYWORD
  171. %token IOTA_KEYWORD NIL_KEYWORD
  172. %token CONST_KEYWORD
  173. %token DOTDOTDOT
  174. %token ENTRY
  175. %token ERROR
  176. /* Special type cases. */
  177. %token BYTE_KEYWORD /* An alias of uint8. */
  178. %token <sval> DOLLAR_VARIABLE
  179. %token <opcode> ASSIGN_MODIFY
  180. %left ','
  181. %left ABOVE_COMMA
  182. %right '=' ASSIGN_MODIFY
  183. %right '?'
  184. %left OROR
  185. %left ANDAND
  186. %left '|'
  187. %left '^'
  188. %left '&'
  189. %left ANDNOT
  190. %left EQUAL NOTEQUAL
  191. %left '<' '>' LEQ GEQ
  192. %left LSH RSH
  193. %left '@'
  194. %left '+' '-'
  195. %left '*' '/' '%'
  196. %right UNARY INCREMENT DECREMENT
  197. %right LEFT_ARROW '.' '[' '('
  198. %%
  199. start : exp1
  200. | type_exp
  201. ;
  202. type_exp: type
  203. { write_exp_elt_opcode(OP_TYPE);
  204. write_exp_elt_type($1);
  205. write_exp_elt_opcode(OP_TYPE); }
  206. ;
  207. /* Expressions, including the comma operator. */
  208. exp1 : exp
  209. | exp1 ',' exp
  210. { write_exp_elt_opcode (BINOP_COMMA); }
  211. ;
  212. /* Expressions, not including the comma operator. */
  213. exp : '*' exp %prec UNARY
  214. { write_exp_elt_opcode (UNOP_IND); }
  215. ;
  216. exp : '&' exp %prec UNARY
  217. { write_exp_elt_opcode (UNOP_ADDR); }
  218. ;
  219. exp : '-' exp %prec UNARY
  220. { write_exp_elt_opcode (UNOP_NEG); }
  221. ;
  222. exp : '+' exp %prec UNARY
  223. { write_exp_elt_opcode (UNOP_PLUS); }
  224. ;
  225. exp : '!' exp %prec UNARY
  226. { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
  227. ;
  228. exp : '^' exp %prec UNARY
  229. { write_exp_elt_opcode (UNOP_COMPLEMENT); }
  230. ;
  231. exp : exp INCREMENT %prec UNARY
  232. { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
  233. ;
  234. exp : exp DECREMENT %prec UNARY
  235. { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
  236. ;
  237. /* foo->bar is not in Go. May want as a gdb extension. Later. */
  238. exp : exp '.' name_not_typename
  239. { write_exp_elt_opcode (STRUCTOP_STRUCT);
  240. write_exp_string ($3.stoken);
  241. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  242. ;
  243. exp : exp '.' name_not_typename COMPLETE
  244. { mark_struct_expression ();
  245. write_exp_elt_opcode (STRUCTOP_STRUCT);
  246. write_exp_string ($3.stoken);
  247. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  248. ;
  249. exp : exp '.' COMPLETE
  250. { struct stoken s;
  251. mark_struct_expression ();
  252. write_exp_elt_opcode (STRUCTOP_STRUCT);
  253. s.ptr = "";
  254. s.length = 0;
  255. write_exp_string (s);
  256. write_exp_elt_opcode (STRUCTOP_STRUCT); }
  257. ;
  258. exp : exp '[' exp1 ']'
  259. { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
  260. ;
  261. exp : exp '('
  262. /* This is to save the value of arglist_len
  263. being accumulated by an outer function call. */
  264. { start_arglist (); }
  265. arglist ')' %prec LEFT_ARROW
  266. { write_exp_elt_opcode (OP_FUNCALL);
  267. write_exp_elt_longcst ((LONGEST) end_arglist ());
  268. write_exp_elt_opcode (OP_FUNCALL); }
  269. ;
  270. lcurly : '{'
  271. { start_arglist (); }
  272. ;
  273. arglist :
  274. ;
  275. arglist : exp
  276. { arglist_len = 1; }
  277. ;
  278. arglist : arglist ',' exp %prec ABOVE_COMMA
  279. { arglist_len++; }
  280. ;
  281. rcurly : '}'
  282. { $$ = end_arglist () - 1; }
  283. ;
  284. exp : lcurly type rcurly exp %prec UNARY
  285. { write_exp_elt_opcode (UNOP_MEMVAL);
  286. write_exp_elt_type ($2);
  287. write_exp_elt_opcode (UNOP_MEMVAL); }
  288. ;
  289. exp : type '(' exp ')' %prec UNARY
  290. { write_exp_elt_opcode (UNOP_CAST);
  291. write_exp_elt_type ($1);
  292. write_exp_elt_opcode (UNOP_CAST); }
  293. ;
  294. exp : '(' exp1 ')'
  295. { }
  296. ;
  297. /* Binary operators in order of decreasing precedence. */
  298. exp : exp '@' exp
  299. { write_exp_elt_opcode (BINOP_REPEAT); }
  300. ;
  301. exp : exp '*' exp
  302. { write_exp_elt_opcode (BINOP_MUL); }
  303. ;
  304. exp : exp '/' exp
  305. { write_exp_elt_opcode (BINOP_DIV); }
  306. ;
  307. exp : exp '%' exp
  308. { write_exp_elt_opcode (BINOP_REM); }
  309. ;
  310. exp : exp '+' exp
  311. { write_exp_elt_opcode (BINOP_ADD); }
  312. ;
  313. exp : exp '-' exp
  314. { write_exp_elt_opcode (BINOP_SUB); }
  315. ;
  316. exp : exp LSH exp
  317. { write_exp_elt_opcode (BINOP_LSH); }
  318. ;
  319. exp : exp RSH exp
  320. { write_exp_elt_opcode (BINOP_RSH); }
  321. ;
  322. exp : exp EQUAL exp
  323. { write_exp_elt_opcode (BINOP_EQUAL); }
  324. ;
  325. exp : exp NOTEQUAL exp
  326. { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  327. ;
  328. exp : exp LEQ exp
  329. { write_exp_elt_opcode (BINOP_LEQ); }
  330. ;
  331. exp : exp GEQ exp
  332. { write_exp_elt_opcode (BINOP_GEQ); }
  333. ;
  334. exp : exp '<' exp
  335. { write_exp_elt_opcode (BINOP_LESS); }
  336. ;
  337. exp : exp '>' exp
  338. { write_exp_elt_opcode (BINOP_GTR); }
  339. ;
  340. exp : exp '&' exp
  341. { write_exp_elt_opcode (BINOP_BITWISE_AND); }
  342. ;
  343. exp : exp '^' exp
  344. { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
  345. ;
  346. exp : exp '|' exp
  347. { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
  348. ;
  349. exp : exp ANDAND exp
  350. { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
  351. ;
  352. exp : exp OROR exp
  353. { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
  354. ;
  355. exp : exp '?' exp ':' exp %prec '?'
  356. { write_exp_elt_opcode (TERNOP_COND); }
  357. ;
  358. exp : exp '=' exp
  359. { write_exp_elt_opcode (BINOP_ASSIGN); }
  360. ;
  361. exp : exp ASSIGN_MODIFY exp
  362. { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  363. write_exp_elt_opcode ($2);
  364. write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  365. ;
  366. exp : INT
  367. { write_exp_elt_opcode (OP_LONG);
  368. write_exp_elt_type ($1.type);
  369. write_exp_elt_longcst ((LONGEST)($1.val));
  370. write_exp_elt_opcode (OP_LONG); }
  371. ;
  372. exp : CHAR
  373. {
  374. struct stoken_vector vec;
  375. vec.len = 1;
  376. vec.tokens = &$1;
  377. write_exp_string_vector ($1.type, &vec);
  378. }
  379. ;
  380. exp : NAME_OR_INT
  381. { YYSTYPE val;
  382. parse_number ($1.stoken.ptr, $1.stoken.length,
  383. 0, &val);
  384. write_exp_elt_opcode (OP_LONG);
  385. write_exp_elt_type (val.typed_val_int.type);
  386. write_exp_elt_longcst ((LONGEST)
  387. val.typed_val_int.val);
  388. write_exp_elt_opcode (OP_LONG);
  389. }
  390. ;
  391. exp : FLOAT
  392. { write_exp_elt_opcode (OP_DOUBLE);
  393. write_exp_elt_type ($1.type);
  394. write_exp_elt_dblcst ($1.dval);
  395. write_exp_elt_opcode (OP_DOUBLE); }
  396. ;
  397. exp : variable
  398. ;
  399. exp : DOLLAR_VARIABLE
  400. {
  401. write_dollar_variable ($1);
  402. }
  403. ;
  404. exp : SIZEOF_KEYWORD '(' type ')' %prec UNARY
  405. {
  406. /* TODO(dje): Go objects in structs. */
  407. write_exp_elt_opcode (OP_LONG);
  408. /* TODO(dje): What's the right type here? */
  409. write_exp_elt_type (parse_type->builtin_unsigned_int);
  410. CHECK_TYPEDEF ($3);
  411. write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  412. write_exp_elt_opcode (OP_LONG);
  413. }
  414. ;
  415. exp : SIZEOF_KEYWORD '(' exp ')' %prec UNARY
  416. {
  417. /* TODO(dje): Go objects in structs. */
  418. write_exp_elt_opcode (UNOP_SIZEOF);
  419. }
  420. string_exp:
  421. STRING
  422. {
  423. /* We copy the string here, and not in the
  424. lexer, to guarantee that we do not leak a
  425. string. */
  426. /* Note that we NUL-terminate here, but just
  427. for convenience. */
  428. struct typed_stoken *vec = XNEW (struct typed_stoken);
  429. $$.len = 1;
  430. $$.tokens = vec;
  431. vec->type = $1.type;
  432. vec->length = $1.length;
  433. vec->ptr = malloc ($1.length + 1);
  434. memcpy (vec->ptr, $1.ptr, $1.length + 1);
  435. }
  436. | string_exp '+' STRING
  437. {
  438. /* Note that we NUL-terminate here, but just
  439. for convenience. */
  440. char *p;
  441. ++$$.len;
  442. $$.tokens = realloc ($$.tokens,
  443. $$.len * sizeof (struct typed_stoken));
  444. p = malloc ($3.length + 1);
  445. memcpy (p, $3.ptr, $3.length + 1);
  446. $$.tokens[$$.len - 1].type = $3.type;
  447. $$.tokens[$$.len - 1].length = $3.length;
  448. $$.tokens[$$.len - 1].ptr = p;
  449. }
  450. ;
  451. exp : string_exp %prec ABOVE_COMMA
  452. {
  453. int i;
  454. write_exp_string_vector (0 /*always utf8*/, &$1);
  455. for (i = 0; i < $1.len; ++i)
  456. free ($1.tokens[i].ptr);
  457. free ($1.tokens);
  458. }
  459. ;
  460. exp : TRUE_KEYWORD
  461. { write_exp_elt_opcode (OP_BOOL);
  462. write_exp_elt_longcst ((LONGEST) $1);
  463. write_exp_elt_opcode (OP_BOOL); }
  464. ;
  465. exp : FALSE_KEYWORD
  466. { write_exp_elt_opcode (OP_BOOL);
  467. write_exp_elt_longcst ((LONGEST) $1);
  468. write_exp_elt_opcode (OP_BOOL); }
  469. ;
  470. variable: name_not_typename ENTRY
  471. { struct symbol *sym = $1.sym;
  472. if (sym == NULL
  473. || !SYMBOL_IS_ARGUMENT (sym)
  474. || !symbol_read_needs_frame (sym))
  475. error (_("@entry can be used only for function "
  476. "parameters, not for \"%s\""),
  477. copy_name ($1.stoken));
  478. write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
  479. write_exp_elt_sym (sym);
  480. write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
  481. }
  482. ;
  483. variable: name_not_typename
  484. { struct symbol *sym = $1.sym;
  485. if (sym)
  486. {
  487. if (symbol_read_needs_frame (sym))
  488. {
  489. if (innermost_block == 0
  490. || contained_in (block_found,
  491. innermost_block))
  492. innermost_block = block_found;
  493. }
  494. write_exp_elt_opcode (OP_VAR_VALUE);
  495. /* We want to use the selected frame, not
  496. another more inner frame which happens to
  497. be in the same block. */
  498. write_exp_elt_block (NULL);
  499. write_exp_elt_sym (sym);
  500. write_exp_elt_opcode (OP_VAR_VALUE);
  501. }
  502. else if ($1.is_a_field_of_this)
  503. {
  504. /* TODO(dje): Can we get here?
  505. E.g., via a mix of c++ and go? */
  506. gdb_assert_not_reached ("go with `this' field");
  507. }
  508. else
  509. {
  510. struct minimal_symbol *msymbol;
  511. char *arg = copy_name ($1.stoken);
  512. msymbol =
  513. lookup_minimal_symbol (arg, NULL, NULL);
  514. if (msymbol != NULL)
  515. write_exp_msymbol (msymbol);
  516. else if (!have_full_symbols ()
  517. && !have_partial_symbols ())
  518. error (_("No symbol table is loaded. "
  519. "Use the \"file\" command."));
  520. else
  521. error (_("No symbol \"%s\" in current context."),
  522. copy_name ($1.stoken));
  523. }
  524. }
  525. ;
  526. /* TODO
  527. method_exp: PACKAGENAME '.' name '.' name
  528. {
  529. }
  530. ;
  531. */
  532. type /* Implements (approximately): [*] type-specifier */
  533. : '*' type
  534. { $$ = lookup_pointer_type ($2); }
  535. | TYPENAME
  536. { $$ = $1.type; }
  537. /*
  538. | STRUCT_KEYWORD name
  539. { $$ = lookup_struct (copy_name ($2),
  540. expression_context_block); }
  541. */
  542. | BYTE_KEYWORD
  543. { $$ = builtin_go_type (parse_gdbarch)
  544. ->builtin_uint8; }
  545. ;
  546. /* TODO
  547. name : NAME { $$ = $1.stoken; }
  548. | TYPENAME { $$ = $1.stoken; }
  549. | NAME_OR_INT { $$ = $1.stoken; }
  550. ;
  551. */
  552. name_not_typename
  553. : NAME
  554. /* These would be useful if name_not_typename was useful, but it is just
  555. a fake for "variable", so these cause reduce/reduce conflicts because
  556. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  557. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  558. context where only a name could occur, this might be useful.
  559. | NAME_OR_INT
  560. */
  561. ;
  562. %%
  563. /* Wrapper on parse_c_float to get the type right for Go. */
  564. static int
  565. parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
  566. DOUBLEST *d, struct type **t)
  567. {
  568. int result = parse_c_float (gdbarch, p, len, d, t);
  569. const struct builtin_type *builtin_types = builtin_type (gdbarch);
  570. const struct builtin_go_type *builtin_go_types = builtin_go_type (gdbarch);
  571. if (*t == builtin_types->builtin_float)
  572. *t = builtin_go_types->builtin_float32;
  573. else if (*t == builtin_types->builtin_double)
  574. *t = builtin_go_types->builtin_float64;
  575. return result;
  576. }
  577. /* Take care of parsing a number (anything that starts with a digit).
  578. Set yylval and return the token type; update lexptr.
  579. LEN is the number of characters in it. */
  580. /* FIXME: Needs some error checking for the float case. */
  581. /* FIXME(dje): IWBN to use c-exp.y's parse_number if we could.
  582. That will require moving the guts into a function that we both call
  583. as our YYSTYPE is different than c-exp.y's */
  584. static int
  585. parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
  586. {
  587. /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
  588. here, and we do kind of silly things like cast to unsigned. */
  589. LONGEST n = 0;
  590. LONGEST prevn = 0;
  591. ULONGEST un;
  592. int i = 0;
  593. int c;
  594. int base = input_radix;
  595. int unsigned_p = 0;
  596. /* Number of "L" suffixes encountered. */
  597. int long_p = 0;
  598. /* We have found a "L" or "U" suffix. */
  599. int found_suffix = 0;
  600. ULONGEST high_bit;
  601. struct type *signed_type;
  602. struct type *unsigned_type;
  603. if (parsed_float)
  604. {
  605. if (! parse_go_float (parse_gdbarch, p, len,
  606. &putithere->typed_val_float.dval,
  607. &putithere->typed_val_float.type))
  608. return ERROR;
  609. return FLOAT;
  610. }
  611. /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
  612. if (p[0] == '0')
  613. switch (p[1])
  614. {
  615. case 'x':
  616. case 'X':
  617. if (len >= 3)
  618. {
  619. p += 2;
  620. base = 16;
  621. len -= 2;
  622. }
  623. break;
  624. case 'b':
  625. case 'B':
  626. if (len >= 3)
  627. {
  628. p += 2;
  629. base = 2;
  630. len -= 2;
  631. }
  632. break;
  633. case 't':
  634. case 'T':
  635. case 'd':
  636. case 'D':
  637. if (len >= 3)
  638. {
  639. p += 2;
  640. base = 10;
  641. len -= 2;
  642. }
  643. break;
  644. default:
  645. base = 8;
  646. break;
  647. }
  648. while (len-- > 0)
  649. {
  650. c = *p++;
  651. if (c >= 'A' && c <= 'Z')
  652. c += 'a' - 'A';
  653. if (c != 'l' && c != 'u')
  654. n *= base;
  655. if (c >= '0' && c <= '9')
  656. {
  657. if (found_suffix)
  658. return ERROR;
  659. n += i = c - '0';
  660. }
  661. else
  662. {
  663. if (base > 10 && c >= 'a' && c <= 'f')
  664. {
  665. if (found_suffix)
  666. return ERROR;
  667. n += i = c - 'a' + 10;
  668. }
  669. else if (c == 'l')
  670. {
  671. ++long_p;
  672. found_suffix = 1;
  673. }
  674. else if (c == 'u')
  675. {
  676. unsigned_p = 1;
  677. found_suffix = 1;
  678. }
  679. else
  680. return ERROR; /* Char not a digit */
  681. }
  682. if (i >= base)
  683. return ERROR; /* Invalid digit in this base. */
  684. /* Portably test for overflow (only works for nonzero values, so make
  685. a second check for zero). FIXME: Can't we just make n and prevn
  686. unsigned and avoid this? */
  687. if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  688. unsigned_p = 1; /* Try something unsigned. */
  689. /* Portably test for unsigned overflow.
  690. FIXME: This check is wrong; for example it doesn't find overflow
  691. on 0x123456789 when LONGEST is 32 bits. */
  692. if (c != 'l' && c != 'u' && n != 0)
  693. {
  694. if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  695. error (_("Numeric constant too large."));
  696. }
  697. prevn = n;
  698. }
  699. /* An integer constant is an int, a long, or a long long. An L
  700. suffix forces it to be long; an LL suffix forces it to be long
  701. long. If not forced to a larger size, it gets the first type of
  702. the above that it fits in. To figure out whether it fits, we
  703. shift it right and see whether anything remains. Note that we
  704. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  705. operation, because many compilers will warn about such a shift
  706. (which always produces a zero result). Sometimes gdbarch_int_bit
  707. or gdbarch_long_bit will be that big, sometimes not. To deal with
  708. the case where it is we just always shift the value more than
  709. once, with fewer bits each time. */
  710. un = (ULONGEST)n >> 2;
  711. if (long_p == 0
  712. && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
  713. {
  714. high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
  715. /* A large decimal (not hex or octal) constant (between INT_MAX
  716. and UINT_MAX) is a long or unsigned long, according to ANSI,
  717. never an unsigned int, but this code treats it as unsigned
  718. int. This probably should be fixed. GCC gives a warning on
  719. such constants. */
  720. unsigned_type = parse_type->builtin_unsigned_int;
  721. signed_type = parse_type->builtin_int;
  722. }
  723. else if (long_p <= 1
  724. && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
  725. {
  726. high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
  727. unsigned_type = parse_type->builtin_unsigned_long;
  728. signed_type = parse_type->builtin_long;
  729. }
  730. else
  731. {
  732. int shift;
  733. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  734. < gdbarch_long_long_bit (parse_gdbarch))
  735. /* A long long does not fit in a LONGEST. */
  736. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  737. else
  738. shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
  739. high_bit = (ULONGEST) 1 << shift;
  740. unsigned_type = parse_type->builtin_unsigned_long_long;
  741. signed_type = parse_type->builtin_long_long;
  742. }
  743. putithere->typed_val_int.val = n;
  744. /* If the high bit of the worked out type is set then this number
  745. has to be unsigned. */
  746. if (unsigned_p || (n & high_bit))
  747. {
  748. putithere->typed_val_int.type = unsigned_type;
  749. }
  750. else
  751. {
  752. putithere->typed_val_int.type = signed_type;
  753. }
  754. return INT;
  755. }
  756. /* Temporary obstack used for holding strings. */
  757. static struct obstack tempbuf;
  758. static int tempbuf_init;
  759. /* Parse a string or character literal from TOKPTR. The string or
  760. character may be wide or unicode. *OUTPTR is set to just after the
  761. end of the literal in the input string. The resulting token is
  762. stored in VALUE. This returns a token value, either STRING or
  763. CHAR, depending on what was parsed. *HOST_CHARS is set to the
  764. number of host characters in the literal. */
  765. static int
  766. parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
  767. int *host_chars)
  768. {
  769. int quote;
  770. /* Build the gdb internal form of the input string in tempbuf. Note
  771. that the buffer is null byte terminated *only* for the
  772. convenience of debugging gdb itself and printing the buffer
  773. contents when the buffer contains no embedded nulls. Gdb does
  774. not depend upon the buffer being null byte terminated, it uses
  775. the length string instead. This allows gdb to handle C strings
  776. (as well as strings in other languages) with embedded null
  777. bytes */
  778. if (!tempbuf_init)
  779. tempbuf_init = 1;
  780. else
  781. obstack_free (&tempbuf, NULL);
  782. obstack_init (&tempbuf);
  783. /* Skip the quote. */
  784. quote = *tokptr;
  785. ++tokptr;
  786. *host_chars = 0;
  787. while (*tokptr)
  788. {
  789. char c = *tokptr;
  790. if (c == '\\')
  791. {
  792. ++tokptr;
  793. *host_chars += c_parse_escape (&tokptr, &tempbuf);
  794. }
  795. else if (c == quote)
  796. break;
  797. else
  798. {
  799. obstack_1grow (&tempbuf, c);
  800. ++tokptr;
  801. /* FIXME: this does the wrong thing with multi-byte host
  802. characters. We could use mbrlen here, but that would
  803. make "set host-charset" a bit less useful. */
  804. ++*host_chars;
  805. }
  806. }
  807. if (*tokptr != quote)
  808. {
  809. if (quote == '"')
  810. error (_("Unterminated string in expression."));
  811. else
  812. error (_("Unmatched single quote."));
  813. }
  814. ++tokptr;
  815. value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/
  816. value->ptr = obstack_base (&tempbuf);
  817. value->length = obstack_object_size (&tempbuf);
  818. *outptr = tokptr;
  819. return quote == '\'' ? CHAR : STRING;
  820. }
  821. struct token
  822. {
  823. char *operator;
  824. int token;
  825. enum exp_opcode opcode;
  826. };
  827. static const struct token tokentab3[] =
  828. {
  829. {">>=", ASSIGN_MODIFY, BINOP_RSH},
  830. {"<<=", ASSIGN_MODIFY, BINOP_LSH},
  831. /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */
  832. {"...", DOTDOTDOT, OP_NULL},
  833. };
  834. static const struct token tokentab2[] =
  835. {
  836. {"+=", ASSIGN_MODIFY, BINOP_ADD},
  837. {"-=", ASSIGN_MODIFY, BINOP_SUB},
  838. {"*=", ASSIGN_MODIFY, BINOP_MUL},
  839. {"/=", ASSIGN_MODIFY, BINOP_DIV},
  840. {"%=", ASSIGN_MODIFY, BINOP_REM},
  841. {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
  842. {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
  843. {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
  844. {"++", INCREMENT, BINOP_END},
  845. {"--", DECREMENT, BINOP_END},
  846. /*{"->", RIGHT_ARROW, BINOP_END}, Doesn't exist in Go. */
  847. {"<-", LEFT_ARROW, BINOP_END},
  848. {"&&", ANDAND, BINOP_END},
  849. {"||", OROR, BINOP_END},
  850. {"<<", LSH, BINOP_END},
  851. {">>", RSH, BINOP_END},
  852. {"==", EQUAL, BINOP_END},
  853. {"!=", NOTEQUAL, BINOP_END},
  854. {"<=", LEQ, BINOP_END},
  855. {">=", GEQ, BINOP_END},
  856. /*{"&^", ANDNOT, BINOP_END}, TODO */
  857. };
  858. /* Identifier-like tokens. */
  859. static const struct token ident_tokens[] =
  860. {
  861. {"true", TRUE_KEYWORD, OP_NULL},
  862. {"false", FALSE_KEYWORD, OP_NULL},
  863. {"nil", NIL_KEYWORD, OP_NULL},
  864. {"const", CONST_KEYWORD, OP_NULL},
  865. {"struct", STRUCT_KEYWORD, OP_NULL},
  866. {"type", TYPE_KEYWORD, OP_NULL},
  867. {"interface", INTERFACE_KEYWORD, OP_NULL},
  868. {"chan", CHAN_KEYWORD, OP_NULL},
  869. {"byte", BYTE_KEYWORD, OP_NULL}, /* An alias of uint8. */
  870. {"len", LEN_KEYWORD, OP_NULL},
  871. {"cap", CAP_KEYWORD, OP_NULL},
  872. {"new", NEW_KEYWORD, OP_NULL},
  873. {"iota", IOTA_KEYWORD, OP_NULL},
  874. };
  875. /* This is set if a NAME token appeared at the very end of the input
  876. string, with no whitespace separating the name from the EOF. This
  877. is used only when parsing to do field name completion. */
  878. static int saw_name_at_eof;
  879. /* This is set if the previously-returned token was a structure
  880. operator -- either '.' or ARROW. This is used only when parsing to
  881. do field name completion. */
  882. static int last_was_structop;
  883. /* Read one token, getting characters through lexptr. */
  884. static int
  885. lex_one_token (void)
  886. {
  887. int c;
  888. int namelen;
  889. unsigned int i;
  890. char *tokstart;
  891. int saw_structop = last_was_structop;
  892. char *copy;
  893. last_was_structop = 0;
  894. retry:
  895. prev_lexptr = lexptr;
  896. tokstart = lexptr;
  897. /* See if it is a special token of length 3. */
  898. for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
  899. if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
  900. {
  901. lexptr += 3;
  902. yylval.opcode = tokentab3[i].opcode;
  903. return tokentab3[i].token;
  904. }
  905. /* See if it is a special token of length 2. */
  906. for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
  907. if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
  908. {
  909. lexptr += 2;
  910. yylval.opcode = tokentab2[i].opcode;
  911. /* NOTE: -> doesn't exist in Go, so we don't need to watch for
  912. setting last_was_structop here. */
  913. return tokentab2[i].token;
  914. }
  915. switch (c = *tokstart)
  916. {
  917. case 0:
  918. if (saw_name_at_eof)
  919. {
  920. saw_name_at_eof = 0;
  921. return COMPLETE;
  922. }
  923. else if (saw_structop)
  924. return COMPLETE;
  925. else
  926. return 0;
  927. case ' ':
  928. case '\t':
  929. case '\n':
  930. lexptr++;
  931. goto retry;
  932. case '[':
  933. case '(':
  934. paren_depth++;
  935. lexptr++;
  936. return c;
  937. case ']':
  938. case ')':
  939. if (paren_depth == 0)
  940. return 0;
  941. paren_depth--;
  942. lexptr++;
  943. return c;
  944. case ',':
  945. if (comma_terminates
  946. && paren_depth == 0)
  947. return 0;
  948. lexptr++;
  949. return c;
  950. case '.':
  951. /* Might be a floating point number. */
  952. if (lexptr[1] < '0' || lexptr[1] > '9')
  953. {
  954. if (in_parse_field)
  955. last_was_structop = 1;
  956. goto symbol; /* Nope, must be a symbol. */
  957. }
  958. /* FALL THRU into number case. */
  959. case '0':
  960. case '1':
  961. case '2':
  962. case '3':
  963. case '4':
  964. case '5':
  965. case '6':
  966. case '7':
  967. case '8':
  968. case '9':
  969. {
  970. /* It's a number. */
  971. int got_dot = 0, got_e = 0, toktype;
  972. char *p = tokstart;
  973. int hex = input_radix > 10;
  974. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  975. {
  976. p += 2;
  977. hex = 1;
  978. }
  979. for (;; ++p)
  980. {
  981. /* This test includes !hex because 'e' is a valid hex digit
  982. and thus does not indicate a floating point number when
  983. the radix is hex. */
  984. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  985. got_dot = got_e = 1;
  986. /* This test does not include !hex, because a '.' always indicates
  987. a decimal floating point number regardless of the radix. */
  988. else if (!got_dot && *p == '.')
  989. got_dot = 1;
  990. else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  991. && (*p == '-' || *p == '+'))
  992. /* This is the sign of the exponent, not the end of the
  993. number. */
  994. continue;
  995. /* We will take any letters or digits. parse_number will
  996. complain if past the radix, or if L or U are not final. */
  997. else if ((*p < '0' || *p > '9')
  998. && ((*p < 'a' || *p > 'z')
  999. && (*p < 'A' || *p > 'Z')))
  1000. break;
  1001. }
  1002. toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
  1003. if (toktype == ERROR)
  1004. {
  1005. char *err_copy = (char *) alloca (p - tokstart + 1);
  1006. memcpy (err_copy, tokstart, p - tokstart);
  1007. err_copy[p - tokstart] = 0;
  1008. error (_("Invalid number \"%s\"."), err_copy);
  1009. }
  1010. lexptr = p;
  1011. return toktype;
  1012. }
  1013. case '@':
  1014. {
  1015. char *p = &tokstart[1];
  1016. size_t len = strlen ("entry");
  1017. while (isspace (*p))
  1018. p++;
  1019. if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
  1020. && p[len] != '_')
  1021. {
  1022. lexptr = &p[len];
  1023. return ENTRY;
  1024. }
  1025. }
  1026. /* FALLTHRU */
  1027. case '+':
  1028. case '-':
  1029. case '*':
  1030. case '/':
  1031. case '%':
  1032. case '|':
  1033. case '&':
  1034. case '^':
  1035. case '~':
  1036. case '!':
  1037. case '<':
  1038. case '>':
  1039. case '?':
  1040. case ':':
  1041. case '=':
  1042. case '{':
  1043. case '}':
  1044. symbol:
  1045. lexptr++;
  1046. return c;
  1047. case '\'':
  1048. case '"':
  1049. case '`':
  1050. {
  1051. int host_len;
  1052. int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
  1053. &host_len);
  1054. if (result == CHAR)
  1055. {
  1056. if (host_len == 0)
  1057. error (_("Empty character constant."));
  1058. else if (host_len > 2 && c == '\'')
  1059. {
  1060. ++tokstart;
  1061. namelen = lexptr - tokstart - 1;
  1062. goto tryname;
  1063. }
  1064. else if (host_len > 1)
  1065. error (_("Invalid character constant."));
  1066. }
  1067. return result;
  1068. }
  1069. }
  1070. if (!(c == '_' || c == '$'
  1071. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1072. /* We must have come across a bad character (e.g. ';'). */
  1073. error (_("Invalid character '%c' in expression."), c);
  1074. /* It's a name. See how long it is. */
  1075. namelen = 0;
  1076. for (c = tokstart[namelen];
  1077. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1078. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
  1079. {
  1080. c = tokstart[++namelen];
  1081. }
  1082. /* The token "if" terminates the expression and is NOT removed from
  1083. the input stream. It doesn't count if it appears in the
  1084. expansion of a macro. */
  1085. if (namelen == 2
  1086. && tokstart[0] == 'i'
  1087. && tokstart[1] == 'f')
  1088. {
  1089. return 0;
  1090. }
  1091. /* For the same reason (breakpoint conditions), "thread N"
  1092. terminates the expression. "thread" could be an identifier, but
  1093. an identifier is never followed by a number without intervening
  1094. punctuation.
  1095. Handle abbreviations of these, similarly to
  1096. breakpoint.c:find_condition_and_thread.
  1097. TODO: Watch for "goroutine" here? */
  1098. if (namelen >= 1
  1099. && strncmp (tokstart, "thread", namelen) == 0
  1100. && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
  1101. {
  1102. char *p = tokstart + namelen + 1;
  1103. while (*p == ' ' || *p == '\t')
  1104. p++;
  1105. if (*p >= '0' && *p <= '9')
  1106. return 0;
  1107. }
  1108. lexptr += namelen;
  1109. tryname:
  1110. yylval.sval.ptr = tokstart;
  1111. yylval.sval.length = namelen;
  1112. /* Catch specific keywords. */
  1113. copy = copy_name (yylval.sval);
  1114. for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++)
  1115. if (strcmp (copy, ident_tokens[i].operator) == 0)
  1116. {
  1117. /* It is ok to always set this, even though we don't always
  1118. strictly need to. */
  1119. yylval.opcode = ident_tokens[i].opcode;
  1120. return ident_tokens[i].token;
  1121. }
  1122. if (*tokstart == '$')
  1123. return DOLLAR_VARIABLE;
  1124. if (in_parse_field && *lexptr == '\0')
  1125. saw_name_at_eof = 1;
  1126. return NAME;
  1127. }
  1128. /* An object of this type is pushed on a FIFO by the "outer" lexer. */
  1129. typedef struct
  1130. {
  1131. int token;
  1132. YYSTYPE value;
  1133. } token_and_value;
  1134. DEF_VEC_O (token_and_value);
  1135. /* A FIFO of tokens that have been read but not yet returned to the
  1136. parser. */
  1137. static VEC (token_and_value) *token_fifo;
  1138. /* Non-zero if the lexer should return tokens from the FIFO. */
  1139. static int popping;
  1140. /* Temporary storage for yylex; this holds symbol names as they are
  1141. built up. */
  1142. static struct obstack name_obstack;
  1143. /* Build "package.name" in name_obstack.
  1144. For convenience of the caller, the name is NUL-terminated,
  1145. but the NUL is not included in the recorded length. */
  1146. static struct stoken
  1147. build_packaged_name (const char *package, int package_len,
  1148. const char *name, int name_len)
  1149. {
  1150. struct stoken result;
  1151. obstack_free (&name_obstack, obstack_base (&name_obstack));
  1152. obstack_grow (&name_obstack, package, package_len);
  1153. obstack_grow_str (&name_obstack, ".");
  1154. obstack_grow (&name_obstack, name, name_len);
  1155. obstack_grow (&name_obstack, "", 1);
  1156. result.ptr = obstack_base (&name_obstack);
  1157. result.length = obstack_object_size (&name_obstack) - 1;
  1158. return result;
  1159. }
  1160. /* Return non-zero if NAME is a package name.
  1161. BLOCK is the scope in which to interpret NAME; this can be NULL
  1162. to mean the global scope. */
  1163. static int
  1164. package_name_p (const char *name, struct block *block)
  1165. {
  1166. struct symbol *sym;
  1167. int is_a_field_of_this;
  1168. sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this);
  1169. if (sym
  1170. && SYMBOL_CLASS (sym) == LOC_TYPEDEF
  1171. && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE)
  1172. return 1;
  1173. return 0;
  1174. }
  1175. /* Classify a (potential) function in the "unsafe" package.
  1176. We fold these into "keywords" to keep things simple, at least until
  1177. something more complex is warranted. */
  1178. static int
  1179. classify_unsafe_function (struct stoken function_name)
  1180. {
  1181. char *copy = copy_name (function_name);
  1182. if (strcmp (copy, "Sizeof") == 0)
  1183. {
  1184. yylval.sval = function_name;
  1185. return SIZEOF_KEYWORD;
  1186. }
  1187. error (_("Unknown function in `unsafe' package: %s"), copy);
  1188. }
  1189. /* Classify token(s) "name1.name2" where name1 is known to be a package.
  1190. The contents of the token are in `yylval'.
  1191. Updates yylval and returns the new token type.
  1192. The result is one of NAME, NAME_OR_INT, or TYPENAME. */
  1193. static int
  1194. classify_packaged_name (struct block *block)
  1195. {
  1196. char *copy;
  1197. struct symbol *sym;
  1198. int is_a_field_of_this = 0;
  1199. copy = copy_name (yylval.sval);
  1200. sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
  1201. if (sym)
  1202. {
  1203. yylval.ssym.sym = sym;
  1204. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1205. }
  1206. return NAME;
  1207. }
  1208. /* Classify a NAME token.
  1209. The contents of the token are in `yylval'.
  1210. Updates yylval and returns the new token type.
  1211. BLOCK is the block in which lookups start; this can be NULL
  1212. to mean the global scope.
  1213. The result is one of NAME, NAME_OR_INT, or TYPENAME. */
  1214. static int
  1215. classify_name (struct block *block)
  1216. {
  1217. struct type *type;
  1218. struct symbol *sym;
  1219. char *copy;
  1220. int is_a_field_of_this = 0;
  1221. copy = copy_name (yylval.sval);
  1222. /* Try primitive types first so they win over bad/weird debug info. */
  1223. type = language_lookup_primitive_type_by_name (parse_language,
  1224. parse_gdbarch, copy);
  1225. if (type != NULL)
  1226. {
  1227. /* NOTE: We take advantage of the fact that yylval coming in was a
  1228. NAME, and that struct ttype is a compatible extension of struct
  1229. stoken, so yylval.tsym.stoken is already filled in. */
  1230. yylval.tsym.type = type;
  1231. return TYPENAME;
  1232. }
  1233. /* TODO: What about other types? */
  1234. sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
  1235. if (sym)
  1236. {
  1237. yylval.ssym.sym = sym;
  1238. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1239. return NAME;
  1240. }
  1241. /* If we didn't find a symbol, look again in the current package.
  1242. This is to, e.g., make "p global_var" work without having to specify
  1243. the package name. We intentionally only looks for objects in the
  1244. current package. */
  1245. {
  1246. char *current_package_name = go_block_package_name (block);
  1247. if (current_package_name != NULL)
  1248. {
  1249. struct stoken sval =
  1250. build_packaged_name (current_package_name,
  1251. strlen (current_package_name),
  1252. copy, strlen (copy));
  1253. xfree (current_package_name);
  1254. sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN,
  1255. &is_a_field_of_this);
  1256. if (sym)
  1257. {
  1258. yylval.ssym.stoken = sval;
  1259. yylval.ssym.sym = sym;
  1260. yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1261. return NAME;
  1262. }
  1263. }
  1264. }
  1265. /* Input names that aren't symbols but ARE valid hex numbers, when
  1266. the input radix permits them, can be names or numbers depending
  1267. on the parse. Note we support radixes > 16 here. */
  1268. if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
  1269. || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10))
  1270. {
  1271. YYSTYPE newlval; /* Its value is ignored. */
  1272. int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
  1273. if (hextype == INT)
  1274. {
  1275. yylval.ssym.sym = NULL;
  1276. yylval.ssym.is_a_field_of_this = 0;
  1277. return NAME_OR_INT;
  1278. }
  1279. }
  1280. yylval.ssym.sym = NULL;
  1281. yylval.ssym.is_a_field_of_this = 0;
  1282. return NAME;
  1283. }
  1284. /* This is taken from c-exp.y mostly to get something working.
  1285. The basic structure has been kept because we may yet need some of it. */
  1286. static int
  1287. yylex (void)
  1288. {
  1289. token_and_value current, next;
  1290. if (popping && !VEC_empty (token_and_value, token_fifo))
  1291. {
  1292. token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
  1293. VEC_ordered_remove (token_and_value, token_fifo, 0);
  1294. yylval = tv.value;
  1295. /* There's no need to fall through to handle package.name
  1296. as that can never happen here. In theory. */
  1297. return tv.token;
  1298. }
  1299. popping = 0;
  1300. current.token = lex_one_token ();
  1301. /* TODO: Need a way to force specifying name1 as a package.
  1302. .name1.name2 ? */
  1303. if (current.token != NAME)
  1304. return current.token;
  1305. /* See if we have "name1 . name2". */
  1306. current.value = yylval;
  1307. next.token = lex_one_token ();
  1308. next.value = yylval;
  1309. if (next.token == '.')
  1310. {
  1311. token_and_value name2;
  1312. name2.token = lex_one_token ();
  1313. name2.value = yylval;
  1314. if (name2.token == NAME)
  1315. {
  1316. /* Ok, we have "name1 . name2". */
  1317. char *copy;
  1318. copy = copy_name (current.value.sval);
  1319. if (strcmp (copy, "unsafe") == 0)
  1320. {
  1321. popping = 1;
  1322. return classify_unsafe_function (name2.value.sval);
  1323. }
  1324. if (package_name_p (copy, expression_context_block))
  1325. {
  1326. popping = 1;
  1327. yylval.sval = build_packaged_name (current.value.sval.ptr,
  1328. current.value.sval.length,
  1329. name2.value.sval.ptr,
  1330. name2.value.sval.length);
  1331. return classify_packaged_name (expression_context_block);
  1332. }
  1333. }
  1334. VEC_safe_push (token_and_value, token_fifo, &next);
  1335. VEC_safe_push (token_and_value, token_fifo, &name2);
  1336. }
  1337. else
  1338. {
  1339. VEC_safe_push (token_and_value, token_fifo, &next);
  1340. }
  1341. /* If we arrive here we don't have a package-qualified name. */
  1342. popping = 1;
  1343. yylval = current.value;
  1344. return classify_name (expression_context_block);
  1345. }
  1346. int
  1347. go_parse (void)
  1348. {
  1349. int result;
  1350. struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
  1351. make_cleanup_restore_integer (&yydebug);
  1352. yydebug = parser_debug;
  1353. /* Initialize some state used by the lexer. */
  1354. last_was_structop = 0;
  1355. saw_name_at_eof = 0;
  1356. VEC_free (token_and_value, token_fifo);
  1357. popping = 0;
  1358. obstack_init (&name_obstack);
  1359. make_cleanup_obstack_free (&name_obstack);
  1360. result = yyparse ();
  1361. do_cleanups (back_to);
  1362. return result;
  1363. }
  1364. void
  1365. yyerror (char *msg)
  1366. {
  1367. if (prev_lexptr)
  1368. lexptr = prev_lexptr;
  1369. error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
  1370. }