1 /* YACC parser for Go expressions, for GDB.
3 Copyright (C) 2012-2014 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.
57 #include "expression.h"
59 #include "parser-defs.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 */
69 #define parse_type builtin_type (parse_gdbarch)
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. */
78 #define yymaxdepth go_maxdepth
79 #define yyparse go_parse_internal
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
92 #define yyexca go_exca
93 #define yyerrflag go_errflag
94 #define yynerrs go_nerrs
99 #define yystate go_state
102 #define yy_yyv go_yyv
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
120 #define YYDEBUG 1 /* Default to yydebug support */
123 #define YYFPRINTF parser_fprintf
127 static int yylex (void);
129 void yyerror (char *);
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. */
149 struct symtoken ssym;
151 struct typed_stoken tsval;
154 enum exp_opcode opcode;
155 struct internalvar *ivar;
156 struct stoken_vector svec;
160 /* YYSTYPE gets defined by %union. */
161 static int parse_number (const char *, int, int, YYSTYPE *);
162 static int parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
163 DOUBLEST *d, struct type **t);
166 %type <voidval> exp exp1 type_exp start variable lcurly
170 %token <typed_val_int> INT
171 %token <typed_val_float> FLOAT
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. */
181 %token <tsval> RAW_STRING
182 %token <tsval> STRING
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
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
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
202 %token IOTA_KEYWORD NIL_KEYWORD
208 /* Special type cases. */
209 %token BYTE_KEYWORD /* An alias of uint8. */
211 %token <sval> DOLLAR_VARIABLE
213 %token <opcode> ASSIGN_MODIFY
217 %right '=' ASSIGN_MODIFY
226 %left '<' '>' LEQ GEQ
231 %right UNARY INCREMENT DECREMENT
232 %right LEFT_ARROW '.' '[' '('
242 { write_exp_elt_opcode(OP_TYPE);
243 write_exp_elt_type($1);
244 write_exp_elt_opcode(OP_TYPE); }
247 /* Expressions, including the comma operator. */
250 { write_exp_elt_opcode (BINOP_COMMA); }
253 /* Expressions, not including the comma operator. */
254 exp : '*' exp %prec UNARY
255 { write_exp_elt_opcode (UNOP_IND); }
258 exp : '&' exp %prec UNARY
259 { write_exp_elt_opcode (UNOP_ADDR); }
262 exp : '-' exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_NEG); }
266 exp : '+' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_PLUS); }
270 exp : '!' exp %prec UNARY
271 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
274 exp : '^' exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
278 exp : exp INCREMENT %prec UNARY
279 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
282 exp : exp DECREMENT %prec UNARY
283 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
286 /* foo->bar is not in Go. May want as a gdb extension. Later. */
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); }
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); }
301 exp : exp '.' COMPLETE
303 mark_struct_expression ();
304 write_exp_elt_opcode (STRUCTOP_STRUCT);
307 write_exp_string (s);
308 write_exp_elt_opcode (STRUCTOP_STRUCT); }
311 exp : exp '[' exp1 ']'
312 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
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); }
326 { start_arglist (); }
336 arglist : arglist ',' exp %prec ABOVE_COMMA
341 { $$ = end_arglist () - 1; }
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); }
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); }
360 /* Binary operators in order of decreasing precedence. */
363 { write_exp_elt_opcode (BINOP_REPEAT); }
367 { write_exp_elt_opcode (BINOP_MUL); }
371 { write_exp_elt_opcode (BINOP_DIV); }
375 { write_exp_elt_opcode (BINOP_REM); }
379 { write_exp_elt_opcode (BINOP_ADD); }
383 { write_exp_elt_opcode (BINOP_SUB); }
387 { write_exp_elt_opcode (BINOP_LSH); }
391 { write_exp_elt_opcode (BINOP_RSH); }
395 { write_exp_elt_opcode (BINOP_EQUAL); }
398 exp : exp NOTEQUAL exp
399 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
403 { write_exp_elt_opcode (BINOP_LEQ); }
407 { write_exp_elt_opcode (BINOP_GEQ); }
411 { write_exp_elt_opcode (BINOP_LESS); }
415 { write_exp_elt_opcode (BINOP_GTR); }
419 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
423 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
427 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
431 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
435 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
438 exp : exp '?' exp ':' exp %prec '?'
439 { write_exp_elt_opcode (TERNOP_COND); }
443 { write_exp_elt_opcode (BINOP_ASSIGN); }
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); }
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); }
461 struct stoken_vector vec;
464 write_exp_string_vector ($1.type, &vec);
470 parse_number ($1.stoken.ptr, $1.stoken.length,
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);
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); }
491 exp : DOLLAR_VARIABLE
493 write_dollar_variable ($1);
497 exp : SIZEOF_KEYWORD '(' type ')' %prec UNARY
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);
504 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
505 write_exp_elt_opcode (OP_LONG);
509 exp : SIZEOF_KEYWORD '(' exp ')' %prec UNARY
511 /* TODO(dje): Go objects in structs. */
512 write_exp_elt_opcode (UNOP_SIZEOF);
518 /* We copy the string here, and not in the
519 lexer, to guarantee that we do not leak a
521 /* Note that we NUL-terminate here, but just
523 struct typed_stoken *vec = XNEW (struct typed_stoken);
528 vec->length = $1.length;
529 vec->ptr = malloc ($1.length + 1);
530 memcpy (vec->ptr, $1.ptr, $1.length + 1);
533 | string_exp '+' STRING
535 /* Note that we NUL-terminate here, but just
539 $$.tokens = realloc ($$.tokens,
540 $$.len * sizeof (struct typed_stoken));
542 p = malloc ($3.length + 1);
543 memcpy (p, $3.ptr, $3.length + 1);
545 $$.tokens[$$.len - 1].type = $3.type;
546 $$.tokens[$$.len - 1].length = $3.length;
547 $$.tokens[$$.len - 1].ptr = p;
551 exp : string_exp %prec ABOVE_COMMA
555 write_exp_string_vector (0 /*always utf8*/, &$1);
556 for (i = 0; i < $1.len; ++i)
557 free ($1.tokens[i].ptr);
563 { write_exp_elt_opcode (OP_BOOL);
564 write_exp_elt_longcst ((LONGEST) $1);
565 write_exp_elt_opcode (OP_BOOL); }
569 { write_exp_elt_opcode (OP_BOOL);
570 write_exp_elt_longcst ((LONGEST) $1);
571 write_exp_elt_opcode (OP_BOOL); }
574 variable: name_not_typename ENTRY
575 { struct symbol *sym = $1.sym;
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));
584 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
585 write_exp_elt_sym (sym);
586 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
590 variable: name_not_typename
591 { struct symbol *sym = $1.sym;
595 if (symbol_read_needs_frame (sym))
597 if (innermost_block == 0
598 || contained_in (block_found,
600 innermost_block = block_found;
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);
611 else if ($1.is_a_field_of_this)
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");
619 struct bound_minimal_symbol msymbol;
620 char *arg = copy_name ($1.stoken);
623 lookup_bound_minimal_symbol (arg);
624 if (msymbol.minsym != 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."));
631 error (_("No symbol \"%s\" in current context."),
632 copy_name ($1.stoken));
638 method_exp: PACKAGENAME '.' name '.' name
644 type /* Implements (approximately): [*] type-specifier */
646 { $$ = lookup_pointer_type ($2); }
650 | STRUCT_KEYWORD name
651 { $$ = lookup_struct (copy_name ($2),
652 expression_context_block); }
655 { $$ = builtin_go_type (parse_gdbarch)
660 name : NAME { $$ = $1.stoken; }
661 | TYPENAME { $$ = $1.stoken; }
662 | NAME_OR_INT { $$ = $1.stoken; }
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.
679 /* Wrapper on parse_c_float to get the type right for Go. */
682 parse_go_float (struct gdbarch *gdbarch, const char *p, int len,
683 DOUBLEST *d, struct type **t)
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);
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;
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. */
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 */
707 parse_number (const char *p, int len, int parsed_float, YYSTYPE *putithere)
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. */
717 int base = input_radix;
720 /* Number of "L" suffixes encountered. */
723 /* We have found a "L" or "U" suffix. */
724 int found_suffix = 0;
727 struct type *signed_type;
728 struct type *unsigned_type;
732 if (! parse_go_float (parse_gdbarch, p, len,
733 &putithere->typed_val_float.dval,
734 &putithere->typed_val_float.type))
739 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
783 if (c >= 'A' && c <= 'Z')
785 if (c != 'l' && c != 'u')
787 if (c >= '0' && c <= '9')
795 if (base > 10 && c >= 'a' && c <= 'f')
799 n += i = c - 'a' + 10;
812 return ERROR; /* Char not a digit */
815 return ERROR; /* Invalid digit in this base. */
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. */
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)
828 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
829 error (_("Numeric constant too large."));
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. */
846 un = (ULONGEST)n >> 2;
848 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
850 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
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
858 unsigned_type = parse_type->builtin_unsigned_int;
859 signed_type = parse_type->builtin_int;
862 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
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;
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);
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;
882 putithere->typed_val_int.val = n;
884 /* If the high bit of the worked out type is set then this number
885 has to be unsigned. */
887 if (unsigned_p || (n & high_bit))
889 putithere->typed_val_int.type = unsigned_type;
893 putithere->typed_val_int.type = signed_type;
899 /* Temporary obstack used for holding strings. */
900 static struct obstack tempbuf;
901 static int tempbuf_init;
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. */
911 parse_string_or_char (const char *tokptr, const char **outptr,
912 struct typed_stoken *value, int *host_chars)
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
928 obstack_free (&tempbuf, NULL);
929 obstack_init (&tempbuf);
931 /* Skip the quote. */
943 *host_chars += c_parse_escape (&tokptr, &tempbuf);
949 obstack_1grow (&tempbuf, c);
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. */
958 if (*tokptr != quote)
961 error (_("Unterminated string in expression."));
963 error (_("Unmatched single quote."));
967 value->type = C_STRING | (quote == '\'' ? C_CHAR : 0); /*FIXME*/
968 value->ptr = obstack_base (&tempbuf);
969 value->length = obstack_object_size (&tempbuf);
973 return quote == '\'' ? CHAR : STRING;
980 enum exp_opcode opcode;
983 static const struct token tokentab3[] =
985 {">>=", ASSIGN_MODIFY, BINOP_RSH},
986 {"<<=", ASSIGN_MODIFY, BINOP_LSH},
987 /*{"&^=", ASSIGN_MODIFY, BINOP_BITWISE_ANDNOT}, TODO */
988 {"...", DOTDOTDOT, OP_NULL},
991 static const struct token tokentab2[] =
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 */
1016 /* Identifier-like tokens. */
1017 static const struct token ident_tokens[] =
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},
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;
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;
1044 /* Read one token, getting characters through lexptr. */
1047 lex_one_token (void)
1052 const char *tokstart;
1053 int saw_structop = last_was_structop;
1056 last_was_structop = 0;
1060 prev_lexptr = 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)
1068 yylval.opcode = tokentab3[i].opcode;
1069 return tokentab3[i].token;
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)
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;
1083 switch (c = *tokstart)
1086 if (saw_name_at_eof)
1088 saw_name_at_eof = 0;
1091 else if (saw_structop)
1110 if (paren_depth == 0)
1117 if (comma_terminates
1118 && paren_depth == 0)
1124 /* Might be a floating point number. */
1125 if (lexptr[1] < '0' || lexptr[1] > '9')
1127 if (parse_completion)
1128 last_was_structop = 1;
1129 goto symbol; /* Nope, must be a symbol. */
1131 /* FALL THRU into number case. */
1144 /* It's a number. */
1145 int got_dot = 0, got_e = 0, toktype;
1146 const char *p = tokstart;
1147 int hex = input_radix > 10;
1149 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
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 == '.')
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
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')))
1178 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1179 if (toktype == ERROR)
1181 char *err_copy = (char *) alloca (p - tokstart + 1);
1183 memcpy (err_copy, tokstart, p - tokstart);
1184 err_copy[p - tokstart] = 0;
1185 error (_("Invalid number \"%s\"."), err_copy);
1193 const char *p = &tokstart[1];
1194 size_t len = strlen ("entry");
1196 while (isspace (*p))
1198 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
1232 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
1237 error (_("Empty character constant."));
1238 else if (host_len > 2 && c == '\'')
1241 namelen = lexptr - tokstart - 1;
1244 else if (host_len > 1)
1245 error (_("Invalid character constant."));
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);
1256 /* It's a name. See how long it is. */
1258 for (c = tokstart[namelen];
1259 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1260 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
1262 c = tokstart[++namelen];
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. */
1269 && tokstart[0] == 'i'
1270 && tokstart[1] == 'f')
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
1279 Handle abbreviations of these, similarly to
1280 breakpoint.c:find_condition_and_thread.
1281 TODO: Watch for "goroutine" here? */
1283 && strncmp (tokstart, "thread", namelen) == 0
1284 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
1286 const char *p = tokstart + namelen + 1;
1288 while (*p == ' ' || *p == '\t')
1290 if (*p >= '0' && *p <= '9')
1298 yylval.sval.ptr = tokstart;
1299 yylval.sval.length = namelen;
1301 /* Catch specific keywords. */
1302 copy = copy_name (yylval.sval);
1303 for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++)
1304 if (strcmp (copy, ident_tokens[i].operator) == 0)
1306 /* It is ok to always set this, even though we don't always
1307 strictly need to. */
1308 yylval.opcode = ident_tokens[i].opcode;
1309 return ident_tokens[i].token;
1312 if (*tokstart == '$')
1313 return DOLLAR_VARIABLE;
1315 if (parse_completion && *lexptr == '\0')
1316 saw_name_at_eof = 1;
1320 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
1327 DEF_VEC_O (token_and_value);
1329 /* A FIFO of tokens that have been read but not yet returned to the
1331 static VEC (token_and_value) *token_fifo;
1333 /* Non-zero if the lexer should return tokens from the FIFO. */
1336 /* Temporary storage for yylex; this holds symbol names as they are
1338 static struct obstack name_obstack;
1340 /* Build "package.name" in name_obstack.
1341 For convenience of the caller, the name is NUL-terminated,
1342 but the NUL is not included in the recorded length. */
1344 static struct stoken
1345 build_packaged_name (const char *package, int package_len,
1346 const char *name, int name_len)
1348 struct stoken result;
1350 obstack_free (&name_obstack, obstack_base (&name_obstack));
1351 obstack_grow (&name_obstack, package, package_len);
1352 obstack_grow_str (&name_obstack, ".");
1353 obstack_grow (&name_obstack, name, name_len);
1354 obstack_grow (&name_obstack, "", 1);
1355 result.ptr = obstack_base (&name_obstack);
1356 result.length = obstack_object_size (&name_obstack) - 1;
1361 /* Return non-zero if NAME is a package name.
1362 BLOCK is the scope in which to interpret NAME; this can be NULL
1363 to mean the global scope. */
1366 package_name_p (const char *name, const struct block *block)
1369 struct field_of_this_result is_a_field_of_this;
1371 sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this);
1374 && SYMBOL_CLASS (sym) == LOC_TYPEDEF
1375 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_MODULE)
1381 /* Classify a (potential) function in the "unsafe" package.
1382 We fold these into "keywords" to keep things simple, at least until
1383 something more complex is warranted. */
1386 classify_unsafe_function (struct stoken function_name)
1388 char *copy = copy_name (function_name);
1390 if (strcmp (copy, "Sizeof") == 0)
1392 yylval.sval = function_name;
1393 return SIZEOF_KEYWORD;
1396 error (_("Unknown function in `unsafe' package: %s"), copy);
1399 /* Classify token(s) "name1.name2" where name1 is known to be a package.
1400 The contents of the token are in `yylval'.
1401 Updates yylval and returns the new token type.
1403 The result is one of NAME, NAME_OR_INT, or TYPENAME. */
1406 classify_packaged_name (const struct block *block)
1410 struct field_of_this_result is_a_field_of_this;
1412 copy = copy_name (yylval.sval);
1414 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1418 yylval.ssym.sym = sym;
1419 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1425 /* Classify a NAME token.
1426 The contents of the token are in `yylval'.
1427 Updates yylval and returns the new token type.
1428 BLOCK is the block in which lookups start; this can be NULL
1429 to mean the global scope.
1431 The result is one of NAME, NAME_OR_INT, or TYPENAME. */
1434 classify_name (const struct block *block)
1439 struct field_of_this_result is_a_field_of_this;
1441 copy = copy_name (yylval.sval);
1443 /* Try primitive types first so they win over bad/weird debug info. */
1444 type = language_lookup_primitive_type_by_name (parse_language,
1445 parse_gdbarch, copy);
1448 /* NOTE: We take advantage of the fact that yylval coming in was a
1449 NAME, and that struct ttype is a compatible extension of struct
1450 stoken, so yylval.tsym.stoken is already filled in. */
1451 yylval.tsym.type = type;
1455 /* TODO: What about other types? */
1457 sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1461 yylval.ssym.sym = sym;
1462 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1466 /* If we didn't find a symbol, look again in the current package.
1467 This is to, e.g., make "p global_var" work without having to specify
1468 the package name. We intentionally only looks for objects in the
1472 char *current_package_name = go_block_package_name (block);
1474 if (current_package_name != NULL)
1476 struct stoken sval =
1477 build_packaged_name (current_package_name,
1478 strlen (current_package_name),
1479 copy, strlen (copy));
1481 xfree (current_package_name);
1482 sym = lookup_symbol (sval.ptr, block, VAR_DOMAIN,
1483 &is_a_field_of_this);
1486 yylval.ssym.stoken = sval;
1487 yylval.ssym.sym = sym;
1488 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
1494 /* Input names that aren't symbols but ARE valid hex numbers, when
1495 the input radix permits them, can be names or numbers depending
1496 on the parse. Note we support radixes > 16 here. */
1497 if ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
1498 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10))
1500 YYSTYPE newlval; /* Its value is ignored. */
1501 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
1504 yylval.ssym.sym = NULL;
1505 yylval.ssym.is_a_field_of_this = 0;
1510 yylval.ssym.sym = NULL;
1511 yylval.ssym.is_a_field_of_this = 0;
1515 /* This is taken from c-exp.y mostly to get something working.
1516 The basic structure has been kept because we may yet need some of it. */
1521 token_and_value current, next;
1523 if (popping && !VEC_empty (token_and_value, token_fifo))
1525 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
1526 VEC_ordered_remove (token_and_value, token_fifo, 0);
1528 /* There's no need to fall through to handle package.name
1529 as that can never happen here. In theory. */
1534 current.token = lex_one_token ();
1536 /* TODO: Need a way to force specifying name1 as a package.
1539 if (current.token != NAME)
1540 return current.token;
1542 /* See if we have "name1 . name2". */
1544 current.value = yylval;
1545 next.token = lex_one_token ();
1546 next.value = yylval;
1548 if (next.token == '.')
1550 token_and_value name2;
1552 name2.token = lex_one_token ();
1553 name2.value = yylval;
1555 if (name2.token == NAME)
1557 /* Ok, we have "name1 . name2". */
1560 copy = copy_name (current.value.sval);
1562 if (strcmp (copy, "unsafe") == 0)
1565 return classify_unsafe_function (name2.value.sval);
1568 if (package_name_p (copy, expression_context_block))
1571 yylval.sval = build_packaged_name (current.value.sval.ptr,
1572 current.value.sval.length,
1573 name2.value.sval.ptr,
1574 name2.value.sval.length);
1575 return classify_packaged_name (expression_context_block);
1579 VEC_safe_push (token_and_value, token_fifo, &next);
1580 VEC_safe_push (token_and_value, token_fifo, &name2);
1584 VEC_safe_push (token_and_value, token_fifo, &next);
1587 /* If we arrive here we don't have a package-qualified name. */
1590 yylval = current.value;
1591 return classify_name (expression_context_block);
1598 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
1600 make_cleanup_restore_integer (&yydebug);
1601 yydebug = parser_debug;
1603 /* Initialize some state used by the lexer. */
1604 last_was_structop = 0;
1605 saw_name_at_eof = 0;
1607 VEC_free (token_and_value, token_fifo);
1609 obstack_init (&name_obstack);
1610 make_cleanup_obstack_free (&name_obstack);
1612 result = yyparse ();
1613 do_cleanups (back_to);
1621 lexptr = prev_lexptr;
1623 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);