1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/cocoa/objc/objc_uniquifying.h
3 // Purpose: Allows wxWidgets code to get a direct pointer to a compiled
4 // Objective-C class and provides a method to fix up the
5 // name to include a unique identifier (currently the address
6 // of the objc_class structure).
7 // Author: David Elliott <dfe@cox.net>
11 // Copyright: (c) 2007 Software 2000 Ltd.
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 #ifndef __WX_COCOA_OBJC_CLASS_H__
16 #define __WX_COCOA_OBJC_CLASS_H__
18 /* A note about this header:
19 Nothing in here is guaranteed to exist in future versions of wxCocoa. There
20 are other ways of adding Objective-C classes at runtime and a future wxCocoa
21 might use these instead of this ugly hack. You may use this header file in
22 your own wxCocoa code if you need your own Objective-C classes to be
25 You cannot turn this on for 64-bit mode. It will not compile due to opaque
26 Objective-C data structures and it is not needed because it is a workaround
27 for a bug that does not exist in the 64-bit runtime.
29 You should not use this when wxCocoa is built as a dynamic library. This has
30 only been tested for the case when wxCocoa is built as a static library and
31 statically linked to user code to form a loadable bundle (e.g. a Cocoa plugin).
32 It forces each plugin (when multiple wxCocoa-using plugins are used) to use
33 its own internal Objective-C classes which is desirable when wxCocoa is
34 statically linked to the rest of the code.
36 Do not use uniquifying on your principal class. That one should be named
37 differently for different bundles.
40 #if wxUSE_OBJC_UNIQUIFYING
42 // objc_getClass and stuff
43 #include <objc/objc-runtime.h>
45 ////////////// Objective-C uniquifying implementation //////////////
47 template <typename ObjcType
>
48 class wxObjcClassInitializer
;
50 template <typename ObjcType
>
53 template <typename ObjcType
>
54 class wxObjcCompilerInformation
56 friend class wxObjcClassInitializer
<ObjcType
>;
57 friend class UniquifiedName
<ObjcType
>;
59 // GetCompiledClass must be partially specialized for an ObjcType
60 // If you're not using it, implement an inline returning NULL
61 inline static struct objc_class
* GetCompiledClass();
63 // sm_theClassName must be partially specialized for each type
64 static const char sm_theClassName
[];
66 // GetSuperclass must be specialized. Typically one of two ways:
67 // 1. objc_getClass("SomeRealClassName")
68 // 2. wxGetObjcClass_SomeWxClassName();
69 inline static struct objc_class
*GetSuperclass();
73 template <typename ObjcType
>
76 // We're going for OriginalClassName@ClassStructureAddress
77 // Therefore our size is the sizeof the original class name constant string (which includes the terminating NULL)
78 // plus the sizeof a pointer to struct objc_class times two (two hex digits for each byte) plus 3 for "@0x"
79 typedef char Type
[sizeof(wxObjcCompilerInformation
<ObjcType
>::sm_theClassName
) + (sizeof(struct objc_class
*)<<1) + 3];
80 static void Init(Type m_theString
, const objc_class
*aClass
)
82 snprintf(const_cast<char*>(m_theString
), sizeof(Type
), "%s@%p", wxObjcCompilerInformation
<ObjcType
>::sm_theClassName
, aClass
);
86 /*! @function HidePointerFromGC
87 @abstract Returns an l-value whose location the compiler cannot know.
89 The compiler-generated Objective-C class structures are located in the static data area.
90 They are by design Objective-C objects in their own right which makes the compiler issue
91 write barriers as if they were located in the GC-managed heap as most Objective-C objects.
93 By accepting and returning a reference to any pointer type we can set any i-var of an
94 Objective-C object that is a pointer to another Objective-C object without the compiler
95 generating an objc_assign_ivar write barrier. It will instad generate an
96 objc_assign_strongCast write barrier which is the appropriate write-barrier when assigning
97 pointers to Objective-C objects located in unknown memory.
100 Class *someClass = ...;
101 HidePointerFromGC(someClass->isa) = ...;
103 template <typename ObjcType
>
104 inline ObjcType
* & HidePointerFromGC(ObjcType
* &p
) __attribute__((always_inline
));
106 template <typename ObjcType
>
107 inline ObjcType
* & HidePointerFromGC(ObjcType
* &p
)
112 template <typename ObjcType
>
113 class wxObjcClassInitializer
116 static struct objc_class
* Get()
118 static wxObjcClassInitializer
<ObjcType
> s_theInstance
;
119 s_theInstance
.noop(); // Make the compiler think we need this instance
120 return wxObjcCompilerInformation
<ObjcType
>::GetCompiledClass();
125 // This "constructor" operates solely on static data
126 // It exists so that we can take advantage of a function-static
127 // "instance" of this class to do the static data initialization.
128 wxObjcClassInitializer()
130 // Objective-C class initialization occurs before C++ static initialization because the
131 // libobjc.dylib gets notified directly by dyld on Tiger.
132 // Therefore, even though we change the name, the class is still registered with the
133 // original name. We unfortunately can't change that.
135 // The first time the class is loaded, Objective-C will already have fixed up the super_class
136 // and isa->isa and isa->super_class variables so much of this won't do anything. But
137 // the next time the class is loaded, Objective-C will ignore it and thus we need to
138 // initialize the data structures appropriately.
140 // Ideally we'd have some sort of lock here, but we depend on the fact that we get called
141 // just before the first time someone wants to send a class message so it should be
142 // reasonably safe to do this without any locks.
144 struct objc_class
&theClassData
= *wxObjcCompilerInformation
<ObjcType
>::GetCompiledClass();
145 // Initialize the uniquified class name
146 UniquifiedName
<ObjcType
>::Init(sm_theUniquifiedClassName
, &theClassData
);
148 //////// Class Initialization ////////
149 // Use objc_getClass to fix up the superclass pointer
150 theClassData
.super_class
= wxObjcCompilerInformation
<ObjcType
>::GetSuperclass();
151 // Fix up the compiler generated class struct to use the new name
152 theClassData
.name
= sm_theUniquifiedClassName
;
154 //////// Meta-Class Initialization ////////
155 // theClassData.isa is the metaclass pointer
156 // Globals on Darwin use PC-relative access (slow) so it's quicker to use theClassData.isa
158 // In any object hierarchy a metaclass's metaclass is always the root class's metaclass
159 // Therefore, our superclass's metaclass's metaclass should already be the root class's metaclass
160 HidePointerFromGC(theClassData
.isa
->isa
) = theClassData
.super_class
->isa
->isa
;
161 // A metaclass's superclass is always the superclass's metaclass.
162 HidePointerFromGC(theClassData
.isa
->super_class
) = theClassData
.super_class
->isa
;
163 // Fix up the compiler generated metaclass struct to use the new name
164 theClassData
.isa
->name
= sm_theUniquifiedClassName
;
166 // We need to set the initialized flag because after we change the name, Objective-C can't
167 // look us up by name because we're only registered with the original name.
168 theClassData
.isa
->info
|= CLS_INITIALIZED
;
170 wxObjcClassInitializer(const wxObjcClassInitializer
&); // NO COPY
171 wxObjcClassInitializer
& operator =(const wxObjcClassInitializer
&); // NO ASSIGN
172 static typename UniquifiedName
<ObjcType
>::Type sm_theUniquifiedClassName
;
175 template<typename ObjcType
>
176 typename UniquifiedName
<ObjcType
>::Type wxObjcClassInitializer
<ObjcType
>::sm_theUniquifiedClassName
;
178 // WX_DECLARE_GET_OBJC_CLASS
179 // Declares a function to get a direct pointer to an objective-C class.
180 // The class is guaranteed to be usable.
181 // When wxCocoa is built into a Mach-O bundle this function allows the wxCocoa
182 // code to get a reference to the Objective-C class structure located in the
183 // same bundle. This allows a static wxCocoa library to be built into
184 // two different Mach-O bundles without having one bundle's Objective-C
185 // classes trample on the other's.
186 // Right now we toss the ObjcSuperClass parameter, but we might use it later.
187 #define WX_DECLARE_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) \
188 struct objc_class* wx_GetObjcClass_ ## ObjcClass();
190 // WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass)
191 // Provides an architecture-dependent way to get the direct pointer to the
192 // objc_class structure in the __OBJC segment.
193 // This takes advantage of the fact that the Objective-C compiler uses guessable
194 // local assembler labels for the class structures.
195 // Those class structures are only available on the Objective-C file containing the
196 // @implementation block.
199 // Generic implementation - Tested on i386 and PPC. Should work in all cases.
200 // This is a hack that depends on GCC asm symbol names.
201 // The static variable winds up being initialized with a direct reference to the appropriate
202 // L_OBJC_CLASS and no global symbol reference is generated because nothing uses the global symbol
203 // except for the static initializer which does it directly.
204 // The generated assembler for s_objc_class_ptr is basically like this:
205 // _s_objc_class_ptr_ObjcClass:
206 // .long L_OBJC_CLASS_ObjcClass
207 // Once that static symbol is defined, the function implementation is easy for GCC to generate.
208 // Do note that return &s_objc_class_data_ObjcClass won't work. The code is wrong in the case.
209 #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
210 extern "C" objc_class s_objc_class_data_ ## ObjcClass asm("L_OBJC_CLASS_" #ObjcClass); \
211 static objc_class * s_objc_class_ptr_ ## ObjcClass = &s_objc_class_data_ ## ObjcClass; \
213 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
215 return s_objc_class_ptr_## ObjcClass; \
218 #elif defined(__i386__)
219 // Not used because the generic implementation seems to work fine.
220 // But this is here since it was written beforehand and it also works.
222 // This is based on the code GCC generates for accessing file-static data on i386.
223 // The i386 PC-relative addressing happens in this manner
224 // 1. The program counter is placed into ecx using the code that GCC should have
225 // already generated.
226 // 2. A label is placed directly after the call to get the program counter.
227 // 3. The Load Effective Address instruction is used to add the offset of the
228 // local assembler label we're interested in minus the local assembler label
229 // from step 2 to the program counter register in ecx and place the result
230 // into the result register (typically eax if not inlined).
231 #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
233 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
235 register struct objc_class *retval; \
237 ( "call ___i686.get_pc_thunk.cx\n" \
238 "\"LPC_FOR_GET_CLASS_" #ObjcClass "\":\n\t" \
239 "leal L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\"(%%ecx), %0" \
247 #elif defined(__ppc__)
248 // Not used because the generic implementation seems to work fine.
249 // But this is here since it was written beforehand and it also works.
251 // This is based on the code GCC generates for accessing file-static data on PPC.
252 // The PowerPC PC-relative addressing happens in this manner
253 // 1. The link register is saved (mflr) to a temporary (we re-use the output register for this)
254 // 2. An unconditional branch instruction (bcl) "branches" to the following address (labeled)
255 // 3. The link register (filled in by bcl) is saved to r10 (a temporary)
256 // 4. The previous link register is restored (mtlr) (from the output register we were using as a temporary)
257 // 5. The address of the LPC label as executed is added to the high 16 bits of the offset between that label and the static data we want
258 // and stored in a temporary register (r2)
259 // 6. That temporary register plus the low 16 bits of the offset are stored into the result register.
260 #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
262 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
264 register struct objc_class *retval; \
267 "\n\tbcl 20, 31, \"LPC_FOR_GET_CLASS_" #ObjcClass "\"" \
268 "\n\"LPC_FOR_GET_CLASS_" #ObjcClass "\":" \
271 "\n\taddis r2,r10,ha16(L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\")" \
272 "\n\tla %0,lo16(L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\")(r2)" \
280 // TODO: __x86_64__, __ppc64__
281 #else // Can't wrie inline asm to bust into __OBJC segment
282 // This won't be used since the generic implementation takes precedence.
284 #warning "Don't know how to implement wxObjcCompilerInformation<ObjcClass>::GetCompiledClass on this platform"
288 // The WX_IMPLEMENT_OBJC_GET_SUPERCLASS macro implements the template specialization
289 // to get the superclass. This only works if it's a real superclass. If you are
290 // deriving from a class that's already being uniquified then you'd need to
291 // implement the specialization to call the appropriate get method instead.
292 #define WX_IMPLEMENT_OBJC_GET_SUPERCLASS(ObjcClass,ObjcSuperClass) \
294 inline objc_class* wxObjcCompilerInformation<ObjcClass>::GetSuperclass() \
296 return objc_getClass(#ObjcSuperClass); \
299 // The WX_IMPLEMENT_OBJC_GET_UNIQUIFIED_SUPERCLASS macro implements the template
300 // specialization to get the superclass when the superclass is another uniquified
301 // Objective-C class.
302 #define WX_IMPLEMENT_OBJC_GET_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass) \
304 inline objc_class* wxObjcCompilerInformation<ObjcClass>::GetSuperclass() \
306 return wx_GetObjcClass_ ## ObjcSuperClass(); \
309 // The WX_IMPLEMENT_OBJC_CLASS_NAME macro implements the template specialization
310 // of the sm_theClassName constant. As soon as this specialization is in place
311 // sizeof(sm_theClassName) will return the number of bytes at compile time.
312 #define WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \
314 const char wxObjcCompilerInformation<ObjcClass>::sm_theClassName[] = #ObjcClass;
316 // The WX_IMPLEMENT_OBJC_GET_OBJC_CLASS macro is the final one that actually provides
317 // the wx_GetObjcClass_XXX function that will be called in lieu of asking the Objective-C
318 // runtime for the class. All the others are really machinery to make this happen.
319 #define WX_IMPLEMENT_OBJC_GET_OBJC_CLASS(ObjcClass) \
320 objc_class* wx_GetObjcClass_ ## ObjcClass() \
322 return wxObjcClassInitializer<ObjcClass>::Get(); \
325 // The WX_IMPLEMENT_GET_OBJC_CLASS macro combines all of these together
326 // for the case when the superclass is a non-uniquified class.
327 #define WX_IMPLEMENT_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) \
328 WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
329 WX_IMPLEMENT_OBJC_GET_SUPERCLASS(ObjcClass,ObjcSuperClass) \
330 WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \
331 WX_IMPLEMENT_OBJC_GET_OBJC_CLASS(ObjcClass)
333 // The WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS macro combines all
334 // of these together for the case when the superclass is another uniquified class.
335 #define WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass) \
336 WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
337 WX_IMPLEMENT_OBJC_GET_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass) \
338 WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \
339 WX_IMPLEMENT_OBJC_GET_OBJC_CLASS(ObjcClass)
341 // The WX_GET_OBJC_CLASS macro is intended to wrap the class name when the class
342 // is used as a message receiver (e.g. for calling class methods). When
343 // class name uniquifying is used, this calls the global function implemented
344 // in the Objective-C file containing the class @implementation.
345 #define WX_GET_OBJC_CLASS(ObjcClass) wx_GetObjcClass_ ## ObjcClass()
347 #else // wxUSE_OBJC_UNIQUIFYING
349 // Define WX_DECLARE_GET_OBJC_CLASS as nothing
350 #define WX_DECLARE_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass)
351 // Define WX_IMPLEMENT_GET_OBJC_CLASS as nothing
352 #define WX_IMPLEMENT_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass)
353 // Define WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS as nothing
354 #define WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass)
356 // Define WX_GET_OBJC_CLASS macro to output the class name and let the compiler do the normal thing
357 // The WX_GET_OBJC_CLASS macro is intended to wrap the class name when the class
358 // is used as a message receiver (e.g. for calling class methods). When
359 // class name uniquifying is not used, this is simply defined to be the class
360 // name which will allow the compiler to do the normal thing.
361 #define WX_GET_OBJC_CLASS(ObjcClass) ObjcClass
363 #endif // wxUSE_OBJC_UNIQUIFYING
365 #endif //ndef __WX_COCOA_OBJC_CLASS_H__