]>
Commit | Line | Data |
---|---|---|
3d6b6a90 JG |
1 | /* YACC parser for C expressions, for GDB. |
2 | Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* Parse a C expression from text in a string, | |
21 | and return the result as a struct expression pointer. | |
22 | That structure contains arithmetic operations in reverse polish, | |
23 | with constants represented by operations that are followed by special data. | |
24 | See expression.h for the details of the format. | |
25 | What is important here is that it can be built up sequentially | |
26 | during the process of parsing; the lower levels of the tree always | |
27 | come first in the result. */ | |
28 | ||
29 | %{ | |
30 | ||
31 | #include <stdio.h> | |
32 | #include <string.h> | |
33 | #include "defs.h" | |
3d6b6a90 | 34 | #include "symtab.h" |
1ab3bf1b | 35 | #include "gdbtypes.h" |
3d6b6a90 JG |
36 | #include "frame.h" |
37 | #include "expression.h" | |
38 | #include "parser-defs.h" | |
39 | #include "value.h" | |
40 | #include "language.h" | |
41 | ||
318bf84f FF |
42 | /* Ensure that if the generated parser contains any calls to malloc/realloc, |
43 | that they get mapped to xmalloc/xrealloc. */ | |
44 | ||
45 | #define malloc xmalloc | |
46 | #define realloc xrealloc | |
47 | ||
3d6b6a90 JG |
48 | /* These MUST be included in any grammar file!!!! |
49 | Please choose unique names! */ | |
d018c8a6 | 50 | #define yymaxdepth c_maxdepth |
3d6b6a90 JG |
51 | #define yyparse c_parse |
52 | #define yylex c_lex | |
53 | #define yyerror c_error | |
54 | #define yylval c_lval | |
55 | #define yychar c_char | |
56 | #define yydebug c_debug | |
57 | #define yypact c_pact | |
58 | #define yyr1 c_r1 | |
59 | #define yyr2 c_r2 | |
60 | #define yydef c_def | |
61 | #define yychk c_chk | |
62 | #define yypgo c_pgo | |
63 | #define yyact c_act | |
64 | #define yyexca c_exca | |
9ce7cb7c SG |
65 | #define yyerrflag c_errflag |
66 | #define yynerrs c_nerrs | |
39bf5952 JG |
67 | #define yyps c_ps |
68 | #define yypv c_pv | |
69 | #define yys c_s | |
d018c8a6 | 70 | #define yy_yys c_yys |
39bf5952 JG |
71 | #define yystate c_state |
72 | #define yytmp c_tmp | |
73 | #define yyv c_v | |
d018c8a6 | 74 | #define yy_yyv c_yyv |
39bf5952 JG |
75 | #define yyval c_val |
76 | #define yylloc c_lloc | |
3d6b6a90 | 77 | |
d26b50b7 | 78 | int |
1ab3bf1b JG |
79 | yyparse PARAMS ((void)); |
80 | ||
81 | int | |
82 | yylex PARAMS ((void)); | |
83 | ||
84 | void | |
85 | yyerror PARAMS ((char *)); | |
3d6b6a90 JG |
86 | |
87 | /* #define YYDEBUG 1 */ | |
88 | ||
89 | %} | |
90 | ||
91 | /* Although the yacc "value" of an expression is not used, | |
92 | since the result is stored in the structure being created, | |
93 | other node types do have values. */ | |
94 | ||
95 | %union | |
96 | { | |
97 | LONGEST lval; | |
98 | unsigned LONGEST ulval; | |
99 | double dval; | |
100 | struct symbol *sym; | |
101 | struct type *tval; | |
102 | struct stoken sval; | |
103 | struct ttype tsym; | |
104 | struct symtoken ssym; | |
105 | int voidval; | |
106 | struct block *bval; | |
107 | enum exp_opcode opcode; | |
108 | struct internalvar *ivar; | |
109 | ||
110 | struct type **tvec; | |
111 | int *ivec; | |
112 | } | |
113 | ||
1ab3bf1b JG |
114 | %{ |
115 | /* YYSTYPE gets defined by %union */ | |
116 | static int | |
117 | parse_number PARAMS ((char *, int, int, YYSTYPE *)); | |
118 | %} | |
119 | ||
01be6913 | 120 | %type <voidval> exp exp1 type_exp start variable qualified_name |
3d6b6a90 JG |
121 | %type <tval> type typebase |
122 | %type <tvec> nonempty_typelist | |
123 | /* %type <bval> block */ | |
124 | ||
125 | /* Fancy type parsing. */ | |
126 | %type <voidval> func_mod direct_abs_decl abs_decl | |
127 | %type <tval> ptype | |
128 | %type <lval> array_mod | |
129 | ||
130 | %token <lval> INT CHAR | |
131 | %token <ulval> UINT | |
132 | %token <dval> FLOAT | |
133 | ||
134 | /* Both NAME and TYPENAME tokens represent symbols in the input, | |
135 | and both convey their data as strings. | |
136 | But a TYPENAME is a string that happens to be defined as a typedef | |
137 | or builtin type name (such as int or char) | |
138 | and a NAME is any other symbol. | |
139 | Contexts where this distinction is not important can use the | |
140 | nonterminal "name", which matches either NAME or TYPENAME. */ | |
141 | ||
142 | %token <sval> STRING | |
143 | %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */ | |
144 | %token <tsym> TYPENAME | |
145 | %type <sval> name | |
146 | %type <ssym> name_not_typename | |
147 | %type <tsym> typename | |
148 | ||
149 | /* A NAME_OR_INT is a symbol which is not known in the symbol table, | |
150 | but which would parse as a valid number in the current input radix. | |
151 | E.g. "c" when input_radix==16. Depending on the parse, it will be | |
152 | turned into a name or into a number. NAME_OR_UINT ditto. */ | |
153 | ||
154 | %token <ssym> NAME_OR_INT NAME_OR_UINT | |
155 | ||
156 | %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON | |
4c53d9ca | 157 | %token TEMPLATE |
3d6b6a90 JG |
158 | %token ERROR |
159 | ||
160 | /* Special type cases, put in to allow the parser to distinguish different | |
161 | legal basetypes. */ | |
088c3a0b | 162 | %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD |
3d6b6a90 JG |
163 | |
164 | %token <lval> LAST REGNAME | |
165 | ||
166 | %token <ivar> VARIABLE | |
167 | ||
168 | %token <opcode> ASSIGN_MODIFY | |
169 | ||
170 | /* C++ */ | |
171 | %token THIS | |
172 | ||
173 | %left ',' | |
174 | %left ABOVE_COMMA | |
175 | %right '=' ASSIGN_MODIFY | |
176 | %right '?' | |
088c3a0b JG |
177 | %left OROR |
178 | %left ANDAND | |
3d6b6a90 JG |
179 | %left '|' |
180 | %left '^' | |
181 | %left '&' | |
182 | %left EQUAL NOTEQUAL | |
183 | %left '<' '>' LEQ GEQ | |
184 | %left LSH RSH | |
185 | %left '@' | |
186 | %left '+' '-' | |
187 | %left '*' '/' '%' | |
188 | %right UNARY INCREMENT DECREMENT | |
189 | %right ARROW '.' '[' '(' | |
190 | %token <ssym> BLOCKNAME | |
191 | %type <bval> block | |
192 | %left COLONCOLON | |
193 | \f | |
194 | %% | |
195 | ||
196 | start : exp1 | |
197 | | type_exp | |
198 | ; | |
199 | ||
200 | type_exp: type | |
201 | { write_exp_elt_opcode(OP_TYPE); | |
202 | write_exp_elt_type($1); | |
203 | write_exp_elt_opcode(OP_TYPE);} | |
204 | ; | |
205 | ||
206 | /* Expressions, including the comma operator. */ | |
207 | exp1 : exp | |
208 | | exp1 ',' exp | |
209 | { write_exp_elt_opcode (BINOP_COMMA); } | |
210 | ; | |
211 | ||
212 | /* Expressions, not including the comma operator. */ | |
213 | exp : '*' exp %prec UNARY | |
214 | { write_exp_elt_opcode (UNOP_IND); } | |
215 | ||
216 | exp : '&' exp %prec UNARY | |
217 | { write_exp_elt_opcode (UNOP_ADDR); } | |
218 | ||
219 | exp : '-' exp %prec UNARY | |
220 | { write_exp_elt_opcode (UNOP_NEG); } | |
221 | ; | |
222 | ||
223 | exp : '!' exp %prec UNARY | |
224 | { write_exp_elt_opcode (UNOP_ZEROP); } | |
225 | ; | |
226 | ||
227 | exp : '~' exp %prec UNARY | |
228 | { write_exp_elt_opcode (UNOP_LOGNOT); } | |
229 | ; | |
230 | ||
231 | exp : INCREMENT exp %prec UNARY | |
232 | { write_exp_elt_opcode (UNOP_PREINCREMENT); } | |
233 | ; | |
234 | ||
235 | exp : DECREMENT exp %prec UNARY | |
236 | { write_exp_elt_opcode (UNOP_PREDECREMENT); } | |
237 | ; | |
238 | ||
239 | exp : exp INCREMENT %prec UNARY | |
240 | { write_exp_elt_opcode (UNOP_POSTINCREMENT); } | |
241 | ; | |
242 | ||
243 | exp : exp DECREMENT %prec UNARY | |
244 | { write_exp_elt_opcode (UNOP_POSTDECREMENT); } | |
245 | ; | |
246 | ||
247 | exp : SIZEOF exp %prec UNARY | |
248 | { write_exp_elt_opcode (UNOP_SIZEOF); } | |
249 | ; | |
250 | ||
251 | exp : exp ARROW name | |
252 | { write_exp_elt_opcode (STRUCTOP_PTR); | |
253 | write_exp_string ($3); | |
254 | write_exp_elt_opcode (STRUCTOP_PTR); } | |
255 | ; | |
256 | ||
01be6913 PB |
257 | exp : exp ARROW qualified_name |
258 | { /* exp->type::name becomes exp->*(&type::name) */ | |
259 | /* Note: this doesn't work if name is a | |
260 | static member! FIXME */ | |
261 | write_exp_elt_opcode (UNOP_ADDR); | |
262 | write_exp_elt_opcode (STRUCTOP_MPTR); } | |
263 | ; | |
3d6b6a90 JG |
264 | exp : exp ARROW '*' exp |
265 | { write_exp_elt_opcode (STRUCTOP_MPTR); } | |
266 | ; | |
267 | ||
268 | exp : exp '.' name | |
269 | { write_exp_elt_opcode (STRUCTOP_STRUCT); | |
270 | write_exp_string ($3); | |
271 | write_exp_elt_opcode (STRUCTOP_STRUCT); } | |
272 | ; | |
273 | ||
01be6913 PB |
274 | exp : exp '.' qualified_name |
275 | { /* exp.type::name becomes exp.*(&type::name) */ | |
276 | /* Note: this doesn't work if name is a | |
277 | static member! FIXME */ | |
278 | write_exp_elt_opcode (UNOP_ADDR); | |
279 | write_exp_elt_opcode (STRUCTOP_MEMBER); } | |
280 | ; | |
281 | ||
3d6b6a90 JG |
282 | exp : exp '.' '*' exp |
283 | { write_exp_elt_opcode (STRUCTOP_MEMBER); } | |
284 | ; | |
285 | ||
286 | exp : exp '[' exp1 ']' | |
287 | { write_exp_elt_opcode (BINOP_SUBSCRIPT); } | |
288 | ; | |
289 | ||
290 | exp : exp '(' | |
291 | /* This is to save the value of arglist_len | |
292 | being accumulated by an outer function call. */ | |
293 | { start_arglist (); } | |
294 | arglist ')' %prec ARROW | |
295 | { write_exp_elt_opcode (OP_FUNCALL); | |
296 | write_exp_elt_longcst ((LONGEST) end_arglist ()); | |
297 | write_exp_elt_opcode (OP_FUNCALL); } | |
298 | ; | |
299 | ||
300 | arglist : | |
301 | ; | |
302 | ||
303 | arglist : exp | |
304 | { arglist_len = 1; } | |
305 | ; | |
306 | ||
307 | arglist : arglist ',' exp %prec ABOVE_COMMA | |
308 | { arglist_len++; } | |
309 | ; | |
310 | ||
311 | exp : '{' type '}' exp %prec UNARY | |
312 | { write_exp_elt_opcode (UNOP_MEMVAL); | |
313 | write_exp_elt_type ($2); | |
314 | write_exp_elt_opcode (UNOP_MEMVAL); } | |
315 | ; | |
316 | ||
317 | exp : '(' type ')' exp %prec UNARY | |
318 | { write_exp_elt_opcode (UNOP_CAST); | |
319 | write_exp_elt_type ($2); | |
320 | write_exp_elt_opcode (UNOP_CAST); } | |
321 | ; | |
322 | ||
323 | exp : '(' exp1 ')' | |
324 | { } | |
325 | ; | |
326 | ||
327 | /* Binary operators in order of decreasing precedence. */ | |
328 | ||
329 | exp : exp '@' exp | |
330 | { write_exp_elt_opcode (BINOP_REPEAT); } | |
331 | ; | |
332 | ||
333 | exp : exp '*' exp | |
334 | { write_exp_elt_opcode (BINOP_MUL); } | |
335 | ; | |
336 | ||
337 | exp : exp '/' exp | |
338 | { write_exp_elt_opcode (BINOP_DIV); } | |
339 | ; | |
340 | ||
341 | exp : exp '%' exp | |
342 | { write_exp_elt_opcode (BINOP_REM); } | |
343 | ; | |
344 | ||
345 | exp : exp '+' exp | |
346 | { write_exp_elt_opcode (BINOP_ADD); } | |
347 | ; | |
348 | ||
349 | exp : exp '-' exp | |
350 | { write_exp_elt_opcode (BINOP_SUB); } | |
351 | ; | |
352 | ||
353 | exp : exp LSH exp | |
354 | { write_exp_elt_opcode (BINOP_LSH); } | |
355 | ; | |
356 | ||
357 | exp : exp RSH exp | |
358 | { write_exp_elt_opcode (BINOP_RSH); } | |
359 | ; | |
360 | ||
361 | exp : exp EQUAL exp | |
362 | { write_exp_elt_opcode (BINOP_EQUAL); } | |
363 | ; | |
364 | ||
365 | exp : exp NOTEQUAL exp | |
366 | { write_exp_elt_opcode (BINOP_NOTEQUAL); } | |
367 | ; | |
368 | ||
369 | exp : exp LEQ exp | |
370 | { write_exp_elt_opcode (BINOP_LEQ); } | |
371 | ; | |
372 | ||
373 | exp : exp GEQ exp | |
374 | { write_exp_elt_opcode (BINOP_GEQ); } | |
375 | ; | |
376 | ||
377 | exp : exp '<' exp | |
378 | { write_exp_elt_opcode (BINOP_LESS); } | |
379 | ; | |
380 | ||
381 | exp : exp '>' exp | |
382 | { write_exp_elt_opcode (BINOP_GTR); } | |
383 | ; | |
384 | ||
385 | exp : exp '&' exp | |
386 | { write_exp_elt_opcode (BINOP_LOGAND); } | |
387 | ; | |
388 | ||
389 | exp : exp '^' exp | |
390 | { write_exp_elt_opcode (BINOP_LOGXOR); } | |
391 | ; | |
392 | ||
393 | exp : exp '|' exp | |
394 | { write_exp_elt_opcode (BINOP_LOGIOR); } | |
395 | ; | |
396 | ||
088c3a0b | 397 | exp : exp ANDAND exp |
3d6b6a90 JG |
398 | { write_exp_elt_opcode (BINOP_AND); } |
399 | ; | |
400 | ||
088c3a0b | 401 | exp : exp OROR exp |
3d6b6a90 JG |
402 | { write_exp_elt_opcode (BINOP_OR); } |
403 | ; | |
404 | ||
405 | exp : exp '?' exp ':' exp %prec '?' | |
406 | { write_exp_elt_opcode (TERNOP_COND); } | |
407 | ; | |
408 | ||
409 | exp : exp '=' exp | |
410 | { write_exp_elt_opcode (BINOP_ASSIGN); } | |
411 | ; | |
412 | ||
413 | exp : exp ASSIGN_MODIFY exp | |
414 | { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); | |
415 | write_exp_elt_opcode ($2); | |
416 | write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); } | |
417 | ; | |
418 | ||
419 | exp : INT | |
420 | { write_exp_elt_opcode (OP_LONG); | |
421 | if ($1 == (int) $1 || $1 == (unsigned int) $1) | |
422 | write_exp_elt_type (builtin_type_int); | |
423 | else | |
424 | write_exp_elt_type (BUILTIN_TYPE_LONGEST); | |
425 | write_exp_elt_longcst ((LONGEST) $1); | |
426 | write_exp_elt_opcode (OP_LONG); } | |
427 | ; | |
428 | ||
429 | exp : NAME_OR_INT | |
430 | { YYSTYPE val; | |
431 | parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val); | |
432 | write_exp_elt_opcode (OP_LONG); | |
433 | if (val.lval == (int) val.lval || | |
434 | val.lval == (unsigned int) val.lval) | |
435 | write_exp_elt_type (builtin_type_int); | |
436 | else | |
437 | write_exp_elt_type (BUILTIN_TYPE_LONGEST); | |
438 | write_exp_elt_longcst (val.lval); | |
439 | write_exp_elt_opcode (OP_LONG); } | |
440 | ; | |
441 | ||
442 | exp : UINT | |
443 | { | |
444 | write_exp_elt_opcode (OP_LONG); | |
445 | if ($1 == (unsigned int) $1) | |
446 | write_exp_elt_type (builtin_type_unsigned_int); | |
447 | else | |
448 | write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST); | |
449 | write_exp_elt_longcst ((LONGEST) $1); | |
450 | write_exp_elt_opcode (OP_LONG); | |
451 | } | |
452 | ; | |
453 | ||
454 | exp : NAME_OR_UINT | |
455 | { YYSTYPE val; | |
456 | parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val); | |
457 | write_exp_elt_opcode (OP_LONG); | |
458 | if (val.ulval == (unsigned int) val.ulval) | |
459 | write_exp_elt_type (builtin_type_unsigned_int); | |
460 | else | |
461 | write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST); | |
462 | write_exp_elt_longcst ((LONGEST)val.ulval); | |
463 | write_exp_elt_opcode (OP_LONG); | |
464 | } | |
465 | ; | |
466 | ||
467 | exp : CHAR | |
468 | { write_exp_elt_opcode (OP_LONG); | |
469 | write_exp_elt_type (builtin_type_char); | |
470 | write_exp_elt_longcst ((LONGEST) $1); | |
471 | write_exp_elt_opcode (OP_LONG); } | |
472 | ; | |
473 | ||
474 | exp : FLOAT | |
475 | { write_exp_elt_opcode (OP_DOUBLE); | |
476 | write_exp_elt_type (builtin_type_double); | |
477 | write_exp_elt_dblcst ($1); | |
478 | write_exp_elt_opcode (OP_DOUBLE); } | |
479 | ; | |
480 | ||
481 | exp : variable | |
482 | ; | |
483 | ||
484 | exp : LAST | |
485 | { write_exp_elt_opcode (OP_LAST); | |
486 | write_exp_elt_longcst ((LONGEST) $1); | |
487 | write_exp_elt_opcode (OP_LAST); } | |
488 | ; | |
489 | ||
490 | exp : REGNAME | |
491 | { write_exp_elt_opcode (OP_REGISTER); | |
492 | write_exp_elt_longcst ((LONGEST) $1); | |
493 | write_exp_elt_opcode (OP_REGISTER); } | |
494 | ; | |
495 | ||
496 | exp : VARIABLE | |
497 | { write_exp_elt_opcode (OP_INTERNALVAR); | |
498 | write_exp_elt_intern ($1); | |
499 | write_exp_elt_opcode (OP_INTERNALVAR); } | |
500 | ; | |
501 | ||
502 | exp : SIZEOF '(' type ')' %prec UNARY | |
503 | { write_exp_elt_opcode (OP_LONG); | |
504 | write_exp_elt_type (builtin_type_int); | |
505 | write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3)); | |
506 | write_exp_elt_opcode (OP_LONG); } | |
507 | ; | |
508 | ||
509 | exp : STRING | |
510 | { write_exp_elt_opcode (OP_STRING); | |
511 | write_exp_string ($1); | |
512 | write_exp_elt_opcode (OP_STRING); } | |
513 | ; | |
514 | ||
515 | /* C++. */ | |
516 | exp : THIS | |
517 | { write_exp_elt_opcode (OP_THIS); | |
518 | write_exp_elt_opcode (OP_THIS); } | |
519 | ; | |
520 | ||
521 | /* end of C++. */ | |
522 | ||
523 | block : BLOCKNAME | |
524 | { | |
525 | if ($1.sym != 0) | |
526 | $$ = SYMBOL_BLOCK_VALUE ($1.sym); | |
527 | else | |
528 | { | |
529 | struct symtab *tem = | |
530 | lookup_symtab (copy_name ($1.stoken)); | |
531 | if (tem) | |
532 | $$ = BLOCKVECTOR_BLOCK | |
533 | (BLOCKVECTOR (tem), STATIC_BLOCK); | |
534 | else | |
535 | error ("No file or function \"%s\".", | |
536 | copy_name ($1.stoken)); | |
537 | } | |
538 | } | |
539 | ; | |
540 | ||
541 | block : block COLONCOLON name | |
542 | { struct symbol *tem | |
543 | = lookup_symbol (copy_name ($3), $1, | |
544 | VAR_NAMESPACE, 0, NULL); | |
545 | if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK) | |
546 | error ("No function \"%s\" in specified context.", | |
547 | copy_name ($3)); | |
548 | $$ = SYMBOL_BLOCK_VALUE (tem); } | |
549 | ; | |
550 | ||
551 | variable: block COLONCOLON name | |
552 | { struct symbol *sym; | |
553 | sym = lookup_symbol (copy_name ($3), $1, | |
554 | VAR_NAMESPACE, 0, NULL); | |
555 | if (sym == 0) | |
556 | error ("No symbol \"%s\" in specified context.", | |
557 | copy_name ($3)); | |
558 | ||
559 | write_exp_elt_opcode (OP_VAR_VALUE); | |
560 | write_exp_elt_sym (sym); | |
561 | write_exp_elt_opcode (OP_VAR_VALUE); } | |
562 | ; | |
563 | ||
01be6913 | 564 | qualified_name: typebase COLONCOLON name |
3d6b6a90 JG |
565 | { |
566 | struct type *type = $1; | |
567 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
568 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
569 | error ("`%s' is not defined as an aggregate type.", | |
570 | TYPE_NAME (type)); | |
571 | ||
572 | write_exp_elt_opcode (OP_SCOPE); | |
573 | write_exp_elt_type (type); | |
574 | write_exp_string ($3); | |
575 | write_exp_elt_opcode (OP_SCOPE); | |
576 | } | |
577 | | typebase COLONCOLON '~' name | |
578 | { | |
579 | struct type *type = $1; | |
01be6913 | 580 | struct stoken tmp_token; |
3d6b6a90 JG |
581 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
582 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
583 | error ("`%s' is not defined as an aggregate type.", | |
584 | TYPE_NAME (type)); | |
585 | ||
586 | if (strcmp (type_name_no_tag (type), $4.ptr)) | |
587 | error ("invalid destructor `%s::~%s'", | |
588 | type_name_no_tag (type), $4.ptr); | |
589 | ||
01be6913 PB |
590 | tmp_token.ptr = (char*) alloca ($4.length + 2); |
591 | tmp_token.length = $4.length + 1; | |
592 | tmp_token.ptr[0] = '~'; | |
593 | memcpy (tmp_token.ptr+1, $4.ptr, $4.length); | |
594 | tmp_token.ptr[tmp_token.length] = 0; | |
3d6b6a90 JG |
595 | write_exp_elt_opcode (OP_SCOPE); |
596 | write_exp_elt_type (type); | |
01be6913 | 597 | write_exp_string (tmp_token); |
3d6b6a90 | 598 | write_exp_elt_opcode (OP_SCOPE); |
3d6b6a90 | 599 | } |
01be6913 PB |
600 | ; |
601 | ||
602 | variable: qualified_name | |
3d6b6a90 JG |
603 | | COLONCOLON name |
604 | { | |
605 | char *name = copy_name ($2); | |
606 | struct symbol *sym; | |
1ab3bf1b | 607 | struct minimal_symbol *msymbol; |
3d6b6a90 JG |
608 | |
609 | sym = | |
610 | lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL); | |
611 | if (sym) | |
612 | { | |
613 | write_exp_elt_opcode (OP_VAR_VALUE); | |
614 | write_exp_elt_sym (sym); | |
615 | write_exp_elt_opcode (OP_VAR_VALUE); | |
616 | break; | |
617 | } | |
3d6b6a90 | 618 | |
1ab3bf1b JG |
619 | msymbol = lookup_minimal_symbol (name, |
620 | (struct objfile *) NULL); | |
621 | if (msymbol != NULL) | |
3d6b6a90 | 622 | { |
3d6b6a90 JG |
623 | write_exp_elt_opcode (OP_LONG); |
624 | write_exp_elt_type (builtin_type_int); | |
1ab3bf1b | 625 | write_exp_elt_longcst ((LONGEST) msymbol -> address); |
3d6b6a90 JG |
626 | write_exp_elt_opcode (OP_LONG); |
627 | write_exp_elt_opcode (UNOP_MEMVAL); | |
1ab3bf1b JG |
628 | if (msymbol -> type == mst_data || |
629 | msymbol -> type == mst_bss) | |
3d6b6a90 | 630 | write_exp_elt_type (builtin_type_int); |
1ab3bf1b | 631 | else if (msymbol -> type == mst_text) |
3d6b6a90 JG |
632 | write_exp_elt_type (lookup_function_type (builtin_type_int)); |
633 | else | |
634 | write_exp_elt_type (builtin_type_char); | |
635 | write_exp_elt_opcode (UNOP_MEMVAL); | |
636 | } | |
637 | else | |
1ab3bf1b | 638 | if (!have_full_symbols () && !have_partial_symbols ()) |
3d6b6a90 JG |
639 | error ("No symbol table is loaded. Use the \"file\" command."); |
640 | else | |
641 | error ("No symbol \"%s\" in current context.", name); | |
642 | } | |
643 | ; | |
644 | ||
645 | variable: name_not_typename | |
646 | { struct symbol *sym = $1.sym; | |
647 | ||
648 | if (sym) | |
649 | { | |
5b0a744f | 650 | switch (SYMBOL_CLASS (sym)) |
3d6b6a90 JG |
651 | { |
652 | case LOC_REGISTER: | |
653 | case LOC_ARG: | |
654 | case LOC_REF_ARG: | |
655 | case LOC_REGPARM: | |
656 | case LOC_LOCAL: | |
657 | case LOC_LOCAL_ARG: | |
658 | if (innermost_block == 0 || | |
659 | contained_in (block_found, | |
660 | innermost_block)) | |
661 | innermost_block = block_found; | |
662 | case LOC_UNDEF: | |
663 | case LOC_CONST: | |
664 | case LOC_STATIC: | |
665 | case LOC_TYPEDEF: | |
666 | case LOC_LABEL: | |
667 | case LOC_BLOCK: | |
668 | case LOC_CONST_BYTES: | |
669 | ||
670 | /* In this case the expression can | |
671 | be evaluated regardless of what | |
672 | frame we are in, so there is no | |
673 | need to check for the | |
674 | innermost_block. These cases are | |
675 | listed so that gcc -Wall will | |
676 | report types that may not have | |
677 | been considered. */ | |
678 | ||
679 | break; | |
680 | } | |
681 | write_exp_elt_opcode (OP_VAR_VALUE); | |
682 | write_exp_elt_sym (sym); | |
683 | write_exp_elt_opcode (OP_VAR_VALUE); | |
684 | } | |
685 | else if ($1.is_a_field_of_this) | |
686 | { | |
687 | /* C++: it hangs off of `this'. Must | |
688 | not inadvertently convert from a method call | |
689 | to data ref. */ | |
690 | if (innermost_block == 0 || | |
691 | contained_in (block_found, innermost_block)) | |
692 | innermost_block = block_found; | |
693 | write_exp_elt_opcode (OP_THIS); | |
694 | write_exp_elt_opcode (OP_THIS); | |
695 | write_exp_elt_opcode (STRUCTOP_PTR); | |
696 | write_exp_string ($1.stoken); | |
697 | write_exp_elt_opcode (STRUCTOP_PTR); | |
698 | } | |
699 | else | |
700 | { | |
1ab3bf1b | 701 | struct minimal_symbol *msymbol; |
3d6b6a90 JG |
702 | register char *arg = copy_name ($1.stoken); |
703 | ||
1ab3bf1b JG |
704 | msymbol = lookup_minimal_symbol (arg, |
705 | (struct objfile *) NULL); | |
706 | if (msymbol != NULL) | |
3d6b6a90 | 707 | { |
3d6b6a90 JG |
708 | write_exp_elt_opcode (OP_LONG); |
709 | write_exp_elt_type (builtin_type_int); | |
1ab3bf1b | 710 | write_exp_elt_longcst ((LONGEST) msymbol -> address); |
3d6b6a90 JG |
711 | write_exp_elt_opcode (OP_LONG); |
712 | write_exp_elt_opcode (UNOP_MEMVAL); | |
1ab3bf1b JG |
713 | if (msymbol -> type == mst_data || |
714 | msymbol -> type == mst_bss) | |
3d6b6a90 | 715 | write_exp_elt_type (builtin_type_int); |
1ab3bf1b | 716 | else if (msymbol -> type == mst_text) |
3d6b6a90 JG |
717 | write_exp_elt_type (lookup_function_type (builtin_type_int)); |
718 | else | |
719 | write_exp_elt_type (builtin_type_char); | |
720 | write_exp_elt_opcode (UNOP_MEMVAL); | |
721 | } | |
1ab3bf1b | 722 | else if (!have_full_symbols () && !have_partial_symbols ()) |
3d6b6a90 JG |
723 | error ("No symbol table is loaded. Use the \"file\" command."); |
724 | else | |
725 | error ("No symbol \"%s\" in current context.", | |
726 | copy_name ($1.stoken)); | |
727 | } | |
728 | } | |
729 | ; | |
730 | ||
731 | ||
732 | ptype : typebase | |
733 | | typebase abs_decl | |
734 | { | |
735 | /* This is where the interesting stuff happens. */ | |
736 | int done = 0; | |
737 | int array_size; | |
738 | struct type *follow_type = $1; | |
739 | ||
740 | while (!done) | |
741 | switch (pop_type ()) | |
742 | { | |
743 | case tp_end: | |
744 | done = 1; | |
745 | break; | |
746 | case tp_pointer: | |
747 | follow_type = lookup_pointer_type (follow_type); | |
748 | break; | |
749 | case tp_reference: | |
750 | follow_type = lookup_reference_type (follow_type); | |
751 | break; | |
752 | case tp_array: | |
753 | array_size = pop_type_int (); | |
754 | if (array_size != -1) | |
755 | follow_type = create_array_type (follow_type, | |
756 | array_size); | |
757 | else | |
758 | follow_type = lookup_pointer_type (follow_type); | |
759 | break; | |
760 | case tp_function: | |
761 | follow_type = lookup_function_type (follow_type); | |
762 | break; | |
763 | } | |
764 | $$ = follow_type; | |
765 | } | |
766 | ; | |
767 | ||
768 | abs_decl: '*' | |
769 | { push_type (tp_pointer); $$ = 0; } | |
770 | | '*' abs_decl | |
771 | { push_type (tp_pointer); $$ = $2; } | |
772 | | '&' | |
773 | { push_type (tp_reference); $$ = 0; } | |
774 | | '&' abs_decl | |
775 | { push_type (tp_reference); $$ = $2; } | |
776 | | direct_abs_decl | |
777 | ; | |
778 | ||
779 | direct_abs_decl: '(' abs_decl ')' | |
780 | { $$ = $2; } | |
781 | | direct_abs_decl array_mod | |
782 | { | |
783 | push_type_int ($2); | |
784 | push_type (tp_array); | |
785 | } | |
786 | | array_mod | |
787 | { | |
788 | push_type_int ($1); | |
789 | push_type (tp_array); | |
790 | $$ = 0; | |
791 | } | |
792 | | direct_abs_decl func_mod | |
793 | { push_type (tp_function); } | |
794 | | func_mod | |
795 | { push_type (tp_function); } | |
796 | ; | |
797 | ||
798 | array_mod: '[' ']' | |
799 | { $$ = -1; } | |
800 | | '[' INT ']' | |
801 | { $$ = $2; } | |
802 | ; | |
803 | ||
804 | func_mod: '(' ')' | |
805 | { $$ = 0; } | |
0e2a896c | 806 | | '(' nonempty_typelist ')' |
be772100 | 807 | { free ((PTR)$2); $$ = 0; } |
3d6b6a90 JG |
808 | ; |
809 | ||
810 | type : ptype | |
811 | | typebase COLONCOLON '*' | |
812 | { $$ = lookup_member_type (builtin_type_int, $1); } | |
813 | | type '(' typebase COLONCOLON '*' ')' | |
814 | { $$ = lookup_member_type ($1, $3); } | |
815 | | type '(' typebase COLONCOLON '*' ')' '(' ')' | |
816 | { $$ = lookup_member_type | |
817 | (lookup_function_type ($1), $3); } | |
818 | | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')' | |
819 | { $$ = lookup_member_type | |
820 | (lookup_function_type ($1), $3); | |
be772100 | 821 | free ((PTR)$8); } |
3d6b6a90 JG |
822 | ; |
823 | ||
824 | typebase | |
825 | : TYPENAME | |
826 | { $$ = $1.type; } | |
827 | | INT_KEYWORD | |
828 | { $$ = builtin_type_int; } | |
829 | | LONG | |
830 | { $$ = builtin_type_long; } | |
831 | | SHORT | |
832 | { $$ = builtin_type_short; } | |
833 | | LONG INT_KEYWORD | |
834 | { $$ = builtin_type_long; } | |
835 | | UNSIGNED LONG INT_KEYWORD | |
836 | { $$ = builtin_type_unsigned_long; } | |
837 | | LONG LONG | |
838 | { $$ = builtin_type_long_long; } | |
839 | | LONG LONG INT_KEYWORD | |
840 | { $$ = builtin_type_long_long; } | |
841 | | UNSIGNED LONG LONG | |
842 | { $$ = builtin_type_unsigned_long_long; } | |
843 | | UNSIGNED LONG LONG INT_KEYWORD | |
844 | { $$ = builtin_type_unsigned_long_long; } | |
845 | | SHORT INT_KEYWORD | |
846 | { $$ = builtin_type_short; } | |
847 | | UNSIGNED SHORT INT_KEYWORD | |
848 | { $$ = builtin_type_unsigned_short; } | |
849 | | STRUCT name | |
850 | { $$ = lookup_struct (copy_name ($2), | |
851 | expression_context_block); } | |
852 | | UNION name | |
853 | { $$ = lookup_union (copy_name ($2), | |
854 | expression_context_block); } | |
855 | | ENUM name | |
856 | { $$ = lookup_enum (copy_name ($2), | |
857 | expression_context_block); } | |
858 | | UNSIGNED typename | |
859 | { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); } | |
860 | | UNSIGNED | |
861 | { $$ = builtin_type_unsigned_int; } | |
088c3a0b | 862 | | SIGNED_KEYWORD typename |
3d6b6a90 | 863 | { $$ = $2.type; } |
088c3a0b | 864 | | SIGNED_KEYWORD |
3d6b6a90 | 865 | { $$ = builtin_type_int; } |
4c53d9ca DHW |
866 | | TEMPLATE name '<' type '>' |
867 | { $$ = lookup_template_type(copy_name($2), $4, | |
868 | expression_context_block); | |
869 | } | |
3d6b6a90 JG |
870 | ; |
871 | ||
872 | typename: TYPENAME | |
873 | | INT_KEYWORD | |
874 | { | |
875 | $$.stoken.ptr = "int"; | |
876 | $$.stoken.length = 3; | |
877 | $$.type = builtin_type_int; | |
878 | } | |
879 | | LONG | |
880 | { | |
881 | $$.stoken.ptr = "long"; | |
882 | $$.stoken.length = 4; | |
883 | $$.type = builtin_type_long; | |
884 | } | |
885 | | SHORT | |
886 | { | |
887 | $$.stoken.ptr = "short"; | |
888 | $$.stoken.length = 5; | |
889 | $$.type = builtin_type_short; | |
890 | } | |
891 | ; | |
892 | ||
893 | nonempty_typelist | |
894 | : type | |
895 | { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2); | |
896 | $$[0] = (struct type *)0; | |
897 | $$[1] = $1; | |
898 | } | |
899 | | nonempty_typelist ',' type | |
900 | { int len = sizeof (struct type *) * ++($<ivec>1[0]); | |
1ab3bf1b | 901 | $$ = (struct type **)xrealloc ((char *) $1, len); |
3d6b6a90 JG |
902 | $$[$<ivec>$[0]] = $3; |
903 | } | |
904 | ; | |
905 | ||
906 | name : NAME { $$ = $1.stoken; } | |
907 | | BLOCKNAME { $$ = $1.stoken; } | |
908 | | TYPENAME { $$ = $1.stoken; } | |
909 | | NAME_OR_INT { $$ = $1.stoken; } | |
910 | | NAME_OR_UINT { $$ = $1.stoken; } | |
911 | ; | |
912 | ||
913 | name_not_typename : NAME | |
914 | | BLOCKNAME | |
915 | /* These would be useful if name_not_typename was useful, but it is just | |
916 | a fake for "variable", so these cause reduce/reduce conflicts because | |
917 | the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable, | |
918 | =exp) or just an exp. If name_not_typename was ever used in an lvalue | |
919 | context where only a name could occur, this might be useful. | |
920 | | NAME_OR_INT | |
921 | | NAME_OR_UINT | |
922 | */ | |
923 | ; | |
924 | ||
925 | %% | |
926 | ||
927 | /* Take care of parsing a number (anything that starts with a digit). | |
928 | Set yylval and return the token type; update lexptr. | |
929 | LEN is the number of characters in it. */ | |
930 | ||
931 | /*** Needs some error checking for the float case ***/ | |
932 | ||
933 | static int | |
934 | parse_number (p, len, parsed_float, putithere) | |
935 | register char *p; | |
936 | register int len; | |
937 | int parsed_float; | |
938 | YYSTYPE *putithere; | |
939 | { | |
940 | register LONGEST n = 0; | |
941 | register LONGEST prevn = 0; | |
942 | register int i; | |
943 | register int c; | |
944 | register int base = input_radix; | |
945 | int unsigned_p = 0; | |
946 | ||
3d6b6a90 JG |
947 | if (parsed_float) |
948 | { | |
949 | /* It's a float since it contains a point or an exponent. */ | |
950 | putithere->dval = atof (p); | |
951 | return FLOAT; | |
952 | } | |
953 | ||
954 | /* Handle base-switching prefixes 0x, 0t, 0d, 0 */ | |
955 | if (p[0] == '0') | |
956 | switch (p[1]) | |
957 | { | |
958 | case 'x': | |
959 | case 'X': | |
960 | if (len >= 3) | |
961 | { | |
962 | p += 2; | |
963 | base = 16; | |
964 | len -= 2; | |
965 | } | |
966 | break; | |
967 | ||
968 | case 't': | |
969 | case 'T': | |
970 | case 'd': | |
971 | case 'D': | |
972 | if (len >= 3) | |
973 | { | |
974 | p += 2; | |
975 | base = 10; | |
976 | len -= 2; | |
977 | } | |
978 | break; | |
979 | ||
980 | default: | |
981 | base = 8; | |
982 | break; | |
983 | } | |
984 | ||
985 | while (len-- > 0) | |
986 | { | |
987 | c = *p++; | |
988 | if (c >= 'A' && c <= 'Z') | |
989 | c += 'a' - 'A'; | |
990 | if (c != 'l' && c != 'u') | |
991 | n *= base; | |
992 | if (c >= '0' && c <= '9') | |
993 | n += i = c - '0'; | |
994 | else | |
995 | { | |
996 | if (base > 10 && c >= 'a' && c <= 'f') | |
997 | n += i = c - 'a' + 10; | |
998 | else if (len == 0 && c == 'l') | |
999 | ; | |
1000 | else if (len == 0 && c == 'u') | |
1001 | unsigned_p = 1; | |
1002 | else | |
1003 | return ERROR; /* Char not a digit */ | |
1004 | } | |
1005 | if (i >= base) | |
1006 | return ERROR; /* Invalid digit in this base */ | |
2a5ec41d JG |
1007 | /* Portably test for overflow (only works for nonzero values, so make |
1008 | a second check for zero). */ | |
1009 | if((prevn >= n) && n != 0) | |
3d6b6a90 | 1010 | unsigned_p=1; /* Try something unsigned */ |
2a5ec41d | 1011 | /* If range checking enabled, portably test for unsigned overflow. */ |
3d6b6a90 JG |
1012 | if(RANGE_CHECK && n!=0) |
1013 | { | |
1014 | if((unsigned_p && (unsigned)prevn >= (unsigned)n)) | |
1015 | range_error("Overflow on numeric constant."); | |
1016 | } | |
1017 | prevn=n; | |
1018 | } | |
1019 | ||
1020 | if (unsigned_p) | |
1021 | { | |
1022 | putithere->ulval = n; | |
1023 | return UINT; | |
1024 | } | |
1025 | else | |
1026 | { | |
1027 | putithere->lval = n; | |
1028 | return INT; | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | struct token | |
1033 | { | |
1034 | char *operator; | |
1035 | int token; | |
1036 | enum exp_opcode opcode; | |
1037 | }; | |
1038 | ||
1039 | const static struct token tokentab3[] = | |
1040 | { | |
1041 | {">>=", ASSIGN_MODIFY, BINOP_RSH}, | |
1042 | {"<<=", ASSIGN_MODIFY, BINOP_LSH} | |
1043 | }; | |
1044 | ||
1045 | const static struct token tokentab2[] = | |
1046 | { | |
1047 | {"+=", ASSIGN_MODIFY, BINOP_ADD}, | |
1048 | {"-=", ASSIGN_MODIFY, BINOP_SUB}, | |
1049 | {"*=", ASSIGN_MODIFY, BINOP_MUL}, | |
1050 | {"/=", ASSIGN_MODIFY, BINOP_DIV}, | |
1051 | {"%=", ASSIGN_MODIFY, BINOP_REM}, | |
1052 | {"|=", ASSIGN_MODIFY, BINOP_LOGIOR}, | |
1053 | {"&=", ASSIGN_MODIFY, BINOP_LOGAND}, | |
1054 | {"^=", ASSIGN_MODIFY, BINOP_LOGXOR}, | |
1055 | {"++", INCREMENT, BINOP_END}, | |
1056 | {"--", DECREMENT, BINOP_END}, | |
1057 | {"->", ARROW, BINOP_END}, | |
088c3a0b JG |
1058 | {"&&", ANDAND, BINOP_END}, |
1059 | {"||", OROR, BINOP_END}, | |
3d6b6a90 JG |
1060 | {"::", COLONCOLON, BINOP_END}, |
1061 | {"<<", LSH, BINOP_END}, | |
1062 | {">>", RSH, BINOP_END}, | |
1063 | {"==", EQUAL, BINOP_END}, | |
1064 | {"!=", NOTEQUAL, BINOP_END}, | |
1065 | {"<=", LEQ, BINOP_END}, | |
1066 | {">=", GEQ, BINOP_END} | |
1067 | }; | |
1068 | ||
1069 | /* Read one token, getting characters through lexptr. */ | |
1070 | ||
1071 | int | |
1072 | yylex () | |
1073 | { | |
1074 | register int c; | |
1075 | register int namelen; | |
1076 | register unsigned i; | |
1077 | register char *tokstart; | |
1078 | ||
1079 | retry: | |
1080 | ||
1081 | tokstart = lexptr; | |
1082 | /* See if it is a special token of length 3. */ | |
1083 | for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) | |
1084 | if (!strncmp (tokstart, tokentab3[i].operator, 3)) | |
1085 | { | |
1086 | lexptr += 3; | |
1087 | yylval.opcode = tokentab3[i].opcode; | |
1088 | return tokentab3[i].token; | |
1089 | } | |
1090 | ||
1091 | /* See if it is a special token of length 2. */ | |
1092 | for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) | |
1093 | if (!strncmp (tokstart, tokentab2[i].operator, 2)) | |
1094 | { | |
1095 | lexptr += 2; | |
1096 | yylval.opcode = tokentab2[i].opcode; | |
1097 | return tokentab2[i].token; | |
1098 | } | |
1099 | ||
1100 | switch (c = *tokstart) | |
1101 | { | |
1102 | case 0: | |
1103 | return 0; | |
1104 | ||
1105 | case ' ': | |
1106 | case '\t': | |
1107 | case '\n': | |
1108 | lexptr++; | |
1109 | goto retry; | |
1110 | ||
1111 | case '\'': | |
1112 | lexptr++; | |
1113 | c = *lexptr++; | |
1114 | if (c == '\\') | |
1115 | c = parse_escape (&lexptr); | |
1116 | yylval.lval = c; | |
1117 | c = *lexptr++; | |
1118 | if (c != '\'') | |
1119 | error ("Invalid character constant."); | |
1120 | return CHAR; | |
1121 | ||
1122 | case '(': | |
1123 | paren_depth++; | |
1124 | lexptr++; | |
1125 | return c; | |
1126 | ||
1127 | case ')': | |
1128 | if (paren_depth == 0) | |
1129 | return 0; | |
1130 | paren_depth--; | |
1131 | lexptr++; | |
1132 | return c; | |
1133 | ||
1134 | case ',': | |
1135 | if (comma_terminates && paren_depth == 0) | |
1136 | return 0; | |
1137 | lexptr++; | |
1138 | return c; | |
1139 | ||
1140 | case '.': | |
1141 | /* Might be a floating point number. */ | |
1142 | if (lexptr[1] < '0' || lexptr[1] > '9') | |
1143 | goto symbol; /* Nope, must be a symbol. */ | |
1144 | /* FALL THRU into number case. */ | |
1145 | ||
1146 | case '0': | |
1147 | case '1': | |
1148 | case '2': | |
1149 | case '3': | |
1150 | case '4': | |
1151 | case '5': | |
1152 | case '6': | |
1153 | case '7': | |
1154 | case '8': | |
1155 | case '9': | |
1156 | { | |
1157 | /* It's a number. */ | |
1158 | int got_dot = 0, got_e = 0, toktype; | |
1159 | register char *p = tokstart; | |
1160 | int hex = input_radix > 10; | |
1161 | ||
1162 | if (c == '0' && (p[1] == 'x' || p[1] == 'X')) | |
1163 | { | |
1164 | p += 2; | |
1165 | hex = 1; | |
1166 | } | |
1167 | else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D')) | |
1168 | { | |
1169 | p += 2; | |
1170 | hex = 0; | |
1171 | } | |
1172 | ||
1173 | for (;; ++p) | |
1174 | { | |
1175 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) | |
1176 | got_dot = got_e = 1; | |
1177 | else if (!hex && !got_dot && *p == '.') | |
1178 | got_dot = 1; | |
1179 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') | |
1180 | && (*p == '-' || *p == '+')) | |
1181 | /* This is the sign of the exponent, not the end of the | |
1182 | number. */ | |
1183 | continue; | |
1184 | /* We will take any letters or digits. parse_number will | |
1185 | complain if past the radix, or if L or U are not final. */ | |
1186 | else if ((*p < '0' || *p > '9') | |
1187 | && ((*p < 'a' || *p > 'z') | |
1188 | && (*p < 'A' || *p > 'Z'))) | |
1189 | break; | |
1190 | } | |
1191 | toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval); | |
1192 | if (toktype == ERROR) | |
1193 | { | |
1194 | char *err_copy = (char *) alloca (p - tokstart + 1); | |
1195 | ||
1196 | bcopy (tokstart, err_copy, p - tokstart); | |
1197 | err_copy[p - tokstart] = 0; | |
1198 | error ("Invalid number \"%s\".", err_copy); | |
1199 | } | |
1200 | lexptr = p; | |
1201 | return toktype; | |
1202 | } | |
1203 | ||
1204 | case '+': | |
1205 | case '-': | |
1206 | case '*': | |
1207 | case '/': | |
1208 | case '%': | |
1209 | case '|': | |
1210 | case '&': | |
1211 | case '^': | |
1212 | case '~': | |
1213 | case '!': | |
1214 | case '@': | |
1215 | case '<': | |
1216 | case '>': | |
1217 | case '[': | |
1218 | case ']': | |
1219 | case '?': | |
1220 | case ':': | |
1221 | case '=': | |
1222 | case '{': | |
1223 | case '}': | |
1224 | symbol: | |
1225 | lexptr++; | |
1226 | return c; | |
1227 | ||
1228 | case '"': | |
1229 | for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++) | |
1230 | if (c == '\\') | |
1231 | { | |
1232 | c = tokstart[++namelen]; | |
1233 | if (c >= '0' && c <= '9') | |
1234 | { | |
1235 | c = tokstart[++namelen]; | |
1236 | if (c >= '0' && c <= '9') | |
1237 | c = tokstart[++namelen]; | |
1238 | } | |
1239 | } | |
1240 | yylval.sval.ptr = tokstart + 1; | |
1241 | yylval.sval.length = namelen - 1; | |
1242 | lexptr += namelen + 1; | |
1243 | return STRING; | |
1244 | } | |
1245 | ||
1246 | if (!(c == '_' || c == '$' | |
1247 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) | |
1248 | /* We must have come across a bad character (e.g. ';'). */ | |
1249 | error ("Invalid character '%c' in expression.", c); | |
1250 | ||
1251 | /* It's a name. See how long it is. */ | |
1252 | namelen = 0; | |
1253 | for (c = tokstart[namelen]; | |
1254 | (c == '_' || c == '$' || (c >= '0' && c <= '9') | |
1255 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); | |
1256 | c = tokstart[++namelen]) | |
1257 | ; | |
1258 | ||
1259 | /* The token "if" terminates the expression and is NOT | |
1260 | removed from the input stream. */ | |
1261 | if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f') | |
1262 | { | |
1263 | return 0; | |
1264 | } | |
1265 | ||
1266 | lexptr += namelen; | |
1267 | ||
1268 | /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1) | |
1269 | and $$digits (equivalent to $<-digits> if you could type that). | |
1270 | Make token type LAST, and put the number (the digits) in yylval. */ | |
1271 | ||
1272 | if (*tokstart == '$') | |
1273 | { | |
1274 | register int negate = 0; | |
1275 | c = 1; | |
1276 | /* Double dollar means negate the number and add -1 as well. | |
1277 | Thus $$ alone means -1. */ | |
1278 | if (namelen >= 2 && tokstart[1] == '$') | |
1279 | { | |
1280 | negate = 1; | |
1281 | c = 2; | |
1282 | } | |
1283 | if (c == namelen) | |
1284 | { | |
1285 | /* Just dollars (one or two) */ | |
1286 | yylval.lval = - negate; | |
1287 | return LAST; | |
1288 | } | |
1289 | /* Is the rest of the token digits? */ | |
1290 | for (; c < namelen; c++) | |
1291 | if (!(tokstart[c] >= '0' && tokstart[c] <= '9')) | |
1292 | break; | |
1293 | if (c == namelen) | |
1294 | { | |
1295 | yylval.lval = atoi (tokstart + 1 + negate); | |
1296 | if (negate) | |
1297 | yylval.lval = - yylval.lval; | |
1298 | return LAST; | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | /* Handle tokens that refer to machine registers: | |
1303 | $ followed by a register name. */ | |
1304 | ||
1305 | if (*tokstart == '$') { | |
1306 | for (c = 0; c < NUM_REGS; c++) | |
1307 | if (namelen - 1 == strlen (reg_names[c]) | |
1308 | && !strncmp (tokstart + 1, reg_names[c], namelen - 1)) | |
1309 | { | |
1310 | yylval.lval = c; | |
1311 | return REGNAME; | |
1312 | } | |
1313 | for (c = 0; c < num_std_regs; c++) | |
1314 | if (namelen - 1 == strlen (std_regs[c].name) | |
1315 | && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1)) | |
1316 | { | |
1317 | yylval.lval = std_regs[c].regnum; | |
1318 | return REGNAME; | |
1319 | } | |
1320 | } | |
1321 | /* Catch specific keywords. Should be done with a data structure. */ | |
1322 | switch (namelen) | |
1323 | { | |
1324 | case 8: | |
1325 | if (!strncmp (tokstart, "unsigned", 8)) | |
1326 | return UNSIGNED; | |
5a4e7215 JG |
1327 | if (current_language->la_language == language_cplus |
1328 | && !strncmp (tokstart, "template", 8)) | |
4c53d9ca | 1329 | return TEMPLATE; |
3d6b6a90 JG |
1330 | break; |
1331 | case 6: | |
1332 | if (!strncmp (tokstart, "struct", 6)) | |
1333 | return STRUCT; | |
1334 | if (!strncmp (tokstart, "signed", 6)) | |
088c3a0b | 1335 | return SIGNED_KEYWORD; |
3d6b6a90 JG |
1336 | if (!strncmp (tokstart, "sizeof", 6)) |
1337 | return SIZEOF; | |
1338 | break; | |
1339 | case 5: | |
1340 | if (!strncmp (tokstart, "union", 5)) | |
1341 | return UNION; | |
1342 | if (!strncmp (tokstart, "short", 5)) | |
1343 | return SHORT; | |
1344 | break; | |
1345 | case 4: | |
1346 | if (!strncmp (tokstart, "enum", 4)) | |
1347 | return ENUM; | |
1348 | if (!strncmp (tokstart, "long", 4)) | |
1349 | return LONG; | |
5a4e7215 JG |
1350 | if (current_language->la_language == language_cplus |
1351 | && !strncmp (tokstart, "this", 4)) | |
3d6b6a90 JG |
1352 | { |
1353 | static const char this_name[] = | |
1354 | { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' }; | |
1355 | ||
1356 | if (lookup_symbol (this_name, expression_context_block, | |
1357 | VAR_NAMESPACE, 0, NULL)) | |
1358 | return THIS; | |
1359 | } | |
1360 | break; | |
1361 | case 3: | |
1362 | if (!strncmp (tokstart, "int", 3)) | |
1363 | return INT_KEYWORD; | |
1364 | break; | |
1365 | default: | |
1366 | break; | |
1367 | } | |
1368 | ||
1369 | yylval.sval.ptr = tokstart; | |
1370 | yylval.sval.length = namelen; | |
1371 | ||
1372 | /* Any other names starting in $ are debugger internal variables. */ | |
1373 | ||
1374 | if (*tokstart == '$') | |
1375 | { | |
1376 | yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1); | |
1377 | return VARIABLE; | |
1378 | } | |
1379 | ||
1380 | /* Use token-type BLOCKNAME for symbols that happen to be defined as | |
1381 | functions or symtabs. If this is not so, then ... | |
1382 | Use token-type TYPENAME for symbols that happen to be defined | |
1383 | currently as names of types; NAME for other symbols. | |
1384 | The caller is not constrained to care about the distinction. */ | |
1385 | { | |
1386 | char *tmp = copy_name (yylval.sval); | |
1387 | struct symbol *sym; | |
1388 | int is_a_field_of_this = 0; | |
1389 | int hextype; | |
1390 | ||
1391 | sym = lookup_symbol (tmp, expression_context_block, | |
545af6ce PB |
1392 | VAR_NAMESPACE, |
1393 | current_language->la_language == language_cplus | |
1394 | ? &is_a_field_of_this : NULL, | |
1395 | NULL); | |
3d6b6a90 JG |
1396 | if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) || |
1397 | lookup_partial_symtab (tmp)) | |
1398 | { | |
1399 | yylval.ssym.sym = sym; | |
1400 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1401 | return BLOCKNAME; | |
1402 | } | |
1403 | if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF) | |
1404 | { | |
1405 | yylval.tsym.type = SYMBOL_TYPE (sym); | |
1406 | return TYPENAME; | |
1407 | } | |
1408 | if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0) | |
1409 | return TYPENAME; | |
1410 | ||
1411 | /* Input names that aren't symbols but ARE valid hex numbers, | |
1412 | when the input radix permits them, can be names or numbers | |
1413 | depending on the parse. Note we support radixes > 16 here. */ | |
1414 | if (!sym && | |
1415 | ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) || | |
1416 | (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10))) | |
1417 | { | |
1418 | YYSTYPE newlval; /* Its value is ignored. */ | |
1419 | hextype = parse_number (tokstart, namelen, 0, &newlval); | |
1420 | if (hextype == INT) | |
1421 | { | |
1422 | yylval.ssym.sym = sym; | |
1423 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1424 | return NAME_OR_INT; | |
1425 | } | |
1426 | if (hextype == UINT) | |
1427 | { | |
1428 | yylval.ssym.sym = sym; | |
1429 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1430 | return NAME_OR_UINT; | |
1431 | } | |
1432 | } | |
1433 | ||
1434 | /* Any other kind of symbol */ | |
1435 | yylval.ssym.sym = sym; | |
1436 | yylval.ssym.is_a_field_of_this = is_a_field_of_this; | |
1437 | return NAME; | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | void | |
1442 | yyerror (msg) | |
1443 | char *msg; | |
1444 | { | |
d671e293 | 1445 | error (msg ? msg : "Invalid syntax in expression."); |
3d6b6a90 JG |
1446 | } |
1447 | \f | |
1448 | /* Table mapping opcodes into strings for printing operators | |
1449 | and precedences of the operators. */ | |
1450 | ||
1451 | const static struct op_print c_op_print_tab[] = | |
1452 | { | |
1453 | {",", BINOP_COMMA, PREC_COMMA, 0}, | |
1454 | {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, | |
1455 | {"||", BINOP_OR, PREC_OR, 0}, | |
1456 | {"&&", BINOP_AND, PREC_AND, 0}, | |
1457 | {"|", BINOP_LOGIOR, PREC_LOGIOR, 0}, | |
1458 | {"&", BINOP_LOGAND, PREC_LOGAND, 0}, | |
1459 | {"^", BINOP_LOGXOR, PREC_LOGXOR, 0}, | |
1460 | {"==", BINOP_EQUAL, PREC_EQUAL, 0}, | |
1461 | {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0}, | |
1462 | {"<=", BINOP_LEQ, PREC_ORDER, 0}, | |
1463 | {">=", BINOP_GEQ, PREC_ORDER, 0}, | |
1464 | {">", BINOP_GTR, PREC_ORDER, 0}, | |
1465 | {"<", BINOP_LESS, PREC_ORDER, 0}, | |
1466 | {">>", BINOP_RSH, PREC_SHIFT, 0}, | |
1467 | {"<<", BINOP_LSH, PREC_SHIFT, 0}, | |
1468 | {"+", BINOP_ADD, PREC_ADD, 0}, | |
1469 | {"-", BINOP_SUB, PREC_ADD, 0}, | |
1470 | {"*", BINOP_MUL, PREC_MUL, 0}, | |
1471 | {"/", BINOP_DIV, PREC_MUL, 0}, | |
1472 | {"%", BINOP_REM, PREC_MUL, 0}, | |
1473 | {"@", BINOP_REPEAT, PREC_REPEAT, 0}, | |
1474 | {"-", UNOP_NEG, PREC_PREFIX, 0}, | |
1475 | {"!", UNOP_ZEROP, PREC_PREFIX, 0}, | |
1476 | {"~", UNOP_LOGNOT, PREC_PREFIX, 0}, | |
1477 | {"*", UNOP_IND, PREC_PREFIX, 0}, | |
1478 | {"&", UNOP_ADDR, PREC_PREFIX, 0}, | |
1479 | {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0}, | |
1480 | {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0}, | |
1481 | {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0}, | |
1482 | /* C++ */ | |
1483 | {"::", BINOP_SCOPE, PREC_PREFIX, 0}, | |
1484 | }; | |
1485 | \f | |
1486 | /* These variables point to the objects | |
1487 | representing the predefined C data types. */ | |
1488 | ||
1489 | struct type *builtin_type_void; | |
1490 | struct type *builtin_type_char; | |
1491 | struct type *builtin_type_short; | |
1492 | struct type *builtin_type_int; | |
1493 | struct type *builtin_type_long; | |
1494 | struct type *builtin_type_long_long; | |
04f27ddc | 1495 | struct type *builtin_type_signed_char; |
3d6b6a90 JG |
1496 | struct type *builtin_type_unsigned_char; |
1497 | struct type *builtin_type_unsigned_short; | |
1498 | struct type *builtin_type_unsigned_int; | |
1499 | struct type *builtin_type_unsigned_long; | |
1500 | struct type *builtin_type_unsigned_long_long; | |
1501 | struct type *builtin_type_float; | |
1502 | struct type *builtin_type_double; | |
e2aab031 FF |
1503 | struct type *builtin_type_long_double; |
1504 | struct type *builtin_type_complex; | |
1505 | struct type *builtin_type_double_complex; | |
3d6b6a90 | 1506 | |
9dffe475 | 1507 | struct type ** const (c_builtin_types[]) = |
3d6b6a90 JG |
1508 | { |
1509 | &builtin_type_int, | |
1510 | &builtin_type_long, | |
1511 | &builtin_type_short, | |
1512 | &builtin_type_char, | |
1513 | &builtin_type_float, | |
1514 | &builtin_type_double, | |
1515 | &builtin_type_void, | |
1516 | &builtin_type_long_long, | |
04f27ddc | 1517 | &builtin_type_signed_char, |
3d6b6a90 JG |
1518 | &builtin_type_unsigned_char, |
1519 | &builtin_type_unsigned_short, | |
1520 | &builtin_type_unsigned_int, | |
1521 | &builtin_type_unsigned_long, | |
1522 | &builtin_type_unsigned_long_long, | |
e2aab031 FF |
1523 | &builtin_type_long_double, |
1524 | &builtin_type_complex, | |
1525 | &builtin_type_double_complex, | |
3d6b6a90 JG |
1526 | 0 |
1527 | }; | |
1528 | ||
9dffe475 | 1529 | const struct language_defn c_language_defn = { |
3d6b6a90 JG |
1530 | "c", /* Language name */ |
1531 | language_c, | |
1532 | c_builtin_types, | |
1533 | range_check_off, | |
1534 | type_check_off, | |
1535 | c_parse, | |
1536 | c_error, | |
1537 | &BUILTIN_TYPE_LONGEST, /* longest signed integral type */ | |
1538 | &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */ | |
e2aab031 | 1539 | &builtin_type_double, /* longest floating point type */ /*FIXME*/ |
3d6b6a90 JG |
1540 | "0x%x", "0x%", "x", /* Hex format, prefix, suffix */ |
1541 | "0%o", "0%", "o", /* Octal format, prefix, suffix */ | |
1542 | c_op_print_tab, /* expression operators for printing */ | |
1543 | LANG_MAGIC | |
1544 | }; | |
1545 | ||
545af6ce PB |
1546 | const struct language_defn cplus_language_defn = { |
1547 | "c++", /* Language name */ | |
1548 | language_cplus, | |
1549 | c_builtin_types, | |
1550 | range_check_off, | |
1551 | type_check_off, | |
1552 | c_parse, | |
1553 | c_error, | |
1554 | &BUILTIN_TYPE_LONGEST, /* longest signed integral type */ | |
1555 | &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */ | |
1556 | &builtin_type_double, /* longest floating point type */ /*FIXME*/ | |
1557 | "0x%x", "0x%", "x", /* Hex format, prefix, suffix */ | |
1558 | "0%o", "0%", "o", /* Octal format, prefix, suffix */ | |
1559 | c_op_print_tab, /* expression operators for printing */ | |
1560 | LANG_MAGIC | |
1561 | }; | |
1562 | ||
3d6b6a90 JG |
1563 | void |
1564 | _initialize_c_exp () | |
1565 | { | |
e2aab031 | 1566 | builtin_type_void = |
1ab3bf1b JG |
1567 | init_type (TYPE_CODE_VOID, 1, |
1568 | 0, | |
1569 | "void", (struct objfile *) NULL); | |
e2aab031 | 1570 | builtin_type_char = |
1ab3bf1b JG |
1571 | init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, |
1572 | 0, | |
1573 | "char", (struct objfile *) NULL); | |
04f27ddc PB |
1574 | builtin_type_signed_char = |
1575 | init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
1576 | TYPE_FLAG_SIGNED, | |
1577 | "signed char", (struct objfile *) NULL); | |
e2aab031 | 1578 | builtin_type_unsigned_char = |
1ab3bf1b JG |
1579 | init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, |
1580 | TYPE_FLAG_UNSIGNED, | |
1581 | "unsigned char", (struct objfile *) NULL); | |
e2aab031 | 1582 | builtin_type_short = |
1ab3bf1b JG |
1583 | init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT, |
1584 | 0, | |
1585 | "short", (struct objfile *) NULL); | |
e2aab031 | 1586 | builtin_type_unsigned_short = |
1ab3bf1b JG |
1587 | init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT, |
1588 | TYPE_FLAG_UNSIGNED, | |
1589 | "unsigned short", (struct objfile *) NULL); | |
e2aab031 | 1590 | builtin_type_int = |
1ab3bf1b JG |
1591 | init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, |
1592 | 0, | |
1593 | "int", (struct objfile *) NULL); | |
e2aab031 | 1594 | builtin_type_unsigned_int = |
1ab3bf1b JG |
1595 | init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, |
1596 | TYPE_FLAG_UNSIGNED, | |
1597 | "unsigned int", (struct objfile *) NULL); | |
e2aab031 | 1598 | builtin_type_long = |
1ab3bf1b JG |
1599 | init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT, |
1600 | 0, | |
1601 | "long", (struct objfile *) NULL); | |
e2aab031 | 1602 | builtin_type_unsigned_long = |
1ab3bf1b JG |
1603 | init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT, |
1604 | TYPE_FLAG_UNSIGNED, | |
1605 | "unsigned long", (struct objfile *) NULL); | |
3d6b6a90 | 1606 | builtin_type_long_long = |
1ab3bf1b JG |
1607 | init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, |
1608 | 0, | |
1609 | "long long", (struct objfile *) NULL); | |
3d6b6a90 | 1610 | builtin_type_unsigned_long_long = |
1ab3bf1b JG |
1611 | init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, |
1612 | TYPE_FLAG_UNSIGNED, | |
1613 | "unsigned long long", (struct objfile *) NULL); | |
e2aab031 | 1614 | builtin_type_float = |
1ab3bf1b JG |
1615 | init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT, |
1616 | 0, | |
1617 | "float", (struct objfile *) NULL); | |
e2aab031 | 1618 | builtin_type_double = |
1ab3bf1b JG |
1619 | init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT, |
1620 | 0, | |
1621 | "double", (struct objfile *) NULL); | |
e2aab031 | 1622 | builtin_type_long_double = |
1ab3bf1b JG |
1623 | init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT, |
1624 | 0, | |
1625 | "long double", (struct objfile *) NULL); | |
e2aab031 | 1626 | builtin_type_complex = |
1ab3bf1b JG |
1627 | init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT, |
1628 | 0, | |
1629 | "complex", (struct objfile *) NULL); | |
e2aab031 | 1630 | builtin_type_double_complex = |
1ab3bf1b JG |
1631 | init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT, |
1632 | 0, | |
1633 | "double complex", (struct objfile *) NULL); | |
3d6b6a90 JG |
1634 | |
1635 | add_language (&c_language_defn); | |
545af6ce | 1636 | add_language (&cplus_language_defn); |
3d6b6a90 | 1637 | } |