OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-reader.c
index 44fb37e..2ac3124 100644 (file)
@@ -1,23 +1,25 @@
 /* This file read a Java(TM) .class file.
    It is not stand-alone:  It depends on tons of macros, and the
    intent is you #include this file after you've defined the macros.
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006
+   Free Software Foundation, Inc.
 
-   Copyright (C) 1996  Free Software Foundation, Inc.
+This file is part of GCC.
 
-This program is free software; you can redistribute it and/or modify
+GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
-This program is distributed in the hope that it will be useful,
+GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  
+along with GCC; see the file COPYING.  If not, write to
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  
 
 Java and all Java-based marks are trademarks or registered trademarks
 of Sun Microsystems, Inc. in the United States and other countries.
@@ -26,18 +28,83 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 #include "jcf.h"
 #include "zipfile.h"
 
-static int get_attribute PROTO((JCF *));
-static int jcf_parse_preamble PROTO((JCF *));
-static int jcf_parse_constant_pool PROTO((JCF *));
-static void jcf_parse_class PROTO((JCF *));
-static int jcf_parse_fields PROTO((JCF *));
-static int jcf_parse_one_method PROTO((JCF *));
-static int jcf_parse_methods PROTO((JCF *));
-static int jcf_parse_final_attributes PROTO((JCF *));
+static int get_attribute (JCF *, int, jv_attr_type);
+static int jcf_parse_preamble (JCF *);
+static int jcf_parse_constant_pool (JCF *);
+static void jcf_parse_class (JCF *);
+static int jcf_parse_fields (JCF *);
+static int jcf_parse_one_method (JCF *, int);
+static int jcf_parse_methods (JCF *);
+static int jcf_parse_final_attributes (JCF *);
+#ifdef NEED_PEEK_ATTRIBUTE
+static int peek_attribute (JCF *, int, const char *, int);
+#endif
+#ifdef NEED_SKIP_ATTRIBUTE
+static void skip_attribute (JCF *, int);
+#endif
+
+/* Go through all available attribute (ATTRIBUTE_NUMER) and try to
+   identify PEEKED_NAME.  Return 1 if PEEKED_NAME was found, 0
+   otherwise. JCF is restored to its initial position before
+   returning.  */
 
+#ifdef NEED_PEEK_ATTRIBUTE     /* Not everyone uses this function */
 static int
