| 1 | /* YACC parser for Go expressions, for GDB. |
| 2 | |
| 3 | Copyright (C) 2012 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. */ |
| 19 | |
| 20 | /* This file is derived from c-exp.y, p-exp.y. */ |
| 21 | |
| 22 | /* Parse a Go expression from text in a string, |
| 23 | and return the result as a struct expression pointer. |
| 24 | That structure contains arithmetic operations in reverse polish, |
| 25 | with constants represented by operations that are followed by special data. |
| 26 | See expression.h for the details of the format. |
| 27 | What is important here is that it can be built up sequentially |
| 28 | during the process of parsing; the lower levels of the tree always |
| 29 | come first in the result. |
| 30 | |
| 31 | Note that malloc's and realloc's in this file are transformed to |
| 32 | xmalloc and xrealloc respectively by the same sed command in the |
| 33 | makefile that remaps any other malloc/realloc inserted by the parser |
| 34 | generator. Doing this with #defines and trying to control the interaction |
| 35 | with include files (<malloc.h> and <stdlib.h> for example) just became |
| 36 | too messy, particularly when such includes can be inserted at random |
| 37 | times by the parser generator. */ |
| 38 | |
| 39 | /* Known bugs or limitations: |
| 40 | |
| 41 | - Unicode |
| 42 | - &^ |
| 43 | - '_' (blank identifier) |
| 44 | - automatic deref of pointers |
| 45 | - method expressions |
| 46 | - interfaces, channels, etc. |
| 47 | |
| 48 | And lots of other things. |
| 49 | I'm sure there's some cleanup to do. |
| 50 | */ |
| 51 | |
| 52 | %{ |
| 53 | |
| 54 | #include "defs.h" |
| 55 | #include "gdb_string.h" |
| 56 | #include <ctype.h> |
| 57 | #include "expression.h" |
| 58 | #include "value.h" |
| 59 | #include "parser-defs.h" |
| 60 | #include "language.h" |
| 61 | #include "c-lang.h" |
| 62 | #include "go-lang.h" |
| 63 | #include "bfd.h" /* Required by objfiles.h. */ |
| 64 | #include "symfile.h" /* Required by objfiles.h. */ |
| 65 | #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */ |
| 66 | #include "charset.h" |
| 67 | #include "block.h" |
| 68 | |
| 69 | #define parse_type builtin_type (parse_gdbarch) |
| 70 | |
| 71 | /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc), |
| 72 | as well as gratuitiously global symbol names, so we can have multiple |
| 73 | yacc generated parsers in gdb. Note that these are only the variables |
| 74 | produced by yacc. If other parser generators (bison, byacc, etc) produce |
| 75 | additional global names that conflict at link time, then those parser |
| 76 | generators need to be fixed instead of adding those names to this list. */ |
| 77 | |
| 78 | #define yymaxdepth go_maxdepth |
| 79 | #define yyparse go_parse_internal |
| 80 | #define yylex go_lex |
| 81 | #define yyerror go_error |
| 82 | #define yylval go_lval |
| 83 | #define yychar go_char |
| 84 | #define yydebug go_debug |
| 85 | #define yypact go_pact |
| 86 | #define yyr1 go_r1 |
| 87 | #define yyr2 go_r2 |
| 88 | #define yydef go_def |
| 89 | #define yychk go_chk |
| 90 | #define yypgo go_pgo |
| 91 | #define yyact go_act |
| 92 | #define yyexca go_exca |
| 93 | #define yyerrflag go_errflag |
| 94 | #define yynerrs go_nerrs |
| 95 | #define yyps go_ps |
| 96 | #define yypv go_pv |
| 97 | #define yys go_s |
| 98 | #define yy_yys go_yys |
| 99 | #define yystate go_state |
| 100 | #define yytmp go_tmp |
| 101 | #define yyv go_v |
| 102 | #define yy_yyv go_yyv |
| 103 | #define yyval go_val |
| 104 | #define yylloc go_lloc |
| 105 | #define yyreds go_reds /* With YYDEBUG defined */ |
| 106 | #define yytoks go_toks /* With YYDEBUG defined */ |
| 107 | #define yyname go_name /* With YYDEBUG defined */ |
| 108 | #define yyrule go_rule /* With YYDEBUG defined */ |
| 109 | #define yylhs go_yylhs |
| 110 | #define yylen go_yylen |
| 111 | #define yydefred go_yydefred |
| 112 | #define yydgoto go_yydgoto |
| 113 | #define yysindex go_yysindex |
| 114 | #define yyrindex go_yyrindex |
| 115 | #define yygindex go_yygindex |
| 116 | #define yytable go_yytable |
| 117 | #define yycheck go_yycheck |
| 118 | |
| 119 | #ifndef YYDEBUG |
| 120 | #define YYDEBUG 1 /* Default to yydebug support */ |
| 121 | #endif |
| 122 | |
| 123 | #define YYFPRINTF parser_fprintf |
| 124 | |
| 125 | int yyparse (void); |
| 126 | |
| 127 | static int yylex (void); |
| 128 | |
| 129 | void yyerror (char *); |
| 130 | |
| 131 | %} |
| 132 | |
| 133 | /* Although the yacc "value" of an expression is not used, |
| 134 | since the result is stored in the structure being created, |
| 135 | other node types do have values. */ |
| 136 | |
| 137 | %union |
| 138 | { |
| 139 | LONGEST lval; |
| 140 | struct { |
| 141 | LONGEST val; |
| 142 | struct type *type; |
| 143 | } typed_val_int; |
| 144 | struct { |
| 145 | DOUBLEST dval; |
| 146 | struct type *type; |
| 147 | } typed_val_float; |
| 148 | struct stoken sval; |
| 149 | struct symtoken ssym; |
| 150 | struct type *tval; |
| 151 | struct typed_stoken tsval; |
| 152 | struct ttype tsym; |
| 153 | int voidval; |
| 154 | enum exp_opcode opcode; |
| 155 | struct internalvar *ivar; |
| 156 | struct stoken_vector svec; |
| 157 | } |
| 158 | |
| 159 | %{ |
| 160 | /* YYSTYPE gets defined by %union. */ |
| 161 | static int parse_number (char *, int, int, YYSTYPE *); |
| 162 | static int parse_go_float (struct gdbarch *gdbarch, const char *p, int len, |
| 163 | DOUBLEST *d, struct type **t); |
| 164 | %} |
| 165 | |
| 166 | %type <voidval> exp exp1 type_exp start variable lcurly |
| 167 | %type <lval> rcurly |
| 168 | %type <tval> type |
| 169 | |
| 170 | %token <typed_val_int> INT |
| 171 | %token <typed_val_float> FLOAT |
| 172 | |
| 173 | /* Both NAME and TYPENAME tokens represent symbols in the input, |
| 174 | and both convey their data as strings. |
| 175 | But a TYPENAME is a string that happens to be defined as a type |
| 176 | or builtin type name (such as int or char) |
| 177 | and a NAME is any other symbol. |
| 178 | Contexts where this distinction is not important can use the |
| 179 | nonterminal "name", which matches either NAME or TYPENAME. */ |
| 180 | |
| 181 | %token <tsval> RAW_STRING |
| 182 | %token <tsval> STRING |
| 183 | %token <tsval> CHAR |
| 184 | %token <ssym> NAME |
| 185 | %token <tsym> TYPENAME /* Not TYPE_NAME cus already taken. */ |
| 186 | %token <voidval> COMPLETE |
| 187 | /*%type <sval> name*/ |
| 188 | %type <svec> string_exp |
| 189 | %type <ssym> name_not_typename |
| 190 | |
| 191 | /* A NAME_OR_INT is a symbol which is not known in the symbol table, |
| 192 | but which would parse as a valid number in the current input radix. |
| 193 | E.g. "c" when input_radix==16. Depending on the parse, it will be |
| 194 | turned into a name or into a number. */ |
| 195 | %token <ssym> NAME_OR_INT |
| 196 | |
| 197 | %token <lval> TRUE_KEYWORD FALSE_KEYWORD |
| 198 | %token STRUCT_KEYWORD INTERFACE_KEYWORD TYPE_KEYWORD CHAN_KEYWORD |
| 199 | %token SIZEOF_KEYWORD |
| 200 | %token LEN_KEYWORD CAP_KEYWORD |
| 201 | %token NEW_KEYWORD |
| 202 | %token IOTA_KEYWORD NIL_KEYWORD |
| 203 | %token CONST_KEYWORD |
| 204 | %token DOTDOTDOT |
| 205 | %token ENTRY |
| 206 | %token ERROR |
| 207 | |
| 208 | /* Special type cases. */ |
| 209 | %token BYTE_KEYWORD /* An alias of uint8. */ |
| 210 | |
| 211 | %token <sval> DOLLAR_VARIABLE |
| 212 | |
| 213 | %token <opcode> ASSIGN_MODIFY |
| 214 | |
| 215 | %left ',' |
| 216 | %left ABOVE_COMMA |
| 217 | %right '=' ASSIGN_MODIFY |
| 218 | %right '?' |
| 219 | %left OROR |
| 220 | %left ANDAND |
| 221 | %left '|' |
| 222 | %left '^' |
| 223 | %left '&' |
| 224 | %left ANDNOT |
| 225 | %left EQUAL NOTEQUAL |
| 226 | %left '<' '>' LEQ GEQ |
| 227 | %left LSH RSH |
| 228 | %left '@' |
| 229 | %left '+' '-' |
| 230 | %left '*' '/' '%' |
| 231 | %right UNARY INCREMENT DECREMENT |
| 232 | %right LEFT_ARROW '.' '[' '(' |
| 233 | |
| 234 | \f |
| 235 | %% |
| 236 | |
| 237 | start : exp1 |
| 238 | | type_exp |
| 239 | ; |
| 240 | |
| 241 | type_exp: type |
| 242 | { write_exp_elt_opcode(OP_TYPE); |
| 243 | write_exp_elt_type($1); |
| 244 | write_exp_elt_opcode(OP_TYPE); } |
| 245 | ; |
| 246 | |
| 247 | /* Expressions, including the comma operator. */ |
| 248 | exp1 : exp |
| 249 | | exp1 ',' exp |
| 250 | { write_exp_elt_opcode (BINOP_COMMA); } |
| 251 | ; |
| 252 | |
| 253 | /* Expressions, not including the comma operator. */ |
| 254 | exp : '*' exp %prec UNARY |
| 255 | { write_exp_elt_opcode (UNOP_IND); } |
| 256 | ; |
| 257 | |
| 258 | exp : '&' exp %prec UNARY |
| 259 | { write_exp_elt_opcode (UNOP_ADDR); } |
| 260 | ; |
| 261 | |
| 262 | exp : '-' exp %prec UNARY |
| 263 | { write_exp_elt_opcode (UNOP_NEG); } |
| 264 | ; |
| 265 | |
| 266 | exp : '+' exp %prec UNARY |
| 267 | { write_exp_elt_opcode (UNOP_PLUS); } |
| 268 | ; |
| 269 | |
| 270 | exp : '!' exp %prec UNARY |
| 271 | { write_exp_elt_opcode (UNOP_LOGICAL_NOT); } |
| 272 | ; |
| 273 | |
| 274 | exp : '^' exp %prec UNARY |
| 275 | { write_exp_elt_opcode (UNOP_COMPLEMENT); } |
| 276 | ; |
| 277 | |
| 278 | exp : exp INCREMENT %prec UNARY |
| 279 | { write_exp_elt_opcode (UNOP_POSTINCREMENT); } |
| 280 | ; |
| 281 | |
| 282 | exp : exp DECREMENT %prec UNARY |
| 283 | { write_exp_elt_opcode (UNOP_POSTDECREMENT); } |
| 284 | ; |
| 285 | |
| 286 | /* foo->bar is not in Go. May want as a gdb extension. Later. */ |
| 287 | |
| 288 | exp : exp '.' name_not_typename |
| 289 | { write_exp_elt_opcode (STRUCTOP_STRUCT); |
| 290 | write_exp_string ($3.stoken); |
| 291 | write_exp_elt_opcode (STRUCTOP_STRUCT); } |
| 292 | ; |
| 293 | |
| 294 | exp : exp '.' name_not_typename COMPLETE |
| 295 | { mark_struct_expression (); |
| 296 | write_exp_elt_opcode (STRUCTOP_STRUCT); |
| 297 | write_exp_string ($3.stoken); |
| 298 | write_exp_elt_opcode (STRUCTOP_STRUCT); } |
| 299 | ; |
| 300 | |
| 301 | exp : exp '.' COMPLETE |
| 302 | { struct stoken s; |
| 303 | mark_struct_expression (); |
| 304 | write_exp_elt_opcode (STRUCTOP_STRUCT); |
| 305 | s.ptr = ""; |
| 306 | s.length = 0; |
| 307 | write_exp_string (s); |
| 308 | write_exp_elt_opcode (STRUCTOP_STRUCT); } |
| 309 | ; |
| 310 | |
| 311 | exp : exp '[' exp1 ']' |
| 312 | { write_exp_elt_opcode (BINOP_SUBSCRIPT); } |
| 313 | ; |
| 314 | |
| 315 | exp : exp '(' |
| 316 | /* This is to save the value of arglist_len |
| 317 | being accumulated by an outer function call. */ |
| 318 | { start_arglist (); } |
| 319 | arglist ')' %prec LEFT_ARROW |
| 320 | { write_exp_elt_opcode (OP_FUNCALL); |
| 321 | write_exp_elt_longcst ((LONGEST) end_arglist ()); |
| 322 | write_exp_elt_opcode (OP_FUNCALL); } |
| 323 | ; |
| 324 | |
| 325 | lcurly : '{' |
| 326 | { start_arglist (); } |
| 327 | ; |
| 328 | |
| 329 | arglist : |
| 330 | ; |
| 331 | |
| 332 | arglist : exp |
| 333 | { arglist_len = 1; } |
| 334 | ; |
| 335 | |
| 336 | arglist : arglist ',' exp %prec ABOVE_COMMA |
| 337 | { arglist_len++; } |
| 338 | ; |
| 339 | |
| 340 | rcurly : '}' |
| 341 | { $$ = end_arglist () - 1; } |
| 342 | ; |
| 343 | |
| 344 | exp : lcurly type rcurly exp %prec UNARY |
| 345 | { write_exp_elt_opcode (UNOP_MEMVAL); |
| 346 | write_exp_elt_type ($2); |
| 347 | write_exp_elt_opcode (UNOP_MEMVAL); } |
| 348 | ; |
| 349 | |
| 350 | exp : type '(' exp ')' %prec UNARY |
| 351 | { write_exp_elt_opcode (UNOP_CAST); |
| 352 | write_exp_elt_type ($1); |
| 353 | write_exp_elt_opcode (UNOP_CAST); } |
| 354 | ; |
| 355 | |
| 356 | exp : '(' exp1 ')' |
| 357 | { } |
| 358 | ; |
| 359 | |
| 360 | /* Binary operators in order of decreasing precedence. */ |
| 361 | |
| 362 | exp : exp '@' exp |
| 363 | { write_exp_elt_opcode (BINOP_REPEAT); } |
| 364 | ; |
| 365 | |
| 366 | exp : exp '*' exp |
| 367 | { write_exp_elt_opcode (BINOP_MUL); } |
| 368 | ; |
| 369 | |
| 370 | exp : exp '/' exp |
| 371 | { write_exp_elt_opcode (BINOP_DIV); } |
| 372 | ; |
| 373 | |
| 374 | exp : exp '%' exp |
| 375 | { write_exp_elt_opcode (BINOP_REM); } |
| 376 | ; |
| 377 | |
| 378 | exp : exp '+' exp |
| 379 | { write_exp_elt_opcode (BINOP_ADD); } |
| 380 | ; |
| 381 | |
| 382 | exp : exp '-' exp |
| 383 | { write_exp_elt_opcode (BINOP_SUB); } |
| 384 | ; |
| 385 | |
| 386 | exp : exp LSH exp |
| 387 | { write_exp_elt_opcode (BINOP_LSH); } |
| 388 | ; |
| 389 | |
| 390 | exp : exp RSH exp |
| 391 | { write_exp_elt_opcode (BINOP_RSH); } |
| 392 | ; |
| 393 | |
| 394 | exp : exp EQUAL exp |
| 395 | { write_exp_elt_opcode (BINOP_EQUAL); } |
| 396 | ; |
| 397 | |
| 398 | exp : exp NOTEQUAL exp |
| 399 | { write_exp_elt_opcode (BINOP_NOTEQUAL); } |
| 400 | ; |
| 401 | |
| 402 | exp : exp LEQ exp |
| 403 | { write_exp_elt_opcode (BINOP_LEQ); } |
| 404 | ; |
| 405 | |
| 406 | exp : exp GEQ exp |
| 407 | { write_exp_elt_opcode (BINOP_GEQ); } |
| 408 | ; |
| 409 | |
| 410 | exp : exp '<' exp |
| 411 | { write_exp_elt_opcode (BINOP_LESS); } |
| 412 | ; |
| 413 | |
| 414 | exp : exp '>' exp |
| 415 | { write_exp_elt_opcode (BINOP_GTR); } |
| 416 | ; |
| 417 | |
| 418 | exp : exp '&' exp |
| 419 | { write_exp_elt_opcode (BINOP_BITWISE_AND); } |
| 420 | ; |
| 421 | |
| 422 | exp : exp '^' exp |
| 423 | { write_exp_elt_opcode (BINOP_BITWISE_XOR); } |
| 424 | ; |
| 425 | |
| 426 | exp : exp '|' exp |
| 427 | { write_exp_elt_opcode (BINOP_BITWISE_IOR); } |
| 428 | ; |
| 429 | |
| 430 | exp : exp ANDAND exp |
| 431 | { write_exp_elt_opcode (BINOP_LOGICAL_AND); } |
| 432 | ; |
| 433 | |
| 434 | exp : exp OROR exp |
| 435 | { write_exp_elt_opcode (BINOP_LOGICAL_OR); } |
| 436 | ; |
| 437 | |
| 438 | exp : exp '?' exp ':' exp %prec '?' |
| 439 | { write_exp_elt_opcode (TERNOP_COND); } |
| 440 | ; |
| 441 | |
| 442 | exp : exp '=' exp |
| 443 | { write_exp_elt_opcode (BINOP_ASSIGN); } |
| 444 | ; |
| 445 | |
| 446 | exp : exp ASSIGN_MODIFY exp |
| 447 | { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); |
| 448 | write_exp_elt_opcode ($2); |
| 449 | write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); } |
| 450 | ; |
| 451 | |
| 452 | exp : INT |
| 453 | { write_exp_elt_opcode (OP_LONG); |
| 454 | write_exp_elt_type ($1.type); |
| 455 | write_exp_elt_longcst ((LONGEST)($1.val)); |
| 456 | write_exp_elt_opcode (OP_LONG); } |
| 457 | ; |
| 458 | |
| 459 | exp : CHAR |
| 460 | { |
| 461 | struct stoken_vector vec; |
| 462 | vec.len = 1; |
| 463 | vec.tokens = &$1; |
| 464 | write_exp_string_vector ($1.type, &vec); |
| 465 | } |
| 466 | ; |
| 467 | |
| 468 | exp : NAME_OR_INT |
| 469 | { YYSTYPE val; |
| 470 | parse_number ($1.stoken.ptr, $1.stoken.length, |
| 471 | 0, &val); |
| 472 | write_exp_elt_opcode (OP_LONG); |
| 473 | write_exp_elt_type (val.typed_val_int.type); |
| 474 | write_exp_elt_longcst ((LONGEST) |
| 475 | val.typed_val_int.val); |
| 476 | write_exp_elt_opcode (OP_LONG); |
| 477 | } |
| 478 | ; |
| 479 | |
| 480 | |
| 481 | exp : FLOAT |
| 482 | { write_exp_elt_opcode (OP_DOUBLE); |
| 483 | write_exp_elt_type ($1.type); |
| 484 | write_exp_elt_dblcst ($1.dval); |
| 485 | write_exp_elt_opcode (OP_DOUBLE); } |
| 486 | ; |
| 487 | |
| 488 | exp : variable |
| 489 | ; |
| 490 | |
| 491 | exp : DOLLAR_VARIABLE |
| 492 | { |
| 493 | write_dollar_variable ($1); |
| 494 | } |
| 495 | ; |
| 496 | |
| 497 | exp : SIZEOF_KEYWORD '(' type ')' %prec UNARY |
| 498 | { |
| 499 | /* TODO(dje): Go objects in structs. */ |
| 500 | write_exp_elt_opcode (OP_LONG); |
| 501 | /* TODO(dje): What's the right type here? */ |
| 502 | write_exp_elt_type (parse_type->builtin_unsigned_int); |
| 503 | CHECK_TYPEDEF ($3); |
| 504 | write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3)); |
| 505 | write_exp_elt_opcode (OP_LONG); |
| 506 | } |
| 507 | ; |
| 508 | |
| 509 | exp : SIZEOF_KEYWORD '(' exp ')' %prec UNARY |
| 510 | { |
| 511 | /* TODO(dje): Go objects in structs. */ |
| 512 | write_exp_elt_opcode (UNOP_SIZEOF); |
| 513 | } |
| 514 | |
| 515 | string_exp: |
| 516 | STRING |
| 517 | { |
| 518 | /* We copy the string here, and not in the |
| 519 | lexer, to guarantee that we do not leak a |
| 520 | string. */ |
| 521 | /* Note that we NUL-terminate here, but just |
| 522 | for convenience. */ |
| 523 | struct typed_stoken *vec = XNEW (struct typed_stoken); |
| 524 | $$.len = 1; |
| 525 | $$.tokens = vec; |
| 526 | |
| 527 | vec->type = $1.type; |
| 528 | vec->length = $1.length; |
| 529 | vec->ptr = malloc ($1.length + 1); |
| 530 | memcpy (vec->ptr, $1.ptr, $1.length + 1); |
| 531 | } |
| 532 | |
| 533 | | string_exp '+' STRING |
| 534 | { |
| 535 | /* Note that we NUL-terminate here, but just |
| 536 | for convenience. */ |
| 537 | char *p; |
| 538 | ++$$.len; |
| 539 | $$.tokens = realloc ($$.tokens, |
| 540 | $$.len * sizeof (struct typed_stoken)); |
| 541 | |
| 542 | p = malloc ($3.length + 1); |
| 543 | memcpy (p, $3.ptr, $3.length + 1); |
| 544 | |
| 545 | $$.tokens[$$.len - 1].type = $3.type; |
| 546 | $$.tokens[$$.len - 1].length = $3.length; |
| 547 | $$.tokens[$$.len - 1].ptr = p; |
| 548 | } |
| 549 | ; |
| 550 | |
| 551 | exp : string_exp %prec ABOVE_COMMA |
| 552 | { |
| 553 | int i; |
| 554 | |
| 555 | write_exp_string_vector (0 /*always utf8*/, &$1); |
| 556 | for (i = 0; i < $1.len; ++i) |
| 557 | free ($1.tokens[i].ptr); |
| 558 | free ($1.tokens); |
| 559 | } |
| 560 | ; |
| 561 | |
| 562 | exp : TRUE_KEYWORD |
| 563 | { write_exp_elt_opcode (OP_BOOL); |
| 564 | write_exp_elt_longcst ((LONGEST) $1); |
| 565 | write_exp_elt_opcode (OP_BOOL); } |
| 566 | ; |
| 567 | |
| 568 | exp : FALSE_KEYWORD |
| 569 | { write_exp_elt_opcode (OP_BOOL); |
| 570 | write_exp_elt_longcst ((LONGEST) $1); |
| 571 | write_exp_elt_opcode (OP_BOOL); } |
| 572 | ; |
| 573 | |
| 574 | variable: name_not_typename ENTRY |
| 575 | { struct symbol *sym = $1.sym; |
| 576 | |
| 577 | if (sym == NULL |
| 578 | || !SYMBOL_IS_ARGUMENT (sym) |
| 579 | || !symbol_read_needs_frame (sym)) |
| 580 | error (_("@entry can be used only for function " |
| 581 | "parameters, not for \"%s\""), |
| 582 | copy_name ($1.stoken)); |
| 583 | |
| 584 | write_exp_elt_opcode (OP_VAR_ENTRY_VALUE); |
| 585 | write_exp_elt_sym (sym); |
| 586 | write_exp_elt_opcode (OP_VAR_ENTRY_VALUE); |
| 587 | } |
| 588 | ; |
| 589 | |
| 590 | variable: name_not_typename |
| 591 | { struct symbol *sym = $1.sym; |
| 592 | |
| 593 | if (sym) |
| 594 | { |
| 595 | if (symbol_read_needs_frame (sym)) |
| 596 | { |
| 597 | if (innermost_block == 0 |
| 598 | || contained_in (block_found, |
| 599 | innermost_block)) |
| 600 | innermost_block = block_found; |
| 601 | } |
| 602 | |
| 603 | write_exp_elt_opcode (OP_VAR_VALUE); |
| 604 | /* We want to use the selected frame, not |
| 605 | another more inner frame which happens to |
| 606 | be in the same block. */ |
| 607 | write_exp_elt_block (NULL); |
| 608 | write_exp_elt_sym (sym); |
| 609 | write_exp_elt_opcode (OP_VAR_VALUE); |
| 610 | } |
| 611 | else if ($1.is_a_field_of_this) |
| 612 | { |
| 613 | /* TODO(dje): Can we get here? |
| 614 | E.g., via a mix of c++ and go? */ |
| 615 | gdb_assert_not_reached ("go with `this' field"); |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | struct minimal_symbol *msymbol; |
| 620 | char *arg = copy_name ($1.stoken); |
| 621 | |
| 622 | msymbol = |
| 623 | lookup_minimal_symbol (arg, NULL, NULL); |
| 624 | if (msymbol != NULL) |
| 625 | write_exp_msymbol (msymbol); |
| 626 | else if (!have_full_symbols () |
| 627 | && !have_partial_symbols ()) |
| 628 | error (_("No symbol table is loaded. " |
| 629 | "Use the \"file\" command.")); |
| 630 | else |
| 631 | error (_("No symbol \"%s\" in current context."), |
| 632 | copy_name ($1.stoken)); |
| 633 | } |
| 634 | } |
| 635 | ; |
| 636 | |
| 637 | /* TODO |
| 638 | method_exp: PACKAGENAME '.' name '.' name |
| 639 | { |
| 640 | } |
| 641 | ; |
| 642 | */ |
| 643 | |
| 644 | type /* Implements (approximately): [*] type-specifier */ |
| 645 | : '*' type |
| 646 | { $$ = lookup_pointer_type ($2); } |
| 647 | | TYPENAME |
| 648 | { $$ = $1.type; } |
| 649 | /* |
| 650 | | STRUCT_KEYWORD name |
| 651 | { $$ = lookup_struct (copy_name ($2), |
| 652 | expression_context_block); } |
| 653 | */ |
| 654 | | BYTE_KEYWORD |
| 655 | { $$ = builtin_go_type (parse_gdbarch) |
| 656 | ->builtin_uint8; } |
| 657 | ; |
| 658 | |
| 659 | /* TODO |
| 660 | name : NAME { $$ = $1.stoken; } |
| 661 | | TYPENAME { $$ = $1.stoken; } |
| 662 | | NAME_OR_INT { $$ = $1.stoken; } |
| 663 | ; |
| 664 | */ |
| 665 | |
| 666 | name_not_typename |
| 667 | : NAME |
| 668 | /* These would be useful if name_not_typename was useful, but it is just |
| 669 | a fake for "variable", so these cause reduce/reduce conflicts because |
| 670 | the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable, |
| 671 | =exp) or just an exp. If name_not_typename was ever used in an lvalue |
| 672 | context where only a name could occur, this might be useful. |
| 673 | | NAME_OR_INT |
| 674 | */ |
| 675 | ; |
| 676 | |
| 677 | %% |
| 678 | |
| 679 | /* Wrapper on parse_c_float to get the type right for Go. */ |
| 680 | |
| 681 | static int |
| 682 | parse_go_float (struct gdbarch *gdbarch, const char *p, int len, |
| 683 | DOUBLEST *d, struct type **t) |
| 684 | { |
| 685 | int result = parse_c_float (gdbarch, p, len, d, t); |
| 686 | const struct builtin_type *builtin_types = builtin_type (gdbarch); |
| 687 | const struct builtin_go_type *builtin_go_types = builtin_go_type (gdbarch); |
| 688 | |
| 689 | if (*t == builtin_types->builtin_float) |
| 690 | *t = builtin_go_types->builtin_float32; |
| 691 | else if (*t == builtin_types->builtin_double) |
| 692 | *t = builtin_go_types->builtin_float64; |
| 693 | |
| 694 | return result; |
| 695 | } |
| 696 | |
| 697 | /* Take care of parsing a number (anything that starts with a digit). |
| 698 | Set yylval and return the token type; update lexptr. |
| 699 | LEN is the number of characters in it. */ |
| 700 | |
| 701 | /* FIXME: Needs some error checking for the float case. */ |
| 702 | /* FIXME(dje): IWBN to use c-exp.y's parse_number if we could. |
| 703 | That will require moving the guts into a function that we both call |
| 704 | as our YYSTYPE is different than c-exp.y's */ |
| 705 | |
| 706 | static int |
| 707 | parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere) |
| 708 | { |
| 709 | /* FIXME: Shouldn't these be unsigned? We don't deal with negative values |
| 710 | here, and we do kind of silly things like cast to unsigned. */ |
| 711 | LONGEST n = 0; |
| 712 | LONGEST prevn = 0; |
| 713 | ULONGEST un; |
| 714 | |
| 715 | int i = 0; |
| 716 | int c; |
| 717 | int base = input_radix; |
| 718 | int unsigned_p = 0; |
| 719 | |
| 720 | /* Number of "L" suffixes encountered. */ |
| 721 | int long_p = 0; |
| 722 | |
| 723 | /* We have found a "L" or "U" suffix. */ |
| 724 | int found_suffix = 0; |
| 725 | |
| 726 | ULONGEST high_bit; |
| 727 | struct type *signed_type; |
| 728 | struct type *unsigned_type; |
| 729 | |
| 730 | if (parsed_float) |
| 731 | { |
| 732 | if (! parse_go_float (parse_gdbarch, p, len, |
| 733 | &putithere->typed_val_float.dval, |
| 734 | &putithere->typed_val_float.type)) |
| 735 | return ERROR; |
| 736 | return FLOAT; |
| 737 | } |
| 738 | |
| 739 | /* Handle base-switching prefixes 0x, 0t, 0d, 0. */ |
| 740 | if (p[0] == '0') |
| 741 | switch (p[1]) |
| 742 | { |
| 743 | case 'x': |
| 744 | case 'X': |
| 745 | if (len >= 3) |
| 746 | { |
| 747 | p += 2; |
| 748 | base = 16; |
| 749 | len -= 2; |
| 750 | } |
| 751 | break; |
| 752 | |
| 753 | case 'b': |
| 754 | case 'B': |
| 755 | if (len >= 3) |
| 756 | { |
| 757 | p += 2; |
| 758 | base = 2; |
| 759 | len -= 2; |
| 760 | } |
| 761 | break; |
| 762 | |
| 763 | case 't': |
| 764 | case 'T': |
| 765 | case 'd': |
| 766 | case 'D': |
| 767 | if (len >= 3) |
| 768 | { |
| 769 | p += 2; |
| 770 | base = 10; |
| 771 | len -= 2; |
| 772 | } |
| 773 | break; |
| 774 | |
| 775 | default: |
| 776 | base = 8; |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | while (len-- > 0) |
| 781 | { |
| 782 | c = *p++; |
| 783 | if (c >= 'A' && c <= 'Z') |
| 784 | c += 'a' - 'A'; |
| 785 | if (c != 'l' && c != 'u') |
| 786 | n *= base; |
| 787 | if (c >= '0' && c <= '9') |
| 788 | { |
| 789 | if (found_suffix) |
| 790 | return ERROR; |
| 791 | n += i = c - '0'; |
| 792 | } |
| 793 | else |
| 794 | { |
| 795 | if (base > 10 && c >= 'a' && c <= 'f') |
| 796 | { |
| 797 | if (found_suffix) |
| 798 | return ERROR; |
| 799 | n += i = c - 'a' + 10; |
| 800 | } |
| 801 | else if (c == 'l') |
| 802 | { |
| 803 | ++long_p; |
| 804 | found_suffix = 1; |
| 805 | } |
| 806 | else if (c == 'u') |
| 807 | { |
| 808 | unsigned_p = 1; |
| 809 | found_suffix = 1; |
| 810 | } |
| 811 | else |
| 812 | return ERROR; /* Char not a digit */ |
| 813 | } |
| 814 | if (i >= base) |
| 815 | return ERROR; /* Invalid digit in this base. */ |
| 816 | |
| 817 | /* Portably test for overflow (only works for nonzero values, so make |
| 818 | a second check for zero). FIXME: Can't we just make n and prevn |
| 819 | unsigned and avoid this? */ |
| 820 | if (c != 'l' && c != 'u' && (prevn >= n) && n != 0) |
| 821 | unsigned_p = 1; /* Try something unsigned. */ |
| 822 | |
| 823 | /* Portably test for unsigned overflow. |
| 824 | FIXME: This check is wrong; for example it doesn't find overflow |
| 825 | on 0x123456789 when LONGEST is 32 bits. */ |
| 826 | if (c != 'l' && c != 'u' && n != 0) |
| 827 | { |
| 828 | if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) |
| 829 | error (_("Numeric constant too large.")); |
| 830 | } |
| 831 | prevn = n; |
| 832 | } |
| 833 | |
| 834 | /* An integer constant is an int, a long, or a long long. An L |
| 835 | suffix forces it to be long; an LL suffix forces it to be long |
| 836 | long. If not forced to a larger size, it gets the first type of |
| 837 | the above that it fits in. To figure out whether it fits, we |
| 838 | shift it right and see whether anything remains. Note that we |
| 839 | can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one |
| 840 | operation, because many compilers will warn about such a shift |
| 841 | (which always produces a zero result). Sometimes gdbarch_int_bit |
| 842 | or gdbarch_long_bit will be that big, sometimes not. To deal with |
| 843 | the case where it is we just always shift the value more than |
| 844 | once, with fewer bits each time. */ |
| 845 | |
| 846 | un = (ULONGEST)n >> 2; |
| 847 | if (long_p == 0 |
| 848 | && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0) |
| 849 | { |
| 850 | high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1); |
| 851 | |
| 852 | /* A large decimal (not hex or octal) constant (between INT_MAX |
| 853 | and UINT_MAX) is a long or unsigned long, according to ANSI, |
| 854 | never an unsigned int, but this code treats it as unsigned |
| 855 | int. This probably should be fixed. GCC gives a warning on |
| 856 | such constants. */ |
| 857 | |
| 858 | unsigned_type = parse_type->builtin_unsigned_int; |
| 859 | signed_type = parse_type->builtin_int; |
| 860 | } |
| 861 | else if (long_p <= 1 |
| 862 | && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0) |
| 863 | { |
| 864 | high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1); |
| 865 | unsigned_type = parse_type->builtin_unsigned_long; |
| 866 | signed_type = parse_type->builtin_long; |
| 867 | } |
| 868 | else |
| 869 | { |
| 870 | int shift; |
| 871 | if (sizeof (ULONGEST) * HOST_CHAR_BIT |
| 872 | < gdbarch_long_long_bit (parse_gdbarch)) |
| 873 | /* A long long does not fit in a LONGEST. */ |
| 874 | shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1); |
| 875 | else |
| 876 | shift = (gdbarch_long_long_bit (parse_gdbarch) - 1); |
| 877 | high_bit = (ULONGEST) 1 << shift; |
| 878 | unsigned_type = parse_type->builtin_unsigned_long_long; |
| 879 | signed_type = parse_type->builtin_long_long; |
| 880 | } |
| 881 | |
| 882 | putithere->typed_val_int.val = n; |
| 883 | |
| 884 | /* If the high bit of the worked out type is set then this number |
| 885 | has to be unsigned. */ |
| 886 | |
| 887 | if (unsigned_p || (n & high_bit)) |
| 888 | { |
| 889 | putithere->typed_val_int.type = unsigned_type; |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | putithere->typed_val_int.type = signed_type; |
| 894 | } |
| 895 | |
| 896 | return INT; |
| 897 | } |
| 898 | |
| 899 | /* Temporary obstack used for holding strings. */ |
| 900 | static struct obstack tempbuf; |
| 901 | static int tempbuf_init; |
| 902 | |
| 903 | /* Parse a string or character literal from TOKPTR. The string or |
| 904 | character may be wide or unicode. *OUTPTR is set to just after the |
| 905 | end of the literal in the input string. The resulting token is |
| 906 | stored in VALUE. This returns a token value, either STRING or |
| 907 | CHAR, depending on what was parsed. *HOST_CHARS is set to the |
| 908 | number of host characters in the literal. */ |
| 909 | |
| 910 | static int |
| 911 | parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value, |
| 912 | int *host_chars) |
| 913 | { |
| 914 | int quote; |
| 915 | |
| 916 | /* Build the gdb internal form of the input string in tempbuf. Note |
| 917 | that the buffer is null byte terminated *only* for the |
| 918 | convenience of debugging gdb itself and printing the buffer |
| 919 | contents when the buffer contains no embedded nulls. Gdb does |
| 920 | not depend upon the buffer being null byte terminated, it uses |
| 921 | the length string instead. This allows gdb to handle C strings |
| 922 | (as well as strings in other languages) with embedded null |
| 923 | bytes */ |
| 924 | |
| 925 | if (!tempbuf_init) |
| 926 | tempbuf_init = 1; |
| 927 | else |
| 928 | obstack_free (&tempbuf, NULL); |
| 929 | obstack_init (&tempbuf); |
| 930 | |
| 931 | /* Skip the quote. */ |
| 932 | quote = *tokptr; |
| 933 | ++tokptr; |
| 934 | |
| 935 | *host_chars = 0; |
| 936 | |
| 937 | while (*tokptr) |
| 938 | { |
| 939 | char c = *tokptr; |
| 940 | if (c == '\\') |
| 941 | { |
| 942 | ++tokptr; |
| 943 | *host_chars += c_parse_escape (&tokptr, &tempbuf); |
| 944 | } |
| 945 | else if (c == quote) |
| 946 | break; |
| 947 | else |
| 948 | { |
| 949 | obstack_1grow (&tempbuf, c); |
| 950 | ++tokptr; |
| 951 | /* FIXME: this does the wrong thing with multi-byte host |
| 952 | characters. We could use mbrlen here, but that would |
| 953 | make "set host-charset" a bit less useful. */ |
| 954 | ++*host_chars; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | if (*tokptr != quote) |
| 959 | { |
| 960 | if (quote == '"') |
| 961 | error (_("Unterminated string in expression.")); |
| 962 | else |
| 963 | error (_("Unmatched single quote.")); |
| 964 | } |
| 965 | ++tokptr; |
| 966 | |
| 967 | value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/ |
| 968 | value->ptr = obstack_base (&tempbuf); |
| 969 | value->length = obstack_object_size (&tempbuf); |
| 970 | |
| 971 | *outptr = tokptr; |
| 972 | |
| 973 | return quote == '\'' ? CHAR : STRING; |
| 974 | } |
| 975 | |
| 976 | struct token |
| 977 | { |
| 978 | char *operator; |
| 979 | int token; |
| 980 | enum exp_opcode opcode; |
| 981 | }; |
| 982 | |
| 983 | static const struct token tokentab3[] = |
| 984 | { |
| 985 | {">>=", ASSIGN_MODIFY, BINOP_RSH}, |
| 986 | {"<<=", ASSIGN_MODIFY, BINOP_LSH}, |
| 987 | /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */ |
| 988 | {"...", DOTDOTDOT, OP_NULL}, |
| 989 | }; |
| 990 | |
| 991 | static const struct token tokentab2[] = |
| 992 | { |
| 993 | {"+=", ASSIGN_MODIFY, BINOP_ADD}, |
| 994 | {"-=", ASSIGN_MODIFY, BINOP_SUB}, |
| 995 | {"*=", ASSIGN_MODIFY, BINOP_MUL}, |
| 996 | {"/=", ASSIGN_MODIFY, BINOP_DIV}, |
| 997 | {"%=", ASSIGN_MODIFY, BINOP_REM}, |
| 998 | {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR}, |
| 999 | {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND}, |
| 1000 | {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR}, |
| 1001 | {"++", INCREMENT, BINOP_END}, |
| 1002 | {"--", DECREMENT, BINOP_END}, |
| 1003 | /*{"->", RIGHT_ARROW, BINOP_END}, Doesn't exist in Go. */ |
| 1004 | {"<-", LEFT_ARROW, BINOP_END}, |
| 1005 | {"&&", ANDAND, BINOP_END}, |
| 1006 | {"||", OROR, BINOP_END}, |
| 1007 | {"<<", LSH, BINOP_END}, |
| 1008 | {">>", RSH, BINOP_END}, |
| 1009 | {"==", EQUAL, BINOP_END}, |
| 1010 | {"!=", NOTEQUAL, BINOP_END}, |
| 1011 | {"<=", LEQ, BINOP_END}, |
| 1012 | {">=", GEQ, BINOP_END}, |
| 1013 | /*{"&^", ANDNOT, BINOP_END}, TODO */ |
| 1014 | }; |
| 1015 | |
| 1016 | /* Identifier-like tokens. */ |
| 1017 | static const struct token ident_tokens[] = |
| 1018 | { |
| 1019 | {"true", TRUE_KEYWORD, OP_NULL}, |
| 1020 | {"false", FALSE_KEYWORD, OP_NULL}, |
| 1021 | {"nil", NIL_KEYWORD, OP_NULL}, |
| 1022 | {"const", CONST_KEYWORD, OP_NULL}, |
| 1023 | {"struct", STRUCT_KEYWORD, OP_NULL}, |
| 1024 | {"type", TYPE_KEYWORD, OP_NULL}, |
| 1025 | {"interface", INTERFACE_KEYWORD, OP_NULL}, |
| 1026 | {"chan", CHAN_KEYWORD, OP_NULL}, |
| 1027 | {"byte", BYTE_KEYWORD, OP_NULL}, /* An alias of uint8. */ |
| 1028 | {"len", LEN_KEYWORD, OP_NULL}, |
| 1029 | {"cap", CAP_KEYWORD, OP_NULL}, |
| 1030 | {"new", NEW_KEYWORD, OP_NULL}, |
| 1031 | {"iota", IOTA_KEYWORD, OP_NULL}, |
| 1032 | }; |
| 1033 | |
| 1034 | /* This is set if a NAME token appeared at the very end of the input |
| 1035 | string, with no whitespace separating the name from the EOF. This |
| 1036 | is used only when parsing to do field name completion. */ |
| 1037 | static int saw_name_at_eof; |
| 1038 | |
| 1039 | /* This is set if the previously-returned token was a structure |
| 1040 | operator -- either '.' or ARROW. This is used only when parsing to |
| 1041 | do field name completion. */ |
| 1042 | static int last_was_structop; |
| 1043 | |
| 1044 | /* Read one token, getting characters through lexptr. */ |
| 1045 | |
| 1046 | static int |
| 1047 | lex_one_token (void) |
| 1048 | { |
| 1049 | int c; |
| 1050 | int namelen; |
| 1051 | unsigned int i; |
| 1052 | char *tokstart; |
| 1053 | int saw_structop = last_was_structop; |
| 1054 | char *copy; |
| 1055 | |
| 1056 | last_was_structop = 0; |
| 1057 | |
| 1058 | retry: |
| 1059 | |
| 1060 | prev_lexptr = lexptr; |
| 1061 | |
| 1062 | tokstart = lexptr; |
| 1063 | /* See if it is a special token of length 3. */ |
| 1064 | for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++) |
| 1065 | if (strncmp (tokstart, tokentab3[i].operator, 3) == 0) |
| 1066 | { |
| 1067 | lexptr += 3; |
| 1068 | yylval.opcode = tokentab3[i].opcode; |
| 1069 | return tokentab3[i].token; |
| 1070 | } |
| 1071 | |
| 1072 | /* See if it is a special token of length 2. */ |
| 1073 | for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++) |
| 1074 | if (strncmp (tokstart, tokentab2[i].operator, 2) == 0) |
| 1075 | { |
| 1076 | lexptr += 2; |
| 1077 | yylval.opcode = tokentab2[i].opcode; |
| 1078 | /* NOTE: -> doesn't exist in Go, so we don't need to watch for |
| 1079 | setting last_was_structop here. */ |
| 1080 | return tokentab2[i].token; |
| 1081 | } |
| 1082 | |
| 1083 | switch (c = *tokstart) |
| 1084 | { |
| 1085 | case 0: |
| 1086 | if (saw_name_at_eof) |
| 1087 | { |
| 1088 | saw_name_at_eof = 0; |
| 1089 | return COMPLETE; |
| 1090 | } |
| 1091 | else if (saw_structop) |
| 1092 | return COMPLETE; |
| 1093 | else |
| 1094 | return 0; |
| 1095 | |
| 1096 | case ' ': |
| 1097 | case '\t': |
| 1098 | case '\n': |
| 1099 | lexptr++; |
| 1100 | goto retry; |
| 1101 | |
| 1102 | case '[': |
| 1103 | case '(': |
| 1104 | paren_depth++; |
| 1105 | lexptr++; |
| 1106 | return c; |
| 1107 | |
| 1108 | case ']': |
| 1109 | case ')': |
| 1110 | if (paren_depth == 0) |
| 1111 | return 0; |
| 1112 | paren_depth--; |
| 1113 | lexptr++; |
| 1114 | return c; |
| 1115 | |
| 1116 | case ',': |
| 1117 | if (comma_terminates |
| 1118 | && paren_depth == 0) |
| 1119 | return 0; |
| 1120 | lexptr++; |
| 1121 | return c; |
| 1122 | |
| 1123 | case '.': |
| 1124 | /* Might be a floating point number. */ |
| 1125 | if (lexptr[1] < '0' || lexptr[1] > '9') |
| 1126 | { |
| 1127 | if (in_parse_field) |
| 1128 | last_was_structop = 1; |
| 1129 | goto symbol; /* Nope, must be a symbol. */ |
| 1130 | } |
| 1131 | /* FALL THRU into number case. */ |
| 1132 | |
| 1133 | case '0': |
| 1134 | case '1': |
| 1135 | case '2': |
| 1136 | case '3': |
| 1137 | case '4': |
| 1138 | case '5': |
| 1139 | case '6': |
| 1140 | case '7': |
| 1141 | case '8': |
| 1142 | case '9': |
| 1143 | { |
| 1144 | /* It's a number. */ |
| 1145 | int got_dot = 0, got_e = 0, toktype; |
| 1146 | char *p = tokstart; |
| 1147 | int hex = input_radix > 10; |
| 1148 | |
| 1149 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) |
| 1150 | { |
| 1151 | p += 2; |
| 1152 | hex = 1; |
| 1153 | } |
| 1154 | |
| 1155 | for (;; ++p) |
| 1156 | { |
| 1157 | /* This test includes !hex because 'e' is a valid hex digit |
| 1158 | and thus does not indicate a floating point number when |
| 1159 | the radix is hex. */ |
| 1160 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) |
| 1161 | got_dot = got_e = 1; |
| 1162 | /* This test does not include !hex, because a '.' always indicates |
| 1163 | a decimal floating point number regardless of the radix. */ |
| 1164 | else if (!got_dot && *p == '.') |
| 1165 | got_dot = 1; |
| 1166 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') |
| 1167 | && (*p == '-' || *p == '+')) |
| 1168 | /* This is the sign of the exponent, not the end of the |
| 1169 | number. */ |
| 1170 | continue; |
| 1171 | /* We will take any letters or digits. parse_number will |
| 1172 | complain if past the radix, or if L or U are not final. */ |
| 1173 | else if ((*p < '0' || *p > '9') |
| 1174 | && ((*p < 'a' || *p > 'z') |
| 1175 | && (*p < 'A' || *p > 'Z'))) |
| 1176 | break; |
| 1177 | } |
| 1178 | toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval); |
| 1179 | if (toktype == ERROR) |
| 1180 | { |
| 1181 | char *err_copy = (char *) alloca (p - tokstart + 1); |
| 1182 | |
| 1183 | memcpy (err_copy, tokstart, p - tokstart); |
| 1184 | err_copy[p - tokstart] = 0; |
| 1185 | error (_("Invalid number \"%s\"."), err_copy); |
| 1186 | } |
| 1187 | lexptr = p; |
| 1188 | return toktype; |
| 1189 | } |
| 1190 | |
| 1191 | case '@': |
| 1192 | { |
| 1193 | char *p = &tokstart[1]; |
| 1194 | size_t len = strlen ("entry"); |
| 1195 | |
| 1196 | while (isspace (*p)) |
| 1197 | p++; |
| 1198 | if (strncmp (p, "entry", len) == 0 && !isalnum (p[len]) |
| 1199 | && p[len] != '_') |
| 1200 | { |
| 1201 | lexptr = &p[len]; |
| 1202 | return ENTRY; |
| 1203 | } |
| 1204 | } |
| 1205 | /* FALLTHRU */ |
| 1206 | case '+': |
| 1207 | case '-': |
| 1208 | case '*': |
| 1209 | case '/': |
| 1210 | case '%': |
| 1211 | case '|': |
| 1212 | case '&': |
| 1213 | case '^': |
| 1214 | case '~': |
| 1215 | case '!': |
| 1216 | case '<': |
| 1217 | case '>': |
| 1218 | case '?': |
| 1219 | case ':': |
| 1220 | case '=': |
| 1221 | case '{': |
| 1222 | case '}': |
| 1223 | symbol: |
| 1224 | lexptr++; |
| 1225 | return c; |
| 1226 | |
| 1227 | case '\'': |
| 1228 | case '"': |
| 1229 | case '`': |
| 1230 | { |
| 1231 | int host_len; |
| 1232 | int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval, |
| 1233 | &host_len); |
| 1234 | if (result == CHAR) |
| 1235 | { |
| 1236 | if (host_len == 0) |
| 1237 | error (_("Empty character constant.")); |
| 1238 | else if (host_len > 2 && c == '\'') |
| 1239 | { |
| 1240 | ++tokstart; |
| 1241 | namelen = lexptr - tokstart - 1; |
| 1242 | goto tryname; |
| 1243 | } |
| 1244 | else if (host_len > 1) |
| 1245 | error (_("Invalid character constant.")); |
| 1246 | } |
| 1247 | return result; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | if (!(c == '_' || c == '$' |
| 1252 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) |
| 1253 | /* We must have come across a bad character (e.g. ';'). */ |
| 1254 | error (_("Invalid character '%c' in expression."), c); |
| 1255 | |
| 1256 | /* It's a name. See how long it is. */ |
| 1257 | namelen = 0; |
| 1258 | for (c = tokstart[namelen]; |
| 1259 | (c == '_' || c == '$' || (c >= '0' && c <= '9') |
| 1260 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));) |
| 1261 | { |
| 1262 | c = tokstart[++namelen]; |
| 1263 | } |
| 1264 | |
| 1265 | /* The token "if" terminates the expression and is NOT removed from |
| 1266 | the input stream. It doesn't count if it appears in the |
| 1267 | expansion of a macro. */ |
| 1268 | if (namelen == 2 |
| 1269 | && tokstart[0] == 'i' |
| 1270 | && tokstart[1] == 'f') |
| 1271 | { |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | /* For the same reason (breakpoint conditions), "thread N" |
| 1276 | terminates the expression. "thread" could be an identifier, but |
| 1277 | an identifier is never followed by a number without intervening |
| 1278 | punctuation. |
| 1279 | Handle abbreviations of these, similarly to |
| 1280 | breakpoint.c:find_condition_and_thread. |
| 1281 | TODO: Watch for "goroutine" here? */ |
| 1282 | if (namelen >= 1 |
| 1283 | && strncmp (tokstart, "thread", namelen) == 0 |
| 1284 | && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')) |
| 1285 | { |
| 1286 | char *p = tokstart + namelen + 1; |
| 1287 | while (*p == ' ' || *p == '\t') |
| 1288 | p++; |
| 1289 | if (*p >= '0' && *p <= '9') |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | lexptr += namelen; |
| 1294 | |
| 1295 | tryname: |
| 1296 | |
| 1297 | yylval.sval.ptr = tokstart; |
| 1298 | yylval.sval.length = namelen; |
| 1299 | |
| 1300 | /* Catch specific keywords. */ |
| 1301 | copy = copy_name (yylval.sval); |
| 1302 | for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++) |
| 1303 | if (strcmp (copy, ident_tokens[i].operator) == 0) |
| 1304 | { |
| 1305 | /* It is ok to always set this, even though we don't always |
| 1306 | strictly need to. */ |
| 1307 | yylval.opcode = ident_tokens[i].opcode; |
| 1308 | return ident_tokens[i].token; |
| 1309 | } |
| 1310 | |
| 1311 | if (*tokstart == '$') |
| 1312 | return DOLLAR_VARIABLE; |
| 1313 | |
| 1314 | if (in_parse_field && *lexptr == '\0') |
| 1315 | saw_name_at_eof = 1; |
| 1316 | return NAME; |
| 1317 | } |
| 1318 | |
| 1319 | /* An object of this type is pushed on a FIFO by the "outer" lexer. */ |
| 1320 | typedef struct |
| 1321 | { |
| 1322 | int token; |
| 1323 | YYSTYPE value; |
| 1324 | } token_and_value; |
| 1325 | |
| 1326 | DEF_VEC_O (token_and_value); |
| 1327 | |
| 1328 | /* A FIFO of tokens that have been read but not yet returned to the |
| 1329 | parser. */ |
| 1330 | static VEC (token_and_value) *token_fifo; |
| 1331 | |
| 1332 | /* Non-zero if the lexer should return tokens from the FIFO. */ |
| 1333 | static int popping; |
| 1334 | |
| 1335 | /* Temporary storage for yylex; this holds symbol names as they are |
| 1336 | built up. */ |
| 1337 | static struct obstack name_obstack; |
| 1338 | |
| 1339 | /* Build "package.name" in name_obstack. |
| 1340 | For convenience of the caller, the name is NUL-terminated, |
| 1341 | but the NUL is not included in the recorded length. */ |
| 1342 | |
| 1343 | static struct stoken |
| 1344 | build_packaged_name (const char *package, int package_len, |
| 1345 | const char *name, int name_len) |
| 1346 | { |
| 1347 | struct stoken result; |
| 1348 | |
| 1349 | obstack_free (&name_obstack, obstack_base (&name_obstack)); |
| 1350 | obstack_grow (&name_obstack, package, package_len); |
| 1351 | obstack_grow_str (&name_obstack, "."); |
| 1352 | obstack_grow (&name_obstack, name, name_len); |
| 1353 | obstack_grow (&name_obstack, "", 1); |
| 1354 | result.ptr = obstack_base (&name_obstack); |
| 1355 | result.length = obstack_object_size (&name_obstack) - 1; |
| 1356 | |
| 1357 | return result; |
| 1358 | } |
| 1359 | |
| 1360 | /* Return non-zero if NAME is a package name. |
| 1361 | BLOCK is the scope in which to interpret NAME; this can be NULL |
| 1362 | to mean the global scope. */ |
| 1363 | |
| 1364 | static int |
| 1365 | package_name_p (const char *name, struct block *block) |
| 1366 | { |
| 1367 | struct symbol *sym; |
| 1368 | int is_a_field_of_this; |
| 1369 | |
| 1370 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this); |
| 1371 | |
| 1372 | if (sym |
| 1373 | && SYMBOL_CLASS (sym) == LOC_TYPEDEF |
| 1374 | && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE) |
| 1375 | return 1; |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | /* Classify a (potential) function in the "unsafe" package. |
| 1381 | We fold these into "keywords" to keep things simple, at least until |
| 1382 | something more complex is warranted. */ |
| 1383 | |
| 1384 | static int |
| 1385 | classify_unsafe_function (struct stoken function_name) |
| 1386 | { |
| 1387 | char *copy = copy_name (function_name); |
| 1388 | |
| 1389 | if (strcmp (copy, "Sizeof") == 0) |
| 1390 | { |
| 1391 | yylval.sval = function_name; |
| 1392 | return SIZEOF_KEYWORD; |
| 1393 | } |
| 1394 | |
| 1395 | error (_("Unknown function in `unsafe' package: %s"), copy); |
| 1396 | } |
| 1397 | |
| 1398 | /* Classify token(s) "name1.name2" where name1 is known to be a package. |
| 1399 | The contents of the token are in `yylval'. |
| 1400 | Updates yylval and returns the new token type. |
| 1401 | |
| 1402 | The result is one of NAME, NAME_OR_INT, or TYPENAME. */ |
| 1403 | |
| 1404 | static int |
| 1405 | classify_packaged_name (struct block *block) |
| 1406 | { |
| 1407 | char *copy; |
| 1408 | struct symbol *sym; |
| 1409 | int is_a_field_of_this = 0; |
| 1410 | |
| 1411 | copy = copy_name (yylval.sval); |
| 1412 | |
| 1413 | sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this); |
| 1414 | |
| 1415 | if (sym) |
| 1416 | { |
| 1417 | yylval.ssym.sym = sym; |
| 1418 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; |
| 1419 | } |
| 1420 | |
| 1421 | return NAME; |
| 1422 | } |
| 1423 | |
| 1424 | /* Classify a NAME token. |
| 1425 | The contents of the token are in `yylval'. |
| 1426 | Updates yylval and returns the new token type. |
| 1427 | BLOCK is the block in which lookups start; this can be NULL |
| 1428 | to mean the global scope. |
| 1429 | |
| 1430 | The result is one of NAME, NAME_OR_INT, or TYPENAME. */ |
| 1431 | |
| 1432 | static int |
| 1433 | classify_name (struct block *block) |
| 1434 | { |
| 1435 | struct type *type; |
| 1436 | struct symbol *sym; |
| 1437 | char *copy; |
| 1438 | int is_a_field_of_this = 0; |
| 1439 | |
| 1440 | copy = copy_name (yylval.sval); |
| 1441 | |
| 1442 | /* Try primitive types first so they win over bad/weird debug info. */ |
| 1443 | type = language_lookup_primitive_type_by_name (parse_language, |
| 1444 | parse_gdbarch, copy); |
| 1445 | if (type != NULL) |
| 1446 | { |
| 1447 | /* NOTE: We take advantage of the fact that yylval coming in was a |
| 1448 | NAME, and that struct ttype is a compatible extension of struct |
| 1449 | stoken, so yylval.tsym.stoken is already filled in. */ |
| 1450 | yylval.tsym.type = type; |
| 1451 | return TYPENAME; |
| 1452 | } |
| 1453 | |
| 1454 | /* TODO: What about other types? */ |
| 1455 | |
| 1456 | sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this); |
| 1457 | |
| 1458 | if (sym) |
| 1459 | { |
| 1460 | yylval.ssym.sym = sym; |
| 1461 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; |
| 1462 | return NAME; |
| 1463 | } |
| 1464 | |
| 1465 | /* If we didn't find a symbol, look again in the current package. |
| 1466 | This is to, e.g., make "p global_var" work without having to specify |
| 1467 | the package name. We intentionally only looks for objects in the |
| 1468 | current package. */ |
| 1469 | |
| 1470 | { |
| 1471 | char *current_package_name = go_block_package_name (block); |
| 1472 | |
| 1473 | if (current_package_name != NULL) |
| 1474 | { |
| 1475 | struct stoken sval = |
| 1476 | build_packaged_name (current_package_name, |
| 1477 | strlen (current_package_name), |
| 1478 | copy, strlen (copy)); |
| 1479 | |
| 1480 | xfree (current_package_name); |
| 1481 | sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN, |
| 1482 | &is_a_field_of_this); |
| 1483 | if (sym) |
| 1484 | { |
| 1485 | yylval.ssym.stoken = sval; |
| 1486 | yylval.ssym.sym = sym; |
| 1487 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; |
| 1488 | return NAME; |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | /* Input names that aren't symbols but ARE valid hex numbers, when |
| 1494 | the input radix permits them, can be names or numbers depending |
| 1495 | on the parse. Note we support radixes > 16 here. */ |
| 1496 | if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10) |
| 1497 | || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)) |
| 1498 | { |
| 1499 | YYSTYPE newlval; /* Its value is ignored. */ |
| 1500 | int hextype = parse_number (copy, yylval.sval.length, 0, &newlval); |
| 1501 | if (hextype == INT) |
| 1502 | { |
| 1503 | yylval.ssym.sym = NULL; |
| 1504 | yylval.ssym.is_a_field_of_this = 0; |
| 1505 | return NAME_OR_INT; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | yylval.ssym.sym = NULL; |
| 1510 | yylval.ssym.is_a_field_of_this = 0; |
| 1511 | return NAME; |
| 1512 | } |
| 1513 | |
| 1514 | /* This is taken from c-exp.y mostly to get something working. |
| 1515 | The basic structure has been kept because we may yet need some of it. */ |
| 1516 | |
| 1517 | static int |
| 1518 | yylex (void) |
| 1519 | { |
| 1520 | token_and_value current, next; |
| 1521 | |
| 1522 | if (popping && !VEC_empty (token_and_value, token_fifo)) |
| 1523 | { |
| 1524 | token_and_value tv = *VEC_index (token_and_value, token_fifo, 0); |
| 1525 | VEC_ordered_remove (token_and_value, token_fifo, 0); |
| 1526 | yylval = tv.value; |
| 1527 | /* There's no need to fall through to handle package.name |
| 1528 | as that can never happen here. In theory. */ |
| 1529 | return tv.token; |
| 1530 | } |
| 1531 | popping = 0; |
| 1532 | |
| 1533 | current.token = lex_one_token (); |
| 1534 | |
| 1535 | /* TODO: Need a way to force specifying name1 as a package. |
| 1536 | .name1.name2 ? */ |
| 1537 | |
| 1538 | if (current.token != NAME) |
| 1539 | return current.token; |
| 1540 | |
| 1541 | /* See if we have "name1 . name2". */ |
| 1542 | |
| 1543 | current.value = yylval; |
| 1544 | next.token = lex_one_token (); |
| 1545 | next.value = yylval; |
| 1546 | |
| 1547 | if (next.token == '.') |
| 1548 | { |
| 1549 | token_and_value name2; |
| 1550 | |
| 1551 | name2.token = lex_one_token (); |
| 1552 | name2.value = yylval; |
| 1553 | |
| 1554 | if (name2.token == NAME) |
| 1555 | { |
| 1556 | /* Ok, we have "name1 . name2". */ |
| 1557 | char *copy; |
| 1558 | |
| 1559 | copy = copy_name (current.value.sval); |
| 1560 | |
| 1561 | if (strcmp (copy, "unsafe") == 0) |
| 1562 | { |
| 1563 | popping = 1; |
| 1564 | return classify_unsafe_function (name2.value.sval); |
| 1565 | } |
| 1566 | |
| 1567 | if (package_name_p (copy, expression_context_block)) |
| 1568 | { |
| 1569 | popping = 1; |
| 1570 | yylval.sval = build_packaged_name (current.value.sval.ptr, |
| 1571 | current.value.sval.length, |
| 1572 | name2.value.sval.ptr, |
| 1573 | name2.value.sval.length); |
| 1574 | return classify_packaged_name (expression_context_block); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | VEC_safe_push (token_and_value, token_fifo, &next); |
| 1579 | VEC_safe_push (token_and_value, token_fifo, &name2); |
| 1580 | } |
| 1581 | else |
| 1582 | { |
| 1583 | VEC_safe_push (token_and_value, token_fifo, &next); |
| 1584 | } |
| 1585 | |
| 1586 | /* If we arrive here we don't have a package-qualified name. */ |
| 1587 | |
| 1588 | popping = 1; |
| 1589 | yylval = current.value; |
| 1590 | return classify_name (expression_context_block); |
| 1591 | } |
| 1592 | |
| 1593 | int |
| 1594 | go_parse (void) |
| 1595 | { |
| 1596 | int result; |
| 1597 | struct cleanup *back_to = make_cleanup (null_cleanup, NULL); |
| 1598 | |
| 1599 | make_cleanup_restore_integer (&yydebug); |
| 1600 | yydebug = parser_debug; |
| 1601 | |
| 1602 | /* Initialize some state used by the lexer. */ |
| 1603 | last_was_structop = 0; |
| 1604 | saw_name_at_eof = 0; |
| 1605 | |
| 1606 | VEC_free (token_and_value, token_fifo); |
| 1607 | popping = 0; |
| 1608 | obstack_init (&name_obstack); |
| 1609 | make_cleanup_obstack_free (&name_obstack); |
| 1610 | |
| 1611 | result = yyparse (); |
| 1612 | do_cleanups (back_to); |
| 1613 | return result; |
| 1614 | } |
| 1615 | |
| 1616 | void |
| 1617 | yyerror (char *msg) |
| 1618 | { |
| 1619 | if (prev_lexptr) |
| 1620 | lexptr = prev_lexptr; |
| 1621 | |
| 1622 | error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr); |
| 1623 | } |