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