1 /* Parser for GNU CHILL (CCITT High-Level Language) -*- C -*-
2 Copyright (C) 1992, 1993, 1995, 2001 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 (__FILE__
, __LINE__
,
220 "too much lookahead");
221 if (terminal_buffer
[i
] == TOKEN_NOT_READ
)
223 terminal_buffer
[i
] = ch_lex ();
224 val_buffer
[i
] = yylval
;
226 return terminal_buffer
[i
];
232 pushback_token (enum ch_terminal code
, YYSTYPE node
)
235 if (terminal_buffer
[MAX_LOOK_AHEAD
] != TOKEN_NOT_READ
)
236 internal_error (__FILE__
, __LINE__
,
237 "cannot pushback token");
238 for (i
= MAX_LOOK_AHEAD
; i
> 0; i
--)
240 terminal_buffer
[i
] = terminal_buffer
[i
- 1];
241 val_buffer
[i
] = val_buffer
[i
- 1];
243 terminal_buffer
[0] = code
;
244 val_buffer
[0] = node
;
250 forward_token_ (void)
253 for (i
= 0; i
< MAX_LOOK_AHEAD
; i
++)
255 terminal_buffer
[i
] = terminal_buffer
[i
+ 1];
256 val_buffer
[i
] = val_buffer
[i
+ 1];
258 terminal_buffer
[MAX_LOOK_AHEAD
] = TOKEN_NOT_READ
;
260 #define FORWARD_TOKEN() forward_token_()
262 /* Skip the next token.
263 if it isn't TOKEN, the parser is broken. */
266 require (enum ch_terminal token
)
268 if (PEEK_TOKEN () != token
)
270 internal_error (__FILE__
, __LINE__
,
271 "expected token %d", (int) token
);
277 check_token (enum ch_terminal token
)
279 if (PEEK_TOKEN () != token
)
285 /* return 0 if expected token was not found,
289 expect (enum ch_terminal token
, char *message
)
291 if (PEEK_TOKEN () != token
)
295 else if (token
< 256)
296 error ("syntax error - expected a '%c' here \"%s\"", token
, lexptr
);
298 error ("syntax error");
307 /* Parse a name string. If ALLOW_ALL is 1, ALL is allowed as a postfix. */
310 parse_opt_name_string (int allow_all
)
312 int token
= PEEK_TOKEN ();
316 if (token
== ALL
&& allow_all
)
327 token
= PEEK_TOKEN ();
331 token
= PEEK_TOKEN ();
332 if (token
== ALL
&& allow_all
)
333 return get_identifier3 (IDENTIFIER_POINTER (name
), "!", "*");
337 error ("'%s!' is not followed by an identifier",
338 IDENTIFIER_POINTER (name
));
341 name
= get_identifier3 (IDENTIFIER_POINTER (name
),
342 "!", IDENTIFIER_POINTER (PEEK_LVAL ()));
347 parse_simple_name_string (void)
349 int token
= PEEK_TOKEN ();
353 error ("expected a name here");
354 return error_mark_node
;
362 parse_name_string (void)
364 tree name
= parse_opt_name_string (0);
368 error ("expected a name string here");
369 return error_mark_node
;
372 /* Matches: <name_string>
373 Returns if pass 1: the identifier.
374 Returns if pass 2: a decl or value for identifier. */
379 tree name
= parse_name_string ();
380 if (pass
== 1 || ignoring
)
384 tree decl
= lookup_name (name
);
385 if (decl
== NULL_TREE
)
387 error ("`%s' undeclared", IDENTIFIER_POINTER (name
));
388 return error_mark_node
;
390 else if (TREE_CODE (TREE_TYPE (decl
)) == ERROR_MARK
)
391 return error_mark_node
;
392 else if (TREE_CODE (decl
) == CONST_DECL
)
393 return DECL_INITIAL (decl
);
394 else if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
395 return convert_from_reference (decl
);
404 pushback_paren_expr (tree expr
)
406 if (pass
== 1 && !ignoring
)
407 expr
= build1 (PAREN_EXPR
, NULL_TREE
, expr
);
408 pushback_token (EXPR
, expr
);
412 /* Matches: <case label> */
415 parse_case_label (void)
417 if (check_token (ELSE
))
418 error ("ELSE in tuples labels not implemented");
419 /* Does not handle the case of a mode name. FIXME */
421 if (check_token (':'))
424 write_exp_elt_opcode (BINOP_RANGE
);
429 parse_opt_untyped_expr (void)
431 switch (PEEK_TOKEN ())
438 parse_untyped_expr ();
444 parse_unary_call (void)
452 /* Parse NAME '(' MODENAME ')'. */
457 parse_mode_call (void)
462 if (PEEK_TOKEN () != TYPENAME
)
463 error ("expect MODENAME here `%s'", lexptr
);
464 type
= PEEK_LVAL ().tsym
.type
;
473 parse_mode_or_normal_call (void)
478 if (PEEK_TOKEN () == TYPENAME
)
480 type
= PEEK_LVAL ().tsym
.type
;
492 /* Parse something that looks like a function call.
493 Assume we have parsed the function, and are at the '('. */
500 /* This is to save the value of arglist_len
501 being accumulated for each dimension. */
503 if (parse_opt_untyped_expr ())
505 int tok
= PEEK_TOKEN ();
507 if (tok
== UP
|| tok
== ':')
511 expect (')', "expected ')' to terminate slice");
513 write_exp_elt_opcode (tok
== UP
? TERNOP_SLICE_COUNT
517 while (check_token (','))
519 parse_untyped_expr ();
526 arg_count
= end_arglist ();
527 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
528 write_exp_elt_longcst (arg_count
);
529 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
533 parse_named_record_element (void)
538 label
= PEEK_LVAL ().sval
;
539 sprintf (buf
, "expected a field name here `%s'", lexptr
);
540 expect (DOT_FIELD_NAME
, buf
);
541 if (check_token (','))
542 parse_named_record_element ();
543 else if (check_token (':'))
546 error ("syntax error near `%s' in named record tuple element", lexptr
);
547 write_exp_elt_opcode (OP_LABELED
);
548 write_exp_string (label
);
549 write_exp_elt_opcode (OP_LABELED
);
552 /* Returns one or more TREE_LIST nodes, in reverse order. */
555 parse_tuple_element (struct type
*type
)
557 if (PEEK_TOKEN () == DOT_FIELD_NAME
)
559 /* Parse a labelled structure tuple. */
560 parse_named_record_element ();
564 if (check_token ('('))
566 if (check_token ('*'))
568 expect (')', "missing ')' after '*' case label list");
571 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
573 /* do this as a range from low to high */
574 struct type
*range_type
= TYPE_FIELD_TYPE (type
, 0);
575 LONGEST low_bound
, high_bound
;
576 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
577 error ("cannot determine bounds for (*)");
579 write_exp_elt_opcode (OP_LONG
);
580 write_exp_elt_type (range_type
);
581 write_exp_elt_longcst (low_bound
);
582 write_exp_elt_opcode (OP_LONG
);
584 write_exp_elt_opcode (OP_LONG
);
585 write_exp_elt_type (range_type
);
586 write_exp_elt_longcst (high_bound
);
587 write_exp_elt_opcode (OP_LONG
);
588 write_exp_elt_opcode (BINOP_RANGE
);
591 error ("(*) in invalid context");
594 error ("(*) only possible with modename in front of tuple (mode[..])");
599 while (check_token (','))
602 write_exp_elt_opcode (BINOP_COMMA
);
608 parse_untyped_expr ();
609 if (check_token (':'))
611 /* A powerset range or a labeled Array. */
612 parse_untyped_expr ();
613 write_exp_elt_opcode (BINOP_RANGE
);
617 /* Matches: a COMMA-separated list of tuple elements.
618 Returns a list (of TREE_LIST nodes). */
620 parse_opt_element_list (struct type
*type
)
623 if (PEEK_TOKEN () == ']')
627 parse_tuple_element (type
);
629 if (PEEK_TOKEN () == ']')
631 if (!check_token (','))
632 error ("bad syntax in tuple");
636 /* Parses: '[' elements ']'
637 If modename is non-NULL it prefixed the tuple. */
640 parse_tuple (struct type
*mode
)
644 type
= check_typedef (mode
);
649 parse_opt_element_list (type
);
650 expect (']', "missing ']' after tuple");
651 write_exp_elt_opcode (OP_ARRAY
);
652 write_exp_elt_longcst ((LONGEST
) 0);
653 write_exp_elt_longcst ((LONGEST
) end_arglist () - 1);
654 write_exp_elt_opcode (OP_ARRAY
);
657 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
658 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
659 && TYPE_CODE (type
) != TYPE_CODE_SET
)
660 error ("invalid tuple mode");
661 write_exp_elt_opcode (UNOP_CAST
);
662 write_exp_elt_type (mode
);
663 write_exp_elt_opcode (UNOP_CAST
);
673 switch (PEEK_TOKEN ())
675 case INTEGER_LITERAL
:
676 case CHARACTER_LITERAL
:
677 write_exp_elt_opcode (OP_LONG
);
678 write_exp_elt_type (PEEK_LVAL ().typed_val
.type
);
679 write_exp_elt_longcst (PEEK_LVAL ().typed_val
.val
);
680 write_exp_elt_opcode (OP_LONG
);
683 case BOOLEAN_LITERAL
:
684 write_exp_elt_opcode (OP_BOOL
);
685 write_exp_elt_longcst ((LONGEST
) PEEK_LVAL ().ulval
);
686 write_exp_elt_opcode (OP_BOOL
);
690 write_exp_elt_opcode (OP_DOUBLE
);
691 write_exp_elt_type (builtin_type_double
);
692 write_exp_elt_dblcst (PEEK_LVAL ().dval
);
693 write_exp_elt_opcode (OP_DOUBLE
);
696 case EMPTINESS_LITERAL
:
697 write_exp_elt_opcode (OP_LONG
);
698 write_exp_elt_type (lookup_pointer_type (builtin_type_void
));
699 write_exp_elt_longcst (0);
700 write_exp_elt_opcode (OP_LONG
);
703 case CHARACTER_STRING_LITERAL
:
704 write_exp_elt_opcode (OP_STRING
);
705 write_exp_string (PEEK_LVAL ().sval
);
706 write_exp_elt_opcode (OP_STRING
);
709 case BIT_STRING_LITERAL
:
710 write_exp_elt_opcode (OP_BITSTRING
);
711 write_exp_bitstring (PEEK_LVAL ().sval
);
712 write_exp_elt_opcode (OP_BITSTRING
);
717 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
718 which casts to an artificial array. */
721 if (PEEK_TOKEN () != TYPENAME
)
722 error ("missing MODENAME after ARRAY()");
723 type
= PEEK_LVAL ().tsym
.type
;
727 expect (')', "missing right parenthesis");
728 type
= create_array_type ((struct type
*) NULL
, type
,
729 create_range_type ((struct type
*) NULL
,
730 builtin_type_int
, 0, 0));
731 TYPE_ARRAY_UPPER_BOUND_TYPE (type
) = BOUND_CANNOT_BE_DETERMINED
;
732 write_exp_elt_opcode (UNOP_CAST
);
733 write_exp_elt_type (type
);
734 write_exp_elt_opcode (UNOP_CAST
);
746 expect (')', "missing right parenthesis");
751 case GENERAL_PROCEDURE_NAME
:
753 write_exp_elt_opcode (OP_VAR_VALUE
);
754 write_exp_elt_block (NULL
);
755 write_exp_elt_sym (PEEK_LVAL ().ssym
.sym
);
756 write_exp_elt_opcode (OP_VAR_VALUE
);
759 case GDB_VARIABLE
: /* gdb specific */
764 write_exp_elt_opcode (UNOP_CAST
);
765 write_exp_elt_type (builtin_type_int
);
766 write_exp_elt_opcode (UNOP_CAST
);
770 write_exp_elt_opcode (UNOP_CARD
);
774 write_exp_elt_opcode (UNOP_CHMAX
);
778 write_exp_elt_opcode (UNOP_CHMIN
);
782 goto unimplemented_unary_builtin
;
785 goto unimplemented_unary_builtin
;
788 goto unimplemented_unary_builtin
;
789 unimplemented_unary_builtin
:
791 error ("not implemented: %s builtin function", op_name
);
795 write_exp_elt_opcode (UNOP_ADDR
);
798 type
= parse_mode_or_normal_call ();
801 write_exp_elt_opcode (OP_LONG
);
802 write_exp_elt_type (builtin_type_int
);
803 CHECK_TYPEDEF (type
);
804 write_exp_elt_longcst ((LONGEST
) TYPE_LENGTH (type
));
805 write_exp_elt_opcode (OP_LONG
);
808 write_exp_elt_opcode (UNOP_SIZEOF
);
817 type
= parse_mode_or_normal_call ();
818 write_lower_upper_value (op
, type
);
822 write_exp_elt_opcode (UNOP_LENGTH
);
825 type
= PEEK_LVAL ().tsym
.type
;
827 switch (PEEK_TOKEN ())
835 expect (')', "missing right parenthesis");
836 write_exp_elt_opcode (UNOP_CAST
);
837 write_exp_elt_type (type
);
838 write_exp_elt_opcode (UNOP_CAST
);
841 error ("typename in invalid context");
846 error ("invalid expression syntax at `%s'", lexptr
);
850 switch (PEEK_TOKEN ())
853 write_exp_elt_opcode (STRUCTOP_STRUCT
);
854 write_exp_string (PEEK_LVAL ().sval
);
855 write_exp_elt_opcode (STRUCTOP_STRUCT
);
860 if (PEEK_TOKEN () == TYPENAME
)
862 type
= PEEK_LVAL ().tsym
.type
;
863 write_exp_elt_opcode (UNOP_CAST
);
864 write_exp_elt_type (lookup_pointer_type (type
));
865 write_exp_elt_opcode (UNOP_CAST
);
868 write_exp_elt_opcode (UNOP_IND
);
873 case CHARACTER_STRING_LITERAL
:
874 case CHARACTER_LITERAL
:
875 case BIT_STRING_LITERAL
:
876 /* Handle string repetition. (See comment in parse_operand5.) */
878 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
879 write_exp_elt_longcst (1);
880 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
884 case INTEGER_LITERAL
:
885 case BOOLEAN_LITERAL
:
887 case GENERAL_PROCEDURE_NAME
:
889 case EMPTINESS_LITERAL
:
938 parse_operand6 (void)
940 if (check_token (RECEIVE
))
943 error ("not implemented: RECEIVE expression");
945 else if (check_token (POINTER
))
948 write_exp_elt_opcode (UNOP_ADDR
);
955 parse_operand5 (void)
958 /* We are supposed to be looking for a <string repetition operator>,
959 but in general we can't distinguish that from a parenthesized
960 expression. This is especially difficult if we allow the
961 string operand to be a constant expression (as requested by
962 some users), and not just a string literal.
963 Consider: LPRN expr RPRN LPRN expr RPRN
964 Is that a function call or string repetition?
965 Instead, we handle string repetition in parse_primval,
966 and build_generalized_call. */
967 switch (PEEK_TOKEN ())
970 op
= UNOP_LOGICAL_NOT
;
982 write_exp_elt_opcode (op
);
986 parse_operand4 (void)
992 switch (PEEK_TOKEN ())
1011 write_exp_elt_opcode (op
);
1016 parse_operand3 (void)
1022 switch (PEEK_TOKEN ())
1038 write_exp_elt_opcode (op
);
1043 parse_operand2 (void)
1049 if (check_token (IN
))
1052 write_exp_elt_opcode (BINOP_IN
);
1056 switch (PEEK_TOKEN ())
1074 op
= BINOP_NOTEQUAL
;
1081 write_exp_elt_opcode (op
);
1087 parse_operand1 (void)
1093 switch (PEEK_TOKEN ())
1096 op
= BINOP_BITWISE_AND
;
1099 op
= BINOP_LOGICAL_AND
;
1106 write_exp_elt_opcode (op
);
1111 parse_operand0 (void)
1117 switch (PEEK_TOKEN ())
1120 op
= BINOP_BITWISE_IOR
;
1123 op
= BINOP_BITWISE_XOR
;
1126 op
= BINOP_LOGICAL_OR
;
1133 write_exp_elt_opcode (op
);
1141 if (check_token (GDB_ASSIGNMENT
))
1144 write_exp_elt_opcode (BINOP_ASSIGN
);
1149 parse_then_alternative (void)
1151 expect (THEN
, "missing 'THEN' in 'IF' expression");
1156 parse_else_alternative (void)
1158 if (check_token (ELSIF
))
1159 parse_if_expression_body ();
1160 else if (check_token (ELSE
))
1163 error ("missing ELSE/ELSIF in IF expression");
1166 /* Matches: <boolean expression> <then alternative> <else alternative> */
1169 parse_if_expression_body (void)
1172 parse_then_alternative ();
1173 parse_else_alternative ();
1174 write_exp_elt_opcode (TERNOP_COND
);
1178 parse_if_expression (void)
1181 parse_if_expression_body ();
1182 expect (FI
, "missing 'FI' at end of conditional expression");
1185 /* An <untyped_expr> is a superset of <expr>. It also includes
1186 <conditional expressions> and untyped <tuples>, whose types
1187 are not given by their constituents. Hence, these are only
1188 allowed in certain contexts that expect a certain type.
1189 You should call convert() to fix up the <untyped_expr>. */
1192 parse_untyped_expr (void)
1194 switch (PEEK_TOKEN ())
1197 parse_if_expression ();
1200 error ("not implemented: CASE expression");
1202 switch (PEEK_TOKEN1 ())
1210 parse_untyped_expr ();
1211 expect (')', "missing ')'");
1224 terminal_buffer
[0] = TOKEN_NOT_READ
;
1225 if (PEEK_TOKEN () == TYPENAME
&& PEEK_TOKEN1 () == END_TOKEN
)
1227 write_exp_elt_opcode (OP_TYPE
);
1228 write_exp_elt_type (PEEK_LVAL ().tsym
.type
);
1229 write_exp_elt_opcode (OP_TYPE
);
1234 if (terminal_buffer
[0] != END_TOKEN
)
1236 if (comma_terminates
&& terminal_buffer
[0] == ',')
1237 lexptr
--; /* Put the comma back. */
1239 error ("Junk after end of expression.");
1245 /* Implementation of a dynamically expandable buffer for processing input
1246 characters acquired through lexptr and building a value to return in
1249 static char *tempbuf
; /* Current buffer contents */
1250 static int tempbufsize
; /* Size of allocated buffer */
1251 static int tempbufindex
; /* Current index into buffer */
1253 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1255 #define CHECKBUF(size) \
1257 if (tempbufindex + (size) >= tempbufsize) \
1259 growbuf_by_size (size); \
1263 /* Grow the static temp buffer if necessary, including allocating the first one
1267 growbuf_by_size (int count
)
1271 growby
= max (count
, GROWBY_MIN_SIZE
);
1272 tempbufsize
+= growby
;
1273 if (tempbuf
== NULL
)
1275 tempbuf
= (char *) xmalloc (tempbufsize
);
1279 tempbuf
= (char *) xrealloc (tempbuf
, tempbufsize
);
1283 /* Try to consume a simple name string token. If successful, returns
1284 a pointer to a nullbyte terminated copy of the name that can be used
1285 in symbol table lookups. If not successful, returns NULL. */
1288 match_simple_name_string (void)
1290 char *tokptr
= lexptr
;
1292 if (isalpha (*tokptr
) || *tokptr
== '_')
1299 while (isalnum (*tokptr
) || (*tokptr
== '_'));
1300 yylval
.sval
.ptr
= lexptr
;
1301 yylval
.sval
.length
= tokptr
- lexptr
;
1303 result
= copy_name (yylval
.sval
);
1309 /* Start looking for a value composed of valid digits as set by the base
1310 in use. Note that '_' characters are valid anywhere, in any quantity,
1311 and are simply ignored. Since we must find at least one valid digit,
1312 or reject this token as an integer literal, we keep track of how many
1313 digits we have encountered. */
1316 decode_integer_value (int base
, char **tokptrptr
, LONGEST
*ivalptr
)
1318 char *tokptr
= *tokptrptr
;
1322 while (*tokptr
!= '\0')
1326 temp
= tolower (temp
);
1365 /* Found something not in domain for current base. */
1366 tokptr
--; /* Unconsume what gave us indigestion. */
1371 /* If we didn't find any digits, then we don't have a valid integer
1372 value, so reject the entire token. Otherwise, update the lexical
1373 scan pointer, and return non-zero for success. */
1381 *tokptrptr
= tokptr
;
1387 decode_integer_literal (LONGEST
*valptr
, char **tokptrptr
)
1389 char *tokptr
= *tokptrptr
;
1392 int explicit_base
= 0;
1394 /* Look for an explicit base specifier, which is optional. */
1427 /* If we found an explicit base ensure that the character after the
1428 explicit base is a single quote. */
1430 if (explicit_base
&& (*tokptr
++ != '\''))
1435 /* Attempt to decode whatever follows as an integer value in the
1436 indicated base, updating the token pointer in the process and
1437 computing the value into ival. Also, if we have an explicit
1438 base, then the next character must not be a single quote, or we
1439 have a bitstring literal, so reject the entire token in this case.
1440 Otherwise, update the lexical scan pointer, and return non-zero
1443 if (!decode_integer_value (base
, &tokptr
, &ival
))
1447 else if (explicit_base
&& (*tokptr
== '\''))
1454 *tokptrptr
= tokptr
;
1459 /* If it wasn't for the fact that floating point values can contain '_'
1460 characters, we could just let strtod do all the hard work by letting it
1461 try to consume as much of the current token buffer as possible and
1462 find a legal conversion. Unfortunately we need to filter out the '_'
1463 characters before calling strtod, which we do by copying the other
1464 legal chars to a local buffer to be converted. However since we also
1465 need to keep track of where the last unconsumed character in the input
1466 buffer is, we have transfer only as many characters as may compose a
1467 legal floating point value. */
1469 static enum ch_terminal
1470 match_float_literal (void)
1472 char *tokptr
= lexptr
;
1476 extern double strtod ();
1478 /* Make local buffer in which to build the string to convert. This is
1479 required because underscores are valid in chill floating point numbers
1480 but not in the string passed to strtod to convert. The string will be
1481 no longer than our input string. */
1483 copy
= buf
= (char *) alloca (strlen (tokptr
) + 1);
1485 /* Transfer all leading digits to the conversion buffer, discarding any
1488 while (isdigit (*tokptr
) || *tokptr
== '_')
1497 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1498 of whether we found any leading digits, and we simply accept it and
1499 continue on to look for the fractional part and/or exponent. One of
1500 [eEdD] is legal only if we have seen digits, and means that there
1501 is no fractional part. If we find neither of these, then this is
1502 not a floating point number, so return failure. */
1507 /* Accept and then look for fractional part and/or exponent. */
1520 goto collect_exponent
;
1528 /* We found a '.', copy any fractional digits to the conversion buffer, up
1529 to the first nondigit, non-underscore character. */
1531 while (isdigit (*tokptr
) || *tokptr
== '_')
1540 /* Look for an exponent, which must start with one of [eEdD]. If none
1541 is found, jump directly to trying to convert what we have collected
1558 /* Accept an optional '-' or '+' following one of [eEdD]. */
1561 if (*tokptr
== '+' || *tokptr
== '-')
1563 *copy
++ = *tokptr
++;
1566 /* Now copy an exponent into the conversion buffer. Note that at the
1567 moment underscores are *not* allowed in exponents. */
1569 while (isdigit (*tokptr
))
1571 *copy
++ = *tokptr
++;
1574 /* If we transfered any chars to the conversion buffer, try to interpret its
1575 contents as a floating point value. If any characters remain, then we
1576 must not have a valid floating point string. */
1582 dval
= strtod (buf
, ©
);
1587 return (FLOAT_LITERAL
);
1593 /* Recognize a string literal. A string literal is a sequence
1594 of characters enclosed in matching single or double quotes, except that
1595 a single character inside single quotes is a character literal, which
1596 we reject as a string literal. To embed the terminator character inside
1597 a string, it is simply doubled (I.E. "this""is""one""string") */
1599 static enum ch_terminal
1600 match_string_literal (void)
1602 char *tokptr
= lexptr
;
1606 for (tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1612 /* skip possible whitespaces */
1613 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1621 else if (*tokptr
!= ',')
1622 error ("Invalid control sequence");
1624 /* skip possible whitespaces */
1625 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1627 if (!decode_integer_literal (&ival
, &tokptr
))
1628 error ("Invalid control sequence");
1631 else if (*tokptr
== *lexptr
)
1633 if (*(tokptr
+ 1) == *lexptr
)
1642 else if (*tokptr
== '^')
1644 if (*(tokptr
+ 1) == '(')
1648 if (!decode_integer_literal (&ival
, &tokptr
))
1649 error ("Invalid control sequence");
1652 else if (*(tokptr
+ 1) == '^')
1655 error ("Invalid control sequence");
1659 tempbuf
[tempbufindex
++] = ival
;
1662 error ("Invalid control sequence");
1664 if (*tokptr
== '\0' /* no terminator */
1665 || (tempbufindex
== 1 && *tokptr
== '\'')) /* char literal */
1671 tempbuf
[tempbufindex
] = '\0';
1672 yylval
.sval
.ptr
= tempbuf
;
1673 yylval
.sval
.length
= tempbufindex
;
1675 return (CHARACTER_STRING_LITERAL
);
1679 /* Recognize a character literal. A character literal is single character
1680 or a control sequence, enclosed in single quotes. A control sequence
1681 is a comma separated list of one or more integer literals, enclosed
1682 in parenthesis and introduced with a circumflex character.
1684 EX: 'a' '^(7)' '^(7,8)'
1686 As a GNU chill extension, the syntax C'xx' is also recognized as a
1687 character literal, where xx is a hex value for the character.
1689 Note that more than a single character, enclosed in single quotes, is
1692 Returns CHARACTER_LITERAL if a match is found.
1695 static enum ch_terminal
1696 match_character_literal (void)
1698 char *tokptr
= lexptr
;
1701 if ((*tokptr
== 'c' || *tokptr
== 'C') && (*(tokptr
+ 1) == '\''))
1703 /* We have a GNU chill extension form, so skip the leading "C'",
1704 decode the hex value, and then ensure that we have a trailing
1705 single quote character. */
1707 if (!decode_integer_value (16, &tokptr
, &ival
) || (*tokptr
!= '\''))
1713 else if (*tokptr
== '\'')
1717 /* Determine which form we have, either a control sequence or the
1718 single character form. */
1722 if (*(tokptr
+ 1) == '(')
1724 /* Match and decode a control sequence. Return zero if we don't
1725 find a valid integer literal, or if the next unconsumed character
1726 after the integer literal is not the trailing ')'. */
1728 if (!decode_integer_literal (&ival
, &tokptr
) || (*tokptr
++ != ')'))
1733 else if (*(tokptr
+ 1) == '^')
1740 error ("Invalid control sequence");
1742 else if (*tokptr
== '\'')
1744 /* this must be duplicated */
1753 /* The trailing quote has not yet been consumed. If we don't find
1754 it, then we have no match. */
1756 if (*tokptr
++ != '\'')
1763 /* Not a character literal. */
1766 yylval
.typed_val
.val
= ival
;
1767 yylval
.typed_val
.type
= builtin_type_chill_char
;
1769 return (CHARACTER_LITERAL
);
1772 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1773 Note that according to 5.2.4.2, a single "_" is also a valid integer
1774 literal, however GNU-chill requires there to be at least one "digit"
1775 in any integer literal. */
1777 static enum ch_terminal
1778 match_integer_literal (void)
1780 char *tokptr
= lexptr
;
1783 if (!decode_integer_literal (&ival
, &tokptr
))
1789 yylval
.typed_val
.val
= ival
;
1790 #if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
1791 if (ival
> (LONGEST
) 2147483647U || ival
< -(LONGEST
) 2147483648U)
1792 yylval
.typed_val
.type
= builtin_type_long_long
;
1795 yylval
.typed_val
.type
= builtin_type_int
;
1797 return (INTEGER_LITERAL
);
1801 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1802 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1803 literal, however GNU-chill requires there to be at least one "digit"
1804 in any bit-string literal. */
1806 static enum ch_terminal
1807 match_bitstring_literal (void)
1809 register char *tokptr
= lexptr
;
1819 /* Look for the required explicit base specifier. */
1840 /* Ensure that the character after the explicit base is a single quote. */
1842 if (*tokptr
++ != '\'')
1847 while (*tokptr
!= '\0' && *tokptr
!= '\'')
1850 if (isupper (digit
))
1851 digit
= tolower (digit
);
1879 /* this is not a bitstring literal, probably an integer */
1882 if (digit
>= 1 << bits_per_char
)
1884 /* Found something not in domain for current base. */
1885 error ("Too-large digit in bitstring or integer.");
1889 /* Extract bits from digit, packing them into the bitstring byte. */
1890 int k
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? bits_per_char
- 1 : 0;
1891 for (; TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
>= 0 : k
< bits_per_char
;
1892 TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
-- : k
++)
1895 if (digit
& (1 << k
))
1897 tempbuf
[tempbufindex
] |=
1898 (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1899 ? (1 << (HOST_CHAR_BIT
- 1 - bitoffset
))
1903 if (bitoffset
== HOST_CHAR_BIT
)
1908 tempbuf
[tempbufindex
] = 0;
1914 /* Verify that we consumed everything up to the trailing single quote,
1915 and that we found some bits (IE not just underbars). */
1917 if (*tokptr
++ != '\'')
1923 yylval
.sval
.ptr
= tempbuf
;
1924 yylval
.sval
.length
= bitcount
;
1926 return (BIT_STRING_LITERAL
);
1936 static const struct token idtokentab
[] =
1960 {"addr", ADDR_TOKEN
},
1961 {"null", EMPTINESS_LITERAL
}
1964 static const struct token tokentab2
[] =
1966 {":=", GDB_ASSIGNMENT
},
1967 {"//", SLASH_SLASH
},
1974 /* Read one token, getting characters through lexptr. */
1975 /* This is where we will check to make sure that the language and the
1976 operators used are compatible. */
1978 static enum ch_terminal
1982 enum ch_terminal token
;
1986 /* Skip over any leading whitespace. */
1987 while (isspace (*lexptr
))
1991 /* Look for special single character cases which can't be the first
1992 character of some other multicharacter token. */
2009 /* Look for characters which start a particular kind of multicharacter
2010 token, such as a character literal, register name, convenience
2011 variable name, string literal, etc. */
2016 /* First try to match a string literal, which is any
2017 sequence of characters enclosed in matching single or double
2018 quotes, except that a single character inside single quotes
2019 is a character literal, so we have to catch that case also. */
2020 token
= match_string_literal ();
2025 if (*lexptr
== '\'')
2027 token
= match_character_literal ();
2036 token
= match_character_literal ();
2043 yylval
.sval
.ptr
= lexptr
;
2048 while (isalnum (*lexptr
) || *lexptr
== '_' || *lexptr
== '$');
2049 yylval
.sval
.length
= lexptr
- yylval
.sval
.ptr
;
2050 write_dollar_variable (yylval
.sval
);
2051 return GDB_VARIABLE
;
2054 /* See if it is a special token of length 2. */
2055 for (i
= 0; i
< sizeof (tokentab2
) / sizeof (tokentab2
[0]); i
++)
2057 if (STREQN (lexptr
, tokentab2
[i
].operator, 2))
2060 return (tokentab2
[i
].token
);
2063 /* Look for single character cases which which could be the first
2064 character of some other multicharacter token, but aren't, or we
2065 would already have found it. */
2075 /* Look for a float literal before looking for an integer literal, so
2076 we match as much of the input stream as possible. */
2077 token
= match_float_literal ();
2082 token
= match_bitstring_literal ();
2087 token
= match_integer_literal ();
2093 /* Try to match a simple name string, and if a match is found, then
2094 further classify what sort of name it is and return an appropriate
2095 token. Note that attempting to match a simple name string consumes
2096 the token from lexptr, so we can't back out if we later find that
2097 we can't classify what sort of name it is. */
2099 inputname
= match_simple_name_string ();
2101 if (inputname
!= NULL
)
2103 char *simplename
= (char *) alloca (strlen (inputname
) + 1);
2105 char *dptr
= simplename
, *sptr
= inputname
;
2106 for (; *sptr
; sptr
++)
2107 *dptr
++ = isupper (*sptr
) ? tolower (*sptr
) : *sptr
;
2110 /* See if it is a reserved identifier. */
2111 for (i
= 0; i
< sizeof (idtokentab
) / sizeof (idtokentab
[0]); i
++)
2113 if (STREQ (simplename
, idtokentab
[i
].operator))
2115 return (idtokentab
[i
].token
);
2119 /* Look for other special tokens. */
2120 if (STREQ (simplename
, "true"))
2123 return (BOOLEAN_LITERAL
);
2125 if (STREQ (simplename
, "false"))
2128 return (BOOLEAN_LITERAL
);
2131 sym
= lookup_symbol (inputname
, expression_context_block
,
2132 VAR_NAMESPACE
, (int *) NULL
,
2133 (struct symtab
**) NULL
);
2134 if (sym
== NULL
&& strcmp (inputname
, simplename
) != 0)
2136 sym
= lookup_symbol (simplename
, expression_context_block
,
2137 VAR_NAMESPACE
, (int *) NULL
,
2138 (struct symtab
**) NULL
);
2142 yylval
.ssym
.stoken
.ptr
= NULL
;
2143 yylval
.ssym
.stoken
.length
= 0;
2144 yylval
.ssym
.sym
= sym
;
2145 yylval
.ssym
.is_a_field_of_this
= 0; /* FIXME, C++'ism */
2146 switch (SYMBOL_CLASS (sym
))
2149 /* Found a procedure name. */
2150 return (GENERAL_PROCEDURE_NAME
);
2152 /* Found a global or local static variable. */
2153 return (LOCATION_NAME
);
2158 case LOC_REGPARM_ADDR
:
2162 case LOC_BASEREG_ARG
:
2163 if (innermost_block
== NULL
2164 || contained_in (block_found
, innermost_block
))
2166 innermost_block
= block_found
;
2168 return (LOCATION_NAME
);
2172 return (LOCATION_NAME
);
2175 yylval
.tsym
.type
= SYMBOL_TYPE (sym
);
2178 case LOC_CONST_BYTES
:
2179 case LOC_OPTIMIZED_OUT
:
2180 error ("Symbol \"%s\" names no location.", inputname
);
2183 internal_error (__FILE__
, __LINE__
,
2184 "unhandled SYMBOL_CLASS in ch_lex()");
2188 else if (!have_full_symbols () && !have_partial_symbols ())
2190 error ("No symbol table is loaded. Use the \"file\" command.");
2194 error ("No symbol \"%s\" in current context.", inputname
);
2198 /* Catch single character tokens which are not part of some
2203 case '.': /* Not float for example. */
2205 while (isspace (*lexptr
))
2207 inputname
= match_simple_name_string ();
2210 return DOT_FIELD_NAME
;
2213 return (ILLEGAL_TOKEN
);
2217 write_lower_upper_value (enum exp_opcode opcode
, /* Either UNOP_LOWER or UNOP_UPPER */
2221 write_exp_elt_opcode (opcode
);
2224 struct type
*result_type
;
2225 LONGEST val
= type_lower_upper (opcode
, type
, &result_type
);
2226 write_exp_elt_opcode (OP_LONG
);
2227 write_exp_elt_type (result_type
);
2228 write_exp_elt_longcst (val
);
2229 write_exp_elt_opcode (OP_LONG
);
2234 chill_error (char *msg
)