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 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_case_label (void);
156 static void parse_untyped_expr (void);
157 static void parse_if_expression (void);
158 static void parse_if_expression_body (void);
159 static void parse_else_alternative (void);
160 static void parse_then_alternative (void);
161 static void parse_expr (void);
162 static void parse_operand0 (void);
163 static void parse_operand1 (void);
164 static void parse_operand2 (void);
165 static void parse_operand3 (void);
166 static void parse_operand4 (void);
167 static void parse_operand5 (void);
168 static void parse_operand6 (void);
169 static void parse_primval (void);
170 static void parse_tuple (struct type *);
171 static void parse_opt_element_list (struct type *);
172 static void parse_tuple_element (struct type *);
173 static void parse_named_record_element (void);
174 static void parse_call (void);
175 static struct type *parse_mode_or_normal_call (void);
177 static struct type *parse_mode_call (void);
179 static void parse_unary_call (void);
180 static int parse_opt_untyped_expr (void);
181 static int expect (enum ch_terminal, char *);
182 static enum ch_terminal ch_lex (void);
183 INLINE static enum ch_terminal PEEK_TOKEN (void);
184 static enum ch_terminal peek_token_ (int);
185 static void forward_token_ (void);
186 static void require (enum ch_terminal);
187 static int check_token (enum ch_terminal);
189 #define MAX_LOOK_AHEAD 2
190 static enum ch_terminal terminal_buffer[MAX_LOOK_AHEAD + 1] =
192 TOKEN_NOT_READ, TOKEN_NOT_READ, TOKEN_NOT_READ};
193 static YYSTYPE yylval;
194 static YYSTYPE val_buffer[MAX_LOOK_AHEAD + 1];
196 /*int current_token, lookahead_token; */
198 INLINE static enum ch_terminal
201 if (terminal_buffer[0] == TOKEN_NOT_READ)
203 terminal_buffer[0] = ch_lex ();
204 val_buffer[0] = yylval;
206 return terminal_buffer[0];
208 #define PEEK_LVAL() val_buffer[0]
209 #define PEEK_TOKEN1() peek_token_(1)
210 #define PEEK_TOKEN2() peek_token_(2)
211 static enum ch_terminal
214 if (i > MAX_LOOK_AHEAD)
215 internal_error (__FILE__, __LINE__,
216 "too much lookahead");
217 if (terminal_buffer[i] == TOKEN_NOT_READ)
219 terminal_buffer[i] = ch_lex ();
220 val_buffer[i] = yylval;
222 return terminal_buffer[i];
228 pushback_token (enum ch_terminal code, YYSTYPE node)
231 if (terminal_buffer[MAX_LOOK_AHEAD] != TOKEN_NOT_READ)
232 internal_error (__FILE__, __LINE__,
233 "cannot pushback token");
234 for (i = MAX_LOOK_AHEAD; i > 0; i--)
236 terminal_buffer[i] = terminal_buffer[i - 1];
237 val_buffer[i] = val_buffer[i - 1];
239 terminal_buffer[0] = code;
240 val_buffer[0] = node;
246 forward_token_ (void)
249 for (i = 0; i < MAX_LOOK_AHEAD; i++)
251 terminal_buffer[i] = terminal_buffer[i + 1];
252 val_buffer[i] = val_buffer[i + 1];
254 terminal_buffer[MAX_LOOK_AHEAD] = TOKEN_NOT_READ;
256 #define FORWARD_TOKEN() forward_token_()
258 /* Skip the next token.
259 if it isn't TOKEN, the parser is broken. */
262 require (enum ch_terminal token)
264 if (PEEK_TOKEN () != token)
266 internal_error (__FILE__, __LINE__,
267 "expected token %d", (int) token);
273 check_token (enum ch_terminal token)
275 if (PEEK_TOKEN () != token)
281 /* return 0 if expected token was not found,
285 expect (enum ch_terminal token, char *message)
287 if (PEEK_TOKEN () != token)
291 else if (token < 256)
292 error ("syntax error - expected a '%c' here \"%s\"", token, lexptr);
294 error ("syntax error");
303 /* Parse a name string. If ALLOW_ALL is 1, ALL is allowed as a postfix. */
306 parse_opt_name_string (int allow_all)
308 int token = PEEK_TOKEN ();
312 if (token == ALL && allow_all)
323 token = PEEK_TOKEN ();
327 token = PEEK_TOKEN ();
328 if (token == ALL && allow_all)
329 return get_identifier3 (IDENTIFIER_POINTER (name), "!", "*");
333 error ("'%s!' is not followed by an identifier",
334 IDENTIFIER_POINTER (name));
337 name = get_identifier3 (IDENTIFIER_POINTER (name),
338 "!", IDENTIFIER_POINTER (PEEK_LVAL ()));
343 parse_simple_name_string (void)
345 int token = PEEK_TOKEN ();
349 error ("expected a name here");
350 return error_mark_node;
358 parse_name_string (void)
360 tree name = parse_opt_name_string (0);
364 error ("expected a name string here");
365 return error_mark_node;
368 /* Matches: <name_string>
369 Returns if pass 1: the identifier.
370 Returns if pass 2: a decl or value for identifier. */
375 tree name = parse_name_string ();
376 if (pass == 1 || ignoring)
380 tree decl = lookup_name (name);
381 if (decl == NULL_TREE)
383 error ("`%s' undeclared", IDENTIFIER_POINTER (name));
384 return error_mark_node;
386 else if (TREE_CODE (TREE_TYPE (decl)) == ERROR_MARK)
387 return error_mark_node;
388 else if (TREE_CODE (decl) == CONST_DECL)
389 return DECL_INITIAL (decl);
390 else if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
391 return convert_from_reference (decl);
400 pushback_paren_expr (tree expr)
402 if (pass == 1 && !ignoring)
403 expr = build1 (PAREN_EXPR, NULL_TREE, expr);
404 pushback_token (EXPR, expr);
408 /* Matches: <case label> */
411 parse_case_label (void)
413 if (check_token (ELSE))
414 error ("ELSE in tuples labels not implemented");
415 /* Does not handle the case of a mode name. FIXME */
417 if (check_token (':'))
420 write_exp_elt_opcode (BINOP_RANGE);
425 parse_opt_untyped_expr (void)
427 switch (PEEK_TOKEN ())
434 parse_untyped_expr ();
440 parse_unary_call (void)
448 /* Parse NAME '(' MODENAME ')'. */
453 parse_mode_call (void)
458 if (PEEK_TOKEN () != TYPENAME)
459 error ("expect MODENAME here `%s'", lexptr);
460 type = PEEK_LVAL ().tsym.type;
469 parse_mode_or_normal_call (void)
474 if (PEEK_TOKEN () == TYPENAME)
476 type = PEEK_LVAL ().tsym.type;
488 /* Parse something that looks like a function call.
489 Assume we have parsed the function, and are at the '('. */
496 /* This is to save the value of arglist_len
497 being accumulated for each dimension. */
499 if (parse_opt_untyped_expr ())
501 int tok = PEEK_TOKEN ();
503 if (tok == UP || tok == ':')
507 expect (')', "expected ')' to terminate slice");
509 write_exp_elt_opcode (tok == UP ? TERNOP_SLICE_COUNT
513 while (check_token (','))
515 parse_untyped_expr ();
522 arg_count = end_arglist ();
523 write_exp_elt_opcode (MULTI_SUBSCRIPT);
524 write_exp_elt_longcst (arg_count);
525 write_exp_elt_opcode (MULTI_SUBSCRIPT);
529 parse_named_record_element (void)
534 label = PEEK_LVAL ().sval;
535 sprintf (buf, "expected a field name here `%s'", lexptr);
536 expect (DOT_FIELD_NAME, buf);
537 if (check_token (','))
538 parse_named_record_element ();
539 else if (check_token (':'))
542 error ("syntax error near `%s' in named record tuple element", lexptr);
543 write_exp_elt_opcode (OP_LABELED);
544 write_exp_string (label);
545 write_exp_elt_opcode (OP_LABELED);
548 /* Returns one or more TREE_LIST nodes, in reverse order. */
551 parse_tuple_element (struct type *type)
553 if (PEEK_TOKEN () == DOT_FIELD_NAME)
555 /* Parse a labelled structure tuple. */
556 parse_named_record_element ();
560 if (check_token ('('))
562 if (check_token ('*'))
564 expect (')', "missing ')' after '*' case label list");
567 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
569 /* do this as a range from low to high */
570 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
571 LONGEST low_bound, high_bound;
572 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
573 error ("cannot determine bounds for (*)");
575 write_exp_elt_opcode (OP_LONG);
576 write_exp_elt_type (range_type);
577 write_exp_elt_longcst (low_bound);
578 write_exp_elt_opcode (OP_LONG);
580 write_exp_elt_opcode (OP_LONG);
581 write_exp_elt_type (range_type);
582 write_exp_elt_longcst (high_bound);
583 write_exp_elt_opcode (OP_LONG);
584 write_exp_elt_opcode (BINOP_RANGE);
587 error ("(*) in invalid context");
590 error ("(*) only possible with modename in front of tuple (mode[..])");
595 while (check_token (','))
598 write_exp_elt_opcode (BINOP_COMMA);
604 parse_untyped_expr ();
605 if (check_token (':'))
607 /* A powerset range or a labeled Array. */
608 parse_untyped_expr ();
609 write_exp_elt_opcode (BINOP_RANGE);
613 /* Matches: a COMMA-separated list of tuple elements.
614 Returns a list (of TREE_LIST nodes). */
616 parse_opt_element_list (struct type *type)
619 if (PEEK_TOKEN () == ']')
623 parse_tuple_element (type);
625 if (PEEK_TOKEN () == ']')
627 if (!check_token (','))
628 error ("bad syntax in tuple");
632 /* Parses: '[' elements ']'
633 If modename is non-NULL it prefixed the tuple. */
636 parse_tuple (struct type *mode)
640 type = check_typedef (mode);
645 parse_opt_element_list (type);
646 expect (']', "missing ']' after tuple");
647 write_exp_elt_opcode (OP_ARRAY);
648 write_exp_elt_longcst ((LONGEST) 0);
649 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
650 write_exp_elt_opcode (OP_ARRAY);
653 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
654 && TYPE_CODE (type) != TYPE_CODE_STRUCT
655 && TYPE_CODE (type) != TYPE_CODE_SET)
656 error ("invalid tuple mode");
657 write_exp_elt_opcode (UNOP_CAST);
658 write_exp_elt_type (mode);
659 write_exp_elt_opcode (UNOP_CAST);
669 switch (PEEK_TOKEN ())
671 case INTEGER_LITERAL:
672 case CHARACTER_LITERAL:
673 write_exp_elt_opcode (OP_LONG);
674 write_exp_elt_type (PEEK_LVAL ().typed_val.type);
675 write_exp_elt_longcst (PEEK_LVAL ().typed_val.val);
676 write_exp_elt_opcode (OP_LONG);
679 case BOOLEAN_LITERAL:
680 write_exp_elt_opcode (OP_BOOL);
681 write_exp_elt_longcst ((LONGEST) PEEK_LVAL ().ulval);
682 write_exp_elt_opcode (OP_BOOL);
686 write_exp_elt_opcode (OP_DOUBLE);
687 write_exp_elt_type (builtin_type_double);
688 write_exp_elt_dblcst (PEEK_LVAL ().dval);
689 write_exp_elt_opcode (OP_DOUBLE);
692 case EMPTINESS_LITERAL:
693 write_exp_elt_opcode (OP_LONG);
694 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
695 write_exp_elt_longcst (0);
696 write_exp_elt_opcode (OP_LONG);
699 case CHARACTER_STRING_LITERAL:
700 write_exp_elt_opcode (OP_STRING);
701 write_exp_string (PEEK_LVAL ().sval);
702 write_exp_elt_opcode (OP_STRING);
705 case BIT_STRING_LITERAL:
706 write_exp_elt_opcode (OP_BITSTRING);
707 write_exp_bitstring (PEEK_LVAL ().sval);
708 write_exp_elt_opcode (OP_BITSTRING);
713 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
714 which casts to an artificial array. */
717 if (PEEK_TOKEN () != TYPENAME)
718 error ("missing MODENAME after ARRAY()");
719 type = PEEK_LVAL ().tsym.type;
723 expect (')', "missing right parenthesis");
724 type = create_array_type ((struct type *) NULL, type,
725 create_range_type ((struct type *) NULL,
726 builtin_type_int, 0, 0));
727 TYPE_ARRAY_UPPER_BOUND_TYPE (type) = BOUND_CANNOT_BE_DETERMINED;
728 write_exp_elt_opcode (UNOP_CAST);
729 write_exp_elt_type (type);
730 write_exp_elt_opcode (UNOP_CAST);
742 expect (')', "missing right parenthesis");
747 case GENERAL_PROCEDURE_NAME:
749 write_exp_elt_opcode (OP_VAR_VALUE);
750 write_exp_elt_block (NULL);
751 write_exp_elt_sym (PEEK_LVAL ().ssym.sym);
752 write_exp_elt_opcode (OP_VAR_VALUE);
755 case GDB_VARIABLE: /* gdb specific */
760 write_exp_elt_opcode (UNOP_CAST);
761 write_exp_elt_type (builtin_type_int);
762 write_exp_elt_opcode (UNOP_CAST);
766 write_exp_elt_opcode (UNOP_CARD);
770 write_exp_elt_opcode (UNOP_CHMAX);
774 write_exp_elt_opcode (UNOP_CHMIN);
778 goto unimplemented_unary_builtin;
781 goto unimplemented_unary_builtin;
784 goto unimplemented_unary_builtin;
785 unimplemented_unary_builtin:
787 error ("not implemented: %s builtin function", op_name);
791 write_exp_elt_opcode (UNOP_ADDR);
794 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:
934 parse_operand6 (void)
936 if (check_token (RECEIVE))
939 error ("not implemented: RECEIVE expression");
941 else if (check_token (POINTER))
944 write_exp_elt_opcode (UNOP_ADDR);
951 parse_operand5 (void)
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 ())
966 op = UNOP_LOGICAL_NOT;
978 write_exp_elt_opcode (op);
982 parse_operand4 (void)
988 switch (PEEK_TOKEN ())
1007 write_exp_elt_opcode (op);
1012 parse_operand3 (void)
1018 switch (PEEK_TOKEN ())
1034 write_exp_elt_opcode (op);
1039 parse_operand2 (void)
1045 if (check_token (IN))
1048 write_exp_elt_opcode (BINOP_IN);
1052 switch (PEEK_TOKEN ())
1070 op = BINOP_NOTEQUAL;
1077 write_exp_elt_opcode (op);
1083 parse_operand1 (void)
1089 switch (PEEK_TOKEN ())
1092 op = BINOP_BITWISE_AND;
1095 op = BINOP_LOGICAL_AND;
1102 write_exp_elt_opcode (op);
1107 parse_operand0 (void)
1113 switch (PEEK_TOKEN ())
1116 op = BINOP_BITWISE_IOR;
1119 op = BINOP_BITWISE_XOR;
1122 op = BINOP_LOGICAL_OR;
1129 write_exp_elt_opcode (op);
1137 if (check_token (GDB_ASSIGNMENT))
1140 write_exp_elt_opcode (BINOP_ASSIGN);
1145 parse_then_alternative (void)
1147 expect (THEN, "missing 'THEN' in 'IF' expression");
1152 parse_else_alternative (void)
1154 if (check_token (ELSIF))
1155 parse_if_expression_body ();
1156 else if (check_token (ELSE))
1159 error ("missing ELSE/ELSIF in IF expression");
1162 /* Matches: <boolean expression> <then alternative> <else alternative> */
1165 parse_if_expression_body (void)
1168 parse_then_alternative ();
1169 parse_else_alternative ();
1170 write_exp_elt_opcode (TERNOP_COND);
1174 parse_if_expression (void)
1177 parse_if_expression_body ();
1178 expect (FI, "missing 'FI' at end of conditional expression");
1181 /* An <untyped_expr> is a superset of <expr>. It also includes
1182 <conditional expressions> and untyped <tuples>, whose types
1183 are not given by their constituents. Hence, these are only
1184 allowed in certain contexts that expect a certain type.
1185 You should call convert() to fix up the <untyped_expr>. */
1188 parse_untyped_expr (void)
1190 switch (PEEK_TOKEN ())
1193 parse_if_expression ();
1196 error ("not implemented: CASE expression");
1198 switch (PEEK_TOKEN1 ())
1206 parse_untyped_expr ();
1207 expect (')', "missing ')'");
1220 terminal_buffer[0] = TOKEN_NOT_READ;
1221 if (PEEK_TOKEN () == TYPENAME && PEEK_TOKEN1 () == END_TOKEN)
1223 write_exp_elt_opcode (OP_TYPE);
1224 write_exp_elt_type (PEEK_LVAL ().tsym.type);
1225 write_exp_elt_opcode (OP_TYPE);
1230 if (terminal_buffer[0] != END_TOKEN)
1232 if (comma_terminates && terminal_buffer[0] == ',')
1233 lexptr--; /* Put the comma back. */
1235 error ("Junk after end of expression.");
1241 /* Implementation of a dynamically expandable buffer for processing input
1242 characters acquired through lexptr and building a value to return in
1245 static char *tempbuf; /* Current buffer contents */
1246 static int tempbufsize; /* Size of allocated buffer */
1247 static int tempbufindex; /* Current index into buffer */
1249 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1251 #define CHECKBUF(size) \
1253 if (tempbufindex + (size) >= tempbufsize) \
1255 growbuf_by_size (size); \
1259 /* Grow the static temp buffer if necessary, including allocating the first one
1263 growbuf_by_size (int count)
1267 growby = max (count, GROWBY_MIN_SIZE);
1268 tempbufsize += growby;
1269 if (tempbuf == NULL)
1271 tempbuf = (char *) xmalloc (tempbufsize);
1275 tempbuf = (char *) xrealloc (tempbuf, tempbufsize);
1279 /* Try to consume a simple name string token. If successful, returns
1280 a pointer to a nullbyte terminated copy of the name that can be used
1281 in symbol table lookups. If not successful, returns NULL. */
1284 match_simple_name_string (void)
1286 char *tokptr = lexptr;
1288 if (isalpha (*tokptr) || *tokptr == '_')
1295 while (isalnum (*tokptr) || (*tokptr == '_'));
1296 yylval.sval.ptr = lexptr;
1297 yylval.sval.length = tokptr - lexptr;
1299 result = copy_name (yylval.sval);
1305 /* Start looking for a value composed of valid digits as set by the base
1306 in use. Note that '_' characters are valid anywhere, in any quantity,
1307 and are simply ignored. Since we must find at least one valid digit,
1308 or reject this token as an integer literal, we keep track of how many
1309 digits we have encountered. */
1312 decode_integer_value (int base, char **tokptrptr, LONGEST *ivalptr)
1314 char *tokptr = *tokptrptr;
1318 while (*tokptr != '\0')
1322 temp = tolower (temp);
1361 /* Found something not in domain for current base. */
1362 tokptr--; /* Unconsume what gave us indigestion. */
1367 /* If we didn't find any digits, then we don't have a valid integer
1368 value, so reject the entire token. Otherwise, update the lexical
1369 scan pointer, and return non-zero for success. */
1377 *tokptrptr = tokptr;
1383 decode_integer_literal (LONGEST *valptr, char **tokptrptr)
1385 char *tokptr = *tokptrptr;
1388 int explicit_base = 0;
1390 /* Look for an explicit base specifier, which is optional. */
1423 /* If we found an explicit base ensure that the character after the
1424 explicit base is a single quote. */
1426 if (explicit_base && (*tokptr++ != '\''))
1431 /* Attempt to decode whatever follows as an integer value in the
1432 indicated base, updating the token pointer in the process and
1433 computing the value into ival. Also, if we have an explicit
1434 base, then the next character must not be a single quote, or we
1435 have a bitstring literal, so reject the entire token in this case.
1436 Otherwise, update the lexical scan pointer, and return non-zero
1439 if (!decode_integer_value (base, &tokptr, &ival))
1443 else if (explicit_base && (*tokptr == '\''))
1450 *tokptrptr = tokptr;
1455 /* If it wasn't for the fact that floating point values can contain '_'
1456 characters, we could just let strtod do all the hard work by letting it
1457 try to consume as much of the current token buffer as possible and
1458 find a legal conversion. Unfortunately we need to filter out the '_'
1459 characters before calling strtod, which we do by copying the other
1460 legal chars to a local buffer to be converted. However since we also
1461 need to keep track of where the last unconsumed character in the input
1462 buffer is, we have transfer only as many characters as may compose a
1463 legal floating point value. */
1465 static enum ch_terminal
1466 match_float_literal (void)
1468 char *tokptr = lexptr;
1472 extern double strtod ();
1474 /* Make local buffer in which to build the string to convert. This is
1475 required because underscores are valid in chill floating point numbers
1476 but not in the string passed to strtod to convert. The string will be
1477 no longer than our input string. */
1479 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1481 /* Transfer all leading digits to the conversion buffer, discarding any
1484 while (isdigit (*tokptr) || *tokptr == '_')
1493 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1494 of whether we found any leading digits, and we simply accept it and
1495 continue on to look for the fractional part and/or exponent. One of
1496 [eEdD] is legal only if we have seen digits, and means that there
1497 is no fractional part. If we find neither of these, then this is
1498 not a floating point number, so return failure. */
1503 /* Accept and then look for fractional part and/or exponent. */
1516 goto collect_exponent;
1524 /* We found a '.', copy any fractional digits to the conversion buffer, up
1525 to the first nondigit, non-underscore character. */
1527 while (isdigit (*tokptr) || *tokptr == '_')
1536 /* Look for an exponent, which must start with one of [eEdD]. If none
1537 is found, jump directly to trying to convert what we have collected
1554 /* Accept an optional '-' or '+' following one of [eEdD]. */
1557 if (*tokptr == '+' || *tokptr == '-')
1559 *copy++ = *tokptr++;
1562 /* Now copy an exponent into the conversion buffer. Note that at the
1563 moment underscores are *not* allowed in exponents. */
1565 while (isdigit (*tokptr))
1567 *copy++ = *tokptr++;
1570 /* If we transfered any chars to the conversion buffer, try to interpret its
1571 contents as a floating point value. If any characters remain, then we
1572 must not have a valid floating point string. */
1578 dval = strtod (buf, ©);
1583 return (FLOAT_LITERAL);
1589 /* Recognize a string literal. A string literal is a sequence
1590 of characters enclosed in matching single or double quotes, except that
1591 a single character inside single quotes is a character literal, which
1592 we reject as a string literal. To embed the terminator character inside
1593 a string, it is simply doubled (I.E. "this""is""one""string") */
1595 static enum ch_terminal
1596 match_string_literal (void)
1598 char *tokptr = lexptr;
1602 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1608 /* skip possible whitespaces */
1609 while ((*tokptr == ' ' || *tokptr == '\t') && *tokptr)
1617 else if (*tokptr != ',')
1618 error ("Invalid control sequence");
1620 /* skip possible whitespaces */
1621 while ((*tokptr == ' ' || *tokptr == '\t') && *tokptr)
1623 if (!decode_integer_literal (&ival, &tokptr))
1624 error ("Invalid control sequence");
1627 else if (*tokptr == *lexptr)
1629 if (*(tokptr + 1) == *lexptr)
1638 else if (*tokptr == '^')
1640 if (*(tokptr + 1) == '(')
1644 if (!decode_integer_literal (&ival, &tokptr))
1645 error ("Invalid control sequence");
1648 else if (*(tokptr + 1) == '^')
1651 error ("Invalid control sequence");
1655 tempbuf[tempbufindex++] = ival;
1658 error ("Invalid control sequence");
1660 if (*tokptr == '\0' /* no terminator */
1661 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1667 tempbuf[tempbufindex] = '\0';
1668 yylval.sval.ptr = tempbuf;
1669 yylval.sval.length = tempbufindex;
1671 return (CHARACTER_STRING_LITERAL);
1675 /* Recognize a character literal. A character literal is single character
1676 or a control sequence, enclosed in single quotes. A control sequence
1677 is a comma separated list of one or more integer literals, enclosed
1678 in parenthesis and introduced with a circumflex character.
1680 EX: 'a' '^(7)' '^(7,8)'
1682 As a GNU chill extension, the syntax C'xx' is also recognized as a
1683 character literal, where xx is a hex value for the character.
1685 Note that more than a single character, enclosed in single quotes, is
1688 Returns CHARACTER_LITERAL if a match is found.
1691 static enum ch_terminal
1692 match_character_literal (void)
1694 char *tokptr = lexptr;
1697 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
1699 /* We have a GNU chill extension form, so skip the leading "C'",
1700 decode the hex value, and then ensure that we have a trailing
1701 single quote character. */
1703 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1709 else if (*tokptr == '\'')
1713 /* Determine which form we have, either a control sequence or the
1714 single character form. */
1718 if (*(tokptr + 1) == '(')
1720 /* Match and decode a control sequence. Return zero if we don't
1721 find a valid integer literal, or if the next unconsumed character
1722 after the integer literal is not the trailing ')'. */
1724 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1729 else if (*(tokptr + 1) == '^')
1736 error ("Invalid control sequence");
1738 else if (*tokptr == '\'')
1740 /* this must be duplicated */
1749 /* The trailing quote has not yet been consumed. If we don't find
1750 it, then we have no match. */
1752 if (*tokptr++ != '\'')
1759 /* Not a character literal. */
1762 yylval.typed_val.val = ival;
1763 yylval.typed_val.type = builtin_type_chill_char;
1765 return (CHARACTER_LITERAL);
1768 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1769 Note that according to 5.2.4.2, a single "_" is also a valid integer
1770 literal, however GNU-chill requires there to be at least one "digit"
1771 in any integer literal. */
1773 static enum ch_terminal
1774 match_integer_literal (void)
1776 char *tokptr = lexptr;
1779 if (!decode_integer_literal (&ival, &tokptr))
1785 yylval.typed_val.val = ival;
1786 #if defined(CC_HAS_LONG_LONG)
1787 if (ival > (LONGEST) 2147483647U || ival < -(LONGEST) 2147483648U)
1788 yylval.typed_val.type = builtin_type_long_long;
1791 yylval.typed_val.type = builtin_type_int;
1793 return (INTEGER_LITERAL);
1797 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1798 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1799 literal, however GNU-chill requires there to be at least one "digit"
1800 in any bit-string literal. */
1802 static enum ch_terminal
1803 match_bitstring_literal (void)
1805 register char *tokptr = lexptr;
1815 /* Look for the required explicit base specifier. */
1836 /* Ensure that the character after the explicit base is a single quote. */
1838 if (*tokptr++ != '\'')
1843 while (*tokptr != '\0' && *tokptr != '\'')
1846 if (isupper (digit))
1847 digit = tolower (digit);
1875 /* this is not a bitstring literal, probably an integer */
1878 if (digit >= 1 << bits_per_char)
1880 /* Found something not in domain for current base. */
1881 error ("Too-large digit in bitstring or integer.");
1885 /* Extract bits from digit, packing them into the bitstring byte. */
1886 int k = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? bits_per_char - 1 : 0;
1887 for (; TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? k >= 0 : k < bits_per_char;
1888 TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? k-- : k++)
1891 if (digit & (1 << k))
1893 tempbuf[tempbufindex] |=
1894 (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1895 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1899 if (bitoffset == HOST_CHAR_BIT)
1904 tempbuf[tempbufindex] = 0;
1910 /* Verify that we consumed everything up to the trailing single quote,
1911 and that we found some bits (IE not just underbars). */
1913 if (*tokptr++ != '\'')
1919 yylval.sval.ptr = tempbuf;
1920 yylval.sval.length = bitcount;
1922 return (BIT_STRING_LITERAL);
1932 static const struct token idtokentab[] =
1956 {"addr", ADDR_TOKEN},
1957 {"null", EMPTINESS_LITERAL}
1960 static const struct token tokentab2[] =
1962 {":=", GDB_ASSIGNMENT},
1963 {"//", SLASH_SLASH},
1970 /* Read one token, getting characters through lexptr. */
1971 /* This is where we will check to make sure that the language and the
1972 operators used are compatible. */
1974 static enum ch_terminal
1978 enum ch_terminal token;
1982 /* Skip over any leading whitespace. */
1983 while (isspace (*lexptr))
1987 /* Look for special single character cases which can't be the first
1988 character of some other multicharacter token. */
2005 /* Look for characters which start a particular kind of multicharacter
2006 token, such as a character literal, register name, convenience
2007 variable name, string literal, etc. */
2012 /* First try to match a string literal, which is any
2013 sequence of characters enclosed in matching single or double
2014 quotes, except that a single character inside single quotes
2015 is a character literal, so we have to catch that case also. */
2016 token = match_string_literal ();
2021 if (*lexptr == '\'')
2023 token = match_character_literal ();
2032 token = match_character_literal ();
2039 yylval.sval.ptr = lexptr;
2044 while (isalnum (*lexptr) || *lexptr == '_' || *lexptr == '$');
2045 yylval.sval.length = lexptr - yylval.sval.ptr;
2046 write_dollar_variable (yylval.sval);
2047 return GDB_VARIABLE;
2050 /* See if it is a special token of length 2. */
2051 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
2053 if (STREQN (lexptr, tokentab2[i].operator, 2))
2056 return (tokentab2[i].token);
2059 /* Look for single character cases which which could be the first
2060 character of some other multicharacter token, but aren't, or we
2061 would already have found it. */
2071 /* Look for a float literal before looking for an integer literal, so
2072 we match as much of the input stream as possible. */
2073 token = match_float_literal ();
2078 token = match_bitstring_literal ();
2083 token = match_integer_literal ();
2089 /* Try to match a simple name string, and if a match is found, then
2090 further classify what sort of name it is and return an appropriate
2091 token. Note that attempting to match a simple name string consumes
2092 the token from lexptr, so we can't back out if we later find that
2093 we can't classify what sort of name it is. */
2095 inputname = match_simple_name_string ();
2097 if (inputname != NULL)
2099 char *simplename = (char *) alloca (strlen (inputname) + 1);
2101 char *dptr = simplename, *sptr = inputname;
2102 for (; *sptr; sptr++)
2103 *dptr++ = isupper (*sptr) ? tolower (*sptr) : *sptr;
2106 /* See if it is a reserved identifier. */
2107 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
2109 if (STREQ (simplename, idtokentab[i].operator))
2111 return (idtokentab[i].token);
2115 /* Look for other special tokens. */
2116 if (STREQ (simplename, "true"))
2119 return (BOOLEAN_LITERAL);
2121 if (STREQ (simplename, "false"))
2124 return (BOOLEAN_LITERAL);
2127 sym = lookup_symbol (inputname, expression_context_block,
2128 VAR_NAMESPACE, (int *) NULL,
2129 (struct symtab **) NULL);
2130 if (sym == NULL && strcmp (inputname, simplename) != 0)
2132 sym = lookup_symbol (simplename, expression_context_block,
2133 VAR_NAMESPACE, (int *) NULL,
2134 (struct symtab **) NULL);
2138 yylval.ssym.stoken.ptr = NULL;
2139 yylval.ssym.stoken.length = 0;
2140 yylval.ssym.sym = sym;
2141 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
2142 switch (SYMBOL_CLASS (sym))
2145 /* Found a procedure name. */
2146 return (GENERAL_PROCEDURE_NAME);
2148 /* Found a global or local static variable. */
2149 return (LOCATION_NAME);
2154 case LOC_REGPARM_ADDR:
2158 case LOC_BASEREG_ARG:
2159 if (innermost_block == NULL
2160 || contained_in (block_found, innermost_block))
2162 innermost_block = block_found;
2164 return (LOCATION_NAME);
2168 return (LOCATION_NAME);
2171 yylval.tsym.type = SYMBOL_TYPE (sym);
2174 case LOC_CONST_BYTES:
2175 case LOC_OPTIMIZED_OUT:
2176 error ("Symbol \"%s\" names no location.", inputname);
2179 internal_error (__FILE__, __LINE__,
2180 "unhandled SYMBOL_CLASS in ch_lex()");
2184 else if (!have_full_symbols () && !have_partial_symbols ())
2186 error ("No symbol table is loaded. Use the \"file\" command.");
2190 error ("No symbol \"%s\" in current context.", inputname);
2194 /* Catch single character tokens which are not part of some
2199 case '.': /* Not float for example. */
2201 while (isspace (*lexptr))
2203 inputname = match_simple_name_string ();
2206 return DOT_FIELD_NAME;
2209 return (ILLEGAL_TOKEN);
2213 write_lower_upper_value (enum exp_opcode opcode, /* Either UNOP_LOWER or UNOP_UPPER */
2217 write_exp_elt_opcode (opcode);
2220 struct type *result_type;
2221 LONGEST val = type_lower_upper (opcode, type, &result_type);
2222 write_exp_elt_opcode (OP_LONG);
2223 write_exp_elt_type (result_type);
2224 write_exp_elt_longcst (val);
2225 write_exp_elt_opcode (OP_LONG);
2230 chill_error (char *msg)