1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Parse a C expression from text in a string,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result.
28 Note that malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
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"
56 #include "type-stack.h"
57 #include "target-float.h"
60 #define parse_type(ps) builtin_type (ps->gdbarch ())
62 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
64 #define GDB_YY_REMAP_PREFIX c_
67 /* The state of the parser, used internally when we are parsing the
70 static struct parser_state *pstate = NULL;
72 /* Data that must be held for the duration of a parse. */
76 /* These are used to hold type lists and type stacks that are
77 allocated during the parse. */
78 std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
79 std::vector<std::unique_ptr<struct type_stack>> type_stacks;
81 /* Storage for some strings allocated during the parse. */
82 std::vector<gdb::unique_xmalloc_ptr<char>> strings;
84 /* When we find that lexptr (the global var defined in parse.c) is
85 pointing at a macro invocation, we expand the invocation, and call
86 scan_macro_expansion to save the old lexptr here and point lexptr
87 into the expanded text. When we reach the end of that, we call
88 end_macro_expansion to pop back to the value we saved here. The
89 macro expansion code promises to return only fully-expanded text,
90 so we don't need to "push" more than one level.
92 This is disgusting, of course. It would be cleaner to do all macro
93 expansion beforehand, and then hand that to lexptr. But we don't
94 really know where the expression ends. Remember, in a command like
96 (gdb) break *ADDRESS if CONDITION
98 we evaluate ADDRESS in the scope of the current frame, but we
99 evaluate CONDITION in the scope of the breakpoint's location. So
100 it's simply wrong to try to macro-expand the whole thing at once. */
101 const char *macro_original_text = nullptr;
103 /* We save all intermediate macro expansions on this obstack for the
104 duration of a single parse. The expansion text may sometimes have
105 to live past the end of the expansion, due to yacc lookahead.
106 Rather than try to be clever about saving the data for a single
107 token, we simply keep it all and delete it after parsing has
109 auto_obstack expansion_obstack;
111 /* The type stack. */
112 struct type_stack type_stack;
115 /* This is set and cleared in c_parse. */
117 static struct c_parse_state *cpstate;
121 static int yylex (void);
123 static void yyerror (const char *);
125 static int type_aggregate_p (struct type *);
127 using namespace expr;
130 /* Although the yacc "value" of an expression is not used,
131 since the result is stored in the structure being created,
132 other node types do have values. */
147 struct typed_stoken tsval;
149 struct symtoken ssym;
151 const struct block *bval;
152 enum exp_opcode opcode;
154 struct stoken_vector svec;
155 std::vector<struct type *> *tvec;
157 struct type_stack *type_stack;
159 struct objc_class_str theclass;
163 /* YYSTYPE gets defined by %union */
164 static int parse_number (struct parser_state *par_state,
165 const char *, int, int, YYSTYPE *);
166 static struct stoken operator_stoken (const char *);
167 static struct stoken typename_stoken (const char *);
168 static void check_parameter_typelist (std::vector<struct type *> *);
171 static void c_print_token (FILE *file, int type, YYSTYPE value);
172 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
176 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
178 %type <tval> type typebase scalar_type
179 %type <tvec> nonempty_typelist func_mod parameter_typelist
180 /* %type <bval> block */
182 /* Fancy type parsing. */
184 %type <lval> array_mod
185 %type <tval> conversion_type_id
187 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
189 %token <typed_val_int> INT COMPLEX_INT
190 %token <typed_val_float> FLOAT COMPLEX_FLOAT
192 /* Both NAME and TYPENAME tokens represent symbols in the input,
193 and both convey their data as strings.
194 But a TYPENAME is a string that happens to be defined as a typedef
195 or builtin type name (such as int or char)
196 and a NAME is any other symbol.
197 Contexts where this distinction is not important can use the
198 nonterminal "name", which matches either NAME or TYPENAME. */
200 %token <tsval> STRING
201 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
202 %token SELECTOR /* ObjC "@selector" pseudo-operator */
204 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
205 %token <ssym> UNKNOWN_CPP_NAME
206 %token <voidval> COMPLETE
207 %token <tsym> TYPENAME
208 %token <theclass> CLASSNAME /* ObjC Class name */
209 %type <sval> name field_name
210 %type <svec> string_exp
211 %type <ssym> name_not_typename
212 %type <tsym> type_name
214 /* This is like a '[' token, but is only generated when parsing
215 Objective C. This lets us reuse the same parser without
216 erroneously parsing ObjC-specific expressions in C. */
219 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
220 but which would parse as a valid number in the current input radix.
221 E.g. "c" when input_radix==16. Depending on the parse, it will be
222 turned into a name or into a number. */
224 %token <ssym> NAME_OR_INT
227 %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
232 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
238 /* Special type cases, put in to allow the parser to distinguish different
240 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
241 %token RESTRICT ATOMIC
242 %token FLOAT_KEYWORD COMPLEX
244 %token <sval> DOLLAR_VARIABLE
246 %token <opcode> ASSIGN_MODIFY
255 %right '=' ASSIGN_MODIFY
263 %left '<' '>' LEQ GEQ
268 %right UNARY INCREMENT DECREMENT
269 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
270 %token <ssym> BLOCKNAME
271 %token <bval> FILENAME
286 pstate->push_new<type_operation> ($1);
290 pstate->wrap<typeof_operation> ();
292 | TYPEOF '(' type ')'
294 pstate->push_new<type_operation> ($3);
296 | DECLTYPE '(' exp ')'
298 pstate->wrap<decltype_operation> ();
302 /* Expressions, including the comma operator. */
305 { pstate->wrap2<comma_operation> (); }
308 /* Expressions, not including the comma operator. */
309 exp : '*' exp %prec UNARY
310 { pstate->wrap<unop_ind_operation> (); }
313 exp : '&' exp %prec UNARY
314 { pstate->wrap<unop_addr_operation> (); }
317 exp : '-' exp %prec UNARY
318 { pstate->wrap<unary_neg_operation> (); }
321 exp : '+' exp %prec UNARY
322 { pstate->wrap<unary_plus_operation> (); }
325 exp : '!' exp %prec UNARY
327 if (pstate->language ()->la_language
329 pstate->wrap<opencl_not_operation> ();
331 pstate->wrap<unary_logical_not_operation> ();
335 exp : '~' exp %prec UNARY
336 { pstate->wrap<unary_complement_operation> (); }
339 exp : INCREMENT exp %prec UNARY
340 { pstate->wrap<preinc_operation> (); }
343 exp : DECREMENT exp %prec UNARY
344 { pstate->wrap<predec_operation> (); }
347 exp : exp INCREMENT %prec UNARY
348 { pstate->wrap<postinc_operation> (); }
351 exp : exp DECREMENT %prec UNARY
352 { pstate->wrap<postdec_operation> (); }
355 exp : TYPEID '(' exp ')' %prec UNARY
356 { pstate->wrap<typeid_operation> (); }
359 exp : TYPEID '(' type_exp ')' %prec UNARY
360 { pstate->wrap<typeid_operation> (); }
363 exp : SIZEOF exp %prec UNARY
364 { pstate->wrap<unop_sizeof_operation> (); }
367 exp : ALIGNOF '(' type_exp ')' %prec UNARY
368 { pstate->wrap<unop_alignof_operation> (); }
371 exp : exp ARROW field_name
373 pstate->push_new<structop_ptr_operation>
374 (pstate->pop (), copy_name ($3));
378 exp : exp ARROW field_name COMPLETE
380 structop_base_operation *op
381 = new structop_ptr_operation (pstate->pop (),
383 pstate->mark_struct_expression (op);
384 pstate->push (operation_up (op));
388 exp : exp ARROW COMPLETE
390 structop_base_operation *op
391 = new structop_ptr_operation (pstate->pop (), "");
392 pstate->mark_struct_expression (op);
393 pstate->push (operation_up (op));
397 exp : exp ARROW '~' name
399 pstate->push_new<structop_ptr_operation>
400 (pstate->pop (), "~" + copy_name ($4));
404 exp : exp ARROW '~' name COMPLETE
406 structop_base_operation *op
407 = new structop_ptr_operation (pstate->pop (),
408 "~" + copy_name ($4));
409 pstate->mark_struct_expression (op);
410 pstate->push (operation_up (op));
414 exp : exp ARROW 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 pstate->wrap<unop_addr_operation> ();
419 pstate->wrap2<structop_mptr_operation> (); }
422 exp : exp ARROW_STAR exp
423 { pstate->wrap2<structop_mptr_operation> (); }
426 exp : exp '.' field_name
428 if (pstate->language ()->la_language
430 pstate->push_new<opencl_structop_operation>
431 (pstate->pop (), copy_name ($3));
433 pstate->push_new<structop_operation>
434 (pstate->pop (), copy_name ($3));
438 exp : exp '.' field_name COMPLETE
440 structop_base_operation *op
441 = new structop_operation (pstate->pop (),
443 pstate->mark_struct_expression (op);
444 pstate->push (operation_up (op));
448 exp : exp '.' COMPLETE
450 structop_base_operation *op
451 = new structop_operation (pstate->pop (), "");
452 pstate->mark_struct_expression (op);
453 pstate->push (operation_up (op));
457 exp : exp '.' '~' name
459 pstate->push_new<structop_operation>
460 (pstate->pop (), "~" + copy_name ($4));
464 exp : exp '.' '~' name COMPLETE
466 structop_base_operation *op
467 = new structop_operation (pstate->pop (),
468 "~" + copy_name ($4));
469 pstate->mark_struct_expression (op);
470 pstate->push (operation_up (op));
474 exp : exp '.' qualified_name
475 { /* exp.type::name becomes exp.*(&type::name) */
476 /* Note: this doesn't work if name is a
477 static member! FIXME */
478 pstate->wrap<unop_addr_operation> ();
479 pstate->wrap2<structop_member_operation> (); }
482 exp : exp DOT_STAR exp
483 { pstate->wrap2<structop_member_operation> (); }
486 exp : exp '[' exp1 ']'
487 { pstate->wrap2<subscript_operation> (); }
490 exp : exp OBJC_LBRAC exp1 ']'
491 { pstate->wrap2<subscript_operation> (); }
495 * The rules below parse ObjC message calls of the form:
496 * '[' target selector {':' argument}* ']'
499 exp : OBJC_LBRAC TYPENAME
503 std::string copy = copy_name ($2.stoken);
504 theclass = lookup_objc_class (pstate->gdbarch (),
507 error (_("%s is not an ObjC Class"),
509 pstate->push_new<long_const_operation>
510 (parse_type (pstate)->builtin_int,
515 { end_msglist (pstate); }
518 exp : OBJC_LBRAC CLASSNAME
520 pstate->push_new<long_const_operation>
521 (parse_type (pstate)->builtin_int,
522 (LONGEST) $2.theclass);
526 { end_msglist (pstate); }
532 { end_msglist (pstate); }
536 { add_msglist(&$1, 0); }
544 msgarg : name ':' exp
545 { add_msglist(&$1, 1); }
546 | ':' exp /* Unnamed arg. */
547 { add_msglist(0, 1); }
548 | ',' exp /* Variable number of args. */
549 { add_msglist(0, 0); }
553 /* This is to save the value of arglist_len
554 being accumulated by an outer function call. */
555 { pstate->start_arglist (); }
556 arglist ')' %prec ARROW
558 std::vector<operation_up> args
559 = pstate->pop_vector (pstate->end_arglist ());
560 pstate->push_new<funcall_operation>
561 (pstate->pop (), std::move (args));
565 /* This is here to disambiguate with the production for
566 "func()::static_var" further below, which uses
567 function_method_void. */
568 exp : exp '(' ')' %prec ARROW
570 pstate->push_new<funcall_operation>
571 (pstate->pop (), std::vector<operation_up> ());
576 exp : UNKNOWN_CPP_NAME '('
578 /* This could potentially be a an argument defined
579 lookup function (Koenig). */
580 /* This is to save the value of arglist_len
581 being accumulated by an outer function call. */
582 pstate->start_arglist ();
584 arglist ')' %prec ARROW
586 std::vector<operation_up> args
587 = pstate->pop_vector (pstate->end_arglist ());
588 pstate->push_new<adl_func_operation>
589 (copy_name ($1.stoken),
590 pstate->expression_context_block,
596 { pstate->start_arglist (); }
603 { pstate->arglist_len = 1; }
606 arglist : arglist ',' exp %prec ABOVE_COMMA
607 { pstate->arglist_len++; }
610 function_method: exp '(' parameter_typelist ')' const_or_volatile
612 std::vector<struct type *> *type_list = $3;
613 /* Save the const/volatile qualifiers as
614 recorded by the const_or_volatile
615 production's actions. */
616 type_instance_flags flags
617 = (cpstate->type_stack
618 .follow_type_instance_flags ());
619 pstate->push_new<type_instance_operation>
620 (flags, std::move (*type_list),
625 function_method_void: exp '(' ')' const_or_volatile
627 type_instance_flags flags
628 = (cpstate->type_stack
629 .follow_type_instance_flags ());
630 pstate->push_new<type_instance_operation>
631 (flags, std::vector<type *> (), pstate->pop ());
635 exp : function_method
638 /* Normally we must interpret "func()" as a function call, instead of
639 a type. The user needs to write func(void) to disambiguate.
640 However, in the "func()::static_var" case, there's no
642 function_method_void_or_typelist: function_method
643 | function_method_void
646 exp : function_method_void_or_typelist COLONCOLON name
648 pstate->push_new<func_static_var_operation>
649 (pstate->pop (), copy_name ($3));
654 { $$ = pstate->end_arglist () - 1; }
656 exp : lcurly arglist rcurly %prec ARROW
658 std::vector<operation_up> args
659 = pstate->pop_vector ($3 + 1);
660 pstate->push_new<array_operation> (0, $3,
665 exp : lcurly type_exp rcurly exp %prec UNARY
666 { pstate->wrap2<unop_memval_type_operation> (); }
669 exp : '(' type_exp ')' exp %prec UNARY
671 if (pstate->language ()->la_language
673 pstate->wrap2<opencl_cast_type_operation> ();
675 pstate->wrap2<unop_cast_type_operation> ();
683 /* Binary operators in order of decreasing precedence. */
686 { pstate->wrap2<repeat_operation> (); }
690 { pstate->wrap2<mul_operation> (); }
694 { pstate->wrap2<div_operation> (); }
698 { pstate->wrap2<rem_operation> (); }
702 { pstate->wrap2<add_operation> (); }
706 { pstate->wrap2<sub_operation> (); }
710 { pstate->wrap2<lsh_operation> (); }
714 { pstate->wrap2<rsh_operation> (); }
719 if (pstate->language ()->la_language
721 pstate->wrap2<opencl_equal_operation> ();
723 pstate->wrap2<equal_operation> ();
727 exp : exp NOTEQUAL exp
729 if (pstate->language ()->la_language
731 pstate->wrap2<opencl_notequal_operation> ();
733 pstate->wrap2<notequal_operation> ();
739 if (pstate->language ()->la_language
741 pstate->wrap2<opencl_leq_operation> ();
743 pstate->wrap2<leq_operation> ();
749 if (pstate->language ()->la_language
751 pstate->wrap2<opencl_geq_operation> ();
753 pstate->wrap2<geq_operation> ();
759 if (pstate->language ()->la_language
761 pstate->wrap2<opencl_less_operation> ();
763 pstate->wrap2<less_operation> ();
769 if (pstate->language ()->la_language
771 pstate->wrap2<opencl_gtr_operation> ();
773 pstate->wrap2<gtr_operation> ();
778 { pstate->wrap2<bitwise_and_operation> (); }
782 { pstate->wrap2<bitwise_xor_operation> (); }
786 { pstate->wrap2<bitwise_ior_operation> (); }
791 if (pstate->language ()->la_language
794 operation_up rhs = pstate->pop ();
795 operation_up lhs = pstate->pop ();
796 pstate->push_new<opencl_logical_binop_operation>
797 (BINOP_LOGICAL_AND, std::move (lhs),
801 pstate->wrap2<logical_and_operation> ();
807 if (pstate->language ()->la_language
810 operation_up rhs = pstate->pop ();
811 operation_up lhs = pstate->pop ();
812 pstate->push_new<opencl_logical_binop_operation>
813 (BINOP_LOGICAL_OR, std::move (lhs),
817 pstate->wrap2<logical_or_operation> ();
821 exp : exp '?' exp ':' exp %prec '?'
823 operation_up last = pstate->pop ();
824 operation_up mid = pstate->pop ();
825 operation_up first = pstate->pop ();
826 if (pstate->language ()->la_language
828 pstate->push_new<opencl_ternop_cond_operation>
829 (std::move (first), std::move (mid),
832 pstate->push_new<ternop_cond_operation>
833 (std::move (first), std::move (mid),
840 if (pstate->language ()->la_language
842 pstate->wrap2<opencl_assign_operation> ();
844 pstate->wrap2<assign_operation> ();
848 exp : exp ASSIGN_MODIFY exp
850 operation_up rhs = pstate->pop ();
851 operation_up lhs = pstate->pop ();
852 pstate->push_new<assign_modify_operation>
853 ($2, std::move (lhs), std::move (rhs));
859 pstate->push_new<long_const_operation>
867 = (make_operation<long_const_operation>
868 (TYPE_TARGET_TYPE ($1.type), 0));
870 = (make_operation<long_const_operation>
871 (TYPE_TARGET_TYPE ($1.type), $1.val));
872 pstate->push_new<complex_operation>
873 (std::move (real), std::move (imag), $1.type);
879 struct stoken_vector vec;
882 pstate->push_c_string ($1.type, &vec);
888 parse_number (pstate, $1.stoken.ptr,
889 $1.stoken.length, 0, &val);
890 pstate->push_new<long_const_operation>
891 (val.typed_val_int.type,
892 val.typed_val_int.val);
900 std::copy (std::begin ($1.val), std::end ($1.val),
902 pstate->push_new<float_const_operation> ($1.type, data);
908 struct type *underlying
909 = TYPE_TARGET_TYPE ($1.type);
912 target_float_from_host_double (val.data (),
915 = (make_operation<float_const_operation>
918 std::copy (std::begin ($1.val), std::end ($1.val),
921 = (make_operation<float_const_operation>
924 pstate->push_new<complex_operation>
925 (std::move (real), std::move (imag),
933 exp : DOLLAR_VARIABLE
935 pstate->push_dollar ($1);
939 exp : SELECTOR '(' name ')'
941 pstate->push_new<objc_selector_operation>
946 exp : SIZEOF '(' type ')' %prec UNARY
947 { struct type *type = $3;
948 struct type *int_type
949 = lookup_signed_typename (pstate->language (),
951 type = check_typedef (type);
953 /* $5.3.3/2 of the C++ Standard (n3290 draft)
954 says of sizeof: "When applied to a reference
955 or a reference type, the result is the size of
956 the referenced type." */
957 if (TYPE_IS_REFERENCE (type))
958 type = check_typedef (TYPE_TARGET_TYPE (type));
959 pstate->push_new<long_const_operation>
960 (int_type, TYPE_LENGTH (type));
964 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
965 { pstate->wrap2<reinterpret_cast_operation> (); }
968 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
969 { pstate->wrap2<unop_cast_type_operation> (); }
972 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
973 { pstate->wrap2<dynamic_cast_operation> (); }
976 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
977 { /* We could do more error checking here, but
978 it doesn't seem worthwhile. */
979 pstate->wrap2<unop_cast_type_operation> (); }
985 /* We copy the string here, and not in the
986 lexer, to guarantee that we do not leak a
987 string. Note that we follow the
988 NUL-termination convention of the
990 struct typed_stoken *vec = XNEW (struct typed_stoken);
995 vec->length = $1.length;
996 vec->ptr = (char *) malloc ($1.length + 1);
997 memcpy (vec->ptr, $1.ptr, $1.length + 1);
1002 /* Note that we NUL-terminate here, but just
1006 $$.tokens = XRESIZEVEC (struct typed_stoken,
1009 p = (char *) malloc ($2.length + 1);
1010 memcpy (p, $2.ptr, $2.length + 1);
1012 $$.tokens[$$.len - 1].type = $2.type;
1013 $$.tokens[$$.len - 1].length = $2.length;
1014 $$.tokens[$$.len - 1].ptr = p;
1021 c_string_type type = C_STRING;
1023 for (i = 0; i < $1.len; ++i)
1025 switch ($1.tokens[i].type)
1032 if (type != C_STRING
1033 && type != $1.tokens[i].type)
1034 error (_("Undefined string concatenation."));
1035 type = (enum c_string_type_values) $1.tokens[i].type;
1038 /* internal error */
1039 internal_error (__FILE__, __LINE__,
1040 "unrecognized type in string concatenation");
1044 pstate->push_c_string (type, &$1);
1045 for (i = 0; i < $1.len; ++i)
1046 free ($1.tokens[i].ptr);
1051 exp : NSSTRING /* ObjC NextStep NSString constant
1052 * of the form '@' '"' string '"'.
1055 pstate->push_new<objc_nsstring_operation>
1062 { pstate->push_new<long_const_operation>
1063 (parse_type (pstate)->builtin_bool, 1);
1068 { pstate->push_new<long_const_operation>
1069 (parse_type (pstate)->builtin_bool, 0);
1078 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
1080 error (_("No file or function \"%s\"."),
1081 copy_name ($1.stoken).c_str ());
1089 block : block COLONCOLON name
1091 std::string copy = copy_name ($3);
1093 = lookup_symbol (copy.c_str (), $1,
1094 VAR_DOMAIN, NULL).symbol;
1096 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
1097 error (_("No function \"%s\" in specified context."),
1099 $$ = SYMBOL_BLOCK_VALUE (tem); }
1102 variable: name_not_typename ENTRY
1103 { struct symbol *sym = $1.sym.symbol;
1105 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
1106 || !symbol_read_needs_frame (sym))
1107 error (_("@entry can be used only for function "
1108 "parameters, not for \"%s\""),
1109 copy_name ($1.stoken).c_str ());
1111 pstate->push_new<var_entry_value_operation> (sym);
1115 variable: block COLONCOLON name
1117 std::string copy = copy_name ($3);
1118 struct block_symbol sym
1119 = lookup_symbol (copy.c_str (), $1,
1122 if (sym.symbol == 0)
1123 error (_("No symbol \"%s\" in specified context."),
1125 if (symbol_read_needs_frame (sym.symbol))
1126 pstate->block_tracker->update (sym);
1128 pstate->push_new<var_value_operation> (sym.symbol,
1133 qualified_name: TYPENAME COLONCOLON name
1135 struct type *type = $1.type;
1136 type = check_typedef (type);
1137 if (!type_aggregate_p (type))
1138 error (_("`%s' is not defined as an aggregate type."),
1139 TYPE_SAFE_NAME (type));
1141 pstate->push_new<scope_operation> (type,
1144 | TYPENAME COLONCOLON '~' name
1146 struct type *type = $1.type;
1148 type = check_typedef (type);
1149 if (!type_aggregate_p (type))
1150 error (_("`%s' is not defined as an aggregate type."),
1151 TYPE_SAFE_NAME (type));
1152 std::string name = "~" + std::string ($4.ptr,
1155 /* Check for valid destructor name. */
1156 destructor_name_p (name.c_str (), $1.type);
1157 pstate->push_new<scope_operation> (type,
1160 | TYPENAME COLONCOLON name COLONCOLON name
1162 std::string copy = copy_name ($3);
1163 error (_("No type \"%s\" within class "
1164 "or namespace \"%s\"."),
1165 copy.c_str (), TYPE_SAFE_NAME ($1.type));
1169 variable: qualified_name
1170 | COLONCOLON name_not_typename
1172 std::string name = copy_name ($2.stoken);
1173 struct block_symbol sym
1174 = lookup_symbol (name.c_str (),
1175 (const struct block *) NULL,
1177 pstate->push_symbol (name.c_str (), sym);
1181 variable: name_not_typename
1182 { struct block_symbol sym = $1.sym;
1186 if (symbol_read_needs_frame (sym.symbol))
1187 pstate->block_tracker->update (sym);
1189 /* If we found a function, see if it's
1190 an ifunc resolver that has the same
1191 address as the ifunc symbol itself.
1192 If so, prefer the ifunc symbol. */
1194 bound_minimal_symbol resolver
1195 = find_gnu_ifunc (sym.symbol);
1196 if (resolver.minsym != NULL)
1197 pstate->push_new<var_msym_value_operation>
1198 (resolver.minsym, resolver.objfile);
1200 pstate->push_new<var_value_operation>
1201 (sym.symbol, sym.block);
1203 else if ($1.is_a_field_of_this)
1205 /* C++: it hangs off of `this'. Must
1206 not inadvertently convert from a method call
1208 pstate->block_tracker->update (sym);
1210 = make_operation<op_this_operation> ();
1211 pstate->push_new<structop_ptr_operation>
1212 (std::move (thisop), copy_name ($1.stoken));
1216 std::string arg = copy_name ($1.stoken);
1218 bound_minimal_symbol msymbol
1219 = lookup_bound_minimal_symbol (arg.c_str ());
1220 if (msymbol.minsym == NULL)
1222 if (!have_full_symbols () && !have_partial_symbols ())
1223 error (_("No symbol table is loaded. Use the \"file\" command."));
1225 error (_("No symbol \"%s\" in current context."),
1229 /* This minsym might be an alias for
1230 another function. See if we can find
1231 the debug symbol for the target, and
1232 if so, use it instead, since it has
1233 return type / prototype info. This
1234 is important for example for "p
1235 *__errno_location()". */
1236 symbol *alias_target
1237 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1238 && msymbol.minsym->type != mst_data_gnu_ifunc)
1239 ? find_function_alias_target (msymbol)
1241 if (alias_target != NULL)
1242 pstate->push_new<var_value_operation>
1243 (alias_target, SYMBOL_BLOCK_VALUE (alias_target));
1245 pstate->push_new<var_msym_value_operation>
1246 (msymbol.minsym, msymbol.objfile);
1251 const_or_volatile: const_or_volatile_noopt
1257 { cpstate->type_stack.insert (tp_const); }
1259 { cpstate->type_stack.insert (tp_volatile); }
1261 { cpstate->type_stack.insert (tp_atomic); }
1263 { cpstate->type_stack.insert (tp_restrict); }
1266 cpstate->type_stack.insert (pstate,
1267 copy_name ($2.stoken).c_str ());
1271 qualifier_seq_noopt:
1273 | qualifier_seq single_qualifier
1283 { cpstate->type_stack.insert (tp_pointer); }
1286 { cpstate->type_stack.insert (tp_pointer); }
1289 { cpstate->type_stack.insert (tp_reference); }
1291 { cpstate->type_stack.insert (tp_reference); }
1293 { cpstate->type_stack.insert (tp_rvalue_reference); }
1294 | ANDAND ptr_operator
1295 { cpstate->type_stack.insert (tp_rvalue_reference); }
1298 ptr_operator_ts: ptr_operator
1300 $$ = cpstate->type_stack.create ();
1301 cpstate->type_stacks.emplace_back ($$);
1305 abs_decl: ptr_operator_ts direct_abs_decl
1306 { $$ = $2->append ($1); }
1311 direct_abs_decl: '(' abs_decl ')'
1313 | direct_abs_decl array_mod
1315 cpstate->type_stack.push ($1);
1316 cpstate->type_stack.push ($2);
1317 cpstate->type_stack.push (tp_array);
1318 $$ = cpstate->type_stack.create ();
1319 cpstate->type_stacks.emplace_back ($$);
1323 cpstate->type_stack.push ($1);
1324 cpstate->type_stack.push (tp_array);
1325 $$ = cpstate->type_stack.create ();
1326 cpstate->type_stacks.emplace_back ($$);
1329 | direct_abs_decl func_mod
1331 cpstate->type_stack.push ($1);
1332 cpstate->type_stack.push ($2);
1333 $$ = cpstate->type_stack.create ();
1334 cpstate->type_stacks.emplace_back ($$);
1338 cpstate->type_stack.push ($1);
1339 $$ = cpstate->type_stack.create ();
1340 cpstate->type_stacks.emplace_back ($$);
1350 | OBJC_LBRAC INT ']'
1356 $$ = new std::vector<struct type *>;
1357 cpstate->type_lists.emplace_back ($$);
1359 | '(' parameter_typelist ')'
1363 /* We used to try to recognize pointer to member types here, but
1364 that didn't work (shift/reduce conflicts meant that these rules never
1365 got executed). The problem is that
1366 int (foo::bar::baz::bizzle)
1367 is a function type but
1368 int (foo::bar::baz::bizzle::*)
1369 is a pointer to member type. Stroustrup loses again! */
1374 /* A helper production that recognizes scalar types that can validly
1375 be used with _Complex. */
1379 { $$ = lookup_signed_typename (pstate->language (),
1382 { $$ = lookup_signed_typename (pstate->language (),
1385 { $$ = lookup_signed_typename (pstate->language (),
1388 { $$ = lookup_signed_typename (pstate->language (),
1390 | LONG SIGNED_KEYWORD INT_KEYWORD
1391 { $$ = lookup_signed_typename (pstate->language (),
1393 | LONG SIGNED_KEYWORD
1394 { $$ = lookup_signed_typename (pstate->language (),
1396 | SIGNED_KEYWORD LONG INT_KEYWORD
1397 { $$ = lookup_signed_typename (pstate->language (),
1399 | UNSIGNED LONG INT_KEYWORD
1400 { $$ = lookup_unsigned_typename (pstate->language (),
1402 | LONG UNSIGNED INT_KEYWORD
1403 { $$ = lookup_unsigned_typename (pstate->language (),
1406 { $$ = lookup_unsigned_typename (pstate->language (),
1409 { $$ = lookup_signed_typename (pstate->language (),
1411 | LONG LONG INT_KEYWORD
1412 { $$ = lookup_signed_typename (pstate->language (),
1414 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1415 { $$ = lookup_signed_typename (pstate->language (),
1417 | LONG LONG SIGNED_KEYWORD
1418 { $$ = lookup_signed_typename (pstate->language (),
1420 | SIGNED_KEYWORD LONG LONG
1421 { $$ = lookup_signed_typename (pstate->language (),
1423 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1424 { $$ = lookup_signed_typename (pstate->language (),
1426 | UNSIGNED LONG LONG
1427 { $$ = lookup_unsigned_typename (pstate->language (),
1429 | UNSIGNED LONG LONG INT_KEYWORD
1430 { $$ = lookup_unsigned_typename (pstate->language (),
1432 | LONG LONG UNSIGNED
1433 { $$ = lookup_unsigned_typename (pstate->language (),
1435 | LONG LONG UNSIGNED INT_KEYWORD
1436 { $$ = lookup_unsigned_typename (pstate->language (),
1439 { $$ = lookup_signed_typename (pstate->language (),
1441 | SHORT SIGNED_KEYWORD INT_KEYWORD
1442 { $$ = lookup_signed_typename (pstate->language (),
1444 | SHORT SIGNED_KEYWORD
1445 { $$ = lookup_signed_typename (pstate->language (),
1447 | UNSIGNED SHORT INT_KEYWORD
1448 { $$ = lookup_unsigned_typename (pstate->language (),
1451 { $$ = lookup_unsigned_typename (pstate->language (),
1453 | SHORT UNSIGNED INT_KEYWORD
1454 { $$ = lookup_unsigned_typename (pstate->language (),
1457 { $$ = lookup_typename (pstate->language (),
1462 { $$ = lookup_typename (pstate->language (),
1466 | LONG DOUBLE_KEYWORD
1467 { $$ = lookup_typename (pstate->language (),
1471 | UNSIGNED type_name
1472 { $$ = lookup_unsigned_typename (pstate->language (),
1473 $2.type->name ()); }
1475 { $$ = lookup_unsigned_typename (pstate->language (),
1477 | SIGNED_KEYWORD type_name
1478 { $$ = lookup_signed_typename (pstate->language (),
1479 $2.type->name ()); }
1481 { $$ = lookup_signed_typename (pstate->language (),
1485 /* Implements (approximately): (type-qualifier)* type-specifier.
1487 When type-specifier is only ever a single word, like 'float' then these
1488 arrive as pre-built TYPENAME tokens thanks to the classify_name
1489 function. However, when a type-specifier can contain multiple words,
1490 for example 'double' can appear as just 'double' or 'long double', and
1491 similarly 'long' can appear as just 'long' or in 'long double', then
1492 these type-specifiers are parsed into their own tokens in the function
1493 lex_one_token and the ident_tokens array. These separate tokens are all
1500 | COMPLEX scalar_type
1502 $$ = init_complex_type (nullptr, $2);
1506 = lookup_struct (copy_name ($2).c_str (),
1507 pstate->expression_context_block);
1511 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1515 | STRUCT name COMPLETE
1517 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1522 { $$ = lookup_struct
1523 (copy_name ($2).c_str (),
1524 pstate->expression_context_block);
1528 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1532 | CLASS name COMPLETE
1534 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1540 = lookup_union (copy_name ($2).c_str (),
1541 pstate->expression_context_block);
1545 pstate->mark_completion_tag (TYPE_CODE_UNION,
1549 | UNION name COMPLETE
1551 pstate->mark_completion_tag (TYPE_CODE_UNION,
1556 { $$ = lookup_enum (copy_name ($2).c_str (),
1557 pstate->expression_context_block);
1561 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1564 | ENUM name COMPLETE
1566 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1570 /* It appears that this rule for templates is never
1571 reduced; template recognition happens by lookahead
1572 in the token processing code in yylex. */
1573 | TEMPLATE name '<' type '>'
1574 { $$ = lookup_template_type
1575 (copy_name($2).c_str (), $4,
1576 pstate->expression_context_block);
1578 | qualifier_seq_noopt typebase
1579 { $$ = cpstate->type_stack.follow_types ($2); }
1580 | typebase qualifier_seq_noopt
1581 { $$ = cpstate->type_stack.follow_types ($1); }
1587 $$.stoken.ptr = "int";
1588 $$.stoken.length = 3;
1589 $$.type = lookup_signed_typename (pstate->language (),
1594 $$.stoken.ptr = "long";
1595 $$.stoken.length = 4;
1596 $$.type = lookup_signed_typename (pstate->language (),
1601 $$.stoken.ptr = "short";
1602 $$.stoken.length = 5;
1603 $$.type = lookup_signed_typename (pstate->language (),
1610 { check_parameter_typelist ($1); }
1611 | nonempty_typelist ',' DOTDOTDOT
1613 $1->push_back (NULL);
1614 check_parameter_typelist ($1);
1622 std::vector<struct type *> *typelist
1623 = new std::vector<struct type *>;
1624 cpstate->type_lists.emplace_back (typelist);
1626 typelist->push_back ($1);
1629 | nonempty_typelist ',' type
1639 cpstate->type_stack.push ($2);
1640 $$ = cpstate->type_stack.follow_types ($1);
1644 conversion_type_id: typebase conversion_declarator
1645 { $$ = cpstate->type_stack.follow_types ($1); }
1648 conversion_declarator: /* Nothing. */
1649 | ptr_operator conversion_declarator
1652 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1653 | VOLATILE_KEYWORD CONST_KEYWORD
1656 const_or_volatile_noopt: const_and_volatile
1657 { cpstate->type_stack.insert (tp_const);
1658 cpstate->type_stack.insert (tp_volatile);
1661 { cpstate->type_stack.insert (tp_const); }
1663 { cpstate->type_stack.insert (tp_volatile); }
1667 { $$ = operator_stoken (" new"); }
1669 { $$ = operator_stoken (" delete"); }
1670 | OPERATOR NEW '[' ']'
1671 { $$ = operator_stoken (" new[]"); }
1672 | OPERATOR DELETE '[' ']'
1673 { $$ = operator_stoken (" delete[]"); }
1674 | OPERATOR NEW OBJC_LBRAC ']'
1675 { $$ = operator_stoken (" new[]"); }
1676 | OPERATOR DELETE OBJC_LBRAC ']'
1677 { $$ = operator_stoken (" delete[]"); }
1679 { $$ = operator_stoken ("+"); }
1681 { $$ = operator_stoken ("-"); }
1683 { $$ = operator_stoken ("*"); }
1685 { $$ = operator_stoken ("/"); }
1687 { $$ = operator_stoken ("%"); }
1689 { $$ = operator_stoken ("^"); }
1691 { $$ = operator_stoken ("&"); }
1693 { $$ = operator_stoken ("|"); }
1695 { $$ = operator_stoken ("~"); }
1697 { $$ = operator_stoken ("!"); }
1699 { $$ = operator_stoken ("="); }
1701 { $$ = operator_stoken ("<"); }
1703 { $$ = operator_stoken (">"); }
1704 | OPERATOR ASSIGN_MODIFY
1705 { const char *op = " unknown";
1729 case BINOP_BITWISE_IOR:
1732 case BINOP_BITWISE_AND:
1735 case BINOP_BITWISE_XOR:
1742 $$ = operator_stoken (op);
1745 { $$ = operator_stoken ("<<"); }
1747 { $$ = operator_stoken (">>"); }
1749 { $$ = operator_stoken ("=="); }
1751 { $$ = operator_stoken ("!="); }
1753 { $$ = operator_stoken ("<="); }
1755 { $$ = operator_stoken (">="); }
1757 { $$ = operator_stoken ("&&"); }
1759 { $$ = operator_stoken ("||"); }
1760 | OPERATOR INCREMENT
1761 { $$ = operator_stoken ("++"); }
1762 | OPERATOR DECREMENT
1763 { $$ = operator_stoken ("--"); }
1765 { $$ = operator_stoken (","); }
1766 | OPERATOR ARROW_STAR
1767 { $$ = operator_stoken ("->*"); }
1769 { $$ = operator_stoken ("->"); }
1771 { $$ = operator_stoken ("()"); }
1773 { $$ = operator_stoken ("[]"); }
1774 | OPERATOR OBJC_LBRAC ']'
1775 { $$ = operator_stoken ("[]"); }
1776 | OPERATOR conversion_type_id
1779 c_print_type ($2, NULL, &buf, -1, 0,
1780 &type_print_raw_options);
1781 std::string name = std::move (buf.string ());
1783 /* This also needs canonicalization. */
1784 gdb::unique_xmalloc_ptr<char> canon
1785 = cp_canonicalize_string (name.c_str ());
1786 if (canon != nullptr)
1787 name = canon.get ();
1788 $$ = operator_stoken ((" " + name).c_str ());
1792 /* This rule exists in order to allow some tokens that would not normally
1793 match the 'name' rule to appear as fields within a struct. The example
1794 that initially motivated this was the RISC-V target which models the
1795 floating point registers as a union with fields called 'float' and
1799 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1800 | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
1801 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1802 | LONG { $$ = typename_stoken ("long"); }
1803 | SHORT { $$ = typename_stoken ("short"); }
1804 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1805 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
1808 name : NAME { $$ = $1.stoken; }
1809 | BLOCKNAME { $$ = $1.stoken; }
1810 | TYPENAME { $$ = $1.stoken; }
1811 | NAME_OR_INT { $$ = $1.stoken; }
1812 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1816 name_not_typename : NAME
1818 /* These would be useful if name_not_typename was useful, but it is just
1819 a fake for "variable", so these cause reduce/reduce conflicts because
1820 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1821 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1822 context where only a name could occur, this might be useful.
1827 struct field_of_this_result is_a_field_of_this;
1831 = lookup_symbol ($1.ptr,
1832 pstate->expression_context_block,
1834 &is_a_field_of_this);
1835 $$.is_a_field_of_this
1836 = is_a_field_of_this.type != NULL;
1843 /* Returns a stoken of the operator name given by OP (which does not
1844 include the string "operator"). */
1846 static struct stoken
1847 operator_stoken (const char *op)
1849 struct stoken st = { NULL, 0 };
1852 st.length = CP_OPERATOR_LEN + strlen (op);
1853 buf = (char *) malloc (st.length + 1);
1854 strcpy (buf, CP_OPERATOR_STR);
1858 /* The toplevel (c_parse) will free the memory allocated here. */
1859 cpstate->strings.emplace_back (buf);
1863 /* Returns a stoken of the type named TYPE. */
1865 static struct stoken
1866 typename_stoken (const char *type)
1868 struct stoken st = { type, 0 };
1869 st.length = strlen (type);
1873 /* Return true if the type is aggregate-like. */
1876 type_aggregate_p (struct type *type)
1878 return (type->code () == TYPE_CODE_STRUCT
1879 || type->code () == TYPE_CODE_UNION
1880 || type->code () == TYPE_CODE_NAMESPACE
1881 || (type->code () == TYPE_CODE_ENUM
1882 && TYPE_DECLARED_CLASS (type)));
1885 /* Validate a parameter typelist. */
1888 check_parameter_typelist (std::vector<struct type *> *params)
1893 for (ix = 0; ix < params->size (); ++ix)
1895 type = (*params)[ix];
1896 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
1900 if (params->size () == 1)
1905 error (_("parameter types following 'void'"));
1908 error (_("'void' invalid as parameter type"));
1913 /* Take care of parsing a number (anything that starts with a digit).
1914 Set yylval and return the token type; update lexptr.
1915 LEN is the number of characters in it. */
1917 /*** Needs some error checking for the float case ***/
1920 parse_number (struct parser_state *par_state,
1921 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1929 int base = input_radix;
1932 /* Number of "L" suffixes encountered. */
1935 /* Imaginary number. */
1936 bool imaginary_p = false;
1938 /* We have found a "L" or "U" (or "i") suffix. */
1939 int found_suffix = 0;
1942 struct type *signed_type;
1943 struct type *unsigned_type;
1946 p = (char *) alloca (len);
1947 memcpy (p, buf, len);
1951 if (len >= 1 && p[len - 1] == 'i')
1957 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
1958 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1960 putithere->typed_val_float.type
1961 = parse_type (par_state)->builtin_decfloat;
1964 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1966 putithere->typed_val_float.type
1967 = parse_type (par_state)->builtin_decdouble;
1970 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1972 putithere->typed_val_float.type
1973 = parse_type (par_state)->builtin_declong;
1976 /* Handle suffixes: 'f' for float, 'l' for long double. */
1977 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
1979 putithere->typed_val_float.type
1980 = parse_type (par_state)->builtin_float;
1983 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
1985 putithere->typed_val_float.type
1986 = parse_type (par_state)->builtin_long_double;
1989 /* Default type for floating-point literals is double. */
1992 putithere->typed_val_float.type
1993 = parse_type (par_state)->builtin_double;
1996 if (!parse_float (p, len,
1997 putithere->typed_val_float.type,
1998 putithere->typed_val_float.val))
2002 putithere->typed_val_float.type
2003 = init_complex_type (nullptr, putithere->typed_val_float.type);
2005 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
2008 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
2009 if (p[0] == '0' && len > 1)
2052 if (c >= 'A' && c <= 'Z')
2054 if (c != 'l' && c != 'u' && c != 'i')
2056 if (c >= '0' && c <= '9')
2064 if (base > 10 && c >= 'a' && c <= 'f')
2068 n += i = c - 'a' + 10;
2086 return ERROR; /* Char not a digit */
2089 return ERROR; /* Invalid digit in this base */
2091 /* Portably test for overflow (only works for nonzero values, so make
2092 a second check for zero). FIXME: Can't we just make n and prevn
2093 unsigned and avoid this? */
2094 if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
2095 unsigned_p = 1; /* Try something unsigned */
2097 /* Portably test for unsigned overflow.
2098 FIXME: This check is wrong; for example it doesn't find overflow
2099 on 0x123456789 when LONGEST is 32 bits. */
2100 if (c != 'l' && c != 'u' && c != 'i' && n != 0)
2102 if (unsigned_p && prevn >= n)
2103 error (_("Numeric constant too large."));
2108 /* An integer constant is an int, a long, or a long long. An L
2109 suffix forces it to be long; an LL suffix forces it to be long
2110 long. If not forced to a larger size, it gets the first type of
2111 the above that it fits in. To figure out whether it fits, we
2112 shift it right and see whether anything remains. Note that we
2113 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2114 operation, because many compilers will warn about such a shift
2115 (which always produces a zero result). Sometimes gdbarch_int_bit
2116 or gdbarch_long_bit will be that big, sometimes not. To deal with
2117 the case where it is we just always shift the value more than
2118 once, with fewer bits each time. */
2122 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
2125 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
2127 /* A large decimal (not hex or octal) constant (between INT_MAX
2128 and UINT_MAX) is a long or unsigned long, according to ANSI,
2129 never an unsigned int, but this code treats it as unsigned
2130 int. This probably should be fixed. GCC gives a warning on
2133 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2134 signed_type = parse_type (par_state)->builtin_int;
2136 else if (long_p <= 1
2137 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
2140 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
2141 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2142 signed_type = parse_type (par_state)->builtin_long;
2147 if (sizeof (ULONGEST) * HOST_CHAR_BIT
2148 < gdbarch_long_long_bit (par_state->gdbarch ()))
2149 /* A long long does not fit in a LONGEST. */
2150 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2152 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
2153 high_bit = (ULONGEST) 1 << shift;
2154 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2155 signed_type = parse_type (par_state)->builtin_long_long;
2158 putithere->typed_val_int.val = n;
2160 /* If the high bit of the worked out type is set then this number
2161 has to be unsigned. */
2163 if (unsigned_p || (n & high_bit))
2165 putithere->typed_val_int.type = unsigned_type;
2169 putithere->typed_val_int.type = signed_type;
2173 putithere->typed_val_int.type
2174 = init_complex_type (nullptr, putithere->typed_val_int.type);
2176 return imaginary_p ? COMPLEX_INT : INT;
2179 /* Temporary obstack used for holding strings. */
2180 static struct obstack tempbuf;
2181 static int tempbuf_init;
2183 /* Parse a C escape sequence. The initial backslash of the sequence
2184 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2185 last character of the sequence. If OUTPUT is not NULL, the
2186 translated form of the escape sequence will be written there. If
2187 OUTPUT is NULL, no output is written and the call will only affect
2188 *PTR. If an escape sequence is expressed in target bytes, then the
2189 entire sequence will simply be copied to OUTPUT. Return 1 if any
2190 character was emitted, 0 otherwise. */
2193 c_parse_escape (const char **ptr, struct obstack *output)
2195 const char *tokptr = *ptr;
2198 /* Some escape sequences undergo character set conversion. Those we
2202 /* Hex escapes do not undergo character set conversion, so keep
2203 the escape sequence for later. */
2206 obstack_grow_str (output, "\\x");
2208 if (!ISXDIGIT (*tokptr))
2209 error (_("\\x escape without a following hex digit"));
2210 while (ISXDIGIT (*tokptr))
2213 obstack_1grow (output, *tokptr);
2218 /* Octal escapes do not undergo character set conversion, so
2219 keep the escape sequence for later. */
2231 obstack_grow_str (output, "\\");
2233 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
2237 obstack_1grow (output, *tokptr);
2243 /* We handle UCNs later. We could handle them here, but that
2244 would mean a spurious error in the case where the UCN could
2245 be converted to the target charset but not the host
2251 int i, len = c == 'U' ? 8 : 4;
2254 obstack_1grow (output, '\\');
2255 obstack_1grow (output, *tokptr);
2258 if (!ISXDIGIT (*tokptr))
2259 error (_("\\%c escape without a following hex digit"), c);
2260 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
2263 obstack_1grow (output, *tokptr);
2269 /* We must pass backslash through so that it does not
2270 cause quoting during the second expansion. */
2273 obstack_grow_str (output, "\\\\");
2277 /* Escapes which undergo conversion. */
2280 obstack_1grow (output, '\a');
2285 obstack_1grow (output, '\b');
2290 obstack_1grow (output, '\f');
2295 obstack_1grow (output, '\n');
2300 obstack_1grow (output, '\r');
2305 obstack_1grow (output, '\t');
2310 obstack_1grow (output, '\v');
2314 /* GCC extension. */
2317 obstack_1grow (output, HOST_ESCAPE_CHAR);
2321 /* Backslash-newline expands to nothing at all. */
2327 /* A few escapes just expand to the character itself. */
2331 /* GCC extensions. */
2336 /* Unrecognized escapes turn into the character itself. */
2339 obstack_1grow (output, *tokptr);
2347 /* Parse a string or character literal from TOKPTR. The string or
2348 character may be wide or unicode. *OUTPTR is set to just after the
2349 end of the literal in the input string. The resulting token is
2350 stored in VALUE. This returns a token value, either STRING or
2351 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2352 number of host characters in the literal. */
2355 parse_string_or_char (const char *tokptr, const char **outptr,
2356 struct typed_stoken *value, int *host_chars)
2362 /* Build the gdb internal form of the input string in tempbuf. Note
2363 that the buffer is null byte terminated *only* for the
2364 convenience of debugging gdb itself and printing the buffer
2365 contents when the buffer contains no embedded nulls. Gdb does
2366 not depend upon the buffer being null byte terminated, it uses
2367 the length string instead. This allows gdb to handle C strings
2368 (as well as strings in other languages) with embedded null
2374 obstack_free (&tempbuf, NULL);
2375 obstack_init (&tempbuf);
2377 /* Record the string type. */
2380 type = C_WIDE_STRING;
2383 else if (*tokptr == 'u')
2388 else if (*tokptr == 'U')
2393 else if (*tokptr == '@')
2395 /* An Objective C string. */
2403 /* Skip the quote. */
2417 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2419 else if (c == quote)
2423 obstack_1grow (&tempbuf, c);
2425 /* FIXME: this does the wrong thing with multi-byte host
2426 characters. We could use mbrlen here, but that would
2427 make "set host-charset" a bit less useful. */
2432 if (*tokptr != quote)
2435 error (_("Unterminated string in expression."));
2437 error (_("Unmatched single quote."));
2442 value->ptr = (char *) obstack_base (&tempbuf);
2443 value->length = obstack_object_size (&tempbuf);
2447 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2450 /* This is used to associate some attributes with a token. */
2454 /* If this bit is set, the token is C++-only. */
2458 /* If this bit is set, the token is C-only. */
2462 /* If this bit is set, the token is conditional: if there is a
2463 symbol of the same name, then the token is a symbol; otherwise,
2464 the token is a keyword. */
2468 DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
2474 enum exp_opcode opcode;
2478 static const struct token tokentab3[] =
2480 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2481 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2482 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2483 {"...", DOTDOTDOT, BINOP_END, 0}
2486 static const struct token tokentab2[] =
2488 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2489 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2490 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2491 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2492 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2493 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2494 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2495 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2496 {"++", INCREMENT, BINOP_END, 0},
2497 {"--", DECREMENT, BINOP_END, 0},
2498 {"->", ARROW, BINOP_END, 0},
2499 {"&&", ANDAND, BINOP_END, 0},
2500 {"||", OROR, BINOP_END, 0},
2501 /* "::" is *not* only C++: gdb overrides its meaning in several
2502 different ways, e.g., 'filename'::func, function::variable. */
2503 {"::", COLONCOLON, BINOP_END, 0},
2504 {"<<", LSH, BINOP_END, 0},
2505 {">>", RSH, BINOP_END, 0},
2506 {"==", EQUAL, BINOP_END, 0},
2507 {"!=", NOTEQUAL, BINOP_END, 0},
2508 {"<=", LEQ, BINOP_END, 0},
2509 {">=", GEQ, BINOP_END, 0},
2510 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2513 /* Identifier-like tokens. Only type-specifiers than can appear in
2514 multi-word type names (for example 'double' can appear in 'long
2515 double') need to be listed here. type-specifiers that are only ever
2516 single word (like 'char') are handled by the classify_name function. */
2517 static const struct token ident_tokens[] =
2519 {"unsigned", UNSIGNED, OP_NULL, 0},
2520 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2521 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2522 {"struct", STRUCT, OP_NULL, 0},
2523 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2524 {"sizeof", SIZEOF, OP_NULL, 0},
2525 {"_Alignof", ALIGNOF, OP_NULL, 0},
2526 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
2527 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2528 {"float", FLOAT_KEYWORD, OP_NULL, 0},
2529 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2530 {"class", CLASS, OP_NULL, FLAG_CXX},
2531 {"union", UNION, OP_NULL, 0},
2532 {"short", SHORT, OP_NULL, 0},
2533 {"const", CONST_KEYWORD, OP_NULL, 0},
2534 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2535 {"__restrict__", RESTRICT, OP_NULL, 0},
2536 {"__restrict", RESTRICT, OP_NULL, 0},
2537 {"_Atomic", ATOMIC, OP_NULL, 0},
2538 {"enum", ENUM, OP_NULL, 0},
2539 {"long", LONG, OP_NULL, 0},
2540 {"_Complex", COMPLEX, OP_NULL, 0},
2541 {"__complex__", COMPLEX, OP_NULL, 0},
2543 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2544 {"int", INT_KEYWORD, OP_NULL, 0},
2545 {"new", NEW, OP_NULL, FLAG_CXX},
2546 {"delete", DELETE, OP_NULL, FLAG_CXX},
2547 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2549 {"and", ANDAND, BINOP_END, FLAG_CXX},
2550 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2551 {"bitand", '&', OP_NULL, FLAG_CXX},
2552 {"bitor", '|', OP_NULL, FLAG_CXX},
2553 {"compl", '~', OP_NULL, FLAG_CXX},
2554 {"not", '!', OP_NULL, FLAG_CXX},
2555 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2556 {"or", OROR, BINOP_END, FLAG_CXX},
2557 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2558 {"xor", '^', OP_NULL, FLAG_CXX},
2559 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2561 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2562 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2563 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2564 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2566 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2567 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2568 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2569 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2570 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2572 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2577 scan_macro_expansion (const char *expansion)
2579 /* We'd better not be trying to push the stack twice. */
2580 gdb_assert (! cpstate->macro_original_text);
2582 /* Copy to the obstack. */
2583 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
2585 /* Save the old lexptr value, so we can return to it when we're done
2586 parsing the expanded text. */
2587 cpstate->macro_original_text = pstate->lexptr;
2588 pstate->lexptr = copy;
2592 scanning_macro_expansion (void)
2594 return cpstate->macro_original_text != 0;
2598 finished_macro_expansion (void)
2600 /* There'd better be something to pop back to. */
2601 gdb_assert (cpstate->macro_original_text);
2603 /* Pop back to the original text. */
2604 pstate->lexptr = cpstate->macro_original_text;
2605 cpstate->macro_original_text = 0;
2608 /* Return true iff the token represents a C++ cast operator. */
2611 is_cast_operator (const char *token, int len)
2613 return (! strncmp (token, "dynamic_cast", len)
2614 || ! strncmp (token, "static_cast", len)
2615 || ! strncmp (token, "reinterpret_cast", len)
2616 || ! strncmp (token, "const_cast", len));
2619 /* The scope used for macro expansion. */
2620 static struct macro_scope *expression_macro_scope;
2622 /* This is set if a NAME token appeared at the very end of the input
2623 string, with no whitespace separating the name from the EOF. This
2624 is used only when parsing to do field name completion. */
2625 static int saw_name_at_eof;
2627 /* This is set if the previously-returned token was a structure
2628 operator -- either '.' or ARROW. */
2629 static bool last_was_structop;
2631 /* Depth of parentheses. */
2632 static int paren_depth;
2634 /* Read one token, getting characters through lexptr. */
2637 lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
2642 const char *tokstart;
2643 bool saw_structop = last_was_structop;
2645 last_was_structop = false;
2646 *is_quoted_name = false;
2650 /* Check if this is a macro invocation that we need to expand. */
2651 if (! scanning_macro_expansion ())
2653 gdb::unique_xmalloc_ptr<char> expanded
2654 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
2656 if (expanded != nullptr)
2657 scan_macro_expansion (expanded.get ());
2660 pstate->prev_lexptr = pstate->lexptr;
2662 tokstart = pstate->lexptr;
2663 /* See if it is a special token of length 3. */
2664 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2665 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
2667 if ((tokentab3[i].flags & FLAG_CXX) != 0
2668 && par_state->language ()->la_language != language_cplus)
2670 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
2672 pstate->lexptr += 3;
2673 yylval.opcode = tokentab3[i].opcode;
2674 return tokentab3[i].token;
2677 /* See if it is a special token of length 2. */
2678 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2679 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
2681 if ((tokentab2[i].flags & FLAG_CXX) != 0
2682 && par_state->language ()->la_language != language_cplus)
2684 gdb_assert ((tokentab2[i].flags & FLAG_C) == 0);
2686 pstate->lexptr += 2;
2687 yylval.opcode = tokentab2[i].opcode;
2688 if (tokentab2[i].token == ARROW)
2689 last_was_structop = 1;
2690 return tokentab2[i].token;
2693 switch (c = *tokstart)
2696 /* If we were just scanning the result of a macro expansion,
2697 then we need to resume scanning the original text.
2698 If we're parsing for field name completion, and the previous
2699 token allows such completion, return a COMPLETE token.
2700 Otherwise, we were already scanning the original text, and
2701 we're really done. */
2702 if (scanning_macro_expansion ())
2704 finished_macro_expansion ();
2707 else if (saw_name_at_eof)
2709 saw_name_at_eof = 0;
2712 else if (par_state->parse_completion && saw_structop)
2727 if (par_state->language ()->la_language == language_objc
2734 if (paren_depth == 0)
2741 if (pstate->comma_terminates
2743 && ! scanning_macro_expansion ())
2749 /* Might be a floating point number. */
2750 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
2752 last_was_structop = true;
2753 goto symbol; /* Nope, must be a symbol. */
2768 /* It's a number. */
2769 int got_dot = 0, got_e = 0, got_p = 0, toktype;
2770 const char *p = tokstart;
2771 int hex = input_radix > 10;
2773 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2778 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2786 /* This test includes !hex because 'e' is a valid hex digit
2787 and thus does not indicate a floating point number when
2788 the radix is hex. */
2789 if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
2790 got_dot = got_e = 1;
2791 else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
2792 got_dot = got_p = 1;
2793 /* This test does not include !hex, because a '.' always indicates
2794 a decimal floating point number regardless of the radix. */
2795 else if (!got_dot && *p == '.')
2797 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
2798 || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
2799 && (*p == '-' || *p == '+'))
2800 /* This is the sign of the exponent, not the end of the
2803 /* We will take any letters or digits. parse_number will
2804 complain if past the radix, or if L or U are not final. */
2805 else if ((*p < '0' || *p > '9')
2806 && ((*p < 'a' || *p > 'z')
2807 && (*p < 'A' || *p > 'Z')))
2810 toktype = parse_number (par_state, tokstart, p - tokstart,
2811 got_dot | got_e | got_p, &yylval);
2812 if (toktype == ERROR)
2814 char *err_copy = (char *) alloca (p - tokstart + 1);
2816 memcpy (err_copy, tokstart, p - tokstart);
2817 err_copy[p - tokstart] = 0;
2818 error (_("Invalid number \"%s\"."), err_copy);
2826 const char *p = &tokstart[1];
2828 if (par_state->language ()->la_language == language_objc)
2830 size_t len = strlen ("selector");
2832 if (strncmp (p, "selector", len) == 0
2833 && (p[len] == '\0' || ISSPACE (p[len])))
2835 pstate->lexptr = p + len;
2842 while (ISSPACE (*p))
2844 size_t len = strlen ("entry");
2845 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
2848 pstate->lexptr = &p[len];
2877 if (tokstart[1] != '"' && tokstart[1] != '\'')
2886 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2887 &yylval.tsval, &host_len);
2891 error (_("Empty character constant."));
2892 else if (host_len > 2 && c == '\'')
2895 namelen = pstate->lexptr - tokstart - 1;
2896 *is_quoted_name = true;
2900 else if (host_len > 1)
2901 error (_("Invalid character constant."));
2907 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
2908 /* We must have come across a bad character (e.g. ';'). */
2909 error (_("Invalid character '%c' in expression."), c);
2911 /* It's a name. See how long it is. */
2913 for (c = tokstart[namelen];
2914 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
2916 /* Template parameter lists are part of the name.
2917 FIXME: This mishandles `print $a<4&&$a>3'. */
2921 if (! is_cast_operator (tokstart, namelen))
2923 /* Scan ahead to get rest of the template specification. Note
2924 that we look ahead only when the '<' adjoins non-whitespace
2925 characters; for comparison expressions, e.g. "a < b > c",
2926 there must be spaces before the '<', etc. */
2927 const char *p = find_template_name_end (tokstart + namelen);
2930 namelen = p - tokstart;
2934 c = tokstart[++namelen];
2937 /* The token "if" terminates the expression and is NOT removed from
2938 the input stream. It doesn't count if it appears in the
2939 expansion of a macro. */
2941 && tokstart[0] == 'i'
2942 && tokstart[1] == 'f'
2943 && ! scanning_macro_expansion ())
2948 /* For the same reason (breakpoint conditions), "thread N"
2949 terminates the expression. "thread" could be an identifier, but
2950 an identifier is never followed by a number without intervening
2951 punctuation. "task" is similar. Handle abbreviations of these,
2952 similarly to breakpoint.c:find_condition_and_thread. */
2954 && (strncmp (tokstart, "thread", namelen) == 0
2955 || strncmp (tokstart, "task", namelen) == 0)
2956 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2957 && ! scanning_macro_expansion ())
2959 const char *p = tokstart + namelen + 1;
2961 while (*p == ' ' || *p == '\t')
2963 if (*p >= '0' && *p <= '9')
2967 pstate->lexptr += namelen;
2971 yylval.sval.ptr = tokstart;
2972 yylval.sval.length = namelen;
2974 /* Catch specific keywords. */
2975 std::string copy = copy_name (yylval.sval);
2976 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2977 if (copy == ident_tokens[i].oper)
2979 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2980 && par_state->language ()->la_language != language_cplus)
2982 if ((ident_tokens[i].flags & FLAG_C) != 0
2983 && par_state->language ()->la_language != language_c
2984 && par_state->language ()->la_language != language_objc)
2987 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2989 struct field_of_this_result is_a_field_of_this;
2991 if (lookup_symbol (copy.c_str (),
2992 pstate->expression_context_block,
2994 (par_state->language ()->la_language
2995 == language_cplus ? &is_a_field_of_this
2999 /* The keyword is shadowed. */
3004 /* It is ok to always set this, even though we don't always
3005 strictly need to. */
3006 yylval.opcode = ident_tokens[i].opcode;
3007 return ident_tokens[i].token;
3010 if (*tokstart == '$')
3011 return DOLLAR_VARIABLE;
3013 if (pstate->parse_completion && *pstate->lexptr == '\0')
3014 saw_name_at_eof = 1;
3016 yylval.ssym.stoken = yylval.sval;
3017 yylval.ssym.sym.symbol = NULL;
3018 yylval.ssym.sym.block = NULL;
3019 yylval.ssym.is_a_field_of_this = 0;
3023 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
3024 struct token_and_value
3030 /* A FIFO of tokens that have been read but not yet returned to the
3032 static std::vector<token_and_value> token_fifo;
3034 /* Non-zero if the lexer should return tokens from the FIFO. */
3037 /* Temporary storage for c_lex; this holds symbol names as they are
3039 static auto_obstack name_obstack;
3041 /* Classify a NAME token. The contents of the token are in `yylval'.
3042 Updates yylval and returns the new token type. BLOCK is the block
3043 in which lookups start; this can be NULL to mean the global scope.
3044 IS_QUOTED_NAME is non-zero if the name token was originally quoted
3045 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3046 a structure operator -- either '.' or ARROW */
3049 classify_name (struct parser_state *par_state, const struct block *block,
3050 bool is_quoted_name, bool is_after_structop)
3052 struct block_symbol bsym;
3053 struct field_of_this_result is_a_field_of_this;
3055 std::string copy = copy_name (yylval.sval);
3057 /* Initialize this in case we *don't* use it in this call; that way
3058 we can refer to it unconditionally below. */
3059 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3061 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
3062 par_state->language ()->name_of_this ()
3063 ? &is_a_field_of_this : NULL);
3065 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
3067 yylval.ssym.sym = bsym;
3068 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3071 else if (!bsym.symbol)
3073 /* If we found a field of 'this', we might have erroneously
3074 found a constructor where we wanted a type name. Handle this
3075 case by noticing that we found a constructor and then look up
3076 the type tag instead. */
3077 if (is_a_field_of_this.type != NULL
3078 && is_a_field_of_this.fn_field != NULL
3079 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3082 struct field_of_this_result inner_is_a_field_of_this;
3084 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
3085 &inner_is_a_field_of_this);
3086 if (bsym.symbol != NULL)
3088 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3093 /* If we found a field on the "this" object, or we are looking
3094 up a field on a struct, then we want to prefer it over a
3095 filename. However, if the name was quoted, then it is better
3096 to check for a filename or a block, since this is the only
3097 way the user has of requiring the extension to be used. */
3098 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3101 /* See if it's a file name. */
3102 struct symtab *symtab;
3104 symtab = lookup_symtab (copy.c_str ());
3107 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
3114 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
3116 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
3120 /* See if it's an ObjC classname. */
3121 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
3123 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3129 yylval.theclass.theclass = Class;
3130 sym = lookup_struct_typedef (copy.c_str (),
3131 par_state->expression_context_block, 1);
3133 yylval.theclass.type = SYMBOL_TYPE (sym);
3138 /* Input names that aren't symbols but ARE valid hex numbers, when
3139 the input radix permits them, can be names or numbers depending
3140 on the parse. Note we support radixes > 16 here. */
3142 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3143 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3145 YYSTYPE newlval; /* Its value is ignored. */
3146 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
3151 yylval.ssym.sym = bsym;
3152 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3157 /* Any other kind of symbol */
3158 yylval.ssym.sym = bsym;
3159 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
3161 if (bsym.symbol == NULL
3162 && par_state->language ()->la_language == language_cplus
3163 && is_a_field_of_this.type == NULL
3164 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
3165 return UNKNOWN_CPP_NAME;
3170 /* Like classify_name, but used by the inner loop of the lexer, when a
3171 name might have already been seen. CONTEXT is the context type, or
3172 NULL if this is the first component of a name. */
3175 classify_inner_name (struct parser_state *par_state,
3176 const struct block *block, struct type *context)
3180 if (context == NULL)
3181 return classify_name (par_state, block, false, false);
3183 type = check_typedef (context);
3184 if (!type_aggregate_p (type))
3187 std::string copy = copy_name (yylval.ssym.stoken);
3188 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3189 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3192 /* If no symbol was found, search for a matching base class named
3193 COPY. This will allow users to enter qualified names of class members
3194 relative to the `this' pointer. */
3195 if (yylval.ssym.sym.symbol == NULL)
3197 struct type *base_type = cp_find_type_baseclass_by_name (type,
3200 if (base_type != NULL)
3202 yylval.tsym.type = base_type;
3209 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
3213 /* cp_lookup_nested_symbol might have accidentally found a constructor
3214 named COPY when we really wanted a base class of the same name.
3215 Double-check this case by looking for a base class. */
3217 struct type *base_type
3218 = cp_find_type_baseclass_by_name (type, copy.c_str ());
3220 if (base_type != NULL)
3222 yylval.tsym.type = base_type;
3229 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
3235 internal_error (__FILE__, __LINE__, _("not reached"));
3238 /* The outer level of a two-level lexer. This calls the inner lexer
3239 to return tokens. It then either returns these tokens, or
3240 aggregates them into a larger token. This lets us work around a
3241 problem in our parsing approach, where the parser could not
3242 distinguish between qualified names and qualified types at the
3245 This approach is still not ideal, because it mishandles template
3246 types. See the comment in lex_one_token for an example. However,
3247 this is still an improvement over the earlier approach, and will
3248 suffice until we move to better parsing technology. */
3253 token_and_value current;
3254 int first_was_coloncolon, last_was_coloncolon;
3255 struct type *context_type = NULL;
3256 int last_to_examine, next_to_examine, checkpoint;
3257 const struct block *search_block;
3258 bool is_quoted_name, last_lex_was_structop;
3260 if (popping && !token_fifo.empty ())
3264 last_lex_was_structop = last_was_structop;
3266 /* Read the first token and decide what to do. Most of the
3267 subsequent code is C++-only; but also depends on seeing a "::" or
3269 current.token = lex_one_token (pstate, &is_quoted_name);
3270 if (current.token == NAME)
3271 current.token = classify_name (pstate, pstate->expression_context_block,
3272 is_quoted_name, last_lex_was_structop);
3273 if (pstate->language ()->la_language != language_cplus
3274 || (current.token != TYPENAME && current.token != COLONCOLON
3275 && current.token != FILENAME))
3276 return current.token;
3278 /* Read any sequence of alternating "::" and name-like tokens into
3280 current.value = yylval;
3281 token_fifo.push_back (current);
3282 last_was_coloncolon = current.token == COLONCOLON;
3287 /* We ignore quoted names other than the very first one.
3288 Subsequent ones do not have any special meaning. */
3289 current.token = lex_one_token (pstate, &ignore);
3290 current.value = yylval;
3291 token_fifo.push_back (current);
3293 if ((last_was_coloncolon && current.token != NAME)
3294 || (!last_was_coloncolon && current.token != COLONCOLON))
3296 last_was_coloncolon = !last_was_coloncolon;
3300 /* We always read one extra token, so compute the number of tokens
3301 to examine accordingly. */
3302 last_to_examine = token_fifo.size () - 2;
3303 next_to_examine = 0;
3305 current = token_fifo[next_to_examine];
3308 name_obstack.clear ();
3310 if (current.token == FILENAME)
3311 search_block = current.value.bval;
3312 else if (current.token == COLONCOLON)
3313 search_block = NULL;
3316 gdb_assert (current.token == TYPENAME);
3317 search_block = pstate->expression_context_block;
3318 obstack_grow (&name_obstack, current.value.sval.ptr,
3319 current.value.sval.length);
3320 context_type = current.value.tsym.type;
3324 first_was_coloncolon = current.token == COLONCOLON;
3325 last_was_coloncolon = first_was_coloncolon;
3327 while (next_to_examine <= last_to_examine)
3329 token_and_value next;
3331 next = token_fifo[next_to_examine];
3334 if (next.token == NAME && last_was_coloncolon)
3338 yylval = next.value;
3339 classification = classify_inner_name (pstate, search_block,
3341 /* We keep going until we either run out of names, or until
3342 we have a qualified name which is not a type. */
3343 if (classification != TYPENAME && classification != NAME)
3346 /* Accept up to this token. */
3347 checkpoint = next_to_examine;
3349 /* Update the partial name we are constructing. */
3350 if (context_type != NULL)
3352 /* We don't want to put a leading "::" into the name. */
3353 obstack_grow_str (&name_obstack, "::");
3355 obstack_grow (&name_obstack, next.value.sval.ptr,
3356 next.value.sval.length);
3358 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
3359 yylval.sval.length = obstack_object_size (&name_obstack);
3360 current.value = yylval;
3361 current.token = classification;
3363 last_was_coloncolon = 0;
3365 if (classification == NAME)
3368 context_type = yylval.tsym.type;
3370 else if (next.token == COLONCOLON && !last_was_coloncolon)
3371 last_was_coloncolon = 1;
3374 /* We've reached the end of the name. */
3379 /* If we have a replacement token, install it as the first token in
3380 the FIFO, and delete the other constituent tokens. */
3383 current.value.sval.ptr
3384 = obstack_strndup (&cpstate->expansion_obstack,
3385 current.value.sval.ptr,
3386 current.value.sval.length);
3388 token_fifo[0] = current;
3390 token_fifo.erase (token_fifo.begin () + 1,
3391 token_fifo.begin () + checkpoint);
3395 current = token_fifo[0];
3396 token_fifo.erase (token_fifo.begin ());
3397 yylval = current.value;
3398 return current.token;
3402 c_parse (struct parser_state *par_state)
3404 /* Setting up the parser state. */
3405 scoped_restore pstate_restore = make_scoped_restore (&pstate);
3406 gdb_assert (par_state != NULL);
3409 c_parse_state cstate;
3410 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3412 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
3414 if (par_state->expression_context_block)
3416 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
3418 macro_scope = default_macro_scope ();
3420 macro_scope = user_macro_scope ();
3422 scoped_restore restore_macro_scope
3423 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
3425 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3428 /* Initialize some state used by the lexer. */
3429 last_was_structop = false;
3430 saw_name_at_eof = 0;
3433 token_fifo.clear ();
3435 name_obstack.clear ();
3437 int result = yyparse ();
3439 pstate->set_operation (pstate->pop ());
3445 /* This is called via the YYPRINT macro when parser debugging is
3446 enabled. It prints a token's value. */
3449 c_print_token (FILE *file, int type, YYSTYPE value)
3454 parser_fprintf (file, "typed_val_int<%s, %s>",
3455 TYPE_SAFE_NAME (value.typed_val_int.type),
3456 pulongest (value.typed_val_int.val));
3462 char *copy = (char *) alloca (value.tsval.length + 1);
3464 memcpy (copy, value.tsval.ptr, value.tsval.length);
3465 copy[value.tsval.length] = '\0';
3467 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3472 case DOLLAR_VARIABLE:
3473 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
3477 parser_fprintf (file, "tsym<type=%s, name=%s>",
3478 TYPE_SAFE_NAME (value.tsym.type),
3479 copy_name (value.tsym.stoken).c_str ());
3483 case UNKNOWN_CPP_NAME:
3486 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3487 copy_name (value.ssym.stoken).c_str (),
3488 (value.ssym.sym.symbol == NULL
3489 ? "(null)" : value.ssym.sym.symbol->print_name ()),
3490 value.ssym.is_a_field_of_this);
3494 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3502 yyerror (const char *msg)
3504 if (pstate->prev_lexptr)
3505 pstate->lexptr = pstate->prev_lexptr;
3507 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);