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