* ch-exp.y (write_lower_upper_value): Add prototype so bison
[binutils-gdb.git] / gdb / ch-exp.y
1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright 1992, 1993, 1994 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* Parse a Chill expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
28
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
36
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
51
52 */
53
54 %{
55
56 #include "defs.h"
57 #include <string.h>
58 #include <ctype.h>
59 #include "expression.h"
60 #include "language.h"
61 #include "value.h"
62 #include "parser-defs.h"
63 #include "ch-lang.h"
64 #include "bfd.h" /* Required by objfiles.h. */
65 #include "symfile.h" /* Required by objfiles.h. */
66 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
67
68 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
69 as well as gratuitiously global symbol names, so we can have multiple
70 yacc generated parsers in gdb. Note that these are only the variables
71 produced by yacc. If other parser generators (bison, byacc, etc) produce
72 additional global names that conflict at link time, then those parser
73 generators need to be fixed instead of adding those names to this list. */
74
75 #define yymaxdepth chill_maxdepth
76 #define yyparse chill_parse
77 #define yylex chill_lex
78 #define yyerror chill_error
79 #define yylval chill_lval
80 #define yychar chill_char
81 #define yydebug chill_debug
82 #define yypact chill_pact
83 #define yyr1 chill_r1
84 #define yyr2 chill_r2
85 #define yydef chill_def
86 #define yychk chill_chk
87 #define yypgo chill_pgo
88 #define yyact chill_act
89 #define yyexca chill_exca
90 #define yyerrflag chill_errflag
91 #define yynerrs chill_nerrs
92 #define yyps chill_ps
93 #define yypv chill_pv
94 #define yys chill_s
95 #define yy_yys chill_yys
96 #define yystate chill_state
97 #define yytmp chill_tmp
98 #define yyv chill_v
99 #define yy_yyv chill_yyv
100 #define yyval chill_val
101 #define yylloc chill_lloc
102 #define yyreds chill_reds /* With YYDEBUG defined */
103 #define yytoks chill_toks /* With YYDEBUG defined */
104 #define yylhs chill_yylhs
105 #define yylen chill_yylen
106 #define yydefred chill_yydefred
107 #define yydgoto chill_yydgoto
108 #define yysindex chill_yysindex
109 #define yyrindex chill_yyrindex
110 #define yygindex chill_yygindex
111 #define yytable chill_yytable
112 #define yycheck chill_yycheck
113
114 #ifndef YYDEBUG
115 #define YYDEBUG 0 /* Default to no yydebug support */
116 #endif
117
118 static void
119 write_lower_upper_value PARAMS ((enum exp_opcode, struct type *type));
120
121 int
122 yyparse PARAMS ((void));
123
124 static int
125 yylex PARAMS ((void));
126
127 void
128 yyerror PARAMS ((char *));
129
130 %}
131
132 /* Although the yacc "value" of an expression is not used,
133 since the result is stored in the structure being created,
134 other node types do have values. */
135
136 %union
137 {
138 LONGEST lval;
139 unsigned LONGEST ulval;
140 struct {
141 LONGEST val;
142 struct type *type;
143 } typed_val;
144 double dval;
145 struct symbol *sym;
146 struct type *tval;
147 struct stoken sval;
148 struct ttype tsym;
149 struct symtoken ssym;
150 int voidval;
151 struct block *bval;
152 enum exp_opcode opcode;
153 struct internalvar *ivar;
154
155 struct type **tvec;
156 int *ivec;
157 }
158
159 %token <typed_val> INTEGER_LITERAL
160 %token <ulval> BOOLEAN_LITERAL
161 %token <typed_val> CHARACTER_LITERAL
162 %token <dval> FLOAT_LITERAL
163 %token <ssym> GENERAL_PROCEDURE_NAME
164 %token <ssym> LOCATION_NAME
165 %token <voidval> EMPTINESS_LITERAL
166 %token <sval> CHARACTER_STRING_LITERAL
167 %token <sval> BIT_STRING_LITERAL
168 %token <tsym> TYPENAME
169 %token <sval> FIELD_NAME
170
171 %token <voidval> '.'
172 %token <voidval> ';'
173 %token <voidval> ':'
174 %token <voidval> CASE
175 %token <voidval> OF
176 %token <voidval> ESAC
177 %token <voidval> LOGIOR
178 %token <voidval> ORIF
179 %token <voidval> LOGXOR
180 %token <voidval> LOGAND
181 %token <voidval> ANDIF
182 %token <voidval> '='
183 %token <voidval> NOTEQUAL
184 %token <voidval> '>'
185 %token <voidval> GTR
186 %token <voidval> '<'
187 %token <voidval> LEQ
188 %token <voidval> IN
189 %token <voidval> '+'
190 %token <voidval> '-'
191 %token <voidval> '*'
192 %token <voidval> '/'
193 %token <voidval> SLASH_SLASH
194 %token <voidval> MOD
195 %token <voidval> REM
196 %token <voidval> NOT
197 %token <voidval> POINTER
198 %token <voidval> RECEIVE
199 %token <voidval> '['
200 %token <voidval> ']'
201 %token <voidval> '('
202 %token <voidval> ')'
203 %token <voidval> UP
204 %token <voidval> IF
205 %token <voidval> THEN
206 %token <voidval> ELSE
207 %token <voidval> FI
208 %token <voidval> ELSIF
209 %token <voidval> ILLEGAL_TOKEN
210 %token <voidval> NUM
211 %token <voidval> PRED
212 %token <voidval> SUCC
213 %token <voidval> ABS
214 %token <voidval> CARD
215 %token <voidval> MAX_TOKEN
216 %token <voidval> MIN_TOKEN
217 %token <voidval> ADDR_TOKEN
218 %token <voidval> SIZE
219 %token <voidval> UPPER
220 %token <voidval> LOWER
221 %token <voidval> LENGTH
222 %token <voidval> ARRAY
223
224 /* Tokens which are not Chill tokens used in expressions, but rather GDB
225 specific things that we recognize in the same context as Chill tokens
226 (register names for example). */
227
228 %token <lval> GDB_REGNAME /* Machine register name */
229 %token <lval> GDB_LAST /* Value history */
230 %token <ivar> GDB_VARIABLE /* Convenience variable */
231 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
232
233 %type <voidval> access_name
234 %type <voidval> primitive_value
235 %type <voidval> value_name
236 %type <voidval> literal
237 %type <voidval> tuple
238 %type <voidval> slice
239 %type <voidval> expression_conversion
240 %type <voidval> value_built_in_routine_call
241 %type <voidval> parenthesised_expression
242 %type <voidval> value
243 %type <voidval> expression
244 %type <voidval> conditional_expression
245 %type <voidval> then_alternative
246 %type <voidval> else_alternative
247 %type <voidval> operand_0
248 %type <voidval> operand_1
249 %type <voidval> operand_2
250 %type <voidval> operand_3
251 %type <voidval> operand_4
252 %type <voidval> operand_5
253 %type <voidval> operand_6
254 %type <voidval> expression_list
255 %type <tval> mode_argument
256 %type <voidval> single_assignment_action
257 %type <tsym> mode_name
258 %type <lval> rparen
259
260 /* Not implemented:
261 %type <voidval> undefined_value
262 %type <voidval> array_mode_name
263 %type <voidval> string_mode_name
264 %type <voidval> variant_structure_mode_name
265 */
266
267 %%
268
269 /* Z.200, 5.3.1 */
270
271 start : value { }
272 | mode_name
273 { write_exp_elt_opcode(OP_TYPE);
274 write_exp_elt_type($1.type);
275 write_exp_elt_opcode(OP_TYPE);}
276 ;
277
278 value : expression
279 /*
280 | undefined_value
281 { ??? }
282 */
283 ;
284
285 /* Z.200, 4.2.2 */
286
287 access_name : LOCATION_NAME
288 {
289 write_exp_elt_opcode (OP_VAR_VALUE);
290 write_exp_elt_block (NULL);
291 write_exp_elt_sym ($1.sym);
292 write_exp_elt_opcode (OP_VAR_VALUE);
293 }
294 | GDB_LAST /* gdb specific */
295 {
296 write_exp_elt_opcode (OP_LAST);
297 write_exp_elt_longcst ($1);
298 write_exp_elt_opcode (OP_LAST);
299 }
300 | GDB_REGNAME /* gdb specific */
301 {
302 write_exp_elt_opcode (OP_REGISTER);
303 write_exp_elt_longcst ($1);
304 write_exp_elt_opcode (OP_REGISTER);
305 }
306 | GDB_VARIABLE /* gdb specific */
307 {
308 write_exp_elt_opcode (OP_INTERNALVAR);
309 write_exp_elt_intern ($1);
310 write_exp_elt_opcode (OP_INTERNALVAR);
311 }
312 ;
313
314 /* Z.200, 4.2.8 */
315
316 expression_list : expression
317 {
318 arglist_len = 1;
319 }
320 | expression_list ',' expression
321 {
322 arglist_len++;
323 }
324 ;
325
326 maybe_expression_list: /* EMPTY */
327 {
328 arglist_len = 0;
329 }
330 | expression_list
331 ;
332
333
334 /* Z.200, 5.2.1 */
335
336 primitive_value_lparen: primitive_value '('
337 /* This is to save the value of arglist_len
338 being accumulated for each dimension. */
339 { start_arglist (); }
340 ;
341
342 rparen : ')'
343 { $$ = end_arglist (); }
344 ;
345
346 primitive_value :
347 access_name
348 | primitive_value_lparen maybe_expression_list rparen
349 {
350 write_exp_elt_opcode (MULTI_SUBSCRIPT);
351 write_exp_elt_longcst ($3);
352 write_exp_elt_opcode (MULTI_SUBSCRIPT);
353 }
354 | primitive_value FIELD_NAME
355 { write_exp_elt_opcode (STRUCTOP_STRUCT);
356 write_exp_string ($2);
357 write_exp_elt_opcode (STRUCTOP_STRUCT);
358 }
359 | primitive_value POINTER
360 {
361 write_exp_elt_opcode (UNOP_IND);
362 }
363 | primitive_value POINTER mode_name
364 {
365 write_exp_elt_opcode (UNOP_CAST);
366 write_exp_elt_type (lookup_pointer_type ($3.type));
367 write_exp_elt_opcode (UNOP_CAST);
368 write_exp_elt_opcode (UNOP_IND);
369 }
370 | value_name
371 | literal
372 | tuple
373 | slice
374 | expression_conversion
375 | value_built_in_routine_call
376 /*
377 | start_expression
378 { ??? }
379 | zero_adic_operator
380 { ??? }
381 */
382 | parenthesised_expression
383 ;
384
385 /* Z.200, 5.2.3 */
386
387 value_name : GENERAL_PROCEDURE_NAME
388 {
389 write_exp_elt_opcode (OP_VAR_VALUE);
390 write_exp_elt_block (NULL);
391 write_exp_elt_sym ($1.sym);
392 write_exp_elt_opcode (OP_VAR_VALUE);
393 }
394 ;
395
396 /* Z.200, 5.2.4.1 */
397
398 literal : INTEGER_LITERAL
399 {
400 write_exp_elt_opcode (OP_LONG);
401 write_exp_elt_type ($1.type);
402 write_exp_elt_longcst ((LONGEST) ($1.val));
403 write_exp_elt_opcode (OP_LONG);
404 }
405 | BOOLEAN_LITERAL
406 {
407 write_exp_elt_opcode (OP_BOOL);
408 write_exp_elt_longcst ((LONGEST) $1);
409 write_exp_elt_opcode (OP_BOOL);
410 }
411 | CHARACTER_LITERAL
412 {
413 write_exp_elt_opcode (OP_LONG);
414 write_exp_elt_type ($1.type);
415 write_exp_elt_longcst ((LONGEST) ($1.val));
416 write_exp_elt_opcode (OP_LONG);
417 }
418 | FLOAT_LITERAL
419 {
420 write_exp_elt_opcode (OP_DOUBLE);
421 write_exp_elt_type (builtin_type_double);
422 write_exp_elt_dblcst ($1);
423 write_exp_elt_opcode (OP_DOUBLE);
424 }
425 | EMPTINESS_LITERAL
426 {
427 struct type *void_ptr_type
428 = lookup_pointer_type (builtin_type_void);
429 write_exp_elt_opcode (OP_LONG);
430 write_exp_elt_type (void_ptr_type);
431 write_exp_elt_longcst (0);
432 write_exp_elt_opcode (OP_LONG);
433 }
434 | CHARACTER_STRING_LITERAL
435 {
436 write_exp_elt_opcode (OP_STRING);
437 write_exp_string ($1);
438 write_exp_elt_opcode (OP_STRING);
439 }
440 | BIT_STRING_LITERAL
441 {
442 write_exp_elt_opcode (OP_BITSTRING);
443 write_exp_bitstring ($1);
444 write_exp_elt_opcode (OP_BITSTRING);
445 }
446 ;
447
448 /* Z.200, 5.2.5 */
449
450 tuple_element : expression
451 | named_record_element
452 ;
453
454 named_record_element: FIELD_NAME ',' named_record_element
455 { write_exp_elt_opcode (OP_LABELED);
456 write_exp_string ($1);
457 write_exp_elt_opcode (OP_LABELED);
458 }
459 | FIELD_NAME ':' expression
460 { write_exp_elt_opcode (OP_LABELED);
461 write_exp_string ($1);
462 write_exp_elt_opcode (OP_LABELED);
463 }
464 ;
465
466 tuple_elements : tuple_element
467 {
468 arglist_len = 1;
469 }
470 | tuple_elements ',' tuple_element
471 {
472 arglist_len++;
473 }
474 ;
475
476 maybe_tuple_elements : tuple_elements
477 | /* EMPTY */
478 ;
479
480 tuple : '['
481 { start_arglist (); }
482 maybe_tuple_elements ']'
483 {
484 write_exp_elt_opcode (OP_ARRAY);
485 write_exp_elt_longcst ((LONGEST) 0);
486 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
487 write_exp_elt_opcode (OP_ARRAY);
488 }
489 |
490 mode_name '['
491 { start_arglist (); }
492 maybe_tuple_elements ']'
493 {
494 write_exp_elt_opcode (OP_ARRAY);
495 write_exp_elt_longcst ((LONGEST) 0);
496 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
497 write_exp_elt_opcode (OP_ARRAY);
498
499 write_exp_elt_opcode (UNOP_CAST);
500 write_exp_elt_type ($1.type);
501 write_exp_elt_opcode (UNOP_CAST);
502 }
503 ;
504
505
506 /* Z.200, 5.2.6 */
507
508
509 slice: primitive_value_lparen expression ':' expression rparen
510 {
511 write_exp_elt_opcode (TERNOP_SLICE);
512 }
513 | primitive_value_lparen expression UP expression rparen
514 {
515 write_exp_elt_opcode (TERNOP_SLICE_COUNT);
516 }
517 ;
518
519 /* Z.200, 5.2.11 */
520
521 expression_conversion: mode_name parenthesised_expression
522 {
523 write_exp_elt_opcode (UNOP_CAST);
524 write_exp_elt_type ($1.type);
525 write_exp_elt_opcode (UNOP_CAST);
526 }
527 | ARRAY '(' ')' mode_name parenthesised_expression
528 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
529 which casts to an artificial array. */
530 {
531 struct type *range_type
532 = create_range_type ((struct type *) NULL,
533 builtin_type_int, 0, 0);
534 struct type *array_type
535 = create_array_type ((struct type *) NULL,
536 $4.type, range_type);
537 TYPE_ARRAY_UPPER_BOUND_TYPE(array_type)
538 = BOUND_CANNOT_BE_DETERMINED;
539 write_exp_elt_opcode (UNOP_CAST);
540 write_exp_elt_type (array_type);
541 write_exp_elt_opcode (UNOP_CAST);
542 }
543 ;
544
545 /* Z.200, 5.2.16 */
546
547 parenthesised_expression: '(' expression ')'
548 ;
549
550 /* Z.200, 5.3.2 */
551
552 expression : operand_0
553 | single_assignment_action
554 | conditional_expression
555 ;
556
557 conditional_expression : IF expression then_alternative else_alternative FI
558 { write_exp_elt_opcode (TERNOP_COND); }
559 /*
560 | CASE case_selector_list OF value_case_alternative ELSE expression ESAC
561 { error ("not implemented: CASE expression" }
562 */
563 ;
564
565 then_alternative: THEN expression
566 ;
567
568 else_alternative: ELSE expression
569 | ELSIF expression then_alternative else_alternative
570 { write_exp_elt_opcode (TERNOP_COND); }
571 ;
572
573 /* Z.200, 5.3.3 */
574
575 operand_0 : operand_1
576 | operand_0 LOGIOR operand_1
577 {
578 write_exp_elt_opcode (BINOP_BITWISE_IOR);
579 }
580 | operand_0 ORIF operand_1
581 {
582 write_exp_elt_opcode (BINOP_LOGICAL_OR);
583 }
584 | operand_0 LOGXOR operand_1
585 {
586 write_exp_elt_opcode (BINOP_BITWISE_XOR);
587 }
588 ;
589
590 /* Z.200, 5.3.4 */
591
592 operand_1 : operand_2
593 | operand_1 LOGAND operand_2
594 {
595 write_exp_elt_opcode (BINOP_BITWISE_AND);
596 }
597 | operand_1 ANDIF operand_2
598 {
599 write_exp_elt_opcode (BINOP_LOGICAL_AND);
600 }
601 ;
602
603 /* Z.200, 5.3.5 */
604
605 operand_2 : operand_3
606 | operand_2 '=' operand_3
607 {
608 write_exp_elt_opcode (BINOP_EQUAL);
609 }
610 | operand_2 NOTEQUAL operand_3
611 {
612 write_exp_elt_opcode (BINOP_NOTEQUAL);
613 }
614 | operand_2 '>' operand_3
615 {
616 write_exp_elt_opcode (BINOP_GTR);
617 }
618 | operand_2 GTR operand_3
619 {
620 write_exp_elt_opcode (BINOP_GEQ);
621 }
622 | operand_2 '<' operand_3
623 {
624 write_exp_elt_opcode (BINOP_LESS);
625 }
626 | operand_2 LEQ operand_3
627 {
628 write_exp_elt_opcode (BINOP_LEQ);
629 }
630 | operand_2 IN operand_3
631 {
632 write_exp_elt_opcode (BINOP_IN);
633 }
634 ;
635
636
637 /* Z.200, 5.3.6 */
638
639 operand_3 : operand_4
640 | operand_3 '+' operand_4
641 {
642 write_exp_elt_opcode (BINOP_ADD);
643 }
644 | operand_3 '-' operand_4
645 {
646 write_exp_elt_opcode (BINOP_SUB);
647 }
648 | operand_3 SLASH_SLASH operand_4
649 {
650 write_exp_elt_opcode (BINOP_CONCAT);
651 }
652 ;
653
654 /* Z.200, 5.3.7 */
655
656 operand_4 : operand_5
657 | operand_4 '*' operand_5
658 {
659 write_exp_elt_opcode (BINOP_MUL);
660 }
661 | operand_4 '/' operand_5
662 {
663 write_exp_elt_opcode (BINOP_DIV);
664 }
665 | operand_4 MOD operand_5
666 {
667 write_exp_elt_opcode (BINOP_MOD);
668 }
669 | operand_4 REM operand_5
670 {
671 write_exp_elt_opcode (BINOP_REM);
672 }
673 ;
674
675 /* Z.200, 5.3.8 */
676
677 operand_5 : operand_6
678 | '-' operand_6
679 {
680 write_exp_elt_opcode (UNOP_NEG);
681 }
682 | NOT operand_6
683 {
684 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
685 }
686 | parenthesised_expression literal
687 /* We require the string operand to be a literal, to avoid some
688 nasty parsing ambiguities. */
689 {
690 write_exp_elt_opcode (BINOP_CONCAT);
691 }
692 ;
693
694 /* Z.200, 5.3.9 */
695
696 operand_6 : POINTER primitive_value
697 {
698 write_exp_elt_opcode (UNOP_ADDR);
699 }
700 | RECEIVE expression
701 { error ("not implemented: RECEIVE expression"); }
702 | primitive_value
703 ;
704
705
706 /* Z.200, 6.2 */
707
708 single_assignment_action :
709 primitive_value GDB_ASSIGNMENT value
710 {
711 write_exp_elt_opcode (BINOP_ASSIGN);
712 }
713 ;
714
715 /* Z.200, 6.20.3 */
716
717 value_built_in_routine_call :
718 NUM '(' expression ')'
719 {
720 write_exp_elt_opcode (UNOP_CAST);
721 write_exp_elt_type (builtin_type_int);
722 write_exp_elt_opcode (UNOP_CAST);
723 }
724 | PRED '(' expression ')'
725 { error ("not implemented: PRED builtin function"); }
726 | SUCC '(' expression ')'
727 { error ("not implemented: SUCC builtin function"); }
728 | ADDR_TOKEN '(' expression ')'
729 { write_exp_elt_opcode (UNOP_ADDR); }
730 | ABS '(' expression ')'
731 { error ("not implemented: ABS builtin function"); }
732 | CARD '(' expression ')'
733 { error ("not implemented: CARD builtin function"); }
734 | MAX_TOKEN '(' expression ')'
735 { error ("not implemented: MAX builtin function"); }
736 | MIN_TOKEN '(' expression ')'
737 { error ("not implemented: MIN builtin function"); }
738 | SIZE '(' expression ')'
739 { write_exp_elt_opcode (UNOP_SIZEOF); }
740 | SIZE '(' mode_argument ')'
741 { write_exp_elt_opcode (OP_LONG);
742 write_exp_elt_type (builtin_type_int);
743 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
744 write_exp_elt_opcode (OP_LONG); }
745 | LOWER '(' mode_argument ')'
746 { write_lower_upper_value (UNOP_LOWER, $3); }
747 | UPPER '(' mode_argument ')'
748 { write_lower_upper_value (UNOP_UPPER, $3); }
749 | LOWER '(' expression ')'
750 { write_exp_elt_opcode (UNOP_LOWER); }
751 | UPPER '(' expression ')'
752 { write_exp_elt_opcode (UNOP_UPPER); }
753 | LENGTH '(' expression ')'
754 { write_exp_elt_opcode (UNOP_LENGTH); }
755 ;
756
757 mode_argument : mode_name
758 {
759 $$ = $1.type;
760 }
761 /*
762 | array_mode_name '(' expression ')'
763 { ??? }
764 | string_mode_name '(' expression ')'
765 { ??? }
766 | variant_structure_mode_name '(' expression_list ')'
767 { ??? }
768 */
769 ;
770
771 mode_name : TYPENAME
772 ;
773
774 %%
775
776 /* Implementation of a dynamically expandable buffer for processing input
777 characters acquired through lexptr and building a value to return in
778 yylval. */
779
780 static char *tempbuf; /* Current buffer contents */
781 static int tempbufsize; /* Size of allocated buffer */
782 static int tempbufindex; /* Current index into buffer */
783
784 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
785
786 #define CHECKBUF(size) \
787 do { \
788 if (tempbufindex + (size) >= tempbufsize) \
789 { \
790 growbuf_by_size (size); \
791 } \
792 } while (0);
793
794 /* Grow the static temp buffer if necessary, including allocating the first one
795 on demand. */
796
797 static void
798 growbuf_by_size (count)
799 int count;
800 {
801 int growby;
802
803 growby = max (count, GROWBY_MIN_SIZE);
804 tempbufsize += growby;
805 if (tempbuf == NULL)
806 {
807 tempbuf = (char *) malloc (tempbufsize);
808 }
809 else
810 {
811 tempbuf = (char *) realloc (tempbuf, tempbufsize);
812 }
813 }
814
815 /* Try to consume a simple name string token. If successful, returns
816 a pointer to a nullbyte terminated copy of the name that can be used
817 in symbol table lookups. If not successful, returns NULL. */
818
819 static char *
820 match_simple_name_string ()
821 {
822 char *tokptr = lexptr;
823
824 if (isalpha (*tokptr) || *tokptr == '_')
825 {
826 char *result;
827 do {
828 tokptr++;
829 } while (isalnum (*tokptr) || (*tokptr == '_'));
830 yylval.sval.ptr = lexptr;
831 yylval.sval.length = tokptr - lexptr;
832 lexptr = tokptr;
833 result = copy_name (yylval.sval);
834 return result;
835 }
836 return (NULL);
837 }
838
839 /* Start looking for a value composed of valid digits as set by the base
840 in use. Note that '_' characters are valid anywhere, in any quantity,
841 and are simply ignored. Since we must find at least one valid digit,
842 or reject this token as an integer literal, we keep track of how many
843 digits we have encountered. */
844
845 static int
846 decode_integer_value (base, tokptrptr, ivalptr)
847 int base;
848 char **tokptrptr;
849 int *ivalptr;
850 {
851 char *tokptr = *tokptrptr;
852 int temp;
853 int digits = 0;
854
855 while (*tokptr != '\0')
856 {
857 temp = *tokptr;
858 if (isupper (temp))
859 temp = tolower (temp);
860 tokptr++;
861 switch (temp)
862 {
863 case '_':
864 continue;
865 case '0': case '1': case '2': case '3': case '4':
866 case '5': case '6': case '7': case '8': case '9':
867 temp -= '0';
868 break;
869 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
870 temp -= 'a';
871 temp += 10;
872 break;
873 default:
874 temp = base;
875 break;
876 }
877 if (temp < base)
878 {
879 digits++;
880 *ivalptr *= base;
881 *ivalptr += temp;
882 }
883 else
884 {
885 /* Found something not in domain for current base. */
886 tokptr--; /* Unconsume what gave us indigestion. */
887 break;
888 }
889 }
890
891 /* If we didn't find any digits, then we don't have a valid integer
892 value, so reject the entire token. Otherwise, update the lexical
893 scan pointer, and return non-zero for success. */
894
895 if (digits == 0)
896 {
897 return (0);
898 }
899 else
900 {
901 *tokptrptr = tokptr;
902 return (1);
903 }
904 }
905
906 static int
907 decode_integer_literal (valptr, tokptrptr)
908 int *valptr;
909 char **tokptrptr;
910 {
911 char *tokptr = *tokptrptr;
912 int base = 0;
913 int ival = 0;
914 int explicit_base = 0;
915
916 /* Look for an explicit base specifier, which is optional. */
917
918 switch (*tokptr)
919 {
920 case 'd':
921 case 'D':
922 explicit_base++;
923 base = 10;
924 tokptr++;
925 break;
926 case 'b':
927 case 'B':
928 explicit_base++;
929 base = 2;
930 tokptr++;
931 break;
932 case 'h':
933 case 'H':
934 explicit_base++;
935 base = 16;
936 tokptr++;
937 break;
938 case 'o':
939 case 'O':
940 explicit_base++;
941 base = 8;
942 tokptr++;
943 break;
944 default:
945 base = 10;
946 break;
947 }
948
949 /* If we found an explicit base ensure that the character after the
950 explicit base is a single quote. */
951
952 if (explicit_base && (*tokptr++ != '\''))
953 {
954 return (0);
955 }
956
957 /* Attempt to decode whatever follows as an integer value in the
958 indicated base, updating the token pointer in the process and
959 computing the value into ival. Also, if we have an explicit
960 base, then the next character must not be a single quote, or we
961 have a bitstring literal, so reject the entire token in this case.
962 Otherwise, update the lexical scan pointer, and return non-zero
963 for success. */
964
965 if (!decode_integer_value (base, &tokptr, &ival))
966 {
967 return (0);
968 }
969 else if (explicit_base && (*tokptr == '\''))
970 {
971 return (0);
972 }
973 else
974 {
975 *valptr = ival;
976 *tokptrptr = tokptr;
977 return (1);
978 }
979 }
980
981 /* If it wasn't for the fact that floating point values can contain '_'
982 characters, we could just let strtod do all the hard work by letting it
983 try to consume as much of the current token buffer as possible and
984 find a legal conversion. Unfortunately we need to filter out the '_'
985 characters before calling strtod, which we do by copying the other
986 legal chars to a local buffer to be converted. However since we also
987 need to keep track of where the last unconsumed character in the input
988 buffer is, we have transfer only as many characters as may compose a
989 legal floating point value. */
990
991 static int
992 match_float_literal ()
993 {
994 char *tokptr = lexptr;
995 char *buf;
996 char *copy;
997 double dval;
998 extern double strtod ();
999
1000 /* Make local buffer in which to build the string to convert. This is
1001 required because underscores are valid in chill floating point numbers
1002 but not in the string passed to strtod to convert. The string will be
1003 no longer than our input string. */
1004
1005 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1006
1007 /* Transfer all leading digits to the conversion buffer, discarding any
1008 underscores. */
1009
1010 while (isdigit (*tokptr) || *tokptr == '_')
1011 {
1012 if (*tokptr != '_')
1013 {
1014 *copy++ = *tokptr;
1015 }
1016 tokptr++;
1017 }
1018
1019 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1020 of whether we found any leading digits, and we simply accept it and
1021 continue on to look for the fractional part and/or exponent. One of
1022 [eEdD] is legal only if we have seen digits, and means that there
1023 is no fractional part. If we find neither of these, then this is
1024 not a floating point number, so return failure. */
1025
1026 switch (*tokptr++)
1027 {
1028 case '.':
1029 /* Accept and then look for fractional part and/or exponent. */
1030 *copy++ = '.';
1031 break;
1032
1033 case 'e':
1034 case 'E':
1035 case 'd':
1036 case 'D':
1037 if (copy == buf)
1038 {
1039 return (0);
1040 }
1041 *copy++ = 'e';
1042 goto collect_exponent;
1043 break;
1044
1045 default:
1046 return (0);
1047 break;
1048 }
1049
1050 /* We found a '.', copy any fractional digits to the conversion buffer, up
1051 to the first nondigit, non-underscore character. */
1052
1053 while (isdigit (*tokptr) || *tokptr == '_')
1054 {
1055 if (*tokptr != '_')
1056 {
1057 *copy++ = *tokptr;
1058 }
1059 tokptr++;
1060 }
1061
1062 /* Look for an exponent, which must start with one of [eEdD]. If none
1063 is found, jump directly to trying to convert what we have collected
1064 so far. */
1065
1066 switch (*tokptr)
1067 {
1068 case 'e':
1069 case 'E':
1070 case 'd':
1071 case 'D':
1072 *copy++ = 'e';
1073 tokptr++;
1074 break;
1075 default:
1076 goto convert_float;
1077 break;
1078 }
1079
1080 /* Accept an optional '-' or '+' following one of [eEdD]. */
1081
1082 collect_exponent:
1083 if (*tokptr == '+' || *tokptr == '-')
1084 {
1085 *copy++ = *tokptr++;
1086 }
1087
1088 /* Now copy an exponent into the conversion buffer. Note that at the
1089 moment underscores are *not* allowed in exponents. */
1090
1091 while (isdigit (*tokptr))
1092 {
1093 *copy++ = *tokptr++;
1094 }
1095
1096 /* If we transfered any chars to the conversion buffer, try to interpret its
1097 contents as a floating point value. If any characters remain, then we
1098 must not have a valid floating point string. */
1099
1100 convert_float:
1101 *copy = '\0';
1102 if (copy != buf)
1103 {
1104 dval = strtod (buf, &copy);
1105 if (*copy == '\0')
1106 {
1107 yylval.dval = dval;
1108 lexptr = tokptr;
1109 return (FLOAT_LITERAL);
1110 }
1111 }
1112 return (0);
1113 }
1114
1115 /* Recognize a string literal. A string literal is a sequence
1116 of characters enclosed in matching single or double quotes, except that
1117 a single character inside single quotes is a character literal, which
1118 we reject as a string literal. To embed the terminator character inside
1119 a string, it is simply doubled (I.E. "this""is""one""string") */
1120
1121 static int
1122 match_string_literal ()
1123 {
1124 char *tokptr = lexptr;
1125
1126 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1127 {
1128 CHECKBUF (1);
1129 if (*tokptr == *lexptr)
1130 {
1131 if (*(tokptr + 1) == *lexptr)
1132 {
1133 tokptr++;
1134 }
1135 else
1136 {
1137 break;
1138 }
1139 }
1140 tempbuf[tempbufindex++] = *tokptr;
1141 }
1142 if (*tokptr == '\0' /* no terminator */
1143 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1144 {
1145 return (0);
1146 }
1147 else
1148 {
1149 tempbuf[tempbufindex] = '\0';
1150 yylval.sval.ptr = tempbuf;
1151 yylval.sval.length = tempbufindex;
1152 lexptr = ++tokptr;
1153 return (CHARACTER_STRING_LITERAL);
1154 }
1155 }
1156
1157 /* Recognize a character literal. A character literal is single character
1158 or a control sequence, enclosed in single quotes. A control sequence
1159 is a comma separated list of one or more integer literals, enclosed
1160 in parenthesis and introduced with a circumflex character.
1161
1162 EX: 'a' '^(7)' '^(7,8)'
1163
1164 As a GNU chill extension, the syntax C'xx' is also recognized as a
1165 character literal, where xx is a hex value for the character.
1166
1167 Note that more than a single character, enclosed in single quotes, is
1168 a string literal.
1169
1170 Also note that the control sequence form is not in GNU Chill since it
1171 is ambiguous with the string literal form using single quotes. I.E.
1172 is '^(7)' a character literal or a string literal. In theory it it
1173 possible to tell by context, but GNU Chill doesn't accept the control
1174 sequence form, so neither do we (for now the code is disabled).
1175
1176 Returns CHARACTER_LITERAL if a match is found.
1177 */
1178
1179 static int
1180 match_character_literal ()
1181 {
1182 char *tokptr = lexptr;
1183 int ival = 0;
1184
1185 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
1186 {
1187 /* We have a GNU chill extension form, so skip the leading "C'",
1188 decode the hex value, and then ensure that we have a trailing
1189 single quote character. */
1190 tokptr += 2;
1191 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1192 {
1193 return (0);
1194 }
1195 tokptr++;
1196 }
1197 else if (*tokptr == '\'')
1198 {
1199 tokptr++;
1200
1201 /* Determine which form we have, either a control sequence or the
1202 single character form. */
1203
1204 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1205 {
1206 #if 0 /* Disable, see note above. -fnf */
1207 /* Match and decode a control sequence. Return zero if we don't
1208 find a valid integer literal, or if the next unconsumed character
1209 after the integer literal is not the trailing ')'.
1210 FIXME: We currently don't handle the multiple integer literal
1211 form. */
1212 tokptr += 2;
1213 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1214 {
1215 return (0);
1216 }
1217 #else
1218 return (0);
1219 #endif
1220 }
1221 else
1222 {
1223 ival = *tokptr++;
1224 }
1225
1226 /* The trailing quote has not yet been consumed. If we don't find
1227 it, then we have no match. */
1228
1229 if (*tokptr++ != '\'')
1230 {
1231 return (0);
1232 }
1233 }
1234 else
1235 {
1236 /* Not a character literal. */
1237 return (0);
1238 }
1239 yylval.typed_val.val = ival;
1240 yylval.typed_val.type = builtin_type_chill_char;
1241 lexptr = tokptr;
1242 return (CHARACTER_LITERAL);
1243 }
1244
1245 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1246 Note that according to 5.2.4.2, a single "_" is also a valid integer
1247 literal, however GNU-chill requires there to be at least one "digit"
1248 in any integer literal. */
1249
1250 static int
1251 match_integer_literal ()
1252 {
1253 char *tokptr = lexptr;
1254 int ival;
1255
1256 if (!decode_integer_literal (&ival, &tokptr))
1257 {
1258 return (0);
1259 }
1260 else
1261 {
1262 yylval.typed_val.val = ival;
1263 yylval.typed_val.type = builtin_type_int;
1264 lexptr = tokptr;
1265 return (INTEGER_LITERAL);
1266 }
1267 }
1268
1269 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1270 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1271 literal, however GNU-chill requires there to be at least one "digit"
1272 in any bit-string literal. */
1273
1274 static int
1275 match_bitstring_literal ()
1276 {
1277 register char *tokptr = lexptr;
1278 int bitoffset = 0;
1279 int bitcount = 0;
1280 int bits_per_char;
1281 int digit;
1282
1283 tempbufindex = 0;
1284 CHECKBUF (1);
1285 tempbuf[0] = 0;
1286
1287 /* Look for the required explicit base specifier. */
1288
1289 switch (*tokptr++)
1290 {
1291 case 'b':
1292 case 'B':
1293 bits_per_char = 1;
1294 break;
1295 case 'o':
1296 case 'O':
1297 bits_per_char = 3;
1298 break;
1299 case 'h':
1300 case 'H':
1301 bits_per_char = 4;
1302 break;
1303 default:
1304 return (0);
1305 break;
1306 }
1307
1308 /* Ensure that the character after the explicit base is a single quote. */
1309
1310 if (*tokptr++ != '\'')
1311 {
1312 return (0);
1313 }
1314
1315 while (*tokptr != '\0' && *tokptr != '\'')
1316 {
1317 digit = *tokptr;
1318 if (isupper (digit))
1319 digit = tolower (digit);
1320 tokptr++;
1321 switch (digit)
1322 {
1323 case '_':
1324 continue;
1325 case '0': case '1': case '2': case '3': case '4':
1326 case '5': case '6': case '7': case '8': case '9':
1327 digit -= '0';
1328 break;
1329 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1330 digit -= 'a';
1331 digit += 10;
1332 break;
1333 default:
1334 return (0);
1335 break;
1336 }
1337 if (digit >= 1 << bits_per_char)
1338 {
1339 /* Found something not in domain for current base. */
1340 return (0);
1341 }
1342 else
1343 {
1344 /* Extract bits from digit, packing them into the bitstring byte. */
1345 int k = TARGET_BYTE_ORDER == BIG_ENDIAN ? bits_per_char - 1 : 0;
1346 for (; TARGET_BYTE_ORDER == BIG_ENDIAN ? k >= 0 : k < bits_per_char;
1347 TARGET_BYTE_ORDER == BIG_ENDIAN ? k-- : k++)
1348 {
1349 bitcount++;
1350 if (digit & (1 << k))
1351 {
1352 tempbuf[tempbufindex] |=
1353 (TARGET_BYTE_ORDER == BIG_ENDIAN)
1354 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1355 : (1 << bitoffset);
1356 }
1357 bitoffset++;
1358 if (bitoffset == HOST_CHAR_BIT)
1359 {
1360 bitoffset = 0;
1361 tempbufindex++;
1362 CHECKBUF(1);
1363 tempbuf[tempbufindex] = 0;
1364 }
1365 }
1366 }
1367 }
1368
1369 /* Verify that we consumed everything up to the trailing single quote,
1370 and that we found some bits (IE not just underbars). */
1371
1372 if (*tokptr++ != '\'')
1373 {
1374 return (0);
1375 }
1376 else
1377 {
1378 yylval.sval.ptr = tempbuf;
1379 yylval.sval.length = bitcount;
1380 lexptr = tokptr;
1381 return (BIT_STRING_LITERAL);
1382 }
1383 }
1384
1385 /* Recognize tokens that start with '$'. These include:
1386
1387 $regname A native register name or a "standard
1388 register name".
1389 Return token GDB_REGNAME.
1390
1391 $variable A convenience variable with a name chosen
1392 by the user.
1393 Return token GDB_VARIABLE.
1394
1395 $digits Value history with index <digits>, starting
1396 from the first value which has index 1.
1397 Return GDB_LAST.
1398
1399 $$digits Value history with index <digits> relative
1400 to the last value. I.E. $$0 is the last
1401 value, $$1 is the one previous to that, $$2
1402 is the one previous to $$1, etc.
1403 Return token GDB_LAST.
1404
1405 $ | $0 | $$0 The last value in the value history.
1406 Return token GDB_LAST.
1407
1408 $$ An abbreviation for the second to the last
1409 value in the value history, I.E. $$1
1410 Return token GDB_LAST.
1411
1412 Note that we currently assume that register names and convenience
1413 variables follow the convention of starting with a letter or '_'.
1414
1415 */
1416
1417 static int
1418 match_dollar_tokens ()
1419 {
1420 char *tokptr;
1421 int regno;
1422 int namelength;
1423 int negate;
1424 int ival;
1425
1426 /* We will always have a successful match, even if it is just for
1427 a single '$', the abbreviation for $$0. So advance lexptr. */
1428
1429 tokptr = ++lexptr;
1430
1431 if (*tokptr == '_' || isalpha (*tokptr))
1432 {
1433 /* Look for a match with a native register name, usually something
1434 like "r0" for example. */
1435
1436 for (regno = 0; regno < NUM_REGS; regno++)
1437 {
1438 namelength = strlen (reg_names[regno]);
1439 if (STREQN (tokptr, reg_names[regno], namelength)
1440 && !isalnum (tokptr[namelength]))
1441 {
1442 yylval.lval = regno;
1443 lexptr += namelength;
1444 return (GDB_REGNAME);
1445 }
1446 }
1447
1448 /* Look for a match with a standard register name, usually something
1449 like "pc", which gdb always recognizes as the program counter
1450 regardless of what the native register name is. */
1451
1452 for (regno = 0; regno < num_std_regs; regno++)
1453 {
1454 namelength = strlen (std_regs[regno].name);
1455 if (STREQN (tokptr, std_regs[regno].name, namelength)
1456 && !isalnum (tokptr[namelength]))
1457 {
1458 yylval.lval = std_regs[regno].regnum;
1459 lexptr += namelength;
1460 return (GDB_REGNAME);
1461 }
1462 }
1463
1464 /* Attempt to match against a convenience variable. Note that
1465 this will always succeed, because if no variable of that name
1466 already exists, the lookup_internalvar will create one for us.
1467 Also note that both lexptr and tokptr currently point to the
1468 start of the input string we are trying to match, and that we
1469 have already tested the first character for non-numeric, so we
1470 don't have to treat it specially. */
1471
1472 while (*tokptr == '_' || isalnum (*tokptr))
1473 {
1474 tokptr++;
1475 }
1476 yylval.sval.ptr = lexptr;
1477 yylval.sval.length = tokptr - lexptr;
1478 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1479 lexptr = tokptr;
1480 return (GDB_VARIABLE);
1481 }
1482
1483 /* Since we didn't match against a register name or convenience
1484 variable, our only choice left is a history value. */
1485
1486 if (*tokptr == '$')
1487 {
1488 negate = 1;
1489 ival = 1;
1490 tokptr++;
1491 }
1492 else
1493 {
1494 negate = 0;
1495 ival = 0;
1496 }
1497
1498 /* Attempt to decode more characters as an integer value giving
1499 the index in the history list. If successful, the value will
1500 overwrite ival (currently 0 or 1), and if not, ival will be
1501 left alone, which is good since it is currently correct for
1502 the '$' or '$$' case. */
1503
1504 decode_integer_literal (&ival, &tokptr);
1505 yylval.lval = negate ? -ival : ival;
1506 lexptr = tokptr;
1507 return (GDB_LAST);
1508 }
1509
1510 struct token
1511 {
1512 char *operator;
1513 int token;
1514 };
1515
1516 static const struct token idtokentab[] =
1517 {
1518 { "array", ARRAY },
1519 { "length", LENGTH },
1520 { "lower", LOWER },
1521 { "upper", UPPER },
1522 { "andif", ANDIF },
1523 { "pred", PRED },
1524 { "succ", SUCC },
1525 { "card", CARD },
1526 { "size", SIZE },
1527 { "orif", ORIF },
1528 { "num", NUM },
1529 { "abs", ABS },
1530 { "max", MAX_TOKEN },
1531 { "min", MIN_TOKEN },
1532 { "mod", MOD },
1533 { "rem", REM },
1534 { "not", NOT },
1535 { "xor", LOGXOR },
1536 { "and", LOGAND },
1537 { "in", IN },
1538 { "or", LOGIOR },
1539 { "up", UP },
1540 { "addr", ADDR_TOKEN },
1541 { "null", EMPTINESS_LITERAL }
1542 };
1543
1544 static const struct token tokentab2[] =
1545 {
1546 { ":=", GDB_ASSIGNMENT },
1547 { "//", SLASH_SLASH },
1548 { "->", POINTER },
1549 { "/=", NOTEQUAL },
1550 { "<=", LEQ },
1551 { ">=", GTR }
1552 };
1553
1554 /* Read one token, getting characters through lexptr. */
1555 /* This is where we will check to make sure that the language and the
1556 operators used are compatible. */
1557
1558 static int
1559 yylex ()
1560 {
1561 unsigned int i;
1562 int token;
1563 char *inputname;
1564 struct symbol *sym;
1565
1566 /* Skip over any leading whitespace. */
1567 while (isspace (*lexptr))
1568 {
1569 lexptr++;
1570 }
1571 /* Look for special single character cases which can't be the first
1572 character of some other multicharacter token. */
1573 switch (*lexptr)
1574 {
1575 case '\0':
1576 return (0);
1577 case ',':
1578 case '=':
1579 case ';':
1580 case '!':
1581 case '+':
1582 case '*':
1583 case '(':
1584 case ')':
1585 case '[':
1586 case ']':
1587 return (*lexptr++);
1588 }
1589 /* Look for characters which start a particular kind of multicharacter
1590 token, such as a character literal, register name, convenience
1591 variable name, string literal, etc. */
1592 switch (*lexptr)
1593 {
1594 case '\'':
1595 case '\"':
1596 /* First try to match a string literal, which is any
1597 sequence of characters enclosed in matching single or double
1598 quotes, except that a single character inside single quotes
1599 is a character literal, so we have to catch that case also. */
1600 token = match_string_literal ();
1601 if (token != 0)
1602 {
1603 return (token);
1604 }
1605 if (*lexptr == '\'')
1606 {
1607 token = match_character_literal ();
1608 if (token != 0)
1609 {
1610 return (token);
1611 }
1612 }
1613 break;
1614 case 'C':
1615 case 'c':
1616 token = match_character_literal ();
1617 if (token != 0)
1618 {
1619 return (token);
1620 }
1621 break;
1622 case '$':
1623 token = match_dollar_tokens ();
1624 if (token != 0)
1625 {
1626 return (token);
1627 }
1628 break;
1629 }
1630 /* See if it is a special token of length 2. */
1631 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1632 {
1633 if (STREQN (lexptr, tokentab2[i].operator, 2))
1634 {
1635 lexptr += 2;
1636 return (tokentab2[i].token);
1637 }
1638 }
1639 /* Look for single character cases which which could be the first
1640 character of some other multicharacter token, but aren't, or we
1641 would already have found it. */
1642 switch (*lexptr)
1643 {
1644 case '-':
1645 case ':':
1646 case '/':
1647 case '<':
1648 case '>':
1649 return (*lexptr++);
1650 }
1651 /* Look for a float literal before looking for an integer literal, so
1652 we match as much of the input stream as possible. */
1653 token = match_float_literal ();
1654 if (token != 0)
1655 {
1656 return (token);
1657 }
1658 token = match_bitstring_literal ();
1659 if (token != 0)
1660 {
1661 return (token);
1662 }
1663 token = match_integer_literal ();
1664 if (token != 0)
1665 {
1666 return (token);
1667 }
1668
1669 /* Try to match a simple name string, and if a match is found, then
1670 further classify what sort of name it is and return an appropriate
1671 token. Note that attempting to match a simple name string consumes
1672 the token from lexptr, so we can't back out if we later find that
1673 we can't classify what sort of name it is. */
1674
1675 inputname = match_simple_name_string ();
1676
1677 if (inputname != NULL)
1678 {
1679 char *simplename = (char*) alloca (strlen (inputname) + 1);
1680
1681 char *dptr = simplename, *sptr = inputname;
1682 for (; *sptr; sptr++)
1683 *dptr++ = isupper (*sptr) ? tolower(*sptr) : *sptr;
1684 *dptr = '\0';
1685
1686 /* See if it is a reserved identifier. */
1687 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
1688 {
1689 if (STREQ (simplename, idtokentab[i].operator))
1690 {
1691 return (idtokentab[i].token);
1692 }
1693 }
1694
1695 /* Look for other special tokens. */
1696 if (STREQ (simplename, "true"))
1697 {
1698 yylval.ulval = 1;
1699 return (BOOLEAN_LITERAL);
1700 }
1701 if (STREQ (simplename, "false"))
1702 {
1703 yylval.ulval = 0;
1704 return (BOOLEAN_LITERAL);
1705 }
1706
1707 sym = lookup_symbol (inputname, expression_context_block,
1708 VAR_NAMESPACE, (int *) NULL,
1709 (struct symtab **) NULL);
1710 if (sym == NULL && strcmp (inputname, simplename) != 0)
1711 {
1712 sym = lookup_symbol (simplename, expression_context_block,
1713 VAR_NAMESPACE, (int *) NULL,
1714 (struct symtab **) NULL);
1715 }
1716 if (sym != NULL)
1717 {
1718 yylval.ssym.stoken.ptr = NULL;
1719 yylval.ssym.stoken.length = 0;
1720 yylval.ssym.sym = sym;
1721 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1722 switch (SYMBOL_CLASS (sym))
1723 {
1724 case LOC_BLOCK:
1725 /* Found a procedure name. */
1726 return (GENERAL_PROCEDURE_NAME);
1727 case LOC_STATIC:
1728 /* Found a global or local static variable. */
1729 return (LOCATION_NAME);
1730 case LOC_REGISTER:
1731 case LOC_ARG:
1732 case LOC_REF_ARG:
1733 case LOC_REGPARM:
1734 case LOC_REGPARM_ADDR:
1735 case LOC_LOCAL:
1736 case LOC_LOCAL_ARG:
1737 case LOC_BASEREG:
1738 case LOC_BASEREG_ARG:
1739 if (innermost_block == NULL
1740 || contained_in (block_found, innermost_block))
1741 {
1742 innermost_block = block_found;
1743 }
1744 return (LOCATION_NAME);
1745 break;
1746 case LOC_CONST:
1747 case LOC_LABEL:
1748 return (LOCATION_NAME);
1749 break;
1750 case LOC_TYPEDEF:
1751 yylval.tsym.type = SYMBOL_TYPE (sym);
1752 return TYPENAME;
1753 case LOC_UNDEF:
1754 case LOC_CONST_BYTES:
1755 case LOC_OPTIMIZED_OUT:
1756 error ("Symbol \"%s\" names no location.", inputname);
1757 break;
1758 }
1759 }
1760 else if (!have_full_symbols () && !have_partial_symbols ())
1761 {
1762 error ("No symbol table is loaded. Use the \"file\" command.");
1763 }
1764 else
1765 {
1766 error ("No symbol \"%s\" in current context.", inputname);
1767 }
1768 }
1769
1770 /* Catch single character tokens which are not part of some
1771 longer token. */
1772
1773 switch (*lexptr)
1774 {
1775 case '.': /* Not float for example. */
1776 lexptr++;
1777 while (isspace (*lexptr)) lexptr++;
1778 inputname = match_simple_name_string ();
1779 if (!inputname)
1780 return '.';
1781 return FIELD_NAME;
1782 }
1783
1784 return (ILLEGAL_TOKEN);
1785 }
1786
1787 static void
1788 write_lower_upper_value (opcode, type)
1789 enum exp_opcode opcode; /* Either UNOP_LOWER or UNOP_UPPER */
1790 struct type *type;
1791 {
1792 extern LONGEST type_lower_upper ();
1793 struct type *result_type;
1794 LONGEST val = type_lower_upper (opcode, type, &result_type);
1795 write_exp_elt_opcode (OP_LONG);
1796 write_exp_elt_type (result_type);
1797 write_exp_elt_longcst (val);
1798 write_exp_elt_opcode (OP_LONG);
1799 }
1800
1801 void
1802 yyerror (msg)
1803 char *msg;
1804 {
1805 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1806 }