PR exp/13206:
[binutils-gdb.git] / gdb / c-exp.y
1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989-2000, 2003-2004, 2006-2012 Free Software
3 Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
36
37 %{
38
39 #include "defs.h"
40 #include "gdb_string.h"
41 #include <ctype.h>
42 #include "expression.h"
43 #include "value.h"
44 #include "parser-defs.h"
45 #include "language.h"
46 #include "c-lang.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
50 #include "charset.h"
51 #include "block.h"
52 #include "cp-support.h"
53 #include "dfp.h"
54 #include "gdb_assert.h"
55 #include "macroscope.h"
56
57 #define parse_type builtin_type (parse_gdbarch)
58
59 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
60 as well as gratuitiously global symbol names, so we can have multiple
61 yacc generated parsers in gdb. Note that these are only the variables
62 produced by yacc. If other parser generators (bison, byacc, etc) produce
63 additional global names that conflict at link time, then those parser
64 generators need to be fixed instead of adding those names to this list. */
65
66 #define yymaxdepth c_maxdepth
67 #define yyparse c_parse_internal
68 #define yylex c_lex
69 #define yyerror c_error
70 #define yylval c_lval
71 #define yychar c_char
72 #define yydebug c_debug
73 #define yypact c_pact
74 #define yyr1 c_r1
75 #define yyr2 c_r2
76 #define yydef c_def
77 #define yychk c_chk
78 #define yypgo c_pgo
79 #define yyact c_act
80 #define yyexca c_exca
81 #define yyerrflag c_errflag
82 #define yynerrs c_nerrs
83 #define yyps c_ps
84 #define yypv c_pv
85 #define yys c_s
86 #define yy_yys c_yys
87 #define yystate c_state
88 #define yytmp c_tmp
89 #define yyv c_v
90 #define yy_yyv c_yyv
91 #define yyval c_val
92 #define yylloc c_lloc
93 #define yyreds c_reds /* With YYDEBUG defined */
94 #define yytoks c_toks /* With YYDEBUG defined */
95 #define yyname c_name /* With YYDEBUG defined */
96 #define yyrule c_rule /* With YYDEBUG defined */
97 #define yylhs c_yylhs
98 #define yylen c_yylen
99 #define yydefred c_yydefred
100 #define yydgoto c_yydgoto
101 #define yysindex c_yysindex
102 #define yyrindex c_yyrindex
103 #define yygindex c_yygindex
104 #define yytable c_yytable
105 #define yycheck c_yycheck
106 #define yyss c_yyss
107 #define yysslim c_yysslim
108 #define yyssp c_yyssp
109 #define yystacksize c_yystacksize
110 #define yyvs c_yyvs
111 #define yyvsp c_yyvsp
112
113 #ifndef YYDEBUG
114 #define YYDEBUG 1 /* Default to yydebug support */
115 #endif
116
117 #define YYFPRINTF parser_fprintf
118
119 int yyparse (void);
120
121 static int yylex (void);
122
123 void yyerror (char *);
124
125 %}
126
127 /* Although the yacc "value" of an expression is not used,
128 since the result is stored in the structure being created,
129 other node types do have values. */
130
131 %union
132 {
133 LONGEST lval;
134 struct {
135 LONGEST val;
136 struct type *type;
137 } typed_val_int;
138 struct {
139 DOUBLEST dval;
140 struct type *type;
141 } typed_val_float;
142 struct {
143 gdb_byte val[16];
144 struct type *type;
145 } typed_val_decfloat;
146 struct symbol *sym;
147 struct type *tval;
148 struct stoken sval;
149 struct typed_stoken tsval;
150 struct ttype tsym;
151 struct symtoken ssym;
152 int voidval;
153 struct block *bval;
154 enum exp_opcode opcode;
155 struct internalvar *ivar;
156
157 struct stoken_vector svec;
158 VEC (type_ptr) *tvec;
159 int *ivec;
160
161 struct type_stack *type_stack;
162 }
163
164 %{
165 /* YYSTYPE gets defined by %union */
166 static int parse_number (char *, int, int, YYSTYPE *);
167 static struct stoken operator_stoken (const char *);
168 static void check_parameter_typelist (VEC (type_ptr) *);
169 %}
170
171 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
172 %type <lval> rcurly
173 %type <tval> type typebase
174 %type <tvec> nonempty_typelist func_mod parameter_typelist
175 /* %type <bval> block */
176
177 /* Fancy type parsing. */
178 %type <tval> ptype
179 %type <lval> array_mod
180 %type <tval> conversion_type_id
181
182 %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
183
184 %token <typed_val_int> INT
185 %token <typed_val_float> FLOAT
186 %token <typed_val_decfloat> DECFLOAT
187
188 /* Both NAME and TYPENAME tokens represent symbols in the input,
189 and both convey their data as strings.
190 But a TYPENAME is a string that happens to be defined as a typedef
191 or builtin type name (such as int or char)
192 and a NAME is any other symbol.
193 Contexts where this distinction is not important can use the
194 nonterminal "name", which matches either NAME or TYPENAME. */
195
196 %token <tsval> STRING
197 %token <tsval> CHAR
198 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
199 %token <ssym> UNKNOWN_CPP_NAME
200 %token <voidval> COMPLETE
201 %token <tsym> TYPENAME
202 %type <sval> name
203 %type <svec> string_exp
204 %type <ssym> name_not_typename
205 %type <tsym> typename
206
207 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
208 but which would parse as a valid number in the current input radix.
209 E.g. "c" when input_radix==16. Depending on the parse, it will be
210 turned into a name or into a number. */
211
212 %token <ssym> NAME_OR_INT
213
214 %token OPERATOR
215 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
216 %token TEMPLATE
217 %token ERROR
218 %token NEW DELETE
219 %type <sval> operator
220 %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
221 %token ENTRY
222 %token TYPEOF
223 %token DECLTYPE
224
225 /* Special type cases, put in to allow the parser to distinguish different
226 legal basetypes. */
227 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
228
229 %token <sval> VARIABLE
230
231 %token <opcode> ASSIGN_MODIFY
232
233 /* C++ */
234 %token TRUEKEYWORD
235 %token FALSEKEYWORD
236
237
238 %left ','
239 %left ABOVE_COMMA
240 %right '=' ASSIGN_MODIFY
241 %right '?'
242 %left OROR
243 %left ANDAND
244 %left '|'
245 %left '^'
246 %left '&'
247 %left EQUAL NOTEQUAL
248 %left '<' '>' LEQ GEQ
249 %left LSH RSH
250 %left '@'
251 %left '+' '-'
252 %left '*' '/' '%'
253 %right UNARY INCREMENT DECREMENT
254 %right ARROW ARROW_STAR '.' DOT_STAR '[' '('
255 %token <ssym> BLOCKNAME
256 %token <bval> FILENAME
257 %type <bval> block
258 %left COLONCOLON
259
260 %token DOTDOTDOT
261
262 \f
263 %%
264
265 start : exp1
266 | type_exp
267 ;
268
269 type_exp: type
270 { write_exp_elt_opcode(OP_TYPE);
271 write_exp_elt_type($1);
272 write_exp_elt_opcode(OP_TYPE);}
273 | TYPEOF '(' exp ')'
274 {
275 write_exp_elt_opcode (OP_TYPEOF);
276 }
277 | TYPEOF '(' type ')'
278 {
279 write_exp_elt_opcode (OP_TYPE);
280 write_exp_elt_type ($3);
281 write_exp_elt_opcode (OP_TYPE);
282 }
283 | DECLTYPE '(' exp ')'
284 {
285 write_exp_elt_opcode (OP_DECLTYPE);
286 }
287 ;
288
289 /* Expressions, including the comma operator. */
290 exp1 : exp
291 | exp1 ',' exp
292 { write_exp_elt_opcode (BINOP_COMMA); }
293 ;
294
295 /* Expressions, not including the comma operator. */
296 exp : '*' exp %prec UNARY
297 { write_exp_elt_opcode (UNOP_IND); }
298 ;
299
300 exp : '&' exp %prec UNARY
301 { write_exp_elt_opcode (UNOP_ADDR); }
302 ;
303
304 exp : '-' exp %prec UNARY
305 { write_exp_elt_opcode (UNOP_NEG); }
306 ;
307
308 exp : '+' exp %prec UNARY
309 { write_exp_elt_opcode (UNOP_PLUS); }
310 ;
311
312 exp : '!' exp %prec UNARY
313 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
314 ;
315
316 exp : '~' exp %prec UNARY
317 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
318 ;
319
320 exp : INCREMENT exp %prec UNARY
321 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
322 ;
323
324 exp : DECREMENT exp %prec UNARY
325 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
326 ;
327
328 exp : exp INCREMENT %prec UNARY
329 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
330 ;
331
332 exp : exp DECREMENT %prec UNARY
333 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
334 ;
335
336 exp : SIZEOF exp %prec UNARY
337 { write_exp_elt_opcode (UNOP_SIZEOF); }
338 ;
339
340 exp : exp ARROW name
341 { write_exp_elt_opcode (STRUCTOP_PTR);
342 write_exp_string ($3);
343 write_exp_elt_opcode (STRUCTOP_PTR); }
344 ;
345
346 exp : exp ARROW name COMPLETE
347 { mark_struct_expression ();
348 write_exp_elt_opcode (STRUCTOP_PTR);
349 write_exp_string ($3);
350 write_exp_elt_opcode (STRUCTOP_PTR); }
351 ;
352
353 exp : exp ARROW COMPLETE
354 { struct stoken s;
355 mark_struct_expression ();
356 write_exp_elt_opcode (STRUCTOP_PTR);
357 s.ptr = "";
358 s.length = 0;
359 write_exp_string (s);
360 write_exp_elt_opcode (STRUCTOP_PTR); }
361 ;
362
363 exp : exp ARROW qualified_name
364 { /* exp->type::name becomes exp->*(&type::name) */
365 /* Note: this doesn't work if name is a
366 static member! FIXME */
367 write_exp_elt_opcode (UNOP_ADDR);
368 write_exp_elt_opcode (STRUCTOP_MPTR); }
369 ;
370
371 exp : exp ARROW_STAR exp
372 { write_exp_elt_opcode (STRUCTOP_MPTR); }
373 ;
374
375 exp : exp '.' name
376 { write_exp_elt_opcode (STRUCTOP_STRUCT);
377 write_exp_string ($3);
378 write_exp_elt_opcode (STRUCTOP_STRUCT); }
379 ;
380
381 exp : exp '.' name COMPLETE
382 { mark_struct_expression ();
383 write_exp_elt_opcode (STRUCTOP_STRUCT);
384 write_exp_string ($3);
385 write_exp_elt_opcode (STRUCTOP_STRUCT); }
386 ;
387
388 exp : exp '.' COMPLETE
389 { struct stoken s;
390 mark_struct_expression ();
391 write_exp_elt_opcode (STRUCTOP_STRUCT);
392 s.ptr = "";
393 s.length = 0;
394 write_exp_string (s);
395 write_exp_elt_opcode (STRUCTOP_STRUCT); }
396 ;
397
398 exp : exp '.' qualified_name
399 { /* exp.type::name becomes exp.*(&type::name) */
400 /* Note: this doesn't work if name is a
401 static member! FIXME */
402 write_exp_elt_opcode (UNOP_ADDR);
403 write_exp_elt_opcode (STRUCTOP_MEMBER); }
404 ;
405
406 exp : exp DOT_STAR exp
407 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
408 ;
409
410 exp : exp '[' exp1 ']'
411 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
412 ;
413
414 exp : exp '('
415 /* This is to save the value of arglist_len
416 being accumulated by an outer function call. */
417 { start_arglist (); }
418 arglist ')' %prec ARROW
419 { write_exp_elt_opcode (OP_FUNCALL);
420 write_exp_elt_longcst ((LONGEST) end_arglist ());
421 write_exp_elt_opcode (OP_FUNCALL); }
422 ;
423
424 exp : UNKNOWN_CPP_NAME '('
425 {
426 /* This could potentially be a an argument defined
427 lookup function (Koenig). */
428 write_exp_elt_opcode (OP_ADL_FUNC);
429 write_exp_elt_block (expression_context_block);
430 write_exp_elt_sym (NULL); /* Placeholder. */
431 write_exp_string ($1.stoken);
432 write_exp_elt_opcode (OP_ADL_FUNC);
433
434 /* This is to save the value of arglist_len
435 being accumulated by an outer function call. */
436
437 start_arglist ();
438 }
439 arglist ')' %prec ARROW
440 {
441 write_exp_elt_opcode (OP_FUNCALL);
442 write_exp_elt_longcst ((LONGEST) end_arglist ());
443 write_exp_elt_opcode (OP_FUNCALL);
444 }
445 ;
446
447 lcurly : '{'
448 { start_arglist (); }
449 ;
450
451 arglist :
452 ;
453
454 arglist : exp
455 { arglist_len = 1; }
456 ;
457
458 arglist : arglist ',' exp %prec ABOVE_COMMA
459 { arglist_len++; }
460 ;
461
462 exp : exp '(' parameter_typelist ')' const_or_volatile
463 { int i;
464 VEC (type_ptr) *type_list = $3;
465 struct type *type_elt;
466 LONGEST len = VEC_length (type_ptr, type_list);
467
468 write_exp_elt_opcode (TYPE_INSTANCE);
469 write_exp_elt_longcst (len);
470 for (i = 0;
471 VEC_iterate (type_ptr, type_list, i, type_elt);
472 ++i)
473 write_exp_elt_type (type_elt);
474 write_exp_elt_longcst(len);
475 write_exp_elt_opcode (TYPE_INSTANCE);
476 VEC_free (type_ptr, type_list);
477 }
478 ;
479
480 rcurly : '}'
481 { $$ = end_arglist () - 1; }
482 ;
483 exp : lcurly arglist rcurly %prec ARROW
484 { write_exp_elt_opcode (OP_ARRAY);
485 write_exp_elt_longcst ((LONGEST) 0);
486 write_exp_elt_longcst ((LONGEST) $3);
487 write_exp_elt_opcode (OP_ARRAY); }
488 ;
489
490 exp : lcurly type_exp rcurly exp %prec UNARY
491 { write_exp_elt_opcode (UNOP_MEMVAL_TYPE); }
492 ;
493
494 exp : '(' type_exp ')' exp %prec UNARY
495 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
496 ;
497
498 exp : '(' exp1 ')'
499 { }
500 ;
501
502 /* Binary operators in order of decreasing precedence. */
503
504 exp : exp '@' exp
505 { write_exp_elt_opcode (BINOP_REPEAT); }
506 ;
507
508 exp : exp '*' exp
509 { write_exp_elt_opcode (BINOP_MUL); }
510 ;
511
512 exp : exp '/' exp
513 { write_exp_elt_opcode (BINOP_DIV); }
514 ;
515
516 exp : exp '%' exp
517 { write_exp_elt_opcode (BINOP_REM); }
518 ;
519
520 exp : exp '+' exp
521 { write_exp_elt_opcode (BINOP_ADD); }
522 ;
523
524 exp : exp '-' exp
525 { write_exp_elt_opcode (BINOP_SUB); }
526 ;
527
528 exp : exp LSH exp
529 { write_exp_elt_opcode (BINOP_LSH); }
530 ;
531
532 exp : exp RSH exp
533 { write_exp_elt_opcode (BINOP_RSH); }
534 ;
535
536 exp : exp EQUAL exp
537 { write_exp_elt_opcode (BINOP_EQUAL); }
538 ;
539
540 exp : exp NOTEQUAL exp
541 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
542 ;
543
544 exp : exp LEQ exp
545 { write_exp_elt_opcode (BINOP_LEQ); }
546 ;
547
548 exp : exp GEQ exp
549 { write_exp_elt_opcode (BINOP_GEQ); }
550 ;
551
552 exp : exp '<' exp
553 { write_exp_elt_opcode (BINOP_LESS); }
554 ;
555
556 exp : exp '>' exp
557 { write_exp_elt_opcode (BINOP_GTR); }
558 ;
559
560 exp : exp '&' exp
561 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
562 ;
563
564 exp : exp '^' exp
565 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
566 ;
567
568 exp : exp '|' exp
569 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
570 ;
571
572 exp : exp ANDAND exp
573 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
574 ;
575
576 exp : exp OROR exp
577 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
578 ;
579
580 exp : exp '?' exp ':' exp %prec '?'
581 { write_exp_elt_opcode (TERNOP_COND); }
582 ;
583
584 exp : exp '=' exp
585 { write_exp_elt_opcode (BINOP_ASSIGN); }
586 ;
587
588 exp : exp ASSIGN_MODIFY exp
589 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
590 write_exp_elt_opcode ($2);
591 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
592 ;
593
594 exp : INT
595 { write_exp_elt_opcode (OP_LONG);
596 write_exp_elt_type ($1.type);
597 write_exp_elt_longcst ((LONGEST)($1.val));
598 write_exp_elt_opcode (OP_LONG); }
599 ;
600
601 exp : CHAR
602 {
603 struct stoken_vector vec;
604 vec.len = 1;
605 vec.tokens = &$1;
606 write_exp_string_vector ($1.type, &vec);
607 }
608 ;
609
610 exp : NAME_OR_INT
611 { YYSTYPE val;
612 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
613 write_exp_elt_opcode (OP_LONG);
614 write_exp_elt_type (val.typed_val_int.type);
615 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
616 write_exp_elt_opcode (OP_LONG);
617 }
618 ;
619
620
621 exp : FLOAT
622 { write_exp_elt_opcode (OP_DOUBLE);
623 write_exp_elt_type ($1.type);
624 write_exp_elt_dblcst ($1.dval);
625 write_exp_elt_opcode (OP_DOUBLE); }
626 ;
627
628 exp : DECFLOAT
629 { write_exp_elt_opcode (OP_DECFLOAT);
630 write_exp_elt_type ($1.type);
631 write_exp_elt_decfloatcst ($1.val);
632 write_exp_elt_opcode (OP_DECFLOAT); }
633 ;
634
635 exp : variable
636 ;
637
638 exp : VARIABLE
639 {
640 write_dollar_variable ($1);
641 }
642 ;
643
644 exp : SIZEOF '(' type ')' %prec UNARY
645 { write_exp_elt_opcode (OP_LONG);
646 write_exp_elt_type (lookup_signed_typename
647 (parse_language, parse_gdbarch,
648 "int"));
649 CHECK_TYPEDEF ($3);
650 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
651 write_exp_elt_opcode (OP_LONG); }
652 ;
653
654 exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
655 { write_exp_elt_opcode (UNOP_REINTERPRET_CAST); }
656 ;
657
658 exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
659 { write_exp_elt_opcode (UNOP_CAST_TYPE); }
660 ;
661
662 exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
663 { write_exp_elt_opcode (UNOP_DYNAMIC_CAST); }
664 ;
665
666 exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
667 { /* We could do more error checking here, but
668 it doesn't seem worthwhile. */
669 write_exp_elt_opcode (UNOP_CAST_TYPE); }
670 ;
671
672 string_exp:
673 STRING
674 {
675 /* We copy the string here, and not in the
676 lexer, to guarantee that we do not leak a
677 string. Note that we follow the
678 NUL-termination convention of the
679 lexer. */
680 struct typed_stoken *vec = XNEW (struct typed_stoken);
681 $$.len = 1;
682 $$.tokens = vec;
683
684 vec->type = $1.type;
685 vec->length = $1.length;
686 vec->ptr = malloc ($1.length + 1);
687 memcpy (vec->ptr, $1.ptr, $1.length + 1);
688 }
689
690 | string_exp STRING
691 {
692 /* Note that we NUL-terminate here, but just
693 for convenience. */
694 char *p;
695 ++$$.len;
696 $$.tokens = realloc ($$.tokens,
697 $$.len * sizeof (struct typed_stoken));
698
699 p = malloc ($2.length + 1);
700 memcpy (p, $2.ptr, $2.length + 1);
701
702 $$.tokens[$$.len - 1].type = $2.type;
703 $$.tokens[$$.len - 1].length = $2.length;
704 $$.tokens[$$.len - 1].ptr = p;
705 }
706 ;
707
708 exp : string_exp
709 {
710 int i;
711 enum c_string_type type = C_STRING;
712
713 for (i = 0; i < $1.len; ++i)
714 {
715 switch ($1.tokens[i].type)
716 {
717 case C_STRING:
718 break;
719 case C_WIDE_STRING:
720 case C_STRING_16:
721 case C_STRING_32:
722 if (type != C_STRING
723 && type != $1.tokens[i].type)
724 error (_("Undefined string concatenation."));
725 type = $1.tokens[i].type;
726 break;
727 default:
728 /* internal error */
729 internal_error (__FILE__, __LINE__,
730 "unrecognized type in string concatenation");
731 }
732 }
733
734 write_exp_string_vector (type, &$1);
735 for (i = 0; i < $1.len; ++i)
736 free ($1.tokens[i].ptr);
737 free ($1.tokens);
738 }
739 ;
740
741 /* C++. */
742 exp : TRUEKEYWORD
743 { write_exp_elt_opcode (OP_LONG);
744 write_exp_elt_type (parse_type->builtin_bool);
745 write_exp_elt_longcst ((LONGEST) 1);
746 write_exp_elt_opcode (OP_LONG); }
747 ;
748
749 exp : FALSEKEYWORD
750 { write_exp_elt_opcode (OP_LONG);
751 write_exp_elt_type (parse_type->builtin_bool);
752 write_exp_elt_longcst ((LONGEST) 0);
753 write_exp_elt_opcode (OP_LONG); }
754 ;
755
756 /* end of C++. */
757
758 block : BLOCKNAME
759 {
760 if ($1.sym)
761 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
762 else
763 error (_("No file or function \"%s\"."),
764 copy_name ($1.stoken));
765 }
766 | FILENAME
767 {
768 $$ = $1;
769 }
770 ;
771
772 block : block COLONCOLON name
773 { struct symbol *tem
774 = lookup_symbol (copy_name ($3), $1,
775 VAR_DOMAIN, (int *) NULL);
776 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
777 error (_("No function \"%s\" in specified context."),
778 copy_name ($3));
779 $$ = SYMBOL_BLOCK_VALUE (tem); }
780 ;
781
782 variable: name_not_typename ENTRY
783 { struct symbol *sym = $1.sym;
784
785 if (sym == NULL || !SYMBOL_IS_ARGUMENT (sym)
786 || !symbol_read_needs_frame (sym))
787 error (_("@entry can be used only for function "
788 "parameters, not for \"%s\""),
789 copy_name ($1.stoken));
790
791 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
792 write_exp_elt_sym (sym);
793 write_exp_elt_opcode (OP_VAR_ENTRY_VALUE);
794 }
795 ;
796
797 variable: block COLONCOLON name
798 { struct symbol *sym;
799 sym = lookup_symbol (copy_name ($3), $1,
800 VAR_DOMAIN, (int *) NULL);
801 if (sym == 0)
802 error (_("No symbol \"%s\" in specified context."),
803 copy_name ($3));
804 if (symbol_read_needs_frame (sym))
805 {
806 if (innermost_block == 0
807 || contained_in (block_found,
808 innermost_block))
809 innermost_block = block_found;
810 }
811
812 write_exp_elt_opcode (OP_VAR_VALUE);
813 /* block_found is set by lookup_symbol. */
814 write_exp_elt_block (block_found);
815 write_exp_elt_sym (sym);
816 write_exp_elt_opcode (OP_VAR_VALUE); }
817 ;
818
819 qualified_name: TYPENAME COLONCOLON name
820 {
821 struct type *type = $1.type;
822 CHECK_TYPEDEF (type);
823 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
824 && TYPE_CODE (type) != TYPE_CODE_UNION
825 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
826 error (_("`%s' is not defined as an aggregate type."),
827 TYPE_NAME (type));
828
829 write_exp_elt_opcode (OP_SCOPE);
830 write_exp_elt_type (type);
831 write_exp_string ($3);
832 write_exp_elt_opcode (OP_SCOPE);
833 }
834 | TYPENAME COLONCOLON '~' name
835 {
836 struct type *type = $1.type;
837 struct stoken tmp_token;
838 CHECK_TYPEDEF (type);
839 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
840 && TYPE_CODE (type) != TYPE_CODE_UNION
841 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
842 error (_("`%s' is not defined as an aggregate type."),
843 TYPE_NAME (type));
844
845 tmp_token.ptr = (char*) alloca ($4.length + 2);
846 tmp_token.length = $4.length + 1;
847 tmp_token.ptr[0] = '~';
848 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
849 tmp_token.ptr[tmp_token.length] = 0;
850
851 /* Check for valid destructor name. */
852 destructor_name_p (tmp_token.ptr, $1.type);
853 write_exp_elt_opcode (OP_SCOPE);
854 write_exp_elt_type (type);
855 write_exp_string (tmp_token);
856 write_exp_elt_opcode (OP_SCOPE);
857 }
858 | TYPENAME COLONCOLON name COLONCOLON name
859 {
860 char *copy = copy_name ($3);
861 error (_("No type \"%s\" within class "
862 "or namespace \"%s\"."),
863 copy, TYPE_NAME ($1.type));
864 }
865 ;
866
867 variable: qualified_name
868 | COLONCOLON name_not_typename
869 {
870 char *name = copy_name ($2.stoken);
871 struct symbol *sym;
872 struct minimal_symbol *msymbol;
873
874 sym =
875 lookup_symbol (name, (const struct block *) NULL,
876 VAR_DOMAIN, (int *) NULL);
877 if (sym)
878 {
879 write_exp_elt_opcode (OP_VAR_VALUE);
880 write_exp_elt_block (NULL);
881 write_exp_elt_sym (sym);
882 write_exp_elt_opcode (OP_VAR_VALUE);
883 break;
884 }
885
886 msymbol = lookup_minimal_symbol (name, NULL, NULL);
887 if (msymbol != NULL)
888 write_exp_msymbol (msymbol);
889 else if (!have_full_symbols () && !have_partial_symbols ())
890 error (_("No symbol table is loaded. Use the \"file\" command."));
891 else
892 error (_("No symbol \"%s\" in current context."), name);
893 }
894 ;
895
896 variable: name_not_typename
897 { struct symbol *sym = $1.sym;
898
899 if (sym)
900 {
901 if (symbol_read_needs_frame (sym))
902 {
903 if (innermost_block == 0
904 || contained_in (block_found,
905 innermost_block))
906 innermost_block = block_found;
907 }
908
909 write_exp_elt_opcode (OP_VAR_VALUE);
910 /* We want to use the selected frame, not
911 another more inner frame which happens to
912 be in the same block. */
913 write_exp_elt_block (NULL);
914 write_exp_elt_sym (sym);
915 write_exp_elt_opcode (OP_VAR_VALUE);
916 }
917 else if ($1.is_a_field_of_this)
918 {
919 /* C++: it hangs off of `this'. Must
920 not inadvertently convert from a method call
921 to data ref. */
922 if (innermost_block == 0
923 || contained_in (block_found,
924 innermost_block))
925 innermost_block = block_found;
926 write_exp_elt_opcode (OP_THIS);
927 write_exp_elt_opcode (OP_THIS);
928 write_exp_elt_opcode (STRUCTOP_PTR);
929 write_exp_string ($1.stoken);
930 write_exp_elt_opcode (STRUCTOP_PTR);
931 }
932 else
933 {
934 struct minimal_symbol *msymbol;
935 char *arg = copy_name ($1.stoken);
936
937 msymbol =
938 lookup_minimal_symbol (arg, NULL, NULL);
939 if (msymbol != NULL)
940 write_exp_msymbol (msymbol);
941 else if (!have_full_symbols () && !have_partial_symbols ())
942 error (_("No symbol table is loaded. Use the \"file\" command."));
943 else
944 error (_("No symbol \"%s\" in current context."),
945 copy_name ($1.stoken));
946 }
947 }
948 ;
949
950 space_identifier : '@' NAME
951 { insert_type_address_space (copy_name ($2.stoken)); }
952 ;
953
954 const_or_volatile: const_or_volatile_noopt
955 |
956 ;
957
958 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
959 ;
960
961 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
962 | const_or_volatile_noopt
963 ;
964
965 const_or_volatile_or_space_identifier:
966 const_or_volatile_or_space_identifier_noopt
967 |
968 ;
969
970 ptr_operator:
971 ptr_operator '*'
972 { insert_type (tp_pointer); }
973 const_or_volatile_or_space_identifier
974 | '*'
975 { insert_type (tp_pointer); }
976 const_or_volatile_or_space_identifier
977 | '&'
978 { insert_type (tp_reference); }
979 | '&' ptr_operator
980 { insert_type (tp_reference); }
981 ;
982
983 ptr_operator_ts: ptr_operator
984 {
985 $$ = get_type_stack ();
986 /* This cleanup is eventually run by
987 c_parse. */
988 make_cleanup (type_stack_cleanup, $$);
989 }
990 ;
991
992 abs_decl: ptr_operator_ts direct_abs_decl
993 { $$ = append_type_stack ($2, $1); }
994 | ptr_operator_ts
995 | direct_abs_decl
996 ;
997
998 direct_abs_decl: '(' abs_decl ')'
999 { $$ = $2; }
1000 | direct_abs_decl array_mod
1001 {
1002 push_type_stack ($1);
1003 push_type_int ($2);
1004 push_type (tp_array);
1005 $$ = get_type_stack ();
1006 }
1007 | array_mod
1008 {
1009 push_type_int ($1);
1010 push_type (tp_array);
1011 $$ = get_type_stack ();
1012 }
1013
1014 | direct_abs_decl func_mod
1015 {
1016 push_type_stack ($1);
1017 push_typelist ($2);
1018 $$ = get_type_stack ();
1019 }
1020 | func_mod
1021 {
1022 push_typelist ($1);
1023 $$ = get_type_stack ();
1024 }
1025 ;
1026
1027 array_mod: '[' ']'
1028 { $$ = -1; }
1029 | '[' INT ']'
1030 { $$ = $2.val; }
1031 ;
1032
1033 func_mod: '(' ')'
1034 { $$ = NULL; }
1035 | '(' parameter_typelist ')'
1036 { $$ = $2; }
1037 ;
1038
1039 /* We used to try to recognize pointer to member types here, but
1040 that didn't work (shift/reduce conflicts meant that these rules never
1041 got executed). The problem is that
1042 int (foo::bar::baz::bizzle)
1043 is a function type but
1044 int (foo::bar::baz::bizzle::*)
1045 is a pointer to member type. Stroustrup loses again! */
1046
1047 type : ptype
1048 ;
1049
1050 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
1051 : TYPENAME
1052 { $$ = $1.type; }
1053 | INT_KEYWORD
1054 { $$ = lookup_signed_typename (parse_language,
1055 parse_gdbarch,
1056 "int"); }
1057 | LONG
1058 { $$ = lookup_signed_typename (parse_language,
1059 parse_gdbarch,
1060 "long"); }
1061 | SHORT
1062 { $$ = lookup_signed_typename (parse_language,
1063 parse_gdbarch,
1064 "short"); }
1065 | LONG INT_KEYWORD
1066 { $$ = lookup_signed_typename (parse_language,
1067 parse_gdbarch,
1068 "long"); }
1069 | LONG SIGNED_KEYWORD INT_KEYWORD
1070 { $$ = lookup_signed_typename (parse_language,
1071 parse_gdbarch,
1072 "long"); }
1073 | LONG SIGNED_KEYWORD
1074 { $$ = lookup_signed_typename (parse_language,
1075 parse_gdbarch,
1076 "long"); }
1077 | SIGNED_KEYWORD LONG INT_KEYWORD
1078 { $$ = lookup_signed_typename (parse_language,
1079 parse_gdbarch,
1080 "long"); }
1081 | UNSIGNED LONG INT_KEYWORD
1082 { $$ = lookup_unsigned_typename (parse_language,
1083 parse_gdbarch,
1084 "long"); }
1085 | LONG UNSIGNED INT_KEYWORD
1086 { $$ = lookup_unsigned_typename (parse_language,
1087 parse_gdbarch,
1088 "long"); }
1089 | LONG UNSIGNED
1090 { $$ = lookup_unsigned_typename (parse_language,
1091 parse_gdbarch,
1092 "long"); }
1093 | LONG LONG
1094 { $$ = lookup_signed_typename (parse_language,
1095 parse_gdbarch,
1096 "long long"); }
1097 | LONG LONG INT_KEYWORD
1098 { $$ = lookup_signed_typename (parse_language,
1099 parse_gdbarch,
1100 "long long"); }
1101 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
1102 { $$ = lookup_signed_typename (parse_language,
1103 parse_gdbarch,
1104 "long long"); }
1105 | LONG LONG SIGNED_KEYWORD
1106 { $$ = lookup_signed_typename (parse_language,
1107 parse_gdbarch,
1108 "long long"); }
1109 | SIGNED_KEYWORD LONG LONG
1110 { $$ = lookup_signed_typename (parse_language,
1111 parse_gdbarch,
1112 "long long"); }
1113 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
1114 { $$ = lookup_signed_typename (parse_language,
1115 parse_gdbarch,
1116 "long long"); }
1117 | UNSIGNED LONG LONG
1118 { $$ = lookup_unsigned_typename (parse_language,
1119 parse_gdbarch,
1120 "long long"); }
1121 | UNSIGNED LONG LONG INT_KEYWORD
1122 { $$ = lookup_unsigned_typename (parse_language,
1123 parse_gdbarch,
1124 "long long"); }
1125 | LONG LONG UNSIGNED
1126 { $$ = lookup_unsigned_typename (parse_language,
1127 parse_gdbarch,
1128 "long long"); }
1129 | LONG LONG UNSIGNED INT_KEYWORD
1130 { $$ = lookup_unsigned_typename (parse_language,
1131 parse_gdbarch,
1132 "long long"); }
1133 | SHORT INT_KEYWORD
1134 { $$ = lookup_signed_typename (parse_language,
1135 parse_gdbarch,
1136 "short"); }
1137 | SHORT SIGNED_KEYWORD INT_KEYWORD
1138 { $$ = lookup_signed_typename (parse_language,
1139 parse_gdbarch,
1140 "short"); }
1141 | SHORT SIGNED_KEYWORD
1142 { $$ = lookup_signed_typename (parse_language,
1143 parse_gdbarch,
1144 "short"); }
1145 | UNSIGNED SHORT INT_KEYWORD
1146 { $$ = lookup_unsigned_typename (parse_language,
1147 parse_gdbarch,
1148 "short"); }
1149 | SHORT UNSIGNED
1150 { $$ = lookup_unsigned_typename (parse_language,
1151 parse_gdbarch,
1152 "short"); }
1153 | SHORT UNSIGNED INT_KEYWORD
1154 { $$ = lookup_unsigned_typename (parse_language,
1155 parse_gdbarch,
1156 "short"); }
1157 | DOUBLE_KEYWORD
1158 { $$ = lookup_typename (parse_language, parse_gdbarch,
1159 "double", (struct block *) NULL,
1160 0); }
1161 | LONG DOUBLE_KEYWORD
1162 { $$ = lookup_typename (parse_language, parse_gdbarch,
1163 "long double",
1164 (struct block *) NULL, 0); }
1165 | STRUCT name
1166 { $$ = lookup_struct (copy_name ($2),
1167 expression_context_block); }
1168 | CLASS name
1169 { $$ = lookup_struct (copy_name ($2),
1170 expression_context_block); }
1171 | UNION name
1172 { $$ = lookup_union (copy_name ($2),
1173 expression_context_block); }
1174 | ENUM name
1175 { $$ = lookup_enum (copy_name ($2),
1176 expression_context_block); }
1177 | UNSIGNED typename
1178 { $$ = lookup_unsigned_typename (parse_language,
1179 parse_gdbarch,
1180 TYPE_NAME($2.type)); }
1181 | UNSIGNED
1182 { $$ = lookup_unsigned_typename (parse_language,
1183 parse_gdbarch,
1184 "int"); }
1185 | SIGNED_KEYWORD typename
1186 { $$ = lookup_signed_typename (parse_language,
1187 parse_gdbarch,
1188 TYPE_NAME($2.type)); }
1189 | SIGNED_KEYWORD
1190 { $$ = lookup_signed_typename (parse_language,
1191 parse_gdbarch,
1192 "int"); }
1193 /* It appears that this rule for templates is never
1194 reduced; template recognition happens by lookahead
1195 in the token processing code in yylex. */
1196 | TEMPLATE name '<' type '>'
1197 { $$ = lookup_template_type(copy_name($2), $4,
1198 expression_context_block);
1199 }
1200 | const_or_volatile_or_space_identifier_noopt typebase
1201 { $$ = follow_types ($2); }
1202 | typebase const_or_volatile_or_space_identifier_noopt
1203 { $$ = follow_types ($1); }
1204 ;
1205
1206 typename: TYPENAME
1207 | INT_KEYWORD
1208 {
1209 $$.stoken.ptr = "int";
1210 $$.stoken.length = 3;
1211 $$.type = lookup_signed_typename (parse_language,
1212 parse_gdbarch,
1213 "int");
1214 }
1215 | LONG
1216 {
1217 $$.stoken.ptr = "long";
1218 $$.stoken.length = 4;
1219 $$.type = lookup_signed_typename (parse_language,
1220 parse_gdbarch,
1221 "long");
1222 }
1223 | SHORT
1224 {
1225 $$.stoken.ptr = "short";
1226 $$.stoken.length = 5;
1227 $$.type = lookup_signed_typename (parse_language,
1228 parse_gdbarch,
1229 "short");
1230 }
1231 ;
1232
1233 parameter_typelist:
1234 nonempty_typelist
1235 { check_parameter_typelist ($1); }
1236 | nonempty_typelist ',' DOTDOTDOT
1237 {
1238 VEC_safe_push (type_ptr, $1, NULL);
1239 check_parameter_typelist ($1);
1240 $$ = $1;
1241 }
1242 ;
1243
1244 nonempty_typelist
1245 : type
1246 {
1247 VEC (type_ptr) *typelist = NULL;
1248 VEC_safe_push (type_ptr, typelist, $1);
1249 $$ = typelist;
1250 }
1251 | nonempty_typelist ',' type
1252 {
1253 VEC_safe_push (type_ptr, $1, $3);
1254 $$ = $1;
1255 }
1256 ;
1257
1258 ptype : typebase
1259 | ptype abs_decl
1260 {
1261 push_type_stack ($2);
1262 $$ = follow_types ($1);
1263 }
1264 ;
1265
1266 conversion_type_id: typebase conversion_declarator
1267 { $$ = follow_types ($1); }
1268 ;
1269
1270 conversion_declarator: /* Nothing. */
1271 | ptr_operator conversion_declarator
1272 ;
1273
1274 const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1275 | VOLATILE_KEYWORD CONST_KEYWORD
1276 ;
1277
1278 const_or_volatile_noopt: const_and_volatile
1279 { insert_type (tp_const);
1280 insert_type (tp_volatile);
1281 }
1282 | CONST_KEYWORD
1283 { insert_type (tp_const); }
1284 | VOLATILE_KEYWORD
1285 { insert_type (tp_volatile); }
1286 ;
1287
1288 operator: OPERATOR NEW
1289 { $$ = operator_stoken (" new"); }
1290 | OPERATOR DELETE
1291 { $$ = operator_stoken (" delete"); }
1292 | OPERATOR NEW '[' ']'
1293 { $$ = operator_stoken (" new[]"); }
1294 | OPERATOR DELETE '[' ']'
1295 { $$ = operator_stoken (" delete[]"); }
1296 | OPERATOR '+'
1297 { $$ = operator_stoken ("+"); }
1298 | OPERATOR '-'
1299 { $$ = operator_stoken ("-"); }
1300 | OPERATOR '*'
1301 { $$ = operator_stoken ("*"); }
1302 | OPERATOR '/'
1303 { $$ = operator_stoken ("/"); }
1304 | OPERATOR '%'
1305 { $$ = operator_stoken ("%"); }
1306 | OPERATOR '^'
1307 { $$ = operator_stoken ("^"); }
1308 | OPERATOR '&'
1309 { $$ = operator_stoken ("&"); }
1310 | OPERATOR '|'
1311 { $$ = operator_stoken ("|"); }
1312 | OPERATOR '~'
1313 { $$ = operator_stoken ("~"); }
1314 | OPERATOR '!'
1315 { $$ = operator_stoken ("!"); }
1316 | OPERATOR '='
1317 { $$ = operator_stoken ("="); }
1318 | OPERATOR '<'
1319 { $$ = operator_stoken ("<"); }
1320 | OPERATOR '>'
1321 { $$ = operator_stoken (">"); }
1322 | OPERATOR ASSIGN_MODIFY
1323 { const char *op = "unknown";
1324 switch ($2)
1325 {
1326 case BINOP_RSH:
1327 op = ">>=";
1328 break;
1329 case BINOP_LSH:
1330 op = "<<=";
1331 break;
1332 case BINOP_ADD:
1333 op = "+=";
1334 break;
1335 case BINOP_SUB:
1336 op = "-=";
1337 break;
1338 case BINOP_MUL:
1339 op = "*=";
1340 break;
1341 case BINOP_DIV:
1342 op = "/=";
1343 break;
1344 case BINOP_REM:
1345 op = "%=";
1346 break;
1347 case BINOP_BITWISE_IOR:
1348 op = "|=";
1349 break;
1350 case BINOP_BITWISE_AND:
1351 op = "&=";
1352 break;
1353 case BINOP_BITWISE_XOR:
1354 op = "^=";
1355 break;
1356 default:
1357 break;
1358 }
1359
1360 $$ = operator_stoken (op);
1361 }
1362 | OPERATOR LSH
1363 { $$ = operator_stoken ("<<"); }
1364 | OPERATOR RSH
1365 { $$ = operator_stoken (">>"); }
1366 | OPERATOR EQUAL
1367 { $$ = operator_stoken ("=="); }
1368 | OPERATOR NOTEQUAL
1369 { $$ = operator_stoken ("!="); }
1370 | OPERATOR LEQ
1371 { $$ = operator_stoken ("<="); }
1372 | OPERATOR GEQ
1373 { $$ = operator_stoken (">="); }
1374 | OPERATOR ANDAND
1375 { $$ = operator_stoken ("&&"); }
1376 | OPERATOR OROR
1377 { $$ = operator_stoken ("||"); }
1378 | OPERATOR INCREMENT
1379 { $$ = operator_stoken ("++"); }
1380 | OPERATOR DECREMENT
1381 { $$ = operator_stoken ("--"); }
1382 | OPERATOR ','
1383 { $$ = operator_stoken (","); }
1384 | OPERATOR ARROW_STAR
1385 { $$ = operator_stoken ("->*"); }
1386 | OPERATOR ARROW
1387 { $$ = operator_stoken ("->"); }
1388 | OPERATOR '(' ')'
1389 { $$ = operator_stoken ("()"); }
1390 | OPERATOR '[' ']'
1391 { $$ = operator_stoken ("[]"); }
1392 | OPERATOR conversion_type_id
1393 { char *name;
1394 long length;
1395 struct ui_file *buf = mem_fileopen ();
1396
1397 c_print_type ($2, NULL, buf, -1, 0);
1398 name = ui_file_xstrdup (buf, &length);
1399 ui_file_delete (buf);
1400 $$ = operator_stoken (name);
1401 free (name);
1402 }
1403 ;
1404
1405
1406
1407 name : NAME { $$ = $1.stoken; }
1408 | BLOCKNAME { $$ = $1.stoken; }
1409 | TYPENAME { $$ = $1.stoken; }
1410 | NAME_OR_INT { $$ = $1.stoken; }
1411 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
1412 | operator { $$ = $1; }
1413 ;
1414
1415 name_not_typename : NAME
1416 | BLOCKNAME
1417 /* These would be useful if name_not_typename was useful, but it is just
1418 a fake for "variable", so these cause reduce/reduce conflicts because
1419 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1420 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1421 context where only a name could occur, this might be useful.
1422 | NAME_OR_INT
1423 */
1424 | operator
1425 {
1426 $$.stoken = $1;
1427 $$.sym = lookup_symbol ($1.ptr,
1428 expression_context_block,
1429 VAR_DOMAIN,
1430 &$$.is_a_field_of_this);
1431 }
1432 | UNKNOWN_CPP_NAME
1433 ;
1434
1435 %%
1436
1437 /* Returns a stoken of the operator name given by OP (which does not
1438 include the string "operator"). */
1439 static struct stoken
1440 operator_stoken (const char *op)
1441 {
1442 static const char *operator_string = "operator";
1443 struct stoken st = { NULL, 0 };
1444 st.length = strlen (operator_string) + strlen (op);
1445 st.ptr = malloc (st.length + 1);
1446 strcpy (st.ptr, operator_string);
1447 strcat (st.ptr, op);
1448
1449 /* The toplevel (c_parse) will free the memory allocated here. */
1450 make_cleanup (free, st.ptr);
1451 return st;
1452 };
1453
1454 /* Validate a parameter typelist. */
1455
1456 static void
1457 check_parameter_typelist (VEC (type_ptr) *params)
1458 {
1459 struct type *type;
1460 int ix;
1461
1462 for (ix = 0; VEC_iterate (type_ptr, params, ix, type); ++ix)
1463 {
1464 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1465 {
1466 if (ix == 0)
1467 {
1468 if (VEC_length (type_ptr, params) == 1)
1469 {
1470 /* Ok. */
1471 break;
1472 }
1473 VEC_free (type_ptr, params);
1474 error (_("parameter types following 'void'"));
1475 }
1476 else
1477 {
1478 VEC_free (type_ptr, params);
1479 error (_("'void' invalid as parameter type"));
1480 }
1481 }
1482 }
1483 }
1484
1485 /* Take care of parsing a number (anything that starts with a digit).
1486 Set yylval and return the token type; update lexptr.
1487 LEN is the number of characters in it. */
1488
1489 /*** Needs some error checking for the float case ***/
1490
1491 static int
1492 parse_number (char *p, int len, int parsed_float, YYSTYPE *putithere)
1493 {
1494 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
1495 here, and we do kind of silly things like cast to unsigned. */
1496 LONGEST n = 0;
1497 LONGEST prevn = 0;
1498 ULONGEST un;
1499
1500 int i = 0;
1501 int c;
1502 int base = input_radix;
1503 int unsigned_p = 0;
1504
1505 /* Number of "L" suffixes encountered. */
1506 int long_p = 0;
1507
1508 /* We have found a "L" or "U" suffix. */
1509 int found_suffix = 0;
1510
1511 ULONGEST high_bit;
1512 struct type *signed_type;
1513 struct type *unsigned_type;
1514
1515 if (parsed_float)
1516 {
1517 /* If it ends at "df", "dd" or "dl", take it as type of decimal floating
1518 point. Return DECFLOAT. */
1519
1520 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
1521 {
1522 p[len - 2] = '\0';
1523 putithere->typed_val_decfloat.type
1524 = parse_type->builtin_decfloat;
1525 decimal_from_string (putithere->typed_val_decfloat.val, 4,
1526 gdbarch_byte_order (parse_gdbarch), p);
1527 p[len - 2] = 'd';
1528 return DECFLOAT;
1529 }
1530
1531 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
1532 {
1533 p[len - 2] = '\0';
1534 putithere->typed_val_decfloat.type
1535 = parse_type->builtin_decdouble;
1536 decimal_from_string (putithere->typed_val_decfloat.val, 8,
1537 gdbarch_byte_order (parse_gdbarch), p);
1538 p[len - 2] = 'd';
1539 return DECFLOAT;
1540 }
1541
1542 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
1543 {
1544 p[len - 2] = '\0';
1545 putithere->typed_val_decfloat.type
1546 = parse_type->builtin_declong;
1547 decimal_from_string (putithere->typed_val_decfloat.val, 16,
1548 gdbarch_byte_order (parse_gdbarch), p);
1549 p[len - 2] = 'd';
1550 return DECFLOAT;
1551 }
1552
1553 if (! parse_c_float (parse_gdbarch, p, len,
1554 &putithere->typed_val_float.dval,
1555 &putithere->typed_val_float.type))
1556 return ERROR;
1557 return FLOAT;
1558 }
1559
1560 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1561 if (p[0] == '0')
1562 switch (p[1])
1563 {
1564 case 'x':
1565 case 'X':
1566 if (len >= 3)
1567 {
1568 p += 2;
1569 base = 16;
1570 len -= 2;
1571 }
1572 break;
1573
1574 case 'b':
1575 case 'B':
1576 if (len >= 3)
1577 {
1578 p += 2;
1579 base = 2;
1580 len -= 2;
1581 }
1582 break;
1583
1584 case 't':
1585 case 'T':
1586 case 'd':
1587 case 'D':
1588 if (len >= 3)
1589 {
1590 p += 2;
1591 base = 10;
1592 len -= 2;
1593 }
1594 break;
1595
1596 default:
1597 base = 8;
1598 break;
1599 }
1600
1601 while (len-- > 0)
1602 {
1603 c = *p++;
1604 if (c >= 'A' && c <= 'Z')
1605 c += 'a' - 'A';
1606 if (c != 'l' && c != 'u')
1607 n *= base;
1608 if (c >= '0' && c <= '9')
1609 {
1610 if (found_suffix)
1611 return ERROR;
1612 n += i = c - '0';
1613 }
1614 else
1615 {
1616 if (base > 10 && c >= 'a' && c <= 'f')
1617 {
1618 if (found_suffix)
1619 return ERROR;
1620 n += i = c - 'a' + 10;
1621 }
1622 else if (c == 'l')
1623 {
1624 ++long_p;
1625 found_suffix = 1;
1626 }
1627 else if (c == 'u')
1628 {
1629 unsigned_p = 1;
1630 found_suffix = 1;
1631 }
1632 else
1633 return ERROR; /* Char not a digit */
1634 }
1635 if (i >= base)
1636 return ERROR; /* Invalid digit in this base */
1637
1638 /* Portably test for overflow (only works for nonzero values, so make
1639 a second check for zero). FIXME: Can't we just make n and prevn
1640 unsigned and avoid this? */
1641 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1642 unsigned_p = 1; /* Try something unsigned */
1643
1644 /* Portably test for unsigned overflow.
1645 FIXME: This check is wrong; for example it doesn't find overflow
1646 on 0x123456789 when LONGEST is 32 bits. */
1647 if (c != 'l' && c != 'u' && n != 0)
1648 {
1649 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1650 error (_("Numeric constant too large."));
1651 }
1652 prevn = n;
1653 }
1654
1655 /* An integer constant is an int, a long, or a long long. An L
1656 suffix forces it to be long; an LL suffix forces it to be long
1657 long. If not forced to a larger size, it gets the first type of
1658 the above that it fits in. To figure out whether it fits, we
1659 shift it right and see whether anything remains. Note that we
1660 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1661 operation, because many compilers will warn about such a shift
1662 (which always produces a zero result). Sometimes gdbarch_int_bit
1663 or gdbarch_long_bit will be that big, sometimes not. To deal with
1664 the case where it is we just always shift the value more than
1665 once, with fewer bits each time. */
1666
1667 un = (ULONGEST)n >> 2;
1668 if (long_p == 0
1669 && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
1670 {
1671 high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
1672
1673 /* A large decimal (not hex or octal) constant (between INT_MAX
1674 and UINT_MAX) is a long or unsigned long, according to ANSI,
1675 never an unsigned int, but this code treats it as unsigned
1676 int. This probably should be fixed. GCC gives a warning on
1677 such constants. */
1678
1679 unsigned_type = parse_type->builtin_unsigned_int;
1680 signed_type = parse_type->builtin_int;
1681 }
1682 else if (long_p <= 1
1683 && (un >> (gdbarch_long_bit (parse_gdbarch) - 2)) == 0)
1684 {
1685 high_bit = ((ULONGEST)1) << (gdbarch_long_bit (parse_gdbarch) - 1);
1686 unsigned_type = parse_type->builtin_unsigned_long;
1687 signed_type = parse_type->builtin_long;
1688 }
1689 else
1690 {
1691 int shift;
1692 if (sizeof (ULONGEST) * HOST_CHAR_BIT
1693 < gdbarch_long_long_bit (parse_gdbarch))
1694 /* A long long does not fit in a LONGEST. */
1695 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1696 else
1697 shift = (gdbarch_long_long_bit (parse_gdbarch) - 1);
1698 high_bit = (ULONGEST) 1 << shift;
1699 unsigned_type = parse_type->builtin_unsigned_long_long;
1700 signed_type = parse_type->builtin_long_long;
1701 }
1702
1703 putithere->typed_val_int.val = n;
1704
1705 /* If the high bit of the worked out type is set then this number
1706 has to be unsigned. */
1707
1708 if (unsigned_p || (n & high_bit))
1709 {
1710 putithere->typed_val_int.type = unsigned_type;
1711 }
1712 else
1713 {
1714 putithere->typed_val_int.type = signed_type;
1715 }
1716
1717 return INT;
1718 }
1719
1720 /* Temporary obstack used for holding strings. */
1721 static struct obstack tempbuf;
1722 static int tempbuf_init;
1723
1724 /* Parse a C escape sequence. The initial backslash of the sequence
1725 is at (*PTR)[-1]. *PTR will be updated to point to just after the
1726 last character of the sequence. If OUTPUT is not NULL, the
1727 translated form of the escape sequence will be written there. If
1728 OUTPUT is NULL, no output is written and the call will only affect
1729 *PTR. If an escape sequence is expressed in target bytes, then the
1730 entire sequence will simply be copied to OUTPUT. Return 1 if any
1731 character was emitted, 0 otherwise. */
1732
1733 int
1734 c_parse_escape (char **ptr, struct obstack *output)
1735 {
1736 char *tokptr = *ptr;
1737 int result = 1;
1738
1739 /* Some escape sequences undergo character set conversion. Those we
1740 translate here. */
1741 switch (*tokptr)
1742 {
1743 /* Hex escapes do not undergo character set conversion, so keep
1744 the escape sequence for later. */
1745 case 'x':
1746 if (output)
1747 obstack_grow_str (output, "\\x");
1748 ++tokptr;
1749 if (!isxdigit (*tokptr))
1750 error (_("\\x escape without a following hex digit"));
1751 while (isxdigit (*tokptr))
1752 {
1753 if (output)
1754 obstack_1grow (output, *tokptr);
1755 ++tokptr;
1756 }
1757 break;
1758
1759 /* Octal escapes do not undergo character set conversion, so
1760 keep the escape sequence for later. */
1761 case '0':
1762 case '1':
1763 case '2':
1764 case '3':
1765 case '4':
1766 case '5':
1767 case '6':
1768 case '7':
1769 {
1770 int i;
1771 if (output)
1772 obstack_grow_str (output, "\\");
1773 for (i = 0;
1774 i < 3 && isdigit (*tokptr) && *tokptr != '8' && *tokptr != '9';
1775 ++i)
1776 {
1777 if (output)
1778 obstack_1grow (output, *tokptr);
1779 ++tokptr;
1780 }
1781 }
1782 break;
1783
1784 /* We handle UCNs later. We could handle them here, but that
1785 would mean a spurious error in the case where the UCN could
1786 be converted to the target charset but not the host
1787 charset. */
1788 case 'u':
1789 case 'U':
1790 {
1791 char c = *tokptr;
1792 int i, len = c == 'U' ? 8 : 4;
1793 if (output)
1794 {
1795 obstack_1grow (output, '\\');
1796 obstack_1grow (output, *tokptr);
1797 }
1798 ++tokptr;
1799 if (!isxdigit (*tokptr))
1800 error (_("\\%c escape without a following hex digit"), c);
1801 for (i = 0; i < len && isxdigit (*tokptr); ++i)
1802 {
1803 if (output)
1804 obstack_1grow (output, *tokptr);
1805 ++tokptr;
1806 }
1807 }
1808 break;
1809
1810 /* We must pass backslash through so that it does not
1811 cause quoting during the second expansion. */
1812 case '\\':
1813 if (output)
1814 obstack_grow_str (output, "\\\\");
1815 ++tokptr;
1816 break;
1817
1818 /* Escapes which undergo conversion. */
1819 case 'a':
1820 if (output)
1821 obstack_1grow (output, '\a');
1822 ++tokptr;
1823 break;
1824 case 'b':
1825 if (output)
1826 obstack_1grow (output, '\b');
1827 ++tokptr;
1828 break;
1829 case 'f':
1830 if (output)
1831 obstack_1grow (output, '\f');
1832 ++tokptr;
1833 break;
1834 case 'n':
1835 if (output)
1836 obstack_1grow (output, '\n');
1837 ++tokptr;
1838 break;
1839 case 'r':
1840 if (output)
1841 obstack_1grow (output, '\r');
1842 ++tokptr;
1843 break;
1844 case 't':
1845 if (output)
1846 obstack_1grow (output, '\t');
1847 ++tokptr;
1848 break;
1849 case 'v':
1850 if (output)
1851 obstack_1grow (output, '\v');
1852 ++tokptr;
1853 break;
1854
1855 /* GCC extension. */
1856 case 'e':
1857 if (output)
1858 obstack_1grow (output, HOST_ESCAPE_CHAR);
1859 ++tokptr;
1860 break;
1861
1862 /* Backslash-newline expands to nothing at all. */
1863 case '\n':
1864 ++tokptr;
1865 result = 0;
1866 break;
1867
1868 /* A few escapes just expand to the character itself. */
1869 case '\'':
1870 case '\"':
1871 case '?':
1872 /* GCC extensions. */
1873 case '(':
1874 case '{':
1875 case '[':
1876 case '%':
1877 /* Unrecognized escapes turn into the character itself. */
1878 default:
1879 if (output)
1880 obstack_1grow (output, *tokptr);
1881 ++tokptr;
1882 break;
1883 }
1884 *ptr = tokptr;
1885 return result;
1886 }
1887
1888 /* Parse a string or character literal from TOKPTR. The string or
1889 character may be wide or unicode. *OUTPTR is set to just after the
1890 end of the literal in the input string. The resulting token is
1891 stored in VALUE. This returns a token value, either STRING or
1892 CHAR, depending on what was parsed. *HOST_CHARS is set to the
1893 number of host characters in the literal. */
1894 static int
1895 parse_string_or_char (char *tokptr, char **outptr, struct typed_stoken *value,
1896 int *host_chars)
1897 {
1898 int quote;
1899 enum c_string_type type;
1900
1901 /* Build the gdb internal form of the input string in tempbuf. Note
1902 that the buffer is null byte terminated *only* for the
1903 convenience of debugging gdb itself and printing the buffer
1904 contents when the buffer contains no embedded nulls. Gdb does
1905 not depend upon the buffer being null byte terminated, it uses
1906 the length string instead. This allows gdb to handle C strings
1907 (as well as strings in other languages) with embedded null
1908 bytes */
1909
1910 if (!tempbuf_init)
1911 tempbuf_init = 1;
1912 else
1913 obstack_free (&tempbuf, NULL);
1914 obstack_init (&tempbuf);
1915
1916 /* Record the string type. */
1917 if (*tokptr == 'L')
1918 {
1919 type = C_WIDE_STRING;
1920 ++tokptr;
1921 }
1922 else if (*tokptr == 'u')
1923 {
1924 type = C_STRING_16;
1925 ++tokptr;
1926 }
1927 else if (*tokptr == 'U')
1928 {
1929 type = C_STRING_32;
1930 ++tokptr;
1931 }
1932 else
1933 type = C_STRING;
1934
1935 /* Skip the quote. */
1936 quote = *tokptr;
1937 if (quote == '\'')
1938 type |= C_CHAR;
1939 ++tokptr;
1940
1941 *host_chars = 0;
1942
1943 while (*tokptr)
1944 {
1945 char c = *tokptr;
1946 if (c == '\\')
1947 {
1948 ++tokptr;
1949 *host_chars += c_parse_escape (&tokptr, &tempbuf);
1950 }
1951 else if (c == quote)
1952 break;
1953 else
1954 {
1955 obstack_1grow (&tempbuf, c);
1956 ++tokptr;
1957 /* FIXME: this does the wrong thing with multi-byte host
1958 characters. We could use mbrlen here, but that would
1959 make "set host-charset" a bit less useful. */
1960 ++*host_chars;
1961 }
1962 }
1963
1964 if (*tokptr != quote)
1965 {
1966 if (quote == '"')
1967 error (_("Unterminated string in expression."));
1968 else
1969 error (_("Unmatched single quote."));
1970 }
1971 ++tokptr;
1972
1973 value->type = type;
1974 value->ptr = obstack_base (&tempbuf);
1975 value->length = obstack_object_size (&tempbuf);
1976
1977 *outptr = tokptr;
1978
1979 return quote == '"' ? STRING : CHAR;
1980 }
1981
1982 /* This is used to associate some attributes with a token. */
1983
1984 enum token_flags
1985 {
1986 /* If this bit is set, the token is C++-only. */
1987
1988 FLAG_CXX = 1,
1989
1990 /* If this bit is set, the token is conditional: if there is a
1991 symbol of the same name, then the token is a symbol; otherwise,
1992 the token is a keyword. */
1993
1994 FLAG_SHADOW = 2
1995 };
1996
1997 struct token
1998 {
1999 char *operator;
2000 int token;
2001 enum exp_opcode opcode;
2002 enum token_flags flags;
2003 };
2004
2005 static const struct token tokentab3[] =
2006 {
2007 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
2008 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
2009 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
2010 {"...", DOTDOTDOT, BINOP_END, 0}
2011 };
2012
2013 static const struct token tokentab2[] =
2014 {
2015 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2016 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2017 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2018 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2019 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2020 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2021 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2022 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2023 {"++", INCREMENT, BINOP_END, 0},
2024 {"--", DECREMENT, BINOP_END, 0},
2025 {"->", ARROW, BINOP_END, 0},
2026 {"&&", ANDAND, BINOP_END, 0},
2027 {"||", OROR, BINOP_END, 0},
2028 /* "::" is *not* only C++: gdb overrides its meaning in several
2029 different ways, e.g., 'filename'::func, function::variable. */
2030 {"::", COLONCOLON, BINOP_END, 0},
2031 {"<<", LSH, BINOP_END, 0},
2032 {">>", RSH, BINOP_END, 0},
2033 {"==", EQUAL, BINOP_END, 0},
2034 {"!=", NOTEQUAL, BINOP_END, 0},
2035 {"<=", LEQ, BINOP_END, 0},
2036 {">=", GEQ, BINOP_END, 0},
2037 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
2038 };
2039
2040 /* Identifier-like tokens. */
2041 static const struct token ident_tokens[] =
2042 {
2043 {"unsigned", UNSIGNED, OP_NULL, 0},
2044 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
2045 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2046 {"struct", STRUCT, OP_NULL, 0},
2047 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2048 {"sizeof", SIZEOF, OP_NULL, 0},
2049 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
2050 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2051 {"class", CLASS, OP_NULL, FLAG_CXX},
2052 {"union", UNION, OP_NULL, 0},
2053 {"short", SHORT, OP_NULL, 0},
2054 {"const", CONST_KEYWORD, OP_NULL, 0},
2055 {"enum", ENUM, OP_NULL, 0},
2056 {"long", LONG, OP_NULL, 0},
2057 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
2058 {"int", INT_KEYWORD, OP_NULL, 0},
2059 {"new", NEW, OP_NULL, FLAG_CXX},
2060 {"delete", DELETE, OP_NULL, FLAG_CXX},
2061 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2062
2063 {"and", ANDAND, BINOP_END, FLAG_CXX},
2064 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2065 {"bitand", '&', OP_NULL, FLAG_CXX},
2066 {"bitor", '|', OP_NULL, FLAG_CXX},
2067 {"compl", '~', OP_NULL, FLAG_CXX},
2068 {"not", '!', OP_NULL, FLAG_CXX},
2069 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2070 {"or", OROR, BINOP_END, FLAG_CXX},
2071 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2072 {"xor", '^', OP_NULL, FLAG_CXX},
2073 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2074
2075 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2076 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2077 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
2078 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2079
2080 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2081 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2082 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2083 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
2084 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW }
2085 };
2086
2087 /* When we find that lexptr (the global var defined in parse.c) is
2088 pointing at a macro invocation, we expand the invocation, and call
2089 scan_macro_expansion to save the old lexptr here and point lexptr
2090 into the expanded text. When we reach the end of that, we call
2091 end_macro_expansion to pop back to the value we saved here. The
2092 macro expansion code promises to return only fully-expanded text,
2093 so we don't need to "push" more than one level.
2094
2095 This is disgusting, of course. It would be cleaner to do all macro
2096 expansion beforehand, and then hand that to lexptr. But we don't
2097 really know where the expression ends. Remember, in a command like
2098
2099 (gdb) break *ADDRESS if CONDITION
2100
2101 we evaluate ADDRESS in the scope of the current frame, but we
2102 evaluate CONDITION in the scope of the breakpoint's location. So
2103 it's simply wrong to try to macro-expand the whole thing at once. */
2104 static char *macro_original_text;
2105
2106 /* We save all intermediate macro expansions on this obstack for the
2107 duration of a single parse. The expansion text may sometimes have
2108 to live past the end of the expansion, due to yacc lookahead.
2109 Rather than try to be clever about saving the data for a single
2110 token, we simply keep it all and delete it after parsing has
2111 completed. */
2112 static struct obstack expansion_obstack;
2113
2114 static void
2115 scan_macro_expansion (char *expansion)
2116 {
2117 char *copy;
2118
2119 /* We'd better not be trying to push the stack twice. */
2120 gdb_assert (! macro_original_text);
2121
2122 /* Copy to the obstack, and then free the intermediate
2123 expansion. */
2124 copy = obstack_copy0 (&expansion_obstack, expansion, strlen (expansion));
2125 xfree (expansion);
2126
2127 /* Save the old lexptr value, so we can return to it when we're done
2128 parsing the expanded text. */
2129 macro_original_text = lexptr;
2130 lexptr = copy;
2131 }
2132
2133
2134 static int
2135 scanning_macro_expansion (void)
2136 {
2137 return macro_original_text != 0;
2138 }
2139
2140
2141 static void
2142 finished_macro_expansion (void)
2143 {
2144 /* There'd better be something to pop back to. */
2145 gdb_assert (macro_original_text);
2146
2147 /* Pop back to the original text. */
2148 lexptr = macro_original_text;
2149 macro_original_text = 0;
2150 }
2151
2152
2153 static void
2154 scan_macro_cleanup (void *dummy)
2155 {
2156 if (macro_original_text)
2157 finished_macro_expansion ();
2158
2159 obstack_free (&expansion_obstack, NULL);
2160 }
2161
2162 /* Return true iff the token represents a C++ cast operator. */
2163
2164 static int
2165 is_cast_operator (const char *token, int len)
2166 {
2167 return (! strncmp (token, "dynamic_cast", len)
2168 || ! strncmp (token, "static_cast", len)
2169 || ! strncmp (token, "reinterpret_cast", len)
2170 || ! strncmp (token, "const_cast", len));
2171 }
2172
2173 /* The scope used for macro expansion. */
2174 static struct macro_scope *expression_macro_scope;
2175
2176 /* This is set if a NAME token appeared at the very end of the input
2177 string, with no whitespace separating the name from the EOF. This
2178 is used only when parsing to do field name completion. */
2179 static int saw_name_at_eof;
2180
2181 /* This is set if the previously-returned token was a structure
2182 operator -- either '.' or ARROW. This is used only when parsing to
2183 do field name completion. */
2184 static int last_was_structop;
2185
2186 /* Read one token, getting characters through lexptr. */
2187
2188 static int
2189 lex_one_token (void)
2190 {
2191 int c;
2192 int namelen;
2193 unsigned int i;
2194 char *tokstart;
2195 int saw_structop = last_was_structop;
2196 char *copy;
2197
2198 last_was_structop = 0;
2199
2200 retry:
2201
2202 /* Check if this is a macro invocation that we need to expand. */
2203 if (! scanning_macro_expansion ())
2204 {
2205 char *expanded = macro_expand_next (&lexptr,
2206 standard_macro_lookup,
2207 expression_macro_scope);
2208
2209 if (expanded)
2210 scan_macro_expansion (expanded);
2211 }
2212
2213 prev_lexptr = lexptr;
2214
2215 tokstart = lexptr;
2216 /* See if it is a special token of length 3. */
2217 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
2218 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
2219 {
2220 if ((tokentab3[i].flags & FLAG_CXX) != 0
2221 && parse_language->la_language != language_cplus)
2222 break;
2223
2224 lexptr += 3;
2225 yylval.opcode = tokentab3[i].opcode;
2226 return tokentab3[i].token;
2227 }
2228
2229 /* See if it is a special token of length 2. */
2230 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
2231 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
2232 {
2233 if ((tokentab2[i].flags & FLAG_CXX) != 0
2234 && parse_language->la_language != language_cplus)
2235 break;
2236
2237 lexptr += 2;
2238 yylval.opcode = tokentab2[i].opcode;
2239 if (in_parse_field && tokentab2[i].token == ARROW)
2240 last_was_structop = 1;
2241 return tokentab2[i].token;
2242 }
2243
2244 switch (c = *tokstart)
2245 {
2246 case 0:
2247 /* If we were just scanning the result of a macro expansion,
2248 then we need to resume scanning the original text.
2249 If we're parsing for field name completion, and the previous
2250 token allows such completion, return a COMPLETE token.
2251 Otherwise, we were already scanning the original text, and
2252 we're really done. */
2253 if (scanning_macro_expansion ())
2254 {
2255 finished_macro_expansion ();
2256 goto retry;
2257 }
2258 else if (saw_name_at_eof)
2259 {
2260 saw_name_at_eof = 0;
2261 return COMPLETE;
2262 }
2263 else if (saw_structop)
2264 return COMPLETE;
2265 else
2266 return 0;
2267
2268 case ' ':
2269 case '\t':
2270 case '\n':
2271 lexptr++;
2272 goto retry;
2273
2274 case '[':
2275 case '(':
2276 paren_depth++;
2277 lexptr++;
2278 return c;
2279
2280 case ']':
2281 case ')':
2282 if (paren_depth == 0)
2283 return 0;
2284 paren_depth--;
2285 lexptr++;
2286 return c;
2287
2288 case ',':
2289 if (comma_terminates
2290 && paren_depth == 0
2291 && ! scanning_macro_expansion ())
2292 return 0;
2293 lexptr++;
2294 return c;
2295
2296 case '.':
2297 /* Might be a floating point number. */
2298 if (lexptr[1] < '0' || lexptr[1] > '9')
2299 {
2300 if (in_parse_field)
2301 last_was_structop = 1;
2302 goto symbol; /* Nope, must be a symbol. */
2303 }
2304 /* FALL THRU into number case. */
2305
2306 case '0':
2307 case '1':
2308 case '2':
2309 case '3':
2310 case '4':
2311 case '5':
2312 case '6':
2313 case '7':
2314 case '8':
2315 case '9':
2316 {
2317 /* It's a number. */
2318 int got_dot = 0, got_e = 0, toktype;
2319 char *p = tokstart;
2320 int hex = input_radix > 10;
2321
2322 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2323 {
2324 p += 2;
2325 hex = 1;
2326 }
2327 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2328 {
2329 p += 2;
2330 hex = 0;
2331 }
2332
2333 for (;; ++p)
2334 {
2335 /* This test includes !hex because 'e' is a valid hex digit
2336 and thus does not indicate a floating point number when
2337 the radix is hex. */
2338 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2339 got_dot = got_e = 1;
2340 /* This test does not include !hex, because a '.' always indicates
2341 a decimal floating point number regardless of the radix. */
2342 else if (!got_dot && *p == '.')
2343 got_dot = 1;
2344 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2345 && (*p == '-' || *p == '+'))
2346 /* This is the sign of the exponent, not the end of the
2347 number. */
2348 continue;
2349 /* We will take any letters or digits. parse_number will
2350 complain if past the radix, or if L or U are not final. */
2351 else if ((*p < '0' || *p > '9')
2352 && ((*p < 'a' || *p > 'z')
2353 && (*p < 'A' || *p > 'Z')))
2354 break;
2355 }
2356 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
2357 if (toktype == ERROR)
2358 {
2359 char *err_copy = (char *) alloca (p - tokstart + 1);
2360
2361 memcpy (err_copy, tokstart, p - tokstart);
2362 err_copy[p - tokstart] = 0;
2363 error (_("Invalid number \"%s\"."), err_copy);
2364 }
2365 lexptr = p;
2366 return toktype;
2367 }
2368
2369 case '@':
2370 {
2371 char *p = &tokstart[1];
2372 size_t len = strlen ("entry");
2373
2374 while (isspace (*p))
2375 p++;
2376 if (strncmp (p, "entry", len) == 0 && !isalnum (p[len])
2377 && p[len] != '_')
2378 {
2379 lexptr = &p[len];
2380 return ENTRY;
2381 }
2382 }
2383 /* FALLTHRU */
2384 case '+':
2385 case '-':
2386 case '*':
2387 case '/':
2388 case '%':
2389 case '|':
2390 case '&':
2391 case '^':
2392 case '~':
2393 case '!':
2394 case '<':
2395 case '>':
2396 case '?':
2397 case ':':
2398 case '=':
2399 case '{':
2400 case '}':
2401 symbol:
2402 lexptr++;
2403 return c;
2404
2405 case 'L':
2406 case 'u':
2407 case 'U':
2408 if (tokstart[1] != '"' && tokstart[1] != '\'')
2409 break;
2410 /* Fall through. */
2411 case '\'':
2412 case '"':
2413 {
2414 int host_len;
2415 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2416 &host_len);
2417 if (result == CHAR)
2418 {
2419 if (host_len == 0)
2420 error (_("Empty character constant."));
2421 else if (host_len > 2 && c == '\'')
2422 {
2423 ++tokstart;
2424 namelen = lexptr - tokstart - 1;
2425 goto tryname;
2426 }
2427 else if (host_len > 1)
2428 error (_("Invalid character constant."));
2429 }
2430 return result;
2431 }
2432 }
2433
2434 if (!(c == '_' || c == '$'
2435 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
2436 /* We must have come across a bad character (e.g. ';'). */
2437 error (_("Invalid character '%c' in expression."), c);
2438
2439 /* It's a name. See how long it is. */
2440 namelen = 0;
2441 for (c = tokstart[namelen];
2442 (c == '_' || c == '$' || (c >= '0' && c <= '9')
2443 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
2444 {
2445 /* Template parameter lists are part of the name.
2446 FIXME: This mishandles `print $a<4&&$a>3'. */
2447
2448 if (c == '<')
2449 {
2450 if (! is_cast_operator (tokstart, namelen))
2451 {
2452 /* Scan ahead to get rest of the template specification. Note
2453 that we look ahead only when the '<' adjoins non-whitespace
2454 characters; for comparison expressions, e.g. "a < b > c",
2455 there must be spaces before the '<', etc. */
2456
2457 char * p = find_template_name_end (tokstart + namelen);
2458 if (p)
2459 namelen = p - tokstart;
2460 }
2461 break;
2462 }
2463 c = tokstart[++namelen];
2464 }
2465
2466 /* The token "if" terminates the expression and is NOT removed from
2467 the input stream. It doesn't count if it appears in the
2468 expansion of a macro. */
2469 if (namelen == 2
2470 && tokstart[0] == 'i'
2471 && tokstart[1] == 'f'
2472 && ! scanning_macro_expansion ())
2473 {
2474 return 0;
2475 }
2476
2477 /* For the same reason (breakpoint conditions), "thread N"
2478 terminates the expression. "thread" could be an identifier, but
2479 an identifier is never followed by a number without intervening
2480 punctuation. "task" is similar. Handle abbreviations of these,
2481 similarly to breakpoint.c:find_condition_and_thread. */
2482 if (namelen >= 1
2483 && (strncmp (tokstart, "thread", namelen) == 0
2484 || strncmp (tokstart, "task", namelen) == 0)
2485 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2486 && ! scanning_macro_expansion ())
2487 {
2488 char *p = tokstart + namelen + 1;
2489 while (*p == ' ' || *p == '\t')
2490 p++;
2491 if (*p >= '0' && *p <= '9')
2492 return 0;
2493 }
2494
2495 lexptr += namelen;
2496
2497 tryname:
2498
2499 yylval.sval.ptr = tokstart;
2500 yylval.sval.length = namelen;
2501
2502 /* Catch specific keywords. */
2503 copy = copy_name (yylval.sval);
2504 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
2505 if (strcmp (copy, ident_tokens[i].operator) == 0)
2506 {
2507 if ((ident_tokens[i].flags & FLAG_CXX) != 0
2508 && parse_language->la_language != language_cplus)
2509 break;
2510
2511 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2512 {
2513 int is_a_field_of_this = 0;
2514
2515 if (lookup_symbol (copy, expression_context_block,
2516 VAR_DOMAIN,
2517 (parse_language->la_language == language_cplus
2518 ? &is_a_field_of_this
2519 : NULL))
2520 != NULL)
2521 {
2522 /* The keyword is shadowed. */
2523 break;
2524 }
2525 }
2526
2527 /* It is ok to always set this, even though we don't always
2528 strictly need to. */
2529 yylval.opcode = ident_tokens[i].opcode;
2530 return ident_tokens[i].token;
2531 }
2532
2533 if (*tokstart == '$')
2534 return VARIABLE;
2535
2536 if (in_parse_field && *lexptr == '\0')
2537 saw_name_at_eof = 1;
2538 return NAME;
2539 }
2540
2541 /* An object of this type is pushed on a FIFO by the "outer" lexer. */
2542 typedef struct
2543 {
2544 int token;
2545 YYSTYPE value;
2546 } token_and_value;
2547
2548 DEF_VEC_O (token_and_value);
2549
2550 /* A FIFO of tokens that have been read but not yet returned to the
2551 parser. */
2552 static VEC (token_and_value) *token_fifo;
2553
2554 /* Non-zero if the lexer should return tokens from the FIFO. */
2555 static int popping;
2556
2557 /* Temporary storage for c_lex; this holds symbol names as they are
2558 built up. */
2559 static struct obstack name_obstack;
2560
2561 /* Classify a NAME token. The contents of the token are in `yylval'.
2562 Updates yylval and returns the new token type. BLOCK is the block
2563 in which lookups start; this can be NULL to mean the global
2564 scope. */
2565 static int
2566 classify_name (struct block *block)
2567 {
2568 struct symbol *sym;
2569 char *copy;
2570 int is_a_field_of_this = 0;
2571
2572 copy = copy_name (yylval.sval);
2573
2574 sym = lookup_symbol (copy, block, VAR_DOMAIN,
2575 parse_language->la_language == language_cplus
2576 ? &is_a_field_of_this : (int *) NULL);
2577
2578 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2579 {
2580 yylval.ssym.sym = sym;
2581 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2582 return BLOCKNAME;
2583 }
2584 else if (!sym)
2585 {
2586 /* See if it's a file name. */
2587 struct symtab *symtab;
2588
2589 symtab = lookup_symtab (copy);
2590 if (symtab)
2591 {
2592 yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
2593 return FILENAME;
2594 }
2595 }
2596
2597 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2598 {
2599 yylval.tsym.type = SYMBOL_TYPE (sym);
2600 return TYPENAME;
2601 }
2602
2603 yylval.tsym.type
2604 = language_lookup_primitive_type_by_name (parse_language,
2605 parse_gdbarch, copy);
2606 if (yylval.tsym.type != NULL)
2607 return TYPENAME;
2608
2609 /* Input names that aren't symbols but ARE valid hex numbers, when
2610 the input radix permits them, can be names or numbers depending
2611 on the parse. Note we support radixes > 16 here. */
2612 if (!sym
2613 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
2614 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
2615 {
2616 YYSTYPE newlval; /* Its value is ignored. */
2617 int hextype = parse_number (copy, yylval.sval.length, 0, &newlval);
2618 if (hextype == INT)
2619 {
2620 yylval.ssym.sym = sym;
2621 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2622 return NAME_OR_INT;
2623 }
2624 }
2625
2626 /* Any other kind of symbol */
2627 yylval.ssym.sym = sym;
2628 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
2629
2630 if (sym == NULL
2631 && parse_language->la_language == language_cplus
2632 && !is_a_field_of_this
2633 && !lookup_minimal_symbol (copy, NULL, NULL))
2634 return UNKNOWN_CPP_NAME;
2635
2636 return NAME;
2637 }
2638
2639 /* Like classify_name, but used by the inner loop of the lexer, when a
2640 name might have already been seen. FIRST_NAME is true if the token
2641 in `yylval' is the first component of a name, false otherwise. */
2642
2643 static int
2644 classify_inner_name (struct block *block, int first_name)
2645 {
2646 struct type *type, *new_type;
2647 char *copy;
2648
2649 if (first_name)
2650 return classify_name (block);
2651
2652 type = check_typedef (yylval.tsym.type);
2653 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2654 && TYPE_CODE (type) != TYPE_CODE_UNION
2655 && TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2656 return ERROR;
2657
2658 copy = copy_name (yylval.tsym.stoken);
2659 yylval.ssym.sym = cp_lookup_nested_symbol (yylval.tsym.type, copy, block);
2660 if (yylval.ssym.sym == NULL)
2661 return ERROR;
2662
2663 switch (SYMBOL_CLASS (yylval.ssym.sym))
2664 {
2665 case LOC_BLOCK:
2666 case LOC_LABEL:
2667 return ERROR;
2668
2669 case LOC_TYPEDEF:
2670 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym);;
2671 return TYPENAME;
2672
2673 default:
2674 yylval.ssym.is_a_field_of_this = 0;
2675 return NAME;
2676 }
2677 internal_error (__FILE__, __LINE__, _("not reached"));
2678 }
2679
2680 /* The outer level of a two-level lexer. This calls the inner lexer
2681 to return tokens. It then either returns these tokens, or
2682 aggregates them into a larger token. This lets us work around a
2683 problem in our parsing approach, where the parser could not
2684 distinguish between qualified names and qualified types at the
2685 right point.
2686
2687 This approach is still not ideal, because it mishandles template
2688 types. See the comment in lex_one_token for an example. However,
2689 this is still an improvement over the earlier approach, and will
2690 suffice until we move to better parsing technology. */
2691 static int
2692 yylex (void)
2693 {
2694 token_and_value current;
2695 int first_was_coloncolon, last_was_coloncolon, first_iter;
2696
2697 if (popping && !VEC_empty (token_and_value, token_fifo))
2698 {
2699 token_and_value tv = *VEC_index (token_and_value, token_fifo, 0);
2700 VEC_ordered_remove (token_and_value, token_fifo, 0);
2701 yylval = tv.value;
2702 return tv.token;
2703 }
2704 popping = 0;
2705
2706 current.token = lex_one_token ();
2707 if (current.token == NAME)
2708 current.token = classify_name (expression_context_block);
2709 if (parse_language->la_language != language_cplus
2710 || (current.token != TYPENAME && current.token != COLONCOLON))
2711 return current.token;
2712
2713 first_was_coloncolon = current.token == COLONCOLON;
2714 last_was_coloncolon = first_was_coloncolon;
2715 obstack_free (&name_obstack, obstack_base (&name_obstack));
2716 if (!last_was_coloncolon)
2717 obstack_grow (&name_obstack, yylval.sval.ptr, yylval.sval.length);
2718 current.value = yylval;
2719 first_iter = 1;
2720 while (1)
2721 {
2722 token_and_value next;
2723
2724 next.token = lex_one_token ();
2725 next.value = yylval;
2726
2727 if (next.token == NAME && last_was_coloncolon)
2728 {
2729 int classification;
2730
2731 classification = classify_inner_name (first_was_coloncolon
2732 ? NULL
2733 : expression_context_block,
2734 first_iter);
2735 /* We keep going until we either run out of names, or until
2736 we have a qualified name which is not a type. */
2737 if (classification != TYPENAME && classification != NAME)
2738 {
2739 /* Push the final component and leave the loop. */
2740 VEC_safe_push (token_and_value, token_fifo, &next);
2741 break;
2742 }
2743
2744 /* Update the partial name we are constructing. */
2745 if (!first_iter)
2746 {
2747 /* We don't want to put a leading "::" into the name. */
2748 obstack_grow_str (&name_obstack, "::");
2749 }
2750 obstack_grow (&name_obstack, next.value.sval.ptr,
2751 next.value.sval.length);
2752
2753 yylval.sval.ptr = obstack_base (&name_obstack);
2754 yylval.sval.length = obstack_object_size (&name_obstack);
2755 current.value = yylval;
2756 current.token = classification;
2757
2758 last_was_coloncolon = 0;
2759 }
2760 else if (next.token == COLONCOLON && !last_was_coloncolon)
2761 last_was_coloncolon = 1;
2762 else
2763 {
2764 /* We've reached the end of the name. */
2765 VEC_safe_push (token_and_value, token_fifo, &next);
2766 break;
2767 }
2768
2769 first_iter = 0;
2770 }
2771
2772 popping = 1;
2773
2774 /* If we ended with a "::", insert it too. */
2775 if (last_was_coloncolon)
2776 {
2777 token_and_value cc;
2778 memset (&cc, 0, sizeof (token_and_value));
2779 if (first_was_coloncolon && first_iter)
2780 {
2781 yylval = cc.value;
2782 return COLONCOLON;
2783 }
2784 cc.token = COLONCOLON;
2785 VEC_safe_insert (token_and_value, token_fifo, 0, &cc);
2786 }
2787
2788 yylval = current.value;
2789 yylval.sval.ptr = obstack_copy0 (&expansion_obstack,
2790 yylval.sval.ptr,
2791 yylval.sval.length);
2792 return current.token;
2793 }
2794
2795 int
2796 c_parse (void)
2797 {
2798 int result;
2799 struct cleanup *back_to = make_cleanup (free_current_contents,
2800 &expression_macro_scope);
2801
2802 /* Set up the scope for macro expansion. */
2803 expression_macro_scope = NULL;
2804
2805 if (expression_context_block)
2806 expression_macro_scope
2807 = sal_macro_scope (find_pc_line (expression_context_pc, 0));
2808 else
2809 expression_macro_scope = default_macro_scope ();
2810 if (! expression_macro_scope)
2811 expression_macro_scope = user_macro_scope ();
2812
2813 /* Initialize macro expansion code. */
2814 obstack_init (&expansion_obstack);
2815 gdb_assert (! macro_original_text);
2816 make_cleanup (scan_macro_cleanup, 0);
2817
2818 make_cleanup_restore_integer (&yydebug);
2819 yydebug = parser_debug;
2820
2821 /* Initialize some state used by the lexer. */
2822 last_was_structop = 0;
2823 saw_name_at_eof = 0;
2824
2825 VEC_free (token_and_value, token_fifo);
2826 popping = 0;
2827 obstack_init (&name_obstack);
2828 make_cleanup_obstack_free (&name_obstack);
2829
2830 result = yyparse ();
2831 do_cleanups (back_to);
2832 return result;
2833 }
2834
2835
2836 void
2837 yyerror (char *msg)
2838 {
2839 if (prev_lexptr)
2840 lexptr = prev_lexptr;
2841
2842 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
2843 }