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