Chris Lattner | 1e03a56 | 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" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/Stmt.h" |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 21 | // ObjCListBase |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 24 | void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) { |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 25 | List = 0; |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 26 | if (Elts == 0) return; // Setting to an empty list is a noop. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | |
| 28 | |
Chris Lattner | 4ee413b | 2009-02-20 21:44:01 +0000 | [diff] [blame] | 29 | List = new (Ctx) void*[Elts]; |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 30 | NumElts = Elts; |
| 31 | memcpy(List, InList, sizeof(void*)*Elts); |
| 32 | } |
| 33 | |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 34 | void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts, |
| 35 | const SourceLocation *Locs, ASTContext &Ctx) { |
| 36 | if (Elts == 0) |
| 37 | return; |
| 38 | |
| 39 | Locations = new (Ctx) SourceLocation[Elts]; |
| 40 | memcpy(Locations, Locs, sizeof(SourceLocation) * Elts); |
| 41 | set(InList, Elts, Ctx); |
| 42 | } |
| 43 | |
Chris Lattner | 11e1e1a | 2009-02-20 21:16:26 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 45 | // ObjCInterfaceDecl |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 48 | /// getIvarDecl - This method looks up an ivar in this ContextDecl. |
| 49 | /// |
| 50 | ObjCIvarDecl * |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 51 | ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 52 | lookup_const_iterator Ivar, IvarEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 53 | for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 54 | if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar)) |
| 55 | return ivar; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 60 | // Get the local instance/class method declared in this interface. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 61 | ObjCMethodDecl * |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 62 | ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const { |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 63 | // Since instance & class methods can have the same name, the loop below |
| 64 | // ensures we get the correct method. |
| 65 | // |
| 66 | // @interface Whatever |
| 67 | // - (int) class_method; |
| 68 | // + (float) class_method; |
| 69 | // @end |
| 70 | // |
| 71 | lookup_const_iterator Meth, MethEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 72 | for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) { |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 73 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth); |
Argyrios Kyrtzidis | 467c0b1 | 2009-07-25 22:15:22 +0000 | [diff] [blame] | 74 | if (MD && MD->isInstanceMethod() == isInstance) |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 75 | return MD; |
| 76 | } |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 80 | ObjCPropertyDecl * |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 81 | ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC, |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 82 | IdentifierInfo *propertyID) { |
| 83 | |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 84 | DeclContext::lookup_const_iterator I, E; |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 85 | llvm::tie(I, E) = DC->lookup(propertyID); |
| 86 | for ( ; I != E; ++I) |
| 87 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I)) |
| 88 | return PD; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 93 | /// FindPropertyDeclaration - Finds declaration of the property given its name |
| 94 | /// in 'PropertyId' and returns it. It returns 0, if not found. |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 95 | ObjCPropertyDecl * |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 96 | ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 98 | if (ObjCPropertyDecl *PD = |
| 99 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId)) |
| 100 | return PD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 102 | switch (getKind()) { |
| 103 | default: |
| 104 | break; |
| 105 | case Decl::ObjCProtocol: { |
| 106 | const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this); |
| 107 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 108 | E = PID->protocol_end(); I != E; ++I) |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 109 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 110 | return P; |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | case Decl::ObjCInterface: { |
| 114 | const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this); |
| 115 | // Look through categories. |
| 116 | for (ObjCCategoryDecl *Cat = OID->getCategoryList(); |
| 117 | Cat; Cat = Cat->getNextClassCategory()) |
| 118 | if (!Cat->IsClassExtension()) |
| 119 | if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId)) |
| 120 | return P; |
| 121 | |
| 122 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 123 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 124 | I = OID->all_referenced_protocol_begin(), |
| 125 | E = OID->all_referenced_protocol_end(); I != E; ++I) |
Ted Kremenek | de09d0c | 2010-03-15 20:11:53 +0000 | [diff] [blame] | 126 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 127 | return P; |
| 128 | |
| 129 | // Finally, check the super class. |
| 130 | if (const ObjCInterfaceDecl *superClass = OID->getSuperClass()) |
| 131 | return superClass->FindPropertyDeclaration(PropertyId); |
| 132 | break; |
| 133 | } |
| 134 | case Decl::ObjCCategory: { |
| 135 | const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this); |
| 136 | // Look through protocols. |
| 137 | if (!OCD->IsClassExtension()) |
| 138 | for (ObjCCategoryDecl::protocol_iterator |
| 139 | I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I) |
| 140 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 141 | return P; |
| 142 | |
| 143 | break; |
Fariborz Jahanian | f034e9c | 2009-01-19 18:16:19 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
Steve Naroff | 3d2c22b | 2008-06-05 13:55:23 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 149 | /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property |
| 150 | /// with name 'PropertyId' in the primary class; including those in protocols |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 151 | /// (direct or indirect) used by the primary class. |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 152 | /// |
| 153 | ObjCPropertyDecl * |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 154 | ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass( |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 155 | IdentifierInfo *PropertyId) const { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 156 | if (ExternallyCompleted) |
| 157 | LoadExternalDefinition(); |
| 158 | |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 159 | if (ObjCPropertyDecl *PD = |
| 160 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId)) |
| 161 | return PD; |
| 162 | |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 163 | // Look through protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 164 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 165 | I = all_referenced_protocol_begin(), |
| 166 | E = all_referenced_protocol_end(); I != E; ++I) |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 167 | if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId)) |
| 168 | return P; |
Ted Kremenek | 37cafb0 | 2010-03-15 20:30:07 +0000 | [diff] [blame] | 169 | |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 173 | void ObjCInterfaceDecl::mergeClassExtensionProtocolList( |
| 174 | ObjCProtocolDecl *const* ExtList, unsigned ExtNum, |
| 175 | ASTContext &C) |
| 176 | { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 177 | if (ExternallyCompleted) |
| 178 | LoadExternalDefinition(); |
| 179 | |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 180 | if (AllReferencedProtocols.empty() && ReferencedProtocols.empty()) { |
| 181 | AllReferencedProtocols.set(ExtList, ExtNum, C); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 184 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 185 | // Check for duplicate protocol in class's protocol list. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 186 | // This is O(n*m). But it is extremely rare and number of protocols in |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 187 | // class or its extension are very few. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 188 | SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs; |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 189 | for (unsigned i = 0; i < ExtNum; i++) { |
| 190 | bool protocolExists = false; |
| 191 | ObjCProtocolDecl *ProtoInExtension = ExtList[i]; |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 192 | for (all_protocol_iterator |
| 193 | p = all_referenced_protocol_begin(), |
| 194 | e = all_referenced_protocol_end(); p != e; ++p) { |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 195 | ObjCProtocolDecl *Proto = (*p); |
| 196 | if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) { |
| 197 | protocolExists = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | // Do we want to warn on a protocol in extension class which |
| 202 | // already exist in the class? Probably not. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 203 | if (!protocolExists) |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 204 | ProtocolRefs.push_back(ProtoInExtension); |
| 205 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 206 | |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 207 | if (ProtocolRefs.empty()) |
| 208 | return; |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 209 | |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 210 | // Merge ProtocolRefs into class's protocol list; |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 211 | for (all_protocol_iterator p = all_referenced_protocol_begin(), |
| 212 | e = all_referenced_protocol_end(); p != e; ++p) { |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 213 | ProtocolRefs.push_back(*p); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 214 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 215 | |
| 216 | AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(), C); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 219 | /// getFirstClassExtension - Find first class extension of the given class. |
| 220 | ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const { |
| 221 | for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl; |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 222 | CDecl = CDecl->getNextClassCategory()) |
| 223 | if (CDecl->IsClassExtension()) |
| 224 | return CDecl; |
| 225 | return 0; |
| 226 | } |
| 227 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 228 | /// getNextClassCategory - Find next class extension in list of categories. |
| 229 | const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const { |
| 230 | for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl; |
| 231 | CDecl = CDecl->getNextClassCategory()) |
| 232 | if (CDecl->IsClassExtension()) |
| 233 | return CDecl; |
| 234 | return 0; |
| 235 | } |
| 236 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 237 | ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID, |
| 238 | ObjCInterfaceDecl *&clsDeclared) { |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 239 | ObjCInterfaceDecl* ClassDecl = this; |
| 240 | while (ClassDecl != NULL) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 241 | if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) { |
Fariborz Jahanian | 496b5a8 | 2009-06-05 18:16:35 +0000 | [diff] [blame] | 242 | clsDeclared = ClassDecl; |
| 243 | return I; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 244 | } |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 245 | for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension(); |
| 246 | CDecl; CDecl = CDecl->getNextClassExtension()) { |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 247 | if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) { |
| 248 | clsDeclared = ClassDecl; |
| 249 | return I; |
| 250 | } |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 251 | } |
Fariborz Jahanian | 0e5ad25 | 2010-02-23 01:26:30 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 253 | ClassDecl = ClassDecl->getSuperClass(); |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 258 | /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super |
| 259 | /// class whose name is passed as argument. If it is not one of the super classes |
| 260 | /// the it returns NULL. |
| 261 | ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass( |
| 262 | const IdentifierInfo*ICName) { |
| 263 | ObjCInterfaceDecl* ClassDecl = this; |
| 264 | while (ClassDecl != NULL) { |
| 265 | if (ClassDecl->getIdentifier() == ICName) |
| 266 | return ClassDecl; |
| 267 | ClassDecl = ClassDecl->getSuperClass(); |
| 268 | } |
| 269 | return NULL; |
| 270 | } |
| 271 | |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 272 | /// lookupMethod - This method returns an instance/class method by looking in |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 273 | /// the class, its categories, and its super classes (using a linear search). |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 274 | ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, |
| 275 | bool isInstance) const { |
| 276 | const ObjCInterfaceDecl* ClassDecl = this; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 277 | ObjCMethodDecl *MethodDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 279 | if (ExternallyCompleted) |
| 280 | LoadExternalDefinition(); |
| 281 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 282 | while (ClassDecl != NULL) { |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 283 | if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 284 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 286 | // Didn't find one yet - look through protocols. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 287 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 288 | ClassDecl->getReferencedProtocols(); |
| 289 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 290 | E = Protocols.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 291 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 292 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 294 | // Didn't find one yet - now look through categories. |
| 295 | ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
| 296 | while (CatDecl) { |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 297 | if ((MethodDecl = CatDecl->getMethod(Sel, isInstance))) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 298 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Steve Naroff | b79c01e | 2008-12-08 20:57:28 +0000 | [diff] [blame] | 300 | // Didn't find one yet - look through protocols. |
| 301 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 302 | CatDecl->getReferencedProtocols(); |
| 303 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 304 | E = Protocols.end(); I != E; ++I) |
Argyrios Kyrtzidis | aa5420c | 2009-07-25 22:15:51 +0000 | [diff] [blame] | 305 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Steve Naroff | b558422 | 2009-02-26 11:32:02 +0000 | [diff] [blame] | 306 | return MethodDecl; |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 307 | CatDecl = CatDecl->getNextClassCategory(); |
| 308 | } |
| 309 | ClassDecl = ClassDecl->getSuperClass(); |
| 310 | } |
| 311 | return NULL; |
| 312 | } |
| 313 | |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 314 | ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod( |
| 315 | const Selector &Sel, |
| 316 | bool Instance) { |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 317 | ObjCMethodDecl *Method = 0; |
| 318 | if (ObjCImplementationDecl *ImpDecl = getImplementation()) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 319 | Method = Instance ? ImpDecl->getInstanceMethod(Sel) |
| 320 | : ImpDecl->getClassMethod(Sel); |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 321 | |
| 322 | if (!Method && getSuperClass()) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 323 | return getSuperClass()->lookupPrivateMethod(Sel, Instance); |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 324 | return Method; |
| 325 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 326 | |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | // ObjCMethodDecl |
| 329 | //===----------------------------------------------------------------------===// |
| 330 | |
| 331 | ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | SourceLocation beginLoc, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 333 | SourceLocation endLoc, |
| 334 | Selector SelInfo, QualType T, |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 335 | TypeSourceInfo *ResultTInfo, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 336 | DeclContext *contextDecl, |
| 337 | bool isInstance, |
| 338 | bool isVariadic, |
| 339 | bool isSynthesized, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 340 | bool isImplicitlyDeclared, |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 341 | bool isDefined, |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 342 | ImplementationControl impControl, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 343 | bool HasRelatedResultType, |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 344 | unsigned numSelectorArgs) { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 345 | return new (C) ObjCMethodDecl(beginLoc, endLoc, |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 346 | SelInfo, T, ResultTInfo, contextDecl, |
| 347 | isInstance, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 348 | isVariadic, isSynthesized, isImplicitlyDeclared, |
| 349 | isDefined, |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 350 | impControl, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 351 | HasRelatedResultType, |
Fariborz Jahanian | 7732cc9 | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 352 | numSelectorArgs); |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 355 | /// \brief A definition will return its interface declaration. |
| 356 | /// An interface declaration will return its definition. |
| 357 | /// Otherwise it will return itself. |
| 358 | ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() { |
| 359 | ASTContext &Ctx = getASTContext(); |
| 360 | ObjCMethodDecl *Redecl = 0; |
| 361 | Decl *CtxD = cast<Decl>(getDeclContext()); |
| 362 | |
| 363 | if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) { |
| 364 | if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD)) |
| 365 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); |
| 366 | |
| 367 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) { |
| 368 | if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD)) |
| 369 | Redecl = ImplD->getMethod(getSelector(), isInstanceMethod()); |
| 370 | |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 371 | } else if (ObjCImplementationDecl *ImplD = |
| 372 | dyn_cast<ObjCImplementationDecl>(CtxD)) { |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 373 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 374 | Redecl = IFD->getMethod(getSelector(), isInstanceMethod()); |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 375 | |
| 376 | } else if (ObjCCategoryImplDecl *CImplD = |
| 377 | dyn_cast<ObjCCategoryImplDecl>(CtxD)) { |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 378 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 379 | Redecl = CatD->getMethod(getSelector(), isInstanceMethod()); |
Argyrios Kyrtzidis | 57ea6be | 2009-07-21 00:06:36 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return Redecl ? Redecl : this; |
| 383 | } |
| 384 | |
Argyrios Kyrtzidis | e7f9d30 | 2009-07-28 05:11:17 +0000 | [diff] [blame] | 385 | ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() { |
| 386 | Decl *CtxD = cast<Decl>(getDeclContext()); |
| 387 | |
| 388 | if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) { |
| 389 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 390 | if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(), |
| 391 | isInstanceMethod())) |
| 392 | return MD; |
| 393 | |
| 394 | } else if (ObjCCategoryImplDecl *CImplD = |
| 395 | dyn_cast<ObjCCategoryImplDecl>(CtxD)) { |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 396 | if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl()) |
Argyrios Kyrtzidis | e7f9d30 | 2009-07-28 05:11:17 +0000 | [diff] [blame] | 397 | if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(), |
| 398 | isInstanceMethod())) |
| 399 | return MD; |
| 400 | } |
| 401 | |
| 402 | return this; |
| 403 | } |
| 404 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 405 | ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const { |
| 406 | ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family); |
John McCall | d976c8e | 2011-03-02 21:01:41 +0000 | [diff] [blame] | 407 | if (family != static_cast<unsigned>(InvalidObjCMethodFamily)) |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 408 | return family; |
| 409 | |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 410 | // Check for an explicit attribute. |
| 411 | if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) { |
| 412 | // The unfortunate necessity of mapping between enums here is due |
| 413 | // to the attributes framework. |
| 414 | switch (attr->getFamily()) { |
| 415 | case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break; |
| 416 | case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break; |
| 417 | case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break; |
| 418 | case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break; |
| 419 | case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break; |
| 420 | case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break; |
| 421 | } |
| 422 | Family = static_cast<unsigned>(family); |
| 423 | return family; |
| 424 | } |
| 425 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 426 | family = getSelector().getMethodFamily(); |
| 427 | switch (family) { |
| 428 | case OMF_None: break; |
| 429 | |
| 430 | // init only has a conventional meaning for an instance method, and |
| 431 | // it has to return an object. |
| 432 | case OMF_init: |
| 433 | if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType()) |
| 434 | family = OMF_None; |
| 435 | break; |
| 436 | |
| 437 | // alloc/copy/new have a conventional meaning for both class and |
| 438 | // instance methods, but they require an object return. |
| 439 | case OMF_alloc: |
| 440 | case OMF_copy: |
| 441 | case OMF_mutableCopy: |
| 442 | case OMF_new: |
| 443 | if (!getResultType()->isObjCObjectPointerType()) |
| 444 | family = OMF_None; |
| 445 | break; |
| 446 | |
| 447 | // These selectors have a conventional meaning only for instance methods. |
| 448 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 449 | case OMF_finalize: |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 450 | case OMF_retain: |
| 451 | case OMF_release: |
| 452 | case OMF_autorelease: |
| 453 | case OMF_retainCount: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 454 | case OMF_self: |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 455 | if (!isInstanceMethod()) |
| 456 | family = OMF_None; |
| 457 | break; |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 458 | |
| 459 | case OMF_performSelector: |
| 460 | if (!isInstanceMethod() || |
| 461 | !getResultType()->isObjCIdType()) |
| 462 | family = OMF_None; |
| 463 | else { |
| 464 | unsigned noParams = param_size(); |
| 465 | if (noParams < 1 || noParams > 3) |
| 466 | family = OMF_None; |
| 467 | else { |
| 468 | ObjCMethodDecl::arg_type_iterator it = arg_type_begin(); |
| 469 | QualType ArgT = (*it); |
| 470 | if (!ArgT->isObjCSelType()) { |
| 471 | family = OMF_None; |
| 472 | break; |
| 473 | } |
| 474 | while (--noParams) { |
| 475 | it++; |
| 476 | ArgT = (*it); |
| 477 | if (!ArgT->isObjCIdType()) { |
| 478 | family = OMF_None; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | break; |
| 485 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Cache the result. |
| 489 | Family = static_cast<unsigned>(family); |
| 490 | return family; |
| 491 | } |
| 492 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | void ObjCMethodDecl::createImplicitParams(ASTContext &Context, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 494 | const ObjCInterfaceDecl *OID) { |
| 495 | QualType selfTy; |
| 496 | if (isInstanceMethod()) { |
| 497 | // There may be no interface context due to error in declaration |
| 498 | // of the interface (which has been reported). Recover gracefully. |
| 499 | if (OID) { |
Daniel Dunbar | 3b3a458 | 2009-04-22 04:34:53 +0000 | [diff] [blame] | 500 | selfTy = Context.getObjCInterfaceType(OID); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 501 | selfTy = Context.getObjCObjectPointerType(selfTy); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 502 | } else { |
| 503 | selfTy = Context.getObjCIdType(); |
| 504 | } |
| 505 | } else // we have a factory method. |
| 506 | selfTy = Context.getObjCClassType(); |
| 507 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 508 | bool selfIsPseudoStrong = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 509 | bool selfIsConsumed = false; |
| 510 | if (isInstanceMethod() && Context.getLangOptions().ObjCAutoRefCount) { |
| 511 | selfIsConsumed = hasAttr<NSConsumesSelfAttr>(); |
| 512 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 513 | // 'self' is always __strong. It's actually pseudo-strong except |
| 514 | // in init methods, though. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 515 | Qualifiers qs; |
| 516 | qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 517 | selfTy = Context.getQualifiedType(selfTy, qs); |
| 518 | |
| 519 | // In addition, 'self' is const unless this is an init method. |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 520 | if (getMethodFamily() != OMF_init) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 521 | selfTy = selfTy.withConst(); |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 522 | selfIsPseudoStrong = true; |
| 523 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | ImplicitParamDecl *self |
| 527 | = ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 528 | &Context.Idents.get("self"), selfTy); |
| 529 | setSelfDecl(self); |
| 530 | |
| 531 | if (selfIsConsumed) |
| 532 | self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context)); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 533 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 534 | if (selfIsPseudoStrong) |
| 535 | self->setARCPseudoStrong(true); |
| 536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(), |
| 538 | &Context.Idents.get("_cmd"), |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 539 | Context.getObjCSelType())); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 542 | ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() { |
| 543 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext())) |
| 544 | return ID; |
| 545 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext())) |
| 546 | return CD->getClassInterface(); |
Argyrios Kyrtzidis | a853037 | 2009-07-28 05:10:52 +0000 | [diff] [blame] | 547 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext())) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 548 | return IMD->getClassInterface(); |
Argyrios Kyrtzidis | a853037 | 2009-07-28 05:10:52 +0000 | [diff] [blame] | 549 | |
| 550 | assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method"); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 551 | assert(false && "unknown method context"); |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 555 | //===----------------------------------------------------------------------===// |
| 556 | // ObjCInterfaceDecl |
| 557 | //===----------------------------------------------------------------------===// |
| 558 | |
| 559 | ObjCInterfaceDecl *ObjCInterfaceDecl::Create(ASTContext &C, |
| 560 | DeclContext *DC, |
| 561 | SourceLocation atLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | IdentifierInfo *Id, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 563 | SourceLocation ClassLoc, |
| 564 | bool ForwardDecl, bool isInternal){ |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 565 | return new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, ForwardDecl, |
| 566 | isInternal); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | ObjCInterfaceDecl:: |
| 570 | ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id, |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 571 | SourceLocation CLoc, bool FD, bool isInternal) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 572 | : ObjCContainerDecl(ObjCInterface, DC, atLoc, Id), |
| 573 | TypeForDecl(0), SuperClass(0), |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 574 | CategoryList(0), IvarList(0), |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 575 | ForwardDecl(FD), InternalInterface(isInternal), ExternallyCompleted(false), |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 576 | ClassLoc(CLoc) { |
| 577 | } |
| 578 | |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 579 | void ObjCInterfaceDecl::LoadExternalDefinition() const { |
| 580 | assert(ExternallyCompleted && "Class is not externally completed"); |
| 581 | ExternallyCompleted = false; |
| 582 | getASTContext().getExternalSource()->CompleteType( |
| 583 | const_cast<ObjCInterfaceDecl *>(this)); |
| 584 | } |
| 585 | |
| 586 | void ObjCInterfaceDecl::setExternallyCompleted() { |
| 587 | assert(getASTContext().getExternalSource() && |
| 588 | "Class can't be externally completed without an external source"); |
| 589 | assert(!ForwardDecl && |
| 590 | "Forward declarations can't be externally completed"); |
| 591 | ExternallyCompleted = true; |
| 592 | } |
| 593 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 594 | ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 595 | if (ExternallyCompleted) |
| 596 | LoadExternalDefinition(); |
| 597 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 598 | return getASTContext().getObjCImplementation( |
| 599 | const_cast<ObjCInterfaceDecl*>(this)); |
| 600 | } |
| 601 | |
| 602 | void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) { |
| 603 | getASTContext().setObjCImplementation(this, ImplD); |
| 604 | } |
| 605 | |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 606 | /// all_declared_ivar_begin - return first ivar declared in this class, |
| 607 | /// its extensions and its implementation. Lazily build the list on first |
| 608 | /// access. |
| 609 | ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() { |
| 610 | if (IvarList) |
| 611 | return IvarList; |
| 612 | |
| 613 | ObjCIvarDecl *curIvar = 0; |
| 614 | if (!ivar_empty()) { |
| 615 | ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end(); |
| 616 | IvarList = (*I); ++I; |
| 617 | for (curIvar = IvarList; I != E; curIvar = *I, ++I) |
| 618 | curIvar->setNextIvar(*I); |
| 619 | } |
| 620 | |
| 621 | for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl; |
| 622 | CDecl = CDecl->getNextClassExtension()) { |
| 623 | if (!CDecl->ivar_empty()) { |
| 624 | ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(), |
| 625 | E = CDecl->ivar_end(); |
| 626 | if (!IvarList) { |
| 627 | IvarList = (*I); ++I; |
| 628 | curIvar = IvarList; |
| 629 | } |
| 630 | for ( ;I != E; curIvar = *I, ++I) |
| 631 | curIvar->setNextIvar(*I); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (ObjCImplementationDecl *ImplDecl = getImplementation()) { |
| 636 | if (!ImplDecl->ivar_empty()) { |
| 637 | ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(), |
| 638 | E = ImplDecl->ivar_end(); |
| 639 | if (!IvarList) { |
| 640 | IvarList = (*I); ++I; |
| 641 | curIvar = IvarList; |
| 642 | } |
| 643 | for ( ;I != E; curIvar = *I, ++I) |
| 644 | curIvar->setNextIvar(*I); |
| 645 | } |
| 646 | } |
| 647 | return IvarList; |
| 648 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 649 | |
| 650 | /// FindCategoryDeclaration - Finds category declaration in the list of |
| 651 | /// categories for this class and returns it. Name of the category is passed |
| 652 | /// in 'CategoryId'. If category not found, return 0; |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 653 | /// |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 654 | ObjCCategoryDecl * |
| 655 | ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const { |
Douglas Gregor | 26ac3f3 | 2010-12-01 23:49:52 +0000 | [diff] [blame] | 656 | if (ExternallyCompleted) |
| 657 | LoadExternalDefinition(); |
| 658 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 659 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 660 | Category; Category = Category->getNextClassCategory()) |
| 661 | if (Category->getIdentifier() == CategoryId) |
| 662 | return Category; |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 666 | ObjCMethodDecl * |
| 667 | ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const { |
| 668 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 669 | Category; Category = Category->getNextClassCategory()) |
| 670 | if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) |
| 671 | if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel)) |
| 672 | return MD; |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const { |
| 677 | for (ObjCCategoryDecl *Category = getCategoryList(); |
| 678 | Category; Category = Category->getNextClassCategory()) |
| 679 | if (ObjCCategoryImplDecl *Impl = Category->getImplementation()) |
| 680 | if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel)) |
| 681 | return MD; |
| 682 | return 0; |
| 683 | } |
| 684 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 685 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 686 | /// has been implemented in IDecl class, its super class or categories (if |
| 687 | /// lookupCategory is true). |
| 688 | bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 689 | bool lookupCategory, |
| 690 | bool RHSIsQualifiedID) { |
| 691 | ObjCInterfaceDecl *IDecl = this; |
| 692 | // 1st, look up the class. |
| 693 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 694 | IDecl->getReferencedProtocols(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 696 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 697 | E = Protocols.end(); PI != E; ++PI) { |
| 698 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 699 | return true; |
| 700 | // This is dubious and is added to be compatible with gcc. In gcc, it is |
| 701 | // also allowed assigning a protocol-qualified 'id' type to a LHS object |
| 702 | // when protocol in qualified LHS is in list of protocols in the rhs 'id' |
| 703 | // object. This IMO, should be a bug. |
| 704 | // FIXME: Treat this as an extension, and flag this as an error when GCC |
| 705 | // extensions are not enabled. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | if (RHSIsQualifiedID && |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 707 | getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto)) |
| 708 | return true; |
| 709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 711 | // 2nd, look up the category. |
| 712 | if (lookupCategory) |
| 713 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 714 | CDecl = CDecl->getNextClassCategory()) { |
| 715 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 716 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 717 | if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 718 | return true; |
| 719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 721 | // 3rd, look up the super class(s) |
| 722 | if (IDecl->getSuperClass()) |
| 723 | return |
| 724 | IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory, |
| 725 | RHSIsQualifiedID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 727 | return false; |
| 728 | } |
| 729 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 730 | //===----------------------------------------------------------------------===// |
| 731 | // ObjCIvarDecl |
| 732 | //===----------------------------------------------------------------------===// |
| 733 | |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 734 | ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 735 | SourceLocation StartLoc, |
| 736 | SourceLocation IdLoc, IdentifierInfo *Id, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 737 | QualType T, TypeSourceInfo *TInfo, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 738 | AccessControl ac, Expr *BW, |
| 739 | bool synthesized) { |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 740 | if (DC) { |
| 741 | // Ivar's can only appear in interfaces, implementations (via synthesized |
| 742 | // properties), and class extensions (via direct declaration, or synthesized |
| 743 | // properties). |
| 744 | // |
| 745 | // FIXME: This should really be asserting this: |
| 746 | // (isa<ObjCCategoryDecl>(DC) && |
| 747 | // cast<ObjCCategoryDecl>(DC)->IsClassExtension())) |
| 748 | // but unfortunately we sometimes place ivars into non-class extension |
| 749 | // categories on error. This breaks an AST invariant, and should not be |
| 750 | // fixed. |
| 751 | assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || |
| 752 | isa<ObjCCategoryDecl>(DC)) && |
| 753 | "Invalid ivar decl context!"); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 754 | // Once a new ivar is created in any of class/class-extension/implementation |
| 755 | // decl contexts, the previously built IvarList must be rebuilt. |
| 756 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC); |
| 757 | if (!ID) { |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 758 | if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 759 | ID = IM->getClassInterface(); |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 760 | if (BW) |
| 761 | IM->setHasSynthBitfield(true); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 762 | } else { |
Fariborz Jahanian | 000835d | 2010-08-23 18:51:39 +0000 | [diff] [blame] | 763 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); |
| 764 | ID = CD->getClassInterface(); |
| 765 | if (BW) |
| 766 | CD->setHasSynthBitfield(true); |
| 767 | } |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 768 | } |
| 769 | ID->setIvarList(0); |
Daniel Dunbar | a065492 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 772 | return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, |
| 773 | ac, BW, synthesized); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Daniel Dunbar | 27a961a | 2010-04-02 21:13:59 +0000 | [diff] [blame] | 776 | const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const { |
| 777 | const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext()); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 778 | |
Daniel Dunbar | 27a961a | 2010-04-02 21:13:59 +0000 | [diff] [blame] | 779 | switch (DC->getKind()) { |
| 780 | default: |
| 781 | case ObjCCategoryImpl: |
| 782 | case ObjCProtocol: |
| 783 | assert(0 && "invalid ivar container!"); |
| 784 | return 0; |
| 785 | |
| 786 | // Ivars can only appear in class extension categories. |
| 787 | case ObjCCategory: { |
| 788 | const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC); |
| 789 | assert(CD->IsClassExtension() && "invalid container for ivar!"); |
| 790 | return CD->getClassInterface(); |
| 791 | } |
| 792 | |
| 793 | case ObjCImplementation: |
| 794 | return cast<ObjCImplementationDecl>(DC)->getClassInterface(); |
| 795 | |
| 796 | case ObjCInterface: |
| 797 | return cast<ObjCInterfaceDecl>(DC); |
| 798 | } |
| 799 | } |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 800 | |
| 801 | //===----------------------------------------------------------------------===// |
| 802 | // ObjCAtDefsFieldDecl |
| 803 | //===----------------------------------------------------------------------===// |
| 804 | |
| 805 | ObjCAtDefsFieldDecl |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 806 | *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC, |
| 807 | SourceLocation StartLoc, SourceLocation IdLoc, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 808 | IdentifierInfo *Id, QualType T, Expr *BW) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 809 | return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 812 | //===----------------------------------------------------------------------===// |
| 813 | // ObjCProtocolDecl |
| 814 | //===----------------------------------------------------------------------===// |
| 815 | |
| 816 | ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | SourceLocation L, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 818 | IdentifierInfo *Id) { |
| 819 | return new (C) ObjCProtocolDecl(DC, L, Id); |
| 820 | } |
| 821 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 822 | ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) { |
| 823 | ObjCProtocolDecl *PDecl = this; |
| 824 | |
| 825 | if (Name == getIdentifier()) |
| 826 | return PDecl; |
| 827 | |
| 828 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
| 829 | if ((PDecl = (*I)->lookupProtocolNamed(Name))) |
| 830 | return PDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 832 | return NULL; |
| 833 | } |
| 834 | |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 835 | // lookupMethod - Lookup a instance/class method in the protocol and protocols |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 836 | // it inherited. |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 837 | ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel, |
| 838 | bool isInstance) const { |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 839 | ObjCMethodDecl *MethodDecl = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 841 | if ((MethodDecl = getMethod(Sel, isInstance))) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 842 | return MethodDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 844 | for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I) |
Argyrios Kyrtzidis | 094e2bb | 2009-07-25 22:15:38 +0000 | [diff] [blame] | 845 | if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance))) |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 846 | return MethodDecl; |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | //===----------------------------------------------------------------------===// |
| 851 | // ObjCClassDecl |
| 852 | //===----------------------------------------------------------------------===// |
| 853 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | ObjCClassDecl::ObjCClassDecl(DeclContext *DC, SourceLocation L, |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 855 | ObjCInterfaceDecl *const Elt, |
| 856 | const SourceLocation Loc, |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 857 | ASTContext &C) |
| 858 | : Decl(ObjCClass, DC, L) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 859 | setClass(C, Elt, Loc); |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 860 | } |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 861 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 862 | ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, DeclContext *DC, |
| 863 | SourceLocation L, |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 864 | ObjCInterfaceDecl *const Elt, |
| 865 | const SourceLocation Loc) { |
| 866 | return new (C) ObjCClassDecl(DC, L, Elt, Loc, C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 869 | void ObjCClassDecl::setClass(ASTContext &C, ObjCInterfaceDecl*const Cls, |
| 870 | const SourceLocation Loc) { |
| 871 | |
| 872 | ForwardDecl = (ObjCClassRef*) C.Allocate(sizeof(ObjCClassRef), |
| 873 | llvm::alignOf<ObjCClassRef>()); |
| 874 | new (ForwardDecl) ObjCClassRef(Cls, Loc); |
| 875 | } |
| 876 | |
Ted Kremenek | 2dbdd62 | 2009-11-18 01:26:56 +0000 | [diff] [blame] | 877 | SourceRange ObjCClassDecl::getSourceRange() const { |
| 878 | // FIXME: We should include the semicolon |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 879 | return SourceRange(getLocation(), ForwardDecl->getLocation()); |
Ted Kremenek | 2dbdd62 | 2009-11-18 01:26:56 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 882 | //===----------------------------------------------------------------------===// |
| 883 | // ObjCForwardProtocolDecl |
| 884 | //===----------------------------------------------------------------------===// |
| 885 | |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 886 | ObjCForwardProtocolDecl:: |
| 887 | ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L, |
| 888 | ObjCProtocolDecl *const *Elts, unsigned nElts, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 889 | const SourceLocation *Locs, ASTContext &C) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | : Decl(ObjCForwardProtocol, DC, L) { |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 891 | ReferencedProtocols.set(Elts, nElts, Locs, C); |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 895 | ObjCForwardProtocolDecl * |
| 896 | ObjCForwardProtocolDecl::Create(ASTContext &C, DeclContext *DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | SourceLocation L, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 898 | ObjCProtocolDecl *const *Elts, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 899 | unsigned NumElts, |
| 900 | const SourceLocation *Locs) { |
| 901 | return new (C) ObjCForwardProtocolDecl(DC, L, Elts, NumElts, Locs, C); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 904 | //===----------------------------------------------------------------------===// |
| 905 | // ObjCCategoryDecl |
| 906 | //===----------------------------------------------------------------------===// |
| 907 | |
| 908 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 3db211b | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 909 | SourceLocation AtLoc, |
| 910 | SourceLocation ClassNameLoc, |
| 911 | SourceLocation CategoryNameLoc, |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame^] | 912 | IdentifierInfo *Id, |
| 913 | ObjCInterfaceDecl *IDecl) { |
| 914 | ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, |
| 915 | CategoryNameLoc, Id, |
| 916 | IDecl); |
| 917 | if (IDecl) { |
| 918 | // Link this category into its class's category list. |
| 919 | CatDecl->NextClassCategory = IDecl->getCategoryList(); |
| 920 | IDecl->setCategoryList(CatDecl); |
| 921 | IDecl->setChangedSinceDeserialization(true); |
| 922 | } |
| 923 | |
| 924 | return CatDecl; |
| 925 | } |
| 926 | |
| 927 | ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, EmptyShell Empty) { |
| 928 | return new (C) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(), |
| 929 | SourceLocation(), 0, 0); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 932 | ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const { |
| 933 | return getASTContext().getObjCImplementation( |
| 934 | const_cast<ObjCCategoryDecl*>(this)); |
| 935 | } |
| 936 | |
| 937 | void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) { |
| 938 | getASTContext().setObjCImplementation(this, ImplD); |
| 939 | } |
| 940 | |
| 941 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 942 | //===----------------------------------------------------------------------===// |
| 943 | // ObjCCategoryImplDecl |
| 944 | //===----------------------------------------------------------------------===// |
| 945 | |
| 946 | ObjCCategoryImplDecl * |
| 947 | ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC, |
| 948 | SourceLocation L,IdentifierInfo *Id, |
| 949 | ObjCInterfaceDecl *ClassInterface) { |
| 950 | return new (C) ObjCCategoryImplDecl(DC, L, Id, ClassInterface); |
| 951 | } |
| 952 | |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 953 | ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 954 | // The class interface might be NULL if we are working with invalid code. |
| 955 | if (const ObjCInterfaceDecl *ID = getClassInterface()) |
| 956 | return ID->FindCategoryDeclaration(getIdentifier()); |
| 957 | return 0; |
Argyrios Kyrtzidis | 4292073 | 2009-07-28 05:11:05 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 960 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 961 | void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) { |
Douglas Gregor | 2c2d43c | 2009-04-23 02:42:49 +0000 | [diff] [blame] | 962 | // FIXME: The context should be correct before we get here. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 963 | property->setLexicalDeclContext(this); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 964 | addDecl(property); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 967 | void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) { |
| 968 | ASTContext &Ctx = getASTContext(); |
| 969 | |
| 970 | if (ObjCImplementationDecl *ImplD |
Duncan Sands | 98f2cca | 2009-07-21 07:56:29 +0000 | [diff] [blame] | 971 | = dyn_cast_or_null<ObjCImplementationDecl>(this)) { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 972 | if (IFace) |
| 973 | Ctx.setObjCImplementation(IFace, ImplD); |
| 974 | |
Duncan Sands | 98f2cca | 2009-07-21 07:56:29 +0000 | [diff] [blame] | 975 | } else if (ObjCCategoryImplDecl *ImplD = |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 976 | dyn_cast_or_null<ObjCCategoryImplDecl>(this)) { |
| 977 | if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier())) |
| 978 | Ctx.setObjCImplementation(CD, ImplD); |
| 979 | } |
| 980 | |
| 981 | ClassInterface = IFace; |
| 982 | } |
| 983 | |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 984 | /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of |
Chris Lattner | d6eed1c | 2009-02-16 19:24:31 +0000 | [diff] [blame] | 985 | /// properties implemented in this category @implementation block and returns |
| 986 | /// the implemented property that uses it. |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 987 | /// |
Chris Lattner | 3aa1861 | 2009-02-28 18:42:10 +0000 | [diff] [blame] | 988 | ObjCPropertyImplDecl *ObjCImplDecl:: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 989 | FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const { |
| 990 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 991 | ObjCPropertyImplDecl *PID = *i; |
| 992 | if (PID->getPropertyIvarDecl() && |
| 993 | PID->getPropertyIvarDecl()->getIdentifier() == ivarId) |
| 994 | return PID; |
| 995 | } |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl |
| 1000 | /// added to the list of those properties @synthesized/@dynamic in this |
| 1001 | /// category @implementation block. |
| 1002 | /// |
Chris Lattner | 3aa1861 | 2009-02-28 18:42:10 +0000 | [diff] [blame] | 1003 | ObjCPropertyImplDecl *ObjCImplDecl:: |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1004 | FindPropertyImplDecl(IdentifierInfo *Id) const { |
| 1005 | for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){ |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1006 | ObjCPropertyImplDecl *PID = *i; |
| 1007 | if (PID->getPropertyDecl()->getIdentifier() == Id) |
| 1008 | return PID; |
| 1009 | } |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1013 | raw_ostream &clang::operator<<(raw_ostream &OS, |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1014 | const ObjCCategoryImplDecl *CID) { |
| 1015 | OS << CID->getName(); |
| 1016 | return OS; |
| 1017 | } |
| 1018 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1019 | //===----------------------------------------------------------------------===// |
| 1020 | // ObjCImplementationDecl |
| 1021 | //===----------------------------------------------------------------------===// |
| 1022 | |
| 1023 | ObjCImplementationDecl * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1025 | SourceLocation L, |
| 1026 | ObjCInterfaceDecl *ClassInterface, |
| 1027 | ObjCInterfaceDecl *SuperDecl) { |
| 1028 | return new (C) ObjCImplementationDecl(DC, L, ClassInterface, SuperDecl); |
| 1029 | } |
| 1030 | |
John McCall | da6d976 | 2011-07-22 04:15:06 +0000 | [diff] [blame] | 1031 | void ObjCImplementationDecl::setIvarInitializers(ASTContext &C, |
| 1032 | CXXCtorInitializer ** initializers, |
| 1033 | unsigned numInitializers) { |
| 1034 | if (numInitializers > 0) { |
| 1035 | NumIvarInitializers = numInitializers; |
| 1036 | CXXCtorInitializer **ivarInitializers = |
| 1037 | new (C) CXXCtorInitializer*[NumIvarInitializers]; |
| 1038 | memcpy(ivarInitializers, initializers, |
| 1039 | numInitializers * sizeof(CXXCtorInitializer*)); |
| 1040 | IvarInitializers = ivarInitializers; |
| 1041 | } |
| 1042 | } |
| 1043 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1044 | raw_ostream &clang::operator<<(raw_ostream &OS, |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1045 | const ObjCImplementationDecl *ID) { |
| 1046 | OS << ID->getName(); |
| 1047 | return OS; |
| 1048 | } |
| 1049 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1050 | //===----------------------------------------------------------------------===// |
| 1051 | // ObjCCompatibleAliasDecl |
| 1052 | //===----------------------------------------------------------------------===// |
| 1053 | |
| 1054 | ObjCCompatibleAliasDecl * |
| 1055 | ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 1056 | SourceLocation L, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | IdentifierInfo *Id, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1058 | ObjCInterfaceDecl* AliasedClass) { |
| 1059 | return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass); |
| 1060 | } |
| 1061 | |
| 1062 | //===----------------------------------------------------------------------===// |
| 1063 | // ObjCPropertyDecl |
| 1064 | //===----------------------------------------------------------------------===// |
| 1065 | |
| 1066 | ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC, |
| 1067 | SourceLocation L, |
| 1068 | IdentifierInfo *Id, |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 1069 | SourceLocation AtLoc, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 1070 | TypeSourceInfo *T, |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1071 | PropertyControl propControl) { |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 1072 | return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T); |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Chris Lattner | ab35163 | 2009-02-20 20:59:54 +0000 | [diff] [blame] | 1075 | //===----------------------------------------------------------------------===// |
| 1076 | // ObjCPropertyImplDecl |
| 1077 | //===----------------------------------------------------------------------===// |
| 1078 | |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1079 | ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1080 | DeclContext *DC, |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1081 | SourceLocation atLoc, |
| 1082 | SourceLocation L, |
| 1083 | ObjCPropertyDecl *property, |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 1084 | Kind PK, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1085 | ObjCIvarDecl *ivar, |
| 1086 | SourceLocation ivarLoc) { |
| 1087 | return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar, |
| 1088 | ivarLoc); |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1089 | } |
Chris Lattner | f4af515 | 2008-03-17 01:19:02 +0000 | [diff] [blame] | 1090 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1091 | SourceRange ObjCPropertyImplDecl::getSourceRange() const { |
| 1092 | SourceLocation EndLoc = getLocation(); |
| 1093 | if (IvarLoc.isValid()) |
| 1094 | EndLoc = IvarLoc; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1096 | return SourceRange(AtLoc, EndLoc); |
| 1097 | } |