import strstr and strerror modules
[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 127
793156e6 128static char *uptok (const char *, int);
373a8247
PM
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 1107static char *
793156e6 1108uptok (const 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;
793156e6 1136 const char *tokstart;
373a8247 1137 char *uptokstart;
793156e6 1138 const 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
793156e6 1149 tokstart = lexptr;
d3d6d173 1150 explen = strlen (lexptr);
d7561cbb 1151
373a8247 1152 /* See if it is a special token of length 3. */
d3d6d173
PM
1153 if (explen > 2)
1154 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
9c21ccdc 1155 if (strncasecmp (tokstart, tokentab3[i].operator, 3) == 0
d3d6d173 1156 && (!isalpha (tokentab3[i].operator[0]) || explen == 3
0df8b418
MS
1157 || (!isalpha (tokstart[3])
1158 && !isdigit (tokstart[3]) && tokstart[3] != '_')))
d3d6d173
PM
1159 {
1160 lexptr += 3;
1161 yylval.opcode = tokentab3[i].opcode;
1162 return tokentab3[i].token;
1163 }
373a8247
PM
1164
1165 /* See if it is a special token of length 2. */
d3d6d173
PM
1166 if (explen > 1)
1167 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
9c21ccdc 1168 if (strncasecmp (tokstart, tokentab2[i].operator, 2) == 0
d3d6d173 1169 && (!isalpha (tokentab2[i].operator[0]) || explen == 2
0df8b418
MS
1170 || (!isalpha (tokstart[2])
1171 && !isdigit (tokstart[2]) && tokstart[2] != '_')))
d3d6d173
PM
1172 {
1173 lexptr += 2;
1174 yylval.opcode = tokentab2[i].opcode;
1175 return tokentab2[i].token;
1176 }
373a8247
PM
1177
1178 switch (c = *tokstart)
1179 {
1180 case 0:
a5a44b53
PM
1181 if (saw_structop && search_field)
1182 return COMPLETE;
1183 else
1184 return 0;
373a8247
PM
1185
1186 case ' ':
1187 case '\t':
1188 case '\n':
1189 lexptr++;
1190 goto retry;
1191
1192 case '\'':
1193 /* We either have a character constant ('0' or '\177' for example)
1194 or we have a quoted symbol reference ('foo(int,int)' in object pascal
0df8b418 1195 for example). */
373a8247
PM
1196 lexptr++;
1197 c = *lexptr++;
1198 if (c == '\\')
f870a310 1199 c = parse_escape (parse_gdbarch, &lexptr);
373a8247 1200 else if (c == '\'')
001083c6 1201 error (_("Empty character constant."));
373a8247
PM
1202
1203 yylval.typed_val_int.val = c;
3e79cecf 1204 yylval.typed_val_int.type = parse_type->builtin_char;
373a8247
PM
1205
1206 c = *lexptr++;
1207 if (c != '\'')
1208 {
1209 namelen = skip_quoted (tokstart) - tokstart;
1210 if (namelen > 2)
1211 {
1212 lexptr = tokstart + namelen;
1213 if (lexptr[-1] != '\'')
001083c6 1214 error (_("Unmatched single quote."));
373a8247
PM
1215 namelen -= 2;
1216 tokstart++;
1217 uptokstart = uptok(tokstart,namelen);
1218 goto tryname;
1219 }
001083c6 1220 error (_("Invalid character constant."));
373a8247
PM
1221 }
1222 return INT;
1223
1224 case '(':
1225 paren_depth++;
1226 lexptr++;
1227 return c;
1228
1229 case ')':
1230 if (paren_depth == 0)
1231 return 0;
1232 paren_depth--;
1233 lexptr++;
1234 return c;
1235
1236 case ',':
1237 if (comma_terminates && paren_depth == 0)
1238 return 0;
1239 lexptr++;
1240 return c;
1241
1242 case '.':
1243 /* Might be a floating point number. */
1244 if (lexptr[1] < '0' || lexptr[1] > '9')
a5a44b53 1245 {
155da517 1246 if (parse_completion)
a5a44b53
PM
1247 last_was_structop = 1;
1248 goto symbol; /* Nope, must be a symbol. */
1249 }
1250
373a8247
PM
1251 /* FALL THRU into number case. */
1252
1253 case '0':
1254 case '1':
1255 case '2':
1256 case '3':
1257 case '4':
1258 case '5':
1259 case '6':
1260 case '7':
1261 case '8':
1262 case '9':
1263 {
1264 /* It's a number. */
1265 int got_dot = 0, got_e = 0, toktype;
793156e6 1266 const char *p = tokstart;
373a8247
PM
1267 int hex = input_radix > 10;
1268
1269 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1270 {
1271 p += 2;
1272 hex = 1;
1273 }
0df8b418
MS
1274 else if (c == '0' && (p[1]=='t' || p[1]=='T'
1275 || p[1]=='d' || p[1]=='D'))
373a8247
PM
1276 {
1277 p += 2;
1278 hex = 0;
1279 }
1280
1281 for (;; ++p)
1282 {
1283 /* This test includes !hex because 'e' is a valid hex digit
1284 and thus does not indicate a floating point number when
1285 the radix is hex. */
1286 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1287 got_dot = got_e = 1;
1288 /* This test does not include !hex, because a '.' always indicates
1289 a decimal floating point number regardless of the radix. */
1290 else if (!got_dot && *p == '.')
1291 got_dot = 1;
1292 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1293 && (*p == '-' || *p == '+'))
1294 /* This is the sign of the exponent, not the end of the
1295 number. */
1296 continue;
1297 /* We will take any letters or digits. parse_number will
1298 complain if past the radix, or if L or U are not final. */
1299 else if ((*p < '0' || *p > '9')
1300 && ((*p < 'a' || *p > 'z')
1301 && (*p < 'A' || *p > 'Z')))
1302 break;
1303 }
0df8b418
MS
1304 toktype = parse_number (tokstart,
1305 p - tokstart, got_dot | got_e, &yylval);
373a8247
PM
1306 if (toktype == ERROR)
1307 {
1308 char *err_copy = (char *) alloca (p - tokstart + 1);
1309
1310 memcpy (err_copy, tokstart, p - tokstart);
1311 err_copy[p - tokstart] = 0;
001083c6 1312 error (_("Invalid number \"%s\"."), err_copy);
373a8247
PM
1313 }
1314 lexptr = p;
1315 return toktype;
1316 }
1317
1318 case '+':
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 symbol:
1338 lexptr++;
1339 return c;
1340
1341 case '"':
1342
1343 /* Build the gdb internal form of the input string in tempbuf,
1344 translating any standard C escape forms seen. Note that the
1345 buffer is null byte terminated *only* for the convenience of
1346 debugging gdb itself and printing the buffer contents when
1347 the buffer contains no embedded nulls. Gdb does not depend
1348 upon the buffer being null byte terminated, it uses the length
1349 string instead. This allows gdb to handle C strings (as well
0df8b418 1350 as strings in other languages) with embedded null bytes. */
373a8247
PM
1351
1352 tokptr = ++tokstart;
1353 tempbufindex = 0;
1354
1355 do {
1356 /* Grow the static temp buffer if necessary, including allocating
0df8b418 1357 the first one on demand. */
373a8247
PM
1358 if (tempbufindex + 1 >= tempbufsize)
1359 {
1360 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1361 }
9819c6c8 1362
373a8247
PM
1363 switch (*tokptr)
1364 {
1365 case '\0':
1366 case '"':
0df8b418 1367 /* Do nothing, loop will terminate. */
373a8247
PM
1368 break;
1369 case '\\':
793156e6
KS
1370 ++tokptr;
1371 c = parse_escape (parse_gdbarch, &tokptr);
1372 if (c == -1)
1373 {
1374 continue;
1375 }
1376 tempbuf[tempbufindex++] = c;
373a8247
PM
1377 break;
1378 default:
1379 tempbuf[tempbufindex++] = *tokptr++;
1380 break;
1381 }
1382 } while ((*tokptr != '"') && (*tokptr != '\0'));
1383 if (*tokptr++ != '"')
1384 {
001083c6 1385 error (_("Unterminated string in expression."));
373a8247 1386 }
0df8b418 1387 tempbuf[tempbufindex] = '\0'; /* See note above. */
373a8247
PM
1388 yylval.sval.ptr = tempbuf;
1389 yylval.sval.length = tempbufindex;
1390 lexptr = tokptr;
1391 return (STRING);
1392 }
1393
1394 if (!(c == '_' || c == '$'
1395 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1396 /* We must have come across a bad character (e.g. ';'). */
001083c6 1397 error (_("Invalid character '%c' in expression."), c);
373a8247
PM
1398
1399 /* It's a name. See how long it is. */
1400 namelen = 0;
1401 for (c = tokstart[namelen];
1402 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1403 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1404 {
1405 /* Template parameter lists are part of the name.
1406 FIXME: This mishandles `print $a<4&&$a>3'. */
1407 if (c == '<')
1408 {
1409 int i = namelen;
1410 int nesting_level = 1;
1411 while (tokstart[++i])
1412 {
1413 if (tokstart[i] == '<')
1414 nesting_level++;
1415 else if (tokstart[i] == '>')
1416 {
1417 if (--nesting_level == 0)
1418 break;
1419 }
1420 }
1421 if (tokstart[i] == '>')
1422 namelen = i;
1423 else
1424 break;
1425 }
1426
0df8b418 1427 /* do NOT uppercase internals because of registers !!! */
373a8247
PM
1428 c = tokstart[++namelen];
1429 }
1430
1431 uptokstart = uptok(tokstart,namelen);
1432
1433 /* The token "if" terminates the expression and is NOT
1434 removed from the input stream. */
1435 if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
1436 {
7877e977 1437 free (uptokstart);
373a8247
PM
1438 return 0;
1439 }
1440
1441 lexptr += namelen;
1442
1443 tryname:
1444
1445 /* Catch specific keywords. Should be done with a data structure. */
1446 switch (namelen)
1447 {
1448 case 6:
0b058123 1449 if (strcmp (uptokstart, "OBJECT") == 0)
7877e977
MS
1450 {
1451 free (uptokstart);
1452 return CLASS;
1453 }
0b058123 1454 if (strcmp (uptokstart, "RECORD") == 0)
7877e977
MS
1455 {
1456 free (uptokstart);
1457 return STRUCT;
1458 }
0b058123 1459 if (strcmp (uptokstart, "SIZEOF") == 0)
7877e977
MS
1460 {
1461 free (uptokstart);
1462 return SIZEOF;
1463 }
373a8247
PM
1464 break;
1465 case 5:
0b058123 1466 if (strcmp (uptokstart, "CLASS") == 0)
7877e977
MS
1467 {
1468 free (uptokstart);
1469 return CLASS;
1470 }
0b058123 1471 if (strcmp (uptokstart, "FALSE") == 0)
373a8247
PM
1472 {
1473 yylval.lval = 0;
7877e977 1474 free (uptokstart);
2692ddb3 1475 return FALSEKEYWORD;
373a8247
PM
1476 }
1477 break;
1478 case 4:
0b058123 1479 if (strcmp (uptokstart, "TRUE") == 0)
373a8247
PM
1480 {
1481 yylval.lval = 1;
7877e977 1482 free (uptokstart);
2692ddb3 1483 return TRUEKEYWORD;
373a8247 1484 }
0b058123 1485 if (strcmp (uptokstart, "SELF") == 0)
373a8247 1486 {
0df8b418
MS
1487 /* Here we search for 'this' like
1488 inserted in FPC stabs debug info. */
8343f86c 1489 static const char this_name[] = "this";
373a8247
PM
1490
1491 if (lookup_symbol (this_name, expression_context_block,
1993b719 1492 VAR_DOMAIN, NULL))
7877e977
MS
1493 {
1494 free (uptokstart);
1495 return THIS;
1496 }
373a8247
PM
1497 }
1498 break;
1499 default:
1500 break;
1501 }
1502
1503 yylval.sval.ptr = tokstart;
1504 yylval.sval.length = namelen;
1505
1506 if (*tokstart == '$')
1507 {
793156e6
KS
1508 char *tmp;
1509
373a8247
PM
1510 /* $ is the normal prefix for pascal hexadecimal values
1511 but this conflicts with the GDB use for debugger variables
1512 so in expression to enter hexadecimal values
1513 we still need to use C syntax with 0xff */
1514 write_dollar_variable (yylval.sval);
793156e6
KS
1515 tmp = alloca (namelen + 1);
1516 memcpy (tmp, tokstart, namelen);
1517 tmp[namelen] = '\0';
1518 intvar = lookup_only_internalvar (tmp + 1);
7877e977 1519 free (uptokstart);
373a8247
PM
1520 return VARIABLE;
1521 }
1522
1523 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1524 functions or symtabs. If this is not so, then ...
1525 Use token-type TYPENAME for symbols that happen to be defined
1526 currently as names of types; NAME for other symbols.
1527 The caller is not constrained to care about the distinction. */
1528 {
1529 char *tmp = copy_name (yylval.sval);
1530 struct symbol *sym;
1993b719 1531 struct field_of_this_result is_a_field_of_this;
9819c6c8 1532 int is_a_field = 0;
373a8247
PM
1533 int hextype;
1534
9819c6c8
PM
1535
1536 if (search_field && current_type)
0df8b418 1537 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
155da517 1538 if (is_a_field || parse_completion)
9819c6c8
PM
1539 sym = NULL;
1540 else
1541 sym = lookup_symbol (tmp, expression_context_block,
2570f2b7 1542 VAR_DOMAIN, &is_a_field_of_this);
94a716bf 1543 /* second chance uppercased (as Free Pascal does). */
1993b719 1544 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
373a8247 1545 {
94a716bf 1546 for (i = 0; i <= namelen; i++)
373a8247 1547 {
94a716bf 1548 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
373a8247 1549 tmp[i] -= ('a'-'A');
373a8247 1550 }
9819c6c8 1551 if (search_field && current_type)
0df8b418 1552 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
155da517 1553 if (is_a_field || parse_completion)
9819c6c8
PM
1554 sym = NULL;
1555 else
1556 sym = lookup_symbol (tmp, expression_context_block,
2570f2b7 1557 VAR_DOMAIN, &is_a_field_of_this);
94a716bf
PM
1558 }
1559 /* Third chance Capitalized (as GPC does). */
1993b719 1560 if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
94a716bf
PM
1561 {
1562 for (i = 0; i <= namelen; i++)
1563 {
1564 if (i == 0)
1565 {
1566 if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
1567 tmp[i] -= ('a'-'A');
1568 }
1569 else
1570 if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
1571 tmp[i] -= ('A'-'a');
1572 }
9819c6c8 1573 if (search_field && current_type)
0df8b418 1574 is_a_field = (lookup_struct_elt_type (current_type, tmp, 1) != NULL);
155da517 1575 if (is_a_field || parse_completion)
9819c6c8
PM
1576 sym = NULL;
1577 else
1578 sym = lookup_symbol (tmp, expression_context_block,
2570f2b7 1579 VAR_DOMAIN, &is_a_field_of_this);
373a8247 1580 }
9819c6c8
PM
1581
1582 if (is_a_field)
1583 {
1584 tempbuf = (char *) realloc (tempbuf, namelen + 1);
793156e6
KS
1585 strncpy (tempbuf, tmp, namelen);
1586 tempbuf [namelen] = 0;
9819c6c8 1587 yylval.sval.ptr = tempbuf;
6ced1581 1588 yylval.sval.length = namelen;
7877e977 1589 free (uptokstart);
9819c6c8 1590 return FIELDNAME;
6ced1581 1591 }
373a8247
PM
1592 /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1593 no psymtabs (coff, xcoff, or some future change to blow away the
1594 psymtabs once once symbols are read). */
0b058123
PM
1595 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1596 || lookup_symtab (tmp))
373a8247
PM
1597 {
1598 yylval.ssym.sym = sym;
1993b719 1599 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
7877e977 1600 free (uptokstart);
373a8247
PM
1601 return BLOCKNAME;
1602 }
1603 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1604 {
1605#if 1
1606 /* Despite the following flaw, we need to keep this code enabled.
1607 Because we can get called from check_stub_method, if we don't
1608 handle nested types then it screws many operations in any
1609 program which uses nested types. */
1610 /* In "A::x", if x is a member function of A and there happens
1611 to be a type (nested or not, since the stabs don't make that
1612 distinction) named x, then this code incorrectly thinks we
1613 are dealing with nested types rather than a member function. */
1614
d7561cbb
KS
1615 const char *p;
1616 const char *namestart;
373a8247
PM
1617 struct symbol *best_sym;
1618
1619 /* Look ahead to detect nested types. This probably should be
1620 done in the grammar, but trying seemed to introduce a lot
1621 of shift/reduce and reduce/reduce conflicts. It's possible
1622 that it could be done, though. Or perhaps a non-grammar, but
1623 less ad hoc, approach would work well. */
1624
1625 /* Since we do not currently have any way of distinguishing
1626 a nested type from a non-nested one (the stabs don't tell
1627 us whether a type is nested), we just ignore the
1628 containing type. */
1629
1630 p = lexptr;
1631 best_sym = sym;
1632 while (1)
1633 {
1634 /* Skip whitespace. */
1635 while (*p == ' ' || *p == '\t' || *p == '\n')
1636 ++p;
1637 if (*p == ':' && p[1] == ':')
1638 {
1639 /* Skip the `::'. */
1640 p += 2;
1641 /* Skip whitespace. */
1642 while (*p == ' ' || *p == '\t' || *p == '\n')
1643 ++p;
1644 namestart = p;
1645 while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1646 || (*p >= 'a' && *p <= 'z')
1647 || (*p >= 'A' && *p <= 'Z'))
1648 ++p;
1649 if (p != namestart)
1650 {
1651 struct symbol *cur_sym;
1652 /* As big as the whole rest of the expression, which is
1653 at least big enough. */
1654 char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1655 char *tmp1;
1656
1657 tmp1 = ncopy;
1658 memcpy (tmp1, tmp, strlen (tmp));
1659 tmp1 += strlen (tmp);
1660 memcpy (tmp1, "::", 2);
1661 tmp1 += 2;
1662 memcpy (tmp1, namestart, p - namestart);
1663 tmp1[p - namestart] = '\0';
1664 cur_sym = lookup_symbol (ncopy, expression_context_block,
1993b719 1665 VAR_DOMAIN, NULL);
373a8247
PM
1666 if (cur_sym)
1667 {
1668 if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1669 {
1670 best_sym = cur_sym;
1671 lexptr = p;
1672 }
1673 else
1674 break;
1675 }
1676 else
1677 break;
1678 }
1679 else
1680 break;
1681 }
1682 else
1683 break;
1684 }
1685
1686 yylval.tsym.type = SYMBOL_TYPE (best_sym);
1687#else /* not 0 */
1688 yylval.tsym.type = SYMBOL_TYPE (sym);
1689#endif /* not 0 */
7877e977 1690 free (uptokstart);
373a8247
PM
1691 return TYPENAME;
1692 }
54a5b07d 1693 yylval.tsym.type
3e79cecf
UW
1694 = language_lookup_primitive_type_by_name (parse_language,
1695 parse_gdbarch, tmp);
54a5b07d 1696 if (yylval.tsym.type != NULL)
7877e977
MS
1697 {
1698 free (uptokstart);
1699 return TYPENAME;
1700 }
373a8247
PM
1701
1702 /* Input names that aren't symbols but ARE valid hex numbers,
1703 when the input radix permits them, can be names or numbers
1704 depending on the parse. Note we support radixes > 16 here. */
0b058123
PM
1705 if (!sym
1706 && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
1707 || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
373a8247
PM
1708 {
1709 YYSTYPE newlval; /* Its value is ignored. */
1710 hextype = parse_number (tokstart, namelen, 0, &newlval);
1711 if (hextype == INT)
1712 {
1713 yylval.ssym.sym = sym;
1993b719 1714 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
7877e977 1715 free (uptokstart);
373a8247
PM
1716 return NAME_OR_INT;
1717 }
1718 }
1719
1720 free(uptokstart);
0df8b418 1721 /* Any other kind of symbol. */
373a8247 1722 yylval.ssym.sym = sym;
1993b719 1723 yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
373a8247
PM
1724 return NAME;
1725 }
1726}
1727
1728void
d04550a6 1729yyerror (char *msg)
373a8247 1730{
24467a86
PM
1731 if (prev_lexptr)
1732 lexptr = prev_lexptr;
1733
001083c6 1734 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
373a8247 1735}
This page took 1.234578 seconds and 4 git commands to generate.