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
108 #define YYDEBUG 1 /* Default to yydebug support */
111 #define YYFPRINTF parser_fprintf
115 static int yylex (void);
117 void yyerror (char *);
121 /* Although the yacc "value" of an expression is not used,
122 since the result is stored in the structure being created,
123 other node types do have values. */
139 } typed_val_decfloat;
143 struct typed_stoken tsval;
145 struct symtoken ssym;
148 enum exp_opcode opcode;
149 struct internalvar *ivar;
151 struct stoken_vector svec;
157 /* YYSTYPE gets defined by %union */
158 static int parse_number (char *, int, int, YYSTYPE *);
159 static struct stoken operator_stoken (const char *);
162 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
164 %type <tval> type typebase
165 %type <tvec> nonempty_typelist
166 /* %type <bval> block */
168 /* Fancy type parsing. */
169 %type <voidval> func_mod direct_abs_decl abs_decl
171 %type <lval> array_mod
173 %token <typed_val_int> INT
174 %token <typed_val_float> FLOAT
175 %token <typed_val_decfloat> DECFLOAT
177 /* Both NAME and TYPENAME tokens represent symbols in the input,
178 and both convey their data as strings.
179 But a TYPENAME is a string that happens to be defined as a typedef
180 or builtin type name (such as int or char)
181 and a NAME is any other symbol.
182 Contexts where this distinction is not important can use the
183 nonterminal "name", which matches either NAME or TYPENAME. */
185 %token <tsval> STRING
187 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
188 %token <ssym> UNKNOWN_CPP_NAME
189 %token <voidval> COMPLETE
190 %token <tsym> TYPENAME
192 %type <svec> string_exp
193 %type <ssym> name_not_typename
194 %type <tsym> typename
196 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
197 but which would parse as a valid number in the current input radix.
198 E.g. "c" when input_radix==16. Depending on the parse, it will be
199 turned into a name or into a number. */
201 %token <ssym> NAME_OR_INT
204 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
208 %type <sval> operator
209 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
212 /* Special type cases, put in to allow the parser to distinguish different
214 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
216 %token <sval> VARIABLE
218 %token <opcode> ASSIGN_MODIFY
227 %right '=' ASSIGN_MODIFY
235 %left '<' '>' LEQ GEQ
240 %right UNARY INCREMENT DECREMENT
241 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
242 %token <ssym> BLOCKNAME
243 %token <bval> FILENAME
255 { write_exp_elt_opcode(OP_TYPE);
256 write_exp_elt_type($1);
257 write_exp_elt_opcode(OP_TYPE);}
260 /* Expressions, including the comma operator. */
263 { write_exp_elt_opcode (BINOP_COMMA); }
266 /* Expressions, not including the comma operator. */
267 exp : '*' exp %prec UNARY
268 { write_exp_elt_opcode (UNOP_IND); }
271 exp : '&' exp %prec UNARY
272 { write_exp_elt_opcode (UNOP_ADDR); }
275 exp : '-' exp %prec UNARY
276 { write_exp_elt_opcode (UNOP_NEG); }
279 exp : '+' exp %prec UNARY
280 { write_exp_elt_opcode (UNOP_PLUS); }
283 exp : '!' exp %prec UNARY
284 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
287 exp : '~' exp %prec UNARY
288 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
291 exp : INCREMENT exp %prec UNARY
292 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
295 exp : DECREMENT exp %prec UNARY
296 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
299 exp : exp INCREMENT %prec UNARY
300 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
303 exp : exp DECREMENT %prec UNARY
304 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
307 exp : SIZEOF exp %prec UNARY
308 { write_exp_elt_opcode (UNOP_SIZEOF); }
312 { write_exp_elt_opcode (STRUCTOP_PTR);
313 write_exp_string ($3);
314 write_exp_elt_opcode (STRUCTOP_PTR); }
317 exp : exp ARROW name COMPLETE
318 { mark_struct_expression ();
319 write_exp_elt_opcode (STRUCTOP_PTR);
320 write_exp_string ($3);
321 write_exp_elt_opcode (STRUCTOP_PTR); }
324 exp : exp ARROW COMPLETE
326 mark_struct_expression ();
327 write_exp_elt_opcode (STRUCTOP_PTR);
330 write_exp_string (s);
331 write_exp_elt_opcode (STRUCTOP_PTR); }
334 exp : exp ARROW qualified_name
335 { /* exp->type::name becomes exp->*(&type::name) */
336 /* Note: this doesn't work if name is a
337 static member! FIXME */
338 write_exp_elt_opcode (UNOP_ADDR);
339 write_exp_elt_opcode (STRUCTOP_MPTR); }
342 exp : exp ARROW_STAR exp
343 { write_exp_elt_opcode (STRUCTOP_MPTR); }
347 { write_exp_elt_opcode (STRUCTOP_STRUCT);
348 write_exp_string ($3);
349 write_exp_elt_opcode (STRUCTOP_STRUCT); }
352 exp : exp '.' name COMPLETE
353 { mark_struct_expression ();
354 write_exp_elt_opcode (STRUCTOP_STRUCT);
355 write_exp_string ($3);
356 write_exp_elt_opcode (STRUCTOP_STRUCT); }
359 exp : exp '.' COMPLETE
361 mark_struct_expression ();
362 write_exp_elt_opcode (STRUCTOP_STRUCT);
365 write_exp_string (s);
366 write_exp_elt_opcode (STRUCTOP_STRUCT); }
369 exp : exp '.' qualified_name
370 { /* exp.type::name becomes exp.*(&type::name) */
371 /* Note: this doesn't work if name is a
372 static member! FIXME */
373 write_exp_elt_opcode (UNOP_ADDR);
374 write_exp_elt_opcode (STRUCTOP_MEMBER); }
377 exp : exp DOT_STAR exp
378 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
381 exp : exp '[' exp1 ']'
382 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
386 /* This is to save the value of arglist_len
387 being accumulated by an outer function call. */
388 { start_arglist (); }
389 arglist ')' %prec ARROW
390 { write_exp_elt_opcode (OP_FUNCALL);
391 write_exp_elt_longcst ((LONGEST) end_arglist ());
392 write_exp_elt_opcode (OP_FUNCALL); }
395 exp : UNKNOWN_CPP_NAME '('
397 /* This could potentially be a an argument defined
398 lookup function (Koenig). */
399 write_exp_elt_opcode (OP_ADL_FUNC);
400 write_exp_elt_block (expression_context_block);
401 write_exp_elt_sym (NULL); /* Placeholder. */
402 write_exp_string ($1.stoken);
403 write_exp_elt_opcode (OP_ADL_FUNC);
405 /* This is to save the value of arglist_len
406 being accumulated by an outer function call. */
410 arglist ')' %prec ARROW
412 write_exp_elt_opcode (OP_FUNCALL);
413 write_exp_elt_longcst ((LONGEST) end_arglist ());
414 write_exp_elt_opcode (OP_FUNCALL);
419 { start_arglist (); }
429 arglist : arglist ',' exp %prec ABOVE_COMMA
433 exp : exp '(' nonempty_typelist ')' const_or_volatile
435 write_exp_elt_opcode (TYPE_INSTANCE);
436 write_exp_elt_longcst ((LONGEST) $<ivec>3[0]);
437 for (i = 0; i < $<ivec>3[0]; ++i)
438 write_exp_elt_type ($<tvec>3[i + 1]);
439 write_exp_elt_longcst((LONGEST) $<ivec>3[0]);
440 write_exp_elt_opcode (TYPE_INSTANCE);
446 { $$ = end_arglist () - 1; }
448 exp : lcurly arglist rcurly %prec ARROW
449 { write_exp_elt_opcode (OP_ARRAY);
450 write_exp_elt_longcst ((LONGEST) 0);
451 write_exp_elt_longcst ((LONGEST) $3);
452 write_exp_elt_opcode (OP_ARRAY); }
455 exp : lcurly type rcurly exp %prec UNARY
456 { write_exp_elt_opcode (UNOP_MEMVAL);
457 write_exp_elt_type ($2);
458 write_exp_elt_opcode (UNOP_MEMVAL); }
461 exp : '(' type ')' exp %prec UNARY
462 { write_exp_elt_opcode (UNOP_CAST);
463 write_exp_elt_type ($2);
464 write_exp_elt_opcode (UNOP_CAST); }
471 /* Binary operators in order of decreasing precedence. */
474 { write_exp_elt_opcode (BINOP_REPEAT); }
478 { write_exp_elt_opcode (BINOP_MUL); }
482 { write_exp_elt_opcode (BINOP_DIV); }
486 { write_exp_elt_opcode (BINOP_REM); }
490 { write_exp_elt_opcode (BINOP_ADD); }
494 { write_exp_elt_opcode (BINOP_SUB); }
498 { write_exp_elt_opcode (BINOP_LSH); }
502 { write_exp_elt_opcode (BINOP_RSH); }
506 { write_exp_elt_opcode (BINOP_EQUAL); }
509 exp : exp NOTEQUAL exp
510 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
514 { write_exp_elt_opcode (BINOP_LEQ); }
518 { write_exp_elt_opcode (BINOP_GEQ); }
522 { write_exp_elt_opcode (BINOP_LESS); }
526 { write_exp_elt_opcode (BINOP_GTR); }
530 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
534 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
538 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
542 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
546 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
549 exp : exp '?' exp ':' exp %prec '?'
550 { write_exp_elt_opcode (TERNOP_COND); }
554 { write_exp_elt_opcode (BINOP_ASSIGN); }
557 exp : exp ASSIGN_MODIFY exp
558 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
559 write_exp_elt_opcode ($2);
560 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
564 { write_exp_elt_opcode (OP_LONG);
565 write_exp_elt_type ($1.type);
566 write_exp_elt_longcst ((LONGEST)($1.val));
567 write_exp_elt_opcode (OP_LONG); }
572 struct stoken_vector vec;
575 write_exp_string_vector ($1.type, &vec);
581 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
582 write_exp_elt_opcode (OP_LONG);
583 write_exp_elt_type (val.typed_val_int.type);
584 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
585 write_exp_elt_opcode (OP_LONG);
591 { write_exp_elt_opcode (OP_DOUBLE);
592 write_exp_elt_type ($1.type);
593 write_exp_elt_dblcst ($1.dval);
594 write_exp_elt_opcode (OP_DOUBLE); }
598 { write_exp_elt_opcode (OP_DECFLOAT);
599 write_exp_elt_type ($1.type);
600 write_exp_elt_decfloatcst ($1.val);
601 write_exp_elt_opcode (OP_DECFLOAT); }
609 write_dollar_variable ($1);
613 exp : SIZEOF '(' type ')' %prec UNARY
614 { write_exp_elt_opcode (OP_LONG);
615 write_exp_elt_type (lookup_signed_typename
616 (parse_language, parse_gdbarch,
619 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
620 write_exp_elt_opcode (OP_LONG); }
623 exp : REINTERPRET_CAST '<' type '>' '(' exp ')' %prec UNARY
624 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST);
625 write_exp_elt_type ($3);
626 write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
629 exp : STATIC_CAST '<' type '>' '(' exp ')' %prec UNARY
630 { write_exp_elt_opcode (UNOP_CAST);
631 write_exp_elt_type ($3);
632 write_exp_elt_opcode (UNOP_CAST); }
635 exp : DYNAMIC_CAST '<' type '>' '(' exp ')' %prec UNARY
636 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST);
637 write_exp_elt_type ($3);
638 write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
641 exp : CONST_CAST '<' type '>' '(' exp ')' %prec UNARY
642 { /* We could do more error checking here, but
643 it doesn't seem worthwhile. */
644 write_exp_elt_opcode (UNOP_CAST);
645 write_exp_elt_type ($3);
646 write_exp_elt_opcode (UNOP_CAST); }
652 /* We copy the string here, and not in the
653 lexer, to guarantee that we do not leak a
654 string. Note that we follow the
655 NUL-termination convention of the
657 struct typed_stoken *vec = XNEW (struct typed_stoken);
662 vec->length = $1.length;
663 vec->ptr = malloc ($1.length + 1);
664 memcpy (vec->ptr, $1.ptr, $1.length + 1);
669 /* Note that we NUL-terminate here, but just
673 $$.tokens = realloc ($$.tokens,
674 $$.len * sizeof (struct typed_stoken));
676 p = malloc ($2.length + 1);
677 memcpy (p, $2.ptr, $2.length + 1);
679 $$.tokens[$$.len - 1].type = $2.type;
680 $$.tokens[$$.len - 1].length = $2.length;
681 $$.tokens[$$.len - 1].ptr = p;
688 enum c_string_type type = C_STRING;
690 for (i = 0; i < $1.len; ++i)
692 switch ($1.tokens[i].type)
700 && type != $1.tokens[i].type)
701 error (_("Undefined string concatenation."));
702 type = $1.tokens[i].type;
706 internal_error (__FILE__, __LINE__,
707 "unrecognized type in string concatenation");
711 write_exp_string_vector (type, &$1);
712 for (i = 0; i < $1.len; ++i)
713 free ($1.tokens[i].ptr);
720 { write_exp_elt_opcode (OP_LONG);
721 write_exp_elt_type (parse_type->builtin_bool);
722 write_exp_elt_longcst ((LONGEST) 1);
723 write_exp_elt_opcode (OP_LONG); }
727 { write_exp_elt_opcode (OP_LONG);
728 write_exp_elt_type (parse_type->builtin_bool);
729 write_exp_elt_longcst ((LONGEST) 0);
730 write_exp_elt_opcode (OP_LONG); }
738 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
740 error (_("No file or function \"%s\"."),
741 copy_name ($1.stoken));
749 block : block COLONCOLON name
751 = lookup_symbol (copy_name ($3), $1,
752 VAR_DOMAIN, (int *) NULL);
753 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
754 error (_("No function \"%s\" in specified context."),
756 $$ = SYMBOL_BLOCK_VALUE (tem); }
759 variable: name_not_typename ENTRY
760 { struct symbol *sym = $1.sym;
762 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
763 || !symbol_read_needs_frame (sym))
764 error (_("@entry can be used only for function "
765 "parameters, not for \"%s\""),
766 copy_name ($1.stoken));
768 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
769 write_exp_elt_sym (sym);
770 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
774 variable: block COLONCOLON name
775 { struct symbol *sym;
776 sym = lookup_symbol (copy_name ($3), $1,
777 VAR_DOMAIN, (int *) NULL);
779 error (_("No symbol \"%s\" in specified context."),
781 if (symbol_read_needs_frame (sym))
783 if (innermost_block == 0
784 || contained_in (block_found,
786 innermost_block = block_found;
789 write_exp_elt_opcode (OP_VAR_VALUE);
790 /* block_found is set by lookup_symbol. */
791 write_exp_elt_block (block_found);
792 write_exp_elt_sym (sym);
793 write_exp_elt_opcode (OP_VAR_VALUE); }
796 qualified_name: TYPENAME COLONCOLON name
798 struct type *type = $1.type;
799 CHECK_TYPEDEF (type);
800 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
801 && TYPE_CODE (type) != TYPE_CODE_UNION
802 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
803 error (_("`%s' is not defined as an aggregate type."),
806 write_exp_elt_opcode (OP_SCOPE);
807 write_exp_elt_type (type);
808 write_exp_string ($3);
809 write_exp_elt_opcode (OP_SCOPE);
811 | TYPENAME COLONCOLON '~' name
813 struct type *type = $1.type;
814 struct stoken tmp_token;
815 CHECK_TYPEDEF (type);
816 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
817 && TYPE_CODE (type) != TYPE_CODE_UNION
818 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
819 error (_("`%s' is not defined as an aggregate type."),
822 tmp_token.ptr = (char*) alloca ($4.length + 2);
823 tmp_token.length = $4.length + 1;
824 tmp_token.ptr[0] = '~';
825 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
826 tmp_token.ptr[tmp_token.length] = 0;
828 /* Check for valid destructor name. */
829 destructor_name_p (tmp_token.ptr, $1.type);
830 write_exp_elt_opcode (OP_SCOPE);
831 write_exp_elt_type (type);
832 write_exp_string (tmp_token);
833 write_exp_elt_opcode (OP_SCOPE);
835 | TYPENAME COLONCOLON name COLONCOLON name
837 char *copy = copy_name ($3);
838 error (_("No type \"%s\" within class "
839 "or namespace \"%s\"."),
840 copy, TYPE_NAME ($1.type));
844 variable: qualified_name
845 | COLONCOLON name_not_typename
847 char *name = copy_name ($2.stoken);
849 struct minimal_symbol *msymbol;
852 lookup_symbol (name, (const struct block *) NULL,
853 VAR_DOMAIN, (int *) NULL);
856 write_exp_elt_opcode (OP_VAR_VALUE);
857 write_exp_elt_block (NULL);
858 write_exp_elt_sym (sym);
859 write_exp_elt_opcode (OP_VAR_VALUE);
863 msymbol = lookup_minimal_symbol (name, NULL, NULL);
865 write_exp_msymbol (msymbol);
866 else if (!have_full_symbols () && !have_partial_symbols ())
867 error (_("No symbol table is loaded. Use the \"file\" command."));
869 error (_("No symbol \"%s\" in current context."), name);
873 variable: name_not_typename
874 { struct symbol *sym = $1.sym;
878 if (symbol_read_needs_frame (sym))
880 if (innermost_block == 0
881 || contained_in (block_found,
883 innermost_block = block_found;
886 write_exp_elt_opcode (OP_VAR_VALUE);
887 /* We want to use the selected frame, not
888 another more inner frame which happens to
889 be in the same block. */
890 write_exp_elt_block (NULL);
891 write_exp_elt_sym (sym);
892 write_exp_elt_opcode (OP_VAR_VALUE);
894 else if ($1.is_a_field_of_this)
896 /* C++: it hangs off of `this'. Must
897 not inadvertently convert from a method call
899 if (innermost_block == 0
900 || contained_in (block_found,
902 innermost_block = block_found;
903 write_exp_elt_opcode (OP_THIS);
904 write_exp_elt_opcode (OP_THIS);
905 write_exp_elt_opcode (STRUCTOP_PTR);
906 write_exp_string ($1.stoken);
907 write_exp_elt_opcode (STRUCTOP_PTR);
911 struct minimal_symbol *msymbol;
912 char *arg = copy_name ($1.stoken);
915 lookup_minimal_symbol (arg, NULL, NULL);
917 write_exp_msymbol (msymbol);
918 else if (!have_full_symbols () && !have_partial_symbols ())
919 error (_("No symbol table is loaded. Use the \"file\" command."));
921 error (_("No symbol \"%s\" in current context."),
922 copy_name ($1.stoken));
927 space_identifier : '@' NAME
928 { push_type_address_space (copy_name ($2.stoken));
929 push_type (tp_space_identifier);
933 const_or_volatile: const_or_volatile_noopt
937 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
940 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
941 | const_or_volatile_noopt
944 const_or_volatile_or_space_identifier:
945 const_or_volatile_or_space_identifier_noopt
950 { push_type (tp_pointer); $$ = 0; }
952 { push_type (tp_pointer); $$ = $2; }
954 { push_type (tp_reference); $$ = 0; }
956 { push_type (tp_reference); $$ = $2; }
960 direct_abs_decl: '(' abs_decl ')'
962 | direct_abs_decl array_mod
965 push_type (tp_array);
970 push_type (tp_array);
974 | direct_abs_decl func_mod
975 { push_type (tp_function); }
977 { push_type (tp_function); }
988 | '(' nonempty_typelist ')'
989 { free ($2); $$ = 0; }
992 /* We used to try to recognize pointer to member types here, but
993 that didn't work (shift/reduce conflicts meant that these rules never
994 got executed). The problem is that
995 int (foo::bar::baz::bizzle)
996 is a function type but
997 int (foo::bar::baz::bizzle::*)
998 is a pointer to member type. Stroustrup loses again! */
1003 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1007 { $$ = lookup_signed_typename (parse_language,
1011 { $$ = lookup_signed_typename (parse_language,
1015 { $$ = lookup_signed_typename (parse_language,
1019 { $$ = lookup_signed_typename (parse_language,
1022 | LONG SIGNED_KEYWORD INT_KEYWORD
1023 { $$ = lookup_signed_typename (parse_language,
1026 | LONG SIGNED_KEYWORD
1027 { $$ = lookup_signed_typename (parse_language,
1030 | SIGNED_KEYWORD LONG INT_KEYWORD
1031 { $$ = lookup_signed_typename (parse_language,
1034 | UNSIGNED LONG INT_KEYWORD
1035 { $$ = lookup_unsigned_typename (parse_language,
1038 | LONG UNSIGNED INT_KEYWORD
1039 { $$ = lookup_unsigned_typename (parse_language,
1043 { $$ = lookup_unsigned_typename (parse_language,
1047 { $$ = lookup_signed_typename (parse_language,
1050 | LONG LONG INT_KEYWORD
1051 { $$ = lookup_signed_typename (parse_language,
1054 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1055 { $$ = lookup_signed_typename (parse_language,
1058 | LONG LONG SIGNED_KEYWORD
1059 { $$ = lookup_signed_typename (parse_language,
1062 | SIGNED_KEYWORD LONG LONG
1063 { $$ = lookup_signed_typename (parse_language,
1066 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1067 { $$ = lookup_signed_typename (parse_language,
1070 | UNSIGNED LONG LONG
1071 { $$ = lookup_unsigned_typename (parse_language,
1074 | UNSIGNED LONG LONG INT_KEYWORD
1075 { $$ = lookup_unsigned_typename (parse_language,
1078 | LONG LONG UNSIGNED
1079 { $$ = lookup_unsigned_typename (parse_language,
1082 | LONG LONG UNSIGNED INT_KEYWORD
1083 { $$ = lookup_unsigned_typename (parse_language,
1087 { $$ = lookup_signed_typename (parse_language,
1090 | SHORT SIGNED_KEYWORD INT_KEYWORD
1091 { $$ = lookup_signed_typename (parse_language,
1094 | SHORT SIGNED_KEYWORD
1095 { $$ = lookup_signed_typename (parse_language,
1098 | UNSIGNED SHORT INT_KEYWORD
1099 { $$ = lookup_unsigned_typename (parse_language,
1103 { $$ = lookup_unsigned_typename (parse_language,
1106 | SHORT UNSIGNED INT_KEYWORD
1107 { $$ = lookup_unsigned_typename (parse_language,
1111 { $$ = lookup_typename (parse_language, parse_gdbarch,
1112 "double", (struct block *) NULL,
1114 | LONG DOUBLE_KEYWORD
1115 { $$ = lookup_typename (parse_language, parse_gdbarch,
1117 (struct block *) NULL, 0); }
1119 { $$ = lookup_struct (copy_name ($2),
1120 expression_context_block); }
1122 { $$ = lookup_struct (copy_name ($2),
1123 expression_context_block); }
1125 { $$ = lookup_union (copy_name ($2),
1126 expression_context_block); }
1128 { $$ = lookup_enum (copy_name ($2),
1129 expression_context_block); }
1131 { $$ = lookup_unsigned_typename (parse_language,
1133 TYPE_NAME($2.type)); }
1135 { $$ = lookup_unsigned_typename (parse_language,
1138 | SIGNED_KEYWORD typename
1139 { $$ = lookup_signed_typename (parse_language,
1141 TYPE_NAME($2.type)); }
1143 { $$ = lookup_signed_typename (parse_language,
1146 /* It appears that this rule for templates is never
1147 reduced; template recognition happens by lookahead
1148 in the token processing code in yylex. */
1149 | TEMPLATE name '<' type '>'
1150 { $$ = lookup_template_type(copy_name($2), $4,
1151 expression_context_block);
1153 | const_or_volatile_or_space_identifier_noopt typebase
1154 { $$ = follow_types ($2); }
1155 | typebase const_or_volatile_or_space_identifier_noopt
1156 { $$ = follow_types ($1); }
1162 $$.stoken.ptr = "int";
1163 $$.stoken.length = 3;
1164 $$.type = lookup_signed_typename (parse_language,
1170 $$.stoken.ptr = "long";
1171 $$.stoken.length = 4;
1172 $$.type = lookup_signed_typename (parse_language,
1178 $$.stoken.ptr = "short";
1179 $$.stoken.length = 5;
1180 $$.type = lookup_signed_typename (parse_language,
1188 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1189 $<ivec>$[0] = 1; /* Number of types in vector */
1192 | nonempty_typelist ',' type
1193 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1194 $$ = (struct type **) realloc ((char *) $1, len);
1195 $$[$<ivec>$[0]] = $3;
1200 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1201 { $$ = follow_types ($1); }
1204 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1205 | VOLATILE_KEYWORD CONST_KEYWORD
1208 const_or_volatile_noopt: const_and_volatile
1209 { push_type (tp_const);
1210 push_type (tp_volatile);
1213 { push_type (tp_const); }
1215 { push_type (tp_volatile); }
1218 operator: OPERATOR NEW
1219 { $$ = operator_stoken (" new"); }
1221 { $$ = operator_stoken (" delete "); }
1222 | OPERATOR NEW '[' ']'
1223 { $$ = operator_stoken (" new[]"); }
1224 | OPERATOR DELETE '[' ']'
1225 { $$ = operator_stoken (" delete[] "); }
1227 { $$ = operator_stoken ("+"); }
1229 { $$ = operator_stoken ("-"); }
1231 { $$ = operator_stoken ("*"); }
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 (">"); }
1252 | OPERATOR ASSIGN_MODIFY
1253 { const char *op = "unknown";
1277 case BINOP_BITWISE_IOR:
1280 case BINOP_BITWISE_AND:
1283 case BINOP_BITWISE_XOR:
1290 $$ = operator_stoken (op);
1293 { $$ = operator_stoken ("<<"); }
1295 { $$ = operator_stoken (">>"); }
1297 { $$ = operator_stoken ("=="); }
1299 { $$ = operator_stoken ("!="); }
1301 { $$ = operator_stoken ("<="); }
1303 { $$ = operator_stoken (">="); }
1305 { $$ = operator_stoken ("&&"); }
1307 { $$ = operator_stoken ("||"); }
1308 | OPERATOR INCREMENT
1309 { $$ = operator_stoken ("++"); }
1310 | OPERATOR DECREMENT
1311 { $$ = operator_stoken ("--"); }
1313 { $$ = operator_stoken (","); }
1314 | OPERATOR ARROW_STAR
1315 { $$ = operator_stoken ("->*"); }
1317 { $$ = operator_stoken ("->"); }
1319 { $$ = operator_stoken ("()"); }
1321 { $$ = operator_stoken ("[]"); }
1325 struct ui_file *buf = mem_fileopen ();
1327 c_print_type ($2, NULL, buf, -1, 0);
1328 name = ui_file_xstrdup (buf, &length);
1329 ui_file_delete (buf);
1330 $$ = operator_stoken (name);
1337 name : NAME { $$ = $1.stoken; }
1338 | BLOCKNAME { $$ = $1.stoken; }
1339 | TYPENAME { $$ = $1.stoken; }
1340 | NAME_OR_INT { $$ = $1.stoken; }
1341 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1342 | operator { $$ = $1; }
1345 name_not_typename : NAME
1347 /* These would be useful if name_not_typename was useful, but it is just
1348 a fake for "variable", so these cause reduce/reduce conflicts because
1349 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1350 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1351 context where only a name could occur, this might be useful.
1357 $$.sym = lookup_symbol ($1.ptr,
1358 expression_context_block,
1360 &$$.is_a_field_of_this);
1367 /* Returns a stoken of the operator name given by OP (which does not
1368 include the string "operator"). */
1369 static struct stoken
1370 operator_stoken (const char *op)
1372 static const char *operator_string = "operator";
1373 struct stoken st = { NULL, 0 };
1374 st.length = strlen (operator_string) + strlen (op);
1375 st.ptr = malloc (st.length + 1);
1376 strcpy (st.ptr, operator_string);
1377 strcat (st.ptr, op);
1379 /* The toplevel (c_parse) will free the memory allocated here. */
1380 make_cleanup (free, st.ptr);
1384 /* Take care of parsing a number (anything that starts with a digit).
1385 Set yylval and return the token type; update lexptr.
1386 LEN is the number of characters in it. */
1388 /*** Needs some error checking for the float case ***/
1391 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1393 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1394 here, and we do kind of silly things like cast to unsigned. */
1401 int base = input_radix;
1404 /* Number of "L" suffixes encountered. */
1407 /* We have found a "L" or "U" suffix. */
1408 int found_suffix = 0;
1411 struct type *signed_type;
1412 struct type *unsigned_type;
1419 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1420 point. Return DECFLOAT. */
1422 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1425 putithere->typed_val_decfloat.type
1426 = parse_type->builtin_decfloat;
1427 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1428 gdbarch_byte_order (parse_gdbarch), p);
1433 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1436 putithere->typed_val_decfloat.type
1437 = parse_type->builtin_decdouble;
1438 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1439 gdbarch_byte_order (parse_gdbarch), p);
1444 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1447 putithere->typed_val_decfloat.type
1448 = parse_type->builtin_declong;
1449 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1450 gdbarch_byte_order (parse_gdbarch), p);
1455 if (! parse_c_float (parse_gdbarch, p, len,
1456 &putithere->typed_val_float.dval,
1457 &putithere->typed_val_float.type))
1462 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1506 if (c >= 'A' && c <= 'Z')
1508 if (c != 'l' && c != 'u')
1510 if (c >= '0' && c <= '9')
1518 if (base > 10 && c >= 'a' && c <= 'f')
1522 n += i = c - 'a' + 10;
1535 return ERROR; /* Char not a digit */
1538 return ERROR; /* Invalid digit in this base */
1540 /* Portably test for overflow (only works for nonzero values, so make
1541 a second check for zero). FIXME: Can't we just make n and prevn
1542 unsigned and avoid this? */
1543 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1544 unsigned_p = 1; /* Try something unsigned */
1546 /* Portably test for unsigned overflow.
1547 FIXME: This check is wrong; for example it doesn't find overflow
1548 on 0x123456789 when LONGEST is 32 bits. */
1549 if (c != 'l' && c != 'u' && n != 0)
1551 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1552 error (_("Numeric constant too large."));
1557 /* An integer constant is an int, a long, or a long long. An L
1558 suffix forces it to be long; an LL suffix forces it to be long
1559 long. If not forced to a larger size, it gets the first type of
1560 the above that it fits in. To figure out whether it fits, we
1561 shift it right and see whether anything remains. Note that we
1562 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1563 operation, because many compilers will warn about such a shift
1564 (which always produces a zero result). Sometimes gdbarch_int_bit
1565 or gdbarch_long_bit will be that big, sometimes not. To deal with
1566 the case where it is we just always shift the value more than
1567 once, with fewer bits each time. */
1569 un = (ULONGEST)n >> 2;
1571 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1573 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1575 /* A large decimal (not hex or octal) constant (between INT_MAX
1576 and UINT_MAX) is a long or unsigned long, according to ANSI,
1577 never an unsigned int, but this code treats it as unsigned
1578 int. This probably should be fixed. GCC gives a warning on
1581 unsigned_type = parse_type->builtin_unsigned_int;
1582 signed_type = parse_type->builtin_int;
1584 else if (long_p <= 1
1585 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1587 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1588 unsigned_type = parse_type->builtin_unsigned_long;
1589 signed_type = parse_type->builtin_long;
1594 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1595 < gdbarch_long_long_bit (parse_gdbarch))
1596 /* A long long does not fit in a LONGEST. */
1597 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1599 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1600 high_bit = (ULONGEST) 1 << shift;
1601 unsigned_type = parse_type->builtin_unsigned_long_long;
1602 signed_type = parse_type->builtin_long_long;
1605 putithere->typed_val_int.val = n;
1607 /* If the high bit of the worked out type is set then this number
1608 has to be unsigned. */
1610 if (unsigned_p || (n & high_bit))
1612 putithere->typed_val_int.type = unsigned_type;
1616 putithere->typed_val_int.type = signed_type;
1622 /* Temporary obstack used for holding strings. */
1623 static struct obstack tempbuf;
1624 static int tempbuf_init;
1626 /* Parse a C escape sequence. The initial backslash of the sequence
1627 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1628 last character of the sequence. If OUTPUT is not NULL, the
1629 translated form of the escape sequence will be written there. If
1630 OUTPUT is NULL, no output is written and the call will only affect
1631 *PTR. If an escape sequence is expressed in target bytes, then the
1632 entire sequence will simply be copied to OUTPUT. Return 1 if any
1633 character was emitted, 0 otherwise. */
1636 c_parse_escape (char **ptr, struct obstack *output)
1638 char *tokptr = *ptr;
1641 /* Some escape sequences undergo character set conversion. Those we
1645 /* Hex escapes do not undergo character set conversion, so keep
1646 the escape sequence for later. */
1649 obstack_grow_str (output, "\\x");
1651 if (!isxdigit (*tokptr))
1652 error (_("\\x escape without a following hex digit"));
1653 while (isxdigit (*tokptr))
1656 obstack_1grow (output, *tokptr);
1661 /* Octal escapes do not undergo character set conversion, so
1662 keep the escape sequence for later. */
1674 obstack_grow_str (output, "\\");
1676 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1680 obstack_1grow (output, *tokptr);
1686 /* We handle UCNs later. We could handle them here, but that
1687 would mean a spurious error in the case where the UCN could
1688 be converted to the target charset but not the host
1694 int i, len = c == 'U' ? 8 : 4;
1697 obstack_1grow (output, '\\');
1698 obstack_1grow (output, *tokptr);
1701 if (!isxdigit (*tokptr))
1702 error (_("\\%c escape without a following hex digit"), c);
1703 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1706 obstack_1grow (output, *tokptr);
1712 /* We must pass backslash through so that it does not
1713 cause quoting during the second expansion. */
1716 obstack_grow_str (output, "\\\\");
1720 /* Escapes which undergo conversion. */
1723 obstack_1grow (output, '\a');
1728 obstack_1grow (output, '\b');
1733 obstack_1grow (output, '\f');
1738 obstack_1grow (output, '\n');
1743 obstack_1grow (output, '\r');
1748 obstack_1grow (output, '\t');
1753 obstack_1grow (output, '\v');
1757 /* GCC extension. */
1760 obstack_1grow (output, HOST_ESCAPE_CHAR);
1764 /* Backslash-newline expands to nothing at all. */
1770 /* A few escapes just expand to the character itself. */
1774 /* GCC extensions. */
1779 /* Unrecognized escapes turn into the character itself. */
1782 obstack_1grow (output, *tokptr);
1790 /* Parse a string or character literal from TOKPTR. The string or
1791 character may be wide or unicode. *OUTPTR is set to just after the
1792 end of the literal in the input string. The resulting token is
1793 stored in VALUE. This returns a token value, either STRING or
1794 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1795 number of host characters in the literal. */
1797 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1801 enum c_string_type type;
1803 /* Build the gdb internal form of the input string in tempbuf. Note
1804 that the buffer is null byte terminated *only* for the
1805 convenience of debugging gdb itself and printing the buffer
1806 contents when the buffer contains no embedded nulls. Gdb does
1807 not depend upon the buffer being null byte terminated, it uses
1808 the length string instead. This allows gdb to handle C strings
1809 (as well as strings in other languages) with embedded null
1815 obstack_free (&tempbuf, NULL);
1816 obstack_init (&tempbuf);
1818 /* Record the string type. */
1821 type = C_WIDE_STRING;
1824 else if (*tokptr == 'u')
1829 else if (*tokptr == 'U')
1837 /* Skip the quote. */
1851 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1853 else if (c == quote)
1857 obstack_1grow (&tempbuf, c);
1859 /* FIXME: this does the wrong thing with multi-byte host
1860 characters. We could use mbrlen here, but that would
1861 make "set host-charset" a bit less useful. */
1866 if (*tokptr != quote)
1869 error (_("Unterminated string in expression."));
1871 error (_("Unmatched single quote."));
1876 value->ptr = obstack_base (&tempbuf);
1877 value->length = obstack_object_size (&tempbuf);
1881 return quote == '"' ? STRING : CHAR;
1888 enum exp_opcode opcode;
1892 static const struct token tokentab3[] =
1894 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1895 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
1896 {"->*", ARROW_STAR, BINOP_END, 1}
1899 static const struct token tokentab2[] =
1901 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1902 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1903 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1904 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1905 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1906 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1907 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1908 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1909 {"++", INCREMENT, BINOP_END, 0},
1910 {"--", DECREMENT, BINOP_END, 0},
1911 {"->", ARROW, BINOP_END, 0},
1912 {"&&", ANDAND, BINOP_END, 0},
1913 {"||", OROR, BINOP_END, 0},
1914 /* "::" is *not* only C++: gdb overrides its meaning in several
1915 different ways, e.g., 'filename'::func, function::variable. */
1916 {"::", COLONCOLON, BINOP_END, 0},
1917 {"<<", LSH, BINOP_END, 0},
1918 {">>", RSH, BINOP_END, 0},
1919 {"==", EQUAL, BINOP_END, 0},
1920 {"!=", NOTEQUAL, BINOP_END, 0},
1921 {"<=", LEQ, BINOP_END, 0},
1922 {">=", GEQ, BINOP_END, 0},
1923 {".*", DOT_STAR, BINOP_END, 1}
1926 /* Identifier-like tokens. */
1927 static const struct token ident_tokens[] =
1929 {"unsigned", UNSIGNED, OP_NULL, 0},
1930 {"template", TEMPLATE, OP_NULL, 1},
1931 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1932 {"struct", STRUCT, OP_NULL, 0},
1933 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1934 {"sizeof", SIZEOF, OP_NULL, 0},
1935 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1936 {"false", FALSEKEYWORD, OP_NULL, 1},
1937 {"class", CLASS, OP_NULL, 1},
1938 {"union", UNION, OP_NULL, 0},
1939 {"short", SHORT, OP_NULL, 0},
1940 {"const", CONST_KEYWORD, OP_NULL, 0},
1941 {"enum", ENUM, OP_NULL, 0},
1942 {"long", LONG, OP_NULL, 0},
1943 {"true", TRUEKEYWORD, OP_NULL, 1},
1944 {"int", INT_KEYWORD, OP_NULL, 0},
1945 {"new", NEW, OP_NULL, 1},
1946 {"delete", DELETE, OP_NULL, 1},
1947 {"operator", OPERATOR, OP_NULL, 1},
1949 {"and", ANDAND, BINOP_END, 1},
1950 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1951 {"bitand", '&', OP_NULL, 1},
1952 {"bitor", '|', OP_NULL, 1},
1953 {"compl", '~', OP_NULL, 1},
1954 {"not", '!', OP_NULL, 1},
1955 {"not_eq", NOTEQUAL, BINOP_END, 1},
1956 {"or", OROR, BINOP_END, 1},
1957 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1958 {"xor", '^', OP_NULL, 1},
1959 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1},
1961 {"const_cast", CONST_CAST, OP_NULL, 1 },
1962 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, 1 },
1963 {"static_cast", STATIC_CAST, OP_NULL, 1 },
1964 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, 1 }
1967 /* When we find that lexptr (the global var defined in parse.c) is
1968 pointing at a macro invocation, we expand the invocation, and call
1969 scan_macro_expansion to save the old lexptr here and point lexptr
1970 into the expanded text. When we reach the end of that, we call
1971 end_macro_expansion to pop back to the value we saved here. The
1972 macro expansion code promises to return only fully-expanded text,
1973 so we don't need to "push" more than one level.
1975 This is disgusting, of course. It would be cleaner to do all macro
1976 expansion beforehand, and then hand that to lexptr. But we don't
1977 really know where the expression ends. Remember, in a command like
1979 (gdb) break *ADDRESS if CONDITION
1981 we evaluate ADDRESS in the scope of the current frame, but we
1982 evaluate CONDITION in the scope of the breakpoint's location. So
1983 it's simply wrong to try to macro-expand the whole thing at once. */
1984 static char *macro_original_text;
1986 /* We save all intermediate macro expansions on this obstack for the
1987 duration of a single parse. The expansion text may sometimes have
1988 to live past the end of the expansion, due to yacc lookahead.
1989 Rather than try to be clever about saving the data for a single
1990 token, we simply keep it all and delete it after parsing has
1992 static struct obstack expansion_obstack;
1995 scan_macro_expansion (char *expansion)
1999 /* We'd better not be trying to push the stack twice. */
2000 gdb_assert (! macro_original_text);
2002 /* Copy to the obstack, and then free the intermediate
2004 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2007 /* Save the old lexptr value, so we can return to it when we're done
2008 parsing the expanded text. */
2009 macro_original_text = lexptr;
2015 scanning_macro_expansion (void)
2017 return macro_original_text != 0;
2022 finished_macro_expansion (void)
2024 /* There'd better be something to pop back to. */
2025 gdb_assert (macro_original_text);
2027 /* Pop back to the original text. */
2028 lexptr = macro_original_text;
2029 macro_original_text = 0;
2034 scan_macro_cleanup (void *dummy)
2036 if (macro_original_text)
2037 finished_macro_expansion ();
2039 obstack_free (&expansion_obstack, NULL);
2042 /* Return true iff the token represents a C++ cast operator. */
2045 is_cast_operator (const char *token, int len)
2047 return (! strncmp (token, "dynamic_cast", len)
2048 || ! strncmp (token, "static_cast", len)
2049 || ! strncmp (token, "reinterpret_cast", len)
2050 || ! strncmp (token, "const_cast", len));
2053 /* The scope used for macro expansion. */
2054 static struct macro_scope *expression_macro_scope;
2056 /* This is set if a NAME token appeared at the very end of the input
2057 string, with no whitespace separating the name from the EOF. This
2058 is used only when parsing to do field name completion. */
2059 static int saw_name_at_eof;
2061 /* This is set if the previously-returned token was a structure
2062 operator -- either '.' or ARROW. This is used only when parsing to
2063 do field name completion. */
2064 static int last_was_structop;
2066 /* Read one token, getting characters through lexptr. */
2069 lex_one_token (void)
2075 int saw_structop = last_was_structop;
2078 last_was_structop = 0;
2082 /* Check if this is a macro invocation that we need to expand. */
2083 if (! scanning_macro_expansion ())
2085 char *expanded = macro_expand_next (&lexptr,
2086 standard_macro_lookup,
2087 expression_macro_scope);
2090 scan_macro_expansion (expanded);
2093 prev_lexptr = lexptr;
2096 /* See if it is a special token of length 3. */
2097 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2098 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2100 if (tokentab3[i].cxx_only
2101 && parse_language->la_language != language_cplus)
2105 yylval.opcode = tokentab3[i].opcode;
2106 return tokentab3[i].token;
2109 /* See if it is a special token of length 2. */
2110 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2111 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2113 if (tokentab2[i].cxx_only
2114 && parse_language->la_language != language_cplus)
2118 yylval.opcode = tokentab2[i].opcode;
2119 if (in_parse_field && tokentab2[i].token == ARROW)
2120 last_was_structop = 1;
2121 return tokentab2[i].token;
2124 switch (c = *tokstart)
2127 /* If we were just scanning the result of a macro expansion,
2128 then we need to resume scanning the original text.
2129 If we're parsing for field name completion, and the previous
2130 token allows such completion, return a COMPLETE token.
2131 Otherwise, we were already scanning the original text, and
2132 we're really done. */
2133 if (scanning_macro_expansion ())
2135 finished_macro_expansion ();
2138 else if (saw_name_at_eof)
2140 saw_name_at_eof = 0;
2143 else if (saw_structop)
2162 if (paren_depth == 0)
2169 if (comma_terminates
2171 && ! scanning_macro_expansion ())
2177 /* Might be a floating point number. */
2178 if (lexptr[1] < '0' || lexptr[1] > '9')
2181 last_was_structop = 1;
2182 goto symbol; /* Nope, must be a symbol. */
2184 /* FALL THRU into number case. */
2197 /* It's a number. */
2198 int got_dot = 0, got_e = 0, toktype;
2200 int hex = input_radix > 10;
2202 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2207 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2215 /* This test includes !hex because 'e' is a valid hex digit
2216 and thus does not indicate a floating point number when
2217 the radix is hex. */
2218 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2219 got_dot = got_e = 1;
2220 /* This test does not include !hex, because a '.' always indicates
2221 a decimal floating point number regardless of the radix. */
2222 else if (!got_dot && *p == '.')
2224 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2225 && (*p == '-' || *p == '+'))
2226 /* This is the sign of the exponent, not the end of the
2229 /* We will take any letters or digits. parse_number will
2230 complain if past the radix, or if L or U are not final. */
2231 else if ((*p < '0' || *p > '9')
2232 && ((*p < 'a' || *p > 'z')
2233 && (*p < 'A' || *p > 'Z')))
2236 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2237 if (toktype == ERROR)
2239 char *err_copy = (char *) alloca (p - tokstart + 1);
2241 memcpy (err_copy, tokstart, p - tokstart);
2242 err_copy[p - tokstart] = 0;
2243 error (_("Invalid number \"%s\"."), err_copy);
2251 char *p = &tokstart[1];
2252 size_t len = strlen ("entry");
2254 while (isspace (*p))
2256 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2288 if (tokstart[1] != '"' && tokstart[1] != '\'')
2295 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2300 error (_("Empty character constant."));
2301 else if (host_len > 2 && c == '\'')
2304 namelen = lexptr - tokstart - 1;
2307 else if (host_len > 1)
2308 error (_("Invalid character constant."));
2314 if (!(c == '_' || c == '$'
2315 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2316 /* We must have come across a bad character (e.g. ';'). */
2317 error (_("Invalid character '%c' in expression."), c);
2319 /* It's a name. See how long it is. */
2321 for (c = tokstart[namelen];
2322 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2323 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2325 /* Template parameter lists are part of the name.
2326 FIXME: This mishandles `print $a<4&&$a>3'. */
2330 if (! is_cast_operator (tokstart, namelen))
2332 /* Scan ahead to get rest of the template specification. Note
2333 that we look ahead only when the '<' adjoins non-whitespace
2334 characters; for comparison expressions, e.g. "a < b > c",
2335 there must be spaces before the '<', etc. */
2337 char * p = find_template_name_end (tokstart + namelen);
2339 namelen = p - tokstart;
2343 c = tokstart[++namelen];
2346 /* The token "if" terminates the expression and is NOT removed from
2347 the input stream. It doesn't count if it appears in the
2348 expansion of a macro. */
2350 && tokstart[0] == 'i'
2351 && tokstart[1] == 'f'
2352 && ! scanning_macro_expansion ())
2357 /* For the same reason (breakpoint conditions), "thread N"
2358 terminates the expression. "thread" could be an identifier, but
2359 an identifier is never followed by a number without intervening
2360 punctuation. "task" is similar. Handle abbreviations of these,
2361 similarly to breakpoint.c:find_condition_and_thread. */
2363 && (strncmp (tokstart, "thread", namelen) == 0
2364 || strncmp (tokstart, "task", namelen) == 0)
2365 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2366 && ! scanning_macro_expansion ())
2368 char *p = tokstart + namelen + 1;
2369 while (*p == ' ' || *p == '\t')
2371 if (*p >= '0' && *p <= '9')
2379 yylval.sval.ptr = tokstart;
2380 yylval.sval.length = namelen;
2382 /* Catch specific keywords. */
2383 copy = copy_name (yylval.sval);
2384 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2385 if (strcmp (copy, ident_tokens[i].operator) == 0)
2387 if (ident_tokens[i].cxx_only
2388 && parse_language->la_language != language_cplus)
2391 /* It is ok to always set this, even though we don't always
2392 strictly need to. */
2393 yylval.opcode = ident_tokens[i].opcode;
2394 return ident_tokens[i].token;
2397 if (*tokstart == '$')
2400 if (in_parse_field && *lexptr == '\0')
2401 saw_name_at_eof = 1;
2405 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2412 DEF_VEC_O (token_and_value);
2414 /* A FIFO of tokens that have been read but not yet returned to the
2416 static VEC (token_and_value) *token_fifo;
2418 /* Non-zero if the lexer should return tokens from the FIFO. */
2421 /* Temporary storage for c_lex; this holds symbol names as they are
2423 static struct obstack name_obstack;
2425 /* Classify a NAME token. The contents of the token are in `yylval'.
2426 Updates yylval and returns the new token type. BLOCK is the block
2427 in which lookups start; this can be NULL to mean the global
2430 classify_name (struct block *block)
2434 int is_a_field_of_this = 0;
2436 copy = copy_name (yylval.sval);
2438 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2439 parse_language->la_language == language_cplus
2440 ? &is_a_field_of_this : (int *) NULL);
2442 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2444 yylval.ssym.sym = sym;
2445 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2450 /* See if it's a file name. */
2451 struct symtab *symtab;
2453 symtab = lookup_symtab (copy);
2456 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2461 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2463 yylval.tsym.type = SYMBOL_TYPE (sym);
2468 = language_lookup_primitive_type_by_name (parse_language,
2469 parse_gdbarch, copy);
2470 if (yylval.tsym.type != NULL)
2473 /* Input names that aren't symbols but ARE valid hex numbers, when
2474 the input radix permits them, can be names or numbers depending
2475 on the parse. Note we support radixes > 16 here. */
2477 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2478 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2480 YYSTYPE newlval; /* Its value is ignored. */
2481 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2484 yylval.ssym.sym = sym;
2485 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2490 /* Any other kind of symbol */
2491 yylval.ssym.sym = sym;
2492 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2495 && parse_language->la_language == language_cplus
2496 && !is_a_field_of_this
2497 && !lookup_minimal_symbol (copy, NULL, NULL))
2498 return UNKNOWN_CPP_NAME;
2503 /* Like classify_name, but used by the inner loop of the lexer, when a
2504 name might have already been seen. FIRST_NAME is true if the token
2505 in `yylval' is the first component of a name, false otherwise. If
2506 this function returns NAME, it might not have updated `yylval'.
2507 This is ok because the caller only cares about TYPENAME. */
2509 classify_inner_name (struct block *block, int first_name)
2511 struct type *type, *new_type;
2515 return classify_name (block);
2517 type = check_typedef (yylval.tsym.type);
2518 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2519 && TYPE_CODE (type) != TYPE_CODE_UNION
2520 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2521 /* We know the caller won't expect us to update yylval. */
2524 copy = copy_name (yylval.tsym.stoken);
2525 new_type = cp_lookup_nested_type (yylval.tsym.type, copy, block);
2527 if (new_type == NULL)
2528 /* We know the caller won't expect us to update yylval. */
2531 yylval.tsym.type = new_type;
2535 /* The outer level of a two-level lexer. This calls the inner lexer
2536 to return tokens. It then either returns these tokens, or
2537 aggregates them into a larger token. This lets us work around a
2538 problem in our parsing approach, where the parser could not
2539 distinguish between qualified names and qualified types at the
2542 This approach is still not ideal, because it mishandles template
2543 types. See the comment in lex_one_token for an example. However,
2544 this is still an improvement over the earlier approach, and will
2545 suffice until we move to better parsing technology. */
2549 token_and_value current;
2550 int first_was_coloncolon, last_was_coloncolon, first_iter;
2552 if (popping && !VEC_empty (token_and_value, token_fifo))
2554 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2555 VEC_ordered_remove (token_and_value, token_fifo, 0);
2561 current.token = lex_one_token ();
2562 if (current.token == NAME)
2563 current.token = classify_name (expression_context_block);
2564 if (parse_language->la_language != language_cplus
2565 || (current.token != TYPENAME && current.token != COLONCOLON))
2566 return current.token;
2568 first_was_coloncolon = current.token == COLONCOLON;
2569 last_was_coloncolon = first_was_coloncolon;
2570 obstack_free (&name_obstack, obstack_base (&name_obstack));
2571 if (!last_was_coloncolon)
2572 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2573 current.value = yylval;
2577 token_and_value next;
2579 next.token = lex_one_token ();
2580 next.value = yylval;
2582 if (next.token == NAME && last_was_coloncolon)
2586 classification = classify_inner_name (first_was_coloncolon
2588 : expression_context_block,
2590 /* We keep going until we either run out of names, or until
2591 we have a qualified name which is not a type. */
2592 if (classification != TYPENAME)
2594 /* Push the final component and leave the loop. */
2595 VEC_safe_push (token_and_value, token_fifo, &next);
2599 /* Update the partial name we are constructing. */
2602 /* We don't want to put a leading "::" into the name. */
2603 obstack_grow_str (&name_obstack, "::");
2605 obstack_grow (&name_obstack, next.value.sval.ptr,
2606 next.value.sval.length);
2608 yylval.sval.ptr = obstack_base (&name_obstack);
2609 yylval.sval.length = obstack_object_size (&name_obstack);
2610 current.value = yylval;
2611 current.token = classification;
2613 last_was_coloncolon = 0;
2615 else if (next.token == COLONCOLON && !last_was_coloncolon)
2616 last_was_coloncolon = 1;
2619 /* We've reached the end of the name. */
2620 VEC_safe_push (token_and_value, token_fifo, &next);
2629 /* If we ended with a "::", insert it too. */
2630 if (last_was_coloncolon)
2633 memset (&cc, 0, sizeof (token_and_value));
2634 if (first_was_coloncolon && first_iter)
2639 cc.token = COLONCOLON;
2640 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2643 yylval = current.value;
2644 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2646 yylval.sval.length);
2647 return current.token;
2654 struct cleanup *back_to = make_cleanup (free_current_contents,
2655 &expression_macro_scope);
2657 /* Set up the scope for macro expansion. */
2658 expression_macro_scope = NULL;
2660 if (expression_context_block)
2661 expression_macro_scope
2662 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2664 expression_macro_scope = default_macro_scope ();
2665 if (! expression_macro_scope)
2666 expression_macro_scope = user_macro_scope ();
2668 /* Initialize macro expansion code. */
2669 obstack_init (&expansion_obstack);
2670 gdb_assert (! macro_original_text);
2671 make_cleanup (scan_macro_cleanup, 0);
2673 make_cleanup_restore_integer (&yydebug);
2674 yydebug = parser_debug;
2676 /* Initialize some state used by the lexer. */
2677 last_was_structop = 0;
2678 saw_name_at_eof = 0;
2680 VEC_free (token_and_value, token_fifo);
2682 obstack_init (&name_obstack);
2683 make_cleanup_obstack_free (&name_obstack);
2685 result = yyparse ();
2686 do_cleanups (back_to);
2695 lexptr = prev_lexptr;
2697 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);