| 1 | /* YACC parser for Ada expressions, for GDB. |
| 2 | Copyright (C) 1986-2020 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* Parse an Ada expression from text in a string, |
| 20 | and return the result as a struct expression pointer. |
| 21 | That structure contains arithmetic operations in reverse polish, |
| 22 | with constants represented by operations that are followed by special data. |
| 23 | See expression.h for the details of the format. |
| 24 | What is important here is that it can be built up sequentially |
| 25 | during the process of parsing; the lower levels of the tree always |
| 26 | come first in the result. |
| 27 | |
| 28 | malloc's and realloc's in this file are transformed to |
| 29 | xmalloc and xrealloc respectively by the same sed command in the |
| 30 | makefile that remaps any other malloc/realloc inserted by the parser |
| 31 | generator. Doing this with #defines and trying to control the interaction |
| 32 | with include files (<malloc.h> and <stdlib.h> for example) just became |
| 33 | too messy, particularly when such includes can be inserted at random |
| 34 | times by the parser generator. */ |
| 35 | |
| 36 | %{ |
| 37 | |
| 38 | #include "defs.h" |
| 39 | #include <ctype.h> |
| 40 | #include "expression.h" |
| 41 | #include "value.h" |
| 42 | #include "parser-defs.h" |
| 43 | #include "language.h" |
| 44 | #include "ada-lang.h" |
| 45 | #include "bfd.h" /* Required by objfiles.h. */ |
| 46 | #include "symfile.h" /* Required by objfiles.h. */ |
| 47 | #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */ |
| 48 | #include "frame.h" |
| 49 | #include "block.h" |
| 50 | |
| 51 | #define parse_type(ps) builtin_type (ps->gdbarch ()) |
| 52 | |
| 53 | /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, |
| 54 | etc). */ |
| 55 | #define GDB_YY_REMAP_PREFIX ada_ |
| 56 | #include "yy-remap.h" |
| 57 | |
| 58 | struct name_info { |
| 59 | struct symbol *sym; |
| 60 | struct minimal_symbol *msym; |
| 61 | const struct block *block; |
| 62 | struct stoken stoken; |
| 63 | }; |
| 64 | |
| 65 | /* The state of the parser, used internally when we are parsing the |
| 66 | expression. */ |
| 67 | |
| 68 | static struct parser_state *pstate = NULL; |
| 69 | |
| 70 | static struct stoken empty_stoken = { "", 0 }; |
| 71 | |
| 72 | /* If expression is in the context of TYPE'(...), then TYPE, else |
| 73 | * NULL. */ |
| 74 | static struct type *type_qualifier; |
| 75 | |
| 76 | int yyparse (void); |
| 77 | |
| 78 | static int yylex (void); |
| 79 | |
| 80 | static void yyerror (const char *); |
| 81 | |
| 82 | static void write_int (struct parser_state *, LONGEST, struct type *); |
| 83 | |
| 84 | static void write_object_renaming (struct parser_state *, |
| 85 | const struct block *, const char *, int, |
| 86 | const char *, int); |
| 87 | |
| 88 | static struct type* write_var_or_type (struct parser_state *, |
| 89 | const struct block *, struct stoken); |
| 90 | |
| 91 | static void write_name_assoc (struct parser_state *, struct stoken); |
| 92 | |
| 93 | static void write_exp_op_with_string (struct parser_state *, enum exp_opcode, |
| 94 | struct stoken); |
| 95 | |
| 96 | static const struct block *block_lookup (const struct block *, const char *); |
| 97 | |
| 98 | static LONGEST convert_char_literal (struct type *, LONGEST); |
| 99 | |
| 100 | static void write_ambiguous_var (struct parser_state *, |
| 101 | const struct block *, char *, int); |
| 102 | |
| 103 | static struct type *type_int (struct parser_state *); |
| 104 | |
| 105 | static struct type *type_long (struct parser_state *); |
| 106 | |
| 107 | static struct type *type_long_long (struct parser_state *); |
| 108 | |
| 109 | static struct type *type_long_double (struct parser_state *); |
| 110 | |
| 111 | static struct type *type_char (struct parser_state *); |
| 112 | |
| 113 | static struct type *type_boolean (struct parser_state *); |
| 114 | |
| 115 | static struct type *type_system_address (struct parser_state *); |
| 116 | |
| 117 | %} |
| 118 | |
| 119 | %union |
| 120 | { |
| 121 | LONGEST lval; |
| 122 | struct { |
| 123 | LONGEST val; |
| 124 | struct type *type; |
| 125 | } typed_val; |
| 126 | struct { |
| 127 | gdb_byte val[16]; |
| 128 | struct type *type; |
| 129 | } typed_val_float; |
| 130 | struct type *tval; |
| 131 | struct stoken sval; |
| 132 | const struct block *bval; |
| 133 | struct internalvar *ivar; |
| 134 | } |
| 135 | |
| 136 | %type <lval> positional_list component_groups component_associations |
| 137 | %type <lval> aggregate_component_list |
| 138 | %type <tval> var_or_type |
| 139 | |
| 140 | %token <typed_val> INT NULL_PTR CHARLIT |
| 141 | %token <typed_val_float> FLOAT |
| 142 | %token TRUEKEYWORD FALSEKEYWORD |
| 143 | %token COLONCOLON |
| 144 | %token <sval> STRING NAME DOT_ID |
| 145 | %type <bval> block |
| 146 | %type <lval> arglist tick_arglist |
| 147 | |
| 148 | %type <tval> save_qualifier |
| 149 | |
| 150 | %token DOT_ALL |
| 151 | |
| 152 | /* Special type cases, put in to allow the parser to distinguish different |
| 153 | legal basetypes. */ |
| 154 | %token <sval> DOLLAR_VARIABLE |
| 155 | |
| 156 | %nonassoc ASSIGN |
| 157 | %left _AND_ OR XOR THEN ELSE |
| 158 | %left '=' NOTEQUAL '<' '>' LEQ GEQ IN DOTDOT |
| 159 | %left '@' |
| 160 | %left '+' '-' '&' |
| 161 | %left UNARY |
| 162 | %left '*' '/' MOD REM |
| 163 | %right STARSTAR ABS NOT |
| 164 | |
| 165 | /* Artificial token to give NAME => ... and NAME | priority over reducing |
| 166 | NAME to <primary> and to give <primary>' priority over reducing <primary> |
| 167 | to <simple_exp>. */ |
| 168 | %nonassoc VAR |
| 169 | |
| 170 | %nonassoc ARROW '|' |
| 171 | |
| 172 | %right TICK_ACCESS TICK_ADDRESS TICK_FIRST TICK_LAST TICK_LENGTH |
| 173 | %right TICK_MAX TICK_MIN TICK_MODULUS |
| 174 | %right TICK_POS TICK_RANGE TICK_SIZE TICK_TAG TICK_VAL |
| 175 | /* The following are right-associative only so that reductions at this |
| 176 | precedence have lower precedence than '.' and '('. The syntax still |
| 177 | forces a.b.c, e.g., to be LEFT-associated. */ |
| 178 | %right '.' '(' '[' DOT_ID DOT_ALL |
| 179 | |
| 180 | %token NEW OTHERS |
| 181 | |
| 182 | \f |
| 183 | %% |
| 184 | |
| 185 | start : exp1 |
| 186 | ; |
| 187 | |
| 188 | /* Expressions, including the sequencing operator. */ |
| 189 | exp1 : exp |
| 190 | | exp1 ';' exp |
| 191 | { write_exp_elt_opcode (pstate, BINOP_COMMA); } |
| 192 | | primary ASSIGN exp /* Extension for convenience */ |
| 193 | { write_exp_elt_opcode (pstate, BINOP_ASSIGN); } |
| 194 | ; |
| 195 | |
| 196 | /* Expressions, not including the sequencing operator. */ |
| 197 | primary : primary DOT_ALL |
| 198 | { write_exp_elt_opcode (pstate, UNOP_IND); } |
| 199 | ; |
| 200 | |
| 201 | primary : primary DOT_ID |
| 202 | { write_exp_op_with_string (pstate, STRUCTOP_STRUCT, |
| 203 | $2); } |
| 204 | ; |
| 205 | |
| 206 | primary : primary '(' arglist ')' |
| 207 | { |
| 208 | write_exp_elt_opcode (pstate, OP_FUNCALL); |
| 209 | write_exp_elt_longcst (pstate, $3); |
| 210 | write_exp_elt_opcode (pstate, OP_FUNCALL); |
| 211 | } |
| 212 | | var_or_type '(' arglist ')' |
| 213 | { |
| 214 | if ($1 != NULL) |
| 215 | { |
| 216 | if ($3 != 1) |
| 217 | error (_("Invalid conversion")); |
| 218 | write_exp_elt_opcode (pstate, UNOP_CAST); |
| 219 | write_exp_elt_type (pstate, $1); |
| 220 | write_exp_elt_opcode (pstate, UNOP_CAST); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | write_exp_elt_opcode (pstate, OP_FUNCALL); |
| 225 | write_exp_elt_longcst (pstate, $3); |
| 226 | write_exp_elt_opcode (pstate, OP_FUNCALL); |
| 227 | } |
| 228 | } |
| 229 | ; |
| 230 | |
| 231 | primary : var_or_type '\'' save_qualifier { type_qualifier = $1; } |
| 232 | '(' exp ')' |
| 233 | { |
| 234 | if ($1 == NULL) |
| 235 | error (_("Type required for qualification")); |
| 236 | write_exp_elt_opcode (pstate, UNOP_QUAL); |
| 237 | write_exp_elt_type (pstate, $1); |
| 238 | write_exp_elt_opcode (pstate, UNOP_QUAL); |
| 239 | type_qualifier = $3; |
| 240 | } |
| 241 | ; |
| 242 | |
| 243 | save_qualifier : { $$ = type_qualifier; } |
| 244 | ; |
| 245 | |
| 246 | primary : |
| 247 | primary '(' simple_exp DOTDOT simple_exp ')' |
| 248 | { write_exp_elt_opcode (pstate, TERNOP_SLICE); } |
| 249 | | var_or_type '(' simple_exp DOTDOT simple_exp ')' |
| 250 | { if ($1 == NULL) |
| 251 | write_exp_elt_opcode (pstate, TERNOP_SLICE); |
| 252 | else |
| 253 | error (_("Cannot slice a type")); |
| 254 | } |
| 255 | ; |
| 256 | |
| 257 | primary : '(' exp1 ')' { } |
| 258 | ; |
| 259 | |
| 260 | /* The following rule causes a conflict with the type conversion |
| 261 | var_or_type (exp) |
| 262 | To get around it, we give '(' higher priority and add bridge rules for |
| 263 | var_or_type (exp, exp, ...) |
| 264 | var_or_type (exp .. exp) |
| 265 | We also have the action for var_or_type(exp) generate a function call |
| 266 | when the first symbol does not denote a type. */ |
| 267 | |
| 268 | primary : var_or_type %prec VAR |
| 269 | { if ($1 != NULL) |
| 270 | { |
| 271 | write_exp_elt_opcode (pstate, OP_TYPE); |
| 272 | write_exp_elt_type (pstate, $1); |
| 273 | write_exp_elt_opcode (pstate, OP_TYPE); |
| 274 | } |
| 275 | } |
| 276 | ; |
| 277 | |
| 278 | primary : DOLLAR_VARIABLE /* Various GDB extensions */ |
| 279 | { write_dollar_variable (pstate, $1); } |
| 280 | ; |
| 281 | |
| 282 | primary : aggregate |
| 283 | ; |
| 284 | |
| 285 | simple_exp : primary |
| 286 | ; |
| 287 | |
| 288 | simple_exp : '-' simple_exp %prec UNARY |
| 289 | { write_exp_elt_opcode (pstate, UNOP_NEG); } |
| 290 | ; |
| 291 | |
| 292 | simple_exp : '+' simple_exp %prec UNARY |
| 293 | { write_exp_elt_opcode (pstate, UNOP_PLUS); } |
| 294 | ; |
| 295 | |
| 296 | simple_exp : NOT simple_exp %prec UNARY |
| 297 | { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); } |
| 298 | ; |
| 299 | |
| 300 | simple_exp : ABS simple_exp %prec UNARY |
| 301 | { write_exp_elt_opcode (pstate, UNOP_ABS); } |
| 302 | ; |
| 303 | |
| 304 | arglist : { $$ = 0; } |
| 305 | ; |
| 306 | |
| 307 | arglist : exp |
| 308 | { $$ = 1; } |
| 309 | | NAME ARROW exp |
| 310 | { $$ = 1; } |
| 311 | | arglist ',' exp |
| 312 | { $$ = $1 + 1; } |
| 313 | | arglist ',' NAME ARROW exp |
| 314 | { $$ = $1 + 1; } |
| 315 | ; |
| 316 | |
| 317 | primary : '{' var_or_type '}' primary %prec '.' |
| 318 | /* GDB extension */ |
| 319 | { |
| 320 | if ($2 == NULL) |
| 321 | error (_("Type required within braces in coercion")); |
| 322 | write_exp_elt_opcode (pstate, UNOP_MEMVAL); |
| 323 | write_exp_elt_type (pstate, $2); |
| 324 | write_exp_elt_opcode (pstate, UNOP_MEMVAL); |
| 325 | } |
| 326 | ; |
| 327 | |
| 328 | /* Binary operators in order of decreasing precedence. */ |
| 329 | |
| 330 | simple_exp : simple_exp STARSTAR simple_exp |
| 331 | { write_exp_elt_opcode (pstate, BINOP_EXP); } |
| 332 | ; |
| 333 | |
| 334 | simple_exp : simple_exp '*' simple_exp |
| 335 | { write_exp_elt_opcode (pstate, BINOP_MUL); } |
| 336 | ; |
| 337 | |
| 338 | simple_exp : simple_exp '/' simple_exp |
| 339 | { write_exp_elt_opcode (pstate, BINOP_DIV); } |
| 340 | ; |
| 341 | |
| 342 | simple_exp : simple_exp REM simple_exp /* May need to be fixed to give correct Ada REM */ |
| 343 | { write_exp_elt_opcode (pstate, BINOP_REM); } |
| 344 | ; |
| 345 | |
| 346 | simple_exp : simple_exp MOD simple_exp |
| 347 | { write_exp_elt_opcode (pstate, BINOP_MOD); } |
| 348 | ; |
| 349 | |
| 350 | simple_exp : simple_exp '@' simple_exp /* GDB extension */ |
| 351 | { write_exp_elt_opcode (pstate, BINOP_REPEAT); } |
| 352 | ; |
| 353 | |
| 354 | simple_exp : simple_exp '+' simple_exp |
| 355 | { write_exp_elt_opcode (pstate, BINOP_ADD); } |
| 356 | ; |
| 357 | |
| 358 | simple_exp : simple_exp '&' simple_exp |
| 359 | { write_exp_elt_opcode (pstate, BINOP_CONCAT); } |
| 360 | ; |
| 361 | |
| 362 | simple_exp : simple_exp '-' simple_exp |
| 363 | { write_exp_elt_opcode (pstate, BINOP_SUB); } |
| 364 | ; |
| 365 | |
| 366 | relation : simple_exp |
| 367 | ; |
| 368 | |
| 369 | relation : simple_exp '=' simple_exp |
| 370 | { write_exp_elt_opcode (pstate, BINOP_EQUAL); } |
| 371 | ; |
| 372 | |
| 373 | relation : simple_exp NOTEQUAL simple_exp |
| 374 | { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); } |
| 375 | ; |
| 376 | |
| 377 | relation : simple_exp LEQ simple_exp |
| 378 | { write_exp_elt_opcode (pstate, BINOP_LEQ); } |
| 379 | ; |
| 380 | |
| 381 | relation : simple_exp IN simple_exp DOTDOT simple_exp |
| 382 | { write_exp_elt_opcode (pstate, TERNOP_IN_RANGE); } |
| 383 | | simple_exp IN primary TICK_RANGE tick_arglist |
| 384 | { write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS); |
| 385 | write_exp_elt_longcst (pstate, (LONGEST) $5); |
| 386 | write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS); |
| 387 | } |
| 388 | | simple_exp IN var_or_type %prec TICK_ACCESS |
| 389 | { |
| 390 | if ($3 == NULL) |
| 391 | error (_("Right operand of 'in' must be type")); |
| 392 | write_exp_elt_opcode (pstate, UNOP_IN_RANGE); |
| 393 | write_exp_elt_type (pstate, $3); |
| 394 | write_exp_elt_opcode (pstate, UNOP_IN_RANGE); |
| 395 | } |
| 396 | | simple_exp NOT IN simple_exp DOTDOT simple_exp |
| 397 | { write_exp_elt_opcode (pstate, TERNOP_IN_RANGE); |
| 398 | write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); |
| 399 | } |
| 400 | | simple_exp NOT IN primary TICK_RANGE tick_arglist |
| 401 | { write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS); |
| 402 | write_exp_elt_longcst (pstate, (LONGEST) $6); |
| 403 | write_exp_elt_opcode (pstate, BINOP_IN_BOUNDS); |
| 404 | write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); |
| 405 | } |
| 406 | | simple_exp NOT IN var_or_type %prec TICK_ACCESS |
| 407 | { |
| 408 | if ($4 == NULL) |
| 409 | error (_("Right operand of 'in' must be type")); |
| 410 | write_exp_elt_opcode (pstate, UNOP_IN_RANGE); |
| 411 | write_exp_elt_type (pstate, $4); |
| 412 | write_exp_elt_opcode (pstate, UNOP_IN_RANGE); |
| 413 | write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); |
| 414 | } |
| 415 | ; |
| 416 | |
| 417 | relation : simple_exp GEQ simple_exp |
| 418 | { write_exp_elt_opcode (pstate, BINOP_GEQ); } |
| 419 | ; |
| 420 | |
| 421 | relation : simple_exp '<' simple_exp |
| 422 | { write_exp_elt_opcode (pstate, BINOP_LESS); } |
| 423 | ; |
| 424 | |
| 425 | relation : simple_exp '>' simple_exp |
| 426 | { write_exp_elt_opcode (pstate, BINOP_GTR); } |
| 427 | ; |
| 428 | |
| 429 | exp : relation |
| 430 | | and_exp |
| 431 | | and_then_exp |
| 432 | | or_exp |
| 433 | | or_else_exp |
| 434 | | xor_exp |
| 435 | ; |
| 436 | |
| 437 | and_exp : |
| 438 | relation _AND_ relation |
| 439 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); } |
| 440 | | and_exp _AND_ relation |
| 441 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); } |
| 442 | ; |
| 443 | |
| 444 | and_then_exp : |
| 445 | relation _AND_ THEN relation |
| 446 | { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); } |
| 447 | | and_then_exp _AND_ THEN relation |
| 448 | { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); } |
| 449 | ; |
| 450 | |
| 451 | or_exp : |
| 452 | relation OR relation |
| 453 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); } |
| 454 | | or_exp OR relation |
| 455 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); } |
| 456 | ; |
| 457 | |
| 458 | or_else_exp : |
| 459 | relation OR ELSE relation |
| 460 | { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); } |
| 461 | | or_else_exp OR ELSE relation |
| 462 | { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); } |
| 463 | ; |
| 464 | |
| 465 | xor_exp : relation XOR relation |
| 466 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); } |
| 467 | | xor_exp XOR relation |
| 468 | { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); } |
| 469 | ; |
| 470 | |
| 471 | /* Primaries can denote types (OP_TYPE). In cases such as |
| 472 | primary TICK_ADDRESS, where a type would be invalid, it will be |
| 473 | caught when evaluate_subexp in ada-lang.c tries to evaluate the |
| 474 | primary, expecting a value. Precedence rules resolve the ambiguity |
| 475 | in NAME TICK_ACCESS in favor of shifting to form a var_or_type. A |
| 476 | construct such as aType'access'access will again cause an error when |
| 477 | aType'access evaluates to a type that evaluate_subexp attempts to |
| 478 | evaluate. */ |
| 479 | primary : primary TICK_ACCESS |
| 480 | { write_exp_elt_opcode (pstate, UNOP_ADDR); } |
| 481 | | primary TICK_ADDRESS |
| 482 | { write_exp_elt_opcode (pstate, UNOP_ADDR); |
| 483 | write_exp_elt_opcode (pstate, UNOP_CAST); |
| 484 | write_exp_elt_type (pstate, |
| 485 | type_system_address (pstate)); |
| 486 | write_exp_elt_opcode (pstate, UNOP_CAST); |
| 487 | } |
| 488 | | primary TICK_FIRST tick_arglist |
| 489 | { write_int (pstate, $3, type_int (pstate)); |
| 490 | write_exp_elt_opcode (pstate, OP_ATR_FIRST); } |
| 491 | | primary TICK_LAST tick_arglist |
| 492 | { write_int (pstate, $3, type_int (pstate)); |
| 493 | write_exp_elt_opcode (pstate, OP_ATR_LAST); } |
| 494 | | primary TICK_LENGTH tick_arglist |
| 495 | { write_int (pstate, $3, type_int (pstate)); |
| 496 | write_exp_elt_opcode (pstate, OP_ATR_LENGTH); } |
| 497 | | primary TICK_SIZE |
| 498 | { write_exp_elt_opcode (pstate, OP_ATR_SIZE); } |
| 499 | | primary TICK_TAG |
| 500 | { write_exp_elt_opcode (pstate, OP_ATR_TAG); } |
| 501 | | opt_type_prefix TICK_MIN '(' exp ',' exp ')' |
| 502 | { write_exp_elt_opcode (pstate, OP_ATR_MIN); } |
| 503 | | opt_type_prefix TICK_MAX '(' exp ',' exp ')' |
| 504 | { write_exp_elt_opcode (pstate, OP_ATR_MAX); } |
| 505 | | opt_type_prefix TICK_POS '(' exp ')' |
| 506 | { write_exp_elt_opcode (pstate, OP_ATR_POS); } |
| 507 | | type_prefix TICK_VAL '(' exp ')' |
| 508 | { write_exp_elt_opcode (pstate, OP_ATR_VAL); } |
| 509 | | type_prefix TICK_MODULUS |
| 510 | { write_exp_elt_opcode (pstate, OP_ATR_MODULUS); } |
| 511 | ; |
| 512 | |
| 513 | tick_arglist : %prec '(' |
| 514 | { $$ = 1; } |
| 515 | | '(' INT ')' |
| 516 | { $$ = $2.val; } |
| 517 | ; |
| 518 | |
| 519 | type_prefix : |
| 520 | var_or_type |
| 521 | { |
| 522 | if ($1 == NULL) |
| 523 | error (_("Prefix must be type")); |
| 524 | write_exp_elt_opcode (pstate, OP_TYPE); |
| 525 | write_exp_elt_type (pstate, $1); |
| 526 | write_exp_elt_opcode (pstate, OP_TYPE); } |
| 527 | ; |
| 528 | |
| 529 | opt_type_prefix : |
| 530 | type_prefix |
| 531 | | /* EMPTY */ |
| 532 | { write_exp_elt_opcode (pstate, OP_TYPE); |
| 533 | write_exp_elt_type (pstate, |
| 534 | parse_type (pstate)->builtin_void); |
| 535 | write_exp_elt_opcode (pstate, OP_TYPE); } |
| 536 | ; |
| 537 | |
| 538 | |
| 539 | primary : INT |
| 540 | { write_int (pstate, (LONGEST) $1.val, $1.type); } |
| 541 | ; |
| 542 | |
| 543 | primary : CHARLIT |
| 544 | { write_int (pstate, |
| 545 | convert_char_literal (type_qualifier, $1.val), |
| 546 | (type_qualifier == NULL) |
| 547 | ? $1.type : type_qualifier); |
| 548 | } |
| 549 | ; |
| 550 | |
| 551 | primary : FLOAT |
| 552 | { write_exp_elt_opcode (pstate, OP_FLOAT); |
| 553 | write_exp_elt_type (pstate, $1.type); |
| 554 | write_exp_elt_floatcst (pstate, $1.val); |
| 555 | write_exp_elt_opcode (pstate, OP_FLOAT); |
| 556 | } |
| 557 | ; |
| 558 | |
| 559 | primary : NULL_PTR |
| 560 | { write_int (pstate, 0, type_int (pstate)); } |
| 561 | ; |
| 562 | |
| 563 | primary : STRING |
| 564 | { |
| 565 | write_exp_op_with_string (pstate, OP_STRING, $1); |
| 566 | } |
| 567 | ; |
| 568 | |
| 569 | primary : TRUEKEYWORD |
| 570 | { write_int (pstate, 1, type_boolean (pstate)); } |
| 571 | | FALSEKEYWORD |
| 572 | { write_int (pstate, 0, type_boolean (pstate)); } |
| 573 | ; |
| 574 | |
| 575 | primary : NEW NAME |
| 576 | { error (_("NEW not implemented.")); } |
| 577 | ; |
| 578 | |
| 579 | var_or_type: NAME %prec VAR |
| 580 | { $$ = write_var_or_type (pstate, NULL, $1); } |
| 581 | | block NAME %prec VAR |
| 582 | { $$ = write_var_or_type (pstate, $1, $2); } |
| 583 | | NAME TICK_ACCESS |
| 584 | { |
| 585 | $$ = write_var_or_type (pstate, NULL, $1); |
| 586 | if ($$ == NULL) |
| 587 | write_exp_elt_opcode (pstate, UNOP_ADDR); |
| 588 | else |
| 589 | $$ = lookup_pointer_type ($$); |
| 590 | } |
| 591 | | block NAME TICK_ACCESS |
| 592 | { |
| 593 | $$ = write_var_or_type (pstate, $1, $2); |
| 594 | if ($$ == NULL) |
| 595 | write_exp_elt_opcode (pstate, UNOP_ADDR); |
| 596 | else |
| 597 | $$ = lookup_pointer_type ($$); |
| 598 | } |
| 599 | ; |
| 600 | |
| 601 | /* GDB extension */ |
| 602 | block : NAME COLONCOLON |
| 603 | { $$ = block_lookup (NULL, $1.ptr); } |
| 604 | | block NAME COLONCOLON |
| 605 | { $$ = block_lookup ($1, $2.ptr); } |
| 606 | ; |
| 607 | |
| 608 | aggregate : |
| 609 | '(' aggregate_component_list ')' |
| 610 | { |
| 611 | write_exp_elt_opcode (pstate, OP_AGGREGATE); |
| 612 | write_exp_elt_longcst (pstate, $2); |
| 613 | write_exp_elt_opcode (pstate, OP_AGGREGATE); |
| 614 | } |
| 615 | ; |
| 616 | |
| 617 | aggregate_component_list : |
| 618 | component_groups { $$ = $1; } |
| 619 | | positional_list exp |
| 620 | { write_exp_elt_opcode (pstate, OP_POSITIONAL); |
| 621 | write_exp_elt_longcst (pstate, $1); |
| 622 | write_exp_elt_opcode (pstate, OP_POSITIONAL); |
| 623 | $$ = $1 + 1; |
| 624 | } |
| 625 | | positional_list component_groups |
| 626 | { $$ = $1 + $2; } |
| 627 | ; |
| 628 | |
| 629 | positional_list : |
| 630 | exp ',' |
| 631 | { write_exp_elt_opcode (pstate, OP_POSITIONAL); |
| 632 | write_exp_elt_longcst (pstate, 0); |
| 633 | write_exp_elt_opcode (pstate, OP_POSITIONAL); |
| 634 | $$ = 1; |
| 635 | } |
| 636 | | positional_list exp ',' |
| 637 | { write_exp_elt_opcode (pstate, OP_POSITIONAL); |
| 638 | write_exp_elt_longcst (pstate, $1); |
| 639 | write_exp_elt_opcode (pstate, OP_POSITIONAL); |
| 640 | $$ = $1 + 1; |
| 641 | } |
| 642 | ; |
| 643 | |
| 644 | component_groups: |
| 645 | others { $$ = 1; } |
| 646 | | component_group { $$ = 1; } |
| 647 | | component_group ',' component_groups |
| 648 | { $$ = $3 + 1; } |
| 649 | ; |
| 650 | |
| 651 | others : OTHERS ARROW exp |
| 652 | { write_exp_elt_opcode (pstate, OP_OTHERS); } |
| 653 | ; |
| 654 | |
| 655 | component_group : |
| 656 | component_associations |
| 657 | { |
| 658 | write_exp_elt_opcode (pstate, OP_CHOICES); |
| 659 | write_exp_elt_longcst (pstate, $1); |
| 660 | write_exp_elt_opcode (pstate, OP_CHOICES); |
| 661 | } |
| 662 | ; |
| 663 | |
| 664 | /* We use this somewhat obscure definition in order to handle NAME => and |
| 665 | NAME | differently from exp => and exp |. ARROW and '|' have a precedence |
| 666 | above that of the reduction of NAME to var_or_type. By delaying |
| 667 | decisions until after the => or '|', we convert the ambiguity to a |
| 668 | resolved shift/reduce conflict. */ |
| 669 | component_associations : |
| 670 | NAME ARROW |
| 671 | { write_name_assoc (pstate, $1); } |
| 672 | exp { $$ = 1; } |
| 673 | | simple_exp ARROW exp |
| 674 | { $$ = 1; } |
| 675 | | simple_exp DOTDOT simple_exp ARROW |
| 676 | { write_exp_elt_opcode (pstate, OP_DISCRETE_RANGE); |
| 677 | write_exp_op_with_string (pstate, OP_NAME, |
| 678 | empty_stoken); |
| 679 | } |
| 680 | exp { $$ = 1; } |
| 681 | | NAME '|' |
| 682 | { write_name_assoc (pstate, $1); } |
| 683 | component_associations { $$ = $4 + 1; } |
| 684 | | simple_exp '|' |
| 685 | component_associations { $$ = $3 + 1; } |
| 686 | | simple_exp DOTDOT simple_exp '|' |
| 687 | { write_exp_elt_opcode (pstate, OP_DISCRETE_RANGE); } |
| 688 | component_associations { $$ = $6 + 1; } |
| 689 | ; |
| 690 | |
| 691 | /* Some extensions borrowed from C, for the benefit of those who find they |
| 692 | can't get used to Ada notation in GDB. */ |
| 693 | |
| 694 | primary : '*' primary %prec '.' |
| 695 | { write_exp_elt_opcode (pstate, UNOP_IND); } |
| 696 | | '&' primary %prec '.' |
| 697 | { write_exp_elt_opcode (pstate, UNOP_ADDR); } |
| 698 | | primary '[' exp ']' |
| 699 | { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); } |
| 700 | ; |
| 701 | |
| 702 | %% |
| 703 | |
| 704 | /* yylex defined in ada-lex.c: Reads one token, getting characters */ |
| 705 | /* through lexptr. */ |
| 706 | |
| 707 | /* Remap normal flex interface names (yylex) as well as gratuitiously */ |
| 708 | /* global symbol names, so we can have multiple flex-generated parsers */ |
| 709 | /* in gdb. */ |
| 710 | |
| 711 | /* (See note above on previous definitions for YACC.) */ |
| 712 | |
| 713 | #define yy_create_buffer ada_yy_create_buffer |
| 714 | #define yy_delete_buffer ada_yy_delete_buffer |
| 715 | #define yy_init_buffer ada_yy_init_buffer |
| 716 | #define yy_load_buffer_state ada_yy_load_buffer_state |
| 717 | #define yy_switch_to_buffer ada_yy_switch_to_buffer |
| 718 | #define yyrestart ada_yyrestart |
| 719 | #define yytext ada_yytext |
| 720 | |
| 721 | static struct obstack temp_parse_space; |
| 722 | |
| 723 | /* The following kludge was found necessary to prevent conflicts between */ |
| 724 | /* defs.h and non-standard stdlib.h files. */ |
| 725 | #define qsort __qsort__dummy |
| 726 | #include "ada-lex.c" |
| 727 | |
| 728 | int |
| 729 | ada_parse (struct parser_state *par_state) |
| 730 | { |
| 731 | /* Setting up the parser state. */ |
| 732 | scoped_restore pstate_restore = make_scoped_restore (&pstate); |
| 733 | gdb_assert (par_state != NULL); |
| 734 | pstate = par_state; |
| 735 | |
| 736 | lexer_init (yyin); /* (Re-)initialize lexer. */ |
| 737 | type_qualifier = NULL; |
| 738 | obstack_free (&temp_parse_space, NULL); |
| 739 | obstack_init (&temp_parse_space); |
| 740 | |
| 741 | return yyparse (); |
| 742 | } |
| 743 | |
| 744 | static void |
| 745 | yyerror (const char *msg) |
| 746 | { |
| 747 | error (_("Error in expression, near `%s'."), pstate->lexptr); |
| 748 | } |
| 749 | |
| 750 | /* Emit expression to access an instance of SYM, in block BLOCK (if |
| 751 | non-NULL). */ |
| 752 | |
| 753 | static void |
| 754 | write_var_from_sym (struct parser_state *par_state, |
| 755 | const struct block *block, |
| 756 | struct symbol *sym) |
| 757 | { |
| 758 | if (symbol_read_needs_frame (sym)) |
| 759 | par_state->block_tracker->update (block, INNERMOST_BLOCK_FOR_SYMBOLS); |
| 760 | |
| 761 | write_exp_elt_opcode (par_state, OP_VAR_VALUE); |
| 762 | write_exp_elt_block (par_state, block); |
| 763 | write_exp_elt_sym (par_state, sym); |
| 764 | write_exp_elt_opcode (par_state, OP_VAR_VALUE); |
| 765 | } |
| 766 | |
| 767 | /* Write integer or boolean constant ARG of type TYPE. */ |
| 768 | |
| 769 | static void |
| 770 | write_int (struct parser_state *par_state, LONGEST arg, struct type *type) |
| 771 | { |
| 772 | write_exp_elt_opcode (par_state, OP_LONG); |
| 773 | write_exp_elt_type (par_state, type); |
| 774 | write_exp_elt_longcst (par_state, arg); |
| 775 | write_exp_elt_opcode (par_state, OP_LONG); |
| 776 | } |
| 777 | |
| 778 | /* Write an OPCODE, string, OPCODE sequence to the current expression. */ |
| 779 | static void |
| 780 | write_exp_op_with_string (struct parser_state *par_state, |
| 781 | enum exp_opcode opcode, struct stoken token) |
| 782 | { |
| 783 | write_exp_elt_opcode (par_state, opcode); |
| 784 | write_exp_string (par_state, token); |
| 785 | write_exp_elt_opcode (par_state, opcode); |
| 786 | } |
| 787 | |
| 788 | /* Emit expression corresponding to the renamed object named |
| 789 | * designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the |
| 790 | * context of ORIG_LEFT_CONTEXT, to which is applied the operations |
| 791 | * encoded by RENAMING_EXPR. MAX_DEPTH is the maximum number of |
| 792 | * cascaded renamings to allow. If ORIG_LEFT_CONTEXT is null, it |
| 793 | * defaults to the currently selected block. ORIG_SYMBOL is the |
| 794 | * symbol that originally encoded the renaming. It is needed only |
| 795 | * because its prefix also qualifies any index variables used to index |
| 796 | * or slice an array. It should not be necessary once we go to the |
| 797 | * new encoding entirely (FIXME pnh 7/20/2007). */ |
| 798 | |
| 799 | static void |
| 800 | write_object_renaming (struct parser_state *par_state, |
| 801 | const struct block *orig_left_context, |
| 802 | const char *renamed_entity, int renamed_entity_len, |
| 803 | const char *renaming_expr, int max_depth) |
| 804 | { |
| 805 | char *name; |
| 806 | enum { SIMPLE_INDEX, LOWER_BOUND, UPPER_BOUND } slice_state; |
| 807 | struct block_symbol sym_info; |
| 808 | |
| 809 | if (max_depth <= 0) |
| 810 | error (_("Could not find renamed symbol")); |
| 811 | |
| 812 | if (orig_left_context == NULL) |
| 813 | orig_left_context = get_selected_block (NULL); |
| 814 | |
| 815 | name = obstack_strndup (&temp_parse_space, renamed_entity, |
| 816 | renamed_entity_len); |
| 817 | ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info); |
| 818 | if (sym_info.symbol == NULL) |
| 819 | error (_("Could not find renamed variable: %s"), ada_decode (name).c_str ()); |
| 820 | else if (SYMBOL_CLASS (sym_info.symbol) == LOC_TYPEDEF) |
| 821 | /* We have a renaming of an old-style renaming symbol. Don't |
| 822 | trust the block information. */ |
| 823 | sym_info.block = orig_left_context; |
| 824 | |
| 825 | { |
| 826 | const char *inner_renamed_entity; |
| 827 | int inner_renamed_entity_len; |
| 828 | const char *inner_renaming_expr; |
| 829 | |
| 830 | switch (ada_parse_renaming (sym_info.symbol, &inner_renamed_entity, |
| 831 | &inner_renamed_entity_len, |
| 832 | &inner_renaming_expr)) |
| 833 | { |
| 834 | case ADA_NOT_RENAMING: |
| 835 | write_var_from_sym (par_state, sym_info.block, sym_info.symbol); |
| 836 | break; |
| 837 | case ADA_OBJECT_RENAMING: |
| 838 | write_object_renaming (par_state, sym_info.block, |
| 839 | inner_renamed_entity, inner_renamed_entity_len, |
| 840 | inner_renaming_expr, max_depth - 1); |
| 841 | break; |
| 842 | default: |
| 843 | goto BadEncoding; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | slice_state = SIMPLE_INDEX; |
| 848 | while (*renaming_expr == 'X') |
| 849 | { |
| 850 | renaming_expr += 1; |
| 851 | |
| 852 | switch (*renaming_expr) { |
| 853 | case 'A': |
| 854 | renaming_expr += 1; |
| 855 | write_exp_elt_opcode (par_state, UNOP_IND); |
| 856 | break; |
| 857 | case 'L': |
| 858 | slice_state = LOWER_BOUND; |
| 859 | /* FALLTHROUGH */ |
| 860 | case 'S': |
| 861 | renaming_expr += 1; |
| 862 | if (isdigit (*renaming_expr)) |
| 863 | { |
| 864 | char *next; |
| 865 | long val = strtol (renaming_expr, &next, 10); |
| 866 | if (next == renaming_expr) |
| 867 | goto BadEncoding; |
| 868 | renaming_expr = next; |
| 869 | write_exp_elt_opcode (par_state, OP_LONG); |
| 870 | write_exp_elt_type (par_state, type_int (par_state)); |
| 871 | write_exp_elt_longcst (par_state, (LONGEST) val); |
| 872 | write_exp_elt_opcode (par_state, OP_LONG); |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | const char *end; |
| 877 | char *index_name; |
| 878 | struct block_symbol index_sym_info; |
| 879 | |
| 880 | end = strchr (renaming_expr, 'X'); |
| 881 | if (end == NULL) |
| 882 | end = renaming_expr + strlen (renaming_expr); |
| 883 | |
| 884 | index_name = obstack_strndup (&temp_parse_space, renaming_expr, |
| 885 | end - renaming_expr); |
| 886 | renaming_expr = end; |
| 887 | |
| 888 | ada_lookup_encoded_symbol (index_name, orig_left_context, |
| 889 | VAR_DOMAIN, &index_sym_info); |
| 890 | if (index_sym_info.symbol == NULL) |
| 891 | error (_("Could not find %s"), index_name); |
| 892 | else if (SYMBOL_CLASS (index_sym_info.symbol) == LOC_TYPEDEF) |
| 893 | /* Index is an old-style renaming symbol. */ |
| 894 | index_sym_info.block = orig_left_context; |
| 895 | write_var_from_sym (par_state, index_sym_info.block, |
| 896 | index_sym_info.symbol); |
| 897 | } |
| 898 | if (slice_state == SIMPLE_INDEX) |
| 899 | { |
| 900 | write_exp_elt_opcode (par_state, OP_FUNCALL); |
| 901 | write_exp_elt_longcst (par_state, (LONGEST) 1); |
| 902 | write_exp_elt_opcode (par_state, OP_FUNCALL); |
| 903 | } |
| 904 | else if (slice_state == LOWER_BOUND) |
| 905 | slice_state = UPPER_BOUND; |
| 906 | else if (slice_state == UPPER_BOUND) |
| 907 | { |
| 908 | write_exp_elt_opcode (par_state, TERNOP_SLICE); |
| 909 | slice_state = SIMPLE_INDEX; |
| 910 | } |
| 911 | break; |
| 912 | |
| 913 | case 'R': |
| 914 | { |
| 915 | struct stoken field_name; |
| 916 | const char *end; |
| 917 | char *buf; |
| 918 | |
| 919 | renaming_expr += 1; |
| 920 | |
| 921 | if (slice_state != SIMPLE_INDEX) |
| 922 | goto BadEncoding; |
| 923 | end = strchr (renaming_expr, 'X'); |
| 924 | if (end == NULL) |
| 925 | end = renaming_expr + strlen (renaming_expr); |
| 926 | field_name.length = end - renaming_expr; |
| 927 | buf = (char *) malloc (end - renaming_expr + 1); |
| 928 | field_name.ptr = buf; |
| 929 | strncpy (buf, renaming_expr, end - renaming_expr); |
| 930 | buf[end - renaming_expr] = '\000'; |
| 931 | renaming_expr = end; |
| 932 | write_exp_op_with_string (par_state, STRUCTOP_STRUCT, field_name); |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | default: |
| 937 | goto BadEncoding; |
| 938 | } |
| 939 | } |
| 940 | if (slice_state == SIMPLE_INDEX) |
| 941 | return; |
| 942 | |
| 943 | BadEncoding: |
| 944 | error (_("Internal error in encoding of renaming declaration")); |
| 945 | } |
| 946 | |
| 947 | static const struct block* |
| 948 | block_lookup (const struct block *context, const char *raw_name) |
| 949 | { |
| 950 | const char *name; |
| 951 | std::vector<struct block_symbol> syms; |
| 952 | int nsyms; |
| 953 | struct symtab *symtab; |
| 954 | const struct block *result = NULL; |
| 955 | |
| 956 | if (raw_name[0] == '\'') |
| 957 | { |
| 958 | raw_name += 1; |
| 959 | name = raw_name; |
| 960 | } |
| 961 | else |
| 962 | name = ada_encode (raw_name); |
| 963 | |
| 964 | nsyms = ada_lookup_symbol_list (name, context, VAR_DOMAIN, &syms); |
| 965 | |
| 966 | if (context == NULL |
| 967 | && (nsyms == 0 || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK)) |
| 968 | symtab = lookup_symtab (name); |
| 969 | else |
| 970 | symtab = NULL; |
| 971 | |
| 972 | if (symtab != NULL) |
| 973 | result = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), STATIC_BLOCK); |
| 974 | else if (nsyms == 0 || SYMBOL_CLASS (syms[0].symbol) != LOC_BLOCK) |
| 975 | { |
| 976 | if (context == NULL) |
| 977 | error (_("No file or function \"%s\"."), raw_name); |
| 978 | else |
| 979 | error (_("No function \"%s\" in specified context."), raw_name); |
| 980 | } |
| 981 | else |
| 982 | { |
| 983 | if (nsyms > 1) |
| 984 | warning (_("Function name \"%s\" ambiguous here"), raw_name); |
| 985 | result = SYMBOL_BLOCK_VALUE (syms[0].symbol); |
| 986 | } |
| 987 | |
| 988 | return result; |
| 989 | } |
| 990 | |
| 991 | static struct symbol* |
| 992 | select_possible_type_sym (const std::vector<struct block_symbol> &syms) |
| 993 | { |
| 994 | int i; |
| 995 | int preferred_index; |
| 996 | struct type *preferred_type; |
| 997 | |
| 998 | preferred_index = -1; preferred_type = NULL; |
| 999 | for (i = 0; i < syms.size (); i += 1) |
| 1000 | switch (SYMBOL_CLASS (syms[i].symbol)) |
| 1001 | { |
| 1002 | case LOC_TYPEDEF: |
| 1003 | if (ada_prefer_type (SYMBOL_TYPE (syms[i].symbol), preferred_type)) |
| 1004 | { |
| 1005 | preferred_index = i; |
| 1006 | preferred_type = SYMBOL_TYPE (syms[i].symbol); |
| 1007 | } |
| 1008 | break; |
| 1009 | case LOC_REGISTER: |
| 1010 | case LOC_ARG: |
| 1011 | case LOC_REF_ARG: |
| 1012 | case LOC_REGPARM_ADDR: |
| 1013 | case LOC_LOCAL: |
| 1014 | case LOC_COMPUTED: |
| 1015 | return NULL; |
| 1016 | default: |
| 1017 | break; |
| 1018 | } |
| 1019 | if (preferred_type == NULL) |
| 1020 | return NULL; |
| 1021 | return syms[preferred_index].symbol; |
| 1022 | } |
| 1023 | |
| 1024 | static struct type* |
| 1025 | find_primitive_type (struct parser_state *par_state, char *name) |
| 1026 | { |
| 1027 | struct type *type; |
| 1028 | type = language_lookup_primitive_type (par_state->language (), |
| 1029 | par_state->gdbarch (), |
| 1030 | name); |
| 1031 | if (type == NULL && strcmp ("system__address", name) == 0) |
| 1032 | type = type_system_address (par_state); |
| 1033 | |
| 1034 | if (type != NULL) |
| 1035 | { |
| 1036 | /* Check to see if we have a regular definition of this |
| 1037 | type that just didn't happen to have been read yet. */ |
| 1038 | struct symbol *sym; |
| 1039 | char *expanded_name = |
| 1040 | (char *) alloca (strlen (name) + sizeof ("standard__")); |
| 1041 | strcpy (expanded_name, "standard__"); |
| 1042 | strcat (expanded_name, name); |
| 1043 | sym = ada_lookup_symbol (expanded_name, NULL, VAR_DOMAIN).symbol; |
| 1044 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
| 1045 | type = SYMBOL_TYPE (sym); |
| 1046 | } |
| 1047 | |
| 1048 | return type; |
| 1049 | } |
| 1050 | |
| 1051 | static int |
| 1052 | chop_selector (char *name, int end) |
| 1053 | { |
| 1054 | int i; |
| 1055 | for (i = end - 1; i > 0; i -= 1) |
| 1056 | if (name[i] == '.' || (name[i] == '_' && name[i+1] == '_')) |
| 1057 | return i; |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | /* If NAME is a string beginning with a separator (either '__', or |
| 1062 | '.'), chop this separator and return the result; else, return |
| 1063 | NAME. */ |
| 1064 | |
| 1065 | static char * |
| 1066 | chop_separator (char *name) |
| 1067 | { |
| 1068 | if (*name == '.') |
| 1069 | return name + 1; |
| 1070 | |
| 1071 | if (name[0] == '_' && name[1] == '_') |
| 1072 | return name + 2; |
| 1073 | |
| 1074 | return name; |
| 1075 | } |
| 1076 | |
| 1077 | /* Given that SELS is a string of the form (<sep><identifier>)*, where |
| 1078 | <sep> is '__' or '.', write the indicated sequence of |
| 1079 | STRUCTOP_STRUCT expression operators. */ |
| 1080 | static void |
| 1081 | write_selectors (struct parser_state *par_state, char *sels) |
| 1082 | { |
| 1083 | while (*sels != '\0') |
| 1084 | { |
| 1085 | struct stoken field_name; |
| 1086 | char *p = chop_separator (sels); |
| 1087 | sels = p; |
| 1088 | while (*sels != '\0' && *sels != '.' |
| 1089 | && (sels[0] != '_' || sels[1] != '_')) |
| 1090 | sels += 1; |
| 1091 | field_name.length = sels - p; |
| 1092 | field_name.ptr = p; |
| 1093 | write_exp_op_with_string (par_state, STRUCTOP_STRUCT, field_name); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* Write a variable access (OP_VAR_VALUE) to ambiguous encoded name |
| 1098 | NAME[0..LEN-1], in block context BLOCK, to be resolved later. Writes |
| 1099 | a temporary symbol that is valid until the next call to ada_parse. |
| 1100 | */ |
| 1101 | static void |
| 1102 | write_ambiguous_var (struct parser_state *par_state, |
| 1103 | const struct block *block, char *name, int len) |
| 1104 | { |
| 1105 | struct symbol *sym = new (&temp_parse_space) symbol (); |
| 1106 | |
| 1107 | SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN; |
| 1108 | sym->set_linkage_name (obstack_strndup (&temp_parse_space, name, len)); |
| 1109 | sym->set_language (language_ada, nullptr); |
| 1110 | |
| 1111 | write_exp_elt_opcode (par_state, OP_VAR_VALUE); |
| 1112 | write_exp_elt_block (par_state, block); |
| 1113 | write_exp_elt_sym (par_state, sym); |
| 1114 | write_exp_elt_opcode (par_state, OP_VAR_VALUE); |
| 1115 | } |
| 1116 | |
| 1117 | /* A convenient wrapper around ada_get_field_index that takes |
| 1118 | a non NUL-terminated FIELD_NAME0 and a FIELD_NAME_LEN instead |
| 1119 | of a NUL-terminated field name. */ |
| 1120 | |
| 1121 | static int |
| 1122 | ada_nget_field_index (const struct type *type, const char *field_name0, |
| 1123 | int field_name_len, int maybe_missing) |
| 1124 | { |
| 1125 | char *field_name = (char *) alloca ((field_name_len + 1) * sizeof (char)); |
| 1126 | |
| 1127 | strncpy (field_name, field_name0, field_name_len); |
| 1128 | field_name[field_name_len] = '\0'; |
| 1129 | return ada_get_field_index (type, field_name, maybe_missing); |
| 1130 | } |
| 1131 | |
| 1132 | /* If encoded_field_name is the name of a field inside symbol SYM, |
| 1133 | then return the type of that field. Otherwise, return NULL. |
| 1134 | |
| 1135 | This function is actually recursive, so if ENCODED_FIELD_NAME |
| 1136 | doesn't match one of the fields of our symbol, then try to see |
| 1137 | if ENCODED_FIELD_NAME could not be a succession of field names |
| 1138 | (in other words, the user entered an expression of the form |
| 1139 | TYPE_NAME.FIELD1.FIELD2.FIELD3), in which case we evaluate |
| 1140 | each field name sequentially to obtain the desired field type. |
| 1141 | In case of failure, we return NULL. */ |
| 1142 | |
| 1143 | static struct type * |
| 1144 | get_symbol_field_type (struct symbol *sym, char *encoded_field_name) |
| 1145 | { |
| 1146 | char *field_name = encoded_field_name; |
| 1147 | char *subfield_name; |
| 1148 | struct type *type = SYMBOL_TYPE (sym); |
| 1149 | int fieldno; |
| 1150 | |
| 1151 | if (type == NULL || field_name == NULL) |
| 1152 | return NULL; |
| 1153 | type = check_typedef (type); |
| 1154 | |
| 1155 | while (field_name[0] != '\0') |
| 1156 | { |
| 1157 | field_name = chop_separator (field_name); |
| 1158 | |
| 1159 | fieldno = ada_get_field_index (type, field_name, 1); |
| 1160 | if (fieldno >= 0) |
| 1161 | return TYPE_FIELD_TYPE (type, fieldno); |
| 1162 | |
| 1163 | subfield_name = field_name; |
| 1164 | while (*subfield_name != '\0' && *subfield_name != '.' |
| 1165 | && (subfield_name[0] != '_' || subfield_name[1] != '_')) |
| 1166 | subfield_name += 1; |
| 1167 | |
| 1168 | if (subfield_name[0] == '\0') |
| 1169 | return NULL; |
| 1170 | |
| 1171 | fieldno = ada_nget_field_index (type, field_name, |
| 1172 | subfield_name - field_name, 1); |
| 1173 | if (fieldno < 0) |
| 1174 | return NULL; |
| 1175 | |
| 1176 | type = TYPE_FIELD_TYPE (type, fieldno); |
| 1177 | field_name = subfield_name; |
| 1178 | } |
| 1179 | |
| 1180 | return NULL; |
| 1181 | } |
| 1182 | |
| 1183 | /* Look up NAME0 (an unencoded identifier or dotted name) in BLOCK (or |
| 1184 | expression_block_context if NULL). If it denotes a type, return |
| 1185 | that type. Otherwise, write expression code to evaluate it as an |
| 1186 | object and return NULL. In this second case, NAME0 will, in general, |
| 1187 | have the form <name>(.<selector_name>)*, where <name> is an object |
| 1188 | or renaming encoded in the debugging data. Calls error if no |
| 1189 | prefix <name> matches a name in the debugging data (i.e., matches |
| 1190 | either a complete name or, as a wild-card match, the final |
| 1191 | identifier). */ |
| 1192 | |
| 1193 | static struct type* |
| 1194 | write_var_or_type (struct parser_state *par_state, |
| 1195 | const struct block *block, struct stoken name0) |
| 1196 | { |
| 1197 | int depth; |
| 1198 | char *encoded_name; |
| 1199 | int name_len; |
| 1200 | |
| 1201 | if (block == NULL) |
| 1202 | block = par_state->expression_context_block; |
| 1203 | |
| 1204 | encoded_name = ada_encode (name0.ptr); |
| 1205 | name_len = strlen (encoded_name); |
| 1206 | encoded_name = obstack_strndup (&temp_parse_space, encoded_name, name_len); |
| 1207 | for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1) |
| 1208 | { |
| 1209 | int tail_index; |
| 1210 | |
| 1211 | tail_index = name_len; |
| 1212 | while (tail_index > 0) |
| 1213 | { |
| 1214 | int nsyms; |
| 1215 | std::vector<struct block_symbol> syms; |
| 1216 | struct symbol *type_sym; |
| 1217 | struct symbol *renaming_sym; |
| 1218 | const char* renaming; |
| 1219 | int renaming_len; |
| 1220 | const char* renaming_expr; |
| 1221 | int terminator = encoded_name[tail_index]; |
| 1222 | |
| 1223 | encoded_name[tail_index] = '\0'; |
| 1224 | nsyms = ada_lookup_symbol_list (encoded_name, block, |
| 1225 | VAR_DOMAIN, &syms); |
| 1226 | encoded_name[tail_index] = terminator; |
| 1227 | |
| 1228 | type_sym = select_possible_type_sym (syms); |
| 1229 | |
| 1230 | if (type_sym != NULL) |
| 1231 | renaming_sym = type_sym; |
| 1232 | else if (nsyms == 1) |
| 1233 | renaming_sym = syms[0].symbol; |
| 1234 | else |
| 1235 | renaming_sym = NULL; |
| 1236 | |
| 1237 | switch (ada_parse_renaming (renaming_sym, &renaming, |
| 1238 | &renaming_len, &renaming_expr)) |
| 1239 | { |
| 1240 | case ADA_NOT_RENAMING: |
| 1241 | break; |
| 1242 | case ADA_PACKAGE_RENAMING: |
| 1243 | case ADA_EXCEPTION_RENAMING: |
| 1244 | case ADA_SUBPROGRAM_RENAMING: |
| 1245 | { |
| 1246 | int alloc_len = renaming_len + name_len - tail_index + 1; |
| 1247 | char *new_name |
| 1248 | = (char *) obstack_alloc (&temp_parse_space, alloc_len); |
| 1249 | strncpy (new_name, renaming, renaming_len); |
| 1250 | strcpy (new_name + renaming_len, encoded_name + tail_index); |
| 1251 | encoded_name = new_name; |
| 1252 | name_len = renaming_len + name_len - tail_index; |
| 1253 | goto TryAfterRenaming; |
| 1254 | } |
| 1255 | case ADA_OBJECT_RENAMING: |
| 1256 | write_object_renaming (par_state, block, renaming, renaming_len, |
| 1257 | renaming_expr, MAX_RENAMING_CHAIN_LENGTH); |
| 1258 | write_selectors (par_state, encoded_name + tail_index); |
| 1259 | return NULL; |
| 1260 | default: |
| 1261 | internal_error (__FILE__, __LINE__, |
| 1262 | _("impossible value from ada_parse_renaming")); |
| 1263 | } |
| 1264 | |
| 1265 | if (type_sym != NULL) |
| 1266 | { |
| 1267 | struct type *field_type; |
| 1268 | |
| 1269 | if (tail_index == name_len) |
| 1270 | return SYMBOL_TYPE (type_sym); |
| 1271 | |
| 1272 | /* We have some extraneous characters after the type name. |
| 1273 | If this is an expression "TYPE_NAME.FIELD0.[...].FIELDN", |
| 1274 | then try to get the type of FIELDN. */ |
| 1275 | field_type |
| 1276 | = get_symbol_field_type (type_sym, encoded_name + tail_index); |
| 1277 | if (field_type != NULL) |
| 1278 | return field_type; |
| 1279 | else |
| 1280 | error (_("Invalid attempt to select from type: \"%s\"."), |
| 1281 | name0.ptr); |
| 1282 | } |
| 1283 | else if (tail_index == name_len && nsyms == 0) |
| 1284 | { |
| 1285 | struct type *type = find_primitive_type (par_state, |
| 1286 | encoded_name); |
| 1287 | |
| 1288 | if (type != NULL) |
| 1289 | return type; |
| 1290 | } |
| 1291 | |
| 1292 | if (nsyms == 1) |
| 1293 | { |
| 1294 | write_var_from_sym (par_state, syms[0].block, syms[0].symbol); |
| 1295 | write_selectors (par_state, encoded_name + tail_index); |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | else if (nsyms == 0) |
| 1299 | { |
| 1300 | struct bound_minimal_symbol msym |
| 1301 | = ada_lookup_simple_minsym (encoded_name); |
| 1302 | if (msym.minsym != NULL) |
| 1303 | { |
| 1304 | write_exp_msymbol (par_state, msym); |
| 1305 | /* Maybe cause error here rather than later? FIXME? */ |
| 1306 | write_selectors (par_state, encoded_name + tail_index); |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | |
| 1310 | if (tail_index == name_len |
| 1311 | && strncmp (encoded_name, "standard__", |
| 1312 | sizeof ("standard__") - 1) == 0) |
| 1313 | error (_("No definition of \"%s\" found."), name0.ptr); |
| 1314 | |
| 1315 | tail_index = chop_selector (encoded_name, tail_index); |
| 1316 | } |
| 1317 | else |
| 1318 | { |
| 1319 | write_ambiguous_var (par_state, block, encoded_name, |
| 1320 | tail_index); |
| 1321 | write_selectors (par_state, encoded_name + tail_index); |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | if (!have_full_symbols () && !have_partial_symbols () && block == NULL) |
| 1327 | error (_("No symbol table is loaded. Use the \"file\" command.")); |
| 1328 | if (block == par_state->expression_context_block) |
| 1329 | error (_("No definition of \"%s\" in current context."), name0.ptr); |
| 1330 | else |
| 1331 | error (_("No definition of \"%s\" in specified context."), name0.ptr); |
| 1332 | |
| 1333 | TryAfterRenaming: ; |
| 1334 | } |
| 1335 | |
| 1336 | error (_("Could not find renamed symbol \"%s\""), name0.ptr); |
| 1337 | |
| 1338 | } |
| 1339 | |
| 1340 | /* Write a left side of a component association (e.g., NAME in NAME => |
| 1341 | exp). If NAME has the form of a selected component, write it as an |
| 1342 | ordinary expression. If it is a simple variable that unambiguously |
| 1343 | corresponds to exactly one symbol that does not denote a type or an |
| 1344 | object renaming, also write it normally as an OP_VAR_VALUE. |
| 1345 | Otherwise, write it as an OP_NAME. |
| 1346 | |
| 1347 | Unfortunately, we don't know at this point whether NAME is supposed |
| 1348 | to denote a record component name or the value of an array index. |
| 1349 | Therefore, it is not appropriate to disambiguate an ambiguous name |
| 1350 | as we normally would, nor to replace a renaming with its referent. |
| 1351 | As a result, in the (one hopes) rare case that one writes an |
| 1352 | aggregate such as (R => 42) where R renames an object or is an |
| 1353 | ambiguous name, one must write instead ((R) => 42). */ |
| 1354 | |
| 1355 | static void |
| 1356 | write_name_assoc (struct parser_state *par_state, struct stoken name) |
| 1357 | { |
| 1358 | if (strchr (name.ptr, '.') == NULL) |
| 1359 | { |
| 1360 | std::vector<struct block_symbol> syms; |
| 1361 | int nsyms = ada_lookup_symbol_list (name.ptr, |
| 1362 | par_state->expression_context_block, |
| 1363 | VAR_DOMAIN, &syms); |
| 1364 | |
| 1365 | if (nsyms != 1 || SYMBOL_CLASS (syms[0].symbol) == LOC_TYPEDEF) |
| 1366 | write_exp_op_with_string (par_state, OP_NAME, name); |
| 1367 | else |
| 1368 | write_var_from_sym (par_state, syms[0].block, syms[0].symbol); |
| 1369 | } |
| 1370 | else |
| 1371 | if (write_var_or_type (par_state, NULL, name) != NULL) |
| 1372 | error (_("Invalid use of type.")); |
| 1373 | } |
| 1374 | |
| 1375 | /* Convert the character literal whose ASCII value would be VAL to the |
| 1376 | appropriate value of type TYPE, if there is a translation. |
| 1377 | Otherwise return VAL. Hence, in an enumeration type ('A', 'B'), |
| 1378 | the literal 'A' (VAL == 65), returns 0. */ |
| 1379 | |
| 1380 | static LONGEST |
| 1381 | convert_char_literal (struct type *type, LONGEST val) |
| 1382 | { |
| 1383 | char name[7]; |
| 1384 | int f; |
| 1385 | |
| 1386 | if (type == NULL) |
| 1387 | return val; |
| 1388 | type = check_typedef (type); |
| 1389 | if (TYPE_CODE (type) != TYPE_CODE_ENUM) |
| 1390 | return val; |
| 1391 | |
| 1392 | if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9')) |
| 1393 | xsnprintf (name, sizeof (name), "Q%c", (int) val); |
| 1394 | else |
| 1395 | xsnprintf (name, sizeof (name), "QU%02x", (int) val); |
| 1396 | size_t len = strlen (name); |
| 1397 | for (f = 0; f < TYPE_NFIELDS (type); f += 1) |
| 1398 | { |
| 1399 | /* Check the suffix because an enum constant in a package will |
| 1400 | have a name like "pkg__QUxx". This is safe enough because we |
| 1401 | already have the correct type, and because mangling means |
| 1402 | there can't be clashes. */ |
| 1403 | const char *ename = TYPE_FIELD_NAME (type, f); |
| 1404 | size_t elen = strlen (ename); |
| 1405 | |
| 1406 | if (elen >= len && strcmp (name, ename + elen - len) == 0) |
| 1407 | return TYPE_FIELD_ENUMVAL (type, f); |
| 1408 | } |
| 1409 | return val; |
| 1410 | } |
| 1411 | |
| 1412 | static struct type * |
| 1413 | type_int (struct parser_state *par_state) |
| 1414 | { |
| 1415 | return parse_type (par_state)->builtin_int; |
| 1416 | } |
| 1417 | |
| 1418 | static struct type * |
| 1419 | type_long (struct parser_state *par_state) |
| 1420 | { |
| 1421 | return parse_type (par_state)->builtin_long; |
| 1422 | } |
| 1423 | |
| 1424 | static struct type * |
| 1425 | type_long_long (struct parser_state *par_state) |
| 1426 | { |
| 1427 | return parse_type (par_state)->builtin_long_long; |
| 1428 | } |
| 1429 | |
| 1430 | static struct type * |
| 1431 | type_long_double (struct parser_state *par_state) |
| 1432 | { |
| 1433 | return parse_type (par_state)->builtin_long_double; |
| 1434 | } |
| 1435 | |
| 1436 | static struct type * |
| 1437 | type_char (struct parser_state *par_state) |
| 1438 | { |
| 1439 | return language_string_char_type (par_state->language (), |
| 1440 | par_state->gdbarch ()); |
| 1441 | } |
| 1442 | |
| 1443 | static struct type * |
| 1444 | type_boolean (struct parser_state *par_state) |
| 1445 | { |
| 1446 | return parse_type (par_state)->builtin_bool; |
| 1447 | } |
| 1448 | |
| 1449 | static struct type * |
| 1450 | type_system_address (struct parser_state *par_state) |
| 1451 | { |
| 1452 | struct type *type |
| 1453 | = language_lookup_primitive_type (par_state->language (), |
| 1454 | par_state->gdbarch (), |
| 1455 | "system__address"); |
| 1456 | return type != NULL ? type : parse_type (par_state)->builtin_data_ptr; |
| 1457 | } |
| 1458 | |
| 1459 | void _initialize_ada_exp (); |
| 1460 | void |
| 1461 | _initialize_ada_exp () |
| 1462 | { |
| 1463 | obstack_init (&temp_parse_space); |
| 1464 | } |