Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 1 | //===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Objective-C related Decl classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclObjC.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | using namespace clang; |
| 17 | |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | // ObjC Decl Allocation/Deallocation Method Implementations |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 22 | ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, |
| 23 | SourceLocation beginLoc, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 24 | SourceLocation endLoc, |
| 25 | Selector SelInfo, QualType T, |
| 26 | Decl *contextDecl, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 27 | AttributeList *M, bool isInstance, |
| 28 | bool isVariadic, |
Chris Lattner | f735583 | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 29 | ImplementationControl impControl) { |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 30 | void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 31 | return new (Mem) ObjCMethodDecl(beginLoc, endLoc, |
| 32 | SelInfo, T, contextDecl, |
Chris Lattner | f735583 | 2008-03-16 00:58:16 +0000 | [diff] [blame] | 33 | M, isInstance, |
| 34 | isVariadic, impControl); |
Chris Lattner | 0db541b | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 35 | } |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 36 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 37 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 38 | SourceLocation atLoc, |
Chris Lattner | 0db541b | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 39 | unsigned numRefProtos, |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 40 | IdentifierInfo *Id, |
| 41 | SourceLocation ClassLoc, |
Chris Lattner | 0db541b | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 42 | bool ForwardDecl, bool isInternal){ |
| 43 | void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 44 | return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos, |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 45 | Id, ClassLoc, ForwardDecl, |
Chris Lattner | 0db541b | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 46 | isInternal); |
| 47 | } |
| 48 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 49 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 0db541b | 2008-03-16 01:15:50 +0000 | [diff] [blame] | 50 | IdentifierInfo *Id, QualType T) { |
| 51 | void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>(); |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 52 | return new (Mem) ObjCIvarDecl(L, Id, T); |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 55 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, |
| 56 | SourceLocation L, |
Chris Lattner | 180f7e2 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 57 | unsigned numRefProtos, |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 58 | IdentifierInfo *Id) { |
Chris Lattner | 180f7e2 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 59 | void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>(); |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 60 | return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id); |
Chris Lattner | 180f7e2 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 63 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, |
| 64 | SourceLocation L, |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 65 | ObjCInterfaceDecl **Elts, unsigned nElts) { |
| 66 | void *Mem = C.getAllocator().Allocate<ObjCClassDecl>(); |
| 67 | return new (Mem) ObjCClassDecl(L, Elts, nElts); |
| 68 | } |
| 69 | |
| 70 | ObjCForwardProtocolDecl * |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 71 | ObjCForwardProtocolDecl::Create(ASTContext &C, |
| 72 | SourceLocation L, |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 73 | ObjCProtocolDecl **Elts, unsigned NumElts) { |
| 74 | void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>(); |
| 75 | return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts); |
| 76 | } |
| 77 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 78 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, |
| 79 | SourceLocation L, |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 80 | IdentifierInfo *Id) { |
| 81 | void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>(); |
Chris Lattner | 321b5d1 | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 82 | return new (Mem) ObjCCategoryDecl(L, Id); |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 85 | ObjCCategoryImplDecl * |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 86 | ObjCCategoryImplDecl::Create(ASTContext &C, |
| 87 | SourceLocation L,IdentifierInfo *Id, |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 88 | ObjCInterfaceDecl *ClassInterface) { |
| 89 | void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>(); |
| 90 | return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface); |
| 91 | } |
| 92 | |
| 93 | ObjCImplementationDecl * |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 94 | ObjCImplementationDecl::Create(ASTContext &C, |
| 95 | SourceLocation L, |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 96 | IdentifierInfo *Id, |
| 97 | ObjCInterfaceDecl *ClassInterface, |
| 98 | ObjCInterfaceDecl *SuperDecl) { |
| 99 | void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>(); |
| 100 | return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl); |
| 101 | } |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 103 | ObjCCompatibleAliasDecl * |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 104 | ObjCCompatibleAliasDecl::Create(ASTContext &C, |
| 105 | SourceLocation L, |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 106 | IdentifierInfo *Id, |
| 107 | ObjCInterfaceDecl* AliasedClass) { |
| 108 | void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>(); |
| 109 | return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass); |
| 110 | } |
| 111 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 112 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 113 | SourceLocation L, |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 114 | IdentifierInfo *Id, |
Fariborz Jahanian | e707172 | 2008-04-11 23:40:25 +0000 | [diff] [blame] | 115 | QualType T) { |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 116 | void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>(); |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 117 | return new (Mem) ObjCPropertyDecl(L, Id, T); |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 120 | //===----------------------------------------------------------------------===// |
| 121 | // Objective-C Decl Implementation |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
| 124 | void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 125 | unsigned NumParams) { |
| 126 | assert(ParamInfo == 0 && "Already has param info!"); |
| 127 | |
| 128 | // Zero params -> null pointer. |
| 129 | if (NumParams) { |
| 130 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 131 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 132 | NumMethodParams = NumParams; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | ObjCMethodDecl::~ObjCMethodDecl() { |
| 137 | delete[] ParamInfo; |
| 138 | } |
| 139 | |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 140 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 141 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 142 | /// |
| 143 | ObjCPropertyDecl * |
| 144 | ObjCInterfaceDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 145 | for (ObjCInterfaceDecl::classprop_iterator I = classprop_begin(), |
| 146 | E = classprop_end(); I != E; ++I) { |
| 147 | ObjCPropertyDecl *property = *I; |
| 148 | if (property->getIdentifier() == PropertyId) |
| 149 | return property; |
| 150 | } |
Fariborz Jahanian | 8acf335 | 2008-04-21 23:57:08 +0000 | [diff] [blame] | 151 | if (getSuperClass()) |
| 152 | return getSuperClass()->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 157 | /// categories for this class and returns it. Name of the category is passed |
| 158 | /// in 'CategoryId'. If category not found, return 0; |
| 159 | /// |
| 160 | ObjCCategoryDecl * |
| 161 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
| 162 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 163 | Category; Category = Category->getNextClassCategory()) |
| 164 | if (Category->getIdentifier() == CategoryId) |
| 165 | return Category; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /// FindIvarDeclaration - Find an Ivar declaration in this class given its |
| 170 | /// name in 'IvarId'. On failure to find, return 0; |
| 171 | /// |
| 172 | ObjCIvarDecl * |
| 173 | ObjCInterfaceDecl::FindIvarDeclaration(IdentifierInfo *IvarId) const { |
| 174 | for (ObjCInterfaceDecl::ivar_iterator IVI = ivar_begin(), |
| 175 | IVE = ivar_end(); IVI != IVE; ++IVI) { |
| 176 | ObjCIvarDecl* Ivar = (*IVI); |
| 177 | if (Ivar->getIdentifier() == IvarId) |
| 178 | return Ivar; |
| 179 | } |
Fariborz Jahanian | 8acf335 | 2008-04-21 23:57:08 +0000 | [diff] [blame] | 180 | if (getSuperClass()) |
| 181 | return getSuperClass()->FindIvarDeclaration(IvarId); |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 185 | /// ObjCAddInstanceVariablesToClass - Inserts instance variables |
| 186 | /// into ObjCInterfaceDecl's fields. |
| 187 | /// |
| 188 | void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars, |
| 189 | unsigned numIvars, |
| 190 | SourceLocation RBrac) { |
| 191 | NumIvars = numIvars; |
| 192 | if (numIvars) { |
| 193 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 194 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 195 | } |
| 196 | setLocEnd(RBrac); |
| 197 | } |
| 198 | |
| 199 | /// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 200 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 201 | /// Ivars into ObjCImplementationDecl's fields. |
| 202 | /// |
| 203 | void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl( |
| 204 | ObjCIvarDecl **ivars, unsigned numIvars) { |
| 205 | NumIvars = numIvars; |
| 206 | if (numIvars) { |
| 207 | Ivars = new ObjCIvarDecl*[numIvars]; |
| 208 | memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*)); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// addMethods - Insert instance and methods declarations into |
| 213 | /// ObjCInterfaceDecl's InsMethods and ClsMethods fields. |
| 214 | /// |
| 215 | void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods, |
| 216 | unsigned numInsMembers, |
| 217 | ObjCMethodDecl **clsMethods, |
| 218 | unsigned numClsMembers, |
| 219 | SourceLocation endLoc) { |
| 220 | NumInstanceMethods = numInsMembers; |
| 221 | if (numInsMembers) { |
| 222 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 223 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 224 | } |
| 225 | NumClassMethods = numClsMembers; |
| 226 | if (numClsMembers) { |
| 227 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 228 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 229 | } |
| 230 | AtEndLoc = endLoc; |
| 231 | } |
| 232 | |
Fariborz Jahanian | 52ff844 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 233 | /// addProperties - Insert property declaration AST nodes into |
| 234 | /// ObjCInterfaceDecl's PropertyDecl field. |
Chris Lattner | cffe366 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 235 | /// |
| 236 | void ObjCInterfaceDecl::addProperties(ObjCPropertyDecl **Properties, |
| 237 | unsigned NumProperties) { |
| 238 | if (NumProperties == 0) return; |
| 239 | |
| 240 | NumPropertyDecl = NumProperties; |
| 241 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 242 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 243 | } |
| 244 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 245 | /// mergeProperties - Adds properties to the end of list of current properties |
| 246 | /// for this class. |
| 247 | |
| 248 | void ObjCInterfaceDecl::mergeProperties(ObjCPropertyDecl **Properties, |
| 249 | unsigned NumNewProperties) { |
| 250 | if (NumNewProperties == 0) return; |
| 251 | |
| 252 | if (PropertyDecl) { |
| 253 | ObjCPropertyDecl **newPropertyDecl = |
| 254 | new ObjCPropertyDecl*[NumNewProperties + NumPropertyDecl]; |
| 255 | ObjCPropertyDecl **buf = newPropertyDecl; |
| 256 | // put back original properties in buffer. |
| 257 | memcpy(buf, PropertyDecl, NumPropertyDecl*sizeof(ObjCPropertyDecl*)); |
| 258 | // Add new properties to this buffer. |
| 259 | memcpy(buf+NumPropertyDecl, Properties, |
| 260 | NumNewProperties*sizeof(ObjCPropertyDecl*)); |
| 261 | free(PropertyDecl); |
| 262 | PropertyDecl = newPropertyDecl; |
| 263 | NumPropertyDecl += NumNewProperties; |
| 264 | } |
| 265 | else { |
| 266 | PropertyDecl = new ObjCPropertyDecl*[NumNewProperties]; |
| 267 | memcpy(PropertyDecl, Properties, NumNewProperties*sizeof(ObjCPropertyDecl*)); |
| 268 | NumPropertyDecl = NumNewProperties; |
| 269 | } |
| 270 | } |
| 271 | |
Fariborz Jahanian | 52ff844 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 272 | /// addProperties - Insert property declaration AST nodes into |
Fariborz Jahanian | 8516e9a | 2008-04-17 18:25:18 +0000 | [diff] [blame] | 273 | /// ObjCProtocolDecl's PropertyDecl field. |
| 274 | /// |
| 275 | void ObjCProtocolDecl::addProperties(ObjCPropertyDecl **Properties, |
| 276 | unsigned NumProperties) { |
| 277 | if (NumProperties == 0) return; |
| 278 | |
| 279 | NumPropertyDecl = NumProperties; |
| 280 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 281 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 282 | } |
| 283 | |
| 284 | /// addProperties - Insert property declaration AST nodes into |
Fariborz Jahanian | 5742a0f | 2008-04-16 21:11:25 +0000 | [diff] [blame] | 285 | /// ObjCCategoryDecl's PropertyDecl field. |
Fariborz Jahanian | 52ff844 | 2008-04-16 21:08:45 +0000 | [diff] [blame] | 286 | /// |
| 287 | void ObjCCategoryDecl::addProperties(ObjCPropertyDecl **Properties, |
| 288 | unsigned NumProperties) { |
| 289 | if (NumProperties == 0) return; |
| 290 | |
| 291 | NumPropertyDecl = NumProperties; |
| 292 | PropertyDecl = new ObjCPropertyDecl*[NumProperties]; |
| 293 | memcpy(PropertyDecl, Properties, NumProperties*sizeof(ObjCPropertyDecl*)); |
| 294 | } |
| 295 | |
Chris Lattner | cffe366 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 296 | /// addMethods - Insert instance and methods declarations into |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 297 | /// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 298 | /// |
| 299 | void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods, |
| 300 | unsigned numInsMembers, |
| 301 | ObjCMethodDecl **clsMethods, |
| 302 | unsigned numClsMembers, |
| 303 | SourceLocation endLoc) { |
| 304 | NumInstanceMethods = numInsMembers; |
| 305 | if (numInsMembers) { |
| 306 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 307 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 308 | } |
| 309 | NumClassMethods = numClsMembers; |
| 310 | if (numClsMembers) { |
| 311 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 312 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 313 | } |
| 314 | AtEndLoc = endLoc; |
| 315 | } |
| 316 | |
Chris Lattner | 321b5d1 | 2008-03-16 20:47:45 +0000 | [diff] [blame] | 317 | void ObjCCategoryDecl::setReferencedProtocolList(ObjCProtocolDecl **List, |
| 318 | unsigned NumRPs) { |
| 319 | assert(NumReferencedProtocols == 0 && "Protocol list already set"); |
| 320 | if (NumRPs == 0) return; |
| 321 | |
| 322 | ReferencedProtocols = new ObjCProtocolDecl*[NumRPs]; |
| 323 | memcpy(ReferencedProtocols, List, NumRPs*sizeof(ObjCProtocolDecl*)); |
| 324 | NumReferencedProtocols = NumRPs; |
| 325 | } |
| 326 | |
| 327 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 328 | /// addMethods - Insert instance and methods declarations into |
| 329 | /// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields. |
| 330 | /// |
| 331 | void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods, |
| 332 | unsigned numInsMembers, |
| 333 | ObjCMethodDecl **clsMethods, |
| 334 | unsigned numClsMembers, |
| 335 | SourceLocation endLoc) { |
| 336 | NumInstanceMethods = numInsMembers; |
| 337 | if (numInsMembers) { |
| 338 | InstanceMethods = new ObjCMethodDecl*[numInsMembers]; |
| 339 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*)); |
| 340 | } |
| 341 | NumClassMethods = numClsMembers; |
| 342 | if (numClsMembers) { |
| 343 | ClassMethods = new ObjCMethodDecl*[numClsMembers]; |
| 344 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*)); |
| 345 | } |
| 346 | AtEndLoc = endLoc; |
| 347 | } |
| 348 | |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 349 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 350 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
| 351 | /// |
| 352 | ObjCPropertyDecl * |
| 353 | ObjCCategoryDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
| 354 | for (ObjCCategoryDecl::classprop_iterator I = classprop_begin(), |
| 355 | E = classprop_end(); I != E; ++I) { |
| 356 | ObjCPropertyDecl *property = *I; |
| 357 | if (property->getIdentifier() == PropertyId) |
| 358 | return property; |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 363 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable( |
| 364 | IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) { |
| 365 | ObjCInterfaceDecl* ClassDecl = this; |
| 366 | while (ClassDecl != NULL) { |
| 367 | for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end(); |
| 368 | I != E; ++I) { |
| 369 | if ((*I)->getIdentifier() == ID) { |
| 370 | clsDeclared = ClassDecl; |
| 371 | return *I; |
| 372 | } |
| 373 | } |
| 374 | ClassDecl = ClassDecl->getSuperClass(); |
| 375 | } |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 380 | /// the class, its categories, and its super classes (using a linear search). |
| 381 | ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) { |
| 382 | ObjCInterfaceDecl* ClassDecl = this; |
| 383 | ObjCMethodDecl *MethodDecl = 0; |
| 384 | |
| 385 | while (ClassDecl != NULL) { |
| 386 | if ((MethodDecl = ClassDecl->getInstanceMethod(Sel))) |
| 387 | return MethodDecl; |
| 388 | |
| 389 | // Didn't find one yet - look through protocols. |
| 390 | ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 391 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 392 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 393 | if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel))) |
| 394 | return MethodDecl; |
| 395 | } |
| 396 | // Didn't find one yet - now look through categories. |
| 397 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 398 | while (CatDecl) { |
| 399 | if ((MethodDecl = CatDecl->getInstanceMethod(Sel))) |
| 400 | return MethodDecl; |
| 401 | CatDecl = CatDecl->getNextClassCategory(); |
| 402 | } |
| 403 | ClassDecl = ClassDecl->getSuperClass(); |
| 404 | } |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | // lookupClassMethod - This method returns a class method by looking in the |
| 409 | // class, its categories, and its super classes (using a linear search). |
| 410 | ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) { |
| 411 | ObjCInterfaceDecl* ClassDecl = this; |
| 412 | ObjCMethodDecl *MethodDecl = 0; |
| 413 | |
| 414 | while (ClassDecl != NULL) { |
| 415 | if ((MethodDecl = ClassDecl->getClassMethod(Sel))) |
| 416 | return MethodDecl; |
| 417 | |
| 418 | // Didn't find one yet - look through protocols. |
| 419 | ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 420 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 421 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 422 | if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel))) |
| 423 | return MethodDecl; |
| 424 | } |
| 425 | // Didn't find one yet - now look through categories. |
| 426 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 427 | while (CatDecl) { |
| 428 | if ((MethodDecl = CatDecl->getClassMethod(Sel))) |
| 429 | return MethodDecl; |
| 430 | CatDecl = CatDecl->getNextClassCategory(); |
| 431 | } |
| 432 | ClassDecl = ClassDecl->getSuperClass(); |
| 433 | } |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 438 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 439 | /// implementation. |
| 440 | ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) { |
| 441 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 442 | if ((*I)->getSelector() == Sel) |
| 443 | return *I; |
| 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | /// lookupClassMethod - This method returns a class method by looking in |
| 448 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 449 | /// implementation. |
| 450 | ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) { |
| 451 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 452 | I != E; ++I) |
| 453 | if ((*I)->getSelector() == Sel) |
| 454 | return *I; |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 459 | // the class implementation. Unlike interfaces, we don't look outside the |
| 460 | // implementation. |
| 461 | ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) { |
| 462 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 463 | if ((*I)->getSelector() == Sel) |
| 464 | return *I; |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | // lookupClassMethod - This method returns an instance method by looking in |
| 469 | // the class implementation. Unlike interfaces, we don't look outside the |
| 470 | // implementation. |
| 471 | ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) { |
| 472 | for (classmeth_iterator I = classmeth_begin(), E = classmeth_end(); |
| 473 | I != E; ++I) |
| 474 | if ((*I)->getSelector() == Sel) |
| 475 | return *I; |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 480 | // it inherited. |
| 481 | ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) { |
| 482 | ObjCMethodDecl *MethodDecl = NULL; |
| 483 | |
| 484 | if ((MethodDecl = getInstanceMethod(Sel))) |
| 485 | return MethodDecl; |
| 486 | |
| 487 | if (getNumReferencedProtocols() > 0) { |
| 488 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 489 | |
| 490 | for (unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 491 | if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel))) |
| 492 | return MethodDecl; |
| 493 | } |
| 494 | } |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 499 | // it inherited. |
| 500 | ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) { |
| 501 | ObjCMethodDecl *MethodDecl = NULL; |
| 502 | |
| 503 | if ((MethodDecl = getClassMethod(Sel))) |
| 504 | return MethodDecl; |
| 505 | |
| 506 | if (getNumReferencedProtocols() > 0) { |
| 507 | ObjCProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 508 | |
| 509 | for(unsigned i = 0; i < getNumReferencedProtocols(); i++) { |
| 510 | if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel))) |
| 511 | return MethodDecl; |
| 512 | } |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | /// getSynthesizedMethodSize - Compute size of synthesized method name |
| 518 | /// as done be the rewrite. |
| 519 | /// |
| 520 | unsigned ObjCMethodDecl::getSynthesizedMethodSize() const { |
| 521 | // syntesized method name is a concatenation of -/+[class-name selector] |
| 522 | // Get length of this name. |
| 523 | unsigned length = 3; // _I_ or _C_ |
| 524 | length += strlen(getClassInterface()->getName()) +1; // extra for _ |
| 525 | NamedDecl *MethodContext = getMethodContext(); |
| 526 | if (ObjCCategoryImplDecl *CID = |
| 527 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
| 528 | length += strlen(CID->getName()) +1; |
| 529 | length += getSelector().getName().size(); // selector name |
| 530 | return length; |
| 531 | } |
| 532 | |
Chris Lattner | 052fbcd | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 533 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 534 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext)) |
| 535 | return ID; |
| 536 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext)) |
| 537 | return CD->getClassInterface(); |
| 538 | if (ObjCImplementationDecl *IMD = |
Chris Lattner | 052fbcd | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 539 | dyn_cast<ObjCImplementationDecl>(MethodContext)) |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 540 | return IMD->getClassInterface(); |
Chris Lattner | 052fbcd | 2008-04-06 05:25:03 +0000 | [diff] [blame] | 541 | if (ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(MethodContext)) |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 542 | return CID->getClassInterface(); |
| 543 | assert(false && "unknown method context"); |
| 544 | return 0; |
| 545 | } |
Chris Lattner | 4485961 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 546 | |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 547 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
| 548 | SourceLocation atLoc, |
| 549 | SourceLocation L, |
| 550 | ObjCPropertyDecl *property, |
| 551 | PropertyImplKind kind, |
| 552 | ObjCIvarDecl *ivar) { |
| 553 | void *Mem = C.getAllocator().Allocate<ObjCPropertyImplDecl>(); |
| 554 | return new (Mem) ObjCPropertyImplDecl(atLoc, L, property, kind, ivar); |
| 555 | } |
Chris Lattner | 4485961 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 556 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 557 | |