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