1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/* GNU Objective C Runtime protocol related functions.
Copyright (C) 2010-2016 Free Software Foundation, Inc.
Contributed by Nicola Pero
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include "objc-private/common.h"
#include "objc/runtime.h"
#include "objc-private/module-abi-8.h" /* For runtime structures */
#include "objc/thr.h"
#include "objc-private/runtime.h" /* the kitchen sink */
#include "objc-private/hash.h" /* For the hash table of protocols. */
#include "objc-private/protocols.h" /* For __objc_protocols_init() and
__objc_protocols_add_protocol(). */
#include <stdlib.h> /* For malloc. */
/* This is a table that maps a name to a Protocol instance with that
name. Because there may be multiple Protocol instances with the
same name (no harm in that) the table records only one
instance. */
static cache_ptr __protocols_hashtable;
/* A mutex protecting the protocol_hashtable. */
static objc_mutex_t __protocols_hashtable_lock = NULL;
/* Called at startup by init.c. */
void
__objc_protocols_init (void)
{
__protocols_hashtable_lock = objc_mutex_allocate ();
/* The keys in the table are strings, and the values are Protocol
objects. */
__protocols_hashtable = objc_hash_new (64, (hash_func_type) objc_hash_string,
(compare_func_type) objc_compare_strings);
}
/* Add a protocol to the hashtable. */
void
__objc_protocols_add_protocol (const char *name, struct objc_protocol *object)
{
objc_mutex_lock (__protocols_hashtable_lock);
/* If we find a protocol with the same name already in the
hashtable, we do not need to add the new one, because it will be
identical to it. This in the reasonable assumption that two
protocols with the same name are identical, which is expected in
any sane program. If we are really paranoid, we would compare
the protocols and abort if they are not identical.
Unfortunately, this would slow down the startup of all
Objective-C programs while trying to catch a problem that has
never been seen in practice, so we don't do it. */
if (! objc_hash_is_key_in_hash (__protocols_hashtable, name))
objc_hash_add (&__protocols_hashtable, name, object);
objc_mutex_unlock (__protocols_hashtable_lock);
}
Protocol *
objc_getProtocol (const char *name)
{
Protocol *protocol;
if (name == NULL)
return NULL;
objc_mutex_lock (__protocols_hashtable_lock);
protocol = (Protocol *)(objc_hash_value_for_key (__protocols_hashtable, name));
objc_mutex_unlock (__protocols_hashtable_lock);
return protocol;
}
Protocol **
objc_copyProtocolList (unsigned int *numberOfReturnedProtocols)
{
unsigned int count = 0;
Protocol **returnValue = NULL;
node_ptr node;
objc_mutex_lock (__protocols_hashtable_lock);
/* Count how many protocols we have. */
node = objc_hash_next (__protocols_hashtable, NULL);
while (node)
{
count++;
node = objc_hash_next (__protocols_hashtable, node);
}
if (count != 0)
{
unsigned int i = 0;
/* Allocate enough memory to hold them. */
returnValue = (Protocol **)(malloc (sizeof (Protocol *) * (count + 1)));
/* Copy the protocols. */
node = objc_hash_next (__protocols_hashtable, NULL);
while (node)
{
returnValue[i] = node->value;
i++;
node = objc_hash_next (__protocols_hashtable, node);
}
returnValue[i] = NULL;
}
objc_mutex_unlock (__protocols_hashtable_lock);
if (numberOfReturnedProtocols)
*numberOfReturnedProtocols = count;
return returnValue;
}
BOOL
class_addProtocol (Class class_, Protocol *protocol)
{
struct objc_protocol_list *protocols;
if (class_ == Nil || protocol == NULL)
return NO;
if (class_conformsToProtocol (class_, protocol))
return NO;
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return NO;
objc_mutex_lock (__objc_runtime_mutex);
/* Create the objc_protocol_list. */
protocols = malloc (sizeof (struct objc_protocol_list));
protocols->count = 1;
protocols->list[0] = (struct objc_protocol *)protocol;
/* Attach it to the list of class protocols. */
protocols->next = class_->protocols;
class_->protocols = protocols;
objc_mutex_unlock (__objc_runtime_mutex);
return YES;
}
BOOL
class_conformsToProtocol (Class class_, Protocol *protocol)
{
struct objc_protocol_list* proto_list;
if (class_ == Nil || protocol == NULL)
return NO;
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return NO;
/* Acquire the runtime lock because the list of protocols for a
class may be modified concurrently, for example if another thread
calls class_addProtocol(), or dynamically loads from a file a
category of the class. */
objc_mutex_lock (__objc_runtime_mutex);
proto_list = class_->protocols;
while (proto_list)
{
size_t i;
for (i = 0; i < proto_list->count; i++)
{
if (proto_list->list[i] == (struct objc_protocol *)protocol
|| protocol_conformsToProtocol ((Protocol *)proto_list->list[i],
protocol))
{
objc_mutex_unlock (__objc_runtime_mutex);
return YES;
}
}
proto_list = proto_list->next;
}
objc_mutex_unlock (__objc_runtime_mutex);
return NO;
}
Protocol **
class_copyProtocolList (Class class_, unsigned int *numberOfReturnedProtocols)
{
unsigned int count = 0;
Protocol **returnValue = NULL;
struct objc_protocol_list* proto_list;
if (class_ == Nil)
{
if (numberOfReturnedProtocols)
*numberOfReturnedProtocols = 0;
return NULL;
}
/* Lock the runtime mutex because the class protocols may be
concurrently modified. */
objc_mutex_lock (__objc_runtime_mutex);
/* Count how many protocols we have. */
proto_list = class_->protocols;
while (proto_list)
{
count = count + proto_list->count;
proto_list = proto_list->next;
}
if (count != 0)
{
unsigned int i = 0;
/* Allocate enough memory to hold them. */
returnValue = (Protocol **)(malloc (sizeof (Protocol *) * (count + 1)));
/* Copy the protocols. */
proto_list = class_->protocols;
while (proto_list)
{
size_t j;
for (j = 0; j < proto_list->count; j++)
{
returnValue[i] = (Protocol *)proto_list->list[j];
i++;
}
proto_list = proto_list->next;
}
returnValue[i] = NULL;
}
objc_mutex_unlock (__objc_runtime_mutex);
if (numberOfReturnedProtocols)
*numberOfReturnedProtocols = count;
return returnValue;
}
BOOL
protocol_conformsToProtocol (Protocol *protocol, Protocol *anotherProtocol)
{
struct objc_protocol_list* proto_list;
if (protocol == NULL || anotherProtocol == NULL)
return NO;
if (protocol == anotherProtocol)
return YES;
/* Check that the objects are Protocol objects before casting them
to (struct objc_protocol *). */
if (protocol->class_pointer != anotherProtocol->class_pointer)
return NO;
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return NO;
if (strcmp (((struct objc_protocol *)protocol)->protocol_name,
((struct objc_protocol *)anotherProtocol)->protocol_name) == 0)
return YES;
/* We do not acquire any lock because protocols are currently
immutable. We can freely iterate over a protocol structure. */
proto_list = ((struct objc_protocol *)protocol)->protocol_list;
while (proto_list)
{
size_t i;
for (i = 0; i < proto_list->count; i++)
{
if (protocol_conformsToProtocol ((Protocol *)proto_list->list[i], anotherProtocol))
return YES;
}
proto_list = proto_list->next;
}
return NO;
}
BOOL
protocol_isEqual (Protocol *protocol, Protocol *anotherProtocol)
{
if (protocol == anotherProtocol)
return YES;
if (protocol == NULL || anotherProtocol == NULL)
return NO;
/* Check that the objects are Protocol objects before casting them
to (struct objc_protocol *). */
if (protocol->class_pointer != anotherProtocol->class_pointer)
return NO;
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return NO;
/* Equality between formal protocols is only formal (nothing to do
with actually checking the list of methods they have!). Two
formal Protocols are equal if and only if they have the same
name.
Please note (for comparisons with other implementations) that
checking the names is equivalent to checking that Protocol A
conforms to Protocol B and Protocol B conforms to Protocol A,
because this happens iff they have the same name. If they have
different names, A conforms to B if and only if A includes B, but
the situation where A includes B and B includes A is a circular
dependency between Protocols which is forbidden by the compiler,
so A conforms to B and B conforms to A with A and B having
different names is an impossible case. */
if (strcmp (((struct objc_protocol *)protocol)->protocol_name,
((struct objc_protocol *)anotherProtocol)->protocol_name) == 0)
return YES;
return NO;
}
const char *
protocol_getName (Protocol *protocol)
{
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return NULL;
return ((struct objc_protocol *)protocol)->protocol_name;
}
struct objc_method_description protocol_getMethodDescription (Protocol *protocol,
SEL selector,
BOOL requiredMethod,
BOOL instanceMethod)
{
struct objc_method_description no_result = { NULL, NULL };
struct objc_method_description_list *methods;
int i;
/* TODO: New ABI. */
/* The current ABI does not have any information on optional protocol methods. */
if (! requiredMethod)
return no_result;
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return no_result;
if (instanceMethod)
methods = ((struct objc_protocol *)protocol)->instance_methods;
else
methods = ((struct objc_protocol *)protocol)->class_methods;
if (methods)
{
for (i = 0; i < methods->count; i++)
{
if (sel_isEqual (methods->list[i].name, selector))
return methods->list[i];
/*
if (strcmp (sel_getName (methods->list[i].name), selector_name) == 0)
return methods->list[i];
*/
}
}
return no_result;
}
struct objc_method_description *protocol_copyMethodDescriptionList (Protocol *protocol,
BOOL requiredMethod,
BOOL instanceMethod,
unsigned int *numberOfReturnedMethods)
{
struct objc_method_description_list *methods;
unsigned int count = 0;
struct objc_method_description *returnValue = NULL;
/* TODO: New ABI */
/* The current ABI does not have any information on optional protocol methods. */
if (! requiredMethod)
{
if (numberOfReturnedMethods)
*numberOfReturnedMethods = 0;
return NULL;
}
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol == NULL || protocol->class_pointer != objc_lookUpClass ("Protocol"))
{
if (numberOfReturnedMethods)
*numberOfReturnedMethods = 0;
return NULL;
}
/* We do not acquire any lock because protocols are currently
immutable. We can freely iterate over a protocol structure. */
if (instanceMethod)
methods = ((struct objc_protocol *)protocol)->instance_methods;
else
methods = ((struct objc_protocol *)protocol)->class_methods;
if (methods)
{
unsigned int i;
count = methods->count;
/* Allocate enough memory to hold them. */
returnValue = (struct objc_method_description *)(malloc (sizeof (struct objc_method_description) * (count + 1)));
/* Copy them. */
for (i = 0; i < count; i++)
{
returnValue[i].name = methods->list[i].name;
returnValue[i].types = methods->list[i].types;
}
returnValue[i].name = NULL;
returnValue[i].types = NULL;
}
if (numberOfReturnedMethods)
*numberOfReturnedMethods = count;
return returnValue;
}
Property protocol_getProperty (Protocol *protocol, const char *propertyName,
BOOL requiredProperty, BOOL instanceProperty)
{
if (protocol == NULL || propertyName == NULL)
return NULL;
if (!requiredProperty || !instanceProperty)
return NULL;
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol->class_pointer != objc_lookUpClass ("Protocol"))
return NULL;
/* TODO: New ABI. */
/* The current ABI does not have any information on protocol properties. */
return NULL;
}
Property *protocol_copyPropertyList (Protocol *protocol, unsigned int *numberOfReturnedProperties)
{
unsigned int count = 0;
Property *returnValue = NULL;
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol == NULL || protocol->class_pointer != objc_lookUpClass ("Protocol"))
{
if (numberOfReturnedProperties)
*numberOfReturnedProperties = 0;
return NULL;
}
/* We do not acquire any lock because protocols are currently
immutable. We can freely iterate over a protocol structure. */
/* TODO: New ABI. */
/* The current ABI does not have any information on protocol properties. */
if (numberOfReturnedProperties)
*numberOfReturnedProperties = count;
return returnValue;
}
Protocol **protocol_copyProtocolList (Protocol *protocol, unsigned int *numberOfReturnedProtocols)
{
unsigned int count = 0;
Protocol **returnValue = NULL;
struct objc_protocol_list* proto_list;
/* Check that it is a Protocol object before casting it to (struct
objc_protocol *). */
if (protocol == NULL || protocol->class_pointer != objc_lookUpClass ("Protocol"))
{
if (numberOfReturnedProtocols)
*numberOfReturnedProtocols = 0;
return NULL;
}
/* We do not acquire any lock because protocols are currently
immutable. We can freely iterate over a protocol structure. */
/* Count how many protocols we have. */
proto_list = ((struct objc_protocol *)protocol)->protocol_list;
while (proto_list)
{
count = count + proto_list->count;
proto_list = proto_list->next;
}
if (count != 0)
{
unsigned int i = 0;
/* Allocate enough memory to hold them. */
returnValue = (Protocol **)(malloc (sizeof (Protocol *) * (count + 1)));
/* Copy the protocols. */
proto_list = ((struct objc_protocol *)protocol)->protocol_list;
while (proto_list)
{
size_t j;
for (j = 0; j < proto_list->count; j++)
{
returnValue[i] = (Protocol *)proto_list->list[j];
i++;
}
proto_list = proto_list->next;
}
returnValue[i] = NULL;
}
if (numberOfReturnedProtocols)
*numberOfReturnedProtocols = count;
return returnValue;
}
|