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