OSDN Git Service

* java-tree.h (all_class_filename): Remove useless macro.
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-parse.c
index 20dc3ff..07f8ccc 100644 (file)
@@ -1,5 +1,5 @@
 /* Parser for Java(TM) .class files.
-   Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003
+   Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -42,12 +42,13 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 #include "debug.h"
 #include "assert.h"
 #include "tm_p.h"
+#include "cgraph.h"
 
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
 #endif
 
-#ifdef HAVE_NL_LANGINFO
+#ifdef HAVE_LANGINFO_CODESET
 #include <langinfo.h>
 #endif
 
@@ -71,10 +72,6 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 
 extern struct obstack temporary_obstack;
 
-/* Set to nonzero value in order to emit class initialization code
-   before static field references.  */
-extern int always_initialize_class_p;
-
 static GTY(()) tree parse_roots[3];
 
 /* The FIELD_DECL for the current field.  */
@@ -102,10 +99,28 @@ static void parse_source_file_1 (tree, FILE *);
 static void parse_source_file_2 (void);
 static void parse_source_file_3 (void);
 static void parse_class_file (void);
+static void handle_deprecated (void);
 static void set_source_filename (JCF *, int);
 static void jcf_parse (struct JCF*);
 static void load_inner_classes (tree);
 
+/* Handle "Deprecated" attribute.  */
+static void
+handle_deprecated (void)
+{
+  if (current_field != NULL_TREE)
+    FIELD_DEPRECATED (current_field) = 1;
+  else if (current_method != NULL_TREE)
+    METHOD_DEPRECATED (current_method) = 1;
+  else if (current_class != NULL_TREE)
+    CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
+  else
+    {
+      /* Shouldn't happen.  */
+      abort ();
+    }
+}
+
 /* Handle "SourceFile" attribute. */
 
 static void
@@ -134,7 +149,7 @@ set_source_filename (JCF *jcf, int index)
 
 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
-  current_class = give_name_to_class (jcf, THIS); \
+  output_class = current_class = give_name_to_class (jcf, THIS); \
   set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
 
 #define HANDLE_CLASS_INTERFACE(INDEX) \
@@ -200,6 +215,8 @@ set_source_filename (JCF *jcf, int index)
   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
 }
 
+#define HANDLE_DEPRECATED_ATTRIBUTE()  handle_deprecated ()
+
 /* 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) \
@@ -398,7 +415,7 @@ give_name_to_class (JCF *jcf, int i)
                                            JPOOL_UTF_LENGTH (jcf, j));
       this_class = lookup_class (class_name);
       input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
-      lineno = 0;
+      input_line = 0;
       if (main_input_filename == NULL && jcf == main_jcf)
        main_input_filename = input_filename;
 
@@ -451,7 +468,8 @@ read_class (tree name)
   JCF this_jcf, *jcf;
   tree icv, class = NULL_TREE;
   tree save_current_class = current_class;
-  const char *save_input_filename = input_filename;
+  tree save_output_class = output_class;
+  location_t save_location = input_location;
   JCF *save_current_jcf = current_jcf;
 
   if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
@@ -476,29 +494,35 @@ read_class (tree name)
   if (current_jcf->java_source)
     {
       const char *filename = current_jcf->filename;
-      tree file;
+      tree given_file, real_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);
+
+      given_file = get_identifier (filename);
+      real_file = get_identifier (lrealpath (filename));
+
+      generate = IS_A_COMMAND_LINE_FILENAME_P (given_file);
       if (wfl_operator == NULL_TREE)
        wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
-      EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
+      EXPR_WFL_FILENAME_NODE (wfl_operator) = given_file;
       input_filename = ggc_strdup (filename);
-      current_class = NULL_TREE;
+      output_class = current_class = NULL_TREE;
       current_function_decl = NULL_TREE;
-      if (!HAS_BEEN_ALREADY_PARSED_P (file))
+
+      if (! HAS_BEEN_ALREADY_PARSED_P (real_file))
        {
-         if (!(finput = fopen (input_filename, "r")))
-           fatal_io_error ("can't reopen %s", input_filename);
-         parse_source_file_1 (file, finput);
+         if (! (finput = fopen (input_filename, "r")))
+           fatal_error ("can't reopen %s: %m", input_filename);
+
+         parse_source_file_1 (real_file, finput);
          parse_source_file_2 ();
          parse_source_file_3 ();
+
          if (fclose (finput))
-           fatal_io_error ("can't close %s", input_filename);
+           fatal_error ("can't close %s: %m", input_filename);
        }
       JCF_FINISH (current_jcf);
       java_pop_parser_context (generate);
@@ -510,7 +534,7 @@ read_class (tree name)
        {
          java_parser_context_save_global ();
          java_push_parser_context ();
-         current_class = class;
+         output_class = current_class = class;
          input_filename = current_jcf->filename;
          if (JCF_SEEN_IN_ZIP (current_jcf))
            read_zip_member(current_jcf,
@@ -528,8 +552,9 @@ read_class (tree name)
       load_inner_classes (class);
     }
 
+  output_class = save_output_class;
   current_class = save_current_class;
-  input_filename = save_input_filename;
+  input_location = save_location;
   current_jcf = save_current_jcf;
   return 1;
 }
@@ -537,10 +562,6 @@ read_class (tree name)
 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
    called from the parser, otherwise it's a RECORD_TYPE node. If
    VERBOSE is 1, print error message on failure to load a class. */
