1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989-2000, 2003-2004, 2006-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
40 #include "gdb_string.h"
42 #include "expression.h"
44 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #include "cp-support.h"
54 #include "gdb_assert.h"
55 #include "macroscope.h"
57 #define parse_type builtin_type (parse_gdbarch)
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
60 as well as gratuitiously global symbol names, so we can have multiple
61 yacc generated parsers in gdb. Note that these are only the variables
62 produced by yacc. If other parser generators (bison, byacc, etc) produce
63 additional global names that conflict at link time, then those parser
64 generators need to be fixed instead of adding those names to this list. */
66 #define yymaxdepth c_maxdepth
67 #define yyparse c_parse_internal
69 #define yyerror c_error
72 #define yydebug c_debug
81 #define yyerrflag c_errflag
82 #define yynerrs c_nerrs
87 #define yystate c_state
93 #define yyreds c_reds /* With YYDEBUG defined */
94 #define yytoks c_toks /* With YYDEBUG defined */
95 #define yyname c_name /* With YYDEBUG defined */
96 #define yyrule c_rule /* With YYDEBUG defined */
99 #define yydefred c_yydefred
100 #define yydgoto c_yydgoto
101 #define yysindex c_yysindex
102 #define yyrindex c_yyrindex
103 #define yygindex c_yygindex
104 #define yytable c_yytable
105 #define yycheck c_yycheck
107 #define yysslim c_yysslim
108 #define yyssp c_yyssp
109 #define yystacksize c_yystacksize
111 #define yyvsp c_yyvsp
114 #define YYDEBUG 1 /* Default to yydebug support */
117 #define YYFPRINTF parser_fprintf
121 static int yylex (void);
123 void yyerror (char *);
127 /* Although the yacc "value" of an expression is not used,
128 since the result is stored in the structure being created,
129 other node types do have values. */
145 } typed_val_decfloat;
149 struct typed_stoken tsval;
151 struct symtoken ssym;
154 enum exp_opcode opcode;
155 struct internalvar *ivar;
157 struct stoken_vector svec;
163 /* YYSTYPE gets defined by %union */
164 static int parse_number (char *, int, int, YYSTYPE *);
165 static struct stoken operator_stoken (const char *);
168 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
170 %type <tval> type typebase
171 %type <tvec> nonempty_typelist
172 /* %type <bval> block */
174 /* Fancy type parsing. */
175 %type <voidval> func_mod direct_abs_decl abs_decl ptr_operator
177 %type <lval> array_mod
178 %type <tval> conversion_type_id
180 %token <typed_val_int> INT
181 %token <typed_val_float> FLOAT
182 %token <typed_val_decfloat> DECFLOAT
184 /* Both NAME and TYPENAME tokens represent symbols in the input,
185 and both convey their data as strings.
186 But a TYPENAME is a string that happens to be defined as a typedef
187 or builtin type name (such as int or char)
188 and a NAME is any other symbol.
189 Contexts where this distinction is not important can use the
190 nonterminal "name", which matches either NAME or TYPENAME. */
192 %token <tsval> STRING
194 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
195 %token <ssym> UNKNOWN_CPP_NAME
196 %token <voidval> COMPLETE
197 %token <tsym> TYPENAME
199 %type <svec> string_exp
200 %type <ssym> name_not_typename
201 %type <tsym> typename
203 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
204 but which would parse as a valid number in the current input radix.
205 E.g. "c" when input_radix==16. Depending on the parse, it will be
206 turned into a name or into a number. */
208 %token <ssym> NAME_OR_INT
211 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
215 %type <sval> operator
216 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
219 /* Special type cases, put in to allow the parser to distinguish different
221 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
223 %token <sval> VARIABLE
225 %token <opcode> ASSIGN_MODIFY
234 %right '=' ASSIGN_MODIFY
242 %left '<' '>' LEQ GEQ
247 %right UNARY INCREMENT DECREMENT
248 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
249 %token <ssym> BLOCKNAME
250 %token <bval> FILENAME
262 { write_exp_elt_opcode(OP_TYPE);
263 write_exp_elt_type($1);
264 write_exp_elt_opcode(OP_TYPE);}
267 /* Expressions, including the comma operator. */
270 { write_exp_elt_opcode (BINOP_COMMA); }
273 /* Expressions, not including the comma operator. */
274 exp : '*' exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_IND); }
278 exp : '&' exp %prec UNARY
279 { write_exp_elt_opcode (UNOP_ADDR); }
282 exp : '-' exp %prec UNARY
283 { write_exp_elt_opcode (UNOP_NEG); }
286 exp : '+' exp %prec UNARY
287 { write_exp_elt_opcode (UNOP_PLUS); }
290 exp : '!' exp %prec UNARY
291 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
294 exp : '~' exp %prec UNARY
295 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
298 exp : INCREMENT exp %prec UNARY
299 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
302 exp : DECREMENT exp %prec UNARY
303 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
306 exp : exp INCREMENT %prec UNARY
307 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
310 exp : exp DECREMENT %prec UNARY
311 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
314 exp : SIZEOF exp %prec UNARY
315 { write_exp_elt_opcode (UNOP_SIZEOF); }
319 { write_exp_elt_opcode (STRUCTOP_PTR);
320 write_exp_string ($3);
321 write_exp_elt_opcode (STRUCTOP_PTR); }
324 exp : exp ARROW name COMPLETE
325 { mark_struct_expression ();
326 write_exp_elt_opcode (STRUCTOP_PTR);
327 write_exp_string ($3);
328 write_exp_elt_opcode (STRUCTOP_PTR); }
331 exp : exp ARROW COMPLETE
333 mark_struct_expression ();
334 write_exp_elt_opcode (STRUCTOP_PTR);
337 write_exp_string (s);
338 write_exp_elt_opcode (STRUCTOP_PTR); }
341 exp : exp ARROW qualified_name
342 { /* exp->type::name becomes exp->*(&type::name) */
343 /* Note: this doesn't work if name is a
344 static member! FIXME */
345 write_exp_elt_opcode (UNOP_ADDR);
346 write_exp_elt_opcode (STRUCTOP_MPTR); }
349 exp : exp ARROW_STAR exp
350 { write_exp_elt_opcode (STRUCTOP_MPTR); }
354 { write_exp_elt_opcode (STRUCTOP_STRUCT);
355 write_exp_string ($3);
356 write_exp_elt_opcode (STRUCTOP_STRUCT); }
359 exp : exp '.' name COMPLETE
360 { mark_struct_expression ();
361 write_exp_elt_opcode (STRUCTOP_STRUCT);
362 write_exp_string ($3);
363 write_exp_elt_opcode (STRUCTOP_STRUCT); }
366 exp : exp '.' COMPLETE
368 mark_struct_expression ();
369 write_exp_elt_opcode (STRUCTOP_STRUCT);
372 write_exp_string (s);
373 write_exp_elt_opcode (STRUCTOP_STRUCT); }
376 exp : exp '.' qualified_name
377 { /* exp.type::name becomes exp.*(&type::name) */
378 /* Note: this doesn't work if name is a
379 static member! FIXME */
380 write_exp_elt_opcode (UNOP_ADDR);
381 write_exp_elt_opcode (STRUCTOP_MEMBER); }
384 exp : exp DOT_STAR exp
385 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
388 exp : exp '[' exp1 ']'
389 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
393 /* This is to save the value of arglist_len
394 being accumulated by an outer function call. */
395 { start_arglist (); }
396 arglist ')' %prec ARROW
397 { write_exp_elt_opcode (OP_FUNCALL);
398 write_exp_elt_longcst ((LONGEST) end_arglist ());
399 write_exp_elt_opcode (OP_FUNCALL); }
402 exp : UNKNOWN_CPP_NAME '('
404 /* This could potentially be a an argument defined
405 lookup function (Koenig). */
406 write_exp_elt_opcode (OP_ADL_FUNC);
407 write_exp_elt_block (expression_context_block);
408 write_exp_elt_sym (NULL); /* Placeholder. */
409 write_exp_string ($1.stoken);
410 write_exp_elt_opcode (OP_ADL_FUNC);
412 /* This is to save the value of arglist_len
413 being accumulated by an outer function call. */
417 arglist ')' %prec ARROW
419 write_exp_elt_opcode (OP_FUNCALL);
420 write_exp_elt_longcst ((LONGEST) end_arglist ());
421 write_exp_elt_opcode (OP_FUNCALL);
426 { start_arglist (); }
436 arglist : arglist ',' exp %prec ABOVE_COMMA
440 exp : exp '(' nonempty_typelist ')' const_or_volatile
442 write_exp_elt_opcode (TYPE_INSTANCE);
443 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
444 for (i = 0; i < $<ivec>3[0]; ++i)
445 write_exp_elt_type ($<tvec>3[i + 1]);
446 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
447 write_exp_elt_opcode (TYPE_INSTANCE);
453 { $$ = end_arglist () - 1; }
455 exp : lcurly arglist rcurly %prec ARROW
456 { write_exp_elt_opcode (OP_ARRAY);
457 write_exp_elt_longcst ((LONGEST) 0);
458 write_exp_elt_longcst ((LONGEST) $3);
459 write_exp_elt_opcode (OP_ARRAY); }
462 exp : lcurly type rcurly exp %prec UNARY
463 { write_exp_elt_opcode (UNOP_MEMVAL);
464 write_exp_elt_type ($2);
465 write_exp_elt_opcode (UNOP_MEMVAL); }
468 exp : '(' type ')' exp %prec UNARY
469 { write_exp_elt_opcode (UNOP_CAST);
470 write_exp_elt_type ($2);
471 write_exp_elt_opcode (UNOP_CAST); }
478 /* Binary operators in order of decreasing precedence. */
481 { write_exp_elt_opcode (BINOP_REPEAT); }
485 { write_exp_elt_opcode (BINOP_MUL); }
489 { write_exp_elt_opcode (BINOP_DIV); }
493 { write_exp_elt_opcode (BINOP_REM); }
497 { write_exp_elt_opcode (BINOP_ADD); }
501 { write_exp_elt_opcode (BINOP_SUB); }
505 { write_exp_elt_opcode (BINOP_LSH); }
509 { write_exp_elt_opcode (BINOP_RSH); }
513 { write_exp_elt_opcode (BINOP_EQUAL); }
516 exp : exp NOTEQUAL exp
517 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
521 { write_exp_elt_opcode (BINOP_LEQ); }
525 { write_exp_elt_opcode (BINOP_GEQ); }
529 { write_exp_elt_opcode (BINOP_LESS); }
533 { write_exp_elt_opcode (BINOP_GTR); }
537 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
541 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
545 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
549 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
553 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
556 exp : exp '?' exp ':' exp %prec '?'
557 { write_exp_elt_opcode (TERNOP_COND); }
561 { write_exp_elt_opcode (BINOP_ASSIGN); }
564 exp : exp ASSIGN_MODIFY exp
565 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
566 write_exp_elt_opcode ($2);
567 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
571 { write_exp_elt_opcode (OP_LONG);
572 write_exp_elt_type ($1.type);
573 write_exp_elt_longcst ((LONGEST)($1.val));
574 write_exp_elt_opcode (OP_LONG); }
579 struct stoken_vector vec;
582 write_exp_string_vector ($1.type, &vec);
588 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
589 write_exp_elt_opcode (OP_LONG);
590 write_exp_elt_type (val.typed_val_int.type);
591 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
592 write_exp_elt_opcode (OP_LONG);
598 { write_exp_elt_opcode (OP_DOUBLE);
599 write_exp_elt_type ($1.type);
600 write_exp_elt_dblcst ($1.dval);
601 write_exp_elt_opcode (OP_DOUBLE); }
605 { write_exp_elt_opcode (OP_DECFLOAT);
606 write_exp_elt_type ($1.type);
607 write_exp_elt_decfloatcst ($1.val);
608 write_exp_elt_opcode (OP_DECFLOAT); }
616 write_dollar_variable ($1);
620 exp : SIZEOF '(' type ')' %prec UNARY
621 { write_exp_elt_opcode (OP_LONG);
622 write_exp_elt_type (lookup_signed_typename
623 (parse_language, parse_gdbarch,
626 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
627 write_exp_elt_opcode (OP_LONG); }
630 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
631 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
632 write_exp_elt_type ($3);
633 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
636 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
637 { write_exp_elt_opcode (UNOP_CAST);
638 write_exp_elt_type ($3);
639 write_exp_elt_opcode (UNOP_CAST); }
642 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
643 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
644 write_exp_elt_type ($3);
645 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
648 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
649 { /* We could do more error checking here, but
650 it doesn't seem worthwhile. */
651 write_exp_elt_opcode (UNOP_CAST);
652 write_exp_elt_type ($3);
653 write_exp_elt_opcode (UNOP_CAST); }
659 /* We copy the string here, and not in the
660 lexer, to guarantee that we do not leak a
661 string. Note that we follow the
662 NUL-termination convention of the
664 struct typed_stoken *vec = XNEW (struct typed_stoken);
669 vec->length = $1.length;
670 vec->ptr = malloc ($1.length + 1);
671 memcpy (vec->ptr, $1.ptr, $1.length + 1);
676 /* Note that we NUL-terminate here, but just
680 $$.tokens = realloc ($$.tokens,
681 $$.len * sizeof (struct typed_stoken));
683 p = malloc ($2.length + 1);
684 memcpy (p, $2.ptr, $2.length + 1);
686 $$.tokens[$$.len - 1].type = $2.type;
687 $$.tokens[$$.len - 1].length = $2.length;
688 $$.tokens[$$.len - 1].ptr = p;
695 enum c_string_type type = C_STRING;
697 for (i = 0; i < $1.len; ++i)
699 switch ($1.tokens[i].type)
707 && type != $1.tokens[i].type)
708 error (_("Undefined string concatenation."));
709 type = $1.tokens[i].type;
713 internal_error (__FILE__, __LINE__,
714 "unrecognized type in string concatenation");
718 write_exp_string_vector (type, &$1);
719 for (i = 0; i < $1.len; ++i)
720 free ($1.tokens[i].ptr);
727 { write_exp_elt_opcode (OP_LONG);
728 write_exp_elt_type (parse_type->builtin_bool);
729 write_exp_elt_longcst ((LONGEST) 1);
730 write_exp_elt_opcode (OP_LONG); }
734 { write_exp_elt_opcode (OP_LONG);
735 write_exp_elt_type (parse_type->builtin_bool);
736 write_exp_elt_longcst ((LONGEST) 0);
737 write_exp_elt_opcode (OP_LONG); }
745 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
747 error (_("No file or function \"%s\"."),
748 copy_name ($1.stoken));
756 block : block COLONCOLON name
758 = lookup_symbol (copy_name ($3), $1,
759 VAR_DOMAIN, (int *) NULL);
760 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
761 error (_("No function \"%s\" in specified context."),
763 $$ = SYMBOL_BLOCK_VALUE (tem); }
766 variable: name_not_typename ENTRY
767 { struct symbol *sym = $1.sym;
769 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
770 || !symbol_read_needs_frame (sym))
771 error (_("@entry can be used only for function "
772 "parameters, not for \"%s\""),
773 copy_name ($1.stoken));
775 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
776 write_exp_elt_sym (sym);
777 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
781 variable: block COLONCOLON name
782 { struct symbol *sym;
783 sym = lookup_symbol (copy_name ($3), $1,
784 VAR_DOMAIN, (int *) NULL);
786 error (_("No symbol \"%s\" in specified context."),
788 if (symbol_read_needs_frame (sym))
790 if (innermost_block == 0
791 || contained_in (block_found,
793 innermost_block = block_found;
796 write_exp_elt_opcode (OP_VAR_VALUE);
797 /* block_found is set by lookup_symbol. */
798 write_exp_elt_block (block_found);
799 write_exp_elt_sym (sym);
800 write_exp_elt_opcode (OP_VAR_VALUE); }
803 qualified_name: TYPENAME COLONCOLON name
805 struct type *type = $1.type;
806 CHECK_TYPEDEF (type);
807 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
808 && TYPE_CODE (type) != TYPE_CODE_UNION
809 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
810 error (_("`%s' is not defined as an aggregate type."),
813 write_exp_elt_opcode (OP_SCOPE);
814 write_exp_elt_type (type);
815 write_exp_string ($3);
816 write_exp_elt_opcode (OP_SCOPE);
818 | TYPENAME COLONCOLON '~' name
820 struct type *type = $1.type;
821 struct stoken tmp_token;
822 CHECK_TYPEDEF (type);
823 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
824 && TYPE_CODE (type) != TYPE_CODE_UNION
825 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
826 error (_("`%s' is not defined as an aggregate type."),
829 tmp_token.ptr = (char*) alloca ($4.length + 2);
830 tmp_token.length = $4.length + 1;
831 tmp_token.ptr[0] = '~';
832 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
833 tmp_token.ptr[tmp_token.length] = 0;
835 /* Check for valid destructor name. */
836 destructor_name_p (tmp_token.ptr, $1.type);
837 write_exp_elt_opcode (OP_SCOPE);
838 write_exp_elt_type (type);
839 write_exp_string (tmp_token);
840 write_exp_elt_opcode (OP_SCOPE);
842 | TYPENAME COLONCOLON name COLONCOLON name
844 char *copy = copy_name ($3);
845 error (_("No type \"%s\" within class "
846 "or namespace \"%s\"."),
847 copy, TYPE_NAME ($1.type));
851 variable: qualified_name
852 | COLONCOLON name_not_typename
854 char *name = copy_name ($2.stoken);
856 struct minimal_symbol *msymbol;
859 lookup_symbol (name, (const struct block *) NULL,
860 VAR_DOMAIN, (int *) NULL);
863 write_exp_elt_opcode (OP_VAR_VALUE);
864 write_exp_elt_block (NULL);
865 write_exp_elt_sym (sym);
866 write_exp_elt_opcode (OP_VAR_VALUE);
870 msymbol = lookup_minimal_symbol (name, NULL, NULL);
872 write_exp_msymbol (msymbol);
873 else if (!have_full_symbols () && !have_partial_symbols ())
874 error (_("No symbol table is loaded. Use the \"file\" command."));
876 error (_("No symbol \"%s\" in current context."), name);
880 variable: name_not_typename
881 { struct symbol *sym = $1.sym;
885 if (symbol_read_needs_frame (sym))
887 if (innermost_block == 0
888 || contained_in (block_found,
890 innermost_block = block_found;
893 write_exp_elt_opcode (OP_VAR_VALUE);
894 /* We want to use the selected frame, not
895 another more inner frame which happens to
896 be in the same block. */
897 write_exp_elt_block (NULL);
898 write_exp_elt_sym (sym);
899 write_exp_elt_opcode (OP_VAR_VALUE);
901 else if ($1.is_a_field_of_this)
903 /* C++: it hangs off of `this'. Must
904 not inadvertently convert from a method call
906 if (innermost_block == 0
907 || contained_in (block_found,
909 innermost_block = block_found;
910 write_exp_elt_opcode (OP_THIS);
911 write_exp_elt_opcode (OP_THIS);
912 write_exp_elt_opcode (STRUCTOP_PTR);
913 write_exp_string ($1.stoken);
914 write_exp_elt_opcode (STRUCTOP_PTR);
918 struct minimal_symbol *msymbol;
919 char *arg = copy_name ($1.stoken);
922 lookup_minimal_symbol (arg, NULL, NULL);
924 write_exp_msymbol (msymbol);
925 else if (!have_full_symbols () && !have_partial_symbols ())
926 error (_("No symbol table is loaded. Use the \"file\" command."));
928 error (_("No symbol \"%s\" in current context."),
929 copy_name ($1.stoken));
934 space_identifier : '@' NAME
935 { insert_type_address_space (copy_name ($2.stoken)); }
938 const_or_volatile: const_or_volatile_noopt
942 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
945 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
946 | const_or_volatile_noopt
949 const_or_volatile_or_space_identifier:
950 const_or_volatile_or_space_identifier_noopt
956 { insert_type (tp_pointer); }
957 const_or_volatile_or_space_identifier
960 { insert_type (tp_pointer); }
961 const_or_volatile_or_space_identifier
964 { insert_type (tp_reference); $$ = 0; }
966 { insert_type (tp_reference); $$ = 0; }
969 abs_decl: ptr_operator direct_abs_decl
974 direct_abs_decl: '(' abs_decl ')'
976 | direct_abs_decl array_mod
979 push_type (tp_array);
984 push_type (tp_array);
988 | direct_abs_decl func_mod
989 { push_type (tp_function); }
991 { push_type (tp_function); }
1002 | '(' nonempty_typelist ')'
1003 { free ($2); $$ = 0; }
1006 /* We used to try to recognize pointer to member types here, but
1007 that didn't work (shift/reduce conflicts meant that these rules never
1008 got executed). The problem is that
1009 int (foo::bar::baz::bizzle)
1010 is a function type but
1011 int (foo::bar::baz::bizzle::*)
1012 is a pointer to member type. Stroustrup loses again! */
1017 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1021 { $$ = lookup_signed_typename (parse_language,
1025 { $$ = lookup_signed_typename (parse_language,
1029 { $$ = lookup_signed_typename (parse_language,
1033 { $$ = lookup_signed_typename (parse_language,
1036 | LONG SIGNED_KEYWORD INT_KEYWORD
1037 { $$ = lookup_signed_typename (parse_language,
1040 | LONG SIGNED_KEYWORD
1041 { $$ = lookup_signed_typename (parse_language,
1044 | SIGNED_KEYWORD LONG INT_KEYWORD
1045 { $$ = lookup_signed_typename (parse_language,
1048 | UNSIGNED LONG INT_KEYWORD
1049 { $$ = lookup_unsigned_typename (parse_language,
1052 | LONG UNSIGNED INT_KEYWORD
1053 { $$ = lookup_unsigned_typename (parse_language,
1057 { $$ = lookup_unsigned_typename (parse_language,
1061 { $$ = lookup_signed_typename (parse_language,
1064 | LONG LONG INT_KEYWORD
1065 { $$ = lookup_signed_typename (parse_language,
1068 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1069 { $$ = lookup_signed_typename (parse_language,
1072 | LONG LONG SIGNED_KEYWORD
1073 { $$ = lookup_signed_typename (parse_language,
1076 | SIGNED_KEYWORD LONG LONG
1077 { $$ = lookup_signed_typename (parse_language,
1080 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1081 { $$ = lookup_signed_typename (parse_language,
1084 | UNSIGNED LONG LONG
1085 { $$ = lookup_unsigned_typename (parse_language,
1088 | UNSIGNED LONG LONG INT_KEYWORD
1089 { $$ = lookup_unsigned_typename (parse_language,
1092 | LONG LONG UNSIGNED
1093 { $$ = lookup_unsigned_typename (parse_language,
1096 | LONG LONG UNSIGNED INT_KEYWORD
1097 { $$ = lookup_unsigned_typename (parse_language,
1101 { $$ = lookup_signed_typename (parse_language,
1104 | SHORT SIGNED_KEYWORD INT_KEYWORD
1105 { $$ = lookup_signed_typename (parse_language,
1108 | SHORT SIGNED_KEYWORD
1109 { $$ = lookup_signed_typename (parse_language,
1112 | UNSIGNED SHORT INT_KEYWORD
1113 { $$ = lookup_unsigned_typename (parse_language,
1117 { $$ = lookup_unsigned_typename (parse_language,
1120 | SHORT UNSIGNED INT_KEYWORD
1121 { $$ = lookup_unsigned_typename (parse_language,
1125 { $$ = lookup_typename (parse_language, parse_gdbarch,
1126 "double", (struct block *) NULL,
1128 | LONG DOUBLE_KEYWORD
1129 { $$ = lookup_typename (parse_language, parse_gdbarch,
1131 (struct block *) NULL, 0); }
1133 { $$ = lookup_struct (copy_name ($2),
1134 expression_context_block); }
1136 { $$ = lookup_struct (copy_name ($2),
1137 expression_context_block); }
1139 { $$ = lookup_union (copy_name ($2),
1140 expression_context_block); }
1142 { $$ = lookup_enum (copy_name ($2),
1143 expression_context_block); }
1145 { $$ = lookup_unsigned_typename (parse_language,
1147 TYPE_NAME($2.type)); }
1149 { $$ = lookup_unsigned_typename (parse_language,
1152 | SIGNED_KEYWORD typename
1153 { $$ = lookup_signed_typename (parse_language,
1155 TYPE_NAME($2.type)); }
1157 { $$ = lookup_signed_typename (parse_language,
1160 /* It appears that this rule for templates is never
1161 reduced; template recognition happens by lookahead
1162 in the token processing code in yylex. */
1163 | TEMPLATE name '<' type '>'
1164 { $$ = lookup_template_type(copy_name($2), $4,
1165 expression_context_block);
1167 | const_or_volatile_or_space_identifier_noopt typebase
1168 { $$ = follow_types ($2); }
1169 | typebase const_or_volatile_or_space_identifier_noopt
1170 { $$ = follow_types ($1); }
1176 $$.stoken.ptr = "int";
1177 $$.stoken.length = 3;
1178 $$.type = lookup_signed_typename (parse_language,
1184 $$.stoken.ptr = "long";
1185 $$.stoken.length = 4;
1186 $$.type = lookup_signed_typename (parse_language,
1192 $$.stoken.ptr = "short";
1193 $$.stoken.length = 5;
1194 $$.type = lookup_signed_typename (parse_language,
1202 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1203 $<ivec>$[0] = 1; /* Number of types in vector */
1206 | nonempty_typelist ',' type
1207 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1208 $$ = (struct type **) realloc ((char *) $1, len);
1209 $$[$<ivec>$[0]] = $3;
1215 { $$ = follow_types ($1); }
1218 conversion_type_id: typebase conversion_declarator
1219 { $$ = follow_types ($1); }
1222 conversion_declarator: /* Nothing. */
1223 | ptr_operator conversion_declarator
1226 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1227 | VOLATILE_KEYWORD CONST_KEYWORD
1230 const_or_volatile_noopt: const_and_volatile
1231 { insert_type (tp_const);
1232 insert_type (tp_volatile);
1235 { insert_type (tp_const); }
1237 { insert_type (tp_volatile); }
1240 operator: OPERATOR NEW
1241 { $$ = operator_stoken (" new"); }
1243 { $$ = operator_stoken (" delete"); }
1244 | OPERATOR NEW '[' ']'
1245 { $$ = operator_stoken (" new[]"); }
1246 | OPERATOR DELETE '[' ']'
1247 { $$ = operator_stoken (" delete[]"); }
1249 { $$ = operator_stoken ("+"); }
1251 { $$ = operator_stoken ("-"); }
1253 { $$ = operator_stoken ("*"); }
1255 { $$ = operator_stoken ("/"); }
1257 { $$ = operator_stoken ("%"); }
1259 { $$ = operator_stoken ("^"); }
1261 { $$ = operator_stoken ("&"); }
1263 { $$ = operator_stoken ("|"); }
1265 { $$ = operator_stoken ("~"); }
1267 { $$ = operator_stoken ("!"); }
1269 { $$ = operator_stoken ("="); }
1271 { $$ = operator_stoken ("<"); }
1273 { $$ = operator_stoken (">"); }
1274 | OPERATOR ASSIGN_MODIFY
1275 { const char *op = "unknown";
1299 case BINOP_BITWISE_IOR:
1302 case BINOP_BITWISE_AND:
1305 case BINOP_BITWISE_XOR:
1312 $$ = operator_stoken (op);
1315 { $$ = operator_stoken ("<<"); }
1317 { $$ = operator_stoken (">>"); }
1319 { $$ = operator_stoken ("=="); }
1321 { $$ = operator_stoken ("!="); }
1323 { $$ = operator_stoken ("<="); }
1325 { $$ = operator_stoken (">="); }
1327 { $$ = operator_stoken ("&&"); }
1329 { $$ = operator_stoken ("||"); }
1330 | OPERATOR INCREMENT
1331 { $$ = operator_stoken ("++"); }
1332 | OPERATOR DECREMENT
1333 { $$ = operator_stoken ("--"); }
1335 { $$ = operator_stoken (","); }
1336 | OPERATOR ARROW_STAR
1337 { $$ = operator_stoken ("->*"); }
1339 { $$ = operator_stoken ("->"); }
1341 { $$ = operator_stoken ("()"); }
1343 { $$ = operator_stoken ("[]"); }
1344 | OPERATOR conversion_type_id
1347 struct ui_file *buf = mem_fileopen ();
1349 c_print_type ($2, NULL, buf, -1, 0);
1350 name = ui_file_xstrdup (buf, &length);
1351 ui_file_delete (buf);
1352 $$ = operator_stoken (name);
1359 name : NAME { $$ = $1.stoken; }
1360 | BLOCKNAME { $$ = $1.stoken; }
1361 | TYPENAME { $$ = $1.stoken; }
1362 | NAME_OR_INT { $$ = $1.stoken; }
1363 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1364 | operator { $$ = $1; }
1367 name_not_typename : NAME
1369 /* These would be useful if name_not_typename was useful, but it is just
1370 a fake for "variable", so these cause reduce/reduce conflicts because
1371 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1372 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1373 context where only a name could occur, this might be useful.
1379 $$.sym = lookup_symbol ($1.ptr,
1380 expression_context_block,
1382 &$$.is_a_field_of_this);
1389 /* Returns a stoken of the operator name given by OP (which does not
1390 include the string "operator"). */
1391 static struct stoken
1392 operator_stoken (const char *op)
1394 static const char *operator_string = "operator";
1395 struct stoken st = { NULL, 0 };
1396 st.length = strlen (operator_string) + strlen (op);
1397 st.ptr = malloc (st.length + 1);
1398 strcpy (st.ptr, operator_string);
1399 strcat (st.ptr, op);
1401 /* The toplevel (c_parse) will free the memory allocated here. */
1402 make_cleanup (free, st.ptr);
1406 /* Take care of parsing a number (anything that starts with a digit).
1407 Set yylval and return the token type; update lexptr.
1408 LEN is the number of characters in it. */
1410 /*** Needs some error checking for the float case ***/
1413 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1415 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1416 here, and we do kind of silly things like cast to unsigned. */
1423 int base = input_radix;
1426 /* Number of "L" suffixes encountered. */
1429 /* We have found a "L" or "U" suffix. */
1430 int found_suffix = 0;
1433 struct type *signed_type;
1434 struct type *unsigned_type;
1438 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1439 point. Return DECFLOAT. */
1441 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1444 putithere->typed_val_decfloat.type
1445 = parse_type->builtin_decfloat;
1446 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1447 gdbarch_byte_order (parse_gdbarch), p);
1452 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1455 putithere->typed_val_decfloat.type
1456 = parse_type->builtin_decdouble;
1457 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1458 gdbarch_byte_order (parse_gdbarch), p);
1463 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1466 putithere->typed_val_decfloat.type
1467 = parse_type->builtin_declong;
1468 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1469 gdbarch_byte_order (parse_gdbarch), p);
1474 if (! parse_c_float (parse_gdbarch, p, len,
1475 &putithere->typed_val_float.dval,
1476 &putithere->typed_val_float.type))
1481 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1525 if (c >= 'A' && c <= 'Z')
1527 if (c != 'l' && c != 'u')
1529 if (c >= '0' && c <= '9')
1537 if (base > 10 && c >= 'a' && c <= 'f')
1541 n += i = c - 'a' + 10;
1554 return ERROR; /* Char not a digit */
1557 return ERROR; /* Invalid digit in this base */
1559 /* Portably test for overflow (only works for nonzero values, so make
1560 a second check for zero). FIXME: Can't we just make n and prevn
1561 unsigned and avoid this? */
1562 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1563 unsigned_p = 1; /* Try something unsigned */
1565 /* Portably test for unsigned overflow.
1566 FIXME: This check is wrong; for example it doesn't find overflow
1567 on 0x123456789 when LONGEST is 32 bits. */
1568 if (c != 'l' && c != 'u' && n != 0)
1570 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1571 error (_("Numeric constant too large."));
1576 /* An integer constant is an int, a long, or a long long. An L
1577 suffix forces it to be long; an LL suffix forces it to be long
1578 long. If not forced to a larger size, it gets the first type of
1579 the above that it fits in. To figure out whether it fits, we
1580 shift it right and see whether anything remains. Note that we
1581 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1582 operation, because many compilers will warn about such a shift
1583 (which always produces a zero result). Sometimes gdbarch_int_bit
1584 or gdbarch_long_bit will be that big, sometimes not. To deal with
1585 the case where it is we just always shift the value more than
1586 once, with fewer bits each time. */
1588 un = (ULONGEST)n >> 2;
1590 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1592 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1594 /* A large decimal (not hex or octal) constant (between INT_MAX
1595 and UINT_MAX) is a long or unsigned long, according to ANSI,
1596 never an unsigned int, but this code treats it as unsigned
1597 int. This probably should be fixed. GCC gives a warning on
1600 unsigned_type = parse_type->builtin_unsigned_int;
1601 signed_type = parse_type->builtin_int;
1603 else if (long_p <= 1
1604 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1606 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1607 unsigned_type = parse_type->builtin_unsigned_long;
1608 signed_type = parse_type->builtin_long;
1613 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1614 < gdbarch_long_long_bit (parse_gdbarch))
1615 /* A long long does not fit in a LONGEST. */
1616 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1618 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1619 high_bit = (ULONGEST) 1 << shift;
1620 unsigned_type = parse_type->builtin_unsigned_long_long;
1621 signed_type = parse_type->builtin_long_long;
1624 putithere->typed_val_int.val = n;
1626 /* If the high bit of the worked out type is set then this number
1627 has to be unsigned. */
1629 if (unsigned_p || (n & high_bit))
1631 putithere->typed_val_int.type = unsigned_type;
1635 putithere->typed_val_int.type = signed_type;
1641 /* Temporary obstack used for holding strings. */
1642 static struct obstack tempbuf;
1643 static int tempbuf_init;
1645 /* Parse a C escape sequence. The initial backslash of the sequence
1646 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1647 last character of the sequence. If OUTPUT is not NULL, the
1648 translated form of the escape sequence will be written there. If
1649 OUTPUT is NULL, no output is written and the call will only affect
1650 *PTR. If an escape sequence is expressed in target bytes, then the
1651 entire sequence will simply be copied to OUTPUT. Return 1 if any
1652 character was emitted, 0 otherwise. */
1655 c_parse_escape (char **ptr, struct obstack *output)
1657 char *tokptr = *ptr;
1660 /* Some escape sequences undergo character set conversion. Those we
1664 /* Hex escapes do not undergo character set conversion, so keep
1665 the escape sequence for later. */
1668 obstack_grow_str (output, "\\x");
1670 if (!isxdigit (*tokptr))
1671 error (_("\\x escape without a following hex digit"));
1672 while (isxdigit (*tokptr))
1675 obstack_1grow (output, *tokptr);
1680 /* Octal escapes do not undergo character set conversion, so
1681 keep the escape sequence for later. */
1693 obstack_grow_str (output, "\\");
1695 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1699 obstack_1grow (output, *tokptr);
1705 /* We handle UCNs later. We could handle them here, but that
1706 would mean a spurious error in the case where the UCN could
1707 be converted to the target charset but not the host
1713 int i, len = c == 'U' ? 8 : 4;
1716 obstack_1grow (output, '\\');
1717 obstack_1grow (output, *tokptr);
1720 if (!isxdigit (*tokptr))
1721 error (_("\\%c escape without a following hex digit"), c);
1722 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1725 obstack_1grow (output, *tokptr);
1731 /* We must pass backslash through so that it does not
1732 cause quoting during the second expansion. */
1735 obstack_grow_str (output, "\\\\");
1739 /* Escapes which undergo conversion. */
1742 obstack_1grow (output, '\a');
1747 obstack_1grow (output, '\b');
1752 obstack_1grow (output, '\f');
1757 obstack_1grow (output, '\n');
1762 obstack_1grow (output, '\r');
1767 obstack_1grow (output, '\t');
1772 obstack_1grow (output, '\v');
1776 /* GCC extension. */
1779 obstack_1grow (output, HOST_ESCAPE_CHAR);
1783 /* Backslash-newline expands to nothing at all. */
1789 /* A few escapes just expand to the character itself. */
1793 /* GCC extensions. */
1798 /* Unrecognized escapes turn into the character itself. */
1801 obstack_1grow (output, *tokptr);
1809 /* Parse a string or character literal from TOKPTR. The string or
1810 character may be wide or unicode. *OUTPTR is set to just after the
1811 end of the literal in the input string. The resulting token is
1812 stored in VALUE. This returns a token value, either STRING or
1813 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1814 number of host characters in the literal. */
1816 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1820 enum c_string_type type;
1822 /* Build the gdb internal form of the input string in tempbuf. Note
1823 that the buffer is null byte terminated *only* for the
1824 convenience of debugging gdb itself and printing the buffer
1825 contents when the buffer contains no embedded nulls. Gdb does
1826 not depend upon the buffer being null byte terminated, it uses
1827 the length string instead. This allows gdb to handle C strings
1828 (as well as strings in other languages) with embedded null
1834 obstack_free (&tempbuf, NULL);
1835 obstack_init (&tempbuf);
1837 /* Record the string type. */
1840 type = C_WIDE_STRING;
1843 else if (*tokptr == 'u')
1848 else if (*tokptr == 'U')
1856 /* Skip the quote. */
1870 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1872 else if (c == quote)
1876 obstack_1grow (&tempbuf, c);
1878 /* FIXME: this does the wrong thing with multi-byte host
1879 characters. We could use mbrlen here, but that would
1880 make "set host-charset" a bit less useful. */
1885 if (*tokptr != quote)
1888 error (_("Unterminated string in expression."));
1890 error (_("Unmatched single quote."));
1895 value->ptr = obstack_base (&tempbuf);
1896 value->length = obstack_object_size (&tempbuf);
1900 return quote == '"' ? STRING : CHAR;
1907 enum exp_opcode opcode;
1911 static const struct token tokentab3[] =
1913 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1914 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1915 {"->*", ARROW_STAR, BINOP_END, 1}
1918 static const struct token tokentab2[] =
1920 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1921 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1922 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1923 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1924 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1925 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1926 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1927 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1928 {"++", INCREMENT, BINOP_END, 0},
1929 {"--", DECREMENT, BINOP_END, 0},
1930 {"->", ARROW, BINOP_END, 0},
1931 {"&&", ANDAND, BINOP_END, 0},
1932 {"||", OROR, BINOP_END, 0},
1933 /* "::" is *not* only C++: gdb overrides its meaning in several
1934 different ways, e.g., 'filename'::func, function::variable. */
1935 {"::", COLONCOLON, BINOP_END, 0},
1936 {"<<", LSH, BINOP_END, 0},
1937 {">>", RSH, BINOP_END, 0},
1938 {"==", EQUAL, BINOP_END, 0},
1939 {"!=", NOTEQUAL, BINOP_END, 0},
1940 {"<=", LEQ, BINOP_END, 0},
1941 {">=", GEQ, BINOP_END, 0},
1942 {".*", DOT_STAR, BINOP_END, 1}
1945 /* Identifier-like tokens. */
1946 static const struct token ident_tokens[] =
1948 {"unsigned", UNSIGNED, OP_NULL, 0},
1949 {"template", TEMPLATE, OP_NULL, 1},
1950 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1951 {"struct", STRUCT, OP_NULL, 0},
1952 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1953 {"sizeof", SIZEOF, OP_NULL, 0},
1954 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1955 {"false", FALSEKEYWORD, OP_NULL, 1},
1956 {"class", CLASS, OP_NULL, 1},
1957 {"union", UNION, OP_NULL, 0},
1958 {"short", SHORT, OP_NULL, 0},
1959 {"const", CONST_KEYWORD, OP_NULL, 0},
1960 {"enum", ENUM, OP_NULL, 0},
1961 {"long", LONG, OP_NULL, 0},
1962 {"true", TRUEKEYWORD, OP_NULL, 1},
1963 {"int", INT_KEYWORD, OP_NULL, 0},
1964 {"new", NEW, OP_NULL, 1},
1965 {"delete", DELETE, OP_NULL, 1},
1966 {"operator", OPERATOR, OP_NULL, 1},
1968 {"and", ANDAND, BINOP_END, 1},
1969 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1970 {"bitand", '&', OP_NULL, 1},
1971 {"bitor", '|', OP_NULL, 1},
1972 {"compl", '~', OP_NULL, 1},
1973 {"not", '!', OP_NULL, 1},
1974 {"not_eq", NOTEQUAL, BINOP_END, 1},
1975 {"or", OROR, BINOP_END, 1},
1976 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1977 {"xor", '^', OP_NULL, 1},
1978 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1980 {"const_cast", CONST_CAST, OP_NULL, 1 },
1981 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1982 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1983 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1986 /* When we find that lexptr (the global var defined in parse.c) is
1987 pointing at a macro invocation, we expand the invocation, and call
1988 scan_macro_expansion to save the old lexptr here and point lexptr
1989 into the expanded text. When we reach the end of that, we call
1990 end_macro_expansion to pop back to the value we saved here. The
1991 macro expansion code promises to return only fully-expanded text,
1992 so we don't need to "push" more than one level.
1994 This is disgusting, of course. It would be cleaner to do all macro
1995 expansion beforehand, and then hand that to lexptr. But we don't
1996 really know where the expression ends. Remember, in a command like
1998 (gdb) break *ADDRESS if CONDITION
2000 we evaluate ADDRESS in the scope of the current frame, but we
2001 evaluate CONDITION in the scope of the breakpoint's location. So
2002 it's simply wrong to try to macro-expand the whole thing at once. */
2003 static char *macro_original_text;
2005 /* We save all intermediate macro expansions on this obstack for the
2006 duration of a single parse. The expansion text may sometimes have
2007 to live past the end of the expansion, due to yacc lookahead.
2008 Rather than try to be clever about saving the data for a single
2009 token, we simply keep it all and delete it after parsing has
2011 static struct obstack expansion_obstack;
2014 scan_macro_expansion (char *expansion)
2018 /* We'd better not be trying to push the stack twice. */
2019 gdb_assert (! macro_original_text);
2021 /* Copy to the obstack, and then free the intermediate
2023 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2026 /* Save the old lexptr value, so we can return to it when we're done
2027 parsing the expanded text. */
2028 macro_original_text = lexptr;
2034 scanning_macro_expansion (void)
2036 return macro_original_text != 0;
2041 finished_macro_expansion (void)
2043 /* There'd better be something to pop back to. */
2044 gdb_assert (macro_original_text);
2046 /* Pop back to the original text. */
2047 lexptr = macro_original_text;
2048 macro_original_text = 0;
2053 scan_macro_cleanup (void *dummy)
2055 if (macro_original_text)
2056 finished_macro_expansion ();
2058 obstack_free (&expansion_obstack, NULL);
2061 /* Return true iff the token represents a C++ cast operator. */
2064 is_cast_operator (const char *token, int len)
2066 return (! strncmp (token, "dynamic_cast", len)
2067 || ! strncmp (token, "static_cast", len)
2068 || ! strncmp (token, "reinterpret_cast", len)
2069 || ! strncmp (token, "const_cast", len));
2072 /* The scope used for macro expansion. */
2073 static struct macro_scope *expression_macro_scope;
2075 /* This is set if a NAME token appeared at the very end of the input
2076 string, with no whitespace separating the name from the EOF. This
2077 is used only when parsing to do field name completion. */
2078 static int saw_name_at_eof;
2080 /* This is set if the previously-returned token was a structure
2081 operator -- either '.' or ARROW. This is used only when parsing to
2082 do field name completion. */
2083 static int last_was_structop;
2085 /* Read one token, getting characters through lexptr. */
2088 lex_one_token (void)
2094 int saw_structop = last_was_structop;
2097 last_was_structop = 0;
2101 /* Check if this is a macro invocation that we need to expand. */
2102 if (! scanning_macro_expansion ())
2104 char *expanded = macro_expand_next (&lexptr,
2105 standard_macro_lookup,
2106 expression_macro_scope);
2109 scan_macro_expansion (expanded);
2112 prev_lexptr = lexptr;
2115 /* See if it is a special token of length 3. */
2116 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2117 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2119 if (tokentab3[i].cxx_only
2120 && parse_language->la_language != language_cplus)
2124 yylval.opcode = tokentab3[i].opcode;
2125 return tokentab3[i].token;
2128 /* See if it is a special token of length 2. */
2129 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2130 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2132 if (tokentab2[i].cxx_only
2133 && parse_language->la_language != language_cplus)
2137 yylval.opcode = tokentab2[i].opcode;
2138 if (in_parse_field && tokentab2[i].token == ARROW)
2139 last_was_structop = 1;
2140 return tokentab2[i].token;
2143 switch (c = *tokstart)
2146 /* If we were just scanning the result of a macro expansion,
2147 then we need to resume scanning the original text.
2148 If we're parsing for field name completion, and the previous
2149 token allows such completion, return a COMPLETE token.
2150 Otherwise, we were already scanning the original text, and
2151 we're really done. */
2152 if (scanning_macro_expansion ())
2154 finished_macro_expansion ();
2157 else if (saw_name_at_eof)
2159 saw_name_at_eof = 0;
2162 else if (saw_structop)
2181 if (paren_depth == 0)
2188 if (comma_terminates
2190 && ! scanning_macro_expansion ())
2196 /* Might be a floating point number. */
2197 if (lexptr[1] < '0' || lexptr[1] > '9')
2200 last_was_structop = 1;
2201 goto symbol; /* Nope, must be a symbol. */
2203 /* FALL THRU into number case. */
2216 /* It's a number. */
2217 int got_dot = 0, got_e = 0, toktype;
2219 int hex = input_radix > 10;
2221 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2226 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2234 /* This test includes !hex because 'e' is a valid hex digit
2235 and thus does not indicate a floating point number when
2236 the radix is hex. */
2237 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2238 got_dot = got_e = 1;
2239 /* This test does not include !hex, because a '.' always indicates
2240 a decimal floating point number regardless of the radix. */
2241 else if (!got_dot && *p == '.')
2243 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2244 && (*p == '-' || *p == '+'))
2245 /* This is the sign of the exponent, not the end of the
2248 /* We will take any letters or digits. parse_number will
2249 complain if past the radix, or if L or U are not final. */
2250 else if ((*p < '0' || *p > '9')
2251 && ((*p < 'a' || *p > 'z')
2252 && (*p < 'A' || *p > 'Z')))
2255 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2256 if (toktype == ERROR)
2258 char *err_copy = (char *) alloca (p - tokstart + 1);
2260 memcpy (err_copy, tokstart, p - tokstart);
2261 err_copy[p - tokstart] = 0;
2262 error (_("Invalid number \"%s\"."), err_copy);
2270 char *p = &tokstart[1];
2271 size_t len = strlen ("entry");
2273 while (isspace (*p))
2275 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2307 if (tokstart[1] != '"' && tokstart[1] != '\'')
2314 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2319 error (_("Empty character constant."));
2320 else if (host_len > 2 && c == '\'')
2323 namelen = lexptr - tokstart - 1;
2326 else if (host_len > 1)
2327 error (_("Invalid character constant."));
2333 if (!(c == '_' || c == '$'
2334 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2335 /* We must have come across a bad character (e.g. ';'). */
2336 error (_("Invalid character '%c' in expression."), c);
2338 /* It's a name. See how long it is. */
2340 for (c = tokstart[namelen];
2341 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2342 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2344 /* Template parameter lists are part of the name.
2345 FIXME: This mishandles `print $a<4&&$a>3'. */
2349 if (! is_cast_operator (tokstart, namelen))
2351 /* Scan ahead to get rest of the template specification. Note
2352 that we look ahead only when the '<' adjoins non-whitespace
2353 characters; for comparison expressions, e.g. "a < b > c",
2354 there must be spaces before the '<', etc. */
2356 char * p = find_template_name_end (tokstart + namelen);
2358 namelen = p - tokstart;
2362 c = tokstart[++namelen];
2365 /* The token "if" terminates the expression and is NOT removed from
2366 the input stream. It doesn't count if it appears in the
2367 expansion of a macro. */
2369 && tokstart[0] == 'i'
2370 && tokstart[1] == 'f'
2371 && ! scanning_macro_expansion ())
2376 /* For the same reason (breakpoint conditions), "thread N"
2377 terminates the expression. "thread" could be an identifier, but
2378 an identifier is never followed by a number without intervening
2379 punctuation. "task" is similar. Handle abbreviations of these,
2380 similarly to breakpoint.c:find_condition_and_thread. */
2382 && (strncmp (tokstart, "thread", namelen) == 0
2383 || strncmp (tokstart, "task", namelen) == 0)
2384 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2385 && ! scanning_macro_expansion ())
2387 char *p = tokstart + namelen + 1;
2388 while (*p == ' ' || *p == '\t')
2390 if (*p >= '0' && *p <= '9')
2398 yylval.sval.ptr = tokstart;
2399 yylval.sval.length = namelen;
2401 /* Catch specific keywords. */
2402 copy = copy_name (yylval.sval);
2403 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2404 if (strcmp (copy, ident_tokens[i].operator) == 0)
2406 if (ident_tokens[i].cxx_only
2407 && parse_language->la_language != language_cplus)
2410 /* It is ok to always set this, even though we don't always
2411 strictly need to. */
2412 yylval.opcode = ident_tokens[i].opcode;
2413 return ident_tokens[i].token;
2416 if (*tokstart == '$')
2419 if (in_parse_field && *lexptr == '\0')
2420 saw_name_at_eof = 1;
2424 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2431 DEF_VEC_O (token_and_value);
2433 /* A FIFO of tokens that have been read but not yet returned to the
2435 static VEC (token_and_value) *token_fifo;
2437 /* Non-zero if the lexer should return tokens from the FIFO. */
2440 /* Temporary storage for c_lex; this holds symbol names as they are
2442 static struct obstack name_obstack;
2444 /* Classify a NAME token. The contents of the token are in `yylval'.
2445 Updates yylval and returns the new token type. BLOCK is the block
2446 in which lookups start; this can be NULL to mean the global
2449 classify_name (struct block *block)
2453 int is_a_field_of_this = 0;
2455 copy = copy_name (yylval.sval);
2457 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2458 parse_language->la_language == language_cplus
2459 ? &is_a_field_of_this : (int *) NULL);
2461 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2463 yylval.ssym.sym = sym;
2464 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2469 /* See if it's a file name. */
2470 struct symtab *symtab;
2472 symtab = lookup_symtab (copy);
2475 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2480 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2482 yylval.tsym.type = SYMBOL_TYPE (sym);
2487 = language_lookup_primitive_type_by_name (parse_language,
2488 parse_gdbarch, copy);
2489 if (yylval.tsym.type != NULL)
2492 /* Input names that aren't symbols but ARE valid hex numbers, when
2493 the input radix permits them, can be names or numbers depending
2494 on the parse. Note we support radixes > 16 here. */
2496 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2497 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2499 YYSTYPE newlval; /* Its value is ignored. */
2500 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2503 yylval.ssym.sym = sym;
2504 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2509 /* Any other kind of symbol */
2510 yylval.ssym.sym = sym;
2511 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2514 && parse_language->la_language == language_cplus
2515 && !is_a_field_of_this
2516 && !lookup_minimal_symbol (copy, NULL, NULL))
2517 return UNKNOWN_CPP_NAME;
2522 /* Like classify_name, but used by the inner loop of the lexer, when a
2523 name might have already been seen. FIRST_NAME is true if the token
2524 in `yylval' is the first component of a name, false otherwise. */
2527 classify_inner_name (struct block *block, int first_name)
2529 struct type *type, *new_type;
2533 return classify_name (block);
2535 type = check_typedef (yylval.tsym.type);
2536 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2537 && TYPE_CODE (type) != TYPE_CODE_UNION
2538 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2541 copy = copy_name (yylval.tsym.stoken);
2542 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2543 if (yylval.ssym.sym == NULL)
2546 switch (SYMBOL_CLASS (yylval.ssym.sym))
2553 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2557 yylval.ssym.is_a_field_of_this = 0;
2560 internal_error (__FILE__, __LINE__, _("not reached"));
2563 /* The outer level of a two-level lexer. This calls the inner lexer
2564 to return tokens. It then either returns these tokens, or
2565 aggregates them into a larger token. This lets us work around a
2566 problem in our parsing approach, where the parser could not
2567 distinguish between qualified names and qualified types at the
2570 This approach is still not ideal, because it mishandles template
2571 types. See the comment in lex_one_token for an example. However,
2572 this is still an improvement over the earlier approach, and will
2573 suffice until we move to better parsing technology. */
2577 token_and_value current;
2578 int first_was_coloncolon, last_was_coloncolon, first_iter;
2580 if (popping && !VEC_empty (token_and_value, token_fifo))
2582 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2583 VEC_ordered_remove (token_and_value, token_fifo, 0);
2589 current.token = lex_one_token ();
2590 if (current.token == NAME)
2591 current.token = classify_name (expression_context_block);
2592 if (parse_language->la_language != language_cplus
2593 || (current.token != TYPENAME && current.token != COLONCOLON))
2594 return current.token;
2596 first_was_coloncolon = current.token == COLONCOLON;
2597 last_was_coloncolon = first_was_coloncolon;
2598 obstack_free (&name_obstack, obstack_base (&name_obstack));
2599 if (!last_was_coloncolon)
2600 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2601 current.value = yylval;
2605 token_and_value next;
2607 next.token = lex_one_token ();
2608 next.value = yylval;
2610 if (next.token == NAME && last_was_coloncolon)
2614 classification = classify_inner_name (first_was_coloncolon
2616 : expression_context_block,
2618 /* We keep going until we either run out of names, or until
2619 we have a qualified name which is not a type. */
2620 if (classification != TYPENAME && classification != NAME)
2622 /* Push the final component and leave the loop. */
2623 VEC_safe_push (token_and_value, token_fifo, &next);
2627 /* Update the partial name we are constructing. */
2630 /* We don't want to put a leading "::" into the name. */
2631 obstack_grow_str (&name_obstack, "::");
2633 obstack_grow (&name_obstack, next.value.sval.ptr,
2634 next.value.sval.length);
2636 yylval.sval.ptr = obstack_base (&name_obstack);
2637 yylval.sval.length = obstack_object_size (&name_obstack);
2638 current.value = yylval;
2639 current.token = classification;
2641 last_was_coloncolon = 0;
2643 else if (next.token == COLONCOLON && !last_was_coloncolon)
2644 last_was_coloncolon = 1;
2647 /* We've reached the end of the name. */
2648 VEC_safe_push (token_and_value, token_fifo, &next);
2657 /* If we ended with a "::", insert it too. */
2658 if (last_was_coloncolon)
2661 memset (&cc, 0, sizeof (token_and_value));
2662 if (first_was_coloncolon && first_iter)
2667 cc.token = COLONCOLON;
2668 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2671 yylval = current.value;
2672 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2674 yylval.sval.length);
2675 return current.token;
2682 struct cleanup *back_to = make_cleanup (free_current_contents,
2683 &expression_macro_scope);
2685 /* Set up the scope for macro expansion. */
2686 expression_macro_scope = NULL;
2688 if (expression_context_block)
2689 expression_macro_scope
2690 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2692 expression_macro_scope = default_macro_scope ();
2693 if (! expression_macro_scope)
2694 expression_macro_scope = user_macro_scope ();
2696 /* Initialize macro expansion code. */
2697 obstack_init (&expansion_obstack);
2698 gdb_assert (! macro_original_text);
2699 make_cleanup (scan_macro_cleanup, 0);
2701 make_cleanup_restore_integer (&yydebug);
2702 yydebug = parser_debug;
2704 /* Initialize some state used by the lexer. */
2705 last_was_structop = 0;
2706 saw_name_at_eof = 0;
2708 VEC_free (token_and_value, token_fifo);
2710 obstack_init (&name_obstack);
2711 make_cleanup_obstack_free (&name_obstack);
2713 result = yyparse ();
2714 do_cleanups (back_to);
2723 lexptr = prev_lexptr;
2725 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);