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