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