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