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