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