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