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