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