gdb/go-exp.y - gdb

  1. /* YACC parser for Go expressions, for GDB.

  2.    Copyright (C) 2012-2015 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 <ctype.h>
  43. #include "expression.h"
  44. #include "value.h"
  45. #include "parser-defs.h"
  46. #include "language.h"
  47. #include "c-lang.h"
  48. #include "go-lang.h"
  49. #include "bfd.h" /* Required by objfiles.h.  */
  50. #include "symfile.h" /* Required by objfiles.h.  */
  51. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  52. #include "charset.h"
  53. #include "block.h"

  54. #define parse_type(ps) builtin_type (parse_gdbarch (ps))

  55. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  56.    as well as gratuitiously global symbol names, so we can have multiple
  57.    yacc generated parsers in gdb.  Note that these are only the variables
  58.    produced by yacc.  If other parser generators (bison, byacc, etc) produce
  59.    additional global names that conflict at link time, then those parser
  60.    generators need to be fixed instead of adding those names to this list.  */

  61. #define        yymaxdepth go_maxdepth
  62. #define        yyparse        go_parse_internal
  63. #define        yylex        go_lex
  64. #define        yyerror        go_error
  65. #define        yylval        go_lval
  66. #define        yychar        go_char
  67. #define        yydebug        go_debug
  68. #define        yypact        go_pact
  69. #define        yyr1        go_r1
  70. #define        yyr2        go_r2
  71. #define        yydef        go_def
  72. #define        yychk        go_chk
  73. #define        yypgo        go_pgo
  74. #define        yyact        go_act
  75. #define        yyexca        go_exca
  76. #define yyerrflag go_errflag
  77. #define yynerrs        go_nerrs
  78. #define        yyps        go_ps
  79. #define        yypv        go_pv
  80. #define        yys        go_s
  81. #define        yy_yys        go_yys
  82. #define        yystate        go_state
  83. #define        yytmp        go_tmp
  84. #define        yyv        go_v
  85. #define        yy_yyv        go_yyv
  86. #define        yyval        go_val
  87. #define        yylloc        go_lloc
  88. #define yyreds        go_reds                /* With YYDEBUG defined */
  89. #define yytoks        go_toks                /* With YYDEBUG defined */
  90. #define yyname        go_name                /* With YYDEBUG defined */
  91. #define yyrule        go_rule                /* With YYDEBUG defined */
  92. #define yylhs        go_yylhs
  93. #define yylen        go_yylen
  94. #define yydefred go_yydefred
  95. #define yydgoto        go_yydgoto
  96. #define yysindex go_yysindex
  97. #define yyrindex go_yyrindex
  98. #define yygindex go_yygindex
  99. #define yytable         go_yytable
  100. #define yycheck         go_yycheck

  101. #ifndef YYDEBUG
  102. #define        YYDEBUG 1                /* Default to yydebug support */
  103. #endif

  104. #define YYFPRINTF parser_fprintf

  105. /* The state of the parser, used internally when we are parsing the
  106.    expression.  */

  107. static struct parser_state *pstate = NULL;

  108. int yyparse (void);

  109. static int yylex (void);

  110. void yyerror (char *);

  111. %}

  112. /* Although the yacc "value" of an expression is not used,
  113.    since the result is stored in the structure being created,
  114.    other node types do have values.  */

  115. %union
  116.   {
  117.     LONGEST lval;
  118.     struct {
  119.       LONGEST val;
  120.       struct type *type;
  121.     } typed_val_int;
  122.     struct {
  123.       DOUBLEST dval;
  124.       struct type *type;
  125.     } typed_val_float;
  126.     struct stoken sval;
  127.     struct symtoken ssym;
  128.     struct type *tval;
  129.     struct typed_stoken tsval;
  130.     struct ttype tsym;
  131.     int voidval;
  132.     enum exp_opcode opcode;
  133.     struct internalvar *ivar;
  134.     struct stoken_vector svec;
  135.   }

  136. %{
  137. /* YYSTYPE gets defined by %union.  */
  138. static int parse_number (struct parser_state *,
  139.                          const char *, int, int, YYSTYPE *);
  140. static int parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
  141.                            DOUBLEST *d, struct type **t);
  142. %}

  143. %type <voidval> exp exp1 type_exp start variable lcurly
  144. %type <lval> rcurly
  145. %type <tval> type

  146. %token <typed_val_int> INT
  147. %token <typed_val_float> FLOAT

  148. /* Both NAME and TYPENAME tokens represent symbols in the input,
  149.    and both convey their data as strings.
  150.    But a TYPENAME is a string that happens to be defined as a type
  151.    or builtin type name (such as int or char)
  152.    and a NAME is any other symbol.
  153.    Contexts where this distinction is not important can use the
  154.    nonterminal "name", which matches either NAME or TYPENAME.  */

  155. %token <tsval> RAW_STRING
  156. %token <tsval> STRING
  157. %token <tsval> CHAR
  158. %token <ssym> NAME
  159. %token <tsym> TYPENAME /* Not TYPE_NAME cus already taken.  */
  160. %token <voidval> COMPLETE
  161. /*%type <sval> name*/
  162. %type <svec> string_exp
  163. %type <ssym> name_not_typename

  164. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  165.    but which would parse as a valid number in the current input radix.
  166.    E.g. "c" when input_radix==16.  Depending on the parse, it will be
  167.    turned into a name or into a number.  */
  168. %token <ssym> NAME_OR_INT

  169. %token <lval> TRUE_KEYWORD FALSE_KEYWORD
  170. %token STRUCT_KEYWORD INTERFACE_KEYWORD TYPE_KEYWORD CHAN_KEYWORD
  171. %token SIZEOF_KEYWORD
  172. %token LEN_KEYWORD CAP_KEYWORD
  173. %token NEW_KEYWORD
  174. %token IOTA_KEYWORD NIL_KEYWORD
  175. %token CONST_KEYWORD
  176. %token DOTDOTDOT
  177. %token ENTRY
  178. %token ERROR

  179. /* Special type cases.  */
  180. %token BYTE_KEYWORD /* An alias of uint8.  */

  181. %token <sval> DOLLAR_VARIABLE

  182. %token <opcode> ASSIGN_MODIFY

  183. %left ','
  184. %left ABOVE_COMMA
  185. %right '=' ASSIGN_MODIFY
  186. %right '?'
  187. %left OROR
  188. %left ANDAND
  189. %left '|'
  190. %left '^'
  191. %left '&'
  192. %left ANDNOT
  193. %left EQUAL NOTEQUAL
  194. %left '<' '>' LEQ GEQ
  195. %left LSH RSH
  196. %left '@'
  197. %left '+' '-'
  198. %left '*' '/' '%'
  199. %right UNARY INCREMENT DECREMENT
  200. %right LEFT_ARROW '.' '[' '('


  201. %%

  202. start   :        exp1
  203.         |        type_exp
  204.         ;

  205. type_exp:        type
  206.                         { write_exp_elt_opcode (pstate, OP_TYPE);
  207.                           write_exp_elt_type (pstate, $1);
  208.                           write_exp_elt_opcode (pstate, OP_TYPE); }
  209.         ;

  210. /* Expressions, including the comma operator.  */
  211. exp1        :        exp
  212.         |        exp1 ',' exp
  213.                         { write_exp_elt_opcode (pstate, BINOP_COMMA); }
  214.         ;

  215. /* Expressions, not including the comma operator.  */
  216. exp        :        '*' exp    %prec UNARY
  217.                         { write_exp_elt_opcode (pstate, UNOP_IND); }
  218.         ;

  219. exp        :        '&' exp    %prec UNARY
  220.                         { write_exp_elt_opcode (pstate, UNOP_ADDR); }
  221.         ;

  222. exp        :        '-' exp    %prec UNARY
  223.                         { write_exp_elt_opcode (pstate, UNOP_NEG); }
  224.         ;

  225. exp        :        '+' exp    %prec UNARY
  226.                         { write_exp_elt_opcode (pstate, UNOP_PLUS); }
  227.         ;

  228. exp        :        '!' exp    %prec UNARY
  229.                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  230.         ;

  231. exp        :        '^' exp    %prec UNARY
  232.                         { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
  233.         ;

  234. exp        :        exp INCREMENT    %prec UNARY
  235.                         { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
  236.         ;

  237. exp        :        exp DECREMENT    %prec UNARY
  238.                         { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
  239.         ;

  240. /* foo->bar is not in Go.  May want as a gdb extension.  Later.  */

  241. exp        :        exp '.' name_not_typename
  242.                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  243.                           write_exp_string (pstate, $3.stoken);
  244.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  245.         ;

  246. exp        :        exp '.' name_not_typename COMPLETE
  247.                         { mark_struct_expression (pstate);
  248.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  249.                           write_exp_string (pstate, $3.stoken);
  250.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  251.         ;

  252. exp        :        exp '.' COMPLETE
  253.                         { struct stoken s;
  254.                           mark_struct_expression (pstate);
  255.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  256.                           s.ptr = "";
  257.                           s.length = 0;
  258.                           write_exp_string (pstate, s);
  259.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  260.         ;

  261. exp        :        exp '[' exp1 ']'
  262.                         { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
  263.         ;

  264. exp        :        exp '('
  265.                         /* This is to save the value of arglist_len
  266.                            being accumulated by an outer function call.  */
  267.                         { start_arglist (); }
  268.                 arglist ')'        %prec LEFT_ARROW
  269.                         { write_exp_elt_opcode (pstate, OP_FUNCALL);
  270.                           write_exp_elt_longcst (pstate,
  271.                                                  (LONGEST) end_arglist ());
  272.                           write_exp_elt_opcode (pstate, OP_FUNCALL); }
  273.         ;

  274. lcurly        :        '{'
  275.                         { start_arglist (); }
  276.         ;

  277. arglist        :
  278.         ;

  279. arglist        :        exp
  280.                         { arglist_len = 1; }
  281.         ;

  282. arglist        :        arglist ',' exp   %prec ABOVE_COMMA
  283.                         { arglist_len++; }
  284.         ;

  285. rcurly        :        '}'
  286.                         { $$ = end_arglist () - 1; }
  287.         ;

  288. exp        :        lcurly type rcurly exp  %prec UNARY
  289.                         { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
  290.                           write_exp_elt_type (pstate, $2);
  291.                           write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
  292.         ;

  293. exp        :        type '(' exp ')'  %prec UNARY
  294.                         { write_exp_elt_opcode (pstate, UNOP_CAST);
  295.                           write_exp_elt_type (pstate, $1);
  296.                           write_exp_elt_opcode (pstate, UNOP_CAST); }
  297.         ;

  298. exp        :        '(' exp1 ')'
  299.                         { }
  300.         ;

  301. /* Binary operators in order of decreasing precedence.  */

  302. exp        :        exp '@' exp
  303.                         { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
  304.         ;

  305. exp        :        exp '*' exp
  306.                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
  307.         ;

  308. exp        :        exp '/' exp
  309.                         { write_exp_elt_opcode (pstate, BINOP_DIV); }
  310.         ;

  311. exp        :        exp '%' exp
  312.                         { write_exp_elt_opcode (pstate, BINOP_REM); }
  313.         ;

  314. exp        :        exp '+' exp
  315.                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
  316.         ;

  317. exp        :        exp '-' exp
  318.                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
  319.         ;

  320. exp        :        exp LSH exp
  321.                         { write_exp_elt_opcode (pstate, BINOP_LSH); }
  322.         ;

  323. exp        :        exp RSH exp
  324.                         { write_exp_elt_opcode (pstate, BINOP_RSH); }
  325.         ;

  326. exp        :        exp EQUAL exp
  327.                         { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  328.         ;

  329. exp        :        exp NOTEQUAL exp
  330.                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  331.         ;

  332. exp        :        exp LEQ exp
  333.                         { write_exp_elt_opcode (pstate, BINOP_LEQ); }
  334.         ;

  335. exp        :        exp GEQ exp
  336.                         { write_exp_elt_opcode (pstate, BINOP_GEQ); }
  337.         ;

  338. exp        :        exp '<' exp
  339.                         { write_exp_elt_opcode (pstate, BINOP_LESS); }
  340.         ;

  341. exp        :        exp '>' exp
  342.                         { write_exp_elt_opcode (pstate, BINOP_GTR); }
  343.         ;

  344. exp        :        exp '&' exp
  345.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  346.         ;

  347. exp        :        exp '^' exp
  348.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  349.         ;

  350. exp        :        exp '|' exp
  351.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  352.         ;

  353. exp        :        exp ANDAND exp
  354.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  355.         ;

  356. exp        :        exp OROR exp
  357.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  358.         ;

  359. exp        :        exp '?' exp ':' exp        %prec '?'
  360.                         { write_exp_elt_opcode (pstate, TERNOP_COND); }
  361.         ;

  362. exp        :        exp '=' exp
  363.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  364.         ;

  365. exp        :        exp ASSIGN_MODIFY exp
  366.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
  367.                           write_exp_elt_opcode (pstate, $2);
  368.                           write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
  369.         ;

  370. exp        :        INT
  371.                         { write_exp_elt_opcode (pstate, OP_LONG);
  372.                           write_exp_elt_type (pstate, $1.type);
  373.                           write_exp_elt_longcst (pstate, (LONGEST)($1.val));
  374.                           write_exp_elt_opcode (pstate, OP_LONG); }
  375.         ;

  376. exp        :        CHAR
  377.                         {
  378.                           struct stoken_vector vec;
  379.                           vec.len = 1;
  380.                           vec.tokens = &$1;
  381.                           write_exp_string_vector (pstate, $1.type, &vec);
  382.                         }
  383.         ;

  384. exp        :        NAME_OR_INT
  385.                         { YYSTYPE val;
  386.                           parse_number (pstate, $1.stoken.ptr,
  387.                                         $1.stoken.length, 0, &val);
  388.                           write_exp_elt_opcode (pstate, OP_LONG);
  389.                           write_exp_elt_type (pstate, val.typed_val_int.type);
  390.                           write_exp_elt_longcst (pstate, (LONGEST)
  391.                                                  val.typed_val_int.val);
  392.                           write_exp_elt_opcode (pstate, OP_LONG);
  393.                         }
  394.         ;


  395. exp        :        FLOAT
  396.                         { write_exp_elt_opcode (pstate, OP_DOUBLE);
  397.                           write_exp_elt_type (pstate, $1.type);
  398.                           write_exp_elt_dblcst (pstate, $1.dval);
  399.                           write_exp_elt_opcode (pstate, OP_DOUBLE); }
  400.         ;

  401. exp        :        variable
  402.         ;

  403. exp        :        DOLLAR_VARIABLE
  404.                         {
  405.                           write_dollar_variable (pstate, $1);
  406.                         }
  407.         ;

  408. exp        :        SIZEOF_KEYWORD '(' type ')'  %prec UNARY
  409.                         {
  410.                           /* TODO(dje): Go objects in structs.  */
  411.                           write_exp_elt_opcode (pstate, OP_LONG);
  412.                           /* TODO(dje): What's the right type here?  */
  413.                           write_exp_elt_type
  414.                             (pstate,
  415.                              parse_type (pstate)->builtin_unsigned_int);
  416.                           CHECK_TYPEDEF ($3);
  417.                           write_exp_elt_longcst (pstate,
  418.                                                  (LONGEST) TYPE_LENGTH ($3));
  419.                           write_exp_elt_opcode (pstate, OP_LONG);
  420.                         }
  421.         ;

  422. exp        :        SIZEOF_KEYWORD  '(' exp ')'  %prec UNARY
  423.                         {
  424.                           /* TODO(dje): Go objects in structs.  */
  425.                           write_exp_elt_opcode (pstate, UNOP_SIZEOF);
  426.                         }

  427. string_exp:
  428.                 STRING
  429.                         {
  430.                           /* We copy the string here, and not in the
  431.                              lexer, to guarantee that we do not leak a
  432.                              string.  */
  433.                           /* Note that we NUL-terminate here, but just
  434.                              for convenience.  */
  435.                           struct typed_stoken *vec = XNEW (struct typed_stoken);
  436.                           $$.len = 1;
  437.                           $$.tokens = vec;

  438.                           vec->type = $1.type;
  439.                           vec->length = $1.length;
  440.                           vec->ptr = malloc ($1.length + 1);
  441.                           memcpy (vec->ptr, $1.ptr, $1.length + 1);
  442.                         }

  443.         |        string_exp '+' STRING
  444.                         {
  445.                           /* Note that we NUL-terminate here, but just
  446.                              for convenience.  */
  447.                           char *p;
  448.                           ++$$.len;
  449.                           $$.tokens = realloc ($$.tokens,
  450.                                                $$.len * sizeof (struct typed_stoken));

  451.                           p = malloc ($3.length + 1);
  452.                           memcpy (p, $3.ptr, $3.length + 1);

  453.                           $$.tokens[$$.len - 1].type = $3.type;
  454.                           $$.tokens[$$.len - 1].length = $3.length;
  455.                           $$.tokens[$$.len - 1].ptr = p;
  456.                         }
  457.         ;

  458. exp        :        string_exp  %prec ABOVE_COMMA
  459.                         {
  460.                           int i;

  461.                           write_exp_string_vector (pstate, 0 /*always utf8*/,
  462.                                                    &$1);
  463.                           for (i = 0; i < $1.len; ++i)
  464.                             free ($1.tokens[i].ptr);
  465.                           free ($1.tokens);
  466.                         }
  467.         ;

  468. exp        :        TRUE_KEYWORD
  469.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  470.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  471.                           write_exp_elt_opcode (pstate, OP_BOOL); }
  472.         ;

  473. exp        :        FALSE_KEYWORD
  474.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  475.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  476.                           write_exp_elt_opcode (pstate, OP_BOOL); }
  477.         ;

  478. variable:        name_not_typename ENTRY
  479.                         { struct symbol *sym = $1.sym;

  480.                           if (sym == NULL
  481.                               || !SYMBOL_IS_ARGUMENT (sym)
  482.                               || !symbol_read_needs_frame (sym))
  483.                             error (_("@entry can be used only for function "
  484.                                      "parameters, not for \"%s\""),
  485.                                    copy_name ($1.stoken));

  486.                           write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
  487.                           write_exp_elt_sym (pstate, sym);
  488.                           write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
  489.                         }
  490.         ;

  491. variable:        name_not_typename
  492.                         { struct symbol *sym = $1.sym;

  493.                           if (sym)
  494.                             {
  495.                               if (symbol_read_needs_frame (sym))
  496.                                 {
  497.                                   if (innermost_block == 0
  498.                                       || contained_in (block_found,
  499.                                                        innermost_block))
  500.                                     innermost_block = block_found;
  501.                                 }

  502.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  503.                               /* We want to use the selected frame, not
  504.                                  another more inner frame which happens to
  505.                                  be in the same block.  */
  506.                               write_exp_elt_block (pstate, NULL);
  507.                               write_exp_elt_sym (pstate, sym);
  508.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  509.                             }
  510.                           else if ($1.is_a_field_of_this)
  511.                             {
  512.                               /* TODO(dje): Can we get here?
  513.                                  E.g., via a mix of c++ and go?  */
  514.                               gdb_assert_not_reached ("go with `this' field");
  515.                             }
  516.                           else
  517.                             {
  518.                               struct bound_minimal_symbol msymbol;
  519.                               char *arg = copy_name ($1.stoken);

  520.                               msymbol =
  521.                                 lookup_bound_minimal_symbol (arg);
  522.                               if (msymbol.minsym != NULL)
  523.                                 write_exp_msymbol (pstate, msymbol);
  524.                               else if (!have_full_symbols ()
  525.                                        && !have_partial_symbols ())
  526.                                 error (_("No symbol table is loaded.  "
  527.                                        "Use the \"file\" command."));
  528.                               else
  529.                                 error (_("No symbol \"%s\" in current context."),
  530.                                        copy_name ($1.stoken));
  531.                             }
  532.                         }
  533.         ;

  534. /* TODO
  535. method_exp: PACKAGENAME '.' name '.' name
  536.                         {
  537.                         }
  538.         ;
  539. */

  540. type  /* Implements (approximately): [*] type-specifier */
  541.         :        '*' type
  542.                         { $$ = lookup_pointer_type ($2); }
  543.         |        TYPENAME
  544.                         { $$ = $1.type; }
  545. /*
  546.         |        STRUCT_KEYWORD name
  547.                         { $$ = lookup_struct (copy_name ($2),
  548.                                               expression_context_block); }
  549. */
  550.         |        BYTE_KEYWORD
  551.                         { $$ = builtin_go_type (parse_gdbarch (pstate))
  552.                             ->builtin_uint8; }
  553.         ;

  554. /* TODO
  555. name        :        NAME { $$ = $1.stoken; }
  556.         |        TYPENAME { $$ = $1.stoken; }
  557.         |        NAME_OR_INT  { $$ = $1.stoken; }
  558.         ;
  559. */

  560. name_not_typename
  561.         :        NAME
  562. /* These would be useful if name_not_typename was useful, but it is just
  563.    a fake for "variable", so these cause reduce/reduce conflicts because
  564.    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  565.    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
  566.    context where only a name could occur, this might be useful.
  567.         |        NAME_OR_INT
  568. */
  569.         ;

  570. %%

  571. /* Wrapper on parse_c_float to get the type right for Go.  */

  572. static int
  573. parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
  574.                 DOUBLEST *d, struct type **t)
  575. {
  576.   int result = parse_c_float (gdbarch, p, len, d, t);
  577.   const struct builtin_type *builtin_types = builtin_type (gdbarch);
  578.   const struct builtin_go_type *builtin_go_types = builtin_go_type (gdbarch);

  579.   if (*t == builtin_types->builtin_float)
  580.     *t = builtin_go_types->builtin_float32;
  581.   else if (*t == builtin_types->builtin_double)
  582.     *t = builtin_go_types->builtin_float64;

  583.   return result;
  584. }

  585. /* Take care of parsing a number (anything that starts with a digit).
  586.    Set yylval and return the token type; update lexptr.
  587.    LEN is the number of characters in it.  */

  588. /* FIXME: Needs some error checking for the float case.  */
  589. /* FIXME(dje): IWBN to use c-exp.y's parse_number if we could.
  590.    That will require moving the guts into a function that we both call
  591.    as our YYSTYPE is different than c-exp.y's  */

  592. static int
  593. parse_number (struct parser_state *par_state,
  594.               const char *p, int len, int parsed_float, YYSTYPE *putithere)
  595. {
  596.   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
  597.      here, and we do kind of silly things like cast to unsigned.  */
  598.   LONGEST n = 0;
  599.   LONGEST prevn = 0;
  600.   ULONGEST un;

  601.   int i = 0;
  602.   int c;
  603.   int base = input_radix;
  604.   int unsigned_p = 0;

  605.   /* Number of "L" suffixes encountered.  */
  606.   int long_p = 0;

  607.   /* We have found a "L" or "U" suffix.  */
  608.   int found_suffix = 0;

  609.   ULONGEST high_bit;
  610.   struct type *signed_type;
  611.   struct type *unsigned_type;

  612.   if (parsed_float)
  613.     {
  614.       if (! parse_go_float (parse_gdbarch (par_state), p, len,
  615.                             &putithere->typed_val_float.dval,
  616.                             &putithere->typed_val_float.type))
  617.         return ERROR;
  618.       return FLOAT;
  619.     }

  620.   /* Handle base-switching prefixes 0x, 0t, 0d, 0.  */
  621.   if (p[0] == '0')
  622.     switch (p[1])
  623.       {
  624.       case 'x':
  625.       case 'X':
  626.         if (len >= 3)
  627.           {
  628.             p += 2;
  629.             base = 16;
  630.             len -= 2;
  631.           }
  632.         break;

  633.       case 'b':
  634.       case 'B':
  635.         if (len >= 3)
  636.           {
  637.             p += 2;
  638.             base = 2;
  639.             len -= 2;
  640.           }
  641.         break;

  642.       case 't':
  643.       case 'T':
  644.       case 'd':
  645.       case 'D':
  646.         if (len >= 3)
  647.           {
  648.             p += 2;
  649.             base = 10;
  650.             len -= 2;
  651.           }
  652.         break;

  653.       default:
  654.         base = 8;
  655.         break;
  656.       }

  657.   while (len-- > 0)
  658.     {
  659.       c = *p++;
  660.       if (c >= 'A' && c <= 'Z')
  661.         c += 'a' - 'A';
  662.       if (c != 'l' && c != 'u')
  663.         n *= base;
  664.       if (c >= '0' && c <= '9')
  665.         {
  666.           if (found_suffix)
  667.             return ERROR;
  668.           n += i = c - '0';
  669.         }
  670.       else
  671.         {
  672.           if (base > 10 && c >= 'a' && c <= 'f')
  673.             {
  674.               if (found_suffix)
  675.                 return ERROR;
  676.               n += i = c - 'a' + 10;
  677.             }
  678.           else if (c == 'l')
  679.             {
  680.               ++long_p;
  681.               found_suffix = 1;
  682.             }
  683.           else if (c == 'u')
  684.             {
  685.               unsigned_p = 1;
  686.               found_suffix = 1;
  687.             }
  688.           else
  689.             return ERROR;        /* Char not a digit */
  690.         }
  691.       if (i >= base)
  692.         return ERROR;                /* Invalid digit in this base.  */

  693.       /* Portably test for overflow (only works for nonzero values, so make
  694.          a second check for zero).  FIXME: Can't we just make n and prevn
  695.          unsigned and avoid this?  */
  696.       if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  697.         unsigned_p = 1;                /* Try something unsigned.  */

  698.       /* Portably test for unsigned overflow.
  699.          FIXME: This check is wrong; for example it doesn't find overflow
  700.          on 0x123456789 when LONGEST is 32 bits.  */
  701.       if (c != 'l' && c != 'u' && n != 0)
  702.         {
  703.           if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  704.             error (_("Numeric constant too large."));
  705.         }
  706.       prevn = n;
  707.     }

  708.   /* An integer constant is an int, a long, or a long long.  An L
  709.      suffix forces it to be long; an LL suffix forces it to be long
  710.      long.  If not forced to a larger size, it gets the first type of
  711.      the above that it fits in.  To figure out whether it fits, we
  712.      shift it right and see whether anything remains.  Note that we
  713.      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  714.      operation, because many compilers will warn about such a shift
  715.      (which always produces a zero result).  Sometimes gdbarch_int_bit
  716.      or gdbarch_long_bit will be that big, sometimes not.  To deal with
  717.      the case where it is we just always shift the value more than
  718.      once, with fewer bits each time.  */

  719.   un = (ULONGEST)n >> 2;
  720.   if (long_p == 0
  721.       && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
  722.     {
  723.       high_bit
  724.         = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);

  725.       /* A large decimal (not hex or octal) constant (between INT_MAX
  726.          and UINT_MAX) is a long or unsigned long, according to ANSI,
  727.          never an unsigned int, but this code treats it as unsigned
  728.          int.  This probably should be fixed.  GCC gives a warning on
  729.          such constants.  */

  730.       unsigned_type = parse_type (par_state)->builtin_unsigned_int;
  731.       signed_type = parse_type (par_state)->builtin_int;
  732.     }
  733.   else if (long_p <= 1
  734.            && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
  735.     {
  736.       high_bit
  737.         = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
  738.       unsigned_type = parse_type (par_state)->builtin_unsigned_long;
  739.       signed_type = parse_type (par_state)->builtin_long;
  740.     }
  741.   else
  742.     {
  743.       int shift;
  744.       if (sizeof (ULONGEST) * HOST_CHAR_BIT
  745.           < gdbarch_long_long_bit (parse_gdbarch (par_state)))
  746.         /* A long long does not fit in a LONGEST.  */
  747.         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  748.       else
  749.         shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
  750.       high_bit = (ULONGEST) 1 << shift;
  751.       unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
  752.       signed_type = parse_type (par_state)->builtin_long_long;
  753.     }

  754.    putithere->typed_val_int.val = n;

  755.    /* If the high bit of the worked out type is set then this number
  756.       has to be unsigned.  */

  757.    if (unsigned_p || (n & high_bit))
  758.      {
  759.        putithere->typed_val_int.type = unsigned_type;
  760.      }
  761.    else
  762.      {
  763.        putithere->typed_val_int.type = signed_type;
  764.      }

  765.    return INT;
  766. }

  767. /* Temporary obstack used for holding strings.  */
  768. static struct obstack tempbuf;
  769. static int tempbuf_init;

  770. /* Parse a string or character literal from TOKPTR.  The string or
  771.    character may be wide or unicode.  *OUTPTR is set to just after the
  772.    end of the literal in the input string.  The resulting token is
  773.    stored in VALUE.  This returns a token value, either STRING or
  774.    CHAR, depending on what was parsed.  *HOST_CHARS is set to the
  775.    number of host characters in the literal.  */

  776. static int
  777. parse_string_or_char (const char *tokptr, const char **outptr,
  778.                       struct typed_stoken *value, int *host_chars)
  779. {
  780.   int quote;

  781.   /* Build the gdb internal form of the input string in tempbuf.  Note
  782.      that the buffer is null byte terminated *only* for the
  783.      convenience of debugging gdb itself and printing the buffer
  784.      contents when the buffer contains no embedded nulls.  Gdb does
  785.      not depend upon the buffer being null byte terminated, it uses
  786.      the length string instead.  This allows gdb to handle C strings
  787.      (as well as strings in other languages) with embedded null
  788.      bytes */

  789.   if (!tempbuf_init)
  790.     tempbuf_init = 1;
  791.   else
  792.     obstack_free (&tempbuf, NULL);
  793.   obstack_init (&tempbuf);

  794.   /* Skip the quote.  */
  795.   quote = *tokptr;
  796.   ++tokptr;

  797.   *host_chars = 0;

  798.   while (*tokptr)
  799.     {
  800.       char c = *tokptr;
  801.       if (c == '\\')
  802.         {
  803.           ++tokptr;
  804.           *host_chars += c_parse_escape (&tokptr, &tempbuf);
  805.         }
  806.       else if (c == quote)
  807.         break;
  808.       else
  809.         {
  810.           obstack_1grow (&tempbuf, c);
  811.           ++tokptr;
  812.           /* FIXME: this does the wrong thing with multi-byte host
  813.              characters.  We could use mbrlen here, but that would
  814.              make "set host-charset" a bit less useful.  */
  815.           ++*host_chars;
  816.         }
  817.     }

  818.   if (*tokptr != quote)
  819.     {
  820.       if (quote == '"')
  821.         error (_("Unterminated string in expression."));
  822.       else
  823.         error (_("Unmatched single quote."));
  824.     }
  825.   ++tokptr;

  826.   value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/
  827.   value->ptr = obstack_base (&tempbuf);
  828.   value->length = obstack_object_size (&tempbuf);

  829.   *outptr = tokptr;

  830.   return quote == '\'' ? CHAR : STRING;
  831. }

  832. struct token
  833. {
  834.   char *operator;
  835.   int token;
  836.   enum exp_opcode opcode;
  837. };

  838. static const struct token tokentab3[] =
  839.   {
  840.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  841.     {"<<=", ASSIGN_MODIFY, BINOP_LSH},
  842.     /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */
  843.     {"...", DOTDOTDOT, OP_NULL},
  844.   };

  845. static const struct token tokentab2[] =
  846.   {
  847.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  848.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  849.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  850.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  851.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  852.     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
  853.     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
  854.     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
  855.     {"++", INCREMENT, BINOP_END},
  856.     {"--", DECREMENT, BINOP_END},
  857.     /*{"->", RIGHT_ARROW, BINOP_END}, Doesn't exist in Go.  */
  858.     {"<-", LEFT_ARROW, BINOP_END},
  859.     {"&&", ANDAND, BINOP_END},
  860.     {"||", OROR, BINOP_END},
  861.     {"<<", LSH, BINOP_END},
  862.     {">>", RSH, BINOP_END},
  863.     {"==", EQUAL, BINOP_END},
  864.     {"!=", NOTEQUAL, BINOP_END},
  865.     {"<=", LEQ, BINOP_END},
  866.     {">=", GEQ, BINOP_END},
  867.     /*{"&^", ANDNOT, BINOP_END}, TODO */
  868.   };

  869. /* Identifier-like tokens.  */
  870. static const struct token ident_tokens[] =
  871.   {
  872.     {"true", TRUE_KEYWORD, OP_NULL},
  873.     {"false", FALSE_KEYWORD, OP_NULL},
  874.     {"nil", NIL_KEYWORD, OP_NULL},
  875.     {"const", CONST_KEYWORD, OP_NULL},
  876.     {"struct", STRUCT_KEYWORD, OP_NULL},
  877.     {"type", TYPE_KEYWORD, OP_NULL},
  878.     {"interface", INTERFACE_KEYWORD, OP_NULL},
  879.     {"chan", CHAN_KEYWORD, OP_NULL},
  880.     {"byte", BYTE_KEYWORD, OP_NULL}, /* An alias of uint8.  */
  881.     {"len", LEN_KEYWORD, OP_NULL},
  882.     {"cap", CAP_KEYWORD, OP_NULL},
  883.     {"new", NEW_KEYWORD, OP_NULL},
  884.     {"iota", IOTA_KEYWORD, OP_NULL},
  885.   };

  886. /* This is set if a NAME token appeared at the very end of the input
  887.    string, with no whitespace separating the name from the EOF.  This
  888.    is used only when parsing to do field name completion.  */
  889. static int saw_name_at_eof;

  890. /* This is set if the previously-returned token was a structure
  891.    operator -- either '.' or ARROW.  This is used only when parsing to
  892.    do field name completion.  */
  893. static int last_was_structop;

  894. /* Read one token, getting characters through lexptr.  */

  895. static int
  896. lex_one_token (struct parser_state *par_state)
  897. {
  898.   int c;
  899.   int namelen;
  900.   unsigned int i;
  901.   const char *tokstart;
  902.   int saw_structop = last_was_structop;
  903.   char *copy;

  904.   last_was_structop = 0;

  905. retry:

  906.   prev_lexptr = lexptr;

  907.   tokstart = lexptr;
  908.   /* See if it is a special token of length 3.  */
  909.   for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
  910.     if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
  911.       {
  912.         lexptr += 3;
  913.         yylval.opcode = tokentab3[i].opcode;
  914.         return tokentab3[i].token;
  915.       }

  916.   /* See if it is a special token of length 2.  */
  917.   for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
  918.     if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
  919.       {
  920.         lexptr += 2;
  921.         yylval.opcode = tokentab2[i].opcode;
  922.         /* NOTE: -> doesn't exist in Go, so we don't need to watch for
  923.            setting last_was_structop here.  */
  924.         return tokentab2[i].token;
  925.       }

  926.   switch (c = *tokstart)
  927.     {
  928.     case 0:
  929.       if (saw_name_at_eof)
  930.         {
  931.           saw_name_at_eof = 0;
  932.           return COMPLETE;
  933.         }
  934.       else if (saw_structop)
  935.         return COMPLETE;
  936.       else
  937.         return 0;

  938.     case ' ':
  939.     case '\t':
  940.     case '\n':
  941.       lexptr++;
  942.       goto retry;

  943.     case '[':
  944.     case '(':
  945.       paren_depth++;
  946.       lexptr++;
  947.       return c;

  948.     case ']':
  949.     case ')':
  950.       if (paren_depth == 0)
  951.         return 0;
  952.       paren_depth--;
  953.       lexptr++;
  954.       return c;

  955.     case ',':
  956.       if (comma_terminates
  957.           && paren_depth == 0)
  958.         return 0;
  959.       lexptr++;
  960.       return c;

  961.     case '.':
  962.       /* Might be a floating point number.  */
  963.       if (lexptr[1] < '0' || lexptr[1] > '9')
  964.         {
  965.           if (parse_completion)
  966.             last_was_structop = 1;
  967.           goto symbol;                /* Nope, must be a symbol. */
  968.         }
  969.       /* FALL THRU into number case.  */

  970.     case '0':
  971.     case '1':
  972.     case '2':
  973.     case '3':
  974.     case '4':
  975.     case '5':
  976.     case '6':
  977.     case '7':
  978.     case '8':
  979.     case '9':
  980.       {
  981.         /* It's a number.  */
  982.         int got_dot = 0, got_e = 0, toktype;
  983.         const char *p = tokstart;
  984.         int hex = input_radix > 10;

  985.         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  986.           {
  987.             p += 2;
  988.             hex = 1;
  989.           }

  990.         for (;; ++p)
  991.           {
  992.             /* This test includes !hex because 'e' is a valid hex digit
  993.                and thus does not indicate a floating point number when
  994.                the radix is hex.  */
  995.             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  996.               got_dot = got_e = 1;
  997.             /* This test does not include !hex, because a '.' always indicates
  998.                a decimal floating point number regardless of the radix.  */
  999.             else if (!got_dot && *p == '.')
  1000.               got_dot = 1;
  1001.             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1002.                      && (*p == '-' || *p == '+'))
  1003.               /* This is the sign of the exponent, not the end of the
  1004.                  number.  */
  1005.               continue;
  1006.             /* We will take any letters or digits.  parse_number will
  1007.                complain if past the radix, or if L or U are not final.  */
  1008.             else if ((*p < '0' || *p > '9')
  1009.                      && ((*p < 'a' || *p > 'z')
  1010.                                   && (*p < 'A' || *p > 'Z')))
  1011.               break;
  1012.           }
  1013.         toktype = parse_number (par_state, tokstart, p - tokstart,
  1014.                                 got_dot|got_e, &yylval);
  1015.         if (toktype == ERROR)
  1016.           {
  1017.             char *err_copy = (char *) alloca (p - tokstart + 1);

  1018.             memcpy (err_copy, tokstart, p - tokstart);
  1019.             err_copy[p - tokstart] = 0;
  1020.             error (_("Invalid number \"%s\"."), err_copy);
  1021.           }
  1022.         lexptr = p;
  1023.         return toktype;
  1024.       }

  1025.     case '@':
  1026.       {
  1027.         const char *p = &tokstart[1];
  1028.         size_t len = strlen ("entry");

  1029.         while (isspace (*p))
  1030.           p++;
  1031.         if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
  1032.             && p[len] != '_')
  1033.           {
  1034.             lexptr = &p[len];
  1035.             return ENTRY;
  1036.           }
  1037.       }
  1038.       /* FALLTHRU */
  1039.     case '+':
  1040.     case '-':
  1041.     case '*':
  1042.     case '/':
  1043.     case '%':
  1044.     case '|':
  1045.     case '&':
  1046.     case '^':
  1047.     case '~':
  1048.     case '!':
  1049.     case '<':
  1050.     case '>':
  1051.     case '?':
  1052.     case ':':
  1053.     case '=':
  1054.     case '{':
  1055.     case '}':
  1056.     symbol:
  1057.       lexptr++;
  1058.       return c;

  1059.     case '\'':
  1060.     case '"':
  1061.     case '`':
  1062.       {
  1063.         int host_len;
  1064.         int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
  1065.                                            &host_len);
  1066.         if (result == CHAR)
  1067.           {
  1068.             if (host_len == 0)
  1069.               error (_("Empty character constant."));
  1070.             else if (host_len > 2 && c == '\'')
  1071.               {
  1072.                 ++tokstart;
  1073.                 namelen = lexptr - tokstart - 1;
  1074.                 goto tryname;
  1075.               }
  1076.             else if (host_len > 1)
  1077.               error (_("Invalid character constant."));
  1078.           }
  1079.         return result;
  1080.       }
  1081.     }

  1082.   if (!(c == '_' || c == '$'
  1083.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1084.     /* We must have come across a bad character (e.g. ';').  */
  1085.     error (_("Invalid character '%c' in expression."), c);

  1086.   /* It's a name.  See how long it is.  */
  1087.   namelen = 0;
  1088.   for (c = tokstart[namelen];
  1089.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1090.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
  1091.     {
  1092.       c = tokstart[++namelen];
  1093.     }

  1094.   /* The token "if" terminates the expression and is NOT removed from
  1095.      the input stream.  It doesn't count if it appears in the
  1096.      expansion of a macro.  */
  1097.   if (namelen == 2
  1098.       && tokstart[0] == 'i'
  1099.       && tokstart[1] == 'f')
  1100.     {
  1101.       return 0;
  1102.     }

  1103.   /* For the same reason (breakpoint conditions), "thread N"
  1104.      terminates the expression.  "thread" could be an identifier, but
  1105.      an identifier is never followed by a number without intervening
  1106.      punctuation.
  1107.      Handle abbreviations of these, similarly to
  1108.      breakpoint.c:find_condition_and_thread.
  1109.      TODO: Watch for "goroutine" here?  */
  1110.   if (namelen >= 1
  1111.       && strncmp (tokstart, "thread", namelen) == 0
  1112.       && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
  1113.     {
  1114.       const char *p = tokstart + namelen + 1;

  1115.       while (*p == ' ' || *p == '\t')
  1116.         p++;
  1117.       if (*p >= '0' && *p <= '9')
  1118.         return 0;
  1119.     }

  1120.   lexptr += namelen;

  1121.   tryname:

  1122.   yylval.sval.ptr = tokstart;
  1123.   yylval.sval.length = namelen;

  1124.   /* Catch specific keywords.  */
  1125.   copy = copy_name (yylval.sval);
  1126.   for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++)
  1127.     if (strcmp (copy, ident_tokens[i].operator) == 0)
  1128.       {
  1129.         /* It is ok to always set this, even though we don't always
  1130.            strictly need to.  */
  1131.         yylval.opcode = ident_tokens[i].opcode;
  1132.         return ident_tokens[i].token;
  1133.       }

  1134.   if (*tokstart == '$')
  1135.     return DOLLAR_VARIABLE;

  1136.   if (parse_completion && *lexptr == '\0')
  1137.     saw_name_at_eof = 1;
  1138.   return NAME;
  1139. }

  1140. /* An object of this type is pushed on a FIFO by the "outer" lexer.  */
  1141. typedef struct
  1142. {
  1143.   int token;
  1144.   YYSTYPE value;
  1145. } token_and_value;

  1146. DEF_VEC_O (token_and_value);

  1147. /* A FIFO of tokens that have been read but not yet returned to the
  1148.    parser.  */
  1149. static VEC (token_and_value) *token_fifo;

  1150. /* Non-zero if the lexer should return tokens from the FIFO.  */
  1151. static int popping;

  1152. /* Temporary storage for yylex; this holds symbol names as they are
  1153.    built up.  */
  1154. static struct obstack name_obstack;

  1155. /* Build "package.name" in name_obstack.
  1156.    For convenience of the caller, the name is NUL-terminated,
  1157.    but the NUL is not included in the recorded length.  */

  1158. static struct stoken
  1159. build_packaged_name (const char *package, int package_len,
  1160.                      const char *name, int name_len)
  1161. {
  1162.   struct stoken result;

  1163.   obstack_free (&name_obstack, obstack_base (&name_obstack));
  1164.   obstack_grow (&name_obstack, package, package_len);
  1165.   obstack_grow_str (&name_obstack, ".");
  1166.   obstack_grow (&name_obstack, name, name_len);
  1167.   obstack_grow (&name_obstack, "", 1);
  1168.   result.ptr = obstack_base (&name_obstack);
  1169.   result.length = obstack_object_size (&name_obstack) - 1;

  1170.   return result;
  1171. }

  1172. /* Return non-zero if NAME is a package name.
  1173.    BLOCK is the scope in which to interpret NAME; this can be NULL
  1174.    to mean the global scope.  */

  1175. static int
  1176. package_name_p (const char *name, const struct block *block)
  1177. {
  1178.   struct symbol *sym;
  1179.   struct field_of_this_result is_a_field_of_this;

  1180.   sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this);

  1181.   if (sym
  1182.       && SYMBOL_CLASS (sym) == LOC_TYPEDEF
  1183.       && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE)
  1184.     return 1;

  1185.   return 0;
  1186. }

  1187. /* Classify a (potential) function in the "unsafe" package.
  1188.    We fold these into "keywords" to keep things simple, at least until
  1189.    something more complex is warranted.  */

  1190. static int
  1191. classify_unsafe_function (struct stoken function_name)
  1192. {
  1193.   char *copy = copy_name (function_name);

  1194.   if (strcmp (copy, "Sizeof") == 0)
  1195.     {
  1196.       yylval.sval = function_name;
  1197.       return SIZEOF_KEYWORD;
  1198.     }

  1199.   error (_("Unknown function in `unsafe' package: %s"), copy);
  1200. }

  1201. /* Classify token(s) "name1.name2" where name1 is known to be a package.
  1202.    The contents of the token are in `yylval'.
  1203.    Updates yylval and returns the new token type.

  1204.    The result is one of NAME, NAME_OR_INT, or TYPENAME.  */

  1205. static int
  1206. classify_packaged_name (const struct block *block)
  1207. {
  1208.   char *copy;
  1209.   struct symbol *sym;
  1210.   struct field_of_this_result is_a_field_of_this;

  1211.   copy = copy_name (yylval.sval);

  1212.   sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);

  1213.   if (sym)
  1214.     {
  1215.       yylval.ssym.sym = sym;
  1216.       yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1217.     }

  1218.   return NAME;
  1219. }

  1220. /* Classify a NAME token.
  1221.    The contents of the token are in `yylval'.
  1222.    Updates yylval and returns the new token type.
  1223.    BLOCK is the block in which lookups start; this can be NULL
  1224.    to mean the global scope.

  1225.    The result is one of NAME, NAME_OR_INT, or TYPENAME.  */

  1226. static int
  1227. classify_name (struct parser_state *par_state, const struct block *block)
  1228. {
  1229.   struct type *type;
  1230.   struct symbol *sym;
  1231.   char *copy;
  1232.   struct field_of_this_result is_a_field_of_this;

  1233.   copy = copy_name (yylval.sval);

  1234.   /* Try primitive types first so they win over bad/weird debug info.  */
  1235.   type = language_lookup_primitive_type (parse_language (par_state),
  1236.                                          parse_gdbarch (par_state),
  1237.                                          copy);
  1238.   if (type != NULL)
  1239.     {
  1240.       /* NOTE: We take advantage of the fact that yylval coming in was a
  1241.          NAME, and that struct ttype is a compatible extension of struct
  1242.          stoken, so yylval.tsym.stoken is already filled in.  */
  1243.       yylval.tsym.type = type;
  1244.       return TYPENAME;
  1245.     }

  1246.   /* TODO: What about other types?  */

  1247.   sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);

  1248.   if (sym)
  1249.     {
  1250.       yylval.ssym.sym = sym;
  1251.       yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1252.       return NAME;
  1253.     }

  1254.   /* If we didn't find a symbol, look again in the current package.
  1255.      This is to, e.g., make "p global_var" work without having to specify
  1256.      the package name.  We intentionally only looks for objects in the
  1257.      current package.  */

  1258.   {
  1259.     char *current_package_name = go_block_package_name (block);

  1260.     if (current_package_name != NULL)
  1261.       {
  1262.         struct stoken sval =
  1263.           build_packaged_name (current_package_name,
  1264.                                strlen (current_package_name),
  1265.                                copy, strlen (copy));

  1266.         xfree (current_package_name);
  1267.         sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN,
  1268.                              &is_a_field_of_this);
  1269.         if (sym)
  1270.           {
  1271.             yylval.ssym.stoken = sval;
  1272.             yylval.ssym.sym = sym;
  1273.             yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1274.             return NAME;
  1275.           }
  1276.       }
  1277.   }

  1278.   /* Input names that aren't symbols but ARE valid hex numbers, when
  1279.      the input radix permits them, can be names or numbers depending
  1280.      on the parse.  Note we support radixes > 16 here.  */
  1281.   if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
  1282.       || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10))
  1283.     {
  1284.       YYSTYPE newlval;        /* Its value is ignored.  */
  1285.       int hextype = parse_number (par_state, copy, yylval.sval.length,
  1286.                                   0, &newlval);
  1287.       if (hextype == INT)
  1288.         {
  1289.           yylval.ssym.sym = NULL;
  1290.           yylval.ssym.is_a_field_of_this = 0;
  1291.           return NAME_OR_INT;
  1292.         }
  1293.     }

  1294.   yylval.ssym.sym = NULL;
  1295.   yylval.ssym.is_a_field_of_this = 0;
  1296.   return NAME;
  1297. }

  1298. /* This is taken from c-exp.y mostly to get something working.
  1299.    The basic structure has been kept because we may yet need some of it.  */

  1300. static int
  1301. yylex (void)
  1302. {
  1303.   token_and_value current, next;

  1304.   if (popping && !VEC_empty (token_and_value, token_fifo))
  1305.     {
  1306.       token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
  1307.       VEC_ordered_remove (token_and_value, token_fifo, 0);
  1308.       yylval = tv.value;
  1309.       /* There's no need to fall through to handle package.name
  1310.          as that can never happen here.  In theory.  */
  1311.       return tv.token;
  1312.     }
  1313.   popping = 0;

  1314.   current.token = lex_one_token (pstate);

  1315.   /* TODO: Need a way to force specifying name1 as a package.
  1316.      .name1.name2 ?  */

  1317.   if (current.token != NAME)
  1318.     return current.token;

  1319.   /* See if we have "name1 . name2".  */

  1320.   current.value = yylval;
  1321.   next.token = lex_one_token (pstate);
  1322.   next.value = yylval;

  1323.   if (next.token == '.')
  1324.     {
  1325.       token_and_value name2;

  1326.       name2.token = lex_one_token (pstate);
  1327.       name2.value = yylval;

  1328.       if (name2.token == NAME)
  1329.         {
  1330.           /* Ok, we have "name1 . name2".  */
  1331.           char *copy;

  1332.           copy = copy_name (current.value.sval);

  1333.           if (strcmp (copy, "unsafe") == 0)
  1334.             {
  1335.               popping = 1;
  1336.               return classify_unsafe_function (name2.value.sval);
  1337.             }

  1338.           if (package_name_p (copy, expression_context_block))
  1339.             {
  1340.               popping = 1;
  1341.               yylval.sval = build_packaged_name (current.value.sval.ptr,
  1342.                                                  current.value.sval.length,
  1343.                                                  name2.value.sval.ptr,
  1344.                                                  name2.value.sval.length);
  1345.               return classify_packaged_name (expression_context_block);
  1346.             }
  1347.         }

  1348.       VEC_safe_push (token_and_value, token_fifo, &next);
  1349.       VEC_safe_push (token_and_value, token_fifo, &name2);
  1350.     }
  1351.   else
  1352.     {
  1353.       VEC_safe_push (token_and_value, token_fifo, &next);
  1354.     }

  1355.   /* If we arrive here we don't have a package-qualified name.  */

  1356.   popping = 1;
  1357.   yylval = current.value;
  1358.   return classify_name (pstate, expression_context_block);
  1359. }

  1360. int
  1361. go_parse (struct parser_state *par_state)
  1362. {
  1363.   int result;
  1364.   struct cleanup *back_to;

  1365.   /* Setting up the parser state.  */
  1366.   gdb_assert (par_state != NULL);
  1367.   pstate = par_state;

  1368.   back_to = make_cleanup (null_cleanup, NULL);

  1369.   make_cleanup_restore_integer (&yydebug);
  1370.   make_cleanup_clear_parser_state (&pstate);
  1371.   yydebug = parser_debug;

  1372.   /* Initialize some state used by the lexer.  */
  1373.   last_was_structop = 0;
  1374.   saw_name_at_eof = 0;

  1375.   VEC_free (token_and_value, token_fifo);
  1376.   popping = 0;
  1377.   obstack_init (&name_obstack);
  1378.   make_cleanup_obstack_free (&name_obstack);

  1379.   result = yyparse ();
  1380.   do_cleanups (back_to);
  1381.   return result;
  1382. }

  1383. void
  1384. yyerror (char *msg)
  1385. {
  1386.   if (prev_lexptr)
  1387.     lexptr = prev_lexptr;

  1388.   error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
  1389. }