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