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