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