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