-DEFUN(get_attribute, (jcf),
-      JCF *jcf)
+peek_attribute (JCF *jcf, int attribute_number, const char *peeked_name,
+               int peeked_name_length)
+{
+  int to_return = 0;
+  long absolute_offset = (long)JCF_TELL (jcf);
+  int i;
+
+  for (i = 0; !to_return && i < attribute_number; i++)
+    {
+      uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
+      uint32 attribute_length = JCF_readu4 (jcf);
+      int name_length;
+      const unsigned char *name_data; 
+
+      JCF_FILL (jcf, (long) attribute_length);
+      if (attribute_name <= 0 || attribute_name >= JPOOL_SIZE(jcf)
+         || JPOOL_TAG (jcf, attribute_name) != CONSTANT_Utf8)
+       continue;
+
+      name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
+      name_data = JPOOL_UTF_DATA (jcf, attribute_name);
+
+      if (name_length == peeked_name_length 
+         && ! memcmp (name_data, peeked_name, peeked_name_length)) 
+       {
+         to_return = 1; 
+         break;
+       }
+      
+      JCF_SKIP (jcf, attribute_length);
+    }
+
+  JCF_SEEK (jcf, absolute_offset);
+  return to_return;
+}
+#endif
+
+#ifdef NEED_SKIP_ATTRIBUTE     /* Not everyone uses this function */
+static void
+skip_attribute (JCF *jcf, int number_of_attribute)
+{
+  while (number_of_attribute--)
+    {
+      JCF_u4 N;
+      JCF_FILL (jcf, 6);
+      (void) JCF_readu2 (jcf);
+      N = JCF_readu4 (jcf);
+      JCF_SKIP (jcf, N);
+    }
+}
+#endif
+
+static int
+get_attribute (JCF *jcf, int index, 
+              jv_attr_type attr_type ATTRIBUTE_UNUSED)
 {
   uint16 attribute_name = (JCF_FILL (jcf, 6), JCF_readu2 (jcf));
   uint32 attribute_length = JCF_readu4 (jcf);
@@ -52,6 +119,9 @@ DEFUN(get_attribute, (jcf),
   name_length = JPOOL_UTF_LENGTH (jcf, attribute_name);
   name_data = JPOOL_UTF_DATA (jcf, attribute_name);
 
+#define MATCH_ATTRIBUTE(S) \
+  (name_length == sizeof (S)-1 && memcmp (name_data, S, sizeof (S)-1) == 0)
+
 #ifdef IGNORE_ATTRIBUTE
    if (IGNORE_ATTRIBUTE (jcf, attribute_name, attribute_length))
      {
@@ -60,7 +130,7 @@ DEFUN(get_attribute, (jcf),
    else
 #endif
 #ifdef HANDLE_SOURCEFILE
-  if (name_length == 10 && memcmp (name_data, "SourceFile", 10) == 0)
+  if (MATCH_ATTRIBUTE ("SourceFile"))
     {
       uint16 sourcefile_index = JCF_readu2 (jcf);
       HANDLE_SOURCEFILE(sourcefile_index);
@@ -68,7 +138,7 @@ DEFUN(get_attribute, (jcf),
   else
 #endif
 #ifdef HANDLE_CONSTANTVALUE
-  if (name_length == 13 && memcmp (name_data, "ConstantValue", 13) == 0)
+  if (MATCH_ATTRIBUTE ("ConstantValue"))
     {
       uint16 constantvalue_index = JCF_readu2 (jcf);
       if (constantvalue_index <= 0 || constantvalue_index >= JPOOL_SIZE(jcf))
@@ -78,7 +148,7 @@ DEFUN(get_attribute, (jcf),
   else
 #endif
 #ifdef HANDLE_CODE_ATTRIBUTE
-  if (name_length == 4 && memcmp (name_data, "Code", 4) == 0)
+  if (MATCH_ATTRIBUTE ("Code"))
     {
       uint16 j;
       uint16 max_stack ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
@@ -99,7 +169,7 @@ DEFUN(get_attribute, (jcf),
       attributes_count = JCF_readu2 (jcf);
       for (j = 0; j < attributes_count; j++)
        {
-         int code = get_attribute (jcf);
+         int code = get_attribute (jcf, index, JV_METHOD_ATTR);
          if (code != 0)
            return code;
        }
@@ -107,7 +177,7 @@ DEFUN(get_attribute, (jcf),
   else
 #endif /* HANDLE_CODE_ATTRIBUTE */
 #ifdef HANDLE_EXCEPTIONS_ATTRIBUTE
-  if (name_length == 10 && memcmp (name_data, "Exceptions", 10) == 0)
+  if (MATCH_ATTRIBUTE ("Exceptions"))
     {
       uint16 count = JCF_readu2 (jcf);
       HANDLE_EXCEPTIONS_ATTRIBUTE (count);
@@ -115,7 +185,7 @@ DEFUN(get_attribute, (jcf),
   else
 #endif
 #ifdef HANDLE_LINENUMBERTABLE_ATTRIBUTE
-  if (name_length == 15 && memcmp (name_data, "LineNumberTable", 15) == 0)
+  if (MATCH_ATTRIBUTE ("LineNumberTable"))
     {
       uint16 count = JCF_readu2 (jcf);
       HANDLE_LINENUMBERTABLE_ATTRIBUTE (count);
@@ -123,13 +193,106 @@ DEFUN(get_attribute, (jcf),
   else
 #endif
 #ifdef HANDLE_LOCALVARIABLETABLE_ATTRIBUTE
-  if (name_length == 18 && memcmp (name_data, "LocalVariableTable", 18) == 0)
+  if (MATCH_ATTRIBUTE ("LocalVariableTable"))
     {
       uint16 count = JCF_readu2 (jcf);
       HANDLE_LOCALVARIABLETABLE_ATTRIBUTE (count);
     }
   else
 #endif
+#ifdef HANDLE_LOCALVARIABLETYPETABLE_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("LocalVariableTypeTable"))
+    {
+      uint16 count = JCF_readu2 (jcf);
+      HANDLE_LOCALVARIABLETYPETABLE_ATTRIBUTE (count);
+    }
+  else
+#endif
+#ifdef HANDLE_INNERCLASSES_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("InnerClasses"))
+    {
+      uint16 count = JCF_readu2 (jcf);
+      HANDLE_INNERCLASSES_ATTRIBUTE (count);
+    }
+  else
+#endif
+#ifdef HANDLE_SYNTHETIC_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("Synthetic"))
+    {
+      HANDLE_SYNTHETIC_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_GCJCOMPILED_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("gnu.gcj.gcj-compiled"))
+    {
+      HANDLE_GCJCOMPILED_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_DEPRECATED_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("Deprecated"))
+    {
+      HANDLE_DEPRECATED_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("SourceDebugExtension")) /* JSR 45 */
+    {
+      HANDLE_SOURCEDEBUGEXTENSION_ATTRIBUTE (attribute_length);
+    }
+  else
+#endif
+#ifdef HANDLE_ENCLOSINGMETHOD_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("EnclosingMethod"))
+    {
+      HANDLE_ENCLOSINGMETHOD_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_SIGNATURE_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("Signature"))
+    {
+      HANDLE_SIGNATURE_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("RuntimeVisibleAnnotations"))
+    {
+      HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("RuntimeInvisibleAnnotations"))
+    {
+      HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("RuntimeVisibleParameterAnnotations"))
+    {
+      HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("RuntimeInvisibleParameterAnnotations"))
+    {
+      HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE ();
+    }
+  else
+#endif
+#ifdef HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE
+  if (MATCH_ATTRIBUTE ("AnnotationDefault"))
+    {
+      HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE ();
+    }
+  else
+#endif
     {
 #ifdef PROCESS_OTHER_ATTRIBUTE
       PROCESS_OTHER_ATTRIBUTE(jcf, attribute_name, attribute_length);
@@ -144,8 +307,7 @@ DEFUN(get_attribute, (jcf),
 
 /* Read and handle the pre-amble. */
 static int
-DEFUN(jcf_parse_preamble, (jcf),
-      JCF* jcf)
+jcf_parse_preamble (JCF* jcf)
 {
   uint32 magic = (JCF_FILL (jcf, 8), JCF_readu4 (jcf));
   uint16 minor_version ATTRIBUTE_UNUSED = JCF_readu2 (jcf);
@@ -165,13 +327,12 @@ DEFUN(jcf_parse_preamble, (jcf),
    Return -2 if a bad cross-reference (index of other constant) was seen.
 */
 static int
-DEFUN(jcf_parse_constant_pool, (jcf),
-      JCF* jcf)
+jcf_parse_constant_pool (JCF* jcf)
 {
   int i, n;
   JPOOL_SIZE (jcf) = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
-  jcf->cpool.tags = ALLOC (JPOOL_SIZE (jcf));
-  jcf->cpool.data = ALLOC (sizeof (jword) * JPOOL_SIZE (jcf));
+  jcf->cpool.tags = ggc_alloc (JPOOL_SIZE (jcf));
+  jcf->cpool.data = ggc_alloc (sizeof (jword) * JPOOL_SIZE (jcf));
   jcf->cpool.tags[0] = 0;
 #ifdef HANDLE_START_CONSTANT_POOL
   HANDLE_START_CONSTANT_POOL (JPOOL_SIZE (jcf));
@@ -191,25 +352,25 @@ DEFUN(jcf_parse_constant_pool, (jcf),
        {
        case CONSTANT_String:
        case CONSTANT_Class:
-         jcf->cpool.data[i] = JCF_readu2 (jcf);
+         jcf->cpool.data[i].w = JCF_readu2 (jcf);
          break;
        case CONSTANT_Fieldref:
        case CONSTANT_Methodref:
        case CONSTANT_InterfaceMethodref:
        case CONSTANT_NameAndType:
-         jcf->cpool.data[i] = JCF_readu2 (jcf);
-         jcf->cpool.data[i] |= JCF_readu2 (jcf) << 16;
+         jcf->cpool.data[i].w = JCF_readu2 (jcf);
+         jcf->cpool.data[i].w |= JCF_readu2 (jcf) << 16;
          break;
        case CONSTANT_Integer:
        case CONSTANT_Float:
-         jcf->cpool.data[i] = JCF_readu4 (jcf);
+         jcf->cpool.data[i].w = JCF_readu4 (jcf);
          break;
        case CONSTANT_Long:
        case CONSTANT_Double:
-         jcf->cpool.data[i] = JCF_readu4 (jcf);
+         jcf->cpool.data[i].w = JCF_readu4 (jcf);
          i++; /* These take up two spots in the constant pool */
          jcf->cpool.tags[i] = 0;
-         jcf->cpool.data[i] = JCF_readu4 (jcf);
+         jcf->cpool.data[i].w = JCF_readu4 (jcf);
          break;
        case CONSTANT_Utf8:
          n = JCF_readu2 (jcf);
@@ -217,7 +378,7 @@ DEFUN(jcf_parse_constant_pool, (jcf),
 #ifdef HANDLE_CONSTANT_Utf8
          HANDLE_CONSTANT_Utf8(jcf, i, n);
 #else
-         jcf->cpool.data[i] = JCF_TELL(jcf) - 2;
+         jcf->cpool.data[i].w = JCF_TELL(jcf) - 2;
          JCF_SKIP (jcf, n);
 #endif
          break;
@@ -231,8 +392,7 @@ DEFUN(jcf_parse_constant_pool, (jcf),
 /* Read various class flags and numbers. */
 
 static void
-DEFUN(jcf_parse_class, (jcf),
-      JCF* jcf)
+jcf_parse_class (JCF* jcf)
 {
   int i;
   uint16 interfaces_count;
@@ -260,8 +420,7 @@ DEFUN(jcf_parse_class, (jcf),
 
 /* Read fields. */
 static int
-DEFUN(jcf_parse_fields, (jcf),
-      JCF* jcf)
+jcf_parse_fields (JCF* jcf)
 {
   int i, j;
   uint16 fields_count;
@@ -279,11 +438,11 @@ DEFUN(jcf_parse_fields, (jcf),
       uint16 attribute_count = JCF_readu2 (jcf);
 #ifdef HANDLE_START_FIELD
       HANDLE_START_FIELD (access_flags, name_index, signature_index,
-                   attribute_count);
+                         attribute_count);
 #endif
       for (j = 0; j < attribute_count; j++)
        {
-         int code = get_attribute (jcf);
+         int code = get_attribute (jcf, i, JV_FIELD_ATTR);
          if (code != 0)
            return code;
        }
@@ -300,8 +459,7 @@ DEFUN(jcf_parse_fields, (jcf),
 /* Read methods. */
 
 static int
-DEFUN(jcf_parse_one_method, (jcf),
-      JCF* jcf)
+jcf_parse_one_method (JCF* jcf, int index)
 {
   int i;
   uint16 access_flags = (JCF_FILL (jcf, 8), JCF_readu2 (jcf));
@@ -313,7 +471,7 @@ DEFUN(jcf_parse_one_method, (jcf),
 #endif
   for (i = 0; i < attribute_count; i++)
     {
-      int code = get_attribute (jcf);
+      int code = get_attribute (jcf, index, JV_METHOD_ATTR);
       if (code != 0)
        return code;
     }
@@ -324,8 +482,7 @@ DEFUN(jcf_parse_one_method, (jcf),
 }
 
 static int
-DEFUN(jcf_parse_methods, (jcf),
-      JCF* jcf)
+jcf_parse_methods (JCF* jcf)
 {
   int i;
   uint16 methods_count;
@@ -336,7 +493,7 @@ DEFUN(jcf_parse_methods, (jcf),
 #endif
   for (i = 0; i < methods_count; i++)
     {
-      int code = jcf_parse_one_method (jcf);
+      int code = jcf_parse_one_method (jcf, i);
       if (code != 0)
        return code;
     }
@@ -348,8 +505,7 @@ DEFUN(jcf_parse_methods, (jcf),
 
 /* Read attributes. */
 static int
-DEFUN(jcf_parse_final_attributes, (jcf),
-      JCF *jcf)
+jcf_parse_final_attributes (JCF *jcf)
 {
   int i;
   uint16 attributes_count = (JCF_FILL (jcf, 2), JCF_readu2 (jcf));
@@ -358,7 +514,7 @@ DEFUN(jcf_parse_final_attributes, (jcf),
 #endif
   for (i = 0; i < attributes_count; i++)
     {
-      int code = get_attribute (jcf);
+      int code = get_attribute (jcf, i, JV_CLASS_ATTR);
       if (code != 0)
        return code;
     }