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