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