* src/vm/global.h (ACC_CLASS_MEMBER): New define.
[cacao.git] / src / vmcore / class.c
index b33227872c0693adaa84cb9c64e77578cbf70aef..05f04dffd456390b493828282e52c45bfb1ff38c 100644 (file)
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: class.c 8330 2007-08-16 18:15:51Z twisti $
+   $Id: class.c 8339 2007-08-17 21:21:51Z twisti $
 
 */
 
@@ -520,11 +520,16 @@ static bool class_load_attribute_enclosingmethod(classbuffer *cb)
 
 bool class_load_attributes(classbuffer *cb)
 {
-       classinfo *c;
-       u4         i, j;
-       u2         attributes_count;
-       u2         attribute_name_index;
-       utf       *attribute_name;
+       classinfo             *c;
+       uint16_t               attributes_count;
+       uint16_t               attribute_name_index;
+       utf                   *attribute_name;
+       innerclassinfo        *info;
+       classref_or_classinfo  inner;
+       classref_or_classinfo  outer;
+       utf                   *name;
+       uint16_t               flags;
+       int                    i, j;
 
        c = cb->class;
 
@@ -574,19 +579,45 @@ bool class_load_attributes(classbuffer *cb)
                        for (j = 0; j < c->innerclasscount; j++) {
                                /* The innerclass structure contains a class with an encoded
                                   name, its defining scope, its simple name and a bitmask of
-                                  the access flags. If an inner class is not a member, its
-                                  outer_class is NULL, if a class is anonymous, its name is
-                                  NULL. */
+                                  the access flags. */
                                                                
-                               innerclassinfo *info = c->innerclass + j;
-
-                               info->inner_class.ref =
-                                       innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
-                               info->outer_class.ref =
-                                       innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
-                               info->name =
-                                       innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
-                               info->flags = suck_u2(cb);
+                               info = c->innerclass + j;
+
+                               inner.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
+                               outer.ref = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
+                               name      = innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
+                               flags     = suck_u2(cb);
+
+                               /* If the current inner-class is the currently loaded
+                                  class check for some special flags. */
+
+                               if (inner.ref->name == c->name) {
+                                       /* If an inner-class is not a member, its
+                                          outer-class is NULL. */
+
+                                       if (outer.ref != NULL) {
+                                               c->flags |= ACC_CLASS_MEMBER;
+
+                                               /* A member class doesn't have an
+                                                  EnclosingMethod attribute, so set the
+                                                  enclosing-class to be the same as the
+                                                  declaring-class. */
+
+                                               c->declaringclass = outer;
+                                               c->enclosingclass = outer;
+                                       }
+
+                                       /* If an inner-class is anonymous, its name is
+                                          NULL. */
+
+                                       if (name == NULL)
+                                               c->flags |= ACC_CLASS_ANONYMOUS;
+                               }
+
+                               info->inner_class = inner;
+                               info->outer_class = outer;
+                               info->name        = name;
+                               info->flags       = flags;
                        }
                }
                else if (attribute_name == utf_SourceFile) {
@@ -1605,6 +1636,21 @@ bool class_is_primitive(classinfo *c)
 }
 
 
