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