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