1 /* YACC parser for Go expressions, for GDB.
3 Copyright (C) 2012-2017 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,
72 #define GDB_YY_REMAP_PREFIX go_
75 /* The state of the parser, used internally when we are parsing the
78 static struct parser_state *pstate = NULL;
82 static int yylex (void);
84 void yyerror (const char *);
88 /* Although the yacc "value" of an expression is not used,
89 since the result is stored in the structure being created,
90 other node types do have values. */
104 struct symtoken ssym;
106 struct typed_stoken tsval;
109 enum exp_opcode opcode;
110 struct internalvar *ivar;
111 struct stoken_vector svec;
115 /* YYSTYPE gets defined by %union. */
116 static int parse_number (struct parser_state *,
117 const char *, int, int, YYSTYPE *);
118 static int parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
119 DOUBLEST *d, struct type **t);
122 %type <voidval> exp exp1 type_exp start variable lcurly
126 %token <typed_val_int> INT
127 %token <typed_val_float> FLOAT
129 /* Both NAME and TYPENAME tokens represent symbols in the input,
130 and both convey their data as strings.
131 But a TYPENAME is a string that happens to be defined as a type
132 or builtin type name (such as int or char)
133 and a NAME is any other symbol.
134 Contexts where this distinction is not important can use the
135 nonterminal "name", which matches either NAME or TYPENAME. */
137 %token <tsval> RAW_STRING
138 %token <tsval> STRING
141 %token <tsym> TYPENAME /* Not TYPE_NAME cus already taken. */
142 %token <voidval> COMPLETE
143 /*%type <sval> name*/
144 %type <svec> string_exp
145 %type <ssym> name_not_typename
147 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
148 but which would parse as a valid number in the current input radix.
149 E.g. "c" when input_radix==16. Depending on the parse, it will be
150 turned into a name or into a number. */
151 %token <ssym> NAME_OR_INT
153 %token <lval> TRUE_KEYWORD FALSE_KEYWORD
154 %token STRUCT_KEYWORD INTERFACE_KEYWORD TYPE_KEYWORD CHAN_KEYWORD
155 %token SIZEOF_KEYWORD
156 %token LEN_KEYWORD CAP_KEYWORD
158 %token IOTA_KEYWORD NIL_KEYWORD
164 /* Special type cases. */
165 %token BYTE_KEYWORD /* An alias of uint8. */
167 %token <sval> DOLLAR_VARIABLE
169 %token <opcode> ASSIGN_MODIFY
173 %right '=' ASSIGN_MODIFY
182 %left '<' '>' LEQ GEQ
187 %right UNARY INCREMENT DECREMENT
188 %right LEFT_ARROW '.' '[' '('
198 { write_exp_elt_opcode (pstate, OP_TYPE);
199 write_exp_elt_type (pstate, $1);
200 write_exp_elt_opcode (pstate, OP_TYPE); }
203 /* Expressions, including the comma operator. */
206 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
209 /* Expressions, not including the comma operator. */
210 exp : '*' exp %prec UNARY
211 { write_exp_elt_opcode (pstate, UNOP_IND); }
214 exp : '&' exp %prec UNARY
215 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
218 exp : '-' exp %prec UNARY
219 { write_exp_elt_opcode (pstate, UNOP_NEG); }
222 exp : '+' exp %prec UNARY
223 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
226 exp : '!' exp %prec UNARY
227 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
230 exp : '^' exp %prec UNARY
231 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
234 exp : exp INCREMENT %prec UNARY
235 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
238 exp : exp DECREMENT %prec UNARY
239 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
242 /* foo->bar is not in Go. May want as a gdb extension. Later. */
244 exp : exp '.' name_not_typename
245 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
246 write_exp_string (pstate, $3.stoken);
247 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
250 exp : exp '.' name_not_typename COMPLETE
251 { mark_struct_expression (pstate);
252 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
253 write_exp_string (pstate, $3.stoken);
254 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
257 exp : exp '.' COMPLETE
259 mark_struct_expression (pstate);
260 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
263 write_exp_string (pstate, s);
264 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
267 exp : exp '[' exp1 ']'
268 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
272 /* This is to save the value of arglist_len
273 being accumulated by an outer function call. */
274 { start_arglist (); }
275 arglist ')' %prec LEFT_ARROW
276 { write_exp_elt_opcode (pstate, OP_FUNCALL);
277 write_exp_elt_longcst (pstate,
278 (LONGEST) end_arglist ());
279 write_exp_elt_opcode (pstate, OP_FUNCALL); }
283 { start_arglist (); }
293 arglist : arglist ',' exp %prec ABOVE_COMMA
298 { $$ = end_arglist () - 1; }
301 exp : lcurly type rcurly exp %prec UNARY
302 { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
303 write_exp_elt_type (pstate, $2);
304 write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
307 exp : type '(' exp ')' %prec UNARY
308 { write_exp_elt_opcode (pstate, UNOP_CAST);
309 write_exp_elt_type (pstate, $1);
310 write_exp_elt_opcode (pstate, UNOP_CAST); }
317 /* Binary operators in order of decreasing precedence. */
320 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
324 { write_exp_elt_opcode (pstate, BINOP_MUL); }
328 { write_exp_elt_opcode (pstate, BINOP_DIV); }
332 { write_exp_elt_opcode (pstate, BINOP_REM); }
336 { write_exp_elt_opcode (pstate, BINOP_ADD); }
340 { write_exp_elt_opcode (pstate, BINOP_SUB); }
344 { write_exp_elt_opcode (pstate, BINOP_LSH); }
348 { write_exp_elt_opcode (pstate, BINOP_RSH); }
352 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
355 exp : exp NOTEQUAL exp
356 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
360 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
364 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
368 { write_exp_elt_opcode (pstate, BINOP_LESS); }
372 { write_exp_elt_opcode (pstate, BINOP_GTR); }
376 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
380 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
384 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
388 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
392 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
395 exp : exp '?' exp ':' exp %prec '?'
396 { write_exp_elt_opcode (pstate, TERNOP_COND); }
400 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
403 exp : exp ASSIGN_MODIFY exp
404 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
405 write_exp_elt_opcode (pstate, $2);
406 write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
410 { write_exp_elt_opcode (pstate, OP_LONG);
411 write_exp_elt_type (pstate, $1.type);
412 write_exp_elt_longcst (pstate, (LONGEST)($1.val));
413 write_exp_elt_opcode (pstate, OP_LONG); }
418 struct stoken_vector vec;
421 write_exp_string_vector (pstate, $1.type, &vec);
427 parse_number (pstate, $1.stoken.ptr,
428 $1.stoken.length, 0, &val);
429 write_exp_elt_opcode (pstate, OP_LONG);
430 write_exp_elt_type (pstate, val.typed_val_int.type);
431 write_exp_elt_longcst (pstate, (LONGEST)
432 val.typed_val_int.val);
433 write_exp_elt_opcode (pstate, OP_LONG);
439 { write_exp_elt_opcode (pstate, OP_DOUBLE);
440 write_exp_elt_type (pstate, $1.type);
441 write_exp_elt_dblcst (pstate, $1.dval);
442 write_exp_elt_opcode (pstate, OP_DOUBLE); }
448 exp : DOLLAR_VARIABLE
450 write_dollar_variable (pstate, $1);
454 exp : SIZEOF_KEYWORD '(' type ')' %prec UNARY
456 /* TODO(dje): Go objects in structs. */
457 write_exp_elt_opcode (pstate, OP_LONG);
458 /* TODO(dje): What's the right type here? */
461 parse_type (pstate)->builtin_unsigned_int);
462 $3 = check_typedef ($3);
463 write_exp_elt_longcst (pstate,
464 (LONGEST) TYPE_LENGTH ($3));
465 write_exp_elt_opcode (pstate, OP_LONG);
469 exp : SIZEOF_KEYWORD '(' exp ')' %prec UNARY
471 /* TODO(dje): Go objects in structs. */
472 write_exp_elt_opcode (pstate, UNOP_SIZEOF);
478 /* We copy the string here, and not in the
479 lexer, to guarantee that we do not leak a
481 /* Note that we NUL-terminate here, but just
483 struct typed_stoken *vec = XNEW (struct typed_stoken);
488 vec->length = $1.length;
489 vec->ptr = (char *) malloc ($1.length + 1);
490 memcpy (vec->ptr, $1.ptr, $1.length + 1);
493 | string_exp '+' STRING
495 /* Note that we NUL-terminate here, but just
499 $$.tokens = XRESIZEVEC (struct typed_stoken,
502 p = (char *) malloc ($3.length + 1);
503 memcpy (p, $3.ptr, $3.length + 1);
505 $$.tokens[$$.len - 1].type = $3.type;
506 $$.tokens[$$.len - 1].length = $3.length;
507 $$.tokens[$$.len - 1].ptr = p;
511 exp : string_exp %prec ABOVE_COMMA
515 write_exp_string_vector (pstate, 0 /*always utf8*/,
517 for (i = 0; i < $1.len; ++i)
518 free ($1.tokens[i].ptr);
524 { write_exp_elt_opcode (pstate, OP_BOOL);
525 write_exp_elt_longcst (pstate, (LONGEST) $1);
526 write_exp_elt_opcode (pstate, OP_BOOL); }
530 { write_exp_elt_opcode (pstate, OP_BOOL);
531 write_exp_elt_longcst (pstate, (LONGEST) $1);
532 write_exp_elt_opcode (pstate, OP_BOOL); }
535 variable: name_not_typename ENTRY
536 { struct symbol *sym = $1.sym.symbol;
539 || !SYMBOL_IS_ARGUMENT (sym)
540 || !symbol_read_needs_frame (sym))
541 error (_("@entry can be used only for function "
542 "parameters, not for \"%s\""),
543 copy_name ($1.stoken));
545 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
546 write_exp_elt_sym (pstate, sym);
547 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
551 variable: name_not_typename
552 { struct block_symbol sym = $1.sym;
556 if (symbol_read_needs_frame (sym.symbol))
558 if (innermost_block == 0
559 || contained_in (sym.block,
561 innermost_block = sym.block;
564 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
565 write_exp_elt_block (pstate, sym.block);
566 write_exp_elt_sym (pstate, sym.symbol);
567 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
569 else if ($1.is_a_field_of_this)
571 /* TODO(dje): Can we get here?
572 E.g., via a mix of c++ and go? */
573 gdb_assert_not_reached ("go with `this' field");
577 struct bound_minimal_symbol msymbol;
578 char *arg = copy_name ($1.stoken);
581 lookup_bound_minimal_symbol (arg);
582 if (msymbol.minsym != NULL)
583 write_exp_msymbol (pstate, msymbol);
584 else if (!have_full_symbols ()
585 && !have_partial_symbols ())
586 error (_("No symbol table is loaded. "
587 "Use the \"file\" command."));
589 error (_("No symbol \"%s\" in current context."),
590 copy_name ($1.stoken));
596 method_exp: PACKAGENAME '.' name '.' name
602 type /* Implements (approximately): [*] type-specifier */
604 { $$ = lookup_pointer_type ($2); }
608 | STRUCT_KEYWORD name
609 { $$ = lookup_struct (copy_name ($2),
610 expression_context_block); }
613 { $$ = builtin_go_type (parse_gdbarch (pstate))
618 name : NAME { $$ = $1.stoken; }
619 | TYPENAME { $$ = $1.stoken; }
620 | NAME_OR_INT { $$ = $1.stoken; }
626 /* These would be useful if name_not_typename was useful, but it is just
627 a fake for "variable", so these cause reduce/reduce conflicts because
628 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
629 =exp) or just an exp. If name_not_typename was ever used in an lvalue
630 context where only a name could occur, this might be useful.
637 /* Wrapper on parse_c_float to get the type right for Go. */
640 parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
641 DOUBLEST *d, struct type **t)
643 int result = parse_c_float (gdbarch, p, len, d, t);
644 const struct builtin_type *builtin_types = builtin_type (gdbarch);
645 const struct builtin_go_type *builtin_go_types = builtin_go_type (gdbarch);
647 if (*t == builtin_types->builtin_float)
648 *t = builtin_go_types->builtin_float32;
649 else if (*t == builtin_types->builtin_double)
650 *t = builtin_go_types->builtin_float64;
655 /* Take care of parsing a number (anything that starts with a digit).
656 Set yylval and return the token type; update lexptr.
657 LEN is the number of characters in it. */
659 /* FIXME: Needs some error checking for the float case. */
660 /* FIXME(dje): IWBN to use c-exp.y's parse_number if we could.
661 That will require moving the guts into a function that we both call
662 as our YYSTYPE is different than c-exp.y's */
665 parse_number (struct parser_state *par_state,
666 const char *p, int len, int parsed_float, YYSTYPE *putithere)
668 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
669 here, and we do kind of silly things like cast to unsigned. */
676 int base = input_radix;
679 /* Number of "L" suffixes encountered. */
682 /* We have found a "L" or "U" suffix. */
683 int found_suffix = 0;
686 struct type *signed_type;
687 struct type *unsigned_type;
691 if (! parse_go_float (parse_gdbarch (par_state), p, len,
692 &putithere->typed_val_float.dval,
693 &putithere->typed_val_float.type))
698 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
742 if (c >= 'A' && c <= 'Z')
744 if (c != 'l' && c != 'u')
746 if (c >= '0' && c <= '9')
754 if (base > 10 && c >= 'a' && c <= 'f')
758 n += i = c - 'a' + 10;
771 return ERROR; /* Char not a digit */
774 return ERROR; /* Invalid digit in this base. */
776 /* Portably test for overflow (only works for nonzero values, so make
777 a second check for zero). FIXME: Can't we just make n and prevn
778 unsigned and avoid this? */
779 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
780 unsigned_p = 1; /* Try something unsigned. */
782 /* Portably test for unsigned overflow.
783 FIXME: This check is wrong; for example it doesn't find overflow
784 on 0x123456789 when LONGEST is 32 bits. */
785 if (c != 'l' && c != 'u' && n != 0)
787 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
788 error (_("Numeric constant too large."));
793 /* An integer constant is an int, a long, or a long long. An L
794 suffix forces it to be long; an LL suffix forces it to be long
795 long. If not forced to a larger size, it gets the first type of
796 the above that it fits in. To figure out whether it fits, we
797 shift it right and see whether anything remains. Note that we
798 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
799 operation, because many compilers will warn about such a shift
800 (which always produces a zero result). Sometimes gdbarch_int_bit
801 or gdbarch_long_bit will be that big, sometimes not. To deal with
802 the case where it is we just always shift the value more than
803 once, with fewer bits each time. */
805 un = (ULONGEST)n >> 2;
807 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
810 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
812 /* A large decimal (not hex or octal) constant (between INT_MAX
813 and UINT_MAX) is a long or unsigned long, according to ANSI,
814 never an unsigned int, but this code treats it as unsigned
815 int. This probably should be fixed. GCC gives a warning on
818 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
819 signed_type = parse_type (par_state)->builtin_int;
822 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
825 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
826 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
827 signed_type = parse_type (par_state)->builtin_long;
832 if (sizeof (ULONGEST) * HOST_CHAR_BIT
833 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
834 /* A long long does not fit in a LONGEST. */
835 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
837 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
838 high_bit = (ULONGEST) 1 << shift;
839 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
840 signed_type = parse_type (par_state)->builtin_long_long;
843 putithere->typed_val_int.val = n;
845 /* If the high bit of the worked out type is set then this number
846 has to be unsigned. */
848 if (unsigned_p || (n & high_bit))
850 putithere->typed_val_int.type = unsigned_type;
854 putithere->typed_val_int.type = signed_type;
860 /* Temporary obstack used for holding strings. */
861 static struct obstack tempbuf;
862 static int tempbuf_init;
864 /* Parse a string or character literal from TOKPTR. The string or
865 character may be wide or unicode. *OUTPTR is set to just after the
866 end of the literal in the input string. The resulting token is
867 stored in VALUE. This returns a token value, either STRING or
868 CHAR, depending on what was parsed. *HOST_CHARS is set to the
869 number of host characters in the literal. */
872 parse_string_or_char (const char *tokptr, const char **outptr,
873 struct typed_stoken *value, int *host_chars)
877 /* Build the gdb internal form of the input string in tempbuf. Note
878 that the buffer is null byte terminated *only* for the
879 convenience of debugging gdb itself and printing the buffer
880 contents when the buffer contains no embedded nulls. Gdb does
881 not depend upon the buffer being null byte terminated, it uses
882 the length string instead. This allows gdb to handle C strings
883 (as well as strings in other languages) with embedded null
889 obstack_free (&tempbuf, NULL);
890 obstack_init (&tempbuf);
892 /* Skip the quote. */
904 *host_chars += c_parse_escape (&tokptr, &tempbuf);
910 obstack_1grow (&tempbuf, c);
912 /* FIXME: this does the wrong thing with multi-byte host
913 characters. We could use mbrlen here, but that would
914 make "set host-charset" a bit less useful. */
919 if (*tokptr != quote)
922 error (_("Unterminated string in expression."));
924 error (_("Unmatched single quote."));
928 value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/
929 value->ptr = (char *) obstack_base (&tempbuf);
930 value->length = obstack_object_size (&tempbuf);
934 return quote == '\'' ? CHAR : STRING;
941 enum exp_opcode opcode;
944 static const struct token tokentab3[] =
946 {">>=", ASSIGN_MODIFY, BINOP_RSH},
947 {"<<=", ASSIGN_MODIFY, BINOP_LSH},
948 /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */
949 {"...", DOTDOTDOT, OP_NULL},
952 static const struct token tokentab2[] =
954 {"+=", ASSIGN_MODIFY, BINOP_ADD},
955 {"-=", ASSIGN_MODIFY, BINOP_SUB},
956 {"*=", ASSIGN_MODIFY, BINOP_MUL},
957 {"/=", ASSIGN_MODIFY, BINOP_DIV},
958 {"%=", ASSIGN_MODIFY, BINOP_REM},
959 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
960 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
961 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
962 {"++", INCREMENT, BINOP_END},
963 {"--", DECREMENT, BINOP_END},
964 /*{"->", RIGHT_ARROW, BINOP_END}, Doesn't exist in Go. */
965 {"<-", LEFT_ARROW, BINOP_END},
966 {"&&", ANDAND, BINOP_END},
967 {"||", OROR, BINOP_END},
968 {"<<", LSH, BINOP_END},
969 {">>", RSH, BINOP_END},
970 {"==", EQUAL, BINOP_END},
971 {"!=", NOTEQUAL, BINOP_END},
972 {"<=", LEQ, BINOP_END},
973 {">=", GEQ, BINOP_END},
974 /*{"&^", ANDNOT, BINOP_END}, TODO */
977 /* Identifier-like tokens. */
978 static const struct token ident_tokens[] =
980 {"true", TRUE_KEYWORD, OP_NULL},
981 {"false", FALSE_KEYWORD, OP_NULL},
982 {"nil", NIL_KEYWORD, OP_NULL},
983 {"const", CONST_KEYWORD, OP_NULL},
984 {"struct", STRUCT_KEYWORD, OP_NULL},
985 {"type", TYPE_KEYWORD, OP_NULL},
986 {"interface", INTERFACE_KEYWORD, OP_NULL},
987 {"chan", CHAN_KEYWORD, OP_NULL},
988 {"byte", BYTE_KEYWORD, OP_NULL}, /* An alias of uint8. */
989 {"len", LEN_KEYWORD, OP_NULL},
990 {"cap", CAP_KEYWORD, OP_NULL},
991 {"new", NEW_KEYWORD, OP_NULL},
992 {"iota", IOTA_KEYWORD, OP_NULL},
995 /* This is set if a NAME token appeared at the very end of the input
996 string, with no whitespace separating the name from the EOF. This
997 is used only when parsing to do field name completion. */
998 static int saw_name_at_eof;
1000 /* This is set if the previously-returned token was a structure
1001 operator -- either '.' or ARROW. This is used only when parsing to
1002 do field name completion. */
1003 static int last_was_structop;
1005 /* Read one token, getting characters through lexptr. */
1008 lex_one_token (struct parser_state *par_state)
1013 const char *tokstart;
1014 int saw_structop = last_was_structop;
1017 last_was_structop = 0;
1021 prev_lexptr = lexptr;
1024 /* See if it is a special token of length 3. */
1025 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1026 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
1029 yylval.opcode = tokentab3[i].opcode;
1030 return tokentab3[i].token;
1033 /* See if it is a special token of length 2. */
1034 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1035 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
1038 yylval.opcode = tokentab2[i].opcode;
1039 /* NOTE: -> doesn't exist in Go, so we don't need to watch for
1040 setting last_was_structop here. */
1041 return tokentab2[i].token;
1044 switch (c = *tokstart)
1047 if (saw_name_at_eof)
1049 saw_name_at_eof = 0;
1052 else if (saw_structop)
1071 if (paren_depth == 0)
1078 if (comma_terminates
1079 && paren_depth == 0)
1085 /* Might be a floating point number. */
1086 if (lexptr[1] < '0' || lexptr[1] > '9')
1088 if (parse_completion)
1089 last_was_structop = 1;
1090 goto symbol; /* Nope, must be a symbol. */
1092 /* FALL THRU into number case. */
1105 /* It's a number. */
1106 int got_dot = 0, got_e = 0, toktype;
1107 const char *p = tokstart;
1108 int hex = input_radix > 10;
1110 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1118 /* This test includes !hex because 'e' is a valid hex digit
1119 and thus does not indicate a floating point number when
1120 the radix is hex. */
1121 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1122 got_dot = got_e = 1;
1123 /* This test does not include !hex, because a '.' always indicates
1124 a decimal floating point number regardless of the radix. */
1125 else if (!got_dot && *p == '.')
1127 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1128 && (*p == '-' || *p == '+'))
1129 /* This is the sign of the exponent, not the end of the
1132 /* We will take any letters or digits. parse_number will
1133 complain if past the radix, or if L or U are not final. */
1134 else if ((*p < '0' || *p > '9')
1135 && ((*p < 'a' || *p > 'z')
1136 && (*p < 'A' || *p > 'Z')))
1139 toktype = parse_number (par_state, tokstart, p - tokstart,
1140 got_dot|got_e, &yylval);
1141 if (toktype == ERROR)
1143 char *err_copy = (char *) alloca (p - tokstart + 1);
1145 memcpy (err_copy, tokstart, p - tokstart);
1146 err_copy[p - tokstart] = 0;
1147 error (_("Invalid number \"%s\"."), err_copy);
1155 const char *p = &tokstart[1];
1156 size_t len = strlen ("entry");
1158 while (isspace (*p))
1160 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
1194 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
1199 error (_("Empty character constant."));
1200 else if (host_len > 2 && c == '\'')
1203 namelen = lexptr - tokstart - 1;
1206 else if (host_len > 1)
1207 error (_("Invalid character constant."));
1213 if (!(c == '_' || c == '$'
1214 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1215 /* We must have come across a bad character (e.g. ';'). */
1216 error (_("Invalid character '%c' in expression."), c);
1218 /* It's a name. See how long it is. */
1220 for (c = tokstart[namelen];
1221 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1222 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
1224 c = tokstart[++namelen];
1227 /* The token "if" terminates the expression and is NOT removed from
1228 the input stream. It doesn't count if it appears in the
1229 expansion of a macro. */
1231 && tokstart[0] == 'i'
1232 && tokstart[1] == 'f')
1237 /* For the same reason (breakpoint conditions), "thread N"
1238 terminates the expression. "thread" could be an identifier, but
1239 an identifier is never followed by a number without intervening
1241 Handle abbreviations of these, similarly to
1242 breakpoint.c:find_condition_and_thread.
1243 TODO: Watch for "goroutine" here? */
1245 && strncmp (tokstart, "thread", namelen) == 0
1246 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
1248 const char *p = tokstart + namelen + 1;
1250 while (*p == ' ' || *p == '\t')
1252 if (*p >= '0' && *p <= '9')
1260 yylval.sval.ptr = tokstart;
1261 yylval.sval.length = namelen;
1263 /* Catch specific keywords. */
1264 copy = copy_name (yylval.sval);
1265 for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++)
1266 if (strcmp (copy, ident_tokens[i].oper) == 0)
1268 /* It is ok to always set this, even though we don't always
1269 strictly need to. */
1270 yylval.opcode = ident_tokens[i].opcode;
1271 return ident_tokens[i].token;
1274 if (*tokstart == '$')
1275 return DOLLAR_VARIABLE;
1277 if (parse_completion && *lexptr == '\0')
1278 saw_name_at_eof = 1;
1282 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
1289 DEF_VEC_O (token_and_value);
1291 /* A FIFO of tokens that have been read but not yet returned to the
1293 static VEC (token_and_value) *token_fifo;
1295 /* Non-zero if the lexer should return tokens from the FIFO. */
1298 /* Temporary storage for yylex; this holds symbol names as they are
1300 static auto_obstack name_obstack;
1302 /* Build "package.name" in name_obstack.
1303 For convenience of the caller, the name is NUL-terminated,
1304 but the NUL is not included in the recorded length. */
1306 static struct stoken
1307 build_packaged_name (const char *package, int package_len,
1308 const char *name, int name_len)
1310 struct stoken result;
1312 name_obstack.clear ();
1313 obstack_grow (&name_obstack, package, package_len);
1314 obstack_grow_str (&name_obstack, ".");
1315 obstack_grow (&name_obstack, name, name_len);
1316 obstack_grow (&name_obstack, "", 1);
1317 result.ptr = (char *) obstack_base (&name_obstack);
1318 result.length = obstack_object_size (&name_obstack) - 1;
1323 /* Return non-zero if NAME is a package name.
1324 BLOCK is the scope in which to interpret NAME; this can be NULL
1325 to mean the global scope. */
1328 package_name_p (const char *name, const struct block *block)
1331 struct field_of_this_result is_a_field_of_this;
1333 sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this).symbol;
1336 && SYMBOL_CLASS (sym) == LOC_TYPEDEF
1337 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE)
1343 /* Classify a (potential) function in the "unsafe" package.
1344 We fold these into "keywords" to keep things simple, at least until
1345 something more complex is warranted. */
1348 classify_unsafe_function (struct stoken function_name)
1350 char *copy = copy_name (function_name);
1352 if (strcmp (copy, "Sizeof") == 0)
1354 yylval.sval = function_name;
1355 return SIZEOF_KEYWORD;
1358 error (_("Unknown function in `unsafe' package: %s"), copy);
1361 /* Classify token(s) "name1.name2" where name1 is known to be a package.
1362 The contents of the token are in `yylval'.
1363 Updates yylval and returns the new token type.
1365 The result is one of NAME, NAME_OR_INT, or TYPENAME. */
1368 classify_packaged_name (const struct block *block)
1371 struct block_symbol sym;
1372 struct field_of_this_result is_a_field_of_this;
1374 copy = copy_name (yylval.sval);
1376 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1380 yylval.ssym.sym = sym;
1381 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1387 /* Classify a NAME token.
1388 The contents of the token are in `yylval'.
1389 Updates yylval and returns the new token type.
1390 BLOCK is the block in which lookups start; this can be NULL
1391 to mean the global scope.
1393 The result is one of NAME, NAME_OR_INT, or TYPENAME. */
1396 classify_name (struct parser_state *par_state, const struct block *block)
1399 struct block_symbol sym;
1401 struct field_of_this_result is_a_field_of_this;
1403 copy = copy_name (yylval.sval);
1405 /* Try primitive types first so they win over bad/weird debug info. */
1406 type = language_lookup_primitive_type (parse_language (par_state),
1407 parse_gdbarch (par_state),
1411 /* NOTE: We take advantage of the fact that yylval coming in was a
1412 NAME, and that struct ttype is a compatible extension of struct
1413 stoken, so yylval.tsym.stoken is already filled in. */
1414 yylval.tsym.type = type;
1418 /* TODO: What about other types? */
1420 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1424 yylval.ssym.sym = sym;
1425 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1429 /* If we didn't find a symbol, look again in the current package.
1430 This is to, e.g., make "p global_var" work without having to specify
1431 the package name. We intentionally only looks for objects in the
1435 char *current_package_name = go_block_package_name (block);
1437 if (current_package_name != NULL)
1439 struct stoken sval =
1440 build_packaged_name (current_package_name,
1441 strlen (current_package_name),
1442 copy, strlen (copy));
1444 xfree (current_package_name);
1445 sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN,
1446 &is_a_field_of_this);
1449 yylval.ssym.stoken = sval;
1450 yylval.ssym.sym = sym;
1451 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1457 /* Input names that aren't symbols but ARE valid hex numbers, when
1458 the input radix permits them, can be names or numbers depending
1459 on the parse. Note we support radixes > 16 here. */
1460 if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
1461 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10))
1463 YYSTYPE newlval; /* Its value is ignored. */
1464 int hextype = parse_number (par_state, copy, yylval.sval.length,
1468 yylval.ssym.sym.symbol = NULL;
1469 yylval.ssym.sym.block = NULL;
1470 yylval.ssym.is_a_field_of_this = 0;
1475 yylval.ssym.sym.symbol = NULL;
1476 yylval.ssym.sym.block = NULL;
1477 yylval.ssym.is_a_field_of_this = 0;
1481 /* This is taken from c-exp.y mostly to get something working.
1482 The basic structure has been kept because we may yet need some of it. */
1487 token_and_value current, next;
1489 if (popping && !VEC_empty (token_and_value, token_fifo))
1491 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
1492 VEC_ordered_remove (token_and_value, token_fifo, 0);
1494 /* There's no need to fall through to handle package.name
1495 as that can never happen here. In theory. */
1500 current.token = lex_one_token (pstate);
1502 /* TODO: Need a way to force specifying name1 as a package.
1505 if (current.token != NAME)
1506 return current.token;
1508 /* See if we have "name1 . name2". */
1510 current.value = yylval;
1511 next.token = lex_one_token (pstate);
1512 next.value = yylval;
1514 if (next.token == '.')
1516 token_and_value name2;
1518 name2.token = lex_one_token (pstate);
1519 name2.value = yylval;
1521 if (name2.token == NAME)
1523 /* Ok, we have "name1 . name2". */
1526 copy = copy_name (current.value.sval);
1528 if (strcmp (copy, "unsafe") == 0)
1531 return classify_unsafe_function (name2.value.sval);
1534 if (package_name_p (copy, expression_context_block))
1537 yylval.sval = build_packaged_name (current.value.sval.ptr,
1538 current.value.sval.length,
1539 name2.value.sval.ptr,
1540 name2.value.sval.length);
1541 return classify_packaged_name (expression_context_block);
1545 VEC_safe_push (token_and_value, token_fifo, &next);
1546 VEC_safe_push (token_and_value, token_fifo, &name2);
1550 VEC_safe_push (token_and_value, token_fifo, &next);
1553 /* If we arrive here we don't have a package-qualified name. */
1556 yylval = current.value;
1557 return classify_name (pstate, expression_context_block);
1561 go_parse (struct parser_state *par_state)
1563 /* Setting up the parser state. */
1564 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1565 gdb_assert (par_state != NULL);
1568 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
1571 /* Initialize some state used by the lexer. */
1572 last_was_structop = 0;
1573 saw_name_at_eof = 0;
1575 VEC_free (token_and_value, token_fifo);
1577 name_obstack.clear ();
1583 yyerror (const char *msg)
1586 lexptr = prev_lexptr;
1588 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);