Automatic date update in version.in
[binutils-gdb.git] / gdb / d-exp.y
blob027a8fa206dab145a84d99cf116bbbdb7b792341
1 /* YACC parser for D expressions, for GDB.
3 Copyright (C) 2014-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* This file is derived from c-exp.y, jv-exp.y. */
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.
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. */
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 #include "expop.h"
57 #define parse_type(ps) builtin_type (ps->gdbarch ())
58 #define parse_d_type(ps) builtin_d_type (ps->gdbarch ())
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
61 etc). */
62 #define GDB_YY_REMAP_PREFIX d_
63 #include "yy-remap.h"
65 /* The state of the parser, used internally when we are parsing the
66 expression. */
68 static struct parser_state *pstate = NULL;
70 /* The current type stack. */
71 static struct type_stack *type_stack;
73 int yyparse (void);
75 static int yylex (void);
77 static void yyerror (const char *);
79 static int type_aggregate_p (struct type *);
81 using namespace expr;
85 /* Although the yacc "value" of an expression is not used,
86 since the result is stored in the structure being created,
87 other node types do have values. */
89 %union
91 struct {
92 LONGEST val;
93 struct type *type;
94 } typed_val_int;
95 struct {
96 gdb_byte val[16];
97 struct type *type;
98 } typed_val_float;
99 struct symbol *sym;
100 struct type *tval;
101 struct typed_stoken tsval;
102 struct stoken sval;
103 struct ttype tsym;
104 struct symtoken ssym;
105 int ival;
106 int voidval;
107 enum exp_opcode opcode;
108 struct stoken_vector svec;
112 /* YYSTYPE gets defined by %union */
113 static int parse_number (struct parser_state *, const char *,
114 int, int, YYSTYPE *);
117 %token <sval> IDENTIFIER UNKNOWN_NAME
118 %token <tsym> TYPENAME
119 %token <voidval> COMPLETE
121 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
122 but which would parse as a valid number in the current input radix.
123 E.g. "c" when input_radix==16. Depending on the parse, it will be
124 turned into a name or into a number. */
126 %token <sval> NAME_OR_INT
128 %token <typed_val_int> INTEGER_LITERAL
129 %token <typed_val_float> FLOAT_LITERAL
130 %token <tsval> CHARACTER_LITERAL
131 %token <tsval> STRING_LITERAL
133 %type <svec> StringExp
134 %type <tval> BasicType TypeExp
135 %type <sval> IdentifierExp
136 %type <ival> ArrayLiteral
138 %token ENTRY
139 %token ERROR
141 /* Keywords that have a constant value. */
142 %token TRUE_KEYWORD FALSE_KEYWORD NULL_KEYWORD
143 /* Class 'super' accessor. */
144 %token SUPER_KEYWORD
145 /* Properties. */
146 %token CAST_KEYWORD SIZEOF_KEYWORD
147 %token TYPEOF_KEYWORD TYPEID_KEYWORD
148 %token INIT_KEYWORD
149 /* Comparison keywords. */
150 /* Type storage classes. */
151 %token IMMUTABLE_KEYWORD CONST_KEYWORD SHARED_KEYWORD
152 /* Non-scalar type keywords. */
153 %token STRUCT_KEYWORD UNION_KEYWORD
154 %token CLASS_KEYWORD INTERFACE_KEYWORD
155 %token ENUM_KEYWORD TEMPLATE_KEYWORD
156 %token DELEGATE_KEYWORD FUNCTION_KEYWORD
158 %token <sval> DOLLAR_VARIABLE
160 %token <opcode> ASSIGN_MODIFY
162 %left ','
163 %right '=' ASSIGN_MODIFY
164 %right '?'
165 %left OROR
166 %left ANDAND
167 %left '|'
168 %left '^'
169 %left '&'
170 %left EQUAL NOTEQUAL '<' '>' LEQ GEQ
171 %right LSH RSH
172 %left '+' '-'
173 %left '*' '/' '%'
174 %right HATHAT
175 %left IDENTITY NOTIDENTITY
176 %right INCREMENT DECREMENT
177 %right '.' '[' '('
178 %token DOTDOT
183 start :
184 Expression
185 | TypeExp
188 /* Expressions, including the comma operator. */
190 Expression:
191 CommaExpression
194 CommaExpression:
195 AssignExpression
196 | AssignExpression ',' CommaExpression
197 { pstate->wrap2<comma_operation> (); }
200 AssignExpression:
201 ConditionalExpression
202 | ConditionalExpression '=' AssignExpression
203 { pstate->wrap2<assign_operation> (); }
204 | ConditionalExpression ASSIGN_MODIFY AssignExpression
206 operation_up rhs = pstate->pop ();
207 operation_up lhs = pstate->pop ();
208 pstate->push_new<assign_modify_operation>
209 ($2, std::move (lhs), std::move (rhs));
213 ConditionalExpression:
214 OrOrExpression
215 | OrOrExpression '?' Expression ':' ConditionalExpression
217 operation_up last = pstate->pop ();
218 operation_up mid = pstate->pop ();
219 operation_up first = pstate->pop ();
220 pstate->push_new<ternop_cond_operation>
221 (std::move (first), std::move (mid),
222 std::move (last));
226 OrOrExpression:
227 AndAndExpression
228 | OrOrExpression OROR AndAndExpression
229 { pstate->wrap2<logical_or_operation> (); }
232 AndAndExpression:
233 OrExpression
234 | AndAndExpression ANDAND OrExpression
235 { pstate->wrap2<logical_and_operation> (); }
238 OrExpression:
239 XorExpression
240 | OrExpression '|' XorExpression
241 { pstate->wrap2<bitwise_ior_operation> (); }
244 XorExpression:
245 AndExpression
246 | XorExpression '^' AndExpression
247 { pstate->wrap2<bitwise_xor_operation> (); }
250 AndExpression:
251 CmpExpression
252 | AndExpression '&' CmpExpression
253 { pstate->wrap2<bitwise_and_operation> (); }
256 CmpExpression:
257 ShiftExpression
258 | EqualExpression
259 | IdentityExpression
260 | RelExpression
263 EqualExpression:
264 ShiftExpression EQUAL ShiftExpression
265 { pstate->wrap2<equal_operation> (); }
266 | ShiftExpression NOTEQUAL ShiftExpression
267 { pstate->wrap2<notequal_operation> (); }
270 IdentityExpression:
271 ShiftExpression IDENTITY ShiftExpression
272 { pstate->wrap2<equal_operation> (); }
273 | ShiftExpression NOTIDENTITY ShiftExpression
274 { pstate->wrap2<notequal_operation> (); }
277 RelExpression:
278 ShiftExpression '<' ShiftExpression
279 { pstate->wrap2<less_operation> (); }
280 | ShiftExpression LEQ ShiftExpression
281 { pstate->wrap2<leq_operation> (); }
282 | ShiftExpression '>' ShiftExpression
283 { pstate->wrap2<gtr_operation> (); }
284 | ShiftExpression GEQ ShiftExpression
285 { pstate->wrap2<geq_operation> (); }
288 ShiftExpression:
289 AddExpression
290 | ShiftExpression LSH AddExpression
291 { pstate->wrap2<lsh_operation> (); }
292 | ShiftExpression RSH AddExpression
293 { pstate->wrap2<rsh_operation> (); }
296 AddExpression:
297 MulExpression
298 | AddExpression '+' MulExpression
299 { pstate->wrap2<add_operation> (); }
300 | AddExpression '-' MulExpression
301 { pstate->wrap2<sub_operation> (); }
302 | AddExpression '~' MulExpression
303 { pstate->wrap2<concat_operation> (); }
306 MulExpression:
307 UnaryExpression
308 | MulExpression '*' UnaryExpression
309 { pstate->wrap2<mul_operation> (); }
310 | MulExpression '/' UnaryExpression
311 { pstate->wrap2<div_operation> (); }
312 | MulExpression '%' UnaryExpression
313 { pstate->wrap2<rem_operation> (); }
315 UnaryExpression:
316 '&' UnaryExpression
317 { pstate->wrap<unop_addr_operation> (); }
318 | INCREMENT UnaryExpression
319 { pstate->wrap<preinc_operation> (); }
320 | DECREMENT UnaryExpression
321 { pstate->wrap<predec_operation> (); }
322 | '*' UnaryExpression
323 { pstate->wrap<unop_ind_operation> (); }
324 | '-' UnaryExpression
325 { pstate->wrap<unary_neg_operation> (); }
326 | '+' UnaryExpression
327 { pstate->wrap<unary_plus_operation> (); }
328 | '!' UnaryExpression
329 { pstate->wrap<unary_logical_not_operation> (); }
330 | '~' UnaryExpression
331 { pstate->wrap<unary_complement_operation> (); }
332 | TypeExp '.' SIZEOF_KEYWORD
333 { pstate->wrap<unop_sizeof_operation> (); }
334 | CastExpression
335 | PowExpression
338 CastExpression:
339 CAST_KEYWORD '(' TypeExp ')' UnaryExpression
340 { pstate->wrap2<unop_cast_type_operation> (); }
341 /* C style cast is illegal D, but is still recognised in
342 the grammar, so we keep this around for convenience. */
343 | '(' TypeExp ')' UnaryExpression
344 { pstate->wrap2<unop_cast_type_operation> (); }
347 PowExpression:
348 PostfixExpression
349 | PostfixExpression HATHAT UnaryExpression
350 { pstate->wrap2<exp_operation> (); }
353 PostfixExpression:
354 PrimaryExpression
355 | PostfixExpression '.' COMPLETE
357 structop_base_operation *op
358 = new structop_ptr_operation (pstate->pop (), "");
359 pstate->mark_struct_expression (op);
360 pstate->push (operation_up (op));
362 | PostfixExpression '.' IDENTIFIER
364 pstate->push_new<structop_operation>
365 (pstate->pop (), copy_name ($3));
367 | PostfixExpression '.' IDENTIFIER COMPLETE
369 structop_base_operation *op
370 = new structop_operation (pstate->pop (), copy_name ($3));
371 pstate->mark_struct_expression (op);
372 pstate->push (operation_up (op));
374 | PostfixExpression '.' SIZEOF_KEYWORD
375 { pstate->wrap<unop_sizeof_operation> (); }
376 | PostfixExpression INCREMENT
377 { pstate->wrap<postinc_operation> (); }
378 | PostfixExpression DECREMENT
379 { pstate->wrap<postdec_operation> (); }
380 | CallExpression
381 | IndexExpression
382 | SliceExpression
385 ArgumentList:
386 AssignExpression
387 { pstate->arglist_len = 1; }
388 | ArgumentList ',' AssignExpression
389 { pstate->arglist_len++; }
392 ArgumentList_opt:
393 /* EMPTY */
394 { pstate->arglist_len = 0; }
395 | ArgumentList
398 CallExpression:
399 PostfixExpression '('
400 { pstate->start_arglist (); }
401 ArgumentList_opt ')'
403 std::vector<operation_up> args
404 = pstate->pop_vector (pstate->end_arglist ());
405 pstate->push_new<funcall_operation>
406 (pstate->pop (), std::move (args));
410 IndexExpression:
411 PostfixExpression '[' ArgumentList ']'
412 { if (pstate->arglist_len > 0)
414 std::vector<operation_up> args
415 = pstate->pop_vector (pstate->arglist_len);
416 pstate->push_new<multi_subscript_operation>
417 (pstate->pop (), std::move (args));
419 else
420 pstate->wrap2<subscript_operation> ();
424 SliceExpression:
425 PostfixExpression '[' ']'
426 { /* Do nothing. */ }
427 | PostfixExpression '[' AssignExpression DOTDOT AssignExpression ']'
429 operation_up last = pstate->pop ();
430 operation_up mid = pstate->pop ();
431 operation_up first = pstate->pop ();
432 pstate->push_new<ternop_slice_operation>
433 (std::move (first), std::move (mid),
434 std::move (last));
438 PrimaryExpression:
439 '(' Expression ')'
440 { /* Do nothing. */ }
441 | IdentifierExp
442 { struct bound_minimal_symbol msymbol;
443 std::string copy = copy_name ($1);
444 struct field_of_this_result is_a_field_of_this;
445 struct block_symbol sym;
447 /* Handle VAR, which could be local or global. */
448 sym = lookup_symbol (copy.c_str (),
449 pstate->expression_context_block,
450 VAR_DOMAIN, &is_a_field_of_this);
451 if (sym.symbol && sym.symbol->aclass () != LOC_TYPEDEF)
453 if (symbol_read_needs_frame (sym.symbol))
454 pstate->block_tracker->update (sym);
455 pstate->push_new<var_value_operation> (sym);
457 else if (is_a_field_of_this.type != NULL)
459 /* It hangs off of `this'. Must not inadvertently convert from a
460 method call to data ref. */
461 pstate->block_tracker->update (sym);
462 operation_up thisop
463 = make_operation<op_this_operation> ();
464 pstate->push_new<structop_ptr_operation>
465 (std::move (thisop), std::move (copy));
467 else
469 /* Lookup foreign name in global static symbols. */
470 msymbol = lookup_bound_minimal_symbol (copy.c_str ());
471 if (msymbol.minsym != NULL)
472 pstate->push_new<var_msym_value_operation> (msymbol);
473 else if (!have_full_symbols () && !have_partial_symbols ())
474 error (_("No symbol table is loaded. Use the \"file\" command"));
475 else
476 error (_("No symbol \"%s\" in current context."),
477 copy.c_str ());
480 | TypeExp '.' IdentifierExp
481 { struct type *type = check_typedef ($1);
483 /* Check if the qualified name is in the global
484 context. However if the symbol has not already
485 been resolved, it's not likely to be found. */
486 if (type->code () == TYPE_CODE_MODULE)
488 struct block_symbol sym;
489 const char *type_name = TYPE_SAFE_NAME (type);
490 int type_name_len = strlen (type_name);
491 std::string name
492 = string_printf ("%.*s.%.*s",
493 type_name_len, type_name,
494 $3.length, $3.ptr);
496 sym =
497 lookup_symbol (name.c_str (),
498 (const struct block *) NULL,
499 VAR_DOMAIN, NULL);
500 pstate->push_symbol (name.c_str (), sym);
502 else
504 /* Check if the qualified name resolves as a member
505 of an aggregate or an enum type. */
506 if (!type_aggregate_p (type))
507 error (_("`%s' is not defined as an aggregate type."),
508 TYPE_SAFE_NAME (type));
510 pstate->push_new<scope_operation>
511 (type, copy_name ($3));
514 | DOLLAR_VARIABLE
515 { pstate->push_dollar ($1); }
516 | NAME_OR_INT
517 { YYSTYPE val;
518 parse_number (pstate, $1.ptr, $1.length, 0, &val);
519 pstate->push_new<long_const_operation>
520 (val.typed_val_int.type, val.typed_val_int.val); }
521 | NULL_KEYWORD
522 { struct type *type = parse_d_type (pstate)->builtin_void;
523 type = lookup_pointer_type (type);
524 pstate->push_new<long_const_operation> (type, 0); }
525 | TRUE_KEYWORD
526 { pstate->push_new<bool_operation> (true); }
527 | FALSE_KEYWORD
528 { pstate->push_new<bool_operation> (false); }
529 | INTEGER_LITERAL
530 { pstate->push_new<long_const_operation> ($1.type, $1.val); }
531 | FLOAT_LITERAL
533 float_data data;
534 std::copy (std::begin ($1.val), std::end ($1.val),
535 std::begin (data));
536 pstate->push_new<float_const_operation> ($1.type, data);
538 | CHARACTER_LITERAL
539 { struct stoken_vector vec;
540 vec.len = 1;
541 vec.tokens = &$1;
542 pstate->push_c_string (0, &vec); }
543 | StringExp
544 { int i;
545 pstate->push_c_string (0, &$1);
546 for (i = 0; i < $1.len; ++i)
547 free ($1.tokens[i].ptr);
548 free ($1.tokens); }
549 | ArrayLiteral
551 std::vector<operation_up> args
552 = pstate->pop_vector ($1);
553 pstate->push_new<array_operation>
554 (0, $1 - 1, std::move (args));
556 | TYPEOF_KEYWORD '(' Expression ')'
557 { pstate->wrap<typeof_operation> (); }
560 ArrayLiteral:
561 '[' ArgumentList_opt ']'
562 { $$ = pstate->arglist_len; }
565 IdentifierExp:
566 IDENTIFIER
569 StringExp:
570 STRING_LITERAL
571 { /* We copy the string here, and not in the
572 lexer, to guarantee that we do not leak a
573 string. Note that we follow the
574 NUL-termination convention of the
575 lexer. */
576 struct typed_stoken *vec = XNEW (struct typed_stoken);
577 $$.len = 1;
578 $$.tokens = vec;
580 vec->type = $1.type;
581 vec->length = $1.length;
582 vec->ptr = (char *) malloc ($1.length + 1);
583 memcpy (vec->ptr, $1.ptr, $1.length + 1);
585 | StringExp STRING_LITERAL
586 { /* Note that we NUL-terminate here, but just
587 for convenience. */
588 char *p;
589 ++$$.len;
590 $$.tokens
591 = XRESIZEVEC (struct typed_stoken, $$.tokens, $$.len);
593 p = (char *) malloc ($2.length + 1);
594 memcpy (p, $2.ptr, $2.length + 1);
596 $$.tokens[$$.len - 1].type = $2.type;
597 $$.tokens[$$.len - 1].length = $2.length;
598 $$.tokens[$$.len - 1].ptr = p;
602 TypeExp:
603 '(' TypeExp ')'
604 { /* Do nothing. */ }
605 | BasicType
606 { pstate->push_new<type_operation> ($1); }
607 | BasicType BasicType2
608 { $$ = type_stack->follow_types ($1);
609 pstate->push_new<type_operation> ($$);
613 BasicType2:
615 { type_stack->push (tp_pointer); }
616 | '*' BasicType2
617 { type_stack->push (tp_pointer); }
618 | '[' INTEGER_LITERAL ']'
619 { type_stack->push ($2.val);
620 type_stack->push (tp_array); }
621 | '[' INTEGER_LITERAL ']' BasicType2
622 { type_stack->push ($2.val);
623 type_stack->push (tp_array); }
626 BasicType:
627 TYPENAME
628 { $$ = $1.type; }
633 /* Return true if the type is aggregate-like. */
635 static int
636 type_aggregate_p (struct type *type)
638 return (type->code () == TYPE_CODE_STRUCT
639 || type->code () == TYPE_CODE_UNION
640 || type->code () == TYPE_CODE_MODULE
641 || (type->code () == TYPE_CODE_ENUM
642 && type->is_declared_class ()));
645 /* Take care of parsing a number (anything that starts with a digit).
646 Set yylval and return the token type; update lexptr.
647 LEN is the number of characters in it. */
649 /*** Needs some error checking for the float case ***/
651 static int
652 parse_number (struct parser_state *ps, const char *p,
653 int len, int parsed_float, YYSTYPE *putithere)
655 ULONGEST n = 0;
656 ULONGEST prevn = 0;
657 ULONGEST un;
659 int i = 0;
660 int c;
661 int base = input_radix;
662 int unsigned_p = 0;
663 int long_p = 0;
665 /* We have found a "L" or "U" suffix. */
666 int found_suffix = 0;
668 ULONGEST high_bit;
669 struct type *signed_type;
670 struct type *unsigned_type;
672 if (parsed_float)
674 char *s, *sp;
676 /* Strip out all embedded '_' before passing to parse_float. */
677 s = (char *) alloca (len + 1);
678 sp = s;
679 while (len-- > 0)
681 if (*p != '_')
682 *sp++ = *p;
683 p++;
685 *sp = '\0';
686 len = strlen (s);
688 /* Check suffix for `i' , `fi' or `li' (idouble, ifloat or ireal). */
689 if (len >= 1 && tolower (s[len - 1]) == 'i')
691 if (len >= 2 && tolower (s[len - 2]) == 'f')
693 putithere->typed_val_float.type
694 = parse_d_type (ps)->builtin_ifloat;
695 len -= 2;
697 else if (len >= 2 && tolower (s[len - 2]) == 'l')
699 putithere->typed_val_float.type
700 = parse_d_type (ps)->builtin_ireal;
701 len -= 2;
703 else
705 putithere->typed_val_float.type
706 = parse_d_type (ps)->builtin_idouble;
707 len -= 1;
710 /* Check suffix for `f' or `l'' (float or real). */
711 else if (len >= 1 && tolower (s[len - 1]) == 'f')
713 putithere->typed_val_float.type
714 = parse_d_type (ps)->builtin_float;
715 len -= 1;
717 else if (len >= 1 && tolower (s[len - 1]) == 'l')
719 putithere->typed_val_float.type
720 = parse_d_type (ps)->builtin_real;
721 len -= 1;
723 /* Default type if no suffix. */
724 else
726 putithere->typed_val_float.type
727 = parse_d_type (ps)->builtin_double;
730 if (!parse_float (s, len,
731 putithere->typed_val_float.type,
732 putithere->typed_val_float.val))
733 return ERROR;
735 return FLOAT_LITERAL;
738 /* Handle base-switching prefixes 0x, 0b, 0 */
739 if (p[0] == '0')
740 switch (p[1])
742 case 'x':
743 case 'X':
744 if (len >= 3)
746 p += 2;
747 base = 16;
748 len -= 2;
750 break;
752 case 'b':
753 case 'B':
754 if (len >= 3)
756 p += 2;
757 base = 2;
758 len -= 2;
760 break;
762 default:
763 base = 8;
764 break;
767 while (len-- > 0)
769 c = *p++;
770 if (c == '_')
771 continue; /* Ignore embedded '_'. */
772 if (c >= 'A' && c <= 'Z')
773 c += 'a' - 'A';
774 if (c != 'l' && c != 'u')
775 n *= base;
776 if (c >= '0' && c <= '9')
778 if (found_suffix)
779 return ERROR;
780 n += i = c - '0';
782 else
784 if (base > 10 && c >= 'a' && c <= 'f')
786 if (found_suffix)
787 return ERROR;
788 n += i = c - 'a' + 10;
790 else if (c == 'l' && long_p == 0)
792 long_p = 1;
793 found_suffix = 1;
795 else if (c == 'u' && unsigned_p == 0)
797 unsigned_p = 1;
798 found_suffix = 1;
800 else
801 return ERROR; /* Char not a digit */
803 if (i >= base)
804 return ERROR; /* Invalid digit in this base. */
805 /* Portably test for integer overflow. */
806 if (c != 'l' && c != 'u')
808 ULONGEST n2 = prevn * base;
809 if ((n2 / base != prevn) || (n2 + i < prevn))
810 error (_("Numeric constant too large."));
812 prevn = n;
815 /* An integer constant is an int or a long. An L suffix forces it to
816 be long, and a U suffix forces it to be unsigned. To figure out
817 whether it fits, we shift it right and see whether anything remains.
818 Note that we can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or
819 more in one operation, because many compilers will warn about such a
820 shift (which always produces a zero result). To deal with the case
821 where it is we just always shift the value more than once, with fewer
822 bits each time. */
823 un = (ULONGEST) n >> 2;
824 if (long_p == 0 && (un >> 30) == 0)
826 high_bit = ((ULONGEST) 1) << 31;
827 signed_type = parse_d_type (ps)->builtin_int;
828 /* For decimal notation, keep the sign of the worked out type. */
829 if (base == 10 && !unsigned_p)
830 unsigned_type = parse_d_type (ps)->builtin_long;
831 else
832 unsigned_type = parse_d_type (ps)->builtin_uint;
834 else
836 int shift;
837 if (sizeof (ULONGEST) * HOST_CHAR_BIT < 64)
838 /* A long long does not fit in a LONGEST. */
839 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
840 else
841 shift = 63;
842 high_bit = (ULONGEST) 1 << shift;
843 signed_type = parse_d_type (ps)->builtin_long;
844 unsigned_type = parse_d_type (ps)->builtin_ulong;
847 putithere->typed_val_int.val = n;
849 /* If the high bit of the worked out type is set then this number
850 has to be unsigned_type. */
851 if (unsigned_p || (n & high_bit))
852 putithere->typed_val_int.type = unsigned_type;
853 else
854 putithere->typed_val_int.type = signed_type;
856 return INTEGER_LITERAL;
859 /* Temporary obstack used for holding strings. */
860 static struct obstack tempbuf;
861 static int tempbuf_init;
863 /* Parse a string or character literal from TOKPTR. The string or
864 character may be wide or unicode. *OUTPTR is set to just after the
865 end of the literal in the input string. The resulting token is
866 stored in VALUE. This returns a token value, either STRING or
867 CHAR, depending on what was parsed. *HOST_CHARS is set to the
868 number of host characters in the literal. */
870 static int
871 parse_string_or_char (const char *tokptr, const char **outptr,
872 struct typed_stoken *value, int *host_chars)
874 int quote;
876 /* Build the gdb internal form of the input string in tempbuf. Note
877 that the buffer is null byte terminated *only* for the
878 convenience of debugging gdb itself and printing the buffer
879 contents when the buffer contains no embedded nulls. Gdb does
880 not depend upon the buffer being null byte terminated, it uses
881 the length string instead. This allows gdb to handle C strings
882 (as well as strings in other languages) with embedded null
883 bytes */
885 if (!tempbuf_init)
886 tempbuf_init = 1;
887 else
888 obstack_free (&tempbuf, NULL);
889 obstack_init (&tempbuf);
891 /* Skip the quote. */
892 quote = *tokptr;
893 ++tokptr;
895 *host_chars = 0;
897 while (*tokptr)
899 char c = *tokptr;
900 if (c == '\\')
902 ++tokptr;
903 *host_chars += c_parse_escape (&tokptr, &tempbuf);
905 else if (c == quote)
906 break;
907 else
909 obstack_1grow (&tempbuf, c);
910 ++tokptr;
911 /* FIXME: this does the wrong thing with multi-byte host
912 characters. We could use mbrlen here, but that would
913 make "set host-charset" a bit less useful. */
914 ++*host_chars;
918 if (*tokptr != quote)
920 if (quote == '"' || quote == '`')
921 error (_("Unterminated string in expression."));
922 else
923 error (_("Unmatched single quote."));
925 ++tokptr;
927 /* FIXME: should instead use own language string_type enum
928 and handle D-specific string suffixes here. */
929 if (quote == '\'')
930 value->type = C_CHAR;
931 else
932 value->type = C_STRING;
934 value->ptr = (char *) obstack_base (&tempbuf);
935 value->length = obstack_object_size (&tempbuf);
937 *outptr = tokptr;
939 return quote == '\'' ? CHARACTER_LITERAL : STRING_LITERAL;
942 struct token
944 const char *oper;
945 int token;
946 enum exp_opcode opcode;
949 static const struct token tokentab3[] =
951 {"^^=", ASSIGN_MODIFY, BINOP_EXP},
952 {"<<=", ASSIGN_MODIFY, BINOP_LSH},
953 {">>=", ASSIGN_MODIFY, BINOP_RSH},
956 static const struct token tokentab2[] =
958 {"+=", ASSIGN_MODIFY, BINOP_ADD},
959 {"-=", ASSIGN_MODIFY, BINOP_SUB},
960 {"*=", ASSIGN_MODIFY, BINOP_MUL},
961 {"/=", ASSIGN_MODIFY, BINOP_DIV},
962 {"%=", ASSIGN_MODIFY, BINOP_REM},
963 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
964 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
965 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
966 {"++", INCREMENT, OP_NULL},
967 {"--", DECREMENT, OP_NULL},
968 {"&&", ANDAND, OP_NULL},
969 {"||", OROR, OP_NULL},
970 {"^^", HATHAT, OP_NULL},
971 {"<<", LSH, OP_NULL},
972 {">>", RSH, OP_NULL},
973 {"==", EQUAL, OP_NULL},
974 {"!=", NOTEQUAL, OP_NULL},
975 {"<=", LEQ, OP_NULL},
976 {">=", GEQ, OP_NULL},
977 {"..", DOTDOT, OP_NULL},
980 /* Identifier-like tokens. */
981 static const struct token ident_tokens[] =
983 {"is", IDENTITY, OP_NULL},
984 {"!is", NOTIDENTITY, OP_NULL},
986 {"cast", CAST_KEYWORD, OP_NULL},
987 {"const", CONST_KEYWORD, OP_NULL},
988 {"immutable", IMMUTABLE_KEYWORD, OP_NULL},
989 {"shared", SHARED_KEYWORD, OP_NULL},
990 {"super", SUPER_KEYWORD, OP_NULL},
992 {"null", NULL_KEYWORD, OP_NULL},
993 {"true", TRUE_KEYWORD, OP_NULL},
994 {"false", FALSE_KEYWORD, OP_NULL},
996 {"init", INIT_KEYWORD, OP_NULL},
997 {"sizeof", SIZEOF_KEYWORD, OP_NULL},
998 {"typeof", TYPEOF_KEYWORD, OP_NULL},
999 {"typeid", TYPEID_KEYWORD, OP_NULL},
1001 {"delegate", DELEGATE_KEYWORD, OP_NULL},
1002 {"function", FUNCTION_KEYWORD, OP_NULL},
1003 {"struct", STRUCT_KEYWORD, OP_NULL},
1004 {"union", UNION_KEYWORD, OP_NULL},
1005 {"class", CLASS_KEYWORD, OP_NULL},
1006 {"interface", INTERFACE_KEYWORD, OP_NULL},
1007 {"enum", ENUM_KEYWORD, OP_NULL},
1008 {"template", TEMPLATE_KEYWORD, OP_NULL},
1011 /* This is set if a NAME token appeared at the very end of the input
1012 string, with no whitespace separating the name from the EOF. This
1013 is used only when parsing to do field name completion. */
1014 static int saw_name_at_eof;
1016 /* This is set if the previously-returned token was a structure operator.
1017 This is used only when parsing to do field name completion. */
1018 static int last_was_structop;
1020 /* Depth of parentheses. */
1021 static int paren_depth;
1023 /* Read one token, getting characters through lexptr. */
1025 static int
1026 lex_one_token (struct parser_state *par_state)
1028 int c;
1029 int namelen;
1030 const char *tokstart;
1031 int saw_structop = last_was_structop;
1033 last_was_structop = 0;
1035 retry:
1037 pstate->prev_lexptr = pstate->lexptr;
1039 tokstart = pstate->lexptr;
1040 /* See if it is a special token of length 3. */
1041 for (const auto &token : tokentab3)
1042 if (strncmp (tokstart, token.oper, 3) == 0)
1044 pstate->lexptr += 3;
1045 yylval.opcode = token.opcode;
1046 return token.token;
1049 /* See if it is a special token of length 2. */
1050 for (const auto &token : tokentab2)
1051 if (strncmp (tokstart, token.oper, 2) == 0)
1053 pstate->lexptr += 2;
1054 yylval.opcode = token.opcode;
1055 return token.token;
1058 switch (c = *tokstart)
1060 case 0:
1061 /* If we're parsing for field name completion, and the previous
1062 token allows such completion, return a COMPLETE token.
1063 Otherwise, we were already scanning the original text, and
1064 we're really done. */
1065 if (saw_name_at_eof)
1067 saw_name_at_eof = 0;
1068 return COMPLETE;
1070 else if (saw_structop)
1071 return COMPLETE;
1072 else
1073 return 0;
1075 case ' ':
1076 case '\t':
1077 case '\n':
1078 pstate->lexptr++;
1079 goto retry;
1081 case '[':
1082 case '(':
1083 paren_depth++;
1084 pstate->lexptr++;
1085 return c;
1087 case ']':
1088 case ')':
1089 if (paren_depth == 0)
1090 return 0;
1091 paren_depth--;
1092 pstate->lexptr++;
1093 return c;
1095 case ',':
1096 if (pstate->comma_terminates && paren_depth == 0)
1097 return 0;
1098 pstate->lexptr++;
1099 return c;
1101 case '.':
1102 /* Might be a floating point number. */
1103 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
1105 if (pstate->parse_completion)
1106 last_was_structop = 1;
1107 goto symbol; /* Nope, must be a symbol. */
1109 /* FALL THRU. */
1111 case '0':
1112 case '1':
1113 case '2':
1114 case '3':
1115 case '4':
1116 case '5':
1117 case '6':
1118 case '7':
1119 case '8':
1120 case '9':
1122 /* It's a number. */
1123 int got_dot = 0, got_e = 0, toktype;
1124 const char *p = tokstart;
1125 int hex = input_radix > 10;
1127 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1129 p += 2;
1130 hex = 1;
1133 for (;; ++p)
1135 /* Hex exponents start with 'p', because 'e' is a valid hex
1136 digit and thus does not indicate a floating point number
1137 when the radix is hex. */
1138 if ((!hex && !got_e && tolower (p[0]) == 'e')
1139 || (hex && !got_e && tolower (p[0] == 'p')))
1140 got_dot = got_e = 1;
1141 /* A '.' always indicates a decimal floating point number
1142 regardless of the radix. If we have a '..' then its the
1143 end of the number and the beginning of a slice. */
1144 else if (!got_dot && (p[0] == '.' && p[1] != '.'))
1145 got_dot = 1;
1146 /* This is the sign of the exponent, not the end of the number. */
1147 else if (got_e && (tolower (p[-1]) == 'e' || tolower (p[-1]) == 'p')
1148 && (*p == '-' || *p == '+'))
1149 continue;
1150 /* We will take any letters or digits, ignoring any embedded '_'.
1151 parse_number will complain if past the radix, or if L or U are
1152 not final. */
1153 else if ((*p < '0' || *p > '9') && (*p != '_')
1154 && ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z')))
1155 break;
1158 toktype = parse_number (par_state, tokstart, p - tokstart,
1159 got_dot|got_e, &yylval);
1160 if (toktype == ERROR)
1162 char *err_copy = (char *) alloca (p - tokstart + 1);
1164 memcpy (err_copy, tokstart, p - tokstart);
1165 err_copy[p - tokstart] = 0;
1166 error (_("Invalid number \"%s\"."), err_copy);
1168 pstate->lexptr = p;
1169 return toktype;
1172 case '@':
1174 const char *p = &tokstart[1];
1175 size_t len = strlen ("entry");
1177 while (isspace (*p))
1178 p++;
1179 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
1180 && p[len] != '_')
1182 pstate->lexptr = &p[len];
1183 return ENTRY;
1186 /* FALLTHRU */
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 case '=':
1202 case '{':
1203 case '}':
1204 symbol:
1205 pstate->lexptr++;
1206 return c;
1208 case '\'':
1209 case '"':
1210 case '`':
1212 int host_len;
1213 int result = parse_string_or_char (tokstart, &pstate->lexptr,
1214 &yylval.tsval, &host_len);
1215 if (result == CHARACTER_LITERAL)
1217 if (host_len == 0)
1218 error (_("Empty character constant."));
1219 else if (host_len > 2 && c == '\'')
1221 ++tokstart;
1222 namelen = pstate->lexptr - tokstart - 1;
1223 goto tryname;
1225 else if (host_len > 1)
1226 error (_("Invalid character constant."));
1228 return result;
1232 if (!(c == '_' || c == '$'
1233 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1234 /* We must have come across a bad character (e.g. ';'). */
1235 error (_("Invalid character '%c' in expression"), c);
1237 /* It's a name. See how long it is. */
1238 namelen = 0;
1239 for (c = tokstart[namelen];
1240 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1241 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));)
1242 c = tokstart[++namelen];
1244 /* The token "if" terminates the expression and is NOT
1245 removed from the input stream. */
1246 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1247 return 0;
1249 /* For the same reason (breakpoint conditions), "thread N"
1250 terminates the expression. "thread" could be an identifier, but
1251 an identifier is never followed by a number without intervening
1252 punctuation. "task" is similar. Handle abbreviations of these,
1253 similarly to breakpoint.c:find_condition_and_thread. */
1254 if (namelen >= 1
1255 && (strncmp (tokstart, "thread", namelen) == 0
1256 || strncmp (tokstart, "task", namelen) == 0)
1257 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t'))
1259 const char *p = tokstart + namelen + 1;
1261 while (*p == ' ' || *p == '\t')
1262 p++;
1263 if (*p >= '0' && *p <= '9')
1264 return 0;
1267 pstate->lexptr += namelen;
1269 tryname:
1271 yylval.sval.ptr = tokstart;
1272 yylval.sval.length = namelen;
1274 /* Catch specific keywords. */
1275 std::string copy = copy_name (yylval.sval);
1276 for (const auto &token : ident_tokens)
1277 if (copy == token.oper)
1279 /* It is ok to always set this, even though we don't always
1280 strictly need to. */
1281 yylval.opcode = token.opcode;
1282 return token.token;
1285 if (*tokstart == '$')
1286 return DOLLAR_VARIABLE;
1288 yylval.tsym.type
1289 = language_lookup_primitive_type (par_state->language (),
1290 par_state->gdbarch (), copy.c_str ());
1291 if (yylval.tsym.type != NULL)
1292 return TYPENAME;
1294 /* Input names that aren't symbols but ARE valid hex numbers,
1295 when the input radix permits them, can be names or numbers
1296 depending on the parse. Note we support radixes > 16 here. */
1297 if ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1298 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))
1300 YYSTYPE newlval; /* Its value is ignored. */
1301 int hextype = parse_number (par_state, tokstart, namelen, 0, &newlval);
1302 if (hextype == INTEGER_LITERAL)
1303 return NAME_OR_INT;
1306 if (pstate->parse_completion && *pstate->lexptr == '\0')
1307 saw_name_at_eof = 1;
1309 return IDENTIFIER;
1312 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
1313 struct token_and_value
1315 int token;
1316 YYSTYPE value;
1320 /* A FIFO of tokens that have been read but not yet returned to the
1321 parser. */
1322 static std::vector<token_and_value> token_fifo;
1324 /* Non-zero if the lexer should return tokens from the FIFO. */
1325 static int popping;
1327 /* Temporary storage for yylex; this holds symbol names as they are
1328 built up. */
1329 static auto_obstack name_obstack;
1331 /* Classify an IDENTIFIER token. The contents of the token are in `yylval'.
1332 Updates yylval and returns the new token type. BLOCK is the block
1333 in which lookups start; this can be NULL to mean the global scope. */
1335 static int
1336 classify_name (struct parser_state *par_state, const struct block *block)
1338 struct block_symbol sym;
1339 struct field_of_this_result is_a_field_of_this;
1341 std::string copy = copy_name (yylval.sval);
1343 sym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN, &is_a_field_of_this);
1344 if (sym.symbol && sym.symbol->aclass () == LOC_TYPEDEF)
1346 yylval.tsym.type = sym.symbol->type ();
1347 return TYPENAME;
1349 else if (sym.symbol == NULL)
1351 /* Look-up first for a module name, then a type. */
1352 sym = lookup_symbol (copy.c_str (), block, MODULE_DOMAIN, NULL);
1353 if (sym.symbol == NULL)
1354 sym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN, NULL);
1356 if (sym.symbol != NULL)
1358 yylval.tsym.type = sym.symbol->type ();
1359 return TYPENAME;
1362 return UNKNOWN_NAME;
1365 return IDENTIFIER;
1368 /* Like classify_name, but used by the inner loop of the lexer, when a
1369 name might have already been seen. CONTEXT is the context type, or
1370 NULL if this is the first component of a name. */
1372 static int
1373 classify_inner_name (struct parser_state *par_state,
1374 const struct block *block, struct type *context)
1376 struct type *type;
1378 if (context == NULL)
1379 return classify_name (par_state, block);
1381 type = check_typedef (context);
1382 if (!type_aggregate_p (type))
1383 return ERROR;
1385 std::string copy = copy_name (yylval.ssym.stoken);
1386 yylval.ssym.sym = d_lookup_nested_symbol (type, copy.c_str (), block);
1388 if (yylval.ssym.sym.symbol == NULL)
1389 return ERROR;
1391 if (yylval.ssym.sym.symbol->aclass () == LOC_TYPEDEF)
1393 yylval.tsym.type = yylval.ssym.sym.symbol->type ();
1394 return TYPENAME;
1397 return IDENTIFIER;
1400 /* The outer level of a two-level lexer. This calls the inner lexer
1401 to return tokens. It then either returns these tokens, or
1402 aggregates them into a larger token. This lets us work around a
1403 problem in our parsing approach, where the parser could not
1404 distinguish between qualified names and qualified types at the
1405 right point. */
1407 static int
1408 yylex (void)
1410 token_and_value current;
1411 int last_was_dot;
1412 struct type *context_type = NULL;
1413 int last_to_examine, next_to_examine, checkpoint;
1414 const struct block *search_block;
1416 if (popping && !token_fifo.empty ())
1417 goto do_pop;
1418 popping = 0;
1420 /* Read the first token and decide what to do. */
1421 current.token = lex_one_token (pstate);
1422 if (current.token != IDENTIFIER && current.token != '.')
1423 return current.token;
1425 /* Read any sequence of alternating "." and identifier tokens into
1426 the token FIFO. */
1427 current.value = yylval;
1428 token_fifo.push_back (current);
1429 last_was_dot = current.token == '.';
1431 while (1)
1433 current.token = lex_one_token (pstate);
1434 current.value = yylval;
1435 token_fifo.push_back (current);
1437 if ((last_was_dot && current.token != IDENTIFIER)
1438 || (!last_was_dot && current.token != '.'))
1439 break;
1441 last_was_dot = !last_was_dot;
1443 popping = 1;
1445 /* We always read one extra token, so compute the number of tokens
1446 to examine accordingly. */
1447 last_to_examine = token_fifo.size () - 2;
1448 next_to_examine = 0;
1450 current = token_fifo[next_to_examine];
1451 ++next_to_examine;
1453 /* If we are not dealing with a typename, now is the time to find out. */
1454 if (current.token == IDENTIFIER)
1456 yylval = current.value;
1457 current.token = classify_name (pstate, pstate->expression_context_block);
1458 current.value = yylval;
1461 /* If the IDENTIFIER is not known, it could be a package symbol,
1462 first try building up a name until we find the qualified module. */
1463 if (current.token == UNKNOWN_NAME)
1465 name_obstack.clear ();
1466 obstack_grow (&name_obstack, current.value.sval.ptr,
1467 current.value.sval.length);
1469 last_was_dot = 0;
1471 while (next_to_examine <= last_to_examine)
1473 token_and_value next;
1475 next = token_fifo[next_to_examine];
1476 ++next_to_examine;
1478 if (next.token == IDENTIFIER && last_was_dot)
1480 /* Update the partial name we are constructing. */
1481 obstack_grow_str (&name_obstack, ".");
1482 obstack_grow (&name_obstack, next.value.sval.ptr,
1483 next.value.sval.length);
1485 yylval.sval.ptr = (char *) obstack_base (&name_obstack);
1486 yylval.sval.length = obstack_object_size (&name_obstack);
1488 current.token = classify_name (pstate,
1489 pstate->expression_context_block);
1490 current.value = yylval;
1492 /* We keep going until we find a TYPENAME. */
1493 if (current.token == TYPENAME)
1495 /* Install it as the first token in the FIFO. */
1496 token_fifo[0] = current;
1497 token_fifo.erase (token_fifo.begin () + 1,
1498 token_fifo.begin () + next_to_examine);
1499 break;
1502 else if (next.token == '.' && !last_was_dot)
1503 last_was_dot = 1;
1504 else
1506 /* We've reached the end of the name. */
1507 break;
1511 /* Reset our current token back to the start, if we found nothing
1512 this means that we will just jump to do pop. */
1513 current = token_fifo[0];
1514 next_to_examine = 1;
1516 if (current.token != TYPENAME && current.token != '.')
1517 goto do_pop;
1519 name_obstack.clear ();
1520 checkpoint = 0;
1521 if (current.token == '.')
1522 search_block = NULL;
1523 else
1525 gdb_assert (current.token == TYPENAME);
1526 search_block = pstate->expression_context_block;
1527 obstack_grow (&name_obstack, current.value.sval.ptr,
1528 current.value.sval.length);
1529 context_type = current.value.tsym.type;
1530 checkpoint = 1;
1533 last_was_dot = current.token == '.';
1535 while (next_to_examine <= last_to_examine)
1537 token_and_value next;
1539 next = token_fifo[next_to_examine];
1540 ++next_to_examine;
1542 if (next.token == IDENTIFIER && last_was_dot)
1544 int classification;
1546 yylval = next.value;
1547 classification = classify_inner_name (pstate, search_block,
1548 context_type);
1549 /* We keep going until we either run out of names, or until
1550 we have a qualified name which is not a type. */
1551 if (classification != TYPENAME && classification != IDENTIFIER)
1552 break;
1554 /* Accept up to this token. */
1555 checkpoint = next_to_examine;
1557 /* Update the partial name we are constructing. */
1558 if (context_type != NULL)
1560 /* We don't want to put a leading "." into the name. */
1561 obstack_grow_str (&name_obstack, ".");
1563 obstack_grow (&name_obstack, next.value.sval.ptr,
1564 next.value.sval.length);
1566 yylval.sval.ptr = (char *) obstack_base (&name_obstack);
1567 yylval.sval.length = obstack_object_size (&name_obstack);
1568 current.value = yylval;
1569 current.token = classification;
1571 last_was_dot = 0;
1573 if (classification == IDENTIFIER)
1574 break;
1576 context_type = yylval.tsym.type;
1578 else if (next.token == '.' && !last_was_dot)
1579 last_was_dot = 1;
1580 else
1582 /* We've reached the end of the name. */
1583 break;
1587 /* If we have a replacement token, install it as the first token in
1588 the FIFO, and delete the other constituent tokens. */
1589 if (checkpoint > 0)
1591 token_fifo[0] = current;
1592 if (checkpoint > 1)
1593 token_fifo.erase (token_fifo.begin () + 1,
1594 token_fifo.begin () + checkpoint);
1597 do_pop:
1598 current = token_fifo[0];
1599 token_fifo.erase (token_fifo.begin ());
1600 yylval = current.value;
1601 return current.token;
1605 d_parse (struct parser_state *par_state)
1607 /* Setting up the parser state. */
1608 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1609 gdb_assert (par_state != NULL);
1610 pstate = par_state;
1612 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
1613 parser_debug);
1615 struct type_stack stack;
1616 scoped_restore restore_type_stack = make_scoped_restore (&type_stack,
1617 &stack);
1619 /* Initialize some state used by the lexer. */
1620 last_was_structop = 0;
1621 saw_name_at_eof = 0;
1622 paren_depth = 0;
1624 token_fifo.clear ();
1625 popping = 0;
1626 name_obstack.clear ();
1628 int result = yyparse ();
1629 if (!result)
1630 pstate->set_operation (pstate->pop ());
1631 return result;
1634 static void
1635 yyerror (const char *msg)
1637 if (pstate->prev_lexptr)
1638 pstate->lexptr = pstate->prev_lexptr;
1640 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);