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