1 /* YACC parser for Go expressions, for GDB.
3 Copyright (C) 2012-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This file is derived from c-exp.y, p-exp.y. */
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.
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. */
39 /* Known bugs or limitations:
43 - '_' (blank identifier)
44 - automatic deref of pointers
46 - interfaces, channels, etc.
48 And lots of other things.
49 I'm sure there's some cleanup to do.
56 #include "expression.h"
58 #include "parser-defs.h"
62 #include "bfd.h" /* Required by objfiles.h. */
63 #include "symfile.h" /* Required by objfiles.h. */
64 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
68 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
70 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
71 as well as gratuitiously global symbol names, so we can have multiple
72 yacc generated parsers in gdb. Note that these are only the variables
73 produced by yacc. If other parser generators (bison, byacc, etc) produce
74 additional global names that conflict at link time, then those parser
75 generators need to be fixed instead of adding those names to this list. */
77 #define yymaxdepth go_maxdepth
78 #define yyparse go_parse_internal
80 #define yyerror go_error
81 #define yylval go_lval
82 #define yychar go_char
83 #define yydebug go_debug
84 #define yypact go_pact
91 #define yyexca go_exca
92 #define yyerrflag go_errflag
93 #define yynerrs go_nerrs
98 #define yystate go_state
101 #define yy_yyv go_yyv
103 #define yylloc go_lloc
104 #define yyreds go_reds /* With YYDEBUG defined */
105 #define yytoks go_toks /* With YYDEBUG defined */
106 #define yyname go_name /* With YYDEBUG defined */
107 #define yyrule go_rule /* With YYDEBUG defined */
108 #define yylhs go_yylhs
109 #define yylen go_yylen
110 #define yydefred go_yydefred
111 #define yydgoto go_yydgoto
112 #define yysindex go_yysindex
113 #define yyrindex go_yyrindex
114 #define yygindex go_yygindex
115 #define yytable go_yytable
116 #define yycheck go_yycheck
119 #define YYDEBUG 1 /* Default to yydebug support */
122 #define YYFPRINTF parser_fprintf
124 /* The state of the parser, used internally when we are parsing the
127 static struct parser_state *pstate = NULL;
131 static int yylex (void);
133 void yyerror (char *);
137 /* Although the yacc "value" of an expression is not used,
138 since the result is stored in the structure being created,
139 other node types do have values. */
153 struct symtoken ssym;
155 struct typed_stoken tsval;
158 enum exp_opcode opcode;
159 struct internalvar *ivar;
160 struct stoken_vector svec;
164 /* YYSTYPE gets defined by %union. */
165 static int parse_number (struct parser_state *,
166 const char *, int, int, YYSTYPE *);
167 static int parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
168 DOUBLEST *d, struct type **t);
171 %type <voidval> exp exp1 type_exp start variable lcurly
175 %token <typed_val_int> INT
176 %token <typed_val_float> FLOAT
178 /* Both NAME and TYPENAME tokens represent symbols in the input,
179 and both convey their data as strings.
180 But a TYPENAME is a string that happens to be defined as a type
181 or builtin type name (such as int or char)
182 and a NAME is any other symbol.
183 Contexts where this distinction is not important can use the
184 nonterminal "name", which matches either NAME or TYPENAME. */
186 %token <tsval> RAW_STRING
187 %token <tsval> STRING
190 %token <tsym> TYPENAME /* Not TYPE_NAME cus already taken. */
191 %token <voidval> COMPLETE
192 /*%type <sval> name*/
193 %type <svec> string_exp
194 %type <ssym> name_not_typename
196 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
197 but which would parse as a valid number in the current input radix.
198 E.g. "c" when input_radix==16. Depending on the parse, it will be
199 turned into a name or into a number. */
200 %token <ssym> NAME_OR_INT
202 %token <lval> TRUE_KEYWORD FALSE_KEYWORD
203 %token STRUCT_KEYWORD INTERFACE_KEYWORD TYPE_KEYWORD CHAN_KEYWORD
204 %token SIZEOF_KEYWORD
205 %token LEN_KEYWORD CAP_KEYWORD
207 %token IOTA_KEYWORD NIL_KEYWORD
213 /* Special type cases. */
214 %token BYTE_KEYWORD /* An alias of uint8. */
216 %token <sval> DOLLAR_VARIABLE
218 %token <opcode> ASSIGN_MODIFY
222 %right '=' ASSIGN_MODIFY
231 %left '<' '>' LEQ GEQ
236 %right UNARY INCREMENT DECREMENT
237 %right LEFT_ARROW '.' '[' '('
247 { write_exp_elt_opcode (pstate, OP_TYPE);
248 write_exp_elt_type (pstate, $1);
249 write_exp_elt_opcode (pstate, OP_TYPE); }
252 /* Expressions, including the comma operator. */
255 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
258 /* Expressions, not including the comma operator. */
259 exp : '*' exp %prec UNARY
260 { write_exp_elt_opcode (pstate, UNOP_IND); }
263 exp : '&' exp %prec UNARY
264 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
267 exp : '-' exp %prec UNARY
268 { write_exp_elt_opcode (pstate, UNOP_NEG); }
271 exp : '+' exp %prec UNARY
272 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
275 exp : '!' exp %prec UNARY
276 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
279 exp : '^' exp %prec UNARY
280 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
283 exp : exp INCREMENT %prec UNARY
284 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
287 exp : exp DECREMENT %prec UNARY
288 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
291 /* foo->bar is not in Go. May want as a gdb extension. Later. */
293 exp : exp '.' name_not_typename
294 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
295 write_exp_string (pstate, $3.stoken);
296 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
299 exp : exp '.' name_not_typename COMPLETE
300 { mark_struct_expression (pstate);
301 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
302 write_exp_string (pstate, $3.stoken);
303 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
306 exp : exp '.' COMPLETE
308 mark_struct_expression (pstate);
309 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
312 write_exp_string (pstate, s);
313 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
316 exp : exp '[' exp1 ']'
317 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
321 /* This is to save the value of arglist_len
322 being accumulated by an outer function call. */
323 { start_arglist (); }
324 arglist ')' %prec LEFT_ARROW
325 { write_exp_elt_opcode (pstate, OP_FUNCALL);
326 write_exp_elt_longcst (pstate,
327 (LONGEST) end_arglist ());
328 write_exp_elt_opcode (pstate, OP_FUNCALL); }
332 { start_arglist (); }
342 arglist : arglist ',' exp %prec ABOVE_COMMA
347 { $$ = end_arglist () - 1; }
350 exp : lcurly type rcurly exp %prec UNARY
351 { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
352 write_exp_elt_type (pstate, $2);
353 write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
356 exp : type '(' exp ')' %prec UNARY
357 { write_exp_elt_opcode (pstate, UNOP_CAST);
358 write_exp_elt_type (pstate, $1);
359 write_exp_elt_opcode (pstate, UNOP_CAST); }
366 /* Binary operators in order of decreasing precedence. */
369 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
373 { write_exp_elt_opcode (pstate, BINOP_MUL); }
377 { write_exp_elt_opcode (pstate, BINOP_DIV); }
381 { write_exp_elt_opcode (pstate, BINOP_REM); }
385 { write_exp_elt_opcode (pstate, BINOP_ADD); }
389 { write_exp_elt_opcode (pstate, BINOP_SUB); }
393 { write_exp_elt_opcode (pstate, BINOP_LSH); }
397 { write_exp_elt_opcode (pstate, BINOP_RSH); }
401 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
404 exp : exp NOTEQUAL exp
405 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
409 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
413 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
417 { write_exp_elt_opcode (pstate, BINOP_LESS); }
421 { write_exp_elt_opcode (pstate, BINOP_GTR); }
425 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
429 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
433 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
437 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
441 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
444 exp : exp '?' exp ':' exp %prec '?'
445 { write_exp_elt_opcode (pstate, TERNOP_COND); }
449 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
452 exp : exp ASSIGN_MODIFY exp
453 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
454 write_exp_elt_opcode (pstate, $2);
455 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
459 { write_exp_elt_opcode (pstate, OP_LONG);
460 write_exp_elt_type (pstate, $1.type);
461 write_exp_elt_longcst (pstate, (LONGEST)($1.val));
462 write_exp_elt_opcode (pstate, OP_LONG); }
467 struct stoken_vector vec;
470 write_exp_string_vector (pstate, $1.type, &vec);
476 parse_number (pstate, $1.stoken.ptr,
477 $1.stoken.length, 0, &val);
478 write_exp_elt_opcode (pstate, OP_LONG);
479 write_exp_elt_type (pstate, val.typed_val_int.type);
480 write_exp_elt_longcst (pstate, (LONGEST)
481 val.typed_val_int.val);
482 write_exp_elt_opcode (pstate, OP_LONG);
488 { write_exp_elt_opcode (pstate, OP_DOUBLE);
489 write_exp_elt_type (pstate, $1.type);
490 write_exp_elt_dblcst (pstate, $1.dval);
491 write_exp_elt_opcode (pstate, OP_DOUBLE); }
497 exp : DOLLAR_VARIABLE
499 write_dollar_variable (pstate, $1);
503 exp : SIZEOF_KEYWORD '(' type ')' %prec UNARY
505 /* TODO(dje): Go objects in structs. */
506 write_exp_elt_opcode (pstate, OP_LONG);
507 /* TODO(dje): What's the right type here? */
510 parse_type (pstate)->builtin_unsigned_int);
511 $3 = check_typedef ($3);
512 write_exp_elt_longcst (pstate,
513 (LONGEST) TYPE_LENGTH ($3));
514 write_exp_elt_opcode (pstate, OP_LONG);
518 exp : SIZEOF_KEYWORD '(' exp ')' %prec UNARY
520 /* TODO(dje): Go objects in structs. */
521 write_exp_elt_opcode (pstate, UNOP_SIZEOF);
527 /* We copy the string here, and not in the
528 lexer, to guarantee that we do not leak a
530 /* Note that we NUL-terminate here, but just
532 struct typed_stoken *vec = XNEW (struct typed_stoken);
537 vec->length = $1.length;
538 vec->ptr = (char *) malloc ($1.length + 1);
539 memcpy (vec->ptr, $1.ptr, $1.length + 1);
542 | string_exp '+' STRING
544 /* Note that we NUL-terminate here, but just
548 $$.tokens = XRESIZEVEC (struct typed_stoken,
551 p = (char *) malloc ($3.length + 1);
552 memcpy (p, $3.ptr, $3.length + 1);
554 $$.tokens[$$.len - 1].type = $3.type;
555 $$.tokens[$$.len - 1].length = $3.length;
556 $$.tokens[$$.len - 1].ptr = p;
560 exp : string_exp %prec ABOVE_COMMA
564 write_exp_string_vector (pstate, 0 /*always utf8*/,
566 for (i = 0; i < $1.len; ++i)
567 free ($1.tokens[i].ptr);
573 { write_exp_elt_opcode (pstate, OP_BOOL);
574 write_exp_elt_longcst (pstate, (LONGEST) $1);
575 write_exp_elt_opcode (pstate, OP_BOOL); }
579 { write_exp_elt_opcode (pstate, OP_BOOL);
580 write_exp_elt_longcst (pstate, (LONGEST) $1);
581 write_exp_elt_opcode (pstate, OP_BOOL); }
584 variable: name_not_typename ENTRY
585 { struct symbol *sym = $1.sym.symbol;
588 || !SYMBOL_IS_ARGUMENT (sym)
589 || !symbol_read_needs_frame (sym))
590 error (_("@entry can be used only for function "
591 "parameters, not for \"%s\""),
592 copy_name ($1.stoken));
594 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
595 write_exp_elt_sym (pstate, sym);
596 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
600 variable: name_not_typename
601 { struct block_symbol sym = $1.sym;
605 if (symbol_read_needs_frame (sym.symbol))
607 if (innermost_block == 0
608 || contained_in (sym.block,
610 innermost_block = sym.block;
613 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
614 write_exp_elt_block (pstate, sym.block);
615 write_exp_elt_sym (pstate, sym.symbol);
616 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
618 else if ($1.is_a_field_of_this)
620 /* TODO(dje): Can we get here?
621 E.g., via a mix of c++ and go? */
622 gdb_assert_not_reached ("go with `this' field");
626 struct bound_minimal_symbol msymbol;
627 char *arg = copy_name ($1.stoken);
630 lookup_bound_minimal_symbol (arg);
631 if (msymbol.minsym != NULL)
632 write_exp_msymbol (pstate, msymbol);
633 else if (!have_full_symbols ()
634 && !have_partial_symbols ())
635 error (_("No symbol table is loaded. "
636 "Use the \"file\" command."));
638 error (_("No symbol \"%s\" in current context."),
639 copy_name ($1.stoken));
645 method_exp: PACKAGENAME '.' name '.' name
651 type /* Implements (approximately): [*] type-specifier */
653 { $$ = lookup_pointer_type ($2); }
657 | STRUCT_KEYWORD name
658 { $$ = lookup_struct (copy_name ($2),
659 expression_context_block); }
662 { $$ = builtin_go_type (parse_gdbarch (pstate))
667 name : NAME { $$ = $1.stoken; }
668 | TYPENAME { $$ = $1.stoken; }
669 | NAME_OR_INT { $$ = $1.stoken; }
675 /* These would be useful if name_not_typename was useful, but it is just
676 a fake for "variable", so these cause reduce/reduce conflicts because
677 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
678 =exp) or just an exp. If name_not_typename was ever used in an lvalue
679 context where only a name could occur, this might be useful.
686 /* Wrapper on parse_c_float to get the type right for Go. */
689 parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
690 DOUBLEST *d, struct type **t)
692 int result = parse_c_float (gdbarch, p, len, d, t);
693 const struct builtin_type *builtin_types = builtin_type (gdbarch);
694 const struct builtin_go_type *builtin_go_types = builtin_go_type (gdbarch);
696 if (*t == builtin_types->builtin_float)
697 *t = builtin_go_types->builtin_float32;
698 else if (*t == builtin_types->builtin_double)
699 *t = builtin_go_types->builtin_float64;
704 /* Take care of parsing a number (anything that starts with a digit).
705 Set yylval and return the token type; update lexptr.
706 LEN is the number of characters in it. */
708 /* FIXME: Needs some error checking for the float case. */
709 /* FIXME(dje): IWBN to use c-exp.y's parse_number if we could.
710 That will require moving the guts into a function that we both call
711 as our YYSTYPE is different than c-exp.y's */
714 parse_number (struct parser_state *par_state,
715 const char *p, int len, int parsed_float, YYSTYPE *putithere)
717 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
718 here, and we do kind of silly things like cast to unsigned. */
725 int base = input_radix;
728 /* Number of "L" suffixes encountered. */
731 /* We have found a "L" or "U" suffix. */
732 int found_suffix = 0;
735 struct type *signed_type;
736 struct type *unsigned_type;
740 if (! parse_go_float (parse_gdbarch (par_state), p, len,
741 &putithere->typed_val_float.dval,
742 &putithere->typed_val_float.type))
747 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
791 if (c >= 'A' && c <= 'Z')
793 if (c != 'l' && c != 'u')
795 if (c >= '0' && c <= '9')
803 if (base > 10 && c >= 'a' && c <= 'f')
807 n += i = c - 'a' + 10;
820 return ERROR; /* Char not a digit */
823 return ERROR; /* Invalid digit in this base. */
825 /* Portably test for overflow (only works for nonzero values, so make
826 a second check for zero). FIXME: Can't we just make n and prevn
827 unsigned and avoid this? */
828 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
829 unsigned_p = 1; /* Try something unsigned. */
831 /* Portably test for unsigned overflow.
832 FIXME: This check is wrong; for example it doesn't find overflow
833 on 0x123456789 when LONGEST is 32 bits. */
834 if (c != 'l' && c != 'u' && n != 0)
836 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
837 error (_("Numeric constant too large."));
842 /* An integer constant is an int, a long, or a long long. An L
843 suffix forces it to be long; an LL suffix forces it to be long
844 long. If not forced to a larger size, it gets the first type of
845 the above that it fits in. To figure out whether it fits, we
846 shift it right and see whether anything remains. Note that we
847 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
848 operation, because many compilers will warn about such a shift
849 (which always produces a zero result). Sometimes gdbarch_int_bit
850 or gdbarch_long_bit will be that big, sometimes not. To deal with
851 the case where it is we just always shift the value more than
852 once, with fewer bits each time. */
854 un = (ULONGEST)n >> 2;
856 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
859 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
861 /* A large decimal (not hex or octal) constant (between INT_MAX
862 and UINT_MAX) is a long or unsigned long, according to ANSI,
863 never an unsigned int, but this code treats it as unsigned
864 int. This probably should be fixed. GCC gives a warning on
867 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
868 signed_type = parse_type (par_state)->builtin_int;
871 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
874 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
875 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
876 signed_type = parse_type (par_state)->builtin_long;
881 if (sizeof (ULONGEST) * HOST_CHAR_BIT
882 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
883 /* A long long does not fit in a LONGEST. */
884 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
886 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
887 high_bit = (ULONGEST) 1 << shift;
888 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
889 signed_type = parse_type (par_state)->builtin_long_long;
892 putithere->typed_val_int.val = n;
894 /* If the high bit of the worked out type is set then this number
895 has to be unsigned. */
897 if (unsigned_p || (n & high_bit))
899 putithere->typed_val_int.type = unsigned_type;
903 putithere->typed_val_int.type = signed_type;
909 /* Temporary obstack used for holding strings. */
910 static struct obstack tempbuf;
911 static int tempbuf_init;
913 /* Parse a string or character literal from TOKPTR. The string or
914 character may be wide or unicode. *OUTPTR is set to just after the
915 end of the literal in the input string. The resulting token is
916 stored in VALUE. This returns a token value, either STRING or
917 CHAR, depending on what was parsed. *HOST_CHARS is set to the
918 number of host characters in the literal. */
921 parse_string_or_char (const char *tokptr, const char **outptr,
922 struct typed_stoken *value, int *host_chars)
926 /* Build the gdb internal form of the input string in tempbuf. Note
927 that the buffer is null byte terminated *only* for the
928 convenience of debugging gdb itself and printing the buffer
929 contents when the buffer contains no embedded nulls. Gdb does
930 not depend upon the buffer being null byte terminated, it uses
931 the length string instead. This allows gdb to handle C strings
932 (as well as strings in other languages) with embedded null
938 obstack_free (&tempbuf, NULL);
939 obstack_init (&tempbuf);
941 /* Skip the quote. */
953 *host_chars += c_parse_escape (&tokptr, &tempbuf);
959 obstack_1grow (&tempbuf, c);
961 /* FIXME: this does the wrong thing with multi-byte host
962 characters. We could use mbrlen here, but that would
963 make "set host-charset" a bit less useful. */
968 if (*tokptr != quote)
971 error (_("Unterminated string in expression."));
973 error (_("Unmatched single quote."));
977 value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/
978 value->ptr = (char *) obstack_base (&tempbuf);
979 value->length = obstack_object_size (&tempbuf);
983 return quote == '\'' ? CHAR : STRING;
990 enum exp_opcode opcode;
993 static const struct token tokentab3[] =
995 {">>=", ASSIGN_MODIFY, BINOP_RSH},
996 {"<<=", ASSIGN_MODIFY, BINOP_LSH},
997 /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */
998 {"...", DOTDOTDOT, OP_NULL},
1001 static const struct token tokentab2[] =
1003 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1004 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1005 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1006 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1007 {"%=", ASSIGN_MODIFY, BINOP_REM},
1008 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1009 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1010 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1011 {"++", INCREMENT, BINOP_END},
1012 {"--", DECREMENT, BINOP_END},
1013 /*{"->", RIGHT_ARROW, BINOP_END}, Doesn't exist in Go. */
1014 {"<-", LEFT_ARROW, BINOP_END},
1015 {"&&", ANDAND, BINOP_END},
1016 {"||", OROR, BINOP_END},
1017 {"<<", LSH, BINOP_END},
1018 {">>", RSH, BINOP_END},
1019 {"==", EQUAL, BINOP_END},
1020 {"!=", NOTEQUAL, BINOP_END},
1021 {"<=", LEQ, BINOP_END},
1022 {">=", GEQ, BINOP_END},
1023 /*{"&^", ANDNOT, BINOP_END}, TODO */
1026 /* Identifier-like tokens. */
1027 static const struct token ident_tokens[] =
1029 {"true", TRUE_KEYWORD, OP_NULL},
1030 {"false", FALSE_KEYWORD, OP_NULL},
1031 {"nil", NIL_KEYWORD, OP_NULL},
1032 {"const", CONST_KEYWORD, OP_NULL},
1033 {"struct", STRUCT_KEYWORD, OP_NULL},
1034 {"type", TYPE_KEYWORD, OP_NULL},
1035 {"interface", INTERFACE_KEYWORD, OP_NULL},
1036 {"chan", CHAN_KEYWORD, OP_NULL},
1037 {"byte", BYTE_KEYWORD, OP_NULL}, /* An alias of uint8. */
1038 {"len", LEN_KEYWORD, OP_NULL},
1039 {"cap", CAP_KEYWORD, OP_NULL},
1040 {"new", NEW_KEYWORD, OP_NULL},
1041 {"iota", IOTA_KEYWORD, OP_NULL},
1044 /* This is set if a NAME token appeared at the very end of the input
1045 string, with no whitespace separating the name from the EOF. This
1046 is used only when parsing to do field name completion. */
1047 static int saw_name_at_eof;
1049 /* This is set if the previously-returned token was a structure
1050 operator -- either '.' or ARROW. This is used only when parsing to
1051 do field name completion. */
1052 static int last_was_structop;
1054 /* Read one token, getting characters through lexptr. */
1057 lex_one_token (struct parser_state *par_state)
1062 const char *tokstart;
1063 int saw_structop = last_was_structop;
1066 last_was_structop = 0;
1070 prev_lexptr = lexptr;
1073 /* See if it is a special token of length 3. */
1074 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1075 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
1078 yylval.opcode = tokentab3[i].opcode;
1079 return tokentab3[i].token;
1082 /* See if it is a special token of length 2. */
1083 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1084 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
1087 yylval.opcode = tokentab2[i].opcode;
1088 /* NOTE: -> doesn't exist in Go, so we don't need to watch for
1089 setting last_was_structop here. */
1090 return tokentab2[i].token;
1093 switch (c = *tokstart)
1096 if (saw_name_at_eof)
1098 saw_name_at_eof = 0;
1101 else if (saw_structop)
1120 if (paren_depth == 0)
1127 if (comma_terminates
1128 && paren_depth == 0)
1134 /* Might be a floating point number. */
1135 if (lexptr[1] < '0' || lexptr[1] > '9')
1137 if (parse_completion)
1138 last_was_structop = 1;
1139 goto symbol; /* Nope, must be a symbol. */
1141 /* FALL THRU into number case. */
1154 /* It's a number. */
1155 int got_dot = 0, got_e = 0, toktype;
1156 const char *p = tokstart;
1157 int hex = input_radix > 10;
1159 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1167 /* This test includes !hex because 'e' is a valid hex digit
1168 and thus does not indicate a floating point number when
1169 the radix is hex. */
1170 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1171 got_dot = got_e = 1;
1172 /* This test does not include !hex, because a '.' always indicates
1173 a decimal floating point number regardless of the radix. */
1174 else if (!got_dot && *p == '.')
1176 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1177 && (*p == '-' || *p == '+'))
1178 /* This is the sign of the exponent, not the end of the
1181 /* We will take any letters or digits. parse_number will
1182 complain if past the radix, or if L or U are not final. */
1183 else if ((*p < '0' || *p > '9')
1184 && ((*p < 'a' || *p > 'z')
1185 && (*p < 'A' || *p > 'Z')))
1188 toktype = parse_number (par_state, tokstart, p - tokstart,
1189 got_dot|got_e, &yylval);
1190 if (toktype == ERROR)
1192 char *err_copy = (char *) alloca (p - tokstart + 1);
1194 memcpy (err_copy, tokstart, p - tokstart);
1195 err_copy[p - tokstart] = 0;
1196 error (_("Invalid number \"%s\"."), err_copy);
1204 const char *p = &tokstart[1];
1205 size_t len = strlen ("entry");
1207 while (isspace (*p))
1209 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
1243 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
1248 error (_("Empty character constant."));
1249 else if (host_len > 2 && c == '\'')
1252 namelen = lexptr - tokstart - 1;
1255 else if (host_len > 1)
1256 error (_("Invalid character constant."));
1262 if (!(c == '_' || c == '$'
1263 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1264 /* We must have come across a bad character (e.g. ';'). */
1265 error (_("Invalid character '%c' in expression."), c);
1267 /* It's a name. See how long it is. */
1269 for (c = tokstart[namelen];
1270 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1271 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
1273 c = tokstart[++namelen];
1276 /* The token "if" terminates the expression and is NOT removed from
1277 the input stream. It doesn't count if it appears in the
1278 expansion of a macro. */
1280 && tokstart[0] == 'i'
1281 && tokstart[1] == 'f')
1286 /* For the same reason (breakpoint conditions), "thread N"
1287 terminates the expression. "thread" could be an identifier, but
1288 an identifier is never followed by a number without intervening
1290 Handle abbreviations of these, similarly to
1291 breakpoint.c:find_condition_and_thread.
1292 TODO: Watch for "goroutine" here? */
1294 && strncmp (tokstart, "thread", namelen) == 0
1295 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
1297 const char *p = tokstart + namelen + 1;
1299 while (*p == ' ' || *p == '\t')
1301 if (*p >= '0' && *p <= '9')
1309 yylval.sval.ptr = tokstart;
1310 yylval.sval.length = namelen;
1312 /* Catch specific keywords. */
1313 copy = copy_name (yylval.sval);
1314 for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++)
1315 if (strcmp (copy, ident_tokens[i].oper) == 0)
1317 /* It is ok to always set this, even though we don't always
1318 strictly need to. */
1319 yylval.opcode = ident_tokens[i].opcode;
1320 return ident_tokens[i].token;
1323 if (*tokstart == '$')
1324 return DOLLAR_VARIABLE;
1326 if (parse_completion && *lexptr == '\0')
1327 saw_name_at_eof = 1;
1331 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
1338 DEF_VEC_O (token_and_value);
1340 /* A FIFO of tokens that have been read but not yet returned to the
1342 static VEC (token_and_value) *token_fifo;
1344 /* Non-zero if the lexer should return tokens from the FIFO. */
1347 /* Temporary storage for yylex; this holds symbol names as they are
1349 static struct obstack name_obstack;
1351 /* Build "package.name" in name_obstack.
1352 For convenience of the caller, the name is NUL-terminated,
1353 but the NUL is not included in the recorded length. */
1355 static struct stoken
1356 build_packaged_name (const char *package, int package_len,
1357 const char *name, int name_len)
1359 struct stoken result;
1361 obstack_free (&name_obstack, obstack_base (&name_obstack));
1362 obstack_grow (&name_obstack, package, package_len);
1363 obstack_grow_str (&name_obstack, ".");
1364 obstack_grow (&name_obstack, name, name_len);
1365 obstack_grow (&name_obstack, "", 1);
1366 result.ptr = (char *) obstack_base (&name_obstack);
1367 result.length = obstack_object_size (&name_obstack) - 1;
1372 /* Return non-zero if NAME is a package name.
1373 BLOCK is the scope in which to interpret NAME; this can be NULL
1374 to mean the global scope. */
1377 package_name_p (const char *name, const struct block *block)
1380 struct field_of_this_result is_a_field_of_this;
1382 sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this).symbol;
1385 && SYMBOL_CLASS (sym) == LOC_TYPEDEF
1386 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE)
1392 /* Classify a (potential) function in the "unsafe" package.
1393 We fold these into "keywords" to keep things simple, at least until
1394 something more complex is warranted. */
1397 classify_unsafe_function (struct stoken function_name)
1399 char *copy = copy_name (function_name);
1401 if (strcmp (copy, "Sizeof") == 0)
1403 yylval.sval = function_name;
1404 return SIZEOF_KEYWORD;
1407 error (_("Unknown function in `unsafe' package: %s"), copy);
1410 /* Classify token(s) "name1.name2" where name1 is known to be a package.
1411 The contents of the token are in `yylval'.
1412 Updates yylval and returns the new token type.
1414 The result is one of NAME, NAME_OR_INT, or TYPENAME. */
1417 classify_packaged_name (const struct block *block)
1420 struct block_symbol sym;
1421 struct field_of_this_result is_a_field_of_this;
1423 copy = copy_name (yylval.sval);
1425 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1429 yylval.ssym.sym = sym;
1430 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1436 /* Classify a NAME token.
1437 The contents of the token are in `yylval'.
1438 Updates yylval and returns the new token type.
1439 BLOCK is the block in which lookups start; this can be NULL
1440 to mean the global scope.
1442 The result is one of NAME, NAME_OR_INT, or TYPENAME. */
1445 classify_name (struct parser_state *par_state, const struct block *block)
1448 struct block_symbol sym;
1450 struct field_of_this_result is_a_field_of_this;
1452 copy = copy_name (yylval.sval);
1454 /* Try primitive types first so they win over bad/weird debug info. */
1455 type = language_lookup_primitive_type (parse_language (par_state),
1456 parse_gdbarch (par_state),
1460 /* NOTE: We take advantage of the fact that yylval coming in was a
1461 NAME, and that struct ttype is a compatible extension of struct
1462 stoken, so yylval.tsym.stoken is already filled in. */
1463 yylval.tsym.type = type;
1467 /* TODO: What about other types? */
1469 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1473 yylval.ssym.sym = sym;
1474 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1478 /* If we didn't find a symbol, look again in the current package.
1479 This is to, e.g., make "p global_var" work without having to specify
1480 the package name. We intentionally only looks for objects in the
1484 char *current_package_name = go_block_package_name (block);
1486 if (current_package_name != NULL)
1488 struct stoken sval =
1489 build_packaged_name (current_package_name,
1490 strlen (current_package_name),
1491 copy, strlen (copy));
1493 xfree (current_package_name);
1494 sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN,
1495 &is_a_field_of_this);
1498 yylval.ssym.stoken = sval;
1499 yylval.ssym.sym = sym;
1500 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1506 /* Input names that aren't symbols but ARE valid hex numbers, when
1507 the input radix permits them, can be names or numbers depending
1508 on the parse. Note we support radixes > 16 here. */
1509 if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
1510 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10))
1512 YYSTYPE newlval; /* Its value is ignored. */
1513 int hextype = parse_number (par_state, copy, yylval.sval.length,
1517 yylval.ssym.sym.symbol = NULL;
1518 yylval.ssym.sym.block = NULL;
1519 yylval.ssym.is_a_field_of_this = 0;
1524 yylval.ssym.sym.symbol = NULL;
1525 yylval.ssym.sym.block = NULL;
1526 yylval.ssym.is_a_field_of_this = 0;
1530 /* This is taken from c-exp.y mostly to get something working.
1531 The basic structure has been kept because we may yet need some of it. */
1536 token_and_value current, next;
1538 if (popping && !VEC_empty (token_and_value, token_fifo))
1540 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
1541 VEC_ordered_remove (token_and_value, token_fifo, 0);
1543 /* There's no need to fall through to handle package.name
1544 as that can never happen here. In theory. */
1549 current.token = lex_one_token (pstate);
1551 /* TODO: Need a way to force specifying name1 as a package.
1554 if (current.token != NAME)
1555 return current.token;
1557 /* See if we have "name1 . name2". */
1559 current.value = yylval;
1560 next.token = lex_one_token (pstate);
1561 next.value = yylval;
1563 if (next.token == '.')
1565 token_and_value name2;
1567 name2.token = lex_one_token (pstate);
1568 name2.value = yylval;
1570 if (name2.token == NAME)
1572 /* Ok, we have "name1 . name2". */
1575 copy = copy_name (current.value.sval);
1577 if (strcmp (copy, "unsafe") == 0)
1580 return classify_unsafe_function (name2.value.sval);
1583 if (package_name_p (copy, expression_context_block))
1586 yylval.sval = build_packaged_name (current.value.sval.ptr,
1587 current.value.sval.length,
1588 name2.value.sval.ptr,
1589 name2.value.sval.length);
1590 return classify_packaged_name (expression_context_block);
1594 VEC_safe_push (token_and_value, token_fifo, &next);
1595 VEC_safe_push (token_and_value, token_fifo, &name2);
1599 VEC_safe_push (token_and_value, token_fifo, &next);
1602 /* If we arrive here we don't have a package-qualified name. */
1605 yylval = current.value;
1606 return classify_name (pstate, expression_context_block);
1610 go_parse (struct parser_state *par_state)
1613 struct cleanup *back_to;
1615 /* Setting up the parser state. */
1616 gdb_assert (par_state != NULL);
1619 back_to = make_cleanup (null_cleanup, NULL);
1621 make_cleanup_restore_integer (&yydebug);
1622 make_cleanup_clear_parser_state (&pstate);
1623 yydebug = parser_debug;
1625 /* Initialize some state used by the lexer. */
1626 last_was_structop = 0;
1627 saw_name_at_eof = 0;
1629 VEC_free (token_and_value, token_fifo);
1631 obstack_init (&name_obstack);
1632 make_cleanup_obstack_free (&name_obstack);
1634 result = yyparse ();
1635 do_cleanups (back_to);
1643 lexptr = prev_lexptr;
1645 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);