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