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