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