1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Parse a C expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
41 #include "gdb_string.h"
43 #include "expression.h"
45 #include "parser-defs.h"
48 #include "bfd.h" /* Required by objfiles.h. */
49 #include "symfile.h" /* Required by objfiles.h. */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 #include "cp-support.h"
55 #include "gdb_assert.h"
56 #include "macroscope.h"
58 #define parse_type builtin_type (parse_gdbarch)
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
109 #define YYDEBUG 1 /* Default to yydebug support */
112 #define YYFPRINTF parser_fprintf
116 static int yylex (void);
118 void yyerror (char *);
122 /* Although the yacc "value" of an expression is not used,
123 since the result is stored in the structure being created,
124 other node types do have values. */
140 } typed_val_decfloat;
144 struct typed_stoken tsval;
146 struct symtoken ssym;
149 enum exp_opcode opcode;
150 struct internalvar *ivar;
152 struct stoken_vector svec;
158 /* YYSTYPE gets defined by %union */
159 static int parse_number (char *, int, int, YYSTYPE *);
160 static struct stoken operator_stoken (const char *);
163 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
165 %type <tval> type typebase
166 %type <tvec> nonempty_typelist
167 /* %type <bval> block */
169 /* Fancy type parsing. */
170 %type <voidval> func_mod direct_abs_decl abs_decl
172 %type <lval> array_mod
174 %token <typed_val_int> INT
175 %token <typed_val_float> FLOAT
176 %token <typed_val_decfloat> DECFLOAT
178 /* Both NAME and TYPENAME tokens represent symbols in the input,
179 and both convey their data as strings.
180 But a TYPENAME is a string that happens to be defined as a typedef
181 or builtin type name (such as int or char)
182 and a NAME is any other symbol.
183 Contexts where this distinction is not important can use the
184 nonterminal "name", which matches either NAME or TYPENAME. */
186 %token <tsval> STRING
188 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
189 %token <voidval> COMPLETE
190 %token <tsym> TYPENAME
192 %type <svec> string_exp
193 %type <ssym> name_not_typename
194 %type <tsym> typename
196 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
197 but which would parse as a valid number in the current input radix.
198 E.g. "c" when input_radix==16. Depending on the parse, it will be
199 turned into a name or into a number. */
201 %token <ssym> NAME_OR_INT
204 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
208 %type <sval> operator
209 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
211 /* Special type cases, put in to allow the parser to distinguish different
213 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
215 %token <sval> VARIABLE
217 %token <opcode> ASSIGN_MODIFY
226 %right '=' ASSIGN_MODIFY
234 %left '<' '>' LEQ GEQ
239 %right UNARY INCREMENT DECREMENT
240 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
241 %token <ssym> BLOCKNAME
242 %token <bval> FILENAME
254 { write_exp_elt_opcode(OP_TYPE);
255 write_exp_elt_type($1);
256 write_exp_elt_opcode(OP_TYPE);}
259 /* Expressions, including the comma operator. */
262 { write_exp_elt_opcode (BINOP_COMMA); }
265 /* Expressions, not including the comma operator. */
266 exp : '*' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_IND); }
270 exp : '&' exp %prec UNARY
271 { write_exp_elt_opcode (UNOP_ADDR); }
274 exp : '-' exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_NEG); }
278 exp : '+' exp %prec UNARY
279 { write_exp_elt_opcode (UNOP_PLUS); }
282 exp : '!' exp %prec UNARY
283 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
286 exp : '~' exp %prec UNARY
287 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
290 exp : INCREMENT exp %prec UNARY
291 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
294 exp : DECREMENT exp %prec UNARY
295 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
298 exp : exp INCREMENT %prec UNARY
299 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
302 exp : exp DECREMENT %prec UNARY
303 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
306 exp : SIZEOF exp %prec UNARY
307 { write_exp_elt_opcode (UNOP_SIZEOF); }
311 { write_exp_elt_opcode (STRUCTOP_PTR);
312 write_exp_string ($3);
313 write_exp_elt_opcode (STRUCTOP_PTR); }
316 exp : exp ARROW name COMPLETE
317 { mark_struct_expression ();
318 write_exp_elt_opcode (STRUCTOP_PTR);
319 write_exp_string ($3);
320 write_exp_elt_opcode (STRUCTOP_PTR); }
323 exp : exp ARROW COMPLETE
325 mark_struct_expression ();
326 write_exp_elt_opcode (STRUCTOP_PTR);
329 write_exp_string (s);
330 write_exp_elt_opcode (STRUCTOP_PTR); }
333 exp : exp ARROW qualified_name
334 { /* exp->type::name becomes exp->*(&type::name) */
335 /* Note: this doesn't work if name is a
336 static member! FIXME */
337 write_exp_elt_opcode (UNOP_ADDR);
338 write_exp_elt_opcode (STRUCTOP_MPTR); }
341 exp : exp ARROW_STAR exp
342 { write_exp_elt_opcode (STRUCTOP_MPTR); }
346 { write_exp_elt_opcode (STRUCTOP_STRUCT);
347 write_exp_string ($3);
348 write_exp_elt_opcode (STRUCTOP_STRUCT); }
351 exp : exp '.' name COMPLETE
352 { mark_struct_expression ();
353 write_exp_elt_opcode (STRUCTOP_STRUCT);
354 write_exp_string ($3);
355 write_exp_elt_opcode (STRUCTOP_STRUCT); }
358 exp : exp '.' COMPLETE
360 mark_struct_expression ();
361 write_exp_elt_opcode (STRUCTOP_STRUCT);
364 write_exp_string (s);
365 write_exp_elt_opcode (STRUCTOP_STRUCT); }
368 exp : exp '.' qualified_name
369 { /* exp.type::name becomes exp.*(&type::name) */
370 /* Note: this doesn't work if name is a
371 static member! FIXME */
372 write_exp_elt_opcode (UNOP_ADDR);
373 write_exp_elt_opcode (STRUCTOP_MEMBER); }
376 exp : exp DOT_STAR exp
377 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
380 exp : exp '[' exp1 ']'
381 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
385 /* This is to save the value of arglist_len
386 being accumulated by an outer function call. */
387 { start_arglist (); }
388 arglist ')' %prec ARROW
389 { write_exp_elt_opcode (OP_FUNCALL);
390 write_exp_elt_longcst ((LONGEST) end_arglist ());
391 write_exp_elt_opcode (OP_FUNCALL); }
395 { start_arglist (); }
405 arglist : arglist ',' exp %prec ABOVE_COMMA
409 exp : exp '(' nonempty_typelist ')' const_or_volatile
411 write_exp_elt_opcode (TYPE_INSTANCE);
412 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
413 for (i = 0; i < $<ivec>3[0]; ++i)
414 write_exp_elt_type ($<tvec>3[i + 1]);
415 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
416 write_exp_elt_opcode (TYPE_INSTANCE);
422 { $$ = end_arglist () - 1; }
424 exp : lcurly arglist rcurly %prec ARROW
425 { write_exp_elt_opcode (OP_ARRAY);
426 write_exp_elt_longcst ((LONGEST) 0);
427 write_exp_elt_longcst ((LONGEST) $3);
428 write_exp_elt_opcode (OP_ARRAY); }
431 exp : lcurly type rcurly exp %prec UNARY
432 { write_exp_elt_opcode (UNOP_MEMVAL);
433 write_exp_elt_type ($2);
434 write_exp_elt_opcode (UNOP_MEMVAL); }
437 exp : '(' type ')' exp %prec UNARY
438 { write_exp_elt_opcode (UNOP_CAST);
439 write_exp_elt_type ($2);
440 write_exp_elt_opcode (UNOP_CAST); }
447 /* Binary operators in order of decreasing precedence. */
450 { write_exp_elt_opcode (BINOP_REPEAT); }
454 { write_exp_elt_opcode (BINOP_MUL); }
458 { write_exp_elt_opcode (BINOP_DIV); }
462 { write_exp_elt_opcode (BINOP_REM); }
466 { write_exp_elt_opcode (BINOP_ADD); }
470 { write_exp_elt_opcode (BINOP_SUB); }
474 { write_exp_elt_opcode (BINOP_LSH); }
478 { write_exp_elt_opcode (BINOP_RSH); }
482 { write_exp_elt_opcode (BINOP_EQUAL); }
485 exp : exp NOTEQUAL exp
486 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
490 { write_exp_elt_opcode (BINOP_LEQ); }
494 { write_exp_elt_opcode (BINOP_GEQ); }
498 { write_exp_elt_opcode (BINOP_LESS); }
502 { write_exp_elt_opcode (BINOP_GTR); }
506 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
510 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
514 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
518 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
522 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
525 exp : exp '?' exp ':' exp %prec '?'
526 { write_exp_elt_opcode (TERNOP_COND); }
530 { write_exp_elt_opcode (BINOP_ASSIGN); }
533 exp : exp ASSIGN_MODIFY exp
534 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
535 write_exp_elt_opcode ($2);
536 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
540 { write_exp_elt_opcode (OP_LONG);
541 write_exp_elt_type ($1.type);
542 write_exp_elt_longcst ((LONGEST)($1.val));
543 write_exp_elt_opcode (OP_LONG); }
548 struct stoken_vector vec;
551 write_exp_string_vector ($1.type, &vec);
557 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
558 write_exp_elt_opcode (OP_LONG);
559 write_exp_elt_type (val.typed_val_int.type);
560 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
561 write_exp_elt_opcode (OP_LONG);
567 { write_exp_elt_opcode (OP_DOUBLE);
568 write_exp_elt_type ($1.type);
569 write_exp_elt_dblcst ($1.dval);
570 write_exp_elt_opcode (OP_DOUBLE); }
574 { write_exp_elt_opcode (OP_DECFLOAT);
575 write_exp_elt_type ($1.type);
576 write_exp_elt_decfloatcst ($1.val);
577 write_exp_elt_opcode (OP_DECFLOAT); }
585 write_dollar_variable ($1);
589 exp : SIZEOF '(' type ')' %prec UNARY
590 { write_exp_elt_opcode (OP_LONG);
591 write_exp_elt_type (parse_type->builtin_int);
593 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
594 write_exp_elt_opcode (OP_LONG); }
597 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
598 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
599 write_exp_elt_type ($3);
600 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
603 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
604 { write_exp_elt_opcode (UNOP_CAST);
605 write_exp_elt_type ($3);
606 write_exp_elt_opcode (UNOP_CAST); }
609 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
610 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
611 write_exp_elt_type ($3);
612 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
615 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
616 { /* We could do more error checking here, but
617 it doesn't seem worthwhile. */
618 write_exp_elt_opcode (UNOP_CAST);
619 write_exp_elt_type ($3);
620 write_exp_elt_opcode (UNOP_CAST); }
626 /* We copy the string here, and not in the
627 lexer, to guarantee that we do not leak a
628 string. Note that we follow the
629 NUL-termination convention of the
631 struct typed_stoken *vec = XNEW (struct typed_stoken);
636 vec->length = $1.length;
637 vec->ptr = malloc ($1.length + 1);
638 memcpy (vec->ptr, $1.ptr, $1.length + 1);
643 /* Note that we NUL-terminate here, but just
647 $$.tokens = realloc ($$.tokens,
648 $$.len * sizeof (struct typed_stoken));
650 p = malloc ($2.length + 1);
651 memcpy (p, $2.ptr, $2.length + 1);
653 $$.tokens[$$.len - 1].type = $2.type;
654 $$.tokens[$$.len - 1].length = $2.length;
655 $$.tokens[$$.len - 1].ptr = p;
662 enum c_string_type type = C_STRING;
664 for (i = 0; i < $1.len; ++i)
666 switch ($1.tokens[i].type)
674 && type != $1.tokens[i].type)
675 error ("Undefined string concatenation.");
676 type = $1.tokens[i].type;
680 internal_error (__FILE__, __LINE__,
681 "unrecognized type in string concatenation");
685 write_exp_string_vector (type, &$1);
686 for (i = 0; i < $1.len; ++i)
687 free ($1.tokens[i].ptr);
694 { write_exp_elt_opcode (OP_LONG);
695 write_exp_elt_type (parse_type->builtin_bool);
696 write_exp_elt_longcst ((LONGEST) 1);
697 write_exp_elt_opcode (OP_LONG); }
701 { write_exp_elt_opcode (OP_LONG);
702 write_exp_elt_type (parse_type->builtin_bool);
703 write_exp_elt_longcst ((LONGEST) 0);
704 write_exp_elt_opcode (OP_LONG); }
712 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
714 error ("No file or function \"%s\".",
715 copy_name ($1.stoken));
723 block : block COLONCOLON name
725 = lookup_symbol (copy_name ($3), $1,
726 VAR_DOMAIN, (int *) NULL);
727 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
728 error ("No function \"%s\" in specified context.",
730 $$ = SYMBOL_BLOCK_VALUE (tem); }
733 variable: block COLONCOLON name
734 { struct symbol *sym;
735 sym = lookup_symbol (copy_name ($3), $1,
736 VAR_DOMAIN, (int *) NULL);
738 error ("No symbol \"%s\" in specified context.",
741 write_exp_elt_opcode (OP_VAR_VALUE);
742 /* block_found is set by lookup_symbol. */
743 write_exp_elt_block (block_found);
744 write_exp_elt_sym (sym);
745 write_exp_elt_opcode (OP_VAR_VALUE); }
748 qualified_name: TYPENAME COLONCOLON name
750 struct type *type = $1.type;
751 CHECK_TYPEDEF (type);
752 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
753 && TYPE_CODE (type) != TYPE_CODE_UNION
754 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
755 error ("`%s' is not defined as an aggregate type.",
758 write_exp_elt_opcode (OP_SCOPE);
759 write_exp_elt_type (type);
760 write_exp_string ($3);
761 write_exp_elt_opcode (OP_SCOPE);
763 | TYPENAME COLONCOLON '~' name
765 struct type *type = $1.type;
766 struct stoken tmp_token;
767 CHECK_TYPEDEF (type);
768 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
769 && TYPE_CODE (type) != TYPE_CODE_UNION
770 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
771 error ("`%s' is not defined as an aggregate type.",
774 tmp_token.ptr = (char*) alloca ($4.length + 2);
775 tmp_token.length = $4.length + 1;
776 tmp_token.ptr[0] = '~';
777 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
778 tmp_token.ptr[tmp_token.length] = 0;
780 /* Check for valid destructor name. */
781 destructor_name_p (tmp_token.ptr, type);
782 write_exp_elt_opcode (OP_SCOPE);
783 write_exp_elt_type (type);
784 write_exp_string (tmp_token);
785 write_exp_elt_opcode (OP_SCOPE);
787 | TYPENAME COLONCOLON name COLONCOLON name
789 char *copy = copy_name ($3);
790 error (_("No type \"%s\" within class "
791 "or namespace \"%s\"."),
792 copy, TYPE_NAME ($1.type));
796 variable: qualified_name
797 | COLONCOLON name_not_typename
799 char *name = copy_name ($2.stoken);
801 struct minimal_symbol *msymbol;
804 lookup_symbol (name, (const struct block *) NULL,
805 VAR_DOMAIN, (int *) NULL);
808 write_exp_elt_opcode (OP_VAR_VALUE);
809 write_exp_elt_block (NULL);
810 write_exp_elt_sym (sym);
811 write_exp_elt_opcode (OP_VAR_VALUE);
815 msymbol = lookup_minimal_symbol (name, NULL, NULL);
817 write_exp_msymbol (msymbol);
818 else if (!have_full_symbols () && !have_partial_symbols ())
819 error ("No symbol table is loaded. Use the \"file\" command.");
821 error ("No symbol \"%s\" in current context.", name);
825 variable: name_not_typename
826 { struct symbol *sym = $1.sym;
830 if (symbol_read_needs_frame (sym))
832 if (innermost_block == 0
833 || contained_in (block_found,
835 innermost_block = block_found;
838 write_exp_elt_opcode (OP_VAR_VALUE);
839 /* We want to use the selected frame, not
840 another more inner frame which happens to
841 be in the same block. */
842 write_exp_elt_block (NULL);
843 write_exp_elt_sym (sym);
844 write_exp_elt_opcode (OP_VAR_VALUE);
846 else if ($1.is_a_field_of_this)
848 /* C++: it hangs off of `this'. Must
849 not inadvertently convert from a method call
851 if (innermost_block == 0
852 || contained_in (block_found,
854 innermost_block = block_found;
855 write_exp_elt_opcode (OP_THIS);
856 write_exp_elt_opcode (OP_THIS);
857 write_exp_elt_opcode (STRUCTOP_PTR);
858 write_exp_string ($1.stoken);
859 write_exp_elt_opcode (STRUCTOP_PTR);
863 struct minimal_symbol *msymbol;
864 char *arg = copy_name ($1.stoken);
867 lookup_minimal_symbol (arg, NULL, NULL);
869 write_exp_msymbol (msymbol);
870 else if (!have_full_symbols () && !have_partial_symbols ())
871 error ("No symbol table is loaded. Use the \"file\" command.");
873 error ("No symbol \"%s\" in current context.",
874 copy_name ($1.stoken));
879 space_identifier : '@' NAME
880 { push_type_address_space (copy_name ($2.stoken));
881 push_type (tp_space_identifier);
885 const_or_volatile: const_or_volatile_noopt
889 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
892 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
893 | const_or_volatile_noopt
896 const_or_volatile_or_space_identifier:
897 const_or_volatile_or_space_identifier_noopt
902 { push_type (tp_pointer); $$ = 0; }
904 { push_type (tp_pointer); $$ = $2; }
906 { push_type (tp_reference); $$ = 0; }
908 { push_type (tp_reference); $$ = $2; }
912 direct_abs_decl: '(' abs_decl ')'
914 | direct_abs_decl array_mod
917 push_type (tp_array);
922 push_type (tp_array);
926 | direct_abs_decl func_mod
927 { push_type (tp_function); }
929 { push_type (tp_function); }
940 | '(' nonempty_typelist ')'
941 { free ($2); $$ = 0; }
944 /* We used to try to recognize pointer to member types here, but
945 that didn't work (shift/reduce conflicts meant that these rules never
946 got executed). The problem is that
947 int (foo::bar::baz::bizzle)
948 is a function type but
949 int (foo::bar::baz::bizzle::*)
950 is a pointer to member type. Stroustrup loses again! */
955 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
959 { $$ = parse_type->builtin_int; }
961 { $$ = parse_type->builtin_long; }
963 { $$ = parse_type->builtin_short; }
965 { $$ = parse_type->builtin_long; }
966 | LONG SIGNED_KEYWORD INT_KEYWORD
967 { $$ = parse_type->builtin_long; }
968 | LONG SIGNED_KEYWORD
969 { $$ = parse_type->builtin_long; }
970 | SIGNED_KEYWORD LONG INT_KEYWORD
971 { $$ = parse_type->builtin_long; }
972 | UNSIGNED LONG INT_KEYWORD
973 { $$ = parse_type->builtin_unsigned_long; }
974 | LONG UNSIGNED INT_KEYWORD
975 { $$ = parse_type->builtin_unsigned_long; }
977 { $$ = parse_type->builtin_unsigned_long; }
979 { $$ = parse_type->builtin_long_long; }
980 | LONG LONG INT_KEYWORD
981 { $$ = parse_type->builtin_long_long; }
982 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
983 { $$ = parse_type->builtin_long_long; }
984 | LONG LONG SIGNED_KEYWORD
985 { $$ = parse_type->builtin_long_long; }
986 | SIGNED_KEYWORD LONG LONG
987 { $$ = parse_type->builtin_long_long; }
988 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
989 { $$ = parse_type->builtin_long_long; }
991 { $$ = parse_type->builtin_unsigned_long_long; }
992 | UNSIGNED LONG LONG INT_KEYWORD
993 { $$ = parse_type->builtin_unsigned_long_long; }
995 { $$ = parse_type->builtin_unsigned_long_long; }
996 | LONG LONG UNSIGNED INT_KEYWORD
997 { $$ = parse_type->builtin_unsigned_long_long; }
999 { $$ = parse_type->builtin_short; }
1000 | SHORT SIGNED_KEYWORD INT_KEYWORD
1001 { $$ = parse_type->builtin_short; }
1002 | SHORT SIGNED_KEYWORD
1003 { $$ = parse_type->builtin_short; }
1004 | UNSIGNED SHORT INT_KEYWORD
1005 { $$ = parse_type->builtin_unsigned_short; }
1007 { $$ = parse_type->builtin_unsigned_short; }
1008 | SHORT UNSIGNED INT_KEYWORD
1009 { $$ = parse_type->builtin_unsigned_short; }
1011 { $$ = parse_type->builtin_double; }
1012 | LONG DOUBLE_KEYWORD
1013 { $$ = parse_type->builtin_long_double; }
1015 { $$ = lookup_struct (copy_name ($2),
1016 expression_context_block); }
1018 { $$ = lookup_struct (copy_name ($2),
1019 expression_context_block); }
1021 { $$ = lookup_union (copy_name ($2),
1022 expression_context_block); }
1024 { $$ = lookup_enum (copy_name ($2),
1025 expression_context_block); }
1027 { $$ = lookup_unsigned_typename (parse_language,
1029 TYPE_NAME($2.type)); }
1031 { $$ = parse_type->builtin_unsigned_int; }
1032 | SIGNED_KEYWORD typename
1033 { $$ = lookup_signed_typename (parse_language,
1035 TYPE_NAME($2.type)); }
1037 { $$ = parse_type->builtin_int; }
1038 /* It appears that this rule for templates is never
1039 reduced; template recognition happens by lookahead
1040 in the token processing code in yylex. */
1041 | TEMPLATE name '<' type '>'
1042 { $$ = lookup_template_type(copy_name($2), $4,
1043 expression_context_block);
1045 | const_or_volatile_or_space_identifier_noopt typebase
1046 { $$ = follow_types ($2); }
1047 | typebase const_or_volatile_or_space_identifier_noopt
1048 { $$ = follow_types ($1); }
1054 $$.stoken.ptr = "int";
1055 $$.stoken.length = 3;
1056 $$.type = parse_type->builtin_int;
1060 $$.stoken.ptr = "long";
1061 $$.stoken.length = 4;
1062 $$.type = parse_type->builtin_long;
1066 $$.stoken.ptr = "short";
1067 $$.stoken.length = 5;
1068 $$.type = parse_type->builtin_short;
1074 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1075 $<ivec>$[0] = 1; /* Number of types in vector */
1078 | nonempty_typelist ',' type
1079 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1080 $$ = (struct type **) realloc ((char *) $1, len);
1081 $$[$<ivec>$[0]] = $3;
1086 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1087 { $$ = follow_types ($1); }
1090 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1091 | VOLATILE_KEYWORD CONST_KEYWORD
1094 const_or_volatile_noopt: const_and_volatile
1095 { push_type (tp_const);
1096 push_type (tp_volatile);
1099 { push_type (tp_const); }
1101 { push_type (tp_volatile); }
1104 operator: OPERATOR NEW
1105 { $$ = operator_stoken (" new"); }
1107 { $$ = operator_stoken (" delete"); }
1108 | OPERATOR NEW '[' ']'
1109 { $$ = operator_stoken (" new[]"); }
1110 | OPERATOR DELETE '[' ']'
1111 { $$ = operator_stoken (" delete[]"); }
1113 { $$ = operator_stoken ("+"); }
1115 { $$ = operator_stoken ("-"); }
1117 { $$ = operator_stoken ("*"); }
1119 { $$ = operator_stoken ("/"); }
1121 { $$ = operator_stoken ("%"); }
1123 { $$ = operator_stoken ("^"); }
1125 { $$ = operator_stoken ("&"); }
1127 { $$ = operator_stoken ("|"); }
1129 { $$ = operator_stoken ("~"); }
1131 { $$ = operator_stoken ("!"); }
1133 { $$ = operator_stoken ("="); }
1135 { $$ = operator_stoken ("<"); }
1137 { $$ = operator_stoken (">"); }
1138 | OPERATOR ASSIGN_MODIFY
1139 { const char *op = "unknown";
1163 case BINOP_BITWISE_IOR:
1166 case BINOP_BITWISE_AND:
1169 case BINOP_BITWISE_XOR:
1176 $$ = operator_stoken (op);
1179 { $$ = operator_stoken ("<<"); }
1181 { $$ = operator_stoken (">>"); }
1183 { $$ = operator_stoken ("=="); }
1185 { $$ = operator_stoken ("!="); }
1187 { $$ = operator_stoken ("<="); }
1189 { $$ = operator_stoken (">="); }
1191 { $$ = operator_stoken ("&&"); }
1193 { $$ = operator_stoken ("||"); }
1194 | OPERATOR INCREMENT
1195 { $$ = operator_stoken ("++"); }
1196 | OPERATOR DECREMENT
1197 { $$ = operator_stoken ("--"); }
1199 { $$ = operator_stoken (","); }
1200 | OPERATOR ARROW_STAR
1201 { $$ = operator_stoken ("->*"); }
1203 { $$ = operator_stoken ("->"); }
1205 { $$ = operator_stoken ("()"); }
1207 { $$ = operator_stoken ("[]"); }
1211 struct ui_file *buf = mem_fileopen ();
1213 c_print_type ($2, NULL, buf, -1, 0);
1214 name = ui_file_xstrdup (buf, &length);
1215 ui_file_delete (buf);
1216 $$ = operator_stoken (name);
1223 name : NAME { $$ = $1.stoken; }
1224 | BLOCKNAME { $$ = $1.stoken; }
1225 | TYPENAME { $$ = $1.stoken; }
1226 | NAME_OR_INT { $$ = $1.stoken; }
1227 | operator { $$ = $1; }
1230 name_not_typename : NAME
1232 /* These would be useful if name_not_typename was useful, but it is just
1233 a fake for "variable", so these cause reduce/reduce conflicts because
1234 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1235 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1236 context where only a name could occur, this might be useful.
1243 /* Returns a stoken of the operator name given by OP (which does not
1244 include the string "operator"). */
1245 static struct stoken
1246 operator_stoken (const char *op)
1248 static const char *operator_string = "operator";
1249 struct stoken st = { NULL, 0 };
1250 st.length = strlen (operator_string) + strlen (op);
1251 st.ptr = malloc (st.length + 1);
1252 strcpy (st.ptr, operator_string);
1253 strcat (st.ptr, op);
1255 /* The toplevel (c_parse) will free the memory allocated here. */
1256 make_cleanup (free, st.ptr);
1260 /* Take care of parsing a number (anything that starts with a digit).
1261 Set yylval and return the token type; update lexptr.
1262 LEN is the number of characters in it. */
1264 /*** Needs some error checking for the float case ***/
1267 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1269 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1270 here, and we do kind of silly things like cast to unsigned. */
1277 int base = input_radix;
1280 /* Number of "L" suffixes encountered. */
1283 /* We have found a "L" or "U" suffix. */
1284 int found_suffix = 0;
1287 struct type *signed_type;
1288 struct type *unsigned_type;
1292 /* It's a float since it contains a point or an exponent. */
1294 int num; /* number of tokens scanned by scanf */
1297 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1298 point. Return DECFLOAT. */
1300 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1303 putithere->typed_val_decfloat.type
1304 = parse_type->builtin_decfloat;
1305 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1306 gdbarch_byte_order (parse_gdbarch), p);
1311 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1314 putithere->typed_val_decfloat.type
1315 = parse_type->builtin_decdouble;
1316 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1317 gdbarch_byte_order (parse_gdbarch), p);
1322 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1325 putithere->typed_val_decfloat.type
1326 = parse_type->builtin_declong;
1327 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1328 gdbarch_byte_order (parse_gdbarch), p);
1334 saved_char = p[len];
1335 p[len] = 0; /* null-terminate the token */
1336 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
1337 &putithere->typed_val_float.dval, s);
1338 p[len] = saved_char; /* restore the input stream */
1341 putithere->typed_val_float.type =
1342 parse_type->builtin_double;
1346 /* See if it has any float suffix: 'f' for float, 'l' for long
1348 if (!strcasecmp (s, "f"))
1349 putithere->typed_val_float.type =
1350 parse_type->builtin_float;
1351 else if (!strcasecmp (s, "l"))
1352 putithere->typed_val_float.type =
1353 parse_type->builtin_long_double;
1365 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1409 if (c >= 'A' && c <= 'Z')
1411 if (c != 'l' && c != 'u')
1413 if (c >= '0' && c <= '9')
1421 if (base > 10 && c >= 'a' && c <= 'f')
1425 n += i = c - 'a' + 10;
1438 return ERROR; /* Char not a digit */
1441 return ERROR; /* Invalid digit in this base */
1443 /* Portably test for overflow (only works for nonzero values, so make
1444 a second check for zero). FIXME: Can't we just make n and prevn
1445 unsigned and avoid this? */
1446 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1447 unsigned_p = 1; /* Try something unsigned */
1449 /* Portably test for unsigned overflow.
1450 FIXME: This check is wrong; for example it doesn't find overflow
1451 on 0x123456789 when LONGEST is 32 bits. */
1452 if (c != 'l' && c != 'u' && n != 0)
1454 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1455 error ("Numeric constant too large.");
1460 /* An integer constant is an int, a long, or a long long. An L
1461 suffix forces it to be long; an LL suffix forces it to be long
1462 long. If not forced to a larger size, it gets the first type of
1463 the above that it fits in. To figure out whether it fits, we
1464 shift it right and see whether anything remains. Note that we
1465 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1466 operation, because many compilers will warn about such a shift
1467 (which always produces a zero result). Sometimes gdbarch_int_bit
1468 or gdbarch_long_bit will be that big, sometimes not. To deal with
1469 the case where it is we just always shift the value more than
1470 once, with fewer bits each time. */
1472 un = (ULONGEST)n >> 2;
1474 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1476 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1478 /* A large decimal (not hex or octal) constant (between INT_MAX
1479 and UINT_MAX) is a long or unsigned long, according to ANSI,
1480 never an unsigned int, but this code treats it as unsigned
1481 int. This probably should be fixed. GCC gives a warning on
1484 unsigned_type = parse_type->builtin_unsigned_int;
1485 signed_type = parse_type->builtin_int;
1487 else if (long_p <= 1
1488 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1490 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1491 unsigned_type = parse_type->builtin_unsigned_long;
1492 signed_type = parse_type->builtin_long;
1497 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1498 < gdbarch_long_long_bit (parse_gdbarch))
1499 /* A long long does not fit in a LONGEST. */
1500 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1502 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1503 high_bit = (ULONGEST) 1 << shift;
1504 unsigned_type = parse_type->builtin_unsigned_long_long;
1505 signed_type = parse_type->builtin_long_long;
1508 putithere->typed_val_int.val = n;
1510 /* If the high bit of the worked out type is set then this number
1511 has to be unsigned. */
1513 if (unsigned_p || (n & high_bit))
1515 putithere->typed_val_int.type = unsigned_type;
1519 putithere->typed_val_int.type = signed_type;
1525 /* Temporary obstack used for holding strings. */
1526 static struct obstack tempbuf;
1527 static int tempbuf_init;
1529 /* Parse a C escape sequence. The initial backslash of the sequence
1530 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1531 last character of the sequence. If OUTPUT is not NULL, the
1532 translated form of the escape sequence will be written there. If
1533 OUTPUT is NULL, no output is written and the call will only affect
1534 *PTR. If an escape sequence is expressed in target bytes, then the
1535 entire sequence will simply be copied to OUTPUT. Return 1 if any
1536 character was emitted, 0 otherwise. */
1539 c_parse_escape (char **ptr, struct obstack *output)
1541 char *tokptr = *ptr;
1544 /* Some escape sequences undergo character set conversion. Those we
1548 /* Hex escapes do not undergo character set conversion, so keep
1549 the escape sequence for later. */
1552 obstack_grow_str (output, "\\x");
1554 if (!isxdigit (*tokptr))
1555 error (_("\\x escape without a following hex digit"));
1556 while (isxdigit (*tokptr))
1559 obstack_1grow (output, *tokptr);
1564 /* Octal escapes do not undergo character set conversion, so
1565 keep the escape sequence for later. */
1577 obstack_grow_str (output, "\\");
1579 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1583 obstack_1grow (output, *tokptr);
1589 /* We handle UCNs later. We could handle them here, but that
1590 would mean a spurious error in the case where the UCN could
1591 be converted to the target charset but not the host
1597 int i, len = c == 'U' ? 8 : 4;
1600 obstack_1grow (output, '\\');
1601 obstack_1grow (output, *tokptr);
1604 if (!isxdigit (*tokptr))
1605 error (_("\\%c escape without a following hex digit"), c);
1606 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1609 obstack_1grow (output, *tokptr);
1615 /* We must pass backslash through so that it does not
1616 cause quoting during the second expansion. */
1619 obstack_grow_str (output, "\\\\");
1623 /* Escapes which undergo conversion. */
1626 obstack_1grow (output, '\a');
1631 obstack_1grow (output, '\b');
1636 obstack_1grow (output, '\f');
1641 obstack_1grow (output, '\n');
1646 obstack_1grow (output, '\r');
1651 obstack_1grow (output, '\t');
1656 obstack_1grow (output, '\v');
1660 /* GCC extension. */
1663 obstack_1grow (output, HOST_ESCAPE_CHAR);
1667 /* Backslash-newline expands to nothing at all. */
1673 /* A few escapes just expand to the character itself. */
1677 /* GCC extensions. */
1682 /* Unrecognized escapes turn into the character itself. */
1685 obstack_1grow (output, *tokptr);
1693 /* Parse a string or character literal from TOKPTR. The string or
1694 character may be wide or unicode. *OUTPTR is set to just after the
1695 end of the literal in the input string. The resulting token is
1696 stored in VALUE. This returns a token value, either STRING or
1697 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1698 number of host characters in the literal. */
1700 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1704 enum c_string_type type;
1706 /* Build the gdb internal form of the input string in tempbuf. Note
1707 that the buffer is null byte terminated *only* for the
1708 convenience of debugging gdb itself and printing the buffer
1709 contents when the buffer contains no embedded nulls. Gdb does
1710 not depend upon the buffer being null byte terminated, it uses
1711 the length string instead. This allows gdb to handle C strings
1712 (as well as strings in other languages) with embedded null
1718 obstack_free (&tempbuf, NULL);
1719 obstack_init (&tempbuf);
1721 /* Record the string type. */
1724 type = C_WIDE_STRING;
1727 else if (*tokptr == 'u')
1732 else if (*tokptr == 'U')
1740 /* Skip the quote. */
1754 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1756 else if (c == quote)
1760 obstack_1grow (&tempbuf, c);
1762 /* FIXME: this does the wrong thing with multi-byte host
1763 characters. We could use mbrlen here, but that would
1764 make "set host-charset" a bit less useful. */
1769 if (*tokptr != quote)
1772 error ("Unterminated string in expression.");
1774 error ("Unmatched single quote.");
1779 value->ptr = obstack_base (&tempbuf);
1780 value->length = obstack_object_size (&tempbuf);
1784 return quote == '"' ? STRING : CHAR;
1791 enum exp_opcode opcode;
1795 static const struct token tokentab3[] =
1797 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1798 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1799 {"->*", ARROW_STAR, BINOP_END, 1}
1802 static const struct token tokentab2[] =
1804 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1805 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1806 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1807 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1808 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1809 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1810 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1811 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1812 {"++", INCREMENT, BINOP_END, 0},
1813 {"--", DECREMENT, BINOP_END, 0},
1814 {"->", ARROW, BINOP_END, 0},
1815 {"&&", ANDAND, BINOP_END, 0},
1816 {"||", OROR, BINOP_END, 0},
1817 /* "::" is *not* only C++: gdb overrides its meaning in several
1818 different ways, e.g., 'filename'::func, function::variable. */
1819 {"::", COLONCOLON, BINOP_END, 0},
1820 {"<<", LSH, BINOP_END, 0},
1821 {">>", RSH, BINOP_END, 0},
1822 {"==", EQUAL, BINOP_END, 0},
1823 {"!=", NOTEQUAL, BINOP_END, 0},
1824 {"<=", LEQ, BINOP_END, 0},
1825 {">=", GEQ, BINOP_END, 0},
1826 {".*", DOT_STAR, BINOP_END, 1}
1829 /* Identifier-like tokens. */
1830 static const struct token ident_tokens[] =
1832 {"unsigned", UNSIGNED, OP_NULL, 0},
1833 {"template", TEMPLATE, OP_NULL, 1},
1834 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1835 {"struct", STRUCT, OP_NULL, 0},
1836 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1837 {"sizeof", SIZEOF, OP_NULL, 0},
1838 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1839 {"false", FALSEKEYWORD, OP_NULL, 1},
1840 {"class", CLASS, OP_NULL, 1},
1841 {"union", UNION, OP_NULL, 0},
1842 {"short", SHORT, OP_NULL, 0},
1843 {"const", CONST_KEYWORD, OP_NULL, 0},
1844 {"enum", ENUM, OP_NULL, 0},
1845 {"long", LONG, OP_NULL, 0},
1846 {"true", TRUEKEYWORD, OP_NULL, 1},
1847 {"int", INT_KEYWORD, OP_NULL, 0},
1848 {"new", NEW, OP_NULL, 1},
1849 {"delete", DELETE, OP_NULL, 1},
1850 {"operator", OPERATOR, OP_NULL, 1},
1852 {"and", ANDAND, BINOP_END, 1},
1853 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1854 {"bitand", '&', OP_NULL, 1},
1855 {"bitor", '|', OP_NULL, 1},
1856 {"compl", '~', OP_NULL, 1},
1857 {"not", '!', OP_NULL, 1},
1858 {"not_eq", NOTEQUAL, BINOP_END, 1},
1859 {"or", OROR, BINOP_END, 1},
1860 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1861 {"xor", '^', OP_NULL, 1},
1862 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1864 {"const_cast", CONST_CAST, OP_NULL, 1 },
1865 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1866 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1867 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1870 /* When we find that lexptr (the global var defined in parse.c) is
1871 pointing at a macro invocation, we expand the invocation, and call
1872 scan_macro_expansion to save the old lexptr here and point lexptr
1873 into the expanded text. When we reach the end of that, we call
1874 end_macro_expansion to pop back to the value we saved here. The
1875 macro expansion code promises to return only fully-expanded text,
1876 so we don't need to "push" more than one level.
1878 This is disgusting, of course. It would be cleaner to do all macro
1879 expansion beforehand, and then hand that to lexptr. But we don't
1880 really know where the expression ends. Remember, in a command like
1882 (gdb) break *ADDRESS if CONDITION
1884 we evaluate ADDRESS in the scope of the current frame, but we
1885 evaluate CONDITION in the scope of the breakpoint's location. So
1886 it's simply wrong to try to macro-expand the whole thing at once. */
1887 static char *macro_original_text;
1889 /* We save all intermediate macro expansions on this obstack for the
1890 duration of a single parse. The expansion text may sometimes have
1891 to live past the end of the expansion, due to yacc lookahead.
1892 Rather than try to be clever about saving the data for a single
1893 token, we simply keep it all and delete it after parsing has
1895 static struct obstack expansion_obstack;
1898 scan_macro_expansion (char *expansion)
1902 /* We'd better not be trying to push the stack twice. */
1903 gdb_assert (! macro_original_text);
1905 /* Copy to the obstack, and then free the intermediate
1907 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
1910 /* Save the old lexptr value, so we can return to it when we're done
1911 parsing the expanded text. */
1912 macro_original_text = lexptr;
1918 scanning_macro_expansion (void)
1920 return macro_original_text != 0;
1925 finished_macro_expansion (void)
1927 /* There'd better be something to pop back to. */
1928 gdb_assert (macro_original_text);
1930 /* Pop back to the original text. */
1931 lexptr = macro_original_text;
1932 macro_original_text = 0;
1937 scan_macro_cleanup (void *dummy)
1939 if (macro_original_text)
1940 finished_macro_expansion ();
1942 obstack_free (&expansion_obstack, NULL);
1945 /* Return true iff the token represents a C++ cast operator. */
1948 is_cast_operator (const char *token, int len)
1950 return (! strncmp (token, "dynamic_cast", len)
1951 || ! strncmp (token, "static_cast", len)
1952 || ! strncmp (token, "reinterpret_cast", len)
1953 || ! strncmp (token, "const_cast", len));
1956 /* The scope used for macro expansion. */
1957 static struct macro_scope *expression_macro_scope;
1959 /* This is set if a NAME token appeared at the very end of the input
1960 string, with no whitespace separating the name from the EOF. This
1961 is used only when parsing to do field name completion. */
1962 static int saw_name_at_eof;
1964 /* This is set if the previously-returned token was a structure
1965 operator -- either '.' or ARROW. This is used only when parsing to
1966 do field name completion. */
1967 static int last_was_structop;
1969 /* Read one token, getting characters through lexptr. */
1972 lex_one_token (void)
1978 int saw_structop = last_was_structop;
1981 last_was_structop = 0;
1985 /* Check if this is a macro invocation that we need to expand. */
1986 if (! scanning_macro_expansion ())
1988 char *expanded = macro_expand_next (&lexptr,
1989 standard_macro_lookup,
1990 expression_macro_scope);
1993 scan_macro_expansion (expanded);
1996 prev_lexptr = lexptr;
1999 /* See if it is a special token of length 3. */
2000 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2001 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2003 if (tokentab3[i].cxx_only
2004 && parse_language->la_language != language_cplus)
2008 yylval.opcode = tokentab3[i].opcode;
2009 return tokentab3[i].token;
2012 /* See if it is a special token of length 2. */
2013 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2014 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2016 if (tokentab2[i].cxx_only
2017 && parse_language->la_language != language_cplus)
2021 yylval.opcode = tokentab2[i].opcode;
2022 if (in_parse_field && tokentab2[i].token == ARROW)
2023 last_was_structop = 1;
2024 return tokentab2[i].token;
2027 switch (c = *tokstart)
2030 /* If we were just scanning the result of a macro expansion,
2031 then we need to resume scanning the original text.
2032 If we're parsing for field name completion, and the previous
2033 token allows such completion, return a COMPLETE token.
2034 Otherwise, we were already scanning the original text, and
2035 we're really done. */
2036 if (scanning_macro_expansion ())
2038 finished_macro_expansion ();
2041 else if (saw_name_at_eof)
2043 saw_name_at_eof = 0;
2046 else if (saw_structop)
2065 if (paren_depth == 0)
2072 if (comma_terminates
2074 && ! scanning_macro_expansion ())
2080 /* Might be a floating point number. */
2081 if (lexptr[1] < '0' || lexptr[1] > '9')
2084 last_was_structop = 1;
2085 goto symbol; /* Nope, must be a symbol. */
2087 /* FALL THRU into number case. */
2100 /* It's a number. */
2101 int got_dot = 0, got_e = 0, toktype;
2103 int hex = input_radix > 10;
2105 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2110 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2118 /* This test includes !hex because 'e' is a valid hex digit
2119 and thus does not indicate a floating point number when
2120 the radix is hex. */
2121 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2122 got_dot = got_e = 1;
2123 /* This test does not include !hex, because a '.' always indicates
2124 a decimal floating point number regardless of the radix. */
2125 else if (!got_dot && *p == '.')
2127 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2128 && (*p == '-' || *p == '+'))
2129 /* This is the sign of the exponent, not the end of the
2132 /* We will take any letters or digits. parse_number will
2133 complain if past the radix, or if L or U are not final. */
2134 else if ((*p < '0' || *p > '9')
2135 && ((*p < 'a' || *p > 'z')
2136 && (*p < 'A' || *p > 'Z')))
2139 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2140 if (toktype == ERROR)
2142 char *err_copy = (char *) alloca (p - tokstart + 1);
2144 memcpy (err_copy, tokstart, p - tokstart);
2145 err_copy[p - tokstart] = 0;
2146 error ("Invalid number \"%s\".", err_copy);
2177 if (tokstart[1] != '"' && tokstart[1] != '\'')
2184 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2189 error ("Empty character constant.");
2190 else if (host_len > 2 && c == '\'')
2193 namelen = lexptr - tokstart - 1;
2196 else if (host_len > 1)
2197 error ("Invalid character constant.");
2203 if (!(c == '_' || c == '$'
2204 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2205 /* We must have come across a bad character (e.g. ';'). */
2206 error ("Invalid character '%c' in expression.", c);
2208 /* It's a name. See how long it is. */
2210 for (c = tokstart[namelen];
2211 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2212 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2214 /* Template parameter lists are part of the name.
2215 FIXME: This mishandles `print $a<4&&$a>3'. */
2219 if (! is_cast_operator (tokstart, namelen))
2221 /* Scan ahead to get rest of the template specification. Note
2222 that we look ahead only when the '<' adjoins non-whitespace
2223 characters; for comparison expressions, e.g. "a < b > c",
2224 there must be spaces before the '<', etc. */
2226 char * p = find_template_name_end (tokstart + namelen);
2228 namelen = p - tokstart;
2232 c = tokstart[++namelen];
2235 /* The token "if" terminates the expression and is NOT removed from
2236 the input stream. It doesn't count if it appears in the
2237 expansion of a macro. */
2239 && tokstart[0] == 'i'
2240 && tokstart[1] == 'f'
2241 && ! scanning_macro_expansion ())
2246 /* For the same reason (breakpoint conditions), "thread N"
2247 terminates the expression. "thread" could be an identifier, but
2248 an identifier is never followed by a number without intervening
2249 punctuation. "task" is similar. Handle abbreviations of these,
2250 similarly to breakpoint.c:find_condition_and_thread. */
2252 && (strncmp (tokstart, "thread", namelen) == 0
2253 || strncmp (tokstart, "task", namelen) == 0)
2254 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2255 && ! scanning_macro_expansion ())
2257 char *p = tokstart + namelen + 1;
2258 while (*p == ' ' || *p == '\t')
2260 if (*p >= '0' && *p <= '9')
2268 yylval.sval.ptr = tokstart;
2269 yylval.sval.length = namelen;
2271 /* Catch specific keywords. */
2272 copy = copy_name (yylval.sval);
2273 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2274 if (strcmp (copy, ident_tokens[i].operator) == 0)
2276 if (ident_tokens[i].cxx_only
2277 && parse_language->la_language != language_cplus)
2280 /* It is ok to always set this, even though we don't always
2281 strictly need to. */
2282 yylval.opcode = ident_tokens[i].opcode;
2283 return ident_tokens[i].token;
2286 if (*tokstart == '$')
2289 if (in_parse_field && *lexptr == '\0')
2290 saw_name_at_eof = 1;
2294 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2298 union YYSTYPE value;
2301 DEF_VEC_O (token_and_value);
2303 /* A FIFO of tokens that have been read but not yet returned to the
2305 static VEC (token_and_value) *token_fifo;
2307 /* Non-zero if the lexer should return tokens from the FIFO. */
2310 /* Temporary storage for c_lex; this holds symbol names as they are
2312 static struct obstack name_obstack;
2314 /* Classify a NAME token. The contents of the token are in `yylval'.
2315 Updates yylval and returns the new token type. BLOCK is the block
2316 in which lookups start; this can be NULL to mean the global
2319 classify_name (struct block *block)
2323 int is_a_field_of_this = 0;
2325 copy = copy_name (yylval.sval);
2327 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2328 parse_language->la_language == language_cplus
2329 ? &is_a_field_of_this : (int *) NULL);
2331 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2333 yylval.ssym.sym = sym;
2334 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2339 /* See if it's a file name. */
2340 struct symtab *symtab;
2342 symtab = lookup_symtab (copy);
2345 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2350 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2352 yylval.tsym.type = SYMBOL_TYPE (sym);
2357 = language_lookup_primitive_type_by_name (parse_language,
2358 parse_gdbarch, copy);
2359 if (yylval.tsym.type != NULL)
2362 /* Input names that aren't symbols but ARE valid hex numbers, when
2363 the input radix permits them, can be names or numbers depending
2364 on the parse. Note we support radixes > 16 here. */
2366 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2367 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2369 YYSTYPE newlval; /* Its value is ignored. */
2370 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2373 yylval.ssym.sym = sym;
2374 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2379 /* Any other kind of symbol */
2380 yylval.ssym.sym = sym;
2381 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2385 /* Like classify_name, but used by the inner loop of the lexer, when a
2386 name might have already been seen. FIRST_NAME is true if the token
2387 in `yylval' is the first component of a name, false otherwise. If
2388 this function returns NAME, it might not have updated `yylval'.
2389 This is ok because the caller only cares about TYPENAME. */
2391 classify_inner_name (struct block *block, int first_name)
2393 struct type *type, *new_type;
2397 return classify_name (block);
2399 type = check_typedef (yylval.tsym.type);
2400 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2401 && TYPE_CODE (type) != TYPE_CODE_UNION
2402 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2403 /* We know the caller won't expect us to update yylval. */
2406 copy = copy_name (yylval.tsym.stoken);
2407 new_type = cp_lookup_nested_type (type, copy, block);
2409 if (new_type == NULL)
2410 /* We know the caller won't expect us to update yylval. */
2413 yylval.tsym.type = new_type;
2417 /* The outer level of a two-level lexer. This calls the inner lexer
2418 to return tokens. It then either returns these tokens, or
2419 aggregates them into a larger token. This lets us work around a
2420 problem in our parsing approach, where the parser could not
2421 distinguish between qualified names and qualified types at the
2424 This approach is still not ideal, because it mishandles template
2425 types. See the comment in lex_one_token for an example. However,
2426 this is still an improvement over the earlier approach, and will
2427 suffice until we move to better parsing technology. */
2431 token_and_value current;
2433 int first_was_coloncolon, last_was_coloncolon, first_iter;
2435 if (popping && !VEC_empty (token_and_value, token_fifo))
2437 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2438 VEC_ordered_remove (token_and_value, token_fifo, 0);
2444 current.token = lex_one_token ();
2445 if (current.token == NAME)
2446 current.token = classify_name (expression_context_block);
2447 if (parse_language->la_language != language_cplus
2448 || (current.token != TYPENAME && current.token != COLONCOLON))
2449 return current.token;
2451 first_was_coloncolon = current.token == COLONCOLON;
2452 last_was_coloncolon = first_was_coloncolon;
2453 obstack_free (&name_obstack, obstack_base (&name_obstack));
2454 if (!last_was_coloncolon)
2455 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2456 current.value = yylval;
2460 token_and_value next;
2462 next.token = lex_one_token ();
2463 next.value = yylval;
2465 if (next.token == NAME && last_was_coloncolon)
2469 classification = classify_inner_name (first_was_coloncolon
2471 : expression_context_block,
2473 /* We keep going until we either run out of names, or until
2474 we have a qualified name which is not a type. */
2475 if (classification != TYPENAME)
2477 /* Push the final component and leave the loop. */
2478 VEC_safe_push (token_and_value, token_fifo, &next);
2482 /* Update the partial name we are constructing. */
2485 /* We don't want to put a leading "::" into the name. */
2486 obstack_grow_str (&name_obstack, "::");
2488 obstack_grow (&name_obstack, next.value.sval.ptr,
2489 next.value.sval.length);
2491 yylval.sval.ptr = obstack_base (&name_obstack);
2492 yylval.sval.length = obstack_object_size (&name_obstack);
2493 current.value = yylval;
2494 current.token = classification;
2496 last_was_coloncolon = 0;
2498 else if (next.token == COLONCOLON && !last_was_coloncolon)
2499 last_was_coloncolon = 1;
2502 /* We've reached the end of the name. */
2503 VEC_safe_push (token_and_value, token_fifo, &next);
2512 /* If we ended with a "::", insert it too. */
2513 if (last_was_coloncolon)
2516 memset (&cc, 0, sizeof (token_and_value));
2517 if (first_was_coloncolon)
2522 cc.token = COLONCOLON;
2523 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2526 yylval = current.value;
2527 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2529 yylval.sval.length);
2530 return current.token;
2537 struct cleanup *back_to = make_cleanup (free_current_contents,
2538 &expression_macro_scope);
2540 /* Set up the scope for macro expansion. */
2541 expression_macro_scope = NULL;
2543 if (expression_context_block)
2544 expression_macro_scope
2545 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2547 expression_macro_scope = default_macro_scope ();
2548 if (! expression_macro_scope)
2549 expression_macro_scope = user_macro_scope ();
2551 /* Initialize macro expansion code. */
2552 obstack_init (&expansion_obstack);
2553 gdb_assert (! macro_original_text);
2554 make_cleanup (scan_macro_cleanup, 0);
2556 make_cleanup_restore_integer (&yydebug);
2557 yydebug = parser_debug;
2559 /* Initialize some state used by the lexer. */
2560 last_was_structop = 0;
2561 saw_name_at_eof = 0;
2563 VEC_free (token_and_value, token_fifo);
2565 obstack_init (&name_obstack);
2566 make_cleanup_obstack_free (&name_obstack);
2568 result = yyparse ();
2569 do_cleanups (back_to);
2578 lexptr = prev_lexptr;
2580 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);