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