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