Automatic date update in version.in
[binutils-gdb.git] / gdb / ada-exp.y
blobf67c221d8733fd523294038e81b1c5c3b95adcfb
1 /* YACC parser for Ada expressions, for GDB.
2 Copyright (C) 1986-2022 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. */
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"
52 #define parse_type(ps) builtin_type (ps->gdbarch ())
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
55 etc). */
56 #define GDB_YY_REMAP_PREFIX ada_
57 #include "yy-remap.h"
59 struct name_info {
60 struct symbol *sym;
61 struct minimal_symbol *msym;
62 const struct block *block;
63 struct stoken stoken;
66 /* The state of the parser, used internally when we are parsing the
67 expression. */
69 static struct parser_state *pstate = NULL;
71 /* The original expression string. */
72 static const char *original_expr;
74 int yyparse (void);
76 static int yylex (void);
78 static void yyerror (const char *);
80 static void write_int (struct parser_state *, LONGEST, struct type *);
82 static void write_object_renaming (struct parser_state *,
83 const struct block *, const char *, int,
84 const char *, int);
86 static struct type* write_var_or_type (struct parser_state *,
87 const struct block *, struct stoken);
88 static struct type *write_var_or_type_completion (struct parser_state *,
89 const struct block *,
90 struct stoken);
92 static void write_name_assoc (struct parser_state *, struct stoken);
94 static const struct block *block_lookup (const struct block *, const char *);
96 static void write_ambiguous_var (struct parser_state *,
97 const struct block *, const 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_for_char (struct parser_state *, ULONGEST);
109 static struct type *type_boolean (struct parser_state *);
111 static struct type *type_system_address (struct parser_state *);
113 static std::string find_completion_bounds (struct parser_state *);
115 using namespace expr;
117 /* Handle Ada type resolution for OP. DEPROCEDURE_P and CONTEXT_TYPE
118 are passed to the resolve method, if called. */
119 static operation_up
120 resolve (operation_up &&op, bool deprocedure_p, struct type *context_type)
122 operation_up result = std::move (op);
123 ada_resolvable *res = dynamic_cast<ada_resolvable *> (result.get ());
124 if (res != nullptr)
125 return res->replace (std::move (result),
126 pstate->expout.get (),
127 deprocedure_p,
128 pstate->parse_completion,
129 pstate->block_tracker,
130 context_type);
131 return result;
134 /* Like parser_state::pop, but handles Ada type resolution.
135 DEPROCEDURE_P and CONTEXT_TYPE are passed to the resolve method, if
136 called. */
137 static operation_up
138 ada_pop (bool deprocedure_p = true, struct type *context_type = nullptr)
140 /* Of course it's ok to call parser_state::pop here... */
141 return resolve (pstate->pop (), deprocedure_p, context_type);
144 /* Like parser_state::wrap, but use ada_pop to pop the value. */
145 template<typename T>
146 void
147 ada_wrap ()
149 operation_up arg = ada_pop ();
150 pstate->push_new<T> (std::move (arg));
153 /* Create and push an address-of operation, as appropriate for Ada.
154 If TYPE is not NULL, the resulting operation will be wrapped in a
155 cast to TYPE. */
156 static void
157 ada_addrof (struct type *type = nullptr)
159 operation_up arg = ada_pop (false);
160 operation_up addr = make_operation<unop_addr_operation> (std::move (arg));
161 operation_up wrapped
162 = make_operation<ada_wrapped_operation> (std::move (addr));
163 if (type != nullptr)
164 wrapped = make_operation<unop_cast_operation> (std::move (wrapped), type);
165 pstate->push (std::move (wrapped));
168 /* Handle operator overloading. Either returns a function all
169 operation wrapping the arguments, or it returns null, leaving the
170 caller to construct the appropriate operation. If RHS is null, a
171 unary operator is assumed. */
172 static operation_up
173 maybe_overload (enum exp_opcode op, operation_up &lhs, operation_up &rhs)
175 struct value *args[2];
177 int nargs = 1;
178 args[0] = lhs->evaluate (nullptr, pstate->expout.get (),
179 EVAL_AVOID_SIDE_EFFECTS);
180 if (rhs == nullptr)
181 args[1] = nullptr;
182 else
184 args[1] = rhs->evaluate (nullptr, pstate->expout.get (),
185 EVAL_AVOID_SIDE_EFFECTS);
186 ++nargs;
189 block_symbol fn = ada_find_operator_symbol (op, pstate->parse_completion,
190 nargs, args);
191 if (fn.symbol == nullptr)
192 return {};
194 if (symbol_read_needs_frame (fn.symbol))
195 pstate->block_tracker->update (fn.block, INNERMOST_BLOCK_FOR_SYMBOLS);
196 operation_up callee = make_operation<ada_var_value_operation> (fn);
198 std::vector<operation_up> argvec;
199 argvec.push_back (std::move (lhs));
200 if (rhs != nullptr)
201 argvec.push_back (std::move (rhs));
202 return make_operation<ada_funcall_operation> (std::move (callee),
203 std::move (argvec));
206 /* Like parser_state::wrap, but use ada_pop to pop the value, and
207 handle unary overloading. */
208 template<typename T>
209 void
210 ada_wrap_overload (enum exp_opcode op)
212 operation_up arg = ada_pop ();
213 operation_up empty;
215 operation_up call = maybe_overload (op, arg, empty);
216 if (call == nullptr)
217 call = make_operation<T> (std::move (arg));
218 pstate->push (std::move (call));
221 /* A variant of parser_state::wrap2 that uses ada_pop to pop both
222 operands, and then pushes a new Ada-wrapped operation of the
223 template type T. */
224 template<typename T>
225 void
226 ada_un_wrap2 (enum exp_opcode op)
228 operation_up rhs = ada_pop ();
229 operation_up lhs = ada_pop ();
231 operation_up wrapped = maybe_overload (op, lhs, rhs);
232 if (wrapped == nullptr)
234 wrapped = make_operation<T> (std::move (lhs), std::move (rhs));
235 wrapped = make_operation<ada_wrapped_operation> (std::move (wrapped));
237 pstate->push (std::move (wrapped));
240 /* A variant of parser_state::wrap2 that uses ada_pop to pop both
241 operands. Unlike ada_un_wrap2, ada_wrapped_operation is not
242 used. */
243 template<typename T>
244 void
245 ada_wrap2 (enum exp_opcode op)
247 operation_up rhs = ada_pop ();
248 operation_up lhs = ada_pop ();
249 operation_up call = maybe_overload (op, lhs, rhs);
250 if (call == nullptr)
251 call = make_operation<T> (std::move (lhs), std::move (rhs));
252 pstate->push (std::move (call));
255 /* A variant of parser_state::wrap2 that uses ada_pop to pop both
256 operands. OP is also passed to the constructor of the new binary
257 operation. */
258 template<typename T>
259 void
260 ada_wrap_op (enum exp_opcode op)
262 operation_up rhs = ada_pop ();
263 operation_up lhs = ada_pop ();
264 operation_up call = maybe_overload (op, lhs, rhs);
265 if (call == nullptr)
266 call = make_operation<T> (op, std::move (lhs), std::move (rhs));
267 pstate->push (std::move (call));
270 /* Pop three operands using ada_pop, then construct a new ternary
271 operation of type T and push it. */
272 template<typename T>
273 void
274 ada_wrap3 ()
276 operation_up rhs = ada_pop ();
277 operation_up mid = ada_pop ();
278 operation_up lhs = ada_pop ();
279 pstate->push_new<T> (std::move (lhs), std::move (mid), std::move (rhs));
282 /* Pop NARGS operands, then a callee operand, and use these to
283 construct and push a new Ada function call operation. */
284 static void
285 ada_funcall (int nargs)
287 /* We use the ordinary pop here, because we're going to do
288 resolution in a separate step, in order to handle array
289 indices. */
290 std::vector<operation_up> args = pstate->pop_vector (nargs);
291 /* Call parser_state::pop here, because we don't want to
292 function-convert the callee slot of a call we're already
293 constructing. */
294 operation_up callee = pstate->pop ();
296 ada_var_value_operation *vvo
297 = dynamic_cast<ada_var_value_operation *> (callee.get ());
298 int array_arity = 0;
299 struct type *callee_t = nullptr;
300 if (vvo == nullptr
301 || vvo->get_symbol ()->domain () != UNDEF_DOMAIN)
303 struct value *callee_v = callee->evaluate (nullptr,
304 pstate->expout.get (),
305 EVAL_AVOID_SIDE_EFFECTS);
306 callee_t = ada_check_typedef (value_type (callee_v));
307 array_arity = ada_array_arity (callee_t);
310 for (int i = 0; i < nargs; ++i)
312 struct type *subtype = nullptr;
313 if (i < array_arity)
314 subtype = ada_index_type (callee_t, i + 1, "array type");
315 args[i] = resolve (std::move (args[i]), true, subtype);
318 std::unique_ptr<ada_funcall_operation> funcall
319 (new ada_funcall_operation (std::move (callee), std::move (args)));
320 funcall->resolve (pstate->expout.get (), true, pstate->parse_completion,
321 pstate->block_tracker, nullptr);
322 pstate->push (std::move (funcall));
325 /* The components being constructed during this parse. */
326 static std::vector<ada_component_up> components;
328 /* Create a new ada_component_up of the indicated type and arguments,
329 and push it on the global 'components' vector. */
330 template<typename T, typename... Arg>
331 void
332 push_component (Arg... args)
334 components.emplace_back (new T (std::forward<Arg> (args)...));
337 /* Examine the final element of the 'components' vector, and return it
338 as a pointer to an ada_choices_component. The caller is
339 responsible for ensuring that the final element is in fact an
340 ada_choices_component. */
341 static ada_choices_component *
342 choice_component ()
344 ada_component *last = components.back ().get ();
345 return gdb::checked_static_cast<ada_choices_component *> (last);
348 /* Pop the most recent component from the global stack, and return
349 it. */
350 static ada_component_up
351 pop_component ()
353 ada_component_up result = std::move (components.back ());
354 components.pop_back ();
355 return result;
358 /* Pop the N most recent components from the global stack, and return
359 them in a vector. */
360 static std::vector<ada_component_up>
361 pop_components (int n)
363 std::vector<ada_component_up> result (n);
364 for (int i = 1; i <= n; ++i)
365 result[n - i] = pop_component ();
366 return result;
369 /* The associations being constructed during this parse. */
370 static std::vector<ada_association_up> associations;
372 /* Create a new ada_association_up of the indicated type and
373 arguments, and push it on the global 'associations' vector. */
374 template<typename T, typename... Arg>
375 void
376 push_association (Arg... args)
378 associations.emplace_back (new T (std::forward<Arg> (args)...));
381 /* Pop the most recent association from the global stack, and return
382 it. */
383 static ada_association_up
384 pop_association ()
386 ada_association_up result = std::move (associations.back ());
387 associations.pop_back ();
388 return result;
391 /* Pop the N most recent associations from the global stack, and
392 return them in a vector. */
393 static std::vector<ada_association_up>
394 pop_associations (int n)
396 std::vector<ada_association_up> result (n);
397 for (int i = 1; i <= n; ++i)
398 result[n - i] = pop_association ();
399 return result;
402 /* Expression completer for attributes. */
403 struct ada_tick_completer : public expr_completion_base
405 explicit ada_tick_completer (std::string &&name)
406 : m_name (std::move (name))
410 bool complete (struct expression *exp,
411 completion_tracker &tracker) override;
413 private:
415 std::string m_name;
418 /* Make a new ada_tick_completer and wrap it in a unique pointer. */
419 static std::unique_ptr<expr_completion_base>
420 make_tick_completer (struct stoken tok)
422 return (std::unique_ptr<expr_completion_base>
423 (new ada_tick_completer (std::string (tok.ptr, tok.length))));
428 %union
430 LONGEST lval;
431 struct {
432 LONGEST val;
433 struct type *type;
434 } typed_val;
435 struct {
436 gdb_byte val[16];
437 struct type *type;
438 } typed_val_float;
439 struct type *tval;
440 struct stoken sval;
441 const struct block *bval;
442 struct internalvar *ivar;
445 %type <lval> positional_list component_groups component_associations
446 %type <lval> aggregate_component_list
447 %type <tval> var_or_type type_prefix opt_type_prefix
449 %token <typed_val> INT NULL_PTR CHARLIT
450 %token <typed_val_float> FLOAT
451 %token TRUEKEYWORD FALSEKEYWORD
452 %token COLONCOLON
453 %token <sval> STRING NAME DOT_ID TICK_COMPLETE DOT_COMPLETE NAME_COMPLETE
454 %type <bval> block
455 %type <lval> arglist tick_arglist
457 /* Special type cases, put in to allow the parser to distinguish different
458 legal basetypes. */
459 %token <sval> DOLLAR_VARIABLE
461 %nonassoc ASSIGN
462 %left _AND_ OR XOR THEN ELSE
463 %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
464 %left '@'
465 %left '+' '-' '&'
466 %left UNARY
467 %left '*' '/' MOD REM
468 %right STARSTAR ABS NOT
470 /* Artificial token to give NAME => ... and NAME | priority over reducing
471 NAME to <primary> and to give <primary>' priority over reducing <primary>
472 to <simple_exp>. */
473 %nonassoc VAR
475 %nonassoc ARROW '|'
477 %right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
478 %right TICK_MAX TICK_MIN TICK_MODULUS
479 %right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
480 %right TICK_COMPLETE
481 /* The following are right-associative only so that reductions at this
482 precedence have lower precedence than '.' and '('. The syntax still
483 forces a.b.c, e.g., to be LEFT-associated. */
484 %right '.' '(' '[' DOT_ID DOT_COMPLETE
486 %token NEW OTHERS
491 start : exp1
494 /* Expressions, including the sequencing operator. */
495 exp1 : exp
496 | exp1 ';' exp
497 { ada_wrap2<comma_operation> (BINOP_COMMA); }
498 | primary ASSIGN exp /* Extension for convenience */
500 operation_up rhs = pstate->pop ();
501 operation_up lhs = ada_pop ();
502 value *lhs_val
503 = lhs->evaluate (nullptr, pstate->expout.get (),
504 EVAL_AVOID_SIDE_EFFECTS);
505 rhs = resolve (std::move (rhs), true,
506 value_type (lhs_val));
507 pstate->push_new<ada_assign_operation>
508 (std::move (lhs), std::move (rhs));
512 /* Expressions, not including the sequencing operator. */
514 primary : primary DOT_ID
516 if (strcmp ($2.ptr, "all") == 0)
517 ada_wrap<ada_unop_ind_operation> ();
518 else
520 operation_up arg = ada_pop ();
521 pstate->push_new<ada_structop_operation>
522 (std::move (arg), copy_name ($2));
527 primary : primary DOT_COMPLETE
529 /* This is done even for ".all", because
530 that might be a prefix. */
531 operation_up arg = ada_pop ();
532 ada_structop_operation *str_op
533 = (new ada_structop_operation
534 (std::move (arg), copy_name ($2)));
535 str_op->set_prefix (find_completion_bounds (pstate));
536 pstate->push (operation_up (str_op));
537 pstate->mark_struct_expression (str_op);
541 primary : primary '(' arglist ')'
542 { ada_funcall ($3); }
543 | var_or_type '(' arglist ')'
545 if ($1 != NULL)
547 if ($3 != 1)
548 error (_("Invalid conversion"));
549 operation_up arg = ada_pop ();
550 pstate->push_new<unop_cast_operation>
551 (std::move (arg), $1);
553 else
554 ada_funcall ($3);
558 primary : var_or_type '\'' '(' exp ')'
560 if ($1 == NULL)
561 error (_("Type required for qualification"));
562 operation_up arg = ada_pop (true,
563 check_typedef ($1));
564 pstate->push_new<ada_qual_operation>
565 (std::move (arg), $1);
569 primary :
570 primary '(' simple_exp DOTDOT simple_exp ')'
571 { ada_wrap3<ada_ternop_slice_operation> (); }
572 | var_or_type '(' simple_exp DOTDOT simple_exp ')'
573 { if ($1 == NULL)
574 ada_wrap3<ada_ternop_slice_operation> ();
575 else
576 error (_("Cannot slice a type"));
580 primary : '(' exp1 ')' { }
583 /* The following rule causes a conflict with the type conversion
584 var_or_type (exp)
585 To get around it, we give '(' higher priority and add bridge rules for
586 var_or_type (exp, exp, ...)
587 var_or_type (exp .. exp)
588 We also have the action for var_or_type(exp) generate a function call
589 when the first symbol does not denote a type. */
591 primary : var_or_type %prec VAR
592 { if ($1 != NULL)
593 pstate->push_new<type_operation> ($1);
597 primary : DOLLAR_VARIABLE /* Various GDB extensions */
598 { pstate->push_dollar ($1); }
601 primary : aggregate
603 pstate->push_new<ada_aggregate_operation>
604 (pop_component ());
608 simple_exp : primary
611 simple_exp : '-' simple_exp %prec UNARY
612 { ada_wrap_overload<ada_neg_operation> (UNOP_NEG); }
615 simple_exp : '+' simple_exp %prec UNARY
617 operation_up arg = ada_pop ();
618 operation_up empty;
620 /* If an overloaded operator was found, use
621 it. Otherwise, unary + has no effect and
622 the argument can be pushed instead. */
623 operation_up call = maybe_overload (UNOP_PLUS, arg,
624 empty);
625 if (call != nullptr)
626 arg = std::move (call);
627 pstate->push (std::move (arg));
631 simple_exp : NOT simple_exp %prec UNARY
633 ada_wrap_overload<unary_logical_not_operation>
634 (UNOP_LOGICAL_NOT);
638 simple_exp : ABS simple_exp %prec UNARY
639 { ada_wrap_overload<ada_abs_operation> (UNOP_ABS); }
642 arglist : { $$ = 0; }
645 arglist : exp
646 { $$ = 1; }
647 | NAME ARROW exp
648 { $$ = 1; }
649 | arglist ',' exp
650 { $$ = $1 + 1; }
651 | arglist ',' NAME ARROW exp
652 { $$ = $1 + 1; }
655 primary : '{' var_or_type '}' primary %prec '.'
656 /* GDB extension */
658 if ($2 == NULL)
659 error (_("Type required within braces in coercion"));
660 operation_up arg = ada_pop ();
661 pstate->push_new<unop_memval_operation>
662 (std::move (arg), $2);
666 /* Binary operators in order of decreasing precedence. */
668 simple_exp : simple_exp STARSTAR simple_exp
669 { ada_wrap2<ada_binop_exp_operation> (BINOP_EXP); }
672 simple_exp : simple_exp '*' simple_exp
673 { ada_wrap2<ada_binop_mul_operation> (BINOP_MUL); }
676 simple_exp : simple_exp '/' simple_exp
677 { ada_wrap2<ada_binop_div_operation> (BINOP_DIV); }
680 simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
681 { ada_wrap2<ada_binop_rem_operation> (BINOP_REM); }
684 simple_exp : simple_exp MOD simple_exp
685 { ada_wrap2<ada_binop_mod_operation> (BINOP_MOD); }
688 simple_exp : simple_exp '@' simple_exp /* GDB extension */
689 { ada_wrap2<repeat_operation> (BINOP_REPEAT); }
692 simple_exp : simple_exp '+' simple_exp
693 { ada_wrap_op<ada_binop_addsub_operation> (BINOP_ADD); }
696 simple_exp : simple_exp '&' simple_exp
697 { ada_wrap2<ada_concat_operation> (BINOP_CONCAT); }
700 simple_exp : simple_exp '-' simple_exp
701 { ada_wrap_op<ada_binop_addsub_operation> (BINOP_SUB); }
704 relation : simple_exp
707 relation : simple_exp '=' simple_exp
708 { ada_wrap_op<ada_binop_equal_operation> (BINOP_EQUAL); }
711 relation : simple_exp NOTEQUAL simple_exp
712 { ada_wrap_op<ada_binop_equal_operation> (BINOP_NOTEQUAL); }
715 relation : simple_exp LEQ simple_exp
716 { ada_un_wrap2<leq_operation> (BINOP_LEQ); }
719 relation : simple_exp IN simple_exp DOTDOT simple_exp
720 { ada_wrap3<ada_ternop_range_operation> (); }
721 | simple_exp IN primary TICK_RANGE tick_arglist
723 operation_up rhs = ada_pop ();
724 operation_up lhs = ada_pop ();
725 pstate->push_new<ada_binop_in_bounds_operation>
726 (std::move (lhs), std::move (rhs), $5);
728 | simple_exp IN var_or_type %prec TICK_ACCESS
730 if ($3 == NULL)
731 error (_("Right operand of 'in' must be type"));
732 operation_up arg = ada_pop ();
733 pstate->push_new<ada_unop_range_operation>
734 (std::move (arg), $3);
736 | simple_exp NOT IN simple_exp DOTDOT simple_exp
737 { ada_wrap3<ada_ternop_range_operation> ();
738 ada_wrap<unary_logical_not_operation> (); }
739 | simple_exp NOT IN primary TICK_RANGE tick_arglist
741 operation_up rhs = ada_pop ();
742 operation_up lhs = ada_pop ();
743 pstate->push_new<ada_binop_in_bounds_operation>
744 (std::move (lhs), std::move (rhs), $6);
745 ada_wrap<unary_logical_not_operation> ();
747 | simple_exp NOT IN var_or_type %prec TICK_ACCESS
749 if ($4 == NULL)
750 error (_("Right operand of 'in' must be type"));
751 operation_up arg = ada_pop ();
752 pstate->push_new<ada_unop_range_operation>
753 (std::move (arg), $4);
754 ada_wrap<unary_logical_not_operation> ();
758 relation : simple_exp GEQ simple_exp
759 { ada_un_wrap2<geq_operation> (BINOP_GEQ); }
762 relation : simple_exp '<' simple_exp
763 { ada_un_wrap2<less_operation> (BINOP_LESS); }
766 relation : simple_exp '>' simple_exp
767 { ada_un_wrap2<gtr_operation> (BINOP_GTR); }
770 exp : relation
771 | and_exp
772 | and_then_exp
773 | or_exp
774 | or_else_exp
775 | xor_exp
778 and_exp :
779 relation _AND_ relation
780 { ada_wrap2<ada_bitwise_and_operation>
781 (BINOP_BITWISE_AND); }
782 | and_exp _AND_ relation
783 { ada_wrap2<ada_bitwise_and_operation>
784 (BINOP_BITWISE_AND); }
787 and_then_exp :
788 relation _AND_ THEN relation
789 { ada_wrap2<logical_and_operation>
790 (BINOP_LOGICAL_AND); }
791 | and_then_exp _AND_ THEN relation
792 { ada_wrap2<logical_and_operation>
793 (BINOP_LOGICAL_AND); }
796 or_exp :
797 relation OR relation
798 { ada_wrap2<ada_bitwise_ior_operation>
799 (BINOP_BITWISE_IOR); }
800 | or_exp OR relation
801 { ada_wrap2<ada_bitwise_ior_operation>
802 (BINOP_BITWISE_IOR); }
805 or_else_exp :
806 relation OR ELSE relation
807 { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
808 | or_else_exp OR ELSE relation
809 { ada_wrap2<logical_or_operation> (BINOP_LOGICAL_OR); }
812 xor_exp : relation XOR relation
813 { ada_wrap2<ada_bitwise_xor_operation>
814 (BINOP_BITWISE_XOR); }
815 | xor_exp XOR relation
816 { ada_wrap2<ada_bitwise_xor_operation>
817 (BINOP_BITWISE_XOR); }
820 /* Primaries can denote types (OP_TYPE). In cases such as
821 primary TICK_ADDRESS, where a type would be invalid, it will be
822 caught when evaluate_subexp in ada-lang.c tries to evaluate the
823 primary, expecting a value. Precedence rules resolve the ambiguity
824 in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A
825 construct such as aType'access'access will again cause an error when
826 aType'access evaluates to a type that evaluate_subexp attempts to
827 evaluate. */
828 primary : primary TICK_ACCESS
829 { ada_addrof (); }
830 | primary TICK_ADDRESS
831 { ada_addrof (type_system_address (pstate)); }
832 | primary TICK_COMPLETE
834 pstate->mark_completion (make_tick_completer ($2));
836 | primary TICK_FIRST tick_arglist
838 operation_up arg = ada_pop ();
839 pstate->push_new<ada_unop_atr_operation>
840 (std::move (arg), OP_ATR_FIRST, $3);
842 | primary TICK_LAST tick_arglist
844 operation_up arg = ada_pop ();
845 pstate->push_new<ada_unop_atr_operation>
846 (std::move (arg), OP_ATR_LAST, $3);
848 | primary TICK_LENGTH tick_arglist
850 operation_up arg = ada_pop ();
851 pstate->push_new<ada_unop_atr_operation>
852 (std::move (arg), OP_ATR_LENGTH, $3);
854 | primary TICK_SIZE
855 { ada_wrap<ada_atr_size_operation> (); }
856 | primary TICK_TAG
857 { ada_wrap<ada_atr_tag_operation> (); }
858 | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
859 { ada_wrap2<ada_binop_min_operation> (BINOP_MIN); }
860 | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
861 { ada_wrap2<ada_binop_max_operation> (BINOP_MAX); }
862 | opt_type_prefix TICK_POS '(' exp ')'
863 { ada_wrap<ada_pos_operation> (); }
864 | type_prefix TICK_VAL '(' exp ')'
866 operation_up arg = ada_pop ();
867 pstate->push_new<ada_atr_val_operation>
868 ($1, std::move (arg));
870 | type_prefix TICK_MODULUS
872 struct type *type_arg = check_typedef ($1);
873 if (!ada_is_modular_type (type_arg))
874 error (_("'modulus must be applied to modular type"));
875 write_int (pstate, ada_modulus (type_arg),
876 type_arg->target_type ());
880 tick_arglist : %prec '('
881 { $$ = 1; }
882 | '(' INT ')'
883 { $$ = $2.val; }
886 type_prefix :
887 var_or_type
889 if ($1 == NULL)
890 error (_("Prefix must be type"));
891 $$ = $1;
895 opt_type_prefix :
896 type_prefix
897 { $$ = $1; }
898 | /* EMPTY */
899 { $$ = parse_type (pstate)->builtin_void; }
903 primary : INT
904 { write_int (pstate, (LONGEST) $1.val, $1.type); }
907 primary : CHARLIT
909 pstate->push_new<ada_char_operation> ($1.type, $1.val);
913 primary : FLOAT
915 float_data data;
916 std::copy (std::begin ($1.val), std::end ($1.val),
917 std::begin (data));
918 pstate->push_new<float_const_operation>
919 ($1.type, data);
920 ada_wrap<ada_wrapped_operation> ();
924 primary : NULL_PTR
926 struct type *null_ptr_type
927 = lookup_pointer_type (parse_type (pstate)->builtin_int0);
928 write_int (pstate, 0, null_ptr_type);
932 primary : STRING
934 pstate->push_new<ada_string_operation>
935 (copy_name ($1));
939 primary : TRUEKEYWORD
940 { write_int (pstate, 1, type_boolean (pstate)); }
941 | FALSEKEYWORD
942 { write_int (pstate, 0, type_boolean (pstate)); }
945 primary : NEW NAME
946 { error (_("NEW not implemented.")); }
949 var_or_type: NAME %prec VAR
950 { $$ = write_var_or_type (pstate, NULL, $1); }
951 | NAME_COMPLETE %prec VAR
953 $$ = write_var_or_type_completion (pstate,
954 NULL,
955 $1);
957 | block NAME %prec VAR
958 { $$ = write_var_or_type (pstate, $1, $2); }
959 | block NAME_COMPLETE %prec VAR
961 $$ = write_var_or_type_completion (pstate,
963 $2);
965 | NAME TICK_ACCESS
967 $$ = write_var_or_type (pstate, NULL, $1);
968 if ($$ == NULL)
969 ada_addrof ();
970 else
971 $$ = lookup_pointer_type ($$);
973 | block NAME TICK_ACCESS
975 $$ = write_var_or_type (pstate, $1, $2);
976 if ($$ == NULL)
977 ada_addrof ();
978 else
979 $$ = lookup_pointer_type ($$);
983 /* GDB extension */
984 block : NAME COLONCOLON
985 { $$ = block_lookup (NULL, $1.ptr); }
986 | block NAME COLONCOLON
987 { $$ = block_lookup ($1, $2.ptr); }
990 aggregate :
991 '(' aggregate_component_list ')'
993 std::vector<ada_component_up> components
994 = pop_components ($2);
996 push_component<ada_aggregate_component>
997 (std::move (components));
1001 aggregate_component_list :
1002 component_groups { $$ = $1; }
1003 | positional_list exp
1005 push_component<ada_positional_component>
1006 ($1, ada_pop ());
1007 $$ = $1 + 1;
1009 | positional_list component_groups
1010 { $$ = $1 + $2; }
1013 positional_list :
1014 exp ','
1016 push_component<ada_positional_component>
1017 (0, ada_pop ());
1018 $$ = 1;
1020 | positional_list exp ','
1022 push_component<ada_positional_component>
1023 ($1, ada_pop ());
1024 $$ = $1 + 1;
1028 component_groups:
1029 others { $$ = 1; }
1030 | component_group { $$ = 1; }
1031 | component_group ',' component_groups
1032 { $$ = $3 + 1; }
1035 others : OTHERS ARROW exp
1037 push_component<ada_others_component> (ada_pop ());
1041 component_group :
1042 component_associations
1044 ada_choices_component *choices = choice_component ();
1045 choices->set_associations (pop_associations ($1));
1049 /* We use this somewhat obscure definition in order to handle NAME => and
1050 NAME | differently from exp => and exp |. ARROW and '|' have a precedence
1051 above that of the reduction of NAME to var_or_type. By delaying
1052 decisions until after the => or '|', we convert the ambiguity to a
1053 resolved shift/reduce conflict. */
1054 component_associations :
1055 NAME ARROW exp
1057 push_component<ada_choices_component> (ada_pop ());
1058 write_name_assoc (pstate, $1);
1059 $$ = 1;
1061 | simple_exp ARROW exp
1063 push_component<ada_choices_component> (ada_pop ());
1064 push_association<ada_name_association> (ada_pop ());
1065 $$ = 1;
1067 | simple_exp DOTDOT simple_exp ARROW exp
1069 push_component<ada_choices_component> (ada_pop ());
1070 operation_up rhs = ada_pop ();
1071 operation_up lhs = ada_pop ();
1072 push_association<ada_discrete_range_association>
1073 (std::move (lhs), std::move (rhs));
1074 $$ = 1;
1076 | NAME '|' component_associations
1078 write_name_assoc (pstate, $1);
1079 $$ = $3 + 1;
1081 | simple_exp '|' component_associations
1083 push_association<ada_name_association> (ada_pop ());
1084 $$ = $3 + 1;
1086 | simple_exp DOTDOT simple_exp '|' component_associations
1089 operation_up rhs = ada_pop ();
1090 operation_up lhs = ada_pop ();
1091 push_association<ada_discrete_range_association>
1092 (std::move (lhs), std::move (rhs));
1093 $$ = $5 + 1;
1097 /* Some extensions borrowed from C, for the benefit of those who find they
1098 can't get used to Ada notation in GDB. */
1100 primary : '*' primary %prec '.'
1101 { ada_wrap<ada_unop_ind_operation> (); }
1102 | '&' primary %prec '.'
1103 { ada_addrof (); }
1104 | primary '[' exp ']'
1106 ada_wrap2<subscript_operation> (BINOP_SUBSCRIPT);
1107 ada_wrap<ada_wrapped_operation> ();
1113 /* yylex defined in ada-lex.c: Reads one token, getting characters */
1114 /* through lexptr. */
1116 /* Remap normal flex interface names (yylex) as well as gratuitiously */
1117 /* global symbol names, so we can have multiple flex-generated parsers */
1118 /* in gdb. */
1120 /* (See note above on previous definitions for YACC.) */
1122 #define yy_create_buffer ada_yy_create_buffer
1123 #define yy_delete_buffer ada_yy_delete_buffer
1124 #define yy_init_buffer ada_yy_init_buffer
1125 #define yy_load_buffer_state ada_yy_load_buffer_state
1126 #define yy_switch_to_buffer ada_yy_switch_to_buffer
1127 #define yyrestart ada_yyrestart
1128 #define yytext ada_yytext
1130 static struct obstack temp_parse_space;
1132 /* The following kludge was found necessary to prevent conflicts between */
1133 /* defs.h and non-standard stdlib.h files. */
1134 #define qsort __qsort__dummy
1135 #include "ada-lex.c"
1138 ada_parse (struct parser_state *par_state)
1140 /* Setting up the parser state. */
1141 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1142 gdb_assert (par_state != NULL);
1143 pstate = par_state;
1144 original_expr = par_state->lexptr;
1146 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
1147 parser_debug);
1149 lexer_init (yyin); /* (Re-)initialize lexer. */
1150 obstack_free (&temp_parse_space, NULL);
1151 obstack_init (&temp_parse_space);
1152 components.clear ();
1153 associations.clear ();
1155 int result = yyparse ();
1156 if (!result)
1158 struct type *context_type = nullptr;
1159 if (par_state->void_context_p)
1160 context_type = parse_type (par_state)->builtin_void;
1161 pstate->set_operation (ada_pop (true, context_type));
1163 return result;
1166 static void
1167 yyerror (const char *msg)
1169 error (_("Error in expression, near `%s'."), pstate->lexptr);
1172 /* Emit expression to access an instance of SYM, in block BLOCK (if
1173 non-NULL). */
1175 static void
1176 write_var_from_sym (struct parser_state *par_state, block_symbol sym)
1178 if (symbol_read_needs_frame (sym.symbol))
1179 par_state->block_tracker->update (sym.block, INNERMOST_BLOCK_FOR_SYMBOLS);
1181 par_state->push_new<ada_var_value_operation> (sym);
1184 /* Write integer or boolean constant ARG of type TYPE. */
1186 static void
1187 write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
1189 pstate->push_new<long_const_operation> (type, arg);
1190 ada_wrap<ada_wrapped_operation> ();
1193 /* Emit expression corresponding to the renamed object named
1194 designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
1195 context of ORIG_LEFT_CONTEXT, to which is applied the operations
1196 encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of
1197 cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it
1198 defaults to the currently selected block. ORIG_SYMBOL is the
1199 symbol that originally encoded the renaming. It is needed only
1200 because its prefix also qualifies any index variables used to index
1201 or slice an array. It should not be necessary once we go to the
1202 new encoding entirely (FIXME pnh 7/20/2007). */
1204 static void
1205 write_object_renaming (struct parser_state *par_state,
1206 const struct block *orig_left_context,
1207 const char *renamed_entity, int renamed_entity_len,
1208 const char *renaming_expr, int max_depth)
1210 char *name;
1211 enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
1212 struct block_symbol sym_info;
1214 if (max_depth <= 0)
1215 error (_("Could not find renamed symbol"));
1217 if (orig_left_context == NULL)
1218 orig_left_context = get_selected_block (NULL);
1220 name = obstack_strndup (&temp_parse_space, renamed_entity,
1221 renamed_entity_len);
1222 ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
1223 if (sym_info.symbol == NULL)
1224 error (_("Could not find renamed variable: %s"), ada_decode (name).c_str ());
1225 else if (sym_info.symbol->aclass () == LOC_TYPEDEF)
1226 /* We have a renaming of an old-style renaming symbol. Don't
1227 trust the block information. */
1228 sym_info.block = orig_left_context;
1231 const char *inner_renamed_entity;
1232 int inner_renamed_entity_len;
1233 const char *inner_renaming_expr;
1235 switch (ada_parse_renaming (sym_info.symbol, &inner_renamed_entity,
1236 &inner_renamed_entity_len,
1237 &inner_renaming_expr))
1239 case ADA_NOT_RENAMING:
1240 write_var_from_sym (par_state, sym_info);
1241 break;
1242 case ADA_OBJECT_RENAMING:
1243 write_object_renaming (par_state, sym_info.block,
1244 inner_renamed_entity, inner_renamed_entity_len,
1245 inner_renaming_expr, max_depth - 1);
1246 break;
1247 default:
1248 goto BadEncoding;
1252 slice_state = SIMPLE_INDEX;
1253 while (*renaming_expr == 'X')
1255 renaming_expr += 1;
1257 switch (*renaming_expr) {
1258 case 'A':
1259 renaming_expr += 1;
1260 ada_wrap<ada_unop_ind_operation> ();
1261 break;
1262 case 'L':
1263 slice_state = LOWER_BOUND;
1264 /* FALLTHROUGH */
1265 case 'S':
1266 renaming_expr += 1;
1267 if (isdigit (*renaming_expr))
1269 char *next;
1270 long val = strtol (renaming_expr, &next, 10);
1271 if (next == renaming_expr)
1272 goto BadEncoding;
1273 renaming_expr = next;
1274 write_int (par_state, val, type_int (par_state));
1276 else
1278 const char *end;
1279 char *index_name;
1280 struct block_symbol index_sym_info;
1282 end = strchr (renaming_expr, 'X');
1283 if (end == NULL)
1284 end = renaming_expr + strlen (renaming_expr);
1286 index_name = obstack_strndup (&temp_parse_space, renaming_expr,
1287 end - renaming_expr);
1288 renaming_expr = end;
1290 ada_lookup_encoded_symbol (index_name, orig_left_context,
1291 VAR_DOMAIN, &index_sym_info);
1292 if (index_sym_info.symbol == NULL)
1293 error (_("Could not find %s"), index_name);
1294 else if (index_sym_info.symbol->aclass () == LOC_TYPEDEF)
1295 /* Index is an old-style renaming symbol. */
1296 index_sym_info.block = orig_left_context;
1297 write_var_from_sym (par_state, index_sym_info);
1299 if (slice_state == SIMPLE_INDEX)
1300 ada_funcall (1);
1301 else if (slice_state == LOWER_BOUND)
1302 slice_state = UPPER_BOUND;
1303 else if (slice_state == UPPER_BOUND)
1305 ada_wrap3<ada_ternop_slice_operation> ();
1306 slice_state = SIMPLE_INDEX;
1308 break;
1310 case 'R':
1312 const char *end;
1314 renaming_expr += 1;
1316 if (slice_state != SIMPLE_INDEX)
1317 goto BadEncoding;
1318 end = strchr (renaming_expr, 'X');
1319 if (end == NULL)
1320 end = renaming_expr + strlen (renaming_expr);
1322 operation_up arg = ada_pop ();
1323 pstate->push_new<ada_structop_operation>
1324 (std::move (arg), std::string (renaming_expr,
1325 end - renaming_expr));
1326 renaming_expr = end;
1327 break;
1330 default:
1331 goto BadEncoding;
1334 if (slice_state == SIMPLE_INDEX)
1335 return;
1337 BadEncoding:
1338 error (_("Internal error in encoding of renaming declaration"));
1341 static const struct block*
1342 block_lookup (const struct block *context, const char *raw_name)
1344 const char *name;
1345 struct symtab *symtab;
1346 const struct block *result = NULL;
1348 std::string name_storage;
1349 if (raw_name[0] == '\'')
1351 raw_name += 1;
1352 name = raw_name;
1354 else
1356 name_storage = ada_encode (raw_name);
1357 name = name_storage.c_str ();
1360 std::vector<struct block_symbol> syms
1361 = ada_lookup_symbol_list (name, context, VAR_DOMAIN);
1363 if (context == NULL
1364 && (syms.empty () || syms[0].symbol->aclass () != LOC_BLOCK))
1365 symtab = lookup_symtab (name);
1366 else
1367 symtab = NULL;
1369 if (symtab != NULL)
1370 result = symtab->compunit ()->blockvector ()->static_block ();
1371 else if (syms.empty () || syms[0].symbol->aclass () != LOC_BLOCK)
1373 if (context == NULL)
1374 error (_("No file or function \"%s\"."), raw_name);
1375 else
1376 error (_("No function \"%s\" in specified context."), raw_name);
1378 else
1380 if (syms.size () > 1)
1381 warning (_("Function name \"%s\" ambiguous here"), raw_name);
1382 result = syms[0].symbol->value_block ();
1385 return result;
1388 static struct symbol*
1389 select_possible_type_sym (const std::vector<struct block_symbol> &syms)
1391 int i;
1392 int preferred_index;
1393 struct type *preferred_type;
1395 preferred_index = -1; preferred_type = NULL;
1396 for (i = 0; i < syms.size (); i += 1)
1397 switch (syms[i].symbol->aclass ())
1399 case LOC_TYPEDEF:
1400 if (ada_prefer_type (syms[i].symbol->type (), preferred_type))
1402 preferred_index = i;
1403 preferred_type = syms[i].symbol->type ();
1405 break;
1406 case LOC_REGISTER:
1407 case LOC_ARG:
1408 case LOC_REF_ARG:
1409 case LOC_REGPARM_ADDR:
1410 case LOC_LOCAL:
1411 case LOC_COMPUTED:
1412 return NULL;
1413 default:
1414 break;
1416 if (preferred_type == NULL)
1417 return NULL;
1418 return syms[preferred_index].symbol;
1421 static struct type*
1422 find_primitive_type (struct parser_state *par_state, const char *name)
1424 struct type *type;
1425 type = language_lookup_primitive_type (par_state->language (),
1426 par_state->gdbarch (),
1427 name);
1428 if (type == NULL && strcmp ("system__address", name) == 0)
1429 type = type_system_address (par_state);
1431 if (type != NULL)
1433 /* Check to see if we have a regular definition of this
1434 type that just didn't happen to have been read yet. */
1435 struct symbol *sym;
1436 char *expanded_name =
1437 (char *) alloca (strlen (name) + sizeof ("standard__"));
1438 strcpy (expanded_name, "standard__");
1439 strcat (expanded_name, name);
1440 sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN).symbol;
1441 if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
1442 type = sym->type ();
1445 return type;
1448 static int
1449 chop_selector (const char *name, int end)
1451 int i;
1452 for (i = end - 1; i > 0; i -= 1)
1453 if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
1454 return i;
1455 return -1;
1458 /* If NAME is a string beginning with a separator (either '__', or
1459 '.'), chop this separator and return the result; else, return
1460 NAME. */
1462 static const char *
1463 chop_separator (const char *name)
1465 if (*name == '.')
1466 return name + 1;
1468 if (name[0] == '_' && name[1] == '_')
1469 return name + 2;
1471 return name;
1474 /* Given that SELS is a string of the form (<sep><identifier>)*, where
1475 <sep> is '__' or '.', write the indicated sequence of
1476 STRUCTOP_STRUCT expression operators. Returns a pointer to the
1477 last operation that was pushed. */
1478 static ada_structop_operation *
1479 write_selectors (struct parser_state *par_state, const char *sels)
1481 ada_structop_operation *result = nullptr;
1482 while (*sels != '\0')
1484 const char *p = chop_separator (sels);
1485 sels = p;
1486 while (*sels != '\0' && *sels != '.'
1487 && (sels[0] != '_' || sels[1] != '_'))
1488 sels += 1;
1489 operation_up arg = ada_pop ();
1490 result = new ada_structop_operation (std::move (arg),
1491 std::string (p, sels - p));
1492 pstate->push (operation_up (result));
1494 return result;
1497 /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
1498 NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes
1499 a temporary symbol that is valid until the next call to ada_parse.
1501 static void
1502 write_ambiguous_var (struct parser_state *par_state,
1503 const struct block *block, const char *name, int len)
1505 struct symbol *sym = new (&temp_parse_space) symbol ();
1507 sym->set_domain (UNDEF_DOMAIN);
1508 sym->set_linkage_name (obstack_strndup (&temp_parse_space, name, len));
1509 sym->set_language (language_ada, nullptr);
1511 block_symbol bsym { sym, block };
1512 par_state->push_new<ada_var_value_operation> (bsym);
1515 /* A convenient wrapper around ada_get_field_index that takes
1516 a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
1517 of a NUL-terminated field name. */
1519 static int
1520 ada_nget_field_index (const struct type *type, const char *field_name0,
1521 int field_name_len, int maybe_missing)
1523 char *field_name = (char *) alloca ((field_name_len + 1) * sizeof (char));
1525 strncpy (field_name, field_name0, field_name_len);
1526 field_name[field_name_len] = '\0';
1527 return ada_get_field_index (type, field_name, maybe_missing);
1530 /* If encoded_field_name is the name of a field inside symbol SYM,
1531 then return the type of that field. Otherwise, return NULL.
1533 This function is actually recursive, so if ENCODED_FIELD_NAME
1534 doesn't match one of the fields of our symbol, then try to see
1535 if ENCODED_FIELD_NAME could not be a succession of field names
1536 (in other words, the user entered an expression of the form
1537 TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
1538 each field name sequentially to obtain the desired field type.
1539 In case of failure, we return NULL. */
1541 static struct type *
1542 get_symbol_field_type (struct symbol *sym, const char *encoded_field_name)
1544 const char *field_name = encoded_field_name;
1545 const char *subfield_name;
1546 struct type *type = sym->type ();
1547 int fieldno;
1549 if (type == NULL || field_name == NULL)
1550 return NULL;
1551 type = check_typedef (type);
1553 while (field_name[0] != '\0')
1555 field_name = chop_separator (field_name);
1557 fieldno = ada_get_field_index (type, field_name, 1);
1558 if (fieldno >= 0)
1559 return type->field (fieldno).type ();
1561 subfield_name = field_name;
1562 while (*subfield_name != '\0' && *subfield_name != '.'
1563 && (subfield_name[0] != '_' || subfield_name[1] != '_'))
1564 subfield_name += 1;
1566 if (subfield_name[0] == '\0')
1567 return NULL;
1569 fieldno = ada_nget_field_index (type, field_name,
1570 subfield_name - field_name, 1);
1571 if (fieldno < 0)
1572 return NULL;
1574 type = type->field (fieldno).type ();
1575 field_name = subfield_name;
1578 return NULL;
1581 /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
1582 expression_block_context if NULL). If it denotes a type, return
1583 that type. Otherwise, write expression code to evaluate it as an
1584 object and return NULL. In this second case, NAME0 will, in general,
1585 have the form <name>(.<selector_name>)*, where <name> is an object
1586 or renaming encoded in the debugging data. Calls error if no
1587 prefix <name> matches a name in the debugging data (i.e., matches
1588 either a complete name or, as a wild-card match, the final
1589 identifier). */
1591 static struct type*
1592 write_var_or_type (struct parser_state *par_state,
1593 const struct block *block, struct stoken name0)
1595 int depth;
1596 char *encoded_name;
1597 int name_len;
1599 if (block == NULL)
1600 block = par_state->expression_context_block;
1602 std::string name_storage = ada_encode (name0.ptr);
1603 name_len = name_storage.size ();
1604 encoded_name = obstack_strndup (&temp_parse_space, name_storage.c_str (),
1605 name_len);
1606 for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
1608 int tail_index;
1610 tail_index = name_len;
1611 while (tail_index > 0)
1613 struct symbol *type_sym;
1614 struct symbol *renaming_sym;
1615 const char* renaming;
1616 int renaming_len;
1617 const char* renaming_expr;
1618 int terminator = encoded_name[tail_index];
1620 encoded_name[tail_index] = '\0';
1621 /* In order to avoid double-encoding, we want to only pass
1622 the decoded form to lookup functions. */
1623 std::string decoded_name = ada_decode (encoded_name);
1624 encoded_name[tail_index] = terminator;
1626 std::vector<struct block_symbol> syms
1627 = ada_lookup_symbol_list (decoded_name.c_str (), block, VAR_DOMAIN);
1629 type_sym = select_possible_type_sym (syms);
1631 if (type_sym != NULL)
1632 renaming_sym = type_sym;
1633 else if (syms.size () == 1)
1634 renaming_sym = syms[0].symbol;
1635 else
1636 renaming_sym = NULL;
1638 switch (ada_parse_renaming (renaming_sym, &renaming,
1639 &renaming_len, &renaming_expr))
1641 case ADA_NOT_RENAMING:
1642 break;
1643 case ADA_PACKAGE_RENAMING:
1644 case ADA_EXCEPTION_RENAMING:
1645 case ADA_SUBPROGRAM_RENAMING:
1647 int alloc_len = renaming_len + name_len - tail_index + 1;
1648 char *new_name
1649 = (char *) obstack_alloc (&temp_parse_space, alloc_len);
1650 strncpy (new_name, renaming, renaming_len);
1651 strcpy (new_name + renaming_len, encoded_name + tail_index);
1652 encoded_name = new_name;
1653 name_len = renaming_len + name_len - tail_index;
1654 goto TryAfterRenaming;
1656 case ADA_OBJECT_RENAMING:
1657 write_object_renaming (par_state, block, renaming, renaming_len,
1658 renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
1659 write_selectors (par_state, encoded_name + tail_index);
1660 return NULL;
1661 default:
1662 internal_error (__FILE__, __LINE__,
1663 _("impossible value from ada_parse_renaming"));
1666 if (type_sym != NULL)
1668 struct type *field_type;
1670 if (tail_index == name_len)
1671 return type_sym->type ();
1673 /* We have some extraneous characters after the type name.
1674 If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
1675 then try to get the type of FIELDN. */
1676 field_type
1677 = get_symbol_field_type (type_sym, encoded_name + tail_index);
1678 if (field_type != NULL)
1679 return field_type;
1680 else
1681 error (_("Invalid attempt to select from type: \"%s\"."),
1682 name0.ptr);
1684 else if (tail_index == name_len && syms.empty ())
1686 struct type *type = find_primitive_type (par_state,
1687 encoded_name);
1689 if (type != NULL)
1690 return type;
1693 if (syms.size () == 1)
1695 write_var_from_sym (par_state, syms[0]);
1696 write_selectors (par_state, encoded_name + tail_index);
1697 return NULL;
1699 else if (syms.empty ())
1701 struct bound_minimal_symbol msym
1702 = ada_lookup_simple_minsym (decoded_name.c_str ());
1703 if (msym.minsym != NULL)
1705 par_state->push_new<ada_var_msym_value_operation> (msym);
1706 /* Maybe cause error here rather than later? FIXME? */
1707 write_selectors (par_state, encoded_name + tail_index);
1708 return NULL;
1711 if (tail_index == name_len
1712 && strncmp (encoded_name, "standard__",
1713 sizeof ("standard__") - 1) == 0)
1714 error (_("No definition of \"%s\" found."), name0.ptr);
1716 tail_index = chop_selector (encoded_name, tail_index);
1718 else
1720 write_ambiguous_var (par_state, block, encoded_name,
1721 tail_index);
1722 write_selectors (par_state, encoded_name + tail_index);
1723 return NULL;
1727 if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
1728 error (_("No symbol table is loaded. Use the \"file\" command."));
1729 if (block == par_state->expression_context_block)
1730 error (_("No definition of \"%s\" in current context."), name0.ptr);
1731 else
1732 error (_("No definition of \"%s\" in specified context."), name0.ptr);
1734 TryAfterRenaming: ;
1737 error (_("Could not find renamed symbol \"%s\""), name0.ptr);
1741 /* Because ada_completer_word_break_characters does not contain '.' --
1742 and it cannot easily be added, this breaks other completions -- we
1743 have to recreate the completion word-splitting here, so that we can
1744 provide a prefix that is then used when completing field names.
1745 Without this, an attempt like "complete print abc.d" will give a
1746 result like "print def" rather than "print abc.def". */
1748 static std::string
1749 find_completion_bounds (struct parser_state *par_state)
1751 const char *end = pstate->lexptr;
1752 /* First the end of the prefix. Here we stop at the token start or
1753 at '.' or space. */
1754 for (; end > original_expr && end[-1] != '.' && !isspace (end[-1]); --end)
1756 /* Nothing. */
1758 /* Now find the start of the prefix. */
1759 const char *ptr = end;
1760 /* Here we allow '.'. */
1761 for (;
1762 ptr > original_expr && (ptr[-1] == '.'
1763 || ptr[-1] == '_'
1764 || (ptr[-1] >= 'a' && ptr[-1] <= 'z')
1765 || (ptr[-1] >= 'A' && ptr[-1] <= 'Z')
1766 || (ptr[-1] & 0xff) >= 0x80);
1767 --ptr)
1769 /* Nothing. */
1771 /* ... except, skip leading spaces. */
1772 ptr = skip_spaces (ptr);
1774 return std::string (ptr, end);
1777 /* A wrapper for write_var_or_type that is used specifically when
1778 completion is requested for the last of a sequence of
1779 identifiers. */
1781 static struct type *
1782 write_var_or_type_completion (struct parser_state *par_state,
1783 const struct block *block, struct stoken name0)
1785 int tail_index = chop_selector (name0.ptr, name0.length);
1786 /* If there's no separator, just defer to ordinary symbol
1787 completion. */
1788 if (tail_index == -1)
1789 return write_var_or_type (par_state, block, name0);
1791 std::string copy (name0.ptr, tail_index);
1792 struct type *type = write_var_or_type (par_state, block,
1793 { copy.c_str (),
1794 (int) copy.length () });
1795 /* For completion purposes, it's enough that we return a type
1796 here. */
1797 if (type != nullptr)
1798 return type;
1800 ada_structop_operation *op = write_selectors (par_state,
1801 name0.ptr + tail_index);
1802 op->set_prefix (find_completion_bounds (par_state));
1803 par_state->mark_struct_expression (op);
1804 return nullptr;
1807 /* Write a left side of a component association (e.g., NAME in NAME =>
1808 exp). If NAME has the form of a selected component, write it as an
1809 ordinary expression. If it is a simple variable that unambiguously
1810 corresponds to exactly one symbol that does not denote a type or an
1811 object renaming, also write it normally as an OP_VAR_VALUE.
1812 Otherwise, write it as an OP_NAME.
1814 Unfortunately, we don't know at this point whether NAME is supposed
1815 to denote a record component name or the value of an array index.
1816 Therefore, it is not appropriate to disambiguate an ambiguous name
1817 as we normally would, nor to replace a renaming with its referent.
1818 As a result, in the (one hopes) rare case that one writes an
1819 aggregate such as (R => 42) where R renames an object or is an
1820 ambiguous name, one must write instead ((R) => 42). */
1822 static void
1823 write_name_assoc (struct parser_state *par_state, struct stoken name)
1825 if (strchr (name.ptr, '.') == NULL)
1827 std::vector<struct block_symbol> syms
1828 = ada_lookup_symbol_list (name.ptr,
1829 par_state->expression_context_block,
1830 VAR_DOMAIN);
1832 if (syms.size () != 1 || syms[0].symbol->aclass () == LOC_TYPEDEF)
1833 pstate->push_new<ada_string_operation> (copy_name (name));
1834 else
1835 write_var_from_sym (par_state, syms[0]);
1837 else
1838 if (write_var_or_type (par_state, NULL, name) != NULL)
1839 error (_("Invalid use of type."));
1841 push_association<ada_name_association> (ada_pop ());
1844 static struct type *
1845 type_int (struct parser_state *par_state)
1847 return parse_type (par_state)->builtin_int;
1850 static struct type *
1851 type_long (struct parser_state *par_state)
1853 return parse_type (par_state)->builtin_long;
1856 static struct type *
1857 type_long_long (struct parser_state *par_state)
1859 return parse_type (par_state)->builtin_long_long;
1862 static struct type *
1863 type_long_double (struct parser_state *par_state)
1865 return parse_type (par_state)->builtin_long_double;
1868 static struct type *
1869 type_for_char (struct parser_state *par_state, ULONGEST value)
1871 if (value <= 0xff)
1872 return language_string_char_type (par_state->language (),
1873 par_state->gdbarch ());
1874 else if (value <= 0xffff)
1875 return language_lookup_primitive_type (par_state->language (),
1876 par_state->gdbarch (),
1877 "wide_character");
1878 return language_lookup_primitive_type (par_state->language (),
1879 par_state->gdbarch (),
1880 "wide_wide_character");
1883 static struct type *
1884 type_boolean (struct parser_state *par_state)
1886 return parse_type (par_state)->builtin_bool;
1889 static struct type *
1890 type_system_address (struct parser_state *par_state)
1892 struct type *type
1893 = language_lookup_primitive_type (par_state->language (),
1894 par_state->gdbarch (),
1895 "system__address");
1896 return type != NULL ? type : parse_type (par_state)->builtin_data_ptr;
1899 void _initialize_ada_exp ();
1900 void
1901 _initialize_ada_exp ()
1903 obstack_init (&temp_parse_space);