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