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 | 186 | %token <tsym> TYPENAME |
c209f847 | 187 | %type <sval> name string_exp |
c906108c SS |
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 | ||
c209f847 TT |
565 | string_exp: |
566 | STRING | |
567 | { | |
568 | /* We copy the string here, and not in the | |
569 | lexer, to guarantee that we do not leak a | |
570 | string. Note that we follow the | |
571 | NUL-termination convention of the | |
572 | lexer. */ | |
573 | $$.length = $1.length; | |
574 | $$.ptr = malloc ($1.length + 1); | |
575 | memcpy ($$.ptr, $1.ptr, $1.length + 1); | |
576 | } | |
577 | ||
578 | | string_exp STRING | |
579 | { | |
580 | /* Note that we NUL-terminate here, but just | |
581 | for convenience. */ | |
582 | struct stoken t; | |
583 | t.length = $1.length + $2.length; | |
584 | t.ptr = malloc (t.length + 1); | |
585 | memcpy (t.ptr, $1.ptr, $1.length); | |
586 | memcpy (t.ptr + $1.length, $2.ptr, $2.length + 1); | |
587 | free ($1.ptr); | |
588 | $$ = t; | |
589 | } | |
590 | ; | |
591 | ||
592 | exp : string_exp | |
c906108c SS |
593 | { /* C strings are converted into array constants with |
594 | an explicit null byte added at the end. Thus | |
595 | the array upper bound is the string length. | |
596 | There is no such thing in C as a completely empty | |
597 | string. */ | |
598 | char *sp = $1.ptr; int count = $1.length; | |
599 | while (count-- > 0) | |
600 | { | |
601 | write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 602 | write_exp_elt_type (parse_type->builtin_char); |
c906108c SS |
603 | write_exp_elt_longcst ((LONGEST)(*sp++)); |
604 | write_exp_elt_opcode (OP_LONG); | |
605 | } | |
606 | write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 607 | write_exp_elt_type (parse_type->builtin_char); |
c906108c SS |
608 | write_exp_elt_longcst ((LONGEST)'\0'); |
609 | write_exp_elt_opcode (OP_LONG); | |
610 | write_exp_elt_opcode (OP_ARRAY); | |
611 | write_exp_elt_longcst ((LONGEST) 0); | |
612 | write_exp_elt_longcst ((LONGEST) ($1.length)); | |
c209f847 TT |
613 | write_exp_elt_opcode (OP_ARRAY); |
614 | free ($1.ptr); | |
615 | } | |
c906108c SS |
616 | ; |
617 | ||
618 | /* C++. */ | |
c906108c SS |
619 | exp : TRUEKEYWORD |
620 | { write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 621 | write_exp_elt_type (parse_type->builtin_bool); |
c906108c SS |
622 | write_exp_elt_longcst ((LONGEST) 1); |
623 | write_exp_elt_opcode (OP_LONG); } | |
624 | ; | |
625 | ||
626 | exp : FALSEKEYWORD | |
627 | { write_exp_elt_opcode (OP_LONG); | |
3e79cecf | 628 | write_exp_elt_type (parse_type->builtin_bool); |
c906108c SS |
629 | write_exp_elt_longcst ((LONGEST) 0); |
630 | write_exp_elt_opcode (OP_LONG); } | |
631 | ; | |
632 | ||
633 | /* end of C++. */ | |
634 | ||
635 | block : BLOCKNAME | |
636 | { | |
637 | if ($1.sym) | |
638 | $$ = SYMBOL_BLOCK_VALUE ($1.sym); | |
639 | else | |
640 | error ("No file or function \"%s\".", | |
641 | copy_name ($1.stoken)); | |
642 | } | |
643 | | FILENAME | |
644 | { | |
645 | $$ = $1; | |
646 | } | |
647 | ; | |
648 | ||
649 | block : block COLONCOLON name | |
650 | { struct symbol *tem | |
651 | = lookup_symbol (copy_name ($3), $1, | |
2570f2b7 | 652 | VAR_DOMAIN, (int *) NULL); |
c906108c SS |
653 | if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK) |
654 | error ("No function \"%s\" in specified context.", | |
655 | copy_name ($3)); | |
656 | $$ = SYMBOL_BLOCK_VALUE (tem); } | |
657 | ; | |
658 | ||
659 | variable: block COLONCOLON name | |
660 | { struct symbol *sym; | |
661 | sym = lookup_symbol (copy_name ($3), $1, | |
2570f2b7 | 662 | VAR_DOMAIN, (int *) NULL); |
c906108c SS |
663 | if (sym == 0) |
664 | error ("No symbol \"%s\" in specified context.", | |
665 | copy_name ($3)); | |
666 | ||
667 | write_exp_elt_opcode (OP_VAR_VALUE); | |
668 | /* block_found is set by lookup_symbol. */ | |
669 | write_exp_elt_block (block_found); | |
670 | write_exp_elt_sym (sym); | |
671 | write_exp_elt_opcode (OP_VAR_VALUE); } | |
672 | ; | |
673 | ||
674 | qualified_name: typebase COLONCOLON name | |
675 | { | |
676 | struct type *type = $1; | |
677 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
79c2c32d DC |
678 | && TYPE_CODE (type) != TYPE_CODE_UNION |
679 | && TYPE_CODE (type) != TYPE_CODE_NAMESPACE) | |
c906108c SS |
680 | error ("`%s' is not defined as an aggregate type.", |
681 | TYPE_NAME (type)); | |
682 | ||
683 | write_exp_elt_opcode (OP_SCOPE); | |
684 | write_exp_elt_type (type); | |
685 | write_exp_string ($3); | |
686 | write_exp_elt_opcode (OP_SCOPE); | |
687 | } | |
688 | | typebase COLONCOLON '~' name | |
689 | { | |
690 | struct type *type = $1; | |
691 | struct stoken tmp_token; | |
692 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
79c2c32d DC |
693 | && TYPE_CODE (type) != TYPE_CODE_UNION |
694 | && TYPE_CODE (type) != TYPE_CODE_NAMESPACE) | |
c906108c SS |
695 | error ("`%s' is not defined as an aggregate type.", |
696 | TYPE_NAME (type)); | |
697 | ||
698 | tmp_token.ptr = (char*) alloca ($4.length + 2); | |
699 | tmp_token.length = $4.length + 1; | |
700 | tmp_token.ptr[0] = '~'; | |
701 | memcpy (tmp_token.ptr+1, $4.ptr, $4.length); | |
702 | tmp_token.ptr[tmp_token.length] = 0; | |
703 | ||
704 | /* Check for valid destructor name. */ | |
705 | destructor_name_p (tmp_token.ptr, type); | |
706 | write_exp_elt_opcode (OP_SCOPE); | |
707 | write_exp_elt_type (type); | |
708 | write_exp_string (tmp_token); | |
709 | write_exp_elt_opcode (OP_SCOPE); | |
710 | } | |
711 | ; | |
712 | ||
713 | variable: qualified_name | |
714 | | COLONCOLON name | |
715 | { | |
716 | char *name = copy_name ($2); | |
717 | struct symbol *sym; | |
718 | struct minimal_symbol *msymbol; | |
719 | ||
720 | sym = | |
721 | lookup_symbol (name, (const struct block *) NULL, | |
2570f2b7 | 722 | VAR_DOMAIN, (int *) NULL); |
c906108c SS |
723 | if (sym) |
724 | { | |
725 | write_exp_elt_opcode (OP_VAR_VALUE); | |
726 | write_exp_elt_block (NULL); | |
727 | write_exp_elt_sym (sym); | |
728 | write_exp_elt_opcode (OP_VAR_VALUE); | |
729 | break; | |
730 | } | |
731 | ||
732 | msymbol = lookup_minimal_symbol (name, NULL, NULL); | |
733 | if (msymbol != NULL) | |
c841afd5 UW |
734 | write_exp_msymbol (msymbol); |
735 | else if (!have_full_symbols () && !have_partial_symbols ()) | |
736 | error ("No symbol table is loaded. Use the \"file\" command."); | |
c906108c | 737 | else |
c841afd5 | 738 | error ("No symbol \"%s\" in current context.", name); |
c906108c SS |
739 | } |
740 | ; | |
741 | ||
742 | variable: name_not_typename | |
743 | { struct symbol *sym = $1.sym; | |
744 | ||
745 | if (sym) | |
746 | { | |
747 | if (symbol_read_needs_frame (sym)) | |
748 | { | |
749 | if (innermost_block == 0 || | |
750 | contained_in (block_found, | |
751 | innermost_block)) | |
752 | innermost_block = block_found; | |
753 | } | |
754 | ||
755 | write_exp_elt_opcode (OP_VAR_VALUE); | |
756 | /* We want to use the selected frame, not | |
757 | another more inner frame which happens to | |
758 | be in the same block. */ | |
759 | write_exp_elt_block (NULL); | |
760 | write_exp_elt_sym (sym); | |
761 | write_exp_elt_opcode (OP_VAR_VALUE); | |
762 | } | |
763 | else if ($1.is_a_field_of_this) | |
764 | { | |
765 | /* C++: it hangs off of `this'. Must | |
766 | not inadvertently convert from a method call | |
767 | to data ref. */ | |
768 | if (innermost_block == 0 || | |
769 | contained_in (block_found, innermost_block)) | |
770 | innermost_block = block_found; | |
771 | write_exp_elt_opcode (OP_THIS); | |
772 | write_exp_elt_opcode (OP_THIS); | |
773 | write_exp_elt_opcode (STRUCTOP_PTR); | |
774 | write_exp_string ($1.stoken); | |
775 | write_exp_elt_opcode (STRUCTOP_PTR); | |
776 | } | |
777 | else | |
778 | { | |
779 | struct minimal_symbol *msymbol; | |
710122da | 780 | char *arg = copy_name ($1.stoken); |
c906108c SS |
781 | |
782 | msymbol = | |
783 | lookup_minimal_symbol (arg, NULL, NULL); | |
784 | if (msymbol != NULL) | |
c841afd5 | 785 | write_exp_msymbol (msymbol); |
c906108c SS |
786 | else if (!have_full_symbols () && !have_partial_symbols ()) |
787 | error ("No symbol table is loaded. Use the \"file\" command."); | |
788 | else | |
789 | error ("No symbol \"%s\" in current context.", | |
790 | copy_name ($1.stoken)); | |
791 | } | |
792 | } | |
793 | ; | |
794 | ||
47663de5 MS |
795 | space_identifier : '@' NAME |
796 | { push_type_address_space (copy_name ($2.stoken)); | |
797 | push_type (tp_space_identifier); | |
798 | } | |
799 | ; | |
c906108c | 800 | |
47663de5 MS |
801 | const_or_volatile: const_or_volatile_noopt |
802 | | | |
c906108c | 803 | ; |
47663de5 MS |
804 | |
805 | cv_with_space_id : const_or_volatile space_identifier const_or_volatile | |
56e2d25a | 806 | ; |
47663de5 MS |
807 | |
808 | const_or_volatile_or_space_identifier_noopt: cv_with_space_id | |
809 | | const_or_volatile_noopt | |
56e2d25a | 810 | ; |
47663de5 MS |
811 | |
812 | const_or_volatile_or_space_identifier: | |
813 | const_or_volatile_or_space_identifier_noopt | |
814 | | | |
56e2d25a | 815 | ; |
47663de5 | 816 | |
c906108c SS |
817 | abs_decl: '*' |
818 | { push_type (tp_pointer); $$ = 0; } | |
819 | | '*' abs_decl | |
820 | { push_type (tp_pointer); $$ = $2; } | |
821 | | '&' | |
822 | { push_type (tp_reference); $$ = 0; } | |
823 | | '&' abs_decl | |
824 | { push_type (tp_reference); $$ = $2; } | |
825 | | direct_abs_decl | |
826 | ; | |
827 | ||
828 | direct_abs_decl: '(' abs_decl ')' | |
829 | { $$ = $2; } | |
830 | | direct_abs_decl array_mod | |
831 | { | |
832 | push_type_int ($2); | |
833 | push_type (tp_array); | |
834 | } | |
835 | | array_mod | |
836 | { | |
837 | push_type_int ($1); | |
838 | push_type (tp_array); | |
839 | $$ = 0; | |
840 | } | |
841 | ||
842 | | direct_abs_decl func_mod | |
843 | { push_type (tp_function); } | |
844 | | func_mod | |
845 | { push_type (tp_function); } | |
846 | ; | |
847 | ||
848 | array_mod: '[' ']' | |
849 | { $$ = -1; } | |
850 | | '[' INT ']' | |
851 | { $$ = $2.val; } | |
852 | ; | |
853 | ||
854 | func_mod: '(' ')' | |
855 | { $$ = 0; } | |
856 | | '(' nonempty_typelist ')' | |
8dbb1c65 | 857 | { free ($2); $$ = 0; } |
c906108c SS |
858 | ; |
859 | ||
a22229c4 | 860 | /* We used to try to recognize pointer to member types here, but |
c906108c SS |
861 | that didn't work (shift/reduce conflicts meant that these rules never |
862 | got executed). The problem is that | |
863 | int (foo::bar::baz::bizzle) | |
864 | is a function type but | |
865 | int (foo::bar::baz::bizzle::*) | |
866 | is a pointer to member type. Stroustrup loses again! */ | |
867 | ||
868 | type : ptype | |
c906108c SS |
869 | ; |
870 | ||
871 | typebase /* Implements (approximately): (type-qualifier)* type-specifier */ | |
872 | : TYPENAME | |
873 | { $$ = $1.type; } | |
874 | | INT_KEYWORD | |
3e79cecf | 875 | { $$ = parse_type->builtin_int; } |
c906108c | 876 | | LONG |
3e79cecf | 877 | { $$ = parse_type->builtin_long; } |
c906108c | 878 | | SHORT |
3e79cecf | 879 | { $$ = parse_type->builtin_short; } |
c906108c | 880 | | LONG INT_KEYWORD |
3e79cecf | 881 | { $$ = parse_type->builtin_long; } |
b2c4da81 | 882 | | LONG SIGNED_KEYWORD INT_KEYWORD |
3e79cecf | 883 | { $$ = parse_type->builtin_long; } |
b2c4da81 | 884 | | LONG SIGNED_KEYWORD |
3e79cecf | 885 | { $$ = parse_type->builtin_long; } |
b2c4da81 | 886 | | SIGNED_KEYWORD LONG INT_KEYWORD |
3e79cecf | 887 | { $$ = parse_type->builtin_long; } |
c906108c | 888 | | UNSIGNED LONG INT_KEYWORD |
3e79cecf | 889 | { $$ = parse_type->builtin_unsigned_long; } |
b2c4da81 | 890 | | LONG UNSIGNED INT_KEYWORD |
3e79cecf | 891 | { $$ = parse_type->builtin_unsigned_long; } |
b2c4da81 | 892 | | LONG UNSIGNED |
3e79cecf | 893 | { $$ = parse_type->builtin_unsigned_long; } |
c906108c | 894 | | LONG LONG |
3e79cecf | 895 | { $$ = parse_type->builtin_long_long; } |
c906108c | 896 | | LONG LONG INT_KEYWORD |
3e79cecf | 897 | { $$ = parse_type->builtin_long_long; } |
b2c4da81 | 898 | | LONG LONG SIGNED_KEYWORD INT_KEYWORD |
3e79cecf | 899 | { $$ = parse_type->builtin_long_long; } |
b2c4da81 | 900 | | LONG LONG SIGNED_KEYWORD |
3e79cecf | 901 | { $$ = parse_type->builtin_long_long; } |
b2c4da81 | 902 | | SIGNED_KEYWORD LONG LONG |
3e79cecf | 903 | { $$ = parse_type->builtin_long_long; } |
55baeb84 | 904 | | SIGNED_KEYWORD LONG LONG INT_KEYWORD |
3e79cecf | 905 | { $$ = parse_type->builtin_long_long; } |
c906108c | 906 | | UNSIGNED LONG LONG |
3e79cecf | 907 | { $$ = parse_type->builtin_unsigned_long_long; } |
c906108c | 908 | | UNSIGNED LONG LONG INT_KEYWORD |
3e79cecf | 909 | { $$ = parse_type->builtin_unsigned_long_long; } |
b2c4da81 | 910 | | LONG LONG UNSIGNED |
3e79cecf | 911 | { $$ = parse_type->builtin_unsigned_long_long; } |
b2c4da81 | 912 | | LONG LONG UNSIGNED INT_KEYWORD |
3e79cecf | 913 | { $$ = parse_type->builtin_unsigned_long_long; } |
c906108c | 914 | | SHORT INT_KEYWORD |
3e79cecf | 915 | { $$ = parse_type->builtin_short; } |
b2c4da81 | 916 | | SHORT SIGNED_KEYWORD INT_KEYWORD |
3e79cecf | 917 | { $$ = parse_type->builtin_short; } |
b2c4da81 | 918 | | SHORT SIGNED_KEYWORD |
3e79cecf | 919 | { $$ = parse_type->builtin_short; } |
c906108c | 920 | | UNSIGNED SHORT INT_KEYWORD |
3e79cecf | 921 | { $$ = parse_type->builtin_unsigned_short; } |
b2c4da81 | 922 | | SHORT UNSIGNED |
3e79cecf | 923 | { $$ = parse_type->builtin_unsigned_short; } |
b2c4da81 | 924 | | SHORT UNSIGNED INT_KEYWORD |
3e79cecf | 925 | { $$ = parse_type->builtin_unsigned_short; } |
c906108c | 926 | | DOUBLE_KEYWORD |
3e79cecf | 927 | { $$ = parse_type->builtin_double; } |
c906108c | 928 | | LONG DOUBLE_KEYWORD |
3e79cecf | 929 | { $$ = parse_type->builtin_long_double; } |
c906108c SS |
930 | | STRUCT name |
931 | { $$ = lookup_struct (copy_name ($2), | |
932 | expression_context_block); } | |
933 | | CLASS name | |
934 | { $$ = lookup_struct (copy_name ($2), | |
935 | expression_context_block); } | |
936 | | UNION name | |
937 | { $$ = lookup_union (copy_name ($2), | |
938 | expression_context_block); } | |
939 | | ENUM name | |
940 | { $$ = lookup_enum (copy_name ($2), | |
941 | expression_context_block); } | |
942 | | UNSIGNED typename | |
943 | { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); } | |
944 | | UNSIGNED | |
3e79cecf | 945 | { $$ = parse_type->builtin_unsigned_int; } |
c906108c SS |
946 | | SIGNED_KEYWORD typename |
947 | { $$ = lookup_signed_typename (TYPE_NAME($2.type)); } | |
948 | | SIGNED_KEYWORD | |
3e79cecf | 949 | { $$ = parse_type->builtin_int; } |
c906108c SS |
950 | /* It appears that this rule for templates is never |
951 | reduced; template recognition happens by lookahead | |
952 | in the token processing code in yylex. */ | |
953 | | TEMPLATE name '<' type '>' | |
954 | { $$ = lookup_template_type(copy_name($2), $4, | |
955 | expression_context_block); | |
956 | } | |
47663de5 MS |
957 | | const_or_volatile_or_space_identifier_noopt typebase |
958 | { $$ = follow_types ($2); } | |
959 | | typebase const_or_volatile_or_space_identifier_noopt | |
960 | { $$ = follow_types ($1); } | |
79c2c32d DC |
961 | | qualified_type |
962 | ; | |
963 | ||
964 | /* FIXME: carlton/2003-09-25: This next bit leads to lots of | |
965 | reduce-reduce conflicts, because the parser doesn't know whether or | |
966 | not to use qualified_name or qualified_type: the rules are | |
967 | identical. If the parser is parsing 'A::B::x', then, when it sees | |
968 | the second '::', it knows that the expression to the left of it has | |
969 | to be a type, so it uses qualified_type. But if it is parsing just | |
970 | 'A::B', then it doesn't have any way of knowing which rule to use, | |
971 | so there's a reduce-reduce conflict; it picks qualified_name, since | |
972 | that occurs earlier in this file than qualified_type. | |
973 | ||
974 | There's no good way to fix this with the grammar as it stands; as | |
975 | far as I can tell, some of the problems arise from ambiguities that | |
976 | GDB introduces ('start' can be either an expression or a type), but | |
977 | some of it is inherent to the nature of C++ (you want to treat the | |
978 | input "(FOO)" fairly differently depending on whether FOO is an | |
979 | expression or a type, and if FOO is a complex expression, this can | |
980 | be hard to determine at the right time). Fortunately, it works | |
981 | pretty well in most cases. For example, if you do 'ptype A::B', | |
982 | where A::B is a nested type, then the parser will mistakenly | |
983 | misidentify it as an expression; but evaluate_subexp will get | |
984 | called with 'noside' set to EVAL_AVOID_SIDE_EFFECTS, and everything | |
985 | will work out anyways. But there are situations where the parser | |
986 | will get confused: the most common one that I've run into is when | |
987 | you want to do | |
988 | ||
989 | print *((A::B *) x)" | |
990 | ||
991 | where the parser doesn't realize that A::B has to be a type until | |
992 | it hits the first right paren, at which point it's too late. (The | |
993 | workaround is to type "print *(('A::B' *) x)" instead.) (And | |
994 | another solution is to fix our symbol-handling code so that the | |
995 | user never wants to type something like that in the first place, | |
996 | because we get all the types right without the user's help!) | |
997 | ||
998 | Perhaps we could fix this by making the lexer smarter. Some of | |
999 | this functionality used to be in the lexer, but in a way that | |
1000 | worked even less well than the current solution: that attempt | |
1001 | involved having the parser sometimes handle '::' and having the | |
1002 | lexer sometimes handle it, and without a clear division of | |
1003 | responsibility, it quickly degenerated into a big mess. Probably | |
1004 | the eventual correct solution will give more of a role to the lexer | |
1005 | (ideally via code that is shared between the lexer and | |
1006 | decode_line_1), but I'm not holding my breath waiting for somebody | |
1007 | to get around to cleaning this up... */ | |
1008 | ||
79c2c32d DC |
1009 | qualified_type: typebase COLONCOLON name |
1010 | { | |
1011 | struct type *type = $1; | |
1012 | struct type *new_type; | |
1013 | char *ncopy = alloca ($3.length + 1); | |
1014 | ||
1015 | memcpy (ncopy, $3.ptr, $3.length); | |
1016 | ncopy[$3.length] = '\0'; | |
1017 | ||
63d06c5c DC |
1018 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
1019 | && TYPE_CODE (type) != TYPE_CODE_UNION | |
1020 | && TYPE_CODE (type) != TYPE_CODE_NAMESPACE) | |
1021 | error ("`%s' is not defined as an aggregate type.", | |
79c2c32d DC |
1022 | TYPE_NAME (type)); |
1023 | ||
1024 | new_type = cp_lookup_nested_type (type, ncopy, | |
1025 | expression_context_block); | |
1026 | if (new_type == NULL) | |
63d06c5c | 1027 | error ("No type \"%s\" within class or namespace \"%s\".", |
79c2c32d DC |
1028 | ncopy, TYPE_NAME (type)); |
1029 | ||
1030 | $$ = new_type; | |
1031 | } | |
c906108c SS |
1032 | ; |
1033 | ||
1034 | typename: TYPENAME | |
1035 | | INT_KEYWORD | |
1036 | { | |
1037 | $$.stoken.ptr = "int"; | |
1038 | $$.stoken.length = 3; | |
3e79cecf | 1039 | $$.type = parse_type->builtin_int; |
c906108c SS |
1040 | } |
1041 | | LONG | |
1042 | { | |
1043 | $$.stoken.ptr = "long"; | |
1044 | $$.stoken.length = 4; | |
3e79cecf | 1045 | $$.type = parse_type->builtin_long; |
c906108c SS |
1046 | } |
1047 | | SHORT | |
1048 | { | |
1049 | $$.stoken.ptr = "short"; | |
1050 | $$.stoken.length = 5; | |
3e79cecf | 1051 | $$.type = parse_type->builtin_short; |
c906108c SS |
1052 | } |
1053 | ; | |
1054 | ||
1055 | nonempty_typelist | |
1056 | : type | |
1057 | { $$ = (struct type **) malloc (sizeof (struct type *) * 2); | |
1058 | $<ivec>$[0] = 1; /* Number of types in vector */ | |
1059 | $$[1] = $1; | |
1060 | } | |
1061 | | nonempty_typelist ',' type | |
1062 | { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1); | |
1063 | $$ = (struct type **) realloc ((char *) $1, len); | |
1064 | $$[$<ivec>$[0]] = $3; | |
1065 | } | |
1066 | ; | |
1067 | ||
47663de5 MS |
1068 | ptype : typebase |
1069 | | ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier | |
1070 | { $$ = follow_types ($1); } | |
1071 | ; | |
1072 | ||
1073 | const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD | |
1074 | | VOLATILE_KEYWORD CONST_KEYWORD | |
1075 | ; | |
1076 | ||
1077 | const_or_volatile_noopt: const_and_volatile | |
1078 | { push_type (tp_const); | |
1079 | push_type (tp_volatile); | |
1080 | } | |
1081 | | CONST_KEYWORD | |
1082 | { push_type (tp_const); } | |
1083 | | VOLATILE_KEYWORD | |
1084 | { push_type (tp_volatile); } | |
1085 | ; | |
1086 | ||
c906108c SS |
1087 | name : NAME { $$ = $1.stoken; } |
1088 | | BLOCKNAME { $$ = $1.stoken; } | |
1089 | | TYPENAME { $$ = $1.stoken; } | |
1090 | | NAME_OR_INT { $$ = $1.stoken; } | |
1091 | ; | |
1092 | ||
1093 | name_not_typename : NAME | |
1094 | | BLOCKNAME | |
1095 | /* These would be useful if name_not_typename was useful, but it is just | |
1096 | a fake for "variable", so these cause reduce/reduce conflicts because | |
1097 | the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable, | |
1098 | =exp) or just an exp. If name_not_typename was ever used in an lvalue | |
1099 | context where only a name could occur, this might be useful. | |
1100 | | NAME_OR_INT | |
1101 | */ | |
1102 | ; | |
1103 | ||
1104 | %% | |
1105 | ||
1106 | /* Take care of parsing a number (anything that starts with a digit). | |
1107 | Set yylval and return the token type; update lexptr. | |
1108 | LEN is the number of characters in it. */ | |
1109 | ||
1110 | /*** Needs some error checking for the float case ***/ | |
1111 | ||
1112 | static int | |
1113 | parse_number (p, len, parsed_float, putithere) | |
710122da DC |
1114 | char *p; |
1115 | int len; | |
c906108c SS |
1116 | int parsed_float; |
1117 | YYSTYPE *putithere; | |
1118 | { | |
1119 | /* FIXME: Shouldn't these be unsigned? We don't deal with negative values | |
1120 | here, and we do kind of silly things like cast to unsigned. */ | |
710122da DC |
1121 | LONGEST n = 0; |
1122 | LONGEST prevn = 0; | |
c906108c SS |
1123 | ULONGEST un; |
1124 | ||
710122da DC |
1125 | int i = 0; |
1126 | int c; | |
1127 | int base = input_radix; | |
c906108c SS |
1128 | int unsigned_p = 0; |
1129 | ||
1130 | /* Number of "L" suffixes encountered. */ | |
1131 | int long_p = 0; | |
1132 | ||
1133 | /* We have found a "L" or "U" suffix. */ | |
1134 | int found_suffix = 0; | |
1135 | ||
1136 | ULONGEST high_bit; | |
1137 | struct type *signed_type; | |
1138 | struct type *unsigned_type; | |
1139 | ||
1140 | if (parsed_float) | |
1141 | { | |
1142 | /* It's a float since it contains a point or an exponent. */ | |
fe9441f6 JK |
1143 | char *s; |
1144 | int num; /* number of tokens scanned by scanf */ | |
1145 | char saved_char; | |
27bc4d80 TJB |
1146 | |
1147 | /* If it ends at "df", "dd" or "dl", take it as type of decimal floating | |
1148 | point. Return DECFLOAT. */ | |
1149 | ||
fe9441f6 | 1150 | if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f') |
27bc4d80 TJB |
1151 | { |
1152 | p[len - 2] = '\0'; | |
1153 | putithere->typed_val_decfloat.type | |
3e79cecf | 1154 | = parse_type->builtin_decfloat; |
27bc4d80 | 1155 | decimal_from_string (putithere->typed_val_decfloat.val, 4, p); |
fe9441f6 JK |
1156 | p[len - 2] = 'd'; |
1157 | return DECFLOAT; | |
27bc4d80 TJB |
1158 | } |
1159 | ||
fe9441f6 | 1160 | if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd') |
27bc4d80 TJB |
1161 | { |
1162 | p[len - 2] = '\0'; | |
1163 | putithere->typed_val_decfloat.type | |
3e79cecf | 1164 | = parse_type->builtin_decdouble; |
27bc4d80 | 1165 | decimal_from_string (putithere->typed_val_decfloat.val, 8, p); |
fe9441f6 JK |
1166 | p[len - 2] = 'd'; |
1167 | return DECFLOAT; | |
27bc4d80 TJB |
1168 | } |
1169 | ||
fe9441f6 | 1170 | if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l') |
27bc4d80 TJB |
1171 | { |
1172 | p[len - 2] = '\0'; | |
1173 | putithere->typed_val_decfloat.type | |
3e79cecf | 1174 | = parse_type->builtin_declong; |
27bc4d80 | 1175 | decimal_from_string (putithere->typed_val_decfloat.val, 16, p); |
fe9441f6 JK |
1176 | p[len - 2] = 'd'; |
1177 | return DECFLOAT; | |
27bc4d80 TJB |
1178 | } |
1179 | ||
fe9441f6 JK |
1180 | s = malloc (len); |
1181 | saved_char = p[len]; | |
1182 | p[len] = 0; /* null-terminate the token */ | |
689e4e2d | 1183 | num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s", |
96c1eda2 | 1184 | &putithere->typed_val_float.dval, s); |
c906108c | 1185 | p[len] = saved_char; /* restore the input stream */ |
42969d33 WZ |
1186 | |
1187 | if (num == 1) | |
1188 | putithere->typed_val_float.type = | |
3e79cecf | 1189 | parse_type->builtin_double; |
42969d33 WZ |
1190 | |
1191 | if (num == 2 ) | |
1192 | { | |
1193 | /* See if it has any float suffix: 'f' for float, 'l' for long | |
1194 | double. */ | |
1195 | if (!strcasecmp (s, "f")) | |
1196 | putithere->typed_val_float.type = | |
3e79cecf | 1197 | parse_type->builtin_float; |
42969d33 WZ |
1198 | else if (!strcasecmp (s, "l")) |
1199 | putithere->typed_val_float.type = | |
3e79cecf | 1200 | parse_type->builtin_long_double; |
42969d33 | 1201 | else |
348038cd MS |
1202 | { |
1203 | free (s); | |
1204 | return ERROR; | |
1205 | } | |
42969d33 | 1206 | } |
c906108c | 1207 | |
348038cd | 1208 | free (s); |
c906108c SS |
1209 | return FLOAT; |
1210 | } | |
1211 | ||
1212 | /* Handle base-switching prefixes 0x, 0t, 0d, 0 */ | |
1213 | if (p[0] == '0') | |
1214 | switch (p[1]) | |
1215 | { | |
1216 | case 'x': | |
1217 | case 'X': | |
1218 | if (len >= 3) | |
1219 | { | |
1220 | p += 2; | |
1221 | base = 16; | |
1222 | len -= 2; | |
1223 | } | |
1224 | break; | |
1225 | ||
1226 | case 't': | |
1227 | case 'T': | |
1228 | case 'd': | |
1229 | case 'D': | |
1230 | if (len >= 3) | |
1231 | { | |
1232 | p += 2; | |
1233 | base = 10; | |
1234 | len -= 2; | |
1235 | } | |
1236 | break; | |
1237 | ||
1238 | default: | |
1239 | base = 8; | |
1240 | break; | |
1241 | } | |
1242 | ||
1243 | while (len-- > 0) | |
1244 | { | |
1245 | c = *p++; | |
1246 | if (c >= 'A' && c <= 'Z') | |
1247 | c += 'a' - 'A'; | |
1248 | if (c != 'l' && c != 'u') | |
1249 | n *= base; | |
1250 | if (c >= '0' && c <= '9') | |
1251 | { | |
1252 | if (found_suffix) | |
1253 | return ERROR; | |
1254 | n += i = c - '0'; | |
1255 | } | |
1256 | else | |
1257 | { | |
1258 | if (base > 10 && c >= 'a' && c <= 'f') | |
1259 | { | |
1260 | if (found_suffix) | |
1261 | return ERROR; | |
1262 | n += i = c - 'a' + 10; | |
1263 | } | |
1264 | else if (c == 'l') | |
1265 | { | |
1266 | ++long_p; | |
1267 | found_suffix = 1; | |
1268 | } | |
1269 | else if (c == 'u') | |
1270 | { | |
1271 | unsigned_p = 1; | |
1272 | found_suffix = 1; | |
1273 | } | |
1274 | else | |
1275 | return ERROR; /* Char not a digit */ | |
1276 | } | |
1277 | if (i >= base) | |
1278 | return ERROR; /* Invalid digit in this base */ | |
1279 | ||
1280 | /* Portably test for overflow (only works for nonzero values, so make | |
1281 | a second check for zero). FIXME: Can't we just make n and prevn | |
1282 | unsigned and avoid this? */ | |
1283 | if (c != 'l' && c != 'u' && (prevn >= n) && n != 0) | |
1284 | unsigned_p = 1; /* Try something unsigned */ | |
1285 | ||
1286 | /* Portably test for unsigned overflow. | |
1287 | FIXME: This check is wrong; for example it doesn't find overflow | |
1288 | on 0x123456789 when LONGEST is 32 bits. */ | |
1289 | if (c != 'l' && c != 'u' && n != 0) | |
1290 | { | |
1291 | if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) | |
1292 | error ("Numeric constant too large."); | |
1293 | } | |
1294 | prevn = n; | |
1295 | } | |
1296 | ||
1297 | /* An integer constant is an int, a long, or a long long. An L | |
1298 | suffix forces it to be long; an LL suffix forces it to be long | |
1299 | long. If not forced to a larger size, it gets the first type of | |
1300 | the above that it fits in. To figure out whether it fits, we | |
1301 | shift it right and see whether anything remains. Note that we | |
1302 | can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one | |
1303 | operation, because many compilers will warn about such a shift | |
9a76efb6 UW |
1304 | (which always produces a zero result). Sometimes gdbarch_int_bit |
1305 | or gdbarch_long_bit will be that big, sometimes not. To deal with | |
c906108c SS |
1306 | the case where it is we just always shift the value more than |
1307 | once, with fewer bits each time. */ | |
1308 | ||
1309 | un = (ULONGEST)n >> 2; | |
1310 | if (long_p == 0 | |
3e79cecf | 1311 | && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0) |
c906108c | 1312 | { |
3e79cecf | 1313 | high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1); |
c906108c SS |
1314 | |
1315 | /* A large decimal (not hex or octal) constant (between INT_MAX | |
1316 | and UINT_MAX) is a long or unsigned long, according to ANSI, | |
1317 | never an unsigned int, but this code treats it as unsigned | |
1318 | int. This probably should be fixed. GCC gives a warning on | |
1319 | such constants. */ | |
1320 | ||
3e79cecf UW |
1321 | unsigned_type = parse_type->builtin_unsigned_int; |
1322 | signed_type = parse_type->builtin_int; | |
c906108c SS |
1323 | } |
1324 | else if (long_p <= 1 | |
3e79cecf | 1325 | && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0) |
c906108c | 1326 | { |
3e79cecf UW |
1327 | high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1); |
1328 | unsigned_type = parse_type->builtin_unsigned_long; | |
1329 | signed_type = parse_type->builtin_long; | |
c906108c SS |
1330 | } |
1331 | else | |
1332 | { | |
1333 | int shift; | |
9a76efb6 | 1334 | if (sizeof (ULONGEST) * HOST_CHAR_BIT |
3e79cecf | 1335 | < gdbarch_long_long_bit (parse_gdbarch)) |
c906108c SS |
1336 | /* A long long does not fit in a LONGEST. */ |
1337 | shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1); | |
1338 | else | |
3e79cecf | 1339 | shift = (gdbarch_long_long_bit (parse_gdbarch) - 1); |
c906108c | 1340 | high_bit = (ULONGEST) 1 << shift; |
3e79cecf UW |
1341 | unsigned_type = parse_type->builtin_unsigned_long_long; |
1342 | signed_type = parse_type->builtin_long_long; | |
c906108c SS |
1343 | } |
1344 | ||
1345 | putithere->typed_val_int.val = n; | |
1346 | ||
1347 | /* If the high bit of the worked out type is set then this number | |
1348 | has to be unsigned. */ | |
1349 | ||
1350 | if (unsigned_p || (n & high_bit)) | |
1351 | { | |
1352 | putithere->typed_val_int.type = unsigned_type; | |
1353 | } | |
1354 | else | |
1355 | { | |
1356 | putithere->typed_val_int.type = signed_type; | |
1357 | } | |
1358 | ||
1359 | return INT; | |
1360 | } | |
1361 | ||
1362 | struct token | |
1363 | { | |
1364 | char *operator; | |
1365 | int token; | |
1366 | enum exp_opcode opcode; | |
1367 | }; | |
1368 | ||
1369 | static const struct token tokentab3[] = | |
1370 | { | |
1371 | {">>=", ASSIGN_MODIFY, BINOP_RSH}, | |
1372 | {"<<=", ASSIGN_MODIFY, BINOP_LSH} | |
1373 | }; | |
1374 | ||
1375 | static const struct token tokentab2[] = | |
1376 | { | |
1377 | {"+=", ASSIGN_MODIFY, BINOP_ADD}, | |
1378 | {"-=", ASSIGN_MODIFY, BINOP_SUB}, | |
1379 | {"*=", ASSIGN_MODIFY, BINOP_MUL}, | |
1380 | {"/=", ASSIGN_MODIFY, BINOP_DIV}, | |
1381 | {"%=", ASSIGN_MODIFY, BINOP_REM}, | |
1382 | {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR}, | |
1383 | {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND}, | |
1384 | {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR}, | |
1385 | {"++", INCREMENT, BINOP_END}, | |
1386 | {"--", DECREMENT, BINOP_END}, | |
1387 | {"->", ARROW, BINOP_END}, | |
1388 | {"&&", ANDAND, BINOP_END}, | |
1389 | {"||", OROR, BINOP_END}, | |
1390 | {"::", COLONCOLON, BINOP_END}, | |
1391 | {"<<", LSH, BINOP_END}, | |
1392 | {">>", RSH, BINOP_END}, | |
1393 | {"==", EQUAL, BINOP_END}, | |
1394 | {"!=", NOTEQUAL, BINOP_END}, | |
1395 | {"<=", LEQ, BINOP_END}, | |
1396 | {">=", GEQ, BINOP_END} | |
1397 | }; | |
1398 | ||
65d12d83 TT |
1399 | /* This is set if a NAME token appeared at the very end of the input |
1400 | string, with no whitespace separating the name from the EOF. This | |
1401 | is used only when parsing to do field name completion. */ | |
1402 | static int saw_name_at_eof; | |
1403 | ||
1404 | /* This is set if the previously-returned token was a structure | |
1405 | operator -- either '.' or ARROW. This is used only when parsing to | |
1406 | do field name completion. */ | |
1407 | static int last_was_structop; | |
1408 | ||
c906108c SS |
1409 | /* Read one token, getting characters through lexptr. */ |
1410 | ||
1411 | static int | |
1412 | yylex () | |
1413 | { | |
1414 | int c; | |
1415 | int namelen; | |
1416 | unsigned int i; | |
1417 | char *tokstart; | |
1418 | char *tokptr; | |
1419 | int tempbufindex; | |
1420 | static char *tempbuf; | |
1421 | static int tempbufsize; | |
c906108c SS |
1422 | char * token_string = NULL; |
1423 | int class_prefix = 0; | |
65d12d83 TT |
1424 | int saw_structop = last_was_structop; |
1425 | ||
1426 | last_was_structop = 0; | |
1427 | ||
c906108c SS |
1428 | retry: |
1429 | ||
84f0252a JB |
1430 | /* Check if this is a macro invocation that we need to expand. */ |
1431 | if (! scanning_macro_expansion ()) | |
1432 | { | |
1433 | char *expanded = macro_expand_next (&lexptr, | |
1434 | expression_macro_lookup_func, | |
1435 | expression_macro_lookup_baton); | |
1436 | ||
1437 | if (expanded) | |
1438 | scan_macro_expansion (expanded); | |
1439 | } | |
1440 | ||
665132f9 | 1441 | prev_lexptr = lexptr; |
c906108c SS |
1442 | |
1443 | tokstart = lexptr; | |
1444 | /* See if it is a special token of length 3. */ | |
1445 | for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) | |
bf896cb0 | 1446 | if (strncmp (tokstart, tokentab3[i].operator, 3) == 0) |
c906108c SS |
1447 | { |
1448 | lexptr += 3; | |
1449 | yylval.opcode = tokentab3[i].opcode; | |
1450 | return tokentab3[i].token; | |
1451 | } | |
1452 | ||
1453 | /* See if it is a special token of length 2. */ | |
1454 | for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) | |
bf896cb0 | 1455 | if (strncmp (tokstart, tokentab2[i].operator, 2) == 0) |
c906108c SS |
1456 | { |
1457 | lexptr += 2; | |
1458 | yylval.opcode = tokentab2[i].opcode; | |
37cd5d19 | 1459 | if (in_parse_field && tokentab2[i].token == ARROW) |
65d12d83 | 1460 | last_was_structop = 1; |
c906108c SS |
1461 | return tokentab2[i].token; |
1462 | } | |
1463 | ||
1464 | switch (c = *tokstart) | |
1465 | { | |
1466 | case 0: | |
84f0252a JB |
1467 | /* If we were just scanning the result of a macro expansion, |
1468 | then we need to resume scanning the original text. | |
65d12d83 TT |
1469 | If we're parsing for field name completion, and the previous |
1470 | token allows such completion, return a COMPLETE token. | |
84f0252a JB |
1471 | Otherwise, we were already scanning the original text, and |
1472 | we're really done. */ | |
1473 | if (scanning_macro_expansion ()) | |
1474 | { | |
1475 | finished_macro_expansion (); | |
1476 | goto retry; | |
1477 | } | |
65d12d83 TT |
1478 | else if (saw_name_at_eof) |
1479 | { | |
1480 | saw_name_at_eof = 0; | |
1481 | return COMPLETE; | |
1482 | } | |
1483 | else if (saw_structop) | |
1484 | return COMPLETE; | |
84f0252a JB |
1485 | else |
1486 | return 0; | |
c906108c SS |
1487 | |
1488 | case ' ': | |
1489 | case '\t': | |
1490 | case '\n': | |
1491 | lexptr++; | |
1492 | goto retry; | |
1493 | ||
1494 | case '\'': | |
1495 | /* We either have a character constant ('0' or '\177' for example) | |
1496 | or we have a quoted symbol reference ('foo(int,int)' in C++ | |
1497 | for example). */ | |
1498 | lexptr++; | |
1499 | c = *lexptr++; | |
1500 | if (c == '\\') | |
1501 | c = parse_escape (&lexptr); | |
1502 | else if (c == '\'') | |
1503 | error ("Empty character constant."); | |
234b45d4 KB |
1504 | else if (! host_char_to_target (c, &c)) |
1505 | { | |
1506 | int toklen = lexptr - tokstart + 1; | |
1507 | char *tok = alloca (toklen + 1); | |
1508 | memcpy (tok, tokstart, toklen); | |
1509 | tok[toklen] = '\0'; | |
1510 | error ("There is no character corresponding to %s in the target " | |
1511 | "character set `%s'.", tok, target_charset ()); | |
1512 | } | |
c906108c SS |
1513 | |
1514 | yylval.typed_val_int.val = c; | |
3e79cecf | 1515 | yylval.typed_val_int.type = parse_type->builtin_char; |
c906108c SS |
1516 | |
1517 | c = *lexptr++; | |
1518 | if (c != '\'') | |
1519 | { | |
1520 | namelen = skip_quoted (tokstart) - tokstart; | |
1521 | if (namelen > 2) | |
1522 | { | |
1523 | lexptr = tokstart + namelen; | |
c906108c SS |
1524 | if (lexptr[-1] != '\'') |
1525 | error ("Unmatched single quote."); | |
1526 | namelen -= 2; | |
1527 | tokstart++; | |
1528 | goto tryname; | |
1529 | } | |
1530 | error ("Invalid character constant."); | |
1531 | } | |
1532 | return INT; | |
1533 | ||
1534 | case '(': | |
1535 | paren_depth++; | |
1536 | lexptr++; | |
1537 | return c; | |
1538 | ||
1539 | case ')': | |
1540 | if (paren_depth == 0) | |
1541 | return 0; | |
1542 | paren_depth--; | |
1543 | lexptr++; | |
1544 | return c; | |
1545 | ||
1546 | case ',': | |
84f0252a JB |
1547 | if (comma_terminates |
1548 | && paren_depth == 0 | |
1549 | && ! scanning_macro_expansion ()) | |
c906108c SS |
1550 | return 0; |
1551 | lexptr++; | |
1552 | return c; | |
1553 | ||
1554 | case '.': | |
1555 | /* Might be a floating point number. */ | |
1556 | if (lexptr[1] < '0' || lexptr[1] > '9') | |
65d12d83 TT |
1557 | { |
1558 | if (in_parse_field) | |
1559 | last_was_structop = 1; | |
1560 | goto symbol; /* Nope, must be a symbol. */ | |
1561 | } | |
c906108c SS |
1562 | /* FALL THRU into number case. */ |
1563 | ||
1564 | case '0': | |
1565 | case '1': | |
1566 | case '2': | |
1567 | case '3': | |
1568 | case '4': | |
1569 | case '5': | |
1570 | case '6': | |
1571 | case '7': | |
1572 | case '8': | |
1573 | case '9': | |
1574 | { | |
1575 | /* It's a number. */ | |
1576 | int got_dot = 0, got_e = 0, toktype; | |
710122da | 1577 | char *p = tokstart; |
c906108c SS |
1578 | int hex = input_radix > 10; |
1579 | ||
1580 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) | |
1581 | { | |
1582 | p += 2; | |
1583 | hex = 1; | |
1584 | } | |
1585 | else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D')) | |
1586 | { | |
1587 | p += 2; | |
1588 | hex = 0; | |
1589 | } | |
1590 | ||
1591 | for (;; ++p) | |
1592 | { | |
1593 | /* This test includes !hex because 'e' is a valid hex digit | |
1594 | and thus does not indicate a floating point number when | |
1595 | the radix is hex. */ | |
1596 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) | |
1597 | got_dot = got_e = 1; | |
1598 | /* This test does not include !hex, because a '.' always indicates | |
1599 | a decimal floating point number regardless of the radix. */ | |
1600 | else if (!got_dot && *p == '.') | |
1601 | got_dot = 1; | |
1602 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') | |
1603 | && (*p == '-' || *p == '+')) | |
1604 | /* This is the sign of the exponent, not the end of the | |
1605 | number. */ | |
1606 | continue; | |
1607 | /* We will take any letters or digits. parse_number will | |
1608 | complain if past the radix, or if L or U are not final. */ | |
1609 | else if ((*p < '0' || *p > '9') | |
1610 | && ((*p < 'a' || *p > 'z') | |
1611 | && (*p < 'A' || *p > 'Z'))) | |
1612 | break; | |
1613 | } | |
1614 | toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval); | |
1615 | if (toktype == ERROR) | |
1616 | { | |
1617 | char *err_copy = (char *) alloca (p - tokstart + 1); | |
1618 | ||
1619 | memcpy (err_copy, tokstart, p - tokstart); | |
1620 | err_copy[p - tokstart] = 0; | |
1621 | error ("Invalid number \"%s\".", err_copy); | |
1622 | } | |
1623 | lexptr = p; | |
1624 | return toktype; | |
1625 | } | |
1626 | ||
1627 | case '+': | |
1628 | case '-': | |
1629 | case '*': | |
1630 | case '/': | |
1631 | case '%': | |
1632 | case '|': | |
1633 | case '&': | |
1634 | case '^': | |
1635 | case '~': | |
1636 | case '!': | |
1637 | case '@': | |
1638 | case '<': | |
1639 | case '>': | |
1640 | case '[': | |
1641 | case ']': | |
1642 | case '?': | |
1643 | case ':': | |
1644 | case '=': | |
1645 | case '{': | |
1646 | case '}': | |
1647 | symbol: | |
1648 | lexptr++; | |
1649 | return c; | |
1650 | ||
1651 | case '"': | |
1652 | ||
1653 | /* Build the gdb internal form of the input string in tempbuf, | |
1654 | translating any standard C escape forms seen. Note that the | |
1655 | buffer is null byte terminated *only* for the convenience of | |
1656 | debugging gdb itself and printing the buffer contents when | |
1657 | the buffer contains no embedded nulls. Gdb does not depend | |
1658 | upon the buffer being null byte terminated, it uses the length | |
1659 | string instead. This allows gdb to handle C strings (as well | |
1660 | as strings in other languages) with embedded null bytes */ | |
1661 | ||
1662 | tokptr = ++tokstart; | |
1663 | tempbufindex = 0; | |
1664 | ||
1665 | do { | |
234b45d4 KB |
1666 | char *char_start_pos = tokptr; |
1667 | ||
c906108c SS |
1668 | /* Grow the static temp buffer if necessary, including allocating |
1669 | the first one on demand. */ | |
1670 | if (tempbufindex + 1 >= tempbufsize) | |
1671 | { | |
1672 | tempbuf = (char *) realloc (tempbuf, tempbufsize += 64); | |
1673 | } | |
1674 | switch (*tokptr) | |
1675 | { | |
1676 | case '\0': | |
1677 | case '"': | |
1678 | /* Do nothing, loop will terminate. */ | |
1679 | break; | |
1680 | case '\\': | |
1681 | tokptr++; | |
1682 | c = parse_escape (&tokptr); | |
1683 | if (c == -1) | |
1684 | { | |
1685 | continue; | |
1686 | } | |
1687 | tempbuf[tempbufindex++] = c; | |
1688 | break; | |
1689 | default: | |
234b45d4 KB |
1690 | c = *tokptr++; |
1691 | if (! host_char_to_target (c, &c)) | |
1692 | { | |
1693 | int len = tokptr - char_start_pos; | |
1694 | char *copy = alloca (len + 1); | |
1695 | memcpy (copy, char_start_pos, len); | |
1696 | copy[len] = '\0'; | |
1697 | ||
1698 | error ("There is no character corresponding to `%s' " | |
1699 | "in the target character set `%s'.", | |
1700 | copy, target_charset ()); | |
1701 | } | |
1702 | tempbuf[tempbufindex++] = c; | |
c906108c SS |
1703 | break; |
1704 | } | |
1705 | } while ((*tokptr != '"') && (*tokptr != '\0')); | |
1706 | if (*tokptr++ != '"') | |
1707 | { | |
1708 | error ("Unterminated string in expression."); | |
1709 | } | |
1710 | tempbuf[tempbufindex] = '\0'; /* See note above */ | |
1711 | yylval.sval.ptr = tempbuf; | |
1712 | yylval.sval.length = tempbufindex; | |
1713 | lexptr = tokptr; | |
1714 | return (STRING); | |
1715 | } | |
1716 | ||
1717 | if (!(c == '_' || c == '$' | |
1718 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) | |
1719 | /* We must have come across a bad character (e.g. ';'). */ | |
1720 | error ("Invalid character '%c' in expression.", c); | |
1721 | ||
1722 | /* It's a name. See how long it is. */ | |
1723 | namelen = 0; | |
1724 | for (c = tokstart[namelen]; | |
1725 | (c == '_' || c == '$' || (c >= '0' && c <= '9') | |
1726 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');) | |
1727 | { | |
1728 | /* Template parameter lists are part of the name. | |
1729 | FIXME: This mishandles `print $a<4&&$a>3'. */ | |
1730 | ||
1731 | if (c == '<') | |
1732 | { | |
c906108c SS |
1733 | /* Scan ahead to get rest of the template specification. Note |
1734 | that we look ahead only when the '<' adjoins non-whitespace | |
1735 | characters; for comparison expressions, e.g. "a < b > c", | |
1736 | there must be spaces before the '<', etc. */ | |
1737 | ||
1738 | char * p = find_template_name_end (tokstart + namelen); | |
1739 | if (p) | |
1740 | namelen = p - tokstart; | |
1741 | break; | |
c906108c SS |
1742 | } |
1743 | c = tokstart[++namelen]; | |
1744 | } | |
1745 | ||
84f0252a JB |
1746 | /* The token "if" terminates the expression and is NOT removed from |
1747 | the input stream. It doesn't count if it appears in the | |
1748 | expansion of a macro. */ | |
1749 | if (namelen == 2 | |
1750 | && tokstart[0] == 'i' | |
1751 | && tokstart[1] == 'f' | |
1752 | && ! scanning_macro_expansion ()) | |
c906108c SS |
1753 | { |
1754 | return 0; | |
1755 | } | |
1756 | ||
1757 | lexptr += namelen; | |
1758 | ||
1759 | tryname: | |
1760 | ||
1761 | /* Catch specific keywords. Should be done with a data structure. */ | |
1762 | switch (namelen) | |
1763 | { | |
1764 | case 8: | |
bf896cb0 | 1765 | if (strncmp (tokstart, "unsigned", 8) == 0) |
c906108c | 1766 | return UNSIGNED; |
3e79cecf | 1767 | if (parse_language->la_language == language_cplus |
bf896cb0 | 1768 | && strncmp (tokstart, "template", 8) == 0) |
c906108c | 1769 | return TEMPLATE; |
bf896cb0 | 1770 | if (strncmp (tokstart, "volatile", 8) == 0) |
c906108c SS |
1771 | return VOLATILE_KEYWORD; |
1772 | break; | |
1773 | case 6: | |
bf896cb0 | 1774 | if (strncmp (tokstart, "struct", 6) == 0) |
c906108c | 1775 | return STRUCT; |
bf896cb0 | 1776 | if (strncmp (tokstart, "signed", 6) == 0) |
c906108c | 1777 | return SIGNED_KEYWORD; |
bf896cb0 | 1778 | if (strncmp (tokstart, "sizeof", 6) == 0) |
c906108c | 1779 | return SIZEOF; |
bf896cb0 | 1780 | if (strncmp (tokstart, "double", 6) == 0) |
c906108c SS |
1781 | return DOUBLE_KEYWORD; |
1782 | break; | |
1783 | case 5: | |
3e79cecf | 1784 | if (parse_language->la_language == language_cplus) |
c906108c | 1785 | { |
bf896cb0 | 1786 | if (strncmp (tokstart, "false", 5) == 0) |
c906108c | 1787 | return FALSEKEYWORD; |
bf896cb0 | 1788 | if (strncmp (tokstart, "class", 5) == 0) |
c906108c SS |
1789 | return CLASS; |
1790 | } | |
bf896cb0 | 1791 | if (strncmp (tokstart, "union", 5) == 0) |
c906108c | 1792 | return UNION; |
bf896cb0 | 1793 | if (strncmp (tokstart, "short", 5) == 0) |
c906108c | 1794 | return SHORT; |
bf896cb0 | 1795 | if (strncmp (tokstart, "const", 5) == 0) |
c906108c SS |
1796 | return CONST_KEYWORD; |
1797 | break; | |
1798 | case 4: | |
bf896cb0 | 1799 | if (strncmp (tokstart, "enum", 4) == 0) |
c906108c | 1800 | return ENUM; |
bf896cb0 | 1801 | if (strncmp (tokstart, "long", 4) == 0) |
c906108c | 1802 | return LONG; |
3e79cecf | 1803 | if (parse_language->la_language == language_cplus) |
c906108c | 1804 | { |
bf896cb0 | 1805 | if (strncmp (tokstart, "true", 4) == 0) |
c906108c | 1806 | return TRUEKEYWORD; |
c906108c SS |
1807 | } |
1808 | break; | |
1809 | case 3: | |
bf896cb0 | 1810 | if (strncmp (tokstart, "int", 3) == 0) |
c906108c SS |
1811 | return INT_KEYWORD; |
1812 | break; | |
1813 | default: | |
1814 | break; | |
1815 | } | |
1816 | ||
1817 | yylval.sval.ptr = tokstart; | |
1818 | yylval.sval.length = namelen; | |
1819 | ||
1820 | if (*tokstart == '$') | |
1821 | { | |
1822 | write_dollar_variable (yylval.sval); | |
1823 | return VARIABLE; | |
1824 | } | |
1825 | ||
c906108c SS |
1826 | /* Use token-type BLOCKNAME for symbols that happen to be defined as |
1827 | functions or symtabs. If this is not so, then ... | |
1828 | Use token-type TYPENAME for symbols that happen to be defined | |
1829 | currently as names of types; NAME for other symbols. | |
1830 | The caller is not constrained to care about the distinction. */ | |
1831 | { | |
1832 | char *tmp = copy_name (yylval.sval); | |
1833 | struct symbol *sym; | |
1834 | int is_a_field_of_this = 0; | |
1835 | int hextype; | |
1836 | ||
1837 | sym = lookup_symbol (tmp, expression_context_block, | |
176620f1 | 1838 | VAR_DOMAIN, |
3e79cecf | 1839 | parse_language->la_language == language_cplus |
2570f2b7 | 1840 | ? &is_a_field_of_this : (int *) NULL); |
c906108c SS |
1841 | /* Call lookup_symtab, not lookup_partial_symtab, in case there are |
1842 | no psymtabs (coff, xcoff, or some future change to blow away the | |
1843 | psymtabs once once symbols are read). */ | |
1844 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
1845 | { | |
1846 | yylval.ssym.sym = sym; | |
1847 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1848 | return BLOCKNAME; | |
1849 | } | |
1850 | else if (!sym) | |
1851 | { /* See if it's a file name. */ | |
1852 | struct symtab *symtab; | |
1853 | ||
1854 | symtab = lookup_symtab (tmp); | |
1855 | ||
1856 | if (symtab) | |
1857 | { | |
1858 | yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK); | |
1859 | return FILENAME; | |
1860 | } | |
1861 | } | |
1862 | ||
1863 | if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
1864 | { | |
79c2c32d DC |
1865 | /* NOTE: carlton/2003-09-25: There used to be code here to |
1866 | handle nested types. It didn't work very well. See the | |
1867 | comment before qualified_type for more info. */ | |
c906108c | 1868 | yylval.tsym.type = SYMBOL_TYPE (sym); |
c906108c SS |
1869 | return TYPENAME; |
1870 | } | |
54a5b07d | 1871 | yylval.tsym.type |
3e79cecf UW |
1872 | = language_lookup_primitive_type_by_name (parse_language, |
1873 | parse_gdbarch, tmp); | |
54a5b07d | 1874 | if (yylval.tsym.type != NULL) |
47663de5 | 1875 | return TYPENAME; |
c906108c SS |
1876 | |
1877 | /* Input names that aren't symbols but ARE valid hex numbers, | |
1878 | when the input radix permits them, can be names or numbers | |
1879 | depending on the parse. Note we support radixes > 16 here. */ | |
1880 | if (!sym && | |
1881 | ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) || | |
1882 | (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))) | |
1883 | { | |
1884 | YYSTYPE newlval; /* Its value is ignored. */ | |
1885 | hextype = parse_number (tokstart, namelen, 0, &newlval); | |
1886 | if (hextype == INT) | |
1887 | { | |
1888 | yylval.ssym.sym = sym; | |
1889 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1890 | return NAME_OR_INT; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | /* Any other kind of symbol */ | |
1895 | yylval.ssym.sym = sym; | |
1896 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
65d12d83 TT |
1897 | if (in_parse_field && *lexptr == '\0') |
1898 | saw_name_at_eof = 1; | |
c906108c SS |
1899 | return NAME; |
1900 | } | |
1901 | } | |
1902 | ||
65d12d83 TT |
1903 | int |
1904 | c_parse (void) | |
1905 | { | |
1906 | last_was_structop = 0; | |
1907 | saw_name_at_eof = 0; | |
1908 | return yyparse (); | |
1909 | } | |
1910 | ||
c906108c SS |
1911 | void |
1912 | yyerror (msg) | |
1913 | char *msg; | |
1914 | { | |
665132f9 MS |
1915 | if (prev_lexptr) |
1916 | lexptr = prev_lexptr; | |
1917 | ||
c906108c SS |
1918 | error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr); |
1919 | } |