[gdb/cli] Add a progress meter
[deliverable/binutils-gdb.git] / gdb / p-exp.y
CommitLineData
373a8247 1/* YACC parser for Pascal expressions, for GDB.
b811d2c2 2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
373a8247 3
5b1ba0e5 4 This file is part of GDB.
373a8247 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.
373a8247 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.
373a8247 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/>. */
373a8247
PM
18
19/* This file is derived from c-exp.y */
20
21/* Parse a Pascal expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
29
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
37
29f319b8 38/* Known bugs or limitations:
373a8247
PM
39 - pascal string operations are not supported at all.
40 - there are some problems with boolean types.
41 - Pascal type hexadecimal constants are not supported
42 because they conflict with the internal variables format.
0df8b418 43 Probably also lots of other problems, less well defined PM. */
373a8247
PM
44%{
45
46#include "defs.h"
373a8247
PM
47#include <ctype.h>
48#include "expression.h"
49#include "value.h"
50#include "parser-defs.h"
51#include "language.h"
52#include "p-lang.h"
53#include "bfd.h" /* Required by objfiles.h. */
54#include "symfile.h" /* Required by objfiles.h. */
0df8b418 55#include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
fe898f56 56#include "block.h"
d7561cbb 57#include "completer.h"
373a8247 58
fa9f5be6 59#define parse_type(ps) builtin_type (ps->gdbarch ())
3e79cecf 60
b3f11165
PA
61/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
62 etc). */
63#define GDB_YY_REMAP_PREFIX pascal_
64#include "yy-remap.h"
f461f5cf 65
410a0ff2
SDJ
66/* The state of the parser, used internally when we are parsing the
67 expression. */
68
69static struct parser_state *pstate = NULL;
70
28aaf3fd
TT
71/* Depth of parentheses. */
72static int paren_depth;
73
373a8247
PM
74int yyparse (void);
75
76static int yylex (void);
77
69d340c6 78static void yyerror (const char *);
373a8247 79
793156e6 80static char *uptok (const char *, int);
373a8247
PM
81%}
82
83/* Although the yacc "value" of an expression is not used,
84 since the result is stored in the structure being created,
85 other node types do have values. */
86
87%union
88 {
89 LONGEST lval;
90 struct {
91 LONGEST val;
92 struct type *type;
93 } typed_val_int;
94 struct {
edd079d9 95 gdb_byte val[16];
373a8247
PM
96 struct type *type;
97 } typed_val_float;
98 struct symbol *sym;
99 struct type *tval;
100 struct stoken sval;
101 struct ttype tsym;
102 struct symtoken ssym;
103 int voidval;
3977b71f 104 const struct block *bval;
373a8247
PM
105 enum exp_opcode opcode;
106 struct internalvar *ivar;
107
108 struct type **tvec;
109 int *ivec;
110 }
111
112%{
113/* YYSTYPE gets defined by %union */
410a0ff2
SDJ
114static int parse_number (struct parser_state *,
115 const char *, int, int, YYSTYPE *);
9819c6c8
PM
116
117static struct type *current_type;
4ae0885a 118static int leftdiv_is_integer;
b9362cc7
AC
119static void push_current_type (void);
120static void pop_current_type (void);
9819c6c8 121static int search_field;
373a8247
PM
122%}
123
9819c6c8 124%type <voidval> exp exp1 type_exp start normal_start variable qualified_name
373a8247
PM
125%type <tval> type typebase
126/* %type <bval> block */
127
128/* Fancy type parsing. */
129%type <tval> ptype
130
131%token <typed_val_int> INT
132%token <typed_val_float> FLOAT
133
134/* Both NAME and TYPENAME tokens represent symbols in the input,
135 and both convey their data as strings.
136 But a TYPENAME is a string that happens to be defined as a typedef
137 or builtin type name (such as int or char)
138 and a NAME is any other symbol.
139 Contexts where this distinction is not important can use the
140 nonterminal "name", which matches either NAME or TYPENAME. */
141
6ced1581 142%token <sval> STRING
9819c6c8 143%token <sval> FIELDNAME
a5a44b53 144%token <voidval> COMPLETE
0df8b418 145%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
373a8247
PM
146%token <tsym> TYPENAME
147%type <sval> name
148%type <ssym> name_not_typename
149
150/* A NAME_OR_INT is a symbol which is not known in the symbol table,
151 but which would parse as a valid number in the current input radix.
152 E.g. "c" when input_radix==16. Depending on the parse, it will be
153 turned into a name or into a number. */
154
155%token <ssym> NAME_OR_INT
156
157%token STRUCT CLASS SIZEOF COLONCOLON
158%token ERROR
159
160/* Special type cases, put in to allow the parser to distinguish different
161 legal basetypes. */
162
02c72701 163%token <sval> DOLLAR_VARIABLE
373a8247
PM
164
165
166/* Object pascal */
167%token THIS
2692ddb3 168%token <lval> TRUEKEYWORD FALSEKEYWORD
373a8247
PM
169
170%left ','
171%left ABOVE_COMMA
172%right ASSIGN
173%left NOT
174%left OR
175%left XOR
176%left ANDAND
177%left '=' NOTEQUAL
178%left '<' '>' LEQ GEQ
179%left LSH RSH DIV MOD
180%left '@'
181%left '+' '-'
182%left '*' '/'
183%right UNARY INCREMENT DECREMENT
184%right ARROW '.' '[' '('
29f319b8 185%left '^'
373a8247
PM
186%token <ssym> BLOCKNAME
187%type <bval> block
188%left COLONCOLON
189
190\f
191%%
192
9819c6c8
PM
193start : { current_type = NULL;
194 search_field = 0;
4ae0885a 195 leftdiv_is_integer = 0;
9819c6c8 196 }
ef944135
TR
197 normal_start {}
198 ;
9819c6c8
PM
199
200normal_start :
201 exp1
373a8247
PM
202 | type_exp
203 ;
204
205type_exp: type
410a0ff2
SDJ
206 { write_exp_elt_opcode (pstate, OP_TYPE);
207 write_exp_elt_type (pstate, $1);
208 write_exp_elt_opcode (pstate, OP_TYPE);
9819c6c8 209 current_type = $1; } ;
373a8247
PM
210
211/* Expressions, including the comma operator. */
212exp1 : exp
213 | exp1 ',' exp
410a0ff2 214 { write_exp_elt_opcode (pstate, BINOP_COMMA); }
373a8247
PM
215 ;
216
217/* Expressions, not including the comma operator. */
218exp : exp '^' %prec UNARY
410a0ff2 219 { write_exp_elt_opcode (pstate, UNOP_IND);
6ced1581 220 if (current_type)
9819c6c8 221 current_type = TYPE_TARGET_TYPE (current_type); }
ef944135 222 ;
373a8247
PM
223
224exp : '@' exp %prec UNARY
410a0ff2 225 { write_exp_elt_opcode (pstate, UNOP_ADDR);
9819c6c8
PM
226 if (current_type)
227 current_type = TYPE_POINTER_TYPE (current_type); }
ef944135 228 ;
373a8247
PM
229
230exp : '-' exp %prec UNARY
410a0ff2 231 { write_exp_elt_opcode (pstate, UNOP_NEG); }
373a8247
PM
232 ;
233
234exp : NOT exp %prec UNARY
410a0ff2 235 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
373a8247
PM
236 ;
237
238exp : INCREMENT '(' exp ')' %prec UNARY
410a0ff2 239 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
373a8247
PM
240 ;
241
242exp : DECREMENT '(' exp ')' %prec UNARY
410a0ff2 243 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT); }
373a8247
PM
244 ;
245
a5a44b53
PM
246
247field_exp : exp '.' %prec UNARY
6ced1581 248 { search_field = 1; }
a5a44b53
PM
249 ;
250
6ced1581 251exp : field_exp FIELDNAME
410a0ff2
SDJ
252 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
253 write_exp_string (pstate, $2);
254 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
6ced1581 255 search_field = 0;
a5a44b53 256 if (current_type)
6ced1581 257 {
78134374 258 while (current_type->code ()
a5a44b53
PM
259 == TYPE_CODE_PTR)
260 current_type =
261 TYPE_TARGET_TYPE (current_type);
262 current_type = lookup_struct_elt_type (
263 current_type, $2.ptr, 0);
264 }
265 }
6ced1581
PM
266 ;
267
a5a44b53
PM
268
269exp : field_exp name
410a0ff2
SDJ
270 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
271 write_exp_string (pstate, $2);
272 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
6ced1581 273 search_field = 0;
9819c6c8 274 if (current_type)
6ced1581 275 {
78134374 276 while (current_type->code ()
a5a44b53
PM
277 == TYPE_CODE_PTR)
278 current_type =
279 TYPE_TARGET_TYPE (current_type);
9819c6c8 280 current_type = lookup_struct_elt_type (
a5a44b53
PM
281 current_type, $2.ptr, 0);
282 }
283 }
284 ;
8662d513 285exp : field_exp name COMPLETE
2a612529 286 { pstate->mark_struct_expression ();
410a0ff2
SDJ
287 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
288 write_exp_string (pstate, $2);
289 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
8662d513 290 ;
a5a44b53
PM
291exp : field_exp COMPLETE
292 { struct stoken s;
2a612529 293 pstate->mark_struct_expression ();
410a0ff2 294 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
a5a44b53
PM
295 s.ptr = "";
296 s.length = 0;
410a0ff2
SDJ
297 write_exp_string (pstate, s);
298 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
a5a44b53
PM
299 ;
300
9819c6c8 301exp : exp '['
0df8b418 302 /* We need to save the current_type value. */
0d5cff50 303 { const char *arrayname;
9819c6c8
PM
304 int arrayfieldindex;
305 arrayfieldindex = is_pascal_string_type (
306 current_type, NULL, NULL,
6ced1581
PM
307 NULL, NULL, &arrayname);
308 if (arrayfieldindex)
9819c6c8
PM
309 {
310 struct stoken stringsval;
d7561cbb
KS
311 char *buf;
312
224c3ddb 313 buf = (char *) alloca (strlen (arrayname) + 1);
d7561cbb 314 stringsval.ptr = buf;
9819c6c8 315 stringsval.length = strlen (arrayname);
d7561cbb 316 strcpy (buf, arrayname);
940da03e
SM
317 current_type
318 = (current_type
319 ->field (arrayfieldindex - 1).type ());
410a0ff2
SDJ
320 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
321 write_exp_string (pstate, stringsval);
322 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
9819c6c8
PM
323 }
324 push_current_type (); }
325 exp1 ']'
326 { pop_current_type ();
410a0ff2 327 write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT);
9819c6c8
PM
328 if (current_type)
329 current_type = TYPE_TARGET_TYPE (current_type); }
ef944135 330 ;
373a8247
PM
331
332exp : exp '('
333 /* This is to save the value of arglist_len
334 being accumulated by an outer function call. */
9819c6c8 335 { push_current_type ();
43476f0b 336 pstate->start_arglist (); }
373a8247 337 arglist ')' %prec ARROW
410a0ff2
SDJ
338 { write_exp_elt_opcode (pstate, OP_FUNCALL);
339 write_exp_elt_longcst (pstate,
43476f0b 340 pstate->end_arglist ());
410a0ff2 341 write_exp_elt_opcode (pstate, OP_FUNCALL);
4ae0885a
PM
342 pop_current_type ();
343 if (current_type)
344 current_type = TYPE_TARGET_TYPE (current_type);
345 }
373a8247
PM
346 ;
347
348arglist :
dda83cd7 349 | exp
43476f0b 350 { pstate->arglist_len = 1; }
373a8247 351 | arglist ',' exp %prec ABOVE_COMMA
43476f0b 352 { pstate->arglist_len++; }
373a8247
PM
353 ;
354
355exp : type '(' exp ')' %prec UNARY
fd0e9d45
PM
356 { if (current_type)
357 {
358 /* Allow automatic dereference of classes. */
78134374
SM
359 if ((current_type->code () == TYPE_CODE_PTR)
360 && (TYPE_TARGET_TYPE (current_type)->code () == TYPE_CODE_STRUCT)
361 && (($1)->code () == TYPE_CODE_STRUCT))
410a0ff2 362 write_exp_elt_opcode (pstate, UNOP_IND);
fd0e9d45 363 }
410a0ff2
SDJ
364 write_exp_elt_opcode (pstate, UNOP_CAST);
365 write_exp_elt_type (pstate, $1);
366 write_exp_elt_opcode (pstate, UNOP_CAST);
9819c6c8 367 current_type = $1; }
373a8247
PM
368 ;
369
370exp : '(' exp1 ')'
371 { }
372 ;
373
374/* Binary operators in order of decreasing precedence. */
375
376exp : exp '*' exp
410a0ff2 377 { write_exp_elt_opcode (pstate, BINOP_MUL); }
373a8247
PM
378 ;
379
4ae0885a
PM
380exp : exp '/' {
381 if (current_type && is_integral_type (current_type))
382 leftdiv_is_integer = 1;
6ced1581 383 }
4ae0885a 384 exp
6ced1581 385 {
4ae0885a
PM
386 if (leftdiv_is_integer && current_type
387 && is_integral_type (current_type))
388 {
410a0ff2
SDJ
389 write_exp_elt_opcode (pstate, UNOP_CAST);
390 write_exp_elt_type (pstate,
391 parse_type (pstate)
392 ->builtin_long_double);
393 current_type
394 = parse_type (pstate)->builtin_long_double;
395 write_exp_elt_opcode (pstate, UNOP_CAST);
4ae0885a
PM
396 leftdiv_is_integer = 0;
397 }
398
410a0ff2 399 write_exp_elt_opcode (pstate, BINOP_DIV);
4ae0885a 400 }
373a8247
PM
401 ;
402
403exp : exp DIV exp
410a0ff2 404 { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
373a8247
PM
405 ;
406
407exp : exp MOD exp
410a0ff2 408 { write_exp_elt_opcode (pstate, BINOP_REM); }
373a8247
PM
409 ;
410
411exp : exp '+' exp
410a0ff2 412 { write_exp_elt_opcode (pstate, BINOP_ADD); }
373a8247
PM
413 ;
414
415exp : exp '-' exp
410a0ff2 416 { write_exp_elt_opcode (pstate, BINOP_SUB); }
373a8247
PM
417 ;
418
419exp : exp LSH exp
410a0ff2 420 { write_exp_elt_opcode (pstate, BINOP_LSH); }
373a8247
PM
421 ;
422
423exp : exp RSH exp
410a0ff2 424 { write_exp_elt_opcode (pstate, BINOP_RSH); }
373a8247
PM
425 ;
426
427exp : exp '=' exp
410a0ff2
SDJ
428 { write_exp_elt_opcode (pstate, BINOP_EQUAL);
429 current_type = parse_type (pstate)->builtin_bool;
4ae0885a 430 }
373a8247
PM
431 ;
432
433exp : exp NOTEQUAL exp
410a0ff2
SDJ
434 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL);
435 current_type = parse_type (pstate)->builtin_bool;
4ae0885a 436 }
373a8247
PM
437 ;
438
439exp : exp LEQ exp
410a0ff2
SDJ
440 { write_exp_elt_opcode (pstate, BINOP_LEQ);
441 current_type = parse_type (pstate)->builtin_bool;
4ae0885a 442 }
373a8247
PM
443 ;
444
445exp : exp GEQ exp
410a0ff2
SDJ
446 { write_exp_elt_opcode (pstate, BINOP_GEQ);
447 current_type = parse_type (pstate)->builtin_bool;
4ae0885a 448 }
373a8247
PM
449 ;
450
451exp : exp '<' exp
410a0ff2
SDJ
452 { write_exp_elt_opcode (pstate, BINOP_LESS);
453 current_type = parse_type (pstate)->builtin_bool;
4ae0885a 454 }
373a8247
PM
455 ;
456
457exp : exp '>' exp
410a0ff2
SDJ
458 { write_exp_elt_opcode (pstate, BINOP_GTR);
459 current_type = parse_type (pstate)->builtin_bool;
4ae0885a 460 }
373a8247
PM
461 ;
462
463exp : exp ANDAND exp
410a0ff2 464 { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
373a8247
PM
465 ;
466
467exp : exp XOR exp
410a0ff2 468 { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
373a8247
PM
469 ;
470
471exp : exp OR exp
410a0ff2 472 { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
373a8247
PM
473 ;
474
475exp : exp ASSIGN exp
410a0ff2 476 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
373a8247
PM
477 ;
478
2692ddb3 479exp : TRUEKEYWORD
410a0ff2
SDJ
480 { write_exp_elt_opcode (pstate, OP_BOOL);
481 write_exp_elt_longcst (pstate, (LONGEST) $1);
482 current_type = parse_type (pstate)->builtin_bool;
483 write_exp_elt_opcode (pstate, OP_BOOL); }
373a8247
PM
484 ;
485
2692ddb3 486exp : FALSEKEYWORD
410a0ff2
SDJ
487 { write_exp_elt_opcode (pstate, OP_BOOL);
488 write_exp_elt_longcst (pstate, (LONGEST) $1);
489 current_type = parse_type (pstate)->builtin_bool;
490 write_exp_elt_opcode (pstate, OP_BOOL); }
373a8247
PM
491 ;
492
493exp : INT
410a0ff2
SDJ
494 { write_exp_elt_opcode (pstate, OP_LONG);
495 write_exp_elt_type (pstate, $1.type);
4ae0885a 496 current_type = $1.type;
410a0ff2
SDJ
497 write_exp_elt_longcst (pstate, (LONGEST)($1.val));
498 write_exp_elt_opcode (pstate, OP_LONG); }
373a8247
PM
499 ;
500
501exp : NAME_OR_INT
502 { YYSTYPE val;
410a0ff2 503 parse_number (pstate, $1.stoken.ptr,
0df8b418 504 $1.stoken.length, 0, &val);
410a0ff2
SDJ
505 write_exp_elt_opcode (pstate, OP_LONG);
506 write_exp_elt_type (pstate, val.typed_val_int.type);
4ae0885a 507 current_type = val.typed_val_int.type;
410a0ff2 508 write_exp_elt_longcst (pstate, (LONGEST)
0df8b418 509 val.typed_val_int.val);
410a0ff2 510 write_exp_elt_opcode (pstate, OP_LONG);
373a8247
PM
511 }
512 ;
513
514
515exp : FLOAT
edd079d9 516 { write_exp_elt_opcode (pstate, OP_FLOAT);
410a0ff2 517 write_exp_elt_type (pstate, $1.type);
4ae0885a 518 current_type = $1.type;
edd079d9
UW
519 write_exp_elt_floatcst (pstate, $1.val);
520 write_exp_elt_opcode (pstate, OP_FLOAT); }
373a8247
PM
521 ;
522
523exp : variable
524 ;
525
cfeadda5 526exp : DOLLAR_VARIABLE
02c72701
TT
527 {
528 write_dollar_variable (pstate, $1);
529
530 /* $ is the normal prefix for pascal
531 hexadecimal values but this conflicts
532 with the GDB use for debugger variables
533 so in expression to enter hexadecimal
534 values we still need to use C syntax with
535 0xff */
536 std::string tmp ($1.ptr, $1.length);
537 /* Handle current_type. */
538 struct internalvar *intvar
539 = lookup_only_internalvar (tmp.c_str () + 1);
540 if (intvar != nullptr)
541 {
542 scoped_value_mark mark;
543
544 value *val
545 = value_of_internalvar (pstate->gdbarch (),
546 intvar);
547 current_type = value_type (val);
548 }
a5a44b53
PM
549 }
550 ;
373a8247
PM
551
552exp : SIZEOF '(' type ')' %prec UNARY
410a0ff2
SDJ
553 { write_exp_elt_opcode (pstate, OP_LONG);
554 write_exp_elt_type (pstate,
555 parse_type (pstate)->builtin_int);
556 current_type = parse_type (pstate)->builtin_int;
f168693b 557 $3 = check_typedef ($3);
410a0ff2
SDJ
558 write_exp_elt_longcst (pstate,
559 (LONGEST) TYPE_LENGTH ($3));
560 write_exp_elt_opcode (pstate, OP_LONG); }
373a8247
PM
561 ;
562
28e176a6 563exp : SIZEOF '(' exp ')' %prec UNARY
410a0ff2
SDJ
564 { write_exp_elt_opcode (pstate, UNOP_SIZEOF);
565 current_type = parse_type (pstate)->builtin_int; }
6ced1581 566
373a8247
PM
567exp : STRING
568 { /* C strings are converted into array constants with
569 an explicit null byte added at the end. Thus
570 the array upper bound is the string length.
571 There is no such thing in C as a completely empty
0df8b418 572 string. */
d7561cbb
KS
573 const char *sp = $1.ptr; int count = $1.length;
574
373a8247
PM
575 while (count-- > 0)
576 {
410a0ff2
SDJ
577 write_exp_elt_opcode (pstate, OP_LONG);
578 write_exp_elt_type (pstate,
579 parse_type (pstate)
580 ->builtin_char);
581 write_exp_elt_longcst (pstate,
582 (LONGEST) (*sp++));
583 write_exp_elt_opcode (pstate, OP_LONG);
373a8247 584 }
410a0ff2
SDJ
585 write_exp_elt_opcode (pstate, OP_LONG);
586 write_exp_elt_type (pstate,
587 parse_type (pstate)
588 ->builtin_char);
589 write_exp_elt_longcst (pstate, (LONGEST)'\0');
590 write_exp_elt_opcode (pstate, OP_LONG);
591 write_exp_elt_opcode (pstate, OP_ARRAY);
592 write_exp_elt_longcst (pstate, (LONGEST) 0);
593 write_exp_elt_longcst (pstate,
594 (LONGEST) ($1.length));
595 write_exp_elt_opcode (pstate, OP_ARRAY); }
373a8247
PM
596 ;
597
598/* Object pascal */
599exp : THIS
6ced1581 600 {
fd0e9d45
PM
601 struct value * this_val;
602 struct type * this_type;
410a0ff2
SDJ
603 write_exp_elt_opcode (pstate, OP_THIS);
604 write_exp_elt_opcode (pstate, OP_THIS);
0df8b418 605 /* We need type of this. */
410a0ff2 606 this_val
73923d7e 607 = value_of_this_silent (pstate->language ());
fd0e9d45 608 if (this_val)
04624583 609 this_type = value_type (this_val);
fd0e9d45
PM
610 else
611 this_type = NULL;
612 if (this_type)
613 {
78134374 614 if (this_type->code () == TYPE_CODE_PTR)
fd0e9d45
PM
615 {
616 this_type = TYPE_TARGET_TYPE (this_type);
410a0ff2 617 write_exp_elt_opcode (pstate, UNOP_IND);
fd0e9d45
PM
618 }
619 }
6ced1581 620
fd0e9d45
PM
621 current_type = this_type;
622 }
373a8247
PM
623 ;
624
625/* end of object pascal. */
626
627block : BLOCKNAME
628 {
d12307c1
PMR
629 if ($1.sym.symbol != 0)
630 $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
373a8247
PM
631 else
632 {
61f4b350 633 std::string copy = copy_name ($1.stoken);
373a8247 634 struct symtab *tem =
61f4b350 635 lookup_symtab (copy.c_str ());
373a8247 636 if (tem)
439247b6 637 $$ = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (tem),
0df8b418 638 STATIC_BLOCK);
373a8247 639 else
001083c6 640 error (_("No file or function \"%s\"."),
61f4b350 641 copy.c_str ());
373a8247
PM
642 }
643 }
644 ;
645
646block : block COLONCOLON name
61f4b350
TT
647 {
648 std::string copy = copy_name ($3);
649 struct symbol *tem
650 = lookup_symbol (copy.c_str (), $1,
d12307c1
PMR
651 VAR_DOMAIN, NULL).symbol;
652
373a8247 653 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
001083c6 654 error (_("No function \"%s\" in specified context."),
61f4b350 655 copy.c_str ());
373a8247
PM
656 $$ = SYMBOL_BLOCK_VALUE (tem); }
657 ;
658
659variable: block COLONCOLON name
d12307c1
PMR
660 { struct block_symbol sym;
661
61f4b350
TT
662 std::string copy = copy_name ($3);
663 sym = lookup_symbol (copy.c_str (), $1,
1993b719 664 VAR_DOMAIN, NULL);
d12307c1 665 if (sym.symbol == 0)
001083c6 666 error (_("No symbol \"%s\" in specified context."),
61f4b350 667 copy.c_str ());
373a8247 668
410a0ff2 669 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
d12307c1
PMR
670 write_exp_elt_block (pstate, sym.block);
671 write_exp_elt_sym (pstate, sym.symbol);
410a0ff2 672 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
373a8247
PM
673 ;
674
675qualified_name: typebase COLONCOLON name
676 {
677 struct type *type = $1;
d12307c1 678
78134374
SM
679 if (type->code () != TYPE_CODE_STRUCT
680 && type->code () != TYPE_CODE_UNION)
001083c6 681 error (_("`%s' is not defined as an aggregate type."),
7d93a1e0 682 type->name ());
373a8247 683
410a0ff2
SDJ
684 write_exp_elt_opcode (pstate, OP_SCOPE);
685 write_exp_elt_type (pstate, type);
686 write_exp_string (pstate, $3);
687 write_exp_elt_opcode (pstate, OP_SCOPE);
373a8247
PM
688 }
689 ;
690
691variable: qualified_name
692 | COLONCOLON name
693 {
61f4b350 694 std::string name = copy_name ($2);
373a8247 695 struct symbol *sym;
7c7b6655 696 struct bound_minimal_symbol msymbol;
373a8247
PM
697
698 sym =
61f4b350
TT
699 lookup_symbol (name.c_str (),
700 (const struct block *) NULL,
d12307c1 701 VAR_DOMAIN, NULL).symbol;
373a8247
PM
702 if (sym)
703 {
410a0ff2
SDJ
704 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
705 write_exp_elt_block (pstate, NULL);
706 write_exp_elt_sym (pstate, sym);
707 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
373a8247
PM
708 break;
709 }
710
61f4b350
TT
711 msymbol
712 = lookup_bound_minimal_symbol (name.c_str ());
7c7b6655 713 if (msymbol.minsym != NULL)
410a0ff2 714 write_exp_msymbol (pstate, msymbol);
0df8b418
MS
715 else if (!have_full_symbols ()
716 && !have_partial_symbols ())
001083c6
PM
717 error (_("No symbol table is loaded. "
718 "Use the \"file\" command."));
373a8247 719 else
001083c6 720 error (_("No symbol \"%s\" in current context."),
61f4b350 721 name.c_str ());
373a8247
PM
722 }
723 ;
724
725variable: name_not_typename
d12307c1 726 { struct block_symbol sym = $1.sym;
373a8247 727
d12307c1 728 if (sym.symbol)
373a8247 729 {
d12307c1 730 if (symbol_read_needs_frame (sym.symbol))
699bd4cf 731 pstate->block_tracker->update (sym);
373a8247 732
410a0ff2 733 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
63e43d3a 734 write_exp_elt_block (pstate, sym.block);
d12307c1 735 write_exp_elt_sym (pstate, sym.symbol);
410a0ff2 736 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
d12307c1 737 current_type = sym.symbol->type; }
373a8247
PM
738 else if ($1.is_a_field_of_this)
739 {
9819c6c8
PM
740 struct value * this_val;
741 struct type * this_type;
373a8247 742 /* Object pascal: it hangs off of `this'. Must
dda83cd7 743 not inadvertently convert from a method call
373a8247 744 to data ref. */
699bd4cf 745 pstate->block_tracker->update (sym);
410a0ff2
SDJ
746 write_exp_elt_opcode (pstate, OP_THIS);
747 write_exp_elt_opcode (pstate, OP_THIS);
748 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
749 write_exp_string (pstate, $1.stoken);
750 write_exp_elt_opcode (pstate, STRUCTOP_PTR);
0df8b418 751 /* We need type of this. */
410a0ff2 752 this_val
73923d7e 753 = value_of_this_silent (pstate->language ());
9819c6c8 754 if (this_val)
04624583 755 this_type = value_type (this_val);
9819c6c8
PM
756 else
757 this_type = NULL;
758 if (this_type)
759 current_type = lookup_struct_elt_type (
760 this_type,
61f4b350 761 copy_name ($1.stoken).c_str (), 0);
9819c6c8 762 else
6ced1581 763 current_type = NULL;
373a8247
PM
764 }
765 else
766 {
7c7b6655 767 struct bound_minimal_symbol msymbol;
61f4b350 768 std::string arg = copy_name ($1.stoken);
373a8247
PM
769
770 msymbol =
61f4b350 771 lookup_bound_minimal_symbol (arg.c_str ());
7c7b6655 772 if (msymbol.minsym != NULL)
410a0ff2 773 write_exp_msymbol (pstate, msymbol);
0df8b418
MS
774 else if (!have_full_symbols ()
775 && !have_partial_symbols ())
001083c6
PM
776 error (_("No symbol table is loaded. "
777 "Use the \"file\" command."));
373a8247 778 else
001083c6 779 error (_("No symbol \"%s\" in current context."),
61f4b350 780 arg.c_str ());
373a8247
PM
781 }
782 }
783 ;
784
785
786ptype : typebase
787 ;
788
789/* We used to try to recognize more pointer to member types here, but
790 that didn't work (shift/reduce conflicts meant that these rules never
791 got executed). The problem is that
792 int (foo::bar::baz::bizzle)
793 is a function type but
794 int (foo::bar::baz::bizzle::*)
795 is a pointer to member type. Stroustrup loses again! */
796
797type : ptype
373a8247
PM
798 ;
799
800typebase /* Implements (approximately): (type-qualifier)* type-specifier */
fd0e9d45
PM
801 : '^' typebase
802 { $$ = lookup_pointer_type ($2); }
803 | TYPENAME
373a8247
PM
804 { $$ = $1.type; }
805 | STRUCT name
1e58a4a4 806 { $$
61f4b350 807 = lookup_struct (copy_name ($2).c_str (),
1e58a4a4
TT
808 pstate->expression_context_block);
809 }
373a8247 810 | CLASS name
1e58a4a4 811 { $$
61f4b350 812 = lookup_struct (copy_name ($2).c_str (),
1e58a4a4
TT
813 pstate->expression_context_block);
814 }
373a8247
PM
815 /* "const" and "volatile" are curently ignored. A type qualifier
816 after the type is handled in the ptype rule. I think these could
817 be too. */
818 ;
819
820name : NAME { $$ = $1.stoken; }
821 | BLOCKNAME { $$ = $1.stoken; }
822 | TYPENAME { $$ = $1.stoken; }
823 | NAME_OR_INT { $$ = $1.stoken; }
824 ;
825
826name_not_typename : NAME
827 | BLOCKNAME
828/* These would be useful if name_not_typename was useful, but it is just
829 a fake for "variable", so these cause reduce/reduce conflicts because
830 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
831 =exp) or just an exp. If name_not_typename was ever used in an lvalue
832 context where only a name could occur, this might be useful.
833 | NAME_OR_INT
834 */
835 ;
836
837%%
838
839/* Take care of parsing a number (anything that starts with a digit).
840 Set yylval and return the token type; update lexptr.
841 LEN is the number of characters in it. */
842
843/*** Needs some error checking for the float case ***/
844
845static int
410a0ff2
SDJ
846parse_number (struct parser_state *par_state,
847 const char *p, int len, int parsed_float, YYSTYPE *putithere)
373a8247
PM
848{
849 /* FIXME: Shouldn't these be unsigned? We don't deal with negative values
850 here, and we do kind of silly things like cast to unsigned. */
710122da
DC
851 LONGEST n = 0;
852 LONGEST prevn = 0;
373a8247
PM
853 ULONGEST un;
854
710122da
DC
855 int i = 0;
856 int c;
857 int base = input_radix;
373a8247
PM
858 int unsigned_p = 0;
859
860 /* Number of "L" suffixes encountered. */
861 int long_p = 0;
862
863 /* We have found a "L" or "U" suffix. */
864 int found_suffix = 0;
865
866 ULONGEST high_bit;
867 struct type *signed_type;
868 struct type *unsigned_type;
869
870 if (parsed_float)
871 {
edd079d9 872 /* Handle suffixes: 'f' for float, 'l' for long double.
dda83cd7 873 FIXME: This appears to be an extension -- do we want this? */
edd079d9
UW
874 if (len >= 1 && tolower (p[len - 1]) == 'f')
875 {
876 putithere->typed_val_float.type
877 = parse_type (par_state)->builtin_float;
878 len--;
879 }
880 else if (len >= 1 && tolower (p[len - 1]) == 'l')
881 {
882 putithere->typed_val_float.type
883 = parse_type (par_state)->builtin_long_double;
884 len--;
885 }
886 /* Default type for floating-point literals is double. */
887 else
888 {
889 putithere->typed_val_float.type
890 = parse_type (par_state)->builtin_double;
891 }
892
893 if (!parse_float (p, len,
894 putithere->typed_val_float.type,
895 putithere->typed_val_float.val))
373a8247 896 return ERROR;
373a8247
PM
897 return FLOAT;
898 }
899
0df8b418 900 /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
373a8247
PM
901 if (p[0] == '0')
902 switch (p[1])
903 {
904 case 'x':
905 case 'X':
906 if (len >= 3)
907 {
908 p += 2;
909 base = 16;
910 len -= 2;
911 }
912 break;
913
914 case 't':
915 case 'T':
916 case 'd':
917 case 'D':
918 if (len >= 3)
919 {
920 p += 2;
921 base = 10;
922 len -= 2;
923 }
924 break;
925
926 default:
927 base = 8;
928 break;
929 }
930
931 while (len-- > 0)
932 {
933 c = *p++;
934 if (c >= 'A' && c <= 'Z')
935 c += 'a' - 'A';
936 if (c != 'l' && c != 'u')
937 n *= base;
938 if (c >= '0' && c <= '9')
939 {
940 if (found_suffix)
941 return ERROR;
942 n += i = c - '0';
943 }
944 else
945 {
946 if (base > 10 && c >= 'a' && c <= 'f')
947 {
948 if (found_suffix)
949 return ERROR;
950 n += i = c - 'a' + 10;
951 }
952 else if (c == 'l')
953 {
954 ++long_p;
955 found_suffix = 1;
956 }
957 else if (c == 'u')
958 {
959 unsigned_p = 1;
960 found_suffix = 1;
961 }
962 else
963 return ERROR; /* Char not a digit */
964 }
965 if (i >= base)
0df8b418 966 return ERROR; /* Invalid digit in this base. */
373a8247
PM
967
968 /* Portably test for overflow (only works for nonzero values, so make
969 a second check for zero). FIXME: Can't we just make n and prevn
970 unsigned and avoid this? */
971 if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
0df8b418 972 unsigned_p = 1; /* Try something unsigned. */
373a8247
PM
973
974 /* Portably test for unsigned overflow.
975 FIXME: This check is wrong; for example it doesn't find overflow
976 on 0x123456789 when LONGEST is 32 bits. */
977 if (c != 'l' && c != 'u' && n != 0)
6ced1581 978 {
373a8247 979 if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
001083c6 980 error (_("Numeric constant too large."));
373a8247
PM
981 }
982 prevn = n;
983 }
984
985 /* An integer constant is an int, a long, or a long long. An L
986 suffix forces it to be long; an LL suffix forces it to be long
987 long. If not forced to a larger size, it gets the first type of
988 the above that it fits in. To figure out whether it fits, we
989 shift it right and see whether anything remains. Note that we
990 can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
991 operation, because many compilers will warn about such a shift
9a76efb6
UW
992 (which always produces a zero result). Sometimes gdbarch_int_bit
993 or gdbarch_long_bit will be that big, sometimes not. To deal with
373a8247
PM
994 the case where it is we just always shift the value more than
995 once, with fewer bits each time. */
996
997 un = (ULONGEST)n >> 2;
998 if (long_p == 0
fa9f5be6 999 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
373a8247 1000 {
410a0ff2 1001 high_bit
fa9f5be6 1002 = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
373a8247
PM
1003
1004 /* A large decimal (not hex or octal) constant (between INT_MAX
1005 and UINT_MAX) is a long or unsigned long, according to ANSI,
1006 never an unsigned int, but this code treats it as unsigned
1007 int. This probably should be fixed. GCC gives a warning on
1008 such constants. */
1009
410a0ff2
SDJ
1010 unsigned_type = parse_type (par_state)->builtin_unsigned_int;
1011 signed_type = parse_type (par_state)->builtin_int;
373a8247
PM
1012 }
1013 else if (long_p <= 1
fa9f5be6 1014 && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
373a8247 1015 {
410a0ff2 1016 high_bit
fa9f5be6 1017 = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
410a0ff2
SDJ
1018 unsigned_type = parse_type (par_state)->builtin_unsigned_long;
1019 signed_type = parse_type (par_state)->builtin_long;
373a8247
PM
1020 }
1021 else
1022 {
7451d027 1023 int shift;
9a76efb6 1024 if (sizeof (ULONGEST) * HOST_CHAR_BIT
fa9f5be6 1025 < gdbarch_long_long_bit (par_state->gdbarch ()))
373a8247 1026 /* A long long does not fit in a LONGEST. */
7451d027
AC
1027 shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1028 else
fa9f5be6 1029 shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
7451d027 1030 high_bit = (ULONGEST) 1 << shift;
410a0ff2
SDJ
1031 unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
1032 signed_type = parse_type (par_state)->builtin_long_long;
373a8247
PM
1033 }
1034
1035 putithere->typed_val_int.val = n;
1036
1037 /* If the high bit of the worked out type is set then this number
0df8b418 1038 has to be unsigned. */
373a8247
PM
1039
1040 if (unsigned_p || (n & high_bit))
1041 {
1042 putithere->typed_val_int.type = unsigned_type;
1043 }
1044 else
1045 {
1046 putithere->typed_val_int.type = signed_type;
1047 }
1048
1049 return INT;
1050}
1051
9819c6c8
PM
1052
1053struct type_push
1054{
1055 struct type *stored;
1056 struct type_push *next;
1057};
1058
1059static struct type_push *tp_top = NULL;
1060
b9362cc7
AC
1061static void
1062push_current_type (void)
9819c6c8
PM
1063{
1064 struct type_push *tpnew;
1065 tpnew = (struct type_push *) malloc (sizeof (struct type_push));
1066 tpnew->next = tp_top;
1067 tpnew->stored = current_type;
1068 current_type = NULL;
6ced1581 1069 tp_top = tpnew;
9819c6c8
PM
1070}
1071
b9362cc7
AC
1072static void
1073pop_current_type (void)
9819c6c8
PM
1074{
1075 struct type_push *tp = tp_top;
1076 if (tp)
1077 {
1078 current_type = tp->stored;
1079 tp_top = tp->next;
bbe2ba60 1080 free (tp);
9819c6c8
PM
1081 }
1082}
1083
373a8247
PM
1084struct token
1085{
a121b7c1 1086 const char *oper;
373a8247
PM
1087 int token;
1088 enum exp_opcode opcode;
1089};
1090
1091static const struct token tokentab3[] =
1092 {
1093 {"shr", RSH, BINOP_END},
1094 {"shl", LSH, BINOP_END},
1095 {"and", ANDAND, BINOP_END},
1096 {"div", DIV, BINOP_END},
1097 {"not", NOT, BINOP_END},
1098 {"mod", MOD, BINOP_END},
1099 {"inc", INCREMENT, BINOP_END},
1100 {"dec", DECREMENT, BINOP_END},
1101 {"xor", XOR, BINOP_END}
1102 };
1103
1104static const struct token tokentab2[] =
1105 {
1106 {"or", OR, BINOP_END},
1107 {"<>", NOTEQUAL, BINOP_END},
1108 {"<=", LEQ, BINOP_END},
1109 {">=", GEQ, BINOP_END},
9819c6c8
PM
1110 {":=", ASSIGN, BINOP_END},
1111 {"::", COLONCOLON, BINOP_END} };
373a8247 1112
0df8b418
MS
1113/* Allocate uppercased var: */
1114/* make an uppercased copy of tokstart. */
d04550a6 1115static char *
793156e6 1116uptok (const char *tokstart, int namelen)
373a8247
PM
1117{
1118 int i;
1119 char *uptokstart = (char *)malloc(namelen+1);
1120 for (i = 0;i <= namelen;i++)
1121 {
1122 if ((tokstart[i]>='a' && tokstart[i]<='z'))
dda83cd7 1123 uptokstart[i] = tokstart[i]-('a'-'A');
373a8247 1124 else
dda83cd7 1125 uptokstart[i] = tokstart[i];
373a8247
PM
1126 }
1127 uptokstart[namelen]='\0';
1128 return uptokstart;
1129}
373a8247 1130
a5a44b53 1131/* Read one token, getting characters through lexptr. */
373a8247
PM
1132
1133static int
eeae04df 1134yylex (void)
373a8247
PM
1135{
1136 int c;
1137 int namelen;
793156e6 1138 const char *tokstart;
373a8247 1139 char *uptokstart;
793156e6 1140 const char *tokptr;
d3d6d173 1141 int explen, tempbufindex;
373a8247
PM
1142 static char *tempbuf;
1143 static int tempbufsize;
6ced1581 1144
373a8247
PM
1145 retry:
1146
5776fca3 1147 pstate->prev_lexptr = pstate->lexptr;
24467a86 1148
5776fca3
TT
1149 tokstart = pstate->lexptr;
1150 explen = strlen (pstate->lexptr);
d7561cbb 1151
373a8247 1152 /* See if it is a special token of length 3. */
d3d6d173 1153 if (explen > 2)
b926417a 1154 for (int i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
fe978cb0 1155 if (strncasecmp (tokstart, tokentab3[i].oper, 3) == 0
dda83cd7
SM
1156 && (!isalpha (tokentab3[i].oper[0]) || explen == 3
1157 || (!isalpha (tokstart[3])
0df8b418 1158 && !isdigit (tokstart[3]) && tokstart[3] != '_')))
dda83cd7
SM
1159 {
1160 pstate->lexptr += 3;
1161 yylval.opcode = tokentab3[i].opcode;
1162 return tokentab3[i].token;
1163 }
373a8247
PM
1164
1165 /* See if it is a special token of length 2. */
d3d6d173 1166 if (explen > 1)
b926417a 1167 for (int i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
fe978cb0 1168 if (strncasecmp (tokstart, tokentab2[i].oper, 2) == 0
dda83cd7
SM
1169 && (!isalpha (tokentab2[i].oper[0]) || explen == 2
1170 || (!isalpha (tokstart[2])
0df8b418 1171 && !isdigit (tokstart[2]) && tokstart[2] != '_')))
dda83cd7
SM
1172 {
1173 pstate->lexptr += 2;
1174 yylval.opcode = tokentab2[i].opcode;
1175 return tokentab2[i].token;
1176 }
373a8247
PM
1177
1178 switch (c = *tokstart)
1179 {
1180 case 0:
2a612529 1181 if (search_field && pstate->parse_completion)
a5a44b53
PM
1182 return COMPLETE;
1183 else
1184 return 0;
373a8247
PM
1185
1186 case ' ':
1187 case '\t':
1188 case '\n':
5776fca3 1189 pstate->lexptr++;
373a8247
PM
1190 goto retry;
1191
1192 case '\'':
1193 /* We either have a character constant ('0' or '\177' for example)
1194 or we have a quoted symbol reference ('foo(int,int)' in object pascal
0df8b418 1195 for example). */
5776fca3
TT
1196 pstate->lexptr++;
1197 c = *pstate->lexptr++;
373a8247 1198 if (c == '\\')
5776fca3 1199 c = parse_escape (pstate->gdbarch (), &pstate->lexptr);
373a8247 1200 else if (c == '\'')
001083c6 1201 error (_("Empty character constant."));
373a8247
PM
1202
1203 yylval.typed_val_int.val = c;
410a0ff2 1204 yylval.typed_val_int.type = parse_type (pstate)->builtin_char;
373a8247 1205
5776fca3 1206 c = *pstate->lexptr++;
373a8247
PM
1207 if (c != '\'')
1208 {
1209 namelen = skip_quoted (tokstart) - tokstart;
1210 if (namelen > 2)
1211 {
5776fca3
TT
1212 pstate->lexptr = tokstart + namelen;
1213 if (pstate->lexptr[-1] != '\'')
001083c6 1214 error (_("Unmatched single quote."));
373a8247 1215 namelen -= 2;
dda83cd7
SM
1216 tokstart++;
1217 uptokstart = uptok(tokstart,namelen);
373a8247
PM
1218 goto tryname;
1219 }
001083c6 1220 error (_("Invalid character constant."));
373a8247
PM
1221 }
1222 return INT;
1223
1224 case '(':
1225 paren_depth++;
5776fca3 1226 pstate->lexptr++;
373a8247
PM
1227 return c;
1228
1229 case ')':
1230 if (paren_depth == 0)
1231 return 0;
1232 paren_depth--;
5776fca3 1233 pstate->lexptr++;
373a8247
PM
1234 return c;
1235
1236 case ',':
8621b685 1237 if (pstate->comma_terminates && paren_depth == 0)
373a8247 1238 return 0;
5776fca3 1239 pstate->lexptr++;
373a8247
PM
1240 return c;
1241
1242 case '.':
1243 /* Might be a floating point number. */
5776fca3 1244 if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
a5a44b53 1245 {
a5a44b53
PM
1246 goto symbol; /* Nope, must be a symbol. */
1247 }
1248
86a73007 1249 /* FALL THRU. */
373a8247
PM
1250
1251 case '0':
1252 case '1':
1253 case '2':
1254 case '3':
1255 case '4':
1256 case '5':
1257 case '6':
1258 case '7':
1259 case '8':
1260 case '9':
1261 {
1262 /* It's a number. */
1263 int got_dot = 0, got_e = 0, toktype;
793156e6 1264 const char *p = tokstart;
373a8247
PM
1265 int hex = input_radix > 10;
1266
1267 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1268 {
1269 p += 2;
1270 hex = 1;
1271 }
0df8b418
MS
1272 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1273 || p[1]=='d' || p[1]=='D'))
373a8247
PM
1274 {
1275 p += 2;
1276 hex = 0;
1277 }
1278
1279 for (;; ++p)
1280 {
1281 /* This test includes !hex because 'e' is a valid hex digit
1282 and thus does not indicate a floating point number when
1283 the radix is hex. */
1284 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1285 got_dot = got_e = 1;
1286 /* This test does not include !hex, because a '.' always indicates
1287 a decimal floating point number regardless of the radix. */
1288 else if (!got_dot && *p == '.')
1289 got_dot = 1;
1290 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1291 && (*p == '-' || *p == '+'))
1292 /* This is the sign of the exponent, not the end of the
1293 number. */
1294 continue;
1295 /* We will take any letters or digits. parse_number will
1296 complain if past the radix, or if L or U are not final. */
1297 else if ((*p < '0' || *p > '9')
1298 && ((*p < 'a' || *p > 'z')
1299 && (*p < 'A' || *p > 'Z')))
1300 break;
1301 }
410a0ff2 1302 toktype = parse_number (pstate, tokstart,
0df8b418 1303 p - tokstart, got_dot | got_e, &yylval);
dda83cd7 1304 if (toktype == ERROR)
373a8247
PM
1305 {
1306 char *err_copy = (char *) alloca (p - tokstart + 1);
1307
1308 memcpy (err_copy, tokstart, p - tokstart);
1309 err_copy[p - tokstart] = 0;
001083c6 1310 error (_("Invalid number \"%s\"."), err_copy);
373a8247 1311 }
5776fca3 1312 pstate->lexptr = p;
373a8247
PM
1313 return toktype;
1314 }
1315
1316 case '+':
1317 case '-':
1318 case '*':
1319 case '/':
1320 case '|':
1321 case '&':
1322 case '^':
1323 case '~':
1324 case '!':
1325 case '@':
1326 case '<':
1327 case '>':
1328 case '[':
1329 case ']':
1330 case '?':
1331 case ':':
1332 case '=':
1333 case '{':
1334 case '}':
1335 symbol:
5776fca3 1336 pstate->lexptr++;
373a8247
PM
1337 return c;
1338
1339 case '"':
1340
1341 /* Build the gdb internal form of the input string in tempbuf,
1342 translating any standard C escape forms seen. Note that the
1343 buffer is null byte terminated *only* for the convenience of
1344 debugging gdb itself and printing the buffer contents when
1345 the buffer contains no embedded nulls. Gdb does not depend
1346 upon the buffer being null byte terminated, it uses the length
1347 string instead. This allows gdb to handle C strings (as well
0df8b418 1348 as strings in other languages) with embedded null bytes. */
373a8247
PM
1349
1350 tokptr = ++tokstart;
1351 tempbufindex = 0;
1352
1353 do {
1354 /* Grow the static temp buffer if necessary, including allocating
0df8b418 1355 the first one on demand. */
373a8247
PM
1356 if (tempbufindex + 1 >= tempbufsize)
1357 {
1358 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1359 }
9819c6c8 1360
373a8247
PM
1361 switch (*tokptr)
1362 {
1363 case '\0':
1364 case '"':
0df8b418 1365 /* Do nothing, loop will terminate. */
373a8247
PM
1366 break;
1367 case '\\':
793156e6 1368 ++tokptr;
fa9f5be6 1369 c = parse_escape (pstate->gdbarch (), &tokptr);
793156e6
KS
1370 if (c == -1)
1371 {
1372 continue;
1373 }
1374 tempbuf[tempbufindex++] = c;
373a8247
PM
1375 break;
1376 default:
1377 tempbuf[tempbufindex++] = *tokptr++;
1378 break;
1379 }
1380 } while ((*tokptr != '"') && (*tokptr != '\0'));
1381 if (*tokptr++ != '"')
1382 {
001083c6 1383 error (_("Unterminated string in expression."));
373a8247 1384 }
0df8b418 1385 tempbuf[tempbufindex] = '\0'; /* See note above. */
373a8247
PM
1386 yylval.sval.ptr = tempbuf;
1387 yylval.sval.length = tempbufindex;
5776fca3 1388 pstate->lexptr = tokptr;
373a8247
PM
1389 return (STRING);
1390 }
1391
1392 if (!(c == '_' || c == '$'
1393 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1394 /* We must have come across a bad character (e.g. ';'). */
001083c6 1395 error (_("Invalid character '%c' in expression."), c);
373a8247
PM
1396
1397 /* It's a name. See how long it is. */
1398 namelen = 0;
1399 for (c = tokstart[namelen];
1400 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1401 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1402 {
1403 /* Template parameter lists are part of the name.
1404 FIXME: This mishandles `print $a<4&&$a>3'. */
1405 if (c == '<')
1406 {
1407 int i = namelen;
1408 int nesting_level = 1;
1409 while (tokstart[++i])
1410 {
1411 if (tokstart[i] == '<')
1412 nesting_level++;
1413 else if (tokstart[i] == '>')
1414 {
1415 if (--nesting_level == 0)
1416 break;
1417 }
1418 }
1419 if (tokstart[i] == '>')
1420 namelen = i;
1421 else
1422 break;
1423 }
1424
0df8b418 1425 /* do NOT uppercase internals because of registers !!! */
373a8247
PM
1426 c = tokstart[++namelen];
1427 }
1428
1429 uptokstart = uptok(tokstart,namelen);
1430
1431 /* The token "if" terminates the expression and is NOT
1432 removed from the input stream. */
1433 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1434 {
7877e977 1435 free (uptokstart);
373a8247
PM
1436 return 0;
1437 }
1438
5776fca3 1439 pstate->lexptr += namelen;
373a8247
PM
1440
1441 tryname:
1442
1443 /* Catch specific keywords. Should be done with a data structure. */
1444 switch (namelen)
1445 {
1446 case 6:
0b058123 1447 if (strcmp (uptokstart, "OBJECT") == 0)
7877e977
MS
1448 {
1449 free (uptokstart);
1450 return CLASS;
1451 }
0b058123 1452 if (strcmp (uptokstart, "RECORD") == 0)
7877e977
MS
1453 {
1454 free (uptokstart);
1455 return STRUCT;
1456 }
0b058123 1457 if (strcmp (uptokstart, "SIZEOF") == 0)
7877e977
MS
1458 {
1459 free (uptokstart);
1460 return SIZEOF;
1461 }
373a8247
PM
1462 break;
1463 case 5:
0b058123 1464 if (strcmp (uptokstart, "CLASS") == 0)
7877e977
MS
1465 {
1466 free (uptokstart);
1467 return CLASS;
1468 }
0b058123 1469 if (strcmp (uptokstart, "FALSE") == 0)
373a8247 1470 {
dda83cd7 1471 yylval.lval = 0;
7877e977 1472 free (uptokstart);
dda83cd7
SM
1473 return FALSEKEYWORD;
1474 }
373a8247
PM
1475 break;
1476 case 4:
0b058123 1477 if (strcmp (uptokstart, "TRUE") == 0)
373a8247 1478 {
dda83cd7 1479 yylval.lval = 1;
7877e977 1480 free (uptokstart);
2692ddb3 1481 return TRUEKEYWORD;
dda83cd7 1482 }
0b058123 1483 if (strcmp (uptokstart, "SELF") == 0)
dda83cd7
SM
1484 {
1485 /* Here we search for 'this' like
1486 inserted in FPC stabs debug info. */
8343f86c 1487 static const char this_name[] = "this";
373a8247 1488
1e58a4a4 1489 if (lookup_symbol (this_name, pstate->expression_context_block,
d12307c1 1490 VAR_DOMAIN, NULL).symbol)
7877e977
MS
1491 {
1492 free (uptokstart);
1493 return THIS;
1494 }
373a8247
PM
1495 }
1496 break;
1497 default:
1498 break;
1499 }
1500
1501 yylval.sval.ptr = tokstart;
1502 yylval.sval.length = namelen;
1503
1504 if (*tokstart == '$')
1505 {
7877e977 1506 free (uptokstart);
cfeadda5 1507 return DOLLAR_VARIABLE;
373a8247
PM
1508 }
1509
1510 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1511 functions or symtabs. If this is not so, then ...
1512 Use token-type TYPENAME for symbols that happen to be defined
1513 currently as names of types; NAME for other symbols.
1514 The caller is not constrained to care about the distinction. */
1515 {
61f4b350 1516 std::string tmp = copy_name (yylval.sval);
373a8247 1517 struct symbol *sym;
1993b719 1518 struct field_of_this_result is_a_field_of_this;
9819c6c8 1519 int is_a_field = 0;
373a8247
PM
1520 int hextype;
1521
8aae4344 1522 is_a_field_of_this.type = NULL;
9819c6c8 1523 if (search_field && current_type)
61f4b350
TT
1524 is_a_field = (lookup_struct_elt_type (current_type,
1525 tmp.c_str (), 1) != NULL);
8662d513 1526 if (is_a_field)
9819c6c8
PM
1527 sym = NULL;
1528 else
61f4b350 1529 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
d12307c1 1530 VAR_DOMAIN, &is_a_field_of_this).symbol;
94a716bf 1531 /* second chance uppercased (as Free Pascal does). */
1993b719 1532 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
373a8247 1533 {
b926417a 1534 for (int i = 0; i <= namelen; i++)
dda83cd7
SM
1535 {
1536 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1537 tmp[i] -= ('a'-'A');
1538 }
9819c6c8 1539 if (search_field && current_type)
61f4b350
TT
1540 is_a_field = (lookup_struct_elt_type (current_type,
1541 tmp.c_str (), 1) != NULL);
8662d513 1542 if (is_a_field)
9819c6c8
PM
1543 sym = NULL;
1544 else
61f4b350 1545 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
d12307c1 1546 VAR_DOMAIN, &is_a_field_of_this).symbol;
94a716bf
PM
1547 }
1548 /* Third chance Capitalized (as GPC does). */
1993b719 1549 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
94a716bf 1550 {
b926417a 1551 for (int i = 0; i <= namelen; i++)
dda83cd7
SM
1552 {
1553 if (i == 0)
1554 {
1555 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1556 tmp[i] -= ('a'-'A');
1557 }
1558 else
1559 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1560 tmp[i] -= ('A'-'a');
1561 }
9819c6c8 1562 if (search_field && current_type)
61f4b350
TT
1563 is_a_field = (lookup_struct_elt_type (current_type,
1564 tmp.c_str (), 1) != NULL);
8662d513 1565 if (is_a_field)
9819c6c8
PM
1566 sym = NULL;
1567 else
61f4b350 1568 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
d12307c1 1569 VAR_DOMAIN, &is_a_field_of_this).symbol;
373a8247 1570 }
9819c6c8 1571
8aae4344 1572 if (is_a_field || (is_a_field_of_this.type != NULL))
9819c6c8
PM
1573 {
1574 tempbuf = (char *) realloc (tempbuf, namelen + 1);
61f4b350 1575 strncpy (tempbuf, tmp.c_str (), namelen);
793156e6 1576 tempbuf [namelen] = 0;
9819c6c8 1577 yylval.sval.ptr = tempbuf;
6ced1581 1578 yylval.sval.length = namelen;
d12307c1
PMR
1579 yylval.ssym.sym.symbol = NULL;
1580 yylval.ssym.sym.block = NULL;
7877e977 1581 free (uptokstart);
dda83cd7 1582 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
8aae4344
PM
1583 if (is_a_field)
1584 return FIELDNAME;
1585 else
1586 return NAME;
6ced1581 1587 }
373a8247
PM
1588 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1589 no psymtabs (coff, xcoff, or some future change to blow away the
1590 psymtabs once once symbols are read). */
0b058123 1591 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
dda83cd7 1592 || lookup_symtab (tmp.c_str ()))
373a8247 1593 {
d12307c1
PMR
1594 yylval.ssym.sym.symbol = sym;
1595 yylval.ssym.sym.block = NULL;
1993b719 1596 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
7877e977 1597 free (uptokstart);
373a8247
PM
1598 return BLOCKNAME;
1599 }
1600 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
dda83cd7 1601 {
373a8247
PM
1602#if 1
1603 /* Despite the following flaw, we need to keep this code enabled.
1604 Because we can get called from check_stub_method, if we don't
1605 handle nested types then it screws many operations in any
1606 program which uses nested types. */
1607 /* In "A::x", if x is a member function of A and there happens
1608 to be a type (nested or not, since the stabs don't make that
1609 distinction) named x, then this code incorrectly thinks we
1610 are dealing with nested types rather than a member function. */
1611
d7561cbb
KS
1612 const char *p;
1613 const char *namestart;
373a8247
PM
1614 struct symbol *best_sym;
1615
1616 /* Look ahead to detect nested types. This probably should be
1617 done in the grammar, but trying seemed to introduce a lot
1618 of shift/reduce and reduce/reduce conflicts. It's possible
1619 that it could be done, though. Or perhaps a non-grammar, but
1620 less ad hoc, approach would work well. */
1621
1622 /* Since we do not currently have any way of distinguishing
1623 a nested type from a non-nested one (the stabs don't tell
1624 us whether a type is nested), we just ignore the
1625 containing type. */
1626
5776fca3 1627 p = pstate->lexptr;
373a8247
PM
1628 best_sym = sym;
1629 while (1)
1630 {
1631 /* Skip whitespace. */
1632 while (*p == ' ' || *p == '\t' || *p == '\n')
1633 ++p;
1634 if (*p == ':' && p[1] == ':')
1635 {
1636 /* Skip the `::'. */
1637 p += 2;
1638 /* Skip whitespace. */
1639 while (*p == ' ' || *p == '\t' || *p == '\n')
1640 ++p;
1641 namestart = p;
1642 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1643 || (*p >= 'a' && *p <= 'z')
1644 || (*p >= 'A' && *p <= 'Z'))
1645 ++p;
1646 if (p != namestart)
1647 {
1648 struct symbol *cur_sym;
1649 /* As big as the whole rest of the expression, which is
1650 at least big enough. */
224c3ddb 1651 char *ncopy
61f4b350 1652 = (char *) alloca (tmp.size () + strlen (namestart)
224c3ddb 1653 + 3);
373a8247
PM
1654 char *tmp1;
1655
1656 tmp1 = ncopy;
61f4b350
TT
1657 memcpy (tmp1, tmp.c_str (), tmp.size ());
1658 tmp1 += tmp.size ();
373a8247
PM
1659 memcpy (tmp1, "::", 2);
1660 tmp1 += 2;
1661 memcpy (tmp1, namestart, p - namestart);
1662 tmp1[p - namestart] = '\0';
1e58a4a4
TT
1663 cur_sym
1664 = lookup_symbol (ncopy,
1665 pstate->expression_context_block,
1666 VAR_DOMAIN, NULL).symbol;
373a8247
PM
1667 if (cur_sym)
1668 {
1669 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1670 {
1671 best_sym = cur_sym;
5776fca3 1672 pstate->lexptr = p;
373a8247
PM
1673 }
1674 else
1675 break;
1676 }
1677 else
1678 break;
1679 }
1680 else
1681 break;
1682 }
1683 else
1684 break;
1685 }
1686
1687 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1688#else /* not 0 */
1689 yylval.tsym.type = SYMBOL_TYPE (sym);
1690#endif /* not 0 */
7877e977 1691 free (uptokstart);
373a8247 1692 return TYPENAME;
dda83cd7 1693 }
54a5b07d 1694 yylval.tsym.type
73923d7e 1695 = language_lookup_primitive_type (pstate->language (),
61f4b350 1696 pstate->gdbarch (), tmp.c_str ());
54a5b07d 1697 if (yylval.tsym.type != NULL)
7877e977
MS
1698 {
1699 free (uptokstart);
1700 return TYPENAME;
1701 }
373a8247
PM
1702
1703 /* Input names that aren't symbols but ARE valid hex numbers,
1704 when the input radix permits them, can be names or numbers
1705 depending on the parse. Note we support radixes > 16 here. */
0b058123 1706 if (!sym
dda83cd7
SM
1707 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1708 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
373a8247
PM
1709 {
1710 YYSTYPE newlval; /* Its value is ignored. */
410a0ff2 1711 hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
373a8247
PM
1712 if (hextype == INT)
1713 {
d12307c1
PMR
1714 yylval.ssym.sym.symbol = sym;
1715 yylval.ssym.sym.block = NULL;
1993b719 1716 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
7877e977 1717 free (uptokstart);
373a8247
PM
1718 return NAME_OR_INT;
1719 }
1720 }
1721
1722 free(uptokstart);
0df8b418 1723 /* Any other kind of symbol. */
d12307c1
PMR
1724 yylval.ssym.sym.symbol = sym;
1725 yylval.ssym.sym.block = NULL;
373a8247
PM
1726 return NAME;
1727 }
1728}
1729
410a0ff2
SDJ
1730int
1731pascal_parse (struct parser_state *par_state)
1732{
410a0ff2 1733 /* Setting up the parser state. */
eae49211 1734 scoped_restore pstate_restore = make_scoped_restore (&pstate);
410a0ff2
SDJ
1735 gdb_assert (par_state != NULL);
1736 pstate = par_state;
28aaf3fd 1737 paren_depth = 0;
410a0ff2 1738
eae49211 1739 return yyparse ();
410a0ff2
SDJ
1740}
1741
69d340c6 1742static void
a121b7c1 1743yyerror (const char *msg)
373a8247 1744{
5776fca3
TT
1745 if (pstate->prev_lexptr)
1746 pstate->lexptr = pstate->prev_lexptr;
24467a86 1747
5776fca3 1748 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
373a8247 1749}
This page took 1.986201 seconds and 4 git commands to generate.