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