ObjFW
Loading...
Searching...
No Matches
private.h
1/*
2 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3 *
4 * All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version 3.0 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * version 3.0 for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * version 3.0 along with this program. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#include "config.h"
21
22#import "macros.h"
23
24#if !defined(__has_feature) || !__has_feature(nullability)
25# ifndef _Nonnull
26# define _Nonnull
27# endif
28# ifndef _Nullable
29# define _Nullable
30# endif
31#endif
32
33typedef uint32_t (*_Nonnull objc_hashtable_hash_func)(const void *_Nonnull key);
34typedef bool (*_Nonnull objc_hashtable_equal_func)(const void *_Nonnull key1,
35 const void *_Nonnull key2);
36
37struct objc_class {
38 Class _Nonnull isa;
39 Class _Nullable superclass;
40 const char *_Nonnull name;
41 unsigned long version;
42 unsigned long info;
43 long instanceSize;
44 struct objc_ivar_list *_Nullable ivars;
45 struct objc_method_list *_Nullable methodList;
46 struct objc_dtable *_Nullable dTable;
47 Class _Nullable *_Nullable subclassList;
48 void *_Nullable siblingClass;
49 struct objc_protocol_list *_Nullable protocols;
50 void *_Nullable GCObjectType;
51 unsigned long ABIVersion;
52 int32_t *_Nonnull *_Nullable ivarOffsets;
53 struct objc_property_list *_Nullable propertyList;
54};
55
56enum objc_class_info {
57 OBJC_CLASS_INFO_CLASS = 0x001,
58 OBJC_CLASS_INFO_METACLASS = 0x002,
59 OBJC_CLASS_INFO_NEW_ABI = 0x010,
60 OBJC_CLASS_INFO_SETUP = 0x100,
61 OBJC_CLASS_INFO_LOADED = 0x200,
62 OBJC_CLASS_INFO_DTABLE = 0x400,
63 OBJC_CLASS_INFO_INITIALIZED = 0x800
64};
65
67 Class _Nonnull isa;
68};
69
70struct objc_selector {
71 uintptr_t UID;
72 const char *_Nullable typeEncoding;
73};
74
75struct objc_method {
76 struct objc_selector selector;
77 IMP _Nonnull implementation;
78};
79
80struct objc_method_list {
81 struct objc_method_list *_Nullable next;
82 unsigned int count;
83 struct objc_method methods[1];
84};
85
86struct objc_category {
87 const char *_Nonnull categoryName;
88 const char *_Nonnull className;
89 struct objc_method_list *_Nullable instanceMethods;
90 struct objc_method_list *_Nullable classMethods;
91 struct objc_protocol_list *_Nullable protocols;
92};
93
94struct objc_ivar {
95 const char *_Nonnull name;
96 const char *_Nonnull typeEncoding;
97 unsigned int offset;
98};
99
100struct objc_ivar_list {
101 unsigned int count;
102 struct objc_ivar ivars[1];
103};
104
105struct objc_method_description {
106 const char *_Nonnull name;
107 const char *_Nonnull typeEncoding;
108};
109
110struct objc_method_description_list {
111 int count;
112 struct objc_method_description list[1];
113};
114
115struct objc_protocol_list {
116 struct objc_protocol_list *_Nullable next;
117 long count;
118 Protocol *__unsafe_unretained _Nonnull list[1];
119};
120
121#if __has_attribute(__objc_root_class__)
122__attribute__((__objc_root_class__))
123#endif
124@interface Protocol
125{
126@public
127 Class _Nonnull isa;
128 const char *_Nonnull name;
129 struct objc_protocol_list *_Nullable protocolList;
130 struct objc_method_description_list *_Nullable instanceMethods;
131 struct objc_method_description_list *_Nullable classMethods;
132}
133@end
134
135enum objc_property_attributes {
136 OBJC_PROPERTY_READONLY = 0x01,
137 OBJC_PROPERTY_GETTER = 0x02,
138 OBJC_PROPERTY_ASSIGN = 0x04,
139 OBJC_PROPERTY_READWRITE = 0x08,
140 OBJC_PROPERTY_RETAIN = 0x10,
141 OBJC_PROPERTY_COPY = 0x20,
142 OBJC_PROPERTY_NONATOMIC = 0x40,
143 OBJC_PROPERTY_SETTER = 0x80
144};
145
146enum objc_property_extended_attributes {
147 OBJC_PROPERTY_SYNTHESIZED = 0x1,
148 OBJC_PROPERTY_DYNAMIC = 0x2,
149 OBJC_PROPERTY_PROTOCOL = 0x3,
150 OBJC_PROPERTY_ATOMIC = 0x4,
151 OBJC_PROPERTY_WEAK = 0x8,
152 OBJC_PROPERTY_STRONG = 0x10,
153 OBJC_PROPERTY_UNSAFE_UNRETAINED = 0x20
154};
155
156struct objc_property {
157 const char *_Nonnull name;
158 unsigned char attributes, extendedAttributes;
159 struct {
160 const char *_Nullable name;
161 const char *_Nullable typeEncoding;
162 } getter, setter;
163};
164
165struct objc_property_list {
166 unsigned int count;
167 struct objc_property_list *_Nullable next;
168 struct objc_property properties[1];
169};
170
171struct objc_static_instances {
172 const char *_Nonnull className;
173 id _Nullable instances[1];
174};
175
176struct objc_symtab {
177 unsigned long unknown;
178 struct objc_selector *_Nullable selectorRefs;
179 uint16_t classDefsCount;
180 uint16_t categoryDefsCount;
181 void *_Nonnull defs[1];
182};
183
184struct objc_module {
185 unsigned long version; /* 9 = non-fragile */
186 unsigned long size;
187 const char *_Nullable name;
188 struct objc_symtab *_Nonnull symtab;
189};
190
191struct objc_hashtable_bucket {
192 const void *_Nonnull key, *_Nonnull object;
193 uint32_t hash;
194};
195
196struct objc_hashtable {
197 objc_hashtable_hash_func hash;
198 objc_hashtable_equal_func equal;
199 uint32_t count, size;
200 struct objc_hashtable_bucket *_Nonnull *_Nullable data;
201};
202
203struct objc_sparsearray {
204 struct objc_sparsearray_data {
205 void *_Nullable next[256];
206 } *_Nonnull data;
207 uint8_t levels;
208};
209
210struct objc_dtable {
211 struct objc_dtable_level2 {
212#ifdef OF_SELUID24
213 struct objc_dtable_level3 {
214 IMP _Nullable buckets[256];
215 } *_Nonnull buckets[256];
216#else
217 IMP _Nullable buckets[256];
218#endif
219 } *_Nonnull buckets[256];
220};
221
222extern void objc_registerAllCategories(struct objc_symtab *_Nonnull)
223 OF_VISIBILITY_HIDDEN;
224extern struct objc_category *_Nullable *_Nullable
225 objc_categoriesForClass(Class _Nonnull) OF_VISIBILITY_HIDDEN;
226extern void objc_unregisterAllCategories(void) OF_VISIBILITY_HIDDEN;
227extern void objc_initializeClass(Class _Nonnull) OF_VISIBILITY_HIDDEN;
228extern void objc_updateDTable(Class _Nonnull) OF_VISIBILITY_HIDDEN;
229extern void objc_registerAllClasses(struct objc_symtab *_Nonnull)
230 OF_VISIBILITY_HIDDEN;
231extern Class _Nullable objc_classnameToClass(const char *_Nonnull, bool)
232 OF_VISIBILITY_HIDDEN;
233extern void objc_unregisterClass(Class _Nonnull) OF_VISIBILITY_HIDDEN;
234extern void objc_unregisterAllClasses(void) OF_VISIBILITY_HIDDEN;
235extern uint32_t objc_string_hash(const void *_Nonnull) OF_VISIBILITY_HIDDEN;
236extern bool objc_string_equal(const void *_Nonnull, const void *_Nonnull)
237 OF_VISIBILITY_HIDDEN;
238extern struct objc_hashtable *_Nonnull objc_hashtable_new(
239 objc_hashtable_hash_func, objc_hashtable_equal_func, uint32_t)
240 OF_VISIBILITY_HIDDEN;
241extern struct objc_hashtable_bucket objc_deletedBucket OF_VISIBILITY_HIDDEN;
242extern void objc_hashtable_set(struct objc_hashtable *_Nonnull,
243 const void *_Nonnull, const void *_Nonnull) OF_VISIBILITY_HIDDEN;
244extern void *_Nullable objc_hashtable_get(struct objc_hashtable *_Nonnull,
245 const void *_Nonnull) OF_VISIBILITY_HIDDEN;
246extern void objc_hashtable_delete(struct objc_hashtable *_Nonnull,
247 const void *_Nonnull) OF_VISIBILITY_HIDDEN;
248extern void objc_hashtable_free(struct objc_hashtable *_Nonnull)
249 OF_VISIBILITY_HIDDEN;
250extern void objc_registerSelector(struct objc_selector *_Nonnull)
251 OF_VISIBILITY_HIDDEN;
252extern void objc_registerAllSelectors(struct objc_symtab *_Nonnull)
253 OF_VISIBILITY_HIDDEN;
254extern void objc_unregisterAllSelectors(void) OF_VISIBILITY_HIDDEN;
255extern struct objc_sparsearray *_Nonnull objc_sparsearray_new(uint8_t)
256 OF_VISIBILITY_HIDDEN;
257extern void *_Nullable objc_sparsearray_get(struct objc_sparsearray *_Nonnull,
258 uintptr_t) OF_VISIBILITY_HIDDEN;
259extern void objc_sparsearray_set(struct objc_sparsearray *_Nonnull, uintptr_t,
260 void *_Nullable) OF_VISIBILITY_HIDDEN;
261extern void objc_sparsearray_free(struct objc_sparsearray *_Nonnull)
262 OF_VISIBILITY_HIDDEN;
263extern struct objc_dtable *_Nonnull objc_dtable_new(void) OF_VISIBILITY_HIDDEN;
264extern void objc_dtable_copy(struct objc_dtable *_Nonnull,
265 struct objc_dtable *_Nonnull) OF_VISIBILITY_HIDDEN;
266extern void objc_dtable_set(struct objc_dtable *_Nonnull, uint32_t,
267 IMP _Nullable) OF_VISIBILITY_HIDDEN;
268extern void objc_dtable_free(struct objc_dtable *_Nonnull) OF_VISIBILITY_HIDDEN;
269extern void objc_dtable_cleanup(void) OF_VISIBILITY_HIDDEN;
270extern void objc_initStaticInstances(struct objc_symtab *_Nonnull)
271 OF_VISIBILITY_HIDDEN;
272extern void objc_forgetPendingStaticInstances(void) OF_VISIBILITY_HIDDEN;
273extern void objc_zeroWeakReferences(id _Nonnull) OF_VISIBILITY_HIDDEN;
274extern Class _Nullable object_getTaggedPointerClass(id _Nonnull)
275 OF_VISIBILITY_HIDDEN;
276#ifdef OF_HAVE_THREADS
277extern void objc_globalMutex_lock(void) OF_VISIBILITY_HIDDEN;
278extern void objc_globalMutex_unlock(void) OF_VISIBILITY_HIDDEN;
279extern void objc_globalMutex_free(void) OF_VISIBILITY_HIDDEN;
280#else
281# define objc_globalMutex_lock()
282# define objc_globalMutex_unlock()
283# define objc_globalMutex_free()
284#endif
285extern char *_Nullable objc_strdup(const char *_Nonnull string)
286 OF_VISIBILITY_HIDDEN;
287
288static OF_INLINE IMP _Nullable
289objc_dtable_get(const struct objc_dtable *_Nonnull dtable, uint32_t idx)
290{
291#ifdef OF_SELUID24
292 uint8_t i = idx >> 16;
293 uint8_t j = idx >> 8;
294 uint8_t k = idx;
295
296 return dtable->buckets[i]->buckets[j]->buckets[k];
297#else
298 uint8_t i = idx >> 8;
299 uint8_t j = idx;
300
301 return dtable->buckets[i]->buckets[j];
302#endif
303}
304
305extern void OF_NO_RETURN_FUNC objc_error(const char *_Nonnull title,
306 const char *_Nonnull format, ...) OF_VISIBILITY_HIDDEN;
307#define OBJC_ERROR(...) \
308 objc_error("ObjFWRT @ " __FILE__ ":" OF_STRINGIFY(__LINE__), \
309 __VA_ARGS__)
310
311#if defined(OF_ELF)
312# if defined(OF_AMD64) || defined(OF_X86) || \
313 defined(OF_POWERPC64) || defined(OF_POWERPC) || \
314 defined(OF_ARM64) || defined(OF_ARM) || \
315 defined(OF_MIPS64_N64) || defined(OF_MIPS) || \
316 defined(OF_SPARC64) || defined(OF_SPARC)
317# define OF_ASM_LOOKUP
318# endif
319#elif defined(OF_MACH_O)
320# if defined(OF_AMD64)
321# define OF_ASM_LOOKUP
322# endif
323#elif defined(OF_WINDOWS)
324# if defined(OF_AMD64) || defined(OF_X86)
325# define OF_ASM_LOOKUP
326# endif
327#endif
328
329@interface DummyObject
330{
331 Class _Nonnull isa;
332}
333
334@property (readonly, nonatomic) bool allowsWeakReference;
335
336+ (void)initialize;
337+ (bool)resolveClassMethod: (nonnull SEL)selector;
338+ (bool)resolveInstanceMethod: (nonnull SEL)selector;
339- (nonnull id)retain;
340- (void)release;
341- (nonnull id)autorelease;
342- (nonnull id)copy;
343- (nonnull id)mutableCopy;
344- (bool)retainWeakReference;
345@end
id(* IMP)(id object, SEL selector,...)
A method implementation.
Definition ObjFWRT.h:146
A pointer to a class.
Definition private.h:37
A pointer to any object.
Definition private.h:66