1 /* YACC parser for Pascal expressions, for GDB.
2 Copyright (C) 2000, 2006
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* This file is derived from c-exp.y */
24 /* Parse a Pascal expression from text in a string,
25 and return the result as a struct expression pointer.
26 That structure contains arithmetic operations in reverse polish,
27 with constants represented by operations that are followed by special data.
28 See expression.h for the details of the format.
29 What is important here is that it can be built up sequentially
30 during the process of parsing; the lower levels of the tree always
31 come first in the result.
33 Note that malloc's and realloc's in this file are transformed to
34 xmalloc and xrealloc respectively by the same sed command in the
35 makefile that remaps any other malloc/realloc inserted by the parser
36 generator. Doing this with #defines and trying to control the interaction
37 with include files (<malloc.h> and <stdlib.h> for example) just became
38 too messy, particularly when such includes can be inserted at random
39 times by the parser generator. */
41 /* Known bugs or limitations:
42 - pascal string operations are not supported at all.
43 - there are some problems with boolean types.
44 - Pascal type hexadecimal constants are not supported
45 because they conflict with the internal variables format.
46 Probably also lots of other problems, less well defined PM */
50 #include "gdb_string.h"
52 #include "expression.h"
54 #include "parser-defs.h"
57 #include "bfd.h" /* Required by objfiles.h. */
58 #include "symfile.h" /* Required by objfiles.h. */
59 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
62 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
63 as well as gratuitiously global symbol names, so we can have multiple
64 yacc generated parsers in gdb. Note that these are only the variables
65 produced by yacc. If other parser generators (bison, byacc, etc) produce
66 additional global names that conflict at link time, then those parser
67 generators need to be fixed instead of adding those names to this list. */
69 #define yymaxdepth pascal_maxdepth
70 #define yyparse pascal_parse
71 #define yylex pascal_lex
72 #define yyerror pascal_error
73 #define yylval pascal_lval
74 #define yychar pascal_char
75 #define yydebug pascal_debug
76 #define yypact pascal_pact
77 #define yyr1 pascal_r1
78 #define yyr2 pascal_r2
79 #define yydef pascal_def
80 #define yychk pascal_chk
81 #define yypgo pascal_pgo
82 #define yyact pascal_act
83 #define yyexca pascal_exca
84 #define yyerrflag pascal_errflag
85 #define yynerrs pascal_nerrs
86 #define yyps pascal_ps
87 #define yypv pascal_pv
89 #define yy_yys pascal_yys
90 #define yystate pascal_state
91 #define yytmp pascal_tmp
93 #define yy_yyv pascal_yyv
94 #define yyval pascal_val
95 #define yylloc pascal_lloc
96 #define yyreds pascal_reds /* With YYDEBUG defined */
97 #define yytoks pascal_toks /* With YYDEBUG defined */
98 #define yyname pascal_name /* With YYDEBUG defined */
99 #define yyrule pascal_rule /* With YYDEBUG defined */
100 #define yylhs pascal_yylhs
101 #define yylen pascal_yylen
102 #define yydefred pascal_yydefred
103 #define yydgoto pascal_yydgoto
104 #define yysindex pascal_yysindex
105 #define yyrindex pascal_yyrindex
106 #define yygindex pascal_yygindex
107 #define yytable pascal_yytable
108 #define yycheck pascal_yycheck
111 #define YYDEBUG 1 /* Default to yydebug support */
114 #define YYFPRINTF parser_fprintf
118 static int yylex (void);
123 static char * uptok (char *, int);
126 /* Although the yacc "value" of an expression is not used,
127 since the result is stored in the structure being created,
128 other node types do have values. */
145 struct symtoken ssym;
148 enum exp_opcode opcode;
149 struct internalvar *ivar;
156 /* YYSTYPE gets defined by %union */
158 parse_number (char *, int, int, YYSTYPE *);
160 static struct type *current_type;
162 static void push_current_type (void);
163 static void pop_current_type (void);
164 static int search_field;
167 %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
168 %type <tval> type typebase
169 /* %type <bval> block */
171 /* Fancy type parsing. */
174 %token <typed_val_int> INT
175 %token <typed_val_float> FLOAT
177 /* Both NAME and TYPENAME tokens represent symbols in the input,
178 and both convey their data as strings.
179 But a TYPENAME is a string that happens to be defined as a typedef
180 or builtin type name (such as int or char)
181 and a NAME is any other symbol.
182 Contexts where this distinction is not important can use the
183 nonterminal "name", which matches either NAME or TYPENAME. */
186 %token <sval> FIELDNAME
187 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
188 %token <tsym> TYPENAME
190 %type <ssym> name_not_typename
192 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
193 but which would parse as a valid number in the current input radix.
194 E.g. "c" when input_radix==16. Depending on the parse, it will be
195 turned into a name or into a number. */
197 %token <ssym> NAME_OR_INT
199 %token STRUCT CLASS SIZEOF COLONCOLON
202 /* Special type cases, put in to allow the parser to distinguish different
205 %token <voidval> VARIABLE
210 %token <lval> TRUEKEYWORD FALSEKEYWORD
220 %left '<' '>' LEQ GEQ
221 %left LSH RSH DIV MOD
225 %right UNARY INCREMENT DECREMENT
226 %right ARROW '.' '[' '('
228 %token <ssym> BLOCKNAME
235 start : { current_type = NULL;
247 { write_exp_elt_opcode(OP_TYPE);
248 write_exp_elt_type($1);
249 write_exp_elt_opcode(OP_TYPE);
250 current_type = $1; } ;
252 /* Expressions, including the comma operator. */
255 { write_exp_elt_opcode (BINOP_COMMA); }
258 /* Expressions, not including the comma operator. */
259 exp : exp '^' %prec UNARY
260 { write_exp_elt_opcode (UNOP_IND);
262 current_type = TYPE_TARGET_TYPE (current_type); }
265 exp : '@' exp %prec UNARY
266 { write_exp_elt_opcode (UNOP_ADDR);
268 current_type = TYPE_POINTER_TYPE (current_type); }
271 exp : '-' exp %prec UNARY
272 { write_exp_elt_opcode (UNOP_NEG); }
275 exp : NOT exp %prec UNARY
276 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
279 exp : INCREMENT '(' exp ')' %prec UNARY
280 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
283 exp : DECREMENT '(' exp ')' %prec UNARY
284 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
287 exp : exp '.' { search_field = 1; }
290 { write_exp_elt_opcode (STRUCTOP_STRUCT);
291 write_exp_string ($4);
292 write_exp_elt_opcode (STRUCTOP_STRUCT);
295 { while (TYPE_CODE (current_type) == TYPE_CODE_PTR)
296 current_type = TYPE_TARGET_TYPE (current_type);
297 current_type = lookup_struct_elt_type (
298 current_type, $4.ptr, 0); };
301 /* We need to save the current_type value */
304 arrayfieldindex = is_pascal_string_type (
305 current_type, NULL, NULL,
306 NULL, NULL, &arrayname);
309 struct stoken stringsval;
310 stringsval.ptr = alloca (strlen (arrayname) + 1);
311 stringsval.length = strlen (arrayname);
312 strcpy (stringsval.ptr, arrayname);
313 current_type = TYPE_FIELD_TYPE (current_type,
314 arrayfieldindex - 1);
315 write_exp_elt_opcode (STRUCTOP_STRUCT);
316 write_exp_string (stringsval);
317 write_exp_elt_opcode (STRUCTOP_STRUCT);
319 push_current_type (); }
321 { pop_current_type ();
322 write_exp_elt_opcode (BINOP_SUBSCRIPT);
324 current_type = TYPE_TARGET_TYPE (current_type); }
328 /* This is to save the value of arglist_len
329 being accumulated by an outer function call. */
330 { push_current_type ();
332 arglist ')' %prec ARROW
333 { write_exp_elt_opcode (OP_FUNCALL);
334 write_exp_elt_longcst ((LONGEST) end_arglist ());
335 write_exp_elt_opcode (OP_FUNCALL);
336 pop_current_type (); }
342 | arglist ',' exp %prec ABOVE_COMMA
346 exp : type '(' exp ')' %prec UNARY
349 /* Allow automatic dereference of classes. */
350 if ((TYPE_CODE (current_type) == TYPE_CODE_PTR)
351 && (TYPE_CODE (TYPE_TARGET_TYPE (current_type)) == TYPE_CODE_CLASS)
352 && (TYPE_CODE ($1) == TYPE_CODE_CLASS))
353 write_exp_elt_opcode (UNOP_IND);
355 write_exp_elt_opcode (UNOP_CAST);
356 write_exp_elt_type ($1);
357 write_exp_elt_opcode (UNOP_CAST);
365 /* Binary operators in order of decreasing precedence. */
368 { write_exp_elt_opcode (BINOP_MUL); }
372 { write_exp_elt_opcode (BINOP_DIV); }
376 { write_exp_elt_opcode (BINOP_INTDIV); }
380 { write_exp_elt_opcode (BINOP_REM); }
384 { write_exp_elt_opcode (BINOP_ADD); }
388 { write_exp_elt_opcode (BINOP_SUB); }
392 { write_exp_elt_opcode (BINOP_LSH); }
396 { write_exp_elt_opcode (BINOP_RSH); }
400 { write_exp_elt_opcode (BINOP_EQUAL); }
403 exp : exp NOTEQUAL exp
404 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
408 { write_exp_elt_opcode (BINOP_LEQ); }
412 { write_exp_elt_opcode (BINOP_GEQ); }
416 { write_exp_elt_opcode (BINOP_LESS); }
420 { write_exp_elt_opcode (BINOP_GTR); }
424 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
428 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
432 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
436 { write_exp_elt_opcode (BINOP_ASSIGN); }
440 { write_exp_elt_opcode (OP_BOOL);
441 write_exp_elt_longcst ((LONGEST) $1);
442 write_exp_elt_opcode (OP_BOOL); }
446 { write_exp_elt_opcode (OP_BOOL);
447 write_exp_elt_longcst ((LONGEST) $1);
448 write_exp_elt_opcode (OP_BOOL); }
452 { write_exp_elt_opcode (OP_LONG);
453 write_exp_elt_type ($1.type);
454 write_exp_elt_longcst ((LONGEST)($1.val));
455 write_exp_elt_opcode (OP_LONG); }
460 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
461 write_exp_elt_opcode (OP_LONG);
462 write_exp_elt_type (val.typed_val_int.type);
463 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
464 write_exp_elt_opcode (OP_LONG);
470 { write_exp_elt_opcode (OP_DOUBLE);
471 write_exp_elt_type ($1.type);
472 write_exp_elt_dblcst ($1.dval);
473 write_exp_elt_opcode (OP_DOUBLE); }
480 /* Already written by write_dollar_variable. */
483 exp : SIZEOF '(' type ')' %prec UNARY
484 { write_exp_elt_opcode (OP_LONG);
485 write_exp_elt_type (builtin_type_int);
487 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
488 write_exp_elt_opcode (OP_LONG); }
492 { /* C strings are converted into array constants with
493 an explicit null byte added at the end. Thus
494 the array upper bound is the string length.
495 There is no such thing in C as a completely empty
497 char *sp = $1.ptr; int count = $1.length;
500 write_exp_elt_opcode (OP_LONG);
501 write_exp_elt_type (builtin_type_char);
502 write_exp_elt_longcst ((LONGEST)(*sp++));
503 write_exp_elt_opcode (OP_LONG);
505 write_exp_elt_opcode (OP_LONG);
506 write_exp_elt_type (builtin_type_char);
507 write_exp_elt_longcst ((LONGEST)'\0');
508 write_exp_elt_opcode (OP_LONG);
509 write_exp_elt_opcode (OP_ARRAY);
510 write_exp_elt_longcst ((LONGEST) 0);
511 write_exp_elt_longcst ((LONGEST) ($1.length));
512 write_exp_elt_opcode (OP_ARRAY); }
518 struct value * this_val;
519 struct type * this_type;
520 write_exp_elt_opcode (OP_THIS);
521 write_exp_elt_opcode (OP_THIS);
522 /* we need type of this */
523 this_val = value_of_this (0);
525 this_type = value_type (this_val);
530 if (TYPE_CODE (this_type) == TYPE_CODE_PTR)
532 this_type = TYPE_TARGET_TYPE (this_type);
533 write_exp_elt_opcode (UNOP_IND);
537 current_type = this_type;
541 /* end of object pascal. */
546 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
550 lookup_symtab (copy_name ($1.stoken));
552 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK);
554 error ("No file or function \"%s\".",
555 copy_name ($1.stoken));
560 block : block COLONCOLON name
562 = lookup_symbol (copy_name ($3), $1,
563 VAR_DOMAIN, (int *) NULL,
564 (struct symtab **) NULL);
565 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
566 error ("No function \"%s\" in specified context.",
568 $$ = SYMBOL_BLOCK_VALUE (tem); }
571 variable: block COLONCOLON name
572 { struct symbol *sym;
573 sym = lookup_symbol (copy_name ($3), $1,
574 VAR_DOMAIN, (int *) NULL,
575 (struct symtab **) NULL);
577 error ("No symbol \"%s\" in specified context.",
580 write_exp_elt_opcode (OP_VAR_VALUE);
581 /* block_found is set by lookup_symbol. */
582 write_exp_elt_block (block_found);
583 write_exp_elt_sym (sym);
584 write_exp_elt_opcode (OP_VAR_VALUE); }
587 qualified_name: typebase COLONCOLON name
589 struct type *type = $1;
590 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
591 && TYPE_CODE (type) != TYPE_CODE_UNION)
592 error ("`%s' is not defined as an aggregate type.",
595 write_exp_elt_opcode (OP_SCOPE);
596 write_exp_elt_type (type);
597 write_exp_string ($3);
598 write_exp_elt_opcode (OP_SCOPE);
602 variable: qualified_name
605 char *name = copy_name ($2);
607 struct minimal_symbol *msymbol;
610 lookup_symbol (name, (const struct block *) NULL,
611 VAR_DOMAIN, (int *) NULL,
612 (struct symtab **) NULL);
615 write_exp_elt_opcode (OP_VAR_VALUE);
616 write_exp_elt_block (NULL);
617 write_exp_elt_sym (sym);
618 write_exp_elt_opcode (OP_VAR_VALUE);
622 msymbol = lookup_minimal_symbol (name, NULL, NULL);
625 write_exp_msymbol (msymbol,
626 lookup_function_type (builtin_type_int),
630 if (!have_full_symbols () && !have_partial_symbols ())
631 error ("No symbol table is loaded. Use the \"file\" command.");
633 error ("No symbol \"%s\" in current context.", name);
637 variable: name_not_typename
638 { struct symbol *sym = $1.sym;
642 if (symbol_read_needs_frame (sym))
644 if (innermost_block == 0 ||
645 contained_in (block_found,
647 innermost_block = block_found;
650 write_exp_elt_opcode (OP_VAR_VALUE);
651 /* We want to use the selected frame, not
652 another more inner frame which happens to
653 be in the same block. */
654 write_exp_elt_block (NULL);
655 write_exp_elt_sym (sym);
656 write_exp_elt_opcode (OP_VAR_VALUE);
657 current_type = sym->type; }
658 else if ($1.is_a_field_of_this)
660 struct value * this_val;
661 struct type * this_type;
662 /* Object pascal: it hangs off of `this'. Must
663 not inadvertently convert from a method call
665 if (innermost_block == 0 ||
666 contained_in (block_found, innermost_block))
667 innermost_block = block_found;
668 write_exp_elt_opcode (OP_THIS);
669 write_exp_elt_opcode (OP_THIS);
670 write_exp_elt_opcode (STRUCTOP_PTR);
671 write_exp_string ($1.stoken);
672 write_exp_elt_opcode (STRUCTOP_PTR);
673 /* we need type of this */
674 this_val = value_of_this (0);
676 this_type = value_type (this_val);
680 current_type = lookup_struct_elt_type (
682 copy_name ($1.stoken), 0);
688 struct minimal_symbol *msymbol;
689 char *arg = copy_name ($1.stoken);
692 lookup_minimal_symbol (arg, NULL, NULL);
695 write_exp_msymbol (msymbol,
696 lookup_function_type (builtin_type_int),
699 else if (!have_full_symbols () && !have_partial_symbols ())
700 error ("No symbol table is loaded. Use the \"file\" command.");
702 error ("No symbol \"%s\" in current context.",
703 copy_name ($1.stoken));
712 /* We used to try to recognize more pointer to member types here, but
713 that didn't work (shift/reduce conflicts meant that these rules never
714 got executed). The problem is that
715 int (foo::bar::baz::bizzle)
716 is a function type but
717 int (foo::bar::baz::bizzle::*)
718 is a pointer to member type. Stroustrup loses again! */
723 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
725 { $$ = lookup_pointer_type ($2); }
729 { $$ = lookup_struct (copy_name ($2),
730 expression_context_block); }
732 { $$ = lookup_struct (copy_name ($2),
733 expression_context_block); }
734 /* "const" and "volatile" are curently ignored. A type qualifier
735 after the type is handled in the ptype rule. I think these could
739 name : NAME { $$ = $1.stoken; }
740 | BLOCKNAME { $$ = $1.stoken; }
741 | TYPENAME { $$ = $1.stoken; }
742 | NAME_OR_INT { $$ = $1.stoken; }
745 name_not_typename : NAME
747 /* These would be useful if name_not_typename was useful, but it is just
748 a fake for "variable", so these cause reduce/reduce conflicts because
749 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
750 =exp) or just an exp. If name_not_typename was ever used in an lvalue
751 context where only a name could occur, this might be useful.
758 /* Take care of parsing a number (anything that starts with a digit).
759 Set yylval and return the token type; update lexptr.
760 LEN is the number of characters in it. */
762 /*** Needs some error checking for the float case ***/
765 parse_number (p, len, parsed_float, putithere)
771 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
772 here, and we do kind of silly things like cast to unsigned. */
779 int base = input_radix;
782 /* Number of "L" suffixes encountered. */
785 /* We have found a "L" or "U" suffix. */
786 int found_suffix = 0;
789 struct type *signed_type;
790 struct type *unsigned_type;
794 /* It's a float since it contains a point or an exponent. */
796 int num = 0; /* number of tokens scanned by scanf */
797 char saved_char = p[len];
799 p[len] = 0; /* null-terminate the token */
800 num = sscanf (p, DOUBLEST_SCAN_FORMAT "%c",
801 &putithere->typed_val_float.dval, &c);
802 p[len] = saved_char; /* restore the input stream */
803 if (num != 1) /* check scanf found ONLY a float ... */
805 /* See if it has `f' or `l' suffix (float or long double). */
807 c = tolower (p[len - 1]);
810 putithere->typed_val_float.type = builtin_type_float;
812 putithere->typed_val_float.type = builtin_type_long_double;
813 else if (isdigit (c) || c == '.')
814 putithere->typed_val_float.type = builtin_type_double;
821 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
855 if (c >= 'A' && c <= 'Z')
857 if (c != 'l' && c != 'u')
859 if (c >= '0' && c <= '9')
867 if (base > 10 && c >= 'a' && c <= 'f')
871 n += i = c - 'a' + 10;
884 return ERROR; /* Char not a digit */
887 return ERROR; /* Invalid digit in this base */
889 /* Portably test for overflow (only works for nonzero values, so make
890 a second check for zero). FIXME: Can't we just make n and prevn
891 unsigned and avoid this? */
892 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
893 unsigned_p = 1; /* Try something unsigned */
895 /* Portably test for unsigned overflow.
896 FIXME: This check is wrong; for example it doesn't find overflow
897 on 0x123456789 when LONGEST is 32 bits. */
898 if (c != 'l' && c != 'u' && n != 0)
900 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
901 error ("Numeric constant too large.");
906 /* An integer constant is an int, a long, or a long long. An L
907 suffix forces it to be long; an LL suffix forces it to be long
908 long. If not forced to a larger size, it gets the first type of
909 the above that it fits in. To figure out whether it fits, we
910 shift it right and see whether anything remains. Note that we
911 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
912 operation, because many compilers will warn about such a shift
913 (which always produces a zero result). Sometimes TARGET_INT_BIT
914 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
915 the case where it is we just always shift the value more than
916 once, with fewer bits each time. */
918 un = (ULONGEST)n >> 2;
920 && (un >> (TARGET_INT_BIT - 2)) == 0)
922 high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
924 /* A large decimal (not hex or octal) constant (between INT_MAX
925 and UINT_MAX) is a long or unsigned long, according to ANSI,
926 never an unsigned int, but this code treats it as unsigned
927 int. This probably should be fixed. GCC gives a warning on
930 unsigned_type = builtin_type_unsigned_int;
931 signed_type = builtin_type_int;
934 && (un >> (TARGET_LONG_BIT - 2)) == 0)
936 high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
937 unsigned_type = builtin_type_unsigned_long;
938 signed_type = builtin_type_long;
943 if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
944 /* A long long does not fit in a LONGEST. */
945 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
947 shift = (TARGET_LONG_LONG_BIT - 1);
948 high_bit = (ULONGEST) 1 << shift;
949 unsigned_type = builtin_type_unsigned_long_long;
950 signed_type = builtin_type_long_long;
953 putithere->typed_val_int.val = n;
955 /* If the high bit of the worked out type is set then this number
956 has to be unsigned. */
958 if (unsigned_p || (n & high_bit))
960 putithere->typed_val_int.type = unsigned_type;
964 putithere->typed_val_int.type = signed_type;
974 struct type_push *next;
977 static struct type_push *tp_top = NULL;
980 push_current_type (void)
982 struct type_push *tpnew;
983 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
984 tpnew->next = tp_top;
985 tpnew->stored = current_type;
991 pop_current_type (void)
993 struct type_push *tp = tp_top;
996 current_type = tp->stored;
1006 enum exp_opcode opcode;
1009 static const struct token tokentab3[] =
1011 {"shr", RSH, BINOP_END},
1012 {"shl", LSH, BINOP_END},
1013 {"and", ANDAND, BINOP_END},
1014 {"div", DIV, BINOP_END},
1015 {"not", NOT, BINOP_END},
1016 {"mod", MOD, BINOP_END},
1017 {"inc", INCREMENT, BINOP_END},
1018 {"dec", DECREMENT, BINOP_END},
1019 {"xor", XOR, BINOP_END}
1022 static const struct token tokentab2[] =
1024 {"or", OR, BINOP_END},
1025 {"<>", NOTEQUAL, BINOP_END},
1026 {"<=", LEQ, BINOP_END},
1027 {">=", GEQ, BINOP_END},
1028 {":=", ASSIGN, BINOP_END},
1029 {"::", COLONCOLON, BINOP_END} };
1031 /* Allocate uppercased var */
1032 /* make an uppercased copy of tokstart */
1033 static char * uptok (tokstart, namelen)
1038 char *uptokstart = (char *)malloc(namelen+1);
1039 for (i = 0;i <= namelen;i++)
1041 if ((tokstart[i]>='a' && tokstart[i]<='z'))
1042 uptokstart[i] = tokstart[i]-('a'-'A');
1044 uptokstart[i] = tokstart[i];
1046 uptokstart[namelen]='\0';
1049 /* Read one token, getting characters through lexptr. */
1062 int explen, tempbufindex;
1063 static char *tempbuf;
1064 static int tempbufsize;
1068 prev_lexptr = lexptr;
1071 explen = strlen (lexptr);
1072 /* See if it is a special token of length 3. */
1074 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1075 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
1076 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
1077 || (!isalpha (tokstart[3]) && !isdigit (tokstart[3]) && tokstart[3] != '_')))
1080 yylval.opcode = tokentab3[i].opcode;
1081 return tokentab3[i].token;
1084 /* See if it is a special token of length 2. */
1086 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1087 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
1088 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
1089 || (!isalpha (tokstart[2]) && !isdigit (tokstart[2]) && tokstart[2] != '_')))
1092 yylval.opcode = tokentab2[i].opcode;
1093 return tokentab2[i].token;
1096 switch (c = *tokstart)
1108 /* We either have a character constant ('0' or '\177' for example)
1109 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1114 c = parse_escape (&lexptr);
1116 error ("Empty character constant.");
1118 yylval.typed_val_int.val = c;
1119 yylval.typed_val_int.type = builtin_type_char;
1124 namelen = skip_quoted (tokstart) - tokstart;
1127 lexptr = tokstart + namelen;
1128 if (lexptr[-1] != '\'')
1129 error ("Unmatched single quote.");
1132 uptokstart = uptok(tokstart,namelen);
1135 error ("Invalid character constant.");
1145 if (paren_depth == 0)
1152 if (comma_terminates && paren_depth == 0)
1158 /* Might be a floating point number. */
1159 if (lexptr[1] < '0' || lexptr[1] > '9')
1160 goto symbol; /* Nope, must be a symbol. */
1161 /* FALL THRU into number case. */
1174 /* It's a number. */
1175 int got_dot = 0, got_e = 0, toktype;
1177 int hex = input_radix > 10;
1179 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1184 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1192 /* This test includes !hex because 'e' is a valid hex digit
1193 and thus does not indicate a floating point number when
1194 the radix is hex. */
1195 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1196 got_dot = got_e = 1;
1197 /* This test does not include !hex, because a '.' always indicates
1198 a decimal floating point number regardless of the radix. */
1199 else if (!got_dot && *p == '.')
1201 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1202 && (*p == '-' || *p == '+'))
1203 /* This is the sign of the exponent, not the end of the
1206 /* We will take any letters or digits. parse_number will
1207 complain if past the radix, or if L or U are not final. */
1208 else if ((*p < '0' || *p > '9')
1209 && ((*p < 'a' || *p > 'z')
1210 && (*p < 'A' || *p > 'Z')))
1213 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1214 if (toktype == ERROR)
1216 char *err_copy = (char *) alloca (p - tokstart + 1);
1218 memcpy (err_copy, tokstart, p - tokstart);
1219 err_copy[p - tokstart] = 0;
1220 error ("Invalid number \"%s\".", err_copy);
1251 /* Build the gdb internal form of the input string in tempbuf,
1252 translating any standard C escape forms seen. Note that the
1253 buffer is null byte terminated *only* for the convenience of
1254 debugging gdb itself and printing the buffer contents when
1255 the buffer contains no embedded nulls. Gdb does not depend
1256 upon the buffer being null byte terminated, it uses the length
1257 string instead. This allows gdb to handle C strings (as well
1258 as strings in other languages) with embedded null bytes */
1260 tokptr = ++tokstart;
1264 /* Grow the static temp buffer if necessary, including allocating
1265 the first one on demand. */
1266 if (tempbufindex + 1 >= tempbufsize)
1268 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1275 /* Do nothing, loop will terminate. */
1279 c = parse_escape (&tokptr);
1284 tempbuf[tempbufindex++] = c;
1287 tempbuf[tempbufindex++] = *tokptr++;
1290 } while ((*tokptr != '"') && (*tokptr != '\0'));
1291 if (*tokptr++ != '"')
1293 error ("Unterminated string in expression.");
1295 tempbuf[tempbufindex] = '\0'; /* See note above */
1296 yylval.sval.ptr = tempbuf;
1297 yylval.sval.length = tempbufindex;
1302 if (!(c == '_' || c == '$'
1303 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1304 /* We must have come across a bad character (e.g. ';'). */
1305 error ("Invalid character '%c' in expression.", c);
1307 /* It's a name. See how long it is. */
1309 for (c = tokstart[namelen];
1310 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1311 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1313 /* Template parameter lists are part of the name.
1314 FIXME: This mishandles `print $a<4&&$a>3'. */
1318 int nesting_level = 1;
1319 while (tokstart[++i])
1321 if (tokstart[i] == '<')
1323 else if (tokstart[i] == '>')
1325 if (--nesting_level == 0)
1329 if (tokstart[i] == '>')
1335 /* do NOT uppercase internals because of registers !!! */
1336 c = tokstart[++namelen];
1339 uptokstart = uptok(tokstart,namelen);
1341 /* The token "if" terminates the expression and is NOT
1342 removed from the input stream. */
1343 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1352 /* Catch specific keywords. Should be done with a data structure. */
1356 if (DEPRECATED_STREQ (uptokstart, "OBJECT"))
1358 if (DEPRECATED_STREQ (uptokstart, "RECORD"))
1360 if (DEPRECATED_STREQ (uptokstart, "SIZEOF"))
1364 if (DEPRECATED_STREQ (uptokstart, "CLASS"))
1366 if (DEPRECATED_STREQ (uptokstart, "FALSE"))
1369 return FALSEKEYWORD;
1373 if (DEPRECATED_STREQ (uptokstart, "TRUE"))
1378 if (DEPRECATED_STREQ (uptokstart, "SELF"))
1380 /* here we search for 'this' like
1381 inserted in FPC stabs debug info */
1382 static const char this_name[] = "this";
1384 if (lookup_symbol (this_name, expression_context_block,
1385 VAR_DOMAIN, (int *) NULL,
1386 (struct symtab **) NULL))
1394 yylval.sval.ptr = tokstart;
1395 yylval.sval.length = namelen;
1397 if (*tokstart == '$')
1399 /* $ is the normal prefix for pascal hexadecimal values
1400 but this conflicts with the GDB use for debugger variables
1401 so in expression to enter hexadecimal values
1402 we still need to use C syntax with 0xff */
1403 write_dollar_variable (yylval.sval);
1407 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1408 functions or symtabs. If this is not so, then ...
1409 Use token-type TYPENAME for symbols that happen to be defined
1410 currently as names of types; NAME for other symbols.
1411 The caller is not constrained to care about the distinction. */
1413 char *tmp = copy_name (yylval.sval);
1415 int is_a_field_of_this = 0;
1420 if (search_field && current_type)
1421 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1425 sym = lookup_symbol (tmp, expression_context_block,
1427 &is_a_field_of_this,
1428 (struct symtab **) NULL);
1429 /* second chance uppercased (as Free Pascal does). */
1430 if (!sym && !is_a_field_of_this && !is_a_field)
1432 for (i = 0; i <= namelen; i++)
1434 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1435 tmp[i] -= ('a'-'A');
1437 if (search_field && current_type)
1438 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1442 sym = lookup_symbol (tmp, expression_context_block,
1444 &is_a_field_of_this,
1445 (struct symtab **) NULL);
1446 if (sym || is_a_field_of_this || is_a_field)
1447 for (i = 0; i <= namelen; i++)
1449 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1450 tokstart[i] -= ('a'-'A');
1453 /* Third chance Capitalized (as GPC does). */
1454 if (!sym && !is_a_field_of_this && !is_a_field)
1456 for (i = 0; i <= namelen; i++)
1460 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1461 tmp[i] -= ('a'-'A');
1464 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1465 tmp[i] -= ('A'-'a');
1467 if (search_field && current_type)
1468 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
1472 sym = lookup_symbol (tmp, expression_context_block,
1474 &is_a_field_of_this,
1475 (struct symtab **) NULL);
1476 if (sym || is_a_field_of_this || is_a_field)
1477 for (i = 0; i <= namelen; i++)
1481 if ((tokstart[i] >= 'a' && tokstart[i] <= 'z'))
1482 tokstart[i] -= ('a'-'A');
1485 if ((tokstart[i] >= 'A' && tokstart[i] <= 'Z'))
1486 tokstart[i] -= ('A'-'a');
1492 tempbuf = (char *) realloc (tempbuf, namelen + 1);
1493 strncpy (tempbuf, tokstart, namelen); tempbuf [namelen] = 0;
1494 yylval.sval.ptr = tempbuf;
1495 yylval.sval.length = namelen;
1498 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1499 no psymtabs (coff, xcoff, or some future change to blow away the
1500 psymtabs once once symbols are read). */
1501 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1502 lookup_symtab (tmp))
1504 yylval.ssym.sym = sym;
1505 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1508 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1511 /* Despite the following flaw, we need to keep this code enabled.
1512 Because we can get called from check_stub_method, if we don't
1513 handle nested types then it screws many operations in any
1514 program which uses nested types. */
1515 /* In "A::x", if x is a member function of A and there happens
1516 to be a type (nested or not, since the stabs don't make that
1517 distinction) named x, then this code incorrectly thinks we
1518 are dealing with nested types rather than a member function. */
1522 struct symbol *best_sym;
1524 /* Look ahead to detect nested types. This probably should be
1525 done in the grammar, but trying seemed to introduce a lot
1526 of shift/reduce and reduce/reduce conflicts. It's possible
1527 that it could be done, though. Or perhaps a non-grammar, but
1528 less ad hoc, approach would work well. */
1530 /* Since we do not currently have any way of distinguishing
1531 a nested type from a non-nested one (the stabs don't tell
1532 us whether a type is nested), we just ignore the
1539 /* Skip whitespace. */
1540 while (*p == ' ' || *p == '\t' || *p == '\n')
1542 if (*p == ':' && p[1] == ':')
1544 /* Skip the `::'. */
1546 /* Skip whitespace. */
1547 while (*p == ' ' || *p == '\t' || *p == '\n')
1550 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1551 || (*p >= 'a' && *p <= 'z')
1552 || (*p >= 'A' && *p <= 'Z'))
1556 struct symbol *cur_sym;
1557 /* As big as the whole rest of the expression, which is
1558 at least big enough. */
1559 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1563 memcpy (tmp1, tmp, strlen (tmp));
1564 tmp1 += strlen (tmp);
1565 memcpy (tmp1, "::", 2);
1567 memcpy (tmp1, namestart, p - namestart);
1568 tmp1[p - namestart] = '\0';
1569 cur_sym = lookup_symbol (ncopy, expression_context_block,
1570 VAR_DOMAIN, (int *) NULL,
1571 (struct symtab **) NULL);
1574 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1592 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1594 yylval.tsym.type = SYMBOL_TYPE (sym);
1599 = language_lookup_primitive_type_by_name (current_language,
1600 current_gdbarch, tmp);
1601 if (yylval.tsym.type != NULL)
1604 /* Input names that aren't symbols but ARE valid hex numbers,
1605 when the input radix permits them, can be names or numbers
1606 depending on the parse. Note we support radixes > 16 here. */
1608 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1609 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1611 YYSTYPE newlval; /* Its value is ignored. */
1612 hextype = parse_number (tokstart, namelen, 0, &newlval);
1615 yylval.ssym.sym = sym;
1616 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1622 /* Any other kind of symbol */
1623 yylval.ssym.sym = sym;
1624 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1634 lexptr = prev_lexptr;
1636 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);