Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/ASTMutationListener.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 18 | #include "clang/AST/DataRecursiveASTVisitor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
| 24 | #include "clang/Sema/ExternalSemaSource.h" |
| 25 | #include "clang/Sema/Lookup.h" |
| 26 | #include "clang/Sema/Scope.h" |
| 27 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 32 | /// Check whether the given method, which must be in the 'init' |
| 33 | /// family, is a valid member of that family. |
| 34 | /// |
| 35 | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| 36 | /// if non-null, check this as if making a call to it with the given |
| 37 | /// receiver type |
| 38 | /// |
| 39 | /// \return true to indicate that there was an error and appropriate |
| 40 | /// actions were taken |
| 41 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 42 | QualType receiverTypeIfCall) { |
| 43 | if (method->isInvalidDecl()) return true; |
| 44 | |
| 45 | // This castAs is safe: methods that don't return an object |
| 46 | // pointer won't be inferred as inits and will reject an explicit |
| 47 | // objc_method_family(init). |
| 48 | |
| 49 | // We ignore protocols here. Should we? What about Class? |
| 50 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 51 | const ObjCObjectType *result = |
| 52 | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 53 | |
| 54 | if (result->isObjCId()) { |
| 55 | return false; |
| 56 | } else if (result->isObjCClass()) { |
| 57 | // fall through: always an error |
| 58 | } else { |
| 59 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 60 | assert(resultClass && "unexpected object type!"); |
| 61 | |
| 62 | // It's okay for the result type to still be a forward declaration |
| 63 | // if we're checking an interface declaration. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 64 | if (!resultClass->hasDefinition()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 65 | if (receiverTypeIfCall.isNull() && |
| 66 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 67 | return false; |
| 68 | |
| 69 | // Otherwise, we try to compare class types. |
| 70 | } else { |
| 71 | // If this method was declared in a protocol, we can't check |
| 72 | // anything unless we have a receiver type that's an interface. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 73 | const ObjCInterfaceDecl *receiverClass = nullptr; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 74 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 75 | if (receiverTypeIfCall.isNull()) |
| 76 | return false; |
| 77 | |
| 78 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 79 | ->getInterfaceDecl(); |
| 80 | |
| 81 | // This can be null for calls to e.g. id<Foo>. |
| 82 | if (!receiverClass) return false; |
| 83 | } else { |
| 84 | receiverClass = method->getClassInterface(); |
| 85 | assert(receiverClass && "method not associated with a class!"); |
| 86 | } |
| 87 | |
| 88 | // If either class is a subclass of the other, it's fine. |
| 89 | if (receiverClass->isSuperClassOf(resultClass) || |
| 90 | resultClass->isSuperClassOf(receiverClass)) |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | SourceLocation loc = method->getLocation(); |
| 96 | |
| 97 | // If we're in a system header, and this is not a call, just make |
| 98 | // the method unusable. |
| 99 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 100 | method->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 101 | "init method returns a type unrelated to its receiver type", |
| 102 | loc)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Otherwise, it's an error. |
| 107 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 108 | method->setInvalidDecl(); |
| 109 | return true; |
| 110 | } |
| 111 | |
Fariborz Jahanian | 3240fe3 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 112 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 113 | const ObjCMethodDecl *Overridden) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 114 | if (Overridden->hasRelatedResultType() && |
| 115 | !NewMethod->hasRelatedResultType()) { |
| 116 | // This can only happen when the method follows a naming convention that |
| 117 | // implies a related result type, and the original (overridden) method has |
| 118 | // a suitable return type, but the new (overriding) method does not have |
| 119 | // a suitable return type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 120 | QualType ResultType = NewMethod->getReturnType(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 121 | SourceRange ResultTypeRange; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 122 | if (const TypeSourceInfo *ResultTypeInfo = |
| 123 | NewMethod->getReturnTypeSourceInfo()) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 124 | ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange(); |
| 125 | |
| 126 | // Figure out which class this method is part of, if any. |
| 127 | ObjCInterfaceDecl *CurrentClass |
| 128 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 129 | if (!CurrentClass) { |
| 130 | DeclContext *DC = NewMethod->getDeclContext(); |
| 131 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 132 | CurrentClass = Cat->getClassInterface(); |
| 133 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 134 | CurrentClass = Impl->getClassInterface(); |
| 135 | else if (ObjCCategoryImplDecl *CatImpl |
| 136 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 137 | CurrentClass = CatImpl->getClassInterface(); |
| 138 | } |
| 139 | |
| 140 | if (CurrentClass) { |
| 141 | Diag(NewMethod->getLocation(), |
| 142 | diag::warn_related_result_type_compatibility_class) |
| 143 | << Context.getObjCInterfaceType(CurrentClass) |
| 144 | << ResultType |
| 145 | << ResultTypeRange; |
| 146 | } else { |
| 147 | Diag(NewMethod->getLocation(), |
| 148 | diag::warn_related_result_type_compatibility_protocol) |
| 149 | << ResultType |
| 150 | << ResultTypeRange; |
| 151 | } |
| 152 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 153 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
| 154 | Diag(Overridden->getLocation(), |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 155 | diag::note_related_result_type_family) |
| 156 | << /*overridden method*/ 0 |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 157 | << Family; |
| 158 | else |
| 159 | Diag(Overridden->getLocation(), |
| 160 | diag::note_related_result_type_overridden); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 161 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 162 | if (getLangOpts().ObjCAutoRefCount) { |
Fariborz Jahanian | 3240fe3 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 163 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 164 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 165 | Diag(NewMethod->getLocation(), |
| 166 | diag::err_nsreturns_retained_attribute_mismatch) << 1; |
| 167 | Diag(Overridden->getLocation(), diag::note_previous_decl) |
| 168 | << "method"; |
| 169 | } |
| 170 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 171 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 172 | Diag(NewMethod->getLocation(), |
| 173 | diag::err_nsreturns_retained_attribute_mismatch) << 0; |
| 174 | Diag(Overridden->getLocation(), diag::note_previous_decl) |
| 175 | << "method"; |
| 176 | } |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 177 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 178 | oe = Overridden->param_end(); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 179 | for (ObjCMethodDecl::param_iterator |
| 180 | ni = NewMethod->param_begin(), ne = NewMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 181 | ni != ne && oi != oe; ++ni, ++oi) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 182 | const ParmVarDecl *oldDecl = (*oi); |
Fariborz Jahanian | 3240fe3 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 183 | ParmVarDecl *newDecl = (*ni); |
| 184 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 185 | oldDecl->hasAttr<NSConsumedAttr>()) { |
| 186 | Diag(newDecl->getLocation(), |
| 187 | diag::err_nsconsumed_attribute_mismatch); |
| 188 | Diag(oldDecl->getLocation(), diag::note_previous_decl) |
| 189 | << "parameter"; |
| 190 | } |
| 191 | } |
| 192 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 193 | } |
| 194 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 195 | /// \brief Check a method declaration for compatibility with the Objective-C |
| 196 | /// ARC conventions. |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 197 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | ObjCMethodFamily family = method->getMethodFamily(); |
| 199 | switch (family) { |
| 200 | case OMF_None: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 201 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 202 | case OMF_retain: |
| 203 | case OMF_release: |
| 204 | case OMF_autorelease: |
| 205 | case OMF_retainCount: |
| 206 | case OMF_self: |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 207 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 208 | return false; |
| 209 | |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 210 | case OMF_dealloc: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 211 | if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 212 | SourceRange ResultTypeRange; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 213 | if (const TypeSourceInfo *ResultTypeInfo = |
| 214 | method->getReturnTypeSourceInfo()) |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 215 | ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange(); |
| 216 | if (ResultTypeRange.isInvalid()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 217 | Diag(method->getLocation(), diag::error_dealloc_bad_result_type) |
| 218 | << method->getReturnType() |
| 219 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 220 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 221 | Diag(method->getLocation(), diag::error_dealloc_bad_result_type) |
| 222 | << method->getReturnType() |
| 223 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | return false; |
| 227 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 228 | case OMF_init: |
| 229 | // If the method doesn't obey the init rules, don't bother annotating it. |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 230 | if (checkInitMethod(method, QualType())) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 231 | return true; |
| 232 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 233 | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 234 | |
| 235 | // Don't add a second copy of this attribute, but otherwise don't |
| 236 | // let it be suppressed. |
| 237 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 238 | return false; |
| 239 | break; |
| 240 | |
| 241 | case OMF_alloc: |
| 242 | case OMF_copy: |
| 243 | case OMF_mutableCopy: |
| 244 | case OMF_new: |
| 245 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 246 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 247 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 248 | return false; |
| 249 | break; |
| 250 | } |
| 251 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 252 | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 256 | static void DiagnoseObjCImplementedDeprecations(Sema &S, |
| 257 | NamedDecl *ND, |
| 258 | SourceLocation ImplLoc, |
| 259 | int select) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 260 | if (ND && ND->isDeprecated()) { |
Fariborz Jahanian | 98d810e | 2011-02-16 00:30:31 +0000 | [diff] [blame] | 261 | S.Diag(ImplLoc, diag::warn_deprecated_def) << select; |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 262 | if (select == 0) |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 263 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 264 | << ND->getDeclName(); |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 265 | else |
| 266 | S.Diag(ND->getLocation(), diag::note_previous_decl) << "class"; |
| 267 | } |
| 268 | } |
| 269 | |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 270 | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| 271 | /// pool. |
| 272 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 273 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| 274 | |
| 275 | // If we don't have a valid method decl, simply return. |
| 276 | if (!MDecl) |
| 277 | return; |
| 278 | if (MDecl->isInstanceMethod()) |
| 279 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 280 | else |
| 281 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 282 | } |
| 283 | |
Fariborz Jahanian | a1a32f7 | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 284 | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| 285 | /// has explicit ownership attribute; false otherwise. |
| 286 | static bool |
| 287 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 288 | QualType T = Param->getType(); |
| 289 | |
Fariborz Jahanian | a1a32f7 | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 290 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 291 | T = PT->getPointeeType(); |
| 292 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 293 | T = RT->getPointeeType(); |
| 294 | } else { |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | // If we have a lifetime qualifier, but it's local, we must have |
| 299 | // inferred it. So, it is implicit. |
| 300 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 301 | } |
| 302 | |
Fariborz Jahanian | 8c6cb46 | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 303 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| 304 | /// and user declared, in the method definition's AST. |
| 305 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 306 | assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 307 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 308 | |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 309 | // If we don't have a valid method decl, simply return. |
| 310 | if (!MDecl) |
| 311 | return; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 313 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 314 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 315 | PushFunctionScope(); |
| 316 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 317 | // Create Decl objects for each parameter, entrring them in the scope for |
| 318 | // binding to their use. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 319 | |
| 320 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 321 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Daniel Dunbar | 451318c | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 323 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 324 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 325 | |
Reid Kleckner | 8c0501c | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 326 | // The ObjC parser requires parameter names so there's no need to check. |
| 327 | CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(), |
| 328 | /*CheckParameterNames=*/false); |
| 329 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 330 | // Introduce all of the other parameters into this scope. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 331 | for (auto *Param : MDecl->params()) { |
Fariborz Jahanian | 23c0104 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 332 | if (!Param->isInvalidDecl() && |
Fariborz Jahanian | a1a32f7 | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 333 | getLangOpts().ObjCAutoRefCount && |
| 334 | !HasExplicitOwnershipAttr(*this, Param)) |
| 335 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 336 | Param->getType(); |
Fariborz Jahanian | 918546c | 2012-08-30 23:56:02 +0000 | [diff] [blame] | 337 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 338 | if (Param->getIdentifier()) |
| 339 | PushOnScopeChains(Param, FnBodyScope); |
Fariborz Jahanian | 23c0104 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 340 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 341 | |
| 342 | // In ARC, disallow definition of retain/release/autorelease/retainCount |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 343 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 344 | switch (MDecl->getMethodFamily()) { |
| 345 | case OMF_retain: |
| 346 | case OMF_retainCount: |
| 347 | case OMF_release: |
| 348 | case OMF_autorelease: |
| 349 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
Fariborz Jahanian | b8ed071 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 350 | << 0 << MDecl->getSelector(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case OMF_None: |
| 354 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 355 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 356 | case OMF_alloc: |
| 357 | case OMF_init: |
| 358 | case OMF_mutableCopy: |
| 359 | case OMF_copy: |
| 360 | case OMF_new: |
| 361 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 362 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 367 | // Warn on deprecated methods under -Wdeprecated-implementations, |
| 368 | // and prepare for warning on missing super calls. |
| 369 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 370 | ObjCMethodDecl *IMD = |
| 371 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
| 372 | |
Fariborz Jahanian | 5fa6676 | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 373 | if (IMD) { |
| 374 | ObjCImplDecl *ImplDeclOfMethodDef = |
| 375 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
| 376 | ObjCContainerDecl *ContDeclOfMethodDecl = |
| 377 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 378 | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
Fariborz Jahanian | 5fa6676 | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 379 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 380 | ImplDeclOfMethodDecl = OID->getImplementation(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 381 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| 382 | if (CD->IsClassExtension()) { |
| 383 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| 384 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 385 | } else |
| 386 | ImplDeclOfMethodDecl = CD->getImplementation(); |
| 387 | } |
Fariborz Jahanian | 5fa6676 | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 388 | // No need to issue deprecated warning if deprecated mehod in class/category |
| 389 | // is being implemented in its own implementation (no overriding is involved). |
| 390 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
| 391 | DiagnoseObjCImplementedDeprecations(*this, |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 392 | dyn_cast<NamedDecl>(IMD), |
| 393 | MDecl->getLocation(), 0); |
Fariborz Jahanian | 5fa6676 | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 394 | } |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 395 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 396 | if (MDecl->getMethodFamily() == OMF_init) { |
| 397 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
| 398 | getCurFunction()->ObjCIsDesignatedInit = true; |
| 399 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 400 | IC->getSuperClass() != nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 401 | } else if (IC->hasDesignatedInitializers()) { |
| 402 | getCurFunction()->ObjCIsSecondaryInit = true; |
| 403 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
| 404 | } |
| 405 | } |
| 406 | |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 407 | // If this is "dealloc" or "finalize", set some bit here. |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 408 | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| 409 | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| 410 | // Only do this if the current class actually has a superclass. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 411 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 412 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 413 | if (Family == OMF_dealloc) { |
| 414 | if (!(getLangOpts().ObjCAutoRefCount || |
| 415 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 416 | getCurFunction()->ObjCShouldCallSuper = true; |
| 417 | |
| 418 | } else if (Family == OMF_finalize) { |
| 419 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 420 | getCurFunction()->ObjCShouldCallSuper = true; |
| 421 | |
Fariborz Jahanian | 2b61039 | 2013-11-05 00:28:21 +0000 | [diff] [blame] | 422 | } else { |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 423 | const ObjCMethodDecl *SuperMethod = |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 424 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 425 | MDecl->isInstanceMethod()); |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 426 | getCurFunction()->ObjCShouldCallSuper = |
| 427 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
Fariborz Jahanian | 6f93860 | 2012-09-10 18:04:25 +0000 | [diff] [blame] | 428 | } |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 429 | } |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 430 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 433 | namespace { |
| 434 | |
| 435 | // Callback to only accept typo corrections that are Objective-C classes. |
| 436 | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| 437 | // function will reject corrections to that class. |
| 438 | class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback { |
| 439 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 440 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 441 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 442 | : CurrentIDecl(IDecl) {} |
| 443 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 444 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 445 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 446 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 447 | } |
| 448 | |
| 449 | private: |
| 450 | ObjCInterfaceDecl *CurrentIDecl; |
| 451 | }; |
| 452 | |
| 453 | } |
| 454 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 455 | Decl *Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 456 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 457 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 458 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 459 | Decl * const *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 460 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 461 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 462 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 464 | // Check for another declaration kind with the same name. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 465 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 466 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 468 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 469 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 470 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 473 | // Create a declaration to describe this @interface. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 474 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | e7e8fca | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 475 | |
| 476 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 477 | // A previous decl with a different name is because of |
| 478 | // @compatibility_alias, for example: |
| 479 | // \code |
| 480 | // @class NewImage; |
| 481 | // @compatibility_alias OldImage NewImage; |
| 482 | // \endcode |
| 483 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 484 | // |
| 485 | // In such a case use the real declaration name, instead of the alias one, |
| 486 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 487 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 488 | // has been aliased. |
| 489 | ClassName = PrevIDecl->getIdentifier(); |
| 490 | } |
| 491 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 492 | ObjCInterfaceDecl *IDecl |
| 493 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 494 | PrevIDecl, ClassLoc); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 496 | if (PrevIDecl) { |
| 497 | // Class already seen. Was it a definition? |
| 498 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 499 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 500 | << PrevIDecl->getDeclName(); |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 501 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 502 | IDecl->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 503 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 504 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 505 | |
| 506 | if (AttrList) |
| 507 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| 508 | PushOnScopeChains(IDecl, TUScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 510 | // Start the definition of this class. If we're in a redefinition case, there |
| 511 | // may already be a definition, so we'll end up adding to it. |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 512 | if (!IDecl->hasDefinition()) |
| 513 | IDecl->startDefinition(); |
| 514 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 515 | if (SuperName) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 516 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 517 | PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 518 | LookupOrdinaryName); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 519 | |
| 520 | if (!PrevDecl) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 521 | // Try to correct for a typo in the superclass name without correcting |
| 522 | // to the class we're defining. |
| 523 | ObjCInterfaceValidatorCCC Validator(IDecl); |
| 524 | if (TypoCorrection Corrected = CorrectTypo( |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 525 | DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 526 | nullptr, Validator, CTK_ErrorRecovery)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 527 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 528 | << SuperName << ClassName); |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 529 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 533 | if (declaresSameEntity(PrevDecl, IDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 534 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 535 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 536 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 537 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | ObjCInterfaceDecl *SuperClassDecl = |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 539 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 540 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 541 | // Diagnose classes that inherit from deprecated classes. |
| 542 | if (SuperClassDecl) |
| 543 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 545 | if (PrevDecl && !SuperClassDecl) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 546 | // The previous declaration was not a class decl. Check if we have a |
| 547 | // typedef. If we do, get the underlying class type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 548 | if (const TypedefNameDecl *TDecl = |
| 549 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 550 | QualType T = TDecl->getUnderlyingType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 551 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | 740991b | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 552 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 553 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | 740991b | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 554 | // This handles the following case: |
| 555 | // @interface NewI @end |
| 556 | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| 557 | // @interface SI : DeprI /* warn here */ @end |
| 558 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 559 | } |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 563 | // This handles the following case: |
| 564 | // |
| 565 | // typedef int SuperClass; |
| 566 | // @interface MyClass : SuperClass {} @end |
| 567 | // |
| 568 | if (!SuperClassDecl) { |
| 569 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 570 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 574 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 575 | if (!SuperClassDecl) |
| 576 | Diag(SuperLoc, diag::err_undef_superclass) |
| 577 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 578 | else if (RequireCompleteType(SuperLoc, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 579 | Context.getObjCInterfaceType(SuperClassDecl), |
| 580 | diag::err_forward_superclass, |
| 581 | SuperClassDecl->getDeclName(), |
| 582 | ClassName, |
| 583 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 584 | SuperClassDecl = nullptr; |
Fariborz Jahanian | a813973 | 2011-06-23 23:16:19 +0000 | [diff] [blame] | 585 | } |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 586 | } |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 587 | IDecl->setSuperClass(SuperClassDecl); |
| 588 | IDecl->setSuperClassLoc(SuperLoc); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 589 | IDecl->setEndOfDefinitionLoc(SuperLoc); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 591 | } else { // we have a root class. |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 592 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 593 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 595 | // Check then save referenced protocols. |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 596 | if (NumProtoRefs) { |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 597 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 598 | ProtoLocs, Context); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 599 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 602 | CheckObjCDeclScope(IDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 603 | return ActOnObjCContainerStartDefinition(IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Fariborz Jahanian | a924f84 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 606 | /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
| 607 | /// typedef'ed use for a qualified super class and adds them to the list |
| 608 | /// of the protocols. |
| 609 | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
| 610 | IdentifierInfo *SuperName, |
| 611 | SourceLocation SuperLoc) { |
| 612 | if (!SuperName) |
| 613 | return; |
| 614 | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 615 | LookupOrdinaryName); |
| 616 | if (!IDecl) |
| 617 | return; |
| 618 | |
| 619 | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| 620 | QualType T = TDecl->getUnderlyingType(); |
| 621 | if (T->isObjCObjectType()) |
| 622 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 623 | for (auto *I : OPT->quals()) |
| 624 | ProtocolRefs.push_back(I); |
Fariborz Jahanian | a924f84 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Richard Smith | de01b7a | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 628 | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 629 | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
Richard Smith | de01b7a | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 630 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 631 | IdentifierInfo *AliasName, |
| 632 | SourceLocation AliasLocation, |
| 633 | IdentifierInfo *ClassName, |
| 634 | SourceLocation ClassLocation) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 635 | // Look for previous declaration of alias name |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 636 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 637 | LookupOrdinaryName, ForRedeclaration); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 638 | if (ADecl) { |
Eli Friedman | 104f96b | 2013-06-21 01:49:53 +0000 | [diff] [blame] | 639 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 640 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 641 | return nullptr; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 642 | } |
| 643 | // Check for class declaration |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 644 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 645 | LookupOrdinaryName, ForRedeclaration); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 646 | if (const TypedefNameDecl *TDecl = |
| 647 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 648 | QualType T = TDecl->getUnderlyingType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 649 | if (T->isObjCObjectType()) { |
| 650 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 651 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 652 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 653 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | } |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 657 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 658 | if (!CDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 659 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 660 | if (CDeclU) |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 661 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 662 | return nullptr; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 665 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 667 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 669 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 670 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 671 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 672 | return AliasDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 675 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 676 | IdentifierInfo *PName, |
| 677 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 678 | const ObjCList<ObjCProtocolDecl> &PList) { |
| 679 | |
| 680 | bool res = false; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 681 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 682 | E = PList.end(); I != E; ++I) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 683 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 684 | Ploc)) { |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 685 | if (PDecl->getIdentifier() == PName) { |
| 686 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 687 | Diag(PrevLoc, diag::note_previous_definition); |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 688 | res = true; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 689 | } |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 690 | |
| 691 | if (!PDecl->hasDefinition()) |
| 692 | continue; |
| 693 | |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 694 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 695 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 696 | res = true; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 699 | return res; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 700 | } |
| 701 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 702 | Decl * |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 703 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 704 | IdentifierInfo *ProtocolName, |
| 705 | SourceLocation ProtocolLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 706 | Decl * const *ProtoRefs, |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 707 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 708 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 709 | SourceLocation EndProtoLoc, |
| 710 | AttributeList *AttrList) { |
Fariborz Jahanian | 96b69a7 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 711 | bool err = false; |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 712 | // FIXME: Deal with AttrList. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 713 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 714 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
| 715 | ForRedeclaration); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 716 | ObjCProtocolDecl *PDecl = nullptr; |
| 717 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 718 | // If we already have a definition, complain. |
| 719 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 720 | Diag(Def->getLocation(), diag::note_previous_definition); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 722 | // Create a new protocol that is completely distinct from previous |
| 723 | // declarations, and do not make this protocol available for name lookup. |
| 724 | // That way, we'll end up completely ignoring the duplicate. |
| 725 | // FIXME: Can we turn this into an error? |
| 726 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 727 | ProtocolLoc, AtProtoInterfaceLoc, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 728 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 729 | PDecl->startDefinition(); |
| 730 | } else { |
| 731 | if (PrevDecl) { |
| 732 | // Check for circular dependencies among protocol declarations. This can |
| 733 | // only happen if this protocol was forward-declared. |
Argyrios Kyrtzidis | 4fc04da | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 734 | ObjCList<ObjCProtocolDecl> PList; |
| 735 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 736 | err = CheckForwardProtocolDeclarationForCircularDependency( |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 737 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
Argyrios Kyrtzidis | 4fc04da | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 738 | } |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 739 | |
| 740 | // Create the new declaration. |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 741 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
Argyrios Kyrtzidis | b05d7b2 | 2011-10-17 19:48:06 +0000 | [diff] [blame] | 742 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | c9d3c7e | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 743 | /*PrevDecl=*/PrevDecl); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 745 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 746 | PDecl->startDefinition(); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 747 | } |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 748 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 749 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 750 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 751 | |
| 752 | // Merge attributes from previous declarations. |
| 753 | if (PrevDecl) |
| 754 | mergeDeclAttributes(PDecl, PrevDecl); |
| 755 | |
Fariborz Jahanian | 96b69a7 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 756 | if (!err && NumProtoRefs ) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 757 | /// Check then save referenced protocols. |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 758 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 759 | ProtoLocs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 760 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
| 762 | CheckObjCDeclScope(PDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 763 | return ActOnObjCContainerStartDefinition(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 766 | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| 767 | ObjCProtocolDecl *&UndefinedProtocol) { |
| 768 | if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| 769 | UndefinedProtocol = PDecl; |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | for (auto *PI : PDecl->protocols()) |
| 774 | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| 775 | UndefinedProtocol = PI; |
| 776 | return true; |
| 777 | } |
| 778 | return false; |
| 779 | } |
| 780 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 781 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 782 | /// issues an error if they are not declared. It returns list of |
| 783 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 784 | void |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 785 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 786 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 787 | unsigned NumProtocols, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 788 | SmallVectorImpl<Decl *> &Protocols) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 789 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 790 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first, |
| 791 | ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 792 | if (!PDecl) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 793 | DeclFilterCCC<ObjCProtocolDecl> Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 794 | TypoCorrection Corrected = CorrectTypo( |
| 795 | DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 796 | LookupObjCProtocolName, TUScope, nullptr, Validator, |
| 797 | CTK_ErrorRecovery); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 798 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 799 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
| 800 | << ProtocolId[i].first); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | if (!PDecl) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 804 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 805 | << ProtocolId[i].first; |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 806 | continue; |
| 807 | } |
Fariborz Jahanian | 3c9a024 | 2013-04-09 17:52:29 +0000 | [diff] [blame] | 808 | // If this is a forward protocol declaration, get its definition. |
| 809 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 810 | PDecl = PDecl->getDefinition(); |
| 811 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 812 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 813 | |
| 814 | // If this is a forward declaration and we are supposed to warn in this |
| 815 | // case, do it. |
Douglas Gregor | 0f9b9f3 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 816 | // FIXME: Recover nicely in the hidden case. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 817 | ObjCProtocolDecl *UndefinedProtocol; |
| 818 | |
Douglas Gregor | 0f9b9f3 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 819 | if (WarnOnDeclarations && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 820 | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 821 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 822 | << ProtocolId[i].first; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 823 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| 824 | << UndefinedProtocol; |
| 825 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 826 | Protocols.push_back(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Fariborz Jahanian | 78c39c7 | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 830 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 831 | /// a class method in its extension. |
| 832 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 834 | ObjCInterfaceDecl *ID) { |
| 835 | if (!ID) |
| 836 | return; // Possibly due to previous error |
| 837 | |
| 838 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 839 | for (auto *MD : ID->methods()) |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 840 | MethodMap[MD->getSelector()] = MD; |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 841 | |
| 842 | if (MethodMap.empty()) |
| 843 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 844 | for (const auto *Method : CAT->methods()) { |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 845 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 846 | if (PrevMethod && |
| 847 | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| 848 | !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 849 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 850 | << Method->getDeclName(); |
| 851 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 856 | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 857 | Sema::DeclGroupPtrTy |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 858 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 859 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 860 | unsigned NumElts, |
| 861 | AttributeList *attrList) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 862 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 863 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 864 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 865 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second, |
| 866 | ForRedeclaration); |
| 867 | ObjCProtocolDecl *PDecl |
| 868 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
| 869 | IdentList[i].second, AtProtocolLoc, |
Douglas Gregor | c9d3c7e | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 870 | PrevDecl); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 871 | |
| 872 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 873 | CheckObjCDeclScope(PDecl); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 874 | |
Douglas Gregor | 3937f87 | 2012-01-01 20:33:24 +0000 | [diff] [blame] | 875 | if (attrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 876 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 877 | |
| 878 | if (PrevDecl) |
| 879 | mergeDeclAttributes(PDecl, PrevDecl); |
| 880 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 881 | DeclsInGroup.push_back(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 884 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 885 | } |
| 886 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 887 | Decl *Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 888 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 889 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 890 | IdentifierInfo *CategoryName, |
| 891 | SourceLocation CategoryLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 892 | Decl * const *ProtoRefs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 893 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 894 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 895 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 896 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 897 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 898 | |
| 899 | /// Check that class of this category is already completely declared. |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 900 | |
| 901 | if (!IDecl |
| 902 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 903 | diag::err_category_forward_interface, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 904 | CategoryName == nullptr)) { |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 905 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 906 | // the enclosing method declarations. We mark the decl invalid |
| 907 | // to make it clear that this isn't a valid AST. |
| 908 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 909 | ClassLoc, CategoryLoc, CategoryName,IDecl); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 910 | CDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 9a0b6b4 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 911 | CurContext->addDecl(CDecl); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 912 | |
| 913 | if (!IDecl) |
| 914 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 915 | return ActOnObjCContainerStartDefinition(CDecl); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 918 | if (!CategoryName && IDecl->getImplementation()) { |
| 919 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 920 | Diag(IDecl->getImplementation()->getLocation(), |
| 921 | diag::note_implementation_declared); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 924 | if (CategoryName) { |
| 925 | /// Check for duplicate interface declaration for this category |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 926 | if (ObjCCategoryDecl *Previous |
| 927 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 928 | // Class extensions can be declared multiple times, categories cannot. |
| 929 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 930 | << ClassName << CategoryName; |
| 931 | Diag(Previous->getLocation(), diag::note_previous_definition); |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 934 | |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 935 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 936 | ClassLoc, CategoryLoc, CategoryName, IDecl); |
| 937 | // FIXME: PushOnScopeChains? |
| 938 | CurContext->addDecl(CDecl); |
| 939 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 940 | if (NumProtoRefs) { |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 941 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 942 | ProtoLocs, Context); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 943 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 944 | if (CDecl->IsClassExtension()) |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 945 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 946 | NumProtoRefs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 949 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 950 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 954 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 955 | /// object. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 956 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 957 | SourceLocation AtCatImplLoc, |
| 958 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 959 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 960 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 961 | ObjCCategoryDecl *CatIDecl = nullptr; |
Argyrios Kyrtzidis | 5a61e0c | 2012-03-02 19:14:29 +0000 | [diff] [blame] | 962 | if (IDecl && IDecl->hasDefinition()) { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 963 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 964 | if (!CatIDecl) { |
| 965 | // Category @implementation with no corresponding @interface. |
| 966 | // Create and install one. |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 967 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 968 | ClassLoc, CatLoc, |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 969 | CatName, IDecl); |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 970 | CatIDecl->setImplicit(); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | ObjCCategoryImplDecl *CDecl = |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 975 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
Argyrios Kyrtzidis | c699400 | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 976 | ClassLoc, AtCatImplLoc, CatLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 977 | /// Check that class of this category is already completely declared. |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 978 | if (!IDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 979 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 980 | CDecl->setInvalidDecl(); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 981 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 982 | diag::err_undef_interface)) { |
| 983 | CDecl->setInvalidDecl(); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 984 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 985 | |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 986 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 987 | CurContext->addDecl(CDecl); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 988 | |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 989 | // If the interface is deprecated/unavailable, warn/error about it. |
| 990 | if (IDecl) |
| 991 | DiagnoseUseOfDecl(IDecl, ClassLoc); |
| 992 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 993 | /// Check that CatName, category name, is not used in another implementation. |
| 994 | if (CatIDecl) { |
| 995 | if (CatIDecl->getImplementation()) { |
| 996 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 997 | << CatName; |
| 998 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 999 | diag::note_previous_definition); |
Argyrios Kyrtzidis | df08c4b | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1000 | CDecl->setInvalidDecl(); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1001 | } else { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1002 | CatIDecl->setImplementation(CDecl); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1003 | // Warn on implementating category of deprecated class under |
| 1004 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 1005 | DiagnoseObjCImplementedDeprecations(*this, |
| 1006 | dyn_cast<NamedDecl>(IDecl), |
| 1007 | CDecl->getLocation(), 2); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1008 | } |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1009 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1011 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1012 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1015 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1016 | SourceLocation AtClassImplLoc, |
| 1017 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | IdentifierInfo *SuperClassname, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1019 | SourceLocation SuperClassLoc) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1020 | ObjCInterfaceDecl *IDecl = nullptr; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1021 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1022 | NamedDecl *PrevDecl |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1023 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 1024 | ForRedeclaration); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1025 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1026 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1027 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1028 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1029 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1030 | diag::warn_undef_interface); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1031 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1032 | // We did not find anything with the name ClassName; try to correct for |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1033 | // typos in the class name. |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1034 | ObjCInterfaceValidatorCCC Validator; |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1035 | TypoCorrection Corrected = |
| 1036 | CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1037 | LookupOrdinaryName, TUScope, nullptr, Validator, |
| 1038 | CTK_NonError); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1039 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1040 | // Suggest the (potentially) correct interface name. Don't provide a |
| 1041 | // code-modification hint or use the typo name for recovery, because |
| 1042 | // this is just a warning. The program may actually be correct. |
| 1043 | diagnoseTypo(Corrected, |
| 1044 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 1045 | /*ErrorRecovery*/false); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1046 | } else { |
| 1047 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 1048 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1051 | // Check that super class name is valid class name |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1052 | ObjCInterfaceDecl *SDecl = nullptr; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1053 | if (SuperClassname) { |
| 1054 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1055 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 1056 | LookupOrdinaryName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1057 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1058 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 1059 | << SuperClassname; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1060 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1061 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | cd707ab | 2012-03-13 01:09:36 +0000 | [diff] [blame] | 1063 | if (SDecl && !SDecl->hasDefinition()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1064 | SDecl = nullptr; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1065 | if (!SDecl) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1066 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 1067 | << SuperClassname << ClassName; |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1068 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1069 | // This implementation and its interface do not have the same |
| 1070 | // super class. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1071 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1072 | << SDecl->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1073 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1078 | if (!IDecl) { |
| 1079 | // Legacy case of @implementation with no corresponding @interface. |
| 1080 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 1081 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1082 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 1083 | // copy them over. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1085 | ClassName, /*PrevDecl=*/nullptr, ClassLoc, |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1086 | true); |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1087 | IDecl->startDefinition(); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1088 | if (SDecl) { |
| 1089 | IDecl->setSuperClass(SDecl); |
| 1090 | IDecl->setSuperClassLoc(SuperClassLoc); |
| 1091 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 1092 | } else { |
| 1093 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 1094 | } |
| 1095 | |
Douglas Gregor | 8b9fb30 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 1096 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1097 | } else { |
| 1098 | // Mark the interface as being completed, even if it was just as |
| 1099 | // @class ....; |
| 1100 | // declaration; the user cannot reopen it. |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1101 | if (!IDecl->hasDefinition()) |
| 1102 | IDecl->startDefinition(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
| 1105 | ObjCImplementationDecl* IMPDecl = |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1106 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
Argyrios Kyrtzidis | 634c563 | 2013-05-03 18:05:44 +0000 | [diff] [blame] | 1107 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1109 | if (CheckObjCDeclScope(IMPDecl)) |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1110 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1112 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1113 | if (IDecl->getImplementation()) { |
| 1114 | // FIXME: Don't leak everything! |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1115 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1116 | Diag(IDecl->getImplementation()->getLocation(), |
| 1117 | diag::note_previous_definition); |
Argyrios Kyrtzidis | df08c4b | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1118 | IMPDecl->setInvalidDecl(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1119 | } else { // add it to the list. |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1120 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 1121 | PushOnScopeChains(IMPDecl, TUScope); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1122 | // Warn on implementating deprecated class under |
| 1123 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 1124 | DiagnoseObjCImplementedDeprecations(*this, |
| 1125 | dyn_cast<NamedDecl>(IDecl), |
| 1126 | IMPDecl->getLocation(), 1); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1127 | } |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1128 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Argyrios Kyrtzidis | 644af7b | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1131 | Sema::DeclGroupPtrTy |
| 1132 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 1133 | SmallVector<Decl *, 64> DeclsInGroup; |
| 1134 | DeclsInGroup.reserve(Decls.size() + 1); |
| 1135 | |
| 1136 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 1137 | Decl *Dcl = Decls[i]; |
| 1138 | if (!Dcl) |
| 1139 | continue; |
| 1140 | if (Dcl->getDeclContext()->isFileContext()) |
| 1141 | Dcl->setTopLevelDeclInObjCContainer(); |
| 1142 | DeclsInGroup.push_back(Dcl); |
| 1143 | } |
| 1144 | |
| 1145 | DeclsInGroup.push_back(ObjCImpDecl); |
| 1146 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1147 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Argyrios Kyrtzidis | 644af7b | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1150 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 1151 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1152 | SourceLocation RBrace) { |
| 1153 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1154 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1155 | if (!IDecl) |
| 1156 | return; |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1157 | /// Check case of non-existing \@interface decl. |
| 1158 | /// (legacy objective-c \@implementation decl without an \@interface decl). |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1159 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 1160 | if (IDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1161 | IDecl->setEndOfDefinitionLoc(RBrace); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 1162 | // Add ivar's to class's DeclContext. |
| 1163 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | 2f14c4d | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 1164 | ivars[i]->setLexicalDeclContext(ImpDecl); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1165 | IDecl->makeDeclVisibleInContext(ivars[i]); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1166 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1169 | return; |
| 1170 | } |
| 1171 | // If implementation has empty ivar list, just return. |
| 1172 | if (numIvars == 0) |
| 1173 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1175 | assert(ivars && "missing @implementation ivars"); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1176 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1177 | if (ImpDecl->getSuperClass()) |
| 1178 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 1179 | for (unsigned i = 0; i < numIvars; i++) { |
| 1180 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 1181 | if (const ObjCIvarDecl *ClsIvar = |
| 1182 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 1183 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 1184 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 1185 | continue; |
| 1186 | } |
Fariborz Jahanian | 3b20f58 | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 1187 | // Check class extensions (unnamed categories) for duplicate ivars. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1188 | for (const auto *CDecl : IDecl->visible_extensions()) { |
Fariborz Jahanian | 3b20f58 | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 1189 | if (const ObjCIvarDecl *ClsExtIvar = |
| 1190 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 1191 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 1192 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 1193 | continue; |
| 1194 | } |
| 1195 | } |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1196 | // Instance ivar to Implementation's DeclContext. |
| 1197 | ImplIvar->setLexicalDeclContext(ImpDecl); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1198 | IDecl->makeDeclVisibleInContext(ImplIvar); |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1199 | ImpDecl->addDecl(ImplIvar); |
| 1200 | } |
| 1201 | return; |
| 1202 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1203 | // Check interface's Ivar list against those in the implementation. |
| 1204 | // names and types must match. |
| 1205 | // |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1206 | unsigned j = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1208 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 1209 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1210 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1211 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1212 | assert (ImplIvar && "missing implementation ivar"); |
| 1213 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 1215 | // First, make sure the types match. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1216 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1217 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1218 | << ImplIvar->getIdentifier() |
| 1219 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1220 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1221 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 1222 | ImplIvar->getBitWidthValue(Context) != |
| 1223 | ClsIvar->getBitWidthValue(Context)) { |
| 1224 | Diag(ImplIvar->getBitWidth()->getLocStart(), |
| 1225 | diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier(); |
| 1226 | Diag(ClsIvar->getBitWidth()->getLocStart(), |
| 1227 | diag::note_previous_definition); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | } |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 1229 | // Make sure the names are identical. |
| 1230 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1231 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1232 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1233 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1234 | } |
| 1235 | --numIvars; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 1238 | if (numIvars > 0) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1239 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 1240 | else if (IVI != IVE) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1241 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1244 | static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| 1245 | ObjCMethodDecl *method, |
| 1246 | bool &IncompleteImpl, |
| 1247 | unsigned DiagID, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1248 | NamedDecl *NeededFor = nullptr) { |
Fariborz Jahanian | 327126e | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 1249 | // No point warning no definition of method which is 'unavailable'. |
Douglas Gregor | 86f6cf6 | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 1250 | switch (method->getAvailability()) { |
| 1251 | case AR_Available: |
| 1252 | case AR_Deprecated: |
| 1253 | break; |
| 1254 | |
| 1255 | // Don't warn about unavailable or not-yet-introduced methods. |
| 1256 | case AR_NotYetIntroduced: |
| 1257 | case AR_Unavailable: |
Fariborz Jahanian | 327126e | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 1258 | return; |
Douglas Gregor | 86f6cf6 | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1261 | // FIXME: For now ignore 'IncompleteImpl'. |
| 1262 | // Previously we grouped all unimplemented methods under a single |
| 1263 | // warning, but some users strongly voiced that they would prefer |
| 1264 | // separate warnings. We will give that approach a try, as that |
| 1265 | // matches what we do with protocols. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1266 | { |
| 1267 | const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| 1268 | B << method; |
| 1269 | if (NeededFor) |
| 1270 | B << NeededFor; |
| 1271 | } |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Issue a note to the original declaration. |
| 1274 | SourceLocation MethodLoc = method->getLocStart(); |
| 1275 | if (MethodLoc.isValid()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1276 | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1279 | /// Determines if type B can be substituted for type A. Returns true if we can |
| 1280 | /// guarantee that anything that the user will do to an object of type A can |
| 1281 | /// also be done to an object of type B. This is trivially true if the two |
| 1282 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 1283 | /// in cases where protocols are involved. |
| 1284 | /// |
| 1285 | /// Object types in Objective-C describe the minimum requirements for an |
| 1286 | /// object, rather than providing a complete description of a type. For |
| 1287 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 1288 | /// The principle of substitutability means that we may use an instance of A |
| 1289 | /// anywhere that we may use an instance of B - it will implement all of the |
| 1290 | /// ivars of B and all of the methods of B. |
| 1291 | /// |
| 1292 | /// This substitutability is important when type checking methods, because |
| 1293 | /// the implementation may have stricter type definitions than the interface. |
| 1294 | /// The interface specifies minimum requirements, but the implementation may |
| 1295 | /// have more accurate ones. For example, a method may privately accept |
| 1296 | /// instances of B, but only publish that it accepts instances of A. Any |
| 1297 | /// object passed to it will be type checked against B, and so will implicitly |
| 1298 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 1299 | /// it is declared as returning. |
| 1300 | /// |
| 1301 | /// This is most important when considering subclassing. A method in a |
| 1302 | /// subclass must accept any object as an argument that its superclass's |
| 1303 | /// implementation accepts. It may, however, accept a more general type |
| 1304 | /// without breaking substitutability (i.e. you can still use the subclass |
| 1305 | /// anywhere that you can use the superclass, but not vice versa). The |
| 1306 | /// converse requirement applies to return types: the return type for a |
| 1307 | /// subclass method must be a valid object of the kind that the superclass |
| 1308 | /// advertises, but it may be specified more accurately. This avoids the need |
| 1309 | /// for explicit down-casting by callers. |
| 1310 | /// |
| 1311 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1312 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 1313 | const ObjCObjectPointerType *A, |
| 1314 | const ObjCObjectPointerType *B, |
| 1315 | bool rejectId) { |
| 1316 | // Reject a protocol-unqualified id. |
| 1317 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1318 | |
| 1319 | // If B is a qualified id, then A must also be a qualified id and it must |
| 1320 | // implement all of the protocols in B. It may not be a qualified class. |
| 1321 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 1322 | // stricter definition so it is not substitutable for id<A>. |
| 1323 | if (B->isObjCQualifiedIdType()) { |
| 1324 | return A->isObjCQualifiedIdType() && |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1325 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 1326 | QualType(B,0), |
| 1327 | false); |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | // id is a special type that bypasses type checking completely. We want a |
| 1332 | // warning when it is used in one place but not another. |
| 1333 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 1334 | |
| 1335 | |
| 1336 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 1337 | // if we've got this far) |
| 1338 | if (B->isObjCQualifiedIdType()) return false; |
| 1339 | */ |
| 1340 | |
| 1341 | // Now we know that A and B are (potentially-qualified) class types. The |
| 1342 | // normal rules for assignment apply. |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1343 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1346 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 1347 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 1348 | } |
| 1349 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1350 | static bool CheckMethodOverrideReturn(Sema &S, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1351 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1352 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1353 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1354 | bool IsOverridingMode, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1355 | bool Warn) { |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1356 | if (IsProtocolMethodDecl && |
| 1357 | (MethodDecl->getObjCDeclQualifier() != |
| 1358 | MethodImpl->getObjCDeclQualifier())) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1359 | if (Warn) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1360 | S.Diag(MethodImpl->getLocation(), |
| 1361 | (IsOverridingMode |
| 1362 | ? diag::warn_conflicting_overriding_ret_type_modifiers |
| 1363 | : diag::warn_conflicting_ret_type_modifiers)) |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1364 | << MethodImpl->getDeclName() |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1365 | << getTypeRange(MethodImpl->getReturnTypeSourceInfo()); |
| 1366 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
| 1367 | << getTypeRange(MethodDecl->getReturnTypeSourceInfo()); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1368 | } |
| 1369 | else |
| 1370 | return false; |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1371 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| 1374 | MethodDecl->getReturnType())) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1375 | return true; |
| 1376 | if (!Warn) |
| 1377 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1378 | |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1379 | unsigned DiagID = |
| 1380 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
| 1381 | : diag::warn_conflicting_ret_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1382 | |
| 1383 | // Mismatches between ObjC pointers go into a different warning |
| 1384 | // category, and sometimes they're even completely whitelisted. |
| 1385 | if (const ObjCObjectPointerType *ImplPtrTy = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1386 | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1387 | if (const ObjCObjectPointerType *IfacePtrTy = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1388 | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1389 | // Allow non-matching return types as long as they don't violate |
| 1390 | // the principle of substitutability. Specifically, we permit |
| 1391 | // return types that are subclasses of the declared return type, |
| 1392 | // or that are more-qualified versions of the declared type. |
| 1393 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1394 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1395 | |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1396 | DiagID = |
| 1397 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
| 1398 | : diag::warn_non_covariant_ret_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | S.Diag(MethodImpl->getLocation(), DiagID) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1403 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| 1404 | << MethodImpl->getReturnType() |
| 1405 | << getTypeRange(MethodImpl->getReturnTypeSourceInfo()); |
| 1406 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| 1407 | ? diag::note_previous_declaration |
| 1408 | : diag::note_previous_definition) |
| 1409 | << getTypeRange(MethodDecl->getReturnTypeSourceInfo()); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1410 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1413 | static bool CheckMethodOverrideParam(Sema &S, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1414 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1415 | ObjCMethodDecl *MethodDecl, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1416 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1417 | ParmVarDecl *IfaceVar, |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1418 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1419 | bool IsOverridingMode, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1420 | bool Warn) { |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1421 | if (IsProtocolMethodDecl && |
| 1422 | (ImplVar->getObjCDeclQualifier() != |
| 1423 | IfaceVar->getObjCDeclQualifier())) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1424 | if (Warn) { |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1425 | if (IsOverridingMode) |
| 1426 | S.Diag(ImplVar->getLocation(), |
| 1427 | diag::warn_conflicting_overriding_param_modifiers) |
| 1428 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 1429 | << MethodImpl->getDeclName(); |
| 1430 | else S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1431 | diag::warn_conflicting_param_modifiers) |
| 1432 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1433 | << MethodImpl->getDeclName(); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1434 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| 1435 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 1436 | } |
| 1437 | else |
| 1438 | return false; |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1441 | QualType ImplTy = ImplVar->getType(); |
| 1442 | QualType IfaceTy = IfaceVar->getType(); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1443 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1444 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1445 | return true; |
| 1446 | |
| 1447 | if (!Warn) |
| 1448 | return false; |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1449 | unsigned DiagID = |
| 1450 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
| 1451 | : diag::warn_conflicting_param_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Mismatches between ObjC pointers go into a different warning |
| 1454 | // category, and sometimes they're even completely whitelisted. |
| 1455 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 1456 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 1457 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 1458 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 1459 | // Allow non-matching argument types as long as they don't |
| 1460 | // violate the principle of substitutability. Specifically, the |
| 1461 | // implementation must accept any objects that the superclass |
| 1462 | // accepts, however it may also accept others. |
| 1463 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1464 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1465 | |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1466 | DiagID = |
| 1467 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
| 1468 | : diag::warn_non_contravariant_param_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | S.Diag(ImplVar->getLocation(), DiagID) |
| 1473 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1474 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| 1475 | S.Diag(IfaceVar->getLocation(), |
| 1476 | (IsOverridingMode ? diag::note_previous_declaration |
| 1477 | : diag::note_previous_definition)) |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1478 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1479 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1480 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1481 | |
| 1482 | /// In ARC, check whether the conventional meanings of the two methods |
| 1483 | /// match. If they don't, it's a hard error. |
| 1484 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 1485 | ObjCMethodDecl *decl) { |
| 1486 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 1487 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 1488 | if (implFamily == declFamily) return false; |
| 1489 | |
| 1490 | // Since conventions are sorted by selector, the only possibility is |
| 1491 | // that the types differ enough to cause one selector or the other |
| 1492 | // to fall out of the family. |
| 1493 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 1494 | |
| 1495 | // No further diagnostics required on invalid declarations. |
| 1496 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 1497 | |
| 1498 | const ObjCMethodDecl *unmatched = impl; |
| 1499 | ObjCMethodFamily family = declFamily; |
| 1500 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 1501 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 1502 | if (declFamily == OMF_None) { |
| 1503 | unmatched = decl; |
| 1504 | family = implFamily; |
| 1505 | errorID = diag::err_arc_gained_method_convention; |
| 1506 | noteID = diag::note_arc_gained_method_convention; |
| 1507 | } |
| 1508 | |
| 1509 | // Indexes into a %select clause in the diagnostic. |
| 1510 | enum FamilySelector { |
| 1511 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 1512 | }; |
| 1513 | FamilySelector familySelector = FamilySelector(); |
| 1514 | |
| 1515 | switch (family) { |
| 1516 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 1517 | case OMF_retain: |
| 1518 | case OMF_release: |
| 1519 | case OMF_autorelease: |
| 1520 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 1521 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1522 | case OMF_retainCount: |
| 1523 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1524 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1525 | // Mismatches for these methods don't change ownership |
| 1526 | // conventions, so we don't care. |
| 1527 | return false; |
| 1528 | |
| 1529 | case OMF_init: familySelector = F_init; break; |
| 1530 | case OMF_alloc: familySelector = F_alloc; break; |
| 1531 | case OMF_copy: familySelector = F_copy; break; |
| 1532 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 1533 | case OMF_new: familySelector = F_new; break; |
| 1534 | } |
| 1535 | |
| 1536 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 1537 | ReasonSelector reasonSelector; |
| 1538 | |
| 1539 | // The only reason these methods don't fall within their families is |
| 1540 | // due to unusual result types. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1541 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1542 | reasonSelector = R_UnrelatedReturn; |
| 1543 | } else { |
| 1544 | reasonSelector = R_NonObjectReturn; |
| 1545 | } |
| 1546 | |
Joerg Sonnenberger | 7348454 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 1547 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 1548 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1549 | |
| 1550 | return true; |
| 1551 | } |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1552 | |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 1553 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1554 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1555 | bool IsProtocolMethodDecl) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1556 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1557 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 1558 | return; |
| 1559 | |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1560 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1561 | IsProtocolMethodDecl, false, |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1562 | true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 1564 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 1565 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 1566 | EF = MethodDecl->param_end(); |
| 1567 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1568 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1569 | IsProtocolMethodDecl, false, true); |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1570 | } |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1571 | |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1572 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1573 | Diag(ImpMethodDecl->getLocation(), |
| 1574 | diag::warn_conflicting_variadic); |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1575 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1576 | } |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1579 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 1580 | ObjCMethodDecl *Overridden, |
| 1581 | bool IsProtocolMethodDecl) { |
| 1582 | |
| 1583 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 1584 | IsProtocolMethodDecl, true, |
| 1585 | true); |
| 1586 | |
| 1587 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 1588 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 1589 | EF = Overridden->param_end(); |
| 1590 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1591 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 1592 | IsProtocolMethodDecl, true, true); |
| 1593 | } |
| 1594 | |
| 1595 | if (Method->isVariadic() != Overridden->isVariadic()) { |
| 1596 | Diag(Method->getLocation(), |
| 1597 | diag::warn_conflicting_overriding_variadic); |
| 1598 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 1599 | } |
| 1600 | } |
| 1601 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1602 | /// WarnExactTypedMethods - This routine issues a warning if method |
| 1603 | /// implementation declaration matches exactly that of its declaration. |
| 1604 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 1605 | ObjCMethodDecl *MethodDecl, |
| 1606 | bool IsProtocolMethodDecl) { |
| 1607 | // don't issue warning when protocol method is optional because primary |
| 1608 | // class is not required to implement it and it is safe for protocol |
| 1609 | // to implement it. |
| 1610 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 1611 | return; |
| 1612 | // don't issue warning when primary class's method is |
| 1613 | // depecated/unavailable. |
| 1614 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 1615 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 1616 | return; |
| 1617 | |
| 1618 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 1619 | IsProtocolMethodDecl, false, false); |
| 1620 | if (match) |
| 1621 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 1622 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 1623 | EF = MethodDecl->param_end(); |
| 1624 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1625 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
| 1626 | *IM, *IF, |
| 1627 | IsProtocolMethodDecl, false, false); |
| 1628 | if (!match) |
| 1629 | break; |
| 1630 | } |
| 1631 | if (match) |
| 1632 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
David Chisnall | 7ca13ef | 2011-08-08 17:32:19 +0000 | [diff] [blame] | 1633 | if (match) |
| 1634 | match = !(MethodDecl->isClassMethod() && |
| 1635 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1636 | |
| 1637 | if (match) { |
| 1638 | Diag(ImpMethodDecl->getLocation(), |
| 1639 | diag::warn_category_method_impl_match); |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 1640 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 1641 | << MethodDecl->getDeclName(); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1645 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 1646 | /// improve the efficiency of selector lookups and type checking by associating |
| 1647 | /// with each protocol / interface / category the flattened instance tables. If |
| 1648 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 1649 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1650 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1651 | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
| 1652 | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
| 1653 | |
| 1654 | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| 1655 | ProtocolNameSet &PNS) { |
| 1656 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 1657 | PNS.insert(PDecl->getIdentifier()); |
| 1658 | for (const auto *PI : PDecl->protocols()) |
| 1659 | findProtocolsWithExplicitImpls(PI, PNS); |
| 1660 | } |
| 1661 | |
| 1662 | /// Recursively populates a set with all conformed protocols in a class |
| 1663 | /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
| 1664 | /// attribute. |
| 1665 | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| 1666 | ProtocolNameSet &PNS) { |
| 1667 | if (!Super) |
| 1668 | return; |
| 1669 | |
| 1670 | for (const auto *I : Super->all_referenced_protocols()) |
| 1671 | findProtocolsWithExplicitImpls(I, PNS); |
| 1672 | |
| 1673 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
| 1674 | } |
| 1675 | |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 1676 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1677 | /// Declared in protocol, and those referenced by it. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1678 | static void CheckProtocolMethodDefs(Sema &S, |
| 1679 | SourceLocation ImpLoc, |
| 1680 | ObjCProtocolDecl *PDecl, |
| 1681 | bool& IncompleteImpl, |
| 1682 | const Sema::SelectorSet &InsMap, |
| 1683 | const Sema::SelectorSet &ClsMap, |
| 1684 | ObjCContainerDecl *CDecl, |
| 1685 | LazyProtocolNameSet &ProtocolsExplictImpl) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1686 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| 1687 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
| 1688 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
Fariborz Jahanian | f283859 | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 1689 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 1690 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1691 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1692 | ObjCInterfaceDecl *NSIDecl = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1693 | |
| 1694 | // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
| 1695 | // then we should check if any class in the super class hierarchy also |
| 1696 | // conforms to this protocol, either directly or via protocol inheritance. |
| 1697 | // If so, we can skip checking this protocol completely because we |
| 1698 | // know that a parent class already satisfies this protocol. |
| 1699 | // |
| 1700 | // Note: we could generalize this logic for all protocols, and merely |
| 1701 | // add the limit on looking at the super class chain for just |
| 1702 | // specially marked protocols. This may be a good optimization. This |
| 1703 | // change is restricted to 'objc_protocol_requires_explicit_implementation' |
| 1704 | // protocols for now for controlled evaluation. |
| 1705 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
| 1706 | if (!ProtocolsExplictImpl) { |
| 1707 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| 1708 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| 1709 | } |
| 1710 | if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| 1711 | ProtocolsExplictImpl->end()) |
| 1712 | return; |
| 1713 | |
| 1714 | // If no super class conforms to the protocol, we should not search |
| 1715 | // for methods in the super class to implicitly satisfy the protocol. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1716 | Super = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | // check to see if class implements forwardInvocation method and objects |
| 1721 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1722 | // from one object to another. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | // Under such conditions, which means that every method possible is |
| 1724 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1725 | // found" warnings. |
| 1726 | // FIXME: Use a general GetUnarySelector method for this. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1727 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| 1728 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1729 | if (InsMap.count(fISelector)) |
| 1730 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 1731 | // need be implemented in the implementation. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1732 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1733 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | |
Fariborz Jahanian | 32b94be | 2013-01-07 19:21:03 +0000 | [diff] [blame] | 1735 | // If this is a forward protocol declaration, get its definition. |
| 1736 | if (!PDecl->isThisDeclarationADefinition() && |
| 1737 | PDecl->getDefinition()) |
| 1738 | PDecl = PDecl->getDefinition(); |
| 1739 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1740 | // If a method lookup fails locally we still need to look and see if |
| 1741 | // the method was implemented by a base class or an inherited |
| 1742 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 1743 | // and otherwise would terminate in a warning. |
| 1744 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1745 | // check unimplemented instance methods. |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1746 | if (!NSIDecl) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1747 | for (auto *method : PDecl->instance_methods()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1749 | !method->isPropertyAccessor() && |
| 1750 | !InsMap.count(method->getSelector()) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1751 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 1752 | true /* instance */, |
| 1753 | false /* shallowCategory */, |
| 1754 | true /* followsSuper */, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1755 | nullptr /* category */))) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1756 | // If a method is not implemented in the category implementation but |
| 1757 | // has been declared in its primary class, superclass, |
| 1758 | // or in one of their protocols, no need to issue the warning. |
| 1759 | // This is because method will be implemented in the primary class |
| 1760 | // or one of its super class implementation. |
| 1761 | |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1762 | // Ugly, but necessary. Method declared in protcol might have |
| 1763 | // have been synthesized due to a property declared in the class which |
| 1764 | // uses the protocol. |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1765 | if (ObjCMethodDecl *MethodInClass = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1766 | IDecl->lookupMethod(method->getSelector(), |
| 1767 | true /* instance */, |
| 1768 | true /* shallowCategoryLookup */, |
| 1769 | false /* followSuper */)) |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1770 | if (C || MethodInClass->isPropertyAccessor()) |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1771 | continue; |
| 1772 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1773 | if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1774 | != DiagnosticsEngine::Ignored) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1775 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
| 1776 | PDecl); |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1777 | } |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1780 | // check unimplemented class methods |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1781 | for (auto *method : PDecl->class_methods()) { |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1782 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 1783 | !ClsMap.count(method->getSelector()) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1784 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 1785 | false /* class method */, |
| 1786 | false /* shallowCategoryLookup */, |
| 1787 | true /* followSuper */, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1788 | nullptr /* category */))) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1789 | // See above comment for instance method lookups. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1790 | if (C && IDecl->lookupMethod(method->getSelector(), |
| 1791 | false /* class */, |
| 1792 | true /* shallowCategoryLookup */, |
| 1793 | false /* followSuper */)) |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1794 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1795 | |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1796 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1797 | if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) != |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1798 | DiagnosticsEngine::Ignored) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1799 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl); |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1800 | } |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1801 | } |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1802 | } |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 1803 | // Check on this protocols's referenced protocols, recursively. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1804 | for (auto *PI : PDecl->protocols()) |
| 1805 | CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap, |
| 1806 | CDecl, ProtocolsExplictImpl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Fariborz Jahanian | 1e159bc | 2011-07-16 00:08:33 +0000 | [diff] [blame] | 1809 | /// MatchAllMethodDeclarations - Check methods declared in interface |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1810 | /// or protocol against those declared in their implementations. |
| 1811 | /// |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1812 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 1813 | const SelectorSet &ClsMap, |
| 1814 | SelectorSet &InsMapSeen, |
| 1815 | SelectorSet &ClsMapSeen, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1816 | ObjCImplDecl* IMPDecl, |
| 1817 | ObjCContainerDecl* CDecl, |
| 1818 | bool &IncompleteImpl, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1819 | bool ImmediateClass, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1820 | bool WarnCategoryMethodImpl) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1821 | // Check and see if instance methods in class interface have been |
| 1822 | // implemented in the implementation class. If so, their types match. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1823 | for (auto *I : CDecl->instance_methods()) { |
| 1824 | if (!InsMapSeen.insert(I->getSelector())) |
Benjamin Kramer | 7dcff5b | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 1825 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1826 | if (!I->isPropertyAccessor() && |
| 1827 | !InsMap.count(I->getSelector())) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1828 | if (ImmediateClass) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1829 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1830 | diag::warn_undef_method_impl); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1831 | continue; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1832 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | ObjCMethodDecl *ImpMethodDecl = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1834 | IMPDecl->getInstanceMethod(I->getSelector()); |
| 1835 | assert(CDecl->getInstanceMethod(I->getSelector()) && |
Argyrios Kyrtzidis | 2334f3a | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 1836 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1837 | // ImpMethodDecl may be null as in a @dynamic property. |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1838 | if (ImpMethodDecl) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1839 | if (!WarnCategoryMethodImpl) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1840 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1841 | isa<ObjCProtocolDecl>(CDecl)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1842 | else if (!I->isPropertyAccessor()) |
| 1843 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1844 | } |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1845 | } |
| 1846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1848 | // Check and see if class methods in class interface have been |
| 1849 | // implemented in the implementation class. If so, their types match. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1850 | for (auto *I : CDecl->class_methods()) { |
| 1851 | if (!ClsMapSeen.insert(I->getSelector())) |
Benjamin Kramer | 7dcff5b | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 1852 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1853 | if (!ClsMap.count(I->getSelector())) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1854 | if (ImmediateClass) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1855 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1856 | diag::warn_undef_method_impl); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1857 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1858 | ObjCMethodDecl *ImpMethodDecl = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1859 | IMPDecl->getClassMethod(I->getSelector()); |
| 1860 | assert(CDecl->getClassMethod(I->getSelector()) && |
Argyrios Kyrtzidis | 2334f3a | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 1861 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1862 | if (!WarnCategoryMethodImpl) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1863 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1864 | isa<ObjCProtocolDecl>(CDecl)); |
| 1865 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1866 | WarnExactTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1867 | isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1868 | } |
| 1869 | } |
Fariborz Jahanian | f54e3ae | 2010-10-08 22:59:25 +0000 | [diff] [blame] | 1870 | |
Fariborz Jahanian | 41594c5 | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 1871 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 1872 | // Also, check for methods declared in protocols inherited by |
| 1873 | // this protocol. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1874 | for (auto *PI : PD->protocols()) |
Fariborz Jahanian | 41594c5 | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 1875 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1876 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 41594c5 | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 1877 | WarnCategoryMethodImpl); |
| 1878 | } |
| 1879 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1880 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1881 | // when checking that methods in implementation match their declaration, |
| 1882 | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
| 1883 | // extension; as well as those in categories. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1884 | if (!WarnCategoryMethodImpl) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1885 | for (auto *Cat : I->visible_categories()) |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1886 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1887 | IMPDecl, Cat, IncompleteImpl, false, |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1888 | WarnCategoryMethodImpl); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1889 | } else { |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1890 | // Also methods in class extensions need be looked at next. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1891 | for (auto *Ext : I->visible_extensions()) |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1892 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1893 | IMPDecl, Ext, IncompleteImpl, false, |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1894 | WarnCategoryMethodImpl); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1897 | // Check for any implementation of a methods declared in protocol. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1898 | for (auto *PI : I->all_referenced_protocols()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1900 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1901 | WarnCategoryMethodImpl); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1902 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1903 | // FIXME. For now, we are not checking for extact match of methods |
| 1904 | // in category implementation and its primary class's super class. |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1905 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1906 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | IMPDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1908 | I->getSuperClass(), IncompleteImpl, false); |
| 1909 | } |
| 1910 | } |
| 1911 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1912 | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
| 1913 | /// category matches with those implemented in its primary class and |
| 1914 | /// warns each time an exact match is found. |
| 1915 | void Sema::CheckCategoryVsClassMethodMatches( |
| 1916 | ObjCCategoryImplDecl *CatIMPDecl) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1917 | // Get category's primary class. |
| 1918 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 1919 | if (!CatDecl) |
| 1920 | return; |
| 1921 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 1922 | if (!IDecl) |
| 1923 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1924 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
| 1925 | SelectorSet InsMap, ClsMap; |
| 1926 | |
| 1927 | for (const auto *I : CatIMPDecl->instance_methods()) { |
| 1928 | Selector Sel = I->getSelector(); |
| 1929 | // When checking for methods implemented in the category, skip over |
| 1930 | // those declared in category class's super class. This is because |
| 1931 | // the super class must implement the method. |
| 1932 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)) |
| 1933 | continue; |
| 1934 | InsMap.insert(Sel); |
| 1935 | } |
| 1936 | |
| 1937 | for (const auto *I : CatIMPDecl->class_methods()) { |
| 1938 | Selector Sel = I->getSelector(); |
| 1939 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)) |
| 1940 | continue; |
| 1941 | ClsMap.insert(Sel); |
| 1942 | } |
| 1943 | if (InsMap.empty() && ClsMap.empty()) |
| 1944 | return; |
| 1945 | |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1946 | SelectorSet InsMapSeen, ClsMapSeen; |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1947 | bool IncompleteImpl = false; |
| 1948 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1949 | CatIMPDecl, IDecl, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1950 | IncompleteImpl, false, |
| 1951 | true /*WarnCategoryMethodImpl*/); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1952 | } |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1953 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1954 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | ObjCContainerDecl* CDecl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1956 | bool IncompleteImpl) { |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1957 | SelectorSet InsMap; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1958 | // Check and see if instance methods in class interface have been |
| 1959 | // implemented in the implementation class. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1960 | for (const auto *I : IMPDecl->instance_methods()) |
| 1961 | InsMap.insert(I->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 1963 | // Check and see if properties declared in the interface have either 1) |
| 1964 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 1965 | // of the property in the @implementation. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1966 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1967 | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
| 1968 | LangOpts.ObjCRuntime.isNonFragile() && |
| 1969 | !IDecl->isObjCRequiresPropertyDefs(); |
| 1970 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
| 1971 | } |
| 1972 | |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1973 | SelectorSet ClsMap; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1974 | for (const auto *I : IMPDecl->class_methods()) |
| 1975 | ClsMap.insert(I->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1977 | // Check for type conflict of methods declared in a class/protocol and |
| 1978 | // its implementation; if any. |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1979 | SelectorSet InsMapSeen, ClsMapSeen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1981 | IMPDecl, CDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1982 | IncompleteImpl, true); |
Fariborz Jahanian | 7413307 | 2011-08-03 18:21:12 +0000 | [diff] [blame] | 1983 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1984 | // check all methods implemented in category against those declared |
| 1985 | // in its primary class. |
| 1986 | if (ObjCCategoryImplDecl *CatDecl = |
| 1987 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 1988 | CheckCategoryVsClassMethodMatches(CatDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1990 | // Check the protocol list for unimplemented methods in the @implementation |
| 1991 | // class. |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1992 | // Check and see if class methods in class interface have been |
| 1993 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1995 | LazyProtocolNameSet ExplicitImplProtocols; |
| 1996 | |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1997 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1998 | for (auto *PI : I->all_referenced_protocols()) |
| 1999 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl, |
| 2000 | InsMap, ClsMap, I, ExplicitImplProtocols); |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2001 | // Check class extensions (unnamed categories) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2002 | for (auto *Ext : I->visible_extensions()) |
| 2003 | ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl); |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2004 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 2005 | // For extended class, unimplemented methods in its protocols will |
| 2006 | // be reported in the primary class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2007 | if (!C->IsClassExtension()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2008 | for (auto *P : C->protocols()) |
| 2009 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P, |
| 2010 | IncompleteImpl, InsMap, ClsMap, CDecl, |
| 2011 | ExplicitImplProtocols); |
| 2012 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
| 2013 | /* SynthesizeProperties */ false); |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 2014 | } |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2015 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2016 | llvm_unreachable("invalid ObjCContainerDecl type."); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | /// ActOnForwardClassDeclaration - |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 2020 | Sema::DeclGroupPtrTy |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2021 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 2022 | IdentifierInfo **IdentList, |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 2023 | SourceLocation *IdentLocs, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 2024 | unsigned NumElts) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 2025 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2026 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2027 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2028 | NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2029 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 2030 | LookupOrdinaryName, ForRedeclaration); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2031 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | c733388 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 2032 | // GCC apparently allows the following idiom: |
| 2033 | // |
| 2034 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 2035 | // @class XCElementToggler; |
| 2036 | // |
Fariborz Jahanian | e42670b | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 2037 | // Here we have chosen to ignore the forward class declaration |
| 2038 | // with a warning. Since this is the implied behavior. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2039 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2040 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2041 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2042 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2043 | } else { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2044 | // a forward class declaration matching a typedef name of a class refers |
Fariborz Jahanian | e42670b | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 2045 | // to the underlying class. Just ignore the forward class with a warning |
| 2046 | // as this will force the intended behavior which is to lookup the typedef |
| 2047 | // name. |
| 2048 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
| 2049 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i]; |
| 2050 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2051 | continue; |
| 2052 | } |
Fariborz Jahanian | cae27c5 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 2053 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2054 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2055 | |
| 2056 | // Create a declaration to describe this forward declaration. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2057 | ObjCInterfaceDecl *PrevIDecl |
| 2058 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | e7e8fca | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 2059 | |
| 2060 | IdentifierInfo *ClassName = IdentList[i]; |
| 2061 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 2062 | // A previous decl with a different name is because of |
| 2063 | // @compatibility_alias, for example: |
| 2064 | // \code |
| 2065 | // @class NewImage; |
| 2066 | // @compatibility_alias OldImage NewImage; |
| 2067 | // \endcode |
| 2068 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 2069 | // |
| 2070 | // In such a case use the real declaration name, instead of the alias one, |
| 2071 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 2072 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 2073 | // has been aliased. |
| 2074 | ClassName = PrevIDecl->getIdentifier(); |
| 2075 | } |
| 2076 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2077 | ObjCInterfaceDecl *IDecl |
| 2078 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Argyrios Kyrtzidis | e7e8fca | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 2079 | ClassName, PrevIDecl, IdentLocs[i]); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2080 | IDecl->setAtEndRange(IdentLocs[i]); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2082 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 2083 | CheckObjCDeclScope(IDecl); |
| 2084 | DeclsInGroup.push_back(IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2085 | } |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2086 | |
| 2087 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2090 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 2091 | Sema::MethodMatchStrategy strategy, |
| 2092 | const Type *left, const Type *right); |
| 2093 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2094 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 2095 | QualType leftQT, QualType rightQT) { |
| 2096 | const Type *left = |
| 2097 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 2098 | const Type *right = |
| 2099 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 2100 | |
| 2101 | if (left == right) return true; |
| 2102 | |
| 2103 | // If we're doing a strict match, the types have to match exactly. |
| 2104 | if (strategy == Sema::MMS_strict) return false; |
| 2105 | |
| 2106 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 2107 | |
| 2108 | // Otherwise, use this absurdly complicated algorithm to try to |
| 2109 | // validate the basic, low-level compatibility of the two types. |
| 2110 | |
| 2111 | // As a minimum, require the sizes and alignments to match. |
| 2112 | if (Context.getTypeInfo(left) != Context.getTypeInfo(right)) |
| 2113 | return false; |
| 2114 | |
| 2115 | // Consider all the kinds of non-dependent canonical types: |
| 2116 | // - functions and arrays aren't possible as return and parameter types |
| 2117 | |
| 2118 | // - vector types of equal size can be arbitrarily mixed |
| 2119 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 2120 | if (isa<VectorType>(right)) return false; |
| 2121 | |
| 2122 | // - references should only match references of identical type |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2123 | // - structs, unions, and Objective-C objects must match more-or-less |
| 2124 | // exactly |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2125 | // - everything else should be a scalar |
| 2126 | if (!left->isScalarType() || !right->isScalarType()) |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2127 | return tryMatchRecordTypes(Context, strategy, left, right); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2128 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2129 | // Make scalars agree in kind, except count bools as chars, and group |
| 2130 | // all non-member pointers together. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2131 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 2132 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 2133 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 2134 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2135 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 2136 | leftSK = Type::STK_ObjCObjectPointer; |
| 2137 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 2138 | rightSK = Type::STK_ObjCObjectPointer; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Note that data member pointers and function member pointers don't |
| 2141 | // intermix because of the size differences. |
| 2142 | |
| 2143 | return (leftSK == rightSK); |
| 2144 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2145 | |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2146 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 2147 | Sema::MethodMatchStrategy strategy, |
| 2148 | const Type *lt, const Type *rt) { |
| 2149 | assert(lt && rt && lt != rt); |
| 2150 | |
| 2151 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 2152 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 2153 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 2154 | |
| 2155 | // Require union-hood to match. |
| 2156 | if (left->isUnion() != right->isUnion()) return false; |
| 2157 | |
| 2158 | // Require an exact match if either is non-POD. |
| 2159 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 2160 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 2161 | return false; |
| 2162 | |
| 2163 | // Require size and alignment to match. |
| 2164 | if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false; |
| 2165 | |
| 2166 | // Require fields to match. |
| 2167 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 2168 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 2169 | for (; li != le && ri != re; ++li, ++ri) { |
| 2170 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 2171 | return false; |
| 2172 | } |
| 2173 | return (li == le && ri == re); |
| 2174 | } |
| 2175 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2176 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 2177 | /// returns true, or false, accordingly. |
| 2178 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2179 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 2180 | const ObjCMethodDecl *right, |
| 2181 | MethodMatchStrategy strategy) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2182 | if (!matchTypes(Context, strategy, left->getReturnType(), |
| 2183 | right->getReturnType())) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2184 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | 7666b03 | 2013-02-07 19:13:24 +0000 | [diff] [blame] | 2186 | // If either is hidden, it is not considered to match. |
| 2187 | if (left->isHidden() || right->isHidden()) |
| 2188 | return false; |
| 2189 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2190 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2191 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 2192 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 2193 | left->hasAttr<NSConsumesSelfAttr>() |
| 2194 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 2195 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2197 | ObjCMethodDecl::param_const_iterator |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2198 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 2199 | re = right->param_end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2201 | for (; li != le && ri != re; ++li, ++ri) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2202 | assert(ri != right->param_end() && "Param mismatch"); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2203 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2204 | |
| 2205 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 2206 | return false; |
| 2207 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2208 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2209 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 2210 | return false; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2211 | } |
| 2212 | return true; |
| 2213 | } |
| 2214 | |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2215 | void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) { |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2216 | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
| 2217 | // inside categories. |
Argyrios Kyrtzidis | ab3d509 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 2218 | if (ObjCCategoryDecl * |
| 2219 | CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
| 2220 | if (!CD->IsClassExtension() && List->getBits() < 2) |
| 2221 | List->setBits(List->getBits()+1); |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2223 | // If the list is empty, make it a singleton list. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2224 | if (List->Method == nullptr) { |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2225 | List->Method = Method; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2226 | List->setNext(nullptr); |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2227 | return; |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | // We've seen a method with this name, see if we have already seen this type |
| 2231 | // signature. |
| 2232 | ObjCMethodList *Previous = List; |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2233 | for (; List; Previous = List, List = List->getNext()) { |
Douglas Gregor | fc46be9 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 2234 | // If we are building a module, keep all of the methods. |
| 2235 | if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) |
| 2236 | continue; |
| 2237 | |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2238 | if (!MatchTwoMethodDeclarations(Method, List->Method)) |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2239 | continue; |
| 2240 | |
| 2241 | ObjCMethodDecl *PrevObjCMethod = List->Method; |
| 2242 | |
| 2243 | // Propagate the 'defined' bit. |
| 2244 | if (Method->isDefined()) |
| 2245 | PrevObjCMethod->setDefined(true); |
| 2246 | |
| 2247 | // If a method is deprecated, push it in the global pool. |
| 2248 | // This is used for better diagnostics. |
| 2249 | if (Method->isDeprecated()) { |
| 2250 | if (!PrevObjCMethod->isDeprecated()) |
| 2251 | List->Method = Method; |
| 2252 | } |
| 2253 | // If new method is unavailable, push it into global pool |
| 2254 | // unless previous one is deprecated. |
| 2255 | if (Method->isUnavailable()) { |
| 2256 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
| 2257 | List->Method = Method; |
| 2258 | } |
| 2259 | |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2260 | return; |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | // We have a new signature for an existing method - add it. |
| 2264 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2265 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2266 | Previous->setNext(new (Mem) ObjCMethodList(Method, nullptr)); |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2269 | /// \brief Read the contents of the method pool for a given selector from |
| 2270 | /// external storage. |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2271 | void Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2272 | assert(ExternalSource && "We need an external AST source"); |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2273 | ExternalSource->ReadMethodPool(Sel); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2276 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2277 | bool instance) { |
Argyrios Kyrtzidis | 9a0b6b4 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 2278 | // Ignore methods of invalid containers. |
| 2279 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2280 | return; |
Argyrios Kyrtzidis | 9a0b6b4 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 2281 | |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2282 | if (ExternalSource) |
| 2283 | ReadMethodPool(Method->getSelector()); |
| 2284 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2285 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2286 | if (Pos == MethodPool.end()) |
| 2287 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 2288 | GlobalMethods())).first; |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2289 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2290 | Method->setDefined(impl); |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2291 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2292 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2293 | addMethodToGlobalList(&Entry, Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2296 | /// Determines if this is an "acceptable" loose mismatch in the global |
| 2297 | /// method pool. This exists mostly as a hack to get around certain |
| 2298 | /// global mismatches which we can't afford to make warnings / errors. |
| 2299 | /// Really, what we want is a way to take a method out of the global |
| 2300 | /// method pool. |
| 2301 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 2302 | ObjCMethodDecl *other) { |
| 2303 | if (!chosen->isInstanceMethod()) |
| 2304 | return false; |
| 2305 | |
| 2306 | Selector sel = chosen->getSelector(); |
| 2307 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 2308 | return false; |
| 2309 | |
| 2310 | // Don't complain about mismatches for -length if the method we |
| 2311 | // chose has an integral result type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2312 | return (chosen->getReturnType()->isIntegerType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2315 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2316 | bool receiverIdOrClass, |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2317 | bool warn, bool instance) { |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2318 | if (ExternalSource) |
| 2319 | ReadMethodPool(Sel); |
| 2320 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2321 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2322 | if (Pos == MethodPool.end()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2323 | return nullptr; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2325 | // Gather the non-hidden methods. |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2326 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Robert Wilhelm | e7205c0 | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 2327 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2328 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2329 | if (M->Method && !M->Method->isHidden()) { |
| 2330 | // If we're not supposed to warn about mismatches, we're done. |
| 2331 | if (!warn) |
| 2332 | return M->Method; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2334 | Methods.push_back(M->Method); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2335 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2336 | } |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2337 | |
| 2338 | // If there aren't any visible methods, we're done. |
| 2339 | // FIXME: Recover if there are any known-but-hidden methods? |
| 2340 | if (Methods.empty()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2341 | return nullptr; |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (Methods.size() == 1) |
| 2344 | return Methods[0]; |
| 2345 | |
| 2346 | // We found multiple methods, so we may have to complain. |
| 2347 | bool issueDiagnostic = false, issueError = false; |
| 2348 | |
| 2349 | // We support a warning which complains about *any* difference in |
| 2350 | // method signature. |
| 2351 | bool strictSelectorMatch = |
| 2352 | (receiverIdOrClass && warn && |
| 2353 | (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl, |
| 2354 | R.getBegin()) |
| 2355 | != DiagnosticsEngine::Ignored)); |
| 2356 | if (strictSelectorMatch) { |
| 2357 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 2358 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 2359 | issueDiagnostic = true; |
| 2360 | break; |
| 2361 | } |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | // If we didn't see any strict differences, we won't see any loose |
| 2366 | // differences. In ARC, however, we also need to check for loose |
| 2367 | // mismatches, because most of them are errors. |
| 2368 | if (!strictSelectorMatch || |
| 2369 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 2370 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 2371 | // This checks if the methods differ in type mismatch. |
| 2372 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 2373 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 2374 | issueDiagnostic = true; |
| 2375 | if (getLangOpts().ObjCAutoRefCount) |
| 2376 | issueError = true; |
| 2377 | break; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | if (issueDiagnostic) { |
| 2382 | if (issueError) |
| 2383 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 2384 | else if (strictSelectorMatch) |
| 2385 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 2386 | else |
| 2387 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
| 2388 | |
| 2389 | Diag(Methods[0]->getLocStart(), |
| 2390 | issueError ? diag::note_possibility : diag::note_using) |
| 2391 | << Methods[0]->getSourceRange(); |
| 2392 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 2393 | Diag(Methods[I]->getLocStart(), diag::note_also_found) |
| 2394 | << Methods[I]->getSourceRange(); |
| 2395 | } |
| 2396 | } |
| 2397 | return Methods[0]; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2400 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2401 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 2402 | if (Pos == MethodPool.end()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2403 | return nullptr; |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2404 | |
| 2405 | GlobalMethods &Methods = Pos->second; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2406 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 2407 | Method = Method->getNext()) |
| 2408 | if (Method->Method && Method->Method->isDefined()) |
| 2409 | return Method->Method; |
| 2410 | |
| 2411 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 2412 | Method = Method->getNext()) |
| 2413 | if (Method->Method && Method->Method->isDefined()) |
| 2414 | return Method->Method; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2415 | return nullptr; |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Fariborz Jahanian | f98c688 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 2418 | static void |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2419 | HelperSelectorsForTypoCorrection( |
| 2420 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 2421 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 2422 | const unsigned MaxEditDistance = 1; |
| 2423 | unsigned BestEditDistance = MaxEditDistance + 1; |
Richard Trieu | 4fe9644 | 2013-06-06 02:22:29 +0000 | [diff] [blame] | 2424 | std::string MethodName = Method->getSelector().getAsString(); |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2425 | |
| 2426 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 2427 | if (MinPossibleEditDistance > 0 && |
| 2428 | Typo.size() / MinPossibleEditDistance < 1) |
| 2429 | return; |
| 2430 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 2431 | if (EditDistance > MaxEditDistance) |
| 2432 | return; |
| 2433 | if (EditDistance == BestEditDistance) |
| 2434 | BestMethod.push_back(Method); |
| 2435 | else if (EditDistance < BestEditDistance) { |
| 2436 | BestMethod.clear(); |
| 2437 | BestMethod.push_back(Method); |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
Fariborz Jahanian | d395e34 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 2441 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 2442 | QualType ObjectType) { |
| 2443 | if (ObjectType.isNull()) |
| 2444 | return true; |
| 2445 | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
| 2446 | return true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2447 | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != |
| 2448 | nullptr; |
Fariborz Jahanian | d395e34 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2451 | const ObjCMethodDecl * |
Fariborz Jahanian | d395e34 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 2452 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 2453 | QualType ObjectType) { |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2454 | unsigned NumArgs = Sel.getNumArgs(); |
| 2455 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2456 | bool ObjectIsId = true, ObjectIsClass = true; |
| 2457 | if (ObjectType.isNull()) |
| 2458 | ObjectIsId = ObjectIsClass = false; |
| 2459 | else if (!ObjectType->isObjCObjectPointerType()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2460 | return nullptr; |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2461 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 2462 | ObjectType->getAsObjCInterfacePointerType()) { |
| 2463 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 2464 | ObjectIsId = ObjectIsClass = false; |
| 2465 | } |
| 2466 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 2467 | ObjectIsClass = false; |
| 2468 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 2469 | ObjectIsId = false; |
| 2470 | else |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2471 | return nullptr; |
| 2472 | |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2473 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 2474 | e = MethodPool.end(); b != e; b++) { |
| 2475 | // instance methods |
| 2476 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
| 2477 | if (M->Method && |
Fariborz Jahanian | cd9c9b5 | 2013-06-18 17:10:58 +0000 | [diff] [blame] | 2478 | (M->Method->getSelector().getNumArgs() == NumArgs) && |
| 2479 | (M->Method->getSelector() != Sel)) { |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2480 | if (ObjectIsId) |
| 2481 | Methods.push_back(M->Method); |
| 2482 | else if (!ObjectIsClass && |
| 2483 | HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType)) |
| 2484 | Methods.push_back(M->Method); |
| 2485 | } |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2486 | // class methods |
| 2487 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
| 2488 | if (M->Method && |
Fariborz Jahanian | cd9c9b5 | 2013-06-18 17:10:58 +0000 | [diff] [blame] | 2489 | (M->Method->getSelector().getNumArgs() == NumArgs) && |
| 2490 | (M->Method->getSelector() != Sel)) { |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2491 | if (ObjectIsClass) |
| 2492 | Methods.push_back(M->Method); |
| 2493 | else if (!ObjectIsId && |
| 2494 | HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType)) |
| 2495 | Methods.push_back(M->Method); |
| 2496 | } |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 2500 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 2501 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 2502 | Sel.getAsString(), Methods[i]); |
| 2503 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2504 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr; |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Fariborz Jahanian | f98c688 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 2507 | /// DiagnoseDuplicateIvars - |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2508 | /// Check for duplicate ivars in the entire class at the start of |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2509 | /// \@implementation. This becomes necesssary because class extension can |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2510 | /// add ivars to a class in random order which will not be known until |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2511 | /// class's \@implementation is seen. |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2512 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 2513 | ObjCInterfaceDecl *SID) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2514 | for (auto *Ivar : ID->ivars()) { |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2515 | if (Ivar->isInvalidDecl()) |
| 2516 | continue; |
| 2517 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 2518 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 2519 | if (prevIvar) { |
| 2520 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 2521 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 2522 | Ivar->setInvalidDecl(); |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2528 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 2529 | switch (CurContext->getDeclKind()) { |
| 2530 | case Decl::ObjCInterface: |
| 2531 | return Sema::OCK_Interface; |
| 2532 | case Decl::ObjCProtocol: |
| 2533 | return Sema::OCK_Protocol; |
| 2534 | case Decl::ObjCCategory: |
| 2535 | if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
| 2536 | return Sema::OCK_ClassExtension; |
| 2537 | else |
| 2538 | return Sema::OCK_Category; |
| 2539 | case Decl::ObjCImplementation: |
| 2540 | return Sema::OCK_Implementation; |
| 2541 | case Decl::ObjCCategoryImpl: |
| 2542 | return Sema::OCK_CategoryImplementation; |
| 2543 | |
| 2544 | default: |
| 2545 | return Sema::OCK_None; |
| 2546 | } |
| 2547 | } |
| 2548 | |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2549 | // Note: For class/category implementations, allMethods is always null. |
Robert Wilhelm | 0111e4d | 2013-07-17 21:14:35 +0000 | [diff] [blame] | 2550 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
Fariborz Jahanian | 80f8aca | 2013-07-17 00:05:08 +0000 | [diff] [blame] | 2551 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2552 | if (getObjCContainerKind() == Sema::OCK_None) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2553 | return nullptr; |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2554 | |
| 2555 | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 2556 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2557 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 2558 | Decl *ClassDecl = cast<Decl>(OCD); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 2559 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | bool isInterfaceDeclKind = |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 2561 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 2562 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2563 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2564 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 2565 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 2566 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 2567 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 2568 | |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2569 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2570 | ObjCMethodDecl *Method = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2571 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2572 | |
| 2573 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2574 | if (Method->isInstanceMethod()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2575 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2576 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2578 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 2580 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2581 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2582 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2583 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 2584 | Method->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2585 | } else { |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2586 | if (PrevMethod) { |
Argyrios Kyrtzidis | 3a919e7 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 2587 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2588 | if (!Context.getSourceManager().isInSystemHeader( |
| 2589 | Method->getLocation())) |
| 2590 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 2591 | << Method->getDeclName(); |
| 2592 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 2593 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2594 | InsMap[Method->getSelector()] = Method; |
| 2595 | /// The following allows us to typecheck messages to "id". |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2596 | AddInstanceMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2597 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2598 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2599 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2600 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2602 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 2604 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2605 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2606 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2607 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 2608 | Method->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2609 | } else { |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2610 | if (PrevMethod) { |
Argyrios Kyrtzidis | 3a919e7 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 2611 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2612 | if (!Context.getSourceManager().isInSystemHeader( |
| 2613 | Method->getLocation())) |
| 2614 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 2615 | << Method->getDeclName(); |
| 2616 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 2617 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2618 | ClsMap[Method->getSelector()] = Method; |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2619 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | } |
Douglas Gregor | b892d70 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 2623 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 2624 | // Nothing to do here. |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2625 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 2626 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | // By the same token, they are also used to add new properties. No |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 2628 | // need to compare the added property to those in the class. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2629 | |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 2630 | if (C->IsClassExtension()) { |
| 2631 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 2632 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 2633 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2634 | } |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2635 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2636 | if (CDecl->getIdentifier()) |
| 2637 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 2638 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 2639 | // and adds them to the DeclContext and global method pools. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2640 | for (auto *I : CDecl->properties()) |
| 2641 | ProcessPropertyDecl(I, CDecl); |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 2642 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2643 | } |
| 2644 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 2645 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 2646 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2647 | // Any property declared in a class extension might have user |
| 2648 | // declared setter or getter in current class extension or one |
| 2649 | // of the other class extensions. Mark them as synthesized as |
| 2650 | // property will be synthesized when property with same name is |
| 2651 | // seen in the @implementation. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2652 | for (const auto *Ext : IDecl->visible_extensions()) { |
| 2653 | for (const auto *Property : Ext->properties()) { |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2654 | // Skip over properties declared @dynamic |
| 2655 | if (const ObjCPropertyImplDecl *PIDecl |
| 2656 | = IC->FindPropertyImplDecl(Property->getIdentifier())) |
| 2657 | if (PIDecl->getPropertyImplementation() |
| 2658 | == ObjCPropertyImplDecl::Dynamic) |
| 2659 | continue; |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2660 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2661 | for (const auto *Ext : IDecl->visible_extensions()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2662 | if (ObjCMethodDecl *GetterMethod |
| 2663 | = Ext->getInstanceMethod(Property->getGetterName())) |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2664 | GetterMethod->setPropertyAccessor(true); |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2665 | if (!Property->isReadOnly()) |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2666 | if (ObjCMethodDecl *SetterMethod |
| 2667 | = Ext->getInstanceMethod(Property->getSetterName())) |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2668 | SetterMethod->setPropertyAccessor(true); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2669 | } |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2670 | } |
| 2671 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 2672 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 2673 | AtomicPropertySetterGetterRules(IC, IDecl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2674 | DiagnoseOwningPropertyGetterSynthesis(IC); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2675 | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
| 2676 | if (IDecl->hasDesignatedInitializers()) |
| 2677 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
| 2678 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2679 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2680 | if (IDecl->getSuperClass() == nullptr) { |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2681 | // This class has no superclass, so check that it has been marked with |
| 2682 | // __attribute((objc_root_class)). |
| 2683 | if (!HasRootClassAttr) { |
| 2684 | SourceLocation DeclLoc(IDecl->getLocation()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2685 | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2686 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 2687 | << IDecl->getIdentifier(); |
| 2688 | // See if NSObject is in the current scope, and if it is, suggest |
| 2689 | // adding " : NSObject " to the class declaration. |
| 2690 | NamedDecl *IF = LookupSingleName(TUScope, |
| 2691 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 2692 | DeclLoc, LookupOrdinaryName); |
| 2693 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 2694 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 2695 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 2696 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 2697 | } else { |
| 2698 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 2699 | } |
| 2700 | } |
| 2701 | } else if (HasRootClassAttr) { |
| 2702 | // Complain that only root classes may have this attribute. |
| 2703 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 2704 | } |
| 2705 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2706 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2707 | while (IDecl->getSuperClass()) { |
| 2708 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 2709 | IDecl = IDecl->getSuperClass(); |
| 2710 | } |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2711 | } |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 2712 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2713 | SetIvarInitializers(IC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2715 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 2716 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2718 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2719 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 97a5887 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 2720 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2721 | if (ObjCCategoryDecl *Cat |
| 2722 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 2723 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2724 | } |
| 2725 | } |
| 2726 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2727 | if (isInterfaceDeclKind) { |
| 2728 | // Reject invalid vardecls. |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2729 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 1806239 | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2730 | DeclGroupRef DG = allTUVars[i].get(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2731 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 2732 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 2733 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 8745416 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 2734 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | b31cb7f | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 2735 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2736 | } |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 2737 | } |
Fariborz Jahanian | 10af879 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 2738 | ActOnObjCContainerFinishDefinition(); |
Argyrios Kyrtzidis | b4a686d | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 2739 | |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2740 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 1806239 | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 2741 | DeclGroupRef DG = allTUVars[i].get(); |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 2742 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 2743 | (*I)->setTopLevelDeclInObjCContainer(); |
Argyrios Kyrtzidis | b4a686d | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 2744 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 2745 | } |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2746 | |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 2747 | ActOnDocumentableDecl(ClassDecl); |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2748 | return ClassDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 2753 | /// objective-c's type qualifier from the parser version of the same info. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2754 | static Decl::ObjCDeclQualifier |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2755 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
John McCall | 09e2c52 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 2756 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2759 | /// \brief Check whether the declared result type of the given Objective-C |
| 2760 | /// method declaration is compatible with the method's class. |
| 2761 | /// |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2762 | static Sema::ResultTypeCompatibilityKind |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2763 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 2764 | ObjCInterfaceDecl *CurrentClass) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2765 | QualType ResultType = Method->getReturnType(); |
| 2766 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2767 | // If an Objective-C method inherits its related result type, then its |
| 2768 | // declared result type must be compatible with its own class type. The |
| 2769 | // declared result type is compatible if: |
| 2770 | if (const ObjCObjectPointerType *ResultObjectType |
| 2771 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 2772 | // - it is id or qualified id, or |
| 2773 | if (ResultObjectType->isObjCIdType() || |
| 2774 | ResultObjectType->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2775 | return Sema::RTC_Compatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2776 | |
| 2777 | if (CurrentClass) { |
| 2778 | if (ObjCInterfaceDecl *ResultClass |
| 2779 | = ResultObjectType->getInterfaceDecl()) { |
| 2780 | // - it is the same as the method's class type, or |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2781 | if (declaresSameEntity(CurrentClass, ResultClass)) |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2782 | return Sema::RTC_Compatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2783 | |
| 2784 | // - it is a superclass of the method's class type |
| 2785 | if (ResultClass->isSuperClassOf(CurrentClass)) |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2786 | return Sema::RTC_Compatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2787 | } |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 2788 | } else { |
| 2789 | // Any Objective-C pointer type might be acceptable for a protocol |
| 2790 | // method; we just don't know. |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2791 | return Sema::RTC_Unknown; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2792 | } |
| 2793 | } |
| 2794 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2795 | return Sema::RTC_Incompatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2798 | namespace { |
| 2799 | /// A helper class for searching for methods which a particular method |
| 2800 | /// overrides. |
| 2801 | class OverrideSearch { |
Daniel Dunbar | b732fce | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 2802 | public: |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2803 | Sema &S; |
| 2804 | ObjCMethodDecl *Method; |
Daniel Dunbar | b732fce | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 2805 | llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2806 | bool Recursive; |
| 2807 | |
| 2808 | public: |
| 2809 | OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) { |
| 2810 | Selector selector = method->getSelector(); |
| 2811 | |
| 2812 | // Bypass this search if we've never seen an instance/class method |
| 2813 | // with this selector before. |
| 2814 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 2815 | if (it == S.MethodPool.end()) { |
Axel Naumann | 0ec56b7 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 2816 | if (!S.getExternalSource()) return; |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2817 | S.ReadMethodPool(selector); |
| 2818 | |
| 2819 | it = S.MethodPool.find(selector); |
| 2820 | if (it == S.MethodPool.end()) |
| 2821 | return; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2822 | } |
| 2823 | ObjCMethodList &list = |
| 2824 | method->isInstanceMethod() ? it->second.first : it->second.second; |
| 2825 | if (!list.Method) return; |
| 2826 | |
| 2827 | ObjCContainerDecl *container |
| 2828 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 2829 | |
| 2830 | // Prevent the search from reaching this container again. This is |
| 2831 | // important with categories, which override methods from the |
| 2832 | // interface and each other. |
Douglas Gregor | c968334 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 2833 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) { |
| 2834 | searchFromContainer(container); |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2835 | if (ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
| 2836 | searchFromContainer(Interface); |
Douglas Gregor | c968334 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 2837 | } else { |
| 2838 | searchFromContainer(container); |
| 2839 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2840 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2841 | |
Daniel Dunbar | b732fce | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 2842 | typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2843 | iterator begin() const { return Overridden.begin(); } |
| 2844 | iterator end() const { return Overridden.end(); } |
| 2845 | |
| 2846 | private: |
| 2847 | void searchFromContainer(ObjCContainerDecl *container) { |
| 2848 | if (container->isInvalidDecl()) return; |
| 2849 | |
| 2850 | switch (container->getDeclKind()) { |
| 2851 | #define OBJCCONTAINER(type, base) \ |
| 2852 | case Decl::type: \ |
| 2853 | searchFrom(cast<type##Decl>(container)); \ |
| 2854 | break; |
| 2855 | #define ABSTRACT_DECL(expansion) |
| 2856 | #define DECL(type, base) \ |
| 2857 | case Decl::type: |
| 2858 | #include "clang/AST/DeclNodes.inc" |
| 2859 | llvm_unreachable("not an ObjC container!"); |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | void searchFrom(ObjCProtocolDecl *protocol) { |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 2864 | if (!protocol->hasDefinition()) |
| 2865 | return; |
| 2866 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2867 | // A method in a protocol declaration overrides declarations from |
| 2868 | // referenced ("parent") protocols. |
| 2869 | search(protocol->getReferencedProtocols()); |
| 2870 | } |
| 2871 | |
| 2872 | void searchFrom(ObjCCategoryDecl *category) { |
| 2873 | // A method in a category declaration overrides declarations from |
| 2874 | // the main class and from protocols the category references. |
Douglas Gregor | c968334 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 2875 | // The main class is handled in the constructor. |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2876 | search(category->getReferencedProtocols()); |
| 2877 | } |
| 2878 | |
| 2879 | void searchFrom(ObjCCategoryImplDecl *impl) { |
| 2880 | // A method in a category definition that has a category |
| 2881 | // declaration overrides declarations from the category |
| 2882 | // declaration. |
| 2883 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 2884 | search(category); |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2885 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 2886 | search(Interface); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2887 | |
| 2888 | // Otherwise it overrides declarations from the class. |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2889 | } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) { |
| 2890 | search(Interface); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | void searchFrom(ObjCInterfaceDecl *iface) { |
| 2895 | // A method in a class declaration overrides declarations from |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2896 | if (!iface->hasDefinition()) |
| 2897 | return; |
| 2898 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2899 | // - categories, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2900 | for (auto *Cat : iface->known_categories()) |
| 2901 | search(Cat); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2902 | |
| 2903 | // - the super class, and |
| 2904 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 2905 | search(super); |
| 2906 | |
| 2907 | // - any referenced protocols. |
| 2908 | search(iface->getReferencedProtocols()); |
| 2909 | } |
| 2910 | |
| 2911 | void searchFrom(ObjCImplementationDecl *impl) { |
| 2912 | // A method in a class implementation overrides declarations from |
| 2913 | // the class interface. |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2914 | if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) |
| 2915 | search(Interface); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | |
| 2919 | void search(const ObjCProtocolList &protocols) { |
| 2920 | for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end(); |
| 2921 | i != e; ++i) |
| 2922 | search(*i); |
| 2923 | } |
| 2924 | |
| 2925 | void search(ObjCContainerDecl *container) { |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2926 | // Check for a method in this container which matches this selector. |
| 2927 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
Argyrios Kyrtzidis | 04593d0 | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 2928 | Method->isInstanceMethod(), |
| 2929 | /*AllowHidden=*/true); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2930 | |
| 2931 | // If we find one, record it and bail out. |
| 2932 | if (meth) { |
| 2933 | Overridden.insert(meth); |
| 2934 | return; |
| 2935 | } |
| 2936 | |
| 2937 | // Otherwise, search for methods that a hypothetical method here |
| 2938 | // would have overridden. |
| 2939 | |
| 2940 | // Note that we're now in a recursive case. |
| 2941 | Recursive = true; |
| 2942 | |
| 2943 | searchFromContainer(container); |
| 2944 | } |
| 2945 | }; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2948 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 2949 | ObjCInterfaceDecl *CurrentClass, |
| 2950 | ResultTypeCompatibilityKind RTC) { |
| 2951 | // Search for overridden methods and merge information down from them. |
| 2952 | OverrideSearch overrides(*this, ObjCMethod); |
| 2953 | // Keep track if the method overrides any method in the class's base classes, |
| 2954 | // its protocols, or its categories' protocols; we will keep that info |
| 2955 | // in the ObjCMethodDecl. |
| 2956 | // For this info, a method in an implementation is not considered as |
| 2957 | // overriding the same method in the interface or its categories. |
| 2958 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
| 2959 | for (OverrideSearch::iterator |
| 2960 | i = overrides.begin(), e = overrides.end(); i != e; ++i) { |
| 2961 | ObjCMethodDecl *overridden = *i; |
| 2962 | |
Argyrios Kyrtzidis | e7a7772 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 2963 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 2964 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 2965 | CurrentClass != overridden->getClassInterface() || |
| 2966 | overridden->isOverriding()) { |
| 2967 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 2968 | |
| 2969 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 2970 | // OverrideSearch will return as "overridden" the same method in the |
| 2971 | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
| 2972 | // check whether a category of a base class introduced a method with the |
| 2973 | // same selector, after the interface method declaration. |
| 2974 | // To avoid unnecessary lookups in the majority of cases, we use the |
| 2975 | // extra info bits in GlobalMethodPool to check whether there were any |
| 2976 | // category methods with this selector. |
| 2977 | GlobalMethodPool::iterator It = |
| 2978 | MethodPool.find(ObjCMethod->getSelector()); |
| 2979 | if (It != MethodPool.end()) { |
| 2980 | ObjCMethodList &List = |
| 2981 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 2982 | unsigned CategCount = List.getBits(); |
| 2983 | if (CategCount > 0) { |
| 2984 | // If the method is in a category we'll do lookup if there were at |
| 2985 | // least 2 category methods recorded, otherwise only one will do. |
| 2986 | if (CategCount > 1 || |
| 2987 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 2988 | OverrideSearch overrides(*this, overridden); |
| 2989 | for (OverrideSearch::iterator |
| 2990 | OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) { |
| 2991 | ObjCMethodDecl *SuperOverridden = *OI; |
Argyrios Kyrtzidis | ab3d509 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 2992 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 2993 | CurrentClass != SuperOverridden->getClassInterface()) { |
Argyrios Kyrtzidis | e7a7772 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 2994 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 2995 | overridden->setOverriding(true); |
| 2996 | break; |
| 2997 | } |
| 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | } |
| 3002 | } |
| 3003 | } |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Propagate down the 'related result type' bit from overridden methods. |
| 3006 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
| 3007 | ObjCMethod->SetRelatedResultType(); |
| 3008 | |
| 3009 | // Then merge the declarations. |
| 3010 | mergeObjCMethodDecls(ObjCMethod, overridden); |
| 3011 | |
| 3012 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 3013 | continue; // Conflicting properties are detected elsewhere. |
| 3014 | |
| 3015 | // Check for overriding methods |
| 3016 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
| 3017 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 3018 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 3019 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
| 3020 | |
| 3021 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
Fariborz Jahanian | c4133a4 | 2012-07-05 22:26:07 +0000 | [diff] [blame] | 3022 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 3023 | !overridden->isImplicit() /* not meant for properties */) { |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3024 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 3025 | E = ObjCMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3026 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 3027 | PrevE = overridden->param_end(); |
| 3028 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3029 | assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 3030 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 3031 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 3032 | // If type of argument of method in this class does not match its |
| 3033 | // respective argument type in the super class method, issue warning; |
| 3034 | if (!Context.typesAreCompatible(T1, T2)) { |
| 3035 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 3036 | << T1 << T2; |
| 3037 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 3038 | break; |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 3045 | } |
| 3046 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3047 | Decl *Sema::ActOnMethodDeclaration( |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3048 | Scope *S, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3049 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3050 | tok::TokenKind MethodType, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3051 | ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3052 | ArrayRef<SourceLocation> SelectorLocs, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3053 | Selector Sel, |
| 3054 | // optional arguments. The number of types/arguments is obtained |
| 3055 | // from the Sel.getNumArgs(). |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3056 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3057 | DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3058 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 3059 | bool isVariadic, bool MethodDefinition) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 3060 | // Make sure we can establish a context for the method. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3061 | if (!CurContext->isObjCContainer()) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 3062 | Diag(MethodLoc, diag::error_missing_method_context); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3063 | return nullptr; |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 3064 | } |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3065 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 3066 | Decl *ClassDecl = cast<Decl>(OCD); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3067 | QualType resultDeclType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3069 | bool HasRelatedResultType = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3070 | TypeSourceInfo *ReturnTInfo = nullptr; |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 3071 | if (ReturnType) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3072 | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 3074 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3075 | return nullptr; |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 3076 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3077 | HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType()); |
Fariborz Jahanian | aab24a6 | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 3078 | } else { // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3079 | resultDeclType = Context.getObjCIdType(); |
Fariborz Jahanian | feb4fa1 | 2011-07-21 17:38:14 +0000 | [diff] [blame] | 3080 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3081 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
Fariborz Jahanian | aab24a6 | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 3082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3084 | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
| 3085 | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
| 3086 | MethodType == tok::minus, isVariadic, |
| 3087 | /*isPropertyAccessor=*/false, |
| 3088 | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
| 3089 | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional |
| 3090 | : ObjCMethodDecl::Required, |
| 3091 | HasRelatedResultType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3093 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | 7db638d | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 3095 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 3096 | QualType ArgType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3097 | TypeSourceInfo *DI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3099 | if (!ArgInfo[i].Type) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 3100 | ArgType = Context.getObjCIdType(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3101 | DI = nullptr; |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3102 | } else { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 3103 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3106 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
| 3107 | LookupOrdinaryName, ForRedeclaration); |
| 3108 | LookupName(R, S); |
| 3109 | if (R.isSingleResult()) { |
| 3110 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 3111 | if (S->isDeclScope(PrevDecl)) { |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 3112 | Diag(ArgInfo[i].NameLoc, |
| 3113 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 3114 | : diag::warn_method_param_declaration)) |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3115 | << ArgInfo[i].Name; |
| 3116 | Diag(PrevDecl->getLocation(), |
| 3117 | diag::note_previous_declaration); |
| 3118 | } |
| 3119 | } |
| 3120 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3121 | SourceLocation StartLoc = DI |
| 3122 | ? DI->getTypeLoc().getBeginLoc() |
| 3123 | : ArgInfo[i].NameLoc; |
| 3124 | |
John McCall | 81ef3e6 | 2011-04-23 02:46:06 +0000 | [diff] [blame] | 3125 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 3126 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3127 | ArgType, DI, SC_None); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | |
John McCall | 7079886 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 3129 | Param->setObjCMethodScopeInfo(i); |
| 3130 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3131 | Param->setObjCDeclQualifier( |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3132 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 3134 | // Apply the attributes to the parameter. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3135 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
Fariborz Jahanian | 47b1d96 | 2012-01-14 18:44:35 +0000 | [diff] [blame] | 3137 | if (Param->hasAttr<BlocksAttr>()) { |
| 3138 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 3139 | Param->setInvalidDecl(); |
| 3140 | } |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3141 | S->AddDecl(Param); |
| 3142 | IdResolver.AddDecl(Param); |
| 3143 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3144 | Params.push_back(Param); |
| 3145 | } |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3146 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3147 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3148 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3149 | QualType ArgType = Param->getType(); |
| 3150 | if (ArgType.isNull()) |
| 3151 | ArgType = Context.getObjCIdType(); |
| 3152 | else |
| 3153 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3154 | ArgType = Context.getAdjustedParameterType(ArgType); |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 3155 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3156 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3157 | Params.push_back(Param); |
| 3158 | } |
| 3159 | |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3160 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3161 | ObjCMethod->setObjCDeclQualifier( |
| 3162 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3165 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3167 | // Add the method now. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3168 | const ObjCMethodDecl *PrevMethod = nullptr; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3169 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3170 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3171 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 3172 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3173 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3174 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 3175 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3176 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3177 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3178 | ObjCMethodDecl *IMD = nullptr; |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 3179 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) |
| 3180 | IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 3181 | ObjCMethod->isInstanceMethod()); |
Fariborz Jahanian | 38b3bd8 | 2013-07-09 22:02:20 +0000 | [diff] [blame] | 3182 | if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() && |
| 3183 | !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) { |
| 3184 | // merge the attribute into implementation. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3185 | ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context, |
| 3186 | ObjCMethod->getLocation())); |
Fariborz Jahanian | 38b3bd8 | 2013-07-09 22:02:20 +0000 | [diff] [blame] | 3187 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3188 | if (isa<ObjCCategoryImplDecl>(ImpDecl)) { |
| 3189 | ObjCMethodFamily family = |
| 3190 | ObjCMethod->getSelector().getMethodFamily(); |
| 3191 | if (family == OMF_dealloc && IMD && IMD->isOverriding()) |
| 3192 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 3193 | << ObjCMethod->getDeclName(); |
Fariborz Jahanian | ec23678 | 2011-12-06 00:02:41 +0000 | [diff] [blame] | 3194 | } |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3195 | } else { |
| 3196 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3197 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3198 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3199 | if (PrevMethod) { |
| 3200 | // You can never have two method definitions with the same name. |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3201 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3202 | << ObjCMethod->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3203 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | fbff0c4 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 3204 | ObjCMethod->setInvalidDecl(); |
| 3205 | return ObjCMethod; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3208 | // If this Objective-C method does not have a related result type, but we |
| 3209 | // are allowed to infer related result types, try to do so based on the |
| 3210 | // method family. |
| 3211 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 3212 | if (!CurrentClass) { |
| 3213 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 3214 | CurrentClass = Cat->getClassInterface(); |
| 3215 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 3216 | CurrentClass = Impl->getClassInterface(); |
| 3217 | else if (ObjCCategoryImplDecl *CatImpl |
| 3218 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 3219 | CurrentClass = CatImpl->getClassInterface(); |
| 3220 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3221 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3222 | ResultTypeCompatibilityKind RTC |
| 3223 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3224 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3225 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3226 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3227 | bool ARCError = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3228 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 3229 | ARCError = CheckARCMethodDecl(ObjCMethod); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3231 | // Infer the related result type when possible. |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3232 | if (!ARCError && RTC == Sema::RTC_Compatible && |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3233 | !ObjCMethod->hasRelatedResultType() && |
| 3234 | LangOpts.ObjCInferRelatedResultType) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3235 | bool InferRelatedResultType = false; |
| 3236 | switch (ObjCMethod->getMethodFamily()) { |
| 3237 | case OMF_None: |
| 3238 | case OMF_copy: |
| 3239 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 3240 | case OMF_finalize: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3241 | case OMF_mutableCopy: |
| 3242 | case OMF_release: |
| 3243 | case OMF_retainCount: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3244 | case OMF_performSelector: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3245 | break; |
| 3246 | |
| 3247 | case OMF_alloc: |
| 3248 | case OMF_new: |
| 3249 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
| 3250 | break; |
| 3251 | |
| 3252 | case OMF_init: |
| 3253 | case OMF_autorelease: |
| 3254 | case OMF_retain: |
| 3255 | case OMF_self: |
| 3256 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 3257 | break; |
| 3258 | } |
| 3259 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3260 | if (InferRelatedResultType) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3261 | ObjCMethod->SetRelatedResultType(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3262 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 3263 | |
| 3264 | ActOnDocumentableDecl(ObjCMethod); |
| 3265 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3266 | return ObjCMethod; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3269 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Fariborz Jahanian | 58a7649 | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 3270 | // Following is also an error. But it is caused by a missing @end |
| 3271 | // and diagnostic is issued elsewhere. |
Argyrios Kyrtzidis | fce79eb | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 3272 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3273 | return false; |
Argyrios Kyrtzidis | fce79eb | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 3274 | |
| 3275 | // If we switched context to translation unit while we are still lexically in |
| 3276 | // an objc container, it means the parser missed emitting an error. |
| 3277 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 3278 | return false; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3279 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 3280 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 3281 | D->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 3283 | return true; |
| 3284 | } |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3285 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 3286 | /// Called whenever \@defs(ClassName) is encountered in the source. Inserts the |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3287 | /// instance variables of ClassName into Decls. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3288 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3289 | IdentifierInfo *ClassName, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3290 | SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3291 | // Check that ClassName is a valid class |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3292 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3293 | if (!Class) { |
| 3294 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 3295 | return; |
| 3296 | } |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 3297 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 0468fb9 | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 3298 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 3299 | return; |
| 3300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3302 | // Collect the instance variables |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3303 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3304 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 3305 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3306 | for (unsigned i = 0; i < Ivars.size(); i++) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3307 | const FieldDecl* ID = cast<FieldDecl>(Ivars[i]); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3308 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3309 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 3310 | /*FIXME: StartL=*/ID->getLocation(), |
| 3311 | ID->getLocation(), |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 3312 | ID->getIdentifier(), ID->getType(), |
| 3313 | ID->getBitWidth()); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3314 | Decls.push_back(FD); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 3315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3317 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3318 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3319 | D != Decls.end(); ++D) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3320 | FieldDecl *FD = cast<FieldDecl>(*D); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3321 | if (getLangOpts().CPlusPlus) |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3322 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3323 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3324 | Record->addDecl(FD); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3328 | /// \brief Build a type-check a new Objective-C exception variable declaration. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3329 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 3330 | SourceLocation StartLoc, |
| 3331 | SourceLocation IdLoc, |
| 3332 | IdentifierInfo *Id, |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3333 | bool Invalid) { |
| 3334 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 3335 | // duration shall not be qualified by an address-space qualifier." |
| 3336 | // Since all parameters have automatic store duration, they can not have |
| 3337 | // an address space. |
| 3338 | if (T.getAddressSpace() != 0) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3339 | Diag(IdLoc, diag::err_arg_with_address_space); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3340 | Invalid = true; |
| 3341 | } |
| 3342 | |
| 3343 | // An @catch parameter must be an unqualified object pointer type; |
| 3344 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 3345 | if (Invalid) { |
| 3346 | // Don't do any further checking. |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 3347 | } else if (T->isDependentType()) { |
| 3348 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3349 | } else if (!T->isObjCObjectPointerType()) { |
| 3350 | Invalid = true; |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3351 | Diag(IdLoc ,diag::err_catch_param_not_objc_type); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3352 | } else if (T->isObjCQualifiedIdType()) { |
| 3353 | Invalid = true; |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3354 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3357 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3358 | T, TInfo, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 3359 | New->setExceptionVariable(true); |
| 3360 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 3361 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3362 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 3363 | Invalid = true; |
| 3364 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3365 | if (Invalid) |
| 3366 | New->setInvalidDecl(); |
| 3367 | return New; |
| 3368 | } |
| 3369 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3370 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3371 | const DeclSpec &DS = D.getDeclSpec(); |
| 3372 | |
| 3373 | // We allow the "register" storage class on exception variables because |
| 3374 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 3375 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 3376 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 3377 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3378 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3379 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3380 | << DeclSpec::getSpecifierName(SCS); |
| 3381 | } |
| 3382 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 3383 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 3384 | diag::err_invalid_thread) |
| 3385 | << DeclSpec::getSpecifierName(TSCS); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3386 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 3387 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3388 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3389 | |
| 3390 | // Check that there are no default arguments inside the type of this |
| 3391 | // exception object (C++ only). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3392 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3393 | CheckExtraCXXDefaultArguments(D); |
| 3394 | |
Argyrios Kyrtzidis | 3215398 | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 3395 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3396 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3397 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3398 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 3399 | D.getSourceRange().getBegin(), |
| 3400 | D.getIdentifierLoc(), |
| 3401 | D.getIdentifier(), |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3402 | D.isInvalidType()); |
| 3403 | |
| 3404 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 3405 | if (D.getCXXScopeSpec().isSet()) { |
| 3406 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 3407 | << D.getCXXScopeSpec().getRange(); |
| 3408 | New->setInvalidDecl(); |
| 3409 | } |
| 3410 | |
| 3411 | // Add the parameter declaration into this scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3412 | S->AddDecl(New); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3413 | if (D.getIdentifier()) |
| 3414 | IdResolver.AddDecl(New); |
| 3415 | |
| 3416 | ProcessDeclAttributes(S, New, D); |
| 3417 | |
| 3418 | if (New->hasAttr<BlocksAttr>()) |
| 3419 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3420 | return New; |
Douglas Gregor | 4e6c0d1 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 3421 | } |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 3422 | |
| 3423 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 3424 | /// initialization. |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3425 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3426 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3427 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
| 3428 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 3429 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 3430 | if (QT->isRecordType()) |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3431 | Ivars.push_back(Iv); |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 3432 | } |
| 3433 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 3434 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3435 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Douglas Gregor | 5b9dc7c | 2011-07-28 14:54:22 +0000 | [diff] [blame] | 3436 | // Load referenced selectors from the external source. |
| 3437 | if (ExternalSource) { |
| 3438 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 3439 | ExternalSource->ReadReferencedSelectors(Sels); |
| 3440 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 3441 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 3442 | } |
| 3443 | |
Fariborz Jahanian | 8b78913 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 3444 | // Warning will be issued only when selector table is |
| 3445 | // generated (which means there is at lease one implementation |
| 3446 | // in the TU). This is to match gcc's behavior. |
| 3447 | if (ReferencedSelectors.empty() || |
| 3448 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3449 | return; |
| 3450 | for (llvm::DenseMap<Selector, SourceLocation>::iterator S = |
| 3451 | ReferencedSelectors.begin(), |
| 3452 | E = ReferencedSelectors.end(); S != E; ++S) { |
| 3453 | Selector Sel = (*S).first; |
| 3454 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
| 3455 | Diag((*S).second, diag::warn_unimplemented_selector) << Sel; |
| 3456 | } |
| 3457 | return; |
| 3458 | } |
Fariborz Jahanian | 4e7f00c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 3459 | |
| 3460 | ObjCIvarDecl * |
| 3461 | Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method, |
| 3462 | const ObjCPropertyDecl *&PDecl) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3463 | if (Method->isClassMethod()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3464 | return nullptr; |
Fariborz Jahanian | 4e7f00c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 3465 | const ObjCInterfaceDecl *IDecl = Method->getClassInterface(); |
| 3466 | if (!IDecl) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3467 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3468 | Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true, |
| 3469 | /*shallowCategoryLookup=*/false, |
| 3470 | /*followSuper=*/false); |
Fariborz Jahanian | 4e7f00c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 3471 | if (!Method || !Method->isPropertyAccessor()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3472 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3473 | if ((PDecl = Method->findPropertyDecl())) |
| 3474 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) { |
| 3475 | // property backing ivar must belong to property's class |
| 3476 | // or be a private ivar in class's implementation. |
| 3477 | // FIXME. fix the const-ness issue. |
| 3478 | IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable( |
| 3479 | IV->getIdentifier()); |
| 3480 | return IV; |
| 3481 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3482 | return nullptr; |
Fariborz Jahanian | 4e7f00c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3485 | namespace { |
| 3486 | /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property |
| 3487 | /// accessor references the backing ivar. |
| 3488 | class UnusedBackingIvarChecker : |
| 3489 | public DataRecursiveASTVisitor<UnusedBackingIvarChecker> { |
| 3490 | public: |
| 3491 | Sema &S; |
| 3492 | const ObjCMethodDecl *Method; |
| 3493 | const ObjCIvarDecl *IvarD; |
| 3494 | bool AccessedIvar; |
| 3495 | bool InvokedSelfMethod; |
| 3496 | |
| 3497 | UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method, |
| 3498 | const ObjCIvarDecl *IvarD) |
| 3499 | : S(S), Method(Method), IvarD(IvarD), |
| 3500 | AccessedIvar(false), InvokedSelfMethod(false) { |
| 3501 | assert(IvarD); |
| 3502 | } |
| 3503 | |
| 3504 | bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 3505 | if (E->getDecl() == IvarD) { |
| 3506 | AccessedIvar = true; |
| 3507 | return false; |
| 3508 | } |
| 3509 | return true; |
| 3510 | } |
| 3511 | |
| 3512 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 3513 | if (E->getReceiverKind() == ObjCMessageExpr::Instance && |
| 3514 | S.isSelfExpr(E->getInstanceReceiver(), Method)) { |
| 3515 | InvokedSelfMethod = true; |
| 3516 | } |
| 3517 | return true; |
| 3518 | } |
| 3519 | }; |
| 3520 | } |
| 3521 | |
| 3522 | void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S, |
| 3523 | const ObjCImplementationDecl *ImplD) { |
| 3524 | if (S->hasUnrecoverableErrorOccurred()) |
Fariborz Jahanian | 4e7f00c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 3525 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3526 | |
| 3527 | for (const auto *CurMethod : ImplD->instance_methods()) { |
| 3528 | unsigned DIAG = diag::warn_unused_property_backing_ivar; |
| 3529 | SourceLocation Loc = CurMethod->getLocation(); |
| 3530 | if (Diags.getDiagnosticLevel(DIAG, Loc) == DiagnosticsEngine::Ignored) |
| 3531 | continue; |
| 3532 | |
| 3533 | const ObjCPropertyDecl *PDecl; |
| 3534 | const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); |
| 3535 | if (!IV) |
| 3536 | continue; |
| 3537 | |
| 3538 | UnusedBackingIvarChecker Checker(*this, CurMethod, IV); |
| 3539 | Checker.TraverseStmt(CurMethod->getBody()); |
| 3540 | if (Checker.AccessedIvar) |
| 3541 | continue; |
| 3542 | |
| 3543 | // Do not issue this warning if backing ivar is used somewhere and accessor |
| 3544 | // implementation makes a self call. This is to prevent false positive in |
| 3545 | // cases where the ivar is accessed by another method that the accessor |
| 3546 | // delegates to. |
| 3547 | if (!IV->isReferenced() || !Checker.InvokedSelfMethod) { |
| 3548 | Diag(Loc, DIAG) << IV; |
| 3549 | Diag(PDecl->getLocation(), diag::note_property_declare); |
| 3550 | } |
Fariborz Jahanian | 4e7f00c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 3551 | } |
| 3552 | } |