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