/
CDTypeFormatter.m
280 lines (231 loc) · 9.32 KB
/
CDTypeFormatter.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
// -*- 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-2013 Steve Nygard.
#import "CDTypeFormatter.h"
#import "CDMethodType.h"
#import "CDType.h"
#import "CDTypeLexer.h"
#import "CDTypeParser.h"
#import "CDTypeController.h"
static BOOL debug = NO;
@interface CDTypeFormatter ()
@end
#pragma mark -
@implementation CDTypeFormatter
{
__weak CDTypeController *_typeController;
NSUInteger _baseLevel;
BOOL _shouldExpand; // But just top level struct, level == 0
BOOL _shouldAutoExpand;
BOOL _shouldShowLexing;
}
- (id)init;
{
if ((self = [super init])) {
_typeController = nil;
_baseLevel = 0;
_shouldExpand = NO;
_shouldAutoExpand = NO;
_shouldShowLexing = debug;
}
return self;
}
#pragma mark - Debugging
- (NSString *)description;
{
return [NSString stringWithFormat:@"<%@:%p> baseLevel: %lu, shouldExpand: %u, shouldAutoExpand: %u, shouldShowLexing: %u, tc: %p",
NSStringFromClass([self class]), self,
self.baseLevel, self.shouldExpand, self.shouldAutoExpand, self.shouldShowLexing, self.typeController];
}
#pragma mark -
- (NSString *)_specialCaseVariable:(NSString *)name type:(NSString *)type;
{
if ([type isEqual:@"c"]) {
if (name == nil)
return @"BOOL";
else
return [NSString stringWithFormat:@"BOOL %@", name];
#if 0
} else if ([type isEqual:@"b1"]) {
if (name == nil)
return @"BOOL :1";
else
return [NSString stringWithFormat:@"BOOL %@:1", name];
#endif
}
return nil;
}
- (NSString *)_specialCaseVariable:(NSString *)name parsedType:(CDType *)type;
{
if (type.primitiveType == 'c') {
if (name == nil)
return @"BOOL";
else
return [NSString stringWithFormat:@"BOOL %@", name];
}
return nil;
}
- (NSString *)formatVariable:(NSString *)name type:(CDType *)type;
{
NSMutableString *resultString = [NSMutableString string];
NSString *specialCase = [self _specialCaseVariable:name parsedType:type];
[resultString appendSpacesIndentedToLevel:self.baseLevel spacesPerLevel:4];
if (specialCase != nil) {
[resultString appendString:specialCase];
} else {
// TODO: (2009-08-26) Ideally, just formatting a type shouldn't change it. These changes should be done before, but this is handy.
type.variableName = name;
[type phase0RecursivelyFixStructureNames:NO]; // Nuke the $_ names
[type phase3MergeWithTypeController:self.typeController];
[resultString appendString:[type formattedString:nil formatter:self level:0]];
}
return resultString;
}
- (NSDictionary *)formattedTypesForMethodName:(NSString *)name type:(NSString *)type;
{
CDTypeParser *parser = [[CDTypeParser alloc] initWithString:type];
NSError *error = nil;
NSArray *methodTypes = [parser parseMethodType:&error];
if (methodTypes == nil)
NSLog(@"Warning: Parsing method types failed, %@", name);
if (methodTypes == nil || [methodTypes count] == 0) {
return nil;
}
NSMutableDictionary *typeDict = [NSMutableDictionary dictionary];
{
NSUInteger count = [methodTypes count];
NSUInteger index = 0;
BOOL noMoreTypes = NO;
CDMethodType *methodType = methodTypes[index];
NSString *specialCase = [self _specialCaseVariable:nil type:methodType.type.bareTypeString];
if (specialCase != nil) {
[typeDict setValue:specialCase forKey:@"return-type"];
} else {
NSString *str = [[methodType type] formattedString:nil formatter:self level:0];
if (str != nil)
[typeDict setValue:str forKey:@"return-type"];
}
index += 3;
NSMutableArray *parameterTypes = [NSMutableArray array];
[typeDict setValue:parameterTypes forKey:@"parametertypes"];
NSScanner *scanner = [[NSScanner alloc] initWithString:name];
while ([scanner isAtEnd] == NO) {
NSString *str;
// We can have unnamed parameters, :::
if ([scanner scanUpToString:@":" intoString:&str]) {
//NSLog(@"str += '%@'", str);
// int unnamedCount, unnamedIndex;
// unnamedCount = [str length];
// for (unnamedIndex = 0; unnamedIndex < unnamedCount; unnamedIndex++)
// [parameterTypes addObject:@{ @"type": @"", @"name": @""}];
}
if ([scanner scanString:@":" intoString:NULL]) {
if (index >= count) {
noMoreTypes = YES;
} else {
NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
methodType = methodTypes[index];
specialCase = [self _specialCaseVariable:nil type:methodType.type.bareTypeString];
if (specialCase != nil) {
[parameter setValue:specialCase forKey:@"type"];
} else {
NSString *typeString = [methodType.type formattedString:nil formatter:self level:0];
[parameter setValue:typeString forKey:@"type"];
}
//[parameter setValue:[NSString stringWithFormat:@"fp%@", methodType.offset] forKey:@"name"];
[parameter setValue:[NSString stringWithFormat:@"arg%lu", index-2] forKey:@"name"];
[parameterTypes addObject:parameter];
index++;
}
}
}
if (noMoreTypes) {
NSLog(@" /* Error: Ran out of types for this method. */");
}
}
return typeDict;
}
- (NSString *)formatMethodName:(NSString *)methodName typeString:(NSString *)typeString;
{
CDTypeParser *parser = [[CDTypeParser alloc] initWithString:typeString];
NSError *error = nil;
NSArray *methodTypes = [parser parseMethodType:&error];
if (methodTypes == nil)
NSLog(@"Warning: Parsing method types failed, %@", methodName);
if (methodTypes == nil || [methodTypes count] == 0) {
return nil;
}
NSMutableString *resultString = [NSMutableString string];
{
NSUInteger count = [methodTypes count];
NSUInteger index = 0;
BOOL noMoreTypes = NO;
CDMethodType *methodType = methodTypes[index];
[resultString appendString:@"("];
NSString *specialCase = [self _specialCaseVariable:nil type:methodType.type.bareTypeString];
if (specialCase != nil) {
[resultString appendString:specialCase];
} else {
NSString *str = [methodType.type formattedString:nil formatter:self level:0];
if (str != nil)
[resultString appendFormat:@"%@", str];
}
[resultString appendString:@")"];
index += 3;
NSScanner *scanner = [[NSScanner alloc] initWithString:methodName];
while ([scanner isAtEnd] == NO) {
NSString *str;
// We can have unnamed paramenters, :::
if ([scanner scanUpToString:@":" intoString:&str]) {
//NSLog(@"str += '%@'", str);
[resultString appendString:str];
}
if ([scanner scanString:@":" intoString:NULL]) {
[resultString appendString:@":"];
if (index >= count) {
noMoreTypes = YES;
} else {
methodType = methodTypes[index];
specialCase = [self _specialCaseVariable:nil type:methodType.type.bareTypeString];
if (specialCase != nil) {
[resultString appendFormat:@"(%@)", specialCase];
} else {
NSString *formattedType = [methodType.type formattedString:nil formatter:self level:0];
//if ([[methodType type] isIDType] == NO)
[resultString appendFormat:@"(%@)", formattedType];
}
//[resultString appendFormat:@"fp%@", [methodType offset]];
[resultString appendFormat:@"arg%lu", index-2];
NSString *ch = [scanner peekCharacter];
// if next character is not ':' nor EOS then add space
if (ch != nil && [ch isEqual:@":"] == NO)
[resultString appendString:@" "];
index++;
}
}
}
if (noMoreTypes) {
[resultString appendString:@" /* Error: Ran out of types for this method. */"];
}
}
return resultString;
}
// Called from CDType, which gets a formatter but not a type controller.
- (CDType *)replacementForType:(CDType *)type;
{
return [self.typeController typeFormatter:self replacementForType:type];
}
// Called from CDType, which gets a formatter but not a type controller.
- (NSString *)typedefNameForStructure:(CDType *)structureType level:(NSUInteger)level;
{
return [self.typeController typeFormatter:self typedefNameForStructure:structureType level:level];
}
- (void)formattingDidReferenceClassName:(NSString *)name;
{
[self.typeController typeFormatter:self didReferenceClassName:name];
}
- (void)formattingDidReferenceProtocolNames:(NSArray *)names
{
[self.typeController typeFormatter:self didReferenceProtocolNames:names];
}
@end