Remove ada_value_print_inner
[binutils-gdb.git] / gdb / ada-exp.y
1 /* YACC parser for Ada expressions, for GDB.
2 Copyright (C) 1986-2021 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
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. */
35
36 %{
37
38 #include "defs.h"
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"
49 #include "block.h"
50 #include "ada-exp.h"
51
52 #define parse_type(ps) builtin_type (ps->gdbarch ())
53
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
55 etc). */
56 #define GDB_YY_REMAP_PREFIX ada_
57 #include "yy-remap.h"
58
59 struct name_info {
60 struct symbol *sym;
61 struct minimal_symbol *msym;
62 const struct block *block;
63 struct stoken stoken;
64 };
65
66 /* The state of the parser, used internally when we are parsing the
67 expression. */
68
69 static struct parser_state *pstate = NULL;
70
71 int yyparse (void);
72
73 static int yylex (void);
74
75 static void yyerror (const char *);
76
77 static void write_int (struct parser_state *, LONGEST, struct type *);
78
79 static void write_object_renaming (struct parser_state *,
80 const struct block *, const char *, int,
81 const char *, int);
82
83 static struct type* write_var_or_type (struct parser_state *,
84 const struct block *, struct stoken);
85
86 static void write_name_assoc (struct parser_state *, struct stoken);
87
88 static const struct block *block_lookup (const struct block *, const char *);
89
90 static void write_ambiguous_var (struct parser_state *,
91 const struct block *, const char *, int);
92
93 static struct type *type_int (struct parser_state *);
94
95 static struct type *type_long (struct parser_state *);
96
97 static struct type *type_long_long (struct parser_state *);
98
99 static struct type *type_long_double (struct parser_state *);
100
101 static struct type *type_char (struct parser_state *);
102
103 static struct type *type_boolean (struct parser_state *);
104
105 static struct type *type_system_address (struct parser_state *);
106
107 using namespace expr;
108
109 /* Handle Ada type resolution for OP. DEPROCEDURE_P and CONTEXT_TYPE
110 are passed to the resolve method, if called. */
111 static operation_up
112 resolve (operation_up &&op, bool deprocedure_p, struct type *context_type)
113 {
114 operation_up result = std::move (op);
115 ada_resolvable *res = dynamic_cast<ada_resolvable *> (result.get ());
116 if (res != nullptr)
117 return res->replace (std::move (result),
118 pstate->expout.get (),
119 deprocedure_p,
120 pstate->parse_completion,
121 pstate->block_tracker,
122 context_type);
123 return result;
124 }
125
126 /* Like parser_state::pop, but handles Ada type resolution.
127 DEPROCEDURE_P and CONTEXT_TYPE are passed to the resolve method, if
128 called. */
129 static operation_up
130 ada_pop (bool deprocedure_p = true, struct type *context_type = nullptr)
131 {
132 /* Of course it's ok to call parser_state::pop here... */
133 return resolve (pstate->pop (), deprocedure_p, context_type);
134 }
135
136 /* Like parser_state::wrap, but use ada_pop to pop the value. */
137 template<typename T>
138 void
139 ada_wrap ()
140 {
141 operation_up arg = ada_pop ();
142 pstate->push_new<T> (std::move (arg));
143 }
144
145 /* Create and push an address-of operation, as appropriate for Ada.
146 If TYPE is not NULL, the resulting operation will be wrapped in a
147 cast to TYPE. */
148 static void
149 ada_addrof (struct type *type = nullptr)
150 {
151 operation_up arg = ada_pop (false);
152 operation_up addr = make_operation<unop_addr_operation> (std::move (arg));
153 operation_up wrapped
154 = make_operation<ada_wrapped_operation> (std::move (addr));
155 if (type != nullptr)
156 wrapped = make_operation<unop_cast_operation> (std::move (wrapped), type);
157 pstate->push (std::move (wrapped));
158 }
159
160 /* Handle operator overloading. Either returns a function all
161 operation wrapping the arguments, or it returns null, leaving the
162 caller to construct the appropriate operation. If RHS is null, a
163 unary operator is assumed. */
164 static operation_up
165 maybe_overload (enum exp_opcode op, operation_up &lhs, operation_up &rhs)
166 {
167 struct value *args[2];
168
169 int nargs = 1;
170 args[0] = lhs->evaluate (nullptr, pstate->expout.get (),
171 EVAL_AVOID_SIDE_EFFECTS);
172 if (rhs == nullptr)
173 args[1] = nullptr;
174 else
175 {
176 args[1] = rhs->evaluate (nullptr, pstate->expout.get (),
177 EVAL_AVOID_SIDE_EFFECTS);
178 ++nargs;
179 }
180
181 block_symbol fn = ada_find_operator_symbol (op, pstate->parse_completion,
182 nargs, args);
183 if (fn.symbol == nullptr)
184 return {};
185
186 if (symbol_read_needs_frame (fn.symbol))
187 pstate->block_tracker->update (fn.block, INNERMOST_BLOCK_FOR_SYMBOLS);
188 operation_up callee = make_operation<ada_var_value_operation> (fn);
189
190 std::vector<operation_up> argvec;
191 argvec.push_back (std::move (lhs));
192 if (rhs != nullptr)
193 argvec.push_back (std::move (rhs));
194 return make_operation<ada_funcall_operation> (std::move (callee),
195 std::move (argvec));
196 }
197
198 /* Like parser_state::wrap, but use ada_pop to pop the value, and
199 handle unary overloading. */
200 template<typename T>
201 void
202 ada_wrap_overload (enum exp_opcode op)
203 {
204 operation_up arg = ada_pop ();
205 operation_up empty;
206
207 operation_up call = maybe_overload (op, arg, empty);
208 if (call == nullptr)
209 call = make_operation<T> (std::move (arg));
210 pstate->push (std::move (call));
211 }
212
213 /* A variant of parser_state::wrap2 that uses ada_pop to pop both
214 operands, and then pushes a new Ada-wrapped operation of the
215 template type T. */
216 template<typename T>
217 void
218 ada_un_wrap2 (enum exp_opcode op)
219 {
220 operation_up rhs = ada_pop ();
221 operation_up lhs = ada_pop ();
222
223 operation_up wrapped = maybe_overload (op, lhs, rhs);
224 if (wrapped == nullptr)
225 {
226 wrapped = make_operation<T> (std::move (lhs), std::move (rhs));
227 wrapped = make_operation<ada_wrapped_operation> (std::move (wrapped));
228 }
229 pstate->push (std::move (wrapped));
230 }
231
232 /* A variant of parser_state::wrap2 that uses ada_pop to pop both
233 operands. Unlike ada_un_wrap2, ada_wrapped_operation is not
234 used. */
235 template<typename T>
236 void
237 ada_wrap2 (enum exp_opcode op)
238 {
239 operation_up rhs = ada_pop ();
240 operation_up lhs = ada_pop ();
241 operation_up call = maybe_overload (op, lhs, rhs);
242 if (call == nullptr)
243 call = make_operation<T> (std::move (lhs), std::move (rhs));
244 pstate->push (std::move (call));
245 }
246
247 /* A variant of parser_state::wrap2 that uses ada_pop to pop both
248 operands. OP is also passed to the constructor of the new binary
249 operation. */
250 template<typename T>
251 void
252 ada_wrap_op (enum exp_opcode op)
253 {
254 operation_up rhs = ada_pop ();
255 operation_up lhs = ada_pop ();
256 operation_up call = maybe_overload (op, lhs, rhs);
257 if (call == nullptr)
258 call = make_operation<T> (op, std::move (lhs), std::move (rhs));
259 pstate->push (std::move (call));
260 }
261
262 /* Pop three operands using ada_pop, then construct a new ternary
263 operation of type T and push it. */
264 template<typename T>
265 void
266 ada_wrap3 ()
267 {
268 operation_up rhs = ada_pop ();
269 operation_up mid = ada_pop ();
270 operation_up lhs = ada_pop ();
271 pstate->push_new<T> (std::move (lhs), std::move (mid), std::move (rhs));
272 }
273
274 /* Pop NARGS operands, then a callee operand, and use these to
275 construct and push a new Ada function call operation. */
276 static void
277 ada_funcall (int nargs)
278 {
279 /* We use the ordinary pop here, because we're going to do
280 resolution in a separate step, in order to handle array
281 indices. */
282 std::vector<operation_up> args = pstate->pop_vector (nargs);
283 /* Call parser_state::pop here, because we don't want to
284 function-convert the callee slot of a call we're already
285 constructing. */
286 operation_up callee = pstate->pop ();
287
288 ada_var_value_operation *vvo
289 = dynamic_cast<ada_var_value_operation *> (callee.get ());
290 int array_arity = 0;
291 struct type *callee_t = nullptr;
292 if (vvo == nullptr
293 || SYMBOL_DOMAIN (vvo->get_symbol ()) != UNDEF_DOMAIN)
294 {
295 struct value *callee_v = callee->evaluate (nullptr,
296 pstate->expout.get (),
297 EVAL_AVOID_SIDE_EFFECTS);
298 callee_t = ada_check_typedef (value_type (callee_v));
299 array_arity = ada_array_arity (callee_t);
300 }
301
302 for (int i = 0; i < nargs; ++i)
303 {
304 struct type *subtype = nullptr;
305 if (i < array_arity)
306 subtype = ada_index_type (callee_t, i + 1, "array type");
307 args[i] = resolve (std::move (args[i]), true, subtype);
308 }
309
310 std::unique_ptr<ada_funcall_operation> funcall
311 (new ada_funcall_operation (std::move (callee), std::move (args)));
312 funcall->resolve (pstate->expout.get (), true, pstate->parse_completion,
313 pstate->block_tracker, nullptr);
314 pstate->push (std::move (funcall));
315 }
316
317 /* The components being constructed during this parse. */
318 static std::vector<ada_component_up> components;
319
320 /* Create a new ada_component_up of the indicated type and arguments,
321 and push it on the global 'components' vector. */
322 template<typename T, typename... Arg>
323 void
324 push_component (Arg... args)
325 {
326 components.emplace_back (new T (std::forward<Arg> (args)...));
327 }
328
329 /* Examine the final element of the 'components' vector, and return it
330 as a pointer to an ada_choices_component. The caller is
331 responsible for ensuring that the final element is in fact an
332 ada_choices_component. */
333 static ada_choices_component *
334 choice_component ()
335 {
336 ada_component *last = components.back ().get ();
337 ada_choices_component *result = dynamic_cast<ada_choices_component *> (last);
338 gdb_assert (result != nullptr);
339 return result;
340 }
341
342 /* Pop the most recent component from the global stack, and return
343 it. */
344 static ada_component_up
345 pop_component ()
346 {
347 ada_component_up result = std::move (components.back ());
348 components.pop_back ();
349 return result;
350 }
351
352 /* Pop the N most recent components from the global stack, and return
353 them in a vector. */
354 static std::vector<ada_component_up>
355 pop_components (int n)
356 {
357 std::vector<ada_component_up> result (n);
358 for (int i = 1; i <= n; ++i)
359 result[n - i] = pop_component ();
360 return result;
361 }
362
363 /* The associations being constructed during this parse. */
364 static std::vector<ada_association_up> associations;
365
366 /* Create a new ada_association_up of the indicated type and
367 arguments, and push it on the global 'associations' vector. */
368 template<typename T, typename... Arg>
369 void
370 push_association (Arg... args)
371 {
372 associations.emplace_back (new T (std::forward<Arg> (args)...));
373 }
374
375 /* Pop the most recent association from the global stack, and return
376 it. */
377 static ada_association_up
378 pop_association ()
379 {
380 ada_association_up result = std::move (associations.back ());
381 associations.pop_back ();
382 return result;
383 }
384
385 /* Pop the N most recent associations from the global stack, and
386 return them in a vector. */
387 static std::vector<ada_association_up>
388 pop_associations (int n)
389 {
390 std::vector<ada_association_up> result (n);
391 for (int i = 1; i <= n; ++i)
392 result[n - i] = pop_association ();
393 return result;
394 }
395
396 %}
397
398 %union
399 {
400 LONGEST lval;
401 struct {
402 LONGEST val;
403 struct type *type;
404 } typed_val;
405 struct {
406 gdb_byte val[16];
407 struct type *type;
408 } typed_val_float;
409 struct type *tval;
410 struct stoken sval;
411 const struct block *bval;
412 struct internalvar *ivar;
413 }
414
415 %type <lval> positional_list component_groups component_associations
416 %type <lval> aggregate_component_list
417 %type <tval> var_or_type type_prefix opt_type_prefix
418
419 %token <typed_val> INT NULL_PTR CHARLIT
420 %token <typed_val_float> FLOAT
421 %token TRUEKEYWORD FALSEKEYWORD
422 %token COLONCOLON
423 %token <sval> STRING NAME DOT_ID
424 %type <bval> block
425 %type <lval> arglist tick_arglist
426
427 %token DOT_ALL
428
429 /* Special type cases, put in to allow the parser to distinguish different
430 legal basetypes. */
431 %token <sval> DOLLAR_VARIABLE
432
433 %nonassoc ASSIGN
434 %left _AND_ OR XOR THEN ELSE
435 %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
436 %left '@'
437 %left '+' '-' '&'
438 %left UNARY
439 %left '*' '/' MOD REM
440 %right STARSTAR ABS NOT
441
442 /* Artificial token to give NAME => ... and NAME | priority over reducing
443 NAME to <primary> and to give <primary>' priority over reducing <primary>
444 to <simple_exp>. */
445 %nonassoc VAR
446
447 %nonassoc ARROW '|'
448
449 %right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
450 %right TICK_MAX TICK_MIN TICK_MODULUS
451 %right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
452 /* The following are right-associative only so that reductions at this
453 precedence have lower precedence than '.' and '('. The syntax still
454 forces a.b.c, e.g., to be LEFT-associated. */
455 %right '.' '(' '[' DOT_ID DOT_ALL
456
457 %token NEW OTHERS
458
459 \f
460 %%
461
462 start : exp1
463 ;
464
465 /* Expressions, including the sequencing operator. */
466 exp1 : exp
467 | exp1 ';' exp
468 { ada_wrap2<comma_operation> (BINOP_COMMA); }
469 | primary ASSIGN exp /* Extension for convenience */
470 {
471 operation_up rhs = pstate->pop ();
472 operation_up lhs = ada_pop ();
473 value *lhs_val
474 = lhs->evaluate (nullptr, pstate->expout.get (),
475 EVAL_AVOID_SIDE_EFFECTS);
476 rhs = resolve (std::move (rhs), true,
477 value_type (lhs_val));
478 pstate->push_new<ada_assign_operation>
479 (std::move (lhs), std::move (rhs));
480 }
481 ;
482
483 /* Expressions, not including the sequencing operator. */
484 primary : primary DOT_ALL
485 { ada_wrap<ada_unop_ind_operation> (); }
486 ;
487
488 primary : primary DOT_ID
489 {
490 operation_up arg = ada_pop ();
491 pstate->push_new<ada_structop_operation>
492 (std::move (arg), copy_name ($2));
493 }
494 ;
495
496 primary : primary '(' arglist ')'
497 { ada_funcall ($3); }
498 | var_or_type '(' arglist ')'
499 {
500 if ($1 != NULL)
501 {
502 if ($3 != 1)
503 error (_("Invalid conversion"));
504 operation_up arg = ada_pop ();
505 pstate->push_new<unop_cast_operation>
506 (std::move (arg), $1);
507 }
508 else
509 ada_funcall ($3);
510 }
511 ;
512
513 primary : var_or_type '\'' '(' exp ')'
514 {
515 if ($1 == NULL)
516 error (_("Type required for qualification"));
517 operation_up arg = ada_pop (true,
518 check_typedef ($1));
519 pstate->push_new<ada_qual_operation>
520 (std::move (arg), $1);
521 }
522 ;
523
524 primary :
525 primary '(' simple_exp DOTDOT simple_exp ')'
526 { ada_wrap3<ada_ternop_slice_operation> (); }
527 | var_or_type '(' simple_exp DOTDOT simple_exp ')'
528 { if ($1 == NULL)
529 ada_wrap3<ada_ternop_slice_operation> ();
530 else
531 error (_("Cannot slice a type"));
532 }
533 ;
534
535 primary : '(' exp1 ')' { }
536 ;
537
538 /* The following rule causes a conflict with the type conversion
539 var_or_type (exp)
540 To get around it, we give '(' higher priority and add bridge rules for
541 var_or_type (exp, exp, ...)
542 var_or_type (exp .. exp)
543 We also have the action for var_or_type(exp) generate a function call
544 when the first symbol does not denote a type. */
545
546 primary : var_or_type %prec VAR
547 { if ($1 != NULL)
548 pstate->push_new<type_operation> ($1);
549 }
550 ;
551
552 primary : DOLLAR_VARIABLE /* Various GDB extensions */
553 { pstate->push_dollar ($1); }
554 ;
555
556 primary : aggregate
557 {
558 pstate->push_new<ada_aggregate_operation>
559 (pop_component ());
560 }
561 ;
562
563 simple_exp : primary
564 ;
565
566 simple_exp : '-' simple_exp %prec UNARY
567 { ada_wrap_overload<ada_neg_operation> (UNOP_NEG); }
568 ;
569
570 simple_exp : '+' simple_exp %prec UNARY
571 {
572 operation_up arg = ada_pop ();
573 operation_up empty;
574
575 /* If an overloaded operator was found, use
576 it. Otherwise, unary + has no effect and
577 the argument can be pushed instead. */
578 operation_up call = maybe_overload (UNOP_PLUS, arg,
579 empty);
580 if (call != nullptr)
581 arg = std::move (call);
582 pstate->push (std::move (arg));
583 }
584 ;
585
586 simple_exp : NOT simple_exp %prec UNARY
587 {
588 ada_wrap_overload<unary_logical_not_operation>
589 (UNOP_LOGICAL_NOT);
590 }
591 ;
592
593 simple_exp : ABS simple_exp %prec UNARY
594 { ada_wrap_overload<ada_abs_operation> (UNOP_ABS); }
595 ;
596
597 arglist : { $$ = 0; }
598 ;
599
600 arglist : exp
601 { $$ = 1; }
602 | NAME ARROW exp
603 { $$ = 1; }
604 | arglist ',' exp
605 { $$ = $1 + 1; }
606 | arglist ',' NAME ARROW exp
607 { $$ = $1 + 1; }
608 ;
609
610 primary : '{' var_or_type '}' primary %prec '.'
611 /* GDB extension */
612 {
613 if ($2 == NULL)
614 error (_("Type required within braces in coercion"));
615 operation_up arg = ada_pop ();
616 pstate->push_new<unop_memval_operation>
617 (std::move (arg), $2);
618 }
619 ;
620
621 /* Binary operators in order of decreasing precedence. */
622
623 simple_exp : simple_exp STARSTAR simple_exp
624 { ada_wrap2<ada_binop_exp_operation> (BINOP_EXP); }
625 ;
626
627 simple_exp : simple_exp '*' simple_exp
628 { ada_wrap2<ada_binop_mul_operation> (BINOP_MUL); }
629 ;
630
631 simple_exp : simple_exp '/' simple_exp
632 { ada_wrap2<ada_binop_div_operation> (BINOP_DIV); }
633 ;
634
635 simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
636 { ada_wrap2<ada_binop_rem_operation> (BINOP_REM); }
637 ;
638
639 simple_exp : simple_exp MOD simple_exp
640 { ada_wrap2<ada_binop_mod_operation> (BINOP_MOD); }
641 ;
642
643 simple_exp : simple_exp '@' simple_exp /* GDB extension */
644 { ada_wrap2<repeat_operation> (BINOP_REPEAT); }
645 ;
646
647 simple_exp : simple_exp '+' simple_exp
648 { ada_wrap_op<ada_binop_addsub_operation> (BINOP_ADD); }
649 ;
650
651 simple_exp : simple_exp '&' simple_exp
652 { ada_wrap2<concat_operation> (BINOP_CONCAT); }
653 ;
654
655 simple_exp : simple_exp '-' simple_exp
656 { ada_wrap_op<ada_binop_addsub_operation> (BINOP_SUB); }
657 ;
658
659 relation : simple_exp
660 ;
661
662 relation : simple_exp '=' simple_exp
663 { ada_wrap_op<ada_binop_equal_operation> (BINOP_EQUAL); }
664 ;
665
666 relation : simple_exp NOTEQUAL simple_exp
667 { ada_wrap_op<ada_binop_equal_operation> (BINOP_NOTEQUAL); }
668 ;
669
670 relation : simple_exp LEQ simple_exp
671 { ada_un_wrap2<leq_operation> (BINOP_LEQ); }
672 ;
673
674 relation : simple_exp IN simple_exp DOTDOT simple_exp
675 { ada_wrap3<ada_ternop_range_operation> (); }
676 | simple_exp IN primary TICK_RANGE tick_arglist
677 {
678 operation_up rhs = ada_pop ();
679 operation_up lhs = ada_pop ();
680 pstate->push_new<ada_binop_in_bounds_operation>
681 (std::move (lhs), std::move (rhs), $5);
682 }
683 | simple_exp IN var_or_type %prec TICK_ACCESS
684 {
685 if ($3 == NULL)
686 error (_("Right operand of 'in' must be type"));
687 operation_up arg = ada_pop ();
688 pstate->push_new<ada_unop_range_operation>
689 (std::move (arg), $3);
690 }
691 | simple_exp NOT IN simple_exp DOTDOT simple_exp
692 { ada_wrap3<ada_ternop_range_operation> ();
693 ada_wrap<unary_logical_not_operation> (); }
694 | simple_exp NOT IN primary TICK_RANGE tick_arglist
695 {
696 operation_up rhs = ada_pop ();
697 operation_up lhs = ada_pop ();
698 pstate->push_new<ada_binop_in_bounds_operation>
699 (std::move (lhs), std::move (rhs), $6);
700 ada_wrap<unary_logical_not_operation> ();
701 }
702 | simple_exp NOT IN var_or_type %prec TICK_ACCESS
703 {
704 if ($4 == NULL)
705 error (_("Right operand of 'in' must be type"));
706 operation_up arg = ada_pop ();
707 pstate->push_new<ada_unop_range_operation>
708 (std::move (arg), $4);
709 ada_wrap<unary_logical_not_operation> ();
710 }
711 ;
712
713 relation : simple_exp GEQ simple_exp
714 { ada_un_wrap2<geq_operation> (BINOP_GEQ); }
715 ;
716
717 relation : simple_exp '<' simple_exp
718 { ada_un_wrap2<less_operation> (BINOP_LESS); }
719 ;
720
721 relation : simple_exp '>' simple_exp
722 { ada_un_wrap2<gtr_operation> (BINOP_GTR); }
723 ;
724
725 exp : relation
726 | and_exp
727 | and_then_exp
728 | or_exp
729 | or_else_exp
730 | xor_exp
731 ;
732
733 and_exp :
734 relation _AND_ relation
735 { ada_wrap2<ada_bitwise_and_operation>
736 (BINOP_BITWISE_AND); }
737 | and_exp _AND_ relation
738 { ada_wrap2<ada_bitwise_and_operation>
739 (BINOP_BITWISE_AND); }
740 ;
741
742 and_then_exp :
743 relation _AND_ THEN relation
744 { ada_wrap2<logical_and_operation>
745 (BINOP_LOGICAL_AND); }
746 | and_then_exp _AND_ THEN relation
747 { ada_wrap2<logical_and_operation>
748 (BINOP_LOGICAL_AND); }
749 ;
750
751 or_exp :
752 relation OR relation
753 { ada_wrap2<ada_bitwise_ior_operation>
754 (BINOP_BITWISE_IOR); }
755 | or_exp OR relation
756 { ada_wrap2<ada_bitwise_ior_operation>
757 (BINOP_BITWISE_IOR); }
758 ;
759
760 or_else_exp :
761 relation OR ELSE relation
762 { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
763 | or_else_exp OR ELSE relation
764 { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
765 ;
766
767 xor_exp : relation XOR relation
768 { ada_wrap2<ada_bitwise_xor_operation>
769 (BINOP_BITWISE_XOR); }
770 | xor_exp XOR relation
771 { ada_wrap2<ada_bitwise_xor_operation>
772 (BINOP_BITWISE_XOR); }
773 ;
774
775 /* Primaries can denote types (OP_TYPE). In cases such as
776 primary TICK_ADDRESS, where a type would be invalid, it will be
777 caught when evaluate_subexp in ada-lang.c tries to evaluate the
778 primary, expecting a value. Precedence rules resolve the ambiguity
779 in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A
780 construct such as aType'access'access will again cause an error when
781 aType'access evaluates to a type that evaluate_subexp attempts to
782 evaluate. */
783 primary : primary TICK_ACCESS
784 { ada_addrof (); }
785 | primary TICK_ADDRESS
786 { ada_addrof (type_system_address (pstate)); }
787 | primary TICK_FIRST tick_arglist
788 {
789 operation_up arg = ada_pop ();
790 pstate->push_new<ada_unop_atr_operation>
791 (std::move (arg), OP_ATR_FIRST, $3);
792 }
793 | primary TICK_LAST tick_arglist
794 {
795 operation_up arg = ada_pop ();
796 pstate->push_new<ada_unop_atr_operation>
797 (std::move (arg), OP_ATR_LAST, $3);
798 }
799 | primary TICK_LENGTH tick_arglist
800 {
801 operation_up arg = ada_pop ();
802 pstate->push_new<ada_unop_atr_operation>
803 (std::move (arg), OP_ATR_LENGTH, $3);
804 }
805 | primary TICK_SIZE
806 { ada_wrap<ada_atr_size_operation> (); }
807 | primary TICK_TAG
808 { ada_wrap<ada_atr_tag_operation> (); }
809 | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
810 { ada_wrap2<ada_binop_min_operation> (BINOP_MIN); }
811 | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
812 { ada_wrap2<ada_binop_max_operation> (BINOP_MAX); }
813 | opt_type_prefix TICK_POS '(' exp ')'
814 { ada_wrap<ada_pos_operation> (); }
815 | type_prefix TICK_VAL '(' exp ')'
816 {
817 operation_up arg = ada_pop ();
818 pstate->push_new<ada_atr_val_operation>
819 ($1, std::move (arg));
820 }
821 | type_prefix TICK_MODULUS
822 {
823 struct type *type_arg = check_typedef ($1);
824 if (!ada_is_modular_type (type_arg))
825 error (_("'modulus must be applied to modular type"));
826 write_int (pstate, ada_modulus (type_arg),
827 TYPE_TARGET_TYPE (type_arg));
828 }
829 ;
830
831 tick_arglist : %prec '('
832 { $$ = 1; }
833 | '(' INT ')'
834 { $$ = $2.val; }
835 ;
836
837 type_prefix :
838 var_or_type
839 {
840 if ($1 == NULL)
841 error (_("Prefix must be type"));
842 $$ = $1;
843 }
844 ;
845
846 opt_type_prefix :
847 type_prefix
848 { $$ = $1; }
849 | /* EMPTY */
850 { $$ = parse_type (pstate)->builtin_void; }
851 ;
852
853
854 primary : INT
855 { write_int (pstate, (LONGEST) $1.val, $1.type); }
856 ;
857
858 primary : CHARLIT
859 {
860 pstate->push_new<ada_char_operation> ($1.type, $1.val);
861 }
862 ;
863
864 primary : FLOAT
865 {
866 float_data data;
867 std::copy (std::begin ($1.val), std::end ($1.val),
868 std::begin (data));
869 pstate->push_new<float_const_operation>
870 ($1.type, data);
871 ada_wrap<ada_wrapped_operation> ();
872 }
873 ;
874
875 primary : NULL_PTR
876 {
877 struct type *null_ptr_type
878 = lookup_pointer_type (parse_type (pstate)->builtin_int0);
879 write_int (pstate, 0, null_ptr_type);
880 }
881 ;
882
883 primary : STRING
884 {
885 pstate->push_new<ada_string_operation>
886 (copy_name ($1));
887 }
888 ;
889
890 primary : TRUEKEYWORD
891 { write_int (pstate, 1, type_boolean (pstate)); }
892 | FALSEKEYWORD
893 { write_int (pstate, 0, type_boolean (pstate)); }
894 ;
895
896 primary : NEW NAME
897 { error (_("NEW not implemented.")); }
898 ;
899
900 var_or_type: NAME %prec VAR
901 { $$ = write_var_or_type (pstate, NULL, $1); }
902 | block NAME %prec VAR
903 { $$ = write_var_or_type (pstate, $1, $2); }
904 | NAME TICK_ACCESS
905 {
906 $$ = write_var_or_type (pstate, NULL, $1);
907 if ($$ == NULL)
908 ada_addrof ();
909 else
910 $$ = lookup_pointer_type ($$);
911 }
912 | block NAME TICK_ACCESS
913 {
914 $$ = write_var_or_type (pstate, $1, $2);
915 if ($$ == NULL)
916 ada_addrof ();
917 else
918 $$ = lookup_pointer_type ($$);
919 }
920 ;
921
922 /* GDB extension */
923 block : NAME COLONCOLON
924 { $$ = block_lookup (NULL, $1.ptr); }
925 | block NAME COLONCOLON
926 { $$ = block_lookup ($1, $2.ptr); }
927 ;
928
929 aggregate :
930 '(' aggregate_component_list ')'
931 {
932 std::vector<ada_component_up> components
933 = pop_components ($2);
934
935 push_component<ada_aggregate_component>
936 (std::move (components));
937 }
938 ;
939
940 aggregate_component_list :
941 component_groups { $$ = $1; }
942 | positional_list exp
943 {
944 push_component<ada_positional_component>
945 ($1, ada_pop ());
946 $$ = $1 + 1;
947 }
948 | positional_list component_groups
949 { $$ = $1 + $2; }
950 ;
951
952 positional_list :
953 exp ','
954 {
955 push_component<ada_positional_component>
956 (0, ada_pop ());
957 $$ = 1;
958 }
959 | positional_list exp ','
960 {
961 push_component<ada_positional_component>
962 ($1, ada_pop ());
963 $$ = $1 + 1;
964 }
965 ;
966
967 component_groups:
968 others { $$ = 1; }
969 | component_group { $$ = 1; }
970 | component_group ',' component_groups
971 { $$ = $3 + 1; }
972 ;
973
974 others : OTHERS ARROW exp
975 {
976 push_component<ada_others_component> (ada_pop ());
977 }
978 ;
979
980 component_group :
981 component_associations
982 {
983 ada_choices_component *choices = choice_component ();
984 choices->set_associations (pop_associations ($1));
985 }
986 ;
987
988 /* We use this somewhat obscure definition in order to handle NAME => and
989 NAME | differently from exp => and exp |. ARROW and '|' have a precedence
990 above that of the reduction of NAME to var_or_type. By delaying
991 decisions until after the => or '|', we convert the ambiguity to a
992 resolved shift/reduce conflict. */
993 component_associations :
994 NAME ARROW exp
995 {
996 push_component<ada_choices_component> (ada_pop ());
997 write_name_assoc (pstate, $1);
998 $$ = 1;
999 }
1000 | simple_exp ARROW exp
1001 {
1002 push_component<ada_choices_component> (ada_pop ());
1003 push_association<ada_name_association> (ada_pop ());
1004 $$ = 1;
1005 }
1006 | simple_exp DOTDOT simple_exp ARROW exp
1007 {
1008 push_component<ada_choices_component> (ada_pop ());
1009 operation_up rhs = ada_pop ();
1010 operation_up lhs = ada_pop ();
1011 push_association<ada_discrete_range_association>
1012 (std::move (lhs), std::move (rhs));
1013 $$ = 1;
1014 }
1015 | NAME '|' component_associations
1016 {
1017 write_name_assoc (pstate, $1);
1018 $$ = $3 + 1;
1019 }
1020 | simple_exp '|' component_associations
1021 {
1022 push_association<ada_name_association> (ada_pop ());
1023 $$ = $3 + 1;
1024 }
1025 | simple_exp DOTDOT simple_exp '|' component_associations
1026
1027 {
1028 operation_up rhs = ada_pop ();
1029 operation_up lhs = ada_pop ();
1030 push_association<ada_discrete_range_association>
1031 (std::move (lhs), std::move (rhs));
1032 $$ = $5 + 1;
1033 }
1034 ;
1035
1036 /* Some extensions borrowed from C, for the benefit of those who find they
1037 can't get used to Ada notation in GDB. */
1038
1039 primary : '*' primary %prec '.'
1040 { ada_wrap<ada_unop_ind_operation> (); }
1041 | '&' primary %prec '.'
1042 { ada_addrof (); }
1043 | primary '[' exp ']'
1044 {
1045 ada_wrap2<subscript_operation> (BINOP_SUBSCRIPT);
1046 ada_wrap<ada_wrapped_operation> ();
1047 }
1048 ;
1049
1050 %%
1051
1052 /* yylex defined in ada-lex.c: Reads one token, getting characters */
1053 /* through lexptr. */
1054
1055 /* Remap normal flex interface names (yylex) as well as gratuitiously */
1056 /* global symbol names, so we can have multiple flex-generated parsers */
1057 /* in gdb. */
1058
1059 /* (See note above on previous definitions for YACC.) */
1060
1061 #define yy_create_buffer ada_yy_create_buffer
1062 #define yy_delete_buffer ada_yy_delete_buffer
1063 #define yy_init_buffer ada_yy_init_buffer
1064 #define yy_load_buffer_state ada_yy_load_buffer_state
1065 #define yy_switch_to_buffer ada_yy_switch_to_buffer
1066 #define yyrestart ada_yyrestart
1067 #define yytext ada_yytext
1068
1069 static struct obstack temp_parse_space;
1070
1071 /* The following kludge was found necessary to prevent conflicts between */
1072 /* defs.h and non-standard stdlib.h files. */
1073 #define qsort __qsort__dummy
1074 #include "ada-lex.c"
1075
1076 int
1077 ada_parse (struct parser_state *par_state)
1078 {
1079 /* Setting up the parser state. */
1080 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1081 gdb_assert (par_state != NULL);
1082 pstate = par_state;
1083
1084 lexer_init (yyin); /* (Re-)initialize lexer. */
1085 obstack_free (&temp_parse_space, NULL);
1086 obstack_init (&temp_parse_space);
1087 components.clear ();
1088 associations.clear ();
1089
1090 int result = yyparse ();
1091 if (!result)
1092 {
1093 struct type *context_type = nullptr;
1094 if (par_state->void_context_p)
1095 context_type = parse_type (par_state)->builtin_void;
1096 pstate->set_operation (ada_pop (true, context_type));
1097 }
1098 return result;
1099 }
1100
1101 static void
1102 yyerror (const char *msg)
1103 {
1104 error (_("Error in expression, near `%s'."), pstate->lexptr);
1105 }
1106
1107 /* Emit expression to access an instance of SYM, in block BLOCK (if
1108 non-NULL). */
1109
1110 static void
1111 write_var_from_sym (struct parser_state *par_state, block_symbol sym)
1112 {
1113 if (symbol_read_needs_frame (sym.symbol))
1114 par_state->block_tracker->update (sym.block, INNERMOST_BLOCK_FOR_SYMBOLS);
1115
1116 par_state->push_new<ada_var_value_operation> (sym);
1117 }
1118
1119 /* Write integer or boolean constant ARG of type TYPE. */
1120
1121 static void
1122 write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
1123 {
1124 pstate->push_new<long_const_operation> (type, arg);
1125 ada_wrap<ada_wrapped_operation> ();
1126 }
1127
1128 /* Emit expression corresponding to the renamed object named
1129 * designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
1130 * context of ORIG_LEFT_CONTEXT, to which is applied the operations
1131 * encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of
1132 * cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it
1133 * defaults to the currently selected block. ORIG_SYMBOL is the
1134 * symbol that originally encoded the renaming. It is needed only
1135 * because its prefix also qualifies any index variables used to index
1136 * or slice an array. It should not be necessary once we go to the
1137 * new encoding entirely (FIXME pnh 7/20/2007). */
1138
1139 static void
1140 write_object_renaming (struct parser_state *par_state,
1141 const struct block *orig_left_context,
1142 const char *renamed_entity, int renamed_entity_len,
1143 const char *renaming_expr, int max_depth)
1144 {
1145 char *name;
1146 enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
1147 struct block_symbol sym_info;
1148
1149 if (max_depth <= 0)
1150 error (_("Could not find renamed symbol"));
1151
1152 if (orig_left_context == NULL)
1153 orig_left_context = get_selected_block (NULL);
1154
1155 name = obstack_strndup (&temp_parse_space, renamed_entity,
1156 renamed_entity_len);
1157 ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
1158 if (sym_info.symbol == NULL)
1159 error (_("Could not find renamed variable: %s"), ada_decode (name).c_str ());
1160 else if (SYMBOL_CLASS (sym_info.symbol) == LOC_TYPEDEF)
1161 /* We have a renaming of an old-style renaming symbol. Don't
1162 trust the block information. */
1163 sym_info.block = orig_left_context;
1164
1165 {
1166 const char *inner_renamed_entity;
1167 int inner_renamed_entity_len;
1168 const char *inner_renaming_expr;
1169
1170 switch (ada_parse_renaming (sym_info.symbol, &inner_renamed_entity,
1171 &inner_renamed_entity_len,
1172 &inner_renaming_expr))
1173 {
1174 case ADA_NOT_RENAMING:
1175 write_var_from_sym (par_state, sym_info);
1176 break;
1177 case ADA_OBJECT_RENAMING:
1178 write_object_renaming (par_state, sym_info.block,
1179 inner_renamed_entity, inner_renamed_entity_len,
1180 inner_renaming_expr, max_depth - 1);
1181 break;
1182 default:
1183 goto BadEncoding;
1184 }
1185 }
1186
1187 slice_state = SIMPLE_INDEX;
1188 while (*renaming_expr == 'X')
1189 {
1190 renaming_expr += 1;
1191
1192 switch (*renaming_expr) {
1193 case 'A':
1194 renaming_expr += 1;
1195 ada_wrap<ada_unop_ind_operation> ();
1196 break;
1197 case 'L':
1198 slice_state = LOWER_BOUND;
1199 /* FALLTHROUGH */
1200 case 'S':
1201 renaming_expr += 1;
1202 if (isdigit (*renaming_expr))
1203 {
1204 char *next;
1205 long val = strtol (renaming_expr, &next, 10);
1206 if (next == renaming_expr)
1207 goto BadEncoding;
1208 renaming_expr = next;
1209 write_int (par_state, val, type_int (par_state));
1210 }
1211 else
1212 {
1213 const char *end;
1214 char *index_name;
1215 struct block_symbol index_sym_info;
1216
1217 end = strchr (renaming_expr, 'X');
1218 if (end == NULL)
1219 end = renaming_expr + strlen (renaming_expr);
1220
1221 index_name = obstack_strndup (&temp_parse_space, renaming_expr,
1222 end - renaming_expr);
1223 renaming_expr = end;
1224
1225 ada_lookup_encoded_symbol (index_name, orig_left_context,
1226 VAR_DOMAIN, &index_sym_info);
1227 if (index_sym_info.symbol == NULL)
1228 error (_("Could not find %s"), index_name);
1229 else if (SYMBOL_CLASS (index_sym_info.symbol) == LOC_TYPEDEF)
1230 /* Index is an old-style renaming symbol. */
1231 index_sym_info.block = orig_left_context;
1232 write_var_from_sym (par_state, index_sym_info);
1233 }
1234 if (slice_state == SIMPLE_INDEX)
1235 ada_funcall (1);
1236 else if (slice_state == LOWER_BOUND)
1237 slice_state = UPPER_BOUND;
1238 else if (slice_state == UPPER_BOUND)
1239 {
1240 ada_wrap3<ada_ternop_slice_operation> ();
1241 slice_state = SIMPLE_INDEX;
1242 }
1243 break;
1244
1245 case 'R':
1246 {
1247 const char *end;
1248
1249 renaming_expr += 1;
1250
1251 if (slice_state != SIMPLE_INDEX)
1252 goto BadEncoding;
1253 end = strchr (renaming_expr, 'X');
1254 if (end == NULL)
1255 end = renaming_expr + strlen (renaming_expr);
1256
1257 operation_up arg = ada_pop ();
1258 pstate->push_new<ada_structop_operation>
1259 (std::move (arg), std::string (renaming_expr,
1260 end - renaming_expr));
1261 renaming_expr = end;
1262 break;
1263 }
1264
1265 default:
1266 goto BadEncoding;
1267 }
1268 }
1269 if (slice_state == SIMPLE_INDEX)
1270 return;
1271
1272 BadEncoding:
1273 error (_("Internal error in encoding of renaming declaration"));
1274 }
1275
1276 static const struct block*
1277 block_lookup (const struct block *context, const char *raw_name)
1278 {
1279 const char *name;
1280 struct symtab *symtab;
1281 const struct block *result = NULL;
1282
1283 std::string name_storage;
1284 if (raw_name[0] == '\'')
1285 {
1286 raw_name += 1;
1287 name = raw_name;
1288 }
1289 else
1290 {
1291 name_storage = ada_encode (raw_name);
1292 name = name_storage.c_str ();
1293 }
1294
1295 std::vector<struct block_symbol> syms
1296 = ada_lookup_symbol_list (name, context, VAR_DOMAIN);
1297
1298 if (context == NULL
1299 && (syms.empty () || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK))
1300 symtab = lookup_symtab (name);
1301 else
1302 symtab = NULL;
1303
1304 if (symtab != NULL)
1305 result = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
1306 else if (syms.empty () || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK)
1307 {
1308 if (context == NULL)
1309 error (_("No file or function \"%s\"."), raw_name);
1310 else
1311 error (_("No function \"%s\" in specified context."), raw_name);
1312 }
1313 else
1314 {
1315 if (syms.size () > 1)
1316 warning (_("Function name \"%s\" ambiguous here"), raw_name);
1317 result = SYMBOL_BLOCK_VALUE (syms[0].symbol);
1318 }
1319
1320 return result;
1321 }
1322
1323 static struct symbol*
1324 select_possible_type_sym (const std::vector<struct block_symbol> &syms)
1325 {
1326 int i;
1327 int preferred_index;
1328 struct type *preferred_type;
1329
1330 preferred_index = -1; preferred_type = NULL;
1331 for (i = 0; i < syms.size (); i += 1)
1332 switch (SYMBOL_CLASS (syms[i].symbol))
1333 {
1334 case LOC_TYPEDEF:
1335 if (ada_prefer_type (SYMBOL_TYPE (syms[i].symbol), preferred_type))
1336 {
1337 preferred_index = i;
1338 preferred_type = SYMBOL_TYPE (syms[i].symbol);
1339 }
1340 break;
1341 case LOC_REGISTER:
1342 case LOC_ARG:
1343 case LOC_REF_ARG:
1344 case LOC_REGPARM_ADDR:
1345 case LOC_LOCAL:
1346 case LOC_COMPUTED:
1347 return NULL;
1348 default:
1349 break;
1350 }
1351 if (preferred_type == NULL)
1352 return NULL;
1353 return syms[preferred_index].symbol;
1354 }
1355
1356 static struct type*
1357 find_primitive_type (struct parser_state *par_state, const char *name)
1358 {
1359 struct type *type;
1360 type = language_lookup_primitive_type (par_state->language (),
1361 par_state->gdbarch (),
1362 name);
1363 if (type == NULL && strcmp ("system__address", name) == 0)
1364 type = type_system_address (par_state);
1365
1366 if (type != NULL)
1367 {
1368 /* Check to see if we have a regular definition of this
1369 type that just didn't happen to have been read yet. */
1370 struct symbol *sym;
1371 char *expanded_name =
1372 (char *) alloca (strlen (name) + sizeof ("standard__"));
1373 strcpy (expanded_name, "standard__");
1374 strcat (expanded_name, name);
1375 sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN).symbol;
1376 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1377 type = SYMBOL_TYPE (sym);
1378 }
1379
1380 return type;
1381 }
1382
1383 static int
1384 chop_selector (const char *name, int end)
1385 {
1386 int i;
1387 for (i = end - 1; i > 0; i -= 1)
1388 if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
1389 return i;
1390 return -1;
1391 }
1392
1393 /* If NAME is a string beginning with a separator (either '__', or
1394 '.'), chop this separator and return the result; else, return
1395 NAME. */
1396
1397 static const char *
1398 chop_separator (const char *name)
1399 {
1400 if (*name == '.')
1401 return name + 1;
1402
1403 if (name[0] == '_' && name[1] == '_')
1404 return name + 2;
1405
1406 return name;
1407 }
1408
1409 /* Given that SELS is a string of the form (<sep><identifier>)*, where
1410 <sep> is '__' or '.', write the indicated sequence of
1411 STRUCTOP_STRUCT expression operators. */
1412 static void
1413 write_selectors (struct parser_state *par_state, const char *sels)
1414 {
1415 while (*sels != '\0')
1416 {
1417 const char *p = chop_separator (sels);
1418 sels = p;
1419 while (*sels != '\0' && *sels != '.'
1420 && (sels[0] != '_' || sels[1] != '_'))
1421 sels += 1;
1422 operation_up arg = ada_pop ();
1423 pstate->push_new<ada_structop_operation>
1424 (std::move (arg), std::string (p, sels - p));
1425 }
1426 }
1427
1428 /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
1429 NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes
1430 a temporary symbol that is valid until the next call to ada_parse.
1431 */
1432 static void
1433 write_ambiguous_var (struct parser_state *par_state,
1434 const struct block *block, const char *name, int len)
1435 {
1436 struct symbol *sym = new (&temp_parse_space) symbol ();
1437
1438 SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
1439 sym->set_linkage_name (obstack_strndup (&temp_parse_space, name, len));
1440 sym->set_language (language_ada, nullptr);
1441
1442 block_symbol bsym { sym, block };
1443 par_state->push_new<ada_var_value_operation> (bsym);
1444 }
1445
1446 /* A convenient wrapper around ada_get_field_index that takes
1447 a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
1448 of a NUL-terminated field name. */
1449
1450 static int
1451 ada_nget_field_index (const struct type *type, const char *field_name0,
1452 int field_name_len, int maybe_missing)
1453 {
1454 char *field_name = (char *) alloca ((field_name_len + 1) * sizeof (char));
1455
1456 strncpy (field_name, field_name0, field_name_len);
1457 field_name[field_name_len] = '\0';
1458 return ada_get_field_index (type, field_name, maybe_missing);
1459 }
1460
1461 /* If encoded_field_name is the name of a field inside symbol SYM,
1462 then return the type of that field. Otherwise, return NULL.
1463
1464 This function is actually recursive, so if ENCODED_FIELD_NAME
1465 doesn't match one of the fields of our symbol, then try to see
1466 if ENCODED_FIELD_NAME could not be a succession of field names
1467 (in other words, the user entered an expression of the form
1468 TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
1469 each field name sequentially to obtain the desired field type.
1470 In case of failure, we return NULL. */
1471
1472 static struct type *
1473 get_symbol_field_type (struct symbol *sym, const char *encoded_field_name)
1474 {
1475 const char *field_name = encoded_field_name;
1476 const char *subfield_name;
1477 struct type *type = SYMBOL_TYPE (sym);
1478 int fieldno;
1479
1480 if (type == NULL || field_name == NULL)
1481 return NULL;
1482 type = check_typedef (type);
1483
1484 while (field_name[0] != '\0')
1485 {
1486 field_name = chop_separator (field_name);
1487
1488 fieldno = ada_get_field_index (type, field_name, 1);
1489 if (fieldno >= 0)
1490 return type->field (fieldno).type ();
1491
1492 subfield_name = field_name;
1493 while (*subfield_name != '\0' && *subfield_name != '.'
1494 && (subfield_name[0] != '_' || subfield_name[1] != '_'))
1495 subfield_name += 1;
1496
1497 if (subfield_name[0] == '\0')
1498 return NULL;
1499
1500 fieldno = ada_nget_field_index (type, field_name,
1501 subfield_name - field_name, 1);
1502 if (fieldno < 0)
1503 return NULL;
1504
1505 type = type->field (fieldno).type ();
1506 field_name = subfield_name;
1507 }
1508
1509 return NULL;
1510 }
1511
1512 /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
1513 expression_block_context if NULL). If it denotes a type, return
1514 that type. Otherwise, write expression code to evaluate it as an
1515 object and return NULL. In this second case, NAME0 will, in general,
1516 have the form <name>(.<selector_name>)*, where <name> is an object
1517 or renaming encoded in the debugging data. Calls error if no
1518 prefix <name> matches a name in the debugging data (i.e., matches
1519 either a complete name or, as a wild-card match, the final
1520 identifier). */
1521
1522 static struct type*
1523 write_var_or_type (struct parser_state *par_state,
1524 const struct block *block, struct stoken name0)
1525 {
1526 int depth;
1527 char *encoded_name;
1528 int name_len;
1529
1530 if (block == NULL)
1531 block = par_state->expression_context_block;
1532
1533 std::string name_storage = ada_encode (name0.ptr);
1534 name_len = name_storage.size ();
1535 encoded_name = obstack_strndup (&temp_parse_space, name_storage.c_str (),
1536 name_len);
1537 for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
1538 {
1539 int tail_index;
1540
1541 tail_index = name_len;
1542 while (tail_index > 0)
1543 {
1544 struct symbol *type_sym;
1545 struct symbol *renaming_sym;
1546 const char* renaming;
1547 int renaming_len;
1548 const char* renaming_expr;
1549 int terminator = encoded_name[tail_index];
1550
1551 encoded_name[tail_index] = '\0';
1552 std::vector<struct block_symbol> syms
1553 = ada_lookup_symbol_list (encoded_name, block, VAR_DOMAIN);
1554 encoded_name[tail_index] = terminator;
1555
1556 type_sym = select_possible_type_sym (syms);
1557
1558 if (type_sym != NULL)
1559 renaming_sym = type_sym;
1560 else if (syms.size () == 1)
1561 renaming_sym = syms[0].symbol;
1562 else
1563 renaming_sym = NULL;
1564
1565 switch (ada_parse_renaming (renaming_sym, &renaming,
1566 &renaming_len, &renaming_expr))
1567 {
1568 case ADA_NOT_RENAMING:
1569 break;
1570 case ADA_PACKAGE_RENAMING:
1571 case ADA_EXCEPTION_RENAMING:
1572 case ADA_SUBPROGRAM_RENAMING:
1573 {
1574 int alloc_len = renaming_len + name_len - tail_index + 1;
1575 char *new_name
1576 = (char *) obstack_alloc (&temp_parse_space, alloc_len);
1577 strncpy (new_name, renaming, renaming_len);
1578 strcpy (new_name + renaming_len, encoded_name + tail_index);
1579 encoded_name = new_name;
1580 name_len = renaming_len + name_len - tail_index;
1581 goto TryAfterRenaming;
1582 }
1583 case ADA_OBJECT_RENAMING:
1584 write_object_renaming (par_state, block, renaming, renaming_len,
1585 renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
1586 write_selectors (par_state, encoded_name + tail_index);
1587 return NULL;
1588 default:
1589 internal_error (__FILE__, __LINE__,
1590 _("impossible value from ada_parse_renaming"));
1591 }
1592
1593 if (type_sym != NULL)
1594 {
1595 struct type *field_type;
1596
1597 if (tail_index == name_len)
1598 return SYMBOL_TYPE (type_sym);
1599
1600 /* We have some extraneous characters after the type name.
1601 If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
1602 then try to get the type of FIELDN. */
1603 field_type
1604 = get_symbol_field_type (type_sym, encoded_name + tail_index);
1605 if (field_type != NULL)
1606 return field_type;
1607 else
1608 error (_("Invalid attempt to select from type: \"%s\"."),
1609 name0.ptr);
1610 }
1611 else if (tail_index == name_len && syms.empty ())
1612 {
1613 struct type *type = find_primitive_type (par_state,
1614 encoded_name);
1615
1616 if (type != NULL)
1617 return type;
1618 }
1619
1620 if (syms.size () == 1)
1621 {
1622 write_var_from_sym (par_state, syms[0]);
1623 write_selectors (par_state, encoded_name + tail_index);
1624 return NULL;
1625 }
1626 else if (syms.empty ())
1627 {
1628 struct bound_minimal_symbol msym
1629 = ada_lookup_simple_minsym (encoded_name);
1630 if (msym.minsym != NULL)
1631 {
1632 par_state->push_new<ada_var_msym_value_operation> (msym);
1633 /* Maybe cause error here rather than later? FIXME? */
1634 write_selectors (par_state, encoded_name + tail_index);
1635 return NULL;
1636 }
1637
1638 if (tail_index == name_len
1639 && strncmp (encoded_name, "standard__",
1640 sizeof ("standard__") - 1) == 0)
1641 error (_("No definition of \"%s\" found."), name0.ptr);
1642
1643 tail_index = chop_selector (encoded_name, tail_index);
1644 }
1645 else
1646 {
1647 write_ambiguous_var (par_state, block, encoded_name,
1648 tail_index);
1649 write_selectors (par_state, encoded_name + tail_index);
1650 return NULL;
1651 }
1652 }
1653
1654 if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
1655 error (_("No symbol table is loaded. Use the \"file\" command."));
1656 if (block == par_state->expression_context_block)
1657 error (_("No definition of \"%s\" in current context."), name0.ptr);
1658 else
1659 error (_("No definition of \"%s\" in specified context."), name0.ptr);
1660
1661 TryAfterRenaming: ;
1662 }
1663
1664 error (_("Could not find renamed symbol \"%s\""), name0.ptr);
1665
1666 }
1667
1668 /* Write a left side of a component association (e.g., NAME in NAME =>
1669 exp). If NAME has the form of a selected component, write it as an
1670 ordinary expression. If it is a simple variable that unambiguously
1671 corresponds to exactly one symbol that does not denote a type or an
1672 object renaming, also write it normally as an OP_VAR_VALUE.
1673 Otherwise, write it as an OP_NAME.
1674
1675 Unfortunately, we don't know at this point whether NAME is supposed
1676 to denote a record component name or the value of an array index.
1677 Therefore, it is not appropriate to disambiguate an ambiguous name
1678 as we normally would, nor to replace a renaming with its referent.
1679 As a result, in the (one hopes) rare case that one writes an
1680 aggregate such as (R => 42) where R renames an object or is an
1681 ambiguous name, one must write instead ((R) => 42). */
1682
1683 static void
1684 write_name_assoc (struct parser_state *par_state, struct stoken name)
1685 {
1686 if (strchr (name.ptr, '.') == NULL)
1687 {
1688 std::vector<struct block_symbol> syms
1689 = ada_lookup_symbol_list (name.ptr,
1690 par_state->expression_context_block,
1691 VAR_DOMAIN);
1692
1693 if (syms.size () != 1 || SYMBOL_CLASS (syms[0].symbol) == LOC_TYPEDEF)
1694 pstate->push_new<ada_string_operation> (copy_name (name));
1695 else
1696 write_var_from_sym (par_state, syms[0]);
1697 }
1698 else
1699 if (write_var_or_type (par_state, NULL, name) != NULL)
1700 error (_("Invalid use of type."));
1701
1702 push_association<ada_name_association> (ada_pop ());
1703 }
1704
1705 static struct type *
1706 type_int (struct parser_state *par_state)
1707 {
1708 return parse_type (par_state)->builtin_int;
1709 }
1710
1711 static struct type *
1712 type_long (struct parser_state *par_state)
1713 {
1714 return parse_type (par_state)->builtin_long;
1715 }
1716
1717 static struct type *
1718 type_long_long (struct parser_state *par_state)
1719 {
1720 return parse_type (par_state)->builtin_long_long;
1721 }
1722
1723 static struct type *
1724 type_long_double (struct parser_state *par_state)
1725 {
1726 return parse_type (par_state)->builtin_long_double;
1727 }
1728
1729 static struct type *
1730 type_char (struct parser_state *par_state)
1731 {
1732 return language_string_char_type (par_state->language (),
1733 par_state->gdbarch ());
1734 }
1735
1736 static struct type *
1737 type_boolean (struct parser_state *par_state)
1738 {
1739 return parse_type (par_state)->builtin_bool;
1740 }
1741
1742 static struct type *
1743 type_system_address (struct parser_state *par_state)
1744 {
1745 struct type *type
1746 = language_lookup_primitive_type (par_state->language (),
1747 par_state->gdbarch (),
1748 "system__address");
1749 return type != NULL ? type : parse_type (par_state)->builtin_data_ptr;
1750 }
1751
1752 void _initialize_ada_exp ();
1753 void
1754 _initialize_ada_exp ()
1755 {
1756 obstack_init (&temp_parse_space);
1757 }