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