1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Parse a C expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
41 #include "gdb_string.h"
43 #include "expression.h"
45 #include "parser-defs.h"
48 #include "bfd.h" /* Required by objfiles.h. */
49 #include "symfile.h" /* Required by objfiles.h. */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 #include "cp-support.h"
55 #include "gdb_assert.h"
56 #include "macroscope.h"
58 #define parse_type builtin_type (parse_gdbarch)
60 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
61 as well as gratuitiously global symbol names, so we can have multiple
62 yacc generated parsers in gdb. Note that these are only the variables
63 produced by yacc. If other parser generators (bison, byacc, etc) produce
64 additional global names that conflict at link time, then those parser
65 generators need to be fixed instead of adding those names to this list. */
67 #define yymaxdepth c_maxdepth
68 #define yyparse c_parse_internal
70 #define yyerror c_error
73 #define yydebug c_debug
82 #define yyerrflag c_errflag
83 #define yynerrs c_nerrs
88 #define yystate c_state
94 #define yyreds c_reds /* With YYDEBUG defined */
95 #define yytoks c_toks /* With YYDEBUG defined */
96 #define yyname c_name /* With YYDEBUG defined */
97 #define yyrule c_rule /* With YYDEBUG defined */
100 #define yydefred c_yydefred
101 #define yydgoto c_yydgoto
102 #define yysindex c_yysindex
103 #define yyrindex c_yyrindex
104 #define yygindex c_yygindex
105 #define yytable c_yytable
106 #define yycheck c_yycheck
109 #define YYDEBUG 1 /* Default to yydebug support */
112 #define YYFPRINTF parser_fprintf
116 static int yylex (void);
118 void yyerror (char *);
122 /* Although the yacc "value" of an expression is not used,
123 since the result is stored in the structure being created,
124 other node types do have values. */
140 } typed_val_decfloat;
145 struct symtoken ssym;
148 enum exp_opcode opcode;
149 struct internalvar *ivar;
156 /* YYSTYPE gets defined by %union */
157 static int parse_number (char *, int, int, YYSTYPE *);
160 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
162 %type <tval> type typebase qualified_type
163 %type <tvec> nonempty_typelist
164 /* %type <bval> block */
166 /* Fancy type parsing. */
167 %type <voidval> func_mod direct_abs_decl abs_decl
169 %type <lval> array_mod
171 %token <typed_val_int> INT
172 %token <typed_val_float> FLOAT
173 %token <typed_val_decfloat> DECFLOAT
175 /* Both NAME and TYPENAME tokens represent symbols in the input,
176 and both convey their data as strings.
177 But a TYPENAME is a string that happens to be defined as a typedef
178 or builtin type name (such as int or char)
179 and a NAME is any other symbol.
180 Contexts where this distinction is not important can use the
181 nonterminal "name", which matches either NAME or TYPENAME. */
184 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
185 %token <voidval> COMPLETE
186 %token <tsym> TYPENAME
187 %type <sval> name string_exp
188 %type <ssym> name_not_typename
189 %type <tsym> typename
191 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
192 but which would parse as a valid number in the current input radix.
193 E.g. "c" when input_radix==16. Depending on the parse, it will be
194 turned into a name or into a number. */
196 %token <ssym> NAME_OR_INT
198 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
202 /* Special type cases, put in to allow the parser to distinguish different
204 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
206 %token <voidval> VARIABLE
208 %token <opcode> ASSIGN_MODIFY
217 %right '=' ASSIGN_MODIFY
225 %left '<' '>' LEQ GEQ
230 %right UNARY INCREMENT DECREMENT
231 %right ARROW '.' '[' '('
232 %token <ssym> BLOCKNAME
233 %token <bval> FILENAME
245 { write_exp_elt_opcode(OP_TYPE);
246 write_exp_elt_type($1);
247 write_exp_elt_opcode(OP_TYPE);}
250 /* Expressions, including the comma operator. */
253 { write_exp_elt_opcode (BINOP_COMMA); }
256 /* Expressions, not including the comma operator. */
257 exp : '*' exp %prec UNARY
258 { write_exp_elt_opcode (UNOP_IND); }
261 exp : '&' exp %prec UNARY
262 { write_exp_elt_opcode (UNOP_ADDR); }
265 exp : '-' exp %prec UNARY
266 { write_exp_elt_opcode (UNOP_NEG); }
269 exp : '+' exp %prec UNARY
270 { write_exp_elt_opcode (UNOP_PLUS); }
273 exp : '!' exp %prec UNARY
274 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
277 exp : '~' exp %prec UNARY
278 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
281 exp : INCREMENT exp %prec UNARY
282 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
285 exp : DECREMENT exp %prec UNARY
286 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
289 exp : exp INCREMENT %prec UNARY
290 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
293 exp : exp DECREMENT %prec UNARY
294 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
297 exp : SIZEOF exp %prec UNARY
298 { write_exp_elt_opcode (UNOP_SIZEOF); }
302 { write_exp_elt_opcode (STRUCTOP_PTR);
303 write_exp_string ($3);
304 write_exp_elt_opcode (STRUCTOP_PTR); }
307 exp : exp ARROW name COMPLETE
308 { mark_struct_expression ();
309 write_exp_elt_opcode (STRUCTOP_PTR);
310 write_exp_string ($3);
311 write_exp_elt_opcode (STRUCTOP_PTR); }
314 exp : exp ARROW COMPLETE
316 mark_struct_expression ();
317 write_exp_elt_opcode (STRUCTOP_PTR);
320 write_exp_string (s);
321 write_exp_elt_opcode (STRUCTOP_PTR); }
324 exp : exp ARROW qualified_name
325 { /* exp->type::name becomes exp->*(&type::name) */
326 /* Note: this doesn't work if name is a
327 static member! FIXME */
328 write_exp_elt_opcode (UNOP_ADDR);
329 write_exp_elt_opcode (STRUCTOP_MPTR); }
332 exp : exp ARROW '*' exp
333 { write_exp_elt_opcode (STRUCTOP_MPTR); }
337 { write_exp_elt_opcode (STRUCTOP_STRUCT);
338 write_exp_string ($3);
339 write_exp_elt_opcode (STRUCTOP_STRUCT); }
342 exp : exp '.' name COMPLETE
343 { mark_struct_expression ();
344 write_exp_elt_opcode (STRUCTOP_STRUCT);
345 write_exp_string ($3);
346 write_exp_elt_opcode (STRUCTOP_STRUCT); }
349 exp : exp '.' COMPLETE
351 mark_struct_expression ();
352 write_exp_elt_opcode (STRUCTOP_STRUCT);
355 write_exp_string (s);
356 write_exp_elt_opcode (STRUCTOP_STRUCT); }
359 exp : exp '.' qualified_name
360 { /* exp.type::name becomes exp.*(&type::name) */
361 /* Note: this doesn't work if name is a
362 static member! FIXME */
363 write_exp_elt_opcode (UNOP_ADDR);
364 write_exp_elt_opcode (STRUCTOP_MEMBER); }
367 exp : exp '.' '*' exp
368 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
371 exp : exp '[' exp1 ']'
372 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
376 /* This is to save the value of arglist_len
377 being accumulated by an outer function call. */
378 { start_arglist (); }
379 arglist ')' %prec ARROW
380 { write_exp_elt_opcode (OP_FUNCALL);
381 write_exp_elt_longcst ((LONGEST) end_arglist ());
382 write_exp_elt_opcode (OP_FUNCALL); }
386 { start_arglist (); }
396 arglist : arglist ',' exp %prec ABOVE_COMMA
401 { $$ = end_arglist () - 1; }
403 exp : lcurly arglist rcurly %prec ARROW
404 { write_exp_elt_opcode (OP_ARRAY);
405 write_exp_elt_longcst ((LONGEST) 0);
406 write_exp_elt_longcst ((LONGEST) $3);
407 write_exp_elt_opcode (OP_ARRAY); }
410 exp : lcurly type rcurly exp %prec UNARY
411 { write_exp_elt_opcode (UNOP_MEMVAL);
412 write_exp_elt_type ($2);
413 write_exp_elt_opcode (UNOP_MEMVAL); }
416 exp : '(' type ')' exp %prec UNARY
417 { write_exp_elt_opcode (UNOP_CAST);
418 write_exp_elt_type ($2);
419 write_exp_elt_opcode (UNOP_CAST); }
426 /* Binary operators in order of decreasing precedence. */
429 { write_exp_elt_opcode (BINOP_REPEAT); }
433 { write_exp_elt_opcode (BINOP_MUL); }
437 { write_exp_elt_opcode (BINOP_DIV); }
441 { write_exp_elt_opcode (BINOP_REM); }
445 { write_exp_elt_opcode (BINOP_ADD); }
449 { write_exp_elt_opcode (BINOP_SUB); }
453 { write_exp_elt_opcode (BINOP_LSH); }
457 { write_exp_elt_opcode (BINOP_RSH); }
461 { write_exp_elt_opcode (BINOP_EQUAL); }
464 exp : exp NOTEQUAL exp
465 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
469 { write_exp_elt_opcode (BINOP_LEQ); }
473 { write_exp_elt_opcode (BINOP_GEQ); }
477 { write_exp_elt_opcode (BINOP_LESS); }
481 { write_exp_elt_opcode (BINOP_GTR); }
485 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
489 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
493 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
497 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
501 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
504 exp : exp '?' exp ':' exp %prec '?'
505 { write_exp_elt_opcode (TERNOP_COND); }
509 { write_exp_elt_opcode (BINOP_ASSIGN); }
512 exp : exp ASSIGN_MODIFY exp
513 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
514 write_exp_elt_opcode ($2);
515 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
519 { write_exp_elt_opcode (OP_LONG);
520 write_exp_elt_type ($1.type);
521 write_exp_elt_longcst ((LONGEST)($1.val));
522 write_exp_elt_opcode (OP_LONG); }
527 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
528 write_exp_elt_opcode (OP_LONG);
529 write_exp_elt_type (val.typed_val_int.type);
530 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
531 write_exp_elt_opcode (OP_LONG);
537 { write_exp_elt_opcode (OP_DOUBLE);
538 write_exp_elt_type ($1.type);
539 write_exp_elt_dblcst ($1.dval);
540 write_exp_elt_opcode (OP_DOUBLE); }
544 { write_exp_elt_opcode (OP_DECFLOAT);
545 write_exp_elt_type ($1.type);
546 write_exp_elt_decfloatcst ($1.val);
547 write_exp_elt_opcode (OP_DECFLOAT); }
554 /* Already written by write_dollar_variable. */
557 exp : SIZEOF '(' type ')' %prec UNARY
558 { write_exp_elt_opcode (OP_LONG);
559 write_exp_elt_type (parse_type->builtin_int);
561 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
562 write_exp_elt_opcode (OP_LONG); }
568 /* We copy the string here, and not in the
569 lexer, to guarantee that we do not leak a
570 string. Note that we follow the
571 NUL-termination convention of the
573 $$.length = $1.length;
574 $$.ptr = malloc ($1.length + 1);
575 memcpy ($$.ptr, $1.ptr, $1.length + 1);
580 /* Note that we NUL-terminate here, but just
583 t.length = $1.length + $2.length;
584 t.ptr = malloc (t.length + 1);
585 memcpy (t.ptr, $1.ptr, $1.length);
586 memcpy (t.ptr + $1.length, $2.ptr, $2.length + 1);
593 { /* C strings are converted into array constants with
594 an explicit null byte added at the end. Thus
595 the array upper bound is the string length.
596 There is no such thing in C as a completely empty
598 char *sp = $1.ptr; int count = $1.length;
601 write_exp_elt_opcode (OP_LONG);
602 write_exp_elt_type (parse_type->builtin_char);
603 write_exp_elt_longcst ((LONGEST)(*sp++));
604 write_exp_elt_opcode (OP_LONG);
606 write_exp_elt_opcode (OP_LONG);
607 write_exp_elt_type (parse_type->builtin_char);
608 write_exp_elt_longcst ((LONGEST)'\0');
609 write_exp_elt_opcode (OP_LONG);
610 write_exp_elt_opcode (OP_ARRAY);
611 write_exp_elt_longcst ((LONGEST) 0);
612 write_exp_elt_longcst ((LONGEST) ($1.length));
613 write_exp_elt_opcode (OP_ARRAY);
620 { write_exp_elt_opcode (OP_LONG);
621 write_exp_elt_type (parse_type->builtin_bool);
622 write_exp_elt_longcst ((LONGEST) 1);
623 write_exp_elt_opcode (OP_LONG); }
627 { write_exp_elt_opcode (OP_LONG);
628 write_exp_elt_type (parse_type->builtin_bool);
629 write_exp_elt_longcst ((LONGEST) 0);
630 write_exp_elt_opcode (OP_LONG); }
638 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
640 error ("No file or function \"%s\".",
641 copy_name ($1.stoken));
649 block : block COLONCOLON name
651 = lookup_symbol (copy_name ($3), $1,
652 VAR_DOMAIN, (int *) NULL);
653 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
654 error ("No function \"%s\" in specified context.",
656 $$ = SYMBOL_BLOCK_VALUE (tem); }
659 variable: block COLONCOLON name
660 { struct symbol *sym;
661 sym = lookup_symbol (copy_name ($3), $1,
662 VAR_DOMAIN, (int *) NULL);
664 error ("No symbol \"%s\" in specified context.",
667 write_exp_elt_opcode (OP_VAR_VALUE);
668 /* block_found is set by lookup_symbol. */
669 write_exp_elt_block (block_found);
670 write_exp_elt_sym (sym);
671 write_exp_elt_opcode (OP_VAR_VALUE); }
674 qualified_name: typebase COLONCOLON name
676 struct type *type = $1;
677 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
678 && TYPE_CODE (type) != TYPE_CODE_UNION
679 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
680 error ("`%s' is not defined as an aggregate type.",
683 write_exp_elt_opcode (OP_SCOPE);
684 write_exp_elt_type (type);
685 write_exp_string ($3);
686 write_exp_elt_opcode (OP_SCOPE);
688 | typebase COLONCOLON '~' name
690 struct type *type = $1;
691 struct stoken tmp_token;
692 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
693 && TYPE_CODE (type) != TYPE_CODE_UNION
694 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
695 error ("`%s' is not defined as an aggregate type.",
698 tmp_token.ptr = (char*) alloca ($4.length + 2);
699 tmp_token.length = $4.length + 1;
700 tmp_token.ptr[0] = '~';
701 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
702 tmp_token.ptr[tmp_token.length] = 0;
704 /* Check for valid destructor name. */
705 destructor_name_p (tmp_token.ptr, type);
706 write_exp_elt_opcode (OP_SCOPE);
707 write_exp_elt_type (type);
708 write_exp_string (tmp_token);
709 write_exp_elt_opcode (OP_SCOPE);
713 variable: qualified_name
716 char *name = copy_name ($2);
718 struct minimal_symbol *msymbol;
721 lookup_symbol (name, (const struct block *) NULL,
722 VAR_DOMAIN, (int *) NULL);
725 write_exp_elt_opcode (OP_VAR_VALUE);
726 write_exp_elt_block (NULL);
727 write_exp_elt_sym (sym);
728 write_exp_elt_opcode (OP_VAR_VALUE);
732 msymbol = lookup_minimal_symbol (name, NULL, NULL);
734 write_exp_msymbol (msymbol);
735 else if (!have_full_symbols () && !have_partial_symbols ())
736 error ("No symbol table is loaded. Use the \"file\" command.");
738 error ("No symbol \"%s\" in current context.", name);
742 variable: name_not_typename
743 { struct symbol *sym = $1.sym;
747 if (symbol_read_needs_frame (sym))
749 if (innermost_block == 0 ||
750 contained_in (block_found,
752 innermost_block = block_found;
755 write_exp_elt_opcode (OP_VAR_VALUE);
756 /* We want to use the selected frame, not
757 another more inner frame which happens to
758 be in the same block. */
759 write_exp_elt_block (NULL);
760 write_exp_elt_sym (sym);
761 write_exp_elt_opcode (OP_VAR_VALUE);
763 else if ($1.is_a_field_of_this)
765 /* C++: it hangs off of `this'. Must
766 not inadvertently convert from a method call
768 if (innermost_block == 0 ||
769 contained_in (block_found, innermost_block))
770 innermost_block = block_found;
771 write_exp_elt_opcode (OP_THIS);
772 write_exp_elt_opcode (OP_THIS);
773 write_exp_elt_opcode (STRUCTOP_PTR);
774 write_exp_string ($1.stoken);
775 write_exp_elt_opcode (STRUCTOP_PTR);
779 struct minimal_symbol *msymbol;
780 char *arg = copy_name ($1.stoken);
783 lookup_minimal_symbol (arg, NULL, NULL);
785 write_exp_msymbol (msymbol);
786 else if (!have_full_symbols () && !have_partial_symbols ())
787 error ("No symbol table is loaded. Use the \"file\" command.");
789 error ("No symbol \"%s\" in current context.",
790 copy_name ($1.stoken));
795 space_identifier : '@' NAME
796 { push_type_address_space (copy_name ($2.stoken));
797 push_type (tp_space_identifier);
801 const_or_volatile: const_or_volatile_noopt
805 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
808 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
809 | const_or_volatile_noopt
812 const_or_volatile_or_space_identifier:
813 const_or_volatile_or_space_identifier_noopt
818 { push_type (tp_pointer); $$ = 0; }
820 { push_type (tp_pointer); $$ = $2; }
822 { push_type (tp_reference); $$ = 0; }
824 { push_type (tp_reference); $$ = $2; }
828 direct_abs_decl: '(' abs_decl ')'
830 | direct_abs_decl array_mod
833 push_type (tp_array);
838 push_type (tp_array);
842 | direct_abs_decl func_mod
843 { push_type (tp_function); }
845 { push_type (tp_function); }
856 | '(' nonempty_typelist ')'
857 { free ($2); $$ = 0; }
860 /* We used to try to recognize pointer to member types here, but
861 that didn't work (shift/reduce conflicts meant that these rules never
862 got executed). The problem is that
863 int (foo::bar::baz::bizzle)
864 is a function type but
865 int (foo::bar::baz::bizzle::*)
866 is a pointer to member type. Stroustrup loses again! */
871 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
875 { $$ = parse_type->builtin_int; }
877 { $$ = parse_type->builtin_long; }
879 { $$ = parse_type->builtin_short; }
881 { $$ = parse_type->builtin_long; }
882 | LONG SIGNED_KEYWORD INT_KEYWORD
883 { $$ = parse_type->builtin_long; }
884 | LONG SIGNED_KEYWORD
885 { $$ = parse_type->builtin_long; }
886 | SIGNED_KEYWORD LONG INT_KEYWORD
887 { $$ = parse_type->builtin_long; }
888 | UNSIGNED LONG INT_KEYWORD
889 { $$ = parse_type->builtin_unsigned_long; }
890 | LONG UNSIGNED INT_KEYWORD
891 { $$ = parse_type->builtin_unsigned_long; }
893 { $$ = parse_type->builtin_unsigned_long; }
895 { $$ = parse_type->builtin_long_long; }
896 | LONG LONG INT_KEYWORD
897 { $$ = parse_type->builtin_long_long; }
898 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
899 { $$ = parse_type->builtin_long_long; }
900 | LONG LONG SIGNED_KEYWORD
901 { $$ = parse_type->builtin_long_long; }
902 | SIGNED_KEYWORD LONG LONG
903 { $$ = parse_type->builtin_long_long; }
904 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
905 { $$ = parse_type->builtin_long_long; }
907 { $$ = parse_type->builtin_unsigned_long_long; }
908 | UNSIGNED LONG LONG INT_KEYWORD
909 { $$ = parse_type->builtin_unsigned_long_long; }
911 { $$ = parse_type->builtin_unsigned_long_long; }
912 | LONG LONG UNSIGNED INT_KEYWORD
913 { $$ = parse_type->builtin_unsigned_long_long; }
915 { $$ = parse_type->builtin_short; }
916 | SHORT SIGNED_KEYWORD INT_KEYWORD
917 { $$ = parse_type->builtin_short; }
918 | SHORT SIGNED_KEYWORD
919 { $$ = parse_type->builtin_short; }
920 | UNSIGNED SHORT INT_KEYWORD
921 { $$ = parse_type->builtin_unsigned_short; }
923 { $$ = parse_type->builtin_unsigned_short; }
924 | SHORT UNSIGNED INT_KEYWORD
925 { $$ = parse_type->builtin_unsigned_short; }
927 { $$ = parse_type->builtin_double; }
928 | LONG DOUBLE_KEYWORD
929 { $$ = parse_type->builtin_long_double; }
931 { $$ = lookup_struct (copy_name ($2),
932 expression_context_block); }
934 { $$ = lookup_struct (copy_name ($2),
935 expression_context_block); }
937 { $$ = lookup_union (copy_name ($2),
938 expression_context_block); }
940 { $$ = lookup_enum (copy_name ($2),
941 expression_context_block); }
943 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
945 { $$ = parse_type->builtin_unsigned_int; }
946 | SIGNED_KEYWORD typename
947 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
949 { $$ = parse_type->builtin_int; }
950 /* It appears that this rule for templates is never
951 reduced; template recognition happens by lookahead
952 in the token processing code in yylex. */
953 | TEMPLATE name '<' type '>'
954 { $$ = lookup_template_type(copy_name($2), $4,
955 expression_context_block);
957 | const_or_volatile_or_space_identifier_noopt typebase
958 { $$ = follow_types ($2); }
959 | typebase const_or_volatile_or_space_identifier_noopt
960 { $$ = follow_types ($1); }
964 /* FIXME: carlton/2003-09-25: This next bit leads to lots of
965 reduce-reduce conflicts, because the parser doesn't know whether or
966 not to use qualified_name or qualified_type: the rules are
967 identical. If the parser is parsing 'A::B::x', then, when it sees
968 the second '::', it knows that the expression to the left of it has
969 to be a type, so it uses qualified_type. But if it is parsing just
970 'A::B', then it doesn't have any way of knowing which rule to use,
971 so there's a reduce-reduce conflict; it picks qualified_name, since
972 that occurs earlier in this file than qualified_type.
974 There's no good way to fix this with the grammar as it stands; as
975 far as I can tell, some of the problems arise from ambiguities that
976 GDB introduces ('start' can be either an expression or a type), but
977 some of it is inherent to the nature of C++ (you want to treat the
978 input "(FOO)" fairly differently depending on whether FOO is an
979 expression or a type, and if FOO is a complex expression, this can
980 be hard to determine at the right time). Fortunately, it works
981 pretty well in most cases. For example, if you do 'ptype A::B',
982 where A::B is a nested type, then the parser will mistakenly
983 misidentify it as an expression; but evaluate_subexp will get
984 called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything
985 will work out anyways. But there are situations where the parser
986 will get confused: the most common one that I've run into is when
991 where the parser doesn't realize that A::B has to be a type until
992 it hits the first right paren, at which point it's too late. (The
993 workaround is to type "print *(('A::B' *) x)" instead.) (And
994 another solution is to fix our symbol-handling code so that the
995 user never wants to type something like that in the first place,
996 because we get all the types right without the user's help!)
998 Perhaps we could fix this by making the lexer smarter. Some of
999 this functionality used to be in the lexer, but in a way that
1000 worked even less well than the current solution: that attempt
1001 involved having the parser sometimes handle '::' and having the
1002 lexer sometimes handle it, and without a clear division of
1003 responsibility, it quickly degenerated into a big mess. Probably
1004 the eventual correct solution will give more of a role to the lexer
1005 (ideally via code that is shared between the lexer and
1006 decode_line_1), but I'm not holding my breath waiting for somebody
1007 to get around to cleaning this up... */
1009 qualified_type: typebase COLONCOLON name
1011 struct type *type = $1;
1012 struct type *new_type;
1013 char *ncopy = alloca ($3.length + 1);
1015 memcpy (ncopy, $3.ptr, $3.length);
1016 ncopy[$3.length] = '\0';
1018 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1019 && TYPE_CODE (type) != TYPE_CODE_UNION
1020 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
1021 error ("`%s' is not defined as an aggregate type.",
1024 new_type = cp_lookup_nested_type (type, ncopy,
1025 expression_context_block);
1026 if (new_type == NULL)
1027 error ("No type \"%s\" within class or namespace \"%s\".",
1028 ncopy, TYPE_NAME (type));
1037 $$.stoken.ptr = "int";
1038 $$.stoken.length = 3;
1039 $$.type = parse_type->builtin_int;
1043 $$.stoken.ptr = "long";
1044 $$.stoken.length = 4;
1045 $$.type = parse_type->builtin_long;
1049 $$.stoken.ptr = "short";
1050 $$.stoken.length = 5;
1051 $$.type = parse_type->builtin_short;
1057 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
1058 $<ivec>$[0] = 1; /* Number of types in vector */
1061 | nonempty_typelist ',' type
1062 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
1063 $$ = (struct type **) realloc ((char *) $1, len);
1064 $$[$<ivec>$[0]] = $3;
1069 | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
1070 { $$ = follow_types ($1); }
1073 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1074 | VOLATILE_KEYWORD CONST_KEYWORD
1077 const_or_volatile_noopt: const_and_volatile
1078 { push_type (tp_const);
1079 push_type (tp_volatile);
1082 { push_type (tp_const); }
1084 { push_type (tp_volatile); }
1087 name : NAME { $$ = $1.stoken; }
1088 | BLOCKNAME { $$ = $1.stoken; }
1089 | TYPENAME { $$ = $1.stoken; }
1090 | NAME_OR_INT { $$ = $1.stoken; }
1093 name_not_typename : NAME
1095 /* These would be useful if name_not_typename was useful, but it is just
1096 a fake for "variable", so these cause reduce/reduce conflicts because
1097 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1098 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1099 context where only a name could occur, this might be useful.
1106 /* Take care of parsing a number (anything that starts with a digit).
1107 Set yylval and return the token type; update lexptr.
1108 LEN is the number of characters in it. */
1110 /*** Needs some error checking for the float case ***/
1113 parse_number (p, len, parsed_float, putithere)
1119 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1120 here, and we do kind of silly things like cast to unsigned. */
1127 int base = input_radix;
1130 /* Number of "L" suffixes encountered. */
1133 /* We have found a "L" or "U" suffix. */
1134 int found_suffix = 0;
1137 struct type *signed_type;
1138 struct type *unsigned_type;
1142 /* It's a float since it contains a point or an exponent. */
1144 int num; /* number of tokens scanned by scanf */
1147 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1148 point. Return DECFLOAT. */
1150 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1153 putithere->typed_val_decfloat.type
1154 = parse_type->builtin_decfloat;
1155 decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
1160 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1163 putithere->typed_val_decfloat.type
1164 = parse_type->builtin_decdouble;
1165 decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
1170 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1173 putithere->typed_val_decfloat.type
1174 = parse_type->builtin_declong;
1175 decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
1181 saved_char = p[len];
1182 p[len] = 0; /* null-terminate the token */
1183 num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
1184 &putithere->typed_val_float.dval, s);
1185 p[len] = saved_char; /* restore the input stream */
1188 putithere->typed_val_float.type =
1189 parse_type->builtin_double;
1193 /* See if it has any float suffix: 'f' for float, 'l' for long
1195 if (!strcasecmp (s, "f"))
1196 putithere->typed_val_float.type =
1197 parse_type->builtin_float;
1198 else if (!strcasecmp (s, "l"))
1199 putithere->typed_val_float.type =
1200 parse_type->builtin_long_double;
1212 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1246 if (c >= 'A' && c <= 'Z')
1248 if (c != 'l' && c != 'u')
1250 if (c >= '0' && c <= '9')
1258 if (base > 10 && c >= 'a' && c <= 'f')
1262 n += i = c - 'a' + 10;
1275 return ERROR; /* Char not a digit */
1278 return ERROR; /* Invalid digit in this base */
1280 /* Portably test for overflow (only works for nonzero values, so make
1281 a second check for zero). FIXME: Can't we just make n and prevn
1282 unsigned and avoid this? */
1283 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1284 unsigned_p = 1; /* Try something unsigned */
1286 /* Portably test for unsigned overflow.
1287 FIXME: This check is wrong; for example it doesn't find overflow
1288 on 0x123456789 when LONGEST is 32 bits. */
1289 if (c != 'l' && c != 'u' && n != 0)
1291 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1292 error ("Numeric constant too large.");
1297 /* An integer constant is an int, a long, or a long long. An L
1298 suffix forces it to be long; an LL suffix forces it to be long
1299 long. If not forced to a larger size, it gets the first type of
1300 the above that it fits in. To figure out whether it fits, we
1301 shift it right and see whether anything remains. Note that we
1302 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1303 operation, because many compilers will warn about such a shift
1304 (which always produces a zero result). Sometimes gdbarch_int_bit
1305 or gdbarch_long_bit will be that big, sometimes not. To deal with
1306 the case where it is we just always shift the value more than
1307 once, with fewer bits each time. */
1309 un = (ULONGEST)n >> 2;
1311 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1313 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1315 /* A large decimal (not hex or octal) constant (between INT_MAX
1316 and UINT_MAX) is a long or unsigned long, according to ANSI,
1317 never an unsigned int, but this code treats it as unsigned
1318 int. This probably should be fixed. GCC gives a warning on
1321 unsigned_type = parse_type->builtin_unsigned_int;
1322 signed_type = parse_type->builtin_int;
1324 else if (long_p <= 1
1325 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1327 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1328 unsigned_type = parse_type->builtin_unsigned_long;
1329 signed_type = parse_type->builtin_long;
1334 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1335 < gdbarch_long_long_bit (parse_gdbarch))
1336 /* A long long does not fit in a LONGEST. */
1337 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1339 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1340 high_bit = (ULONGEST) 1 << shift;
1341 unsigned_type = parse_type->builtin_unsigned_long_long;
1342 signed_type = parse_type->builtin_long_long;
1345 putithere->typed_val_int.val = n;
1347 /* If the high bit of the worked out type is set then this number
1348 has to be unsigned. */
1350 if (unsigned_p || (n & high_bit))
1352 putithere->typed_val_int.type = unsigned_type;
1356 putithere->typed_val_int.type = signed_type;
1366 enum exp_opcode opcode;
1370 static const struct token tokentab3[] =
1372 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
1373 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0}
1376 static const struct token tokentab2[] =
1378 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
1379 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
1380 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
1381 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
1382 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
1383 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
1384 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
1385 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
1386 {"++", INCREMENT, BINOP_END, 0},
1387 {"--", DECREMENT, BINOP_END, 0},
1388 {"->", ARROW, BINOP_END, 0},
1389 {"&&", ANDAND, BINOP_END, 0},
1390 {"||", OROR, BINOP_END, 0},
1391 {"::", COLONCOLON, BINOP_END, 0},
1392 {"<<", LSH, BINOP_END, 0},
1393 {">>", RSH, BINOP_END, 0},
1394 {"==", EQUAL, BINOP_END, 0},
1395 {"!=", NOTEQUAL, BINOP_END, 0},
1396 {"<=", LEQ, BINOP_END, 0},
1397 {">=", GEQ, BINOP_END, 0}
1400 /* Identifier-like tokens. */
1401 static const struct token ident_tokens[] =
1403 {"unsigned", UNSIGNED, OP_NULL, 0},
1404 {"template", TEMPLATE, OP_NULL, 1},
1405 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
1406 {"struct", STRUCT, OP_NULL, 0},
1407 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
1408 {"sizeof", SIZEOF, OP_NULL, 0},
1409 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
1410 {"false", FALSEKEYWORD, OP_NULL, 1},
1411 {"class", CLASS, OP_NULL, 1},
1412 {"union", UNION, OP_NULL, 0},
1413 {"short", SHORT, OP_NULL, 0},
1414 {"const", CONST_KEYWORD, OP_NULL, 0},
1415 {"enum", ENUM, OP_NULL, 0},
1416 {"long", LONG, OP_NULL, 0},
1417 {"true", TRUEKEYWORD, OP_NULL, 1},
1418 {"int", INT_KEYWORD, OP_NULL, 0},
1420 {"and", ANDAND, BINOP_END, 1},
1421 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, 1},
1422 {"bitand", '&', OP_NULL, 1},
1423 {"bitor", '|', OP_NULL, 1},
1424 {"compl", '~', OP_NULL, 1},
1425 {"not", '!', OP_NULL, 1},
1426 {"not_eq", NOTEQUAL, BINOP_END, 1},
1427 {"or", OROR, BINOP_END, 1},
1428 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 1},
1429 {"xor", '^', OP_NULL, 1},
1430 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 1}
1433 /* When we find that lexptr (the global var defined in parse.c) is
1434 pointing at a macro invocation, we expand the invocation, and call
1435 scan_macro_expansion to save the old lexptr here and point lexptr
1436 into the expanded text. When we reach the end of that, we call
1437 end_macro_expansion to pop back to the value we saved here. The
1438 macro expansion code promises to return only fully-expanded text,
1439 so we don't need to "push" more than one level.
1441 This is disgusting, of course. It would be cleaner to do all macro
1442 expansion beforehand, and then hand that to lexptr. But we don't
1443 really know where the expression ends. Remember, in a command like
1445 (gdb) break *ADDRESS if CONDITION
1447 we evaluate ADDRESS in the scope of the current frame, but we
1448 evaluate CONDITION in the scope of the breakpoint's location. So
1449 it's simply wrong to try to macro-expand the whole thing at once. */
1450 static char *macro_original_text;
1452 /* We save all intermediate macro expansions on this obstack for the
1453 duration of a single parse. The expansion text may sometimes have
1454 to live past the end of the expansion, due to yacc lookahead.
1455 Rather than try to be clever about saving the data for a single
1456 token, we simply keep it all and delete it after parsing has
1458 static struct obstack expansion_obstack;
1461 scan_macro_expansion (char *expansion)
1465 /* We'd better not be trying to push the stack twice. */
1466 gdb_assert (! macro_original_text);
1468 /* Copy to the obstack, and then free the intermediate
1470 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
1473 /* Save the old lexptr value, so we can return to it when we're done
1474 parsing the expanded text. */
1475 macro_original_text = lexptr;
1481 scanning_macro_expansion (void)
1483 return macro_original_text != 0;
1488 finished_macro_expansion (void)
1490 /* There'd better be something to pop back to. */
1491 gdb_assert (macro_original_text);
1493 /* Pop back to the original text. */
1494 lexptr = macro_original_text;
1495 macro_original_text = 0;
1500 scan_macro_cleanup (void *dummy)
1502 if (macro_original_text)
1503 finished_macro_expansion ();
1505 obstack_free (&expansion_obstack, NULL);
1509 /* The scope used for macro expansion. */
1510 static struct macro_scope *expression_macro_scope;
1512 /* This is set if a NAME token appeared at the very end of the input
1513 string, with no whitespace separating the name from the EOF. This
1514 is used only when parsing to do field name completion. */
1515 static int saw_name_at_eof;
1517 /* This is set if the previously-returned token was a structure
1518 operator -- either '.' or ARROW. This is used only when parsing to
1519 do field name completion. */
1520 static int last_was_structop;
1522 /* Read one token, getting characters through lexptr. */
1533 static char *tempbuf;
1534 static int tempbufsize;
1535 char * token_string = NULL;
1536 int class_prefix = 0;
1537 int saw_structop = last_was_structop;
1540 last_was_structop = 0;
1544 /* Check if this is a macro invocation that we need to expand. */
1545 if (! scanning_macro_expansion ())
1547 char *expanded = macro_expand_next (&lexptr,
1548 standard_macro_lookup,
1549 expression_macro_scope);
1552 scan_macro_expansion (expanded);
1555 prev_lexptr = lexptr;
1558 /* See if it is a special token of length 3. */
1559 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1560 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
1563 yylval.opcode = tokentab3[i].opcode;
1564 return tokentab3[i].token;
1567 /* See if it is a special token of length 2. */
1568 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1569 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
1572 yylval.opcode = tokentab2[i].opcode;
1573 if (in_parse_field && tokentab2[i].token == ARROW)
1574 last_was_structop = 1;
1575 return tokentab2[i].token;
1578 switch (c = *tokstart)
1581 /* If we were just scanning the result of a macro expansion,
1582 then we need to resume scanning the original text.
1583 If we're parsing for field name completion, and the previous
1584 token allows such completion, return a COMPLETE token.
1585 Otherwise, we were already scanning the original text, and
1586 we're really done. */
1587 if (scanning_macro_expansion ())
1589 finished_macro_expansion ();
1592 else if (saw_name_at_eof)
1594 saw_name_at_eof = 0;
1597 else if (saw_structop)
1609 /* We either have a character constant ('0' or '\177' for example)
1610 or we have a quoted symbol reference ('foo(int,int)' in C++
1615 c = parse_escape (&lexptr);
1617 error ("Empty character constant.");
1618 else if (! host_char_to_target (c, &c))
1620 int toklen = lexptr - tokstart + 1;
1621 char *tok = alloca (toklen + 1);
1622 memcpy (tok, tokstart, toklen);
1624 error ("There is no character corresponding to %s in the target "
1625 "character set `%s'.", tok, target_charset ());
1628 yylval.typed_val_int.val = c;
1629 yylval.typed_val_int.type = parse_type->builtin_char;
1634 namelen = skip_quoted (tokstart) - tokstart;
1637 lexptr = tokstart + namelen;
1638 if (lexptr[-1] != '\'')
1639 error ("Unmatched single quote.");
1644 error ("Invalid character constant.");
1654 if (paren_depth == 0)
1661 if (comma_terminates
1663 && ! scanning_macro_expansion ())
1669 /* Might be a floating point number. */
1670 if (lexptr[1] < '0' || lexptr[1] > '9')
1673 last_was_structop = 1;
1674 goto symbol; /* Nope, must be a symbol. */
1676 /* FALL THRU into number case. */
1689 /* It's a number. */
1690 int got_dot = 0, got_e = 0, toktype;
1692 int hex = input_radix > 10;
1694 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1699 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1707 /* This test includes !hex because 'e' is a valid hex digit
1708 and thus does not indicate a floating point number when
1709 the radix is hex. */
1710 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1711 got_dot = got_e = 1;
1712 /* This test does not include !hex, because a '.' always indicates
1713 a decimal floating point number regardless of the radix. */
1714 else if (!got_dot && *p == '.')
1716 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1717 && (*p == '-' || *p == '+'))
1718 /* This is the sign of the exponent, not the end of the
1721 /* We will take any letters or digits. parse_number will
1722 complain if past the radix, or if L or U are not final. */
1723 else if ((*p < '0' || *p > '9')
1724 && ((*p < 'a' || *p > 'z')
1725 && (*p < 'A' || *p > 'Z')))
1728 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1729 if (toktype == ERROR)
1731 char *err_copy = (char *) alloca (p - tokstart + 1);
1733 memcpy (err_copy, tokstart, p - tokstart);
1734 err_copy[p - tokstart] = 0;
1735 error ("Invalid number \"%s\".", err_copy);
1767 /* Build the gdb internal form of the input string in tempbuf,
1768 translating any standard C escape forms seen. Note that the
1769 buffer is null byte terminated *only* for the convenience of
1770 debugging gdb itself and printing the buffer contents when
1771 the buffer contains no embedded nulls. Gdb does not depend
1772 upon the buffer being null byte terminated, it uses the length
1773 string instead. This allows gdb to handle C strings (as well
1774 as strings in other languages) with embedded null bytes */
1776 tokptr = ++tokstart;
1780 char *char_start_pos = tokptr;
1782 /* Grow the static temp buffer if necessary, including allocating
1783 the first one on demand. */
1784 if (tempbufindex + 1 >= tempbufsize)
1786 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1792 /* Do nothing, loop will terminate. */
1796 c = parse_escape (&tokptr);
1801 tempbuf[tempbufindex++] = c;
1805 if (! host_char_to_target (c, &c))
1807 int len = tokptr - char_start_pos;
1808 char *copy = alloca (len + 1);
1809 memcpy (copy, char_start_pos, len);
1812 error ("There is no character corresponding to `%s' "
1813 "in the target character set `%s'.",
1814 copy, target_charset ());
1816 tempbuf[tempbufindex++] = c;
1819 } while ((*tokptr != '"') && (*tokptr != '\0'));
1820 if (*tokptr++ != '"')
1822 error ("Unterminated string in expression.");
1824 tempbuf[tempbufindex] = '\0'; /* See note above */
1825 yylval.sval.ptr = tempbuf;
1826 yylval.sval.length = tempbufindex;
1831 if (!(c == '_' || c == '$'
1832 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1833 /* We must have come across a bad character (e.g. ';'). */
1834 error ("Invalid character '%c' in expression.", c);
1836 /* It's a name. See how long it is. */
1838 for (c = tokstart[namelen];
1839 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1840 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1842 /* Template parameter lists are part of the name.
1843 FIXME: This mishandles `print $a<4&&$a>3'. */
1847 /* Scan ahead to get rest of the template specification. Note
1848 that we look ahead only when the '<' adjoins non-whitespace
1849 characters; for comparison expressions, e.g. "a < b > c",
1850 there must be spaces before the '<', etc. */
1852 char * p = find_template_name_end (tokstart + namelen);
1854 namelen = p - tokstart;
1857 c = tokstart[++namelen];
1860 /* The token "if" terminates the expression and is NOT removed from
1861 the input stream. It doesn't count if it appears in the
1862 expansion of a macro. */
1864 && tokstart[0] == 'i'
1865 && tokstart[1] == 'f'
1866 && ! scanning_macro_expansion ())
1875 yylval.sval.ptr = tokstart;
1876 yylval.sval.length = namelen;
1878 /* Catch specific keywords. */
1879 copy = copy_name (yylval.sval);
1880 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
1881 if (strcmp (copy, ident_tokens[i].operator) == 0)
1883 if (ident_tokens[i].cxx_only
1884 && parse_language->la_language != language_cplus)
1887 /* It is ok to always set this, even though we don't always
1888 strictly need to. */
1889 yylval.opcode = ident_tokens[i].opcode;
1890 return ident_tokens[i].token;
1893 if (*tokstart == '$')
1895 write_dollar_variable (yylval.sval);
1899 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1900 functions or symtabs. If this is not so, then ...
1901 Use token-type TYPENAME for symbols that happen to be defined
1902 currently as names of types; NAME for other symbols.
1903 The caller is not constrained to care about the distinction. */
1906 int is_a_field_of_this = 0;
1909 sym = lookup_symbol (copy, expression_context_block,
1911 parse_language->la_language == language_cplus
1912 ? &is_a_field_of_this : (int *) NULL);
1913 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1914 no psymtabs (coff, xcoff, or some future change to blow away the
1915 psymtabs once once symbols are read). */
1916 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1918 yylval.ssym.sym = sym;
1919 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1923 { /* See if it's a file name. */
1924 struct symtab *symtab;
1926 symtab = lookup_symtab (copy);
1930 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1935 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1937 /* NOTE: carlton/2003-09-25: There used to be code here to
1938 handle nested types. It didn't work very well. See the
1939 comment before qualified_type for more info. */
1940 yylval.tsym.type = SYMBOL_TYPE (sym);
1944 = language_lookup_primitive_type_by_name (parse_language,
1945 parse_gdbarch, copy);
1946 if (yylval.tsym.type != NULL)
1949 /* Input names that aren't symbols but ARE valid hex numbers,
1950 when the input radix permits them, can be names or numbers
1951 depending on the parse. Note we support radixes > 16 here. */
1953 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1954 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1956 YYSTYPE newlval; /* Its value is ignored. */
1957 hextype = parse_number (tokstart, namelen, 0, &newlval);
1960 yylval.ssym.sym = sym;
1961 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1966 /* Any other kind of symbol */
1967 yylval.ssym.sym = sym;
1968 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1969 if (in_parse_field && *lexptr == '\0')
1970 saw_name_at_eof = 1;
1979 struct cleanup *back_to = make_cleanup (free_current_contents,
1980 &expression_macro_scope);
1982 /* Set up the scope for macro expansion. */
1983 expression_macro_scope = NULL;
1985 if (expression_context_block)
1986 expression_macro_scope
1987 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
1989 expression_macro_scope = default_macro_scope ();
1990 if (! expression_macro_scope)
1991 expression_macro_scope = user_macro_scope ();
1993 /* Initialize macro expansion code. */
1994 obstack_init (&expansion_obstack);
1995 gdb_assert (! macro_original_text);
1996 make_cleanup (scan_macro_cleanup, 0);
1998 /* Initialize some state used by the lexer. */
1999 last_was_structop = 0;
2000 saw_name_at_eof = 0;
2002 result = yyparse ();
2003 do_cleanups (back_to);
2013 lexptr = prev_lexptr;
2015 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);