-
-/* Replace calls to load_class by having callers call read_class directly
-   - and then perhaps rename read_class to load_class.  FIXME */
-
 void
 load_class (tree class_or_name, int verbose)
 {
@@ -556,7 +577,12 @@ load_class (tree class_or_name, int verbose)
     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
   /* Or it's a type in the making */
   else
-    name = DECL_NAME (TYPE_NAME (class_or_name));
+    {
+      /* If the class is from source code, then it must already be loaded.  */
+      if (CLASS_FROM_SOURCE_P (class_or_name))
+        return;
+      name = DECL_NAME (TYPE_NAME (class_or_name));
+    }
 
   saved = name;
   while (1)
@@ -587,7 +613,7 @@ load_class (tree class_or_name, int verbose)
 
 /* Parse the .class file JCF. */
 
-void
+static void
 jcf_parse (JCF* jcf)
 {
   int i, code;
@@ -672,34 +698,25 @@ load_inner_classes (tree cur_class)
     }
 }
 
-void
-init_outgoing_cpool (void)
-{
-  outgoing_cpool = ggc_alloc_cleared (sizeof (struct CPool));
-}
-
 static void
 parse_class_file (void)
 {
-  tree method, field;
-  const char *save_input_filename = input_filename;
-  int save_lineno = lineno;
+  tree method;
+  location_t save_location = input_location;
 
   java_layout_seen_class_methods ();
 
   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
-  lineno = 0;
-  (*debug_hooks->start_source_file) (lineno, input_filename);
-  init_outgoing_cpool ();
+  input_line = 0;
+  (*debug_hooks->start_source_file) (input_line, input_filename);
 
   /* Currently we always have to emit calls to _Jv_InitClass when
      compiling from class files.  */
   always_initialize_class_p = 1;
 
-  for (field = TYPE_FIELDS (current_class);
-       field != NULL_TREE; field = TREE_CHAIN (field))
-    if (FIELD_STATIC (field))
-      DECL_EXTERNAL (field) = 0;
+  gen_indirect_dispatch_tables (current_class);
+
+  java_mark_class_local (current_class);
 
   for (method = TYPE_METHODS (current_class);
        method != NULL_TREE; method = TREE_CHAIN (method))
@@ -728,7 +745,7 @@ parse_class_file (void)
          DECL_MAX_LOCALS (method) = decl_max_locals;
          start_java_method (method);
          give_name_to_locals (jcf);
-         expand_expr_stmt (build_jni_stub (method));
+         *get_stmts () = build_jni_stub (method);
          end_java_method ();
          continue;
        }
