gdb/jv-exp.y - gdb

  1. /* YACC parser for Java expressions, for GDB.
  2.    Copyright (C) 1997-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. /* Parse a Java expression from text in a string,
  15.    and return the result as a  struct expression  pointer.
  16.    That structure contains arithmetic operations in reverse polish,
  17.    with constants represented by operations that are followed by special data.
  18.    See expression.h for the details of the format.
  19.    What is important here is that it can be built up sequentially
  20.    during the process of parsing; the lower levels of the tree always
  21.    come first in the result.  Well, almost always; see ArrayAccess.

  22.    Note that malloc's and realloc's in this file are transformed to
  23.    xmalloc and xrealloc respectively by the same sed command in the
  24.    makefile that remaps any other malloc/realloc inserted by the parser
  25.    generator.  Doing this with #defines and trying to control the interaction
  26.    with include files (<malloc.h> and <stdlib.h> for example) just became
  27.    too messy, particularly when such includes can be inserted at random
  28.    times by the parser generator.  */

  29. %{

  30. #include "defs.h"
  31. #include <ctype.h>
  32. #include "expression.h"
  33. #include "value.h"
  34. #include "parser-defs.h"
  35. #include "language.h"
  36. #include "jv-lang.h"
  37. #include "bfd.h" /* Required by objfiles.h.  */
  38. #include "symfile.h" /* Required by objfiles.h.  */
  39. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  40. #include "block.h"
  41. #include "completer.h"

  42. #define parse_type(ps) builtin_type (parse_gdbarch (ps))
  43. #define parse_java_type(ps) builtin_java_type (parse_gdbarch (ps))

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

  50. #define        yymaxdepth java_maxdepth
  51. #define        yyparse        java_parse_internal
  52. #define        yylex        java_lex
  53. #define        yyerror        java_error
  54. #define        yylval        java_lval
  55. #define        yychar        java_char
  56. #define        yydebug        java_debug
  57. #define        yypact        java_pact
  58. #define        yyr1        java_r1
  59. #define        yyr2        java_r2
  60. #define        yydef        java_def
  61. #define        yychk        java_chk
  62. #define        yypgo        java_pgo
  63. #define        yyact        java_act
  64. #define        yyexca        java_exca
  65. #define yyerrflag java_errflag
  66. #define yynerrs        java_nerrs
  67. #define        yyps        java_ps
  68. #define        yypv        java_pv
  69. #define        yys        java_s
  70. #define        yy_yys        java_yys
  71. #define        yystate        java_state
  72. #define        yytmp        java_tmp
  73. #define        yyv        java_v
  74. #define        yy_yyv        java_yyv
  75. #define        yyval        java_val
  76. #define        yylloc        java_lloc
  77. #define yyreds        java_reds                /* With YYDEBUG defined */
  78. #define yytoks        java_toks                /* With YYDEBUG defined */
  79. #define yyname        java_name                /* With YYDEBUG defined */
  80. #define yyrule        java_rule                /* With YYDEBUG defined */
  81. #define yylhs        java_yylhs
  82. #define yylen        java_yylen
  83. #define yydefred java_yydefred
  84. #define yydgoto        java_yydgoto
  85. #define yysindex java_yysindex
  86. #define yyrindex java_yyrindex
  87. #define yygindex java_yygindex
  88. #define yytable         java_yytable
  89. #define yycheck         java_yycheck
  90. #define yyss        java_yyss
  91. #define yysslim        java_yysslim
  92. #define yyssp        java_yyssp
  93. #define yystacksize java_yystacksize
  94. #define yyvs        java_yyvs
  95. #define yyvsp        java_yyvsp

  96. #ifndef YYDEBUG
  97. #define        YYDEBUG 1                /* Default to yydebug support */
  98. #endif

  99. #define YYFPRINTF parser_fprintf

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

  102. static struct parser_state *pstate = NULL;

  103. int yyparse (void);

  104. static int yylex (void);

  105. void yyerror (char *);

  106. static struct type *java_type_from_name (struct stoken);
  107. static void push_expression_name (struct parser_state *, struct stoken);
  108. static void push_fieldnames (struct parser_state *, struct stoken);

  109. static struct expression *copy_exp (struct expression *, int);
  110. static void insert_exp (struct parser_state *, int, struct expression *);

  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 symbol *sym;
  127.     struct type *tval;
  128.     struct stoken sval;
  129.     struct ttype tsym;
  130.     struct symtoken ssym;
  131.     struct block *bval;
  132.     enum exp_opcode opcode;
  133.     struct internalvar *ivar;
  134.     int *ivec;
  135.   }

  136. %{
  137. /* YYSTYPE gets defined by %union */
  138. static int parse_number (struct parser_state *, const char *, int,
  139.                          int, YYSTYPE *);
  140. %}

  141. %type <lval> rcurly Dims Dims_opt
  142. %type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
  143. %type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType

  144. %token <typed_val_int> INTEGER_LITERAL
  145. %token <typed_val_float> FLOATING_POINT_LITERAL

  146. %token <sval> IDENTIFIER
  147. %token <sval> STRING_LITERAL
  148. %token <lval> BOOLEAN_LITERAL
  149. %token <tsym> TYPENAME
  150. %type <sval> Name SimpleName QualifiedName ForcedName

  151. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  152.    but which would parse as a valid number in the current input radix.
  153.    E.g. "c" when input_radix==16.  Depending on the parse, it will be
  154.    turned into a name or into a number.  */

  155. %token <sval> NAME_OR_INT

  156. %token ERROR

  157. /* Special type cases, put in to allow the parser to distinguish different
  158.    legal basetypes.  */
  159. %token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT

  160. %token VARIABLE

  161. %token <opcode> ASSIGN_MODIFY

  162. %token SUPER NEW

  163. %left ','
  164. %right '=' ASSIGN_MODIFY
  165. %right '?'
  166. %left OROR
  167. %left ANDAND
  168. %left '|'
  169. %left '^'
  170. %left '&'
  171. %left EQUAL NOTEQUAL
  172. %left '<' '>' LEQ GEQ
  173. %left LSH RSH
  174. %left '+' '-'
  175. %left '*' '/' '%'
  176. %right INCREMENT DECREMENT
  177. %right '.' '[' '('


  178. %%

  179. start   :        exp1
  180.         |        type_exp
  181.         ;

  182. type_exp:        PrimitiveOrArrayType
  183.                 {
  184.                   write_exp_elt_opcode (pstate, OP_TYPE);
  185.                   write_exp_elt_type (pstate, $1);
  186.                   write_exp_elt_opcode (pstate, OP_TYPE);
  187.                 }
  188.         ;

  189. PrimitiveOrArrayType:
  190.                 PrimitiveType
  191.         |        ArrayType
  192.         ;

  193. StringLiteral:
  194.         STRING_LITERAL
  195.                 {
  196.                   write_exp_elt_opcode (pstate, OP_STRING);
  197.                   write_exp_string (pstate, $1);
  198.                   write_exp_elt_opcode (pstate, OP_STRING);
  199.                 }
  200. ;

  201. Literal:
  202.         INTEGER_LITERAL
  203.                 { write_exp_elt_opcode (pstate, OP_LONG);
  204.                   write_exp_elt_type (pstate, $1.type);
  205.                   write_exp_elt_longcst (pstate, (LONGEST)($1.val));
  206.                   write_exp_elt_opcode (pstate, OP_LONG); }
  207. |        NAME_OR_INT
  208.                 { YYSTYPE val;
  209.                   parse_number (pstate, $1.ptr, $1.length, 0, &val);
  210.                   write_exp_elt_opcode (pstate, OP_LONG);
  211.                   write_exp_elt_type (pstate, val.typed_val_int.type);
  212.                   write_exp_elt_longcst (pstate,
  213.                                          (LONGEST) val.typed_val_int.val);
  214.                   write_exp_elt_opcode (pstate, OP_LONG);
  215.                 }
  216. |        FLOATING_POINT_LITERAL
  217.                 { write_exp_elt_opcode (pstate, OP_DOUBLE);
  218.                   write_exp_elt_type (pstate, $1.type);
  219.                   write_exp_elt_dblcst (pstate, $1.dval);
  220.                   write_exp_elt_opcode (pstate, OP_DOUBLE); }
  221. |        BOOLEAN_LITERAL
  222.                 { write_exp_elt_opcode (pstate, OP_LONG);
  223.                   write_exp_elt_type (pstate,
  224.                                   parse_java_type (pstate)->builtin_boolean);
  225.                   write_exp_elt_longcst (pstate, (LONGEST)$1);
  226.                   write_exp_elt_opcode (pstate, OP_LONG); }
  227. |        StringLiteral
  228.         ;

  229. /* UNUSED:
  230. Type:
  231.         PrimitiveType
  232. |        ReferenceType
  233. ;
  234. */

  235. PrimitiveType:
  236.         NumericType
  237. |        BOOLEAN
  238.                 { $$ = parse_java_type (pstate)->builtin_boolean; }
  239. ;

  240. NumericType:
  241.         IntegralType
  242. |        FloatingPointType
  243. ;

  244. IntegralType:
  245.         BYTE
  246.                 { $$ = parse_java_type (pstate)->builtin_byte; }
  247. |        SHORT
  248.                 { $$ = parse_java_type (pstate)->builtin_short; }
  249. |        INT
  250.                 { $$ = parse_java_type (pstate)->builtin_int; }
  251. |        LONG
  252.                 { $$ = parse_java_type (pstate)->builtin_long; }
  253. |        CHAR
  254.                 { $$ = parse_java_type (pstate)->builtin_char; }
  255. ;

  256. FloatingPointType:
  257.         FLOAT
  258.                 { $$ = parse_java_type (pstate)->builtin_float; }
  259. |        DOUBLE
  260.                 { $$ = parse_java_type (pstate)->builtin_double; }
  261. ;

  262. /* UNUSED:
  263. ReferenceType:
  264.         ClassOrInterfaceType
  265. |        ArrayType
  266. ;
  267. */

  268. ClassOrInterfaceType:
  269.         Name
  270.                 { $$ = java_type_from_name ($1); }
  271. ;

  272. ClassType:
  273.         ClassOrInterfaceType
  274. ;

  275. ArrayType:
  276.         PrimitiveType Dims
  277.                 { $$ = java_array_type ($1, $2); }
  278. |        Name Dims
  279.                 { $$ = java_array_type (java_type_from_name ($1), $2); }
  280. ;

  281. Name:
  282.         IDENTIFIER
  283. |        QualifiedName
  284. ;

  285. ForcedName:
  286.         SimpleName
  287. |        QualifiedName
  288. ;

  289. SimpleName:
  290.         IDENTIFIER
  291. |        NAME_OR_INT
  292. ;

  293. QualifiedName:
  294.         Name '.' SimpleName
  295.                 { $$.length = $1.length + $3.length + 1;
  296.                   if ($1.ptr + $1.length + 1 == $3.ptr
  297.                       && $1.ptr[$1.length] == '.')
  298.                     $$.ptr = $1.ptr;  /* Optimization.  */
  299.                   else
  300.                     {
  301.                       char *buf;

  302.                       buf = malloc ($$.length + 1);
  303.                       make_cleanup (free, buf);
  304.                       sprintf (buf, "%.*s.%.*s",
  305.                                $1.length, $1.ptr, $3.length, $3.ptr);
  306.                       $$.ptr = buf;
  307.                 } }
  308. ;

  309. /*
  310. type_exp:        type
  311.                         { write_exp_elt_opcode(OP_TYPE);
  312.                           write_exp_elt_type($1);
  313.                           write_exp_elt_opcode(OP_TYPE);}
  314.         ;
  315.         */

  316. /* Expressions, including the comma operator.  */
  317. exp1        :        Expression
  318.         |        exp1 ',' Expression
  319.                         { write_exp_elt_opcode (pstate, BINOP_COMMA); }
  320.         ;

  321. Primary:
  322.         PrimaryNoNewArray
  323. |        ArrayCreationExpression
  324. ;

  325. PrimaryNoNewArray:
  326.         Literal
  327. |        '(' Expression ')'
  328. |        ClassInstanceCreationExpression
  329. |        FieldAccess
  330. |        MethodInvocation
  331. |        ArrayAccess
  332. |        lcurly ArgumentList rcurly
  333.                 { write_exp_elt_opcode (pstate, OP_ARRAY);
  334.                   write_exp_elt_longcst (pstate, (LONGEST) 0);
  335.                   write_exp_elt_longcst (pstate, (LONGEST) $3);
  336.                   write_exp_elt_opcode (pstate, OP_ARRAY); }
  337. ;

  338. lcurly:
  339.         '{'
  340.                 { start_arglist (); }
  341. ;

  342. rcurly:
  343.         '}'
  344.                 { $$ = end_arglist () - 1; }
  345. ;

  346. ClassInstanceCreationExpression:
  347.         NEW ClassType '(' ArgumentList_opt ')'
  348.                 { internal_error (__FILE__, __LINE__,
  349.                                   _("FIXME - ClassInstanceCreationExpression")); }
  350. ;

  351. ArgumentList:
  352.         Expression
  353.                 { arglist_len = 1; }
  354. |        ArgumentList ',' Expression
  355.                 { arglist_len++; }
  356. ;

  357. ArgumentList_opt:
  358.         /* EMPTY */
  359.                 { arglist_len = 0; }
  360. | ArgumentList
  361. ;

  362. ArrayCreationExpression:
  363.         NEW PrimitiveType DimExprs Dims_opt
  364.                 { internal_error (__FILE__, __LINE__,
  365.                                   _("FIXME - ArrayCreationExpression")); }
  366. |        NEW ClassOrInterfaceType DimExprs Dims_opt
  367.                 { internal_error (__FILE__, __LINE__,
  368.                                   _("FIXME - ArrayCreationExpression")); }
  369. ;

  370. DimExprs:
  371.         DimExpr
  372. |        DimExprs DimExpr
  373. ;

  374. DimExpr:
  375.         '[' Expression ']'
  376. ;

  377. Dims:
  378.         '[' ']'
  379.                 { $$ = 1; }
  380. |        Dims '[' ']'
  381.         { $$ = $1 + 1; }
  382. ;

  383. Dims_opt:
  384.         Dims
  385. |        /* EMPTY */
  386.                 { $$ = 0; }
  387. ;

  388. FieldAccess:
  389.         Primary '.' SimpleName
  390.                 { push_fieldnames (pstate, $3); }
  391. |        VARIABLE '.' SimpleName
  392.                 { push_fieldnames (pstate, $3); }
  393. /*|        SUPER '.' SimpleName { FIXME } */
  394. ;

  395. FuncStart:
  396.         Name '('
  397.                 { push_expression_name (pstate, $1); }
  398. ;

  399. MethodInvocation:
  400.         FuncStart
  401.                 { start_arglist(); }
  402.         ArgumentList_opt ')'
  403.                 { write_exp_elt_opcode (pstate, OP_FUNCALL);
  404.                   write_exp_elt_longcst (pstate, (LONGEST) end_arglist ());
  405.                   write_exp_elt_opcode (pstate, OP_FUNCALL); }
  406. |        Primary '.' SimpleName '(' ArgumentList_opt ')'
  407.                 { error (_("Form of method invocation not implemented")); }
  408. |        SUPER '.' SimpleName '(' ArgumentList_opt ')'
  409.                 { error (_("Form of method invocation not implemented")); }
  410. ;

  411. ArrayAccess:
  412.         Name '[' Expression ']'
  413.                 {
  414.                   /* Emit code for the Name now, then exchange it in the
  415.                      expout array with the Expression's code.  We could
  416.                      introduce a OP_SWAP code or a reversed version of
  417.                      BINOP_SUBSCRIPT, but that makes the rest of GDB pay
  418.                      for our parsing kludges.  */
  419.                   struct expression *name_expr;

  420.                   push_expression_name (pstate, $1);
  421.                   name_expr = copy_exp (pstate->expout, pstate->expout_ptr);
  422.                   pstate->expout_ptr -= name_expr->nelts;
  423.                   insert_exp (pstate,
  424.                               pstate->expout_ptr
  425.                               - length_of_subexp (pstate->expout,
  426.                                                   pstate->expout_ptr),
  427.                               name_expr);
  428.                   free (name_expr);
  429.                   write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
  430.                 }
  431. |        VARIABLE '[' Expression ']'
  432.                 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
  433. |        PrimaryNoNewArray '[' Expression ']'
  434.                 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
  435. ;

  436. PostfixExpression:
  437.         Primary
  438. |        Name
  439.                 { push_expression_name (pstate, $1); }
  440. |        VARIABLE
  441.                 /* Already written by write_dollar_variable.  */
  442. |        PostIncrementExpression
  443. |        PostDecrementExpression
  444. ;

  445. PostIncrementExpression:
  446.         PostfixExpression INCREMENT
  447.                 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
  448. ;

  449. PostDecrementExpression:
  450.         PostfixExpression DECREMENT
  451.                 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
  452. ;

  453. UnaryExpression:
  454.         PreIncrementExpression
  455. |        PreDecrementExpression
  456. |        '+' UnaryExpression
  457. |        '-' UnaryExpression
  458.                 { write_exp_elt_opcode (pstate, UNOP_NEG); }
  459. |        '*' UnaryExpression
  460.                 { write_exp_elt_opcode (pstate,
  461.                                         UNOP_IND); } /*FIXME not in Java  */
  462. |        UnaryExpressionNotPlusMinus
  463. ;

  464. PreIncrementExpression:
  465.         INCREMENT UnaryExpression
  466.                 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
  467. ;

  468. PreDecrementExpression:
  469.         DECREMENT UnaryExpression
  470.                 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
  471. ;

  472. UnaryExpressionNotPlusMinus:
  473.         PostfixExpression
  474. |        '~' UnaryExpression
  475.                 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
  476. |        '!' UnaryExpression
  477.                 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  478. |        CastExpression
  479.         ;

  480. CastExpression:
  481.         '(' PrimitiveType Dims_opt ')' UnaryExpression
  482.                 { write_exp_elt_opcode (pstate, UNOP_CAST);
  483.                   write_exp_elt_type (pstate, java_array_type ($2, $3));
  484.                   write_exp_elt_opcode (pstate, UNOP_CAST); }
  485. |        '(' Expression ')' UnaryExpressionNotPlusMinus
  486.                 {
  487.                   int last_exp_size = length_of_subexp (pstate->expout,
  488.                                                         pstate->expout_ptr);
  489.                   struct type *type;
  490.                   int i;
  491.                   int base = pstate->expout_ptr - last_exp_size - 3;

  492.                   if (base < 0
  493.                       || pstate->expout->elts[base+2].opcode != OP_TYPE)
  494.                     error (_("Invalid cast expression"));
  495.                   type = pstate->expout->elts[base+1].type;
  496.                   /* Remove the 'Expression' and slide the
  497.                      UnaryExpressionNotPlusMinus down to replace it.  */
  498.                   for (i = 0;  i < last_exp_size;  i++)
  499.                     pstate->expout->elts[base + i]
  500.                       = pstate->expout->elts[base + i + 3];
  501.                   pstate->expout_ptr -= 3;
  502.                   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  503.                     type = lookup_pointer_type (type);
  504.                   write_exp_elt_opcode (pstate, UNOP_CAST);
  505.                   write_exp_elt_type (pstate, type);
  506.                   write_exp_elt_opcode (pstate, UNOP_CAST);
  507.                 }
  508. |        '(' Name Dims ')' UnaryExpressionNotPlusMinus
  509.                 { write_exp_elt_opcode (pstate, UNOP_CAST);
  510.                   write_exp_elt_type (pstate,
  511.                                       java_array_type (java_type_from_name
  512.                                                        ($2), $3));
  513.                   write_exp_elt_opcode (pstate, UNOP_CAST); }
  514. ;


  515. MultiplicativeExpression:
  516.         UnaryExpression
  517. |        MultiplicativeExpression '*' UnaryExpression
  518.                 { write_exp_elt_opcode (pstate, BINOP_MUL); }
  519. |        MultiplicativeExpression '/' UnaryExpression
  520.                 { write_exp_elt_opcode (pstate, BINOP_DIV); }
  521. |        MultiplicativeExpression '%' UnaryExpression
  522.                 { write_exp_elt_opcode (pstate, BINOP_REM); }
  523. ;

  524. AdditiveExpression:
  525.         MultiplicativeExpression
  526. |        AdditiveExpression '+' MultiplicativeExpression
  527.                 { write_exp_elt_opcode (pstate, BINOP_ADD); }
  528. |        AdditiveExpression '-' MultiplicativeExpression
  529.                 { write_exp_elt_opcode (pstate, BINOP_SUB); }
  530. ;

  531. ShiftExpression:
  532.         AdditiveExpression
  533. |        ShiftExpression LSH AdditiveExpression
  534.                 { write_exp_elt_opcode (pstate, BINOP_LSH); }
  535. |        ShiftExpression RSH AdditiveExpression
  536.                 { write_exp_elt_opcode (pstate, BINOP_RSH); }
  537. /* |        ShiftExpression >>> AdditiveExpression { FIXME } */
  538. ;

  539. RelationalExpression:
  540.         ShiftExpression
  541. |        RelationalExpression '<' ShiftExpression
  542.                 { write_exp_elt_opcode (pstate, BINOP_LESS); }
  543. |        RelationalExpression '>' ShiftExpression
  544.                 { write_exp_elt_opcode (pstate, BINOP_GTR); }
  545. |        RelationalExpression LEQ ShiftExpression
  546.                 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
  547. |        RelationalExpression GEQ ShiftExpression
  548.                 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
  549. /* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
  550. ;

  551. EqualityExpression:
  552.         RelationalExpression
  553. |        EqualityExpression EQUAL RelationalExpression
  554.                 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  555. |        EqualityExpression NOTEQUAL RelationalExpression
  556.                 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  557. ;

  558. AndExpression:
  559.         EqualityExpression
  560. |        AndExpression '&' EqualityExpression
  561.                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  562. ;

  563. ExclusiveOrExpression:
  564.         AndExpression
  565. |        ExclusiveOrExpression '^' AndExpression
  566.                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  567. ;
  568. InclusiveOrExpression:
  569.         ExclusiveOrExpression
  570. |        InclusiveOrExpression '|' ExclusiveOrExpression
  571.                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  572. ;

  573. ConditionalAndExpression:
  574.         InclusiveOrExpression
  575. |        ConditionalAndExpression ANDAND InclusiveOrExpression
  576.                 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  577. ;

  578. ConditionalOrExpression:
  579.         ConditionalAndExpression
  580. |        ConditionalOrExpression OROR ConditionalAndExpression
  581.                 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  582. ;

  583. ConditionalExpression:
  584.         ConditionalOrExpression
  585. |        ConditionalOrExpression '?' Expression ':' ConditionalExpression
  586.                 { write_exp_elt_opcode (pstate, TERNOP_COND); }
  587. ;

  588. AssignmentExpression:
  589.         ConditionalExpression
  590. |        Assignment
  591. ;

  592. Assignment:
  593.         LeftHandSide '=' ConditionalExpression
  594.                 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  595. |        LeftHandSide ASSIGN_MODIFY ConditionalExpression
  596.                 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
  597.                   write_exp_elt_opcode (pstate, $2);
  598.                   write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
  599. ;

  600. LeftHandSide:
  601.         ForcedName
  602.                 { push_expression_name (pstate, $1); }
  603. |        VARIABLE
  604.                 /* Already written by write_dollar_variable.  */
  605. |        FieldAccess
  606. |        ArrayAccess
  607. ;


  608. Expression:
  609.         AssignmentExpression
  610. ;

  611. %%
  612. /* Take care of parsing a number (anything that starts with a digit).
  613.    Set yylval and return the token type; update lexptr.
  614.    LEN is the number of characters in it.  */

  615. /*** Needs some error checking for the float case ***/

  616. static int
  617. parse_number (struct parser_state *par_state,
  618.               const char *p, int len, int parsed_float, YYSTYPE *putithere)
  619. {
  620.   ULONGEST n = 0;
  621.   ULONGEST limit, limit_div_base;

  622.   int c;
  623.   int base = input_radix;

  624.   struct type *type;

  625.   if (parsed_float)
  626.     {
  627.       const char *suffix;
  628.       int suffix_len;

  629.       if (! parse_float (p, len, &putithere->typed_val_float.dval, &suffix))
  630.         return ERROR;

  631.       suffix_len = p + len - suffix;

  632.       if (suffix_len == 0)
  633.         putithere->typed_val_float.type
  634.           = parse_type (par_state)->builtin_double;
  635.       else if (suffix_len == 1)
  636.         {
  637.           /* See if it has `f' or `d' suffix (float or double).  */
  638.           if (tolower (*suffix) == 'f')
  639.             putithere->typed_val_float.type =
  640.               parse_type (par_state)->builtin_float;
  641.           else if (tolower (*suffix) == 'd')
  642.             putithere->typed_val_float.type =
  643.               parse_type (par_state)->builtin_double;
  644.           else
  645.             return ERROR;
  646.         }
  647.       else
  648.         return ERROR;

  649.       return FLOATING_POINT_LITERAL;
  650.     }

  651.   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  652.   if (p[0] == '0')
  653.     switch (p[1])
  654.       {
  655.       case 'x':
  656.       case 'X':
  657.         if (len >= 3)
  658.           {
  659.             p += 2;
  660.             base = 16;
  661.             len -= 2;
  662.           }
  663.         break;

  664.       case 't':
  665.       case 'T':
  666.       case 'd':
  667.       case 'D':
  668.         if (len >= 3)
  669.           {
  670.             p += 2;
  671.             base = 10;
  672.             len -= 2;
  673.           }
  674.         break;

  675.       default:
  676.         base = 8;
  677.         break;
  678.       }

  679.   c = p[len-1];
  680.   /* A paranoid calculation of (1<<64)-1.  */
  681.   limit = (ULONGEST)0xffffffff;
  682.   limit = ((limit << 16) << 16) | limit;
  683.   if (c == 'l' || c == 'L')
  684.     {
  685.       type = parse_java_type (par_state)->builtin_long;
  686.       len--;
  687.     }
  688.   else
  689.     {
  690.       type = parse_java_type (par_state)->builtin_int;
  691.     }
  692.   limit_div_base = limit / (ULONGEST) base;

  693.   while (--len >= 0)
  694.     {
  695.       c = *p++;
  696.       if (c >= '0' && c <= '9')
  697.         c -= '0';
  698.       else if (c >= 'A' && c <= 'Z')
  699.         c -= 'A' - 10;
  700.       else if (c >= 'a' && c <= 'z')
  701.         c -= 'a' - 10;
  702.       else
  703.         return ERROR;        /* Char not a digit */
  704.       if (c >= base)
  705.         return ERROR;
  706.       if (n > limit_div_base
  707.           || (n *= base) > limit - c)
  708.         error (_("Numeric constant too large"));
  709.       n += c;
  710.         }

  711.   /* If the type is bigger than a 32-bit signed integer can be, implicitly
  712.      promote to long.  Java does not do this, so mark it as
  713.      parse_type (par_state)->builtin_uint64 rather than
  714.      parse_java_type (par_state)->builtin_long.
  715.      0x80000000 will become -0x80000000 instead of 0x80000000L, because we
  716.      don't know the sign at this point.  */
  717.   if (type == parse_java_type (par_state)->builtin_int
  718.       && n > (ULONGEST)0x80000000)
  719.     type = parse_type (par_state)->builtin_uint64;

  720.   putithere->typed_val_int.val = n;
  721.   putithere->typed_val_int.type = type;

  722.   return INTEGER_LITERAL;
  723. }

  724. struct token
  725. {
  726.   char *operator;
  727.   int token;
  728.   enum exp_opcode opcode;
  729. };

  730. static const struct token tokentab3[] =
  731.   {
  732.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  733.     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
  734.   };

  735. static const struct token tokentab2[] =
  736.   {
  737.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  738.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  739.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  740.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  741.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  742.     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
  743.     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
  744.     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
  745.     {"++", INCREMENT, BINOP_END},
  746.     {"--", DECREMENT, BINOP_END},
  747.     {"&&", ANDAND, BINOP_END},
  748.     {"||", OROR, BINOP_END},
  749.     {"<<", LSH, BINOP_END},
  750.     {">>", RSH, BINOP_END},
  751.     {"==", EQUAL, BINOP_END},
  752.     {"!=", NOTEQUAL, BINOP_END},
  753.     {"<=", LEQ, BINOP_END},
  754.     {">=", GEQ, BINOP_END}
  755.   };

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

  757. static int
  758. yylex (void)
  759. {
  760.   int c;
  761.   int namelen;
  762.   unsigned int i;
  763.   const char *tokstart;
  764.   const char *tokptr;
  765.   int tempbufindex;
  766.   static char *tempbuf;
  767.   static int tempbufsize;

  768. retry:

  769.   prev_lexptr = lexptr;

  770.   tokstart = lexptr;
  771.   /* See if it is a special token of length 3.  */
  772.   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  773.     if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
  774.       {
  775.         lexptr += 3;
  776.         yylval.opcode = tokentab3[i].opcode;
  777.         return tokentab3[i].token;
  778.       }

  779.   /* See if it is a special token of length 2.  */
  780.   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  781.     if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
  782.       {
  783.         lexptr += 2;
  784.         yylval.opcode = tokentab2[i].opcode;
  785.         return tokentab2[i].token;
  786.       }

  787.   switch (c = *tokstart)
  788.     {
  789.     case 0:
  790.       return 0;

  791.     case ' ':
  792.     case '\t':
  793.     case '\n':
  794.       lexptr++;
  795.       goto retry;

  796.     case '\'':
  797.       /* We either have a character constant ('0' or '\177' for example)
  798.          or we have a quoted symbol reference ('foo(int,int)' in C++
  799.          for example).  */
  800.       lexptr++;
  801.       c = *lexptr++;
  802.       if (c == '\\')
  803.         c = parse_escape (parse_gdbarch (pstate), &lexptr);
  804.       else if (c == '\'')
  805.         error (_("Empty character constant"));

  806.       yylval.typed_val_int.val = c;
  807.       yylval.typed_val_int.type = parse_java_type (pstate)->builtin_char;

  808.       c = *lexptr++;
  809.       if (c != '\'')
  810.         {
  811.           namelen = skip_quoted (tokstart) - tokstart;
  812.           if (namelen > 2)
  813.             {
  814.               lexptr = tokstart + namelen;
  815.               if (lexptr[-1] != '\'')
  816.                 error (_("Unmatched single quote"));
  817.               namelen -= 2;
  818.               tokstart++;
  819.               goto tryname;
  820.             }
  821.           error (_("Invalid character constant"));
  822.         }
  823.       return INTEGER_LITERAL;

  824.     case '(':
  825.       paren_depth++;
  826.       lexptr++;
  827.       return c;

  828.     case ')':
  829.       if (paren_depth == 0)
  830.         return 0;
  831.       paren_depth--;
  832.       lexptr++;
  833.       return c;

  834.     case ',':
  835.       if (comma_terminates && paren_depth == 0)
  836.         return 0;
  837.       lexptr++;
  838.       return c;

  839.     case '.':
  840.       /* Might be a floating point number.  */
  841.       if (lexptr[1] < '0' || lexptr[1] > '9')
  842.         goto symbol;                /* Nope, must be a symbol.  */
  843.       /* FALL THRU into number case.  */

  844.     case '0':
  845.     case '1':
  846.     case '2':
  847.     case '3':
  848.     case '4':
  849.     case '5':
  850.     case '6':
  851.     case '7':
  852.     case '8':
  853.     case '9':
  854.       {
  855.         /* It's a number.  */
  856.         int got_dot = 0, got_e = 0, toktype;
  857.         const char *p = tokstart;
  858.         int hex = input_radix > 10;

  859.         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  860.           {
  861.             p += 2;
  862.             hex = 1;
  863.           }
  864.         else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  865.           {
  866.             p += 2;
  867.             hex = 0;
  868.           }

  869.         for (;; ++p)
  870.           {
  871.             /* This test includes !hex because 'e' is a valid hex digit
  872.                and thus does not indicate a floating point number when
  873.                the radix is hex.  */
  874.             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  875.               got_dot = got_e = 1;
  876.             /* This test does not include !hex, because a '.' always indicates
  877.                a decimal floating point number regardless of the radix.  */
  878.             else if (!got_dot && *p == '.')
  879.               got_dot = 1;
  880.             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  881.                      && (*p == '-' || *p == '+'))
  882.               /* This is the sign of the exponent, not the end of the
  883.                  number.  */
  884.               continue;
  885.             /* We will take any letters or digits.  parse_number will
  886.                complain if past the radix, or if L or U are not final.  */
  887.             else if ((*p < '0' || *p > '9')
  888.                      && ((*p < 'a' || *p > 'z')
  889.                                   && (*p < 'A' || *p > 'Z')))
  890.               break;
  891.           }
  892.         toktype = parse_number (pstate, tokstart, p - tokstart,
  893.                                 got_dot|got_e, &yylval);
  894.         if (toktype == ERROR)
  895.           {
  896.             char *err_copy = (char *) alloca (p - tokstart + 1);

  897.             memcpy (err_copy, tokstart, p - tokstart);
  898.             err_copy[p - tokstart] = 0;
  899.             error (_("Invalid number \"%s\""), err_copy);
  900.           }
  901.         lexptr = p;
  902.         return toktype;
  903.       }

  904.     case '+':
  905.     case '-':
  906.     case '*':
  907.     case '/':
  908.     case '%':
  909.     case '|':
  910.     case '&':
  911.     case '^':
  912.     case '~':
  913.     case '!':
  914.     case '<':
  915.     case '>':
  916.     case '[':
  917.     case ']':
  918.     case '?':
  919.     case ':':
  920.     case '=':
  921.     case '{':
  922.     case '}':
  923.     symbol:
  924.       lexptr++;
  925.       return c;

  926.     case '"':

  927.       /* Build the gdb internal form of the input string in tempbuf,
  928.          translating any standard C escape forms seen.  Note that the
  929.          buffer is null byte terminated *only* for the convenience of
  930.          debugging gdb itself and printing the buffer contents when
  931.          the buffer contains no embedded nulls.  Gdb does not depend
  932.          upon the buffer being null byte terminated, it uses the length
  933.          string instead.  This allows gdb to handle C strings (as well
  934.          as strings in other languages) with embedded null bytes */

  935.       tokptr = ++tokstart;
  936.       tempbufindex = 0;

  937.       do {
  938.         /* Grow the static temp buffer if necessary, including allocating
  939.            the first one on demand.  */
  940.         if (tempbufindex + 1 >= tempbufsize)
  941.           {
  942.             tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
  943.           }
  944.         switch (*tokptr)
  945.           {
  946.           case '\0':
  947.           case '"':
  948.             /* Do nothing, loop will terminate.  */
  949.             break;
  950.           case '\\':
  951.             tokptr++;
  952.             c = parse_escape (parse_gdbarch (pstate), &tokptr);
  953.             if (c == -1)
  954.               {
  955.                 continue;
  956.               }
  957.             tempbuf[tempbufindex++] = c;
  958.             break;
  959.           default:
  960.             tempbuf[tempbufindex++] = *tokptr++;
  961.             break;
  962.           }
  963.       } while ((*tokptr != '"') && (*tokptr != '\0'));
  964.       if (*tokptr++ != '"')
  965.         {
  966.           error (_("Unterminated string in expression"));
  967.         }
  968.       tempbuf[tempbufindex] = '\0';        /* See note above */
  969.       yylval.sval.ptr = tempbuf;
  970.       yylval.sval.length = tempbufindex;
  971.       lexptr = tokptr;
  972.       return (STRING_LITERAL);
  973.     }

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

  978.   /* It's a name.  See how long it is.  */
  979.   namelen = 0;
  980.   for (c = tokstart[namelen];
  981.        (c == '_'
  982.         || c == '$'
  983.         || (c >= '0' && c <= '9')
  984.         || (c >= 'a' && c <= 'z')
  985.         || (c >= 'A' && c <= 'Z')
  986.         || c == '<');
  987.        )
  988.     {
  989.       if (c == '<')
  990.         {
  991.           int i = namelen;
  992.           while (tokstart[++i] && tokstart[i] != '>');
  993.           if (tokstart[i] == '>')
  994.             namelen = i;
  995.         }
  996.        c = tokstart[++namelen];
  997.      }

  998.   /* The token "if" terminates the expression and is NOT
  999.      removed from the input stream.  */
  1000.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1001.     {
  1002.       return 0;
  1003.     }

  1004.   lexptr += namelen;

  1005.   tryname:

  1006.   /* Catch specific keywords.  Should be done with a data structure.  */
  1007.   switch (namelen)
  1008.     {
  1009.     case 7:
  1010.       if (strncmp (tokstart, "boolean", 7) == 0)
  1011.         return BOOLEAN;
  1012.       break;
  1013.     case 6:
  1014.       if (strncmp (tokstart, "double", 6) == 0)
  1015.         return DOUBLE;
  1016.       break;
  1017.     case 5:
  1018.       if (strncmp (tokstart, "short", 5) == 0)
  1019.         return SHORT;
  1020.       if (strncmp (tokstart, "false", 5) == 0)
  1021.         {
  1022.           yylval.lval = 0;
  1023.           return BOOLEAN_LITERAL;
  1024.         }
  1025.       if (strncmp (tokstart, "super", 5) == 0)
  1026.         return SUPER;
  1027.       if (strncmp (tokstart, "float", 5) == 0)
  1028.         return FLOAT;
  1029.       break;
  1030.     case 4:
  1031.       if (strncmp (tokstart, "long", 4) == 0)
  1032.         return LONG;
  1033.       if (strncmp (tokstart, "byte", 4) == 0)
  1034.         return BYTE;
  1035.       if (strncmp (tokstart, "char", 4) == 0)
  1036.         return CHAR;
  1037.       if (strncmp (tokstart, "true", 4) == 0)
  1038.         {
  1039.           yylval.lval = 1;
  1040.           return BOOLEAN_LITERAL;
  1041.         }
  1042.       break;
  1043.     case 3:
  1044.       if (strncmp (tokstart, "int", 3) == 0)
  1045.         return INT;
  1046.       if (strncmp (tokstart, "new", 3) == 0)
  1047.         return NEW;
  1048.       break;
  1049.     default:
  1050.       break;
  1051.     }

  1052.   yylval.sval.ptr = tokstart;
  1053.   yylval.sval.length = namelen;

  1054.   if (*tokstart == '$')
  1055.     {
  1056.       write_dollar_variable (pstate, yylval.sval);
  1057.       return VARIABLE;
  1058.     }

  1059.   /* Input names that aren't symbols but ARE valid hex numbers,
  1060.      when the input radix permits them, can be names or numbers
  1061.      depending on the parse.  Note we support radixes > 16 here.  */
  1062.   if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
  1063.        (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1064.     {
  1065.       YYSTYPE newlval;        /* Its value is ignored.  */
  1066.       int hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
  1067.       if (hextype == INTEGER_LITERAL)
  1068.         return NAME_OR_INT;
  1069.     }
  1070.   return IDENTIFIER;
  1071. }

  1072. int
  1073. java_parse (struct parser_state *par_state)
  1074. {
  1075.   int result;
  1076.   struct cleanup *c = make_cleanup_clear_parser_state (&pstate);

  1077.   /* Setting up the parser state.  */
  1078.   gdb_assert (par_state != NULL);
  1079.   pstate = par_state;

  1080.   result = yyparse ();
  1081.   do_cleanups (c);

  1082.   return result;
  1083. }

  1084. void
  1085. yyerror (char *msg)
  1086. {
  1087.   if (prev_lexptr)
  1088.     lexptr = prev_lexptr;

  1089.   if (msg)
  1090.     error (_("%s: near `%s'"), msg, lexptr);
  1091.   else
  1092.     error (_("error in expression, near `%s'"), lexptr);
  1093. }

  1094. static struct type *
  1095. java_type_from_name (struct stoken name)
  1096. {
  1097.   char *tmp = copy_name (name);
  1098.   struct type *typ = java_lookup_class (tmp);
  1099.   if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
  1100.     error (_("No class named `%s'"), tmp);
  1101.   return typ;
  1102. }

  1103. /* If NAME is a valid variable name in this scope, push it and return 1.
  1104.    Otherwise, return 0.  */

  1105. static int
  1106. push_variable (struct parser_state *par_state, struct stoken name)
  1107. {
  1108.   char *tmp = copy_name (name);
  1109.   struct field_of_this_result is_a_field_of_this;
  1110.   struct symbol *sym;

  1111.   sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
  1112.                        &is_a_field_of_this);
  1113.   if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  1114.     {
  1115.       if (symbol_read_needs_frame (sym))
  1116.         {
  1117.           if (innermost_block == 0 ||
  1118.               contained_in (block_found, innermost_block))
  1119.             innermost_block = block_found;
  1120.         }

  1121.       write_exp_elt_opcode (par_state, OP_VAR_VALUE);
  1122.       /* We want to use the selected frame, not another more inner frame
  1123.          which happens to be in the same block.  */
  1124.       write_exp_elt_block (par_state, NULL);
  1125.       write_exp_elt_sym (par_state, sym);
  1126.       write_exp_elt_opcode (par_state, OP_VAR_VALUE);
  1127.       return 1;
  1128.     }
  1129.   if (is_a_field_of_this.type != NULL)
  1130.     {
  1131.       /* it hangs off of `this'.  Must not inadvertently convert from a
  1132.          method call to data ref.  */
  1133.       if (innermost_block == 0 ||
  1134.           contained_in (block_found, innermost_block))
  1135.         innermost_block = block_found;
  1136.       write_exp_elt_opcode (par_state, OP_THIS);
  1137.       write_exp_elt_opcode (par_state, OP_THIS);
  1138.       write_exp_elt_opcode (par_state, STRUCTOP_PTR);
  1139.       write_exp_string (par_state, name);
  1140.       write_exp_elt_opcode (par_state, STRUCTOP_PTR);
  1141.       return 1;
  1142.     }
  1143.   return 0;
  1144. }

  1145. /* Assuming a reference expression has been pushed, emit the
  1146.    STRUCTOP_PTR ops to access the field named NAME.  If NAME is a
  1147.    qualified name (has '.'), generate a field access for each part.  */

  1148. static void
  1149. push_fieldnames (struct parser_state *par_state, struct stoken name)
  1150. {
  1151.   int i;
  1152.   struct stoken token;
  1153.   token.ptr = name.ptr;
  1154.   for (i = 0;  ;  i++)
  1155.     {
  1156.       if (i == name.length || name.ptr[i] == '.')
  1157.         {
  1158.           /* token.ptr is start of current field name.  */
  1159.           token.length = &name.ptr[i] - token.ptr;
  1160.           write_exp_elt_opcode (par_state, STRUCTOP_PTR);
  1161.           write_exp_string (par_state, token);
  1162.           write_exp_elt_opcode (par_state, STRUCTOP_PTR);
  1163.           token.ptr += token.length + 1;
  1164.         }
  1165.       if (i >= name.length)
  1166.         break;
  1167.     }
  1168. }

  1169. /* Helper routine for push_expression_name.
  1170.    Handle a qualified name, where DOT_INDEX is the index of the first '.' */

  1171. static void
  1172. push_qualified_expression_name (struct parser_state *par_state,
  1173.                                 struct stoken name, int dot_index)
  1174. {
  1175.   struct stoken token;
  1176.   char *tmp;
  1177.   struct type *typ;

  1178.   token.ptr = name.ptr;
  1179.   token.length = dot_index;

  1180.   if (push_variable (par_state, token))
  1181.     {
  1182.       token.ptr = name.ptr + dot_index + 1;
  1183.       token.length = name.length - dot_index - 1;
  1184.       push_fieldnames (par_state, token);
  1185.       return;
  1186.     }

  1187.   token.ptr = name.ptr;
  1188.   for (;;)
  1189.     {
  1190.       token.length = dot_index;
  1191.       tmp = copy_name (token);
  1192.       typ = java_lookup_class (tmp);
  1193.       if (typ != NULL)
  1194.         {
  1195.           if (dot_index == name.length)
  1196.             {
  1197.               write_exp_elt_opcode (par_state, OP_TYPE);
  1198.               write_exp_elt_type (par_state, typ);
  1199.               write_exp_elt_opcode (par_state, OP_TYPE);
  1200.               return;
  1201.             }
  1202.           dot_index++;  /* Skip '.' */
  1203.           name.ptr += dot_index;
  1204.           name.length -= dot_index;
  1205.           dot_index = 0;
  1206.           while (dot_index < name.length && name.ptr[dot_index] != '.')
  1207.             dot_index++;
  1208.           token.ptr = name.ptr;
  1209.           token.length = dot_index;
  1210.           write_exp_elt_opcode (par_state, OP_SCOPE);
  1211.           write_exp_elt_type (par_state, typ);
  1212.           write_exp_string (par_state, token);
  1213.           write_exp_elt_opcode (par_state, OP_SCOPE);
  1214.           if (dot_index < name.length)
  1215.             {
  1216.               dot_index++;
  1217.               name.ptr += dot_index;
  1218.               name.length -= dot_index;
  1219.               push_fieldnames (par_state, name);
  1220.             }
  1221.           return;
  1222.         }
  1223.       else if (dot_index >= name.length)
  1224.         break;
  1225.       dot_index++;  /* Skip '.' */
  1226.       while (dot_index < name.length && name.ptr[dot_index] != '.')
  1227.         dot_index++;
  1228.     }
  1229.   error (_("unknown type `%.*s'"), name.length, name.ptr);
  1230. }

  1231. /* Handle Name in an expression (or LHS).
  1232.    Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN.  */

  1233. static void
  1234. push_expression_name (struct parser_state *par_state, struct stoken name)
  1235. {
  1236.   char *tmp;
  1237.   struct type *typ;
  1238.   int i;

  1239.   for (i = 0;  i < name.length;  i++)
  1240.     {
  1241.       if (name.ptr[i] == '.')
  1242.         {
  1243.           /* It's a Qualified Expression Name.  */
  1244.           push_qualified_expression_name (par_state, name, i);
  1245.           return;
  1246.         }
  1247.     }

  1248.   /* It's a Simple Expression Name.  */

  1249.   if (push_variable (par_state, name))
  1250.     return;
  1251.   tmp = copy_name (name);
  1252.   typ = java_lookup_class (tmp);
  1253.   if (typ != NULL)
  1254.     {
  1255.       write_exp_elt_opcode (par_state, OP_TYPE);
  1256.       write_exp_elt_type (par_state, typ);
  1257.       write_exp_elt_opcode (par_state, OP_TYPE);
  1258.     }
  1259.   else
  1260.     {
  1261.       struct bound_minimal_symbol msymbol;

  1262.       msymbol = lookup_bound_minimal_symbol (tmp);
  1263.       if (msymbol.minsym != NULL)
  1264.         write_exp_msymbol (par_state, msymbol);
  1265.       else if (!have_full_symbols () && !have_partial_symbols ())
  1266.         error (_("No symbol table is loaded.  Use the \"file\" command"));
  1267.       else
  1268.         error (_("No symbol \"%s\" in current context."), tmp);
  1269.     }

  1270. }


  1271. /* The following two routines, copy_exp and insert_exp, aren't specific to
  1272.    Java, so they could go in parse.c, but their only purpose is to support
  1273.    the parsing kludges we use in this file, so maybe it's best to isolate
  1274.    them here.  */

  1275. /* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
  1276.    into a freshly malloc'ed struct expression.  Its language_defn is set
  1277.    to null.  */
  1278. static struct expression *
  1279. copy_exp (struct expression *expr, int endpos)
  1280. {
  1281.   int len = length_of_subexp (expr, endpos);
  1282.   struct expression *new
  1283.     = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));

  1284.   new->nelts = len;
  1285.   memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
  1286.   new->language_defn = 0;

  1287.   return new;
  1288. }

  1289. /* Insert the expression NEW into the current expression (expout) at POS.  */
  1290. static void
  1291. insert_exp (struct parser_state *par_state, int pos, struct expression *new)
  1292. {
  1293.   int newlen = new->nelts;
  1294.   int i;

  1295.   /* Grow expout if necessary.  In this function's only use at present,
  1296.      this should never be necessary.  */
  1297.   increase_expout_size (par_state, newlen);

  1298.   for (i = par_state->expout_ptr - 1; i >= pos; i--)
  1299.     par_state->expout->elts[i + newlen] = par_state->expout->elts[i];

  1300.   memcpy (par_state->expout->elts + pos, new->elts,
  1301.           EXP_ELEM_TO_BYTES (newlen));
  1302.   par_state->expout_ptr += newlen;
  1303. }