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;
79 /* This is set and cleared in c_parse. */
81 static struct c_parse_state *cpstate;
85 static int yylex (void);
87 static void yyerror (const char *);
89 static int type_aggregate_p (struct type *);
93 /* Although the yacc "value" of an expression is not used,
94 since the result is stored in the structure being created,
95 other node types do have values. */
110 struct typed_stoken tsval;
112 struct symtoken ssym;
114 const struct block *bval;
115 enum exp_opcode opcode;
117 struct stoken_vector svec;
118 std::vector<struct type *> *tvec;
120 struct type_stack *type_stack;
122 struct objc_class_str theclass;
126 /* YYSTYPE gets defined by %union */
127 static int parse_number (struct parser_state *par_state,
128 const char *, int, int, YYSTYPE *);
129 static struct stoken operator_stoken (const char *);
130 static struct stoken typename_stoken (const char *);
131 static void check_parameter_typelist (std::vector<struct type *> *);
132 static void write_destructor_name (struct parser_state *par_state,
136 static void c_print_token (FILE *file, int type, YYSTYPE value);
137 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
141 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
143 %type <tval> type typebase
144 %type <tvec> nonempty_typelist func_mod parameter_typelist
145 /* %type <bval> block */
147 /* Fancy type parsing. */
149 %type <lval> array_mod
150 %type <tval> conversion_type_id
152 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
154 %token <typed_val_int> INT
155 %token <typed_val_float> FLOAT
157 /* Both NAME and TYPENAME tokens represent symbols in the input,
158 and both convey their data as strings.
159 But a TYPENAME is a string that happens to be defined as a typedef
160 or builtin type name (such as int or char)
161 and a NAME is any other symbol.
162 Contexts where this distinction is not important can use the
163 nonterminal "name", which matches either NAME or TYPENAME. */
165 %token <tsval> STRING
166 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
167 %token SELECTOR /* ObjC "@selector" pseudo-operator */
169 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
170 %token <ssym> UNKNOWN_CPP_NAME
171 %token <voidval> COMPLETE
172 %token <tsym> TYPENAME
173 %token <theclass> CLASSNAME /* ObjC Class name */
174 %type <sval> name field_name
175 %type <svec> string_exp
176 %type <ssym> name_not_typename
177 %type <tsym> type_name
179 /* This is like a '[' token, but is only generated when parsing
180 Objective C. This lets us reuse the same parser without
181 erroneously parsing ObjC-specific expressions in C. */
184 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
185 but which would parse as a valid number in the current input radix.
186 E.g. "c" when input_radix==16. Depending on the parse, it will be
187 turned into a name or into a number. */
189 %token <ssym> NAME_OR_INT
192 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
197 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
203 /* Special type cases, put in to allow the parser to distinguish different
205 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
207 %token <sval> VARIABLE
209 %token <opcode> ASSIGN_MODIFY
218 %right '=' ASSIGN_MODIFY
226 %left '<' '>' LEQ GEQ
231 %right UNARY INCREMENT DECREMENT
232 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
233 %token <ssym> BLOCKNAME
234 %token <bval> FILENAME
248 { write_exp_elt_opcode(pstate, OP_TYPE);
249 write_exp_elt_type(pstate, $1);
250 write_exp_elt_opcode(pstate, OP_TYPE);}
253 write_exp_elt_opcode (pstate, OP_TYPEOF);
255 | TYPEOF '(' type ')'
257 write_exp_elt_opcode (pstate, OP_TYPE);
258 write_exp_elt_type (pstate, $3);
259 write_exp_elt_opcode (pstate, OP_TYPE);
261 | DECLTYPE '(' exp ')'
263 write_exp_elt_opcode (pstate, OP_DECLTYPE);
267 /* Expressions, including the comma operator. */
270 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
273 /* Expressions, not including the comma operator. */
274 exp : '*' exp %prec UNARY
275 { write_exp_elt_opcode (pstate, UNOP_IND); }
278 exp : '&' exp %prec UNARY
279 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
282 exp : '-' exp %prec UNARY
283 { write_exp_elt_opcode (pstate, UNOP_NEG); }
286 exp : '+' exp %prec UNARY
287 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
290 exp : '!' exp %prec UNARY
291 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
294 exp : '~' exp %prec UNARY
295 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
298 exp : INCREMENT exp %prec UNARY
299 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
302 exp : DECREMENT exp %prec UNARY
303 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
306 exp : exp INCREMENT %prec UNARY
307 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
310 exp : exp DECREMENT %prec UNARY
311 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
314 exp : TYPEID '(' exp ')' %prec UNARY
315 { write_exp_elt_opcode (pstate, OP_TYPEID); }
318 exp : TYPEID '(' type_exp ')' %prec UNARY
319 { write_exp_elt_opcode (pstate, OP_TYPEID); }
322 exp : SIZEOF exp %prec UNARY
323 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
326 exp : ALIGNOF '(' type_exp ')' %prec UNARY
327 { write_exp_elt_opcode (pstate, UNOP_ALIGNOF); }
330 exp : exp ARROW field_name
331 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
332 write_exp_string (pstate, $3);
333 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
336 exp : exp ARROW field_name COMPLETE
337 { mark_struct_expression (pstate);
338 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
339 write_exp_string (pstate, $3);
340 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
343 exp : exp ARROW COMPLETE
345 mark_struct_expression (pstate);
346 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
349 write_exp_string (pstate, s);
350 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
353 exp : exp ARROW '~' name
354 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
355 write_destructor_name (pstate, $4);
356 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
359 exp : exp ARROW '~' name COMPLETE
360 { mark_struct_expression (pstate);
361 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
362 write_destructor_name (pstate, $4);
363 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
366 exp : exp ARROW qualified_name
367 { /* exp->type::name becomes exp->*(&type::name) */
368 /* Note: this doesn't work if name is a
369 static member! FIXME */
370 write_exp_elt_opcode (pstate, UNOP_ADDR);
371 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
374 exp : exp ARROW_STAR exp
375 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
378 exp : exp '.' field_name
379 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
380 write_exp_string (pstate, $3);
381 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
384 exp : exp '.' field_name COMPLETE
385 { mark_struct_expression (pstate);
386 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
387 write_exp_string (pstate, $3);
388 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
391 exp : exp '.' COMPLETE
393 mark_struct_expression (pstate);
394 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
397 write_exp_string (pstate, s);
398 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
401 exp : exp '.' '~' name
402 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
403 write_destructor_name (pstate, $4);
404 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
407 exp : exp '.' '~' name COMPLETE
408 { mark_struct_expression (pstate);
409 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
410 write_destructor_name (pstate, $4);
411 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
414 exp : exp '.' qualified_name
415 { /* exp.type::name becomes exp.*(&type::name) */
416 /* Note: this doesn't work if name is a
417 static member! FIXME */
418 write_exp_elt_opcode (pstate, UNOP_ADDR);
419 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
422 exp : exp DOT_STAR exp
423 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
426 exp : exp '[' exp1 ']'
427 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
430 exp : exp OBJC_LBRAC exp1 ']'
431 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
435 * The rules below parse ObjC message calls of the form:
436 * '[' target selector {':' argument}* ']'
439 exp : OBJC_LBRAC TYPENAME
443 theclass = lookup_objc_class (parse_gdbarch (pstate),
444 copy_name ($2.stoken));
446 error (_("%s is not an ObjC Class"),
447 copy_name ($2.stoken));
448 write_exp_elt_opcode (pstate, OP_LONG);
449 write_exp_elt_type (pstate,
450 parse_type (pstate)->builtin_int);
451 write_exp_elt_longcst (pstate, (LONGEST) theclass);
452 write_exp_elt_opcode (pstate, OP_LONG);
456 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
457 end_msglist (pstate);
458 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
462 exp : OBJC_LBRAC CLASSNAME
464 write_exp_elt_opcode (pstate, OP_LONG);
465 write_exp_elt_type (pstate,
466 parse_type (pstate)->builtin_int);
467 write_exp_elt_longcst (pstate, (LONGEST) $2.theclass);
468 write_exp_elt_opcode (pstate, OP_LONG);
472 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
473 end_msglist (pstate);
474 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
481 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
482 end_msglist (pstate);
483 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
488 { add_msglist(&$1, 0); }
496 msgarg : name ':' exp
497 { add_msglist(&$1, 1); }
498 | ':' exp /* Unnamed arg. */
499 { add_msglist(0, 1); }
500 | ',' exp /* Variable number of args. */
501 { add_msglist(0, 0); }
505 /* This is to save the value of arglist_len
506 being accumulated by an outer function call. */
507 { start_arglist (); }
508 arglist ')' %prec ARROW
509 { write_exp_elt_opcode (pstate, OP_FUNCALL);
510 write_exp_elt_longcst (pstate,
511 (LONGEST) end_arglist ());
512 write_exp_elt_opcode (pstate, OP_FUNCALL); }
515 /* This is here to disambiguate with the production for
516 "func()::static_var" further below, which uses
517 function_method_void. */
518 exp : exp '(' ')' %prec ARROW
520 write_exp_elt_opcode (pstate, OP_FUNCALL);
521 write_exp_elt_longcst (pstate,
522 (LONGEST) end_arglist ());
523 write_exp_elt_opcode (pstate, OP_FUNCALL); }
527 exp : UNKNOWN_CPP_NAME '('
529 /* This could potentially be a an argument defined
530 lookup function (Koenig). */
531 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
532 write_exp_elt_block (pstate,
533 expression_context_block);
534 write_exp_elt_sym (pstate,
535 NULL); /* Placeholder. */
536 write_exp_string (pstate, $1.stoken);
537 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
539 /* This is to save the value of arglist_len
540 being accumulated by an outer function call. */
544 arglist ')' %prec ARROW
546 write_exp_elt_opcode (pstate, OP_FUNCALL);
547 write_exp_elt_longcst (pstate,
548 (LONGEST) end_arglist ());
549 write_exp_elt_opcode (pstate, OP_FUNCALL);
554 { start_arglist (); }
564 arglist : arglist ',' exp %prec ABOVE_COMMA
568 function_method: exp '(' parameter_typelist ')' const_or_volatile
570 std::vector<struct type *> *type_list = $3;
571 LONGEST len = type_list->size ();
573 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
574 /* Save the const/volatile qualifiers as
575 recorded by the const_or_volatile
576 production's actions. */
577 write_exp_elt_longcst (pstate,
578 follow_type_instance_flags ());
579 write_exp_elt_longcst (pstate, len);
580 for (type *type_elt : *type_list)
581 write_exp_elt_type (pstate, type_elt);
582 write_exp_elt_longcst(pstate, len);
583 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
587 function_method_void: exp '(' ')' const_or_volatile
588 { write_exp_elt_opcode (pstate, TYPE_INSTANCE);
590 write_exp_elt_longcst (pstate,
591 follow_type_instance_flags ());
592 write_exp_elt_longcst (pstate, 0);
593 write_exp_elt_longcst (pstate, 0);
594 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
598 exp : function_method
601 /* Normally we must interpret "func()" as a function call, instead of
602 a type. The user needs to write func(void) to disambiguate.
603 However, in the "func()::static_var" case, there's no
605 function_method_void_or_typelist: function_method
606 | function_method_void
609 exp : function_method_void_or_typelist COLONCOLON name
611 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
612 write_exp_string (pstate, $3);
613 write_exp_elt_opcode (pstate, OP_FUNC_STATIC_VAR);
618 { $$ = end_arglist () - 1; }
620 exp : lcurly arglist rcurly %prec ARROW
621 { write_exp_elt_opcode (pstate, OP_ARRAY);
622 write_exp_elt_longcst (pstate, (LONGEST) 0);
623 write_exp_elt_longcst (pstate, (LONGEST) $3);
624 write_exp_elt_opcode (pstate, OP_ARRAY); }
627 exp : lcurly type_exp rcurly exp %prec UNARY
628 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
631 exp : '(' type_exp ')' exp %prec UNARY
632 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
639 /* Binary operators in order of decreasing precedence. */
642 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
646 { write_exp_elt_opcode (pstate, BINOP_MUL); }
650 { write_exp_elt_opcode (pstate, BINOP_DIV); }
654 { write_exp_elt_opcode (pstate, BINOP_REM); }
658 { write_exp_elt_opcode (pstate, BINOP_ADD); }
662 { write_exp_elt_opcode (pstate, BINOP_SUB); }
666 { write_exp_elt_opcode (pstate, BINOP_LSH); }
670 { write_exp_elt_opcode (pstate, BINOP_RSH); }
674 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
677 exp : exp NOTEQUAL exp
678 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
682 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
686 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
690 { write_exp_elt_opcode (pstate, BINOP_LESS); }
694 { write_exp_elt_opcode (pstate, BINOP_GTR); }
698 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
702 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
706 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
710 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
714 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
717 exp : exp '?' exp ':' exp %prec '?'
718 { write_exp_elt_opcode (pstate, TERNOP_COND); }
722 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
725 exp : exp ASSIGN_MODIFY exp
726 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
727 write_exp_elt_opcode (pstate, $2);
728 write_exp_elt_opcode (pstate,
729 BINOP_ASSIGN_MODIFY); }
733 { write_exp_elt_opcode (pstate, OP_LONG);
734 write_exp_elt_type (pstate, $1.type);
735 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
736 write_exp_elt_opcode (pstate, OP_LONG); }
741 struct stoken_vector vec;
744 write_exp_string_vector (pstate, $1.type, &vec);
750 parse_number (pstate, $1.stoken.ptr,
751 $1.stoken.length, 0, &val);
752 write_exp_elt_opcode (pstate, OP_LONG);
753 write_exp_elt_type (pstate, val.typed_val_int.type);
754 write_exp_elt_longcst (pstate,
755 (LONGEST) val.typed_val_int.val);
756 write_exp_elt_opcode (pstate, OP_LONG);
762 { write_exp_elt_opcode (pstate, OP_FLOAT);
763 write_exp_elt_type (pstate, $1.type);
764 write_exp_elt_floatcst (pstate, $1.val);
765 write_exp_elt_opcode (pstate, OP_FLOAT); }
773 write_dollar_variable (pstate, $1);
777 exp : SELECTOR '(' name ')'
779 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
780 write_exp_string (pstate, $3);
781 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
784 exp : SIZEOF '(' type ')' %prec UNARY
785 { struct type *type = $3;
786 write_exp_elt_opcode (pstate, OP_LONG);
787 write_exp_elt_type (pstate, lookup_signed_typename
788 (parse_language (pstate),
789 parse_gdbarch (pstate),
791 type = check_typedef (type);
793 /* $5.3.3/2 of the C++ Standard (n3290 draft)
794 says of sizeof: "When applied to a reference
795 or a reference type, the result is the size of
796 the referenced type." */
797 if (TYPE_IS_REFERENCE (type))
798 type = check_typedef (TYPE_TARGET_TYPE (type));
799 write_exp_elt_longcst (pstate,
800 (LONGEST) TYPE_LENGTH (type));
801 write_exp_elt_opcode (pstate, OP_LONG); }
804 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
805 { write_exp_elt_opcode (pstate,
806 UNOP_REINTERPRET_CAST); }
809 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
810 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
813 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
814 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
817 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
818 { /* We could do more error checking here, but
819 it doesn't seem worthwhile. */
820 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
826 /* We copy the string here, and not in the
827 lexer, to guarantee that we do not leak a
828 string. Note that we follow the
829 NUL-termination convention of the
831 struct typed_stoken *vec = XNEW (struct typed_stoken);
836 vec->length = $1.length;
837 vec->ptr = (char *) malloc ($1.length + 1);
838 memcpy (vec->ptr, $1.ptr, $1.length + 1);
843 /* Note that we NUL-terminate here, but just
847 $$.tokens = XRESIZEVEC (struct typed_stoken,
850 p = (char *) malloc ($2.length + 1);
851 memcpy (p, $2.ptr, $2.length + 1);
853 $$.tokens[$$.len - 1].type = $2.type;
854 $$.tokens[$$.len - 1].length = $2.length;
855 $$.tokens[$$.len - 1].ptr = p;
862 c_string_type type = C_STRING;
864 for (i = 0; i < $1.len; ++i)
866 switch ($1.tokens[i].type)
874 && type != $1.tokens[i].type)
875 error (_("Undefined string concatenation."));
876 type = (enum c_string_type_values) $1.tokens[i].type;
880 internal_error (__FILE__, __LINE__,
881 "unrecognized type in string concatenation");
885 write_exp_string_vector (pstate, type, &$1);
886 for (i = 0; i < $1.len; ++i)
887 free ($1.tokens[i].ptr);
892 exp : NSSTRING /* ObjC NextStep NSString constant
893 * of the form '@' '"' string '"'.
895 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
896 write_exp_string (pstate, $1);
897 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
902 { write_exp_elt_opcode (pstate, OP_LONG);
903 write_exp_elt_type (pstate,
904 parse_type (pstate)->builtin_bool);
905 write_exp_elt_longcst (pstate, (LONGEST) 1);
906 write_exp_elt_opcode (pstate, OP_LONG); }
910 { write_exp_elt_opcode (pstate, OP_LONG);
911 write_exp_elt_type (pstate,
912 parse_type (pstate)->builtin_bool);
913 write_exp_elt_longcst (pstate, (LONGEST) 0);
914 write_exp_elt_opcode (pstate, OP_LONG); }
922 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
924 error (_("No file or function \"%s\"."),
925 copy_name ($1.stoken));
933 block : block COLONCOLON name
935 = lookup_symbol (copy_name ($3), $1,
936 VAR_DOMAIN, NULL).symbol;
938 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
939 error (_("No function \"%s\" in specified context."),
941 $$ = SYMBOL_BLOCK_VALUE (tem); }
944 variable: name_not_typename ENTRY
945 { struct symbol *sym = $1.sym.symbol;
947 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
948 || !symbol_read_needs_frame (sym))
949 error (_("@entry can be used only for function "
950 "parameters, not for \"%s\""),
951 copy_name ($1.stoken));
953 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
954 write_exp_elt_sym (pstate, sym);
955 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
959 variable: block COLONCOLON name
960 { struct block_symbol sym
961 = lookup_symbol (copy_name ($3), $1,
965 error (_("No symbol \"%s\" in specified context."),
967 if (symbol_read_needs_frame (sym.symbol))
969 innermost_block.update (sym);
971 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
972 write_exp_elt_block (pstate, sym.block);
973 write_exp_elt_sym (pstate, sym.symbol);
974 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
977 qualified_name: TYPENAME COLONCOLON name
979 struct type *type = $1.type;
980 type = check_typedef (type);
981 if (!type_aggregate_p (type))
982 error (_("`%s' is not defined as an aggregate type."),
983 TYPE_SAFE_NAME (type));
985 write_exp_elt_opcode (pstate, OP_SCOPE);
986 write_exp_elt_type (pstate, type);
987 write_exp_string (pstate, $3);
988 write_exp_elt_opcode (pstate, OP_SCOPE);
990 | TYPENAME COLONCOLON '~' name
992 struct type *type = $1.type;
993 struct stoken tmp_token;
996 type = check_typedef (type);
997 if (!type_aggregate_p (type))
998 error (_("`%s' is not defined as an aggregate type."),
999 TYPE_SAFE_NAME (type));
1000 buf = (char *) alloca ($4.length + 2);
1001 tmp_token.ptr = buf;
1002 tmp_token.length = $4.length + 1;
1004 memcpy (buf+1, $4.ptr, $4.length);
1005 buf[tmp_token.length] = 0;
1007 /* Check for valid destructor name. */
1008 destructor_name_p (tmp_token.ptr, $1.type);
1009 write_exp_elt_opcode (pstate, OP_SCOPE);
1010 write_exp_elt_type (pstate, type);
1011 write_exp_string (pstate, tmp_token);
1012 write_exp_elt_opcode (pstate, OP_SCOPE);
1014 | TYPENAME COLONCOLON name COLONCOLON name
1016 char *copy = copy_name ($3);
1017 error (_("No type \"%s\" within class "
1018 "or namespace \"%s\"."),
1019 copy, TYPE_SAFE_NAME ($1.type));
1023 variable: qualified_name
1024 | COLONCOLON name_not_typename
1026 char *name = copy_name ($2.stoken);
1028 struct bound_minimal_symbol msymbol;
1031 = lookup_symbol (name, (const struct block *) NULL,
1032 VAR_DOMAIN, NULL).symbol;
1035 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1036 write_exp_elt_block (pstate, NULL);
1037 write_exp_elt_sym (pstate, sym);
1038 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1042 msymbol = lookup_bound_minimal_symbol (name);
1043 if (msymbol.minsym != NULL)
1044 write_exp_msymbol (pstate, msymbol);
1045 else if (!have_full_symbols () && !have_partial_symbols ())
1046 error (_("No symbol table is loaded. Use the \"file\" command."));
1048 error (_("No symbol \"%s\" in current context."), name);
1052 variable: name_not_typename
1053 { struct block_symbol sym = $1.sym;
1057 if (symbol_read_needs_frame (sym.symbol))
1058 innermost_block.update (sym);
1060 /* If we found a function, see if it's
1061 an ifunc resolver that has the same
1062 address as the ifunc symbol itself.
1063 If so, prefer the ifunc symbol. */
1065 bound_minimal_symbol resolver
1066 = find_gnu_ifunc (sym.symbol);
1067 if (resolver.minsym != NULL)
1068 write_exp_msymbol (pstate, resolver);
1071 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1072 write_exp_elt_block (pstate, sym.block);
1073 write_exp_elt_sym (pstate, sym.symbol);
1074 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1077 else if ($1.is_a_field_of_this)
1079 /* C++: it hangs off of `this'. Must
1080 not inadvertently convert from a method call
1082 innermost_block.update (sym);
1083 write_exp_elt_opcode (pstate, OP_THIS);
1084 write_exp_elt_opcode (pstate, OP_THIS);
1085 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1086 write_exp_string (pstate, $1.stoken);
1087 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1091 char *arg = copy_name ($1.stoken);
1093 bound_minimal_symbol msymbol
1094 = lookup_bound_minimal_symbol (arg);
1095 if (msymbol.minsym == NULL)
1097 if (!have_full_symbols () && !have_partial_symbols ())
1098 error (_("No symbol table is loaded. Use the \"file\" command."));
1100 error (_("No symbol \"%s\" in current context."),
1101 copy_name ($1.stoken));
1104 /* This minsym might be an alias for
1105 another function. See if we can find
1106 the debug symbol for the target, and
1107 if so, use it instead, since it has
1108 return type / prototype info. This
1109 is important for example for "p
1110 *__errno_location()". */
1111 symbol *alias_target
1112 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1113 && msymbol.minsym->type != mst_data_gnu_ifunc)
1114 ? find_function_alias_target (msymbol)
1116 if (alias_target != NULL)
1118 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1120 (pstate, SYMBOL_BLOCK_VALUE (alias_target));
1121 write_exp_elt_sym (pstate, alias_target);
1122 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1125 write_exp_msymbol (pstate, msymbol);
1130 space_identifier : '@' NAME
1131 { insert_type_address_space (pstate, copy_name ($2.stoken)); }
1134 const_or_volatile: const_or_volatile_noopt
1138 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1141 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1142 | const_or_volatile_noopt
1145 const_or_volatile_or_space_identifier:
1146 const_or_volatile_or_space_identifier_noopt
1152 { insert_type (tp_pointer); }
1153 const_or_volatile_or_space_identifier
1155 { insert_type (tp_pointer); }
1156 const_or_volatile_or_space_identifier
1158 { insert_type (tp_reference); }
1160 { insert_type (tp_reference); }
1162 { insert_type (tp_rvalue_reference); }
1163 | ANDAND ptr_operator
1164 { insert_type (tp_rvalue_reference); }
1167 ptr_operator_ts: ptr_operator
1169 $$ = get_type_stack ();
1170 cpstate->type_stacks.emplace_back ($$);
1174 abs_decl: ptr_operator_ts direct_abs_decl
1175 { $$ = append_type_stack ($2, $1); }
1180 direct_abs_decl: '(' abs_decl ')'
1182 | direct_abs_decl array_mod
1184 push_type_stack ($1);
1186 push_type (tp_array);
1187 $$ = get_type_stack ();
1192 push_type (tp_array);
1193 $$ = get_type_stack ();
1196 | direct_abs_decl func_mod
1198 push_type_stack ($1);
1200 $$ = get_type_stack ();
1205 $$ = get_type_stack ();
1215 | OBJC_LBRAC INT ']'
1221 $$ = new std::vector<struct type *>;
1222 cpstate->type_lists.emplace_back ($$);
1224 | '(' parameter_typelist ')'
1228 /* We used to try to recognize pointer to member types here, but
1229 that didn't work (shift/reduce conflicts meant that these rules never
1230 got executed). The problem is that
1231 int (foo::bar::baz::bizzle)
1232 is a function type but
1233 int (foo::bar::baz::bizzle::*)
1234 is a pointer to member type. Stroustrup loses again! */
1239 /* Implements (approximately): (type-qualifier)* type-specifier.
1241 When type-specifier is only ever a single word, like 'float' then these
1242 arrive as pre-built TYPENAME tokens thanks to the classify_name
1243 function. However, when a type-specifier can contain multiple words,
1244 for example 'double' can appear as just 'double' or 'long double', and
1245 similarly 'long' can appear as just 'long' or in 'long double', then
1246 these type-specifiers are parsed into their own tokens in the function
1247 lex_one_token and the ident_tokens array. These separate tokens are all
1253 { $$ = lookup_signed_typename (parse_language (pstate),
1254 parse_gdbarch (pstate),
1257 { $$ = lookup_signed_typename (parse_language (pstate),
1258 parse_gdbarch (pstate),
1261 { $$ = lookup_signed_typename (parse_language (pstate),
1262 parse_gdbarch (pstate),
1265 { $$ = lookup_signed_typename (parse_language (pstate),
1266 parse_gdbarch (pstate),
1268 | LONG SIGNED_KEYWORD INT_KEYWORD
1269 { $$ = lookup_signed_typename (parse_language (pstate),
1270 parse_gdbarch (pstate),
1272 | LONG SIGNED_KEYWORD
1273 { $$ = lookup_signed_typename (parse_language (pstate),
1274 parse_gdbarch (pstate),
1276 | SIGNED_KEYWORD LONG INT_KEYWORD
1277 { $$ = lookup_signed_typename (parse_language (pstate),
1278 parse_gdbarch (pstate),
1280 | UNSIGNED LONG INT_KEYWORD
1281 { $$ = lookup_unsigned_typename (parse_language (pstate),
1282 parse_gdbarch (pstate),
1284 | LONG UNSIGNED INT_KEYWORD
1285 { $$ = lookup_unsigned_typename (parse_language (pstate),
1286 parse_gdbarch (pstate),
1289 { $$ = lookup_unsigned_typename (parse_language (pstate),
1290 parse_gdbarch (pstate),
1293 { $$ = lookup_signed_typename (parse_language (pstate),
1294 parse_gdbarch (pstate),
1296 | LONG LONG INT_KEYWORD
1297 { $$ = lookup_signed_typename (parse_language (pstate),
1298 parse_gdbarch (pstate),
1300 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1301 { $$ = lookup_signed_typename (parse_language (pstate),
1302 parse_gdbarch (pstate),
1304 | LONG LONG SIGNED_KEYWORD
1305 { $$ = lookup_signed_typename (parse_language (pstate),
1306 parse_gdbarch (pstate),
1308 | SIGNED_KEYWORD LONG LONG
1309 { $$ = lookup_signed_typename (parse_language (pstate),
1310 parse_gdbarch (pstate),
1312 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1313 { $$ = lookup_signed_typename (parse_language (pstate),
1314 parse_gdbarch (pstate),
1316 | UNSIGNED LONG LONG
1317 { $$ = lookup_unsigned_typename (parse_language (pstate),
1318 parse_gdbarch (pstate),
1320 | UNSIGNED LONG LONG INT_KEYWORD
1321 { $$ = lookup_unsigned_typename (parse_language (pstate),
1322 parse_gdbarch (pstate),
1324 | LONG LONG UNSIGNED
1325 { $$ = lookup_unsigned_typename (parse_language (pstate),
1326 parse_gdbarch (pstate),
1328 | LONG LONG UNSIGNED INT_KEYWORD
1329 { $$ = lookup_unsigned_typename (parse_language (pstate),
1330 parse_gdbarch (pstate),
1333 { $$ = lookup_signed_typename (parse_language (pstate),
1334 parse_gdbarch (pstate),
1336 | SHORT SIGNED_KEYWORD INT_KEYWORD
1337 { $$ = lookup_signed_typename (parse_language (pstate),
1338 parse_gdbarch (pstate),
1340 | SHORT SIGNED_KEYWORD
1341 { $$ = lookup_signed_typename (parse_language (pstate),
1342 parse_gdbarch (pstate),
1344 | UNSIGNED SHORT INT_KEYWORD
1345 { $$ = lookup_unsigned_typename (parse_language (pstate),
1346 parse_gdbarch (pstate),
1349 { $$ = lookup_unsigned_typename (parse_language (pstate),
1350 parse_gdbarch (pstate),
1352 | SHORT UNSIGNED INT_KEYWORD
1353 { $$ = lookup_unsigned_typename (parse_language (pstate),
1354 parse_gdbarch (pstate),
1357 { $$ = lookup_typename (parse_language (pstate),
1358 parse_gdbarch (pstate),
1360 (struct block *) NULL,
1362 | LONG DOUBLE_KEYWORD
1363 { $$ = lookup_typename (parse_language (pstate),
1364 parse_gdbarch (pstate),
1366 (struct block *) NULL,
1369 { $$ = lookup_struct (copy_name ($2),
1370 expression_context_block); }
1373 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1376 | STRUCT name COMPLETE
1378 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1383 { $$ = lookup_struct (copy_name ($2),
1384 expression_context_block); }
1387 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1390 | CLASS name COMPLETE
1392 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1397 { $$ = lookup_union (copy_name ($2),
1398 expression_context_block); }
1401 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1404 | UNION name COMPLETE
1406 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1411 { $$ = lookup_enum (copy_name ($2),
1412 expression_context_block); }
1415 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1418 | ENUM name COMPLETE
1420 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1424 | UNSIGNED type_name
1425 { $$ = lookup_unsigned_typename (parse_language (pstate),
1426 parse_gdbarch (pstate),
1427 TYPE_NAME($2.type)); }
1429 { $$ = lookup_unsigned_typename (parse_language (pstate),
1430 parse_gdbarch (pstate),
1432 | SIGNED_KEYWORD type_name
1433 { $$ = lookup_signed_typename (parse_language (pstate),
1434 parse_gdbarch (pstate),
1435 TYPE_NAME($2.type)); }
1437 { $$ = lookup_signed_typename (parse_language (pstate),
1438 parse_gdbarch (pstate),
1440 /* It appears that this rule for templates is never
1441 reduced; template recognition happens by lookahead
1442 in the token processing code in yylex. */
1443 | TEMPLATE name '<' type '>'
1444 { $$ = lookup_template_type(copy_name($2), $4,
1445 expression_context_block);
1447 | const_or_volatile_or_space_identifier_noopt typebase
1448 { $$ = follow_types ($2); }
1449 | typebase const_or_volatile_or_space_identifier_noopt
1450 { $$ = follow_types ($1); }
1456 $$.stoken.ptr = "int";
1457 $$.stoken.length = 3;
1458 $$.type = lookup_signed_typename (parse_language (pstate),
1459 parse_gdbarch (pstate),
1464 $$.stoken.ptr = "long";
1465 $$.stoken.length = 4;
1466 $$.type = lookup_signed_typename (parse_language (pstate),
1467 parse_gdbarch (pstate),
1472 $$.stoken.ptr = "short";
1473 $$.stoken.length = 5;
1474 $$.type = lookup_signed_typename (parse_language (pstate),
1475 parse_gdbarch (pstate),
1482 { check_parameter_typelist ($1); }
1483 | nonempty_typelist ',' DOTDOTDOT
1485 $1->push_back (NULL);
1486 check_parameter_typelist ($1);
1494 std::vector<struct type *> *typelist
1495 = new std::vector<struct type *>;
1496 cpstate->type_lists.emplace_back (typelist);
1498 typelist->push_back ($1);
1501 | nonempty_typelist ',' type
1511 push_type_stack ($2);
1512 $$ = follow_types ($1);
1516 conversion_type_id: typebase conversion_declarator
1517 { $$ = follow_types ($1); }
1520 conversion_declarator: /* Nothing. */
1521 | ptr_operator conversion_declarator
1524 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1525 | VOLATILE_KEYWORD CONST_KEYWORD
1528 const_or_volatile_noopt: const_and_volatile
1529 { insert_type (tp_const);
1530 insert_type (tp_volatile);
1533 { insert_type (tp_const); }
1535 { insert_type (tp_volatile); }
1539 { $$ = operator_stoken (" new"); }
1541 { $$ = operator_stoken (" delete"); }
1542 | OPERATOR NEW '[' ']'
1543 { $$ = operator_stoken (" new[]"); }
1544 | OPERATOR DELETE '[' ']'
1545 { $$ = operator_stoken (" delete[]"); }
1546 | OPERATOR NEW OBJC_LBRAC ']'
1547 { $$ = operator_stoken (" new[]"); }
1548 | OPERATOR DELETE OBJC_LBRAC ']'
1549 { $$ = operator_stoken (" delete[]"); }
1551 { $$ = operator_stoken ("+"); }
1553 { $$ = operator_stoken ("-"); }
1555 { $$ = operator_stoken ("*"); }
1557 { $$ = operator_stoken ("/"); }
1559 { $$ = operator_stoken ("%"); }
1561 { $$ = operator_stoken ("^"); }
1563 { $$ = operator_stoken ("&"); }
1565 { $$ = operator_stoken ("|"); }
1567 { $$ = operator_stoken ("~"); }
1569 { $$ = operator_stoken ("!"); }
1571 { $$ = operator_stoken ("="); }
1573 { $$ = operator_stoken ("<"); }
1575 { $$ = operator_stoken (">"); }
1576 | OPERATOR ASSIGN_MODIFY
1577 { const char *op = " unknown";
1601 case BINOP_BITWISE_IOR:
1604 case BINOP_BITWISE_AND:
1607 case BINOP_BITWISE_XOR:
1614 $$ = operator_stoken (op);
1617 { $$ = operator_stoken ("<<"); }
1619 { $$ = operator_stoken (">>"); }
1621 { $$ = operator_stoken ("=="); }
1623 { $$ = operator_stoken ("!="); }
1625 { $$ = operator_stoken ("<="); }
1627 { $$ = operator_stoken (">="); }
1629 { $$ = operator_stoken ("&&"); }
1631 { $$ = operator_stoken ("||"); }
1632 | OPERATOR INCREMENT
1633 { $$ = operator_stoken ("++"); }
1634 | OPERATOR DECREMENT
1635 { $$ = operator_stoken ("--"); }
1637 { $$ = operator_stoken (","); }
1638 | OPERATOR ARROW_STAR
1639 { $$ = operator_stoken ("->*"); }
1641 { $$ = operator_stoken ("->"); }
1643 { $$ = operator_stoken ("()"); }
1645 { $$ = operator_stoken ("[]"); }
1646 | OPERATOR OBJC_LBRAC ']'
1647 { $$ = operator_stoken ("[]"); }
1648 | OPERATOR conversion_type_id
1651 c_print_type ($2, NULL, &buf, -1, 0,
1652 &type_print_raw_options);
1654 /* This also needs canonicalization. */
1656 = cp_canonicalize_string (buf.c_str ());
1658 canon = std::move (buf.string ());
1659 $$ = operator_stoken ((" " + canon).c_str ());
1663 /* This rule exists in order to allow some tokens that would not normally
1664 match the 'name' rule to appear as fields within a struct. The example
1665 that initially motivated this was the RISC-V target which models the
1666 floating point registers as a union with fields called 'float' and
1667 'double'. The 'float' string becomes a TYPENAME token and can appear
1668 anywhere a 'name' can, however 'double' is its own token,
1669 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
1672 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1673 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1674 | LONG { $$ = typename_stoken ("long"); }
1675 | SHORT { $$ = typename_stoken ("short"); }
1676 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1677 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1680 name : NAME { $$ = $1.stoken; }
1681 | BLOCKNAME { $$ = $1.stoken; }
1682 | TYPENAME { $$ = $1.stoken; }
1683 | NAME_OR_INT { $$ = $1.stoken; }
1684 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1688 name_not_typename : NAME
1690 /* These would be useful if name_not_typename was useful, but it is just
1691 a fake for "variable", so these cause reduce/reduce conflicts because
1692 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1693 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1694 context where only a name could occur, this might be useful.
1699 struct field_of_this_result is_a_field_of_this;
1702 $$.sym = lookup_symbol ($1.ptr,
1703 expression_context_block,
1705 &is_a_field_of_this);
1706 $$.is_a_field_of_this
1707 = is_a_field_of_this.type != NULL;
1714 /* Like write_exp_string, but prepends a '~'. */
1717 write_destructor_name (struct parser_state *par_state, struct stoken token)
1719 char *copy = (char *) alloca (token.length + 1);
1722 memcpy (©[1], token.ptr, token.length);
1727 write_exp_string (par_state, token);
1730 /* Returns a stoken of the operator name given by OP (which does not
1731 include the string "operator"). */
1733 static struct stoken
1734 operator_stoken (const char *op)
1736 struct stoken st = { NULL, 0 };
1739 st.length = CP_OPERATOR_LEN + strlen (op);
1740 buf = (char *) malloc (st.length + 1);
1741 strcpy (buf, CP_OPERATOR_STR);
1745 /* The toplevel (c_parse) will free the memory allocated here. */
1746 make_cleanup (free, buf);
1750 /* Returns a stoken of the type named TYPE. */
1752 static struct stoken
1753 typename_stoken (const char *type)
1755 struct stoken st = { type, 0 };
1756 st.length = strlen (type);
1760 /* Return true if the type is aggregate-like. */
1763 type_aggregate_p (struct type *type)
1765 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1766 || TYPE_CODE (type) == TYPE_CODE_UNION
1767 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1768 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1769 && TYPE_DECLARED_CLASS (type)));
1772 /* Validate a parameter typelist. */
1775 check_parameter_typelist (std::vector<struct type *> *params)
1780 for (ix = 0; ix < params->size (); ++ix)
1782 type = (*params)[ix];
1783 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1787 if (params->size () == 1)
1792 error (_("parameter types following 'void'"));
1795 error (_("'void' invalid as parameter type"));
1800 /* Take care of parsing a number (anything that starts with a digit).
1801 Set yylval and return the token type; update lexptr.
1802 LEN is the number of characters in it. */
1804 /*** Needs some error checking for the float case ***/
1807 parse_number (struct parser_state *par_state,
1808 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1816 int base = input_radix;
1819 /* Number of "L" suffixes encountered. */
1822 /* We have found a "L" or "U" suffix. */
1823 int found_suffix = 0;
1826 struct type *signed_type;
1827 struct type *unsigned_type;
1830 p = (char *) alloca (len);
1831 memcpy (p, buf, len);
1835 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1836 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1838 putithere->typed_val_float.type
1839 = parse_type (par_state)->builtin_decfloat;
1842 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1844 putithere->typed_val_float.type
1845 = parse_type (par_state)->builtin_decdouble;
1848 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1850 putithere->typed_val_float.type
1851 = parse_type (par_state)->builtin_declong;
1854 /* Handle suffixes: 'f' for float, 'l' for long double. */
1855 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1857 putithere->typed_val_float.type
1858 = parse_type (par_state)->builtin_float;
1861 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1863 putithere->typed_val_float.type
1864 = parse_type (par_state)->builtin_long_double;
1867 /* Default type for floating-point literals is double. */
1870 putithere->typed_val_float.type
1871 = parse_type (par_state)->builtin_double;
1874 if (!parse_float (p, len,
1875 putithere->typed_val_float.type,
1876 putithere->typed_val_float.val))
1881 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1882 if (p[0] == '0' && len > 1)
1925 if (c >= 'A' && c <= 'Z')
1927 if (c != 'l' && c != 'u')
1929 if (c >= '0' && c <= '9')
1937 if (base > 10 && c >= 'a' && c <= 'f')
1941 n += i = c - 'a' + 10;
1954 return ERROR; /* Char not a digit */
1957 return ERROR; /* Invalid digit in this base */
1959 /* Portably test for overflow (only works for nonzero values, so make
1960 a second check for zero). FIXME: Can't we just make n and prevn
1961 unsigned and avoid this? */
1962 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1963 unsigned_p = 1; /* Try something unsigned */
1965 /* Portably test for unsigned overflow.
1966 FIXME: This check is wrong; for example it doesn't find overflow
1967 on 0x123456789 when LONGEST is 32 bits. */
1968 if (c != 'l' && c != 'u' && n != 0)
1970 if (unsigned_p && prevn >= n)
1971 error (_("Numeric constant too large."));
1976 /* An integer constant is an int, a long, or a long long. An L
1977 suffix forces it to be long; an LL suffix forces it to be long
1978 long. If not forced to a larger size, it gets the first type of
1979 the above that it fits in. To figure out whether it fits, we
1980 shift it right and see whether anything remains. Note that we
1981 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1982 operation, because many compilers will warn about such a shift
1983 (which always produces a zero result). Sometimes gdbarch_int_bit
1984 or gdbarch_long_bit will be that big, sometimes not. To deal with
1985 the case where it is we just always shift the value more than
1986 once, with fewer bits each time. */
1990 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
1993 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
1995 /* A large decimal (not hex or octal) constant (between INT_MAX
1996 and UINT_MAX) is a long or unsigned long, according to ANSI,
1997 never an unsigned int, but this code treats it as unsigned
1998 int. This probably should be fixed. GCC gives a warning on
2001 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2002 signed_type = parse_type (par_state)->builtin_int;
2004 else if (long_p <= 1
2005 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
2008 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
2009 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2010 signed_type = parse_type (par_state)->builtin_long;
2015 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2016 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
2017 /* A long long does not fit in a LONGEST. */
2018 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2020 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
2021 high_bit = (ULONGEST) 1 << shift;
2022 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2023 signed_type = parse_type (par_state)->builtin_long_long;
2026 putithere->typed_val_int.val = n;
2028 /* If the high bit of the worked out type is set then this number
2029 has to be unsigned. */
2031 if (unsigned_p || (n & high_bit))
2033 putithere->typed_val_int.type = unsigned_type;
2037 putithere->typed_val_int.type = signed_type;
2043 /* Temporary obstack used for holding strings. */
2044 static struct obstack tempbuf;
2045 static int tempbuf_init;
2047 /* Parse a C escape sequence. The initial backslash of the sequence
2048 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2049 last character of the sequence. If OUTPUT is not NULL, the
2050 translated form of the escape sequence will be written there. If
2051 OUTPUT is NULL, no output is written and the call will only affect
2052 *PTR. If an escape sequence is expressed in target bytes, then the
2053 entire sequence will simply be copied to OUTPUT. Return 1 if any
2054 character was emitted, 0 otherwise. */
2057 c_parse_escape (const char **ptr, struct obstack *output)
2059 const char *tokptr = *ptr;
2062 /* Some escape sequences undergo character set conversion. Those we
2066 /* Hex escapes do not undergo character set conversion, so keep
2067 the escape sequence for later. */
2070 obstack_grow_str (output, "\\x");
2072 if (!ISXDIGIT (*tokptr))
2073 error (_("\\x escape without a following hex digit"));
2074 while (ISXDIGIT (*tokptr))
2077 obstack_1grow (output, *tokptr);
2082 /* Octal escapes do not undergo character set conversion, so
2083 keep the escape sequence for later. */
2095 obstack_grow_str (output, "\\");
2097 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2101 obstack_1grow (output, *tokptr);
2107 /* We handle UCNs later. We could handle them here, but that
2108 would mean a spurious error in the case where the UCN could
2109 be converted to the target charset but not the host
2115 int i, len = c == 'U' ? 8 : 4;
2118 obstack_1grow (output, '\\');
2119 obstack_1grow (output, *tokptr);
2122 if (!ISXDIGIT (*tokptr))
2123 error (_("\\%c escape without a following hex digit"), c);
2124 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2127 obstack_1grow (output, *tokptr);
2133 /* We must pass backslash through so that it does not
2134 cause quoting during the second expansion. */
2137 obstack_grow_str (output, "\\\\");
2141 /* Escapes which undergo conversion. */
2144 obstack_1grow (output, '\a');
2149 obstack_1grow (output, '\b');
2154 obstack_1grow (output, '\f');
2159 obstack_1grow (output, '\n');
2164 obstack_1grow (output, '\r');
2169 obstack_1grow (output, '\t');
2174 obstack_1grow (output, '\v');
2178 /* GCC extension. */
2181 obstack_1grow (output, HOST_ESCAPE_CHAR);
2185 /* Backslash-newline expands to nothing at all. */
2191 /* A few escapes just expand to the character itself. */
2195 /* GCC extensions. */
2200 /* Unrecognized escapes turn into the character itself. */
2203 obstack_1grow (output, *tokptr);
2211 /* Parse a string or character literal from TOKPTR. The string or
2212 character may be wide or unicode. *OUTPTR is set to just after the
2213 end of the literal in the input string. The resulting token is
2214 stored in VALUE. This returns a token value, either STRING or
2215 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2216 number of host characters in the literal. */
2219 parse_string_or_char (const char *tokptr, const char **outptr,
2220 struct typed_stoken *value, int *host_chars)
2226 /* Build the gdb internal form of the input string in tempbuf. Note
2227 that the buffer is null byte terminated *only* for the
2228 convenience of debugging gdb itself and printing the buffer
2229 contents when the buffer contains no embedded nulls. Gdb does
2230 not depend upon the buffer being null byte terminated, it uses
2231 the length string instead. This allows gdb to handle C strings
2232 (as well as strings in other languages) with embedded null
2238 obstack_free (&tempbuf, NULL);
2239 obstack_init (&tempbuf);
2241 /* Record the string type. */
2244 type = C_WIDE_STRING;
2247 else if (*tokptr == 'u')
2252 else if (*tokptr == 'U')
2257 else if (*tokptr == '@')
2259 /* An Objective C string. */
2267 /* Skip the quote. */
2281 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2283 else if (c == quote)
2287 obstack_1grow (&tempbuf, c);
2289 /* FIXME: this does the wrong thing with multi-byte host
2290 characters. We could use mbrlen here, but that would
2291 make "set host-charset" a bit less useful. */
2296 if (*tokptr != quote)
2299 error (_("Unterminated string in expression."));
2301 error (_("Unmatched single quote."));
2306 value->ptr = (char *) obstack_base (&tempbuf);
2307 value->length = obstack_object_size (&tempbuf);
2311 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2314 /* This is used to associate some attributes with a token. */
2318 /* If this bit is set, the token is C++-only. */
2322 /* If this bit is set, the token is conditional: if there is a
2323 symbol of the same name, then the token is a symbol; otherwise,
2324 the token is a keyword. */
2328 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2334 enum exp_opcode opcode;
2338 static const struct token tokentab3[] =
2340 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2341 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2342 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2343 {"...", DOTDOTDOT, BINOP_END, 0}
2346 static const struct token tokentab2[] =
2348 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2349 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2350 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2351 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2352 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2353 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2354 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2355 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2356 {"++", INCREMENT, BINOP_END, 0},
2357 {"--", DECREMENT, BINOP_END, 0},
2358 {"->", ARROW, BINOP_END, 0},
2359 {"&&", ANDAND, BINOP_END, 0},
2360 {"||", OROR, BINOP_END, 0},
2361 /* "::" is *not* only C++: gdb overrides its meaning in several
2362 different ways, e.g., 'filename'::func, function::variable. */
2363 {"::", COLONCOLON, BINOP_END, 0},
2364 {"<<", LSH, BINOP_END, 0},
2365 {">>", RSH, BINOP_END, 0},
2366 {"==", EQUAL, BINOP_END, 0},
2367 {"!=", NOTEQUAL, BINOP_END, 0},
2368 {"<=", LEQ, BINOP_END, 0},
2369 {">=", GEQ, BINOP_END, 0},
2370 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2373 /* Identifier-like tokens. Only type-specifiers than can appear in
2374 multi-word type names (for example 'double' can appear in 'long
2375 double') need to be listed here. type-specifiers that are only ever
2376 single word (like 'float') are handled by the classify_name function. */
2377 static const struct token ident_tokens[] =
2379 {"unsigned", UNSIGNED, OP_NULL, 0},
2380 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2381 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2382 {"struct", STRUCT, OP_NULL, 0},
2383 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2384 {"sizeof", SIZEOF, OP_NULL, 0},
2385 {"_Alignof", ALIGNOF, OP_NULL, 0},
2386 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2387 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2388 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2389 {"class", CLASS, OP_NULL, FLAG_CXX},
2390 {"union", UNION, OP_NULL, 0},
2391 {"short", SHORT, OP_NULL, 0},
2392 {"const", CONST_KEYWORD, OP_NULL, 0},
2393 {"enum", ENUM, OP_NULL, 0},
2394 {"long", LONG, OP_NULL, 0},
2395 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2396 {"int", INT_KEYWORD, OP_NULL, 0},
2397 {"new", NEW, OP_NULL, FLAG_CXX},
2398 {"delete", DELETE, OP_NULL, FLAG_CXX},
2399 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2401 {"and", ANDAND, BINOP_END, FLAG_CXX},
2402 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2403 {"bitand", '&', OP_NULL, FLAG_CXX},
2404 {"bitor", '|', OP_NULL, FLAG_CXX},
2405 {"compl", '~', OP_NULL, FLAG_CXX},
2406 {"not", '!', OP_NULL, FLAG_CXX},
2407 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2408 {"or", OROR, BINOP_END, FLAG_CXX},
2409 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2410 {"xor", '^', OP_NULL, FLAG_CXX},
2411 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2413 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2414 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2415 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2416 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2418 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2419 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2420 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2421 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2422 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2424 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2427 /* When we find that lexptr (the global var defined in parse.c) is
2428 pointing at a macro invocation, we expand the invocation, and call
2429 scan_macro_expansion to save the old lexptr here and point lexptr
2430 into the expanded text. When we reach the end of that, we call
2431 end_macro_expansion to pop back to the value we saved here. The
2432 macro expansion code promises to return only fully-expanded text,
2433 so we don't need to "push" more than one level.
2435 This is disgusting, of course. It would be cleaner to do all macro
2436 expansion beforehand, and then hand that to lexptr. But we don't
2437 really know where the expression ends. Remember, in a command like
2439 (gdb) break *ADDRESS if CONDITION
2441 we evaluate ADDRESS in the scope of the current frame, but we
2442 evaluate CONDITION in the scope of the breakpoint's location. So
2443 it's simply wrong to try to macro-expand the whole thing at once. */
2444 static const char *macro_original_text;
2446 /* We save all intermediate macro expansions on this obstack for the
2447 duration of a single parse. The expansion text may sometimes have
2448 to live past the end of the expansion, due to yacc lookahead.
2449 Rather than try to be clever about saving the data for a single
2450 token, we simply keep it all and delete it after parsing has
2452 static struct obstack expansion_obstack;
2455 scan_macro_expansion (char *expansion)
2459 /* We'd better not be trying to push the stack twice. */
2460 gdb_assert (! macro_original_text);
2462 /* Copy to the obstack, and then free the intermediate
2464 copy = (char *) obstack_copy0 (&expansion_obstack, expansion,
2465 strlen (expansion));
2468 /* Save the old lexptr value, so we can return to it when we're done
2469 parsing the expanded text. */
2470 macro_original_text = lexptr;
2475 scanning_macro_expansion (void)
2477 return macro_original_text != 0;
2481 finished_macro_expansion (void)
2483 /* There'd better be something to pop back to. */
2484 gdb_assert (macro_original_text);
2486 /* Pop back to the original text. */
2487 lexptr = macro_original_text;
2488 macro_original_text = 0;
2492 scan_macro_cleanup (void *dummy)
2494 if (macro_original_text)
2495 finished_macro_expansion ();
2497 obstack_free (&expansion_obstack, NULL);
2500 /* Return true iff the token represents a C++ cast operator. */
2503 is_cast_operator (const char *token, int len)
2505 return (! strncmp (token, "dynamic_cast", len)
2506 || ! strncmp (token, "static_cast", len)
2507 || ! strncmp (token, "reinterpret_cast", len)
2508 || ! strncmp (token, "const_cast", len));
2511 /* The scope used for macro expansion. */
2512 static struct macro_scope *expression_macro_scope;
2514 /* This is set if a NAME token appeared at the very end of the input
2515 string, with no whitespace separating the name from the EOF. This
2516 is used only when parsing to do field name completion. */
2517 static int saw_name_at_eof;
2519 /* This is set if the previously-returned token was a structure
2520 operator -- either '.' or ARROW. */
2521 static bool last_was_structop;
2523 /* Read one token, getting characters through lexptr. */
2526 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2531 const char *tokstart;
2532 bool saw_structop = last_was_structop;
2535 last_was_structop = false;
2536 *is_quoted_name = false;
2540 /* Check if this is a macro invocation that we need to expand. */
2541 if (! scanning_macro_expansion ())
2543 char *expanded = macro_expand_next (&lexptr,
2544 standard_macro_lookup,
2545 expression_macro_scope);
2548 scan_macro_expansion (expanded);
2551 prev_lexptr = lexptr;
2554 /* See if it is a special token of length 3. */
2555 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2556 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2558 if ((tokentab3[i].flags & FLAG_CXX) != 0
2559 && parse_language (par_state)->la_language != language_cplus)
2563 yylval.opcode = tokentab3[i].opcode;
2564 return tokentab3[i].token;
2567 /* See if it is a special token of length 2. */
2568 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2569 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2571 if ((tokentab2[i].flags & FLAG_CXX) != 0
2572 && parse_language (par_state)->la_language != language_cplus)
2576 yylval.opcode = tokentab2[i].opcode;
2577 if (tokentab2[i].token == ARROW)
2578 last_was_structop = 1;
2579 return tokentab2[i].token;
2582 switch (c = *tokstart)
2585 /* If we were just scanning the result of a macro expansion,
2586 then we need to resume scanning the original text.
2587 If we're parsing for field name completion, and the previous
2588 token allows such completion, return a COMPLETE token.
2589 Otherwise, we were already scanning the original text, and
2590 we're really done. */
2591 if (scanning_macro_expansion ())
2593 finished_macro_expansion ();
2596 else if (saw_name_at_eof)
2598 saw_name_at_eof = 0;
2601 else if (parse_completion && saw_structop)
2616 if (parse_language (par_state)->la_language == language_objc
2623 if (paren_depth == 0)
2630 if (comma_terminates
2632 && ! scanning_macro_expansion ())
2638 /* Might be a floating point number. */
2639 if (lexptr[1] < '0' || lexptr[1] > '9')
2641 last_was_structop = true;
2642 goto symbol; /* Nope, must be a symbol. */
2657 /* It's a number. */
2658 int got_dot = 0, got_e = 0, toktype;
2659 const char *p = tokstart;
2660 int hex = input_radix > 10;
2662 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2667 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2675 /* This test includes !hex because 'e' is a valid hex digit
2676 and thus does not indicate a floating point number when
2677 the radix is hex. */
2678 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2679 got_dot = got_e = 1;
2680 /* This test does not include !hex, because a '.' always indicates
2681 a decimal floating point number regardless of the radix. */
2682 else if (!got_dot && *p == '.')
2684 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2685 && (*p == '-' || *p == '+'))
2686 /* This is the sign of the exponent, not the end of the
2689 /* We will take any letters or digits. parse_number will
2690 complain if past the radix, or if L or U are not final. */
2691 else if ((*p < '0' || *p > '9')
2692 && ((*p < 'a' || *p > 'z')
2693 && (*p < 'A' || *p > 'Z')))
2696 toktype = parse_number (par_state, tokstart, p - tokstart,
2697 got_dot|got_e, &yylval);
2698 if (toktype == ERROR)
2700 char *err_copy = (char *) alloca (p - tokstart + 1);
2702 memcpy (err_copy, tokstart, p - tokstart);
2703 err_copy[p - tokstart] = 0;
2704 error (_("Invalid number \"%s\"."), err_copy);
2712 const char *p = &tokstart[1];
2714 if (parse_language (par_state)->la_language == language_objc)
2716 size_t len = strlen ("selector");
2718 if (strncmp (p, "selector", len) == 0
2719 && (p[len] == '\0' || ISSPACE (p[len])))
2728 while (ISSPACE (*p))
2730 size_t len = strlen ("entry");
2731 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2763 if (tokstart[1] != '"' && tokstart[1] != '\'')
2772 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2777 error (_("Empty character constant."));
2778 else if (host_len > 2 && c == '\'')
2781 namelen = lexptr - tokstart - 1;
2782 *is_quoted_name = true;
2786 else if (host_len > 1)
2787 error (_("Invalid character constant."));
2793 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2794 /* We must have come across a bad character (e.g. ';'). */
2795 error (_("Invalid character '%c' in expression."), c);
2797 /* It's a name. See how long it is. */
2799 for (c = tokstart[namelen];
2800 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2802 /* Template parameter lists are part of the name.
2803 FIXME: This mishandles `print $a<4&&$a>3'. */
2807 if (! is_cast_operator (tokstart, namelen))
2809 /* Scan ahead to get rest of the template specification. Note
2810 that we look ahead only when the '<' adjoins non-whitespace
2811 characters; for comparison expressions, e.g. "a < b > c",
2812 there must be spaces before the '<', etc. */
2813 const char *p = find_template_name_end (tokstart + namelen);
2816 namelen = p - tokstart;
2820 c = tokstart[++namelen];
2823 /* The token "if" terminates the expression and is NOT removed from
2824 the input stream. It doesn't count if it appears in the
2825 expansion of a macro. */
2827 && tokstart[0] == 'i'
2828 && tokstart[1] == 'f'
2829 && ! scanning_macro_expansion ())
2834 /* For the same reason (breakpoint conditions), "thread N"
2835 terminates the expression. "thread" could be an identifier, but
2836 an identifier is never followed by a number without intervening
2837 punctuation. "task" is similar. Handle abbreviations of these,
2838 similarly to breakpoint.c:find_condition_and_thread. */
2840 && (strncmp (tokstart, "thread", namelen) == 0
2841 || strncmp (tokstart, "task", namelen) == 0)
2842 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2843 && ! scanning_macro_expansion ())
2845 const char *p = tokstart + namelen + 1;
2847 while (*p == ' ' || *p == '\t')
2849 if (*p >= '0' && *p <= '9')
2857 yylval.sval.ptr = tokstart;
2858 yylval.sval.length = namelen;
2860 /* Catch specific keywords. */
2861 copy = copy_name (yylval.sval);
2862 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2863 if (strcmp (copy, ident_tokens[i].oper) == 0)
2865 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2866 && parse_language (par_state)->la_language != language_cplus)
2869 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2871 struct field_of_this_result is_a_field_of_this;
2873 if (lookup_symbol (copy, expression_context_block,
2875 (parse_language (par_state)->la_language
2876 == language_cplus ? &is_a_field_of_this
2880 /* The keyword is shadowed. */
2885 /* It is ok to always set this, even though we don't always
2886 strictly need to. */
2887 yylval.opcode = ident_tokens[i].opcode;
2888 return ident_tokens[i].token;
2891 if (*tokstart == '$')
2894 if (parse_completion && *lexptr == '\0')
2895 saw_name_at_eof = 1;
2897 yylval.ssym.stoken = yylval.sval;
2898 yylval.ssym.sym.symbol = NULL;
2899 yylval.ssym.sym.block = NULL;
2900 yylval.ssym.is_a_field_of_this = 0;
2904 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2905 struct token_and_value
2911 /* A FIFO of tokens that have been read but not yet returned to the
2913 static std::vector<token_and_value> token_fifo;
2915 /* Non-zero if the lexer should return tokens from the FIFO. */
2918 /* Temporary storage for c_lex; this holds symbol names as they are
2920 auto_obstack name_obstack;
2922 /* Classify a NAME token. The contents of the token are in `yylval'.
2923 Updates yylval and returns the new token type. BLOCK is the block
2924 in which lookups start; this can be NULL to mean the global scope.
2925 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2926 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2927 a structure operator -- either '.' or ARROW */
2930 classify_name (struct parser_state *par_state, const struct block *block,
2931 bool is_quoted_name, bool is_after_structop)
2933 struct block_symbol bsym;
2935 struct field_of_this_result is_a_field_of_this;
2937 copy = copy_name (yylval.sval);
2939 /* Initialize this in case we *don't* use it in this call; that way
2940 we can refer to it unconditionally below. */
2941 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2943 bsym = lookup_symbol (copy, block, VAR_DOMAIN,
2944 parse_language (par_state)->la_name_of_this
2945 ? &is_a_field_of_this : NULL);
2947 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
2949 yylval.ssym.sym = bsym;
2950 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2953 else if (!bsym.symbol)
2955 /* If we found a field of 'this', we might have erroneously
2956 found a constructor where we wanted a type name. Handle this
2957 case by noticing that we found a constructor and then look up
2958 the type tag instead. */
2959 if (is_a_field_of_this.type != NULL
2960 && is_a_field_of_this.fn_field != NULL
2961 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2964 struct field_of_this_result inner_is_a_field_of_this;
2966 bsym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2967 &inner_is_a_field_of_this);
2968 if (bsym.symbol != NULL)
2970 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
2975 /* If we found a field on the "this" object, or we are looking
2976 up a field on a struct, then we want to prefer it over a
2977 filename. However, if the name was quoted, then it is better
2978 to check for a filename or a block, since this is the only
2979 way the user has of requiring the extension to be used. */
2980 if ((is_a_field_of_this.type == NULL && !is_after_structop)
2983 /* See if it's a file name. */
2984 struct symtab *symtab;
2986 symtab = lookup_symtab (copy);
2989 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
2996 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
2998 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3002 /* See if it's an ObjC classname. */
3003 if (parse_language (par_state)->la_language == language_objc && !bsym.symbol)
3005 CORE_ADDR Class = lookup_objc_class (parse_gdbarch (par_state), copy);
3010 yylval.theclass.theclass = Class;
3011 sym = lookup_struct_typedef (copy, expression_context_block, 1);
3013 yylval.theclass.type = SYMBOL_TYPE (sym);
3018 /* Input names that aren't symbols but ARE valid hex numbers, when
3019 the input radix permits them, can be names or numbers depending
3020 on the parse. Note we support radixes > 16 here. */
3022 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3023 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3025 YYSTYPE newlval; /* Its value is ignored. */
3026 int hextype = parse_number (par_state, copy, yylval.sval.length,
3031 yylval.ssym.sym = bsym;
3032 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3037 /* Any other kind of symbol */
3038 yylval.ssym.sym = bsym;
3039 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3041 if (bsym.symbol == NULL
3042 && parse_language (par_state)->la_language == language_cplus
3043 && is_a_field_of_this.type == NULL
3044 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
3045 return UNKNOWN_CPP_NAME;
3050 /* Like classify_name, but used by the inner loop of the lexer, when a
3051 name might have already been seen. CONTEXT is the context type, or
3052 NULL if this is the first component of a name. */
3055 classify_inner_name (struct parser_state *par_state,
3056 const struct block *block, struct type *context)
3061 if (context == NULL)
3062 return classify_name (par_state, block, false, false);
3064 type = check_typedef (context);
3065 if (!type_aggregate_p (type))
3068 copy = copy_name (yylval.ssym.stoken);
3069 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3070 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block, VAR_DOMAIN);
3072 /* If no symbol was found, search for a matching base class named
3073 COPY. This will allow users to enter qualified names of class members
3074 relative to the `this' pointer. */
3075 if (yylval.ssym.sym.symbol == NULL)
3077 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3079 if (base_type != NULL)
3081 yylval.tsym.type = base_type;
3088 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3092 /* cp_lookup_nested_symbol might have accidentally found a constructor
3093 named COPY when we really wanted a base class of the same name.
3094 Double-check this case by looking for a base class. */
3096 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
3098 if (base_type != NULL)
3100 yylval.tsym.type = base_type;
3107 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3113 internal_error (__FILE__, __LINE__, _("not reached"));
3116 /* The outer level of a two-level lexer. This calls the inner lexer
3117 to return tokens. It then either returns these tokens, or
3118 aggregates them into a larger token. This lets us work around a
3119 problem in our parsing approach, where the parser could not
3120 distinguish between qualified names and qualified types at the
3123 This approach is still not ideal, because it mishandles template
3124 types. See the comment in lex_one_token for an example. However,
3125 this is still an improvement over the earlier approach, and will
3126 suffice until we move to better parsing technology. */
3131 token_and_value current;
3132 int first_was_coloncolon, last_was_coloncolon;
3133 struct type *context_type = NULL;
3134 int last_to_examine, next_to_examine, checkpoint;
3135 const struct block *search_block;
3136 bool is_quoted_name, last_lex_was_structop;
3138 if (popping && !token_fifo.empty ())
3142 last_lex_was_structop = last_was_structop;
3144 /* Read the first token and decide what to do. Most of the
3145 subsequent code is C++-only; but also depends on seeing a "::" or
3147 current.token = lex_one_token (pstate, &is_quoted_name);
3148 if (current.token == NAME)
3149 current.token = classify_name (pstate, expression_context_block,
3150 is_quoted_name, last_lex_was_structop);
3151 if (parse_language (pstate)->la_language != language_cplus
3152 || (current.token != TYPENAME && current.token != COLONCOLON
3153 && current.token != FILENAME))
3154 return current.token;
3156 /* Read any sequence of alternating "::" and name-like tokens into
3158 current.value = yylval;
3159 token_fifo.push_back (current);
3160 last_was_coloncolon = current.token == COLONCOLON;
3165 /* We ignore quoted names other than the very first one.
3166 Subsequent ones do not have any special meaning. */
3167 current.token = lex_one_token (pstate, &ignore);
3168 current.value = yylval;
3169 token_fifo.push_back (current);
3171 if ((last_was_coloncolon && current.token != NAME)
3172 || (!last_was_coloncolon && current.token != COLONCOLON))
3174 last_was_coloncolon = !last_was_coloncolon;
3178 /* We always read one extra token, so compute the number of tokens
3179 to examine accordingly. */
3180 last_to_examine = token_fifo.size () - 2;
3181 next_to_examine = 0;
3183 current = token_fifo[next_to_examine];
3186 name_obstack.clear ();
3188 if (current.token == FILENAME)
3189 search_block = current.value.bval;
3190 else if (current.token == COLONCOLON)
3191 search_block = NULL;
3194 gdb_assert (current.token == TYPENAME);
3195 search_block = expression_context_block;
3196 obstack_grow (&name_obstack, current.value.sval.ptr,
3197 current.value.sval.length);
3198 context_type = current.value.tsym.type;
3202 first_was_coloncolon = current.token == COLONCOLON;
3203 last_was_coloncolon = first_was_coloncolon;
3205 while (next_to_examine <= last_to_examine)
3207 token_and_value next;
3209 next = token_fifo[next_to_examine];
3212 if (next.token == NAME && last_was_coloncolon)
3216 yylval = next.value;
3217 classification = classify_inner_name (pstate, search_block,
3219 /* We keep going until we either run out of names, or until
3220 we have a qualified name which is not a type. */
3221 if (classification != TYPENAME && classification != NAME)
3224 /* Accept up to this token. */
3225 checkpoint = next_to_examine;
3227 /* Update the partial name we are constructing. */
3228 if (context_type != NULL)
3230 /* We don't want to put a leading "::" into the name. */
3231 obstack_grow_str (&name_obstack, "::");
3233 obstack_grow (&name_obstack, next.value.sval.ptr,
3234 next.value.sval.length);
3236 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3237 yylval.sval.length = obstack_object_size (&name_obstack);
3238 current.value = yylval;
3239 current.token = classification;
3241 last_was_coloncolon = 0;
3243 if (classification == NAME)
3246 context_type = yylval.tsym.type;
3248 else if (next.token == COLONCOLON && !last_was_coloncolon)
3249 last_was_coloncolon = 1;
3252 /* We've reached the end of the name. */
3257 /* If we have a replacement token, install it as the first token in
3258 the FIFO, and delete the other constituent tokens. */
3261 current.value.sval.ptr
3262 = (const char *) obstack_copy0 (&expansion_obstack,
3263 current.value.sval.ptr,
3264 current.value.sval.length);
3266 token_fifo[0] = current;
3268 token_fifo.erase (token_fifo.begin () + 1,
3269 token_fifo.begin () + checkpoint);
3273 current = token_fifo[0];
3274 token_fifo.erase (token_fifo.begin ());
3275 yylval = current.value;
3276 return current.token;
3280 c_parse (struct parser_state *par_state)
3283 struct cleanup *back_to;
3285 /* Setting up the parser state. */
3286 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3287 gdb_assert (par_state != NULL);
3290 c_parse_state cstate;
3291 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3293 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3295 if (expression_context_block)
3296 macro_scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3298 macro_scope = default_macro_scope ();
3300 macro_scope = user_macro_scope ();
3302 scoped_restore restore_macro_scope
3303 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3305 /* Initialize macro expansion code. */
3306 obstack_init (&expansion_obstack);
3307 gdb_assert (! macro_original_text);
3308 /* Note that parsing (within yyparse) freely installs cleanups
3309 assuming they'll be run here (below). */
3310 back_to = make_cleanup (scan_macro_cleanup, 0);
3312 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3315 /* Initialize some state used by the lexer. */
3316 last_was_structop = false;
3317 saw_name_at_eof = 0;
3319 token_fifo.clear ();
3321 name_obstack.clear ();
3323 result = yyparse ();
3324 do_cleanups (back_to);
3331 /* This is called via the YYPRINT macro when parser debugging is
3332 enabled. It prints a token's value. */
3335 c_print_token (FILE *file, int type, YYSTYPE value)
3340 parser_fprintf (file, "typed_val_int<%s, %s>",
3341 TYPE_SAFE_NAME (value.typed_val_int.type),
3342 pulongest (value.typed_val_int.val));
3348 char *copy = (char *) alloca (value.tsval.length + 1);
3350 memcpy (copy, value.tsval.ptr, value.tsval.length);
3351 copy[value.tsval.length] = '\0';
3353 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3359 parser_fprintf (file, "sval<%s>", copy_name (value.sval));
3363 parser_fprintf (file, "tsym<type=%s, name=%s>",
3364 TYPE_SAFE_NAME (value.tsym.type),
3365 copy_name (value.tsym.stoken));
3369 case UNKNOWN_CPP_NAME:
3372 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3373 copy_name (value.ssym.stoken),
3374 (value.ssym.sym.symbol == NULL
3375 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym.symbol)),
3376 value.ssym.is_a_field_of_this);
3380 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3388 yyerror (const char *msg)
3391 lexptr = prev_lexptr;
3393 error (_("A %s in expression, near `%s'."), msg, lexptr);