]> gcc.gnu.org Git - gcc.git/blobdiff - gcc/java/jcf-parse.c
lex.c (java_new_lexer): Alias "646" to DEFAULT_ENCODING.
[gcc.git] / gcc / java / jcf-parse.c
index 1962f80ecc2b62d9d5ccb4d7faeaa0a135567fc4..53e647cb2c6313114a5e652a439f990752ce9b7b 100644 (file)
@@ -1,5 +1,6 @@
 /* Parser for Java(TM) .class files.
-   Copyright (C) 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002
+   Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -34,6 +35,17 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 #include "java-tree.h"
 #include "toplev.h"
 #include "parse.h"
+#include "ggc.h"
+#include "debug.h"
+#include "assert.h"
+
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
+
+#ifdef HAVE_NL_LANGINFO
+#include <langinfo.h>
+#endif
 
 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
@@ -61,35 +73,58 @@ extern struct obstack permanent_obstack;
    before static field references.  */
 extern int always_initialize_class_p;
 
-/* The class we are currently processing. */
-tree current_class = NULL_TREE;
-
-/* The class we started with. */
-tree main_class = NULL_TREE;
-
-/* List of all class DECL seen so far.  */
-tree all_class_list = NULL_TREE;
+static tree parse_roots[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
 
 /* The FIELD_DECL for the current field.  */
-static tree current_field = NULL_TREE;
+#define current_field parse_roots[0]
 
 /* The METHOD_DECL for the current method.  */
-static tree current_method = NULL_TREE;
+#define current_method parse_roots[1]
+
+/* A list of file names.  */
+#define current_file_list parse_roots[2]
 
-/* The Java .class file that provides main_class;  the main input file. */
+/* The Java archive that provides main_class;  the main input file. */
 static struct JCF main_jcf[1];
 
+static struct ZipFile *localToFile;
+
 /* Declarations of some functions used here.  */
+static void handle_innerclass_attribute PARAMS ((int count, JCF *));
 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
 static void parse_zip_file_entries PARAMS ((void));
-static void process_zip_dir PARAMS ((void));
-static void parse_source_file PARAMS ((tree));
-static void jcf_parse_source PARAMS ((void));
-static int jcf_figure_file_type PARAMS ((JCF *));
-static int find_in_current_zip PARAMS ((const char *, struct JCF **));
+static void process_zip_dir PARAMS ((FILE *));
+static void parse_source_file_1 PARAMS ((tree, FILE *));
+static void parse_source_file_2 PARAMS ((void));
+static void parse_source_file_3 PARAMS ((void));
 static void parse_class_file PARAMS ((void));
 static void set_source_filename PARAMS ((JCF *, int));
-static int predefined_filename_p PARAMS ((tree));
+static void ggc_mark_jcf PARAMS ((void**));
+static void jcf_parse PARAMS ((struct JCF*));
+static void load_inner_classes PARAMS ((tree));
+
+/* Mark (for garbage collection) all the tree nodes that are
+   referenced from JCF's constant pool table. Do that only if the JCF
+   hasn't been marked finished.  */
+
+static void
+ggc_mark_jcf (elt)
+     void **elt;
+{
+  JCF *jcf = *(JCF**) elt;
+  if (jcf != NULL && !jcf->finished)
+    {
+      CPool *cpool = &jcf->cpool;
+      int size = CPOOL_COUNT(cpool);
+      int index;
+      for (index = 1; index < size;  index++)
+       {
+         int tag = JPOOL_TAG (jcf, index);
+         if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
+           ggc_mark_tree ((tree) cpool->data[index]);
+       }
+    }
+}
 
 /* Handle "SourceFile" attribute. */
 
@@ -99,7 +134,7 @@ set_source_filename (jcf, index)
      int index;
 {
   tree sfname_id = get_name_constant (jcf, index);
-  char *sfname = IDENTIFIER_POINTER (sfname_id);
+  const char *sfname = IDENTIFIER_POINTER (sfname_id);
   if (input_filename != NULL)
     {
       int old_len = strlen (input_filename);
@@ -131,7 +166,10 @@ set_source_filename (jcf, index)
 { int sig_index = SIGNATURE; \
   current_field = add_field (current_class, get_name_constant (jcf, NAME), \
                             parse_signature (jcf, sig_index), ACCESS_FLAGS); \
- set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
+ set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
+ if ((ACCESS_FLAGS) & ACC_FINAL) \
+   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
+}
 
 #define HANDLE_END_FIELDS() \
   (current_field = NULL_TREE)
@@ -155,7 +193,8 @@ set_source_filename (jcf, index)
 
 #define HANDLE_END_METHODS() \
 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
-  if (handle_type != current_class) layout_type (handle_type); }
+  if (handle_type != current_class) layout_type (handle_type); \
+  current_method = NULL_TREE; }
 
 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
@@ -187,27 +226,23 @@ set_source_filename (jcf, index)
 
 /* Link seen inner classes to their outer context and register the
    inner class to its outer context. They will be later loaded.  */
-#define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT)                             \
-{                                                                        \
-  int c = (count);                                                       \
-  while (c--)                                                            \
-    {                                                                    \
-      tree class = get_class_constant (jcf, JCF_readu2 (jcf));           \
-      tree decl = TYPE_NAME (class);                                     \
-      if (DECL_P (decl) && !CLASS_COMPLETE_P (decl))                     \
-       {                                                                 \
-         tree outer = TYPE_NAME (get_class_constant (jcf,                \
-                                                     JCF_readu2 (jcf))); \
-         tree alias = get_name_constant (jcf, JCF_readu2 (jcf));         \
-         JCF_SKIP (jcf, 2);                                              \
-         DECL_CONTEXT (decl) = outer;                                    \
-         DECL_INNER_CLASS_LIST (outer) =                                 \
-           tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));       \
-         CLASS_COMPLETE_P (decl) = 1;                                    \
-       }                                                                 \
-      else                                                               \
-       JCF_SKIP (jcf, 6);                                                \
-    }                                                                    \
+#define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
+  handle_innerclass_attribute (COUNT, jcf)
+
+#define HANDLE_SYNTHETIC_ATTRIBUTE()                                   \
+{                                                                      \
+  /* Irrelevant decls should have been nullified by the END macros.    \
+     We only handle the `Synthetic' attribute on method DECLs.         \
+     DECL_ARTIFICIAL on fields is used for something else (See         \
+     PUSH_FIELD in java-tree.h) */                                     \
+  if (current_method)                                                  \
+    DECL_ARTIFICIAL (current_method) = 1;                              \
+}
+
+#define HANDLE_GCJCOMPILED_ATTRIBUTE()         \
+{                                              \
+  if (current_class == object_type_node)       \
+    jcf->right_zip = 1;                                \
 }
 
 #include "jcf-reader.c"
