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