1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989-2000, 2003-2004, 2006-2012 Free Software
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
40 #include "gdb_string.h"
42 #include "expression.h"
44 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #include "cp-support.h"
54 #include "gdb_assert.h"
55 #include "macroscope.h"
57 #define parse_type builtin_type (parse_gdbarch)
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
60 as well as gratuitiously global symbol names, so we can have multiple
61 yacc generated parsers in gdb. Note that these are only the variables
62 produced by yacc. If other parser generators (bison, byacc, etc) produce
63 additional global names that conflict at link time, then those parser
64 generators need to be fixed instead of adding those names to this list. */
66 #define yymaxdepth c_maxdepth
67 #define yyparse c_parse_internal
69 #define yyerror c_error
72 #define yydebug c_debug
81 #define yyerrflag c_errflag
82 #define yynerrs c_nerrs
87 #define yystate c_state
93 #define yyreds c_reds /* With YYDEBUG defined */
94 #define yytoks c_toks /* With YYDEBUG defined */
95 #define yyname c_name /* With YYDEBUG defined */
96 #define yyrule c_rule /* With YYDEBUG defined */
99 #define yydefred c_yydefred
100 #define yydgoto c_yydgoto
101 #define yysindex c_yysindex
102 #define yyrindex c_yyrindex
103 #define yygindex c_yygindex
104 #define yytable c_yytable
105 #define yycheck c_yycheck
107 #define yysslim c_yysslim
108 #define yyssp c_yyssp
109 #define yystacksize c_yystacksize
111 #define yyvsp c_yyvsp
114 #define YYDEBUG 1 /* Default to yydebug support */
117 #define YYFPRINTF parser_fprintf
121 static int yylex (void);
123 void yyerror (char *);
127 /* Although the yacc "value" of an expression is not used,
128 since the result is stored in the structure being created,
129 other node types do have values. */
145 } typed_val_decfloat;
149 struct typed_stoken tsval;
151 struct symtoken ssym;
154 enum exp_opcode opcode;
155 struct internalvar *ivar;
157 struct stoken_vector svec;
163 /* YYSTYPE gets defined by %union */
164 static int parse_number (char *, int, int, YYSTYPE *);
165 static struct stoken operator_stoken (const char *);
168 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
170 %type <tval> type typebase
171 %type <tvec> nonempty_typelist
172 /* %type <bval> block */
174 /* Fancy type parsing. */
175 %type <voidval> func_mod direct_abs_decl abs_decl
177 %type <lval> array_mod
179 %token <typed_val_int> INT
180 %token <typed_val_float> FLOAT
181 %token <typed_val_decfloat> DECFLOAT
183 /* Both NAME and TYPENAME tokens represent symbols in the input,
184 and both convey their data as strings.
185 But a TYPENAME is a string that happens to be defined as a typedef
186 or builtin type name (such as int or char)
187 and a NAME is any other symbol.
188 Contexts where this distinction is not important can use the
189 nonterminal "name", which matches either NAME or TYPENAME. */
191 %token <tsval> STRING
193 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
194 %token <ssym> UNKNOWN_CPP_NAME
195 %token <voidval> COMPLETE
196 %token <tsym> TYPENAME
198 %type <svec> string_exp
199 %type <ssym> name_not_typename
200 %type <tsym> typename
202 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
203 but which would parse as a valid number in the current input radix.
204 E.g. "c" when input_radix==16. Depending on the parse, it will be
205 turned into a name or into a number. */
207 %token <ssym> NAME_OR_INT
210 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
214 %type <sval> operator
215 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
218 /* Special type cases, put in to allow the parser to distinguish different
220 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
222 %token <sval> VARIABLE
224 %token <opcode> ASSIGN_MODIFY
233 %right '=' ASSIGN_MODIFY
241 %left '<' '>' LEQ GEQ
246 %right UNARY INCREMENT DECREMENT
247 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
248 %token <ssym> BLOCKNAME
249 %token <bval> FILENAME
261 { write_exp_elt_opcode(OP_TYPE);
262 write_exp_elt_type($1);
263 write_exp_elt_opcode(OP_TYPE);}
266 /* Expressions, including the comma operator. */
269 { write_exp_elt_opcode (BINOP_COMMA); }
272 /* Expressions, not including the comma operator. */
273 exp : '*' exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_IND); }
277 exp : '&' exp %prec UNARY
278 { write_exp_elt_opcode (UNOP_ADDR); }
281 exp : '-' exp %prec UNARY
282 { write_exp_elt_opcode (UNOP_NEG); }
285 exp : '+' exp %prec UNARY
286 { write_exp_elt_opcode (UNOP_PLUS); }
289 exp : '!' exp %prec UNARY
290 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
293 exp : '~' exp %prec UNARY
294 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
297 exp : INCREMENT exp %prec UNARY
298 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
301 exp : DECREMENT exp %prec UNARY
302 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
305 exp : exp INCREMENT %prec UNARY
306 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
309 exp : exp DECREMENT %prec UNARY
310 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
313 exp : SIZEOF exp %prec UNARY
314 { write_exp_elt_opcode (UNOP_SIZEOF); }
318 { write_exp_elt_opcode (STRUCTOP_PTR);
319 write_exp_string ($3);
320 write_exp_elt_opcode (STRUCTOP_PTR); }
323 exp : exp ARROW name COMPLETE
324 { mark_struct_expression ();
325 write_exp_elt_opcode (STRUCTOP_PTR);
326 write_exp_string ($3);
327 write_exp_elt_opcode (STRUCTOP_PTR); }
330 exp : exp ARROW COMPLETE
332 mark_struct_expression ();
333 write_exp_elt_opcode (STRUCTOP_PTR);
336 write_exp_string (s);
337 write_exp_elt_opcode (STRUCTOP_PTR); }
340 exp : exp ARROW qualified_name
341 { /* exp->type::name becomes exp->*(&type::name) */
342 /* Note: this doesn't work if name is a
343 static member! FIXME */
344 write_exp_elt_opcode (UNOP_ADDR);
345 write_exp_elt_opcode (STRUCTOP_MPTR); }
348 exp : exp ARROW_STAR exp
349 { write_exp_elt_opcode (STRUCTOP_MPTR); }
353 { write_exp_elt_opcode (STRUCTOP_STRUCT);
354 write_exp_string ($3);
355 write_exp_elt_opcode (STRUCTOP_STRUCT); }
358 exp : exp '.' name COMPLETE
359 { mark_struct_expression ();
360 write_exp_elt_opcode (STRUCTOP_STRUCT);
361 write_exp_string ($3);
362 write_exp_elt_opcode (STRUCTOP_STRUCT); }
365 exp : exp '.' COMPLETE
367 mark_struct_expression ();
368 write_exp_elt_opcode (STRUCTOP_STRUCT);
371 write_exp_string (s);
372 write_exp_elt_opcode (STRUCTOP_STRUCT); }
375 exp : exp '.' qualified_name
376 { /* exp.type::name becomes exp.*(&type::name) */
377 /* Note: this doesn't work if name is a
378 static member! FIXME */
379 write_exp_elt_opcode (UNOP_ADDR);
380 write_exp_elt_opcode (STRUCTOP_MEMBER); }
383 exp : exp DOT_STAR exp
384 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
387 exp : exp '[' exp1 ']'
388 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
392 /* This is to save the value of arglist_len
393 being accumulated by an outer function call. */
394 { start_arglist (); }
395 arglist ')' %prec ARROW
396 { write_exp_elt_opcode (OP_FUNCALL);
397 write_exp_elt_longcst ((LONGEST) end_arglist ());
398 write_exp_elt_opcode (OP_FUNCALL); }
401 exp : UNKNOWN_CPP_NAME '('
403 /* This could potentially be a an argument defined
404 lookup function (Koenig). */
405 write_exp_elt_opcode (OP_ADL_FUNC);
406 write_exp_elt_block (expression_context_block);
407 write_exp_elt_sym (NULL); /* Placeholder. */
408 write_exp_string ($1.stoken);
409 write_exp_elt_opcode (OP_ADL_FUNC);
411 /* This is to save the value of arglist_len
412 being accumulated by an outer function call. */
416 arglist ')' %prec ARROW
418 write_exp_elt_opcode (OP_FUNCALL);
419 write_exp_elt_longcst ((LONGEST) end_arglist ());
420 write_exp_elt_opcode (OP_FUNCALL);
425 { start_arglist (); }
435 arglist : arglist ',' exp %prec ABOVE_COMMA
439 exp : exp '(' nonempty_typelist ')' const_or_volatile
441 write_exp_elt_opcode (TYPE_INSTANCE);
442 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
443 for (i = 0; i < $<ivec>3[0]; ++i)
444 write_exp_elt_type ($<tvec>3[i + 1]);
445 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
446 write_exp_elt_opcode (TYPE_INSTANCE);
452 { $$ = end_arglist () - 1; }
454 exp : lcurly arglist rcurly %prec ARROW
455 { write_exp_elt_opcode (OP_ARRAY);
456 write_exp_elt_longcst ((LONGEST) 0);
457 write_exp_elt_longcst ((LONGEST) $3);
458 write_exp_elt_opcode (OP_ARRAY); }
461 exp : lcurly type rcurly exp %prec UNARY
462 { write_exp_elt_opcode (UNOP_MEMVAL);
463 write_exp_elt_type ($2);
464 write_exp_elt_opcode (UNOP_MEMVAL); }
467 exp : '(' type ')' exp %prec UNARY
468 { write_exp_elt_opcode (UNOP_CAST);
469 write_exp_elt_type ($2);
470 write_exp_elt_opcode (UNOP_CAST); }
477 /* Binary operators in order of decreasing precedence. */
480 { write_exp_elt_opcode (BINOP_REPEAT); }
484 { write_exp_elt_opcode (BINOP_MUL); }
488 { write_exp_elt_opcode (BINOP_DIV); }
492 { write_exp_elt_opcode (BINOP_REM); }
496 { write_exp_elt_opcode (BINOP_ADD); }
500 { write_exp_elt_opcode (BINOP_SUB); }
504 { write_exp_elt_opcode (BINOP_LSH); }
508 { write_exp_elt_opcode (BINOP_RSH); }
512 { write_exp_elt_opcode (BINOP_EQUAL); }
515 exp : exp NOTEQUAL exp
516 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
520 { write_exp_elt_opcode (BINOP_LEQ); }
524 { write_exp_elt_opcode (BINOP_GEQ); }
528 { write_exp_elt_opcode (BINOP_LESS); }
532 { write_exp_elt_opcode (BINOP_GTR); }
536 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
540 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
544 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
548 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
552 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
555 exp : exp '?' exp ':' exp %prec '?'
556 { write_exp_elt_opcode (TERNOP_COND); }
560 { write_exp_elt_opcode (BINOP_ASSIGN); }
563 exp : exp ASSIGN_MODIFY exp
564 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
565 write_exp_elt_opcode ($2);
566 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
570 { write_exp_elt_opcode (OP_LONG);
571 write_exp_elt_type ($1.type);
572 write_exp_elt_longcst ((LONGEST)($1.val));
573 write_exp_elt_opcode (OP_LONG); }
578 struct stoken_vector vec;
581 write_exp_string_vector ($1.type, &vec);
587 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
588 write_exp_elt_opcode (OP_LONG);
589 write_exp_elt_type (val.typed_val_int.type);
590 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
591 write_exp_elt_opcode (OP_LONG);
597 { write_exp_elt_opcode (OP_DOUBLE);
598 write_exp_elt_type ($1.type);
599 write_exp_elt_dblcst ($1.dval);
600 write_exp_elt_opcode (OP_DOUBLE); }
604 { write_exp_elt_opcode (OP_DECFLOAT);
605 write_exp_elt_type ($1.type);
606 write_exp_elt_decfloatcst ($1.val);
607 write_exp_elt_opcode (OP_DECFLOAT); }
615 write_dollar_variable ($1);
619 exp : SIZEOF '(' type ')' %prec UNARY
620 { write_exp_elt_opcode (OP_LONG);
621 write_exp_elt_type (lookup_signed_typename
622 (parse_language, parse_gdbarch,
625 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
626 write_exp_elt_opcode (OP_LONG); }
629 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
630 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
631 write_exp_elt_type ($3);
632 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
635 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
636 { write_exp_elt_opcode (UNOP_CAST);
637 write_exp_elt_type ($3);
638 write_exp_elt_opcode (UNOP_CAST); }
641 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
642 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
643 write_exp_elt_type ($3);
644 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
647 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
648 { /* We could do more error checking here, but
649 it doesn't seem worthwhile. */
650 write_exp_elt_opcode (UNOP_CAST);
651 write_exp_elt_type ($3);
652 write_exp_elt_opcode (UNOP_CAST); }
658 /* We copy the string here, and not in the
659 lexer, to guarantee that we do not leak a
660 string. Note that we follow the
661 NUL-termination convention of the
663 struct typed_stoken *vec = XNEW (struct typed_stoken);
668 vec->length = $1.length;
669 vec->ptr = malloc ($1.length + 1);
670 memcpy (vec->ptr, $1.ptr, $1.length + 1);
675 /* Note that we NUL-terminate here, but just
679 $$.tokens = realloc ($$.tokens,
680 $$.len * sizeof (struct typed_stoken));
682 p = malloc ($2.length + 1);
683 memcpy (p, $2.ptr, $2.length + 1);
685 $$.tokens[$$.len - 1].type = $2.type;
686 $$.tokens[$$.len - 1].length = $2.length;
687 $$.tokens[$$.len - 1].ptr = p;
694 enum c_string_type type = C_STRING;
696 for (i = 0; i < $1.len; ++i)
698 switch ($1.tokens[i].type)
706 && type != $1.tokens[i].type)
707 error (_("Undefined string concatenation."));
708 type = $1.tokens[i].type;
712 internal_error (__FILE__, __LINE__,
713 "unrecognized type in string concatenation");
717 write_exp_string_vector (type, &$1);
718 for (i = 0; i < $1.len; ++i)
719 free ($1.tokens[i].ptr);
726 { write_exp_elt_opcode (OP_LONG);
727 write_exp_elt_type (parse_type->builtin_bool);
728 write_exp_elt_longcst ((LONGEST) 1);
729 write_exp_elt_opcode (OP_LONG); }
733 { write_exp_elt_opcode (OP_LONG);
734 write_exp_elt_type (parse_type->builtin_bool);
735 write_exp_elt_longcst ((LONGEST) 0);
736 write_exp_elt_opcode (OP_LONG); }
744 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
746 error (_("No file or function \"%s\"."),
747 copy_name ($1.stoken));
755 block : block COLONCOLON name
757 = lookup_symbol (copy_name ($3), $1,
758 VAR_DOMAIN, (int *) NULL);
759 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
760 error (_("No function \"%s\" in specified context."),
762 $$ = SYMBOL_BLOCK_VALUE (tem); }
765 variable: name_not_typename ENTRY
766 { struct symbol *sym = $1.sym;
768 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
769 || !symbol_read_needs_frame (sym))
770 error (_("@entry can be used only for function "
771 "parameters, not for \"%s\""),
772 copy_name ($1.stoken));
774 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
775 write_exp_elt_sym (sym);
776 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
780 variable: block COLONCOLON name
781 { struct symbol *sym;
782 sym = lookup_symbol (copy_name ($3), $1,
783 VAR_DOMAIN, (int *) NULL);
785 error (_("No symbol \"%s\" in specified context."),
787 if (symbol_read_needs_frame (sym))
789 if (innermost_block == 0
790 || contained_in (block_found,
792 innermost_block = block_found;
795 write_exp_elt_opcode (OP_VAR_VALUE);
796 /* block_found is set by lookup_symbol. */
797 write_exp_elt_block (block_found);
798 write_exp_elt_sym (sym);
799 write_exp_elt_opcode (OP_VAR_VALUE); }
802 qualified_name: TYPENAME COLONCOLON name
804 struct type *type = $1.type;
805 CHECK_TYPEDEF (type);
806 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
807 && TYPE_CODE (type) != TYPE_CODE_UNION
808 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
809 error (_("`%s' is not defined as an aggregate type."),
812 write_exp_elt_opcode (OP_SCOPE);
813 write_exp_elt_type (type);
814 write_exp_string ($3);
815 write_exp_elt_opcode (OP_SCOPE);
817 | TYPENAME COLONCOLON '~' name
819 struct type *type = $1.type;
820 struct stoken tmp_token;
821 CHECK_TYPEDEF (type);
822 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
823 && TYPE_CODE (type) != TYPE_CODE_UNION
824 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
825 error (_("`%s' is not defined as an aggregate type."),
828 tmp_token.ptr = (char*) alloca ($4.length + 2);
829 tmp_token.length = $4.length + 1;
830 tmp_token.ptr[0] = '~';
831 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
832 tmp_token.ptr[tmp_token.length] = 0;
834 /* Check for valid destructor name. */
835 destructor_name_p (tmp_token.ptr, $1.type);
836 write_exp_elt_opcode (OP_SCOPE);
837 write_exp_elt_type (type);
838 write_exp_string (tmp_token);
839 write_exp_elt_opcode (OP_SCOPE);
841 | TYPENAME COLONCOLON name COLONCOLON name
843 char *copy = copy_name ($3);
844 error (_("No type \"%s\" within class "
845 "or namespace \"%s\"."),
846 copy, TYPE_NAME ($1.type));
850 variable: qualified_name
851 | COLONCOLON name_not_typename
853 char *name = copy_name ($2.stoken);
855 struct minimal_symbol *msymbol;
858 lookup_symbol (name, (const struct block *) NULL,
859 VAR_DOMAIN, (int *) NULL);
862 write_exp_elt_opcode (OP_VAR_VALUE);
863 write_exp_elt_block (NULL);
864 write_exp_elt_sym (sym);
865 write_exp_elt_opcode (OP_VAR_VALUE);
869 msymbol = lookup_minimal_symbol (name, NULL, NULL);
871 write_exp_msymbol (msymbol);
872 else if (!have_full_symbols () && !have_partial_symbols ())
873 error (_("No symbol table is loaded. Use the \"file\" command."));
875 error (_("No symbol \"%s\" in current context."), name);
879 variable: name_not_typename
880 { struct symbol *sym = $1.sym;
884 if (symbol_read_needs_frame (sym))
886 if (innermost_block == 0
887 || contained_in (block_found,
889 innermost_block = block_found;
892 write_exp_elt_opcode (OP_VAR_VALUE);
893 /* We want to use the selected frame, not
894 another more inner frame which happens to
895 be in the same block. */
896 write_exp_elt_block (NULL);
897 write_exp_elt_sym (sym);
898 write_exp_elt_opcode (OP_VAR_VALUE);
900 else if ($1.is_a_field_of_this)
902 /* C++: it hangs off of `this'. Must
903 not inadvertently convert from a method call
905 if (innermost_block == 0
906 || contained_in (block_found,
908 innermost_block = block_found;
909 write_exp_elt_opcode (OP_THIS);
910 write_exp_elt_opcode (OP_THIS);
911 write_exp_elt_opcode (STRUCTOP_PTR);
912 write_exp_string ($1.stoken);
913 write_exp_elt_opcode (STRUCTOP_PTR);
917 struct minimal_symbol *msymbol;
918 char *arg = copy_name ($1.stoken);
921 lookup_minimal_symbol (arg, NULL, NULL);
923 write_exp_msymbol (msymbol);
924 else if (!have_full_symbols () && !have_partial_symbols ())
925 error (_("No symbol table is loaded. Use the \"file\" command."));
927 error (_("No symbol \"%s\" in current context."),
928 copy_name ($1.stoken));
933 space_identifier : '@' NAME
934 { push_type_address_space (copy_name ($2.stoken));
935 push_type (tp_space_identifier);
939 const_or_volatile: const_or_volatile_noopt
943 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
946 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
947 | const_or_volatile_noopt
950 const_or_volatile_or_space_identifier:
951 const_or_volatile_or_space_identifier_noopt
956 { push_type (tp_pointer); $$ = 0; }
958 { push_type (tp_pointer); $$ = $2; }
960 { push_type (tp_reference); $$ = 0; }
962 { push_type (tp_reference); $$ = $2; }
966 direct_abs_decl: '(' abs_decl ')'
968 | direct_abs_decl array_mod
971 push_type (tp_array);
976 push_type (tp_array);
980 | direct_abs_decl func_mod
981 { push_type (tp_function); }
983 { push_type (tp_function); }
994 | '(' nonempty_typelist ')'
995 { free ($2); $$ = 0; }
998 /* We used to try to recognize pointer to member types here, but
999 that didn't work (shift/reduce conflicts meant that these rules never
1000 got executed). The problem is that
1001 int (foo::bar::baz::bizzle)
1002 is a function type but
1003 int (foo::bar::baz::bizzle::*)
1004 is a pointer to member type. Stroustrup loses again! */
1009 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1013 { $$ = lookup_signed_typename (parse_language,
1017 { $$ = lookup_signed_typename (parse_language,
1021 { $$ = lookup_signed_typename (parse_language,
1025 { $$ = lookup_signed_typename (parse_language,
1028 | LONG SIGNED_KEYWORD INT_KEYWORD
1029 { $$ = lookup_signed_typename (parse_language,
1032 | LONG SIGNED_KEYWORD
1033 { $$ = lookup_signed_typename (parse_language,
1036 | SIGNED_KEYWORD LONG INT_KEYWORD
1037 { $$ = lookup_signed_typename (parse_language,
1040 | UNSIGNED LONG INT_KEYWORD
1041 { $$ = lookup_unsigned_typename (parse_language,
1044 | LONG UNSIGNED INT_KEYWORD
1045 { $$ = lookup_unsigned_typename (parse_language,
1049 { $$ = lookup_unsigned_typename (parse_language,
1053 { $$ = lookup_signed_typename (parse_language,
1056 | LONG LONG INT_KEYWORD
1057 { $$ = lookup_signed_typename (parse_language,
1060 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1061 { $$ = lookup_signed_typename (parse_language,
1064 | LONG LONG SIGNED_KEYWORD
1065 { $$ = lookup_signed_typename (parse_language,
1068 | SIGNED_KEYWORD LONG LONG
1069 { $$ = lookup_signed_typename (parse_language,
1072 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1073 { $$ = lookup_signed_typename (parse_language,
1076 | UNSIGNED LONG LONG
1077 { $$ = lookup_unsigned_typename (parse_language,
1080 | UNSIGNED LONG LONG INT_KEYWORD
1081 { $$ = lookup_unsigned_typename (parse_language,
1084 | LONG LONG UNSIGNED
1085 { $$ = lookup_unsigned_typename (parse_language,
1088 | LONG LONG UNSIGNED INT_KEYWORD
1089 { $$ = lookup_unsigned_typename (parse_language,
1093 { $$ = lookup_signed_typename (parse_language,
1096 | SHORT SIGNED_KEYWORD INT_KEYWORD
1097 { $$ = lookup_signed_typename (parse_language,
1100 | SHORT SIGNED_KEYWORD
1101 { $$ = lookup_signed_typename (parse_language,
1104 | UNSIGNED SHORT INT_KEYWORD
1105 { $$ = lookup_unsigned_typename (parse_language,
1109 { $$ = lookup_unsigned_typename (parse_language,
1112 | SHORT UNSIGNED INT_KEYWORD
1113 { $$ = lookup_unsigned_typename (parse_language,
1117 { $$ = lookup_typename (parse_language, parse_gdbarch,
1118 "double", (struct block *) NULL,
1120 | LONG DOUBLE_KEYWORD
1121 { $$ = lookup_typename (parse_language, parse_gdbarch,
1123 (struct block *) NULL, 0); }
1125 { $$ = lookup_struct (copy_name ($2),
1126 expression_context_block); }
1128 { $$ = lookup_struct (copy_name ($2),
1129 expression_context_block); }
1131 { $$ = lookup_union (copy_name ($2),
1132 expression_context_block); }
1134 { $$ = lookup_enum (copy_name ($2),
1135 expression_context_block); }
1137 { $$ = lookup_unsigned_typename (parse_language,
1139 TYPE_NAME($2.type)); }
1141 { $$ = lookup_unsigned_typename (parse_language,
1144 | SIGNED_KEYWORD typename
1145 { $$ = lookup_signed_typename (parse_language,
1147 TYPE_NAME($2.type)); }
1149 { $$ = lookup_signed_typename (parse_language,
1152 /* It appears that this rule for templates is never
1153 reduced; template recognition happens by lookahead
1154 in the token processing code in yylex. */
1155 | TEMPLATE name '<' type '>'
1156 { $$ = lookup_template_type(copy_name($2), $4,
1157 expression_context_block);
1159 | const_or_volatile_or_space_identifier_noopt typebase
1160 { $$ = follow_types ($2); }
1161 | typebase const_or_volatile_or_space_identifier_noopt
1162 { $$ = follow_types ($1); }
1168 $$.stoken.ptr = "int";
1169 $$.stoken.length = 3;
1170 $$.type = lookup_signed_typename (parse_language,
1176 $$.stoken.ptr = "long";
1177 $$.stoken.length = 4;
1178 $$.type = lookup_signed_typename (parse_language,
1184 $$.stoken.ptr = "short";
1185 $$.stoken.length = 5;
1186 $$.type = lookup_signed_typename (parse_language,
1194 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1195 $<ivec>$[0] = 1; /* Number of types in vector */
1198 | nonempty_typelist ',' type
1199 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1200 $$ = (struct type **) realloc ((char *) $1, len);
1201 $$[$<ivec>$[0]] = $3;
1206 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1207 { $$ = follow_types ($1); }
1210 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1211 | VOLATILE_KEYWORD CONST_KEYWORD
1214 const_or_volatile_noopt: const_and_volatile
1215 { push_type (tp_const);
1216 push_type (tp_volatile);
1219 { push_type (tp_const); }
1221 { push_type (tp_volatile); }
1224 operator: OPERATOR NEW
1225 { $$ = operator_stoken (" new"); }
1227 { $$ = operator_stoken (" delete"); }
1228 | OPERATOR NEW '[' ']'
1229 { $$ = operator_stoken (" new[]"); }
1230 | OPERATOR DELETE '[' ']'
1231 { $$ = operator_stoken (" delete[]"); }
1233 { $$ = operator_stoken ("+"); }
1235 { $$ = operator_stoken ("-"); }
1237 { $$ = operator_stoken ("*"); }
1239 { $$ = operator_stoken ("/"); }
1241 { $$ = operator_stoken ("%"); }
1243 { $$ = operator_stoken ("^"); }
1245 { $$ = operator_stoken ("&"); }
1247 { $$ = operator_stoken ("|"); }
1249 { $$ = operator_stoken ("~"); }
1251 { $$ = operator_stoken ("!"); }
1253 { $$ = operator_stoken ("="); }
1255 { $$ = operator_stoken ("<"); }
1257 { $$ = operator_stoken (">"); }
1258 | OPERATOR ASSIGN_MODIFY
1259 { const char *op = "unknown";
1283 case BINOP_BITWISE_IOR:
1286 case BINOP_BITWISE_AND:
1289 case BINOP_BITWISE_XOR:
1296 $$ = operator_stoken (op);
1299 { $$ = operator_stoken ("<<"); }
1301 { $$ = operator_stoken (">>"); }
1303 { $$ = operator_stoken ("=="); }
1305 { $$ = operator_stoken ("!="); }
1307 { $$ = operator_stoken ("<="); }
1309 { $$ = operator_stoken (">="); }
1311 { $$ = operator_stoken ("&&"); }
1313 { $$ = operator_stoken ("||"); }
1314 | OPERATOR INCREMENT
1315 { $$ = operator_stoken ("++"); }
1316 | OPERATOR DECREMENT
1317 { $$ = operator_stoken ("--"); }
1319 { $$ = operator_stoken (","); }
1320 | OPERATOR ARROW_STAR
1321 { $$ = operator_stoken ("->*"); }
1323 { $$ = operator_stoken ("->"); }
1325 { $$ = operator_stoken ("()"); }
1327 { $$ = operator_stoken ("[]"); }
1331 struct ui_file *buf = mem_fileopen ();
1333 c_print_type ($2, NULL, buf, -1, 0);
1334 name = ui_file_xstrdup (buf, &length);
1335 ui_file_delete (buf);
1336 $$ = operator_stoken (name);
1343 name : NAME { $$ = $1.stoken; }
1344 | BLOCKNAME { $$ = $1.stoken; }
1345 | TYPENAME { $$ = $1.stoken; }
1346 | NAME_OR_INT { $$ = $1.stoken; }
1347 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1348 | operator { $$ = $1; }
1351 name_not_typename : NAME
1353 /* These would be useful if name_not_typename was useful, but it is just
1354 a fake for "variable", so these cause reduce/reduce conflicts because
1355 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1356 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1357 context where only a name could occur, this might be useful.
1363 $$.sym = lookup_symbol ($1.ptr,
1364 expression_context_block,
1366 &$$.is_a_field_of_this);
1373 /* Returns a stoken of the operator name given by OP (which does not
1374 include the string "operator"). */
1375 static struct stoken
1376 operator_stoken (const char *op)
1378 static const char *operator_string = "operator";
1379 struct stoken st = { NULL, 0 };
1380 st.length = strlen (operator_string) + strlen (op);
1381 st.ptr = malloc (st.length + 1);
1382 strcpy (st.ptr, operator_string);
1383 strcat (st.ptr, op);
1385 /* The toplevel (c_parse) will free the memory allocated here. */
1386 make_cleanup (free, st.ptr);
1390 /* Take care of parsing a number (anything that starts with a digit).
1391 Set yylval and return the token type; update lexptr.
1392 LEN is the number of characters in it. */
1394 /*** Needs some error checking for the float case ***/
1397 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1399 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1400 here, and we do kind of silly things like cast to unsigned. */
1407 int base = input_radix;
1410 /* Number of "L" suffixes encountered. */
1413 /* We have found a "L" or "U" suffix. */
1414 int found_suffix = 0;
1417 struct type *signed_type;
1418 struct type *unsigned_type;
1422 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1423 point. Return DECFLOAT. */
1425 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1428 putithere->typed_val_decfloat.type
1429 = parse_type->builtin_decfloat;
1430 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1431 gdbarch_byte_order (parse_gdbarch), p);
1436 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1439 putithere->typed_val_decfloat.type
1440 = parse_type->builtin_decdouble;
1441 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1442 gdbarch_byte_order (parse_gdbarch), p);
1447 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1450 putithere->typed_val_decfloat.type
1451 = parse_type->builtin_declong;
1452 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1453 gdbarch_byte_order (parse_gdbarch), p);
1458 if (! parse_c_float (parse_gdbarch, p, len,
1459 &putithere->typed_val_float.dval,
1460 &putithere->typed_val_float.type))
1465 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1509 if (c >= 'A' && c <= 'Z')
1511 if (c != 'l' && c != 'u')
1513 if (c >= '0' && c <= '9')
1521 if (base > 10 && c >= 'a' && c <= 'f')
1525 n += i = c - 'a' + 10;
1538 return ERROR; /* Char not a digit */
1541 return ERROR; /* Invalid digit in this base */
1543 /* Portably test for overflow (only works for nonzero values, so make
1544 a second check for zero). FIXME: Can't we just make n and prevn
1545 unsigned and avoid this? */
1546 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1547 unsigned_p = 1; /* Try something unsigned */
1549 /* Portably test for unsigned overflow.
1550 FIXME: This check is wrong; for example it doesn't find overflow
1551 on 0x123456789 when LONGEST is 32 bits. */
1552 if (c != 'l' && c != 'u' && n != 0)
1554 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1555 error (_("Numeric constant too large."));
1560 /* An integer constant is an int, a long, or a long long. An L
1561 suffix forces it to be long; an LL suffix forces it to be long
1562 long. If not forced to a larger size, it gets the first type of
1563 the above that it fits in. To figure out whether it fits, we
1564 shift it right and see whether anything remains. Note that we
1565 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1566 operation, because many compilers will warn about such a shift
1567 (which always produces a zero result). Sometimes gdbarch_int_bit
1568 or gdbarch_long_bit will be that big, sometimes not. To deal with
1569 the case where it is we just always shift the value more than
1570 once, with fewer bits each time. */
1572 un = (ULONGEST)n >> 2;
1574 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1576 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1578 /* A large decimal (not hex or octal) constant (between INT_MAX
1579 and UINT_MAX) is a long or unsigned long, according to ANSI,
1580 never an unsigned int, but this code treats it as unsigned
1581 int. This probably should be fixed. GCC gives a warning on
1584 unsigned_type = parse_type->builtin_unsigned_int;
1585 signed_type = parse_type->builtin_int;
1587 else if (long_p <= 1
1588 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1590 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1591 unsigned_type = parse_type->builtin_unsigned_long;
1592 signed_type = parse_type->builtin_long;
1597 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1598 < gdbarch_long_long_bit (parse_gdbarch))
1599 /* A long long does not fit in a LONGEST. */
1600 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1602 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1603 high_bit = (ULONGEST) 1 << shift;
1604 unsigned_type = parse_type->builtin_unsigned_long_long;
1605 signed_type = parse_type->builtin_long_long;
1608 putithere->typed_val_int.val = n;
1610 /* If the high bit of the worked out type is set then this number
1611 has to be unsigned. */
1613 if (unsigned_p || (n & high_bit))
1615 putithere->typed_val_int.type = unsigned_type;
1619 putithere->typed_val_int.type = signed_type;
1625 /* Temporary obstack used for holding strings. */
1626 static struct obstack tempbuf;
1627 static int tempbuf_init;
1629 /* Parse a C escape sequence. The initial backslash of the sequence
1630 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1631 last character of the sequence. If OUTPUT is not NULL, the
1632 translated form of the escape sequence will be written there. If
1633 OUTPUT is NULL, no output is written and the call will only affect
1634 *PTR. If an escape sequence is expressed in target bytes, then the
1635 entire sequence will simply be copied to OUTPUT. Return 1 if any
1636 character was emitted, 0 otherwise. */
1639 c_parse_escape (char **ptr, struct obstack *output)
1641 char *tokptr = *ptr;
1644 /* Some escape sequences undergo character set conversion. Those we
1648 /* Hex escapes do not undergo character set conversion, so keep
1649 the escape sequence for later. */
1652 obstack_grow_str (output, "\\x");
1654 if (!isxdigit (*tokptr))
1655 error (_("\\x escape without a following hex digit"));
1656 while (isxdigit (*tokptr))
1659 obstack_1grow (output, *tokptr);
1664 /* Octal escapes do not undergo character set conversion, so
1665 keep the escape sequence for later. */
1677 obstack_grow_str (output, "\\");
1679 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1683 obstack_1grow (output, *tokptr);
1689 /* We handle UCNs later. We could handle them here, but that
1690 would mean a spurious error in the case where the UCN could
1691 be converted to the target charset but not the host
1697 int i, len = c == 'U' ? 8 : 4;
1700 obstack_1grow (output, '\\');
1701 obstack_1grow (output, *tokptr);
1704 if (!isxdigit (*tokptr))
1705 error (_("\\%c escape without a following hex digit"), c);
1706 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1709 obstack_1grow (output, *tokptr);
1715 /* We must pass backslash through so that it does not
1716 cause quoting during the second expansion. */
1719 obstack_grow_str (output, "\\\\");
1723 /* Escapes which undergo conversion. */
1726 obstack_1grow (output, '\a');
1731 obstack_1grow (output, '\b');
1736 obstack_1grow (output, '\f');
1741 obstack_1grow (output, '\n');
1746 obstack_1grow (output, '\r');
1751 obstack_1grow (output, '\t');
1756 obstack_1grow (output, '\v');
1760 /* GCC extension. */
1763 obstack_1grow (output, HOST_ESCAPE_CHAR);
1767 /* Backslash-newline expands to nothing at all. */
1773 /* A few escapes just expand to the character itself. */
1777 /* GCC extensions. */
1782 /* Unrecognized escapes turn into the character itself. */
1785 obstack_1grow (output, *tokptr);
1793 /* Parse a string or character literal from TOKPTR. The string or
1794 character may be wide or unicode. *OUTPTR is set to just after the
1795 end of the literal in the input string. The resulting token is
1796 stored in VALUE. This returns a token value, either STRING or
1797 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1798 number of host characters in the literal. */
1800 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1804 enum c_string_type type;
1806 /* Build the gdb internal form of the input string in tempbuf. Note
1807 that the buffer is null byte terminated *only* for the
1808 convenience of debugging gdb itself and printing the buffer
1809 contents when the buffer contains no embedded nulls. Gdb does
1810 not depend upon the buffer being null byte terminated, it uses
1811 the length string instead. This allows gdb to handle C strings
1812 (as well as strings in other languages) with embedded null
1818 obstack_free (&tempbuf, NULL);
1819 obstack_init (&tempbuf);
1821 /* Record the string type. */
1824 type = C_WIDE_STRING;
1827 else if (*tokptr == 'u')
1832 else if (*tokptr == 'U')
1840 /* Skip the quote. */
1854 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1856 else if (c == quote)
1860 obstack_1grow (&tempbuf, c);
1862 /* FIXME: this does the wrong thing with multi-byte host
1863 characters. We could use mbrlen here, but that would
1864 make "set host-charset" a bit less useful. */
1869 if (*tokptr != quote)
1872 error (_("Unterminated string in expression."));
1874 error (_("Unmatched single quote."));
1879 value->ptr = obstack_base (&tempbuf);
1880 value->length = obstack_object_size (&tempbuf);
1884 return quote == '"' ? STRING : CHAR;
1891 enum exp_opcode opcode;
1895 static const struct token tokentab3[] =
1897 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1898 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1899 {"->*", ARROW_STAR, BINOP_END, 1}
1902 static const struct token tokentab2[] =
1904 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1905 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1906 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1907 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1908 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1909 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1910 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1911 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1912 {"++", INCREMENT, BINOP_END, 0},
1913 {"--", DECREMENT, BINOP_END, 0},
1914 {"->", ARROW, BINOP_END, 0},
1915 {"&&", ANDAND, BINOP_END, 0},
1916 {"||", OROR, BINOP_END, 0},
1917 /* "::" is *not* only C++: gdb overrides its meaning in several
1918 different ways, e.g., 'filename'::func, function::variable. */
1919 {"::", COLONCOLON, BINOP_END, 0},
1920 {"<<", LSH, BINOP_END, 0},
1921 {">>", RSH, BINOP_END, 0},
1922 {"==", EQUAL, BINOP_END, 0},
1923 {"!=", NOTEQUAL, BINOP_END, 0},
1924 {"<=", LEQ, BINOP_END, 0},
1925 {">=", GEQ, BINOP_END, 0},
1926 {".*", DOT_STAR, BINOP_END, 1}
1929 /* Identifier-like tokens. */
1930 static const struct token ident_tokens[] =
1932 {"unsigned", UNSIGNED, OP_NULL, 0},
1933 {"template", TEMPLATE, OP_NULL, 1},
1934 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1935 {"struct", STRUCT, OP_NULL, 0},
1936 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1937 {"sizeof", SIZEOF, OP_NULL, 0},
1938 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1939 {"false", FALSEKEYWORD, OP_NULL, 1},
1940 {"class", CLASS, OP_NULL, 1},
1941 {"union", UNION, OP_NULL, 0},
1942 {"short", SHORT, OP_NULL, 0},
1943 {"const", CONST_KEYWORD, OP_NULL, 0},
1944 {"enum", ENUM, OP_NULL, 0},
1945 {"long", LONG, OP_NULL, 0},
1946 {"true", TRUEKEYWORD, OP_NULL, 1},
1947 {"int", INT_KEYWORD, OP_NULL, 0},
1948 {"new", NEW, OP_NULL, 1},
1949 {"delete", DELETE, OP_NULL, 1},
1950 {"operator", OPERATOR, OP_NULL, 1},
1952 {"and", ANDAND, BINOP_END, 1},
1953 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1954 {"bitand", '&', OP_NULL, 1},
1955 {"bitor", '|', OP_NULL, 1},
1956 {"compl", '~', OP_NULL, 1},
1957 {"not", '!', OP_NULL, 1},
1958 {"not_eq", NOTEQUAL, BINOP_END, 1},
1959 {"or", OROR, BINOP_END, 1},
1960 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1961 {"xor", '^', OP_NULL, 1},
1962 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1964 {"const_cast", CONST_CAST, OP_NULL, 1 },
1965 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1966 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1967 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1970 /* When we find that lexptr (the global var defined in parse.c) is
1971 pointing at a macro invocation, we expand the invocation, and call
1972 scan_macro_expansion to save the old lexptr here and point lexptr
1973 into the expanded text. When we reach the end of that, we call
1974 end_macro_expansion to pop back to the value we saved here. The
1975 macro expansion code promises to return only fully-expanded text,
1976 so we don't need to "push" more than one level.
1978 This is disgusting, of course. It would be cleaner to do all macro
1979 expansion beforehand, and then hand that to lexptr. But we don't
1980 really know where the expression ends. Remember, in a command like
1982 (gdb) break *ADDRESS if CONDITION
1984 we evaluate ADDRESS in the scope of the current frame, but we
1985 evaluate CONDITION in the scope of the breakpoint's location. So
1986 it's simply wrong to try to macro-expand the whole thing at once. */
1987 static char *macro_original_text;
1989 /* We save all intermediate macro expansions on this obstack for the
1990 duration of a single parse. The expansion text may sometimes have
1991 to live past the end of the expansion, due to yacc lookahead.
1992 Rather than try to be clever about saving the data for a single
1993 token, we simply keep it all and delete it after parsing has
1995 static struct obstack expansion_obstack;
1998 scan_macro_expansion (char *expansion)
2002 /* We'd better not be trying to push the stack twice. */
2003 gdb_assert (! macro_original_text);
2005 /* Copy to the obstack, and then free the intermediate
2007 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2010 /* Save the old lexptr value, so we can return to it when we're done
2011 parsing the expanded text. */
2012 macro_original_text = lexptr;
2018 scanning_macro_expansion (void)
2020 return macro_original_text != 0;
2025 finished_macro_expansion (void)
2027 /* There'd better be something to pop back to. */
2028 gdb_assert (macro_original_text);
2030 /* Pop back to the original text. */
2031 lexptr = macro_original_text;
2032 macro_original_text = 0;
2037 scan_macro_cleanup (void *dummy)
2039 if (macro_original_text)
2040 finished_macro_expansion ();
2042 obstack_free (&expansion_obstack, NULL);
2045 /* Return true iff the token represents a C++ cast operator. */
2048 is_cast_operator (const char *token, int len)
2050 return (! strncmp (token, "dynamic_cast", len)
2051 || ! strncmp (token, "static_cast", len)
2052 || ! strncmp (token, "reinterpret_cast", len)
2053 || ! strncmp (token, "const_cast", len));
2056 /* The scope used for macro expansion. */
2057 static struct macro_scope *expression_macro_scope;
2059 /* This is set if a NAME token appeared at the very end of the input
2060 string, with no whitespace separating the name from the EOF. This
2061 is used only when parsing to do field name completion. */
2062 static int saw_name_at_eof;
2064 /* This is set if the previously-returned token was a structure
2065 operator -- either '.' or ARROW. This is used only when parsing to
2066 do field name completion. */
2067 static int last_was_structop;
2069 /* Read one token, getting characters through lexptr. */
2072 lex_one_token (void)
2078 int saw_structop = last_was_structop;
2081 last_was_structop = 0;
2085 /* Check if this is a macro invocation that we need to expand. */
2086 if (! scanning_macro_expansion ())
2088 char *expanded = macro_expand_next (&lexptr,
2089 standard_macro_lookup,
2090 expression_macro_scope);
2093 scan_macro_expansion (expanded);
2096 prev_lexptr = lexptr;
2099 /* See if it is a special token of length 3. */
2100 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2101 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2103 if (tokentab3[i].cxx_only
2104 && parse_language->la_language != language_cplus)
2108 yylval.opcode = tokentab3[i].opcode;
2109 return tokentab3[i].token;
2112 /* See if it is a special token of length 2. */
2113 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2114 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2116 if (tokentab2[i].cxx_only
2117 && parse_language->la_language != language_cplus)
2121 yylval.opcode = tokentab2[i].opcode;
2122 if (in_parse_field && tokentab2[i].token == ARROW)
2123 last_was_structop = 1;
2124 return tokentab2[i].token;
2127 switch (c = *tokstart)
2130 /* If we were just scanning the result of a macro expansion,
2131 then we need to resume scanning the original text.
2132 If we're parsing for field name completion, and the previous
2133 token allows such completion, return a COMPLETE token.
2134 Otherwise, we were already scanning the original text, and
2135 we're really done. */
2136 if (scanning_macro_expansion ())
2138 finished_macro_expansion ();
2141 else if (saw_name_at_eof)
2143 saw_name_at_eof = 0;
2146 else if (saw_structop)
2165 if (paren_depth == 0)
2172 if (comma_terminates
2174 && ! scanning_macro_expansion ())
2180 /* Might be a floating point number. */
2181 if (lexptr[1] < '0' || lexptr[1] > '9')
2184 last_was_structop = 1;
2185 goto symbol; /* Nope, must be a symbol. */
2187 /* FALL THRU into number case. */
2200 /* It's a number. */
2201 int got_dot = 0, got_e = 0, toktype;
2203 int hex = input_radix > 10;
2205 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2210 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2218 /* This test includes !hex because 'e' is a valid hex digit
2219 and thus does not indicate a floating point number when
2220 the radix is hex. */
2221 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2222 got_dot = got_e = 1;
2223 /* This test does not include !hex, because a '.' always indicates
2224 a decimal floating point number regardless of the radix. */
2225 else if (!got_dot && *p == '.')
2227 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2228 && (*p == '-' || *p == '+'))
2229 /* This is the sign of the exponent, not the end of the
2232 /* We will take any letters or digits. parse_number will
2233 complain if past the radix, or if L or U are not final. */
2234 else if ((*p < '0' || *p > '9')
2235 && ((*p < 'a' || *p > 'z')
2236 && (*p < 'A' || *p > 'Z')))
2239 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2240 if (toktype == ERROR)
2242 char *err_copy = (char *) alloca (p - tokstart + 1);
2244 memcpy (err_copy, tokstart, p - tokstart);
2245 err_copy[p - tokstart] = 0;
2246 error (_("Invalid number \"%s\"."), err_copy);
2254 char *p = &tokstart[1];
2255 size_t len = strlen ("entry");
2257 while (isspace (*p))
2259 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2291 if (tokstart[1] != '"' && tokstart[1] != '\'')
2298 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2303 error (_("Empty character constant."));
2304 else if (host_len > 2 && c == '\'')
2307 namelen = lexptr - tokstart - 1;
2310 else if (host_len > 1)
2311 error (_("Invalid character constant."));
2317 if (!(c == '_' || c == '$'
2318 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2319 /* We must have come across a bad character (e.g. ';'). */
2320 error (_("Invalid character '%c' in expression."), c);
2322 /* It's a name. See how long it is. */
2324 for (c = tokstart[namelen];
2325 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2326 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2328 /* Template parameter lists are part of the name.
2329 FIXME: This mishandles `print $a<4&&$a>3'. */
2333 if (! is_cast_operator (tokstart, namelen))
2335 /* Scan ahead to get rest of the template specification. Note
2336 that we look ahead only when the '<' adjoins non-whitespace
2337 characters; for comparison expressions, e.g. "a < b > c",
2338 there must be spaces before the '<', etc. */
2340 char * p = find_template_name_end (tokstart + namelen);
2342 namelen = p - tokstart;
2346 c = tokstart[++namelen];
2349 /* The token "if" terminates the expression and is NOT removed from
2350 the input stream. It doesn't count if it appears in the
2351 expansion of a macro. */
2353 && tokstart[0] == 'i'
2354 && tokstart[1] == 'f'
2355 && ! scanning_macro_expansion ())
2360 /* For the same reason (breakpoint conditions), "thread N"
2361 terminates the expression. "thread" could be an identifier, but
2362 an identifier is never followed by a number without intervening
2363 punctuation. "task" is similar. Handle abbreviations of these,
2364 similarly to breakpoint.c:find_condition_and_thread. */
2366 && (strncmp (tokstart, "thread", namelen) == 0
2367 || strncmp (tokstart, "task", namelen) == 0)
2368 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2369 && ! scanning_macro_expansion ())
2371 char *p = tokstart + namelen + 1;
2372 while (*p == ' ' || *p == '\t')
2374 if (*p >= '0' && *p <= '9')
2382 yylval.sval.ptr = tokstart;
2383 yylval.sval.length = namelen;
2385 /* Catch specific keywords. */
2386 copy = copy_name (yylval.sval);
2387 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2388 if (strcmp (copy, ident_tokens[i].operator) == 0)
2390 if (ident_tokens[i].cxx_only
2391 && parse_language->la_language != language_cplus)
2394 /* It is ok to always set this, even though we don't always
2395 strictly need to. */
2396 yylval.opcode = ident_tokens[i].opcode;
2397 return ident_tokens[i].token;
2400 if (*tokstart == '$')
2403 if (in_parse_field && *lexptr == '\0')
2404 saw_name_at_eof = 1;
2408 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2415 DEF_VEC_O (token_and_value);
2417 /* A FIFO of tokens that have been read but not yet returned to the
2419 static VEC (token_and_value) *token_fifo;
2421 /* Non-zero if the lexer should return tokens from the FIFO. */
2424 /* Temporary storage for c_lex; this holds symbol names as they are
2426 static struct obstack name_obstack;
2428 /* Classify a NAME token. The contents of the token are in `yylval'.
2429 Updates yylval and returns the new token type. BLOCK is the block
2430 in which lookups start; this can be NULL to mean the global
2433 classify_name (struct block *block)
2437 int is_a_field_of_this = 0;
2439 copy = copy_name (yylval.sval);
2441 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2442 parse_language->la_language == language_cplus
2443 ? &is_a_field_of_this : (int *) NULL);
2445 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2447 yylval.ssym.sym = sym;
2448 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2453 /* See if it's a file name. */
2454 struct symtab *symtab;
2456 symtab = lookup_symtab (copy);
2459 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2464 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2466 yylval.tsym.type = SYMBOL_TYPE (sym);
2471 = language_lookup_primitive_type_by_name (parse_language,
2472 parse_gdbarch, copy);
2473 if (yylval.tsym.type != NULL)
2476 /* Input names that aren't symbols but ARE valid hex numbers, when
2477 the input radix permits them, can be names or numbers depending
2478 on the parse. Note we support radixes > 16 here. */
2480 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2481 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2483 YYSTYPE newlval; /* Its value is ignored. */
2484 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2487 yylval.ssym.sym = sym;
2488 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2493 /* Any other kind of symbol */
2494 yylval.ssym.sym = sym;
2495 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2498 && parse_language->la_language == language_cplus
2499 && !is_a_field_of_this
2500 && !lookup_minimal_symbol (copy, NULL, NULL))
2501 return UNKNOWN_CPP_NAME;
2506 /* Like classify_name, but used by the inner loop of the lexer, when a
2507 name might have already been seen. FIRST_NAME is true if the token
2508 in `yylval' is the first component of a name, false otherwise. */
2511 classify_inner_name (struct block *block, int first_name)
2513 struct type *type, *new_type;
2517 return classify_name (block);
2519 type = check_typedef (yylval.tsym.type);
2520 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2521 && TYPE_CODE (type) != TYPE_CODE_UNION
2522 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2525 copy = copy_name (yylval.tsym.stoken);
2526 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2527 if (yylval.ssym.sym == NULL)
2530 switch (SYMBOL_CLASS (yylval.ssym.sym))
2537 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2541 yylval.ssym.is_a_field_of_this = 0;
2544 internal_error (__FILE__, __LINE__, _("not reached"));
2547 /* The outer level of a two-level lexer. This calls the inner lexer
2548 to return tokens. It then either returns these tokens, or
2549 aggregates them into a larger token. This lets us work around a
2550 problem in our parsing approach, where the parser could not
2551 distinguish between qualified names and qualified types at the
2554 This approach is still not ideal, because it mishandles template
2555 types. See the comment in lex_one_token for an example. However,
2556 this is still an improvement over the earlier approach, and will
2557 suffice until we move to better parsing technology. */
2561 token_and_value current;
2562 int first_was_coloncolon, last_was_coloncolon, first_iter;
2564 if (popping && !VEC_empty (token_and_value, token_fifo))
2566 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2567 VEC_ordered_remove (token_and_value, token_fifo, 0);
2573 current.token = lex_one_token ();
2574 if (current.token == NAME)
2575 current.token = classify_name (expression_context_block);
2576 if (parse_language->la_language != language_cplus
2577 || (current.token != TYPENAME && current.token != COLONCOLON))
2578 return current.token;
2580 first_was_coloncolon = current.token == COLONCOLON;
2581 last_was_coloncolon = first_was_coloncolon;
2582 obstack_free (&name_obstack, obstack_base (&name_obstack));
2583 if (!last_was_coloncolon)
2584 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2585 current.value = yylval;
2589 token_and_value next;
2591 next.token = lex_one_token ();
2592 next.value = yylval;
2594 if (next.token == NAME && last_was_coloncolon)
2598 classification = classify_inner_name (first_was_coloncolon
2600 : expression_context_block,
2602 /* We keep going until we either run out of names, or until
2603 we have a qualified name which is not a type. */
2604 if (classification != TYPENAME && classification != NAME)
2606 /* Push the final component and leave the loop. */
2607 VEC_safe_push (token_and_value, token_fifo, &next);
2611 /* Update the partial name we are constructing. */
2614 /* We don't want to put a leading "::" into the name. */
2615 obstack_grow_str (&name_obstack, "::");
2617 obstack_grow (&name_obstack, next.value.sval.ptr,
2618 next.value.sval.length);
2620 yylval.sval.ptr = obstack_base (&name_obstack);
2621 yylval.sval.length = obstack_object_size (&name_obstack);
2622 current.value = yylval;
2623 current.token = classification;
2625 last_was_coloncolon = 0;
2627 else if (next.token == COLONCOLON && !last_was_coloncolon)
2628 last_was_coloncolon = 1;
2631 /* We've reached the end of the name. */
2632 VEC_safe_push (token_and_value, token_fifo, &next);
2641 /* If we ended with a "::", insert it too. */
2642 if (last_was_coloncolon)
2645 memset (&cc, 0, sizeof (token_and_value));
2646 if (first_was_coloncolon && first_iter)
2651 cc.token = COLONCOLON;
2652 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2655 yylval = current.value;
2656 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2658 yylval.sval.length);
2659 return current.token;
2666 struct cleanup *back_to = make_cleanup (free_current_contents,
2667 &expression_macro_scope);
2669 /* Set up the scope for macro expansion. */
2670 expression_macro_scope = NULL;
2672 if (expression_context_block)
2673 expression_macro_scope
2674 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2676 expression_macro_scope = default_macro_scope ();
2677 if (! expression_macro_scope)
2678 expression_macro_scope = user_macro_scope ();
2680 /* Initialize macro expansion code. */
2681 obstack_init (&expansion_obstack);
2682 gdb_assert (! macro_original_text);
2683 make_cleanup (scan_macro_cleanup, 0);
2685 make_cleanup_restore_integer (&yydebug);
2686 yydebug = parser_debug;
2688 /* Initialize some state used by the lexer. */
2689 last_was_structop = 0;
2690 saw_name_at_eof = 0;
2692 VEC_free (token_and_value, token_fifo);
2694 obstack_init (&name_obstack);
2695 make_cleanup_obstack_free (&name_obstack);
2697 result = yyparse ();
2698 do_cleanups (back_to);
2707 lexptr = prev_lexptr;
2709 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);