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;
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
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 write_exp_elt_opcode (TYPE_INSTANCE);
446 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
447 for (i = 0; i < $<ivec>3[0]; ++i)
448 write_exp_elt_type ($<tvec>3[i + 1]);
449 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
450 write_exp_elt_opcode (TYPE_INSTANCE);
456 { $$ = end_arglist () - 1; }
458 exp : lcurly arglist rcurly %prec ARROW
459 { write_exp_elt_opcode (OP_ARRAY);
460 write_exp_elt_longcst ((LONGEST) 0);
461 write_exp_elt_longcst ((LONGEST) $3);
462 write_exp_elt_opcode (OP_ARRAY); }
465 exp : lcurly type rcurly exp %prec UNARY
466 { write_exp_elt_opcode (UNOP_MEMVAL);
467 write_exp_elt_type ($2);
468 write_exp_elt_opcode (UNOP_MEMVAL); }
471 exp : '(' type ')' exp %prec UNARY
472 { write_exp_elt_opcode (UNOP_CAST);
473 write_exp_elt_type ($2);
474 write_exp_elt_opcode (UNOP_CAST); }
481 /* Binary operators in order of decreasing precedence. */
484 { write_exp_elt_opcode (BINOP_REPEAT); }
488 { write_exp_elt_opcode (BINOP_MUL); }
492 { write_exp_elt_opcode (BINOP_DIV); }
496 { write_exp_elt_opcode (BINOP_REM); }
500 { write_exp_elt_opcode (BINOP_ADD); }
504 { write_exp_elt_opcode (BINOP_SUB); }
508 { write_exp_elt_opcode (BINOP_LSH); }
512 { write_exp_elt_opcode (BINOP_RSH); }
516 { write_exp_elt_opcode (BINOP_EQUAL); }
519 exp : exp NOTEQUAL exp
520 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
524 { write_exp_elt_opcode (BINOP_LEQ); }
528 { write_exp_elt_opcode (BINOP_GEQ); }
532 { write_exp_elt_opcode (BINOP_LESS); }
536 { write_exp_elt_opcode (BINOP_GTR); }
540 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
544 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
548 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
552 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
556 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
559 exp : exp '?' exp ':' exp %prec '?'
560 { write_exp_elt_opcode (TERNOP_COND); }
564 { write_exp_elt_opcode (BINOP_ASSIGN); }
567 exp : exp ASSIGN_MODIFY exp
568 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
569 write_exp_elt_opcode ($2);
570 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
574 { write_exp_elt_opcode (OP_LONG);
575 write_exp_elt_type ($1.type);
576 write_exp_elt_longcst ((LONGEST)($1.val));
577 write_exp_elt_opcode (OP_LONG); }
582 struct stoken_vector vec;
585 write_exp_string_vector ($1.type, &vec);
591 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
592 write_exp_elt_opcode (OP_LONG);
593 write_exp_elt_type (val.typed_val_int.type);
594 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
595 write_exp_elt_opcode (OP_LONG);
601 { write_exp_elt_opcode (OP_DOUBLE);
602 write_exp_elt_type ($1.type);
603 write_exp_elt_dblcst ($1.dval);
604 write_exp_elt_opcode (OP_DOUBLE); }
608 { write_exp_elt_opcode (OP_DECFLOAT);
609 write_exp_elt_type ($1.type);
610 write_exp_elt_decfloatcst ($1.val);
611 write_exp_elt_opcode (OP_DECFLOAT); }
619 write_dollar_variable ($1);
623 exp : SIZEOF '(' type ')' %prec UNARY
624 { write_exp_elt_opcode (OP_LONG);
625 write_exp_elt_type (lookup_signed_typename
626 (parse_language, parse_gdbarch,
629 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
630 write_exp_elt_opcode (OP_LONG); }
633 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
634 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
635 write_exp_elt_type ($3);
636 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
639 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
640 { write_exp_elt_opcode (UNOP_CAST);
641 write_exp_elt_type ($3);
642 write_exp_elt_opcode (UNOP_CAST); }
645 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
646 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
647 write_exp_elt_type ($3);
648 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
651 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
652 { /* We could do more error checking here, but
653 it doesn't seem worthwhile. */
654 write_exp_elt_opcode (UNOP_CAST);
655 write_exp_elt_type ($3);
656 write_exp_elt_opcode (UNOP_CAST); }
662 /* We copy the string here, and not in the
663 lexer, to guarantee that we do not leak a
664 string. Note that we follow the
665 NUL-termination convention of the
667 struct typed_stoken *vec = XNEW (struct typed_stoken);
672 vec->length = $1.length;
673 vec->ptr = malloc ($1.length + 1);
674 memcpy (vec->ptr, $1.ptr, $1.length + 1);
679 /* Note that we NUL-terminate here, but just
683 $$.tokens = realloc ($$.tokens,
684 $$.len * sizeof (struct typed_stoken));
686 p = malloc ($2.length + 1);
687 memcpy (p, $2.ptr, $2.length + 1);
689 $$.tokens[$$.len - 1].type = $2.type;
690 $$.tokens[$$.len - 1].length = $2.length;
691 $$.tokens[$$.len - 1].ptr = p;
698 enum c_string_type type = C_STRING;
700 for (i = 0; i < $1.len; ++i)
702 switch ($1.tokens[i].type)
710 && type != $1.tokens[i].type)
711 error (_("Undefined string concatenation."));
712 type = $1.tokens[i].type;
716 internal_error (__FILE__, __LINE__,
717 "unrecognized type in string concatenation");
721 write_exp_string_vector (type, &$1);
722 for (i = 0; i < $1.len; ++i)
723 free ($1.tokens[i].ptr);
730 { write_exp_elt_opcode (OP_LONG);
731 write_exp_elt_type (parse_type->builtin_bool);
732 write_exp_elt_longcst ((LONGEST) 1);
733 write_exp_elt_opcode (OP_LONG); }
737 { write_exp_elt_opcode (OP_LONG);
738 write_exp_elt_type (parse_type->builtin_bool);
739 write_exp_elt_longcst ((LONGEST) 0);
740 write_exp_elt_opcode (OP_LONG); }
748 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
750 error (_("No file or function \"%s\"."),
751 copy_name ($1.stoken));
759 block : block COLONCOLON name
761 = lookup_symbol (copy_name ($3), $1,
762 VAR_DOMAIN, (int *) NULL);
763 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
764 error (_("No function \"%s\" in specified context."),
766 $$ = SYMBOL_BLOCK_VALUE (tem); }
769 variable: name_not_typename ENTRY
770 { struct symbol *sym = $1.sym;
772 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
773 || !symbol_read_needs_frame (sym))
774 error (_("@entry can be used only for function "
775 "parameters, not for \"%s\""),
776 copy_name ($1.stoken));
778 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
779 write_exp_elt_sym (sym);
780 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
784 variable: block COLONCOLON name
785 { struct symbol *sym;
786 sym = lookup_symbol (copy_name ($3), $1,
787 VAR_DOMAIN, (int *) NULL);
789 error (_("No symbol \"%s\" in specified context."),
791 if (symbol_read_needs_frame (sym))
793 if (innermost_block == 0
794 || contained_in (block_found,
796 innermost_block = block_found;
799 write_exp_elt_opcode (OP_VAR_VALUE);
800 /* block_found is set by lookup_symbol. */
801 write_exp_elt_block (block_found);
802 write_exp_elt_sym (sym);
803 write_exp_elt_opcode (OP_VAR_VALUE); }
806 qualified_name: TYPENAME COLONCOLON name
808 struct type *type = $1.type;
809 CHECK_TYPEDEF (type);
810 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
811 && TYPE_CODE (type) != TYPE_CODE_UNION
812 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
813 error (_("`%s' is not defined as an aggregate type."),
816 write_exp_elt_opcode (OP_SCOPE);
817 write_exp_elt_type (type);
818 write_exp_string ($3);
819 write_exp_elt_opcode (OP_SCOPE);
821 | TYPENAME COLONCOLON '~' name
823 struct type *type = $1.type;
824 struct stoken tmp_token;
825 CHECK_TYPEDEF (type);
826 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
827 && TYPE_CODE (type) != TYPE_CODE_UNION
828 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
829 error (_("`%s' is not defined as an aggregate type."),
832 tmp_token.ptr = (char*) alloca ($4.length + 2);
833 tmp_token.length = $4.length + 1;
834 tmp_token.ptr[0] = '~';
835 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
836 tmp_token.ptr[tmp_token.length] = 0;
838 /* Check for valid destructor name. */
839 destructor_name_p (tmp_token.ptr, $1.type);
840 write_exp_elt_opcode (OP_SCOPE);
841 write_exp_elt_type (type);
842 write_exp_string (tmp_token);
843 write_exp_elt_opcode (OP_SCOPE);
845 | TYPENAME COLONCOLON name COLONCOLON name
847 char *copy = copy_name ($3);
848 error (_("No type \"%s\" within class "
849 "or namespace \"%s\"."),
850 copy, TYPE_NAME ($1.type));
854 variable: qualified_name
855 | COLONCOLON name_not_typename
857 char *name = copy_name ($2.stoken);
859 struct minimal_symbol *msymbol;
862 lookup_symbol (name, (const struct block *) NULL,
863 VAR_DOMAIN, (int *) NULL);
866 write_exp_elt_opcode (OP_VAR_VALUE);
867 write_exp_elt_block (NULL);
868 write_exp_elt_sym (sym);
869 write_exp_elt_opcode (OP_VAR_VALUE);
873 msymbol = lookup_minimal_symbol (name, NULL, NULL);
875 write_exp_msymbol (msymbol);
876 else if (!have_full_symbols () && !have_partial_symbols ())
877 error (_("No symbol table is loaded. Use the \"file\" command."));
879 error (_("No symbol \"%s\" in current context."), name);
883 variable: name_not_typename
884 { struct symbol *sym = $1.sym;
888 if (symbol_read_needs_frame (sym))
890 if (innermost_block == 0
891 || contained_in (block_found,
893 innermost_block = block_found;
896 write_exp_elt_opcode (OP_VAR_VALUE);
897 /* We want to use the selected frame, not
898 another more inner frame which happens to
899 be in the same block. */
900 write_exp_elt_block (NULL);
901 write_exp_elt_sym (sym);
902 write_exp_elt_opcode (OP_VAR_VALUE);
904 else if ($1.is_a_field_of_this)
906 /* C++: it hangs off of `this'. Must
907 not inadvertently convert from a method call
909 if (innermost_block == 0
910 || contained_in (block_found,
912 innermost_block = block_found;
913 write_exp_elt_opcode (OP_THIS);
914 write_exp_elt_opcode (OP_THIS);
915 write_exp_elt_opcode (STRUCTOP_PTR);
916 write_exp_string ($1.stoken);
917 write_exp_elt_opcode (STRUCTOP_PTR);
921 struct minimal_symbol *msymbol;
922 char *arg = copy_name ($1.stoken);
925 lookup_minimal_symbol (arg, NULL, NULL);
927 write_exp_msymbol (msymbol);
928 else if (!have_full_symbols () && !have_partial_symbols ())
929 error (_("No symbol table is loaded. Use the \"file\" command."));
931 error (_("No symbol \"%s\" in current context."),
932 copy_name ($1.stoken));
937 space_identifier : '@' NAME
938 { insert_type_address_space (copy_name ($2.stoken)); }
941 const_or_volatile: const_or_volatile_noopt
945 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
948 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
949 | const_or_volatile_noopt
952 const_or_volatile_or_space_identifier:
953 const_or_volatile_or_space_identifier_noopt
959 { insert_type (tp_pointer); }
960 const_or_volatile_or_space_identifier
962 { insert_type (tp_pointer); }
963 const_or_volatile_or_space_identifier
965 { insert_type (tp_reference); }
967 { insert_type (tp_reference); }
970 ptr_operator_ts: ptr_operator
972 $$ = get_type_stack ();
973 /* This cleanup is eventually run by
975 make_cleanup (type_stack_cleanup, $$);
979 abs_decl: ptr_operator_ts direct_abs_decl
980 { $$ = append_type_stack ($2, $1); }
985 direct_abs_decl: '(' abs_decl ')'
987 | direct_abs_decl array_mod
989 push_type_stack ($1);
991 push_type (tp_array);
992 $$ = get_type_stack ();
997 push_type (tp_array);
998 $$ = get_type_stack ();
1001 | direct_abs_decl func_mod
1003 push_type_stack ($1);
1004 push_type (tp_function);
1005 $$ = get_type_stack ();
1009 push_type (tp_function);
1010 $$ = get_type_stack ();
1021 | '(' nonempty_typelist ')'
1025 /* We used to try to recognize pointer to member types here, but
1026 that didn't work (shift/reduce conflicts meant that these rules never
1027 got executed). The problem is that
1028 int (foo::bar::baz::bizzle)
1029 is a function type but
1030 int (foo::bar::baz::bizzle::*)
1031 is a pointer to member type. Stroustrup loses again! */
1036 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1040 { $$ = lookup_signed_typename (parse_language,
1044 { $$ = lookup_signed_typename (parse_language,
1048 { $$ = lookup_signed_typename (parse_language,
1052 { $$ = lookup_signed_typename (parse_language,
1055 | LONG SIGNED_KEYWORD INT_KEYWORD
1056 { $$ = lookup_signed_typename (parse_language,
1059 | LONG SIGNED_KEYWORD
1060 { $$ = lookup_signed_typename (parse_language,
1063 | SIGNED_KEYWORD LONG INT_KEYWORD
1064 { $$ = lookup_signed_typename (parse_language,
1067 | UNSIGNED LONG INT_KEYWORD
1068 { $$ = lookup_unsigned_typename (parse_language,
1071 | LONG UNSIGNED INT_KEYWORD
1072 { $$ = lookup_unsigned_typename (parse_language,
1076 { $$ = lookup_unsigned_typename (parse_language,
1080 { $$ = lookup_signed_typename (parse_language,
1083 | LONG LONG INT_KEYWORD
1084 { $$ = lookup_signed_typename (parse_language,
1087 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1088 { $$ = lookup_signed_typename (parse_language,
1091 | LONG LONG SIGNED_KEYWORD
1092 { $$ = lookup_signed_typename (parse_language,
1095 | SIGNED_KEYWORD LONG LONG
1096 { $$ = lookup_signed_typename (parse_language,
1099 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1100 { $$ = lookup_signed_typename (parse_language,
1103 | UNSIGNED LONG LONG
1104 { $$ = lookup_unsigned_typename (parse_language,
1107 | UNSIGNED LONG LONG INT_KEYWORD
1108 { $$ = lookup_unsigned_typename (parse_language,
1111 | LONG LONG UNSIGNED
1112 { $$ = lookup_unsigned_typename (parse_language,
1115 | LONG LONG UNSIGNED INT_KEYWORD
1116 { $$ = lookup_unsigned_typename (parse_language,
1120 { $$ = lookup_signed_typename (parse_language,
1123 | SHORT SIGNED_KEYWORD INT_KEYWORD
1124 { $$ = lookup_signed_typename (parse_language,
1127 | SHORT SIGNED_KEYWORD
1128 { $$ = lookup_signed_typename (parse_language,
1131 | UNSIGNED SHORT INT_KEYWORD
1132 { $$ = lookup_unsigned_typename (parse_language,
1136 { $$ = lookup_unsigned_typename (parse_language,
1139 | SHORT UNSIGNED INT_KEYWORD
1140 { $$ = lookup_unsigned_typename (parse_language,
1144 { $$ = lookup_typename (parse_language, parse_gdbarch,
1145 "double", (struct block *) NULL,
1147 | LONG DOUBLE_KEYWORD
1148 { $$ = lookup_typename (parse_language, parse_gdbarch,
1150 (struct block *) NULL, 0); }
1152 { $$ = lookup_struct (copy_name ($2),
1153 expression_context_block); }
1155 { $$ = lookup_struct (copy_name ($2),
1156 expression_context_block); }
1158 { $$ = lookup_union (copy_name ($2),
1159 expression_context_block); }
1161 { $$ = lookup_enum (copy_name ($2),
1162 expression_context_block); }
1164 { $$ = lookup_unsigned_typename (parse_language,
1166 TYPE_NAME($2.type)); }
1168 { $$ = lookup_unsigned_typename (parse_language,
1171 | SIGNED_KEYWORD typename
1172 { $$ = lookup_signed_typename (parse_language,
1174 TYPE_NAME($2.type)); }
1176 { $$ = lookup_signed_typename (parse_language,
1179 /* It appears that this rule for templates is never
1180 reduced; template recognition happens by lookahead
1181 in the token processing code in yylex. */
1182 | TEMPLATE name '<' type '>'
1183 { $$ = lookup_template_type(copy_name($2), $4,
1184 expression_context_block);
1186 | const_or_volatile_or_space_identifier_noopt typebase
1187 { $$ = follow_types ($2); }
1188 | typebase const_or_volatile_or_space_identifier_noopt
1189 { $$ = follow_types ($1); }
1195 $$.stoken.ptr = "int";
1196 $$.stoken.length = 3;
1197 $$.type = lookup_signed_typename (parse_language,
1203 $$.stoken.ptr = "long";
1204 $$.stoken.length = 4;
1205 $$.type = lookup_signed_typename (parse_language,
1211 $$.stoken.ptr = "short";
1212 $$.stoken.length = 5;
1213 $$.type = lookup_signed_typename (parse_language,
1221 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1222 $<ivec>$[0] = 1; /* Number of types in vector */
1225 | nonempty_typelist ',' type
1226 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1227 $$ = (struct type **) realloc ((char *) $1, len);
1228 $$[$<ivec>$[0]] = $3;
1235 push_type_stack ($2);
1236 $$ = follow_types ($1);
1240 conversion_type_id: typebase conversion_declarator
1241 { $$ = follow_types ($1); }
1244 conversion_declarator: /* Nothing. */
1245 | ptr_operator conversion_declarator
1248 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1249 | VOLATILE_KEYWORD CONST_KEYWORD
1252 const_or_volatile_noopt: const_and_volatile
1253 { insert_type (tp_const);
1254 insert_type (tp_volatile);
1257 { insert_type (tp_const); }
1259 { insert_type (tp_volatile); }
1262 operator: OPERATOR NEW
1263 { $$ = operator_stoken (" new"); }
1265 { $$ = operator_stoken (" delete"); }
1266 | OPERATOR NEW '[' ']'
1267 { $$ = operator_stoken (" new[]"); }
1268 | OPERATOR DELETE '[' ']'
1269 { $$ = operator_stoken (" delete[]"); }
1271 { $$ = operator_stoken ("+"); }
1273 { $$ = operator_stoken ("-"); }
1275 { $$ = operator_stoken ("*"); }
1277 { $$ = operator_stoken ("/"); }
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 (">"); }
1296 | OPERATOR ASSIGN_MODIFY
1297 { const char *op = "unknown";
1321 case BINOP_BITWISE_IOR:
1324 case BINOP_BITWISE_AND:
1327 case BINOP_BITWISE_XOR:
1334 $$ = operator_stoken (op);
1337 { $$ = operator_stoken ("<<"); }
1339 { $$ = operator_stoken (">>"); }
1341 { $$ = operator_stoken ("=="); }
1343 { $$ = operator_stoken ("!="); }
1345 { $$ = operator_stoken ("<="); }
1347 { $$ = operator_stoken (">="); }
1349 { $$ = operator_stoken ("&&"); }
1351 { $$ = operator_stoken ("||"); }
1352 | OPERATOR INCREMENT
1353 { $$ = operator_stoken ("++"); }
1354 | OPERATOR DECREMENT
1355 { $$ = operator_stoken ("--"); }
1357 { $$ = operator_stoken (","); }
1358 | OPERATOR ARROW_STAR
1359 { $$ = operator_stoken ("->*"); }
1361 { $$ = operator_stoken ("->"); }
1363 { $$ = operator_stoken ("()"); }
1365 { $$ = operator_stoken ("[]"); }
1366 | OPERATOR conversion_type_id
1369 struct ui_file *buf = mem_fileopen ();
1371 c_print_type ($2, NULL, buf, -1, 0);
1372 name = ui_file_xstrdup (buf, &length);
1373 ui_file_delete (buf);
1374 $$ = operator_stoken (name);
1381 name : NAME { $$ = $1.stoken; }
1382 | BLOCKNAME { $$ = $1.stoken; }
1383 | TYPENAME { $$ = $1.stoken; }
1384 | NAME_OR_INT { $$ = $1.stoken; }
1385 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1386 | operator { $$ = $1; }
1389 name_not_typename : NAME
1391 /* These would be useful if name_not_typename was useful, but it is just
1392 a fake for "variable", so these cause reduce/reduce conflicts because
1393 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1394 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1395 context where only a name could occur, this might be useful.
1401 $$.sym = lookup_symbol ($1.ptr,
1402 expression_context_block,
1404 &$$.is_a_field_of_this);
1411 /* Returns a stoken of the operator name given by OP (which does not
1412 include the string "operator"). */
1413 static struct stoken
1414 operator_stoken (const char *op)
1416 static const char *operator_string = "operator";
1417 struct stoken st = { NULL, 0 };
1418 st.length = strlen (operator_string) + strlen (op);
1419 st.ptr = malloc (st.length + 1);
1420 strcpy (st.ptr, operator_string);
1421 strcat (st.ptr, op);
1423 /* The toplevel (c_parse) will free the memory allocated here. */
1424 make_cleanup (free, st.ptr);
1428 /* Take care of parsing a number (anything that starts with a digit).
1429 Set yylval and return the token type; update lexptr.
1430 LEN is the number of characters in it. */
1432 /*** Needs some error checking for the float case ***/
1435 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1437 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1438 here, and we do kind of silly things like cast to unsigned. */
1445 int base = input_radix;
1448 /* Number of "L" suffixes encountered. */
1451 /* We have found a "L" or "U" suffix. */
1452 int found_suffix = 0;
1455 struct type *signed_type;
1456 struct type *unsigned_type;
1460 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1461 point. Return DECFLOAT. */
1463 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1466 putithere->typed_val_decfloat.type
1467 = parse_type->builtin_decfloat;
1468 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1469 gdbarch_byte_order (parse_gdbarch), p);
1474 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1477 putithere->typed_val_decfloat.type
1478 = parse_type->builtin_decdouble;
1479 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1480 gdbarch_byte_order (parse_gdbarch), p);
1485 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1488 putithere->typed_val_decfloat.type
1489 = parse_type->builtin_declong;
1490 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1491 gdbarch_byte_order (parse_gdbarch), p);
1496 if (! parse_c_float (parse_gdbarch, p, len,
1497 &putithere->typed_val_float.dval,
1498 &putithere->typed_val_float.type))
1503 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1547 if (c >= 'A' && c <= 'Z')
1549 if (c != 'l' && c != 'u')
1551 if (c >= '0' && c <= '9')
1559 if (base > 10 && c >= 'a' && c <= 'f')
1563 n += i = c - 'a' + 10;
1576 return ERROR; /* Char not a digit */
1579 return ERROR; /* Invalid digit in this base */
1581 /* Portably test for overflow (only works for nonzero values, so make
1582 a second check for zero). FIXME: Can't we just make n and prevn
1583 unsigned and avoid this? */
1584 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1585 unsigned_p = 1; /* Try something unsigned */
1587 /* Portably test for unsigned overflow.
1588 FIXME: This check is wrong; for example it doesn't find overflow
1589 on 0x123456789 when LONGEST is 32 bits. */
1590 if (c != 'l' && c != 'u' && n != 0)
1592 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1593 error (_("Numeric constant too large."));
1598 /* An integer constant is an int, a long, or a long long. An L
1599 suffix forces it to be long; an LL suffix forces it to be long
1600 long. If not forced to a larger size, it gets the first type of
1601 the above that it fits in. To figure out whether it fits, we
1602 shift it right and see whether anything remains. Note that we
1603 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1604 operation, because many compilers will warn about such a shift
1605 (which always produces a zero result). Sometimes gdbarch_int_bit
1606 or gdbarch_long_bit will be that big, sometimes not. To deal with
1607 the case where it is we just always shift the value more than
1608 once, with fewer bits each time. */
1610 un = (ULONGEST)n >> 2;
1612 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1614 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1616 /* A large decimal (not hex or octal) constant (between INT_MAX
1617 and UINT_MAX) is a long or unsigned long, according to ANSI,
1618 never an unsigned int, but this code treats it as unsigned
1619 int. This probably should be fixed. GCC gives a warning on
1622 unsigned_type = parse_type->builtin_unsigned_int;
1623 signed_type = parse_type->builtin_int;
1625 else if (long_p <= 1
1626 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1628 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1629 unsigned_type = parse_type->builtin_unsigned_long;
1630 signed_type = parse_type->builtin_long;
1635 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1636 < gdbarch_long_long_bit (parse_gdbarch))
1637 /* A long long does not fit in a LONGEST. */
1638 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1640 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1641 high_bit = (ULONGEST) 1 << shift;
1642 unsigned_type = parse_type->builtin_unsigned_long_long;
1643 signed_type = parse_type->builtin_long_long;
1646 putithere->typed_val_int.val = n;
1648 /* If the high bit of the worked out type is set then this number
1649 has to be unsigned. */
1651 if (unsigned_p || (n & high_bit))
1653 putithere->typed_val_int.type = unsigned_type;
1657 putithere->typed_val_int.type = signed_type;
1663 /* Temporary obstack used for holding strings. */
1664 static struct obstack tempbuf;
1665 static int tempbuf_init;
1667 /* Parse a C escape sequence. The initial backslash of the sequence
1668 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1669 last character of the sequence. If OUTPUT is not NULL, the
1670 translated form of the escape sequence will be written there. If
1671 OUTPUT is NULL, no output is written and the call will only affect
1672 *PTR. If an escape sequence is expressed in target bytes, then the
1673 entire sequence will simply be copied to OUTPUT. Return 1 if any
1674 character was emitted, 0 otherwise. */
1677 c_parse_escape (char **ptr, struct obstack *output)
1679 char *tokptr = *ptr;
1682 /* Some escape sequences undergo character set conversion. Those we
1686 /* Hex escapes do not undergo character set conversion, so keep
1687 the escape sequence for later. */
1690 obstack_grow_str (output, "\\x");
1692 if (!isxdigit (*tokptr))
1693 error (_("\\x escape without a following hex digit"));
1694 while (isxdigit (*tokptr))
1697 obstack_1grow (output, *tokptr);
1702 /* Octal escapes do not undergo character set conversion, so
1703 keep the escape sequence for later. */
1715 obstack_grow_str (output, "\\");
1717 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1721 obstack_1grow (output, *tokptr);
1727 /* We handle UCNs later. We could handle them here, but that
1728 would mean a spurious error in the case where the UCN could
1729 be converted to the target charset but not the host
1735 int i, len = c == 'U' ? 8 : 4;
1738 obstack_1grow (output, '\\');
1739 obstack_1grow (output, *tokptr);
1742 if (!isxdigit (*tokptr))
1743 error (_("\\%c escape without a following hex digit"), c);
1744 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1747 obstack_1grow (output, *tokptr);
1753 /* We must pass backslash through so that it does not
1754 cause quoting during the second expansion. */
1757 obstack_grow_str (output, "\\\\");
1761 /* Escapes which undergo conversion. */
1764 obstack_1grow (output, '\a');
1769 obstack_1grow (output, '\b');
1774 obstack_1grow (output, '\f');
1779 obstack_1grow (output, '\n');
1784 obstack_1grow (output, '\r');
1789 obstack_1grow (output, '\t');
1794 obstack_1grow (output, '\v');
1798 /* GCC extension. */
1801 obstack_1grow (output, HOST_ESCAPE_CHAR);
1805 /* Backslash-newline expands to nothing at all. */
1811 /* A few escapes just expand to the character itself. */
1815 /* GCC extensions. */
1820 /* Unrecognized escapes turn into the character itself. */
1823 obstack_1grow (output, *tokptr);
1831 /* Parse a string or character literal from TOKPTR. The string or
1832 character may be wide or unicode. *OUTPTR is set to just after the
1833 end of the literal in the input string. The resulting token is
1834 stored in VALUE. This returns a token value, either STRING or
1835 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1836 number of host characters in the literal. */
1838 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1842 enum c_string_type type;
1844 /* Build the gdb internal form of the input string in tempbuf. Note
1845 that the buffer is null byte terminated *only* for the
1846 convenience of debugging gdb itself and printing the buffer
1847 contents when the buffer contains no embedded nulls. Gdb does
1848 not depend upon the buffer being null byte terminated, it uses
1849 the length string instead. This allows gdb to handle C strings
1850 (as well as strings in other languages) with embedded null
1856 obstack_free (&tempbuf, NULL);
1857 obstack_init (&tempbuf);
1859 /* Record the string type. */
1862 type = C_WIDE_STRING;
1865 else if (*tokptr == 'u')
1870 else if (*tokptr == 'U')
1878 /* Skip the quote. */
1892 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1894 else if (c == quote)
1898 obstack_1grow (&tempbuf, c);
1900 /* FIXME: this does the wrong thing with multi-byte host
1901 characters. We could use mbrlen here, but that would
1902 make "set host-charset" a bit less useful. */
1907 if (*tokptr != quote)
1910 error (_("Unterminated string in expression."));
1912 error (_("Unmatched single quote."));
1917 value->ptr = obstack_base (&tempbuf);
1918 value->length = obstack_object_size (&tempbuf);
1922 return quote == '"' ? STRING : CHAR;
1929 enum exp_opcode opcode;
1933 static const struct token tokentab3[] =
1935 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1936 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1937 {"->*", ARROW_STAR, BINOP_END, 1}
1940 static const struct token tokentab2[] =
1942 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1943 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1944 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1945 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1946 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1947 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1948 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1949 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1950 {"++", INCREMENT, BINOP_END, 0},
1951 {"--", DECREMENT, BINOP_END, 0},
1952 {"->", ARROW, BINOP_END, 0},
1953 {"&&", ANDAND, BINOP_END, 0},
1954 {"||", OROR, BINOP_END, 0},
1955 /* "::" is *not* only C++: gdb overrides its meaning in several
1956 different ways, e.g., 'filename'::func, function::variable. */
1957 {"::", COLONCOLON, BINOP_END, 0},
1958 {"<<", LSH, BINOP_END, 0},
1959 {">>", RSH, BINOP_END, 0},
1960 {"==", EQUAL, BINOP_END, 0},
1961 {"!=", NOTEQUAL, BINOP_END, 0},
1962 {"<=", LEQ, BINOP_END, 0},
1963 {">=", GEQ, BINOP_END, 0},
1964 {".*", DOT_STAR, BINOP_END, 1}
1967 /* Identifier-like tokens. */
1968 static const struct token ident_tokens[] =
1970 {"unsigned", UNSIGNED, OP_NULL, 0},
1971 {"template", TEMPLATE, OP_NULL, 1},
1972 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1973 {"struct", STRUCT, OP_NULL, 0},
1974 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1975 {"sizeof", SIZEOF, OP_NULL, 0},
1976 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1977 {"false", FALSEKEYWORD, OP_NULL, 1},
1978 {"class", CLASS, OP_NULL, 1},
1979 {"union", UNION, OP_NULL, 0},
1980 {"short", SHORT, OP_NULL, 0},
1981 {"const", CONST_KEYWORD, OP_NULL, 0},
1982 {"enum", ENUM, OP_NULL, 0},
1983 {"long", LONG, OP_NULL, 0},
1984 {"true", TRUEKEYWORD, OP_NULL, 1},
1985 {"int", INT_KEYWORD, OP_NULL, 0},
1986 {"new", NEW, OP_NULL, 1},
1987 {"delete", DELETE, OP_NULL, 1},
1988 {"operator", OPERATOR, OP_NULL, 1},
1990 {"and", ANDAND, BINOP_END, 1},
1991 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1992 {"bitand", '&', OP_NULL, 1},
1993 {"bitor", '|', OP_NULL, 1},
1994 {"compl", '~', OP_NULL, 1},
1995 {"not", '!', OP_NULL, 1},
1996 {"not_eq", NOTEQUAL, BINOP_END, 1},
1997 {"or", OROR, BINOP_END, 1},
1998 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1999 {"xor", '^', OP_NULL, 1},
2000 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
2002 {"const_cast", CONST_CAST, OP_NULL, 1 },
2003 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
2004 {"static_cast", STATIC_CAST, OP_NULL, 1 },
2005 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
2008 /* When we find that lexptr (the global var defined in parse.c) is
2009 pointing at a macro invocation, we expand the invocation, and call
2010 scan_macro_expansion to save the old lexptr here and point lexptr
2011 into the expanded text. When we reach the end of that, we call
2012 end_macro_expansion to pop back to the value we saved here. The
2013 macro expansion code promises to return only fully-expanded text,
2014 so we don't need to "push" more than one level.
2016 This is disgusting, of course. It would be cleaner to do all macro
2017 expansion beforehand, and then hand that to lexptr. But we don't
2018 really know where the expression ends. Remember, in a command like
2020 (gdb) break *ADDRESS if CONDITION
2022 we evaluate ADDRESS in the scope of the current frame, but we
2023 evaluate CONDITION in the scope of the breakpoint's location. So
2024 it's simply wrong to try to macro-expand the whole thing at once. */
2025 static char *macro_original_text;
2027 /* We save all intermediate macro expansions on this obstack for the
2028 duration of a single parse. The expansion text may sometimes have
2029 to live past the end of the expansion, due to yacc lookahead.
2030 Rather than try to be clever about saving the data for a single
2031 token, we simply keep it all and delete it after parsing has
2033 static struct obstack expansion_obstack;
2036 scan_macro_expansion (char *expansion)
2040 /* We'd better not be trying to push the stack twice. */
2041 gdb_assert (! macro_original_text);
2043 /* Copy to the obstack, and then free the intermediate
2045 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2048 /* Save the old lexptr value, so we can return to it when we're done
2049 parsing the expanded text. */
2050 macro_original_text = lexptr;
2056 scanning_macro_expansion (void)
2058 return macro_original_text != 0;
2063 finished_macro_expansion (void)
2065 /* There'd better be something to pop back to. */
2066 gdb_assert (macro_original_text);
2068 /* Pop back to the original text. */
2069 lexptr = macro_original_text;
2070 macro_original_text = 0;
2075 scan_macro_cleanup (void *dummy)
2077 if (macro_original_text)
2078 finished_macro_expansion ();
2080 obstack_free (&expansion_obstack, NULL);
2083 /* Return true iff the token represents a C++ cast operator. */
2086 is_cast_operator (const char *token, int len)
2088 return (! strncmp (token, "dynamic_cast", len)
2089 || ! strncmp (token, "static_cast", len)
2090 || ! strncmp (token, "reinterpret_cast", len)
2091 || ! strncmp (token, "const_cast", len));
2094 /* The scope used for macro expansion. */
2095 static struct macro_scope *expression_macro_scope;
2097 /* This is set if a NAME token appeared at the very end of the input
2098 string, with no whitespace separating the name from the EOF. This
2099 is used only when parsing to do field name completion. */
2100 static int saw_name_at_eof;
2102 /* This is set if the previously-returned token was a structure
2103 operator -- either '.' or ARROW. This is used only when parsing to
2104 do field name completion. */
2105 static int last_was_structop;
2107 /* Read one token, getting characters through lexptr. */
2110 lex_one_token (void)
2116 int saw_structop = last_was_structop;
2119 last_was_structop = 0;
2123 /* Check if this is a macro invocation that we need to expand. */
2124 if (! scanning_macro_expansion ())
2126 char *expanded = macro_expand_next (&lexptr,
2127 standard_macro_lookup,
2128 expression_macro_scope);
2131 scan_macro_expansion (expanded);
2134 prev_lexptr = lexptr;
2137 /* See if it is a special token of length 3. */
2138 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2139 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2141 if (tokentab3[i].cxx_only
2142 && parse_language->la_language != language_cplus)
2146 yylval.opcode = tokentab3[i].opcode;
2147 return tokentab3[i].token;
2150 /* See if it is a special token of length 2. */
2151 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2152 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2154 if (tokentab2[i].cxx_only
2155 && parse_language->la_language != language_cplus)
2159 yylval.opcode = tokentab2[i].opcode;
2160 if (in_parse_field && tokentab2[i].token == ARROW)
2161 last_was_structop = 1;
2162 return tokentab2[i].token;
2165 switch (c = *tokstart)
2168 /* If we were just scanning the result of a macro expansion,
2169 then we need to resume scanning the original text.
2170 If we're parsing for field name completion, and the previous
2171 token allows such completion, return a COMPLETE token.
2172 Otherwise, we were already scanning the original text, and
2173 we're really done. */
2174 if (scanning_macro_expansion ())
2176 finished_macro_expansion ();
2179 else if (saw_name_at_eof)
2181 saw_name_at_eof = 0;
2184 else if (saw_structop)
2203 if (paren_depth == 0)
2210 if (comma_terminates
2212 && ! scanning_macro_expansion ())
2218 /* Might be a floating point number. */
2219 if (lexptr[1] < '0' || lexptr[1] > '9')
2222 last_was_structop = 1;
2223 goto symbol; /* Nope, must be a symbol. */
2225 /* FALL THRU into number case. */
2238 /* It's a number. */
2239 int got_dot = 0, got_e = 0, toktype;
2241 int hex = input_radix > 10;
2243 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2248 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2256 /* This test includes !hex because 'e' is a valid hex digit
2257 and thus does not indicate a floating point number when
2258 the radix is hex. */
2259 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2260 got_dot = got_e = 1;
2261 /* This test does not include !hex, because a '.' always indicates
2262 a decimal floating point number regardless of the radix. */
2263 else if (!got_dot && *p == '.')
2265 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2266 && (*p == '-' || *p == '+'))
2267 /* This is the sign of the exponent, not the end of the
2270 /* We will take any letters or digits. parse_number will
2271 complain if past the radix, or if L or U are not final. */
2272 else if ((*p < '0' || *p > '9')
2273 && ((*p < 'a' || *p > 'z')
2274 && (*p < 'A' || *p > 'Z')))
2277 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2278 if (toktype == ERROR)
2280 char *err_copy = (char *) alloca (p - tokstart + 1);
2282 memcpy (err_copy, tokstart, p - tokstart);
2283 err_copy[p - tokstart] = 0;
2284 error (_("Invalid number \"%s\"."), err_copy);
2292 char *p = &tokstart[1];
2293 size_t len = strlen ("entry");
2295 while (isspace (*p))
2297 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2329 if (tokstart[1] != '"' && tokstart[1] != '\'')
2336 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2341 error (_("Empty character constant."));
2342 else if (host_len > 2 && c == '\'')
2345 namelen = lexptr - tokstart - 1;
2348 else if (host_len > 1)
2349 error (_("Invalid character constant."));
2355 if (!(c == '_' || c == '$'
2356 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2357 /* We must have come across a bad character (e.g. ';'). */
2358 error (_("Invalid character '%c' in expression."), c);
2360 /* It's a name. See how long it is. */
2362 for (c = tokstart[namelen];
2363 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2364 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2366 /* Template parameter lists are part of the name.
2367 FIXME: This mishandles `print $a<4&&$a>3'. */
2371 if (! is_cast_operator (tokstart, namelen))
2373 /* Scan ahead to get rest of the template specification. Note
2374 that we look ahead only when the '<' adjoins non-whitespace
2375 characters; for comparison expressions, e.g. "a < b > c",
2376 there must be spaces before the '<', etc. */
2378 char * p = find_template_name_end (tokstart + namelen);
2380 namelen = p - tokstart;
2384 c = tokstart[++namelen];
2387 /* The token "if" terminates the expression and is NOT removed from
2388 the input stream. It doesn't count if it appears in the
2389 expansion of a macro. */
2391 && tokstart[0] == 'i'
2392 && tokstart[1] == 'f'
2393 && ! scanning_macro_expansion ())
2398 /* For the same reason (breakpoint conditions), "thread N"
2399 terminates the expression. "thread" could be an identifier, but
2400 an identifier is never followed by a number without intervening
2401 punctuation. "task" is similar. Handle abbreviations of these,
2402 similarly to breakpoint.c:find_condition_and_thread. */
2404 && (strncmp (tokstart, "thread", namelen) == 0
2405 || strncmp (tokstart, "task", namelen) == 0)
2406 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2407 && ! scanning_macro_expansion ())
2409 char *p = tokstart + namelen + 1;
2410 while (*p == ' ' || *p == '\t')
2412 if (*p >= '0' && *p <= '9')
2420 yylval.sval.ptr = tokstart;
2421 yylval.sval.length = namelen;
2423 /* Catch specific keywords. */
2424 copy = copy_name (yylval.sval);
2425 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2426 if (strcmp (copy, ident_tokens[i].operator) == 0)
2428 if (ident_tokens[i].cxx_only
2429 && parse_language->la_language != language_cplus)
2432 /* It is ok to always set this, even though we don't always
2433 strictly need to. */
2434 yylval.opcode = ident_tokens[i].opcode;
2435 return ident_tokens[i].token;
2438 if (*tokstart == '$')
2441 if (in_parse_field && *lexptr == '\0')
2442 saw_name_at_eof = 1;
2446 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2453 DEF_VEC_O (token_and_value);
2455 /* A FIFO of tokens that have been read but not yet returned to the
2457 static VEC (token_and_value) *token_fifo;
2459 /* Non-zero if the lexer should return tokens from the FIFO. */
2462 /* Temporary storage for c_lex; this holds symbol names as they are
2464 static struct obstack name_obstack;
2466 /* Classify a NAME token. The contents of the token are in `yylval'.
2467 Updates yylval and returns the new token type. BLOCK is the block
2468 in which lookups start; this can be NULL to mean the global
2471 classify_name (struct block *block)
2475 int is_a_field_of_this = 0;
2477 copy = copy_name (yylval.sval);
2479 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2480 parse_language->la_language == language_cplus
2481 ? &is_a_field_of_this : (int *) NULL);
2483 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2485 yylval.ssym.sym = sym;
2486 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2491 /* See if it's a file name. */
2492 struct symtab *symtab;
2494 symtab = lookup_symtab (copy);
2497 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2502 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2504 yylval.tsym.type = SYMBOL_TYPE (sym);
2509 = language_lookup_primitive_type_by_name (parse_language,
2510 parse_gdbarch, copy);
2511 if (yylval.tsym.type != NULL)
2514 /* Input names that aren't symbols but ARE valid hex numbers, when
2515 the input radix permits them, can be names or numbers depending
2516 on the parse. Note we support radixes > 16 here. */
2518 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2519 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2521 YYSTYPE newlval; /* Its value is ignored. */
2522 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2525 yylval.ssym.sym = sym;
2526 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2531 /* Any other kind of symbol */
2532 yylval.ssym.sym = sym;
2533 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2536 && parse_language->la_language == language_cplus
2537 && !is_a_field_of_this
2538 && !lookup_minimal_symbol (copy, NULL, NULL))
2539 return UNKNOWN_CPP_NAME;
2544 /* Like classify_name, but used by the inner loop of the lexer, when a
2545 name might have already been seen. FIRST_NAME is true if the token
2546 in `yylval' is the first component of a name, false otherwise. */
2549 classify_inner_name (struct block *block, int first_name)
2551 struct type *type, *new_type;
2555 return classify_name (block);
2557 type = check_typedef (yylval.tsym.type);
2558 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2559 && TYPE_CODE (type) != TYPE_CODE_UNION
2560 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2563 copy = copy_name (yylval.tsym.stoken);
2564 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2565 if (yylval.ssym.sym == NULL)
2568 switch (SYMBOL_CLASS (yylval.ssym.sym))
2575 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2579 yylval.ssym.is_a_field_of_this = 0;
2582 internal_error (__FILE__, __LINE__, _("not reached"));
2585 /* The outer level of a two-level lexer. This calls the inner lexer
2586 to return tokens. It then either returns these tokens, or
2587 aggregates them into a larger token. This lets us work around a
2588 problem in our parsing approach, where the parser could not
2589 distinguish between qualified names and qualified types at the
2592 This approach is still not ideal, because it mishandles template
2593 types. See the comment in lex_one_token for an example. However,
2594 this is still an improvement over the earlier approach, and will
2595 suffice until we move to better parsing technology. */
2599 token_and_value current;
2600 int first_was_coloncolon, last_was_coloncolon, first_iter;
2602 if (popping && !VEC_empty (token_and_value, token_fifo))
2604 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2605 VEC_ordered_remove (token_and_value, token_fifo, 0);
2611 current.token = lex_one_token ();
2612 if (current.token == NAME)
2613 current.token = classify_name (expression_context_block);
2614 if (parse_language->la_language != language_cplus
2615 || (current.token != TYPENAME && current.token != COLONCOLON))
2616 return current.token;
2618 first_was_coloncolon = current.token == COLONCOLON;
2619 last_was_coloncolon = first_was_coloncolon;
2620 obstack_free (&name_obstack, obstack_base (&name_obstack));
2621 if (!last_was_coloncolon)
2622 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2623 current.value = yylval;
2627 token_and_value next;
2629 next.token = lex_one_token ();
2630 next.value = yylval;
2632 if (next.token == NAME && last_was_coloncolon)
2636 classification = classify_inner_name (first_was_coloncolon
2638 : expression_context_block,
2640 /* We keep going until we either run out of names, or until
2641 we have a qualified name which is not a type. */
2642 if (classification != TYPENAME && classification != NAME)
2644 /* Push the final component and leave the loop. */
2645 VEC_safe_push (token_and_value, token_fifo, &next);
2649 /* Update the partial name we are constructing. */
2652 /* We don't want to put a leading "::" into the name. */
2653 obstack_grow_str (&name_obstack, "::");
2655 obstack_grow (&name_obstack, next.value.sval.ptr,
2656 next.value.sval.length);
2658 yylval.sval.ptr = obstack_base (&name_obstack);
2659 yylval.sval.length = obstack_object_size (&name_obstack);
2660 current.value = yylval;
2661 current.token = classification;
2663 last_was_coloncolon = 0;
2665 else if (next.token == COLONCOLON && !last_was_coloncolon)
2666 last_was_coloncolon = 1;
2669 /* We've reached the end of the name. */
2670 VEC_safe_push (token_and_value, token_fifo, &next);
2679 /* If we ended with a "::", insert it too. */
2680 if (last_was_coloncolon)
2683 memset (&cc, 0, sizeof (token_and_value));
2684 if (first_was_coloncolon && first_iter)
2689 cc.token = COLONCOLON;
2690 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2693 yylval = current.value;
2694 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2696 yylval.sval.length);
2697 return current.token;
2704 struct cleanup *back_to = make_cleanup (free_current_contents,
2705 &expression_macro_scope);
2707 /* Set up the scope for macro expansion. */
2708 expression_macro_scope = NULL;
2710 if (expression_context_block)
2711 expression_macro_scope
2712 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2714 expression_macro_scope = default_macro_scope ();
2715 if (! expression_macro_scope)
2716 expression_macro_scope = user_macro_scope ();
2718 /* Initialize macro expansion code. */
2719 obstack_init (&expansion_obstack);
2720 gdb_assert (! macro_original_text);
2721 make_cleanup (scan_macro_cleanup, 0);
2723 make_cleanup_restore_integer (&yydebug);
2724 yydebug = parser_debug;
2726 /* Initialize some state used by the lexer. */
2727 last_was_structop = 0;
2728 saw_name_at_eof = 0;
2730 VEC_free (token_and_value, token_fifo);
2732 obstack_init (&name_obstack);
2733 make_cleanup_obstack_free (&name_obstack);
2735 result = yyparse ();
2736 do_cleanups (back_to);
2745 lexptr = prev_lexptr;
2747 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);