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 malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
57 #include "expression.h"
60 #include "parser-defs.h"
62 #include "bfd.h" /* Required by objfiles.h. */
63 #include "symfile.h" /* Required by objfiles.h. */
64 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
70 unsigned LONGEST ulval
;
85 /* '\001' ... '\xff' come first. */
91 GENERAL_PROCEDURE_NAME
,
94 CHARACTER_STRING_LITERAL
,
140 /* Forward declarations. */
141 static void parse_expr ();
142 static void parse_primval ();
143 static void parse_untyped_expr ();
144 static int parse_opt_untyped_expr ();
145 static void parse_if_expression_body
PARAMS((void));
146 static void write_lower_upper_value
PARAMS ((enum exp_opcode
, struct type
*));
147 static enum ch_terminal
ch_lex ();
149 #define MAX_LOOK_AHEAD 2
150 static enum ch_terminal terminal_buffer
[MAX_LOOK_AHEAD
+1] = {
151 TOKEN_NOT_READ
, TOKEN_NOT_READ
, TOKEN_NOT_READ
};
152 static YYSTYPE yylval
;
153 static YYSTYPE val_buffer
[MAX_LOOK_AHEAD
+1];
155 /*int current_token, lookahead_token;*/
160 static enum ch_terminal
163 if (terminal_buffer
[0] == TOKEN_NOT_READ
)
165 terminal_buffer
[0] = ch_lex ();
166 val_buffer
[0] = yylval
;
168 return terminal_buffer
[0];
170 #define PEEK_LVAL() val_buffer[0]
171 #define PEEK_TOKEN1() peek_token_(1)
172 #define PEEK_TOKEN2() peek_token_(2)
173 static enum ch_terminal
177 if (i
> MAX_LOOK_AHEAD
)
178 fatal ("internal error - too much lookahead");
179 if (terminal_buffer
[i
] == TOKEN_NOT_READ
)
181 terminal_buffer
[i
] = ch_lex ();
182 val_buffer
[i
] = yylval
;
184 return terminal_buffer
[i
];
188 pushback_token (code
, node
)
189 enum ch_terminal code
;
193 if (terminal_buffer
[MAX_LOOK_AHEAD
] != TOKEN_NOT_READ
)
194 fatal ("internal error - cannot pushback token");
195 for (i
= MAX_LOOK_AHEAD
; i
> 0; i
--)
197 terminal_buffer
[i
] = terminal_buffer
[i
- 1];
198 val_buffer
[i
] = val_buffer
[i
- 1];
200 terminal_buffer
[0] = code
;
201 val_buffer
[0] = node
;
208 for (i
= 0; i
< MAX_LOOK_AHEAD
; i
++)
210 terminal_buffer
[i
] = terminal_buffer
[i
+1];
211 val_buffer
[i
] = val_buffer
[i
+1];
213 terminal_buffer
[MAX_LOOK_AHEAD
] = TOKEN_NOT_READ
;
215 #define FORWARD_TOKEN() forward_token_()
217 /* Skip the next token.
218 if it isn't TOKEN, the parser is broken. */
222 enum ch_terminal token
;
224 if (PEEK_TOKEN() != token
)
227 sprintf (buf
, "internal parser error - expected token %d", (int)token
);
235 enum ch_terminal token
;
237 if (PEEK_TOKEN() != token
)
243 /* return 0 if expected token was not found,
247 expect(token
, message
)
248 enum ch_terminal token
;
251 if (PEEK_TOKEN() != token
)
255 else if (token
< 256)
256 error ("syntax error - expected a '%c' here \"%s\"", token
, lexptr
);
258 error ("syntax error");
268 parse_opt_name_string (allow_all
)
269 int allow_all
; /* 1 if ALL is allowed as a postfix */
271 int token
= PEEK_TOKEN();
275 if (token
== ALL
&& allow_all
)
286 token
= PEEK_TOKEN();
290 token
= PEEK_TOKEN();
291 if (token
== ALL
&& allow_all
)
292 return get_identifier3(IDENTIFIER_POINTER (name
), "!", "*");
296 error ("'%s!' is not followed by an identifier",
297 IDENTIFIER_POINTER (name
));
300 name
= get_identifier3(IDENTIFIER_POINTER(name
),
301 "!", IDENTIFIER_POINTER(PEEK_LVAL()));
306 parse_simple_name_string ()
308 int token
= PEEK_TOKEN();
312 error ("expected a name here");
313 return error_mark_node
;
323 tree name
= parse_opt_name_string (0);
327 error ("expected a name string here");
328 return error_mark_node
;
331 /* Matches: <name_string>
332 Returns if pass 1: the identifier.
333 Returns if pass 2: a decl or value for identifier. */
338 tree name
= parse_name_string ();
339 if (pass
== 1 || ignoring
)
343 tree decl
= lookup_name (name
);
344 if (decl
== NULL_TREE
)
346 error ("`%s' undeclared", IDENTIFIER_POINTER (name
));
347 return error_mark_node
;
349 else if (TREE_CODE (TREE_TYPE (decl
)) == ERROR_MARK
)
350 return error_mark_node
;
351 else if (TREE_CODE (decl
) == CONST_DECL
)
352 return DECL_INITIAL (decl
);
353 else if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
354 return convert_from_reference (decl
);
363 pushback_paren_expr (expr
)
366 if (pass
== 1 && !ignoring
)
367 expr
= build1 (PAREN_EXPR
, NULL_TREE
, expr
);
368 pushback_token (EXPR
, expr
);
372 /* Matches: <case label> */
377 if (check_token (ELSE
))
378 error ("ELSE in tuples labels not implemented");
379 /* Does not handle the case of a mode name. FIXME */
381 if (check_token (':'))
384 write_exp_elt_opcode (BINOP_RANGE
);
389 parse_opt_untyped_expr ()
391 switch (PEEK_TOKEN ())
398 parse_untyped_expr ();
412 /* Parse NAME '(' MODENAME ')'. */
420 if (PEEK_TOKEN () != TYPENAME
)
421 error ("expect MODENAME here `%s'", lexptr
);
422 type
= PEEK_LVAL().tsym
.type
;
429 parse_mode_or_normal_call ()
434 if (PEEK_TOKEN () == TYPENAME
)
436 type
= PEEK_LVAL().tsym
.type
;
448 /* Parse something that looks like a function call.
449 Assume we have parsed the function, and are at the '('. */
456 /* This is to save the value of arglist_len
457 being accumulated for each dimension. */
459 if (parse_opt_untyped_expr ())
461 int tok
= PEEK_TOKEN ();
463 if (tok
== UP
|| tok
== ':')
467 expect (')', "expected ')' to terminate slice");
469 write_exp_elt_opcode (tok
== UP
? TERNOP_SLICE_COUNT
473 while (check_token (','))
475 parse_untyped_expr ();
482 arg_count
= end_arglist ();
483 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
484 write_exp_elt_longcst (arg_count
);
485 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
489 parse_named_record_element ()
493 label
= PEEK_LVAL ().sval
;
494 expect (FIELD_NAME
, "expected a field name here `%s'", lexptr
);
495 if (check_token (','))
496 parse_named_record_element ();
497 else if (check_token (':'))
500 error ("syntax error near `%s' in named record tuple element", lexptr
);
501 write_exp_elt_opcode (OP_LABELED
);
502 write_exp_string (label
);
503 write_exp_elt_opcode (OP_LABELED
);
506 /* Returns one or nore TREE_LIST nodes, in reverse order. */
509 parse_tuple_element ()
511 if (PEEK_TOKEN () == FIELD_NAME
)
513 /* Parse a labelled structure tuple. */
514 parse_named_record_element ();
518 if (check_token ('('))
520 if (check_token ('*'))
522 expect (')', "missing ')' after '*' case label list");
523 error ("(*) not implemented in case label list");
528 while (check_token (','))
531 write_exp_elt_opcode (BINOP_COMMA
);
537 parse_untyped_expr ();
538 if (check_token (':'))
540 /* A powerset range or a labeled Array. */
541 parse_untyped_expr ();
542 write_exp_elt_opcode (BINOP_RANGE
);
546 /* Matches: a COMMA-separated list of tuple elements.
547 Returns a list (of TREE_LIST nodes). */
549 parse_opt_element_list ()
552 if (PEEK_TOKEN () == ']')
556 parse_tuple_element ();
558 if (PEEK_TOKEN () == ']')
560 if (!check_token (','))
561 error ("bad syntax in tuple");
565 /* Parses: '[' elements ']'
566 If modename is non-NULL it prefixed the tuple. */
574 parse_opt_element_list ();
575 expect (']', "missing ']' after tuple");
576 write_exp_elt_opcode (OP_ARRAY
);
577 write_exp_elt_longcst ((LONGEST
) 0);
578 write_exp_elt_longcst ((LONGEST
) end_arglist () - 1);
579 write_exp_elt_opcode (OP_ARRAY
);
582 struct type
*type
= check_typedef (mode
);
583 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
584 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
585 && TYPE_CODE (type
) != TYPE_CODE_SET
)
586 error ("invalid tuple mode");
587 write_exp_elt_opcode (UNOP_CAST
);
588 write_exp_elt_type (mode
);
589 write_exp_elt_opcode (UNOP_CAST
);
599 switch (PEEK_TOKEN ())
601 case INTEGER_LITERAL
:
602 case CHARACTER_LITERAL
:
603 write_exp_elt_opcode (OP_LONG
);
604 write_exp_elt_type (PEEK_LVAL ().typed_val
.type
);
605 write_exp_elt_longcst (PEEK_LVAL ().typed_val
.val
);
606 write_exp_elt_opcode (OP_LONG
);
609 case BOOLEAN_LITERAL
:
610 write_exp_elt_opcode (OP_BOOL
);
611 write_exp_elt_longcst ((LONGEST
) PEEK_LVAL ().ulval
);
612 write_exp_elt_opcode (OP_BOOL
);
616 write_exp_elt_opcode (OP_DOUBLE
);
617 write_exp_elt_type (builtin_type_double
);
618 write_exp_elt_dblcst (PEEK_LVAL ().dval
);
619 write_exp_elt_opcode (OP_DOUBLE
);
622 case EMPTINESS_LITERAL
:
623 write_exp_elt_opcode (OP_LONG
);
624 write_exp_elt_type (lookup_pointer_type (builtin_type_void
));
625 write_exp_elt_longcst (0);
626 write_exp_elt_opcode (OP_LONG
);
629 case CHARACTER_STRING_LITERAL
:
630 write_exp_elt_opcode (OP_STRING
);
631 write_exp_string (PEEK_LVAL ().sval
);
632 write_exp_elt_opcode (OP_STRING
);
635 case BIT_STRING_LITERAL
:
636 write_exp_elt_opcode (OP_BITSTRING
);
637 write_exp_bitstring (PEEK_LVAL ().sval
);
638 write_exp_elt_opcode (OP_BITSTRING
);
643 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
644 which casts to an artificial array. */
647 if (PEEK_TOKEN () != TYPENAME
)
648 error ("missing MODENAME after ARRAY()");
649 type
= PEEK_LVAL().tsym
.type
;
653 expect (')', "missing right parenthesis");
654 type
= create_array_type ((struct type
*) NULL
, type
,
655 create_range_type ((struct type
*) NULL
,
656 builtin_type_int
, 0, 0));
657 TYPE_ARRAY_UPPER_BOUND_TYPE(type
) = BOUND_CANNOT_BE_DETERMINED
;
658 write_exp_elt_opcode (UNOP_CAST
);
659 write_exp_elt_type (type
);
660 write_exp_elt_opcode (UNOP_CAST
);
672 expect (')', "missing right parenthesis");
677 case GENERAL_PROCEDURE_NAME
:
679 write_exp_elt_opcode (OP_VAR_VALUE
);
680 write_exp_elt_block (NULL
);
681 write_exp_elt_sym (PEEK_LVAL ().ssym
.sym
);
682 write_exp_elt_opcode (OP_VAR_VALUE
);
685 case GDB_VARIABLE
: /* gdb specific */
690 write_exp_elt_opcode (UNOP_CAST
);
691 write_exp_elt_type (builtin_type_int
);
692 write_exp_elt_opcode (UNOP_CAST
);
694 case PRED
: op_name
= "PRED"; goto unimplemented_unary_builtin
;
695 case SUCC
: op_name
= "SUCC"; goto unimplemented_unary_builtin
;
696 case ABS
: op_name
= "ABS"; goto unimplemented_unary_builtin
;
697 case CARD
: op_name
= "CARD"; goto unimplemented_unary_builtin
;
698 case MAX_TOKEN
: op_name
= "MAX"; goto unimplemented_unary_builtin
;
699 case MIN_TOKEN
: op_name
= "MIN"; goto unimplemented_unary_builtin
;
700 unimplemented_unary_builtin
:
702 error ("not implemented: %s builtin function", op_name
);
706 write_exp_elt_opcode (UNOP_ADDR
);
709 type
= parse_mode_or_normal_call ();
711 { write_exp_elt_opcode (OP_LONG
);
712 write_exp_elt_type (builtin_type_int
);
713 CHECK_TYPEDEF (type
);
714 write_exp_elt_longcst ((LONGEST
) TYPE_LENGTH (type
));
715 write_exp_elt_opcode (OP_LONG
);
718 write_exp_elt_opcode (UNOP_SIZEOF
);
727 type
= parse_mode_or_normal_call ();
728 write_lower_upper_value (op
, type
);
732 write_exp_elt_opcode (UNOP_LENGTH
);
735 type
= PEEK_LVAL ().tsym
.type
;
737 switch (PEEK_TOKEN())
745 expect (')', "missing right parenthesis");
746 write_exp_elt_opcode (UNOP_CAST
);
747 write_exp_elt_type (type
);
748 write_exp_elt_opcode (UNOP_CAST
);
751 error ("typename in invalid context");
756 error ("invalid expression syntax at `%s'", lexptr
);
760 switch (PEEK_TOKEN ())
763 write_exp_elt_opcode (STRUCTOP_STRUCT
);
764 write_exp_string (PEEK_LVAL ().sval
);
765 write_exp_elt_opcode (STRUCTOP_STRUCT
);
770 if (PEEK_TOKEN () == TYPENAME
)
772 type
= PEEK_LVAL ().tsym
.type
;
773 write_exp_elt_opcode (UNOP_CAST
);
774 write_exp_elt_type (lookup_pointer_type (type
));
775 write_exp_elt_opcode (UNOP_CAST
);
778 write_exp_elt_opcode (UNOP_IND
);
783 case CHARACTER_STRING_LITERAL
:
784 case CHARACTER_LITERAL
:
785 case BIT_STRING_LITERAL
:
786 /* Handle string repetition. (See comment in parse_operand5.) */
788 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
789 write_exp_elt_longcst (1);
790 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
801 if (check_token (RECEIVE
))
804 error ("not implemented: RECEIVE expression");
806 else if (check_token (POINTER
))
809 write_exp_elt_opcode (UNOP_ADDR
);
819 /* We are supposed to be looking for a <string repetition operator>,
820 but in general we can't distinguish that from a parenthesized
821 expression. This is especially difficult if we allow the
822 string operand to be a constant expression (as requested by
823 some users), and not just a string literal.
824 Consider: LPRN expr RPRN LPRN expr RPRN
825 Is that a function call or string repetition?
826 Instead, we handle string repetition in parse_primval,
827 and build_generalized_call. */
828 switch (PEEK_TOKEN())
830 case NOT
: op
= UNOP_LOGICAL_NOT
; break;
831 case '-': op
= UNOP_NEG
; break;
839 write_exp_elt_opcode (op
);
849 switch (PEEK_TOKEN())
851 case '*': op
= BINOP_MUL
; break;
852 case '/': op
= BINOP_DIV
; break;
853 case MOD
: op
= BINOP_MOD
; break;
854 case REM
: op
= BINOP_REM
; break;
860 write_exp_elt_opcode (op
);
871 switch (PEEK_TOKEN())
873 case '+': op
= BINOP_ADD
; break;
874 case '-': op
= BINOP_SUB
; break;
875 case SLASH_SLASH
: op
= BINOP_CONCAT
; break;
881 write_exp_elt_opcode (op
);
892 if (check_token (IN
))
895 write_exp_elt_opcode (BINOP_IN
);
899 switch (PEEK_TOKEN())
901 case '>': op
= BINOP_GTR
; break;
902 case GEQ
: op
= BINOP_GEQ
; break;
903 case '<': op
= BINOP_LESS
; break;
904 case LEQ
: op
= BINOP_LEQ
; break;
905 case '=': op
= BINOP_EQUAL
; break;
906 case NOTEQUAL
: op
= BINOP_NOTEQUAL
; break;
912 write_exp_elt_opcode (op
);
924 switch (PEEK_TOKEN())
926 case LOGAND
: op
= BINOP_BITWISE_AND
; break;
927 case ANDIF
: op
= BINOP_LOGICAL_AND
; break;
933 write_exp_elt_opcode (op
);
944 switch (PEEK_TOKEN())
946 case LOGIOR
: op
= BINOP_BITWISE_IOR
; break;
947 case LOGXOR
: op
= BINOP_BITWISE_XOR
; break;
948 case ORIF
: op
= BINOP_LOGICAL_OR
; break;
954 write_exp_elt_opcode (op
);
962 if (check_token (GDB_ASSIGNMENT
))
965 write_exp_elt_opcode (BINOP_ASSIGN
);
970 parse_then_alternative ()
972 expect (THEN
, "missing 'THEN' in 'IF' expression");
977 parse_else_alternative ()
979 if (check_token (ELSIF
))
980 parse_if_expression_body ();
981 else if (check_token (ELSE
))
984 error ("missing ELSE/ELSIF in IF expression");
987 /* Matches: <boolean expression> <then alternative> <else alternative> */
990 parse_if_expression_body ()
993 parse_then_alternative ();
994 parse_else_alternative ();
995 write_exp_elt_opcode (TERNOP_COND
);
999 parse_if_expression ()
1002 parse_if_expression_body ();
1003 expect (FI
, "missing 'FI' at end of conditional expression");
1006 /* An <untyped_expr> is a superset of <expr>. It also includes
1007 <conditional expressions> and untyped <tuples>, whose types
1008 are not given by their constituents. Hence, these are only
1009 allowed in certain contexts that expect a certain type.
1010 You should call convert() to fix up the <untyped_expr>. */
1013 parse_untyped_expr ()
1015 switch (PEEK_TOKEN())
1018 parse_if_expression ();
1021 error ("not implemented: CASE expression");
1023 switch (PEEK_TOKEN1())
1031 parse_untyped_expr ();
1032 expect (')', "missing ')'");
1045 terminal_buffer
[0] = TOKEN_NOT_READ
;
1046 if (PEEK_TOKEN () == TYPENAME
&& PEEK_TOKEN1 () == END_TOKEN
)
1048 write_exp_elt_opcode(OP_TYPE
);
1049 write_exp_elt_type(PEEK_LVAL ().tsym
.type
);
1050 write_exp_elt_opcode(OP_TYPE
);
1055 if (terminal_buffer
[0] != END_TOKEN
)
1057 if (comma_terminates
&& terminal_buffer
[0] == ',')
1058 lexptr
--; /* Put the comma back. */
1060 error ("Junk after end of expression.");
1066 /* Implementation of a dynamically expandable buffer for processing input
1067 characters acquired through lexptr and building a value to return in
1070 static char *tempbuf
; /* Current buffer contents */
1071 static int tempbufsize
; /* Size of allocated buffer */
1072 static int tempbufindex
; /* Current index into buffer */
1074 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1076 #define CHECKBUF(size) \
1078 if (tempbufindex + (size) >= tempbufsize) \
1080 growbuf_by_size (size); \
1084 /* Grow the static temp buffer if necessary, including allocating the first one
1088 growbuf_by_size (count
)
1093 growby
= max (count
, GROWBY_MIN_SIZE
);
1094 tempbufsize
+= growby
;
1095 if (tempbuf
== NULL
)
1097 tempbuf
= (char *) malloc (tempbufsize
);
1101 tempbuf
= (char *) realloc (tempbuf
, tempbufsize
);
1105 /* Try to consume a simple name string token. If successful, returns
1106 a pointer to a nullbyte terminated copy of the name that can be used
1107 in symbol table lookups. If not successful, returns NULL. */
1110 match_simple_name_string ()
1112 char *tokptr
= lexptr
;
1114 if (isalpha (*tokptr
) || *tokptr
== '_')
1119 } while (isalnum (*tokptr
) || (*tokptr
== '_'));
1120 yylval
.sval
.ptr
= lexptr
;
1121 yylval
.sval
.length
= tokptr
- lexptr
;
1123 result
= copy_name (yylval
.sval
);
1129 /* Start looking for a value composed of valid digits as set by the base
1130 in use. Note that '_' characters are valid anywhere, in any quantity,
1131 and are simply ignored. Since we must find at least one valid digit,
1132 or reject this token as an integer literal, we keep track of how many
1133 digits we have encountered. */
1136 decode_integer_value (base
, tokptrptr
, ivalptr
)
1141 char *tokptr
= *tokptrptr
;
1145 while (*tokptr
!= '\0')
1149 temp
= tolower (temp
);
1155 case '0': case '1': case '2': case '3': case '4':
1156 case '5': case '6': case '7': case '8': case '9':
1159 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1175 /* Found something not in domain for current base. */
1176 tokptr
--; /* Unconsume what gave us indigestion. */
1181 /* If we didn't find any digits, then we don't have a valid integer
1182 value, so reject the entire token. Otherwise, update the lexical
1183 scan pointer, and return non-zero for success. */
1191 *tokptrptr
= tokptr
;
1197 decode_integer_literal (valptr
, tokptrptr
)
1201 char *tokptr
= *tokptrptr
;
1204 int explicit_base
= 0;
1206 /* Look for an explicit base specifier, which is optional. */
1239 /* If we found an explicit base ensure that the character after the
1240 explicit base is a single quote. */
1242 if (explicit_base
&& (*tokptr
++ != '\''))
1247 /* Attempt to decode whatever follows as an integer value in the
1248 indicated base, updating the token pointer in the process and
1249 computing the value into ival. Also, if we have an explicit
1250 base, then the next character must not be a single quote, or we
1251 have a bitstring literal, so reject the entire token in this case.
1252 Otherwise, update the lexical scan pointer, and return non-zero
1255 if (!decode_integer_value (base
, &tokptr
, &ival
))
1259 else if (explicit_base
&& (*tokptr
== '\''))
1266 *tokptrptr
= tokptr
;
1271 /* If it wasn't for the fact that floating point values can contain '_'
1272 characters, we could just let strtod do all the hard work by letting it
1273 try to consume as much of the current token buffer as possible and
1274 find a legal conversion. Unfortunately we need to filter out the '_'
1275 characters before calling strtod, which we do by copying the other
1276 legal chars to a local buffer to be converted. However since we also
1277 need to keep track of where the last unconsumed character in the input
1278 buffer is, we have transfer only as many characters as may compose a
1279 legal floating point value. */
1281 static enum ch_terminal
1282 match_float_literal ()
1284 char *tokptr
= lexptr
;
1288 extern double strtod ();
1290 /* Make local buffer in which to build the string to convert. This is
1291 required because underscores are valid in chill floating point numbers
1292 but not in the string passed to strtod to convert. The string will be
1293 no longer than our input string. */
1295 copy
= buf
= (char *) alloca (strlen (tokptr
) + 1);
1297 /* Transfer all leading digits to the conversion buffer, discarding any
1300 while (isdigit (*tokptr
) || *tokptr
== '_')
1309 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1310 of whether we found any leading digits, and we simply accept it and
1311 continue on to look for the fractional part and/or exponent. One of
1312 [eEdD] is legal only if we have seen digits, and means that there
1313 is no fractional part. If we find neither of these, then this is
1314 not a floating point number, so return failure. */
1319 /* Accept and then look for fractional part and/or exponent. */
1332 goto collect_exponent
;
1340 /* We found a '.', copy any fractional digits to the conversion buffer, up
1341 to the first nondigit, non-underscore character. */
1343 while (isdigit (*tokptr
) || *tokptr
== '_')
1352 /* Look for an exponent, which must start with one of [eEdD]. If none
1353 is found, jump directly to trying to convert what we have collected
1370 /* Accept an optional '-' or '+' following one of [eEdD]. */
1373 if (*tokptr
== '+' || *tokptr
== '-')
1375 *copy
++ = *tokptr
++;
1378 /* Now copy an exponent into the conversion buffer. Note that at the
1379 moment underscores are *not* allowed in exponents. */
1381 while (isdigit (*tokptr
))
1383 *copy
++ = *tokptr
++;
1386 /* If we transfered any chars to the conversion buffer, try to interpret its
1387 contents as a floating point value. If any characters remain, then we
1388 must not have a valid floating point string. */
1394 dval
= strtod (buf
, ©
);
1399 return (FLOAT_LITERAL
);
1405 /* Recognize a string literal. A string literal is a sequence
1406 of characters enclosed in matching single or double quotes, except that
1407 a single character inside single quotes is a character literal, which
1408 we reject as a string literal. To embed the terminator character inside
1409 a string, it is simply doubled (I.E. "this""is""one""string") */
1411 static enum ch_terminal
1412 match_string_literal ()
1414 char *tokptr
= lexptr
;
1416 for (tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1419 if (*tokptr
== *lexptr
)
1421 if (*(tokptr
+ 1) == *lexptr
)
1430 tempbuf
[tempbufindex
++] = *tokptr
;
1432 if (*tokptr
== '\0' /* no terminator */
1433 || (tempbufindex
== 1 && *tokptr
== '\'')) /* char literal */
1439 tempbuf
[tempbufindex
] = '\0';
1440 yylval
.sval
.ptr
= tempbuf
;
1441 yylval
.sval
.length
= tempbufindex
;
1443 return (CHARACTER_STRING_LITERAL
);
1447 /* Recognize a character literal. A character literal is single character
1448 or a control sequence, enclosed in single quotes. A control sequence
1449 is a comma separated list of one or more integer literals, enclosed
1450 in parenthesis and introduced with a circumflex character.
1452 EX: 'a' '^(7)' '^(7,8)'
1454 As a GNU chill extension, the syntax C'xx' is also recognized as a
1455 character literal, where xx is a hex value for the character.
1457 Note that more than a single character, enclosed in single quotes, is
1460 Also note that the control sequence form is not in GNU Chill since it
1461 is ambiguous with the string literal form using single quotes. I.E.
1462 is '^(7)' a character literal or a string literal. In theory it it
1463 possible to tell by context, but GNU Chill doesn't accept the control
1464 sequence form, so neither do we (for now the code is disabled).
1466 Returns CHARACTER_LITERAL if a match is found.
1469 static enum ch_terminal
1470 match_character_literal ()
1472 char *tokptr
= lexptr
;
1475 if ((*tokptr
== 'c' || *tokptr
== 'C') && (*(tokptr
+ 1) == '\''))
1477 /* We have a GNU chill extension form, so skip the leading "C'",
1478 decode the hex value, and then ensure that we have a trailing
1479 single quote character. */
1481 if (!decode_integer_value (16, &tokptr
, &ival
) || (*tokptr
!= '\''))
1487 else if (*tokptr
== '\'')
1491 /* Determine which form we have, either a control sequence or the
1492 single character form. */
1494 if ((*tokptr
== '^') && (*(tokptr
+ 1) == '('))
1496 #if 0 /* Disable, see note above. -fnf */
1497 /* Match and decode a control sequence. Return zero if we don't
1498 find a valid integer literal, or if the next unconsumed character
1499 after the integer literal is not the trailing ')'.
1500 FIXME: We currently don't handle the multiple integer literal
1503 if (!decode_integer_literal (&ival
, &tokptr
) || (*tokptr
++ != ')'))
1516 /* The trailing quote has not yet been consumed. If we don't find
1517 it, then we have no match. */
1519 if (*tokptr
++ != '\'')
1526 /* Not a character literal. */
1529 yylval
.typed_val
.val
= ival
;
1530 yylval
.typed_val
.type
= builtin_type_chill_char
;
1532 return (CHARACTER_LITERAL
);
1535 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1536 Note that according to 5.2.4.2, a single "_" is also a valid integer
1537 literal, however GNU-chill requires there to be at least one "digit"
1538 in any integer literal. */
1540 static enum ch_terminal
1541 match_integer_literal ()
1543 char *tokptr
= lexptr
;
1546 if (!decode_integer_literal (&ival
, &tokptr
))
1552 yylval
.typed_val
.val
= ival
;
1553 #if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
1554 if (ival
> (LONGEST
)2147483647U || ival
< -(LONGEST
)2147483648U)
1555 yylval
.typed_val
.type
= builtin_type_long_long
;
1558 yylval
.typed_val
.type
= builtin_type_int
;
1560 return (INTEGER_LITERAL
);
1564 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1565 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1566 literal, however GNU-chill requires there to be at least one "digit"
1567 in any bit-string literal. */
1569 static enum ch_terminal
1570 match_bitstring_literal ()
1572 register char *tokptr
= lexptr
;
1582 /* Look for the required explicit base specifier. */
1603 /* Ensure that the character after the explicit base is a single quote. */
1605 if (*tokptr
++ != '\'')
1610 while (*tokptr
!= '\0' && *tokptr
!= '\'')
1613 if (isupper (digit
))
1614 digit
= tolower (digit
);
1620 case '0': case '1': case '2': case '3': case '4':
1621 case '5': case '6': case '7': case '8': case '9':
1624 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1629 error ("Invalid character in bitstring or integer.");
1631 if (digit
>= 1 << bits_per_char
)
1633 /* Found something not in domain for current base. */
1634 error ("Too-large digit in bitstring or integer.");
1638 /* Extract bits from digit, packing them into the bitstring byte. */
1639 int k
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? bits_per_char
- 1 : 0;
1640 for (; TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
>= 0 : k
< bits_per_char
;
1641 TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
-- : k
++)
1644 if (digit
& (1 << k
))
1646 tempbuf
[tempbufindex
] |=
1647 (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1648 ? (1 << (HOST_CHAR_BIT
- 1 - bitoffset
))
1652 if (bitoffset
== HOST_CHAR_BIT
)
1657 tempbuf
[tempbufindex
] = 0;
1663 /* Verify that we consumed everything up to the trailing single quote,
1664 and that we found some bits (IE not just underbars). */
1666 if (*tokptr
++ != '\'')
1672 yylval
.sval
.ptr
= tempbuf
;
1673 yylval
.sval
.length
= bitcount
;
1675 return (BIT_STRING_LITERAL
);
1685 static const struct token idtokentab
[] =
1688 { "length", LENGTH
},
1699 { "max", MAX_TOKEN
},
1700 { "min", MIN_TOKEN
},
1709 { "addr", ADDR_TOKEN
},
1710 { "null", EMPTINESS_LITERAL
}
1713 static const struct token tokentab2
[] =
1715 { ":=", GDB_ASSIGNMENT
},
1716 { "//", SLASH_SLASH
},
1723 /* Read one token, getting characters through lexptr. */
1724 /* This is where we will check to make sure that the language and the
1725 operators used are compatible. */
1727 static enum ch_terminal
1731 enum ch_terminal token
;
1735 /* Skip over any leading whitespace. */
1736 while (isspace (*lexptr
))
1740 /* Look for special single character cases which can't be the first
1741 character of some other multicharacter token. */
1758 /* Look for characters which start a particular kind of multicharacter
1759 token, such as a character literal, register name, convenience
1760 variable name, string literal, etc. */
1765 /* First try to match a string literal, which is any
1766 sequence of characters enclosed in matching single or double
1767 quotes, except that a single character inside single quotes
1768 is a character literal, so we have to catch that case also. */
1769 token
= match_string_literal ();
1774 if (*lexptr
== '\'')
1776 token
= match_character_literal ();
1785 token
= match_character_literal ();
1792 yylval
.sval
.ptr
= lexptr
;
1795 } while (isalnum (*lexptr
) || *lexptr
== '_' || *lexptr
== '$');
1796 yylval
.sval
.length
= lexptr
- yylval
.sval
.ptr
;
1797 write_dollar_variable (yylval
.sval
);
1798 return GDB_VARIABLE
;
1801 /* See if it is a special token of length 2. */
1802 for (i
= 0; i
< sizeof (tokentab2
) / sizeof (tokentab2
[0]); i
++)
1804 if (STREQN (lexptr
, tokentab2
[i
].operator, 2))
1807 return (tokentab2
[i
].token
);
1810 /* Look for single character cases which which could be the first
1811 character of some other multicharacter token, but aren't, or we
1812 would already have found it. */
1822 /* Look for a float literal before looking for an integer literal, so
1823 we match as much of the input stream as possible. */
1824 token
= match_float_literal ();
1829 token
= match_bitstring_literal ();
1834 token
= match_integer_literal ();
1840 /* Try to match a simple name string, and if a match is found, then
1841 further classify what sort of name it is and return an appropriate
1842 token. Note that attempting to match a simple name string consumes
1843 the token from lexptr, so we can't back out if we later find that
1844 we can't classify what sort of name it is. */
1846 inputname
= match_simple_name_string ();
1848 if (inputname
!= NULL
)
1850 char *simplename
= (char*) alloca (strlen (inputname
) + 1);
1852 char *dptr
= simplename
, *sptr
= inputname
;
1853 for (; *sptr
; sptr
++)
1854 *dptr
++ = isupper (*sptr
) ? tolower(*sptr
) : *sptr
;
1857 /* See if it is a reserved identifier. */
1858 for (i
= 0; i
< sizeof (idtokentab
) / sizeof (idtokentab
[0]); i
++)
1860 if (STREQ (simplename
, idtokentab
[i
].operator))
1862 return (idtokentab
[i
].token
);
1866 /* Look for other special tokens. */
1867 if (STREQ (simplename
, "true"))
1870 return (BOOLEAN_LITERAL
);
1872 if (STREQ (simplename
, "false"))
1875 return (BOOLEAN_LITERAL
);
1878 sym
= lookup_symbol (inputname
, expression_context_block
,
1879 VAR_NAMESPACE
, (int *) NULL
,
1880 (struct symtab
**) NULL
);
1881 if (sym
== NULL
&& strcmp (inputname
, simplename
) != 0)
1883 sym
= lookup_symbol (simplename
, expression_context_block
,
1884 VAR_NAMESPACE
, (int *) NULL
,
1885 (struct symtab
**) NULL
);
1889 yylval
.ssym
.stoken
.ptr
= NULL
;
1890 yylval
.ssym
.stoken
.length
= 0;
1891 yylval
.ssym
.sym
= sym
;
1892 yylval
.ssym
.is_a_field_of_this
= 0; /* FIXME, C++'ism */
1893 switch (SYMBOL_CLASS (sym
))
1896 /* Found a procedure name. */
1897 return (GENERAL_PROCEDURE_NAME
);
1899 /* Found a global or local static variable. */
1900 return (LOCATION_NAME
);
1905 case LOC_REGPARM_ADDR
:
1909 case LOC_BASEREG_ARG
:
1910 if (innermost_block
== NULL
1911 || contained_in (block_found
, innermost_block
))
1913 innermost_block
= block_found
;
1915 return (LOCATION_NAME
);
1919 return (LOCATION_NAME
);
1922 yylval
.tsym
.type
= SYMBOL_TYPE (sym
);
1925 case LOC_CONST_BYTES
:
1926 case LOC_OPTIMIZED_OUT
:
1927 error ("Symbol \"%s\" names no location.", inputname
);
1931 else if (!have_full_symbols () && !have_partial_symbols ())
1933 error ("No symbol table is loaded. Use the \"file\" command.");
1937 error ("No symbol \"%s\" in current context.", inputname
);
1941 /* Catch single character tokens which are not part of some
1946 case '.': /* Not float for example. */
1948 while (isspace (*lexptr
)) lexptr
++;
1949 inputname
= match_simple_name_string ();
1955 return (ILLEGAL_TOKEN
);
1959 write_lower_upper_value (opcode
, type
)
1960 enum exp_opcode opcode
; /* Either UNOP_LOWER or UNOP_UPPER */
1964 write_exp_elt_opcode (opcode
);
1967 extern LONGEST
type_lower_upper ();
1968 struct type
*result_type
;
1969 LONGEST val
= type_lower_upper (opcode
, type
, &result_type
);
1970 write_exp_elt_opcode (OP_LONG
);
1971 write_exp_elt_type (result_type
);
1972 write_exp_elt_longcst (val
);
1973 write_exp_elt_opcode (OP_LONG
);