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