forked from nygard/class-dump
/
CDTypeParser.m
556 lines (454 loc) · 16 KB
/
CDTypeParser.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
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
// -*- 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-2011 Steve Nygard.
#import "CDTypeParser.h"
#include <assert.h>
#import "CDMethodType.h"
#import "CDType.h"
#import "CDTypeName.h"
#import "CDTypeLexer.h"
#import "NSString-Extensions.h"
NSString *CDSyntaxError = @"Syntax Error";
NSString *CDTypeParserErrorDomain = @"CDTypeParserErrorDomain";
static BOOL debug = NO;
static NSString *CDTokenDescription(int token)
{
if (token < 128)
return [NSString stringWithFormat:@"%d(%c)", token, token];
return [NSString stringWithFormat:@"%d", token];
}
@implementation CDTypeParser
- (id)initWithType:(NSString *)aType;
{
NSMutableString *str;
if ((self = [super init])) {
// Do some preprocessing first: Replace "<unnamed>::" with just "unnamed::".
str = [aType mutableCopy];
[str replaceOccurrencesOfString:@"<unnamed>::" withString:@"unnamed::" options:0 range:NSMakeRange(0, [aType length])];
lexer = [[CDTypeLexer alloc] initWithString:str];
lookahead = 0;
[str release];
}
return self;
}
- (void)dealloc;
{
[lexer release];
[super dealloc];
}
- (CDTypeLexer *)lexer;
{
return lexer;
}
- (NSArray *)parseMethodType:(NSError **)error;
{
NSArray *result;
*error = nil;
@try {
lookahead = [lexer scanNextToken];
result = [self _parseMethodType];
}
@catch (NSException *exception) {
NSDictionary *userInfo;
int code;
// Obviously I need to figure out a sane method of dealing with errors here. This is not.
if ([[exception name] isEqual:CDSyntaxError]) {
code = CDTypeParserCode_SyntaxError;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:@"Syntax Error", @"reason",
[NSString stringWithFormat:@"Syntax Error, %@:\n\t type: %@\n\tremaining: %@",
[exception reason], [lexer string], [lexer remainingString]], @"explanation",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
} else {
code = CDTypeParserCode_Default;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:[exception reason], @"reason",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
}
*error = [NSError errorWithDomain:CDTypeParserErrorDomain code:code userInfo:userInfo];
[userInfo release];
result = nil;
}
return result;
}
- (CDType *)parseType:(NSError **)error;
{
CDType *result;
*error = nil;
@try {
lookahead = [lexer scanNextToken];
result = [self _parseType];
}
@catch (NSException *exception) {
NSDictionary *userInfo;
int code;
// Obviously I need to figure out a sane method of dealing with errors here. This is not.
if ([[exception name] isEqual:CDSyntaxError]) {
code = CDTypeParserCode_SyntaxError;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:@"Syntax Error", @"reason",
[NSString stringWithFormat:@"%@:\n\t type: %@\n\tremaining: %@",
[exception reason], [lexer string], [lexer remainingString]], @"explanation",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
} else {
code = CDTypeParserCode_Default;
userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:[exception reason], @"reason",
[lexer string], @"type",
[lexer remainingString], @"remaining string",
nil];
}
*error = [NSError errorWithDomain:CDTypeParserErrorDomain code:code userInfo:userInfo];
[userInfo release];
result = nil;
}
return result;
}
@end
@implementation CDTypeParser (Private)
- (void)match:(int)token;
{
[self match:token enterState:[lexer state]];
}
- (void)match:(int)token enterState:(CDTypeLexerState)newState;
{
if (lookahead == token) {
if (debug) NSLog(@"matched %@", CDTokenDescription(token));
[lexer setState:newState];
lookahead = [lexer scanNextToken];
} else {
[NSException raise:CDSyntaxError format:@"expected token %@, got %@",
CDTokenDescription(token),
CDTokenDescription(lookahead)];
}
}
- (void)error:(NSString *)errorString;
{
[NSException raise:CDSyntaxError format:@"%@", errorString];
}
- (NSArray *)_parseMethodType;
{
NSMutableArray *methodTypes;
CDMethodType *aMethodType;
CDType *type;
NSString *number;
methodTypes = [NSMutableArray array];
// Has to have at least one pair for the return type;
// Probably needs at least two more, for object and selector
// So it must be <type><number><type><number><type><number>. Three pairs at a minimum.
do {
type = [self _parseType];
number = [self parseNumber];
aMethodType = [[CDMethodType alloc] initWithType:type offset:number];
[methodTypes addObject:aMethodType];
[aMethodType release];
} while ([self isTokenInTypeStartSet:lookahead]);
return methodTypes;
}
// Plain object types can be:
// @ - plain id type
// @"NSObject" - NSObject *
// @"<MyProtocol>" - id <MyProtocol>
// But these can also be part of a structure, with the field name in quotes before the type:
// "foo"i"bar"i - int foo, int bar
// "foo"@"bar"i - id foo, int bar
// "foo"@"Foo""bar"i - Foo *foo, int bar
// So this is where we need to be careful.
//
// I'm going to make a simplifying assumption: Either the structure/union has member names,
// or is doesn't, it can't have some names and be missing others.
// The two key tests are:
// {my_struct3="field1"@"field2"i}
// {my_struct4="field1"@"NSObject""field2"i}
- (CDType *)_parseType;
{
return [self _parseTypeInStruct:NO];
}
- (CDType *)_parseTypeInStruct:(BOOL)isInStruct;
{
CDType *result;
if (lookahead == 'r'
|| lookahead == 'n'
|| lookahead == 'N'
|| lookahead == 'o'
|| lookahead == 'O'
|| lookahead == 'R'
|| lookahead == 'V') { // modifiers
int modifier;
CDType *unmodifiedType;
modifier = lookahead;
[self match:modifier];
if ([self isTokenInTypeStartSet:lookahead])
unmodifiedType = [self _parseTypeInStruct:isInStruct];
else
unmodifiedType = nil;
result = [[CDType alloc] initModifier:modifier type:unmodifiedType];
} else if (lookahead == '^') { // pointer
CDType *type;
[self match:'^'];
if (lookahead == TK_QUOTED_STRING || lookahead == '}' || lookahead == ')') {
type = [[CDType alloc] initSimpleType:'v'];
// Safari on 10.5 has: "m_function"{?="__pfn"^"__delta"i}
result = [[CDType alloc] initPointerType:type];
[type release];
} else {
type = [self _parseTypeInStruct:isInStruct];
result = [[CDType alloc] initPointerType:type];
}
} else if (lookahead == 'b') { // bitfield
NSString *number;
[self match:'b'];
number = [self parseNumber];
result = [[CDType alloc] initBitfieldType:number];
} else if (lookahead == '@') { // id
[self match:'@'];
#if 0
if (lookahead == TK_QUOTED_STRING) {
NSLog(@"%s, quoted string ahead, shouldCheckFieldNames: %d, end: %d",
__cmd, shouldCheckFieldNames, [[lexer scanner] isAtEnd]);
if ([[lexer scanner] isAtEnd] == NO)
NSLog(@"next character: %d (%c), isInTypeStartSet: %d", [lexer peekChar], [lexer peekChar], [self isTokenInTypeStartSet:[lexer peekChar]]);
}
#endif
if (lookahead == TK_QUOTED_STRING && (isInStruct == NO || [[lexer lexText] isFirstLetterUppercase] || [self isTokenInTypeStartSet:[lexer peekChar]] == NO)) {
NSString *str;
CDTypeName *typeName;
str = [lexer lexText];
if ([str hasPrefix:@"<"] && [str hasSuffix:@">"]) {
str = [str substringWithRange:NSMakeRange(1, [str length] - 2)];
result = [[CDType alloc] initIDTypeWithProtocols:[str componentsSeparatedByString:@","]];
} else {
typeName = [[CDTypeName alloc] init];
[typeName setName:str];
result = [[CDType alloc] initIDType:typeName];
[typeName release];
}
[self match:TK_QUOTED_STRING];
} else {
result = [[CDType alloc] initIDType:nil];
}
} else if (lookahead == '{') { // structure
CDTypeName *typeName;
NSArray *optionalMembers;
CDTypeLexerState savedState;
savedState = [lexer state];
[self match:'{' enterState:CDTypeLexerState_Identifier];
typeName = [self parseTypeName];
optionalMembers = [self parseOptionalMembers];
[self match:'}' enterState:savedState];
result = [[CDType alloc] initStructType:typeName members:optionalMembers];
} else if (lookahead == '(') { // union
CDTypeLexerState savedState;
savedState = [lexer state];
[self match:'(' enterState:CDTypeLexerState_Identifier];
if (lookahead == TK_IDENTIFIER) {
CDTypeName *typeName;
NSArray *optionalMembers;
typeName = [self parseTypeName];
optionalMembers = [self parseOptionalMembers];
[self match:')' enterState:savedState];
result = [[CDType alloc] initUnionType:typeName members:optionalMembers];
} else {
NSArray *unionTypes;
unionTypes = [self parseUnionTypes];
[self match:')' enterState:savedState];
result = [[CDType alloc] initUnionType:nil members:unionTypes];
}
} else if (lookahead == '[') { // array
NSString *number;
CDType *type;
[self match:'['];
number = [self parseNumber];
type = [self _parseType];
[self match:']'];
result = [[CDType alloc] initArrayType:type count:number];
} else if ([self isTokenInSimpleTypeSet:lookahead]) { // simple type
int simpleType;
simpleType = lookahead;
[self match:simpleType];
result = [[CDType alloc] initSimpleType:simpleType];
} else {
result = nil;
[NSException raise:CDSyntaxError format:@"expected (many things), got %@", CDTokenDescription(lookahead)];
}
return [result autorelease];
}
// This seems to be used in method types -- no names
- (NSArray *)parseUnionTypes;
{
NSMutableArray *members;
members = [NSMutableArray array];
while ([self isTokenInTypeSet:lookahead]) {
CDType *aType;
aType = [self _parseType];
//[aType setVariableName:@"___"];
[members addObject:aType];
}
return members;
}
- (NSArray *)parseOptionalMembers;
{
NSArray *result;
if (lookahead == '=') {
[self match:'='];
result = [self parseMemberList];
} else
result = nil;
return result;
}
- (NSArray *)parseMemberList;
{
NSMutableArray *result;
//NSLog(@" > %s", __cmd);
result = [NSMutableArray array];
while (lookahead == TK_QUOTED_STRING || [self isTokenInTypeSet:lookahead])
[result addObject:[self parseMember]];
//NSLog(@"< %s", __cmd);
return result;
}
- (CDType *)parseMember;
{
CDType *result;
//NSLog(@" > %s", __cmd);
if (lookahead == TK_QUOTED_STRING) {
NSString *identifier = nil;
while (lookahead == TK_QUOTED_STRING) {
if (identifier == nil)
identifier = [lexer lexText];
else {
// TextMate 1.5.4 has structures like... "storage""stack"{etc} -- two quoted strings next to each other.
identifier = [NSString stringWithFormat:@"%@__%@", identifier, [lexer lexText]];
}
[self match:TK_QUOTED_STRING];
}
//NSLog(@"got identifier: %@", identifier);
result = [self _parseTypeInStruct:YES];
[result setVariableName:identifier];
//NSLog(@"And parsed struct type.");
} else {
result = [self _parseTypeInStruct:YES];
}
//NSLog(@"< %s", __cmd);
return result;
}
- (CDTypeName *)parseTypeName;
{
CDTypeName *typeName;
typeName = [[[CDTypeName alloc] init] autorelease];
[typeName setName:[self parseIdentifier]];
if (lookahead == '<') {
CDTypeLexerState savedState;
savedState = [lexer state];
[self match:'<' enterState:CDTypeLexerState_TemplateTypes];
[typeName addTemplateType:[self parseTypeName]];
while (lookahead == ',') {
[self match:','];
[typeName addTemplateType:[self parseTypeName]];
}
[self match:'>' enterState:savedState];
if ([lexer state] == CDTypeLexerState_TemplateTypes) {
if (lookahead == TK_IDENTIFIER) {
NSString *suffix = [lexer lexText];
[self match:TK_IDENTIFIER];
[typeName setSuffix:suffix];
}
}
}
#if 0
// This breaks a bunch of the unit tests... need to figure out what's up with that first.
// We'll treat "?" as no name, returning nil here instead of testing the type name for this later.
if ([[typeName name] isEqualToString:@"?"] && [typeName isTemplateType] == NO)
typeName = nil;
#endif
return typeName;
}
- (NSString *)parseIdentifier;
{
NSString *result = nil;
if (lookahead == TK_IDENTIFIER) {
result = [lexer lexText];
[self match:TK_IDENTIFIER];
}
return result;
}
- (NSString *)parseNumber;
{
if (lookahead == TK_NUMBER) {
NSString *result;
result = [lexer lexText];
[self match:TK_NUMBER];
return result;
}
return nil;
}
- (BOOL)isTokenInModifierSet:(int)aToken;
{
if (aToken == 'r'
|| aToken == 'n'
|| aToken == 'N'
|| aToken == 'o'
|| aToken == 'O'
|| aToken == 'R'
|| aToken == 'V')
return YES;
return NO;
}
- (BOOL)isTokenInSimpleTypeSet:(int)aToken;
{
if (aToken == 'c'
|| aToken == 'i'
|| aToken == 's'
|| aToken == 'l'
|| aToken == 'q'
|| aToken == 'C'
|| aToken == 'I'
|| aToken == 'S'
|| aToken == 'L'
|| aToken == 'Q'
|| aToken == 'f'
|| aToken == 'd'
|| aToken == 'B'
|| aToken == 'v'
|| aToken == '*'
|| aToken == '#'
|| aToken == ':'
|| aToken == '%'
|| aToken == '?')
return YES;
return NO;
}
- (BOOL)isTokenInTypeSet:(int)aToken;
{
if ([self isTokenInModifierSet:aToken]
|| [self isTokenInSimpleTypeSet:aToken]
|| aToken == '^'
|| aToken == 'b'
|| aToken == '@'
|| aToken == '{'
|| aToken == '('
|| aToken == '[')
return YES;
return NO;
}
- (BOOL)isTokenInTypeStartSet:(int)aToken;
{
if (aToken == 'r'
|| aToken == 'n'
|| aToken == 'N'
|| aToken == 'o'
|| aToken == 'O'
|| aToken == 'R'
|| aToken == 'V'
|| aToken == '^'
|| aToken == 'b'
|| aToken == '@'
|| aToken == '{'
|| aToken == '('
|| aToken == '['
|| [self isTokenInSimpleTypeSet:aToken])
return YES;
return NO;
}
@end