1 /* Language lexer for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* It defines java_lex (yylex) that reads a Java ASCII source file
28 possibly containing Unicode escape sequence or utf8 encoded
29 characters and returns a token for everything found but comments,
30 white spaces and line terminators. When necessary, it also fills
31 the java_lval (yylval) union. It's implemented to be called by a
32 re-entrant parser generated by Bison.
34 The lexical analysis conforms to the Java grammar described in "The
35 Java(TM) Language Specification. J. Gosling, B. Joy, G. Steele.
36 Addison Wesley 1996" (http://java.sun.com/docs/books/jls/html/3.doc.html) */
40 #include "chartables.h"
45 /* Function declarations. */
46 static char *java_sprint_unicode (int);
47 static void java_unicode_2_utf8 (unicode_t);
48 static void java_lex_error (const char *, int);
50 static int do_java_lex (YYSTYPE *);
51 static int java_lex (YYSTYPE *);
52 static int java_is_eol (FILE *, int);
53 static tree build_wfl_node (tree);
55 static int java_parse_escape_sequence (void);
56 static int java_start_char_p (unicode_t);
57 static int java_part_char_p (unicode_t);
58 static int java_space_char_p (unicode_t);
59 static void java_parse_doc_section (int);
60 static void java_parse_end_comment (int);
61 static int java_read_char (java_lexer *);
62 static int java_get_unicode (void);
63 static int java_peek_unicode (void);
64 static void java_next_unicode (void);
65 static int java_read_unicode (java_lexer *, int *);
67 static int utf8_cmp (const unsigned char *, int, const char *);
70 java_lexer *java_new_lexer (FILE *, const char *);
72 static void error_if_numeric_overflow (tree);
76 /* This is nonzero if we have initialized `need_byteswap'. */
77 static int byteswap_init = 0;
79 /* Some versions of iconv() (e.g., glibc 2.1.3) will return UCS-2 in
80 big-endian order -- not native endian order. We handle this by
81 doing a conversion once at startup and seeing what happens. This
82 flag holds the results of this determination. */
83 static int need_byteswap = 0;
87 java_init_lex (FILE *finput, const char *encoding)
90 int java_lang_imported = 0;
93 java_lang_id = get_identifier ("java.lang");
95 inst_id = get_identifier ("inst$");
97 wpv_id = get_identifier ("write_parm_value$");
99 if (!java_lang_imported)
101 tree node = build_tree_list (build_unknown_wfl (java_lang_id),
103 read_import_dir (TREE_PURPOSE (node));
104 TREE_CHAIN (node) = ctxp->import_demand_list;
105 ctxp->import_demand_list = node;
106 java_lang_imported = 1;
112 #ifdef USE_MAPPED_LOCATION
113 wfl_operator = build_expr_wfl (NULL_TREE, input_location);
115 wfl_operator = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
120 label_id = get_identifier ("$L");
122 wfl_append = build_unknown_wfl (get_identifier ("append"));
123 if (!wfl_string_buffer)
125 build_unknown_wfl (get_identifier (flag_emit_class_files
126 ? "java.lang.StringBuffer"
127 : "gnu.gcj.runtime.StringBuffer"));
129 wfl_to_string = build_unknown_wfl (get_identifier ("toString"));
131 CPC_INITIALIZER_LIST (ctxp) = CPC_STATIC_INITIALIZER_LIST (ctxp) =
132 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = NULL_TREE;
134 memset (ctxp->modifier_ctx, 0, sizeof (ctxp->modifier_ctx));
135 ctxp->current_parsed_class = NULL;
136 ctxp->package = NULL_TREE;
140 ctxp->save_location = input_location;
142 ctxp->java_error_flag = 0;
143 ctxp->lexer = java_new_lexer (finput, encoding);
147 java_sprint_unicode (int c)
149 static char buffer [10];
150 if (c < ' ' || c >= 127)
151 sprintf (buffer, "\\u%04x", c);
160 /* Create a new lexer object. */
163 java_new_lexer (FILE *finput, const char *encoding)
165 java_lexer *lex = xmalloc (sizeof (java_lexer));
168 lex->finput = finput;
170 lex->unget_value = 0;
171 lex->next_unicode = 0;
172 lex->avail_unicode = 0;
173 lex->next_columns = 1;
174 lex->encoding = encoding;
175 lex->position.line = 1;
176 lex->position.col = 1;
178 #ifdef USE_MAPPED_LOCATION
180 = linemap_line_start (&line_table, 1, 120);
187 lex->handle = iconv_open ("UCS-2", encoding);
188 if (lex->handle != (iconv_t) -1)
194 lex->read_anything = 0;
195 lex->use_fallback = 0;
197 /* Work around broken iconv() implementations by doing checking at
198 runtime. We assume that if the UTF-8 => UCS-2 encoder is broken,
199 then all UCS-2 encoders will be broken. Perhaps not a valid
207 handle = iconv_open ("UCS-2", "UTF-8");
208 if (handle != (iconv_t) -1)
215 /* This is the UTF-8 encoding of \ufeff. */
222 outp = (char *) &result;
225 r = iconv (handle, (ICONV_CONST char **) &inp, &inc,
227 iconv_close (handle);
228 /* Conversion must be complete for us to use the result. */
229 if (r != (size_t) -1 && inc == 0 && outc == 0)
230 need_byteswap = (result != 0xfeff);
234 lex->byte_swap = need_byteswap;
237 #endif /* HAVE_ICONV */
239 /* If iconv failed, use the internal decoder if the default
240 encoding was requested. This code is used on platforms where
241 iconv exists but is insufficient for our needs. For
242 instance, on Solaris 2.5 iconv cannot handle UTF-8 or UCS-2.
244 On Solaris the default encoding, as returned by nl_langinfo(),
245 is `646' (aka ASCII), but the Solaris iconv_open() doesn't
246 understand that. We work around that by pretending
247 `646' to be the same as UTF-8. */
248 if (strcmp (encoding, DEFAULT_ENCODING) && strcmp (encoding, "646"))
253 lex->use_fallback = 1;
254 lex->encoding = "UTF-8";
256 #endif /* HAVE_ICONV */
260 fatal_error ("unknown encoding: %qs\nThis might mean that your locale's encoding is not supported\nby your system's iconv(3) implementation. If you aren't trying\nto use a particular encoding for your input file, try the\n%<--encoding=UTF-8%> option", encoding);
266 java_destroy_lexer (java_lexer *lex)
269 if (! lex->use_fallback)
270 iconv_close (lex->handle);
276 java_read_char (java_lexer *lex)
279 if (! lex->use_fallback)
281 size_t ir, inbytesleft, in_save, out_count, out_save;
285 /* If there is data which has already been converted, use it. */
286 if (lex->out_first == -1 || lex->out_first >= lex->out_last)
293 /* See if we need to read more data. If FIRST == 0 then
294 the previous conversion attempt ended in the middle of
295 a character at the end of the buffer. Otherwise we
296 only have to read if the buffer is empty. */
297 if (lex->first == 0 || lex->first >= lex->last)
301 if (lex->first >= lex->last)
306 if (feof (lex->finput))
308 r = fread (&lex->buffer[lex->last], 1,
309 sizeof (lex->buffer) - lex->last,
314 inbytesleft = lex->last - lex->first;
315 out_count = sizeof (lex->out_buffer) - lex->out_last;
317 if (inbytesleft == 0)
319 /* We've tried to read and there is nothing left. */
323 in_save = inbytesleft;
324 out_save = out_count;
325 inp = &lex->buffer[lex->first];
326 outp = (char *) &lex->out_buffer[lex->out_last];
327 ir = iconv (lex->handle, (ICONV_CONST char **) &inp,
328 &inbytesleft, &outp, &out_count);
330 /* If we haven't read any bytes, then look to see if we
332 if (! lex->read_anything && out_save - out_count >= 2)
334 unicode_t uc = * (unicode_t *) &lex->out_buffer[0];
340 else if (uc == 0xfffe)
345 lex->read_anything = 1;
351 for (i = 0; i < out_save - out_count; i += 2)
353 char t = lex->out_buffer[lex->out_last + i];
354 lex->out_buffer[lex->out_last + i]
355 = lex->out_buffer[lex->out_last + i + 1];
356 lex->out_buffer[lex->out_last + i + 1] = t;
360 lex->first += in_save - inbytesleft;
361 lex->out_last += out_save - out_count;
363 /* If we converted anything at all, move along. */
364 if (out_count != out_save)
367 if (ir == (size_t) -1)
371 /* This is ok. This means that the end of our buffer
372 is in the middle of a character sequence. We just
373 move the valid part of the buffer to the beginning
375 memmove (&lex->buffer[0], &lex->buffer[lex->first],
376 lex->last - lex->first);
377 lex->last -= lex->first;
382 /* A more serious error. */
385 "Unrecognized character for encoding '%s'",
387 java_lex_error (buffer, 0);
394 if (lex->out_first == -1 || lex->out_first >= lex->out_last)
396 /* Don't have any data. */
401 result = * ((unicode_t *) &lex->out_buffer[lex->out_first]);
406 #endif /* HAVE_ICONV */
409 c = getc (lex->finput);
414 return (unicode_t) c;
417 if ((c & 0xe0) == 0xc0)
419 c1 = getc (lex->finput);
420 if ((c1 & 0xc0) == 0x80)
422 unicode_t r = (unicode_t)(((c & 0x1f) << 6) + (c1 & 0x3f));
423 /* Check for valid 2-byte characters. We explicitly
424 allow \0 because this encoding is common in the
426 if (r == 0 || (r >= 0x80 && r <= 0x7ff))
430 else if ((c & 0xf0) == 0xe0)
432 c1 = getc (lex->finput);
433 if ((c1 & 0xc0) == 0x80)
435 c2 = getc (lex->finput);
436 if ((c2 & 0xc0) == 0x80)
438 unicode_t r = (unicode_t)(((c & 0xf) << 12) +
441 /* Check for valid 3-byte characters.
442 Don't allow surrogate, \ufffe or \uffff. */
443 if (IN_RANGE (r, 0x800, 0xffff)
444 && ! IN_RANGE (r, 0xd800, 0xdfff)
445 && r != 0xfffe && r != 0xffff)
451 /* We simply don't support invalid characters. We also
452 don't support 4-, 5-, or 6-byte UTF-8 sequences, as these
453 cannot be valid Java characters. */
454 java_lex_error ("malformed UTF-8 character", 0);
458 /* We only get here on error. */
463 java_read_unicode (java_lexer *lex, int *unicode_escape_p)
467 if (lex->unget_value)
469 c = lex->unget_value;
470 lex->unget_value = 0;
473 c = java_read_char (lex);
475 *unicode_escape_p = 0;
484 if ((lex->bs_count) % 2 == 1)
486 /* Odd number of \ seen. */
487 c = java_read_char (lex);
490 unicode_t unicode = 0;
493 /* Recognize any number of `u's in \u. */
494 while ((c = java_read_char (lex)) == 'u')
502 java_lex_error ("prematurely terminated \\u sequence", 0);
507 unicode |= (unicode_t)(hex_value (c) << shift);
510 java_lex_error ("non-hex digit in \\u sequence", 0);
514 c = java_read_char (lex);
520 lex->unget_value = c;
523 *unicode_escape_p = 1;
526 lex->unget_value = c;
528 return (unicode_t) '\\';
531 /* Get the next Unicode character (post-Unicode-escape-handling).
532 Move the current position to just after returned character. */
535 java_get_unicode (void)
537 int next = java_peek_unicode ();
538 java_next_unicode ();
542 /* Return the next Unicode character (post-Unicode-escape-handling).
543 Do not move the current position, which remains just before
544 the returned character. */
547 java_peek_unicode (void)
549 int unicode_escape_p;
550 java_lexer *lex = ctxp->lexer;
553 if (lex->avail_unicode)
554 return lex->next_unicode;
556 next = java_read_unicode (lex, &unicode_escape_p);
560 /* We have to read ahead to see if we got \r\n.
561 In that case we return a single line terminator. */
563 next = java_read_unicode (lex, &dummy);
564 if (next != '\n' && next != UEOF)
565 lex->unget_value = next;
566 /* In either case we must return a newline. */
570 lex->next_unicode = next;
571 lex->avail_unicode = 1;
575 lex->next_columns = 0;
581 lex->next_columns = 1 - lex->position.col;
583 else if (next == '\t')
585 int cur_col = lex->position.col;
586 lex->next_columns = ((cur_col + 7) & ~7) + 1 - cur_col;
591 lex->next_columns = 1;
593 if (unicode_escape_p)
594 lex->next_columns = 6;
598 /* Move forward one Unicode character (post-Unicode-escape-handling).
599 Only allowed after java_peek_unicode. The combination java_peek_unicode
600 followed by java_next_unicode is equivalent to java_get_unicode. */
602 static void java_next_unicode (void)
604 struct java_lexer *lex = ctxp->lexer;
605 lex->position.col += lex->next_columns;
606 if (lex->next_unicode == '\n')
608 lex->position.line++;
610 #ifdef USE_MAPPED_LOCATION
612 = linemap_line_start (&line_table, lex->position.line, 120);
614 input_line = lex->position.line;
618 lex->avail_unicode = 0;
622 /* The inverse of java_next_unicode.
623 Not currently used, but could be if it would be cleaner or faster.
624 java_peek_unicode == java_get_unicode + java_unget_unicode.
625 java_get_unicode == java_peek_unicode + java_next_unicode.
627 static void java_unget_unicode ()
629 struct java_lexer *lex = ctxp->lexer;
630 if (lex->avail_unicode)
631 fatal_error ("internal error - bad unget");
632 lex->avail_unicode = 1;
633 lex->position.col -= lex->next_columns;
637 /* Parse the end of a C style comment.
638 * C is the first character following the '/' and '*'. */
640 java_parse_end_comment (int c)
642 for ( ;; c = java_get_unicode ())
647 java_lex_error ("Comment not terminated at end of input", 0);
650 switch (c = java_peek_unicode ())
653 java_lex_error ("Comment not terminated at end of input", 0);
656 java_next_unicode ();
658 case '*': /* Reparse only '*'. */
665 /* Parse the documentation section. Keywords must be at the beginning
666 of a documentation comment line (ignoring white space and any `*'
667 character). Parsed keyword(s): @DEPRECATED. */
670 java_parse_doc_section (int c)
674 /* We reset this here, because only the most recent doc comment
675 applies to the following declaration. */
676 ctxp->deprecated = 0;
678 /* We loop over all the lines of the comment. We'll eventually exit
679 if we hit EOF prematurely, or when we see the comment
683 /* These first steps need only be done if we're still looking
684 for the deprecated tag. If we've already seen it, we might
685 as well skip looking for it again. */
686 if (! ctxp->deprecated)
688 /* Skip whitespace and '*'s. We must also check for the end
689 of the comment here. */
690 while (JAVA_WHITE_SPACE_P (c) || c == '*')
692 last_was_star = (c == '*');
693 c = java_get_unicode ();
694 if (last_was_star && c == '/')
696 /* We just saw the comment terminator. */
706 const char *deprecated = "@deprecated";
709 for (i = 0; deprecated[i]; ++i)
711 if (c != deprecated[i])
713 /* We write the code in this way, with the
714 update at the end, so that after the loop
715 we're left with the next character in C. */
716 c = java_get_unicode ();
722 /* @deprecated must be followed by a space or newline.
723 We also allow a '*' in case it appears just before
724 the end of a comment. In this position only we also
725 must allow any Unicode space character. */
726 if (c == ' ' || c == '\n' || c == '*' || java_space_char_p (c))
729 ctxp->deprecated = 1;
734 /* We've examined the relevant content from this line. Now we
735 skip the remaining characters and start over with the next
736 line. We also check for end of comment here. */
737 while (c != '\n' && c != UEOF)
739 last_was_star = (c == '*');
740 c = java_get_unicode ();
741 if (last_was_star && c == '/')
747 /* We have to advance past the \n. */
748 c = java_get_unicode ();
754 java_lex_error ("Comment not terminated at end of input", 0);
757 /* Return true if C is a valid start character for a Java identifier.
758 This is only called if C >= 128 -- smaller values are handled
759 inline. However, this function handles all values anyway. */
761 java_start_char_p (unicode_t c)
763 unsigned int hi = c / 256;
764 const char *const page = type_table[hi];
765 unsigned long val = (unsigned long) page;
768 if ((val & ~ LETTER_MASK) != 0)
769 flags = page[c & 255];
773 return flags & LETTER_START;
776 /* Return true if C is a valid part character for a Java identifier.
777 This is only called if C >= 128 -- smaller values are handled
778 inline. However, this function handles all values anyway. */
780 java_part_char_p (unicode_t c)
782 unsigned int hi = c / 256;
783 const char *const page = type_table[hi];
784 unsigned long val = (unsigned long) page;
787 if ((val & ~ LETTER_MASK) != 0)
788 flags = page[c & 255];
792 return flags & LETTER_PART;
795 /* Return true if C is whitespace. */
797 java_space_char_p (unicode_t c)
799 unsigned int hi = c / 256;
800 const char *const page = type_table[hi];
801 unsigned long val = (unsigned long) page;
804 if ((val & ~ LETTER_MASK) != 0)
805 flags = page[c & 255];
809 return flags & LETTER_SPACE;
813 java_parse_escape_sequence (void)
817 switch (c = java_get_unicode ())
820 return (unicode_t)0x8;
822 return (unicode_t)0x9;
824 return (unicode_t)0xa;
826 return (unicode_t)0xc;
828 return (unicode_t)0xd;
830 return (unicode_t)0x22;
832 return (unicode_t)0x27;
834 return (unicode_t)0x5c;
835 case '0': case '1': case '2': case '3': case '4':
836 case '5': case '6': case '7':
839 unicode_t char_lit = 0;
843 /* According to the grammar, `\477' has a well-defined
844 meaning -- it is `\47' followed by `7'. */
850 char_lit = 8 * char_lit + c - '0';
853 c = java_peek_unicode ();
854 if (! RANGE (c, '0', '7'))
856 java_next_unicode ();
862 java_lex_error ("Invalid character in escape sequence", -1);
863 return JAVA_CHAR_ERROR;
868 #define IS_ZERO(X) REAL_VALUES_EQUAL (X, dconst0)
870 /* Subroutine of java_lex: converts floating-point literals to tree
871 nodes. LITERAL_TOKEN is the input literal, JAVA_LVAL is where to
872 store the result. FFLAG indicates whether the literal was tagged
873 with an 'f', indicating it is of type 'float'; NUMBER_BEGINNING
874 is the line number on which to report any error. */
876 static void java_perform_atof (YYSTYPE *, char *, int, int);
879 java_perform_atof (YYSTYPE *java_lval, char *literal_token, int fflag,
880 int number_beginning)
882 REAL_VALUE_TYPE value;
883 tree type = (fflag ? FLOAT_TYPE_NODE : DOUBLE_TYPE_NODE);
885 SET_REAL_VALUE_ATOF (value,
886 REAL_VALUE_ATOF (literal_token, TYPE_MODE (type)));
888 if (REAL_VALUE_ISINF (value) || REAL_VALUE_ISNAN (value))
890 JAVA_FLOAT_RANGE_ERROR (fflag ? "float" : "double");
893 else if (IS_ZERO (value))
895 /* We check to see if the value is really 0 or if we've found an
896 underflow. We do this in the most primitive imaginable way. */
898 char *p = literal_token;
901 while (*p && *p != 'e' && *p != 'E')
903 if (*p != '0' && *p != '.')
912 int save_col = ctxp->lexer->position.col;
913 ctxp->lexer->position.col = number_beginning;
914 java_lex_error ("Floating point literal underflow", 0);
915 ctxp->lexer->position.col = save_col;
919 SET_LVAL_NODE (build_real (type, value));
923 static int yylex (YYSTYPE *);
927 yylex (YYSTYPE *java_lval)
929 do_java_lex (YYSTYPE *java_lval)
935 /* Translation of the Unicode escape in the raw stream of Unicode
936 characters. Takes care of line terminator. */
938 /* Skip white spaces: SP, TAB and FF or ULT. */
941 c = java_peek_unicode ();
942 if (c != '\n' && ! JAVA_WHITE_SPACE_P (c))
944 java_next_unicode ();
947 /* Handle EOF here. */
948 if (c == UEOF) /* Should probably do something here... */
952 #ifdef USE_MAPPED_LOCATION
953 LINEMAP_POSITION_FOR_COLUMN (input_location, &line_table,
954 ctxp->lexer->position.col);
956 ctxp->lexer->token_start = ctxp->lexer->position;
960 /* Numeric literals. */
961 if (JAVA_ASCII_DIGIT (c) || (c == '.'))
963 /* This section of code is borrowed from gcc/c-lex.c. */
964 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
965 int parts[TOTAL_PARTS];
966 HOST_WIDE_INT high, low;
967 /* End borrowed section. */
968 char literal_token [256];
969 int literal_index = 0, radix = 10, long_suffix = 0, overflow = 0, bytes;
970 int found_hex_digits = 0, found_non_octal_digits = -1;
973 int number_beginning = ctxp->lexer->position.col;
977 for (i = 0; i < TOTAL_PARTS; i++)
982 java_next_unicode ();
983 c = java_peek_unicode ();
984 if (c == 'x' || c == 'X')
987 java_next_unicode ();
988 c = java_peek_unicode ();
990 else if (JAVA_ASCII_DIGIT (c))
992 literal_token [literal_index++] = '0';
995 else if (c == '.' || c == 'e' || c =='E')
997 literal_token [literal_index++] = '0';
998 /* Handle C during floating-point parsing. */
1002 /* We have a zero literal: 0, 0{l,L}, 0{f,F}, 0{d,D}. */
1006 java_next_unicode ();
1007 SET_LVAL_NODE (long_zero_node);
1008 return (INT_LIT_TK);
1010 java_next_unicode ();
1011 SET_LVAL_NODE (float_zero_node);
1014 java_next_unicode ();
1015 SET_LVAL_NODE (double_zero_node);
1018 SET_LVAL_NODE (integer_zero_node);
1019 return (INT_LIT_TK);
1023 /* Parse the first part of the literal, until we find something
1024 which is not a number. */
1025 while (radix == 16 ? JAVA_ASCII_HEXDIGIT (c) : JAVA_ASCII_DIGIT (c))
1027 /* We store in a string (in case it turns out to be a FP) and in
1028 PARTS if we have to process a integer literal. */
1029 int numeric = hex_value (c);
1032 /* Remember when we find a valid hexadecimal digit. */
1034 found_hex_digits = 1;
1035 /* Remember when we find an invalid octal digit. */
1036 else if (radix == 8 && numeric >= 8 && found_non_octal_digits < 0)
1037 found_non_octal_digits = literal_index;
1039 literal_token [literal_index++] = c;
1040 /* This section of code if borrowed from gcc/c-lex.c. */
1041 for (count = 0; count < TOTAL_PARTS; count++)
1043 parts[count] *= radix;
1046 parts[count] += (parts[count-1] >> HOST_BITS_PER_CHAR);
1047 parts[count-1] &= (1 << HOST_BITS_PER_CHAR) - 1;
1050 parts[0] += numeric;
1052 if (parts [TOTAL_PARTS-1] != 0)
1054 /* End borrowed section. */
1055 java_next_unicode ();
1056 c = java_peek_unicode ();
1059 /* If we have something from the FP char set but not a digit, parse
1061 if (JAVA_ASCII_FPCHAR (c) && !JAVA_ASCII_DIGIT (c))
1063 /* stage==0: seen digits only
1064 * stage==1: seen '.'
1065 * stage==2: seen 'e' or 'E'.
1066 * stage==3: seen '+' or '-' after 'e' or 'E'.
1067 * stage==4: seen type suffix ('f'/'F'/'d'/'D')
1070 int seen_digit = (literal_index ? 1 : 0);
1071 int seen_exponent = 0;
1072 int fflag = 0; /* 1 for {f,F}, 0 for {d,D}. FP literal are
1073 double unless specified. */
1075 /* It is ok if the radix is 8 because this just means we've
1076 seen a leading `0'. However, radix==16 is invalid. */
1078 java_lex_error ("Can't express non-decimal FP literal", 0);
1088 literal_token [literal_index++ ] = c;
1089 java_next_unicode ();
1090 c = java_peek_unicode ();
1091 if (literal_index == 1 && !JAVA_ASCII_DIGIT (c))
1092 BUILD_OPERATOR (DOT_TK);
1095 java_lex_error ("Invalid character in FP literal", 0);
1098 if (c == 'e' || c == 'E')
1102 /* {E,e} must have seen at least a digit. */
1105 ("Invalid FP literal, mantissa must have digit", 0);
1109 literal_token [literal_index++] = c;
1110 java_next_unicode ();
1111 c = java_peek_unicode ();
1114 java_lex_error ("Invalid character in FP literal", 0);
1116 if ( c == 'f' || c == 'F' || c == 'd' || c == 'D')
1118 fflag = ((c == 'd') || (c == 'D')) ? 0 : 1;
1119 stage = 4; /* So we fall through. */
1122 if ((c=='-' || c =='+') && stage == 2)
1125 literal_token [literal_index++] = c;
1126 java_next_unicode ();
1127 c = java_peek_unicode ();
1130 if ((stage == 0 && JAVA_ASCII_FPCHAR (c)) ||
1131 (stage == 1 && JAVA_ASCII_FPCHAR (c) && !(c == '.')) ||
1132 (stage == 2 && (JAVA_ASCII_DIGIT (c) || JAVA_FP_PM (c))) ||
1133 (stage == 3 && JAVA_ASCII_DIGIT (c)))
1135 if (JAVA_ASCII_DIGIT (c))
1139 literal_token [literal_index++ ] = c;
1140 java_next_unicode ();
1141 c = java_peek_unicode ();
1145 if (stage == 4) /* Don't push back fF/dD. */
1146 java_next_unicode ();
1148 /* An exponent (if any) must have seen a digit. */
1149 if (seen_exponent && !seen_digit)
1151 ("Invalid FP literal, exponent must have digit", 0);
1153 literal_token [literal_index] = '\0';
1156 java_perform_atof (java_lval, literal_token,
1157 fflag, number_beginning);
1162 } /* JAVA_ASCII_FPCHAR (c) */
1164 /* Here we get back to converting the integral literal. */
1165 if (radix == 16 && ! found_hex_digits)
1167 ("0x must be followed by at least one hexadecimal digit", 0);
1168 else if (radix == 8 && found_non_octal_digits >= 0)
1170 int back = literal_index - found_non_octal_digits;
1171 ctxp->lexer->position.col -= back;
1172 java_lex_error ("Octal literal contains digit out of range", 0);
1173 ctxp->lexer->position.col += back;
1175 else if (c == 'L' || c == 'l')
1177 java_next_unicode ();
1181 /* This section of code is borrowed from gcc/c-lex.c. */
1184 bytes = GET_TYPE_PRECISION (long_type_node);
1185 for (i = bytes; i < TOTAL_PARTS; i++)
1193 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1195 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1196 / HOST_BITS_PER_CHAR)]
1197 << (i * HOST_BITS_PER_CHAR));
1198 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1200 /* End borrowed section. */
1203 /* Range checking. */
1204 /* Temporarily set type to unsigned. */
1205 value = build_int_cst_wide (long_suffix
1206 ? unsigned_long_type_node
1207 : unsigned_int_type_node, low, high);
1208 SET_LVAL_NODE (value);
1210 /* For base 10 numbers, only values up to the highest value
1211 (plus one) can be written. For instance, only ints up to
1212 2147483648 can be written. The special case of the largest
1213 negative value is handled elsewhere. For other bases, any
1214 number can be represented. */
1215 if (overflow || (radix == 10
1216 && tree_int_cst_lt (long_suffix
1222 JAVA_RANGE_ERROR ("Numeric overflow for 'long' literal");
1224 JAVA_RANGE_ERROR ("Numeric overflow for 'int' literal");
1227 /* Sign extend the value. */
1228 value = build_int_cst_wide (long_suffix ? long_type_node : int_type_node,
1230 value = force_fit_type (value, 0, false, false);
1234 value = copy_node (value);
1235 JAVA_NOT_RADIX10_FLAG (value) = 1;
1238 SET_LVAL_NODE (value);
1243 /* We may have an ID here. */
1244 if (JAVA_START_CHAR_P (c))
1246 int ascii_index = 0, all_ascii = 1;
1248 /* Keyword, boolean literal or null literal. */
1249 while (c != UEOF && JAVA_PART_CHAR_P (c))
1251 java_unicode_2_utf8 (c);
1254 java_next_unicode ();
1256 c = java_peek_unicode ();
1259 obstack_1grow (&temporary_obstack, '\0');
1260 string = obstack_finish (&temporary_obstack);
1262 /* If we have something all ascii, we consider a keyword, a boolean
1263 literal, a null literal or an all ASCII identifier. Otherwise,
1264 this is an identifier (possibly not respecting formation rule). */
1267 const struct java_keyword *kw;
1268 if ((kw=java_keyword (string, ascii_index)))
1272 case PUBLIC_TK: case PROTECTED_TK: case STATIC_TK:
1273 case ABSTRACT_TK: case FINAL_TK: case NATIVE_TK:
1274 case SYNCHRONIZED_TK: case TRANSIENT_TK: case VOLATILE_TK:
1275 case PRIVATE_TK: case STRICT_TK:
1276 SET_MODIFIER_CTX (kw->token);
1279 SET_LVAL_NODE (float_type_node);
1282 SET_LVAL_NODE (double_type_node);
1285 SET_LVAL_NODE (boolean_type_node);
1288 SET_LVAL_NODE (byte_type_node);
1291 SET_LVAL_NODE (short_type_node);
1294 SET_LVAL_NODE (int_type_node);
1297 SET_LVAL_NODE (long_type_node);
1300 SET_LVAL_NODE (char_type_node);
1303 /* Keyword based literals. */
1306 SET_LVAL_NODE ((kw->token == TRUE_TK ?
1307 boolean_true_node : boolean_false_node));
1310 SET_LVAL_NODE (null_pointer_node);
1316 BUILD_OPERATOR (kw->token);
1322 /* Some keyword we want to retain information on the location
1323 they where found. */
1335 BUILD_OPERATOR (kw->token);
1343 java_lval->node = BUILD_ID_WFL (GET_IDENTIFIER (string));
1347 java_next_unicode ();
1349 /* Character literals. */
1354 if ((c = java_get_unicode ()) == '\\')
1355 char_lit = java_parse_escape_sequence ();
1358 if (c == '\n' || c == '\'')
1359 java_lex_error ("Invalid character literal", 0);
1363 c = java_get_unicode ();
1365 if ((c == '\n') || (c == UEOF))
1366 java_lex_error ("Character literal not terminated at end of line", 0);
1368 java_lex_error ("Syntax error in character literal", 0);
1370 if (char_lit == JAVA_CHAR_ERROR)
1371 char_lit = 0; /* We silently convert it to zero. */
1373 SET_LVAL_NODE (build_int_cst (char_type_node, char_lit));
1377 /* String literals. */
1385 c = java_peek_unicode ();
1386 if (c == '\n' || c == UEOF) /* ULT. */
1388 java_lex_error ("String not terminated at end of line", 0);
1391 java_next_unicode ();
1395 c = java_parse_escape_sequence ();
1396 if (c == JAVA_CHAR_ERROR)
1399 c = 0; /* We silently convert it to zero. */
1401 java_unicode_2_utf8 (c);
1404 obstack_1grow (&temporary_obstack, '\0');
1405 string = obstack_finish (&temporary_obstack);
1407 if (!no_error || (c != '"'))
1408 java_lval->node = error_mark_node; /* FIXME: Requires further
1411 java_lval->node = build_string (strlen (string), string);
1413 obstack_free (&temporary_obstack, string);
1414 return STRING_LIT_TK;
1420 /* Check for comment. */
1421 switch (c = java_peek_unicode ())
1424 java_next_unicode ();
1427 c = java_get_unicode ();
1430 /* It is ok to end a `//' comment with EOF, unless
1431 we're being pedantic. */
1433 java_lex_error ("Comment not terminated at end of input",
1437 if (c == '\n') /* ULT */
1443 java_next_unicode ();
1444 if ((c = java_get_unicode ()) == '*')
1446 c = java_get_unicode ();
1449 /* Empty documentation comment. We have to reset
1450 the deprecation marker as only the most recent
1451 doc comment applies. */
1452 ctxp->deprecated = 0;
1455 java_parse_doc_section (c);
1458 java_parse_end_comment ((c = java_get_unicode ()));
1463 java_next_unicode ();
1464 BUILD_OPERATOR2 (DIV_ASSIGN_TK);
1467 BUILD_OPERATOR (DIV_TK);
1471 BUILD_OPERATOR (OP_TK);
1476 java_lval->operator.token = OCB_TK;
1477 java_lval->operator.location = BUILD_LOCATION();
1478 #ifdef USE_MAPPED_LOCATION
1479 if (ctxp->ccb_indent == 1)
1480 ctxp->first_ccb_indent1 = input_location;
1482 if (ctxp->ccb_indent == 1)
1483 ctxp->first_ccb_indent1 = input_line;
1491 java_lval->operator.token = CCB_TK;
1492 java_lval->operator.location = BUILD_LOCATION();
1493 #ifdef USE_MAPPED_LOCATION
1494 if (ctxp->ccb_indent == 1)
1495 ctxp->last_ccb_indent1 = input_location;
1497 if (ctxp->ccb_indent == 1)
1498 ctxp->last_ccb_indent1 = input_line;
1503 BUILD_OPERATOR (OSB_TK);
1511 BUILD_OPERATOR (DOT_TK);
1515 c = java_peek_unicode ();
1518 java_next_unicode ();
1519 BUILD_OPERATOR (EQ_TK);
1523 /* Equals is used in two different locations. In the
1524 variable_declarator: rule, it has to be seen as '=' as opposed
1525 to being seen as an ordinary assignment operator in
1526 assignment_operators: rule. */
1527 BUILD_OPERATOR (ASSIGN_TK);
1531 switch ((c = java_peek_unicode ()))
1534 java_next_unicode ();
1535 BUILD_OPERATOR (GTE_TK);
1537 java_next_unicode ();
1538 switch ((c = java_peek_unicode ()))
1541 java_next_unicode ();
1542 c = java_peek_unicode ();
1545 java_next_unicode ();
1546 BUILD_OPERATOR2 (ZRS_ASSIGN_TK);
1550 BUILD_OPERATOR (ZRS_TK);
1553 java_next_unicode ();
1554 BUILD_OPERATOR2 (SRS_ASSIGN_TK);
1556 BUILD_OPERATOR (SRS_TK);
1559 BUILD_OPERATOR (GT_TK);
1563 switch ((c = java_peek_unicode ()))
1566 java_next_unicode ();
1567 BUILD_OPERATOR (LTE_TK);
1569 java_next_unicode ();
1570 if ((c = java_peek_unicode ()) == '=')
1572 java_next_unicode ();
1573 BUILD_OPERATOR2 (LS_ASSIGN_TK);
1577 BUILD_OPERATOR (LS_TK);
1580 BUILD_OPERATOR (LT_TK);
1584 switch ((c = java_peek_unicode ()))
1587 java_next_unicode ();
1588 BUILD_OPERATOR (BOOL_AND_TK);
1590 java_next_unicode ();
1591 BUILD_OPERATOR2 (AND_ASSIGN_TK);
1593 BUILD_OPERATOR (AND_TK);
1597 switch ((c = java_peek_unicode ()))
1600 java_next_unicode ();
1601 BUILD_OPERATOR (BOOL_OR_TK);
1603 java_next_unicode ();
1604 BUILD_OPERATOR2 (OR_ASSIGN_TK);
1606 BUILD_OPERATOR (OR_TK);
1610 switch ((c = java_peek_unicode ()))
1613 java_next_unicode ();
1614 BUILD_OPERATOR (INCR_TK);
1616 java_next_unicode ();
1617 BUILD_OPERATOR2 (PLUS_ASSIGN_TK);
1619 BUILD_OPERATOR (PLUS_TK);
1623 switch ((c = java_peek_unicode ()))
1626 java_next_unicode ();
1627 BUILD_OPERATOR (DECR_TK);
1629 java_next_unicode ();
1630 BUILD_OPERATOR2 (MINUS_ASSIGN_TK);
1632 BUILD_OPERATOR (MINUS_TK);
1636 if ((c = java_peek_unicode ()) == '=')
1638 java_next_unicode ();
1639 BUILD_OPERATOR2 (MULT_ASSIGN_TK);
1643 BUILD_OPERATOR (MULT_TK);
1647 if ((c = java_peek_unicode ()) == '=')
1649 java_next_unicode ();
1650 BUILD_OPERATOR2 (XOR_ASSIGN_TK);
1654 BUILD_OPERATOR (XOR_TK);
1658 if ((c = java_peek_unicode ()) == '=')
1660 java_next_unicode ();
1661 BUILD_OPERATOR2 (REM_ASSIGN_TK);
1665 BUILD_OPERATOR (REM_TK);
1669 if ((c = java_peek_unicode()) == '=')
1671 java_next_unicode ();
1672 BUILD_OPERATOR (NEQ_TK);
1676 BUILD_OPERATOR (NEG_TK);
1680 BUILD_OPERATOR (REL_QM_TK);
1682 BUILD_OPERATOR (REL_CL_TK);
1684 BUILD_OPERATOR (NOT_TK);
1687 if (c == 0x1a) /* CTRL-Z. */
1689 if ((c = java_peek_unicode ()) == UEOF)
1690 return 0; /* Ok here. */
1693 /* Everything else is an invalid character in the input. */
1695 char lex_error_buffer [128];
1696 sprintf (lex_error_buffer, "Invalid character '%s' in input",
1697 java_sprint_unicode (c));
1698 java_lex_error (lex_error_buffer, -1);
1705 /* The exported interface to the lexer. */
1707 java_lex (YYSTYPE *java_lval)
1711 timevar_push (TV_LEX);
1712 r = do_java_lex (java_lval);
1713 timevar_pop (TV_LEX);
1717 /* This is called by the parser to see if an error should be generated
1718 due to numeric overflow. This function only handles the particular
1719 case of the largest negative value, and is only called in the case
1720 where this value is not preceded by `-'. */
1722 error_if_numeric_overflow (tree value)
1724 if (TREE_CODE (value) == INTEGER_CST
1725 && !JAVA_NOT_RADIX10_FLAG (value)
1726 && tree_int_cst_sgn (value) < 0)
1728 if (TREE_TYPE (value) == long_type_node)
1729 java_lex_error ("Numeric overflow for 'long' literal", 0);
1731 java_lex_error ("Numeric overflow for 'int' literal", 0);
1735 #endif /* JC1_LITE */
1738 java_unicode_2_utf8 (unicode_t unicode)
1740 if (RANGE (unicode, 0x01, 0x7f))
1741 obstack_1grow (&temporary_obstack, (char)unicode);
1742 else if (RANGE (unicode, 0x80, 0x7ff) || unicode == 0)
1744 obstack_1grow (&temporary_obstack,
1745 (unsigned char)(0xc0 | ((0x7c0 & unicode) >> 6)));
1746 obstack_1grow (&temporary_obstack,
1747 (unsigned char)(0x80 | (unicode & 0x3f)));
1749 else /* Range 0x800-0xffff. */
1751 obstack_1grow (&temporary_obstack,
1752 (unsigned char)(0xe0 | (unicode & 0xf000) >> 12));
1753 obstack_1grow (&temporary_obstack,
1754 (unsigned char)(0x80 | (unicode & 0x0fc0) >> 6));
1755 obstack_1grow (&temporary_obstack,
1756 (unsigned char)(0x80 | (unicode & 0x003f)));
1762 build_wfl_node (tree node)
1764 #ifdef USE_MAPPED_LOCATION
1765 node = build_expr_wfl (node, input_location);
1767 node = build_expr_wfl (node, ctxp->filename,
1768 ctxp->lexer->token_start.line,
1769 ctxp->lexer->token_start.col);
1771 /* Prevent java_complete_lhs from short-circuiting node (if constant). */
1772 TREE_TYPE (node) = NULL_TREE;
1778 java_lex_error (const char *msg ATTRIBUTE_UNUSED, int forward ATTRIBUTE_UNUSED)
1781 int col = (ctxp->lexer->position.col
1782 + forward * ctxp->lexer->next_columns);
1783 #if USE_MAPPED_LOCATION
1784 source_location save_location = input_location;
1785 LINEMAP_POSITION_FOR_COLUMN (input_location, &line_table, col);
1787 /* Might be caught in the middle of some error report. */
1788 ctxp->java_error_flag = 0;
1791 input_location = save_location;
1793 java_lc save = ctxp->lexer->token_start;
1794 ctxp->lexer->token_start.line = ctxp->lexer->position.line;
1795 ctxp->lexer->token_start.col = col;
1797 /* Might be caught in the middle of some error report. */
1798 ctxp->java_error_flag = 0;
1801 ctxp->lexer->token_start = save;
1808 java_is_eol (FILE *fp, int c)
1815 if (next != '\n' && next != EOF)
1827 java_get_line_col (const char *filename ATTRIBUTE_UNUSED,
1828 int line ATTRIBUTE_UNUSED, int col ATTRIBUTE_UNUSED)
1833 /* Dumb implementation. Doesn't try to cache or optimize things. */
1834 /* First line of the file is line 1, first column is 1. */
1836 /* COL == -1 means, at the CR/LF in LINE. */
1837 /* COL == -2 means, at the first non space char in LINE. */
1840 int c, ccol, cline = 1;
1841 int current_line_col = 0;
1842 int first_non_space = 0;
1845 if (!(fp = fopen (filename, "r")))
1846 fatal_error ("can't open %s: %m", filename);
1848 while (cline != line)
1853 static const char msg[] = "<<file too short - unexpected EOF>>";
1854 obstack_grow (&temporary_obstack, msg, sizeof(msg)-1);
1857 if (java_is_eol (fp, c))
1861 /* Gather the chars of the current line in a buffer. */
1865 if (c < 0 || java_is_eol (fp, c))
1867 if (!first_non_space && !JAVA_WHITE_SPACE_P (c))
1868 first_non_space = current_line_col;
1869 obstack_1grow (&temporary_obstack, c);
1874 obstack_1grow (&temporary_obstack, '\n');
1878 col = current_line_col;
1879 first_non_space = 0;
1882 col = first_non_space;
1884 first_non_space = 0;
1886 /* Place the '^' a the right position. */
1887 base = obstack_base (&temporary_obstack);
1888 for (col += 2, ccol = 0; ccol < col; ccol++)
1890 /* Compute \t when reaching first_non_space. */
1891 char c = (first_non_space ?
1892 (base [ccol] == '\t' ? '\t' : ' ') : ' ');
1893 obstack_1grow (&temporary_obstack, c);
1895 obstack_grow0 (&temporary_obstack, "^", 1);
1898 return obstack_finish (&temporary_obstack);
1904 utf8_cmp (const unsigned char *str, int length, const char *name)
1906 const unsigned char *limit = str + length;
1909 for (i = 0; name[i]; ++i)
1911 int ch = UTF8_GET (str, limit);
1913 return ch - name[i];
1916 return str == limit ? 0 : 1;
1919 /* A sorted list of all C++ keywords. */
1921 static const char *const cxx_keywords[] =
2029 /* Return true if NAME is a C++ keyword. */
2032 cxx_keyword_p (const char *name, int length)
2034 int last = ARRAY_SIZE (cxx_keywords);
2036 int mid = (last + first) / 2;
2039 for (mid = (last + first) / 2;
2041 old = mid, mid = (last + first) / 2)
2043 int kwl = strlen (cxx_keywords[mid]);
2044 int min_length = kwl > length ? length : kwl;
2045 int r = utf8_cmp ((const unsigned char *) name, min_length, cxx_keywords[mid]);
2050 /* We've found a match if all the remaining characters are `$'. */
2051 for (i = min_length; i < length && name[i] == '$'; ++i)
2065 #endif /* JC1_LITE */