]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/c-exp.y
Make c-exp.y work with Bison 3.8+
[thirdparty/binutils-gdb.git] / gdb / c-exp.y
1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 /* Parse a C expression from text in a string,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result.
27
28 Note that malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
35
36 %{
37
38 #include "defs.h"
39 #include <ctype.h>
40 #include "expression.h"
41 #include "value.h"
42 #include "parser-defs.h"
43 #include "language.h"
44 #include "c-lang.h"
45 #include "c-support.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
49 #include "charset.h"
50 #include "block.h"
51 #include "cp-support.h"
52 #include "macroscope.h"
53 #include "objc-lang.h"
54 #include "typeprint.h"
55 #include "cp-abi.h"
56 #include "type-stack.h"
57 #include "target-float.h"
58
59 #define parse_type(ps) builtin_type (ps->gdbarch ())
60
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
62 etc). */
63 #define GDB_YY_REMAP_PREFIX c_
64 #include "yy-remap.h"
65
66 /* The state of the parser, used internally when we are parsing the
67 expression. */
68
69 static struct parser_state *pstate = NULL;
70
71 /* Data that must be held for the duration of a parse. */
72
73 struct c_parse_state
74 {
75 /* These are used to hold type lists and type stacks that are
76 allocated during the parse. */
77 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
78 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
79
80 /* Storage for some strings allocated during the parse. */
81 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
82
83 /* When we find that lexptr (the global var defined in parse.c) is
84 pointing at a macro invocation, we expand the invocation, and call
85 scan_macro_expansion to save the old lexptr here and point lexptr
86 into the expanded text. When we reach the end of that, we call
87 end_macro_expansion to pop back to the value we saved here. The
88 macro expansion code promises to return only fully-expanded text,
89 so we don't need to "push" more than one level.
90
91 This is disgusting, of course. It would be cleaner to do all macro
92 expansion beforehand, and then hand that to lexptr. But we don't
93 really know where the expression ends. Remember, in a command like
94
95 (gdb) break *ADDRESS if CONDITION
96
97 we evaluate ADDRESS in the scope of the current frame, but we
98 evaluate CONDITION in the scope of the breakpoint's location. So
99 it's simply wrong to try to macro-expand the whole thing at once. */
100 const char *macro_original_text = nullptr;
101
102 /* We save all intermediate macro expansions on this obstack for the
103 duration of a single parse. The expansion text may sometimes have
104 to live past the end of the expansion, due to yacc lookahead.
105 Rather than try to be clever about saving the data for a single
106 token, we simply keep it all and delete it after parsing has
107 completed. */
108 auto_obstack expansion_obstack;
109
110 /* The type stack. */
111 struct type_stack type_stack;
112 };
113
114 /* This is set and cleared in c_parse. */
115
116 static struct c_parse_state *cpstate;
117
118 int yyparse (void);
119
120 static int yylex (void);
121
122 static void yyerror (const char *);
123
124 static int type_aggregate_p (struct type *);
125
126 %}
127
128 /* Although the yacc "value" of an expression is not used,
129 since the result is stored in the structure being created,
130 other node types do have values. */
131
132 %union
133 {
134 LONGEST lval;
135 struct {
136 LONGEST val;
137 struct type *type;
138 } typed_val_int;
139 struct {
140 gdb_byte val[16];
141 struct type *type;
142 } typed_val_float;
143 struct type *tval;
144 struct stoken sval;
145 struct typed_stoken tsval;
146 struct ttype tsym;
147 struct symtoken ssym;
148 int voidval;
149 const struct block *bval;
150 enum exp_opcode opcode;
151
152 struct stoken_vector svec;
153 std::vector<struct type *> *tvec;
154
155 struct type_stack *type_stack;
156
157 struct objc_class_str theclass;
158 }
159
160 %{
161 /* YYSTYPE gets defined by %union */
162 static int parse_number (struct parser_state *par_state,
163 const char *, int, int, YYSTYPE *);
164 static struct stoken operator_stoken (const char *);
165 static struct stoken typename_stoken (const char *);
166 static void check_parameter_typelist (std::vector<struct type *> *);
167 static void write_destructor_name (struct parser_state *par_state,
168 struct stoken);
169
170 #if defined(YYBISON) && YYBISON < 30800
171 static void c_print_token (FILE *file, int type, YYSTYPE value);
172 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
173 #endif
174 %}
175
176 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
177 %type <lval> rcurly
178 %type <tval> type typebase scalar_type
179 %type <tvec> nonempty_typelist func_mod parameter_typelist
180 /* %type <bval> block */
181
182 /* Fancy type parsing. */
183 %type <tval> ptype
184 %type <lval> array_mod
185 %type <tval> conversion_type_id
186
187 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
188
189 %token <typed_val_int> INT COMPLEX_INT
190 %token <typed_val_float> FLOAT COMPLEX_FLOAT
191
192 /* Both NAME and TYPENAME tokens represent symbols in the input,
193 and both convey their data as strings.
194 But a TYPENAME is a string that happens to be defined as a typedef
195 or builtin type name (such as int or char)
196 and a NAME is any other symbol.
197 Contexts where this distinction is not important can use the
198 nonterminal "name", which matches either NAME or TYPENAME. */
199
200 %token <tsval> STRING
201 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
202 %token SELECTOR /* ObjC "@selector" pseudo-operator */
203 %token <tsval> CHAR
204 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
205 %token <ssym> UNKNOWN_CPP_NAME
206 %token <voidval> COMPLETE
207 %token <tsym> TYPENAME
208 %token <theclass> CLASSNAME /* ObjC Class name */
209 %type <sval> name field_name
210 %type <svec> string_exp
211 %type <ssym> name_not_typename
212 %type <tsym> type_name
213
214 /* This is like a '[' token, but is only generated when parsing
215 Objective C. This lets us reuse the same parser without
216 erroneously parsing ObjC-specific expressions in C. */
217 %token OBJC_LBRAC
218
219 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
220 but which would parse as a valid number in the current input radix.
221 E.g. "c" when input_radix==16. Depending on the parse, it will be
222 turned into a name or into a number. */
223
224 %token <ssym> NAME_OR_INT
225
226 %token OPERATOR
227 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
228 %token TEMPLATE
229 %token ERROR
230 %token NEW DELETE
231 %type <sval> oper
232 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
233 %token ENTRY
234 %token TYPEOF
235 %token DECLTYPE
236 %token TYPEID
237
238 /* Special type cases, put in to allow the parser to distinguish different
239 legal basetypes. */
240 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
241 %token RESTRICT ATOMIC CAPABILITY
242 %token FLOAT_KEYWORD COMPLEX INTCAP_KEYWORD UINTCAP_KEYWORD
243
244 %token <sval> DOLLAR_VARIABLE
245
246 %token <opcode> ASSIGN_MODIFY
247
248 /* C++ */
249 %token TRUEKEYWORD
250 %token FALSEKEYWORD
251
252
253 %left ','
254 %left ABOVE_COMMA
255 %right '=' ASSIGN_MODIFY
256 %right '?'
257 %left OROR
258 %left ANDAND
259 %left '|'
260 %left '^'
261 %left '&'
262 %left EQUAL NOTEQUAL
263 %left '<' '>' LEQ GEQ
264 %left LSH RSH
265 %left '@'
266 %left '+' '-'
267 %left '*' '/' '%'
268 %right UNARY INCREMENT DECREMENT
269 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
270 %token <ssym> BLOCKNAME
271 %token <bval> FILENAME
272 %type <bval> block
273 %left COLONCOLON
274
275 %token DOTDOTDOT
276
277 \f
278 %%
279
280 start : exp1
281 | type_exp
282 ;
283
284 type_exp: type
285 { write_exp_elt_opcode(pstate, OP_TYPE);
286 write_exp_elt_type(pstate, $1);
287 write_exp_elt_opcode(pstate, OP_TYPE);}
288 | TYPEOF '(' exp ')'
289 {
290 write_exp_elt_opcode (pstate, OP_TYPEOF);
291 }
292 | TYPEOF '(' type ')'
293 {
294 write_exp_elt_opcode (pstate, OP_TYPE);
295 write_exp_elt_type (pstate, $3);
296 write_exp_elt_opcode (pstate, OP_TYPE);
297 }
298 | DECLTYPE '(' exp ')'
299 {
300 write_exp_elt_opcode (pstate, OP_DECLTYPE);
301 }
302 ;
303
304 /* Expressions, including the comma operator. */
305 exp1 : exp
306 | exp1 ',' exp
307 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
308 ;
309
310 /* Expressions, not including the comma operator. */
311 exp : '*' exp %prec UNARY
312 { write_exp_elt_opcode (pstate, UNOP_IND); }
313 ;
314
315 exp : '&' exp %prec UNARY
316 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
317 ;
318
319 exp : '-' exp %prec UNARY
320 { write_exp_elt_opcode (pstate, UNOP_NEG); }
321 ;
322
323 exp : '+' exp %prec UNARY
324 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
325 ;
326
327 exp : '!' exp %prec UNARY
328 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
329 ;
330
331 exp : '~' exp %prec UNARY
332 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
333 ;
334
335 exp : INCREMENT exp %prec UNARY
336 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
337 ;
338
339 exp : DECREMENT exp %prec UNARY
340 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
341 ;
342
343 exp : exp INCREMENT %prec UNARY
344 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
345 ;
346
347 exp : exp DECREMENT %prec UNARY
348 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
349 ;
350
351 exp : TYPEID '(' exp ')' %prec UNARY
352 { write_exp_elt_opcode (pstate, OP_TYPEID); }
353 ;
354
355 exp : TYPEID '(' type_exp ')' %prec UNARY
356 { write_exp_elt_opcode (pstate, OP_TYPEID); }
357 ;
358
359 exp : SIZEOF exp %prec UNARY
360 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
361 ;
362
363 exp : ALIGNOF '(' type_exp ')' %prec UNARY
364 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
365 ;
366
367 exp : exp ARROW field_name
368 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
369 write_exp_string (pstate, $3);
370 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
371 ;
372
373 exp : exp ARROW field_name COMPLETE
374 { pstate->mark_struct_expression ();
375 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
376 write_exp_string (pstate, $3);
377 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
378 ;
379
380 exp : exp ARROW COMPLETE
381 { struct stoken s;
382 pstate->mark_struct_expression ();
383 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
384 s.ptr = "";
385 s.length = 0;
386 write_exp_string (pstate, s);
387 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
388 ;
389
390 exp : exp ARROW '~' name
391 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
392 write_destructor_name (pstate, $4);
393 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
394 ;
395
396 exp : exp ARROW '~' name COMPLETE
397 { pstate->mark_struct_expression ();
398 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
399 write_destructor_name (pstate, $4);
400 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
401 ;
402
403 exp : exp ARROW qualified_name
404 { /* exp->type::name becomes exp->*(&type::name) */
405 /* Note: this doesn't work if name is a
406 static member! FIXME */
407 write_exp_elt_opcode (pstate, UNOP_ADDR);
408 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
409 ;
410
411 exp : exp ARROW_STAR exp
412 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
413 ;
414
415 exp : exp '.' field_name
416 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
417 write_exp_string (pstate, $3);
418 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
419 ;
420
421 exp : exp '.' field_name COMPLETE
422 { pstate->mark_struct_expression ();
423 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
424 write_exp_string (pstate, $3);
425 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
426 ;
427
428 exp : exp '.' COMPLETE
429 { struct stoken s;
430 pstate->mark_struct_expression ();
431 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
432 s.ptr = "";
433 s.length = 0;
434 write_exp_string (pstate, s);
435 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
436 ;
437
438 exp : exp '.' '~' name
439 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
440 write_destructor_name (pstate, $4);
441 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
442 ;
443
444 exp : exp '.' '~' name COMPLETE
445 { pstate->mark_struct_expression ();
446 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
447 write_destructor_name (pstate, $4);
448 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
449 ;
450
451 exp : exp '.' qualified_name
452 { /* exp.type::name becomes exp.*(&type::name) */
453 /* Note: this doesn't work if name is a
454 static member! FIXME */
455 write_exp_elt_opcode (pstate, UNOP_ADDR);
456 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
457 ;
458
459 exp : exp DOT_STAR exp
460 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
461 ;
462
463 exp : exp '[' exp1 ']'
464 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
465 ;
466
467 exp : exp OBJC_LBRAC exp1 ']'
468 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
469 ;
470
471 /*
472 * The rules below parse ObjC message calls of the form:
473 * '[' target selector {':' argument}* ']'
474 */
475
476 exp : OBJC_LBRAC TYPENAME
477 {
478 CORE_ADDR theclass;
479
480 std::string copy = copy_name ($2.stoken);
481 theclass = lookup_objc_class (pstate->gdbarch (),
482 copy.c_str ());
483 if (theclass == 0)
484 error (_("%s is not an ObjC Class"),
485 copy.c_str ());
486 write_exp_elt_opcode (pstate, OP_LONG);
487 write_exp_elt_type (pstate,
488 parse_type (pstate)->builtin_int);
489 write_exp_elt_longcst (pstate, (LONGEST) theclass);
490 write_exp_elt_opcode (pstate, OP_LONG);
491 start_msglist();
492 }
493 msglist ']'
494 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
495 end_msglist (pstate);
496 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
497 }
498 ;
499
500 exp : OBJC_LBRAC CLASSNAME
501 {
502 write_exp_elt_opcode (pstate, OP_LONG);
503 write_exp_elt_type (pstate,
504 parse_type (pstate)->builtin_int);
505 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
506 write_exp_elt_opcode (pstate, OP_LONG);
507 start_msglist();
508 }
509 msglist ']'
510 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
511 end_msglist (pstate);
512 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
513 }
514 ;
515
516 exp : OBJC_LBRAC exp
517 { start_msglist(); }
518 msglist ']'
519 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
520 end_msglist (pstate);
521 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
522 }
523 ;
524
525 msglist : name
526 { add_msglist(&$1, 0); }
527 | msgarglist
528 ;
529
530 msgarglist : msgarg
531 | msgarglist msgarg
532 ;
533
534 msgarg : name ':' exp
535 { add_msglist(&$1, 1); }
536 | ':' exp /* Unnamed arg. */
537 { add_msglist(0, 1); }
538 | ',' exp /* Variable number of args. */
539 { add_msglist(0, 0); }
540 ;
541
542 exp : exp '('
543 /* This is to save the value of arglist_len
544 being accumulated by an outer function call. */
545 { pstate->start_arglist (); }
546 arglist ')' %prec ARROW
547 { write_exp_elt_opcode (pstate, OP_FUNCALL);
548 write_exp_elt_longcst (pstate,
549 pstate->end_arglist ());
550 write_exp_elt_opcode (pstate, OP_FUNCALL); }
551 ;
552
553 /* This is here to disambiguate with the production for
554 "func()::static_var" further below, which uses
555 function_method_void. */
556 exp : exp '(' ')' %prec ARROW
557 { pstate->start_arglist ();
558 write_exp_elt_opcode (pstate, OP_FUNCALL);
559 write_exp_elt_longcst (pstate,
560 pstate->end_arglist ());
561 write_exp_elt_opcode (pstate, OP_FUNCALL); }
562 ;
563
564
565 exp : UNKNOWN_CPP_NAME '('
566 {
567 /* This could potentially be a an argument defined
568 lookup function (Koenig). */
569 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
570 write_exp_elt_block
571 (pstate, pstate->expression_context_block);
572 write_exp_elt_sym (pstate,
573 NULL); /* Placeholder. */
574 write_exp_string (pstate, $1.stoken);
575 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
576
577 /* This is to save the value of arglist_len
578 being accumulated by an outer function call. */
579
580 pstate->start_arglist ();
581 }
582 arglist ')' %prec ARROW
583 {
584 write_exp_elt_opcode (pstate, OP_FUNCALL);
585 write_exp_elt_longcst (pstate,
586 pstate->end_arglist ());
587 write_exp_elt_opcode (pstate, OP_FUNCALL);
588 }
589 ;
590
591 lcurly : '{'
592 { pstate->start_arglist (); }
593 ;
594
595 arglist :
596 ;
597
598 arglist : exp
599 { pstate->arglist_len = 1; }
600 ;
601
602 arglist : arglist ',' exp %prec ABOVE_COMMA
603 { pstate->arglist_len++; }
604 ;
605
606 function_method: exp '(' parameter_typelist ')' const_or_volatile
607 {
608 std::vector<struct type *> *type_list = $3;
609 LONGEST len = type_list->size ();
610
611 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
612 /* Save the const/volatile qualifiers as
613 recorded by the const_or_volatile
614 production's actions. */
615 write_exp_elt_longcst
616 (pstate,
617 (cpstate->type_stack
618 .follow_type_instance_flags ()));
619 write_exp_elt_longcst (pstate, len);
620 for (type *type_elt : *type_list)
621 write_exp_elt_type (pstate, type_elt);
622 write_exp_elt_longcst(pstate, len);
623 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
624 }
625 ;
626
627 function_method_void: exp '(' ')' const_or_volatile
628 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
629 /* See above. */
630 write_exp_elt_longcst
631 (pstate,
632 cpstate->type_stack.follow_type_instance_flags ());
633 write_exp_elt_longcst (pstate, 0);
634 write_exp_elt_longcst (pstate, 0);
635 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
636 }
637 ;
638
639 exp : function_method
640 ;
641
642 /* Normally we must interpret "func()" as a function call, instead of
643 a type. The user needs to write func(void) to disambiguate.
644 However, in the "func()::static_var" case, there's no
645 ambiguity. */
646 function_method_void_or_typelist: function_method
647 | function_method_void
648 ;
649
650 exp : function_method_void_or_typelist COLONCOLON name
651 {
652 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
653 write_exp_string (pstate, $3);
654 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
655 }
656 ;
657
658 rcurly : '}'
659 { $$ = pstate->end_arglist () - 1; }
660 ;
661 exp : lcurly arglist rcurly %prec ARROW
662 { write_exp_elt_opcode (pstate, OP_ARRAY);
663 write_exp_elt_longcst (pstate, (LONGEST) 0);
664 write_exp_elt_longcst (pstate, (LONGEST) $3);
665 write_exp_elt_opcode (pstate, OP_ARRAY); }
666 ;
667
668 exp : lcurly type_exp rcurly exp %prec UNARY
669 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
670 ;
671
672 exp : '(' type_exp ')' exp %prec UNARY
673 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
674 ;
675
676 exp : '(' exp1 ')'
677 { }
678 ;
679
680 /* Binary operators in order of decreasing precedence. */
681
682 exp : exp '@' exp
683 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
684 ;
685
686 exp : exp '*' exp
687 { write_exp_elt_opcode (pstate, BINOP_MUL); }
688 ;
689
690 exp : exp '/' exp
691 { write_exp_elt_opcode (pstate, BINOP_DIV); }
692 ;
693
694 exp : exp '%' exp
695 { write_exp_elt_opcode (pstate, BINOP_REM); }
696 ;
697
698 exp : exp '+' exp
699 { write_exp_elt_opcode (pstate, BINOP_ADD); }
700 ;
701
702 exp : exp '-' exp
703 { write_exp_elt_opcode (pstate, BINOP_SUB); }
704 ;
705
706 exp : exp LSH exp
707 { write_exp_elt_opcode (pstate, BINOP_LSH); }
708 ;
709
710 exp : exp RSH exp
711 { write_exp_elt_opcode (pstate, BINOP_RSH); }
712 ;
713
714 exp : exp EQUAL exp
715 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
716 ;
717
718 exp : exp NOTEQUAL exp
719 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
720 ;
721
722 exp : exp LEQ exp
723 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
724 ;
725
726 exp : exp GEQ exp
727 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
728 ;
729
730 exp : exp '<' exp
731 { write_exp_elt_opcode (pstate, BINOP_LESS); }
732 ;
733
734 exp : exp '>' exp
735 { write_exp_elt_opcode (pstate, BINOP_GTR); }
736 ;
737
738 exp : exp '&' exp
739 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
740 ;
741
742 exp : exp '^' exp
743 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
744 ;
745
746 exp : exp '|' exp
747 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
748 ;
749
750 exp : exp ANDAND exp
751 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
752 ;
753
754 exp : exp OROR exp
755 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
756 ;
757
758 exp : exp '?' exp ':' exp %prec '?'
759 { write_exp_elt_opcode (pstate, TERNOP_COND); }
760 ;
761
762 exp : exp '=' exp
763 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
764 ;
765
766 exp : exp ASSIGN_MODIFY exp
767 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
768 write_exp_elt_opcode (pstate, $2);
769 write_exp_elt_opcode (pstate,
770 BINOP_ASSIGN_MODIFY); }
771 ;
772
773 exp : INT
774 { write_exp_elt_opcode (pstate, OP_LONG);
775 write_exp_elt_type (pstate, $1.type);
776 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
777 write_exp_elt_opcode (pstate, OP_LONG); }
778 ;
779
780 exp : COMPLEX_INT
781 {
782 write_exp_elt_opcode (pstate, OP_LONG);
783 write_exp_elt_type (pstate, TYPE_TARGET_TYPE ($1.type));
784 write_exp_elt_longcst (pstate, 0);
785 write_exp_elt_opcode (pstate, OP_LONG);
786 write_exp_elt_opcode (pstate, OP_LONG);
787 write_exp_elt_type (pstate, TYPE_TARGET_TYPE ($1.type));
788 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
789 write_exp_elt_opcode (pstate, OP_LONG);
790 write_exp_elt_opcode (pstate, OP_COMPLEX);
791 write_exp_elt_type (pstate, $1.type);
792 write_exp_elt_opcode (pstate, OP_COMPLEX);
793 }
794 ;
795
796 exp : CHAR
797 {
798 struct stoken_vector vec;
799 vec.len = 1;
800 vec.tokens = &$1;
801 write_exp_string_vector (pstate, $1.type, &vec);
802 }
803 ;
804
805 exp : NAME_OR_INT
806 { YYSTYPE val;
807 parse_number (pstate, $1.stoken.ptr,
808 $1.stoken.length, 0, &val);
809 write_exp_elt_opcode (pstate, OP_LONG);
810 write_exp_elt_type (pstate, val.typed_val_int.type);
811 write_exp_elt_longcst (pstate,
812 (LONGEST) val.typed_val_int.val);
813 write_exp_elt_opcode (pstate, OP_LONG);
814 }
815 ;
816
817
818 exp : FLOAT
819 { write_exp_elt_opcode (pstate, OP_FLOAT);
820 write_exp_elt_type (pstate, $1.type);
821 write_exp_elt_floatcst (pstate, $1.val);
822 write_exp_elt_opcode (pstate, OP_FLOAT); }
823 ;
824
825 exp : COMPLEX_FLOAT
826 {
827 struct type *underlying
828 = TYPE_TARGET_TYPE ($1.type);
829
830 write_exp_elt_opcode (pstate, OP_FLOAT);
831 write_exp_elt_type (pstate, underlying);
832 gdb_byte val[16];
833 target_float_from_host_double (val, underlying, 0);
834 write_exp_elt_floatcst (pstate, val);
835 write_exp_elt_opcode (pstate, OP_FLOAT);
836 write_exp_elt_opcode (pstate, OP_FLOAT);
837 write_exp_elt_type (pstate, underlying);
838 write_exp_elt_floatcst (pstate, $1.val);
839 write_exp_elt_opcode (pstate, OP_FLOAT);
840 write_exp_elt_opcode (pstate, OP_COMPLEX);
841 write_exp_elt_type (pstate, $1.type);
842 write_exp_elt_opcode (pstate, OP_COMPLEX);
843 }
844 ;
845
846 exp : variable
847 ;
848
849 exp : DOLLAR_VARIABLE
850 {
851 write_dollar_variable (pstate, $1);
852 }
853 ;
854
855 exp : SELECTOR '(' name ')'
856 {
857 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
858 write_exp_string (pstate, $3);
859 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
860 ;
861
862 exp : SIZEOF '(' type ')' %prec UNARY
863 { struct type *type = $3;
864 write_exp_elt_opcode (pstate, OP_LONG);
865 write_exp_elt_type (pstate, lookup_signed_typename
866 (pstate->language (),
867 "int"));
868 type = check_typedef (type);
869
870 /* $5.3.3/2 of the C++ Standard (n3290 draft)
871 says of sizeof: "When applied to a reference
872 or a reference type, the result is the size of
873 the referenced type." */
874 if (TYPE_IS_REFERENCE (type))
875 type = check_typedef (TYPE_TARGET_TYPE (type));
876 write_exp_elt_longcst (pstate,
877 (LONGEST) TYPE_LENGTH (type));
878 write_exp_elt_opcode (pstate, OP_LONG); }
879 ;
880
881 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
882 { write_exp_elt_opcode (pstate,
883 UNOP_REINTERPRET_CAST); }
884 ;
885
886 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
887 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
888 ;
889
890 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
891 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
892 ;
893
894 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
895 { /* We could do more error checking here, but
896 it doesn't seem worthwhile. */
897 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
898 ;
899
900 string_exp:
901 STRING
902 {
903 /* We copy the string here, and not in the
904 lexer, to guarantee that we do not leak a
905 string. Note that we follow the
906 NUL-termination convention of the
907 lexer. */
908 struct typed_stoken *vec = XNEW (struct typed_stoken);
909 $$.len = 1;
910 $$.tokens = vec;
911
912 vec->type = $1.type;
913 vec->length = $1.length;
914 vec->ptr = (char *) malloc ($1.length + 1);
915 memcpy (vec->ptr, $1.ptr, $1.length + 1);
916 }
917
918 | string_exp STRING
919 {
920 /* Note that we NUL-terminate here, but just
921 for convenience. */
922 char *p;
923 ++$$.len;
924 $$.tokens = XRESIZEVEC (struct typed_stoken,
925 $$.tokens, $$.len);
926
927 p = (char *) malloc ($2.length + 1);
928 memcpy (p, $2.ptr, $2.length + 1);
929
930 $$.tokens[$$.len - 1].type = $2.type;
931 $$.tokens[$$.len - 1].length = $2.length;
932 $$.tokens[$$.len - 1].ptr = p;
933 }
934 ;
935
936 exp : string_exp
937 {
938 int i;
939 c_string_type type = C_STRING;
940
941 for (i = 0; i < $1.len; ++i)
942 {
943 switch ($1.tokens[i].type)
944 {
945 case C_STRING:
946 break;
947 case C_WIDE_STRING:
948 case C_STRING_16:
949 case C_STRING_32:
950 if (type != C_STRING
951 && type != $1.tokens[i].type)
952 error (_("Undefined string concatenation."));
953 type = (enum c_string_type_values) $1.tokens[i].type;
954 break;
955 default:
956 /* internal error */
957 internal_error (__FILE__, __LINE__,
958 "unrecognized type in string concatenation");
959 }
960 }
961
962 write_exp_string_vector (pstate, type, &$1);
963 for (i = 0; i < $1.len; ++i)
964 free ($1.tokens[i].ptr);
965 free ($1.tokens);
966 }
967 ;
968
969 exp : NSSTRING /* ObjC NextStep NSString constant
970 * of the form '@' '"' string '"'.
971 */
972 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
973 write_exp_string (pstate, $1);
974 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
975 ;
976
977 /* C++. */
978 exp : TRUEKEYWORD
979 { write_exp_elt_opcode (pstate, OP_LONG);
980 write_exp_elt_type (pstate,
981 parse_type (pstate)->builtin_bool);
982 write_exp_elt_longcst (pstate, (LONGEST) 1);
983 write_exp_elt_opcode (pstate, OP_LONG); }
984 ;
985
986 exp : FALSEKEYWORD
987 { write_exp_elt_opcode (pstate, OP_LONG);
988 write_exp_elt_type (pstate,
989 parse_type (pstate)->builtin_bool);
990 write_exp_elt_longcst (pstate, (LONGEST) 0);
991 write_exp_elt_opcode (pstate, OP_LONG); }
992 ;
993
994 /* end of C++. */
995
996 block : BLOCKNAME
997 {
998 if ($1.sym.symbol)
999 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
1000 else
1001 error (_("No file or function \"%s\"."),
1002 copy_name ($1.stoken).c_str ());
1003 }
1004 | FILENAME
1005 {
1006 $$ = $1;
1007 }
1008 ;
1009
1010 block : block COLONCOLON name
1011 {
1012 std::string copy = copy_name ($3);
1013 struct symbol *tem
1014 = lookup_symbol (copy.c_str (), $1,
1015 VAR_DOMAIN, NULL).symbol;
1016
1017 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
1018 error (_("No function \"%s\" in specified context."),
1019 copy.c_str ());
1020 $$ = SYMBOL_BLOCK_VALUE (tem); }
1021 ;
1022
1023 variable: name_not_typename ENTRY
1024 { struct symbol *sym = $1.sym.symbol;
1025
1026 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
1027 || !symbol_read_needs_frame (sym))
1028 error (_("@entry can be used only for function "
1029 "parameters, not for \"%s\""),
1030 copy_name ($1.stoken).c_str ());
1031
1032 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
1033 write_exp_elt_sym (pstate, sym);
1034 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
1035 }
1036 ;
1037
1038 variable: block COLONCOLON name
1039 {
1040 std::string copy = copy_name ($3);
1041 struct block_symbol sym
1042 = lookup_symbol (copy.c_str (), $1,
1043 VAR_DOMAIN, NULL);
1044
1045 if (sym.symbol == 0)
1046 error (_("No symbol \"%s\" in specified context."),
1047 copy.c_str ());
1048 if (symbol_read_needs_frame (sym.symbol))
1049 pstate->block_tracker->update (sym);
1050
1051 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1052 write_exp_elt_block (pstate, sym.block);
1053 write_exp_elt_sym (pstate, sym.symbol);
1054 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1055 ;
1056
1057 qualified_name: TYPENAME COLONCOLON name
1058 {
1059 struct type *type = $1.type;
1060 type = check_typedef (type);
1061 if (!type_aggregate_p (type))
1062 error (_("`%s' is not defined as an aggregate type."),
1063 TYPE_SAFE_NAME (type));
1064
1065 write_exp_elt_opcode (pstate, OP_SCOPE);
1066 write_exp_elt_type (pstate, type);
1067 write_exp_string (pstate, $3);
1068 write_exp_elt_opcode (pstate, OP_SCOPE);
1069 }
1070 | TYPENAME COLONCOLON '~' name
1071 {
1072 struct type *type = $1.type;
1073 struct stoken tmp_token;
1074 char *buf;
1075
1076 type = check_typedef (type);
1077 if (!type_aggregate_p (type))
1078 error (_("`%s' is not defined as an aggregate type."),
1079 TYPE_SAFE_NAME (type));
1080 buf = (char *) alloca ($4.length + 2);
1081 tmp_token.ptr = buf;
1082 tmp_token.length = $4.length + 1;
1083 buf[0] = '~';
1084 memcpy (buf+1, $4.ptr, $4.length);
1085 buf[tmp_token.length] = 0;
1086
1087 /* Check for valid destructor name. */
1088 destructor_name_p (tmp_token.ptr, $1.type);
1089 write_exp_elt_opcode (pstate, OP_SCOPE);
1090 write_exp_elt_type (pstate, type);
1091 write_exp_string (pstate, tmp_token);
1092 write_exp_elt_opcode (pstate, OP_SCOPE);
1093 }
1094 | TYPENAME COLONCOLON name COLONCOLON name
1095 {
1096 std::string copy = copy_name ($3);
1097 error (_("No type \"%s\" within class "
1098 "or namespace \"%s\"."),
1099 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1100 }
1101 ;
1102
1103 variable: qualified_name
1104 | COLONCOLON name_not_typename
1105 {
1106 std::string name = copy_name ($2.stoken);
1107 struct symbol *sym;
1108 struct bound_minimal_symbol msymbol;
1109
1110 sym
1111 = lookup_symbol (name.c_str (),
1112 (const struct block *) NULL,
1113 VAR_DOMAIN, NULL).symbol;
1114 if (sym)
1115 {
1116 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1117 write_exp_elt_block (pstate, NULL);
1118 write_exp_elt_sym (pstate, sym);
1119 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1120 break;
1121 }
1122
1123 msymbol = lookup_bound_minimal_symbol (name.c_str ());
1124 if (msymbol.minsym != NULL)
1125 write_exp_msymbol (pstate, msymbol);
1126 else if (!have_full_symbols () && !have_partial_symbols ())
1127 error (_("No symbol table is loaded. Use the \"file\" command."));
1128 else
1129 error (_("No symbol \"%s\" in current context."),
1130 name.c_str ());
1131 }
1132 ;
1133
1134 variable: name_not_typename
1135 { struct block_symbol sym = $1.sym;
1136
1137 if (sym.symbol)
1138 {
1139 if (symbol_read_needs_frame (sym.symbol))
1140 pstate->block_tracker->update (sym);
1141
1142 /* If we found a function, see if it's
1143 an ifunc resolver that has the same
1144 address as the ifunc symbol itself.
1145 If so, prefer the ifunc symbol. */
1146
1147 bound_minimal_symbol resolver
1148 = find_gnu_ifunc (sym.symbol);
1149 if (resolver.minsym != NULL)
1150 write_exp_msymbol (pstate, resolver);
1151 else
1152 {
1153 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1154 write_exp_elt_block (pstate, sym.block);
1155 write_exp_elt_sym (pstate, sym.symbol);
1156 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1157 }
1158 }
1159 else if ($1.is_a_field_of_this)
1160 {
1161 /* C++: it hangs off of `this'. Must
1162 not inadvertently convert from a method call
1163 to data ref. */
1164 pstate->block_tracker->update (sym);
1165 write_exp_elt_opcode (pstate, OP_THIS);
1166 write_exp_elt_opcode (pstate, OP_THIS);
1167 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1168 write_exp_string (pstate, $1.stoken);
1169 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1170 }
1171 else
1172 {
1173 std::string arg = copy_name ($1.stoken);
1174
1175 bound_minimal_symbol msymbol
1176 = lookup_bound_minimal_symbol (arg.c_str ());
1177 if (msymbol.minsym == NULL)
1178 {
1179 if (!have_full_symbols () && !have_partial_symbols ())
1180 error (_("No symbol table is loaded. Use the \"file\" command."));
1181 else
1182 error (_("No symbol \"%s\" in current context."),
1183 arg.c_str ());
1184 }
1185
1186 /* This minsym might be an alias for
1187 another function. See if we can find
1188 the debug symbol for the target, and
1189 if so, use it instead, since it has
1190 return type / prototype info. This
1191 is important for example for "p
1192 *__errno_location()". */
1193 symbol *alias_target
1194 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1195 && msymbol.minsym->type != mst_data_gnu_ifunc)
1196 ? find_function_alias_target (msymbol)
1197 : NULL);
1198 if (alias_target != NULL)
1199 {
1200 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1201 write_exp_elt_block
1202 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1203 write_exp_elt_sym (pstate, alias_target);
1204 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1205 }
1206 else
1207 write_exp_msymbol (pstate, msymbol);
1208 }
1209 }
1210 ;
1211
1212 const_or_volatile: const_or_volatile_noopt
1213 |
1214 ;
1215
1216 single_qualifier:
1217 CONST_KEYWORD
1218 { cpstate->type_stack.insert (tp_const); }
1219 | VOLATILE_KEYWORD
1220 { cpstate->type_stack.insert (tp_volatile); }
1221 | ATOMIC
1222 { cpstate->type_stack.insert (tp_atomic); }
1223 | RESTRICT
1224 { cpstate->type_stack.insert (tp_restrict); }
1225 | CAPABILITY
1226 { cpstate->type_stack.insert (tp_capability); }
1227 | '@' NAME
1228 {
1229 cpstate->type_stack.insert (pstate,
1230 copy_name ($2.stoken).c_str ());
1231 }
1232 ;
1233
1234 qualifier_seq_noopt:
1235 single_qualifier
1236 | qualifier_seq single_qualifier
1237 ;
1238
1239 qualifier_seq:
1240 qualifier_seq_noopt
1241 |
1242 ;
1243
1244 ptr_operator:
1245 ptr_operator '*'
1246 { cpstate->type_stack.insert (tp_pointer); }
1247 qualifier_seq
1248 | '*'
1249 { cpstate->type_stack.insert (tp_pointer); }
1250 qualifier_seq
1251 | '&'
1252 { cpstate->type_stack.insert (tp_reference); }
1253 | '&' ptr_operator
1254 { cpstate->type_stack.insert (tp_reference); }
1255 | ANDAND
1256 { cpstate->type_stack.insert (tp_rvalue_reference); }
1257 | ANDAND ptr_operator
1258 { cpstate->type_stack.insert (tp_rvalue_reference); }
1259 ;
1260
1261 ptr_operator_ts: ptr_operator
1262 {
1263 $$ = cpstate->type_stack.create ();
1264 cpstate->type_stacks.emplace_back ($$);
1265 }
1266 ;
1267
1268 abs_decl: ptr_operator_ts direct_abs_decl
1269 { $$ = $2->append ($1); }
1270 | ptr_operator_ts
1271 | direct_abs_decl
1272 ;
1273
1274 direct_abs_decl: '(' abs_decl ')'
1275 { $$ = $2; }
1276 | direct_abs_decl array_mod
1277 {
1278 cpstate->type_stack.push ($1);
1279 cpstate->type_stack.push ($2);
1280 cpstate->type_stack.push (tp_array);
1281 $$ = cpstate->type_stack.create ();
1282 cpstate->type_stacks.emplace_back ($$);
1283 }
1284 | array_mod
1285 {
1286 cpstate->type_stack.push ($1);
1287 cpstate->type_stack.push (tp_array);
1288 $$ = cpstate->type_stack.create ();
1289 cpstate->type_stacks.emplace_back ($$);
1290 }
1291
1292 | direct_abs_decl func_mod
1293 {
1294 cpstate->type_stack.push ($1);
1295 cpstate->type_stack.push ($2);
1296 $$ = cpstate->type_stack.create ();
1297 cpstate->type_stacks.emplace_back ($$);
1298 }
1299 | func_mod
1300 {
1301 cpstate->type_stack.push ($1);
1302 $$ = cpstate->type_stack.create ();
1303 cpstate->type_stacks.emplace_back ($$);
1304 }
1305 ;
1306
1307 array_mod: '[' ']'
1308 { $$ = -1; }
1309 | OBJC_LBRAC ']'
1310 { $$ = -1; }
1311 | '[' INT ']'
1312 { $$ = $2.val; }
1313 | OBJC_LBRAC INT ']'
1314 { $$ = $2.val; }
1315 ;
1316
1317 func_mod: '(' ')'
1318 {
1319 $$ = new std::vector<struct type *>;
1320 cpstate->type_lists.emplace_back ($$);
1321 }
1322 | '(' parameter_typelist ')'
1323 { $$ = $2; }
1324 ;
1325
1326 /* We used to try to recognize pointer to member types here, but
1327 that didn't work (shift/reduce conflicts meant that these rules never
1328 got executed). The problem is that
1329 int (foo::bar::baz::bizzle)
1330 is a function type but
1331 int (foo::bar::baz::bizzle::*)
1332 is a pointer to member type. Stroustrup loses again! */
1333
1334 type : ptype
1335 ;
1336
1337 /* A helper production that recognizes scalar types that can validly
1338 be used with _Complex. */
1339
1340 scalar_type:
1341 INT_KEYWORD
1342 { $$ = lookup_signed_typename (pstate->language (),
1343 "int"); }
1344 | LONG
1345 { $$ = lookup_signed_typename (pstate->language (),
1346 "long"); }
1347 | SHORT
1348 { $$ = lookup_signed_typename (pstate->language (),
1349 "short"); }
1350 | LONG INT_KEYWORD
1351 { $$ = lookup_signed_typename (pstate->language (),
1352 "long"); }
1353 | LONG SIGNED_KEYWORD INT_KEYWORD
1354 { $$ = lookup_signed_typename (pstate->language (),
1355 "long"); }
1356 | LONG SIGNED_KEYWORD
1357 { $$ = lookup_signed_typename (pstate->language (),
1358 "long"); }
1359 | SIGNED_KEYWORD LONG INT_KEYWORD
1360 { $$ = lookup_signed_typename (pstate->language (),
1361 "long"); }
1362 | UNSIGNED LONG INT_KEYWORD
1363 { $$ = lookup_unsigned_typename (pstate->language (),
1364 "long"); }
1365 | LONG UNSIGNED INT_KEYWORD
1366 { $$ = lookup_unsigned_typename (pstate->language (),
1367 "long"); }
1368 | LONG UNSIGNED
1369 { $$ = lookup_unsigned_typename (pstate->language (),
1370 "long"); }
1371 | LONG LONG
1372 { $$ = lookup_signed_typename (pstate->language (),
1373 "long long"); }
1374 | LONG LONG INT_KEYWORD
1375 { $$ = lookup_signed_typename (pstate->language (),
1376 "long long"); }
1377 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1378 { $$ = lookup_signed_typename (pstate->language (),
1379 "long long"); }
1380 | LONG LONG SIGNED_KEYWORD
1381 { $$ = lookup_signed_typename (pstate->language (),
1382 "long long"); }
1383 | SIGNED_KEYWORD LONG LONG
1384 { $$ = lookup_signed_typename (pstate->language (),
1385 "long long"); }
1386 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1387 { $$ = lookup_signed_typename (pstate->language (),
1388 "long long"); }
1389 | UNSIGNED LONG LONG
1390 { $$ = lookup_unsigned_typename (pstate->language (),
1391 "long long"); }
1392 | UNSIGNED LONG LONG INT_KEYWORD
1393 { $$ = lookup_unsigned_typename (pstate->language (),
1394 "long long"); }
1395 | LONG LONG UNSIGNED
1396 { $$ = lookup_unsigned_typename (pstate->language (),
1397 "long long"); }
1398 | LONG LONG UNSIGNED INT_KEYWORD
1399 { $$ = lookup_unsigned_typename (pstate->language (),
1400 "long long"); }
1401 | SHORT INT_KEYWORD
1402 { $$ = lookup_signed_typename (pstate->language (),
1403 "short"); }
1404 | SHORT SIGNED_KEYWORD INT_KEYWORD
1405 { $$ = lookup_signed_typename (pstate->language (),
1406 "short"); }
1407 | SHORT SIGNED_KEYWORD
1408 { $$ = lookup_signed_typename (pstate->language (),
1409 "short"); }
1410 | UNSIGNED SHORT INT_KEYWORD
1411 { $$ = lookup_unsigned_typename (pstate->language (),
1412 "short"); }
1413 | SHORT UNSIGNED
1414 { $$ = lookup_unsigned_typename (pstate->language (),
1415 "short"); }
1416 | SHORT UNSIGNED INT_KEYWORD
1417 { $$ = lookup_unsigned_typename (pstate->language (),
1418 "short"); }
1419 | DOUBLE_KEYWORD
1420 { $$ = lookup_typename (pstate->language (),
1421 "double",
1422 NULL,
1423 0); }
1424 | FLOAT_KEYWORD
1425 { $$ = lookup_typename (pstate->language (),
1426 "float",
1427 NULL,
1428 0); }
1429 | INTCAP_KEYWORD
1430 { $$ = lookup_typename (pstate->language (),
1431 "__intcap_t",
1432 NULL,
1433 0); }
1434 | UINTCAP_KEYWORD
1435 { $$ = lookup_typename (pstate->language (),
1436 "__uintcap_t",
1437 NULL,
1438 0); }
1439 | LONG DOUBLE_KEYWORD
1440 { $$ = lookup_typename (pstate->language (),
1441 "long double",
1442 NULL,
1443 0); }
1444 | UNSIGNED type_name
1445 { $$ = lookup_unsigned_typename (pstate->language (),
1446 $2.type->name ()); }
1447 | UNSIGNED
1448 { $$ = lookup_unsigned_typename (pstate->language (),
1449 "int"); }
1450 | SIGNED_KEYWORD type_name
1451 { $$ = lookup_signed_typename (pstate->language (),
1452 $2.type->name ()); }
1453 | SIGNED_KEYWORD
1454 { $$ = lookup_signed_typename (pstate->language (),
1455 "int"); }
1456 ;
1457
1458 /* Implements (approximately): (type-qualifier)* type-specifier.
1459
1460 When type-specifier is only ever a single word, like 'float' then these
1461 arrive as pre-built TYPENAME tokens thanks to the classify_name
1462 function. However, when a type-specifier can contain multiple words,
1463 for example 'double' can appear as just 'double' or 'long double', and
1464 similarly 'long' can appear as just 'long' or in 'long double', then
1465 these type-specifiers are parsed into their own tokens in the function
1466 lex_one_token and the ident_tokens array. These separate tokens are all
1467 recognised here. */
1468 typebase
1469 : TYPENAME
1470 { $$ = $1.type; }
1471 | scalar_type
1472 { $$ = $1; }
1473 | COMPLEX scalar_type
1474 {
1475 $$ = init_complex_type (nullptr, $2);
1476 }
1477 | STRUCT name
1478 { $$
1479 = lookup_struct (copy_name ($2).c_str (),
1480 pstate->expression_context_block);
1481 }
1482 | STRUCT COMPLETE
1483 {
1484 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1485 "", 0);
1486 $$ = NULL;
1487 }
1488 | STRUCT name COMPLETE
1489 {
1490 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1491 $2.ptr, $2.length);
1492 $$ = NULL;
1493 }
1494 | CLASS name
1495 { $$ = lookup_struct
1496 (copy_name ($2).c_str (),
1497 pstate->expression_context_block);
1498 }
1499 | CLASS COMPLETE
1500 {
1501 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1502 "", 0);
1503 $$ = NULL;
1504 }
1505 | CLASS name COMPLETE
1506 {
1507 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1508 $2.ptr, $2.length);
1509 $$ = NULL;
1510 }
1511 | UNION name
1512 { $$
1513 = lookup_union (copy_name ($2).c_str (),
1514 pstate->expression_context_block);
1515 }
1516 | UNION COMPLETE
1517 {
1518 pstate->mark_completion_tag (TYPE_CODE_UNION,
1519 "", 0);
1520 $$ = NULL;
1521 }
1522 | UNION name COMPLETE
1523 {
1524 pstate->mark_completion_tag (TYPE_CODE_UNION,
1525 $2.ptr, $2.length);
1526 $$ = NULL;
1527 }
1528 | ENUM name
1529 { $$ = lookup_enum (copy_name ($2).c_str (),
1530 pstate->expression_context_block);
1531 }
1532 | ENUM COMPLETE
1533 {
1534 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1535 $$ = NULL;
1536 }
1537 | ENUM name COMPLETE
1538 {
1539 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1540 $2.length);
1541 $$ = NULL;
1542 }
1543 /* It appears that this rule for templates is never
1544 reduced; template recognition happens by lookahead
1545 in the token processing code in yylex. */
1546 | TEMPLATE name '<' type '>'
1547 { $$ = lookup_template_type
1548 (copy_name($2).c_str (), $4,
1549 pstate->expression_context_block);
1550 }
1551 | qualifier_seq_noopt typebase
1552 { $$ = cpstate->type_stack.follow_types ($2); }
1553 | typebase qualifier_seq_noopt
1554 { $$ = cpstate->type_stack.follow_types ($1); }
1555 ;
1556
1557 type_name: TYPENAME
1558 | INT_KEYWORD
1559 {
1560 $$.stoken.ptr = "int";
1561 $$.stoken.length = 3;
1562 $$.type = lookup_signed_typename (pstate->language (),
1563 "int");
1564 }
1565 | LONG
1566 {
1567 $$.stoken.ptr = "long";
1568 $$.stoken.length = 4;
1569 $$.type = lookup_signed_typename (pstate->language (),
1570 "long");
1571 }
1572 | SHORT
1573 {
1574 $$.stoken.ptr = "short";
1575 $$.stoken.length = 5;
1576 $$.type = lookup_signed_typename (pstate->language (),
1577 "short");
1578 }
1579 ;
1580
1581 parameter_typelist:
1582 nonempty_typelist
1583 { check_parameter_typelist ($1); }
1584 | nonempty_typelist ',' DOTDOTDOT
1585 {
1586 $1->push_back (NULL);
1587 check_parameter_typelist ($1);
1588 $$ = $1;
1589 }
1590 ;
1591
1592 nonempty_typelist
1593 : type
1594 {
1595 std::vector<struct type *> *typelist
1596 = new std::vector<struct type *>;
1597 cpstate->type_lists.emplace_back (typelist);
1598
1599 typelist->push_back ($1);
1600 $$ = typelist;
1601 }
1602 | nonempty_typelist ',' type
1603 {
1604 $1->push_back ($3);
1605 $$ = $1;
1606 }
1607 ;
1608
1609 ptype : typebase
1610 | ptype abs_decl
1611 {
1612 cpstate->type_stack.push ($2);
1613 $$ = cpstate->type_stack.follow_types ($1);
1614 }
1615 ;
1616
1617 conversion_type_id: typebase conversion_declarator
1618 { $$ = cpstate->type_stack.follow_types ($1); }
1619 ;
1620
1621 conversion_declarator: /* Nothing. */
1622 | ptr_operator conversion_declarator
1623 ;
1624
1625 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1626 | VOLATILE_KEYWORD CONST_KEYWORD
1627 ;
1628
1629 const_or_volatile_noopt: const_and_volatile
1630 { cpstate->type_stack.insert (tp_const);
1631 cpstate->type_stack.insert (tp_volatile);
1632 }
1633 | CONST_KEYWORD
1634 { cpstate->type_stack.insert (tp_const); }
1635 | VOLATILE_KEYWORD
1636 { cpstate->type_stack.insert (tp_volatile); }
1637 ;
1638
1639 oper: OPERATOR NEW
1640 { $$ = operator_stoken (" new"); }
1641 | OPERATOR DELETE
1642 { $$ = operator_stoken (" delete"); }
1643 | OPERATOR NEW '[' ']'
1644 { $$ = operator_stoken (" new[]"); }
1645 | OPERATOR DELETE '[' ']'
1646 { $$ = operator_stoken (" delete[]"); }
1647 | OPERATOR NEW OBJC_LBRAC ']'
1648 { $$ = operator_stoken (" new[]"); }
1649 | OPERATOR DELETE OBJC_LBRAC ']'
1650 { $$ = operator_stoken (" delete[]"); }
1651 | OPERATOR '+'
1652 { $$ = operator_stoken ("+"); }
1653 | OPERATOR '-'
1654 { $$ = operator_stoken ("-"); }
1655 | OPERATOR '*'
1656 { $$ = operator_stoken ("*"); }
1657 | OPERATOR '/'
1658 { $$ = operator_stoken ("/"); }
1659 | OPERATOR '%'
1660 { $$ = operator_stoken ("%"); }
1661 | OPERATOR '^'
1662 { $$ = operator_stoken ("^"); }
1663 | OPERATOR '&'
1664 { $$ = operator_stoken ("&"); }
1665 | OPERATOR '|'
1666 { $$ = operator_stoken ("|"); }
1667 | OPERATOR '~'
1668 { $$ = operator_stoken ("~"); }
1669 | OPERATOR '!'
1670 { $$ = operator_stoken ("!"); }
1671 | OPERATOR '='
1672 { $$ = operator_stoken ("="); }
1673 | OPERATOR '<'
1674 { $$ = operator_stoken ("<"); }
1675 | OPERATOR '>'
1676 { $$ = operator_stoken (">"); }
1677 | OPERATOR ASSIGN_MODIFY
1678 { const char *op = " unknown";
1679 switch ($2)
1680 {
1681 case BINOP_RSH:
1682 op = ">>=";
1683 break;
1684 case BINOP_LSH:
1685 op = "<<=";
1686 break;
1687 case BINOP_ADD:
1688 op = "+=";
1689 break;
1690 case BINOP_SUB:
1691 op = "-=";
1692 break;
1693 case BINOP_MUL:
1694 op = "*=";
1695 break;
1696 case BINOP_DIV:
1697 op = "/=";
1698 break;
1699 case BINOP_REM:
1700 op = "%=";
1701 break;
1702 case BINOP_BITWISE_IOR:
1703 op = "|=";
1704 break;
1705 case BINOP_BITWISE_AND:
1706 op = "&=";
1707 break;
1708 case BINOP_BITWISE_XOR:
1709 op = "^=";
1710 break;
1711 default:
1712 break;
1713 }
1714
1715 $$ = operator_stoken (op);
1716 }
1717 | OPERATOR LSH
1718 { $$ = operator_stoken ("<<"); }
1719 | OPERATOR RSH
1720 { $$ = operator_stoken (">>"); }
1721 | OPERATOR EQUAL
1722 { $$ = operator_stoken ("=="); }
1723 | OPERATOR NOTEQUAL
1724 { $$ = operator_stoken ("!="); }
1725 | OPERATOR LEQ
1726 { $$ = operator_stoken ("<="); }
1727 | OPERATOR GEQ
1728 { $$ = operator_stoken (">="); }
1729 | OPERATOR ANDAND
1730 { $$ = operator_stoken ("&&"); }
1731 | OPERATOR OROR
1732 { $$ = operator_stoken ("||"); }
1733 | OPERATOR INCREMENT
1734 { $$ = operator_stoken ("++"); }
1735 | OPERATOR DECREMENT
1736 { $$ = operator_stoken ("--"); }
1737 | OPERATOR ','
1738 { $$ = operator_stoken (","); }
1739 | OPERATOR ARROW_STAR
1740 { $$ = operator_stoken ("->*"); }
1741 | OPERATOR ARROW
1742 { $$ = operator_stoken ("->"); }
1743 | OPERATOR '(' ')'
1744 { $$ = operator_stoken ("()"); }
1745 | OPERATOR '[' ']'
1746 { $$ = operator_stoken ("[]"); }
1747 | OPERATOR OBJC_LBRAC ']'
1748 { $$ = operator_stoken ("[]"); }
1749 | OPERATOR conversion_type_id
1750 { string_file buf;
1751
1752 c_print_type ($2, NULL, &buf, -1, 0,
1753 &type_print_raw_options);
1754 std::string name = std::move (buf.string ());
1755
1756 /* This also needs canonicalization. */
1757 gdb::unique_xmalloc_ptr<char> canon
1758 = cp_canonicalize_string (name.c_str ());
1759 if (canon != nullptr)
1760 name = canon.get ();
1761 $$ = operator_stoken ((" " + name).c_str ());
1762 }
1763 ;
1764
1765 /* This rule exists in order to allow some tokens that would not normally
1766 match the 'name' rule to appear as fields within a struct. The example
1767 that initially motivated this was the RISC-V target which models the
1768 floating point registers as a union with fields called 'float' and
1769 'double'. */
1770 field_name
1771 : name
1772 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1773 | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
1774 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1775 | LONG { $$ = typename_stoken ("long"); }
1776 | SHORT { $$ = typename_stoken ("short"); }
1777 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1778 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1779 ;
1780
1781 name : NAME { $$ = $1.stoken; }
1782 | BLOCKNAME { $$ = $1.stoken; }
1783 | TYPENAME { $$ = $1.stoken; }
1784 | NAME_OR_INT { $$ = $1.stoken; }
1785 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1786 | oper { $$ = $1; }
1787 ;
1788
1789 name_not_typename : NAME
1790 | BLOCKNAME
1791 /* These would be useful if name_not_typename was useful, but it is just
1792 a fake for "variable", so these cause reduce/reduce conflicts because
1793 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1794 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1795 context where only a name could occur, this might be useful.
1796 | NAME_OR_INT
1797 */
1798 | oper
1799 {
1800 struct field_of_this_result is_a_field_of_this;
1801
1802 $$.stoken = $1;
1803 $$.sym
1804 = lookup_symbol ($1.ptr,
1805 pstate->expression_context_block,
1806 VAR_DOMAIN,
1807 &is_a_field_of_this);
1808 $$.is_a_field_of_this
1809 = is_a_field_of_this.type != NULL;
1810 }
1811 | UNKNOWN_CPP_NAME
1812 ;
1813
1814 %%
1815
1816 /* Like write_exp_string, but prepends a '~'. */
1817
1818 static void
1819 write_destructor_name (struct parser_state *par_state, struct stoken token)
1820 {
1821 char *copy = (char *) alloca (token.length + 1);
1822
1823 copy[0] = '~';
1824 memcpy (&copy[1], token.ptr, token.length);
1825
1826 token.ptr = copy;
1827 ++token.length;
1828
1829 write_exp_string (par_state, token);
1830 }
1831
1832 /* Returns a stoken of the operator name given by OP (which does not
1833 include the string "operator"). */
1834
1835 static struct stoken
1836 operator_stoken (const char *op)
1837 {
1838 struct stoken st = { NULL, 0 };
1839 char *buf;
1840
1841 st.length = CP_OPERATOR_LEN + strlen (op);
1842 buf = (char *) malloc (st.length + 1);
1843 strcpy (buf, CP_OPERATOR_STR);
1844 strcat (buf, op);
1845 st.ptr = buf;
1846
1847 /* The toplevel (c_parse) will free the memory allocated here. */
1848 cpstate->strings.emplace_back (buf);
1849 return st;
1850 };
1851
1852 /* Returns a stoken of the type named TYPE. */
1853
1854 static struct stoken
1855 typename_stoken (const char *type)
1856 {
1857 struct stoken st = { type, 0 };
1858 st.length = strlen (type);
1859 return st;
1860 };
1861
1862 /* Return true if the type is aggregate-like. */
1863
1864 static int
1865 type_aggregate_p (struct type *type)
1866 {
1867 return (type->code () == TYPE_CODE_STRUCT
1868 || type->code () == TYPE_CODE_UNION
1869 || type->code () == TYPE_CODE_NAMESPACE
1870 || (type->code () == TYPE_CODE_ENUM
1871 && TYPE_DECLARED_CLASS (type)));
1872 }
1873
1874 /* Validate a parameter typelist. */
1875
1876 static void
1877 check_parameter_typelist (std::vector<struct type *> *params)
1878 {
1879 struct type *type;
1880 int ix;
1881
1882 for (ix = 0; ix < params->size (); ++ix)
1883 {
1884 type = (*params)[ix];
1885 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1886 {
1887 if (ix == 0)
1888 {
1889 if (params->size () == 1)
1890 {
1891 /* Ok. */
1892 break;
1893 }
1894 error (_("parameter types following 'void'"));
1895 }
1896 else
1897 error (_("'void' invalid as parameter type"));
1898 }
1899 }
1900 }
1901
1902 /* Take care of parsing a number (anything that starts with a digit).
1903 Set yylval and return the token type; update lexptr.
1904 LEN is the number of characters in it. */
1905
1906 /*** Needs some error checking for the float case ***/
1907
1908 static int
1909 parse_number (struct parser_state *par_state,
1910 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1911 {
1912 ULONGEST n = 0;
1913 ULONGEST prevn = 0;
1914 ULONGEST un;
1915
1916 int i = 0;
1917 int c;
1918 int base = input_radix;
1919 int unsigned_p = 0;
1920
1921 /* Number of "L" suffixes encountered. */
1922 int long_p = 0;
1923
1924 /* Imaginary number. */
1925 bool imaginary_p = false;
1926
1927 /* We have found a "L" or "U" (or "i") suffix. */
1928 int found_suffix = 0;
1929
1930 ULONGEST high_bit;
1931 struct type *signed_type;
1932 struct type *unsigned_type;
1933 char *p;
1934
1935 p = (char *) alloca (len);
1936 memcpy (p, buf, len);
1937
1938 if (parsed_float)
1939 {
1940 if (len >= 1 && p[len - 1] == 'i')
1941 {
1942 imaginary_p = true;
1943 --len;
1944 }
1945
1946 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1947 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1948 {
1949 putithere->typed_val_float.type
1950 = parse_type (par_state)->builtin_decfloat;
1951 len -= 2;
1952 }
1953 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1954 {
1955 putithere->typed_val_float.type
1956 = parse_type (par_state)->builtin_decdouble;
1957 len -= 2;
1958 }
1959 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1960 {
1961 putithere->typed_val_float.type
1962 = parse_type (par_state)->builtin_declong;
1963 len -= 2;
1964 }
1965 /* Handle suffixes: 'f' for float, 'l' for long double. */
1966 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1967 {
1968 putithere->typed_val_float.type
1969 = parse_type (par_state)->builtin_float;
1970 len -= 1;
1971 }
1972 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1973 {
1974 putithere->typed_val_float.type
1975 = parse_type (par_state)->builtin_long_double;
1976 len -= 1;
1977 }
1978 /* Default type for floating-point literals is double. */
1979 else
1980 {
1981 putithere->typed_val_float.type
1982 = parse_type (par_state)->builtin_double;
1983 }
1984
1985 if (!parse_float (p, len,
1986 putithere->typed_val_float.type,
1987 putithere->typed_val_float.val))
1988 return ERROR;
1989
1990 if (imaginary_p)
1991 putithere->typed_val_float.type
1992 = init_complex_type (nullptr, putithere->typed_val_float.type);
1993
1994 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
1995 }
1996
1997 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1998 if (p[0] == '0' && len > 1)
1999 switch (p[1])
2000 {
2001 case 'x':
2002 case 'X':
2003 if (len >= 3)
2004 {
2005 p += 2;
2006 base = 16;
2007 len -= 2;
2008 }
2009 break;
2010
2011 case 'b':
2012 case 'B':
2013 if (len >= 3)
2014 {
2015 p += 2;
2016 base = 2;
2017 len -= 2;
2018 }
2019 break;
2020
2021 case 't':
2022 case 'T':
2023 case 'd':
2024 case 'D':
2025 if (len >= 3)
2026 {
2027 p += 2;
2028 base = 10;
2029 len -= 2;
2030 }
2031 break;
2032
2033 default:
2034 base = 8;
2035 break;
2036 }
2037
2038 while (len-- > 0)
2039 {
2040 c = *p++;
2041 if (c >= 'A' && c <= 'Z')
2042 c += 'a' - 'A';
2043 if (c != 'l' && c != 'u' && c != 'i')
2044 n *= base;
2045 if (c >= '0' && c <= '9')
2046 {
2047 if (found_suffix)
2048 return ERROR;
2049 n += i = c - '0';
2050 }
2051 else
2052 {
2053 if (base > 10 && c >= 'a' && c <= 'f')
2054 {
2055 if (found_suffix)
2056 return ERROR;
2057 n += i = c - 'a' + 10;
2058 }
2059 else if (c == 'l')
2060 {
2061 ++long_p;
2062 found_suffix = 1;
2063 }
2064 else if (c == 'u')
2065 {
2066 unsigned_p = 1;
2067 found_suffix = 1;
2068 }
2069 else if (c == 'i')
2070 {
2071 imaginary_p = true;
2072 found_suffix = 1;
2073 }
2074 else
2075 return ERROR; /* Char not a digit */
2076 }
2077 if (i >= base)
2078 return ERROR; /* Invalid digit in this base */
2079
2080 /* Portably test for overflow (only works for nonzero values, so make
2081 a second check for zero). FIXME: Can't we just make n and prevn
2082 unsigned and avoid this? */
2083 if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
2084 unsigned_p = 1; /* Try something unsigned */
2085
2086 /* Portably test for unsigned overflow.
2087 FIXME: This check is wrong; for example it doesn't find overflow
2088 on 0x123456789 when LONGEST is 32 bits. */
2089 if (c != 'l' && c != 'u' && c != 'i' && n != 0)
2090 {
2091 if (unsigned_p && prevn >= n)
2092 error (_("Numeric constant too large."));
2093 }
2094 prevn = n;
2095 }
2096
2097 /* An integer constant is an int, a long, or a long long. An L
2098 suffix forces it to be long; an LL suffix forces it to be long
2099 long. If not forced to a larger size, it gets the first type of
2100 the above that it fits in. To figure out whether it fits, we
2101 shift it right and see whether anything remains. Note that we
2102 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2103 operation, because many compilers will warn about such a shift
2104 (which always produces a zero result). Sometimes gdbarch_int_bit
2105 or gdbarch_long_bit will be that big, sometimes not. To deal with
2106 the case where it is we just always shift the value more than
2107 once, with fewer bits each time. */
2108
2109 un = n >> 2;
2110 if (long_p == 0
2111 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2112 {
2113 high_bit
2114 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2115
2116 /* A large decimal (not hex or octal) constant (between INT_MAX
2117 and UINT_MAX) is a long or unsigned long, according to ANSI,
2118 never an unsigned int, but this code treats it as unsigned
2119 int. This probably should be fixed. GCC gives a warning on
2120 such constants. */
2121
2122 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2123 signed_type = parse_type (par_state)->builtin_int;
2124 }
2125 else if (long_p <= 1
2126 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2127 {
2128 high_bit
2129 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2130 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2131 signed_type = parse_type (par_state)->builtin_long;
2132 }
2133 else
2134 {
2135 int shift;
2136 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2137 < gdbarch_long_long_bit (par_state->gdbarch ()))
2138 /* A long long does not fit in a LONGEST. */
2139 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2140 else
2141 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2142 high_bit = (ULONGEST) 1 << shift;
2143 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2144 signed_type = parse_type (par_state)->builtin_long_long;
2145 }
2146
2147 putithere->typed_val_int.val = n;
2148
2149 /* If the high bit of the worked out type is set then this number
2150 has to be unsigned. */
2151
2152 if (unsigned_p || (n & high_bit))
2153 {
2154 putithere->typed_val_int.type = unsigned_type;
2155 }
2156 else
2157 {
2158 putithere->typed_val_int.type = signed_type;
2159 }
2160
2161 if (imaginary_p)
2162 putithere->typed_val_int.type
2163 = init_complex_type (nullptr, putithere->typed_val_int.type);
2164
2165 return imaginary_p ? COMPLEX_INT : INT;
2166 }
2167
2168 /* Temporary obstack used for holding strings. */
2169 static struct obstack tempbuf;
2170 static int tempbuf_init;
2171
2172 /* Parse a C escape sequence. The initial backslash of the sequence
2173 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2174 last character of the sequence. If OUTPUT is not NULL, the
2175 translated form of the escape sequence will be written there. If
2176 OUTPUT is NULL, no output is written and the call will only affect
2177 *PTR. If an escape sequence is expressed in target bytes, then the
2178 entire sequence will simply be copied to OUTPUT. Return 1 if any
2179 character was emitted, 0 otherwise. */
2180
2181 int
2182 c_parse_escape (const char **ptr, struct obstack *output)
2183 {
2184 const char *tokptr = *ptr;
2185 int result = 1;
2186
2187 /* Some escape sequences undergo character set conversion. Those we
2188 translate here. */
2189 switch (*tokptr)
2190 {
2191 /* Hex escapes do not undergo character set conversion, so keep
2192 the escape sequence for later. */
2193 case 'x':
2194 if (output)
2195 obstack_grow_str (output, "\\x");
2196 ++tokptr;
2197 if (!ISXDIGIT (*tokptr))
2198 error (_("\\x escape without a following hex digit"));
2199 while (ISXDIGIT (*tokptr))
2200 {
2201 if (output)
2202 obstack_1grow (output, *tokptr);
2203 ++tokptr;
2204 }
2205 break;
2206
2207 /* Octal escapes do not undergo character set conversion, so
2208 keep the escape sequence for later. */
2209 case '0':
2210 case '1':
2211 case '2':
2212 case '3':
2213 case '4':
2214 case '5':
2215 case '6':
2216 case '7':
2217 {
2218 int i;
2219 if (output)
2220 obstack_grow_str (output, "\\");
2221 for (i = 0;
2222 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2223 ++i)
2224 {
2225 if (output)
2226 obstack_1grow (output, *tokptr);
2227 ++tokptr;
2228 }
2229 }
2230 break;
2231
2232 /* We handle UCNs later. We could handle them here, but that
2233 would mean a spurious error in the case where the UCN could
2234 be converted to the target charset but not the host
2235 charset. */
2236 case 'u':
2237 case 'U':
2238 {
2239 char c = *tokptr;
2240 int i, len = c == 'U' ? 8 : 4;
2241 if (output)
2242 {
2243 obstack_1grow (output, '\\');
2244 obstack_1grow (output, *tokptr);
2245 }
2246 ++tokptr;
2247 if (!ISXDIGIT (*tokptr))
2248 error (_("\\%c escape without a following hex digit"), c);
2249 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2250 {
2251 if (output)
2252 obstack_1grow (output, *tokptr);
2253 ++tokptr;
2254 }
2255 }
2256 break;
2257
2258 /* We must pass backslash through so that it does not
2259 cause quoting during the second expansion. */
2260 case '\\':
2261 if (output)
2262 obstack_grow_str (output, "\\\\");
2263 ++tokptr;
2264 break;
2265
2266 /* Escapes which undergo conversion. */
2267 case 'a':
2268 if (output)
2269 obstack_1grow (output, '\a');
2270 ++tokptr;
2271 break;
2272 case 'b':
2273 if (output)
2274 obstack_1grow (output, '\b');
2275 ++tokptr;
2276 break;
2277 case 'f':
2278 if (output)
2279 obstack_1grow (output, '\f');
2280 ++tokptr;
2281 break;
2282 case 'n':
2283 if (output)
2284 obstack_1grow (output, '\n');
2285 ++tokptr;
2286 break;
2287 case 'r':
2288 if (output)
2289 obstack_1grow (output, '\r');
2290 ++tokptr;
2291 break;
2292 case 't':
2293 if (output)
2294 obstack_1grow (output, '\t');
2295 ++tokptr;
2296 break;
2297 case 'v':
2298 if (output)
2299 obstack_1grow (output, '\v');
2300 ++tokptr;
2301 break;
2302
2303 /* GCC extension. */
2304 case 'e':
2305 if (output)
2306 obstack_1grow (output, HOST_ESCAPE_CHAR);
2307 ++tokptr;
2308 break;
2309
2310 /* Backslash-newline expands to nothing at all. */
2311 case '\n':
2312 ++tokptr;
2313 result = 0;
2314 break;
2315
2316 /* A few escapes just expand to the character itself. */
2317 case '\'':
2318 case '\"':
2319 case '?':
2320 /* GCC extensions. */
2321 case '(':
2322 case '{':
2323 case '[':
2324 case '%':
2325 /* Unrecognized escapes turn into the character itself. */
2326 default:
2327 if (output)
2328 obstack_1grow (output, *tokptr);
2329 ++tokptr;
2330 break;
2331 }
2332 *ptr = tokptr;
2333 return result;
2334 }
2335
2336 /* Parse a string or character literal from TOKPTR. The string or
2337 character may be wide or unicode. *OUTPTR is set to just after the
2338 end of the literal in the input string. The resulting token is
2339 stored in VALUE. This returns a token value, either STRING or
2340 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2341 number of host characters in the literal. */
2342
2343 static int
2344 parse_string_or_char (const char *tokptr, const char **outptr,
2345 struct typed_stoken *value, int *host_chars)
2346 {
2347 int quote;
2348 c_string_type type;
2349 int is_objc = 0;
2350
2351 /* Build the gdb internal form of the input string in tempbuf. Note
2352 that the buffer is null byte terminated *only* for the
2353 convenience of debugging gdb itself and printing the buffer
2354 contents when the buffer contains no embedded nulls. Gdb does
2355 not depend upon the buffer being null byte terminated, it uses
2356 the length string instead. This allows gdb to handle C strings
2357 (as well as strings in other languages) with embedded null
2358 bytes */
2359
2360 if (!tempbuf_init)
2361 tempbuf_init = 1;
2362 else
2363 obstack_free (&tempbuf, NULL);
2364 obstack_init (&tempbuf);
2365
2366 /* Record the string type. */
2367 if (*tokptr == 'L')
2368 {
2369 type = C_WIDE_STRING;
2370 ++tokptr;
2371 }
2372 else if (*tokptr == 'u')
2373 {
2374 type = C_STRING_16;
2375 ++tokptr;
2376 }
2377 else if (*tokptr == 'U')
2378 {
2379 type = C_STRING_32;
2380 ++tokptr;
2381 }
2382 else if (*tokptr == '@')
2383 {
2384 /* An Objective C string. */
2385 is_objc = 1;
2386 type = C_STRING;
2387 ++tokptr;
2388 }
2389 else
2390 type = C_STRING;
2391
2392 /* Skip the quote. */
2393 quote = *tokptr;
2394 if (quote == '\'')
2395 type |= C_CHAR;
2396 ++tokptr;
2397
2398 *host_chars = 0;
2399
2400 while (*tokptr)
2401 {
2402 char c = *tokptr;
2403 if (c == '\\')
2404 {
2405 ++tokptr;
2406 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2407 }
2408 else if (c == quote)
2409 break;
2410 else
2411 {
2412 obstack_1grow (&tempbuf, c);
2413 ++tokptr;
2414 /* FIXME: this does the wrong thing with multi-byte host
2415 characters. We could use mbrlen here, but that would
2416 make "set host-charset" a bit less useful. */
2417 ++*host_chars;
2418 }
2419 }
2420
2421 if (*tokptr != quote)
2422 {
2423 if (quote == '"')
2424 error (_("Unterminated string in expression."));
2425 else
2426 error (_("Unmatched single quote."));
2427 }
2428 ++tokptr;
2429
2430 value->type = type;
2431 value->ptr = (char *) obstack_base (&tempbuf);
2432 value->length = obstack_object_size (&tempbuf);
2433
2434 *outptr = tokptr;
2435
2436 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2437 }
2438
2439 /* This is used to associate some attributes with a token. */
2440
2441 enum token_flag
2442 {
2443 /* If this bit is set, the token is C++-only. */
2444
2445 FLAG_CXX = 1,
2446
2447 /* If this bit is set, the token is C-only. */
2448
2449 FLAG_C = 2,
2450
2451 /* If this bit is set, the token is conditional: if there is a
2452 symbol of the same name, then the token is a symbol; otherwise,
2453 the token is a keyword. */
2454
2455 FLAG_SHADOW = 4
2456 };
2457 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2458
2459 struct token
2460 {
2461 const char *oper;
2462 int token;
2463 enum exp_opcode opcode;
2464 token_flags flags;
2465 };
2466
2467 static const struct token tokentab3[] =
2468 {
2469 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2470 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2471 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2472 {"...", DOTDOTDOT, BINOP_END, 0}
2473 };
2474
2475 static const struct token tokentab2[] =
2476 {
2477 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2478 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2479 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2480 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2481 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2482 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2483 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2484 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2485 {"++", INCREMENT, BINOP_END, 0},
2486 {"--", DECREMENT, BINOP_END, 0},
2487 {"->", ARROW, BINOP_END, 0},
2488 {"&&", ANDAND, BINOP_END, 0},
2489 {"||", OROR, BINOP_END, 0},
2490 /* "::" is *not* only C++: gdb overrides its meaning in several
2491 different ways, e.g., 'filename'::func, function::variable. */
2492 {"::", COLONCOLON, BINOP_END, 0},
2493 {"<<", LSH, BINOP_END, 0},
2494 {">>", RSH, BINOP_END, 0},
2495 {"==", EQUAL, BINOP_END, 0},
2496 {"!=", NOTEQUAL, BINOP_END, 0},
2497 {"<=", LEQ, BINOP_END, 0},
2498 {">=", GEQ, BINOP_END, 0},
2499 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2500 };
2501
2502 /* Identifier-like tokens. Only type-specifiers than can appear in
2503 multi-word type names (for example 'double' can appear in 'long
2504 double') need to be listed here. type-specifiers that are only ever
2505 single word (like 'char') are handled by the classify_name function. */
2506 static const struct token ident_tokens[] =
2507 {
2508 {"unsigned", UNSIGNED, OP_NULL, 0},
2509 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2510 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2511 {"struct", STRUCT, OP_NULL, 0},
2512 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2513 {"sizeof", SIZEOF, OP_NULL, 0},
2514 {"_Alignof", ALIGNOF, OP_NULL, 0},
2515 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2516 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2517 {"float", FLOAT_KEYWORD, OP_NULL, 0},
2518 {"__intcap_t", INTCAP_KEYWORD, OP_NULL, 0},
2519 {"__uintcap_t", UINTCAP_KEYWORD, OP_NULL, 0},
2520 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2521 {"class", CLASS, OP_NULL, FLAG_CXX},
2522 {"union", UNION, OP_NULL, 0},
2523 {"short", SHORT, OP_NULL, 0},
2524 {"const", CONST_KEYWORD, OP_NULL, 0},
2525 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2526 {"__restrict__", RESTRICT, OP_NULL, 0},
2527 {"__restrict", RESTRICT, OP_NULL, 0},
2528 {"_Atomic", ATOMIC, OP_NULL, 0},
2529 {"enum", ENUM, OP_NULL, 0},
2530 {"long", LONG, OP_NULL, 0},
2531 {"_Complex", COMPLEX, OP_NULL, 0},
2532 {"__complex__", COMPLEX, OP_NULL, 0},
2533 {"__capability", CAPABILITY, OP_NULL, 0},
2534
2535 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2536 {"int", INT_KEYWORD, OP_NULL, 0},
2537 {"new", NEW, OP_NULL, FLAG_CXX},
2538 {"delete", DELETE, OP_NULL, FLAG_CXX},
2539 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2540
2541 {"and", ANDAND, BINOP_END, FLAG_CXX},
2542 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2543 {"bitand", '&', OP_NULL, FLAG_CXX},
2544 {"bitor", '|', OP_NULL, FLAG_CXX},
2545 {"compl", '~', OP_NULL, FLAG_CXX},
2546 {"not", '!', OP_NULL, FLAG_CXX},
2547 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2548 {"or", OROR, BINOP_END, FLAG_CXX},
2549 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2550 {"xor", '^', OP_NULL, FLAG_CXX},
2551 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2552
2553 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2554 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2555 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2556 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2557
2558 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2559 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2560 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2561 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2562 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2563
2564 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2565 };
2566
2567
2568 static void
2569 scan_macro_expansion (const char *expansion)
2570 {
2571 /* We'd better not be trying to push the stack twice. */
2572 gdb_assert (! cpstate->macro_original_text);
2573
2574 /* Copy to the obstack. */
2575 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2576
2577 /* Save the old lexptr value, so we can return to it when we're done
2578 parsing the expanded text. */
2579 cpstate->macro_original_text = pstate->lexptr;
2580 pstate->lexptr = copy;
2581 }
2582
2583 static int
2584 scanning_macro_expansion (void)
2585 {
2586 return cpstate->macro_original_text != 0;
2587 }
2588
2589 static void
2590 finished_macro_expansion (void)
2591 {
2592 /* There'd better be something to pop back to. */
2593 gdb_assert (cpstate->macro_original_text);
2594
2595 /* Pop back to the original text. */
2596 pstate->lexptr = cpstate->macro_original_text;
2597 cpstate->macro_original_text = 0;
2598 }
2599
2600 /* Return true iff the token represents a C++ cast operator. */
2601
2602 static int
2603 is_cast_operator (const char *token, int len)
2604 {
2605 return (! strncmp (token, "dynamic_cast", len)
2606 || ! strncmp (token, "static_cast", len)
2607 || ! strncmp (token, "reinterpret_cast", len)
2608 || ! strncmp (token, "const_cast", len));
2609 }
2610
2611 /* The scope used for macro expansion. */
2612 static struct macro_scope *expression_macro_scope;
2613
2614 /* This is set if a NAME token appeared at the very end of the input
2615 string, with no whitespace separating the name from the EOF. This
2616 is used only when parsing to do field name completion. */
2617 static int saw_name_at_eof;
2618
2619 /* This is set if the previously-returned token was a structure
2620 operator -- either '.' or ARROW. */
2621 static bool last_was_structop;
2622
2623 /* Depth of parentheses. */
2624 static int paren_depth;
2625
2626 /* Read one token, getting characters through lexptr. */
2627
2628 static int
2629 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2630 {
2631 int c;
2632 int namelen;
2633 unsigned int i;
2634 const char *tokstart;
2635 bool saw_structop = last_was_structop;
2636
2637 last_was_structop = false;
2638 *is_quoted_name = false;
2639
2640 retry:
2641
2642 /* Check if this is a macro invocation that we need to expand. */
2643 if (! scanning_macro_expansion ())
2644 {
2645 gdb::unique_xmalloc_ptr<char> expanded
2646 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
2647
2648 if (expanded != nullptr)
2649 scan_macro_expansion (expanded.get ());
2650 }
2651
2652 pstate->prev_lexptr = pstate->lexptr;
2653
2654 tokstart = pstate->lexptr;
2655 /* See if it is a special token of length 3. */
2656 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2657 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2658 {
2659 if ((tokentab3[i].flags & FLAG_CXX) != 0
2660 && par_state->language ()->la_language != language_cplus)
2661 break;
2662 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2663
2664 pstate->lexptr += 3;
2665 yylval.opcode = tokentab3[i].opcode;
2666 return tokentab3[i].token;
2667 }
2668
2669 /* See if it is a special token of length 2. */
2670 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2671 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2672 {
2673 if ((tokentab2[i].flags & FLAG_CXX) != 0
2674 && par_state->language ()->la_language != language_cplus)
2675 break;
2676 gdb_assert ((tokentab2[i].flags & FLAG_C) == 0);
2677
2678 pstate->lexptr += 2;
2679 yylval.opcode = tokentab2[i].opcode;
2680 if (tokentab2[i].token == ARROW)
2681 last_was_structop = 1;
2682 return tokentab2[i].token;
2683 }
2684
2685 switch (c = *tokstart)
2686 {
2687 case 0:
2688 /* If we were just scanning the result of a macro expansion,
2689 then we need to resume scanning the original text.
2690 If we're parsing for field name completion, and the previous
2691 token allows such completion, return a COMPLETE token.
2692 Otherwise, we were already scanning the original text, and
2693 we're really done. */
2694 if (scanning_macro_expansion ())
2695 {
2696 finished_macro_expansion ();
2697 goto retry;
2698 }
2699 else if (saw_name_at_eof)
2700 {
2701 saw_name_at_eof = 0;
2702 return COMPLETE;
2703 }
2704 else if (par_state->parse_completion && saw_structop)
2705 return COMPLETE;
2706 else
2707 return 0;
2708
2709 case ' ':
2710 case '\t':
2711 case '\n':
2712 pstate->lexptr++;
2713 goto retry;
2714
2715 case '[':
2716 case '(':
2717 paren_depth++;
2718 pstate->lexptr++;
2719 if (par_state->language ()->la_language == language_objc
2720 && c == '[')
2721 return OBJC_LBRAC;
2722 return c;
2723
2724 case ']':
2725 case ')':
2726 if (paren_depth == 0)
2727 return 0;
2728 paren_depth--;
2729 pstate->lexptr++;
2730 return c;
2731
2732 case ',':
2733 if (pstate->comma_terminates
2734 && paren_depth == 0
2735 && ! scanning_macro_expansion ())
2736 return 0;
2737 pstate->lexptr++;
2738 return c;
2739
2740 case '.':
2741 /* Might be a floating point number. */
2742 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2743 {
2744 last_was_structop = true;
2745 goto symbol; /* Nope, must be a symbol. */
2746 }
2747 /* FALL THRU. */
2748
2749 case '0':
2750 case '1':
2751 case '2':
2752 case '3':
2753 case '4':
2754 case '5':
2755 case '6':
2756 case '7':
2757 case '8':
2758 case '9':
2759 {
2760 /* It's a number. */
2761 int got_dot = 0, got_e = 0, got_p = 0, toktype;
2762 const char *p = tokstart;
2763 int hex = input_radix > 10;
2764
2765 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2766 {
2767 p += 2;
2768 hex = 1;
2769 }
2770 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2771 {
2772 p += 2;
2773 hex = 0;
2774 }
2775
2776 for (;; ++p)
2777 {
2778 /* This test includes !hex because 'e' is a valid hex digit
2779 and thus does not indicate a floating point number when
2780 the radix is hex. */
2781 if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
2782 got_dot = got_e = 1;
2783 else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
2784 got_dot = got_p = 1;
2785 /* This test does not include !hex, because a '.' always indicates
2786 a decimal floating point number regardless of the radix. */
2787 else if (!got_dot && *p == '.')
2788 got_dot = 1;
2789 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
2790 || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
2791 && (*p == '-' || *p == '+'))
2792 /* This is the sign of the exponent, not the end of the
2793 number. */
2794 continue;
2795 /* We will take any letters or digits. parse_number will
2796 complain if past the radix, or if L or U are not final. */
2797 else if ((*p < '0' || *p > '9')
2798 && ((*p < 'a' || *p > 'z')
2799 && (*p < 'A' || *p > 'Z')))
2800 break;
2801 }
2802 toktype = parse_number (par_state, tokstart, p - tokstart,
2803 got_dot | got_e | got_p, &yylval);
2804 if (toktype == ERROR)
2805 {
2806 char *err_copy = (char *) alloca (p - tokstart + 1);
2807
2808 memcpy (err_copy, tokstart, p - tokstart);
2809 err_copy[p - tokstart] = 0;
2810 error (_("Invalid number \"%s\"."), err_copy);
2811 }
2812 pstate->lexptr = p;
2813 return toktype;
2814 }
2815
2816 case '@':
2817 {
2818 const char *p = &tokstart[1];
2819
2820 if (par_state->language ()->la_language == language_objc)
2821 {
2822 size_t len = strlen ("selector");
2823
2824 if (strncmp (p, "selector", len) == 0
2825 && (p[len] == '\0' || ISSPACE (p[len])))
2826 {
2827 pstate->lexptr = p + len;
2828 return SELECTOR;
2829 }
2830 else if (*p == '"')
2831 goto parse_string;
2832 }
2833
2834 while (ISSPACE (*p))
2835 p++;
2836 size_t len = strlen ("entry");
2837 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2838 && p[len] != '_')
2839 {
2840 pstate->lexptr = &p[len];
2841 return ENTRY;
2842 }
2843 }
2844 /* FALLTHRU */
2845 case '+':
2846 case '-':
2847 case '*':
2848 case '/':
2849 case '%':
2850 case '|':
2851 case '&':
2852 case '^':
2853 case '~':
2854 case '!':
2855 case '<':
2856 case '>':
2857 case '?':
2858 case ':':
2859 case '=':
2860 case '{':
2861 case '}':
2862 symbol:
2863 pstate->lexptr++;
2864 return c;
2865
2866 case 'L':
2867 case 'u':
2868 case 'U':
2869 if (tokstart[1] != '"' && tokstart[1] != '\'')
2870 break;
2871 /* Fall through. */
2872 case '\'':
2873 case '"':
2874
2875 parse_string:
2876 {
2877 int host_len;
2878 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2879 &yylval.tsval, &host_len);
2880 if (result == CHAR)
2881 {
2882 if (host_len == 0)
2883 error (_("Empty character constant."));
2884 else if (host_len > 2 && c == '\'')
2885 {
2886 ++tokstart;
2887 namelen = pstate->lexptr - tokstart - 1;
2888 *is_quoted_name = true;
2889
2890 goto tryname;
2891 }
2892 else if (host_len > 1)
2893 error (_("Invalid character constant."));
2894 }
2895 return result;
2896 }
2897 }
2898
2899 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2900 /* We must have come across a bad character (e.g. ';'). */
2901 error (_("Invalid character '%c' in expression."), c);
2902
2903 /* It's a name. See how long it is. */
2904 namelen = 0;
2905 for (c = tokstart[namelen];
2906 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2907 {
2908 /* Template parameter lists are part of the name.
2909 FIXME: This mishandles `print $a<4&&$a>3'. */
2910
2911 if (c == '<')
2912 {
2913 if (! is_cast_operator (tokstart, namelen))
2914 {
2915 /* Scan ahead to get rest of the template specification. Note
2916 that we look ahead only when the '<' adjoins non-whitespace
2917 characters; for comparison expressions, e.g. "a < b > c",
2918 there must be spaces before the '<', etc. */
2919 const char *p = find_template_name_end (tokstart + namelen);
2920
2921 if (p)
2922 namelen = p - tokstart;
2923 }
2924 break;
2925 }
2926 c = tokstart[++namelen];
2927 }
2928
2929 /* The token "if" terminates the expression and is NOT removed from
2930 the input stream. It doesn't count if it appears in the
2931 expansion of a macro. */
2932 if (namelen == 2
2933 && tokstart[0] == 'i'
2934 && tokstart[1] == 'f'
2935 && ! scanning_macro_expansion ())
2936 {
2937 return 0;
2938 }
2939
2940 /* For the same reason (breakpoint conditions), "thread N"
2941 terminates the expression. "thread" could be an identifier, but
2942 an identifier is never followed by a number without intervening
2943 punctuation. "task" is similar. Handle abbreviations of these,
2944 similarly to breakpoint.c:find_condition_and_thread. */
2945 if (namelen >= 1
2946 && (strncmp (tokstart, "thread", namelen) == 0
2947 || strncmp (tokstart, "task", namelen) == 0)
2948 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2949 && ! scanning_macro_expansion ())
2950 {
2951 const char *p = tokstart + namelen + 1;
2952
2953 while (*p == ' ' || *p == '\t')
2954 p++;
2955 if (*p >= '0' && *p <= '9')
2956 return 0;
2957 }
2958
2959 pstate->lexptr += namelen;
2960
2961 tryname:
2962
2963 yylval.sval.ptr = tokstart;
2964 yylval.sval.length = namelen;
2965
2966 /* Catch specific keywords. */
2967 std::string copy = copy_name (yylval.sval);
2968 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2969 if (copy == ident_tokens[i].oper)
2970 {
2971 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2972 && par_state->language ()->la_language != language_cplus)
2973 break;
2974 if ((ident_tokens[i].flags & FLAG_C) != 0
2975 && par_state->language ()->la_language != language_c
2976 && par_state->language ()->la_language != language_objc)
2977 break;
2978
2979 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2980 {
2981 struct field_of_this_result is_a_field_of_this;
2982
2983 if (lookup_symbol (copy.c_str (),
2984 pstate->expression_context_block,
2985 VAR_DOMAIN,
2986 (par_state->language ()->la_language
2987 == language_cplus ? &is_a_field_of_this
2988 : NULL)).symbol
2989 != NULL)
2990 {
2991 /* The keyword is shadowed. */
2992 break;
2993 }
2994 }
2995
2996 /* It is ok to always set this, even though we don't always
2997 strictly need to. */
2998 yylval.opcode = ident_tokens[i].opcode;
2999 return ident_tokens[i].token;
3000 }
3001
3002 if (*tokstart == '$')
3003 return DOLLAR_VARIABLE;
3004
3005 if (pstate->parse_completion && *pstate->lexptr == '\0')
3006 saw_name_at_eof = 1;
3007
3008 yylval.ssym.stoken = yylval.sval;
3009 yylval.ssym.sym.symbol = NULL;
3010 yylval.ssym.sym.block = NULL;
3011 yylval.ssym.is_a_field_of_this = 0;
3012 return NAME;
3013 }
3014
3015 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3016 struct token_and_value
3017 {
3018 int token;
3019 YYSTYPE value;
3020 };
3021
3022 /* A FIFO of tokens that have been read but not yet returned to the
3023 parser. */
3024 static std::vector<token_and_value> token_fifo;
3025
3026 /* Non-zero if the lexer should return tokens from the FIFO. */
3027 static int popping;
3028
3029 /* Temporary storage for c_lex; this holds symbol names as they are
3030 built up. */
3031 static auto_obstack name_obstack;
3032
3033 /* Classify a NAME token. The contents of the token are in `yylval'.
3034 Updates yylval and returns the new token type. BLOCK is the block
3035 in which lookups start; this can be NULL to mean the global scope.
3036 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3037 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3038 a structure operator -- either '.' or ARROW */
3039
3040 static int
3041 classify_name (struct parser_state *par_state, const struct block *block,
3042 bool is_quoted_name, bool is_after_structop)
3043 {
3044 struct block_symbol bsym;
3045 struct field_of_this_result is_a_field_of_this;
3046
3047 std::string copy = copy_name (yylval.sval);
3048
3049 /* Initialize this in case we *don't* use it in this call; that way
3050 we can refer to it unconditionally below. */
3051 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3052
3053 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
3054 par_state->language ()->name_of_this ()
3055 ? &is_a_field_of_this : NULL);
3056
3057 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
3058 {
3059 yylval.ssym.sym = bsym;
3060 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3061 return BLOCKNAME;
3062 }
3063 else if (!bsym.symbol)
3064 {
3065 /* If we found a field of 'this', we might have erroneously
3066 found a constructor where we wanted a type name. Handle this
3067 case by noticing that we found a constructor and then look up
3068 the type tag instead. */
3069 if (is_a_field_of_this.type != NULL
3070 && is_a_field_of_this.fn_field != NULL
3071 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3072 0))
3073 {
3074 struct field_of_this_result inner_is_a_field_of_this;
3075
3076 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
3077 &inner_is_a_field_of_this);
3078 if (bsym.symbol != NULL)
3079 {
3080 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3081 return TYPENAME;
3082 }
3083 }
3084
3085 /* If we found a field on the "this" object, or we are looking
3086 up a field on a struct, then we want to prefer it over a
3087 filename. However, if the name was quoted, then it is better
3088 to check for a filename or a block, since this is the only
3089 way the user has of requiring the extension to be used. */
3090 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3091 || is_quoted_name)
3092 {
3093 /* See if it's a file name. */
3094 struct symtab *symtab;
3095
3096 symtab = lookup_symtab (copy.c_str ());
3097 if (symtab)
3098 {
3099 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3100 STATIC_BLOCK);
3101 return FILENAME;
3102 }
3103 }
3104 }
3105
3106 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3107 {
3108 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3109 return TYPENAME;
3110 }
3111
3112 /* See if it's an ObjC classname. */
3113 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3114 {
3115 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3116 copy.c_str ());
3117 if (Class)
3118 {
3119 struct symbol *sym;
3120
3121 yylval.theclass.theclass = Class;
3122 sym = lookup_struct_typedef (copy.c_str (),
3123 par_state->expression_context_block, 1);
3124 if (sym)
3125 yylval.theclass.type = SYMBOL_TYPE (sym);
3126 return CLASSNAME;
3127 }
3128 }
3129
3130 /* Input names that aren't symbols but ARE valid hex numbers, when
3131 the input radix permits them, can be names or numbers depending
3132 on the parse. Note we support radixes > 16 here. */
3133 if (!bsym.symbol
3134 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3135 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3136 {
3137 YYSTYPE newlval; /* Its value is ignored. */
3138 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3139 0, &newlval);
3140
3141 if (hextype == INT)
3142 {
3143 yylval.ssym.sym = bsym;
3144 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3145 return NAME_OR_INT;
3146 }
3147 }
3148
3149 /* Any other kind of symbol */
3150 yylval.ssym.sym = bsym;
3151 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3152
3153 if (bsym.symbol == NULL
3154 && par_state->language ()->la_language == language_cplus
3155 && is_a_field_of_this.type == NULL
3156 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3157 return UNKNOWN_CPP_NAME;
3158
3159 return NAME;
3160 }
3161
3162 /* Like classify_name, but used by the inner loop of the lexer, when a
3163 name might have already been seen. CONTEXT is the context type, or
3164 NULL if this is the first component of a name. */
3165
3166 static int
3167 classify_inner_name (struct parser_state *par_state,
3168 const struct block *block, struct type *context)
3169 {
3170 struct type *type;
3171
3172 if (context == NULL)
3173 return classify_name (par_state, block, false, false);
3174
3175 type = check_typedef (context);
3176 if (!type_aggregate_p (type))
3177 return ERROR;
3178
3179 std::string copy = copy_name (yylval.ssym.stoken);
3180 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3181 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3182 VAR_DOMAIN);
3183
3184 /* If no symbol was found, search for a matching base class named
3185 COPY. This will allow users to enter qualified names of class members
3186 relative to the `this' pointer. */
3187 if (yylval.ssym.sym.symbol == NULL)
3188 {
3189 struct type *base_type = cp_find_type_baseclass_by_name (type,
3190 copy.c_str ());
3191
3192 if (base_type != NULL)
3193 {
3194 yylval.tsym.type = base_type;
3195 return TYPENAME;
3196 }
3197
3198 return ERROR;
3199 }
3200
3201 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3202 {
3203 case LOC_BLOCK:
3204 case LOC_LABEL:
3205 /* cp_lookup_nested_symbol might have accidentally found a constructor
3206 named COPY when we really wanted a base class of the same name.
3207 Double-check this case by looking for a base class. */
3208 {
3209 struct type *base_type
3210 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3211
3212 if (base_type != NULL)
3213 {
3214 yylval.tsym.type = base_type;
3215 return TYPENAME;
3216 }
3217 }
3218 return ERROR;
3219
3220 case LOC_TYPEDEF:
3221 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3222 return TYPENAME;
3223
3224 default:
3225 return NAME;
3226 }
3227 internal_error (__FILE__, __LINE__, _("not reached"));
3228 }
3229
3230 /* The outer level of a two-level lexer. This calls the inner lexer
3231 to return tokens. It then either returns these tokens, or
3232 aggregates them into a larger token. This lets us work around a
3233 problem in our parsing approach, where the parser could not
3234 distinguish between qualified names and qualified types at the
3235 right point.
3236
3237 This approach is still not ideal, because it mishandles template
3238 types. See the comment in lex_one_token for an example. However,
3239 this is still an improvement over the earlier approach, and will
3240 suffice until we move to better parsing technology. */
3241
3242 static int
3243 yylex (void)
3244 {
3245 token_and_value current;
3246 int first_was_coloncolon, last_was_coloncolon;
3247 struct type *context_type = NULL;
3248 int last_to_examine, next_to_examine, checkpoint;
3249 const struct block *search_block;
3250 bool is_quoted_name, last_lex_was_structop;
3251
3252 if (popping && !token_fifo.empty ())
3253 goto do_pop;
3254 popping = 0;
3255
3256 last_lex_was_structop = last_was_structop;
3257
3258 /* Read the first token and decide what to do. Most of the
3259 subsequent code is C++-only; but also depends on seeing a "::" or
3260 name-like token. */
3261 current.token = lex_one_token (pstate, &is_quoted_name);
3262 if (current.token == NAME)
3263 current.token = classify_name (pstate, pstate->expression_context_block,
3264 is_quoted_name, last_lex_was_structop);
3265 if (pstate->language ()->la_language != language_cplus
3266 || (current.token != TYPENAME && current.token != COLONCOLON
3267 && current.token != FILENAME))
3268 return current.token;
3269
3270 /* Read any sequence of alternating "::" and name-like tokens into
3271 the token FIFO. */
3272 current.value = yylval;
3273 token_fifo.push_back (current);
3274 last_was_coloncolon = current.token == COLONCOLON;
3275 while (1)
3276 {
3277 bool ignore;
3278
3279 /* We ignore quoted names other than the very first one.
3280 Subsequent ones do not have any special meaning. */
3281 current.token = lex_one_token (pstate, &ignore);
3282 current.value = yylval;
3283 token_fifo.push_back (current);
3284
3285 if ((last_was_coloncolon && current.token != NAME)
3286 || (!last_was_coloncolon && current.token != COLONCOLON))
3287 break;
3288 last_was_coloncolon = !last_was_coloncolon;
3289 }
3290 popping = 1;
3291
3292 /* We always read one extra token, so compute the number of tokens
3293 to examine accordingly. */
3294 last_to_examine = token_fifo.size () - 2;
3295 next_to_examine = 0;
3296
3297 current = token_fifo[next_to_examine];
3298 ++next_to_examine;
3299
3300 name_obstack.clear ();
3301 checkpoint = 0;
3302 if (current.token == FILENAME)
3303 search_block = current.value.bval;
3304 else if (current.token == COLONCOLON)
3305 search_block = NULL;
3306 else
3307 {
3308 gdb_assert (current.token == TYPENAME);
3309 search_block = pstate->expression_context_block;
3310 obstack_grow (&name_obstack, current.value.sval.ptr,
3311 current.value.sval.length);
3312 context_type = current.value.tsym.type;
3313 checkpoint = 1;
3314 }
3315
3316 first_was_coloncolon = current.token == COLONCOLON;
3317 last_was_coloncolon = first_was_coloncolon;
3318
3319 while (next_to_examine <= last_to_examine)
3320 {
3321 token_and_value next;
3322
3323 next = token_fifo[next_to_examine];
3324 ++next_to_examine;
3325
3326 if (next.token == NAME && last_was_coloncolon)
3327 {
3328 int classification;
3329
3330 yylval = next.value;
3331 classification = classify_inner_name (pstate, search_block,
3332 context_type);
3333 /* We keep going until we either run out of names, or until
3334 we have a qualified name which is not a type. */
3335 if (classification != TYPENAME && classification != NAME)
3336 break;
3337
3338 /* Accept up to this token. */
3339 checkpoint = next_to_examine;
3340
3341 /* Update the partial name we are constructing. */
3342 if (context_type != NULL)
3343 {
3344 /* We don't want to put a leading "::" into the name. */
3345 obstack_grow_str (&name_obstack, "::");
3346 }
3347 obstack_grow (&name_obstack, next.value.sval.ptr,
3348 next.value.sval.length);
3349
3350 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3351 yylval.sval.length = obstack_object_size (&name_obstack);
3352 current.value = yylval;
3353 current.token = classification;
3354
3355 last_was_coloncolon = 0;
3356
3357 if (classification == NAME)
3358 break;
3359
3360 context_type = yylval.tsym.type;
3361 }
3362 else if (next.token == COLONCOLON && !last_was_coloncolon)
3363 last_was_coloncolon = 1;
3364 else
3365 {
3366 /* We've reached the end of the name. */
3367 break;
3368 }
3369 }
3370
3371 /* If we have a replacement token, install it as the first token in
3372 the FIFO, and delete the other constituent tokens. */
3373 if (checkpoint > 0)
3374 {
3375 current.value.sval.ptr
3376 = obstack_strndup (&cpstate->expansion_obstack,
3377 current.value.sval.ptr,
3378 current.value.sval.length);
3379
3380 token_fifo[0] = current;
3381 if (checkpoint > 1)
3382 token_fifo.erase (token_fifo.begin () + 1,
3383 token_fifo.begin () + checkpoint);
3384 }
3385
3386 do_pop:
3387 current = token_fifo[0];
3388 token_fifo.erase (token_fifo.begin ());
3389 yylval = current.value;
3390 return current.token;
3391 }
3392
3393 int
3394 c_parse (struct parser_state *par_state)
3395 {
3396 /* Setting up the parser state. */
3397 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3398 gdb_assert (par_state != NULL);
3399 pstate = par_state;
3400
3401 c_parse_state cstate;
3402 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3403
3404 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3405
3406 if (par_state->expression_context_block)
3407 macro_scope
3408 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3409 else
3410 macro_scope = default_macro_scope ();
3411 if (! macro_scope)
3412 macro_scope = user_macro_scope ();
3413
3414 scoped_restore restore_macro_scope
3415 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3416
3417 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3418 parser_debug);
3419
3420 /* Initialize some state used by the lexer. */
3421 last_was_structop = false;
3422 saw_name_at_eof = 0;
3423 paren_depth = 0;
3424
3425 token_fifo.clear ();
3426 popping = 0;
3427 name_obstack.clear ();
3428
3429 return yyparse ();
3430 }
3431
3432 #if defined(YYBISON) && YYBISON < 30800
3433
3434
3435 /* This is called via the YYPRINT macro when parser debugging is
3436 enabled. It prints a token's value. */
3437
3438 static void
3439 c_print_token (FILE *file, int type, YYSTYPE value)
3440 {
3441 switch (type)
3442 {
3443 case INT:
3444 parser_fprintf (file, "typed_val_int<%s, %s>",
3445 TYPE_SAFE_NAME (value.typed_val_int.type),
3446 pulongest (value.typed_val_int.val));
3447 break;
3448
3449 case CHAR:
3450 case STRING:
3451 {
3452 char *copy = (char *) alloca (value.tsval.length + 1);
3453
3454 memcpy (copy, value.tsval.ptr, value.tsval.length);
3455 copy[value.tsval.length] = '\0';
3456
3457 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3458 }
3459 break;
3460
3461 case NSSTRING:
3462 case DOLLAR_VARIABLE:
3463 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3464 break;
3465
3466 case TYPENAME:
3467 parser_fprintf (file, "tsym<type=%s, name=%s>",
3468 TYPE_SAFE_NAME (value.tsym.type),
3469 copy_name (value.tsym.stoken).c_str ());
3470 break;
3471
3472 case NAME:
3473 case UNKNOWN_CPP_NAME:
3474 case NAME_OR_INT:
3475 case BLOCKNAME:
3476 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3477 copy_name (value.ssym.stoken).c_str (),
3478 (value.ssym.sym.symbol == NULL
3479 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3480 value.ssym.is_a_field_of_this);
3481 break;
3482
3483 case FILENAME:
3484 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3485 break;
3486 }
3487 }
3488
3489 #endif
3490
3491 static void
3492 yyerror (const char *msg)
3493 {
3494 if (pstate->prev_lexptr)
3495 pstate->lexptr = pstate->prev_lexptr;
3496
3497 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
3498 }