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