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