nm: Add --quiet to suppress "no symbols" diagnostic
[binutils-gdb.git] / gdb / c-exp.y
blob13b06f39bbf02f3f1fa341f17f68d98540d151e2
1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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.
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. */
38 #include "defs.h"
39 #include <ctype.h>
40 #include "expression.h"
41 #include "value.h"
42 #include "parser-defs.h"
43 #include "language.h"
44 #include "c-lang.h"
45 #include "c-support.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
49 #include "charset.h"
50 #include "block.h"
51 #include "cp-support.h"
52 #include "macroscope.h"
53 #include "objc-lang.h"
54 #include "typeprint.h"
55 #include "cp-abi.h"
56 #include "type-stack.h"
57 #include "target-float.h"
59 #define parse_type(ps) builtin_type (ps->gdbarch ())
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
62 etc). */
63 #define GDB_YY_REMAP_PREFIX c_
64 #include "yy-remap.h"
66 /* The state of the parser, used internally when we are parsing the
67 expression. */
69 static struct parser_state *pstate = NULL;
71 /* Data that must be held for the duration of a parse. */
73 struct c_parse_state
75 /* These are used to hold type lists and type stacks that are
76 allocated during the parse. */
77 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
78 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
80 /* Storage for some strings allocated during the parse. */
81 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
83 /* When we find that lexptr (the global var defined in parse.c) is
84 pointing at a macro invocation, we expand the invocation, and call
85 scan_macro_expansion to save the old lexptr here and point lexptr
86 into the expanded text. When we reach the end of that, we call
87 end_macro_expansion to pop back to the value we saved here. The
88 macro expansion code promises to return only fully-expanded text,
89 so we don't need to "push" more than one level.
91 This is disgusting, of course. It would be cleaner to do all macro
92 expansion beforehand, and then hand that to lexptr. But we don't
93 really know where the expression ends. Remember, in a command like
95 (gdb) break *ADDRESS if CONDITION
97 we evaluate ADDRESS in the scope of the current frame, but we
98 evaluate CONDITION in the scope of the breakpoint's location. So
99 it's simply wrong to try to macro-expand the whole thing at once. */
100 const char *macro_original_text = nullptr;
102 /* We save all intermediate macro expansions on this obstack for the
103 duration of a single parse. The expansion text may sometimes have
104 to live past the end of the expansion, due to yacc lookahead.
105 Rather than try to be clever about saving the data for a single
106 token, we simply keep it all and delete it after parsing has
107 completed. */
108 auto_obstack expansion_obstack;
110 /* The type stack. */
111 struct type_stack type_stack;
114 /* This is set and cleared in c_parse. */
116 static struct c_parse_state *cpstate;
118 int yyparse (void);
120 static int yylex (void);
122 static void yyerror (const char *);
124 static int type_aggregate_p (struct type *);
128 /* Although the yacc "value" of an expression is not used,
129 since the result is stored in the structure being created,
130 other node types do have values. */
132 %union
134 LONGEST lval;
135 struct {
136 LONGEST val;
137 struct type *type;
138 } typed_val_int;
139 struct {
140 gdb_byte val[16];
141 struct type *type;
142 } typed_val_float;
143 struct type *tval;
144 struct stoken sval;
145 struct typed_stoken tsval;
146 struct ttype tsym;
147 struct symtoken ssym;
148 int voidval;
149 const struct block *bval;
150 enum exp_opcode opcode;
152 struct stoken_vector svec;
153 std::vector<struct type *> *tvec;
155 struct type_stack *type_stack;
157 struct objc_class_str theclass;
161 /* YYSTYPE gets defined by %union */
162 static int parse_number (struct parser_state *par_state,
163 const char *, int, int, YYSTYPE *);
164 static struct stoken operator_stoken (const char *);
165 static struct stoken typename_stoken (const char *);
166 static void check_parameter_typelist (std::vector<struct type *> *);
167 static void write_destructor_name (struct parser_state *par_state,
168 struct stoken);
170 #ifdef YYBISON
171 static void c_print_token (FILE *file, int type, YYSTYPE value);
172 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
173 #endif
176 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
177 %type <lval> rcurly
178 %type <tval> type typebase scalar_type
179 %type <tvec> nonempty_typelist func_mod parameter_typelist
180 /* %type <bval> block */
182 /* Fancy type parsing. */
183 %type <tval> ptype
184 %type <lval> array_mod
185 %type <tval> conversion_type_id
187 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
189 %token <typed_val_int> INT COMPLEX_INT
190 %token <typed_val_float> FLOAT COMPLEX_FLOAT
192 /* Both NAME and TYPENAME tokens represent symbols in the input,
193 and both convey their data as strings.
194 But a TYPENAME is a string that happens to be defined as a typedef
195 or builtin type name (such as int or char)
196 and a NAME is any other symbol.
197 Contexts where this distinction is not important can use the
198 nonterminal "name", which matches either NAME or TYPENAME. */
200 %token <tsval> STRING
201 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
202 %token SELECTOR /* ObjC "@selector" pseudo-operator */
203 %token <tsval> CHAR
204 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
205 %token <ssym> UNKNOWN_CPP_NAME
206 %token <voidval> COMPLETE
207 %token <tsym> TYPENAME
208 %token <theclass> CLASSNAME /* ObjC Class name */
209 %type <sval> name field_name
210 %type <svec> string_exp
211 %type <ssym> name_not_typename
212 %type <tsym> type_name
214 /* This is like a '[' token, but is only generated when parsing
215 Objective C. This lets us reuse the same parser without
216 erroneously parsing ObjC-specific expressions in C. */
217 %token OBJC_LBRAC
219 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
220 but which would parse as a valid number in the current input radix.
221 E.g. "c" when input_radix==16. Depending on the parse, it will be
222 turned into a name or into a number. */
224 %token <ssym> NAME_OR_INT
226 %token OPERATOR
227 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
228 %token TEMPLATE
229 %token ERROR
230 %token NEW DELETE
231 %type <sval> oper
232 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
233 %token ENTRY
234 %token TYPEOF
235 %token DECLTYPE
236 %token TYPEID
238 /* Special type cases, put in to allow the parser to distinguish different
239 legal basetypes. */
240 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
241 %token RESTRICT ATOMIC
242 %token FLOAT_KEYWORD COMPLEX
244 %token <sval> DOLLAR_VARIABLE
246 %token <opcode> ASSIGN_MODIFY
248 /* C++ */
249 %token TRUEKEYWORD
250 %token FALSEKEYWORD
253 %left ','
254 %left ABOVE_COMMA
255 %right '=' ASSIGN_MODIFY
256 %right '?'
257 %left OROR
258 %left ANDAND
259 %left '|'
260 %left '^'
261 %left '&'
262 %left EQUAL NOTEQUAL
263 %left '<' '>' LEQ GEQ
264 %left LSH RSH
265 %left '@'
266 %left '+' '-'
267 %left '*' '/' '%'
268 %right UNARY INCREMENT DECREMENT
269 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
270 %token <ssym> BLOCKNAME
271 %token <bval> FILENAME
272 %type <bval> block
273 %left COLONCOLON
275 %token DOTDOTDOT
280 start : exp1
281 | type_exp
284 type_exp: type
285 { write_exp_elt_opcode(pstate, OP_TYPE);
286 write_exp_elt_type(pstate, $1);
287 write_exp_elt_opcode(pstate, OP_TYPE);}
288 | TYPEOF '(' exp ')'
290 write_exp_elt_opcode (pstate, OP_TYPEOF);
292 | TYPEOF '(' type ')'
294 write_exp_elt_opcode (pstate, OP_TYPE);
295 write_exp_elt_type (pstate, $3);
296 write_exp_elt_opcode (pstate, OP_TYPE);
298 | DECLTYPE '(' exp ')'
300 write_exp_elt_opcode (pstate, OP_DECLTYPE);
304 /* Expressions, including the comma operator. */
305 exp1 : exp
306 | exp1 ',' exp
307 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
310 /* Expressions, not including the comma operator. */
311 exp : '*' exp %prec UNARY
312 { write_exp_elt_opcode (pstate, UNOP_IND); }
315 exp : '&' exp %prec UNARY
316 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
319 exp : '-' exp %prec UNARY
320 { write_exp_elt_opcode (pstate, UNOP_NEG); }
323 exp : '+' exp %prec UNARY
324 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
327 exp : '!' exp %prec UNARY
328 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
331 exp : '~' exp %prec UNARY
332 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
335 exp : INCREMENT exp %prec UNARY
336 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
339 exp : DECREMENT exp %prec UNARY
340 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
343 exp : exp INCREMENT %prec UNARY
344 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
347 exp : exp DECREMENT %prec UNARY
348 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
351 exp : TYPEID '(' exp ')' %prec UNARY
352 { write_exp_elt_opcode (pstate, OP_TYPEID); }
355 exp : TYPEID '(' type_exp ')' %prec UNARY
356 { write_exp_elt_opcode (pstate, OP_TYPEID); }
359 exp : SIZEOF exp %prec UNARY
360 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
363 exp : ALIGNOF '(' type_exp ')' %prec UNARY
364 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
367 exp : exp ARROW field_name
368 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
369 write_exp_string (pstate, $3);
370 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
373 exp : exp ARROW field_name COMPLETE
374 { pstate->mark_struct_expression ();
375 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
376 write_exp_string (pstate, $3);
377 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
380 exp : exp ARROW COMPLETE
381 { struct stoken s;
382 pstate->mark_struct_expression ();
383 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
384 s.ptr = "";
385 s.length = 0;
386 write_exp_string (pstate, s);
387 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
390 exp : exp ARROW '~' name
391 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
392 write_destructor_name (pstate, $4);
393 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
396 exp : exp ARROW '~' name COMPLETE
397 { pstate->mark_struct_expression ();
398 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
399 write_destructor_name (pstate, $4);
400 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
403 exp : exp ARROW qualified_name
404 { /* exp->type::name becomes exp->*(&type::name) */
405 /* Note: this doesn't work if name is a
406 static member! FIXME */
407 write_exp_elt_opcode (pstate, UNOP_ADDR);
408 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
411 exp : exp ARROW_STAR exp
412 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
415 exp : exp '.' field_name
416 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
417 write_exp_string (pstate, $3);
418 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
421 exp : exp '.' field_name COMPLETE
422 { pstate->mark_struct_expression ();
423 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
424 write_exp_string (pstate, $3);
425 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
428 exp : exp '.' COMPLETE
429 { struct stoken s;
430 pstate->mark_struct_expression ();
431 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
432 s.ptr = "";
433 s.length = 0;
434 write_exp_string (pstate, s);
435 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
438 exp : exp '.' '~' name
439 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
440 write_destructor_name (pstate, $4);
441 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
444 exp : exp '.' '~' name COMPLETE
445 { pstate->mark_struct_expression ();
446 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
447 write_destructor_name (pstate, $4);
448 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
451 exp : exp '.' qualified_name
452 { /* exp.type::name becomes exp.*(&type::name) */
453 /* Note: this doesn't work if name is a
454 static member! FIXME */
455 write_exp_elt_opcode (pstate, UNOP_ADDR);
456 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
459 exp : exp DOT_STAR exp
460 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
463 exp : exp '[' exp1 ']'
464 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
467 exp : exp OBJC_LBRAC exp1 ']'
468 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
472 * The rules below parse ObjC message calls of the form:
473 * '[' target selector {':' argument}* ']'
476 exp : OBJC_LBRAC TYPENAME
478 CORE_ADDR theclass;
480 std::string copy = copy_name ($2.stoken);
481 theclass = lookup_objc_class (pstate->gdbarch (),
482 copy.c_str ());
483 if (theclass == 0)
484 error (_("%s is not an ObjC Class"),
485 copy.c_str ());
486 write_exp_elt_opcode (pstate, OP_LONG);
487 write_exp_elt_type (pstate,
488 parse_type (pstate)->builtin_int);
489 write_exp_elt_longcst (pstate, (LONGEST) theclass);
490 write_exp_elt_opcode (pstate, OP_LONG);
491 start_msglist();
493 msglist ']'
494 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
495 end_msglist (pstate);
496 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
500 exp : OBJC_LBRAC CLASSNAME
502 write_exp_elt_opcode (pstate, OP_LONG);
503 write_exp_elt_type (pstate,
504 parse_type (pstate)->builtin_int);
505 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
506 write_exp_elt_opcode (pstate, OP_LONG);
507 start_msglist();
509 msglist ']'
510 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
511 end_msglist (pstate);
512 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
516 exp : OBJC_LBRAC exp
517 { start_msglist(); }
518 msglist ']'
519 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
520 end_msglist (pstate);
521 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
525 msglist : name
526 { add_msglist(&$1, 0); }
527 | msgarglist
530 msgarglist : msgarg
531 | msgarglist msgarg
534 msgarg : name ':' exp
535 { add_msglist(&$1, 1); }
536 | ':' exp /* Unnamed arg. */
537 { add_msglist(0, 1); }
538 | ',' exp /* Variable number of args. */
539 { add_msglist(0, 0); }
542 exp : exp '('
543 /* This is to save the value of arglist_len
544 being accumulated by an outer function call. */
545 { pstate->start_arglist (); }
546 arglist ')' %prec ARROW
547 { write_exp_elt_opcode (pstate, OP_FUNCALL);
548 write_exp_elt_longcst (pstate,
549 pstate->end_arglist ());
550 write_exp_elt_opcode (pstate, OP_FUNCALL); }
553 /* This is here to disambiguate with the production for
554 "func()::static_var" further below, which uses
555 function_method_void. */
556 exp : exp '(' ')' %prec ARROW
557 { pstate->start_arglist ();
558 write_exp_elt_opcode (pstate, OP_FUNCALL);
559 write_exp_elt_longcst (pstate,
560 pstate->end_arglist ());
561 write_exp_elt_opcode (pstate, OP_FUNCALL); }
565 exp : UNKNOWN_CPP_NAME '('
567 /* This could potentially be a an argument defined
568 lookup function (Koenig). */
569 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
570 write_exp_elt_block
571 (pstate, pstate->expression_context_block);
572 write_exp_elt_sym (pstate,
573 NULL); /* Placeholder. */
574 write_exp_string (pstate, $1.stoken);
575 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
577 /* This is to save the value of arglist_len
578 being accumulated by an outer function call. */
580 pstate->start_arglist ();
582 arglist ')' %prec ARROW
584 write_exp_elt_opcode (pstate, OP_FUNCALL);
585 write_exp_elt_longcst (pstate,
586 pstate->end_arglist ());
587 write_exp_elt_opcode (pstate, OP_FUNCALL);
591 lcurly : '{'
592 { pstate->start_arglist (); }
595 arglist :
598 arglist : exp
599 { pstate->arglist_len = 1; }
602 arglist : arglist ',' exp %prec ABOVE_COMMA
603 { pstate->arglist_len++; }
606 function_method: exp '(' parameter_typelist ')' const_or_volatile
608 std::vector<struct type *> *type_list = $3;
609 LONGEST len = type_list->size ();
611 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
612 /* Save the const/volatile qualifiers as
613 recorded by the const_or_volatile
614 production's actions. */
615 write_exp_elt_longcst
616 (pstate,
617 (cpstate->type_stack
618 .follow_type_instance_flags ()));
619 write_exp_elt_longcst (pstate, len);
620 for (type *type_elt : *type_list)
621 write_exp_elt_type (pstate, type_elt);
622 write_exp_elt_longcst(pstate, len);
623 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
627 function_method_void: exp '(' ')' const_or_volatile
628 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
629 /* See above. */
630 write_exp_elt_longcst
631 (pstate,
632 cpstate->type_stack.follow_type_instance_flags ());
633 write_exp_elt_longcst (pstate, 0);
634 write_exp_elt_longcst (pstate, 0);
635 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
639 exp : function_method
642 /* Normally we must interpret "func()" as a function call, instead of
643 a type. The user needs to write func(void) to disambiguate.
644 However, in the "func()::static_var" case, there's no
645 ambiguity. */
646 function_method_void_or_typelist: function_method
647 | function_method_void
650 exp : function_method_void_or_typelist COLONCOLON name
652 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
653 write_exp_string (pstate, $3);
654 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
658 rcurly : '}'
659 { $$ = pstate->end_arglist () - 1; }
661 exp : lcurly arglist rcurly %prec ARROW
662 { write_exp_elt_opcode (pstate, OP_ARRAY);
663 write_exp_elt_longcst (pstate, (LONGEST) 0);
664 write_exp_elt_longcst (pstate, (LONGEST) $3);
665 write_exp_elt_opcode (pstate, OP_ARRAY); }
668 exp : lcurly type_exp rcurly exp %prec UNARY
669 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
672 exp : '(' type_exp ')' exp %prec UNARY
673 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
676 exp : '(' exp1 ')'
680 /* Binary operators in order of decreasing precedence. */
682 exp : exp '@' exp
683 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
686 exp : exp '*' exp
687 { write_exp_elt_opcode (pstate, BINOP_MUL); }
690 exp : exp '/' exp
691 { write_exp_elt_opcode (pstate, BINOP_DIV); }
694 exp : exp '%' exp
695 { write_exp_elt_opcode (pstate, BINOP_REM); }
698 exp : exp '+' exp
699 { write_exp_elt_opcode (pstate, BINOP_ADD); }
702 exp : exp '-' exp
703 { write_exp_elt_opcode (pstate, BINOP_SUB); }
706 exp : exp LSH exp
707 { write_exp_elt_opcode (pstate, BINOP_LSH); }
710 exp : exp RSH exp
711 { write_exp_elt_opcode (pstate, BINOP_RSH); }
714 exp : exp EQUAL exp
715 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
718 exp : exp NOTEQUAL exp
719 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
722 exp : exp LEQ exp
723 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
726 exp : exp GEQ exp
727 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
730 exp : exp '<' exp
731 { write_exp_elt_opcode (pstate, BINOP_LESS); }
734 exp : exp '>' exp
735 { write_exp_elt_opcode (pstate, BINOP_GTR); }
738 exp : exp '&' exp
739 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
742 exp : exp '^' exp
743 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
746 exp : exp '|' exp
747 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
750 exp : exp ANDAND exp
751 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
754 exp : exp OROR exp
755 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
758 exp : exp '?' exp ':' exp %prec '?'
759 { write_exp_elt_opcode (pstate, TERNOP_COND); }
762 exp : exp '=' exp
763 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
766 exp : exp ASSIGN_MODIFY exp
767 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
768 write_exp_elt_opcode (pstate, $2);
769 write_exp_elt_opcode (pstate,
770 BINOP_ASSIGN_MODIFY); }
773 exp : INT
774 { write_exp_elt_opcode (pstate, OP_LONG);
775 write_exp_elt_type (pstate, $1.type);
776 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
777 write_exp_elt_opcode (pstate, OP_LONG); }
780 exp : COMPLEX_INT
782 write_exp_elt_opcode (pstate, OP_LONG);
783 write_exp_elt_type (pstate, TYPE_TARGET_TYPE ($1.type));
784 write_exp_elt_longcst (pstate, 0);
785 write_exp_elt_opcode (pstate, OP_LONG);
786 write_exp_elt_opcode (pstate, OP_LONG);
787 write_exp_elt_type (pstate, TYPE_TARGET_TYPE ($1.type));
788 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
789 write_exp_elt_opcode (pstate, OP_LONG);
790 write_exp_elt_opcode (pstate, OP_COMPLEX);
791 write_exp_elt_type (pstate, $1.type);
792 write_exp_elt_opcode (pstate, OP_COMPLEX);
796 exp : CHAR
798 struct stoken_vector vec;
799 vec.len = 1;
800 vec.tokens = &$1;
801 write_exp_string_vector (pstate, $1.type, &vec);
805 exp : NAME_OR_INT
806 { YYSTYPE val;
807 parse_number (pstate, $1.stoken.ptr,
808 $1.stoken.length, 0, &val);
809 write_exp_elt_opcode (pstate, OP_LONG);
810 write_exp_elt_type (pstate, val.typed_val_int.type);
811 write_exp_elt_longcst (pstate,
812 (LONGEST) val.typed_val_int.val);
813 write_exp_elt_opcode (pstate, OP_LONG);
818 exp : FLOAT
819 { write_exp_elt_opcode (pstate, OP_FLOAT);
820 write_exp_elt_type (pstate, $1.type);
821 write_exp_elt_floatcst (pstate, $1.val);
822 write_exp_elt_opcode (pstate, OP_FLOAT); }
825 exp : COMPLEX_FLOAT
827 struct type *underlying
828 = TYPE_TARGET_TYPE ($1.type);
830 write_exp_elt_opcode (pstate, OP_FLOAT);
831 write_exp_elt_type (pstate, underlying);
832 gdb_byte val[16];
833 target_float_from_host_double (val, underlying, 0);
834 write_exp_elt_floatcst (pstate, val);
835 write_exp_elt_opcode (pstate, OP_FLOAT);
836 write_exp_elt_opcode (pstate, OP_FLOAT);
837 write_exp_elt_type (pstate, underlying);
838 write_exp_elt_floatcst (pstate, $1.val);
839 write_exp_elt_opcode (pstate, OP_FLOAT);
840 write_exp_elt_opcode (pstate, OP_COMPLEX);
841 write_exp_elt_type (pstate, $1.type);
842 write_exp_elt_opcode (pstate, OP_COMPLEX);
846 exp : variable
849 exp : DOLLAR_VARIABLE
851 write_dollar_variable (pstate, $1);
855 exp : SELECTOR '(' name ')'
857 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
858 write_exp_string (pstate, $3);
859 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
862 exp : SIZEOF '(' type ')' %prec UNARY
863 { struct type *type = $3;
864 write_exp_elt_opcode (pstate, OP_LONG);
865 write_exp_elt_type (pstate, lookup_signed_typename
866 (pstate->language (),
867 "int"));
868 type = check_typedef (type);
870 /* $5.3.3/2 of the C++ Standard (n3290 draft)
871 says of sizeof: "When applied to a reference
872 or a reference type, the result is the size of
873 the referenced type." */
874 if (TYPE_IS_REFERENCE (type))
875 type = check_typedef (TYPE_TARGET_TYPE (type));
876 write_exp_elt_longcst (pstate,
877 (LONGEST) TYPE_LENGTH (type));
878 write_exp_elt_opcode (pstate, OP_LONG); }
881 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
882 { write_exp_elt_opcode (pstate,
883 UNOP_REINTERPRET_CAST); }
886 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
887 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
890 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
891 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
894 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
895 { /* We could do more error checking here, but
896 it doesn't seem worthwhile. */
897 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
900 string_exp:
901 STRING
903 /* We copy the string here, and not in the
904 lexer, to guarantee that we do not leak a
905 string. Note that we follow the
906 NUL-termination convention of the
907 lexer. */
908 struct typed_stoken *vec = XNEW (struct typed_stoken);
909 $$.len = 1;
910 $$.tokens = vec;
912 vec->type = $1.type;
913 vec->length = $1.length;
914 vec->ptr = (char *) malloc ($1.length + 1);
915 memcpy (vec->ptr, $1.ptr, $1.length + 1);
918 | string_exp STRING
920 /* Note that we NUL-terminate here, but just
921 for convenience. */
922 char *p;
923 ++$$.len;
924 $$.tokens = XRESIZEVEC (struct typed_stoken,
925 $$.tokens, $$.len);
927 p = (char *) malloc ($2.length + 1);
928 memcpy (p, $2.ptr, $2.length + 1);
930 $$.tokens[$$.len - 1].type = $2.type;
931 $$.tokens[$$.len - 1].length = $2.length;
932 $$.tokens[$$.len - 1].ptr = p;
936 exp : string_exp
938 int i;
939 c_string_type type = C_STRING;
941 for (i = 0; i < $1.len; ++i)
943 switch ($1.tokens[i].type)
945 case C_STRING:
946 break;
947 case C_WIDE_STRING:
948 case C_STRING_16:
949 case C_STRING_32:
950 if (type != C_STRING
951 && type != $1.tokens[i].type)
952 error (_("Undefined string concatenation."));
953 type = (enum c_string_type_values) $1.tokens[i].type;
954 break;
955 default:
956 /* internal error */
957 internal_error (__FILE__, __LINE__,
958 "unrecognized type in string concatenation");
962 write_exp_string_vector (pstate, type, &$1);
963 for (i = 0; i < $1.len; ++i)
964 free ($1.tokens[i].ptr);
965 free ($1.tokens);
969 exp : NSSTRING /* ObjC NextStep NSString constant
970 * of the form '@' '"' string '"'.
972 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
973 write_exp_string (pstate, $1);
974 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
977 /* C++. */
978 exp : TRUEKEYWORD
979 { write_exp_elt_opcode (pstate, OP_LONG);
980 write_exp_elt_type (pstate,
981 parse_type (pstate)->builtin_bool);
982 write_exp_elt_longcst (pstate, (LONGEST) 1);
983 write_exp_elt_opcode (pstate, OP_LONG); }
986 exp : FALSEKEYWORD
987 { write_exp_elt_opcode (pstate, OP_LONG);
988 write_exp_elt_type (pstate,
989 parse_type (pstate)->builtin_bool);
990 write_exp_elt_longcst (pstate, (LONGEST) 0);
991 write_exp_elt_opcode (pstate, OP_LONG); }
994 /* end of C++. */
996 block : BLOCKNAME
998 if ($1.sym.symbol)
999 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
1000 else
1001 error (_("No file or function \"%s\"."),
1002 copy_name ($1.stoken).c_str ());
1004 | FILENAME
1006 $$ = $1;
1010 block : block COLONCOLON name
1012 std::string copy = copy_name ($3);
1013 struct symbol *tem
1014 = lookup_symbol (copy.c_str (), $1,
1015 VAR_DOMAIN, NULL).symbol;
1017 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
1018 error (_("No function \"%s\" in specified context."),
1019 copy.c_str ());
1020 $$ = SYMBOL_BLOCK_VALUE (tem); }
1023 variable: name_not_typename ENTRY
1024 { struct symbol *sym = $1.sym.symbol;
1026 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
1027 || !symbol_read_needs_frame (sym))
1028 error (_("@entry can be used only for function "
1029 "parameters, not for \"%s\""),
1030 copy_name ($1.stoken).c_str ());
1032 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
1033 write_exp_elt_sym (pstate, sym);
1034 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
1038 variable: block COLONCOLON name
1040 std::string copy = copy_name ($3);
1041 struct block_symbol sym
1042 = lookup_symbol (copy.c_str (), $1,
1043 VAR_DOMAIN, NULL);
1045 if (sym.symbol == 0)
1046 error (_("No symbol \"%s\" in specified context."),
1047 copy.c_str ());
1048 if (symbol_read_needs_frame (sym.symbol))
1049 pstate->block_tracker->update (sym);
1051 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1052 write_exp_elt_block (pstate, sym.block);
1053 write_exp_elt_sym (pstate, sym.symbol);
1054 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
1057 qualified_name: TYPENAME COLONCOLON name
1059 struct type *type = $1.type;
1060 type = check_typedef (type);
1061 if (!type_aggregate_p (type))
1062 error (_("`%s' is not defined as an aggregate type."),
1063 TYPE_SAFE_NAME (type));
1065 write_exp_elt_opcode (pstate, OP_SCOPE);
1066 write_exp_elt_type (pstate, type);
1067 write_exp_string (pstate, $3);
1068 write_exp_elt_opcode (pstate, OP_SCOPE);
1070 | TYPENAME COLONCOLON '~' name
1072 struct type *type = $1.type;
1073 struct stoken tmp_token;
1074 char *buf;
1076 type = check_typedef (type);
1077 if (!type_aggregate_p (type))
1078 error (_("`%s' is not defined as an aggregate type."),
1079 TYPE_SAFE_NAME (type));
1080 buf = (char *) alloca ($4.length + 2);
1081 tmp_token.ptr = buf;
1082 tmp_token.length = $4.length + 1;
1083 buf[0] = '~';
1084 memcpy (buf+1, $4.ptr, $4.length);
1085 buf[tmp_token.length] = 0;
1087 /* Check for valid destructor name. */
1088 destructor_name_p (tmp_token.ptr, $1.type);
1089 write_exp_elt_opcode (pstate, OP_SCOPE);
1090 write_exp_elt_type (pstate, type);
1091 write_exp_string (pstate, tmp_token);
1092 write_exp_elt_opcode (pstate, OP_SCOPE);
1094 | TYPENAME COLONCOLON name COLONCOLON name
1096 std::string copy = copy_name ($3);
1097 error (_("No type \"%s\" within class "
1098 "or namespace \"%s\"."),
1099 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1103 variable: qualified_name
1104 | COLONCOLON name_not_typename
1106 std::string name = copy_name ($2.stoken);
1107 struct block_symbol sym
1108 = lookup_symbol (name.c_str (),
1109 (const struct block *) NULL,
1110 VAR_DOMAIN, NULL);
1111 write_exp_symbol_reference (pstate, name.c_str (),
1112 sym);
1116 variable: name_not_typename
1117 { struct block_symbol sym = $1.sym;
1119 if (sym.symbol)
1121 if (symbol_read_needs_frame (sym.symbol))
1122 pstate->block_tracker->update (sym);
1124 /* If we found a function, see if it's
1125 an ifunc resolver that has the same
1126 address as the ifunc symbol itself.
1127 If so, prefer the ifunc symbol. */
1129 bound_minimal_symbol resolver
1130 = find_gnu_ifunc (sym.symbol);
1131 if (resolver.minsym != NULL)
1132 write_exp_msymbol (pstate, resolver);
1133 else
1135 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1136 write_exp_elt_block (pstate, sym.block);
1137 write_exp_elt_sym (pstate, sym.symbol);
1138 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1141 else if ($1.is_a_field_of_this)
1143 /* C++: it hangs off of `this'. Must
1144 not inadvertently convert from a method call
1145 to data ref. */
1146 pstate->block_tracker->update (sym);
1147 write_exp_elt_opcode (pstate, OP_THIS);
1148 write_exp_elt_opcode (pstate, OP_THIS);
1149 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1150 write_exp_string (pstate, $1.stoken);
1151 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1153 else
1155 std::string arg = copy_name ($1.stoken);
1157 bound_minimal_symbol msymbol
1158 = lookup_bound_minimal_symbol (arg.c_str ());
1159 if (msymbol.minsym == NULL)
1161 if (!have_full_symbols () && !have_partial_symbols ())
1162 error (_("No symbol table is loaded. Use the \"file\" command."));
1163 else
1164 error (_("No symbol \"%s\" in current context."),
1165 arg.c_str ());
1168 /* This minsym might be an alias for
1169 another function. See if we can find
1170 the debug symbol for the target, and
1171 if so, use it instead, since it has
1172 return type / prototype info. This
1173 is important for example for "p
1174 *__errno_location()". */
1175 symbol *alias_target
1176 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1177 && msymbol.minsym->type != mst_data_gnu_ifunc)
1178 ? find_function_alias_target (msymbol)
1179 : NULL);
1180 if (alias_target != NULL)
1182 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1183 write_exp_elt_block
1184 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1185 write_exp_elt_sym (pstate, alias_target);
1186 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1188 else
1189 write_exp_msymbol (pstate, msymbol);
1194 const_or_volatile: const_or_volatile_noopt
1198 single_qualifier:
1199 CONST_KEYWORD
1200 { cpstate->type_stack.insert (tp_const); }
1201 | VOLATILE_KEYWORD
1202 { cpstate->type_stack.insert (tp_volatile); }
1203 | ATOMIC
1204 { cpstate->type_stack.insert (tp_atomic); }
1205 | RESTRICT
1206 { cpstate->type_stack.insert (tp_restrict); }
1207 | '@' NAME
1209 cpstate->type_stack.insert (pstate,
1210 copy_name ($2.stoken).c_str ());
1214 qualifier_seq_noopt:
1215 single_qualifier
1216 | qualifier_seq single_qualifier
1219 qualifier_seq:
1220 qualifier_seq_noopt
1224 ptr_operator:
1225 ptr_operator '*'
1226 { cpstate->type_stack.insert (tp_pointer); }
1227 qualifier_seq
1228 | '*'
1229 { cpstate->type_stack.insert (tp_pointer); }
1230 qualifier_seq
1231 | '&'
1232 { cpstate->type_stack.insert (tp_reference); }
1233 | '&' ptr_operator
1234 { cpstate->type_stack.insert (tp_reference); }
1235 | ANDAND
1236 { cpstate->type_stack.insert (tp_rvalue_reference); }
1237 | ANDAND ptr_operator
1238 { cpstate->type_stack.insert (tp_rvalue_reference); }
1241 ptr_operator_ts: ptr_operator
1243 $$ = cpstate->type_stack.create ();
1244 cpstate->type_stacks.emplace_back ($$);
1248 abs_decl: ptr_operator_ts direct_abs_decl
1249 { $$ = $2->append ($1); }
1250 | ptr_operator_ts
1251 | direct_abs_decl
1254 direct_abs_decl: '(' abs_decl ')'
1255 { $$ = $2; }
1256 | direct_abs_decl array_mod
1258 cpstate->type_stack.push ($1);
1259 cpstate->type_stack.push ($2);
1260 cpstate->type_stack.push (tp_array);
1261 $$ = cpstate->type_stack.create ();
1262 cpstate->type_stacks.emplace_back ($$);
1264 | array_mod
1266 cpstate->type_stack.push ($1);
1267 cpstate->type_stack.push (tp_array);
1268 $$ = cpstate->type_stack.create ();
1269 cpstate->type_stacks.emplace_back ($$);
1272 | direct_abs_decl func_mod
1274 cpstate->type_stack.push ($1);
1275 cpstate->type_stack.push ($2);
1276 $$ = cpstate->type_stack.create ();
1277 cpstate->type_stacks.emplace_back ($$);
1279 | func_mod
1281 cpstate->type_stack.push ($1);
1282 $$ = cpstate->type_stack.create ();
1283 cpstate->type_stacks.emplace_back ($$);
1287 array_mod: '[' ']'
1288 { $$ = -1; }
1289 | OBJC_LBRAC ']'
1290 { $$ = -1; }
1291 | '[' INT ']'
1292 { $$ = $2.val; }
1293 | OBJC_LBRAC INT ']'
1294 { $$ = $2.val; }
1297 func_mod: '(' ')'
1299 $$ = new std::vector<struct type *>;
1300 cpstate->type_lists.emplace_back ($$);
1302 | '(' parameter_typelist ')'
1303 { $$ = $2; }
1306 /* We used to try to recognize pointer to member types here, but
1307 that didn't work (shift/reduce conflicts meant that these rules never
1308 got executed). The problem is that
1309 int (foo::bar::baz::bizzle)
1310 is a function type but
1311 int (foo::bar::baz::bizzle::*)
1312 is a pointer to member type. Stroustrup loses again! */
1314 type : ptype
1317 /* A helper production that recognizes scalar types that can validly
1318 be used with _Complex. */
1320 scalar_type:
1321 INT_KEYWORD
1322 { $$ = lookup_signed_typename (pstate->language (),
1323 "int"); }
1324 | LONG
1325 { $$ = lookup_signed_typename (pstate->language (),
1326 "long"); }
1327 | SHORT
1328 { $$ = lookup_signed_typename (pstate->language (),
1329 "short"); }
1330 | LONG INT_KEYWORD
1331 { $$ = lookup_signed_typename (pstate->language (),
1332 "long"); }
1333 | LONG SIGNED_KEYWORD INT_KEYWORD
1334 { $$ = lookup_signed_typename (pstate->language (),
1335 "long"); }
1336 | LONG SIGNED_KEYWORD
1337 { $$ = lookup_signed_typename (pstate->language (),
1338 "long"); }
1339 | SIGNED_KEYWORD LONG INT_KEYWORD
1340 { $$ = lookup_signed_typename (pstate->language (),
1341 "long"); }
1342 | UNSIGNED LONG INT_KEYWORD
1343 { $$ = lookup_unsigned_typename (pstate->language (),
1344 "long"); }
1345 | LONG UNSIGNED INT_KEYWORD
1346 { $$ = lookup_unsigned_typename (pstate->language (),
1347 "long"); }
1348 | LONG UNSIGNED
1349 { $$ = lookup_unsigned_typename (pstate->language (),
1350 "long"); }
1351 | LONG LONG
1352 { $$ = lookup_signed_typename (pstate->language (),
1353 "long long"); }
1354 | LONG LONG INT_KEYWORD
1355 { $$ = lookup_signed_typename (pstate->language (),
1356 "long long"); }
1357 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1358 { $$ = lookup_signed_typename (pstate->language (),
1359 "long long"); }
1360 | LONG LONG SIGNED_KEYWORD
1361 { $$ = lookup_signed_typename (pstate->language (),
1362 "long long"); }
1363 | SIGNED_KEYWORD LONG LONG
1364 { $$ = lookup_signed_typename (pstate->language (),
1365 "long long"); }
1366 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1367 { $$ = lookup_signed_typename (pstate->language (),
1368 "long long"); }
1369 | UNSIGNED LONG LONG
1370 { $$ = lookup_unsigned_typename (pstate->language (),
1371 "long long"); }
1372 | UNSIGNED LONG LONG INT_KEYWORD
1373 { $$ = lookup_unsigned_typename (pstate->language (),
1374 "long long"); }
1375 | LONG LONG UNSIGNED
1376 { $$ = lookup_unsigned_typename (pstate->language (),
1377 "long long"); }
1378 | LONG LONG UNSIGNED INT_KEYWORD
1379 { $$ = lookup_unsigned_typename (pstate->language (),
1380 "long long"); }
1381 | SHORT INT_KEYWORD
1382 { $$ = lookup_signed_typename (pstate->language (),
1383 "short"); }
1384 | SHORT SIGNED_KEYWORD INT_KEYWORD
1385 { $$ = lookup_signed_typename (pstate->language (),
1386 "short"); }
1387 | SHORT SIGNED_KEYWORD
1388 { $$ = lookup_signed_typename (pstate->language (),
1389 "short"); }
1390 | UNSIGNED SHORT INT_KEYWORD
1391 { $$ = lookup_unsigned_typename (pstate->language (),
1392 "short"); }
1393 | SHORT UNSIGNED
1394 { $$ = lookup_unsigned_typename (pstate->language (),
1395 "short"); }
1396 | SHORT UNSIGNED INT_KEYWORD
1397 { $$ = lookup_unsigned_typename (pstate->language (),
1398 "short"); }
1399 | DOUBLE_KEYWORD
1400 { $$ = lookup_typename (pstate->language (),
1401 "double",
1402 NULL,
1403 0); }
1404 | FLOAT_KEYWORD
1405 { $$ = lookup_typename (pstate->language (),
1406 "float",
1407 NULL,
1408 0); }
1409 | LONG DOUBLE_KEYWORD
1410 { $$ = lookup_typename (pstate->language (),
1411 "long double",
1412 NULL,
1413 0); }
1414 | UNSIGNED type_name
1415 { $$ = lookup_unsigned_typename (pstate->language (),
1416 $2.type->name ()); }
1417 | UNSIGNED
1418 { $$ = lookup_unsigned_typename (pstate->language (),
1419 "int"); }
1420 | SIGNED_KEYWORD type_name
1421 { $$ = lookup_signed_typename (pstate->language (),
1422 $2.type->name ()); }
1423 | SIGNED_KEYWORD
1424 { $$ = lookup_signed_typename (pstate->language (),
1425 "int"); }
1428 /* Implements (approximately): (type-qualifier)* type-specifier.
1430 When type-specifier is only ever a single word, like 'float' then these
1431 arrive as pre-built TYPENAME tokens thanks to the classify_name
1432 function. However, when a type-specifier can contain multiple words,
1433 for example 'double' can appear as just 'double' or 'long double', and
1434 similarly 'long' can appear as just 'long' or in 'long double', then
1435 these type-specifiers are parsed into their own tokens in the function
1436 lex_one_token and the ident_tokens array. These separate tokens are all
1437 recognised here. */
1438 typebase
1439 : TYPENAME
1440 { $$ = $1.type; }
1441 | scalar_type
1442 { $$ = $1; }
1443 | COMPLEX scalar_type
1445 $$ = init_complex_type (nullptr, $2);
1447 | STRUCT name
1448 { $$
1449 = lookup_struct (copy_name ($2).c_str (),
1450 pstate->expression_context_block);
1452 | STRUCT COMPLETE
1454 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1455 "", 0);
1456 $$ = NULL;
1458 | STRUCT name COMPLETE
1460 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1461 $2.ptr, $2.length);
1462 $$ = NULL;
1464 | CLASS name
1465 { $$ = lookup_struct
1466 (copy_name ($2).c_str (),
1467 pstate->expression_context_block);
1469 | CLASS COMPLETE
1471 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1472 "", 0);
1473 $$ = NULL;
1475 | CLASS name COMPLETE
1477 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1478 $2.ptr, $2.length);
1479 $$ = NULL;
1481 | UNION name
1482 { $$
1483 = lookup_union (copy_name ($2).c_str (),
1484 pstate->expression_context_block);
1486 | UNION COMPLETE
1488 pstate->mark_completion_tag (TYPE_CODE_UNION,
1489 "", 0);
1490 $$ = NULL;
1492 | UNION name COMPLETE
1494 pstate->mark_completion_tag (TYPE_CODE_UNION,
1495 $2.ptr, $2.length);
1496 $$ = NULL;
1498 | ENUM name
1499 { $$ = lookup_enum (copy_name ($2).c_str (),
1500 pstate->expression_context_block);
1502 | ENUM COMPLETE
1504 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1505 $$ = NULL;
1507 | ENUM name COMPLETE
1509 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1510 $2.length);
1511 $$ = NULL;
1513 /* It appears that this rule for templates is never
1514 reduced; template recognition happens by lookahead
1515 in the token processing code in yylex. */
1516 | TEMPLATE name '<' type '>'
1517 { $$ = lookup_template_type
1518 (copy_name($2).c_str (), $4,
1519 pstate->expression_context_block);
1521 | qualifier_seq_noopt typebase
1522 { $$ = cpstate->type_stack.follow_types ($2); }
1523 | typebase qualifier_seq_noopt
1524 { $$ = cpstate->type_stack.follow_types ($1); }
1527 type_name: TYPENAME
1528 | INT_KEYWORD
1530 $$.stoken.ptr = "int";
1531 $$.stoken.length = 3;
1532 $$.type = lookup_signed_typename (pstate->language (),
1533 "int");
1535 | LONG
1537 $$.stoken.ptr = "long";
1538 $$.stoken.length = 4;
1539 $$.type = lookup_signed_typename (pstate->language (),
1540 "long");
1542 | SHORT
1544 $$.stoken.ptr = "short";
1545 $$.stoken.length = 5;
1546 $$.type = lookup_signed_typename (pstate->language (),
1547 "short");
1551 parameter_typelist:
1552 nonempty_typelist
1553 { check_parameter_typelist ($1); }
1554 | nonempty_typelist ',' DOTDOTDOT
1556 $1->push_back (NULL);
1557 check_parameter_typelist ($1);
1558 $$ = $1;
1562 nonempty_typelist
1563 : type
1565 std::vector<struct type *> *typelist
1566 = new std::vector<struct type *>;
1567 cpstate->type_lists.emplace_back (typelist);
1569 typelist->push_back ($1);
1570 $$ = typelist;
1572 | nonempty_typelist ',' type
1574 $1->push_back ($3);
1575 $$ = $1;
1579 ptype : typebase
1580 | ptype abs_decl
1582 cpstate->type_stack.push ($2);
1583 $$ = cpstate->type_stack.follow_types ($1);
1587 conversion_type_id: typebase conversion_declarator
1588 { $$ = cpstate->type_stack.follow_types ($1); }
1591 conversion_declarator: /* Nothing. */
1592 | ptr_operator conversion_declarator
1595 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1596 | VOLATILE_KEYWORD CONST_KEYWORD
1599 const_or_volatile_noopt: const_and_volatile
1600 { cpstate->type_stack.insert (tp_const);
1601 cpstate->type_stack.insert (tp_volatile);
1603 | CONST_KEYWORD
1604 { cpstate->type_stack.insert (tp_const); }
1605 | VOLATILE_KEYWORD
1606 { cpstate->type_stack.insert (tp_volatile); }
1609 oper: OPERATOR NEW
1610 { $$ = operator_stoken (" new"); }
1611 | OPERATOR DELETE
1612 { $$ = operator_stoken (" delete"); }
1613 | OPERATOR NEW '[' ']'
1614 { $$ = operator_stoken (" new[]"); }
1615 | OPERATOR DELETE '[' ']'
1616 { $$ = operator_stoken (" delete[]"); }
1617 | OPERATOR NEW OBJC_LBRAC ']'
1618 { $$ = operator_stoken (" new[]"); }
1619 | OPERATOR DELETE OBJC_LBRAC ']'
1620 { $$ = operator_stoken (" delete[]"); }
1621 | OPERATOR '+'
1622 { $$ = operator_stoken ("+"); }
1623 | OPERATOR '-'
1624 { $$ = operator_stoken ("-"); }
1625 | OPERATOR '*'
1626 { $$ = operator_stoken ("*"); }
1627 | OPERATOR '/'
1628 { $$ = operator_stoken ("/"); }
1629 | OPERATOR '%'
1630 { $$ = operator_stoken ("%"); }
1631 | OPERATOR '^'
1632 { $$ = operator_stoken ("^"); }
1633 | OPERATOR '&'
1634 { $$ = operator_stoken ("&"); }
1635 | OPERATOR '|'
1636 { $$ = operator_stoken ("|"); }
1637 | OPERATOR '~'
1638 { $$ = operator_stoken ("~"); }
1639 | OPERATOR '!'
1640 { $$ = operator_stoken ("!"); }
1641 | OPERATOR '='
1642 { $$ = operator_stoken ("="); }
1643 | OPERATOR '<'
1644 { $$ = operator_stoken ("<"); }
1645 | OPERATOR '>'
1646 { $$ = operator_stoken (">"); }
1647 | OPERATOR ASSIGN_MODIFY
1648 { const char *op = " unknown";
1649 switch ($2)
1651 case BINOP_RSH:
1652 op = ">>=";
1653 break;
1654 case BINOP_LSH:
1655 op = "<<=";
1656 break;
1657 case BINOP_ADD:
1658 op = "+=";
1659 break;
1660 case BINOP_SUB:
1661 op = "-=";
1662 break;
1663 case BINOP_MUL:
1664 op = "*=";
1665 break;
1666 case BINOP_DIV:
1667 op = "/=";
1668 break;
1669 case BINOP_REM:
1670 op = "%=";
1671 break;
1672 case BINOP_BITWISE_IOR:
1673 op = "|=";
1674 break;
1675 case BINOP_BITWISE_AND:
1676 op = "&=";
1677 break;
1678 case BINOP_BITWISE_XOR:
1679 op = "^=";
1680 break;
1681 default:
1682 break;
1685 $$ = operator_stoken (op);
1687 | OPERATOR LSH
1688 { $$ = operator_stoken ("<<"); }
1689 | OPERATOR RSH
1690 { $$ = operator_stoken (">>"); }
1691 | OPERATOR EQUAL
1692 { $$ = operator_stoken ("=="); }
1693 | OPERATOR NOTEQUAL
1694 { $$ = operator_stoken ("!="); }
1695 | OPERATOR LEQ
1696 { $$ = operator_stoken ("<="); }
1697 | OPERATOR GEQ
1698 { $$ = operator_stoken (">="); }
1699 | OPERATOR ANDAND
1700 { $$ = operator_stoken ("&&"); }
1701 | OPERATOR OROR
1702 { $$ = operator_stoken ("||"); }
1703 | OPERATOR INCREMENT
1704 { $$ = operator_stoken ("++"); }
1705 | OPERATOR DECREMENT
1706 { $$ = operator_stoken ("--"); }
1707 | OPERATOR ','
1708 { $$ = operator_stoken (","); }
1709 | OPERATOR ARROW_STAR
1710 { $$ = operator_stoken ("->*"); }
1711 | OPERATOR ARROW
1712 { $$ = operator_stoken ("->"); }
1713 | OPERATOR '(' ')'
1714 { $$ = operator_stoken ("()"); }
1715 | OPERATOR '[' ']'
1716 { $$ = operator_stoken ("[]"); }
1717 | OPERATOR OBJC_LBRAC ']'
1718 { $$ = operator_stoken ("[]"); }
1719 | OPERATOR conversion_type_id
1720 { string_file buf;
1722 c_print_type ($2, NULL, &buf, -1, 0,
1723 &type_print_raw_options);
1724 std::string name = std::move (buf.string ());
1726 /* This also needs canonicalization. */
1727 gdb::unique_xmalloc_ptr<char> canon
1728 = cp_canonicalize_string (name.c_str ());
1729 if (canon != nullptr)
1730 name = canon.get ();
1731 $$ = operator_stoken ((" " + name).c_str ());
1735 /* This rule exists in order to allow some tokens that would not normally
1736 match the 'name' rule to appear as fields within a struct. The example
1737 that initially motivated this was the RISC-V target which models the
1738 floating point registers as a union with fields called 'float' and
1739 'double'. */
1740 field_name
1741 : name
1742 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1743 | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
1744 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1745 | LONG { $$ = typename_stoken ("long"); }
1746 | SHORT { $$ = typename_stoken ("short"); }
1747 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1748 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1751 name : NAME { $$ = $1.stoken; }
1752 | BLOCKNAME { $$ = $1.stoken; }
1753 | TYPENAME { $$ = $1.stoken; }
1754 | NAME_OR_INT { $$ = $1.stoken; }
1755 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1756 | oper { $$ = $1; }
1759 name_not_typename : NAME
1760 | BLOCKNAME
1761 /* These would be useful if name_not_typename was useful, but it is just
1762 a fake for "variable", so these cause reduce/reduce conflicts because
1763 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1764 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1765 context where only a name could occur, this might be useful.
1766 | NAME_OR_INT
1768 | oper
1770 struct field_of_this_result is_a_field_of_this;
1772 $$.stoken = $1;
1773 $$.sym
1774 = lookup_symbol ($1.ptr,
1775 pstate->expression_context_block,
1776 VAR_DOMAIN,
1777 &is_a_field_of_this);
1778 $$.is_a_field_of_this
1779 = is_a_field_of_this.type != NULL;
1781 | UNKNOWN_CPP_NAME
1786 /* Like write_exp_string, but prepends a '~'. */
1788 static void
1789 write_destructor_name (struct parser_state *par_state, struct stoken token)
1791 char *copy = (char *) alloca (token.length + 1);
1793 copy[0] = '~';
1794 memcpy (&copy[1], token.ptr, token.length);
1796 token.ptr = copy;
1797 ++token.length;
1799 write_exp_string (par_state, token);
1802 /* Returns a stoken of the operator name given by OP (which does not
1803 include the string "operator"). */
1805 static struct stoken
1806 operator_stoken (const char *op)
1808 struct stoken st = { NULL, 0 };
1809 char *buf;
1811 st.length = CP_OPERATOR_LEN + strlen (op);
1812 buf = (char *) malloc (st.length + 1);
1813 strcpy (buf, CP_OPERATOR_STR);
1814 strcat (buf, op);
1815 st.ptr = buf;
1817 /* The toplevel (c_parse) will free the memory allocated here. */
1818 cpstate->strings.emplace_back (buf);
1819 return st;
1822 /* Returns a stoken of the type named TYPE. */
1824 static struct stoken
1825 typename_stoken (const char *type)
1827 struct stoken st = { type, 0 };
1828 st.length = strlen (type);
1829 return st;
1832 /* Return true if the type is aggregate-like. */
1834 static int
1835 type_aggregate_p (struct type *type)
1837 return (type->code () == TYPE_CODE_STRUCT
1838 || type->code () == TYPE_CODE_UNION
1839 || type->code () == TYPE_CODE_NAMESPACE
1840 || (type->code () == TYPE_CODE_ENUM
1841 && TYPE_DECLARED_CLASS (type)));
1844 /* Validate a parameter typelist. */
1846 static void
1847 check_parameter_typelist (std::vector<struct type *> *params)
1849 struct type *type;
1850 int ix;
1852 for (ix = 0; ix < params->size (); ++ix)
1854 type = (*params)[ix];
1855 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1857 if (ix == 0)
1859 if (params->size () == 1)
1861 /* Ok. */
1862 break;
1864 error (_("parameter types following 'void'"));
1866 else
1867 error (_("'void' invalid as parameter type"));
1872 /* Take care of parsing a number (anything that starts with a digit).
1873 Set yylval and return the token type; update lexptr.
1874 LEN is the number of characters in it. */
1876 /*** Needs some error checking for the float case ***/
1878 static int
1879 parse_number (struct parser_state *par_state,
1880 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1882 ULONGEST n = 0;
1883 ULONGEST prevn = 0;
1884 ULONGEST un;
1886 int i = 0;
1887 int c;
1888 int base = input_radix;
1889 int unsigned_p = 0;
1891 /* Number of "L" suffixes encountered. */
1892 int long_p = 0;
1894 /* Imaginary number. */
1895 bool imaginary_p = false;
1897 /* We have found a "L" or "U" (or "i") suffix. */
1898 int found_suffix = 0;
1900 ULONGEST high_bit;
1901 struct type *signed_type;
1902 struct type *unsigned_type;
1903 char *p;
1905 p = (char *) alloca (len);
1906 memcpy (p, buf, len);
1908 if (parsed_float)
1910 if (len >= 1 && p[len - 1] == 'i')
1912 imaginary_p = true;
1913 --len;
1916 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1917 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1919 putithere->typed_val_float.type
1920 = parse_type (par_state)->builtin_decfloat;
1921 len -= 2;
1923 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1925 putithere->typed_val_float.type
1926 = parse_type (par_state)->builtin_decdouble;
1927 len -= 2;
1929 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1931 putithere->typed_val_float.type
1932 = parse_type (par_state)->builtin_declong;
1933 len -= 2;
1935 /* Handle suffixes: 'f' for float, 'l' for long double. */
1936 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1938 putithere->typed_val_float.type
1939 = parse_type (par_state)->builtin_float;
1940 len -= 1;
1942 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1944 putithere->typed_val_float.type
1945 = parse_type (par_state)->builtin_long_double;
1946 len -= 1;
1948 /* Default type for floating-point literals is double. */
1949 else
1951 putithere->typed_val_float.type
1952 = parse_type (par_state)->builtin_double;
1955 if (!parse_float (p, len,
1956 putithere->typed_val_float.type,
1957 putithere->typed_val_float.val))
1958 return ERROR;
1960 if (imaginary_p)
1961 putithere->typed_val_float.type
1962 = init_complex_type (nullptr, putithere->typed_val_float.type);
1964 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
1967 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1968 if (p[0] == '0' && len > 1)
1969 switch (p[1])
1971 case 'x':
1972 case 'X':
1973 if (len >= 3)
1975 p += 2;
1976 base = 16;
1977 len -= 2;
1979 break;
1981 case 'b':
1982 case 'B':
1983 if (len >= 3)
1985 p += 2;
1986 base = 2;
1987 len -= 2;
1989 break;
1991 case 't':
1992 case 'T':
1993 case 'd':
1994 case 'D':
1995 if (len >= 3)
1997 p += 2;
1998 base = 10;
1999 len -= 2;
2001 break;
2003 default:
2004 base = 8;
2005 break;
2008 while (len-- > 0)
2010 c = *p++;
2011 if (c >= 'A' && c <= 'Z')
2012 c += 'a' - 'A';
2013 if (c != 'l' && c != 'u' && c != 'i')
2014 n *= base;
2015 if (c >= '0' && c <= '9')
2017 if (found_suffix)
2018 return ERROR;
2019 n += i = c - '0';
2021 else
2023 if (base > 10 && c >= 'a' && c <= 'f')
2025 if (found_suffix)
2026 return ERROR;
2027 n += i = c - 'a' + 10;
2029 else if (c == 'l')
2031 ++long_p;
2032 found_suffix = 1;
2034 else if (c == 'u')
2036 unsigned_p = 1;
2037 found_suffix = 1;
2039 else if (c == 'i')
2041 imaginary_p = true;
2042 found_suffix = 1;
2044 else
2045 return ERROR; /* Char not a digit */
2047 if (i >= base)
2048 return ERROR; /* Invalid digit in this base */
2050 /* Portably test for overflow (only works for nonzero values, so make
2051 a second check for zero). FIXME: Can't we just make n and prevn
2052 unsigned and avoid this? */
2053 if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
2054 unsigned_p = 1; /* Try something unsigned */
2056 /* Portably test for unsigned overflow.
2057 FIXME: This check is wrong; for example it doesn't find overflow
2058 on 0x123456789 when LONGEST is 32 bits. */
2059 if (c != 'l' && c != 'u' && c != 'i' && n != 0)
2061 if (unsigned_p && prevn >= n)
2062 error (_("Numeric constant too large."));
2064 prevn = n;
2067 /* An integer constant is an int, a long, or a long long. An L
2068 suffix forces it to be long; an LL suffix forces it to be long
2069 long. If not forced to a larger size, it gets the first type of
2070 the above that it fits in. To figure out whether it fits, we
2071 shift it right and see whether anything remains. Note that we
2072 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2073 operation, because many compilers will warn about such a shift
2074 (which always produces a zero result). Sometimes gdbarch_int_bit
2075 or gdbarch_long_bit will be that big, sometimes not. To deal with
2076 the case where it is we just always shift the value more than
2077 once, with fewer bits each time. */
2079 un = n >> 2;
2080 if (long_p == 0
2081 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2083 high_bit
2084 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2086 /* A large decimal (not hex or octal) constant (between INT_MAX
2087 and UINT_MAX) is a long or unsigned long, according to ANSI,
2088 never an unsigned int, but this code treats it as unsigned
2089 int. This probably should be fixed. GCC gives a warning on
2090 such constants. */
2092 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2093 signed_type = parse_type (par_state)->builtin_int;
2095 else if (long_p <= 1
2096 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2098 high_bit
2099 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2100 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2101 signed_type = parse_type (par_state)->builtin_long;
2103 else
2105 int shift;
2106 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2107 < gdbarch_long_long_bit (par_state->gdbarch ()))
2108 /* A long long does not fit in a LONGEST. */
2109 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2110 else
2111 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2112 high_bit = (ULONGEST) 1 << shift;
2113 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2114 signed_type = parse_type (par_state)->builtin_long_long;
2117 putithere->typed_val_int.val = n;
2119 /* If the high bit of the worked out type is set then this number
2120 has to be unsigned. */
2122 if (unsigned_p || (n & high_bit))
2124 putithere->typed_val_int.type = unsigned_type;
2126 else
2128 putithere->typed_val_int.type = signed_type;
2131 if (imaginary_p)
2132 putithere->typed_val_int.type
2133 = init_complex_type (nullptr, putithere->typed_val_int.type);
2135 return imaginary_p ? COMPLEX_INT : INT;
2138 /* Temporary obstack used for holding strings. */
2139 static struct obstack tempbuf;
2140 static int tempbuf_init;
2142 /* Parse a C escape sequence. The initial backslash of the sequence
2143 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2144 last character of the sequence. If OUTPUT is not NULL, the
2145 translated form of the escape sequence will be written there. If
2146 OUTPUT is NULL, no output is written and the call will only affect
2147 *PTR. If an escape sequence is expressed in target bytes, then the
2148 entire sequence will simply be copied to OUTPUT. Return 1 if any
2149 character was emitted, 0 otherwise. */
2152 c_parse_escape (const char **ptr, struct obstack *output)
2154 const char *tokptr = *ptr;
2155 int result = 1;
2157 /* Some escape sequences undergo character set conversion. Those we
2158 translate here. */
2159 switch (*tokptr)
2161 /* Hex escapes do not undergo character set conversion, so keep
2162 the escape sequence for later. */
2163 case 'x':
2164 if (output)
2165 obstack_grow_str (output, "\\x");
2166 ++tokptr;
2167 if (!ISXDIGIT (*tokptr))
2168 error (_("\\x escape without a following hex digit"));
2169 while (ISXDIGIT (*tokptr))
2171 if (output)
2172 obstack_1grow (output, *tokptr);
2173 ++tokptr;
2175 break;
2177 /* Octal escapes do not undergo character set conversion, so
2178 keep the escape sequence for later. */
2179 case '0':
2180 case '1':
2181 case '2':
2182 case '3':
2183 case '4':
2184 case '5':
2185 case '6':
2186 case '7':
2188 int i;
2189 if (output)
2190 obstack_grow_str (output, "\\");
2191 for (i = 0;
2192 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2193 ++i)
2195 if (output)
2196 obstack_1grow (output, *tokptr);
2197 ++tokptr;
2200 break;
2202 /* We handle UCNs later. We could handle them here, but that
2203 would mean a spurious error in the case where the UCN could
2204 be converted to the target charset but not the host
2205 charset. */
2206 case 'u':
2207 case 'U':
2209 char c = *tokptr;
2210 int i, len = c == 'U' ? 8 : 4;
2211 if (output)
2213 obstack_1grow (output, '\\');
2214 obstack_1grow (output, *tokptr);
2216 ++tokptr;
2217 if (!ISXDIGIT (*tokptr))
2218 error (_("\\%c escape without a following hex digit"), c);
2219 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2221 if (output)
2222 obstack_1grow (output, *tokptr);
2223 ++tokptr;
2226 break;
2228 /* We must pass backslash through so that it does not
2229 cause quoting during the second expansion. */
2230 case '\\':
2231 if (output)
2232 obstack_grow_str (output, "\\\\");
2233 ++tokptr;
2234 break;
2236 /* Escapes which undergo conversion. */
2237 case 'a':
2238 if (output)
2239 obstack_1grow (output, '\a');
2240 ++tokptr;
2241 break;
2242 case 'b':
2243 if (output)
2244 obstack_1grow (output, '\b');
2245 ++tokptr;
2246 break;
2247 case 'f':
2248 if (output)
2249 obstack_1grow (output, '\f');
2250 ++tokptr;
2251 break;
2252 case 'n':
2253 if (output)
2254 obstack_1grow (output, '\n');
2255 ++tokptr;
2256 break;
2257 case 'r':
2258 if (output)
2259 obstack_1grow (output, '\r');
2260 ++tokptr;
2261 break;
2262 case 't':
2263 if (output)
2264 obstack_1grow (output, '\t');
2265 ++tokptr;
2266 break;
2267 case 'v':
2268 if (output)
2269 obstack_1grow (output, '\v');
2270 ++tokptr;
2271 break;
2273 /* GCC extension. */
2274 case 'e':
2275 if (output)
2276 obstack_1grow (output, HOST_ESCAPE_CHAR);
2277 ++tokptr;
2278 break;
2280 /* Backslash-newline expands to nothing at all. */
2281 case '\n':
2282 ++tokptr;
2283 result = 0;
2284 break;
2286 /* A few escapes just expand to the character itself. */
2287 case '\'':
2288 case '\"':
2289 case '?':
2290 /* GCC extensions. */
2291 case '(':
2292 case '{':
2293 case '[':
2294 case '%':
2295 /* Unrecognized escapes turn into the character itself. */
2296 default:
2297 if (output)
2298 obstack_1grow (output, *tokptr);
2299 ++tokptr;
2300 break;
2302 *ptr = tokptr;
2303 return result;
2306 /* Parse a string or character literal from TOKPTR. The string or
2307 character may be wide or unicode. *OUTPTR is set to just after the
2308 end of the literal in the input string. The resulting token is
2309 stored in VALUE. This returns a token value, either STRING or
2310 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2311 number of host characters in the literal. */
2313 static int
2314 parse_string_or_char (const char *tokptr, const char **outptr,
2315 struct typed_stoken *value, int *host_chars)
2317 int quote;
2318 c_string_type type;
2319 int is_objc = 0;
2321 /* Build the gdb internal form of the input string in tempbuf. Note
2322 that the buffer is null byte terminated *only* for the
2323 convenience of debugging gdb itself and printing the buffer
2324 contents when the buffer contains no embedded nulls. Gdb does
2325 not depend upon the buffer being null byte terminated, it uses
2326 the length string instead. This allows gdb to handle C strings
2327 (as well as strings in other languages) with embedded null
2328 bytes */
2330 if (!tempbuf_init)
2331 tempbuf_init = 1;
2332 else
2333 obstack_free (&tempbuf, NULL);
2334 obstack_init (&tempbuf);
2336 /* Record the string type. */
2337 if (*tokptr == 'L')
2339 type = C_WIDE_STRING;
2340 ++tokptr;
2342 else if (*tokptr == 'u')
2344 type = C_STRING_16;
2345 ++tokptr;
2347 else if (*tokptr == 'U')
2349 type = C_STRING_32;
2350 ++tokptr;
2352 else if (*tokptr == '@')
2354 /* An Objective C string. */
2355 is_objc = 1;
2356 type = C_STRING;
2357 ++tokptr;
2359 else
2360 type = C_STRING;
2362 /* Skip the quote. */
2363 quote = *tokptr;
2364 if (quote == '\'')
2365 type |= C_CHAR;
2366 ++tokptr;
2368 *host_chars = 0;
2370 while (*tokptr)
2372 char c = *tokptr;
2373 if (c == '\\')
2375 ++tokptr;
2376 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2378 else if (c == quote)
2379 break;
2380 else
2382 obstack_1grow (&tempbuf, c);
2383 ++tokptr;
2384 /* FIXME: this does the wrong thing with multi-byte host
2385 characters. We could use mbrlen here, but that would
2386 make "set host-charset" a bit less useful. */
2387 ++*host_chars;
2391 if (*tokptr != quote)
2393 if (quote == '"')
2394 error (_("Unterminated string in expression."));
2395 else
2396 error (_("Unmatched single quote."));
2398 ++tokptr;
2400 value->type = type;
2401 value->ptr = (char *) obstack_base (&tempbuf);
2402 value->length = obstack_object_size (&tempbuf);
2404 *outptr = tokptr;
2406 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2409 /* This is used to associate some attributes with a token. */
2411 enum token_flag
2413 /* If this bit is set, the token is C++-only. */
2415 FLAG_CXX = 1,
2417 /* If this bit is set, the token is C-only. */
2419 FLAG_C = 2,
2421 /* If this bit is set, the token is conditional: if there is a
2422 symbol of the same name, then the token is a symbol; otherwise,
2423 the token is a keyword. */
2425 FLAG_SHADOW = 4
2427 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2429 struct token
2431 const char *oper;
2432 int token;
2433 enum exp_opcode opcode;
2434 token_flags flags;
2437 static const struct token tokentab3[] =
2439 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2440 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2441 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2442 {"...", DOTDOTDOT, BINOP_END, 0}
2445 static const struct token tokentab2[] =
2447 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2448 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2449 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2450 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2451 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2452 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2453 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2454 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2455 {"++", INCREMENT, BINOP_END, 0},
2456 {"--", DECREMENT, BINOP_END, 0},
2457 {"->", ARROW, BINOP_END, 0},
2458 {"&&", ANDAND, BINOP_END, 0},
2459 {"||", OROR, BINOP_END, 0},
2460 /* "::" is *not* only C++: gdb overrides its meaning in several
2461 different ways, e.g., 'filename'::func, function::variable. */
2462 {"::", COLONCOLON, BINOP_END, 0},
2463 {"<<", LSH, BINOP_END, 0},
2464 {">>", RSH, BINOP_END, 0},
2465 {"==", EQUAL, BINOP_END, 0},
2466 {"!=", NOTEQUAL, BINOP_END, 0},
2467 {"<=", LEQ, BINOP_END, 0},
2468 {">=", GEQ, BINOP_END, 0},
2469 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2472 /* Identifier-like tokens. Only type-specifiers than can appear in
2473 multi-word type names (for example 'double' can appear in 'long
2474 double') need to be listed here. type-specifiers that are only ever
2475 single word (like 'char') are handled by the classify_name function. */
2476 static const struct token ident_tokens[] =
2478 {"unsigned", UNSIGNED, OP_NULL, 0},
2479 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2480 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2481 {"struct", STRUCT, OP_NULL, 0},
2482 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2483 {"sizeof", SIZEOF, OP_NULL, 0},
2484 {"_Alignof", ALIGNOF, OP_NULL, 0},
2485 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2486 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2487 {"float", FLOAT_KEYWORD, OP_NULL, 0},
2488 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2489 {"class", CLASS, OP_NULL, FLAG_CXX},
2490 {"union", UNION, OP_NULL, 0},
2491 {"short", SHORT, OP_NULL, 0},
2492 {"const", CONST_KEYWORD, OP_NULL, 0},
2493 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2494 {"__restrict__", RESTRICT, OP_NULL, 0},
2495 {"__restrict", RESTRICT, OP_NULL, 0},
2496 {"_Atomic", ATOMIC, OP_NULL, 0},
2497 {"enum", ENUM, OP_NULL, 0},
2498 {"long", LONG, OP_NULL, 0},
2499 {"_Complex", COMPLEX, OP_NULL, 0},
2500 {"__complex__", COMPLEX, OP_NULL, 0},
2502 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2503 {"int", INT_KEYWORD, OP_NULL, 0},
2504 {"new", NEW, OP_NULL, FLAG_CXX},
2505 {"delete", DELETE, OP_NULL, FLAG_CXX},
2506 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2508 {"and", ANDAND, BINOP_END, FLAG_CXX},
2509 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2510 {"bitand", '&', OP_NULL, FLAG_CXX},
2511 {"bitor", '|', OP_NULL, FLAG_CXX},
2512 {"compl", '~', OP_NULL, FLAG_CXX},
2513 {"not", '!', OP_NULL, FLAG_CXX},
2514 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2515 {"or", OROR, BINOP_END, FLAG_CXX},
2516 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2517 {"xor", '^', OP_NULL, FLAG_CXX},
2518 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2520 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2521 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2522 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2523 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2525 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2526 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2527 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2528 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2529 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2531 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2535 static void
2536 scan_macro_expansion (const char *expansion)
2538 /* We'd better not be trying to push the stack twice. */
2539 gdb_assert (! cpstate->macro_original_text);
2541 /* Copy to the obstack. */
2542 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2544 /* Save the old lexptr value, so we can return to it when we're done
2545 parsing the expanded text. */
2546 cpstate->macro_original_text = pstate->lexptr;
2547 pstate->lexptr = copy;
2550 static int
2551 scanning_macro_expansion (void)
2553 return cpstate->macro_original_text != 0;
2556 static void
2557 finished_macro_expansion (void)
2559 /* There'd better be something to pop back to. */
2560 gdb_assert (cpstate->macro_original_text);
2562 /* Pop back to the original text. */
2563 pstate->lexptr = cpstate->macro_original_text;
2564 cpstate->macro_original_text = 0;
2567 /* Return true iff the token represents a C++ cast operator. */
2569 static int
2570 is_cast_operator (const char *token, int len)
2572 return (! strncmp (token, "dynamic_cast", len)
2573 || ! strncmp (token, "static_cast", len)
2574 || ! strncmp (token, "reinterpret_cast", len)
2575 || ! strncmp (token, "const_cast", len));
2578 /* The scope used for macro expansion. */
2579 static struct macro_scope *expression_macro_scope;
2581 /* This is set if a NAME token appeared at the very end of the input
2582 string, with no whitespace separating the name from the EOF. This
2583 is used only when parsing to do field name completion. */
2584 static int saw_name_at_eof;
2586 /* This is set if the previously-returned token was a structure
2587 operator -- either '.' or ARROW. */
2588 static bool last_was_structop;
2590 /* Depth of parentheses. */
2591 static int paren_depth;
2593 /* Read one token, getting characters through lexptr. */
2595 static int
2596 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2598 int c;
2599 int namelen;
2600 unsigned int i;
2601 const char *tokstart;
2602 bool saw_structop = last_was_structop;
2604 last_was_structop = false;
2605 *is_quoted_name = false;
2607 retry:
2609 /* Check if this is a macro invocation that we need to expand. */
2610 if (! scanning_macro_expansion ())
2612 gdb::unique_xmalloc_ptr<char> expanded
2613 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
2615 if (expanded != nullptr)
2616 scan_macro_expansion (expanded.get ());
2619 pstate->prev_lexptr = pstate->lexptr;
2621 tokstart = pstate->lexptr;
2622 /* See if it is a special token of length 3. */
2623 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2624 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2626 if ((tokentab3[i].flags & FLAG_CXX) != 0
2627 && par_state->language ()->la_language != language_cplus)
2628 break;
2629 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2631 pstate->lexptr += 3;
2632 yylval.opcode = tokentab3[i].opcode;
2633 return tokentab3[i].token;
2636 /* See if it is a special token of length 2. */
2637 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2638 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2640 if ((tokentab2[i].flags & FLAG_CXX) != 0
2641 && par_state->language ()->la_language != language_cplus)
2642 break;
2643 gdb_assert ((tokentab2[i].flags & FLAG_C) == 0);
2645 pstate->lexptr += 2;
2646 yylval.opcode = tokentab2[i].opcode;
2647 if (tokentab2[i].token == ARROW)
2648 last_was_structop = 1;
2649 return tokentab2[i].token;
2652 switch (c = *tokstart)
2654 case 0:
2655 /* If we were just scanning the result of a macro expansion,
2656 then we need to resume scanning the original text.
2657 If we're parsing for field name completion, and the previous
2658 token allows such completion, return a COMPLETE token.
2659 Otherwise, we were already scanning the original text, and
2660 we're really done. */
2661 if (scanning_macro_expansion ())
2663 finished_macro_expansion ();
2664 goto retry;
2666 else if (saw_name_at_eof)
2668 saw_name_at_eof = 0;
2669 return COMPLETE;
2671 else if (par_state->parse_completion && saw_structop)
2672 return COMPLETE;
2673 else
2674 return 0;
2676 case ' ':
2677 case '\t':
2678 case '\n':
2679 pstate->lexptr++;
2680 goto retry;
2682 case '[':
2683 case '(':
2684 paren_depth++;
2685 pstate->lexptr++;
2686 if (par_state->language ()->la_language == language_objc
2687 && c == '[')
2688 return OBJC_LBRAC;
2689 return c;
2691 case ']':
2692 case ')':
2693 if (paren_depth == 0)
2694 return 0;
2695 paren_depth--;
2696 pstate->lexptr++;
2697 return c;
2699 case ',':
2700 if (pstate->comma_terminates
2701 && paren_depth == 0
2702 && ! scanning_macro_expansion ())
2703 return 0;
2704 pstate->lexptr++;
2705 return c;
2707 case '.':
2708 /* Might be a floating point number. */
2709 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2711 last_was_structop = true;
2712 goto symbol; /* Nope, must be a symbol. */
2714 /* FALL THRU. */
2716 case '0':
2717 case '1':
2718 case '2':
2719 case '3':
2720 case '4':
2721 case '5':
2722 case '6':
2723 case '7':
2724 case '8':
2725 case '9':
2727 /* It's a number. */
2728 int got_dot = 0, got_e = 0, got_p = 0, toktype;
2729 const char *p = tokstart;
2730 int hex = input_radix > 10;
2732 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2734 p += 2;
2735 hex = 1;
2737 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2739 p += 2;
2740 hex = 0;
2743 for (;; ++p)
2745 /* This test includes !hex because 'e' is a valid hex digit
2746 and thus does not indicate a floating point number when
2747 the radix is hex. */
2748 if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
2749 got_dot = got_e = 1;
2750 else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
2751 got_dot = got_p = 1;
2752 /* This test does not include !hex, because a '.' always indicates
2753 a decimal floating point number regardless of the radix. */
2754 else if (!got_dot && *p == '.')
2755 got_dot = 1;
2756 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
2757 || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
2758 && (*p == '-' || *p == '+'))
2759 /* This is the sign of the exponent, not the end of the
2760 number. */
2761 continue;
2762 /* We will take any letters or digits. parse_number will
2763 complain if past the radix, or if L or U are not final. */
2764 else if ((*p < '0' || *p > '9')
2765 && ((*p < 'a' || *p > 'z')
2766 && (*p < 'A' || *p > 'Z')))
2767 break;
2769 toktype = parse_number (par_state, tokstart, p - tokstart,
2770 got_dot | got_e | got_p, &yylval);
2771 if (toktype == ERROR)
2773 char *err_copy = (char *) alloca (p - tokstart + 1);
2775 memcpy (err_copy, tokstart, p - tokstart);
2776 err_copy[p - tokstart] = 0;
2777 error (_("Invalid number \"%s\"."), err_copy);
2779 pstate->lexptr = p;
2780 return toktype;
2783 case '@':
2785 const char *p = &tokstart[1];
2787 if (par_state->language ()->la_language == language_objc)
2789 size_t len = strlen ("selector");
2791 if (strncmp (p, "selector", len) == 0
2792 && (p[len] == '\0' || ISSPACE (p[len])))
2794 pstate->lexptr = p + len;
2795 return SELECTOR;
2797 else if (*p == '"')
2798 goto parse_string;
2801 while (ISSPACE (*p))
2802 p++;
2803 size_t len = strlen ("entry");
2804 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2805 && p[len] != '_')
2807 pstate->lexptr = &p[len];
2808 return ENTRY;
2811 /* FALLTHRU */
2812 case '+':
2813 case '-':
2814 case '*':
2815 case '/':
2816 case '%':
2817 case '|':
2818 case '&':
2819 case '^':
2820 case '~':
2821 case '!':
2822 case '<':
2823 case '>':
2824 case '?':
2825 case ':':
2826 case '=':
2827 case '{':
2828 case '}':
2829 symbol:
2830 pstate->lexptr++;
2831 return c;
2833 case 'L':
2834 case 'u':
2835 case 'U':
2836 if (tokstart[1] != '"' && tokstart[1] != '\'')
2837 break;
2838 /* Fall through. */
2839 case '\'':
2840 case '"':
2842 parse_string:
2844 int host_len;
2845 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2846 &yylval.tsval, &host_len);
2847 if (result == CHAR)
2849 if (host_len == 0)
2850 error (_("Empty character constant."));
2851 else if (host_len > 2 && c == '\'')
2853 ++tokstart;
2854 namelen = pstate->lexptr - tokstart - 1;
2855 *is_quoted_name = true;
2857 goto tryname;
2859 else if (host_len > 1)
2860 error (_("Invalid character constant."));
2862 return result;
2866 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2867 /* We must have come across a bad character (e.g. ';'). */
2868 error (_("Invalid character '%c' in expression."), c);
2870 /* It's a name. See how long it is. */
2871 namelen = 0;
2872 for (c = tokstart[namelen];
2873 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2875 /* Template parameter lists are part of the name.
2876 FIXME: This mishandles `print $a<4&&$a>3'. */
2878 if (c == '<')
2880 if (! is_cast_operator (tokstart, namelen))
2882 /* Scan ahead to get rest of the template specification. Note
2883 that we look ahead only when the '<' adjoins non-whitespace
2884 characters; for comparison expressions, e.g. "a < b > c",
2885 there must be spaces before the '<', etc. */
2886 const char *p = find_template_name_end (tokstart + namelen);
2888 if (p)
2889 namelen = p - tokstart;
2891 break;
2893 c = tokstart[++namelen];
2896 /* The token "if" terminates the expression and is NOT removed from
2897 the input stream. It doesn't count if it appears in the
2898 expansion of a macro. */
2899 if (namelen == 2
2900 && tokstart[0] == 'i'
2901 && tokstart[1] == 'f'
2902 && ! scanning_macro_expansion ())
2904 return 0;
2907 /* For the same reason (breakpoint conditions), "thread N"
2908 terminates the expression. "thread" could be an identifier, but
2909 an identifier is never followed by a number without intervening
2910 punctuation. "task" is similar. Handle abbreviations of these,
2911 similarly to breakpoint.c:find_condition_and_thread. */
2912 if (namelen >= 1
2913 && (strncmp (tokstart, "thread", namelen) == 0
2914 || strncmp (tokstart, "task", namelen) == 0)
2915 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2916 && ! scanning_macro_expansion ())
2918 const char *p = tokstart + namelen + 1;
2920 while (*p == ' ' || *p == '\t')
2921 p++;
2922 if (*p >= '0' && *p <= '9')
2923 return 0;
2926 pstate->lexptr += namelen;
2928 tryname:
2930 yylval.sval.ptr = tokstart;
2931 yylval.sval.length = namelen;
2933 /* Catch specific keywords. */
2934 std::string copy = copy_name (yylval.sval);
2935 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2936 if (copy == ident_tokens[i].oper)
2938 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2939 && par_state->language ()->la_language != language_cplus)
2940 break;
2941 if ((ident_tokens[i].flags & FLAG_C) != 0
2942 && par_state->language ()->la_language != language_c
2943 && par_state->language ()->la_language != language_objc)
2944 break;
2946 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2948 struct field_of_this_result is_a_field_of_this;
2950 if (lookup_symbol (copy.c_str (),
2951 pstate->expression_context_block,
2952 VAR_DOMAIN,
2953 (par_state->language ()->la_language
2954 == language_cplus ? &is_a_field_of_this
2955 : NULL)).symbol
2956 != NULL)
2958 /* The keyword is shadowed. */
2959 break;
2963 /* It is ok to always set this, even though we don't always
2964 strictly need to. */
2965 yylval.opcode = ident_tokens[i].opcode;
2966 return ident_tokens[i].token;
2969 if (*tokstart == '$')
2970 return DOLLAR_VARIABLE;
2972 if (pstate->parse_completion && *pstate->lexptr == '\0')
2973 saw_name_at_eof = 1;
2975 yylval.ssym.stoken = yylval.sval;
2976 yylval.ssym.sym.symbol = NULL;
2977 yylval.ssym.sym.block = NULL;
2978 yylval.ssym.is_a_field_of_this = 0;
2979 return NAME;
2982 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2983 struct token_and_value
2985 int token;
2986 YYSTYPE value;
2989 /* A FIFO of tokens that have been read but not yet returned to the
2990 parser. */
2991 static std::vector<token_and_value> token_fifo;
2993 /* Non-zero if the lexer should return tokens from the FIFO. */
2994 static int popping;
2996 /* Temporary storage for c_lex; this holds symbol names as they are
2997 built up. */
2998 static auto_obstack name_obstack;
3000 /* Classify a NAME token. The contents of the token are in `yylval'.
3001 Updates yylval and returns the new token type. BLOCK is the block
3002 in which lookups start; this can be NULL to mean the global scope.
3003 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3004 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3005 a structure operator -- either '.' or ARROW */
3007 static int
3008 classify_name (struct parser_state *par_state, const struct block *block,
3009 bool is_quoted_name, bool is_after_structop)
3011 struct block_symbol bsym;
3012 struct field_of_this_result is_a_field_of_this;
3014 std::string copy = copy_name (yylval.sval);
3016 /* Initialize this in case we *don't* use it in this call; that way
3017 we can refer to it unconditionally below. */
3018 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3020 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
3021 par_state->language ()->name_of_this ()
3022 ? &is_a_field_of_this : NULL);
3024 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
3026 yylval.ssym.sym = bsym;
3027 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3028 return BLOCKNAME;
3030 else if (!bsym.symbol)
3032 /* If we found a field of 'this', we might have erroneously
3033 found a constructor where we wanted a type name. Handle this
3034 case by noticing that we found a constructor and then look up
3035 the type tag instead. */
3036 if (is_a_field_of_this.type != NULL
3037 && is_a_field_of_this.fn_field != NULL
3038 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3041 struct field_of_this_result inner_is_a_field_of_this;
3043 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
3044 &inner_is_a_field_of_this);
3045 if (bsym.symbol != NULL)
3047 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3048 return TYPENAME;
3052 /* If we found a field on the "this" object, or we are looking
3053 up a field on a struct, then we want to prefer it over a
3054 filename. However, if the name was quoted, then it is better
3055 to check for a filename or a block, since this is the only
3056 way the user has of requiring the extension to be used. */
3057 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3058 || is_quoted_name)
3060 /* See if it's a file name. */
3061 struct symtab *symtab;
3063 symtab = lookup_symtab (copy.c_str ());
3064 if (symtab)
3066 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3067 STATIC_BLOCK);
3068 return FILENAME;
3073 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3075 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3076 return TYPENAME;
3079 /* See if it's an ObjC classname. */
3080 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3082 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3083 copy.c_str ());
3084 if (Class)
3086 struct symbol *sym;
3088 yylval.theclass.theclass = Class;
3089 sym = lookup_struct_typedef (copy.c_str (),
3090 par_state->expression_context_block, 1);
3091 if (sym)
3092 yylval.theclass.type = SYMBOL_TYPE (sym);
3093 return CLASSNAME;
3097 /* Input names that aren't symbols but ARE valid hex numbers, when
3098 the input radix permits them, can be names or numbers depending
3099 on the parse. Note we support radixes > 16 here. */
3100 if (!bsym.symbol
3101 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3102 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3104 YYSTYPE newlval; /* Its value is ignored. */
3105 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3106 0, &newlval);
3108 if (hextype == INT)
3110 yylval.ssym.sym = bsym;
3111 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3112 return NAME_OR_INT;
3116 /* Any other kind of symbol */
3117 yylval.ssym.sym = bsym;
3118 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3120 if (bsym.symbol == NULL
3121 && par_state->language ()->la_language == language_cplus
3122 && is_a_field_of_this.type == NULL
3123 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3124 return UNKNOWN_CPP_NAME;
3126 return NAME;
3129 /* Like classify_name, but used by the inner loop of the lexer, when a
3130 name might have already been seen. CONTEXT is the context type, or
3131 NULL if this is the first component of a name. */
3133 static int
3134 classify_inner_name (struct parser_state *par_state,
3135 const struct block *block, struct type *context)
3137 struct type *type;
3139 if (context == NULL)
3140 return classify_name (par_state, block, false, false);
3142 type = check_typedef (context);
3143 if (!type_aggregate_p (type))
3144 return ERROR;
3146 std::string copy = copy_name (yylval.ssym.stoken);
3147 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3148 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3149 VAR_DOMAIN);
3151 /* If no symbol was found, search for a matching base class named
3152 COPY. This will allow users to enter qualified names of class members
3153 relative to the `this' pointer. */
3154 if (yylval.ssym.sym.symbol == NULL)
3156 struct type *base_type = cp_find_type_baseclass_by_name (type,
3157 copy.c_str ());
3159 if (base_type != NULL)
3161 yylval.tsym.type = base_type;
3162 return TYPENAME;
3165 return ERROR;
3168 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3170 case LOC_BLOCK:
3171 case LOC_LABEL:
3172 /* cp_lookup_nested_symbol might have accidentally found a constructor
3173 named COPY when we really wanted a base class of the same name.
3174 Double-check this case by looking for a base class. */
3176 struct type *base_type
3177 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3179 if (base_type != NULL)
3181 yylval.tsym.type = base_type;
3182 return TYPENAME;
3185 return ERROR;
3187 case LOC_TYPEDEF:
3188 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3189 return TYPENAME;
3191 default:
3192 return NAME;
3194 internal_error (__FILE__, __LINE__, _("not reached"));
3197 /* The outer level of a two-level lexer. This calls the inner lexer
3198 to return tokens. It then either returns these tokens, or
3199 aggregates them into a larger token. This lets us work around a
3200 problem in our parsing approach, where the parser could not
3201 distinguish between qualified names and qualified types at the
3202 right point.
3204 This approach is still not ideal, because it mishandles template
3205 types. See the comment in lex_one_token for an example. However,
3206 this is still an improvement over the earlier approach, and will
3207 suffice until we move to better parsing technology. */
3209 static int
3210 yylex (void)
3212 token_and_value current;
3213 int first_was_coloncolon, last_was_coloncolon;
3214 struct type *context_type = NULL;
3215 int last_to_examine, next_to_examine, checkpoint;
3216 const struct block *search_block;
3217 bool is_quoted_name, last_lex_was_structop;
3219 if (popping && !token_fifo.empty ())
3220 goto do_pop;
3221 popping = 0;
3223 last_lex_was_structop = last_was_structop;
3225 /* Read the first token and decide what to do. Most of the
3226 subsequent code is C++-only; but also depends on seeing a "::" or
3227 name-like token. */
3228 current.token = lex_one_token (pstate, &is_quoted_name);
3229 if (current.token == NAME)
3230 current.token = classify_name (pstate, pstate->expression_context_block,
3231 is_quoted_name, last_lex_was_structop);
3232 if (pstate->language ()->la_language != language_cplus
3233 || (current.token != TYPENAME && current.token != COLONCOLON
3234 && current.token != FILENAME))
3235 return current.token;
3237 /* Read any sequence of alternating "::" and name-like tokens into
3238 the token FIFO. */
3239 current.value = yylval;
3240 token_fifo.push_back (current);
3241 last_was_coloncolon = current.token == COLONCOLON;
3242 while (1)
3244 bool ignore;
3246 /* We ignore quoted names other than the very first one.
3247 Subsequent ones do not have any special meaning. */
3248 current.token = lex_one_token (pstate, &ignore);
3249 current.value = yylval;
3250 token_fifo.push_back (current);
3252 if ((last_was_coloncolon && current.token != NAME)
3253 || (!last_was_coloncolon && current.token != COLONCOLON))
3254 break;
3255 last_was_coloncolon = !last_was_coloncolon;
3257 popping = 1;
3259 /* We always read one extra token, so compute the number of tokens
3260 to examine accordingly. */
3261 last_to_examine = token_fifo.size () - 2;
3262 next_to_examine = 0;
3264 current = token_fifo[next_to_examine];
3265 ++next_to_examine;
3267 name_obstack.clear ();
3268 checkpoint = 0;
3269 if (current.token == FILENAME)
3270 search_block = current.value.bval;
3271 else if (current.token == COLONCOLON)
3272 search_block = NULL;
3273 else
3275 gdb_assert (current.token == TYPENAME);
3276 search_block = pstate->expression_context_block;
3277 obstack_grow (&name_obstack, current.value.sval.ptr,
3278 current.value.sval.length);
3279 context_type = current.value.tsym.type;
3280 checkpoint = 1;
3283 first_was_coloncolon = current.token == COLONCOLON;
3284 last_was_coloncolon = first_was_coloncolon;
3286 while (next_to_examine <= last_to_examine)
3288 token_and_value next;
3290 next = token_fifo[next_to_examine];
3291 ++next_to_examine;
3293 if (next.token == NAME && last_was_coloncolon)
3295 int classification;
3297 yylval = next.value;
3298 classification = classify_inner_name (pstate, search_block,
3299 context_type);
3300 /* We keep going until we either run out of names, or until
3301 we have a qualified name which is not a type. */
3302 if (classification != TYPENAME && classification != NAME)
3303 break;
3305 /* Accept up to this token. */
3306 checkpoint = next_to_examine;
3308 /* Update the partial name we are constructing. */
3309 if (context_type != NULL)
3311 /* We don't want to put a leading "::" into the name. */
3312 obstack_grow_str (&name_obstack, "::");
3314 obstack_grow (&name_obstack, next.value.sval.ptr,
3315 next.value.sval.length);
3317 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3318 yylval.sval.length = obstack_object_size (&name_obstack);
3319 current.value = yylval;
3320 current.token = classification;
3322 last_was_coloncolon = 0;
3324 if (classification == NAME)
3325 break;
3327 context_type = yylval.tsym.type;
3329 else if (next.token == COLONCOLON && !last_was_coloncolon)
3330 last_was_coloncolon = 1;
3331 else
3333 /* We've reached the end of the name. */
3334 break;
3338 /* If we have a replacement token, install it as the first token in
3339 the FIFO, and delete the other constituent tokens. */
3340 if (checkpoint > 0)
3342 current.value.sval.ptr
3343 = obstack_strndup (&cpstate->expansion_obstack,
3344 current.value.sval.ptr,
3345 current.value.sval.length);
3347 token_fifo[0] = current;
3348 if (checkpoint > 1)
3349 token_fifo.erase (token_fifo.begin () + 1,
3350 token_fifo.begin () + checkpoint);
3353 do_pop:
3354 current = token_fifo[0];
3355 token_fifo.erase (token_fifo.begin ());
3356 yylval = current.value;
3357 return current.token;
3361 c_parse (struct parser_state *par_state)
3363 /* Setting up the parser state. */
3364 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3365 gdb_assert (par_state != NULL);
3366 pstate = par_state;
3368 c_parse_state cstate;
3369 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3371 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3373 if (par_state->expression_context_block)
3374 macro_scope
3375 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3376 else
3377 macro_scope = default_macro_scope ();
3378 if (! macro_scope)
3379 macro_scope = user_macro_scope ();
3381 scoped_restore restore_macro_scope
3382 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3384 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3385 parser_debug);
3387 /* Initialize some state used by the lexer. */
3388 last_was_structop = false;
3389 saw_name_at_eof = 0;
3390 paren_depth = 0;
3392 token_fifo.clear ();
3393 popping = 0;
3394 name_obstack.clear ();
3396 return yyparse ();
3399 #ifdef YYBISON
3401 /* This is called via the YYPRINT macro when parser debugging is
3402 enabled. It prints a token's value. */
3404 static void
3405 c_print_token (FILE *file, int type, YYSTYPE value)
3407 switch (type)
3409 case INT:
3410 parser_fprintf (file, "typed_val_int<%s, %s>",
3411 TYPE_SAFE_NAME (value.typed_val_int.type),
3412 pulongest (value.typed_val_int.val));
3413 break;
3415 case CHAR:
3416 case STRING:
3418 char *copy = (char *) alloca (value.tsval.length + 1);
3420 memcpy (copy, value.tsval.ptr, value.tsval.length);
3421 copy[value.tsval.length] = '\0';
3423 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3425 break;
3427 case NSSTRING:
3428 case DOLLAR_VARIABLE:
3429 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3430 break;
3432 case TYPENAME:
3433 parser_fprintf (file, "tsym<type=%s, name=%s>",
3434 TYPE_SAFE_NAME (value.tsym.type),
3435 copy_name (value.tsym.stoken).c_str ());
3436 break;
3438 case NAME:
3439 case UNKNOWN_CPP_NAME:
3440 case NAME_OR_INT:
3441 case BLOCKNAME:
3442 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3443 copy_name (value.ssym.stoken).c_str (),
3444 (value.ssym.sym.symbol == NULL
3445 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3446 value.ssym.is_a_field_of_this);
3447 break;
3449 case FILENAME:
3450 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3451 break;
3455 #endif
3457 static void
3458 yyerror (const char *msg)
3460 if (pstate->prev_lexptr)
3461 pstate->lexptr = pstate->prev_lexptr;
3463 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);