OLD | NEW |
(Empty) | |
| 1 // -*- mode: ObjC -*- |
| 2 |
| 3 // This file is part of class-dump, a utility for examining the Objective-C seg
ment of Mach-O files. |
| 4 // Copyright (C) 1997-1998, 2000-2001, 2004-2010 Steve Nygard. |
| 5 |
| 6 #import "CDOCProtocol.h" |
| 7 |
| 8 #import "NSArray-Extensions.h" |
| 9 #import "NSError-CDExtensions.h" |
| 10 #import "CDClassDump.h" |
| 11 #import "CDOCMethod.h" |
| 12 #import "CDOCSymtab.h" |
| 13 #import "CDSymbolReferences.h" |
| 14 #import "CDTypeParser.h" |
| 15 #import "CDVisitor.h" |
| 16 #import "CDOCProperty.h" |
| 17 #import "CDMethodType.h" |
| 18 #import "CDType.h" |
| 19 #import "CDTypeController.h" |
| 20 #import "CDVisitorPropertyState.h" |
| 21 |
| 22 @implementation CDOCProtocol |
| 23 |
| 24 - (id)init; |
| 25 { |
| 26 if ([super init] == nil) |
| 27 return nil; |
| 28 |
| 29 name = nil; |
| 30 protocols = [[NSMutableArray alloc] init]; |
| 31 classMethods = [[NSMutableArray alloc] init]; |
| 32 instanceMethods = [[NSMutableArray alloc] init]; |
| 33 optionalClassMethods = [[NSMutableArray alloc] init]; |
| 34 optionalInstanceMethods = [[NSMutableArray alloc] init]; |
| 35 properties = [[NSMutableArray alloc] init]; |
| 36 |
| 37 adoptedProtocolNames = [[NSMutableSet alloc] init]; |
| 38 |
| 39 return self; |
| 40 } |
| 41 |
| 42 - (void)dealloc; |
| 43 { |
| 44 [name release]; |
| 45 [protocols release]; |
| 46 [classMethods release]; |
| 47 [instanceMethods release]; |
| 48 [optionalClassMethods release]; |
| 49 [optionalInstanceMethods release]; |
| 50 [properties release]; |
| 51 |
| 52 [adoptedProtocolNames release]; |
| 53 |
| 54 [super dealloc]; |
| 55 } |
| 56 |
| 57 - (NSString *)name; |
| 58 { |
| 59 return name; |
| 60 } |
| 61 |
| 62 - (void)setName:(NSString *)newName; |
| 63 { |
| 64 if (newName == name) |
| 65 return; |
| 66 |
| 67 [name release]; |
| 68 name = [newName retain]; |
| 69 } |
| 70 |
| 71 - (NSArray *)protocols; |
| 72 { |
| 73 return protocols; |
| 74 } |
| 75 |
| 76 // This assumes that the protocol name doesn't change after it's been added to t
his. |
| 77 - (void)addProtocol:(CDOCProtocol *)aProtocol; |
| 78 { |
| 79 if ([adoptedProtocolNames containsObject:[aProtocol name]] == NO) { |
| 80 [protocols addObject:aProtocol]; |
| 81 [adoptedProtocolNames addObject:[aProtocol name]]; |
| 82 } |
| 83 } |
| 84 |
| 85 - (void)removeProtocol:(CDOCProtocol *)aProtocol; |
| 86 { |
| 87 [adoptedProtocolNames removeObject:[aProtocol name]]; |
| 88 [protocols removeObject:aProtocol]; |
| 89 } |
| 90 |
| 91 - (NSArray *)classMethods; |
| 92 { |
| 93 return classMethods; |
| 94 } |
| 95 |
| 96 - (void)addClassMethod:(CDOCMethod *)method; |
| 97 { |
| 98 [classMethods addObject:method]; |
| 99 } |
| 100 |
| 101 - (NSArray *)instanceMethods; |
| 102 { |
| 103 return instanceMethods; |
| 104 } |
| 105 |
| 106 - (void)addInstanceMethod:(CDOCMethod *)method; |
| 107 { |
| 108 [instanceMethods addObject:method]; |
| 109 } |
| 110 |
| 111 - (NSArray *)optionalClassMethods; |
| 112 { |
| 113 return optionalClassMethods; |
| 114 } |
| 115 |
| 116 - (void)addOptionalClassMethod:(CDOCMethod *)method; |
| 117 { |
| 118 [optionalClassMethods addObject:method]; |
| 119 } |
| 120 |
| 121 - (NSArray *)optionalInstanceMethods; |
| 122 { |
| 123 return optionalInstanceMethods; |
| 124 } |
| 125 |
| 126 - (void)addOptionalInstanceMethod:(CDOCMethod *)method; |
| 127 { |
| 128 [optionalInstanceMethods addObject:method]; |
| 129 } |
| 130 |
| 131 - (NSArray *)properties; |
| 132 { |
| 133 return properties; |
| 134 } |
| 135 |
| 136 - (void)addProperty:(CDOCProperty *)property; |
| 137 { |
| 138 [properties addObject:property]; |
| 139 } |
| 140 |
| 141 - (BOOL)hasMethods; |
| 142 { |
| 143 return [classMethods count] > 0 || [instanceMethods count] > 0 || [optionalC
lassMethods count] > 0 || [optionalInstanceMethods count] > 0; |
| 144 } |
| 145 |
| 146 - (NSString *)description; |
| 147 { |
| 148 return [NSString stringWithFormat:@"<%@:%p> name: %@, protocols: %d, class m
ethods: %d, instance methods: %d", |
| 149 NSStringFromClass([self class]), self, name, [protocols cou
nt], [classMethods count], [instanceMethods count]]; |
| 150 } |
| 151 |
| 152 - (void)registerTypesWithObject:(CDTypeController *)typeController phase:(NSUInt
eger)phase; |
| 153 { |
| 154 [self registerTypesFromMethods:classMethods withObject:typeController phase:
phase]; |
| 155 [self registerTypesFromMethods:instanceMethods withObject:typeController pha
se:phase]; |
| 156 |
| 157 [self registerTypesFromMethods:optionalClassMethods withObject:typeControlle
r phase:phase]; |
| 158 [self registerTypesFromMethods:optionalInstanceMethods withObject:typeContro
ller phase:phase]; |
| 159 } |
| 160 |
| 161 - (void)registerTypesFromMethods:(NSArray *)methods withObject:(CDTypeController
*)typeController phase:(NSUInteger)phase; |
| 162 { |
| 163 for (CDOCMethod *method in methods) { |
| 164 for (CDMethodType *methodType in [method parsedMethodTypes]) { |
| 165 [[methodType type] phase:phase registerTypesWithObject:typeControlle
r usedInMethod:YES]; |
| 166 } |
| 167 } |
| 168 } |
| 169 |
| 170 - (NSString *)sortableName; |
| 171 { |
| 172 return name; |
| 173 } |
| 174 |
| 175 - (NSComparisonResult)ascendingCompareByName:(CDOCProtocol *)otherProtocol; |
| 176 { |
| 177 return [[self sortableName] compare:[otherProtocol sortableName]]; |
| 178 } |
| 179 |
| 180 - (NSString *)findTag:(CDSymbolReferences *)symbolReferences; |
| 181 { |
| 182 NSMutableString *resultString = [NSMutableString string]; |
| 183 |
| 184 [resultString appendFormat:@"@protocol %@", name]; |
| 185 if ([protocols count] > 0) |
| 186 [resultString appendFormat:@" <%@>", [[protocols arrayByMappingSelector:
@selector(name)] componentsJoinedByString:@", "]]; |
| 187 |
| 188 return resultString; |
| 189 } |
| 190 |
| 191 - (void)recursivelyVisit:(CDVisitor *)aVisitor; |
| 192 { |
| 193 CDVisitorPropertyState *propertyState; |
| 194 |
| 195 if ([[aVisitor classDump] shouldMatchRegex] && [[aVisitor classDump] regexMa
tchesString:[self name]] == NO) |
| 196 return; |
| 197 |
| 198 // Wonderful. Need to typecast because there's also -[NSHTTPCookie initWith
Properties:] that takes a dictionary. |
| 199 propertyState = [(CDVisitorPropertyState *)[CDVisitorPropertyState alloc] in
itWithProperties:[self properties]]; |
| 200 |
| 201 [aVisitor willVisitProtocol:self]; |
| 202 |
| 203 //[aVisitor willVisitPropertiesOfProtocol:self]; |
| 204 //[self visitProperties:aVisitor]; |
| 205 //[aVisitor didVisitPropertiesOfProtocol:self]; |
| 206 |
| 207 [self visitMethods:aVisitor propertyState:propertyState]; |
| 208 |
| 209 // This shouldn't happen for protocols. @optional properties will generate
optional instance methods, and we'll emit @property in the @optional section. |
| 210 // BTW, otool doesn't show optional methods. |
| 211 [aVisitor visitRemainingProperties:propertyState]; |
| 212 |
| 213 [aVisitor didVisitProtocol:self]; |
| 214 |
| 215 [propertyState release]; |
| 216 } |
| 217 |
| 218 - (void)visitMethods:(CDVisitor *)aVisitor propertyState:(CDVisitorPropertyState
*)propertyState; |
| 219 { |
| 220 NSArray *methods; |
| 221 |
| 222 methods = classMethods; |
| 223 if ([[aVisitor classDump] shouldSortMethods]) |
| 224 methods = [methods sortedArrayUsingSelector:@selector(ascendingCompareBy
Name:)]; |
| 225 for (CDOCMethod *method in methods) |
| 226 [aVisitor visitClassMethod:method]; |
| 227 |
| 228 methods = instanceMethods; |
| 229 if ([[aVisitor classDump] shouldSortMethods]) |
| 230 methods = [methods sortedArrayUsingSelector:@selector(ascendingCompareBy
Name:)]; |
| 231 for (CDOCMethod *method in methods) |
| 232 [aVisitor visitInstanceMethod:method propertyState:propertyState]; |
| 233 |
| 234 if ([optionalClassMethods count] > 0 || [optionalInstanceMethods count] > 0)
{ |
| 235 [aVisitor willVisitOptionalMethods]; |
| 236 |
| 237 methods = optionalClassMethods; |
| 238 if ([[aVisitor classDump] shouldSortMethods]) |
| 239 methods = [methods sortedArrayUsingSelector:@selector(ascendingCompa
reByName:)]; |
| 240 for (CDOCMethod *method in methods) |
| 241 [aVisitor visitClassMethod:method]; |
| 242 |
| 243 methods = optionalInstanceMethods; |
| 244 if ([[aVisitor classDump] shouldSortMethods]) |
| 245 methods = [methods sortedArrayUsingSelector:@selector(ascendingCompa
reByName:)]; |
| 246 for (CDOCMethod *method in methods) |
| 247 [aVisitor visitInstanceMethod:method propertyState:propertyState]; |
| 248 |
| 249 [aVisitor didVisitOptionalMethods]; |
| 250 } |
| 251 } |
| 252 |
| 253 - (void)visitProperties:(CDVisitor *)aVisitor; |
| 254 { |
| 255 NSArray *array; |
| 256 |
| 257 array = properties; |
| 258 if ([[aVisitor classDump] shouldSortMethods]) |
| 259 array = [array sortedArrayUsingSelector:@selector(ascendingCompareByName
:)]; |
| 260 for (CDOCProperty *property in array) |
| 261 [aVisitor visitProperty:property]; |
| 262 } |
| 263 |
| 264 @end |
OLD | NEW |