forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDOCProtocol.m
331 lines (269 loc) · 10.7 KB
/
CDOCProtocol.m
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
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2015 Steve Nygard.
#import "CDOCProtocol.h"
#import "CDClassDump.h"
#import "CDOCMethod.h"
#import "CDVisitor.h"
#import "CDOCProperty.h"
#import "CDMethodType.h"
#import "CDType.h"
#import "CDTypeController.h"
#import "CDVisitorPropertyState.h"
@interface CDOCProtocol ()
@property (nonatomic, readonly) NSString *sortableName;
@end
#pragma mark -
@implementation CDOCProtocol
{
NSString *_name;
NSMutableArray *_protocols;
NSMutableArray *_classMethods;
NSMutableArray *_instanceMethods;
NSMutableArray *_optionalClassMethods;
NSMutableArray *_optionalInstanceMethods;
NSMutableArray *_properties;
NSMutableSet *_adoptedProtocolNames;
}
- (id)init;
{
if ((self = [super init])) {
_name = nil;
_protocols = [[NSMutableArray alloc] init];
_classMethods = [[NSMutableArray alloc] init];
_instanceMethods = [[NSMutableArray alloc] init];
_optionalClassMethods = [[NSMutableArray alloc] init];
_optionalInstanceMethods = [[NSMutableArray alloc] init];
_properties = [[NSMutableArray alloc] init];
_adoptedProtocolNames = [[NSMutableSet alloc] init];
}
return self;
}
#pragma mark - Debugging
- (NSString *)description;
{
return [NSString stringWithFormat:@"<%@:%p> name: %@, protocols: %ld, class methods: %ld, instance methods: %ld",
NSStringFromClass([self class]), self, self.name, [self.protocols count], [self.classMethods count], [self.instanceMethods count]];
}
#pragma mark -
// This assumes that the protocol name doesn't change after it's been added to this.
- (void)addProtocol:(CDOCProtocol *)protocol;
{
if ([_adoptedProtocolNames containsObject:protocol.name] == NO) {
[_protocols addObject:protocol];
[_adoptedProtocolNames addObject:protocol.name];
}
}
- (void)removeProtocol:(CDOCProtocol *)protocol;
{
[_adoptedProtocolNames removeObject:protocol.name];
[_protocols removeObject:protocol];
}
- (NSArray *)protocolNames;
{
NSMutableArray *names = [[NSMutableArray alloc] init];
[self.protocols enumerateObjectsUsingBlock:^(CDOCProtocol *protocol, NSUInteger index, BOOL *stop){
if (protocol.name != nil)
[names addObject:protocol.name];
}];
return [names copy];
}
- (NSString *)protocolsString;
{
NSArray *names = self.protocolNames;
if ([names count] == 0)
return @"";
return [names componentsJoinedByString:@", "];
}
- (void)addClassMethod:(CDOCMethod *)method;
{
[_classMethods addObject:method];
}
- (void)addInstanceMethod:(CDOCMethod *)method;
{
[_instanceMethods addObject:method];
}
- (void)addOptionalClassMethod:(CDOCMethod *)method;
{
[_optionalClassMethods addObject:method];
}
- (void)addOptionalInstanceMethod:(CDOCMethod *)method;
{
[_optionalInstanceMethods addObject:method];
}
- (void)addProperty:(CDOCProperty *)property;
{
[_properties addObject:property];
}
- (BOOL)hasMethods;
{
return [self.classMethods count] > 0 || [self.instanceMethods count] > 0 || [self.optionalClassMethods count] > 0 || [self.optionalInstanceMethods count] > 0;
}
- (void)registerTypesWithObject:(CDTypeController *)typeController phase:(NSUInteger)phase;
{
[self registerTypesFromMethods:self.classMethods withObject:typeController phase:phase];
[self registerTypesFromMethods:self.instanceMethods withObject:typeController phase:phase];
[self registerTypesFromMethods:self.optionalClassMethods withObject:typeController phase:phase];
[self registerTypesFromMethods:self.optionalInstanceMethods withObject:typeController phase:phase];
}
- (void)registerTypesFromMethods:(NSArray *)methods withObject:(CDTypeController *)typeController phase:(NSUInteger)phase;
{
for (CDOCMethod *method in methods) {
for (CDMethodType *methodType in method.parsedMethodTypes) {
[methodType.type phase:phase registerTypesWithObject:typeController usedInMethod:YES];
}
}
}
#pragma mark - Sorting
- (NSString *)sortableName;
{
return self.name;
}
- (NSComparisonResult)ascendingCompareByName:(CDOCProtocol *)other;
{
return [self.sortableName compare:other.sortableName];
}
#pragma mark -
- (NSString *)methodSearchContext;
{
NSMutableString *resultString = [NSMutableString string];
[resultString appendFormat:@"@protocol %@", self.name];
if ([self.protocols count] > 0)
[resultString appendFormat:@" <%@>", self.protocolsString];
return resultString;
}
- (void)recursivelyVisit:(CDVisitor *)visitor;
{
if ([visitor.classDump shouldShowName:self.name] && visitor.shouldShowProtocolSection) {
CDVisitorPropertyState *propertyState = [[CDVisitorPropertyState alloc] initWithProperties:self.properties];
[visitor willVisitProtocol:self];
//[aVisitor willVisitPropertiesOfProtocol:self];
//[self visitProperties:aVisitor];
//[aVisitor didVisitPropertiesOfProtocol:self];
[self visitMethods:visitor propertyState:propertyState];
// @optional properties will generate optional instance methods, and we'll emit @property in the @optional section.
[visitor visitRemainingProperties:propertyState];
[visitor didVisitProtocol:self];
}
}
- (void)visitMethods:(CDVisitor *)visitor propertyState:(CDVisitorPropertyState *)propertyState;
{
NSArray *methods = self.classMethods;
if (visitor.classDump.shouldSortMethods)
methods = [methods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
for (CDOCMethod *method in methods)
[visitor visitClassMethod:method];
methods = self.instanceMethods;
if (visitor.classDump.shouldSortMethods)
methods = [methods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
for (CDOCMethod *method in methods)
[visitor visitInstanceMethod:method propertyState:propertyState];
if ([self.optionalClassMethods count] > 0 || [self.optionalInstanceMethods count] > 0) {
[visitor willVisitOptionalMethods];
methods = self.optionalClassMethods;
if (visitor.classDump.shouldSortMethods)
methods = [methods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
for (CDOCMethod *method in methods)
[visitor visitClassMethod:method];
methods = self.optionalInstanceMethods;
if (visitor.classDump.shouldSortMethods)
methods = [methods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
for (CDOCMethod *method in methods)
[visitor visitInstanceMethod:method propertyState:propertyState];
[visitor didVisitOptionalMethods];
}
}
#if 0
- (void)visitProperties:(CDVisitor *)visitor;
{
NSArray *array = properties;
if (visitor.classDump.shouldSortMethods)
array = [array sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
for (CDOCProperty *property in array)
[visitor visitProperty:property];
}
#endif
#pragma mark -
- (void)mergeMethodsFromProtocol:(CDOCProtocol *)other;
{
NSMutableDictionary *instanceMethodsByName = [NSMutableDictionary dictionary];
NSMutableDictionary *optionalInstanceMethodsByName = [NSMutableDictionary dictionary];
NSMutableDictionary *classMethodsByName = [NSMutableDictionary dictionary];
NSMutableDictionary *optionalClassMethodsByName = [NSMutableDictionary dictionary];
for (CDOCMethod *method in _instanceMethods)
instanceMethodsByName[method.name] = method;
for (CDOCMethod *method in _optionalInstanceMethods)
optionalInstanceMethodsByName[method.name] = method;
for (CDOCMethod *method in _classMethods)
classMethodsByName[method.name] = method;
for (CDOCMethod *method in _optionalClassMethods)
optionalClassMethodsByName[method.name] = method;
// Instance methods
for (CDOCMethod *method in other.instanceMethods) {
CDOCMethod *m2 = instanceMethodsByName[method.name];
if (m2 == nil) {
// Add if it is not an optional instance method.
if (optionalInstanceMethodsByName[method.name] == nil) {
[self addInstanceMethod:method];
instanceMethodsByName[method.name] = method;
}
}
}
for (CDOCMethod *method in other.optionalInstanceMethods) {
CDOCMethod *m2 = optionalInstanceMethodsByName[method.name];
if (m2 == nil) {
m2 = instanceMethodsByName[method.name];
if (m2 == nil) {
[self addOptionalInstanceMethod:method];
optionalInstanceMethodsByName[method.name] = method;
} else {
// Move to the optional instance methods.
[self addOptionalInstanceMethod:m2];
[_instanceMethods removeObject:m2];
optionalInstanceMethodsByName[m2.name] = m2;
[instanceMethodsByName removeObjectForKey:m2.name];
}
}
}
// Class methods
for (CDOCMethod *method in other.classMethods) {
CDOCMethod *m2 = classMethodsByName[method.name];
if (m2 == nil) {
// Add if it is not an optional class method.
if (optionalClassMethodsByName[method.name] == nil) {
[self addClassMethod:method];
classMethodsByName[method.name] = method;
}
}
}
for (CDOCMethod *method in other.optionalClassMethods) {
CDOCMethod *m2 = optionalClassMethodsByName[method.name];
if (m2 == nil) {
m2 = classMethodsByName[method.name];
if (m2 == nil) {
[self addOptionalClassMethod:method];
optionalClassMethodsByName[method.name] = method;
} else {
// Move to the optional class methods.
[self addOptionalClassMethod:m2];
[_classMethods removeObject:m2];
optionalClassMethodsByName[m2.name] = m2;
[classMethodsByName removeObjectForKey:m2.name];
}
}
}
}
- (void)mergePropertiesFromProtocol:(CDOCProtocol *)other;
{
NSMutableDictionary *propertiesByName = [NSMutableDictionary dictionary];
for (CDOCProperty *property in _properties)
propertiesByName[property.name] = property;
for (CDOCProperty *property in other.properties) {
CDOCProperty *p2 = propertiesByName[property.name];
if (p2 == nil) {
[self addProperty:property];
propertiesByName[property.name] = property;
}
}
}
@end