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