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 "macroscope.h"
54 #include "objc-lang.h"
55 #include "typeprint.h"
58 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
61 as well as gratuitiously global symbol names, so we can have multiple
62 yacc generated parsers in gdb. Note that these are only the variables
63 produced by yacc. If other parser generators (bison, byacc, etc) produce
64 additional global names that conflict at link time, then those parser
65 generators need to be fixed instead of adding those names to this list. */
67 #define yymaxdepth c_maxdepth
68 #define yyparse c_parse_internal
70 #define yyerror c_error
73 #define yydebug c_debug
82 #define yyerrflag c_errflag
83 #define yynerrs c_nerrs
88 #define yystate c_state
94 #define yyreds c_reds /* With YYDEBUG defined */
95 #define yytoks c_toks /* With YYDEBUG defined */
96 #define yyname c_name /* With YYDEBUG defined */
97 #define yyrule c_rule /* With YYDEBUG defined */
100 #define yydefred c_yydefred
101 #define yydgoto c_yydgoto
102 #define yysindex c_yysindex
103 #define yyrindex c_yyrindex
104 #define yygindex c_yygindex
105 #define yytable c_yytable
106 #define yycheck c_yycheck
108 #define yysslim c_yysslim
109 #define yyssp c_yyssp
110 #define yystacksize c_yystacksize
112 #define yyvsp c_yyvsp
115 #define YYDEBUG 1 /* Default to yydebug support */
118 #define YYFPRINTF parser_fprintf
120 /* The state of the parser, used internally when we are parsing the
123 static struct parser_state *pstate = NULL;
127 static int yylex (void);
129 void yyerror (char *);
131 static int type_aggregate_p (struct type *);
135 /* Although the yacc "value" of an expression is not used,
136 since the result is stored in the structure being created,
137 other node types do have values. */
153 } typed_val_decfloat;
156 struct typed_stoken tsval;
158 struct symtoken ssym;
160 const struct block *bval;
161 enum exp_opcode opcode;
163 struct stoken_vector svec;
164 VEC (type_ptr) *tvec;
166 struct type_stack *type_stack;
168 struct objc_class_str class;
172 /* YYSTYPE gets defined by %union */
173 static int parse_number (struct parser_state *par_state,
174 const char *, int, int, YYSTYPE *);
175 static struct stoken operator_stoken (const char *);
176 static void check_parameter_typelist (VEC (type_ptr) *);
177 static void write_destructor_name (struct parser_state *par_state,
181 static void c_print_token (FILE *file, int type, YYSTYPE value);
182 #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
186 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
188 %type <tval> type typebase
189 %type <tvec> nonempty_typelist func_mod parameter_typelist
190 /* %type <bval> block */
192 /* Fancy type parsing. */
194 %type <lval> array_mod
195 %type <tval> conversion_type_id
197 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
199 %token <typed_val_int> INT
200 %token <typed_val_float> FLOAT
201 %token <typed_val_decfloat> DECFLOAT
203 /* Both NAME and TYPENAME tokens represent symbols in the input,
204 and both convey their data as strings.
205 But a TYPENAME is a string that happens to be defined as a typedef
206 or builtin type name (such as int or char)
207 and a NAME is any other symbol.
208 Contexts where this distinction is not important can use the
209 nonterminal "name", which matches either NAME or TYPENAME. */
211 %token <tsval> STRING
212 %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
213 %token SELECTOR /* ObjC "@selector" pseudo-operator */
215 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
216 %token <ssym> UNKNOWN_CPP_NAME
217 %token <voidval> COMPLETE
218 %token <tsym> TYPENAME
219 %token <class> CLASSNAME /* ObjC Class name */
221 %type <svec> string_exp
222 %type <ssym> name_not_typename
223 %type <tsym> typename
225 /* This is like a '[' token, but is only generated when parsing
226 Objective C. This lets us reuse the same parser without
227 erroneously parsing ObjC-specific expressions in C. */
230 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
231 but which would parse as a valid number in the current input radix.
232 E.g. "c" when input_radix==16. Depending on the parse, it will be
233 turned into a name or into a number. */
235 %token <ssym> NAME_OR_INT
238 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
242 %type <sval> operator
243 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
249 /* Special type cases, put in to allow the parser to distinguish different
251 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
253 %token <sval> VARIABLE
255 %token <opcode> ASSIGN_MODIFY
264 %right '=' ASSIGN_MODIFY
272 %left '<' '>' LEQ GEQ
277 %right UNARY INCREMENT DECREMENT
278 %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
279 %token <ssym> BLOCKNAME
280 %token <bval> FILENAME
294 { write_exp_elt_opcode(pstate, OP_TYPE);
295 write_exp_elt_type(pstate, $1);
296 write_exp_elt_opcode(pstate, OP_TYPE);}
299 write_exp_elt_opcode (pstate, OP_TYPEOF);
301 | TYPEOF '(' type ')'
303 write_exp_elt_opcode (pstate, OP_TYPE);
304 write_exp_elt_type (pstate, $3);
305 write_exp_elt_opcode (pstate, OP_TYPE);
307 | DECLTYPE '(' exp ')'
309 write_exp_elt_opcode (pstate, OP_DECLTYPE);
313 /* Expressions, including the comma operator. */
316 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
319 /* Expressions, not including the comma operator. */
320 exp : '*' exp %prec UNARY
321 { write_exp_elt_opcode (pstate, UNOP_IND); }
324 exp : '&' exp %prec UNARY
325 { write_exp_elt_opcode (pstate, UNOP_ADDR); }
328 exp : '-' exp %prec UNARY
329 { write_exp_elt_opcode (pstate, UNOP_NEG); }
332 exp : '+' exp %prec UNARY
333 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
336 exp : '!' exp %prec UNARY
337 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
340 exp : '~' exp %prec UNARY
341 { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
344 exp : INCREMENT exp %prec UNARY
345 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
348 exp : DECREMENT exp %prec UNARY
349 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
352 exp : exp INCREMENT %prec UNARY
353 { write_exp_elt_opcode (pstate, UNOP_POSTINCREMENT); }
356 exp : exp DECREMENT %prec UNARY
357 { write_exp_elt_opcode (pstate, UNOP_POSTDECREMENT); }
360 exp : TYPEID '(' exp ')' %prec UNARY
361 { write_exp_elt_opcode (pstate, OP_TYPEID); }
364 exp : TYPEID '(' type_exp ')' %prec UNARY
365 { write_exp_elt_opcode (pstate, OP_TYPEID); }
368 exp : SIZEOF exp %prec UNARY
369 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
373 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
374 write_exp_string (pstate, $3);
375 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
378 exp : exp ARROW name COMPLETE
379 { mark_struct_expression (pstate);
380 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
381 write_exp_string (pstate, $3);
382 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
385 exp : exp ARROW COMPLETE
387 mark_struct_expression (pstate);
388 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
391 write_exp_string (pstate, s);
392 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
395 exp : exp ARROW '~' name
396 { write_exp_elt_opcode (pstate, STRUCTOP_PTR);
397 write_destructor_name (pstate, $4);
398 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
401 exp : exp ARROW '~' name COMPLETE
402 { mark_struct_expression (pstate);
403 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
404 write_destructor_name (pstate, $4);
405 write_exp_elt_opcode (pstate, STRUCTOP_PTR); }
408 exp : exp ARROW qualified_name
409 { /* exp->type::name becomes exp->*(&type::name) */
410 /* Note: this doesn't work if name is a
411 static member! FIXME */
412 write_exp_elt_opcode (pstate, UNOP_ADDR);
413 write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
416 exp : exp ARROW_STAR exp
417 { write_exp_elt_opcode (pstate, STRUCTOP_MPTR); }
421 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
422 write_exp_string (pstate, $3);
423 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
426 exp : exp '.' name COMPLETE
427 { mark_struct_expression (pstate);
428 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
429 write_exp_string (pstate, $3);
430 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
433 exp : exp '.' COMPLETE
435 mark_struct_expression (pstate);
436 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
439 write_exp_string (pstate, s);
440 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
443 exp : exp '.' '~' name
444 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
445 write_destructor_name (pstate, $4);
446 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
449 exp : exp '.' '~' name COMPLETE
450 { mark_struct_expression (pstate);
451 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
452 write_destructor_name (pstate, $4);
453 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
456 exp : exp '.' qualified_name
457 { /* exp.type::name becomes exp.*(&type::name) */
458 /* Note: this doesn't work if name is a
459 static member! FIXME */
460 write_exp_elt_opcode (pstate, UNOP_ADDR);
461 write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
464 exp : exp DOT_STAR exp
465 { write_exp_elt_opcode (pstate, STRUCTOP_MEMBER); }
468 exp : exp '[' exp1 ']'
469 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
472 exp : exp OBJC_LBRAC exp1 ']'
473 { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
477 * The rules below parse ObjC message calls of the form:
478 * '[' target selector {':' argument}* ']'
481 exp : OBJC_LBRAC TYPENAME
485 class = lookup_objc_class (parse_gdbarch (pstate),
486 copy_name ($2.stoken));
488 error (_("%s is not an ObjC Class"),
489 copy_name ($2.stoken));
490 write_exp_elt_opcode (pstate, OP_LONG);
491 write_exp_elt_type (pstate,
492 parse_type (pstate)->builtin_int);
493 write_exp_elt_longcst (pstate, (LONGEST) class);
494 write_exp_elt_opcode (pstate, OP_LONG);
498 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
499 end_msglist (pstate);
500 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
504 exp : OBJC_LBRAC CLASSNAME
506 write_exp_elt_opcode (pstate, OP_LONG);
507 write_exp_elt_type (pstate,
508 parse_type (pstate)->builtin_int);
509 write_exp_elt_longcst (pstate, (LONGEST) $2.class);
510 write_exp_elt_opcode (pstate, OP_LONG);
514 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
515 end_msglist (pstate);
516 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
523 { write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
524 end_msglist (pstate);
525 write_exp_elt_opcode (pstate, OP_OBJC_MSGCALL);
530 { add_msglist(&$1, 0); }
538 msgarg : name ':' exp
539 { add_msglist(&$1, 1); }
540 | ':' exp /* Unnamed arg. */
541 { add_msglist(0, 1); }
542 | ',' exp /* Variable number of args. */
543 { add_msglist(0, 0); }
547 /* This is to save the value of arglist_len
548 being accumulated by an outer function call. */
549 { start_arglist (); }
550 arglist ')' %prec ARROW
551 { write_exp_elt_opcode (pstate, OP_FUNCALL);
552 write_exp_elt_longcst (pstate,
553 (LONGEST) end_arglist ());
554 write_exp_elt_opcode (pstate, OP_FUNCALL); }
557 exp : UNKNOWN_CPP_NAME '('
559 /* This could potentially be a an argument defined
560 lookup function (Koenig). */
561 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
562 write_exp_elt_block (pstate,
563 expression_context_block);
564 write_exp_elt_sym (pstate,
565 NULL); /* Placeholder. */
566 write_exp_string (pstate, $1.stoken);
567 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
569 /* This is to save the value of arglist_len
570 being accumulated by an outer function call. */
574 arglist ')' %prec ARROW
576 write_exp_elt_opcode (pstate, OP_FUNCALL);
577 write_exp_elt_longcst (pstate,
578 (LONGEST) end_arglist ());
579 write_exp_elt_opcode (pstate, OP_FUNCALL);
584 { start_arglist (); }
594 arglist : arglist ',' exp %prec ABOVE_COMMA
598 exp : exp '(' parameter_typelist ')' const_or_volatile
600 VEC (type_ptr) *type_list = $3;
601 struct type *type_elt;
602 LONGEST len = VEC_length (type_ptr, type_list);
604 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
605 write_exp_elt_longcst (pstate, len);
607 VEC_iterate (type_ptr, type_list, i, type_elt);
609 write_exp_elt_type (pstate, type_elt);
610 write_exp_elt_longcst(pstate, len);
611 write_exp_elt_opcode (pstate, TYPE_INSTANCE);
612 VEC_free (type_ptr, type_list);
617 { $$ = end_arglist () - 1; }
619 exp : lcurly arglist rcurly %prec ARROW
620 { write_exp_elt_opcode (pstate, OP_ARRAY);
621 write_exp_elt_longcst (pstate, (LONGEST) 0);
622 write_exp_elt_longcst (pstate, (LONGEST) $3);
623 write_exp_elt_opcode (pstate, OP_ARRAY); }
626 exp : lcurly type_exp rcurly exp %prec UNARY
627 { write_exp_elt_opcode (pstate, UNOP_MEMVAL_TYPE); }
630 exp : '(' type_exp ')' exp %prec UNARY
631 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
638 /* Binary operators in order of decreasing precedence. */
641 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
645 { write_exp_elt_opcode (pstate, BINOP_MUL); }
649 { write_exp_elt_opcode (pstate, BINOP_DIV); }
653 { write_exp_elt_opcode (pstate, BINOP_REM); }
657 { write_exp_elt_opcode (pstate, BINOP_ADD); }
661 { write_exp_elt_opcode (pstate, BINOP_SUB); }
665 { write_exp_elt_opcode (pstate, BINOP_LSH); }
669 { write_exp_elt_opcode (pstate, BINOP_RSH); }
673 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
676 exp : exp NOTEQUAL exp
677 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
681 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
685 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
689 { write_exp_elt_opcode (pstate, BINOP_LESS); }
693 { write_exp_elt_opcode (pstate, BINOP_GTR); }
697 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
701 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
705 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
709 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
713 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
716 exp : exp '?' exp ':' exp %prec '?'
717 { write_exp_elt_opcode (pstate, TERNOP_COND); }
721 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
724 exp : exp ASSIGN_MODIFY exp
725 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
726 write_exp_elt_opcode (pstate, $2);
727 write_exp_elt_opcode (pstate,
728 BINOP_ASSIGN_MODIFY); }
732 { write_exp_elt_opcode (pstate, OP_LONG);
733 write_exp_elt_type (pstate, $1.type);
734 write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
735 write_exp_elt_opcode (pstate, OP_LONG); }
740 struct stoken_vector vec;
743 write_exp_string_vector (pstate, $1.type, &vec);
749 parse_number (pstate, $1.stoken.ptr,
750 $1.stoken.length, 0, &val);
751 write_exp_elt_opcode (pstate, OP_LONG);
752 write_exp_elt_type (pstate, val.typed_val_int.type);
753 write_exp_elt_longcst (pstate,
754 (LONGEST) val.typed_val_int.val);
755 write_exp_elt_opcode (pstate, OP_LONG);
761 { write_exp_elt_opcode (pstate, OP_DOUBLE);
762 write_exp_elt_type (pstate, $1.type);
763 write_exp_elt_dblcst (pstate, $1.dval);
764 write_exp_elt_opcode (pstate, OP_DOUBLE); }
768 { write_exp_elt_opcode (pstate, OP_DECFLOAT);
769 write_exp_elt_type (pstate, $1.type);
770 write_exp_elt_decfloatcst (pstate, $1.val);
771 write_exp_elt_opcode (pstate, OP_DECFLOAT); }
779 write_dollar_variable (pstate, $1);
783 exp : SELECTOR '(' name ')'
785 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR);
786 write_exp_string (pstate, $3);
787 write_exp_elt_opcode (pstate, OP_OBJC_SELECTOR); }
790 exp : SIZEOF '(' type ')' %prec UNARY
791 { struct type *type = $3;
792 write_exp_elt_opcode (pstate, OP_LONG);
793 write_exp_elt_type (pstate, lookup_signed_typename
794 (parse_language (pstate),
795 parse_gdbarch (pstate),
797 CHECK_TYPEDEF (type);
799 /* $5.3.3/2 of the C++ Standard (n3290 draft)
800 says of sizeof: "When applied to a reference
801 or a reference type, the result is the size of
802 the referenced type." */
803 if (TYPE_CODE (type) == TYPE_CODE_REF)
804 type = check_typedef (TYPE_TARGET_TYPE (type));
805 write_exp_elt_longcst (pstate,
806 (LONGEST) TYPE_LENGTH (type));
807 write_exp_elt_opcode (pstate, OP_LONG); }
810 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
811 { write_exp_elt_opcode (pstate,
812 UNOP_REINTERPRET_CAST); }
815 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
816 { write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
819 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
820 { write_exp_elt_opcode (pstate, UNOP_DYNAMIC_CAST); }
823 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
824 { /* We could do more error checking here, but
825 it doesn't seem worthwhile. */
826 write_exp_elt_opcode (pstate, UNOP_CAST_TYPE); }
832 /* We copy the string here, and not in the
833 lexer, to guarantee that we do not leak a
834 string. Note that we follow the
835 NUL-termination convention of the
837 struct typed_stoken *vec = XNEW (struct typed_stoken);
842 vec->length = $1.length;
843 vec->ptr = malloc ($1.length + 1);
844 memcpy (vec->ptr, $1.ptr, $1.length + 1);
849 /* Note that we NUL-terminate here, but just
853 $$.tokens = realloc ($$.tokens,
854 $$.len * sizeof (struct typed_stoken));
856 p = malloc ($2.length + 1);
857 memcpy (p, $2.ptr, $2.length + 1);
859 $$.tokens[$$.len - 1].type = $2.type;
860 $$.tokens[$$.len - 1].length = $2.length;
861 $$.tokens[$$.len - 1].ptr = p;
868 enum c_string_type type = C_STRING;
870 for (i = 0; i < $1.len; ++i)
872 switch ($1.tokens[i].type)
880 && type != $1.tokens[i].type)
881 error (_("Undefined string concatenation."));
882 type = $1.tokens[i].type;
886 internal_error (__FILE__, __LINE__,
887 "unrecognized type in string concatenation");
891 write_exp_string_vector (pstate, type, &$1);
892 for (i = 0; i < $1.len; ++i)
893 free ($1.tokens[i].ptr);
898 exp : NSSTRING /* ObjC NextStep NSString constant
899 * of the form '@' '"' string '"'.
901 { write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING);
902 write_exp_string (pstate, $1);
903 write_exp_elt_opcode (pstate, OP_OBJC_NSSTRING); }
908 { write_exp_elt_opcode (pstate, OP_LONG);
909 write_exp_elt_type (pstate,
910 parse_type (pstate)->builtin_bool);
911 write_exp_elt_longcst (pstate, (LONGEST) 1);
912 write_exp_elt_opcode (pstate, OP_LONG); }
916 { write_exp_elt_opcode (pstate, OP_LONG);
917 write_exp_elt_type (pstate,
918 parse_type (pstate)->builtin_bool);
919 write_exp_elt_longcst (pstate, (LONGEST) 0);
920 write_exp_elt_opcode (pstate, OP_LONG); }
928 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
930 error (_("No file or function \"%s\"."),
931 copy_name ($1.stoken));
939 block : block COLONCOLON name
941 = lookup_symbol (copy_name ($3), $1,
943 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
944 error (_("No function \"%s\" in specified context."),
946 $$ = SYMBOL_BLOCK_VALUE (tem); }
949 variable: name_not_typename ENTRY
950 { struct symbol *sym = $1.sym;
952 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
953 || !symbol_read_needs_frame (sym))
954 error (_("@entry can be used only for function "
955 "parameters, not for \"%s\""),
956 copy_name ($1.stoken));
958 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
959 write_exp_elt_sym (pstate, sym);
960 write_exp_elt_opcode (pstate, OP_VAR_ENTRY_VALUE);
964 variable: block COLONCOLON name
965 { struct symbol *sym;
966 sym = lookup_symbol (copy_name ($3), $1,
969 error (_("No symbol \"%s\" in specified context."),
971 if (symbol_read_needs_frame (sym))
973 if (innermost_block == 0
974 || contained_in (block_found,
976 innermost_block = block_found;
979 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
980 /* block_found is set by lookup_symbol. */
981 write_exp_elt_block (pstate, block_found);
982 write_exp_elt_sym (pstate, sym);
983 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
986 qualified_name: TYPENAME COLONCOLON name
988 struct type *type = $1.type;
989 CHECK_TYPEDEF (type);
990 if (!type_aggregate_p (type))
991 error (_("`%s' is not defined as an aggregate type."),
992 TYPE_SAFE_NAME (type));
994 write_exp_elt_opcode (pstate, OP_SCOPE);
995 write_exp_elt_type (pstate, type);
996 write_exp_string (pstate, $3);
997 write_exp_elt_opcode (pstate, OP_SCOPE);
999 | TYPENAME COLONCOLON '~' name
1001 struct type *type = $1.type;
1002 struct stoken tmp_token;
1005 CHECK_TYPEDEF (type);
1006 if (!type_aggregate_p (type))
1007 error (_("`%s' is not defined as an aggregate type."),
1008 TYPE_SAFE_NAME (type));
1009 buf = alloca ($4.length + 2);
1010 tmp_token.ptr = buf;
1011 tmp_token.length = $4.length + 1;
1013 memcpy (buf+1, $4.ptr, $4.length);
1014 buf[tmp_token.length] = 0;
1016 /* Check for valid destructor name. */
1017 destructor_name_p (tmp_token.ptr, $1.type);
1018 write_exp_elt_opcode (pstate, OP_SCOPE);
1019 write_exp_elt_type (pstate, type);
1020 write_exp_string (pstate, tmp_token);
1021 write_exp_elt_opcode (pstate, OP_SCOPE);
1023 | TYPENAME COLONCOLON name COLONCOLON name
1025 char *copy = copy_name ($3);
1026 error (_("No type \"%s\" within class "
1027 "or namespace \"%s\"."),
1028 copy, TYPE_SAFE_NAME ($1.type));
1032 variable: qualified_name
1033 | COLONCOLON name_not_typename
1035 char *name = copy_name ($2.stoken);
1037 struct bound_minimal_symbol msymbol;
1040 lookup_symbol (name, (const struct block *) NULL,
1044 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1045 write_exp_elt_block (pstate, NULL);
1046 write_exp_elt_sym (pstate, sym);
1047 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1051 msymbol = lookup_bound_minimal_symbol (name);
1052 if (msymbol.minsym != NULL)
1053 write_exp_msymbol (pstate, msymbol);
1054 else if (!have_full_symbols () && !have_partial_symbols ())
1055 error (_("No symbol table is loaded. Use the \"file\" command."));
1057 error (_("No symbol \"%s\" in current context."), name);
1061 variable: name_not_typename
1062 { struct symbol *sym = $1.sym;
1066 if (symbol_read_needs_frame (sym))
1068 if (innermost_block == 0
1069 || contained_in (block_found,
1071 innermost_block = block_found;
1074 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1075 /* We want to use the selected frame, not
1076 another more inner frame which happens to
1077 be in the same block. */
1078 write_exp_elt_block (pstate, NULL);
1079 write_exp_elt_sym (pstate, sym);
1080 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
1082 else if ($1.is_a_field_of_this)
1084 /* C++: it hangs off of `this'. Must
1085 not inadvertently convert from a method call
1087 if (innermost_block == 0
1088 || contained_in (block_found,
1090 innermost_block = block_found;
1091 write_exp_elt_opcode (pstate, OP_THIS);
1092 write_exp_elt_opcode (pstate, OP_THIS);
1093 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1094 write_exp_string (pstate, $1.stoken);
1095 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
1099 struct bound_minimal_symbol msymbol;
1100 char *arg = copy_name ($1.stoken);
1103 lookup_bound_minimal_symbol (arg);
1104 if (msymbol.minsym != NULL)
1105 write_exp_msymbol (pstate, msymbol);
1106 else if (!have_full_symbols () && !have_partial_symbols ())
1107 error (_("No symbol table is loaded. Use the \"file\" command."));
1109 error (_("No symbol \"%s\" in current context."),
1110 copy_name ($1.stoken));
1115 space_identifier : '@' NAME
1116 { insert_type_address_space (pstate, copy_name ($2.stoken)); }
1119 const_or_volatile: const_or_volatile_noopt
1123 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
1126 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
1127 | const_or_volatile_noopt
1130 const_or_volatile_or_space_identifier:
1131 const_or_volatile_or_space_identifier_noopt
1137 { insert_type (tp_pointer); }
1138 const_or_volatile_or_space_identifier
1140 { insert_type (tp_pointer); }
1141 const_or_volatile_or_space_identifier
1143 { insert_type (tp_reference); }
1145 { insert_type (tp_reference); }
1148 ptr_operator_ts: ptr_operator
1150 $$ = get_type_stack ();
1151 /* This cleanup is eventually run by
1153 make_cleanup (type_stack_cleanup, $$);
1157 abs_decl: ptr_operator_ts direct_abs_decl
1158 { $$ = append_type_stack ($2, $1); }
1163 direct_abs_decl: '(' abs_decl ')'
1165 | direct_abs_decl array_mod
1167 push_type_stack ($1);
1169 push_type (tp_array);
1170 $$ = get_type_stack ();
1175 push_type (tp_array);
1176 $$ = get_type_stack ();
1179 | direct_abs_decl func_mod
1181 push_type_stack ($1);
1183 $$ = get_type_stack ();
1188 $$ = get_type_stack ();
1198 | OBJC_LBRAC INT ']'
1204 | '(' parameter_typelist ')'
1208 /* We used to try to recognize pointer to member types here, but
1209 that didn't work (shift/reduce conflicts meant that these rules never
1210 got executed). The problem is that
1211 int (foo::bar::baz::bizzle)
1212 is a function type but
1213 int (foo::bar::baz::bizzle::*)
1214 is a pointer to member type. Stroustrup loses again! */
1219 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1223 { $$ = lookup_signed_typename (parse_language (pstate),
1224 parse_gdbarch (pstate),
1227 { $$ = lookup_signed_typename (parse_language (pstate),
1228 parse_gdbarch (pstate),
1231 { $$ = lookup_signed_typename (parse_language (pstate),
1232 parse_gdbarch (pstate),
1235 { $$ = lookup_signed_typename (parse_language (pstate),
1236 parse_gdbarch (pstate),
1238 | LONG SIGNED_KEYWORD INT_KEYWORD
1239 { $$ = lookup_signed_typename (parse_language (pstate),
1240 parse_gdbarch (pstate),
1242 | LONG SIGNED_KEYWORD
1243 { $$ = lookup_signed_typename (parse_language (pstate),
1244 parse_gdbarch (pstate),
1246 | SIGNED_KEYWORD LONG INT_KEYWORD
1247 { $$ = lookup_signed_typename (parse_language (pstate),
1248 parse_gdbarch (pstate),
1250 | UNSIGNED LONG INT_KEYWORD
1251 { $$ = lookup_unsigned_typename (parse_language (pstate),
1252 parse_gdbarch (pstate),
1254 | LONG UNSIGNED INT_KEYWORD
1255 { $$ = lookup_unsigned_typename (parse_language (pstate),
1256 parse_gdbarch (pstate),
1259 { $$ = lookup_unsigned_typename (parse_language (pstate),
1260 parse_gdbarch (pstate),
1263 { $$ = lookup_signed_typename (parse_language (pstate),
1264 parse_gdbarch (pstate),
1266 | LONG LONG INT_KEYWORD
1267 { $$ = lookup_signed_typename (parse_language (pstate),
1268 parse_gdbarch (pstate),
1270 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1271 { $$ = lookup_signed_typename (parse_language (pstate),
1272 parse_gdbarch (pstate),
1274 | LONG LONG SIGNED_KEYWORD
1275 { $$ = lookup_signed_typename (parse_language (pstate),
1276 parse_gdbarch (pstate),
1278 | SIGNED_KEYWORD LONG LONG
1279 { $$ = lookup_signed_typename (parse_language (pstate),
1280 parse_gdbarch (pstate),
1282 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1283 { $$ = lookup_signed_typename (parse_language (pstate),
1284 parse_gdbarch (pstate),
1286 | UNSIGNED LONG LONG
1287 { $$ = lookup_unsigned_typename (parse_language (pstate),
1288 parse_gdbarch (pstate),
1290 | UNSIGNED LONG LONG INT_KEYWORD
1291 { $$ = lookup_unsigned_typename (parse_language (pstate),
1292 parse_gdbarch (pstate),
1294 | LONG LONG UNSIGNED
1295 { $$ = lookup_unsigned_typename (parse_language (pstate),
1296 parse_gdbarch (pstate),
1298 | LONG LONG UNSIGNED INT_KEYWORD
1299 { $$ = lookup_unsigned_typename (parse_language (pstate),
1300 parse_gdbarch (pstate),
1303 { $$ = lookup_signed_typename (parse_language (pstate),
1304 parse_gdbarch (pstate),
1306 | SHORT SIGNED_KEYWORD INT_KEYWORD
1307 { $$ = lookup_signed_typename (parse_language (pstate),
1308 parse_gdbarch (pstate),
1310 | SHORT SIGNED_KEYWORD
1311 { $$ = lookup_signed_typename (parse_language (pstate),
1312 parse_gdbarch (pstate),
1314 | UNSIGNED SHORT INT_KEYWORD
1315 { $$ = lookup_unsigned_typename (parse_language (pstate),
1316 parse_gdbarch (pstate),
1319 { $$ = lookup_unsigned_typename (parse_language (pstate),
1320 parse_gdbarch (pstate),
1322 | SHORT UNSIGNED INT_KEYWORD
1323 { $$ = lookup_unsigned_typename (parse_language (pstate),
1324 parse_gdbarch (pstate),
1327 { $$ = lookup_typename (parse_language (pstate),
1328 parse_gdbarch (pstate),
1330 (struct block *) NULL,
1332 | LONG DOUBLE_KEYWORD
1333 { $$ = lookup_typename (parse_language (pstate),
1334 parse_gdbarch (pstate),
1336 (struct block *) NULL,
1339 { $$ = lookup_struct (copy_name ($2),
1340 expression_context_block); }
1343 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1346 | STRUCT name COMPLETE
1348 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1353 { $$ = lookup_struct (copy_name ($2),
1354 expression_context_block); }
1357 mark_completion_tag (TYPE_CODE_CLASS, "", 0);
1360 | CLASS name COMPLETE
1362 mark_completion_tag (TYPE_CODE_CLASS, $2.ptr,
1367 { $$ = lookup_union (copy_name ($2),
1368 expression_context_block); }
1371 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1374 | UNION name COMPLETE
1376 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1381 { $$ = lookup_enum (copy_name ($2),
1382 expression_context_block); }
1385 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1388 | ENUM name COMPLETE
1390 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1395 { $$ = lookup_unsigned_typename (parse_language (pstate),
1396 parse_gdbarch (pstate),
1397 TYPE_NAME($2.type)); }
1399 { $$ = lookup_unsigned_typename (parse_language (pstate),
1400 parse_gdbarch (pstate),
1402 | SIGNED_KEYWORD typename
1403 { $$ = lookup_signed_typename (parse_language (pstate),
1404 parse_gdbarch (pstate),
1405 TYPE_NAME($2.type)); }
1407 { $$ = lookup_signed_typename (parse_language (pstate),
1408 parse_gdbarch (pstate),
1410 /* It appears that this rule for templates is never
1411 reduced; template recognition happens by lookahead
1412 in the token processing code in yylex. */
1413 | TEMPLATE name '<' type '>'
1414 { $$ = lookup_template_type(copy_name($2), $4,
1415 expression_context_block);
1417 | const_or_volatile_or_space_identifier_noopt typebase
1418 { $$ = follow_types ($2); }
1419 | typebase const_or_volatile_or_space_identifier_noopt
1420 { $$ = follow_types ($1); }
1426 $$.stoken.ptr = "int";
1427 $$.stoken.length = 3;
1428 $$.type = lookup_signed_typename (parse_language (pstate),
1429 parse_gdbarch (pstate),
1434 $$.stoken.ptr = "long";
1435 $$.stoken.length = 4;
1436 $$.type = lookup_signed_typename (parse_language (pstate),
1437 parse_gdbarch (pstate),
1442 $$.stoken.ptr = "short";
1443 $$.stoken.length = 5;
1444 $$.type = lookup_signed_typename (parse_language (pstate),
1445 parse_gdbarch (pstate),
1452 { check_parameter_typelist ($1); }
1453 | nonempty_typelist ',' DOTDOTDOT
1455 VEC_safe_push (type_ptr, $1, NULL);
1456 check_parameter_typelist ($1);
1464 VEC (type_ptr) *typelist = NULL;
1465 VEC_safe_push (type_ptr, typelist, $1);
1468 | nonempty_typelist ',' type
1470 VEC_safe_push (type_ptr, $1, $3);
1478 push_type_stack ($2);
1479 $$ = follow_types ($1);
1483 conversion_type_id: typebase conversion_declarator
1484 { $$ = follow_types ($1); }
1487 conversion_declarator: /* Nothing. */
1488 | ptr_operator conversion_declarator
1491 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1492 | VOLATILE_KEYWORD CONST_KEYWORD
1495 const_or_volatile_noopt: const_and_volatile
1496 { insert_type (tp_const);
1497 insert_type (tp_volatile);
1500 { insert_type (tp_const); }
1502 { insert_type (tp_volatile); }
1505 operator: OPERATOR NEW
1506 { $$ = operator_stoken (" new"); }
1508 { $$ = operator_stoken (" delete"); }
1509 | OPERATOR NEW '[' ']'
1510 { $$ = operator_stoken (" new[]"); }
1511 | OPERATOR DELETE '[' ']'
1512 { $$ = operator_stoken (" delete[]"); }
1513 | OPERATOR NEW OBJC_LBRAC ']'
1514 { $$ = operator_stoken (" new[]"); }
1515 | OPERATOR DELETE OBJC_LBRAC ']'
1516 { $$ = operator_stoken (" delete[]"); }
1518 { $$ = operator_stoken ("+"); }
1520 { $$ = operator_stoken ("-"); }
1522 { $$ = operator_stoken ("*"); }
1524 { $$ = operator_stoken ("/"); }
1526 { $$ = operator_stoken ("%"); }
1528 { $$ = operator_stoken ("^"); }
1530 { $$ = operator_stoken ("&"); }
1532 { $$ = operator_stoken ("|"); }
1534 { $$ = operator_stoken ("~"); }
1536 { $$ = operator_stoken ("!"); }
1538 { $$ = operator_stoken ("="); }
1540 { $$ = operator_stoken ("<"); }
1542 { $$ = operator_stoken (">"); }
1543 | OPERATOR ASSIGN_MODIFY
1544 { const char *op = "unknown";
1568 case BINOP_BITWISE_IOR:
1571 case BINOP_BITWISE_AND:
1574 case BINOP_BITWISE_XOR:
1581 $$ = operator_stoken (op);
1584 { $$ = operator_stoken ("<<"); }
1586 { $$ = operator_stoken (">>"); }
1588 { $$ = operator_stoken ("=="); }
1590 { $$ = operator_stoken ("!="); }
1592 { $$ = operator_stoken ("<="); }
1594 { $$ = operator_stoken (">="); }
1596 { $$ = operator_stoken ("&&"); }
1598 { $$ = operator_stoken ("||"); }
1599 | OPERATOR INCREMENT
1600 { $$ = operator_stoken ("++"); }
1601 | OPERATOR DECREMENT
1602 { $$ = operator_stoken ("--"); }
1604 { $$ = operator_stoken (","); }
1605 | OPERATOR ARROW_STAR
1606 { $$ = operator_stoken ("->*"); }
1608 { $$ = operator_stoken ("->"); }
1610 { $$ = operator_stoken ("()"); }
1612 { $$ = operator_stoken ("[]"); }
1613 | OPERATOR OBJC_LBRAC ']'
1614 { $$ = operator_stoken ("[]"); }
1615 | OPERATOR conversion_type_id
1618 struct ui_file *buf = mem_fileopen ();
1620 c_print_type ($2, NULL, buf, -1, 0,
1621 &type_print_raw_options);
1622 name = ui_file_xstrdup (buf, &length);
1623 ui_file_delete (buf);
1624 $$ = operator_stoken (name);
1631 name : NAME { $$ = $1.stoken; }
1632 | BLOCKNAME { $$ = $1.stoken; }
1633 | TYPENAME { $$ = $1.stoken; }
1634 | NAME_OR_INT { $$ = $1.stoken; }
1635 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1636 | operator { $$ = $1; }
1639 name_not_typename : NAME
1641 /* These would be useful if name_not_typename was useful, but it is just
1642 a fake for "variable", so these cause reduce/reduce conflicts because
1643 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1644 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1645 context where only a name could occur, this might be useful.
1650 struct field_of_this_result is_a_field_of_this;
1653 $$.sym = lookup_symbol ($1.ptr,
1654 expression_context_block,
1656 &is_a_field_of_this);
1657 $$.is_a_field_of_this
1658 = is_a_field_of_this.type != NULL;
1665 /* Like write_exp_string, but prepends a '~'. */
1668 write_destructor_name (struct parser_state *par_state, struct stoken token)
1670 char *copy = alloca (token.length + 1);
1673 memcpy (©[1], token.ptr, token.length);
1678 write_exp_string (par_state, token);
1681 /* Returns a stoken of the operator name given by OP (which does not
1682 include the string "operator"). */
1683 static struct stoken
1684 operator_stoken (const char *op)
1686 static const char *operator_string = "operator";
1687 struct stoken st = { NULL, 0 };
1690 st.length = strlen (operator_string) + strlen (op);
1691 buf = malloc (st.length + 1);
1692 strcpy (buf, operator_string);
1696 /* The toplevel (c_parse) will free the memory allocated here. */
1697 make_cleanup (free, buf);
1701 /* Return true if the type is aggregate-like. */
1704 type_aggregate_p (struct type *type)
1706 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1707 || TYPE_CODE (type) == TYPE_CODE_UNION
1708 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1709 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1710 && TYPE_DECLARED_CLASS (type)));
1713 /* Validate a parameter typelist. */
1716 check_parameter_typelist (VEC (type_ptr) *params)
1721 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1723 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1727 if (VEC_length (type_ptr, params) == 1)
1732 VEC_free (type_ptr, params);
1733 error (_("parameter types following 'void'"));
1737 VEC_free (type_ptr, params);
1738 error (_("'void' invalid as parameter type"));
1744 /* Take care of parsing a number (anything that starts with a digit).
1745 Set yylval and return the token type; update lexptr.
1746 LEN is the number of characters in it. */
1748 /*** Needs some error checking for the float case ***/
1751 parse_number (struct parser_state *par_state,
1752 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
1754 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1755 here, and we do kind of silly things like cast to unsigned. */
1762 int base = input_radix;
1765 /* Number of "L" suffixes encountered. */
1768 /* We have found a "L" or "U" suffix. */
1769 int found_suffix = 0;
1772 struct type *signed_type;
1773 struct type *unsigned_type;
1777 memcpy (p, buf, len);
1781 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1782 point. Return DECFLOAT. */
1784 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1787 putithere->typed_val_decfloat.type
1788 = parse_type (par_state)->builtin_decfloat;
1789 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1790 gdbarch_byte_order (parse_gdbarch (par_state)),
1796 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1799 putithere->typed_val_decfloat.type
1800 = parse_type (par_state)->builtin_decdouble;
1801 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1802 gdbarch_byte_order (parse_gdbarch (par_state)),
1808 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1811 putithere->typed_val_decfloat.type
1812 = parse_type (par_state)->builtin_declong;
1813 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1814 gdbarch_byte_order (parse_gdbarch (par_state)),
1820 if (! parse_c_float (parse_gdbarch (par_state), p, len,
1821 &putithere->typed_val_float.dval,
1822 &putithere->typed_val_float.type))
1827 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1871 if (c >= 'A' && c <= 'Z')
1873 if (c != 'l' && c != 'u')
1875 if (c >= '0' && c <= '9')
1883 if (base > 10 && c >= 'a' && c <= 'f')
1887 n += i = c - 'a' + 10;
1900 return ERROR; /* Char not a digit */
1903 return ERROR; /* Invalid digit in this base */
1905 /* Portably test for overflow (only works for nonzero values, so make
1906 a second check for zero). FIXME: Can't we just make n and prevn
1907 unsigned and avoid this? */
1908 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1909 unsigned_p = 1; /* Try something unsigned */
1911 /* Portably test for unsigned overflow.
1912 FIXME: This check is wrong; for example it doesn't find overflow
1913 on 0x123456789 when LONGEST is 32 bits. */
1914 if (c != 'l' && c != 'u' && n != 0)
1916 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1917 error (_("Numeric constant too large."));
1922 /* An integer constant is an int, a long, or a long long. An L
1923 suffix forces it to be long; an LL suffix forces it to be long
1924 long. If not forced to a larger size, it gets the first type of
1925 the above that it fits in. To figure out whether it fits, we
1926 shift it right and see whether anything remains. Note that we
1927 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1928 operation, because many compilers will warn about such a shift
1929 (which always produces a zero result). Sometimes gdbarch_int_bit
1930 or gdbarch_long_bit will be that big, sometimes not. To deal with
1931 the case where it is we just always shift the value more than
1932 once, with fewer bits each time. */
1934 un = (ULONGEST)n >> 2;
1936 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0)
1939 = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
1941 /* A large decimal (not hex or octal) constant (between INT_MAX
1942 and UINT_MAX) is a long or unsigned long, according to ANSI,
1943 never an unsigned int, but this code treats it as unsigned
1944 int. This probably should be fixed. GCC gives a warning on
1947 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
1948 signed_type = parse_type (par_state)->builtin_int;
1950 else if (long_p <= 1
1951 && (un >> (gdbarch_long_bit (parse_gdbarch (par_state)) - 2)) == 0)
1954 = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch (par_state)) - 1);
1955 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
1956 signed_type = parse_type (par_state)->builtin_long;
1961 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1962 < gdbarch_long_long_bit (parse_gdbarch (par_state)))
1963 /* A long long does not fit in a LONGEST. */
1964 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1966 shift = (gdbarch_long_long_bit (parse_gdbarch (par_state)) - 1);
1967 high_bit = (ULONGEST) 1 << shift;
1968 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
1969 signed_type = parse_type (par_state)->builtin_long_long;
1972 putithere->typed_val_int.val = n;
1974 /* If the high bit of the worked out type is set then this number
1975 has to be unsigned. */
1977 if (unsigned_p || (n & high_bit))
1979 putithere->typed_val_int.type = unsigned_type;
1983 putithere->typed_val_int.type = signed_type;
1989 /* Temporary obstack used for holding strings. */
1990 static struct obstack tempbuf;
1991 static int tempbuf_init;
1993 /* Parse a C escape sequence. The initial backslash of the sequence
1994 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1995 last character of the sequence. If OUTPUT is not NULL, the
1996 translated form of the escape sequence will be written there. If
1997 OUTPUT is NULL, no output is written and the call will only affect
1998 *PTR. If an escape sequence is expressed in target bytes, then the
1999 entire sequence will simply be copied to OUTPUT. Return 1 if any
2000 character was emitted, 0 otherwise. */
2003 c_parse_escape (const char **ptr, struct obstack *output)
2005 const char *tokptr = *ptr;
2008 /* Some escape sequences undergo character set conversion. Those we
2012 /* Hex escapes do not undergo character set conversion, so keep
2013 the escape sequence for later. */
2016 obstack_grow_str (output, "\\x");
2018 if (!isxdigit (*tokptr))
2019 error (_("\\x escape without a following hex digit"));
2020 while (isxdigit (*tokptr))
2023 obstack_1grow (output, *tokptr);
2028 /* Octal escapes do not undergo character set conversion, so
2029 keep the escape sequence for later. */
2041 obstack_grow_str (output, "\\");
2043 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
2047 obstack_1grow (output, *tokptr);
2053 /* We handle UCNs later. We could handle them here, but that
2054 would mean a spurious error in the case where the UCN could
2055 be converted to the target charset but not the host
2061 int i, len = c == 'U' ? 8 : 4;
2064 obstack_1grow (output, '\\');
2065 obstack_1grow (output, *tokptr);
2068 if (!isxdigit (*tokptr))
2069 error (_("\\%c escape without a following hex digit"), c);
2070 for (i = 0; i < len && isxdigit (*tokptr); ++i)
2073 obstack_1grow (output, *tokptr);
2079 /* We must pass backslash through so that it does not
2080 cause quoting during the second expansion. */
2083 obstack_grow_str (output, "\\\\");
2087 /* Escapes which undergo conversion. */
2090 obstack_1grow (output, '\a');
2095 obstack_1grow (output, '\b');
2100 obstack_1grow (output, '\f');
2105 obstack_1grow (output, '\n');
2110 obstack_1grow (output, '\r');
2115 obstack_1grow (output, '\t');
2120 obstack_1grow (output, '\v');
2124 /* GCC extension. */
2127 obstack_1grow (output, HOST_ESCAPE_CHAR);
2131 /* Backslash-newline expands to nothing at all. */
2137 /* A few escapes just expand to the character itself. */
2141 /* GCC extensions. */
2146 /* Unrecognized escapes turn into the character itself. */
2149 obstack_1grow (output, *tokptr);
2157 /* Parse a string or character literal from TOKPTR. The string or
2158 character may be wide or unicode. *OUTPTR is set to just after the
2159 end of the literal in the input string. The resulting token is
2160 stored in VALUE. This returns a token value, either STRING or
2161 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2162 number of host characters in the literal. */
2164 parse_string_or_char (const char *tokptr, const char **outptr,
2165 struct typed_stoken *value, int *host_chars)
2168 enum c_string_type type;
2171 /* Build the gdb internal form of the input string in tempbuf. Note
2172 that the buffer is null byte terminated *only* for the
2173 convenience of debugging gdb itself and printing the buffer
2174 contents when the buffer contains no embedded nulls. Gdb does
2175 not depend upon the buffer being null byte terminated, it uses
2176 the length string instead. This allows gdb to handle C strings
2177 (as well as strings in other languages) with embedded null
2183 obstack_free (&tempbuf, NULL);
2184 obstack_init (&tempbuf);
2186 /* Record the string type. */
2189 type = C_WIDE_STRING;
2192 else if (*tokptr == 'u')
2197 else if (*tokptr == 'U')
2202 else if (*tokptr == '@')
2204 /* An Objective C string. */
2212 /* Skip the quote. */
2226 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2228 else if (c == quote)
2232 obstack_1grow (&tempbuf, c);
2234 /* FIXME: this does the wrong thing with multi-byte host
2235 characters. We could use mbrlen here, but that would
2236 make "set host-charset" a bit less useful. */
2241 if (*tokptr != quote)
2244 error (_("Unterminated string in expression."));
2246 error (_("Unmatched single quote."));
2251 value->ptr = obstack_base (&tempbuf);
2252 value->length = obstack_object_size (&tempbuf);
2256 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
2259 /* This is used to associate some attributes with a token. */
2263 /* If this bit is set, the token is C++-only. */
2267 /* If this bit is set, the token is conditional: if there is a
2268 symbol of the same name, then the token is a symbol; otherwise,
2269 the token is a keyword. */
2278 enum exp_opcode opcode;
2279 enum token_flags flags;
2282 static const struct token tokentab3[] =
2284 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2285 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2286 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2287 {"...", DOTDOTDOT, BINOP_END, 0}
2290 static const struct token tokentab2[] =
2292 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2293 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2294 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2295 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2296 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2297 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2298 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2299 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2300 {"++", INCREMENT, BINOP_END, 0},
2301 {"--", DECREMENT, BINOP_END, 0},
2302 {"->", ARROW, BINOP_END, 0},
2303 {"&&", ANDAND, BINOP_END, 0},
2304 {"||", OROR, BINOP_END, 0},
2305 /* "::" is *not* only C++: gdb overrides its meaning in several
2306 different ways, e.g., 'filename'::func, function::variable. */
2307 {"::", COLONCOLON, BINOP_END, 0},
2308 {"<<", LSH, BINOP_END, 0},
2309 {">>", RSH, BINOP_END, 0},
2310 {"==", EQUAL, BINOP_END, 0},
2311 {"!=", NOTEQUAL, BINOP_END, 0},
2312 {"<=", LEQ, BINOP_END, 0},
2313 {">=", GEQ, BINOP_END, 0},
2314 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2317 /* Identifier-like tokens. */
2318 static const struct token ident_tokens[] =
2320 {"unsigned", UNSIGNED, OP_NULL, 0},
2321 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2322 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2323 {"struct", STRUCT, OP_NULL, 0},
2324 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2325 {"sizeof", SIZEOF, OP_NULL, 0},
2326 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2327 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2328 {"class", CLASS, OP_NULL, FLAG_CXX},
2329 {"union", UNION, OP_NULL, 0},
2330 {"short", SHORT, OP_NULL, 0},
2331 {"const", CONST_KEYWORD, OP_NULL, 0},
2332 {"enum", ENUM, OP_NULL, 0},
2333 {"long", LONG, OP_NULL, 0},
2334 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2335 {"int", INT_KEYWORD, OP_NULL, 0},
2336 {"new", NEW, OP_NULL, FLAG_CXX},
2337 {"delete", DELETE, OP_NULL, FLAG_CXX},
2338 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2340 {"and", ANDAND, BINOP_END, FLAG_CXX},
2341 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2342 {"bitand", '&', OP_NULL, FLAG_CXX},
2343 {"bitor", '|', OP_NULL, FLAG_CXX},
2344 {"compl", '~', OP_NULL, FLAG_CXX},
2345 {"not", '!', OP_NULL, FLAG_CXX},
2346 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2347 {"or", OROR, BINOP_END, FLAG_CXX},
2348 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2349 {"xor", '^', OP_NULL, FLAG_CXX},
2350 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2352 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2353 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2354 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2355 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2357 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2358 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2359 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2360 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2361 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2363 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
2366 /* When we find that lexptr (the global var defined in parse.c) is
2367 pointing at a macro invocation, we expand the invocation, and call
2368 scan_macro_expansion to save the old lexptr here and point lexptr
2369 into the expanded text. When we reach the end of that, we call
2370 end_macro_expansion to pop back to the value we saved here. The
2371 macro expansion code promises to return only fully-expanded text,
2372 so we don't need to "push" more than one level.
2374 This is disgusting, of course. It would be cleaner to do all macro
2375 expansion beforehand, and then hand that to lexptr. But we don't
2376 really know where the expression ends. Remember, in a command like
2378 (gdb) break *ADDRESS if CONDITION
2380 we evaluate ADDRESS in the scope of the current frame, but we
2381 evaluate CONDITION in the scope of the breakpoint's location. So
2382 it's simply wrong to try to macro-expand the whole thing at once. */
2383 static const char *macro_original_text;
2385 /* We save all intermediate macro expansions on this obstack for the
2386 duration of a single parse. The expansion text may sometimes have
2387 to live past the end of the expansion, due to yacc lookahead.
2388 Rather than try to be clever about saving the data for a single
2389 token, we simply keep it all and delete it after parsing has
2391 static struct obstack expansion_obstack;
2394 scan_macro_expansion (char *expansion)
2398 /* We'd better not be trying to push the stack twice. */
2399 gdb_assert (! macro_original_text);
2401 /* Copy to the obstack, and then free the intermediate
2403 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2406 /* Save the old lexptr value, so we can return to it when we're done
2407 parsing the expanded text. */
2408 macro_original_text = lexptr;
2414 scanning_macro_expansion (void)
2416 return macro_original_text != 0;
2421 finished_macro_expansion (void)
2423 /* There'd better be something to pop back to. */
2424 gdb_assert (macro_original_text);
2426 /* Pop back to the original text. */
2427 lexptr = macro_original_text;
2428 macro_original_text = 0;
2433 scan_macro_cleanup (void *dummy)
2435 if (macro_original_text)
2436 finished_macro_expansion ();
2438 obstack_free (&expansion_obstack, NULL);
2441 /* Return true iff the token represents a C++ cast operator. */
2444 is_cast_operator (const char *token, int len)
2446 return (! strncmp (token, "dynamic_cast", len)
2447 || ! strncmp (token, "static_cast", len)
2448 || ! strncmp (token, "reinterpret_cast", len)
2449 || ! strncmp (token, "const_cast", len));
2452 /* The scope used for macro expansion. */
2453 static struct macro_scope *expression_macro_scope;
2455 /* This is set if a NAME token appeared at the very end of the input
2456 string, with no whitespace separating the name from the EOF. This
2457 is used only when parsing to do field name completion. */
2458 static int saw_name_at_eof;
2460 /* This is set if the previously-returned token was a structure
2461 operator -- either '.' or ARROW. This is used only when parsing to
2462 do field name completion. */
2463 static int last_was_structop;
2465 /* Read one token, getting characters through lexptr. */
2468 lex_one_token (struct parser_state *par_state, int *is_quoted_name)
2473 const char *tokstart;
2474 int saw_structop = last_was_structop;
2477 last_was_structop = 0;
2478 *is_quoted_name = 0;
2482 /* Check if this is a macro invocation that we need to expand. */
2483 if (! scanning_macro_expansion ())
2485 char *expanded = macro_expand_next (&lexptr,
2486 standard_macro_lookup,
2487 expression_macro_scope);
2490 scan_macro_expansion (expanded);
2493 prev_lexptr = lexptr;
2496 /* See if it is a special token of length 3. */
2497 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2498 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2500 if ((tokentab3[i].flags & FLAG_CXX) != 0
2501 && parse_language (par_state)->la_language != language_cplus)
2505 yylval.opcode = tokentab3[i].opcode;
2506 return tokentab3[i].token;
2509 /* See if it is a special token of length 2. */
2510 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2511 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2513 if ((tokentab2[i].flags & FLAG_CXX) != 0
2514 && parse_language (par_state)->la_language != language_cplus)
2518 yylval.opcode = tokentab2[i].opcode;
2519 if (parse_completion && tokentab2[i].token == ARROW)
2520 last_was_structop = 1;
2521 return tokentab2[i].token;
2524 switch (c = *tokstart)
2527 /* If we were just scanning the result of a macro expansion,
2528 then we need to resume scanning the original text.
2529 If we're parsing for field name completion, and the previous
2530 token allows such completion, return a COMPLETE token.
2531 Otherwise, we were already scanning the original text, and
2532 we're really done. */
2533 if (scanning_macro_expansion ())
2535 finished_macro_expansion ();
2538 else if (saw_name_at_eof)
2540 saw_name_at_eof = 0;
2543 else if (saw_structop)
2558 if (parse_language (par_state)->la_language == language_objc
2565 if (paren_depth == 0)
2572 if (comma_terminates
2574 && ! scanning_macro_expansion ())
2580 /* Might be a floating point number. */
2581 if (lexptr[1] < '0' || lexptr[1] > '9')
2583 if (parse_completion)
2584 last_was_structop = 1;
2585 goto symbol; /* Nope, must be a symbol. */
2587 /* FALL THRU into number case. */
2600 /* It's a number. */
2601 int got_dot = 0, got_e = 0, toktype;
2602 const char *p = tokstart;
2603 int hex = input_radix > 10;
2605 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2610 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2618 /* This test includes !hex because 'e' is a valid hex digit
2619 and thus does not indicate a floating point number when
2620 the radix is hex. */
2621 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2622 got_dot = got_e = 1;
2623 /* This test does not include !hex, because a '.' always indicates
2624 a decimal floating point number regardless of the radix. */
2625 else if (!got_dot && *p == '.')
2627 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2628 && (*p == '-' || *p == '+'))
2629 /* This is the sign of the exponent, not the end of the
2632 /* We will take any letters or digits. parse_number will
2633 complain if past the radix, or if L or U are not final. */
2634 else if ((*p < '0' || *p > '9')
2635 && ((*p < 'a' || *p > 'z')
2636 && (*p < 'A' || *p > 'Z')))
2639 toktype = parse_number (par_state, tokstart, p - tokstart,
2640 got_dot|got_e, &yylval);
2641 if (toktype == ERROR)
2643 char *err_copy = (char *) alloca (p - tokstart + 1);
2645 memcpy (err_copy, tokstart, p - tokstart);
2646 err_copy[p - tokstart] = 0;
2647 error (_("Invalid number \"%s\"."), err_copy);
2655 const char *p = &tokstart[1];
2656 size_t len = strlen ("entry");
2658 if (parse_language (par_state)->la_language == language_objc)
2660 size_t len = strlen ("selector");
2662 if (strncmp (p, "selector", len) == 0
2663 && (p[len] == '\0' || isspace (p[len])))
2672 while (isspace (*p))
2674 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2706 if (tokstart[1] != '"' && tokstart[1] != '\'')
2715 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2720 error (_("Empty character constant."));
2721 else if (host_len > 2 && c == '\'')
2724 namelen = lexptr - tokstart - 1;
2725 *is_quoted_name = 1;
2729 else if (host_len > 1)
2730 error (_("Invalid character constant."));
2736 if (!(c == '_' || c == '$'
2737 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2738 /* We must have come across a bad character (e.g. ';'). */
2739 error (_("Invalid character '%c' in expression."), c);
2741 /* It's a name. See how long it is. */
2743 for (c = tokstart[namelen];
2744 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2745 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2747 /* Template parameter lists are part of the name.
2748 FIXME: This mishandles `print $a<4&&$a>3'. */
2752 if (! is_cast_operator (tokstart, namelen))
2754 /* Scan ahead to get rest of the template specification. Note
2755 that we look ahead only when the '<' adjoins non-whitespace
2756 characters; for comparison expressions, e.g. "a < b > c",
2757 there must be spaces before the '<', etc. */
2759 const char *p = find_template_name_end (tokstart + namelen);
2762 namelen = p - tokstart;
2766 c = tokstart[++namelen];
2769 /* The token "if" terminates the expression and is NOT removed from
2770 the input stream. It doesn't count if it appears in the
2771 expansion of a macro. */
2773 && tokstart[0] == 'i'
2774 && tokstart[1] == 'f'
2775 && ! scanning_macro_expansion ())
2780 /* For the same reason (breakpoint conditions), "thread N"
2781 terminates the expression. "thread" could be an identifier, but
2782 an identifier is never followed by a number without intervening
2783 punctuation. "task" is similar. Handle abbreviations of these,
2784 similarly to breakpoint.c:find_condition_and_thread. */
2786 && (strncmp (tokstart, "thread", namelen) == 0
2787 || strncmp (tokstart, "task", namelen) == 0)
2788 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2789 && ! scanning_macro_expansion ())
2791 const char *p = tokstart + namelen + 1;
2793 while (*p == ' ' || *p == '\t')
2795 if (*p >= '0' && *p <= '9')
2803 yylval.sval.ptr = tokstart;
2804 yylval.sval.length = namelen;
2806 /* Catch specific keywords. */
2807 copy = copy_name (yylval.sval);
2808 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2809 if (strcmp (copy, ident_tokens[i].operator) == 0)
2811 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2812 && parse_language (par_state)->la_language != language_cplus)
2815 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2817 struct field_of_this_result is_a_field_of_this;
2819 if (lookup_symbol (copy, expression_context_block,
2821 (parse_language (par_state)->la_language
2822 == language_cplus ? &is_a_field_of_this
2826 /* The keyword is shadowed. */
2831 /* It is ok to always set this, even though we don't always
2832 strictly need to. */
2833 yylval.opcode = ident_tokens[i].opcode;
2834 return ident_tokens[i].token;
2837 if (*tokstart == '$')
2840 if (parse_completion && *lexptr == '\0')
2841 saw_name_at_eof = 1;
2843 yylval.ssym.stoken = yylval.sval;
2844 yylval.ssym.sym = NULL;
2845 yylval.ssym.is_a_field_of_this = 0;
2849 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2856 DEF_VEC_O (token_and_value);
2858 /* A FIFO of tokens that have been read but not yet returned to the
2860 static VEC (token_and_value) *token_fifo;
2862 /* Non-zero if the lexer should return tokens from the FIFO. */
2865 /* Temporary storage for c_lex; this holds symbol names as they are
2867 static struct obstack name_obstack;
2869 /* Classify a NAME token. The contents of the token are in `yylval'.
2870 Updates yylval and returns the new token type. BLOCK is the block
2871 in which lookups start; this can be NULL to mean the global scope.
2872 IS_QUOTED_NAME is non-zero if the name token was originally quoted
2873 in single quotes. */
2875 classify_name (struct parser_state *par_state, const struct block *block,
2880 struct field_of_this_result is_a_field_of_this;
2882 copy = copy_name (yylval.sval);
2884 /* Initialize this in case we *don't* use it in this call; that way
2885 we can refer to it unconditionally below. */
2886 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2888 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2889 parse_language (par_state)->la_name_of_this
2890 ? &is_a_field_of_this : NULL);
2892 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2894 yylval.ssym.sym = sym;
2895 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2900 /* If we found a field of 'this', we might have erroneously
2901 found a constructor where we wanted a type name. Handle this
2902 case by noticing that we found a constructor and then look up
2903 the type tag instead. */
2904 if (is_a_field_of_this.type != NULL
2905 && is_a_field_of_this.fn_field != NULL
2906 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2909 struct field_of_this_result inner_is_a_field_of_this;
2911 sym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2912 &inner_is_a_field_of_this);
2915 yylval.tsym.type = SYMBOL_TYPE (sym);
2920 /* If we found a field, then we want to prefer it over a
2921 filename. However, if the name was quoted, then it is better
2922 to check for a filename or a block, since this is the only
2923 way the user has of requiring the extension to be used. */
2924 if (is_a_field_of_this.type == NULL || is_quoted_name)
2926 /* See if it's a file name. */
2927 struct symtab *symtab;
2929 symtab = lookup_symtab (copy);
2932 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
2939 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2941 yylval.tsym.type = SYMBOL_TYPE (sym);
2946 = language_lookup_primitive_type_by_name (parse_language (par_state),
2947 parse_gdbarch (par_state),
2949 if (yylval.tsym.type != NULL)
2952 /* See if it's an ObjC classname. */
2953 if (parse_language (par_state)->la_language == language_objc && !sym)
2955 CORE_ADDR Class = lookup_objc_class (parse_gdbarch (par_state), copy);
2958 yylval.class.class = Class;
2959 sym = lookup_struct_typedef (copy, expression_context_block, 1);
2961 yylval.class.type = SYMBOL_TYPE (sym);
2966 /* Input names that aren't symbols but ARE valid hex numbers, when
2967 the input radix permits them, can be names or numbers depending
2968 on the parse. Note we support radixes > 16 here. */
2970 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2971 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2973 YYSTYPE newlval; /* Its value is ignored. */
2974 int hextype = parse_number (par_state, copy, yylval.sval.length,
2978 yylval.ssym.sym = sym;
2979 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2984 /* Any other kind of symbol */
2985 yylval.ssym.sym = sym;
2986 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
2989 && parse_language (par_state)->la_language == language_cplus
2990 && is_a_field_of_this.type == NULL
2991 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
2992 return UNKNOWN_CPP_NAME;
2997 /* Like classify_name, but used by the inner loop of the lexer, when a
2998 name might have already been seen. CONTEXT is the context type, or
2999 NULL if this is the first component of a name. */
3002 classify_inner_name (struct parser_state *par_state,
3003 const struct block *block, struct type *context)
3008 if (context == NULL)
3009 return classify_name (par_state, block, 0);
3011 type = check_typedef (context);
3012 if (!type_aggregate_p (type))
3015 copy = copy_name (yylval.ssym.stoken);
3016 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block);
3018 /* If no symbol was found, search for a matching base class named
3019 COPY. This will allow users to enter qualified names of class members
3020 relative to the `this' pointer. */
3021 if (yylval.ssym.sym == NULL)
3023 struct type *base_type = find_type_baseclass_by_name (type, copy);
3025 if (base_type != NULL)
3027 yylval.tsym.type = base_type;
3034 switch (SYMBOL_CLASS (yylval.ssym.sym))
3038 /* cp_lookup_nested_symbol might have accidentally found a constructor
3039 named COPY when we really wanted a base class of the same name.
3040 Double-check this case by looking for a base class. */
3042 struct type *base_type = find_type_baseclass_by_name (type, copy);
3044 if (base_type != NULL)
3046 yylval.tsym.type = base_type;
3053 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
3059 internal_error (__FILE__, __LINE__, _("not reached"));
3062 /* The outer level of a two-level lexer. This calls the inner lexer
3063 to return tokens. It then either returns these tokens, or
3064 aggregates them into a larger token. This lets us work around a
3065 problem in our parsing approach, where the parser could not
3066 distinguish between qualified names and qualified types at the
3069 This approach is still not ideal, because it mishandles template
3070 types. See the comment in lex_one_token for an example. However,
3071 this is still an improvement over the earlier approach, and will
3072 suffice until we move to better parsing technology. */
3076 token_and_value current;
3077 int first_was_coloncolon, last_was_coloncolon;
3078 struct type *context_type = NULL;
3079 int last_to_examine, next_to_examine, checkpoint;
3080 const struct block *search_block;
3083 if (popping && !VEC_empty (token_and_value, token_fifo))
3087 /* Read the first token and decide what to do. Most of the
3088 subsequent code is C++-only; but also depends on seeing a "::" or
3090 current.token = lex_one_token (pstate, &is_quoted_name);
3091 if (current.token == NAME)
3092 current.token = classify_name (pstate, expression_context_block,
3094 if (parse_language (pstate)->la_language != language_cplus
3095 || (current.token != TYPENAME && current.token != COLONCOLON
3096 && current.token != FILENAME))
3097 return current.token;
3099 /* Read any sequence of alternating "::" and name-like tokens into
3101 current.value = yylval;
3102 VEC_safe_push (token_and_value, token_fifo, ¤t);
3103 last_was_coloncolon = current.token == COLONCOLON;
3108 /* We ignore quoted names other than the very first one.
3109 Subsequent ones do not have any special meaning. */
3110 current.token = lex_one_token (pstate, &ignore);
3111 current.value = yylval;
3112 VEC_safe_push (token_and_value, token_fifo, ¤t);
3114 if ((last_was_coloncolon && current.token != NAME)
3115 || (!last_was_coloncolon && current.token != COLONCOLON))
3117 last_was_coloncolon = !last_was_coloncolon;
3121 /* We always read one extra token, so compute the number of tokens
3122 to examine accordingly. */
3123 last_to_examine = VEC_length (token_and_value, token_fifo) - 2;
3124 next_to_examine = 0;
3126 current = *VEC_index (token_and_value, token_fifo, next_to_examine);
3129 obstack_free (&name_obstack, obstack_base (&name_obstack));
3131 if (current.token == FILENAME)
3132 search_block = current.value.bval;
3133 else if (current.token == COLONCOLON)
3134 search_block = NULL;
3137 gdb_assert (current.token == TYPENAME);
3138 search_block = expression_context_block;
3139 obstack_grow (&name_obstack, current.value.sval.ptr,
3140 current.value.sval.length);
3141 context_type = current.value.tsym.type;
3145 first_was_coloncolon = current.token == COLONCOLON;
3146 last_was_coloncolon = first_was_coloncolon;
3148 while (next_to_examine <= last_to_examine)
3150 token_and_value *next;
3152 next = VEC_index (token_and_value, token_fifo, next_to_examine);
3155 if (next->token == NAME && last_was_coloncolon)
3159 yylval = next->value;
3160 classification = classify_inner_name (pstate, search_block,
3162 /* We keep going until we either run out of names, or until
3163 we have a qualified name which is not a type. */
3164 if (classification != TYPENAME && classification != NAME)
3167 /* Accept up to this token. */
3168 checkpoint = next_to_examine;
3170 /* Update the partial name we are constructing. */
3171 if (context_type != NULL)
3173 /* We don't want to put a leading "::" into the name. */
3174 obstack_grow_str (&name_obstack, "::");
3176 obstack_grow (&name_obstack, next->value.sval.ptr,
3177 next->value.sval.length);
3179 yylval.sval.ptr = obstack_base (&name_obstack);
3180 yylval.sval.length = obstack_object_size (&name_obstack);
3181 current.value = yylval;
3182 current.token = classification;
3184 last_was_coloncolon = 0;
3186 if (classification == NAME)
3189 context_type = yylval.tsym.type;
3191 else if (next->token == COLONCOLON && !last_was_coloncolon)
3192 last_was_coloncolon = 1;
3195 /* We've reached the end of the name. */
3200 /* If we have a replacement token, install it as the first token in
3201 the FIFO, and delete the other constituent tokens. */
3204 current.value.sval.ptr = obstack_copy0 (&expansion_obstack,
3205 current.value.sval.ptr,
3206 current.value.sval.length);
3208 VEC_replace (token_and_value, token_fifo, 0, ¤t);
3210 VEC_block_remove (token_and_value, token_fifo, 1, checkpoint - 1);
3214 current = *VEC_index (token_and_value, token_fifo, 0);
3215 VEC_ordered_remove (token_and_value, token_fifo, 0);
3216 yylval = current.value;
3217 return current.token;
3221 c_parse (struct parser_state *par_state)
3224 struct cleanup *back_to;
3226 /* Setting up the parser state. */
3227 gdb_assert (par_state != NULL);
3230 back_to = make_cleanup (free_current_contents, &expression_macro_scope);
3231 make_cleanup_clear_parser_state (&pstate);
3233 /* Set up the scope for macro expansion. */
3234 expression_macro_scope = NULL;
3236 if (expression_context_block)
3237 expression_macro_scope
3238 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
3240 expression_macro_scope = default_macro_scope ();
3241 if (! expression_macro_scope)
3242 expression_macro_scope = user_macro_scope ();
3244 /* Initialize macro expansion code. */
3245 obstack_init (&expansion_obstack);
3246 gdb_assert (! macro_original_text);
3247 make_cleanup (scan_macro_cleanup, 0);
3249 make_cleanup_restore_integer (&yydebug);
3250 yydebug = parser_debug;
3252 /* Initialize some state used by the lexer. */
3253 last_was_structop = 0;
3254 saw_name_at_eof = 0;
3256 VEC_free (token_and_value, token_fifo);
3258 obstack_init (&name_obstack);
3259 make_cleanup_obstack_free (&name_obstack);
3261 result = yyparse ();
3262 do_cleanups (back_to);
3269 /* This is called via the YYPRINT macro when parser debugging is
3270 enabled. It prints a token's value. */
3273 c_print_token (FILE *file, int type, YYSTYPE value)
3278 fprintf (file, "typed_val_int<%s, %s>",
3279 TYPE_SAFE_NAME (value.typed_val_int.type),
3280 pulongest (value.typed_val_int.val));
3286 char *copy = alloca (value.tsval.length + 1);
3288 memcpy (copy, value.tsval.ptr, value.tsval.length);
3289 copy[value.tsval.length] = '\0';
3291 fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
3297 fprintf (file, "sval<%s>", copy_name (value.sval));
3301 fprintf (file, "tsym<type=%s, name=%s>",
3302 TYPE_SAFE_NAME (value.tsym.type),
3303 copy_name (value.tsym.stoken));
3307 case UNKNOWN_CPP_NAME:
3310 fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3311 copy_name (value.ssym.stoken),
3312 (value.ssym.sym == NULL
3313 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym)),
3314 value.ssym.is_a_field_of_this);
3318 fprintf (file, "bval<%s>", host_address_to_string (value.bval));
3329 lexptr = prev_lexptr;
3331 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);