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