Add casts to memory allocation related calls
[external/binutils.git] / gdb / d-exp.y
1 /* YACC parser for D expressions, for GDB.
2
3    Copyright (C) 2014-2015 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, jv-exp.y.  */
21
22 /* Parse a D 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 %{
40
41 #include "defs.h"
42 #include <ctype.h>
43 #include "expression.h"
44 #include "value.h"
45 #include "parser-defs.h"
46 #include "language.h"
47 #include "c-lang.h"
48 #include "d-lang.h"
49 #include "bfd.h" /* Required by objfiles.h.  */
50 #include "symfile.h" /* Required by objfiles.h.  */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #include "charset.h"
53 #include "block.h"
54
55 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
56 #define parse_d_type(ps) builtin_d_type (parse_gdbarch (ps))
57
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
59    as well as gratuitiously global symbol names, so we can have multiple
60    yacc generated parsers in gdb.  Note that these are only the variables
61    produced by yacc.  If other parser generators (bison, byacc, etc) produce
62    additional global names that conflict at link time, then those parser
63    generators need to be fixed instead of adding those names to this list.  */
64
65 #define yymaxdepth d_maxdepth
66 #define yyparse d_parse_internal
67 #define yylex   d_lex
68 #define yyerror d_error
69 #define yylval  d_lval
70 #define yychar  d_char
71 #define yydebug d_debug
72 #define yypact  d_pact
73 #define yyr1    d_r1
74 #define yyr2    d_r2
75 #define yydef   d_def
76 #define yychk   d_chk
77 #define yypgo   d_pgo
78 #define yyact   d_act
79 #define yyexca  d_exca
80 #define yyerrflag d_errflag
81 #define yynerrs d_nerrs
82 #define yyps    d_ps
83 #define yypv    d_pv
84 #define yys     d_s
85 #define yy_yys  d_yys
86 #define yystate d_state
87 #define yytmp   d_tmp
88 #define yyv     d_v
89 #define yy_yyv  d_yyv
90 #define yyval   d_val
91 #define yylloc  d_lloc
92 #define yyreds  d_reds  /* With YYDEBUG defined */
93 #define yytoks  d_toks  /* With YYDEBUG defined */
94 #define yyname  d_name  /* With YYDEBUG defined */
95 #define yyrule  d_rule  /* With YYDEBUG defined */
96 #define yylhs   d_yylhs
97 #define yylen   d_yylen
98 #define yydefre d_yydefred
99 #define yydgoto d_yydgoto
100 #define yysindex d_yysindex
101 #define yyrindex d_yyrindex
102 #define yygindex d_yygindex
103 #define yytable d_yytable
104 #define yycheck d_yycheck
105 #define yyss    d_yyss
106 #define yysslim d_yysslim
107 #define yyssp   d_yyssp
108 #define yystacksize d_yystacksize
109 #define yyvs    d_yyvs
110 #define yyvsp   d_yyvsp
111
112 #ifndef YYDEBUG
113 #define YYDEBUG 1       /* Default to yydebug support */
114 #endif
115
116 #define YYFPRINTF parser_fprintf
117
118 /* The state of the parser, used internally when we are parsing the
119    expression.  */
120
121 static struct parser_state *pstate = NULL;
122
123 int yyparse (void);
124
125 static int yylex (void);
126
127 void yyerror (char *);
128
129 static int type_aggregate_p (struct type *);
130
131 %}
132
133 /* Although the yacc "value" of an expression is not used,
134    since the result is stored in the structure being created,
135    other node types do have values.  */
136
137 %union
138   {
139     struct {
140       LONGEST val;
141       struct type *type;
142     } typed_val_int;
143     struct {
144       DOUBLEST dval;
145       struct type *type;
146     } typed_val_float;
147     struct symbol *sym;
148     struct type *tval;
149     struct typed_stoken tsval;
150     struct stoken sval;
151     struct ttype tsym;
152     struct symtoken ssym;
153     int ival;
154     int voidval;
155     struct block *bval;
156     enum exp_opcode opcode;
157     struct stoken_vector svec;
158   }
159
160 %{
161 /* YYSTYPE gets defined by %union */
162 static int parse_number (struct parser_state *, const char *,
163                          int, int, YYSTYPE *);
164 %}
165
166 %token <sval> IDENTIFIER UNKNOWN_NAME
167 %token <tsym> TYPENAME
168 %token <voidval> COMPLETE
169
170 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
171    but which would parse as a valid number in the current input radix.
172    E.g. "c" when input_radix==16.  Depending on the parse, it will be
173    turned into a name or into a number.  */
174
175 %token <sval> NAME_OR_INT
176
177 %token <typed_val_int> INTEGER_LITERAL
178 %token <typed_val_float> FLOAT_LITERAL
179 %token <tsval> CHARACTER_LITERAL
180 %token <tsval> STRING_LITERAL
181
182 %type <svec> StringExp
183 %type <tval> BasicType TypeExp
184 %type <sval> IdentifierExp
185 %type <ival> ArrayLiteral
186
187 %token ENTRY
188 %token ERROR
189
190 /* Keywords that have a constant value.  */
191 %token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
192 /* Class 'super' accessor.  */
193 %token SUPER_KEYWORD
194 /* Properties.  */
195 %token CAST_KEYWORD SIZEOF_KEYWORD
196 %token TYPEOF_KEYWORD TYPEID_KEYWORD
197 %token INIT_KEYWORD
198 /* Comparison keywords.  */
199 /* Type storage classes.  */
200 %token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
201 /* Non-scalar type keywords.  */
202 %token STRUCT_KEYWORD UNION_KEYWORD
203 %token CLASS_KEYWORD INTERFACE_KEYWORD
204 %token ENUM_KEYWORD TEMPLATE_KEYWORD
205 %token DELEGATE_KEYWORD FUNCTION_KEYWORD
206
207 %token <sval> DOLLAR_VARIABLE
208
209 %token <opcode> ASSIGN_MODIFY
210
211 %left ','
212 %right '=' ASSIGN_MODIFY
213 %right '?'
214 %left OROR
215 %left ANDAND
216 %left '|'
217 %left '^'
218 %left '&'
219 %left EQUAL NOTEQUAL '<' '>' LEQ GEQ
220 %right LSH RSH
221 %left '+' '-'
222 %left '*' '/' '%'
223 %right HATHAT
224 %left IDENTITY NOTIDENTITY
225 %right INCREMENT DECREMENT
226 %right '.' '[' '('
227 %token DOTDOT
228
229 \f
230 %%
231
232 start   :
233         Expression
234 |       TypeExp
235 ;
236
237 /* Expressions, including the comma operator.  */
238
239 Expression:
240         CommaExpression
241 ;
242
243 CommaExpression:
244         AssignExpression
245 |       AssignExpression ',' CommaExpression
246                 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
247 ;
248
249 AssignExpression:
250         ConditionalExpression
251 |       ConditionalExpression '=' AssignExpression
252                 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
253 |       ConditionalExpression ASSIGN_MODIFY AssignExpression
254                 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
255                   write_exp_elt_opcode (pstate, $2);
256                   write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
257 ;
258
259 ConditionalExpression:
260         OrOrExpression
261 |       OrOrExpression '?' Expression ':' ConditionalExpression
262                 { write_exp_elt_opcode (pstate, TERNOP_COND); }
263 ;
264
265 OrOrExpression:
266         AndAndExpression
267 |       OrOrExpression OROR AndAndExpression
268                 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
269 ;
270
271 AndAndExpression:
272         OrExpression
273 |       AndAndExpression ANDAND OrExpression
274                 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
275 ;
276
277 OrExpression:
278         XorExpression
279 |       OrExpression '|' XorExpression
280                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
281 ;
282
283 XorExpression:
284         AndExpression
285 |       XorExpression '^' AndExpression
286                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
287 ;
288
289 AndExpression:
290         CmpExpression
291 |       AndExpression '&' CmpExpression
292                 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
293 ;
294
295 CmpExpression:
296         ShiftExpression
297 |       EqualExpression
298 |       IdentityExpression
299 |       RelExpression
300 ;
301
302 EqualExpression:
303         ShiftExpression EQUAL ShiftExpression
304                 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
305 |       ShiftExpression NOTEQUAL ShiftExpression
306                 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
307 ;
308
309 IdentityExpression:
310         ShiftExpression IDENTITY ShiftExpression
311                 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
312 |       ShiftExpression NOTIDENTITY ShiftExpression
313                 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
314 ;
315
316 RelExpression:
317         ShiftExpression '<' ShiftExpression
318                 { write_exp_elt_opcode (pstate, BINOP_LESS); }
319 |       ShiftExpression LEQ ShiftExpression
320                 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
321 |       ShiftExpression '>' ShiftExpression
322                 { write_exp_elt_opcode (pstate, BINOP_GTR); }
323 |       ShiftExpression GEQ ShiftExpression
324                 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
325 ;
326
327 ShiftExpression:
328         AddExpression
329 |       ShiftExpression LSH AddExpression
330                 { write_exp_elt_opcode (pstate, BINOP_LSH); }
331 |       ShiftExpression RSH AddExpression
332                 { write_exp_elt_opcode (pstate, BINOP_RSH); }
333 ;
334
335 AddExpression:
336         MulExpression
337 |       AddExpression '+' MulExpression
338                 { write_exp_elt_opcode (pstate, BINOP_ADD); }
339 |       AddExpression '-' MulExpression
340                 { write_exp_elt_opcode (pstate, BINOP_SUB); }
341 |       AddExpression '~' MulExpression
342                 { write_exp_elt_opcode (pstate, BINOP_CONCAT); }
343 ;
344
345 MulExpression:
346         UnaryExpression
347 |       MulExpression '*' UnaryExpression
348                 { write_exp_elt_opcode (pstate, BINOP_MUL); }
349 |       MulExpression '/' UnaryExpression
350                 { write_exp_elt_opcode (pstate, BINOP_DIV); }
351 |       MulExpression '%' UnaryExpression
352                 { write_exp_elt_opcode (pstate, BINOP_REM); }
353
354 UnaryExpression:
355         '&' UnaryExpression
356                 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
357 |       INCREMENT UnaryExpression
358                 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
359 |       DECREMENT UnaryExpression
360                 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
361 |       '*' UnaryExpression
362                 { write_exp_elt_opcode (pstate, UNOP_IND); }
363 |       '-' UnaryExpression
364                 { write_exp_elt_opcode (pstate, UNOP_NEG); }
365 |       '+' UnaryExpression
366                 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
367 |       '!' UnaryExpression
368                 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
369 |       '~' UnaryExpression
370                 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
371 |       CastExpression
372 |       PowExpression
373 ;
374
375 CastExpression:
376         CAST_KEYWORD '(' TypeExp ')' UnaryExpression
377                 { write_exp_elt_opcode (pstate, UNOP_CAST);
378                   write_exp_elt_type (pstate, $3);
379                   write_exp_elt_opcode (pstate, UNOP_CAST); }
380         /* C style cast is illegal D, but is still recognised in
381            the grammar, so we keep this around for convenience.  */
382 |       '(' TypeExp ')' UnaryExpression
383                 { write_exp_elt_opcode (pstate, UNOP_CAST);
384                   write_exp_elt_type (pstate, $2);
385                   write_exp_elt_opcode (pstate, UNOP_CAST); }
386 ;
387
388 PowExpression:
389         PostfixExpression
390 |       PostfixExpression HATHAT UnaryExpression
391                 { write_exp_elt_opcode (pstate, BINOP_EXP); }
392 ;
393
394 PostfixExpression:
395         PrimaryExpression
396 |       PostfixExpression '.' COMPLETE
397                 { struct stoken s;
398                   mark_struct_expression (pstate);
399                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
400                   s.ptr = "";
401                   s.length = 0;
402                   write_exp_string (pstate, s);
403                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
404 |       PostfixExpression '.' IDENTIFIER
405                 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
406                   write_exp_string (pstate, $3);
407                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
408 |       PostfixExpression '.' IDENTIFIER COMPLETE
409                 { mark_struct_expression (pstate);
410                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
411                   write_exp_string (pstate, $3);
412                   write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
413 |       PostfixExpression INCREMENT
414                 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
415 |       PostfixExpression DECREMENT
416                 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
417 |       CallExpression
418 |       IndexExpression
419 |       SliceExpression
420 ;
421
422 ArgumentList:
423         AssignExpression
424                 { arglist_len = 1; }
425 |       ArgumentList ',' AssignExpression
426                 { arglist_len++; }
427 ;
428
429 ArgumentList_opt:
430         /* EMPTY */
431                 { arglist_len = 0; }
432 |       ArgumentList
433 ;
434
435 CallExpression:
436         PostfixExpression '('
437                 { start_arglist (); }
438         ArgumentList_opt ')'
439                 { write_exp_elt_opcode (pstate, OP_FUNCALL);
440                   write_exp_elt_longcst (pstate, (LONGEST) end_arglist ());
441                   write_exp_elt_opcode (pstate, OP_FUNCALL); }
442 ;
443
444 IndexExpression:
445         PostfixExpression '[' ArgumentList ']'
446                 { if (arglist_len > 0)
447                     {
448                       write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
449                       write_exp_elt_longcst (pstate, (LONGEST) arglist_len);
450                       write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
451                     }
452                   else
453                     write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
454                 }
455 ;
456
457 SliceExpression:
458         PostfixExpression '[' ']'
459                 { /* Do nothing.  */ }
460 |       PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']'
461                 { write_exp_elt_opcode (pstate, TERNOP_SLICE); }
462 ;
463
464 PrimaryExpression:
465         '(' Expression ')'
466                 { /* Do nothing.  */ }
467 |       IdentifierExp
468                 { struct bound_minimal_symbol msymbol;
469                   char *copy = copy_name ($1);
470                   struct field_of_this_result is_a_field_of_this;
471                   struct block_symbol sym;
472
473                   /* Handle VAR, which could be local or global.  */
474                   sym = lookup_symbol (copy, expression_context_block, VAR_DOMAIN,
475                                        &is_a_field_of_this);
476                   if (sym.symbol && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
477                     {
478                       if (symbol_read_needs_frame (sym.symbol))
479                         {
480                           if (innermost_block == 0
481                               || contained_in (sym.block, innermost_block))
482                             innermost_block = sym.block;
483                         }
484
485                       write_exp_elt_opcode (pstate, OP_VAR_VALUE);
486                       /* We want to use the selected frame, not another more inner frame
487                          which happens to be in the same block.  */
488                       write_exp_elt_block (pstate, NULL);
489                       write_exp_elt_sym (pstate, sym.symbol);
490                       write_exp_elt_opcode (pstate, OP_VAR_VALUE);
491                     }
492                   else if (is_a_field_of_this.type != NULL)
493                      {
494                       /* It hangs off of `this'.  Must not inadvertently convert from a
495                          method call to data ref.  */
496                       if (innermost_block == 0
497                           || contained_in (sym.block, innermost_block))
498                         innermost_block = sym.block;
499                       write_exp_elt_opcode (pstate, OP_THIS);
500                       write_exp_elt_opcode (pstate, OP_THIS);
501                       write_exp_elt_opcode (pstate, STRUCTOP_PTR);
502                       write_exp_string (pstate, $1);
503                       write_exp_elt_opcode (pstate, STRUCTOP_PTR);
504                     }
505                   else
506                     {
507                       /* Lookup foreign name in global static symbols.  */
508                       msymbol = lookup_bound_minimal_symbol (copy);
509                       if (msymbol.minsym != NULL)
510                         write_exp_msymbol (pstate, msymbol);
511                       else if (!have_full_symbols () && !have_partial_symbols ())
512                         error (_("No symbol table is loaded.  Use the \"file\" command"));
513                       else
514                         error (_("No symbol \"%s\" in current context."), copy);
515                     }
516                   }
517 |       TypeExp '.' IdentifierExp
518                         { struct type *type = check_typedef ($1);
519
520                           /* Check if the qualified name is in the global
521                              context.  However if the symbol has not already
522                              been resolved, it's not likely to be found.  */
523                           if (TYPE_CODE (type) == TYPE_CODE_MODULE)
524                             {
525                               struct bound_minimal_symbol msymbol;
526                               struct block_symbol sym;
527                               const char *type_name = TYPE_SAFE_NAME (type);
528                               int type_name_len = strlen (type_name);
529                               char *name;
530
531                               name = xstrprintf ("%.*s.%.*s",
532                                                  type_name_len, type_name,
533                                                  $3.length, $3.ptr);
534                               make_cleanup (xfree, name);
535
536                               sym =
537                                 lookup_symbol (name, (const struct block *) NULL,
538                                                VAR_DOMAIN, NULL);
539                               if (sym.symbol)
540                                 {
541                                   write_exp_elt_opcode (pstate, OP_VAR_VALUE);
542                                   write_exp_elt_block (pstate, sym.block);
543                                   write_exp_elt_sym (pstate, sym.symbol);
544                                   write_exp_elt_opcode (pstate, OP_VAR_VALUE);
545                                   break;
546                                 }
547
548                               msymbol = lookup_bound_minimal_symbol (name);
549                               if (msymbol.minsym != NULL)
550                                 write_exp_msymbol (pstate, msymbol);
551                               else if (!have_full_symbols () && !have_partial_symbols ())
552                                 error (_("No symbol table is loaded.  Use the \"file\" command."));
553                               else
554                                 error (_("No symbol \"%s\" in current context."), name);
555                             }
556
557                           /* Check if the qualified name resolves as a member
558                              of an aggregate or an enum type.  */
559                           if (!type_aggregate_p (type))
560                             error (_("`%s' is not defined as an aggregate type."),
561                                    TYPE_SAFE_NAME (type));
562
563                           write_exp_elt_opcode (pstate, OP_SCOPE);
564                           write_exp_elt_type (pstate, type);
565                           write_exp_string (pstate, $3);
566                           write_exp_elt_opcode (pstate, OP_SCOPE);
567                         }
568 |       DOLLAR_VARIABLE
569                 { write_dollar_variable (pstate, $1); }
570 |       NAME_OR_INT
571                 { YYSTYPE val;
572                   parse_number (pstate, $1.ptr, $1.length, 0, &val);
573                   write_exp_elt_opcode (pstate, OP_LONG);
574                   write_exp_elt_type (pstate, val.typed_val_int.type);
575                   write_exp_elt_longcst (pstate,
576                                          (LONGEST) val.typed_val_int.val);
577                   write_exp_elt_opcode (pstate, OP_LONG); }
578 |       NULL_KEYWORD
579                 { struct type *type = parse_d_type (pstate)->builtin_void;
580                   type = lookup_pointer_type (type);
581                   write_exp_elt_opcode (pstate, OP_LONG);
582                   write_exp_elt_type (pstate, type);
583                   write_exp_elt_longcst (pstate, (LONGEST) 0);
584                   write_exp_elt_opcode (pstate, OP_LONG); }
585 |       TRUE_KEYWORD
586                 { write_exp_elt_opcode (pstate, OP_BOOL);
587                   write_exp_elt_longcst (pstate, (LONGEST) 1);
588                   write_exp_elt_opcode (pstate, OP_BOOL); }
589 |       FALSE_KEYWORD
590                 { write_exp_elt_opcode (pstate, OP_BOOL);
591                   write_exp_elt_longcst (pstate, (LONGEST) 0);
592                   write_exp_elt_opcode (pstate, OP_BOOL); }
593 |       INTEGER_LITERAL
594                 { write_exp_elt_opcode (pstate, OP_LONG);
595                   write_exp_elt_type (pstate, $1.type);
596                   write_exp_elt_longcst (pstate, (LONGEST)($1.val));
597                   write_exp_elt_opcode (pstate, OP_LONG); }
598 |       FLOAT_LITERAL
599                 { write_exp_elt_opcode (pstate, OP_DOUBLE);
600                   write_exp_elt_type (pstate, $1.type);
601                   write_exp_elt_dblcst (pstate, $1.dval);
602                   write_exp_elt_opcode (pstate, OP_DOUBLE); }
603 |       CHARACTER_LITERAL
604                 { struct stoken_vector vec;
605                   vec.len = 1;
606                   vec.tokens = &$1;
607                   write_exp_string_vector (pstate, $1.type, &vec); }
608 |       StringExp
609                 { int i;
610                   write_exp_string_vector (pstate, 0, &$1);
611                   for (i = 0; i < $1.len; ++i)
612                     free ($1.tokens[i].ptr);
613                   free ($1.tokens); }
614 |       ArrayLiteral
615                 { write_exp_elt_opcode (pstate, OP_ARRAY);
616                   write_exp_elt_longcst (pstate, (LONGEST) 0);
617                   write_exp_elt_longcst (pstate, (LONGEST) $1 - 1);
618                   write_exp_elt_opcode (pstate, OP_ARRAY); }
619 ;
620
621 ArrayLiteral:
622         '[' ArgumentList_opt ']'
623                 { $$ = arglist_len; }
624 ;
625
626 IdentifierExp:
627         IDENTIFIER
628 ;
629
630 StringExp:
631         STRING_LITERAL
632                 { /* We copy the string here, and not in the
633                      lexer, to guarantee that we do not leak a
634                      string.  Note that we follow the
635                      NUL-termination convention of the
636                      lexer.  */
637                   struct typed_stoken *vec = XNEW (struct typed_stoken);
638                   $$.len = 1;
639                   $$.tokens = vec;
640
641                   vec->type = $1.type;
642                   vec->length = $1.length;
643                   vec->ptr = (char *) malloc ($1.length + 1);
644                   memcpy (vec->ptr, $1.ptr, $1.length + 1);
645                 }
646 |       StringExp STRING_LITERAL
647                 { /* Note that we NUL-terminate here, but just
648                      for convenience.  */
649                   char *p;
650                   ++$$.len;
651                   $$.tokens
652                     = XRESIZEVEC (struct typed_stoken, $$.tokens, $$.len);
653
654                   p = (char *) malloc ($2.length + 1);
655                   memcpy (p, $2.ptr, $2.length + 1);
656
657                   $$.tokens[$$.len - 1].type = $2.type;
658                   $$.tokens[$$.len - 1].length = $2.length;
659                   $$.tokens[$$.len - 1].ptr = p;
660                 }
661 ;
662
663 TypeExp:
664         '(' TypeExp ')'
665                 { /* Do nothing.  */ }
666 |       BasicType
667                 { write_exp_elt_opcode (pstate, OP_TYPE);
668                   write_exp_elt_type (pstate, $1);
669                   write_exp_elt_opcode (pstate, OP_TYPE); }
670 |       BasicType BasicType2
671                 { $$ = follow_types ($1);
672                   write_exp_elt_opcode (pstate, OP_TYPE);
673                   write_exp_elt_type (pstate, $$);
674                   write_exp_elt_opcode (pstate, OP_TYPE);
675                 }
676 ;
677
678 BasicType2:
679         '*'
680                 { push_type (tp_pointer); }
681 |       '*' BasicType2
682                 { push_type (tp_pointer); }
683 |       '[' INTEGER_LITERAL ']'
684                 { push_type_int ($2.val);
685                   push_type (tp_array); }
686 |       '[' INTEGER_LITERAL ']' BasicType2
687                 { push_type_int ($2.val);
688                   push_type (tp_array); }
689 ;
690
691 BasicType:
692         TYPENAME
693                 { $$ = $1.type; }
694 ;
695
696 %%
697
698 /* Return true if the type is aggregate-like.  */
699
700 static int
701 type_aggregate_p (struct type *type)
702 {
703   return (TYPE_CODE (type) == TYPE_CODE_STRUCT
704           || TYPE_CODE (type) == TYPE_CODE_UNION
705           || (TYPE_CODE (type) == TYPE_CODE_ENUM
706               && TYPE_DECLARED_CLASS (type)));
707 }
708
709 /* Take care of parsing a number (anything that starts with a digit).
710    Set yylval and return the token type; update lexptr.
711    LEN is the number of characters in it.  */
712
713 /*** Needs some error checking for the float case ***/
714
715 static int
716 parse_number (struct parser_state *ps, const char *p,
717               int len, int parsed_float, YYSTYPE *putithere)
718 {
719   ULONGEST n = 0;
720   ULONGEST prevn = 0;
721   ULONGEST un;
722
723   int i = 0;
724   int c;
725   int base = input_radix;
726   int unsigned_p = 0;
727   int long_p = 0;
728
729   /* We have found a "L" or "U" suffix.  */
730   int found_suffix = 0;
731
732   ULONGEST high_bit;
733   struct type *signed_type;
734   struct type *unsigned_type;
735
736   if (parsed_float)
737     {
738       const struct builtin_d_type *builtin_d_types;
739       const char *suffix;
740       int suffix_len;
741       char *s, *sp;
742
743       /* Strip out all embedded '_' before passing to parse_float.  */
744       s = (char *) alloca (len + 1);
745       sp = s;
746       while (len-- > 0)
747         {
748           if (*p != '_')
749             *sp++ = *p;
750           p++;
751         }
752       *sp = '\0';
753       len = strlen (s);
754
755       if (! parse_float (s, len, &putithere->typed_val_float.dval, &suffix))
756         return ERROR;
757
758       suffix_len = s + len - suffix;
759
760       if (suffix_len == 0)
761         {
762           putithere->typed_val_float.type
763             = parse_d_type (ps)->builtin_double;
764         }
765       else if (suffix_len == 1)
766         {
767           /* Check suffix for `f', `l', or `i' (float, real, or idouble).  */
768           if (tolower (*suffix) == 'f')
769             {
770               putithere->typed_val_float.type
771                 = parse_d_type (ps)->builtin_float;
772             }
773           else if (tolower (*suffix) == 'l')
774             {
775               putithere->typed_val_float.type
776                 = parse_d_type (ps)->builtin_real;
777             }
778           else if (tolower (*suffix) == 'i')
779             {
780               putithere->typed_val_float.type
781                 = parse_d_type (ps)->builtin_idouble;
782             }
783           else
784             return ERROR;
785         }
786       else if (suffix_len == 2)
787         {
788           /* Check suffix for `fi' or `li' (ifloat or ireal).  */
789           if (tolower (suffix[0]) == 'f' && tolower (suffix[1] == 'i'))
790             {
791               putithere->typed_val_float.type
792                 = parse_d_type (ps)->builtin_ifloat;
793             }
794           else if (tolower (suffix[0]) == 'l' && tolower (suffix[1] == 'i'))
795             {
796               putithere->typed_val_float.type
797                 = parse_d_type (ps)->builtin_ireal;
798             }
799           else
800             return ERROR;
801         }
802       else
803         return ERROR;
804
805       return FLOAT_LITERAL;
806     }
807
808   /* Handle base-switching prefixes 0x, 0b, 0 */
809   if (p[0] == '0')
810     switch (p[1])
811       {
812       case 'x':
813       case 'X':
814         if (len >= 3)
815           {
816             p += 2;
817             base = 16;
818             len -= 2;
819           }
820         break;
821
822       case 'b':
823       case 'B':
824         if (len >= 3)
825           {
826             p += 2;
827             base = 2;
828             len -= 2;
829           }
830         break;
831
832       default:
833         base = 8;
834         break;
835       }
836
837   while (len-- > 0)
838     {
839       c = *p++;
840       if (c == '_')
841         continue;       /* Ignore embedded '_'.  */
842       if (c >= 'A' && c <= 'Z')
843         c += 'a' - 'A';
844       if (c != 'l' && c != 'u')
845         n *= base;
846       if (c >= '0' && c <= '9')
847         {
848           if (found_suffix)
849             return ERROR;
850           n += i = c - '0';
851         }
852       else
853         {
854           if (base > 10 && c >= 'a' && c <= 'f')
855             {
856               if (found_suffix)
857                 return ERROR;
858               n += i = c - 'a' + 10;
859             }
860           else if (c == 'l' && long_p == 0)
861             {
862               long_p = 1;
863               found_suffix = 1;
864             }
865           else if (c == 'u' && unsigned_p == 0)
866             {
867               unsigned_p = 1;
868               found_suffix = 1;
869             }
870           else
871             return ERROR;       /* Char not a digit */
872         }
873       if (i >= base)
874         return ERROR;           /* Invalid digit in this base.  */
875       /* Portably test for integer overflow.  */
876       if (c != 'l' && c != 'u')
877         {
878           ULONGEST n2 = prevn * base;
879           if ((n2 / base != prevn) || (n2 + i < prevn))
880             error (_("Numeric constant too large."));
881         }
882       prevn = n;
883     }
884
885   /* An integer constant is an int or a long.  An L suffix forces it to
886      be long, and a U suffix forces it to be unsigned.  To figure out
887      whether it fits, we shift it right and see whether anything remains.
888      Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
889      more in one operation, because many compilers will warn about such a
890      shift (which always produces a zero result).  To deal with the case
891      where it is we just always shift the value more than once, with fewer
892      bits each time.  */
893   un = (ULONGEST) n >> 2;
894   if (long_p == 0 && (un >> 30) == 0)
895     {
896       high_bit = ((ULONGEST) 1) << 31;
897       signed_type = parse_d_type (ps)->builtin_int;
898       /* For decimal notation, keep the sign of the worked out type.  */
899       if (base == 10 && !unsigned_p)
900         unsigned_type = parse_d_type (ps)->builtin_long;
901       else
902         unsigned_type = parse_d_type (ps)->builtin_uint;
903     }
904   else
905     {
906       int shift;
907       if (sizeof (ULONGEST) * HOST_CHAR_BIT < 64)
908         /* A long long does not fit in a LONGEST.  */
909         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
910       else
911         shift = 63;
912       high_bit = (ULONGEST) 1 << shift;
913       signed_type = parse_d_type (ps)->builtin_long;
914       unsigned_type = parse_d_type (ps)->builtin_ulong;
915     }
916
917   putithere->typed_val_int.val = n;
918
919   /* If the high bit of the worked out type is set then this number
920      has to be unsigned_type.  */
921   if (unsigned_p || (n & high_bit))
922     putithere->typed_val_int.type = unsigned_type;
923   else
924     putithere->typed_val_int.type = signed_type;
925
926   return INTEGER_LITERAL;
927 }
928
929 /* Temporary obstack used for holding strings.  */
930 static struct obstack tempbuf;
931 static int tempbuf_init;
932
933 /* Parse a string or character literal from TOKPTR.  The string or
934    character may be wide or unicode.  *OUTPTR is set to just after the
935    end of the literal in the input string.  The resulting token is
936    stored in VALUE.  This returns a token value, either STRING or
937    CHAR, depending on what was parsed.  *HOST_CHARS is set to the
938    number of host characters in the literal.  */
939
940 static int
941 parse_string_or_char (const char *tokptr, const char **outptr,
942                       struct typed_stoken *value, int *host_chars)
943 {
944   int quote;
945
946   /* Build the gdb internal form of the input string in tempbuf.  Note
947      that the buffer is null byte terminated *only* for the
948      convenience of debugging gdb itself and printing the buffer
949      contents when the buffer contains no embedded nulls.  Gdb does
950      not depend upon the buffer being null byte terminated, it uses
951      the length string instead.  This allows gdb to handle C strings
952      (as well as strings in other languages) with embedded null
953      bytes */
954
955   if (!tempbuf_init)
956     tempbuf_init = 1;
957   else
958     obstack_free (&tempbuf, NULL);
959   obstack_init (&tempbuf);
960
961   /* Skip the quote.  */
962   quote = *tokptr;
963   ++tokptr;
964
965   *host_chars = 0;
966
967   while (*tokptr)
968     {
969       char c = *tokptr;
970       if (c == '\\')
971         {
972            ++tokptr;
973            *host_chars += c_parse_escape (&tokptr, &tempbuf);
974         }
975       else if (c == quote)
976         break;
977       else
978         {
979           obstack_1grow (&tempbuf, c);
980           ++tokptr;
981           /* FIXME: this does the wrong thing with multi-byte host
982              characters.  We could use mbrlen here, but that would
983              make "set host-charset" a bit less useful.  */
984           ++*host_chars;
985         }
986     }
987
988   if (*tokptr != quote)
989     {
990       if (quote == '"' || quote == '`')
991         error (_("Unterminated string in expression."));
992       else
993         error (_("Unmatched single quote."));
994     }
995   ++tokptr;
996
997   /* FIXME: should instead use own language string_type enum
998      and handle D-specific string suffixes here. */
999   if (quote == '\'')
1000     value->type = C_CHAR;
1001   else
1002     value->type = C_STRING;
1003
1004   value->ptr = obstack_base (&tempbuf);
1005   value->length = obstack_object_size (&tempbuf);
1006
1007   *outptr = tokptr;
1008
1009   return quote == '\'' ? CHARACTER_LITERAL : STRING_LITERAL;
1010 }
1011
1012 struct token
1013 {
1014   char *oper;
1015   int token;
1016   enum exp_opcode opcode;
1017 };
1018
1019 static const struct token tokentab3[] =
1020   {
1021     {"^^=", ASSIGN_MODIFY, BINOP_EXP},
1022     {"<<=", ASSIGN_MODIFY, BINOP_LSH},
1023     {">>=", ASSIGN_MODIFY, BINOP_RSH},
1024   };
1025
1026 static const struct token tokentab2[] =
1027   {
1028     {"+=", ASSIGN_MODIFY, BINOP_ADD},
1029     {"-=", ASSIGN_MODIFY, BINOP_SUB},
1030     {"*=", ASSIGN_MODIFY, BINOP_MUL},
1031     {"/=", ASSIGN_MODIFY, BINOP_DIV},
1032     {"%=", ASSIGN_MODIFY, BINOP_REM},
1033     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1034     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1035     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1036     {"++", INCREMENT, BINOP_END},
1037     {"--", DECREMENT, BINOP_END},
1038     {"&&", ANDAND, BINOP_END},
1039     {"||", OROR, BINOP_END},
1040     {"^^", HATHAT, BINOP_END},
1041     {"<<", LSH, BINOP_END},
1042     {">>", RSH, BINOP_END},
1043     {"==", EQUAL, BINOP_END},
1044     {"!=", NOTEQUAL, BINOP_END},
1045     {"<=", LEQ, BINOP_END},
1046     {">=", GEQ, BINOP_END},
1047     {"..", DOTDOT, BINOP_END},
1048   };
1049
1050 /* Identifier-like tokens.  */
1051 static const struct token ident_tokens[] =
1052   {
1053     {"is", IDENTITY, BINOP_END},
1054     {"!is", NOTIDENTITY, BINOP_END},
1055
1056     {"cast", CAST_KEYWORD, OP_NULL},
1057     {"const", CONST_KEYWORD, OP_NULL},
1058     {"immutable", IMMUTABLE_KEYWORD, OP_NULL},
1059     {"shared", SHARED_KEYWORD, OP_NULL},
1060     {"super", SUPER_KEYWORD, OP_NULL},
1061
1062     {"null", NULL_KEYWORD, OP_NULL},
1063     {"true", TRUE_KEYWORD, OP_NULL},
1064     {"false", FALSE_KEYWORD, OP_NULL},
1065
1066     {"init", INIT_KEYWORD, OP_NULL},
1067     {"sizeof", SIZEOF_KEYWORD, OP_NULL},
1068     {"typeof", TYPEOF_KEYWORD, OP_NULL},
1069     {"typeid", TYPEID_KEYWORD, OP_NULL},
1070
1071     {"delegate", DELEGATE_KEYWORD, OP_NULL},
1072     {"function", FUNCTION_KEYWORD, OP_NULL},
1073     {"struct", STRUCT_KEYWORD, OP_NULL},
1074     {"union", UNION_KEYWORD, OP_NULL},
1075     {"class", CLASS_KEYWORD, OP_NULL},
1076     {"interface", INTERFACE_KEYWORD, OP_NULL},
1077     {"enum", ENUM_KEYWORD, OP_NULL},
1078     {"template", TEMPLATE_KEYWORD, OP_NULL},
1079   };
1080
1081 /* This is set if a NAME token appeared at the very end of the input
1082    string, with no whitespace separating the name from the EOF.  This
1083    is used only when parsing to do field name completion.  */
1084 static int saw_name_at_eof;
1085
1086 /* This is set if the previously-returned token was a structure operator.
1087    This is used only when parsing to do field name completion.  */
1088 static int last_was_structop;
1089
1090 /* Read one token, getting characters through lexptr.  */
1091
1092 static int
1093 lex_one_token (struct parser_state *par_state)
1094 {
1095   int c;
1096   int namelen;
1097   unsigned int i;
1098   const char *tokstart;
1099   int saw_structop = last_was_structop;
1100   char *copy;
1101
1102   last_was_structop = 0;
1103
1104  retry:
1105
1106   prev_lexptr = lexptr;
1107
1108   tokstart = lexptr;
1109   /* See if it is a special token of length 3.  */
1110   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1111     if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
1112       {
1113         lexptr += 3;
1114         yylval.opcode = tokentab3[i].opcode;
1115         return tokentab3[i].token;
1116       }
1117
1118   /* See if it is a special token of length 2.  */
1119   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1120     if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
1121       {
1122         lexptr += 2;
1123         yylval.opcode = tokentab2[i].opcode;
1124         return tokentab2[i].token;
1125       }
1126
1127   switch (c = *tokstart)
1128     {
1129     case 0:
1130       /* If we're parsing for field name completion, and the previous
1131          token allows such completion, return a COMPLETE token.
1132          Otherwise, we were already scanning the original text, and
1133          we're really done.  */
1134       if (saw_name_at_eof)
1135         {
1136           saw_name_at_eof = 0;
1137           return COMPLETE;
1138         }
1139       else if (saw_structop)
1140         return COMPLETE;
1141       else
1142         return 0;
1143
1144     case ' ':
1145     case '\t':
1146     case '\n':
1147       lexptr++;
1148       goto retry;
1149
1150     case '[':
1151     case '(':
1152       paren_depth++;
1153       lexptr++;
1154       return c;
1155
1156     case ']':
1157     case ')':
1158       if (paren_depth == 0)
1159         return 0;
1160       paren_depth--;
1161       lexptr++;
1162       return c;
1163
1164     case ',':
1165       if (comma_terminates && paren_depth == 0)
1166         return 0;
1167       lexptr++;
1168       return c;
1169
1170     case '.':
1171       /* Might be a floating point number.  */
1172       if (lexptr[1] < '0' || lexptr[1] > '9')
1173         {
1174           if (parse_completion)
1175             last_was_structop = 1;
1176           goto symbol;          /* Nope, must be a symbol.  */
1177         }
1178       /* FALL THRU into number case.  */
1179
1180     case '0':
1181     case '1':
1182     case '2':
1183     case '3':
1184     case '4':
1185     case '5':
1186     case '6':
1187     case '7':
1188     case '8':
1189     case '9':
1190       {
1191         /* It's a number.  */
1192         int got_dot = 0, got_e = 0, toktype;
1193         const char *p = tokstart;
1194         int hex = input_radix > 10;
1195
1196         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1197           {
1198             p += 2;
1199             hex = 1;
1200           }
1201
1202         for (;; ++p)
1203           {
1204             /* Hex exponents start with 'p', because 'e' is a valid hex
1205                digit and thus does not indicate a floating point number
1206                when the radix is hex.  */
1207             if ((!hex && !got_e && tolower (p[0]) == 'e')
1208                 || (hex && !got_e && tolower (p[0] == 'p')))
1209               got_dot = got_e = 1;
1210             /* A '.' always indicates a decimal floating point number
1211                regardless of the radix.  If we have a '..' then its the
1212                end of the number and the beginning of a slice.  */
1213             else if (!got_dot && (p[0] == '.' && p[1] != '.'))
1214                 got_dot = 1;
1215             /* This is the sign of the exponent, not the end of the number.  */
1216             else if (got_e && (tolower (p[-1]) == 'e' || tolower (p[-1]) == 'p')
1217                      && (*p == '-' || *p == '+'))
1218               continue;
1219             /* We will take any letters or digits, ignoring any embedded '_'.
1220                parse_number will complain if past the radix, or if L or U are
1221                not final.  */
1222             else if ((*p < '0' || *p > '9') && (*p != '_')
1223                      && ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z')))
1224               break;
1225           }
1226
1227         toktype = parse_number (par_state, tokstart, p - tokstart,
1228                                 got_dot|got_e, &yylval);
1229         if (toktype == ERROR)
1230           {
1231             char *err_copy = (char *) alloca (p - tokstart + 1);
1232
1233             memcpy (err_copy, tokstart, p - tokstart);
1234             err_copy[p - tokstart] = 0;
1235             error (_("Invalid number \"%s\"."), err_copy);
1236           }
1237         lexptr = p;
1238         return toktype;
1239       }
1240
1241     case '@':
1242       {
1243         const char *p = &tokstart[1];
1244         size_t len = strlen ("entry");
1245
1246         while (isspace (*p))
1247           p++;
1248         if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
1249             && p[len] != '_')
1250           {
1251             lexptr = &p[len];
1252             return ENTRY;
1253           }
1254       }
1255       /* FALLTHRU */
1256     case '+':
1257     case '-':
1258     case '*':
1259     case '/':
1260     case '%':
1261     case '|':
1262     case '&':
1263     case '^':
1264     case '~':
1265     case '!':
1266     case '<':
1267     case '>':
1268     case '?':
1269     case ':':
1270     case '=':
1271     case '{':
1272     case '}':
1273     symbol:
1274       lexptr++;
1275       return c;
1276
1277     case '\'':
1278     case '"':
1279     case '`':
1280       {
1281         int host_len;
1282         int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
1283                                            &host_len);
1284         if (result == CHARACTER_LITERAL)
1285           {
1286             if (host_len == 0)
1287               error (_("Empty character constant."));
1288             else if (host_len > 2 && c == '\'')
1289               {
1290                 ++tokstart;
1291                 namelen = lexptr - tokstart - 1;
1292                 goto tryname;
1293               }
1294             else if (host_len > 1)
1295               error (_("Invalid character constant."));
1296           }
1297         return result;
1298       }
1299     }
1300
1301   if (!(c == '_' || c == '$'
1302         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1303     /* We must have come across a bad character (e.g. ';').  */
1304     error (_("Invalid character '%c' in expression"), c);
1305
1306   /* It's a name.  See how long it is.  */
1307   namelen = 0;
1308   for (c = tokstart[namelen];
1309        (c == '_' || c == '$' || (c >= '0' && c <= '9')
1310         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
1311     c = tokstart[++namelen];
1312
1313   /* The token "if" terminates the expression and is NOT
1314      removed from the input stream.  */
1315   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1316     return 0;
1317
1318   /* For the same reason (breakpoint conditions), "thread N"
1319      terminates the expression.  "thread" could be an identifier, but
1320      an identifier is never followed by a number without intervening
1321      punctuation.  "task" is similar.  Handle abbreviations of these,
1322      similarly to breakpoint.c:find_condition_and_thread.  */
1323   if (namelen >= 1
1324       && (strncmp (tokstart, "thread", namelen) == 0
1325           || strncmp (tokstart, "task", namelen) == 0)
1326       && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
1327     {
1328       const char *p = tokstart + namelen + 1;
1329
1330       while (*p == ' ' || *p == '\t')
1331         p++;
1332       if (*p >= '0' && *p <= '9')
1333         return 0;
1334     }
1335
1336   lexptr += namelen;
1337
1338  tryname:
1339
1340   yylval.sval.ptr = tokstart;
1341   yylval.sval.length = namelen;
1342
1343   /* Catch specific keywords.  */
1344   copy = copy_name (yylval.sval);
1345   for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
1346     if (strcmp (copy, ident_tokens[i].oper) == 0)
1347       {
1348         /* It is ok to always set this, even though we don't always
1349            strictly need to.  */
1350         yylval.opcode = ident_tokens[i].opcode;
1351         return ident_tokens[i].token;
1352       }
1353
1354   if (*tokstart == '$')
1355     return DOLLAR_VARIABLE;
1356
1357   yylval.tsym.type
1358     = language_lookup_primitive_type (parse_language (par_state),
1359                                       parse_gdbarch (par_state), copy);
1360   if (yylval.tsym.type != NULL)
1361     return TYPENAME;
1362
1363   /* Input names that aren't symbols but ARE valid hex numbers,
1364      when the input radix permits them, can be names or numbers
1365      depending on the parse.  Note we support radixes > 16 here.  */
1366   if ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1367       || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))
1368     {
1369       YYSTYPE newlval;  /* Its value is ignored.  */
1370       int hextype = parse_number (par_state, tokstart, namelen, 0, &newlval);
1371       if (hextype == INTEGER_LITERAL)
1372         return NAME_OR_INT;
1373     }
1374
1375   if (parse_completion && *lexptr == '\0')
1376     saw_name_at_eof = 1;
1377
1378   return IDENTIFIER;
1379 }
1380
1381 /* An object of this type is pushed on a FIFO by the "outer" lexer.  */
1382 typedef struct
1383 {
1384   int token;
1385   YYSTYPE value;
1386 } token_and_value;
1387
1388 DEF_VEC_O (token_and_value);
1389
1390 /* A FIFO of tokens that have been read but not yet returned to the
1391    parser.  */
1392 static VEC (token_and_value) *token_fifo;
1393
1394 /* Non-zero if the lexer should return tokens from the FIFO.  */
1395 static int popping;
1396
1397 /* Temporary storage for yylex; this holds symbol names as they are
1398    built up.  */
1399 static struct obstack name_obstack;
1400
1401 /* Classify an IDENTIFIER token.  The contents of the token are in `yylval'.
1402    Updates yylval and returns the new token type.  BLOCK is the block
1403    in which lookups start; this can be NULL to mean the global scope.  */
1404
1405 static int
1406 classify_name (struct parser_state *par_state, const struct block *block)
1407 {
1408   struct block_symbol sym;
1409   char *copy;
1410   struct field_of_this_result is_a_field_of_this;
1411
1412   copy = copy_name (yylval.sval);
1413
1414   sym = lookup_symbol (copy, block, VAR_DOMAIN, &is_a_field_of_this);
1415   if (sym.symbol && SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF)
1416     {
1417       yylval.tsym.type = SYMBOL_TYPE (sym.symbol);
1418       return TYPENAME;
1419     }
1420   else if (sym.symbol == NULL)
1421     {
1422       /* Look-up first for a module name, then a type.  */
1423       sym = lookup_symbol (copy, block, MODULE_DOMAIN, NULL);
1424       if (sym.symbol == NULL)
1425         sym = lookup_symbol (copy, block, STRUCT_DOMAIN, NULL);
1426
1427       if (sym.symbol != NULL)
1428         {
1429           yylval.tsym.type = SYMBOL_TYPE (sym.symbol);
1430           return TYPENAME;
1431         }
1432
1433       return UNKNOWN_NAME;
1434     }
1435
1436   return IDENTIFIER;
1437 }
1438
1439 /* Like classify_name, but used by the inner loop of the lexer, when a
1440    name might have already been seen.  CONTEXT is the context type, or
1441    NULL if this is the first component of a name.  */
1442
1443 static int
1444 classify_inner_name (struct parser_state *par_state,
1445                      const struct block *block, struct type *context)
1446 {
1447   struct type *type;
1448   char *copy;
1449
1450   if (context == NULL)
1451     return classify_name (par_state, block);
1452
1453   type = check_typedef (context);
1454   if (!type_aggregate_p (type))
1455     return ERROR;
1456
1457   copy = copy_name (yylval.ssym.stoken);
1458   yylval.ssym.sym = d_lookup_nested_symbol (type, copy, block);
1459
1460   if (yylval.ssym.sym.symbol == NULL)
1461     return ERROR;
1462
1463   if (SYMBOL_CLASS (yylval.ssym.sym.symbol) == LOC_TYPEDEF)
1464     {
1465       yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
1466       return TYPENAME;
1467     }
1468
1469   return IDENTIFIER;
1470 }
1471
1472 /* The outer level of a two-level lexer.  This calls the inner lexer
1473    to return tokens.  It then either returns these tokens, or
1474    aggregates them into a larger token.  This lets us work around a
1475    problem in our parsing approach, where the parser could not
1476    distinguish between qualified names and qualified types at the
1477    right point.  */
1478
1479 static int
1480 yylex (void)
1481 {
1482   token_and_value current;
1483   int last_was_dot;
1484   struct type *context_type = NULL;
1485   int last_to_examine, next_to_examine, checkpoint;
1486   const struct block *search_block;
1487
1488   if (popping && !VEC_empty (token_and_value, token_fifo))
1489     goto do_pop;
1490   popping = 0;
1491
1492   /* Read the first token and decide what to do.  */
1493   current.token = lex_one_token (pstate);
1494   if (current.token != IDENTIFIER && current.token != '.')
1495     return current.token;
1496
1497   /* Read any sequence of alternating "." and identifier tokens into
1498      the token FIFO.  */
1499   current.value = yylval;
1500   VEC_safe_push (token_and_value, token_fifo, &current);
1501   last_was_dot = current.token == '.';
1502
1503   while (1)
1504     {
1505       current.token = lex_one_token (pstate);
1506       current.value = yylval;
1507       VEC_safe_push (token_and_value, token_fifo, &current);
1508
1509       if ((last_was_dot && current.token != IDENTIFIER)
1510           || (!last_was_dot && current.token != '.'))
1511         break;
1512
1513       last_was_dot = !last_was_dot;
1514     }
1515   popping = 1;
1516
1517   /* We always read one extra token, so compute the number of tokens
1518      to examine accordingly.  */
1519   last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
1520   next_to_examine = 0;
1521
1522   current = *VEC_index (token_and_value, token_fifo, next_to_examine);
1523   ++next_to_examine;
1524
1525   /* If we are not dealing with a typename, now is the time to find out.  */
1526   if (current.token == IDENTIFIER)
1527     {
1528       yylval = current.value;
1529       current.token = classify_name (pstate, expression_context_block);
1530       current.value = yylval;
1531     }
1532
1533   /* If the IDENTIFIER is not known, it could be a package symbol,
1534      first try building up a name until we find the qualified module.  */
1535   if (current.token == UNKNOWN_NAME)
1536     {
1537       obstack_free (&name_obstack, obstack_base (&name_obstack));
1538       obstack_grow (&name_obstack, current.value.sval.ptr,
1539                     current.value.sval.length);
1540
1541       last_was_dot = 0;
1542
1543       while (next_to_examine <= last_to_examine)
1544         {
1545           token_and_value *next;
1546
1547           next = VEC_index (token_and_value, token_fifo, next_to_examine);
1548           ++next_to_examine;
1549
1550           if (next->token == IDENTIFIER && last_was_dot)
1551             {
1552               /* Update the partial name we are constructing.  */
1553               obstack_grow_str (&name_obstack, ".");
1554               obstack_grow (&name_obstack, next->value.sval.ptr,
1555                             next->value.sval.length);
1556
1557               yylval.sval.ptr = obstack_base (&name_obstack);
1558               yylval.sval.length = obstack_object_size (&name_obstack);
1559
1560               current.token = classify_name (pstate, expression_context_block);
1561               current.value = yylval;
1562
1563               /* We keep going until we find a TYPENAME.  */
1564               if (current.token == TYPENAME)
1565                 {
1566                   /* Install it as the first token in the FIFO.  */
1567                   VEC_replace (token_and_value, token_fifo, 0, &current);
1568                   VEC_block_remove (token_and_value, token_fifo, 1,
1569                                     next_to_examine - 1);
1570                   break;
1571                 }
1572             }
1573           else if (next->token == '.' && !last_was_dot)
1574             last_was_dot = 1;
1575           else
1576             {
1577               /* We've reached the end of the name.  */
1578               break;
1579             }
1580         }
1581
1582       /* Reset our current token back to the start, if we found nothing
1583          this means that we will just jump to do pop.  */
1584       current = *VEC_index (token_and_value, token_fifo, 0);
1585       next_to_examine = 1;
1586     }
1587   if (current.token != TYPENAME && current.token != '.')
1588     goto do_pop;
1589
1590   obstack_free (&name_obstack, obstack_base (&name_obstack));
1591   checkpoint = 0;
1592   if (current.token == '.')
1593     search_block = NULL;
1594   else
1595     {
1596       gdb_assert (current.token == TYPENAME);
1597       search_block = expression_context_block;
1598       obstack_grow (&name_obstack, current.value.sval.ptr,
1599                     current.value.sval.length);
1600       context_type = current.value.tsym.type;
1601       checkpoint = 1;
1602     }
1603
1604   last_was_dot = current.token == '.';
1605
1606   while (next_to_examine <= last_to_examine)
1607     {
1608       token_and_value *next;
1609
1610       next = VEC_index (token_and_value, token_fifo, next_to_examine);
1611       ++next_to_examine;
1612
1613       if (next->token == IDENTIFIER && last_was_dot)
1614         {
1615           int classification;
1616
1617           yylval = next->value;
1618           classification = classify_inner_name (pstate, search_block,
1619                                                 context_type);
1620           /* We keep going until we either run out of names, or until
1621              we have a qualified name which is not a type.  */
1622           if (classification != TYPENAME && classification != IDENTIFIER)
1623             break;
1624
1625           /* Accept up to this token.  */
1626           checkpoint = next_to_examine;
1627
1628           /* Update the partial name we are constructing.  */
1629           if (context_type != NULL)
1630             {
1631               /* We don't want to put a leading "." into the name.  */
1632               obstack_grow_str (&name_obstack, ".");
1633             }
1634           obstack_grow (&name_obstack, next->value.sval.ptr,
1635                         next->value.sval.length);
1636
1637           yylval.sval.ptr = obstack_base (&name_obstack);
1638           yylval.sval.length = obstack_object_size (&name_obstack);
1639           current.value = yylval;
1640           current.token = classification;
1641
1642           last_was_dot = 0;
1643
1644           if (classification == IDENTIFIER)
1645             break;
1646
1647           context_type = yylval.tsym.type;
1648         }
1649       else if (next->token == '.' && !last_was_dot)
1650         last_was_dot = 1;
1651       else
1652         {
1653           /* We've reached the end of the name.  */
1654           break;
1655         }
1656     }
1657
1658   /* If we have a replacement token, install it as the first token in
1659      the FIFO, and delete the other constituent tokens.  */
1660   if (checkpoint > 0)
1661     {
1662       VEC_replace (token_and_value, token_fifo, 0, &current);
1663       if (checkpoint > 1)
1664         VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
1665     }
1666
1667  do_pop:
1668   current = *VEC_index (token_and_value, token_fifo, 0);
1669   VEC_ordered_remove (token_and_value, token_fifo, 0);
1670   yylval = current.value;
1671   return current.token;
1672 }
1673
1674 int
1675 d_parse (struct parser_state *par_state)
1676 {
1677   int result;
1678   struct cleanup *back_to;
1679
1680   /* Setting up the parser state.  */
1681   gdb_assert (par_state != NULL);
1682   pstate = par_state;
1683
1684   back_to = make_cleanup (null_cleanup, NULL);
1685
1686   make_cleanup_restore_integer (&yydebug);
1687   make_cleanup_clear_parser_state (&pstate);
1688   yydebug = parser_debug;
1689
1690   /* Initialize some state used by the lexer.  */
1691   last_was_structop = 0;
1692   saw_name_at_eof = 0;
1693
1694   VEC_free (token_and_value, token_fifo);
1695   popping = 0;
1696   obstack_init (&name_obstack);
1697   make_cleanup_obstack_free (&name_obstack);
1698
1699   result = yyparse ();
1700   do_cleanups (back_to);
1701   return result;
1702 }
1703
1704 void
1705 yyerror (char *msg)
1706 {
1707   if (prev_lexptr)
1708     lexptr = prev_lexptr;
1709
1710   error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
1711 }
1712