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 <ssym> UNKNOWN_CPP_NAME
190 %token <voidval> COMPLETE
191 %token <tsym> TYPENAME
193 %type <svec> string_exp
194 %type <ssym> name_not_typename
195 %type <tsym> typename
197 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
198 but which would parse as a valid number in the current input radix.
199 E.g. "c" when input_radix==16. Depending on the parse, it will be
200 turned into a name or into a number. */
202 %token <ssym> NAME_OR_INT
205 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
209 %type <sval> operator
210 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
212 /* Special type cases, put in to allow the parser to distinguish different
214 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
216 %token <sval> VARIABLE
218 %token <opcode> ASSIGN_MODIFY
227 %right '=' ASSIGN_MODIFY
235 %left '<' '>' LEQ GEQ
240 %right UNARY INCREMENT DECREMENT
241 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
242 %token <ssym> BLOCKNAME
243 %token <bval> FILENAME
255 { write_exp_elt_opcode(OP_TYPE);
256 write_exp_elt_type($1);
257 write_exp_elt_opcode(OP_TYPE);}
260 /* Expressions, including the comma operator. */
263 { write_exp_elt_opcode (BINOP_COMMA); }
266 /* Expressions, not including the comma operator. */
267 exp : '*' exp %prec UNARY
268 { write_exp_elt_opcode (UNOP_IND); }
271 exp : '&' exp %prec UNARY
272 { write_exp_elt_opcode (UNOP_ADDR); }
275 exp : '-' exp %prec UNARY
276 { write_exp_elt_opcode (UNOP_NEG); }
279 exp : '+' exp %prec UNARY
280 { write_exp_elt_opcode (UNOP_PLUS); }
283 exp : '!' exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
287 exp : '~' exp %prec UNARY
288 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
291 exp : INCREMENT exp %prec UNARY
292 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
295 exp : DECREMENT exp %prec UNARY
296 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
299 exp : exp INCREMENT %prec UNARY
300 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
303 exp : exp DECREMENT %prec UNARY
304 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
307 exp : SIZEOF exp %prec UNARY
308 { write_exp_elt_opcode (UNOP_SIZEOF); }
312 { write_exp_elt_opcode (STRUCTOP_PTR);
313 write_exp_string ($3);
314 write_exp_elt_opcode (STRUCTOP_PTR); }
317 exp : exp ARROW name COMPLETE
318 { mark_struct_expression ();
319 write_exp_elt_opcode (STRUCTOP_PTR);
320 write_exp_string ($3);
321 write_exp_elt_opcode (STRUCTOP_PTR); }
324 exp : exp ARROW COMPLETE
326 mark_struct_expression ();
327 write_exp_elt_opcode (STRUCTOP_PTR);
330 write_exp_string (s);
331 write_exp_elt_opcode (STRUCTOP_PTR); }
334 exp : exp ARROW qualified_name
335 { /* exp->type::name becomes exp->*(&type::name) */
336 /* Note: this doesn't work if name is a
337 static member! FIXME */
338 write_exp_elt_opcode (UNOP_ADDR);
339 write_exp_elt_opcode (STRUCTOP_MPTR); }
342 exp : exp ARROW_STAR exp
343 { write_exp_elt_opcode (STRUCTOP_MPTR); }
347 { write_exp_elt_opcode (STRUCTOP_STRUCT);
348 write_exp_string ($3);
349 write_exp_elt_opcode (STRUCTOP_STRUCT); }
352 exp : exp '.' name COMPLETE
353 { mark_struct_expression ();
354 write_exp_elt_opcode (STRUCTOP_STRUCT);
355 write_exp_string ($3);
356 write_exp_elt_opcode (STRUCTOP_STRUCT); }
359 exp : exp '.' COMPLETE
361 mark_struct_expression ();
362 write_exp_elt_opcode (STRUCTOP_STRUCT);
365 write_exp_string (s);
366 write_exp_elt_opcode (STRUCTOP_STRUCT); }
369 exp : exp '.' qualified_name
370 { /* exp.type::name becomes exp.*(&type::name) */
371 /* Note: this doesn't work if name is a
372 static member! FIXME */
373 write_exp_elt_opcode (UNOP_ADDR);
374 write_exp_elt_opcode (STRUCTOP_MEMBER); }
377 exp : exp DOT_STAR exp
378 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
381 exp : exp '[' exp1 ']'
382 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
386 /* This is to save the value of arglist_len
387 being accumulated by an outer function call. */
388 { start_arglist (); }
389 arglist ')' %prec ARROW
390 { write_exp_elt_opcode (OP_FUNCALL);
391 write_exp_elt_longcst ((LONGEST) end_arglist ());
392 write_exp_elt_opcode (OP_FUNCALL); }
395 exp : UNKNOWN_CPP_NAME '('
397 /* This could potentially be a an argument defined
398 lookup function (Koenig). */
399 write_exp_elt_opcode (OP_ADL_FUNC);
400 write_exp_elt_block (expression_context_block);
401 write_exp_elt_sym (NULL); /* Placeholder. */
402 write_exp_string ($1.stoken);
403 write_exp_elt_opcode (OP_ADL_FUNC);
405 /* This is to save the value of arglist_len
406 being accumulated by an outer function call. */
410 arglist ')' %prec ARROW
412 write_exp_elt_opcode (OP_FUNCALL);
413 write_exp_elt_longcst ((LONGEST) end_arglist ());
414 write_exp_elt_opcode (OP_FUNCALL);
419 { start_arglist (); }
429 arglist : arglist ',' exp %prec ABOVE_COMMA
433 exp : exp '(' nonempty_typelist ')' const_or_volatile
435 write_exp_elt_opcode (TYPE_INSTANCE);
436 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
437 for (i = 0; i < $<ivec>3[0]; ++i)
438 write_exp_elt_type ($<tvec>3[i + 1]);
439 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
440 write_exp_elt_opcode (TYPE_INSTANCE);
446 { $$ = end_arglist () - 1; }
448 exp : lcurly arglist rcurly %prec ARROW
449 { write_exp_elt_opcode (OP_ARRAY);
450 write_exp_elt_longcst ((LONGEST) 0);
451 write_exp_elt_longcst ((LONGEST) $3);
452 write_exp_elt_opcode (OP_ARRAY); }
455 exp : lcurly type rcurly exp %prec UNARY
456 { write_exp_elt_opcode (UNOP_MEMVAL);
457 write_exp_elt_type ($2);
458 write_exp_elt_opcode (UNOP_MEMVAL); }
461 exp : '(' type ')' exp %prec UNARY
462 { write_exp_elt_opcode (UNOP_CAST);
463 write_exp_elt_type ($2);
464 write_exp_elt_opcode (UNOP_CAST); }
471 /* Binary operators in order of decreasing precedence. */
474 { write_exp_elt_opcode (BINOP_REPEAT); }
478 { write_exp_elt_opcode (BINOP_MUL); }
482 { write_exp_elt_opcode (BINOP_DIV); }
486 { write_exp_elt_opcode (BINOP_REM); }
490 { write_exp_elt_opcode (BINOP_ADD); }
494 { write_exp_elt_opcode (BINOP_SUB); }
498 { write_exp_elt_opcode (BINOP_LSH); }
502 { write_exp_elt_opcode (BINOP_RSH); }
506 { write_exp_elt_opcode (BINOP_EQUAL); }
509 exp : exp NOTEQUAL exp
510 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
514 { write_exp_elt_opcode (BINOP_LEQ); }
518 { write_exp_elt_opcode (BINOP_GEQ); }
522 { write_exp_elt_opcode (BINOP_LESS); }
526 { write_exp_elt_opcode (BINOP_GTR); }
530 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
534 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
538 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
542 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
546 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
549 exp : exp '?' exp ':' exp %prec '?'
550 { write_exp_elt_opcode (TERNOP_COND); }
554 { write_exp_elt_opcode (BINOP_ASSIGN); }
557 exp : exp ASSIGN_MODIFY exp
558 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
559 write_exp_elt_opcode ($2);
560 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
564 { write_exp_elt_opcode (OP_LONG);
565 write_exp_elt_type ($1.type);
566 write_exp_elt_longcst ((LONGEST)($1.val));
567 write_exp_elt_opcode (OP_LONG); }
572 struct stoken_vector vec;
575 write_exp_string_vector ($1.type, &vec);
581 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
582 write_exp_elt_opcode (OP_LONG);
583 write_exp_elt_type (val.typed_val_int.type);
584 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
585 write_exp_elt_opcode (OP_LONG);
591 { write_exp_elt_opcode (OP_DOUBLE);
592 write_exp_elt_type ($1.type);
593 write_exp_elt_dblcst ($1.dval);
594 write_exp_elt_opcode (OP_DOUBLE); }
598 { write_exp_elt_opcode (OP_DECFLOAT);
599 write_exp_elt_type ($1.type);
600 write_exp_elt_decfloatcst ($1.val);
601 write_exp_elt_opcode (OP_DECFLOAT); }
609 write_dollar_variable ($1);
613 exp : SIZEOF '(' type ')' %prec UNARY
614 { write_exp_elt_opcode (OP_LONG);
615 write_exp_elt_type (parse_type->builtin_int);
617 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
618 write_exp_elt_opcode (OP_LONG); }
621 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
622 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
623 write_exp_elt_type ($3);
624 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
627 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
628 { write_exp_elt_opcode (UNOP_CAST);
629 write_exp_elt_type ($3);
630 write_exp_elt_opcode (UNOP_CAST); }
633 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
634 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
635 write_exp_elt_type ($3);
636 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
639 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
640 { /* We could do more error checking here, but
641 it doesn't seem worthwhile. */
642 write_exp_elt_opcode (UNOP_CAST);
643 write_exp_elt_type ($3);
644 write_exp_elt_opcode (UNOP_CAST); }
650 /* We copy the string here, and not in the
651 lexer, to guarantee that we do not leak a
652 string. Note that we follow the
653 NUL-termination convention of the
655 struct typed_stoken *vec = XNEW (struct typed_stoken);
660 vec->length = $1.length;
661 vec->ptr = malloc ($1.length + 1);
662 memcpy (vec->ptr, $1.ptr, $1.length + 1);
667 /* Note that we NUL-terminate here, but just
671 $$.tokens = realloc ($$.tokens,
672 $$.len * sizeof (struct typed_stoken));
674 p = malloc ($2.length + 1);
675 memcpy (p, $2.ptr, $2.length + 1);
677 $$.tokens[$$.len - 1].type = $2.type;
678 $$.tokens[$$.len - 1].length = $2.length;
679 $$.tokens[$$.len - 1].ptr = p;
686 enum c_string_type type = C_STRING;
688 for (i = 0; i < $1.len; ++i)
690 switch ($1.tokens[i].type)
698 && type != $1.tokens[i].type)
699 error ("Undefined string concatenation.");
700 type = $1.tokens[i].type;
704 internal_error (__FILE__, __LINE__,
705 "unrecognized type in string concatenation");
709 write_exp_string_vector (type, &$1);
710 for (i = 0; i < $1.len; ++i)
711 free ($1.tokens[i].ptr);
718 { write_exp_elt_opcode (OP_LONG);
719 write_exp_elt_type (parse_type->builtin_bool);
720 write_exp_elt_longcst ((LONGEST) 1);
721 write_exp_elt_opcode (OP_LONG); }
725 { write_exp_elt_opcode (OP_LONG);
726 write_exp_elt_type (parse_type->builtin_bool);
727 write_exp_elt_longcst ((LONGEST) 0);
728 write_exp_elt_opcode (OP_LONG); }
736 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
738 error ("No file or function \"%s\".",
739 copy_name ($1.stoken));
747 block : block COLONCOLON name
749 = lookup_symbol (copy_name ($3), $1,
750 VAR_DOMAIN, (int *) NULL);
751 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
752 error ("No function \"%s\" in specified context.",
754 $$ = SYMBOL_BLOCK_VALUE (tem); }
757 variable: block COLONCOLON name
758 { struct symbol *sym;
759 sym = lookup_symbol (copy_name ($3), $1,
760 VAR_DOMAIN, (int *) NULL);
762 error ("No symbol \"%s\" in specified context.",
765 write_exp_elt_opcode (OP_VAR_VALUE);
766 /* block_found is set by lookup_symbol. */
767 write_exp_elt_block (block_found);
768 write_exp_elt_sym (sym);
769 write_exp_elt_opcode (OP_VAR_VALUE); }
772 qualified_name: TYPENAME COLONCOLON name
774 struct type *type = $1.type;
775 CHECK_TYPEDEF (type);
776 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
777 && TYPE_CODE (type) != TYPE_CODE_UNION
778 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
779 error ("`%s' is not defined as an aggregate type.",
782 write_exp_elt_opcode (OP_SCOPE);
783 write_exp_elt_type (type);
784 write_exp_string ($3);
785 write_exp_elt_opcode (OP_SCOPE);
787 | TYPENAME COLONCOLON '~' name
789 struct type *type = $1.type;
790 struct stoken tmp_token;
791 CHECK_TYPEDEF (type);
792 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
793 && TYPE_CODE (type) != TYPE_CODE_UNION
794 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
795 error ("`%s' is not defined as an aggregate type.",
798 tmp_token.ptr = (char*) alloca ($4.length + 2);
799 tmp_token.length = $4.length + 1;
800 tmp_token.ptr[0] = '~';
801 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
802 tmp_token.ptr[tmp_token.length] = 0;
804 /* Check for valid destructor name. */
805 destructor_name_p (tmp_token.ptr, type);
806 write_exp_elt_opcode (OP_SCOPE);
807 write_exp_elt_type (type);
808 write_exp_string (tmp_token);
809 write_exp_elt_opcode (OP_SCOPE);
811 | TYPENAME COLONCOLON name COLONCOLON name
813 char *copy = copy_name ($3);
814 error (_("No type \"%s\" within class "
815 "or namespace \"%s\"."),
816 copy, TYPE_NAME ($1.type));
820 variable: qualified_name
821 | COLONCOLON name_not_typename
823 char *name = copy_name ($2.stoken);
825 struct minimal_symbol *msymbol;
828 lookup_symbol (name, (const struct block *) NULL,
829 VAR_DOMAIN, (int *) NULL);
832 write_exp_elt_opcode (OP_VAR_VALUE);
833 write_exp_elt_block (NULL);
834 write_exp_elt_sym (sym);
835 write_exp_elt_opcode (OP_VAR_VALUE);
839 msymbol = lookup_minimal_symbol (name, NULL, NULL);
841 write_exp_msymbol (msymbol);
842 else if (!have_full_symbols () && !have_partial_symbols ())
843 error ("No symbol table is loaded. Use the \"file\" command.");
845 error ("No symbol \"%s\" in current context.", name);
849 variable: name_not_typename
850 { struct symbol *sym = $1.sym;
854 if (symbol_read_needs_frame (sym))
856 if (innermost_block == 0
857 || contained_in (block_found,
859 innermost_block = block_found;
862 write_exp_elt_opcode (OP_VAR_VALUE);
863 /* We want to use the selected frame, not
864 another more inner frame which happens to
865 be in the same block. */
866 write_exp_elt_block (NULL);
867 write_exp_elt_sym (sym);
868 write_exp_elt_opcode (OP_VAR_VALUE);
870 else if ($1.is_a_field_of_this)
872 /* C++: it hangs off of `this'. Must
873 not inadvertently convert from a method call
875 if (innermost_block == 0
876 || contained_in (block_found,
878 innermost_block = block_found;
879 write_exp_elt_opcode (OP_THIS);
880 write_exp_elt_opcode (OP_THIS);
881 write_exp_elt_opcode (STRUCTOP_PTR);
882 write_exp_string ($1.stoken);
883 write_exp_elt_opcode (STRUCTOP_PTR);
887 struct minimal_symbol *msymbol;
888 char *arg = copy_name ($1.stoken);
891 lookup_minimal_symbol (arg, NULL, NULL);
893 write_exp_msymbol (msymbol);
894 else if (!have_full_symbols () && !have_partial_symbols ())
895 error ("No symbol table is loaded. Use the \"file\" command.");
897 error ("No symbol \"%s\" in current context.",
898 copy_name ($1.stoken));
903 space_identifier : '@' NAME
904 { push_type_address_space (copy_name ($2.stoken));
905 push_type (tp_space_identifier);
909 const_or_volatile: const_or_volatile_noopt
913 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
916 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
917 | const_or_volatile_noopt
920 const_or_volatile_or_space_identifier:
921 const_or_volatile_or_space_identifier_noopt
926 { push_type (tp_pointer); $$ = 0; }
928 { push_type (tp_pointer); $$ = $2; }
930 { push_type (tp_reference); $$ = 0; }
932 { push_type (tp_reference); $$ = $2; }
936 direct_abs_decl: '(' abs_decl ')'
938 | direct_abs_decl array_mod
941 push_type (tp_array);
946 push_type (tp_array);
950 | direct_abs_decl func_mod
951 { push_type (tp_function); }
953 { push_type (tp_function); }
964 | '(' nonempty_typelist ')'
965 { free ($2); $$ = 0; }
968 /* We used to try to recognize pointer to member types here, but
969 that didn't work (shift/reduce conflicts meant that these rules never
970 got executed). The problem is that
971 int (foo::bar::baz::bizzle)
972 is a function type but
973 int (foo::bar::baz::bizzle::*)
974 is a pointer to member type. Stroustrup loses again! */
979 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
983 { $$ = parse_type->builtin_int; }
985 { $$ = parse_type->builtin_long; }
987 { $$ = parse_type->builtin_short; }
989 { $$ = parse_type->builtin_long; }
990 | LONG SIGNED_KEYWORD INT_KEYWORD
991 { $$ = parse_type->builtin_long; }
992 | LONG SIGNED_KEYWORD
993 { $$ = parse_type->builtin_long; }
994 | SIGNED_KEYWORD LONG INT_KEYWORD
995 { $$ = parse_type->builtin_long; }
996 | UNSIGNED LONG INT_KEYWORD
997 { $$ = parse_type->builtin_unsigned_long; }
998 | LONG UNSIGNED INT_KEYWORD
999 { $$ = parse_type->builtin_unsigned_long; }
1001 { $$ = parse_type->builtin_unsigned_long; }
1003 { $$ = parse_type->builtin_long_long; }
1004 | LONG LONG INT_KEYWORD
1005 { $$ = parse_type->builtin_long_long; }
1006 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1007 { $$ = parse_type->builtin_long_long; }
1008 | LONG LONG SIGNED_KEYWORD
1009 { $$ = parse_type->builtin_long_long; }
1010 | SIGNED_KEYWORD LONG LONG
1011 { $$ = parse_type->builtin_long_long; }
1012 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1013 { $$ = parse_type->builtin_long_long; }
1014 | UNSIGNED LONG LONG
1015 { $$ = parse_type->builtin_unsigned_long_long; }
1016 | UNSIGNED LONG LONG INT_KEYWORD
1017 { $$ = parse_type->builtin_unsigned_long_long; }
1018 | LONG LONG UNSIGNED
1019 { $$ = parse_type->builtin_unsigned_long_long; }
1020 | LONG LONG UNSIGNED INT_KEYWORD
1021 { $$ = parse_type->builtin_unsigned_long_long; }
1023 { $$ = parse_type->builtin_short; }
1024 | SHORT SIGNED_KEYWORD INT_KEYWORD
1025 { $$ = parse_type->builtin_short; }
1026 | SHORT SIGNED_KEYWORD
1027 { $$ = parse_type->builtin_short; }
1028 | UNSIGNED SHORT INT_KEYWORD
1029 { $$ = parse_type->builtin_unsigned_short; }
1031 { $$ = parse_type->builtin_unsigned_short; }
1032 | SHORT UNSIGNED INT_KEYWORD
1033 { $$ = parse_type->builtin_unsigned_short; }
1035 { $$ = parse_type->builtin_double; }
1036 | LONG DOUBLE_KEYWORD
1037 { $$ = parse_type->builtin_long_double; }
1039 { $$ = lookup_struct (copy_name ($2),
1040 expression_context_block); }
1042 { $$ = lookup_struct (copy_name ($2),
1043 expression_context_block); }
1045 { $$ = lookup_union (copy_name ($2),
1046 expression_context_block); }
1048 { $$ = lookup_enum (copy_name ($2),
1049 expression_context_block); }
1051 { $$ = lookup_unsigned_typename (parse_language,
1053 TYPE_NAME($2.type)); }
1055 { $$ = parse_type->builtin_unsigned_int; }
1056 | SIGNED_KEYWORD typename
1057 { $$ = lookup_signed_typename (parse_language,
1059 TYPE_NAME($2.type)); }
1061 { $$ = parse_type->builtin_int; }
1062 /* It appears that this rule for templates is never
1063 reduced; template recognition happens by lookahead
1064 in the token processing code in yylex. */
1065 | TEMPLATE name '<' type '>'
1066 { $$ = lookup_template_type(copy_name($2), $4,
1067 expression_context_block);
1069 | const_or_volatile_or_space_identifier_noopt typebase
1070 { $$ = follow_types ($2); }
1071 | typebase const_or_volatile_or_space_identifier_noopt
1072 { $$ = follow_types ($1); }
1078 $$.stoken.ptr = "int";
1079 $$.stoken.length = 3;
1080 $$.type = parse_type->builtin_int;
1084 $$.stoken.ptr = "long";
1085 $$.stoken.length = 4;
1086 $$.type = parse_type->builtin_long;
1090 $$.stoken.ptr = "short";
1091 $$.stoken.length = 5;
1092 $$.type = parse_type->builtin_short;
1098 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1099 $<ivec>$[0] = 1; /* Number of types in vector */
1102 | nonempty_typelist ',' type
1103 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1104 $$ = (struct type **) realloc ((char *) $1, len);
1105 $$[$<ivec>$[0]] = $3;
1110 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1111 { $$ = follow_types ($1); }
1114 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1115 | VOLATILE_KEYWORD CONST_KEYWORD
1118 const_or_volatile_noopt: const_and_volatile
1119 { push_type (tp_const);
1120 push_type (tp_volatile);
1123 { push_type (tp_const); }
1125 { push_type (tp_volatile); }
1128 operator: OPERATOR NEW
1129 { $$ = operator_stoken (" new"); }
1131 { $$ = operator_stoken (" delete"); }
1132 | OPERATOR NEW '[' ']'
1133 { $$ = operator_stoken (" new[]"); }
1134 | OPERATOR DELETE '[' ']'
1135 { $$ = operator_stoken (" delete[]"); }
1137 { $$ = operator_stoken ("+"); }
1139 { $$ = operator_stoken ("-"); }
1141 { $$ = operator_stoken ("*"); }
1143 { $$ = operator_stoken ("/"); }
1145 { $$ = operator_stoken ("%"); }
1147 { $$ = operator_stoken ("^"); }
1149 { $$ = operator_stoken ("&"); }
1151 { $$ = operator_stoken ("|"); }
1153 { $$ = operator_stoken ("~"); }
1155 { $$ = operator_stoken ("!"); }
1157 { $$ = operator_stoken ("="); }
1159 { $$ = operator_stoken ("<"); }
1161 { $$ = operator_stoken (">"); }
1162 | OPERATOR ASSIGN_MODIFY
1163 { const char *op = "unknown";
1187 case BINOP_BITWISE_IOR:
1190 case BINOP_BITWISE_AND:
1193 case BINOP_BITWISE_XOR:
1200 $$ = operator_stoken (op);
1203 { $$ = operator_stoken ("<<"); }
1205 { $$ = operator_stoken (">>"); }
1207 { $$ = operator_stoken ("=="); }
1209 { $$ = operator_stoken ("!="); }
1211 { $$ = operator_stoken ("<="); }
1213 { $$ = operator_stoken (">="); }
1215 { $$ = operator_stoken ("&&"); }
1217 { $$ = operator_stoken ("||"); }
1218 | OPERATOR INCREMENT
1219 { $$ = operator_stoken ("++"); }
1220 | OPERATOR DECREMENT
1221 { $$ = operator_stoken ("--"); }
1223 { $$ = operator_stoken (","); }
1224 | OPERATOR ARROW_STAR
1225 { $$ = operator_stoken ("->*"); }
1227 { $$ = operator_stoken ("->"); }
1229 { $$ = operator_stoken ("()"); }
1231 { $$ = operator_stoken ("[]"); }
1235 struct ui_file *buf = mem_fileopen ();
1237 c_print_type ($2, NULL, buf, -1, 0);
1238 name = ui_file_xstrdup (buf, &length);
1239 ui_file_delete (buf);
1240 $$ = operator_stoken (name);
1247 name : NAME { $$ = $1.stoken; }
1248 | BLOCKNAME { $$ = $1.stoken; }
1249 | TYPENAME { $$ = $1.stoken; }
1250 | NAME_OR_INT { $$ = $1.stoken; }
1251 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1252 | operator { $$ = $1; }
1255 name_not_typename : NAME
1257 /* These would be useful if name_not_typename was useful, but it is just
1258 a fake for "variable", so these cause reduce/reduce conflicts because
1259 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1260 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1261 context where only a name could occur, this might be useful.
1267 $$.sym = lookup_symbol ($1.ptr,
1268 expression_context_block,
1270 &$$.is_a_field_of_this);
1277 /* Returns a stoken of the operator name given by OP (which does not
1278 include the string "operator"). */
1279 static struct stoken
1280 operator_stoken (const char *op)
1282 static const char *operator_string = "operator";
1283 struct stoken st = { NULL, 0 };
1284 st.length = strlen (operator_string) + strlen (op);
1285 st.ptr = malloc (st.length + 1);
1286 strcpy (st.ptr, operator_string);
1287 strcat (st.ptr, op);
1289 /* The toplevel (c_parse) will free the memory allocated here. */
1290 make_cleanup (free, st.ptr);
1294 /* Take care of parsing a number (anything that starts with a digit).
1295 Set yylval and return the token type; update lexptr.
1296 LEN is the number of characters in it. */
1298 /*** Needs some error checking for the float case ***/
1301 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1303 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1304 here, and we do kind of silly things like cast to unsigned. */
1311 int base = input_radix;
1314 /* Number of "L" suffixes encountered. */
1317 /* We have found a "L" or "U" suffix. */
1318 int found_suffix = 0;
1321 struct type *signed_type;
1322 struct type *unsigned_type;
1326 /* It's a float since it contains a point or an exponent. */
1328 int num; /* number of tokens scanned by scanf */
1331 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1332 point. Return DECFLOAT. */
1334 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1337 putithere->typed_val_decfloat.type
1338 = parse_type->builtin_decfloat;
1339 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1340 gdbarch_byte_order (parse_gdbarch), p);
1345 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1348 putithere->typed_val_decfloat.type
1349 = parse_type->builtin_decdouble;
1350 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1351 gdbarch_byte_order (parse_gdbarch), p);
1356 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1359 putithere->typed_val_decfloat.type
1360 = parse_type->builtin_declong;
1361 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1362 gdbarch_byte_order (parse_gdbarch), p);
1368 saved_char = p[len];
1369 p[len] = 0; /* null-terminate the token */
1370 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
1371 &putithere->typed_val_float.dval, s);
1372 p[len] = saved_char; /* restore the input stream */
1381 putithere->typed_val_float.type =
1382 parse_type->builtin_double;
1386 /* See if it has any float suffix: 'f' for float, 'l' for long
1388 if (!strcasecmp (s, "f"))
1389 putithere->typed_val_float.type =
1390 parse_type->builtin_float;
1391 else if (!strcasecmp (s, "l"))
1392 putithere->typed_val_float.type =
1393 parse_type->builtin_long_double;
1405 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1449 if (c >= 'A' && c <= 'Z')
1451 if (c != 'l' && c != 'u')
1453 if (c >= '0' && c <= '9')
1461 if (base > 10 && c >= 'a' && c <= 'f')
1465 n += i = c - 'a' + 10;
1478 return ERROR; /* Char not a digit */
1481 return ERROR; /* Invalid digit in this base */
1483 /* Portably test for overflow (only works for nonzero values, so make
1484 a second check for zero). FIXME: Can't we just make n and prevn
1485 unsigned and avoid this? */
1486 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1487 unsigned_p = 1; /* Try something unsigned */
1489 /* Portably test for unsigned overflow.
1490 FIXME: This check is wrong; for example it doesn't find overflow
1491 on 0x123456789 when LONGEST is 32 bits. */
1492 if (c != 'l' && c != 'u' && n != 0)
1494 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1495 error ("Numeric constant too large.");
1500 /* An integer constant is an int, a long, or a long long. An L
1501 suffix forces it to be long; an LL suffix forces it to be long
1502 long. If not forced to a larger size, it gets the first type of
1503 the above that it fits in. To figure out whether it fits, we
1504 shift it right and see whether anything remains. Note that we
1505 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1506 operation, because many compilers will warn about such a shift
1507 (which always produces a zero result). Sometimes gdbarch_int_bit
1508 or gdbarch_long_bit will be that big, sometimes not. To deal with
1509 the case where it is we just always shift the value more than
1510 once, with fewer bits each time. */
1512 un = (ULONGEST)n >> 2;
1514 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1516 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1518 /* A large decimal (not hex or octal) constant (between INT_MAX
1519 and UINT_MAX) is a long or unsigned long, according to ANSI,
1520 never an unsigned int, but this code treats it as unsigned
1521 int. This probably should be fixed. GCC gives a warning on
1524 unsigned_type = parse_type->builtin_unsigned_int;
1525 signed_type = parse_type->builtin_int;
1527 else if (long_p <= 1
1528 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1530 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1531 unsigned_type = parse_type->builtin_unsigned_long;
1532 signed_type = parse_type->builtin_long;
1537 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1538 < gdbarch_long_long_bit (parse_gdbarch))
1539 /* A long long does not fit in a LONGEST. */
1540 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1542 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1543 high_bit = (ULONGEST) 1 << shift;
1544 unsigned_type = parse_type->builtin_unsigned_long_long;
1545 signed_type = parse_type->builtin_long_long;
1548 putithere->typed_val_int.val = n;
1550 /* If the high bit of the worked out type is set then this number
1551 has to be unsigned. */
1553 if (unsigned_p || (n & high_bit))
1555 putithere->typed_val_int.type = unsigned_type;
1559 putithere->typed_val_int.type = signed_type;
1565 /* Temporary obstack used for holding strings. */
1566 static struct obstack tempbuf;
1567 static int tempbuf_init;
1569 /* Parse a C escape sequence. The initial backslash of the sequence
1570 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1571 last character of the sequence. If OUTPUT is not NULL, the
1572 translated form of the escape sequence will be written there. If
1573 OUTPUT is NULL, no output is written and the call will only affect
1574 *PTR. If an escape sequence is expressed in target bytes, then the
1575 entire sequence will simply be copied to OUTPUT. Return 1 if any
1576 character was emitted, 0 otherwise. */
1579 c_parse_escape (char **ptr, struct obstack *output)
1581 char *tokptr = *ptr;
1584 /* Some escape sequences undergo character set conversion. Those we
1588 /* Hex escapes do not undergo character set conversion, so keep
1589 the escape sequence for later. */
1592 obstack_grow_str (output, "\\x");
1594 if (!isxdigit (*tokptr))
1595 error (_("\\x escape without a following hex digit"));
1596 while (isxdigit (*tokptr))
1599 obstack_1grow (output, *tokptr);
1604 /* Octal escapes do not undergo character set conversion, so
1605 keep the escape sequence for later. */
1617 obstack_grow_str (output, "\\");
1619 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1623 obstack_1grow (output, *tokptr);
1629 /* We handle UCNs later. We could handle them here, but that
1630 would mean a spurious error in the case where the UCN could
1631 be converted to the target charset but not the host
1637 int i, len = c == 'U' ? 8 : 4;
1640 obstack_1grow (output, '\\');
1641 obstack_1grow (output, *tokptr);
1644 if (!isxdigit (*tokptr))
1645 error (_("\\%c escape without a following hex digit"), c);
1646 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1649 obstack_1grow (output, *tokptr);
1655 /* We must pass backslash through so that it does not
1656 cause quoting during the second expansion. */
1659 obstack_grow_str (output, "\\\\");
1663 /* Escapes which undergo conversion. */
1666 obstack_1grow (output, '\a');
1671 obstack_1grow (output, '\b');
1676 obstack_1grow (output, '\f');
1681 obstack_1grow (output, '\n');
1686 obstack_1grow (output, '\r');
1691 obstack_1grow (output, '\t');
1696 obstack_1grow (output, '\v');
1700 /* GCC extension. */
1703 obstack_1grow (output, HOST_ESCAPE_CHAR);
1707 /* Backslash-newline expands to nothing at all. */
1713 /* A few escapes just expand to the character itself. */
1717 /* GCC extensions. */
1722 /* Unrecognized escapes turn into the character itself. */
1725 obstack_1grow (output, *tokptr);
1733 /* Parse a string or character literal from TOKPTR. The string or
1734 character may be wide or unicode. *OUTPTR is set to just after the
1735 end of the literal in the input string. The resulting token is
1736 stored in VALUE. This returns a token value, either STRING or
1737 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1738 number of host characters in the literal. */
1740 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1744 enum c_string_type type;
1746 /* Build the gdb internal form of the input string in tempbuf. Note
1747 that the buffer is null byte terminated *only* for the
1748 convenience of debugging gdb itself and printing the buffer
1749 contents when the buffer contains no embedded nulls. Gdb does
1750 not depend upon the buffer being null byte terminated, it uses
1751 the length string instead. This allows gdb to handle C strings
1752 (as well as strings in other languages) with embedded null
1758 obstack_free (&tempbuf, NULL);
1759 obstack_init (&tempbuf);
1761 /* Record the string type. */
1764 type = C_WIDE_STRING;
1767 else if (*tokptr == 'u')
1772 else if (*tokptr == 'U')
1780 /* Skip the quote. */
1794 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1796 else if (c == quote)
1800 obstack_1grow (&tempbuf, c);
1802 /* FIXME: this does the wrong thing with multi-byte host
1803 characters. We could use mbrlen here, but that would
1804 make "set host-charset" a bit less useful. */
1809 if (*tokptr != quote)
1812 error ("Unterminated string in expression.");
1814 error ("Unmatched single quote.");
1819 value->ptr = obstack_base (&tempbuf);
1820 value->length = obstack_object_size (&tempbuf);
1824 return quote == '"' ? STRING : CHAR;
1831 enum exp_opcode opcode;
1835 static const struct token tokentab3[] =
1837 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1838 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1839 {"->*", ARROW_STAR, BINOP_END, 1}
1842 static const struct token tokentab2[] =
1844 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1845 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1846 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1847 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1848 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1849 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1850 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1851 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1852 {"++", INCREMENT, BINOP_END, 0},
1853 {"--", DECREMENT, BINOP_END, 0},
1854 {"->", ARROW, BINOP_END, 0},
1855 {"&&", ANDAND, BINOP_END, 0},
1856 {"||", OROR, BINOP_END, 0},
1857 /* "::" is *not* only C++: gdb overrides its meaning in several
1858 different ways, e.g., 'filename'::func, function::variable. */
1859 {"::", COLONCOLON, BINOP_END, 0},
1860 {"<<", LSH, BINOP_END, 0},
1861 {">>", RSH, BINOP_END, 0},
1862 {"==", EQUAL, BINOP_END, 0},
1863 {"!=", NOTEQUAL, BINOP_END, 0},
1864 {"<=", LEQ, BINOP_END, 0},
1865 {">=", GEQ, BINOP_END, 0},
1866 {".*", DOT_STAR, BINOP_END, 1}
1869 /* Identifier-like tokens. */
1870 static const struct token ident_tokens[] =
1872 {"unsigned", UNSIGNED, OP_NULL, 0},
1873 {"template", TEMPLATE, OP_NULL, 1},
1874 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1875 {"struct", STRUCT, OP_NULL, 0},
1876 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1877 {"sizeof", SIZEOF, OP_NULL, 0},
1878 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1879 {"false", FALSEKEYWORD, OP_NULL, 1},
1880 {"class", CLASS, OP_NULL, 1},
1881 {"union", UNION, OP_NULL, 0},
1882 {"short", SHORT, OP_NULL, 0},
1883 {"const", CONST_KEYWORD, OP_NULL, 0},
1884 {"enum", ENUM, OP_NULL, 0},
1885 {"long", LONG, OP_NULL, 0},
1886 {"true", TRUEKEYWORD, OP_NULL, 1},
1887 {"int", INT_KEYWORD, OP_NULL, 0},
1888 {"new", NEW, OP_NULL, 1},
1889 {"delete", DELETE, OP_NULL, 1},
1890 {"operator", OPERATOR, OP_NULL, 1},
1892 {"and", ANDAND, BINOP_END, 1},
1893 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1894 {"bitand", '&', OP_NULL, 1},
1895 {"bitor", '|', OP_NULL, 1},
1896 {"compl", '~', OP_NULL, 1},
1897 {"not", '!', OP_NULL, 1},
1898 {"not_eq", NOTEQUAL, BINOP_END, 1},
1899 {"or", OROR, BINOP_END, 1},
1900 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1901 {"xor", '^', OP_NULL, 1},
1902 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1904 {"const_cast", CONST_CAST, OP_NULL, 1 },
1905 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1906 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1907 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1910 /* When we find that lexptr (the global var defined in parse.c) is
1911 pointing at a macro invocation, we expand the invocation, and call
1912 scan_macro_expansion to save the old lexptr here and point lexptr
1913 into the expanded text. When we reach the end of that, we call
1914 end_macro_expansion to pop back to the value we saved here. The
1915 macro expansion code promises to return only fully-expanded text,
1916 so we don't need to "push" more than one level.
1918 This is disgusting, of course. It would be cleaner to do all macro
1919 expansion beforehand, and then hand that to lexptr. But we don't
1920 really know where the expression ends. Remember, in a command like
1922 (gdb) break *ADDRESS if CONDITION
1924 we evaluate ADDRESS in the scope of the current frame, but we
1925 evaluate CONDITION in the scope of the breakpoint's location. So
1926 it's simply wrong to try to macro-expand the whole thing at once. */
1927 static char *macro_original_text;
1929 /* We save all intermediate macro expansions on this obstack for the
1930 duration of a single parse. The expansion text may sometimes have
1931 to live past the end of the expansion, due to yacc lookahead.
1932 Rather than try to be clever about saving the data for a single
1933 token, we simply keep it all and delete it after parsing has
1935 static struct obstack expansion_obstack;
1938 scan_macro_expansion (char *expansion)
1942 /* We'd better not be trying to push the stack twice. */
1943 gdb_assert (! macro_original_text);
1945 /* Copy to the obstack, and then free the intermediate
1947 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
1950 /* Save the old lexptr value, so we can return to it when we're done
1951 parsing the expanded text. */
1952 macro_original_text = lexptr;
1958 scanning_macro_expansion (void)
1960 return macro_original_text != 0;
1965 finished_macro_expansion (void)
1967 /* There'd better be something to pop back to. */
1968 gdb_assert (macro_original_text);
1970 /* Pop back to the original text. */
1971 lexptr = macro_original_text;
1972 macro_original_text = 0;
1977 scan_macro_cleanup (void *dummy)
1979 if (macro_original_text)
1980 finished_macro_expansion ();
1982 obstack_free (&expansion_obstack, NULL);
1985 /* Return true iff the token represents a C++ cast operator. */
1988 is_cast_operator (const char *token, int len)
1990 return (! strncmp (token, "dynamic_cast", len)
1991 || ! strncmp (token, "static_cast", len)
1992 || ! strncmp (token, "reinterpret_cast", len)
1993 || ! strncmp (token, "const_cast", len));
1996 /* The scope used for macro expansion. */
1997 static struct macro_scope *expression_macro_scope;
1999 /* This is set if a NAME token appeared at the very end of the input
2000 string, with no whitespace separating the name from the EOF. This
2001 is used only when parsing to do field name completion. */
2002 static int saw_name_at_eof;
2004 /* This is set if the previously-returned token was a structure
2005 operator -- either '.' or ARROW. This is used only when parsing to
2006 do field name completion. */
2007 static int last_was_structop;
2009 /* Read one token, getting characters through lexptr. */
2012 lex_one_token (void)
2018 int saw_structop = last_was_structop;
2021 last_was_structop = 0;
2025 /* Check if this is a macro invocation that we need to expand. */
2026 if (! scanning_macro_expansion ())
2028 char *expanded = macro_expand_next (&lexptr,
2029 standard_macro_lookup,
2030 expression_macro_scope);
2033 scan_macro_expansion (expanded);
2036 prev_lexptr = lexptr;
2039 /* See if it is a special token of length 3. */
2040 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2041 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2043 if (tokentab3[i].cxx_only
2044 && parse_language->la_language != language_cplus)
2048 yylval.opcode = tokentab3[i].opcode;
2049 return tokentab3[i].token;
2052 /* See if it is a special token of length 2. */
2053 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2054 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2056 if (tokentab2[i].cxx_only
2057 && parse_language->la_language != language_cplus)
2061 yylval.opcode = tokentab2[i].opcode;
2062 if (in_parse_field && tokentab2[i].token == ARROW)
2063 last_was_structop = 1;
2064 return tokentab2[i].token;
2067 switch (c = *tokstart)
2070 /* If we were just scanning the result of a macro expansion,
2071 then we need to resume scanning the original text.
2072 If we're parsing for field name completion, and the previous
2073 token allows such completion, return a COMPLETE token.
2074 Otherwise, we were already scanning the original text, and
2075 we're really done. */
2076 if (scanning_macro_expansion ())
2078 finished_macro_expansion ();
2081 else if (saw_name_at_eof)
2083 saw_name_at_eof = 0;
2086 else if (saw_structop)
2105 if (paren_depth == 0)
2112 if (comma_terminates
2114 && ! scanning_macro_expansion ())
2120 /* Might be a floating point number. */
2121 if (lexptr[1] < '0' || lexptr[1] > '9')
2124 last_was_structop = 1;
2125 goto symbol; /* Nope, must be a symbol. */
2127 /* FALL THRU into number case. */
2140 /* It's a number. */
2141 int got_dot = 0, got_e = 0, toktype;
2143 int hex = input_radix > 10;
2145 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2150 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2158 /* This test includes !hex because 'e' is a valid hex digit
2159 and thus does not indicate a floating point number when
2160 the radix is hex. */
2161 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2162 got_dot = got_e = 1;
2163 /* This test does not include !hex, because a '.' always indicates
2164 a decimal floating point number regardless of the radix. */
2165 else if (!got_dot && *p == '.')
2167 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2168 && (*p == '-' || *p == '+'))
2169 /* This is the sign of the exponent, not the end of the
2172 /* We will take any letters or digits. parse_number will
2173 complain if past the radix, or if L or U are not final. */
2174 else if ((*p < '0' || *p > '9')
2175 && ((*p < 'a' || *p > 'z')
2176 && (*p < 'A' || *p > 'Z')))
2179 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2180 if (toktype == ERROR)
2182 char *err_copy = (char *) alloca (p - tokstart + 1);
2184 memcpy (err_copy, tokstart, p - tokstart);
2185 err_copy[p - tokstart] = 0;
2186 error ("Invalid number \"%s\".", err_copy);
2217 if (tokstart[1] != '"' && tokstart[1] != '\'')
2224 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2229 error ("Empty character constant.");
2230 else if (host_len > 2 && c == '\'')
2233 namelen = lexptr - tokstart - 1;
2236 else if (host_len > 1)
2237 error ("Invalid character constant.");
2243 if (!(c == '_' || c == '$'
2244 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2245 /* We must have come across a bad character (e.g. ';'). */
2246 error ("Invalid character '%c' in expression.", c);
2248 /* It's a name. See how long it is. */
2250 for (c = tokstart[namelen];
2251 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2252 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2254 /* Template parameter lists are part of the name.
2255 FIXME: This mishandles `print $a<4&&$a>3'. */
2259 if (! is_cast_operator (tokstart, namelen))
2261 /* Scan ahead to get rest of the template specification. Note
2262 that we look ahead only when the '<' adjoins non-whitespace
2263 characters; for comparison expressions, e.g. "a < b > c",
2264 there must be spaces before the '<', etc. */
2266 char * p = find_template_name_end (tokstart + namelen);
2268 namelen = p - tokstart;
2272 c = tokstart[++namelen];
2275 /* The token "if" terminates the expression and is NOT removed from
2276 the input stream. It doesn't count if it appears in the
2277 expansion of a macro. */
2279 && tokstart[0] == 'i'
2280 && tokstart[1] == 'f'
2281 && ! scanning_macro_expansion ())
2286 /* For the same reason (breakpoint conditions), "thread N"
2287 terminates the expression. "thread" could be an identifier, but
2288 an identifier is never followed by a number without intervening
2289 punctuation. "task" is similar. Handle abbreviations of these,
2290 similarly to breakpoint.c:find_condition_and_thread. */
2292 && (strncmp (tokstart, "thread", namelen) == 0
2293 || strncmp (tokstart, "task", namelen) == 0)
2294 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2295 && ! scanning_macro_expansion ())
2297 char *p = tokstart + namelen + 1;
2298 while (*p == ' ' || *p == '\t')
2300 if (*p >= '0' && *p <= '9')
2308 yylval.sval.ptr = tokstart;
2309 yylval.sval.length = namelen;
2311 /* Catch specific keywords. */
2312 copy = copy_name (yylval.sval);
2313 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2314 if (strcmp (copy, ident_tokens[i].operator) == 0)
2316 if (ident_tokens[i].cxx_only
2317 && parse_language->la_language != language_cplus)
2320 /* It is ok to always set this, even though we don't always
2321 strictly need to. */
2322 yylval.opcode = ident_tokens[i].opcode;
2323 return ident_tokens[i].token;
2326 if (*tokstart == '$')
2329 if (in_parse_field && *lexptr == '\0')
2330 saw_name_at_eof = 1;
2334 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2341 DEF_VEC_O (token_and_value);
2343 /* A FIFO of tokens that have been read but not yet returned to the
2345 static VEC (token_and_value) *token_fifo;
2347 /* Non-zero if the lexer should return tokens from the FIFO. */
2350 /* Temporary storage for c_lex; this holds symbol names as they are
2352 static struct obstack name_obstack;
2354 /* Classify a NAME token. The contents of the token are in `yylval'.
2355 Updates yylval and returns the new token type. BLOCK is the block
2356 in which lookups start; this can be NULL to mean the global
2359 classify_name (struct block *block)
2363 int is_a_field_of_this = 0;
2365 copy = copy_name (yylval.sval);
2367 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2368 parse_language->la_language == language_cplus
2369 ? &is_a_field_of_this : (int *) NULL);
2371 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2373 yylval.ssym.sym = sym;
2374 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2379 /* See if it's a file name. */
2380 struct symtab *symtab;
2382 symtab = lookup_symtab (copy);
2385 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2390 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2392 yylval.tsym.type = SYMBOL_TYPE (sym);
2397 = language_lookup_primitive_type_by_name (parse_language,
2398 parse_gdbarch, copy);
2399 if (yylval.tsym.type != NULL)
2402 /* Input names that aren't symbols but ARE valid hex numbers, when
2403 the input radix permits them, can be names or numbers depending
2404 on the parse. Note we support radixes > 16 here. */
2406 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2407 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2409 YYSTYPE newlval; /* Its value is ignored. */
2410 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2413 yylval.ssym.sym = sym;
2414 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2419 /* Any other kind of symbol */
2420 yylval.ssym.sym = sym;
2421 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2424 && parse_language->la_language == language_cplus
2425 && !lookup_minimal_symbol (copy, NULL, NULL))
2426 return UNKNOWN_CPP_NAME;
2431 /* Like classify_name, but used by the inner loop of the lexer, when a
2432 name might have already been seen. FIRST_NAME is true if the token
2433 in `yylval' is the first component of a name, false otherwise. If
2434 this function returns NAME, it might not have updated `yylval'.
2435 This is ok because the caller only cares about TYPENAME. */
2437 classify_inner_name (struct block *block, int first_name)
2439 struct type *type, *new_type;
2443 return classify_name (block);
2445 type = check_typedef (yylval.tsym.type);
2446 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2447 && TYPE_CODE (type) != TYPE_CODE_UNION
2448 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2449 /* We know the caller won't expect us to update yylval. */
2452 copy = copy_name (yylval.tsym.stoken);
2453 new_type = cp_lookup_nested_type (type, copy, block);
2455 if (new_type == NULL)
2456 /* We know the caller won't expect us to update yylval. */
2459 yylval.tsym.type = new_type;
2463 /* The outer level of a two-level lexer. This calls the inner lexer
2464 to return tokens. It then either returns these tokens, or
2465 aggregates them into a larger token. This lets us work around a
2466 problem in our parsing approach, where the parser could not
2467 distinguish between qualified names and qualified types at the
2470 This approach is still not ideal, because it mishandles template
2471 types. See the comment in lex_one_token for an example. However,
2472 this is still an improvement over the earlier approach, and will
2473 suffice until we move to better parsing technology. */
2477 token_and_value current;
2478 int first_was_coloncolon, last_was_coloncolon, first_iter;
2480 if (popping && !VEC_empty (token_and_value, token_fifo))
2482 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2483 VEC_ordered_remove (token_and_value, token_fifo, 0);
2489 current.token = lex_one_token ();
2490 if (current.token == NAME)
2491 current.token = classify_name (expression_context_block);
2492 if (parse_language->la_language != language_cplus
2493 || (current.token != TYPENAME && current.token != COLONCOLON))
2494 return current.token;
2496 first_was_coloncolon = current.token == COLONCOLON;
2497 last_was_coloncolon = first_was_coloncolon;
2498 obstack_free (&name_obstack, obstack_base (&name_obstack));
2499 if (!last_was_coloncolon)
2500 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2501 current.value = yylval;
2505 token_and_value next;
2507 next.token = lex_one_token ();
2508 next.value = yylval;
2510 if (next.token == NAME && last_was_coloncolon)
2514 classification = classify_inner_name (first_was_coloncolon
2516 : expression_context_block,
2518 /* We keep going until we either run out of names, or until
2519 we have a qualified name which is not a type. */
2520 if (classification != TYPENAME)
2522 /* Push the final component and leave the loop. */
2523 VEC_safe_push (token_and_value, token_fifo, &next);
2527 /* Update the partial name we are constructing. */
2530 /* We don't want to put a leading "::" into the name. */
2531 obstack_grow_str (&name_obstack, "::");
2533 obstack_grow (&name_obstack, next.value.sval.ptr,
2534 next.value.sval.length);
2536 yylval.sval.ptr = obstack_base (&name_obstack);
2537 yylval.sval.length = obstack_object_size (&name_obstack);
2538 current.value = yylval;
2539 current.token = classification;
2541 last_was_coloncolon = 0;
2543 else if (next.token == COLONCOLON && !last_was_coloncolon)
2544 last_was_coloncolon = 1;
2547 /* We've reached the end of the name. */
2548 VEC_safe_push (token_and_value, token_fifo, &next);
2557 /* If we ended with a "::", insert it too. */
2558 if (last_was_coloncolon)
2561 memset (&cc, 0, sizeof (token_and_value));
2562 if (first_was_coloncolon && first_iter)
2567 cc.token = COLONCOLON;
2568 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2571 yylval = current.value;
2572 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2574 yylval.sval.length);
2575 return current.token;
2582 struct cleanup *back_to = make_cleanup (free_current_contents,
2583 &expression_macro_scope);
2585 /* Set up the scope for macro expansion. */
2586 expression_macro_scope = NULL;
2588 if (expression_context_block)
2589 expression_macro_scope
2590 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2592 expression_macro_scope = default_macro_scope ();
2593 if (! expression_macro_scope)
2594 expression_macro_scope = user_macro_scope ();
2596 /* Initialize macro expansion code. */
2597 obstack_init (&expansion_obstack);
2598 gdb_assert (! macro_original_text);
2599 make_cleanup (scan_macro_cleanup, 0);
2601 make_cleanup_restore_integer (&yydebug);
2602 yydebug = parser_debug;
2604 /* Initialize some state used by the lexer. */
2605 last_was_structop = 0;
2606 saw_name_at_eof = 0;
2608 VEC_free (token_and_value, token_fifo);
2610 obstack_init (&name_obstack);
2611 make_cleanup_obstack_free (&name_obstack);
2613 result = yyparse ();
2614 do_cleanups (back_to);
2623 lexptr = prev_lexptr;
2625 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);