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