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