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