1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986-2014 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. */
41 #include "expression.h"
43 #include "parser-defs.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"
53 #include "gdb_assert.h"
54 #include "macroscope.h"
55 #include "objc-lang.h"
56 #include "typeprint.h"
59 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
62 as well as gratuitiously global symbol names, so we can have multiple
63 yacc generated parsers in gdb. Note that these are only the variables
64 produced by yacc. If other parser generators (bison, byacc, etc) produce
65 additional global names that conflict at link time, then those parser
66 generators need to be fixed instead of adding those names to this list. */
68 #define yymaxdepth c_maxdepth
69 #define yyparse c_parse_internal
71 #define yyerror c_error
74 #define yydebug c_debug
83 #define yyerrflag c_errflag
84 #define yynerrs c_nerrs
89 #define yystate c_state
95 #define yyreds c_reds /* With YYDEBUG defined */
96 #define yytoks c_toks /* With YYDEBUG defined */
97 #define yyname c_name /* With YYDEBUG defined */
98 #define yyrule c_rule /* With YYDEBUG defined */
100 #define yylen c_yylen
101 #define yydefred c_yydefred
102 #define yydgoto c_yydgoto
103 #define yysindex c_yysindex
104 #define yyrindex c_yyrindex
105 #define yygindex c_yygindex
106 #define yytable c_yytable
107 #define yycheck c_yycheck
109 #define yysslim c_yysslim
110 #define yyssp c_yyssp
111 #define yystacksize c_yystacksize
113 #define yyvsp c_yyvsp
116 #define YYDEBUG 1 /* Default to yydebug support */
119 #define YYFPRINTF parser_fprintf
121 /* The state of the parser, used internally when we are parsing the
124 static struct parser_state *pstate = NULL;
128 static int yylex (void);
130 void yyerror (char *);
132 static int type_aggregate_p (struct type *);
136 /* Although the yacc "value" of an expression is not used,
137 since the result is stored in the structure being created,
138 other node types do have values. */
154 } typed_val_decfloat;
157 struct typed_stoken tsval;
159 struct symtoken ssym;
162 enum exp_opcode opcode;
164 struct stoken_vector svec;
165 VEC (type_ptr) *tvec;
167 struct type_stack *type_stack;
169 struct objc_class_str class;
173 /* YYSTYPE gets defined by %union */
174 static int parse_number (struct parser_state *par_state,
175 const char *, int, int, YYSTYPE *);
176 static struct stoken operator_stoken (const char *);
177 static void check_parameter_typelist (VEC (type_ptr) *);
178 static void write_destructor_name (struct parser_state *par_state,
182 static void c_print_token (FILE *file, int type, YYSTYPE value);
183 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
187 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
189 %type <tval> type typebase
190 %type <tvec> nonempty_typelist func_mod parameter_typelist
191 /* %type <bval> block */
193 /* Fancy type parsing. */
195 %type <lval> array_mod
196 %type <tval> conversion_type_id
198 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
200 %token <typed_val_int> INT
201 %token <typed_val_float> FLOAT
202 %token <typed_val_decfloat> DECFLOAT
204 /* Both NAME and TYPENAME tokens represent symbols in the input,
205 and both convey their data as strings.
206 But a TYPENAME is a string that happens to be defined as a typedef
207 or builtin type name (such as int or char)
208 and a NAME is any other symbol.
209 Contexts where this distinction is not important can use the
210 nonterminal "name", which matches either NAME or TYPENAME. */
212 %token <tsval> STRING
213 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
214 %token SELECTOR /* ObjC "@selector" pseudo-operator */
216 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
217 %token <ssym> UNKNOWN_CPP_NAME
218 %token <voidval> COMPLETE
219 %token <tsym> TYPENAME
220 %token <class> CLASSNAME /* ObjC Class name */
222 %type <svec> string_exp
223 %type <ssym> name_not_typename
224 %type <tsym> typename
226 /* This is like a '[' token, but is only generated when parsing
227 Objective C. This lets us reuse the same parser without
228 erroneously parsing ObjC-specific expressions in C. */
231 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
232 but which would parse as a valid number in the current input radix.
233 E.g. "c" when input_radix==16. Depending on the parse, it will be
234 turned into a name or into a number. */
236 %token <ssym> NAME_OR_INT
239 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
243 %type <sval> operator
244 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
250 /* Special type cases, put in to allow the parser to distinguish different
252 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
254 %token <sval> VARIABLE
256 %token <opcode> ASSIGN_MODIFY
265 %right '=' ASSIGN_MODIFY
273 %left '<' '>' LEQ GEQ
278 %right UNARY INCREMENT DECREMENT
279 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
280 %token <ssym> BLOCKNAME
281 %token <bval> FILENAME
295 { write_exp_elt_opcode(pstate, OP_TYPE);
296 write_exp_elt_type(pstate, $1);
297 write_exp_elt_opcode(pstate, OP_TYPE);}
300 write_exp_elt_opcode (pstate, OP_TYPEOF);
302 | TYPEOF '(' type ')'
304 write_exp_elt_opcode (pstate, OP_TYPE);
305 write_exp_elt_type (pstate, $3);
306 write_exp_elt_opcode (pstate, OP_TYPE);
308 | DECLTYPE '(' exp ')'
310 write_exp_elt_opcode (pstate, OP_DECLTYPE);
314 /* Expressions, including the comma operator. */
317 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
320 /* Expressions, not including the comma operator. */
321 exp : '*' exp %prec UNARY
322 { write_exp_elt_opcode (pstate, UNOP_IND); }
325 exp : '&' exp %prec UNARY
326 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
329 exp : '-' exp %prec UNARY
330 { write_exp_elt_opcode (pstate, UNOP_NEG); }
333 exp : '+' exp %prec UNARY
334 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
337 exp : '!' exp %prec UNARY
338 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
341 exp : '~' exp %prec UNARY
342 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
345 exp : INCREMENT exp %prec UNARY
346 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
349 exp : DECREMENT exp %prec UNARY
350 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
353 exp : exp INCREMENT %prec UNARY
354 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
357 exp : exp DECREMENT %prec UNARY
358 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
361 exp : TYPEID '(' exp ')' %prec UNARY
362 { write_exp_elt_opcode (pstate, OP_TYPEID); }
365 exp : TYPEID '(' type_exp ')' %prec UNARY
366 { write_exp_elt_opcode (pstate, OP_TYPEID); }
369 exp : SIZEOF exp %prec UNARY
370 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
374 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
375 write_exp_string (pstate, $3);
376 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
379 exp : exp ARROW name COMPLETE
380 { mark_struct_expression (pstate);
381 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
382 write_exp_string (pstate, $3);
383 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
386 exp : exp ARROW COMPLETE
388 mark_struct_expression (pstate);
389 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
392 write_exp_string (pstate, s);
393 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
396 exp : exp ARROW '~' name
397 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
398 write_destructor_name (pstate, $4);
399 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
402 exp : exp ARROW '~' name COMPLETE
403 { mark_struct_expression (pstate);
404 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
405 write_destructor_name (pstate, $4);
406 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
409 exp : exp ARROW qualified_name
410 { /* exp->type::name becomes exp->*(&type::name) */
411 /* Note: this doesn't work if name is a
412 static member! FIXME */
413 write_exp_elt_opcode (pstate, UNOP_ADDR);
414 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
417 exp : exp ARROW_STAR exp
418 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
422 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
423 write_exp_string (pstate, $3);
424 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
427 exp : exp '.' name COMPLETE
428 { mark_struct_expression (pstate);
429 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
430 write_exp_string (pstate, $3);
431 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
434 exp : exp '.' COMPLETE
436 mark_struct_expression (pstate);
437 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
440 write_exp_string (pstate, s);
441 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
444 exp : exp '.' '~' name
445 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
446 write_destructor_name (pstate, $4);
447 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
450 exp : exp '.' '~' name COMPLETE
451 { mark_struct_expression (pstate);
452 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
453 write_destructor_name (pstate, $4);
454 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
457 exp : exp '.' qualified_name
458 { /* exp.type::name becomes exp.*(&type::name) */
459 /* Note: this doesn't work if name is a
460 static member! FIXME */
461 write_exp_elt_opcode (pstate, UNOP_ADDR);
462 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
465 exp : exp DOT_STAR exp
466 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
469 exp : exp '[' exp1 ']'
470 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
473 exp : exp OBJC_LBRAC exp1 ']'
474 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
478 * The rules below parse ObjC message calls of the form:
479 * '[' target selector {':' argument}* ']'
482 exp : OBJC_LBRAC TYPENAME
486 class = lookup_objc_class (parse_gdbarch (pstate),
487 copy_name ($2.stoken));
489 error (_("%s is not an ObjC Class"),
490 copy_name ($2.stoken));
491 write_exp_elt_opcode (pstate, OP_LONG);
492 write_exp_elt_type (pstate,
493 parse_type (pstate)->builtin_int);
494 write_exp_elt_longcst (pstate, (LONGEST) class);
495 write_exp_elt_opcode (pstate, OP_LONG);
499 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
500 end_msglist (pstate);
501 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
505 exp : OBJC_LBRAC CLASSNAME
507 write_exp_elt_opcode (pstate, OP_LONG);
508 write_exp_elt_type (pstate,
509 parse_type (pstate)->builtin_int);
510 write_exp_elt_longcst (pstate, (LONGEST) $2.class);
511 write_exp_elt_opcode (pstate, OP_LONG);
515 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
516 end_msglist (pstate);
517 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
524 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
525 end_msglist (pstate);
526 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
531 { add_msglist(&$1, 0); }
539 msgarg : name ':' exp
540 { add_msglist(&$1, 1); }
541 | ':' exp /* Unnamed arg. */
542 { add_msglist(0, 1); }
543 | ',' exp /* Variable number of args. */
544 { add_msglist(0, 0); }
548 /* This is to save the value of arglist_len
549 being accumulated by an outer function call. */
550 { start_arglist (); }
551 arglist ')' %prec ARROW
552 { write_exp_elt_opcode (pstate, OP_FUNCALL);
553 write_exp_elt_longcst (pstate,
554 (LONGEST) end_arglist ());
555 write_exp_elt_opcode (pstate, OP_FUNCALL); }
558 exp : UNKNOWN_CPP_NAME '('
560 /* This could potentially be a an argument defined
561 lookup function (Koenig). */
562 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
563 write_exp_elt_block (pstate,
564 expression_context_block);
565 write_exp_elt_sym (pstate,
566 NULL); /* Placeholder. */
567 write_exp_string (pstate, $1.stoken);
568 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
570 /* This is to save the value of arglist_len
571 being accumulated by an outer function call. */
575 arglist ')' %prec ARROW
577 write_exp_elt_opcode (pstate, OP_FUNCALL);
578 write_exp_elt_longcst (pstate,
579 (LONGEST) end_arglist ());
580 write_exp_elt_opcode (pstate, OP_FUNCALL);
585 { start_arglist (); }
595 arglist : arglist ',' exp %prec ABOVE_COMMA
599 exp : exp '(' parameter_typelist ')' const_or_volatile
601 VEC (type_ptr) *type_list = $3;
602 struct type *type_elt;
603 LONGEST len = VEC_length (type_ptr, type_list);
605 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
606 write_exp_elt_longcst (pstate, len);
608 VEC_iterate (type_ptr, type_list, i, type_elt);
610 write_exp_elt_type (pstate, type_elt);
611 write_exp_elt_longcst(pstate, len);
612 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
613 VEC_free (type_ptr, type_list);
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_DOUBLE);
763 write_exp_elt_type (pstate, $1.type);
764 write_exp_elt_dblcst (pstate, $1.dval);
765 write_exp_elt_opcode (pstate, OP_DOUBLE); }
769 { write_exp_elt_opcode (pstate, OP_DECFLOAT);
770 write_exp_elt_type (pstate, $1.type);
771 write_exp_elt_decfloatcst (pstate, $1.val);
772 write_exp_elt_opcode (pstate, OP_DECFLOAT); }
780 write_dollar_variable (pstate, $1);
784 exp : SELECTOR '(' name ')'
786 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
787 write_exp_string (pstate, $3);
788 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
791 exp : SIZEOF '(' type ')' %prec UNARY
792 { struct type *type = $3;
793 write_exp_elt_opcode (pstate, OP_LONG);
794 write_exp_elt_type (pstate, lookup_signed_typename
795 (parse_language (pstate),
796 parse_gdbarch (pstate),
798 CHECK_TYPEDEF (type);
800 /* $5.3.3/2 of the C++ Standard (n3290 draft)
801 says of sizeof: "When applied to a reference
802 or a reference type, the result is the size of
803 the referenced type." */
804 if (TYPE_CODE (type) == TYPE_CODE_REF)
805 type = check_typedef (TYPE_TARGET_TYPE (type));
806 write_exp_elt_longcst (pstate,
807 (LONGEST) TYPE_LENGTH (type));
808 write_exp_elt_opcode (pstate, OP_LONG); }
811 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
812 { write_exp_elt_opcode (pstate,
813 UNOP_REINTERPRET_CAST); }
816 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
817 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
820 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
821 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
824 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
825 { /* We could do more error checking here, but
826 it doesn't seem worthwhile. */
827 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
833 /* We copy the string here, and not in the
834 lexer, to guarantee that we do not leak a
835 string. Note that we follow the
836 NUL-termination convention of the
838 struct typed_stoken *vec = XNEW (struct typed_stoken);
843 vec->length = $1.length;
844 vec->ptr = malloc ($1.length + 1);
845 memcpy (vec->ptr, $1.ptr, $1.length + 1);
850 /* Note that we NUL-terminate here, but just
854 $$.tokens = realloc ($$.tokens,
855 $$.len * sizeof (struct typed_stoken));
857 p = malloc ($2.length + 1);
858 memcpy (p, $2.ptr, $2.length + 1);
860 $$.tokens[$$.len - 1].type = $2.type;
861 $$.tokens[$$.len - 1].length = $2.length;
862 $$.tokens[$$.len - 1].ptr = p;
869 enum c_string_type type = C_STRING;
871 for (i = 0; i < $1.len; ++i)
873 switch ($1.tokens[i].type)
881 && type != $1.tokens[i].type)
882 error (_("Undefined string concatenation."));
883 type = $1.tokens[i].type;
887 internal_error (__FILE__, __LINE__,
888 "unrecognized type in string concatenation");
892 write_exp_string_vector (pstate, type, &$1);
893 for (i = 0; i < $1.len; ++i)
894 free ($1.tokens[i].ptr);
899 exp : NSSTRING /* ObjC NextStep NSString constant
900 * of the form '@' '"' string '"'.
902 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
903 write_exp_string (pstate, $1);
904 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
909 { write_exp_elt_opcode (pstate, OP_LONG);
910 write_exp_elt_type (pstate,
911 parse_type (pstate)->builtin_bool);
912 write_exp_elt_longcst (pstate, (LONGEST) 1);
913 write_exp_elt_opcode (pstate, OP_LONG); }
917 { write_exp_elt_opcode (pstate, OP_LONG);
918 write_exp_elt_type (pstate,
919 parse_type (pstate)->builtin_bool);
920 write_exp_elt_longcst (pstate, (LONGEST) 0);
921 write_exp_elt_opcode (pstate, OP_LONG); }
929 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
931 error (_("No file or function \"%s\"."),
932 copy_name ($1.stoken));
940 block : block COLONCOLON name
942 = lookup_symbol (copy_name ($3), $1,
944 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
945 error (_("No function \"%s\" in specified context."),
947 $$ = SYMBOL_BLOCK_VALUE (tem); }
950 variable: name_not_typename ENTRY
951 { struct symbol *sym = $1.sym;
953 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
954 || !symbol_read_needs_frame (sym))
955 error (_("@entry can be used only for function "
956 "parameters, not for \"%s\""),
957 copy_name ($1.stoken));
959 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
960 write_exp_elt_sym (pstate, sym);
961 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
965 variable: block COLONCOLON name
966 { struct symbol *sym;
967 sym = lookup_symbol (copy_name ($3), $1,
970 error (_("No symbol \"%s\" in specified context."),
972 if (symbol_read_needs_frame (sym))
974 if (innermost_block == 0
975 || contained_in (block_found,
977 innermost_block = block_found;
980 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
981 /* block_found is set by lookup_symbol. */
982 write_exp_elt_block (pstate, block_found);
983 write_exp_elt_sym (pstate, sym);
984 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
987 qualified_name: TYPENAME COLONCOLON name
989 struct type *type = $1.type;
990 CHECK_TYPEDEF (type);
991 if (!type_aggregate_p (type))
992 error (_("`%s' is not defined as an aggregate type."),
993 TYPE_SAFE_NAME (type));
995 write_exp_elt_opcode (pstate, OP_SCOPE);
996 write_exp_elt_type (pstate, type);
997 write_exp_string (pstate, $3);
998 write_exp_elt_opcode (pstate, OP_SCOPE);
1000 | TYPENAME COLONCOLON '~' name
1002 struct type *type = $1.type;
1003 struct stoken tmp_token;
1006 CHECK_TYPEDEF (type);
1007 if (!type_aggregate_p (type))
1008 error (_("`%s' is not defined as an aggregate type."),
1009 TYPE_SAFE_NAME (type));
1010 buf = alloca ($4.length + 2);
1011 tmp_token.ptr = buf;
1012 tmp_token.length = $4.length + 1;
1014 memcpy (buf+1, $4.ptr, $4.length);
1015 buf[tmp_token.length] = 0;
1017 /* Check for valid destructor name. */
1018 destructor_name_p (tmp_token.ptr, $1.type);
1019 write_exp_elt_opcode (pstate, OP_SCOPE);
1020 write_exp_elt_type (pstate, type);
1021 write_exp_string (pstate, tmp_token);
1022 write_exp_elt_opcode (pstate, OP_SCOPE);
1024 | TYPENAME COLONCOLON name COLONCOLON name
1026 char *copy = copy_name ($3);
1027 error (_("No type \"%s\" within class "
1028 "or namespace \"%s\"."),
1029 copy, TYPE_SAFE_NAME ($1.type));
1033 variable: qualified_name
1034 | COLONCOLON name_not_typename
1036 char *name = copy_name ($2.stoken);
1038 struct bound_minimal_symbol msymbol;
1041 lookup_symbol (name, (const struct block *) NULL,
1045 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1046 write_exp_elt_block (pstate, NULL);
1047 write_exp_elt_sym (pstate, sym);
1048 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1052 msymbol = lookup_bound_minimal_symbol (name);
1053 if (msymbol.minsym != NULL)
1054 write_exp_msymbol (pstate, msymbol);
1055 else if (!have_full_symbols () && !have_partial_symbols ())
1056 error (_("No symbol table is loaded. Use the \"file\" command."));
1058 error (_("No symbol \"%s\" in current context."), name);
1062 variable: name_not_typename
1063 { struct symbol *sym = $1.sym;
1067 if (symbol_read_needs_frame (sym))
1069 if (innermost_block == 0
1070 || contained_in (block_found,
1072 innermost_block = block_found;
1075 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1076 /* We want to use the selected frame, not
1077 another more inner frame which happens to
1078 be in the same block. */
1079 write_exp_elt_block (pstate, NULL);
1080 write_exp_elt_sym (pstate, sym);
1081 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1083 else if ($1.is_a_field_of_this)
1085 /* C++: it hangs off of `this'. Must
1086 not inadvertently convert from a method call
1088 if (innermost_block == 0
1089 || contained_in (block_found,
1091 innermost_block = block_found;
1092 write_exp_elt_opcode (pstate, OP_THIS);
1093 write_exp_elt_opcode (pstate, OP_THIS);
1094 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1095 write_exp_string (pstate, $1.stoken);
1096 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1100 struct bound_minimal_symbol msymbol;
1101 char *arg = copy_name ($1.stoken);
1104 lookup_bound_minimal_symbol (arg);
1105 if (msymbol.minsym != NULL)
1106 write_exp_msymbol (pstate, msymbol);
1107 else if (!have_full_symbols () && !have_partial_symbols ())
1108 error (_("No symbol table is loaded. Use the \"file\" command."));
1110 error (_("No symbol \"%s\" in current context."),
1111 copy_name ($1.stoken));
1116 space_identifier : '@' NAME
1117 { insert_type_address_space (pstate, copy_name ($2.stoken)); }
1120 const_or_volatile: const_or_volatile_noopt
1124 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1127 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1128 | const_or_volatile_noopt
1131 const_or_volatile_or_space_identifier:
1132 const_or_volatile_or_space_identifier_noopt
1138 { insert_type (tp_pointer); }
1139 const_or_volatile_or_space_identifier
1141 { insert_type (tp_pointer); }
1142 const_or_volatile_or_space_identifier
1144 { insert_type (tp_reference); }
1146 { insert_type (tp_reference); }
1149 ptr_operator_ts: ptr_operator
1151 $$ = get_type_stack ();
1152 /* This cleanup is eventually run by
1154 make_cleanup (type_stack_cleanup, $$);
1158 abs_decl: ptr_operator_ts direct_abs_decl
1159 { $$ = append_type_stack ($2, $1); }
1164 direct_abs_decl: '(' abs_decl ')'
1166 | direct_abs_decl array_mod
1168 push_type_stack ($1);
1170 push_type (tp_array);
1171 $$ = get_type_stack ();
1176 push_type (tp_array);
1177 $$ = get_type_stack ();
1180 | direct_abs_decl func_mod
1182 push_type_stack ($1);
1184 $$ = get_type_stack ();
1189 $$ = get_type_stack ();
1199 | OBJC_LBRAC INT ']'
1205 | '(' parameter_typelist ')'
1209 /* We used to try to recognize pointer to member types here, but
1210 that didn't work (shift/reduce conflicts meant that these rules never
1211 got executed). The problem is that
1212 int (foo::bar::baz::bizzle)
1213 is a function type but
1214 int (foo::bar::baz::bizzle::*)
1215 is a pointer to member type. Stroustrup loses again! */
1220 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1224 { $$ = lookup_signed_typename (parse_language (pstate),
1225 parse_gdbarch (pstate),
1228 { $$ = lookup_signed_typename (parse_language (pstate),
1229 parse_gdbarch (pstate),
1232 { $$ = lookup_signed_typename (parse_language (pstate),
1233 parse_gdbarch (pstate),
1236 { $$ = lookup_signed_typename (parse_language (pstate),
1237 parse_gdbarch (pstate),
1239 | LONG SIGNED_KEYWORD INT_KEYWORD
1240 { $$ = lookup_signed_typename (parse_language (pstate),
1241 parse_gdbarch (pstate),
1243 | LONG SIGNED_KEYWORD
1244 { $$ = lookup_signed_typename (parse_language (pstate),
1245 parse_gdbarch (pstate),
1247 | SIGNED_KEYWORD LONG INT_KEYWORD
1248 { $$ = lookup_signed_typename (parse_language (pstate),
1249 parse_gdbarch (pstate),
1251 | UNSIGNED LONG INT_KEYWORD
1252 { $$ = lookup_unsigned_typename (parse_language (pstate),
1253 parse_gdbarch (pstate),
1255 | LONG UNSIGNED INT_KEYWORD
1256 { $$ = lookup_unsigned_typename (parse_language (pstate),
1257 parse_gdbarch (pstate),
1260 { $$ = lookup_unsigned_typename (parse_language (pstate),
1261 parse_gdbarch (pstate),
1264 { $$ = lookup_signed_typename (parse_language (pstate),
1265 parse_gdbarch (pstate),
1267 | LONG LONG INT_KEYWORD
1268 { $$ = lookup_signed_typename (parse_language (pstate),
1269 parse_gdbarch (pstate),
1271 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1272 { $$ = lookup_signed_typename (parse_language (pstate),
1273 parse_gdbarch (pstate),
1275 | LONG LONG SIGNED_KEYWORD
1276 { $$ = lookup_signed_typename (parse_language (pstate),
1277 parse_gdbarch (pstate),
1279 | SIGNED_KEYWORD LONG LONG
1280 { $$ = lookup_signed_typename (parse_language (pstate),
1281 parse_gdbarch (pstate),
1283 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1284 { $$ = lookup_signed_typename (parse_language (pstate),
1285 parse_gdbarch (pstate),
1287 | UNSIGNED LONG LONG
1288 { $$ = lookup_unsigned_typename (parse_language (pstate),
1289 parse_gdbarch (pstate),
1291 | UNSIGNED LONG LONG INT_KEYWORD
1292 { $$ = lookup_unsigned_typename (parse_language (pstate),
1293 parse_gdbarch (pstate),
1295 | LONG LONG UNSIGNED
1296 { $$ = lookup_unsigned_typename (parse_language (pstate),
1297 parse_gdbarch (pstate),
1299 | LONG LONG UNSIGNED INT_KEYWORD
1300 { $$ = lookup_unsigned_typename (parse_language (pstate),
1301 parse_gdbarch (pstate),
1304 { $$ = lookup_signed_typename (parse_language (pstate),
1305 parse_gdbarch (pstate),
1307 | SHORT SIGNED_KEYWORD INT_KEYWORD
1308 { $$ = lookup_signed_typename (parse_language (pstate),
1309 parse_gdbarch (pstate),
1311 | SHORT SIGNED_KEYWORD
1312 { $$ = lookup_signed_typename (parse_language (pstate),
1313 parse_gdbarch (pstate),
1315 | UNSIGNED SHORT INT_KEYWORD
1316 { $$ = lookup_unsigned_typename (parse_language (pstate),
1317 parse_gdbarch (pstate),
1320 { $$ = lookup_unsigned_typename (parse_language (pstate),
1321 parse_gdbarch (pstate),
1323 | SHORT UNSIGNED INT_KEYWORD
1324 { $$ = lookup_unsigned_typename (parse_language (pstate),
1325 parse_gdbarch (pstate),
1328 { $$ = lookup_typename (parse_language (pstate),
1329 parse_gdbarch (pstate),
1331 (struct block *) NULL,
1333 | LONG DOUBLE_KEYWORD
1334 { $$ = lookup_typename (parse_language (pstate),
1335 parse_gdbarch (pstate),
1337 (struct block *) NULL,
1340 { $$ = lookup_struct (copy_name ($2),
1341 expression_context_block); }
1344 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1347 | STRUCT name COMPLETE
1349 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1354 { $$ = lookup_struct (copy_name ($2),
1355 expression_context_block); }
1358 mark_completion_tag (TYPE_CODE_CLASS, "", 0);
1361 | CLASS name COMPLETE
1363 mark_completion_tag (TYPE_CODE_CLASS, $2.ptr,
1368 { $$ = lookup_union (copy_name ($2),
1369 expression_context_block); }
1372 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1375 | UNION name COMPLETE
1377 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1382 { $$ = lookup_enum (copy_name ($2),
1383 expression_context_block); }
1386 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1389 | ENUM name COMPLETE
1391 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1396 { $$ = lookup_unsigned_typename (parse_language (pstate),
1397 parse_gdbarch (pstate),
1398 TYPE_NAME($2.type)); }
1400 { $$ = lookup_unsigned_typename (parse_language (pstate),
1401 parse_gdbarch (pstate),
1403 | SIGNED_KEYWORD typename
1404 { $$ = lookup_signed_typename (parse_language (pstate),
1405 parse_gdbarch (pstate),
1406 TYPE_NAME($2.type)); }
1408 { $$ = lookup_signed_typename (parse_language (pstate),
1409 parse_gdbarch (pstate),
1411 /* It appears that this rule for templates is never
1412 reduced; template recognition happens by lookahead
1413 in the token processing code in yylex. */
1414 | TEMPLATE name '<' type '>'
1415 { $$ = lookup_template_type(copy_name($2), $4,
1416 expression_context_block);
1418 | const_or_volatile_or_space_identifier_noopt typebase
1419 { $$ = follow_types ($2); }
1420 | typebase const_or_volatile_or_space_identifier_noopt
1421 { $$ = follow_types ($1); }
1427 $$.stoken.ptr = "int";
1428 $$.stoken.length = 3;
1429 $$.type = lookup_signed_typename (parse_language (pstate),
1430 parse_gdbarch (pstate),
1435 $$.stoken.ptr = "long";
1436 $$.stoken.length = 4;
1437 $$.type = lookup_signed_typename (parse_language (pstate),
1438 parse_gdbarch (pstate),
1443 $$.stoken.ptr = "short";
1444 $$.stoken.length = 5;
1445 $$.type = lookup_signed_typename (parse_language (pstate),
1446 parse_gdbarch (pstate),
1453 { check_parameter_typelist ($1); }
1454 | nonempty_typelist ',' DOTDOTDOT
1456 VEC_safe_push (type_ptr, $1, NULL);
1457 check_parameter_typelist ($1);
1465 VEC (type_ptr) *typelist = NULL;
1466 VEC_safe_push (type_ptr, typelist, $1);
1469 | nonempty_typelist ',' type
1471 VEC_safe_push (type_ptr, $1, $3);
1479 push_type_stack ($2);
1480 $$ = follow_types ($1);
1484 conversion_type_id: typebase conversion_declarator
1485 { $$ = follow_types ($1); }
1488 conversion_declarator: /* Nothing. */
1489 | ptr_operator conversion_declarator
1492 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1493 | VOLATILE_KEYWORD CONST_KEYWORD
1496 const_or_volatile_noopt: const_and_volatile
1497 { insert_type (tp_const);
1498 insert_type (tp_volatile);
1501 { insert_type (tp_const); }
1503 { insert_type (tp_volatile); }
1506 operator: OPERATOR NEW
1507 { $$ = operator_stoken (" new"); }
1509 { $$ = operator_stoken (" delete"); }
1510 | OPERATOR NEW '[' ']'
1511 { $$ = operator_stoken (" new[]"); }
1512 | OPERATOR DELETE '[' ']'
1513 { $$ = operator_stoken (" delete[]"); }
1514 | OPERATOR NEW OBJC_LBRAC ']'
1515 { $$ = operator_stoken (" new[]"); }
1516 | OPERATOR DELETE OBJC_LBRAC ']'
1517 { $$ = operator_stoken (" delete[]"); }
1519 { $$ = operator_stoken ("+"); }
1521 { $$ = operator_stoken ("-"); }
1523 { $$ = operator_stoken ("*"); }
1525 { $$ = operator_stoken ("/"); }
1527 { $$ = operator_stoken ("%"); }
1529 { $$ = operator_stoken ("^"); }
1531 { $$ = operator_stoken ("&"); }
1533 { $$ = operator_stoken ("|"); }
1535 { $$ = operator_stoken ("~"); }
1537 { $$ = operator_stoken ("!"); }
1539 { $$ = operator_stoken ("="); }
1541 { $$ = operator_stoken ("<"); }
1543 { $$ = operator_stoken (">"); }
1544 | OPERATOR ASSIGN_MODIFY
1545 { const char *op = "unknown";
1569 case BINOP_BITWISE_IOR:
1572 case BINOP_BITWISE_AND:
1575 case BINOP_BITWISE_XOR:
1582 $$ = operator_stoken (op);
1585 { $$ = operator_stoken ("<<"); }
1587 { $$ = operator_stoken (">>"); }
1589 { $$ = operator_stoken ("=="); }
1591 { $$ = operator_stoken ("!="); }
1593 { $$ = operator_stoken ("<="); }
1595 { $$ = operator_stoken (">="); }
1597 { $$ = operator_stoken ("&&"); }
1599 { $$ = operator_stoken ("||"); }
1600 | OPERATOR INCREMENT
1601 { $$ = operator_stoken ("++"); }
1602 | OPERATOR DECREMENT
1603 { $$ = operator_stoken ("--"); }
1605 { $$ = operator_stoken (","); }
1606 | OPERATOR ARROW_STAR
1607 { $$ = operator_stoken ("->*"); }
1609 { $$ = operator_stoken ("->"); }
1611 { $$ = operator_stoken ("()"); }
1613 { $$ = operator_stoken ("[]"); }
1614 | OPERATOR OBJC_LBRAC ']'
1615 { $$ = operator_stoken ("[]"); }
1616 | OPERATOR conversion_type_id
1619 struct ui_file *buf = mem_fileopen ();
1621 c_print_type ($2, NULL, buf, -1, 0,
1622 &type_print_raw_options);
1623 name = ui_file_xstrdup (buf, &length);
1624 ui_file_delete (buf);
1625 $$ = operator_stoken (name);
1632 name : NAME { $$ = $1.stoken; }
1633 | BLOCKNAME { $$ = $1.stoken; }
1634 | TYPENAME { $$ = $1.stoken; }
1635 | NAME_OR_INT { $$ = $1.stoken; }
1636 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1637 | operator { $$ = $1; }
1640 name_not_typename : NAME
1642 /* These would be useful if name_not_typename was useful, but it is just
1643 a fake for "variable", so these cause reduce/reduce conflicts because
1644 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1645 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1646 context where only a name could occur, this might be useful.
1651 struct field_of_this_result is_a_field_of_this;
1654 $$.sym = lookup_symbol ($1.ptr,
1655 expression_context_block,
1657 &is_a_field_of_this);
1658 $$.is_a_field_of_this
1659 = is_a_field_of_this.type != NULL;
1666 /* Like write_exp_string, but prepends a '~'. */
1669 write_destructor_name (struct parser_state *par_state, struct stoken token)
1671 char *copy = alloca (token.length + 1);
1674 memcpy (©[1], token.ptr, token.length);
1679 write_exp_string (par_state, token);
1682 /* Returns a stoken of the operator name given by OP (which does not
1683 include the string "operator"). */
1684 static struct stoken
1685 operator_stoken (const char *op)
1687 static const char *operator_string = "operator";
1688 struct stoken st = { NULL, 0 };
1691 st.length = strlen (operator_string) + strlen (op);
1692 buf = malloc (st.length + 1);
1693 strcpy (buf, operator_string);
1697 /* The toplevel (c_parse) will free the memory allocated here. */
1698 make_cleanup (free, buf);
1702 /* Return true if the type is aggregate-like. */
1705 type_aggregate_p (struct type *type)
1707 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1708 || TYPE_CODE (type) == TYPE_CODE_UNION
1709 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1710 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1711 && TYPE_DECLARED_CLASS (type)));
1714 /* Validate a parameter typelist. */
1717 check_parameter_typelist (VEC (type_ptr) *params)
1722 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1724 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1728 if (VEC_length (type_ptr, params) == 1)
1733 VEC_free (type_ptr, params);
1734 error (_("parameter types following 'void'"));
1738 VEC_free (type_ptr, params);
1739 error (_("'void' invalid as parameter type"));
1745 /* Take care of parsing a number (anything that starts with a digit).
1746 Set yylval and return the token type; update lexptr.
1747 LEN is the number of characters in it. */
1749 /*** Needs some error checking for the float case ***/
1752 parse_number (struct parser_state *par_state,
1753 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1755 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1756 here, and we do kind of silly things like cast to unsigned. */
1763 int base = input_radix;
1766 /* Number of "L" suffixes encountered. */
1769 /* We have found a "L" or "U" suffix. */
1770 int found_suffix = 0;
1773 struct type *signed_type;
1774 struct type *unsigned_type;
1778 memcpy (p, buf, len);
1782 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1783 point. Return DECFLOAT. */
1785 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1788 putithere->typed_val_decfloat.type
1789 = parse_type (par_state)->builtin_decfloat;
1790 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1791 gdbarch_byte_order (parse_gdbarch (par_state)),
1797 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1800 putithere->typed_val_decfloat.type
1801 = parse_type (par_state)->builtin_decdouble;
1802 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1803 gdbarch_byte_order (parse_gdbarch (par_state)),
1809 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1812 putithere->typed_val_decfloat.type
1813 = parse_type (par_state)->builtin_declong;
1814 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1815 gdbarch_byte_order (parse_gdbarch (par_state)),
1821 if (! parse_c_float (parse_gdbarch (par_state), p, len,
1822 &putithere->typed_val_float.dval,
1823 &putithere->typed_val_float.type))
1828 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1872 if (c >= 'A' && c <= 'Z')
1874 if (c != 'l' && c != 'u')
1876 if (c >= '0' && c <= '9')
1884 if (base > 10 && c >= 'a' && c <= 'f')
1888 n += i = c - 'a' + 10;
1901 return ERROR; /* Char not a digit */
1904 return ERROR; /* Invalid digit in this base */
1906 /* Portably test for overflow (only works for nonzero values, so make
1907 a second check for zero). FIXME: Can't we just make n and prevn
1908 unsigned and avoid this? */
1909 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1910 unsigned_p = 1; /* Try something unsigned */
1912 /* Portably test for unsigned overflow.
1913 FIXME: This check is wrong; for example it doesn't find overflow
1914 on 0x123456789 when LONGEST is 32 bits. */
1915 if (c != 'l' && c != 'u' && n != 0)
1917 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1918 error (_("Numeric constant too large."));
1923 /* An integer constant is an int, a long, or a long long. An L
1924 suffix forces it to be long; an LL suffix forces it to be long
1925 long. If not forced to a larger size, it gets the first type of
1926 the above that it fits in. To figure out whether it fits, we
1927 shift it right and see whether anything remains. Note that we
1928 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1929 operation, because many compilers will warn about such a shift
1930 (which always produces a zero result). Sometimes gdbarch_int_bit
1931 or gdbarch_long_bit will be that big, sometimes not. To deal with
1932 the case where it is we just always shift the value more than
1933 once, with fewer bits each time. */
1935 un = (ULONGEST)n >> 2;
1937 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
1940 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
1942 /* A large decimal (not hex or octal) constant (between INT_MAX
1943 and UINT_MAX) is a long or unsigned long, according to ANSI,
1944 never an unsigned int, but this code treats it as unsigned
1945 int. This probably should be fixed. GCC gives a warning on
1948 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
1949 signed_type = parse_type (par_state)->builtin_int;
1951 else if (long_p <= 1
1952 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
1955 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
1956 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
1957 signed_type = parse_type (par_state)->builtin_long;
1962 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1963 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
1964 /* A long long does not fit in a LONGEST. */
1965 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1967 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
1968 high_bit = (ULONGEST) 1 << shift;
1969 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
1970 signed_type = parse_type (par_state)->builtin_long_long;
1973 putithere->typed_val_int.val = n;
1975 /* If the high bit of the worked out type is set then this number
1976 has to be unsigned. */
1978 if (unsigned_p || (n & high_bit))
1980 putithere->typed_val_int.type = unsigned_type;
1984 putithere->typed_val_int.type = signed_type;
1990 /* Temporary obstack used for holding strings. */
1991 static struct obstack tempbuf;
1992 static int tempbuf_init;
1994 /* Parse a C escape sequence. The initial backslash of the sequence
1995 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1996 last character of the sequence. If OUTPUT is not NULL, the
1997 translated form of the escape sequence will be written there. If
1998 OUTPUT is NULL, no output is written and the call will only affect
1999 *PTR. If an escape sequence is expressed in target bytes, then the
2000 entire sequence will simply be copied to OUTPUT. Return 1 if any
2001 character was emitted, 0 otherwise. */
2004 c_parse_escape (const char **ptr, struct obstack *output)
2006 const char *tokptr = *ptr;
2009 /* Some escape sequences undergo character set conversion. Those we
2013 /* Hex escapes do not undergo character set conversion, so keep
2014 the escape sequence for later. */
2017 obstack_grow_str (output, "\\x");
2019 if (!isxdigit (*tokptr))
2020 error (_("\\x escape without a following hex digit"));
2021 while (isxdigit (*tokptr))
2024 obstack_1grow (output, *tokptr);
2029 /* Octal escapes do not undergo character set conversion, so
2030 keep the escape sequence for later. */
2042 obstack_grow_str (output, "\\");
2044 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
2048 obstack_1grow (output, *tokptr);
2054 /* We handle UCNs later. We could handle them here, but that
2055 would mean a spurious error in the case where the UCN could
2056 be converted to the target charset but not the host
2062 int i, len = c == 'U' ? 8 : 4;
2065 obstack_1grow (output, '\\');
2066 obstack_1grow (output, *tokptr);
2069 if (!isxdigit (*tokptr))
2070 error (_("\\%c escape without a following hex digit"), c);
2071 for (i = 0; i < len && isxdigit (*tokptr); ++i)
2074 obstack_1grow (output, *tokptr);
2080 /* We must pass backslash through so that it does not
2081 cause quoting during the second expansion. */
2084 obstack_grow_str (output, "\\\\");
2088 /* Escapes which undergo conversion. */
2091 obstack_1grow (output, '\a');
2096 obstack_1grow (output, '\b');
2101 obstack_1grow (output, '\f');
2106 obstack_1grow (output, '\n');
2111 obstack_1grow (output, '\r');
2116 obstack_1grow (output, '\t');
2121 obstack_1grow (output, '\v');
2125 /* GCC extension. */
2128 obstack_1grow (output, HOST_ESCAPE_CHAR);
2132 /* Backslash-newline expands to nothing at all. */
2138 /* A few escapes just expand to the character itself. */
2142 /* GCC extensions. */
2147 /* Unrecognized escapes turn into the character itself. */
2150 obstack_1grow (output, *tokptr);
2158 /* Parse a string or character literal from TOKPTR. The string or
2159 character may be wide or unicode. *OUTPTR is set to just after the
2160 end of the literal in the input string. The resulting token is
2161 stored in VALUE. This returns a token value, either STRING or
2162 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2163 number of host characters in the literal. */
2165 parse_string_or_char (const char *tokptr, const char **outptr,
2166 struct typed_stoken *value, int *host_chars)
2169 enum c_string_type type;
2172 /* Build the gdb internal form of the input string in tempbuf. Note
2173 that the buffer is null byte terminated *only* for the
2174 convenience of debugging gdb itself and printing the buffer
2175 contents when the buffer contains no embedded nulls. Gdb does
2176 not depend upon the buffer being null byte terminated, it uses
2177 the length string instead. This allows gdb to handle C strings
2178 (as well as strings in other languages) with embedded null
2184 obstack_free (&tempbuf, NULL);
2185 obstack_init (&tempbuf);
2187 /* Record the string type. */
2190 type = C_WIDE_STRING;
2193 else if (*tokptr == 'u')
2198 else if (*tokptr == 'U')
2203 else if (*tokptr == '@')
2205 /* An Objective C string. */
2213 /* Skip the quote. */
2227 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2229 else if (c == quote)
2233 obstack_1grow (&tempbuf, c);
2235 /* FIXME: this does the wrong thing with multi-byte host
2236 characters. We could use mbrlen here, but that would
2237 make "set host-charset" a bit less useful. */
2242 if (*tokptr != quote)
2245 error (_("Unterminated string in expression."));
2247 error (_("Unmatched single quote."));
2252 value->ptr = obstack_base (&tempbuf);
2253 value->length = obstack_object_size (&tempbuf);
2257 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2260 /* This is used to associate some attributes with a token. */
2264 /* If this bit is set, the token is C++-only. */
2268 /* If this bit is set, the token is conditional: if there is a
2269 symbol of the same name, then the token is a symbol; otherwise,
2270 the token is a keyword. */
2279 enum exp_opcode opcode;
2280 enum token_flags flags;
2283 static const struct token tokentab3[] =
2285 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2286 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2287 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2288 {"...", DOTDOTDOT, BINOP_END, 0}
2291 static const struct token tokentab2[] =
2293 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2294 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2295 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2296 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2297 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2298 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2299 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2300 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2301 {"++", INCREMENT, BINOP_END, 0},
2302 {"--", DECREMENT, BINOP_END, 0},
2303 {"->", ARROW, BINOP_END, 0},
2304 {"&&", ANDAND, BINOP_END, 0},
2305 {"||", OROR, BINOP_END, 0},
2306 /* "::" is *not* only C++: gdb overrides its meaning in several
2307 different ways, e.g., 'filename'::func, function::variable. */
2308 {"::", COLONCOLON, BINOP_END, 0},
2309 {"<<", LSH, BINOP_END, 0},
2310 {">>", RSH, BINOP_END, 0},
2311 {"==", EQUAL, BINOP_END, 0},
2312 {"!=", NOTEQUAL, BINOP_END, 0},
2313 {"<=", LEQ, BINOP_END, 0},
2314 {">=", GEQ, BINOP_END, 0},
2315 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2318 /* Identifier-like tokens. */
2319 static const struct token ident_tokens[] =
2321 {"unsigned", UNSIGNED, OP_NULL, 0},
2322 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2323 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2324 {"struct", STRUCT, OP_NULL, 0},
2325 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2326 {"sizeof", SIZEOF, OP_NULL, 0},
2327 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2328 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2329 {"class", CLASS, OP_NULL, FLAG_CXX},
2330 {"union", UNION, OP_NULL, 0},
2331 {"short", SHORT, OP_NULL, 0},
2332 {"const", CONST_KEYWORD, OP_NULL, 0},
2333 {"enum", ENUM, OP_NULL, 0},
2334 {"long", LONG, OP_NULL, 0},
2335 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2336 {"int", INT_KEYWORD, OP_NULL, 0},
2337 {"new", NEW, OP_NULL, FLAG_CXX},
2338 {"delete", DELETE, OP_NULL, FLAG_CXX},
2339 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2341 {"and", ANDAND, BINOP_END, FLAG_CXX},
2342 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2343 {"bitand", '&', OP_NULL, FLAG_CXX},
2344 {"bitor", '|', OP_NULL, FLAG_CXX},
2345 {"compl", '~', OP_NULL, FLAG_CXX},
2346 {"not", '!', OP_NULL, FLAG_CXX},
2347 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2348 {"or", OROR, BINOP_END, FLAG_CXX},
2349 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2350 {"xor", '^', OP_NULL, FLAG_CXX},
2351 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2353 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2354 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2355 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2356 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2358 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2359 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2360 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2361 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2362 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2364 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2367 /* When we find that lexptr (the global var defined in parse.c) is
2368 pointing at a macro invocation, we expand the invocation, and call
2369 scan_macro_expansion to save the old lexptr here and point lexptr
2370 into the expanded text. When we reach the end of that, we call
2371 end_macro_expansion to pop back to the value we saved here. The
2372 macro expansion code promises to return only fully-expanded text,
2373 so we don't need to "push" more than one level.
2375 This is disgusting, of course. It would be cleaner to do all macro
2376 expansion beforehand, and then hand that to lexptr. But we don't
2377 really know where the expression ends. Remember, in a command like
2379 (gdb) break *ADDRESS if CONDITION
2381 we evaluate ADDRESS in the scope of the current frame, but we
2382 evaluate CONDITION in the scope of the breakpoint's location. So
2383 it's simply wrong to try to macro-expand the whole thing at once. */
2384 static const char *macro_original_text;
2386 /* We save all intermediate macro expansions on this obstack for the
2387 duration of a single parse. The expansion text may sometimes have
2388 to live past the end of the expansion, due to yacc lookahead.
2389 Rather than try to be clever about saving the data for a single
2390 token, we simply keep it all and delete it after parsing has
2392 static struct obstack expansion_obstack;
2395 scan_macro_expansion (char *expansion)
2399 /* We'd better not be trying to push the stack twice. */
2400 gdb_assert (! macro_original_text);
2402 /* Copy to the obstack, and then free the intermediate
2404 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2407 /* Save the old lexptr value, so we can return to it when we're done
2408 parsing the expanded text. */
2409 macro_original_text = lexptr;
2415 scanning_macro_expansion (void)
2417 return macro_original_text != 0;
2422 finished_macro_expansion (void)
2424 /* There'd better be something to pop back to. */
2425 gdb_assert (macro_original_text);
2427 /* Pop back to the original text. */
2428 lexptr = macro_original_text;
2429 macro_original_text = 0;
2434 scan_macro_cleanup (void *dummy)
2436 if (macro_original_text)
2437 finished_macro_expansion ();
2439 obstack_free (&expansion_obstack, NULL);
2442 /* Return true iff the token represents a C++ cast operator. */
2445 is_cast_operator (const char *token, int len)
2447 return (! strncmp (token, "dynamic_cast", len)
2448 || ! strncmp (token, "static_cast", len)
2449 || ! strncmp (token, "reinterpret_cast", len)
2450 || ! strncmp (token, "const_cast", len));
2453 /* The scope used for macro expansion. */
2454 static struct macro_scope *expression_macro_scope;
2456 /* This is set if a NAME token appeared at the very end of the input
2457 string, with no whitespace separating the name from the EOF. This
2458 is used only when parsing to do field name completion. */
2459 static int saw_name_at_eof;
2461 /* This is set if the previously-returned token was a structure
2462 operator -- either '.' or ARROW. This is used only when parsing to
2463 do field name completion. */
2464 static int last_was_structop;
2466 /* Read one token, getting characters through lexptr. */
2469 lex_one_token (struct parser_state *par_state, int *is_quoted_name)
2474 const char *tokstart;
2475 int saw_structop = last_was_structop;
2478 last_was_structop = 0;
2479 *is_quoted_name = 0;
2483 /* Check if this is a macro invocation that we need to expand. */
2484 if (! scanning_macro_expansion ())
2486 char *expanded = macro_expand_next (&lexptr,
2487 standard_macro_lookup,
2488 expression_macro_scope);
2491 scan_macro_expansion (expanded);
2494 prev_lexptr = lexptr;
2497 /* See if it is a special token of length 3. */
2498 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2499 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2501 if ((tokentab3[i].flags & FLAG_CXX) != 0
2502 && parse_language (par_state)->la_language != language_cplus)
2506 yylval.opcode = tokentab3[i].opcode;
2507 return tokentab3[i].token;
2510 /* See if it is a special token of length 2. */
2511 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2512 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2514 if ((tokentab2[i].flags & FLAG_CXX) != 0
2515 && parse_language (par_state)->la_language != language_cplus)
2519 yylval.opcode = tokentab2[i].opcode;
2520 if (parse_completion && tokentab2[i].token == ARROW)
2521 last_was_structop = 1;
2522 return tokentab2[i].token;
2525 switch (c = *tokstart)
2528 /* If we were just scanning the result of a macro expansion,
2529 then we need to resume scanning the original text.
2530 If we're parsing for field name completion, and the previous
2531 token allows such completion, return a COMPLETE token.
2532 Otherwise, we were already scanning the original text, and
2533 we're really done. */
2534 if (scanning_macro_expansion ())
2536 finished_macro_expansion ();
2539 else if (saw_name_at_eof)
2541 saw_name_at_eof = 0;
2544 else if (saw_structop)
2559 if (parse_language (par_state)->la_language == language_objc
2566 if (paren_depth == 0)
2573 if (comma_terminates
2575 && ! scanning_macro_expansion ())
2581 /* Might be a floating point number. */
2582 if (lexptr[1] < '0' || lexptr[1] > '9')
2584 if (parse_completion)
2585 last_was_structop = 1;
2586 goto symbol; /* Nope, must be a symbol. */
2588 /* FALL THRU into number case. */
2601 /* It's a number. */
2602 int got_dot = 0, got_e = 0, toktype;
2603 const char *p = tokstart;
2604 int hex = input_radix > 10;
2606 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2611 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2619 /* This test includes !hex because 'e' is a valid hex digit
2620 and thus does not indicate a floating point number when
2621 the radix is hex. */
2622 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2623 got_dot = got_e = 1;
2624 /* This test does not include !hex, because a '.' always indicates
2625 a decimal floating point number regardless of the radix. */
2626 else if (!got_dot && *p == '.')
2628 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2629 && (*p == '-' || *p == '+'))
2630 /* This is the sign of the exponent, not the end of the
2633 /* We will take any letters or digits. parse_number will
2634 complain if past the radix, or if L or U are not final. */
2635 else if ((*p < '0' || *p > '9')
2636 && ((*p < 'a' || *p > 'z')
2637 && (*p < 'A' || *p > 'Z')))
2640 toktype = parse_number (par_state, tokstart, p - tokstart,
2641 got_dot|got_e, &yylval);
2642 if (toktype == ERROR)
2644 char *err_copy = (char *) alloca (p - tokstart + 1);
2646 memcpy (err_copy, tokstart, p - tokstart);
2647 err_copy[p - tokstart] = 0;
2648 error (_("Invalid number \"%s\"."), err_copy);
2656 const char *p = &tokstart[1];
2657 size_t len = strlen ("entry");
2659 if (parse_language (par_state)->la_language == language_objc)
2661 size_t len = strlen ("selector");
2663 if (strncmp (p, "selector", len) == 0
2664 && (p[len] == '\0' || isspace (p[len])))
2673 while (isspace (*p))
2675 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2707 if (tokstart[1] != '"' && tokstart[1] != '\'')
2716 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2721 error (_("Empty character constant."));
2722 else if (host_len > 2 && c == '\'')
2725 namelen = lexptr - tokstart - 1;
2726 *is_quoted_name = 1;
2730 else if (host_len > 1)
2731 error (_("Invalid character constant."));
2737 if (!(c == '_' || c == '$'
2738 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2739 /* We must have come across a bad character (e.g. ';'). */
2740 error (_("Invalid character '%c' in expression."), c);
2742 /* It's a name. See how long it is. */
2744 for (c = tokstart[namelen];
2745 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2746 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2748 /* Template parameter lists are part of the name.
2749 FIXME: This mishandles `print $a<4&&$a>3'. */
2753 if (! is_cast_operator (tokstart, namelen))
2755 /* Scan ahead to get rest of the template specification. Note
2756 that we look ahead only when the '<' adjoins non-whitespace
2757 characters; for comparison expressions, e.g. "a < b > c",
2758 there must be spaces before the '<', etc. */
2760 const char *p = find_template_name_end (tokstart + namelen);
2763 namelen = p - tokstart;
2767 c = tokstart[++namelen];
2770 /* The token "if" terminates the expression and is NOT removed from
2771 the input stream. It doesn't count if it appears in the
2772 expansion of a macro. */
2774 && tokstart[0] == 'i'
2775 && tokstart[1] == 'f'
2776 && ! scanning_macro_expansion ())
2781 /* For the same reason (breakpoint conditions), "thread N"
2782 terminates the expression. "thread" could be an identifier, but
2783 an identifier is never followed by a number without intervening
2784 punctuation. "task" is similar. Handle abbreviations of these,
2785 similarly to breakpoint.c:find_condition_and_thread. */
2787 && (strncmp (tokstart, "thread", namelen) == 0
2788 || strncmp (tokstart, "task", namelen) == 0)
2789 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2790 && ! scanning_macro_expansion ())
2792 const char *p = tokstart + namelen + 1;
2794 while (*p == ' ' || *p == '\t')
2796 if (*p >= '0' && *p <= '9')
2804 yylval.sval.ptr = tokstart;
2805 yylval.sval.length = namelen;
2807 /* Catch specific keywords. */
2808 copy = copy_name (yylval.sval);
2809 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2810 if (strcmp (copy, ident_tokens[i].operator) == 0)
2812 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2813 && parse_language (par_state)->la_language != language_cplus)
2816 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2818 struct field_of_this_result is_a_field_of_this;
2820 if (lookup_symbol (copy, expression_context_block,
2822 (parse_language (par_state)->la_language
2823 == language_cplus ? &is_a_field_of_this
2827 /* The keyword is shadowed. */
2832 /* It is ok to always set this, even though we don't always
2833 strictly need to. */
2834 yylval.opcode = ident_tokens[i].opcode;
2835 return ident_tokens[i].token;
2838 if (*tokstart == '$')
2841 if (parse_completion && *lexptr == '\0')
2842 saw_name_at_eof = 1;
2844 yylval.ssym.stoken = yylval.sval;
2845 yylval.ssym.sym = NULL;
2846 yylval.ssym.is_a_field_of_this = 0;
2850 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2857 DEF_VEC_O (token_and_value);
2859 /* A FIFO of tokens that have been read but not yet returned to the
2861 static VEC (token_and_value) *token_fifo;
2863 /* Non-zero if the lexer should return tokens from the FIFO. */
2866 /* Temporary storage for c_lex; this holds symbol names as they are
2868 static struct obstack name_obstack;
2870 /* Classify a NAME token. The contents of the token are in `yylval'.
2871 Updates yylval and returns the new token type. BLOCK is the block
2872 in which lookups start; this can be NULL to mean the global scope.
2873 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2874 in single quotes. */
2876 classify_name (struct parser_state *par_state, const struct block *block,
2881 struct field_of_this_result is_a_field_of_this;
2883 copy = copy_name (yylval.sval);
2885 /* Initialize this in case we *don't* use it in this call; that way
2886 we can refer to it unconditionally below. */
2887 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2889 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2890 parse_language (par_state)->la_name_of_this
2891 ? &is_a_field_of_this : NULL);
2893 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2895 yylval.ssym.sym = sym;
2896 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2901 /* If we found a field of 'this', we might have erroneously
2902 found a constructor where we wanted a type name. Handle this
2903 case by noticing that we found a constructor and then look up
2904 the type tag instead. */
2905 if (is_a_field_of_this.type != NULL
2906 && is_a_field_of_this.fn_field != NULL
2907 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2910 struct field_of_this_result inner_is_a_field_of_this;
2912 sym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2913 &inner_is_a_field_of_this);
2916 yylval.tsym.type = SYMBOL_TYPE (sym);
2921 /* If we found a field, then we want to prefer it over a
2922 filename. However, if the name was quoted, then it is better
2923 to check for a filename or a block, since this is the only
2924 way the user has of requiring the extension to be used. */
2925 if (is_a_field_of_this.type == NULL || is_quoted_name)
2927 /* See if it's a file name. */
2928 struct symtab *symtab;
2930 symtab = lookup_symtab (copy);
2933 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
2940 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2942 yylval.tsym.type = SYMBOL_TYPE (sym);
2947 = language_lookup_primitive_type_by_name (parse_language (par_state),
2948 parse_gdbarch (par_state),
2950 if (yylval.tsym.type != NULL)
2953 /* See if it's an ObjC classname. */
2954 if (parse_language (par_state)->la_language == language_objc && !sym)
2956 CORE_ADDR Class = lookup_objc_class (parse_gdbarch (par_state), copy);
2959 yylval.class.class = Class;
2960 sym = lookup_struct_typedef (copy, expression_context_block, 1);
2962 yylval.class.type = SYMBOL_TYPE (sym);
2967 /* Input names that aren't symbols but ARE valid hex numbers, when
2968 the input radix permits them, can be names or numbers depending
2969 on the parse. Note we support radixes > 16 here. */
2971 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2972 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2974 YYSTYPE newlval; /* Its value is ignored. */
2975 int hextype = parse_number (par_state, copy, yylval.sval.length,
2979 yylval.ssym.sym = sym;
2980 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2985 /* Any other kind of symbol */
2986 yylval.ssym.sym = sym;
2987 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2990 && parse_language (par_state)->la_language == language_cplus
2991 && is_a_field_of_this.type == NULL
2992 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
2993 return UNKNOWN_CPP_NAME;
2998 /* Like classify_name, but used by the inner loop of the lexer, when a
2999 name might have already been seen. CONTEXT is the context type, or
3000 NULL if this is the first component of a name. */
3003 classify_inner_name (struct parser_state *par_state,
3004 const struct block *block, struct type *context)
3009 if (context == NULL)
3010 return classify_name (par_state, block, 0);
3012 type = check_typedef (context);
3013 if (!type_aggregate_p (type))
3016 copy = copy_name (yylval.ssym.stoken);
3017 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block);
3019 /* If no symbol was found, search for a matching base class named
3020 COPY. This will allow users to enter qualified names of class members
3021 relative to the `this' pointer. */
3022 if (yylval.ssym.sym == NULL)
3024 struct type *base_type = find_type_baseclass_by_name (type, copy);
3026 if (base_type != NULL)
3028 yylval.tsym.type = base_type;
3035 switch (SYMBOL_CLASS (yylval.ssym.sym))
3039 /* cp_lookup_nested_symbol might have accidentally found a constructor
3040 named COPY when we really wanted a base class of the same name.
3041 Double-check this case by looking for a base class. */
3043 struct type *base_type = find_type_baseclass_by_name (type, copy);
3045 if (base_type != NULL)
3047 yylval.tsym.type = base_type;
3054 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
3060 internal_error (__FILE__, __LINE__, _("not reached"));
3063 /* The outer level of a two-level lexer. This calls the inner lexer
3064 to return tokens. It then either returns these tokens, or
3065 aggregates them into a larger token. This lets us work around a
3066 problem in our parsing approach, where the parser could not
3067 distinguish between qualified names and qualified types at the
3070 This approach is still not ideal, because it mishandles template
3071 types. See the comment in lex_one_token for an example. However,
3072 this is still an improvement over the earlier approach, and will
3073 suffice until we move to better parsing technology. */
3077 token_and_value current;
3078 int first_was_coloncolon, last_was_coloncolon;
3079 struct type *context_type = NULL;
3080 int last_to_examine, next_to_examine, checkpoint;
3081 const struct block *search_block;
3084 if (popping && !VEC_empty (token_and_value, token_fifo))
3088 /* Read the first token and decide what to do. Most of the
3089 subsequent code is C++-only; but also depends on seeing a "::" or
3091 current.token = lex_one_token (pstate, &is_quoted_name);
3092 if (current.token == NAME)
3093 current.token = classify_name (pstate, expression_context_block,
3095 if (parse_language (pstate)->la_language != language_cplus
3096 || (current.token != TYPENAME && current.token != COLONCOLON
3097 && current.token != FILENAME))
3098 return current.token;
3100 /* Read any sequence of alternating "::" and name-like tokens into
3102 current.value = yylval;
3103 VEC_safe_push (token_and_value, token_fifo, ¤t);
3104 last_was_coloncolon = current.token == COLONCOLON;
3109 /* We ignore quoted names other than the very first one.
3110 Subsequent ones do not have any special meaning. */
3111 current.token = lex_one_token (pstate, &ignore);
3112 current.value = yylval;
3113 VEC_safe_push (token_and_value, token_fifo, ¤t);
3115 if ((last_was_coloncolon && current.token != NAME)
3116 || (!last_was_coloncolon && current.token != COLONCOLON))
3118 last_was_coloncolon = !last_was_coloncolon;
3122 /* We always read one extra token, so compute the number of tokens
3123 to examine accordingly. */
3124 last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
3125 next_to_examine = 0;
3127 current = *VEC_index (token_and_value, token_fifo, next_to_examine);
3130 obstack_free (&name_obstack, obstack_base (&name_obstack));
3132 if (current.token == FILENAME)
3133 search_block = current.value.bval;
3134 else if (current.token == COLONCOLON)
3135 search_block = NULL;
3138 gdb_assert (current.token == TYPENAME);
3139 search_block = expression_context_block;
3140 obstack_grow (&name_obstack, current.value.sval.ptr,
3141 current.value.sval.length);
3142 context_type = current.value.tsym.type;
3146 first_was_coloncolon = current.token == COLONCOLON;
3147 last_was_coloncolon = first_was_coloncolon;
3149 while (next_to_examine <= last_to_examine)
3151 token_and_value *next;
3153 next = VEC_index (token_and_value, token_fifo, next_to_examine);
3156 if (next->token == NAME && last_was_coloncolon)
3160 yylval = next->value;
3161 classification = classify_inner_name (pstate, search_block,
3163 /* We keep going until we either run out of names, or until
3164 we have a qualified name which is not a type. */
3165 if (classification != TYPENAME && classification != NAME)
3168 /* Accept up to this token. */
3169 checkpoint = next_to_examine;
3171 /* Update the partial name we are constructing. */
3172 if (context_type != NULL)
3174 /* We don't want to put a leading "::" into the name. */
3175 obstack_grow_str (&name_obstack, "::");
3177 obstack_grow (&name_obstack, next->value.sval.ptr,
3178 next->value.sval.length);
3180 yylval.sval.ptr = obstack_base (&name_obstack);
3181 yylval.sval.length = obstack_object_size (&name_obstack);
3182 current.value = yylval;
3183 current.token = classification;
3185 last_was_coloncolon = 0;
3187 if (classification == NAME)
3190 context_type = yylval.tsym.type;
3192 else if (next->token == COLONCOLON && !last_was_coloncolon)
3193 last_was_coloncolon = 1;
3196 /* We've reached the end of the name. */
3201 /* If we have a replacement token, install it as the first token in
3202 the FIFO, and delete the other constituent tokens. */
3205 current.value.sval.ptr = obstack_copy0 (&expansion_obstack,
3206 current.value.sval.ptr,
3207 current.value.sval.length);
3209 VEC_replace (token_and_value, token_fifo, 0, ¤t);
3211 VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
3215 current = *VEC_index (token_and_value, token_fifo, 0);
3216 VEC_ordered_remove (token_and_value, token_fifo, 0);
3217 yylval = current.value;
3218 return current.token;
3222 c_parse (struct parser_state *par_state)
3225 struct cleanup *back_to;
3227 /* Setting up the parser state. */
3228 gdb_assert (par_state != NULL);
3231 back_to = make_cleanup (free_current_contents, &expression_macro_scope);
3232 make_cleanup_clear_parser_state (&pstate);
3234 /* Set up the scope for macro expansion. */
3235 expression_macro_scope = NULL;
3237 if (expression_context_block)
3238 expression_macro_scope
3239 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3241 expression_macro_scope = default_macro_scope ();
3242 if (! expression_macro_scope)
3243 expression_macro_scope = user_macro_scope ();
3245 /* Initialize macro expansion code. */
3246 obstack_init (&expansion_obstack);
3247 gdb_assert (! macro_original_text);
3248 make_cleanup (scan_macro_cleanup, 0);
3250 make_cleanup_restore_integer (&yydebug);
3251 yydebug = parser_debug;
3253 /* Initialize some state used by the lexer. */
3254 last_was_structop = 0;
3255 saw_name_at_eof = 0;
3257 VEC_free (token_and_value, token_fifo);
3259 obstack_init (&name_obstack);
3260 make_cleanup_obstack_free (&name_obstack);
3262 result = yyparse ();
3263 do_cleanups (back_to);
3270 /* This is called via the YYPRINT macro when parser debugging is
3271 enabled. It prints a token's value. */
3274 c_print_token (FILE *file, int type, YYSTYPE value)
3279 fprintf (file, "typed_val_int<%s, %s>",
3280 TYPE_SAFE_NAME (value.typed_val_int.type),
3281 pulongest (value.typed_val_int.val));
3287 char *copy = alloca (value.tsval.length + 1);
3289 memcpy (copy, value.tsval.ptr, value.tsval.length);
3290 copy[value.tsval.length] = '\0';
3292 fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3298 fprintf (file, "sval<%s>", copy_name (value.sval));
3302 fprintf (file, "tsym<type=%s, name=%s>",
3303 TYPE_SAFE_NAME (value.tsym.type),
3304 copy_name (value.tsym.stoken));
3308 case UNKNOWN_CPP_NAME:
3311 fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3312 copy_name (value.ssym.stoken),
3313 (value.ssym.sym == NULL
3314 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym)),
3315 value.ssym.is_a_field_of_this);
3319 fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3330 lexptr = prev_lexptr;
3332 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);