@@ -219,25 +254,16 @@ parse_signature (jcf, sig_index)
      JCF *jcf;
      int sig_index;
 {
-  if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
+  if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
       || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
-    fatal ("invalid field/method signature");
+    abort ();
   else
-    {
-      return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
-                                    JPOOL_UTF_LENGTH (jcf, sig_index));
-    }
+    return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
+                                  JPOOL_UTF_LENGTH (jcf, sig_index));
 }
 
 void
-init_lex ()
-{
-  /* Make identifier nodes long enough for the language-specific slots.  */
-  set_identifier_size (sizeof (struct lang_identifier));
-}
-
-void
-set_yydebug (value)
+java_set_yydebug (value)
      int value;
 {
   yydebug = value;
@@ -255,7 +281,6 @@ get_constant (jcf, index)
   tag = JPOOL_TAG (jcf, index);
   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
     return (tree) jcf->cpool.data[index];
-  push_obstacks (&permanent_obstack, &permanent_obstack);
   switch (tag)
     {
     case CONSTANT_Integer:
@@ -270,8 +295,8 @@ get_constant (jcf, index)
        jint num = JPOOL_INT (jcf, index);
        HOST_WIDE_INT lo, hi;
        lshift_double (num, 0, 32, 64, &lo, &hi, 0);
-       num = JPOOL_INT (jcf, index+1);
-       add_double (lo, hi, (uint32)num, 0, &lo, &hi);
+       num = JPOOL_INT (jcf, index+1) & 0xffffffff;
+       add_double (lo, hi, num, 0, &lo, &hi);
        value = build_int_2 (lo, hi);
        TREE_TYPE (value) = long_type_node;
        force_fit_type (value, 0);
@@ -282,13 +307,7 @@ get_constant (jcf, index)
       {
        jint num = JPOOL_INT(jcf, index);
        REAL_VALUE_TYPE d;
-#ifdef REAL_ARITHMETIC
        d = REAL_VALUE_FROM_TARGET_SINGLE (num);
-#else
-       union { float f;  jint i; } u;
-       u.i = num;
-       d = u.f;
-#endif
        value = build_real (float_type_node, d);
        break;
       }
@@ -301,7 +320,15 @@ get_constant (jcf, index)
        lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
        num[0] = JPOOL_INT (jcf, index+1);
        add_double (lo, hi, num[0], 0, &lo, &hi);
-       if (FLOAT_WORDS_BIG_ENDIAN)
+
+       /* Since ereal_from_double expects an array of HOST_WIDE_INT
+          in the target's format, we swap the elements for big endian
+          targets, unless HOST_WIDE_INT is sufficiently large to
+          contain a target double, in which case the 2nd element
+          is ignored.
+
+          FIXME: Is this always right for cross targets? */
+       if (FLOAT_WORDS_BIG_ENDIAN && sizeof(num[0]) < 8)
          {
            num[0] = hi;
            num[1] = lo;
@@ -311,93 +338,46 @@ get_constant (jcf, index)
            num[0] = lo;
            num[1] = hi;
          }
-#ifdef REAL_ARITHMETIC
        d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
-#else
-       {
-         union { double d;  jint i[2]; } u;
-         u.i[0] = (jint) num[0];
-         u.i[1] = (jint) num[1];
-         d = u.d;
-       }
-#endif
        value = build_real (double_type_node, d);
        break;
       }
 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
     case CONSTANT_String:
       {
-       extern struct obstack *expression_obstack;
        tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
        const char *utf8_ptr = IDENTIFIER_POINTER (name);
-       unsigned char *str_ptr;
        int utf8_len = IDENTIFIER_LENGTH (name);
-       const unsigned char *str = (const unsigned char *)utf8_ptr;
-       int i = utf8_len;
-       int str_len;
+       const unsigned char *utf8;
+       int i;
 
-       /* Count the number of Unicode characters in the string,
-          while checking for a malformed Utf8 string. */
-       for (str_len = 0; i > 0; str_len++)
+       /* Check for a malformed Utf8 string.  */
+       utf8 = (const unsigned char *) utf8_ptr;
+       i = utf8_len;
+       while (i > 0)
          {
-           int char_len = UT8_CHAR_LENGTH (*str);
+           int char_len = UT8_CHAR_LENGTH (*utf8);
            if (char_len < 0 || char_len > 3 || char_len > i)
-             fatal ("bad string constant");
-           str += char_len;
+             fatal_error ("bad string constant");
+
+           utf8 += char_len;
            i -= char_len;
          }
 
-       value = make_node (STRING_CST);
+       /* Allocate a new string value.  */
+       value = build_string (utf8_len, utf8_ptr);
        TREE_TYPE (value) = build_pointer_type (string_type_node);
-       TREE_STRING_LENGTH (value) = 2 * str_len;
-       TREE_STRING_POINTER (value)
-         = obstack_alloc (expression_obstack, 2 * str_len);
-       str_ptr = (unsigned char *) TREE_STRING_POINTER (value);
-       str = (const unsigned char *)utf8_ptr;
-       for (i = 0; i < str_len; i++)
-         {
-           int char_value;
-           int char_len = UT8_CHAR_LENGTH (*str);
-           switch (char_len)
-             {
-             case 1:
-               char_value = *str++;
-               break;
-             case 2:
-               char_value = *str++ & 0x1F;
-               char_value = (char_value << 6) | (*str++ & 0x3F);
-               break;
-             case 3:
-               char_value = *str++ & 0x0F;
-               char_value = (char_value << 6) | (*str++ & 0x3F);
-               char_value = (char_value << 6) | (*str++ & 0x3F);
-               break;
-             default:
-               goto bad;
-             }
-           if (BYTES_BIG_ENDIAN)
-             {
-               *str_ptr++ = char_value >> 8;
-               *str_ptr++ = char_value & 0xFF;
-             }
-           else
-             {
-               *str_ptr++ = char_value & 0xFF;
-               *str_ptr++ = char_value >> 8;
-             }
-         }
       }
       break;
     default:
       goto bad;
     }
