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