@@ -740,11 +757,11 @@ parse_class_file (void)
          continue;
        }
 
-      lineno = 0;
+      input_line = 0;
       if (DECL_LINENUMBERS_OFFSET (method))
        {
-         register int i;
-         register unsigned char *ptr;
+         int i;
+         unsigned char *ptr;
          JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
          linenumber_count = i = JCF_readu2 (jcf);
          linenumber_table = ptr = jcf->read_ptr;
@@ -752,10 +769,10 @@ parse_class_file (void)
          for (ptr += 2; --i >= 0; ptr += 4)
            {
              int line = GET_u2 (ptr);
-             /* Set initial lineno lineno to smallest linenumber.
+             /* Set initial input_line to smallest linenumber.
               * Needs to be set before init_function_start. */
-             if (lineno == 0 || line < lineno)
-               lineno = line;
+             if (input_line == 0 || line < input_line)
+               input_line = line;
            }  
        }
       else
@@ -770,7 +787,7 @@ parse_class_file (void)
 
       give_name_to_locals (jcf);
 
-      /* Actually generate code. */
+      /* Convert bytecode to trees.  */
       expand_byte_code (jcf, method);
 
       end_java_method ();
@@ -781,19 +798,19 @@ parse_class_file (void)
 
   finish_class ();
 
-  (*debug_hooks->end_source_file) (save_lineno);
-  input_filename = save_input_filename;
-  lineno = save_lineno;
+  (*debug_hooks->end_source_file) (save_location.line);
+  input_location = save_location;
 }
 
 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
 
 static void
-parse_source_file_1 (tree file, FILE *finput)
+parse_source_file_1 (tree real_file, FILE *finput)
 {
   int save_error_count = java_error_count;
-  /* Mark the file as parsed */
-  HAS_BEEN_ALREADY_PARSED_P (file) = 1;
+
+  /* Mark the file as parsed.  */
+  HAS_BEEN_ALREADY_PARSED_P (real_file) = 1;
 
   jcf_dependency_add_file (input_filename, 0);
 
@@ -802,7 +819,7 @@ parse_source_file_1 (tree file, FILE *finput)
   /* 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)
+#if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
   setlocale (LC_CTYPE, "");
   if (current_encoding == NULL)
     current_encoding = nl_langinfo (CODESET);
@@ -858,6 +875,21 @@ predefined_filename_p (tree node)
   return 0;
 }
 
+/* Generate a function that does all static initialization for this 
+   translation unit.  */
+
+static void
+java_emit_static_constructor (void)
+{
+  tree body = NULL;
+
+  emit_register_classes (&body);
+  write_resource_constructor (&body);
+
+  if (body)
+    cgraph_build_static_cdtor ('I', body);
+}
+
 void
 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
 {
@@ -871,7 +903,7 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
       int avail = 2000;
       finput = fopen (input_filename, "r");
       if (finput == NULL)
-       fatal_io_error ("can't open %s", input_filename);
+       fatal_error ("can't open %s: %m", input_filename);
       list = xmalloc(avail);
       next = list;
       for (;;)
@@ -890,7 +922,7 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
          if (count == 0)
            {
              if (! feof (finput))
-               fatal_io_error ("error closing %s", input_filename);
+               fatal_error ("error closing %s: %m", input_filename);
              *next = '\0';
              break;
            }
@@ -901,9 +933,9 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
       finput = NULL;
     }
   else
-    list = xstrdup (input_filename);
+    list = input_filename ? xstrdup (input_filename) : 0;
 
-  do 
+  while (list)
     {
       for (next = list; ; )
        {
@@ -966,14 +998,15 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
 
          if (twice)
            {
-             const char *saved_input_filename = input_filename;
-             input_filename = value;
-             warning ("source file seen twice on command line and will be compiled only once");
-             input_filename = saved_input_filename;
+             location_t warn_loc;
+             warn_loc.file = value;
+             warn_loc.line = 0;
+             warning ("%Hsource file seen twice on command line and "
+                      "will be compiled only once", &warn_loc);
            }
          else
            {
-             BUILD_FILENAME_IDENTIFIER_NODE (node, value);
+             node = get_identifier (value);
              IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
              current_file_list = tree_cons (NULL_TREE, node, 
                                             current_file_list);
@@ -981,7 +1014,6 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
        }
       list = next;
     }
-  while (next);
 
   if (filename_count == 0)
     warning ("no input file specified");
@@ -996,7 +1028,7 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
       resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
       compile_resource_file (resource_name, resource_filename);
 
-      return;
+      goto finish;
     }
 
   current_jcf = main_jcf;
@@ -1006,18 +1038,20 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
       unsigned char magic_string[4];
       uint32 magic = 0;
       tree name = TREE_VALUE (node);
+      tree real_file;
 
       /* Skip already parsed files */
-      if (HAS_BEEN_ALREADY_PARSED_P (name))
+      real_file = get_identifier (lrealpath (IDENTIFIER_POINTER (name)));
+      if (HAS_BEEN_ALREADY_PARSED_P (real_file))
        continue;
       
       /* Close previous descriptor, if any */
       if (finput && fclose (finput))
-       fatal_io_error ("can't close input file %s", main_input_filename);
+       fatal_error ("can't close input file %s: %m", main_input_filename);
       
       finput = fopen (IDENTIFIER_POINTER (name), "rb");
       if (finput == NULL)
-       fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
+       fatal_error ("can't open %s: %m", IDENTIFIER_POINTER (name));
 
 #ifdef IO_BUFFER_SIZE
       setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
@@ -1066,7 +1100,8 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
          JAVA_FILE_P (node) = 1;
          java_push_parser_context ();
          java_parser_context_save_global ();
-         parse_source_file_1 (name, finput);
+
+         parse_source_file_1 (real_file, finput);
          java_parser_context_restore_global ();
          java_pop_parser_context (1);
        }
