1 /* YACC parser for Pascal expressions, for GDB.
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 /* This file is derived from c-exp.y */
23 /* Parse a Pascal expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result.
32 Note that malloc's and realloc's in this file are transformed to
33 xmalloc and xrealloc respectively by the same sed command in the
34 makefile that remaps any other malloc/realloc inserted by the parser
35 generator. Doing this with #defines and trying to control the interaction
36 with include files (<malloc.h> and <stdlib.h> for example) just became
37 too messy, particularly when such includes can be inserted at random
38 times by the parser generator. */
40 /* Known bugs or limitations:
41 - pascal string operations are not supported at all.
42 - there are some problems with boolean types.
43 - Pascal type hexadecimal constants are not supported
44 because they conflict with the internal variables format.
45 Probably also lots of other problems, less well defined PM */
49 #include "gdb_string.h"
51 #include "expression.h"
53 #include "parser-defs.h"
56 #include "bfd.h" /* Required by objfiles.h. */
57 #include "symfile.h" /* Required by objfiles.h. */
58 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
59 #include "completer.h" /* For skip_quoted(). */
61 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
62 as well as gratuitiously global symbol names, so we can have multiple
63 yacc generated parsers in gdb. Note that these are only the variables
64 produced by yacc. If other parser generators (bison, byacc, etc) produce
65 additional global names that conflict at link time, then those parser
66 generators need to be fixed instead of adding those names to this list. */
68 #define yymaxdepth pascal_maxdepth
69 #define yyparse pascal_parse
70 #define yylex pascal_lex
71 #define yyerror pascal_error
72 #define yylval pascal_lval
73 #define yychar pascal_char
74 #define yydebug pascal_debug
75 #define yypact pascal_pact
76 #define yyr1 pascal_r1
77 #define yyr2 pascal_r2
78 #define yydef pascal_def
79 #define yychk pascal_chk
80 #define yypgo pascal_pgo
81 #define yyact pascal_act
82 #define yyexca pascal_exca
83 #define yyerrflag pascal_errflag
84 #define yynerrs pascal_nerrs
85 #define yyps pascal_ps
86 #define yypv pascal_pv
88 #define yy_yys pascal_yys
89 #define yystate pascal_state
90 #define yytmp pascal_tmp
92 #define yy_yyv pascal_yyv
93 #define yyval pascal_val
94 #define yylloc pascal_lloc
95 #define yyreds pascal_reds /* With YYDEBUG defined */
96 #define yytoks pascal_toks /* With YYDEBUG defined */
97 #define yyname pascal_name /* With YYDEBUG defined */
98 #define yyrule pascal_rule /* With YYDEBUG defined */
99 #define yylhs pascal_yylhs
100 #define yylen pascal_yylen
101 #define yydefred pascal_yydefred
102 #define yydgoto pascal_yydgoto
103 #define yysindex pascal_yysindex
104 #define yyrindex pascal_yyrindex
105 #define yygindex pascal_yygindex
106 #define yytable pascal_yytable
107 #define yycheck pascal_yycheck
110 #define YYDEBUG 1 /* Default to yydebug support */
113 #define YYFPRINTF parser_fprintf
117 static int yylex (void);
122 static char * uptok (char *, int);
125 /* Although the yacc "value" of an expression is not used,
126 since the result is stored in the structure being created,
127 other node types do have values. */
144 struct symtoken ssym;
147 enum exp_opcode opcode;
148 struct internalvar *ivar;
155 /* YYSTYPE gets defined by %union */
157 parse_number (char *, int, int, YYSTYPE *);
159 static struct type *current_type;
161 static void push_current_type ();
162 static void pop_current_type ();
163 static int search_field;
166 %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
167 %type <tval> type typebase
168 /* %type <bval> block */
170 /* Fancy type parsing. */
173 %token <typed_val_int> INT
174 %token <typed_val_float> FLOAT
176 /* Both NAME and TYPENAME tokens represent symbols in the input,
177 and both convey their data as strings.
178 But a TYPENAME is a string that happens to be defined as a typedef
179 or builtin type name (such as int or char)
180 and a NAME is any other symbol.
181 Contexts where this distinction is not important can use the
182 nonterminal "name", which matches either NAME or TYPENAME. */
185 %token <sval> FIELDNAME
186 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
187 %token <tsym> TYPENAME
189 %type <ssym> name_not_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 SIZEOF COLONCOLON
201 /* Special type cases, put in to allow the parser to distinguish different
204 %token <voidval> VARIABLE
209 %token <lval> TRUE FALSE
219 %left '<' '>' LEQ GEQ
220 %left LSH RSH DIV MOD
224 %right UNARY INCREMENT DECREMENT
225 %right ARROW '.' '[' '('
227 %token <ssym> BLOCKNAME
234 start : { current_type = NULL;
245 { write_exp_elt_opcode(OP_TYPE);
246 write_exp_elt_type($1);
247 write_exp_elt_opcode(OP_TYPE);
248 current_type = $1; } ;
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);
260 current_type = TYPE_TARGET_TYPE (current_type); }
262 exp : '@' exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_ADDR);
265 current_type = TYPE_POINTER_TYPE (current_type); }
267 exp : '-' exp %prec UNARY
268 { write_exp_elt_opcode (UNOP_NEG); }
271 exp : NOT exp %prec UNARY
272 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
275 exp : INCREMENT '(' exp ')' %prec UNARY
276 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
279 exp : DECREMENT '(' exp ')' %prec UNARY
280 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
283 exp : exp '.' { search_field = 1; }
286 { write_exp_elt_opcode (STRUCTOP_STRUCT);
287 write_exp_string ($4);
288 write_exp_elt_opcode (STRUCTOP_STRUCT);
291 { while (TYPE_CODE (current_type) == TYPE_CODE_PTR)
292 current_type = TYPE_TARGET_TYPE (current_type);
293 current_type = lookup_struct_elt_type (
294 current_type, $4.ptr, false); };
297 /* We need to save the current_type value */
300 arrayfieldindex = is_pascal_string_type (
301 current_type, NULL, NULL,
302 NULL, NULL, &arrayname);
305 struct stoken stringsval;
306 stringsval.ptr = alloca (strlen (arrayname) + 1);
307 stringsval.length = strlen (arrayname);
308 strcpy (stringsval.ptr, arrayname);
309 current_type = TYPE_FIELD_TYPE (current_type,
310 arrayfieldindex - 1);
311 write_exp_elt_opcode (STRUCTOP_STRUCT);
312 write_exp_string (stringsval);
313 write_exp_elt_opcode (STRUCTOP_STRUCT);
315 push_current_type (); }
317 { pop_current_type ();
318 write_exp_elt_opcode (BINOP_SUBSCRIPT);
320 current_type = TYPE_TARGET_TYPE (current_type); }
323 /* This is to save the value of arglist_len
324 being accumulated by an outer function call. */
325 { push_current_type ();
327 arglist ')' %prec ARROW
328 { write_exp_elt_opcode (OP_FUNCALL);
329 write_exp_elt_longcst ((LONGEST) end_arglist ());
330 write_exp_elt_opcode (OP_FUNCALL);
331 pop_current_type (); }
337 | arglist ',' exp %prec ABOVE_COMMA
341 exp : type '(' exp ')' %prec UNARY
342 { write_exp_elt_opcode (UNOP_CAST);
343 write_exp_elt_type ($1);
344 write_exp_elt_opcode (UNOP_CAST);
352 /* Binary operators in order of decreasing precedence. */
355 { write_exp_elt_opcode (BINOP_MUL); }
359 { write_exp_elt_opcode (BINOP_DIV); }
363 { write_exp_elt_opcode (BINOP_INTDIV); }
367 { write_exp_elt_opcode (BINOP_REM); }
371 { write_exp_elt_opcode (BINOP_ADD); }
375 { write_exp_elt_opcode (BINOP_SUB); }
379 { write_exp_elt_opcode (BINOP_LSH); }
383 { write_exp_elt_opcode (BINOP_RSH); }
387 { write_exp_elt_opcode (BINOP_EQUAL); }
390 exp : exp NOTEQUAL exp
391 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
395 { write_exp_elt_opcode (BINOP_LEQ); }
399 { write_exp_elt_opcode (BINOP_GEQ); }
403 { write_exp_elt_opcode (BINOP_LESS); }
407 { write_exp_elt_opcode (BINOP_GTR); }
411 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
415 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
419 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
423 { write_exp_elt_opcode (BINOP_ASSIGN); }
427 { write_exp_elt_opcode (OP_BOOL);
428 write_exp_elt_longcst ((LONGEST) $1);
429 write_exp_elt_opcode (OP_BOOL); }
433 { write_exp_elt_opcode (OP_BOOL);
434 write_exp_elt_longcst ((LONGEST) $1);
435 write_exp_elt_opcode (OP_BOOL); }
439 { write_exp_elt_opcode (OP_LONG);
440 write_exp_elt_type ($1.type);
441 write_exp_elt_longcst ((LONGEST)($1.val));
442 write_exp_elt_opcode (OP_LONG); }
447 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
448 write_exp_elt_opcode (OP_LONG);
449 write_exp_elt_type (val.typed_val_int.type);
450 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
451 write_exp_elt_opcode (OP_LONG);
457 { write_exp_elt_opcode (OP_DOUBLE);
458 write_exp_elt_type ($1.type);
459 write_exp_elt_dblcst ($1.dval);
460 write_exp_elt_opcode (OP_DOUBLE); }
467 /* Already written by write_dollar_variable. */
470 exp : SIZEOF '(' type ')' %prec UNARY
471 { write_exp_elt_opcode (OP_LONG);
472 write_exp_elt_type (builtin_type_int);
474 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
475 write_exp_elt_opcode (OP_LONG); }
479 { /* C strings are converted into array constants with
480 an explicit null byte added at the end. Thus
481 the array upper bound is the string length.
482 There is no such thing in C as a completely empty
484 char *sp = $1.ptr; int count = $1.length;
487 write_exp_elt_opcode (OP_LONG);
488 write_exp_elt_type (builtin_type_char);
489 write_exp_elt_longcst ((LONGEST)(*sp++));
490 write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_opcode (OP_LONG);
493 write_exp_elt_type (builtin_type_char);
494 write_exp_elt_longcst ((LONGEST)'\0');
495 write_exp_elt_opcode (OP_LONG);
496 write_exp_elt_opcode (OP_ARRAY);
497 write_exp_elt_longcst ((LONGEST) 0);
498 write_exp_elt_longcst ((LONGEST) ($1.length));
499 write_exp_elt_opcode (OP_ARRAY); }
504 { write_exp_elt_opcode (OP_THIS);
505 write_exp_elt_opcode (OP_THIS); }
508 /* end of object pascal. */
513 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
517 lookup_symtab (copy_name ($1.stoken));
519 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
521 error ("No file or function \"%s\".",
522 copy_name ($1.stoken));
527 block : block COLONCOLON name
529 = lookup_symbol (copy_name ($3), $1,
530 VAR_NAMESPACE, (int *) NULL,
531 (struct symtab **) NULL);
532 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
533 error ("No function \"%s\" in specified context.",
535 $$ = SYMBOL_BLOCK_VALUE (tem); }
538 variable: block COLONCOLON name
539 { struct symbol *sym;
540 sym = lookup_symbol (copy_name ($3), $1,
541 VAR_NAMESPACE, (int *) NULL,
542 (struct symtab **) NULL);
544 error ("No symbol \"%s\" in specified context.",
547 write_exp_elt_opcode (OP_VAR_VALUE);
548 /* block_found is set by lookup_symbol. */
549 write_exp_elt_block (block_found);
550 write_exp_elt_sym (sym);
551 write_exp_elt_opcode (OP_VAR_VALUE); }
554 qualified_name: typebase COLONCOLON name
556 struct type *type = $1;
557 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
558 && TYPE_CODE (type) != TYPE_CODE_UNION)
559 error ("`%s' is not defined as an aggregate type.",
562 write_exp_elt_opcode (OP_SCOPE);
563 write_exp_elt_type (type);
564 write_exp_string ($3);
565 write_exp_elt_opcode (OP_SCOPE);
569 variable: qualified_name
572 char *name = copy_name ($2);
574 struct minimal_symbol *msymbol;
577 lookup_symbol (name, (const struct block *) NULL,
578 VAR_NAMESPACE, (int *) NULL,
579 (struct symtab **) NULL);
582 write_exp_elt_opcode (OP_VAR_VALUE);
583 write_exp_elt_block (NULL);
584 write_exp_elt_sym (sym);
585 write_exp_elt_opcode (OP_VAR_VALUE);
589 msymbol = lookup_minimal_symbol (name, NULL, NULL);
592 write_exp_msymbol (msymbol,
593 lookup_function_type (builtin_type_int),
597 if (!have_full_symbols () && !have_partial_symbols ())
598 error ("No symbol table is loaded. Use the \"file\" command.");
600 error ("No symbol \"%s\" in current context.", name);
604 variable: name_not_typename
605 { struct symbol *sym = $1.sym;
609 if (symbol_read_needs_frame (sym))
611 if (innermost_block == 0 ||
612 contained_in (block_found,
614 innermost_block = block_found;
617 write_exp_elt_opcode (OP_VAR_VALUE);
618 /* We want to use the selected frame, not
619 another more inner frame which happens to
620 be in the same block. */
621 write_exp_elt_block (NULL);
622 write_exp_elt_sym (sym);
623 write_exp_elt_opcode (OP_VAR_VALUE);
624 current_type = sym->type; }
625 else if ($1.is_a_field_of_this)
627 struct value * this_val;
628 struct type * this_type;
629 /* Object pascal: it hangs off of `this'. Must
630 not inadvertently convert from a method call
632 if (innermost_block == 0 ||
633 contained_in (block_found, innermost_block))
634 innermost_block = block_found;
635 write_exp_elt_opcode (OP_THIS);
636 write_exp_elt_opcode (OP_THIS);
637 write_exp_elt_opcode (STRUCTOP_PTR);
638 write_exp_string ($1.stoken);
639 write_exp_elt_opcode (STRUCTOP_PTR);
640 /* we need type of this */
641 this_val = value_of_this (0);
643 this_type = this_val->type;
647 current_type = lookup_struct_elt_type (
649 $1.stoken.ptr, false);
655 struct minimal_symbol *msymbol;
656 register char *arg = copy_name ($1.stoken);
659 lookup_minimal_symbol (arg, NULL, NULL);
662 write_exp_msymbol (msymbol,
663 lookup_function_type (builtin_type_int),
666 else if (!have_full_symbols () && !have_partial_symbols ())
667 error ("No symbol table is loaded. Use the \"file\" command.");
669 error ("No symbol \"%s\" in current context.",
670 copy_name ($1.stoken));
679 /* We used to try to recognize more pointer to member types here, but
680 that didn't work (shift/reduce conflicts meant that these rules never
681 got executed). The problem is that
682 int (foo::bar::baz::bizzle)
683 is a function type but
684 int (foo::bar::baz::bizzle::*)
685 is a pointer to member type. Stroustrup loses again! */
688 | typebase COLONCOLON '*'
689 { $$ = lookup_member_type (builtin_type_int, $1); }
692 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
696 { $$ = lookup_struct (copy_name ($2),
697 expression_context_block); }
699 { $$ = lookup_struct (copy_name ($2),
700 expression_context_block); }
701 /* "const" and "volatile" are curently ignored. A type qualifier
702 after the type is handled in the ptype rule. I think these could
706 name : NAME { $$ = $1.stoken; }
707 | BLOCKNAME { $$ = $1.stoken; }
708 | TYPENAME { $$ = $1.stoken; }
709 | NAME_OR_INT { $$ = $1.stoken; }
712 name_not_typename : NAME
714 /* These would be useful if name_not_typename was useful, but it is just
715 a fake for "variable", so these cause reduce/reduce conflicts because
716 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
717 =exp) or just an exp. If name_not_typename was ever used in an lvalue
718 context where only a name could occur, this might be useful.
725 /* Take care of parsing a number (anything that starts with a digit).
726 Set yylval and return the token type; update lexptr.
727 LEN is the number of characters in it. */
729 /*** Needs some error checking for the float case ***/
732 parse_number (p, len, parsed_float, putithere)
738 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
739 here, and we do kind of silly things like cast to unsigned. */
740 register LONGEST n = 0;
741 register LONGEST prevn = 0;
746 register int base = input_radix;
749 /* Number of "L" suffixes encountered. */
752 /* We have found a "L" or "U" suffix. */
753 int found_suffix = 0;
756 struct type *signed_type;
757 struct type *unsigned_type;
761 /* It's a float since it contains a point or an exponent. */
763 int num = 0; /* number of tokens scanned by scanf */
764 char saved_char = p[len];
766 p[len] = 0; /* null-terminate the token */
767 if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
768 num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
769 else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
770 num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
773 #ifdef SCANF_HAS_LONG_DOUBLE
774 num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
776 /* Scan it into a double, then assign it to the long double.
777 This at least wins with values representable in the range
780 num = sscanf (p, "%lg%c", &temp,&c);
781 putithere->typed_val_float.dval = temp;
784 p[len] = saved_char; /* restore the input stream */
785 if (num != 1) /* check scanf found ONLY a float ... */
787 /* See if it has `f' or `l' suffix (float or long double). */
789 c = tolower (p[len - 1]);
792 putithere->typed_val_float.type = builtin_type_float;
794 putithere->typed_val_float.type = builtin_type_long_double;
795 else if (isdigit (c) || c == '.')
796 putithere->typed_val_float.type = builtin_type_double;
803 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
837 if (c >= 'A' && c <= 'Z')
839 if (c != 'l' && c != 'u')
841 if (c >= '0' && c <= '9')
849 if (base > 10 && c >= 'a' && c <= 'f')
853 n += i = c - 'a' + 10;
866 return ERROR; /* Char not a digit */
869 return ERROR; /* Invalid digit in this base */
871 /* Portably test for overflow (only works for nonzero values, so make
872 a second check for zero). FIXME: Can't we just make n and prevn
873 unsigned and avoid this? */
874 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
875 unsigned_p = 1; /* Try something unsigned */
877 /* Portably test for unsigned overflow.
878 FIXME: This check is wrong; for example it doesn't find overflow
879 on 0x123456789 when LONGEST is 32 bits. */
880 if (c != 'l' && c != 'u' && n != 0)
882 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
883 error ("Numeric constant too large.");
888 /* An integer constant is an int, a long, or a long long. An L
889 suffix forces it to be long; an LL suffix forces it to be long
890 long. If not forced to a larger size, it gets the first type of
891 the above that it fits in. To figure out whether it fits, we
892 shift it right and see whether anything remains. Note that we
893 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
894 operation, because many compilers will warn about such a shift
895 (which always produces a zero result). Sometimes TARGET_INT_BIT
896 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
897 the case where it is we just always shift the value more than
898 once, with fewer bits each time. */
900 un = (ULONGEST)n >> 2;
902 && (un >> (TARGET_INT_BIT - 2)) == 0)
904 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
906 /* A large decimal (not hex or octal) constant (between INT_MAX
907 and UINT_MAX) is a long or unsigned long, according to ANSI,
908 never an unsigned int, but this code treats it as unsigned
909 int. This probably should be fixed. GCC gives a warning on
912 unsigned_type = builtin_type_unsigned_int;
913 signed_type = builtin_type_int;
916 && (un >> (TARGET_LONG_BIT - 2)) == 0)
918 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
919 unsigned_type = builtin_type_unsigned_long;
920 signed_type = builtin_type_long;
925 if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
926 /* A long long does not fit in a LONGEST. */
927 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
929 shift = (TARGET_LONG_LONG_BIT - 1);
930 high_bit = (ULONGEST) 1 << shift;
931 unsigned_type = builtin_type_unsigned_long_long;
932 signed_type = builtin_type_long_long;
935 putithere->typed_val_int.val = n;
937 /* If the high bit of the worked out type is set then this number
938 has to be unsigned. */
940 if (unsigned_p || (n & high_bit))
942 putithere->typed_val_int.type = unsigned_type;
946 putithere->typed_val_int.type = signed_type;
956 struct type_push *next;
959 static struct type_push *tp_top = NULL;
961 static void push_current_type ()
963 struct type_push *tpnew;
964 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
965 tpnew->next = tp_top;
966 tpnew->stored = current_type;
971 static void pop_current_type ()
973 struct type_push *tp = tp_top;
976 current_type = tp->stored;
986 enum exp_opcode opcode;
989 static const struct token tokentab3[] =
991 {"shr", RSH, BINOP_END},
992 {"shl", LSH, BINOP_END},
993 {"and", ANDAND, BINOP_END},
994 {"div", DIV, BINOP_END},
995 {"not", NOT, BINOP_END},
996 {"mod", MOD, BINOP_END},
997 {"inc", INCREMENT, BINOP_END},
998 {"dec", DECREMENT, BINOP_END},
999 {"xor", XOR, BINOP_END}
1002 static const struct token tokentab2[] =
1004 {"or", OR, BINOP_END},
1005 {"<>", NOTEQUAL, BINOP_END},
1006 {"<=", LEQ, BINOP_END},
1007 {">=", GEQ, BINOP_END},
1008 {":=", ASSIGN, BINOP_END},
1009 {"::", COLONCOLON, BINOP_END} };
1011 /* Allocate uppercased var */
1012 /* make an uppercased copy of tokstart */
1013 static char * uptok (tokstart, namelen)
1018 char *uptokstart = (char *)malloc(namelen+1);
1019 for (i = 0;i <= namelen;i++)
1021 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1022 uptokstart[i] = tokstart[i]-('a'-'A');
1024 uptokstart[i] = tokstart[i];
1026 uptokstart[namelen]='\0';
1029 /* Read one token, getting characters through lexptr. */
1042 int explen, tempbufindex;
1043 static char *tempbuf;
1044 static int tempbufsize;
1048 prev_lexptr = lexptr;
1051 explen = strlen (lexptr);
1052 /* See if it is a special token of length 3. */
1054 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1055 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
1056 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
1057 || (!isalpha (tokstart[3]) && !isdigit (tokstart[3]) && tokstart[3] != '_')))
1060 yylval.opcode = tokentab3[i].opcode;
1061 return tokentab3[i].token;
1064 /* See if it is a special token of length 2. */
1066 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1067 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
1068 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
1069 || (!isalpha (tokstart[2]) && !isdigit (tokstart[2]) && tokstart[2] != '_')))
1072 yylval.opcode = tokentab2[i].opcode;
1073 return tokentab2[i].token;
1076 switch (c = *tokstart)
1088 /* We either have a character constant ('0' or '\177' for example)
1089 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1094 c = parse_escape (&lexptr);
1096 error ("Empty character constant.");
1098 yylval.typed_val_int.val = c;
1099 yylval.typed_val_int.type = builtin_type_char;
1104 namelen = skip_quoted (tokstart) - tokstart;
1107 lexptr = tokstart + namelen;
1108 if (lexptr[-1] != '\'')
1109 error ("Unmatched single quote.");
1112 uptokstart = uptok(tokstart,namelen);
1115 error ("Invalid character constant.");
1125 if (paren_depth == 0)
1132 if (comma_terminates && paren_depth == 0)
1138 /* Might be a floating point number. */
1139 if (lexptr[1] < '0' || lexptr[1] > '9')
1140 goto symbol; /* Nope, must be a symbol. */
1141 /* FALL THRU into number case. */
1154 /* It's a number. */
1155 int got_dot = 0, got_e = 0, toktype;
1156 register char *p = tokstart;
1157 int hex = input_radix > 10;
1159 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1164 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1172 /* This test includes !hex because 'e' is a valid hex digit
1173 and thus does not indicate a floating point number when
1174 the radix is hex. */
1175 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1176 got_dot = got_e = 1;
1177 /* This test does not include !hex, because a '.' always indicates
1178 a decimal floating point number regardless of the radix. */
1179 else if (!got_dot && *p == '.')
1181 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1182 && (*p == '-' || *p == '+'))
1183 /* This is the sign of the exponent, not the end of the
1186 /* We will take any letters or digits. parse_number will
1187 complain if past the radix, or if L or U are not final. */
1188 else if ((*p < '0' || *p > '9')
1189 && ((*p < 'a' || *p > 'z')
1190 && (*p < 'A' || *p > 'Z')))
1193 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1194 if (toktype == ERROR)
1196 char *err_copy = (char *) alloca (p - tokstart + 1);
1198 memcpy (err_copy, tokstart, p - tokstart);
1199 err_copy[p - tokstart] = 0;
1200 error ("Invalid number \"%s\".", err_copy);
1231 /* Build the gdb internal form of the input string in tempbuf,
1232 translating any standard C escape forms seen. Note that the
1233 buffer is null byte terminated *only* for the convenience of
1234 debugging gdb itself and printing the buffer contents when
1235 the buffer contains no embedded nulls. Gdb does not depend
1236 upon the buffer being null byte terminated, it uses the length
1237 string instead. This allows gdb to handle C strings (as well
1238 as strings in other languages) with embedded null bytes */
1240 tokptr = ++tokstart;
1244 /* Grow the static temp buffer if necessary, including allocating
1245 the first one on demand. */
1246 if (tempbufindex + 1 >= tempbufsize)
1248 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1255 /* Do nothing, loop will terminate. */
1259 c = parse_escape (&tokptr);
1264 tempbuf[tempbufindex++] = c;
1267 tempbuf[tempbufindex++] = *tokptr++;
1270 } while ((*tokptr != '"') && (*tokptr != '\0'));
1271 if (*tokptr++ != '"')
1273 error ("Unterminated string in expression.");
1275 tempbuf[tempbufindex] = '\0'; /* See note above */
1276 yylval.sval.ptr = tempbuf;
1277 yylval.sval.length = tempbufindex;
1282 if (!(c == '_' || c == '$'
1283 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1284 /* We must have come across a bad character (e.g. ';'). */
1285 error ("Invalid character '%c' in expression.", c);
1287 /* It's a name. See how long it is. */
1289 for (c = tokstart[namelen];
1290 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1291 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1293 /* Template parameter lists are part of the name.
1294 FIXME: This mishandles `print $a<4&&$a>3'. */
1298 int nesting_level = 1;
1299 while (tokstart[++i])
1301 if (tokstart[i] == '<')
1303 else if (tokstart[i] == '>')
1305 if (--nesting_level == 0)
1309 if (tokstart[i] == '>')
1315 /* do NOT uppercase internals because of registers !!! */
1316 c = tokstart[++namelen];
1319 uptokstart = uptok(tokstart,namelen);
1321 /* The token "if" terminates the expression and is NOT
1322 removed from the input stream. */
1323 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1332 /* Catch specific keywords. Should be done with a data structure. */
1336 if (STREQ (uptokstart, "OBJECT"))
1338 if (STREQ (uptokstart, "RECORD"))
1340 if (STREQ (uptokstart, "SIZEOF"))
1344 if (STREQ (uptokstart, "CLASS"))
1346 if (STREQ (uptokstart, "FALSE"))
1353 if (STREQ (uptokstart, "TRUE"))
1358 if (STREQ (uptokstart, "SELF"))
1360 /* here we search for 'this' like
1361 inserted in FPC stabs debug info */
1362 static const char this_name[] =
1363 { /* CPLUS_MARKER,*/ 't', 'h', 'i', 's', '\0' };
1365 if (lookup_symbol (this_name, expression_context_block,
1366 VAR_NAMESPACE, (int *) NULL,
1367 (struct symtab **) NULL))
1375 yylval.sval.ptr = tokstart;
1376 yylval.sval.length = namelen;
1378 if (*tokstart == '$')
1380 /* $ is the normal prefix for pascal hexadecimal values
1381 but this conflicts with the GDB use for debugger variables
1382 so in expression to enter hexadecimal values
1383 we still need to use C syntax with 0xff */
1384 write_dollar_variable (yylval.sval);
1388 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1389 functions or symtabs. If this is not so, then ...
1390 Use token-type TYPENAME for symbols that happen to be defined
1391 currently as names of types; NAME for other symbols.
1392 The caller is not constrained to care about the distinction. */
1394 char *tmp = copy_name (yylval.sval);
1396 int is_a_field_of_this = 0;
1401 if (search_field && current_type)
1402 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1406 sym = lookup_symbol (tmp, expression_context_block,
1408 &is_a_field_of_this,
1409 (struct symtab **) NULL);
1410 /* second chance uppercased (as Free Pascal does). */
1411 if (!sym && !is_a_field_of_this && !is_a_field)
1413 for (i = 0; i <= namelen; i++)
1415 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1416 tmp[i] -= ('a'-'A');
1418 if (search_field && current_type)
1419 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1423 sym = lookup_symbol (tmp, expression_context_block,
1425 &is_a_field_of_this,
1426 (struct symtab **) NULL);
1427 if (sym || is_a_field_of_this || is_a_field)
1428 for (i = 0; i <= namelen; i++)
1430 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1431 tokstart[i] -= ('a'-'A');
1434 /* Third chance Capitalized (as GPC does). */
1435 if (!sym && !is_a_field_of_this && !is_a_field)
1437 for (i = 0; i <= namelen; i++)
1441 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1442 tmp[i] -= ('a'-'A');
1445 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1446 tmp[i] -= ('A'-'a');
1448 if (search_field && current_type)
1449 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1453 sym = lookup_symbol (tmp, expression_context_block,
1455 &is_a_field_of_this,
1456 (struct symtab **) NULL);
1457 if (sym || is_a_field_of_this || is_a_field)
1458 for (i = 0; i <= namelen; i++)
1462 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1463 tokstart[i] -= ('a'-'A');
1466 if ((tokstart[i] >= 'A' && tokstart[i] <= 'Z'))
1467 tokstart[i] -= ('A'-'a');
1473 tempbuf = (char *) realloc (tempbuf, namelen + 1);
1474 strncpy (tempbuf, tokstart, namelen); tempbuf [namelen] = 0;
1475 yylval.sval.ptr = tempbuf;
1476 yylval.sval.length = namelen;
1479 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1480 no psymtabs (coff, xcoff, or some future change to blow away the
1481 psymtabs once once symbols are read). */
1482 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1483 lookup_symtab (tmp))
1485 yylval.ssym.sym = sym;
1486 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1489 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1492 /* Despite the following flaw, we need to keep this code enabled.
1493 Because we can get called from check_stub_method, if we don't
1494 handle nested types then it screws many operations in any
1495 program which uses nested types. */
1496 /* In "A::x", if x is a member function of A and there happens
1497 to be a type (nested or not, since the stabs don't make that
1498 distinction) named x, then this code incorrectly thinks we
1499 are dealing with nested types rather than a member function. */
1503 struct symbol *best_sym;
1505 /* Look ahead to detect nested types. This probably should be
1506 done in the grammar, but trying seemed to introduce a lot
1507 of shift/reduce and reduce/reduce conflicts. It's possible
1508 that it could be done, though. Or perhaps a non-grammar, but
1509 less ad hoc, approach would work well. */
1511 /* Since we do not currently have any way of distinguishing
1512 a nested type from a non-nested one (the stabs don't tell
1513 us whether a type is nested), we just ignore the
1520 /* Skip whitespace. */
1521 while (*p == ' ' || *p == '\t' || *p == '\n')
1523 if (*p == ':' && p[1] == ':')
1525 /* Skip the `::'. */
1527 /* Skip whitespace. */
1528 while (*p == ' ' || *p == '\t' || *p == '\n')
1531 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1532 || (*p >= 'a' && *p <= 'z')
1533 || (*p >= 'A' && *p <= 'Z'))
1537 struct symbol *cur_sym;
1538 /* As big as the whole rest of the expression, which is
1539 at least big enough. */
1540 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1544 memcpy (tmp1, tmp, strlen (tmp));
1545 tmp1 += strlen (tmp);
1546 memcpy (tmp1, "::", 2);
1548 memcpy (tmp1, namestart, p - namestart);
1549 tmp1[p - namestart] = '\0';
1550 cur_sym = lookup_symbol (ncopy, expression_context_block,
1551 VAR_NAMESPACE, (int *) NULL,
1552 (struct symtab **) NULL);
1555 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1573 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1575 yylval.tsym.type = SYMBOL_TYPE (sym);
1579 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1582 /* Input names that aren't symbols but ARE valid hex numbers,
1583 when the input radix permits them, can be names or numbers
1584 depending on the parse. Note we support radixes > 16 here. */
1586 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1587 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1589 YYSTYPE newlval; /* Its value is ignored. */
1590 hextype = parse_number (tokstart, namelen, 0, &newlval);
1593 yylval.ssym.sym = sym;
1594 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1600 /* Any other kind of symbol */
1601 yylval.ssym.sym = sym;
1602 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1612 lexptr = prev_lexptr;
1614 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);