Commit | Line | Data |
---|---|---|
373a8247 | 1 | /* YACC parser for Pascal expressions, for GDB. |
48426bc2 | 2 | Copyright (C) 2000, 2006 |
373a8247 PM |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
197e01b6 EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
373a8247 PM |
21 | |
22 | /* This file is derived from c-exp.y */ | |
23 | ||
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. | |
32 | ||
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. */ | |
40 | ||
29f319b8 | 41 | /* Known bugs or limitations: |
373a8247 PM |
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 */ | |
47 | %{ | |
48 | ||
49 | #include "defs.h" | |
50 | #include "gdb_string.h" | |
51 | #include <ctype.h> | |
52 | #include "expression.h" | |
53 | #include "value.h" | |
54 | #include "parser-defs.h" | |
55 | #include "language.h" | |
56 | #include "p-lang.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 */ | |
fe898f56 | 60 | #include "block.h" |
373a8247 PM |
61 | |
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. */ | |
68 | ||
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 | |
88 | #define yys pascal_s | |
89 | #define yy_yys pascal_yys | |
90 | #define yystate pascal_state | |
91 | #define yytmp pascal_tmp | |
92 | #define yyv pascal_v | |
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 */ | |
06891d83 JT |
98 | #define yyname pascal_name /* With YYDEBUG defined */ |
99 | #define yyrule pascal_rule /* With YYDEBUG defined */ | |
373a8247 PM |
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 | |
109 | ||
110 | #ifndef YYDEBUG | |
f461f5cf | 111 | #define YYDEBUG 1 /* Default to yydebug support */ |
373a8247 PM |
112 | #endif |
113 | ||
f461f5cf PM |
114 | #define YYFPRINTF parser_fprintf |
115 | ||
373a8247 PM |
116 | int yyparse (void); |
117 | ||
118 | static int yylex (void); | |
119 | ||
120 | void | |
121 | yyerror (char *); | |
122 | ||
123 | static char * uptok (char *, int); | |
124 | %} | |
125 | ||
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. */ | |
129 | ||
130 | %union | |
131 | { | |
132 | LONGEST lval; | |
133 | struct { | |
134 | LONGEST val; | |
135 | struct type *type; | |
136 | } typed_val_int; | |
137 | struct { | |
138 | DOUBLEST dval; | |
139 | struct type *type; | |
140 | } typed_val_float; | |
141 | struct symbol *sym; | |
142 | struct type *tval; | |
143 | struct stoken sval; | |
144 | struct ttype tsym; | |
145 | struct symtoken ssym; | |
146 | int voidval; | |
147 | struct block *bval; | |
148 | enum exp_opcode opcode; | |
149 | struct internalvar *ivar; | |
150 | ||
151 | struct type **tvec; | |
152 | int *ivec; | |
153 | } | |
154 | ||
155 | %{ | |
156 | /* YYSTYPE gets defined by %union */ | |
157 | static int | |
158 | parse_number (char *, int, int, YYSTYPE *); | |
9819c6c8 PM |
159 | |
160 | static struct type *current_type; | |
161 | ||
b9362cc7 AC |
162 | static void push_current_type (void); |
163 | static void pop_current_type (void); | |
9819c6c8 | 164 | static int search_field; |
373a8247 PM |
165 | %} |
166 | ||
9819c6c8 | 167 | %type <voidval> exp exp1 type_exp start normal_start variable qualified_name |
373a8247 PM |
168 | %type <tval> type typebase |
169 | /* %type <bval> block */ | |
170 | ||
171 | /* Fancy type parsing. */ | |
172 | %type <tval> ptype | |
173 | ||
174 | %token <typed_val_int> INT | |
175 | %token <typed_val_float> FLOAT | |
176 | ||
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. */ | |
184 | ||
9819c6c8 PM |
185 | %token <sval> STRING |
186 | %token <sval> FIELDNAME | |
373a8247 PM |
187 | %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */ |
188 | %token <tsym> TYPENAME | |
189 | %type <sval> name | |
190 | %type <ssym> name_not_typename | |
191 | ||
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. */ | |
196 | ||
197 | %token <ssym> NAME_OR_INT | |
198 | ||
199 | %token STRUCT CLASS SIZEOF COLONCOLON | |
200 | %token ERROR | |
201 | ||
202 | /* Special type cases, put in to allow the parser to distinguish different | |
203 | legal basetypes. */ | |
204 | ||
205 | %token <voidval> VARIABLE | |
206 | ||
207 | ||
208 | /* Object pascal */ | |
209 | %token THIS | |
2692ddb3 | 210 | %token <lval> TRUEKEYWORD FALSEKEYWORD |
373a8247 PM |
211 | |
212 | %left ',' | |
213 | %left ABOVE_COMMA | |
214 | %right ASSIGN | |
215 | %left NOT | |
216 | %left OR | |
217 | %left XOR | |
218 | %left ANDAND | |
219 | %left '=' NOTEQUAL | |
220 | %left '<' '>' LEQ GEQ | |
221 | %left LSH RSH DIV MOD | |
222 | %left '@' | |
223 | %left '+' '-' | |
224 | %left '*' '/' | |
225 | %right UNARY INCREMENT DECREMENT | |
226 | %right ARROW '.' '[' '(' | |
29f319b8 | 227 | %left '^' |
373a8247 PM |
228 | %token <ssym> BLOCKNAME |
229 | %type <bval> block | |
230 | %left COLONCOLON | |
231 | ||
232 | \f | |
233 | %% | |
234 | ||
9819c6c8 PM |
235 | start : { current_type = NULL; |
236 | search_field = 0; | |
237 | } | |
ef944135 TR |
238 | normal_start {} |
239 | ; | |
9819c6c8 PM |
240 | |
241 | normal_start : | |
242 | exp1 | |
373a8247 PM |
243 | | type_exp |
244 | ; | |
245 | ||
246 | type_exp: type | |
247 | { write_exp_elt_opcode(OP_TYPE); | |
248 | write_exp_elt_type($1); | |
9819c6c8 PM |
249 | write_exp_elt_opcode(OP_TYPE); |
250 | current_type = $1; } ; | |
373a8247 PM |
251 | |
252 | /* Expressions, including the comma operator. */ | |
253 | exp1 : exp | |
254 | | exp1 ',' exp | |
255 | { write_exp_elt_opcode (BINOP_COMMA); } | |
256 | ; | |
257 | ||
258 | /* Expressions, not including the comma operator. */ | |
259 | exp : exp '^' %prec UNARY | |
9819c6c8 PM |
260 | { write_exp_elt_opcode (UNOP_IND); |
261 | if (current_type) | |
262 | current_type = TYPE_TARGET_TYPE (current_type); } | |
ef944135 | 263 | ; |
373a8247 PM |
264 | |
265 | exp : '@' exp %prec UNARY | |
9819c6c8 PM |
266 | { write_exp_elt_opcode (UNOP_ADDR); |
267 | if (current_type) | |
268 | current_type = TYPE_POINTER_TYPE (current_type); } | |
ef944135 | 269 | ; |
373a8247 PM |
270 | |
271 | exp : '-' exp %prec UNARY | |
272 | { write_exp_elt_opcode (UNOP_NEG); } | |
273 | ; | |
274 | ||
275 | exp : NOT exp %prec UNARY | |
276 | { write_exp_elt_opcode (UNOP_LOGICAL_NOT); } | |
277 | ; | |
278 | ||
279 | exp : INCREMENT '(' exp ')' %prec UNARY | |
280 | { write_exp_elt_opcode (UNOP_PREINCREMENT); } | |
281 | ; | |
282 | ||
283 | exp : DECREMENT '(' exp ')' %prec UNARY | |
284 | { write_exp_elt_opcode (UNOP_PREDECREMENT); } | |
285 | ; | |
286 | ||
9819c6c8 PM |
287 | exp : exp '.' { search_field = 1; } |
288 | FIELDNAME | |
289 | /* name */ | |
373a8247 | 290 | { write_exp_elt_opcode (STRUCTOP_STRUCT); |
9819c6c8 PM |
291 | write_exp_string ($4); |
292 | write_exp_elt_opcode (STRUCTOP_STRUCT); | |
293 | search_field = 0; | |
294 | if (current_type) | |
295 | { while (TYPE_CODE (current_type) == TYPE_CODE_PTR) | |
296 | current_type = TYPE_TARGET_TYPE (current_type); | |
297 | current_type = lookup_struct_elt_type ( | |
020cc13c | 298 | current_type, $4.ptr, 0); }; |
9819c6c8 PM |
299 | } ; |
300 | exp : exp '[' | |
301 | /* We need to save the current_type value */ | |
302 | { char *arrayname; | |
303 | int arrayfieldindex; | |
304 | arrayfieldindex = is_pascal_string_type ( | |
305 | current_type, NULL, NULL, | |
306 | NULL, NULL, &arrayname); | |
307 | if (arrayfieldindex) | |
308 | { | |
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); | |
318 | } | |
319 | push_current_type (); } | |
320 | exp1 ']' | |
321 | { pop_current_type (); | |
322 | write_exp_elt_opcode (BINOP_SUBSCRIPT); | |
323 | if (current_type) | |
324 | current_type = TYPE_TARGET_TYPE (current_type); } | |
ef944135 | 325 | ; |
373a8247 PM |
326 | |
327 | exp : exp '(' | |
328 | /* This is to save the value of arglist_len | |
329 | being accumulated by an outer function call. */ | |
9819c6c8 PM |
330 | { push_current_type (); |
331 | start_arglist (); } | |
373a8247 PM |
332 | arglist ')' %prec ARROW |
333 | { write_exp_elt_opcode (OP_FUNCALL); | |
334 | write_exp_elt_longcst ((LONGEST) end_arglist ()); | |
9819c6c8 PM |
335 | write_exp_elt_opcode (OP_FUNCALL); |
336 | pop_current_type (); } | |
373a8247 PM |
337 | ; |
338 | ||
339 | arglist : | |
340 | | exp | |
341 | { arglist_len = 1; } | |
342 | | arglist ',' exp %prec ABOVE_COMMA | |
343 | { arglist_len++; } | |
344 | ; | |
345 | ||
346 | exp : type '(' exp ')' %prec UNARY | |
fd0e9d45 PM |
347 | { if (current_type) |
348 | { | |
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); | |
354 | } | |
355 | write_exp_elt_opcode (UNOP_CAST); | |
373a8247 | 356 | write_exp_elt_type ($1); |
9819c6c8 PM |
357 | write_exp_elt_opcode (UNOP_CAST); |
358 | current_type = $1; } | |
373a8247 PM |
359 | ; |
360 | ||
361 | exp : '(' exp1 ')' | |
362 | { } | |
363 | ; | |
364 | ||
365 | /* Binary operators in order of decreasing precedence. */ | |
366 | ||
367 | exp : exp '*' exp | |
368 | { write_exp_elt_opcode (BINOP_MUL); } | |
369 | ; | |
370 | ||
371 | exp : exp '/' exp | |
372 | { write_exp_elt_opcode (BINOP_DIV); } | |
373 | ; | |
374 | ||
375 | exp : exp DIV exp | |
376 | { write_exp_elt_opcode (BINOP_INTDIV); } | |
377 | ; | |
378 | ||
379 | exp : exp MOD exp | |
380 | { write_exp_elt_opcode (BINOP_REM); } | |
381 | ; | |
382 | ||
383 | exp : exp '+' exp | |
384 | { write_exp_elt_opcode (BINOP_ADD); } | |
385 | ; | |
386 | ||
387 | exp : exp '-' exp | |
388 | { write_exp_elt_opcode (BINOP_SUB); } | |
389 | ; | |
390 | ||
391 | exp : exp LSH exp | |
392 | { write_exp_elt_opcode (BINOP_LSH); } | |
393 | ; | |
394 | ||
395 | exp : exp RSH exp | |
396 | { write_exp_elt_opcode (BINOP_RSH); } | |
397 | ; | |
398 | ||
399 | exp : exp '=' exp | |
400 | { write_exp_elt_opcode (BINOP_EQUAL); } | |
401 | ; | |
402 | ||
403 | exp : exp NOTEQUAL exp | |
404 | { write_exp_elt_opcode (BINOP_NOTEQUAL); } | |
405 | ; | |
406 | ||
407 | exp : exp LEQ exp | |
408 | { write_exp_elt_opcode (BINOP_LEQ); } | |
409 | ; | |
410 | ||
411 | exp : exp GEQ exp | |
412 | { write_exp_elt_opcode (BINOP_GEQ); } | |
413 | ; | |
414 | ||
415 | exp : exp '<' exp | |
416 | { write_exp_elt_opcode (BINOP_LESS); } | |
417 | ; | |
418 | ||
419 | exp : exp '>' exp | |
420 | { write_exp_elt_opcode (BINOP_GTR); } | |
421 | ; | |
422 | ||
423 | exp : exp ANDAND exp | |
424 | { write_exp_elt_opcode (BINOP_BITWISE_AND); } | |
425 | ; | |
426 | ||
427 | exp : exp XOR exp | |
428 | { write_exp_elt_opcode (BINOP_BITWISE_XOR); } | |
429 | ; | |
430 | ||
431 | exp : exp OR exp | |
432 | { write_exp_elt_opcode (BINOP_BITWISE_IOR); } | |
433 | ; | |
434 | ||
435 | exp : exp ASSIGN exp | |
436 | { write_exp_elt_opcode (BINOP_ASSIGN); } | |
437 | ; | |
438 | ||
2692ddb3 | 439 | exp : TRUEKEYWORD |
373a8247 PM |
440 | { write_exp_elt_opcode (OP_BOOL); |
441 | write_exp_elt_longcst ((LONGEST) $1); | |
442 | write_exp_elt_opcode (OP_BOOL); } | |
443 | ; | |
444 | ||
2692ddb3 | 445 | exp : FALSEKEYWORD |
373a8247 PM |
446 | { write_exp_elt_opcode (OP_BOOL); |
447 | write_exp_elt_longcst ((LONGEST) $1); | |
448 | write_exp_elt_opcode (OP_BOOL); } | |
449 | ; | |
450 | ||
451 | exp : INT | |
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); } | |
456 | ; | |
457 | ||
458 | exp : NAME_OR_INT | |
459 | { YYSTYPE val; | |
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); | |
465 | } | |
466 | ; | |
467 | ||
468 | ||
469 | exp : FLOAT | |
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); } | |
474 | ; | |
475 | ||
476 | exp : variable | |
477 | ; | |
478 | ||
479 | exp : VARIABLE | |
480 | /* Already written by write_dollar_variable. */ | |
481 | ; | |
482 | ||
483 | exp : SIZEOF '(' type ')' %prec UNARY | |
484 | { write_exp_elt_opcode (OP_LONG); | |
485 | write_exp_elt_type (builtin_type_int); | |
486 | CHECK_TYPEDEF ($3); | |
487 | write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3)); | |
488 | write_exp_elt_opcode (OP_LONG); } | |
489 | ; | |
490 | ||
491 | exp : STRING | |
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 | |
496 | string. */ | |
497 | char *sp = $1.ptr; int count = $1.length; | |
498 | while (count-- > 0) | |
499 | { | |
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); | |
504 | } | |
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); } | |
513 | ; | |
514 | ||
515 | /* Object pascal */ | |
516 | exp : THIS | |
fd0e9d45 PM |
517 | { |
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); | |
524 | if (this_val) | |
04624583 | 525 | this_type = value_type (this_val); |
fd0e9d45 PM |
526 | else |
527 | this_type = NULL; | |
528 | if (this_type) | |
529 | { | |
530 | if (TYPE_CODE (this_type) == TYPE_CODE_PTR) | |
531 | { | |
532 | this_type = TYPE_TARGET_TYPE (this_type); | |
533 | write_exp_elt_opcode (UNOP_IND); | |
534 | } | |
535 | } | |
536 | ||
537 | current_type = this_type; | |
538 | } | |
373a8247 PM |
539 | ; |
540 | ||
541 | /* end of object pascal. */ | |
542 | ||
543 | block : BLOCKNAME | |
544 | { | |
545 | if ($1.sym != 0) | |
546 | $$ = SYMBOL_BLOCK_VALUE ($1.sym); | |
547 | else | |
548 | { | |
549 | struct symtab *tem = | |
550 | lookup_symtab (copy_name ($1.stoken)); | |
551 | if (tem) | |
552 | $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), STATIC_BLOCK); | |
553 | else | |
554 | error ("No file or function \"%s\".", | |
555 | copy_name ($1.stoken)); | |
556 | } | |
557 | } | |
558 | ; | |
559 | ||
560 | block : block COLONCOLON name | |
561 | { struct symbol *tem | |
562 | = lookup_symbol (copy_name ($3), $1, | |
176620f1 | 563 | VAR_DOMAIN, (int *) NULL, |
373a8247 PM |
564 | (struct symtab **) NULL); |
565 | if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK) | |
566 | error ("No function \"%s\" in specified context.", | |
567 | copy_name ($3)); | |
568 | $$ = SYMBOL_BLOCK_VALUE (tem); } | |
569 | ; | |
570 | ||
571 | variable: block COLONCOLON name | |
572 | { struct symbol *sym; | |
573 | sym = lookup_symbol (copy_name ($3), $1, | |
176620f1 | 574 | VAR_DOMAIN, (int *) NULL, |
373a8247 PM |
575 | (struct symtab **) NULL); |
576 | if (sym == 0) | |
577 | error ("No symbol \"%s\" in specified context.", | |
578 | copy_name ($3)); | |
579 | ||
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); } | |
585 | ; | |
586 | ||
587 | qualified_name: typebase COLONCOLON name | |
588 | { | |
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.", | |
593 | TYPE_NAME (type)); | |
594 | ||
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); | |
599 | } | |
600 | ; | |
601 | ||
602 | variable: qualified_name | |
603 | | COLONCOLON name | |
604 | { | |
605 | char *name = copy_name ($2); | |
606 | struct symbol *sym; | |
607 | struct minimal_symbol *msymbol; | |
608 | ||
609 | sym = | |
610 | lookup_symbol (name, (const struct block *) NULL, | |
176620f1 | 611 | VAR_DOMAIN, (int *) NULL, |
373a8247 PM |
612 | (struct symtab **) NULL); |
613 | if (sym) | |
614 | { | |
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); | |
619 | break; | |
620 | } | |
621 | ||
622 | msymbol = lookup_minimal_symbol (name, NULL, NULL); | |
623 | if (msymbol != NULL) | |
624 | { | |
625 | write_exp_msymbol (msymbol, | |
626 | lookup_function_type (builtin_type_int), | |
627 | builtin_type_int); | |
628 | } | |
629 | else | |
630 | if (!have_full_symbols () && !have_partial_symbols ()) | |
631 | error ("No symbol table is loaded. Use the \"file\" command."); | |
632 | else | |
633 | error ("No symbol \"%s\" in current context.", name); | |
634 | } | |
635 | ; | |
636 | ||
637 | variable: name_not_typename | |
638 | { struct symbol *sym = $1.sym; | |
639 | ||
640 | if (sym) | |
641 | { | |
642 | if (symbol_read_needs_frame (sym)) | |
643 | { | |
644 | if (innermost_block == 0 || | |
645 | contained_in (block_found, | |
646 | innermost_block)) | |
647 | innermost_block = block_found; | |
648 | } | |
649 | ||
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); | |
9819c6c8 | 657 | current_type = sym->type; } |
373a8247 PM |
658 | else if ($1.is_a_field_of_this) |
659 | { | |
9819c6c8 PM |
660 | struct value * this_val; |
661 | struct type * this_type; | |
373a8247 PM |
662 | /* Object pascal: it hangs off of `this'. Must |
663 | not inadvertently convert from a method call | |
664 | to data ref. */ | |
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); | |
9819c6c8 PM |
673 | /* we need type of this */ |
674 | this_val = value_of_this (0); | |
675 | if (this_val) | |
04624583 | 676 | this_type = value_type (this_val); |
9819c6c8 PM |
677 | else |
678 | this_type = NULL; | |
679 | if (this_type) | |
680 | current_type = lookup_struct_elt_type ( | |
681 | this_type, | |
020cc13c | 682 | copy_name ($1.stoken), 0); |
9819c6c8 PM |
683 | else |
684 | current_type = NULL; | |
373a8247 PM |
685 | } |
686 | else | |
687 | { | |
688 | struct minimal_symbol *msymbol; | |
710122da | 689 | char *arg = copy_name ($1.stoken); |
373a8247 PM |
690 | |
691 | msymbol = | |
692 | lookup_minimal_symbol (arg, NULL, NULL); | |
693 | if (msymbol != NULL) | |
694 | { | |
695 | write_exp_msymbol (msymbol, | |
696 | lookup_function_type (builtin_type_int), | |
697 | builtin_type_int); | |
698 | } | |
699 | else if (!have_full_symbols () && !have_partial_symbols ()) | |
700 | error ("No symbol table is loaded. Use the \"file\" command."); | |
701 | else | |
702 | error ("No symbol \"%s\" in current context.", | |
703 | copy_name ($1.stoken)); | |
704 | } | |
705 | } | |
706 | ; | |
707 | ||
708 | ||
709 | ptype : typebase | |
710 | ; | |
711 | ||
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! */ | |
719 | ||
720 | type : ptype | |
721 | | typebase COLONCOLON '*' | |
722 | { $$ = lookup_member_type (builtin_type_int, $1); } | |
723 | ; | |
724 | ||
725 | typebase /* Implements (approximately): (type-qualifier)* type-specifier */ | |
fd0e9d45 PM |
726 | : '^' typebase |
727 | { $$ = lookup_pointer_type ($2); } | |
728 | | TYPENAME | |
373a8247 PM |
729 | { $$ = $1.type; } |
730 | | STRUCT name | |
731 | { $$ = lookup_struct (copy_name ($2), | |
732 | expression_context_block); } | |
733 | | CLASS name | |
734 | { $$ = lookup_struct (copy_name ($2), | |
735 | expression_context_block); } | |
736 | /* "const" and "volatile" are curently ignored. A type qualifier | |
737 | after the type is handled in the ptype rule. I think these could | |
738 | be too. */ | |
739 | ; | |
740 | ||
741 | name : NAME { $$ = $1.stoken; } | |
742 | | BLOCKNAME { $$ = $1.stoken; } | |
743 | | TYPENAME { $$ = $1.stoken; } | |
744 | | NAME_OR_INT { $$ = $1.stoken; } | |
745 | ; | |
746 | ||
747 | name_not_typename : NAME | |
748 | | BLOCKNAME | |
749 | /* These would be useful if name_not_typename was useful, but it is just | |
750 | a fake for "variable", so these cause reduce/reduce conflicts because | |
751 | the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable, | |
752 | =exp) or just an exp. If name_not_typename was ever used in an lvalue | |
753 | context where only a name could occur, this might be useful. | |
754 | | NAME_OR_INT | |
755 | */ | |
756 | ; | |
757 | ||
758 | %% | |
759 | ||
760 | /* Take care of parsing a number (anything that starts with a digit). | |
761 | Set yylval and return the token type; update lexptr. | |
762 | LEN is the number of characters in it. */ | |
763 | ||
764 | /*** Needs some error checking for the float case ***/ | |
765 | ||
766 | static int | |
767 | parse_number (p, len, parsed_float, putithere) | |
710122da DC |
768 | char *p; |
769 | int len; | |
373a8247 PM |
770 | int parsed_float; |
771 | YYSTYPE *putithere; | |
772 | { | |
773 | /* FIXME: Shouldn't these be unsigned? We don't deal with negative values | |
774 | here, and we do kind of silly things like cast to unsigned. */ | |
710122da DC |
775 | LONGEST n = 0; |
776 | LONGEST prevn = 0; | |
373a8247 PM |
777 | ULONGEST un; |
778 | ||
710122da DC |
779 | int i = 0; |
780 | int c; | |
781 | int base = input_radix; | |
373a8247 PM |
782 | int unsigned_p = 0; |
783 | ||
784 | /* Number of "L" suffixes encountered. */ | |
785 | int long_p = 0; | |
786 | ||
787 | /* We have found a "L" or "U" suffix. */ | |
788 | int found_suffix = 0; | |
789 | ||
790 | ULONGEST high_bit; | |
791 | struct type *signed_type; | |
792 | struct type *unsigned_type; | |
793 | ||
794 | if (parsed_float) | |
795 | { | |
796 | /* It's a float since it contains a point or an exponent. */ | |
797 | char c; | |
798 | int num = 0; /* number of tokens scanned by scanf */ | |
799 | char saved_char = p[len]; | |
800 | ||
801 | p[len] = 0; /* null-terminate the token */ | |
a6205f53 | 802 | num = sscanf (p, DOUBLEST_SCAN_FORMAT "%c", |
96c1eda2 | 803 | &putithere->typed_val_float.dval, &c); |
373a8247 PM |
804 | p[len] = saved_char; /* restore the input stream */ |
805 | if (num != 1) /* check scanf found ONLY a float ... */ | |
806 | return ERROR; | |
807 | /* See if it has `f' or `l' suffix (float or long double). */ | |
808 | ||
809 | c = tolower (p[len - 1]); | |
810 | ||
811 | if (c == 'f') | |
812 | putithere->typed_val_float.type = builtin_type_float; | |
813 | else if (c == 'l') | |
814 | putithere->typed_val_float.type = builtin_type_long_double; | |
815 | else if (isdigit (c) || c == '.') | |
816 | putithere->typed_val_float.type = builtin_type_double; | |
817 | else | |
818 | return ERROR; | |
819 | ||
820 | return FLOAT; | |
821 | } | |
822 | ||
823 | /* Handle base-switching prefixes 0x, 0t, 0d, 0 */ | |
824 | if (p[0] == '0') | |
825 | switch (p[1]) | |
826 | { | |
827 | case 'x': | |
828 | case 'X': | |
829 | if (len >= 3) | |
830 | { | |
831 | p += 2; | |
832 | base = 16; | |
833 | len -= 2; | |
834 | } | |
835 | break; | |
836 | ||
837 | case 't': | |
838 | case 'T': | |
839 | case 'd': | |
840 | case 'D': | |
841 | if (len >= 3) | |
842 | { | |
843 | p += 2; | |
844 | base = 10; | |
845 | len -= 2; | |
846 | } | |
847 | break; | |
848 | ||
849 | default: | |
850 | base = 8; | |
851 | break; | |
852 | } | |
853 | ||
854 | while (len-- > 0) | |
855 | { | |
856 | c = *p++; | |
857 | if (c >= 'A' && c <= 'Z') | |
858 | c += 'a' - 'A'; | |
859 | if (c != 'l' && c != 'u') | |
860 | n *= base; | |
861 | if (c >= '0' && c <= '9') | |
862 | { | |
863 | if (found_suffix) | |
864 | return ERROR; | |
865 | n += i = c - '0'; | |
866 | } | |
867 | else | |
868 | { | |
869 | if (base > 10 && c >= 'a' && c <= 'f') | |
870 | { | |
871 | if (found_suffix) | |
872 | return ERROR; | |
873 | n += i = c - 'a' + 10; | |
874 | } | |
875 | else if (c == 'l') | |
876 | { | |
877 | ++long_p; | |
878 | found_suffix = 1; | |
879 | } | |
880 | else if (c == 'u') | |
881 | { | |
882 | unsigned_p = 1; | |
883 | found_suffix = 1; | |
884 | } | |
885 | else | |
886 | return ERROR; /* Char not a digit */ | |
887 | } | |
888 | if (i >= base) | |
889 | return ERROR; /* Invalid digit in this base */ | |
890 | ||
891 | /* Portably test for overflow (only works for nonzero values, so make | |
892 | a second check for zero). FIXME: Can't we just make n and prevn | |
893 | unsigned and avoid this? */ | |
894 | if (c != 'l' && c != 'u' && (prevn >= n) && n != 0) | |
895 | unsigned_p = 1; /* Try something unsigned */ | |
896 | ||
897 | /* Portably test for unsigned overflow. | |
898 | FIXME: This check is wrong; for example it doesn't find overflow | |
899 | on 0x123456789 when LONGEST is 32 bits. */ | |
900 | if (c != 'l' && c != 'u' && n != 0) | |
901 | { | |
902 | if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) | |
903 | error ("Numeric constant too large."); | |
904 | } | |
905 | prevn = n; | |
906 | } | |
907 | ||
908 | /* An integer constant is an int, a long, or a long long. An L | |
909 | suffix forces it to be long; an LL suffix forces it to be long | |
910 | long. If not forced to a larger size, it gets the first type of | |
911 | the above that it fits in. To figure out whether it fits, we | |
912 | shift it right and see whether anything remains. Note that we | |
913 | can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one | |
914 | operation, because many compilers will warn about such a shift | |
915 | (which always produces a zero result). Sometimes TARGET_INT_BIT | |
916 | or TARGET_LONG_BIT will be that big, sometimes not. To deal with | |
917 | the case where it is we just always shift the value more than | |
918 | once, with fewer bits each time. */ | |
919 | ||
920 | un = (ULONGEST)n >> 2; | |
921 | if (long_p == 0 | |
922 | && (un >> (TARGET_INT_BIT - 2)) == 0) | |
923 | { | |
924 | high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1); | |
925 | ||
926 | /* A large decimal (not hex or octal) constant (between INT_MAX | |
927 | and UINT_MAX) is a long or unsigned long, according to ANSI, | |
928 | never an unsigned int, but this code treats it as unsigned | |
929 | int. This probably should be fixed. GCC gives a warning on | |
930 | such constants. */ | |
931 | ||
932 | unsigned_type = builtin_type_unsigned_int; | |
933 | signed_type = builtin_type_int; | |
934 | } | |
935 | else if (long_p <= 1 | |
936 | && (un >> (TARGET_LONG_BIT - 2)) == 0) | |
937 | { | |
938 | high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1); | |
939 | unsigned_type = builtin_type_unsigned_long; | |
940 | signed_type = builtin_type_long; | |
941 | } | |
942 | else | |
943 | { | |
7451d027 AC |
944 | int shift; |
945 | if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT) | |
373a8247 | 946 | /* A long long does not fit in a LONGEST. */ |
7451d027 AC |
947 | shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1); |
948 | else | |
949 | shift = (TARGET_LONG_LONG_BIT - 1); | |
950 | high_bit = (ULONGEST) 1 << shift; | |
373a8247 PM |
951 | unsigned_type = builtin_type_unsigned_long_long; |
952 | signed_type = builtin_type_long_long; | |
953 | } | |
954 | ||
955 | putithere->typed_val_int.val = n; | |
956 | ||
957 | /* If the high bit of the worked out type is set then this number | |
958 | has to be unsigned. */ | |
959 | ||
960 | if (unsigned_p || (n & high_bit)) | |
961 | { | |
962 | putithere->typed_val_int.type = unsigned_type; | |
963 | } | |
964 | else | |
965 | { | |
966 | putithere->typed_val_int.type = signed_type; | |
967 | } | |
968 | ||
969 | return INT; | |
970 | } | |
971 | ||
9819c6c8 PM |
972 | |
973 | struct type_push | |
974 | { | |
975 | struct type *stored; | |
976 | struct type_push *next; | |
977 | }; | |
978 | ||
979 | static struct type_push *tp_top = NULL; | |
980 | ||
b9362cc7 AC |
981 | static void |
982 | push_current_type (void) | |
9819c6c8 PM |
983 | { |
984 | struct type_push *tpnew; | |
985 | tpnew = (struct type_push *) malloc (sizeof (struct type_push)); | |
986 | tpnew->next = tp_top; | |
987 | tpnew->stored = current_type; | |
988 | current_type = NULL; | |
989 | tp_top = tpnew; | |
990 | } | |
991 | ||
b9362cc7 AC |
992 | static void |
993 | pop_current_type (void) | |
9819c6c8 PM |
994 | { |
995 | struct type_push *tp = tp_top; | |
996 | if (tp) | |
997 | { | |
998 | current_type = tp->stored; | |
999 | tp_top = tp->next; | |
1000 | xfree (tp); | |
1001 | } | |
1002 | } | |
1003 | ||
373a8247 PM |
1004 | struct token |
1005 | { | |
1006 | char *operator; | |
1007 | int token; | |
1008 | enum exp_opcode opcode; | |
1009 | }; | |
1010 | ||
1011 | static const struct token tokentab3[] = | |
1012 | { | |
1013 | {"shr", RSH, BINOP_END}, | |
1014 | {"shl", LSH, BINOP_END}, | |
1015 | {"and", ANDAND, BINOP_END}, | |
1016 | {"div", DIV, BINOP_END}, | |
1017 | {"not", NOT, BINOP_END}, | |
1018 | {"mod", MOD, BINOP_END}, | |
1019 | {"inc", INCREMENT, BINOP_END}, | |
1020 | {"dec", DECREMENT, BINOP_END}, | |
1021 | {"xor", XOR, BINOP_END} | |
1022 | }; | |
1023 | ||
1024 | static const struct token tokentab2[] = | |
1025 | { | |
1026 | {"or", OR, BINOP_END}, | |
1027 | {"<>", NOTEQUAL, BINOP_END}, | |
1028 | {"<=", LEQ, BINOP_END}, | |
1029 | {">=", GEQ, BINOP_END}, | |
9819c6c8 PM |
1030 | {":=", ASSIGN, BINOP_END}, |
1031 | {"::", COLONCOLON, BINOP_END} }; | |
373a8247 PM |
1032 | |
1033 | /* Allocate uppercased var */ | |
1034 | /* make an uppercased copy of tokstart */ | |
1035 | static char * uptok (tokstart, namelen) | |
1036 | char *tokstart; | |
1037 | int namelen; | |
1038 | { | |
1039 | int i; | |
1040 | char *uptokstart = (char *)malloc(namelen+1); | |
1041 | for (i = 0;i <= namelen;i++) | |
1042 | { | |
1043 | if ((tokstart[i]>='a' && tokstart[i]<='z')) | |
1044 | uptokstart[i] = tokstart[i]-('a'-'A'); | |
1045 | else | |
1046 | uptokstart[i] = tokstart[i]; | |
1047 | } | |
1048 | uptokstart[namelen]='\0'; | |
1049 | return uptokstart; | |
1050 | } | |
1051 | /* Read one token, getting characters through lexptr. */ | |
1052 | ||
1053 | ||
1054 | static int | |
1055 | yylex () | |
1056 | { | |
1057 | int c; | |
1058 | int namelen; | |
1059 | unsigned int i; | |
1060 | char *tokstart; | |
1061 | char *uptokstart; | |
1062 | char *tokptr; | |
1063 | char *p; | |
d3d6d173 | 1064 | int explen, tempbufindex; |
373a8247 PM |
1065 | static char *tempbuf; |
1066 | static int tempbufsize; | |
1067 | ||
1068 | retry: | |
1069 | ||
24467a86 PM |
1070 | prev_lexptr = lexptr; |
1071 | ||
373a8247 | 1072 | tokstart = lexptr; |
d3d6d173 | 1073 | explen = strlen (lexptr); |
373a8247 | 1074 | /* See if it is a special token of length 3. */ |
d3d6d173 PM |
1075 | if (explen > 2) |
1076 | for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++) | |
9c21ccdc | 1077 | if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0 |
d3d6d173 PM |
1078 | && (!isalpha (tokentab3[i].operator[0]) || explen == 3 |
1079 | || (!isalpha (tokstart[3]) && !isdigit (tokstart[3]) && tokstart[3] != '_'))) | |
1080 | { | |
1081 | lexptr += 3; | |
1082 | yylval.opcode = tokentab3[i].opcode; | |
1083 | return tokentab3[i].token; | |
1084 | } | |
373a8247 PM |
1085 | |
1086 | /* See if it is a special token of length 2. */ | |
d3d6d173 PM |
1087 | if (explen > 1) |
1088 | for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++) | |
9c21ccdc | 1089 | if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0 |
d3d6d173 PM |
1090 | && (!isalpha (tokentab2[i].operator[0]) || explen == 2 |
1091 | || (!isalpha (tokstart[2]) && !isdigit (tokstart[2]) && tokstart[2] != '_'))) | |
1092 | { | |
1093 | lexptr += 2; | |
1094 | yylval.opcode = tokentab2[i].opcode; | |
1095 | return tokentab2[i].token; | |
1096 | } | |
373a8247 PM |
1097 | |
1098 | switch (c = *tokstart) | |
1099 | { | |
1100 | case 0: | |
1101 | return 0; | |
1102 | ||
1103 | case ' ': | |
1104 | case '\t': | |
1105 | case '\n': | |
1106 | lexptr++; | |
1107 | goto retry; | |
1108 | ||
1109 | case '\'': | |
1110 | /* We either have a character constant ('0' or '\177' for example) | |
1111 | or we have a quoted symbol reference ('foo(int,int)' in object pascal | |
1112 | for example). */ | |
1113 | lexptr++; | |
1114 | c = *lexptr++; | |
1115 | if (c == '\\') | |
1116 | c = parse_escape (&lexptr); | |
1117 | else if (c == '\'') | |
1118 | error ("Empty character constant."); | |
1119 | ||
1120 | yylval.typed_val_int.val = c; | |
1121 | yylval.typed_val_int.type = builtin_type_char; | |
1122 | ||
1123 | c = *lexptr++; | |
1124 | if (c != '\'') | |
1125 | { | |
1126 | namelen = skip_quoted (tokstart) - tokstart; | |
1127 | if (namelen > 2) | |
1128 | { | |
1129 | lexptr = tokstart + namelen; | |
1130 | if (lexptr[-1] != '\'') | |
1131 | error ("Unmatched single quote."); | |
1132 | namelen -= 2; | |
1133 | tokstart++; | |
1134 | uptokstart = uptok(tokstart,namelen); | |
1135 | goto tryname; | |
1136 | } | |
1137 | error ("Invalid character constant."); | |
1138 | } | |
1139 | return INT; | |
1140 | ||
1141 | case '(': | |
1142 | paren_depth++; | |
1143 | lexptr++; | |
1144 | return c; | |
1145 | ||
1146 | case ')': | |
1147 | if (paren_depth == 0) | |
1148 | return 0; | |
1149 | paren_depth--; | |
1150 | lexptr++; | |
1151 | return c; | |
1152 | ||
1153 | case ',': | |
1154 | if (comma_terminates && paren_depth == 0) | |
1155 | return 0; | |
1156 | lexptr++; | |
1157 | return c; | |
1158 | ||
1159 | case '.': | |
1160 | /* Might be a floating point number. */ | |
1161 | if (lexptr[1] < '0' || lexptr[1] > '9') | |
1162 | goto symbol; /* Nope, must be a symbol. */ | |
1163 | /* FALL THRU into number case. */ | |
1164 | ||
1165 | case '0': | |
1166 | case '1': | |
1167 | case '2': | |
1168 | case '3': | |
1169 | case '4': | |
1170 | case '5': | |
1171 | case '6': | |
1172 | case '7': | |
1173 | case '8': | |
1174 | case '9': | |
1175 | { | |
1176 | /* It's a number. */ | |
1177 | int got_dot = 0, got_e = 0, toktype; | |
710122da | 1178 | char *p = tokstart; |
373a8247 PM |
1179 | int hex = input_radix > 10; |
1180 | ||
1181 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) | |
1182 | { | |
1183 | p += 2; | |
1184 | hex = 1; | |
1185 | } | |
1186 | else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D')) | |
1187 | { | |
1188 | p += 2; | |
1189 | hex = 0; | |
1190 | } | |
1191 | ||
1192 | for (;; ++p) | |
1193 | { | |
1194 | /* This test includes !hex because 'e' is a valid hex digit | |
1195 | and thus does not indicate a floating point number when | |
1196 | the radix is hex. */ | |
1197 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) | |
1198 | got_dot = got_e = 1; | |
1199 | /* This test does not include !hex, because a '.' always indicates | |
1200 | a decimal floating point number regardless of the radix. */ | |
1201 | else if (!got_dot && *p == '.') | |
1202 | got_dot = 1; | |
1203 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') | |
1204 | && (*p == '-' || *p == '+')) | |
1205 | /* This is the sign of the exponent, not the end of the | |
1206 | number. */ | |
1207 | continue; | |
1208 | /* We will take any letters or digits. parse_number will | |
1209 | complain if past the radix, or if L or U are not final. */ | |
1210 | else if ((*p < '0' || *p > '9') | |
1211 | && ((*p < 'a' || *p > 'z') | |
1212 | && (*p < 'A' || *p > 'Z'))) | |
1213 | break; | |
1214 | } | |
1215 | toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval); | |
1216 | if (toktype == ERROR) | |
1217 | { | |
1218 | char *err_copy = (char *) alloca (p - tokstart + 1); | |
1219 | ||
1220 | memcpy (err_copy, tokstart, p - tokstart); | |
1221 | err_copy[p - tokstart] = 0; | |
1222 | error ("Invalid number \"%s\".", err_copy); | |
1223 | } | |
1224 | lexptr = p; | |
1225 | return toktype; | |
1226 | } | |
1227 | ||
1228 | case '+': | |
1229 | case '-': | |
1230 | case '*': | |
1231 | case '/': | |
1232 | case '|': | |
1233 | case '&': | |
1234 | case '^': | |
1235 | case '~': | |
1236 | case '!': | |
1237 | case '@': | |
1238 | case '<': | |
1239 | case '>': | |
1240 | case '[': | |
1241 | case ']': | |
1242 | case '?': | |
1243 | case ':': | |
1244 | case '=': | |
1245 | case '{': | |
1246 | case '}': | |
1247 | symbol: | |
1248 | lexptr++; | |
1249 | return c; | |
1250 | ||
1251 | case '"': | |
1252 | ||
1253 | /* Build the gdb internal form of the input string in tempbuf, | |
1254 | translating any standard C escape forms seen. Note that the | |
1255 | buffer is null byte terminated *only* for the convenience of | |
1256 | debugging gdb itself and printing the buffer contents when | |
1257 | the buffer contains no embedded nulls. Gdb does not depend | |
1258 | upon the buffer being null byte terminated, it uses the length | |
1259 | string instead. This allows gdb to handle C strings (as well | |
1260 | as strings in other languages) with embedded null bytes */ | |
1261 | ||
1262 | tokptr = ++tokstart; | |
1263 | tempbufindex = 0; | |
1264 | ||
1265 | do { | |
1266 | /* Grow the static temp buffer if necessary, including allocating | |
1267 | the first one on demand. */ | |
1268 | if (tempbufindex + 1 >= tempbufsize) | |
1269 | { | |
1270 | tempbuf = (char *) realloc (tempbuf, tempbufsize += 64); | |
1271 | } | |
9819c6c8 | 1272 | |
373a8247 PM |
1273 | switch (*tokptr) |
1274 | { | |
1275 | case '\0': | |
1276 | case '"': | |
1277 | /* Do nothing, loop will terminate. */ | |
1278 | break; | |
1279 | case '\\': | |
1280 | tokptr++; | |
1281 | c = parse_escape (&tokptr); | |
1282 | if (c == -1) | |
1283 | { | |
1284 | continue; | |
1285 | } | |
1286 | tempbuf[tempbufindex++] = c; | |
1287 | break; | |
1288 | default: | |
1289 | tempbuf[tempbufindex++] = *tokptr++; | |
1290 | break; | |
1291 | } | |
1292 | } while ((*tokptr != '"') && (*tokptr != '\0')); | |
1293 | if (*tokptr++ != '"') | |
1294 | { | |
1295 | error ("Unterminated string in expression."); | |
1296 | } | |
1297 | tempbuf[tempbufindex] = '\0'; /* See note above */ | |
1298 | yylval.sval.ptr = tempbuf; | |
1299 | yylval.sval.length = tempbufindex; | |
1300 | lexptr = tokptr; | |
1301 | return (STRING); | |
1302 | } | |
1303 | ||
1304 | if (!(c == '_' || c == '$' | |
1305 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) | |
1306 | /* We must have come across a bad character (e.g. ';'). */ | |
1307 | error ("Invalid character '%c' in expression.", c); | |
1308 | ||
1309 | /* It's a name. See how long it is. */ | |
1310 | namelen = 0; | |
1311 | for (c = tokstart[namelen]; | |
1312 | (c == '_' || c == '$' || (c >= '0' && c <= '9') | |
1313 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');) | |
1314 | { | |
1315 | /* Template parameter lists are part of the name. | |
1316 | FIXME: This mishandles `print $a<4&&$a>3'. */ | |
1317 | if (c == '<') | |
1318 | { | |
1319 | int i = namelen; | |
1320 | int nesting_level = 1; | |
1321 | while (tokstart[++i]) | |
1322 | { | |
1323 | if (tokstart[i] == '<') | |
1324 | nesting_level++; | |
1325 | else if (tokstart[i] == '>') | |
1326 | { | |
1327 | if (--nesting_level == 0) | |
1328 | break; | |
1329 | } | |
1330 | } | |
1331 | if (tokstart[i] == '>') | |
1332 | namelen = i; | |
1333 | else | |
1334 | break; | |
1335 | } | |
1336 | ||
1337 | /* do NOT uppercase internals because of registers !!! */ | |
1338 | c = tokstart[++namelen]; | |
1339 | } | |
1340 | ||
1341 | uptokstart = uptok(tokstart,namelen); | |
1342 | ||
1343 | /* The token "if" terminates the expression and is NOT | |
1344 | removed from the input stream. */ | |
1345 | if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F') | |
1346 | { | |
1347 | return 0; | |
1348 | } | |
1349 | ||
1350 | lexptr += namelen; | |
1351 | ||
1352 | tryname: | |
1353 | ||
1354 | /* Catch specific keywords. Should be done with a data structure. */ | |
1355 | switch (namelen) | |
1356 | { | |
1357 | case 6: | |
cb137aa5 | 1358 | if (DEPRECATED_STREQ (uptokstart, "OBJECT")) |
373a8247 | 1359 | return CLASS; |
cb137aa5 | 1360 | if (DEPRECATED_STREQ (uptokstart, "RECORD")) |
373a8247 | 1361 | return STRUCT; |
cb137aa5 | 1362 | if (DEPRECATED_STREQ (uptokstart, "SIZEOF")) |
373a8247 PM |
1363 | return SIZEOF; |
1364 | break; | |
1365 | case 5: | |
cb137aa5 | 1366 | if (DEPRECATED_STREQ (uptokstart, "CLASS")) |
373a8247 | 1367 | return CLASS; |
cb137aa5 | 1368 | if (DEPRECATED_STREQ (uptokstart, "FALSE")) |
373a8247 PM |
1369 | { |
1370 | yylval.lval = 0; | |
2692ddb3 | 1371 | return FALSEKEYWORD; |
373a8247 PM |
1372 | } |
1373 | break; | |
1374 | case 4: | |
cb137aa5 | 1375 | if (DEPRECATED_STREQ (uptokstart, "TRUE")) |
373a8247 PM |
1376 | { |
1377 | yylval.lval = 1; | |
2692ddb3 | 1378 | return TRUEKEYWORD; |
373a8247 | 1379 | } |
cb137aa5 | 1380 | if (DEPRECATED_STREQ (uptokstart, "SELF")) |
373a8247 PM |
1381 | { |
1382 | /* here we search for 'this' like | |
1383 | inserted in FPC stabs debug info */ | |
8343f86c | 1384 | static const char this_name[] = "this"; |
373a8247 PM |
1385 | |
1386 | if (lookup_symbol (this_name, expression_context_block, | |
176620f1 | 1387 | VAR_DOMAIN, (int *) NULL, |
373a8247 PM |
1388 | (struct symtab **) NULL)) |
1389 | return THIS; | |
1390 | } | |
1391 | break; | |
1392 | default: | |
1393 | break; | |
1394 | } | |
1395 | ||
1396 | yylval.sval.ptr = tokstart; | |
1397 | yylval.sval.length = namelen; | |
1398 | ||
1399 | if (*tokstart == '$') | |
1400 | { | |
1401 | /* $ is the normal prefix for pascal hexadecimal values | |
1402 | but this conflicts with the GDB use for debugger variables | |
1403 | so in expression to enter hexadecimal values | |
1404 | we still need to use C syntax with 0xff */ | |
1405 | write_dollar_variable (yylval.sval); | |
1406 | return VARIABLE; | |
1407 | } | |
1408 | ||
1409 | /* Use token-type BLOCKNAME for symbols that happen to be defined as | |
1410 | functions or symtabs. If this is not so, then ... | |
1411 | Use token-type TYPENAME for symbols that happen to be defined | |
1412 | currently as names of types; NAME for other symbols. | |
1413 | The caller is not constrained to care about the distinction. */ | |
1414 | { | |
1415 | char *tmp = copy_name (yylval.sval); | |
1416 | struct symbol *sym; | |
1417 | int is_a_field_of_this = 0; | |
9819c6c8 | 1418 | int is_a_field = 0; |
373a8247 PM |
1419 | int hextype; |
1420 | ||
9819c6c8 PM |
1421 | |
1422 | if (search_field && current_type) | |
1423 | is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL); | |
1424 | if (is_a_field) | |
1425 | sym = NULL; | |
1426 | else | |
1427 | sym = lookup_symbol (tmp, expression_context_block, | |
176620f1 | 1428 | VAR_DOMAIN, |
9819c6c8 PM |
1429 | &is_a_field_of_this, |
1430 | (struct symtab **) NULL); | |
94a716bf | 1431 | /* second chance uppercased (as Free Pascal does). */ |
9819c6c8 | 1432 | if (!sym && !is_a_field_of_this && !is_a_field) |
373a8247 | 1433 | { |
94a716bf | 1434 | for (i = 0; i <= namelen; i++) |
373a8247 | 1435 | { |
94a716bf | 1436 | if ((tmp[i] >= 'a' && tmp[i] <= 'z')) |
373a8247 | 1437 | tmp[i] -= ('a'-'A'); |
373a8247 | 1438 | } |
9819c6c8 PM |
1439 | if (search_field && current_type) |
1440 | is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL); | |
1441 | if (is_a_field) | |
1442 | sym = NULL; | |
1443 | else | |
1444 | sym = lookup_symbol (tmp, expression_context_block, | |
176620f1 | 1445 | VAR_DOMAIN, |
94a716bf PM |
1446 | &is_a_field_of_this, |
1447 | (struct symtab **) NULL); | |
9819c6c8 | 1448 | if (sym || is_a_field_of_this || is_a_field) |
94a716bf PM |
1449 | for (i = 0; i <= namelen; i++) |
1450 | { | |
1451 | if ((tokstart[i] >= 'a' && tokstart[i] <= 'z')) | |
1452 | tokstart[i] -= ('a'-'A'); | |
1453 | } | |
1454 | } | |
1455 | /* Third chance Capitalized (as GPC does). */ | |
9819c6c8 | 1456 | if (!sym && !is_a_field_of_this && !is_a_field) |
94a716bf PM |
1457 | { |
1458 | for (i = 0; i <= namelen; i++) | |
1459 | { | |
1460 | if (i == 0) | |
1461 | { | |
1462 | if ((tmp[i] >= 'a' && tmp[i] <= 'z')) | |
1463 | tmp[i] -= ('a'-'A'); | |
1464 | } | |
1465 | else | |
1466 | if ((tmp[i] >= 'A' && tmp[i] <= 'Z')) | |
1467 | tmp[i] -= ('A'-'a'); | |
1468 | } | |
9819c6c8 PM |
1469 | if (search_field && current_type) |
1470 | is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL); | |
1471 | if (is_a_field) | |
1472 | sym = NULL; | |
1473 | else | |
1474 | sym = lookup_symbol (tmp, expression_context_block, | |
176620f1 | 1475 | VAR_DOMAIN, |
94a716bf PM |
1476 | &is_a_field_of_this, |
1477 | (struct symtab **) NULL); | |
9819c6c8 | 1478 | if (sym || is_a_field_of_this || is_a_field) |
94a716bf PM |
1479 | for (i = 0; i <= namelen; i++) |
1480 | { | |
1481 | if (i == 0) | |
1482 | { | |
1483 | if ((tokstart[i] >= 'a' && tokstart[i] <= 'z')) | |
1484 | tokstart[i] -= ('a'-'A'); | |
1485 | } | |
1486 | else | |
1487 | if ((tokstart[i] >= 'A' && tokstart[i] <= 'Z')) | |
1488 | tokstart[i] -= ('A'-'a'); | |
1489 | } | |
373a8247 | 1490 | } |
9819c6c8 PM |
1491 | |
1492 | if (is_a_field) | |
1493 | { | |
1494 | tempbuf = (char *) realloc (tempbuf, namelen + 1); | |
1495 | strncpy (tempbuf, tokstart, namelen); tempbuf [namelen] = 0; | |
1496 | yylval.sval.ptr = tempbuf; | |
1497 | yylval.sval.length = namelen; | |
1498 | return FIELDNAME; | |
1499 | } | |
373a8247 PM |
1500 | /* Call lookup_symtab, not lookup_partial_symtab, in case there are |
1501 | no psymtabs (coff, xcoff, or some future change to blow away the | |
1502 | psymtabs once once symbols are read). */ | |
1503 | if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) || | |
1504 | lookup_symtab (tmp)) | |
1505 | { | |
1506 | yylval.ssym.sym = sym; | |
1507 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1508 | return BLOCKNAME; | |
1509 | } | |
1510 | if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
1511 | { | |
1512 | #if 1 | |
1513 | /* Despite the following flaw, we need to keep this code enabled. | |
1514 | Because we can get called from check_stub_method, if we don't | |
1515 | handle nested types then it screws many operations in any | |
1516 | program which uses nested types. */ | |
1517 | /* In "A::x", if x is a member function of A and there happens | |
1518 | to be a type (nested or not, since the stabs don't make that | |
1519 | distinction) named x, then this code incorrectly thinks we | |
1520 | are dealing with nested types rather than a member function. */ | |
1521 | ||
1522 | char *p; | |
1523 | char *namestart; | |
1524 | struct symbol *best_sym; | |
1525 | ||
1526 | /* Look ahead to detect nested types. This probably should be | |
1527 | done in the grammar, but trying seemed to introduce a lot | |
1528 | of shift/reduce and reduce/reduce conflicts. It's possible | |
1529 | that it could be done, though. Or perhaps a non-grammar, but | |
1530 | less ad hoc, approach would work well. */ | |
1531 | ||
1532 | /* Since we do not currently have any way of distinguishing | |
1533 | a nested type from a non-nested one (the stabs don't tell | |
1534 | us whether a type is nested), we just ignore the | |
1535 | containing type. */ | |
1536 | ||
1537 | p = lexptr; | |
1538 | best_sym = sym; | |
1539 | while (1) | |
1540 | { | |
1541 | /* Skip whitespace. */ | |
1542 | while (*p == ' ' || *p == '\t' || *p == '\n') | |
1543 | ++p; | |
1544 | if (*p == ':' && p[1] == ':') | |
1545 | { | |
1546 | /* Skip the `::'. */ | |
1547 | p += 2; | |
1548 | /* Skip whitespace. */ | |
1549 | while (*p == ' ' || *p == '\t' || *p == '\n') | |
1550 | ++p; | |
1551 | namestart = p; | |
1552 | while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9') | |
1553 | || (*p >= 'a' && *p <= 'z') | |
1554 | || (*p >= 'A' && *p <= 'Z')) | |
1555 | ++p; | |
1556 | if (p != namestart) | |
1557 | { | |
1558 | struct symbol *cur_sym; | |
1559 | /* As big as the whole rest of the expression, which is | |
1560 | at least big enough. */ | |
1561 | char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3); | |
1562 | char *tmp1; | |
1563 | ||
1564 | tmp1 = ncopy; | |
1565 | memcpy (tmp1, tmp, strlen (tmp)); | |
1566 | tmp1 += strlen (tmp); | |
1567 | memcpy (tmp1, "::", 2); | |
1568 | tmp1 += 2; | |
1569 | memcpy (tmp1, namestart, p - namestart); | |
1570 | tmp1[p - namestart] = '\0'; | |
1571 | cur_sym = lookup_symbol (ncopy, expression_context_block, | |
176620f1 | 1572 | VAR_DOMAIN, (int *) NULL, |
373a8247 PM |
1573 | (struct symtab **) NULL); |
1574 | if (cur_sym) | |
1575 | { | |
1576 | if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF) | |
1577 | { | |
1578 | best_sym = cur_sym; | |
1579 | lexptr = p; | |
1580 | } | |
1581 | else | |
1582 | break; | |
1583 | } | |
1584 | else | |
1585 | break; | |
1586 | } | |
1587 | else | |
1588 | break; | |
1589 | } | |
1590 | else | |
1591 | break; | |
1592 | } | |
1593 | ||
1594 | yylval.tsym.type = SYMBOL_TYPE (best_sym); | |
1595 | #else /* not 0 */ | |
1596 | yylval.tsym.type = SYMBOL_TYPE (sym); | |
1597 | #endif /* not 0 */ | |
1598 | return TYPENAME; | |
1599 | } | |
54a5b07d AC |
1600 | yylval.tsym.type |
1601 | = language_lookup_primitive_type_by_name (current_language, | |
1602 | current_gdbarch, tmp); | |
1603 | if (yylval.tsym.type != NULL) | |
1604 | return TYPENAME; | |
373a8247 PM |
1605 | |
1606 | /* Input names that aren't symbols but ARE valid hex numbers, | |
1607 | when the input radix permits them, can be names or numbers | |
1608 | depending on the parse. Note we support radixes > 16 here. */ | |
1609 | if (!sym && | |
1610 | ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) || | |
1611 | (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))) | |
1612 | { | |
1613 | YYSTYPE newlval; /* Its value is ignored. */ | |
1614 | hextype = parse_number (tokstart, namelen, 0, &newlval); | |
1615 | if (hextype == INT) | |
1616 | { | |
1617 | yylval.ssym.sym = sym; | |
1618 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1619 | return NAME_OR_INT; | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | free(uptokstart); | |
1624 | /* Any other kind of symbol */ | |
1625 | yylval.ssym.sym = sym; | |
1626 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1627 | return NAME; | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | void | |
1632 | yyerror (msg) | |
1633 | char *msg; | |
1634 | { | |
24467a86 PM |
1635 | if (prev_lexptr) |
1636 | lexptr = prev_lexptr; | |
1637 | ||
373a8247 PM |
1638 | error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr); |
1639 | } |