use do_align () directly in tc-ia64.c
[external/binutils.git] / gdb / go-exp.y
1 /* YACC parser for Go expressions, for GDB.
2
3    Copyright (C) 2012-2016 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                           $3 = 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 = (char *) 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 = XRESIZEVEC (struct typed_stoken,
549                                                   $$.tokens, $$.len);
550
551                           p = (char *) 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.symbol;
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 block_symbol sym = $1.sym;
602
603                           if (sym.symbol)
604                             {
605                               if (symbol_read_needs_frame (sym.symbol))
606                                 {
607                                   if (innermost_block == 0
608                                       || contained_in (sym.block,
609                                                        innermost_block))
610                                     innermost_block = sym.block;
611                                 }
612
613                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
614                               write_exp_elt_block (pstate, sym.block);
615                               write_exp_elt_sym (pstate, sym.symbol);
616                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
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                             {
626                               struct bound_minimal_symbol msymbol;
627                               char *arg = copy_name ($1.stoken);
628
629                               msymbol =
630                                 lookup_bound_minimal_symbol (arg);
631                               if (msymbol.minsym != NULL)
632                                 write_exp_msymbol (pstate, msymbol);
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
645 method_exp: PACKAGENAME '.' name '.' name
646                         {
647                         }
648         ;
649 */
650
651 type  /* 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
662                         { $$ = builtin_go_type (parse_gdbarch (pstate))
663                             ->builtin_uint8; }
664         ;
665
666 /* TODO
667 name    :       NAME { $$ = $1.stoken; }
668         |       TYPENAME { $$ = $1.stoken; }
669         |       NAME_OR_INT  { $$ = $1.stoken; }
670         ;
671 */
672
673 name_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
688 static int
689 parse_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
713 static int
714 parse_number (struct parser_state *par_state,
715               const char *p, int len, int parsed_float, YYSTYPE *putithere)
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     {
740       if (! parse_go_float (parse_gdbarch (par_state), p, len,
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
856       && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
857     {
858       high_bit
859         = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
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
867       unsigned_type = parse_type (par_state)->builtin_unsigned_int;
868       signed_type = parse_type (par_state)->builtin_int;
869     }
870   else if (long_p <= 1
871            && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
872     {
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;
877     }
878   else
879     {
880       int shift;
881       if (sizeof (ULONGEST) * HOST_CHAR_BIT
882           < gdbarch_long_long_bit (parse_gdbarch (par_state)))
883         /* A long long does not fit in a LONGEST.  */
884         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
885       else
886         shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
887       high_bit = (ULONGEST) 1 << shift;
888       unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
889       signed_type = parse_type (par_state)->builtin_long_long;
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.  */
910 static struct obstack tempbuf;
911 static 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
920 static int
921 parse_string_or_char (const char *tokptr, const char **outptr,
922                       struct typed_stoken *value, int *host_chars)
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*/
978   value->ptr = (char *) obstack_base (&tempbuf);
979   value->length = obstack_object_size (&tempbuf);
980
981   *outptr = tokptr;
982
983   return quote == '\'' ? CHAR : STRING;
984 }
985
986 struct token
987 {
988   char *oper;
989   int token;
990   enum exp_opcode opcode;
991 };
992
993 static 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
1001 static 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.  */
1027 static 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.  */
1047 static 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.  */
1052 static int last_was_structop;
1053
1054 /* Read one token, getting characters through lexptr.  */
1055
1056 static int
1057 lex_one_token (struct parser_state *par_state)
1058 {
1059   int c;
1060   int namelen;
1061   unsigned int i;
1062   const char *tokstart;
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++)
1075     if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
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++)
1084     if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
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         {
1137           if (parse_completion)
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;
1156         const char *p = tokstart;
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           }
1188         toktype = parse_number (par_state, tokstart, p - tokstart,
1189                                 got_dot|got_e, &yylval);
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       {
1204         const char *p = &tokstart[1];
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     {
1297       const char *p = tokstart + namelen + 1;
1298
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++)
1315     if (strcmp (copy, ident_tokens[i].oper) == 0)
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
1326   if (parse_completion && *lexptr == '\0')
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.  */
1332 typedef struct
1333 {
1334   int token;
1335   YYSTYPE value;
1336 } token_and_value;
1337
1338 DEF_VEC_O (token_and_value);
1339
1340 /* A FIFO of tokens that have been read but not yet returned to the
1341    parser.  */
1342 static VEC (token_and_value) *token_fifo;
1343
1344 /* Non-zero if the lexer should return tokens from the FIFO.  */
1345 static int popping;
1346
1347 /* Temporary storage for yylex; this holds symbol names as they are
1348    built up.  */
1349 static 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
1355 static struct stoken
1356 build_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);
1366   result.ptr = (char *) obstack_base (&name_obstack);
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
1376 static int
1377 package_name_p (const char *name, const struct block *block)
1378 {
1379   struct symbol *sym;
1380   struct field_of_this_result is_a_field_of_this;
1381
1382   sym = lookup_symbol (name, block, STRUCT_DOMAIN, &is_a_field_of_this).symbol;
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
1396 static int
1397 classify_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
1416 static int
1417 classify_packaged_name (const struct block *block)
1418 {
1419   char *copy;
1420   struct block_symbol sym;
1421   struct field_of_this_result is_a_field_of_this;
1422
1423   copy = copy_name (yylval.sval);
1424
1425   sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1426
1427   if (sym.symbol)
1428     {
1429       yylval.ssym.sym = sym;
1430       yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
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
1444 static int
1445 classify_name (struct parser_state *par_state, const struct block *block)
1446 {
1447   struct type *type;
1448   struct block_symbol sym;
1449   char *copy;
1450   struct field_of_this_result is_a_field_of_this;
1451
1452   copy = copy_name (yylval.sval);
1453
1454   /* Try primitive types first so they win over bad/weird debug info.  */
1455   type = language_lookup_primitive_type (parse_language (par_state),
1456                                          parse_gdbarch (par_state),
1457                                          copy);
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
1471   if (sym.symbol)
1472     {
1473       yylval.ssym.sym = sym;
1474       yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
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);
1496         if (sym.symbol)
1497           {
1498             yylval.ssym.stoken = sval;
1499             yylval.ssym.sym = sym;
1500             yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
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.  */
1513       int hextype = parse_number (par_state, copy, yylval.sval.length,
1514                                   0, &newlval);
1515       if (hextype == INT)
1516         {
1517           yylval.ssym.sym.symbol = NULL;
1518           yylval.ssym.sym.block = NULL;
1519           yylval.ssym.is_a_field_of_this = 0;
1520           return NAME_OR_INT;
1521         }
1522     }
1523
1524   yylval.ssym.sym.symbol = NULL;
1525   yylval.ssym.sym.block = NULL;
1526   yylval.ssym.is_a_field_of_this = 0;
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
1533 static int
1534 yylex (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
1549   current.token = lex_one_token (pstate);
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;
1560   next.token = lex_one_token (pstate);
1561   next.value = yylval;
1562
1563   if (next.token == '.')
1564     {
1565       token_and_value name2;
1566
1567       name2.token = lex_one_token (pstate);
1568       name2.value = yylval;
1569
1570       if (name2.token == NAME)
1571         {
1572           /* Ok, we have "name1 . name2".  */
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;
1606   return classify_name (pstate, expression_context_block);
1607 }
1608
1609 int
1610 go_parse (struct parser_state *par_state)
1611 {
1612   int result;
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);
1620
1621   make_cleanup_restore_integer (&yydebug);
1622   make_cleanup_clear_parser_state (&pstate);
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
1639 void
1640 yyerror (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 }