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