1 /* Parser for GNU CHILL (CCITT High-Level Language) -*- C -*-
2 Copyright 1992, 1993, 1995, 1996, 1997, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Parse a Chill expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result.
31 Note that the language accepted by this parser is more liberal
32 than the one accepted by an actual Chill compiler. For example, the
33 language rule that a simple name string can not be one of the reserved
34 simple name strings is not enforced (e.g "case" is not treated as a
35 reserved name). Another example is that Chill is a strongly typed
36 language, and certain expressions that violate the type constraints
37 may still be evaluated if gdb can do so in a meaningful manner, while
38 such expressions would be rejected by the compiler. The reason for
39 this more liberal behavior is the philosophy that the debugger
40 is intended to be a tool that is used by the programmer when things
41 go wrong, and as such, it should provide as few artificial barriers
42 to it's use as possible. If it can do something meaningful, even
43 something that violates language contraints that are enforced by the
44 compiler, it should do so without complaint.
49 #include "gdb_string.h"
51 #include "expression.h"
54 #include "parser-defs.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 */
61 #define INLINE __inline__
87 /* '\001' ... '\xff' come first. */
94 GENERAL_PROCEDURE_NAME
,
97 CHARACTER_STRING_LITERAL
,
100 DOT_FIELD_NAME
, /* '.' followed by <field name> */
143 /* Forward declarations. */
145 static void write_lower_upper_value (enum exp_opcode
, struct type
*);
146 static enum ch_terminal
match_bitstring_literal (void);
147 static enum ch_terminal
match_integer_literal (void);
148 static enum ch_terminal
match_character_literal (void);
149 static enum ch_terminal
match_string_literal (void);
150 static enum ch_terminal
match_float_literal (void);
151 static enum ch_terminal
match_float_literal (void);
152 static int decode_integer_literal (LONGEST
*, char **);
153 static int decode_integer_value (int, char **, LONGEST
*);
154 static char *match_simple_name_string (void);
155 static void growbuf_by_size (int);
156 static void parse_untyped_expr (void);
157 static void parse_if_expression (void);
158 static void parse_else_alternative (void);
159 static void parse_then_alternative (void);
160 static void parse_expr (void);
161 static void parse_operand0 (void);
162 static void parse_operand1 (void);
163 static void parse_operand2 (void);
164 static void parse_operand3 (void);
165 static void parse_operand4 (void);
166 static void parse_operand5 (void);
167 static void parse_operand6 (void);
168 static void parse_primval (void);
169 static void parse_tuple (struct type
*);
170 static void parse_opt_element_list (struct type
*);
171 static void parse_tuple_element (struct type
*);
172 static void parse_named_record_element (void);
173 static void parse_call (void);
174 static struct type
*parse_mode_or_normal_call (void);
176 static struct type
*parse_mode_call (void);
178 static void parse_unary_call (void);
179 static int parse_opt_untyped_expr (void);
180 static void parse_case_label (void);
181 static int expect (enum ch_terminal
, char *);
182 static void parse_expr (void);
183 static void parse_primval (void);
184 static void parse_untyped_expr (void);
185 static int parse_opt_untyped_expr (void);
186 static void parse_if_expression_body (void);
187 static enum ch_terminal
ch_lex (void);
188 INLINE
static enum ch_terminal
PEEK_TOKEN (void);
189 static enum ch_terminal
peek_token_ (int);
190 static void forward_token_ (void);
191 static void require (enum ch_terminal
);
192 static int check_token (enum ch_terminal
);
194 #define MAX_LOOK_AHEAD 2
195 static enum ch_terminal terminal_buffer
[MAX_LOOK_AHEAD
+ 1] =
197 TOKEN_NOT_READ
, TOKEN_NOT_READ
, TOKEN_NOT_READ
};
198 static YYSTYPE yylval
;
199 static YYSTYPE val_buffer
[MAX_LOOK_AHEAD
+ 1];
201 /*int current_token, lookahead_token; */
203 INLINE
static enum ch_terminal
206 if (terminal_buffer
[0] == TOKEN_NOT_READ
)
208 terminal_buffer
[0] = ch_lex ();
209 val_buffer
[0] = yylval
;
211 return terminal_buffer
[0];
213 #define PEEK_LVAL() val_buffer[0]
214 #define PEEK_TOKEN1() peek_token_(1)
215 #define PEEK_TOKEN2() peek_token_(2)
216 static enum ch_terminal
219 if (i
> MAX_LOOK_AHEAD
)
220 internal_error (__FILE__
, __LINE__
,
221 "too much lookahead");
222 if (terminal_buffer
[i
] == TOKEN_NOT_READ
)
224 terminal_buffer
[i
] = ch_lex ();
225 val_buffer
[i
] = yylval
;
227 return terminal_buffer
[i
];
233 pushback_token (enum ch_terminal code
, YYSTYPE node
)
236 if (terminal_buffer
[MAX_LOOK_AHEAD
] != TOKEN_NOT_READ
)
237 internal_error (__FILE__
, __LINE__
,
238 "cannot pushback token");
239 for (i
= MAX_LOOK_AHEAD
; i
> 0; i
--)
241 terminal_buffer
[i
] = terminal_buffer
[i
- 1];
242 val_buffer
[i
] = val_buffer
[i
- 1];
244 terminal_buffer
[0] = code
;
245 val_buffer
[0] = node
;
251 forward_token_ (void)
254 for (i
= 0; i
< MAX_LOOK_AHEAD
; i
++)
256 terminal_buffer
[i
] = terminal_buffer
[i
+ 1];
257 val_buffer
[i
] = val_buffer
[i
+ 1];
259 terminal_buffer
[MAX_LOOK_AHEAD
] = TOKEN_NOT_READ
;
261 #define FORWARD_TOKEN() forward_token_()
263 /* Skip the next token.
264 if it isn't TOKEN, the parser is broken. */
267 require (enum ch_terminal token
)
269 if (PEEK_TOKEN () != token
)
271 internal_error (__FILE__
, __LINE__
,
272 "expected token %d", (int) token
);
278 check_token (enum ch_terminal token
)
280 if (PEEK_TOKEN () != token
)
286 /* return 0 if expected token was not found,
290 expect (enum ch_terminal token
, char *message
)
292 if (PEEK_TOKEN () != token
)
296 else if (token
< 256)
297 error ("syntax error - expected a '%c' here \"%s\"", token
, lexptr
);
299 error ("syntax error");
308 /* Parse a name string. If ALLOW_ALL is 1, ALL is allowed as a postfix. */
311 parse_opt_name_string (int allow_all
)
313 int token
= PEEK_TOKEN ();
317 if (token
== ALL
&& allow_all
)
328 token
= PEEK_TOKEN ();
332 token
= PEEK_TOKEN ();
333 if (token
== ALL
&& allow_all
)
334 return get_identifier3 (IDENTIFIER_POINTER (name
), "!", "*");
338 error ("'%s!' is not followed by an identifier",
339 IDENTIFIER_POINTER (name
));
342 name
= get_identifier3 (IDENTIFIER_POINTER (name
),
343 "!", IDENTIFIER_POINTER (PEEK_LVAL ()));
348 parse_simple_name_string (void)
350 int token
= PEEK_TOKEN ();
354 error ("expected a name here");
355 return error_mark_node
;
363 parse_name_string (void)
365 tree name
= parse_opt_name_string (0);
369 error ("expected a name string here");
370 return error_mark_node
;
373 /* Matches: <name_string>
374 Returns if pass 1: the identifier.
375 Returns if pass 2: a decl or value for identifier. */
380 tree name
= parse_name_string ();
381 if (pass
== 1 || ignoring
)
385 tree decl
= lookup_name (name
);
386 if (decl
== NULL_TREE
)
388 error ("`%s' undeclared", IDENTIFIER_POINTER (name
));
389 return error_mark_node
;
391 else if (TREE_CODE (TREE_TYPE (decl
)) == ERROR_MARK
)
392 return error_mark_node
;
393 else if (TREE_CODE (decl
) == CONST_DECL
)
394 return DECL_INITIAL (decl
);
395 else if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
396 return convert_from_reference (decl
);
405 pushback_paren_expr (tree expr
)
407 if (pass
== 1 && !ignoring
)
408 expr
= build1 (PAREN_EXPR
, NULL_TREE
, expr
);
409 pushback_token (EXPR
, expr
);
413 /* Matches: <case label> */
416 parse_case_label (void)
418 if (check_token (ELSE
))
419 error ("ELSE in tuples labels not implemented");
420 /* Does not handle the case of a mode name. FIXME */
422 if (check_token (':'))
425 write_exp_elt_opcode (BINOP_RANGE
);
430 parse_opt_untyped_expr (void)
432 switch (PEEK_TOKEN ())
439 parse_untyped_expr ();
445 parse_unary_call (void)
453 /* Parse NAME '(' MODENAME ')'. */
458 parse_mode_call (void)
463 if (PEEK_TOKEN () != TYPENAME
)
464 error ("expect MODENAME here `%s'", lexptr
);
465 type
= PEEK_LVAL ().tsym
.type
;
474 parse_mode_or_normal_call (void)
479 if (PEEK_TOKEN () == TYPENAME
)
481 type
= PEEK_LVAL ().tsym
.type
;
493 /* Parse something that looks like a function call.
494 Assume we have parsed the function, and are at the '('. */
501 /* This is to save the value of arglist_len
502 being accumulated for each dimension. */
504 if (parse_opt_untyped_expr ())
506 int tok
= PEEK_TOKEN ();
508 if (tok
== UP
|| tok
== ':')
512 expect (')', "expected ')' to terminate slice");
514 write_exp_elt_opcode (tok
== UP
? TERNOP_SLICE_COUNT
518 while (check_token (','))
520 parse_untyped_expr ();
527 arg_count
= end_arglist ();
528 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
529 write_exp_elt_longcst (arg_count
);
530 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
534 parse_named_record_element (void)
539 label
= PEEK_LVAL ().sval
;
540 sprintf (buf
, "expected a field name here `%s'", lexptr
);
541 expect (DOT_FIELD_NAME
, buf
);
542 if (check_token (','))
543 parse_named_record_element ();
544 else if (check_token (':'))
547 error ("syntax error near `%s' in named record tuple element", lexptr
);
548 write_exp_elt_opcode (OP_LABELED
);
549 write_exp_string (label
);
550 write_exp_elt_opcode (OP_LABELED
);
553 /* Returns one or more TREE_LIST nodes, in reverse order. */
556 parse_tuple_element (struct type
*type
)
558 if (PEEK_TOKEN () == DOT_FIELD_NAME
)
560 /* Parse a labelled structure tuple. */
561 parse_named_record_element ();
565 if (check_token ('('))
567 if (check_token ('*'))
569 expect (')', "missing ')' after '*' case label list");
572 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
574 /* do this as a range from low to high */
575 struct type
*range_type
= TYPE_FIELD_TYPE (type
, 0);
576 LONGEST low_bound
, high_bound
;
577 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
578 error ("cannot determine bounds for (*)");
580 write_exp_elt_opcode (OP_LONG
);
581 write_exp_elt_type (range_type
);
582 write_exp_elt_longcst (low_bound
);
583 write_exp_elt_opcode (OP_LONG
);
585 write_exp_elt_opcode (OP_LONG
);
586 write_exp_elt_type (range_type
);
587 write_exp_elt_longcst (high_bound
);
588 write_exp_elt_opcode (OP_LONG
);
589 write_exp_elt_opcode (BINOP_RANGE
);
592 error ("(*) in invalid context");
595 error ("(*) only possible with modename in front of tuple (mode[..])");
600 while (check_token (','))
603 write_exp_elt_opcode (BINOP_COMMA
);
609 parse_untyped_expr ();
610 if (check_token (':'))
612 /* A powerset range or a labeled Array. */
613 parse_untyped_expr ();
614 write_exp_elt_opcode (BINOP_RANGE
);
618 /* Matches: a COMMA-separated list of tuple elements.
619 Returns a list (of TREE_LIST nodes). */
621 parse_opt_element_list (struct type
*type
)
624 if (PEEK_TOKEN () == ']')
628 parse_tuple_element (type
);
630 if (PEEK_TOKEN () == ']')
632 if (!check_token (','))
633 error ("bad syntax in tuple");
637 /* Parses: '[' elements ']'
638 If modename is non-NULL it prefixed the tuple. */
641 parse_tuple (struct type
*mode
)
645 type
= check_typedef (mode
);
650 parse_opt_element_list (type
);
651 expect (']', "missing ']' after tuple");
652 write_exp_elt_opcode (OP_ARRAY
);
653 write_exp_elt_longcst ((LONGEST
) 0);
654 write_exp_elt_longcst ((LONGEST
) end_arglist () - 1);
655 write_exp_elt_opcode (OP_ARRAY
);
658 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
659 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
660 && TYPE_CODE (type
) != TYPE_CODE_SET
)
661 error ("invalid tuple mode");
662 write_exp_elt_opcode (UNOP_CAST
);
663 write_exp_elt_type (mode
);
664 write_exp_elt_opcode (UNOP_CAST
);
674 switch (PEEK_TOKEN ())
676 case INTEGER_LITERAL
:
677 case CHARACTER_LITERAL
:
678 write_exp_elt_opcode (OP_LONG
);
679 write_exp_elt_type (PEEK_LVAL ().typed_val
.type
);
680 write_exp_elt_longcst (PEEK_LVAL ().typed_val
.val
);
681 write_exp_elt_opcode (OP_LONG
);
684 case BOOLEAN_LITERAL
:
685 write_exp_elt_opcode (OP_BOOL
);
686 write_exp_elt_longcst ((LONGEST
) PEEK_LVAL ().ulval
);
687 write_exp_elt_opcode (OP_BOOL
);
691 write_exp_elt_opcode (OP_DOUBLE
);
692 write_exp_elt_type (builtin_type_double
);
693 write_exp_elt_dblcst (PEEK_LVAL ().dval
);
694 write_exp_elt_opcode (OP_DOUBLE
);
697 case EMPTINESS_LITERAL
:
698 write_exp_elt_opcode (OP_LONG
);
699 write_exp_elt_type (lookup_pointer_type (builtin_type_void
));
700 write_exp_elt_longcst (0);
701 write_exp_elt_opcode (OP_LONG
);
704 case CHARACTER_STRING_LITERAL
:
705 write_exp_elt_opcode (OP_STRING
);
706 write_exp_string (PEEK_LVAL ().sval
);
707 write_exp_elt_opcode (OP_STRING
);
710 case BIT_STRING_LITERAL
:
711 write_exp_elt_opcode (OP_BITSTRING
);
712 write_exp_bitstring (PEEK_LVAL ().sval
);
713 write_exp_elt_opcode (OP_BITSTRING
);
718 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
719 which casts to an artificial array. */
722 if (PEEK_TOKEN () != TYPENAME
)
723 error ("missing MODENAME after ARRAY()");
724 type
= PEEK_LVAL ().tsym
.type
;
728 expect (')', "missing right parenthesis");
729 type
= create_array_type ((struct type
*) NULL
, type
,
730 create_range_type ((struct type
*) NULL
,
731 builtin_type_int
, 0, 0));
732 TYPE_ARRAY_UPPER_BOUND_TYPE (type
) = BOUND_CANNOT_BE_DETERMINED
;
733 write_exp_elt_opcode (UNOP_CAST
);
734 write_exp_elt_type (type
);
735 write_exp_elt_opcode (UNOP_CAST
);
747 expect (')', "missing right parenthesis");
752 case GENERAL_PROCEDURE_NAME
:
754 write_exp_elt_opcode (OP_VAR_VALUE
);
755 write_exp_elt_block (NULL
);
756 write_exp_elt_sym (PEEK_LVAL ().ssym
.sym
);
757 write_exp_elt_opcode (OP_VAR_VALUE
);
760 case GDB_VARIABLE
: /* gdb specific */
765 write_exp_elt_opcode (UNOP_CAST
);
766 write_exp_elt_type (builtin_type_int
);
767 write_exp_elt_opcode (UNOP_CAST
);
771 write_exp_elt_opcode (UNOP_CARD
);
775 write_exp_elt_opcode (UNOP_CHMAX
);
779 write_exp_elt_opcode (UNOP_CHMIN
);
783 goto unimplemented_unary_builtin
;
786 goto unimplemented_unary_builtin
;
789 goto unimplemented_unary_builtin
;
790 unimplemented_unary_builtin
:
792 error ("not implemented: %s builtin function", op_name
);
796 write_exp_elt_opcode (UNOP_ADDR
);
799 type
= parse_mode_or_normal_call ();
802 write_exp_elt_opcode (OP_LONG
);
803 write_exp_elt_type (builtin_type_int
);
804 CHECK_TYPEDEF (type
);
805 write_exp_elt_longcst ((LONGEST
) TYPE_LENGTH (type
));
806 write_exp_elt_opcode (OP_LONG
);
809 write_exp_elt_opcode (UNOP_SIZEOF
);
818 type
= parse_mode_or_normal_call ();
819 write_lower_upper_value (op
, type
);
823 write_exp_elt_opcode (UNOP_LENGTH
);
826 type
= PEEK_LVAL ().tsym
.type
;
828 switch (PEEK_TOKEN ())
836 expect (')', "missing right parenthesis");
837 write_exp_elt_opcode (UNOP_CAST
);
838 write_exp_elt_type (type
);
839 write_exp_elt_opcode (UNOP_CAST
);
842 error ("typename in invalid context");
847 error ("invalid expression syntax at `%s'", lexptr
);
851 switch (PEEK_TOKEN ())
854 write_exp_elt_opcode (STRUCTOP_STRUCT
);
855 write_exp_string (PEEK_LVAL ().sval
);
856 write_exp_elt_opcode (STRUCTOP_STRUCT
);
861 if (PEEK_TOKEN () == TYPENAME
)
863 type
= PEEK_LVAL ().tsym
.type
;
864 write_exp_elt_opcode (UNOP_CAST
);
865 write_exp_elt_type (lookup_pointer_type (type
));
866 write_exp_elt_opcode (UNOP_CAST
);
869 write_exp_elt_opcode (UNOP_IND
);
874 case CHARACTER_STRING_LITERAL
:
875 case CHARACTER_LITERAL
:
876 case BIT_STRING_LITERAL
:
877 /* Handle string repetition. (See comment in parse_operand5.) */
879 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
880 write_exp_elt_longcst (1);
881 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
885 case INTEGER_LITERAL
:
886 case BOOLEAN_LITERAL
:
888 case GENERAL_PROCEDURE_NAME
:
890 case EMPTINESS_LITERAL
:
939 parse_operand6 (void)
941 if (check_token (RECEIVE
))
944 error ("not implemented: RECEIVE expression");
946 else if (check_token (POINTER
))
949 write_exp_elt_opcode (UNOP_ADDR
);
956 parse_operand5 (void)
959 /* We are supposed to be looking for a <string repetition operator>,
960 but in general we can't distinguish that from a parenthesized
961 expression. This is especially difficult if we allow the
962 string operand to be a constant expression (as requested by
963 some users), and not just a string literal.
964 Consider: LPRN expr RPRN LPRN expr RPRN
965 Is that a function call or string repetition?
966 Instead, we handle string repetition in parse_primval,
967 and build_generalized_call. */
968 switch (PEEK_TOKEN ())
971 op
= UNOP_LOGICAL_NOT
;
983 write_exp_elt_opcode (op
);
987 parse_operand4 (void)
993 switch (PEEK_TOKEN ())
1012 write_exp_elt_opcode (op
);
1017 parse_operand3 (void)
1023 switch (PEEK_TOKEN ())
1039 write_exp_elt_opcode (op
);
1044 parse_operand2 (void)
1050 if (check_token (IN
))
1053 write_exp_elt_opcode (BINOP_IN
);
1057 switch (PEEK_TOKEN ())
1075 op
= BINOP_NOTEQUAL
;
1082 write_exp_elt_opcode (op
);
1088 parse_operand1 (void)
1094 switch (PEEK_TOKEN ())
1097 op
= BINOP_BITWISE_AND
;
1100 op
= BINOP_LOGICAL_AND
;
1107 write_exp_elt_opcode (op
);
1112 parse_operand0 (void)
1118 switch (PEEK_TOKEN ())
1121 op
= BINOP_BITWISE_IOR
;
1124 op
= BINOP_BITWISE_XOR
;
1127 op
= BINOP_LOGICAL_OR
;
1134 write_exp_elt_opcode (op
);
1142 if (check_token (GDB_ASSIGNMENT
))
1145 write_exp_elt_opcode (BINOP_ASSIGN
);
1150 parse_then_alternative (void)
1152 expect (THEN
, "missing 'THEN' in 'IF' expression");
1157 parse_else_alternative (void)
1159 if (check_token (ELSIF
))
1160 parse_if_expression_body ();
1161 else if (check_token (ELSE
))
1164 error ("missing ELSE/ELSIF in IF expression");
1167 /* Matches: <boolean expression> <then alternative> <else alternative> */
1170 parse_if_expression_body (void)
1173 parse_then_alternative ();
1174 parse_else_alternative ();
1175 write_exp_elt_opcode (TERNOP_COND
);
1179 parse_if_expression (void)
1182 parse_if_expression_body ();
1183 expect (FI
, "missing 'FI' at end of conditional expression");
1186 /* An <untyped_expr> is a superset of <expr>. It also includes
1187 <conditional expressions> and untyped <tuples>, whose types
1188 are not given by their constituents. Hence, these are only
1189 allowed in certain contexts that expect a certain type.
1190 You should call convert() to fix up the <untyped_expr>. */
1193 parse_untyped_expr (void)
1195 switch (PEEK_TOKEN ())
1198 parse_if_expression ();
1201 error ("not implemented: CASE expression");
1203 switch (PEEK_TOKEN1 ())
1211 parse_untyped_expr ();
1212 expect (')', "missing ')'");
1225 terminal_buffer
[0] = TOKEN_NOT_READ
;
1226 if (PEEK_TOKEN () == TYPENAME
&& PEEK_TOKEN1 () == END_TOKEN
)
1228 write_exp_elt_opcode (OP_TYPE
);
1229 write_exp_elt_type (PEEK_LVAL ().tsym
.type
);
1230 write_exp_elt_opcode (OP_TYPE
);
1235 if (terminal_buffer
[0] != END_TOKEN
)
1237 if (comma_terminates
&& terminal_buffer
[0] == ',')
1238 lexptr
--; /* Put the comma back. */
1240 error ("Junk after end of expression.");
1246 /* Implementation of a dynamically expandable buffer for processing input
1247 characters acquired through lexptr and building a value to return in
1250 static char *tempbuf
; /* Current buffer contents */
1251 static int tempbufsize
; /* Size of allocated buffer */
1252 static int tempbufindex
; /* Current index into buffer */
1254 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1256 #define CHECKBUF(size) \
1258 if (tempbufindex + (size) >= tempbufsize) \
1260 growbuf_by_size (size); \
1264 /* Grow the static temp buffer if necessary, including allocating the first one
1268 growbuf_by_size (int count
)
1272 growby
= max (count
, GROWBY_MIN_SIZE
);
1273 tempbufsize
+= growby
;
1274 if (tempbuf
== NULL
)
1276 tempbuf
= (char *) xmalloc (tempbufsize
);
1280 tempbuf
= (char *) xrealloc (tempbuf
, tempbufsize
);
1284 /* Try to consume a simple name string token. If successful, returns
1285 a pointer to a nullbyte terminated copy of the name that can be used
1286 in symbol table lookups. If not successful, returns NULL. */
1289 match_simple_name_string (void)
1291 char *tokptr
= lexptr
;
1293 if (isalpha (*tokptr
) || *tokptr
== '_')
1300 while (isalnum (*tokptr
) || (*tokptr
== '_'));
1301 yylval
.sval
.ptr
= lexptr
;
1302 yylval
.sval
.length
= tokptr
- lexptr
;
1304 result
= copy_name (yylval
.sval
);
1310 /* Start looking for a value composed of valid digits as set by the base
1311 in use. Note that '_' characters are valid anywhere, in any quantity,
1312 and are simply ignored. Since we must find at least one valid digit,
1313 or reject this token as an integer literal, we keep track of how many
1314 digits we have encountered. */
1317 decode_integer_value (int base
, char **tokptrptr
, LONGEST
*ivalptr
)
1319 char *tokptr
= *tokptrptr
;
1323 while (*tokptr
!= '\0')
1327 temp
= tolower (temp
);
1366 /* Found something not in domain for current base. */
1367 tokptr
--; /* Unconsume what gave us indigestion. */
1372 /* If we didn't find any digits, then we don't have a valid integer
1373 value, so reject the entire token. Otherwise, update the lexical
1374 scan pointer, and return non-zero for success. */
1382 *tokptrptr
= tokptr
;
1388 decode_integer_literal (LONGEST
*valptr
, char **tokptrptr
)
1390 char *tokptr
= *tokptrptr
;
1393 int explicit_base
= 0;
1395 /* Look for an explicit base specifier, which is optional. */
1428 /* If we found an explicit base ensure that the character after the
1429 explicit base is a single quote. */
1431 if (explicit_base
&& (*tokptr
++ != '\''))
1436 /* Attempt to decode whatever follows as an integer value in the
1437 indicated base, updating the token pointer in the process and
1438 computing the value into ival. Also, if we have an explicit
1439 base, then the next character must not be a single quote, or we
1440 have a bitstring literal, so reject the entire token in this case.
1441 Otherwise, update the lexical scan pointer, and return non-zero
1444 if (!decode_integer_value (base
, &tokptr
, &ival
))
1448 else if (explicit_base
&& (*tokptr
== '\''))
1455 *tokptrptr
= tokptr
;
1460 /* If it wasn't for the fact that floating point values can contain '_'
1461 characters, we could just let strtod do all the hard work by letting it
1462 try to consume as much of the current token buffer as possible and
1463 find a legal conversion. Unfortunately we need to filter out the '_'
1464 characters before calling strtod, which we do by copying the other
1465 legal chars to a local buffer to be converted. However since we also
1466 need to keep track of where the last unconsumed character in the input
1467 buffer is, we have transfer only as many characters as may compose a
1468 legal floating point value. */
1470 static enum ch_terminal
1471 match_float_literal (void)
1473 char *tokptr
= lexptr
;
1477 extern double strtod ();
1479 /* Make local buffer in which to build the string to convert. This is
1480 required because underscores are valid in chill floating point numbers
1481 but not in the string passed to strtod to convert. The string will be
1482 no longer than our input string. */
1484 copy
= buf
= (char *) alloca (strlen (tokptr
) + 1);
1486 /* Transfer all leading digits to the conversion buffer, discarding any
1489 while (isdigit (*tokptr
) || *tokptr
== '_')
1498 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1499 of whether we found any leading digits, and we simply accept it and
1500 continue on to look for the fractional part and/or exponent. One of
1501 [eEdD] is legal only if we have seen digits, and means that there
1502 is no fractional part. If we find neither of these, then this is
1503 not a floating point number, so return failure. */
1508 /* Accept and then look for fractional part and/or exponent. */
1521 goto collect_exponent
;
1529 /* We found a '.', copy any fractional digits to the conversion buffer, up
1530 to the first nondigit, non-underscore character. */
1532 while (isdigit (*tokptr
) || *tokptr
== '_')
1541 /* Look for an exponent, which must start with one of [eEdD]. If none
1542 is found, jump directly to trying to convert what we have collected
1559 /* Accept an optional '-' or '+' following one of [eEdD]. */
1562 if (*tokptr
== '+' || *tokptr
== '-')
1564 *copy
++ = *tokptr
++;
1567 /* Now copy an exponent into the conversion buffer. Note that at the
1568 moment underscores are *not* allowed in exponents. */
1570 while (isdigit (*tokptr
))
1572 *copy
++ = *tokptr
++;
1575 /* If we transfered any chars to the conversion buffer, try to interpret its
1576 contents as a floating point value. If any characters remain, then we
1577 must not have a valid floating point string. */
1583 dval
= strtod (buf
, ©
);
1588 return (FLOAT_LITERAL
);
1594 /* Recognize a string literal. A string literal is a sequence
1595 of characters enclosed in matching single or double quotes, except that
1596 a single character inside single quotes is a character literal, which
1597 we reject as a string literal. To embed the terminator character inside
1598 a string, it is simply doubled (I.E. "this""is""one""string") */
1600 static enum ch_terminal
1601 match_string_literal (void)
1603 char *tokptr
= lexptr
;
1607 for (tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1613 /* skip possible whitespaces */
1614 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1622 else if (*tokptr
!= ',')
1623 error ("Invalid control sequence");
1625 /* skip possible whitespaces */
1626 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1628 if (!decode_integer_literal (&ival
, &tokptr
))
1629 error ("Invalid control sequence");
1632 else if (*tokptr
== *lexptr
)
1634 if (*(tokptr
+ 1) == *lexptr
)
1643 else if (*tokptr
== '^')
1645 if (*(tokptr
+ 1) == '(')
1649 if (!decode_integer_literal (&ival
, &tokptr
))
1650 error ("Invalid control sequence");
1653 else if (*(tokptr
+ 1) == '^')
1656 error ("Invalid control sequence");
1660 tempbuf
[tempbufindex
++] = ival
;
1663 error ("Invalid control sequence");
1665 if (*tokptr
== '\0' /* no terminator */
1666 || (tempbufindex
== 1 && *tokptr
== '\'')) /* char literal */
1672 tempbuf
[tempbufindex
] = '\0';
1673 yylval
.sval
.ptr
= tempbuf
;
1674 yylval
.sval
.length
= tempbufindex
;
1676 return (CHARACTER_STRING_LITERAL
);
1680 /* Recognize a character literal. A character literal is single character
1681 or a control sequence, enclosed in single quotes. A control sequence
1682 is a comma separated list of one or more integer literals, enclosed
1683 in parenthesis and introduced with a circumflex character.
1685 EX: 'a' '^(7)' '^(7,8)'
1687 As a GNU chill extension, the syntax C'xx' is also recognized as a
1688 character literal, where xx is a hex value for the character.
1690 Note that more than a single character, enclosed in single quotes, is
1693 Returns CHARACTER_LITERAL if a match is found.
1696 static enum ch_terminal
1697 match_character_literal (void)
1699 char *tokptr
= lexptr
;
1702 if ((*tokptr
== 'c' || *tokptr
== 'C') && (*(tokptr
+ 1) == '\''))
1704 /* We have a GNU chill extension form, so skip the leading "C'",
1705 decode the hex value, and then ensure that we have a trailing
1706 single quote character. */
1708 if (!decode_integer_value (16, &tokptr
, &ival
) || (*tokptr
!= '\''))
1714 else if (*tokptr
== '\'')
1718 /* Determine which form we have, either a control sequence or the
1719 single character form. */
1723 if (*(tokptr
+ 1) == '(')
1725 /* Match and decode a control sequence. Return zero if we don't
1726 find a valid integer literal, or if the next unconsumed character
1727 after the integer literal is not the trailing ')'. */
1729 if (!decode_integer_literal (&ival
, &tokptr
) || (*tokptr
++ != ')'))
1734 else if (*(tokptr
+ 1) == '^')
1741 error ("Invalid control sequence");
1743 else if (*tokptr
== '\'')
1745 /* this must be duplicated */
1754 /* The trailing quote has not yet been consumed. If we don't find
1755 it, then we have no match. */
1757 if (*tokptr
++ != '\'')
1764 /* Not a character literal. */
1767 yylval
.typed_val
.val
= ival
;
1768 yylval
.typed_val
.type
= builtin_type_chill_char
;
1770 return (CHARACTER_LITERAL
);
1773 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1774 Note that according to 5.2.4.2, a single "_" is also a valid integer
1775 literal, however GNU-chill requires there to be at least one "digit"
1776 in any integer literal. */
1778 static enum ch_terminal
1779 match_integer_literal (void)
1781 char *tokptr
= lexptr
;
1784 if (!decode_integer_literal (&ival
, &tokptr
))
1790 yylval
.typed_val
.val
= ival
;
1791 #if defined(CC_HAS_LONG_LONG)
1792 if (ival
> (LONGEST
) 2147483647U || ival
< -(LONGEST
) 2147483648U)
1793 yylval
.typed_val
.type
= builtin_type_long_long
;
1796 yylval
.typed_val
.type
= builtin_type_int
;
1798 return (INTEGER_LITERAL
);
1802 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1803 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1804 literal, however GNU-chill requires there to be at least one "digit"
1805 in any bit-string literal. */
1807 static enum ch_terminal
1808 match_bitstring_literal (void)
1810 register char *tokptr
= lexptr
;
1820 /* Look for the required explicit base specifier. */
1841 /* Ensure that the character after the explicit base is a single quote. */
1843 if (*tokptr
++ != '\'')
1848 while (*tokptr
!= '\0' && *tokptr
!= '\'')
1851 if (isupper (digit
))
1852 digit
= tolower (digit
);
1880 /* this is not a bitstring literal, probably an integer */
1883 if (digit
>= 1 << bits_per_char
)
1885 /* Found something not in domain for current base. */
1886 error ("Too-large digit in bitstring or integer.");
1890 /* Extract bits from digit, packing them into the bitstring byte. */
1891 int k
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? bits_per_char
- 1 : 0;
1892 for (; TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
>= 0 : k
< bits_per_char
;
1893 TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
-- : k
++)
1896 if (digit
& (1 << k
))
1898 tempbuf
[tempbufindex
] |=
1899 (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1900 ? (1 << (HOST_CHAR_BIT
- 1 - bitoffset
))
1904 if (bitoffset
== HOST_CHAR_BIT
)
1909 tempbuf
[tempbufindex
] = 0;
1915 /* Verify that we consumed everything up to the trailing single quote,
1916 and that we found some bits (IE not just underbars). */
1918 if (*tokptr
++ != '\'')
1924 yylval
.sval
.ptr
= tempbuf
;
1925 yylval
.sval
.length
= bitcount
;
1927 return (BIT_STRING_LITERAL
);
1937 static const struct token idtokentab
[] =
1961 {"addr", ADDR_TOKEN
},
1962 {"null", EMPTINESS_LITERAL
}
1965 static const struct token tokentab2
[] =
1967 {":=", GDB_ASSIGNMENT
},
1968 {"//", SLASH_SLASH
},
1975 /* Read one token, getting characters through lexptr. */
1976 /* This is where we will check to make sure that the language and the
1977 operators used are compatible. */
1979 static enum ch_terminal
1983 enum ch_terminal token
;
1987 /* Skip over any leading whitespace. */
1988 while (isspace (*lexptr
))
1992 /* Look for special single character cases which can't be the first
1993 character of some other multicharacter token. */
2010 /* Look for characters which start a particular kind of multicharacter
2011 token, such as a character literal, register name, convenience
2012 variable name, string literal, etc. */
2017 /* First try to match a string literal, which is any
2018 sequence of characters enclosed in matching single or double
2019 quotes, except that a single character inside single quotes
2020 is a character literal, so we have to catch that case also. */
2021 token
= match_string_literal ();
2026 if (*lexptr
== '\'')
2028 token
= match_character_literal ();
2037 token
= match_character_literal ();
2044 yylval
.sval
.ptr
= lexptr
;
2049 while (isalnum (*lexptr
) || *lexptr
== '_' || *lexptr
== '$');
2050 yylval
.sval
.length
= lexptr
- yylval
.sval
.ptr
;
2051 write_dollar_variable (yylval
.sval
);
2052 return GDB_VARIABLE
;
2055 /* See if it is a special token of length 2. */
2056 for (i
= 0; i
< sizeof (tokentab2
) / sizeof (tokentab2
[0]); i
++)
2058 if (STREQN (lexptr
, tokentab2
[i
].operator, 2))
2061 return (tokentab2
[i
].token
);
2064 /* Look for single character cases which which could be the first
2065 character of some other multicharacter token, but aren't, or we
2066 would already have found it. */
2076 /* Look for a float literal before looking for an integer literal, so
2077 we match as much of the input stream as possible. */
2078 token
= match_float_literal ();
2083 token
= match_bitstring_literal ();
2088 token
= match_integer_literal ();
2094 /* Try to match a simple name string, and if a match is found, then
2095 further classify what sort of name it is and return an appropriate
2096 token. Note that attempting to match a simple name string consumes
2097 the token from lexptr, so we can't back out if we later find that
2098 we can't classify what sort of name it is. */
2100 inputname
= match_simple_name_string ();
2102 if (inputname
!= NULL
)
2104 char *simplename
= (char *) alloca (strlen (inputname
) + 1);
2106 char *dptr
= simplename
, *sptr
= inputname
;
2107 for (; *sptr
; sptr
++)
2108 *dptr
++ = isupper (*sptr
) ? tolower (*sptr
) : *sptr
;
2111 /* See if it is a reserved identifier. */
2112 for (i
= 0; i
< sizeof (idtokentab
) / sizeof (idtokentab
[0]); i
++)
2114 if (STREQ (simplename
, idtokentab
[i
].operator))
2116 return (idtokentab
[i
].token
);
2120 /* Look for other special tokens. */
2121 if (STREQ (simplename
, "true"))
2124 return (BOOLEAN_LITERAL
);
2126 if (STREQ (simplename
, "false"))
2129 return (BOOLEAN_LITERAL
);
2132 sym
= lookup_symbol (inputname
, expression_context_block
,
2133 VAR_NAMESPACE
, (int *) NULL
,
2134 (struct symtab
**) NULL
);
2135 if (sym
== NULL
&& strcmp (inputname
, simplename
) != 0)
2137 sym
= lookup_symbol (simplename
, expression_context_block
,
2138 VAR_NAMESPACE
, (int *) NULL
,
2139 (struct symtab
**) NULL
);
2143 yylval
.ssym
.stoken
.ptr
= NULL
;
2144 yylval
.ssym
.stoken
.length
= 0;
2145 yylval
.ssym
.sym
= sym
;
2146 yylval
.ssym
.is_a_field_of_this
= 0; /* FIXME, C++'ism */
2147 switch (SYMBOL_CLASS (sym
))
2150 /* Found a procedure name. */
2151 return (GENERAL_PROCEDURE_NAME
);
2153 /* Found a global or local static variable. */
2154 return (LOCATION_NAME
);
2159 case LOC_REGPARM_ADDR
:
2163 case LOC_BASEREG_ARG
:
2164 if (innermost_block
== NULL
2165 || contained_in (block_found
, innermost_block
))
2167 innermost_block
= block_found
;
2169 return (LOCATION_NAME
);
2173 return (LOCATION_NAME
);
2176 yylval
.tsym
.type
= SYMBOL_TYPE (sym
);
2179 case LOC_CONST_BYTES
:
2180 case LOC_OPTIMIZED_OUT
:
2181 error ("Symbol \"%s\" names no location.", inputname
);
2184 internal_error (__FILE__
, __LINE__
,
2185 "unhandled SYMBOL_CLASS in ch_lex()");
2189 else if (!have_full_symbols () && !have_partial_symbols ())
2191 error ("No symbol table is loaded. Use the \"file\" command.");
2195 error ("No symbol \"%s\" in current context.", inputname
);
2199 /* Catch single character tokens which are not part of some
2204 case '.': /* Not float for example. */
2206 while (isspace (*lexptr
))
2208 inputname
= match_simple_name_string ();
2211 return DOT_FIELD_NAME
;
2214 return (ILLEGAL_TOKEN
);
2218 write_lower_upper_value (enum exp_opcode opcode
, /* Either UNOP_LOWER or UNOP_UPPER */
2222 write_exp_elt_opcode (opcode
);
2225 struct type
*result_type
;
2226 LONGEST val
= type_lower_upper (opcode
, type
, &result_type
);
2227 write_exp_elt_opcode (OP_LONG
);
2228 write_exp_elt_type (result_type
);
2229 write_exp_elt_longcst (val
);
2230 write_exp_elt_opcode (OP_LONG
);
2235 chill_error (char *msg
)