-
Notifications
You must be signed in to change notification settings - Fork 28
/
CDTextClassDumpVisitor.m
297 lines (246 loc) · 9.65 KB
/
CDTextClassDumpVisitor.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
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-2019 Steve Nygard.
#import "CDTextClassDumpVisitor.h"
#import "CDClassDump.h"
#import "CDOCClass.h"
#import "CDOCCategory.h"
#import "CDOCMethod.h"
#import "CDOCProperty.h"
#import "CDTypeController.h"
#import "CDTypeFormatter.h"
#import "CDVisitorPropertyState.h"
#import "CDOCInstanceVariable.h"
static BOOL debug = NO;
@interface CDTextClassDumpVisitor ()
@end
#pragma mark -
@implementation CDTextClassDumpVisitor
{
NSMutableString *_resultString;
}
- (id)init;
{
if ((self = [super init])) {
_resultString = [[NSMutableString alloc] init];
}
return self;
}
#pragma mark -
- (void)willVisitClass:(CDOCClass *)aClass;
{
if (aClass.isExported == NO)
[self.resultString appendString:@"__attribute__((visibility(\"hidden\")))\n"];
[self.resultString appendFormat:@"@interface %@", aClass.name];
if (aClass.superClassName != nil)
[self.resultString appendFormat:@" : %@", aClass.superClassName];
NSArray *protocols = aClass.protocols;
if ([protocols count] > 0) {
[self.resultString appendFormat:@" <%@>", aClass.protocolsString];
}
[self.resultString appendString:@"\n"];
}
- (void)didVisitClass:(CDOCClass *)aClass;
{
if (aClass.hasMethods)
[self.resultString appendString:@"\n"];
[self.resultString appendString:@"@end\n\n"];
}
- (void)willVisitIvarsOfClass:(CDOCClass *)aClass;
{
[self.resultString appendString:@"{\n"];
}
- (void)didVisitIvarsOfClass:(CDOCClass *)aClass;
{
[self.resultString appendString:@"}\n\n"];
}
- (void)willVisitCategory:(CDOCCategory *)category;
{
[self.resultString appendFormat:@"@interface %@ (%@)", category.className, category.name];
NSArray *protocols = category.protocols;
if ([protocols count] > 0) {
[self.resultString appendFormat:@" <%@>", category.protocolsString];
}
[self.resultString appendString:@"\n"];
}
- (void)didVisitCategory:(CDOCCategory *)category;
{
[self.resultString appendString:@"@end\n\n"];
}
- (void)willVisitProtocol:(CDOCProtocol *)protocol;
{
[self.resultString appendFormat:@"@protocol %@", protocol.name];
NSArray *protocols = protocol.protocols;
if ([protocols count] > 0) {
[self.resultString appendFormat:@" <%@>", protocol.protocolsString];
}
[self.resultString appendString:@"\n"];
}
- (void)willVisitOptionalMethods;
{
[self.resultString appendString:@"\n@optional\n"];
}
- (void)didVisitProtocol:(CDOCProtocol *)protocol;
{
[self.resultString appendString:@"@end\n\n"];
}
- (void)visitClassMethod:(CDOCMethod *)method;
{
[self.resultString appendString:@"+ "];
[method appendToString:self.resultString typeController:self.classDump.typeController];
[self.resultString appendString:@"\n"];
}
- (void)visitInstanceMethod:(CDOCMethod *)method propertyState:(CDVisitorPropertyState *)propertyState;
{
CDOCProperty *property = [propertyState propertyForAccessor:method.name];
if (property == nil) {
//NSLog(@"No property for method: %@", method.name);
[self.resultString appendString:@"- "];
[method appendToString:self.resultString typeController:self.classDump.typeController];
[self.resultString appendString:@"\n"];
} else {
if ([propertyState hasUsedProperty:property] == NO) {
//NSLog(@"Emitting property %@ triggered by method %@", property.name, method.name);
[self visitProperty:property];
[propertyState useProperty:property];
} else {
//NSLog(@"Have already emitted property %@ triggered by method %@", property.name, method.name);
}
}
}
- (void)visitIvar:(CDOCInstanceVariable *)ivar;
{
[ivar appendToString:self.resultString typeController:self.classDump.typeController];
[self.resultString appendString:@"\n"];
}
- (void)visitProperty:(CDOCProperty *)property;
{
CDType *parsedType = property.type;
if (parsedType == nil) {
if ([property.attributeString hasPrefix:@"T"]) {
[self.resultString appendFormat:@"// Error parsing type for property %@:\n", property.name];
[self.resultString appendFormat:@"// Property attributes: %@\n\n", property.attributeString];
} else {
[self.resultString appendFormat:@"// Error: Property attributes should begin with the type ('T') attribute, property name: %@\n", property.name];
[self.resultString appendFormat:@"// Property attributes: %@\n\n", property.attributeString];
}
} else {
[self _visitProperty:property parsedType:parsedType attributes:property.attributes];
}
}
- (void)didVisitPropertiesOfClass:(CDOCClass *)aClass;
{
if ([aClass.properties count] > 0)
[self.resultString appendString:@"\n"];
}
- (void)willVisitPropertiesOfCategory:(CDOCCategory *)category;
{
if ([category.properties count] > 0)
[self.resultString appendString:@"\n"];
}
- (void)didVisitPropertiesOfCategory:(CDOCCategory *)category;
{
if ([category.properties count] > 0/* && [aCategory hasMethods]*/)
[self.resultString appendString:@"\n"];
}
- (void)willVisitPropertiesOfProtocol:(CDOCProtocol *)protocol;
{
if ([protocol.properties count] > 0)
[self.resultString appendString:@"\n"];
}
- (void)didVisitPropertiesOfProtocol:(CDOCProtocol *)protocol;
{
if ([protocol.properties count] > 0 /*&& [aProtocol hasMethods]*/)
[self.resultString appendString:@"\n"];
}
- (void)visitRemainingProperties:(CDVisitorPropertyState *)propertyState;
{
NSArray *remaining = propertyState.remainingProperties;
if ([remaining count] > 0) {
[self.resultString appendString:@"\n"];
[self.resultString appendFormat:@"// Remaining properties\n"];
//NSLog(@"Warning: remaining undeclared property count: %u", [remaining count]);
//NSLog(@"remaining: %@", remaining);
for (CDOCProperty *property in remaining)
[self visitProperty:property];
}
}
#pragma mark -
@synthesize resultString = _resultString;
- (void)writeResultToStandardOutput;
{
NSData *data = [self.resultString dataUsingEncoding:NSUTF8StringEncoding];
[(NSFileHandle *)[NSFileHandle fileHandleWithStandardOutput] writeData:data];
}
- (void)_visitProperty:(CDOCProperty *)property parsedType:(CDType *)parsedType attributes:(NSArray *)attrs;
{
NSString *backingVar = nil;
BOOL isWeak = NO;
BOOL isDynamic = NO;
NSMutableArray *alist = [[NSMutableArray alloc] init];
NSMutableArray *unknownAttrs = [[NSMutableArray alloc] init];
// objc_v2_encode_prop_attr() in gcc/objc/objc-act.c
for (NSString *attr in attrs) {
if ([attr hasPrefix:@"T"]) {
if (debug) NSLog(@"Warning: Property attribute 'T' should occur only occur at the beginning");
} else if ([attr hasPrefix:@"R"]) {
[alist addObject:@"readonly"];
} else if ([attr hasPrefix:@"C"]) {
[alist addObject:@"copy"];
} else if ([attr hasPrefix:@"&"]) {
[alist addObject:@"retain"];
} else if ([attr hasPrefix:@"G"]) {
[alist addObject:[NSString stringWithFormat:@"getter=%@", [attr substringFromIndex:1]]];
} else if ([attr hasPrefix:@"S"]) {
[alist addObject:[NSString stringWithFormat:@"setter=%@", [attr substringFromIndex:1]]];
} else if ([attr hasPrefix:@"V"]) {
backingVar = [attr substringFromIndex:1];
} else if ([attr hasPrefix:@"N"]) {
[alist addObject:@"nonatomic"];
} else if ([attr hasPrefix:@"W"]) {
// @property(assign) __weak NSObject *prop;
// Only appears with GC.
isWeak = YES;
} else if ([attr hasPrefix:@"P"]) {
// @property(assign) __strong NSObject *prop;
// Only appears with GC.
// This is the default.
isWeak = NO;
} else if ([attr hasPrefix:@"D"]) {
// Dynamic property. Implementation supplied at runtime.
// @property int prop; // @dynamic prop;
isDynamic = YES;
} else {
if (debug) NSLog(@"Warning: Unknown property attribute '%@'", attr);
[unknownAttrs addObject:attr];
}
}
if ([alist count] > 0) {
[self.resultString appendFormat:@"@property(%@) ", [alist componentsJoinedByString:@", "]];
} else {
[self.resultString appendString:@"@property "];
}
if (isWeak)
[self.resultString appendString:@"__weak "];
NSString *formattedString = [self.classDump.typeController.propertyTypeFormatter formatVariable:property.name type:parsedType];
[self.resultString appendFormat:@"%@;", formattedString];
if (isDynamic) {
[self.resultString appendFormat:@" // @dynamic %@;", property.name];
} else if (backingVar != nil) {
if ([backingVar isEqualToString:property.name]) {
[self.resultString appendFormat:@" // @synthesize %@;", property.name];
} else {
[self.resultString appendFormat:@" // @synthesize %@=%@;", property.name, backingVar];
}
}
[self.resultString appendString:@"\n"];
if ([unknownAttrs count] > 0) {
[self.resultString appendFormat:@"// Preceding property had unknown attributes: %@\n", [unknownAttrs componentsJoinedByString:@","]];
if ([property.attributeString length] > 80) {
[self.resultString appendFormat:@"// Original attribute string (following type): %@\n\n", property.attributeStringAfterType];
} else {
[self.resultString appendFormat:@"// Original attribute string: %@\n\n", property.attributeString];
}
}
}
@end