@@ -1089,7 +1124,7 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
       input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
       if (CLASS_FILE_P (node))
        {
-         current_class = TREE_PURPOSE (node);
+         output_class = current_class = TREE_PURPOSE (node);
          current_jcf = TYPE_JCF (current_class);
          layout_class (current_class);
          load_inner_classes (current_class);
@@ -1100,16 +1135,23 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
   input_filename = main_input_filename;
 
   java_expand_classes ();
-  if (!java_report_errors () && !flag_syntax_only)
-    {
-      emit_register_classes ();
-      if (flag_indirect_dispatch)
-       emit_offset_symbol_table ();
-    }
-
-  write_resource_constructor ();
+  if (java_report_errors () || flag_syntax_only)
+    return;
+    
+  /* Expand all classes compiled from source.  */
+  java_finish_classes ();
+
+ finish:
+  /* Arrange for any necessary initialization to happen.  */
+  java_emit_static_constructor ();
+
+  /* Only finalize the compilation unit after we've told cgraph which
+     functions have their addresses stored.  */
+  cgraph_finalize_compilation_unit ();
+  cgraph_optimize ();
 }
 
+
 /* Return the name of the class corresponding to the name of the file
    in this zip entry.  The result is newly allocated using ALLOC.  */
 static char *
@@ -1139,10 +1181,9 @@ classify_zip_file (struct ZipDirectory *zdir)
                   ".class", 6))
     return 1;
 
-  /* For now we drop the manifest and other information.  Maybe it
-     would make more sense to compile it in?  */
-  if (zdir->filename_length > 8
-      && !strncmp (class_name_in_zip_dir, "META-INF/", 9))
+  /* For now we drop the manifest, but not other information.  */
+  if (zdir->filename_length == 20
+      && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
     return 0;
 
   /* Drop directory entries.  */
@@ -1176,7 +1217,7 @@ parse_zip_file_entries (void)
            class = lookup_class (get_identifier (class_name));
            FREE (class_name);
            current_jcf = TYPE_JCF (class);
-           current_class = class;
+           output_class = current_class = class;
 
            if (! CLASS_LOADED_P (class))
              {