1 /* Parser for GNU CHILL (CCITT High-Level Language) -*- C -*-
2 Copyright (C) 1992, 1993, 1995 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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,
19 Boston, MA 02111-1307, USA. */
21 /* Parse a Chill expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that the language accepted by this parser is more liberal
31 than the one accepted by an actual Chill compiler. For example, the
32 language rule that a simple name string can not be one of the reserved
33 simple name strings is not enforced (e.g "case" is not treated as a
34 reserved name). Another example is that Chill is a strongly typed
35 language, and certain expressions that violate the type constraints
36 may still be evaluated if gdb can do so in a meaningful manner, while
37 such expressions would be rejected by the compiler. The reason for
38 this more liberal behavior is the philosophy that the debugger
39 is intended to be a tool that is used by the programmer when things
40 go wrong, and as such, it should provide as few artificial barriers
41 to it's use as possible. If it can do something meaningful, even
42 something that violates language contraints that are enforced by the
43 compiler, it should do so without complaint.
48 #include "gdb_string.h"
50 #include "expression.h"
53 #include "parser-defs.h"
55 #include "bfd.h" /* Required by objfiles.h. */
56 #include "symfile.h" /* Required by objfiles.h. */
57 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
60 #define INLINE __inline__
86 /* '\001' ... '\xff' come first. */
93 GENERAL_PROCEDURE_NAME
,
96 CHARACTER_STRING_LITERAL
,
99 DOT_FIELD_NAME
, /* '.' followed by <field name> */
142 /* Forward declarations. */
144 static void write_lower_upper_value (enum exp_opcode
, struct type
*);
145 static enum ch_terminal
match_bitstring_literal (void);
146 static enum ch_terminal
match_integer_literal (void);
147 static enum ch_terminal
match_character_literal (void);
148 static enum ch_terminal
match_string_literal (void);
149 static enum ch_terminal
match_float_literal (void);
150 static enum ch_terminal
match_float_literal (void);
151 static int decode_integer_literal (LONGEST
*, char **);
152 static int decode_integer_value (int, char **, LONGEST
*);
153 static char *match_simple_name_string (void);
154 static void growbuf_by_size (int);
155 static void parse_untyped_expr (void);
156 static void parse_if_expression (void);
157 static void parse_else_alternative (void);
158 static void parse_then_alternative (void);
159 static void parse_expr (void);
160 static void parse_operand0 (void);
161 static void parse_operand1 (void);
162 static void parse_operand2 (void);
163 static void parse_operand3 (void);
164 static void parse_operand4 (void);
165 static void parse_operand5 (void);
166 static void parse_operand6 (void);
167 static void parse_primval (void);
168 static void parse_tuple (struct type
*);
169 static void parse_opt_element_list (struct type
*);
170 static void parse_tuple_element (struct type
*);
171 static void parse_named_record_element (void);
172 static void parse_call (void);
173 static struct type
*parse_mode_or_normal_call (void);
175 static struct type
*parse_mode_call (void);
177 static void parse_unary_call (void);
178 static int parse_opt_untyped_expr (void);
179 static void parse_case_label (void);
180 static int expect (enum ch_terminal
, char *);
181 static void parse_expr (void);
182 static void parse_primval (void);
183 static void parse_untyped_expr (void);
184 static int parse_opt_untyped_expr (void);
185 static void parse_if_expression_body (void);
186 static enum ch_terminal
ch_lex (void);
187 INLINE
static enum ch_terminal
PEEK_TOKEN (void);
188 static enum ch_terminal
peek_token_ (int);
189 static void forward_token_ (void);
190 static void require (enum ch_terminal
);
191 static int check_token (enum ch_terminal
);
193 #define MAX_LOOK_AHEAD 2
194 static enum ch_terminal terminal_buffer
[MAX_LOOK_AHEAD
+ 1] =
196 TOKEN_NOT_READ
, TOKEN_NOT_READ
, TOKEN_NOT_READ
};
197 static YYSTYPE yylval
;
198 static YYSTYPE val_buffer
[MAX_LOOK_AHEAD
+ 1];
200 /*int current_token, lookahead_token; */
202 INLINE
static enum ch_terminal
205 if (terminal_buffer
[0] == TOKEN_NOT_READ
)
207 terminal_buffer
[0] = ch_lex ();
208 val_buffer
[0] = yylval
;
210 return terminal_buffer
[0];
212 #define PEEK_LVAL() val_buffer[0]
213 #define PEEK_TOKEN1() peek_token_(1)
214 #define PEEK_TOKEN2() peek_token_(2)
215 static enum ch_terminal
218 if (i
> MAX_LOOK_AHEAD
)
219 internal_error ("ch-exp.c - too much lookahead");
220 if (terminal_buffer
[i
] == TOKEN_NOT_READ
)
222 terminal_buffer
[i
] = ch_lex ();
223 val_buffer
[i
] = yylval
;
225 return terminal_buffer
[i
];
231 pushback_token (enum ch_terminal code
, YYSTYPE node
)
234 if (terminal_buffer
[MAX_LOOK_AHEAD
] != TOKEN_NOT_READ
)
235 internal_error ("ch-exp.c - cannot pushback token");
236 for (i
= MAX_LOOK_AHEAD
; i
> 0; i
--)
238 terminal_buffer
[i
] = terminal_buffer
[i
- 1];
239 val_buffer
[i
] = val_buffer
[i
- 1];
241 terminal_buffer
[0] = code
;
242 val_buffer
[0] = node
;
248 forward_token_ (void)
251 for (i
= 0; i
< MAX_LOOK_AHEAD
; i
++)
253 terminal_buffer
[i
] = terminal_buffer
[i
+ 1];
254 val_buffer
[i
] = val_buffer
[i
+ 1];
256 terminal_buffer
[MAX_LOOK_AHEAD
] = TOKEN_NOT_READ
;
258 #define FORWARD_TOKEN() forward_token_()
260 /* Skip the next token.
261 if it isn't TOKEN, the parser is broken. */
264 require (enum ch_terminal token
)
266 if (PEEK_TOKEN () != token
)
268 internal_error ("ch-exp.c - expected token %d", (int) token
);
274 check_token (enum ch_terminal token
)
276 if (PEEK_TOKEN () != token
)
282 /* return 0 if expected token was not found,
286 expect (enum ch_terminal token
, char *message
)
288 if (PEEK_TOKEN () != token
)
292 else if (token
< 256)
293 error ("syntax error - expected a '%c' here \"%s\"", token
, lexptr
);
295 error ("syntax error");
304 /* Parse a name string. If ALLOW_ALL is 1, ALL is allowed as a postfix. */
307 parse_opt_name_string (int allow_all
)
309 int token
= PEEK_TOKEN ();
313 if (token
== ALL
&& allow_all
)
324 token
= PEEK_TOKEN ();
328 token
= PEEK_TOKEN ();
329 if (token
== ALL
&& allow_all
)
330 return get_identifier3 (IDENTIFIER_POINTER (name
), "!", "*");
334 error ("'%s!' is not followed by an identifier",
335 IDENTIFIER_POINTER (name
));
338 name
= get_identifier3 (IDENTIFIER_POINTER (name
),
339 "!", IDENTIFIER_POINTER (PEEK_LVAL ()));
344 parse_simple_name_string (void)
346 int token
= PEEK_TOKEN ();
350 error ("expected a name here");
351 return error_mark_node
;
359 parse_name_string (void)
361 tree name
= parse_opt_name_string (0);
365 error ("expected a name string here");
366 return error_mark_node
;
369 /* Matches: <name_string>
370 Returns if pass 1: the identifier.
371 Returns if pass 2: a decl or value for identifier. */
376 tree name
= parse_name_string ();
377 if (pass
== 1 || ignoring
)
381 tree decl
= lookup_name (name
);
382 if (decl
== NULL_TREE
)
384 error ("`%s' undeclared", IDENTIFIER_POINTER (name
));
385 return error_mark_node
;
387 else if (TREE_CODE (TREE_TYPE (decl
)) == ERROR_MARK
)
388 return error_mark_node
;
389 else if (TREE_CODE (decl
) == CONST_DECL
)
390 return DECL_INITIAL (decl
);
391 else if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
392 return convert_from_reference (decl
);
401 pushback_paren_expr (tree expr
)
403 if (pass
== 1 && !ignoring
)
404 expr
= build1 (PAREN_EXPR
, NULL_TREE
, expr
);
405 pushback_token (EXPR
, expr
);
409 /* Matches: <case label> */
412 parse_case_label (void)
414 if (check_token (ELSE
))
415 error ("ELSE in tuples labels not implemented");
416 /* Does not handle the case of a mode name. FIXME */
418 if (check_token (':'))
421 write_exp_elt_opcode (BINOP_RANGE
);
426 parse_opt_untyped_expr (void)
428 switch (PEEK_TOKEN ())
435 parse_untyped_expr ();
441 parse_unary_call (void)
449 /* Parse NAME '(' MODENAME ')'. */
454 parse_mode_call (void)
459 if (PEEK_TOKEN () != TYPENAME
)
460 error ("expect MODENAME here `%s'", lexptr
);
461 type
= PEEK_LVAL ().tsym
.type
;
470 parse_mode_or_normal_call (void)
475 if (PEEK_TOKEN () == TYPENAME
)
477 type
= PEEK_LVAL ().tsym
.type
;
489 /* Parse something that looks like a function call.
490 Assume we have parsed the function, and are at the '('. */
497 /* This is to save the value of arglist_len
498 being accumulated for each dimension. */
500 if (parse_opt_untyped_expr ())
502 int tok
= PEEK_TOKEN ();
504 if (tok
== UP
|| tok
== ':')
508 expect (')', "expected ')' to terminate slice");
510 write_exp_elt_opcode (tok
== UP
? TERNOP_SLICE_COUNT
514 while (check_token (','))
516 parse_untyped_expr ();
523 arg_count
= end_arglist ();
524 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
525 write_exp_elt_longcst (arg_count
);
526 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
530 parse_named_record_element (void)
535 label
= PEEK_LVAL ().sval
;
536 sprintf (buf
, "expected a field name here `%s'", lexptr
);
537 expect (DOT_FIELD_NAME
, buf
);
538 if (check_token (','))
539 parse_named_record_element ();
540 else if (check_token (':'))
543 error ("syntax error near `%s' in named record tuple element", lexptr
);
544 write_exp_elt_opcode (OP_LABELED
);
545 write_exp_string (label
);
546 write_exp_elt_opcode (OP_LABELED
);
549 /* Returns one or more TREE_LIST nodes, in reverse order. */
552 parse_tuple_element (struct type
*type
)
554 if (PEEK_TOKEN () == DOT_FIELD_NAME
)
556 /* Parse a labelled structure tuple. */
557 parse_named_record_element ();
561 if (check_token ('('))
563 if (check_token ('*'))
565 expect (')', "missing ')' after '*' case label list");
568 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
570 /* do this as a range from low to high */
571 struct type
*range_type
= TYPE_FIELD_TYPE (type
, 0);
572 LONGEST low_bound
, high_bound
;
573 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
574 error ("cannot determine bounds for (*)");
576 write_exp_elt_opcode (OP_LONG
);
577 write_exp_elt_type (range_type
);
578 write_exp_elt_longcst (low_bound
);
579 write_exp_elt_opcode (OP_LONG
);
581 write_exp_elt_opcode (OP_LONG
);
582 write_exp_elt_type (range_type
);
583 write_exp_elt_longcst (high_bound
);
584 write_exp_elt_opcode (OP_LONG
);
585 write_exp_elt_opcode (BINOP_RANGE
);
588 error ("(*) in invalid context");
591 error ("(*) only possible with modename in front of tuple (mode[..])");
596 while (check_token (','))
599 write_exp_elt_opcode (BINOP_COMMA
);
605 parse_untyped_expr ();
606 if (check_token (':'))
608 /* A powerset range or a labeled Array. */
609 parse_untyped_expr ();
610 write_exp_elt_opcode (BINOP_RANGE
);
614 /* Matches: a COMMA-separated list of tuple elements.
615 Returns a list (of TREE_LIST nodes). */
617 parse_opt_element_list (struct type
*type
)
620 if (PEEK_TOKEN () == ']')
624 parse_tuple_element (type
);
626 if (PEEK_TOKEN () == ']')
628 if (!check_token (','))
629 error ("bad syntax in tuple");
633 /* Parses: '[' elements ']'
634 If modename is non-NULL it prefixed the tuple. */
637 parse_tuple (struct type
*mode
)
641 type
= check_typedef (mode
);
646 parse_opt_element_list (type
);
647 expect (']', "missing ']' after tuple");
648 write_exp_elt_opcode (OP_ARRAY
);
649 write_exp_elt_longcst ((LONGEST
) 0);
650 write_exp_elt_longcst ((LONGEST
) end_arglist () - 1);
651 write_exp_elt_opcode (OP_ARRAY
);
654 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
655 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
656 && TYPE_CODE (type
) != TYPE_CODE_SET
)
657 error ("invalid tuple mode");
658 write_exp_elt_opcode (UNOP_CAST
);
659 write_exp_elt_type (mode
);
660 write_exp_elt_opcode (UNOP_CAST
);
670 switch (PEEK_TOKEN ())
672 case INTEGER_LITERAL
:
673 case CHARACTER_LITERAL
:
674 write_exp_elt_opcode (OP_LONG
);
675 write_exp_elt_type (PEEK_LVAL ().typed_val
.type
);
676 write_exp_elt_longcst (PEEK_LVAL ().typed_val
.val
);
677 write_exp_elt_opcode (OP_LONG
);
680 case BOOLEAN_LITERAL
:
681 write_exp_elt_opcode (OP_BOOL
);
682 write_exp_elt_longcst ((LONGEST
) PEEK_LVAL ().ulval
);
683 write_exp_elt_opcode (OP_BOOL
);
687 write_exp_elt_opcode (OP_DOUBLE
);
688 write_exp_elt_type (builtin_type_double
);
689 write_exp_elt_dblcst (PEEK_LVAL ().dval
);
690 write_exp_elt_opcode (OP_DOUBLE
);
693 case EMPTINESS_LITERAL
:
694 write_exp_elt_opcode (OP_LONG
);
695 write_exp_elt_type (lookup_pointer_type (builtin_type_void
));
696 write_exp_elt_longcst (0);
697 write_exp_elt_opcode (OP_LONG
);
700 case CHARACTER_STRING_LITERAL
:
701 write_exp_elt_opcode (OP_STRING
);
702 write_exp_string (PEEK_LVAL ().sval
);
703 write_exp_elt_opcode (OP_STRING
);
706 case BIT_STRING_LITERAL
:
707 write_exp_elt_opcode (OP_BITSTRING
);
708 write_exp_bitstring (PEEK_LVAL ().sval
);
709 write_exp_elt_opcode (OP_BITSTRING
);
714 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
715 which casts to an artificial array. */
718 if (PEEK_TOKEN () != TYPENAME
)
719 error ("missing MODENAME after ARRAY()");
720 type
= PEEK_LVAL ().tsym
.type
;
724 expect (')', "missing right parenthesis");
725 type
= create_array_type ((struct type
*) NULL
, type
,
726 create_range_type ((struct type
*) NULL
,
727 builtin_type_int
, 0, 0));
728 TYPE_ARRAY_UPPER_BOUND_TYPE (type
) = BOUND_CANNOT_BE_DETERMINED
;
729 write_exp_elt_opcode (UNOP_CAST
);
730 write_exp_elt_type (type
);
731 write_exp_elt_opcode (UNOP_CAST
);
743 expect (')', "missing right parenthesis");
748 case GENERAL_PROCEDURE_NAME
:
750 write_exp_elt_opcode (OP_VAR_VALUE
);
751 write_exp_elt_block (NULL
);
752 write_exp_elt_sym (PEEK_LVAL ().ssym
.sym
);
753 write_exp_elt_opcode (OP_VAR_VALUE
);
756 case GDB_VARIABLE
: /* gdb specific */
761 write_exp_elt_opcode (UNOP_CAST
);
762 write_exp_elt_type (builtin_type_int
);
763 write_exp_elt_opcode (UNOP_CAST
);
767 write_exp_elt_opcode (UNOP_CARD
);
771 write_exp_elt_opcode (UNOP_CHMAX
);
775 write_exp_elt_opcode (UNOP_CHMIN
);
779 goto unimplemented_unary_builtin
;
782 goto unimplemented_unary_builtin
;
785 goto unimplemented_unary_builtin
;
786 unimplemented_unary_builtin
:
788 error ("not implemented: %s builtin function", op_name
);
792 write_exp_elt_opcode (UNOP_ADDR
);
795 type
= parse_mode_or_normal_call ();
798 write_exp_elt_opcode (OP_LONG
);
799 write_exp_elt_type (builtin_type_int
);
800 CHECK_TYPEDEF (type
);
801 write_exp_elt_longcst ((LONGEST
) TYPE_LENGTH (type
));
802 write_exp_elt_opcode (OP_LONG
);
805 write_exp_elt_opcode (UNOP_SIZEOF
);
814 type
= parse_mode_or_normal_call ();
815 write_lower_upper_value (op
, type
);
819 write_exp_elt_opcode (UNOP_LENGTH
);
822 type
= PEEK_LVAL ().tsym
.type
;
824 switch (PEEK_TOKEN ())
832 expect (')', "missing right parenthesis");
833 write_exp_elt_opcode (UNOP_CAST
);
834 write_exp_elt_type (type
);
835 write_exp_elt_opcode (UNOP_CAST
);
838 error ("typename in invalid context");
843 error ("invalid expression syntax at `%s'", lexptr
);
847 switch (PEEK_TOKEN ())
850 write_exp_elt_opcode (STRUCTOP_STRUCT
);
851 write_exp_string (PEEK_LVAL ().sval
);
852 write_exp_elt_opcode (STRUCTOP_STRUCT
);
857 if (PEEK_TOKEN () == TYPENAME
)
859 type
= PEEK_LVAL ().tsym
.type
;
860 write_exp_elt_opcode (UNOP_CAST
);
861 write_exp_elt_type (lookup_pointer_type (type
));
862 write_exp_elt_opcode (UNOP_CAST
);
865 write_exp_elt_opcode (UNOP_IND
);
870 case CHARACTER_STRING_LITERAL
:
871 case CHARACTER_LITERAL
:
872 case BIT_STRING_LITERAL
:
873 /* Handle string repetition. (See comment in parse_operand5.) */
875 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
876 write_exp_elt_longcst (1);
877 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
881 case INTEGER_LITERAL
:
882 case BOOLEAN_LITERAL
:
884 case GENERAL_PROCEDURE_NAME
:
886 case EMPTINESS_LITERAL
:
935 parse_operand6 (void)
937 if (check_token (RECEIVE
))
940 error ("not implemented: RECEIVE expression");
942 else if (check_token (POINTER
))
945 write_exp_elt_opcode (UNOP_ADDR
);
952 parse_operand5 (void)
955 /* We are supposed to be looking for a <string repetition operator>,
956 but in general we can't distinguish that from a parenthesized
957 expression. This is especially difficult if we allow the
958 string operand to be a constant expression (as requested by
959 some users), and not just a string literal.
960 Consider: LPRN expr RPRN LPRN expr RPRN
961 Is that a function call or string repetition?
962 Instead, we handle string repetition in parse_primval,
963 and build_generalized_call. */
964 switch (PEEK_TOKEN ())
967 op
= UNOP_LOGICAL_NOT
;
979 write_exp_elt_opcode (op
);
983 parse_operand4 (void)
989 switch (PEEK_TOKEN ())
1008 write_exp_elt_opcode (op
);
1013 parse_operand3 (void)
1019 switch (PEEK_TOKEN ())
1035 write_exp_elt_opcode (op
);
1040 parse_operand2 (void)
1046 if (check_token (IN
))
1049 write_exp_elt_opcode (BINOP_IN
);
1053 switch (PEEK_TOKEN ())
1071 op
= BINOP_NOTEQUAL
;
1078 write_exp_elt_opcode (op
);
1084 parse_operand1 (void)
1090 switch (PEEK_TOKEN ())
1093 op
= BINOP_BITWISE_AND
;
1096 op
= BINOP_LOGICAL_AND
;
1103 write_exp_elt_opcode (op
);
1108 parse_operand0 (void)
1114 switch (PEEK_TOKEN ())
1117 op
= BINOP_BITWISE_IOR
;
1120 op
= BINOP_BITWISE_XOR
;
1123 op
= BINOP_LOGICAL_OR
;
1130 write_exp_elt_opcode (op
);
1138 if (check_token (GDB_ASSIGNMENT
))
1141 write_exp_elt_opcode (BINOP_ASSIGN
);
1146 parse_then_alternative (void)
1148 expect (THEN
, "missing 'THEN' in 'IF' expression");
1153 parse_else_alternative (void)
1155 if (check_token (ELSIF
))
1156 parse_if_expression_body ();
1157 else if (check_token (ELSE
))
1160 error ("missing ELSE/ELSIF in IF expression");
1163 /* Matches: <boolean expression> <then alternative> <else alternative> */
1166 parse_if_expression_body (void)
1169 parse_then_alternative ();
1170 parse_else_alternative ();
1171 write_exp_elt_opcode (TERNOP_COND
);
1175 parse_if_expression (void)
1178 parse_if_expression_body ();
1179 expect (FI
, "missing 'FI' at end of conditional expression");
1182 /* An <untyped_expr> is a superset of <expr>. It also includes
1183 <conditional expressions> and untyped <tuples>, whose types
1184 are not given by their constituents. Hence, these are only
1185 allowed in certain contexts that expect a certain type.
1186 You should call convert() to fix up the <untyped_expr>. */
1189 parse_untyped_expr (void)
1191 switch (PEEK_TOKEN ())
1194 parse_if_expression ();
1197 error ("not implemented: CASE expression");
1199 switch (PEEK_TOKEN1 ())
1207 parse_untyped_expr ();
1208 expect (')', "missing ')'");
1221 terminal_buffer
[0] = TOKEN_NOT_READ
;
1222 if (PEEK_TOKEN () == TYPENAME
&& PEEK_TOKEN1 () == END_TOKEN
)
1224 write_exp_elt_opcode (OP_TYPE
);
1225 write_exp_elt_type (PEEK_LVAL ().tsym
.type
);
1226 write_exp_elt_opcode (OP_TYPE
);
1231 if (terminal_buffer
[0] != END_TOKEN
)
1233 if (comma_terminates
&& terminal_buffer
[0] == ',')
1234 lexptr
--; /* Put the comma back. */
1236 error ("Junk after end of expression.");
1242 /* Implementation of a dynamically expandable buffer for processing input
1243 characters acquired through lexptr and building a value to return in
1246 static char *tempbuf
; /* Current buffer contents */
1247 static int tempbufsize
; /* Size of allocated buffer */
1248 static int tempbufindex
; /* Current index into buffer */
1250 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1252 #define CHECKBUF(size) \
1254 if (tempbufindex + (size) >= tempbufsize) \
1256 growbuf_by_size (size); \
1260 /* Grow the static temp buffer if necessary, including allocating the first one
1264 growbuf_by_size (int count
)
1268 growby
= max (count
, GROWBY_MIN_SIZE
);
1269 tempbufsize
+= growby
;
1270 if (tempbuf
== NULL
)
1272 tempbuf
= (char *) xmalloc (tempbufsize
);
1276 tempbuf
= (char *) xrealloc (tempbuf
, tempbufsize
);
1280 /* Try to consume a simple name string token. If successful, returns
1281 a pointer to a nullbyte terminated copy of the name that can be used
1282 in symbol table lookups. If not successful, returns NULL. */
1285 match_simple_name_string (void)
1287 char *tokptr
= lexptr
;
1289 if (isalpha (*tokptr
) || *tokptr
== '_')
1296 while (isalnum (*tokptr
) || (*tokptr
== '_'));
1297 yylval
.sval
.ptr
= lexptr
;
1298 yylval
.sval
.length
= tokptr
- lexptr
;
1300 result
= copy_name (yylval
.sval
);
1306 /* Start looking for a value composed of valid digits as set by the base
1307 in use. Note that '_' characters are valid anywhere, in any quantity,
1308 and are simply ignored. Since we must find at least one valid digit,
1309 or reject this token as an integer literal, we keep track of how many
1310 digits we have encountered. */
1313 decode_integer_value (int base
, char **tokptrptr
, LONGEST
*ivalptr
)
1315 char *tokptr
= *tokptrptr
;
1319 while (*tokptr
!= '\0')
1323 temp
= tolower (temp
);
1362 /* Found something not in domain for current base. */
1363 tokptr
--; /* Unconsume what gave us indigestion. */
1368 /* If we didn't find any digits, then we don't have a valid integer
1369 value, so reject the entire token. Otherwise, update the lexical
1370 scan pointer, and return non-zero for success. */
1378 *tokptrptr
= tokptr
;
1384 decode_integer_literal (LONGEST
*valptr
, char **tokptrptr
)
1386 char *tokptr
= *tokptrptr
;
1389 int explicit_base
= 0;
1391 /* Look for an explicit base specifier, which is optional. */
1424 /* If we found an explicit base ensure that the character after the
1425 explicit base is a single quote. */
1427 if (explicit_base
&& (*tokptr
++ != '\''))
1432 /* Attempt to decode whatever follows as an integer value in the
1433 indicated base, updating the token pointer in the process and
1434 computing the value into ival. Also, if we have an explicit
1435 base, then the next character must not be a single quote, or we
1436 have a bitstring literal, so reject the entire token in this case.
1437 Otherwise, update the lexical scan pointer, and return non-zero
1440 if (!decode_integer_value (base
, &tokptr
, &ival
))
1444 else if (explicit_base
&& (*tokptr
== '\''))
1451 *tokptrptr
= tokptr
;
1456 /* If it wasn't for the fact that floating point values can contain '_'
1457 characters, we could just let strtod do all the hard work by letting it
1458 try to consume as much of the current token buffer as possible and
1459 find a legal conversion. Unfortunately we need to filter out the '_'
1460 characters before calling strtod, which we do by copying the other
1461 legal chars to a local buffer to be converted. However since we also
1462 need to keep track of where the last unconsumed character in the input
1463 buffer is, we have transfer only as many characters as may compose a
1464 legal floating point value. */
1466 static enum ch_terminal
1467 match_float_literal (void)
1469 char *tokptr
= lexptr
;
1473 extern double strtod ();
1475 /* Make local buffer in which to build the string to convert. This is
1476 required because underscores are valid in chill floating point numbers
1477 but not in the string passed to strtod to convert. The string will be
1478 no longer than our input string. */
1480 copy
= buf
= (char *) alloca (strlen (tokptr
) + 1);
1482 /* Transfer all leading digits to the conversion buffer, discarding any
1485 while (isdigit (*tokptr
) || *tokptr
== '_')
1494 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1495 of whether we found any leading digits, and we simply accept it and
1496 continue on to look for the fractional part and/or exponent. One of
1497 [eEdD] is legal only if we have seen digits, and means that there
1498 is no fractional part. If we find neither of these, then this is
1499 not a floating point number, so return failure. */
1504 /* Accept and then look for fractional part and/or exponent. */
1517 goto collect_exponent
;
1525 /* We found a '.', copy any fractional digits to the conversion buffer, up
1526 to the first nondigit, non-underscore character. */
1528 while (isdigit (*tokptr
) || *tokptr
== '_')
1537 /* Look for an exponent, which must start with one of [eEdD]. If none
1538 is found, jump directly to trying to convert what we have collected
1555 /* Accept an optional '-' or '+' following one of [eEdD]. */
1558 if (*tokptr
== '+' || *tokptr
== '-')
1560 *copy
++ = *tokptr
++;
1563 /* Now copy an exponent into the conversion buffer. Note that at the
1564 moment underscores are *not* allowed in exponents. */
1566 while (isdigit (*tokptr
))
1568 *copy
++ = *tokptr
++;
1571 /* If we transfered any chars to the conversion buffer, try to interpret its
1572 contents as a floating point value. If any characters remain, then we
1573 must not have a valid floating point string. */
1579 dval
= strtod (buf
, ©
);
1584 return (FLOAT_LITERAL
);
1590 /* Recognize a string literal. A string literal is a sequence
1591 of characters enclosed in matching single or double quotes, except that
1592 a single character inside single quotes is a character literal, which
1593 we reject as a string literal. To embed the terminator character inside
1594 a string, it is simply doubled (I.E. "this""is""one""string") */
1596 static enum ch_terminal
1597 match_string_literal (void)
1599 char *tokptr
= lexptr
;
1603 for (tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1609 /* skip possible whitespaces */
1610 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1618 else if (*tokptr
!= ',')
1619 error ("Invalid control sequence");
1621 /* skip possible whitespaces */
1622 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1624 if (!decode_integer_literal (&ival
, &tokptr
))
1625 error ("Invalid control sequence");
1628 else if (*tokptr
== *lexptr
)
1630 if (*(tokptr
+ 1) == *lexptr
)
1639 else if (*tokptr
== '^')
1641 if (*(tokptr
+ 1) == '(')
1645 if (!decode_integer_literal (&ival
, &tokptr
))
1646 error ("Invalid control sequence");
1649 else if (*(tokptr
+ 1) == '^')
1652 error ("Invalid control sequence");
1656 tempbuf
[tempbufindex
++] = ival
;
1659 error ("Invalid control sequence");
1661 if (*tokptr
== '\0' /* no terminator */
1662 || (tempbufindex
== 1 && *tokptr
== '\'')) /* char literal */
1668 tempbuf
[tempbufindex
] = '\0';
1669 yylval
.sval
.ptr
= tempbuf
;
1670 yylval
.sval
.length
= tempbufindex
;
1672 return (CHARACTER_STRING_LITERAL
);
1676 /* Recognize a character literal. A character literal is single character
1677 or a control sequence, enclosed in single quotes. A control sequence
1678 is a comma separated list of one or more integer literals, enclosed
1679 in parenthesis and introduced with a circumflex character.
1681 EX: 'a' '^(7)' '^(7,8)'
1683 As a GNU chill extension, the syntax C'xx' is also recognized as a
1684 character literal, where xx is a hex value for the character.
1686 Note that more than a single character, enclosed in single quotes, is
1689 Returns CHARACTER_LITERAL if a match is found.
1692 static enum ch_terminal
1693 match_character_literal (void)
1695 char *tokptr
= lexptr
;
1698 if ((*tokptr
== 'c' || *tokptr
== 'C') && (*(tokptr
+ 1) == '\''))
1700 /* We have a GNU chill extension form, so skip the leading "C'",
1701 decode the hex value, and then ensure that we have a trailing
1702 single quote character. */
1704 if (!decode_integer_value (16, &tokptr
, &ival
) || (*tokptr
!= '\''))
1710 else if (*tokptr
== '\'')
1714 /* Determine which form we have, either a control sequence or the
1715 single character form. */
1719 if (*(tokptr
+ 1) == '(')
1721 /* Match and decode a control sequence. Return zero if we don't
1722 find a valid integer literal, or if the next unconsumed character
1723 after the integer literal is not the trailing ')'. */
1725 if (!decode_integer_literal (&ival
, &tokptr
) || (*tokptr
++ != ')'))
1730 else if (*(tokptr
+ 1) == '^')
1737 error ("Invalid control sequence");
1739 else if (*tokptr
== '\'')
1741 /* this must be duplicated */
1750 /* The trailing quote has not yet been consumed. If we don't find
1751 it, then we have no match. */
1753 if (*tokptr
++ != '\'')
1760 /* Not a character literal. */
1763 yylval
.typed_val
.val
= ival
;
1764 yylval
.typed_val
.type
= builtin_type_chill_char
;
1766 return (CHARACTER_LITERAL
);
1769 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1770 Note that according to 5.2.4.2, a single "_" is also a valid integer
1771 literal, however GNU-chill requires there to be at least one "digit"
1772 in any integer literal. */
1774 static enum ch_terminal
1775 match_integer_literal (void)
1777 char *tokptr
= lexptr
;
1780 if (!decode_integer_literal (&ival
, &tokptr
))
1786 yylval
.typed_val
.val
= ival
;
1787 #if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
1788 if (ival
> (LONGEST
) 2147483647U || ival
< -(LONGEST
) 2147483648U)
1789 yylval
.typed_val
.type
= builtin_type_long_long
;
1792 yylval
.typed_val
.type
= builtin_type_int
;
1794 return (INTEGER_LITERAL
);
1798 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1799 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1800 literal, however GNU-chill requires there to be at least one "digit"
1801 in any bit-string literal. */
1803 static enum ch_terminal
1804 match_bitstring_literal (void)
1806 register char *tokptr
= lexptr
;
1816 /* Look for the required explicit base specifier. */
1837 /* Ensure that the character after the explicit base is a single quote. */
1839 if (*tokptr
++ != '\'')
1844 while (*tokptr
!= '\0' && *tokptr
!= '\'')
1847 if (isupper (digit
))
1848 digit
= tolower (digit
);
1876 /* this is not a bitstring literal, probably an integer */
1879 if (digit
>= 1 << bits_per_char
)
1881 /* Found something not in domain for current base. */
1882 error ("Too-large digit in bitstring or integer.");
1886 /* Extract bits from digit, packing them into the bitstring byte. */
1887 int k
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? bits_per_char
- 1 : 0;
1888 for (; TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
>= 0 : k
< bits_per_char
;
1889 TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
-- : k
++)
1892 if (digit
& (1 << k
))
1894 tempbuf
[tempbufindex
] |=
1895 (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1896 ? (1 << (HOST_CHAR_BIT
- 1 - bitoffset
))
1900 if (bitoffset
== HOST_CHAR_BIT
)
1905 tempbuf
[tempbufindex
] = 0;
1911 /* Verify that we consumed everything up to the trailing single quote,
1912 and that we found some bits (IE not just underbars). */
1914 if (*tokptr
++ != '\'')
1920 yylval
.sval
.ptr
= tempbuf
;
1921 yylval
.sval
.length
= bitcount
;
1923 return (BIT_STRING_LITERAL
);
1933 static const struct token idtokentab
[] =
1957 {"addr", ADDR_TOKEN
},
1958 {"null", EMPTINESS_LITERAL
}
1961 static const struct token tokentab2
[] =
1963 {":=", GDB_ASSIGNMENT
},
1964 {"//", SLASH_SLASH
},
1971 /* Read one token, getting characters through lexptr. */
1972 /* This is where we will check to make sure that the language and the
1973 operators used are compatible. */
1975 static enum ch_terminal
1979 enum ch_terminal token
;
1983 /* Skip over any leading whitespace. */
1984 while (isspace (*lexptr
))
1988 /* Look for special single character cases which can't be the first
1989 character of some other multicharacter token. */
2006 /* Look for characters which start a particular kind of multicharacter
2007 token, such as a character literal, register name, convenience
2008 variable name, string literal, etc. */
2013 /* First try to match a string literal, which is any
2014 sequence of characters enclosed in matching single or double
2015 quotes, except that a single character inside single quotes
2016 is a character literal, so we have to catch that case also. */
2017 token
= match_string_literal ();
2022 if (*lexptr
== '\'')
2024 token
= match_character_literal ();
2033 token
= match_character_literal ();
2040 yylval
.sval
.ptr
= lexptr
;
2045 while (isalnum (*lexptr
) || *lexptr
== '_' || *lexptr
== '$');
2046 yylval
.sval
.length
= lexptr
- yylval
.sval
.ptr
;
2047 write_dollar_variable (yylval
.sval
);
2048 return GDB_VARIABLE
;
2051 /* See if it is a special token of length 2. */
2052 for (i
= 0; i
< sizeof (tokentab2
) / sizeof (tokentab2
[0]); i
++)
2054 if (STREQN (lexptr
, tokentab2
[i
].operator, 2))
2057 return (tokentab2
[i
].token
);
2060 /* Look for single character cases which which could be the first
2061 character of some other multicharacter token, but aren't, or we
2062 would already have found it. */
2072 /* Look for a float literal before looking for an integer literal, so
2073 we match as much of the input stream as possible. */
2074 token
= match_float_literal ();
2079 token
= match_bitstring_literal ();
2084 token
= match_integer_literal ();
2090 /* Try to match a simple name string, and if a match is found, then
2091 further classify what sort of name it is and return an appropriate
2092 token. Note that attempting to match a simple name string consumes
2093 the token from lexptr, so we can't back out if we later find that
2094 we can't classify what sort of name it is. */
2096 inputname
= match_simple_name_string ();
2098 if (inputname
!= NULL
)
2100 char *simplename
= (char *) alloca (strlen (inputname
) + 1);
2102 char *dptr
= simplename
, *sptr
= inputname
;
2103 for (; *sptr
; sptr
++)
2104 *dptr
++ = isupper (*sptr
) ? tolower (*sptr
) : *sptr
;
2107 /* See if it is a reserved identifier. */
2108 for (i
= 0; i
< sizeof (idtokentab
) / sizeof (idtokentab
[0]); i
++)
2110 if (STREQ (simplename
, idtokentab
[i
].operator))
2112 return (idtokentab
[i
].token
);
2116 /* Look for other special tokens. */
2117 if (STREQ (simplename
, "true"))
2120 return (BOOLEAN_LITERAL
);
2122 if (STREQ (simplename
, "false"))
2125 return (BOOLEAN_LITERAL
);
2128 sym
= lookup_symbol (inputname
, expression_context_block
,
2129 VAR_NAMESPACE
, (int *) NULL
,
2130 (struct symtab
**) NULL
);
2131 if (sym
== NULL
&& strcmp (inputname
, simplename
) != 0)
2133 sym
= lookup_symbol (simplename
, expression_context_block
,
2134 VAR_NAMESPACE
, (int *) NULL
,
2135 (struct symtab
**) NULL
);
2139 yylval
.ssym
.stoken
.ptr
= NULL
;
2140 yylval
.ssym
.stoken
.length
= 0;
2141 yylval
.ssym
.sym
= sym
;
2142 yylval
.ssym
.is_a_field_of_this
= 0; /* FIXME, C++'ism */
2143 switch (SYMBOL_CLASS (sym
))
2146 /* Found a procedure name. */
2147 return (GENERAL_PROCEDURE_NAME
);
2149 /* Found a global or local static variable. */
2150 return (LOCATION_NAME
);
2155 case LOC_REGPARM_ADDR
:
2159 case LOC_BASEREG_ARG
:
2160 if (innermost_block
== NULL
2161 || contained_in (block_found
, innermost_block
))
2163 innermost_block
= block_found
;
2165 return (LOCATION_NAME
);
2169 return (LOCATION_NAME
);
2172 yylval
.tsym
.type
= SYMBOL_TYPE (sym
);
2175 case LOC_CONST_BYTES
:
2176 case LOC_OPTIMIZED_OUT
:
2177 error ("Symbol \"%s\" names no location.", inputname
);
2180 internal_error ("unhandled SYMBOL_CLASS in ch_lex()");
2184 else if (!have_full_symbols () && !have_partial_symbols ())
2186 error ("No symbol table is loaded. Use the \"file\" command.");
2190 error ("No symbol \"%s\" in current context.", inputname
);
2194 /* Catch single character tokens which are not part of some
2199 case '.': /* Not float for example. */
2201 while (isspace (*lexptr
))
2203 inputname
= match_simple_name_string ();
2206 return DOT_FIELD_NAME
;
2209 return (ILLEGAL_TOKEN
);
2213 write_lower_upper_value (enum exp_opcode opcode
, /* Either UNOP_LOWER or UNOP_UPPER */
2217 write_exp_elt_opcode (opcode
);
2220 struct type
*result_type
;
2221 LONGEST val
= type_lower_upper (opcode
, type
, &result_type
);
2222 write_exp_elt_opcode (OP_LONG
);
2223 write_exp_elt_type (result_type
);
2224 write_exp_elt_longcst (val
);
2225 write_exp_elt_opcode (OP_LONG
);
2230 chill_error (char *msg
)