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