-  pop_obstacks ();
-  JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
+  JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
   jcf->cpool.data [index] = (jword) value;
   return value;
  bad:
-  fatal ("bad value constant type %d, index %d", 
-        JPOOL_TAG( jcf, index ), index);
+  internal_error ("bad value constant type %d, index %d", 
+                 JPOOL_TAG (jcf, index), index);
 }
 
 tree
@@ -406,19 +386,63 @@ get_name_constant (jcf, index)
   int index;
 {
   tree name = get_constant (jcf, index);
+
   if (TREE_CODE (name) != IDENTIFIER_NODE)
-    fatal ("bad nameandtype index %d", index);
+    abort ();
+
   return name;
 }
 
+/* Handle reading innerclass attributes. If a non zero entry (denoting
+   a non anonymous entry) is found, We augment the inner class list of
+   the outer context with the newly resolved innerclass.  */
+
+static void
+handle_innerclass_attribute (count, jcf)
+     int count;
+     JCF *jcf;
+{
+  int c = (count);
+  while (c--)
+    {
+      /* Read inner_class_info_index. This may be 0 */
+      int icii = JCF_readu2 (jcf);
+      /* Read outer_class_info_index. If the innerclasses attribute
+        entry isn't a member (like an inner class) the value is 0. */
+      int ocii = JCF_readu2 (jcf);
+      /* Read inner_name_index. If the class we're dealing with is
+        an annonymous class, it must be 0. */
+      int ini = JCF_readu2 (jcf);
+      /* Read the access flag. */
+      int acc = JCF_readu2 (jcf);
+      /* If icii is 0, don't try to read the class. */
+      if (icii >= 0)
+       {
+         tree class = get_class_constant (jcf, icii);
+         tree decl = TYPE_NAME (class);
+          /* Skip reading further if ocii is null */
+          if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
+           {
+             tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
+             tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
+             set_class_decl_access_flags (acc, decl);
+             DECL_CONTEXT (decl) = outer;
+             DECL_INNER_CLASS_LIST (outer) =
+               tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
+             CLASS_COMPLETE_P (decl) = 1;
+            }
+       }
+    }
+}
+
 static tree
 give_name_to_class (jcf, i)
      JCF *jcf;
      int i;
 {
-  if (i <= 0 || i >= JPOOL_SIZE(jcf)
+  if (i <= 0 || i >= JPOOL_SIZE (jcf)
       || JPOOL_TAG (jcf, i) != CONSTANT_Class)
-    fatal ("bad class index %d", i);
+    abort ();
   else
     {
       tree this_class;
@@ -444,9 +468,9 @@ tree
 get_class_constant (JCF *jcf , int i)
 {
   tree type;
-  if (i <= 0 || i >= JPOOL_SIZE(jcf)
+  if (i <= 0 || i >= JPOOL_SIZE (jcf)
       || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
-    fatal ("bad class index %d", i);
+    abort ();
 
   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
     {
@@ -454,6 +478,7 @@ get_class_constant (JCF *jcf , int i)
       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
       const char *name = JPOOL_UTF_DATA (jcf, name_index);
       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
+
       if (name[0] == '[')  /* Handle array "classes". */
          type = TREE_TYPE (parse_signature_string (name, nlength));
       else
@@ -479,53 +504,84 @@ read_class (name)
      tree name;
 {
   JCF this_jcf, *jcf;
+  tree icv, class = NULL_TREE;
   tree save_current_class = current_class;
   const char *save_input_filename = input_filename;
   JCF *save_current_jcf = current_jcf;
-  long saved_pos = 0;
-  if (current_jcf->read_state)
-    saved_pos = ftell (current_jcf->read_state);
 
-  push_obstacks (&permanent_obstack, &permanent_obstack);
+  if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
+    {
+      class = TREE_TYPE (icv);
+      jcf = TYPE_JCF (class);
+    }
+  else
+    jcf = NULL;
 
-  /* Search in current zip first.  */
-  if (find_in_current_zip (IDENTIFIER_POINTER (name), &jcf) == 0)
+  if (jcf == NULL)
     {
+      this_jcf.zipd = NULL;
+      jcf = &this_jcf;
       if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
                      &this_jcf, 1) == 0)
+       return 0;
+    }
+
+  current_jcf = jcf;
+
+  if (current_jcf->java_source)
+    {
+      const char *filename = current_jcf->filename;
+      tree file;
+      FILE *finput;
+      int generate;
+
+      java_parser_context_save_global ();
+      java_push_parser_context ();
+      BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
+      generate = IS_A_COMMAND_LINE_FILENAME_P (file);
+      if (wfl_operator == NULL_TREE)
+       wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
+      EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
+      input_filename = ggc_strdup (filename);
+      current_class = NULL_TREE;
+      current_function_decl = NULL_TREE;
+      if (!HAS_BEEN_ALREADY_PARSED_P (file))
        {
-         pop_obstacks ();      /* FIXME: one pop_obstack() per function */
-         return 0;
+         if (!(finput = fopen (input_filename, "r")))
+           fatal_io_error ("can't reopen %s", input_filename);
+         parse_source_file_1 (file, finput);
+         parse_source_file_2 ();
+         parse_source_file_3 ();
+         if (fclose (finput))
+           fatal_io_error ("can't close %s", input_filename);
        }
-      else
+      JCF_FINISH (current_jcf);
+      java_pop_parser_context (generate);
+      java_parser_context_restore_global ();
+    }
+  else
+    {
+      if (class == NULL_TREE || ! CLASS_PARSED_P (class))
        {
-         this_jcf.seen_in_zip = 0;
-         current_jcf = &this_jcf;
+         java_parser_context_save_global ();
+         java_push_parser_context ();
+         current_class = class;
+         input_filename = current_jcf->filename;
+         if (JCF_SEEN_IN_ZIP (current_jcf))
+           read_zip_member(current_jcf,
+                           current_jcf->zipd, current_jcf->zipd->zipf);
+         jcf_parse (current_jcf);
+         class = current_class;
+         java_pop_parser_context (0);
+         java_parser_context_restore_global ();
        }
+      layout_class (class);
+      load_inner_classes (class);
     }
-  else
-    current_jcf = jcf;
-
-  if (current_jcf->java_source)
-    jcf_parse_source ();
-  else {
-    java_parser_context_save_global ();
-    java_push_parser_context ();
-    input_filename = current_jcf->filename;
-    jcf_parse (current_jcf);
-    java_pop_parser_context (0);
-    java_parser_context_restore_global ();
-  }
-
-  if (!current_jcf->seen_in_zip)
-    JCF_FINISH (current_jcf);
-  pop_obstacks ();
 
   current_class = save_current_class;
   input_filename = save_input_filename;
   current_jcf = save_current_jcf;
-  if (current_jcf->read_state)
-    fseek (current_jcf->read_state, saved_pos, SEEK_SET);
   return 1;
 }
 
@@ -541,7 +597,8 @@ load_class (class_or_name, verbose)
      tree class_or_name;
      int verbose;
 {
-  tree name;
+  tree name, saved;
+  int class_loaded;
 
   /* class_or_name can be the name of the class we want to load */
   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
@@ -554,35 +611,31 @@ load_class (class_or_name, verbose)
   else
     name = DECL_NAME (TYPE_NAME (class_or_name));
 
-  if (read_class (name) == 0 && verbose)
-    fatal ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
-}
+  saved = name;
+  while (1)
+    {
+      char *separator;
 
-/* Parse a source file when JCF refers to a source file.  */
+      if ((class_loaded = read_class (name)))
+       break;
 
-static void
-jcf_parse_source ()
-{
-  tree file;
-
-  java_parser_context_save_global ();
-  java_push_parser_context ();
-  input_filename = current_jcf->filename;
-  file = get_identifier (input_filename);
-  current_class = NULL_TREE;
-  current_function_decl = NULL_TREE;
-  if (!HAS_BEEN_ALREADY_PARSED_P (file))
-    {
-      if (!(finput = fopen (input_filename, "r")))
-       fatal ("input file `%s' just disappeared - jcf_parse_source",
-              input_filename);
-      parse_source_file (file);
-      if (fclose (finput))
-       fatal ("can't close input file `%s' stream - jcf_parse_source",
-              input_filename);
+      /* We failed loading name. Now consider that we might be looking
+        for a inner class. */
+      if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
+         || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
+       {
+         int c = *separator;
+         *separator = '\0';
+         name = get_identifier (IDENTIFIER_POINTER (name));
+         *separator = c;
+       }
+      /* Otherwise, we failed, we bail. */
+      else
+       break;
     }
-  java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
-  java_parser_context_restore_global ();
+
+  if (!class_loaded && verbose)
+    error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
 }
 
 /* Parse the .class file JCF. */
@@ -592,16 +645,15 @@ jcf_parse (jcf)
      JCF* jcf;
 {
   int i, code;
-  tree current;
 
   if (jcf_parse_preamble (jcf) != 0)
-    fatal ("Not a valid Java .class file.\n");
+    fatal_error ("not a valid Java .class file");
   code = jcf_parse_constant_pool (jcf);
   if (code != 0)
-    fatal ("error while parsing constant pool");
+    fatal_error ("error while parsing constant pool");
   code = verify_constant_pool (jcf);
   if (code > 0)
-    fatal ("error in constant pool entry #%d\n", code);
+    fatal_error ("error in constant pool entry #%d\n", code);
 
   jcf_parse_class (jcf);
   if (main_class == NULL_TREE)
@@ -610,9 +662,14 @@ jcf_parse (jcf)
     fprintf (stderr, " %s %s",
             (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class", 
             IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
-  if (CLASS_LOADED_P (current_class))
-    return;
-  CLASS_LOADED_P (current_class) = 1;
+  if (CLASS_PARSED_P (current_class))
+    {
+      /* FIXME - where was first time */
+      fatal_error ("reading class %s for the second time from %s",
+                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
+                  jcf->filename);
+    }
+  CLASS_PARSED_P (current_class) = 1;
 
   for (i = 1; i < JPOOL_SIZE(jcf); i++)
     {
@@ -626,32 +683,48 @@ jcf_parse (jcf)
   
   code = jcf_parse_fields (jcf);
   if (code != 0)
-    fatal ("error while parsing fields");
+    fatal_error ("error while parsing fields");
   code = jcf_parse_methods (jcf);
   if (code != 0)
-    fatal ("error while parsing methods");
+    fatal_error ("error while parsing methods");
   code = jcf_parse_final_attributes (jcf);
   if (code != 0)
-    fatal ("error while parsing final attributes");
+    fatal_error ("error while parsing final attributes");
 
   /* The fields of class_type_node are already in correct order. */
   if (current_class != class_type_node && current_class != object_type_node)
     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
 
-  push_obstacks (&permanent_obstack, &permanent_obstack);
-  layout_class (current_class);
   if (current_class == object_type_node)
-    layout_class_methods (object_type_node);
+    {
+      layout_class_methods (object_type_node);
+      /* If we don't have the right archive, emit a verbose warning.
+        If we're generating bytecode, emit the warning only if
+        -fforce-classes-archive-check was specified. */
+      if (!jcf->right_zip
+         && (!flag_emit_class_files || flag_force_classes_archive_check))
+       fatal_error ("the `java.lang.Object' that was found in `%s' didn't have the special zero-length `gnu.gcj.gcj-compiled' attribute.  This generally means that your classpath is incorrectly set.  Use `info gcj \"Input Options\"' to see the info page describing how to set the classpath", jcf->filename);
+    }
   else
-    all_class_list = tree_cons (NULL_TREE, 
+    all_class_list = tree_cons (NULL_TREE,
                                TYPE_NAME (current_class), all_class_list );
+}
 
-  /* And if we came accross inner classes, load them now. */
-  for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (current_class)); current;
+/* If we came across inner classes, load them now. */
+static void
+load_inner_classes (cur_class)
+     tree cur_class;
+{
+  tree current;
+  for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
        current = TREE_CHAIN (current))
-    load_class (DECL_NAME (TREE_PURPOSE (current)), 1);
-
-  pop_obstacks ();
+    {
+      tree name = DECL_NAME (TREE_PURPOSE (current));
+      tree decl = IDENTIFIER_GLOBAL_VALUE (name);
+      if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
+         && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
+       load_class (name, 1);
+    }
 }
 
 void
@@ -659,13 +732,13 @@ init_outgoing_cpool ()
 {
   current_constant_pool_data_ref = NULL_TREE;
   outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
-  bzero (outgoing_cpool, sizeof (struct CPool));
+  memset (outgoing_cpool, 0, sizeof (struct CPool));
 }
 
 static void
 parse_class_file ()
 {
-  tree method;
+  tree method, field;
   const char *save_input_filename = input_filename;
   int save_lineno = lineno;
 
@@ -673,15 +746,20 @@ parse_class_file ()
 
   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
   lineno = 0;
-  debug_start_source_file (input_filename);
+  (*debug_hooks->start_source_file) (lineno, input_filename);
   init_outgoing_cpool ();
 
   /* Currently we always have to emit calls to _Jv_InitClass when
      compiling from class files.  */
   always_initialize_class_p = 1;
 
-  for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
-       method != NULL_TREE; method = TREE_CHAIN (method))
+  for (field = TYPE_FIELDS (CLASS_TO_HANDLE_TYPE (current_class));
+       field != NULL_TREE; field = TREE_CHAIN (field))
+    if (FIELD_STATIC (field))
+      DECL_EXTERNAL (field) = 0;
+
+  for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
+       method != NULL_TREE; method = TREE_CHAIN (method))
     {
       JCF *jcf = current_jcf;
 
@@ -690,10 +768,21 @@ parse_class_file ()
 
       if (METHOD_NATIVE (method))
        {
+         tree arg;
+         int  decl_max_locals;
+
          if (! flag_jni)
            continue;
-         DECL_MAX_LOCALS (method)
-           = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
+         /* We need to compute the DECL_MAX_LOCALS. We need to take
+             the wide types into account too. */
+         for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0; 
+              arg != end_params_node;
+              arg = TREE_CHAIN (arg), decl_max_locals += 1)
+           {
+             if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
+               decl_max_locals += 1;
+           }
+         DECL_MAX_LOCALS (method) = decl_max_locals;
          start_java_method (method);
          give_name_to_locals (jcf);
          expand_expr_stmt (build_jni_stub (method));
@@ -703,6 +792,7 @@ parse_class_file ()
 
       if (DECL_CODE_OFFSET (method) == 0)
        {
+         current_function_decl = method;
          error ("missing Code attribute");
          continue;
        }
@@ -733,6 +823,8 @@ parse_class_file ()
 
       start_java_method (method);
 
+      note_instructions (jcf, method);
+
       give_name_to_locals (jcf);
 
       /* Actually generate code. */
@@ -746,7 +838,7 @@ parse_class_file ()
 
   finish_class ();
 
-  debug_end_source_file (save_lineno);
+  (*debug_hooks->end_source_file) (save_lineno);
   input_filename = save_input_filename;
   lineno = save_lineno;
 }
@@ -754,8 +846,9 @@ parse_class_file ()
 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
 
 static void
-parse_source_file (file)
+parse_source_file_1 (file, finput)
      tree file;
+     FILE *finput;
 {
   int save_error_count = java_error_count;
   /* Mark the file as parsed */
@@ -765,13 +858,39 @@ parse_source_file (file)
 
   lang_init_source (1);                    /* Error msgs have no method prototypes */
 
-  java_init_lex ();                /* Initialize the parser */
+  /* There's no point in trying to find the current encoding unless we
+     are going to do something intelligent with it -- hence the test
+     for iconv.  */
+#if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
+  setlocale (LC_CTYPE, "");
+  if (current_encoding == NULL)
+    current_encoding = nl_langinfo (CODESET);
+#endif 
+  if (current_encoding == NULL || *current_encoding == '\0')
+    current_encoding = DEFAULT_ENCODING;
+
+  /* Initialize the parser */
+  java_init_lex (finput, current_encoding);
   java_parse_abort_on_error ();
 
   java_parse ();                   /* Parse and build partial tree nodes. */
   java_parse_abort_on_error ();
+}
+
+/* Process a parsed source file, resolving names etc. */
+
+static void
+parse_source_file_2 ()
+{
+  int save_error_count = java_error_count;
   java_complete_class ();          /* Parse unsatisfied class decl. */
   java_parse_abort_on_error ();
+}
+
+static void
+parse_source_file_3 ()
+{
+  int save_error_count = java_error_count;
   java_check_circular_reference (); /* Check on circular references */
   java_parse_abort_on_error ();
   java_fix_constructors ();        /* Fix the constructors */
@@ -779,31 +898,98 @@ parse_source_file (file)
   java_reorder_fields ();          /* Reorder the fields */
 }
 
-static int
+void
+add_predefined_file (name)
+     tree name;
+{
+  predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
+}
+
+int
 predefined_filename_p (node)
      tree node;
 {
-  int i;
-  for (i = 0; i < predef_filenames_size; i++)
-    if (predef_filenames [i] == node)
-      return 1;
+  tree iter;
+
+  for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
+    {
+      if (TREE_VALUE (iter) == node)
+       return 1;
+    }
   return 0;
 }
 
-int
-yyparse ()
+void
+java_parse_file ()
 {
-  int several_files = 0;
-  char *list = xstrdup (input_filename), *next;
-  tree node, current_file_list = NULL_TREE;
+  int filename_count = 0;
+  char *list, *next;
+  tree node;
+  FILE *finput = NULL;
+
+  if (flag_filelist_file)
+    {
+      int avail = 2000;
+      finput = fopen (input_filename, "r");
+      if (finput == NULL)
+       fatal_io_error ("can't open %s", input_filename);
+      list = xmalloc(avail);
+      next = list;
+      for (;;)
+       {
+         int count;
+         if (avail < 500)
+           {
+             count = next - list;
+             avail = 2 * (count + avail);
+             list = xrealloc (list, avail);
+             next = list + count;
+             avail = avail - count;
+           }
+         /* Subtract to to guarantee space for final '\0'. */
+         count = fread (next, 1, avail - 1, finput);
+         if (count == 0)
+           {
+             if (! feof (finput))
+               fatal_io_error ("error closing %s", input_filename);
+             *next = '\0';
+             break;
+           }
+         avail -= count;
+         next += count;
+       }
+      fclose (finput);
+      finput = NULL;
+    }
+  else
+    list = xstrdup (input_filename);
 
   do 
     {
-      next = strchr (list, '&');
-      if (next)
+      for (next = list; ; )
        {
-         *next++ = '\0';
-         several_files = 1;
+         char ch = *next;
+         if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
+             || ch == '&' /* FIXME */)
+           {
+             if (next == list)
+               {
+                 next++;
+                 list = next;
+                 continue;
+               }
+             else
+               {
+                 *next++ = '\0';
+                 break;
+               }
+           }
+         if (ch == '\0')
+           {
+             next = NULL;
+             break;
+           }
+         next++;
        }
 
       if (list[0]) 
@@ -814,12 +1000,14 @@ yyparse ()
 
          int len = strlen (list);
 
-         if (*list != '/' && several_files)
+         if (*list != '/' && filename_count > 0)
            obstack_grow (&temporary_obstack, "./", 2);
 
          obstack_grow0 (&temporary_obstack, list, len);
          value = obstack_finish (&temporary_obstack);
 
+         filename_count++;
+
          /* Exclude file that we see twice on the command line. For
             all files except {Class,Error,Object,RuntimeException,String,
             Throwable}.java we can rely on maybe_get_identifier. For
@@ -844,12 +1032,12 @@ yyparse ()
            {
              const char *saved_input_filename = input_filename;
              input_filename = value;
-             warning ("source file seen twice on command line and will be compiled only once.");
+             warning ("source file seen twice on command line and will be compiled only once");
              input_filename = saved_input_filename;
            }
          else
            {
-             node = get_identifier (value);
+             BUILD_FILENAME_IDENTIFIER_NODE (node, value);
              IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
              current_file_list = tree_cons (NULL_TREE, node, 
                                             current_file_list);
@@ -859,10 +1047,28 @@ yyparse ()
     }
   while (next);
 
+  if (filename_count == 0)
+    warning ("no input file specified");
+
+  if (resource_name)
+    {
+      const char *resource_filename;
+      
+      /* Only one resource file may be compiled at a time.  */
+      assert (TREE_CHAIN (current_file_list) == NULL);
+
+      resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
+      compile_resource_file (resource_name, resource_filename);
+
+      return;
+    }
+
   current_jcf = main_jcf;
   current_file_list = nreverse (current_file_list);
   for (node = current_file_list; node; node = TREE_CHAIN (node))
     {
+      unsigned char magic_string[4];
+      uint32 magic = 0;
       tree name = TREE_VALUE (node);
 
       /* Skip already parsed files */
@@ -870,52 +1076,101 @@ yyparse ()
        continue;
       
       /* Close previous descriptor, if any */
-      if (main_jcf->read_state && fclose (main_jcf->read_state))
-       fatal ("failed to close input file `%s' - yyparse",
-              (main_jcf->filename ? main_jcf->filename : "<unknown>"));
+      if (finput && fclose (finput))
+       fatal_io_error ("can't close input file %s", main_input_filename);
       
-      /* Set jcf up and open a new file */
-      JCF_ZERO (main_jcf);
-      main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
-      if (main_jcf->read_state == NULL)
-       pfatal_with_name (IDENTIFIER_POINTER (name));
+      finput = fopen (IDENTIFIER_POINTER (name), "rb");
+      if (finput == NULL)
+       fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
       
-      /* Set new input_filename and finput */
-      finput = main_jcf->read_state;
 #ifdef IO_BUFFER_SIZE
       setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
               _IOFBF, IO_BUFFER_SIZE);
 #endif
       input_filename = IDENTIFIER_POINTER (name);
-      main_jcf->filbuf = jcf_filbuf_from_stdio;
 
-      switch (jcf_figure_file_type (current_jcf))
+      /* Figure what kind of file we're dealing with */
+      if (fread (magic_string, 1, 4, finput) == 4)
        {
-       case JCF_ZIP:
-         parse_zip_file_entries ();
-         break;
-       case JCF_CLASS:
+         fseek (finput, 0L, SEEK_SET);
+         magic = GET_u4 (magic_string);
+       }
+      if (magic == 0xcafebabe)
+       {
+         CLASS_FILE_P (node) = 1;
+         current_jcf = ALLOC (sizeof (JCF));
+         JCF_ZERO (current_jcf);
+         current_jcf->read_state = finput;
+         current_jcf->filbuf = jcf_filbuf_from_stdio;
          jcf_parse (current_jcf);
-         parse_class_file ();
-         break;
-       case JCF_SOURCE:
+         TYPE_JCF (current_class) = current_jcf;
+         CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
+         TREE_PURPOSE (node) = current_class;
+       }
+      else if (magic == (JCF_u4)ZIPMAGIC)
+       {
+         ZIP_FILE_P (node) = 1;
+         JCF_ZERO (main_jcf);
+         main_jcf->read_state = finput;
+         main_jcf->filbuf = jcf_filbuf_from_stdio;
+         if (open_in_zip (main_jcf, input_filename, NULL, 0) <  0)
+           fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
+         localToFile = SeenZipFiles;
+         /* Register all the class defined there.  */
+         process_zip_dir (main_jcf->read_state);
+         parse_zip_file_entries ();
+         /*
+         for (each entry)
+           CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
+         */
+       }
+      else
+       {
+         JAVA_FILE_P (node) = 1;
          java_push_parser_context ();
          java_parser_context_save_global ();
-         parse_source_file (name);
+         parse_source_file_1 (name, finput);
          java_parser_context_restore_global ();
          java_pop_parser_context (1);
-         break;
        }
     }
 
+  for (ctxp = ctxp_for_generation;  ctxp;  ctxp = ctxp->next)
+    {
+      input_filename = ctxp->filename;
+      parse_source_file_2 ();
+    }
+
+  for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
+    {
+      input_filename = ctxp->filename;
+      parse_source_file_3 ();
+    }
+
+  for (node = current_file_list; node; node = TREE_CHAIN (node))
+    {
+      input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
+      if (CLASS_FILE_P (node))
+       {
+         current_class = TREE_PURPOSE (node);
+         current_jcf = TYPE_JCF (current_class);
+         layout_class (current_class);
+         load_inner_classes (current_class);
+         parse_class_file ();
+         JCF_FINISH (current_jcf);
+       }
+    }
+  input_filename = main_input_filename;
+
   java_expand_classes ();
   if (!java_report_errors () && !flag_syntax_only)
-    emit_register_classes ();
-  return 0;
+    {
+      emit_register_classes ();
+      if (flag_indirect_dispatch)
+       emit_offset_symbol_table ();
+    }
 }
 
-static struct ZipFileCache *localToFile;
-
 /* Process all class entries found in the zip file.  */
 static void
 parse_zip_file_entries (void)
@@ -923,8 +1178,8 @@ parse_zip_file_entries (void)
   struct ZipDirectory *zdir;
   int i;
 
-  for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
-       i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
+  for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
+       i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
     {
       tree class;
       
@@ -938,8 +1193,13 @@ parse_zip_file_entries (void)
 
       if ( !CLASS_LOADED_P (class))
        {
-          fseek (current_jcf->read_state, current_jcf->zip_offset, SEEK_SET);
-         jcf_parse (current_jcf);
+         if (! CLASS_PARSED_P (class))
+           {
+             read_zip_member(current_jcf, zdir, localToFile);
+             jcf_parse (current_jcf);
+           }
+         layout_class (current_class);
+         load_inner_classes (current_class);
        }
 
       if (TYPE_SIZE (current_class) != error_mark_node)
@@ -958,13 +1218,14 @@ parse_zip_file_entries (void)
 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
    jcf up for further processing and link it to the created class.  */
 
-static void process_zip_dir()
+static void
+process_zip_dir (FILE *finput)
 {
   int i;
   ZipDirectory *zdir;
 
-  for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
-       i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
+  for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
+       i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
     {
       char *class_name, *file_name, *class_name_in_zip_dir;
       tree class;
@@ -974,7 +1235,7 @@ static void process_zip_dir()
       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
 
       /* We choose to not to process entries with a zero size or entries
-        not bearing the .class extention.  */
+        not bearing the .class extension.  */
       if (!zdir->size || !zdir->filename_offset ||
          strncmp (&class_name_in_zip_dir[zdir->filename_length-6], 
                   ".class", 6))
@@ -1004,69 +1265,24 @@ static void process_zip_dir()
 
       jcf->read_state  = finput;
       jcf->filbuf      = jcf_filbuf_from_stdio;
-      jcf->seen_in_zip = 1;
       jcf->java_source = 0;
-      jcf->zip_offset  = zdir->filestart;
       jcf->classname   = class_name;
       jcf->filename    = file_name;
+      jcf->zipd        = zdir;
 
       TYPE_JCF (class) = jcf;
     }
 }
 
-/* Lookup class NAME and figure whether is a class already found in the current
-   zip file.  */
-static int
-DEFUN(find_in_current_zip, (name, length, jcf),
-      const char *name AND JCF **jcf)
-{
-  JCF *local_jcf;
-  tree class_name = maybe_get_identifier (name), class, icv;
-
-  if (!class_name)
-    return 0;
-
-  if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
-    return 0;
+/* Initialization.  */
 
-  class = TREE_TYPE (icv);
-
-  /* Doesn't have jcf specific info ? It's not ours */
-  if (!TYPE_JCF (class))
-    return 0;
-
-  *jcf = local_jcf = TYPE_JCF (class);
-  fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
-  return 1;
-}
-
-/* Figure what kind of file we're dealing with */
-static int
-DEFUN(jcf_figure_file_type, (jcf),
-      JCF *jcf)
+void
+init_jcf_parse ()
 {
-  unsigned char magic_string[4];
-  uint32 magic;
+  /* Register roots with the garbage collector.  */
+  ggc_add_tree_root (parse_roots, ARRAY_SIZE (parse_roots));
 
-  if (fread (magic_string, 1, 4, jcf->read_state) != 4)
-    jcf_unexpected_eof (jcf, 4);
+  ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
 
-  fseek (jcf->read_state, 0L, SEEK_SET);
-  magic = GET_u4 (magic_string);
-
-  if (magic == 0xcafebabe)
-    return JCF_CLASS;
-
-  /* FIXME: is it a system file?  */
-  if (magic ==  (JCF_u4)ZIPMAGIC
-      && !open_in_zip (jcf, input_filename, NULL, 0))
-    {
-      localToFile = ALLOC (sizeof (struct ZipFileCache));
-      bcopy ((PTR) SeenZipFiles, (PTR) localToFile, sizeof (struct ZipFileCache));
-      process_zip_dir ();      /* Register all the class defined there */
-      return JCF_ZIP;
-    }
-
-  return JCF_SOURCE;
+  init_src_parse ();
 }
-
This page took 0.057058 seconds and 5 git commands to generate.