gdb/p-exp.y - gdb

  1. /* YACC parser for Pascal expressions, for GDB.
  2.    Copyright (C) 2000-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 */

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

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

  30. /* Known bugs or limitations:
  31.     - pascal string operations are not supported at all.
  32.     - there are some problems with boolean types.
  33.     - Pascal type hexadecimal constants are not supported
  34.       because they conflict with the internal variables format.
  35.    Probably also lots of other problems, less well defined PM.  */
  36. %{

  37. #include "defs.h"
  38. #include <ctype.h>
  39. #include "expression.h"
  40. #include "value.h"
  41. #include "parser-defs.h"
  42. #include "language.h"
  43. #include "p-lang.h"
  44. #include "bfd.h" /* Required by objfiles.h.  */
  45. #include "symfile.h" /* Required by objfiles.h.  */
  46. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols.  */
  47. #include "block.h"
  48. #include "completer.h"

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

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

  56. #define        yymaxdepth pascal_maxdepth
  57. #define        yyparse        pascal_parse_internal
  58. #define        yylex        pascal_lex
  59. #define        yyerror        pascal_error
  60. #define        yylval        pascal_lval
  61. #define        yychar        pascal_char
  62. #define        yydebug        pascal_debug
  63. #define        yypact        pascal_pact
  64. #define        yyr1        pascal_r1
  65. #define        yyr2        pascal_r2
  66. #define        yydef        pascal_def
  67. #define        yychk        pascal_chk
  68. #define        yypgo        pascal_pgo
  69. #define        yyact        pascal_act
  70. #define        yyexca        pascal_exca
  71. #define yyerrflag pascal_errflag
  72. #define yynerrs        pascal_nerrs
  73. #define        yyps        pascal_ps
  74. #define        yypv        pascal_pv
  75. #define        yys        pascal_s
  76. #define        yy_yys        pascal_yys
  77. #define        yystate        pascal_state
  78. #define        yytmp        pascal_tmp
  79. #define        yyv        pascal_v
  80. #define        yy_yyv        pascal_yyv
  81. #define        yyval        pascal_val
  82. #define        yylloc        pascal_lloc
  83. #define yyreds        pascal_reds                /* With YYDEBUG defined */
  84. #define yytoks        pascal_toks                /* With YYDEBUG defined */
  85. #define yyname        pascal_name                /* With YYDEBUG defined */
  86. #define yyrule        pascal_rule                /* With YYDEBUG defined */
  87. #define yylhs        pascal_yylhs
  88. #define yylen        pascal_yylen
  89. #define yydefred pascal_yydefred
  90. #define yydgoto        pascal_yydgoto
  91. #define yysindex pascal_yysindex
  92. #define yyrindex pascal_yyrindex
  93. #define yygindex pascal_yygindex
  94. #define yytable         pascal_yytable
  95. #define yycheck         pascal_yycheck
  96. #define yyss        pascal_yyss
  97. #define yysslim        pascal_yysslim
  98. #define yyssp        pascal_yyssp
  99. #define yystacksize pascal_yystacksize
  100. #define yyvs        pascal_yyvs
  101. #define yyvsp        pascal_yyvsp

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

  105. #define YYFPRINTF parser_fprintf

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

  108. static struct parser_state *pstate = NULL;

  109. int yyparse (void);

  110. static int yylex (void);

  111. void yyerror (char *);

  112. static char *uptok (const char *, int);
  113. %}

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

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

  137.     struct type **tvec;
  138.     int *ivec;
  139.   }

  140. %{
  141. /* YYSTYPE gets defined by %union */
  142. static int parse_number (struct parser_state *,
  143.                          const char *, int, int, YYSTYPE *);

  144. static struct type *current_type;
  145. static struct internalvar *intvar;
  146. static int leftdiv_is_integer;
  147. static void push_current_type (void);
  148. static void pop_current_type (void);
  149. static int search_field;
  150. %}

  151. %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
  152. %type <tval> type typebase
  153. /* %type <bval> block */

  154. /* Fancy type parsing.  */
  155. %type <tval> ptype

  156. %token <typed_val_int> INT
  157. %token <typed_val_float> FLOAT

  158. /* Both NAME and TYPENAME tokens represent symbols in the input,
  159.    and both convey their data as strings.
  160.    But a TYPENAME is a string that happens to be defined as a typedef
  161.    or builtin type name (such as int or char)
  162.    and a NAME is any other symbol.
  163.    Contexts where this distinction is not important can use the
  164.    nonterminal "name", which matches either NAME or TYPENAME.  */

  165. %token <sval> STRING
  166. %token <sval> FIELDNAME
  167. %token <voidval> COMPLETE
  168. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence.  */
  169. %token <tsym> TYPENAME
  170. %type <sval> name
  171. %type <ssym> name_not_typename

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

  176. %token <ssym> NAME_OR_INT

  177. %token STRUCT CLASS SIZEOF COLONCOLON
  178. %token ERROR

  179. /* Special type cases, put in to allow the parser to distinguish different
  180.    legal basetypes.  */

  181. %token <voidval> VARIABLE


  182. /* Object pascal */
  183. %token THIS
  184. %token <lval> TRUEKEYWORD FALSEKEYWORD

  185. %left ','
  186. %left ABOVE_COMMA
  187. %right ASSIGN
  188. %left NOT
  189. %left OR
  190. %left XOR
  191. %left ANDAND
  192. %left '=' NOTEQUAL
  193. %left '<' '>' LEQ GEQ
  194. %left LSH RSH DIV MOD
  195. %left '@'
  196. %left '+' '-'
  197. %left '*' '/'
  198. %right UNARY INCREMENT DECREMENT
  199. %right ARROW '.' '[' '('
  200. %left '^'
  201. %token <ssym> BLOCKNAME
  202. %type <bval> block
  203. %left COLONCOLON


  204. %%

  205. start   :        { current_type = NULL;
  206.                   intvar = NULL;
  207.                   search_field = 0;
  208.                   leftdiv_is_integer = 0;
  209.                 }
  210.                 normal_start {}
  211.         ;

  212. normal_start        :
  213.                 exp1
  214.         |        type_exp
  215.         ;

  216. type_exp:        type
  217.                         { write_exp_elt_opcode (pstate, OP_TYPE);
  218.                           write_exp_elt_type (pstate, $1);
  219.                           write_exp_elt_opcode (pstate, OP_TYPE);
  220.                           current_type = $1; } ;

  221. /* Expressions, including the comma operator.  */
  222. exp1        :        exp
  223.         |        exp1 ',' exp
  224.                         { write_exp_elt_opcode (pstate, BINOP_COMMA); }
  225.         ;

  226. /* Expressions, not including the comma operator.  */
  227. exp        :        exp '^'   %prec UNARY
  228.                         { write_exp_elt_opcode (pstate, UNOP_IND);
  229.                           if (current_type)
  230.                             current_type = TYPE_TARGET_TYPE (current_type); }
  231.         ;

  232. exp        :        '@' exp    %prec UNARY
  233.                         { write_exp_elt_opcode (pstate, UNOP_ADDR);
  234.                           if (current_type)
  235.                             current_type = TYPE_POINTER_TYPE (current_type); }
  236.         ;

  237. exp        :        '-' exp    %prec UNARY
  238.                         { write_exp_elt_opcode (pstate, UNOP_NEG); }
  239.         ;

  240. exp        :        NOT exp    %prec UNARY
  241.                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  242.         ;

  243. exp        :        INCREMENT '(' exp ')'   %prec UNARY
  244.                         { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
  245.         ;

  246. exp        :        DECREMENT  '(' exp ')'   %prec UNARY
  247.                         { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
  248.         ;


  249. field_exp        :        exp '.'        %prec UNARY
  250.                         { search_field = 1; }
  251.         ;

  252. exp        :        field_exp FIELDNAME
  253.                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  254.                           write_exp_string (pstate, $2);
  255.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  256.                           search_field = 0;
  257.                           if (current_type)
  258.                             {
  259.                               while (TYPE_CODE (current_type)
  260.                                      == TYPE_CODE_PTR)
  261.                                 current_type =
  262.                                   TYPE_TARGET_TYPE (current_type);
  263.                               current_type = lookup_struct_elt_type (
  264.                                 current_type, $2.ptr, 0);
  265.                             }
  266.                          }
  267.         ;


  268. exp        :        field_exp name
  269.                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  270.                           write_exp_string (pstate, $2);
  271.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  272.                           search_field = 0;
  273.                           if (current_type)
  274.                             {
  275.                               while (TYPE_CODE (current_type)
  276.                                      == TYPE_CODE_PTR)
  277.                                 current_type =
  278.                                   TYPE_TARGET_TYPE (current_type);
  279.                               current_type = lookup_struct_elt_type (
  280.                                 current_type, $2.ptr, 0);
  281.                             }
  282.                         }
  283.         ;
  284. exp        :        field_exp  name COMPLETE
  285.                         { mark_struct_expression (pstate);
  286.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  287.                           write_exp_string (pstate, $2);
  288.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  289.         ;
  290. exp        :        field_exp COMPLETE
  291.                         { struct stoken s;
  292.                           mark_struct_expression (pstate);
  293.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  294.                           s.ptr = "";
  295.                           s.length = 0;
  296.                           write_exp_string (pstate, s);
  297.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  298.         ;

  299. exp        :        exp '['
  300.                         /* We need to save the current_type value.  */
  301.                         { const char *arrayname;
  302.                           int arrayfieldindex;
  303.                           arrayfieldindex = is_pascal_string_type (
  304.                                 current_type, NULL, NULL,
  305.                                 NULL, NULL, &arrayname);
  306.                           if (arrayfieldindex)
  307.                             {
  308.                               struct stoken stringsval;
  309.                               char *buf;

  310.                               buf = alloca (strlen (arrayname) + 1);
  311.                               stringsval.ptr = buf;
  312.                               stringsval.length = strlen (arrayname);
  313.                               strcpy (buf, arrayname);
  314.                               current_type = TYPE_FIELD_TYPE (current_type,
  315.                                 arrayfieldindex - 1);
  316.                               write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  317.                               write_exp_string (pstate, stringsval);
  318.                               write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  319.                             }
  320.                           push_current_type ();  }
  321.                 exp1 ']'
  322.                         { pop_current_type ();
  323.                           write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
  324.                           if (current_type)
  325.                             current_type = TYPE_TARGET_TYPE (current_type); }
  326.         ;

  327. exp        :        exp '('
  328.                         /* This is to save the value of arglist_len
  329.                            being accumulated by an outer function call.  */
  330.                         { push_current_type ();
  331.                           start_arglist (); }
  332.                 arglist ')'        %prec ARROW
  333.                         { write_exp_elt_opcode (pstate, OP_FUNCALL);
  334.                           write_exp_elt_longcst (pstate,
  335.                                                  (LONGEST) end_arglist ());
  336.                           write_exp_elt_opcode (pstate, OP_FUNCALL);
  337.                           pop_current_type ();
  338.                           if (current_type)
  339.                                current_type = TYPE_TARGET_TYPE (current_type);
  340.                         }
  341.         ;

  342. arglist        :
  343.          | exp
  344.                         { arglist_len = 1; }
  345.          | arglist ',' exp   %prec ABOVE_COMMA
  346.                         { arglist_len++; }
  347.         ;

  348. exp        :        type '(' exp ')' %prec UNARY
  349.                         { if (current_type)
  350.                             {
  351.                               /* Allow automatic dereference of classes.  */
  352.                               if ((TYPE_CODE (current_type) == TYPE_CODE_PTR)
  353.                                   && (TYPE_CODE (TYPE_TARGET_TYPE (current_type)) == TYPE_CODE_STRUCT)
  354.                                   && (TYPE_CODE ($1) == TYPE_CODE_STRUCT))
  355.                                 write_exp_elt_opcode (pstate, UNOP_IND);
  356.                             }
  357.                           write_exp_elt_opcode (pstate, UNOP_CAST);
  358.                           write_exp_elt_type (pstate, $1);
  359.                           write_exp_elt_opcode (pstate, UNOP_CAST);
  360.                           current_type = $1; }
  361.         ;

  362. exp        :        '(' exp1 ')'
  363.                         { }
  364.         ;

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

  366. exp        :        exp '*' exp
  367.                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
  368.         ;

  369. exp        :        exp '/' {
  370.                           if (current_type && is_integral_type (current_type))
  371.                             leftdiv_is_integer = 1;
  372.                         }
  373.                 exp
  374.                         {
  375.                           if (leftdiv_is_integer && current_type
  376.                               && is_integral_type (current_type))
  377.                             {
  378.                               write_exp_elt_opcode (pstate, UNOP_CAST);
  379.                               write_exp_elt_type (pstate,
  380.                                                   parse_type (pstate)
  381.                                                   ->builtin_long_double);
  382.                               current_type
  383.                                 = parse_type (pstate)->builtin_long_double;
  384.                               write_exp_elt_opcode (pstate, UNOP_CAST);
  385.                               leftdiv_is_integer = 0;
  386.                             }

  387.                           write_exp_elt_opcode (pstate, BINOP_DIV);
  388.                         }
  389.         ;

  390. exp        :        exp DIV exp
  391.                         { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
  392.         ;

  393. exp        :        exp MOD exp
  394.                         { write_exp_elt_opcode (pstate, BINOP_REM); }
  395.         ;

  396. exp        :        exp '+' exp
  397.                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
  398.         ;

  399. exp        :        exp '-' exp
  400.                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
  401.         ;

  402. exp        :        exp LSH exp
  403.                         { write_exp_elt_opcode (pstate, BINOP_LSH); }
  404.         ;

  405. exp        :        exp RSH exp
  406.                         { write_exp_elt_opcode (pstate, BINOP_RSH); }
  407.         ;

  408. exp        :        exp '=' exp
  409.                         { write_exp_elt_opcode (pstate, BINOP_EQUAL);
  410.                           current_type = parse_type (pstate)->builtin_bool;
  411.                         }
  412.         ;

  413. exp        :        exp NOTEQUAL exp
  414.                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL);
  415.                           current_type = parse_type (pstate)->builtin_bool;
  416.                         }
  417.         ;

  418. exp        :        exp LEQ exp
  419.                         { write_exp_elt_opcode (pstate, BINOP_LEQ);
  420.                           current_type = parse_type (pstate)->builtin_bool;
  421.                         }
  422.         ;

  423. exp        :        exp GEQ exp
  424.                         { write_exp_elt_opcode (pstate, BINOP_GEQ);
  425.                           current_type = parse_type (pstate)->builtin_bool;
  426.                         }
  427.         ;

  428. exp        :        exp '<' exp
  429.                         { write_exp_elt_opcode (pstate, BINOP_LESS);
  430.                           current_type = parse_type (pstate)->builtin_bool;
  431.                         }
  432.         ;

  433. exp        :        exp '>' exp
  434.                         { write_exp_elt_opcode (pstate, BINOP_GTR);
  435.                           current_type = parse_type (pstate)->builtin_bool;
  436.                         }
  437.         ;

  438. exp        :        exp ANDAND exp
  439.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  440.         ;

  441. exp        :        exp XOR exp
  442.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  443.         ;

  444. exp        :        exp OR exp
  445.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  446.         ;

  447. exp        :        exp ASSIGN exp
  448.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  449.         ;

  450. exp        :        TRUEKEYWORD
  451.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  452.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  453.                           current_type = parse_type (pstate)->builtin_bool;
  454.                           write_exp_elt_opcode (pstate, OP_BOOL); }
  455.         ;

  456. exp        :        FALSEKEYWORD
  457.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  458.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  459.                           current_type = parse_type (pstate)->builtin_bool;
  460.                           write_exp_elt_opcode (pstate, OP_BOOL); }
  461.         ;

  462. exp        :        INT
  463.                         { write_exp_elt_opcode (pstate, OP_LONG);
  464.                           write_exp_elt_type (pstate, $1.type);
  465.                           current_type = $1.type;
  466.                           write_exp_elt_longcst (pstate, (LONGEST)($1.val));
  467.                           write_exp_elt_opcode (pstate, OP_LONG); }
  468.         ;

  469. exp        :        NAME_OR_INT
  470.                         { YYSTYPE val;
  471.                           parse_number (pstate, $1.stoken.ptr,
  472.                                         $1.stoken.length, 0, &val);
  473.                           write_exp_elt_opcode (pstate, OP_LONG);
  474.                           write_exp_elt_type (pstate, val.typed_val_int.type);
  475.                           current_type = val.typed_val_int.type;
  476.                           write_exp_elt_longcst (pstate, (LONGEST)
  477.                                                  val.typed_val_int.val);
  478.                           write_exp_elt_opcode (pstate, OP_LONG);
  479.                         }
  480.         ;


  481. exp        :        FLOAT
  482.                         { write_exp_elt_opcode (pstate, OP_DOUBLE);
  483.                           write_exp_elt_type (pstate, $1.type);
  484.                           current_type = $1.type;
  485.                           write_exp_elt_dblcst (pstate, $1.dval);
  486.                           write_exp_elt_opcode (pstate, OP_DOUBLE); }
  487.         ;

  488. exp        :        variable
  489.         ;

  490. exp        :        VARIABLE
  491.                         /* Already written by write_dollar_variable.
  492.                            Handle current_type.  */
  493.                          {  if (intvar) {
  494.                               struct value * val, * mark;

  495.                              mark = value_mark ();
  496.                               val = value_of_internalvar (parse_gdbarch (pstate),
  497.                                                           intvar);
  498.                               current_type = value_type (val);
  499.                              value_release_to_mark (mark);
  500.                             }
  501.                          }
  502.          ;

  503. exp        :        SIZEOF '(' type ')'        %prec UNARY
  504.                         { write_exp_elt_opcode (pstate, OP_LONG);
  505.                           write_exp_elt_type (pstate,
  506.                                             parse_type (pstate)->builtin_int);
  507.                           current_type = parse_type (pstate)->builtin_int;
  508.                           CHECK_TYPEDEF ($3);
  509.                           write_exp_elt_longcst (pstate,
  510.                                                  (LONGEST) TYPE_LENGTH ($3));
  511.                           write_exp_elt_opcode (pstate, OP_LONG); }
  512.         ;

  513. exp        :        SIZEOF  '(' exp ')'      %prec UNARY
  514.                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF);
  515.                           current_type = parse_type (pstate)->builtin_int; }

  516. exp        :        STRING
  517.                         { /* C strings are converted into array constants with
  518.                              an explicit null byte added at the end.  Thus
  519.                              the array upper bound is the string length.
  520.                              There is no such thing in C as a completely empty
  521.                              string.  */
  522.                           const char *sp = $1.ptr; int count = $1.length;

  523.                           while (count-- > 0)
  524.                             {
  525.                               write_exp_elt_opcode (pstate, OP_LONG);
  526.                               write_exp_elt_type (pstate,
  527.                                                   parse_type (pstate)
  528.                                                   ->builtin_char);
  529.                               write_exp_elt_longcst (pstate,
  530.                                                      (LONGEST) (*sp++));
  531.                               write_exp_elt_opcode (pstate, OP_LONG);
  532.                             }
  533.                           write_exp_elt_opcode (pstate, OP_LONG);
  534.                           write_exp_elt_type (pstate,
  535.                                               parse_type (pstate)
  536.                                               ->builtin_char);
  537.                           write_exp_elt_longcst (pstate, (LONGEST)'\0');
  538.                           write_exp_elt_opcode (pstate, OP_LONG);
  539.                           write_exp_elt_opcode (pstate, OP_ARRAY);
  540.                           write_exp_elt_longcst (pstate, (LONGEST) 0);
  541.                           write_exp_elt_longcst (pstate,
  542.                                                  (LONGEST) ($1.length));
  543.                           write_exp_elt_opcode (pstate, OP_ARRAY); }
  544.         ;

  545. /* Object pascal  */
  546. exp        :        THIS
  547.                         {
  548.                           struct value * this_val;
  549.                           struct type * this_type;
  550.                           write_exp_elt_opcode (pstate, OP_THIS);
  551.                           write_exp_elt_opcode (pstate, OP_THIS);
  552.                           /* We need type of this.  */
  553.                           this_val
  554.                             = value_of_this_silent (parse_language (pstate));
  555.                           if (this_val)
  556.                             this_type = value_type (this_val);
  557.                           else
  558.                             this_type = NULL;
  559.                           if (this_type)
  560.                             {
  561.                               if (TYPE_CODE (this_type) == TYPE_CODE_PTR)
  562.                                 {
  563.                                   this_type = TYPE_TARGET_TYPE (this_type);
  564.                                   write_exp_elt_opcode (pstate, UNOP_IND);
  565.                                 }
  566.                             }

  567.                           current_type = this_type;
  568.                         }
  569.         ;

  570. /* end of object pascal.  */

  571. block        :        BLOCKNAME
  572.                         {
  573.                           if ($1.sym != 0)
  574.                               $$ = SYMBOL_BLOCK_VALUE ($1.sym);
  575.                           else
  576.                             {
  577.                               struct symtab *tem =
  578.                                   lookup_symtab (copy_name ($1.stoken));
  579.                               if (tem)
  580.                                 $$ = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (tem),
  581.                                                         STATIC_BLOCK);
  582.                               else
  583.                                 error (_("No file or function \"%s\"."),
  584.                                        copy_name ($1.stoken));
  585.                             }
  586.                         }
  587.         ;

  588. block        :        block COLONCOLON name
  589.                         { struct symbol *tem
  590.                             = lookup_symbol (copy_name ($3), $1,
  591.                                              VAR_DOMAIN, NULL);
  592.                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  593.                             error (_("No function \"%s\" in specified context."),
  594.                                    copy_name ($3));
  595.                           $$ = SYMBOL_BLOCK_VALUE (tem); }
  596.         ;

  597. variable:        block COLONCOLON name
  598.                         { struct symbol *sym;
  599.                           sym = lookup_symbol (copy_name ($3), $1,
  600.                                                VAR_DOMAIN, NULL);
  601.                           if (sym == 0)
  602.                             error (_("No symbol \"%s\" in specified context."),
  603.                                    copy_name ($3));

  604.                           write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  605.                           /* block_found is set by lookup_symbol.  */
  606.                           write_exp_elt_block (pstate, block_found);
  607.                           write_exp_elt_sym (pstate, sym);
  608.                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
  609.         ;

  610. qualified_name:        typebase COLONCOLON name
  611.                         {
  612.                           struct type *type = $1;
  613.                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  614.                               && TYPE_CODE (type) != TYPE_CODE_UNION)
  615.                             error (_("`%s' is not defined as an aggregate type."),
  616.                                    TYPE_NAME (type));

  617.                           write_exp_elt_opcode (pstate, OP_SCOPE);
  618.                           write_exp_elt_type (pstate, type);
  619.                           write_exp_string (pstate, $3);
  620.                           write_exp_elt_opcode (pstate, OP_SCOPE);
  621.                         }
  622.         ;

  623. variable:        qualified_name
  624.         |        COLONCOLON name
  625.                         {
  626.                           char *name = copy_name ($2);
  627.                           struct symbol *sym;
  628.                           struct bound_minimal_symbol msymbol;

  629.                           sym =
  630.                             lookup_symbol (name, (const struct block *) NULL,
  631.                                            VAR_DOMAIN, NULL);
  632.                           if (sym)
  633.                             {
  634.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  635.                               write_exp_elt_block (pstate, NULL);
  636.                               write_exp_elt_sym (pstate, sym);
  637.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  638.                               break;
  639.                             }

  640.                           msymbol = lookup_bound_minimal_symbol (name);
  641.                           if (msymbol.minsym != NULL)
  642.                             write_exp_msymbol (pstate, msymbol);
  643.                           else if (!have_full_symbols ()
  644.                                    && !have_partial_symbols ())
  645.                             error (_("No symbol table is loaded.  "
  646.                                    "Use the \"file\" command."));
  647.                           else
  648.                             error (_("No symbol \"%s\" in current context."),
  649.                                    name);
  650.                         }
  651.         ;

  652. variable:        name_not_typename
  653.                         { struct symbol *sym = $1.sym;

  654.                           if (sym)
  655.                             {
  656.                               if (symbol_read_needs_frame (sym))
  657.                                 {
  658.                                   if (innermost_block == 0
  659.                                       || contained_in (block_found,
  660.                                                        innermost_block))
  661.                                     innermost_block = block_found;
  662.                                 }

  663.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  664.                               /* We want to use the selected frame, not
  665.                                  another more inner frame which happens to
  666.                                  be in the same block.  */
  667.                               write_exp_elt_block (pstate, NULL);
  668.                               write_exp_elt_sym (pstate, sym);
  669.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  670.                               current_type = sym->type; }
  671.                           else if ($1.is_a_field_of_this)
  672.                             {
  673.                               struct value * this_val;
  674.                               struct type * this_type;
  675.                               /* Object pascal: it hangs off of `this'.  Must
  676.                                  not inadvertently convert from a method call
  677.                                  to data ref.  */
  678.                               if (innermost_block == 0
  679.                                   || contained_in (block_found,
  680.                                                    innermost_block))
  681.                                 innermost_block = block_found;
  682.                               write_exp_elt_opcode (pstate, OP_THIS);
  683.                               write_exp_elt_opcode (pstate, OP_THIS);
  684.                               write_exp_elt_opcode (pstate, STRUCTOP_PTR);
  685.                               write_exp_string (pstate, $1.stoken);
  686.                               write_exp_elt_opcode (pstate, STRUCTOP_PTR);
  687.                               /* We need type of this.  */
  688.                               this_val
  689.                                 = value_of_this_silent (parse_language (pstate));
  690.                               if (this_val)
  691.                                 this_type = value_type (this_val);
  692.                               else
  693.                                 this_type = NULL;
  694.                               if (this_type)
  695.                                 current_type = lookup_struct_elt_type (
  696.                                   this_type,
  697.                                   copy_name ($1.stoken), 0);
  698.                               else
  699.                                 current_type = NULL;
  700.                             }
  701.                           else
  702.                             {
  703.                               struct bound_minimal_symbol msymbol;
  704.                               char *arg = copy_name ($1.stoken);

  705.                               msymbol =
  706.                                 lookup_bound_minimal_symbol (arg);
  707.                               if (msymbol.minsym != NULL)
  708.                                 write_exp_msymbol (pstate, msymbol);
  709.                               else if (!have_full_symbols ()
  710.                                        && !have_partial_symbols ())
  711.                                 error (_("No symbol table is loaded.  "
  712.                                        "Use the \"file\" command."));
  713.                               else
  714.                                 error (_("No symbol \"%s\" in current context."),
  715.                                        copy_name ($1.stoken));
  716.                             }
  717.                         }
  718.         ;


  719. ptype        :        typebase
  720.         ;

  721. /* We used to try to recognize more pointer to member types here, but
  722.    that didn't work (shift/reduce conflicts meant that these rules never
  723.    got executed).  The problem is that
  724.      int (foo::bar::baz::bizzle)
  725.    is a function type but
  726.      int (foo::bar::baz::bizzle::*)
  727.    is a pointer to member type.  Stroustrup loses again!  */

  728. type        :        ptype
  729.         ;

  730. typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
  731.         :        '^' typebase
  732.                         { $$ = lookup_pointer_type ($2); }
  733.         |        TYPENAME
  734.                         { $$ = $1.type; }
  735.         |        STRUCT name
  736.                         { $$ = lookup_struct (copy_name ($2),
  737.                                               expression_context_block); }
  738.         |        CLASS name
  739.                         { $$ = lookup_struct (copy_name ($2),
  740.                                               expression_context_block); }
  741.         /* "const" and "volatile" are curently ignored.  A type qualifier
  742.            after the type is handled in the ptype rule.  I think these could
  743.            be too.  */
  744.         ;

  745. name        :        NAME { $$ = $1.stoken; }
  746.         |        BLOCKNAME { $$ = $1.stoken; }
  747.         |        TYPENAME { $$ = $1.stoken; }
  748.         |        NAME_OR_INT  { $$ = $1.stoken; }
  749.         ;

  750. name_not_typename :        NAME
  751.         |        BLOCKNAME
  752. /* These would be useful if name_not_typename was useful, but it is just
  753.    a fake for "variable", so these cause reduce/reduce conflicts because
  754.    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  755.    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
  756.    context where only a name could occur, this might be useful.
  757.           |        NAME_OR_INT
  758. */
  759.         ;

  760. %%

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

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

  765. static int
  766. parse_number (struct parser_state *par_state,
  767.               const char *p, int len, int parsed_float, YYSTYPE *putithere)
  768. {
  769.   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
  770.      here, and we do kind of silly things like cast to unsigned.  */
  771.   LONGEST n = 0;
  772.   LONGEST prevn = 0;
  773.   ULONGEST un;

  774.   int i = 0;
  775.   int c;
  776.   int base = input_radix;
  777.   int unsigned_p = 0;

  778.   /* Number of "L" suffixes encountered.  */
  779.   int long_p = 0;

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

  782.   ULONGEST high_bit;
  783.   struct type *signed_type;
  784.   struct type *unsigned_type;

  785.   if (parsed_float)
  786.     {
  787.       if (! parse_c_float (parse_gdbarch (par_state), p, len,
  788.                            &putithere->typed_val_float.dval,
  789.                            &putithere->typed_val_float.type))
  790.         return ERROR;
  791.       return FLOAT;
  792.     }

  793.   /* Handle base-switching prefixes 0x, 0t, 0d, 0.  */
  794.   if (p[0] == '0')
  795.     switch (p[1])
  796.       {
  797.       case 'x':
  798.       case 'X':
  799.         if (len >= 3)
  800.           {
  801.             p += 2;
  802.             base = 16;
  803.             len -= 2;
  804.           }
  805.         break;

  806.       case 't':
  807.       case 'T':
  808.       case 'd':
  809.       case 'D':
  810.         if (len >= 3)
  811.           {
  812.             p += 2;
  813.             base = 10;
  814.             len -= 2;
  815.           }
  816.         break;

  817.       default:
  818.         base = 8;
  819.         break;
  820.       }

  821.   while (len-- > 0)
  822.     {
  823.       c = *p++;
  824.       if (c >= 'A' && c <= 'Z')
  825.         c += 'a' - 'A';
  826.       if (c != 'l' && c != 'u')
  827.         n *= base;
  828.       if (c >= '0' && c <= '9')
  829.         {
  830.           if (found_suffix)
  831.             return ERROR;
  832.           n += i = c - '0';
  833.         }
  834.       else
  835.         {
  836.           if (base > 10 && c >= 'a' && c <= 'f')
  837.             {
  838.               if (found_suffix)
  839.                 return ERROR;
  840.               n += i = c - 'a' + 10;
  841.             }
  842.           else if (c == 'l')
  843.             {
  844.               ++long_p;
  845.               found_suffix = 1;
  846.             }
  847.           else if (c == 'u')
  848.             {
  849.               unsigned_p = 1;
  850.               found_suffix = 1;
  851.             }
  852.           else
  853.             return ERROR;        /* Char not a digit */
  854.         }
  855.       if (i >= base)
  856.         return ERROR;                /* Invalid digit in this base.  */

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

  862.       /* Portably test for unsigned overflow.
  863.          FIXME: This check is wrong; for example it doesn't find overflow
  864.          on 0x123456789 when LONGEST is 32 bits.  */
  865.       if (c != 'l' && c != 'u' && n != 0)
  866.         {
  867.           if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
  868.             error (_("Numeric constant too large."));
  869.         }
  870.       prevn = n;
  871.     }

  872.   /* An integer constant is an int, a long, or a long long.  An L
  873.      suffix forces it to be long; an LL suffix forces it to be long
  874.      long.  If not forced to a larger size, it gets the first type of
  875.      the above that it fits in.  To figure out whether it fits, we
  876.      shift it right and see whether anything remains.  Note that we
  877.      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  878.      operation, because many compilers will warn about such a shift
  879.      (which always produces a zero result).  Sometimes gdbarch_int_bit
  880.      or gdbarch_long_bit will be that big, sometimes not.  To deal with
  881.      the case where it is we just always shift the value more than
  882.      once, with fewer bits each time.  */

  883.   un = (ULONGEST)n >> 2;
  884.   if (long_p == 0
  885.       && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
  886.     {
  887.       high_bit
  888.         = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);

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

  894.       unsigned_type = parse_type (par_state)->builtin_unsigned_int;
  895.       signed_type = parse_type (par_state)->builtin_int;
  896.     }
  897.   else if (long_p <= 1
  898.            && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
  899.     {
  900.       high_bit
  901.         = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
  902.       unsigned_type = parse_type (par_state)->builtin_unsigned_long;
  903.       signed_type = parse_type (par_state)->builtin_long;
  904.     }
  905.   else
  906.     {
  907.       int shift;
  908.       if (sizeof (ULONGEST) * HOST_CHAR_BIT
  909.           < gdbarch_long_long_bit (parse_gdbarch (par_state)))
  910.         /* A long long does not fit in a LONGEST.  */
  911.         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  912.       else
  913.         shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
  914.       high_bit = (ULONGEST) 1 << shift;
  915.       unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
  916.       signed_type = parse_type (par_state)->builtin_long_long;
  917.     }

  918.    putithere->typed_val_int.val = n;

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

  921.    if (unsigned_p || (n & high_bit))
  922.      {
  923.        putithere->typed_val_int.type = unsigned_type;
  924.      }
  925.    else
  926.      {
  927.        putithere->typed_val_int.type = signed_type;
  928.      }

  929.    return INT;
  930. }


  931. struct type_push
  932. {
  933.   struct type *stored;
  934.   struct type_push *next;
  935. };

  936. static struct type_push *tp_top = NULL;

  937. static void
  938. push_current_type (void)
  939. {
  940.   struct type_push *tpnew;
  941.   tpnew = (struct type_push *) malloc (sizeof (struct type_push));
  942.   tpnew->next = tp_top;
  943.   tpnew->stored = current_type;
  944.   current_type = NULL;
  945.   tp_top = tpnew;
  946. }

  947. static void
  948. pop_current_type (void)
  949. {
  950.   struct type_push *tp = tp_top;
  951.   if (tp)
  952.     {
  953.       current_type = tp->stored;
  954.       tp_top = tp->next;
  955.       free (tp);
  956.     }
  957. }

  958. struct token
  959. {
  960.   char *operator;
  961.   int token;
  962.   enum exp_opcode opcode;
  963. };

  964. static const struct token tokentab3[] =
  965.   {
  966.     {"shr", RSH, BINOP_END},
  967.     {"shl", LSH, BINOP_END},
  968.     {"and", ANDAND, BINOP_END},
  969.     {"div", DIV, BINOP_END},
  970.     {"not", NOT, BINOP_END},
  971.     {"mod", MOD, BINOP_END},
  972.     {"inc", INCREMENT, BINOP_END},
  973.     {"dec", DECREMENT, BINOP_END},
  974.     {"xor", XOR, BINOP_END}
  975.   };

  976. static const struct token tokentab2[] =
  977.   {
  978.     {"or", OR, BINOP_END},
  979.     {"<>", NOTEQUAL, BINOP_END},
  980.     {"<=", LEQ, BINOP_END},
  981.     {">=", GEQ, BINOP_END},
  982.     {":=", ASSIGN, BINOP_END},
  983.     {"::", COLONCOLON, BINOP_END} };

  984. /* Allocate uppercased var: */
  985. /* make an uppercased copy of tokstart.  */
  986. static char *
  987. uptok (const char *tokstart, int namelen)
  988. {
  989.   int i;
  990.   char *uptokstart = (char *)malloc(namelen+1);
  991.   for (i = 0;i <= namelen;i++)
  992.     {
  993.       if ((tokstart[i]>='a' && tokstart[i]<='z'))
  994.         uptokstart[i] = tokstart[i]-('a'-'A');
  995.       else
  996.         uptokstart[i] = tokstart[i];
  997.     }
  998.   uptokstart[namelen]='\0';
  999.   return uptokstart;
  1000. }

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

  1002. static int
  1003. yylex (void)
  1004. {
  1005.   int c;
  1006.   int namelen;
  1007.   unsigned int i;
  1008.   const char *tokstart;
  1009.   char *uptokstart;
  1010.   const char *tokptr;
  1011.   int explen, tempbufindex;
  1012.   static char *tempbuf;
  1013.   static int tempbufsize;

  1014. retry:

  1015.   prev_lexptr = lexptr;

  1016.   tokstart = lexptr;
  1017.   explen = strlen (lexptr);

  1018.   /* See if it is a special token of length 3.  */
  1019.   if (explen > 2)
  1020.     for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
  1021.       if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
  1022.           && (!isalpha (tokentab3[i].operator[0]) || explen == 3
  1023.               || (!isalpha (tokstart[3])
  1024.                   && !isdigit (tokstart[3]) && tokstart[3] != '_')))
  1025.         {
  1026.           lexptr += 3;
  1027.           yylval.opcode = tokentab3[i].opcode;
  1028.           return tokentab3[i].token;
  1029.         }

  1030.   /* See if it is a special token of length 2.  */
  1031.   if (explen > 1)
  1032.   for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
  1033.       if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
  1034.           && (!isalpha (tokentab2[i].operator[0]) || explen == 2
  1035.               || (!isalpha (tokstart[2])
  1036.                   && !isdigit (tokstart[2]) && tokstart[2] != '_')))
  1037.         {
  1038.           lexptr += 2;
  1039.           yylval.opcode = tokentab2[i].opcode;
  1040.           return tokentab2[i].token;
  1041.         }

  1042.   switch (c = *tokstart)
  1043.     {
  1044.     case 0:
  1045.       if (search_field && parse_completion)
  1046.         return COMPLETE;
  1047.       else
  1048.        return 0;

  1049.     case ' ':
  1050.     case '\t':
  1051.     case '\n':
  1052.       lexptr++;
  1053.       goto retry;

  1054.     case '\'':
  1055.       /* We either have a character constant ('0' or '\177' for example)
  1056.          or we have a quoted symbol reference ('foo(int,int)' in object pascal
  1057.          for example).  */
  1058.       lexptr++;
  1059.       c = *lexptr++;
  1060.       if (c == '\\')
  1061.         c = parse_escape (parse_gdbarch (pstate), &lexptr);
  1062.       else if (c == '\'')
  1063.         error (_("Empty character constant."));

  1064.       yylval.typed_val_int.val = c;
  1065.       yylval.typed_val_int.type = parse_type (pstate)->builtin_char;

  1066.       c = *lexptr++;
  1067.       if (c != '\'')
  1068.         {
  1069.           namelen = skip_quoted (tokstart) - tokstart;
  1070.           if (namelen > 2)
  1071.             {
  1072.               lexptr = tokstart + namelen;
  1073.               if (lexptr[-1] != '\'')
  1074.                 error (_("Unmatched single quote."));
  1075.               namelen -= 2;
  1076.               tokstart++;
  1077.               uptokstart = uptok(tokstart,namelen);
  1078.               goto tryname;
  1079.             }
  1080.           error (_("Invalid character constant."));
  1081.         }
  1082.       return INT;

  1083.     case '(':
  1084.       paren_depth++;
  1085.       lexptr++;
  1086.       return c;

  1087.     case ')':
  1088.       if (paren_depth == 0)
  1089.         return 0;
  1090.       paren_depth--;
  1091.       lexptr++;
  1092.       return c;

  1093.     case ',':
  1094.       if (comma_terminates && paren_depth == 0)
  1095.         return 0;
  1096.       lexptr++;
  1097.       return c;

  1098.     case '.':
  1099.       /* Might be a floating point number.  */
  1100.       if (lexptr[1] < '0' || lexptr[1] > '9')
  1101.         {
  1102.           goto symbol;                /* Nope, must be a symbol.  */
  1103.         }

  1104.       /* FALL THRU into number case.  */

  1105.     case '0':
  1106.     case '1':
  1107.     case '2':
  1108.     case '3':
  1109.     case '4':
  1110.     case '5':
  1111.     case '6':
  1112.     case '7':
  1113.     case '8':
  1114.     case '9':
  1115.       {
  1116.         /* It's a number.  */
  1117.         int got_dot = 0, got_e = 0, toktype;
  1118.         const char *p = tokstart;
  1119.         int hex = input_radix > 10;

  1120.         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1121.           {
  1122.             p += 2;
  1123.             hex = 1;
  1124.           }
  1125.         else if (c == '0' && (p[1]=='t' || p[1]=='T'
  1126.                               || p[1]=='d' || p[1]=='D'))
  1127.           {
  1128.             p += 2;
  1129.             hex = 0;
  1130.           }

  1131.         for (;; ++p)
  1132.           {
  1133.             /* This test includes !hex because 'e' is a valid hex digit
  1134.                and thus does not indicate a floating point number when
  1135.                the radix is hex.  */
  1136.             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1137.               got_dot = got_e = 1;
  1138.             /* This test does not include !hex, because a '.' always indicates
  1139.                a decimal floating point number regardless of the radix.  */
  1140.             else if (!got_dot && *p == '.')
  1141.               got_dot = 1;
  1142.             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1143.                      && (*p == '-' || *p == '+'))
  1144.               /* This is the sign of the exponent, not the end of the
  1145.                  number.  */
  1146.               continue;
  1147.             /* We will take any letters or digits.  parse_number will
  1148.                complain if past the radix, or if L or U are not final.  */
  1149.             else if ((*p < '0' || *p > '9')
  1150.                      && ((*p < 'a' || *p > 'z')
  1151.                                   && (*p < 'A' || *p > 'Z')))
  1152.               break;
  1153.           }
  1154.         toktype = parse_number (pstate, tokstart,
  1155.                                 p - tokstart, got_dot | got_e, &yylval);
  1156.         if (toktype == ERROR)
  1157.           {
  1158.             char *err_copy = (char *) alloca (p - tokstart + 1);

  1159.             memcpy (err_copy, tokstart, p - tokstart);
  1160.             err_copy[p - tokstart] = 0;
  1161.             error (_("Invalid number \"%s\"."), err_copy);
  1162.           }
  1163.         lexptr = p;
  1164.         return toktype;
  1165.       }

  1166.     case '+':
  1167.     case '-':
  1168.     case '*':
  1169.     case '/':
  1170.     case '|':
  1171.     case '&':
  1172.     case '^':
  1173.     case '~':
  1174.     case '!':
  1175.     case '@':
  1176.     case '<':
  1177.     case '>':
  1178.     case '[':
  1179.     case ']':
  1180.     case '?':
  1181.     case ':':
  1182.     case '=':
  1183.     case '{':
  1184.     case '}':
  1185.     symbol:
  1186.       lexptr++;
  1187.       return c;

  1188.     case '"':

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

  1197.       tokptr = ++tokstart;
  1198.       tempbufindex = 0;

  1199.       do {
  1200.         /* Grow the static temp buffer if necessary, including allocating
  1201.            the first one on demand.  */
  1202.         if (tempbufindex + 1 >= tempbufsize)
  1203.           {
  1204.             tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
  1205.           }

  1206.         switch (*tokptr)
  1207.           {
  1208.           case '\0':
  1209.           case '"':
  1210.             /* Do nothing, loop will terminate.  */
  1211.             break;
  1212.           case '\\':
  1213.             ++tokptr;
  1214.             c = parse_escape (parse_gdbarch (pstate), &tokptr);
  1215.             if (c == -1)
  1216.               {
  1217.                 continue;
  1218.               }
  1219.             tempbuf[tempbufindex++] = c;
  1220.             break;
  1221.           default:
  1222.             tempbuf[tempbufindex++] = *tokptr++;
  1223.             break;
  1224.           }
  1225.       } while ((*tokptr != '"') && (*tokptr != '\0'));
  1226.       if (*tokptr++ != '"')
  1227.         {
  1228.           error (_("Unterminated string in expression."));
  1229.         }
  1230.       tempbuf[tempbufindex] = '\0';        /* See note above.  */
  1231.       yylval.sval.ptr = tempbuf;
  1232.       yylval.sval.length = tempbufindex;
  1233.       lexptr = tokptr;
  1234.       return (STRING);
  1235.     }

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

  1240.   /* It's a name.  See how long it is.  */
  1241.   namelen = 0;
  1242.   for (c = tokstart[namelen];
  1243.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1244.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
  1245.     {
  1246.       /* Template parameter lists are part of the name.
  1247.          FIXME: This mishandles `print $a<4&&$a>3'.  */
  1248.       if (c == '<')
  1249.         {
  1250.           int i = namelen;
  1251.           int nesting_level = 1;
  1252.           while (tokstart[++i])
  1253.             {
  1254.               if (tokstart[i] == '<')
  1255.                 nesting_level++;
  1256.               else if (tokstart[i] == '>')
  1257.                 {
  1258.                   if (--nesting_level == 0)
  1259.                     break;
  1260.                 }
  1261.             }
  1262.           if (tokstart[i] == '>')
  1263.             namelen = i;
  1264.           else
  1265.             break;
  1266.         }

  1267.       /* do NOT uppercase internals because of registers !!!  */
  1268.       c = tokstart[++namelen];
  1269.     }

  1270.   uptokstart = uptok(tokstart,namelen);

  1271.   /* The token "if" terminates the expression and is NOT
  1272.      removed from the input stream.  */
  1273.   if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
  1274.     {
  1275.       free (uptokstart);
  1276.       return 0;
  1277.     }

  1278.   lexptr += namelen;

  1279.   tryname:

  1280.   /* Catch specific keywords.  Should be done with a data structure.  */
  1281.   switch (namelen)
  1282.     {
  1283.     case 6:
  1284.       if (strcmp (uptokstart, "OBJECT") == 0)
  1285.         {
  1286.           free (uptokstart);
  1287.           return CLASS;
  1288.         }
  1289.       if (strcmp (uptokstart, "RECORD") == 0)
  1290.         {
  1291.           free (uptokstart);
  1292.           return STRUCT;
  1293.         }
  1294.       if (strcmp (uptokstart, "SIZEOF") == 0)
  1295.         {
  1296.           free (uptokstart);
  1297.           return SIZEOF;
  1298.         }
  1299.       break;
  1300.     case 5:
  1301.       if (strcmp (uptokstart, "CLASS") == 0)
  1302.         {
  1303.           free (uptokstart);
  1304.           return CLASS;
  1305.         }
  1306.       if (strcmp (uptokstart, "FALSE") == 0)
  1307.         {
  1308.           yylval.lval = 0;
  1309.           free (uptokstart);
  1310.           return FALSEKEYWORD;
  1311.         }
  1312.       break;
  1313.     case 4:
  1314.       if (strcmp (uptokstart, "TRUE") == 0)
  1315.         {
  1316.           yylval.lval = 1;
  1317.           free (uptokstart);
  1318.             return TRUEKEYWORD;
  1319.         }
  1320.       if (strcmp (uptokstart, "SELF") == 0)
  1321.         {
  1322.           /* Here we search for 'this' like
  1323.              inserted in FPC stabs debug info.  */
  1324.           static const char this_name[] = "this";

  1325.           if (lookup_symbol (this_name, expression_context_block,
  1326.                              VAR_DOMAIN, NULL))
  1327.             {
  1328.               free (uptokstart);
  1329.               return THIS;
  1330.             }
  1331.         }
  1332.       break;
  1333.     default:
  1334.       break;
  1335.     }

  1336.   yylval.sval.ptr = tokstart;
  1337.   yylval.sval.length = namelen;

  1338.   if (*tokstart == '$')
  1339.     {
  1340.       char *tmp;

  1341.       /* $ is the normal prefix for pascal hexadecimal values
  1342.         but this conflicts with the GDB use for debugger variables
  1343.         so in expression to enter hexadecimal values
  1344.         we still need to use C syntax with 0xff  */
  1345.       write_dollar_variable (pstate, yylval.sval);
  1346.       tmp = alloca (namelen + 1);
  1347.       memcpy (tmp, tokstart, namelen);
  1348.       tmp[namelen] = '\0';
  1349.       intvar = lookup_only_internalvar (tmp + 1);
  1350.       free (uptokstart);
  1351.       return VARIABLE;
  1352.     }

  1353.   /* Use token-type BLOCKNAME for symbols that happen to be defined as
  1354.      functions or symtabs.  If this is not so, then ...
  1355.      Use token-type TYPENAME for symbols that happen to be defined
  1356.      currently as names of types; NAME for other symbols.
  1357.      The caller is not constrained to care about the distinction.  */
  1358.   {
  1359.     char *tmp = copy_name (yylval.sval);
  1360.     struct symbol *sym;
  1361.     struct field_of_this_result is_a_field_of_this;
  1362.     int is_a_field = 0;
  1363.     int hextype;


  1364.     if (search_field && current_type)
  1365.       is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
  1366.     if (is_a_field)
  1367.       sym = NULL;
  1368.     else
  1369.       sym = lookup_symbol (tmp, expression_context_block,
  1370.                            VAR_DOMAIN, &is_a_field_of_this);
  1371.     /* second chance uppercased (as Free Pascal does).  */
  1372.     if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
  1373.       {
  1374.        for (i = 0; i <= namelen; i++)
  1375.          {
  1376.            if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
  1377.              tmp[i] -= ('a'-'A');
  1378.          }
  1379.        if (search_field && current_type)
  1380.          is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
  1381.        if (is_a_field)
  1382.          sym = NULL;
  1383.        else
  1384.          sym = lookup_symbol (tmp, expression_context_block,
  1385.                               VAR_DOMAIN, &is_a_field_of_this);
  1386.       }
  1387.     /* Third chance Capitalized (as GPC does).  */
  1388.     if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
  1389.       {
  1390.        for (i = 0; i <= namelen; i++)
  1391.          {
  1392.            if (i == 0)
  1393.              {
  1394.               if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
  1395.                 tmp[i] -= ('a'-'A');
  1396.              }
  1397.            else
  1398.            if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
  1399.              tmp[i] -= ('A'-'a');
  1400.           }
  1401.        if (search_field && current_type)
  1402.          is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
  1403.        if (is_a_field)
  1404.          sym = NULL;
  1405.        else
  1406.          sym = lookup_symbol (tmp, expression_context_block,
  1407.                               VAR_DOMAIN, &is_a_field_of_this);
  1408.       }

  1409.     if (is_a_field)
  1410.       {
  1411.         tempbuf = (char *) realloc (tempbuf, namelen + 1);
  1412.         strncpy (tempbuf, tmp, namelen);
  1413.         tempbuf [namelen] = 0;
  1414.         yylval.sval.ptr = tempbuf;
  1415.         yylval.sval.length = namelen;
  1416.         free (uptokstart);
  1417.         return FIELDNAME;
  1418.       }
  1419.     /* Call lookup_symtab, not lookup_partial_symtab, in case there are
  1420.        no psymtabs (coff, xcoff, or some future change to blow away the
  1421.        psymtabs once once symbols are read).  */
  1422.     if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  1423.         || lookup_symtab (tmp))
  1424.       {
  1425.         yylval.ssym.sym = sym;
  1426.         yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1427.         free (uptokstart);
  1428.         return BLOCKNAME;
  1429.       }
  1430.     if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1431.         {
  1432. #if 1
  1433.           /* Despite the following flaw, we need to keep this code enabled.
  1434.              Because we can get called from check_stub_method, if we don't
  1435.              handle nested types then it screws many operations in any
  1436.              program which uses nested types.  */
  1437.           /* In "A::x", if x is a member function of A and there happens
  1438.              to be a type (nested or not, since the stabs don't make that
  1439.              distinction) named x, then this code incorrectly thinks we
  1440.              are dealing with nested types rather than a member function.  */

  1441.           const char *p;
  1442.           const char *namestart;
  1443.           struct symbol *best_sym;

  1444.           /* Look ahead to detect nested types.  This probably should be
  1445.              done in the grammar, but trying seemed to introduce a lot
  1446.              of shift/reduce and reduce/reduce conflicts.  It's possible
  1447.              that it could be done, though.  Or perhaps a non-grammar, but
  1448.              less ad hoc, approach would work well.  */

  1449.           /* Since we do not currently have any way of distinguishing
  1450.              a nested type from a non-nested one (the stabs don't tell
  1451.              us whether a type is nested), we just ignore the
  1452.              containing type.  */

  1453.           p = lexptr;
  1454.           best_sym = sym;
  1455.           while (1)
  1456.             {
  1457.               /* Skip whitespace.  */
  1458.               while (*p == ' ' || *p == '\t' || *p == '\n')
  1459.                 ++p;
  1460.               if (*p == ':' && p[1] == ':')
  1461.                 {
  1462.                   /* Skip the `::'.  */
  1463.                   p += 2;
  1464.                   /* Skip whitespace.  */
  1465.                   while (*p == ' ' || *p == '\t' || *p == '\n')
  1466.                     ++p;
  1467.                   namestart = p;
  1468.                   while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
  1469.                          || (*p >= 'a' && *p <= 'z')
  1470.                          || (*p >= 'A' && *p <= 'Z'))
  1471.                     ++p;
  1472.                   if (p != namestart)
  1473.                     {
  1474.                       struct symbol *cur_sym;
  1475.                       /* As big as the whole rest of the expression, which is
  1476.                          at least big enough.  */
  1477.                       char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
  1478.                       char *tmp1;

  1479.                       tmp1 = ncopy;
  1480.                       memcpy (tmp1, tmp, strlen (tmp));
  1481.                       tmp1 += strlen (tmp);
  1482.                       memcpy (tmp1, "::", 2);
  1483.                       tmp1 += 2;
  1484.                       memcpy (tmp1, namestart, p - namestart);
  1485.                       tmp1[p - namestart] = '\0';
  1486.                       cur_sym = lookup_symbol (ncopy, expression_context_block,
  1487.                                                VAR_DOMAIN, NULL);
  1488.                       if (cur_sym)
  1489.                         {
  1490.                           if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
  1491.                             {
  1492.                               best_sym = cur_sym;
  1493.                               lexptr = p;
  1494.                             }
  1495.                           else
  1496.                             break;
  1497.                         }
  1498.                       else
  1499.                         break;
  1500.                     }
  1501.                   else
  1502.                     break;
  1503.                 }
  1504.               else
  1505.                 break;
  1506.             }

  1507.           yylval.tsym.type = SYMBOL_TYPE (best_sym);
  1508. #else /* not 0 */
  1509.           yylval.tsym.type = SYMBOL_TYPE (sym);
  1510. #endif /* not 0 */
  1511.           free (uptokstart);
  1512.           return TYPENAME;
  1513.         }
  1514.     yylval.tsym.type
  1515.       = language_lookup_primitive_type (parse_language (pstate),
  1516.                                         parse_gdbarch (pstate), tmp);
  1517.     if (yylval.tsym.type != NULL)
  1518.       {
  1519.         free (uptokstart);
  1520.         return TYPENAME;
  1521.       }

  1522.     /* Input names that aren't symbols but ARE valid hex numbers,
  1523.        when the input radix permits them, can be names or numbers
  1524.        depending on the parse.  Note we support radixes > 16 here.  */
  1525.     if (!sym
  1526.         && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1527.             || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1528.       {
  1529.          YYSTYPE newlval;        /* Its value is ignored.  */
  1530.         hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
  1531.         if (hextype == INT)
  1532.           {
  1533.             yylval.ssym.sym = sym;
  1534.             yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1535.             free (uptokstart);
  1536.             return NAME_OR_INT;
  1537.           }
  1538.       }

  1539.     free(uptokstart);
  1540.     /* Any other kind of symbol.  */
  1541.     yylval.ssym.sym = sym;
  1542.     yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1543.     return NAME;
  1544.   }
  1545. }

  1546. int
  1547. pascal_parse (struct parser_state *par_state)
  1548. {
  1549.   int result;
  1550.   struct cleanup *c = make_cleanup_clear_parser_state (&pstate);

  1551.   /* Setting up the parser state.  */
  1552.   gdb_assert (par_state != NULL);
  1553.   pstate = par_state;

  1554.   result = yyparse ();
  1555.   do_cleanups (c);
  1556.   return result;
  1557. }

  1558. void
  1559. yyerror (char *msg)
  1560. {
  1561.   if (prev_lexptr)
  1562.     lexptr = prev_lexptr;

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