OSDN Git Service

Merge remote-tracking branch 'gnu/gcc-4_7-branch' into rework
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / gnu-api-2-objc.m
index 59344ef..d65c120 100644 (file)
 - (id) variable { return variable_ivar; }
 @end
 
+/* Hack to calculate the log2 of a byte alignment.  */
+unsigned char
+log_2_of (unsigned int x)
+{
+  unsigned char result = 0;
+
+  /* We count how many times we need to divide by 2 before we reach 1.
+     This algorithm is good enough for the small numbers (such as 8,
+     16 or 64) that we have to deal with.  */
+  while (x > 1)
+    {
+      x = x / 2;
+      result++;
+    }
+
+  return result;
+}
 
 int main(int argc, void **args)
 {
@@ -56,8 +73,9 @@ int main(int argc, void **args)
     Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass", 0);
 
     /* A new root class would obviously need at least an 'isa'
-       instance variable.  We don't add it so we never actually
-       instantiate an instance of the class, which wouldn't work.  */
+       instance variable.  */
+    class_addIvar (new_root_class, "isa", sizeof (Class), log_2_of (__alignof__ (Class)),
+                  @encode (Class));
 
     objc_registerClassPair (new_root_class);
     objc_registerClassPair (new_class);
@@ -75,7 +93,7 @@ int main(int argc, void **args)
       abort ();
 
     {
-      MySubClass *o = [[objc_getClass ("MyNewSubClass") alloc] init];
+      MySubClass *o = [[(Class)objc_getClass ("MyNewSubClass") alloc] init];
       
       if (object_getClass (o) != objc_getClass ("MyNewSubClass"))
        abort ();
@@ -114,7 +132,7 @@ int main(int argc, void **args)
     /* Add a bit of everything to the class to exercise undoing all these changes.  */
 
     /* Instance variable.  */
-    class_addIvar (new_class, "my_variable", sizeof (float), __alignof__ (float), @encode (float));
+    class_addIvar (new_class, "my_variable", sizeof (float), log_2_of (__alignof__ (float)), @encode (float));
 
     /* Instance method.  */
     class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method),