2004-07-17 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / ada-exp.y
CommitLineData
14f9c5c9 1/* YACC parser for Ada expressions, for GDB.
4c4b4cd2
PH
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994, 1997, 2000, 2003,
3 2004 Free Software Foundation, Inc.
14f9c5c9
AS
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/* Parse an Ada expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
29
30 malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
4c4b4cd2 37
14f9c5c9
AS
38%{
39
40#include "defs.h"
19c1ef65 41#include "gdb_string.h"
14f9c5c9
AS
42#include <ctype.h>
43#include "expression.h"
44#include "value.h"
45#include "parser-defs.h"
46#include "language.h"
47#include "ada-lang.h"
48#include "bfd.h" /* Required by objfiles.h. */
49#include "symfile.h" /* Required by objfiles.h. */
50#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
51#include "frame.h"
fe898f56 52#include "block.h"
14f9c5c9
AS
53
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 */
14f9c5c9
AS
96
97#ifndef YYDEBUG
f461f5cf 98#define YYDEBUG 1 /* Default to yydebug support */
14f9c5c9
AS
99#endif
100
f461f5cf
PM
101#define YYFPRINTF parser_fprintf
102
14f9c5c9 103struct name_info {
4c4b4cd2
PH
104 struct symbol *sym;
105 struct minimal_symbol *msym;
106 struct block *block;
14f9c5c9
AS
107 struct stoken stoken;
108};
109
110/* If expression is in the context of TYPE'(...), then TYPE, else
4c4b4cd2
PH
111 * NULL. */
112static struct type *type_qualifier;
14f9c5c9
AS
113
114int yyparse (void);
115
116static int yylex (void);
117
118void yyerror (char *);
119
120static struct stoken string_to_operator (struct stoken);
121
4c4b4cd2 122static void write_int (LONGEST, struct type *);
14f9c5c9 123
4c4b4cd2 124static void write_object_renaming (struct block *, struct symbol *, int);
14f9c5c9 125
4c4b4cd2 126static void write_var_from_name (struct block *, struct name_info);
14f9c5c9 127
19c1ef65 128static LONGEST convert_char_literal (struct type *, LONGEST);
4c4b4cd2 129%}
14f9c5c9
AS
130
131%union
132 {
133 LONGEST lval;
134 struct {
135 LONGEST val;
136 struct type *type;
137 } typed_val;
138 struct {
139 DOUBLEST dval;
140 struct type *type;
141 } typed_val_float;
142 struct type *tval;
143 struct stoken sval;
144 struct name_info ssym;
145 int voidval;
146 struct block *bval;
147 struct internalvar *ivar;
148
149 }
150
151%type <voidval> exp exp1 simple_exp start variable
152%type <tval> type
153
154%token <typed_val> INT NULL_PTR CHARLIT
155%token <typed_val_float> FLOAT
156%token <tval> TYPENAME
157%token <bval> BLOCKNAME
158
159/* Both NAME and TYPENAME tokens represent symbols in the input,
160 and both convey their data as strings.
161 But a TYPENAME is a string that happens to be defined as a typedef
162 or builtin type name (such as int or char)
163 and a NAME is any other symbol.
164 Contexts where this distinction is not important can use the
165 nonterminal "name", which matches either NAME or TYPENAME. */
166
4c4b4cd2 167%token <sval> STRING
14f9c5c9 168%token <ssym> NAME DOT_ID OBJECT_RENAMING
4c4b4cd2 169%type <bval> block
14f9c5c9
AS
170%type <lval> arglist tick_arglist
171
172%type <tval> save_qualifier
173
174%token DOT_ALL
175
176/* Special type cases, put in to allow the parser to distinguish different
177 legal basetypes. */
4c4b4cd2 178%token <sval> SPECIAL_VARIABLE
14f9c5c9
AS
179
180%nonassoc ASSIGN
181%left _AND_ OR XOR THEN ELSE
182%left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
183%left '@'
184%left '+' '-' '&'
185%left UNARY
186%left '*' '/' MOD REM
187%right STARSTAR ABS NOT
4c4b4cd2
PH
188 /* The following are right-associative only so that reductions at this
189 precedence have lower precedence than '.' and '('. The syntax still
190 forces a.b.c, e.g., to be LEFT-associated. */
14f9c5c9
AS
191%right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
192%right TICK_MAX TICK_MIN TICK_MODULUS
193%right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
194%right '.' '(' '[' DOT_ID DOT_ALL
195
196%token ARROW NEW
197
198\f
199%%
200
201start : exp1
202 | type { write_exp_elt_opcode (OP_TYPE);
203 write_exp_elt_type ($1);
204 write_exp_elt_opcode (OP_TYPE); }
205 ;
206
207/* Expressions, including the sequencing operator. */
208exp1 : exp
209 | exp1 ';' exp
210 { write_exp_elt_opcode (BINOP_COMMA); }
211 ;
212
213/* Expressions, not including the sequencing operator. */
214simple_exp : simple_exp DOT_ALL
215 { write_exp_elt_opcode (UNOP_IND); }
216 ;
217
218simple_exp : simple_exp DOT_ID
219 { write_exp_elt_opcode (STRUCTOP_STRUCT);
220 write_exp_string ($2.stoken);
4c4b4cd2 221 write_exp_elt_opcode (STRUCTOP_STRUCT);
14f9c5c9
AS
222 }
223 ;
224
225simple_exp : simple_exp '(' arglist ')'
226 {
227 write_exp_elt_opcode (OP_FUNCALL);
228 write_exp_elt_longcst ($3);
229 write_exp_elt_opcode (OP_FUNCALL);
230 }
231 ;
232
233simple_exp : type '(' exp ')'
234 {
235 write_exp_elt_opcode (UNOP_CAST);
236 write_exp_elt_type ($1);
4c4b4cd2 237 write_exp_elt_opcode (UNOP_CAST);
14f9c5c9
AS
238 }
239 ;
240
241simple_exp : type '\'' save_qualifier { type_qualifier = $1; } '(' exp ')'
242 {
4c4b4cd2 243 write_exp_elt_opcode (UNOP_QUAL);
14f9c5c9 244 write_exp_elt_type ($1);
4c4b4cd2 245 write_exp_elt_opcode (UNOP_QUAL);
14f9c5c9
AS
246 type_qualifier = $3;
247 }
248 ;
249
250save_qualifier : { $$ = type_qualifier; }
525d6a61 251 ;
14f9c5c9
AS
252
253simple_exp :
254 simple_exp '(' exp DOTDOT exp ')'
255 { write_exp_elt_opcode (TERNOP_SLICE); }
256 ;
257
258simple_exp : '(' exp1 ')' { }
259 ;
260
4c4b4cd2 261simple_exp : variable
14f9c5c9
AS
262 ;
263
4c4b4cd2
PH
264simple_exp: SPECIAL_VARIABLE /* Various GDB extensions */
265 { write_dollar_variable ($1); }
14f9c5c9
AS
266 ;
267
14f9c5c9
AS
268exp : simple_exp
269 ;
270
14f9c5c9
AS
271exp : exp ASSIGN exp /* Extension for convenience */
272 { write_exp_elt_opcode (BINOP_ASSIGN); }
273 ;
274
275exp : '-' exp %prec UNARY
276 { write_exp_elt_opcode (UNOP_NEG); }
277 ;
278
279exp : '+' exp %prec UNARY
280 { write_exp_elt_opcode (UNOP_PLUS); }
281 ;
282
283exp : NOT exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
285 ;
286
287exp : ABS exp %prec UNARY
288 { write_exp_elt_opcode (UNOP_ABS); }
289 ;
290
291arglist : { $$ = 0; }
292 ;
293
294arglist : exp
295 { $$ = 1; }
296 | any_name ARROW exp
297 { $$ = 1; }
298 | arglist ',' exp
299 { $$ = $1 + 1; }
300 | arglist ',' any_name ARROW exp
301 { $$ = $1 + 1; }
302 ;
303
304exp : '{' type '}' exp %prec '.'
305 /* GDB extension */
306 { write_exp_elt_opcode (UNOP_MEMVAL);
307 write_exp_elt_type ($2);
4c4b4cd2 308 write_exp_elt_opcode (UNOP_MEMVAL);
14f9c5c9
AS
309 }
310 ;
311
312/* Binary operators in order of decreasing precedence. */
313
314exp : exp STARSTAR exp
315 { write_exp_elt_opcode (BINOP_EXP); }
316 ;
317
318exp : exp '*' exp
319 { write_exp_elt_opcode (BINOP_MUL); }
320 ;
321
322exp : exp '/' exp
323 { write_exp_elt_opcode (BINOP_DIV); }
324 ;
325
326exp : exp REM exp /* May need to be fixed to give correct Ada REM */
327 { write_exp_elt_opcode (BINOP_REM); }
328 ;
329
330exp : exp MOD exp
331 { write_exp_elt_opcode (BINOP_MOD); }
332 ;
333
334exp : exp '@' exp /* GDB extension */
335 { write_exp_elt_opcode (BINOP_REPEAT); }
336 ;
337
338exp : exp '+' exp
339 { write_exp_elt_opcode (BINOP_ADD); }
340 ;
341
342exp : exp '&' exp
343 { write_exp_elt_opcode (BINOP_CONCAT); }
344 ;
345
346exp : exp '-' exp
347 { write_exp_elt_opcode (BINOP_SUB); }
348 ;
349
350exp : exp '=' exp
351 { write_exp_elt_opcode (BINOP_EQUAL); }
352 ;
353
354exp : exp NOTEQUAL exp
355 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
356 ;
357
358exp : exp LEQ exp
359 { write_exp_elt_opcode (BINOP_LEQ); }
360 ;
361
362exp : exp IN exp DOTDOT exp
4c4b4cd2 363 { write_exp_elt_opcode (TERNOP_IN_RANGE); }
14f9c5c9 364 | exp IN exp TICK_RANGE tick_arglist
4c4b4cd2 365 { write_exp_elt_opcode (BINOP_IN_BOUNDS);
14f9c5c9 366 write_exp_elt_longcst ((LONGEST) $5);
4c4b4cd2 367 write_exp_elt_opcode (BINOP_IN_BOUNDS);
14f9c5c9
AS
368 }
369 | exp IN TYPENAME %prec TICK_ACCESS
4c4b4cd2 370 { write_exp_elt_opcode (UNOP_IN_RANGE);
14f9c5c9 371 write_exp_elt_type ($3);
4c4b4cd2 372 write_exp_elt_opcode (UNOP_IN_RANGE);
14f9c5c9
AS
373 }
374 | exp NOT IN exp DOTDOT exp
4c4b4cd2
PH
375 { write_exp_elt_opcode (TERNOP_IN_RANGE);
376 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
14f9c5c9
AS
377 }
378 | exp NOT IN exp TICK_RANGE tick_arglist
4c4b4cd2 379 { write_exp_elt_opcode (BINOP_IN_BOUNDS);
14f9c5c9 380 write_exp_elt_longcst ((LONGEST) $6);
4c4b4cd2
PH
381 write_exp_elt_opcode (BINOP_IN_BOUNDS);
382 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
14f9c5c9
AS
383 }
384 | exp NOT IN TYPENAME %prec TICK_ACCESS
4c4b4cd2 385 { write_exp_elt_opcode (UNOP_IN_RANGE);
14f9c5c9 386 write_exp_elt_type ($4);
4c4b4cd2
PH
387 write_exp_elt_opcode (UNOP_IN_RANGE);
388 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
14f9c5c9
AS
389 }
390 ;
391
392exp : exp GEQ exp
393 { write_exp_elt_opcode (BINOP_GEQ); }
394 ;
395
396exp : exp '<' exp
397 { write_exp_elt_opcode (BINOP_LESS); }
398 ;
399
400exp : exp '>' exp
401 { write_exp_elt_opcode (BINOP_GTR); }
402 ;
403
4c4b4cd2 404exp : exp _AND_ exp /* Fix for Ada elementwise AND. */
14f9c5c9
AS
405 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
406 ;
407
408exp : exp _AND_ THEN exp %prec _AND_
409 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
410 ;
411
412exp : exp OR exp /* Fix for Ada elementwise OR */
413 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
414 ;
415
4c4b4cd2 416exp : exp OR ELSE exp
14f9c5c9
AS
417 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
418 ;
419
420exp : exp XOR exp /* Fix for Ada elementwise XOR */
421 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
422 ;
423
424simple_exp : simple_exp TICK_ACCESS
425 { write_exp_elt_opcode (UNOP_ADDR); }
426 | simple_exp TICK_ADDRESS
427 { write_exp_elt_opcode (UNOP_ADDR);
428 write_exp_elt_opcode (UNOP_CAST);
429 write_exp_elt_type (builtin_type_ada_system_address);
430 write_exp_elt_opcode (UNOP_CAST);
431 }
432 | simple_exp TICK_FIRST tick_arglist
4c4b4cd2
PH
433 { write_int ($3, builtin_type_int);
434 write_exp_elt_opcode (OP_ATR_FIRST); }
14f9c5c9 435 | simple_exp TICK_LAST tick_arglist
4c4b4cd2
PH
436 { write_int ($3, builtin_type_int);
437 write_exp_elt_opcode (OP_ATR_LAST); }
14f9c5c9 438 | simple_exp TICK_LENGTH tick_arglist
4c4b4cd2
PH
439 { write_int ($3, builtin_type_int);
440 write_exp_elt_opcode (OP_ATR_LENGTH); }
441 | simple_exp TICK_SIZE
442 { write_exp_elt_opcode (OP_ATR_SIZE); }
14f9c5c9 443 | simple_exp TICK_TAG
4c4b4cd2 444 { write_exp_elt_opcode (OP_ATR_TAG); }
14f9c5c9 445 | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
4c4b4cd2 446 { write_exp_elt_opcode (OP_ATR_MIN); }
14f9c5c9 447 | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
4c4b4cd2 448 { write_exp_elt_opcode (OP_ATR_MAX); }
14f9c5c9 449 | opt_type_prefix TICK_POS '(' exp ')'
4c4b4cd2 450 { write_exp_elt_opcode (OP_ATR_POS); }
14f9c5c9 451 | type_prefix TICK_FIRST tick_arglist
4c4b4cd2
PH
452 { write_int ($3, builtin_type_int);
453 write_exp_elt_opcode (OP_ATR_FIRST); }
14f9c5c9 454 | type_prefix TICK_LAST tick_arglist
4c4b4cd2
PH
455 { write_int ($3, builtin_type_int);
456 write_exp_elt_opcode (OP_ATR_LAST); }
14f9c5c9 457 | type_prefix TICK_LENGTH tick_arglist
4c4b4cd2
PH
458 { write_int ($3, builtin_type_int);
459 write_exp_elt_opcode (OP_ATR_LENGTH); }
14f9c5c9 460 | type_prefix TICK_VAL '(' exp ')'
4c4b4cd2
PH
461 { write_exp_elt_opcode (OP_ATR_VAL); }
462 | type_prefix TICK_MODULUS
463 { write_exp_elt_opcode (OP_ATR_MODULUS); }
14f9c5c9
AS
464 ;
465
466tick_arglist : %prec '('
467 { $$ = 1; }
468 | '(' INT ')'
469 { $$ = $2.val; }
470 ;
471
472type_prefix :
473 TYPENAME
474 { write_exp_elt_opcode (OP_TYPE);
475 write_exp_elt_type ($1);
476 write_exp_elt_opcode (OP_TYPE); }
477 ;
478
479opt_type_prefix :
480 type_prefix
4c4b4cd2 481 | /* EMPTY */
14f9c5c9
AS
482 { write_exp_elt_opcode (OP_TYPE);
483 write_exp_elt_type (builtin_type_void);
484 write_exp_elt_opcode (OP_TYPE); }
485 ;
4c4b4cd2 486
14f9c5c9
AS
487
488exp : INT
4c4b4cd2 489 { write_int ((LONGEST) $1.val, $1.type); }
14f9c5c9
AS
490 ;
491
492exp : CHARLIT
4c4b4cd2
PH
493 { write_int (convert_char_literal (type_qualifier, $1.val),
494 (type_qualifier == NULL)
495 ? $1.type : type_qualifier);
496 }
525d6a61 497 ;
4c4b4cd2 498
14f9c5c9
AS
499exp : FLOAT
500 { write_exp_elt_opcode (OP_DOUBLE);
501 write_exp_elt_type ($1.type);
502 write_exp_elt_dblcst ($1.dval);
4c4b4cd2 503 write_exp_elt_opcode (OP_DOUBLE);
14f9c5c9
AS
504 }
505 ;
506
507exp : NULL_PTR
4c4b4cd2 508 { write_int (0, builtin_type_int); }
525d6a61 509 ;
14f9c5c9
AS
510
511exp : STRING
4c4b4cd2
PH
512 {
513 write_exp_elt_opcode (OP_STRING);
514 write_exp_string ($1);
515 write_exp_elt_opcode (OP_STRING);
516 }
14f9c5c9
AS
517 ;
518
519exp : NEW TYPENAME
520 { error ("NEW not implemented."); }
521 ;
522
523variable: NAME { write_var_from_name (NULL, $1); }
524 | block NAME /* GDB extension */
525 { write_var_from_name ($1, $2); }
4c4b4cd2
PH
526 | OBJECT_RENAMING
527 { write_object_renaming (NULL, $1.sym,
528 MAX_RENAMING_CHAIN_LENGTH); }
529 | block OBJECT_RENAMING
530 { write_object_renaming ($1, $2.sym,
531 MAX_RENAMING_CHAIN_LENGTH); }
14f9c5c9
AS
532 ;
533
534any_name : NAME { }
535 | TYPENAME { }
536 | OBJECT_RENAMING { }
537 ;
538
539block : BLOCKNAME /* GDB extension */
540 { $$ = $1; }
541 | block BLOCKNAME /* GDB extension */
542 { $$ = $2; }
543 ;
544
545
546type : TYPENAME { $$ = $1; }
547 | block TYPENAME { $$ = $2; }
4c4b4cd2 548 | TYPENAME TICK_ACCESS
14f9c5c9
AS
549 { $$ = lookup_pointer_type ($1); }
550 | block TYPENAME TICK_ACCESS
551 { $$ = lookup_pointer_type ($2); }
552 ;
553
554/* Some extensions borrowed from C, for the benefit of those who find they
4c4b4cd2 555 can't get used to Ada notation in GDB. */
14f9c5c9
AS
556
557exp : '*' exp %prec '.'
558 { write_exp_elt_opcode (UNOP_IND); }
559 | '&' exp %prec '.'
560 { write_exp_elt_opcode (UNOP_ADDR); }
561 | exp '[' exp ']'
562 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
563 ;
564
565%%
566
567/* yylex defined in ada-lex.c: Reads one token, getting characters */
568/* through lexptr. */
569
570/* Remap normal flex interface names (yylex) as well as gratuitiously */
571/* global symbol names, so we can have multiple flex-generated parsers */
572/* in gdb. */
573
574/* (See note above on previous definitions for YACC.) */
575
576#define yy_create_buffer ada_yy_create_buffer
577#define yy_delete_buffer ada_yy_delete_buffer
578#define yy_init_buffer ada_yy_init_buffer
579#define yy_load_buffer_state ada_yy_load_buffer_state
580#define yy_switch_to_buffer ada_yy_switch_to_buffer
581#define yyrestart ada_yyrestart
582#define yytext ada_yytext
583#define yywrap ada_yywrap
584
4c4b4cd2
PH
585static struct obstack temp_parse_space;
586
14f9c5c9
AS
587/* The following kludge was found necessary to prevent conflicts between */
588/* defs.h and non-standard stdlib.h files. */
589#define qsort __qsort__dummy
590#include "ada-lex.c"
591
592int
4c4b4cd2 593ada_parse (void)
14f9c5c9 594{
4c4b4cd2 595 lexer_init (yyin); /* (Re-)initialize lexer. */
14f9c5c9
AS
596 left_block_context = NULL;
597 type_qualifier = NULL;
4c4b4cd2
PH
598 obstack_free (&temp_parse_space, NULL);
599 obstack_init (&temp_parse_space);
600
14f9c5c9
AS
601 return _ada_parse ();
602}
603
604void
4c4b4cd2 605yyerror (char *msg)
14f9c5c9
AS
606{
607 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
608}
609
4c4b4cd2 610/* The operator name corresponding to operator symbol STRING (adds
14f9c5c9
AS
611 quotes and maps to lower-case). Destroys the previous contents of
612 the array pointed to by STRING.ptr. Error if STRING does not match
613 a valid Ada operator. Assumes that STRING.ptr points to a
614 null-terminated string and that, if STRING is a valid operator
615 symbol, the array pointed to by STRING.ptr contains at least
4c4b4cd2 616 STRING.length+3 characters. */
14f9c5c9
AS
617
618static struct stoken
4c4b4cd2 619string_to_operator (struct stoken string)
14f9c5c9
AS
620{
621 int i;
622
4c4b4cd2 623 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
14f9c5c9 624 {
4c4b4cd2
PH
625 if (string.length == strlen (ada_opname_table[i].decoded)-2
626 && strncasecmp (string.ptr, ada_opname_table[i].decoded+1,
14f9c5c9
AS
627 string.length) == 0)
628 {
4c4b4cd2 629 strncpy (string.ptr, ada_opname_table[i].decoded,
14f9c5c9
AS
630 string.length+2);
631 string.length += 2;
632 return string;
633 }
634 }
635 error ("Invalid operator symbol `%s'", string.ptr);
636}
637
638/* Emit expression to access an instance of SYM, in block BLOCK (if
4c4b4cd2 639 * non-NULL), and with :: qualification ORIG_LEFT_CONTEXT. */
14f9c5c9 640static void
4c4b4cd2
PH
641write_var_from_sym (struct block *orig_left_context,
642 struct block *block,
643 struct symbol *sym)
14f9c5c9
AS
644{
645 if (orig_left_context == NULL && symbol_read_needs_frame (sym))
646 {
c3e5cd34
PH
647 if (innermost_block == 0
648 || contained_in (block, innermost_block))
14f9c5c9
AS
649 innermost_block = block;
650 }
651
652 write_exp_elt_opcode (OP_VAR_VALUE);
4c4b4cd2 653 write_exp_elt_block (block);
14f9c5c9
AS
654 write_exp_elt_sym (sym);
655 write_exp_elt_opcode (OP_VAR_VALUE);
656}
657
4c4b4cd2
PH
658/* Emit expression to access an instance of NAME in :: context
659 * ORIG_LEFT_CONTEXT. If no unique symbol for NAME has been found,
660 * output a dummy symbol (good to the next call of ada_parse) for NAME
661 * in the UNDEF_DOMAIN, for later resolution by ada_resolve. */
14f9c5c9 662static void
4c4b4cd2
PH
663write_var_from_name (struct block *orig_left_context,
664 struct name_info name)
14f9c5c9
AS
665{
666 if (name.msym != NULL)
667 {
4c4b4cd2 668 write_exp_msymbol (name.msym,
14f9c5c9
AS
669 lookup_function_type (builtin_type_int),
670 builtin_type_int);
671 }
4c4b4cd2 672 else if (name.sym == NULL)
14f9c5c9 673 {
4c4b4cd2
PH
674 /* Multiple matches: record name and starting block for later
675 resolution by ada_resolve. */
676 char *encoded_name = ada_encode (name.stoken.ptr);
677 struct symbol *sym =
678 obstack_alloc (&temp_parse_space, sizeof (struct symbol));
679 memset (sym, 0, sizeof (struct symbol));
680 SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
681 SYMBOL_LINKAGE_NAME (sym)
682 = obsavestring (encoded_name, strlen (encoded_name), &temp_parse_space);
683 SYMBOL_LANGUAGE (sym) = language_ada;
684
685 write_exp_elt_opcode (OP_VAR_VALUE);
14f9c5c9 686 write_exp_elt_block (name.block);
4c4b4cd2
PH
687 write_exp_elt_sym (sym);
688 write_exp_elt_opcode (OP_VAR_VALUE);
14f9c5c9
AS
689 }
690 else
691 write_var_from_sym (orig_left_context, name.block, name.sym);
692}
693
4c4b4cd2 694/* Write integer constant ARG of type TYPE. */
14f9c5c9
AS
695
696static void
4c4b4cd2 697write_int (LONGEST arg, struct type *type)
14f9c5c9
AS
698{
699 write_exp_elt_opcode (OP_LONG);
4c4b4cd2 700 write_exp_elt_type (type);
14f9c5c9
AS
701 write_exp_elt_longcst (arg);
702 write_exp_elt_opcode (OP_LONG);
4c4b4cd2 703}
14f9c5c9 704
4c4b4cd2 705/* Emit expression corresponding to the renamed object designated by
14f9c5c9 706 * the type RENAMING, which must be the referent of an object renaming
4c4b4cd2
PH
707 * type, in the context of ORIG_LEFT_CONTEXT. MAX_DEPTH is the maximum
708 * number of cascaded renamings to allow. */
14f9c5c9 709static void
4c4b4cd2
PH
710write_object_renaming (struct block *orig_left_context,
711 struct symbol *renaming, int max_depth)
14f9c5c9 712{
4c4b4cd2
PH
713 const char *qualification = SYMBOL_LINKAGE_NAME (renaming);
714 const char *simple_tail;
715 const char *expr = TYPE_FIELD_NAME (SYMBOL_TYPE (renaming), 0);
716 const char *suffix;
717 char *name;
718 struct symbol *sym;
14f9c5c9
AS
719 enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
720
4c4b4cd2
PH
721 if (max_depth <= 0)
722 error ("Could not find renamed symbol");
723
14f9c5c9 724 /* if orig_left_context is null, then use the currently selected
4c4b4cd2 725 block; otherwise we might fail our symbol lookup below. */
14f9c5c9
AS
726 if (orig_left_context == NULL)
727 orig_left_context = get_selected_block (NULL);
728
4c4b4cd2 729 for (simple_tail = qualification + strlen (qualification);
14f9c5c9
AS
730 simple_tail != qualification; simple_tail -= 1)
731 {
732 if (*simple_tail == '.')
733 {
734 simple_tail += 1;
735 break;
4c4b4cd2
PH
736 }
737 else if (strncmp (simple_tail, "__", 2) == 0)
14f9c5c9
AS
738 {
739 simple_tail += 2;
740 break;
741 }
742 }
743
744 suffix = strstr (expr, "___XE");
745 if (suffix == NULL)
746 goto BadEncoding;
747
4c4b4cd2 748 name = (char *) obstack_alloc (&temp_parse_space, suffix - expr + 1);
14f9c5c9
AS
749 strncpy (name, expr, suffix-expr);
750 name[suffix-expr] = '\000';
176620f1 751 sym = lookup_symbol (name, orig_left_context, VAR_DOMAIN, 0, NULL);
4c4b4cd2
PH
752 if (sym == NULL)
753 error ("Could not find renamed variable: %s", ada_decode (name));
754 if (ada_is_object_renaming (sym))
755 write_object_renaming (orig_left_context, sym, max_depth-1);
756 else
757 write_var_from_sym (orig_left_context, block_found, sym);
14f9c5c9
AS
758
759 suffix += 5;
760 slice_state = SIMPLE_INDEX;
4c4b4cd2 761 while (*suffix == 'X')
14f9c5c9
AS
762 {
763 suffix += 1;
764
765 switch (*suffix) {
4c4b4cd2
PH
766 case 'A':
767 suffix += 1;
768 write_exp_elt_opcode (UNOP_IND);
769 break;
14f9c5c9
AS
770 case 'L':
771 slice_state = LOWER_BOUND;
772 case 'S':
773 suffix += 1;
4c4b4cd2 774 if (isdigit (*suffix))
14f9c5c9 775 {
4c4b4cd2 776 char *next;
14f9c5c9 777 long val = strtol (suffix, &next, 10);
4c4b4cd2 778 if (next == suffix)
14f9c5c9
AS
779 goto BadEncoding;
780 suffix = next;
781 write_exp_elt_opcode (OP_LONG);
782 write_exp_elt_type (builtin_type_ada_int);
783 write_exp_elt_longcst ((LONGEST) val);
784 write_exp_elt_opcode (OP_LONG);
4c4b4cd2 785 }
14f9c5c9
AS
786 else
787 {
4c4b4cd2
PH
788 const char *end;
789 char *index_name;
14f9c5c9 790 int index_len;
4c4b4cd2 791 struct symbol *index_sym;
14f9c5c9
AS
792
793 end = strchr (suffix, 'X');
4c4b4cd2 794 if (end == NULL)
14f9c5c9 795 end = suffix + strlen (suffix);
4c4b4cd2 796
14f9c5c9 797 index_len = simple_tail - qualification + 2 + (suffix - end) + 1;
4c4b4cd2
PH
798 index_name
799 = (char *) obstack_alloc (&temp_parse_space, index_len);
14f9c5c9 800 memset (index_name, '\000', index_len);
14f9c5c9
AS
801 strncpy (index_name, qualification, simple_tail - qualification);
802 index_name[simple_tail - qualification] = '\000';
803 strncat (index_name, suffix, suffix-end);
804 suffix = end;
805
4c4b4cd2 806 index_sym =
176620f1 807 lookup_symbol (index_name, NULL, VAR_DOMAIN, 0, NULL);
14f9c5c9
AS
808 if (index_sym == NULL)
809 error ("Could not find %s", index_name);
810 write_var_from_sym (NULL, block_found, sym);
811 }
812 if (slice_state == SIMPLE_INDEX)
4c4b4cd2 813 {
14f9c5c9
AS
814 write_exp_elt_opcode (OP_FUNCALL);
815 write_exp_elt_longcst ((LONGEST) 1);
816 write_exp_elt_opcode (OP_FUNCALL);
817 }
818 else if (slice_state == LOWER_BOUND)
819 slice_state = UPPER_BOUND;
820 else if (slice_state == UPPER_BOUND)
821 {
822 write_exp_elt_opcode (TERNOP_SLICE);
823 slice_state = SIMPLE_INDEX;
824 }
825 break;
826
827 case 'R':
828 {
829 struct stoken field_name;
4c4b4cd2 830 const char *end;
14f9c5c9 831 suffix += 1;
4c4b4cd2 832
14f9c5c9
AS
833 if (slice_state != SIMPLE_INDEX)
834 goto BadEncoding;
835 end = strchr (suffix, 'X');
4c4b4cd2 836 if (end == NULL)
14f9c5c9
AS
837 end = suffix + strlen (suffix);
838 field_name.length = end - suffix;
4c4b4cd2 839 field_name.ptr = (char *) malloc (end - suffix + 1);
14f9c5c9
AS
840 strncpy (field_name.ptr, suffix, end - suffix);
841 field_name.ptr[end - suffix] = '\000';
842 suffix = end;
843 write_exp_elt_opcode (STRUCTOP_STRUCT);
844 write_exp_string (field_name);
4c4b4cd2 845 write_exp_elt_opcode (STRUCTOP_STRUCT);
14f9c5c9
AS
846 break;
847 }
4c4b4cd2 848
14f9c5c9
AS
849 default:
850 goto BadEncoding;
851 }
852 }
853 if (slice_state == SIMPLE_INDEX)
854 return;
855
856 BadEncoding:
857 error ("Internal error in encoding of renaming declaration: %s",
4c4b4cd2 858 SYMBOL_LINKAGE_NAME (renaming));
14f9c5c9
AS
859}
860
861/* Convert the character literal whose ASCII value would be VAL to the
862 appropriate value of type TYPE, if there is a translation.
4c4b4cd2
PH
863 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
864 the literal 'A' (VAL == 65), returns 0. */
14f9c5c9 865static LONGEST
4c4b4cd2 866convert_char_literal (struct type *type, LONGEST val)
14f9c5c9
AS
867{
868 char name[7];
869 int f;
870
871 if (type == NULL || TYPE_CODE (type) != TYPE_CODE_ENUM)
872 return val;
873 sprintf (name, "QU%02x", (int) val);
4c4b4cd2 874 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
14f9c5c9 875 {
4c4b4cd2 876 if (strcmp (name, TYPE_FIELD_NAME (type, f)) == 0)
14f9c5c9
AS
877 return TYPE_FIELD_BITPOS (type, f);
878 }
879 return val;
880}
4c4b4cd2
PH
881
882void
883_initialize_ada_exp (void)
884{
885 obstack_init (&temp_parse_space);
886}
This page took 0.217452 seconds and 4 git commands to generate.