gdb/d-exp.y - gdb

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

  2.    Copyright (C) 2014-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, jv-exp.y.  */

  15. /* Parse a D expression from text in a string,
  16.    and return the result as a struct expression pointer.
  17.    That structure contains arithmetic operations in reverse polish,
  18.    with constants represented by operations that are followed by special data.
  19.    See expression.h for the details of the format.
  20.    What is important here is that it can be built up sequentially
  21.    during the process of parsing; the lower levels of the tree always
  22.    come first in the result.

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

  30. %{

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

  44. #define parse_type(ps) builtin_type (parse_gdbarch (ps))
  45. #define parse_d_type(ps) builtin_d_type (parse_gdbarch (ps))

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

  52. #define        yymaxdepth d_maxdepth
  53. #define        yyparse        d_parse_internal
  54. #define        yylex        d_lex
  55. #define        yyerror        d_error
  56. #define        yylval        d_lval
  57. #define        yychar        d_char
  58. #define        yydebug        d_debug
  59. #define        yypact        d_pact
  60. #define        yyr1        d_r1
  61. #define        yyr2        d_r2
  62. #define        yydef        d_def
  63. #define        yychk        d_chk
  64. #define        yypgo        d_pgo
  65. #define        yyact        d_act
  66. #define        yyexca        d_exca
  67. #define        yyerrflag d_errflag
  68. #define        yynerrs        d_nerrs
  69. #define        yyps        d_ps
  70. #define        yypv        d_pv
  71. #define        yys        d_s
  72. #define        yy_yys        d_yys
  73. #define        yystate        d_state
  74. #define        yytmp        d_tmp
  75. #define        yyv        d_v
  76. #define        yy_yyv        d_yyv
  77. #define        yyval        d_val
  78. #define        yylloc        d_lloc
  79. #define        yyreds        d_reds        /* With YYDEBUG defined */
  80. #define        yytoks        d_toks        /* With YYDEBUG defined */
  81. #define        yyname        d_name        /* With YYDEBUG defined */
  82. #define        yyrule        d_rule        /* With YYDEBUG defined */
  83. #define        yylhs        d_yylhs
  84. #define        yylen        d_yylen
  85. #define        yydefre        d_yydefred
  86. #define        yydgoto        d_yydgoto
  87. #define        yysindex d_yysindex
  88. #define        yyrindex d_yyrindex
  89. #define        yygindex d_yygindex
  90. #define        yytable        d_yytable
  91. #define        yycheck        d_yycheck
  92. #define        yyss        d_yyss
  93. #define        yysslim        d_yysslim
  94. #define        yyssp        d_yyssp
  95. #define        yystacksize d_yystacksize
  96. #define        yyvs        d_yyvs
  97. #define        yyvsp        d_yyvsp

  98. #ifndef YYDEBUG
  99. #define YYDEBUG 1        /* Default to yydebug support */
  100. #endif

  101. #define YYFPRINTF parser_fprintf

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

  104. static struct parser_state *pstate = NULL;

  105. int yyparse (void);

  106. static int yylex (void);

  107. void yyerror (char *);

  108. %}

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

  112. %union
  113.   {
  114.     struct {
  115.       LONGEST val;
  116.       struct type *type;
  117.     } typed_val_int;
  118.     struct {
  119.       DOUBLEST dval;
  120.       struct type *type;
  121.     } typed_val_float;
  122.     struct symbol *sym;
  123.     struct type *tval;
  124.     struct typed_stoken tsval;
  125.     struct stoken sval;
  126.     struct ttype tsym;
  127.     struct symtoken ssym;
  128.     int ival;
  129.     struct block *bval;
  130.     enum exp_opcode opcode;
  131.     struct stoken_vector svec;
  132.   }

  133. %{
  134. /* YYSTYPE gets defined by %union */
  135. static int parse_number (struct parser_state *, const char *,
  136.                          int, int, YYSTYPE *);

  137. static void push_expression_name (struct parser_state *, struct stoken);
  138. %}

  139. %token <sval> IDENTIFIER
  140. %token <tsym> TYPENAME
  141. %token <voidval> COMPLETE

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

  146. %token <sval> NAME_OR_INT

  147. %token <typed_val_int> INTEGER_LITERAL
  148. %token <typed_val_float> FLOAT_LITERAL
  149. %token <tsval> CHARACTER_LITERAL
  150. %token <tsval> STRING_LITERAL

  151. %type <svec> StringExp
  152. %type <tval> BasicType TypeExp
  153. %type <sval> IdentifierExp
  154. %type <ival> ArrayLiteral

  155. %token ENTRY
  156. %token ERROR

  157. /* Keywords that have a constant value.  */
  158. %token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
  159. /* Class 'super' accessor.  */
  160. %token SUPER_KEYWORD
  161. /* Properties.  */
  162. %token CAST_KEYWORD SIZEOF_KEYWORD
  163. %token TYPEOF_KEYWORD TYPEID_KEYWORD
  164. %token INIT_KEYWORD
  165. /* Comparison keywords.  */
  166. /* Type storage classes.  */
  167. %token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
  168. /* Non-scalar type keywords.  */
  169. %token STRUCT_KEYWORD UNION_KEYWORD
  170. %token CLASS_KEYWORD INTERFACE_KEYWORD
  171. %token ENUM_KEYWORD TEMPLATE_KEYWORD
  172. %token DELEGATE_KEYWORD FUNCTION_KEYWORD

  173. %token <sval> DOLLAR_VARIABLE

  174. %token <opcode> ASSIGN_MODIFY

  175. %left ','
  176. %right '=' ASSIGN_MODIFY
  177. %right '?'
  178. %left OROR
  179. %left ANDAND
  180. %left '|'
  181. %left '^'
  182. %left '&'
  183. %left EQUAL NOTEQUAL '<' '>' LEQ GEQ
  184. %right LSH RSH
  185. %left '+' '-'
  186. %left '*' '/' '%'
  187. %right HATHAT
  188. %left IDENTITY NOTIDENTITY
  189. %right INCREMENT DECREMENT
  190. %right '.' '[' '('
  191. %token DOTDOT


  192. %%

  193. start   :
  194.         Expression
  195. |        TypeExp
  196. ;

  197. /* Expressions, including the comma operator.  */

  198. Expression:
  199.         CommaExpression
  200. ;

  201. CommaExpression:
  202.         AssignExpression
  203. |        AssignExpression ',' CommaExpression
  204.                 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
  205. ;

  206. AssignExpression:
  207.         ConditionalExpression
  208. |        ConditionalExpression '=' AssignExpression
  209.                 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  210. |        ConditionalExpression ASSIGN_MODIFY AssignExpression
  211.                 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
  212.                   write_exp_elt_opcode (pstate, $2);
  213.                   write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
  214. ;

  215. ConditionalExpression:
  216.         OrOrExpression
  217. |        OrOrExpression '?' Expression ':' ConditionalExpression
  218.                 { write_exp_elt_opcode (pstate, TERNOP_COND); }
  219. ;

  220. OrOrExpression:
  221.         AndAndExpression
  222. |        OrOrExpression OROR AndAndExpression
  223.                 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  224. ;

  225. AndAndExpression:
  226.         OrExpression
  227. |        AndAndExpression ANDAND OrExpression
  228.                 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  229. ;

  230. OrExpression:
  231.         XorExpression
  232. |        OrExpression '|' XorExpression
  233.                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  234. ;

  235. XorExpression:
  236.         AndExpression
  237. |        XorExpression '^' AndExpression
  238.                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  239. ;

  240. AndExpression:
  241.         CmpExpression
  242. |        AndExpression '&' CmpExpression
  243.                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  244. ;

  245. CmpExpression:
  246.         ShiftExpression
  247. |        EqualExpression
  248. |        IdentityExpression
  249. |        RelExpression
  250. ;

  251. EqualExpression:
  252.         ShiftExpression EQUAL ShiftExpression
  253.                 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  254. |        ShiftExpression NOTEQUAL ShiftExpression
  255.                 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  256. ;

  257. IdentityExpression:
  258.         ShiftExpression IDENTITY ShiftExpression
  259.                 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  260. |        ShiftExpression NOTIDENTITY ShiftExpression
  261.                 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  262. ;

  263. RelExpression:
  264.         ShiftExpression '<' ShiftExpression
  265.                 { write_exp_elt_opcode (pstate, BINOP_LESS); }
  266. |        ShiftExpression LEQ ShiftExpression
  267.                 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
  268. |        ShiftExpression '>' ShiftExpression
  269.                 { write_exp_elt_opcode (pstate, BINOP_GTR); }
  270. |        ShiftExpression GEQ ShiftExpression
  271.                 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
  272. ;

  273. ShiftExpression:
  274.         AddExpression
  275. |        ShiftExpression LSH AddExpression
  276.                 { write_exp_elt_opcode (pstate, BINOP_LSH); }
  277. |        ShiftExpression RSH AddExpression
  278.                 { write_exp_elt_opcode (pstate, BINOP_RSH); }
  279. ;

  280. AddExpression:
  281.         MulExpression
  282. |        AddExpression '+' MulExpression
  283.                 { write_exp_elt_opcode (pstate, BINOP_ADD); }
  284. |        AddExpression '-' MulExpression
  285.                 { write_exp_elt_opcode (pstate, BINOP_SUB); }
  286. |        AddExpression '~' MulExpression
  287.                 { write_exp_elt_opcode (pstate, BINOP_CONCAT); }
  288. ;

  289. MulExpression:
  290.         UnaryExpression
  291. |        MulExpression '*' UnaryExpression
  292.                 { write_exp_elt_opcode (pstate, BINOP_MUL); }
  293. |        MulExpression '/' UnaryExpression
  294.                 { write_exp_elt_opcode (pstate, BINOP_DIV); }
  295. |        MulExpression '%' UnaryExpression
  296.                 { write_exp_elt_opcode (pstate, BINOP_REM); }

  297. UnaryExpression:
  298.         '&' UnaryExpression
  299.                 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
  300. |        INCREMENT UnaryExpression
  301.                 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
  302. |        DECREMENT UnaryExpression
  303.                 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
  304. |        '*' UnaryExpression
  305.                 { write_exp_elt_opcode (pstate, UNOP_IND); }
  306. |        '-' UnaryExpression
  307.                 { write_exp_elt_opcode (pstate, UNOP_NEG); }
  308. |        '+' UnaryExpression
  309.                 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
  310. |        '!' UnaryExpression
  311.                 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  312. |        '~' UnaryExpression
  313.                 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
  314. |        CastExpression
  315. |        PowExpression
  316. ;

  317. CastExpression:
  318.         CAST_KEYWORD '(' TypeExp ')' UnaryExpression
  319.                 { write_exp_elt_opcode (pstate, UNOP_CAST);
  320.                   write_exp_elt_type (pstate, $3);
  321.                   write_exp_elt_opcode (pstate, UNOP_CAST); }
  322.         /* C style cast is illegal D, but is still recognised in
  323.            the grammar, so we keep this around for convenience.  */
  324. |        '(' TypeExp ')' UnaryExpression
  325.                 { write_exp_elt_opcode (pstate, UNOP_CAST);
  326.                   write_exp_elt_type (pstate, $2);
  327.                   write_exp_elt_opcode (pstate, UNOP_CAST); }
  328. ;

  329. PowExpression:
  330.         PostfixExpression
  331. |        PostfixExpression HATHAT UnaryExpression
  332.                 { write_exp_elt_opcode (pstate, BINOP_EXP); }
  333. ;

  334. PostfixExpression:
  335.         PrimaryExpression
  336. |        PostfixExpression INCREMENT
  337.                 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
  338. |        PostfixExpression DECREMENT
  339.                 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
  340. |        CallExpression
  341. |        IndexExpression
  342. |        SliceExpression
  343. ;

  344. ArgumentList:
  345.         AssignExpression
  346.                 { arglist_len = 1; }
  347. |        ArgumentList ',' AssignExpression
  348.                 { arglist_len++; }
  349. ;

  350. ArgumentList_opt:
  351.         /* EMPTY */
  352.                 { arglist_len = 0; }
  353. |        ArgumentList
  354. ;

  355. CallExpression:
  356.         PostfixExpression '('
  357.                 { start_arglist (); }
  358.         ArgumentList_opt ')'
  359.                 { write_exp_elt_opcode (pstate, OP_FUNCALL);
  360.                   write_exp_elt_longcst (pstate, (LONGEST) end_arglist ());
  361.                   write_exp_elt_opcode (pstate, OP_FUNCALL); }
  362. ;

  363. IndexExpression:
  364.         PostfixExpression '[' ArgumentList ']'
  365.                 { if (arglist_len > 0)
  366.                     {
  367.                       write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
  368.                       write_exp_elt_longcst (pstate, (LONGEST) arglist_len);
  369.                       write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
  370.                     }
  371.                   else
  372.                     write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
  373.                 }
  374. ;

  375. SliceExpression:
  376.         PostfixExpression '[' ']'
  377.                 { /* Do nothing.  */ }
  378. |        PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']'
  379.                 { write_exp_elt_opcode (pstate, TERNOP_SLICE); }
  380. ;

  381. PrimaryExpression:
  382.         '(' Expression ')'
  383.                 { /* Do nothing.  */ }
  384. |        IdentifierExp
  385.                 { push_expression_name (pstate, $1); }
  386. |        IdentifierExp '.' COMPLETE
  387.                 { struct stoken s;
  388.                   s.ptr = "";
  389.                   s.length = 0;
  390.                   push_expression_name (pstate, $1);
  391.                   mark_struct_expression (pstate);
  392.                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  393.                   write_exp_string (pstate, s);
  394.                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  395. |        IdentifierExp '.' IDENTIFIER COMPLETE
  396.                 { push_expression_name (pstate, $1);
  397.                   mark_struct_expression (pstate);
  398.                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  399.                   write_exp_string (pstate, $3);
  400.                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  401. |        DOLLAR_VARIABLE
  402.                 { write_dollar_variable (pstate, $1); }
  403. |        NAME_OR_INT
  404.                 { YYSTYPE val;
  405.                   parse_number (pstate, $1.ptr, $1.length, 0, &val);
  406.                   write_exp_elt_opcode (pstate, OP_LONG);
  407.                   write_exp_elt_type (pstate, val.typed_val_int.type);
  408.                   write_exp_elt_longcst (pstate,
  409.                                          (LONGEST) val.typed_val_int.val);
  410.                   write_exp_elt_opcode (pstate, OP_LONG); }
  411. |        NULL_KEYWORD
  412.                 { struct type *type = parse_d_type (pstate)->builtin_void;
  413.                   type = lookup_pointer_type (type);
  414.                   write_exp_elt_opcode (pstate, OP_LONG);
  415.                   write_exp_elt_type (pstate, type);
  416.                   write_exp_elt_longcst (pstate, (LONGEST) 0);
  417.                   write_exp_elt_opcode (pstate, OP_LONG); }
  418. |        TRUE_KEYWORD
  419.                 { write_exp_elt_opcode (pstate, OP_BOOL);
  420.                   write_exp_elt_longcst (pstate, (LONGEST) 1);
  421.                   write_exp_elt_opcode (pstate, OP_BOOL); }
  422. |        FALSE_KEYWORD
  423.                 { write_exp_elt_opcode (pstate, OP_BOOL);
  424.                   write_exp_elt_longcst (pstate, (LONGEST) 0);
  425.                   write_exp_elt_opcode (pstate, OP_BOOL); }
  426. |        INTEGER_LITERAL
  427.                 { write_exp_elt_opcode (pstate, OP_LONG);
  428.                   write_exp_elt_type (pstate, $1.type);
  429.                   write_exp_elt_longcst (pstate, (LONGEST)($1.val));
  430.                   write_exp_elt_opcode (pstate, OP_LONG); }
  431. |        FLOAT_LITERAL
  432.                 { write_exp_elt_opcode (pstate, OP_DOUBLE);
  433.                   write_exp_elt_type (pstate, $1.type);
  434.                   write_exp_elt_dblcst (pstate, $1.dval);
  435.                   write_exp_elt_opcode (pstate, OP_DOUBLE); }
  436. |        CHARACTER_LITERAL
  437.                 { struct stoken_vector vec;
  438.                   vec.len = 1;
  439.                   vec.tokens = &$1;
  440.                   write_exp_string_vector (pstate, $1.type, &vec); }
  441. |        StringExp
  442.                 { int i;
  443.                   write_exp_string_vector (pstate, 0, &$1);
  444.                   for (i = 0; i < $1.len; ++i)
  445.                     free ($1.tokens[i].ptr);
  446.                   free ($1.tokens); }
  447. |        ArrayLiteral
  448.                 { write_exp_elt_opcode (pstate, OP_ARRAY);
  449.                   write_exp_elt_longcst (pstate, (LONGEST) 0);
  450.                   write_exp_elt_longcst (pstate, (LONGEST) $1 - 1);
  451.                   write_exp_elt_opcode (pstate, OP_ARRAY); }
  452. ;

  453. ArrayLiteral:
  454.         '[' ArgumentList_opt ']'
  455.                 { $$ = arglist_len; }
  456. ;

  457. IdentifierExp:
  458.         IDENTIFIER
  459. |        IdentifierExp '.' IDENTIFIER
  460.                 { $$.length = $1.length + $3.length + 1;
  461.                   if ($1.ptr + $1.length + 1 == $3.ptr
  462.                       && $1.ptr[$1.length] == '.')
  463.                     $$.ptr = $1.ptr;  /* Optimization.  */
  464.                   else
  465.                     {
  466.                       char *buf = malloc ($$.length + 1);
  467.                       make_cleanup (free, buf);
  468.                       sprintf (buf, "%.*s.%.*s",
  469.                                $1.length, $1.ptr, $3.length, $3.ptr);
  470.                       $$.ptr = buf;
  471.                     }
  472.                 }
  473. ;

  474. StringExp:
  475.         STRING_LITERAL
  476.                 { /* We copy the string here, and not in the
  477.                      lexer, to guarantee that we do not leak a
  478.                      string.  Note that we follow the
  479.                      NUL-termination convention of the
  480.                      lexer.  */
  481.                   struct typed_stoken *vec = XNEW (struct typed_stoken);
  482.                   $$.len = 1;
  483.                   $$.tokens = vec;

  484.                   vec->type = $1.type;
  485.                   vec->length = $1.length;
  486.                   vec->ptr = malloc ($1.length + 1);
  487.                   memcpy (vec->ptr, $1.ptr, $1.length + 1);
  488.                 }
  489. |        StringExp STRING_LITERAL
  490.                 { /* Note that we NUL-terminate here, but just
  491.                      for convenience.  */
  492.                   char *p;
  493.                   ++$$.len;
  494.                   $$.tokens = realloc ($$.tokens,
  495.                                        $$.len * sizeof (struct typed_stoken));

  496.                   p = malloc ($2.length + 1);
  497.                   memcpy (p, $2.ptr, $2.length + 1);

  498.                   $$.tokens[$$.len - 1].type = $2.type;
  499.                   $$.tokens[$$.len - 1].length = $2.length;
  500.                   $$.tokens[$$.len - 1].ptr = p;
  501.                 }
  502. ;

  503. TypeExp:
  504.         BasicType
  505.                 { write_exp_elt_opcode (pstate, OP_TYPE);
  506.                   write_exp_elt_type (pstate, $1);
  507.                   write_exp_elt_opcode (pstate, OP_TYPE); }
  508. |        BasicType BasicType2
  509.                 { $$ = follow_types ($1);
  510.                   write_exp_elt_opcode (pstate, OP_TYPE);
  511.                   write_exp_elt_type (pstate, $$);
  512.                   write_exp_elt_opcode (pstate, OP_TYPE);
  513.                 }
  514. ;

  515. BasicType2:
  516.         '*'
  517.                 { push_type (tp_pointer); }
  518. |        '*' BasicType2
  519.                 { push_type (tp_pointer); }
  520. |        '[' INTEGER_LITERAL ']'
  521.                 { push_type_int ($2.val);
  522.                   push_type (tp_array); }
  523. |        '[' INTEGER_LITERAL ']' BasicType2
  524.                 { push_type_int ($2.val);
  525.                   push_type (tp_array); }
  526. ;

  527. BasicType:
  528.         TYPENAME
  529.                 { $$ = $1.type; }
  530. |        CLASS_KEYWORD IdentifierExp
  531.                 { $$ = lookup_struct (copy_name ($2),
  532.                                       expression_context_block); }
  533. |        CLASS_KEYWORD COMPLETE
  534.                 { mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
  535.                   $$ = NULL; }
  536. |        CLASS_KEYWORD IdentifierExp COMPLETE
  537.                 { mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr, $2.length);
  538.                   $$ = NULL; }
  539. |        STRUCT_KEYWORD IdentifierExp
  540.                 { $$ = lookup_struct (copy_name ($2),
  541.                                       expression_context_block); }
  542. |        STRUCT_KEYWORD COMPLETE
  543.                 { mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
  544.                   $$ = NULL; }
  545. |        STRUCT_KEYWORD IdentifierExp COMPLETE
  546.                 { mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr, $2.length);
  547.                   $$ = NULL; }
  548. |        UNION_KEYWORD IdentifierExp
  549.                 { $$ = lookup_union (copy_name ($2),
  550.                                      expression_context_block); }
  551. |        UNION_KEYWORD COMPLETE
  552.                 { mark_completion_tag (TYPE_CODE_UNION, "", 0);
  553.                   $$ = NULL; }
  554. |        UNION_KEYWORD IdentifierExp COMPLETE
  555.                 { mark_completion_tag (TYPE_CODE_UNION, $2.ptr, $2.length);
  556.                   $$ = NULL; }
  557. |        ENUM_KEYWORD IdentifierExp
  558.                 { $$ = lookup_enum (copy_name ($2),
  559.                                     expression_context_block); }
  560. |        ENUM_KEYWORD COMPLETE
  561.                 { mark_completion_tag (TYPE_CODE_ENUM, "", 0);
  562.                   $$ = NULL; }
  563. |        ENUM_KEYWORD IdentifierExp COMPLETE
  564.                 { mark_completion_tag (TYPE_CODE_ENUM, $2.ptr, $2.length);
  565.                   $$ = NULL; }
  566. ;

  567. %%

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

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

  572. static int
  573. parse_number (struct parser_state *ps, const char *p,
  574.               int len, int parsed_float, YYSTYPE *putithere)
  575. {
  576.   ULONGEST n = 0;
  577.   ULONGEST prevn = 0;
  578.   ULONGEST un;

  579.   int i = 0;
  580.   int c;
  581.   int base = input_radix;
  582.   int unsigned_p = 0;
  583.   int long_p = 0;

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

  586.   ULONGEST high_bit;
  587.   struct type *signed_type;
  588.   struct type *unsigned_type;

  589.   if (parsed_float)
  590.     {
  591.       const struct builtin_d_type *builtin_d_types;
  592.       const char *suffix;
  593.       int suffix_len;
  594.       char *s, *sp;

  595.       /* Strip out all embedded '_' before passing to parse_float.  */
  596.       s = (char *) alloca (len + 1);
  597.       sp = s;
  598.       while (len-- > 0)
  599.         {
  600.           if (*p != '_')
  601.             *sp++ = *p;
  602.           p++;
  603.         }
  604.       *sp = '\0';
  605.       len = strlen (s);

  606.       if (! parse_float (s, len, &putithere->typed_val_float.dval, &suffix))
  607.         return ERROR;

  608.       suffix_len = s + len - suffix;

  609.       if (suffix_len == 0)
  610.         {
  611.           putithere->typed_val_float.type
  612.             = parse_d_type (ps)->builtin_double;
  613.         }
  614.       else if (suffix_len == 1)
  615.         {
  616.           /* Check suffix for `f', `l', or `i' (float, real, or idouble).  */
  617.           if (tolower (*suffix) == 'f')
  618.             {
  619.               putithere->typed_val_float.type
  620.                 = parse_d_type (ps)->builtin_float;
  621.             }
  622.           else if (tolower (*suffix) == 'l')
  623.             {
  624.               putithere->typed_val_float.type
  625.                 = parse_d_type (ps)->builtin_real;
  626.             }
  627.           else if (tolower (*suffix) == 'i')
  628.             {
  629.               putithere->typed_val_float.type
  630.                 = parse_d_type (ps)->builtin_idouble;
  631.             }
  632.           else
  633.             return ERROR;
  634.         }
  635.       else if (suffix_len == 2)
  636.         {
  637.           /* Check suffix for `fi' or `li' (ifloat or ireal).  */
  638.           if (tolower (suffix[0]) == 'f' && tolower (suffix[1] == 'i'))
  639.             {
  640.               putithere->typed_val_float.type
  641.                 = parse_d_type (ps)->builtin_ifloat;
  642.             }
  643.           else if (tolower (suffix[0]) == 'l' && tolower (suffix[1] == 'i'))
  644.             {
  645.               putithere->typed_val_float.type
  646.                 = parse_d_type (ps)->builtin_ireal;
  647.             }
  648.           else
  649.             return ERROR;
  650.         }
  651.       else
  652.         return ERROR;

  653.       return FLOAT_LITERAL;
  654.     }

  655.   /* Handle base-switching prefixes 0x, 0b, 0 */
  656.   if (p[0] == '0')
  657.     switch (p[1])
  658.       {
  659.       case 'x':
  660.       case 'X':
  661.         if (len >= 3)
  662.           {
  663.             p += 2;
  664.             base = 16;
  665.             len -= 2;
  666.           }
  667.         break;

  668.       case 'b':
  669.       case 'B':
  670.         if (len >= 3)
  671.           {
  672.             p += 2;
  673.             base = 2;
  674.             len -= 2;
  675.           }
  676.         break;

  677.       default:
  678.         base = 8;
  679.         break;
  680.       }

  681.   while (len-- > 0)
  682.     {
  683.       c = *p++;
  684.       if (c == '_')
  685.         continue;        /* Ignore embedded '_'.  */
  686.       if (c >= 'A' && c <= 'Z')
  687.         c += 'a' - 'A';
  688.       if (c != 'l' && c != 'u')
  689.         n *= base;
  690.       if (c >= '0' && c <= '9')
  691.         {
  692.           if (found_suffix)
  693.             return ERROR;
  694.           n += i = c - '0';
  695.         }
  696.       else
  697.         {
  698.           if (base > 10 && c >= 'a' && c <= 'f')
  699.             {
  700.               if (found_suffix)
  701.                 return ERROR;
  702.               n += i = c - 'a' + 10;
  703.             }
  704.           else if (c == 'l' && long_p == 0)
  705.             {
  706.               long_p = 1;
  707.               found_suffix = 1;
  708.             }
  709.           else if (c == 'u' && unsigned_p == 0)
  710.             {
  711.               unsigned_p = 1;
  712.               found_suffix = 1;
  713.             }
  714.           else
  715.             return ERROR;        /* Char not a digit */
  716.         }
  717.       if (i >= base)
  718.         return ERROR;                /* Invalid digit in this base.  */
  719.       /* Portably test for integer overflow.  */
  720.       if (c != 'l' && c != 'u')
  721.         {
  722.           ULONGEST n2 = prevn * base;
  723.           if ((n2 / base != prevn) || (n2 + i < prevn))
  724.             error (_("Numeric constant too large."));
  725.         }
  726.       prevn = n;
  727.     }

  728.   /* An integer constant is an int or a long.  An L suffix forces it to
  729.      be long, and a U suffix forces it to be unsigned.  To figure out
  730.      whether it fits, we shift it right and see whether anything remains.
  731.      Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
  732.      more in one operation, because many compilers will warn about such a
  733.      shift (which always produces a zero result).  To deal with the case
  734.      where it is we just always shift the value more than once, with fewer
  735.      bits each time.  */
  736.   un = (ULONGEST) n >> 2;
  737.   if (long_p == 0 && (un >> 30) == 0)
  738.     {
  739.       high_bit = ((ULONGEST) 1) << 31;
  740.       signed_type = parse_d_type (ps)->builtin_int;
  741.       /* For decimal notation, keep the sign of the worked out type.  */
  742.       if (base == 10 && !unsigned_p)
  743.         unsigned_type = parse_d_type (ps)->builtin_long;
  744.       else
  745.         unsigned_type = parse_d_type (ps)->builtin_uint;
  746.     }
  747.   else
  748.     {
  749.       int shift;
  750.       if (sizeof (ULONGEST) * HOST_CHAR_BIT < 64)
  751.         /* A long long does not fit in a LONGEST.  */
  752.         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  753.       else
  754.         shift = 63;
  755.       high_bit = (ULONGEST) 1 << shift;
  756.       signed_type = parse_d_type (ps)->builtin_long;
  757.       unsigned_type = parse_d_type (ps)->builtin_ulong;
  758.     }

  759.   putithere->typed_val_int.val = n;

  760.   /* If the high bit of the worked out type is set then this number
  761.      has to be unsigned_type.  */
  762.   if (unsigned_p || (n & high_bit))
  763.     putithere->typed_val_int.type = unsigned_type;
  764.   else
  765.     putithere->typed_val_int.type = signed_type;

  766.   return INTEGER_LITERAL;
  767. }

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

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

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

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

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

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

  798.   *host_chars = 0;

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

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

  827.   /* FIXME: should instead use own language string_type enum
  828.      and handle D-specific string suffixes here. */
  829.   if (quote == '\'')
  830.     value->type = C_CHAR;
  831.   else
  832.     value->type = C_STRING;

  833.   value->ptr = obstack_base (&tempbuf);
  834.   value->length = obstack_object_size (&tempbuf);

  835.   *outptr = tokptr;

  836.   return quote == '\'' ? CHARACTER_LITERAL : STRING_LITERAL;
  837. }

  838. struct token
  839. {
  840.   char *operator;
  841.   int token;
  842.   enum exp_opcode opcode;
  843. };

  844. static const struct token tokentab3[] =
  845.   {
  846.     {"^^=", ASSIGN_MODIFY, BINOP_EXP},
  847.     {"<<=", ASSIGN_MODIFY, BINOP_LSH},
  848.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  849.   };

  850. static const struct token tokentab2[] =
  851.   {
  852.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  853.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  854.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  855.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  856.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  857.     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
  858.     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
  859.     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
  860.     {"++", INCREMENT, BINOP_END},
  861.     {"--", DECREMENT, BINOP_END},
  862.     {"&&", ANDAND, BINOP_END},
  863.     {"||", OROR, BINOP_END},
  864.     {"^^", HATHAT, BINOP_END},
  865.     {"<<", LSH, BINOP_END},
  866.     {">>", RSH, BINOP_END},
  867.     {"==", EQUAL, BINOP_END},
  868.     {"!=", NOTEQUAL, BINOP_END},
  869.     {"<=", LEQ, BINOP_END},
  870.     {">=", GEQ, BINOP_END},
  871.     {"..", DOTDOT, BINOP_END},
  872.   };

  873. /* Identifier-like tokens.  */
  874. static const struct token ident_tokens[] =
  875.   {
  876.     {"is", IDENTITY, BINOP_END},
  877.     {"!is", NOTIDENTITY, BINOP_END},

  878.     {"cast", CAST_KEYWORD, OP_NULL},
  879.     {"const", CONST_KEYWORD, OP_NULL},
  880.     {"immutable", IMMUTABLE_KEYWORD, OP_NULL},
  881.     {"shared", SHARED_KEYWORD, OP_NULL},
  882.     {"super", SUPER_KEYWORD, OP_NULL},

  883.     {"null", NULL_KEYWORD, OP_NULL},
  884.     {"true", TRUE_KEYWORD, OP_NULL},
  885.     {"false", FALSE_KEYWORD, OP_NULL},

  886.     {"init", INIT_KEYWORD, OP_NULL},
  887.     {"sizeof", SIZEOF_KEYWORD, OP_NULL},
  888.     {"typeof", TYPEOF_KEYWORD, OP_NULL},
  889.     {"typeid", TYPEID_KEYWORD, OP_NULL},

  890.     {"delegate", DELEGATE_KEYWORD, OP_NULL},
  891.     {"function", FUNCTION_KEYWORD, OP_NULL},
  892.     {"struct", STRUCT_KEYWORD, OP_NULL},
  893.     {"union", UNION_KEYWORD, OP_NULL},
  894.     {"class", CLASS_KEYWORD, OP_NULL},
  895.     {"interface", INTERFACE_KEYWORD, OP_NULL},
  896.     {"enum", ENUM_KEYWORD, OP_NULL},
  897.     {"template", TEMPLATE_KEYWORD, OP_NULL},
  898.   };

  899. /* If NAME is a type name in this scope, return it.  */

  900. static struct type *
  901. d_type_from_name (struct stoken name)
  902. {
  903.   struct symbol *sym;
  904.   char *copy = copy_name (name);

  905.   sym = lookup_symbol (copy, expression_context_block,
  906.                        STRUCT_DOMAIN, NULL);
  907.   if (sym != NULL)
  908.     return SYMBOL_TYPE (sym);

  909.   return NULL;
  910. }

  911. /* If NAME is a module name in this scope, return it.  */

  912. static struct type *
  913. d_module_from_name (struct stoken name)
  914. {
  915.   struct symbol *sym;
  916.   char *copy = copy_name (name);

  917.   sym = lookup_symbol (copy, expression_context_block,
  918.                        MODULE_DOMAIN, NULL);
  919.   if (sym != NULL)
  920.     return SYMBOL_TYPE (sym);

  921.   return NULL;
  922. }

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

  925. static int
  926. push_variable (struct parser_state *ps, struct stoken name)
  927. {
  928.   char *copy = copy_name (name);
  929.   struct field_of_this_result is_a_field_of_this;
  930.   struct symbol *sym;
  931.   sym = lookup_symbol (copy, expression_context_block, VAR_DOMAIN,
  932.                        &is_a_field_of_this);
  933.   if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  934.     {
  935.       if (symbol_read_needs_frame (sym))
  936.         {
  937.           if (innermost_block == 0 ||
  938.               contained_in (block_found, innermost_block))
  939.             innermost_block = block_found;
  940.         }

  941.       write_exp_elt_opcode (ps, OP_VAR_VALUE);
  942.       /* We want to use the selected frame, not another more inner frame
  943.          which happens to be in the same block.  */
  944.       write_exp_elt_block (ps, NULL);
  945.       write_exp_elt_sym (ps, sym);
  946.       write_exp_elt_opcode (ps, OP_VAR_VALUE);
  947.       return 1;
  948.     }
  949.   if (is_a_field_of_this.type != NULL)
  950.     {
  951.       /* It hangs off of `this'.  Must not inadvertently convert from a
  952.          method call to data ref.  */
  953.       if (innermost_block == 0 ||
  954.           contained_in (block_found, innermost_block))
  955.         innermost_block = block_found;
  956.       write_exp_elt_opcode (ps, OP_THIS);
  957.       write_exp_elt_opcode (ps, OP_THIS);
  958.       write_exp_elt_opcode (ps, STRUCTOP_PTR);
  959.       write_exp_string (ps, name);
  960.       write_exp_elt_opcode (ps, STRUCTOP_PTR);
  961.       return 1;
  962.     }
  963.   return 0;
  964. }

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

  968. static void
  969. push_fieldnames (struct parser_state *ps, struct stoken name)
  970. {
  971.   int i;
  972.   struct stoken token;
  973.   token.ptr = name.ptr;
  974.   for (i = 0;  ;  i++)
  975.     {
  976.       if (i == name.length || name.ptr[i] == '.')
  977.         {
  978.           /* token.ptr is start of current field name.  */
  979.           token.length = &name.ptr[i] - token.ptr;
  980.           write_exp_elt_opcode (ps, STRUCTOP_PTR);
  981.           write_exp_string (ps, token);
  982.           write_exp_elt_opcode (ps, STRUCTOP_PTR);
  983.           token.ptr += token.length + 1;
  984.         }
  985.       if (i >= name.length)
  986.         break;
  987.     }
  988. }

  989. /* Helper routine for push_expression_name.  Handle a TYPE symbol,
  990.    where DOT_INDEX is the index of the first '.' if NAME is part of
  991.    a qualified type.  */

  992. static void
  993. push_type_name (struct parser_state *ps, struct type *type,
  994.                 struct stoken name, int dot_index)
  995. {
  996.   if (dot_index == name.length)
  997.     {
  998.       write_exp_elt_opcode (ps, OP_TYPE);
  999.       write_exp_elt_type (ps, type);
  1000.       write_exp_elt_opcode (ps, OP_TYPE);
  1001.     }
  1002.   else
  1003.     {
  1004.       struct stoken token;

  1005.       token.ptr = name.ptr;
  1006.       token.length = dot_index;

  1007.       dot_index = 0;

  1008.       while (dot_index < name.length && name.ptr[dot_index] != '.')
  1009.         dot_index++;
  1010.       token.ptr = name.ptr;
  1011.       token.length = dot_index;

  1012.       write_exp_elt_opcode (ps, OP_SCOPE);
  1013.       write_exp_elt_type (ps, type);
  1014.       write_exp_string (ps, token);
  1015.       write_exp_elt_opcode (ps, OP_SCOPE);

  1016.       if (dot_index < name.length)
  1017.         {
  1018.           dot_index++;
  1019.           name.ptr += dot_index;
  1020.           name.length -= dot_index;
  1021.           push_fieldnames (ps, name);
  1022.         }
  1023.     }
  1024. }

  1025. /* Helper routine for push_expression_name.  Like push_type_name,
  1026.    but used when TYPE is a module.  Returns 1 on pushing the symbol.  */

  1027. static int
  1028. push_module_name (struct parser_state *ps, struct type *module,
  1029.                   struct stoken name, int dot_index)
  1030. {
  1031.   if (dot_index == name.length)
  1032.     {
  1033.       write_exp_elt_opcode (ps, OP_TYPE);
  1034.       write_exp_elt_type (ps, module);
  1035.       write_exp_elt_opcode (ps, OP_TYPE);
  1036.       return 1;
  1037.     }
  1038.   else
  1039.     {
  1040.       struct symbol *sym;
  1041.       char *copy;

  1042.       copy = copy_name (name);
  1043.       sym = lookup_symbol_in_static_block (copy, expression_context_block,
  1044.                                            VAR_DOMAIN);
  1045.       if (sym != NULL)
  1046.         sym = lookup_global_symbol (copy, expression_context_block,
  1047.                                     VAR_DOMAIN);

  1048.       if (sym != NULL)
  1049.         {
  1050.           if (SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  1051.             {
  1052.               write_exp_elt_opcode (ps, OP_VAR_VALUE);
  1053.               write_exp_elt_block (ps, NULL);
  1054.               write_exp_elt_sym (ps, sym);
  1055.               write_exp_elt_opcode (ps, OP_VAR_VALUE);
  1056.             }
  1057.           else
  1058.             {
  1059.               write_exp_elt_opcode (ps, OP_TYPE);
  1060.               write_exp_elt_type (ps, SYMBOL_TYPE (sym));
  1061.               write_exp_elt_opcode (ps, OP_TYPE);
  1062.             }
  1063.           return 1;
  1064.         }
  1065.     }

  1066.   return 0;
  1067. }

  1068. /* Handle NAME in an expression (or LHS), which could be a
  1069.    variable, type, or module.  */

  1070. static void
  1071. push_expression_name (struct parser_state *ps, struct stoken name)
  1072. {
  1073.   struct stoken token;
  1074.   struct type *typ;
  1075.   struct bound_minimal_symbol msymbol;
  1076.   char *copy;
  1077.   int doti;

  1078.   /* Handle VAR, which could be local or global.  */
  1079.   if (push_variable (ps, name) != 0)
  1080.     return;

  1081.   /* Handle MODULE.  */
  1082.   typ = d_module_from_name (name);
  1083.   if (typ != NULL)
  1084.     {
  1085.       if (push_module_name (ps, typ, name, name.length) != 0)
  1086.         return;
  1087.     }

  1088.   /* Handle TYPE.  */
  1089.   typ = d_type_from_name (name);
  1090.   if (typ != NULL)
  1091.     {
  1092.       push_type_name (ps, typ, name, name.length);
  1093.       return;
  1094.     }

  1095.   /* Handle VAR.FIELD1..FIELDN.  */
  1096.   for (doti = 0;  doti < name.length;  doti++)
  1097.     {
  1098.       if (name.ptr[doti] == '.')
  1099.         {
  1100.           token.ptr = name.ptr;
  1101.           token.length = doti;

  1102.           if (push_variable (ps, token) != 0)
  1103.             {
  1104.               token.ptr = name.ptr + doti + 1;
  1105.               token.length = name.length - doti - 1;
  1106.               push_fieldnames (ps, token);
  1107.               return;
  1108.             }
  1109.           break;
  1110.         }
  1111.     }

  1112.   /* Continue looking if we found a '.' in the name.  */
  1113.   if (doti < name.length)
  1114.     {
  1115.       token.ptr = name.ptr;
  1116.       for (;;)
  1117.         {
  1118.           token.length = doti;

  1119.           /* Handle PACKAGE.MODULE.  */
  1120.           typ = d_module_from_name (token);
  1121.           if (typ != NULL)
  1122.             {
  1123.               if (push_module_name (ps, typ, name, doti) != 0)
  1124.                 return;
  1125.             }
  1126.           /* Handle TYPE.FIELD1..FIELDN.  */
  1127.           typ = d_type_from_name (token);
  1128.           if (typ != NULL)
  1129.             {
  1130.               push_type_name (ps, typ, name, doti);
  1131.               return;
  1132.             }

  1133.           if (doti >= name.length)
  1134.             break;
  1135.           doti++;   /* Skip '.'  */
  1136.           while (doti < name.length && name.ptr[doti] != '.')
  1137.             doti++;
  1138.         }
  1139.     }

  1140.   /* Lookup foreign name in global static symbols.  */
  1141.   copy  = copy_name (name);
  1142.   msymbol = lookup_bound_minimal_symbol (copy);
  1143.   if (msymbol.minsym != NULL)
  1144.     write_exp_msymbol (ps, msymbol);
  1145.   else if (!have_full_symbols () && !have_partial_symbols ())
  1146.     error (_("No symbol table is loaded.  Use the \"file\" command"));
  1147.   else
  1148.     error (_("No symbol \"%s\" in current context."), copy);
  1149. }

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

  1154. /* This is set if the previously-returned token was a structure operator.
  1155.    This is used only when parsing to do field name completion.  */
  1156. static int last_was_structop;

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

  1158. static int
  1159. yylex (void)
  1160. {
  1161.   int c;
  1162.   int namelen;
  1163.   unsigned int i;
  1164.   const char *tokstart;
  1165.   int saw_structop = last_was_structop;
  1166.   char *copy;

  1167.   last_was_structop = 0;

  1168. retry:

  1169.   prev_lexptr = lexptr;

  1170.   tokstart = lexptr;
  1171.   /* See if it is a special token of length 3.  */
  1172.   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  1173.     if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
  1174.       {
  1175.         lexptr += 3;
  1176.         yylval.opcode = tokentab3[i].opcode;
  1177.         return tokentab3[i].token;
  1178.       }

  1179.   /* See if it is a special token of length 2.  */
  1180.   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  1181.     if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
  1182.       {
  1183.         lexptr += 2;
  1184.         yylval.opcode = tokentab2[i].opcode;
  1185.         return tokentab2[i].token;
  1186.       }

  1187.   switch (c = *tokstart)
  1188.     {
  1189.     case 0:
  1190.       /* If we're parsing for field name completion, and the previous
  1191.          token allows such completion, return a COMPLETE token.
  1192.          Otherwise, we were already scanning the original text, and
  1193.          we're really done.  */
  1194.       if (saw_name_at_eof)
  1195.         {
  1196.           saw_name_at_eof = 0;
  1197.           return COMPLETE;
  1198.         }
  1199.       else if (saw_structop)
  1200.         return COMPLETE;
  1201.       else
  1202.         return 0;

  1203.     case ' ':
  1204.     case '\t':
  1205.     case '\n':
  1206.       lexptr++;
  1207.       goto retry;

  1208.     case '[':
  1209.     case '(':
  1210.       paren_depth++;
  1211.       lexptr++;
  1212.       return c;

  1213.     case ']':
  1214.     case ')':
  1215.       if (paren_depth == 0)
  1216.         return 0;
  1217.       paren_depth--;
  1218.       lexptr++;
  1219.       return c;

  1220.     case ',':
  1221.       if (comma_terminates && paren_depth == 0)
  1222.         return 0;
  1223.       lexptr++;
  1224.       return c;

  1225.     case '.':
  1226.       /* Might be a floating point number.  */
  1227.       if (lexptr[1] < '0' || lexptr[1] > '9')
  1228.         {
  1229.           if (parse_completion)
  1230.             last_was_structop = 1;
  1231.           goto symbol;                /* Nope, must be a symbol.  */
  1232.         }
  1233.       /* FALL THRU into number case.  */

  1234.     case '0':
  1235.     case '1':
  1236.     case '2':
  1237.     case '3':
  1238.     case '4':
  1239.     case '5':
  1240.     case '6':
  1241.     case '7':
  1242.     case '8':
  1243.     case '9':
  1244.       {
  1245.         /* It's a number.  */
  1246.         int got_dot = 0, got_e = 0, toktype;
  1247.         const char *p = tokstart;
  1248.         int hex = input_radix > 10;

  1249.         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1250.           {
  1251.             p += 2;
  1252.             hex = 1;
  1253.           }

  1254.         for (;; ++p)
  1255.           {
  1256.             /* Hex exponents start with 'p', because 'e' is a valid hex
  1257.                digit and thus does not indicate a floating point number
  1258.                when the radix is hex.  */
  1259.             if ((!hex && !got_e && tolower (p[0]) == 'e')
  1260.                 || (hex && !got_e && tolower (p[0] == 'p')))
  1261.               got_dot = got_e = 1;
  1262.             /* A '.' always indicates a decimal floating point number
  1263.                regardless of the radix.  If we have a '..' then its the
  1264.                end of the number and the beginning of a slice.  */
  1265.             else if (!got_dot && (p[0] == '.' && p[1] != '.'))
  1266.                 got_dot = 1;
  1267.             /* This is the sign of the exponent, not the end of the number.  */
  1268.             else if (got_e && (tolower (p[-1]) == 'e' || tolower (p[-1]) == 'p')
  1269.                      && (*p == '-' || *p == '+'))
  1270.               continue;
  1271.             /* We will take any letters or digits, ignoring any embedded '_'.
  1272.                parse_number will complain if past the radix, or if L or U are
  1273.                not final.  */
  1274.             else if ((*p < '0' || *p > '9') && (*p != '_') &&
  1275.                      ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z')))
  1276.               break;
  1277.           }

  1278.         toktype = parse_number (pstate, tokstart, p - tokstart,
  1279.                                 got_dot|got_e, &yylval);
  1280.         if (toktype == ERROR)
  1281.           {
  1282.             char *err_copy = (char *) alloca (p - tokstart + 1);

  1283.             memcpy (err_copy, tokstart, p - tokstart);
  1284.             err_copy[p - tokstart] = 0;
  1285.             error (_("Invalid number \"%s\"."), err_copy);
  1286.           }
  1287.         lexptr = p;
  1288.         return toktype;
  1289.       }

  1290.     case '@':
  1291.       {
  1292.         const char *p = &tokstart[1];
  1293.         size_t len = strlen ("entry");

  1294.         while (isspace (*p))
  1295.           p++;
  1296.         if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
  1297.             && p[len] != '_')
  1298.           {
  1299.             lexptr = &p[len];
  1300.             return ENTRY;
  1301.           }
  1302.       }
  1303.       /* FALLTHRU */
  1304.     case '+':
  1305.     case '-':
  1306.     case '*':
  1307.     case '/':
  1308.     case '%':
  1309.     case '|':
  1310.     case '&':
  1311.     case '^':
  1312.     case '~':
  1313.     case '!':
  1314.     case '<':
  1315.     case '>':
  1316.     case '?':
  1317.     case ':':
  1318.     case '=':
  1319.     case '{':
  1320.     case '}':
  1321.     symbol:
  1322.       lexptr++;
  1323.       return c;

  1324.     case '\'':
  1325.     case '"':
  1326.     case '`':
  1327.       {
  1328.         int host_len;
  1329.         int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
  1330.                                            &host_len);
  1331.         if (result == CHARACTER_LITERAL)
  1332.           {
  1333.             if (host_len == 0)
  1334.               error (_("Empty character constant."));
  1335.             else if (host_len > 2 && c == '\'')
  1336.               {
  1337.                 ++tokstart;
  1338.                 namelen = lexptr - tokstart - 1;
  1339.                 goto tryname;
  1340.               }
  1341.             else if (host_len > 1)
  1342.               error (_("Invalid character constant."));
  1343.           }
  1344.         return result;
  1345.       }
  1346.     }

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

  1351.   /* It's a name.  See how long it is.  */
  1352.   namelen = 0;
  1353.   for (c = tokstart[namelen];
  1354.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1355.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
  1356.     c = tokstart[++namelen];

  1357.   /* The token "if" terminates the expression and is NOT
  1358.      removed from the input stream.  */
  1359.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1360.     return 0;

  1361.   /* For the same reason (breakpoint conditions), "thread N"
  1362.      terminates the expression.  "thread" could be an identifier, but
  1363.      an identifier is never followed by a number without intervening
  1364.      punctuation.  "task" is similar.  Handle abbreviations of these,
  1365.      similarly to breakpoint.c:find_condition_and_thread.  */
  1366.   if (namelen >= 1
  1367.       && (strncmp (tokstart, "thread", namelen) == 0
  1368.           || strncmp (tokstart, "task", namelen) == 0)
  1369.       && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
  1370.     {
  1371.       const char *p = tokstart + namelen + 1;

  1372.       while (*p == ' ' || *p == '\t')
  1373.         p++;
  1374.       if (*p >= '0' && *p <= '9')
  1375.         return 0;
  1376.     }

  1377.   lexptr += namelen;

  1378. tryname:

  1379.   yylval.sval.ptr = tokstart;
  1380.   yylval.sval.length = namelen;

  1381.   /* Catch specific keywords.  */
  1382.   copy = copy_name (yylval.sval);
  1383.   for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
  1384.     if (strcmp (copy, ident_tokens[i].operator) == 0)
  1385.       {
  1386.         /* It is ok to always set this, even though we don't always
  1387.            strictly need to.  */
  1388.         yylval.opcode = ident_tokens[i].opcode;
  1389.         return ident_tokens[i].token;
  1390.       }

  1391.   if (*tokstart == '$')
  1392.     return DOLLAR_VARIABLE;

  1393.   yylval.tsym.type
  1394.     = language_lookup_primitive_type (parse_language (pstate),
  1395.                                       parse_gdbarch (pstate), copy);
  1396.   if (yylval.tsym.type != NULL)
  1397.     return TYPENAME;

  1398.   /* Input names that aren't symbols but ARE valid hex numbers,
  1399.      when the input radix permits them, can be names or numbers
  1400.      depending on the parse.  Note we support radixes > 16 here.  */
  1401.   if ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1402.       || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))
  1403.     {
  1404.       YYSTYPE newlval;        /* Its value is ignored.  */
  1405.       int hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
  1406.       if (hextype == INTEGER_LITERAL)
  1407.         return NAME_OR_INT;
  1408.     }

  1409.   if (parse_completion && *lexptr == '\0')
  1410.     saw_name_at_eof = 1;

  1411.   return IDENTIFIER;
  1412. }

  1413. int
  1414. d_parse (struct parser_state *par_state)
  1415. {
  1416.   int result;
  1417.   struct cleanup *back_to;

  1418.   /* Setting up the parser state.  */
  1419.   gdb_assert (par_state != NULL);
  1420.   pstate = par_state;

  1421.   back_to = make_cleanup (null_cleanup, NULL);

  1422.   make_cleanup_restore_integer (&yydebug);
  1423.   make_cleanup_clear_parser_state (&pstate);
  1424.   yydebug = parser_debug;

  1425.   /* Initialize some state used by the lexer.  */
  1426.   last_was_structop = 0;
  1427.   saw_name_at_eof = 0;

  1428.   result = yyparse ();
  1429.   do_cleanups (back_to);
  1430.   return result;
  1431. }

  1432. void
  1433. yyerror (char *msg)
  1434. {
  1435.   if (prev_lexptr)
  1436.     lexptr = prev_lexptr;

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