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