1 /* YACC parser for Ada expressions, for GDB.
2 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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.
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. */
40 #include "expression.h"
42 #include "parser-defs.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 */
52 #define parse_type(ps) builtin_type (ps->gdbarch ())
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
56 #define GDB_YY_REMAP_PREFIX ada_
61 struct minimal_symbol *msym;
62 const struct block *block;
66 /* The state of the parser, used internally when we are parsing the
69 static struct parser_state *pstate = NULL;
71 /* If expression is in the context of TYPE'(...), then TYPE, else
73 static struct type *type_qualifier;
77 static int yylex (void);
79 static void yyerror (const char *);
81 static void write_int (struct parser_state *, LONGEST, struct type *);
83 static void write_object_renaming (struct parser_state *,
84 const struct block *, const char *, int,
87 static struct type* write_var_or_type (struct parser_state *,
88 const struct block *, struct stoken);
90 static void write_name_assoc (struct parser_state *, struct stoken);
92 static const struct block *block_lookup (const struct block *, const char *);
94 static LONGEST convert_char_literal (struct type *, LONGEST);
96 static void write_ambiguous_var (struct parser_state *,
97 const struct block *, char *, int);
99 static struct type *type_int (struct parser_state *);
101 static struct type *type_long (struct parser_state *);
103 static struct type *type_long_long (struct parser_state *);
105 static struct type *type_long_double (struct parser_state *);
107 static struct type *type_char (struct parser_state *);
109 static struct type *type_boolean (struct parser_state *);
111 static struct type *type_system_address (struct parser_state *);
113 using namespace expr;
115 /* Handle Ada type resolution for OP. DEPROCEDURE_P and CONTEXT_TYPE
116 are passed to the resolve method, if called. */
118 resolve (operation_up &&op, bool deprocedure_p, struct type *context_type)
120 operation_up result = std::move (op);
121 ada_resolvable *res = dynamic_cast<ada_resolvable *> (result.get ());
123 && res->resolve (pstate->expout.get (),
125 pstate->parse_completion,
126 pstate->block_tracker,
129 = make_operation<ada_funcall_operation> (std::move (result),
130 std::vector<operation_up> ());
135 /* Like parser_state::pop, but handles Ada type resolution.
136 DEPROCEDURE_P and CONTEXT_TYPE are passed to the resolve method, if
139 ada_pop (bool deprocedure_p = true, struct type *context_type = nullptr)
141 /* Of course it's ok to call parser_state::pop here... */
142 return resolve (pstate->pop (), deprocedure_p, context_type);
145 /* Like parser_state::wrap, but use ada_pop to pop the value. */
150 operation_up arg = ada_pop ();
151 pstate->push_new<T> (std::move (arg));
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
158 ada_addrof (struct type *type = nullptr)
160 operation_up arg = ada_pop (false);
161 operation_up addr = make_operation<unop_addr_operation> (std::move (arg));
163 = make_operation<ada_wrapped_operation> (std::move (addr));
165 wrapped = make_operation<unop_cast_operation> (std::move (wrapped), type);
166 pstate->push (std::move (wrapped));
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. */
174 maybe_overload (enum exp_opcode op, operation_up &lhs, operation_up &rhs)
176 struct value *args[2];
179 args[0] = lhs->evaluate (nullptr, pstate->expout.get (),
180 EVAL_AVOID_SIDE_EFFECTS);
185 args[1] = rhs->evaluate (nullptr, pstate->expout.get (),
186 EVAL_AVOID_SIDE_EFFECTS);
190 block_symbol fn = ada_find_operator_symbol (op, pstate->parse_completion,
192 if (fn.symbol == nullptr)
195 if (symbol_read_needs_frame (fn.symbol))
196 pstate->block_tracker->update (fn.block, INNERMOST_BLOCK_FOR_SYMBOLS);
198 = make_operation<ada_var_value_operation> (fn.symbol, fn.block);
200 std::vector<operation_up> argvec;
201 argvec.push_back (std::move (lhs));
203 argvec.push_back (std::move (rhs));
204 return make_operation<ada_funcall_operation> (std::move (callee),
208 /* Like parser_state::wrap, but use ada_pop to pop the value, and
209 handle unary overloading. */
212 ada_wrap_overload (enum exp_opcode op)
214 operation_up arg = ada_pop ();
217 operation_up call = maybe_overload (op, arg, empty);
219 call = make_operation<T> (std::move (arg));
220 pstate->push (std::move (call));
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
228 ada_un_wrap2 (enum exp_opcode op)
230 operation_up rhs = ada_pop ();
231 operation_up lhs = ada_pop ();
233 operation_up wrapped = maybe_overload (op, lhs, rhs);
234 if (wrapped == nullptr)
236 wrapped = make_operation<T> (std::move (lhs), std::move (rhs));
237 wrapped = make_operation<ada_wrapped_operation> (std::move (wrapped));
239 pstate->push (std::move (wrapped));
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
247 ada_wrap2 (enum exp_opcode op)
249 operation_up rhs = ada_pop ();
250 operation_up lhs = ada_pop ();
251 operation_up call = maybe_overload (op, lhs, rhs);
253 call = make_operation<T> (std::move (lhs), std::move (rhs));
254 pstate->push (std::move (call));
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
262 ada_wrap_op (enum exp_opcode op)
264 operation_up rhs = ada_pop ();
265 operation_up lhs = ada_pop ();
266 operation_up call = maybe_overload (op, lhs, rhs);
268 call = make_operation<T> (op, std::move (lhs), std::move (rhs));
269 pstate->push (std::move (call));
272 /* Pop three operands using ada_pop, then construct a new ternary
273 operation of type T and push it. */
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));
284 /* Pop NARGS operands, then a callee operand, and use these to
285 construct and push a new Ada function call operation. */
287 ada_funcall (int nargs)
289 /* We use the ordinary pop here, because we're going to do
290 resolution in a separate step, in order to handle array
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
296 operation_up callee = pstate->pop ();
298 ada_var_value_operation *vvo
299 = dynamic_cast<ada_var_value_operation *> (callee.get ());
301 struct type *callee_t = nullptr;
303 || SYMBOL_DOMAIN (vvo->get_symbol ()) != UNDEF_DOMAIN)
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);
312 for (int i = 0; i < nargs; ++i)
314 struct type *subtype = nullptr;
316 subtype = ada_index_type (callee_t, i + 1, "array type");
317 args[i] = resolve (std::move (args[i]), true, subtype);
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));
327 /* The components being constructed during this parse. */
328 static std::vector<ada_component_up> components;
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>
334 push_component (Arg... args)
336 components.emplace_back (new T (std::forward<Arg> (args)...));
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 *
346 ada_component *last = components.back ().get ();
347 ada_choices_component *result = dynamic_cast<ada_choices_component *> (last);
348 gdb_assert (result != nullptr);
352 /* Pop the most recent component from the global stack, and return
354 static ada_component_up
357 ada_component_up result = std::move (components.back ());
358 components.pop_back ();
362 /* Pop the N most recent components from the global stack, and return
364 static std::vector<ada_component_up>
365 pop_components (int n)
367 std::vector<ada_component_up> result (n);
368 for (int i = 1; i <= n; ++i)
369 result[n - i] = pop_component ();
373 /* The associations being constructed during this parse. */
374 static std::vector<ada_association_up> associations;
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>
380 push_association (Arg... args)
382 associations.emplace_back (new T (std::forward<Arg> (args)...));
385 /* Pop the most recent association from the global stack, and return
387 static ada_association_up
390 ada_association_up result = std::move (associations.back ());
391 associations.pop_back ();
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)
400 std::vector<ada_association_up> result (n);
401 for (int i = 1; i <= n; ++i)
402 result[n - i] = pop_association ();
421 const struct block *bval;
422 struct internalvar *ivar;
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
429 %token <typed_val> INT NULL_PTR CHARLIT
430 %token <typed_val_float> FLOAT
431 %token TRUEKEYWORD FALSEKEYWORD
433 %token <sval> STRING NAME DOT_ID
435 %type <lval> arglist tick_arglist
437 %type <tval> save_qualifier
441 /* Special type cases, put in to allow the parser to distinguish different
443 %token <sval> DOLLAR_VARIABLE
446 %left _AND_ OR XOR THEN ELSE
447 %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
451 %left '*' '/' MOD REM
452 %right STARSTAR ABS NOT
454 /* Artificial token to give NAME => ... and NAME | priority over reducing
455 NAME to <primary> and to give <primary>' priority over reducing <primary>
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
477 /* Expressions, including the sequencing operator. */
480 { ada_wrap2<comma_operation> (BINOP_COMMA); }
481 | primary ASSIGN exp /* Extension for convenience */
483 operation_up rhs = pstate->pop ();
484 operation_up lhs = ada_pop ();
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));
495 /* Expressions, not including the sequencing operator. */
496 primary : primary DOT_ALL
497 { ada_wrap<ada_unop_ind_operation> (); }
500 primary : primary DOT_ID
502 operation_up arg = ada_pop ();
503 pstate->push_new<ada_structop_operation>
504 (std::move (arg), copy_name ($2));
508 primary : primary '(' arglist ')'
509 { ada_funcall ($3); }
510 | var_or_type '(' arglist ')'
515 error (_("Invalid conversion"));
516 operation_up arg = ada_pop ();
517 pstate->push_new<unop_cast_operation>
518 (std::move (arg), $1);
525 primary : var_or_type '\'' save_qualifier { type_qualifier = $1; }
529 error (_("Type required for qualification"));
530 operation_up arg = ada_pop (true,
532 pstate->push_new<ada_qual_operation>
533 (std::move (arg), $1);
538 save_qualifier : { $$ = type_qualifier; }
542 primary '(' simple_exp DOTDOT simple_exp ')'
543 { ada_wrap3<ada_ternop_slice_operation> (); }
544 | var_or_type '(' simple_exp DOTDOT simple_exp ')'
546 ada_wrap3<ada_ternop_slice_operation> ();
548 error (_("Cannot slice a type"));
552 primary : '(' exp1 ')' { }
555 /* The following rule causes a conflict with the type conversion
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. */
563 primary : var_or_type %prec VAR
565 pstate->push_new<type_operation> ($1);
569 primary : DOLLAR_VARIABLE /* Various GDB extensions */
570 { pstate->push_dollar ($1); }
575 pstate->push_new<ada_aggregate_operation>
583 simple_exp : '-' simple_exp %prec UNARY
584 { ada_wrap_overload<ada_neg_operation> (UNOP_NEG); }
587 simple_exp : '+' simple_exp %prec UNARY
589 operation_up arg = ada_pop ();
592 /* We only need to handle the overloading
593 case here, not anything else. */
594 operation_up call = maybe_overload (UNOP_PLUS, arg,
597 pstate->push (std::move (call));
601 simple_exp : NOT simple_exp %prec UNARY
603 ada_wrap_overload<unary_logical_not_operation>
608 simple_exp : ABS simple_exp %prec UNARY
609 { ada_wrap_overload<ada_abs_operation> (UNOP_ABS); }
612 arglist : { $$ = 0; }
621 | arglist ',' NAME ARROW exp
625 primary : '{' var_or_type '}' primary %prec '.'
629 error (_("Type required within braces in coercion"));
630 operation_up arg = ada_pop ();
631 pstate->push_new<unop_memval_operation>
632 (std::move (arg), $2);
636 /* Binary operators in order of decreasing precedence. */
638 simple_exp : simple_exp STARSTAR simple_exp
639 { ada_wrap2<ada_binop_exp_operation> (BINOP_EXP); }
642 simple_exp : simple_exp '*' simple_exp
643 { ada_wrap2<ada_binop_mul_operation> (BINOP_MUL); }
646 simple_exp : simple_exp '/' simple_exp
647 { ada_wrap2<ada_binop_div_operation> (BINOP_DIV); }
650 simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
651 { ada_wrap2<ada_binop_rem_operation> (BINOP_REM); }
654 simple_exp : simple_exp MOD simple_exp
655 { ada_wrap2<ada_binop_mod_operation> (BINOP_MOD); }
658 simple_exp : simple_exp '@' simple_exp /* GDB extension */
659 { ada_wrap2<repeat_operation> (BINOP_REPEAT); }
662 simple_exp : simple_exp '+' simple_exp
663 { ada_wrap_op<ada_binop_addsub_operation> (BINOP_ADD); }
666 simple_exp : simple_exp '&' simple_exp
667 { ada_wrap2<concat_operation> (BINOP_CONCAT); }
670 simple_exp : simple_exp '-' simple_exp
671 { ada_wrap_op<ada_binop_addsub_operation> (BINOP_SUB); }
674 relation : simple_exp
677 relation : simple_exp '=' simple_exp
678 { ada_wrap_op<ada_binop_equal_operation> (BINOP_EQUAL); }
681 relation : simple_exp NOTEQUAL simple_exp
682 { ada_wrap_op<ada_binop_equal_operation> (BINOP_NOTEQUAL); }
685 relation : simple_exp LEQ simple_exp
686 { ada_un_wrap2<leq_operation> (BINOP_LEQ); }
689 relation : simple_exp IN simple_exp DOTDOT simple_exp
690 { ada_wrap3<ada_ternop_range_operation> (); }
691 | simple_exp IN primary TICK_RANGE tick_arglist
693 operation_up rhs = ada_pop ();
694 operation_up lhs = ada_pop ();
695 pstate->push_new<ada_binop_in_bounds_operation>
696 (std::move (lhs), std::move (rhs), $5);
698 | simple_exp IN var_or_type %prec TICK_ACCESS
701 error (_("Right operand of 'in' must be type"));
702 operation_up arg = ada_pop ();
703 pstate->push_new<ada_unop_range_operation>
704 (std::move (arg), $3);
706 | simple_exp NOT IN simple_exp DOTDOT simple_exp
707 { ada_wrap3<ada_ternop_range_operation> ();
708 ada_wrap<unary_logical_not_operation> (); }
709 | simple_exp NOT IN primary TICK_RANGE tick_arglist
711 operation_up rhs = ada_pop ();
712 operation_up lhs = ada_pop ();
713 pstate->push_new<ada_binop_in_bounds_operation>
714 (std::move (lhs), std::move (rhs), $6);
715 ada_wrap<unary_logical_not_operation> ();
717 | simple_exp NOT IN var_or_type %prec TICK_ACCESS
720 error (_("Right operand of 'in' must be type"));
721 operation_up arg = ada_pop ();
722 pstate->push_new<ada_unop_range_operation>
723 (std::move (arg), $4);
724 ada_wrap<unary_logical_not_operation> ();
728 relation : simple_exp GEQ simple_exp
729 { ada_un_wrap2<geq_operation> (BINOP_GEQ); }
732 relation : simple_exp '<' simple_exp
733 { ada_un_wrap2<less_operation> (BINOP_LESS); }
736 relation : simple_exp '>' simple_exp
737 { ada_un_wrap2<gtr_operation> (BINOP_GTR); }
749 relation _AND_ relation
750 { ada_wrap2<ada_bitwise_and_operation>
751 (BINOP_BITWISE_AND); }
752 | and_exp _AND_ relation
753 { ada_wrap2<ada_bitwise_and_operation>
754 (BINOP_BITWISE_AND); }
758 relation _AND_ THEN relation
759 { ada_wrap2<logical_and_operation>
760 (BINOP_LOGICAL_AND); }
761 | and_then_exp _AND_ THEN relation
762 { ada_wrap2<logical_and_operation>
763 (BINOP_LOGICAL_AND); }
768 { ada_wrap2<ada_bitwise_ior_operation>
769 (BINOP_BITWISE_IOR); }
771 { ada_wrap2<ada_bitwise_ior_operation>
772 (BINOP_BITWISE_IOR); }
776 relation OR ELSE relation
777 { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
778 | or_else_exp OR ELSE relation
779 { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
782 xor_exp : relation XOR relation
783 { ada_wrap2<ada_bitwise_xor_operation>
784 (BINOP_BITWISE_XOR); }
785 | xor_exp XOR relation
786 { ada_wrap2<ada_bitwise_xor_operation>
787 (BINOP_BITWISE_XOR); }
790 /* Primaries can denote types (OP_TYPE). In cases such as
791 primary TICK_ADDRESS, where a type would be invalid, it will be
792 caught when evaluate_subexp in ada-lang.c tries to evaluate the
793 primary, expecting a value. Precedence rules resolve the ambiguity
794 in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A
795 construct such as aType'access'access will again cause an error when
796 aType'access evaluates to a type that evaluate_subexp attempts to
798 primary : primary TICK_ACCESS
800 | primary TICK_ADDRESS
801 { ada_addrof (type_system_address (pstate)); }
802 | primary TICK_FIRST tick_arglist
804 operation_up arg = ada_pop ();
805 pstate->push_new<ada_unop_atr_operation>
806 (std::move (arg), OP_ATR_FIRST, $3);
808 | primary TICK_LAST tick_arglist
810 operation_up arg = ada_pop ();
811 pstate->push_new<ada_unop_atr_operation>
812 (std::move (arg), OP_ATR_LAST, $3);
814 | primary TICK_LENGTH tick_arglist
816 operation_up arg = ada_pop ();
817 pstate->push_new<ada_unop_atr_operation>
818 (std::move (arg), OP_ATR_LENGTH, $3);
821 { ada_wrap<ada_atr_size_operation> (); }
823 { ada_wrap<ada_atr_tag_operation> (); }
824 | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
825 { ada_wrap2<ada_binop_min_operation> (BINOP_MIN); }
826 | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
827 { ada_wrap2<ada_binop_max_operation> (BINOP_MAX); }
828 | opt_type_prefix TICK_POS '(' exp ')'
829 { ada_wrap<ada_pos_operation> (); }
830 | type_prefix TICK_VAL '(' exp ')'
832 operation_up arg = ada_pop ();
833 pstate->push_new<ada_atr_val_operation>
834 ($1, std::move (arg));
836 | type_prefix TICK_MODULUS
838 struct type *type_arg = check_typedef ($1);
839 if (!ada_is_modular_type (type_arg))
840 error (_("'modulus must be applied to modular type"));
841 write_int (pstate, ada_modulus (type_arg),
842 TYPE_TARGET_TYPE (type_arg));
846 tick_arglist : %prec '('
856 error (_("Prefix must be type"));
865 { $$ = parse_type (pstate)->builtin_void; }
870 { write_int (pstate, (LONGEST) $1.val, $1.type); }
875 convert_char_literal (type_qualifier, $1.val),
876 (type_qualifier == NULL)
877 ? $1.type : type_qualifier);
884 std::copy (std::begin ($1.val), std::end ($1.val),
886 pstate->push_new<float_const_operation>
888 ada_wrap<ada_wrapped_operation> ();
893 { write_int (pstate, 0, type_int (pstate)); }
898 pstate->push_new<ada_string_operation>
903 primary : TRUEKEYWORD
904 { write_int (pstate, 1, type_boolean (pstate)); }
906 { write_int (pstate, 0, type_boolean (pstate)); }
910 { error (_("NEW not implemented.")); }
913 var_or_type: NAME %prec VAR
914 { $$ = write_var_or_type (pstate, NULL, $1); }
915 | block NAME %prec VAR
916 { $$ = write_var_or_type (pstate, $1, $2); }
919 $$ = write_var_or_type (pstate, NULL, $1);
923 $$ = lookup_pointer_type ($$);
925 | block NAME TICK_ACCESS
927 $$ = write_var_or_type (pstate, $1, $2);
931 $$ = lookup_pointer_type ($$);
936 block : NAME COLONCOLON
937 { $$ = block_lookup (NULL, $1.ptr); }
938 | block NAME COLONCOLON
939 { $$ = block_lookup ($1, $2.ptr); }
943 '(' aggregate_component_list ')'
945 std::vector<ada_component_up> components
946 = pop_components ($2);
948 push_component<ada_aggregate_component>
949 (std::move (components));
953 aggregate_component_list :
954 component_groups { $$ = $1; }
955 | positional_list exp
957 push_component<ada_positional_component>
961 | positional_list component_groups
968 push_component<ada_positional_component>
972 | positional_list exp ','
974 push_component<ada_positional_component>
982 | component_group { $$ = 1; }
983 | component_group ',' component_groups
987 others : OTHERS ARROW exp
989 push_component<ada_others_component> (ada_pop ());
994 component_associations
996 ada_choices_component *choices = choice_component ();
997 choices->set_associations (pop_associations ($1));
1001 /* We use this somewhat obscure definition in order to handle NAME => and
1002 NAME | differently from exp => and exp |. ARROW and '|' have a precedence
1003 above that of the reduction of NAME to var_or_type. By delaying
1004 decisions until after the => or '|', we convert the ambiguity to a
1005 resolved shift/reduce conflict. */
1006 component_associations :
1009 push_component<ada_choices_component> (ada_pop ());
1010 write_name_assoc (pstate, $1);
1013 | simple_exp ARROW exp
1015 push_component<ada_choices_component> (ada_pop ());
1016 push_association<ada_name_association> (ada_pop ());
1019 | simple_exp DOTDOT simple_exp ARROW exp
1021 push_component<ada_choices_component> (ada_pop ());
1022 operation_up rhs = ada_pop ();
1023 operation_up lhs = ada_pop ();
1024 push_association<ada_discrete_range_association>
1025 (std::move (lhs), std::move (rhs));
1028 | NAME '|' component_associations
1030 write_name_assoc (pstate, $1);
1033 | simple_exp '|' component_associations
1035 push_association<ada_name_association> (ada_pop ());
1038 | simple_exp DOTDOT simple_exp '|' component_associations
1041 operation_up rhs = ada_pop ();
1042 operation_up lhs = ada_pop ();
1043 push_association<ada_discrete_range_association>
1044 (std::move (lhs), std::move (rhs));
1049 /* Some extensions borrowed from C, for the benefit of those who find they
1050 can't get used to Ada notation in GDB. */
1052 primary : '*' primary %prec '.'
1053 { ada_wrap<ada_unop_ind_operation> (); }
1054 | '&' primary %prec '.'
1056 | primary '[' exp ']'
1058 ada_wrap2<subscript_operation> (BINOP_SUBSCRIPT);
1059 ada_wrap<ada_wrapped_operation> ();
1065 /* yylex defined in ada-lex.c: Reads one token, getting characters */
1066 /* through lexptr. */
1068 /* Remap normal flex interface names (yylex) as well as gratuitiously */
1069 /* global symbol names, so we can have multiple flex-generated parsers */
1072 /* (See note above on previous definitions for YACC.) */
1074 #define yy_create_buffer ada_yy_create_buffer
1075 #define yy_delete_buffer ada_yy_delete_buffer
1076 #define yy_init_buffer ada_yy_init_buffer
1077 #define yy_load_buffer_state ada_yy_load_buffer_state
1078 #define yy_switch_to_buffer ada_yy_switch_to_buffer
1079 #define yyrestart ada_yyrestart
1080 #define yytext ada_yytext
1082 static struct obstack temp_parse_space;
1084 /* The following kludge was found necessary to prevent conflicts between */
1085 /* defs.h and non-standard stdlib.h files. */
1086 #define qsort __qsort__dummy
1087 #include "ada-lex.c"
1090 ada_parse (struct parser_state *par_state)
1092 /* Setting up the parser state. */
1093 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1094 gdb_assert (par_state != NULL);
1097 lexer_init (yyin); /* (Re-)initialize lexer. */
1098 type_qualifier = NULL;
1099 obstack_free (&temp_parse_space, NULL);
1100 obstack_init (&temp_parse_space);
1101 components.clear ();
1102 associations.clear ();
1104 int result = yyparse ();
1107 struct type *context_type = nullptr;
1108 if (par_state->void_context_p)
1109 context_type = parse_type (par_state)->builtin_void;
1110 pstate->set_operation (ada_pop (true, context_type));
1116 yyerror (const char *msg)
1118 error (_("Error in expression, near `%s'."), pstate->lexptr);
1121 /* Emit expression to access an instance of SYM, in block BLOCK (if
1125 write_var_from_sym (struct parser_state *par_state,
1126 const struct block *block,
1129 if (symbol_read_needs_frame (sym))
1130 par_state->block_tracker->update (block, INNERMOST_BLOCK_FOR_SYMBOLS);
1132 par_state->push_new<ada_var_value_operation> (sym, block);
1135 /* Write integer or boolean constant ARG of type TYPE. */
1138 write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
1140 pstate->push_new<long_const_operation> (type, arg);
1141 ada_wrap<ada_wrapped_operation> ();
1144 /* Emit expression corresponding to the renamed object named
1145 * designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
1146 * context of ORIG_LEFT_CONTEXT, to which is applied the operations
1147 * encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of
1148 * cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it
1149 * defaults to the currently selected block. ORIG_SYMBOL is the
1150 * symbol that originally encoded the renaming. It is needed only
1151 * because its prefix also qualifies any index variables used to index
1152 * or slice an array. It should not be necessary once we go to the
1153 * new encoding entirely (FIXME pnh 7/20/2007). */
1156 write_object_renaming (struct parser_state *par_state,
1157 const struct block *orig_left_context,
1158 const char *renamed_entity, int renamed_entity_len,
1159 const char *renaming_expr, int max_depth)
1162 enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
1163 struct block_symbol sym_info;
1166 error (_("Could not find renamed symbol"));
1168 if (orig_left_context == NULL)
1169 orig_left_context = get_selected_block (NULL);
1171 name = obstack_strndup (&temp_parse_space, renamed_entity,
1172 renamed_entity_len);
1173 ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
1174 if (sym_info.symbol == NULL)
1175 error (_("Could not find renamed variable: %s"), ada_decode (name).c_str ());
1176 else if (SYMBOL_CLASS (sym_info.symbol) == LOC_TYPEDEF)
1177 /* We have a renaming of an old-style renaming symbol. Don't
1178 trust the block information. */
1179 sym_info.block = orig_left_context;
1182 const char *inner_renamed_entity;
1183 int inner_renamed_entity_len;
1184 const char *inner_renaming_expr;
1186 switch (ada_parse_renaming (sym_info.symbol, &inner_renamed_entity,
1187 &inner_renamed_entity_len,
1188 &inner_renaming_expr))
1190 case ADA_NOT_RENAMING:
1191 write_var_from_sym (par_state, sym_info.block, sym_info.symbol);
1193 case ADA_OBJECT_RENAMING:
1194 write_object_renaming (par_state, sym_info.block,
1195 inner_renamed_entity, inner_renamed_entity_len,
1196 inner_renaming_expr, max_depth - 1);
1203 slice_state = SIMPLE_INDEX;
1204 while (*renaming_expr == 'X')
1208 switch (*renaming_expr) {
1211 ada_wrap<ada_unop_ind_operation> ();
1214 slice_state = LOWER_BOUND;
1218 if (isdigit (*renaming_expr))
1221 long val = strtol (renaming_expr, &next, 10);
1222 if (next == renaming_expr)
1224 renaming_expr = next;
1225 write_int (par_state, val, type_int (par_state));
1231 struct block_symbol index_sym_info;
1233 end = strchr (renaming_expr, 'X');
1235 end = renaming_expr + strlen (renaming_expr);
1237 index_name = obstack_strndup (&temp_parse_space, renaming_expr,
1238 end - renaming_expr);
1239 renaming_expr = end;
1241 ada_lookup_encoded_symbol (index_name, orig_left_context,
1242 VAR_DOMAIN, &index_sym_info);
1243 if (index_sym_info.symbol == NULL)
1244 error (_("Could not find %s"), index_name);
1245 else if (SYMBOL_CLASS (index_sym_info.symbol) == LOC_TYPEDEF)
1246 /* Index is an old-style renaming symbol. */
1247 index_sym_info.block = orig_left_context;
1248 write_var_from_sym (par_state, index_sym_info.block,
1249 index_sym_info.symbol);
1251 if (slice_state == SIMPLE_INDEX)
1253 else if (slice_state == LOWER_BOUND)
1254 slice_state = UPPER_BOUND;
1255 else if (slice_state == UPPER_BOUND)
1257 ada_wrap3<ada_ternop_slice_operation> ();
1258 slice_state = SIMPLE_INDEX;
1268 if (slice_state != SIMPLE_INDEX)
1270 end = strchr (renaming_expr, 'X');
1272 end = renaming_expr + strlen (renaming_expr);
1274 operation_up arg = ada_pop ();
1275 pstate->push_new<ada_structop_operation>
1276 (std::move (arg), std::string (renaming_expr,
1277 end - renaming_expr));
1278 renaming_expr = end;
1286 if (slice_state == SIMPLE_INDEX)
1290 error (_("Internal error in encoding of renaming declaration"));
1293 static const struct block*
1294 block_lookup (const struct block *context, const char *raw_name)
1297 struct symtab *symtab;
1298 const struct block *result = NULL;
1300 std::string name_storage;
1301 if (raw_name[0] == '\'')
1308 name_storage = ada_encode (raw_name);
1309 name = name_storage.c_str ();
1312 std::vector<struct block_symbol> syms
1313 = ada_lookup_symbol_list (name, context, VAR_DOMAIN);
1316 && (syms.empty () || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK))
1317 symtab = lookup_symtab (name);
1322 result = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK);
1323 else if (syms.empty () || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK)
1325 if (context == NULL)
1326 error (_("No file or function \"%s\"."), raw_name);
1328 error (_("No function \"%s\" in specified context."), raw_name);
1332 if (syms.size () > 1)
1333 warning (_("Function name \"%s\" ambiguous here"), raw_name);
1334 result = SYMBOL_BLOCK_VALUE (syms[0].symbol);
1340 static struct symbol*
1341 select_possible_type_sym (const std::vector<struct block_symbol> &syms)
1344 int preferred_index;
1345 struct type *preferred_type;
1347 preferred_index = -1; preferred_type = NULL;
1348 for (i = 0; i < syms.size (); i += 1)
1349 switch (SYMBOL_CLASS (syms[i].symbol))
1352 if (ada_prefer_type (SYMBOL_TYPE (syms[i].symbol), preferred_type))
1354 preferred_index = i;
1355 preferred_type = SYMBOL_TYPE (syms[i].symbol);
1361 case LOC_REGPARM_ADDR:
1368 if (preferred_type == NULL)
1370 return syms[preferred_index].symbol;
1374 find_primitive_type (struct parser_state *par_state, const char *name)
1377 type = language_lookup_primitive_type (par_state->language (),
1378 par_state->gdbarch (),
1380 if (type == NULL && strcmp ("system__address", name) == 0)
1381 type = type_system_address (par_state);
1385 /* Check to see if we have a regular definition of this
1386 type that just didn't happen to have been read yet. */
1388 char *expanded_name =
1389 (char *) alloca (strlen (name) + sizeof ("standard__"));
1390 strcpy (expanded_name, "standard__");
1391 strcat (expanded_name, name);
1392 sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN).symbol;
1393 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1394 type = SYMBOL_TYPE (sym);
1401 chop_selector (char *name, int end)
1404 for (i = end - 1; i > 0; i -= 1)
1405 if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
1410 /* If NAME is a string beginning with a separator (either '__', or
1411 '.'), chop this separator and return the result; else, return
1415 chop_separator (char *name)
1420 if (name[0] == '_' && name[1] == '_')
1426 /* Given that SELS is a string of the form (<sep><identifier>)*, where
1427 <sep> is '__' or '.', write the indicated sequence of
1428 STRUCTOP_STRUCT expression operators. */
1430 write_selectors (struct parser_state *par_state, char *sels)
1432 while (*sels != '\0')
1434 char *p = chop_separator (sels);
1436 while (*sels != '\0' && *sels != '.'
1437 && (sels[0] != '_' || sels[1] != '_'))
1439 operation_up arg = ada_pop ();
1440 pstate->push_new<ada_structop_operation>
1441 (std::move (arg), std::string (p, sels - p));
1445 /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
1446 NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes
1447 a temporary symbol that is valid until the next call to ada_parse.
1450 write_ambiguous_var (struct parser_state *par_state,
1451 const struct block *block, char *name, int len)
1453 struct symbol *sym = new (&temp_parse_space) symbol ();
1455 SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
1456 sym->set_linkage_name (obstack_strndup (&temp_parse_space, name, len));
1457 sym->set_language (language_ada, nullptr);
1459 par_state->push_new<ada_var_value_operation> (sym, block);
1462 /* A convenient wrapper around ada_get_field_index that takes
1463 a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
1464 of a NUL-terminated field name. */
1467 ada_nget_field_index (const struct type *type, const char *field_name0,
1468 int field_name_len, int maybe_missing)
1470 char *field_name = (char *) alloca ((field_name_len + 1) * sizeof (char));
1472 strncpy (field_name, field_name0, field_name_len);
1473 field_name[field_name_len] = '\0';
1474 return ada_get_field_index (type, field_name, maybe_missing);
1477 /* If encoded_field_name is the name of a field inside symbol SYM,
1478 then return the type of that field. Otherwise, return NULL.
1480 This function is actually recursive, so if ENCODED_FIELD_NAME
1481 doesn't match one of the fields of our symbol, then try to see
1482 if ENCODED_FIELD_NAME could not be a succession of field names
1483 (in other words, the user entered an expression of the form
1484 TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
1485 each field name sequentially to obtain the desired field type.
1486 In case of failure, we return NULL. */
1488 static struct type *
1489 get_symbol_field_type (struct symbol *sym, char *encoded_field_name)
1491 char *field_name = encoded_field_name;
1492 char *subfield_name;
1493 struct type *type = SYMBOL_TYPE (sym);
1496 if (type == NULL || field_name == NULL)
1498 type = check_typedef (type);
1500 while (field_name[0] != '\0')
1502 field_name = chop_separator (field_name);
1504 fieldno = ada_get_field_index (type, field_name, 1);
1506 return type->field (fieldno).type ();
1508 subfield_name = field_name;
1509 while (*subfield_name != '\0' && *subfield_name != '.'
1510 && (subfield_name[0] != '_' || subfield_name[1] != '_'))
1513 if (subfield_name[0] == '\0')
1516 fieldno = ada_nget_field_index (type, field_name,
1517 subfield_name - field_name, 1);
1521 type = type->field (fieldno).type ();
1522 field_name = subfield_name;
1528 /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
1529 expression_block_context if NULL). If it denotes a type, return
1530 that type. Otherwise, write expression code to evaluate it as an
1531 object and return NULL. In this second case, NAME0 will, in general,
1532 have the form <name>(.<selector_name>)*, where <name> is an object
1533 or renaming encoded in the debugging data. Calls error if no
1534 prefix <name> matches a name in the debugging data (i.e., matches
1535 either a complete name or, as a wild-card match, the final
1539 write_var_or_type (struct parser_state *par_state,
1540 const struct block *block, struct stoken name0)
1547 block = par_state->expression_context_block;
1549 std::string name_storage = ada_encode (name0.ptr);
1550 name_len = name_storage.size ();
1551 encoded_name = obstack_strndup (&temp_parse_space, name_storage.c_str (),
1553 for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
1557 tail_index = name_len;
1558 while (tail_index > 0)
1560 struct symbol *type_sym;
1561 struct symbol *renaming_sym;
1562 const char* renaming;
1564 const char* renaming_expr;
1565 int terminator = encoded_name[tail_index];
1567 encoded_name[tail_index] = '\0';
1568 std::vector<struct block_symbol> syms
1569 = ada_lookup_symbol_list (encoded_name, block, VAR_DOMAIN);
1570 encoded_name[tail_index] = terminator;
1572 type_sym = select_possible_type_sym (syms);
1574 if (type_sym != NULL)
1575 renaming_sym = type_sym;
1576 else if (syms.size () == 1)
1577 renaming_sym = syms[0].symbol;
1579 renaming_sym = NULL;
1581 switch (ada_parse_renaming (renaming_sym, &renaming,
1582 &renaming_len, &renaming_expr))
1584 case ADA_NOT_RENAMING:
1586 case ADA_PACKAGE_RENAMING:
1587 case ADA_EXCEPTION_RENAMING:
1588 case ADA_SUBPROGRAM_RENAMING:
1590 int alloc_len = renaming_len + name_len - tail_index + 1;
1592 = (char *) obstack_alloc (&temp_parse_space, alloc_len);
1593 strncpy (new_name, renaming, renaming_len);
1594 strcpy (new_name + renaming_len, encoded_name + tail_index);
1595 encoded_name = new_name;
1596 name_len = renaming_len + name_len - tail_index;
1597 goto TryAfterRenaming;
1599 case ADA_OBJECT_RENAMING:
1600 write_object_renaming (par_state, block, renaming, renaming_len,
1601 renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
1602 write_selectors (par_state, encoded_name + tail_index);
1605 internal_error (__FILE__, __LINE__,
1606 _("impossible value from ada_parse_renaming"));
1609 if (type_sym != NULL)
1611 struct type *field_type;
1613 if (tail_index == name_len)
1614 return SYMBOL_TYPE (type_sym);
1616 /* We have some extraneous characters after the type name.
1617 If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
1618 then try to get the type of FIELDN. */
1620 = get_symbol_field_type (type_sym, encoded_name + tail_index);
1621 if (field_type != NULL)
1624 error (_("Invalid attempt to select from type: \"%s\"."),
1627 else if (tail_index == name_len && syms.empty ())
1629 struct type *type = find_primitive_type (par_state,
1636 if (syms.size () == 1)
1638 write_var_from_sym (par_state, syms[0].block, syms[0].symbol);
1639 write_selectors (par_state, encoded_name + tail_index);
1642 else if (syms.empty ())
1644 struct bound_minimal_symbol msym
1645 = ada_lookup_simple_minsym (encoded_name);
1646 if (msym.minsym != NULL)
1648 par_state->push_new<ada_var_msym_value_operation> (msym);
1649 /* Maybe cause error here rather than later? FIXME? */
1650 write_selectors (par_state, encoded_name + tail_index);
1654 if (tail_index == name_len
1655 && strncmp (encoded_name, "standard__",
1656 sizeof ("standard__") - 1) == 0)
1657 error (_("No definition of \"%s\" found."), name0.ptr);
1659 tail_index = chop_selector (encoded_name, tail_index);
1663 write_ambiguous_var (par_state, block, encoded_name,
1665 write_selectors (par_state, encoded_name + tail_index);
1670 if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
1671 error (_("No symbol table is loaded. Use the \"file\" command."));
1672 if (block == par_state->expression_context_block)
1673 error (_("No definition of \"%s\" in current context."), name0.ptr);
1675 error (_("No definition of \"%s\" in specified context."), name0.ptr);
1680 error (_("Could not find renamed symbol \"%s\""), name0.ptr);
1684 /* Write a left side of a component association (e.g., NAME in NAME =>
1685 exp). If NAME has the form of a selected component, write it as an
1686 ordinary expression. If it is a simple variable that unambiguously
1687 corresponds to exactly one symbol that does not denote a type or an
1688 object renaming, also write it normally as an OP_VAR_VALUE.
1689 Otherwise, write it as an OP_NAME.
1691 Unfortunately, we don't know at this point whether NAME is supposed
1692 to denote a record component name or the value of an array index.
1693 Therefore, it is not appropriate to disambiguate an ambiguous name
1694 as we normally would, nor to replace a renaming with its referent.
1695 As a result, in the (one hopes) rare case that one writes an
1696 aggregate such as (R => 42) where R renames an object or is an
1697 ambiguous name, one must write instead ((R) => 42). */
1700 write_name_assoc (struct parser_state *par_state, struct stoken name)
1702 if (strchr (name.ptr, '.') == NULL)
1704 std::vector<struct block_symbol> syms
1705 = ada_lookup_symbol_list (name.ptr,
1706 par_state->expression_context_block,
1709 if (syms.size () != 1 || SYMBOL_CLASS (syms[0].symbol) == LOC_TYPEDEF)
1710 pstate->push_new<ada_string_operation> (copy_name (name));
1712 write_var_from_sym (par_state, syms[0].block, syms[0].symbol);
1715 if (write_var_or_type (par_state, NULL, name) != NULL)
1716 error (_("Invalid use of type."));
1718 push_association<ada_name_association> (ada_pop ());
1721 /* Convert the character literal whose ASCII value would be VAL to the
1722 appropriate value of type TYPE, if there is a translation.
1723 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
1724 the literal 'A' (VAL == 65), returns 0. */
1727 convert_char_literal (struct type *type, LONGEST val)
1734 type = check_typedef (type);
1735 if (type->code () != TYPE_CODE_ENUM)
1738 if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
1739 xsnprintf (name, sizeof (name), "Q%c", (int) val);
1741 xsnprintf (name, sizeof (name), "QU%02x", (int) val);
1742 size_t len = strlen (name);
1743 for (f = 0; f < type->num_fields (); f += 1)
1745 /* Check the suffix because an enum constant in a package will
1746 have a name like "pkg__QUxx". This is safe enough because we
1747 already have the correct type, and because mangling means
1748 there can't be clashes. */
1749 const char *ename = TYPE_FIELD_NAME (type, f);
1750 size_t elen = strlen (ename);
1752 if (elen >= len && strcmp (name, ename + elen - len) == 0)
1753 return TYPE_FIELD_ENUMVAL (type, f);
1758 static struct type *
1759 type_int (struct parser_state *par_state)
1761 return parse_type (par_state)->builtin_int;
1764 static struct type *
1765 type_long (struct parser_state *par_state)
1767 return parse_type (par_state)->builtin_long;
1770 static struct type *
1771 type_long_long (struct parser_state *par_state)
1773 return parse_type (par_state)->builtin_long_long;
1776 static struct type *
1777 type_long_double (struct parser_state *par_state)
1779 return parse_type (par_state)->builtin_long_double;
1782 static struct type *
1783 type_char (struct parser_state *par_state)
1785 return language_string_char_type (par_state->language (),
1786 par_state->gdbarch ());
1789 static struct type *
1790 type_boolean (struct parser_state *par_state)
1792 return parse_type (par_state)->builtin_bool;
1795 static struct type *
1796 type_system_address (struct parser_state *par_state)
1799 = language_lookup_primitive_type (par_state->language (),
1800 par_state->gdbarch (),
1802 return type != NULL ? type : parse_type (par_state)->builtin_data_ptr;
1805 void _initialize_ada_exp ();
1807 _initialize_ada_exp ()
1809 obstack_init (&temp_parse_space);