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, Boston, MA 02111-1307, USA. */
20 /* Parse a Chill expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that the language accepted by this parser is more liberal
30 than the one accepted by an actual Chill compiler. For example, the
31 language rule that a simple name string can not be one of the reserved
32 simple name strings is not enforced (e.g "case" is not treated as a
33 reserved name). Another example is that Chill is a strongly typed
34 language, and certain expressions that violate the type constraints
35 may still be evaluated if gdb can do so in a meaningful manner, while
36 such expressions would be rejected by the compiler. The reason for
37 this more liberal behavior is the philosophy that the debugger
38 is intended to be a tool that is used by the programmer when things
39 go wrong, and as such, it should provide as few artificial barriers
40 to it's use as possible. If it can do something meaningful, even
41 something that violates language contraints that are enforced by the
42 compiler, it should do so without complaint.
47 #include "gdb_string.h"
49 #include "expression.h"
52 #include "parser-defs.h"
54 #include "bfd.h" /* Required by objfiles.h. */
55 #include "symfile.h" /* Required by objfiles.h. */
56 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
59 #define INLINE __inline__
81 /* '\001' ... '\xff' come first. */
88 GENERAL_PROCEDURE_NAME
,
91 CHARACTER_STRING_LITERAL
,
94 DOT_FIELD_NAME
, /* '.' followed by <field name> */
137 /* Forward declarations. */
139 static void write_lower_upper_value
PARAMS ((enum exp_opcode
, struct type
*));
140 static enum ch_terminal match_bitstring_literal
PARAMS ((void));
141 static enum ch_terminal match_integer_literal
PARAMS ((void));
142 static enum ch_terminal match_character_literal
PARAMS ((void));
143 static enum ch_terminal match_string_literal
PARAMS ((void));
144 static enum ch_terminal match_float_literal
PARAMS ((void));
145 static enum ch_terminal match_float_literal
PARAMS ((void));
146 static int decode_integer_literal
PARAMS ((LONGEST
*, char **));
147 static int decode_integer_value
PARAMS ((int, char **, LONGEST
*));
148 static char *match_simple_name_string
PARAMS ((void));
149 static void growbuf_by_size
PARAMS ((int));
150 static void parse_untyped_expr
PARAMS ((void));
151 static void parse_if_expression
PARAMS ((void));
152 static void parse_else_alternative
PARAMS ((void));
153 static void parse_then_alternative
PARAMS ((void));
154 static void parse_expr
PARAMS ((void));
155 static void parse_operand0
PARAMS ((void));
156 static void parse_operand1
PARAMS ((void));
157 static void parse_operand2
PARAMS ((void));
158 static void parse_operand3
PARAMS ((void));
159 static void parse_operand4
PARAMS ((void));
160 static void parse_operand5
PARAMS ((void));
161 static void parse_operand6
PARAMS ((void));
162 static void parse_primval
PARAMS ((void));
163 static void parse_tuple
PARAMS ((struct type
*));
164 static void parse_opt_element_list
PARAMS ((struct type
*));
165 static void parse_tuple_element
PARAMS ((struct type
*));
166 static void parse_named_record_element
PARAMS ((void));
167 static void parse_call
PARAMS ((void));
168 static struct type
*parse_mode_or_normal_call
PARAMS ((void));
170 static struct type
*parse_mode_call
PARAMS ((void));
172 static void parse_unary_call
PARAMS ((void));
173 static int parse_opt_untyped_expr
PARAMS ((void));
174 static void parse_case_label
PARAMS ((void));
175 static int expect
PARAMS ((enum ch_terminal
, char *));
176 static void parse_expr
PARAMS ((void));
177 static void parse_primval
PARAMS ((void));
178 static void parse_untyped_expr
PARAMS ((void));
179 static int parse_opt_untyped_expr
PARAMS ((void));
180 static void parse_if_expression_body
PARAMS((void));
181 static enum ch_terminal ch_lex
PARAMS ((void));
182 INLINE
static enum ch_terminal PEEK_TOKEN
PARAMS ((void));
183 static enum ch_terminal peek_token_
PARAMS ((int));
184 static void forward_token_
PARAMS ((void));
185 static void require
PARAMS ((enum ch_terminal
));
186 static int check_token
PARAMS ((enum ch_terminal
));
188 #define MAX_LOOK_AHEAD 2
189 static enum ch_terminal terminal_buffer
[MAX_LOOK_AHEAD
+1] = {
190 TOKEN_NOT_READ
, TOKEN_NOT_READ
, TOKEN_NOT_READ
};
191 static YYSTYPE yylval
;
192 static YYSTYPE val_buffer
[MAX_LOOK_AHEAD
+1];
194 /*int current_token, lookahead_token;*/
196 INLINE
static enum ch_terminal
199 if (terminal_buffer
[0] == TOKEN_NOT_READ
)
201 terminal_buffer
[0] = ch_lex ();
202 val_buffer
[0] = yylval
;
204 return terminal_buffer
[0];
206 #define PEEK_LVAL() val_buffer[0]
207 #define PEEK_TOKEN1() peek_token_(1)
208 #define PEEK_TOKEN2() peek_token_(2)
209 static enum ch_terminal
213 if (i
> MAX_LOOK_AHEAD
)
214 fatal ("internal error - too much lookahead");
215 if (terminal_buffer
[i
] == TOKEN_NOT_READ
)
217 terminal_buffer
[i
] = ch_lex ();
218 val_buffer
[i
] = yylval
;
220 return terminal_buffer
[i
];
226 pushback_token (code
, node
)
227 enum ch_terminal code
;
231 if (terminal_buffer
[MAX_LOOK_AHEAD
] != TOKEN_NOT_READ
)
232 fatal ("internal error - cannot pushback token");
233 for (i
= MAX_LOOK_AHEAD
; i
> 0; i
--)
235 terminal_buffer
[i
] = terminal_buffer
[i
- 1];
236 val_buffer
[i
] = val_buffer
[i
- 1];
238 terminal_buffer
[0] = code
;
239 val_buffer
[0] = node
;
248 for (i
= 0; i
< MAX_LOOK_AHEAD
; i
++)
250 terminal_buffer
[i
] = terminal_buffer
[i
+1];
251 val_buffer
[i
] = val_buffer
[i
+1];
253 terminal_buffer
[MAX_LOOK_AHEAD
] = TOKEN_NOT_READ
;
255 #define FORWARD_TOKEN() forward_token_()
257 /* Skip the next token.
258 if it isn't TOKEN, the parser is broken. */
262 enum ch_terminal token
;
264 if (PEEK_TOKEN() != token
)
267 sprintf (buf
, "internal parser error - expected token %d", (int)token
);
275 enum ch_terminal token
;
277 if (PEEK_TOKEN() != token
)
283 /* return 0 if expected token was not found,
287 expect (token
, message
)
288 enum ch_terminal token
;
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");
308 parse_opt_name_string (allow_all
)
309 int allow_all
; /* 1 if ALL is allowed as a postfix */
311 int token
= PEEK_TOKEN();
315 if (token
== ALL
&& allow_all
)
326 token
= PEEK_TOKEN();
330 token
= PEEK_TOKEN();
331 if (token
== ALL
&& allow_all
)
332 return get_identifier3(IDENTIFIER_POINTER (name
), "!", "*");
336 error ("'%s!' is not followed by an identifier",
337 IDENTIFIER_POINTER (name
));
340 name
= get_identifier3(IDENTIFIER_POINTER(name
),
341 "!", IDENTIFIER_POINTER(PEEK_LVAL()));
346 parse_simple_name_string ()
348 int token
= PEEK_TOKEN();
352 error ("expected a name here");
353 return error_mark_node
;
363 tree name
= parse_opt_name_string (0);
367 error ("expected a name string here");
368 return error_mark_node
;
371 /* Matches: <name_string>
372 Returns if pass 1: the identifier.
373 Returns if pass 2: a decl or value for identifier. */
378 tree name
= parse_name_string ();
379 if (pass
== 1 || ignoring
)
383 tree decl
= lookup_name (name
);
384 if (decl
== NULL_TREE
)
386 error ("`%s' undeclared", IDENTIFIER_POINTER (name
));
387 return error_mark_node
;
389 else if (TREE_CODE (TREE_TYPE (decl
)) == ERROR_MARK
)
390 return error_mark_node
;
391 else if (TREE_CODE (decl
) == CONST_DECL
)
392 return DECL_INITIAL (decl
);
393 else if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
394 return convert_from_reference (decl
);
403 pushback_paren_expr (expr
)
406 if (pass
== 1 && !ignoring
)
407 expr
= build1 (PAREN_EXPR
, NULL_TREE
, expr
);
408 pushback_token (EXPR
, expr
);
412 /* Matches: <case label> */
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 ()
431 switch (PEEK_TOKEN ())
438 parse_untyped_expr ();
452 /* Parse NAME '(' MODENAME ')'. */
462 if (PEEK_TOKEN () != TYPENAME
)
463 error ("expect MODENAME here `%s'", lexptr
);
464 type
= PEEK_LVAL().tsym
.type
;
473 parse_mode_or_normal_call ()
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 ()
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 (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 (type
)
625 if (PEEK_TOKEN () == ']')
629 parse_tuple_element (type
);
631 if (PEEK_TOKEN () == ']')
633 if (!check_token (','))
634 error ("bad syntax in tuple");
638 /* Parses: '[' elements ']'
639 If modename is non-NULL it prefixed the tuple. */
647 type
= check_typedef (mode
);
652 parse_opt_element_list (type
);
653 expect (']', "missing ']' after tuple");
654 write_exp_elt_opcode (OP_ARRAY
);
655 write_exp_elt_longcst ((LONGEST
) 0);
656 write_exp_elt_longcst ((LONGEST
) end_arglist () - 1);
657 write_exp_elt_opcode (OP_ARRAY
);
660 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
661 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
662 && TYPE_CODE (type
) != TYPE_CODE_SET
)
663 error ("invalid tuple mode");
664 write_exp_elt_opcode (UNOP_CAST
);
665 write_exp_elt_type (mode
);
666 write_exp_elt_opcode (UNOP_CAST
);
676 switch (PEEK_TOKEN ())
678 case INTEGER_LITERAL
:
679 case CHARACTER_LITERAL
:
680 write_exp_elt_opcode (OP_LONG
);
681 write_exp_elt_type (PEEK_LVAL ().typed_val
.type
);
682 write_exp_elt_longcst (PEEK_LVAL ().typed_val
.val
);
683 write_exp_elt_opcode (OP_LONG
);
686 case BOOLEAN_LITERAL
:
687 write_exp_elt_opcode (OP_BOOL
);
688 write_exp_elt_longcst ((LONGEST
) PEEK_LVAL ().ulval
);
689 write_exp_elt_opcode (OP_BOOL
);
693 write_exp_elt_opcode (OP_DOUBLE
);
694 write_exp_elt_type (builtin_type_double
);
695 write_exp_elt_dblcst (PEEK_LVAL ().dval
);
696 write_exp_elt_opcode (OP_DOUBLE
);
699 case EMPTINESS_LITERAL
:
700 write_exp_elt_opcode (OP_LONG
);
701 write_exp_elt_type (lookup_pointer_type (builtin_type_void
));
702 write_exp_elt_longcst (0);
703 write_exp_elt_opcode (OP_LONG
);
706 case CHARACTER_STRING_LITERAL
:
707 write_exp_elt_opcode (OP_STRING
);
708 write_exp_string (PEEK_LVAL ().sval
);
709 write_exp_elt_opcode (OP_STRING
);
712 case BIT_STRING_LITERAL
:
713 write_exp_elt_opcode (OP_BITSTRING
);
714 write_exp_bitstring (PEEK_LVAL ().sval
);
715 write_exp_elt_opcode (OP_BITSTRING
);
720 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
721 which casts to an artificial array. */
724 if (PEEK_TOKEN () != TYPENAME
)
725 error ("missing MODENAME after ARRAY()");
726 type
= PEEK_LVAL().tsym
.type
;
730 expect (')', "missing right parenthesis");
731 type
= create_array_type ((struct type
*) NULL
, type
,
732 create_range_type ((struct type
*) NULL
,
733 builtin_type_int
, 0, 0));
734 TYPE_ARRAY_UPPER_BOUND_TYPE(type
) = BOUND_CANNOT_BE_DETERMINED
;
735 write_exp_elt_opcode (UNOP_CAST
);
736 write_exp_elt_type (type
);
737 write_exp_elt_opcode (UNOP_CAST
);
749 expect (')', "missing right parenthesis");
754 case GENERAL_PROCEDURE_NAME
:
756 write_exp_elt_opcode (OP_VAR_VALUE
);
757 write_exp_elt_block (NULL
);
758 write_exp_elt_sym (PEEK_LVAL ().ssym
.sym
);
759 write_exp_elt_opcode (OP_VAR_VALUE
);
762 case GDB_VARIABLE
: /* gdb specific */
767 write_exp_elt_opcode (UNOP_CAST
);
768 write_exp_elt_type (builtin_type_int
);
769 write_exp_elt_opcode (UNOP_CAST
);
773 write_exp_elt_opcode (UNOP_CARD
);
777 write_exp_elt_opcode (UNOP_CHMAX
);
781 write_exp_elt_opcode (UNOP_CHMIN
);
783 case PRED
: op_name
= "PRED"; goto unimplemented_unary_builtin
;
784 case SUCC
: op_name
= "SUCC"; goto unimplemented_unary_builtin
;
785 case ABS
: op_name
= "ABS"; 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 ();
797 { write_exp_elt_opcode (OP_LONG
);
798 write_exp_elt_type (builtin_type_int
);
799 CHECK_TYPEDEF (type
);
800 write_exp_elt_longcst ((LONGEST
) TYPE_LENGTH (type
));
801 write_exp_elt_opcode (OP_LONG
);
804 write_exp_elt_opcode (UNOP_SIZEOF
);
813 type
= parse_mode_or_normal_call ();
814 write_lower_upper_value (op
, type
);
818 write_exp_elt_opcode (UNOP_LENGTH
);
821 type
= PEEK_LVAL ().tsym
.type
;
823 switch (PEEK_TOKEN())
831 expect (')', "missing right parenthesis");
832 write_exp_elt_opcode (UNOP_CAST
);
833 write_exp_elt_type (type
);
834 write_exp_elt_opcode (UNOP_CAST
);
837 error ("typename in invalid context");
842 error ("invalid expression syntax at `%s'", lexptr
);
846 switch (PEEK_TOKEN ())
849 write_exp_elt_opcode (STRUCTOP_STRUCT
);
850 write_exp_string (PEEK_LVAL ().sval
);
851 write_exp_elt_opcode (STRUCTOP_STRUCT
);
856 if (PEEK_TOKEN () == TYPENAME
)
858 type
= PEEK_LVAL ().tsym
.type
;
859 write_exp_elt_opcode (UNOP_CAST
);
860 write_exp_elt_type (lookup_pointer_type (type
));
861 write_exp_elt_opcode (UNOP_CAST
);
864 write_exp_elt_opcode (UNOP_IND
);
869 case CHARACTER_STRING_LITERAL
:
870 case CHARACTER_LITERAL
:
871 case BIT_STRING_LITERAL
:
872 /* Handle string repetition. (See comment in parse_operand5.) */
874 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
875 write_exp_elt_longcst (1);
876 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
880 case INTEGER_LITERAL
:
881 case BOOLEAN_LITERAL
:
883 case GENERAL_PROCEDURE_NAME
:
885 case EMPTINESS_LITERAL
:
936 if (check_token (RECEIVE
))
939 error ("not implemented: RECEIVE expression");
941 else if (check_token (POINTER
))
944 write_exp_elt_opcode (UNOP_ADDR
);
954 /* We are supposed to be looking for a <string repetition operator>,
955 but in general we can't distinguish that from a parenthesized
956 expression. This is especially difficult if we allow the
957 string operand to be a constant expression (as requested by
958 some users), and not just a string literal.
959 Consider: LPRN expr RPRN LPRN expr RPRN
960 Is that a function call or string repetition?
961 Instead, we handle string repetition in parse_primval,
962 and build_generalized_call. */
963 switch (PEEK_TOKEN())
965 case NOT
: op
= UNOP_LOGICAL_NOT
; break;
966 case '-': op
= UNOP_NEG
; break;
974 write_exp_elt_opcode (op
);
984 switch (PEEK_TOKEN())
986 case '*': op
= BINOP_MUL
; break;
987 case '/': op
= BINOP_DIV
; break;
988 case MOD
: op
= BINOP_MOD
; break;
989 case REM
: op
= BINOP_REM
; break;
995 write_exp_elt_opcode (op
);
1006 switch (PEEK_TOKEN())
1008 case '+': op
= BINOP_ADD
; break;
1009 case '-': op
= BINOP_SUB
; break;
1010 case SLASH_SLASH
: op
= BINOP_CONCAT
; break;
1016 write_exp_elt_opcode (op
);
1027 if (check_token (IN
))
1030 write_exp_elt_opcode (BINOP_IN
);
1034 switch (PEEK_TOKEN())
1036 case '>': op
= BINOP_GTR
; break;
1037 case GEQ
: op
= BINOP_GEQ
; break;
1038 case '<': op
= BINOP_LESS
; break;
1039 case LEQ
: op
= BINOP_LEQ
; break;
1040 case '=': op
= BINOP_EQUAL
; break;
1041 case NOTEQUAL
: op
= BINOP_NOTEQUAL
; break;
1047 write_exp_elt_opcode (op
);
1059 switch (PEEK_TOKEN())
1061 case LOGAND
: op
= BINOP_BITWISE_AND
; break;
1062 case ANDIF
: op
= BINOP_LOGICAL_AND
; break;
1068 write_exp_elt_opcode (op
);
1079 switch (PEEK_TOKEN())
1081 case LOGIOR
: op
= BINOP_BITWISE_IOR
; break;
1082 case LOGXOR
: op
= BINOP_BITWISE_XOR
; break;
1083 case ORIF
: op
= BINOP_LOGICAL_OR
; break;
1089 write_exp_elt_opcode (op
);
1097 if (check_token (GDB_ASSIGNMENT
))
1100 write_exp_elt_opcode (BINOP_ASSIGN
);
1105 parse_then_alternative ()
1107 expect (THEN
, "missing 'THEN' in 'IF' expression");
1112 parse_else_alternative ()
1114 if (check_token (ELSIF
))
1115 parse_if_expression_body ();
1116 else if (check_token (ELSE
))
1119 error ("missing ELSE/ELSIF in IF expression");
1122 /* Matches: <boolean expression> <then alternative> <else alternative> */
1125 parse_if_expression_body ()
1128 parse_then_alternative ();
1129 parse_else_alternative ();
1130 write_exp_elt_opcode (TERNOP_COND
);
1134 parse_if_expression ()
1137 parse_if_expression_body ();
1138 expect (FI
, "missing 'FI' at end of conditional expression");
1141 /* An <untyped_expr> is a superset of <expr>. It also includes
1142 <conditional expressions> and untyped <tuples>, whose types
1143 are not given by their constituents. Hence, these are only
1144 allowed in certain contexts that expect a certain type.
1145 You should call convert() to fix up the <untyped_expr>. */
1148 parse_untyped_expr ()
1150 switch (PEEK_TOKEN())
1153 parse_if_expression ();
1156 error ("not implemented: CASE expression");
1158 switch (PEEK_TOKEN1())
1166 parse_untyped_expr ();
1167 expect (')', "missing ')'");
1180 terminal_buffer
[0] = TOKEN_NOT_READ
;
1181 if (PEEK_TOKEN () == TYPENAME
&& PEEK_TOKEN1 () == END_TOKEN
)
1183 write_exp_elt_opcode(OP_TYPE
);
1184 write_exp_elt_type(PEEK_LVAL ().tsym
.type
);
1185 write_exp_elt_opcode(OP_TYPE
);
1190 if (terminal_buffer
[0] != END_TOKEN
)
1192 if (comma_terminates
&& terminal_buffer
[0] == ',')
1193 lexptr
--; /* Put the comma back. */
1195 error ("Junk after end of expression.");
1201 /* Implementation of a dynamically expandable buffer for processing input
1202 characters acquired through lexptr and building a value to return in
1205 static char *tempbuf
; /* Current buffer contents */
1206 static int tempbufsize
; /* Size of allocated buffer */
1207 static int tempbufindex
; /* Current index into buffer */
1209 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1211 #define CHECKBUF(size) \
1213 if (tempbufindex + (size) >= tempbufsize) \
1215 growbuf_by_size (size); \
1219 /* Grow the static temp buffer if necessary, including allocating the first one
1223 growbuf_by_size (count
)
1228 growby
= max (count
, GROWBY_MIN_SIZE
);
1229 tempbufsize
+= growby
;
1230 if (tempbuf
== NULL
)
1232 tempbuf
= (char *) xmalloc (tempbufsize
);
1236 tempbuf
= (char *) xrealloc (tempbuf
, tempbufsize
);
1240 /* Try to consume a simple name string token. If successful, returns
1241 a pointer to a nullbyte terminated copy of the name that can be used
1242 in symbol table lookups. If not successful, returns NULL. */
1245 match_simple_name_string ()
1247 char *tokptr
= lexptr
;
1249 if (isalpha (*tokptr
) || *tokptr
== '_')
1254 } while (isalnum (*tokptr
) || (*tokptr
== '_'));
1255 yylval
.sval
.ptr
= lexptr
;
1256 yylval
.sval
.length
= tokptr
- lexptr
;
1258 result
= copy_name (yylval
.sval
);
1264 /* Start looking for a value composed of valid digits as set by the base
1265 in use. Note that '_' characters are valid anywhere, in any quantity,
1266 and are simply ignored. Since we must find at least one valid digit,
1267 or reject this token as an integer literal, we keep track of how many
1268 digits we have encountered. */
1271 decode_integer_value (base
, tokptrptr
, ivalptr
)
1276 char *tokptr
= *tokptrptr
;
1280 while (*tokptr
!= '\0')
1284 temp
= tolower (temp
);
1290 case '0': case '1': case '2': case '3': case '4':
1291 case '5': case '6': case '7': case '8': case '9':
1294 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1310 /* Found something not in domain for current base. */
1311 tokptr
--; /* Unconsume what gave us indigestion. */
1316 /* If we didn't find any digits, then we don't have a valid integer
1317 value, so reject the entire token. Otherwise, update the lexical
1318 scan pointer, and return non-zero for success. */
1326 *tokptrptr
= tokptr
;
1332 decode_integer_literal (valptr
, tokptrptr
)
1336 char *tokptr
= *tokptrptr
;
1339 int explicit_base
= 0;
1341 /* Look for an explicit base specifier, which is optional. */
1374 /* If we found an explicit base ensure that the character after the
1375 explicit base is a single quote. */
1377 if (explicit_base
&& (*tokptr
++ != '\''))
1382 /* Attempt to decode whatever follows as an integer value in the
1383 indicated base, updating the token pointer in the process and
1384 computing the value into ival. Also, if we have an explicit
1385 base, then the next character must not be a single quote, or we
1386 have a bitstring literal, so reject the entire token in this case.
1387 Otherwise, update the lexical scan pointer, and return non-zero
1390 if (!decode_integer_value (base
, &tokptr
, &ival
))
1394 else if (explicit_base
&& (*tokptr
== '\''))
1401 *tokptrptr
= tokptr
;
1406 /* If it wasn't for the fact that floating point values can contain '_'
1407 characters, we could just let strtod do all the hard work by letting it
1408 try to consume as much of the current token buffer as possible and
1409 find a legal conversion. Unfortunately we need to filter out the '_'
1410 characters before calling strtod, which we do by copying the other
1411 legal chars to a local buffer to be converted. However since we also
1412 need to keep track of where the last unconsumed character in the input
1413 buffer is, we have transfer only as many characters as may compose a
1414 legal floating point value. */
1416 static enum ch_terminal
1417 match_float_literal ()
1419 char *tokptr
= lexptr
;
1423 extern double strtod ();
1425 /* Make local buffer in which to build the string to convert. This is
1426 required because underscores are valid in chill floating point numbers
1427 but not in the string passed to strtod to convert. The string will be
1428 no longer than our input string. */
1430 copy
= buf
= (char *) alloca (strlen (tokptr
) + 1);
1432 /* Transfer all leading digits to the conversion buffer, discarding any
1435 while (isdigit (*tokptr
) || *tokptr
== '_')
1444 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1445 of whether we found any leading digits, and we simply accept it and
1446 continue on to look for the fractional part and/or exponent. One of
1447 [eEdD] is legal only if we have seen digits, and means that there
1448 is no fractional part. If we find neither of these, then this is
1449 not a floating point number, so return failure. */
1454 /* Accept and then look for fractional part and/or exponent. */
1467 goto collect_exponent
;
1475 /* We found a '.', copy any fractional digits to the conversion buffer, up
1476 to the first nondigit, non-underscore character. */
1478 while (isdigit (*tokptr
) || *tokptr
== '_')
1487 /* Look for an exponent, which must start with one of [eEdD]. If none
1488 is found, jump directly to trying to convert what we have collected
1505 /* Accept an optional '-' or '+' following one of [eEdD]. */
1508 if (*tokptr
== '+' || *tokptr
== '-')
1510 *copy
++ = *tokptr
++;
1513 /* Now copy an exponent into the conversion buffer. Note that at the
1514 moment underscores are *not* allowed in exponents. */
1516 while (isdigit (*tokptr
))
1518 *copy
++ = *tokptr
++;
1521 /* If we transfered any chars to the conversion buffer, try to interpret its
1522 contents as a floating point value. If any characters remain, then we
1523 must not have a valid floating point string. */
1529 dval
= strtod (buf
, ©
);
1534 return (FLOAT_LITERAL
);
1540 /* Recognize a string literal. A string literal is a sequence
1541 of characters enclosed in matching single or double quotes, except that
1542 a single character inside single quotes is a character literal, which
1543 we reject as a string literal. To embed the terminator character inside
1544 a string, it is simply doubled (I.E. "this""is""one""string") */
1546 static enum ch_terminal
1547 match_string_literal ()
1549 char *tokptr
= lexptr
;
1553 for (tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1559 /* skip possible whitespaces */
1560 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1568 else if (*tokptr
!= ',')
1569 error ("Invalid control sequence");
1571 /* skip possible whitespaces */
1572 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1574 if (!decode_integer_literal (&ival
, &tokptr
))
1575 error ("Invalid control sequence");
1578 else if (*tokptr
== *lexptr
)
1580 if (*(tokptr
+ 1) == *lexptr
)
1589 else if (*tokptr
== '^')
1591 if (*(tokptr
+ 1) == '(')
1595 if (!decode_integer_literal (&ival
, &tokptr
))
1596 error ("Invalid control sequence");
1599 else if (*(tokptr
+ 1) == '^')
1602 error ("Invalid control sequence");
1606 tempbuf
[tempbufindex
++] = ival
;
1609 error ("Invalid control sequence");
1611 if (*tokptr
== '\0' /* no terminator */
1612 || (tempbufindex
== 1 && *tokptr
== '\'')) /* char literal */
1618 tempbuf
[tempbufindex
] = '\0';
1619 yylval
.sval
.ptr
= tempbuf
;
1620 yylval
.sval
.length
= tempbufindex
;
1622 return (CHARACTER_STRING_LITERAL
);
1626 /* Recognize a character literal. A character literal is single character
1627 or a control sequence, enclosed in single quotes. A control sequence
1628 is a comma separated list of one or more integer literals, enclosed
1629 in parenthesis and introduced with a circumflex character.
1631 EX: 'a' '^(7)' '^(7,8)'
1633 As a GNU chill extension, the syntax C'xx' is also recognized as a
1634 character literal, where xx is a hex value for the character.
1636 Note that more than a single character, enclosed in single quotes, is
1639 Returns CHARACTER_LITERAL if a match is found.
1642 static enum ch_terminal
1643 match_character_literal ()
1645 char *tokptr
= lexptr
;
1648 if ((*tokptr
== 'c' || *tokptr
== 'C') && (*(tokptr
+ 1) == '\''))
1650 /* We have a GNU chill extension form, so skip the leading "C'",
1651 decode the hex value, and then ensure that we have a trailing
1652 single quote character. */
1654 if (!decode_integer_value (16, &tokptr
, &ival
) || (*tokptr
!= '\''))
1660 else if (*tokptr
== '\'')
1664 /* Determine which form we have, either a control sequence or the
1665 single character form. */
1669 if (*(tokptr
+ 1) == '(')
1671 /* Match and decode a control sequence. Return zero if we don't
1672 find a valid integer literal, or if the next unconsumed character
1673 after the integer literal is not the trailing ')'. */
1675 if (!decode_integer_literal (&ival
, &tokptr
) || (*tokptr
++ != ')'))
1680 else if (*(tokptr
+ 1) == '^')
1687 error ("Invalid control sequence");
1689 else if (*tokptr
== '\'')
1691 /* this must be duplicated */
1700 /* The trailing quote has not yet been consumed. If we don't find
1701 it, then we have no match. */
1703 if (*tokptr
++ != '\'')
1710 /* Not a character literal. */
1713 yylval
.typed_val
.val
= ival
;
1714 yylval
.typed_val
.type
= builtin_type_chill_char
;
1716 return (CHARACTER_LITERAL
);
1719 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1720 Note that according to 5.2.4.2, a single "_" is also a valid integer
1721 literal, however GNU-chill requires there to be at least one "digit"
1722 in any integer literal. */
1724 static enum ch_terminal
1725 match_integer_literal ()
1727 char *tokptr
= lexptr
;
1730 if (!decode_integer_literal (&ival
, &tokptr
))
1736 yylval
.typed_val
.val
= ival
;
1737 #if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
1738 if (ival
> (LONGEST
)2147483647U || ival
< -(LONGEST
)2147483648U)
1739 yylval
.typed_val
.type
= builtin_type_long_long
;
1742 yylval
.typed_val
.type
= builtin_type_int
;
1744 return (INTEGER_LITERAL
);
1748 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1749 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1750 literal, however GNU-chill requires there to be at least one "digit"
1751 in any bit-string literal. */
1753 static enum ch_terminal
1754 match_bitstring_literal ()
1756 register char *tokptr
= lexptr
;
1766 /* Look for the required explicit base specifier. */
1787 /* Ensure that the character after the explicit base is a single quote. */
1789 if (*tokptr
++ != '\'')
1794 while (*tokptr
!= '\0' && *tokptr
!= '\'')
1797 if (isupper (digit
))
1798 digit
= tolower (digit
);
1804 case '0': case '1': case '2': case '3': case '4':
1805 case '5': case '6': case '7': case '8': case '9':
1808 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1813 /* this is not a bitstring literal, probably an integer */
1816 if (digit
>= 1 << bits_per_char
)
1818 /* Found something not in domain for current base. */
1819 error ("Too-large digit in bitstring or integer.");
1823 /* Extract bits from digit, packing them into the bitstring byte. */
1824 int k
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? bits_per_char
- 1 : 0;
1825 for (; TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
>= 0 : k
< bits_per_char
;
1826 TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
-- : k
++)
1829 if (digit
& (1 << k
))
1831 tempbuf
[tempbufindex
] |=
1832 (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1833 ? (1 << (HOST_CHAR_BIT
- 1 - bitoffset
))
1837 if (bitoffset
== HOST_CHAR_BIT
)
1842 tempbuf
[tempbufindex
] = 0;
1848 /* Verify that we consumed everything up to the trailing single quote,
1849 and that we found some bits (IE not just underbars). */
1851 if (*tokptr
++ != '\'')
1857 yylval
.sval
.ptr
= tempbuf
;
1858 yylval
.sval
.length
= bitcount
;
1860 return (BIT_STRING_LITERAL
);
1870 static const struct token idtokentab
[] =
1873 { "length", LENGTH
},
1884 { "max", MAX_TOKEN
},
1885 { "min", MIN_TOKEN
},
1894 { "addr", ADDR_TOKEN
},
1895 { "null", EMPTINESS_LITERAL
}
1898 static const struct token tokentab2
[] =
1900 { ":=", GDB_ASSIGNMENT
},
1901 { "//", SLASH_SLASH
},
1908 /* Read one token, getting characters through lexptr. */
1909 /* This is where we will check to make sure that the language and the
1910 operators used are compatible. */
1912 static enum ch_terminal
1916 enum ch_terminal token
;
1920 /* Skip over any leading whitespace. */
1921 while (isspace (*lexptr
))
1925 /* Look for special single character cases which can't be the first
1926 character of some other multicharacter token. */
1943 /* Look for characters which start a particular kind of multicharacter
1944 token, such as a character literal, register name, convenience
1945 variable name, string literal, etc. */
1950 /* First try to match a string literal, which is any
1951 sequence of characters enclosed in matching single or double
1952 quotes, except that a single character inside single quotes
1953 is a character literal, so we have to catch that case also. */
1954 token
= match_string_literal ();
1959 if (*lexptr
== '\'')
1961 token
= match_character_literal ();
1970 token
= match_character_literal ();
1977 yylval
.sval
.ptr
= lexptr
;
1980 } while (isalnum (*lexptr
) || *lexptr
== '_' || *lexptr
== '$');
1981 yylval
.sval
.length
= lexptr
- yylval
.sval
.ptr
;
1982 write_dollar_variable (yylval
.sval
);
1983 return GDB_VARIABLE
;
1986 /* See if it is a special token of length 2. */
1987 for (i
= 0; i
< sizeof (tokentab2
) / sizeof (tokentab2
[0]); i
++)
1989 if (STREQN (lexptr
, tokentab2
[i
].operator, 2))
1992 return (tokentab2
[i
].token
);
1995 /* Look for single character cases which which could be the first
1996 character of some other multicharacter token, but aren't, or we
1997 would already have found it. */
2007 /* Look for a float literal before looking for an integer literal, so
2008 we match as much of the input stream as possible. */
2009 token
= match_float_literal ();
2014 token
= match_bitstring_literal ();
2019 token
= match_integer_literal ();
2025 /* Try to match a simple name string, and if a match is found, then
2026 further classify what sort of name it is and return an appropriate
2027 token. Note that attempting to match a simple name string consumes
2028 the token from lexptr, so we can't back out if we later find that
2029 we can't classify what sort of name it is. */
2031 inputname
= match_simple_name_string ();
2033 if (inputname
!= NULL
)
2035 char *simplename
= (char*) alloca (strlen (inputname
) + 1);
2037 char *dptr
= simplename
, *sptr
= inputname
;
2038 for (; *sptr
; sptr
++)
2039 *dptr
++ = isupper (*sptr
) ? tolower(*sptr
) : *sptr
;
2042 /* See if it is a reserved identifier. */
2043 for (i
= 0; i
< sizeof (idtokentab
) / sizeof (idtokentab
[0]); i
++)
2045 if (STREQ (simplename
, idtokentab
[i
].operator))
2047 return (idtokentab
[i
].token
);
2051 /* Look for other special tokens. */
2052 if (STREQ (simplename
, "true"))
2055 return (BOOLEAN_LITERAL
);
2057 if (STREQ (simplename
, "false"))
2060 return (BOOLEAN_LITERAL
);
2063 sym
= lookup_symbol (inputname
, expression_context_block
,
2064 VAR_NAMESPACE
, (int *) NULL
,
2065 (struct symtab
**) NULL
);
2066 if (sym
== NULL
&& strcmp (inputname
, simplename
) != 0)
2068 sym
= lookup_symbol (simplename
, expression_context_block
,
2069 VAR_NAMESPACE
, (int *) NULL
,
2070 (struct symtab
**) NULL
);
2074 yylval
.ssym
.stoken
.ptr
= NULL
;
2075 yylval
.ssym
.stoken
.length
= 0;
2076 yylval
.ssym
.sym
= sym
;
2077 yylval
.ssym
.is_a_field_of_this
= 0; /* FIXME, C++'ism */
2078 switch (SYMBOL_CLASS (sym
))
2081 /* Found a procedure name. */
2082 return (GENERAL_PROCEDURE_NAME
);
2084 /* Found a global or local static variable. */
2085 return (LOCATION_NAME
);
2090 case LOC_REGPARM_ADDR
:
2094 case LOC_BASEREG_ARG
:
2095 if (innermost_block
== NULL
2096 || contained_in (block_found
, innermost_block
))
2098 innermost_block
= block_found
;
2100 return (LOCATION_NAME
);
2104 return (LOCATION_NAME
);
2107 yylval
.tsym
.type
= SYMBOL_TYPE (sym
);
2110 case LOC_CONST_BYTES
:
2111 case LOC_OPTIMIZED_OUT
:
2112 error ("Symbol \"%s\" names no location.", inputname
);
2114 case LOC_UNRESOLVED
:
2115 error ("unhandled SYMBOL_CLASS in ch_lex()");
2119 else if (!have_full_symbols () && !have_partial_symbols ())
2121 error ("No symbol table is loaded. Use the \"file\" command.");
2125 error ("No symbol \"%s\" in current context.", inputname
);
2129 /* Catch single character tokens which are not part of some
2134 case '.': /* Not float for example. */
2136 while (isspace (*lexptr
)) lexptr
++;
2137 inputname
= match_simple_name_string ();
2140 return DOT_FIELD_NAME
;
2143 return (ILLEGAL_TOKEN
);
2147 write_lower_upper_value (opcode
, type
)
2148 enum exp_opcode opcode
; /* Either UNOP_LOWER or UNOP_UPPER */
2152 write_exp_elt_opcode (opcode
);
2155 struct type
*result_type
;
2156 LONGEST val
= type_lower_upper (opcode
, type
, &result_type
);
2157 write_exp_elt_opcode (OP_LONG
);
2158 write_exp_elt_type (result_type
);
2159 write_exp_elt_longcst (val
);
2160 write_exp_elt_opcode (OP_LONG
);