1 /* YACC parser for Ada expressions, for GDB.
2 Copyright (C) 1986, 1989-1991, 1993-1994, 1997, 2000, 2003-2004,
3 2007-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Parse an Ada expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
40 #include "gdb_string.h"
42 #include "expression.h"
44 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 #define parse_type builtin_type (parse_gdbarch)
55 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
56 as well as gratuitiously global symbol names, so we can have multiple
57 yacc generated parsers in gdb. These are only the variables
58 produced by yacc. If other parser generators (bison, byacc, etc) produce
59 additional global names that conflict at link time, then those parser
60 generators need to be fixed instead of adding those names to this list. */
62 /* NOTE: This is clumsy, especially since BISON and FLEX provide --prefix
63 options. I presume we are maintaining it to accommodate systems
64 without BISON? (PNH) */
66 #define yymaxdepth ada_maxdepth
67 #define yyparse _ada_parse /* ada_parse calls this after initialization */
69 #define yyerror ada_error
70 #define yylval ada_lval
71 #define yychar ada_char
72 #define yydebug ada_debug
73 #define yypact ada_pact
80 #define yyexca ada_exca
81 #define yyerrflag ada_errflag
82 #define yynerrs ada_nerrs
86 #define yy_yys ada_yys
87 #define yystate ada_state
90 #define yy_yyv ada_yyv
92 #define yylloc ada_lloc
93 #define yyreds ada_reds /* With YYDEBUG defined */
94 #define yytoks ada_toks /* With YYDEBUG defined */
95 #define yyname ada_name /* With YYDEBUG defined */
96 #define yyrule ada_rule /* With YYDEBUG defined */
99 #define YYDEBUG 1 /* Default to yydebug support */
102 #define YYFPRINTF parser_fprintf
106 struct minimal_symbol *msym;
108 struct stoken stoken;
111 static struct stoken empty_stoken = { "", 0 };
113 /* If expression is in the context of TYPE'(...), then TYPE, else
115 static struct type *type_qualifier;
119 static int yylex (void);
121 void yyerror (char *);
123 static struct stoken string_to_operator (struct stoken);
125 static void write_int (LONGEST, struct type *);
127 static void write_object_renaming (struct block *, const char *, int,
130 static struct type* write_var_or_type (struct block *, struct stoken);
132 static void write_name_assoc (struct stoken);
134 static void write_exp_op_with_string (enum exp_opcode, struct stoken);
136 static struct block *block_lookup (struct block *, char *);
138 static LONGEST convert_char_literal (struct type *, LONGEST);
140 static void write_ambiguous_var (struct block *, char *, int);
142 static struct type *type_int (void);
144 static struct type *type_long (void);
146 static struct type *type_long_long (void);
148 static struct type *type_float (void);
150 static struct type *type_double (void);
152 static struct type *type_long_double (void);
154 static struct type *type_char (void);
156 static struct type *type_boolean (void);
158 static struct type *type_system_address (void);
176 struct internalvar *ivar;
179 %type <lval> positional_list component_groups component_associations
180 %type <lval> aggregate_component_list
181 %type <tval> var_or_type
183 %token <typed_val> INT NULL_PTR CHARLIT
184 %token <typed_val_float> FLOAT
185 %token TRUEKEYWORD FALSEKEYWORD
187 %token <sval> STRING NAME DOT_ID
189 %type <lval> arglist tick_arglist
191 %type <tval> save_qualifier
195 /* Special type cases, put in to allow the parser to distinguish different
197 %token <sval> SPECIAL_VARIABLE
200 %left _AND_ OR XOR THEN ELSE
201 %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT
205 %left '*' '/' MOD REM
206 %right STARSTAR ABS NOT
208 /* Artificial token to give NAME => ... and NAME | priority over reducing
209 NAME to <primary> and to give <primary>' priority over reducing <primary>
215 %right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH
216 %right TICK_MAX TICK_MIN TICK_MODULUS
217 %right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL
218 /* The following are right-associative only so that reductions at this
219 precedence have lower precedence than '.' and '('. The syntax still
220 forces a.b.c, e.g., to be LEFT-associated. */
221 %right '.' '(' '[' DOT_ID DOT_ALL
231 /* Expressions, including the sequencing operator. */
234 { write_exp_elt_opcode (BINOP_COMMA); }
235 | primary ASSIGN exp /* Extension for convenience */
236 { write_exp_elt_opcode (BINOP_ASSIGN); }
239 /* Expressions, not including the sequencing operator. */
240 primary : primary DOT_ALL
241 { write_exp_elt_opcode (UNOP_IND); }
244 primary : primary DOT_ID
245 { write_exp_op_with_string (STRUCTOP_STRUCT, $2); }
248 primary : primary '(' arglist ')'
250 write_exp_elt_opcode (OP_FUNCALL);
251 write_exp_elt_longcst ($3);
252 write_exp_elt_opcode (OP_FUNCALL);
254 | var_or_type '(' arglist ')'
259 error (_("Invalid conversion"));
260 write_exp_elt_opcode (UNOP_CAST);
261 write_exp_elt_type ($1);
262 write_exp_elt_opcode (UNOP_CAST);
266 write_exp_elt_opcode (OP_FUNCALL);
267 write_exp_elt_longcst ($3);
268 write_exp_elt_opcode (OP_FUNCALL);
273 primary : var_or_type '\'' save_qualifier { type_qualifier = $1; }
277 error (_("Type required for qualification"));
278 write_exp_elt_opcode (UNOP_QUAL);
279 write_exp_elt_type ($1);
280 write_exp_elt_opcode (UNOP_QUAL);
285 save_qualifier : { $$ = type_qualifier; }
289 primary '(' simple_exp DOTDOT simple_exp ')'
290 { write_exp_elt_opcode (TERNOP_SLICE); }
291 | var_or_type '(' simple_exp DOTDOT simple_exp ')'
293 write_exp_elt_opcode (TERNOP_SLICE);
295 error (_("Cannot slice a type"));
299 primary : '(' exp1 ')' { }
302 /* The following rule causes a conflict with the type conversion
304 To get around it, we give '(' higher priority and add bridge rules for
305 var_or_type (exp, exp, ...)
306 var_or_type (exp .. exp)
307 We also have the action for var_or_type(exp) generate a function call
308 when the first symbol does not denote a type. */
310 primary : var_or_type %prec VAR
313 write_exp_elt_opcode (OP_TYPE);
314 write_exp_elt_type ($1);
315 write_exp_elt_opcode (OP_TYPE);
320 primary : SPECIAL_VARIABLE /* Various GDB extensions */
321 { write_dollar_variable ($1); }
330 simple_exp : '-' simple_exp %prec UNARY
331 { write_exp_elt_opcode (UNOP_NEG); }
334 simple_exp : '+' simple_exp %prec UNARY
335 { write_exp_elt_opcode (UNOP_PLUS); }
338 simple_exp : NOT simple_exp %prec UNARY
339 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
342 simple_exp : ABS simple_exp %prec UNARY
343 { write_exp_elt_opcode (UNOP_ABS); }
346 arglist : { $$ = 0; }
355 | arglist ',' NAME ARROW exp
359 primary : '{' var_or_type '}' primary %prec '.'
363 error (_("Type required within braces in coercion"));
364 write_exp_elt_opcode (UNOP_MEMVAL);
365 write_exp_elt_type ($2);
366 write_exp_elt_opcode (UNOP_MEMVAL);
370 /* Binary operators in order of decreasing precedence. */
372 simple_exp : simple_exp STARSTAR simple_exp
373 { write_exp_elt_opcode (BINOP_EXP); }
376 simple_exp : simple_exp '*' simple_exp
377 { write_exp_elt_opcode (BINOP_MUL); }
380 simple_exp : simple_exp '/' simple_exp
381 { write_exp_elt_opcode (BINOP_DIV); }
384 simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */
385 { write_exp_elt_opcode (BINOP_REM); }
388 simple_exp : simple_exp MOD simple_exp
389 { write_exp_elt_opcode (BINOP_MOD); }
392 simple_exp : simple_exp '@' simple_exp /* GDB extension */
393 { write_exp_elt_opcode (BINOP_REPEAT); }
396 simple_exp : simple_exp '+' simple_exp
397 { write_exp_elt_opcode (BINOP_ADD); }
400 simple_exp : simple_exp '&' simple_exp
401 { write_exp_elt_opcode (BINOP_CONCAT); }
404 simple_exp : simple_exp '-' simple_exp
405 { write_exp_elt_opcode (BINOP_SUB); }
408 relation : simple_exp
411 relation : simple_exp '=' simple_exp
412 { write_exp_elt_opcode (BINOP_EQUAL); }
415 relation : simple_exp NOTEQUAL simple_exp
416 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
419 relation : simple_exp LEQ simple_exp
420 { write_exp_elt_opcode (BINOP_LEQ); }
423 relation : simple_exp IN simple_exp DOTDOT simple_exp
424 { write_exp_elt_opcode (TERNOP_IN_RANGE); }
425 | simple_exp IN primary TICK_RANGE tick_arglist
426 { write_exp_elt_opcode (BINOP_IN_BOUNDS);
427 write_exp_elt_longcst ((LONGEST) $5);
428 write_exp_elt_opcode (BINOP_IN_BOUNDS);
430 | simple_exp IN var_or_type %prec TICK_ACCESS
433 error (_("Right operand of 'in' must be type"));
434 write_exp_elt_opcode (UNOP_IN_RANGE);
435 write_exp_elt_type ($3);
436 write_exp_elt_opcode (UNOP_IN_RANGE);
438 | simple_exp NOT IN simple_exp DOTDOT simple_exp
439 { write_exp_elt_opcode (TERNOP_IN_RANGE);
440 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
442 | simple_exp NOT IN primary TICK_RANGE tick_arglist
443 { write_exp_elt_opcode (BINOP_IN_BOUNDS);
444 write_exp_elt_longcst ((LONGEST) $6);
445 write_exp_elt_opcode (BINOP_IN_BOUNDS);
446 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
448 | simple_exp NOT IN var_or_type %prec TICK_ACCESS
451 error (_("Right operand of 'in' must be type"));
452 write_exp_elt_opcode (UNOP_IN_RANGE);
453 write_exp_elt_type ($4);
454 write_exp_elt_opcode (UNOP_IN_RANGE);
455 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
459 relation : simple_exp GEQ simple_exp
460 { write_exp_elt_opcode (BINOP_GEQ); }
463 relation : simple_exp '<' simple_exp
464 { write_exp_elt_opcode (BINOP_LESS); }
467 relation : simple_exp '>' simple_exp
468 { write_exp_elt_opcode (BINOP_GTR); }
480 relation _AND_ relation
481 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
482 | and_exp _AND_ relation
483 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
487 relation _AND_ THEN relation
488 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
489 | and_then_exp _AND_ THEN relation
490 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
495 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
497 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
501 relation OR ELSE relation
502 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
503 | or_else_exp OR ELSE relation
504 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
507 xor_exp : relation XOR relation
508 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
509 | xor_exp XOR relation
510 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
513 /* Primaries can denote types (OP_TYPE). In cases such as
514 primary TICK_ADDRESS, where a type would be invalid, it will be
515 caught when evaluate_subexp in ada-lang.c tries to evaluate the
516 primary, expecting a value. Precedence rules resolve the ambiguity
517 in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A
518 construct such as aType'access'access will again cause an error when
519 aType'access evaluates to a type that evaluate_subexp attempts to
521 primary : primary TICK_ACCESS
522 { write_exp_elt_opcode (UNOP_ADDR); }
523 | primary TICK_ADDRESS
524 { write_exp_elt_opcode (UNOP_ADDR);
525 write_exp_elt_opcode (UNOP_CAST);
526 write_exp_elt_type (type_system_address ());
527 write_exp_elt_opcode (UNOP_CAST);
529 | primary TICK_FIRST tick_arglist
530 { write_int ($3, type_int ());
531 write_exp_elt_opcode (OP_ATR_FIRST); }
532 | primary TICK_LAST tick_arglist
533 { write_int ($3, type_int ());
534 write_exp_elt_opcode (OP_ATR_LAST); }
535 | primary TICK_LENGTH tick_arglist
536 { write_int ($3, type_int ());
537 write_exp_elt_opcode (OP_ATR_LENGTH); }
539 { write_exp_elt_opcode (OP_ATR_SIZE); }
541 { write_exp_elt_opcode (OP_ATR_TAG); }
542 | opt_type_prefix TICK_MIN '(' exp ',' exp ')'
543 { write_exp_elt_opcode (OP_ATR_MIN); }
544 | opt_type_prefix TICK_MAX '(' exp ',' exp ')'
545 { write_exp_elt_opcode (OP_ATR_MAX); }
546 | opt_type_prefix TICK_POS '(' exp ')'
547 { write_exp_elt_opcode (OP_ATR_POS); }
548 | type_prefix TICK_VAL '(' exp ')'
549 { write_exp_elt_opcode (OP_ATR_VAL); }
550 | type_prefix TICK_MODULUS
551 { write_exp_elt_opcode (OP_ATR_MODULUS); }
554 tick_arglist : %prec '('
564 error (_("Prefix must be type"));
565 write_exp_elt_opcode (OP_TYPE);
566 write_exp_elt_type ($1);
567 write_exp_elt_opcode (OP_TYPE); }
573 { write_exp_elt_opcode (OP_TYPE);
574 write_exp_elt_type (parse_type->builtin_void);
575 write_exp_elt_opcode (OP_TYPE); }
580 { write_int ((LONGEST) $1.val, $1.type); }
584 { write_int (convert_char_literal (type_qualifier, $1.val),
585 (type_qualifier == NULL)
586 ? $1.type : type_qualifier);
591 { write_exp_elt_opcode (OP_DOUBLE);
592 write_exp_elt_type ($1.type);
593 write_exp_elt_dblcst ($1.dval);
594 write_exp_elt_opcode (OP_DOUBLE);
599 { write_int (0, type_int ()); }
604 write_exp_op_with_string (OP_STRING, $1);
608 primary : TRUEKEYWORD
609 { write_int (1, type_boolean ()); }
611 { write_int (0, type_boolean ()); }
615 { error (_("NEW not implemented.")); }
618 var_or_type: NAME %prec VAR
619 { $$ = write_var_or_type (NULL, $1); }
620 | block NAME %prec VAR
621 { $$ = write_var_or_type ($1, $2); }
624 $$ = write_var_or_type (NULL, $1);
626 write_exp_elt_opcode (UNOP_ADDR);
628 $$ = lookup_pointer_type ($$);
630 | block NAME TICK_ACCESS
632 $$ = write_var_or_type ($1, $2);
634 write_exp_elt_opcode (UNOP_ADDR);
636 $$ = lookup_pointer_type ($$);
641 block : NAME COLONCOLON
642 { $$ = block_lookup (NULL, $1.ptr); }
643 | block NAME COLONCOLON
644 { $$ = block_lookup ($1, $2.ptr); }
648 '(' aggregate_component_list ')'
650 write_exp_elt_opcode (OP_AGGREGATE);
651 write_exp_elt_longcst ($2);
652 write_exp_elt_opcode (OP_AGGREGATE);
656 aggregate_component_list :
657 component_groups { $$ = $1; }
658 | positional_list exp
659 { write_exp_elt_opcode (OP_POSITIONAL);
660 write_exp_elt_longcst ($1);
661 write_exp_elt_opcode (OP_POSITIONAL);
664 | positional_list component_groups
670 { write_exp_elt_opcode (OP_POSITIONAL);
671 write_exp_elt_longcst (0);
672 write_exp_elt_opcode (OP_POSITIONAL);
675 | positional_list exp ','
676 { write_exp_elt_opcode (OP_POSITIONAL);
677 write_exp_elt_longcst ($1);
678 write_exp_elt_opcode (OP_POSITIONAL);
685 | component_group { $$ = 1; }
686 | component_group ',' component_groups
690 others : OTHERS ARROW exp
691 { write_exp_elt_opcode (OP_OTHERS); }
695 component_associations
697 write_exp_elt_opcode (OP_CHOICES);
698 write_exp_elt_longcst ($1);
699 write_exp_elt_opcode (OP_CHOICES);
703 /* We use this somewhat obscure definition in order to handle NAME => and
704 NAME | differently from exp => and exp |. ARROW and '|' have a precedence
705 above that of the reduction of NAME to var_or_type. By delaying
706 decisions until after the => or '|', we convert the ambiguity to a
707 resolved shift/reduce conflict. */
708 component_associations :
710 { write_name_assoc ($1); }
712 | simple_exp ARROW exp
714 | simple_exp DOTDOT simple_exp ARROW
715 { write_exp_elt_opcode (OP_DISCRETE_RANGE);
716 write_exp_op_with_string (OP_NAME, empty_stoken);
720 { write_name_assoc ($1); }
721 component_associations { $$ = $4 + 1; }
723 component_associations { $$ = $3 + 1; }
724 | simple_exp DOTDOT simple_exp '|'
725 { write_exp_elt_opcode (OP_DISCRETE_RANGE); }
726 component_associations { $$ = $6 + 1; }
729 /* Some extensions borrowed from C, for the benefit of those who find they
730 can't get used to Ada notation in GDB. */
732 primary : '*' primary %prec '.'
733 { write_exp_elt_opcode (UNOP_IND); }
734 | '&' primary %prec '.'
735 { write_exp_elt_opcode (UNOP_ADDR); }
736 | primary '[' exp ']'
737 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
742 /* yylex defined in ada-lex.c: Reads one token, getting characters */
743 /* through lexptr. */
745 /* Remap normal flex interface names (yylex) as well as gratuitiously */
746 /* global symbol names, so we can have multiple flex-generated parsers */
749 /* (See note above on previous definitions for YACC.) */
751 #define yy_create_buffer ada_yy_create_buffer
752 #define yy_delete_buffer ada_yy_delete_buffer
753 #define yy_init_buffer ada_yy_init_buffer
754 #define yy_load_buffer_state ada_yy_load_buffer_state
755 #define yy_switch_to_buffer ada_yy_switch_to_buffer
756 #define yyrestart ada_yyrestart
757 #define yytext ada_yytext
758 #define yywrap ada_yywrap
760 static struct obstack temp_parse_space;
762 /* The following kludge was found necessary to prevent conflicts between */
763 /* defs.h and non-standard stdlib.h files. */
764 #define qsort __qsort__dummy
770 lexer_init (yyin); /* (Re-)initialize lexer. */
771 type_qualifier = NULL;
772 obstack_free (&temp_parse_space, NULL);
773 obstack_init (&temp_parse_space);
775 return _ada_parse ();
781 error (_("Error in expression, near `%s'."), lexptr);
784 /* The operator name corresponding to operator symbol STRING (adds
785 quotes and maps to lower-case). Destroys the previous contents of
786 the array pointed to by STRING.ptr. Error if STRING does not match
787 a valid Ada operator. Assumes that STRING.ptr points to a
788 null-terminated string and that, if STRING is a valid operator
789 symbol, the array pointed to by STRING.ptr contains at least
790 STRING.length+3 characters. */
793 string_to_operator (struct stoken string)
797 for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
799 if (string.length == strlen (ada_opname_table[i].decoded)-2
800 && strncasecmp (string.ptr, ada_opname_table[i].decoded+1,
803 strncpy (string.ptr, ada_opname_table[i].decoded,
809 error (_("Invalid operator symbol `%s'"), string.ptr);
812 /* Emit expression to access an instance of SYM, in block BLOCK (if
813 * non-NULL), and with :: qualification ORIG_LEFT_CONTEXT. */
815 write_var_from_sym (struct block *orig_left_context,
819 if (orig_left_context == NULL && symbol_read_needs_frame (sym))
821 if (innermost_block == 0
822 || contained_in (block, innermost_block))
823 innermost_block = block;
826 write_exp_elt_opcode (OP_VAR_VALUE);
827 write_exp_elt_block (block);
828 write_exp_elt_sym (sym);
829 write_exp_elt_opcode (OP_VAR_VALUE);
832 /* Write integer or boolean constant ARG of type TYPE. */
835 write_int (LONGEST arg, struct type *type)
837 write_exp_elt_opcode (OP_LONG);
838 write_exp_elt_type (type);
839 write_exp_elt_longcst (arg);
840 write_exp_elt_opcode (OP_LONG);
843 /* Write an OPCODE, string, OPCODE sequence to the current expression. */
845 write_exp_op_with_string (enum exp_opcode opcode, struct stoken token)
847 write_exp_elt_opcode (opcode);
848 write_exp_string (token);
849 write_exp_elt_opcode (opcode);
852 /* Emit expression corresponding to the renamed object named
853 * designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
854 * context of ORIG_LEFT_CONTEXT, to which is applied the operations
855 * encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of
856 * cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it
857 * defaults to the currently selected block. ORIG_SYMBOL is the
858 * symbol that originally encoded the renaming. It is needed only
859 * because its prefix also qualifies any index variables used to index
860 * or slice an array. It should not be necessary once we go to the
861 * new encoding entirely (FIXME pnh 7/20/2007). */
864 write_object_renaming (struct block *orig_left_context,
865 const char *renamed_entity, int renamed_entity_len,
866 const char *renaming_expr, int max_depth)
869 enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state;
874 error (_("Could not find renamed symbol"));
876 if (orig_left_context == NULL)
877 orig_left_context = get_selected_block (NULL);
879 name = obsavestring (renamed_entity, renamed_entity_len, &temp_parse_space);
880 sym = ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN,
883 error (_("Could not find renamed variable: %s"), ada_decode (name));
884 else if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
885 /* We have a renaming of an old-style renaming symbol. Don't
886 trust the block information. */
887 block = orig_left_context;
890 const char *inner_renamed_entity;
891 int inner_renamed_entity_len;
892 const char *inner_renaming_expr;
894 switch (ada_parse_renaming (sym, &inner_renamed_entity,
895 &inner_renamed_entity_len,
896 &inner_renaming_expr))
898 case ADA_NOT_RENAMING:
899 write_var_from_sym (orig_left_context, block, sym);
901 case ADA_OBJECT_RENAMING:
902 write_object_renaming (block,
903 inner_renamed_entity, inner_renamed_entity_len,
904 inner_renaming_expr, max_depth - 1);
911 slice_state = SIMPLE_INDEX;
912 while (*renaming_expr == 'X')
916 switch (*renaming_expr) {
919 write_exp_elt_opcode (UNOP_IND);
922 slice_state = LOWER_BOUND;
926 if (isdigit (*renaming_expr))
929 long val = strtol (renaming_expr, &next, 10);
930 if (next == renaming_expr)
932 renaming_expr = next;
933 write_exp_elt_opcode (OP_LONG);
934 write_exp_elt_type (type_int ());
935 write_exp_elt_longcst ((LONGEST) val);
936 write_exp_elt_opcode (OP_LONG);
942 struct symbol *index_sym;
944 end = strchr (renaming_expr, 'X');
946 end = renaming_expr + strlen (renaming_expr);
949 obsavestring (renaming_expr, end - renaming_expr,
953 index_sym = ada_lookup_encoded_symbol (index_name, NULL,
955 if (index_sym == NULL)
956 error (_("Could not find %s"), index_name);
957 else if (SYMBOL_CLASS (index_sym) == LOC_TYPEDEF)
958 /* Index is an old-style renaming symbol. */
959 block = orig_left_context;
960 write_var_from_sym (NULL, block, index_sym);
962 if (slice_state == SIMPLE_INDEX)
964 write_exp_elt_opcode (OP_FUNCALL);
965 write_exp_elt_longcst ((LONGEST) 1);
966 write_exp_elt_opcode (OP_FUNCALL);
968 else if (slice_state == LOWER_BOUND)
969 slice_state = UPPER_BOUND;
970 else if (slice_state == UPPER_BOUND)
972 write_exp_elt_opcode (TERNOP_SLICE);
973 slice_state = SIMPLE_INDEX;
979 struct stoken field_name;
983 if (slice_state != SIMPLE_INDEX)
985 end = strchr (renaming_expr, 'X');
987 end = renaming_expr + strlen (renaming_expr);
988 field_name.length = end - renaming_expr;
989 field_name.ptr = malloc (end - renaming_expr + 1);
990 strncpy (field_name.ptr, renaming_expr, end - renaming_expr);
991 field_name.ptr[end - renaming_expr] = '\000';
993 write_exp_op_with_string (STRUCTOP_STRUCT, field_name);
1001 if (slice_state == SIMPLE_INDEX)
1005 error (_("Internal error in encoding of renaming declaration"));
1008 static struct block*
1009 block_lookup (struct block *context, char *raw_name)
1012 struct ada_symbol_info *syms;
1014 struct symtab *symtab;
1016 if (raw_name[0] == '\'')
1022 name = ada_encode (raw_name);
1024 nsyms = ada_lookup_symbol_list (name, context, VAR_DOMAIN, &syms);
1026 && (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK))
1027 symtab = lookup_symtab (name);
1032 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1033 else if (nsyms == 0 || SYMBOL_CLASS (syms[0].sym) != LOC_BLOCK)
1035 if (context == NULL)
1036 error (_("No file or function \"%s\"."), raw_name);
1038 error (_("No function \"%s\" in specified context."), raw_name);
1043 warning (_("Function name \"%s\" ambiguous here"), raw_name);
1044 return SYMBOL_BLOCK_VALUE (syms[0].sym);
1048 static struct symbol*
1049 select_possible_type_sym (struct ada_symbol_info *syms, int nsyms)
1052 int preferred_index;
1053 struct type *preferred_type;
1055 preferred_index = -1; preferred_type = NULL;
1056 for (i = 0; i < nsyms; i += 1)
1057 switch (SYMBOL_CLASS (syms[i].sym))
1060 if (ada_prefer_type (SYMBOL_TYPE (syms[i].sym), preferred_type))
1062 preferred_index = i;
1063 preferred_type = SYMBOL_TYPE (syms[i].sym);
1069 case LOC_REGPARM_ADDR:
1076 if (preferred_type == NULL)
1078 return syms[preferred_index].sym;
1082 find_primitive_type (char *name)
1085 type = language_lookup_primitive_type_by_name (parse_language,
1088 if (type == NULL && strcmp ("system__address", name) == 0)
1089 type = type_system_address ();
1093 /* Check to see if we have a regular definition of this
1094 type that just didn't happen to have been read yet. */
1096 char *expanded_name =
1097 (char *) alloca (strlen (name) + sizeof ("standard__"));
1098 strcpy (expanded_name, "standard__");
1099 strcat (expanded_name, name);
1100 sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN, NULL);
1101 if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1102 type = SYMBOL_TYPE (sym);
1109 chop_selector (char *name, int end)
1112 for (i = end - 1; i > 0; i -= 1)
1113 if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_'))
1118 /* If NAME is a string beginning with a separator (either '__', or
1119 '.'), chop this separator and return the result; else, return
1123 chop_separator (char *name)
1128 if (name[0] == '_' && name[1] == '_')
1134 /* Given that SELS is a string of the form (<sep><identifier>)*, where
1135 <sep> is '__' or '.', write the indicated sequence of
1136 STRUCTOP_STRUCT expression operators. */
1138 write_selectors (char *sels)
1140 while (*sels != '\0')
1142 struct stoken field_name;
1143 char *p = chop_separator (sels);
1145 while (*sels != '\0' && *sels != '.'
1146 && (sels[0] != '_' || sels[1] != '_'))
1148 field_name.length = sels - p;
1150 write_exp_op_with_string (STRUCTOP_STRUCT, field_name);
1154 /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name
1155 NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes
1156 a temporary symbol that is valid until the next call to ada_parse.
1159 write_ambiguous_var (struct block *block, char *name, int len)
1161 struct symbol *sym =
1162 obstack_alloc (&temp_parse_space, sizeof (struct symbol));
1163 memset (sym, 0, sizeof (struct symbol));
1164 SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
1165 SYMBOL_LINKAGE_NAME (sym) = obsavestring (name, len, &temp_parse_space);
1166 SYMBOL_LANGUAGE (sym) = language_ada;
1168 write_exp_elt_opcode (OP_VAR_VALUE);
1169 write_exp_elt_block (block);
1170 write_exp_elt_sym (sym);
1171 write_exp_elt_opcode (OP_VAR_VALUE);
1174 /* A convenient wrapper around ada_get_field_index that takes
1175 a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead
1176 of a NUL-terminated field name. */
1179 ada_nget_field_index (const struct type *type, const char *field_name0,
1180 int field_name_len, int maybe_missing)
1182 char *field_name = alloca ((field_name_len + 1) * sizeof (char));
1184 strncpy (field_name, field_name0, field_name_len);
1185 field_name[field_name_len] = '\0';
1186 return ada_get_field_index (type, field_name, maybe_missing);
1189 /* If encoded_field_name is the name of a field inside symbol SYM,
1190 then return the type of that field. Otherwise, return NULL.
1192 This function is actually recursive, so if ENCODED_FIELD_NAME
1193 doesn't match one of the fields of our symbol, then try to see
1194 if ENCODED_FIELD_NAME could not be a succession of field names
1195 (in other words, the user entered an expression of the form
1196 TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate
1197 each field name sequentially to obtain the desired field type.
1198 In case of failure, we return NULL. */
1200 static struct type *
1201 get_symbol_field_type (struct symbol *sym, char *encoded_field_name)
1203 char *field_name = encoded_field_name;
1204 char *subfield_name;
1205 struct type *type = SYMBOL_TYPE (sym);
1208 if (type == NULL || field_name == NULL)
1210 type = check_typedef (type);
1212 while (field_name[0] != '\0')
1214 field_name = chop_separator (field_name);
1216 fieldno = ada_get_field_index (type, field_name, 1);
1218 return TYPE_FIELD_TYPE (type, fieldno);
1220 subfield_name = field_name;
1221 while (*subfield_name != '\0' && *subfield_name != '.'
1222 && (subfield_name[0] != '_' || subfield_name[1] != '_'))
1225 if (subfield_name[0] == '\0')
1228 fieldno = ada_nget_field_index (type, field_name,
1229 subfield_name - field_name, 1);
1233 type = TYPE_FIELD_TYPE (type, fieldno);
1234 field_name = subfield_name;
1240 /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or
1241 expression_block_context if NULL). If it denotes a type, return
1242 that type. Otherwise, write expression code to evaluate it as an
1243 object and return NULL. In this second case, NAME0 will, in general,
1244 have the form <name>(.<selector_name>)*, where <name> is an object
1245 or renaming encoded in the debugging data. Calls error if no
1246 prefix <name> matches a name in the debugging data (i.e., matches
1247 either a complete name or, as a wild-card match, the final
1251 write_var_or_type (struct block *block, struct stoken name0)
1258 block = expression_context_block;
1260 encoded_name = ada_encode (name0.ptr);
1261 name_len = strlen (encoded_name);
1262 encoded_name = obsavestring (encoded_name, name_len, &temp_parse_space);
1263 for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
1267 tail_index = name_len;
1268 while (tail_index > 0)
1271 struct ada_symbol_info *syms;
1272 struct symbol *type_sym;
1273 struct symbol *renaming_sym;
1274 const char* renaming;
1276 const char* renaming_expr;
1277 int terminator = encoded_name[tail_index];
1279 encoded_name[tail_index] = '\0';
1280 nsyms = ada_lookup_symbol_list (encoded_name, block,
1282 encoded_name[tail_index] = terminator;
1284 /* A single symbol may rename a package or object. */
1286 /* This should go away when we move entirely to new version.
1287 FIXME pnh 7/20/2007. */
1290 struct symbol *ren_sym =
1291 ada_find_renaming_symbol (SYMBOL_LINKAGE_NAME (syms[0].sym),
1294 if (ren_sym != NULL)
1295 syms[0].sym = ren_sym;
1298 type_sym = select_possible_type_sym (syms, nsyms);
1300 if (type_sym != NULL)
1301 renaming_sym = type_sym;
1302 else if (nsyms == 1)
1303 renaming_sym = syms[0].sym;
1305 renaming_sym = NULL;
1307 switch (ada_parse_renaming (renaming_sym, &renaming,
1308 &renaming_len, &renaming_expr))
1310 case ADA_NOT_RENAMING:
1312 case ADA_PACKAGE_RENAMING:
1313 case ADA_EXCEPTION_RENAMING:
1314 case ADA_SUBPROGRAM_RENAMING:
1317 = obstack_alloc (&temp_parse_space,
1318 renaming_len + name_len - tail_index + 1);
1319 strncpy (new_name, renaming, renaming_len);
1320 strcpy (new_name + renaming_len, encoded_name + tail_index);
1321 encoded_name = new_name;
1322 name_len = renaming_len + name_len - tail_index;
1323 goto TryAfterRenaming;
1325 case ADA_OBJECT_RENAMING:
1326 write_object_renaming (block, renaming, renaming_len,
1327 renaming_expr, MAX_RENAMING_CHAIN_LENGTH);
1328 write_selectors (encoded_name + tail_index);
1331 internal_error (__FILE__, __LINE__,
1332 _("impossible value from ada_parse_renaming"));
1335 if (type_sym != NULL)
1337 struct type *field_type;
1339 if (tail_index == name_len)
1340 return SYMBOL_TYPE (type_sym);
1342 /* We have some extraneous characters after the type name.
1343 If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN",
1344 then try to get the type of FIELDN. */
1346 = get_symbol_field_type (type_sym, encoded_name + tail_index);
1347 if (field_type != NULL)
1350 error (_("Invalid attempt to select from type: \"%s\"."),
1353 else if (tail_index == name_len && nsyms == 0)
1355 struct type *type = find_primitive_type (encoded_name);
1363 write_var_from_sym (block, syms[0].block, syms[0].sym);
1364 write_selectors (encoded_name + tail_index);
1367 else if (nsyms == 0)
1369 struct minimal_symbol *msym
1370 = ada_lookup_simple_minsym (encoded_name);
1373 write_exp_msymbol (msym);
1374 /* Maybe cause error here rather than later? FIXME? */
1375 write_selectors (encoded_name + tail_index);
1379 if (tail_index == name_len
1380 && strncmp (encoded_name, "standard__",
1381 sizeof ("standard__") - 1) == 0)
1382 error (_("No definition of \"%s\" found."), name0.ptr);
1384 tail_index = chop_selector (encoded_name, tail_index);
1388 write_ambiguous_var (block, encoded_name, tail_index);
1389 write_selectors (encoded_name + tail_index);
1394 if (!have_full_symbols () && !have_partial_symbols () && block == NULL)
1395 error (_("No symbol table is loaded. Use the \"file\" command."));
1396 if (block == expression_context_block)
1397 error (_("No definition of \"%s\" in current context."), name0.ptr);
1399 error (_("No definition of \"%s\" in specified context."), name0.ptr);
1404 error (_("Could not find renamed symbol \"%s\""), name0.ptr);
1408 /* Write a left side of a component association (e.g., NAME in NAME =>
1409 exp). If NAME has the form of a selected component, write it as an
1410 ordinary expression. If it is a simple variable that unambiguously
1411 corresponds to exactly one symbol that does not denote a type or an
1412 object renaming, also write it normally as an OP_VAR_VALUE.
1413 Otherwise, write it as an OP_NAME.
1415 Unfortunately, we don't know at this point whether NAME is supposed
1416 to denote a record component name or the value of an array index.
1417 Therefore, it is not appropriate to disambiguate an ambiguous name
1418 as we normally would, nor to replace a renaming with its referent.
1419 As a result, in the (one hopes) rare case that one writes an
1420 aggregate such as (R => 42) where R renames an object or is an
1421 ambiguous name, one must write instead ((R) => 42). */
1424 write_name_assoc (struct stoken name)
1426 if (strchr (name.ptr, '.') == NULL)
1428 struct ada_symbol_info *syms;
1429 int nsyms = ada_lookup_symbol_list (name.ptr, expression_context_block,
1431 if (nsyms != 1 || SYMBOL_CLASS (syms[0].sym) == LOC_TYPEDEF)
1432 write_exp_op_with_string (OP_NAME, name);
1434 write_var_from_sym (NULL, syms[0].block, syms[0].sym);
1437 if (write_var_or_type (NULL, name) != NULL)
1438 error (_("Invalid use of type."));
1441 /* Convert the character literal whose ASCII value would be VAL to the
1442 appropriate value of type TYPE, if there is a translation.
1443 Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
1444 the literal 'A' (VAL == 65), returns 0. */
1447 convert_char_literal (struct type *type, LONGEST val)
1454 type = check_typedef (type);
1455 if (TYPE_CODE (type) != TYPE_CODE_ENUM)
1458 xsnprintf (name, sizeof (name), "QU%02x", (int) val);
1459 for (f = 0; f < TYPE_NFIELDS (type); f += 1)
1461 if (strcmp (name, TYPE_FIELD_NAME (type, f)) == 0)
1462 return TYPE_FIELD_BITPOS (type, f);
1467 static struct type *
1470 return parse_type->builtin_int;
1473 static struct type *
1476 return parse_type->builtin_long;
1479 static struct type *
1480 type_long_long (void)
1482 return parse_type->builtin_long_long;
1485 static struct type *
1488 return parse_type->builtin_float;
1491 static struct type *
1494 return parse_type->builtin_double;
1497 static struct type *
1498 type_long_double (void)
1500 return parse_type->builtin_long_double;
1503 static struct type *
1506 return language_string_char_type (parse_language, parse_gdbarch);
1509 static struct type *
1512 return parse_type->builtin_bool;
1515 static struct type *
1516 type_system_address (void)
1519 = language_lookup_primitive_type_by_name (parse_language,
1522 return type != NULL ? type : parse_type->builtin_data_ptr;
1525 /* Provide a prototype to silence -Wmissing-prototypes. */
1526 extern initialize_file_ftype _initialize_ada_exp;
1529 _initialize_ada_exp (void)
1531 obstack_init (&temp_parse_space);
1534 /* FIXME: hilfingr/2004-10-05: Hack to remove warning. The function
1535 string_to_operator is supposed to be used for cases where one
1536 calls an operator function with prefix notation, as in
1537 "+" (a, b), but at some point, this code seems to have gone
1540 struct stoken (*dummy_string_to_ada_operator) (struct stoken)
1541 = string_to_operator;