1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994, 1996, 1997
3 Free Software Foundation, Inc.
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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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 */
52 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
53 as well as gratuitiously global symbol names, so we can have multiple
54 yacc generated parsers in gdb. Note that these are only the variables
55 produced by yacc. If other parser generators (bison, byacc, etc) produce
56 additional global names that conflict at link time, then those parser
57 generators need to be fixed instead of adding those names to this list. */
59 #define yymaxdepth c_maxdepth
60 #define yyparse c_parse
62 #define yyerror c_error
65 #define yydebug c_debug
74 #define yyerrflag c_errflag
75 #define yynerrs c_nerrs
80 #define yystate c_state
86 #define yyreds c_reds /* With YYDEBUG defined */
87 #define yytoks c_toks /* With YYDEBUG defined */
90 #define yydefred c_yydefred
91 #define yydgoto c_yydgoto
92 #define yysindex c_yysindex
93 #define yyrindex c_yyrindex
94 #define yygindex c_yygindex
95 #define yytable c_yytable
96 #define yycheck c_yycheck
99 #define YYDEBUG 0 /* Default to no yydebug support */
103 yyparse PARAMS ((void));
106 yylex PARAMS ((void));
109 yyerror PARAMS ((char *));
113 /* Although the yacc "value" of an expression is not used,
114 since the result is stored in the structure being created,
115 other node types do have values. */
132 struct symtoken ssym;
135 enum exp_opcode opcode;
136 struct internalvar *ivar;
143 /* YYSTYPE gets defined by %union */
145 parse_number PARAMS ((char *, int, int, YYSTYPE *));
148 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
150 %type <tval> type typebase
151 %type <tvec> nonempty_typelist
152 /* %type <bval> block */
154 /* Fancy type parsing. */
155 %type <voidval> func_mod direct_abs_decl abs_decl
157 %type <lval> array_mod
159 %token <typed_val_int> INT
160 %token <typed_val_float> FLOAT
162 /* Both NAME and TYPENAME tokens represent symbols in the input,
163 and both convey their data as strings.
164 But a TYPENAME is a string that happens to be defined as a typedef
165 or builtin type name (such as int or char)
166 and a NAME is any other symbol.
167 Contexts where this distinction is not important can use the
168 nonterminal "name", which matches either NAME or TYPENAME. */
171 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
172 %token <tsym> TYPENAME
174 %type <ssym> name_not_typename
175 %type <tsym> typename
177 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
178 but which would parse as a valid number in the current input radix.
179 E.g. "c" when input_radix==16. Depending on the parse, it will be
180 turned into a name or into a number. */
182 %token <ssym> NAME_OR_INT
184 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
188 /* Special type cases, put in to allow the parser to distinguish different
190 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
192 %token <voidval> VARIABLE
194 %token <opcode> ASSIGN_MODIFY
201 %right '=' ASSIGN_MODIFY
209 %left '<' '>' LEQ GEQ
214 %right UNARY INCREMENT DECREMENT
215 %right ARROW '.' '[' '('
216 %token <ssym> BLOCKNAME
217 %token <bval> FILENAME
229 { write_exp_elt_opcode(OP_TYPE);
230 write_exp_elt_type($1);
231 write_exp_elt_opcode(OP_TYPE);}
234 /* Expressions, including the comma operator. */
237 { write_exp_elt_opcode (BINOP_COMMA); }
240 /* Expressions, not including the comma operator. */
241 exp : '*' exp %prec UNARY
242 { write_exp_elt_opcode (UNOP_IND); }
244 exp : '&' exp %prec UNARY
245 { write_exp_elt_opcode (UNOP_ADDR); }
247 exp : '-' exp %prec UNARY
248 { write_exp_elt_opcode (UNOP_NEG); }
251 exp : '!' exp %prec UNARY
252 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
255 exp : '~' exp %prec UNARY
256 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
259 exp : INCREMENT exp %prec UNARY
260 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
263 exp : DECREMENT exp %prec UNARY
264 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
267 exp : exp INCREMENT %prec UNARY
268 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
271 exp : exp DECREMENT %prec UNARY
272 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
275 exp : SIZEOF exp %prec UNARY
276 { write_exp_elt_opcode (UNOP_SIZEOF); }
280 { write_exp_elt_opcode (STRUCTOP_PTR);
281 write_exp_string ($3);
282 write_exp_elt_opcode (STRUCTOP_PTR); }
285 exp : exp ARROW qualified_name
286 { /* exp->type::name becomes exp->*(&type::name) */
287 /* Note: this doesn't work if name is a
288 static member! FIXME */
289 write_exp_elt_opcode (UNOP_ADDR);
290 write_exp_elt_opcode (STRUCTOP_MPTR); }
292 exp : exp ARROW '*' exp
293 { write_exp_elt_opcode (STRUCTOP_MPTR); }
297 { write_exp_elt_opcode (STRUCTOP_STRUCT);
298 write_exp_string ($3);
299 write_exp_elt_opcode (STRUCTOP_STRUCT); }
302 exp : exp '.' qualified_name
303 { /* exp.type::name becomes exp.*(&type::name) */
304 /* Note: this doesn't work if name is a
305 static member! FIXME */
306 write_exp_elt_opcode (UNOP_ADDR);
307 write_exp_elt_opcode (STRUCTOP_MEMBER); }
310 exp : exp '.' '*' exp
311 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
314 exp : exp '[' exp1 ']'
315 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
319 /* This is to save the value of arglist_len
320 being accumulated by an outer function call. */
321 { start_arglist (); }
322 arglist ')' %prec ARROW
323 { write_exp_elt_opcode (OP_FUNCALL);
324 write_exp_elt_longcst ((LONGEST) end_arglist ());
325 write_exp_elt_opcode (OP_FUNCALL); }
329 { start_arglist (); }
339 arglist : arglist ',' exp %prec ABOVE_COMMA
344 { $$ = end_arglist () - 1; }
346 exp : lcurly arglist rcurly %prec ARROW
347 { write_exp_elt_opcode (OP_ARRAY);
348 write_exp_elt_longcst ((LONGEST) 0);
349 write_exp_elt_longcst ((LONGEST) $3);
350 write_exp_elt_opcode (OP_ARRAY); }
353 exp : lcurly type rcurly exp %prec UNARY
354 { write_exp_elt_opcode (UNOP_MEMVAL);
355 write_exp_elt_type ($2);
356 write_exp_elt_opcode (UNOP_MEMVAL); }
359 exp : '(' type ')' exp %prec UNARY
360 { write_exp_elt_opcode (UNOP_CAST);
361 write_exp_elt_type ($2);
362 write_exp_elt_opcode (UNOP_CAST); }
369 /* Binary operators in order of decreasing precedence. */
372 { write_exp_elt_opcode (BINOP_REPEAT); }
376 { write_exp_elt_opcode (BINOP_MUL); }
380 { write_exp_elt_opcode (BINOP_DIV); }
384 { write_exp_elt_opcode (BINOP_REM); }
388 { write_exp_elt_opcode (BINOP_ADD); }
392 { write_exp_elt_opcode (BINOP_SUB); }
396 { write_exp_elt_opcode (BINOP_LSH); }
400 { write_exp_elt_opcode (BINOP_RSH); }
404 { write_exp_elt_opcode (BINOP_EQUAL); }
407 exp : exp NOTEQUAL exp
408 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
412 { write_exp_elt_opcode (BINOP_LEQ); }
416 { write_exp_elt_opcode (BINOP_GEQ); }
420 { write_exp_elt_opcode (BINOP_LESS); }
424 { write_exp_elt_opcode (BINOP_GTR); }
428 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
432 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
436 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
440 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
444 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
447 exp : exp '?' exp ':' exp %prec '?'
448 { write_exp_elt_opcode (TERNOP_COND); }
452 { write_exp_elt_opcode (BINOP_ASSIGN); }
455 exp : exp ASSIGN_MODIFY exp
456 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
457 write_exp_elt_opcode ($2);
458 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
462 { write_exp_elt_opcode (OP_LONG);
463 write_exp_elt_type ($1.type);
464 write_exp_elt_longcst ((LONGEST)($1.val));
465 write_exp_elt_opcode (OP_LONG); }
470 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
471 write_exp_elt_opcode (OP_LONG);
472 write_exp_elt_type (val.typed_val_int.type);
473 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
474 write_exp_elt_opcode (OP_LONG);
480 { write_exp_elt_opcode (OP_DOUBLE);
481 write_exp_elt_type ($1.type);
482 write_exp_elt_dblcst ($1.dval);
483 write_exp_elt_opcode (OP_DOUBLE); }
490 /* Already written by write_dollar_variable. */
493 exp : SIZEOF '(' type ')' %prec UNARY
494 { write_exp_elt_opcode (OP_LONG);
495 write_exp_elt_type (builtin_type_int);
497 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
498 write_exp_elt_opcode (OP_LONG); }
502 { /* C strings are converted into array constants with
503 an explicit null byte added at the end. Thus
504 the array upper bound is the string length.
505 There is no such thing in C as a completely empty
507 char *sp = $1.ptr; int count = $1.length;
510 write_exp_elt_opcode (OP_LONG);
511 write_exp_elt_type (builtin_type_char);
512 write_exp_elt_longcst ((LONGEST)(*sp++));
513 write_exp_elt_opcode (OP_LONG);
515 write_exp_elt_opcode (OP_LONG);
516 write_exp_elt_type (builtin_type_char);
517 write_exp_elt_longcst ((LONGEST)'\0');
518 write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_opcode (OP_ARRAY);
520 write_exp_elt_longcst ((LONGEST) 0);
521 write_exp_elt_longcst ((LONGEST) ($1.length));
522 write_exp_elt_opcode (OP_ARRAY); }
527 { write_exp_elt_opcode (OP_THIS);
528 write_exp_elt_opcode (OP_THIS); }
536 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
538 error ("No file or function \"%s\".",
539 copy_name ($1.stoken));
547 block : block COLONCOLON name
549 = lookup_symbol (copy_name ($3), $1,
550 VAR_NAMESPACE, (int *) NULL,
551 (struct symtab **) NULL);
552 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
553 error ("No function \"%s\" in specified context.",
555 $$ = SYMBOL_BLOCK_VALUE (tem); }
558 variable: block COLONCOLON name
559 { struct symbol *sym;
560 sym = lookup_symbol (copy_name ($3), $1,
561 VAR_NAMESPACE, (int *) NULL,
562 (struct symtab **) NULL);
564 error ("No symbol \"%s\" in specified context.",
567 write_exp_elt_opcode (OP_VAR_VALUE);
568 /* block_found is set by lookup_symbol. */
569 write_exp_elt_block (block_found);
570 write_exp_elt_sym (sym);
571 write_exp_elt_opcode (OP_VAR_VALUE); }
574 qualified_name: typebase COLONCOLON name
576 struct type *type = $1;
577 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
578 && TYPE_CODE (type) != TYPE_CODE_UNION)
579 error ("`%s' is not defined as an aggregate type.",
582 write_exp_elt_opcode (OP_SCOPE);
583 write_exp_elt_type (type);
584 write_exp_string ($3);
585 write_exp_elt_opcode (OP_SCOPE);
587 | typebase COLONCOLON '~' name
589 struct type *type = $1;
590 struct stoken tmp_token;
591 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
592 && TYPE_CODE (type) != TYPE_CODE_UNION)
593 error ("`%s' is not defined as an aggregate type.",
596 tmp_token.ptr = (char*) alloca ($4.length + 2);
597 tmp_token.length = $4.length + 1;
598 tmp_token.ptr[0] = '~';
599 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
600 tmp_token.ptr[tmp_token.length] = 0;
602 /* Check for valid destructor name. */
603 destructor_name_p (tmp_token.ptr, type);
604 write_exp_elt_opcode (OP_SCOPE);
605 write_exp_elt_type (type);
606 write_exp_string (tmp_token);
607 write_exp_elt_opcode (OP_SCOPE);
611 variable: qualified_name
614 char *name = copy_name ($2);
616 struct minimal_symbol *msymbol;
619 lookup_symbol (name, (const struct block *) NULL,
620 VAR_NAMESPACE, (int *) NULL,
621 (struct symtab **) NULL);
624 write_exp_elt_opcode (OP_VAR_VALUE);
625 write_exp_elt_block (NULL);
626 write_exp_elt_sym (sym);
627 write_exp_elt_opcode (OP_VAR_VALUE);
631 msymbol = lookup_minimal_symbol (name, NULL, NULL);
634 write_exp_msymbol (msymbol,
635 lookup_function_type (builtin_type_int),
639 if (!have_full_symbols () && !have_partial_symbols ())
640 error ("No symbol table is loaded. Use the \"file\" command.");
642 error ("No symbol \"%s\" in current context.", name);
646 variable: name_not_typename
647 { struct symbol *sym = $1.sym;
651 if (symbol_read_needs_frame (sym))
653 if (innermost_block == 0 ||
654 contained_in (block_found,
656 innermost_block = block_found;
659 write_exp_elt_opcode (OP_VAR_VALUE);
660 /* We want to use the selected frame, not
661 another more inner frame which happens to
662 be in the same block. */
663 write_exp_elt_block (NULL);
664 write_exp_elt_sym (sym);
665 write_exp_elt_opcode (OP_VAR_VALUE);
667 else if ($1.is_a_field_of_this)
669 /* C++: it hangs off of `this'. Must
670 not inadvertently convert from a method call
672 if (innermost_block == 0 ||
673 contained_in (block_found, innermost_block))
674 innermost_block = block_found;
675 write_exp_elt_opcode (OP_THIS);
676 write_exp_elt_opcode (OP_THIS);
677 write_exp_elt_opcode (STRUCTOP_PTR);
678 write_exp_string ($1.stoken);
679 write_exp_elt_opcode (STRUCTOP_PTR);
683 struct minimal_symbol *msymbol;
684 register char *arg = copy_name ($1.stoken);
687 lookup_minimal_symbol (arg, NULL, NULL);
690 write_exp_msymbol (msymbol,
691 lookup_function_type (builtin_type_int),
694 else if (!have_full_symbols () && !have_partial_symbols ())
695 error ("No symbol table is loaded. Use the \"file\" command.");
697 error ("No symbol \"%s\" in current context.",
698 copy_name ($1.stoken));
705 /* "const" and "volatile" are curently ignored. A type qualifier
706 before the type is currently handled in the typebase rule.
707 The reason for recognizing these here (shift/reduce conflicts)
708 might be obsolete now that some pointer to member rules have
710 | typebase CONST_KEYWORD
711 | typebase VOLATILE_KEYWORD
713 { $$ = follow_types ($1); }
714 | typebase CONST_KEYWORD abs_decl
715 { $$ = follow_types ($1); }
716 | typebase VOLATILE_KEYWORD abs_decl
717 { $$ = follow_types ($1); }
721 { push_type (tp_pointer); $$ = 0; }
723 { push_type (tp_pointer); $$ = $2; }
725 { push_type (tp_reference); $$ = 0; }
727 { push_type (tp_reference); $$ = $2; }
731 direct_abs_decl: '(' abs_decl ')'
733 | direct_abs_decl array_mod
736 push_type (tp_array);
741 push_type (tp_array);
745 | direct_abs_decl func_mod
746 { push_type (tp_function); }
748 { push_type (tp_function); }
759 | '(' nonempty_typelist ')'
760 { free ((PTR)$2); $$ = 0; }
763 /* We used to try to recognize more pointer to member types here, but
764 that didn't work (shift/reduce conflicts meant that these rules never
765 got executed). The problem is that
766 int (foo::bar::baz::bizzle)
767 is a function type but
768 int (foo::bar::baz::bizzle::*)
769 is a pointer to member type. Stroustrup loses again! */
772 | typebase COLONCOLON '*'
773 { $$ = lookup_member_type (builtin_type_int, $1); }
776 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
780 { $$ = builtin_type_int; }
782 { $$ = builtin_type_long; }
784 { $$ = builtin_type_short; }
786 { $$ = builtin_type_long; }
787 | UNSIGNED LONG INT_KEYWORD
788 { $$ = builtin_type_unsigned_long; }
790 { $$ = builtin_type_long_long; }
791 | LONG LONG INT_KEYWORD
792 { $$ = builtin_type_long_long; }
794 { $$ = builtin_type_unsigned_long_long; }
795 | UNSIGNED LONG LONG INT_KEYWORD
796 { $$ = builtin_type_unsigned_long_long; }
798 { $$ = builtin_type_short; }
799 | UNSIGNED SHORT INT_KEYWORD
800 { $$ = builtin_type_unsigned_short; }
802 { $$ = builtin_type_double; }
803 | LONG DOUBLE_KEYWORD
804 { $$ = builtin_type_long_double; }
806 { $$ = lookup_struct (copy_name ($2),
807 expression_context_block); }
809 { $$ = lookup_struct (copy_name ($2),
810 expression_context_block); }
812 { $$ = lookup_union (copy_name ($2),
813 expression_context_block); }
815 { $$ = lookup_enum (copy_name ($2),
816 expression_context_block); }
818 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
820 { $$ = builtin_type_unsigned_int; }
821 | SIGNED_KEYWORD typename
822 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
824 { $$ = builtin_type_int; }
825 | TEMPLATE name '<' type '>'
826 { $$ = lookup_template_type(copy_name($2), $4,
827 expression_context_block);
829 /* "const" and "volatile" are curently ignored. A type qualifier
830 after the type is handled in the ptype rule. I think these could
832 | CONST_KEYWORD typebase { $$ = $2; }
833 | VOLATILE_KEYWORD typebase { $$ = $2; }
839 $$.stoken.ptr = "int";
840 $$.stoken.length = 3;
841 $$.type = builtin_type_int;
845 $$.stoken.ptr = "long";
846 $$.stoken.length = 4;
847 $$.type = builtin_type_long;
851 $$.stoken.ptr = "short";
852 $$.stoken.length = 5;
853 $$.type = builtin_type_short;
859 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
860 $<ivec>$[0] = 1; /* Number of types in vector */
863 | nonempty_typelist ',' type
864 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
865 $$ = (struct type **) realloc ((char *) $1, len);
866 $$[$<ivec>$[0]] = $3;
870 name : NAME { $$ = $1.stoken; }
871 | BLOCKNAME { $$ = $1.stoken; }
872 | TYPENAME { $$ = $1.stoken; }
873 | NAME_OR_INT { $$ = $1.stoken; }
876 name_not_typename : NAME
878 /* These would be useful if name_not_typename was useful, but it is just
879 a fake for "variable", so these cause reduce/reduce conflicts because
880 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
881 =exp) or just an exp. If name_not_typename was ever used in an lvalue
882 context where only a name could occur, this might be useful.
889 /* Take care of parsing a number (anything that starts with a digit).
890 Set yylval and return the token type; update lexptr.
891 LEN is the number of characters in it. */
893 /*** Needs some error checking for the float case ***/
896 parse_number (p, len, parsed_float, putithere)
902 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
903 here, and we do kind of silly things like cast to unsigned. */
904 register LONGEST n = 0;
905 register LONGEST prevn = 0;
910 register int base = input_radix;
913 /* Number of "L" suffixes encountered. */
916 /* We have found a "L" or "U" suffix. */
917 int found_suffix = 0;
920 struct type *signed_type;
921 struct type *unsigned_type;
925 /* It's a float since it contains a point or an exponent. */
927 int num = 0; /* number of tokens scanned by scanf */
928 char saved_char = p[len];
930 p[len] = 0; /* null-terminate the token */
931 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
932 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
933 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
934 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
937 #ifdef SCANF_HAS_LONG_DOUBLE
938 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
940 /* Scan it into a double, then assign it to the long double.
941 This at least wins with values representable in the range
944 num = sscanf (p, "%lg%c", &temp,&c);
945 putithere->typed_val_float.dval = temp;
948 p[len] = saved_char; /* restore the input stream */
949 if (num != 1) /* check scanf found ONLY a float ... */
951 /* See if it has `f' or `l' suffix (float or long double). */
953 c = tolower (p[len - 1]);
956 putithere->typed_val_float.type = builtin_type_float;
958 putithere->typed_val_float.type = builtin_type_long_double;
959 else if (isdigit (c) || c == '.')
960 putithere->typed_val_float.type = builtin_type_double;
967 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1001 if (c >= 'A' && c <= 'Z')
1003 if (c != 'l' && c != 'u')
1005 if (c >= '0' && c <= '9')
1013 if (base > 10 && c >= 'a' && c <= 'f')
1017 n += i = c - 'a' + 10;
1030 return ERROR; /* Char not a digit */
1033 return ERROR; /* Invalid digit in this base */
1035 /* Portably test for overflow (only works for nonzero values, so make
1036 a second check for zero). FIXME: Can't we just make n and prevn
1037 unsigned and avoid this? */
1038 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1039 unsigned_p = 1; /* Try something unsigned */
1041 /* Portably test for unsigned overflow.
1042 FIXME: This check is wrong; for example it doesn't find overflow
1043 on 0x123456789 when LONGEST is 32 bits. */
1044 if (c != 'l' && c != 'u' && n != 0)
1046 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1047 error ("Numeric constant too large.");
1052 /* An integer constant is an int, a long, or a long long. An L
1053 suffix forces it to be long; an LL suffix forces it to be long
1054 long. If not forced to a larger size, it gets the first type of
1055 the above that it fits in. To figure out whether it fits, we
1056 shift it right and see whether anything remains. Note that we
1057 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1058 operation, because many compilers will warn about such a shift
1059 (which always produces a zero result). Sometimes TARGET_INT_BIT
1060 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
1061 the case where it is we just always shift the value more than
1062 once, with fewer bits each time. */
1064 un = (ULONGEST)n >> 2;
1066 && (un >> (TARGET_INT_BIT - 2)) == 0)
1068 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1070 /* A large decimal (not hex or octal) constant (between INT_MAX
1071 and UINT_MAX) is a long or unsigned long, according to ANSI,
1072 never an unsigned int, but this code treats it as unsigned
1073 int. This probably should be fixed. GCC gives a warning on
1076 unsigned_type = builtin_type_unsigned_int;
1077 signed_type = builtin_type_int;
1079 else if (long_p <= 1
1080 && (un >> (TARGET_LONG_BIT - 2)) == 0)
1082 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1083 unsigned_type = builtin_type_unsigned_long;
1084 signed_type = builtin_type_long;
1088 high_bit = (((ULONGEST)1)
1089 << (TARGET_LONG_LONG_BIT - 32 - 1)
1093 /* A long long does not fit in a LONGEST. */
1095 (ULONGEST)1 << (sizeof (LONGEST) * HOST_CHAR_BIT - 1);
1096 unsigned_type = builtin_type_unsigned_long_long;
1097 signed_type = builtin_type_long_long;
1100 putithere->typed_val_int.val = n;
1102 /* If the high bit of the worked out type is set then this number
1103 has to be unsigned. */
1105 if (unsigned_p || (n & high_bit))
1107 putithere->typed_val_int.type = unsigned_type;
1111 putithere->typed_val_int.type = signed_type;
1121 enum exp_opcode opcode;
1124 static const struct token tokentab3[] =
1126 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1127 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1130 static const struct token tokentab2[] =
1132 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1133 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1134 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1135 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1136 {"%=", ASSIGN_MODIFY, BINOP_REM},
1137 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1138 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1139 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1140 {"++", INCREMENT, BINOP_END},
1141 {"--", DECREMENT, BINOP_END},
1142 {"->", ARROW, BINOP_END},
1143 {"&&", ANDAND, BINOP_END},
1144 {"||", OROR, BINOP_END},
1145 {"::", COLONCOLON, BINOP_END},
1146 {"<<", LSH, BINOP_END},
1147 {">>", RSH, BINOP_END},
1148 {"==", EQUAL, BINOP_END},
1149 {"!=", NOTEQUAL, BINOP_END},
1150 {"<=", LEQ, BINOP_END},
1151 {">=", GEQ, BINOP_END}
1154 /* Read one token, getting characters through lexptr. */
1165 static char *tempbuf;
1166 static int tempbufsize;
1171 /* See if it is a special token of length 3. */
1172 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1173 if (STREQN (tokstart, tokentab3[i].operator, 3))
1176 yylval.opcode = tokentab3[i].opcode;
1177 return tokentab3[i].token;
1180 /* See if it is a special token of length 2. */
1181 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1182 if (STREQN (tokstart, tokentab2[i].operator, 2))
1185 yylval.opcode = tokentab2[i].opcode;
1186 return tokentab2[i].token;
1189 switch (c = *tokstart)
1201 /* We either have a character constant ('0' or '\177' for example)
1202 or we have a quoted symbol reference ('foo(int,int)' in C++
1207 c = parse_escape (&lexptr);
1209 error ("Empty character constant.");
1211 yylval.typed_val_int.val = c;
1212 yylval.typed_val_int.type = builtin_type_char;
1217 namelen = skip_quoted (tokstart) - tokstart;
1220 lexptr = tokstart + namelen;
1221 if (lexptr[-1] != '\'')
1222 error ("Unmatched single quote.");
1227 error ("Invalid character constant.");
1237 if (paren_depth == 0)
1244 if (comma_terminates && paren_depth == 0)
1250 /* Might be a floating point number. */
1251 if (lexptr[1] < '0' || lexptr[1] > '9')
1252 goto symbol; /* Nope, must be a symbol. */
1253 /* FALL THRU into number case. */
1266 /* It's a number. */
1267 int got_dot = 0, got_e = 0, toktype;
1268 register char *p = tokstart;
1269 int hex = input_radix > 10;
1271 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1276 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1284 /* This test includes !hex because 'e' is a valid hex digit
1285 and thus does not indicate a floating point number when
1286 the radix is hex. */
1287 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1288 got_dot = got_e = 1;
1289 /* This test does not include !hex, because a '.' always indicates
1290 a decimal floating point number regardless of the radix. */
1291 else if (!got_dot && *p == '.')
1293 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1294 && (*p == '-' || *p == '+'))
1295 /* This is the sign of the exponent, not the end of the
1298 /* We will take any letters or digits. parse_number will
1299 complain if past the radix, or if L or U are not final. */
1300 else if ((*p < '0' || *p > '9')
1301 && ((*p < 'a' || *p > 'z')
1302 && (*p < 'A' || *p > 'Z')))
1305 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1306 if (toktype == ERROR)
1308 char *err_copy = (char *) alloca (p - tokstart + 1);
1310 memcpy (err_copy, tokstart, p - tokstart);
1311 err_copy[p - tokstart] = 0;
1312 error ("Invalid number \"%s\".", err_copy);
1344 /* Build the gdb internal form of the input string in tempbuf,
1345 translating any standard C escape forms seen. Note that the
1346 buffer is null byte terminated *only* for the convenience of
1347 debugging gdb itself and printing the buffer contents when
1348 the buffer contains no embedded nulls. Gdb does not depend
1349 upon the buffer being null byte terminated, it uses the length
1350 string instead. This allows gdb to handle C strings (as well
1351 as strings in other languages) with embedded null bytes */
1353 tokptr = ++tokstart;
1357 /* Grow the static temp buffer if necessary, including allocating
1358 the first one on demand. */
1359 if (tempbufindex + 1 >= tempbufsize)
1361 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1367 /* Do nothing, loop will terminate. */
1371 c = parse_escape (&tokptr);
1376 tempbuf[tempbufindex++] = c;
1379 tempbuf[tempbufindex++] = *tokptr++;
1382 } while ((*tokptr != '"') && (*tokptr != '\0'));
1383 if (*tokptr++ != '"')
1385 error ("Unterminated string in expression.");
1387 tempbuf[tempbufindex] = '\0'; /* See note above */
1388 yylval.sval.ptr = tempbuf;
1389 yylval.sval.length = tempbufindex;
1394 if (!(c == '_' || c == '$'
1395 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1396 /* We must have come across a bad character (e.g. ';'). */
1397 error ("Invalid character '%c' in expression.", c);
1399 /* It's a name. See how long it is. */
1401 for (c = tokstart[namelen];
1402 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1403 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1405 /* Template parameter lists are part of the name.
1406 FIXME: This mishandles `print $a<4&&$a>3'. */
1411 int nesting_level = 1;
1412 while (tokstart[++i])
1414 if (tokstart[i] == '<')
1416 else if (tokstart[i] == '>')
1418 if (--nesting_level == 0)
1422 if (tokstart[i] == '>')
1427 c = tokstart[++namelen];
1430 /* The token "if" terminates the expression and is NOT
1431 removed from the input stream. */
1432 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1441 /* Catch specific keywords. Should be done with a data structure. */
1445 if (STREQN (tokstart, "unsigned", 8))
1447 if (current_language->la_language == language_cplus
1448 && STREQN (tokstart, "template", 8))
1450 if (STREQN (tokstart, "volatile", 8))
1451 return VOLATILE_KEYWORD;
1454 if (STREQN (tokstart, "struct", 6))
1456 if (STREQN (tokstart, "signed", 6))
1457 return SIGNED_KEYWORD;
1458 if (STREQN (tokstart, "sizeof", 6))
1460 if (STREQN (tokstart, "double", 6))
1461 return DOUBLE_KEYWORD;
1464 if (current_language->la_language == language_cplus
1465 && STREQN (tokstart, "class", 5))
1467 if (STREQN (tokstart, "union", 5))
1469 if (STREQN (tokstart, "short", 5))
1471 if (STREQN (tokstart, "const", 5))
1472 return CONST_KEYWORD;
1475 if (STREQN (tokstart, "enum", 4))
1477 if (STREQN (tokstart, "long", 4))
1479 if (current_language->la_language == language_cplus
1480 && STREQN (tokstart, "this", 4))
1482 static const char this_name[] =
1483 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1485 if (lookup_symbol (this_name, expression_context_block,
1486 VAR_NAMESPACE, (int *) NULL,
1487 (struct symtab **) NULL))
1492 if (STREQN (tokstart, "int", 3))
1499 yylval.sval.ptr = tokstart;
1500 yylval.sval.length = namelen;
1502 if (*tokstart == '$')
1504 write_dollar_variable (yylval.sval);
1508 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1509 functions or symtabs. If this is not so, then ...
1510 Use token-type TYPENAME for symbols that happen to be defined
1511 currently as names of types; NAME for other symbols.
1512 The caller is not constrained to care about the distinction. */
1514 char *tmp = copy_name (yylval.sval);
1516 int is_a_field_of_this = 0;
1519 sym = lookup_symbol (tmp, expression_context_block,
1521 current_language->la_language == language_cplus
1522 ? &is_a_field_of_this : (int *) NULL,
1523 (struct symtab **) NULL);
1524 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1525 no psymtabs (coff, xcoff, or some future change to blow away the
1526 psymtabs once once symbols are read). */
1527 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1529 yylval.ssym.sym = sym;
1530 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1534 { /* See if it's a file name. */
1535 struct symtab *symtab;
1537 symtab = lookup_symtab (tmp);
1541 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1546 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1549 /* Despite the following flaw, we need to keep this code enabled.
1550 Because we can get called from check_stub_method, if we don't
1551 handle nested types then it screws many operations in any
1552 program which uses nested types. */
1553 /* In "A::x", if x is a member function of A and there happens
1554 to be a type (nested or not, since the stabs don't make that
1555 distinction) named x, then this code incorrectly thinks we
1556 are dealing with nested types rather than a member function. */
1560 struct symbol *best_sym;
1562 /* Look ahead to detect nested types. This probably should be
1563 done in the grammar, but trying seemed to introduce a lot
1564 of shift/reduce and reduce/reduce conflicts. It's possible
1565 that it could be done, though. Or perhaps a non-grammar, but
1566 less ad hoc, approach would work well. */
1568 /* Since we do not currently have any way of distinguishing
1569 a nested type from a non-nested one (the stabs don't tell
1570 us whether a type is nested), we just ignore the
1577 /* Skip whitespace. */
1578 while (*p == ' ' || *p == '\t' || *p == '\n')
1580 if (*p == ':' && p[1] == ':')
1582 /* Skip the `::'. */
1584 /* Skip whitespace. */
1585 while (*p == ' ' || *p == '\t' || *p == '\n')
1588 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1589 || (*p >= 'a' && *p <= 'z')
1590 || (*p >= 'A' && *p <= 'Z'))
1594 struct symbol *cur_sym;
1595 /* As big as the whole rest of the expression, which is
1596 at least big enough. */
1597 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1601 memcpy (tmp1, tmp, strlen (tmp));
1602 tmp1 += strlen (tmp);
1603 memcpy (tmp1, "::", 2);
1605 memcpy (tmp1, namestart, p - namestart);
1606 tmp1[p - namestart] = '\0';
1607 cur_sym = lookup_symbol (ncopy, expression_context_block,
1608 VAR_NAMESPACE, (int *) NULL,
1609 (struct symtab **) NULL);
1612 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1630 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1632 yylval.tsym.type = SYMBOL_TYPE (sym);
1636 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1639 /* Input names that aren't symbols but ARE valid hex numbers,
1640 when the input radix permits them, can be names or numbers
1641 depending on the parse. Note we support radixes > 16 here. */
1643 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1644 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1646 YYSTYPE newlval; /* Its value is ignored. */
1647 hextype = parse_number (tokstart, namelen, 0, &newlval);
1650 yylval.ssym.sym = sym;
1651 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1656 /* Any other kind of symbol */
1657 yylval.ssym.sym = sym;
1658 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1667 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);