gdb: add type::is_declared_class / type::set_is_declared_class
[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
d182f279
TT
1128 pstate->push_new<var_value_operation> (sym.symbol,
1129 sym.block);
1130 }
c906108c
SS
1131 ;
1132
48e32051 1133qualified_name: TYPENAME COLONCOLON name
c906108c 1134 {
48e32051 1135 struct type *type = $1.type;
f168693b 1136 type = check_typedef (type);
3d567982 1137 if (!type_aggregate_p (type))
001083c6 1138 error (_("`%s' is not defined as an aggregate type."),
7fc75ca7 1139 TYPE_SAFE_NAME (type));
c906108c 1140
d182f279
TT
1141 pstate->push_new<scope_operation> (type,
1142 copy_name ($3));
c906108c 1143 }
48e32051 1144 | TYPENAME COLONCOLON '~' name
c906108c 1145 {
48e32051 1146 struct type *type = $1.type;
d7561cbb 1147
f168693b 1148 type = check_typedef (type);
3d567982 1149 if (!type_aggregate_p (type))
001083c6 1150 error (_("`%s' is not defined as an aggregate type."),
7fc75ca7 1151 TYPE_SAFE_NAME (type));
d182f279
TT
1152 std::string name = "~" + std::string ($4.ptr,
1153 $4.length);
c906108c
SS
1154
1155 /* Check for valid destructor name. */
d182f279
TT
1156 destructor_name_p (name.c_str (), $1.type);
1157 pstate->push_new<scope_operation> (type,
1158 std::move (name));
c906108c 1159 }
48e32051
TT
1160 | TYPENAME COLONCOLON name COLONCOLON name
1161 {
61f4b350 1162 std::string copy = copy_name ($3);
48e32051
TT
1163 error (_("No type \"%s\" within class "
1164 "or namespace \"%s\"."),
61f4b350 1165 copy.c_str (), TYPE_SAFE_NAME ($1.type));
48e32051 1166 }
c906108c
SS
1167 ;
1168
1169variable: qualified_name
48e32051 1170 | COLONCOLON name_not_typename
c906108c 1171 {
61f4b350 1172 std::string name = copy_name ($2.stoken);
1b30f421 1173 struct block_symbol sym
61f4b350
TT
1174 = lookup_symbol (name.c_str (),
1175 (const struct block *) NULL,
1b30f421 1176 VAR_DOMAIN, NULL);
d182f279 1177 pstate->push_symbol (name.c_str (), sym);
c906108c
SS
1178 }
1179 ;
1180
1181variable: name_not_typename
d12307c1 1182 { struct block_symbol sym = $1.sym;
c906108c 1183
d12307c1 1184 if (sym.symbol)
c906108c 1185 {
d12307c1 1186 if (symbol_read_needs_frame (sym.symbol))
699bd4cf 1187 pstate->block_tracker->update (sym);
c906108c 1188
ca31ab1d
PA
1189 /* If we found a function, see if it's
1190 an ifunc resolver that has the same
1191 address as the ifunc symbol itself.
1192 If so, prefer the ifunc symbol. */
1193
1194 bound_minimal_symbol resolver
1195 = find_gnu_ifunc (sym.symbol);
1196 if (resolver.minsym != NULL)
d182f279 1197 pstate->push_new<var_msym_value_operation>
9c79936b 1198 (resolver);
ca31ab1d 1199 else
d182f279
TT
1200 pstate->push_new<var_value_operation>
1201 (sym.symbol, sym.block);
c906108c
SS
1202 }
1203 else if ($1.is_a_field_of_this)
1204 {
1205 /* C++: it hangs off of `this'. Must
dda83cd7 1206 not inadvertently convert from a method call
c906108c 1207 to data ref. */
699bd4cf 1208 pstate->block_tracker->update (sym);
d182f279
TT
1209 operation_up thisop
1210 = make_operation<op_this_operation> ();
1211 pstate->push_new<structop_ptr_operation>
1212 (std::move (thisop), copy_name ($1.stoken));
c906108c
SS
1213 }
1214 else
1215 {
61f4b350 1216 std::string arg = copy_name ($1.stoken);
c906108c 1217
bf223d3e 1218 bound_minimal_symbol msymbol
61f4b350 1219 = lookup_bound_minimal_symbol (arg.c_str ());
bf223d3e
PA
1220 if (msymbol.minsym == NULL)
1221 {
1222 if (!have_full_symbols () && !have_partial_symbols ())
1223 error (_("No symbol table is loaded. Use the \"file\" command."));
1224 else
1225 error (_("No symbol \"%s\" in current context."),
61f4b350 1226 arg.c_str ());
bf223d3e
PA
1227 }
1228
1229 /* This minsym might be an alias for
1230 another function. See if we can find
1231 the debug symbol for the target, and
1232 if so, use it instead, since it has
1233 return type / prototype info. This
1234 is important for example for "p
1235 *__errno_location()". */
1236 symbol *alias_target
f50776aa
PA
1237 = ((msymbol.minsym->type != mst_text_gnu_ifunc
1238 && msymbol.minsym->type != mst_data_gnu_ifunc)
a376e11d
PA
1239 ? find_function_alias_target (msymbol)
1240 : NULL);
bf223d3e 1241 if (alias_target != NULL)
d182f279
TT
1242 pstate->push_new<var_value_operation>
1243 (alias_target, SYMBOL_BLOCK_VALUE (alias_target));
c906108c 1244 else
d182f279 1245 pstate->push_new<var_msym_value_operation>
9c79936b 1246 (msymbol);
c906108c
SS
1247 }
1248 }
1249 ;
1250
47663de5
MS
1251const_or_volatile: const_or_volatile_noopt
1252 |
c906108c 1253 ;
47663de5 1254
3293bbaf
TT
1255single_qualifier:
1256 CONST_KEYWORD
1257 { cpstate->type_stack.insert (tp_const); }
1258 | VOLATILE_KEYWORD
1259 { cpstate->type_stack.insert (tp_volatile); }
1260 | ATOMIC
1261 { cpstate->type_stack.insert (tp_atomic); }
1262 | RESTRICT
1263 { cpstate->type_stack.insert (tp_restrict); }
1264 | '@' NAME
1265 {
1266 cpstate->type_stack.insert (pstate,
1267 copy_name ($2.stoken).c_str ());
1268 }
56e2d25a 1269 ;
47663de5 1270
3293bbaf
TT
1271qualifier_seq_noopt:
1272 single_qualifier
1273 | qualifier_seq single_qualifier
56e2d25a 1274 ;
47663de5 1275
3293bbaf
TT
1276qualifier_seq:
1277 qualifier_seq_noopt
47663de5 1278 |
56e2d25a 1279 ;
47663de5 1280
95c391b6
TT
1281ptr_operator:
1282 ptr_operator '*'
dac43e32 1283 { cpstate->type_stack.insert (tp_pointer); }
3293bbaf 1284 qualifier_seq
4d29c0a8 1285 | '*'
dac43e32 1286 { cpstate->type_stack.insert (tp_pointer); }
3293bbaf 1287 qualifier_seq
c906108c 1288 | '&'
dac43e32 1289 { cpstate->type_stack.insert (tp_reference); }
95c391b6 1290 | '&' ptr_operator
dac43e32 1291 { cpstate->type_stack.insert (tp_reference); }
53cc15f5 1292 | ANDAND
dac43e32 1293 { cpstate->type_stack.insert (tp_rvalue_reference); }
53cc15f5 1294 | ANDAND ptr_operator
dac43e32 1295 { cpstate->type_stack.insert (tp_rvalue_reference); }
95c391b6
TT
1296 ;
1297
fcde5961
TT
1298ptr_operator_ts: ptr_operator
1299 {
dac43e32 1300 $$ = cpstate->type_stack.create ();
02e12e38 1301 cpstate->type_stacks.emplace_back ($$);
fcde5961
TT
1302 }
1303 ;
1304
1305abs_decl: ptr_operator_ts direct_abs_decl
dac43e32 1306 { $$ = $2->append ($1); }
4d29c0a8 1307 | ptr_operator_ts
c906108c
SS
1308 | direct_abs_decl
1309 ;
1310
1311direct_abs_decl: '(' abs_decl ')'
fcde5961 1312 { $$ = $2; }
c906108c
SS
1313 | direct_abs_decl array_mod
1314 {
dac43e32
TT
1315 cpstate->type_stack.push ($1);
1316 cpstate->type_stack.push ($2);
1317 cpstate->type_stack.push (tp_array);
1318 $$ = cpstate->type_stack.create ();
ab759ca8 1319 cpstate->type_stacks.emplace_back ($$);
c906108c
SS
1320 }
1321 | array_mod
1322 {
dac43e32
TT
1323 cpstate->type_stack.push ($1);
1324 cpstate->type_stack.push (tp_array);
1325 $$ = cpstate->type_stack.create ();
ab759ca8 1326 cpstate->type_stacks.emplace_back ($$);
c906108c
SS
1327 }
1328
1329 | direct_abs_decl func_mod
fcde5961 1330 {
dac43e32
TT
1331 cpstate->type_stack.push ($1);
1332 cpstate->type_stack.push ($2);
1333 $$ = cpstate->type_stack.create ();
ab759ca8 1334 cpstate->type_stacks.emplace_back ($$);
fcde5961 1335 }
c906108c 1336 | func_mod
fcde5961 1337 {
dac43e32
TT
1338 cpstate->type_stack.push ($1);
1339 $$ = cpstate->type_stack.create ();
ab759ca8 1340 cpstate->type_stacks.emplace_back ($$);
fcde5961 1341 }
c906108c
SS
1342 ;
1343
1344array_mod: '[' ']'
1345 { $$ = -1; }
f2e8016f
TT
1346 | OBJC_LBRAC ']'
1347 { $$ = -1; }
c906108c
SS
1348 | '[' INT ']'
1349 { $$ = $2.val; }
f2e8016f
TT
1350 | OBJC_LBRAC INT ']'
1351 { $$ = $2.val; }
c906108c
SS
1352 ;
1353
1354func_mod: '(' ')'
02e12e38
TT
1355 {
1356 $$ = new std::vector<struct type *>;
1357 cpstate->type_lists.emplace_back ($$);
1358 }
a6fb9c08 1359 | '(' parameter_typelist ')'
71918a86 1360 { $$ = $2; }
c906108c
SS
1361 ;
1362
a22229c4 1363/* We used to try to recognize pointer to member types here, but
c906108c
SS
1364 that didn't work (shift/reduce conflicts meant that these rules never
1365 got executed). The problem is that
1366 int (foo::bar::baz::bizzle)
1367 is a function type but
1368 int (foo::bar::baz::bizzle::*)
1369 is a pointer to member type. Stroustrup loses again! */
1370
1371type : ptype
c906108c
SS
1372 ;
1373
3638a098
TT
1374/* A helper production that recognizes scalar types that can validly
1375 be used with _Complex. */
b6c95c0c 1376
3638a098
TT
1377scalar_type:
1378 INT_KEYWORD
73923d7e 1379 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1380 "int"); }
c906108c 1381 | LONG
73923d7e 1382 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1383 "long"); }
c906108c 1384 | SHORT
73923d7e 1385 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1386 "short"); }
c906108c 1387 | LONG INT_KEYWORD
73923d7e 1388 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1389 "long"); }
b2c4da81 1390 | LONG SIGNED_KEYWORD INT_KEYWORD
73923d7e 1391 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1392 "long"); }
b2c4da81 1393 | LONG SIGNED_KEYWORD
73923d7e 1394 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1395 "long"); }
b2c4da81 1396 | SIGNED_KEYWORD LONG INT_KEYWORD
73923d7e 1397 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1398 "long"); }
c906108c 1399 | UNSIGNED LONG INT_KEYWORD
73923d7e 1400 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1401 "long"); }
b2c4da81 1402 | LONG UNSIGNED INT_KEYWORD
73923d7e 1403 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1404 "long"); }
b2c4da81 1405 | LONG UNSIGNED
73923d7e 1406 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1407 "long"); }
c906108c 1408 | LONG LONG
73923d7e 1409 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1410 "long long"); }
c906108c 1411 | LONG LONG INT_KEYWORD
73923d7e 1412 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1413 "long long"); }
b2c4da81 1414 | LONG LONG SIGNED_KEYWORD INT_KEYWORD
73923d7e 1415 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1416 "long long"); }
b2c4da81 1417 | LONG LONG SIGNED_KEYWORD
73923d7e 1418 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1419 "long long"); }
b2c4da81 1420 | SIGNED_KEYWORD LONG LONG
73923d7e 1421 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1422 "long long"); }
55baeb84 1423 | SIGNED_KEYWORD LONG LONG INT_KEYWORD
73923d7e 1424 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1425 "long long"); }
c906108c 1426 | UNSIGNED LONG LONG
73923d7e 1427 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1428 "long long"); }
c906108c 1429 | UNSIGNED LONG LONG INT_KEYWORD
73923d7e 1430 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1431 "long long"); }
b2c4da81 1432 | LONG LONG UNSIGNED
73923d7e 1433 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1434 "long long"); }
b2c4da81 1435 | LONG LONG UNSIGNED INT_KEYWORD
73923d7e 1436 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1437 "long long"); }
c906108c 1438 | SHORT INT_KEYWORD
73923d7e 1439 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1440 "short"); }
b2c4da81 1441 | SHORT SIGNED_KEYWORD INT_KEYWORD
73923d7e 1442 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1443 "short"); }
b2c4da81 1444 | SHORT SIGNED_KEYWORD
73923d7e 1445 { $$ = lookup_signed_typename (pstate->language (),
f4b8a18d 1446 "short"); }
c906108c 1447 | UNSIGNED SHORT INT_KEYWORD
73923d7e 1448 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1449 "short"); }
4d29c0a8 1450 | SHORT UNSIGNED
73923d7e 1451 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1452 "short"); }
b2c4da81 1453 | SHORT UNSIGNED INT_KEYWORD
73923d7e 1454 { $$ = lookup_unsigned_typename (pstate->language (),
f4b8a18d 1455 "short"); }
c906108c 1456 | DOUBLE_KEYWORD
73923d7e 1457 { $$ = lookup_typename (pstate->language (),
410a0ff2 1458 "double",
582942f4 1459 NULL,
f4b8a18d 1460 0); }
3638a098
TT
1461 | FLOAT_KEYWORD
1462 { $$ = lookup_typename (pstate->language (),
1463 "float",
1464 NULL,
1465 0); }
c906108c 1466 | LONG DOUBLE_KEYWORD
73923d7e 1467 { $$ = lookup_typename (pstate->language (),
f4b8a18d 1468 "long double",
582942f4 1469 NULL,
410a0ff2 1470 0); }
3638a098
TT
1471 | UNSIGNED type_name
1472 { $$ = lookup_unsigned_typename (pstate->language (),
7d93a1e0 1473 $2.type->name ()); }
3638a098
TT
1474 | UNSIGNED
1475 { $$ = lookup_unsigned_typename (pstate->language (),
1476 "int"); }
1477 | SIGNED_KEYWORD type_name
1478 { $$ = lookup_signed_typename (pstate->language (),
7d93a1e0 1479 $2.type->name ()); }
3638a098
TT
1480 | SIGNED_KEYWORD
1481 { $$ = lookup_signed_typename (pstate->language (),
1482 "int"); }
1483 ;
1484
1485/* Implements (approximately): (type-qualifier)* type-specifier.
1486
1487 When type-specifier is only ever a single word, like 'float' then these
1488 arrive as pre-built TYPENAME tokens thanks to the classify_name
1489 function. However, when a type-specifier can contain multiple words,
1490 for example 'double' can appear as just 'double' or 'long double', and
1491 similarly 'long' can appear as just 'long' or in 'long double', then
1492 these type-specifiers are parsed into their own tokens in the function
1493 lex_one_token and the ident_tokens array. These separate tokens are all
1494 recognised here. */
1495typebase
1496 : TYPENAME
1497 { $$ = $1.type; }
1498 | scalar_type
1499 { $$ = $1; }
1500 | COMPLEX scalar_type
1501 {
1502 $$ = init_complex_type (nullptr, $2);
1503 }
c906108c 1504 | STRUCT name
1e58a4a4 1505 { $$
61f4b350 1506 = lookup_struct (copy_name ($2).c_str (),
1e58a4a4
TT
1507 pstate->expression_context_block);
1508 }
2f68a895
TT
1509 | STRUCT COMPLETE
1510 {
2a612529
TT
1511 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1512 "", 0);
2f68a895
TT
1513 $$ = NULL;
1514 }
1515 | STRUCT name COMPLETE
1516 {
2a612529
TT
1517 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1518 $2.ptr, $2.length);
2f68a895
TT
1519 $$ = NULL;
1520 }
c906108c 1521 | CLASS name
1e58a4a4 1522 { $$ = lookup_struct
61f4b350
TT
1523 (copy_name ($2).c_str (),
1524 pstate->expression_context_block);
1e58a4a4 1525 }
2f68a895
TT
1526 | CLASS COMPLETE
1527 {
2a612529
TT
1528 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1529 "", 0);
2f68a895
TT
1530 $$ = NULL;
1531 }
1532 | CLASS name COMPLETE
1533 {
2a612529
TT
1534 pstate->mark_completion_tag (TYPE_CODE_STRUCT,
1535 $2.ptr, $2.length);
2f68a895
TT
1536 $$ = NULL;
1537 }
c906108c 1538 | UNION name
1e58a4a4 1539 { $$
61f4b350 1540 = lookup_union (copy_name ($2).c_str (),
1e58a4a4
TT
1541 pstate->expression_context_block);
1542 }
2f68a895
TT
1543 | UNION COMPLETE
1544 {
2a612529
TT
1545 pstate->mark_completion_tag (TYPE_CODE_UNION,
1546 "", 0);
2f68a895
TT
1547 $$ = NULL;
1548 }
1549 | UNION name COMPLETE
1550 {
2a612529
TT
1551 pstate->mark_completion_tag (TYPE_CODE_UNION,
1552 $2.ptr, $2.length);
2f68a895
TT
1553 $$ = NULL;
1554 }
c906108c 1555 | ENUM name
61f4b350 1556 { $$ = lookup_enum (copy_name ($2).c_str (),
1e58a4a4
TT
1557 pstate->expression_context_block);
1558 }
2f68a895
TT
1559 | ENUM COMPLETE
1560 {
2a612529 1561 pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
2f68a895
TT
1562 $$ = NULL;
1563 }
1564 | ENUM name COMPLETE
1565 {
2a612529
TT
1566 pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
1567 $2.length);
2f68a895
TT
1568 $$ = NULL;
1569 }
dda83cd7
SM
1570 /* It appears that this rule for templates is never
1571 reduced; template recognition happens by lookahead
1572 in the token processing code in yylex. */
c906108c 1573 | TEMPLATE name '<' type '>'
1e58a4a4 1574 { $$ = lookup_template_type
61f4b350 1575 (copy_name($2).c_str (), $4,
1e58a4a4 1576 pstate->expression_context_block);
c906108c 1577 }
3293bbaf 1578 | qualifier_seq_noopt typebase
dac43e32 1579 { $$ = cpstate->type_stack.follow_types ($2); }
3293bbaf 1580 | typebase qualifier_seq_noopt
dac43e32 1581 { $$ = cpstate->type_stack.follow_types ($1); }
c906108c
SS
1582 ;
1583
fe978cb0 1584type_name: TYPENAME
c906108c
SS
1585 | INT_KEYWORD
1586 {
1587 $$.stoken.ptr = "int";
1588 $$.stoken.length = 3;
73923d7e 1589 $$.type = lookup_signed_typename (pstate->language (),
f4b8a18d 1590 "int");
c906108c
SS
1591 }
1592 | LONG
1593 {
1594 $$.stoken.ptr = "long";
1595 $$.stoken.length = 4;
73923d7e 1596 $$.type = lookup_signed_typename (pstate->language (),
f4b8a18d 1597 "long");
c906108c
SS
1598 }
1599 | SHORT
1600 {
1601 $$.stoken.ptr = "short";
1602 $$.stoken.length = 5;
73923d7e 1603 $$.type = lookup_signed_typename (pstate->language (),
f4b8a18d 1604 "short");
c906108c
SS
1605 }
1606 ;
1607
a6fb9c08
TT
1608parameter_typelist:
1609 nonempty_typelist
e314d629 1610 { check_parameter_typelist ($1); }
a6fb9c08
TT
1611 | nonempty_typelist ',' DOTDOTDOT
1612 {
02e12e38 1613 $1->push_back (NULL);
e314d629 1614 check_parameter_typelist ($1);
a6fb9c08
TT
1615 $$ = $1;
1616 }
1617 ;
1618
c906108c
SS
1619nonempty_typelist
1620 : type
71918a86 1621 {
02e12e38
TT
1622 std::vector<struct type *> *typelist
1623 = new std::vector<struct type *>;
1624 cpstate->type_lists.emplace_back (typelist);
1625
1626 typelist->push_back ($1);
71918a86 1627 $$ = typelist;
c906108c
SS
1628 }
1629 | nonempty_typelist ',' type
71918a86 1630 {
02e12e38 1631 $1->push_back ($3);
71918a86 1632 $$ = $1;
c906108c
SS
1633 }
1634 ;
1635
47663de5 1636ptype : typebase
95c391b6 1637 | ptype abs_decl
fcde5961 1638 {
dac43e32
TT
1639 cpstate->type_stack.push ($2);
1640 $$ = cpstate->type_stack.follow_types ($1);
fcde5961 1641 }
47663de5
MS
1642 ;
1643
95c391b6 1644conversion_type_id: typebase conversion_declarator
dac43e32 1645 { $$ = cpstate->type_stack.follow_types ($1); }
95c391b6
TT
1646 ;
1647
1648conversion_declarator: /* Nothing. */
1649 | ptr_operator conversion_declarator
1650 ;
1651
47663de5
MS
1652const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
1653 | VOLATILE_KEYWORD CONST_KEYWORD
1654 ;
1655
4d29c0a8 1656const_or_volatile_noopt: const_and_volatile
dac43e32
TT
1657 { cpstate->type_stack.insert (tp_const);
1658 cpstate->type_stack.insert (tp_volatile);
47663de5
MS
1659 }
1660 | CONST_KEYWORD
dac43e32 1661 { cpstate->type_stack.insert (tp_const); }
47663de5 1662 | VOLATILE_KEYWORD
dac43e32 1663 { cpstate->type_stack.insert (tp_volatile); }
47663de5
MS
1664 ;
1665
fe978cb0 1666oper: OPERATOR NEW
66c53f2b
KS
1667 { $$ = operator_stoken (" new"); }
1668 | OPERATOR DELETE
4cd18215 1669 { $$ = operator_stoken (" delete"); }
66c53f2b
KS
1670 | OPERATOR NEW '[' ']'
1671 { $$ = operator_stoken (" new[]"); }
1672 | OPERATOR DELETE '[' ']'
4cd18215 1673 { $$ = operator_stoken (" delete[]"); }
f2e8016f
TT
1674 | OPERATOR NEW OBJC_LBRAC ']'
1675 { $$ = operator_stoken (" new[]"); }
1676 | OPERATOR DELETE OBJC_LBRAC ']'
1677 { $$ = operator_stoken (" delete[]"); }
66c53f2b
KS
1678 | OPERATOR '+'
1679 { $$ = operator_stoken ("+"); }
1680 | OPERATOR '-'
1681 { $$ = operator_stoken ("-"); }
1682 | OPERATOR '*'
1683 { $$ = operator_stoken ("*"); }
1684 | OPERATOR '/'
1685 { $$ = operator_stoken ("/"); }
1686 | OPERATOR '%'
1687 { $$ = operator_stoken ("%"); }
1688 | OPERATOR '^'
1689 { $$ = operator_stoken ("^"); }
1690 | OPERATOR '&'
1691 { $$ = operator_stoken ("&"); }
1692 | OPERATOR '|'
1693 { $$ = operator_stoken ("|"); }
1694 | OPERATOR '~'
1695 { $$ = operator_stoken ("~"); }
1696 | OPERATOR '!'
1697 { $$ = operator_stoken ("!"); }
1698 | OPERATOR '='
1699 { $$ = operator_stoken ("="); }
1700 | OPERATOR '<'
1701 { $$ = operator_stoken ("<"); }
1702 | OPERATOR '>'
1703 { $$ = operator_stoken (">"); }
1704 | OPERATOR ASSIGN_MODIFY
c8ba13ad 1705 { const char *op = " unknown";
66c53f2b
KS
1706 switch ($2)
1707 {
1708 case BINOP_RSH:
1709 op = ">>=";
1710 break;
1711 case BINOP_LSH:
1712 op = "<<=";
1713 break;
1714 case BINOP_ADD:
1715 op = "+=";
1716 break;
1717 case BINOP_SUB:
1718 op = "-=";
1719 break;
1720 case BINOP_MUL:
1721 op = "*=";
1722 break;
1723 case BINOP_DIV:
1724 op = "/=";
1725 break;
1726 case BINOP_REM:
1727 op = "%=";
1728 break;
1729 case BINOP_BITWISE_IOR:
1730 op = "|=";
1731 break;
1732 case BINOP_BITWISE_AND:
1733 op = "&=";
1734 break;
1735 case BINOP_BITWISE_XOR:
1736 op = "^=";
1737 break;
1738 default:
1739 break;
1740 }
1741
1742 $$ = operator_stoken (op);
1743 }
1744 | OPERATOR LSH
1745 { $$ = operator_stoken ("<<"); }
1746 | OPERATOR RSH
1747 { $$ = operator_stoken (">>"); }
1748 | OPERATOR EQUAL
1749 { $$ = operator_stoken ("=="); }
1750 | OPERATOR NOTEQUAL
1751 { $$ = operator_stoken ("!="); }
1752 | OPERATOR LEQ
1753 { $$ = operator_stoken ("<="); }
1754 | OPERATOR GEQ
1755 { $$ = operator_stoken (">="); }
1756 | OPERATOR ANDAND
1757 { $$ = operator_stoken ("&&"); }
1758 | OPERATOR OROR
1759 { $$ = operator_stoken ("||"); }
1760 | OPERATOR INCREMENT
1761 { $$ = operator_stoken ("++"); }
1762 | OPERATOR DECREMENT
1763 { $$ = operator_stoken ("--"); }
1764 | OPERATOR ','
1765 { $$ = operator_stoken (","); }
1766 | OPERATOR ARROW_STAR
1767 { $$ = operator_stoken ("->*"); }
1768 | OPERATOR ARROW
1769 { $$ = operator_stoken ("->"); }
1770 | OPERATOR '(' ')'
1771 { $$ = operator_stoken ("()"); }
1772 | OPERATOR '[' ']'
1773 { $$ = operator_stoken ("[]"); }
f2e8016f
TT
1774 | OPERATOR OBJC_LBRAC ']'
1775 { $$ = operator_stoken ("[]"); }
95c391b6 1776 | OPERATOR conversion_type_id
d7e74731 1777 { string_file buf;
66c53f2b 1778
d7e74731 1779 c_print_type ($2, NULL, &buf, -1, 0,
79d43c61 1780 &type_print_raw_options);
596dc4ad 1781 std::string name = std::move (buf.string ());
c8ba13ad
KS
1782
1783 /* This also needs canonicalization. */
596dc4ad
TT
1784 gdb::unique_xmalloc_ptr<char> canon
1785 = cp_canonicalize_string (name.c_str ());
1786 if (canon != nullptr)
1787 name = canon.get ();
1788 $$ = operator_stoken ((" " + name).c_str ());
66c53f2b
KS
1789 }
1790 ;
1791
6f0ffe50
AB
1792/* This rule exists in order to allow some tokens that would not normally
1793 match the 'name' rule to appear as fields within a struct. The example
1794 that initially motivated this was the RISC-V target which models the
1795 floating point registers as a union with fields called 'float' and
3638a098 1796 'double'. */
0f5d3f63
AB
1797field_name
1798 : name
6f0ffe50 1799 | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
3638a098 1800 | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
6f0ffe50
AB
1801 | INT_KEYWORD { $$ = typename_stoken ("int"); }
1802 | LONG { $$ = typename_stoken ("long"); }
1803 | SHORT { $$ = typename_stoken ("short"); }
1804 | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
1805 | UNSIGNED { $$ = typename_stoken ("unsigned"); }
0f5d3f63 1806 ;
66c53f2b 1807
c906108c
SS
1808name : NAME { $$ = $1.stoken; }
1809 | BLOCKNAME { $$ = $1.stoken; }
1810 | TYPENAME { $$ = $1.stoken; }
1811 | NAME_OR_INT { $$ = $1.stoken; }
7322dca9 1812 | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
fe978cb0 1813 | oper { $$ = $1; }
c906108c
SS
1814 ;
1815
1816name_not_typename : NAME
1817 | BLOCKNAME
1818/* These would be useful if name_not_typename was useful, but it is just
1819 a fake for "variable", so these cause reduce/reduce conflicts because
1820 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
1821 =exp) or just an exp. If name_not_typename was ever used in an lvalue
1822 context where only a name could occur, this might be useful.
1823 | NAME_OR_INT
1824 */
fe978cb0 1825 | oper
6e31430b 1826 {
1993b719
TT
1827 struct field_of_this_result is_a_field_of_this;
1828
6e31430b 1829 $$.stoken = $1;
1e58a4a4
TT
1830 $$.sym
1831 = lookup_symbol ($1.ptr,
1832 pstate->expression_context_block,
1833 VAR_DOMAIN,
1834 &is_a_field_of_this);
1993b719
TT
1835 $$.is_a_field_of_this
1836 = is_a_field_of_this.type != NULL;
6e31430b 1837 }
7322dca9 1838 | UNKNOWN_CPP_NAME
c906108c
SS
1839 ;
1840
1841%%
1842
66c53f2b 1843/* Returns a stoken of the operator name given by OP (which does not
4d29c0a8
DE
1844 include the string "operator"). */
1845
66c53f2b
KS
1846static struct stoken
1847operator_stoken (const char *op)
1848{
66c53f2b 1849 struct stoken st = { NULL, 0 };
d7561cbb
KS
1850 char *buf;
1851
8090b426 1852 st.length = CP_OPERATOR_LEN + strlen (op);
224c3ddb 1853 buf = (char *) malloc (st.length + 1);
8090b426 1854 strcpy (buf, CP_OPERATOR_STR);
d7561cbb
KS
1855 strcat (buf, op);
1856 st.ptr = buf;
66c53f2b
KS
1857
1858 /* The toplevel (c_parse) will free the memory allocated here. */
c65bac38 1859 cpstate->strings.emplace_back (buf);
66c53f2b
KS
1860 return st;
1861};
1862
6f0ffe50
AB
1863/* Returns a stoken of the type named TYPE. */
1864
1865static struct stoken
1866typename_stoken (const char *type)
1867{
1868 struct stoken st = { type, 0 };
1869 st.length = strlen (type);
1870 return st;
1871};
1872
3d567982
TT
1873/* Return true if the type is aggregate-like. */
1874
1875static int
1876type_aggregate_p (struct type *type)
1877{
78134374
SM
1878 return (type->code () == TYPE_CODE_STRUCT
1879 || type->code () == TYPE_CODE_UNION
1880 || type->code () == TYPE_CODE_NAMESPACE
1881 || (type->code () == TYPE_CODE_ENUM
3d567982
TT
1882 && TYPE_DECLARED_CLASS (type)));
1883}
1884
e314d629
TT
1885/* Validate a parameter typelist. */
1886
1887static void
02e12e38 1888check_parameter_typelist (std::vector<struct type *> *params)
e314d629
TT
1889{
1890 struct type *type;
1891 int ix;
1892
02e12e38 1893 for (ix = 0; ix < params->size (); ++ix)
e314d629 1894 {
02e12e38 1895 type = (*params)[ix];
78134374 1896 if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
e314d629
TT
1897 {
1898 if (ix == 0)
1899 {
02e12e38 1900 if (params->size () == 1)
e314d629
TT
1901 {
1902 /* Ok. */
1903 break;
1904 }
e314d629
TT
1905 error (_("parameter types following 'void'"));
1906 }
1907 else
02e12e38 1908 error (_("'void' invalid as parameter type"));
e314d629
TT
1909 }
1910 }
1911}
1912
c906108c
SS
1913/* Take care of parsing a number (anything that starts with a digit).
1914 Set yylval and return the token type; update lexptr.
1915 LEN is the number of characters in it. */
1916
1917/*** Needs some error checking for the float case ***/
1918
1919static int
410a0ff2
SDJ
1920parse_number (struct parser_state *par_state,
1921 const char *buf, int len, int parsed_float, YYSTYPE *putithere)
c906108c 1922{
20562150
TT
1923 ULONGEST n = 0;
1924 ULONGEST prevn = 0;
c906108c
SS
1925 ULONGEST un;
1926
710122da
DC
1927 int i = 0;
1928 int c;
1929 int base = input_radix;
c906108c
SS
1930 int unsigned_p = 0;
1931
1932 /* Number of "L" suffixes encountered. */
1933 int long_p = 0;
1934
fa649bb7
TT
1935 /* Imaginary number. */
1936 bool imaginary_p = false;
1937
1938 /* We have found a "L" or "U" (or "i") suffix. */
c906108c
SS
1939 int found_suffix = 0;
1940
1941 ULONGEST high_bit;
1942 struct type *signed_type;
1943 struct type *unsigned_type;
d7561cbb
KS
1944 char *p;
1945
224c3ddb 1946 p = (char *) alloca (len);
d7561cbb 1947 memcpy (p, buf, len);
c906108c
SS
1948
1949 if (parsed_float)
1950 {
fa649bb7
TT
1951 if (len >= 1 && p[len - 1] == 'i')
1952 {
1953 imaginary_p = true;
1954 --len;
1955 }
1956
edd079d9 1957 /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
fe9441f6 1958 if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
27bc4d80 1959 {
edd079d9 1960 putithere->typed_val_float.type
410a0ff2 1961 = parse_type (par_state)->builtin_decfloat;
edd079d9 1962 len -= 2;
27bc4d80 1963 }
edd079d9 1964 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
27bc4d80 1965 {
edd079d9 1966 putithere->typed_val_float.type
410a0ff2 1967 = parse_type (par_state)->builtin_decdouble;
edd079d9 1968 len -= 2;
27bc4d80 1969 }
edd079d9 1970 else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
27bc4d80 1971 {
edd079d9 1972 putithere->typed_val_float.type
410a0ff2 1973 = parse_type (par_state)->builtin_declong;
edd079d9
UW
1974 len -= 2;
1975 }
1976 /* Handle suffixes: 'f' for float, 'l' for long double. */
b1b60145 1977 else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
edd079d9
UW
1978 {
1979 putithere->typed_val_float.type
1980 = parse_type (par_state)->builtin_float;
1981 len -= 1;
1982 }
b1b60145 1983 else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
edd079d9
UW
1984 {
1985 putithere->typed_val_float.type
1986 = parse_type (par_state)->builtin_long_double;
1987 len -= 1;
1988 }
1989 /* Default type for floating-point literals is double. */
1990 else
1991 {
1992 putithere->typed_val_float.type
1993 = parse_type (par_state)->builtin_double;
27bc4d80
TJB
1994 }
1995
edd079d9
UW
1996 if (!parse_float (p, len,
1997 putithere->typed_val_float.type,
1998 putithere->typed_val_float.val))
dda83cd7 1999 return ERROR;
fa649bb7
TT
2000
2001 if (imaginary_p)
2002 putithere->typed_val_float.type
2003 = init_complex_type (nullptr, putithere->typed_val_float.type);
2004
2005 return imaginary_p ? COMPLEX_FLOAT : FLOAT;
c906108c
SS
2006 }
2007
2008 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
ebf13736 2009 if (p[0] == '0' && len > 1)
c906108c
SS
2010 switch (p[1])
2011 {
2012 case 'x':
2013 case 'X':
2014 if (len >= 3)
2015 {
2016 p += 2;
2017 base = 16;
2018 len -= 2;
2019 }
2020 break;
2021
b5cfddf5
JK
2022 case 'b':
2023 case 'B':
2024 if (len >= 3)
2025 {
2026 p += 2;
2027 base = 2;
2028 len -= 2;
2029 }
2030 break;
2031
c906108c
SS
2032 case 't':
2033 case 'T':
2034 case 'd':
2035 case 'D':
2036 if (len >= 3)
2037 {
2038 p += 2;
2039 base = 10;
2040 len -= 2;
2041 }
2042 break;
2043
2044 default:
2045 base = 8;
2046 break;
2047 }
2048
2049 while (len-- > 0)
2050 {
2051 c = *p++;
2052 if (c >= 'A' && c <= 'Z')
2053 c += 'a' - 'A';
fa649bb7 2054 if (c != 'l' && c != 'u' && c != 'i')
c906108c
SS
2055 n *= base;
2056 if (c >= '0' && c <= '9')
2057 {
2058 if (found_suffix)
2059 return ERROR;
2060 n += i = c - '0';
2061 }
2062 else
2063 {
2064 if (base > 10 && c >= 'a' && c <= 'f')
2065 {
2066 if (found_suffix)
2067 return ERROR;
2068 n += i = c - 'a' + 10;
2069 }
2070 else if (c == 'l')
2071 {
2072 ++long_p;
2073 found_suffix = 1;
2074 }
2075 else if (c == 'u')
2076 {
2077 unsigned_p = 1;
2078 found_suffix = 1;
2079 }
fa649bb7
TT
2080 else if (c == 'i')
2081 {
2082 imaginary_p = true;
2083 found_suffix = 1;
2084 }
c906108c
SS
2085 else
2086 return ERROR; /* Char not a digit */
2087 }
2088 if (i >= base)
2089 return ERROR; /* Invalid digit in this base */
2090
2091 /* Portably test for overflow (only works for nonzero values, so make
2092 a second check for zero). FIXME: Can't we just make n and prevn
2093 unsigned and avoid this? */
fa649bb7 2094 if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
c906108c
SS
2095 unsigned_p = 1; /* Try something unsigned */
2096
2097 /* Portably test for unsigned overflow.
2098 FIXME: This check is wrong; for example it doesn't find overflow
2099 on 0x123456789 when LONGEST is 32 bits. */
fa649bb7 2100 if (c != 'l' && c != 'u' && c != 'i' && n != 0)
c906108c 2101 {
20562150 2102 if (unsigned_p && prevn >= n)
001083c6 2103 error (_("Numeric constant too large."));
c906108c
SS
2104 }
2105 prevn = n;
2106 }
2107
2108 /* An integer constant is an int, a long, or a long long. An L
2109 suffix forces it to be long; an LL suffix forces it to be long
2110 long. If not forced to a larger size, it gets the first type of
2111 the above that it fits in. To figure out whether it fits, we
2112 shift it right and see whether anything remains. Note that we
2113 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
2114 operation, because many compilers will warn about such a shift
9a76efb6
UW
2115 (which always produces a zero result). Sometimes gdbarch_int_bit
2116 or gdbarch_long_bit will be that big, sometimes not. To deal with
c906108c
SS
2117 the case where it is we just always shift the value more than
2118 once, with fewer bits each time. */
2119
20562150 2120 un = n >> 2;
c906108c 2121 if (long_p == 0
fa9f5be6 2122 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
c906108c 2123 {
410a0ff2 2124 high_bit
fa9f5be6 2125 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
c906108c
SS
2126
2127 /* A large decimal (not hex or octal) constant (between INT_MAX
2128 and UINT_MAX) is a long or unsigned long, according to ANSI,
2129 never an unsigned int, but this code treats it as unsigned
2130 int. This probably should be fixed. GCC gives a warning on
2131 such constants. */
2132
410a0ff2
SDJ
2133 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
2134 signed_type = parse_type (par_state)->builtin_int;
c906108c
SS
2135 }
2136 else if (long_p <= 1
fa9f5be6 2137 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
c906108c 2138 {
410a0ff2 2139 high_bit
fa9f5be6 2140 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
410a0ff2
SDJ
2141 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
2142 signed_type = parse_type (par_state)->builtin_long;
c906108c
SS
2143 }
2144 else
2145 {
2146 int shift;
4d29c0a8 2147 if (sizeof (ULONGEST) * HOST_CHAR_BIT
fa9f5be6 2148 < gdbarch_long_long_bit (par_state->gdbarch ()))
c906108c
SS
2149 /* A long long does not fit in a LONGEST. */
2150 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
2151 else
fa9f5be6 2152 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
c906108c 2153 high_bit = (ULONGEST) 1 << shift;
410a0ff2
SDJ
2154 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
2155 signed_type = parse_type (par_state)->builtin_long_long;
c906108c
SS
2156 }
2157
2158 putithere->typed_val_int.val = n;
2159
2160 /* If the high bit of the worked out type is set then this number
2161 has to be unsigned. */
2162
4d29c0a8 2163 if (unsigned_p || (n & high_bit))
c906108c
SS
2164 {
2165 putithere->typed_val_int.type = unsigned_type;
2166 }
4d29c0a8 2167 else
c906108c
SS
2168 {
2169 putithere->typed_val_int.type = signed_type;
2170 }
2171
fa649bb7
TT
2172 if (imaginary_p)
2173 putithere->typed_val_int.type
2174 = init_complex_type (nullptr, putithere->typed_val_int.type);
2175
2176 return imaginary_p ? COMPLEX_INT : INT;
c906108c
SS
2177}
2178
6c7a06a3
TT
2179/* Temporary obstack used for holding strings. */
2180static struct obstack tempbuf;
2181static int tempbuf_init;
2182
2183/* Parse a C escape sequence. The initial backslash of the sequence
2184 is at (*PTR)[-1]. *PTR will be updated to point to just after the
2185 last character of the sequence. If OUTPUT is not NULL, the
2186 translated form of the escape sequence will be written there. If
2187 OUTPUT is NULL, no output is written and the call will only affect
2188 *PTR. If an escape sequence is expressed in target bytes, then the
2189 entire sequence will simply be copied to OUTPUT. Return 1 if any
2190 character was emitted, 0 otherwise. */
2191
2192int
d7561cbb 2193c_parse_escape (const char **ptr, struct obstack *output)
6c7a06a3 2194{
d7561cbb 2195 const char *tokptr = *ptr;
6c7a06a3
TT
2196 int result = 1;
2197
2198 /* Some escape sequences undergo character set conversion. Those we
2199 translate here. */
2200 switch (*tokptr)
2201 {
2202 /* Hex escapes do not undergo character set conversion, so keep
2203 the escape sequence for later. */
2204 case 'x':
2205 if (output)
2206 obstack_grow_str (output, "\\x");
2207 ++tokptr;
b1b60145 2208 if (!ISXDIGIT (*tokptr))
6c7a06a3 2209 error (_("\\x escape without a following hex digit"));
b1b60145 2210 while (ISXDIGIT (*tokptr))
6c7a06a3
TT
2211 {
2212 if (output)
2213 obstack_1grow (output, *tokptr);
2214 ++tokptr;
2215 }
2216 break;
2217
2218 /* Octal escapes do not undergo character set conversion, so
2219 keep the escape sequence for later. */
2220 case '0':
2221 case '1':
2222 case '2':
2223 case '3':
2224 case '4':
2225 case '5':
2226 case '6':
2227 case '7':
30b66ecc
TT
2228 {
2229 int i;
2230 if (output)
2231 obstack_grow_str (output, "\\");
2232 for (i = 0;
b1b60145 2233 i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
30b66ecc
TT
2234 ++i)
2235 {
2236 if (output)
2237 obstack_1grow (output, *tokptr);
2238 ++tokptr;
2239 }
2240 }
6c7a06a3
TT
2241 break;
2242
2243 /* We handle UCNs later. We could handle them here, but that
2244 would mean a spurious error in the case where the UCN could
2245 be converted to the target charset but not the host
2246 charset. */
2247 case 'u':
2248 case 'U':
2249 {
2250 char c = *tokptr;
2251 int i, len = c == 'U' ? 8 : 4;
2252 if (output)
2253 {
2254 obstack_1grow (output, '\\');
2255 obstack_1grow (output, *tokptr);
2256 }
2257 ++tokptr;
b1b60145 2258 if (!ISXDIGIT (*tokptr))
6c7a06a3 2259 error (_("\\%c escape without a following hex digit"), c);
b1b60145 2260 for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
6c7a06a3
TT
2261 {
2262 if (output)
2263 obstack_1grow (output, *tokptr);
2264 ++tokptr;
2265 }
2266 }
2267 break;
2268
2269 /* We must pass backslash through so that it does not
2270 cause quoting during the second expansion. */
2271 case '\\':
2272 if (output)
2273 obstack_grow_str (output, "\\\\");
2274 ++tokptr;
2275 break;
2276
2277 /* Escapes which undergo conversion. */
2278 case 'a':
2279 if (output)
2280 obstack_1grow (output, '\a');
2281 ++tokptr;
2282 break;
2283 case 'b':
2284 if (output)
2285 obstack_1grow (output, '\b');
2286 ++tokptr;
2287 break;
2288 case 'f':
2289 if (output)
2290 obstack_1grow (output, '\f');
2291 ++tokptr;
2292 break;
2293 case 'n':
2294 if (output)
2295 obstack_1grow (output, '\n');
2296 ++tokptr;
2297 break;
2298 case 'r':
2299 if (output)
2300 obstack_1grow (output, '\r');
2301 ++tokptr;
2302 break;
2303 case 't':
2304 if (output)
2305 obstack_1grow (output, '\t');
2306 ++tokptr;
2307 break;
2308 case 'v':
2309 if (output)
2310 obstack_1grow (output, '\v');
2311 ++tokptr;
2312 break;
2313
2314 /* GCC extension. */
2315 case 'e':
2316 if (output)
2317 obstack_1grow (output, HOST_ESCAPE_CHAR);
2318 ++tokptr;
2319 break;
2320
2321 /* Backslash-newline expands to nothing at all. */
2322 case '\n':
2323 ++tokptr;
2324 result = 0;
2325 break;
2326
2327 /* A few escapes just expand to the character itself. */
2328 case '\'':
2329 case '\"':
2330 case '?':
2331 /* GCC extensions. */
2332 case '(':
2333 case '{':
2334 case '[':
2335 case '%':
2336 /* Unrecognized escapes turn into the character itself. */
2337 default:
2338 if (output)
2339 obstack_1grow (output, *tokptr);
2340 ++tokptr;
2341 break;
2342 }
2343 *ptr = tokptr;
2344 return result;
2345}
2346
2347/* Parse a string or character literal from TOKPTR. The string or
2348 character may be wide or unicode. *OUTPTR is set to just after the
2349 end of the literal in the input string. The resulting token is
2350 stored in VALUE. This returns a token value, either STRING or
2351 CHAR, depending on what was parsed. *HOST_CHARS is set to the
2352 number of host characters in the literal. */
4d29c0a8 2353
6c7a06a3 2354static int
d7561cbb
KS
2355parse_string_or_char (const char *tokptr, const char **outptr,
2356 struct typed_stoken *value, int *host_chars)
6c7a06a3 2357{
8c5630cb 2358 int quote;
0c801b96 2359 c_string_type type;
f2e8016f 2360 int is_objc = 0;
6c7a06a3
TT
2361
2362 /* Build the gdb internal form of the input string in tempbuf. Note
2363 that the buffer is null byte terminated *only* for the
2364 convenience of debugging gdb itself and printing the buffer
2365 contents when the buffer contains no embedded nulls. Gdb does
2366 not depend upon the buffer being null byte terminated, it uses
2367 the length string instead. This allows gdb to handle C strings
2368 (as well as strings in other languages) with embedded null
2369 bytes */
2370
2371 if (!tempbuf_init)
2372 tempbuf_init = 1;
2373 else
2374 obstack_free (&tempbuf, NULL);
2375 obstack_init (&tempbuf);
2376
2377 /* Record the string type. */
2378 if (*tokptr == 'L')
2379 {
2380 type = C_WIDE_STRING;
2381 ++tokptr;
2382 }
2383 else if (*tokptr == 'u')
2384 {
2385 type = C_STRING_16;
2386 ++tokptr;
2387 }
2388 else if (*tokptr == 'U')
2389 {
2390 type = C_STRING_32;
2391 ++tokptr;
2392 }
f2e8016f
TT
2393 else if (*tokptr == '@')
2394 {
2395 /* An Objective C string. */
2396 is_objc = 1;
2397 type = C_STRING;
2398 ++tokptr;
2399 }
6c7a06a3
TT
2400 else
2401 type = C_STRING;
2402
2403 /* Skip the quote. */
2404 quote = *tokptr;
2405 if (quote == '\'')
2406 type |= C_CHAR;
2407 ++tokptr;
2408
2409 *host_chars = 0;
2410
2411 while (*tokptr)
2412 {
2413 char c = *tokptr;
2414 if (c == '\\')
2415 {
2416 ++tokptr;
2417 *host_chars += c_parse_escape (&tokptr, &tempbuf);
2418 }
2419 else if (c == quote)
2420 break;
2421 else
2422 {
2423 obstack_1grow (&tempbuf, c);
2424 ++tokptr;
2425 /* FIXME: this does the wrong thing with multi-byte host
2426 characters. We could use mbrlen here, but that would
2427 make "set host-charset" a bit less useful. */
2428 ++*host_chars;
2429 }
2430 }
2431
2432 if (*tokptr != quote)
2433 {
2434 if (quote == '"')
001083c6 2435 error (_("Unterminated string in expression."));
6c7a06a3 2436 else
001083c6 2437 error (_("Unmatched single quote."));
6c7a06a3
TT
2438 }
2439 ++tokptr;
2440
2441 value->type = type;
79f33898 2442 value->ptr = (char *) obstack_base (&tempbuf);
6c7a06a3
TT
2443 value->length = obstack_object_size (&tempbuf);
2444
2445 *outptr = tokptr;
2446
f2e8016f 2447 return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
6c7a06a3
TT
2448}
2449
274b54d7
TT
2450/* This is used to associate some attributes with a token. */
2451
8d297bbf 2452enum token_flag
274b54d7
TT
2453{
2454 /* If this bit is set, the token is C++-only. */
2455
2456 FLAG_CXX = 1,
2457
3293bbaf
TT
2458 /* If this bit is set, the token is C-only. */
2459
2460 FLAG_C = 2,
2461
274b54d7
TT
2462 /* If this bit is set, the token is conditional: if there is a
2463 symbol of the same name, then the token is a symbol; otherwise,
2464 the token is a keyword. */
2465
3293bbaf 2466 FLAG_SHADOW = 4
274b54d7 2467};
8d297bbf 2468DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
274b54d7 2469
c906108c
SS
2470struct token
2471{
a121b7c1 2472 const char *oper;
c906108c
SS
2473 int token;
2474 enum exp_opcode opcode;
8d297bbf 2475 token_flags flags;
c906108c
SS
2476};
2477
2478static const struct token tokentab3[] =
2479 {
ba163c7e 2480 {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
c1af96a0 2481 {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
79ab486e
TT
2482 {"->*", ARROW_STAR, OP_NULL, FLAG_CXX},
2483 {"...", DOTDOTDOT, OP_NULL, 0}
c906108c
SS
2484 };
2485
2486static const struct token tokentab2[] =
2487 {
ba163c7e
TT
2488 {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
2489 {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
2490 {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
2491 {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
2492 {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
2493 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
2494 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
2495 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
79ab486e
TT
2496 {"++", INCREMENT, OP_NULL, 0},
2497 {"--", DECREMENT, OP_NULL, 0},
2498 {"->", ARROW, OP_NULL, 0},
2499 {"&&", ANDAND, OP_NULL, 0},
2500 {"||", OROR, OP_NULL, 0},
ec7f2efe
KS
2501 /* "::" is *not* only C++: gdb overrides its meaning in several
2502 different ways, e.g., 'filename'::func, function::variable. */
79ab486e
TT
2503 {"::", COLONCOLON, OP_NULL, 0},
2504 {"<<", LSH, OP_NULL, 0},
2505 {">>", RSH, OP_NULL, 0},
2506 {"==", EQUAL, OP_NULL, 0},
2507 {"!=", NOTEQUAL, OP_NULL, 0},
2508 {"<=", LEQ, OP_NULL, 0},
2509 {">=", GEQ, OP_NULL, 0},
2510 {".*", DOT_STAR, OP_NULL, FLAG_CXX}
ba163c7e
TT
2511 };
2512
b6c95c0c
AB
2513/* Identifier-like tokens. Only type-specifiers than can appear in
2514 multi-word type names (for example 'double' can appear in 'long
2515 double') need to be listed here. type-specifiers that are only ever
3638a098 2516 single word (like 'char') are handled by the classify_name function. */
ba163c7e
TT
2517static const struct token ident_tokens[] =
2518 {
2519 {"unsigned", UNSIGNED, OP_NULL, 0},
274b54d7 2520 {"template", TEMPLATE, OP_NULL, FLAG_CXX},
ba163c7e
TT
2521 {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
2522 {"struct", STRUCT, OP_NULL, 0},
2523 {"signed", SIGNED_KEYWORD, OP_NULL, 0},
2524 {"sizeof", SIZEOF, OP_NULL, 0},
007e1530
TT
2525 {"_Alignof", ALIGNOF, OP_NULL, 0},
2526 {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
ba163c7e 2527 {"double", DOUBLE_KEYWORD, OP_NULL, 0},
3638a098 2528 {"float", FLOAT_KEYWORD, OP_NULL, 0},
274b54d7
TT
2529 {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
2530 {"class", CLASS, OP_NULL, FLAG_CXX},
ba163c7e
TT
2531 {"union", UNION, OP_NULL, 0},
2532 {"short", SHORT, OP_NULL, 0},
2533 {"const", CONST_KEYWORD, OP_NULL, 0},
3293bbaf
TT
2534 {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
2535 {"__restrict__", RESTRICT, OP_NULL, 0},
2536 {"__restrict", RESTRICT, OP_NULL, 0},
2537 {"_Atomic", ATOMIC, OP_NULL, 0},
ba163c7e
TT
2538 {"enum", ENUM, OP_NULL, 0},
2539 {"long", LONG, OP_NULL, 0},
3638a098
TT
2540 {"_Complex", COMPLEX, OP_NULL, 0},
2541 {"__complex__", COMPLEX, OP_NULL, 0},
2542
274b54d7 2543 {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
ba163c7e 2544 {"int", INT_KEYWORD, OP_NULL, 0},
274b54d7
TT
2545 {"new", NEW, OP_NULL, FLAG_CXX},
2546 {"delete", DELETE, OP_NULL, FLAG_CXX},
2547 {"operator", OPERATOR, OP_NULL, FLAG_CXX},
2548
79ab486e 2549 {"and", ANDAND, OP_NULL, FLAG_CXX},
274b54d7
TT
2550 {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
2551 {"bitand", '&', OP_NULL, FLAG_CXX},
2552 {"bitor", '|', OP_NULL, FLAG_CXX},
2553 {"compl", '~', OP_NULL, FLAG_CXX},
2554 {"not", '!', OP_NULL, FLAG_CXX},
79ab486e
TT
2555 {"not_eq", NOTEQUAL, OP_NULL, FLAG_CXX},
2556 {"or", OROR, OP_NULL, FLAG_CXX},
274b54d7
TT
2557 {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
2558 {"xor", '^', OP_NULL, FLAG_CXX},
2559 {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
2560
2561 {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
2562 {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
2563 {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
608b4967
TT
2564 {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
2565
2566 {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
2567 {"__typeof", TYPEOF, OP_TYPEOF, 0 },
2568 {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
2569 {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
6e72ca20
TT
2570 {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
2571
2572 {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
c906108c
SS
2573 };
2574
7c8adf68
TT
2575
2576static void
14d960c8 2577scan_macro_expansion (const char *expansion)
7c8adf68 2578{
7c8adf68 2579 /* We'd better not be trying to push the stack twice. */
9d30e1fd 2580 gdb_assert (! cpstate->macro_original_text);
7c8adf68 2581
14d960c8
SM
2582 /* Copy to the obstack. */
2583 const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
7c8adf68
TT
2584
2585 /* Save the old lexptr value, so we can return to it when we're done
2586 parsing the expanded text. */
5776fca3
TT
2587 cpstate->macro_original_text = pstate->lexptr;
2588 pstate->lexptr = copy;
7c8adf68
TT
2589}
2590
7c8adf68
TT
2591static int
2592scanning_macro_expansion (void)
2593{
9d30e1fd 2594 return cpstate->macro_original_text != 0;
7c8adf68
TT
2595}
2596
4d29c0a8 2597static void
7c8adf68
TT
2598finished_macro_expansion (void)
2599{
2600 /* There'd better be something to pop back to. */
9d30e1fd 2601 gdb_assert (cpstate->macro_original_text);
7c8adf68
TT
2602
2603 /* Pop back to the original text. */
5776fca3 2604 pstate->lexptr = cpstate->macro_original_text;
9d30e1fd 2605 cpstate->macro_original_text = 0;
7c8adf68
TT
2606}
2607
4e8f195d
TT
2608/* Return true iff the token represents a C++ cast operator. */
2609
2610static int
2611is_cast_operator (const char *token, int len)
2612{
2613 return (! strncmp (token, "dynamic_cast", len)
2614 || ! strncmp (token, "static_cast", len)
2615 || ! strncmp (token, "reinterpret_cast", len)
2616 || ! strncmp (token, "const_cast", len));
2617}
7c8adf68
TT
2618
2619/* The scope used for macro expansion. */
2620static struct macro_scope *expression_macro_scope;
2621
65d12d83
TT
2622/* This is set if a NAME token appeared at the very end of the input
2623 string, with no whitespace separating the name from the EOF. This
2624 is used only when parsing to do field name completion. */
2625static int saw_name_at_eof;
2626
2627/* This is set if the previously-returned token was a structure
59498c30
LS
2628 operator -- either '.' or ARROW. */
2629static bool last_was_structop;
65d12d83 2630
28aaf3fd
TT
2631/* Depth of parentheses. */
2632static int paren_depth;
2633
c906108c
SS
2634/* Read one token, getting characters through lexptr. */
2635
2636static int
59498c30 2637lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
c906108c
SS
2638{
2639 int c;
2640 int namelen;
2641 unsigned int i;
d7561cbb 2642 const char *tokstart;
59498c30 2643 bool saw_structop = last_was_structop;
65d12d83 2644
59498c30
LS
2645 last_was_structop = false;
2646 *is_quoted_name = false;
65d12d83 2647
c906108c
SS
2648 retry:
2649
84f0252a
JB
2650 /* Check if this is a macro invocation that we need to expand. */
2651 if (! scanning_macro_expansion ())
2652 {
14d960c8
SM
2653 gdb::unique_xmalloc_ptr<char> expanded
2654 = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
84f0252a 2655
14d960c8 2656 if (expanded != nullptr)
dda83cd7 2657 scan_macro_expansion (expanded.get ());
84f0252a
JB
2658 }
2659
5776fca3 2660 pstate->prev_lexptr = pstate->lexptr;
c906108c 2661
5776fca3 2662 tokstart = pstate->lexptr;
c906108c
SS
2663 /* See if it is a special token of length 3. */
2664 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
fe978cb0 2665 if (strncmp (tokstart, tokentab3[i].oper, 3) == 0)
c906108c 2666 {
274b54d7 2667 if ((tokentab3[i].flags & FLAG_CXX) != 0
73923d7e 2668 && par_state->language ()->la_language != language_cplus)
ec7f2efe 2669 break;
3293bbaf 2670 gdb_assert ((tokentab3[i].flags & FLAG_C) == 0);
ec7f2efe 2671
5776fca3 2672 pstate->lexptr += 3;
c906108c
SS
2673 yylval.opcode = tokentab3[i].opcode;
2674 return tokentab3[i].token;
2675 }
2676
2677 /* See if it is a special token of length 2. */
2678 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
fe978cb0 2679 if (strncmp (tokstart, tokentab2[i].oper, 2) == 0)
c906108c 2680 {
274b54d7 2681 if ((tokentab2[i].flags & FLAG_CXX) != 0
73923d7e 2682 && par_state->language ()->la_language != language_cplus)
ec7f2efe 2683 break;
f7d4f0b1 2684 gdb_assert ((tokentab2[i].flags & FLAG_C) == 0);
ec7f2efe 2685
5776fca3 2686 pstate->lexptr += 2;
c906108c 2687 yylval.opcode = tokentab2[i].opcode;
59498c30 2688 if (tokentab2[i].token == ARROW)
65d12d83 2689 last_was_structop = 1;
c906108c
SS
2690 return tokentab2[i].token;
2691 }
2692
2693 switch (c = *tokstart)
2694 {
2695 case 0:
84f0252a 2696 /* If we were just scanning the result of a macro expansion,
dda83cd7 2697 then we need to resume scanning the original text.
65d12d83
TT
2698 If we're parsing for field name completion, and the previous
2699 token allows such completion, return a COMPLETE token.
dda83cd7
SM
2700 Otherwise, we were already scanning the original text, and
2701 we're really done. */
84f0252a 2702 if (scanning_macro_expansion ())
dda83cd7
SM
2703 {
2704 finished_macro_expansion ();
2705 goto retry;
2706 }
65d12d83
TT
2707 else if (saw_name_at_eof)
2708 {
2709 saw_name_at_eof = 0;
2710 return COMPLETE;
2711 }
2a612529 2712 else if (par_state->parse_completion && saw_structop)
65d12d83 2713 return COMPLETE;
84f0252a 2714 else
dda83cd7 2715 return 0;
c906108c
SS
2716
2717 case ' ':
2718 case '\t':
2719 case '\n':
5776fca3 2720 pstate->lexptr++;
c906108c
SS
2721 goto retry;
2722
379a77b5 2723 case '[':
c906108c
SS
2724 case '(':
2725 paren_depth++;
5776fca3 2726 pstate->lexptr++;
73923d7e 2727 if (par_state->language ()->la_language == language_objc
410a0ff2 2728 && c == '[')
f2e8016f 2729 return OBJC_LBRAC;
c906108c
SS
2730 return c;
2731
379a77b5 2732 case ']':
c906108c
SS
2733 case ')':
2734 if (paren_depth == 0)
2735 return 0;
2736 paren_depth--;
5776fca3 2737 pstate->lexptr++;
c906108c
SS
2738 return c;
2739
2740 case ',':
8621b685 2741 if (pstate->comma_terminates
dda83cd7
SM
2742 && paren_depth == 0
2743 && ! scanning_macro_expansion ())
c906108c 2744 return 0;
5776fca3 2745 pstate->lexptr++;
c906108c
SS
2746 return c;
2747
2748 case '.':
2749 /* Might be a floating point number. */
5776fca3 2750 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
65d12d83 2751 {
59498c30 2752 last_was_structop = true;
65d12d83
TT
2753 goto symbol; /* Nope, must be a symbol. */
2754 }
86a73007 2755 /* FALL THRU. */
c906108c
SS
2756
2757 case '0':
2758 case '1':
2759 case '2':
2760 case '3':
2761 case '4':
2762 case '5':
2763 case '6':
2764 case '7':
2765 case '8':
2766 case '9':
2767 {
2768 /* It's a number. */
2b4e6a3f 2769 int got_dot = 0, got_e = 0, got_p = 0, toktype;
d7561cbb 2770 const char *p = tokstart;
c906108c
SS
2771 int hex = input_radix > 10;
2772
2773 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
2774 {
2775 p += 2;
2776 hex = 1;
2777 }
2778 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
2779 {
2780 p += 2;
2781 hex = 0;
2782 }
2783
2784 for (;; ++p)
2785 {
2786 /* This test includes !hex because 'e' is a valid hex digit
2787 and thus does not indicate a floating point number when
2788 the radix is hex. */
2b4e6a3f 2789 if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
c906108c 2790 got_dot = got_e = 1;
2b4e6a3f
TT
2791 else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
2792 got_dot = got_p = 1;
c906108c
SS
2793 /* This test does not include !hex, because a '.' always indicates
2794 a decimal floating point number regardless of the radix. */
2795 else if (!got_dot && *p == '.')
2796 got_dot = 1;
2b4e6a3f
TT
2797 else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
2798 || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
c906108c
SS
2799 && (*p == '-' || *p == '+'))
2800 /* This is the sign of the exponent, not the end of the
2801 number. */
2802 continue;
2803 /* We will take any letters or digits. parse_number will
2804 complain if past the radix, or if L or U are not final. */
2805 else if ((*p < '0' || *p > '9')
2806 && ((*p < 'a' || *p > 'z')
2807 && (*p < 'A' || *p > 'Z')))
2808 break;
2809 }
410a0ff2 2810 toktype = parse_number (par_state, tokstart, p - tokstart,
2b4e6a3f 2811 got_dot | got_e | got_p, &yylval);
dda83cd7 2812 if (toktype == ERROR)
c906108c
SS
2813 {
2814 char *err_copy = (char *) alloca (p - tokstart + 1);
2815
2816 memcpy (err_copy, tokstart, p - tokstart);
2817 err_copy[p - tokstart] = 0;
001083c6 2818 error (_("Invalid number \"%s\"."), err_copy);
c906108c 2819 }
5776fca3 2820 pstate->lexptr = p;
c906108c
SS
2821 return toktype;
2822 }
2823
941b2081
JK
2824 case '@':
2825 {
d7561cbb 2826 const char *p = &tokstart[1];
941b2081 2827
73923d7e 2828 if (par_state->language ()->la_language == language_objc)
f2e8016f
TT
2829 {
2830 size_t len = strlen ("selector");
2831
2832 if (strncmp (p, "selector", len) == 0
b1b60145 2833 && (p[len] == '\0' || ISSPACE (p[len])))
f2e8016f 2834 {
5776fca3 2835 pstate->lexptr = p + len;
f2e8016f
TT
2836 return SELECTOR;
2837 }
2838 else if (*p == '"')
2839 goto parse_string;
2840 }
2841
b1b60145 2842 while (ISSPACE (*p))
941b2081 2843 p++;
b926417a 2844 size_t len = strlen ("entry");
b1b60145 2845 if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
941b2081
JK
2846 && p[len] != '_')
2847 {
5776fca3 2848 pstate->lexptr = &p[len];
941b2081
JK
2849 return ENTRY;
2850 }
2851 }
2852 /* FALLTHRU */
c906108c
SS
2853 case '+':
2854 case '-':
2855 case '*':
2856 case '/':
2857 case '%':
2858 case '|':
2859 case '&':
2860 case '^':
2861 case '~':
2862 case '!':
c906108c
SS
2863 case '<':
2864 case '>':
c906108c
SS
2865 case '?':
2866 case ':':
2867 case '=':
2868 case '{':
2869 case '}':
2870 symbol:
5776fca3 2871 pstate->lexptr++;
c906108c
SS
2872 return c;
2873
6c7a06a3
TT
2874 case 'L':
2875 case 'u':
2876 case 'U':
2877 if (tokstart[1] != '"' && tokstart[1] != '\'')
2878 break;
2879 /* Fall through. */
2880 case '\'':
c906108c 2881 case '"':
f2e8016f
TT
2882
2883 parse_string:
6c7a06a3
TT
2884 {
2885 int host_len;
5776fca3
TT
2886 int result = parse_string_or_char (tokstart, &pstate->lexptr,
2887 &yylval.tsval, &host_len);
6c7a06a3 2888 if (result == CHAR)
c906108c 2889 {
6c7a06a3 2890 if (host_len == 0)
001083c6 2891 error (_("Empty character constant."));
6c7a06a3 2892 else if (host_len > 2 && c == '\'')
c906108c 2893 {
6c7a06a3 2894 ++tokstart;
5776fca3 2895 namelen = pstate->lexptr - tokstart - 1;
59498c30 2896 *is_quoted_name = true;
805e1f19 2897
6c7a06a3 2898 goto tryname;
c906108c 2899 }
6c7a06a3 2900 else if (host_len > 1)
001083c6 2901 error (_("Invalid character constant."));
c906108c 2902 }
6c7a06a3
TT
2903 return result;
2904 }
c906108c
SS
2905 }
2906
b1b60145 2907 if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
c906108c 2908 /* We must have come across a bad character (e.g. ';'). */
001083c6 2909 error (_("Invalid character '%c' in expression."), c);
c906108c
SS
2910
2911 /* It's a name. See how long it is. */
2912 namelen = 0;
2913 for (c = tokstart[namelen];
b1b60145 2914 (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
c906108c
SS
2915 {
2916 /* Template parameter lists are part of the name.
2917 FIXME: This mishandles `print $a<4&&$a>3'. */
2918
2919 if (c == '<')
4e8f195d
TT
2920 {
2921 if (! is_cast_operator (tokstart, namelen))
2922 {
2923 /* Scan ahead to get rest of the template specification. Note
2924 that we look ahead only when the '<' adjoins non-whitespace
2925 characters; for comparison expressions, e.g. "a < b > c",
2926 there must be spaces before the '<', etc. */
d7561cbb
KS
2927 const char *p = find_template_name_end (tokstart + namelen);
2928
4e8f195d
TT
2929 if (p)
2930 namelen = p - tokstart;
2931 }
2932 break;
c906108c
SS
2933 }
2934 c = tokstart[++namelen];
2935 }
2936
84f0252a
JB
2937 /* The token "if" terminates the expression and is NOT removed from
2938 the input stream. It doesn't count if it appears in the
2939 expansion of a macro. */
2940 if (namelen == 2
2941 && tokstart[0] == 'i'
2942 && tokstart[1] == 'f'
2943 && ! scanning_macro_expansion ())
c906108c
SS
2944 {
2945 return 0;
2946 }
2947
b6199126
DJ
2948 /* For the same reason (breakpoint conditions), "thread N"
2949 terminates the expression. "thread" could be an identifier, but
2950 an identifier is never followed by a number without intervening
2951 punctuation. "task" is similar. Handle abbreviations of these,
2952 similarly to breakpoint.c:find_condition_and_thread. */
2953 if (namelen >= 1
2954 && (strncmp (tokstart, "thread", namelen) == 0
2955 || strncmp (tokstart, "task", namelen) == 0)
2956 && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
2957 && ! scanning_macro_expansion ())
2958 {
d7561cbb
KS
2959 const char *p = tokstart + namelen + 1;
2960
b6199126
DJ
2961 while (*p == ' ' || *p == '\t')
2962 p++;
2963 if (*p >= '0' && *p <= '9')
2964 return 0;
2965 }
2966
5776fca3 2967 pstate->lexptr += namelen;
c906108c
SS
2968
2969 tryname:
2970
c906108c
SS
2971 yylval.sval.ptr = tokstart;
2972 yylval.sval.length = namelen;
2973
ba163c7e 2974 /* Catch specific keywords. */
61f4b350 2975 std::string copy = copy_name (yylval.sval);
ba163c7e 2976 for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++)
61f4b350 2977 if (copy == ident_tokens[i].oper)
ba163c7e 2978 {
274b54d7 2979 if ((ident_tokens[i].flags & FLAG_CXX) != 0
73923d7e 2980 && par_state->language ()->la_language != language_cplus)
ba163c7e 2981 break;
3293bbaf
TT
2982 if ((ident_tokens[i].flags & FLAG_C) != 0
2983 && par_state->language ()->la_language != language_c
2984 && par_state->language ()->la_language != language_objc)
2985 break;
ba163c7e 2986
274b54d7
TT
2987 if ((ident_tokens[i].flags & FLAG_SHADOW) != 0)
2988 {
1993b719 2989 struct field_of_this_result is_a_field_of_this;
274b54d7 2990
61f4b350 2991 if (lookup_symbol (copy.c_str (),
1e58a4a4 2992 pstate->expression_context_block,
274b54d7 2993 VAR_DOMAIN,
73923d7e 2994 (par_state->language ()->la_language
dda83cd7 2995 == language_cplus ? &is_a_field_of_this
d12307c1 2996 : NULL)).symbol
274b54d7
TT
2997 != NULL)
2998 {
2999 /* The keyword is shadowed. */
3000 break;
3001 }
3002 }
3003
ba163c7e
TT
3004 /* It is ok to always set this, even though we don't always
3005 strictly need to. */
3006 yylval.opcode = ident_tokens[i].opcode;
3007 return ident_tokens[i].token;
3008 }
3009
c906108c 3010 if (*tokstart == '$')
cfeadda5 3011 return DOLLAR_VARIABLE;
48e32051 3012
2a612529 3013 if (pstate->parse_completion && *pstate->lexptr == '\0')
48e32051 3014 saw_name_at_eof = 1;
e234dfaf
TT
3015
3016 yylval.ssym.stoken = yylval.sval;
d12307c1
PMR
3017 yylval.ssym.sym.symbol = NULL;
3018 yylval.ssym.sym.block = NULL;
e234dfaf 3019 yylval.ssym.is_a_field_of_this = 0;
48e32051
TT
3020 return NAME;
3021}
3022
3023/* An object of this type is pushed on a FIFO by the "outer" lexer. */
5fe3f3e4 3024struct token_and_value
48e32051
TT
3025{
3026 int token;
e707a91d 3027 YYSTYPE value;
5fe3f3e4 3028};
48e32051
TT
3029
3030/* A FIFO of tokens that have been read but not yet returned to the
3031 parser. */
5fe3f3e4 3032static std::vector<token_and_value> token_fifo;
48e32051
TT
3033
3034/* Non-zero if the lexer should return tokens from the FIFO. */
3035static int popping;
3036
3037/* Temporary storage for c_lex; this holds symbol names as they are
3038 built up. */
9519b2ee 3039static auto_obstack name_obstack;
48e32051
TT
3040
3041/* Classify a NAME token. The contents of the token are in `yylval'.
3042 Updates yylval and returns the new token type. BLOCK is the block
805e1f19
TT
3043 in which lookups start; this can be NULL to mean the global scope.
3044 IS_QUOTED_NAME is non-zero if the name token was originally quoted
59498c30
LS
3045 in single quotes. IS_AFTER_STRUCTOP is true if this name follows
3046 a structure operator -- either '.' or ARROW */
4d29c0a8 3047
48e32051 3048static int
410a0ff2 3049classify_name (struct parser_state *par_state, const struct block *block,
59498c30 3050 bool is_quoted_name, bool is_after_structop)
48e32051 3051{
d12307c1 3052 struct block_symbol bsym;
1993b719 3053 struct field_of_this_result is_a_field_of_this;
48e32051 3054
61f4b350 3055 std::string copy = copy_name (yylval.sval);
48e32051 3056
1993b719
TT
3057 /* Initialize this in case we *don't* use it in this call; that way
3058 we can refer to it unconditionally below. */
3059 memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
3060
61f4b350 3061 bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
5bae7c4e 3062 par_state->language ()->name_of_this ()
d12307c1 3063 ? &is_a_field_of_this : NULL);
48e32051 3064
d12307c1 3065 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_BLOCK)
c906108c 3066 {
d12307c1 3067 yylval.ssym.sym = bsym;
1993b719 3068 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
48e32051 3069 return BLOCKNAME;
c906108c 3070 }
d12307c1 3071 else if (!bsym.symbol)
48e32051 3072 {
6592e36f
TT
3073 /* If we found a field of 'this', we might have erroneously
3074 found a constructor where we wanted a type name. Handle this
3075 case by noticing that we found a constructor and then look up
3076 the type tag instead. */
3077 if (is_a_field_of_this.type != NULL
3078 && is_a_field_of_this.fn_field != NULL
3079 && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
3080 0))
3081 {
3082 struct field_of_this_result inner_is_a_field_of_this;
3083
61f4b350 3084 bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
d12307c1
PMR
3085 &inner_is_a_field_of_this);
3086 if (bsym.symbol != NULL)
6592e36f 3087 {
d12307c1 3088 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
6592e36f
TT
3089 return TYPENAME;
3090 }
3091 }
805e1f19 3092
59498c30
LS
3093 /* If we found a field on the "this" object, or we are looking
3094 up a field on a struct, then we want to prefer it over a
805e1f19
TT
3095 filename. However, if the name was quoted, then it is better
3096 to check for a filename or a block, since this is the only
3097 way the user has of requiring the extension to be used. */
59498c30
LS
3098 if ((is_a_field_of_this.type == NULL && !is_after_structop)
3099 || is_quoted_name)
805e1f19
TT
3100 {
3101 /* See if it's a file name. */
3102 struct symtab *symtab;
3103
61f4b350 3104 symtab = lookup_symtab (copy.c_str ());
805e1f19
TT
3105 if (symtab)
3106 {
439247b6 3107 yylval.bval = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab),
805e1f19
TT
3108 STATIC_BLOCK);
3109 return FILENAME;
3110 }
3111 }
48e32051 3112 }
c906108c 3113
d12307c1 3114 if (bsym.symbol && SYMBOL_CLASS (bsym.symbol) == LOC_TYPEDEF)
48e32051 3115 {
d12307c1 3116 yylval.tsym.type = SYMBOL_TYPE (bsym.symbol);
47663de5 3117 return TYPENAME;
48e32051 3118 }
c906108c 3119
f2e8016f 3120 /* See if it's an ObjC classname. */
73923d7e 3121 if (par_state->language ()->la_language == language_objc && !bsym.symbol)
f2e8016f 3122 {
61f4b350
TT
3123 CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
3124 copy.c_str ());
f2e8016f
TT
3125 if (Class)
3126 {
d12307c1
PMR
3127 struct symbol *sym;
3128
fe978cb0 3129 yylval.theclass.theclass = Class;
61f4b350 3130 sym = lookup_struct_typedef (copy.c_str (),
1e58a4a4 3131 par_state->expression_context_block, 1);
826f0041 3132 if (sym)
fe978cb0 3133 yylval.theclass.type = SYMBOL_TYPE (sym);
f2e8016f
TT
3134 return CLASSNAME;
3135 }
3136 }
3137
48e32051
TT
3138 /* Input names that aren't symbols but ARE valid hex numbers, when
3139 the input radix permits them, can be names or numbers depending
3140 on the parse. Note we support radixes > 16 here. */
d12307c1 3141 if (!bsym.symbol
48e32051
TT
3142 && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
3143 || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
3144 {
3145 YYSTYPE newlval; /* Its value is ignored. */
61f4b350 3146 int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
410a0ff2 3147 0, &newlval);
d12307c1 3148
48e32051
TT
3149 if (hextype == INT)
3150 {
d12307c1 3151 yylval.ssym.sym = bsym;
1993b719 3152 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
48e32051
TT
3153 return NAME_OR_INT;
3154 }
3155 }
3156
3157 /* Any other kind of symbol */
d12307c1 3158 yylval.ssym.sym = bsym;
1993b719 3159 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
7322dca9 3160
d12307c1 3161 if (bsym.symbol == NULL
73923d7e 3162 && par_state->language ()->la_language == language_cplus
1993b719 3163 && is_a_field_of_this.type == NULL
61f4b350 3164 && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
7322dca9
SW
3165 return UNKNOWN_CPP_NAME;
3166
48e32051
TT
3167 return NAME;
3168}
c906108c 3169
48e32051 3170/* Like classify_name, but used by the inner loop of the lexer, when a
e234dfaf
TT
3171 name might have already been seen. CONTEXT is the context type, or
3172 NULL if this is the first component of a name. */
50af5481 3173
48e32051 3174static int
410a0ff2
SDJ
3175classify_inner_name (struct parser_state *par_state,
3176 const struct block *block, struct type *context)
48e32051 3177{
df54f8eb 3178 struct type *type;
48e32051 3179
e234dfaf 3180 if (context == NULL)
59498c30 3181 return classify_name (par_state, block, false, false);
48e32051 3182
e234dfaf 3183 type = check_typedef (context);
3d567982 3184 if (!type_aggregate_p (type))
50af5481 3185 return ERROR;
48e32051 3186
61f4b350 3187 std::string copy = copy_name (yylval.ssym.stoken);
4dcabcc2 3188 /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
61f4b350
TT
3189 yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
3190 VAR_DOMAIN);
f7e3ecae
KS
3191
3192 /* If no symbol was found, search for a matching base class named
3193 COPY. This will allow users to enter qualified names of class members
3194 relative to the `this' pointer. */
d12307c1 3195 if (yylval.ssym.sym.symbol == NULL)
f7e3ecae 3196 {
61f4b350
TT
3197 struct type *base_type = cp_find_type_baseclass_by_name (type,
3198 copy.c_str ());
f7e3ecae
KS
3199
3200 if (base_type != NULL)
3201 {
3202 yylval.tsym.type = base_type;
3203 return TYPENAME;
3204 }
3205
3206 return ERROR;
3207 }
50af5481 3208
d12307c1 3209 switch (SYMBOL_CLASS (yylval.ssym.sym.symbol))
50af5481
JK
3210 {
3211 case LOC_BLOCK:
3212 case LOC_LABEL:
f7e3ecae
KS
3213 /* cp_lookup_nested_symbol might have accidentally found a constructor
3214 named COPY when we really wanted a base class of the same name.
3215 Double-check this case by looking for a base class. */
3216 {
61f4b350
TT
3217 struct type *base_type
3218 = cp_find_type_baseclass_by_name (type, copy.c_str ());
f7e3ecae
KS
3219
3220 if (base_type != NULL)
3221 {
3222 yylval.tsym.type = base_type;
3223 return TYPENAME;
3224 }
3225 }
50af5481 3226 return ERROR;
48e32051 3227
50af5481 3228 case LOC_TYPEDEF:
d12307c1 3229 yylval.tsym.type = SYMBOL_TYPE (yylval.ssym.sym.symbol);
50af5481 3230 return TYPENAME;
48e32051 3231
50af5481 3232 default:
50af5481
JK
3233 return NAME;
3234 }
3235 internal_error (__FILE__, __LINE__, _("not reached"));
48e32051
TT
3236}
3237
3238/* The outer level of a two-level lexer. This calls the inner lexer
3239 to return tokens. It then either returns these tokens, or
3240 aggregates them into a larger token. This lets us work around a
3241 problem in our parsing approach, where the parser could not
3242 distinguish between qualified names and qualified types at the
3243 right point.
4d29c0a8 3244
48e32051
TT
3245 This approach is still not ideal, because it mishandles template
3246 types. See the comment in lex_one_token for an example. However,
3247 this is still an improvement over the earlier approach, and will
3248 suffice until we move to better parsing technology. */
4d29c0a8 3249
48e32051
TT
3250static int
3251yylex (void)
3252{
3253 token_and_value current;
b2f83c08 3254 int first_was_coloncolon, last_was_coloncolon;
e234dfaf 3255 struct type *context_type = NULL;
b2f83c08
TT
3256 int last_to_examine, next_to_examine, checkpoint;
3257 const struct block *search_block;
59498c30 3258 bool is_quoted_name, last_lex_was_structop;
48e32051 3259
5fe3f3e4 3260 if (popping && !token_fifo.empty ())
b2f83c08 3261 goto do_pop;
48e32051
TT
3262 popping = 0;
3263
59498c30
LS
3264 last_lex_was_structop = last_was_structop;
3265
b2f83c08
TT
3266 /* Read the first token and decide what to do. Most of the
3267 subsequent code is C++-only; but also depends on seeing a "::" or
3268 name-like token. */
410a0ff2 3269 current.token = lex_one_token (pstate, &is_quoted_name);
48e32051 3270 if (current.token == NAME)
1e58a4a4 3271 current.token = classify_name (pstate, pstate->expression_context_block,
59498c30 3272 is_quoted_name, last_lex_was_structop);
73923d7e 3273 if (pstate->language ()->la_language != language_cplus
b2f83c08
TT
3274 || (current.token != TYPENAME && current.token != COLONCOLON
3275 && current.token != FILENAME))
48e32051
TT
3276 return current.token;
3277
b2f83c08
TT
3278 /* Read any sequence of alternating "::" and name-like tokens into
3279 the token FIFO. */
3280 current.value = yylval;
5fe3f3e4 3281 token_fifo.push_back (current);
b2f83c08
TT
3282 last_was_coloncolon = current.token == COLONCOLON;
3283 while (1)
3284 {
59498c30 3285 bool ignore;
805e1f19
TT
3286
3287 /* We ignore quoted names other than the very first one.
3288 Subsequent ones do not have any special meaning. */
410a0ff2 3289 current.token = lex_one_token (pstate, &ignore);
b2f83c08 3290 current.value = yylval;
5fe3f3e4 3291 token_fifo.push_back (current);
b2f83c08
TT
3292
3293 if ((last_was_coloncolon && current.token != NAME)
3294 || (!last_was_coloncolon && current.token != COLONCOLON))
3295 break;
3296 last_was_coloncolon = !last_was_coloncolon;
3297 }
3298 popping = 1;
3299
3300 /* We always read one extra token, so compute the number of tokens
3301 to examine accordingly. */
5fe3f3e4 3302 last_to_examine = token_fifo.size () - 2;
b2f83c08
TT
3303 next_to_examine = 0;
3304
5fe3f3e4 3305 current = token_fifo[next_to_examine];
b2f83c08
TT
3306 ++next_to_examine;
3307
8268c778 3308 name_obstack.clear ();
b2f83c08
TT
3309 checkpoint = 0;
3310 if (current.token == FILENAME)
3311 search_block = current.value.bval;
3312 else if (current.token == COLONCOLON)
3313 search_block = NULL;
3314 else
e234dfaf 3315 {
b2f83c08 3316 gdb_assert (current.token == TYPENAME);
1e58a4a4 3317 search_block = pstate->expression_context_block;
b2f83c08
TT
3318 obstack_grow (&name_obstack, current.value.sval.ptr,
3319 current.value.sval.length);
3320 context_type = current.value.tsym.type;
3321 checkpoint = 1;
e234dfaf 3322 }
b2f83c08
TT
3323
3324 first_was_coloncolon = current.token == COLONCOLON;
3325 last_was_coloncolon = first_was_coloncolon;
3326
3327 while (next_to_examine <= last_to_examine)
48e32051 3328 {
5fe3f3e4 3329 token_and_value next;
48e32051 3330
5fe3f3e4 3331 next = token_fifo[next_to_examine];
b2f83c08 3332 ++next_to_examine;
48e32051 3333
5fe3f3e4 3334 if (next.token == NAME && last_was_coloncolon)
48e32051
TT
3335 {
3336 int classification;
3337
5fe3f3e4 3338 yylval = next.value;
410a0ff2
SDJ
3339 classification = classify_inner_name (pstate, search_block,
3340 context_type);
48e32051
TT
3341 /* We keep going until we either run out of names, or until
3342 we have a qualified name which is not a type. */
50af5481 3343 if (classification != TYPENAME && classification != NAME)
b2f83c08
TT
3344 break;
3345
3346 /* Accept up to this token. */
3347 checkpoint = next_to_examine;
48e32051
TT
3348
3349 /* Update the partial name we are constructing. */
e234dfaf 3350 if (context_type != NULL)
48e32051
TT
3351 {
3352 /* We don't want to put a leading "::" into the name. */
3353 obstack_grow_str (&name_obstack, "::");
3354 }
5fe3f3e4
TT
3355 obstack_grow (&name_obstack, next.value.sval.ptr,
3356 next.value.sval.length);
48e32051 3357
79f33898 3358 yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
48e32051
TT
3359 yylval.sval.length = obstack_object_size (&name_obstack);
3360 current.value = yylval;
3361 current.token = classification;
3362
3363 last_was_coloncolon = 0;
4d29c0a8 3364
e234dfaf
TT
3365 if (classification == NAME)
3366 break;
3367
3368 context_type = yylval.tsym.type;
48e32051 3369 }
5fe3f3e4 3370 else if (next.token == COLONCOLON && !last_was_coloncolon)
48e32051
TT
3371 last_was_coloncolon = 1;
3372 else
3373 {
3374 /* We've reached the end of the name. */
48e32051
TT
3375 break;
3376 }
48e32051
TT
3377 }
3378
b2f83c08
TT
3379 /* If we have a replacement token, install it as the first token in
3380 the FIFO, and delete the other constituent tokens. */
3381 if (checkpoint > 0)
48e32051 3382 {
224c3ddb 3383 current.value.sval.ptr
0cf9feb9
TT
3384 = obstack_strndup (&cpstate->expansion_obstack,
3385 current.value.sval.ptr,
3386 current.value.sval.length);
b2f83c08 3387
5fe3f3e4 3388 token_fifo[0] = current;
b2f83c08 3389 if (checkpoint > 1)
5fe3f3e4
TT
3390 token_fifo.erase (token_fifo.begin () + 1,
3391 token_fifo.begin () + checkpoint);
48e32051
TT
3392 }
3393
b2f83c08 3394 do_pop:
5fe3f3e4
TT
3395 current = token_fifo[0];
3396 token_fifo.erase (token_fifo.begin ());
48e32051 3397 yylval = current.value;
48e32051 3398 return current.token;
c906108c
SS
3399}
3400
65d12d83 3401int
410a0ff2 3402c_parse (struct parser_state *par_state)
65d12d83 3403{
410a0ff2 3404 /* Setting up the parser state. */
eae49211 3405 scoped_restore pstate_restore = make_scoped_restore (&pstate);
410a0ff2
SDJ
3406 gdb_assert (par_state != NULL);
3407 pstate = par_state;
3408
02e12e38
TT
3409 c_parse_state cstate;
3410 scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
3411
f6c2623e 3412 gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
7c8adf68 3413
1e58a4a4
TT
3414 if (par_state->expression_context_block)
3415 macro_scope
3416 = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
7c8adf68 3417 else
f6c2623e
TT
3418 macro_scope = default_macro_scope ();
3419 if (! macro_scope)
3420 macro_scope = user_macro_scope ();
3421
3422 scoped_restore restore_macro_scope
3423 = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
7c8adf68 3424
156d9eab
TT
3425 scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
3426 parser_debug);
92981e24 3427
7c8adf68 3428 /* Initialize some state used by the lexer. */
59498c30 3429 last_was_structop = false;
65d12d83 3430 saw_name_at_eof = 0;
28aaf3fd 3431 paren_depth = 0;
7c8adf68 3432
5fe3f3e4 3433 token_fifo.clear ();
48e32051 3434 popping = 0;
8268c778 3435 name_obstack.clear ();
48e32051 3436
d182f279
TT
3437 int result = yyparse ();
3438 if (!result)
3439 pstate->set_operation (pstate->pop ());
3440 return result;
65d12d83
TT
3441}
3442
12c5175d
MK
3443#ifdef YYBISON
3444
9507860e
TT
3445/* This is called via the YYPRINT macro when parser debugging is
3446 enabled. It prints a token's value. */
3447
3448static void
3449c_print_token (FILE *file, int type, YYSTYPE value)
3450{
3451 switch (type)
3452 {
3453 case INT:
66be918f
PA
3454 parser_fprintf (file, "typed_val_int<%s, %s>",
3455 TYPE_SAFE_NAME (value.typed_val_int.type),
3456 pulongest (value.typed_val_int.val));
9507860e
TT
3457 break;
3458
3459 case CHAR:
3460 case STRING:
3461 {
224c3ddb 3462 char *copy = (char *) alloca (value.tsval.length + 1);
9507860e
TT
3463
3464 memcpy (copy, value.tsval.ptr, value.tsval.length);
3465 copy[value.tsval.length] = '\0';
3466
66be918f 3467 parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
9507860e
TT
3468 }
3469 break;
3470
3471 case NSSTRING:
cfeadda5 3472 case DOLLAR_VARIABLE:
61f4b350 3473 parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
9507860e
TT
3474 break;
3475
3476 case TYPENAME:
66be918f
PA
3477 parser_fprintf (file, "tsym<type=%s, name=%s>",
3478 TYPE_SAFE_NAME (value.tsym.type),
61f4b350 3479 copy_name (value.tsym.stoken).c_str ());
9507860e
TT
3480 break;
3481
3482 case NAME:
3483 case UNKNOWN_CPP_NAME:
3484 case NAME_OR_INT:
3485 case BLOCKNAME:
66be918f 3486 parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
61f4b350 3487 copy_name (value.ssym.stoken).c_str (),
66be918f 3488 (value.ssym.sym.symbol == NULL
987012b8 3489 ? "(null)" : value.ssym.sym.symbol->print_name ()),
66be918f 3490 value.ssym.is_a_field_of_this);
9507860e
TT
3491 break;
3492
3493 case FILENAME:
66be918f 3494 parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
9507860e
TT
3495 break;
3496 }
3497}
7c8adf68 3498
12c5175d
MK
3499#endif
3500
69d340c6 3501static void
a121b7c1 3502yyerror (const char *msg)
c906108c 3503{
5776fca3
TT
3504 if (pstate->prev_lexptr)
3505 pstate->lexptr = pstate->prev_lexptr;
665132f9 3506
5776fca3 3507 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
c906108c 3508}
This page took 1.720327 seconds and 4 git commands to generate.