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