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;
158 VEC (type_ptr) *tvec;
161 struct type_stack *type_stack;
165 /* YYSTYPE gets defined by %union */
166 static int parse_number (char *, int, int, YYSTYPE *);
167 static struct stoken operator_stoken (const char *);
170 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
172 %type <tval> type typebase
173 %type <tvec> nonempty_typelist func_mod
174 /* %type <bval> block */
176 /* Fancy type parsing. */
178 %type <lval> array_mod
179 %type <tval> conversion_type_id
181 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
183 %token <typed_val_int> INT
184 %token <typed_val_float> FLOAT
185 %token <typed_val_decfloat> DECFLOAT
187 /* Both NAME and TYPENAME tokens represent symbols in the input,
188 and both convey their data as strings.
189 But a TYPENAME is a string that happens to be defined as a typedef
190 or builtin type name (such as int or char)
191 and a NAME is any other symbol.
192 Contexts where this distinction is not important can use the
193 nonterminal "name", which matches either NAME or TYPENAME. */
195 %token <tsval> STRING
197 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
198 %token <ssym> UNKNOWN_CPP_NAME
199 %token <voidval> COMPLETE
200 %token <tsym> TYPENAME
202 %type <svec> string_exp
203 %type <ssym> name_not_typename
204 %type <tsym> typename
206 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
207 but which would parse as a valid number in the current input radix.
208 E.g. "c" when input_radix==16. Depending on the parse, it will be
209 turned into a name or into a number. */
211 %token <ssym> NAME_OR_INT
214 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
218 %type <sval> operator
219 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
222 /* Special type cases, put in to allow the parser to distinguish different
224 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
226 %token <sval> VARIABLE
228 %token <opcode> ASSIGN_MODIFY
237 %right '=' ASSIGN_MODIFY
245 %left '<' '>' LEQ GEQ
250 %right UNARY INCREMENT DECREMENT
251 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
252 %token <ssym> BLOCKNAME
253 %token <bval> FILENAME
265 { write_exp_elt_opcode(OP_TYPE);
266 write_exp_elt_type($1);
267 write_exp_elt_opcode(OP_TYPE);}
270 /* Expressions, including the comma operator. */
273 { write_exp_elt_opcode (BINOP_COMMA); }
276 /* Expressions, not including the comma operator. */
277 exp : '*' exp %prec UNARY
278 { write_exp_elt_opcode (UNOP_IND); }
281 exp : '&' exp %prec UNARY
282 { write_exp_elt_opcode (UNOP_ADDR); }
285 exp : '-' exp %prec UNARY
286 { write_exp_elt_opcode (UNOP_NEG); }
289 exp : '+' exp %prec UNARY
290 { write_exp_elt_opcode (UNOP_PLUS); }
293 exp : '!' exp %prec UNARY
294 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
297 exp : '~' exp %prec UNARY
298 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
301 exp : INCREMENT exp %prec UNARY
302 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
305 exp : DECREMENT exp %prec UNARY
306 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
309 exp : exp INCREMENT %prec UNARY
310 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
313 exp : exp DECREMENT %prec UNARY
314 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
317 exp : SIZEOF exp %prec UNARY
318 { write_exp_elt_opcode (UNOP_SIZEOF); }
322 { write_exp_elt_opcode (STRUCTOP_PTR);
323 write_exp_string ($3);
324 write_exp_elt_opcode (STRUCTOP_PTR); }
327 exp : exp ARROW name COMPLETE
328 { mark_struct_expression ();
329 write_exp_elt_opcode (STRUCTOP_PTR);
330 write_exp_string ($3);
331 write_exp_elt_opcode (STRUCTOP_PTR); }
334 exp : exp ARROW COMPLETE
336 mark_struct_expression ();
337 write_exp_elt_opcode (STRUCTOP_PTR);
340 write_exp_string (s);
341 write_exp_elt_opcode (STRUCTOP_PTR); }
344 exp : exp ARROW qualified_name
345 { /* exp->type::name becomes exp->*(&type::name) */
346 /* Note: this doesn't work if name is a
347 static member! FIXME */
348 write_exp_elt_opcode (UNOP_ADDR);
349 write_exp_elt_opcode (STRUCTOP_MPTR); }
352 exp : exp ARROW_STAR exp
353 { write_exp_elt_opcode (STRUCTOP_MPTR); }
357 { write_exp_elt_opcode (STRUCTOP_STRUCT);
358 write_exp_string ($3);
359 write_exp_elt_opcode (STRUCTOP_STRUCT); }
362 exp : exp '.' name COMPLETE
363 { mark_struct_expression ();
364 write_exp_elt_opcode (STRUCTOP_STRUCT);
365 write_exp_string ($3);
366 write_exp_elt_opcode (STRUCTOP_STRUCT); }
369 exp : exp '.' COMPLETE
371 mark_struct_expression ();
372 write_exp_elt_opcode (STRUCTOP_STRUCT);
375 write_exp_string (s);
376 write_exp_elt_opcode (STRUCTOP_STRUCT); }
379 exp : exp '.' qualified_name
380 { /* exp.type::name becomes exp.*(&type::name) */
381 /* Note: this doesn't work if name is a
382 static member! FIXME */
383 write_exp_elt_opcode (UNOP_ADDR);
384 write_exp_elt_opcode (STRUCTOP_MEMBER); }
387 exp : exp DOT_STAR exp
388 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
391 exp : exp '[' exp1 ']'
392 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
396 /* This is to save the value of arglist_len
397 being accumulated by an outer function call. */
398 { start_arglist (); }
399 arglist ')' %prec ARROW
400 { write_exp_elt_opcode (OP_FUNCALL);
401 write_exp_elt_longcst ((LONGEST) end_arglist ());
402 write_exp_elt_opcode (OP_FUNCALL); }
405 exp : UNKNOWN_CPP_NAME '('
407 /* This could potentially be a an argument defined
408 lookup function (Koenig). */
409 write_exp_elt_opcode (OP_ADL_FUNC);
410 write_exp_elt_block (expression_context_block);
411 write_exp_elt_sym (NULL); /* Placeholder. */
412 write_exp_string ($1.stoken);
413 write_exp_elt_opcode (OP_ADL_FUNC);
415 /* This is to save the value of arglist_len
416 being accumulated by an outer function call. */
420 arglist ')' %prec ARROW
422 write_exp_elt_opcode (OP_FUNCALL);
423 write_exp_elt_longcst ((LONGEST) end_arglist ());
424 write_exp_elt_opcode (OP_FUNCALL);
429 { start_arglist (); }
439 arglist : arglist ',' exp %prec ABOVE_COMMA
443 exp : exp '(' nonempty_typelist ')' const_or_volatile
445 VEC (type_ptr) *type_list = $3;
446 struct type *type_elt;
447 LONGEST len = VEC_length (type_ptr, type_list);
449 write_exp_elt_opcode (TYPE_INSTANCE);
450 write_exp_elt_longcst (len);
452 VEC_iterate (type_ptr, type_list, i, type_elt);
454 write_exp_elt_type (type_elt);
455 write_exp_elt_longcst(len);
456 write_exp_elt_opcode (TYPE_INSTANCE);
457 VEC_free (type_ptr, type_list);
462 { $$ = end_arglist () - 1; }
464 exp : lcurly arglist rcurly %prec ARROW
465 { write_exp_elt_opcode (OP_ARRAY);
466 write_exp_elt_longcst ((LONGEST) 0);
467 write_exp_elt_longcst ((LONGEST) $3);
468 write_exp_elt_opcode (OP_ARRAY); }
471 exp : lcurly type rcurly exp %prec UNARY
472 { write_exp_elt_opcode (UNOP_MEMVAL);
473 write_exp_elt_type ($2);
474 write_exp_elt_opcode (UNOP_MEMVAL); }
477 exp : '(' type ')' exp %prec UNARY
478 { write_exp_elt_opcode (UNOP_CAST);
479 write_exp_elt_type ($2);
480 write_exp_elt_opcode (UNOP_CAST); }
487 /* Binary operators in order of decreasing precedence. */
490 { write_exp_elt_opcode (BINOP_REPEAT); }
494 { write_exp_elt_opcode (BINOP_MUL); }
498 { write_exp_elt_opcode (BINOP_DIV); }
502 { write_exp_elt_opcode (BINOP_REM); }
506 { write_exp_elt_opcode (BINOP_ADD); }
510 { write_exp_elt_opcode (BINOP_SUB); }
514 { write_exp_elt_opcode (BINOP_LSH); }
518 { write_exp_elt_opcode (BINOP_RSH); }
522 { write_exp_elt_opcode (BINOP_EQUAL); }
525 exp : exp NOTEQUAL exp
526 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
530 { write_exp_elt_opcode (BINOP_LEQ); }
534 { write_exp_elt_opcode (BINOP_GEQ); }
538 { write_exp_elt_opcode (BINOP_LESS); }
542 { write_exp_elt_opcode (BINOP_GTR); }
546 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
550 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
554 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
558 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
562 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
565 exp : exp '?' exp ':' exp %prec '?'
566 { write_exp_elt_opcode (TERNOP_COND); }
570 { write_exp_elt_opcode (BINOP_ASSIGN); }
573 exp : exp ASSIGN_MODIFY exp
574 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
575 write_exp_elt_opcode ($2);
576 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
580 { write_exp_elt_opcode (OP_LONG);
581 write_exp_elt_type ($1.type);
582 write_exp_elt_longcst ((LONGEST)($1.val));
583 write_exp_elt_opcode (OP_LONG); }
588 struct stoken_vector vec;
591 write_exp_string_vector ($1.type, &vec);
597 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
598 write_exp_elt_opcode (OP_LONG);
599 write_exp_elt_type (val.typed_val_int.type);
600 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
601 write_exp_elt_opcode (OP_LONG);
607 { write_exp_elt_opcode (OP_DOUBLE);
608 write_exp_elt_type ($1.type);
609 write_exp_elt_dblcst ($1.dval);
610 write_exp_elt_opcode (OP_DOUBLE); }
614 { write_exp_elt_opcode (OP_DECFLOAT);
615 write_exp_elt_type ($1.type);
616 write_exp_elt_decfloatcst ($1.val);
617 write_exp_elt_opcode (OP_DECFLOAT); }
625 write_dollar_variable ($1);
629 exp : SIZEOF '(' type ')' %prec UNARY
630 { write_exp_elt_opcode (OP_LONG);
631 write_exp_elt_type (lookup_signed_typename
632 (parse_language, parse_gdbarch,
635 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
636 write_exp_elt_opcode (OP_LONG); }
639 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
640 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
641 write_exp_elt_type ($3);
642 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
645 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
646 { write_exp_elt_opcode (UNOP_CAST);
647 write_exp_elt_type ($3);
648 write_exp_elt_opcode (UNOP_CAST); }
651 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
652 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
653 write_exp_elt_type ($3);
654 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
657 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
658 { /* We could do more error checking here, but
659 it doesn't seem worthwhile. */
660 write_exp_elt_opcode (UNOP_CAST);
661 write_exp_elt_type ($3);
662 write_exp_elt_opcode (UNOP_CAST); }
668 /* We copy the string here, and not in the
669 lexer, to guarantee that we do not leak a
670 string. Note that we follow the
671 NUL-termination convention of the
673 struct typed_stoken *vec = XNEW (struct typed_stoken);
678 vec->length = $1.length;
679 vec->ptr = malloc ($1.length + 1);
680 memcpy (vec->ptr, $1.ptr, $1.length + 1);
685 /* Note that we NUL-terminate here, but just
689 $$.tokens = realloc ($$.tokens,
690 $$.len * sizeof (struct typed_stoken));
692 p = malloc ($2.length + 1);
693 memcpy (p, $2.ptr, $2.length + 1);
695 $$.tokens[$$.len - 1].type = $2.type;
696 $$.tokens[$$.len - 1].length = $2.length;
697 $$.tokens[$$.len - 1].ptr = p;
704 enum c_string_type type = C_STRING;
706 for (i = 0; i < $1.len; ++i)
708 switch ($1.tokens[i].type)
716 && type != $1.tokens[i].type)
717 error (_("Undefined string concatenation."));
718 type = $1.tokens[i].type;
722 internal_error (__FILE__, __LINE__,
723 "unrecognized type in string concatenation");
727 write_exp_string_vector (type, &$1);
728 for (i = 0; i < $1.len; ++i)
729 free ($1.tokens[i].ptr);
736 { write_exp_elt_opcode (OP_LONG);
737 write_exp_elt_type (parse_type->builtin_bool);
738 write_exp_elt_longcst ((LONGEST) 1);
739 write_exp_elt_opcode (OP_LONG); }
743 { write_exp_elt_opcode (OP_LONG);
744 write_exp_elt_type (parse_type->builtin_bool);
745 write_exp_elt_longcst ((LONGEST) 0);
746 write_exp_elt_opcode (OP_LONG); }
754 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
756 error (_("No file or function \"%s\"."),
757 copy_name ($1.stoken));
765 block : block COLONCOLON name
767 = lookup_symbol (copy_name ($3), $1,
768 VAR_DOMAIN, (int *) NULL);
769 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
770 error (_("No function \"%s\" in specified context."),
772 $$ = SYMBOL_BLOCK_VALUE (tem); }
775 variable: name_not_typename ENTRY
776 { struct symbol *sym = $1.sym;
778 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
779 || !symbol_read_needs_frame (sym))
780 error (_("@entry can be used only for function "
781 "parameters, not for \"%s\""),
782 copy_name ($1.stoken));
784 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
785 write_exp_elt_sym (sym);
786 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
790 variable: block COLONCOLON name
791 { struct symbol *sym;
792 sym = lookup_symbol (copy_name ($3), $1,
793 VAR_DOMAIN, (int *) NULL);
795 error (_("No symbol \"%s\" in specified context."),
797 if (symbol_read_needs_frame (sym))
799 if (innermost_block == 0
800 || contained_in (block_found,
802 innermost_block = block_found;
805 write_exp_elt_opcode (OP_VAR_VALUE);
806 /* block_found is set by lookup_symbol. */
807 write_exp_elt_block (block_found);
808 write_exp_elt_sym (sym);
809 write_exp_elt_opcode (OP_VAR_VALUE); }
812 qualified_name: TYPENAME COLONCOLON name
814 struct type *type = $1.type;
815 CHECK_TYPEDEF (type);
816 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
817 && TYPE_CODE (type) != TYPE_CODE_UNION
818 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
819 error (_("`%s' is not defined as an aggregate type."),
822 write_exp_elt_opcode (OP_SCOPE);
823 write_exp_elt_type (type);
824 write_exp_string ($3);
825 write_exp_elt_opcode (OP_SCOPE);
827 | TYPENAME COLONCOLON '~' name
829 struct type *type = $1.type;
830 struct stoken tmp_token;
831 CHECK_TYPEDEF (type);
832 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
833 && TYPE_CODE (type) != TYPE_CODE_UNION
834 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
835 error (_("`%s' is not defined as an aggregate type."),
838 tmp_token.ptr = (char*) alloca ($4.length + 2);
839 tmp_token.length = $4.length + 1;
840 tmp_token.ptr[0] = '~';
841 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
842 tmp_token.ptr[tmp_token.length] = 0;
844 /* Check for valid destructor name. */
845 destructor_name_p (tmp_token.ptr, $1.type);
846 write_exp_elt_opcode (OP_SCOPE);
847 write_exp_elt_type (type);
848 write_exp_string (tmp_token);
849 write_exp_elt_opcode (OP_SCOPE);
851 | TYPENAME COLONCOLON name COLONCOLON name
853 char *copy = copy_name ($3);
854 error (_("No type \"%s\" within class "
855 "or namespace \"%s\"."),
856 copy, TYPE_NAME ($1.type));
860 variable: qualified_name
861 | COLONCOLON name_not_typename
863 char *name = copy_name ($2.stoken);
865 struct minimal_symbol *msymbol;
868 lookup_symbol (name, (const struct block *) NULL,
869 VAR_DOMAIN, (int *) NULL);
872 write_exp_elt_opcode (OP_VAR_VALUE);
873 write_exp_elt_block (NULL);
874 write_exp_elt_sym (sym);
875 write_exp_elt_opcode (OP_VAR_VALUE);
879 msymbol = lookup_minimal_symbol (name, NULL, NULL);
881 write_exp_msymbol (msymbol);
882 else if (!have_full_symbols () && !have_partial_symbols ())
883 error (_("No symbol table is loaded. Use the \"file\" command."));
885 error (_("No symbol \"%s\" in current context."), name);
889 variable: name_not_typename
890 { struct symbol *sym = $1.sym;
894 if (symbol_read_needs_frame (sym))
896 if (innermost_block == 0
897 || contained_in (block_found,
899 innermost_block = block_found;
902 write_exp_elt_opcode (OP_VAR_VALUE);
903 /* We want to use the selected frame, not
904 another more inner frame which happens to
905 be in the same block. */
906 write_exp_elt_block (NULL);
907 write_exp_elt_sym (sym);
908 write_exp_elt_opcode (OP_VAR_VALUE);
910 else if ($1.is_a_field_of_this)
912 /* C++: it hangs off of `this'. Must
913 not inadvertently convert from a method call
915 if (innermost_block == 0
916 || contained_in (block_found,
918 innermost_block = block_found;
919 write_exp_elt_opcode (OP_THIS);
920 write_exp_elt_opcode (OP_THIS);
921 write_exp_elt_opcode (STRUCTOP_PTR);
922 write_exp_string ($1.stoken);
923 write_exp_elt_opcode (STRUCTOP_PTR);
927 struct minimal_symbol *msymbol;
928 char *arg = copy_name ($1.stoken);
931 lookup_minimal_symbol (arg, NULL, NULL);
933 write_exp_msymbol (msymbol);
934 else if (!have_full_symbols () && !have_partial_symbols ())
935 error (_("No symbol table is loaded. Use the \"file\" command."));
937 error (_("No symbol \"%s\" in current context."),
938 copy_name ($1.stoken));
943 space_identifier : '@' NAME
944 { insert_type_address_space (copy_name ($2.stoken)); }
947 const_or_volatile: const_or_volatile_noopt
951 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
954 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
955 | const_or_volatile_noopt
958 const_or_volatile_or_space_identifier:
959 const_or_volatile_or_space_identifier_noopt
965 { insert_type (tp_pointer); }
966 const_or_volatile_or_space_identifier
968 { insert_type (tp_pointer); }
969 const_or_volatile_or_space_identifier
971 { insert_type (tp_reference); }
973 { insert_type (tp_reference); }
976 ptr_operator_ts: ptr_operator
978 $$ = get_type_stack ();
979 /* This cleanup is eventually run by
981 make_cleanup (type_stack_cleanup, $$);
985 abs_decl: ptr_operator_ts direct_abs_decl
986 { $$ = append_type_stack ($2, $1); }
991 direct_abs_decl: '(' abs_decl ')'
993 | direct_abs_decl array_mod
995 push_type_stack ($1);
997 push_type (tp_array);
998 $$ = get_type_stack ();
1003 push_type (tp_array);
1004 $$ = get_type_stack ();
1007 | direct_abs_decl func_mod
1009 push_type_stack ($1);
1011 $$ = get_type_stack ();
1016 $$ = get_type_stack ();
1028 | '(' nonempty_typelist ')'
1032 /* We used to try to recognize pointer to member types here, but
1033 that didn't work (shift/reduce conflicts meant that these rules never
1034 got executed). The problem is that
1035 int (foo::bar::baz::bizzle)
1036 is a function type but
1037 int (foo::bar::baz::bizzle::*)
1038 is a pointer to member type. Stroustrup loses again! */
1043 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1047 { $$ = lookup_signed_typename (parse_language,
1051 { $$ = lookup_signed_typename (parse_language,
1055 { $$ = lookup_signed_typename (parse_language,
1059 { $$ = lookup_signed_typename (parse_language,
1062 | LONG SIGNED_KEYWORD INT_KEYWORD
1063 { $$ = lookup_signed_typename (parse_language,
1066 | LONG SIGNED_KEYWORD
1067 { $$ = lookup_signed_typename (parse_language,
1070 | SIGNED_KEYWORD LONG INT_KEYWORD
1071 { $$ = lookup_signed_typename (parse_language,
1074 | UNSIGNED LONG INT_KEYWORD
1075 { $$ = lookup_unsigned_typename (parse_language,
1078 | LONG UNSIGNED INT_KEYWORD
1079 { $$ = lookup_unsigned_typename (parse_language,
1083 { $$ = lookup_unsigned_typename (parse_language,
1087 { $$ = lookup_signed_typename (parse_language,
1090 | LONG LONG INT_KEYWORD
1091 { $$ = lookup_signed_typename (parse_language,
1094 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1095 { $$ = lookup_signed_typename (parse_language,
1098 | LONG LONG SIGNED_KEYWORD
1099 { $$ = lookup_signed_typename (parse_language,
1102 | SIGNED_KEYWORD LONG LONG
1103 { $$ = lookup_signed_typename (parse_language,
1106 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1107 { $$ = lookup_signed_typename (parse_language,
1110 | UNSIGNED LONG LONG
1111 { $$ = lookup_unsigned_typename (parse_language,
1114 | UNSIGNED LONG LONG INT_KEYWORD
1115 { $$ = lookup_unsigned_typename (parse_language,
1118 | LONG LONG UNSIGNED
1119 { $$ = lookup_unsigned_typename (parse_language,
1122 | LONG LONG UNSIGNED INT_KEYWORD
1123 { $$ = lookup_unsigned_typename (parse_language,
1127 { $$ = lookup_signed_typename (parse_language,
1130 | SHORT SIGNED_KEYWORD INT_KEYWORD
1131 { $$ = lookup_signed_typename (parse_language,
1134 | SHORT SIGNED_KEYWORD
1135 { $$ = lookup_signed_typename (parse_language,
1138 | UNSIGNED SHORT INT_KEYWORD
1139 { $$ = lookup_unsigned_typename (parse_language,
1143 { $$ = lookup_unsigned_typename (parse_language,
1146 | SHORT UNSIGNED INT_KEYWORD
1147 { $$ = lookup_unsigned_typename (parse_language,
1151 { $$ = lookup_typename (parse_language, parse_gdbarch,
1152 "double", (struct block *) NULL,
1154 | LONG DOUBLE_KEYWORD
1155 { $$ = lookup_typename (parse_language, parse_gdbarch,
1157 (struct block *) NULL, 0); }
1159 { $$ = lookup_struct (copy_name ($2),
1160 expression_context_block); }
1162 { $$ = lookup_struct (copy_name ($2),
1163 expression_context_block); }
1165 { $$ = lookup_union (copy_name ($2),
1166 expression_context_block); }
1168 { $$ = lookup_enum (copy_name ($2),
1169 expression_context_block); }
1171 { $$ = lookup_unsigned_typename (parse_language,
1173 TYPE_NAME($2.type)); }
1175 { $$ = lookup_unsigned_typename (parse_language,
1178 | SIGNED_KEYWORD typename
1179 { $$ = lookup_signed_typename (parse_language,
1181 TYPE_NAME($2.type)); }
1183 { $$ = lookup_signed_typename (parse_language,
1186 /* It appears that this rule for templates is never
1187 reduced; template recognition happens by lookahead
1188 in the token processing code in yylex. */
1189 | TEMPLATE name '<' type '>'
1190 { $$ = lookup_template_type(copy_name($2), $4,
1191 expression_context_block);
1193 | const_or_volatile_or_space_identifier_noopt typebase
1194 { $$ = follow_types ($2); }
1195 | typebase const_or_volatile_or_space_identifier_noopt
1196 { $$ = follow_types ($1); }
1202 $$.stoken.ptr = "int";
1203 $$.stoken.length = 3;
1204 $$.type = lookup_signed_typename (parse_language,
1210 $$.stoken.ptr = "long";
1211 $$.stoken.length = 4;
1212 $$.type = lookup_signed_typename (parse_language,
1218 $$.stoken.ptr = "short";
1219 $$.stoken.length = 5;
1220 $$.type = lookup_signed_typename (parse_language,
1229 VEC (type_ptr) *typelist = NULL;
1230 VEC_safe_push (type_ptr, typelist, $1);
1233 | nonempty_typelist ',' type
1235 VEC_safe_push (type_ptr, $1, $3);
1243 push_type_stack ($2);
1244 $$ = follow_types ($1);
1248 conversion_type_id: typebase conversion_declarator
1249 { $$ = follow_types ($1); }
1252 conversion_declarator: /* Nothing. */
1253 | ptr_operator conversion_declarator
1256 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1257 | VOLATILE_KEYWORD CONST_KEYWORD
1260 const_or_volatile_noopt: const_and_volatile
1261 { insert_type (tp_const);
1262 insert_type (tp_volatile);
1265 { insert_type (tp_const); }
1267 { insert_type (tp_volatile); }
1270 operator: OPERATOR NEW
1271 { $$ = operator_stoken (" new"); }
1273 { $$ = operator_stoken (" delete"); }
1274 | OPERATOR NEW '[' ']'
1275 { $$ = operator_stoken (" new[]"); }
1276 | OPERATOR DELETE '[' ']'
1277 { $$ = operator_stoken (" delete[]"); }
1279 { $$ = operator_stoken ("+"); }
1281 { $$ = operator_stoken ("-"); }
1283 { $$ = operator_stoken ("*"); }
1285 { $$ = operator_stoken ("/"); }
1287 { $$ = operator_stoken ("%"); }
1289 { $$ = operator_stoken ("^"); }
1291 { $$ = operator_stoken ("&"); }
1293 { $$ = operator_stoken ("|"); }
1295 { $$ = operator_stoken ("~"); }
1297 { $$ = operator_stoken ("!"); }
1299 { $$ = operator_stoken ("="); }
1301 { $$ = operator_stoken ("<"); }
1303 { $$ = operator_stoken (">"); }
1304 | OPERATOR ASSIGN_MODIFY
1305 { const char *op = "unknown";
1329 case BINOP_BITWISE_IOR:
1332 case BINOP_BITWISE_AND:
1335 case BINOP_BITWISE_XOR:
1342 $$ = operator_stoken (op);
1345 { $$ = operator_stoken ("<<"); }
1347 { $$ = operator_stoken (">>"); }
1349 { $$ = operator_stoken ("=="); }
1351 { $$ = operator_stoken ("!="); }
1353 { $$ = operator_stoken ("<="); }
1355 { $$ = operator_stoken (">="); }
1357 { $$ = operator_stoken ("&&"); }
1359 { $$ = operator_stoken ("||"); }
1360 | OPERATOR INCREMENT
1361 { $$ = operator_stoken ("++"); }
1362 | OPERATOR DECREMENT
1363 { $$ = operator_stoken ("--"); }
1365 { $$ = operator_stoken (","); }
1366 | OPERATOR ARROW_STAR
1367 { $$ = operator_stoken ("->*"); }
1369 { $$ = operator_stoken ("->"); }
1371 { $$ = operator_stoken ("()"); }
1373 { $$ = operator_stoken ("[]"); }
1374 | OPERATOR conversion_type_id
1377 struct ui_file *buf = mem_fileopen ();
1379 c_print_type ($2, NULL, buf, -1, 0);
1380 name = ui_file_xstrdup (buf, &length);
1381 ui_file_delete (buf);
1382 $$ = operator_stoken (name);
1389 name : NAME { $$ = $1.stoken; }
1390 | BLOCKNAME { $$ = $1.stoken; }
1391 | TYPENAME { $$ = $1.stoken; }
1392 | NAME_OR_INT { $$ = $1.stoken; }
1393 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1394 | operator { $$ = $1; }
1397 name_not_typename : NAME
1399 /* These would be useful if name_not_typename was useful, but it is just
1400 a fake for "variable", so these cause reduce/reduce conflicts because
1401 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1402 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1403 context where only a name could occur, this might be useful.
1409 $$.sym = lookup_symbol ($1.ptr,
1410 expression_context_block,
1412 &$$.is_a_field_of_this);
1419 /* Returns a stoken of the operator name given by OP (which does not
1420 include the string "operator"). */
1421 static struct stoken
1422 operator_stoken (const char *op)
1424 static const char *operator_string = "operator";
1425 struct stoken st = { NULL, 0 };
1426 st.length = strlen (operator_string) + strlen (op);
1427 st.ptr = malloc (st.length + 1);
1428 strcpy (st.ptr, operator_string);
1429 strcat (st.ptr, op);
1431 /* The toplevel (c_parse) will free the memory allocated here. */
1432 make_cleanup (free, st.ptr);
1436 /* Take care of parsing a number (anything that starts with a digit).
1437 Set yylval and return the token type; update lexptr.
1438 LEN is the number of characters in it. */
1440 /*** Needs some error checking for the float case ***/
1443 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1445 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1446 here, and we do kind of silly things like cast to unsigned. */
1453 int base = input_radix;
1456 /* Number of "L" suffixes encountered. */
1459 /* We have found a "L" or "U" suffix. */
1460 int found_suffix = 0;
1463 struct type *signed_type;
1464 struct type *unsigned_type;
1468 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1469 point. Return DECFLOAT. */
1471 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1474 putithere->typed_val_decfloat.type
1475 = parse_type->builtin_decfloat;
1476 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1477 gdbarch_byte_order (parse_gdbarch), p);
1482 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1485 putithere->typed_val_decfloat.type
1486 = parse_type->builtin_decdouble;
1487 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1488 gdbarch_byte_order (parse_gdbarch), p);
1493 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1496 putithere->typed_val_decfloat.type
1497 = parse_type->builtin_declong;
1498 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1499 gdbarch_byte_order (parse_gdbarch), p);
1504 if (! parse_c_float (parse_gdbarch, p, len,
1505 &putithere->typed_val_float.dval,
1506 &putithere->typed_val_float.type))
1511 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1555 if (c >= 'A' && c <= 'Z')
1557 if (c != 'l' && c != 'u')
1559 if (c >= '0' && c <= '9')
1567 if (base > 10 && c >= 'a' && c <= 'f')
1571 n += i = c - 'a' + 10;
1584 return ERROR; /* Char not a digit */
1587 return ERROR; /* Invalid digit in this base */
1589 /* Portably test for overflow (only works for nonzero values, so make
1590 a second check for zero). FIXME: Can't we just make n and prevn
1591 unsigned and avoid this? */
1592 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1593 unsigned_p = 1; /* Try something unsigned */
1595 /* Portably test for unsigned overflow.
1596 FIXME: This check is wrong; for example it doesn't find overflow
1597 on 0x123456789 when LONGEST is 32 bits. */
1598 if (c != 'l' && c != 'u' && n != 0)
1600 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1601 error (_("Numeric constant too large."));
1606 /* An integer constant is an int, a long, or a long long. An L
1607 suffix forces it to be long; an LL suffix forces it to be long
1608 long. If not forced to a larger size, it gets the first type of
1609 the above that it fits in. To figure out whether it fits, we
1610 shift it right and see whether anything remains. Note that we
1611 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1612 operation, because many compilers will warn about such a shift
1613 (which always produces a zero result). Sometimes gdbarch_int_bit
1614 or gdbarch_long_bit will be that big, sometimes not. To deal with
1615 the case where it is we just always shift the value more than
1616 once, with fewer bits each time. */
1618 un = (ULONGEST)n >> 2;
1620 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1622 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1624 /* A large decimal (not hex or octal) constant (between INT_MAX
1625 and UINT_MAX) is a long or unsigned long, according to ANSI,
1626 never an unsigned int, but this code treats it as unsigned
1627 int. This probably should be fixed. GCC gives a warning on
1630 unsigned_type = parse_type->builtin_unsigned_int;
1631 signed_type = parse_type->builtin_int;
1633 else if (long_p <= 1
1634 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1636 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1637 unsigned_type = parse_type->builtin_unsigned_long;
1638 signed_type = parse_type->builtin_long;
1643 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1644 < gdbarch_long_long_bit (parse_gdbarch))
1645 /* A long long does not fit in a LONGEST. */
1646 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1648 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1649 high_bit = (ULONGEST) 1 << shift;
1650 unsigned_type = parse_type->builtin_unsigned_long_long;
1651 signed_type = parse_type->builtin_long_long;
1654 putithere->typed_val_int.val = n;
1656 /* If the high bit of the worked out type is set then this number
1657 has to be unsigned. */
1659 if (unsigned_p || (n & high_bit))
1661 putithere->typed_val_int.type = unsigned_type;
1665 putithere->typed_val_int.type = signed_type;
1671 /* Temporary obstack used for holding strings. */
1672 static struct obstack tempbuf;
1673 static int tempbuf_init;
1675 /* Parse a C escape sequence. The initial backslash of the sequence
1676 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1677 last character of the sequence. If OUTPUT is not NULL, the
1678 translated form of the escape sequence will be written there. If
1679 OUTPUT is NULL, no output is written and the call will only affect
1680 *PTR. If an escape sequence is expressed in target bytes, then the
1681 entire sequence will simply be copied to OUTPUT. Return 1 if any
1682 character was emitted, 0 otherwise. */
1685 c_parse_escape (char **ptr, struct obstack *output)
1687 char *tokptr = *ptr;
1690 /* Some escape sequences undergo character set conversion. Those we
1694 /* Hex escapes do not undergo character set conversion, so keep
1695 the escape sequence for later. */
1698 obstack_grow_str (output, "\\x");
1700 if (!isxdigit (*tokptr))
1701 error (_("\\x escape without a following hex digit"));
1702 while (isxdigit (*tokptr))
1705 obstack_1grow (output, *tokptr);
1710 /* Octal escapes do not undergo character set conversion, so
1711 keep the escape sequence for later. */
1723 obstack_grow_str (output, "\\");
1725 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1729 obstack_1grow (output, *tokptr);
1735 /* We handle UCNs later. We could handle them here, but that
1736 would mean a spurious error in the case where the UCN could
1737 be converted to the target charset but not the host
1743 int i, len = c == 'U' ? 8 : 4;
1746 obstack_1grow (output, '\\');
1747 obstack_1grow (output, *tokptr);
1750 if (!isxdigit (*tokptr))
1751 error (_("\\%c escape without a following hex digit"), c);
1752 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1755 obstack_1grow (output, *tokptr);
1761 /* We must pass backslash through so that it does not
1762 cause quoting during the second expansion. */
1765 obstack_grow_str (output, "\\\\");
1769 /* Escapes which undergo conversion. */
1772 obstack_1grow (output, '\a');
1777 obstack_1grow (output, '\b');
1782 obstack_1grow (output, '\f');
1787 obstack_1grow (output, '\n');
1792 obstack_1grow (output, '\r');
1797 obstack_1grow (output, '\t');
1802 obstack_1grow (output, '\v');
1806 /* GCC extension. */
1809 obstack_1grow (output, HOST_ESCAPE_CHAR);
1813 /* Backslash-newline expands to nothing at all. */
1819 /* A few escapes just expand to the character itself. */
1823 /* GCC extensions. */
1828 /* Unrecognized escapes turn into the character itself. */
1831 obstack_1grow (output, *tokptr);
1839 /* Parse a string or character literal from TOKPTR. The string or
1840 character may be wide or unicode. *OUTPTR is set to just after the
1841 end of the literal in the input string. The resulting token is
1842 stored in VALUE. This returns a token value, either STRING or
1843 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1844 number of host characters in the literal. */
1846 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1850 enum c_string_type type;
1852 /* Build the gdb internal form of the input string in tempbuf. Note
1853 that the buffer is null byte terminated *only* for the
1854 convenience of debugging gdb itself and printing the buffer
1855 contents when the buffer contains no embedded nulls. Gdb does
1856 not depend upon the buffer being null byte terminated, it uses
1857 the length string instead. This allows gdb to handle C strings
1858 (as well as strings in other languages) with embedded null
1864 obstack_free (&tempbuf, NULL);
1865 obstack_init (&tempbuf);
1867 /* Record the string type. */
1870 type = C_WIDE_STRING;
1873 else if (*tokptr == 'u')
1878 else if (*tokptr == 'U')
1886 /* Skip the quote. */
1900 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1902 else if (c == quote)
1906 obstack_1grow (&tempbuf, c);
1908 /* FIXME: this does the wrong thing with multi-byte host
1909 characters. We could use mbrlen here, but that would
1910 make "set host-charset" a bit less useful. */
1915 if (*tokptr != quote)
1918 error (_("Unterminated string in expression."));
1920 error (_("Unmatched single quote."));
1925 value->ptr = obstack_base (&tempbuf);
1926 value->length = obstack_object_size (&tempbuf);
1930 return quote == '"' ? STRING : CHAR;
1937 enum exp_opcode opcode;
1941 static const struct token tokentab3[] =
1943 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1944 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1945 {"->*", ARROW_STAR, BINOP_END, 1}
1948 static const struct token tokentab2[] =
1950 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1951 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1952 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1953 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1954 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1955 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1956 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1957 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1958 {"++", INCREMENT, BINOP_END, 0},
1959 {"--", DECREMENT, BINOP_END, 0},
1960 {"->", ARROW, BINOP_END, 0},
1961 {"&&", ANDAND, BINOP_END, 0},
1962 {"||", OROR, BINOP_END, 0},
1963 /* "::" is *not* only C++: gdb overrides its meaning in several
1964 different ways, e.g., 'filename'::func, function::variable. */
1965 {"::", COLONCOLON, BINOP_END, 0},
1966 {"<<", LSH, BINOP_END, 0},
1967 {">>", RSH, BINOP_END, 0},
1968 {"==", EQUAL, BINOP_END, 0},
1969 {"!=", NOTEQUAL, BINOP_END, 0},
1970 {"<=", LEQ, BINOP_END, 0},
1971 {">=", GEQ, BINOP_END, 0},
1972 {".*", DOT_STAR, BINOP_END, 1}
1975 /* Identifier-like tokens. */
1976 static const struct token ident_tokens[] =
1978 {"unsigned", UNSIGNED, OP_NULL, 0},
1979 {"template", TEMPLATE, OP_NULL, 1},
1980 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1981 {"struct", STRUCT, OP_NULL, 0},
1982 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1983 {"sizeof", SIZEOF, OP_NULL, 0},
1984 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1985 {"false", FALSEKEYWORD, OP_NULL, 1},
1986 {"class", CLASS, OP_NULL, 1},
1987 {"union", UNION, OP_NULL, 0},
1988 {"short", SHORT, OP_NULL, 0},
1989 {"const", CONST_KEYWORD, OP_NULL, 0},
1990 {"enum", ENUM, OP_NULL, 0},
1991 {"long", LONG, OP_NULL, 0},
1992 {"true", TRUEKEYWORD, OP_NULL, 1},
1993 {"int", INT_KEYWORD, OP_NULL, 0},
1994 {"new", NEW, OP_NULL, 1},
1995 {"delete", DELETE, OP_NULL, 1},
1996 {"operator", OPERATOR, OP_NULL, 1},
1998 {"and", ANDAND, BINOP_END, 1},
1999 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
2000 {"bitand", '&', OP_NULL, 1},
2001 {"bitor", '|', OP_NULL, 1},
2002 {"compl", '~', OP_NULL, 1},
2003 {"not", '!', OP_NULL, 1},
2004 {"not_eq", NOTEQUAL, BINOP_END, 1},
2005 {"or", OROR, BINOP_END, 1},
2006 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
2007 {"xor", '^', OP_NULL, 1},
2008 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
2010 {"const_cast", CONST_CAST, OP_NULL, 1 },
2011 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
2012 {"static_cast", STATIC_CAST, OP_NULL, 1 },
2013 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
2016 /* When we find that lexptr (the global var defined in parse.c) is
2017 pointing at a macro invocation, we expand the invocation, and call
2018 scan_macro_expansion to save the old lexptr here and point lexptr
2019 into the expanded text. When we reach the end of that, we call
2020 end_macro_expansion to pop back to the value we saved here. The
2021 macro expansion code promises to return only fully-expanded text,
2022 so we don't need to "push" more than one level.
2024 This is disgusting, of course. It would be cleaner to do all macro
2025 expansion beforehand, and then hand that to lexptr. But we don't
2026 really know where the expression ends. Remember, in a command like
2028 (gdb) break *ADDRESS if CONDITION
2030 we evaluate ADDRESS in the scope of the current frame, but we
2031 evaluate CONDITION in the scope of the breakpoint's location. So
2032 it's simply wrong to try to macro-expand the whole thing at once. */
2033 static char *macro_original_text;
2035 /* We save all intermediate macro expansions on this obstack for the
2036 duration of a single parse. The expansion text may sometimes have
2037 to live past the end of the expansion, due to yacc lookahead.
2038 Rather than try to be clever about saving the data for a single
2039 token, we simply keep it all and delete it after parsing has
2041 static struct obstack expansion_obstack;
2044 scan_macro_expansion (char *expansion)
2048 /* We'd better not be trying to push the stack twice. */
2049 gdb_assert (! macro_original_text);
2051 /* Copy to the obstack, and then free the intermediate
2053 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2056 /* Save the old lexptr value, so we can return to it when we're done
2057 parsing the expanded text. */
2058 macro_original_text = lexptr;
2064 scanning_macro_expansion (void)
2066 return macro_original_text != 0;
2071 finished_macro_expansion (void)
2073 /* There'd better be something to pop back to. */
2074 gdb_assert (macro_original_text);
2076 /* Pop back to the original text. */
2077 lexptr = macro_original_text;
2078 macro_original_text = 0;
2083 scan_macro_cleanup (void *dummy)
2085 if (macro_original_text)
2086 finished_macro_expansion ();
2088 obstack_free (&expansion_obstack, NULL);
2091 /* Return true iff the token represents a C++ cast operator. */
2094 is_cast_operator (const char *token, int len)
2096 return (! strncmp (token, "dynamic_cast", len)
2097 || ! strncmp (token, "static_cast", len)
2098 || ! strncmp (token, "reinterpret_cast", len)
2099 || ! strncmp (token, "const_cast", len));
2102 /* The scope used for macro expansion. */
2103 static struct macro_scope *expression_macro_scope;
2105 /* This is set if a NAME token appeared at the very end of the input
2106 string, with no whitespace separating the name from the EOF. This
2107 is used only when parsing to do field name completion. */
2108 static int saw_name_at_eof;
2110 /* This is set if the previously-returned token was a structure
2111 operator -- either '.' or ARROW. This is used only when parsing to
2112 do field name completion. */
2113 static int last_was_structop;
2115 /* Read one token, getting characters through lexptr. */
2118 lex_one_token (void)
2124 int saw_structop = last_was_structop;
2127 last_was_structop = 0;
2131 /* Check if this is a macro invocation that we need to expand. */
2132 if (! scanning_macro_expansion ())
2134 char *expanded = macro_expand_next (&lexptr,
2135 standard_macro_lookup,
2136 expression_macro_scope);
2139 scan_macro_expansion (expanded);
2142 prev_lexptr = lexptr;
2145 /* See if it is a special token of length 3. */
2146 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2147 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2149 if (tokentab3[i].cxx_only
2150 && parse_language->la_language != language_cplus)
2154 yylval.opcode = tokentab3[i].opcode;
2155 return tokentab3[i].token;
2158 /* See if it is a special token of length 2. */
2159 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2160 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2162 if (tokentab2[i].cxx_only
2163 && parse_language->la_language != language_cplus)
2167 yylval.opcode = tokentab2[i].opcode;
2168 if (in_parse_field && tokentab2[i].token == ARROW)
2169 last_was_structop = 1;
2170 return tokentab2[i].token;
2173 switch (c = *tokstart)
2176 /* If we were just scanning the result of a macro expansion,
2177 then we need to resume scanning the original text.
2178 If we're parsing for field name completion, and the previous
2179 token allows such completion, return a COMPLETE token.
2180 Otherwise, we were already scanning the original text, and
2181 we're really done. */
2182 if (scanning_macro_expansion ())
2184 finished_macro_expansion ();
2187 else if (saw_name_at_eof)
2189 saw_name_at_eof = 0;
2192 else if (saw_structop)
2211 if (paren_depth == 0)
2218 if (comma_terminates
2220 && ! scanning_macro_expansion ())
2226 /* Might be a floating point number. */
2227 if (lexptr[1] < '0' || lexptr[1] > '9')
2230 last_was_structop = 1;
2231 goto symbol; /* Nope, must be a symbol. */
2233 /* FALL THRU into number case. */
2246 /* It's a number. */
2247 int got_dot = 0, got_e = 0, toktype;
2249 int hex = input_radix > 10;
2251 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2256 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2264 /* This test includes !hex because 'e' is a valid hex digit
2265 and thus does not indicate a floating point number when
2266 the radix is hex. */
2267 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2268 got_dot = got_e = 1;
2269 /* This test does not include !hex, because a '.' always indicates
2270 a decimal floating point number regardless of the radix. */
2271 else if (!got_dot && *p == '.')
2273 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2274 && (*p == '-' || *p == '+'))
2275 /* This is the sign of the exponent, not the end of the
2278 /* We will take any letters or digits. parse_number will
2279 complain if past the radix, or if L or U are not final. */
2280 else if ((*p < '0' || *p > '9')
2281 && ((*p < 'a' || *p > 'z')
2282 && (*p < 'A' || *p > 'Z')))
2285 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2286 if (toktype == ERROR)
2288 char *err_copy = (char *) alloca (p - tokstart + 1);
2290 memcpy (err_copy, tokstart, p - tokstart);
2291 err_copy[p - tokstart] = 0;
2292 error (_("Invalid number \"%s\"."), err_copy);
2300 char *p = &tokstart[1];
2301 size_t len = strlen ("entry");
2303 while (isspace (*p))
2305 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2337 if (tokstart[1] != '"' && tokstart[1] != '\'')
2344 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2349 error (_("Empty character constant."));
2350 else if (host_len > 2 && c == '\'')
2353 namelen = lexptr - tokstart - 1;
2356 else if (host_len > 1)
2357 error (_("Invalid character constant."));
2363 if (!(c == '_' || c == '$'
2364 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2365 /* We must have come across a bad character (e.g. ';'). */
2366 error (_("Invalid character '%c' in expression."), c);
2368 /* It's a name. See how long it is. */
2370 for (c = tokstart[namelen];
2371 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2372 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2374 /* Template parameter lists are part of the name.
2375 FIXME: This mishandles `print $a<4&&$a>3'. */
2379 if (! is_cast_operator (tokstart, namelen))
2381 /* Scan ahead to get rest of the template specification. Note
2382 that we look ahead only when the '<' adjoins non-whitespace
2383 characters; for comparison expressions, e.g. "a < b > c",
2384 there must be spaces before the '<', etc. */
2386 char * p = find_template_name_end (tokstart + namelen);
2388 namelen = p - tokstart;
2392 c = tokstart[++namelen];
2395 /* The token "if" terminates the expression and is NOT removed from
2396 the input stream. It doesn't count if it appears in the
2397 expansion of a macro. */
2399 && tokstart[0] == 'i'
2400 && tokstart[1] == 'f'
2401 && ! scanning_macro_expansion ())
2406 /* For the same reason (breakpoint conditions), "thread N"
2407 terminates the expression. "thread" could be an identifier, but
2408 an identifier is never followed by a number without intervening
2409 punctuation. "task" is similar. Handle abbreviations of these,
2410 similarly to breakpoint.c:find_condition_and_thread. */
2412 && (strncmp (tokstart, "thread", namelen) == 0
2413 || strncmp (tokstart, "task", namelen) == 0)
2414 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2415 && ! scanning_macro_expansion ())
2417 char *p = tokstart + namelen + 1;
2418 while (*p == ' ' || *p == '\t')
2420 if (*p >= '0' && *p <= '9')
2428 yylval.sval.ptr = tokstart;
2429 yylval.sval.length = namelen;
2431 /* Catch specific keywords. */
2432 copy = copy_name (yylval.sval);
2433 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2434 if (strcmp (copy, ident_tokens[i].operator) == 0)
2436 if (ident_tokens[i].cxx_only
2437 && parse_language->la_language != language_cplus)
2440 /* It is ok to always set this, even though we don't always
2441 strictly need to. */
2442 yylval.opcode = ident_tokens[i].opcode;
2443 return ident_tokens[i].token;
2446 if (*tokstart == '$')
2449 if (in_parse_field && *lexptr == '\0')
2450 saw_name_at_eof = 1;
2454 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2461 DEF_VEC_O (token_and_value);
2463 /* A FIFO of tokens that have been read but not yet returned to the
2465 static VEC (token_and_value) *token_fifo;
2467 /* Non-zero if the lexer should return tokens from the FIFO. */
2470 /* Temporary storage for c_lex; this holds symbol names as they are
2472 static struct obstack name_obstack;
2474 /* Classify a NAME token. The contents of the token are in `yylval'.
2475 Updates yylval and returns the new token type. BLOCK is the block
2476 in which lookups start; this can be NULL to mean the global
2479 classify_name (struct block *block)
2483 int is_a_field_of_this = 0;
2485 copy = copy_name (yylval.sval);
2487 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2488 parse_language->la_language == language_cplus
2489 ? &is_a_field_of_this : (int *) NULL);
2491 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2493 yylval.ssym.sym = sym;
2494 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2499 /* See if it's a file name. */
2500 struct symtab *symtab;
2502 symtab = lookup_symtab (copy);
2505 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2510 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2512 yylval.tsym.type = SYMBOL_TYPE (sym);
2517 = language_lookup_primitive_type_by_name (parse_language,
2518 parse_gdbarch, copy);
2519 if (yylval.tsym.type != NULL)
2522 /* Input names that aren't symbols but ARE valid hex numbers, when
2523 the input radix permits them, can be names or numbers depending
2524 on the parse. Note we support radixes > 16 here. */
2526 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2527 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2529 YYSTYPE newlval; /* Its value is ignored. */
2530 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2533 yylval.ssym.sym = sym;
2534 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2539 /* Any other kind of symbol */
2540 yylval.ssym.sym = sym;
2541 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2544 && parse_language->la_language == language_cplus
2545 && !is_a_field_of_this
2546 && !lookup_minimal_symbol (copy, NULL, NULL))
2547 return UNKNOWN_CPP_NAME;
2552 /* Like classify_name, but used by the inner loop of the lexer, when a
2553 name might have already been seen. FIRST_NAME is true if the token
2554 in `yylval' is the first component of a name, false otherwise. */
2557 classify_inner_name (struct block *block, int first_name)
2559 struct type *type, *new_type;
2563 return classify_name (block);
2565 type = check_typedef (yylval.tsym.type);
2566 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2567 && TYPE_CODE (type) != TYPE_CODE_UNION
2568 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2571 copy = copy_name (yylval.tsym.stoken);
2572 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2573 if (yylval.ssym.sym == NULL)
2576 switch (SYMBOL_CLASS (yylval.ssym.sym))
2583 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2587 yylval.ssym.is_a_field_of_this = 0;
2590 internal_error (__FILE__, __LINE__, _("not reached"));
2593 /* The outer level of a two-level lexer. This calls the inner lexer
2594 to return tokens. It then either returns these tokens, or
2595 aggregates them into a larger token. This lets us work around a
2596 problem in our parsing approach, where the parser could not
2597 distinguish between qualified names and qualified types at the
2600 This approach is still not ideal, because it mishandles template
2601 types. See the comment in lex_one_token for an example. However,
2602 this is still an improvement over the earlier approach, and will
2603 suffice until we move to better parsing technology. */
2607 token_and_value current;
2608 int first_was_coloncolon, last_was_coloncolon, first_iter;
2610 if (popping && !VEC_empty (token_and_value, token_fifo))
2612 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2613 VEC_ordered_remove (token_and_value, token_fifo, 0);
2619 current.token = lex_one_token ();
2620 if (current.token == NAME)
2621 current.token = classify_name (expression_context_block);
2622 if (parse_language->la_language != language_cplus
2623 || (current.token != TYPENAME && current.token != COLONCOLON))
2624 return current.token;
2626 first_was_coloncolon = current.token == COLONCOLON;
2627 last_was_coloncolon = first_was_coloncolon;
2628 obstack_free (&name_obstack, obstack_base (&name_obstack));
2629 if (!last_was_coloncolon)
2630 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2631 current.value = yylval;
2635 token_and_value next;
2637 next.token = lex_one_token ();
2638 next.value = yylval;
2640 if (next.token == NAME && last_was_coloncolon)
2644 classification = classify_inner_name (first_was_coloncolon
2646 : expression_context_block,
2648 /* We keep going until we either run out of names, or until
2649 we have a qualified name which is not a type. */
2650 if (classification != TYPENAME && classification != NAME)
2652 /* Push the final component and leave the loop. */
2653 VEC_safe_push (token_and_value, token_fifo, &next);
2657 /* Update the partial name we are constructing. */
2660 /* We don't want to put a leading "::" into the name. */
2661 obstack_grow_str (&name_obstack, "::");
2663 obstack_grow (&name_obstack, next.value.sval.ptr,
2664 next.value.sval.length);
2666 yylval.sval.ptr = obstack_base (&name_obstack);
2667 yylval.sval.length = obstack_object_size (&name_obstack);
2668 current.value = yylval;
2669 current.token = classification;
2671 last_was_coloncolon = 0;
2673 else if (next.token == COLONCOLON && !last_was_coloncolon)
2674 last_was_coloncolon = 1;
2677 /* We've reached the end of the name. */
2678 VEC_safe_push (token_and_value, token_fifo, &next);
2687 /* If we ended with a "::", insert it too. */
2688 if (last_was_coloncolon)
2691 memset (&cc, 0, sizeof (token_and_value));
2692 if (first_was_coloncolon && first_iter)
2697 cc.token = COLONCOLON;
2698 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2701 yylval = current.value;
2702 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2704 yylval.sval.length);
2705 return current.token;
2712 struct cleanup *back_to = make_cleanup (free_current_contents,
2713 &expression_macro_scope);
2715 /* Set up the scope for macro expansion. */
2716 expression_macro_scope = NULL;
2718 if (expression_context_block)
2719 expression_macro_scope
2720 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2722 expression_macro_scope = default_macro_scope ();
2723 if (! expression_macro_scope)
2724 expression_macro_scope = user_macro_scope ();
2726 /* Initialize macro expansion code. */
2727 obstack_init (&expansion_obstack);
2728 gdb_assert (! macro_original_text);
2729 make_cleanup (scan_macro_cleanup, 0);
2731 make_cleanup_restore_integer (&yydebug);
2732 yydebug = parser_debug;
2734 /* Initialize some state used by the lexer. */
2735 last_was_structop = 0;
2736 saw_name_at_eof = 0;
2738 VEC_free (token_and_value, token_fifo);
2740 obstack_init (&name_obstack);
2741 make_cleanup_obstack_free (&name_obstack);
2743 result = yyparse ();
2744 do_cleanups (back_to);
2753 lexptr = prev_lexptr;
2755 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);