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