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,
19 Boston, MA 02111-1307, USA. */
21 /* Parse a Chill expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that the language accepted by this parser is more liberal
31 than the one accepted by an actual Chill compiler. For example, the
32 language rule that a simple name string can not be one of the reserved
33 simple name strings is not enforced (e.g "case" is not treated as a
34 reserved name). Another example is that Chill is a strongly typed
35 language, and certain expressions that violate the type constraints
36 may still be evaluated if gdb can do so in a meaningful manner, while
37 such expressions would be rejected by the compiler. The reason for
38 this more liberal behavior is the philosophy that the debugger
39 is intended to be a tool that is used by the programmer when things
40 go wrong, and as such, it should provide as few artificial barriers
41 to it's use as possible. If it can do something meaningful, even
42 something that violates language contraints that are enforced by the
43 compiler, it should do so without complaint.
48 #include "gdb_string.h"
50 #include "expression.h"
53 #include "parser-defs.h"
55 #include "bfd.h" /* Required by objfiles.h. */
56 #include "symfile.h" /* Required by objfiles.h. */
57 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
60 #define INLINE __inline__
86 /* '\001' ... '\xff' come first. */
93 GENERAL_PROCEDURE_NAME
,
96 CHARACTER_STRING_LITERAL
,
99 DOT_FIELD_NAME
, /* '.' followed by <field name> */
142 /* Forward declarations. */
144 static void write_lower_upper_value
PARAMS ((enum exp_opcode
, struct type
*));
145 static enum ch_terminal match_bitstring_literal
PARAMS ((void));
146 static enum ch_terminal match_integer_literal
PARAMS ((void));
147 static enum ch_terminal match_character_literal
PARAMS ((void));
148 static enum ch_terminal match_string_literal
PARAMS ((void));
149 static enum ch_terminal match_float_literal
PARAMS ((void));
150 static enum ch_terminal match_float_literal
PARAMS ((void));
151 static int decode_integer_literal
PARAMS ((LONGEST
*, char **));
152 static int decode_integer_value
PARAMS ((int, char **, LONGEST
*));
153 static char *match_simple_name_string
PARAMS ((void));
154 static void growbuf_by_size
PARAMS ((int));
155 static void parse_untyped_expr
PARAMS ((void));
156 static void parse_if_expression
PARAMS ((void));
157 static void parse_else_alternative
PARAMS ((void));
158 static void parse_then_alternative
PARAMS ((void));
159 static void parse_expr
PARAMS ((void));
160 static void parse_operand0
PARAMS ((void));
161 static void parse_operand1
PARAMS ((void));
162 static void parse_operand2
PARAMS ((void));
163 static void parse_operand3
PARAMS ((void));
164 static void parse_operand4
PARAMS ((void));
165 static void parse_operand5
PARAMS ((void));
166 static void parse_operand6
PARAMS ((void));
167 static void parse_primval
PARAMS ((void));
168 static void parse_tuple
PARAMS ((struct type
*));
169 static void parse_opt_element_list
PARAMS ((struct type
*));
170 static void parse_tuple_element
PARAMS ((struct type
*));
171 static void parse_named_record_element
PARAMS ((void));
172 static void parse_call
PARAMS ((void));
173 static struct type
*parse_mode_or_normal_call
PARAMS ((void));
175 static struct type
*parse_mode_call
PARAMS ((void));
177 static void parse_unary_call
PARAMS ((void));
178 static int parse_opt_untyped_expr
PARAMS ((void));
179 static void parse_case_label
PARAMS ((void));
180 static int expect
PARAMS ((enum ch_terminal
, char *));
181 static void parse_expr
PARAMS ((void));
182 static void parse_primval
PARAMS ((void));
183 static void parse_untyped_expr
PARAMS ((void));
184 static int parse_opt_untyped_expr
PARAMS ((void));
185 static void parse_if_expression_body
PARAMS ((void));
186 static enum ch_terminal ch_lex
PARAMS ((void));
187 INLINE
static enum ch_terminal PEEK_TOKEN
PARAMS ((void));
188 static enum ch_terminal peek_token_
PARAMS ((int));
189 static void forward_token_
PARAMS ((void));
190 static void require
PARAMS ((enum ch_terminal
));
191 static int check_token
PARAMS ((enum ch_terminal
));
193 #define MAX_LOOK_AHEAD 2
194 static enum ch_terminal terminal_buffer
[MAX_LOOK_AHEAD
+ 1] =
196 TOKEN_NOT_READ
, TOKEN_NOT_READ
, TOKEN_NOT_READ
};
197 static YYSTYPE yylval
;
198 static YYSTYPE val_buffer
[MAX_LOOK_AHEAD
+ 1];
200 /*int current_token, lookahead_token; */
202 INLINE
static enum ch_terminal
205 if (terminal_buffer
[0] == TOKEN_NOT_READ
)
207 terminal_buffer
[0] = ch_lex ();
208 val_buffer
[0] = yylval
;
210 return terminal_buffer
[0];
212 #define PEEK_LVAL() val_buffer[0]
213 #define PEEK_TOKEN1() peek_token_(1)
214 #define PEEK_TOKEN2() peek_token_(2)
215 static enum ch_terminal
219 if (i
> MAX_LOOK_AHEAD
)
220 fatal ("internal error - too much lookahead");
221 if (terminal_buffer
[i
] == TOKEN_NOT_READ
)
223 terminal_buffer
[i
] = ch_lex ();
224 val_buffer
[i
] = yylval
;
226 return terminal_buffer
[i
];
232 pushback_token (code
, node
)
233 enum ch_terminal code
;
237 if (terminal_buffer
[MAX_LOOK_AHEAD
] != TOKEN_NOT_READ
)
238 fatal ("internal error - cannot pushback token");
239 for (i
= MAX_LOOK_AHEAD
; i
> 0; i
--)
241 terminal_buffer
[i
] = terminal_buffer
[i
- 1];
242 val_buffer
[i
] = val_buffer
[i
- 1];
244 terminal_buffer
[0] = code
;
245 val_buffer
[0] = node
;
254 for (i
= 0; i
< MAX_LOOK_AHEAD
; i
++)
256 terminal_buffer
[i
] = terminal_buffer
[i
+ 1];
257 val_buffer
[i
] = val_buffer
[i
+ 1];
259 terminal_buffer
[MAX_LOOK_AHEAD
] = TOKEN_NOT_READ
;
261 #define FORWARD_TOKEN() forward_token_()
263 /* Skip the next token.
264 if it isn't TOKEN, the parser is broken. */
268 enum ch_terminal token
;
270 if (PEEK_TOKEN () != token
)
273 sprintf (buf
, "internal parser error - expected token %d", (int) token
);
281 enum ch_terminal token
;
283 if (PEEK_TOKEN () != token
)
289 /* return 0 if expected token was not found,
293 expect (token
, message
)
294 enum ch_terminal token
;
297 if (PEEK_TOKEN () != token
)
301 else if (token
< 256)
302 error ("syntax error - expected a '%c' here \"%s\"", token
, lexptr
);
304 error ("syntax error");
314 parse_opt_name_string (allow_all
)
315 int allow_all
; /* 1 if ALL is allowed as a postfix */
317 int token
= PEEK_TOKEN ();
321 if (token
== ALL
&& allow_all
)
332 token
= PEEK_TOKEN ();
336 token
= PEEK_TOKEN ();
337 if (token
== ALL
&& allow_all
)
338 return get_identifier3 (IDENTIFIER_POINTER (name
), "!", "*");
342 error ("'%s!' is not followed by an identifier",
343 IDENTIFIER_POINTER (name
));
346 name
= get_identifier3 (IDENTIFIER_POINTER (name
),
347 "!", IDENTIFIER_POINTER (PEEK_LVAL ()));
352 parse_simple_name_string ()
354 int token
= PEEK_TOKEN ();
358 error ("expected a name here");
359 return error_mark_node
;
369 tree name
= parse_opt_name_string (0);
373 error ("expected a name string here");
374 return error_mark_node
;
377 /* Matches: <name_string>
378 Returns if pass 1: the identifier.
379 Returns if pass 2: a decl or value for identifier. */
384 tree name
= parse_name_string ();
385 if (pass
== 1 || ignoring
)
389 tree decl
= lookup_name (name
);
390 if (decl
== NULL_TREE
)
392 error ("`%s' undeclared", IDENTIFIER_POINTER (name
));
393 return error_mark_node
;
395 else if (TREE_CODE (TREE_TYPE (decl
)) == ERROR_MARK
)
396 return error_mark_node
;
397 else if (TREE_CODE (decl
) == CONST_DECL
)
398 return DECL_INITIAL (decl
);
399 else if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
400 return convert_from_reference (decl
);
409 pushback_paren_expr (expr
)
412 if (pass
== 1 && !ignoring
)
413 expr
= build1 (PAREN_EXPR
, NULL_TREE
, expr
);
414 pushback_token (EXPR
, expr
);
418 /* Matches: <case label> */
423 if (check_token (ELSE
))
424 error ("ELSE in tuples labels not implemented");
425 /* Does not handle the case of a mode name. FIXME */
427 if (check_token (':'))
430 write_exp_elt_opcode (BINOP_RANGE
);
435 parse_opt_untyped_expr ()
437 switch (PEEK_TOKEN ())
444 parse_untyped_expr ();
458 /* Parse NAME '(' MODENAME ')'. */
468 if (PEEK_TOKEN () != TYPENAME
)
469 error ("expect MODENAME here `%s'", lexptr
);
470 type
= PEEK_LVAL ().tsym
.type
;
479 parse_mode_or_normal_call ()
484 if (PEEK_TOKEN () == TYPENAME
)
486 type
= PEEK_LVAL ().tsym
.type
;
498 /* Parse something that looks like a function call.
499 Assume we have parsed the function, and are at the '('. */
506 /* This is to save the value of arglist_len
507 being accumulated for each dimension. */
509 if (parse_opt_untyped_expr ())
511 int tok
= PEEK_TOKEN ();
513 if (tok
== UP
|| tok
== ':')
517 expect (')', "expected ')' to terminate slice");
519 write_exp_elt_opcode (tok
== UP
? TERNOP_SLICE_COUNT
523 while (check_token (','))
525 parse_untyped_expr ();
532 arg_count
= end_arglist ();
533 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
534 write_exp_elt_longcst (arg_count
);
535 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
539 parse_named_record_element ()
544 label
= PEEK_LVAL ().sval
;
545 sprintf (buf
, "expected a field name here `%s'", lexptr
);
546 expect (DOT_FIELD_NAME
, buf
);
547 if (check_token (','))
548 parse_named_record_element ();
549 else if (check_token (':'))
552 error ("syntax error near `%s' in named record tuple element", lexptr
);
553 write_exp_elt_opcode (OP_LABELED
);
554 write_exp_string (label
);
555 write_exp_elt_opcode (OP_LABELED
);
558 /* Returns one or more TREE_LIST nodes, in reverse order. */
561 parse_tuple_element (type
)
564 if (PEEK_TOKEN () == DOT_FIELD_NAME
)
566 /* Parse a labelled structure tuple. */
567 parse_named_record_element ();
571 if (check_token ('('))
573 if (check_token ('*'))
575 expect (')', "missing ')' after '*' case label list");
578 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
580 /* do this as a range from low to high */
581 struct type
*range_type
= TYPE_FIELD_TYPE (type
, 0);
582 LONGEST low_bound
, high_bound
;
583 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
584 error ("cannot determine bounds for (*)");
586 write_exp_elt_opcode (OP_LONG
);
587 write_exp_elt_type (range_type
);
588 write_exp_elt_longcst (low_bound
);
589 write_exp_elt_opcode (OP_LONG
);
591 write_exp_elt_opcode (OP_LONG
);
592 write_exp_elt_type (range_type
);
593 write_exp_elt_longcst (high_bound
);
594 write_exp_elt_opcode (OP_LONG
);
595 write_exp_elt_opcode (BINOP_RANGE
);
598 error ("(*) in invalid context");
601 error ("(*) only possible with modename in front of tuple (mode[..])");
606 while (check_token (','))
609 write_exp_elt_opcode (BINOP_COMMA
);
615 parse_untyped_expr ();
616 if (check_token (':'))
618 /* A powerset range or a labeled Array. */
619 parse_untyped_expr ();
620 write_exp_elt_opcode (BINOP_RANGE
);
624 /* Matches: a COMMA-separated list of tuple elements.
625 Returns a list (of TREE_LIST nodes). */
627 parse_opt_element_list (type
)
631 if (PEEK_TOKEN () == ']')
635 parse_tuple_element (type
);
637 if (PEEK_TOKEN () == ']')
639 if (!check_token (','))
640 error ("bad syntax in tuple");
644 /* Parses: '[' elements ']'
645 If modename is non-NULL it prefixed the tuple. */
653 type
= check_typedef (mode
);
658 parse_opt_element_list (type
);
659 expect (']', "missing ']' after tuple");
660 write_exp_elt_opcode (OP_ARRAY
);
661 write_exp_elt_longcst ((LONGEST
) 0);
662 write_exp_elt_longcst ((LONGEST
) end_arglist () - 1);
663 write_exp_elt_opcode (OP_ARRAY
);
666 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
667 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
668 && TYPE_CODE (type
) != TYPE_CODE_SET
)
669 error ("invalid tuple mode");
670 write_exp_elt_opcode (UNOP_CAST
);
671 write_exp_elt_type (mode
);
672 write_exp_elt_opcode (UNOP_CAST
);
682 switch (PEEK_TOKEN ())
684 case INTEGER_LITERAL
:
685 case CHARACTER_LITERAL
:
686 write_exp_elt_opcode (OP_LONG
);
687 write_exp_elt_type (PEEK_LVAL ().typed_val
.type
);
688 write_exp_elt_longcst (PEEK_LVAL ().typed_val
.val
);
689 write_exp_elt_opcode (OP_LONG
);
692 case BOOLEAN_LITERAL
:
693 write_exp_elt_opcode (OP_BOOL
);
694 write_exp_elt_longcst ((LONGEST
) PEEK_LVAL ().ulval
);
695 write_exp_elt_opcode (OP_BOOL
);
699 write_exp_elt_opcode (OP_DOUBLE
);
700 write_exp_elt_type (builtin_type_double
);
701 write_exp_elt_dblcst (PEEK_LVAL ().dval
);
702 write_exp_elt_opcode (OP_DOUBLE
);
705 case EMPTINESS_LITERAL
:
706 write_exp_elt_opcode (OP_LONG
);
707 write_exp_elt_type (lookup_pointer_type (builtin_type_void
));
708 write_exp_elt_longcst (0);
709 write_exp_elt_opcode (OP_LONG
);
712 case CHARACTER_STRING_LITERAL
:
713 write_exp_elt_opcode (OP_STRING
);
714 write_exp_string (PEEK_LVAL ().sval
);
715 write_exp_elt_opcode (OP_STRING
);
718 case BIT_STRING_LITERAL
:
719 write_exp_elt_opcode (OP_BITSTRING
);
720 write_exp_bitstring (PEEK_LVAL ().sval
);
721 write_exp_elt_opcode (OP_BITSTRING
);
726 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
727 which casts to an artificial array. */
730 if (PEEK_TOKEN () != TYPENAME
)
731 error ("missing MODENAME after ARRAY()");
732 type
= PEEK_LVAL ().tsym
.type
;
736 expect (')', "missing right parenthesis");
737 type
= create_array_type ((struct type
*) NULL
, type
,
738 create_range_type ((struct type
*) NULL
,
739 builtin_type_int
, 0, 0));
740 TYPE_ARRAY_UPPER_BOUND_TYPE (type
) = BOUND_CANNOT_BE_DETERMINED
;
741 write_exp_elt_opcode (UNOP_CAST
);
742 write_exp_elt_type (type
);
743 write_exp_elt_opcode (UNOP_CAST
);
755 expect (')', "missing right parenthesis");
760 case GENERAL_PROCEDURE_NAME
:
762 write_exp_elt_opcode (OP_VAR_VALUE
);
763 write_exp_elt_block (NULL
);
764 write_exp_elt_sym (PEEK_LVAL ().ssym
.sym
);
765 write_exp_elt_opcode (OP_VAR_VALUE
);
768 case GDB_VARIABLE
: /* gdb specific */
773 write_exp_elt_opcode (UNOP_CAST
);
774 write_exp_elt_type (builtin_type_int
);
775 write_exp_elt_opcode (UNOP_CAST
);
779 write_exp_elt_opcode (UNOP_CARD
);
783 write_exp_elt_opcode (UNOP_CHMAX
);
787 write_exp_elt_opcode (UNOP_CHMIN
);
791 goto unimplemented_unary_builtin
;
794 goto unimplemented_unary_builtin
;
797 goto unimplemented_unary_builtin
;
798 unimplemented_unary_builtin
:
800 error ("not implemented: %s builtin function", op_name
);
804 write_exp_elt_opcode (UNOP_ADDR
);
807 type
= parse_mode_or_normal_call ();
810 write_exp_elt_opcode (OP_LONG
);
811 write_exp_elt_type (builtin_type_int
);
812 CHECK_TYPEDEF (type
);
813 write_exp_elt_longcst ((LONGEST
) TYPE_LENGTH (type
));
814 write_exp_elt_opcode (OP_LONG
);
817 write_exp_elt_opcode (UNOP_SIZEOF
);
826 type
= parse_mode_or_normal_call ();
827 write_lower_upper_value (op
, type
);
831 write_exp_elt_opcode (UNOP_LENGTH
);
834 type
= PEEK_LVAL ().tsym
.type
;
836 switch (PEEK_TOKEN ())
844 expect (')', "missing right parenthesis");
845 write_exp_elt_opcode (UNOP_CAST
);
846 write_exp_elt_type (type
);
847 write_exp_elt_opcode (UNOP_CAST
);
850 error ("typename in invalid context");
855 error ("invalid expression syntax at `%s'", lexptr
);
859 switch (PEEK_TOKEN ())
862 write_exp_elt_opcode (STRUCTOP_STRUCT
);
863 write_exp_string (PEEK_LVAL ().sval
);
864 write_exp_elt_opcode (STRUCTOP_STRUCT
);
869 if (PEEK_TOKEN () == TYPENAME
)
871 type
= PEEK_LVAL ().tsym
.type
;
872 write_exp_elt_opcode (UNOP_CAST
);
873 write_exp_elt_type (lookup_pointer_type (type
));
874 write_exp_elt_opcode (UNOP_CAST
);
877 write_exp_elt_opcode (UNOP_IND
);
882 case CHARACTER_STRING_LITERAL
:
883 case CHARACTER_LITERAL
:
884 case BIT_STRING_LITERAL
:
885 /* Handle string repetition. (See comment in parse_operand5.) */
887 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
888 write_exp_elt_longcst (1);
889 write_exp_elt_opcode (MULTI_SUBSCRIPT
);
893 case INTEGER_LITERAL
:
894 case BOOLEAN_LITERAL
:
896 case GENERAL_PROCEDURE_NAME
:
898 case EMPTINESS_LITERAL
:
949 if (check_token (RECEIVE
))
952 error ("not implemented: RECEIVE expression");
954 else if (check_token (POINTER
))
957 write_exp_elt_opcode (UNOP_ADDR
);
967 /* We are supposed to be looking for a <string repetition operator>,
968 but in general we can't distinguish that from a parenthesized
969 expression. This is especially difficult if we allow the
970 string operand to be a constant expression (as requested by
971 some users), and not just a string literal.
972 Consider: LPRN expr RPRN LPRN expr RPRN
973 Is that a function call or string repetition?
974 Instead, we handle string repetition in parse_primval,
975 and build_generalized_call. */
976 switch (PEEK_TOKEN ())
979 op
= UNOP_LOGICAL_NOT
;
991 write_exp_elt_opcode (op
);
1001 switch (PEEK_TOKEN ())
1020 write_exp_elt_opcode (op
);
1031 switch (PEEK_TOKEN ())
1047 write_exp_elt_opcode (op
);
1058 if (check_token (IN
))
1061 write_exp_elt_opcode (BINOP_IN
);
1065 switch (PEEK_TOKEN ())
1083 op
= BINOP_NOTEQUAL
;
1090 write_exp_elt_opcode (op
);
1102 switch (PEEK_TOKEN ())
1105 op
= BINOP_BITWISE_AND
;
1108 op
= BINOP_LOGICAL_AND
;
1115 write_exp_elt_opcode (op
);
1126 switch (PEEK_TOKEN ())
1129 op
= BINOP_BITWISE_IOR
;
1132 op
= BINOP_BITWISE_XOR
;
1135 op
= BINOP_LOGICAL_OR
;
1142 write_exp_elt_opcode (op
);
1150 if (check_token (GDB_ASSIGNMENT
))
1153 write_exp_elt_opcode (BINOP_ASSIGN
);
1158 parse_then_alternative ()
1160 expect (THEN
, "missing 'THEN' in 'IF' expression");
1165 parse_else_alternative ()
1167 if (check_token (ELSIF
))
1168 parse_if_expression_body ();
1169 else if (check_token (ELSE
))
1172 error ("missing ELSE/ELSIF in IF expression");
1175 /* Matches: <boolean expression> <then alternative> <else alternative> */
1178 parse_if_expression_body ()
1181 parse_then_alternative ();
1182 parse_else_alternative ();
1183 write_exp_elt_opcode (TERNOP_COND
);
1187 parse_if_expression ()
1190 parse_if_expression_body ();
1191 expect (FI
, "missing 'FI' at end of conditional expression");
1194 /* An <untyped_expr> is a superset of <expr>. It also includes
1195 <conditional expressions> and untyped <tuples>, whose types
1196 are not given by their constituents. Hence, these are only
1197 allowed in certain contexts that expect a certain type.
1198 You should call convert() to fix up the <untyped_expr>. */
1201 parse_untyped_expr ()
1203 switch (PEEK_TOKEN ())
1206 parse_if_expression ();
1209 error ("not implemented: CASE expression");
1211 switch (PEEK_TOKEN1 ())
1219 parse_untyped_expr ();
1220 expect (')', "missing ')'");
1233 terminal_buffer
[0] = TOKEN_NOT_READ
;
1234 if (PEEK_TOKEN () == TYPENAME
&& PEEK_TOKEN1 () == END_TOKEN
)
1236 write_exp_elt_opcode (OP_TYPE
);
1237 write_exp_elt_type (PEEK_LVAL ().tsym
.type
);
1238 write_exp_elt_opcode (OP_TYPE
);
1243 if (terminal_buffer
[0] != END_TOKEN
)
1245 if (comma_terminates
&& terminal_buffer
[0] == ',')
1246 lexptr
--; /* Put the comma back. */
1248 error ("Junk after end of expression.");
1254 /* Implementation of a dynamically expandable buffer for processing input
1255 characters acquired through lexptr and building a value to return in
1258 static char *tempbuf
; /* Current buffer contents */
1259 static int tempbufsize
; /* Size of allocated buffer */
1260 static int tempbufindex
; /* Current index into buffer */
1262 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1264 #define CHECKBUF(size) \
1266 if (tempbufindex + (size) >= tempbufsize) \
1268 growbuf_by_size (size); \
1272 /* Grow the static temp buffer if necessary, including allocating the first one
1276 growbuf_by_size (count
)
1281 growby
= max (count
, GROWBY_MIN_SIZE
);
1282 tempbufsize
+= growby
;
1283 if (tempbuf
== NULL
)
1285 tempbuf
= (char *) xmalloc (tempbufsize
);
1289 tempbuf
= (char *) xrealloc (tempbuf
, tempbufsize
);
1293 /* Try to consume a simple name string token. If successful, returns
1294 a pointer to a nullbyte terminated copy of the name that can be used
1295 in symbol table lookups. If not successful, returns NULL. */
1298 match_simple_name_string ()
1300 char *tokptr
= lexptr
;
1302 if (isalpha (*tokptr
) || *tokptr
== '_')
1309 while (isalnum (*tokptr
) || (*tokptr
== '_'));
1310 yylval
.sval
.ptr
= lexptr
;
1311 yylval
.sval
.length
= tokptr
- lexptr
;
1313 result
= copy_name (yylval
.sval
);
1319 /* Start looking for a value composed of valid digits as set by the base
1320 in use. Note that '_' characters are valid anywhere, in any quantity,
1321 and are simply ignored. Since we must find at least one valid digit,
1322 or reject this token as an integer literal, we keep track of how many
1323 digits we have encountered. */
1326 decode_integer_value (base
, tokptrptr
, ivalptr
)
1331 char *tokptr
= *tokptrptr
;
1335 while (*tokptr
!= '\0')
1339 temp
= tolower (temp
);
1378 /* Found something not in domain for current base. */
1379 tokptr
--; /* Unconsume what gave us indigestion. */
1384 /* If we didn't find any digits, then we don't have a valid integer
1385 value, so reject the entire token. Otherwise, update the lexical
1386 scan pointer, and return non-zero for success. */
1394 *tokptrptr
= tokptr
;
1400 decode_integer_literal (valptr
, tokptrptr
)
1404 char *tokptr
= *tokptrptr
;
1407 int explicit_base
= 0;
1409 /* Look for an explicit base specifier, which is optional. */
1442 /* If we found an explicit base ensure that the character after the
1443 explicit base is a single quote. */
1445 if (explicit_base
&& (*tokptr
++ != '\''))
1450 /* Attempt to decode whatever follows as an integer value in the
1451 indicated base, updating the token pointer in the process and
1452 computing the value into ival. Also, if we have an explicit
1453 base, then the next character must not be a single quote, or we
1454 have a bitstring literal, so reject the entire token in this case.
1455 Otherwise, update the lexical scan pointer, and return non-zero
1458 if (!decode_integer_value (base
, &tokptr
, &ival
))
1462 else if (explicit_base
&& (*tokptr
== '\''))
1469 *tokptrptr
= tokptr
;
1474 /* If it wasn't for the fact that floating point values can contain '_'
1475 characters, we could just let strtod do all the hard work by letting it
1476 try to consume as much of the current token buffer as possible and
1477 find a legal conversion. Unfortunately we need to filter out the '_'
1478 characters before calling strtod, which we do by copying the other
1479 legal chars to a local buffer to be converted. However since we also
1480 need to keep track of where the last unconsumed character in the input
1481 buffer is, we have transfer only as many characters as may compose a
1482 legal floating point value. */
1484 static enum ch_terminal
1485 match_float_literal ()
1487 char *tokptr
= lexptr
;
1491 extern double strtod ();
1493 /* Make local buffer in which to build the string to convert. This is
1494 required because underscores are valid in chill floating point numbers
1495 but not in the string passed to strtod to convert. The string will be
1496 no longer than our input string. */
1498 copy
= buf
= (char *) alloca (strlen (tokptr
) + 1);
1500 /* Transfer all leading digits to the conversion buffer, discarding any
1503 while (isdigit (*tokptr
) || *tokptr
== '_')
1512 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1513 of whether we found any leading digits, and we simply accept it and
1514 continue on to look for the fractional part and/or exponent. One of
1515 [eEdD] is legal only if we have seen digits, and means that there
1516 is no fractional part. If we find neither of these, then this is
1517 not a floating point number, so return failure. */
1522 /* Accept and then look for fractional part and/or exponent. */
1535 goto collect_exponent
;
1543 /* We found a '.', copy any fractional digits to the conversion buffer, up
1544 to the first nondigit, non-underscore character. */
1546 while (isdigit (*tokptr
) || *tokptr
== '_')
1555 /* Look for an exponent, which must start with one of [eEdD]. If none
1556 is found, jump directly to trying to convert what we have collected
1573 /* Accept an optional '-' or '+' following one of [eEdD]. */
1576 if (*tokptr
== '+' || *tokptr
== '-')
1578 *copy
++ = *tokptr
++;
1581 /* Now copy an exponent into the conversion buffer. Note that at the
1582 moment underscores are *not* allowed in exponents. */
1584 while (isdigit (*tokptr
))
1586 *copy
++ = *tokptr
++;
1589 /* If we transfered any chars to the conversion buffer, try to interpret its
1590 contents as a floating point value. If any characters remain, then we
1591 must not have a valid floating point string. */
1597 dval
= strtod (buf
, ©
);
1602 return (FLOAT_LITERAL
);
1608 /* Recognize a string literal. A string literal is a sequence
1609 of characters enclosed in matching single or double quotes, except that
1610 a single character inside single quotes is a character literal, which
1611 we reject as a string literal. To embed the terminator character inside
1612 a string, it is simply doubled (I.E. "this""is""one""string") */
1614 static enum ch_terminal
1615 match_string_literal ()
1617 char *tokptr
= lexptr
;
1621 for (tempbufindex
= 0, tokptr
++; *tokptr
!= '\0'; tokptr
++)
1627 /* skip possible whitespaces */
1628 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1636 else if (*tokptr
!= ',')
1637 error ("Invalid control sequence");
1639 /* skip possible whitespaces */
1640 while ((*tokptr
== ' ' || *tokptr
== '\t') && *tokptr
)
1642 if (!decode_integer_literal (&ival
, &tokptr
))
1643 error ("Invalid control sequence");
1646 else if (*tokptr
== *lexptr
)
1648 if (*(tokptr
+ 1) == *lexptr
)
1657 else if (*tokptr
== '^')
1659 if (*(tokptr
+ 1) == '(')
1663 if (!decode_integer_literal (&ival
, &tokptr
))
1664 error ("Invalid control sequence");
1667 else if (*(tokptr
+ 1) == '^')
1670 error ("Invalid control sequence");
1674 tempbuf
[tempbufindex
++] = ival
;
1677 error ("Invalid control sequence");
1679 if (*tokptr
== '\0' /* no terminator */
1680 || (tempbufindex
== 1 && *tokptr
== '\'')) /* char literal */
1686 tempbuf
[tempbufindex
] = '\0';
1687 yylval
.sval
.ptr
= tempbuf
;
1688 yylval
.sval
.length
= tempbufindex
;
1690 return (CHARACTER_STRING_LITERAL
);
1694 /* Recognize a character literal. A character literal is single character
1695 or a control sequence, enclosed in single quotes. A control sequence
1696 is a comma separated list of one or more integer literals, enclosed
1697 in parenthesis and introduced with a circumflex character.
1699 EX: 'a' '^(7)' '^(7,8)'
1701 As a GNU chill extension, the syntax C'xx' is also recognized as a
1702 character literal, where xx is a hex value for the character.
1704 Note that more than a single character, enclosed in single quotes, is
1707 Returns CHARACTER_LITERAL if a match is found.
1710 static enum ch_terminal
1711 match_character_literal ()
1713 char *tokptr
= lexptr
;
1716 if ((*tokptr
== 'c' || *tokptr
== 'C') && (*(tokptr
+ 1) == '\''))
1718 /* We have a GNU chill extension form, so skip the leading "C'",
1719 decode the hex value, and then ensure that we have a trailing
1720 single quote character. */
1722 if (!decode_integer_value (16, &tokptr
, &ival
) || (*tokptr
!= '\''))
1728 else if (*tokptr
== '\'')
1732 /* Determine which form we have, either a control sequence or the
1733 single character form. */
1737 if (*(tokptr
+ 1) == '(')
1739 /* Match and decode a control sequence. Return zero if we don't
1740 find a valid integer literal, or if the next unconsumed character
1741 after the integer literal is not the trailing ')'. */
1743 if (!decode_integer_literal (&ival
, &tokptr
) || (*tokptr
++ != ')'))
1748 else if (*(tokptr
+ 1) == '^')
1755 error ("Invalid control sequence");
1757 else if (*tokptr
== '\'')
1759 /* this must be duplicated */
1768 /* The trailing quote has not yet been consumed. If we don't find
1769 it, then we have no match. */
1771 if (*tokptr
++ != '\'')
1778 /* Not a character literal. */
1781 yylval
.typed_val
.val
= ival
;
1782 yylval
.typed_val
.type
= builtin_type_chill_char
;
1784 return (CHARACTER_LITERAL
);
1787 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1788 Note that according to 5.2.4.2, a single "_" is also a valid integer
1789 literal, however GNU-chill requires there to be at least one "digit"
1790 in any integer literal. */
1792 static enum ch_terminal
1793 match_integer_literal ()
1795 char *tokptr
= lexptr
;
1798 if (!decode_integer_literal (&ival
, &tokptr
))
1804 yylval
.typed_val
.val
= ival
;
1805 #if defined(CC_HAS_LONG_LONG) && defined(__STDC__)
1806 if (ival
> (LONGEST
) 2147483647U || ival
< -(LONGEST
) 2147483648U)
1807 yylval
.typed_val
.type
= builtin_type_long_long
;
1810 yylval
.typed_val
.type
= builtin_type_int
;
1812 return (INTEGER_LITERAL
);
1816 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1817 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1818 literal, however GNU-chill requires there to be at least one "digit"
1819 in any bit-string literal. */
1821 static enum ch_terminal
1822 match_bitstring_literal ()
1824 register char *tokptr
= lexptr
;
1834 /* Look for the required explicit base specifier. */
1855 /* Ensure that the character after the explicit base is a single quote. */
1857 if (*tokptr
++ != '\'')
1862 while (*tokptr
!= '\0' && *tokptr
!= '\'')
1865 if (isupper (digit
))
1866 digit
= tolower (digit
);
1894 /* this is not a bitstring literal, probably an integer */
1897 if (digit
>= 1 << bits_per_char
)
1899 /* Found something not in domain for current base. */
1900 error ("Too-large digit in bitstring or integer.");
1904 /* Extract bits from digit, packing them into the bitstring byte. */
1905 int k
= TARGET_BYTE_ORDER
== BIG_ENDIAN
? bits_per_char
- 1 : 0;
1906 for (; TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
>= 0 : k
< bits_per_char
;
1907 TARGET_BYTE_ORDER
== BIG_ENDIAN
? k
-- : k
++)
1910 if (digit
& (1 << k
))
1912 tempbuf
[tempbufindex
] |=
1913 (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1914 ? (1 << (HOST_CHAR_BIT
- 1 - bitoffset
))
1918 if (bitoffset
== HOST_CHAR_BIT
)
1923 tempbuf
[tempbufindex
] = 0;
1929 /* Verify that we consumed everything up to the trailing single quote,
1930 and that we found some bits (IE not just underbars). */
1932 if (*tokptr
++ != '\'')
1938 yylval
.sval
.ptr
= tempbuf
;
1939 yylval
.sval
.length
= bitcount
;
1941 return (BIT_STRING_LITERAL
);
1951 static const struct token idtokentab
[] =
1975 {"addr", ADDR_TOKEN
},
1976 {"null", EMPTINESS_LITERAL
}
1979 static const struct token tokentab2
[] =
1981 {":=", GDB_ASSIGNMENT
},
1982 {"//", SLASH_SLASH
},
1989 /* Read one token, getting characters through lexptr. */
1990 /* This is where we will check to make sure that the language and the
1991 operators used are compatible. */
1993 static enum ch_terminal
1997 enum ch_terminal token
;
2001 /* Skip over any leading whitespace. */
2002 while (isspace (*lexptr
))
2006 /* Look for special single character cases which can't be the first
2007 character of some other multicharacter token. */
2024 /* Look for characters which start a particular kind of multicharacter
2025 token, such as a character literal, register name, convenience
2026 variable name, string literal, etc. */
2031 /* First try to match a string literal, which is any
2032 sequence of characters enclosed in matching single or double
2033 quotes, except that a single character inside single quotes
2034 is a character literal, so we have to catch that case also. */
2035 token
= match_string_literal ();
2040 if (*lexptr
== '\'')
2042 token
= match_character_literal ();
2051 token
= match_character_literal ();
2058 yylval
.sval
.ptr
= lexptr
;
2063 while (isalnum (*lexptr
) || *lexptr
== '_' || *lexptr
== '$');
2064 yylval
.sval
.length
= lexptr
- yylval
.sval
.ptr
;
2065 write_dollar_variable (yylval
.sval
);
2066 return GDB_VARIABLE
;
2069 /* See if it is a special token of length 2. */
2070 for (i
= 0; i
< sizeof (tokentab2
) / sizeof (tokentab2
[0]); i
++)
2072 if (STREQN (lexptr
, tokentab2
[i
].operator, 2))
2075 return (tokentab2
[i
].token
);
2078 /* Look for single character cases which which could be the first
2079 character of some other multicharacter token, but aren't, or we
2080 would already have found it. */
2090 /* Look for a float literal before looking for an integer literal, so
2091 we match as much of the input stream as possible. */
2092 token
= match_float_literal ();
2097 token
= match_bitstring_literal ();
2102 token
= match_integer_literal ();
2108 /* Try to match a simple name string, and if a match is found, then
2109 further classify what sort of name it is and return an appropriate
2110 token. Note that attempting to match a simple name string consumes
2111 the token from lexptr, so we can't back out if we later find that
2112 we can't classify what sort of name it is. */
2114 inputname
= match_simple_name_string ();
2116 if (inputname
!= NULL
)
2118 char *simplename
= (char *) alloca (strlen (inputname
) + 1);
2120 char *dptr
= simplename
, *sptr
= inputname
;
2121 for (; *sptr
; sptr
++)
2122 *dptr
++ = isupper (*sptr
) ? tolower (*sptr
) : *sptr
;
2125 /* See if it is a reserved identifier. */
2126 for (i
= 0; i
< sizeof (idtokentab
) / sizeof (idtokentab
[0]); i
++)
2128 if (STREQ (simplename
, idtokentab
[i
].operator))
2130 return (idtokentab
[i
].token
);
2134 /* Look for other special tokens. */
2135 if (STREQ (simplename
, "true"))
2138 return (BOOLEAN_LITERAL
);
2140 if (STREQ (simplename
, "false"))
2143 return (BOOLEAN_LITERAL
);
2146 sym
= lookup_symbol (inputname
, expression_context_block
,
2147 VAR_NAMESPACE
, (int *) NULL
,
2148 (struct symtab
**) NULL
);
2149 if (sym
== NULL
&& strcmp (inputname
, simplename
) != 0)
2151 sym
= lookup_symbol (simplename
, expression_context_block
,
2152 VAR_NAMESPACE
, (int *) NULL
,
2153 (struct symtab
**) NULL
);
2157 yylval
.ssym
.stoken
.ptr
= NULL
;
2158 yylval
.ssym
.stoken
.length
= 0;
2159 yylval
.ssym
.sym
= sym
;
2160 yylval
.ssym
.is_a_field_of_this
= 0; /* FIXME, C++'ism */
2161 switch (SYMBOL_CLASS (sym
))
2164 /* Found a procedure name. */
2165 return (GENERAL_PROCEDURE_NAME
);
2167 /* Found a global or local static variable. */
2168 return (LOCATION_NAME
);
2173 case LOC_REGPARM_ADDR
:
2177 case LOC_BASEREG_ARG
:
2178 if (innermost_block
== NULL
2179 || contained_in (block_found
, innermost_block
))
2181 innermost_block
= block_found
;
2183 return (LOCATION_NAME
);
2187 return (LOCATION_NAME
);
2190 yylval
.tsym
.type
= SYMBOL_TYPE (sym
);
2193 case LOC_CONST_BYTES
:
2194 case LOC_OPTIMIZED_OUT
:
2195 error ("Symbol \"%s\" names no location.", inputname
);
2197 case LOC_UNRESOLVED
:
2198 error ("unhandled SYMBOL_CLASS in ch_lex()");
2202 else if (!have_full_symbols () && !have_partial_symbols ())
2204 error ("No symbol table is loaded. Use the \"file\" command.");
2208 error ("No symbol \"%s\" in current context.", inputname
);
2212 /* Catch single character tokens which are not part of some
2217 case '.': /* Not float for example. */
2219 while (isspace (*lexptr
))
2221 inputname
= match_simple_name_string ();
2224 return DOT_FIELD_NAME
;
2227 return (ILLEGAL_TOKEN
);
2231 write_lower_upper_value (opcode
, type
)
2232 enum exp_opcode opcode
; /* Either UNOP_LOWER or UNOP_UPPER */
2236 write_exp_elt_opcode (opcode
);
2239 struct type
*result_type
;
2240 LONGEST val
= type_lower_upper (opcode
, type
, &result_type
);
2241 write_exp_elt_opcode (OP_LONG
);
2242 write_exp_elt_type (result_type
);
2243 write_exp_elt_longcst (val
);
2244 write_exp_elt_opcode (OP_LONG
);