Commit | Line | Data |
---|---|---|
c906108c | 1 | /* YACC parser for C expressions, for GDB. |
197e01b6 | 2 | Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
9b254dd1 DJ |
3 | 1998, 1999, 2000, 2003, 2004, 2006, 2007, 2008 |
4 | Free Software Foundation, Inc. | |
c906108c SS |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
22 | |
23 | /* Parse a C 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. | |
31 | ||
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. */ | |
39 | ||
40 | %{ | |
41 | ||
42 | #include "defs.h" | |
43 | #include "gdb_string.h" | |
44 | #include <ctype.h> | |
45 | #include "expression.h" | |
46 | #include "value.h" | |
47 | #include "parser-defs.h" | |
48 | #include "language.h" | |
49 | #include "c-lang.h" | |
50 | #include "bfd.h" /* Required by objfiles.h. */ | |
51 | #include "symfile.h" /* Required by objfiles.h. */ | |
52 | #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */ | |
234b45d4 | 53 | #include "charset.h" |
fe898f56 | 54 | #include "block.h" |
79c2c32d | 55 | #include "cp-support.h" |
27bc4d80 | 56 | #include "dfp.h" |
c906108c | 57 | |
3e79cecf UW |
58 | #define parse_type builtin_type (parse_gdbarch) |
59 | ||
c906108c SS |
60 | /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc), |
61 | as well as gratuitiously global symbol names, so we can have multiple | |
62 | yacc generated parsers in gdb. Note that these are only the variables | |
63 | produced by yacc. If other parser generators (bison, byacc, etc) produce | |
64 | additional global names that conflict at link time, then those parser | |
65 | generators need to be fixed instead of adding those names to this list. */ | |
66 | ||
67 | #define yymaxdepth c_maxdepth | |
65d12d83 | 68 | #define yyparse c_parse_internal |
c906108c SS |
69 | #define yylex c_lex |
70 | #define yyerror c_error | |
71 | #define yylval c_lval | |
72 | #define yychar c_char | |
73 | #define yydebug c_debug | |
74 | #define yypact c_pact | |
75 | #define yyr1 c_r1 | |
76 | #define yyr2 c_r2 | |
77 | #define yydef c_def | |
78 | #define yychk c_chk | |
79 | #define yypgo c_pgo | |
80 | #define yyact c_act | |
81 | #define yyexca c_exca | |
82 | #define yyerrflag c_errflag | |
83 | #define yynerrs c_nerrs | |
84 | #define yyps c_ps | |
85 | #define yypv c_pv | |
86 | #define yys c_s | |
87 | #define yy_yys c_yys | |
88 | #define yystate c_state | |
89 | #define yytmp c_tmp | |
90 | #define yyv c_v | |
91 | #define yy_yyv c_yyv | |
92 | #define yyval c_val | |
93 | #define yylloc c_lloc | |
94 | #define yyreds c_reds /* With YYDEBUG defined */ | |
95 | #define yytoks c_toks /* With YYDEBUG defined */ | |
06891d83 JT |
96 | #define yyname c_name /* With YYDEBUG defined */ |
97 | #define yyrule c_rule /* With YYDEBUG defined */ | |
c906108c SS |
98 | #define yylhs c_yylhs |
99 | #define yylen c_yylen | |
100 | #define yydefred c_yydefred | |
101 | #define yydgoto c_yydgoto | |
102 | #define yysindex c_yysindex | |
103 | #define yyrindex c_yyrindex | |
104 | #define yygindex c_yygindex | |
105 | #define yytable c_yytable | |
106 | #define yycheck c_yycheck | |
107 | ||
108 | #ifndef YYDEBUG | |
f461f5cf | 109 | #define YYDEBUG 1 /* Default to yydebug support */ |
c906108c SS |
110 | #endif |
111 | ||
f461f5cf PM |
112 | #define YYFPRINTF parser_fprintf |
113 | ||
a14ed312 | 114 | int yyparse (void); |
c906108c | 115 | |
a14ed312 | 116 | static int yylex (void); |
c906108c | 117 | |
a14ed312 | 118 | void yyerror (char *); |
c906108c SS |
119 | |
120 | %} | |
121 | ||
122 | /* Although the yacc "value" of an expression is not used, | |
123 | since the result is stored in the structure being created, | |
124 | other node types do have values. */ | |
125 | ||
126 | %union | |
127 | { | |
128 | LONGEST lval; | |
129 | struct { | |
130 | LONGEST val; | |
131 | struct type *type; | |
132 | } typed_val_int; | |
133 | struct { | |
134 | DOUBLEST dval; | |
135 | struct type *type; | |
136 | } typed_val_float; | |
27bc4d80 TJB |
137 | struct { |
138 | gdb_byte val[16]; | |
139 | struct type *type; | |
140 | } typed_val_decfloat; | |
c906108c SS |
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 */ | |
a14ed312 | 157 | static int parse_number (char *, int, int, YYSTYPE *); |
c906108c SS |
158 | %} |
159 | ||
160 | %type <voidval> exp exp1 type_exp start variable qualified_name lcurly | |
161 | %type <lval> rcurly | |
79c2c32d | 162 | %type <tval> type typebase qualified_type |
c906108c SS |
163 | %type <tvec> nonempty_typelist |
164 | /* %type <bval> block */ | |
165 | ||
166 | /* Fancy type parsing. */ | |
167 | %type <voidval> func_mod direct_abs_decl abs_decl | |
168 | %type <tval> ptype | |
169 | %type <lval> array_mod | |
170 | ||
171 | %token <typed_val_int> INT | |
172 | %token <typed_val_float> FLOAT | |
27bc4d80 | 173 | %token <typed_val_decfloat> DECFLOAT |
c906108c SS |
174 | |
175 | /* Both NAME and TYPENAME tokens represent symbols in the input, | |
176 | and both convey their data as strings. | |
177 | But a TYPENAME is a string that happens to be defined as a typedef | |
178 | or builtin type name (such as int or char) | |
179 | and a NAME is any other symbol. | |
180 | Contexts where this distinction is not important can use the | |
181 | nonterminal "name", which matches either NAME or TYPENAME. */ | |
182 | ||
183 | %token <sval> STRING | |
184 | %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */ | |
65d12d83 | 185 | %token <voidval> COMPLETE |
c906108c SS |
186 | %token <tsym> TYPENAME |
187 | %type <sval> name | |
188 | %type <ssym> name_not_typename | |
189 | %type <tsym> typename | |
190 | ||
191 | /* A NAME_OR_INT is a symbol which is not known in the symbol table, | |
192 | but which would parse as a valid number in the current input radix. | |
193 | E.g. "c" when input_radix==16. Depending on the parse, it will be | |
194 | turned into a name or into a number. */ | |
195 | ||
196 | %token <ssym> NAME_OR_INT | |
197 | ||
198 | %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON | |
199 | %token TEMPLATE | |
200 | %token ERROR | |
201 | ||
202 | /* Special type cases, put in to allow the parser to distinguish different | |
203 | legal basetypes. */ | |
204 | %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD | |
205 | ||
206 | %token <voidval> VARIABLE | |
207 | ||
208 | %token <opcode> ASSIGN_MODIFY | |
209 | ||
210 | /* C++ */ | |
c906108c SS |
211 | %token TRUEKEYWORD |
212 | %token FALSEKEYWORD | |
213 | ||
214 | ||
215 | %left ',' | |
216 | %left ABOVE_COMMA | |
217 | %right '=' ASSIGN_MODIFY | |
218 | %right '?' | |
219 | %left OROR | |
220 | %left ANDAND | |
221 | %left '|' | |
222 | %left '^' | |
223 | %left '&' | |
224 | %left EQUAL NOTEQUAL | |
225 | %left '<' '>' LEQ GEQ | |
226 | %left LSH RSH | |
227 | %left '@' | |
228 | %left '+' '-' | |
229 | %left '*' '/' '%' | |
230 | %right UNARY INCREMENT DECREMENT | |
231 | %right ARROW '.' '[' '(' | |
232 | %token <ssym> BLOCKNAME | |
233 | %token <bval> FILENAME | |
234 | %type <bval> block | |
235 | %left COLONCOLON | |
236 | ||
237 | \f | |
238 | %% | |
239 | ||
240 | start : exp1 | |
241 | | type_exp | |
242 | ; | |
243 | ||
244 | type_exp: type | |
245 | { write_exp_elt_opcode(OP_TYPE); | |
246 | write_exp_elt_type($1); | |
247 | write_exp_elt_opcode(OP_TYPE);} | |
248 | ; | |
249 | ||
250 | /* Expressions, including the comma operator. */ | |
251 | exp1 : exp | |
252 | | exp1 ',' exp | |
253 | { write_exp_elt_opcode (BINOP_COMMA); } | |
254 | ; | |
255 | ||
256 | /* Expressions, not including the comma operator. */ | |
257 | exp : '*' exp %prec UNARY | |
258 | { write_exp_elt_opcode (UNOP_IND); } | |
ef944135 | 259 | ; |
c906108c SS |
260 | |
261 | exp : '&' exp %prec UNARY | |
262 | { write_exp_elt_opcode (UNOP_ADDR); } | |
ef944135 | 263 | ; |
c906108c SS |
264 | |
265 | exp : '-' exp %prec UNARY | |
266 | { write_exp_elt_opcode (UNOP_NEG); } | |
267 | ; | |
268 | ||
36e9969c NS |
269 | exp : '+' exp %prec UNARY |
270 | { write_exp_elt_opcode (UNOP_PLUS); } | |
271 | ; | |
272 | ||
c906108c SS |
273 | exp : '!' exp %prec UNARY |
274 | { write_exp_elt_opcode (UNOP_LOGICAL_NOT); } | |
275 | ; | |
276 | ||
277 | exp : '~' exp %prec UNARY | |
278 | { write_exp_elt_opcode (UNOP_COMPLEMENT); } | |
279 | ; | |
280 | ||
281 | exp : INCREMENT exp %prec UNARY | |
282 | { write_exp_elt_opcode (UNOP_PREINCREMENT); } | |
283 | ; | |
284 | ||
285 | exp : DECREMENT exp %prec UNARY | |
286 | { write_exp_elt_opcode (UNOP_PREDECREMENT); } | |
287 | ; | |
288 | ||
289 | exp : exp INCREMENT %prec UNARY | |
290 | { write_exp_elt_opcode (UNOP_POSTINCREMENT); } | |
291 | ; | |
292 | ||
293 | exp : exp DECREMENT %prec UNARY | |
294 | { write_exp_elt_opcode (UNOP_POSTDECREMENT); } | |
295 | ; | |
296 | ||
297 | exp : SIZEOF exp %prec UNARY | |
298 | { write_exp_elt_opcode (UNOP_SIZEOF); } | |
299 | ; | |
300 | ||
301 | exp : exp ARROW name | |
302 | { write_exp_elt_opcode (STRUCTOP_PTR); | |
303 | write_exp_string ($3); | |
304 | write_exp_elt_opcode (STRUCTOP_PTR); } | |
305 | ; | |
306 | ||
65d12d83 TT |
307 | exp : exp ARROW name COMPLETE |
308 | { mark_struct_expression (); | |
309 | write_exp_elt_opcode (STRUCTOP_PTR); | |
310 | write_exp_string ($3); | |
311 | write_exp_elt_opcode (STRUCTOP_PTR); } | |
312 | ; | |
313 | ||
314 | exp : exp ARROW COMPLETE | |
315 | { struct stoken s; | |
316 | mark_struct_expression (); | |
317 | write_exp_elt_opcode (STRUCTOP_PTR); | |
318 | s.ptr = ""; | |
319 | s.length = 0; | |
320 | write_exp_string (s); | |
321 | write_exp_elt_opcode (STRUCTOP_PTR); } | |
322 | ; | |
323 | ||
c906108c SS |
324 | exp : exp ARROW qualified_name |
325 | { /* exp->type::name becomes exp->*(&type::name) */ | |
326 | /* Note: this doesn't work if name is a | |
327 | static member! FIXME */ | |
328 | write_exp_elt_opcode (UNOP_ADDR); | |
329 | write_exp_elt_opcode (STRUCTOP_MPTR); } | |
330 | ; | |
331 | ||
332 | exp : exp ARROW '*' exp | |
333 | { write_exp_elt_opcode (STRUCTOP_MPTR); } | |
334 | ; | |
335 | ||
336 | exp : exp '.' name | |
337 | { write_exp_elt_opcode (STRUCTOP_STRUCT); | |
338 | write_exp_string ($3); | |
339 | write_exp_elt_opcode (STRUCTOP_STRUCT); } | |
340 | ; | |
341 | ||
65d12d83 TT |
342 | exp : exp '.' name COMPLETE |
343 | { mark_struct_expression (); | |
344 | write_exp_elt_opcode (STRUCTOP_STRUCT); | |
345 | write_exp_string ($3); | |
346 | write_exp_elt_opcode (STRUCTOP_STRUCT); } | |
347 | ; | |
348 | ||
349 | exp : exp '.' COMPLETE | |
350 | { struct stoken s; | |
351 | mark_struct_expression (); | |
352 | write_exp_elt_opcode (STRUCTOP_STRUCT); | |
353 | s.ptr = ""; | |
354 | s.length = 0; | |
355 | write_exp_string (s); | |
356 | write_exp_elt_opcode (STRUCTOP_STRUCT); } | |
357 | ; | |
358 | ||
c906108c SS |
359 | exp : exp '.' qualified_name |
360 | { /* exp.type::name becomes exp.*(&type::name) */ | |
361 | /* Note: this doesn't work if name is a | |
362 | static member! FIXME */ | |
363 | write_exp_elt_opcode (UNOP_ADDR); | |
364 | write_exp_elt_opcode (STRUCTOP_MEMBER); } | |
365 | ; | |
366 | ||
367 | exp : exp '.' '*' exp | |
368 | { write_exp_elt_opcode (STRUCTOP_MEMBER); } | |
369 | ; | |
370 | ||
371 | exp : exp '[' exp1 ']' | |
372 | { write_exp_elt_opcode (BINOP_SUBSCRIPT); } | |
373 | ; | |
374 | ||
375 | exp : exp '(' | |
376 | /* This is to save the value of arglist_len | |
377 | being accumulated by an outer function call. */ | |
378 | { start_arglist (); } | |
379 | arglist ')' %prec ARROW | |
380 | { write_exp_elt_opcode (OP_FUNCALL); | |
381 | write_exp_elt_longcst ((LONGEST) end_arglist ()); | |
382 | write_exp_elt_opcode (OP_FUNCALL); } | |
383 | ; | |
384 | ||
385 | lcurly : '{' | |
386 | { start_arglist (); } | |
387 | ; | |
388 | ||
389 | arglist : | |
390 | ; | |
391 | ||
392 | arglist : exp | |
393 | { arglist_len = 1; } | |
394 | ; | |
395 | ||
396 | arglist : arglist ',' exp %prec ABOVE_COMMA | |
397 | { arglist_len++; } | |
398 | ; | |
399 | ||
400 | rcurly : '}' | |
401 | { $$ = end_arglist () - 1; } | |
402 | ; | |
403 | exp : lcurly arglist rcurly %prec ARROW | |
404 | { write_exp_elt_opcode (OP_ARRAY); | |
405 | write_exp_elt_longcst ((LONGEST) 0); | |
406 | write_exp_elt_longcst ((LONGEST) $3); | |
407 | write_exp_elt_opcode (OP_ARRAY); } | |
408 | ; | |
409 | ||
410 | exp : lcurly type rcurly exp %prec UNARY | |
411 | { write_exp_elt_opcode (UNOP_MEMVAL); | |
412 | write_exp_elt_type ($2); | |
413 | write_exp_elt_opcode (UNOP_MEMVAL); } | |
414 | ; | |
415 | ||
416 | exp : '(' type ')' exp %prec UNARY | |
417 | { write_exp_elt_opcode (UNOP_CAST); | |
418 | write_exp_elt_type ($2); | |
419 | write_exp_elt_opcode (UNOP_CAST); } | |
420 | ; | |
421 | ||
422 | exp : '(' exp1 ')' | |
423 | { } | |
424 | ; | |
425 | ||
426 | /* Binary operators in order of decreasing precedence. */ | |
427 | ||
428 | exp : exp '@' exp | |
429 | { write_exp_elt_opcode (BINOP_REPEAT); } | |
430 | ; | |
431 | ||
432 | exp : exp '*' exp | |
433 | { write_exp_elt_opcode (BINOP_MUL); } | |
434 | ; | |
435 | ||
436 | exp : exp '/' exp | |
437 | { write_exp_elt_opcode (BINOP_DIV); } | |
438 | ; | |
439 | ||
440 | exp : exp '%' exp | |
441 | { write_exp_elt_opcode (BINOP_REM); } | |
442 | ; | |
443 | ||
444 | exp : exp '+' exp | |
445 | { write_exp_elt_opcode (BINOP_ADD); } | |
446 | ; | |
447 | ||
448 | exp : exp '-' exp | |
449 | { write_exp_elt_opcode (BINOP_SUB); } | |
450 | ; | |
451 | ||
452 | exp : exp LSH exp | |
453 | { write_exp_elt_opcode (BINOP_LSH); } | |
454 | ; | |
455 | ||
456 | exp : exp RSH exp | |
457 | { write_exp_elt_opcode (BINOP_RSH); } | |
458 | ; | |
459 | ||
460 | exp : exp EQUAL exp | |
461 | { write_exp_elt_opcode (BINOP_EQUAL); } | |
462 | ; | |
463 | ||
464 | exp : exp NOTEQUAL exp | |
465 | { write_exp_elt_opcode (BINOP_NOTEQUAL); } | |
466 | ; | |
467 | ||
468 | exp : exp LEQ exp | |
469 | { write_exp_elt_opcode (BINOP_LEQ); } | |
470 | ; | |
471 | ||
472 | exp : exp GEQ exp | |
473 | { write_exp_elt_opcode (BINOP_GEQ); } | |
474 | ; | |
475 | ||
476 | exp : exp '<' exp | |
477 | { write_exp_elt_opcode (BINOP_LESS); } | |
478 | ; | |
479 | ||
480 | exp : exp '>' exp | |
481 | { write_exp_elt_opcode (BINOP_GTR); } | |
482 | ; | |
483 | ||
484 | exp : exp '&' exp | |
485 | { write_exp_elt_opcode (BINOP_BITWISE_AND); } | |
486 | ; | |
487 | ||
488 | exp : exp '^' exp | |
489 | { write_exp_elt_opcode (BINOP_BITWISE_XOR); } | |
490 | ; | |
491 | ||
492 | exp : exp '|' exp | |
493 | { write_exp_elt_opcode (BINOP_BITWISE_IOR); } | |
494 | ; | |
495 | ||
496 | exp : exp ANDAND exp | |
497 | { write_exp_elt_opcode (BINOP_LOGICAL_AND); } | |
498 | ; | |
499 | ||
500 | exp : exp OROR exp | |
501 | { write_exp_elt_opcode (BINOP_LOGICAL_OR); } | |
502 | ; | |
503 | ||
504 | exp : exp '?' exp ':' exp %prec '?' | |
505 | { write_exp_elt_opcode (TERNOP_COND); } | |
506 | ; | |
507 | ||
508 | exp : exp '=' exp | |
509 | { write_exp_elt_opcode (BINOP_ASSIGN); } | |
510 | ; | |
511 | ||
512 | exp : exp ASSIGN_MODIFY exp | |
513 | { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); | |
514 | write_exp_elt_opcode ($2); | |
515 | write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); } | |
516 | ; | |
517 | ||
518 | exp : INT | |
519 | { write_exp_elt_opcode (OP_LONG); | |
520 | write_exp_elt_type ($1.type); | |
521 | write_exp_elt_longcst ((LONGEST)($1.val)); | |
522 | write_exp_elt_opcode (OP_LONG); } | |
523 | ; | |
524 | ||
525 | exp : NAME_OR_INT | |
526 | { YYSTYPE val; | |
527 | parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val); | |
528 | write_exp_elt_opcode (OP_LONG); | |
529 | write_exp_elt_type (val.typed_val_int.type); | |
530 | write_exp_elt_longcst ((LONGEST)val.typed_val_int.val); | |
531 | write_exp_elt_opcode (OP_LONG); | |
532 | } | |
533 | ; | |
534 | ||
535 | ||
536 | exp : FLOAT | |
537 | { write_exp_elt_opcode (OP_DOUBLE); | |
538 | write_exp_elt_type ($1.type); | |
539 | write_exp_elt_dblcst ($1.dval); | |
540 | write_exp_elt_opcode (OP_DOUBLE); } | |
541 | ; | |
542 | ||
27bc4d80 TJB |
543 | exp : DECFLOAT |
544 | { write_exp_elt_opcode (OP_DECFLOAT); | |
545 | write_exp_elt_type ($1.type); | |
546 | write_exp_elt_decfloatcst ($1.val); | |
547 | write_exp_elt_opcode (OP_DECFLOAT); } | |
548 | ; | |
549 | ||
c906108c SS |
550 | exp : variable |
551 | ; | |
552 | ||
553 | exp : VARIABLE | |
554 | /* Already written by write_dollar_variable. */ | |
555 | ; | |
556 | ||
557 | exp : SIZEOF '(' type ')' %prec UNARY | |
558 | { write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 559 | write_exp_elt_type (parse_type->builtin_int); |
c906108c SS |
560 | CHECK_TYPEDEF ($3); |
561 | write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3)); | |
562 | write_exp_elt_opcode (OP_LONG); } | |
563 | ; | |
564 | ||
565 | exp : STRING | |
566 | { /* C strings are converted into array constants with | |
567 | an explicit null byte added at the end. Thus | |
568 | the array upper bound is the string length. | |
569 | There is no such thing in C as a completely empty | |
570 | string. */ | |
571 | char *sp = $1.ptr; int count = $1.length; | |
572 | while (count-- > 0) | |
573 | { | |
574 | write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 575 | write_exp_elt_type (parse_type->builtin_char); |
c906108c SS |
576 | write_exp_elt_longcst ((LONGEST)(*sp++)); |
577 | write_exp_elt_opcode (OP_LONG); | |
578 | } | |
579 | write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 580 | write_exp_elt_type (parse_type->builtin_char); |
c906108c SS |
581 | write_exp_elt_longcst ((LONGEST)'\0'); |
582 | write_exp_elt_opcode (OP_LONG); | |
583 | write_exp_elt_opcode (OP_ARRAY); | |
584 | write_exp_elt_longcst ((LONGEST) 0); | |
585 | write_exp_elt_longcst ((LONGEST) ($1.length)); | |
586 | write_exp_elt_opcode (OP_ARRAY); } | |
587 | ; | |
588 | ||
589 | /* C++. */ | |
c906108c SS |
590 | exp : TRUEKEYWORD |
591 | { write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 592 | write_exp_elt_type (parse_type->builtin_bool); |
c906108c SS |
593 | write_exp_elt_longcst ((LONGEST) 1); |
594 | write_exp_elt_opcode (OP_LONG); } | |
595 | ; | |
596 | ||
597 | exp : FALSEKEYWORD | |
598 | { write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 599 | write_exp_elt_type (parse_type->builtin_bool); |
c906108c SS |
600 | write_exp_elt_longcst ((LONGEST) 0); |
601 | write_exp_elt_opcode (OP_LONG); } | |
602 | ; | |
603 | ||
604 | /* end of C++. */ | |
605 | ||
606 | block : BLOCKNAME | |
607 | { | |
608 | if ($1.sym) | |
609 | $$ = SYMBOL_BLOCK_VALUE ($1.sym); | |
610 | else | |
611 | error ("No file or function \"%s\".", | |
612 | copy_name ($1.stoken)); | |
613 | } | |
614 | | FILENAME | |
615 | { | |
616 | $$ = $1; | |
617 | } | |
618 | ; | |
619 | ||
620 | block : block COLONCOLON name | |
621 | { struct symbol *tem | |
622 | = lookup_symbol (copy_name ($3), $1, | |
2570f2b7 | 623 | VAR_DOMAIN, (int *) NULL); |
c906108c SS |
624 | if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK) |
625 | error ("No function \"%s\" in specified context.", | |
626 | copy_name ($3)); | |
627 | $$ = SYMBOL_BLOCK_VALUE (tem); } | |
628 | ; | |
629 | ||
630 | variable: block COLONCOLON name | |
631 | { struct symbol *sym; | |
632 | sym = lookup_symbol (copy_name ($3), $1, | |
2570f2b7 | 633 | VAR_DOMAIN, (int *) NULL); |
c906108c SS |
634 | if (sym == 0) |
635 | error ("No symbol \"%s\" in specified context.", | |
636 | copy_name ($3)); | |
637 | ||
638 | write_exp_elt_opcode (OP_VAR_VALUE); | |
639 | /* block_found is set by lookup_symbol. */ | |
640 | write_exp_elt_block (block_found); | |
641 | write_exp_elt_sym (sym); | |
642 | write_exp_elt_opcode (OP_VAR_VALUE); } | |
643 | ; | |
644 | ||
645 | qualified_name: typebase COLONCOLON name | |
646 | { | |
647 | struct type *type = $1; | |
648 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
79c2c32d DC |
649 | && TYPE_CODE (type) != TYPE_CODE_UNION |
650 | && TYPE_CODE (type) != TYPE_CODE_NAMESPACE) | |
c906108c SS |
651 | error ("`%s' is not defined as an aggregate type.", |
652 | TYPE_NAME (type)); | |
653 | ||
654 | write_exp_elt_opcode (OP_SCOPE); | |
655 | write_exp_elt_type (type); | |
656 | write_exp_string ($3); | |
657 | write_exp_elt_opcode (OP_SCOPE); | |
658 | } | |
659 | | typebase COLONCOLON '~' name | |
660 | { | |
661 | struct type *type = $1; | |
662 | struct stoken tmp_token; | |
663 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
79c2c32d DC |
664 | && TYPE_CODE (type) != TYPE_CODE_UNION |
665 | && TYPE_CODE (type) != TYPE_CODE_NAMESPACE) | |
c906108c SS |
666 | error ("`%s' is not defined as an aggregate type.", |
667 | TYPE_NAME (type)); | |
668 | ||
669 | tmp_token.ptr = (char*) alloca ($4.length + 2); | |
670 | tmp_token.length = $4.length + 1; | |
671 | tmp_token.ptr[0] = '~'; | |
672 | memcpy (tmp_token.ptr+1, $4.ptr, $4.length); | |
673 | tmp_token.ptr[tmp_token.length] = 0; | |
674 | ||
675 | /* Check for valid destructor name. */ | |
676 | destructor_name_p (tmp_token.ptr, type); | |
677 | write_exp_elt_opcode (OP_SCOPE); | |
678 | write_exp_elt_type (type); | |
679 | write_exp_string (tmp_token); | |
680 | write_exp_elt_opcode (OP_SCOPE); | |
681 | } | |
682 | ; | |
683 | ||
684 | variable: qualified_name | |
685 | | COLONCOLON name | |
686 | { | |
687 | char *name = copy_name ($2); | |
688 | struct symbol *sym; | |
689 | struct minimal_symbol *msymbol; | |
690 | ||
691 | sym = | |
692 | lookup_symbol (name, (const struct block *) NULL, | |
2570f2b7 | 693 | VAR_DOMAIN, (int *) NULL); |
c906108c SS |
694 | if (sym) |
695 | { | |
696 | write_exp_elt_opcode (OP_VAR_VALUE); | |
697 | write_exp_elt_block (NULL); | |
698 | write_exp_elt_sym (sym); | |
699 | write_exp_elt_opcode (OP_VAR_VALUE); | |
700 | break; | |
701 | } | |
702 | ||
703 | msymbol = lookup_minimal_symbol (name, NULL, NULL); | |
704 | if (msymbol != NULL) | |
c841afd5 UW |
705 | write_exp_msymbol (msymbol); |
706 | else if (!have_full_symbols () && !have_partial_symbols ()) | |
707 | error ("No symbol table is loaded. Use the \"file\" command."); | |
c906108c | 708 | else |
c841afd5 | 709 | error ("No symbol \"%s\" in current context.", name); |
c906108c SS |
710 | } |
711 | ; | |
712 | ||
713 | variable: name_not_typename | |
714 | { struct symbol *sym = $1.sym; | |
715 | ||
716 | if (sym) | |
717 | { | |
718 | if (symbol_read_needs_frame (sym)) | |
719 | { | |
720 | if (innermost_block == 0 || | |
721 | contained_in (block_found, | |
722 | innermost_block)) | |
723 | innermost_block = block_found; | |
724 | } | |
725 | ||
726 | write_exp_elt_opcode (OP_VAR_VALUE); | |
727 | /* We want to use the selected frame, not | |
728 | another more inner frame which happens to | |
729 | be in the same block. */ | |
730 | write_exp_elt_block (NULL); | |
731 | write_exp_elt_sym (sym); | |
732 | write_exp_elt_opcode (OP_VAR_VALUE); | |
733 | } | |
734 | else if ($1.is_a_field_of_this) | |
735 | { | |
736 | /* C++: it hangs off of `this'. Must | |
737 | not inadvertently convert from a method call | |
738 | to data ref. */ | |
739 | if (innermost_block == 0 || | |
740 | contained_in (block_found, innermost_block)) | |
741 | innermost_block = block_found; | |
742 | write_exp_elt_opcode (OP_THIS); | |
743 | write_exp_elt_opcode (OP_THIS); | |
744 | write_exp_elt_opcode (STRUCTOP_PTR); | |
745 | write_exp_string ($1.stoken); | |
746 | write_exp_elt_opcode (STRUCTOP_PTR); | |
747 | } | |
748 | else | |
749 | { | |
750 | struct minimal_symbol *msymbol; | |
710122da | 751 | char *arg = copy_name ($1.stoken); |
c906108c SS |
752 | |
753 | msymbol = | |
754 | lookup_minimal_symbol (arg, NULL, NULL); | |
755 | if (msymbol != NULL) | |
c841afd5 | 756 | write_exp_msymbol (msymbol); |
c906108c SS |
757 | else if (!have_full_symbols () && !have_partial_symbols ()) |
758 | error ("No symbol table is loaded. Use the \"file\" command."); | |
759 | else | |
760 | error ("No symbol \"%s\" in current context.", | |
761 | copy_name ($1.stoken)); | |
762 | } | |
763 | } | |
764 | ; | |
765 | ||
47663de5 MS |
766 | space_identifier : '@' NAME |
767 | { push_type_address_space (copy_name ($2.stoken)); | |
768 | push_type (tp_space_identifier); | |
769 | } | |
770 | ; | |
c906108c | 771 | |
47663de5 MS |
772 | const_or_volatile: const_or_volatile_noopt |
773 | | | |
c906108c | 774 | ; |
47663de5 MS |
775 | |
776 | cv_with_space_id : const_or_volatile space_identifier const_or_volatile | |
56e2d25a | 777 | ; |
47663de5 MS |
778 | |
779 | const_or_volatile_or_space_identifier_noopt: cv_with_space_id | |
780 | | const_or_volatile_noopt | |
56e2d25a | 781 | ; |
47663de5 MS |
782 | |
783 | const_or_volatile_or_space_identifier: | |
784 | const_or_volatile_or_space_identifier_noopt | |
785 | | | |
56e2d25a | 786 | ; |
47663de5 | 787 | |
c906108c SS |
788 | abs_decl: '*' |
789 | { push_type (tp_pointer); $$ = 0; } | |
790 | | '*' abs_decl | |
791 | { push_type (tp_pointer); $$ = $2; } | |
792 | | '&' | |
793 | { push_type (tp_reference); $$ = 0; } | |
794 | | '&' abs_decl | |
795 | { push_type (tp_reference); $$ = $2; } | |
796 | | direct_abs_decl | |
797 | ; | |
798 | ||
799 | direct_abs_decl: '(' abs_decl ')' | |
800 | { $$ = $2; } | |
801 | | direct_abs_decl array_mod | |
802 | { | |
803 | push_type_int ($2); | |
804 | push_type (tp_array); | |
805 | } | |
806 | | array_mod | |
807 | { | |
808 | push_type_int ($1); | |
809 | push_type (tp_array); | |
810 | $$ = 0; | |
811 | } | |
812 | ||
813 | | direct_abs_decl func_mod | |
814 | { push_type (tp_function); } | |
815 | | func_mod | |
816 | { push_type (tp_function); } | |
817 | ; | |
818 | ||
819 | array_mod: '[' ']' | |
820 | { $$ = -1; } | |
821 | | '[' INT ']' | |
822 | { $$ = $2.val; } | |
823 | ; | |
824 | ||
825 | func_mod: '(' ')' | |
826 | { $$ = 0; } | |
827 | | '(' nonempty_typelist ')' | |
8dbb1c65 | 828 | { free ($2); $$ = 0; } |
c906108c SS |
829 | ; |
830 | ||
a22229c4 | 831 | /* We used to try to recognize pointer to member types here, but |
c906108c SS |
832 | that didn't work (shift/reduce conflicts meant that these rules never |
833 | got executed). The problem is that | |
834 | int (foo::bar::baz::bizzle) | |
835 | is a function type but | |
836 | int (foo::bar::baz::bizzle::*) | |
837 | is a pointer to member type. Stroustrup loses again! */ | |
838 | ||
839 | type : ptype | |
c906108c SS |
840 | ; |
841 | ||
842 | typebase /* Implements (approximately): (type-qualifier)* type-specifier */ | |
843 | : TYPENAME | |
844 | { $$ = $1.type; } | |
845 | | INT_KEYWORD | |
3e79cecf | 846 | { $$ = parse_type->builtin_int; } |
c906108c | 847 | | LONG |
3e79cecf | 848 | { $$ = parse_type->builtin_long; } |
c906108c | 849 | | SHORT |
3e79cecf | 850 | { $$ = parse_type->builtin_short; } |
c906108c | 851 | | LONG INT_KEYWORD |
3e79cecf | 852 | { $$ = parse_type->builtin_long; } |
b2c4da81 | 853 | | LONG SIGNED_KEYWORD INT_KEYWORD |
3e79cecf | 854 | { $$ = parse_type->builtin_long; } |
b2c4da81 | 855 | | LONG SIGNED_KEYWORD |
3e79cecf | 856 | { $$ = parse_type->builtin_long; } |
b2c4da81 | 857 | | SIGNED_KEYWORD LONG INT_KEYWORD |
3e79cecf | 858 | { $$ = parse_type->builtin_long; } |
c906108c | 859 | | UNSIGNED LONG INT_KEYWORD |
3e79cecf | 860 | { $$ = parse_type->builtin_unsigned_long; } |
b2c4da81 | 861 | | LONG UNSIGNED INT_KEYWORD |
3e79cecf | 862 | { $$ = parse_type->builtin_unsigned_long; } |
b2c4da81 | 863 | | LONG UNSIGNED |
3e79cecf | 864 | { $$ = parse_type->builtin_unsigned_long; } |
c906108c | 865 | | LONG LONG |
3e79cecf | 866 | { $$ = parse_type->builtin_long_long; } |
c906108c | 867 | | LONG LONG INT_KEYWORD |
3e79cecf | 868 | { $$ = parse_type->builtin_long_long; } |
b2c4da81 | 869 | | LONG LONG SIGNED_KEYWORD INT_KEYWORD |
3e79cecf | 870 | { $$ = parse_type->builtin_long_long; } |
b2c4da81 | 871 | | LONG LONG SIGNED_KEYWORD |
3e79cecf | 872 | { $$ = parse_type->builtin_long_long; } |
b2c4da81 | 873 | | SIGNED_KEYWORD LONG LONG |
3e79cecf | 874 | { $$ = parse_type->builtin_long_long; } |
55baeb84 | 875 | | SIGNED_KEYWORD LONG LONG INT_KEYWORD |
3e79cecf | 876 | { $$ = parse_type->builtin_long_long; } |
c906108c | 877 | | UNSIGNED LONG LONG |
3e79cecf | 878 | { $$ = parse_type->builtin_unsigned_long_long; } |
c906108c | 879 | | UNSIGNED LONG LONG INT_KEYWORD |
3e79cecf | 880 | { $$ = parse_type->builtin_unsigned_long_long; } |
b2c4da81 | 881 | | LONG LONG UNSIGNED |
3e79cecf | 882 | { $$ = parse_type->builtin_unsigned_long_long; } |
b2c4da81 | 883 | | LONG LONG UNSIGNED INT_KEYWORD |
3e79cecf | 884 | { $$ = parse_type->builtin_unsigned_long_long; } |
c906108c | 885 | | SHORT INT_KEYWORD |
3e79cecf | 886 | { $$ = parse_type->builtin_short; } |
b2c4da81 | 887 | | SHORT SIGNED_KEYWORD INT_KEYWORD |
3e79cecf | 888 | { $$ = parse_type->builtin_short; } |
b2c4da81 | 889 | | SHORT SIGNED_KEYWORD |
3e79cecf | 890 | { $$ = parse_type->builtin_short; } |
c906108c | 891 | | UNSIGNED SHORT INT_KEYWORD |
3e79cecf | 892 | { $$ = parse_type->builtin_unsigned_short; } |
b2c4da81 | 893 | | SHORT UNSIGNED |
3e79cecf | 894 | { $$ = parse_type->builtin_unsigned_short; } |
b2c4da81 | 895 | | SHORT UNSIGNED INT_KEYWORD |
3e79cecf | 896 | { $$ = parse_type->builtin_unsigned_short; } |
c906108c | 897 | | DOUBLE_KEYWORD |
3e79cecf | 898 | { $$ = parse_type->builtin_double; } |
c906108c | 899 | | LONG DOUBLE_KEYWORD |
3e79cecf | 900 | { $$ = parse_type->builtin_long_double; } |
c906108c SS |
901 | | STRUCT name |
902 | { $$ = lookup_struct (copy_name ($2), | |
903 | expression_context_block); } | |
904 | | CLASS name | |
905 | { $$ = lookup_struct (copy_name ($2), | |
906 | expression_context_block); } | |
907 | | UNION name | |
908 | { $$ = lookup_union (copy_name ($2), | |
909 | expression_context_block); } | |
910 | | ENUM name | |
911 | { $$ = lookup_enum (copy_name ($2), | |
912 | expression_context_block); } | |
913 | | UNSIGNED typename | |
914 | { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); } | |
915 | | UNSIGNED | |
3e79cecf | 916 | { $$ = parse_type->builtin_unsigned_int; } |
c906108c SS |
917 | | SIGNED_KEYWORD typename |
918 | { $$ = lookup_signed_typename (TYPE_NAME($2.type)); } | |
919 | | SIGNED_KEYWORD | |
3e79cecf | 920 | { $$ = parse_type->builtin_int; } |
c906108c SS |
921 | /* It appears that this rule for templates is never |
922 | reduced; template recognition happens by lookahead | |
923 | in the token processing code in yylex. */ | |
924 | | TEMPLATE name '<' type '>' | |
925 | { $$ = lookup_template_type(copy_name($2), $4, | |
926 | expression_context_block); | |
927 | } | |
47663de5 MS |
928 | | const_or_volatile_or_space_identifier_noopt typebase |
929 | { $$ = follow_types ($2); } | |
930 | | typebase const_or_volatile_or_space_identifier_noopt | |
931 | { $$ = follow_types ($1); } | |
79c2c32d DC |
932 | | qualified_type |
933 | ; | |
934 | ||
935 | /* FIXME: carlton/2003-09-25: This next bit leads to lots of | |
936 | reduce-reduce conflicts, because the parser doesn't know whether or | |
937 | not to use qualified_name or qualified_type: the rules are | |
938 | identical. If the parser is parsing 'A::B::x', then, when it sees | |
939 | the second '::', it knows that the expression to the left of it has | |
940 | to be a type, so it uses qualified_type. But if it is parsing just | |
941 | 'A::B', then it doesn't have any way of knowing which rule to use, | |
942 | so there's a reduce-reduce conflict; it picks qualified_name, since | |
943 | that occurs earlier in this file than qualified_type. | |
944 | ||
945 | There's no good way to fix this with the grammar as it stands; as | |
946 | far as I can tell, some of the problems arise from ambiguities that | |
947 | GDB introduces ('start' can be either an expression or a type), but | |
948 | some of it is inherent to the nature of C++ (you want to treat the | |
949 | input "(FOO)" fairly differently depending on whether FOO is an | |
950 | expression or a type, and if FOO is a complex expression, this can | |
951 | be hard to determine at the right time). Fortunately, it works | |
952 | pretty well in most cases. For example, if you do 'ptype A::B', | |
953 | where A::B is a nested type, then the parser will mistakenly | |
954 | misidentify it as an expression; but evaluate_subexp will get | |
955 | called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything | |
956 | will work out anyways. But there are situations where the parser | |
957 | will get confused: the most common one that I've run into is when | |
958 | you want to do | |
959 | ||
960 | print *((A::B *) x)" | |
961 | ||
962 | where the parser doesn't realize that A::B has to be a type until | |
963 | it hits the first right paren, at which point it's too late. (The | |
964 | workaround is to type "print *(('A::B' *) x)" instead.) (And | |
965 | another solution is to fix our symbol-handling code so that the | |
966 | user never wants to type something like that in the first place, | |
967 | because we get all the types right without the user's help!) | |
968 | ||
969 | Perhaps we could fix this by making the lexer smarter. Some of | |
970 | this functionality used to be in the lexer, but in a way that | |
971 | worked even less well than the current solution: that attempt | |
972 | involved having the parser sometimes handle '::' and having the | |
973 | lexer sometimes handle it, and without a clear division of | |
974 | responsibility, it quickly degenerated into a big mess. Probably | |
975 | the eventual correct solution will give more of a role to the lexer | |
976 | (ideally via code that is shared between the lexer and | |
977 | decode_line_1), but I'm not holding my breath waiting for somebody | |
978 | to get around to cleaning this up... */ | |
979 | ||
79c2c32d DC |
980 | qualified_type: typebase COLONCOLON name |
981 | { | |
982 | struct type *type = $1; | |
983 | struct type *new_type; | |
984 | char *ncopy = alloca ($3.length + 1); | |
985 | ||
986 | memcpy (ncopy, $3.ptr, $3.length); | |
987 | ncopy[$3.length] = '\0'; | |
988 | ||
63d06c5c DC |
989 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
990 | && TYPE_CODE (type) != TYPE_CODE_UNION | |
991 | && TYPE_CODE (type) != TYPE_CODE_NAMESPACE) | |
992 | error ("`%s' is not defined as an aggregate type.", | |
79c2c32d DC |
993 | TYPE_NAME (type)); |
994 | ||
995 | new_type = cp_lookup_nested_type (type, ncopy, | |
996 | expression_context_block); | |
997 | if (new_type == NULL) | |
63d06c5c | 998 | error ("No type \"%s\" within class or namespace \"%s\".", |
79c2c32d DC |
999 | ncopy, TYPE_NAME (type)); |
1000 | ||
1001 | $$ = new_type; | |
1002 | } | |
c906108c SS |
1003 | ; |
1004 | ||
1005 | typename: TYPENAME | |
1006 | | INT_KEYWORD | |
1007 | { | |
1008 | $$.stoken.ptr = "int"; | |
1009 | $$.stoken.length = 3; | |
3e79cecf | 1010 | $$.type = parse_type->builtin_int; |
c906108c SS |
1011 | } |
1012 | | LONG | |
1013 | { | |
1014 | $$.stoken.ptr = "long"; | |
1015 | $$.stoken.length = 4; | |
3e79cecf | 1016 | $$.type = parse_type->builtin_long; |
c906108c SS |
1017 | } |
1018 | | SHORT | |
1019 | { | |
1020 | $$.stoken.ptr = "short"; | |
1021 | $$.stoken.length = 5; | |
3e79cecf | 1022 | $$.type = parse_type->builtin_short; |
c906108c SS |
1023 | } |
1024 | ; | |
1025 | ||
1026 | nonempty_typelist | |
1027 | : type | |
1028 | { $$ = (struct type **) malloc (sizeof (struct type *) * 2); | |
1029 | $<ivec>$[0] = 1; /* Number of types in vector */ | |
1030 | $$[1] = $1; | |
1031 | } | |
1032 | | nonempty_typelist ',' type | |
1033 | { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1); | |
1034 | $$ = (struct type **) realloc ((char *) $1, len); | |
1035 | $$[$<ivec>$[0]] = $3; | |
1036 | } | |
1037 | ; | |
1038 | ||
47663de5 MS |
1039 | ptype : typebase |
1040 | | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier | |
1041 | { $$ = follow_types ($1); } | |
1042 | ; | |
1043 | ||
1044 | const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD | |
1045 | | VOLATILE_KEYWORD CONST_KEYWORD | |
1046 | ; | |
1047 | ||
1048 | const_or_volatile_noopt: const_and_volatile | |
1049 | { push_type (tp_const); | |
1050 | push_type (tp_volatile); | |
1051 | } | |
1052 | | CONST_KEYWORD | |
1053 | { push_type (tp_const); } | |
1054 | | VOLATILE_KEYWORD | |
1055 | { push_type (tp_volatile); } | |
1056 | ; | |
1057 | ||
c906108c SS |
1058 | name : NAME { $$ = $1.stoken; } |
1059 | | BLOCKNAME { $$ = $1.stoken; } | |
1060 | | TYPENAME { $$ = $1.stoken; } | |
1061 | | NAME_OR_INT { $$ = $1.stoken; } | |
1062 | ; | |
1063 | ||
1064 | name_not_typename : NAME | |
1065 | | BLOCKNAME | |
1066 | /* These would be useful if name_not_typename was useful, but it is just | |
1067 | a fake for "variable", so these cause reduce/reduce conflicts because | |
1068 | the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable, | |
1069 | =exp) or just an exp. If name_not_typename was ever used in an lvalue | |
1070 | context where only a name could occur, this might be useful. | |
1071 | | NAME_OR_INT | |
1072 | */ | |
1073 | ; | |
1074 | ||
1075 | %% | |
1076 | ||
1077 | /* Take care of parsing a number (anything that starts with a digit). | |
1078 | Set yylval and return the token type; update lexptr. | |
1079 | LEN is the number of characters in it. */ | |
1080 | ||
1081 | /*** Needs some error checking for the float case ***/ | |
1082 | ||
1083 | static int | |
1084 | parse_number (p, len, parsed_float, putithere) | |
710122da DC |
1085 | char *p; |
1086 | int len; | |
c906108c SS |
1087 | int parsed_float; |
1088 | YYSTYPE *putithere; | |
1089 | { | |
1090 | /* FIXME: Shouldn't these be unsigned? We don't deal with negative values | |
1091 | here, and we do kind of silly things like cast to unsigned. */ | |
710122da DC |
1092 | LONGEST n = 0; |
1093 | LONGEST prevn = 0; | |
c906108c SS |
1094 | ULONGEST un; |
1095 | ||
710122da DC |
1096 | int i = 0; |
1097 | int c; | |
1098 | int base = input_radix; | |
c906108c SS |
1099 | int unsigned_p = 0; |
1100 | ||
1101 | /* Number of "L" suffixes encountered. */ | |
1102 | int long_p = 0; | |
1103 | ||
1104 | /* We have found a "L" or "U" suffix. */ | |
1105 | int found_suffix = 0; | |
1106 | ||
1107 | ULONGEST high_bit; | |
1108 | struct type *signed_type; | |
1109 | struct type *unsigned_type; | |
1110 | ||
1111 | if (parsed_float) | |
1112 | { | |
1113 | /* It's a float since it contains a point or an exponent. */ | |
fe9441f6 JK |
1114 | char *s; |
1115 | int num; /* number of tokens scanned by scanf */ | |
1116 | char saved_char; | |
27bc4d80 TJB |
1117 | |
1118 | /* If it ends at "df", "dd" or "dl", take it as type of decimal floating | |
1119 | point. Return DECFLOAT. */ | |
1120 | ||
fe9441f6 | 1121 | if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f') |
27bc4d80 TJB |
1122 | { |
1123 | p[len - 2] = '\0'; | |
1124 | putithere->typed_val_decfloat.type | |
3e79cecf | 1125 | = parse_type->builtin_decfloat; |
27bc4d80 | 1126 | decimal_from_string (putithere->typed_val_decfloat.val, 4, p); |
fe9441f6 JK |
1127 | p[len - 2] = 'd'; |
1128 | return DECFLOAT; | |
27bc4d80 TJB |
1129 | } |
1130 | ||
fe9441f6 | 1131 | if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd') |
27bc4d80 TJB |
1132 | { |
1133 | p[len - 2] = '\0'; | |
1134 | putithere->typed_val_decfloat.type | |
3e79cecf | 1135 | = parse_type->builtin_decdouble; |
27bc4d80 | 1136 | decimal_from_string (putithere->typed_val_decfloat.val, 8, p); |
fe9441f6 JK |
1137 | p[len - 2] = 'd'; |
1138 | return DECFLOAT; | |
27bc4d80 TJB |
1139 | } |
1140 | ||
fe9441f6 | 1141 | if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l') |
27bc4d80 TJB |
1142 | { |
1143 | p[len - 2] = '\0'; | |
1144 | putithere->typed_val_decfloat.type | |
3e79cecf | 1145 | = parse_type->builtin_declong; |
27bc4d80 | 1146 | decimal_from_string (putithere->typed_val_decfloat.val, 16, p); |
fe9441f6 JK |
1147 | p[len - 2] = 'd'; |
1148 | return DECFLOAT; | |
27bc4d80 TJB |
1149 | } |
1150 | ||
fe9441f6 JK |
1151 | s = malloc (len); |
1152 | saved_char = p[len]; | |
1153 | p[len] = 0; /* null-terminate the token */ | |
689e4e2d | 1154 | num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s", |
96c1eda2 | 1155 | &putithere->typed_val_float.dval, s); |
c906108c | 1156 | p[len] = saved_char; /* restore the input stream */ |
42969d33 WZ |
1157 | |
1158 | if (num == 1) | |
1159 | putithere->typed_val_float.type = | |
3e79cecf | 1160 | parse_type->builtin_double; |
42969d33 WZ |
1161 | |
1162 | if (num == 2 ) | |
1163 | { | |
1164 | /* See if it has any float suffix: 'f' for float, 'l' for long | |
1165 | double. */ | |
1166 | if (!strcasecmp (s, "f")) | |
1167 | putithere->typed_val_float.type = | |
3e79cecf | 1168 | parse_type->builtin_float; |
42969d33 WZ |
1169 | else if (!strcasecmp (s, "l")) |
1170 | putithere->typed_val_float.type = | |
3e79cecf | 1171 | parse_type->builtin_long_double; |
42969d33 | 1172 | else |
348038cd MS |
1173 | { |
1174 | free (s); | |
1175 | return ERROR; | |
1176 | } | |
42969d33 | 1177 | } |
c906108c | 1178 | |
348038cd | 1179 | free (s); |
c906108c SS |
1180 | return FLOAT; |
1181 | } | |
1182 | ||
1183 | /* Handle base-switching prefixes 0x, 0t, 0d, 0 */ | |
1184 | if (p[0] == '0') | |
1185 | switch (p[1]) | |
1186 | { | |
1187 | case 'x': | |
1188 | case 'X': | |
1189 | if (len >= 3) | |
1190 | { | |
1191 | p += 2; | |
1192 | base = 16; | |
1193 | len -= 2; | |
1194 | } | |
1195 | break; | |
1196 | ||
1197 | case 't': | |
1198 | case 'T': | |
1199 | case 'd': | |
1200 | case 'D': | |
1201 | if (len >= 3) | |
1202 | { | |
1203 | p += 2; | |
1204 | base = 10; | |
1205 | len -= 2; | |
1206 | } | |
1207 | break; | |
1208 | ||
1209 | default: | |
1210 | base = 8; | |
1211 | break; | |
1212 | } | |
1213 | ||
1214 | while (len-- > 0) | |
1215 | { | |
1216 | c = *p++; | |
1217 | if (c >= 'A' && c <= 'Z') | |
1218 | c += 'a' - 'A'; | |
1219 | if (c != 'l' && c != 'u') | |
1220 | n *= base; | |
1221 | if (c >= '0' && c <= '9') | |
1222 | { | |
1223 | if (found_suffix) | |
1224 | return ERROR; | |
1225 | n += i = c - '0'; | |
1226 | } | |
1227 | else | |
1228 | { | |
1229 | if (base > 10 && c >= 'a' && c <= 'f') | |
1230 | { | |
1231 | if (found_suffix) | |
1232 | return ERROR; | |
1233 | n += i = c - 'a' + 10; | |
1234 | } | |
1235 | else if (c == 'l') | |
1236 | { | |
1237 | ++long_p; | |
1238 | found_suffix = 1; | |
1239 | } | |
1240 | else if (c == 'u') | |
1241 | { | |
1242 | unsigned_p = 1; | |
1243 | found_suffix = 1; | |
1244 | } | |
1245 | else | |
1246 | return ERROR; /* Char not a digit */ | |
1247 | } | |
1248 | if (i >= base) | |
1249 | return ERROR; /* Invalid digit in this base */ | |
1250 | ||
1251 | /* Portably test for overflow (only works for nonzero values, so make | |
1252 | a second check for zero). FIXME: Can't we just make n and prevn | |
1253 | unsigned and avoid this? */ | |
1254 | if (c != 'l' && c != 'u' && (prevn >= n) && n != 0) | |
1255 | unsigned_p = 1; /* Try something unsigned */ | |
1256 | ||
1257 | /* Portably test for unsigned overflow. | |
1258 | FIXME: This check is wrong; for example it doesn't find overflow | |
1259 | on 0x123456789 when LONGEST is 32 bits. */ | |
1260 | if (c != 'l' && c != 'u' && n != 0) | |
1261 | { | |
1262 | if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) | |
1263 | error ("Numeric constant too large."); | |
1264 | } | |
1265 | prevn = n; | |
1266 | } | |
1267 | ||
1268 | /* An integer constant is an int, a long, or a long long. An L | |
1269 | suffix forces it to be long; an LL suffix forces it to be long | |
1270 | long. If not forced to a larger size, it gets the first type of | |
1271 | the above that it fits in. To figure out whether it fits, we | |
1272 | shift it right and see whether anything remains. Note that we | |
1273 | can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one | |
1274 | operation, because many compilers will warn about such a shift | |
9a76efb6 UW |
1275 | (which always produces a zero result). Sometimes gdbarch_int_bit |
1276 | or gdbarch_long_bit will be that big, sometimes not. To deal with | |
c906108c SS |
1277 | the case where it is we just always shift the value more than |
1278 | once, with fewer bits each time. */ | |
1279 | ||
1280 | un = (ULONGEST)n >> 2; | |
1281 | if (long_p == 0 | |
3e79cecf | 1282 | && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0) |
c906108c | 1283 | { |
3e79cecf | 1284 | high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1); |
c906108c SS |
1285 | |
1286 | /* A large decimal (not hex or octal) constant (between INT_MAX | |
1287 | and UINT_MAX) is a long or unsigned long, according to ANSI, | |
1288 | never an unsigned int, but this code treats it as unsigned | |
1289 | int. This probably should be fixed. GCC gives a warning on | |
1290 | such constants. */ | |
1291 | ||
3e79cecf UW |
1292 | unsigned_type = parse_type->builtin_unsigned_int; |
1293 | signed_type = parse_type->builtin_int; | |
c906108c SS |
1294 | } |
1295 | else if (long_p <= 1 | |
3e79cecf | 1296 | && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0) |
c906108c | 1297 | { |
3e79cecf UW |
1298 | high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1); |
1299 | unsigned_type = parse_type->builtin_unsigned_long; | |
1300 | signed_type = parse_type->builtin_long; | |
c906108c SS |
1301 | } |
1302 | else | |
1303 | { | |
1304 | int shift; | |
9a76efb6 | 1305 | if (sizeof (ULONGEST) * HOST_CHAR_BIT |
3e79cecf | 1306 | < gdbarch_long_long_bit (parse_gdbarch)) |
c906108c SS |
1307 | /* A long long does not fit in a LONGEST. */ |
1308 | shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1); | |
1309 | else | |
3e79cecf | 1310 | shift = (gdbarch_long_long_bit (parse_gdbarch) - 1); |
c906108c | 1311 | high_bit = (ULONGEST) 1 << shift; |
3e79cecf UW |
1312 | unsigned_type = parse_type->builtin_unsigned_long_long; |
1313 | signed_type = parse_type->builtin_long_long; | |
c906108c SS |
1314 | } |
1315 | ||
1316 | putithere->typed_val_int.val = n; | |
1317 | ||
1318 | /* If the high bit of the worked out type is set then this number | |
1319 | has to be unsigned. */ | |
1320 | ||
1321 | if (unsigned_p || (n & high_bit)) | |
1322 | { | |
1323 | putithere->typed_val_int.type = unsigned_type; | |
1324 | } | |
1325 | else | |
1326 | { | |
1327 | putithere->typed_val_int.type = signed_type; | |
1328 | } | |
1329 | ||
1330 | return INT; | |
1331 | } | |
1332 | ||
1333 | struct token | |
1334 | { | |
1335 | char *operator; | |
1336 | int token; | |
1337 | enum exp_opcode opcode; | |
1338 | }; | |
1339 | ||
1340 | static const struct token tokentab3[] = | |
1341 | { | |
1342 | {">>=", ASSIGN_MODIFY, BINOP_RSH}, | |
1343 | {"<<=", ASSIGN_MODIFY, BINOP_LSH} | |
1344 | }; | |
1345 | ||
1346 | static const struct token tokentab2[] = | |
1347 | { | |
1348 | {"+=", ASSIGN_MODIFY, BINOP_ADD}, | |
1349 | {"-=", ASSIGN_MODIFY, BINOP_SUB}, | |
1350 | {"*=", ASSIGN_MODIFY, BINOP_MUL}, | |
1351 | {"/=", ASSIGN_MODIFY, BINOP_DIV}, | |
1352 | {"%=", ASSIGN_MODIFY, BINOP_REM}, | |
1353 | {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR}, | |
1354 | {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND}, | |
1355 | {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR}, | |
1356 | {"++", INCREMENT, BINOP_END}, | |
1357 | {"--", DECREMENT, BINOP_END}, | |
1358 | {"->", ARROW, BINOP_END}, | |
1359 | {"&&", ANDAND, BINOP_END}, | |
1360 | {"||", OROR, BINOP_END}, | |
1361 | {"::", COLONCOLON, BINOP_END}, | |
1362 | {"<<", LSH, BINOP_END}, | |
1363 | {">>", RSH, BINOP_END}, | |
1364 | {"==", EQUAL, BINOP_END}, | |
1365 | {"!=", NOTEQUAL, BINOP_END}, | |
1366 | {"<=", LEQ, BINOP_END}, | |
1367 | {">=", GEQ, BINOP_END} | |
1368 | }; | |
1369 | ||
65d12d83 TT |
1370 | /* This is set if a NAME token appeared at the very end of the input |
1371 | string, with no whitespace separating the name from the EOF. This | |
1372 | is used only when parsing to do field name completion. */ | |
1373 | static int saw_name_at_eof; | |
1374 | ||
1375 | /* This is set if the previously-returned token was a structure | |
1376 | operator -- either '.' or ARROW. This is used only when parsing to | |
1377 | do field name completion. */ | |
1378 | static int last_was_structop; | |
1379 | ||
c906108c SS |
1380 | /* Read one token, getting characters through lexptr. */ |
1381 | ||
1382 | static int | |
1383 | yylex () | |
1384 | { | |
1385 | int c; | |
1386 | int namelen; | |
1387 | unsigned int i; | |
1388 | char *tokstart; | |
1389 | char *tokptr; | |
1390 | int tempbufindex; | |
1391 | static char *tempbuf; | |
1392 | static int tempbufsize; | |
c906108c SS |
1393 | char * token_string = NULL; |
1394 | int class_prefix = 0; | |
65d12d83 TT |
1395 | int saw_structop = last_was_structop; |
1396 | ||
1397 | last_was_structop = 0; | |
1398 | ||
c906108c SS |
1399 | retry: |
1400 | ||
84f0252a JB |
1401 | /* Check if this is a macro invocation that we need to expand. */ |
1402 | if (! scanning_macro_expansion ()) | |
1403 | { | |
1404 | char *expanded = macro_expand_next (&lexptr, | |
1405 | expression_macro_lookup_func, | |
1406 | expression_macro_lookup_baton); | |
1407 | ||
1408 | if (expanded) | |
1409 | scan_macro_expansion (expanded); | |
1410 | } | |
1411 | ||
665132f9 | 1412 | prev_lexptr = lexptr; |
c906108c SS |
1413 | |
1414 | tokstart = lexptr; | |
1415 | /* See if it is a special token of length 3. */ | |
1416 | for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) | |
bf896cb0 | 1417 | if (strncmp (tokstart, tokentab3[i].operator, 3) == 0) |
c906108c SS |
1418 | { |
1419 | lexptr += 3; | |
1420 | yylval.opcode = tokentab3[i].opcode; | |
1421 | return tokentab3[i].token; | |
1422 | } | |
1423 | ||
1424 | /* See if it is a special token of length 2. */ | |
1425 | for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) | |
bf896cb0 | 1426 | if (strncmp (tokstart, tokentab2[i].operator, 2) == 0) |
c906108c SS |
1427 | { |
1428 | lexptr += 2; | |
1429 | yylval.opcode = tokentab2[i].opcode; | |
37cd5d19 | 1430 | if (in_parse_field && tokentab2[i].token == ARROW) |
65d12d83 | 1431 | last_was_structop = 1; |
c906108c SS |
1432 | return tokentab2[i].token; |
1433 | } | |
1434 | ||
1435 | switch (c = *tokstart) | |
1436 | { | |
1437 | case 0: | |
84f0252a JB |
1438 | /* If we were just scanning the result of a macro expansion, |
1439 | then we need to resume scanning the original text. | |
65d12d83 TT |
1440 | If we're parsing for field name completion, and the previous |
1441 | token allows such completion, return a COMPLETE token. | |
84f0252a JB |
1442 | Otherwise, we were already scanning the original text, and |
1443 | we're really done. */ | |
1444 | if (scanning_macro_expansion ()) | |
1445 | { | |
1446 | finished_macro_expansion (); | |
1447 | goto retry; | |
1448 | } | |
65d12d83 TT |
1449 | else if (saw_name_at_eof) |
1450 | { | |
1451 | saw_name_at_eof = 0; | |
1452 | return COMPLETE; | |
1453 | } | |
1454 | else if (saw_structop) | |
1455 | return COMPLETE; | |
84f0252a JB |
1456 | else |
1457 | return 0; | |
c906108c SS |
1458 | |
1459 | case ' ': | |
1460 | case '\t': | |
1461 | case '\n': | |
1462 | lexptr++; | |
1463 | goto retry; | |
1464 | ||
1465 | case '\'': | |
1466 | /* We either have a character constant ('0' or '\177' for example) | |
1467 | or we have a quoted symbol reference ('foo(int,int)' in C++ | |
1468 | for example). */ | |
1469 | lexptr++; | |
1470 | c = *lexptr++; | |
1471 | if (c == '\\') | |
1472 | c = parse_escape (&lexptr); | |
1473 | else if (c == '\'') | |
1474 | error ("Empty character constant."); | |
234b45d4 KB |
1475 | else if (! host_char_to_target (c, &c)) |
1476 | { | |
1477 | int toklen = lexptr - tokstart + 1; | |
1478 | char *tok = alloca (toklen + 1); | |
1479 | memcpy (tok, tokstart, toklen); | |
1480 | tok[toklen] = '\0'; | |
1481 | error ("There is no character corresponding to %s in the target " | |
1482 | "character set `%s'.", tok, target_charset ()); | |
1483 | } | |
c906108c SS |
1484 | |
1485 | yylval.typed_val_int.val = c; | |
3e79cecf | 1486 | yylval.typed_val_int.type = parse_type->builtin_char; |
c906108c SS |
1487 | |
1488 | c = *lexptr++; | |
1489 | if (c != '\'') | |
1490 | { | |
1491 | namelen = skip_quoted (tokstart) - tokstart; | |
1492 | if (namelen > 2) | |
1493 | { | |
1494 | lexptr = tokstart + namelen; | |
c906108c SS |
1495 | if (lexptr[-1] != '\'') |
1496 | error ("Unmatched single quote."); | |
1497 | namelen -= 2; | |
1498 | tokstart++; | |
1499 | goto tryname; | |
1500 | } | |
1501 | error ("Invalid character constant."); | |
1502 | } | |
1503 | return INT; | |
1504 | ||
1505 | case '(': | |
1506 | paren_depth++; | |
1507 | lexptr++; | |
1508 | return c; | |
1509 | ||
1510 | case ')': | |
1511 | if (paren_depth == 0) | |
1512 | return 0; | |
1513 | paren_depth--; | |
1514 | lexptr++; | |
1515 | return c; | |
1516 | ||
1517 | case ',': | |
84f0252a JB |
1518 | if (comma_terminates |
1519 | && paren_depth == 0 | |
1520 | && ! scanning_macro_expansion ()) | |
c906108c SS |
1521 | return 0; |
1522 | lexptr++; | |
1523 | return c; | |
1524 | ||
1525 | case '.': | |
1526 | /* Might be a floating point number. */ | |
1527 | if (lexptr[1] < '0' || lexptr[1] > '9') | |
65d12d83 TT |
1528 | { |
1529 | if (in_parse_field) | |
1530 | last_was_structop = 1; | |
1531 | goto symbol; /* Nope, must be a symbol. */ | |
1532 | } | |
c906108c SS |
1533 | /* FALL THRU into number case. */ |
1534 | ||
1535 | case '0': | |
1536 | case '1': | |
1537 | case '2': | |
1538 | case '3': | |
1539 | case '4': | |
1540 | case '5': | |
1541 | case '6': | |
1542 | case '7': | |
1543 | case '8': | |
1544 | case '9': | |
1545 | { | |
1546 | /* It's a number. */ | |
1547 | int got_dot = 0, got_e = 0, toktype; | |
710122da | 1548 | char *p = tokstart; |
c906108c SS |
1549 | int hex = input_radix > 10; |
1550 | ||
1551 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) | |
1552 | { | |
1553 | p += 2; | |
1554 | hex = 1; | |
1555 | } | |
1556 | else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D')) | |
1557 | { | |
1558 | p += 2; | |
1559 | hex = 0; | |
1560 | } | |
1561 | ||
1562 | for (;; ++p) | |
1563 | { | |
1564 | /* This test includes !hex because 'e' is a valid hex digit | |
1565 | and thus does not indicate a floating point number when | |
1566 | the radix is hex. */ | |
1567 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) | |
1568 | got_dot = got_e = 1; | |
1569 | /* This test does not include !hex, because a '.' always indicates | |
1570 | a decimal floating point number regardless of the radix. */ | |
1571 | else if (!got_dot && *p == '.') | |
1572 | got_dot = 1; | |
1573 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') | |
1574 | && (*p == '-' || *p == '+')) | |
1575 | /* This is the sign of the exponent, not the end of the | |
1576 | number. */ | |
1577 | continue; | |
1578 | /* We will take any letters or digits. parse_number will | |
1579 | complain if past the radix, or if L or U are not final. */ | |
1580 | else if ((*p < '0' || *p > '9') | |
1581 | && ((*p < 'a' || *p > 'z') | |
1582 | && (*p < 'A' || *p > 'Z'))) | |
1583 | break; | |
1584 | } | |
1585 | toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval); | |
1586 | if (toktype == ERROR) | |
1587 | { | |
1588 | char *err_copy = (char *) alloca (p - tokstart + 1); | |
1589 | ||
1590 | memcpy (err_copy, tokstart, p - tokstart); | |
1591 | err_copy[p - tokstart] = 0; | |
1592 | error ("Invalid number \"%s\".", err_copy); | |
1593 | } | |
1594 | lexptr = p; | |
1595 | return toktype; | |
1596 | } | |
1597 | ||
1598 | case '+': | |
1599 | case '-': | |
1600 | case '*': | |
1601 | case '/': | |
1602 | case '%': | |
1603 | case '|': | |
1604 | case '&': | |
1605 | case '^': | |
1606 | case '~': | |
1607 | case '!': | |
1608 | case '@': | |
1609 | case '<': | |
1610 | case '>': | |
1611 | case '[': | |
1612 | case ']': | |
1613 | case '?': | |
1614 | case ':': | |
1615 | case '=': | |
1616 | case '{': | |
1617 | case '}': | |
1618 | symbol: | |
1619 | lexptr++; | |
1620 | return c; | |
1621 | ||
1622 | case '"': | |
1623 | ||
1624 | /* Build the gdb internal form of the input string in tempbuf, | |
1625 | translating any standard C escape forms seen. Note that the | |
1626 | buffer is null byte terminated *only* for the convenience of | |
1627 | debugging gdb itself and printing the buffer contents when | |
1628 | the buffer contains no embedded nulls. Gdb does not depend | |
1629 | upon the buffer being null byte terminated, it uses the length | |
1630 | string instead. This allows gdb to handle C strings (as well | |
1631 | as strings in other languages) with embedded null bytes */ | |
1632 | ||
1633 | tokptr = ++tokstart; | |
1634 | tempbufindex = 0; | |
1635 | ||
1636 | do { | |
234b45d4 KB |
1637 | char *char_start_pos = tokptr; |
1638 | ||
c906108c SS |
1639 | /* Grow the static temp buffer if necessary, including allocating |
1640 | the first one on demand. */ | |
1641 | if (tempbufindex + 1 >= tempbufsize) | |
1642 | { | |
1643 | tempbuf = (char *) realloc (tempbuf, tempbufsize += 64); | |
1644 | } | |
1645 | switch (*tokptr) | |
1646 | { | |
1647 | case '\0': | |
1648 | case '"': | |
1649 | /* Do nothing, loop will terminate. */ | |
1650 | break; | |
1651 | case '\\': | |
1652 | tokptr++; | |
1653 | c = parse_escape (&tokptr); | |
1654 | if (c == -1) | |
1655 | { | |
1656 | continue; | |
1657 | } | |
1658 | tempbuf[tempbufindex++] = c; | |
1659 | break; | |
1660 | default: | |
234b45d4 KB |
1661 | c = *tokptr++; |
1662 | if (! host_char_to_target (c, &c)) | |
1663 | { | |
1664 | int len = tokptr - char_start_pos; | |
1665 | char *copy = alloca (len + 1); | |
1666 | memcpy (copy, char_start_pos, len); | |
1667 | copy[len] = '\0'; | |
1668 | ||
1669 | error ("There is no character corresponding to `%s' " | |
1670 | "in the target character set `%s'.", | |
1671 | copy, target_charset ()); | |
1672 | } | |
1673 | tempbuf[tempbufindex++] = c; | |
c906108c SS |
1674 | break; |
1675 | } | |
1676 | } while ((*tokptr != '"') && (*tokptr != '\0')); | |
1677 | if (*tokptr++ != '"') | |
1678 | { | |
1679 | error ("Unterminated string in expression."); | |
1680 | } | |
1681 | tempbuf[tempbufindex] = '\0'; /* See note above */ | |
1682 | yylval.sval.ptr = tempbuf; | |
1683 | yylval.sval.length = tempbufindex; | |
1684 | lexptr = tokptr; | |
1685 | return (STRING); | |
1686 | } | |
1687 | ||
1688 | if (!(c == '_' || c == '$' | |
1689 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) | |
1690 | /* We must have come across a bad character (e.g. ';'). */ | |
1691 | error ("Invalid character '%c' in expression.", c); | |
1692 | ||
1693 | /* It's a name. See how long it is. */ | |
1694 | namelen = 0; | |
1695 | for (c = tokstart[namelen]; | |
1696 | (c == '_' || c == '$' || (c >= '0' && c <= '9') | |
1697 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');) | |
1698 | { | |
1699 | /* Template parameter lists are part of the name. | |
1700 | FIXME: This mishandles `print $a<4&&$a>3'. */ | |
1701 | ||
1702 | if (c == '<') | |
1703 | { | |
c906108c SS |
1704 | /* Scan ahead to get rest of the template specification. Note |
1705 | that we look ahead only when the '<' adjoins non-whitespace | |
1706 | characters; for comparison expressions, e.g. "a < b > c", | |
1707 | there must be spaces before the '<', etc. */ | |
1708 | ||
1709 | char * p = find_template_name_end (tokstart + namelen); | |
1710 | if (p) | |
1711 | namelen = p - tokstart; | |
1712 | break; | |
c906108c SS |
1713 | } |
1714 | c = tokstart[++namelen]; | |
1715 | } | |
1716 | ||
84f0252a JB |
1717 | /* The token "if" terminates the expression and is NOT removed from |
1718 | the input stream. It doesn't count if it appears in the | |
1719 | expansion of a macro. */ | |
1720 | if (namelen == 2 | |
1721 | && tokstart[0] == 'i' | |
1722 | && tokstart[1] == 'f' | |
1723 | && ! scanning_macro_expansion ()) | |
c906108c SS |
1724 | { |
1725 | return 0; | |
1726 | } | |
1727 | ||
1728 | lexptr += namelen; | |
1729 | ||
1730 | tryname: | |
1731 | ||
1732 | /* Catch specific keywords. Should be done with a data structure. */ | |
1733 | switch (namelen) | |
1734 | { | |
1735 | case 8: | |
bf896cb0 | 1736 | if (strncmp (tokstart, "unsigned", 8) == 0) |
c906108c | 1737 | return UNSIGNED; |
3e79cecf | 1738 | if (parse_language->la_language == language_cplus |
bf896cb0 | 1739 | && strncmp (tokstart, "template", 8) == 0) |
c906108c | 1740 | return TEMPLATE; |
bf896cb0 | 1741 | if (strncmp (tokstart, "volatile", 8) == 0) |
c906108c SS |
1742 | return VOLATILE_KEYWORD; |
1743 | break; | |
1744 | case 6: | |
bf896cb0 | 1745 | if (strncmp (tokstart, "struct", 6) == 0) |
c906108c | 1746 | return STRUCT; |
bf896cb0 | 1747 | if (strncmp (tokstart, "signed", 6) == 0) |
c906108c | 1748 | return SIGNED_KEYWORD; |
bf896cb0 | 1749 | if (strncmp (tokstart, "sizeof", 6) == 0) |
c906108c | 1750 | return SIZEOF; |
bf896cb0 | 1751 | if (strncmp (tokstart, "double", 6) == 0) |
c906108c SS |
1752 | return DOUBLE_KEYWORD; |
1753 | break; | |
1754 | case 5: | |
3e79cecf | 1755 | if (parse_language->la_language == language_cplus) |
c906108c | 1756 | { |
bf896cb0 | 1757 | if (strncmp (tokstart, "false", 5) == 0) |
c906108c | 1758 | return FALSEKEYWORD; |
bf896cb0 | 1759 | if (strncmp (tokstart, "class", 5) == 0) |
c906108c SS |
1760 | return CLASS; |
1761 | } | |
bf896cb0 | 1762 | if (strncmp (tokstart, "union", 5) == 0) |
c906108c | 1763 | return UNION; |
bf896cb0 | 1764 | if (strncmp (tokstart, "short", 5) == 0) |
c906108c | 1765 | return SHORT; |
bf896cb0 | 1766 | if (strncmp (tokstart, "const", 5) == 0) |
c906108c SS |
1767 | return CONST_KEYWORD; |
1768 | break; | |
1769 | case 4: | |
bf896cb0 | 1770 | if (strncmp (tokstart, "enum", 4) == 0) |
c906108c | 1771 | return ENUM; |
bf896cb0 | 1772 | if (strncmp (tokstart, "long", 4) == 0) |
c906108c | 1773 | return LONG; |
3e79cecf | 1774 | if (parse_language->la_language == language_cplus) |
c906108c | 1775 | { |
bf896cb0 | 1776 | if (strncmp (tokstart, "true", 4) == 0) |
c906108c | 1777 | return TRUEKEYWORD; |
c906108c SS |
1778 | } |
1779 | break; | |
1780 | case 3: | |
bf896cb0 | 1781 | if (strncmp (tokstart, "int", 3) == 0) |
c906108c SS |
1782 | return INT_KEYWORD; |
1783 | break; | |
1784 | default: | |
1785 | break; | |
1786 | } | |
1787 | ||
1788 | yylval.sval.ptr = tokstart; | |
1789 | yylval.sval.length = namelen; | |
1790 | ||
1791 | if (*tokstart == '$') | |
1792 | { | |
1793 | write_dollar_variable (yylval.sval); | |
1794 | return VARIABLE; | |
1795 | } | |
1796 | ||
c906108c SS |
1797 | /* Use token-type BLOCKNAME for symbols that happen to be defined as |
1798 | functions or symtabs. If this is not so, then ... | |
1799 | Use token-type TYPENAME for symbols that happen to be defined | |
1800 | currently as names of types; NAME for other symbols. | |
1801 | The caller is not constrained to care about the distinction. */ | |
1802 | { | |
1803 | char *tmp = copy_name (yylval.sval); | |
1804 | struct symbol *sym; | |
1805 | int is_a_field_of_this = 0; | |
1806 | int hextype; | |
1807 | ||
1808 | sym = lookup_symbol (tmp, expression_context_block, | |
176620f1 | 1809 | VAR_DOMAIN, |
3e79cecf | 1810 | parse_language->la_language == language_cplus |
2570f2b7 | 1811 | ? &is_a_field_of_this : (int *) NULL); |
c906108c SS |
1812 | /* Call lookup_symtab, not lookup_partial_symtab, in case there are |
1813 | no psymtabs (coff, xcoff, or some future change to blow away the | |
1814 | psymtabs once once symbols are read). */ | |
1815 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
1816 | { | |
1817 | yylval.ssym.sym = sym; | |
1818 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1819 | return BLOCKNAME; | |
1820 | } | |
1821 | else if (!sym) | |
1822 | { /* See if it's a file name. */ | |
1823 | struct symtab *symtab; | |
1824 | ||
1825 | symtab = lookup_symtab (tmp); | |
1826 | ||
1827 | if (symtab) | |
1828 | { | |
1829 | yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK); | |
1830 | return FILENAME; | |
1831 | } | |
1832 | } | |
1833 | ||
1834 | if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
1835 | { | |
79c2c32d DC |
1836 | /* NOTE: carlton/2003-09-25: There used to be code here to |
1837 | handle nested types. It didn't work very well. See the | |
1838 | comment before qualified_type for more info. */ | |
c906108c | 1839 | yylval.tsym.type = SYMBOL_TYPE (sym); |
c906108c SS |
1840 | return TYPENAME; |
1841 | } | |
54a5b07d | 1842 | yylval.tsym.type |
3e79cecf UW |
1843 | = language_lookup_primitive_type_by_name (parse_language, |
1844 | parse_gdbarch, tmp); | |
54a5b07d | 1845 | if (yylval.tsym.type != NULL) |
47663de5 | 1846 | return TYPENAME; |
c906108c SS |
1847 | |
1848 | /* Input names that aren't symbols but ARE valid hex numbers, | |
1849 | when the input radix permits them, can be names or numbers | |
1850 | depending on the parse. Note we support radixes > 16 here. */ | |
1851 | if (!sym && | |
1852 | ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) || | |
1853 | (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))) | |
1854 | { | |
1855 | YYSTYPE newlval; /* Its value is ignored. */ | |
1856 | hextype = parse_number (tokstart, namelen, 0, &newlval); | |
1857 | if (hextype == INT) | |
1858 | { | |
1859 | yylval.ssym.sym = sym; | |
1860 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1861 | return NAME_OR_INT; | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | /* Any other kind of symbol */ | |
1866 | yylval.ssym.sym = sym; | |
1867 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
65d12d83 TT |
1868 | if (in_parse_field && *lexptr == '\0') |
1869 | saw_name_at_eof = 1; | |
c906108c SS |
1870 | return NAME; |
1871 | } | |
1872 | } | |
1873 | ||
65d12d83 TT |
1874 | int |
1875 | c_parse (void) | |
1876 | { | |
1877 | last_was_structop = 0; | |
1878 | saw_name_at_eof = 0; | |
1879 | return yyparse (); | |
1880 | } | |
1881 | ||
c906108c SS |
1882 | void |
1883 | yyerror (msg) | |
1884 | char *msg; | |
1885 | { | |
665132f9 MS |
1886 | if (prev_lexptr) |
1887 | lexptr = prev_lexptr; | |
1888 | ||
c906108c SS |
1889 | error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr); |
1890 | } |