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