From patchwork Sat Oct 16 14:09:25 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicola Pero X-Patchwork-Id: 68032 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 67208B6EEB for ; Sun, 17 Oct 2010 01:09:38 +1100 (EST) Received: (qmail 30273 invoked by alias); 16 Oct 2010 14:09:35 -0000 Received: (qmail 30260 invoked by uid 22791); 16 Oct 2010 14:09:34 -0000 X-SWARE-Spam-Status: No, hits=-1.2 required=5.0 tests=AWL, BAYES_00, SARE_SUB_ENC_UTF8, TW_BJ, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (140.186.70.10) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 16 Oct 2010 14:09:29 +0000 Received: from eggs.gnu.org ([140.186.70.92]:52539) by fencepost.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1P77Rw-00022m-0v for gcc-patches@gnu.org; Sat, 16 Oct 2010 10:09:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P77Ru-0003y7-HV for gcc-patches@gnu.org; Sat, 16 Oct 2010 10:09:27 -0400 Received: from smtp191.iad.emailsrvr.com ([207.97.245.191]:45393) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P77Ru-0003y3-Eo for gcc-patches@gnu.org; Sat, 16 Oct 2010 10:09:26 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp49.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id DD73719051B for ; Sat, 16 Oct 2010 10:09:25 -0400 (EDT) X-Orig-To: gcc-patches@gnu.org Received: from dynamic10.wm-web.iad.mlsrvr.com (dynamic10.wm-web.iad1a.rsapps.net [192.168.2.217]) by smtp49.relay.iad1a.emailsrvr.com (SMTP Server) with ESMTP id CA061190519 for ; Sat, 16 Oct 2010 10:09:25 -0400 (EDT) Received: from meta-innovation.com (localhost [127.0.0.1]) by dynamic10.wm-web.iad.mlsrvr.com (Postfix) with ESMTP id BB37A478807F for ; Sat, 16 Oct 2010 10:09:25 -0400 (EDT) Received: by www2.webmail.us (Authenticated sender: nicola.pero@meta-innovation.com, from: nicola.pero@meta-innovation.com) with HTTP; Sat, 16 Oct 2010 16:09:25 +0200 (CEST) Date: Sat, 16 Oct 2010 16:09:25 +0200 (CEST) Subject: =?UTF-8?Q?libobjc=20-=20more=20modern=20Objective-C=20runtime=20API=20(1?= =?UTF-8?Q?1)?= From: "Nicola Pero" To: "gcc-patches@gnu.org" MIME-Version: 1.0 X-Type: plain Message-ID: <1287238165.765419778@192.168.2.227> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch fixes a problem where class_setSuperclass() and class_getInstanceVariable() wouldn't work for classes that had not been resolved by the runtime yet. It also include a new testcase for all the new ivar_xxx functions. Committed to trunk. Thanks In libobjc/: 2010-10-16 Nicola Pero * class.c (class_getSuperclass): Call __objc_resolve_class_links if the class is not resolved yet. * ivars.c (class_getInstanceVariable): Use class_getSuperclass. In gcc/testsuite/: 2010-10-16 Nicola Pero * objc.dg/gnu-api-2-ivar.m: New. Index: class.c =================================================================== --- class.c (revision 165525) +++ class.c (working copy) @@ -500,7 +500,7 @@ objc_getClass (const char *name) if (class) return class; - + if (__objc_get_unknown_class_handler) return (*__objc_get_unknown_class_handler) (name); @@ -796,12 +796,24 @@ class_isMetaClass (Class class_) return CLS_ISMETA (class_); } +/* Even inside libobjc it may be worth using class_getSuperclass + instead of accessing class_->super_class directly because it + resolves the class links if needed. If you access + class_->super_class directly, make sure to deal with the situation + where the class is not resolved yet! */ Class class_getSuperclass (Class class_) { if (class_ == Nil) return Nil; + /* If the class is not resolved yet, super_class would point to a + string (the name of the super class) as opposed to the actual + super class. In that case, we need to resolve the class links + before we can return super_class. */ + if (! CLS_ISRESOLV (class_)) + __objc_resolve_class_links (); + return class_->super_class; } Index: ChangeLog =================================================================== --- ChangeLog (revision 165533) +++ ChangeLog (working copy) @@ -1,5 +1,11 @@ 2010-10-16 Nicola Pero + * class.c (class_getSuperclass): Call __objc_resolve_class_links + if the class is not resolved yet. + * ivars.c (class_getInstanceVariable): Use class_getSuperclass. + +2010-10-16 Nicola Pero + * objc/runtime.h (class_getIvarLayout): New. (class_getWeakIvarLayout): New. (class_setIvarLayout): New. Index: ivars.c =================================================================== --- ivars.c (revision 165533) +++ ivars.c (working copy) @@ -53,7 +53,7 @@ class_getInstanceVariable (Class class_, const cha } } } - class_ = class_->super_class; + class_ = class_getSuperclass (class_); } objc_mutex_unlock (__objc_runtime_mutex); } Index: ChangeLog =================================================================== --- ChangeLog (revision 165541) +++ ChangeLog (working copy) @@ -1,3 +1,7 @@ +2010-10-16 Nicola Pero + + * objc.dg/gnu-api-2-ivar.m: New. + 2010-10-15 Nicola Pero * objc.dg/gnu-api-2-property.m: New. Index: objc.dg/gnu-api-2-ivar.m =================================================================== --- objc.dg/gnu-api-2-ivar.m (revision 0) +++ objc.dg/gnu-api-2-ivar.m (revision 0) @@ -0,0 +1,80 @@ +/* Test the Modern GNU Objective-C Runtime API. + + This is test 'ivar', covering all functions starting with 'ivar'. */ + +/* { dg-do run } */ +/* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */ + +/* To get the modern GNU Objective-C Runtime API, you include + objc/runtime.h. */ +#include +#include +#include +#include + +@interface MyRootClass +{ Class isa; } ++ alloc; +- init; +@end + +@implementation MyRootClass ++ alloc { return class_createInstance (self, 0); } +- init { return self; } +@end + +@protocol MyProtocol +- (id) variable; +@end + +@protocol MySecondProtocol +- (id) setVariable: (id)value; +@end + +@interface MySubClass : MyRootClass +{ id variable_ivar; } +- (void) setVariable: (id)value; +- (id) variable; +@end + +@implementation MySubClass +- (void) setVariable: (id)value { variable_ivar = value; } +- (id) variable { return variable_ivar; } +@end + + +int main(int argc, void **args) +{ + /* Functions are tested in alphabetical order. */ + + printf ("Testing ivar_getName () ...\n"); + { + Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), + "variable_ivar"); + if (strcmp (ivar_getName (ivar), "variable_ivar") != 0) + abort (); + + ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), + "variable"); + if (ivar != 0) + abort (); + } + + printf ("Testing ivar_getOffset () ...\n"); + { + Ivar ivar = class_getInstanceVariable (objc_getClass ("MyRootClass"), + "isa"); + if (ivar_getOffset (ivar) != 0) + abort (); + } + + printf ("Testing ivar_getTypeEncoding () ...\n"); + { + Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), + "variable_ivar"); + if (strcmp (ivar_getTypeEncoding (ivar), "@") != 0) + abort (); + } + + return 0; +}