+/* class_is_anonymousclass *****************************************************
+
+   Checks if the given class is an anonymous class.
+
+*******************************************************************************/
+
+bool class_is_anonymousclass(classinfo *c)
+{
+       if (c->flags & ACC_CLASS_ANONYMOUS)
+               return true;
+
+       return false;
+}
+
+
 /* class_is_array **************************************************************
 
    Checks if the given class is an array class.
@@ -1636,6 +1682,36 @@ bool class_is_interface(classinfo *c)
 }
 
 
+/* class_is_localclass *********************************************************
+
+   Checks if the given class is a local class.
+
+*******************************************************************************/
+
+bool class_is_localclass(classinfo *c)
+{
+       if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
+               return true;
+
+       return false;
+}
+
+
+/* class_is_memberclass ********************************************************
+
+   Checks if the given class is a member class.
+
+*******************************************************************************/
+
+bool class_is_memberclass(classinfo *c)
+{
+       if (c->flags & ACC_CLASS_MEMBER)
+               return true;
+
+       return false;
+}
+
+
 /* class_get_superclass ********************************************************
 
    Return the super class of the given class.  If the super-field is a
@@ -1722,9 +1798,15 @@ java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOn
                /* Determine number of declared classes. */
 
                for (i = 0; i < c->innerclasscount; i++) {
+                       /* Get outer-class.  If the inner-class is not a member
+                          class, the outer-class is NULL. */
+
                        outer = c->innerclass[i].outer_class;
 
-                       /* Check if outer_class is a classref or a real class and
+                       if (outer.any == NULL)
+                               continue;
+
+                       /* Check if outer-class is a classref or a real class and
                get the class name from the structure. */
 
                        outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
@@ -1748,6 +1830,12 @@ java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOn
                inner = c->innerclass[i].inner_class;
                outer = c->innerclass[i].outer_class;
 
+               /* Get outer-class.  If the inner-class is not a member class,
+                  the outer-class is NULL. */
+
+               if (outer.any == NULL)
+                       continue;
+
                /* Check if outer_class is a classref or a real class and get
                   the class name from the structure. */
 
@@ -1785,52 +1873,71 @@ java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOn
 
 classinfo *class_get_declaringclass(classinfo *c)
 {
-       classref_or_classinfo  innercr;
-       utf                   *innername;
-       classref_or_classinfo  outercr;
-       classinfo             *outer;
-       int16_t                i;
+       classref_or_classinfo  cr;
+       classinfo             *dc;
+
+       /* Get declaring class. */
 
-       /* return NULL for arrayclasses and primitive classes */
+       cr = c->declaringclass;
 
-       if (class_is_primitive(c) || (c->name->text[0] == '['))
+       if (cr.any == NULL)
                return NULL;
 
-       /* no innerclasses exist */
+       /* Resolve the class if necessary. */
 
-       if (c->innerclasscount == 0)
-               return NULL;
+       if (IS_CLASSREF(cr)) {
+/*             dc = resolve_classref_eager(cr.ref); */
+               dc = resolve_classref_or_classinfo_eager(cr, true);
 
-       for (i = 0; i < c->innerclasscount; i++) {
-               /* Check if inner_class is a classref or a real class and get
-                  the class name from the structure. */
+               if (dc == NULL)
+                       return NULL;
+
+               /* Store the resolved class in the class structure. */
 
-               innercr = c->innerclass[i].inner_class;
+               cr.cls = dc;
+       }
 
-               innername = IS_CLASSREF(innercr) ?
-                       innercr.ref->name : innercr.cls->name;
+       dc = cr.cls;
 
-               /* Is the current innerclass this class? */
+       return dc;
+}
 
-               if (innername == c->name) {
-                       /* Maybe the outer class is not loaded yet. */
 
-                       outercr = c->innerclass[i].outer_class;
+/* class_get_enclosingclass ****************************************************
 
-                       outer = resolve_classref_or_classinfo_eager(outercr, false);
+   Return the enclosing class for the given class.
 
-                       if (outer == NULL)
-                               return NULL;
+*******************************************************************************/
 
-                       if (!(outer->state & CLASS_LINKED))
-                               if (!link_class(outer))
-                                       return NULL;
+classinfo *class_get_enclosingclass(classinfo *c)
+{
+       classref_or_classinfo  cr;
+       classinfo             *ec;
 
-                       return outer;
-               }
+       /* Get enclosing class. */
+
+       cr = c->enclosingclass;
+
+       if (cr.any == NULL)
+               return NULL;
+
+       /* Resolve the class if necessary. */
+
+       if (IS_CLASSREF(cr)) {
+/*             ec = resolve_classref_eager(cr.ref); */
+               ec = resolve_classref_or_classinfo_eager(cr, true);
+
+               if (ec == NULL)
+                       return NULL;
+
+               /* Store the resolved class in the class structure. */
+
+               cr.cls = ec;
        }
 
-       return NULL;
+       ec = cr.cls;
+
+       return ec;
 }