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