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