(Ada) Fix Length attribute on array access
[deliverable/binutils-gdb.git] / gdb / ada-exp.y
CommitLineData
14f9c5c9 1/* YACC parser for Ada expressions, for GDB.
e2882c85 2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
14f9c5c9 3
5b1ba0e5 4 This file is part of GDB.
14f9c5c9 5
5b1ba0e5
NS
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
14f9c5c9 10
5b1ba0e5
NS
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
14f9c5c9 15
5b1ba0e5
NS
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
14f9c5c9
AS
18
19/* Parse an Ada expression from text in a string,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result.
27
28 malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
4c4b4cd2 35
14f9c5c9
AS
36%{
37
38#include "defs.h"
14f9c5c9
AS
39#include <ctype.h>
40#include "expression.h"
41#include "value.h"
42#include "parser-defs.h"
43#include "language.h"
44#include "ada-lang.h"
45#include "bfd.h" /* Required by objfiles.h. */
46#include "symfile.h" /* Required by objfiles.h. */
47#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
48#include "frame.h"
fe898f56 49#include "block.h"
14f9c5c9 50
410a0ff2 51#define parse_type(ps) builtin_type (parse_gdbarch (ps))
3e79cecf 52
b3f11165
PA
53/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
54 etc). */
55#define GDB_YY_REMAP_PREFIX ada_
56#include "yy-remap.h"
f461f5cf 57
14f9c5c9 58struct name_info {
4c4b4cd2
PH
59 struct symbol *sym;
60 struct minimal_symbol *msym;
3977b71f 61 const struct block *block;
14f9c5c9
AS
62 struct stoken stoken;
63};
64
410a0ff2
SDJ
65/* The state of the parser, used internally when we are parsing the
66 expression. */
67
68static struct parser_state *pstate = NULL;
69
52ce6436
PH
70static struct stoken empty_stoken = { "", 0 };
71
14f9c5c9 72/* If expression is in the context of TYPE'(...), then TYPE, else
4c4b4cd2
PH
73 * NULL. */
74static struct type *type_qualifier;
14f9c5c9
AS
75
76int yyparse (void);
77
78static int yylex (void);
79
a121b7c1 80void yyerror (const char *);
14f9c5c9 81
410a0ff2 82static void write_int (struct parser_state *, LONGEST, struct type *);
14f9c5c9 83
410a0ff2
SDJ
84static void write_object_renaming (struct parser_state *,
85 const struct block *, const char *, int,
aeb5907d 86 const char *, int);
14f9c5c9 87
410a0ff2
SDJ
88static struct type* write_var_or_type (struct parser_state *,
89 const struct block *, struct stoken);
52ce6436 90
410a0ff2 91static void write_name_assoc (struct parser_state *, struct stoken);
52ce6436 92
410a0ff2
SDJ
93static void write_exp_op_with_string (struct parser_state *, enum exp_opcode,
94 struct stoken);
52ce6436 95
3977b71f 96static const struct block *block_lookup (const struct block *, const char *);
14f9c5c9 97
19c1ef65 98static LONGEST convert_char_literal (struct type *, LONGEST);
72d5681a 99
410a0ff2
SDJ
100static void write_ambiguous_var (struct parser_state *,
101 const struct block *, char *, int);
52ce6436 102
410a0ff2 103static struct type *type_int (struct parser_state *);
72d5681a 104
410a0ff2 105static struct type *type_long (struct parser_state *);
72d5681a 106
410a0ff2 107static struct type *type_long_long (struct parser_state *);
72d5681a 108
410a0ff2 109static struct type *type_long_double (struct parser_state *);
72d5681a 110
410a0ff2 111static struct type *type_char (struct parser_state *);
72d5681a 112
410a0ff2 113static struct type *type_boolean (struct parser_state *);
690cc4eb 114
410a0ff2 115static struct type *type_system_address (struct parser_state *);
52ce6436 116
4c4b4cd2 117%}
14f9c5c9
AS
118
119%union
120 {
121 LONGEST lval;
122 struct {
123 LONGEST val;
124 struct type *type;
125 } typed_val;
126 struct {
edd079d9 127 gdb_byte val[16];
14f9c5c9
AS
128 struct type *type;
129 } typed_val_float;
130 struct type *tval;
131 struct stoken sval;
3977b71f 132 const struct block *bval;
14f9c5c9 133 struct internalvar *ivar;
14f9c5c9
AS
134 }
135
52ce6436
PH
136%type <lval> positional_list component_groups component_associations
137%type <lval> aggregate_component_list
138%type <tval> var_or_type
14f9c5c9
AS
139
140%token <typed_val> INT NULL_PTR CHARLIT
141%token <typed_val_float> FLOAT
690cc4eb 142%token TRUEKEYWORD FALSEKEYWORD
52ce6436
PH
143%token COLONCOLON
144%token <sval> STRING NAME DOT_ID
4c4b4cd2 145%type <bval> block
14f9c5c9
AS
146%type <lval> arglist tick_arglist
147
148%type <tval> save_qualifier
149
150%token DOT_ALL
151
152/* Special type cases, put in to allow the parser to distinguish different
153 legal basetypes. */
4c4b4cd2 154%token <sval> SPECIAL_VARIABLE
14f9c5c9
AS
155
156%nonassoc ASSIGN
157%left _AND_ OR XOR THEN ELSE
158%left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
159%left '@'
160%left '+' '-' '&'
161%left UNARY
162%left '*' '/' MOD REM
163%right STARSTAR ABS NOT
52ce6436
PH
164
165/* Artificial token to give NAME => ... and NAME | priority over reducing
166 NAME to <primary> and to give <primary>' priority over reducing <primary>
167 to <simple_exp>. */
168%nonassoc VAR
169
170%nonassoc ARROW '|'
171
14f9c5c9
AS
172%right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
173%right TICK_MAX TICK_MIN TICK_MODULUS
174%right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
52ce6436
PH
175 /* The following are right-associative only so that reductions at this
176 precedence have lower precedence than '.' and '('. The syntax still
177 forces a.b.c, e.g., to be LEFT-associated. */
14f9c5c9
AS
178%right '.' '(' '[' DOT_ID DOT_ALL
179
52ce6436 180%token NEW OTHERS
14f9c5c9
AS
181
182\f
183%%
184
185start : exp1
14f9c5c9
AS
186 ;
187
188/* Expressions, including the sequencing operator. */
189exp1 : exp
190 | exp1 ';' exp
410a0ff2 191 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
52ce6436 192 | primary ASSIGN exp /* Extension for convenience */
410a0ff2 193 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
14f9c5c9
AS
194 ;
195
196/* Expressions, not including the sequencing operator. */
52ce6436 197primary : primary DOT_ALL
410a0ff2 198 { write_exp_elt_opcode (pstate, UNOP_IND); }
14f9c5c9
AS
199 ;
200
52ce6436 201primary : primary DOT_ID
410a0ff2
SDJ
202 { write_exp_op_with_string (pstate, STRUCTOP_STRUCT,
203 $2); }
14f9c5c9
AS
204 ;
205
52ce6436 206primary : primary '(' arglist ')'
14f9c5c9 207 {
410a0ff2
SDJ
208 write_exp_elt_opcode (pstate, OP_FUNCALL);
209 write_exp_elt_longcst (pstate, $3);
210 write_exp_elt_opcode (pstate, OP_FUNCALL);
14f9c5c9 211 }
52ce6436 212 | var_or_type '(' arglist ')'
14f9c5c9 213 {
52ce6436
PH
214 if ($1 != NULL)
215 {
216 if ($3 != 1)
e1d5a0d2 217 error (_("Invalid conversion"));
410a0ff2
SDJ
218 write_exp_elt_opcode (pstate, UNOP_CAST);
219 write_exp_elt_type (pstate, $1);
220 write_exp_elt_opcode (pstate, UNOP_CAST);
52ce6436
PH
221 }
222 else
223 {
410a0ff2
SDJ
224 write_exp_elt_opcode (pstate, OP_FUNCALL);
225 write_exp_elt_longcst (pstate, $3);
226 write_exp_elt_opcode (pstate, OP_FUNCALL);
52ce6436 227 }
14f9c5c9
AS
228 }
229 ;
230
52ce6436
PH
231primary : var_or_type '\'' save_qualifier { type_qualifier = $1; }
232 '(' exp ')'
14f9c5c9 233 {
52ce6436 234 if ($1 == NULL)
e1d5a0d2 235 error (_("Type required for qualification"));
410a0ff2
SDJ
236 write_exp_elt_opcode (pstate, UNOP_QUAL);
237 write_exp_elt_type (pstate, $1);
238 write_exp_elt_opcode (pstate, UNOP_QUAL);
14f9c5c9
AS
239 type_qualifier = $3;
240 }
241 ;
242
243save_qualifier : { $$ = type_qualifier; }
525d6a61 244 ;
14f9c5c9 245
52ce6436
PH
246primary :
247 primary '(' simple_exp DOTDOT simple_exp ')'
410a0ff2 248 { write_exp_elt_opcode (pstate, TERNOP_SLICE); }
52ce6436
PH
249 | var_or_type '(' simple_exp DOTDOT simple_exp ')'
250 { if ($1 == NULL)
410a0ff2 251 write_exp_elt_opcode (pstate, TERNOP_SLICE);
52ce6436 252 else
e1d5a0d2 253 error (_("Cannot slice a type"));
52ce6436 254 }
14f9c5c9
AS
255 ;
256
52ce6436 257primary : '(' exp1 ')' { }
14f9c5c9
AS
258 ;
259
52ce6436
PH
260/* The following rule causes a conflict with the type conversion
261 var_or_type (exp)
262 To get around it, we give '(' higher priority and add bridge rules for
263 var_or_type (exp, exp, ...)
264 var_or_type (exp .. exp)
265 We also have the action for var_or_type(exp) generate a function call
266 when the first symbol does not denote a type. */
267
268primary : var_or_type %prec VAR
269 { if ($1 != NULL)
270 {
410a0ff2
SDJ
271 write_exp_elt_opcode (pstate, OP_TYPE);
272 write_exp_elt_type (pstate, $1);
273 write_exp_elt_opcode (pstate, OP_TYPE);
52ce6436
PH
274 }
275 }
14f9c5c9
AS
276 ;
277
52ce6436 278primary : SPECIAL_VARIABLE /* Various GDB extensions */
410a0ff2 279 { write_dollar_variable (pstate, $1); }
14f9c5c9
AS
280 ;
281
52ce6436
PH
282primary : aggregate
283 ;
14f9c5c9 284
52ce6436 285simple_exp : primary
14f9c5c9
AS
286 ;
287
52ce6436 288simple_exp : '-' simple_exp %prec UNARY
410a0ff2 289 { write_exp_elt_opcode (pstate, UNOP_NEG); }
14f9c5c9
AS
290 ;
291
52ce6436 292simple_exp : '+' simple_exp %prec UNARY
410a0ff2 293 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
14f9c5c9
AS
294 ;
295
52ce6436 296simple_exp : NOT simple_exp %prec UNARY
410a0ff2 297 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
14f9c5c9
AS
298 ;
299
52ce6436 300simple_exp : ABS simple_exp %prec UNARY
410a0ff2 301 { write_exp_elt_opcode (pstate, UNOP_ABS); }
14f9c5c9
AS
302 ;
303
304arglist : { $$ = 0; }
305 ;
306
307arglist : exp
308 { $$ = 1; }
52ce6436 309 | NAME ARROW exp
14f9c5c9
AS
310 { $$ = 1; }
311 | arglist ',' exp
312 { $$ = $1 + 1; }
52ce6436 313 | arglist ',' NAME ARROW exp
14f9c5c9
AS
314 { $$ = $1 + 1; }
315 ;
316
bb28a9dc 317primary : '{' var_or_type '}' primary %prec '.'
14f9c5c9 318 /* GDB extension */
52ce6436
PH
319 {
320 if ($2 == NULL)
e1d5a0d2 321 error (_("Type required within braces in coercion"));
410a0ff2
SDJ
322 write_exp_elt_opcode (pstate, UNOP_MEMVAL);
323 write_exp_elt_type (pstate, $2);
324 write_exp_elt_opcode (pstate, UNOP_MEMVAL);
14f9c5c9
AS
325 }
326 ;
327
328/* Binary operators in order of decreasing precedence. */
329
52ce6436 330simple_exp : simple_exp STARSTAR simple_exp
410a0ff2 331 { write_exp_elt_opcode (pstate, BINOP_EXP); }
14f9c5c9
AS
332 ;
333
52ce6436 334simple_exp : simple_exp '*' simple_exp
410a0ff2 335 { write_exp_elt_opcode (pstate, BINOP_MUL); }
14f9c5c9
AS
336 ;
337
52ce6436 338simple_exp : simple_exp '/' simple_exp
410a0ff2 339 { write_exp_elt_opcode (pstate, BINOP_DIV); }
14f9c5c9
AS
340 ;
341
52ce6436 342simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
410a0ff2 343 { write_exp_elt_opcode (pstate, BINOP_REM); }
14f9c5c9
AS
344 ;
345
52ce6436 346simple_exp : simple_exp MOD simple_exp
410a0ff2 347 { write_exp_elt_opcode (pstate, BINOP_MOD); }
14f9c5c9
AS
348 ;
349
52ce6436 350simple_exp : simple_exp '@' simple_exp /* GDB extension */
410a0ff2 351 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
14f9c5c9
AS
352 ;
353
52ce6436 354simple_exp : simple_exp '+' simple_exp
410a0ff2 355 { write_exp_elt_opcode (pstate, BINOP_ADD); }
14f9c5c9
AS
356 ;
357
52ce6436 358simple_exp : simple_exp '&' simple_exp
410a0ff2 359 { write_exp_elt_opcode (pstate, BINOP_CONCAT); }
14f9c5c9
AS
360 ;
361
52ce6436 362simple_exp : simple_exp '-' simple_exp
410a0ff2 363 { write_exp_elt_opcode (pstate, BINOP_SUB); }
14f9c5c9
AS
364 ;
365
52ce6436
PH
366relation : simple_exp
367 ;
368
369relation : simple_exp '=' simple_exp
410a0ff2 370 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
14f9c5c9
AS
371 ;
372
52ce6436 373relation : simple_exp NOTEQUAL simple_exp
410a0ff2 374 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
14f9c5c9
AS
375 ;
376
52ce6436 377relation : simple_exp LEQ simple_exp
410a0ff2 378 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
14f9c5c9
AS
379 ;
380
52ce6436 381relation : simple_exp IN simple_exp DOTDOT simple_exp
410a0ff2 382 { write_exp_elt_opcode (pstate, TERNOP_IN_RANGE); }
52ce6436 383 | simple_exp IN primary TICK_RANGE tick_arglist
410a0ff2
SDJ
384 { write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
385 write_exp_elt_longcst (pstate, (LONGEST) $5);
386 write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
14f9c5c9 387 }
52ce6436
PH
388 | simple_exp IN var_or_type %prec TICK_ACCESS
389 {
390 if ($3 == NULL)
e1d5a0d2 391 error (_("Right operand of 'in' must be type"));
410a0ff2
SDJ
392 write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
393 write_exp_elt_type (pstate, $3);
394 write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
14f9c5c9 395 }
52ce6436 396 | simple_exp NOT IN simple_exp DOTDOT simple_exp
410a0ff2
SDJ
397 { write_exp_elt_opcode (pstate, TERNOP_IN_RANGE);
398 write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT);
14f9c5c9 399 }
52ce6436 400 | simple_exp NOT IN primary TICK_RANGE tick_arglist
410a0ff2
SDJ
401 { write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
402 write_exp_elt_longcst (pstate, (LONGEST) $6);
403 write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS);
404 write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT);
14f9c5c9 405 }
52ce6436
PH
406 | simple_exp NOT IN var_or_type %prec TICK_ACCESS
407 {
408 if ($4 == NULL)
e1d5a0d2 409 error (_("Right operand of 'in' must be type"));
410a0ff2
SDJ
410 write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
411 write_exp_elt_type (pstate, $4);
412 write_exp_elt_opcode (pstate, UNOP_IN_RANGE);
413 write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT);
14f9c5c9
AS
414 }
415 ;
416
52ce6436 417relation : simple_exp GEQ simple_exp
410a0ff2 418 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
14f9c5c9
AS
419 ;
420
52ce6436 421relation : simple_exp '<' simple_exp
410a0ff2 422 { write_exp_elt_opcode (pstate, BINOP_LESS); }
14f9c5c9
AS
423 ;
424
52ce6436 425relation : simple_exp '>' simple_exp
410a0ff2 426 { write_exp_elt_opcode (pstate, BINOP_GTR); }
14f9c5c9
AS
427 ;
428
52ce6436
PH
429exp : relation
430 | and_exp
431 | and_then_exp
432 | or_exp
433 | or_else_exp
434 | xor_exp
435 ;
436
437and_exp :
438 relation _AND_ relation
410a0ff2 439 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
52ce6436 440 | and_exp _AND_ relation
410a0ff2 441 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
52ce6436 442 ;
14f9c5c9 443
52ce6436
PH
444and_then_exp :
445 relation _AND_ THEN relation
410a0ff2 446 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
52ce6436 447 | and_then_exp _AND_ THEN relation
410a0ff2 448 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
14f9c5c9
AS
449 ;
450
52ce6436
PH
451or_exp :
452 relation OR relation
410a0ff2 453 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
52ce6436 454 | or_exp OR relation
410a0ff2 455 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
52ce6436 456 ;
14f9c5c9 457
52ce6436
PH
458or_else_exp :
459 relation OR ELSE relation
410a0ff2 460 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
52ce6436 461 | or_else_exp OR ELSE relation
410a0ff2 462 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
14f9c5c9
AS
463 ;
464
52ce6436 465xor_exp : relation XOR relation
410a0ff2 466 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
52ce6436 467 | xor_exp XOR relation
410a0ff2 468 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
14f9c5c9
AS
469 ;
470
52ce6436 471/* Primaries can denote types (OP_TYPE). In cases such as
f98ce7c2 472 primary TICK_ADDRESS, where a type would be invalid, it will be
52ce6436
PH
473 caught when evaluate_subexp in ada-lang.c tries to evaluate the
474 primary, expecting a value. Precedence rules resolve the ambiguity
475 in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A
476 construct such as aType'access'access will again cause an error when
477 aType'access evaluates to a type that evaluate_subexp attempts to
478 evaluate. */
479primary : primary TICK_ACCESS
410a0ff2 480 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
52ce6436 481 | primary TICK_ADDRESS
410a0ff2
SDJ
482 { write_exp_elt_opcode (pstate, UNOP_ADDR);
483 write_exp_elt_opcode (pstate, UNOP_CAST);
484 write_exp_elt_type (pstate,
485 type_system_address (pstate));
486 write_exp_elt_opcode (pstate, UNOP_CAST);
14f9c5c9 487 }
52ce6436 488 | primary TICK_FIRST tick_arglist
410a0ff2
SDJ
489 { write_int (pstate, $3, type_int (pstate));
490 write_exp_elt_opcode (pstate, OP_ATR_FIRST); }
52ce6436 491 | primary TICK_LAST tick_arglist
410a0ff2
SDJ
492 { write_int (pstate, $3, type_int (pstate));
493 write_exp_elt_opcode (pstate, OP_ATR_LAST); }
52ce6436 494 | primary TICK_LENGTH tick_arglist
410a0ff2
SDJ
495 { write_int (pstate, $3, type_int (pstate));
496 write_exp_elt_opcode (pstate, OP_ATR_LENGTH); }
52ce6436 497 | primary TICK_SIZE
410a0ff2 498 { write_exp_elt_opcode (pstate, OP_ATR_SIZE); }
52ce6436 499 | primary TICK_TAG
410a0ff2 500 { write_exp_elt_opcode (pstate, OP_ATR_TAG); }
14f9c5c9 501 | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
410a0ff2 502 { write_exp_elt_opcode (pstate, OP_ATR_MIN); }
14f9c5c9 503 | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
410a0ff2 504 { write_exp_elt_opcode (pstate, OP_ATR_MAX); }
14f9c5c9 505 | opt_type_prefix TICK_POS '(' exp ')'
410a0ff2 506 { write_exp_elt_opcode (pstate, OP_ATR_POS); }
14f9c5c9 507 | type_prefix TICK_VAL '(' exp ')'
410a0ff2 508 { write_exp_elt_opcode (pstate, OP_ATR_VAL); }
4c4b4cd2 509 | type_prefix TICK_MODULUS
410a0ff2 510 { write_exp_elt_opcode (pstate, OP_ATR_MODULUS); }
14f9c5c9
AS
511 ;
512
513tick_arglist : %prec '('
514 { $$ = 1; }
515 | '(' INT ')'
516 { $$ = $2.val; }
517 ;
518
519type_prefix :
52ce6436
PH
520 var_or_type
521 {
522 if ($1 == NULL)
e1d5a0d2 523 error (_("Prefix must be type"));
410a0ff2
SDJ
524 write_exp_elt_opcode (pstate, OP_TYPE);
525 write_exp_elt_type (pstate, $1);
526 write_exp_elt_opcode (pstate, OP_TYPE); }
14f9c5c9
AS
527 ;
528
529opt_type_prefix :
530 type_prefix
4c4b4cd2 531 | /* EMPTY */
410a0ff2
SDJ
532 { write_exp_elt_opcode (pstate, OP_TYPE);
533 write_exp_elt_type (pstate,
534 parse_type (pstate)->builtin_void);
535 write_exp_elt_opcode (pstate, OP_TYPE); }
14f9c5c9 536 ;
4c4b4cd2 537
14f9c5c9 538
52ce6436 539primary : INT
410a0ff2 540 { write_int (pstate, (LONGEST) $1.val, $1.type); }
14f9c5c9
AS
541 ;
542
52ce6436 543primary : CHARLIT
410a0ff2
SDJ
544 { write_int (pstate,
545 convert_char_literal (type_qualifier, $1.val),
4c4b4cd2
PH
546 (type_qualifier == NULL)
547 ? $1.type : type_qualifier);
548 }
525d6a61 549 ;
4c4b4cd2 550
52ce6436 551primary : FLOAT
edd079d9 552 { write_exp_elt_opcode (pstate, OP_FLOAT);
410a0ff2 553 write_exp_elt_type (pstate, $1.type);
edd079d9
UW
554 write_exp_elt_floatcst (pstate, $1.val);
555 write_exp_elt_opcode (pstate, OP_FLOAT);
14f9c5c9
AS
556 }
557 ;
558
52ce6436 559primary : NULL_PTR
410a0ff2 560 { write_int (pstate, 0, type_int (pstate)); }
525d6a61 561 ;
14f9c5c9 562
52ce6436 563primary : STRING
4c4b4cd2 564 {
410a0ff2 565 write_exp_op_with_string (pstate, OP_STRING, $1);
4c4b4cd2 566 }
14f9c5c9
AS
567 ;
568
690cc4eb 569primary : TRUEKEYWORD
410a0ff2 570 { write_int (pstate, 1, type_boolean (pstate)); }
690cc4eb 571 | FALSEKEYWORD
410a0ff2 572 { write_int (pstate, 0, type_boolean (pstate)); }
690cc4eb
PH
573 ;
574
52ce6436 575primary : NEW NAME
e1d5a0d2 576 { error (_("NEW not implemented.")); }
14f9c5c9
AS
577 ;
578
52ce6436 579var_or_type: NAME %prec VAR
410a0ff2 580 { $$ = write_var_or_type (pstate, NULL, $1); }
52ce6436 581 | block NAME %prec VAR
410a0ff2 582 { $$ = write_var_or_type (pstate, $1, $2); }
52ce6436
PH
583 | NAME TICK_ACCESS
584 {
410a0ff2 585 $$ = write_var_or_type (pstate, NULL, $1);
52ce6436 586 if ($$ == NULL)
410a0ff2 587 write_exp_elt_opcode (pstate, UNOP_ADDR);
52ce6436
PH
588 else
589 $$ = lookup_pointer_type ($$);
590 }
591 | block NAME TICK_ACCESS
592 {
410a0ff2 593 $$ = write_var_or_type (pstate, $1, $2);
52ce6436 594 if ($$ == NULL)
410a0ff2 595 write_exp_elt_opcode (pstate, UNOP_ADDR);
52ce6436
PH
596 else
597 $$ = lookup_pointer_type ($$);
598 }
14f9c5c9
AS
599 ;
600
52ce6436
PH
601/* GDB extension */
602block : NAME COLONCOLON
603 { $$ = block_lookup (NULL, $1.ptr); }
604 | block NAME COLONCOLON
605 { $$ = block_lookup ($1, $2.ptr); }
606 ;
14f9c5c9 607
52ce6436
PH
608aggregate :
609 '(' aggregate_component_list ')'
610 {
410a0ff2
SDJ
611 write_exp_elt_opcode (pstate, OP_AGGREGATE);
612 write_exp_elt_longcst (pstate, $2);
613 write_exp_elt_opcode (pstate, OP_AGGREGATE);
52ce6436 614 }
14f9c5c9
AS
615 ;
616
52ce6436
PH
617aggregate_component_list :
618 component_groups { $$ = $1; }
619 | positional_list exp
410a0ff2
SDJ
620 { write_exp_elt_opcode (pstate, OP_POSITIONAL);
621 write_exp_elt_longcst (pstate, $1);
622 write_exp_elt_opcode (pstate, OP_POSITIONAL);
52ce6436
PH
623 $$ = $1 + 1;
624 }
625 | positional_list component_groups
626 { $$ = $1 + $2; }
627 ;
14f9c5c9 628
52ce6436
PH
629positional_list :
630 exp ','
410a0ff2
SDJ
631 { write_exp_elt_opcode (pstate, OP_POSITIONAL);
632 write_exp_elt_longcst (pstate, 0);
633 write_exp_elt_opcode (pstate, OP_POSITIONAL);
52ce6436
PH
634 $$ = 1;
635 }
636 | positional_list exp ','
410a0ff2
SDJ
637 { write_exp_elt_opcode (pstate, OP_POSITIONAL);
638 write_exp_elt_longcst (pstate, $1);
639 write_exp_elt_opcode (pstate, OP_POSITIONAL);
52ce6436
PH
640 $$ = $1 + 1;
641 }
642 ;
643
644component_groups:
645 others { $$ = 1; }
646 | component_group { $$ = 1; }
647 | component_group ',' component_groups
648 { $$ = $3 + 1; }
649 ;
650
651others : OTHERS ARROW exp
410a0ff2 652 { write_exp_elt_opcode (pstate, OP_OTHERS); }
52ce6436
PH
653 ;
654
655component_group :
656 component_associations
657 {
410a0ff2
SDJ
658 write_exp_elt_opcode (pstate, OP_CHOICES);
659 write_exp_elt_longcst (pstate, $1);
660 write_exp_elt_opcode (pstate, OP_CHOICES);
52ce6436
PH
661 }
662 ;
663
664/* We use this somewhat obscure definition in order to handle NAME => and
665 NAME | differently from exp => and exp |. ARROW and '|' have a precedence
666 above that of the reduction of NAME to var_or_type. By delaying
667 decisions until after the => or '|', we convert the ambiguity to a
668 resolved shift/reduce conflict. */
669component_associations :
670 NAME ARROW
410a0ff2 671 { write_name_assoc (pstate, $1); }
52ce6436
PH
672 exp { $$ = 1; }
673 | simple_exp ARROW exp
674 { $$ = 1; }
675 | simple_exp DOTDOT simple_exp ARROW
410a0ff2
SDJ
676 { write_exp_elt_opcode (pstate, OP_DISCRETE_RANGE);
677 write_exp_op_with_string (pstate, OP_NAME,
678 empty_stoken);
52ce6436
PH
679 }
680 exp { $$ = 1; }
681 | NAME '|'
410a0ff2 682 { write_name_assoc (pstate, $1); }
52ce6436
PH
683 component_associations { $$ = $4 + 1; }
684 | simple_exp '|'
685 component_associations { $$ = $3 + 1; }
686 | simple_exp DOTDOT simple_exp '|'
410a0ff2 687 { write_exp_elt_opcode (pstate, OP_DISCRETE_RANGE); }
52ce6436
PH
688 component_associations { $$ = $6 + 1; }
689 ;
14f9c5c9
AS
690
691/* Some extensions borrowed from C, for the benefit of those who find they
4c4b4cd2 692 can't get used to Ada notation in GDB. */
14f9c5c9 693
52ce6436 694primary : '*' primary %prec '.'
410a0ff2 695 { write_exp_elt_opcode (pstate, UNOP_IND); }
52ce6436 696 | '&' primary %prec '.'
410a0ff2 697 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
52ce6436 698 | primary '[' exp ']'
410a0ff2 699 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
14f9c5c9
AS
700 ;
701
702%%
703
704/* yylex defined in ada-lex.c: Reads one token, getting characters */
705/* through lexptr. */
706
707/* Remap normal flex interface names (yylex) as well as gratuitiously */
708/* global symbol names, so we can have multiple flex-generated parsers */
709/* in gdb. */
710
711/* (See note above on previous definitions for YACC.) */
712
713#define yy_create_buffer ada_yy_create_buffer
714#define yy_delete_buffer ada_yy_delete_buffer
715#define yy_init_buffer ada_yy_init_buffer
716#define yy_load_buffer_state ada_yy_load_buffer_state
717#define yy_switch_to_buffer ada_yy_switch_to_buffer
718#define yyrestart ada_yyrestart
719#define yytext ada_yytext
720#define yywrap ada_yywrap
721
4c4b4cd2
PH
722static struct obstack temp_parse_space;
723
14f9c5c9
AS
724/* The following kludge was found necessary to prevent conflicts between */
725/* defs.h and non-standard stdlib.h files. */
726#define qsort __qsort__dummy
727#include "ada-lex.c"
728
729int
410a0ff2 730ada_parse (struct parser_state *par_state)
14f9c5c9 731{
410a0ff2 732 /* Setting up the parser state. */
eae49211 733 scoped_restore pstate_restore = make_scoped_restore (&pstate);
410a0ff2
SDJ
734 gdb_assert (par_state != NULL);
735 pstate = par_state;
736
4c4b4cd2 737 lexer_init (yyin); /* (Re-)initialize lexer. */
14f9c5c9 738 type_qualifier = NULL;
4c4b4cd2
PH
739 obstack_free (&temp_parse_space, NULL);
740 obstack_init (&temp_parse_space);
741
eae49211 742 return yyparse ();
14f9c5c9
AS
743}
744
745void
a121b7c1 746yyerror (const char *msg)
14f9c5c9 747{
03ee6b2e 748 error (_("Error in expression, near `%s'."), lexptr);
14f9c5c9
AS
749}
750
14f9c5c9 751/* Emit expression to access an instance of SYM, in block BLOCK (if
286acbb5
JB
752 non-NULL). */
753
14f9c5c9 754static void
410a0ff2 755write_var_from_sym (struct parser_state *par_state,
270140bd 756 const struct block *block,
4c4b4cd2 757 struct symbol *sym)
14f9c5c9 758{
286acbb5 759 if (symbol_read_needs_frame (sym))
14f9c5c9 760 {
c3e5cd34
PH
761 if (innermost_block == 0
762 || contained_in (block, innermost_block))
14f9c5c9
AS
763 innermost_block = block;
764 }
765
410a0ff2
SDJ
766 write_exp_elt_opcode (par_state, OP_VAR_VALUE);
767 write_exp_elt_block (par_state, block);
768 write_exp_elt_sym (par_state, sym);
769 write_exp_elt_opcode (par_state, OP_VAR_VALUE);
14f9c5c9
AS
770}
771
690cc4eb 772/* Write integer or boolean constant ARG of type TYPE. */
14f9c5c9
AS
773
774static void
410a0ff2 775write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
14f9c5c9 776{
410a0ff2
SDJ
777 write_exp_elt_opcode (par_state, OP_LONG);
778 write_exp_elt_type (par_state, type);
779 write_exp_elt_longcst (par_state, arg);
780 write_exp_elt_opcode (par_state, OP_LONG);
4c4b4cd2 781}
14f9c5c9 782
52ce6436
PH
783/* Write an OPCODE, string, OPCODE sequence to the current expression. */
784static void
410a0ff2
SDJ
785write_exp_op_with_string (struct parser_state *par_state,
786 enum exp_opcode opcode, struct stoken token)
52ce6436 787{
410a0ff2
SDJ
788 write_exp_elt_opcode (par_state, opcode);
789 write_exp_string (par_state, token);
790 write_exp_elt_opcode (par_state, opcode);
52ce6436
PH
791}
792
aeb5907d
JB
793/* Emit expression corresponding to the renamed object named
794 * designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
795 * context of ORIG_LEFT_CONTEXT, to which is applied the operations
796 * encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of
797 * cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it
798 * defaults to the currently selected block. ORIG_SYMBOL is the
799 * symbol that originally encoded the renaming. It is needed only
800 * because its prefix also qualifies any index variables used to index
801 * or slice an array. It should not be necessary once we go to the
802 * new encoding entirely (FIXME pnh 7/20/2007). */
803
14f9c5c9 804static void
410a0ff2
SDJ
805write_object_renaming (struct parser_state *par_state,
806 const struct block *orig_left_context,
aeb5907d
JB
807 const char *renamed_entity, int renamed_entity_len,
808 const char *renaming_expr, int max_depth)
14f9c5c9 809{
4c4b4cd2 810 char *name;
14f9c5c9 811 enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
d12307c1 812 struct block_symbol sym_info;
14f9c5c9 813
4c4b4cd2 814 if (max_depth <= 0)
e1d5a0d2 815 error (_("Could not find renamed symbol"));
4c4b4cd2 816
14f9c5c9
AS
817 if (orig_left_context == NULL)
818 orig_left_context = get_selected_block (NULL);
819
224c3ddb
SM
820 name = (char *) obstack_copy0 (&temp_parse_space, renamed_entity,
821 renamed_entity_len);
4e5c77fe 822 ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
d12307c1 823 if (sym_info.symbol == NULL)
e1d5a0d2 824 error (_("Could not find renamed variable: %s"), ada_decode (name));
d12307c1 825 else if (SYMBOL_CLASS (sym_info.symbol) == LOC_TYPEDEF)
aeb5907d
JB
826 /* We have a renaming of an old-style renaming symbol. Don't
827 trust the block information. */
4e5c77fe 828 sym_info.block = orig_left_context;
aeb5907d
JB
829
830 {
831 const char *inner_renamed_entity;
832 int inner_renamed_entity_len;
833 const char *inner_renaming_expr;
834
d12307c1 835 switch (ada_parse_renaming (sym_info.symbol, &inner_renamed_entity,
aeb5907d
JB
836 &inner_renamed_entity_len,
837 &inner_renaming_expr))
838 {
839 case ADA_NOT_RENAMING:
286acbb5 840 write_var_from_sym (par_state, sym_info.block, sym_info.symbol);
aeb5907d
JB
841 break;
842 case ADA_OBJECT_RENAMING:
410a0ff2 843 write_object_renaming (par_state, sym_info.block,
aeb5907d
JB
844 inner_renamed_entity, inner_renamed_entity_len,
845 inner_renaming_expr, max_depth - 1);
846 break;
847 default:
848 goto BadEncoding;
849 }
850 }
14f9c5c9 851
14f9c5c9 852 slice_state = SIMPLE_INDEX;
aeb5907d 853 while (*renaming_expr == 'X')
14f9c5c9 854 {
aeb5907d 855 renaming_expr += 1;
14f9c5c9 856
aeb5907d 857 switch (*renaming_expr) {
4c4b4cd2 858 case 'A':
aeb5907d 859 renaming_expr += 1;
410a0ff2 860 write_exp_elt_opcode (par_state, UNOP_IND);
4c4b4cd2 861 break;
14f9c5c9
AS
862 case 'L':
863 slice_state = LOWER_BOUND;
8ab1f94d 864 /* FALLTHROUGH */
14f9c5c9 865 case 'S':
aeb5907d
JB
866 renaming_expr += 1;
867 if (isdigit (*renaming_expr))
14f9c5c9 868 {
4c4b4cd2 869 char *next;
aeb5907d
JB
870 long val = strtol (renaming_expr, &next, 10);
871 if (next == renaming_expr)
14f9c5c9 872 goto BadEncoding;
aeb5907d 873 renaming_expr = next;
410a0ff2
SDJ
874 write_exp_elt_opcode (par_state, OP_LONG);
875 write_exp_elt_type (par_state, type_int (par_state));
876 write_exp_elt_longcst (par_state, (LONGEST) val);
877 write_exp_elt_opcode (par_state, OP_LONG);
4c4b4cd2 878 }
14f9c5c9
AS
879 else
880 {
4c4b4cd2
PH
881 const char *end;
882 char *index_name;
d12307c1 883 struct block_symbol index_sym_info;
14f9c5c9 884
aeb5907d 885 end = strchr (renaming_expr, 'X');
4c4b4cd2 886 if (end == NULL)
aeb5907d
JB
887 end = renaming_expr + strlen (renaming_expr);
888
224c3ddb
SM
889 index_name
890 = (char *) obstack_copy0 (&temp_parse_space, renaming_expr,
891 end - renaming_expr);
aeb5907d
JB
892 renaming_expr = end;
893
4e5c77fe
JB
894 ada_lookup_encoded_symbol (index_name, NULL, VAR_DOMAIN,
895 &index_sym_info);
d12307c1 896 if (index_sym_info.symbol == NULL)
e1d5a0d2 897 error (_("Could not find %s"), index_name);
d12307c1 898 else if (SYMBOL_CLASS (index_sym_info.symbol) == LOC_TYPEDEF)
aeb5907d 899 /* Index is an old-style renaming symbol. */
4e5c77fe 900 index_sym_info.block = orig_left_context;
286acbb5 901 write_var_from_sym (par_state, index_sym_info.block,
d12307c1 902 index_sym_info.symbol);
14f9c5c9
AS
903 }
904 if (slice_state == SIMPLE_INDEX)
4c4b4cd2 905 {
410a0ff2
SDJ
906 write_exp_elt_opcode (par_state, OP_FUNCALL);
907 write_exp_elt_longcst (par_state, (LONGEST) 1);
908 write_exp_elt_opcode (par_state, OP_FUNCALL);
14f9c5c9
AS
909 }
910 else if (slice_state == LOWER_BOUND)
911 slice_state = UPPER_BOUND;
912 else if (slice_state == UPPER_BOUND)
913 {
410a0ff2 914 write_exp_elt_opcode (par_state, TERNOP_SLICE);
14f9c5c9
AS
915 slice_state = SIMPLE_INDEX;
916 }
917 break;
918
919 case 'R':
920 {
921 struct stoken field_name;
4c4b4cd2 922 const char *end;
d7561cbb
KS
923 char *buf;
924
aeb5907d 925 renaming_expr += 1;
4c4b4cd2 926
14f9c5c9
AS
927 if (slice_state != SIMPLE_INDEX)
928 goto BadEncoding;
aeb5907d 929 end = strchr (renaming_expr, 'X');
4c4b4cd2 930 if (end == NULL)
aeb5907d
JB
931 end = renaming_expr + strlen (renaming_expr);
932 field_name.length = end - renaming_expr;
224c3ddb 933 buf = (char *) malloc (end - renaming_expr + 1);
d7561cbb
KS
934 field_name.ptr = buf;
935 strncpy (buf, renaming_expr, end - renaming_expr);
936 buf[end - renaming_expr] = '\000';
aeb5907d 937 renaming_expr = end;
410a0ff2 938 write_exp_op_with_string (par_state, STRUCTOP_STRUCT, field_name);
14f9c5c9
AS
939 break;
940 }
4c4b4cd2 941
14f9c5c9
AS
942 default:
943 goto BadEncoding;
944 }
945 }
946 if (slice_state == SIMPLE_INDEX)
947 return;
948
949 BadEncoding:
aeb5907d 950 error (_("Internal error in encoding of renaming declaration"));
14f9c5c9
AS
951}
952
3977b71f
TT
953static const struct block*
954block_lookup (const struct block *context, const char *raw_name)
52ce6436 955{
d7561cbb 956 const char *name;
d12307c1 957 struct block_symbol *syms;
52ce6436
PH
958 int nsyms;
959 struct symtab *symtab;
ec6a20c2
JB
960 struct cleanup *old_chain;
961 const struct block *result = NULL;
52ce6436
PH
962
963 if (raw_name[0] == '\'')
964 {
965 raw_name += 1;
966 name = raw_name;
967 }
968 else
969 name = ada_encode (raw_name);
970
4eeaa230 971 nsyms = ada_lookup_symbol_list (name, context, VAR_DOMAIN, &syms);
ec6a20c2
JB
972 old_chain = make_cleanup (xfree, syms);
973
f8bf5763 974 if (context == NULL
d12307c1 975 && (nsyms == 0 || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK))
52ce6436
PH
976 symtab = lookup_symtab (name);
977 else
978 symtab = NULL;
979
980 if (symtab != NULL)
ec6a20c2 981 result = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
d12307c1 982 else if (nsyms == 0 || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK)
52ce6436
PH
983 {
984 if (context == NULL)
e1d5a0d2 985 error (_("No file or function \"%s\"."), raw_name);
52ce6436 986 else
e1d5a0d2 987 error (_("No function \"%s\" in specified context."), raw_name);
52ce6436
PH
988 }
989 else
990 {
991 if (nsyms > 1)
e1d5a0d2 992 warning (_("Function name \"%s\" ambiguous here"), raw_name);
ec6a20c2 993 result = SYMBOL_BLOCK_VALUE (syms[0].symbol);
52ce6436 994 }
ec6a20c2
JB
995
996 do_cleanups (old_chain);
997 return result;
52ce6436
PH
998}
999
1000static struct symbol*
d12307c1 1001select_possible_type_sym (struct block_symbol *syms, int nsyms)
52ce6436
PH
1002{
1003 int i;
1004 int preferred_index;
1005 struct type *preferred_type;
1006
1007 preferred_index = -1; preferred_type = NULL;
1008 for (i = 0; i < nsyms; i += 1)
d12307c1 1009 switch (SYMBOL_CLASS (syms[i].symbol))
52ce6436
PH
1010 {
1011 case LOC_TYPEDEF:
d12307c1 1012 if (ada_prefer_type (SYMBOL_TYPE (syms[i].symbol), preferred_type))
52ce6436
PH
1013 {
1014 preferred_index = i;
d12307c1 1015 preferred_type = SYMBOL_TYPE (syms[i].symbol);
52ce6436
PH
1016 }
1017 break;
1018 case LOC_REGISTER:
1019 case LOC_ARG:
1020 case LOC_REF_ARG:
52ce6436
PH
1021 case LOC_REGPARM_ADDR:
1022 case LOC_LOCAL:
52ce6436 1023 case LOC_COMPUTED:
52ce6436
PH
1024 return NULL;
1025 default:
1026 break;
1027 }
1028 if (preferred_type == NULL)
1029 return NULL;
d12307c1 1030 return syms[preferred_index].symbol;
52ce6436
PH
1031}
1032
1033static struct type*
410a0ff2 1034find_primitive_type (struct parser_state *par_state, char *name)
52ce6436
PH
1035{
1036 struct type *type;
46b0da17
DE
1037 type = language_lookup_primitive_type (parse_language (par_state),
1038 parse_gdbarch (par_state),
1039 name);
52ce6436 1040 if (type == NULL && strcmp ("system__address", name) == 0)
410a0ff2 1041 type = type_system_address (par_state);
52ce6436
PH
1042
1043 if (type != NULL)
1044 {
1045 /* Check to see if we have a regular definition of this
1046 type that just didn't happen to have been read yet. */
52ce6436
PH
1047 struct symbol *sym;
1048 char *expanded_name =
1049 (char *) alloca (strlen (name) + sizeof ("standard__"));
1050 strcpy (expanded_name, "standard__");
1051 strcat (expanded_name, name);
d12307c1 1052 sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN, NULL).symbol;
52ce6436
PH
1053 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1054 type = SYMBOL_TYPE (sym);
1055 }
1056
1057 return type;
1058}
1059
1060static int
1061chop_selector (char *name, int end)
1062{
1063 int i;
1064 for (i = end - 1; i > 0; i -= 1)
1065 if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
1066 return i;
1067 return -1;
1068}
1069
d3353bbd
JB
1070/* If NAME is a string beginning with a separator (either '__', or
1071 '.'), chop this separator and return the result; else, return
1072 NAME. */
1073
1074static char *
1075chop_separator (char *name)
1076{
1077 if (*name == '.')
1078 return name + 1;
1079
1080 if (name[0] == '_' && name[1] == '_')
1081 return name + 2;
1082
1083 return name;
1084}
1085
52ce6436
PH
1086/* Given that SELS is a string of the form (<sep><identifier>)*, where
1087 <sep> is '__' or '.', write the indicated sequence of
1088 STRUCTOP_STRUCT expression operators. */
1089static void
410a0ff2 1090write_selectors (struct parser_state *par_state, char *sels)
52ce6436
PH
1091{
1092 while (*sels != '\0')
1093 {
1094 struct stoken field_name;
d3353bbd
JB
1095 char *p = chop_separator (sels);
1096 sels = p;
52ce6436
PH
1097 while (*sels != '\0' && *sels != '.'
1098 && (sels[0] != '_' || sels[1] != '_'))
1099 sels += 1;
1100 field_name.length = sels - p;
1101 field_name.ptr = p;
410a0ff2 1102 write_exp_op_with_string (par_state, STRUCTOP_STRUCT, field_name);
52ce6436
PH
1103 }
1104}
1105
1106/* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
1107 NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes
1108 a temporary symbol that is valid until the next call to ada_parse.
1109 */
1110static void
410a0ff2
SDJ
1111write_ambiguous_var (struct parser_state *par_state,
1112 const struct block *block, char *name, int len)
52ce6436 1113{
8d749320
SM
1114 struct symbol *sym = XOBNEW (&temp_parse_space, struct symbol);
1115
52ce6436
PH
1116 memset (sym, 0, sizeof (struct symbol));
1117 SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
224c3ddb
SM
1118 SYMBOL_LINKAGE_NAME (sym)
1119 = (const char *) obstack_copy0 (&temp_parse_space, name, len);
52ce6436
PH
1120 SYMBOL_LANGUAGE (sym) = language_ada;
1121
410a0ff2
SDJ
1122 write_exp_elt_opcode (par_state, OP_VAR_VALUE);
1123 write_exp_elt_block (par_state, block);
1124 write_exp_elt_sym (par_state, sym);
1125 write_exp_elt_opcode (par_state, OP_VAR_VALUE);
52ce6436
PH
1126}
1127
d3353bbd
JB
1128/* A convenient wrapper around ada_get_field_index that takes
1129 a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
1130 of a NUL-terminated field name. */
1131
1132static int
1133ada_nget_field_index (const struct type *type, const char *field_name0,
1134 int field_name_len, int maybe_missing)
1135{
224c3ddb 1136 char *field_name = (char *) alloca ((field_name_len + 1) * sizeof (char));
d3353bbd
JB
1137
1138 strncpy (field_name, field_name0, field_name_len);
1139 field_name[field_name_len] = '\0';
1140 return ada_get_field_index (type, field_name, maybe_missing);
1141}
1142
1143/* If encoded_field_name is the name of a field inside symbol SYM,
1144 then return the type of that field. Otherwise, return NULL.
1145
1146 This function is actually recursive, so if ENCODED_FIELD_NAME
1147 doesn't match one of the fields of our symbol, then try to see
1148 if ENCODED_FIELD_NAME could not be a succession of field names
1149 (in other words, the user entered an expression of the form
1150 TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
1151 each field name sequentially to obtain the desired field type.
1152 In case of failure, we return NULL. */
1153
1154static struct type *
1155get_symbol_field_type (struct symbol *sym, char *encoded_field_name)
1156{
1157 char *field_name = encoded_field_name;
1158 char *subfield_name;
1159 struct type *type = SYMBOL_TYPE (sym);
1160 int fieldno;
1161
1162 if (type == NULL || field_name == NULL)
1163 return NULL;
6cdd57f4 1164 type = check_typedef (type);
d3353bbd
JB
1165
1166 while (field_name[0] != '\0')
1167 {
1168 field_name = chop_separator (field_name);
1169
1170 fieldno = ada_get_field_index (type, field_name, 1);
1171 if (fieldno >= 0)
1172 return TYPE_FIELD_TYPE (type, fieldno);
1173
1174 subfield_name = field_name;
1175 while (*subfield_name != '\0' && *subfield_name != '.'
1176 && (subfield_name[0] != '_' || subfield_name[1] != '_'))
1177 subfield_name += 1;
1178
1179 if (subfield_name[0] == '\0')
1180 return NULL;
1181
1182 fieldno = ada_nget_field_index (type, field_name,
1183 subfield_name - field_name, 1);
1184 if (fieldno < 0)
1185 return NULL;
1186
1187 type = TYPE_FIELD_TYPE (type, fieldno);
1188 field_name = subfield_name;
1189 }
1190
1191 return NULL;
1192}
52ce6436
PH
1193
1194/* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
1195 expression_block_context if NULL). If it denotes a type, return
1196 that type. Otherwise, write expression code to evaluate it as an
1197 object and return NULL. In this second case, NAME0 will, in general,
1198 have the form <name>(.<selector_name>)*, where <name> is an object
1199 or renaming encoded in the debugging data. Calls error if no
1200 prefix <name> matches a name in the debugging data (i.e., matches
1201 either a complete name or, as a wild-card match, the final
1202 identifier). */
1203
1204static struct type*
410a0ff2
SDJ
1205write_var_or_type (struct parser_state *par_state,
1206 const struct block *block, struct stoken name0)
52ce6436
PH
1207{
1208 int depth;
1209 char *encoded_name;
1210 int name_len;
ec6a20c2 1211 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
52ce6436
PH
1212
1213 if (block == NULL)
1214 block = expression_context_block;
1215
1216 encoded_name = ada_encode (name0.ptr);
1217 name_len = strlen (encoded_name);
224c3ddb
SM
1218 encoded_name
1219 = (char *) obstack_copy0 (&temp_parse_space, encoded_name, name_len);
52ce6436
PH
1220 for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
1221 {
1222 int tail_index;
1223
1224 tail_index = name_len;
1225 while (tail_index > 0)
1226 {
1227 int nsyms;
d12307c1 1228 struct block_symbol *syms;
52ce6436 1229 struct symbol *type_sym;
aeb5907d
JB
1230 struct symbol *renaming_sym;
1231 const char* renaming;
1232 int renaming_len;
1233 const char* renaming_expr;
52ce6436
PH
1234 int terminator = encoded_name[tail_index];
1235
1236 encoded_name[tail_index] = '\0';
1237 nsyms = ada_lookup_symbol_list (encoded_name, block,
4eeaa230 1238 VAR_DOMAIN, &syms);
ec6a20c2 1239 make_cleanup (xfree, syms);
52ce6436
PH
1240 encoded_name[tail_index] = terminator;
1241
1242 /* A single symbol may rename a package or object. */
1243
aeb5907d
JB
1244 /* This should go away when we move entirely to new version.
1245 FIXME pnh 7/20/2007. */
1246 if (nsyms == 1)
52ce6436 1247 {
e5e61bd7 1248 struct symbol *ren_sym =
d12307c1 1249 ada_find_renaming_symbol (syms[0].symbol, syms[0].block);
52ce6436 1250
e5e61bd7 1251 if (ren_sym != NULL)
d12307c1 1252 syms[0].symbol = ren_sym;
52ce6436
PH
1253 }
1254
1255 type_sym = select_possible_type_sym (syms, nsyms);
aeb5907d
JB
1256
1257 if (type_sym != NULL)
1258 renaming_sym = type_sym;
1259 else if (nsyms == 1)
d12307c1 1260 renaming_sym = syms[0].symbol;
aeb5907d
JB
1261 else
1262 renaming_sym = NULL;
1263
1264 switch (ada_parse_renaming (renaming_sym, &renaming,
1265 &renaming_len, &renaming_expr))
1266 {
1267 case ADA_NOT_RENAMING:
1268 break;
1269 case ADA_PACKAGE_RENAMING:
1270 case ADA_EXCEPTION_RENAMING:
1271 case ADA_SUBPROGRAM_RENAMING:
1272 {
224c3ddb 1273 int alloc_len = renaming_len + name_len - tail_index + 1;
aeb5907d 1274 char *new_name
224c3ddb 1275 = (char *) obstack_alloc (&temp_parse_space, alloc_len);
aeb5907d
JB
1276 strncpy (new_name, renaming, renaming_len);
1277 strcpy (new_name + renaming_len, encoded_name + tail_index);
1278 encoded_name = new_name;
1279 name_len = renaming_len + name_len - tail_index;
1280 goto TryAfterRenaming;
1281 }
1282 case ADA_OBJECT_RENAMING:
410a0ff2 1283 write_object_renaming (par_state, block, renaming, renaming_len,
aeb5907d 1284 renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
410a0ff2 1285 write_selectors (par_state, encoded_name + tail_index);
ec6a20c2 1286 do_cleanups (old_chain);
aeb5907d
JB
1287 return NULL;
1288 default:
1289 internal_error (__FILE__, __LINE__,
1290 _("impossible value from ada_parse_renaming"));
1291 }
1292
52ce6436
PH
1293 if (type_sym != NULL)
1294 {
d3353bbd
JB
1295 struct type *field_type;
1296
1297 if (tail_index == name_len)
ec6a20c2
JB
1298 {
1299 do_cleanups (old_chain);
1300 return SYMBOL_TYPE (type_sym);
1301 }
d3353bbd
JB
1302
1303 /* We have some extraneous characters after the type name.
1304 If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
1305 then try to get the type of FIELDN. */
1306 field_type
1307 = get_symbol_field_type (type_sym, encoded_name + tail_index);
1308 if (field_type != NULL)
ec6a20c2
JB
1309 {
1310 do_cleanups (old_chain);
1311 return field_type;
1312 }
52ce6436 1313 else
d3353bbd
JB
1314 error (_("Invalid attempt to select from type: \"%s\"."),
1315 name0.ptr);
52ce6436
PH
1316 }
1317 else if (tail_index == name_len && nsyms == 0)
1318 {
410a0ff2
SDJ
1319 struct type *type = find_primitive_type (par_state,
1320 encoded_name);
52ce6436
PH
1321
1322 if (type != NULL)
ec6a20c2
JB
1323 {
1324 do_cleanups (old_chain);
1325 return type;
1326 }
52ce6436
PH
1327 }
1328
1329 if (nsyms == 1)
1330 {
286acbb5 1331 write_var_from_sym (par_state, syms[0].block, syms[0].symbol);
410a0ff2 1332 write_selectors (par_state, encoded_name + tail_index);
ec6a20c2 1333 do_cleanups (old_chain);
52ce6436
PH
1334 return NULL;
1335 }
1336 else if (nsyms == 0)
1337 {
7c7b6655 1338 struct bound_minimal_symbol msym
52ce6436 1339 = ada_lookup_simple_minsym (encoded_name);
7c7b6655 1340 if (msym.minsym != NULL)
52ce6436 1341 {
410a0ff2 1342 write_exp_msymbol (par_state, msym);
52ce6436 1343 /* Maybe cause error here rather than later? FIXME? */
410a0ff2 1344 write_selectors (par_state, encoded_name + tail_index);
ec6a20c2 1345 do_cleanups (old_chain);
52ce6436
PH
1346 return NULL;
1347 }
1348
1349 if (tail_index == name_len
1350 && strncmp (encoded_name, "standard__",
1351 sizeof ("standard__") - 1) == 0)
e1d5a0d2 1352 error (_("No definition of \"%s\" found."), name0.ptr);
52ce6436
PH
1353
1354 tail_index = chop_selector (encoded_name, tail_index);
1355 }
1356 else
1357 {
410a0ff2
SDJ
1358 write_ambiguous_var (par_state, block, encoded_name,
1359 tail_index);
1360 write_selectors (par_state, encoded_name + tail_index);
ec6a20c2 1361 do_cleanups (old_chain);
52ce6436
PH
1362 return NULL;
1363 }
1364 }
1365
1366 if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
e1d5a0d2 1367 error (_("No symbol table is loaded. Use the \"file\" command."));
52ce6436 1368 if (block == expression_context_block)
e1d5a0d2 1369 error (_("No definition of \"%s\" in current context."), name0.ptr);
52ce6436 1370 else
e1d5a0d2 1371 error (_("No definition of \"%s\" in specified context."), name0.ptr);
52ce6436
PH
1372
1373 TryAfterRenaming: ;
1374 }
1375
e1d5a0d2 1376 error (_("Could not find renamed symbol \"%s\""), name0.ptr);
52ce6436
PH
1377
1378}
1379
1380/* Write a left side of a component association (e.g., NAME in NAME =>
1381 exp). If NAME has the form of a selected component, write it as an
1382 ordinary expression. If it is a simple variable that unambiguously
1383 corresponds to exactly one symbol that does not denote a type or an
1384 object renaming, also write it normally as an OP_VAR_VALUE.
1385 Otherwise, write it as an OP_NAME.
1386
1387 Unfortunately, we don't know at this point whether NAME is supposed
1388 to denote a record component name or the value of an array index.
1389 Therefore, it is not appropriate to disambiguate an ambiguous name
1390 as we normally would, nor to replace a renaming with its referent.
1391 As a result, in the (one hopes) rare case that one writes an
1392 aggregate such as (R => 42) where R renames an object or is an
1393 ambiguous name, one must write instead ((R) => 42). */
1394
1395static void
410a0ff2 1396write_name_assoc (struct parser_state *par_state, struct stoken name)
52ce6436
PH
1397{
1398 if (strchr (name.ptr, '.') == NULL)
1399 {
d12307c1 1400 struct block_symbol *syms;
52ce6436 1401 int nsyms = ada_lookup_symbol_list (name.ptr, expression_context_block,
4eeaa230 1402 VAR_DOMAIN, &syms);
ec6a20c2 1403 struct cleanup *old_chain = make_cleanup (xfree, syms);
d12307c1
PMR
1404
1405 if (nsyms != 1 || SYMBOL_CLASS (syms[0].symbol) == LOC_TYPEDEF)
410a0ff2 1406 write_exp_op_with_string (par_state, OP_NAME, name);
52ce6436 1407 else
286acbb5 1408 write_var_from_sym (par_state, syms[0].block, syms[0].symbol);
ec6a20c2
JB
1409
1410 do_cleanups (old_chain);
52ce6436
PH
1411 }
1412 else
410a0ff2 1413 if (write_var_or_type (par_state, NULL, name) != NULL)
e1d5a0d2 1414 error (_("Invalid use of type."));
52ce6436
PH
1415}
1416
14f9c5c9
AS
1417/* Convert the character literal whose ASCII value would be VAL to the
1418 appropriate value of type TYPE, if there is a translation.
4c4b4cd2
PH
1419 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
1420 the literal 'A' (VAL == 65), returns 0. */
52ce6436 1421
14f9c5c9 1422static LONGEST
4c4b4cd2 1423convert_char_literal (struct type *type, LONGEST val)
14f9c5c9
AS
1424{
1425 char name[7];
1426 int f;
1427
18920c42 1428 if (type == NULL)
14f9c5c9 1429 return val;
18920c42
JB
1430 type = check_typedef (type);
1431 if (TYPE_CODE (type) != TYPE_CODE_ENUM)
1432 return val;
1433
88c15c34 1434 xsnprintf (name, sizeof (name), "QU%02x", (int) val);
4c4b4cd2 1435 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
14f9c5c9 1436 {
4c4b4cd2 1437 if (strcmp (name, TYPE_FIELD_NAME (type, f)) == 0)
14e75d8e 1438 return TYPE_FIELD_ENUMVAL (type, f);
14f9c5c9
AS
1439 }
1440 return val;
1441}
4c4b4cd2 1442
72d5681a 1443static struct type *
410a0ff2 1444type_int (struct parser_state *par_state)
72d5681a 1445{
410a0ff2 1446 return parse_type (par_state)->builtin_int;
72d5681a
PH
1447}
1448
1449static struct type *
410a0ff2 1450type_long (struct parser_state *par_state)
72d5681a 1451{
410a0ff2 1452 return parse_type (par_state)->builtin_long;
72d5681a
PH
1453}
1454
1455static struct type *
410a0ff2 1456type_long_long (struct parser_state *par_state)
72d5681a 1457{
410a0ff2 1458 return parse_type (par_state)->builtin_long_long;
72d5681a
PH
1459}
1460
72d5681a 1461static struct type *
410a0ff2 1462type_long_double (struct parser_state *par_state)
72d5681a 1463{
410a0ff2 1464 return parse_type (par_state)->builtin_long_double;
72d5681a
PH
1465}
1466
1467static struct type *
410a0ff2 1468type_char (struct parser_state *par_state)
72d5681a 1469{
410a0ff2
SDJ
1470 return language_string_char_type (parse_language (par_state),
1471 parse_gdbarch (par_state));
72d5681a
PH
1472}
1473
690cc4eb 1474static struct type *
410a0ff2 1475type_boolean (struct parser_state *par_state)
690cc4eb 1476{
410a0ff2 1477 return parse_type (par_state)->builtin_bool;
690cc4eb
PH
1478}
1479
72d5681a 1480static struct type *
410a0ff2 1481type_system_address (struct parser_state *par_state)
72d5681a
PH
1482{
1483 struct type *type
46b0da17
DE
1484 = language_lookup_primitive_type (parse_language (par_state),
1485 parse_gdbarch (par_state),
1486 "system__address");
410a0ff2 1487 return type != NULL ? type : parse_type (par_state)->builtin_data_ptr;
72d5681a
PH
1488}
1489
4c4b4cd2
PH
1490void
1491_initialize_ada_exp (void)
1492{
1493 obstack_init (&temp_parse_space);
1494}
This page took 1.332739 seconds and 4 git commands to generate.