]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/c-exp.y
Turn parse_language into a method
[thirdparty/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
SDJ
561 write_exp_elt_opcode (pstate, OP_ADL_FUNC);
562 write_exp_elt_block (pstate,
563 expression_context_block);
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
SS
1402 | STRUCT name
1403 { $$ = lookup_struct (copy_name ($2),
1404 expression_context_block); }
2f68a895
TT
1405 | STRUCT COMPLETE
1406 {
1407 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
1408 $$ = NULL;
1409 }
1410 | STRUCT name COMPLETE
1411 {
1412 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
1413 $2.length);
1414 $$ = NULL;
1415 }
c906108c
SS
1416 | CLASS name
1417 { $$ = lookup_struct (copy_name ($2),
1418 expression_context_block); }
2f68a895
TT
1419 | CLASS COMPLETE
1420 {
4753d33b 1421 mark_completion_tag (TYPE_CODE_STRUCT, "", 0);
2f68a895
TT
1422 $$ = NULL;
1423 }
1424 | CLASS name COMPLETE
1425 {
4753d33b 1426 mark_completion_tag (TYPE_CODE_STRUCT, $2.ptr,
2f68a895
TT
1427 $2.length);
1428 $$ = NULL;
1429 }
c906108c
SS
1430 | UNION name
1431 { $$ = lookup_union (copy_name ($2),
1432 expression_context_block); }
2f68a895
TT
1433 | UNION COMPLETE
1434 {
1435 mark_completion_tag (TYPE_CODE_UNION, "", 0);
1436 $$ = NULL;
1437 }
1438 | UNION name COMPLETE
1439 {
1440 mark_completion_tag (TYPE_CODE_UNION, $2.ptr,
1441 $2.length);
1442 $$ = NULL;
1443 }
c906108c
SS
1444 | ENUM name
1445 { $$ = lookup_enum (copy_name ($2),
1446 expression_context_block); }
2f68a895
TT
1447 | ENUM COMPLETE
1448 {
1449 mark_completion_tag (TYPE_CODE_ENUM, "", 0);
1450 $$ = NULL;
1451 }
1452 | ENUM name COMPLETE
1453 {
1454 mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1455 $2.length);
1456 $$ = NULL;
1457 }
fe978cb0 1458 | UNSIGNED type_name
73923d7e 1459 { $$ = lookup_unsigned_typename (pstate->language (),
fa9f5be6 1460 pstate->gdbarch (),
e6c014f2 1461 TYPE_NAME($2.type)); }
c906108c 1462 | UNSIGNED
73923d7e 1463 { $$ = lookup_unsigned_typename (pstate->language (),
fa9f5be6 1464 pstate->gdbarch (),
f4b8a18d 1465 "int"); }
fe978cb0 1466 | SIGNED_KEYWORD type_name
73923d7e 1467 { $$ = lookup_signed_typename (pstate->language (),
fa9f5be6 1468 pstate->gdbarch (),
e6c014f2 1469 TYPE_NAME($2.type)); }
c906108c 1470 | SIGNED_KEYWORD
73923d7e 1471 { $$ = lookup_signed_typename (pstate->language (),
fa9f5be6 1472 pstate->gdbarch (),
f4b8a18d 1473 "int"); }
c906108c
SS
1474 /* It appears that this rule for templates is never
1475 reduced; template recognition happens by lookahead
4d29c0a8 1476 in the token processing code in yylex. */
c906108c
SS
1477 | TEMPLATE name '<' type '>'
1478 { $$ = lookup_template_type(copy_name($2), $4,
1479 expression_context_block);
1480 }
4d29c0a8 1481 | const_or_volatile_or_space_identifier_noopt typebase
47663de5 1482 { $$ = follow_types ($2); }
4d29c0a8 1483 | typebase const_or_volatile_or_space_identifier_noopt
47663de5 1484 { $$ = follow_types ($1); }
c906108c
SS
1485 ;
1486
fe978cb0 1487type_name: TYPENAME
c906108c
SS
1488 | INT_KEYWORD
1489 {
1490 $$.stoken.ptr = "int";
1491 $$.stoken.length = 3;
73923d7e 1492 $$.type = lookup_signed_typename (pstate->language (),
fa9f5be6 1493 pstate->gdbarch (),
f4b8a18d 1494 "int");
c906108c
SS
1495 }
1496 | LONG
1497 {
1498 $$.stoken.ptr = "long";
1499 $$.stoken.length = 4;
73923d7e 1500 $$.type = lookup_signed_typename (pstate->language (),
fa9f5be6 1501 pstate->gdbarch (),
f4b8a18d 1502 "long");
c906108c
SS
1503 }
1504 | SHORT
1505 {
1506 $$.stoken.ptr = "short";
1507 $$.stoken.length = 5;
73923d7e 1508 $$.type = lookup_signed_typename (pstate->language (),
fa9f5be6 1509 pstate->gdbarch (),
f4b8a18d 1510 "short");
c906108c
SS
1511 }
1512 ;
1513
a6fb9c08
TT
1514parameter_typelist:
1515 nonempty_typelist
e314d629 1516 { check_parameter_typelist ($1); }
a6fb9c08
TT
1517 | nonempty_typelist ',' DOTDOTDOT
1518 {
02e12e38 1519 $1->push_back (NULL);
e314d629 1520 check_parameter_typelist ($1);
a6fb9c08
TT
1521 $$ = $1;
1522 }
1523 ;
1524
c906108c
SS
1525nonempty_typelist
1526 : type
71918a86 1527 {
02e12e38
TT
1528 std::vector<struct type *> *typelist
1529 = new std::vector<struct type *>;
1530 cpstate->type_lists.emplace_back (typelist);
1531
1532 typelist->push_back ($1);
71918a86 1533 $$ = typelist;
c906108c
SS
1534 }
1535 | nonempty_typelist ',' type
71918a86 1536 {
02e12e38 1537 $1->push_back ($3);
71918a86 1538 $$ = $1;
c906108c
SS
1539 }
1540 ;
1541
47663de5 1542ptype : typebase
95c391b6 1543 | ptype abs_decl
fcde5961
TT
1544 {
1545 push_type_stack ($2);
1546 $$ = follow_types ($1);
1547 }
47663de5
MS
1548 ;
1549
95c391b6
TT
1550conversion_type_id: typebase conversion_declarator
1551 { $$ = follow_types ($1); }
1552 ;
1553
1554conversion_declarator: /* Nothing. */
1555 | ptr_operator conversion_declarator
1556 ;
1557
47663de5
MS
1558const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1559 | VOLATILE_KEYWORD CONST_KEYWORD
1560 ;
1561
4d29c0a8 1562const_or_volatile_noopt: const_and_volatile
95c391b6 1563 { insert_type (tp_const);
4d29c0a8 1564 insert_type (tp_volatile);
47663de5
MS
1565 }
1566 | CONST_KEYWORD
95c391b6 1567 { insert_type (tp_const); }
47663de5 1568 | VOLATILE_KEYWORD
95c391b6 1569 { insert_type (tp_volatile); }
47663de5
MS
1570 ;
1571
fe978cb0 1572oper: OPERATOR NEW
66c53f2b
KS
1573 { $$ = operator_stoken (" new"); }
1574 | OPERATOR DELETE
4cd18215 1575 { $$ = operator_stoken (" delete"); }
66c53f2b
KS
1576 | OPERATOR NEW '[' ']'
1577 { $$ = operator_stoken (" new[]"); }
1578 | OPERATOR DELETE '[' ']'
4cd18215 1579 { $$ = operator_stoken (" delete[]"); }
f2e8016f
TT
1580 | OPERATOR NEW OBJC_LBRAC ']'
1581 { $$ = operator_stoken (" new[]"); }
1582 | OPERATOR DELETE OBJC_LBRAC ']'
1583 { $$ = operator_stoken (" delete[]"); }
66c53f2b
KS
1584 | OPERATOR '+'
1585 { $$ = operator_stoken ("+"); }
1586 | OPERATOR '-'
1587 { $$ = operator_stoken ("-"); }
1588 | OPERATOR '*'
1589 { $$ = operator_stoken ("*"); }
1590 | OPERATOR '/'
1591 { $$ = operator_stoken ("/"); }
1592 | OPERATOR '%'
1593 { $$ = operator_stoken ("%"); }
1594 | OPERATOR '^'
1595 { $$ = operator_stoken ("^"); }
1596 | OPERATOR '&'
1597 { $$ = operator_stoken ("&"); }
1598 | OPERATOR '|'
1599 { $$ = operator_stoken ("|"); }
1600 | OPERATOR '~'
1601 { $$ = operator_stoken ("~"); }
1602 | OPERATOR '!'
1603 { $$ = operator_stoken ("!"); }
1604 | OPERATOR '='
1605 { $$ = operator_stoken ("="); }
1606 | OPERATOR '<'
1607 { $$ = operator_stoken ("<"); }
1608 | OPERATOR '>'
1609 { $$ = operator_stoken (">"); }
1610 | OPERATOR ASSIGN_MODIFY
c8ba13ad 1611 { const char *op = " unknown";
66c53f2b
KS
1612 switch ($2)
1613 {
1614 case BINOP_RSH:
1615 op = ">>=";
1616 break;
1617 case BINOP_LSH:
1618 op = "<<=";
1619 break;
1620 case BINOP_ADD:
1621 op = "+=";
1622 break;
1623 case BINOP_SUB:
1624 op = "-=";
1625 break;
1626 case BINOP_MUL:
1627 op = "*=";
1628 break;
1629 case BINOP_DIV:
1630 op = "/=";
1631 break;
1632 case BINOP_REM:
1633 op = "%=";
1634 break;
1635 case BINOP_BITWISE_IOR:
1636 op = "|=";
1637 break;
1638 case BINOP_BITWISE_AND:
1639 op = "&=";
1640 break;
1641 case BINOP_BITWISE_XOR:
1642 op = "^=";
1643 break;
1644 default:
1645 break;
1646 }
1647
1648 $$ = operator_stoken (op);
1649 }
1650 | OPERATOR LSH
1651 { $$ = operator_stoken ("<<"); }
1652 | OPERATOR RSH
1653 { $$ = operator_stoken (">>"); }
1654 | OPERATOR EQUAL
1655 { $$ = operator_stoken ("=="); }
1656 | OPERATOR NOTEQUAL
1657 { $$ = operator_stoken ("!="); }
1658 | OPERATOR LEQ
1659 { $$ = operator_stoken ("<="); }
1660 | OPERATOR GEQ
1661 { $$ = operator_stoken (">="); }
1662 | OPERATOR ANDAND
1663 { $$ = operator_stoken ("&&"); }
1664 | OPERATOR OROR
1665 { $$ = operator_stoken ("||"); }
1666 | OPERATOR INCREMENT
1667 { $$ = operator_stoken ("++"); }
1668 | OPERATOR DECREMENT
1669 { $$ = operator_stoken ("--"); }
1670 | OPERATOR ','
1671 { $$ = operator_stoken (","); }
1672 | OPERATOR ARROW_STAR
1673 { $$ = operator_stoken ("->*"); }
1674 | OPERATOR ARROW
1675 { $$ = operator_stoken ("->"); }
1676 | OPERATOR '(' ')'
1677 { $$ = operator_stoken ("()"); }
1678 | OPERATOR '[' ']'
1679 { $$ = operator_stoken ("[]"); }
f2e8016f
TT
1680 | OPERATOR OBJC_LBRAC ']'
1681 { $$ = operator_stoken ("[]"); }
95c391b6 1682 | OPERATOR conversion_type_id
d7e74731 1683 { string_file buf;
66c53f2b 1684
d7e74731 1685 c_print_type ($2, NULL, &buf, -1, 0,
79d43c61 1686 &type_print_raw_options);
c8ba13ad
KS
1687
1688 /* This also needs canonicalization. */
1689 std::string canon
1690 = cp_canonicalize_string (buf.c_str ());
1691 if (canon.empty ())
1692 canon = std::move (buf.string ());
1693 $$ = operator_stoken ((" " + canon).c_str ());
66c53f2b
KS
1694 }
1695 ;
1696
6f0ffe50
AB
1697/* This rule exists in order to allow some tokens that would not normally
1698 match the 'name' rule to appear as fields within a struct. The example
1699 that initially motivated this was the RISC-V target which models the
1700 floating point registers as a union with fields called 'float' and
1701 'double'. The 'float' string becomes a TYPENAME token and can appear
1702 anywhere a 'name' can, however 'double' is its own token,
1703 DOUBLE_KEYWORD, and doesn't match the 'name' rule.*/
0f5d3f63
AB
1704field_name
1705 : name
6f0ffe50
AB
1706 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
1707 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1708 | LONG { $$ = typename_stoken ("long"); }
1709 | SHORT { $$ = typename_stoken ("short"); }
1710 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1711 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
0f5d3f63 1712 ;
66c53f2b 1713
c906108c
SS
1714name : NAME { $$ = $1.stoken; }
1715 | BLOCKNAME { $$ = $1.stoken; }
1716 | TYPENAME { $$ = $1.stoken; }
1717 | NAME_OR_INT { $$ = $1.stoken; }
7322dca9 1718 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
fe978cb0 1719 | oper { $$ = $1; }
c906108c
SS
1720 ;
1721
1722name_not_typename : NAME
1723 | BLOCKNAME
1724/* These would be useful if name_not_typename was useful, but it is just
1725 a fake for "variable", so these cause reduce/reduce conflicts because
1726 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1727 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1728 context where only a name could occur, this might be useful.
1729 | NAME_OR_INT
1730 */
fe978cb0 1731 | oper
6e31430b 1732 {
1993b719
TT
1733 struct field_of_this_result is_a_field_of_this;
1734
6e31430b
TT
1735 $$.stoken = $1;
1736 $$.sym = lookup_symbol ($1.ptr,
1737 expression_context_block,
1738 VAR_DOMAIN,
1993b719
TT
1739 &is_a_field_of_this);
1740 $$.is_a_field_of_this
1741 = is_a_field_of_this.type != NULL;
6e31430b 1742 }
7322dca9 1743 | UNKNOWN_CPP_NAME
c906108c
SS
1744 ;
1745
1746%%
1747
24955f63
TT
1748/* Like write_exp_string, but prepends a '~'. */
1749
1750static void
410a0ff2 1751write_destructor_name (struct parser_state *par_state, struct stoken token)
24955f63 1752{
224c3ddb 1753 char *copy = (char *) alloca (token.length + 1);
24955f63
TT
1754
1755 copy[0] = '~';
1756 memcpy (&copy[1], token.ptr, token.length);
1757
1758 token.ptr = copy;
1759 ++token.length;
1760
410a0ff2 1761 write_exp_string (par_state, token);
24955f63
TT
1762}
1763
66c53f2b 1764/* Returns a stoken of the operator name given by OP (which does not
4d29c0a8
DE
1765 include the string "operator"). */
1766
66c53f2b
KS
1767static struct stoken
1768operator_stoken (const char *op)
1769{
66c53f2b 1770 struct stoken st = { NULL, 0 };
d7561cbb
KS
1771 char *buf;
1772
8090b426 1773 st.length = CP_OPERATOR_LEN + strlen (op);
224c3ddb 1774 buf = (char *) malloc (st.length + 1);
8090b426 1775 strcpy (buf, CP_OPERATOR_STR);
d7561cbb
KS
1776 strcat (buf, op);
1777 st.ptr = buf;
66c53f2b
KS
1778
1779 /* The toplevel (c_parse) will free the memory allocated here. */
c65bac38 1780 cpstate->strings.emplace_back (buf);
66c53f2b
KS
1781 return st;
1782};
1783
6f0ffe50
AB
1784/* Returns a stoken of the type named TYPE. */
1785
1786static struct stoken
1787typename_stoken (const char *type)
1788{
1789 struct stoken st = { type, 0 };
1790 st.length = strlen (type);
1791 return st;
1792};
1793
3d567982
TT
1794/* Return true if the type is aggregate-like. */
1795
1796static int
1797type_aggregate_p (struct type *type)
1798{
1799 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1800 || TYPE_CODE (type) == TYPE_CODE_UNION
1801 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE
1802 || (TYPE_CODE (type) == TYPE_CODE_ENUM
1803 && TYPE_DECLARED_CLASS (type)));
1804}
1805
e314d629
TT
1806/* Validate a parameter typelist. */
1807
1808static void
02e12e38 1809check_parameter_typelist (std::vector<struct type *> *params)
e314d629
TT
1810{
1811 struct type *type;
1812 int ix;
1813
02e12e38 1814 for (ix = 0; ix < params->size (); ++ix)
e314d629 1815 {
02e12e38 1816 type = (*params)[ix];
e314d629
TT
1817 if (type != NULL && TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
1818 {
1819 if (ix == 0)
1820 {
02e12e38 1821 if (params->size () == 1)
e314d629
TT
1822 {
1823 /* Ok. */
1824 break;
1825 }
e314d629
TT
1826 error (_("parameter types following 'void'"));
1827 }
1828 else
02e12e38 1829 error (_("'void' invalid as parameter type"));
e314d629
TT
1830 }
1831 }
1832}
1833
c906108c
SS
1834/* Take care of parsing a number (anything that starts with a digit).
1835 Set yylval and return the token type; update lexptr.
1836 LEN is the number of characters in it. */
1837
1838/*** Needs some error checking for the float case ***/
1839
1840static int
410a0ff2
SDJ
1841parse_number (struct parser_state *par_state,
1842 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
c906108c 1843{
20562150
TT
1844 ULONGEST n = 0;
1845 ULONGEST prevn = 0;
c906108c
SS
1846 ULONGEST un;
1847
710122da
DC
1848 int i = 0;
1849 int c;
1850 int base = input_radix;
c906108c
SS
1851 int unsigned_p = 0;
1852
1853 /* Number of "L" suffixes encountered. */
1854 int long_p = 0;
1855
1856 /* We have found a "L" or "U" suffix. */
1857 int found_suffix = 0;
1858
1859 ULONGEST high_bit;
1860 struct type *signed_type;
1861 struct type *unsigned_type;
d7561cbb
KS
1862 char *p;
1863
224c3ddb 1864 p = (char *) alloca (len);
d7561cbb 1865 memcpy (p, buf, len);
c906108c
SS
1866
1867 if (parsed_float)
1868 {
edd079d9 1869 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
fe9441f6 1870 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
27bc4d80 1871 {
edd079d9 1872 putithere->typed_val_float.type
410a0ff2 1873 = parse_type (par_state)->builtin_decfloat;
edd079d9 1874 len -= 2;
27bc4d80 1875 }
edd079d9 1876 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
27bc4d80 1877 {
edd079d9 1878 putithere->typed_val_float.type
410a0ff2 1879 = parse_type (par_state)->builtin_decdouble;
edd079d9 1880 len -= 2;
27bc4d80 1881 }
edd079d9 1882 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
27bc4d80 1883 {
edd079d9 1884 putithere->typed_val_float.type
410a0ff2 1885 = parse_type (par_state)->builtin_declong;
edd079d9
UW
1886 len -= 2;
1887 }
1888 /* Handle suffixes: 'f' for float, 'l' for long double. */
b1b60145 1889 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
edd079d9
UW
1890 {
1891 putithere->typed_val_float.type
1892 = parse_type (par_state)->builtin_float;
1893 len -= 1;
1894 }
b1b60145 1895 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
edd079d9
UW
1896 {
1897 putithere->typed_val_float.type
1898 = parse_type (par_state)->builtin_long_double;
1899 len -= 1;
1900 }
1901 /* Default type for floating-point literals is double. */
1902 else
1903 {
1904 putithere->typed_val_float.type
1905 = parse_type (par_state)->builtin_double;
27bc4d80
TJB
1906 }
1907
edd079d9
UW
1908 if (!parse_float (p, len,
1909 putithere->typed_val_float.type,
1910 putithere->typed_val_float.val))
1911 return ERROR;
c906108c
SS
1912 return FLOAT;
1913 }
1914
1915 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
ebf13736 1916 if (p[0] == '0' && len > 1)
c906108c
SS
1917 switch (p[1])
1918 {
1919 case 'x':
1920 case 'X':
1921 if (len >= 3)
1922 {
1923 p += 2;
1924 base = 16;
1925 len -= 2;
1926 }
1927 break;
1928
b5cfddf5
JK
1929 case 'b':
1930 case 'B':
1931 if (len >= 3)
1932 {
1933 p += 2;
1934 base = 2;
1935 len -= 2;
1936 }
1937 break;
1938
c906108c
SS
1939 case 't':
1940 case 'T':
1941 case 'd':
1942 case 'D':
1943 if (len >= 3)
1944 {
1945 p += 2;
1946 base = 10;
1947 len -= 2;
1948 }
1949 break;
1950
1951 default:
1952 base = 8;
1953 break;
1954 }
1955
1956 while (len-- > 0)
1957 {
1958 c = *p++;
1959 if (c >= 'A' && c <= 'Z')
1960 c += 'a' - 'A';
1961 if (c != 'l' && c != 'u')
1962 n *= base;
1963 if (c >= '0' && c <= '9')
1964 {
1965 if (found_suffix)
1966 return ERROR;
1967 n += i = c - '0';
1968 }
1969 else
1970 {
1971 if (base > 10 && c >= 'a' && c <= 'f')
1972 {
1973 if (found_suffix)
1974 return ERROR;
1975 n += i = c - 'a' + 10;
1976 }
1977 else if (c == 'l')
1978 {
1979 ++long_p;
1980 found_suffix = 1;
1981 }
1982 else if (c == 'u')
1983 {
1984 unsigned_p = 1;
1985 found_suffix = 1;
1986 }
1987 else
1988 return ERROR; /* Char not a digit */
1989 }
1990 if (i >= base)
1991 return ERROR; /* Invalid digit in this base */
1992
1993 /* Portably test for overflow (only works for nonzero values, so make
1994 a second check for zero). FIXME: Can't we just make n and prevn
1995 unsigned and avoid this? */
1996 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1997 unsigned_p = 1; /* Try something unsigned */
1998
1999 /* Portably test for unsigned overflow.
2000 FIXME: This check is wrong; for example it doesn't find overflow
2001 on 0x123456789 when LONGEST is 32 bits. */
2002 if (c != 'l' && c != 'u' && n != 0)
2003 {
20562150 2004 if (unsigned_p && prevn >= n)
001083c6 2005 error (_("Numeric constant too large."));
c906108c
SS
2006 }
2007 prevn = n;
2008 }
2009
2010 /* An integer constant is an int, a long, or a long long. An L
2011 suffix forces it to be long; an LL suffix forces it to be long
2012 long. If not forced to a larger size, it gets the first type of
2013 the above that it fits in. To figure out whether it fits, we
2014 shift it right and see whether anything remains. Note that we
2015 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2016 operation, because many compilers will warn about such a shift
9a76efb6
UW
2017 (which always produces a zero result). Sometimes gdbarch_int_bit
2018 or gdbarch_long_bit will be that big, sometimes not. To deal with
c906108c
SS
2019 the case where it is we just always shift the value more than
2020 once, with fewer bits each time. */
2021
20562150 2022 un = n >> 2;
c906108c 2023 if (long_p == 0
fa9f5be6 2024 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
c906108c 2025 {
410a0ff2 2026 high_bit
fa9f5be6 2027 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
c906108c
SS
2028
2029 /* A large decimal (not hex or octal) constant (between INT_MAX
2030 and UINT_MAX) is a long or unsigned long, according to ANSI,
2031 never an unsigned int, but this code treats it as unsigned
2032 int. This probably should be fixed. GCC gives a warning on
2033 such constants. */
2034
410a0ff2
SDJ
2035 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2036 signed_type = parse_type (par_state)->builtin_int;
c906108c
SS
2037 }
2038 else if (long_p <= 1
fa9f5be6 2039 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
c906108c 2040 {
410a0ff2 2041 high_bit
fa9f5be6 2042 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
410a0ff2
SDJ
2043 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2044 signed_type = parse_type (par_state)->builtin_long;
c906108c
SS
2045 }
2046 else
2047 {
2048 int shift;
4d29c0a8 2049 if (sizeof (ULONGEST) * HOST_CHAR_BIT
fa9f5be6 2050 < gdbarch_long_long_bit (par_state->gdbarch ()))
c906108c
SS
2051 /* A long long does not fit in a LONGEST. */
2052 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2053 else
fa9f5be6 2054 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
c906108c 2055 high_bit = (ULONGEST) 1 << shift;
410a0ff2
SDJ
2056 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2057 signed_type = parse_type (par_state)->builtin_long_long;
c906108c
SS
2058 }
2059
2060 putithere->typed_val_int.val = n;
2061
2062 /* If the high bit of the worked out type is set then this number
2063 has to be unsigned. */
2064
4d29c0a8 2065 if (unsigned_p || (n & high_bit))
c906108c
SS
2066 {
2067 putithere->typed_val_int.type = unsigned_type;
2068 }
4d29c0a8 2069 else
c906108c
SS
2070 {
2071 putithere->typed_val_int.type = signed_type;
2072 }
2073
2074 return INT;
2075}
2076
6c7a06a3
TT
2077/* Temporary obstack used for holding strings. */
2078static struct obstack tempbuf;
2079static int tempbuf_init;
2080
2081/* Parse a C escape sequence. The initial backslash of the sequence
2082 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2083 last character of the sequence. If OUTPUT is not NULL, the
2084 translated form of the escape sequence will be written there. If
2085 OUTPUT is NULL, no output is written and the call will only affect
2086 *PTR. If an escape sequence is expressed in target bytes, then the
2087 entire sequence will simply be copied to OUTPUT. Return 1 if any
2088 character was emitted, 0 otherwise. */
2089
2090int
d7561cbb 2091c_parse_escape (const char **ptr, struct obstack *output)
6c7a06a3 2092{
d7561cbb 2093 const char *tokptr = *ptr;
6c7a06a3
TT
2094 int result = 1;
2095
2096 /* Some escape sequences undergo character set conversion. Those we
2097 translate here. */
2098 switch (*tokptr)
2099 {
2100 /* Hex escapes do not undergo character set conversion, so keep
2101 the escape sequence for later. */
2102 case 'x':
2103 if (output)
2104 obstack_grow_str (output, "\\x");
2105 ++tokptr;
b1b60145 2106 if (!ISXDIGIT (*tokptr))
6c7a06a3 2107 error (_("\\x escape without a following hex digit"));
b1b60145 2108 while (ISXDIGIT (*tokptr))
6c7a06a3
TT
2109 {
2110 if (output)
2111 obstack_1grow (output, *tokptr);
2112 ++tokptr;
2113 }
2114 break;
2115
2116 /* Octal escapes do not undergo character set conversion, so
2117 keep the escape sequence for later. */
2118 case '0':
2119 case '1':
2120 case '2':
2121 case '3':
2122 case '4':
2123 case '5':
2124 case '6':
2125 case '7':
30b66ecc
TT
2126 {
2127 int i;
2128 if (output)
2129 obstack_grow_str (output, "\\");
2130 for (i = 0;
b1b60145 2131 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
30b66ecc
TT
2132 ++i)
2133 {
2134 if (output)
2135 obstack_1grow (output, *tokptr);
2136 ++tokptr;
2137 }
2138 }
6c7a06a3
TT
2139 break;
2140
2141 /* We handle UCNs later. We could handle them here, but that
2142 would mean a spurious error in the case where the UCN could
2143 be converted to the target charset but not the host
2144 charset. */
2145 case 'u':
2146 case 'U':
2147 {
2148 char c = *tokptr;
2149 int i, len = c == 'U' ? 8 : 4;
2150 if (output)
2151 {
2152 obstack_1grow (output, '\\');
2153 obstack_1grow (output, *tokptr);
2154 }
2155 ++tokptr;
b1b60145 2156 if (!ISXDIGIT (*tokptr))
6c7a06a3 2157 error (_("\\%c escape without a following hex digit"), c);
b1b60145 2158 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
6c7a06a3
TT
2159 {
2160 if (output)
2161 obstack_1grow (output, *tokptr);
2162 ++tokptr;
2163 }
2164 }
2165 break;
2166
2167 /* We must pass backslash through so that it does not
2168 cause quoting during the second expansion. */
2169 case '\\':
2170 if (output)
2171 obstack_grow_str (output, "\\\\");
2172 ++tokptr;
2173 break;
2174
2175 /* Escapes which undergo conversion. */
2176 case 'a':
2177 if (output)
2178 obstack_1grow (output, '\a');
2179 ++tokptr;
2180 break;
2181 case 'b':
2182 if (output)
2183 obstack_1grow (output, '\b');
2184 ++tokptr;
2185 break;
2186 case 'f':
2187 if (output)
2188 obstack_1grow (output, '\f');
2189 ++tokptr;
2190 break;
2191 case 'n':
2192 if (output)
2193 obstack_1grow (output, '\n');
2194 ++tokptr;
2195 break;
2196 case 'r':
2197 if (output)
2198 obstack_1grow (output, '\r');
2199 ++tokptr;
2200 break;
2201 case 't':
2202 if (output)
2203 obstack_1grow (output, '\t');
2204 ++tokptr;
2205 break;
2206 case 'v':
2207 if (output)
2208 obstack_1grow (output, '\v');
2209 ++tokptr;
2210 break;
2211
2212 /* GCC extension. */
2213 case 'e':
2214 if (output)
2215 obstack_1grow (output, HOST_ESCAPE_CHAR);
2216 ++tokptr;
2217 break;
2218
2219 /* Backslash-newline expands to nothing at all. */
2220 case '\n':
2221 ++tokptr;
2222 result = 0;
2223 break;
2224
2225 /* A few escapes just expand to the character itself. */
2226 case '\'':
2227 case '\"':
2228 case '?':
2229 /* GCC extensions. */
2230 case '(':
2231 case '{':
2232 case '[':
2233 case '%':
2234 /* Unrecognized escapes turn into the character itself. */
2235 default:
2236 if (output)
2237 obstack_1grow (output, *tokptr);
2238 ++tokptr;
2239 break;
2240 }
2241 *ptr = tokptr;
2242 return result;
2243}
2244
2245/* Parse a string or character literal from TOKPTR. The string or
2246 character may be wide or unicode. *OUTPTR is set to just after the
2247 end of the literal in the input string. The resulting token is
2248 stored in VALUE. This returns a token value, either STRING or
2249 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2250 number of host characters in the literal. */
4d29c0a8 2251
6c7a06a3 2252static int
d7561cbb
KS
2253parse_string_or_char (const char *tokptr, const char **outptr,
2254 struct typed_stoken *value, int *host_chars)
6c7a06a3 2255{
8c5630cb 2256 int quote;
0c801b96 2257 c_string_type type;
f2e8016f 2258 int is_objc = 0;
6c7a06a3
TT
2259
2260 /* Build the gdb internal form of the input string in tempbuf. Note
2261 that the buffer is null byte terminated *only* for the
2262 convenience of debugging gdb itself and printing the buffer
2263 contents when the buffer contains no embedded nulls. Gdb does
2264 not depend upon the buffer being null byte terminated, it uses
2265 the length string instead. This allows gdb to handle C strings
2266 (as well as strings in other languages) with embedded null
2267 bytes */
2268
2269 if (!tempbuf_init)
2270 tempbuf_init = 1;
2271 else
2272 obstack_free (&tempbuf, NULL);
2273 obstack_init (&tempbuf);
2274
2275 /* Record the string type. */
2276 if (*tokptr == 'L')
2277 {
2278 type = C_WIDE_STRING;
2279 ++tokptr;
2280 }
2281 else if (*tokptr == 'u')
2282 {
2283 type = C_STRING_16;
2284 ++tokptr;
2285 }
2286 else if (*tokptr == 'U')
2287 {
2288 type = C_STRING_32;
2289 ++tokptr;
2290 }
f2e8016f
TT
2291 else if (*tokptr == '@')
2292 {
2293 /* An Objective C string. */
2294 is_objc = 1;
2295 type = C_STRING;
2296 ++tokptr;
2297 }
6c7a06a3
TT
2298 else
2299 type = C_STRING;
2300
2301 /* Skip the quote. */
2302 quote = *tokptr;
2303 if (quote == '\'')
2304 type |= C_CHAR;
2305 ++tokptr;
2306
2307 *host_chars = 0;
2308
2309 while (*tokptr)
2310 {
2311 char c = *tokptr;
2312 if (c == '\\')
2313 {
2314 ++tokptr;
2315 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2316 }
2317 else if (c == quote)
2318 break;
2319 else
2320 {
2321 obstack_1grow (&tempbuf, c);
2322 ++tokptr;
2323 /* FIXME: this does the wrong thing with multi-byte host
2324 characters. We could use mbrlen here, but that would
2325 make "set host-charset" a bit less useful. */
2326 ++*host_chars;
2327 }
2328 }
2329
2330 if (*tokptr != quote)
2331 {
2332 if (quote == '"')
001083c6 2333 error (_("Unterminated string in expression."));
6c7a06a3 2334 else
001083c6 2335 error (_("Unmatched single quote."));
6c7a06a3
TT
2336 }
2337 ++tokptr;
2338
2339 value->type = type;
79f33898 2340 value->ptr = (char *) obstack_base (&tempbuf);
6c7a06a3
TT
2341 value->length = obstack_object_size (&tempbuf);
2342
2343 *outptr = tokptr;
2344
f2e8016f 2345 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
6c7a06a3
TT
2346}
2347
274b54d7
TT
2348/* This is used to associate some attributes with a token. */
2349
8d297bbf 2350enum token_flag
274b54d7
TT
2351{
2352 /* If this bit is set, the token is C++-only. */
2353
2354 FLAG_CXX = 1,
2355
2356 /* If this bit is set, the token is conditional: if there is a
2357 symbol of the same name, then the token is a symbol; otherwise,
2358 the token is a keyword. */
2359
2360 FLAG_SHADOW = 2
2361};
8d297bbf 2362DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
274b54d7 2363
c906108c
SS
2364struct token
2365{
a121b7c1 2366 const char *oper;
c906108c
SS
2367 int token;
2368 enum exp_opcode opcode;
8d297bbf 2369 token_flags flags;
c906108c
SS
2370};
2371
2372static const struct token tokentab3[] =
2373 {
ba163c7e 2374 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
c1af96a0 2375 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
274b54d7 2376 {"->*", ARROW_STAR, BINOP_END, FLAG_CXX},
a6fb9c08 2377 {"...", DOTDOTDOT, BINOP_END, 0}
c906108c
SS
2378 };
2379
2380static const struct token tokentab2[] =
2381 {
ba163c7e
TT
2382 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2383 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2384 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2385 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2386 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2387 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2388 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2389 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
2390 {"++", INCREMENT, BINOP_END, 0},
2391 {"--", DECREMENT, BINOP_END, 0},
2392 {"->", ARROW, BINOP_END, 0},
2393 {"&&", ANDAND, BINOP_END, 0},
2394 {"||", OROR, BINOP_END, 0},
ec7f2efe
KS
2395 /* "::" is *not* only C++: gdb overrides its meaning in several
2396 different ways, e.g., 'filename'::func, function::variable. */
ba163c7e
TT
2397 {"::", COLONCOLON, BINOP_END, 0},
2398 {"<<", LSH, BINOP_END, 0},
2399 {">>", RSH, BINOP_END, 0},
2400 {"==", EQUAL, BINOP_END, 0},
2401 {"!=", NOTEQUAL, BINOP_END, 0},
2402 {"<=", LEQ, BINOP_END, 0},
c1af96a0 2403 {">=", GEQ, BINOP_END, 0},
274b54d7 2404 {".*", DOT_STAR, BINOP_END, FLAG_CXX}
ba163c7e
TT
2405 };
2406
b6c95c0c
AB
2407/* Identifier-like tokens. Only type-specifiers than can appear in
2408 multi-word type names (for example 'double' can appear in 'long
2409 double') need to be listed here. type-specifiers that are only ever
2410 single word (like 'float') are handled by the classify_name function. */
ba163c7e
TT
2411static const struct token ident_tokens[] =
2412 {
2413 {"unsigned", UNSIGNED, OP_NULL, 0},
274b54d7 2414 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
ba163c7e
TT
2415 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2416 {"struct", STRUCT, OP_NULL, 0},
2417 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2418 {"sizeof", SIZEOF, OP_NULL, 0},
007e1530
TT
2419 {"_Alignof", ALIGNOF, OP_NULL, 0},
2420 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
ba163c7e 2421 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
274b54d7
TT
2422 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2423 {"class", CLASS, OP_NULL, FLAG_CXX},
ba163c7e
TT
2424 {"union", UNION, OP_NULL, 0},
2425 {"short", SHORT, OP_NULL, 0},
2426 {"const", CONST_KEYWORD, OP_NULL, 0},
2427 {"enum", ENUM, OP_NULL, 0},
2428 {"long", LONG, OP_NULL, 0},
274b54d7 2429 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
ba163c7e 2430 {"int", INT_KEYWORD, OP_NULL, 0},
274b54d7
TT
2431 {"new", NEW, OP_NULL, FLAG_CXX},
2432 {"delete", DELETE, OP_NULL, FLAG_CXX},
2433 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2434
2435 {"and", ANDAND, BINOP_END, FLAG_CXX},
2436 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2437 {"bitand", '&', OP_NULL, FLAG_CXX},
2438 {"bitor", '|', OP_NULL, FLAG_CXX},
2439 {"compl", '~', OP_NULL, FLAG_CXX},
2440 {"not", '!', OP_NULL, FLAG_CXX},
2441 {"not_eq", NOTEQUAL, BINOP_END, FLAG_CXX},
2442 {"or", OROR, BINOP_END, FLAG_CXX},
2443 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2444 {"xor", '^', OP_NULL, FLAG_CXX},
2445 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2446
2447 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2448 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2449 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
608b4967
TT
2450 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2451
2452 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2453 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2454 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2455 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
6e72ca20
TT
2456 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2457
2458 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
c906108c
SS
2459 };
2460
7c8adf68
TT
2461
2462static void
2463scan_macro_expansion (char *expansion)
2464{
2465 char *copy;
2466
2467 /* We'd better not be trying to push the stack twice. */
9d30e1fd 2468 gdb_assert (! cpstate->macro_original_text);
7c8adf68
TT
2469
2470 /* Copy to the obstack, and then free the intermediate
2471 expansion. */
9d30e1fd 2472 copy = (char *) obstack_copy0 (&cpstate->expansion_obstack, expansion,
224c3ddb 2473 strlen (expansion));
7c8adf68
TT
2474 xfree (expansion);
2475
2476 /* Save the old lexptr value, so we can return to it when we're done
2477 parsing the expanded text. */
9d30e1fd 2478 cpstate->macro_original_text = lexptr;
7c8adf68
TT
2479 lexptr = copy;
2480}
2481
7c8adf68
TT
2482static int
2483scanning_macro_expansion (void)
2484{
9d30e1fd 2485 return cpstate->macro_original_text != 0;
7c8adf68
TT
2486}
2487
4d29c0a8 2488static void
7c8adf68
TT
2489finished_macro_expansion (void)
2490{
2491 /* There'd better be something to pop back to. */
9d30e1fd 2492 gdb_assert (cpstate->macro_original_text);
7c8adf68
TT
2493
2494 /* Pop back to the original text. */
9d30e1fd
TT
2495 lexptr = cpstate->macro_original_text;
2496 cpstate->macro_original_text = 0;
7c8adf68
TT
2497}
2498
4e8f195d
TT
2499/* Return true iff the token represents a C++ cast operator. */
2500
2501static int
2502is_cast_operator (const char *token, int len)
2503{
2504 return (! strncmp (token, "dynamic_cast", len)
2505 || ! strncmp (token, "static_cast", len)
2506 || ! strncmp (token, "reinterpret_cast", len)
2507 || ! strncmp (token, "const_cast", len));
2508}
7c8adf68
TT
2509
2510/* The scope used for macro expansion. */
2511static struct macro_scope *expression_macro_scope;
2512
65d12d83
TT
2513/* This is set if a NAME token appeared at the very end of the input
2514 string, with no whitespace separating the name from the EOF. This
2515 is used only when parsing to do field name completion. */
2516static int saw_name_at_eof;
2517
2518/* This is set if the previously-returned token was a structure
59498c30
LS
2519 operator -- either '.' or ARROW. */
2520static bool last_was_structop;
65d12d83 2521
c906108c
SS
2522/* Read one token, getting characters through lexptr. */
2523
2524static int
59498c30 2525lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
c906108c
SS
2526{
2527 int c;
2528 int namelen;
2529 unsigned int i;
d7561cbb 2530 const char *tokstart;
59498c30 2531 bool saw_structop = last_was_structop;
ba163c7e 2532 char *copy;
65d12d83 2533
59498c30
LS
2534 last_was_structop = false;
2535 *is_quoted_name = false;
65d12d83 2536
c906108c
SS
2537 retry:
2538
84f0252a
JB
2539 /* Check if this is a macro invocation that we need to expand. */
2540 if (! scanning_macro_expansion ())
2541 {
2542 char *expanded = macro_expand_next (&lexptr,
7c8adf68
TT
2543 standard_macro_lookup,
2544 expression_macro_scope);
84f0252a
JB
2545
2546 if (expanded)
2547 scan_macro_expansion (expanded);
2548 }
2549
665132f9 2550 prev_lexptr = lexptr;
c906108c
SS
2551
2552 tokstart = lexptr;
2553 /* See if it is a special token of length 3. */
2554 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
fe978cb0 2555 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
c906108c 2556 {
274b54d7 2557 if ((tokentab3[i].flags & FLAG_CXX) != 0
73923d7e 2558 && par_state->language ()->la_language != language_cplus)
ec7f2efe
KS
2559 break;
2560
c906108c
SS
2561 lexptr += 3;
2562 yylval.opcode = tokentab3[i].opcode;
2563 return tokentab3[i].token;
2564 }
2565
2566 /* See if it is a special token of length 2. */
2567 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
fe978cb0 2568 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
c906108c 2569 {
274b54d7 2570 if ((tokentab2[i].flags & FLAG_CXX) != 0
73923d7e 2571 && par_state->language ()->la_language != language_cplus)
ec7f2efe
KS
2572 break;
2573
c906108c
SS
2574 lexptr += 2;
2575 yylval.opcode = tokentab2[i].opcode;
59498c30 2576 if (tokentab2[i].token == ARROW)
65d12d83 2577 last_was_structop = 1;
c906108c
SS
2578 return tokentab2[i].token;
2579 }
2580
2581 switch (c = *tokstart)
2582 {
2583 case 0:
84f0252a
JB
2584 /* If we were just scanning the result of a macro expansion,
2585 then we need to resume scanning the original text.
65d12d83
TT
2586 If we're parsing for field name completion, and the previous
2587 token allows such completion, return a COMPLETE token.
84f0252a
JB
2588 Otherwise, we were already scanning the original text, and
2589 we're really done. */
2590 if (scanning_macro_expansion ())
2591 {
2592 finished_macro_expansion ();
2593 goto retry;
2594 }
65d12d83
TT
2595 else if (saw_name_at_eof)
2596 {
2597 saw_name_at_eof = 0;
2598 return COMPLETE;
2599 }
59498c30 2600 else if (parse_completion && saw_structop)
65d12d83 2601 return COMPLETE;
84f0252a
JB
2602 else
2603 return 0;
c906108c
SS
2604
2605 case ' ':
2606 case '\t':
2607 case '\n':
2608 lexptr++;
2609 goto retry;
2610
379a77b5 2611 case '[':
c906108c
SS
2612 case '(':
2613 paren_depth++;
2614 lexptr++;
73923d7e 2615 if (par_state->language ()->la_language == language_objc
410a0ff2 2616 && c == '[')
f2e8016f 2617 return OBJC_LBRAC;
c906108c
SS
2618 return c;
2619
379a77b5 2620 case ']':
c906108c
SS
2621 case ')':
2622 if (paren_depth == 0)
2623 return 0;
2624 paren_depth--;
2625 lexptr++;
2626 return c;
2627
2628 case ',':
84f0252a
JB
2629 if (comma_terminates
2630 && paren_depth == 0
2631 && ! scanning_macro_expansion ())
c906108c
SS
2632 return 0;
2633 lexptr++;
2634 return c;
2635
2636 case '.':
2637 /* Might be a floating point number. */
2638 if (lexptr[1] < '0' || lexptr[1] > '9')
65d12d83 2639 {
59498c30 2640 last_was_structop = true;
65d12d83
TT
2641 goto symbol; /* Nope, must be a symbol. */
2642 }
86a73007 2643 /* FALL THRU. */
c906108c
SS
2644
2645 case '0':
2646 case '1':
2647 case '2':
2648 case '3':
2649 case '4':
2650 case '5':
2651 case '6':
2652 case '7':
2653 case '8':
2654 case '9':
2655 {
2656 /* It's a number. */
2657 int got_dot = 0, got_e = 0, toktype;
d7561cbb 2658 const char *p = tokstart;
c906108c
SS
2659 int hex = input_radix > 10;
2660
2661 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2662 {
2663 p += 2;
2664 hex = 1;
2665 }
2666 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2667 {
2668 p += 2;
2669 hex = 0;
2670 }
2671
2672 for (;; ++p)
2673 {
2674 /* This test includes !hex because 'e' is a valid hex digit
2675 and thus does not indicate a floating point number when
2676 the radix is hex. */
2677 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
2678 got_dot = got_e = 1;
2679 /* This test does not include !hex, because a '.' always indicates
2680 a decimal floating point number regardless of the radix. */
2681 else if (!got_dot && *p == '.')
2682 got_dot = 1;
2683 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
2684 && (*p == '-' || *p == '+'))
2685 /* This is the sign of the exponent, not the end of the
2686 number. */
2687 continue;
2688 /* We will take any letters or digits. parse_number will
2689 complain if past the radix, or if L or U are not final. */
2690 else if ((*p < '0' || *p > '9')
2691 && ((*p < 'a' || *p > 'z')
2692 && (*p < 'A' || *p > 'Z')))
2693 break;
2694 }
410a0ff2
SDJ
2695 toktype = parse_number (par_state, tokstart, p - tokstart,
2696 got_dot|got_e, &yylval);
c906108c
SS
2697 if (toktype == ERROR)
2698 {
2699 char *err_copy = (char *) alloca (p - tokstart + 1);
2700
2701 memcpy (err_copy, tokstart, p - tokstart);
2702 err_copy[p - tokstart] = 0;
001083c6 2703 error (_("Invalid number \"%s\"."), err_copy);
c906108c
SS
2704 }
2705 lexptr = p;
2706 return toktype;
2707 }
2708
941b2081
JK
2709 case '@':
2710 {
d7561cbb 2711 const char *p = &tokstart[1];
941b2081 2712
73923d7e 2713 if (par_state->language ()->la_language == language_objc)
f2e8016f
TT
2714 {
2715 size_t len = strlen ("selector");
2716
2717 if (strncmp (p, "selector", len) == 0
b1b60145 2718 && (p[len] == '\0' || ISSPACE (p[len])))
f2e8016f
TT
2719 {
2720 lexptr = p + len;
2721 return SELECTOR;
2722 }
2723 else if (*p == '"')
2724 goto parse_string;
2725 }
2726
b1b60145 2727 while (ISSPACE (*p))
941b2081 2728 p++;
b926417a 2729 size_t len = strlen ("entry");
b1b60145 2730 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
941b2081
JK
2731 && p[len] != '_')
2732 {
2733 lexptr = &p[len];
2734 return ENTRY;
2735 }
2736 }
2737 /* FALLTHRU */
c906108c
SS
2738 case '+':
2739 case '-':
2740 case '*':
2741 case '/':
2742 case '%':
2743 case '|':
2744 case '&':
2745 case '^':
2746 case '~':
2747 case '!':
c906108c
SS
2748 case '<':
2749 case '>':
c906108c
SS
2750 case '?':
2751 case ':':
2752 case '=':
2753 case '{':
2754 case '}':
2755 symbol:
2756 lexptr++;
2757 return c;
2758
6c7a06a3
TT
2759 case 'L':
2760 case 'u':
2761 case 'U':
2762 if (tokstart[1] != '"' && tokstart[1] != '\'')
2763 break;
2764 /* Fall through. */
2765 case '\'':
c906108c 2766 case '"':
f2e8016f
TT
2767
2768 parse_string:
6c7a06a3
TT
2769 {
2770 int host_len;
2771 int result = parse_string_or_char (tokstart, &lexptr, &yylval.tsval,
2772 &host_len);
2773 if (result == CHAR)
c906108c 2774 {
6c7a06a3 2775 if (host_len == 0)
001083c6 2776 error (_("Empty character constant."));
6c7a06a3 2777 else if (host_len > 2 && c == '\'')
c906108c 2778 {
6c7a06a3
TT
2779 ++tokstart;
2780 namelen = lexptr - tokstart - 1;
59498c30 2781 *is_quoted_name = true;
805e1f19 2782
6c7a06a3 2783 goto tryname;
c906108c 2784 }
6c7a06a3 2785 else if (host_len > 1)
001083c6 2786 error (_("Invalid character constant."));
c906108c 2787 }
6c7a06a3
TT
2788 return result;
2789 }
c906108c
SS
2790 }
2791
b1b60145 2792 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
c906108c 2793 /* We must have come across a bad character (e.g. ';'). */
001083c6 2794 error (_("Invalid character '%c' in expression."), c);
c906108c
SS
2795
2796 /* It's a name. See how long it is. */
2797 namelen = 0;
2798 for (c = tokstart[namelen];
b1b60145 2799 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
c906108c
SS
2800 {
2801 /* Template parameter lists are part of the name.
2802 FIXME: This mishandles `print $a<4&&$a>3'. */
2803
2804 if (c == '<')
4e8f195d
TT
2805 {
2806 if (! is_cast_operator (tokstart, namelen))
2807 {
2808 /* Scan ahead to get rest of the template specification. Note
2809 that we look ahead only when the '<' adjoins non-whitespace
2810 characters; for comparison expressions, e.g. "a < b > c",
2811 there must be spaces before the '<', etc. */
d7561cbb
KS
2812 const char *p = find_template_name_end (tokstart + namelen);
2813
4e8f195d
TT
2814 if (p)
2815 namelen = p - tokstart;
2816 }
2817 break;
c906108c
SS
2818 }
2819 c = tokstart[++namelen];
2820 }
2821
84f0252a
JB
2822 /* The token "if" terminates the expression and is NOT removed from
2823 the input stream. It doesn't count if it appears in the
2824 expansion of a macro. */
2825 if (namelen == 2
2826 && tokstart[0] == 'i'
2827 && tokstart[1] == 'f'
2828 && ! scanning_macro_expansion ())
c906108c
SS
2829 {
2830 return 0;
2831 }
2832
b6199126
DJ
2833 /* For the same reason (breakpoint conditions), "thread N"
2834 terminates the expression. "thread" could be an identifier, but
2835 an identifier is never followed by a number without intervening
2836 punctuation. "task" is similar. Handle abbreviations of these,
2837 similarly to breakpoint.c:find_condition_and_thread. */
2838 if (namelen >= 1
2839 && (strncmp (tokstart, "thread", namelen) == 0
2840 || strncmp (tokstart, "task", namelen) == 0)
2841 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2842 && ! scanning_macro_expansion ())
2843 {
d7561cbb
KS
2844 const char *p = tokstart + namelen + 1;
2845
b6199126
DJ
2846 while (*p == ' ' || *p == '\t')
2847 p++;
2848 if (*p >= '0' && *p <= '9')
2849 return 0;
2850 }
2851
c906108c
SS
2852 lexptr += namelen;
2853
2854 tryname:
2855
c906108c
SS
2856 yylval.sval.ptr = tokstart;
2857 yylval.sval.length = namelen;
2858
ba163c7e
TT
2859 /* Catch specific keywords. */
2860 copy = copy_name (yylval.sval);
2861 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
fe978cb0 2862 if (strcmp (copy, ident_tokens[i].oper) == 0)
ba163c7e 2863 {
274b54d7 2864 if ((ident_tokens[i].flags & FLAG_CXX) != 0
73923d7e 2865 && par_state->language ()->la_language != language_cplus)
ba163c7e
TT
2866 break;
2867
274b54d7
TT
2868 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2869 {
1993b719 2870 struct field_of_this_result is_a_field_of_this;
274b54d7
TT
2871
2872 if (lookup_symbol (copy, expression_context_block,
2873 VAR_DOMAIN,
73923d7e 2874 (par_state->language ()->la_language
410a0ff2 2875 == language_cplus ? &is_a_field_of_this
d12307c1 2876 : NULL)).symbol
274b54d7
TT
2877 != NULL)
2878 {
2879 /* The keyword is shadowed. */
2880 break;
2881 }
2882 }
2883
ba163c7e
TT
2884 /* It is ok to always set this, even though we don't always
2885 strictly need to. */
2886 yylval.opcode = ident_tokens[i].opcode;
2887 return ident_tokens[i].token;
2888 }
2889
c906108c 2890 if (*tokstart == '$')
cfeadda5 2891 return DOLLAR_VARIABLE;
48e32051 2892
155da517 2893 if (parse_completion && *lexptr == '\0')
48e32051 2894 saw_name_at_eof = 1;
e234dfaf
TT
2895
2896 yylval.ssym.stoken = yylval.sval;
d12307c1
PMR
2897 yylval.ssym.sym.symbol = NULL;
2898 yylval.ssym.sym.block = NULL;
e234dfaf 2899 yylval.ssym.is_a_field_of_this = 0;
48e32051
TT
2900 return NAME;
2901}
2902
2903/* An object of this type is pushed on a FIFO by the "outer" lexer. */
5fe3f3e4 2904struct token_and_value
48e32051
TT
2905{
2906 int token;
e707a91d 2907 YYSTYPE value;
5fe3f3e4 2908};
48e32051
TT
2909
2910/* A FIFO of tokens that have been read but not yet returned to the
2911 parser. */
5fe3f3e4 2912static std::vector<token_and_value> token_fifo;
48e32051
TT
2913
2914/* Non-zero if the lexer should return tokens from the FIFO. */
2915static int popping;
2916
2917/* Temporary storage for c_lex; this holds symbol names as they are
2918 built up. */
8268c778 2919auto_obstack name_obstack;
48e32051
TT
2920
2921/* Classify a NAME token. The contents of the token are in `yylval'.
2922 Updates yylval and returns the new token type. BLOCK is the block
805e1f19
TT
2923 in which lookups start; this can be NULL to mean the global scope.
2924 IS_QUOTED_NAME is non-zero if the name token was originally quoted
59498c30
LS
2925 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
2926 a structure operator -- either '.' or ARROW */
4d29c0a8 2927
48e32051 2928static int
410a0ff2 2929classify_name (struct parser_state *par_state, const struct block *block,
59498c30 2930 bool is_quoted_name, bool is_after_structop)
48e32051 2931{
d12307c1 2932 struct block_symbol bsym;
48e32051 2933 char *copy;
1993b719 2934 struct field_of_this_result is_a_field_of_this;
48e32051
TT
2935
2936 copy = copy_name (yylval.sval);
2937
1993b719
TT
2938 /* Initialize this in case we *don't* use it in this call; that way
2939 we can refer to it unconditionally below. */
2940 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
2941
d12307c1 2942 bsym = lookup_symbol (copy, block, VAR_DOMAIN,
73923d7e 2943 par_state->language ()->la_name_of_this
d12307c1 2944 ? &is_a_field_of_this : NULL);
48e32051 2945
d12307c1 2946 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
c906108c 2947 {
d12307c1 2948 yylval.ssym.sym = bsym;
1993b719 2949 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
48e32051 2950 return BLOCKNAME;
c906108c 2951 }
d12307c1 2952 else if (!bsym.symbol)
48e32051 2953 {
6592e36f
TT
2954 /* If we found a field of 'this', we might have erroneously
2955 found a constructor where we wanted a type name. Handle this
2956 case by noticing that we found a constructor and then look up
2957 the type tag instead. */
2958 if (is_a_field_of_this.type != NULL
2959 && is_a_field_of_this.fn_field != NULL
2960 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
2961 0))
2962 {
2963 struct field_of_this_result inner_is_a_field_of_this;
2964
d12307c1
PMR
2965 bsym = lookup_symbol (copy, block, STRUCT_DOMAIN,
2966 &inner_is_a_field_of_this);
2967 if (bsym.symbol != NULL)
6592e36f 2968 {
d12307c1 2969 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
6592e36f
TT
2970 return TYPENAME;
2971 }
2972 }
805e1f19 2973
59498c30
LS
2974 /* If we found a field on the "this" object, or we are looking
2975 up a field on a struct, then we want to prefer it over a
805e1f19
TT
2976 filename. However, if the name was quoted, then it is better
2977 to check for a filename or a block, since this is the only
2978 way the user has of requiring the extension to be used. */
59498c30
LS
2979 if ((is_a_field_of_this.type == NULL && !is_after_structop)
2980 || is_quoted_name)
805e1f19
TT
2981 {
2982 /* See if it's a file name. */
2983 struct symtab *symtab;
2984
2985 symtab = lookup_symtab (copy);
2986 if (symtab)
2987 {
439247b6 2988 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
805e1f19
TT
2989 STATIC_BLOCK);
2990 return FILENAME;
2991 }
2992 }
48e32051 2993 }
c906108c 2994
d12307c1 2995 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
48e32051 2996 {
d12307c1 2997 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
47663de5 2998 return TYPENAME;
48e32051 2999 }
c906108c 3000
f2e8016f 3001 /* See if it's an ObjC classname. */
73923d7e 3002 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
f2e8016f 3003 {
fa9f5be6 3004 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (), copy);
f2e8016f
TT
3005 if (Class)
3006 {
d12307c1
PMR
3007 struct symbol *sym;
3008
fe978cb0 3009 yylval.theclass.theclass = Class;
826f0041
TT
3010 sym = lookup_struct_typedef (copy, expression_context_block, 1);
3011 if (sym)
fe978cb0 3012 yylval.theclass.type = SYMBOL_TYPE (sym);
f2e8016f
TT
3013 return CLASSNAME;
3014 }
3015 }
3016
48e32051
TT
3017 /* Input names that aren't symbols but ARE valid hex numbers, when
3018 the input radix permits them, can be names or numbers depending
3019 on the parse. Note we support radixes > 16 here. */
d12307c1 3020 if (!bsym.symbol
48e32051
TT
3021 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3022 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3023 {
3024 YYSTYPE newlval; /* Its value is ignored. */
410a0ff2
SDJ
3025 int hextype = parse_number (par_state, copy, yylval.sval.length,
3026 0, &newlval);
d12307c1 3027
48e32051
TT
3028 if (hextype == INT)
3029 {
d12307c1 3030 yylval.ssym.sym = bsym;
1993b719 3031 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
48e32051
TT
3032 return NAME_OR_INT;
3033 }
3034 }
3035
3036 /* Any other kind of symbol */
d12307c1 3037 yylval.ssym.sym = bsym;
1993b719 3038 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
7322dca9 3039
d12307c1 3040 if (bsym.symbol == NULL
73923d7e 3041 && par_state->language ()->la_language == language_cplus
1993b719 3042 && is_a_field_of_this.type == NULL
3b7344d5 3043 && lookup_minimal_symbol (copy, NULL, NULL).minsym == NULL)
7322dca9
SW
3044 return UNKNOWN_CPP_NAME;
3045
48e32051
TT
3046 return NAME;
3047}
c906108c 3048
48e32051 3049/* Like classify_name, but used by the inner loop of the lexer, when a
e234dfaf
TT
3050 name might have already been seen. CONTEXT is the context type, or
3051 NULL if this is the first component of a name. */
50af5481 3052
48e32051 3053static int
410a0ff2
SDJ
3054classify_inner_name (struct parser_state *par_state,
3055 const struct block *block, struct type *context)
48e32051 3056{
df54f8eb 3057 struct type *type;
48e32051
TT
3058 char *copy;
3059
e234dfaf 3060 if (context == NULL)
59498c30 3061 return classify_name (par_state, block, false, false);
48e32051 3062
e234dfaf 3063 type = check_typedef (context);
3d567982 3064 if (!type_aggregate_p (type))
50af5481 3065 return ERROR;
48e32051 3066
e234dfaf 3067 copy = copy_name (yylval.ssym.stoken);
4dcabcc2
DE
3068 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
3069 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy, block, VAR_DOMAIN);
f7e3ecae
KS
3070
3071 /* If no symbol was found, search for a matching base class named
3072 COPY. This will allow users to enter qualified names of class members
3073 relative to the `this' pointer. */
d12307c1 3074 if (yylval.ssym.sym.symbol == NULL)
f7e3ecae 3075 {
a07e3e18 3076 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
f7e3ecae
KS
3077
3078 if (base_type != NULL)
3079 {
3080 yylval.tsym.type = base_type;
3081 return TYPENAME;
3082 }
3083
3084 return ERROR;
3085 }
50af5481 3086
d12307c1 3087 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
50af5481
JK
3088 {
3089 case LOC_BLOCK:
3090 case LOC_LABEL:
f7e3ecae
KS
3091 /* cp_lookup_nested_symbol might have accidentally found a constructor
3092 named COPY when we really wanted a base class of the same name.
3093 Double-check this case by looking for a base class. */
3094 {
a07e3e18 3095 struct type *base_type = cp_find_type_baseclass_by_name (type, copy);
f7e3ecae
KS
3096
3097 if (base_type != NULL)
3098 {
3099 yylval.tsym.type = base_type;
3100 return TYPENAME;
3101 }
3102 }
50af5481 3103 return ERROR;
48e32051 3104
50af5481 3105 case LOC_TYPEDEF:
d12307c1 3106 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
50af5481 3107 return TYPENAME;
48e32051 3108
50af5481 3109 default:
50af5481
JK
3110 return NAME;
3111 }
3112 internal_error (__FILE__, __LINE__, _("not reached"));
48e32051
TT
3113}
3114
3115/* The outer level of a two-level lexer. This calls the inner lexer
3116 to return tokens. It then either returns these tokens, or
3117 aggregates them into a larger token. This lets us work around a
3118 problem in our parsing approach, where the parser could not
3119 distinguish between qualified names and qualified types at the
3120 right point.
4d29c0a8 3121
48e32051
TT
3122 This approach is still not ideal, because it mishandles template
3123 types. See the comment in lex_one_token for an example. However,
3124 this is still an improvement over the earlier approach, and will
3125 suffice until we move to better parsing technology. */
4d29c0a8 3126
48e32051
TT
3127static int
3128yylex (void)
3129{
3130 token_and_value current;
b2f83c08 3131 int first_was_coloncolon, last_was_coloncolon;
e234dfaf 3132 struct type *context_type = NULL;
b2f83c08
TT
3133 int last_to_examine, next_to_examine, checkpoint;
3134 const struct block *search_block;
59498c30 3135 bool is_quoted_name, last_lex_was_structop;
48e32051 3136
5fe3f3e4 3137 if (popping && !token_fifo.empty ())
b2f83c08 3138 goto do_pop;
48e32051
TT
3139 popping = 0;
3140
59498c30
LS
3141 last_lex_was_structop = last_was_structop;
3142
b2f83c08
TT
3143 /* Read the first token and decide what to do. Most of the
3144 subsequent code is C++-only; but also depends on seeing a "::" or
3145 name-like token. */
410a0ff2 3146 current.token = lex_one_token (pstate, &is_quoted_name);
48e32051 3147 if (current.token == NAME)
410a0ff2 3148 current.token = classify_name (pstate, expression_context_block,
59498c30 3149 is_quoted_name, last_lex_was_structop);
73923d7e 3150 if (pstate->language ()->la_language != language_cplus
b2f83c08
TT
3151 || (current.token != TYPENAME && current.token != COLONCOLON
3152 && current.token != FILENAME))
48e32051
TT
3153 return current.token;
3154
b2f83c08
TT
3155 /* Read any sequence of alternating "::" and name-like tokens into
3156 the token FIFO. */
3157 current.value = yylval;
5fe3f3e4 3158 token_fifo.push_back (current);
b2f83c08
TT
3159 last_was_coloncolon = current.token == COLONCOLON;
3160 while (1)
3161 {
59498c30 3162 bool ignore;
805e1f19
TT
3163
3164 /* We ignore quoted names other than the very first one.
3165 Subsequent ones do not have any special meaning. */
410a0ff2 3166 current.token = lex_one_token (pstate, &ignore);
b2f83c08 3167 current.value = yylval;
5fe3f3e4 3168 token_fifo.push_back (current);
b2f83c08
TT
3169
3170 if ((last_was_coloncolon && current.token != NAME)
3171 || (!last_was_coloncolon && current.token != COLONCOLON))
3172 break;
3173 last_was_coloncolon = !last_was_coloncolon;
3174 }
3175 popping = 1;
3176
3177 /* We always read one extra token, so compute the number of tokens
3178 to examine accordingly. */
5fe3f3e4 3179 last_to_examine = token_fifo.size () - 2;
b2f83c08
TT
3180 next_to_examine = 0;
3181
5fe3f3e4 3182 current = token_fifo[next_to_examine];
b2f83c08
TT
3183 ++next_to_examine;
3184
8268c778 3185 name_obstack.clear ();
b2f83c08
TT
3186 checkpoint = 0;
3187 if (current.token == FILENAME)
3188 search_block = current.value.bval;
3189 else if (current.token == COLONCOLON)
3190 search_block = NULL;
3191 else
e234dfaf 3192 {
b2f83c08
TT
3193 gdb_assert (current.token == TYPENAME);
3194 search_block = expression_context_block;
3195 obstack_grow (&name_obstack, current.value.sval.ptr,
3196 current.value.sval.length);
3197 context_type = current.value.tsym.type;
3198 checkpoint = 1;
e234dfaf 3199 }
b2f83c08
TT
3200
3201 first_was_coloncolon = current.token == COLONCOLON;
3202 last_was_coloncolon = first_was_coloncolon;
3203
3204 while (next_to_examine <= last_to_examine)
48e32051 3205 {
5fe3f3e4 3206 token_and_value next;
48e32051 3207
5fe3f3e4 3208 next = token_fifo[next_to_examine];
b2f83c08 3209 ++next_to_examine;
48e32051 3210
5fe3f3e4 3211 if (next.token == NAME && last_was_coloncolon)
48e32051
TT
3212 {
3213 int classification;
3214
5fe3f3e4 3215 yylval = next.value;
410a0ff2
SDJ
3216 classification = classify_inner_name (pstate, search_block,
3217 context_type);
48e32051
TT
3218 /* We keep going until we either run out of names, or until
3219 we have a qualified name which is not a type. */
50af5481 3220 if (classification != TYPENAME && classification != NAME)
b2f83c08
TT
3221 break;
3222
3223 /* Accept up to this token. */
3224 checkpoint = next_to_examine;
48e32051
TT
3225
3226 /* Update the partial name we are constructing. */
e234dfaf 3227 if (context_type != NULL)
48e32051
TT
3228 {
3229 /* We don't want to put a leading "::" into the name. */
3230 obstack_grow_str (&name_obstack, "::");
3231 }
5fe3f3e4
TT
3232 obstack_grow (&name_obstack, next.value.sval.ptr,
3233 next.value.sval.length);
48e32051 3234
79f33898 3235 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
48e32051
TT
3236 yylval.sval.length = obstack_object_size (&name_obstack);
3237 current.value = yylval;
3238 current.token = classification;
3239
3240 last_was_coloncolon = 0;
4d29c0a8 3241
e234dfaf
TT
3242 if (classification == NAME)
3243 break;
3244
3245 context_type = yylval.tsym.type;
48e32051 3246 }
5fe3f3e4 3247 else if (next.token == COLONCOLON && !last_was_coloncolon)
48e32051
TT
3248 last_was_coloncolon = 1;
3249 else
3250 {
3251 /* We've reached the end of the name. */
48e32051
TT
3252 break;
3253 }
48e32051
TT
3254 }
3255
b2f83c08
TT
3256 /* If we have a replacement token, install it as the first token in
3257 the FIFO, and delete the other constituent tokens. */
3258 if (checkpoint > 0)
48e32051 3259 {
224c3ddb 3260 current.value.sval.ptr
9d30e1fd 3261 = (const char *) obstack_copy0 (&cpstate->expansion_obstack,
224c3ddb
SM
3262 current.value.sval.ptr,
3263 current.value.sval.length);
b2f83c08 3264
5fe3f3e4 3265 token_fifo[0] = current;
b2f83c08 3266 if (checkpoint > 1)
5fe3f3e4
TT
3267 token_fifo.erase (token_fifo.begin () + 1,
3268 token_fifo.begin () + checkpoint);
48e32051
TT
3269 }
3270
b2f83c08 3271 do_pop:
5fe3f3e4
TT
3272 current = token_fifo[0];
3273 token_fifo.erase (token_fifo.begin ());
48e32051 3274 yylval = current.value;
48e32051 3275 return current.token;
c906108c
SS
3276}
3277
65d12d83 3278int
410a0ff2 3279c_parse (struct parser_state *par_state)
65d12d83 3280{
410a0ff2 3281 /* Setting up the parser state. */
eae49211 3282 scoped_restore pstate_restore = make_scoped_restore (&pstate);
410a0ff2
SDJ
3283 gdb_assert (par_state != NULL);
3284 pstate = par_state;
3285
02e12e38
TT
3286 c_parse_state cstate;
3287 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3288
f6c2623e 3289 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
7c8adf68
TT
3290
3291 if (expression_context_block)
f6c2623e 3292 macro_scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
7c8adf68 3293 else
f6c2623e
TT
3294 macro_scope = default_macro_scope ();
3295 if (! macro_scope)
3296 macro_scope = user_macro_scope ();
3297
3298 scoped_restore restore_macro_scope
3299 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
7c8adf68 3300
156d9eab
TT
3301 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3302 parser_debug);
92981e24 3303
7c8adf68 3304 /* Initialize some state used by the lexer. */
59498c30 3305 last_was_structop = false;
65d12d83 3306 saw_name_at_eof = 0;
7c8adf68 3307
5fe3f3e4 3308 token_fifo.clear ();
48e32051 3309 popping = 0;
8268c778 3310 name_obstack.clear ();
48e32051 3311
9d30e1fd 3312 return yyparse ();
65d12d83
TT
3313}
3314
12c5175d
MK
3315#ifdef YYBISON
3316
9507860e
TT
3317/* This is called via the YYPRINT macro when parser debugging is
3318 enabled. It prints a token's value. */
3319
3320static void
3321c_print_token (FILE *file, int type, YYSTYPE value)
3322{
3323 switch (type)
3324 {
3325 case INT:
66be918f
PA
3326 parser_fprintf (file, "typed_val_int<%s, %s>",
3327 TYPE_SAFE_NAME (value.typed_val_int.type),
3328 pulongest (value.typed_val_int.val));
9507860e
TT
3329 break;
3330
3331 case CHAR:
3332 case STRING:
3333 {
224c3ddb 3334 char *copy = (char *) alloca (value.tsval.length + 1);
9507860e
TT
3335
3336 memcpy (copy, value.tsval.ptr, value.tsval.length);
3337 copy[value.tsval.length] = '\0';
3338
66be918f 3339 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
9507860e
TT
3340 }
3341 break;
3342
3343 case NSSTRING:
cfeadda5 3344 case DOLLAR_VARIABLE:
66be918f 3345 parser_fprintf (file, "sval<%s>", copy_name (value.sval));
9507860e
TT
3346 break;
3347
3348 case TYPENAME:
66be918f
PA
3349 parser_fprintf (file, "tsym<type=%s, name=%s>",
3350 TYPE_SAFE_NAME (value.tsym.type),
3351 copy_name (value.tsym.stoken));
9507860e
TT
3352 break;
3353
3354 case NAME:
3355 case UNKNOWN_CPP_NAME:
3356 case NAME_OR_INT:
3357 case BLOCKNAME:
66be918f
PA
3358 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
3359 copy_name (value.ssym.stoken),
3360 (value.ssym.sym.symbol == NULL
3361 ? "(null)" : SYMBOL_PRINT_NAME (value.ssym.sym.symbol)),
3362 value.ssym.is_a_field_of_this);
9507860e
TT
3363 break;
3364
3365 case FILENAME:
66be918f 3366 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
9507860e
TT
3367 break;
3368 }
3369}
7c8adf68 3370
12c5175d
MK
3371#endif
3372
69d340c6 3373static void
a121b7c1 3374yyerror (const char *msg)
c906108c 3375{
665132f9
MS
3376 if (prev_lexptr)
3377 lexptr = prev_lexptr;
3378
69d340c6 3379 error (_("A %s in expression, near `%s'."), msg, lexptr);
c906108c 3380}