gdb/ada-exp.y - gdb

  1. /* YACC parser for Ada expressions, for GDB.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

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

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

  29. %{

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

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

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

  49. /* NOTE: This is clumsy, especially since BISON and FLEX provide --prefix
  50.    options.  I presume we are maintaining it to accommodate systems
  51.    without BISON?  (PNH) */

  52. #define        yymaxdepth ada_maxdepth
  53. /* ada_parse calls this after initialization */
  54. #define        yyparse        ada_parse_internal
  55. #define        yylex        ada_lex
  56. #define        yyerror        ada_error
  57. #define        yylval        ada_lval
  58. #define        yychar        ada_char
  59. #define        yydebug        ada_debug
  60. #define        yypact        ada_pact
  61. #define        yyr1        ada_r1
  62. #define        yyr2        ada_r2
  63. #define        yydef        ada_def
  64. #define        yychk        ada_chk
  65. #define        yypgo        ada_pgo
  66. #define        yyact        ada_act
  67. #define        yyexca        ada_exca
  68. #define yyerrflag ada_errflag
  69. #define yynerrs        ada_nerrs
  70. #define        yyps        ada_ps
  71. #define        yypv        ada_pv
  72. #define        yys        ada_s
  73. #define        yy_yys        ada_yys
  74. #define        yystate        ada_state
  75. #define        yytmp        ada_tmp
  76. #define        yyv        ada_v
  77. #define        yy_yyv        ada_yyv
  78. #define        yyval        ada_val
  79. #define        yylloc        ada_lloc
  80. #define yyreds        ada_reds                /* With YYDEBUG defined */
  81. #define yytoks        ada_toks                /* With YYDEBUG defined */
  82. #define yyname        ada_name                /* With YYDEBUG defined */
  83. #define yyrule        ada_rule                /* With YYDEBUG defined */
  84. #define yyss        ada_yyss
  85. #define yysslim        ada_yysslim
  86. #define yyssp        ada_yyssp
  87. #define yystacksize ada_yystacksize
  88. #define yyvs        ada_yyvs
  89. #define yyvsp        ada_yyvsp

  90. #ifndef YYDEBUG
  91. #define        YYDEBUG        1                /* Default to yydebug support */
  92. #endif

  93. #define YYFPRINTF parser_fprintf

  94. struct name_info {
  95.   struct symbol *sym;
  96.   struct minimal_symbol *msym;
  97.   const struct block *block;
  98.   struct stoken stoken;
  99. };

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

  102. static struct parser_state *pstate = NULL;

  103. static struct stoken empty_stoken = { "", 0 };

  104. /* If expression is in the context of TYPE'(...), then TYPE, else
  105. * NULL.  */
  106. static struct type *type_qualifier;

  107. int yyparse (void);

  108. static int yylex (void);

  109. void yyerror (char *);

  110. static void write_int (struct parser_state *, LONGEST, struct type *);

  111. static void write_object_renaming (struct parser_state *,
  112.                                    const struct block *, const char *, int,
  113.                                    const char *, int);

  114. static struct type* write_var_or_type (struct parser_state *,
  115.                                        const struct block *, struct stoken);

  116. static void write_name_assoc (struct parser_state *, struct stoken);

  117. static void write_exp_op_with_string (struct parser_state *, enum exp_opcode,
  118.                                       struct stoken);

  119. static const struct block *block_lookup (const struct block *, const char *);

  120. static LONGEST convert_char_literal (struct type *, LONGEST);

  121. static void write_ambiguous_var (struct parser_state *,
  122.                                  const struct block *, char *, int);

  123. static struct type *type_int (struct parser_state *);

  124. static struct type *type_long (struct parser_state *);

  125. static struct type *type_long_long (struct parser_state *);

  126. static struct type *type_float (struct parser_state *);

  127. static struct type *type_double (struct parser_state *);

  128. static struct type *type_long_double (struct parser_state *);

  129. static struct type *type_char (struct parser_state *);

  130. static struct type *type_boolean (struct parser_state *);

  131. static struct type *type_system_address (struct parser_state *);

  132. %}

  133. %union
  134.   {
  135.     LONGEST lval;
  136.     struct {
  137.       LONGEST val;
  138.       struct type *type;
  139.     } typed_val;
  140.     struct {
  141.       DOUBLEST dval;
  142.       struct type *type;
  143.     } typed_val_float;
  144.     struct type *tval;
  145.     struct stoken sval;
  146.     const struct block *bval;
  147.     struct internalvar *ivar;
  148.   }

  149. %type <lval> positional_list component_groups component_associations
  150. %type <lval> aggregate_component_list
  151. %type <tval> var_or_type

  152. %token <typed_val> INT NULL_PTR CHARLIT
  153. %token <typed_val_float> FLOAT
  154. %token TRUEKEYWORD FALSEKEYWORD
  155. %token COLONCOLON
  156. %token <sval> STRING NAME DOT_ID
  157. %type <bval> block
  158. %type <lval> arglist tick_arglist

  159. %type <tval> save_qualifier

  160. %token DOT_ALL

  161. /* Special type cases, put in to allow the parser to distinguish different
  162.    legal basetypes.  */
  163. %token <sval> SPECIAL_VARIABLE

  164. %nonassoc ASSIGN
  165. %left _AND_ OR XOR THEN ELSE
  166. %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
  167. %left '@'
  168. %left '+' '-' '&'
  169. %left UNARY
  170. %left '*' '/' MOD REM
  171. %right STARSTAR ABS NOT

  172. /* Artificial token to give NAME => ... and NAME | priority over reducing
  173.    NAME to <primary> and to give <primary>' priority over reducing <primary>
  174.    to <simple_exp>. */
  175. %nonassoc VAR

  176. %nonassoc ARROW '|'

  177. %right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
  178. %right TICK_MAX TICK_MIN TICK_MODULUS
  179. %right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
  180. /* The following are right-associative only so that reductions at this
  181.     precedence have lower precedence than '.' and '('.  The syntax still
  182.     forces a.b.c, e.g., to be LEFT-associated.  */
  183. %right '.' '(' '[' DOT_ID DOT_ALL

  184. %token NEW OTHERS


  185. %%

  186. start   :        exp1
  187.         ;

  188. /* Expressions, including the sequencing operator.  */
  189. exp1        :        exp
  190.         |        exp1 ';' exp
  191.                         { write_exp_elt_opcode (pstate, BINOP_COMMA); }
  192.         |         primary ASSIGN exp   /* Extension for convenience */
  193.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  194.         ;

  195. /* Expressions, not including the sequencing operator.  */
  196. primary :        primary DOT_ALL
  197.                         { write_exp_elt_opcode (pstate, UNOP_IND); }
  198.         ;

  199. primary :        primary DOT_ID
  200.                         { write_exp_op_with_string (pstate, STRUCTOP_STRUCT,
  201.                                                     $2); }
  202.         ;

  203. primary :        primary '(' arglist ')'
  204.                         {
  205.                           write_exp_elt_opcode (pstate, OP_FUNCALL);
  206.                           write_exp_elt_longcst (pstate, $3);
  207.                           write_exp_elt_opcode (pstate, OP_FUNCALL);
  208.                         }
  209.         |        var_or_type '(' arglist ')'
  210.                         {
  211.                           if ($1 != NULL)
  212.                             {
  213.                               if ($3 != 1)
  214.                                 error (_("Invalid conversion"));
  215.                               write_exp_elt_opcode (pstate, UNOP_CAST);
  216.                               write_exp_elt_type (pstate, $1);
  217.                               write_exp_elt_opcode (pstate, UNOP_CAST);
  218.                             }
  219.                           else
  220.                             {
  221.                               write_exp_elt_opcode (pstate, OP_FUNCALL);
  222.                               write_exp_elt_longcst (pstate, $3);
  223.                               write_exp_elt_opcode (pstate, OP_FUNCALL);
  224.                             }
  225.                         }
  226.         ;

  227. primary :        var_or_type '\'' save_qualifier { type_qualifier = $1; }
  228.                    '(' exp ')'
  229.                         {
  230.                           if ($1 == NULL)
  231.                             error (_("Type required for qualification"));
  232.                           write_exp_elt_opcode (pstate, UNOP_QUAL);
  233.                           write_exp_elt_type (pstate, $1);
  234.                           write_exp_elt_opcode (pstate, UNOP_QUAL);
  235.                           type_qualifier = $3;
  236.                         }
  237.         ;

  238. save_qualifier :         { $$ = type_qualifier; }
  239.         ;

  240. primary :
  241.                 primary '(' simple_exp DOTDOT simple_exp ')'
  242.                         { write_exp_elt_opcode (pstate, TERNOP_SLICE); }
  243.         |        var_or_type '(' simple_exp DOTDOT simple_exp ')'
  244.                         { if ($1 == NULL)
  245.                             write_exp_elt_opcode (pstate, TERNOP_SLICE);
  246.                           else
  247.                             error (_("Cannot slice a type"));
  248.                         }
  249.         ;

  250. primary :        '(' exp1 ')'        { }
  251.         ;

  252. /* The following rule causes a conflict with the type conversion
  253.        var_or_type (exp)
  254.    To get around it, we give '(' higher priority and add bridge rules for
  255.        var_or_type (exp, exp, ...)
  256.        var_or_type (exp .. exp)
  257.    We also have the action for  var_or_type(exp) generate a function call
  258.    when the first symbol does not denote a type. */

  259. primary :        var_or_type        %prec VAR
  260.                         { if ($1 != NULL)
  261.                             {
  262.                               write_exp_elt_opcode (pstate, OP_TYPE);
  263.                               write_exp_elt_type (pstate, $1);
  264.                               write_exp_elt_opcode (pstate, OP_TYPE);
  265.                             }
  266.                         }
  267.         ;

  268. primary :        SPECIAL_VARIABLE /* Various GDB extensions */
  269.                         { write_dollar_variable (pstate, $1); }
  270.         ;

  271. primary :             aggregate
  272.         ;

  273. simple_exp :         primary
  274.         ;

  275. simple_exp :        '-' simple_exp    %prec UNARY
  276.                         { write_exp_elt_opcode (pstate, UNOP_NEG); }
  277.         ;

  278. simple_exp :        '+' simple_exp    %prec UNARY
  279.                         { write_exp_elt_opcode (pstate, UNOP_PLUS); }
  280.         ;

  281. simple_exp :        NOT simple_exp    %prec UNARY
  282.                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  283.         ;

  284. simple_exp :    ABS simple_exp           %prec UNARY
  285.                         { write_exp_elt_opcode (pstate, UNOP_ABS); }
  286.         ;

  287. arglist        :                { $$ = 0; }
  288.         ;

  289. arglist        :        exp
  290.                         { $$ = 1; }
  291.         |        NAME ARROW exp
  292.                         { $$ = 1; }
  293.         |        arglist ',' exp
  294.                         { $$ = $1 + 1; }
  295.         |        arglist ',' NAME ARROW exp
  296.                         { $$ = $1 + 1; }
  297.         ;

  298. primary :        '{' var_or_type '}' primary  %prec '.'
  299.                 /* GDB extension */
  300.                         {
  301.                           if ($2 == NULL)
  302.                             error (_("Type required within braces in coercion"));
  303.                           write_exp_elt_opcode (pstate, UNOP_MEMVAL);
  304.                           write_exp_elt_type (pstate, $2);
  305.                           write_exp_elt_opcode (pstate, UNOP_MEMVAL);
  306.                         }
  307.         ;

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

  309. simple_exp         :         simple_exp STARSTAR simple_exp
  310.                         { write_exp_elt_opcode (pstate, BINOP_EXP); }
  311.         ;

  312. simple_exp        :        simple_exp '*' simple_exp
  313.                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
  314.         ;

  315. simple_exp        :        simple_exp '/' simple_exp
  316.                         { write_exp_elt_opcode (pstate, BINOP_DIV); }
  317.         ;

  318. simple_exp        :        simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
  319.                         { write_exp_elt_opcode (pstate, BINOP_REM); }
  320.         ;

  321. simple_exp        :        simple_exp MOD simple_exp
  322.                         { write_exp_elt_opcode (pstate, BINOP_MOD); }
  323.         ;

  324. simple_exp        :        simple_exp '@' simple_exp        /* GDB extension */
  325.                         { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
  326.         ;

  327. simple_exp        :        simple_exp '+' simple_exp
  328.                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
  329.         ;

  330. simple_exp        :        simple_exp '&' simple_exp
  331.                         { write_exp_elt_opcode (pstate, BINOP_CONCAT); }
  332.         ;

  333. simple_exp        :        simple_exp '-' simple_exp
  334.                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
  335.         ;

  336. relation :        simple_exp
  337.         ;

  338. relation :        simple_exp '=' simple_exp
  339.                         { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  340.         ;

  341. relation :        simple_exp NOTEQUAL simple_exp
  342.                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  343.         ;

  344. relation :        simple_exp LEQ simple_exp
  345.                         { write_exp_elt_opcode (pstate, BINOP_LEQ); }
  346.         ;

  347. relation :        simple_exp IN simple_exp DOTDOT simple_exp
  348.                         { write_exp_elt_opcode (pstate, TERNOP_IN_RANGE); }
  349.         |       simple_exp IN primary TICK_RANGE tick_arglist
  350.                         { write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
  351.                           write_exp_elt_longcst (pstate, (LONGEST) $5);
  352.                           write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
  353.                         }
  354.          |        simple_exp IN var_or_type        %prec TICK_ACCESS
  355.                         {
  356.                           if ($3 == NULL)
  357.                             error (_("Right operand of 'in' must be type"));
  358.                           write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
  359.                           write_exp_elt_type (pstate, $3);
  360.                           write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
  361.                         }
  362.         |        simple_exp NOT IN simple_exp DOTDOT simple_exp
  363.                         { write_exp_elt_opcode (pstate, TERNOP_IN_RANGE);
  364.                           write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT);
  365.                         }
  366.         |       simple_exp NOT IN primary TICK_RANGE tick_arglist
  367.                         { write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
  368.                           write_exp_elt_longcst (pstate, (LONGEST) $6);
  369.                           write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
  370.                           write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT);
  371.                         }
  372.          |        simple_exp NOT IN var_or_type        %prec TICK_ACCESS
  373.                         {
  374.                           if ($4 == NULL)
  375.                             error (_("Right operand of 'in' must be type"));
  376.                           write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
  377.                           write_exp_elt_type (pstate, $4);
  378.                           write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
  379.                           write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT);
  380.                         }
  381.         ;

  382. relation :        simple_exp GEQ simple_exp
  383.                         { write_exp_elt_opcode (pstate, BINOP_GEQ); }
  384.         ;

  385. relation :        simple_exp '<' simple_exp
  386.                         { write_exp_elt_opcode (pstate, BINOP_LESS); }
  387.         ;

  388. relation :        simple_exp '>' simple_exp
  389.                         { write_exp_elt_opcode (pstate, BINOP_GTR); }
  390.         ;

  391. exp        :        relation
  392.         |        and_exp
  393.         |        and_then_exp
  394.         |        or_exp
  395.         |        or_else_exp
  396.         |        xor_exp
  397.         ;

  398. and_exp :
  399.                 relation _AND_ relation
  400.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  401.         |        and_exp _AND_ relation
  402.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  403.         ;

  404. and_then_exp :
  405.                relation _AND_ THEN relation
  406.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  407.         |        and_then_exp _AND_ THEN relation
  408.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  409.         ;

  410. or_exp :
  411.                 relation OR relation
  412.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  413.         |        or_exp OR relation
  414.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  415.         ;

  416. or_else_exp :
  417.                relation OR ELSE relation
  418.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  419.         |      or_else_exp OR ELSE relation
  420.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  421.         ;

  422. xor_exp :       relation XOR relation
  423.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  424.         |        xor_exp XOR relation
  425.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  426.         ;

  427. /* Primaries can denote types (OP_TYPE).  In cases such as
  428.    primary TICK_ADDRESS, where a type would be invalid, it will be
  429.    caught when evaluate_subexp in ada-lang.c tries to evaluate the
  430.    primary, expecting a value.  Precedence rules resolve the ambiguity
  431.    in NAME TICK_ACCESS in favor of shifting to form a var_or_type.  A
  432.    construct such as aType'access'access will again cause an error when
  433.    aType'access evaluates to a type that evaluate_subexp attempts to
  434.    evaluate. */
  435. primary :        primary TICK_ACCESS
  436.                         { write_exp_elt_opcode (pstate, UNOP_ADDR); }
  437.         |        primary TICK_ADDRESS
  438.                         { write_exp_elt_opcode (pstate, UNOP_ADDR);
  439.                           write_exp_elt_opcode (pstate, UNOP_CAST);
  440.                           write_exp_elt_type (pstate,
  441.                                               type_system_address (pstate));
  442.                           write_exp_elt_opcode (pstate, UNOP_CAST);
  443.                         }
  444.         |        primary TICK_FIRST tick_arglist
  445.                         { write_int (pstate, $3, type_int (pstate));
  446.                           write_exp_elt_opcode (pstate, OP_ATR_FIRST); }
  447.         |        primary TICK_LAST tick_arglist
  448.                         { write_int (pstate, $3, type_int (pstate));
  449.                           write_exp_elt_opcode (pstate, OP_ATR_LAST); }
  450.         |         primary TICK_LENGTH tick_arglist
  451.                         { write_int (pstate, $3, type_int (pstate));
  452.                           write_exp_elt_opcode (pstate, OP_ATR_LENGTH); }
  453.         |       primary TICK_SIZE
  454.                         { write_exp_elt_opcode (pstate, OP_ATR_SIZE); }
  455.         |        primary TICK_TAG
  456.                         { write_exp_elt_opcode (pstate, OP_ATR_TAG); }
  457.         |       opt_type_prefix TICK_MIN '(' exp ',' exp ')'
  458.                         { write_exp_elt_opcode (pstate, OP_ATR_MIN); }
  459.         |       opt_type_prefix TICK_MAX '(' exp ',' exp ')'
  460.                         { write_exp_elt_opcode (pstate, OP_ATR_MAX); }
  461.         |         opt_type_prefix TICK_POS '(' exp ')'
  462.                         { write_exp_elt_opcode (pstate, OP_ATR_POS); }
  463.         |        type_prefix TICK_VAL '(' exp ')'
  464.                         { write_exp_elt_opcode (pstate, OP_ATR_VAL); }
  465.         |        type_prefix TICK_MODULUS
  466.                         { write_exp_elt_opcode (pstate, OP_ATR_MODULUS); }
  467.         ;

  468. tick_arglist :                        %prec '('
  469.                         { $$ = 1; }
  470.         |         '(' INT ')'
  471.                         { $$ = $2.val; }
  472.         ;

  473. type_prefix :
  474.                 var_or_type
  475.                         {
  476.                           if ($1 == NULL)
  477.                             error (_("Prefix must be type"));
  478.                           write_exp_elt_opcode (pstate, OP_TYPE);
  479.                           write_exp_elt_type (pstate, $1);
  480.                           write_exp_elt_opcode (pstate, OP_TYPE); }
  481.         ;

  482. opt_type_prefix :
  483.                 type_prefix
  484.         |         /* EMPTY */
  485.                         { write_exp_elt_opcode (pstate, OP_TYPE);
  486.                           write_exp_elt_type (pstate,
  487.                                           parse_type (pstate)->builtin_void);
  488.                           write_exp_elt_opcode (pstate, OP_TYPE); }
  489.         ;


  490. primary        :        INT
  491.                         { write_int (pstate, (LONGEST) $1.val, $1.type); }
  492.         ;

  493. primary        :        CHARLIT
  494.                   { write_int (pstate,
  495.                                convert_char_literal (type_qualifier, $1.val),
  496.                                (type_qualifier == NULL)
  497.                                ? $1.type : type_qualifier);
  498.                   }
  499.         ;

  500. primary        :        FLOAT
  501.                         { write_exp_elt_opcode (pstate, OP_DOUBLE);
  502.                           write_exp_elt_type (pstate, $1.type);
  503.                           write_exp_elt_dblcst (pstate, $1.dval);
  504.                           write_exp_elt_opcode (pstate, OP_DOUBLE);
  505.                         }
  506.         ;

  507. primary        :        NULL_PTR
  508.                         { write_int (pstate, 0, type_int (pstate)); }
  509.         ;

  510. primary        :        STRING
  511.                         {
  512.                           write_exp_op_with_string (pstate, OP_STRING, $1);
  513.                         }
  514.         ;

  515. primary :        TRUEKEYWORD
  516.                         { write_int (pstate, 1, type_boolean (pstate)); }
  517.         |        FALSEKEYWORD
  518.                         { write_int (pstate, 0, type_boolean (pstate)); }
  519.         ;

  520. primary        :         NEW NAME
  521.                         { error (_("NEW not implemented.")); }
  522.         ;

  523. var_or_type:        NAME               %prec VAR
  524.                                 { $$ = write_var_or_type (pstate, NULL, $1); }
  525.         |        block NAME  %prec VAR
  526.                                 { $$ = write_var_or_type (pstate, $1, $2); }
  527.         |       NAME TICK_ACCESS
  528.                         {
  529.                           $$ = write_var_or_type (pstate, NULL, $1);
  530.                           if ($$ == NULL)
  531.                             write_exp_elt_opcode (pstate, UNOP_ADDR);
  532.                           else
  533.                             $$ = lookup_pointer_type ($$);
  534.                         }
  535.         |        block NAME TICK_ACCESS
  536.                         {
  537.                           $$ = write_var_or_type (pstate, $1, $2);
  538.                           if ($$ == NULL)
  539.                             write_exp_elt_opcode (pstate, UNOP_ADDR);
  540.                           else
  541.                             $$ = lookup_pointer_type ($$);
  542.                         }
  543.         ;

  544. /* GDB extension */
  545. block   :       NAME COLONCOLON
  546.                         { $$ = block_lookup (NULL, $1.ptr); }
  547.         |        block NAME COLONCOLON
  548.                         { $$ = block_lookup ($1, $2.ptr); }
  549.         ;

  550. aggregate :
  551.                 '(' aggregate_component_list ')'
  552.                         {
  553.                           write_exp_elt_opcode (pstate, OP_AGGREGATE);
  554.                           write_exp_elt_longcst (pstate, $2);
  555.                           write_exp_elt_opcode (pstate, OP_AGGREGATE);
  556.                         }
  557.         ;

  558. aggregate_component_list :
  559.                 component_groups         { $$ = $1; }
  560.         |        positional_list exp
  561.                         { write_exp_elt_opcode (pstate, OP_POSITIONAL);
  562.                           write_exp_elt_longcst (pstate, $1);
  563.                           write_exp_elt_opcode (pstate, OP_POSITIONAL);
  564.                           $$ = $1 + 1;
  565.                         }
  566.         |        positional_list component_groups
  567.                                          { $$ = $1 + $2; }
  568.         ;

  569. positional_list :
  570.                 exp ','
  571.                         { write_exp_elt_opcode (pstate, OP_POSITIONAL);
  572.                           write_exp_elt_longcst (pstate, 0);
  573.                           write_exp_elt_opcode (pstate, OP_POSITIONAL);
  574.                           $$ = 1;
  575.                         }
  576.         |        positional_list exp ','
  577.                         { write_exp_elt_opcode (pstate, OP_POSITIONAL);
  578.                           write_exp_elt_longcst (pstate, $1);
  579.                           write_exp_elt_opcode (pstate, OP_POSITIONAL);
  580.                           $$ = $1 + 1;
  581.                         }
  582.         ;

  583. component_groups:
  584.                 others                         { $$ = 1; }
  585.         |        component_group                 { $$ = 1; }
  586.         |        component_group ',' component_groups
  587.                                          { $$ = $3 + 1; }
  588.         ;

  589. others         :        OTHERS ARROW exp
  590.                         { write_exp_elt_opcode (pstate, OP_OTHERS); }
  591.         ;

  592. component_group :
  593.                 component_associations
  594.                         {
  595.                           write_exp_elt_opcode (pstate, OP_CHOICES);
  596.                           write_exp_elt_longcst (pstate, $1);
  597.                           write_exp_elt_opcode (pstate, OP_CHOICES);
  598.                         }
  599.         ;

  600. /* We use this somewhat obscure definition in order to handle NAME => and
  601.    NAME | differently from exp => and exp |.  ARROW and '|' have a precedence
  602.    above that of the reduction of NAME to var_or_type.  By delaying
  603.    decisions until after the => or '|', we convert the ambiguity to a
  604.    resolved shift/reduce conflict. */
  605. component_associations :
  606.                 NAME ARROW
  607.                         { write_name_assoc (pstate, $1); }
  608.                     exp        { $$ = 1; }
  609.         |        simple_exp ARROW exp
  610.                         { $$ = 1; }
  611.         |        simple_exp DOTDOT simple_exp ARROW
  612.                         { write_exp_elt_opcode (pstate, OP_DISCRETE_RANGE);
  613.                           write_exp_op_with_string (pstate, OP_NAME,
  614.                                                     empty_stoken);
  615.                         }
  616.                     exp { $$ = 1; }
  617.         |        NAME '|'
  618.                         { write_name_assoc (pstate, $1); }
  619.                     component_associations  { $$ = $4 + 1; }
  620.         |        simple_exp '|'
  621.                     component_associations  { $$ = $3 + 1; }
  622.         |        simple_exp DOTDOT simple_exp '|'
  623.                         { write_exp_elt_opcode (pstate, OP_DISCRETE_RANGE); }
  624.                     component_associations  { $$ = $6 + 1; }
  625.         ;

  626. /* Some extensions borrowed from C, for the benefit of those who find they
  627.    can't get used to Ada notation in GDB.  */

  628. primary        :        '*' primary                %prec '.'
  629.                         { write_exp_elt_opcode (pstate, UNOP_IND); }
  630.         |        '&' primary                %prec '.'
  631.                         { write_exp_elt_opcode (pstate, UNOP_ADDR); }
  632.         |        primary '[' exp ']'
  633.                         { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
  634.         ;

  635. %%

  636. /* yylex defined in ada-lex.c: Reads one token, getting characters */
  637. /* through lexptr.  */

  638. /* Remap normal flex interface names (yylex) as well as gratuitiously */
  639. /* global symbol names, so we can have multiple flex-generated parsers */
  640. /* in gdb.  */

  641. /* (See note above on previous definitions for YACC.) */

  642. #define yy_create_buffer ada_yy_create_buffer
  643. #define yy_delete_buffer ada_yy_delete_buffer
  644. #define yy_init_buffer ada_yy_init_buffer
  645. #define yy_load_buffer_state ada_yy_load_buffer_state
  646. #define yy_switch_to_buffer ada_yy_switch_to_buffer
  647. #define yyrestart ada_yyrestart
  648. #define yytext ada_yytext
  649. #define yywrap ada_yywrap

  650. static struct obstack temp_parse_space;

  651. /* The following kludge was found necessary to prevent conflicts between */
  652. /* defs.h and non-standard stdlib.h files.  */
  653. #define qsort __qsort__dummy
  654. #include "ada-lex.c"

  655. int
  656. ada_parse (struct parser_state *par_state)
  657. {
  658.   int result;
  659.   struct cleanup *c = make_cleanup_clear_parser_state (&pstate);

  660.   /* Setting up the parser state.  */
  661.   gdb_assert (par_state != NULL);
  662.   pstate = par_state;

  663.   lexer_init (yyin);                /* (Re-)initialize lexer.  */
  664.   type_qualifier = NULL;
  665.   obstack_free (&temp_parse_space, NULL);
  666.   obstack_init (&temp_parse_space);

  667.   result = yyparse ();
  668.   do_cleanups (c);
  669.   return result;
  670. }

  671. void
  672. yyerror (char *msg)
  673. {
  674.   error (_("Error in expression, near `%s'."), lexptr);
  675. }

  676. /* Emit expression to access an instance of SYM, in block BLOCK (if
  677. * non-NULL), and with :: qualification ORIG_LEFT_CONTEXT.  */
  678. static void
  679. write_var_from_sym (struct parser_state *par_state,
  680.                     const struct block *orig_left_context,
  681.                     const struct block *block,
  682.                     struct symbol *sym)
  683. {
  684.   if (orig_left_context == NULL && symbol_read_needs_frame (sym))
  685.     {
  686.       if (innermost_block == 0
  687.           || contained_in (block, innermost_block))
  688.         innermost_block = block;
  689.     }

  690.   write_exp_elt_opcode (par_state, OP_VAR_VALUE);
  691.   write_exp_elt_block (par_state, block);
  692.   write_exp_elt_sym (par_state, sym);
  693.   write_exp_elt_opcode (par_state, OP_VAR_VALUE);
  694. }

  695. /* Write integer or boolean constant ARG of type TYPE.  */

  696. static void
  697. write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
  698. {
  699.   write_exp_elt_opcode (par_state, OP_LONG);
  700.   write_exp_elt_type (par_state, type);
  701.   write_exp_elt_longcst (par_state, arg);
  702.   write_exp_elt_opcode (par_state, OP_LONG);
  703. }

  704. /* Write an OPCODE, string, OPCODE sequence to the current expression.  */
  705. static void
  706. write_exp_op_with_string (struct parser_state *par_state,
  707.                           enum exp_opcode opcode, struct stoken token)
  708. {
  709.   write_exp_elt_opcode (par_state, opcode);
  710.   write_exp_string (par_state, token);
  711.   write_exp_elt_opcode (par_state, opcode);
  712. }

  713. /* Emit expression corresponding to the renamed object named
  714. * designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
  715. * context of ORIG_LEFT_CONTEXT, to which is applied the operations
  716. * encoded by RENAMING_EXPR.  MAX_DEPTH is the maximum number of
  717. * cascaded renamings to allow.  If ORIG_LEFT_CONTEXT is null, it
  718. * defaults to the currently selected block. ORIG_SYMBOL is the
  719. * symbol that originally encoded the renaming.  It is needed only
  720. * because its prefix also qualifies any index variables used to index
  721. * or slice an array.  It should not be necessary once we go to the
  722. * new encoding entirely (FIXME pnh 7/20/2007).  */

  723. static void
  724. write_object_renaming (struct parser_state *par_state,
  725.                        const struct block *orig_left_context,
  726.                        const char *renamed_entity, int renamed_entity_len,
  727.                        const char *renaming_expr, int max_depth)
  728. {
  729.   char *name;
  730.   enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
  731.   struct ada_symbol_info sym_info;

  732.   if (max_depth <= 0)
  733.     error (_("Could not find renamed symbol"));

  734.   if (orig_left_context == NULL)
  735.     orig_left_context = get_selected_block (NULL);

  736.   name = obstack_copy0 (&temp_parse_space, renamed_entity, renamed_entity_len);
  737.   ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
  738.   if (sym_info.sym == NULL)
  739.     error (_("Could not find renamed variable: %s"), ada_decode (name));
  740.   else if (SYMBOL_CLASS (sym_info.sym) == LOC_TYPEDEF)
  741.     /* We have a renaming of an old-style renaming symbol.  Don't
  742.        trust the block information.  */
  743.     sym_info.block = orig_left_context;

  744.   {
  745.     const char *inner_renamed_entity;
  746.     int inner_renamed_entity_len;
  747.     const char *inner_renaming_expr;

  748.     switch (ada_parse_renaming (sym_info.sym, &inner_renamed_entity,
  749.                                 &inner_renamed_entity_len,
  750.                                 &inner_renaming_expr))
  751.       {
  752.       case ADA_NOT_RENAMING:
  753.         write_var_from_sym (par_state, orig_left_context, sym_info.block,
  754.                             sym_info.sym);
  755.         break;
  756.       case ADA_OBJECT_RENAMING:
  757.         write_object_renaming (par_state, sym_info.block,
  758.                                inner_renamed_entity, inner_renamed_entity_len,
  759.                                inner_renaming_expr, max_depth - 1);
  760.         break;
  761.       default:
  762.         goto BadEncoding;
  763.       }
  764.   }

  765.   slice_state = SIMPLE_INDEX;
  766.   while (*renaming_expr == 'X')
  767.     {
  768.       renaming_expr += 1;

  769.       switch (*renaming_expr) {
  770.       case 'A':
  771.         renaming_expr += 1;
  772.         write_exp_elt_opcode (par_state, UNOP_IND);
  773.         break;
  774.       case 'L':
  775.         slice_state = LOWER_BOUND;
  776.         /* FALLTHROUGH */
  777.       case 'S':
  778.         renaming_expr += 1;
  779.         if (isdigit (*renaming_expr))
  780.           {
  781.             char *next;
  782.             long val = strtol (renaming_expr, &next, 10);
  783.             if (next == renaming_expr)
  784.               goto BadEncoding;
  785.             renaming_expr = next;
  786.             write_exp_elt_opcode (par_state, OP_LONG);
  787.             write_exp_elt_type (par_state, type_int (par_state));
  788.             write_exp_elt_longcst (par_state, (LONGEST) val);
  789.             write_exp_elt_opcode (par_state, OP_LONG);
  790.           }
  791.         else
  792.           {
  793.             const char *end;
  794.             char *index_name;
  795.             struct ada_symbol_info index_sym_info;

  796.             end = strchr (renaming_expr, 'X');
  797.             if (end == NULL)
  798.               end = renaming_expr + strlen (renaming_expr);

  799.             index_name =
  800.               obstack_copy0 (&temp_parse_space, renaming_expr,
  801.                              end - renaming_expr);
  802.             renaming_expr = end;

  803.             ada_lookup_encoded_symbol (index_name, NULL, VAR_DOMAIN,
  804.                                        &index_sym_info);
  805.             if (index_sym_info.sym == NULL)
  806.               error (_("Could not find %s"), index_name);
  807.             else if (SYMBOL_CLASS (index_sym_info.sym) == LOC_TYPEDEF)
  808.               /* Index is an old-style renaming symbol.  */
  809.               index_sym_info.block = orig_left_context;
  810.             write_var_from_sym (par_state, NULL, index_sym_info.block,
  811.                                 index_sym_info.sym);
  812.           }
  813.         if (slice_state == SIMPLE_INDEX)
  814.           {
  815.             write_exp_elt_opcode (par_state, OP_FUNCALL);
  816.             write_exp_elt_longcst (par_state, (LONGEST) 1);
  817.             write_exp_elt_opcode (par_state, OP_FUNCALL);
  818.           }
  819.         else if (slice_state == LOWER_BOUND)
  820.           slice_state = UPPER_BOUND;
  821.         else if (slice_state == UPPER_BOUND)
  822.           {
  823.             write_exp_elt_opcode (par_state, TERNOP_SLICE);
  824.             slice_state = SIMPLE_INDEX;
  825.           }
  826.         break;

  827.       case 'R':
  828.         {
  829.           struct stoken field_name;
  830.           const char *end;
  831.           char *buf;

  832.           renaming_expr += 1;

  833.           if (slice_state != SIMPLE_INDEX)
  834.             goto BadEncoding;
  835.           end = strchr (renaming_expr, 'X');
  836.           if (end == NULL)
  837.             end = renaming_expr + strlen (renaming_expr);
  838.           field_name.length = end - renaming_expr;
  839.           buf = malloc (end - renaming_expr + 1);
  840.           field_name.ptr = buf;
  841.           strncpy (buf, renaming_expr, end - renaming_expr);
  842.           buf[end - renaming_expr] = '\000';
  843.           renaming_expr = end;
  844.           write_exp_op_with_string (par_state, STRUCTOP_STRUCT, field_name);
  845.           break;
  846.         }

  847.       default:
  848.         goto BadEncoding;
  849.       }
  850.     }
  851.   if (slice_state == SIMPLE_INDEX)
  852.     return;

  853. BadEncoding:
  854.   error (_("Internal error in encoding of renaming declaration"));
  855. }

  856. static const struct block*
  857. block_lookup (const struct block *context, const char *raw_name)
  858. {
  859.   const char *name;
  860.   struct ada_symbol_info *syms;
  861.   int nsyms;
  862.   struct symtab *symtab;

  863.   if (raw_name[0] == '\'')
  864.     {
  865.       raw_name += 1;
  866.       name = raw_name;
  867.     }
  868.   else
  869.     name = ada_encode (raw_name);

  870.   nsyms = ada_lookup_symbol_list (name, context, VAR_DOMAIN, &syms);
  871.   if (context == NULL
  872.       && (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK))
  873.     symtab = lookup_symtab (name);
  874.   else
  875.     symtab = NULL;

  876.   if (symtab != NULL)
  877.     return BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
  878.   else if (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK)
  879.     {
  880.       if (context == NULL)
  881.         error (_("No file or function \"%s\"."), raw_name);
  882.       else
  883.         error (_("No function \"%s\" in specified context."), raw_name);
  884.     }
  885.   else
  886.     {
  887.       if (nsyms > 1)
  888.         warning (_("Function name \"%s\" ambiguous here"), raw_name);
  889.       return SYMBOL_BLOCK_VALUE (syms[0].sym);
  890.     }
  891. }

  892. static struct symbol*
  893. select_possible_type_sym (struct ada_symbol_info *syms, int nsyms)
  894. {
  895.   int i;
  896.   int preferred_index;
  897.   struct type *preferred_type;

  898.   preferred_index = -1; preferred_type = NULL;
  899.   for (i = 0; i < nsyms; i += 1)
  900.     switch (SYMBOL_CLASS (syms[i].sym))
  901.       {
  902.       case LOC_TYPEDEF:
  903.         if (ada_prefer_type (SYMBOL_TYPE (syms[i].sym), preferred_type))
  904.           {
  905.             preferred_index = i;
  906.             preferred_type = SYMBOL_TYPE (syms[i].sym);
  907.           }
  908.         break;
  909.       case LOC_REGISTER:
  910.       case LOC_ARG:
  911.       case LOC_REF_ARG:
  912.       case LOC_REGPARM_ADDR:
  913.       case LOC_LOCAL:
  914.       case LOC_COMPUTED:
  915.         return NULL;
  916.       default:
  917.         break;
  918.       }
  919.   if (preferred_type == NULL)
  920.     return NULL;
  921.   return syms[preferred_index].sym;
  922. }

  923. static struct type*
  924. find_primitive_type (struct parser_state *par_state, char *name)
  925. {
  926.   struct type *type;
  927.   type = language_lookup_primitive_type (parse_language (par_state),
  928.                                          parse_gdbarch (par_state),
  929.                                          name);
  930.   if (type == NULL && strcmp ("system__address", name) == 0)
  931.     type = type_system_address (par_state);

  932.   if (type != NULL)
  933.     {
  934.       /* Check to see if we have a regular definition of this
  935.          type that just didn't happen to have been read yet.  */
  936.       struct symbol *sym;
  937.       char *expanded_name =
  938.         (char *) alloca (strlen (name) + sizeof ("standard__"));
  939.       strcpy (expanded_name, "standard__");
  940.       strcat (expanded_name, name);
  941.       sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN, NULL);
  942.       if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  943.         type = SYMBOL_TYPE (sym);
  944.     }

  945.   return type;
  946. }

  947. static int
  948. chop_selector (char *name, int end)
  949. {
  950.   int i;
  951.   for (i = end - 1; i > 0; i -= 1)
  952.     if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
  953.       return i;
  954.   return -1;
  955. }

  956. /* If NAME is a string beginning with a separator (either '__', or
  957.    '.'), chop this separator and return the result; else, return
  958.    NAME.  */

  959. static char *
  960. chop_separator (char *name)
  961. {
  962.   if (*name == '.')
  963.    return name + 1;

  964.   if (name[0] == '_' && name[1] == '_')
  965.     return name + 2;

  966.   return name;
  967. }

  968. /* Given that SELS is a string of the form (<sep><identifier>)*, where
  969.    <sep> is '__' or '.', write the indicated sequence of
  970.    STRUCTOP_STRUCT expression operators. */
  971. static void
  972. write_selectors (struct parser_state *par_state, char *sels)
  973. {
  974.   while (*sels != '\0')
  975.     {
  976.       struct stoken field_name;
  977.       char *p = chop_separator (sels);
  978.       sels = p;
  979.       while (*sels != '\0' && *sels != '.'
  980.              && (sels[0] != '_' || sels[1] != '_'))
  981.         sels += 1;
  982.       field_name.length = sels - p;
  983.       field_name.ptr = p;
  984.       write_exp_op_with_string (par_state, STRUCTOP_STRUCT, field_name);
  985.     }
  986. }

  987. /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
  988.    NAME[0..LEN-1], in block context BLOCK, to be resolved later.  Writes
  989.    a temporary symbol that is valid until the next call to ada_parse.
  990.    */
  991. static void
  992. write_ambiguous_var (struct parser_state *par_state,
  993.                      const struct block *block, char *name, int len)
  994. {
  995.   struct symbol *sym =
  996.     obstack_alloc (&temp_parse_space, sizeof (struct symbol));
  997.   memset (sym, 0, sizeof (struct symbol));
  998.   SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
  999.   SYMBOL_LINKAGE_NAME (sym) = obstack_copy0 (&temp_parse_space, name, len);
  1000.   SYMBOL_LANGUAGE (sym) = language_ada;

  1001.   write_exp_elt_opcode (par_state, OP_VAR_VALUE);
  1002.   write_exp_elt_block (par_state, block);
  1003.   write_exp_elt_sym (par_state, sym);
  1004.   write_exp_elt_opcode (par_state, OP_VAR_VALUE);
  1005. }

  1006. /* A convenient wrapper around ada_get_field_index that takes
  1007.    a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
  1008.    of a NUL-terminated field name.  */

  1009. static int
  1010. ada_nget_field_index (const struct type *type, const char *field_name0,
  1011.                       int field_name_len, int maybe_missing)
  1012. {
  1013.   char *field_name = alloca ((field_name_len + 1) * sizeof (char));

  1014.   strncpy (field_name, field_name0, field_name_len);
  1015.   field_name[field_name_len] = '\0';
  1016.   return ada_get_field_index (type, field_name, maybe_missing);
  1017. }

  1018. /* If encoded_field_name is the name of a field inside symbol SYM,
  1019.    then return the type of that field.  Otherwise, return NULL.

  1020.    This function is actually recursive, so if ENCODED_FIELD_NAME
  1021.    doesn't match one of the fields of our symbol, then try to see
  1022.    if ENCODED_FIELD_NAME could not be a succession of field names
  1023.    (in other words, the user entered an expression of the form
  1024.    TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
  1025.    each field name sequentially to obtain the desired field type.
  1026.    In case of failure, we return NULL.  */

  1027. static struct type *
  1028. get_symbol_field_type (struct symbol *sym, char *encoded_field_name)
  1029. {
  1030.   char *field_name = encoded_field_name;
  1031.   char *subfield_name;
  1032.   struct type *type = SYMBOL_TYPE (sym);
  1033.   int fieldno;

  1034.   if (type == NULL || field_name == NULL)
  1035.     return NULL;
  1036.   type = check_typedef (type);

  1037.   while (field_name[0] != '\0')
  1038.     {
  1039.       field_name = chop_separator (field_name);

  1040.       fieldno = ada_get_field_index (type, field_name, 1);
  1041.       if (fieldno >= 0)
  1042.         return TYPE_FIELD_TYPE (type, fieldno);

  1043.       subfield_name = field_name;
  1044.       while (*subfield_name != '\0' && *subfield_name != '.'
  1045.              && (subfield_name[0] != '_' || subfield_name[1] != '_'))
  1046.         subfield_name += 1;

  1047.       if (subfield_name[0] == '\0')
  1048.         return NULL;

  1049.       fieldno = ada_nget_field_index (type, field_name,
  1050.                                       subfield_name - field_name, 1);
  1051.       if (fieldno < 0)
  1052.         return NULL;

  1053.       type = TYPE_FIELD_TYPE (type, fieldno);
  1054.       field_name = subfield_name;
  1055.     }

  1056.   return NULL;
  1057. }

  1058. /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
  1059.    expression_block_context if NULL).  If it denotes a type, return
  1060.    that type.  Otherwise, write expression code to evaluate it as an
  1061.    object and return NULL. In this second case, NAME0 will, in general,
  1062.    have the form <name>(.<selector_name>)*, where <name> is an object
  1063.    or renaming encoded in the debugging data.  Calls error if no
  1064.    prefix <name> matches a name in the debugging data (i.e., matches
  1065.    either a complete name or, as a wild-card match, the final
  1066.    identifier).  */

  1067. static struct type*
  1068. write_var_or_type (struct parser_state *par_state,
  1069.                    const struct block *block, struct stoken name0)
  1070. {
  1071.   int depth;
  1072.   char *encoded_name;
  1073.   int name_len;

  1074.   if (block == NULL)
  1075.     block = expression_context_block;

  1076.   encoded_name = ada_encode (name0.ptr);
  1077.   name_len = strlen (encoded_name);
  1078.   encoded_name = obstack_copy0 (&temp_parse_space, encoded_name, name_len);
  1079.   for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
  1080.     {
  1081.       int tail_index;

  1082.       tail_index = name_len;
  1083.       while (tail_index > 0)
  1084.         {
  1085.           int nsyms;
  1086.           struct ada_symbol_info *syms;
  1087.           struct symbol *type_sym;
  1088.           struct symbol *renaming_sym;
  1089.           const char* renaming;
  1090.           int renaming_len;
  1091.           const char* renaming_expr;
  1092.           int terminator = encoded_name[tail_index];

  1093.           encoded_name[tail_index] = '\0';
  1094.           nsyms = ada_lookup_symbol_list (encoded_name, block,
  1095.                                           VAR_DOMAIN, &syms);
  1096.           encoded_name[tail_index] = terminator;

  1097.           /* A single symbol may rename a package or object. */

  1098.           /* This should go away when we move entirely to new version.
  1099.              FIXME pnh 7/20/2007. */
  1100.           if (nsyms == 1)
  1101.             {
  1102.               struct symbol *ren_sym =
  1103.                 ada_find_renaming_symbol (syms[0].sym, syms[0].block);

  1104.               if (ren_sym != NULL)
  1105.                 syms[0].sym = ren_sym;
  1106.             }

  1107.           type_sym = select_possible_type_sym (syms, nsyms);

  1108.           if (type_sym != NULL)
  1109.             renaming_sym = type_sym;
  1110.           else if (nsyms == 1)
  1111.             renaming_sym = syms[0].sym;
  1112.           else
  1113.             renaming_sym = NULL;

  1114.           switch (ada_parse_renaming (renaming_sym, &renaming,
  1115.                                       &renaming_len, &renaming_expr))
  1116.             {
  1117.             case ADA_NOT_RENAMING:
  1118.               break;
  1119.             case ADA_PACKAGE_RENAMING:
  1120.             case ADA_EXCEPTION_RENAMING:
  1121.             case ADA_SUBPROGRAM_RENAMING:
  1122.               {
  1123.                 char *new_name
  1124.                   = obstack_alloc (&temp_parse_space,
  1125.                                    renaming_len + name_len - tail_index + 1);
  1126.                 strncpy (new_name, renaming, renaming_len);
  1127.                 strcpy (new_name + renaming_len, encoded_name + tail_index);
  1128.                 encoded_name = new_name;
  1129.                 name_len = renaming_len + name_len - tail_index;
  1130.                 goto TryAfterRenaming;
  1131.               }
  1132.             case ADA_OBJECT_RENAMING:
  1133.               write_object_renaming (par_state, block, renaming, renaming_len,
  1134.                                      renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
  1135.               write_selectors (par_state, encoded_name + tail_index);
  1136.               return NULL;
  1137.             default:
  1138.               internal_error (__FILE__, __LINE__,
  1139.                               _("impossible value from ada_parse_renaming"));
  1140.             }

  1141.           if (type_sym != NULL)
  1142.             {
  1143.               struct type *field_type;

  1144.               if (tail_index == name_len)
  1145.                 return SYMBOL_TYPE (type_sym);

  1146.               /* We have some extraneous characters after the type name.
  1147.                  If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
  1148.                  then try to get the type of FIELDN.  */
  1149.               field_type
  1150.                 = get_symbol_field_type (type_sym, encoded_name + tail_index);
  1151.               if (field_type != NULL)
  1152.                 return field_type;
  1153.               else
  1154.                 error (_("Invalid attempt to select from type: \"%s\"."),
  1155.                        name0.ptr);
  1156.             }
  1157.           else if (tail_index == name_len && nsyms == 0)
  1158.             {
  1159.               struct type *type = find_primitive_type (par_state,
  1160.                                                        encoded_name);

  1161.               if (type != NULL)
  1162.                 return type;
  1163.             }

  1164.           if (nsyms == 1)
  1165.             {
  1166.               write_var_from_sym (par_state, block, syms[0].block,
  1167.                                   syms[0].sym);
  1168.               write_selectors (par_state, encoded_name + tail_index);
  1169.               return NULL;
  1170.             }
  1171.           else if (nsyms == 0)
  1172.             {
  1173.               struct bound_minimal_symbol msym
  1174.                 = ada_lookup_simple_minsym (encoded_name);
  1175.               if (msym.minsym != NULL)
  1176.                 {
  1177.                   write_exp_msymbol (par_state, msym);
  1178.                   /* Maybe cause error here rather than later? FIXME? */
  1179.                   write_selectors (par_state, encoded_name + tail_index);
  1180.                   return NULL;
  1181.                 }

  1182.               if (tail_index == name_len
  1183.                   && strncmp (encoded_name, "standard__",
  1184.                               sizeof ("standard__") - 1) == 0)
  1185.                 error (_("No definition of \"%s\" found."), name0.ptr);

  1186.               tail_index = chop_selector (encoded_name, tail_index);
  1187.             }
  1188.           else
  1189.             {
  1190.               write_ambiguous_var (par_state, block, encoded_name,
  1191.                                    tail_index);
  1192.               write_selectors (par_state, encoded_name + tail_index);
  1193.               return NULL;
  1194.             }
  1195.         }

  1196.       if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
  1197.         error (_("No symbol table is loaded.  Use the \"file\" command."));
  1198.       if (block == expression_context_block)
  1199.         error (_("No definition of \"%s\" in current context."), name0.ptr);
  1200.       else
  1201.         error (_("No definition of \"%s\" in specified context."), name0.ptr);

  1202.     TryAfterRenaming: ;
  1203.     }

  1204.   error (_("Could not find renamed symbol \"%s\""), name0.ptr);

  1205. }

  1206. /* Write a left side of a component association (e.g., NAME in NAME =>
  1207.    exp).  If NAME has the form of a selected component, write it as an
  1208.    ordinary expression.  If it is a simple variable that unambiguously
  1209.    corresponds to exactly one symbol that does not denote a type or an
  1210.    object renaming, also write it normally as an OP_VAR_VALUE.
  1211.    Otherwise, write it as an OP_NAME.

  1212.    Unfortunately, we don't know at this point whether NAME is supposed
  1213.    to denote a record component name or the value of an array index.
  1214.    Therefore, it is not appropriate to disambiguate an ambiguous name
  1215.    as we normally would, nor to replace a renaming with its referent.
  1216.    As a result, in the (one hopes) rare case that one writes an
  1217.    aggregate such as (R => 42) where R renames an object or is an
  1218.    ambiguous name, one must write instead ((R) => 42). */

  1219. static void
  1220. write_name_assoc (struct parser_state *par_state, struct stoken name)
  1221. {
  1222.   if (strchr (name.ptr, '.') == NULL)
  1223.     {
  1224.       struct ada_symbol_info *syms;
  1225.       int nsyms = ada_lookup_symbol_list (name.ptr, expression_context_block,
  1226.                                           VAR_DOMAIN, &syms);
  1227.       if (nsyms != 1 || SYMBOL_CLASS (syms[0].sym) == LOC_TYPEDEF)
  1228.         write_exp_op_with_string (par_state, OP_NAME, name);
  1229.       else
  1230.         write_var_from_sym (par_state, NULL, syms[0].block, syms[0].sym);
  1231.     }
  1232.   else
  1233.     if (write_var_or_type (par_state, NULL, name) != NULL)
  1234.       error (_("Invalid use of type."));
  1235. }

  1236. /* Convert the character literal whose ASCII value would be VAL to the
  1237.    appropriate value of type TYPE, if there is a translation.
  1238.    Otherwise return VAL.  Hence, in an enumeration type ('A', 'B'),
  1239.    the literal 'A' (VAL == 65), returns 0.  */

  1240. static LONGEST
  1241. convert_char_literal (struct type *type, LONGEST val)
  1242. {
  1243.   char name[7];
  1244.   int f;

  1245.   if (type == NULL)
  1246.     return val;
  1247.   type = check_typedef (type);
  1248.   if (TYPE_CODE (type) != TYPE_CODE_ENUM)
  1249.     return val;

  1250.   xsnprintf (name, sizeof (name), "QU%02x", (int) val);
  1251.   for (f = 0; f < TYPE_NFIELDS (type); f += 1)
  1252.     {
  1253.       if (strcmp (name, TYPE_FIELD_NAME (type, f)) == 0)
  1254.         return TYPE_FIELD_ENUMVAL (type, f);
  1255.     }
  1256.   return val;
  1257. }

  1258. static struct type *
  1259. type_int (struct parser_state *par_state)
  1260. {
  1261.   return parse_type (par_state)->builtin_int;
  1262. }

  1263. static struct type *
  1264. type_long (struct parser_state *par_state)
  1265. {
  1266.   return parse_type (par_state)->builtin_long;
  1267. }

  1268. static struct type *
  1269. type_long_long (struct parser_state *par_state)
  1270. {
  1271.   return parse_type (par_state)->builtin_long_long;
  1272. }

  1273. static struct type *
  1274. type_float (struct parser_state *par_state)
  1275. {
  1276.   return parse_type (par_state)->builtin_float;
  1277. }

  1278. static struct type *
  1279. type_double (struct parser_state *par_state)
  1280. {
  1281.   return parse_type (par_state)->builtin_double;
  1282. }

  1283. static struct type *
  1284. type_long_double (struct parser_state *par_state)
  1285. {
  1286.   return parse_type (par_state)->builtin_long_double;
  1287. }

  1288. static struct type *
  1289. type_char (struct parser_state *par_state)
  1290. {
  1291.   return language_string_char_type (parse_language (par_state),
  1292.                                     parse_gdbarch (par_state));
  1293. }

  1294. static struct type *
  1295. type_boolean (struct parser_state *par_state)
  1296. {
  1297.   return parse_type (par_state)->builtin_bool;
  1298. }

  1299. static struct type *
  1300. type_system_address (struct parser_state *par_state)
  1301. {
  1302.   struct type *type
  1303.     = language_lookup_primitive_type (parse_language (par_state),
  1304.                                       parse_gdbarch (par_state),
  1305.                                       "system__address");
  1306.   return  type != NULL ? type : parse_type (par_state)->builtin_data_ptr;
  1307. }

  1308. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1309. extern initialize_file_ftype _initialize_ada_exp;

  1310. void
  1311. _initialize_ada_exp (void)
  1312. {
  1313.   obstack_init (&temp_parse_space);
  1314. }