1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2019 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. */
40 #include "expression.h"
42 #include "parser-defs.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 */
51 #include "cp-support.h"
52 #include "macroscope.h"
53 #include "objc-lang.h"
54 #include "typeprint.h"
57 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
61 #define GDB_YY_REMAP_PREFIX c_
64 /* The state of the parser, used internally when we are parsing the
67 static struct parser_state *pstate = NULL;
69 /* Data that must be held for the duration of a parse. */
73 /* These are used to hold type lists and type stacks that are
74 allocated during the parse. */
75 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
76 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
78 /* Storage for some strings allocated during the parse. */
79 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
82 /* This is set and cleared in c_parse. */
84 static struct c_parse_state *cpstate;
88 static int yylex (void);
90 static void yyerror (const char *);
92 static int type_aggregate_p (struct type *);
96 /* Although the yacc "value" of an expression is not used,
97 since the result is stored in the structure being created,
98 other node types do have values. */
113 struct typed_stoken tsval;
115 struct symtoken ssym;
117 const struct block *bval;
118 enum exp_opcode opcode;
120 struct stoken_vector svec;
121 std::vector<struct type *> *tvec;
123 struct type_stack *type_stack;
125 struct objc_class_str theclass;
129 /* YYSTYPE gets defined by %union */
130 static int parse_number (struct parser_state *par_state,
131 const char *, int, int, YYSTYPE *);
132 static struct stoken operator_stoken (const char *);
133 static struct stoken typename_stoken (const char *);
134 static void check_parameter_typelist (std::vector<struct type *> *);
135 static void write_destructor_name (struct parser_state *par_state,
139 static void c_print_token (FILE *file, int type, YYSTYPE value);
140 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
144 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
146 %type <tval> type typebase
147 %type <tvec> nonempty_typelist func_mod parameter_typelist
148 /* %type <bval> block */
150 /* Fancy type parsing. */
152 %type <lval> array_mod
153 %type <tval> conversion_type_id
155 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
157 %token <typed_val_int> INT
158 %token <typed_val_float> FLOAT
160 /* Both NAME and TYPENAME tokens represent symbols in the input,
161 and both convey their data as strings.
162 But a TYPENAME is a string that happens to be defined as a typedef
163 or builtin type name (such as int or char)
164 and a NAME is any other symbol.
165 Contexts where this distinction is not important can use the
166 nonterminal "name", which matches either NAME or TYPENAME. */
168 %token <tsval> STRING
169 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
170 %token SELECTOR /* ObjC "@selector" pseudo-operator */
172 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
173 %token <ssym> UNKNOWN_CPP_NAME
174 %token <voidval> COMPLETE
175 %token <tsym> TYPENAME
176 %token <theclass> CLASSNAME /* ObjC Class name */
177 %type <sval> name field_name
178 %type <svec> string_exp
179 %type <ssym> name_not_typename
180 %type <tsym> type_name
182 /* This is like a '[' token, but is only generated when parsing
183 Objective C. This lets us reuse the same parser without
184 erroneously parsing ObjC-specific expressions in C. */
187 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
188 but which would parse as a valid number in the current input radix.
189 E.g. "c" when input_radix==16. Depending on the parse, it will be
190 turned into a name or into a number. */
192 %token <ssym> NAME_OR_INT
195 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
200 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
206 /* Special type cases, put in to allow the parser to distinguish different
208 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
210 %token <sval> VARIABLE
212 %token <opcode> ASSIGN_MODIFY
221 %right '=' ASSIGN_MODIFY
229 %left '<' '>' LEQ GEQ
234 %right UNARY INCREMENT DECREMENT
235 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
236 %token <ssym> BLOCKNAME
237 %token <bval> FILENAME
251 { write_exp_elt_opcode(pstate, OP_TYPE);
252 write_exp_elt_type(pstate, $1);
253 write_exp_elt_opcode(pstate, OP_TYPE);}
256 write_exp_elt_opcode (pstate, OP_TYPEOF);
258 | TYPEOF '(' type ')'
260 write_exp_elt_opcode (pstate, OP_TYPE);
261 write_exp_elt_type (pstate, $3);
262 write_exp_elt_opcode (pstate, OP_TYPE);
264 | DECLTYPE '(' exp ')'
266 write_exp_elt_opcode (pstate, OP_DECLTYPE);
270 /* Expressions, including the comma operator. */
273 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
276 /* Expressions, not including the comma operator. */
277 exp : '*' exp %prec UNARY
278 { write_exp_elt_opcode (pstate, UNOP_IND); }
281 exp : '&' exp %prec UNARY
282 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
285 exp : '-' exp %prec UNARY
286 { write_exp_elt_opcode (pstate, UNOP_NEG); }
289 exp : '+' exp %prec UNARY
290 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
293 exp : '!' exp %prec UNARY
294 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
297 exp : '~' exp %prec UNARY
298 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
301 exp : INCREMENT exp %prec UNARY
302 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
305 exp : DECREMENT exp %prec UNARY
306 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
309 exp : exp INCREMENT %prec UNARY
310 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
313 exp : exp DECREMENT %prec UNARY
314 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
317 exp : TYPEID '(' exp ')' %prec UNARY
318 { write_exp_elt_opcode (pstate, OP_TYPEID); }
321 exp : TYPEID '(' type_exp ')' %prec UNARY
322 { write_exp_elt_opcode (pstate, OP_TYPEID); }
325 exp : SIZEOF exp %prec UNARY
326 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
329 exp : ALIGNOF '(' type_exp ')' %prec UNARY
330 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
333 exp : exp ARROW field_name
334 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
335 write_exp_string (pstate, $3);
336 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
339 exp : exp ARROW field_name COMPLETE
340 { mark_struct_expression (pstate);
341 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
342 write_exp_string (pstate, $3);
343 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
346 exp : exp ARROW COMPLETE
348 mark_struct_expression (pstate);
349 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
352 write_exp_string (pstate, s);
353 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
356 exp : exp ARROW '~' name
357 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
358 write_destructor_name (pstate, $4);
359 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
362 exp : exp ARROW '~' name COMPLETE
363 { mark_struct_expression (pstate);
364 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
365 write_destructor_name (pstate, $4);
366 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
369 exp : exp ARROW qualified_name
370 { /* exp->type::name becomes exp->*(&type::name) */
371 /* Note: this doesn't work if name is a
372 static member! FIXME */
373 write_exp_elt_opcode (pstate, UNOP_ADDR);
374 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
377 exp : exp ARROW_STAR exp
378 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
381 exp : exp '.' field_name
382 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
383 write_exp_string (pstate, $3);
384 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
387 exp : exp '.' field_name COMPLETE
388 { mark_struct_expression (pstate);
389 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
390 write_exp_string (pstate, $3);
391 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
394 exp : exp '.' COMPLETE
396 mark_struct_expression (pstate);
397 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
400 write_exp_string (pstate, s);
401 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
404 exp : exp '.' '~' name
405 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
406 write_destructor_name (pstate, $4);
407 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
410 exp : exp '.' '~' name COMPLETE
411 { mark_struct_expression (pstate);
412 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
413 write_destructor_name (pstate, $4);
414 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
417 exp : exp '.' qualified_name
418 { /* exp.type::name becomes exp.*(&type::name) */
419 /* Note: this doesn't work if name is a
420 static member! FIXME */
421 write_exp_elt_opcode (pstate, UNOP_ADDR);
422 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
425 exp : exp DOT_STAR exp
426 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
429 exp : exp '[' exp1 ']'
430 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
433 exp : exp OBJC_LBRAC exp1 ']'
434 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
438 * The rules below parse ObjC message calls of the form:
439 * '[' target selector {':' argument}* ']'
442 exp : OBJC_LBRAC TYPENAME
446 theclass = lookup_objc_class (parse_gdbarch (pstate),
447 copy_name ($2.stoken));
449 error (_("%s is not an ObjC Class"),
450 copy_name ($2.stoken));
451 write_exp_elt_opcode (pstate, OP_LONG);
452 write_exp_elt_type (pstate,
453 parse_type (pstate)->builtin_int);
454 write_exp_elt_longcst (pstate, (LONGEST) theclass);
455 write_exp_elt_opcode (pstate, OP_LONG);
459 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
460 end_msglist (pstate);
461 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
465 exp : OBJC_LBRAC CLASSNAME
467 write_exp_elt_opcode (pstate, OP_LONG);
468 write_exp_elt_type (pstate,
469 parse_type (pstate)->builtin_int);
470 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
471 write_exp_elt_opcode (pstate, OP_LONG);
475 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
476 end_msglist (pstate);
477 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
484 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
485 end_msglist (pstate);
486 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
491 { add_msglist(&$1, 0); }
499 msgarg : name ':' exp
500 { add_msglist(&$1, 1); }
501 | ':' exp /* Unnamed arg. */
502 { add_msglist(0, 1); }
503 | ',' exp /* Variable number of args. */
504 { add_msglist(0, 0); }
508 /* This is to save the value of arglist_len
509 being accumulated by an outer function call. */
510 { start_arglist (); }
511 arglist ')' %prec ARROW
512 { write_exp_elt_opcode (pstate, OP_FUNCALL);
513 write_exp_elt_longcst (pstate,
514 (LONGEST) end_arglist ());
515 write_exp_elt_opcode (pstate, OP_FUNCALL); }
518 /* This is here to disambiguate with the production for
519 "func()::static_var" further below, which uses
520 function_method_void. */
521 exp : exp '(' ')' %prec ARROW
523 write_exp_elt_opcode (pstate, OP_FUNCALL);
524 write_exp_elt_longcst (pstate,
525 (LONGEST) end_arglist ());
526 write_exp_elt_opcode (pstate, OP_FUNCALL); }
530 exp : UNKNOWN_CPP_NAME '('
532 /* This could potentially be a an argument defined
533 lookup function (Koenig). */
534 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
535 write_exp_elt_block (pstate,
536 expression_context_block);
537 write_exp_elt_sym (pstate,
538 NULL); /* Placeholder. */
539 write_exp_string (pstate, $1.stoken);
540 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
542 /* This is to save the value of arglist_len
543 being accumulated by an outer function call. */
547 arglist ')' %prec ARROW
549 write_exp_elt_opcode (pstate, OP_FUNCALL);
550 write_exp_elt_longcst (pstate,
551 (LONGEST) end_arglist ());
552 write_exp_elt_opcode (pstate, OP_FUNCALL);
557 { start_arglist (); }
567 arglist : arglist ',' exp %prec ABOVE_COMMA
571 function_method: exp '(' parameter_typelist ')' const_or_volatile
573 std::vector<struct type *> *type_list = $3;
574 LONGEST len = type_list->size ();
576 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
577 /* Save the const/volatile qualifiers as
578 recorded by the const_or_volatile
579 production's actions. */
580 write_exp_elt_longcst (pstate,
581 follow_type_instance_flags ());
582 write_exp_elt_longcst (pstate, len);
583 for (type *type_elt : *type_list)
584 write_exp_elt_type (pstate, type_elt);
585 write_exp_elt_longcst(pstate, len);
586 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
590 function_method_void: exp '(' ')' const_or_volatile
591 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
593 write_exp_elt_longcst (pstate,
594 follow_type_instance_flags ());
595 write_exp_elt_longcst (pstate, 0);
596 write_exp_elt_longcst (pstate, 0);
597 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
601 exp : function_method
604 /* Normally we must interpret "func()" as a function call, instead of
605 a type. The user needs to write func(void) to disambiguate.
606 However, in the "func()::static_var" case, there's no
608 function_method_void_or_typelist: function_method
609 | function_method_void
612 exp : function_method_void_or_typelist COLONCOLON name
614 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
615 write_exp_string (pstate, $3);
616 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
621 { $$ = end_arglist () - 1; }
623 exp : lcurly arglist rcurly %prec ARROW
624 { write_exp_elt_opcode (pstate, OP_ARRAY);
625 write_exp_elt_longcst (pstate, (LONGEST) 0);
626 write_exp_elt_longcst (pstate, (LONGEST) $3);
627 write_exp_elt_opcode (pstate, OP_ARRAY); }
630 exp : lcurly type_exp rcurly exp %prec UNARY
631 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
634 exp : '(' type_exp ')' exp %prec UNARY
635 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
642 /* Binary operators in order of decreasing precedence. */
645 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
649 { write_exp_elt_opcode (pstate, BINOP_MUL); }
653 { write_exp_elt_opcode (pstate, BINOP_DIV); }
657 { write_exp_elt_opcode (pstate, BINOP_REM); }
661 { write_exp_elt_opcode (pstate, BINOP_ADD); }
665 { write_exp_elt_opcode (pstate, BINOP_SUB); }
669 { write_exp_elt_opcode (pstate, BINOP_LSH); }
673 { write_exp_elt_opcode (pstate, BINOP_RSH); }
677 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
680 exp : exp NOTEQUAL exp
681 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
685 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
689 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
693 { write_exp_elt_opcode (pstate, BINOP_LESS); }
697 { write_exp_elt_opcode (pstate, BINOP_GTR); }
701 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
705 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
709 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
713 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
717 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
720 exp : exp '?' exp ':' exp %prec '?'
721 { write_exp_elt_opcode (pstate, TERNOP_COND); }
725 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
728 exp : exp ASSIGN_MODIFY exp
729 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
730 write_exp_elt_opcode (pstate, $2);
731 write_exp_elt_opcode (pstate,
732 BINOP_ASSIGN_MODIFY); }
736 { write_exp_elt_opcode (pstate, OP_LONG);
737 write_exp_elt_type (pstate, $1.type);
738 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
739 write_exp_elt_opcode (pstate, OP_LONG); }
744 struct stoken_vector vec;
747 write_exp_string_vector (pstate, $1.type, &vec);
753 parse_number (pstate, $1.stoken.ptr,
754 $1.stoken.length, 0, &val);
755 write_exp_elt_opcode (pstate, OP_LONG);
756 write_exp_elt_type (pstate, val.typed_val_int.type);
757 write_exp_elt_longcst (pstate,
758 (LONGEST) val.typed_val_int.val);
759 write_exp_elt_opcode (pstate, OP_LONG);
765 { write_exp_elt_opcode (pstate, OP_FLOAT);
766 write_exp_elt_type (pstate, $1.type);
767 write_exp_elt_floatcst (pstate, $1.val);
768 write_exp_elt_opcode (pstate, OP_FLOAT); }
776 write_dollar_variable (pstate, $1);
780 exp : SELECTOR '(' name ')'
782 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
783 write_exp_string (pstate, $3);
784 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
787 exp : SIZEOF '(' type ')' %prec UNARY
788 { struct type *type = $3;
789 write_exp_elt_opcode (pstate, OP_LONG);
790 write_exp_elt_type (pstate, lookup_signed_typename
791 (parse_language (pstate),
792 parse_gdbarch (pstate),
794 type = check_typedef (type);
796 /* $5.3.3/2 of the C++ Standard (n3290 draft)
797 says of sizeof: "When applied to a reference
798 or a reference type, the result is the size of
799 the referenced type." */
800 if (TYPE_IS_REFERENCE (type))
801 type = check_typedef (TYPE_TARGET_TYPE (type));
802 write_exp_elt_longcst (pstate,
803 (LONGEST) TYPE_LENGTH (type));
804 write_exp_elt_opcode (pstate, OP_LONG); }
807 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
808 { write_exp_elt_opcode (pstate,
809 UNOP_REINTERPRET_CAST); }
812 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
813 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
816 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
817 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
820 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
821 { /* We could do more error checking here, but
822 it doesn't seem worthwhile. */
823 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
829 /* We copy the string here, and not in the
830 lexer, to guarantee that we do not leak a
831 string. Note that we follow the
832 NUL-termination convention of the
834 struct typed_stoken *vec = XNEW (struct typed_stoken);
839 vec->length = $1.length;
840 vec->ptr = (char *) malloc ($1.length + 1);
841 memcpy (vec->ptr, $1.ptr, $1.length + 1);
846 /* Note that we NUL-terminate here, but just
850 $$.tokens = XRESIZEVEC (struct typed_stoken,
853 p = (char *) malloc ($2.length + 1);
854 memcpy (p, $2.ptr, $2.length + 1);
856 $$.tokens[$$.len - 1].type = $2.type;
857 $$.tokens[$$.len - 1].length = $2.length;
858 $$.tokens[$$.len - 1].ptr = p;
865 c_string_type type = C_STRING;
867 for (i = 0; i < $1.len; ++i)
869 switch ($1.tokens[i].type)
877 && type != $1.tokens[i].type)
878 error (_("Undefined string concatenation."));
879 type = (enum c_string_type_values) $1.tokens[i].type;
883 internal_error (__FILE__, __LINE__,
884 "unrecognized type in string concatenation");
888 write_exp_string_vector (pstate, type, &$1);
889 for (i = 0; i < $1.len; ++i)
890 free ($1.tokens[i].ptr);
895 exp : NSSTRING /* ObjC NextStep NSString constant
896 * of the form '@' '"' string '"'.
898 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
899 write_exp_string (pstate, $1);
900 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
905 { write_exp_elt_opcode (pstate, OP_LONG);
906 write_exp_elt_type (pstate,
907 parse_type (pstate)->builtin_bool);
908 write_exp_elt_longcst (pstate, (LONGEST) 1);
909 write_exp_elt_opcode (pstate, OP_LONG); }
913 { write_exp_elt_opcode (pstate, OP_LONG);
914 write_exp_elt_type (pstate,
915 parse_type (pstate)->builtin_bool);
916 write_exp_elt_longcst (pstate, (LONGEST) 0);
917 write_exp_elt_opcode (pstate, OP_LONG); }
925 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
927 error (_("No file or function \"%s\"."),
928 copy_name ($1.stoken));
936 block : block COLONCOLON name
938 = lookup_symbol (copy_name ($3), $1,
939 VAR_DOMAIN, NULL).symbol;
941 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
942 error (_("No function \"%s\" in specified context."),
944 $$ = SYMBOL_BLOCK_VALUE (tem); }
947 variable: name_not_typename ENTRY
948 { struct symbol *sym = $1.sym.symbol;
950 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
951 || !symbol_read_needs_frame (sym))
952 error (_("@entry can be used only for function "
953 "parameters, not for \"%s\""),
954 copy_name ($1.stoken));
956 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
957 write_exp_elt_sym (pstate, sym);
958 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
962 variable: block COLONCOLON name
963 { struct block_symbol sym
964 = lookup_symbol (copy_name ($3), $1,
968 error (_("No symbol \"%s\" in specified context."),
970 if (symbol_read_needs_frame (sym.symbol))
972 innermost_block.update (sym);
974 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
975 write_exp_elt_block (pstate, sym.block);
976 write_exp_elt_sym (pstate, sym.symbol);
977 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
980 qualified_name: TYPENAME COLONCOLON name
982 struct type *type = $1.type;
983 type = check_typedef (type);
984 if (!type_aggregate_p (type))
985 error (_("`%s' is not defined as an aggregate type."),
986 TYPE_SAFE_NAME (type));
988 write_exp_elt_opcode (pstate, OP_SCOPE);
989 write_exp_elt_type (pstate, type);
990 write_exp_string (pstate, $3);
991 write_exp_elt_opcode (pstate, OP_SCOPE);
993 | TYPENAME COLONCOLON '~' name
995 struct type *type = $1.type;
996 struct stoken tmp_token;
999 type = check_typedef (type);
1000 if (!type_aggregate_p (type))
1001 error (_("`%s' is not defined as an aggregate type."),
1002 TYPE_SAFE_NAME (type));
1003 buf = (char *) alloca ($4.length + 2);
1004 tmp_token.ptr = buf;
1005 tmp_token.length = $4.length + 1;
1007 memcpy (buf+1, $4.ptr, $4.length);
1008 buf[tmp_token.length] = 0;
1010 /* Check for valid destructor name. */
1011 destructor_name_p (tmp_token.ptr, $1.type);
1012 write_exp_elt_opcode (pstate, OP_SCOPE);
1013 write_exp_elt_type (pstate, type);
1014 write_exp_string (pstate, tmp_token);
1015 write_exp_elt_opcode (pstate, OP_SCOPE);
1017 | TYPENAME COLONCOLON name COLONCOLON name
1019 char *copy = copy_name ($3);
1020 error (_("No type \"%s\" within class "
1021 "or namespace \"%s\"."),
1022 copy, TYPE_SAFE_NAME ($1.type));
1026 variable: qualified_name
1027 | COLONCOLON name_not_typename
1029 char *name = copy_name ($2.stoken);
1031 struct bound_minimal_symbol msymbol;
1034 = lookup_symbol (name, (const struct block *) NULL,
1035 VAR_DOMAIN, NULL).symbol;
1038 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1039 write_exp_elt_block (pstate, NULL);
1040 write_exp_elt_sym (pstate, sym);
1041 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1045 msymbol = lookup_bound_minimal_symbol (name);
1046 if (msymbol.minsym != NULL)
1047 write_exp_msymbol (pstate, msymbol);
1048 else if (!have_full_symbols () && !have_partial_symbols ())
1049 error (_("No symbol table is loaded. Use the \"file\" command."));
1051 error (_("No symbol \"%s\" in current context."), name);
1055 variable: name_not_typename
1056 { struct block_symbol sym = $1.sym;
1060 if (symbol_read_needs_frame (sym.symbol))
1061 innermost_block.update (sym);
1063 /* If we found a function, see if it's
1064 an ifunc resolver that has the same
1065 address as the ifunc symbol itself.
1066 If so, prefer the ifunc symbol. */
1068 bound_minimal_symbol resolver
1069 = find_gnu_ifunc (sym.symbol);
1070 if (resolver.minsym != NULL)
1071 write_exp_msymbol (pstate, resolver);
1074 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1075 write_exp_elt_block (pstate, sym.block);
1076 write_exp_elt_sym (pstate, sym.symbol);
1077 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1080 else if ($1.is_a_field_of_this)
1082 /* C++: it hangs off of `this'. Must
1083 not inadvertently convert from a method call
1085 innermost_block.update (sym);
1086 write_exp_elt_opcode (pstate, OP_THIS);
1087 write_exp_elt_opcode (pstate, OP_THIS);
1088 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1089 write_exp_string (pstate, $1.stoken);
1090 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1094 char *arg = copy_name ($1.stoken);
1096 bound_minimal_symbol msymbol
1097 = lookup_bound_minimal_symbol (arg);
1098 if (msymbol.minsym == NULL)
1100 if (!have_full_symbols () && !have_partial_symbols ())
1101 error (_("No symbol table is loaded. Use the \"file\" command."));
1103 error (_("No symbol \"%s\" in current context."),
1104 copy_name ($1.stoken));
1107 /* This minsym might be an alias for
1108 another function. See if we can find
1109 the debug symbol for the target, and
1110 if so, use it instead, since it has
1111 return type / prototype info. This
1112 is important for example for "p
1113 *__errno_location()". */
1114 symbol *alias_target
1115 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1116 && msymbol.minsym->type != mst_data_gnu_ifunc)
1117 ? find_function_alias_target (msymbol)
1119 if (alias_target != NULL)
1121 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1123 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1124 write_exp_elt_sym (pstate, alias_target);
1125 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1128 write_exp_msymbol (pstate, msymbol);
1133 space_identifier : '@' NAME
1134 { insert_type_address_space (pstate, copy_name ($2.stoken)); }
1137 const_or_volatile: const_or_volatile_noopt
1141 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1144 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1145 | const_or_volatile_noopt
1148 const_or_volatile_or_space_identifier:
1149 const_or_volatile_or_space_identifier_noopt
1155 { insert_type (tp_pointer); }
1156 const_or_volatile_or_space_identifier
1158 { insert_type (tp_pointer); }
1159 const_or_volatile_or_space_identifier
1161 { insert_type (tp_reference); }
1163 { insert_type (tp_reference); }
1165 { insert_type (tp_rvalue_reference); }
1166 | ANDAND ptr_operator
1167 { insert_type (tp_rvalue_reference); }
1170 ptr_operator_ts: ptr_operator
1172 $$ = get_type_stack ();
1173 cpstate->type_stacks.emplace_back ($$);
1177 abs_decl: ptr_operator_ts direct_abs_decl
1178 { $$ = append_type_stack ($2, $1); }
1183 direct_abs_decl: '(' abs_decl ')'
1185 | direct_abs_decl array_mod
1187 push_type_stack ($1);
1189 push_type (tp_array);
1190 $$ = get_type_stack ();
1195 push_type (tp_array);
1196 $$ = get_type_stack ();
1199 | direct_abs_decl func_mod
1201 push_type_stack ($1);
1203 $$ = get_type_stack ();
1208 $$ = get_type_stack ();
1218 | OBJC_LBRAC INT ']'
1224 $$ = new std::vector<struct type *>;
1225 cpstate->type_lists.emplace_back ($$);
1227 | '(' parameter_typelist ')'
1231 /* We used to try to recognize pointer to member types here, but
1232 that didn't work (shift/reduce conflicts meant that these rules never
1233 got executed). The problem is that
1234 int (foo::bar::baz::bizzle)
1235 is a function type but
1236 int (foo::bar::baz::bizzle::*)
1237 is a pointer to member type. Stroustrup loses again! */
1242 /* Implements (approximately): (type-qualifier)* type-specifier.
1244 When type-specifier is only ever a single word, like 'float' then these
1245 arrive as pre-built TYPENAME tokens thanks to the classify_name
1246 function. However, when a type-specifier can contain multiple words,
1247 for example 'double' can appear as just 'double' or 'long double', and
1248 similarly 'long' can appear as just 'long' or in 'long double', then
1249 these type-specifiers are parsed into their own tokens in the function
1250 lex_one_token and the ident_tokens array. These separate tokens are all
1256 { $$ = lookup_signed_typename (parse_language (pstate),
1257 parse_gdbarch (pstate),
1260 { $$ = lookup_signed_typename (parse_language (pstate),
1261 parse_gdbarch (pstate),
1264 { $$ = lookup_signed_typename (parse_language (pstate),
1265 parse_gdbarch (pstate),
1268 { $$ = lookup_signed_typename (parse_language (pstate),
1269 parse_gdbarch (pstate),
1271 | LONG SIGNED_KEYWORD INT_KEYWORD
1272 { $$ = lookup_signed_typename (parse_language (pstate),
1273 parse_gdbarch (pstate),
1275 | LONG SIGNED_KEYWORD
1276 { $$ = lookup_signed_typename (parse_language (pstate),
1277 parse_gdbarch (pstate),
1279 | SIGNED_KEYWORD LONG INT_KEYWORD
1280 { $$ = lookup_signed_typename (parse_language (pstate),
1281 parse_gdbarch (pstate),
1283 | UNSIGNED LONG INT_KEYWORD
1284 { $$ = lookup_unsigned_typename (parse_language (pstate),
1285 parse_gdbarch (pstate),
1287 | LONG UNSIGNED INT_KEYWORD
1288 { $$ = lookup_unsigned_typename (parse_language (pstate),
1289 parse_gdbarch (pstate),
1292 { $$ = lookup_unsigned_typename (parse_language (pstate),
1293 parse_gdbarch (pstate),
1296 { $$ = lookup_signed_typename (parse_language (pstate),
1297 parse_gdbarch (pstate),
1299 | LONG LONG INT_KEYWORD
1300 { $$ = lookup_signed_typename (parse_language (pstate),
1301 parse_gdbarch (pstate),
1303 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1304 { $$ = lookup_signed_typename (parse_language (pstate),
1305 parse_gdbarch (pstate),
1307 | LONG LONG SIGNED_KEYWORD
1308 { $$ = lookup_signed_typename (parse_language (pstate),
1309 parse_gdbarch (pstate),
1311 | SIGNED_KEYWORD LONG LONG
1312 { $$ = lookup_signed_typename (parse_language (pstate),
1313 parse_gdbarch (pstate),
1315 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1316 { $$ = lookup_signed_typename (parse_language (pstate),
1317 parse_gdbarch (pstate),
1319 | UNSIGNED LONG LONG
1320 { $$ = lookup_unsigned_typename (parse_language (pstate),
1321 parse_gdbarch (pstate),
1323 | UNSIGNED LONG LONG INT_KEYWORD
1324 { $$ = lookup_unsigned_typename (parse_language (pstate),
1325 parse_gdbarch (pstate),
1327 | LONG LONG UNSIGNED
1328 { $$ = lookup_unsigned_typename (parse_language (pstate),
1329 parse_gdbarch (pstate),
1331 | LONG LONG UNSIGNED INT_KEYWORD
1332 { $$ = lookup_unsigned_typename (parse_language (pstate),
1333 parse_gdbarch (pstate),
1336 { $$ = lookup_signed_typename (parse_language (pstate),
1337 parse_gdbarch (pstate),
1339 | SHORT SIGNED_KEYWORD INT_KEYWORD
1340 { $$ = lookup_signed_typename (parse_language (pstate),
1341 parse_gdbarch (pstate),
1343 | SHORT SIGNED_KEYWORD
1344 { $$ = lookup_signed_typename (parse_language (pstate),
1345 parse_gdbarch (pstate),
1347 | UNSIGNED SHORT INT_KEYWORD
1348 { $$ = lookup_unsigned_typename (parse_language (pstate),
1349 parse_gdbarch (pstate),
1352 { $$ = lookup_unsigned_typename (parse_language (pstate),
1353 parse_gdbarch (pstate),
1355 | SHORT UNSIGNED INT_KEYWORD
1356 { $$ = lookup_unsigned_typename (parse_language (pstate),
1357 parse_gdbarch (pstate),
1360 { $$ = lookup_typename (parse_language (pstate),
1361 parse_gdbarch (pstate),
1363 (struct block *) NULL,
1365 | LONG DOUBLE_KEYWORD
1366 { $$ = lookup_typename (parse_language (pstate),
1367 parse_gdbarch (pstate),
1369 (struct block *) NULL,
1372 { $$ = lookup_struct (copy_name ($2),
1373 expression_context_block); }
1376 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1379 | STRUCT name COMPLETE
1381 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1386 { $$ = lookup_struct (copy_name ($2),
1387 expression_context_block); }
1390 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1393 | CLASS name COMPLETE
1395 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1400 { $$ = lookup_union (copy_name ($2),
1401 expression_context_block); }
1404 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1407 | UNION name COMPLETE
1409 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1414 { $$ = lookup_enum (copy_name ($2),
1415 expression_context_block); }
1418 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1421 | ENUM name COMPLETE
1423 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1427 | UNSIGNED type_name
1428 { $$ = lookup_unsigned_typename (parse_language (pstate),
1429 parse_gdbarch (pstate),
1430 TYPE_NAME($2.type)); }
1432 { $$ = lookup_unsigned_typename (parse_language (pstate),
1433 parse_gdbarch (pstate),
1435 | SIGNED_KEYWORD type_name
1436 { $$ = lookup_signed_typename (parse_language (pstate),
1437 parse_gdbarch (pstate),
1438 TYPE_NAME($2.type)); }
1440 { $$ = lookup_signed_typename (parse_language (pstate),
1441 parse_gdbarch (pstate),
1443 /* It appears that this rule for templates is never
1444 reduced; template recognition happens by lookahead
1445 in the token processing code in yylex. */
1446 | TEMPLATE name '<' type '>'
1447 { $$ = lookup_template_type(copy_name($2), $4,
1448 expression_context_block);
1450 | const_or_volatile_or_space_identifier_noopt typebase
1451 { $$ = follow_types ($2); }
1452 | typebase const_or_volatile_or_space_identifier_noopt
1453 { $$ = follow_types ($1); }
1459 $$.stoken.ptr = "int";
1460 $$.stoken.length = 3;
1461 $$.type = lookup_signed_typename (parse_language (pstate),
1462 parse_gdbarch (pstate),
1467 $$.stoken.ptr = "long";
1468 $$.stoken.length = 4;
1469 $$.type = lookup_signed_typename (parse_language (pstate),
1470 parse_gdbarch (pstate),
1475 $$.stoken.ptr = "short";
1476 $$.stoken.length = 5;
1477 $$.type = lookup_signed_typename (parse_language (pstate),
1478 parse_gdbarch (pstate),
1485 { check_parameter_typelist ($1); }
1486 | nonempty_typelist ',' DOTDOTDOT
1488 $1->push_back (NULL);
1489 check_parameter_typelist ($1);
1497 std::vector<struct type *> *typelist
1498 = new std::vector<struct type *>;
1499 cpstate->type_lists.emplace_back (typelist);
1501 typelist->push_back ($1);
1504 | nonempty_typelist ',' type
1514 push_type_stack ($2);
1515 $$ = follow_types ($1);
1519 conversion_type_id: typebase conversion_declarator
1520 { $$ = follow_types ($1); }
1523 conversion_declarator: /* Nothing. */
1524 | ptr_operator conversion_declarator
1527 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1528 | VOLATILE_KEYWORD CONST_KEYWORD
1531 const_or_volatile_noopt: const_and_volatile
1532 { insert_type (tp_const);
1533 insert_type (tp_volatile);
1536 { insert_type (tp_const); }
1538 { insert_type (tp_volatile); }
1542 { $$ = operator_stoken (" new"); }
1544 { $$ = operator_stoken (" delete"); }
1545 | OPERATOR NEW '[' ']'
1546 { $$ = operator_stoken (" new[]"); }
1547 | OPERATOR DELETE '[' ']'
1548 { $$ = operator_stoken (" delete[]"); }
1549 | OPERATOR NEW OBJC_LBRAC ']'
1550 { $$ = operator_stoken (" new[]"); }
1551 | OPERATOR DELETE OBJC_LBRAC ']'
1552 { $$ = operator_stoken (" delete[]"); }
1554 { $$ = operator_stoken ("+"); }
1556 { $$ = operator_stoken ("-"); }
1558 { $$ = operator_stoken ("*"); }
1560 { $$ = operator_stoken ("/"); }
1562 { $$ = operator_stoken ("%"); }
1564 { $$ = operator_stoken ("^"); }
1566 { $$ = operator_stoken ("&"); }
1568 { $$ = operator_stoken ("|"); }
1570 { $$ = operator_stoken ("~"); }
1572 { $$ = operator_stoken ("!"); }
1574 { $$ = operator_stoken ("="); }
1576 { $$ = operator_stoken ("<"); }
1578 { $$ = operator_stoken (">"); }
1579 | OPERATOR ASSIGN_MODIFY
1580 { const char *op = " unknown";
1604 case BINOP_BITWISE_IOR:
1607 case BINOP_BITWISE_AND:
1610 case BINOP_BITWISE_XOR:
1617 $$ = operator_stoken (op);
1620 { $$ = operator_stoken ("<<"); }
1622 { $$ = operator_stoken (">>"); }
1624 { $$ = operator_stoken ("=="); }
1626 { $$ = operator_stoken ("!="); }
1628 { $$ = operator_stoken ("<="); }
1630 { $$ = operator_stoken (">="); }
1632 { $$ = operator_stoken ("&&"); }
1634 { $$ = operator_stoken ("||"); }
1635 | OPERATOR INCREMENT
1636 { $$ = operator_stoken ("++"); }
1637 | OPERATOR DECREMENT
1638 { $$ = operator_stoken ("--"); }
1640 { $$ = operator_stoken (","); }
1641 | OPERATOR ARROW_STAR
1642 { $$ = operator_stoken ("->*"); }
1644 { $$ = operator_stoken ("->"); }
1646 { $$ = operator_stoken ("()"); }
1648 { $$ = operator_stoken ("[]"); }
1649 | OPERATOR OBJC_LBRAC ']'
1650 { $$ = operator_stoken ("[]"); }
1651 | OPERATOR conversion_type_id
1654 c_print_type ($2, NULL, &buf, -1, 0,
1655 &type_print_raw_options);
1657 /* This also needs canonicalization. */
1659 = cp_canonicalize_string (buf.c_str ());
1661 canon = std::move (buf.string ());
1662 $$ = operator_stoken ((" " + canon).c_str ());
1666 /* This rule exists in order to allow some tokens that would not normally
1667 match the 'name' rule to appear as fields within a struct. The example
1668 that initially motivated this was the RISC-V target which models the
1669 floating point registers as a union with fields called 'float' and
1670 'double'. The 'float' string becomes a TYPENAME token and can appear
1671 anywhere a 'name' can, however 'double' is its own token,
1672 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1675 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1676 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1677 | LONG { $$ = typename_stoken ("long"); }
1678 | SHORT { $$ = typename_stoken ("short"); }
1679 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1680 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1683 name : NAME { $$ = $1.stoken; }
1684 | BLOCKNAME { $$ = $1.stoken; }
1685 | TYPENAME { $$ = $1.stoken; }
1686 | NAME_OR_INT { $$ = $1.stoken; }
1687 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1691 name_not_typename : NAME
1693 /* These would be useful if name_not_typename was useful, but it is just
1694 a fake for "variable", so these cause reduce/reduce conflicts because
1695 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1696 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1697 context where only a name could occur, this might be useful.
1702 struct field_of_this_result is_a_field_of_this;
1705 $$.sym = lookup_symbol ($1.ptr,
1706 expression_context_block,
1708 &is_a_field_of_this);
1709 $$.is_a_field_of_this
1710 = is_a_field_of_this.type != NULL;
1717 /* Like write_exp_string, but prepends a '~'. */
1720 write_destructor_name (struct parser_state *par_state, struct stoken token)
1722 char *copy = (char *) alloca (token.length + 1);
1725 memcpy (©[1], token.ptr, token.length);
1730 write_exp_string (par_state, token);
1733 /* Returns a stoken of the operator name given by OP (which does not
1734 include the string "operator"). */
1736 static struct stoken
1737 operator_stoken (const char *op)
1739 struct stoken st = { NULL, 0 };
1742 st.length = CP_OPERATOR_LEN + strlen (op);
1743 buf = (char *) malloc (st.length + 1);
1744 strcpy (buf, CP_OPERATOR_STR);
1748 /* The toplevel (c_parse) will free the memory allocated here. */
1749 cpstate->strings.emplace_back (buf);
1753 /* Returns a stoken of the type named TYPE. */
1755 static struct stoken
1756 typename_stoken (const char *type)
1758 struct stoken st = { type, 0 };
1759 st.length = strlen (type);
1763 /* Return true if the type is aggregate-like. */
1766 type_aggregate_p (struct type *type)
1768 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1769 || TYPE_CODE (type) == TYPE_CODE_UNION
1770 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1771 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1772 && TYPE_DECLARED_CLASS (type)));
1775 /* Validate a parameter typelist. */
1778 check_parameter_typelist (std::vector<struct type *> *params)
1783 for (ix = 0; ix < params->size (); ++ix)
1785 type = (*params)[ix];
1786 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1790 if (params->size () == 1)
1795 error (_("parameter types following 'void'"));
1798 error (_("'void' invalid as parameter type"));
1803 /* Take care of parsing a number (anything that starts with a digit).
1804 Set yylval and return the token type; update lexptr.
1805 LEN is the number of characters in it. */
1807 /*** Needs some error checking for the float case ***/
1810 parse_number (struct parser_state *par_state,
1811 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1819 int base = input_radix;
1822 /* Number of "L" suffixes encountered. */
1825 /* We have found a "L" or "U" suffix. */
1826 int found_suffix = 0;
1829 struct type *signed_type;
1830 struct type *unsigned_type;
1833 p = (char *) alloca (len);
1834 memcpy (p, buf, len);
1838 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1839 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1841 putithere->typed_val_float.type
1842 = parse_type (par_state)->builtin_decfloat;
1845 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1847 putithere->typed_val_float.type
1848 = parse_type (par_state)->builtin_decdouble;
1851 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1853 putithere->typed_val_float.type
1854 = parse_type (par_state)->builtin_declong;
1857 /* Handle suffixes: 'f' for float, 'l' for long double. */
1858 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1860 putithere->typed_val_float.type
1861 = parse_type (par_state)->builtin_float;
1864 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1866 putithere->typed_val_float.type
1867 = parse_type (par_state)->builtin_long_double;
1870 /* Default type for floating-point literals is double. */
1873 putithere->typed_val_float.type
1874 = parse_type (par_state)->builtin_double;
1877 if (!parse_float (p, len,
1878 putithere->typed_val_float.type,
1879 putithere->typed_val_float.val))
1884 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1885 if (p[0] == '0' && len > 1)
1928 if (c >= 'A' && c <= 'Z')
1930 if (c != 'l' && c != 'u')
1932 if (c >= '0' && c <= '9')
1940 if (base > 10 && c >= 'a' && c <= 'f')
1944 n += i = c - 'a' + 10;
1957 return ERROR; /* Char not a digit */
1960 return ERROR; /* Invalid digit in this base */
1962 /* Portably test for overflow (only works for nonzero values, so make
1963 a second check for zero). FIXME: Can't we just make n and prevn
1964 unsigned and avoid this? */
1965 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1966 unsigned_p = 1; /* Try something unsigned */
1968 /* Portably test for unsigned overflow.
1969 FIXME: This check is wrong; for example it doesn't find overflow
1970 on 0x123456789 when LONGEST is 32 bits. */
1971 if (c != 'l' && c != 'u' && n != 0)
1973 if (unsigned_p && prevn >= n)
1974 error (_("Numeric constant too large."));
1979 /* An integer constant is an int, a long, or a long long. An L
1980 suffix forces it to be long; an LL suffix forces it to be long
1981 long. If not forced to a larger size, it gets the first type of
1982 the above that it fits in. To figure out whether it fits, we
1983 shift it right and see whether anything remains. Note that we
1984 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1985 operation, because many compilers will warn about such a shift
1986 (which always produces a zero result). Sometimes gdbarch_int_bit
1987 or gdbarch_long_bit will be that big, sometimes not. To deal with
1988 the case where it is we just always shift the value more than
1989 once, with fewer bits each time. */
1993 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
1996 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
1998 /* A large decimal (not hex or octal) constant (between INT_MAX
1999 and UINT_MAX) is a long or unsigned long, according to ANSI,
2000 never an unsigned int, but this code treats it as unsigned
2001 int. This probably should be fixed. GCC gives a warning on
2004 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2005 signed_type = parse_type (par_state)->builtin_int;
2007 else if (long_p <= 1
2008 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
2011 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
2012 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2013 signed_type = parse_type (par_state)->builtin_long;
2018 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2019 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
2020 /* A long long does not fit in a LONGEST. */
2021 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2023 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
2024 high_bit = (ULONGEST) 1 << shift;
2025 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2026 signed_type = parse_type (par_state)->builtin_long_long;
2029 putithere->typed_val_int.val = n;
2031 /* If the high bit of the worked out type is set then this number
2032 has to be unsigned. */
2034 if (unsigned_p || (n & high_bit))
2036 putithere->typed_val_int.type = unsigned_type;
2040 putithere->typed_val_int.type = signed_type;
2046 /* Temporary obstack used for holding strings. */
2047 static struct obstack tempbuf;
2048 static int tempbuf_init;
2050 /* Parse a C escape sequence. The initial backslash of the sequence
2051 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2052 last character of the sequence. If OUTPUT is not NULL, the
2053 translated form of the escape sequence will be written there. If
2054 OUTPUT is NULL, no output is written and the call will only affect
2055 *PTR. If an escape sequence is expressed in target bytes, then the
2056 entire sequence will simply be copied to OUTPUT. Return 1 if any
2057 character was emitted, 0 otherwise. */
2060 c_parse_escape (const char **ptr, struct obstack *output)
2062 const char *tokptr = *ptr;
2065 /* Some escape sequences undergo character set conversion. Those we
2069 /* Hex escapes do not undergo character set conversion, so keep
2070 the escape sequence for later. */
2073 obstack_grow_str (output, "\\x");
2075 if (!ISXDIGIT (*tokptr))
2076 error (_("\\x escape without a following hex digit"));
2077 while (ISXDIGIT (*tokptr))
2080 obstack_1grow (output, *tokptr);
2085 /* Octal escapes do not undergo character set conversion, so
2086 keep the escape sequence for later. */
2098 obstack_grow_str (output, "\\");
2100 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2104 obstack_1grow (output, *tokptr);
2110 /* We handle UCNs later. We could handle them here, but that
2111 would mean a spurious error in the case where the UCN could
2112 be converted to the target charset but not the host
2118 int i, len = c == 'U' ? 8 : 4;
2121 obstack_1grow (output, '\\');
2122 obstack_1grow (output, *tokptr);
2125 if (!ISXDIGIT (*tokptr))
2126 error (_("\\%c escape without a following hex digit"), c);
2127 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2130 obstack_1grow (output, *tokptr);
2136 /* We must pass backslash through so that it does not
2137 cause quoting during the second expansion. */
2140 obstack_grow_str (output, "\\\\");
2144 /* Escapes which undergo conversion. */
2147 obstack_1grow (output, '\a');
2152 obstack_1grow (output, '\b');
2157 obstack_1grow (output, '\f');
2162 obstack_1grow (output, '\n');
2167 obstack_1grow (output, '\r');
2172 obstack_1grow (output, '\t');
2177 obstack_1grow (output, '\v');
2181 /* GCC extension. */
2184 obstack_1grow (output, HOST_ESCAPE_CHAR);
2188 /* Backslash-newline expands to nothing at all. */
2194 /* A few escapes just expand to the character itself. */
2198 /* GCC extensions. */
2203 /* Unrecognized escapes turn into the character itself. */
2206 obstack_1grow (output, *tokptr);
2214 /* Parse a string or character literal from TOKPTR. The string or
2215 character may be wide or unicode. *OUTPTR is set to just after the
2216 end of the literal in the input string. The resulting token is
2217 stored in VALUE. This returns a token value, either STRING or
2218 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2219 number of host characters in the literal. */
2222 parse_string_or_char (const char *tokptr, const char **outptr,
2223 struct typed_stoken *value, int *host_chars)
2229 /* Build the gdb internal form of the input string in tempbuf. Note
2230 that the buffer is null byte terminated *only* for the
2231 convenience of debugging gdb itself and printing the buffer
2232 contents when the buffer contains no embedded nulls. Gdb does
2233 not depend upon the buffer being null byte terminated, it uses
2234 the length string instead. This allows gdb to handle C strings
2235 (as well as strings in other languages) with embedded null
2241 obstack_free (&tempbuf, NULL);
2242 obstack_init (&tempbuf);
2244 /* Record the string type. */
2247 type = C_WIDE_STRING;
2250 else if (*tokptr == 'u')
2255 else if (*tokptr == 'U')
2260 else if (*tokptr == '@')
2262 /* An Objective C string. */
2270 /* Skip the quote. */
2284 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2286 else if (c == quote)
2290 obstack_1grow (&tempbuf, c);
2292 /* FIXME: this does the wrong thing with multi-byte host
2293 characters. We could use mbrlen here, but that would
2294 make "set host-charset" a bit less useful. */
2299 if (*tokptr != quote)
2302 error (_("Unterminated string in expression."));
2304 error (_("Unmatched single quote."));
2309 value->ptr = (char *) obstack_base (&tempbuf);
2310 value->length = obstack_object_size (&tempbuf);
2314 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2317 /* This is used to associate some attributes with a token. */
2321 /* If this bit is set, the token is C++-only. */
2325 /* If this bit is set, the token is conditional: if there is a
2326 symbol of the same name, then the token is a symbol; otherwise,
2327 the token is a keyword. */
2331 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2337 enum exp_opcode opcode;
2341 static const struct token tokentab3[] =
2343 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2344 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2345 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2346 {"...", DOTDOTDOT, BINOP_END, 0}
2349 static const struct token tokentab2[] =
2351 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2352 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2353 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2354 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2355 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2356 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2357 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2358 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2359 {"++", INCREMENT, BINOP_END, 0},
2360 {"--", DECREMENT, BINOP_END, 0},
2361 {"->", ARROW, BINOP_END, 0},
2362 {"&&", ANDAND, BINOP_END, 0},
2363 {"||", OROR, BINOP_END, 0},
2364 /* "::" is *not* only C++: gdb overrides its meaning in several
2365 different ways, e.g., 'filename'::func, function::variable. */
2366 {"::", COLONCOLON, BINOP_END, 0},
2367 {"<<", LSH, BINOP_END, 0},
2368 {">>", RSH, BINOP_END, 0},
2369 {"==", EQUAL, BINOP_END, 0},
2370 {"!=", NOTEQUAL, BINOP_END, 0},
2371 {"<=", LEQ, BINOP_END, 0},
2372 {">=", GEQ, BINOP_END, 0},
2373 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2376 /* Identifier-like tokens. Only type-specifiers than can appear in
2377 multi-word type names (for example 'double' can appear in 'long
2378 double') need to be listed here. type-specifiers that are only ever
2379 single word (like 'float') are handled by the classify_name function. */
2380 static const struct token ident_tokens[] =
2382 {"unsigned", UNSIGNED, OP_NULL, 0},
2383 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2384 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2385 {"struct", STRUCT, OP_NULL, 0},
2386 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2387 {"sizeof", SIZEOF, OP_NULL, 0},
2388 {"_Alignof", ALIGNOF, OP_NULL, 0},
2389 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2390 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2391 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2392 {"class", CLASS, OP_NULL, FLAG_CXX},
2393 {"union", UNION, OP_NULL, 0},
2394 {"short", SHORT, OP_NULL, 0},
2395 {"const", CONST_KEYWORD, OP_NULL, 0},
2396 {"enum", ENUM, OP_NULL, 0},
2397 {"long", LONG, OP_NULL, 0},
2398 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2399 {"int", INT_KEYWORD, OP_NULL, 0},
2400 {"new", NEW, OP_NULL, FLAG_CXX},
2401 {"delete", DELETE, OP_NULL, FLAG_CXX},
2402 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2404 {"and", ANDAND, BINOP_END, FLAG_CXX},
2405 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2406 {"bitand", '&', OP_NULL, FLAG_CXX},
2407 {"bitor", '|', OP_NULL, FLAG_CXX},
2408 {"compl", '~', OP_NULL, FLAG_CXX},
2409 {"not", '!', OP_NULL, FLAG_CXX},
2410 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2411 {"or", OROR, BINOP_END, FLAG_CXX},
2412 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2413 {"xor", '^', OP_NULL, FLAG_CXX},
2414 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2416 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2417 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2418 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2419 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2421 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2422 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2423 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2424 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2425 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2427 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2430 /* When we find that lexptr (the global var defined in parse.c) is
2431 pointing at a macro invocation, we expand the invocation, and call
2432 scan_macro_expansion to save the old lexptr here and point lexptr
2433 into the expanded text. When we reach the end of that, we call
2434 end_macro_expansion to pop back to the value we saved here. The
2435 macro expansion code promises to return only fully-expanded text,
2436 so we don't need to "push" more than one level.
2438 This is disgusting, of course. It would be cleaner to do all macro
2439 expansion beforehand, and then hand that to lexptr. But we don't
2440 really know where the expression ends. Remember, in a command like
2442 (gdb) break *ADDRESS if CONDITION
2444 we evaluate ADDRESS in the scope of the current frame, but we
2445 evaluate CONDITION in the scope of the breakpoint's location. So
2446 it's simply wrong to try to macro-expand the whole thing at once. */
2447 static const char *macro_original_text;
2449 /* We save all intermediate macro expansions on this obstack for the
2450 duration of a single parse. The expansion text may sometimes have
2451 to live past the end of the expansion, due to yacc lookahead.
2452 Rather than try to be clever about saving the data for a single
2453 token, we simply keep it all and delete it after parsing has
2455 static struct obstack expansion_obstack;
2458 scan_macro_expansion (char *expansion)
2462 /* We'd better not be trying to push the stack twice. */
2463 gdb_assert (! macro_original_text);
2465 /* Copy to the obstack, and then free the intermediate
2467 copy = (char *) obstack_copy0 (&expansion_obstack, expansion,
2468 strlen (expansion));
2471 /* Save the old lexptr value, so we can return to it when we're done
2472 parsing the expanded text. */
2473 macro_original_text = lexptr;
2478 scanning_macro_expansion (void)
2480 return macro_original_text != 0;
2484 finished_macro_expansion (void)
2486 /* There'd better be something to pop back to. */
2487 gdb_assert (macro_original_text);
2489 /* Pop back to the original text. */
2490 lexptr = macro_original_text;
2491 macro_original_text = 0;
2495 scan_macro_cleanup (void *dummy)
2497 if (macro_original_text)
2498 finished_macro_expansion ();
2500 obstack_free (&expansion_obstack, NULL);
2503 /* Return true iff the token represents a C++ cast operator. */
2506 is_cast_operator (const char *token, int len)
2508 return (! strncmp (token, "dynamic_cast", len)
2509 || ! strncmp (token, "static_cast", len)
2510 || ! strncmp (token, "reinterpret_cast", len)
2511 || ! strncmp (token, "const_cast", len));
2514 /* The scope used for macro expansion. */
2515 static struct macro_scope *expression_macro_scope;
2517 /* This is set if a NAME token appeared at the very end of the input
2518 string, with no whitespace separating the name from the EOF. This
2519 is used only when parsing to do field name completion. */
2520 static int saw_name_at_eof;
2522 /* This is set if the previously-returned token was a structure
2523 operator -- either '.' or ARROW. */
2524 static bool last_was_structop;
2526 /* Read one token, getting characters through lexptr. */
2529 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2534 const char *tokstart;
2535 bool saw_structop = last_was_structop;
2538 last_was_structop = false;
2539 *is_quoted_name = false;
2543 /* Check if this is a macro invocation that we need to expand. */
2544 if (! scanning_macro_expansion ())
2546 char *expanded = macro_expand_next (&lexptr,
2547 standard_macro_lookup,
2548 expression_macro_scope);
2551 scan_macro_expansion (expanded);
2554 prev_lexptr = lexptr;
2557 /* See if it is a special token of length 3. */
2558 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2559 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2561 if ((tokentab3[i].flags & FLAG_CXX) != 0
2562 && parse_language (par_state)->la_language != language_cplus)
2566 yylval.opcode = tokentab3[i].opcode;
2567 return tokentab3[i].token;
2570 /* See if it is a special token of length 2. */
2571 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2572 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2574 if ((tokentab2[i].flags & FLAG_CXX) != 0
2575 && parse_language (par_state)->la_language != language_cplus)
2579 yylval.opcode = tokentab2[i].opcode;
2580 if (tokentab2[i].token == ARROW)
2581 last_was_structop = 1;
2582 return tokentab2[i].token;
2585 switch (c = *tokstart)
2588 /* If we were just scanning the result of a macro expansion,
2589 then we need to resume scanning the original text.
2590 If we're parsing for field name completion, and the previous
2591 token allows such completion, return a COMPLETE token.
2592 Otherwise, we were already scanning the original text, and
2593 we're really done. */
2594 if (scanning_macro_expansion ())
2596 finished_macro_expansion ();
2599 else if (saw_name_at_eof)
2601 saw_name_at_eof = 0;
2604 else if (parse_completion && saw_structop)
2619 if (parse_language (par_state)->la_language == language_objc
2626 if (paren_depth == 0)
2633 if (comma_terminates
2635 && ! scanning_macro_expansion ())
2641 /* Might be a floating point number. */
2642 if (lexptr[1] < '0' || lexptr[1] > '9')
2644 last_was_structop = true;
2645 goto symbol; /* Nope, must be a symbol. */
2660 /* It's a number. */
2661 int got_dot = 0, got_e = 0, toktype;
2662 const char *p = tokstart;
2663 int hex = input_radix > 10;
2665 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2670 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2678 /* This test includes !hex because 'e' is a valid hex digit
2679 and thus does not indicate a floating point number when
2680 the radix is hex. */
2681 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2682 got_dot = got_e = 1;
2683 /* This test does not include !hex, because a '.' always indicates
2684 a decimal floating point number regardless of the radix. */
2685 else if (!got_dot && *p == '.')
2687 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2688 && (*p == '-' || *p == '+'))
2689 /* This is the sign of the exponent, not the end of the
2692 /* We will take any letters or digits. parse_number will
2693 complain if past the radix, or if L or U are not final. */
2694 else if ((*p < '0' || *p > '9')
2695 && ((*p < 'a' || *p > 'z')
2696 && (*p < 'A' || *p > 'Z')))
2699 toktype = parse_number (par_state, tokstart, p - tokstart,
2700 got_dot|got_e, &yylval);
2701 if (toktype == ERROR)
2703 char *err_copy = (char *) alloca (p - tokstart + 1);
2705 memcpy (err_copy, tokstart, p - tokstart);
2706 err_copy[p - tokstart] = 0;
2707 error (_("Invalid number \"%s\"."), err_copy);
2715 const char *p = &tokstart[1];
2717 if (parse_language (par_state)->la_language == language_objc)
2719 size_t len = strlen ("selector");
2721 if (strncmp (p, "selector", len) == 0
2722 && (p[len] == '\0' || ISSPACE (p[len])))
2731 while (ISSPACE (*p))
2733 size_t len = strlen ("entry");
2734 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2766 if (tokstart[1] != '"' && tokstart[1] != '\'')
2775 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2780 error (_("Empty character constant."));
2781 else if (host_len > 2 && c == '\'')
2784 namelen = lexptr - tokstart - 1;
2785 *is_quoted_name = true;
2789 else if (host_len > 1)
2790 error (_("Invalid character constant."));
2796 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2797 /* We must have come across a bad character (e.g. ';'). */
2798 error (_("Invalid character '%c' in expression."), c);
2800 /* It's a name. See how long it is. */
2802 for (c = tokstart[namelen];
2803 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2805 /* Template parameter lists are part of the name.
2806 FIXME: This mishandles `print $a<4&&$a>3'. */
2810 if (! is_cast_operator (tokstart, namelen))
2812 /* Scan ahead to get rest of the template specification. Note
2813 that we look ahead only when the '<' adjoins non-whitespace
2814 characters; for comparison expressions, e.g. "a < b > c",
2815 there must be spaces before the '<', etc. */
2816 const char *p = find_template_name_end (tokstart + namelen);
2819 namelen = p - tokstart;
2823 c = tokstart[++namelen];
2826 /* The token "if" terminates the expression and is NOT removed from
2827 the input stream. It doesn't count if it appears in the
2828 expansion of a macro. */
2830 && tokstart[0] == 'i'
2831 && tokstart[1] == 'f'
2832 && ! scanning_macro_expansion ())
2837 /* For the same reason (breakpoint conditions), "thread N"
2838 terminates the expression. "thread" could be an identifier, but
2839 an identifier is never followed by a number without intervening
2840 punctuation. "task" is similar. Handle abbreviations of these,
2841 similarly to breakpoint.c:find_condition_and_thread. */
2843 && (strncmp (tokstart, "thread", namelen) == 0
2844 || strncmp (tokstart, "task", namelen) == 0)
2845 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2846 && ! scanning_macro_expansion ())
2848 const char *p = tokstart + namelen + 1;
2850 while (*p == ' ' || *p == '\t')
2852 if (*p >= '0' && *p <= '9')
2860 yylval.sval.ptr = tokstart;
2861 yylval.sval.length = namelen;
2863 /* Catch specific keywords. */
2864 copy = copy_name (yylval.sval);
2865 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2866 if (strcmp (copy, ident_tokens[i].oper) == 0)
2868 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2869 && parse_language (par_state)->la_language != language_cplus)
2872 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2874 struct field_of_this_result is_a_field_of_this;
2876 if (lookup_symbol (copy, expression_context_block,
2878 (parse_language (par_state)->la_language
2879 == language_cplus ? &is_a_field_of_this
2883 /* The keyword is shadowed. */
2888 /* It is ok to always set this, even though we don't always
2889 strictly need to. */
2890 yylval.opcode = ident_tokens[i].opcode;
2891 return ident_tokens[i].token;
2894 if (*tokstart == '$')
2897 if (parse_completion && *lexptr == '\0')
2898 saw_name_at_eof = 1;
2900 yylval.ssym.stoken = yylval.sval;
2901 yylval.ssym.sym.symbol = NULL;
2902 yylval.ssym.sym.block = NULL;
2903 yylval.ssym.is_a_field_of_this = 0;
2907 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2908 struct token_and_value
2914 /* A FIFO of tokens that have been read but not yet returned to the
2916 static std::vector<token_and_value> token_fifo;
2918 /* Non-zero if the lexer should return tokens from the FIFO. */
2921 /* Temporary storage for c_lex; this holds symbol names as they are
2923 auto_obstack name_obstack;
2925 /* Classify a NAME token. The contents of the token are in `yylval'.
2926 Updates yylval and returns the new token type. BLOCK is the block
2927 in which lookups start; this can be NULL to mean the global scope.
2928 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2929 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2930 a structure operator -- either '.' or ARROW */
2933 classify_name (struct parser_state *par_state, const struct block *block,
2934 bool is_quoted_name, bool is_after_structop)
2936 struct block_symbol bsym;
2938 struct field_of_this_result is_a_field_of_this;
2940 copy = copy_name (yylval.sval);
2942 /* Initialize this in case we *don't* use it in this call; that way
2943 we can refer to it unconditionally below. */
2944 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2946 bsym = lookup_symbol (copy, block, VAR_DOMAIN,
2947 parse_language (par_state)->la_name_of_this
2948 ? &is_a_field_of_this : NULL);
2950 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2952 yylval.ssym.sym = bsym;
2953 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2956 else if (!bsym.symbol)
2958 /* If we found a field of 'this', we might have erroneously
2959 found a constructor where we wanted a type name. Handle this
2960 case by noticing that we found a constructor and then look up
2961 the type tag instead. */
2962 if (is_a_field_of_this.type != NULL
2963 && is_a_field_of_this.fn_field != NULL
2964 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2967 struct field_of_this_result inner_is_a_field_of_this;
2969 bsym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2970 &inner_is_a_field_of_this);
2971 if (bsym.symbol != NULL)
2973 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2978 /* If we found a field on the "this" object, or we are looking
2979 up a field on a struct, then we want to prefer it over a
2980 filename. However, if the name was quoted, then it is better
2981 to check for a filename or a block, since this is the only
2982 way the user has of requiring the extension to be used. */
2983 if ((is_a_field_of_this.type == NULL && !is_after_structop)
2986 /* See if it's a file name. */
2987 struct symtab *symtab;
2989 symtab = lookup_symtab (copy);
2992 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
2999 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3001 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3005 /* See if it's an ObjC classname. */
3006 if (parse_language (par_state)->la_language == language_objc && !bsym.symbol)
3008 CORE_ADDR Class = lookup_objc_class (parse_gdbarch (par_state), copy);
3013 yylval.theclass.theclass = Class;
3014 sym = lookup_struct_typedef (copy, expression_context_block, 1);
3016 yylval.theclass.type = SYMBOL_TYPE (sym);
3021 /* Input names that aren't symbols but ARE valid hex numbers, when
3022 the input radix permits them, can be names or numbers depending
3023 on the parse. Note we support radixes > 16 here. */
3025 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3026 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3028 YYSTYPE newlval; /* Its value is ignored. */
3029 int hextype = parse_number (par_state, copy, yylval.sval.length,
3034 yylval.ssym.sym = bsym;
3035 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3040 /* Any other kind of symbol */
3041 yylval.ssym.sym = bsym;
3042 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3044 if (bsym.symbol == NULL
3045 && parse_language (par_state)->la_language == language_cplus
3046 && is_a_field_of_this.type == NULL
3047 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
3048 return UNKNOWN_CPP_NAME;
3053 /* Like classify_name, but used by the inner loop of the lexer, when a
3054 name might have already been seen. CONTEXT is the context type, or
3055 NULL if this is the first component of a name. */
3058 classify_inner_name (struct parser_state *par_state,
3059 const struct block *block, struct type *context)
3064 if (context == NULL)
3065 return classify_name (par_state, block, false, false);
3067 type = check_typedef (context);
3068 if (!type_aggregate_p (type))
3071 copy = copy_name (yylval.ssym.stoken);
3072 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3073 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block, VAR_DOMAIN);
3075 /* If no symbol was found, search for a matching base class named
3076 COPY. This will allow users to enter qualified names of class members
3077 relative to the `this' pointer. */
3078 if (yylval.ssym.sym.symbol == NULL)
3080 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3082 if (base_type != NULL)
3084 yylval.tsym.type = base_type;
3091 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3095 /* cp_lookup_nested_symbol might have accidentally found a constructor
3096 named COPY when we really wanted a base class of the same name.
3097 Double-check this case by looking for a base class. */
3099 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3101 if (base_type != NULL)
3103 yylval.tsym.type = base_type;
3110 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3116 internal_error (__FILE__, __LINE__, _("not reached"));
3119 /* The outer level of a two-level lexer. This calls the inner lexer
3120 to return tokens. It then either returns these tokens, or
3121 aggregates them into a larger token. This lets us work around a
3122 problem in our parsing approach, where the parser could not
3123 distinguish between qualified names and qualified types at the
3126 This approach is still not ideal, because it mishandles template
3127 types. See the comment in lex_one_token for an example. However,
3128 this is still an improvement over the earlier approach, and will
3129 suffice until we move to better parsing technology. */
3134 token_and_value current;
3135 int first_was_coloncolon, last_was_coloncolon;
3136 struct type *context_type = NULL;
3137 int last_to_examine, next_to_examine, checkpoint;
3138 const struct block *search_block;
3139 bool is_quoted_name, last_lex_was_structop;
3141 if (popping && !token_fifo.empty ())
3145 last_lex_was_structop = last_was_structop;
3147 /* Read the first token and decide what to do. Most of the
3148 subsequent code is C++-only; but also depends on seeing a "::" or
3150 current.token = lex_one_token (pstate, &is_quoted_name);
3151 if (current.token == NAME)
3152 current.token = classify_name (pstate, expression_context_block,
3153 is_quoted_name, last_lex_was_structop);
3154 if (parse_language (pstate)->la_language != language_cplus
3155 || (current.token != TYPENAME && current.token != COLONCOLON
3156 && current.token != FILENAME))
3157 return current.token;
3159 /* Read any sequence of alternating "::" and name-like tokens into
3161 current.value = yylval;
3162 token_fifo.push_back (current);
3163 last_was_coloncolon = current.token == COLONCOLON;
3168 /* We ignore quoted names other than the very first one.
3169 Subsequent ones do not have any special meaning. */
3170 current.token = lex_one_token (pstate, &ignore);
3171 current.value = yylval;
3172 token_fifo.push_back (current);
3174 if ((last_was_coloncolon && current.token != NAME)
3175 || (!last_was_coloncolon && current.token != COLONCOLON))
3177 last_was_coloncolon = !last_was_coloncolon;
3181 /* We always read one extra token, so compute the number of tokens
3182 to examine accordingly. */
3183 last_to_examine = token_fifo.size () - 2;
3184 next_to_examine = 0;
3186 current = token_fifo[next_to_examine];
3189 name_obstack.clear ();
3191 if (current.token == FILENAME)
3192 search_block = current.value.bval;
3193 else if (current.token == COLONCOLON)
3194 search_block = NULL;
3197 gdb_assert (current.token == TYPENAME);
3198 search_block = expression_context_block;
3199 obstack_grow (&name_obstack, current.value.sval.ptr,
3200 current.value.sval.length);
3201 context_type = current.value.tsym.type;
3205 first_was_coloncolon = current.token == COLONCOLON;
3206 last_was_coloncolon = first_was_coloncolon;
3208 while (next_to_examine <= last_to_examine)
3210 token_and_value next;
3212 next = token_fifo[next_to_examine];
3215 if (next.token == NAME && last_was_coloncolon)
3219 yylval = next.value;
3220 classification = classify_inner_name (pstate, search_block,
3222 /* We keep going until we either run out of names, or until
3223 we have a qualified name which is not a type. */
3224 if (classification != TYPENAME && classification != NAME)
3227 /* Accept up to this token. */
3228 checkpoint = next_to_examine;
3230 /* Update the partial name we are constructing. */
3231 if (context_type != NULL)
3233 /* We don't want to put a leading "::" into the name. */
3234 obstack_grow_str (&name_obstack, "::");
3236 obstack_grow (&name_obstack, next.value.sval.ptr,
3237 next.value.sval.length);
3239 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3240 yylval.sval.length = obstack_object_size (&name_obstack);
3241 current.value = yylval;
3242 current.token = classification;
3244 last_was_coloncolon = 0;
3246 if (classification == NAME)
3249 context_type = yylval.tsym.type;
3251 else if (next.token == COLONCOLON && !last_was_coloncolon)
3252 last_was_coloncolon = 1;
3255 /* We've reached the end of the name. */
3260 /* If we have a replacement token, install it as the first token in
3261 the FIFO, and delete the other constituent tokens. */
3264 current.value.sval.ptr
3265 = (const char *) obstack_copy0 (&expansion_obstack,
3266 current.value.sval.ptr,
3267 current.value.sval.length);
3269 token_fifo[0] = current;
3271 token_fifo.erase (token_fifo.begin () + 1,
3272 token_fifo.begin () + checkpoint);
3276 current = token_fifo[0];
3277 token_fifo.erase (token_fifo.begin ());
3278 yylval = current.value;
3279 return current.token;
3283 c_parse (struct parser_state *par_state)
3286 struct cleanup *back_to;
3288 /* Setting up the parser state. */
3289 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3290 gdb_assert (par_state != NULL);
3293 c_parse_state cstate;
3294 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3296 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3298 if (expression_context_block)
3299 macro_scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3301 macro_scope = default_macro_scope ();
3303 macro_scope = user_macro_scope ();
3305 scoped_restore restore_macro_scope
3306 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3308 /* Initialize macro expansion code. */
3309 obstack_init (&expansion_obstack);
3310 gdb_assert (! macro_original_text);
3311 /* Note that parsing (within yyparse) freely installs cleanups
3312 assuming they'll be run here (below). */
3313 back_to = make_cleanup (scan_macro_cleanup, 0);
3315 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3318 /* Initialize some state used by the lexer. */
3319 last_was_structop = false;
3320 saw_name_at_eof = 0;
3322 token_fifo.clear ();
3324 name_obstack.clear ();
3326 result = yyparse ();
3327 do_cleanups (back_to);
3334 /* This is called via the YYPRINT macro when parser debugging is
3335 enabled. It prints a token's value. */
3338 c_print_token (FILE *file, int type, YYSTYPE value)
3343 parser_fprintf (file, "typed_val_int<%s, %s>",
3344 TYPE_SAFE_NAME (value.typed_val_int.type),
3345 pulongest (value.typed_val_int.val));
3351 char *copy = (char *) alloca (value.tsval.length + 1);
3353 memcpy (copy, value.tsval.ptr, value.tsval.length);
3354 copy[value.tsval.length] = '\0';
3356 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3362 parser_fprintf (file, "sval<%s>", copy_name (value.sval));
3366 parser_fprintf (file, "tsym<type=%s, name=%s>",
3367 TYPE_SAFE_NAME (value.tsym.type),
3368 copy_name (value.tsym.stoken));
3372 case UNKNOWN_CPP_NAME:
3375 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3376 copy_name (value.ssym.stoken),
3377 (value.ssym.sym.symbol == NULL
3378 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym.symbol)),
3379 value.ssym.is_a_field_of_this);
3383 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3391 yyerror (const char *msg)
3394 lexptr = prev_lexptr;
3396 error (_("A %s in expression, near `%s'."), msg, lexptr);