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