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