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 /* FIXME: there are still 21 shift/reduce conflicts
41 Other 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 */
61 /* MSVC uses strnicmp instead of strncasecmp */
63 #define strncasecmp strnicmp
66 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
67 as well as gratuitiously global symbol names, so we can have multiple
68 yacc generated parsers in gdb. Note that these are only the variables
69 produced by yacc. If other parser generators (bison, byacc, etc) produce
70 additional global names that conflict at link time, then those parser
71 generators need to be fixed instead of adding those names to this list. */
73 #define yymaxdepth pascal_maxdepth
74 #define yyparse pascal_parse
75 #define yylex pascal_lex
76 #define yyerror pascal_error
77 #define yylval pascal_lval
78 #define yychar pascal_char
79 #define yydebug pascal_debug
80 #define yypact pascal_pact
81 #define yyr1 pascal_r1
82 #define yyr2 pascal_r2
83 #define yydef pascal_def
84 #define yychk pascal_chk
85 #define yypgo pascal_pgo
86 #define yyact pascal_act
87 #define yyexca pascal_exca
88 #define yyerrflag pascal_errflag
89 #define yynerrs pascal_nerrs
90 #define yyps pascal_ps
91 #define yypv pascal_pv
93 #define yy_yys pascal_yys
94 #define yystate pascal_state
95 #define yytmp pascal_tmp
97 #define yy_yyv pascal_yyv
98 #define yyval pascal_val
99 #define yylloc pascal_lloc
100 #define yyreds pascal_reds /* With YYDEBUG defined */
101 #define yytoks pascal_toks /* With YYDEBUG defined */
102 #define yylhs pascal_yylhs
103 #define yylen pascal_yylen
104 #define yydefred pascal_yydefred
105 #define yydgoto pascal_yydgoto
106 #define yysindex pascal_yysindex
107 #define yyrindex pascal_yyrindex
108 #define yygindex pascal_yygindex
109 #define yytable pascal_yytable
110 #define yycheck pascal_yycheck
113 #define YYDEBUG 0 /* Default to no yydebug support */
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 *);
161 %type
<voidval
> exp exp1 type_exp start variable qualified_name
162 %type
<tval
> type typebase
163 /* %type <bval> block */
165 /* Fancy type parsing. */
168 %token
<typed_val_int
> INT
169 %token
<typed_val_float
> FLOAT
171 /* Both NAME and TYPENAME tokens represent symbols in the input,
172 and both convey their data as strings.
173 But a TYPENAME is a string that happens to be defined as a typedef
174 or builtin type name (such as int or char)
175 and a NAME is any other symbol.
176 Contexts where this distinction is not important can use the
177 nonterminal "name", which matches either NAME or TYPENAME. */
180 %token
<ssym
> NAME
/* BLOCKNAME defined below to give it higher precedence. */
181 %token
<tsym
> TYPENAME
183 %type
<ssym
> name_not_typename
185 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
186 but which would parse as a valid number in the current input radix.
187 E.g. "c" when input_radix==16. Depending on the parse, it will be
188 turned into a name or into a number. */
190 %token
<ssym
> NAME_OR_INT
192 %token STRUCT CLASS SIZEOF COLONCOLON
195 /* Special type cases, put in to allow the parser to distinguish different
198 %token
<voidval
> VARIABLE
203 %token
<lval
> TRUE FALSE
213 %left
'<' '>' LEQ GEQ
214 %left LSH RSH DIV MOD
218 %right UNARY INCREMENT DECREMENT
219 %right ARROW
'.' '[' '('
220 %token
<ssym
> BLOCKNAME
232 { write_exp_elt_opcode
(OP_TYPE
);
233 write_exp_elt_type
($1);
234 write_exp_elt_opcode
(OP_TYPE
);}
237 /* Expressions, including the comma operator. */
240 { write_exp_elt_opcode
(BINOP_COMMA
); }
243 /* Expressions, not including the comma operator. */
244 exp
: exp
'^' %prec UNARY
245 { write_exp_elt_opcode
(UNOP_IND
); }
247 exp
: '@' exp %prec UNARY
248 { write_exp_elt_opcode
(UNOP_ADDR
); }
250 exp
: '-' exp %prec UNARY
251 { write_exp_elt_opcode
(UNOP_NEG
); }
254 exp
: NOT exp %prec UNARY
255 { write_exp_elt_opcode
(UNOP_LOGICAL_NOT
); }
258 exp
: INCREMENT
'(' exp
')' %prec UNARY
259 { write_exp_elt_opcode
(UNOP_PREINCREMENT
); }
262 exp
: DECREMENT
'(' exp
')' %prec UNARY
263 { write_exp_elt_opcode
(UNOP_PREDECREMENT
); }
267 { write_exp_elt_opcode
(STRUCTOP_STRUCT
);
268 write_exp_string
($3);
269 write_exp_elt_opcode
(STRUCTOP_STRUCT
); }
272 exp
: exp
'[' exp1
']'
273 { write_exp_elt_opcode
(BINOP_SUBSCRIPT
); }
277 /* This is to save the value of arglist_len
278 being accumulated by an outer function call. */
279 { start_arglist
(); }
280 arglist
')' %prec ARROW
281 { write_exp_elt_opcode
(OP_FUNCALL
);
282 write_exp_elt_longcst
((LONGEST
) end_arglist
());
283 write_exp_elt_opcode
(OP_FUNCALL
); }
289 | arglist
',' exp %prec ABOVE_COMMA
293 exp
: type
'(' exp
')' %prec UNARY
294 { write_exp_elt_opcode
(UNOP_CAST
);
295 write_exp_elt_type
($1);
296 write_exp_elt_opcode
(UNOP_CAST
); }
303 /* Binary operators in order of decreasing precedence. */
306 { write_exp_elt_opcode
(BINOP_MUL
); }
310 { write_exp_elt_opcode
(BINOP_DIV
); }
314 { write_exp_elt_opcode
(BINOP_INTDIV
); }
318 { write_exp_elt_opcode
(BINOP_REM
); }
322 { write_exp_elt_opcode
(BINOP_ADD
); }
326 { write_exp_elt_opcode
(BINOP_SUB
); }
330 { write_exp_elt_opcode
(BINOP_LSH
); }
334 { write_exp_elt_opcode
(BINOP_RSH
); }
338 { write_exp_elt_opcode
(BINOP_EQUAL
); }
341 exp
: exp NOTEQUAL exp
342 { write_exp_elt_opcode
(BINOP_NOTEQUAL
); }
346 { write_exp_elt_opcode
(BINOP_LEQ
); }
350 { write_exp_elt_opcode
(BINOP_GEQ
); }
354 { write_exp_elt_opcode
(BINOP_LESS
); }
358 { write_exp_elt_opcode
(BINOP_GTR
); }
362 { write_exp_elt_opcode
(BINOP_BITWISE_AND
); }
366 { write_exp_elt_opcode
(BINOP_BITWISE_XOR
); }
370 { write_exp_elt_opcode
(BINOP_BITWISE_IOR
); }
374 { write_exp_elt_opcode
(BINOP_ASSIGN
); }
378 { write_exp_elt_opcode
(OP_BOOL
);
379 write_exp_elt_longcst
((LONGEST
) $1);
380 write_exp_elt_opcode
(OP_BOOL
); }
384 { write_exp_elt_opcode
(OP_BOOL
);
385 write_exp_elt_longcst
((LONGEST
) $1);
386 write_exp_elt_opcode
(OP_BOOL
); }
390 { write_exp_elt_opcode
(OP_LONG
);
391 write_exp_elt_type
($1.type
);
392 write_exp_elt_longcst
((LONGEST
)($1.val
));
393 write_exp_elt_opcode
(OP_LONG
); }
398 parse_number
($1.stoken.ptr
, $1.stoken.length
, 0, &val
);
399 write_exp_elt_opcode
(OP_LONG
);
400 write_exp_elt_type
(val.typed_val_int.type
);
401 write_exp_elt_longcst
((LONGEST
)val.typed_val_int.val
);
402 write_exp_elt_opcode
(OP_LONG
);
408 { write_exp_elt_opcode
(OP_DOUBLE
);
409 write_exp_elt_type
($1.type
);
410 write_exp_elt_dblcst
($1.dval
);
411 write_exp_elt_opcode
(OP_DOUBLE
); }
418 /* Already written by write_dollar_variable. */
421 exp
: SIZEOF
'(' type
')' %prec UNARY
422 { write_exp_elt_opcode
(OP_LONG
);
423 write_exp_elt_type
(builtin_type_int
);
425 write_exp_elt_longcst
((LONGEST
) TYPE_LENGTH
($3));
426 write_exp_elt_opcode
(OP_LONG
); }
430 { /* C strings are converted into array constants with
431 an explicit null byte added at the end. Thus
432 the array upper bound is the string length.
433 There is no such thing in C as a completely empty
435 char *sp
= $1.ptr
; int count
= $1.length
;
438 write_exp_elt_opcode
(OP_LONG
);
439 write_exp_elt_type
(builtin_type_char
);
440 write_exp_elt_longcst
((LONGEST
)(*sp
++));
441 write_exp_elt_opcode
(OP_LONG
);
443 write_exp_elt_opcode
(OP_LONG
);
444 write_exp_elt_type
(builtin_type_char
);
445 write_exp_elt_longcst
((LONGEST
)'\0');
446 write_exp_elt_opcode
(OP_LONG
);
447 write_exp_elt_opcode
(OP_ARRAY
);
448 write_exp_elt_longcst
((LONGEST
) 0);
449 write_exp_elt_longcst
((LONGEST
) ($1.length
));
450 write_exp_elt_opcode
(OP_ARRAY
); }
455 { write_exp_elt_opcode
(OP_THIS
);
456 write_exp_elt_opcode
(OP_THIS
); }
459 /* end of object pascal. */
464 $$
= SYMBOL_BLOCK_VALUE
($1.sym
);
468 lookup_symtab
(copy_name
($1.stoken
));
470 $$
= BLOCKVECTOR_BLOCK
(BLOCKVECTOR
(tem
), STATIC_BLOCK
);
472 error ("No file or function \"%s\".",
473 copy_name
($1.stoken
));
478 block
: block COLONCOLON name
480 = lookup_symbol
(copy_name
($3), $1,
481 VAR_NAMESPACE
, (int *) NULL
,
482 (struct symtab
**) NULL
);
483 if
(!tem || SYMBOL_CLASS
(tem
) != LOC_BLOCK
)
484 error ("No function \"%s\" in specified context.",
486 $$
= SYMBOL_BLOCK_VALUE
(tem
); }
489 variable: block COLONCOLON name
490 { struct symbol
*sym
;
491 sym
= lookup_symbol
(copy_name
($3), $1,
492 VAR_NAMESPACE
, (int *) NULL
,
493 (struct symtab
**) NULL
);
495 error ("No symbol \"%s\" in specified context.",
498 write_exp_elt_opcode
(OP_VAR_VALUE
);
499 /* block_found is set by lookup_symbol. */
500 write_exp_elt_block
(block_found
);
501 write_exp_elt_sym
(sym
);
502 write_exp_elt_opcode
(OP_VAR_VALUE
); }
505 qualified_name: typebase COLONCOLON name
507 struct type
*type
= $1;
508 if
(TYPE_CODE
(type
) != TYPE_CODE_STRUCT
509 && TYPE_CODE
(type
) != TYPE_CODE_UNION
)
510 error ("`%s' is not defined as an aggregate type.",
513 write_exp_elt_opcode
(OP_SCOPE
);
514 write_exp_elt_type
(type
);
515 write_exp_string
($3);
516 write_exp_elt_opcode
(OP_SCOPE
);
520 variable: qualified_name
523 char *name
= copy_name
($2);
525 struct minimal_symbol
*msymbol
;
528 lookup_symbol
(name
, (const struct block
*) NULL
,
529 VAR_NAMESPACE
, (int *) NULL
,
530 (struct symtab
**) NULL
);
533 write_exp_elt_opcode
(OP_VAR_VALUE
);
534 write_exp_elt_block
(NULL
);
535 write_exp_elt_sym
(sym
);
536 write_exp_elt_opcode
(OP_VAR_VALUE
);
540 msymbol
= lookup_minimal_symbol
(name
, NULL
, NULL
);
543 write_exp_msymbol
(msymbol
,
544 lookup_function_type
(builtin_type_int
),
548 if
(!have_full_symbols
() && !have_partial_symbols
())
549 error ("No symbol table is loaded. Use the \"file\" command.");
551 error ("No symbol \"%s\" in current context.", name
);
555 variable: name_not_typename
556 { struct symbol
*sym
= $1.sym
;
560 if
(symbol_read_needs_frame
(sym
))
562 if
(innermost_block
== 0 ||
563 contained_in
(block_found
,
565 innermost_block
= block_found
;
568 write_exp_elt_opcode
(OP_VAR_VALUE
);
569 /* We want to use the selected frame, not
570 another more inner frame which happens to
571 be in the same block. */
572 write_exp_elt_block
(NULL
);
573 write_exp_elt_sym
(sym
);
574 write_exp_elt_opcode
(OP_VAR_VALUE
);
576 else if
($1.is_a_field_of_this
)
578 /* Object pascal: it hangs off of `this'. Must
579 not inadvertently convert from a method call
581 if
(innermost_block
== 0 ||
582 contained_in
(block_found
, innermost_block
))
583 innermost_block
= block_found
;
584 write_exp_elt_opcode
(OP_THIS
);
585 write_exp_elt_opcode
(OP_THIS
);
586 write_exp_elt_opcode
(STRUCTOP_PTR
);
587 write_exp_string
($1.stoken
);
588 write_exp_elt_opcode
(STRUCTOP_PTR
);
592 struct minimal_symbol
*msymbol
;
593 register
char *arg
= copy_name
($1.stoken
);
596 lookup_minimal_symbol
(arg
, NULL
, NULL
);
599 write_exp_msymbol
(msymbol
,
600 lookup_function_type
(builtin_type_int
),
603 else if
(!have_full_symbols
() && !have_partial_symbols
())
604 error ("No symbol table is loaded. Use the \"file\" command.");
606 error ("No symbol \"%s\" in current context.",
607 copy_name
($1.stoken
));
616 /* We used to try to recognize more pointer to member types here, but
617 that didn't work (shift/reduce conflicts meant that these rules never
618 got executed). The problem is that
619 int (foo::bar::baz::bizzle)
620 is a function type but
621 int (foo::bar::baz::bizzle::*)
622 is a pointer to member type. Stroustrup loses again! */
625 | typebase COLONCOLON
'*'
626 { $$
= lookup_member_type
(builtin_type_int
, $1); }
629 typebase
/* Implements (approximately): (type-qualifier)* type-specifier */
633 { $$
= lookup_struct
(copy_name
($2),
634 expression_context_block
); }
636 { $$
= lookup_struct
(copy_name
($2),
637 expression_context_block
); }
638 /* "const" and "volatile" are curently ignored. A type qualifier
639 after the type is handled in the ptype rule. I think these could
643 name
: NAME
{ $$
= $1.stoken
; }
644 | BLOCKNAME
{ $$
= $1.stoken
; }
645 | TYPENAME
{ $$
= $1.stoken
; }
646 | NAME_OR_INT
{ $$
= $1.stoken
; }
649 name_not_typename
: NAME
651 /* These would be useful if name_not_typename was useful, but it is just
652 a fake for "variable", so these cause reduce/reduce conflicts because
653 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
654 =exp) or just an exp. If name_not_typename was ever used in an lvalue
655 context where only a name could occur, this might be useful.
662 /* Take care of parsing a number (anything that starts with a digit).
663 Set yylval and return the token type; update lexptr.
664 LEN is the number of characters in it. */
666 /*** Needs some error checking for the float case ***/
669 parse_number
(p
, len
, parsed_float
, putithere
)
675 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
676 here, and we do kind of silly things like cast to unsigned. */
677 register LONGEST n
= 0;
678 register LONGEST prevn
= 0;
683 register
int base
= input_radix
;
686 /* Number of "L" suffixes encountered. */
689 /* We have found a "L" or "U" suffix. */
690 int found_suffix
= 0;
693 struct type
*signed_type
;
694 struct type
*unsigned_type
;
698 /* It's a float since it contains a point or an exponent. */
700 int num
= 0; /* number of tokens scanned by scanf */
701 char saved_char
= p
[len
];
703 p
[len
] = 0; /* null-terminate the token */
704 if
(sizeof
(putithere
->typed_val_float.dval
) <= sizeof
(float))
705 num
= sscanf
(p
, "%g%c", (float *) &putithere
->typed_val_float.dval
,&c
);
706 else if
(sizeof
(putithere
->typed_val_float.dval
) <= sizeof
(double))
707 num
= sscanf
(p
, "%lg%c", (double *) &putithere
->typed_val_float.dval
,&c
);
710 #ifdef SCANF_HAS_LONG_DOUBLE
711 num
= sscanf
(p
, "%Lg%c", &putithere
->typed_val_float.dval
,&c
);
713 /* Scan it into a double, then assign it to the long double.
714 This at least wins with values representable in the range
717 num
= sscanf
(p
, "%lg%c", &temp
,&c
);
718 putithere
->typed_val_float.dval
= temp
;
721 p
[len
] = saved_char
; /* restore the input stream */
722 if
(num
!= 1) /* check scanf found ONLY a float ... */
724 /* See if it has `f' or `l' suffix (float or long double). */
726 c
= tolower
(p
[len
- 1]);
729 putithere
->typed_val_float.type
= builtin_type_float
;
731 putithere
->typed_val_float.type
= builtin_type_long_double
;
732 else if
(isdigit
(c
) || c
== '.')
733 putithere
->typed_val_float.type
= builtin_type_double
;
740 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
774 if
(c
>= 'A' && c
<= 'Z')
776 if
(c
!= 'l' && c
!= 'u')
778 if
(c
>= '0' && c
<= '9')
786 if
(base
> 10 && c
>= 'a' && c
<= 'f')
790 n
+= i
= c
- 'a' + 10;
803 return ERROR
; /* Char not a digit */
806 return ERROR
; /* Invalid digit in this base */
808 /* Portably test for overflow (only works for nonzero values, so make
809 a second check for zero). FIXME: Can't we just make n and prevn
810 unsigned and avoid this? */
811 if
(c
!= 'l' && c
!= 'u' && (prevn
>= n
) && n
!= 0)
812 unsigned_p
= 1; /* Try something unsigned */
814 /* Portably test for unsigned overflow.
815 FIXME: This check is wrong; for example it doesn't find overflow
816 on 0x123456789 when LONGEST is 32 bits. */
817 if
(c
!= 'l' && c
!= 'u' && n
!= 0)
819 if
((unsigned_p
&& (ULONGEST
) prevn
>= (ULONGEST
) n
))
820 error ("Numeric constant too large.");
825 /* An integer constant is an int, a long, or a long long. An L
826 suffix forces it to be long; an LL suffix forces it to be long
827 long. If not forced to a larger size, it gets the first type of
828 the above that it fits in. To figure out whether it fits, we
829 shift it right and see whether anything remains. Note that we
830 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
831 operation, because many compilers will warn about such a shift
832 (which always produces a zero result). Sometimes TARGET_INT_BIT
833 or TARGET_LONG_BIT will be that big, sometimes not. To deal with
834 the case where it is we just always shift the value more than
835 once, with fewer bits each time. */
837 un
= (ULONGEST
)n
>> 2;
839 && (un
>> (TARGET_INT_BIT
- 2)) == 0)
841 high_bit
= ((ULONGEST
)1) << (TARGET_INT_BIT
-1);
843 /* A large decimal (not hex or octal) constant (between INT_MAX
844 and UINT_MAX) is a long or unsigned long, according to ANSI,
845 never an unsigned int, but this code treats it as unsigned
846 int. This probably should be fixed. GCC gives a warning on
849 unsigned_type
= builtin_type_unsigned_int
;
850 signed_type
= builtin_type_int
;
853 && (un
>> (TARGET_LONG_BIT
- 2)) == 0)
855 high_bit
= ((ULONGEST
)1) << (TARGET_LONG_BIT
-1);
856 unsigned_type
= builtin_type_unsigned_long
;
857 signed_type
= builtin_type_long
;
861 high_bit
= (((ULONGEST
)1)
862 << (TARGET_LONG_LONG_BIT
- 32 - 1)
866 /* A long long does not fit in a LONGEST. */
868 (ULONGEST
)1 << (sizeof
(LONGEST
) * HOST_CHAR_BIT
- 1);
869 unsigned_type
= builtin_type_unsigned_long_long
;
870 signed_type
= builtin_type_long_long
;
873 putithere
->typed_val_int.val
= n
;
875 /* If the high bit of the worked out type is set then this number
876 has to be unsigned. */
878 if
(unsigned_p ||
(n
& high_bit
))
880 putithere
->typed_val_int.type
= unsigned_type
;
884 putithere
->typed_val_int.type
= signed_type
;
894 enum exp_opcode opcode
;
897 static const struct token tokentab3
[] =
899 {"shr", RSH
, BINOP_END
},
900 {"shl", LSH
, BINOP_END
},
901 {"and", ANDAND
, BINOP_END
},
902 {"div", DIV
, BINOP_END
},
903 {"not", NOT
, BINOP_END
},
904 {"mod", MOD
, BINOP_END
},
905 {"inc", INCREMENT
, BINOP_END
},
906 {"dec", DECREMENT
, BINOP_END
},
907 {"xor", XOR
, BINOP_END
}
910 static const struct token tokentab2
[] =
912 {"or", OR
, BINOP_END
},
913 {"<>", NOTEQUAL
, BINOP_END
},
914 {"<=", LEQ
, BINOP_END
},
915 {">=", GEQ
, BINOP_END
},
916 {":=", ASSIGN
, BINOP_END
}
919 /* Allocate uppercased var */
920 /* make an uppercased copy of tokstart */
921 static char * uptok
(tokstart
, namelen
)
926 char *uptokstart
= (char *)malloc
(namelen
+1);
927 for
(i
= 0;i
<= namelen
;i
++)
929 if
((tokstart
[i
]>='a' && tokstart
[i
]<='z'))
930 uptokstart
[i
] = tokstart
[i
]-('a'-'A');
932 uptokstart
[i
] = tokstart
[i
];
934 uptokstart
[namelen
]='\0';
937 /* Read one token, getting characters through lexptr. */
950 int explen
, tempbufindex
;
951 static char *tempbuf
;
952 static int tempbufsize
;
957 explen
= strlen
(lexptr
);
958 /* See if it is a special token of length 3. */
960 for
(i
= 0; i
< sizeof
(tokentab3
) / sizeof
(tokentab3
[0]); i
++)
961 if
(strncasecmp
(tokstart
, tokentab3
[i
].operator
, 3) == 0
962 && (!isalpha
(tokentab3
[i
].operator
[0]) || explen
== 3
963 ||
(!isalpha
(tokstart
[3]) && !isdigit
(tokstart
[3]) && tokstart
[3] != '_')))
966 yylval.opcode
= tokentab3
[i
].opcode
;
967 return tokentab3
[i
].token
;
970 /* See if it is a special token of length 2. */
972 for
(i
= 0; i
< sizeof
(tokentab2
) / sizeof
(tokentab2
[0]); i
++)
973 if
(strncasecmp
(tokstart
, tokentab2
[i
].operator
, 2) == 0
974 && (!isalpha
(tokentab2
[i
].operator
[0]) || explen
== 2
975 ||
(!isalpha
(tokstart
[2]) && !isdigit
(tokstart
[2]) && tokstart
[2] != '_')))
978 yylval.opcode
= tokentab2
[i
].opcode
;
979 return tokentab2
[i
].token
;
982 switch
(c
= *tokstart
)
994 /* We either have a character constant ('0' or '\177' for example)
995 or we have a quoted symbol reference ('foo(int,int)' in object pascal
1000 c
= parse_escape
(&lexptr
);
1002 error ("Empty character constant.");
1004 yylval.typed_val_int.val
= c
;
1005 yylval.typed_val_int.type
= builtin_type_char
;
1010 namelen
= skip_quoted
(tokstart
) - tokstart
;
1013 lexptr
= tokstart
+ namelen
;
1014 if
(lexptr
[-1] != '\'')
1015 error ("Unmatched single quote.");
1018 uptokstart
= uptok
(tokstart
,namelen
);
1021 error ("Invalid character constant.");
1031 if
(paren_depth
== 0)
1038 if
(comma_terminates
&& paren_depth
== 0)
1044 /* Might be a floating point number. */
1045 if
(lexptr
[1] < '0' || lexptr
[1] > '9')
1046 goto symbol
; /* Nope, must be a symbol. */
1047 /* FALL THRU into number case. */
1060 /* It's a number. */
1061 int got_dot
= 0, got_e
= 0, toktype
;
1062 register
char *p
= tokstart
;
1063 int hex
= input_radix
> 10;
1065 if
(c
== '0' && (p
[1] == 'x' || p
[1] == 'X'))
1070 else if
(c
== '0' && (p
[1]=='t' || p
[1]=='T' || p
[1]=='d' || p
[1]=='D'))
1078 /* This test includes !hex because 'e' is a valid hex digit
1079 and thus does not indicate a floating point number when
1080 the radix is hex. */
1081 if
(!hex
&& !got_e
&& (*p
== 'e' ||
*p
== 'E'))
1082 got_dot
= got_e
= 1;
1083 /* This test does not include !hex, because a '.' always indicates
1084 a decimal floating point number regardless of the radix. */
1085 else if
(!got_dot
&& *p
== '.')
1087 else if
(got_e
&& (p
[-1] == 'e' || p
[-1] == 'E')
1088 && (*p
== '-' ||
*p
== '+'))
1089 /* This is the sign of the exponent, not the end of the
1092 /* We will take any letters or digits. parse_number will
1093 complain if past the radix, or if L or U are not final. */
1094 else if
((*p
< '0' ||
*p
> '9')
1095 && ((*p
< 'a' ||
*p
> 'z')
1096 && (*p
< 'A' ||
*p
> 'Z')))
1099 toktype
= parse_number
(tokstart
, p
- tokstart
, got_dot|got_e
, &yylval);
1100 if
(toktype
== ERROR
)
1102 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
1104 memcpy
(err_copy
, tokstart
, p
- tokstart
);
1105 err_copy
[p
- tokstart
] = 0;
1106 error ("Invalid number \"%s\".", err_copy
);
1137 /* Build the gdb internal form of the input string in tempbuf,
1138 translating any standard C escape forms seen. Note that the
1139 buffer is null byte terminated *only* for the convenience of
1140 debugging gdb itself and printing the buffer contents when
1141 the buffer contains no embedded nulls. Gdb does not depend
1142 upon the buffer being null byte terminated, it uses the length
1143 string instead. This allows gdb to handle C strings (as well
1144 as strings in other languages) with embedded null bytes */
1146 tokptr
= ++tokstart
;
1150 /* Grow the static temp buffer if necessary, including allocating
1151 the first one on demand. */
1152 if
(tempbufindex
+ 1 >= tempbufsize
)
1154 tempbuf
= (char *) realloc
(tempbuf
, tempbufsize
+= 64);
1160 /* Do nothing, loop will terminate. */
1164 c
= parse_escape
(&tokptr
);
1169 tempbuf
[tempbufindex
++] = c
;
1172 tempbuf
[tempbufindex
++] = *tokptr
++;
1175 } while
((*tokptr
!= '"') && (*tokptr
!= '\0'));
1176 if
(*tokptr
++ != '"')
1178 error ("Unterminated string in expression.");
1180 tempbuf
[tempbufindex
] = '\0'; /* See note above */
1181 yylval.sval.ptr
= tempbuf
;
1182 yylval.sval.length
= tempbufindex
;
1187 if
(!(c
== '_' || c
== '$'
1188 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z')))
1189 /* We must have come across a bad character (e.g. ';'). */
1190 error ("Invalid character '%c' in expression.", c
);
1192 /* It's a name. See how long it is. */
1194 for
(c
= tokstart
[namelen
];
1195 (c
== '_' || c
== '$' ||
(c
>= '0' && c
<= '9')
1196 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z') || c
== '<');)
1198 /* Template parameter lists are part of the name.
1199 FIXME: This mishandles `print $a<4&&$a>3'. */
1203 int nesting_level
= 1;
1204 while
(tokstart
[++i
])
1206 if
(tokstart
[i
] == '<')
1208 else if
(tokstart
[i
] == '>')
1210 if
(--nesting_level
== 0)
1214 if
(tokstart
[i
] == '>')
1220 /* do NOT uppercase internals because of registers !!! */
1221 c
= tokstart
[++namelen
];
1224 uptokstart
= uptok
(tokstart
,namelen
);
1226 /* The token "if" terminates the expression and is NOT
1227 removed from the input stream. */
1228 if
(namelen
== 2 && uptokstart
[0] == 'I' && uptokstart
[1] == 'F')
1237 /* Catch specific keywords. Should be done with a data structure. */
1241 if
(STREQ
(uptokstart
, "OBJECT"))
1243 if
(STREQ
(uptokstart
, "RECORD"))
1245 if
(STREQ
(uptokstart
, "SIZEOF"))
1249 if
(STREQ
(uptokstart
, "CLASS"))
1251 if
(STREQ
(uptokstart
, "FALSE"))
1258 if
(STREQ
(uptokstart
, "TRUE"))
1263 if
(STREQ
(uptokstart
, "SELF"))
1265 /* here we search for 'this' like
1266 inserted in FPC stabs debug info */
1267 static const char this_name
[] =
1268 { /* CPLUS_MARKER,*/ 't', 'h', 'i', 's', '\0' };
1270 if
(lookup_symbol
(this_name
, expression_context_block
,
1271 VAR_NAMESPACE
, (int *) NULL
,
1272 (struct symtab
**) NULL
))
1280 yylval.sval.ptr
= tokstart
;
1281 yylval.sval.length
= namelen
;
1283 if
(*tokstart
== '$')
1285 /* $ is the normal prefix for pascal hexadecimal values
1286 but this conflicts with the GDB use for debugger variables
1287 so in expression to enter hexadecimal values
1288 we still need to use C syntax with 0xff */
1289 write_dollar_variable
(yylval.sval
);
1293 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1294 functions or symtabs. If this is not so, then ...
1295 Use token-type TYPENAME for symbols that happen to be defined
1296 currently as names of types; NAME for other symbols.
1297 The caller is not constrained to care about the distinction. */
1299 char *tmp
= copy_name
(yylval.sval
);
1301 int is_a_field_of_this
= 0;
1304 sym
= lookup_symbol
(tmp
, expression_context_block
,
1306 &is_a_field_of_this
,
1307 (struct symtab
**) NULL
);
1308 /* second chance uppercased ! */
1311 for
(i
= 0;i
<= namelen
;i
++)
1313 if
((tmp
[i
]>='a' && tmp
[i
]<='z'))
1314 tmp
[i
] -= ('a'-'A');
1315 /* I am not sure that copy_name gives excatly the same result ! */
1316 if
((tokstart
[i
]>='a' && tokstart
[i
]<='z'))
1317 tokstart
[i
] -= ('a'-'A');
1319 sym
= lookup_symbol
(tmp
, expression_context_block
,
1321 &is_a_field_of_this
,
1322 (struct symtab
**) NULL
);
1324 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1325 no psymtabs (coff, xcoff, or some future change to blow away the
1326 psymtabs once once symbols are read). */
1327 if
((sym
&& SYMBOL_CLASS
(sym
) == LOC_BLOCK
) ||
1328 lookup_symtab
(tmp
))
1330 yylval.ssym.sym
= sym
;
1331 yylval.ssym.is_a_field_of_this
= is_a_field_of_this
;
1334 if
(sym
&& SYMBOL_CLASS
(sym
) == LOC_TYPEDEF
)
1337 /* Despite the following flaw, we need to keep this code enabled.
1338 Because we can get called from check_stub_method, if we don't
1339 handle nested types then it screws many operations in any
1340 program which uses nested types. */
1341 /* In "A::x", if x is a member function of A and there happens
1342 to be a type (nested or not, since the stabs don't make that
1343 distinction) named x, then this code incorrectly thinks we
1344 are dealing with nested types rather than a member function. */
1348 struct symbol
*best_sym
;
1350 /* Look ahead to detect nested types. This probably should be
1351 done in the grammar, but trying seemed to introduce a lot
1352 of shift/reduce and reduce/reduce conflicts. It's possible
1353 that it could be done, though. Or perhaps a non-grammar, but
1354 less ad hoc, approach would work well. */
1356 /* Since we do not currently have any way of distinguishing
1357 a nested type from a non-nested one (the stabs don't tell
1358 us whether a type is nested), we just ignore the
1365 /* Skip whitespace. */
1366 while
(*p
== ' ' ||
*p
== '\t' ||
*p
== '\n')
1368 if
(*p
== ':' && p
[1] == ':')
1370 /* Skip the `::'. */
1372 /* Skip whitespace. */
1373 while
(*p
== ' ' ||
*p
== '\t' ||
*p
== '\n')
1376 while
(*p
== '_' ||
*p
== '$' ||
(*p
>= '0' && *p
<= '9')
1377 ||
(*p
>= 'a' && *p
<= 'z')
1378 ||
(*p
>= 'A' && *p
<= 'Z'))
1382 struct symbol
*cur_sym
;
1383 /* As big as the whole rest of the expression, which is
1384 at least big enough. */
1385 char *ncopy
= alloca
(strlen
(tmp
)+strlen
(namestart
)+3);
1389 memcpy
(tmp1
, tmp
, strlen
(tmp
));
1390 tmp1
+= strlen
(tmp
);
1391 memcpy
(tmp1
, "::", 2);
1393 memcpy
(tmp1
, namestart
, p
- namestart
);
1394 tmp1
[p
- namestart
] = '\0';
1395 cur_sym
= lookup_symbol
(ncopy
, expression_context_block
,
1396 VAR_NAMESPACE
, (int *) NULL
,
1397 (struct symtab
**) NULL
);
1400 if
(SYMBOL_CLASS
(cur_sym
) == LOC_TYPEDEF
)
1418 yylval.tsym.type
= SYMBOL_TYPE
(best_sym
);
1420 yylval.tsym.type
= SYMBOL_TYPE
(sym
);
1424 if
((yylval.tsym.type
= lookup_primitive_typename
(tmp
)) != 0)
1427 /* Input names that aren't symbols but ARE valid hex numbers,
1428 when the input radix permits them, can be names or numbers
1429 depending on the parse. Note we support radixes > 16 here. */
1431 ((tokstart
[0] >= 'a' && tokstart
[0] < 'a' + input_radix
- 10) ||
1432 (tokstart
[0] >= 'A' && tokstart
[0] < 'A' + input_radix
- 10)))
1434 YYSTYPE newlval
; /* Its value is ignored. */
1435 hextype
= parse_number
(tokstart
, namelen
, 0, &newlval
);
1438 yylval.ssym.sym
= sym
;
1439 yylval.ssym.is_a_field_of_this
= is_a_field_of_this
;
1445 /* Any other kind of symbol */
1446 yylval.ssym.sym
= sym
;
1447 yylval.ssym.is_a_field_of_this
= is_a_field_of_this
;
1456 error ("A %s in expression, near `%s'.", (msg ? msg
: "error"), lexptr
);